r15304: Fix smbd build, more updates on getting --enable-dso to build again
[ira/wip.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_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 "netif/netif.h"
34
35 static NTSTATUS smbsrv_recv_generic_request(void *private, DATA_BLOB blob)
36 {
37         NTSTATUS status;
38         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
39         uint32_t protocol_version;
40
41         /* see if its a special NBT packet */
42         if (CVAL(blob.data,0) != 0) {
43                 status = smbsrv_init_smb_connection(smb_conn);
44                 NT_STATUS_NOT_OK_RETURN(status);
45                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
46                 return smbsrv_recv_smb_request(smb_conn, blob);
47         }
48
49         if (blob.length < (NBT_HDR_SIZE + MIN_SMB_SIZE)) {
50                 DEBUG(2,("Invalid SMB packet length count %ld\n", (long)blob.length));
51                 smbsrv_terminate_connection(smb_conn, "Invalid SMB packet");
52                 return NT_STATUS_OK;
53         }
54
55         protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
56
57         switch (protocol_version) {
58         case SMB_MAGIC:
59                 status = smbsrv_init_smb_connection(smb_conn);
60                 NT_STATUS_NOT_OK_RETURN(status);
61                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
62                 return smbsrv_recv_smb_request(smb_conn, blob);
63         case SMB2_MAGIC:
64                 if (lp_maxprotocol() < PROTOCOL_SMB2) break;
65                 status = smbsrv_init_smb2_connection(smb_conn);
66                 NT_STATUS_NOT_OK_RETURN(status);
67                 packet_set_callback(smb_conn->packet, smbsrv_recv_smb2_request);
68                 return smbsrv_recv_smb2_request(smb_conn, blob);
69         }
70
71         DEBUG(2,("Invalid SMB packet: protocol prefix: 0x%08X\n", protocol_version));
72         smbsrv_terminate_connection(smb_conn, "NON-SMB packet");
73         return NT_STATUS_OK;
74 }
75
76 /*
77   close the socket and shutdown a server_context
78 */
79 void smbsrv_terminate_connection(struct smbsrv_connection *smb_conn, const char *reason)
80 {
81         stream_terminate_connection(smb_conn->connection, reason);
82 }
83
84 /*
85   called when a SMB socket becomes readable
86 */
87 static void smbsrv_recv(struct stream_connection *conn, uint16_t flags)
88 {
89         struct smbsrv_connection *smb_conn = talloc_get_type(conn->private,
90                                                              struct smbsrv_connection);
91
92         DEBUG(10,("smbsrv_recv\n"));
93
94         packet_recv(smb_conn->packet);
95
96         /* free up temporary memory */
97         lp_talloc_free();
98 }
99
100 /*
101   called when a SMB socket becomes writable
102 */
103 static void smbsrv_send(struct stream_connection *conn, uint16_t flags)
104 {
105         struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, 
106                                                              struct smbsrv_connection);
107         packet_queue_run(smb_conn->packet);
108 }
109
110 /*
111   handle socket recv errors
112 */
113 static void smbsrv_recv_error(void *private, NTSTATUS status)
114 {
115         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
116         
117         smbsrv_terminate_connection(smb_conn, nt_errstr(status));
118 }
119
120 /*
121   initialise a server_context from a open socket and register a event handler
122   for reading from that socket
123 */
124 static void smbsrv_accept(struct stream_connection *conn)
125 {
126         struct smbsrv_connection *smb_conn;
127
128         DEBUG(5,("smbsrv_accept\n"));
129
130         smb_conn = talloc_zero(conn, struct smbsrv_connection);
131         if (!smb_conn) {
132                 stream_terminate_connection(conn, "out of memory");
133                 return;
134         }
135
136         smb_conn->packet = packet_init(smb_conn);
137         if (!smb_conn->packet) {
138                 smbsrv_terminate_connection(smb_conn, "out of memory");
139                 return;
140         }
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);
149
150         smb_conn->connection = conn;
151         conn->private = smb_conn;
152
153         irpc_add_name(conn->msg_ctx, "smb_server");
154
155         smb_conn->statistics.connect_time = timeval_current();
156
157         smbsrv_management_init(smb_conn);
158 }
159
160 static const struct stream_server_ops smb_stream_ops = {
161         .name                   = "smbsrv",
162         .accept_connection      = smbsrv_accept,
163         .recv_handler           = smbsrv_recv,
164         .send_handler           = smbsrv_send,
165 };
166
167 /*
168   setup a listening socket on all the SMB ports for a particular address
169 */
170 static NTSTATUS smb_add_socket(struct event_context *event_context,
171                                const struct model_ops *model_ops,
172                                const char *address)
173 {
174         const char **ports = lp_smb_ports();
175         int i;
176         NTSTATUS status;
177
178         for (i=0;ports[i];i++) {
179                 uint16_t port = atoi(ports[i]);
180                 if (port == 0) continue;
181                 status = stream_setup_socket(event_context, model_ops, &smb_stream_ops, 
182                                              "ipv4", address, &port, NULL);
183                 NT_STATUS_NOT_OK_RETURN(status);
184         }
185
186         return NT_STATUS_OK;
187 }
188
189 /*
190   open the smb server sockets
191 */
192 static void smbsrv_task_init(struct task_server *task)
193 {       
194         NTSTATUS status;
195
196         task_server_set_title(task, "task[smbsrv]");
197
198         if (lp_interfaces() && lp_bind_interfaces_only()) {
199                 int num_interfaces = iface_count();
200                 int i;
201
202                 /* We have been given an interfaces line, and been 
203                    told to only bind to those interfaces. Create a
204                    socket per interface and bind to only these.
205                 */
206                 for(i = 0; i < num_interfaces; i++) {
207                         const char *address = iface_n_ip(i);
208                         status = smb_add_socket(task->event_ctx, task->model_ops, address);
209                         if (!NT_STATUS_IS_OK(status)) goto failed;
210                 }
211         } else {
212                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
213                 status = smb_add_socket(task->event_ctx, task->model_ops, lp_socket_address());
214                 if (!NT_STATUS_IS_OK(status)) goto failed;
215         }
216
217         return;
218 failed:
219         task_server_terminate(task, "Failed to startup smb server task");       
220 }
221
222 /*
223   called on startup of the smb server service It's job is to start
224   listening on all configured sockets
225 */
226 static NTSTATUS smbsrv_init(struct event_context *event_context, 
227                             const struct model_ops *model_ops)
228 {       
229         return task_server_startup(event_context, model_ops, smbsrv_task_init);
230 }
231
232 /* called at smbd startup - register ourselves as a server service */
233 NTSTATUS server_service_smb_init(void)
234 {
235         return register_server_service("smb", smbsrv_init);
236 }