r26227: Make loadparm_context part of a server task, move loadparm_contexts further...
[jelmer/samba4-debian.git] / source / 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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/service_task.h"
23 #include "smbd/service_stream.h"
24 #include "smbd/service.h"
25 #include "smb_server/smb_server.h"
26 #include "smb_server/service_smb_proto.h"
27 #include "lib/messaging/irpc.h"
28 #include "lib/stream/packet.h"
29 #include "libcli/smb2/smb2.h"
30 #include "smb_server/smb2/smb2_server.h"
31 #include "system/network.h"
32 #include "lib/socket/netif.h"
33 #include "param/share.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "param/param.h"
36
37 static NTSTATUS smbsrv_recv_generic_request(void *private, DATA_BLOB blob)
38 {
39         NTSTATUS status;
40         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
41         uint32_t protocol_version;
42
43         /* see if its a special NBT packet */
44         if (CVAL(blob.data,0) != 0) {
45                 status = smbsrv_init_smb_connection(smb_conn);
46                 NT_STATUS_NOT_OK_RETURN(status);
47                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
48                 return smbsrv_recv_smb_request(smb_conn, blob);
49         }
50
51         if (blob.length < (NBT_HDR_SIZE + MIN_SMB_SIZE)) {
52                 DEBUG(2,("Invalid SMB packet length count %ld\n", (long)blob.length));
53                 smbsrv_terminate_connection(smb_conn, "Invalid SMB packet");
54                 return NT_STATUS_OK;
55         }
56
57         protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
58
59         switch (protocol_version) {
60         case SMB_MAGIC:
61                 status = smbsrv_init_smb_connection(smb_conn);
62                 NT_STATUS_NOT_OK_RETURN(status);
63                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
64                 return smbsrv_recv_smb_request(smb_conn, blob);
65         case SMB2_MAGIC:
66                 if (lp_srv_maxprotocol(global_loadparm) < PROTOCOL_SMB2) break;
67                 status = smbsrv_init_smb2_connection(smb_conn);
68                 NT_STATUS_NOT_OK_RETURN(status);
69                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
70                 return smbsrv_recv_smb2_request(smb_conn, blob);
71         }
72
73         DEBUG(2,("Invalid SMB packet: protocol prefix: 0x%08X\n", protocol_version));
74         smbsrv_terminate_connection(smb_conn, "NON-SMB packet");
75         return NT_STATUS_OK;
76 }
77
78 /*
79   close the socket and shutdown a server_context
80 */
81 void smbsrv_terminate_connection(struct smbsrv_connection *smb_conn, const char *reason)
82 {
83         stream_terminate_connection(smb_conn->connection, reason);
84 }
85
86 /*
87   called when a SMB socket becomes readable
88 */
89 static void smbsrv_recv(struct stream_connection *conn, uint16_t flags)
90 {
91         struct smbsrv_connection *smb_conn = talloc_get_type(conn->private,
92                                                              struct smbsrv_connection);
93
94         DEBUG(10,("smbsrv_recv\n"));
95
96         packet_recv(smb_conn->packet);
97
98         /* free up temporary memory */
99         lp_talloc_free();
100 }
101
102 /*
103   called when a SMB socket becomes writable
104 */
105 static void smbsrv_send(struct stream_connection *conn, uint16_t flags)
106 {
107         struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, 
108                                                              struct smbsrv_connection);
109         packet_queue_run(smb_conn->packet);
110 }
111
112 /*
113   handle socket recv errors
114 */
115 static void smbsrv_recv_error(void *private, NTSTATUS status)
116 {
117         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
118         
119         smbsrv_terminate_connection(smb_conn, nt_errstr(status));
120 }
121
122 /*
123   initialise a server_context from a open socket and register a event handler
124   for reading from that socket
125 */
126 static void smbsrv_accept(struct stream_connection *conn)
127 {
128         struct smbsrv_connection *smb_conn;
129
130         DEBUG(5,("smbsrv_accept\n"));
131
132         smb_conn = talloc_zero(conn, struct smbsrv_connection);
133         if (!smb_conn) {
134                 stream_terminate_connection(conn, "out of memory");
135                 return;
136         }
137
138         smb_conn->packet = packet_init(smb_conn);
139         if (!smb_conn->packet) {
140                 smbsrv_terminate_connection(smb_conn, "out of memory");
141                 return;
142         }
143         packet_set_private(smb_conn->packet, smb_conn);
144         packet_set_socket(smb_conn->packet, conn->socket);
145         packet_set_callback(smb_conn->packet, smbsrv_recv_generic_request);
146         packet_set_full_request(smb_conn->packet, packet_full_request_nbt);
147         packet_set_error_handler(smb_conn->packet, smbsrv_recv_error);
148         packet_set_event_context(smb_conn->packet, conn->event.ctx);
149         packet_set_fde(smb_conn->packet, conn->event.fde);
150         packet_set_serialise(smb_conn->packet);
151
152         smb_conn->connection = conn;
153         conn->private = smb_conn;
154
155         irpc_add_name(conn->msg_ctx, "smb_server");
156
157         smb_conn->statistics.connect_time = timeval_current();
158
159         smbsrv_management_init(smb_conn);
160
161         if (!NT_STATUS_IS_OK(share_get_context(smb_conn, &(smb_conn->share_context)))) {
162                 smbsrv_terminate_connection(smb_conn, "share_init failed!");
163                 return;
164         }
165 }
166
167 static const struct stream_server_ops smb_stream_ops = {
168         .name                   = "smbsrv",
169         .accept_connection      = smbsrv_accept,
170         .recv_handler           = smbsrv_recv,
171         .send_handler           = smbsrv_send,
172 };
173
174 /*
175   setup a listening socket on all the SMB ports for a particular address
176 */
177 _PUBLIC_ NTSTATUS smbsrv_add_socket(struct event_context *event_context,
178                                const struct model_ops *model_ops,
179                                const char *address)
180 {
181         const char **ports = lp_smb_ports(global_loadparm);
182         int i;
183         NTSTATUS status;
184
185         for (i=0;ports[i];i++) {
186                 uint16_t port = atoi(ports[i]);
187                 if (port == 0) continue;
188                 status = stream_setup_socket(event_context, model_ops, &smb_stream_ops, 
189                                              "ipv4", address, &port, NULL);
190                 NT_STATUS_NOT_OK_RETURN(status);
191         }
192
193         return NT_STATUS_OK;
194 }
195
196
197 /*
198   pre-open some of our ldb databases, to prevent an explosion of memory usage
199   when we fork
200  */
201 static void smbsrv_preopen_ldb(struct task_server *task)
202 {
203         /* yes, this looks strange. It is a hack to preload the
204            schema. I'd like to share most of the ldb context with the
205            child too. That will come later */
206         talloc_free(samdb_connect(task, global_loadparm, NULL));
207 }
208
209 /*
210   open the smb server sockets
211 */
212 static void smbsrv_task_init(struct task_server *task)
213 {       
214         NTSTATUS status;
215
216         task_server_set_title(task, "task[smbsrv]");
217
218         if (lp_interfaces(global_loadparm) && lp_bind_interfaces_only(global_loadparm)) {
219                 int num_interfaces = iface_count();
220                 int i;
221
222                 /* We have been given an interfaces line, and been 
223                    told to only bind to those interfaces. Create a
224                    socket per interface and bind to only these.
225                 */
226                 for(i = 0; i < num_interfaces; i++) {
227                         const char *address = iface_n_ip(i);
228                         status = smbsrv_add_socket(task->event_ctx, task->model_ops, address);
229                         if (!NT_STATUS_IS_OK(status)) goto failed;
230                 }
231         } else {
232                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
233                 status = smbsrv_add_socket(task->event_ctx, task->model_ops, 
234                                            lp_socket_address(global_loadparm));
235                 if (!NT_STATUS_IS_OK(status)) goto failed;
236         }
237
238         smbsrv_preopen_ldb(task);
239
240         return;
241 failed:
242         task_server_terminate(task, "Failed to startup smb server task");       
243 }
244
245 /*
246   called on startup of the smb server service It's job is to start
247   listening on all configured sockets
248 */
249 static NTSTATUS smbsrv_init(struct event_context *event_context, 
250                             const struct model_ops *model_ops)
251 {       
252         return task_server_startup(event_context, model_ops, smbsrv_task_init);
253 }
254
255 /* called at smbd startup - register ourselves as a server service */
256 NTSTATUS server_service_smb_init(void)
257 {
258         return register_server_service("smb", smbsrv_init);
259 }