s4:smb_server/smb2: use helper variable smb2srv_chain_reply()
[metze/samba/wip.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 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 "system/time.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "smb_server/smb_server.h"
26 #include "smb_server/smb2/smb2_server.h"
27 #include "smbd/service_stream.h"
28 #include "lib/stream/packet.h"
29 #include "ntvfs/ntvfs.h"
30 #include "param/param.h"
31 #include "auth/auth.h"
32
33
34 /* fill in the bufinfo */
35 void smb2srv_setup_bufinfo(struct smb2srv_request *req)
36 {
37         req->in.bufinfo.mem_ctx    = req;
38         req->in.bufinfo.flags      = BUFINFO_FLAG_UNICODE | BUFINFO_FLAG_SMB2;
39         req->in.bufinfo.align_base = req->in.buffer;
40         if (req->in.dynamic) {
41                 req->in.bufinfo.data       = req->in.dynamic;
42                 req->in.bufinfo.data_size  = req->in.body_size - req->in.body_fixed;
43         } else {
44                 req->in.bufinfo.data       = NULL;
45                 req->in.bufinfo.data_size  = 0;
46         }
47 }
48
49 static int smb2srv_request_destructor(struct smb2srv_request *req)
50 {
51         DLIST_REMOVE(req->smb_conn->requests2.list, req);
52         if (req->pending_id) {
53                 idr_remove(req->smb_conn->requests2.idtree_req, req->pending_id);
54         }
55         return 0;
56 }
57
58 static int smb2srv_request_deny_destructor(struct smb2srv_request *req)
59 {
60         return -1;
61 }
62
63 struct smb2srv_request *smb2srv_init_request(struct smbsrv_connection *smb_conn)
64 {
65         struct smb2srv_request *req;
66
67         req = talloc_zero(smb_conn, struct smb2srv_request);
68         if (!req) return NULL;
69
70         req->smb_conn = smb_conn;
71
72         req->chained_session_id = UINT64_MAX;
73         req->chained_tree_id = UINT32_MAX;
74
75         talloc_set_destructor(req, smb2srv_request_destructor);
76
77         return req;
78 }
79
80 NTSTATUS smb2srv_setup_reply(struct smb2srv_request *req, uint16_t body_fixed_size,
81                              bool body_dynamic_present, uint32_t body_dynamic_size)
82 {
83         uint32_t flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
84         uint32_t pid = IVAL(req->in.hdr, SMB2_HDR_PID);
85         uint32_t tid = IVAL(req->in.hdr, SMB2_HDR_TID);
86         uint16_t credits = SVAL(req->in.hdr, SMB2_HDR_CREDIT);
87
88         if (credits == 0) {
89                 credits = 1;
90         }
91
92         flags |= SMB2_HDR_FLAG_REDIRECT;
93
94         if (req->pending_id) {
95                 flags |= SMB2_HDR_FLAG_ASYNC;
96                 pid = req->pending_id;
97                 tid = 0;
98                 credits = 0;
99         }
100
101         if (body_dynamic_present) {
102                 if (body_dynamic_size == 0) {
103                         body_dynamic_size = 1;
104                 }
105         } else {
106                 body_dynamic_size = 0;
107         }
108
109         req->out.size           = SMB2_HDR_BODY+NBT_HDR_SIZE+body_fixed_size;
110
111         req->out.allocated      = req->out.size + body_dynamic_size;
112         req->out.buffer         = talloc_array(req, uint8_t, 
113                                                req->out.allocated);
114         NT_STATUS_HAVE_NO_MEMORY(req->out.buffer);
115
116         req->out.hdr            = req->out.buffer       + NBT_HDR_SIZE;
117         req->out.body           = req->out.hdr          + SMB2_HDR_BODY;
118         req->out.body_fixed     = body_fixed_size;
119         req->out.body_size      = body_fixed_size;
120         req->out.dynamic        = (body_dynamic_size ? req->out.body + body_fixed_size : NULL);
121
122         SIVAL(req->out.hdr, 0,                          SMB2_MAGIC);
123         SSVAL(req->out.hdr, SMB2_HDR_LENGTH,            SMB2_HDR_BODY);
124         SSVAL(req->out.hdr, SMB2_HDR_CREDIT_CHARGE,
125               SVAL(req->in.hdr, SMB2_HDR_CREDIT_CHARGE));
126         SIVAL(req->out.hdr, SMB2_HDR_STATUS,            NT_STATUS_V(req->status));
127         SSVAL(req->out.hdr, SMB2_HDR_OPCODE,            SVAL(req->in.hdr, SMB2_HDR_OPCODE));
128         SSVAL(req->out.hdr, SMB2_HDR_CREDIT,            credits);
129         SIVAL(req->out.hdr, SMB2_HDR_FLAGS,             flags);
130         SIVAL(req->out.hdr, SMB2_HDR_NEXT_COMMAND,      0);
131         SBVAL(req->out.hdr, SMB2_HDR_MESSAGE_ID,        req->seqnum);
132         SIVAL(req->out.hdr, SMB2_HDR_PID,               pid);
133         SIVAL(req->out.hdr, SMB2_HDR_TID,               tid);
134         SBVAL(req->out.hdr, SMB2_HDR_SESSION_ID,        BVAL(req->in.hdr, SMB2_HDR_SESSION_ID));
135         memcpy(req->out.hdr+SMB2_HDR_SIGNATURE,
136                req->in.hdr+SMB2_HDR_SIGNATURE, 16);
137
138         /* set the length of the fixed body part and +1 if there's a dynamic part also */
139         SSVAL(req->out.body, 0, body_fixed_size + (body_dynamic_size?1:0));
140
141         /* 
142          * if we have a dynamic part, make sure the first byte
143          * which is always be part of the packet is initialized
144          */
145         if (body_dynamic_size) {
146                 req->out.size += 1;
147                 SCVAL(req->out.dynamic, 0, 0);
148         }
149
150         return NT_STATUS_OK;
151 }
152
153 static NTSTATUS smb2srv_reply(struct smb2srv_request *req);
154
155 static void smb2srv_chain_reply(struct smb2srv_request *p_req)
156 {
157         NTSTATUS status;
158         struct smbsrv_connection *smb_conn = p_req->smb_conn;
159         struct smb2srv_request *req;
160         uint32_t chain_offset;
161         uint32_t protocol_version;
162         uint16_t buffer_code;
163         uint32_t dynamic_size;
164         uint32_t flags;
165         uint32_t last_hdr_offset;
166
167         last_hdr_offset = p_req->in.hdr - p_req->in.buffer;
168
169         chain_offset = p_req->chain_offset;
170         p_req->chain_offset = 0;
171
172         if (p_req->in.size < (last_hdr_offset + chain_offset + SMB2_MIN_SIZE_NO_BODY)) {
173                 DEBUG(2,("Invalid SMB2 chained packet at offset 0x%X from last hdr 0x%X\n",
174                         chain_offset, last_hdr_offset));
175                 smbsrv_terminate_connection(smb_conn, "Invalid SMB2 chained packet");
176                 return;
177         }
178
179         protocol_version = IVAL(p_req->in.buffer, last_hdr_offset + chain_offset);
180         if (protocol_version != SMB2_MAGIC) {
181                 DEBUG(2,("Invalid SMB chained packet: protocol prefix: 0x%08X\n",
182                          protocol_version));
183                 smbsrv_terminate_connection(smb_conn, "NON-SMB2 chained packet");
184                 return;
185         }
186
187         req = smb2srv_init_request(smb_conn);
188         if (!req) {
189                 smbsrv_terminate_connection(smb_conn, "SMB2 chained packet - no memory");
190                 return;
191         }
192
193         req->in.buffer          = talloc_steal(req, p_req->in.buffer);
194         req->in.size            = p_req->in.size;
195         req->request_time       = p_req->request_time;
196         req->in.allocated       = req->in.size;
197
198         req->in.hdr             = req->in.buffer+ last_hdr_offset + chain_offset;
199         req->in.body            = req->in.hdr   + SMB2_HDR_BODY;
200         req->in.body_size       = req->in.size  - (last_hdr_offset+ chain_offset + SMB2_HDR_BODY);
201         req->in.dynamic         = NULL;
202
203         req->seqnum             = BVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID);
204
205         if (req->in.body_size < 2) {
206                 /* error handling for this is different for negprot to 
207                    other packet types */
208                 uint16_t opcode = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
209                 if (opcode == SMB2_OP_NEGPROT) {
210                         smbsrv_terminate_connection(smb_conn, "Bad body size in SMB2 negprot");
211                 } else {
212                         smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
213                 }
214         }
215
216         buffer_code             = SVAL(req->in.body, 0);
217         req->in.body_fixed      = (buffer_code & ~1);
218         dynamic_size            = req->in.body_size - req->in.body_fixed;
219
220         if (dynamic_size != 0 && (buffer_code & 1)) {
221                 req->in.dynamic = req->in.body + req->in.body_fixed;
222                 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
223                         DEBUG(1,("SMB2 chained request invalid dynamic size 0x%x\n", 
224                                  dynamic_size));
225                         smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
226                         return;
227                 }
228         }
229
230         smb2srv_setup_bufinfo(req);
231
232         flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
233         if (flags & SMB2_HDR_FLAG_CHAINED) {
234                 if (p_req->chained_file_handle) {
235                         memcpy(req->_chained_file_handle,
236                                p_req->_chained_file_handle,
237                                sizeof(req->_chained_file_handle));
238                         req->chained_file_handle = req->_chained_file_handle;
239                 }
240                 req->chained_session_id = p_req->chained_session_id;
241                 req->chained_tree_id = p_req->chained_tree_id;
242                 req->chain_status = p_req->chain_status;
243         }
244
245         /* 
246          * TODO: - make sure the length field is 64
247          *       - make sure it's a request
248          */
249
250         status = smb2srv_reply(req);
251         if (!NT_STATUS_IS_OK(status)) {
252                 smbsrv_terminate_connection(smb_conn, nt_errstr(status));
253                 talloc_free(req);
254                 return;
255         }
256 }
257
258 void smb2srv_send_reply(struct smb2srv_request *req)
259 {
260         DATA_BLOB blob;
261         NTSTATUS status;
262
263         if (req->smb_conn->connection->event.fde == NULL) {
264                 /* the socket has been destroyed - no point trying to send a reply! */
265                 talloc_free(req);
266                 return;
267         }
268
269         if (req->out.size > NBT_HDR_SIZE) {
270                 _smb_setlen_tcp(req->out.buffer, req->out.size - NBT_HDR_SIZE);
271         }
272
273         /* if signing is active on the session then sign the packet */
274         if (req->is_signed) {
275                 status = smb2_sign_message(&req->out, 
276                                            req->session->session_info->session_key);
277                 if (!NT_STATUS_IS_OK(status)) {
278                         smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
279                         return;
280                 }               
281         }
282
283
284         blob = data_blob_const(req->out.buffer, req->out.size);
285         status = packet_send(req->smb_conn->packet, blob);
286         if (!NT_STATUS_IS_OK(status)) {
287                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
288         }
289         if (req->chain_offset) {
290                 smb2srv_chain_reply(req);
291                 return;
292         }
293         talloc_free(req);
294 }
295
296 void smb2srv_send_error(struct smb2srv_request *req, NTSTATUS error)
297 {
298         NTSTATUS status;
299
300         if (req->smb_conn->connection->event.fde == NULL) {
301                 /* the socket has been destroyed - no point trying to send an error! */
302                 talloc_free(req);
303                 return;
304         }
305
306         status = smb2srv_setup_reply(req, 8, true, 0);
307         if (!NT_STATUS_IS_OK(status)) {
308                 smbsrv_terminate_connection(req->smb_conn, nt_errstr(status));
309                 talloc_free(req);
310                 return;
311         }
312
313         SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(error));
314
315         SSVAL(req->out.body, 0x02, 0);
316         SIVAL(req->out.body, 0x04, 0);
317
318         req->chain_status = NT_STATUS_INVALID_PARAMETER;
319
320         smb2srv_send_reply(req);
321 }
322
323 static NTSTATUS smb2srv_reply(struct smb2srv_request *req)
324 {
325         uint16_t opcode;
326         uint32_t tid;
327         uint64_t uid;
328         uint32_t flags;
329
330         if (SVAL(req->in.hdr, SMB2_HDR_LENGTH) != SMB2_HDR_BODY) {
331                 smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 header length");
332                 return NT_STATUS_INVALID_PARAMETER;
333         }
334         opcode                  = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
335         req->chain_offset       = IVAL(req->in.hdr, SMB2_HDR_NEXT_COMMAND);
336         req->seqnum             = BVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID);
337         tid                     = IVAL(req->in.hdr, SMB2_HDR_TID);
338         uid                     = BVAL(req->in.hdr, SMB2_HDR_SESSION_ID);
339         flags                   = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
340
341         if (opcode != SMB2_OP_CANCEL &&
342             req->smb_conn->highest_smb2_seqnum != 0 &&
343             req->seqnum <= req->smb_conn->highest_smb2_seqnum) {
344                 smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 sequence number");
345                 return NT_STATUS_INVALID_PARAMETER;
346         }
347         if (opcode != SMB2_OP_CANCEL) {
348                 req->smb_conn->highest_smb2_seqnum = req->seqnum;
349         }
350
351         if (flags & SMB2_HDR_FLAG_CHAINED) {
352                 uid = req->chained_session_id;
353                 tid = req->chained_tree_id;
354         }
355
356         req->session    = smbsrv_session_find(req->smb_conn, uid, req->request_time);
357         req->tcon       = smbsrv_smb2_tcon_find(req->session, tid, req->request_time);
358
359         req->chained_session_id = uid;
360         req->chained_tree_id = tid;
361
362         errno = 0;
363
364         /* supporting signing is mandatory in SMB2, and is per-packet. So we 
365            should check the signature on any incoming packet that is signed, and 
366            should give a signed reply to any signed request */
367         if (flags & SMB2_HDR_FLAG_SIGNED) {
368                 NTSTATUS status;
369
370                 if (!req->session) goto nosession;
371
372                 req->is_signed = true;
373                 status = smb2_check_signature(&req->in, 
374                                               req->session->session_info->session_key);
375                 if (!NT_STATUS_IS_OK(status)) {
376                         smb2srv_send_error(req, status);
377                         return NT_STATUS_OK;                    
378                 }
379         } else if (req->session && req->session->smb2_signing.active) {
380                 /* we require signing and this request was not signed */
381                 smb2srv_send_error(req, NT_STATUS_ACCESS_DENIED);
382                 return NT_STATUS_OK;                                    
383         }
384
385         if (!NT_STATUS_IS_OK(req->chain_status)) {
386                 smb2srv_send_error(req, req->chain_status);
387                 return NT_STATUS_OK;
388         }
389
390         switch (opcode) {
391         case SMB2_OP_NEGPROT:
392                 smb2srv_negprot_recv(req);
393                 return NT_STATUS_OK;
394         case SMB2_OP_SESSSETUP:
395                 smb2srv_sesssetup_recv(req);
396                 return NT_STATUS_OK;
397         case SMB2_OP_LOGOFF:
398                 if (!req->session) goto nosession;
399                 smb2srv_logoff_recv(req);
400                 return NT_STATUS_OK;
401         case SMB2_OP_TCON:
402                 if (!req->session) goto nosession;
403                 smb2srv_tcon_recv(req);
404                 return NT_STATUS_OK;
405         case SMB2_OP_TDIS:
406                 if (!req->session) goto nosession;
407                 if (!req->tcon) goto notcon;
408                 smb2srv_tdis_recv(req);
409                 return NT_STATUS_OK;
410         case SMB2_OP_CREATE:
411                 if (!req->session) goto nosession;
412                 if (!req->tcon) goto notcon;
413                 smb2srv_create_recv(req);
414                 return NT_STATUS_OK;
415         case SMB2_OP_CLOSE:
416                 if (!req->session) goto nosession;
417                 if (!req->tcon) goto notcon;
418                 smb2srv_close_recv(req);
419                 return NT_STATUS_OK;
420         case SMB2_OP_FLUSH:
421                 if (!req->session) goto nosession;
422                 if (!req->tcon) goto notcon;
423                 smb2srv_flush_recv(req);
424                 return NT_STATUS_OK;
425         case SMB2_OP_READ:
426                 if (!req->session) goto nosession;
427                 if (!req->tcon) goto notcon;
428                 smb2srv_read_recv(req);
429                 return NT_STATUS_OK;
430         case SMB2_OP_WRITE:
431                 if (!req->session) goto nosession;
432                 if (!req->tcon) goto notcon;
433                 smb2srv_write_recv(req);
434                 return NT_STATUS_OK;
435         case SMB2_OP_LOCK:
436                 if (!req->session) goto nosession;
437                 if (!req->tcon) goto notcon;
438                 smb2srv_lock_recv(req);
439                 return NT_STATUS_OK;
440         case SMB2_OP_IOCTL:
441                 if (!req->session) goto nosession;
442                 if (!req->tcon) goto notcon;
443                 smb2srv_ioctl_recv(req);
444                 return NT_STATUS_OK;
445         case SMB2_OP_CANCEL:
446                 smb2srv_cancel_recv(req);
447                 return NT_STATUS_OK;
448         case SMB2_OP_KEEPALIVE:
449                 smb2srv_keepalive_recv(req);
450                 return NT_STATUS_OK;
451         case SMB2_OP_FIND:
452                 if (!req->session) goto nosession;
453                 if (!req->tcon) goto notcon;
454                 smb2srv_find_recv(req);
455                 return NT_STATUS_OK;
456         case SMB2_OP_NOTIFY:
457                 if (!req->session) goto nosession;
458                 if (!req->tcon) goto notcon;
459                 smb2srv_notify_recv(req);
460                 return NT_STATUS_OK;
461         case SMB2_OP_GETINFO:
462                 if (!req->session) goto nosession;
463                 if (!req->tcon) goto notcon;
464                 smb2srv_getinfo_recv(req);
465                 return NT_STATUS_OK;
466         case SMB2_OP_SETINFO:
467                 if (!req->session) goto nosession;
468                 if (!req->tcon) goto notcon;
469                 smb2srv_setinfo_recv(req);
470                 return NT_STATUS_OK;
471         case SMB2_OP_BREAK:
472                 if (!req->session) goto nosession;
473                 if (!req->tcon) goto notcon;
474                 smb2srv_break_recv(req);
475                 return NT_STATUS_OK;
476         }
477
478         DEBUG(1,("Invalid SMB2 opcode: 0x%04X\n", opcode));
479         smbsrv_terminate_connection(req->smb_conn, "Invalid SMB2 opcode");
480         return NT_STATUS_OK;
481
482 nosession:
483         smb2srv_send_error(req, NT_STATUS_USER_SESSION_DELETED);
484         return NT_STATUS_OK;
485 notcon:
486         smb2srv_send_error(req, NT_STATUS_NETWORK_NAME_DELETED);
487         return NT_STATUS_OK;
488 }
489
490 NTSTATUS smbsrv_recv_smb2_request(void *private_data, DATA_BLOB blob)
491 {
492         struct smbsrv_connection *smb_conn = talloc_get_type(private_data, struct smbsrv_connection);
493         struct smb2srv_request *req;
494         struct timeval cur_time = timeval_current();
495         uint32_t protocol_version;
496         uint16_t buffer_code;
497         uint32_t dynamic_size;
498         uint32_t flags;
499
500         smb_conn->statistics.last_request_time = cur_time;
501
502         /* see if its a special NBT packet */
503         if (CVAL(blob.data,0) != 0) {
504                 DEBUG(2,("Special NBT packet on SMB2 connection"));
505                 smbsrv_terminate_connection(smb_conn, "Special NBT packet on SMB2 connection");
506                 return NT_STATUS_OK;
507         }
508
509         if (blob.length < (NBT_HDR_SIZE + SMB2_MIN_SIZE_NO_BODY)) {
510                 DEBUG(2,("Invalid SMB2 packet length count %ld\n", (long)blob.length));
511                 smbsrv_terminate_connection(smb_conn, "Invalid SMB2 packet");
512                 return NT_STATUS_OK;
513         }
514
515         protocol_version = IVAL(blob.data, NBT_HDR_SIZE);
516         if (protocol_version != SMB2_MAGIC) {
517                 DEBUG(2,("Invalid SMB packet: protocol prefix: 0x%08X\n",
518                          protocol_version));
519                 smbsrv_terminate_connection(smb_conn, "NON-SMB2 packet");
520                 return NT_STATUS_OK;
521         }
522
523         req = smb2srv_init_request(smb_conn);
524         NT_STATUS_HAVE_NO_MEMORY(req);
525
526         req->in.buffer          = talloc_steal(req, blob.data);
527         req->in.size            = blob.length;
528         req->request_time       = cur_time;
529         req->in.allocated       = req->in.size;
530
531         req->in.hdr             = req->in.buffer+ NBT_HDR_SIZE;
532         req->in.body            = req->in.hdr   + SMB2_HDR_BODY;
533         req->in.body_size       = req->in.size  - (SMB2_HDR_BODY+NBT_HDR_SIZE);
534         req->in.dynamic         = NULL;
535
536         req->seqnum             = BVAL(req->in.hdr, SMB2_HDR_MESSAGE_ID);
537
538         if (req->in.body_size < 2) {
539                 /* error handling for this is different for negprot to 
540                    other packet types */
541                 uint16_t opcode = SVAL(req->in.hdr, SMB2_HDR_OPCODE);
542                 if (opcode == SMB2_OP_NEGPROT) {
543                         smbsrv_terminate_connection(req->smb_conn, "Bad body size in SMB2 negprot");                    
544                         return NT_STATUS_OK;
545                 } else {
546                         smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
547                         return NT_STATUS_OK;
548                 }
549         }
550
551         buffer_code             = SVAL(req->in.body, 0);
552         req->in.body_fixed      = (buffer_code & ~1);
553         dynamic_size            = req->in.body_size - req->in.body_fixed;
554
555         if (dynamic_size != 0 && (buffer_code & 1)) {
556                 req->in.dynamic = req->in.body + req->in.body_fixed;
557                 if (smb2_oob(&req->in, req->in.dynamic, dynamic_size)) {
558                         DEBUG(1,("SMB2 request invalid dynamic size 0x%x\n", 
559                                  dynamic_size));
560                         smb2srv_send_error(req, NT_STATUS_INVALID_PARAMETER);
561                         return NT_STATUS_OK;
562                 }
563         }
564
565         smb2srv_setup_bufinfo(req);
566
567         /* 
568          * TODO: - make sure the length field is 64
569          *       - make sure it's a request
570          */
571
572         flags = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
573         /* the first request should never have the related flag set */
574         if (flags & SMB2_HDR_FLAG_CHAINED) {
575                 req->chain_status = NT_STATUS_INVALID_PARAMETER;
576         }
577
578         return smb2srv_reply(req);
579 }
580
581 static NTSTATUS smb2srv_init_pending(struct smbsrv_connection *smb_conn)
582 {
583         smb_conn->requests2.idtree_req = idr_init(smb_conn);
584         NT_STATUS_HAVE_NO_MEMORY(smb_conn->requests2.idtree_req);
585         smb_conn->requests2.idtree_limit        = 0x00FFFFFF & (UINT32_MAX - 1);
586         smb_conn->requests2.list                = NULL;
587
588         return NT_STATUS_OK;
589 }
590
591 NTSTATUS smb2srv_queue_pending(struct smb2srv_request *req)
592 {
593         NTSTATUS status;
594         bool signing_used = false;
595         int id;
596         uint16_t credits = SVAL(req->in.hdr, SMB2_HDR_CREDIT);
597
598         if (credits == 0) {
599                 credits = 1;
600         }
601
602         if (req->pending_id) {
603                 return NT_STATUS_INTERNAL_ERROR;
604         }
605
606         if (req->smb_conn->connection->event.fde == NULL) {
607                 /* the socket has been destroyed - no point trying to send an error! */
608                 return NT_STATUS_REMOTE_DISCONNECT;
609         }
610
611         id = idr_get_new_above(req->smb_conn->requests2.idtree_req, req, 
612                                1, req->smb_conn->requests2.idtree_limit);
613         if (id == -1) {
614                 return NT_STATUS_INSUFFICIENT_RESOURCES;
615         }
616
617         DLIST_ADD_END(req->smb_conn->requests2.list, req, struct smb2srv_request *);
618         req->pending_id = id;
619
620         talloc_set_destructor(req, smb2srv_request_deny_destructor);
621
622         status = smb2srv_setup_reply(req, 8, true, 0);
623         if (!NT_STATUS_IS_OK(status)) {
624                 return status;
625         }
626
627         SIVAL(req->out.hdr, SMB2_HDR_STATUS, NT_STATUS_V(STATUS_PENDING));
628         SSVAL(req->out.hdr, SMB2_HDR_CREDIT, credits);
629
630         SSVAL(req->out.body, 0x02, 0);
631         SIVAL(req->out.body, 0x04, 0);
632
633         /* if the real reply will be signed set the signed flags, but don't sign */
634         if (req->is_signed) {
635                 SIVAL(req->out.hdr, SMB2_HDR_FLAGS, IVAL(req->out.hdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_SIGNED);
636                 signing_used = req->is_signed;
637                 req->is_signed = false;
638         }
639
640         smb2srv_send_reply(req);
641
642         req->is_signed = signing_used;
643
644         talloc_set_destructor(req, smb2srv_request_destructor);
645         return NT_STATUS_OK;
646 }
647
648 void smb2srv_cancel_recv(struct smb2srv_request *req)
649 {
650         uint32_t pending_id;
651         uint32_t flags;
652         void *p;
653         struct smb2srv_request *r;
654
655         if (!req->session) goto done;
656
657         flags           = IVAL(req->in.hdr, SMB2_HDR_FLAGS);
658         pending_id      = IVAL(req->in.hdr, SMB2_HDR_PID);
659
660         if (!(flags & SMB2_HDR_FLAG_ASYNC)) {
661                 /* TODO: what to do here? */
662                 goto done;
663         }
664  
665         p = idr_find(req->smb_conn->requests2.idtree_req, pending_id);
666         if (!p) goto done;
667
668         r = talloc_get_type(p, struct smb2srv_request);
669         if (!r) goto done;
670
671         if (!r->ntvfs) goto done;
672
673         ntvfs_cancel(r->ntvfs);
674
675 done:
676         /* we never generate a reply for a SMB2 Cancel */
677         talloc_free(req);
678 }
679
680 /*
681  * init the SMB2 protocol related stuff
682  */
683 NTSTATUS smbsrv_init_smb2_connection(struct smbsrv_connection *smb_conn)
684 {
685         NTSTATUS status;
686
687         /* now initialise a few default values associated with this smb socket */
688         smb_conn->negotiate.max_send = 0xFFFF;
689
690         /* this is the size that w2k uses, and it appears to be important for
691            good performance */
692         smb_conn->negotiate.max_recv = lpcfg_max_xmit(smb_conn->lp_ctx);
693
694         smb_conn->negotiate.zone_offset = get_time_zone(time(NULL));
695
696         smb_conn->config.nt_status_support = true;
697
698         status = smbsrv_init_sessions(smb_conn, UINT64_MAX);
699         NT_STATUS_NOT_OK_RETURN(status);
700
701         status = smb2srv_init_pending(smb_conn);
702         NT_STATUS_NOT_OK_RETURN(status);
703
704         return NT_STATUS_OK;
705         
706 }