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