r16705: fix a bug found by valgrind...
[kai/samba.git] / source4 / smb_server / smb2 / receive.c
1 /* 
2    Unix SMB2 implementation.
3    
4    Copyright (C) Andrew Tridgell        2005
5    Copyright (C) Stefan Metzmacher      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 "system/time.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "smb_server/smb_server.h"
27 #include "smb_server/service_smb_proto.h"
28 #include "smb_server/smb2/smb2_server.h"
29 #include "lib/stream/packet.h"
30
31
32 static struct smb2srv_request *smb2srv_init_request(struct smbsrv_connection *smb_conn)
33 {
34         struct smb2srv_request *req;
35
36         req = talloc_zero(smb_conn, struct smb2srv_request);
37         if (!req) return NULL;
38
39         req->smb_conn = smb_conn;
40
41         return req;
42 }
43
44 NTSTATUS smb2srv_setup_reply(struct smb2srv_request *req, uint16_t body_fixed_size,
45                              BOOL body_dynamic_present, uint32_t body_dynamic_size)
46 {
47         if (body_dynamic_present) {
48                 if (body_dynamic_size == 0) {
49                         body_dynamic_size = 1;
50                 }
51         } else {
52                 body_dynamic_size = 0;
53         }
54
55         req->out.size           = SMB2_HDR_BODY+NBT_HDR_SIZE+body_fixed_size;
56
57         req->out.allocated      = req->out.size + body_dynamic_size;
58         req->out.buffer         = talloc_size(req, req->out.allocated);
59         NT_STATUS_HAVE_NO_MEMORY(req->out.buffer);
60
61         req->out.hdr            = req->out.buffer       + NBT_HDR_SIZE;
62         req->out.body           = req->out.hdr          + SMB2_HDR_BODY;
63         req->out.body_fixed     = body_fixed_size;
64         req->out.body_size      = body_fixed_size;
65         req->out.dynamic        = (body_dynamic_size ? req->out.body + body_fixed_size : NULL);
66
67         SIVAL(req->out.hdr, 0,                SMB2_MAGIC);
68         SSVAL(req->out.hdr, SMB2_HDR_LENGTH,  SMB2_HDR_BODY);
69         SSVAL(req->out.hdr, SMB2_HDR_PAD1,    0);
70         SIVAL(req->out.hdr, SMB2_HDR_STATUS,  NT_STATUS_V(req->status));
71         SSVAL(req->out.hdr, SMB2_HDR_OPCODE,  SVAL(req->in.hdr, SMB2_HDR_OPCODE));
72         SSVAL(req->out.hdr, SMB2_HDR_PAD2,    0);
73         SIVAL(req->out.hdr, SMB2_HDR_FLAGS,   0x00000001);
74         SIVAL(req->out.hdr, SMB2_HDR_UNKNOWN, 0);
75         SBVAL(req->out.hdr, SMB2_HDR_SEQNUM,  req->seqnum);
76         SIVAL(req->out.hdr, SMB2_HDR_PID,     IVAL(req->in.hdr, SMB2_HDR_PID));
77         SIVAL(req->out.hdr, SMB2_HDR_TID,     IVAL(req->in.hdr, SMB2_HDR_TID));
78         SBVAL(req->out.hdr, SMB2_HDR_UID,     BVAL(req->in.hdr, SMB2_HDR_UID));
79         memset(req->out.hdr+SMB2_HDR_SIG, 0, 16);
80
81         /* set the length of the fixed body part and +1 if there's a dynamic part also */
82         SSVAL(req->out.body, 0, body_fixed_size + (body_dynamic_size?1:0));
83
84         /* 
85          * if we have a dynamic part, make sure the first byte
86          * which is always be part of the packet is initialized
87          */
88         if (body_dynamic_size) {
89                 req->out.size += 1;
90                 SCVAL(req->out.dynamic, 0, 0);
91         }
92
93         return NT_STATUS_OK;
94 }
95
96 void smb2srv_send_reply(struct smb2srv_request *req)
97 {
98         DATA_BLOB blob;
99         NTSTATUS status;
100
101         if (req->out.size > NBT_HDR_SIZE) {
102                 _smb2_setlen(req->out.buffer, req->out.size - NBT_HDR_SIZE);
103         }
104
105         blob = data_blob_const(req->out.buffer, req->out.size);
106         status = packet_send(req->smb_conn->packet, blob);
107         if (!NT_STATUS_IS_OK(status)) {
108                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
109         }
110         talloc_free(req);
111 }
112
113 void smb2srv_send_error(struct smb2srv_request *req, NTSTATUS error)
114 {
115         NTSTATUS status;
116
117         status = smb2srv_setup_reply(req, 8, True, 0);
118         if (!NT_STATUS_IS_OK(status)) {
119                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
120                 talloc_free(req);
121                 return;
122         }
123
124         SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(error));
125
126         SSVAL(req->out.body, 0x02, 0);
127         SIVAL(req->out.body, 0x04, 0);
128
129         smb2srv_send_reply(req);
130 }
131
132 static NTSTATUS smb2srv_reply(struct smb2srv_request *req)
133 {
134         uint16_t opcode;
135         uint32_t tid;
136         uint64_t uid;
137
138         opcode          = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
139         req->seqnum     = BVAL(req->in.hdr, SMB2_HDR_SEQNUM);
140         tid             = IVAL(req->in.hdr, SMB2_HDR_TID);
141         uid             = BVAL(req->in.hdr, SMB2_HDR_UID);
142
143         req->session    = smbsrv_session_find(req->smb_conn, uid, req->request_time);
144         req->tcon       = smbsrv_smb2_tcon_find(req->session, tid, req->request_time);
145
146         errno = 0;
147
148         /* TODO: check the seqnum */
149
150         switch (opcode) {
151         case SMB2_OP_NEGPROT:
152                 smb2srv_negprot_recv(req);
153                 return NT_STATUS_OK;
154         case SMB2_OP_SESSSETUP:
155                 smb2srv_sesssetup_recv(req);
156                 return NT_STATUS_OK;
157         case SMB2_OP_LOGOFF:
158                 if (!req->session) goto nosession;
159                 smb2srv_logoff_recv(req);
160                 return NT_STATUS_OK;
161         case SMB2_OP_TCON:
162                 if (!req->session) goto nosession;
163                 smb2srv_tcon_recv(req);
164                 return NT_STATUS_OK;
165         case SMB2_OP_TDIS:
166                 if (!req->session) goto nosession;
167                 if (!req->tcon) goto notcon;
168                 smb2srv_tdis_recv(req);
169                 return NT_STATUS_OK;
170         case SMB2_OP_CREATE:
171                 if (!req->session) goto nosession;
172                 if (!req->tcon) goto notcon;
173                 smb2srv_create_recv(req);
174                 return NT_STATUS_OK;
175         case SMB2_OP_CLOSE:
176                 if (!req->session) goto nosession;
177                 if (!req->tcon) goto notcon;
178                 smb2srv_close_recv(req);
179                 return NT_STATUS_OK;
180         case SMB2_OP_FLUSH:
181                 if (!req->session) goto nosession;
182                 if (!req->tcon) goto notcon;
183                 smb2srv_flush_recv(req);
184                 return NT_STATUS_OK;
185         case SMB2_OP_READ:
186                 if (!req->session) goto nosession;
187                 if (!req->tcon) goto notcon;
188                 smb2srv_read_recv(req);
189                 return NT_STATUS_OK;
190         case SMB2_OP_WRITE:
191                 if (!req->session) goto nosession;
192                 if (!req->tcon) goto notcon;
193                 smb2srv_write_recv(req);
194                 return NT_STATUS_OK;
195         case SMB2_OP_LOCK:
196                 if (!req->session) goto nosession;
197                 if (!req->tcon) goto notcon;
198                 smb2srv_lock_recv(req);
199                 return NT_STATUS_OK;
200         case SMB2_OP_IOCTL:
201                 if (!req->session) goto nosession;
202                 if (!req->tcon) goto notcon;
203                 smb2srv_ioctl_recv(req);
204                 return NT_STATUS_OK;
205         case SMB2_OP_CANCEL:
206                 if (!req->session) goto nosession;
207                 if (!req->tcon) goto notcon;
208                 smb2srv_cancel_recv(req);
209                 return NT_STATUS_OK;
210         case SMB2_OP_KEEPALIVE:
211                 smb2srv_keepalive_recv(req);
212                 return NT_STATUS_OK;
213         case SMB2_OP_FIND:
214                 if (!req->session) goto nosession;
215                 if (!req->tcon) goto notcon;
216                 smb2srv_find_recv(req);
217                 return NT_STATUS_OK;
218         case SMB2_OP_NOTIFY:
219                 if (!req->session) goto nosession;
220                 if (!req->tcon) goto notcon;
221                 smb2srv_notify_recv(req);
222                 return NT_STATUS_OK;
223         case SMB2_OP_GETINFO:
224                 if (!req->session) goto nosession;
225                 if (!req->tcon) goto notcon;
226                 smb2srv_getinfo_recv(req);
227                 return NT_STATUS_OK;
228         case SMB2_OP_SETINFO:
229                 if (!req->session) goto nosession;
230                 if (!req->tcon) goto notcon;
231                 smb2srv_setinfo_recv(req);
232                 return NT_STATUS_OK;
233         case SMB2_OP_BREAK:
234                 if (!req->session) goto nosession;
235                 if (!req->tcon) goto notcon;
236                 smb2srv_break_recv(req);
237                 return NT_STATUS_OK;
238         }
239
240         DEBUG(1,("Invalid SMB2 opcode: 0x%04X\n", opcode));
241         smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 opcode");
242         return NT_STATUS_OK;
243
244 nosession:
245         smb2srv_send_error(req, NT_STATUS_USER_SESSION_DELETED);
246         return NT_STATUS_OK;
247 notcon:
248         smb2srv_send_error(req, NT_STATUS_NETWORK_NAME_DELETED);
249         return NT_STATUS_OK;
250 }
251
252 NTSTATUS smbsrv_recv_smb2_request(void *private, DATA_BLOB blob)
253 {
254         struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
255         struct smb2srv_request *req;
256         struct timeval cur_time = timeval_current();
257         uint32_t protocol_version;
258         uint16_t buffer_code;
259         uint32_t dynamic_size;
260
261         smb_conn->statistics.last_request_time = cur_time;
262
263         /* see if its a special NBT packet */
264         if (CVAL(blob.data,0) != 0) {
265                 DEBUG(2,("Special NBT packet on SMB2 connection"));
266                 smbsrv_terminate_connection(smb_conn, "Special NBT packet on SMB2 connection");
267                 return NT_STATUS_OK;
268         }
269
270         if (blob.length < (NBT_HDR_SIZE + SMB2_MIN_SIZE)) {
271                 DEBUG(2,("Invalid SMB2 packet length count %ld\n", (long)blob.length));
272                 smbsrv_terminate_connection(smb_conn, "Invalid SMB2 packet");
273                 return NT_STATUS_OK;
274         }
275
276         protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
277
278         if (protocol_version != SMB2_MAGIC) {
279                 DEBUG(2,("Invalid SMB packet: protocl prefix: 0x%08X\n", protocol_version));
280                 smbsrv_terminate_connection(smb_conn, "NON-SMB2 packet");
281                 return NT_STATUS_OK;
282         }
283
284         req = smb2srv_init_request(smb_conn);
285         NT_STATUS_HAVE_NO_MEMORY(req);
286
287         req->in.buffer          = talloc_steal(req, blob.data);
288         req->in.size            = blob.length;
289         req->request_time       = cur_time;
290         req->in.allocated       = req->in.size;
291
292         req->in.hdr             = req->in.buffer+ NBT_HDR_SIZE;
293         req->in.body            = req->in.hdr   + SMB2_HDR_BODY;
294         req->in.body_size       = req->in.size  - (SMB2_HDR_BODY+NBT_HDR_SIZE);
295         req->in.dynamic         = NULL;
296
297         buffer_code             = SVAL(req->in.body, 0);
298         req->in.body_fixed      = (buffer_code & ~1);
299         dynamic_size            = req->in.body_size - req->in.body_fixed;
300
301         if (dynamic_size != 0 && (buffer_code & 1)) {
302                 req->in.dynamic = req->in.body + req->in.body_fixed;
303                 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
304                         DEBUG(1,("SMB2 request invalid dynamic size 0x%x\n", 
305                                  dynamic_size));
306                         smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
307                         return NT_STATUS_OK;
308                 }
309         }
310
311         /* 
312          * TODO: - make sure the length field is 64
313          *       - make sure it's a request
314          */
315
316         return smb2srv_reply(req);
317 }
318
319 /*
320  * init the SMB2 protocol related stuff
321  */
322 NTSTATUS smbsrv_init_smb2_connection(struct smbsrv_connection *smb_conn)
323 {
324         NTSTATUS status;
325
326         /* now initialise a few default values associated with this smb socket */
327         smb_conn->negotiate.max_send = 0xFFFF;
328
329         /* this is the size that w2k uses, and it appears to be important for
330            good performance */
331         smb_conn->negotiate.max_recv = lp_max_xmit();
332
333         smb_conn->negotiate.zone_offset = get_time_zone(time(NULL));
334
335         smb_conn->config.security = SEC_USER;
336         smb_conn->config.nt_status_support = True;
337
338         status = smbsrv_init_sessions(smb_conn, UINT64_MAX);
339         NT_STATUS_NOT_OK_RETURN(status);
340
341         return NT_STATUS_OK;
342         
343 }