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