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