140c81b309e2979b1ebf7cb6ccacc3cd2be2bd9b
[mat/samba.git] / source3 / smbd / smb2_create.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "printing.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "../libcli/smb/smb_common.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "../lib/util/tevent_ntstatus.h"
29 #include "messages.h"
30
31 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
32 {
33         switch(in_oplock_level) {
34         case SMB2_OPLOCK_LEVEL_NONE:
35                 return NO_OPLOCK;
36         case SMB2_OPLOCK_LEVEL_II:
37                 return LEVEL_II_OPLOCK;
38         case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
39                 return EXCLUSIVE_OPLOCK;
40         case SMB2_OPLOCK_LEVEL_BATCH:
41                 return BATCH_OPLOCK;
42         case SMB2_OPLOCK_LEVEL_LEASE:
43                 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
44                         "LEASE_OPLOCK_REQUESTED\n"));
45                 return NO_OPLOCK;
46         default:
47                 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
48                         "unknown level %u\n",
49                         (unsigned int)in_oplock_level));
50                 return NO_OPLOCK;
51         }
52 }
53
54 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
55 {
56         if (BATCH_OPLOCK_TYPE(oplock_type)) {
57                 return SMB2_OPLOCK_LEVEL_BATCH;
58         } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
59                 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
60         } else if (oplock_type == LEVEL_II_OPLOCK) {
61                 /*
62                  * Don't use LEVEL_II_OPLOCK_TYPE here as
63                  * this also includes FAKE_LEVEL_II_OPLOCKs
64                  * which are internal only.
65                  */
66                 return SMB2_OPLOCK_LEVEL_II;
67         } else {
68                 return SMB2_OPLOCK_LEVEL_NONE;
69         }
70 }
71
72 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
73                         struct tevent_context *ev,
74                         struct smbd_smb2_request *smb2req,
75                         uint8_t in_oplock_level,
76                         uint32_t in_impersonation_level,
77                         uint32_t in_desired_access,
78                         uint32_t in_file_attributes,
79                         uint32_t in_share_access,
80                         uint32_t in_create_disposition,
81                         uint32_t in_create_options,
82                         const char *in_name,
83                         struct smb2_create_blobs in_context_blobs);
84 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
85                         TALLOC_CTX *mem_ctx,
86                         uint8_t *out_oplock_level,
87                         uint32_t *out_create_action,
88                         NTTIME *out_creation_time,
89                         NTTIME *out_last_access_time,
90                         NTTIME *out_last_write_time,
91                         NTTIME *out_change_time,
92                         uint64_t *out_allocation_size,
93                         uint64_t *out_end_of_file,
94                         uint32_t *out_file_attributes,
95                         uint64_t *out_file_id_persistent,
96                         uint64_t *out_file_id_volatile,
97                         struct smb2_create_blobs *out_context_blobs);
98
99 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
100 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
101 {
102         const uint8_t *inbody;
103         const struct iovec *indyniov;
104         uint8_t in_oplock_level;
105         uint32_t in_impersonation_level;
106         uint32_t in_desired_access;
107         uint32_t in_file_attributes;
108         uint32_t in_share_access;
109         uint32_t in_create_disposition;
110         uint32_t in_create_options;
111         uint16_t in_name_offset;
112         uint16_t in_name_length;
113         DATA_BLOB in_name_buffer;
114         char *in_name_string;
115         size_t in_name_string_size;
116         uint32_t name_offset = 0;
117         uint32_t name_available_length = 0;
118         uint32_t in_context_offset;
119         uint32_t in_context_length;
120         DATA_BLOB in_context_buffer;
121         struct smb2_create_blobs in_context_blobs;
122         uint32_t context_offset = 0;
123         uint32_t context_available_length = 0;
124         uint32_t dyn_offset;
125         NTSTATUS status;
126         bool ok;
127         struct tevent_req *tsubreq;
128
129         status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
130         if (!NT_STATUS_IS_OK(status)) {
131                 return smbd_smb2_request_error(smb2req, status);
132         }
133         inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
134
135         in_oplock_level         = CVAL(inbody, 0x03);
136         in_impersonation_level  = IVAL(inbody, 0x04);
137         in_desired_access       = IVAL(inbody, 0x18);
138         in_file_attributes      = IVAL(inbody, 0x1C);
139         in_share_access         = IVAL(inbody, 0x20);
140         in_create_disposition   = IVAL(inbody, 0x24);
141         in_create_options       = IVAL(inbody, 0x28);
142         in_name_offset          = SVAL(inbody, 0x2C);
143         in_name_length          = SVAL(inbody, 0x2E);
144         in_context_offset       = IVAL(inbody, 0x30);
145         in_context_length       = IVAL(inbody, 0x34);
146
147         /*
148          * First check if the dynamic name and context buffers
149          * are correctly specified.
150          *
151          * Note: That we don't check if the name and context buffers
152          *       overlap
153          */
154
155         dyn_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req);
156
157         if (in_name_offset == 0 && in_name_length == 0) {
158                 /* This is ok */
159                 name_offset = 0;
160         } else if (in_name_offset < dyn_offset) {
161                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
162         } else {
163                 name_offset = in_name_offset - dyn_offset;
164         }
165
166         indyniov = SMBD_SMB2_IN_DYN_IOV(smb2req);
167
168         if (name_offset > indyniov->iov_len) {
169                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
170         }
171
172         name_available_length = indyniov->iov_len - name_offset;
173
174         if (in_name_length > name_available_length) {
175                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
176         }
177
178         in_name_buffer.data = (uint8_t *)indyniov->iov_base + name_offset;
179         in_name_buffer.length = in_name_length;
180
181         if (in_context_offset == 0 && in_context_length == 0) {
182                 /* This is ok */
183                 context_offset = 0;
184         } else if (in_context_offset < dyn_offset) {
185                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
186         } else {
187                 context_offset = in_context_offset - dyn_offset;
188         }
189
190         if (context_offset > indyniov->iov_len) {
191                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
192         }
193
194         context_available_length = indyniov->iov_len - context_offset;
195
196         if (in_context_length > context_available_length) {
197                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
198         }
199
200         in_context_buffer.data = (uint8_t *)indyniov->iov_base +
201                 context_offset;
202         in_context_buffer.length = in_context_length;
203
204         /*
205          * Now interpret the name and context buffers
206          */
207
208         ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
209                                    in_name_buffer.data,
210                                    in_name_buffer.length,
211                                    &in_name_string,
212                                    &in_name_string_size);
213         if (!ok) {
214                 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
215         }
216
217         if (in_name_buffer.length == 0) {
218                 in_name_string_size = 0;
219         }
220
221         if (strlen(in_name_string) != in_name_string_size) {
222                 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
223         }
224
225         ZERO_STRUCT(in_context_blobs);
226         status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
227         if (!NT_STATUS_IS_OK(status)) {
228                 return smbd_smb2_request_error(smb2req, status);
229         }
230
231         tsubreq = smbd_smb2_create_send(smb2req,
232                                        smb2req->sconn->ev_ctx,
233                                        smb2req,
234                                        in_oplock_level,
235                                        in_impersonation_level,
236                                        in_desired_access,
237                                        in_file_attributes,
238                                        in_share_access,
239                                        in_create_disposition,
240                                        in_create_options,
241                                        in_name_string,
242                                        in_context_blobs);
243         if (tsubreq == NULL) {
244                 smb2req->subreq = NULL;
245                 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
246         }
247         tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
248
249         /*
250          * For now we keep the logic that we do not send STATUS_PENDING
251          * for sharing violations, so we just wait 2 seconds.
252          *
253          * TODO: we need more tests for this.
254          */
255         return smbd_smb2_request_pending_queue(smb2req, tsubreq, 2000000);
256 }
257
258 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
259 {
260         uint8_t *reqhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
261         return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
262 }
263
264 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
265 {
266         struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
267                                         struct smbd_smb2_request);
268         DATA_BLOB outbody;
269         DATA_BLOB outdyn;
270         uint8_t out_oplock_level = 0;
271         uint32_t out_create_action = 0;
272         NTTIME out_creation_time = 0;
273         NTTIME out_last_access_time = 0;
274         NTTIME out_last_write_time = 0;
275         NTTIME out_change_time = 0;
276         uint64_t out_allocation_size = 0;
277         uint64_t out_end_of_file = 0;
278         uint32_t out_file_attributes = 0;
279         uint64_t out_file_id_persistent = 0;
280         uint64_t out_file_id_volatile = 0;
281         struct smb2_create_blobs out_context_blobs;
282         DATA_BLOB out_context_buffer;
283         uint16_t out_context_buffer_offset = 0;
284         NTSTATUS status;
285         NTSTATUS error; /* transport error */
286
287         status = smbd_smb2_create_recv(tsubreq,
288                                        smb2req,
289                                        &out_oplock_level,
290                                        &out_create_action,
291                                        &out_creation_time,
292                                        &out_last_access_time,
293                                        &out_last_write_time,
294                                        &out_change_time,
295                                        &out_allocation_size,
296                                        &out_end_of_file,
297                                        &out_file_attributes,
298                                        &out_file_id_persistent,
299                                        &out_file_id_volatile,
300                                        &out_context_blobs);
301         if (!NT_STATUS_IS_OK(status)) {
302                 error = smbd_smb2_request_error(smb2req, status);
303                 if (!NT_STATUS_IS_OK(error)) {
304                         smbd_server_connection_terminate(smb2req->sconn,
305                                                          nt_errstr(error));
306                         return;
307                 }
308                 return;
309         }
310
311         status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
312         if (!NT_STATUS_IS_OK(status)) {
313                 error = smbd_smb2_request_error(smb2req, status);
314                 if (!NT_STATUS_IS_OK(error)) {
315                         smbd_server_connection_terminate(smb2req->sconn,
316                                                          nt_errstr(error));
317                         return;
318                 }
319                 return;
320         }
321
322         if (out_context_buffer.length > 0) {
323                 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
324         }
325
326         outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
327         if (outbody.data == NULL) {
328                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
329                 if (!NT_STATUS_IS_OK(error)) {
330                         smbd_server_connection_terminate(smb2req->sconn,
331                                                          nt_errstr(error));
332                         return;
333                 }
334                 return;
335         }
336
337         SSVAL(outbody.data, 0x00, 0x58 + 1);    /* struct size */
338         SCVAL(outbody.data, 0x02,
339               out_oplock_level);                /* oplock level */
340         SCVAL(outbody.data, 0x03, 0);           /* reserved */
341         SIVAL(outbody.data, 0x04,
342               out_create_action);               /* create action */
343         SBVAL(outbody.data, 0x08,
344               out_creation_time);               /* creation time */
345         SBVAL(outbody.data, 0x10,
346               out_last_access_time);            /* last access time */
347         SBVAL(outbody.data, 0x18,
348               out_last_write_time);             /* last write time */
349         SBVAL(outbody.data, 0x20,
350               out_change_time);                 /* change time */
351         SBVAL(outbody.data, 0x28,
352               out_allocation_size);             /* allocation size */
353         SBVAL(outbody.data, 0x30,
354               out_end_of_file);                 /* end of file */
355         SIVAL(outbody.data, 0x38,
356               out_file_attributes);             /* file attributes */
357         SIVAL(outbody.data, 0x3C, 0);           /* reserved */
358         SBVAL(outbody.data, 0x40,
359               out_file_id_persistent);          /* file id (persistent) */
360         SBVAL(outbody.data, 0x48,
361               out_file_id_volatile);            /* file id (volatile) */
362         SIVAL(outbody.data, 0x50,
363               out_context_buffer_offset);       /* create contexts offset */
364         SIVAL(outbody.data, 0x54,
365               out_context_buffer.length);       /* create contexts length */
366
367         outdyn = out_context_buffer;
368
369         error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
370         if (!NT_STATUS_IS_OK(error)) {
371                 smbd_server_connection_terminate(smb2req->sconn,
372                                                  nt_errstr(error));
373                 return;
374         }
375 }
376
377 struct smbd_smb2_create_state {
378         struct smbd_smb2_request *smb2req;
379         struct smb_request *smb1req;
380         bool open_was_deferred;
381         struct tevent_timer *te;
382         struct tevent_immediate *im;
383         struct timeval request_time;
384         struct file_id id;
385         DATA_BLOB private_data;
386         uint8_t out_oplock_level;
387         uint32_t out_create_action;
388         NTTIME out_creation_time;
389         NTTIME out_last_access_time;
390         NTTIME out_last_write_time;
391         NTTIME out_change_time;
392         uint64_t out_allocation_size;
393         uint64_t out_end_of_file;
394         uint32_t out_file_attributes;
395         uint64_t out_file_id_persistent;
396         uint64_t out_file_id_volatile;
397         struct smb2_create_blobs out_context_blobs;
398 };
399
400 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
401                         struct tevent_context *ev,
402                         struct smbd_smb2_request *smb2req,
403                         uint8_t in_oplock_level,
404                         uint32_t in_impersonation_level,
405                         uint32_t in_desired_access,
406                         uint32_t in_file_attributes,
407                         uint32_t in_share_access,
408                         uint32_t in_create_disposition,
409                         uint32_t in_create_options,
410                         const char *in_name,
411                         struct smb2_create_blobs in_context_blobs)
412 {
413         struct tevent_req *req = NULL;
414         struct smbd_smb2_create_state *state = NULL;
415         NTSTATUS status;
416         struct smb_request *smb1req = NULL;
417         files_struct *result = NULL;
418         int info;
419         struct timespec write_time_ts;
420         struct smb2_create_blobs out_context_blobs;
421         int requested_oplock_level;
422         struct smb2_create_blob *dhnc = NULL;
423         struct smb2_create_blob *dh2c = NULL;
424         struct smb2_create_blob *dhnq = NULL;
425         struct smb2_create_blob *dh2q = NULL;
426         struct smbXsrv_open *op = NULL;
427
428         ZERO_STRUCT(out_context_blobs);
429
430         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
431                 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
432         } else {
433                 requested_oplock_level = in_oplock_level;
434         }
435
436
437         if (smb2req->subreq == NULL) {
438                 /* New create call. */
439                 req = tevent_req_create(mem_ctx, &state,
440                                 struct smbd_smb2_create_state);
441                 if (req == NULL) {
442                         return NULL;
443                 }
444                 state->smb2req = smb2req;
445
446                 smb1req = smbd_smb2_fake_smb_request(smb2req);
447                 if (tevent_req_nomem(smb1req, req)) {
448                         return tevent_req_post(req, ev);
449                 }
450                 state->smb1req = smb1req;
451                 smb2req->subreq = req;
452                 DEBUG(10,("smbd_smb2_create: name[%s]\n",
453                         in_name));
454         } else {
455                 /* Re-entrant create call. */
456                 req = smb2req->subreq;
457                 state = tevent_req_data(req,
458                                 struct smbd_smb2_create_state);
459                 smb1req = state->smb1req;
460                 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
461                         in_name ));
462         }
463
464         dhnq = smb2_create_blob_find(&in_context_blobs,
465                                      SMB2_CREATE_TAG_DHNQ);
466         dhnc = smb2_create_blob_find(&in_context_blobs,
467                                      SMB2_CREATE_TAG_DHNC);
468         dh2q = smb2_create_blob_find(&in_context_blobs,
469                                      SMB2_CREATE_TAG_DH2Q);
470         dh2c = smb2_create_blob_find(&in_context_blobs,
471                                      SMB2_CREATE_TAG_DH2C);
472
473         if ((dhnc && dh2c) || (dhnc && dh2q) || (dh2c && dhnq) ||
474             (dh2q && dh2c))
475         {
476                 /* not both are allowed at the same time */
477                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
478                 return tevent_req_post(req, ev);
479         }
480
481         if (dhnc) {
482                 uint32_t num_blobs_allowed;
483
484                 if (dhnc->data.length != 16) {
485                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
486                         return tevent_req_post(req, ev);
487                 }
488
489                 /*
490                  * According to MS-SMB2: 3.3.5.9.7, "Handling the
491                  * SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context",
492                  * we should ignore an additional dhnq blob, but fail
493                  * the request (with status OBJECT_NAME_NOT_FOUND) if
494                  * any other extra create blob has been provided.
495                  *
496                  * (Note that the cases of an additional dh2q or dh2c blob
497                  *  which require a different error code, have been treated
498                  *  above.)
499                  *
500                  * TODO:
501                  * This is only true for the oplock case:
502                  * For leases, lease request is required additionally.
503                  */
504
505                 if (dhnq) {
506                         num_blobs_allowed = 2;
507                 } else {
508                         num_blobs_allowed = 1;
509                 }
510
511                 if (in_context_blobs.num_blobs != num_blobs_allowed) {
512                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
513                         return tevent_req_post(req, ev);
514                 }
515         }
516
517         if (dh2c) {
518                 uint32_t num_blobs_allowed;
519
520                 if (dh2c->data.length != 36) {
521                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
522                         return tevent_req_post(req, ev);
523                 }
524
525                 /*
526                  * According to MS-SMB2: 3.3.5.9.12, "Handling the
527                  * SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 Create Context",
528                  * we should fail the request with status
529                  * OBJECT_NAME_NOT_FOUND if any other create blob has been
530                  * provided.
531                  *
532                  * (Note that the cases of an additional dhnq, dhnc or dh2q
533                  *  blob which require a different error code, have been
534                  *  treated above.)
535                  *
536                  * TODO:
537                  * This is only true for the oplock case:
538                  * For leases, lease request is required additionally!
539                  */
540
541                 num_blobs_allowed = 1;
542
543                 if (in_context_blobs.num_blobs != num_blobs_allowed) {
544                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
545                         return tevent_req_post(req, ev);
546                 }
547         }
548
549         if (IS_IPC(smb1req->conn)) {
550                 const char *pipe_name = in_name;
551
552                 if (dhnc || dh2c) {
553                         /* durable handles are not supported on IPC$ */
554                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
555                         return tevent_req_post(req, ev);
556                 }
557
558                 if (!lp_nt_pipe_support()) {
559                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
560                         return tevent_req_post(req, ev);
561                 }
562
563                 status = open_np_file(smb1req, pipe_name, &result);
564                 if (!NT_STATUS_IS_OK(status)) {
565                         tevent_req_nterror(req, status);
566                         return tevent_req_post(req, ev);
567                 }
568                 info = FILE_WAS_OPENED;
569         } else if (CAN_PRINT(smb1req->conn)) {
570                 if (dhnc || dh2c) {
571                         /* durable handles are not supported on printers */
572                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
573                         return tevent_req_post(req, ev);
574                 }
575
576                 status = file_new(smb1req, smb1req->conn, &result);
577                 if(!NT_STATUS_IS_OK(status)) {
578                         tevent_req_nterror(req, status);
579                         return tevent_req_post(req, ev);
580                 }
581
582                 status = print_spool_open(result, in_name,
583                                           smb1req->vuid);
584                 if (!NT_STATUS_IS_OK(status)) {
585                         file_free(smb1req, result);
586                         tevent_req_nterror(req, status);
587                         return tevent_req_post(req, ev);
588                 }
589                 info = FILE_WAS_CREATED;
590         } else {
591                 char *fname;
592                 struct smb2_create_blob *exta = NULL;
593                 struct ea_list *ea_list = NULL;
594                 struct smb2_create_blob *mxac = NULL;
595                 NTTIME max_access_time = 0;
596                 struct smb2_create_blob *secd = NULL;
597                 struct security_descriptor *sec_desc = NULL;
598                 struct smb2_create_blob *alsi = NULL;
599                 uint64_t allocation_size = 0;
600                 struct smb2_create_blob *twrp = NULL;
601                 struct smb2_create_blob *qfid = NULL;
602                 struct GUID _create_guid = GUID_zero();
603                 struct GUID *create_guid = NULL;
604                 bool update_open = false;
605                 bool durable_requested = false;
606                 uint32_t durable_timeout_msec = 0;
607                 bool do_durable_reconnect = false;
608                 uint64_t persistent_id = 0;
609
610                 exta = smb2_create_blob_find(&in_context_blobs,
611                                              SMB2_CREATE_TAG_EXTA);
612                 mxac = smb2_create_blob_find(&in_context_blobs,
613                                              SMB2_CREATE_TAG_MXAC);
614                 secd = smb2_create_blob_find(&in_context_blobs,
615                                              SMB2_CREATE_TAG_SECD);
616                 alsi = smb2_create_blob_find(&in_context_blobs,
617                                              SMB2_CREATE_TAG_ALSI);
618                 twrp = smb2_create_blob_find(&in_context_blobs,
619                                              SMB2_CREATE_TAG_TWRP);
620                 qfid = smb2_create_blob_find(&in_context_blobs,
621                                              SMB2_CREATE_TAG_QFID);
622
623                 fname = talloc_strdup(state, in_name);
624                 if (tevent_req_nomem(fname, req)) {
625                         return tevent_req_post(req, ev);
626                 }
627
628                 if (exta) {
629                         ea_list = read_nttrans_ea_list(mem_ctx,
630                                 (const char *)exta->data.data, exta->data.length);
631                         if (!ea_list) {
632                                 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
633                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
634                                 return tevent_req_post(req, ev);
635                         }
636
637                         if (ea_list_has_invalid_name(ea_list)) {
638                                 tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
639                                 return tevent_req_post(req, ev);
640                         }
641                 }
642
643                 if (mxac) {
644                         if (mxac->data.length == 0) {
645                                 max_access_time = 0;
646                         } else if (mxac->data.length == 8) {
647                                 max_access_time = BVAL(mxac->data.data, 0);
648                         } else {
649                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
650                                 return tevent_req_post(req, ev);
651                         }
652                 }
653
654                 if (secd) {
655                         enum ndr_err_code ndr_err;
656
657                         sec_desc = talloc_zero(state, struct security_descriptor);
658                         if (tevent_req_nomem(sec_desc, req)) {
659                                 return tevent_req_post(req, ev);
660                         }
661
662                         ndr_err = ndr_pull_struct_blob(&secd->data,
663                                 sec_desc, sec_desc,
664                                 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
665                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
666                                 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
667                                          ndr_errstr(ndr_err)));
668                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
669                                 return tevent_req_post(req, ev);
670                         }
671                 }
672
673                 if (dhnq) {
674                         if (dhnq->data.length != 16) {
675                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
676                                 return tevent_req_post(req, ev);
677                         }
678
679                         if (dh2q) {
680                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
681                                 return tevent_req_post(req, ev);
682                         }
683
684                         /*
685                          * durable handle request is processed below.
686                          */
687                         durable_requested = true;
688                         /*
689                          * Set the timeout to 16 mins.
690                          *
691                          * TODO: test this against Windows 2012
692                          *       as the default for durable v2 is 1 min.
693                          */
694                         durable_timeout_msec = (16*60*1000);
695                 }
696
697                 if (dh2q) {
698                         const uint8_t *p = dh2q->data.data;
699                         uint32_t durable_v2_timeout = 0;
700                         DATA_BLOB create_guid_blob;
701
702                         if (dh2q->data.length != 32) {
703                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
704                                 return tevent_req_post(req, ev);
705                         }
706
707                         if (dhnq) {
708                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
709                                 return tevent_req_post(req, ev);
710                         }
711
712                         durable_v2_timeout = IVAL(p, 0);
713                         create_guid_blob = data_blob_const(p + 16, 16);
714
715                         status = GUID_from_ndr_blob(&create_guid_blob,
716                                                     &_create_guid);
717                         if (tevent_req_nterror(req, status)) {
718                                 return tevent_req_post(req, ev);
719                         }
720                         create_guid = &_create_guid;
721                         /*
722                          * we need to store the create_guid later
723                          */
724                         update_open = true;
725
726                         /*
727                          * durable handle v2 request processed below
728                          */
729                         durable_requested = true;
730                         durable_timeout_msec = durable_v2_timeout;
731                         if (durable_timeout_msec == 0) {
732                                 /*
733                                  * Set the timeout to 1 min as default.
734                                  *
735                                  * This matches Windows 2012.
736                                  */
737                                 durable_timeout_msec = (60*1000);
738                         }
739                 }
740
741                 if (dhnc) {
742                         persistent_id = BVAL(dhnc->data.data, 0);
743
744                         do_durable_reconnect = true;
745                 }
746
747                 if (dh2c) {
748                         const uint8_t *p = dh2c->data.data;
749                         DATA_BLOB create_guid_blob;
750
751                         persistent_id = BVAL(p, 0);
752                         create_guid_blob = data_blob_const(p + 16, 16);
753
754                         status = GUID_from_ndr_blob(&create_guid_blob,
755                                                     &_create_guid);
756                         if (tevent_req_nterror(req, status)) {
757                                 return tevent_req_post(req, ev);
758                         }
759                         create_guid = &_create_guid;
760
761                         do_durable_reconnect = true;
762                 }
763
764                 if (alsi) {
765                         if (alsi->data.length != 8) {
766                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
767                                 return tevent_req_post(req, ev);
768                         }
769                         allocation_size = BVAL(alsi->data.data, 0);
770                 }
771
772                 if (twrp) {
773                         NTTIME nttime;
774                         time_t t;
775                         struct tm *tm;
776
777                         if (twrp->data.length != 8) {
778                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
779                                 return tevent_req_post(req, ev);
780                         }
781
782                         nttime = BVAL(twrp->data.data, 0);
783                         t = nt_time_to_unix(nttime);
784                         tm = gmtime(&t);
785
786                         TALLOC_FREE(fname);
787                         fname = talloc_asprintf(state,
788                                         "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
789                                         tm->tm_year + 1900,
790                                         tm->tm_mon + 1,
791                                         tm->tm_mday,
792                                         tm->tm_hour,
793                                         tm->tm_min,
794                                         tm->tm_sec,
795                                         in_name);
796                         if (tevent_req_nomem(fname, req)) {
797                                 return tevent_req_post(req, ev);
798                         }
799                 }
800
801                 if (qfid) {
802                         if (qfid->data.length != 0) {
803                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
804                                 return tevent_req_post(req, ev);
805                         }
806                 }
807
808                 /* these are ignored for SMB2 */
809                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
810                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
811
812                 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
813
814                 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
815
816                 /*
817                  * For the backend file open procedure, there are
818                  * two possible modes: durable_reconnect or not.
819                  */
820                 if (do_durable_reconnect) {
821                         DATA_BLOB new_cookie = data_blob_null;
822                         NTTIME now = timeval_to_nttime(&smb2req->request_time);
823
824                         status = smb2srv_open_recreate(smb2req->sconn->conn,
825                                                 smb1req->conn->session_info,
826                                                 persistent_id, create_guid,
827                                                 now, &op);
828                         if (!NT_STATUS_IS_OK(status)) {
829                                 DEBUG(3, ("smbd_smb2_create_send: "
830                                           "smb2srv_open_recreate failed: %s\n",
831                                           nt_errstr(status)));
832                                 tevent_req_nterror(req, status);
833                                 return tevent_req_post(req, ev);
834                         }
835
836                         DEBUG(10, ("smb2_create_send: %s to recreate the "
837                                    "smb2srv_open struct for a durable handle.\n",
838                                    op->global->durable ? "succeded" : "failed"));
839
840                         if (!op->global->durable) {
841                                 talloc_free(op);
842                                 tevent_req_nterror(req,
843                                         NT_STATUS_OBJECT_NAME_NOT_FOUND);
844                                 return tevent_req_post(req, ev);
845                         }
846
847                         status = SMB_VFS_DURABLE_RECONNECT(smb1req->conn,
848                                                 smb1req,
849                                                 op, /* smbXsrv_open input */
850                                                 op->global->backend_cookie,
851                                                 op, /* TALLOC_CTX */
852                                                 &result, &new_cookie);
853                         if (!NT_STATUS_IS_OK(status)) {
854                                 NTSTATUS return_status;
855
856                                 return_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
857
858                                 DEBUG(3, ("smbd_smb2_create_send: "
859                                           "durable_reconnect failed: %s => %s\n",
860                                           nt_errstr(status),
861                                           nt_errstr(return_status)));
862
863                                 tevent_req_nterror(req, return_status);
864                                 return tevent_req_post(req, ev);
865                         }
866
867                         data_blob_free(&op->global->backend_cookie);
868                         op->global->backend_cookie = new_cookie;
869
870                         op->status = NT_STATUS_OK;
871                         op->global->disconnect_time = 0;
872
873                         /* save the timout for later update */
874                         durable_timeout_msec = op->global->durable_timeout_msec;
875
876                         update_open = true;
877
878                         info = FILE_WAS_OPENED;
879                 } else {
880                         struct smb_filename *smb_fname = NULL;
881
882                         /*
883                          * For a DFS path the function parse_dfs_path()
884                          * will do the path processing.
885                          */
886
887                         if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
888                                 /* convert '\\' into '/' */
889                                 status = check_path_syntax(fname);
890                                 if (!NT_STATUS_IS_OK(status)) {
891                                         tevent_req_nterror(req, status);
892                                         return tevent_req_post(req, ev);
893                                 }
894                         }
895
896                         status = filename_convert(req,
897                                                   smb1req->conn,
898                                                   smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
899                                                   fname,
900                                                   (in_create_disposition == FILE_CREATE) ?
901                                                   UCF_CREATING_FILE : 0,
902                                                   NULL, /* ppath_contains_wcards */
903                                                   &smb_fname);
904                         if (!NT_STATUS_IS_OK(status)) {
905                                 tevent_req_nterror(req, status);
906                                 return tevent_req_post(req, ev);
907                         }
908
909                         status = SMB_VFS_CREATE_FILE(smb1req->conn,
910                                                      smb1req,
911                                                      0, /* root_dir_fid */
912                                                      smb_fname,
913                                                      in_desired_access,
914                                                      in_share_access,
915                                                      in_create_disposition,
916                                                      in_create_options,
917                                                      in_file_attributes,
918                                                      map_smb2_oplock_levels_to_samba(requested_oplock_level),
919                                                      allocation_size,
920                                                      0, /* private_flags */
921                                                      sec_desc,
922                                                      ea_list,
923                                                      &result,
924                                                      &info);
925                         if (!NT_STATUS_IS_OK(status)) {
926                                 if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
927                                         return req;
928                                 }
929                                 tevent_req_nterror(req, status);
930                                 return tevent_req_post(req, ev);
931                         }
932                         op = result->op;
933                 }
934
935                 /*
936                  * here we have op == result->op
937                  */
938
939                 DEBUG(10, ("smbd_smb2_create_send: "
940                            "response construction phase\n"));
941
942                 if (mxac) {
943                         NTTIME last_write_time;
944
945                         unix_timespec_to_nt_time(&last_write_time,
946                                                  result->fsp_name->st.st_ex_mtime);
947                         if (last_write_time != max_access_time) {
948                                 uint8_t p[8];
949                                 uint32_t max_access_granted;
950                                 DATA_BLOB blob = data_blob_const(p, sizeof(p));
951
952                                 status = smbd_calculate_access_mask(smb1req->conn,
953                                                         result->fsp_name,
954                                                         false,
955                                                         SEC_FLAG_MAXIMUM_ALLOWED,
956                                                         &max_access_granted);
957
958                                 SIVAL(p, 0, NT_STATUS_V(status));
959                                 SIVAL(p, 4, max_access_granted);
960
961                                 status = smb2_create_blob_add(state,
962                                                         &out_context_blobs,
963                                                         SMB2_CREATE_TAG_MXAC,
964                                                         blob);
965                                 if (!NT_STATUS_IS_OK(status)) {
966                                         tevent_req_nterror(req, status);
967                                         return tevent_req_post(req, ev);
968                                 }
969                         }
970                 }
971
972                 if (durable_requested &&
973                     BATCH_OPLOCK_TYPE(result->oplock_type))
974                 {
975                         status = SMB_VFS_DURABLE_COOKIE(result,
976                                                 op,
977                                                 &op->global->backend_cookie);
978                         if (!NT_STATUS_IS_OK(status)) {
979                                 op->global->backend_cookie = data_blob_null;
980                         }
981                 }
982                 if (op->global->backend_cookie.length > 0) {
983                         update_open = true;
984
985                         op->global->durable = true;
986                         op->global->durable_timeout_msec = durable_timeout_msec;
987                 }
988
989                 if (update_open) {
990                         op->global->create_guid = _create_guid;
991
992                         status = smbXsrv_open_update(op);
993                         DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
994                                    "returned %s\n",
995                                    nt_errstr(status)));
996                         if (!NT_STATUS_IS_OK(status)) {
997                                 tevent_req_nterror(req, status);
998                                 return tevent_req_post(req, ev);
999                         }
1000                 }
1001
1002                 if (dhnq && op->global->durable) {
1003                         uint8_t p[8] = { 0, };
1004                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
1005
1006                         status = smb2_create_blob_add(state,
1007                                                       &out_context_blobs,
1008                                                       SMB2_CREATE_TAG_DHNQ,
1009                                                       blob);
1010                         if (!NT_STATUS_IS_OK(status)) {
1011                                 tevent_req_nterror(req, status);
1012                                 return tevent_req_post(req, ev);
1013                         }
1014                 }
1015
1016                 if (dh2q && op->global->durable) {
1017                         uint8_t p[8] = { 0, };
1018                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
1019                         uint32_t durable_v2_response_flags = 0;
1020
1021                         SIVAL(p, 0, op->global->durable_timeout_msec);
1022                         SIVAL(p, 4, durable_v2_response_flags);
1023
1024                         status = smb2_create_blob_add(state, &out_context_blobs,
1025                                                       SMB2_CREATE_TAG_DH2Q,
1026                                                       blob);
1027                         if (!NT_STATUS_IS_OK(status)) {
1028                                 tevent_req_nterror(req, status);
1029                                 return tevent_req_post(req, ev);
1030                         }
1031                 }
1032
1033                 if (qfid) {
1034                         uint8_t p[32];
1035                         uint64_t file_index = get_FileIndex(result->conn,
1036                                                         &result->fsp_name->st);
1037                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
1038
1039                         ZERO_STRUCT(p);
1040
1041                         /* From conversations with Microsoft engineers at
1042                            the MS plugfest. The first 8 bytes are the "volume index"
1043                            == inode, the second 8 bytes are the "volume id",
1044                            == dev. This will be updated in the SMB2 doc. */
1045                         SBVAL(p, 0, file_index);
1046                         SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
1047
1048                         status = smb2_create_blob_add(state, &out_context_blobs,
1049                                                       SMB2_CREATE_TAG_QFID,
1050                                                       blob);
1051                         if (!NT_STATUS_IS_OK(status)) {
1052                                 tevent_req_nterror(req, status);
1053                                 return tevent_req_post(req, ev);
1054                         }
1055                 }
1056         }
1057
1058         smb2req->compat_chain_fsp = smb1req->chain_fsp;
1059
1060         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
1061                 state->out_oplock_level = in_oplock_level;
1062         } else {
1063                 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
1064         }
1065
1066         if ((in_create_disposition == FILE_SUPERSEDE)
1067             && (info == FILE_WAS_OVERWRITTEN)) {
1068                 state->out_create_action = FILE_WAS_SUPERSEDED;
1069         } else {
1070                 state->out_create_action = info;
1071         }
1072         state->out_file_attributes = dos_mode(result->conn,
1073                                            result->fsp_name);
1074         /* Deal with other possible opens having a modified
1075            write time. JRA. */
1076         ZERO_STRUCT(write_time_ts);
1077         get_file_infos(result->file_id, 0, NULL, &write_time_ts);
1078         if (!null_timespec(write_time_ts)) {
1079                 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
1080         }
1081
1082         unix_timespec_to_nt_time(&state->out_creation_time,
1083                         get_create_timespec(smb1req->conn, result,
1084                                         result->fsp_name));
1085         unix_timespec_to_nt_time(&state->out_last_access_time,
1086                         result->fsp_name->st.st_ex_atime);
1087         unix_timespec_to_nt_time(&state->out_last_write_time,
1088                         result->fsp_name->st.st_ex_mtime);
1089         unix_timespec_to_nt_time(&state->out_change_time,
1090                         get_change_timespec(smb1req->conn, result,
1091                                         result->fsp_name));
1092         state->out_allocation_size =
1093                         SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
1094                                                &(result->fsp_name->st));
1095         state->out_end_of_file = result->fsp_name->st.st_ex_size;
1096         if (state->out_file_attributes == 0) {
1097                 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
1098         }
1099         state->out_file_id_persistent = result->op->global->open_persistent_id;
1100         state->out_file_id_volatile = result->op->global->open_volatile_id;
1101         state->out_context_blobs = out_context_blobs;
1102
1103         DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1104                   fsp_str_dbg(result), fsp_fnum_dbg(result)));
1105
1106         tevent_req_done(req);
1107         return tevent_req_post(req, ev);
1108 }
1109
1110 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
1111                         TALLOC_CTX *mem_ctx,
1112                         uint8_t *out_oplock_level,
1113                         uint32_t *out_create_action,
1114                         NTTIME *out_creation_time,
1115                         NTTIME *out_last_access_time,
1116                         NTTIME *out_last_write_time,
1117                         NTTIME *out_change_time,
1118                         uint64_t *out_allocation_size,
1119                         uint64_t *out_end_of_file,
1120                         uint32_t *out_file_attributes,
1121                         uint64_t *out_file_id_persistent,
1122                         uint64_t *out_file_id_volatile,
1123                         struct smb2_create_blobs *out_context_blobs)
1124 {
1125         NTSTATUS status;
1126         struct smbd_smb2_create_state *state = tevent_req_data(req,
1127                                                struct smbd_smb2_create_state);
1128
1129         if (tevent_req_is_nterror(req, &status)) {
1130                 tevent_req_received(req);
1131                 return status;
1132         }
1133
1134         *out_oplock_level       = state->out_oplock_level;
1135         *out_create_action      = state->out_create_action;
1136         *out_creation_time      = state->out_creation_time;
1137         *out_last_access_time   = state->out_last_access_time;
1138         *out_last_write_time    = state->out_last_write_time;
1139         *out_change_time        = state->out_change_time;
1140         *out_allocation_size    = state->out_allocation_size;
1141         *out_end_of_file        = state->out_end_of_file;
1142         *out_file_attributes    = state->out_file_attributes;
1143         *out_file_id_persistent = state->out_file_id_persistent;
1144         *out_file_id_volatile   = state->out_file_id_volatile;
1145         *out_context_blobs      = state->out_context_blobs;
1146
1147         talloc_steal(mem_ctx, state->out_context_blobs.blobs);
1148
1149         tevent_req_received(req);
1150         return NT_STATUS_OK;
1151 }
1152
1153 /*********************************************************
1154  Code for dealing with deferred opens.
1155 *********************************************************/
1156
1157 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
1158                         struct timeval *p_request_time,
1159                         void **pp_state)
1160 {
1161         struct smbd_smb2_create_state *state = NULL;
1162         struct tevent_req *req = NULL;
1163
1164         if (!smb2req) {
1165                 return false;
1166         }
1167         req = smb2req->subreq;
1168         if (!req) {
1169                 return false;
1170         }
1171         state = tevent_req_data(req, struct smbd_smb2_create_state);
1172         if (!state) {
1173                 return false;
1174         }
1175         if (!state->open_was_deferred) {
1176                 return false;
1177         }
1178         if (p_request_time) {
1179                 *p_request_time = state->request_time;
1180         }
1181         if (pp_state) {
1182                 *pp_state = (void *)state->private_data.data;
1183         }
1184         return true;
1185 }
1186
1187 /*********************************************************
1188  Re-process this call early - requested by message or
1189  close.
1190 *********************************************************/
1191
1192 static struct smbd_smb2_request *find_open_smb2req(
1193         struct smbd_server_connection *sconn, uint64_t mid)
1194 {
1195         struct smbd_smb2_request *smb2req;
1196
1197         for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
1198                 uint64_t message_id;
1199                 if (smb2req->subreq == NULL) {
1200                         /* This message has been processed. */
1201                         continue;
1202                 }
1203                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1204                         /* This message has been processed. */
1205                         continue;
1206                 }
1207                 message_id = get_mid_from_smb2req(smb2req);
1208                 if (message_id == mid) {
1209                         return smb2req;
1210                 }
1211         }
1212         return NULL;
1213 }
1214
1215 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
1216 {
1217         struct smbd_smb2_create_state *state = NULL;
1218         struct smbd_smb2_request *smb2req;
1219
1220         smb2req = find_open_smb2req(sconn, mid);
1221
1222         if (!smb2req) {
1223                 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1224                         (unsigned long long)mid));
1225                 return false;
1226         }
1227         if (!smb2req->subreq) {
1228                 return false;
1229         }
1230         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1231                 return false;
1232         }
1233         state = tevent_req_data(smb2req->subreq,
1234                         struct smbd_smb2_create_state);
1235         if (!state) {
1236                 return false;
1237         }
1238         /* It's not in progress if there's no timeout event. */
1239         if (!state->open_was_deferred) {
1240                 return false;
1241         }
1242
1243         DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1244                         (unsigned long long)mid));
1245
1246         return true;
1247 }
1248
1249 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
1250                                                         uint64_t mid)
1251 {
1252         struct smbd_smb2_create_state *state = NULL;
1253
1254         if (!smb2req->subreq) {
1255                 return;
1256         }
1257         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1258                 return;
1259         }
1260         state = tevent_req_data(smb2req->subreq,
1261                         struct smbd_smb2_create_state);
1262         if (!state) {
1263                 return;
1264         }
1265
1266         DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1267                 "mid %llu\n",
1268                 (unsigned long long)mid ));
1269
1270         state->open_was_deferred = false;
1271         /* Ensure we don't have any outstanding timer event. */
1272         TALLOC_FREE(state->te);
1273         /* Ensure we don't have any outstanding immediate event. */
1274         TALLOC_FREE(state->im);
1275 }
1276
1277 void remove_deferred_open_message_smb2(
1278         struct smbd_server_connection *sconn, uint64_t mid)
1279 {
1280         struct smbd_smb2_request *smb2req;
1281
1282         smb2req = find_open_smb2req(sconn, mid);
1283
1284         if (!smb2req) {
1285                 DEBUG(10,("remove_deferred_open_message_smb2: "
1286                         "can't find mid %llu\n",
1287                         (unsigned long long)mid ));
1288                 return;
1289         }
1290         remove_deferred_open_message_smb2_internal(smb2req, mid);
1291 }
1292
1293 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1294                                         struct tevent_immediate *im,
1295                                         void *private_data)
1296 {
1297         struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1298                                         struct smbd_smb2_request);
1299         struct smbd_server_connection *sconn = smb2req->sconn;
1300         uint64_t mid = get_mid_from_smb2req(smb2req);
1301         NTSTATUS status;
1302
1303         DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1304                 "re-dispatching mid %llu\n",
1305                 (unsigned long long)mid ));
1306
1307         status = smbd_smb2_request_dispatch(smb2req);
1308         if (!NT_STATUS_IS_OK(status)) {
1309                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1310                 return;
1311         }
1312 }
1313
1314 bool schedule_deferred_open_message_smb2(
1315         struct smbd_server_connection *sconn, uint64_t mid)
1316 {
1317         struct smbd_smb2_create_state *state = NULL;
1318         struct smbd_smb2_request *smb2req;
1319
1320         smb2req = find_open_smb2req(sconn, mid);
1321
1322         if (!smb2req) {
1323                 DEBUG(10,("schedule_deferred_open_message_smb2: "
1324                         "can't find mid %llu\n",
1325                         (unsigned long long)mid ));
1326                 return false;
1327         }
1328         if (!smb2req->subreq) {
1329                 return false;
1330         }
1331         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1332                 return false;
1333         }
1334         state = tevent_req_data(smb2req->subreq,
1335                         struct smbd_smb2_create_state);
1336         if (!state) {
1337                 return false;
1338         }
1339
1340         /* Ensure we don't have any outstanding timer event. */
1341         TALLOC_FREE(state->te);
1342         /* Ensure we don't have any outstanding immediate event. */
1343         TALLOC_FREE(state->im);
1344
1345         /*
1346          * This is subtle. We must null out the callback
1347          * before rescheduling, else the first call to
1348          * tevent_req_nterror() causes the _receive()
1349          * function to be called, this causing tevent_req_post()
1350          * to crash.
1351          */
1352         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1353
1354         state->im = tevent_create_immediate(smb2req);
1355         if (!state->im) {
1356                 smbd_server_connection_terminate(smb2req->sconn,
1357                         nt_errstr(NT_STATUS_NO_MEMORY));
1358                 return false;
1359         }
1360
1361         DEBUG(10,("schedule_deferred_open_message_smb2: "
1362                 "re-processing mid %llu\n",
1363                 (unsigned long long)mid ));
1364
1365         tevent_schedule_immediate(state->im,
1366                         smb2req->sconn->ev_ctx,
1367                         smbd_smb2_create_request_dispatch_immediate,
1368                         smb2req);
1369
1370         return true;
1371 }
1372
1373 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1374 {
1375         struct smbd_smb2_request *smb2req = NULL;
1376         struct smbd_smb2_create_state *state = tevent_req_data(req,
1377                                 struct smbd_smb2_create_state);
1378         uint64_t mid;
1379
1380         if (!state) {
1381                 return false;
1382         }
1383
1384         if (!state->smb2req) {
1385                 return false;
1386         }
1387
1388         smb2req = state->smb2req;
1389         mid = get_mid_from_smb2req(smb2req);
1390
1391         if (is_deferred_open_async(state->private_data.data)) {
1392                 /* Can't cancel an async create. */
1393                 return false;
1394         }
1395
1396         remove_deferred_open_message_smb2_internal(smb2req, mid);
1397
1398         tevent_req_defer_callback(req, smb2req->sconn->ev_ctx);
1399         tevent_req_nterror(req, NT_STATUS_CANCELLED);
1400         return true;
1401 }
1402
1403 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1404                                 struct timeval request_time,
1405                                 struct timeval timeout,
1406                                 struct file_id id,
1407                                 char *private_data,
1408                                 size_t priv_len)
1409 {
1410         struct tevent_req *req = NULL;
1411         struct smbd_smb2_create_state *state = NULL;
1412         struct timeval end_time;
1413
1414         if (!smb2req) {
1415                 return false;
1416         }
1417         req = smb2req->subreq;
1418         if (!req) {
1419                 return false;
1420         }
1421         state = tevent_req_data(req, struct smbd_smb2_create_state);
1422         if (!state) {
1423                 return false;
1424         }
1425         state->id = id;
1426         state->request_time = request_time;
1427         state->private_data = data_blob_talloc(state, private_data,
1428                                                 priv_len);
1429         if (!state->private_data.data) {
1430                 return false;
1431         }
1432
1433         /* Re-schedule us to retry on timer expiry. */
1434         end_time = timeval_sum(&request_time, &timeout);
1435
1436         DEBUG(10,("push_deferred_open_message_smb2: "
1437                 "timeout at %s\n",
1438                 timeval_string(talloc_tos(),
1439                                 &end_time,
1440                                 true) ));
1441
1442         state->open_was_deferred = true;
1443
1444         /* allow this request to be canceled */
1445         tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1446
1447         return true;
1448 }