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
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.
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.
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.
23 #include "lib/events/events.h"
24 #include "system/time.h"
25 #include "dlinklist.h"
26 #include "smbd/service_stream.h"
27 #include "smb_server/smb_server.h"
28 #include "lib/messaging/irpc.h"
29 #include "lib/stream/packet.h"
30 #include "libcli/smb2/smb2.h"
32 static NTSTATUS smbsrv_recv_generic_request(void *private, DATA_BLOB blob)
35 struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
36 uint32_t protocol_version;
38 /* see if its a special NBT packet */
39 if (CVAL(blob.data,0) != 0) {
40 status = smbsrv_init_smb_connection(smb_conn);
41 NT_STATUS_NOT_OK_RETURN(status);
42 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
43 return smbsrv_recv_smb_request(smb_conn, blob);
46 if (blob.length < (NBT_HDR_SIZE + MIN_SMB_SIZE)) {
47 DEBUG(2,("Invalid SMB packet length count %d\n", blob.length));
48 smbsrv_terminate_connection(smb_conn, "Invalid SMB packet");
52 protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
54 switch (protocol_version) {
56 status = smbsrv_init_smb_connection(smb_conn);
57 NT_STATUS_NOT_OK_RETURN(status);
58 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
59 return smbsrv_recv_smb_request(smb_conn, blob);
61 if (!lp_parm_bool(-1, "smbsrv", "enable smb2", False)) break;
62 status = smbsrv_init_smb2_connection(smb_conn);
63 NT_STATUS_NOT_OK_RETURN(status);
64 packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
65 return smbsrv_recv_smb2_request(smb_conn, blob);
68 DEBUG(2,("Invalid SMB packet: protocl prefix: 0x%08X\n", protocol_version));
69 smbsrv_terminate_connection(smb_conn, "NON-SMB packet");
74 close the socket and shutdown a server_context
76 void smbsrv_terminate_connection(struct smbsrv_connection *smb_conn, const char *reason)
78 smb_conn->terminate = reason;
82 called when a SMB socket becomes readable
84 static void smbsrv_recv(struct stream_connection *conn, uint16_t flags)
86 struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, struct smbsrv_connection);
88 DEBUG(10,("smbsrv_recv\n"));
90 packet_recv(smb_conn->packet);
91 if (smb_conn->terminate) {
92 talloc_free(conn->event.fde);
93 conn->event.fde = NULL;
94 stream_terminate_connection(smb_conn->connection, smb_conn->terminate);
98 /* free up temporary memory */
103 called when a SMB socket becomes writable
105 static void smbsrv_send(struct stream_connection *conn, uint16_t flags)
107 struct smbsrv_connection *smb_conn = talloc_get_type(conn->private,
108 struct smbsrv_connection);
109 packet_queue_run(smb_conn->packet);
114 handle socket recv errors
116 static void smbsrv_recv_error(void *private, NTSTATUS status)
118 struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
120 smbsrv_terminate_connection(smb_conn, nt_errstr(status));
124 initialise a server_context from a open socket and register a event handler
125 for reading from that socket
127 static void smbsrv_accept(struct stream_connection *conn)
129 struct smbsrv_connection *smb_conn;
131 DEBUG(5,("smbsrv_accept\n"));
133 smb_conn = talloc_zero(conn, struct smbsrv_connection);
134 if (!smb_conn) return;
136 smb_conn->packet = packet_init(smb_conn);
137 if (smb_conn->packet == NULL) {
138 stream_terminate_connection(conn, "out of memory");
141 packet_set_private(smb_conn->packet, smb_conn);
142 packet_set_socket(smb_conn->packet, conn->socket);
143 packet_set_callback(smb_conn->packet, smbsrv_recv_generic_request);
144 packet_set_full_request(smb_conn->packet, packet_full_request_nbt);
145 packet_set_error_handler(smb_conn->packet, smbsrv_recv_error);
146 packet_set_event_context(smb_conn->packet, conn->event.ctx);
147 packet_set_fde(smb_conn->packet, conn->event.fde);
148 packet_set_serialise(smb_conn->packet);
150 smb_conn->connection = conn;
151 conn->private = smb_conn;
153 irpc_add_name(conn->msg_ctx, "smb_server");
155 smbsrv_management_init(smb_conn);
158 static const struct stream_server_ops smb_stream_ops = {
160 .accept_connection = smbsrv_accept,
161 .recv_handler = smbsrv_recv,
162 .send_handler = smbsrv_send,
166 setup a listening socket on all the SMB ports for a particular address
168 static NTSTATUS smb_add_socket(struct event_context *event_context,
169 const struct model_ops *model_ops,
172 const char **ports = lp_smb_ports();
176 for (i=0;ports[i];i++) {
177 uint16_t port = atoi(ports[i]);
178 if (port == 0) continue;
179 status = stream_setup_socket(event_context, model_ops, &smb_stream_ops,
180 "ipv4", address, &port, NULL);
181 NT_STATUS_NOT_OK_RETURN(status);
188 called on startup of the smb server service It's job is to start
189 listening on all configured SMB server sockets
191 static NTSTATUS smbsrv_init(struct event_context *event_context, const struct model_ops *model_ops)
195 if (lp_interfaces() && lp_bind_interfaces_only()) {
196 int num_interfaces = iface_count();
199 /* We have been given an interfaces line, and been
200 told to only bind to those interfaces. Create a
201 socket per interface and bind to only these.
203 for(i = 0; i < num_interfaces; i++) {
204 const char *address = iface_n_ip(i);
205 status = smb_add_socket(event_context, model_ops, address);
206 NT_STATUS_NOT_OK_RETURN(status);
209 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
210 status = smb_add_socket(event_context, model_ops, lp_socket_address());
211 NT_STATUS_NOT_OK_RETURN(status);
217 /* called at smbd startup - register ourselves as a server service */
218 NTSTATUS server_service_smb_init(void)
220 return register_server_service("smb", smbsrv_init);