smbd:smb2_create: fix return code for durable handle create blob combinations
[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                 if (dhnc->data.length != 16) {
483                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
484                         return tevent_req_post(req, ev);
485                 }
486                 if (in_context_blobs.num_blobs != 1) {
487                         /*
488                          * DHNC should be the only one.
489                          * TODO: This is only true for the oplock case!
490                          * For leases, lease request is required additionally!
491                          */
492                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
493                         return tevent_req_post(req, ev);
494                 }
495         }
496
497         if (dh2c) {
498                 if (dh2c->data.length != 36) {
499                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
500                         return tevent_req_post(req, ev);
501                 }
502                 if (in_context_blobs.num_blobs != 1) {
503                         /*
504                          * DH2C should be the only one.
505                          * TODO: This is only true for the oplock case!
506                          * For leases, lease request is required additionally!
507                          */
508                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
509                         return tevent_req_post(req, ev);
510                 }
511         }
512
513         if (IS_IPC(smb1req->conn)) {
514                 const char *pipe_name = in_name;
515
516                 if (dhnc || dh2c) {
517                         /* durable handles are not supported on IPC$ */
518                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
519                         return tevent_req_post(req, ev);
520                 }
521
522                 if (!lp_nt_pipe_support()) {
523                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
524                         return tevent_req_post(req, ev);
525                 }
526
527                 status = open_np_file(smb1req, pipe_name, &result);
528                 if (!NT_STATUS_IS_OK(status)) {
529                         tevent_req_nterror(req, status);
530                         return tevent_req_post(req, ev);
531                 }
532                 info = FILE_WAS_OPENED;
533         } else if (CAN_PRINT(smb1req->conn)) {
534                 if (dhnc || dh2c) {
535                         /* durable handles are not supported on printers */
536                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
537                         return tevent_req_post(req, ev);
538                 }
539
540                 status = file_new(smb1req, smb1req->conn, &result);
541                 if(!NT_STATUS_IS_OK(status)) {
542                         tevent_req_nterror(req, status);
543                         return tevent_req_post(req, ev);
544                 }
545
546                 status = print_spool_open(result, in_name,
547                                           smb1req->vuid);
548                 if (!NT_STATUS_IS_OK(status)) {
549                         file_free(smb1req, result);
550                         tevent_req_nterror(req, status);
551                         return tevent_req_post(req, ev);
552                 }
553                 info = FILE_WAS_CREATED;
554         } else {
555                 char *fname;
556                 struct smb2_create_blob *exta = NULL;
557                 struct ea_list *ea_list = NULL;
558                 struct smb2_create_blob *mxac = NULL;
559                 NTTIME max_access_time = 0;
560                 struct smb2_create_blob *secd = NULL;
561                 struct security_descriptor *sec_desc = NULL;
562                 struct smb2_create_blob *alsi = NULL;
563                 uint64_t allocation_size = 0;
564                 struct smb2_create_blob *twrp = NULL;
565                 struct smb2_create_blob *qfid = NULL;
566                 struct GUID _create_guid = GUID_zero();
567                 struct GUID *create_guid = NULL;
568                 bool update_open = false;
569                 bool durable_requested = false;
570                 uint32_t durable_timeout_msec = 0;
571                 bool do_durable_reconnect = false;
572                 uint64_t persistent_id = 0;
573
574                 exta = smb2_create_blob_find(&in_context_blobs,
575                                              SMB2_CREATE_TAG_EXTA);
576                 mxac = smb2_create_blob_find(&in_context_blobs,
577                                              SMB2_CREATE_TAG_MXAC);
578                 secd = smb2_create_blob_find(&in_context_blobs,
579                                              SMB2_CREATE_TAG_SECD);
580                 alsi = smb2_create_blob_find(&in_context_blobs,
581                                              SMB2_CREATE_TAG_ALSI);
582                 twrp = smb2_create_blob_find(&in_context_blobs,
583                                              SMB2_CREATE_TAG_TWRP);
584                 qfid = smb2_create_blob_find(&in_context_blobs,
585                                              SMB2_CREATE_TAG_QFID);
586
587                 fname = talloc_strdup(state, in_name);
588                 if (tevent_req_nomem(fname, req)) {
589                         return tevent_req_post(req, ev);
590                 }
591
592                 if (exta) {
593                         ea_list = read_nttrans_ea_list(mem_ctx,
594                                 (const char *)exta->data.data, exta->data.length);
595                         if (!ea_list) {
596                                 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
597                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
598                                 return tevent_req_post(req, ev);
599                         }
600
601                         if (ea_list_has_invalid_name(ea_list)) {
602                                 tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
603                                 return tevent_req_post(req, ev);
604                         }
605                 }
606
607                 if (mxac) {
608                         if (mxac->data.length == 0) {
609                                 max_access_time = 0;
610                         } else if (mxac->data.length == 8) {
611                                 max_access_time = BVAL(mxac->data.data, 0);
612                         } else {
613                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
614                                 return tevent_req_post(req, ev);
615                         }
616                 }
617
618                 if (secd) {
619                         enum ndr_err_code ndr_err;
620
621                         sec_desc = talloc_zero(state, struct security_descriptor);
622                         if (tevent_req_nomem(sec_desc, req)) {
623                                 return tevent_req_post(req, ev);
624                         }
625
626                         ndr_err = ndr_pull_struct_blob(&secd->data,
627                                 sec_desc, sec_desc,
628                                 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
629                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
630                                 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
631                                          ndr_errstr(ndr_err)));
632                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
633                                 return tevent_req_post(req, ev);
634                         }
635                 }
636
637                 if (dhnq) {
638                         if (dhnq->data.length != 16) {
639                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
640                                 return tevent_req_post(req, ev);
641                         }
642
643                         if (dh2q) {
644                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
645                                 return tevent_req_post(req, ev);
646                         }
647
648                         /*
649                          * durable handle request is processed below.
650                          */
651                         durable_requested = true;
652                         /*
653                          * Set the timeout to 16 mins.
654                          *
655                          * TODO: test this against Windows 2012
656                          *       as the default for durable v2 is 1 min.
657                          */
658                         durable_timeout_msec = (16*60*1000);
659                 }
660
661                 if (dh2q) {
662                         const uint8_t *p = dh2q->data.data;
663                         uint32_t durable_v2_timeout = 0;
664                         DATA_BLOB create_guid_blob;
665
666                         if (dh2q->data.length != 32) {
667                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
668                                 return tevent_req_post(req, ev);
669                         }
670
671                         if (dhnq) {
672                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
673                                 return tevent_req_post(req, ev);
674                         }
675
676                         durable_v2_timeout = IVAL(p, 0);
677                         create_guid_blob = data_blob_const(p + 16, 16);
678
679                         status = GUID_from_ndr_blob(&create_guid_blob,
680                                                     &_create_guid);
681                         if (tevent_req_nterror(req, status)) {
682                                 return tevent_req_post(req, ev);
683                         }
684                         create_guid = &_create_guid;
685                         /*
686                          * we need to store the create_guid later
687                          */
688                         update_open = true;
689
690                         /*
691                          * durable handle v2 request processed below
692                          */
693                         durable_requested = true;
694                         durable_timeout_msec = durable_v2_timeout;
695                         if (durable_timeout_msec == 0) {
696                                 /*
697                                  * Set the timeout to 1 min as default.
698                                  *
699                                  * This matches Windows 2012.
700                                  */
701                                 durable_timeout_msec = (60*1000);
702                         }
703                 }
704
705                 if (dhnc) {
706                         persistent_id = BVAL(dhnc->data.data, 0);
707
708                         do_durable_reconnect = true;
709                 }
710
711                 if (dh2c) {
712                         const uint8_t *p = dh2c->data.data;
713                         DATA_BLOB create_guid_blob;
714
715                         persistent_id = BVAL(p, 0);
716                         create_guid_blob = data_blob_const(p + 16, 16);
717
718                         status = GUID_from_ndr_blob(&create_guid_blob,
719                                                     &_create_guid);
720                         if (tevent_req_nterror(req, status)) {
721                                 return tevent_req_post(req, ev);
722                         }
723                         create_guid = &_create_guid;
724
725                         do_durable_reconnect = true;
726                 }
727
728                 if (alsi) {
729                         if (alsi->data.length != 8) {
730                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
731                                 return tevent_req_post(req, ev);
732                         }
733                         allocation_size = BVAL(alsi->data.data, 0);
734                 }
735
736                 if (twrp) {
737                         NTTIME nttime;
738                         time_t t;
739                         struct tm *tm;
740
741                         if (twrp->data.length != 8) {
742                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
743                                 return tevent_req_post(req, ev);
744                         }
745
746                         nttime = BVAL(twrp->data.data, 0);
747                         t = nt_time_to_unix(nttime);
748                         tm = gmtime(&t);
749
750                         TALLOC_FREE(fname);
751                         fname = talloc_asprintf(state,
752                                         "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
753                                         tm->tm_year + 1900,
754                                         tm->tm_mon + 1,
755                                         tm->tm_mday,
756                                         tm->tm_hour,
757                                         tm->tm_min,
758                                         tm->tm_sec,
759                                         in_name);
760                         if (tevent_req_nomem(fname, req)) {
761                                 return tevent_req_post(req, ev);
762                         }
763                 }
764
765                 if (qfid) {
766                         if (qfid->data.length != 0) {
767                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
768                                 return tevent_req_post(req, ev);
769                         }
770                 }
771
772                 /* these are ignored for SMB2 */
773                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
774                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
775
776                 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
777
778                 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
779
780                 /*
781                  * For the backend file open procedure, there are
782                  * two possible modes: durable_reconnect or not.
783                  */
784                 if (do_durable_reconnect) {
785                         DATA_BLOB new_cookie = data_blob_null;
786                         NTTIME now = timeval_to_nttime(&smb2req->request_time);
787
788                         status = smb2srv_open_recreate(smb2req->sconn->conn,
789                                                 smb1req->conn->session_info,
790                                                 persistent_id, create_guid,
791                                                 now, &op);
792                         if (!NT_STATUS_IS_OK(status)) {
793                                 DEBUG(3, ("smbd_smb2_create_send: "
794                                           "smb2srv_open_recreate failed: %s\n",
795                                           nt_errstr(status)));
796                                 tevent_req_nterror(req, status);
797                                 return tevent_req_post(req, ev);
798                         }
799
800                         DEBUG(10, ("smb2_create_send: %s to recreate the "
801                                    "smb2srv_open struct for a durable handle.\n",
802                                    op->global->durable ? "succeded" : "failed"));
803
804                         if (!op->global->durable) {
805                                 talloc_free(op);
806                                 tevent_req_nterror(req,
807                                         NT_STATUS_OBJECT_NAME_NOT_FOUND);
808                                 return tevent_req_post(req, ev);
809                         }
810
811                         status = SMB_VFS_DURABLE_RECONNECT(smb1req->conn,
812                                                 smb1req,
813                                                 op, /* smbXsrv_open input */
814                                                 op->global->backend_cookie,
815                                                 op, /* TALLOC_CTX */
816                                                 &result, &new_cookie);
817                         if (!NT_STATUS_IS_OK(status)) {
818                                 NTSTATUS return_status;
819
820                                 return_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
821
822                                 DEBUG(3, ("smbd_smb2_create_send: "
823                                           "durable_reconnect failed: %s => %s\n",
824                                           nt_errstr(status),
825                                           nt_errstr(return_status)));
826
827                                 tevent_req_nterror(req, return_status);
828                                 return tevent_req_post(req, ev);
829                         }
830
831                         data_blob_free(&op->global->backend_cookie);
832                         op->global->backend_cookie = new_cookie;
833
834                         op->status = NT_STATUS_OK;
835                         op->global->disconnect_time = 0;
836
837                         /* save the timout for later update */
838                         durable_timeout_msec = op->global->durable_timeout_msec;
839
840                         update_open = true;
841
842                         info = FILE_WAS_OPENED;
843                 } else {
844                         struct smb_filename *smb_fname = NULL;
845
846                         /*
847                          * For a DFS path the function parse_dfs_path()
848                          * will do the path processing.
849                          */
850
851                         if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
852                                 /* convert '\\' into '/' */
853                                 status = check_path_syntax(fname);
854                                 if (!NT_STATUS_IS_OK(status)) {
855                                         tevent_req_nterror(req, status);
856                                         return tevent_req_post(req, ev);
857                                 }
858                         }
859
860                         status = filename_convert(req,
861                                                   smb1req->conn,
862                                                   smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
863                                                   fname,
864                                                   (in_create_disposition == FILE_CREATE) ?
865                                                   UCF_CREATING_FILE : 0,
866                                                   NULL, /* ppath_contains_wcards */
867                                                   &smb_fname);
868                         if (!NT_STATUS_IS_OK(status)) {
869                                 tevent_req_nterror(req, status);
870                                 return tevent_req_post(req, ev);
871                         }
872
873                         status = SMB_VFS_CREATE_FILE(smb1req->conn,
874                                                      smb1req,
875                                                      0, /* root_dir_fid */
876                                                      smb_fname,
877                                                      in_desired_access,
878                                                      in_share_access,
879                                                      in_create_disposition,
880                                                      in_create_options,
881                                                      in_file_attributes,
882                                                      map_smb2_oplock_levels_to_samba(requested_oplock_level),
883                                                      allocation_size,
884                                                      0, /* private_flags */
885                                                      sec_desc,
886                                                      ea_list,
887                                                      &result,
888                                                      &info);
889                         if (!NT_STATUS_IS_OK(status)) {
890                                 if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
891                                         return req;
892                                 }
893                                 tevent_req_nterror(req, status);
894                                 return tevent_req_post(req, ev);
895                         }
896                         op = result->op;
897                 }
898
899                 /*
900                  * here we have op == result->op
901                  */
902
903                 DEBUG(10, ("smbd_smb2_create_send: "
904                            "response construction phase\n"));
905
906                 if (mxac) {
907                         NTTIME last_write_time;
908
909                         unix_timespec_to_nt_time(&last_write_time,
910                                                  result->fsp_name->st.st_ex_mtime);
911                         if (last_write_time != max_access_time) {
912                                 uint8_t p[8];
913                                 uint32_t max_access_granted;
914                                 DATA_BLOB blob = data_blob_const(p, sizeof(p));
915
916                                 status = smbd_calculate_access_mask(smb1req->conn,
917                                                         result->fsp_name,
918                                                         false,
919                                                         SEC_FLAG_MAXIMUM_ALLOWED,
920                                                         &max_access_granted);
921
922                                 SIVAL(p, 0, NT_STATUS_V(status));
923                                 SIVAL(p, 4, max_access_granted);
924
925                                 status = smb2_create_blob_add(state,
926                                                         &out_context_blobs,
927                                                         SMB2_CREATE_TAG_MXAC,
928                                                         blob);
929                                 if (!NT_STATUS_IS_OK(status)) {
930                                         tevent_req_nterror(req, status);
931                                         return tevent_req_post(req, ev);
932                                 }
933                         }
934                 }
935
936                 if (durable_requested &&
937                     BATCH_OPLOCK_TYPE(result->oplock_type))
938                 {
939                         status = SMB_VFS_DURABLE_COOKIE(result,
940                                                 op,
941                                                 &op->global->backend_cookie);
942                         if (!NT_STATUS_IS_OK(status)) {
943                                 op->global->backend_cookie = data_blob_null;
944                         }
945                 }
946                 if (op->global->backend_cookie.length > 0) {
947                         update_open = true;
948
949                         op->global->durable = true;
950                         op->global->durable_timeout_msec = durable_timeout_msec;
951                 }
952
953                 if (update_open) {
954                         op->global->create_guid = _create_guid;
955
956                         status = smbXsrv_open_update(op);
957                         DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
958                                    "returned %s\n",
959                                    nt_errstr(status)));
960                         if (!NT_STATUS_IS_OK(status)) {
961                                 tevent_req_nterror(req, status);
962                                 return tevent_req_post(req, ev);
963                         }
964                 }
965
966                 if (dhnq && op->global->durable) {
967                         uint8_t p[8] = { 0, };
968                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
969
970                         status = smb2_create_blob_add(state,
971                                                       &out_context_blobs,
972                                                       SMB2_CREATE_TAG_DHNQ,
973                                                       blob);
974                         if (!NT_STATUS_IS_OK(status)) {
975                                 tevent_req_nterror(req, status);
976                                 return tevent_req_post(req, ev);
977                         }
978                 }
979
980                 if (dh2q && op->global->durable) {
981                         uint8_t p[8] = { 0, };
982                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
983                         uint32_t durable_v2_response_flags = 0;
984
985                         SIVAL(p, 0, op->global->durable_timeout_msec);
986                         SIVAL(p, 4, durable_v2_response_flags);
987
988                         status = smb2_create_blob_add(state, &out_context_blobs,
989                                                       SMB2_CREATE_TAG_DH2Q,
990                                                       blob);
991                         if (!NT_STATUS_IS_OK(status)) {
992                                 tevent_req_nterror(req, status);
993                                 return tevent_req_post(req, ev);
994                         }
995                 }
996
997                 if (qfid) {
998                         uint8_t p[32];
999                         uint64_t file_index = get_FileIndex(result->conn,
1000                                                         &result->fsp_name->st);
1001                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
1002
1003                         ZERO_STRUCT(p);
1004
1005                         /* From conversations with Microsoft engineers at
1006                            the MS plugfest. The first 8 bytes are the "volume index"
1007                            == inode, the second 8 bytes are the "volume id",
1008                            == dev. This will be updated in the SMB2 doc. */
1009                         SBVAL(p, 0, file_index);
1010                         SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
1011
1012                         status = smb2_create_blob_add(state, &out_context_blobs,
1013                                                       SMB2_CREATE_TAG_QFID,
1014                                                       blob);
1015                         if (!NT_STATUS_IS_OK(status)) {
1016                                 tevent_req_nterror(req, status);
1017                                 return tevent_req_post(req, ev);
1018                         }
1019                 }
1020         }
1021
1022         smb2req->compat_chain_fsp = smb1req->chain_fsp;
1023
1024         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
1025                 state->out_oplock_level = in_oplock_level;
1026         } else {
1027                 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
1028         }
1029
1030         if ((in_create_disposition == FILE_SUPERSEDE)
1031             && (info == FILE_WAS_OVERWRITTEN)) {
1032                 state->out_create_action = FILE_WAS_SUPERSEDED;
1033         } else {
1034                 state->out_create_action = info;
1035         }
1036         state->out_file_attributes = dos_mode(result->conn,
1037                                            result->fsp_name);
1038         /* Deal with other possible opens having a modified
1039            write time. JRA. */
1040         ZERO_STRUCT(write_time_ts);
1041         get_file_infos(result->file_id, 0, NULL, &write_time_ts);
1042         if (!null_timespec(write_time_ts)) {
1043                 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
1044         }
1045
1046         unix_timespec_to_nt_time(&state->out_creation_time,
1047                         get_create_timespec(smb1req->conn, result,
1048                                         result->fsp_name));
1049         unix_timespec_to_nt_time(&state->out_last_access_time,
1050                         result->fsp_name->st.st_ex_atime);
1051         unix_timespec_to_nt_time(&state->out_last_write_time,
1052                         result->fsp_name->st.st_ex_mtime);
1053         unix_timespec_to_nt_time(&state->out_change_time,
1054                         get_change_timespec(smb1req->conn, result,
1055                                         result->fsp_name));
1056         state->out_allocation_size =
1057                         SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
1058                                                &(result->fsp_name->st));
1059         state->out_end_of_file = result->fsp_name->st.st_ex_size;
1060         if (state->out_file_attributes == 0) {
1061                 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
1062         }
1063         state->out_file_id_persistent = result->op->global->open_persistent_id;
1064         state->out_file_id_volatile = result->op->global->open_volatile_id;
1065         state->out_context_blobs = out_context_blobs;
1066
1067         DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1068                   fsp_str_dbg(result), fsp_fnum_dbg(result)));
1069
1070         tevent_req_done(req);
1071         return tevent_req_post(req, ev);
1072 }
1073
1074 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
1075                         TALLOC_CTX *mem_ctx,
1076                         uint8_t *out_oplock_level,
1077                         uint32_t *out_create_action,
1078                         NTTIME *out_creation_time,
1079                         NTTIME *out_last_access_time,
1080                         NTTIME *out_last_write_time,
1081                         NTTIME *out_change_time,
1082                         uint64_t *out_allocation_size,
1083                         uint64_t *out_end_of_file,
1084                         uint32_t *out_file_attributes,
1085                         uint64_t *out_file_id_persistent,
1086                         uint64_t *out_file_id_volatile,
1087                         struct smb2_create_blobs *out_context_blobs)
1088 {
1089         NTSTATUS status;
1090         struct smbd_smb2_create_state *state = tevent_req_data(req,
1091                                                struct smbd_smb2_create_state);
1092
1093         if (tevent_req_is_nterror(req, &status)) {
1094                 tevent_req_received(req);
1095                 return status;
1096         }
1097
1098         *out_oplock_level       = state->out_oplock_level;
1099         *out_create_action      = state->out_create_action;
1100         *out_creation_time      = state->out_creation_time;
1101         *out_last_access_time   = state->out_last_access_time;
1102         *out_last_write_time    = state->out_last_write_time;
1103         *out_change_time        = state->out_change_time;
1104         *out_allocation_size    = state->out_allocation_size;
1105         *out_end_of_file        = state->out_end_of_file;
1106         *out_file_attributes    = state->out_file_attributes;
1107         *out_file_id_persistent = state->out_file_id_persistent;
1108         *out_file_id_volatile   = state->out_file_id_volatile;
1109         *out_context_blobs      = state->out_context_blobs;
1110
1111         talloc_steal(mem_ctx, state->out_context_blobs.blobs);
1112
1113         tevent_req_received(req);
1114         return NT_STATUS_OK;
1115 }
1116
1117 /*********************************************************
1118  Code for dealing with deferred opens.
1119 *********************************************************/
1120
1121 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
1122                         struct timeval *p_request_time,
1123                         void **pp_state)
1124 {
1125         struct smbd_smb2_create_state *state = NULL;
1126         struct tevent_req *req = NULL;
1127
1128         if (!smb2req) {
1129                 return false;
1130         }
1131         req = smb2req->subreq;
1132         if (!req) {
1133                 return false;
1134         }
1135         state = tevent_req_data(req, struct smbd_smb2_create_state);
1136         if (!state) {
1137                 return false;
1138         }
1139         if (!state->open_was_deferred) {
1140                 return false;
1141         }
1142         if (p_request_time) {
1143                 *p_request_time = state->request_time;
1144         }
1145         if (pp_state) {
1146                 *pp_state = (void *)state->private_data.data;
1147         }
1148         return true;
1149 }
1150
1151 /*********************************************************
1152  Re-process this call early - requested by message or
1153  close.
1154 *********************************************************/
1155
1156 static struct smbd_smb2_request *find_open_smb2req(
1157         struct smbd_server_connection *sconn, uint64_t mid)
1158 {
1159         struct smbd_smb2_request *smb2req;
1160
1161         for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
1162                 uint64_t message_id;
1163                 if (smb2req->subreq == NULL) {
1164                         /* This message has been processed. */
1165                         continue;
1166                 }
1167                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1168                         /* This message has been processed. */
1169                         continue;
1170                 }
1171                 message_id = get_mid_from_smb2req(smb2req);
1172                 if (message_id == mid) {
1173                         return smb2req;
1174                 }
1175         }
1176         return NULL;
1177 }
1178
1179 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
1180 {
1181         struct smbd_smb2_create_state *state = NULL;
1182         struct smbd_smb2_request *smb2req;
1183
1184         smb2req = find_open_smb2req(sconn, mid);
1185
1186         if (!smb2req) {
1187                 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1188                         (unsigned long long)mid));
1189                 return false;
1190         }
1191         if (!smb2req->subreq) {
1192                 return false;
1193         }
1194         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1195                 return false;
1196         }
1197         state = tevent_req_data(smb2req->subreq,
1198                         struct smbd_smb2_create_state);
1199         if (!state) {
1200                 return false;
1201         }
1202         /* It's not in progress if there's no timeout event. */
1203         if (!state->open_was_deferred) {
1204                 return false;
1205         }
1206
1207         DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1208                         (unsigned long long)mid));
1209
1210         return true;
1211 }
1212
1213 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
1214                                                         uint64_t mid)
1215 {
1216         struct smbd_smb2_create_state *state = NULL;
1217
1218         if (!smb2req->subreq) {
1219                 return;
1220         }
1221         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1222                 return;
1223         }
1224         state = tevent_req_data(smb2req->subreq,
1225                         struct smbd_smb2_create_state);
1226         if (!state) {
1227                 return;
1228         }
1229
1230         DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1231                 "mid %llu\n",
1232                 (unsigned long long)mid ));
1233
1234         state->open_was_deferred = false;
1235         /* Ensure we don't have any outstanding timer event. */
1236         TALLOC_FREE(state->te);
1237         /* Ensure we don't have any outstanding immediate event. */
1238         TALLOC_FREE(state->im);
1239 }
1240
1241 void remove_deferred_open_message_smb2(
1242         struct smbd_server_connection *sconn, uint64_t mid)
1243 {
1244         struct smbd_smb2_request *smb2req;
1245
1246         smb2req = find_open_smb2req(sconn, mid);
1247
1248         if (!smb2req) {
1249                 DEBUG(10,("remove_deferred_open_message_smb2: "
1250                         "can't find mid %llu\n",
1251                         (unsigned long long)mid ));
1252                 return;
1253         }
1254         remove_deferred_open_message_smb2_internal(smb2req, mid);
1255 }
1256
1257 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1258                                         struct tevent_immediate *im,
1259                                         void *private_data)
1260 {
1261         struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1262                                         struct smbd_smb2_request);
1263         struct smbd_server_connection *sconn = smb2req->sconn;
1264         uint64_t mid = get_mid_from_smb2req(smb2req);
1265         NTSTATUS status;
1266
1267         DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1268                 "re-dispatching mid %llu\n",
1269                 (unsigned long long)mid ));
1270
1271         status = smbd_smb2_request_dispatch(smb2req);
1272         if (!NT_STATUS_IS_OK(status)) {
1273                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1274                 return;
1275         }
1276 }
1277
1278 bool schedule_deferred_open_message_smb2(
1279         struct smbd_server_connection *sconn, uint64_t mid)
1280 {
1281         struct smbd_smb2_create_state *state = NULL;
1282         struct smbd_smb2_request *smb2req;
1283
1284         smb2req = find_open_smb2req(sconn, mid);
1285
1286         if (!smb2req) {
1287                 DEBUG(10,("schedule_deferred_open_message_smb2: "
1288                         "can't find mid %llu\n",
1289                         (unsigned long long)mid ));
1290                 return false;
1291         }
1292         if (!smb2req->subreq) {
1293                 return false;
1294         }
1295         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1296                 return false;
1297         }
1298         state = tevent_req_data(smb2req->subreq,
1299                         struct smbd_smb2_create_state);
1300         if (!state) {
1301                 return false;
1302         }
1303
1304         /* Ensure we don't have any outstanding timer event. */
1305         TALLOC_FREE(state->te);
1306         /* Ensure we don't have any outstanding immediate event. */
1307         TALLOC_FREE(state->im);
1308
1309         /*
1310          * This is subtle. We must null out the callback
1311          * before rescheduling, else the first call to
1312          * tevent_req_nterror() causes the _receive()
1313          * function to be called, this causing tevent_req_post()
1314          * to crash.
1315          */
1316         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1317
1318         state->im = tevent_create_immediate(smb2req);
1319         if (!state->im) {
1320                 smbd_server_connection_terminate(smb2req->sconn,
1321                         nt_errstr(NT_STATUS_NO_MEMORY));
1322                 return false;
1323         }
1324
1325         DEBUG(10,("schedule_deferred_open_message_smb2: "
1326                 "re-processing mid %llu\n",
1327                 (unsigned long long)mid ));
1328
1329         tevent_schedule_immediate(state->im,
1330                         smb2req->sconn->ev_ctx,
1331                         smbd_smb2_create_request_dispatch_immediate,
1332                         smb2req);
1333
1334         return true;
1335 }
1336
1337 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1338 {
1339         struct smbd_smb2_request *smb2req = NULL;
1340         struct smbd_smb2_create_state *state = tevent_req_data(req,
1341                                 struct smbd_smb2_create_state);
1342         uint64_t mid;
1343
1344         if (!state) {
1345                 return false;
1346         }
1347
1348         if (!state->smb2req) {
1349                 return false;
1350         }
1351
1352         smb2req = state->smb2req;
1353         mid = get_mid_from_smb2req(smb2req);
1354
1355         if (is_deferred_open_async(state->private_data.data)) {
1356                 /* Can't cancel an async create. */
1357                 return false;
1358         }
1359
1360         remove_deferred_open_message_smb2_internal(smb2req, mid);
1361
1362         tevent_req_defer_callback(req, smb2req->sconn->ev_ctx);
1363         tevent_req_nterror(req, NT_STATUS_CANCELLED);
1364         return true;
1365 }
1366
1367 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1368                                 struct timeval request_time,
1369                                 struct timeval timeout,
1370                                 struct file_id id,
1371                                 char *private_data,
1372                                 size_t priv_len)
1373 {
1374         struct tevent_req *req = NULL;
1375         struct smbd_smb2_create_state *state = NULL;
1376         struct timeval end_time;
1377
1378         if (!smb2req) {
1379                 return false;
1380         }
1381         req = smb2req->subreq;
1382         if (!req) {
1383                 return false;
1384         }
1385         state = tevent_req_data(req, struct smbd_smb2_create_state);
1386         if (!state) {
1387                 return false;
1388         }
1389         state->id = id;
1390         state->request_time = request_time;
1391         state->private_data = data_blob_talloc(state, private_data,
1392                                                 priv_len);
1393         if (!state->private_data.data) {
1394                 return false;
1395         }
1396
1397         /* Re-schedule us to retry on timer expiry. */
1398         end_time = timeval_sum(&request_time, &timeout);
1399
1400         DEBUG(10,("push_deferred_open_message_smb2: "
1401                 "timeout at %s\n",
1402                 timeval_string(talloc_tos(),
1403                                 &end_time,
1404                                 true) ));
1405
1406         state->open_was_deferred = true;
1407
1408         /* allow this request to be canceled */
1409         tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1410
1411         return true;
1412 }