r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / smb_server / smb_server.c
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_stream.h"
24 #include "smb_server/smb_server.h"
25 #include "lib/messaging/irpc.h"
26 #include "lib/stream/packet.h"
27 #include "libcli/smb2/smb2.h"
28 #include "smb_server/smb2/smb2_server.h"
29
30 static NTSTATUS smbsrv_recv_generic_request(void *private, DATA_BLOB blob)
31 {
32         NTSTATUS status;
33         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
34         uint32_t protocol_version;
35
36         /* see if its a special NBT packet */
37         if (CVAL(blob.data,0) != 0) {
38                 status = smbsrv_init_smb_connection(smb_conn);
39                 NT_STATUS_NOT_OK_RETURN(status);
40                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
41                 return smbsrv_recv_smb_request(smb_conn, blob);
42         }
43
44         if (blob.length < (NBT_HDR_SIZE + MIN_SMB_SIZE)) {
45                 DEBUG(2,("Invalid SMB packet length count %ld\n", (long)blob.length));
46                 smbsrv_terminate_connection(smb_conn, "Invalid SMB packet");
47                 return NT_STATUS_OK;
48         }
49
50         protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
51
52         switch (protocol_version) {
53         case SMB_MAGIC:
54                 status = smbsrv_init_smb_connection(smb_conn);
55                 NT_STATUS_NOT_OK_RETURN(status);
56                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
57                 return smbsrv_recv_smb_request(smb_conn, blob);
58         case SMB2_MAGIC:
59                 if (!lp_parm_bool(-1, "smbsrv", "enable smb2", False)) break;
60                 status = smbsrv_init_smb2_connection(smb_conn);
61                 NT_STATUS_NOT_OK_RETURN(status);
62                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
63                 return smbsrv_recv_smb2_request(smb_conn, blob);
64         }
65
66         DEBUG(2,("Invalid SMB packet: protocl prefix: 0x%08X\n", protocol_version));
67         smbsrv_terminate_connection(smb_conn, "NON-SMB packet");
68         return NT_STATUS_OK;
69 }
70
71 /*
72   close the socket and shutdown a server_context
73 */
74 void smbsrv_terminate_connection(struct smbsrv_connection *smb_conn, const char *reason)
75 {
76         stream_terminate_connection(smb_conn->connection, reason);
77 }
78
79 /*
80   called when a SMB socket becomes readable
81 */
82 static void smbsrv_recv(struct stream_connection *conn, uint16_t flags)
83 {
84         struct smbsrv_connection *smb_conn = talloc_get_type(conn->private,
85                                                              struct smbsrv_connection);
86
87         DEBUG(10,("smbsrv_recv\n"));
88
89         packet_recv(smb_conn->packet);
90
91         /* free up temporary memory */
92         lp_talloc_free();
93 }
94
95 /*
96   called when a SMB socket becomes writable
97 */
98 static void smbsrv_send(struct stream_connection *conn, uint16_t flags)
99 {
100         struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, 
101                                                              struct smbsrv_connection);
102         packet_queue_run(smb_conn->packet);
103 }
104
105 /*
106   handle socket recv errors
107 */
108 static void smbsrv_recv_error(void *private, NTSTATUS status)
109 {
110         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
111         
112         smbsrv_terminate_connection(smb_conn, nt_errstr(status));
113 }
114
115 /*
116   initialise a server_context from a open socket and register a event handler
117   for reading from that socket
118 */
119 static void smbsrv_accept(struct stream_connection *conn)
120 {
121         struct smbsrv_connection *smb_conn;
122
123         DEBUG(5,("smbsrv_accept\n"));
124
125         smb_conn = talloc_zero(conn, struct smbsrv_connection);
126         if (!smb_conn) {
127                 stream_terminate_connection(conn, "out of memory");
128                 return;
129         }
130
131         smb_conn->packet = packet_init(smb_conn);
132         if (!smb_conn->packet) {
133                 smbsrv_terminate_connection(smb_conn, "out of memory");
134                 return;
135         }
136         packet_set_private(smb_conn->packet, smb_conn);
137         packet_set_socket(smb_conn->packet, conn->socket);
138         packet_set_callback(smb_conn->packet, smbsrv_recv_generic_request);
139         packet_set_full_request(smb_conn->packet, packet_full_request_nbt);
140         packet_set_error_handler(smb_conn->packet, smbsrv_recv_error);
141         packet_set_event_context(smb_conn->packet, conn->event.ctx);
142         packet_set_fde(smb_conn->packet, conn->event.fde);
143         packet_set_serialise(smb_conn->packet);
144
145         smb_conn->connection = conn;
146         conn->private = smb_conn;
147
148         irpc_add_name(conn->msg_ctx, "smb_server");
149
150         smbsrv_management_init(smb_conn);
151 }
152
153 static const struct stream_server_ops smb_stream_ops = {
154         .name                   = "smb",
155         .accept_connection      = smbsrv_accept,
156         .recv_handler           = smbsrv_recv,
157         .send_handler           = smbsrv_send,
158 };
159
160 /*
161   setup a listening socket on all the SMB ports for a particular address
162 */
163 static NTSTATUS smb_add_socket(struct event_context *event_context,
164                                const struct model_ops *model_ops,
165                                const char *address)
166 {
167         const char **ports = lp_smb_ports();
168         int i;
169         NTSTATUS status;
170
171         for (i=0;ports[i];i++) {
172                 uint16_t port = atoi(ports[i]);
173                 if (port == 0) continue;
174                 status = stream_setup_socket(event_context, model_ops, &smb_stream_ops, 
175                                              "ipv4", address, &port, NULL);
176                 NT_STATUS_NOT_OK_RETURN(status);
177         }
178
179         return NT_STATUS_OK;
180 }
181
182 /*
183   called on startup of the smb server service It's job is to start
184   listening on all configured SMB server sockets
185 */
186 static NTSTATUS smbsrv_init(struct event_context *event_context, const struct model_ops *model_ops)
187 {       
188         NTSTATUS status;
189
190         if (lp_interfaces() && lp_bind_interfaces_only()) {
191                 int num_interfaces = iface_count();
192                 int i;
193
194                 /* We have been given an interfaces line, and been 
195                    told to only bind to those interfaces. Create a
196                    socket per interface and bind to only these.
197                 */
198                 for(i = 0; i < num_interfaces; i++) {
199                         const char *address = iface_n_ip(i);
200                         status = smb_add_socket(event_context, model_ops, address);
201                         NT_STATUS_NOT_OK_RETURN(status);
202                 }
203         } else {
204                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
205                 status = smb_add_socket(event_context, model_ops, lp_socket_address());
206                 NT_STATUS_NOT_OK_RETURN(status);
207         }
208
209         return NT_STATUS_OK;
210 }
211
212 /* called at smbd startup - register ourselves as a server service */
213 NTSTATUS server_service_smb_init(void)
214 {
215         return register_server_service("smb", smbsrv_init);
216 }