1 /*
2    Unix SMB/CIFS implementation.
3    process incoming packets - main loop
4    Copyright (C) Andrew Tridgell	2004-2005
5    Copyright (C) Stefan Metzmacher	2004-2005
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 
22 #include "includes.h"
23 #include "smbd/service_task.h"
24 #include "smbd/service_stream.h"
25 #include "smbd/service.h"
26 #include "smb_server/smb_server.h"
27 #include "smb_server/service_smb_proto.h"
28 #include "lib/messaging/irpc.h"
29 #include "lib/stream/packet.h"
30 #include "libcli/smb2/smb2.h"
31 #include "smb_server/smb2/smb2_server.h"
32 #include "system/network.h"
33 #include "lib/socket/netif.h"
34 #include "param/share.h"
35 
smbsrv_recv_generic_request(void * private,DATA_BLOB blob)36 static NTSTATUS smbsrv_recv_generic_request(void *private, DATA_BLOB blob)
37 {
38 	NTSTATUS status;
39 	struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
40 	uint32_t protocol_version;
41 
42 	/* see if its a special NBT packet */
43 	if (CVAL(blob.data,0) != 0) {
44 		status = smbsrv_init_smb_connection(smb_conn);
45 		NT_STATUS_NOT_OK_RETURN(status);
46 		packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
47 		return smbsrv_recv_smb_request(smb_conn, blob);
48 	}
49 
50 	if (blob.length < (NBT_HDR_SIZE + MIN_SMB_SIZE)) {
51 		DEBUG(2,("Invalid SMB packet length count %ld\n", (long)blob.length));
52 		smbsrv_terminate_connection(smb_conn, "Invalid SMB packet");
53 		return NT_STATUS_OK;
54 	}
55 
56 	protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
57 
58 	switch (protocol_version) {
59 	case SMB_MAGIC:
60 		status = smbsrv_init_smb_connection(smb_conn);
61 		NT_STATUS_NOT_OK_RETURN(status);
62 		packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
63 		return smbsrv_recv_smb_request(smb_conn, blob);
64 	case SMB2_MAGIC:
65 		if (lp_srv_maxprotocol() < PROTOCOL_SMB2) break;
66 		status = smbsrv_init_smb2_connection(smb_conn);
67 		NT_STATUS_NOT_OK_RETURN(status);
68 		packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
69 		return smbsrv_recv_smb2_request(smb_conn, blob);
70 	}
71 
72 	DEBUG(2,("Invalid SMB packet: protocol prefix: 0x%08X\n", protocol_version));
73 	smbsrv_terminate_connection(smb_conn, "NON-SMB packet");
74 	return NT_STATUS_OK;
75 }
76 
77 /*
78   close the socket and shutdown a server_context
79 */
smbsrv_terminate_connection(struct smbsrv_connection * smb_conn,const char * reason)80 void smbsrv_terminate_connection(struct smbsrv_connection *smb_conn, const char *reason)
81 {
82 	stream_terminate_connection(smb_conn->connection, reason);
83 }
84 
85 /*
86   called when a SMB socket becomes readable
87 */
smbsrv_recv(struct stream_connection * conn,uint16_t flags)88 static void smbsrv_recv(struct stream_connection *conn, uint16_t flags)
89 {
90 	struct smbsrv_connection *smb_conn = talloc_get_type(conn->private,
91 							     struct smbsrv_connection);
92 
93 	DEBUG(10,("smbsrv_recv\n"));
94 
95 	packet_recv(smb_conn->packet);
96 
97 	/* free up temporary memory */
98 	lp_talloc_free();
99 }
100 
101 /*
102   called when a SMB socket becomes writable
103 */
smbsrv_send(struct stream_connection * conn,uint16_t flags)104 static void smbsrv_send(struct stream_connection *conn, uint16_t flags)
105 {
106 	struct smbsrv_connection *smb_conn = talloc_get_type(conn->private,
107 							     struct smbsrv_connection);
108 	packet_queue_run(smb_conn->packet);
109 }
110 
111 /*
112   handle socket recv errors
113 */
smbsrv_recv_error(void * private,NTSTATUS status)114 static void smbsrv_recv_error(void *private, NTSTATUS status)
115 {
116 	struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
117 
118 	smbsrv_terminate_connection(smb_conn, nt_errstr(status));
119 }
120 
121 /*
122   initialise a server_context from a open socket and register a event handler
123   for reading from that socket
124 */
smbsrv_accept(struct stream_connection * conn)125 static void smbsrv_accept(struct stream_connection *conn)
126 {
127 	struct smbsrv_connection *smb_conn;
128 
129 	DEBUG(5,("smbsrv_accept\n"));
130 
131 	smb_conn = talloc_zero(conn, struct smbsrv_connection);
132 	if (!smb_conn) {
133 		stream_terminate_connection(conn, "out of memory");
134 		return;
135 	}
136 
137 	smb_conn->packet = packet_init(smb_conn);
138 	if (!smb_conn->packet) {
139 		smbsrv_terminate_connection(smb_conn, "out of memory");
140 		return;
141 	}
142 	packet_set_private(smb_conn->packet, smb_conn);
143 	packet_set_socket(smb_conn->packet, conn->socket);
144 	packet_set_callback(smb_conn->packet, smbsrv_recv_generic_request);
145 	packet_set_full_request(smb_conn->packet, packet_full_request_nbt);
146 	packet_set_error_handler(smb_conn->packet, smbsrv_recv_error);
147 	packet_set_event_context(smb_conn->packet, conn->event.ctx);
148 	packet_set_fde(smb_conn->packet, conn->event.fde);
149 	packet_set_serialise(smb_conn->packet);
150 
151 	smb_conn->connection = conn;
152 	conn->private = smb_conn;
153 
154 	irpc_add_name(conn->msg_ctx, "smb_server");
155 
156 	smb_conn->statistics.connect_time = timeval_current();
157 
158 	smbsrv_management_init(smb_conn);
159 
160 	if (!NT_STATUS_IS_OK(share_get_context(smb_conn, &(smb_conn->share_context)))) {
161 		smbsrv_terminate_connection(smb_conn, "share_init failed!");
162 		return;
163 	}
164 }
165 
166 static const struct stream_server_ops smb_stream_ops = {
167 	.name			= "smbsrv",
168 	.accept_connection	= smbsrv_accept,
169 	.recv_handler		= smbsrv_recv,
170 	.send_handler		= smbsrv_send,
171 };
172 
173 /*
174   setup a listening socket on all the SMB ports for a particular address
175 */
smb_add_socket(struct event_context * event_context,const struct model_ops * model_ops,const char * address)176 static NTSTATUS smb_add_socket(struct event_context *event_context,
177 			       const struct model_ops *model_ops,
178 			       const char *address)
179 {
180 	const char **ports = lp_smb_ports();
181 	int i;
182 	NTSTATUS status;
183 
184 	for (i=0;ports[i];i++) {
185 		uint16_t port = atoi(ports[i]);
186 		if (port == 0) continue;
187 		status = stream_setup_socket(event_context, model_ops, &smb_stream_ops,
188 					     "ipv4", address, &port, NULL);
189 		NT_STATUS_NOT_OK_RETURN(status);
190 	}
191 
192 	return NT_STATUS_OK;
193 }
194 
195 /*
196   open the smb server sockets
197 */
smbsrv_task_init(struct task_server * task)198 static void smbsrv_task_init(struct task_server *task)
199 {
200 	NTSTATUS status;
201 
202 	task_server_set_title(task, "task[smbsrv]");
203 
204 	if (lp_interfaces() && lp_bind_interfaces_only()) {
205 		int num_interfaces = iface_count();
206 		int i;
207 
208 		/* We have been given an interfaces line, and been
209 		   told to only bind to those interfaces. Create a
210 		   socket per interface and bind to only these.
211 		*/
212 		for(i = 0; i < num_interfaces; i++) {
213 			const char *address = iface_n_ip(i);
214 			status = smb_add_socket(task->event_ctx, task->model_ops, address);
215 			if (!NT_STATUS_IS_OK(status)) goto failed;
216 		}
217 	} else {
218 		/* Just bind to lp_socket_address() (usually 0.0.0.0) */
219 		status = smb_add_socket(task->event_ctx, task->model_ops, lp_socket_address());
220 		if (!NT_STATUS_IS_OK(status)) goto failed;
221 	}
222 
223 	return;
224 failed:
225 	task_server_terminate(task, "Failed to startup smb server task");
226 }
227 
228 /*
229   called on startup of the smb server service It's job is to start
230   listening on all configured sockets
231 */
smbsrv_init(struct event_context * event_context,const struct model_ops * model_ops)232 static NTSTATUS smbsrv_init(struct event_context *event_context,
233 			    const struct model_ops *model_ops)
234 {
235 	return task_server_startup(event_context, model_ops, smbsrv_task_init);
236 }
237 
238 /* called at smbd startup - register ourselves as a server service */
server_service_smb_init(void)239 NTSTATUS server_service_smb_init(void)
240 {
241 	return register_server_service("smb", smbsrv_init);
242 }
243