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