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