s3:smb2_create: avoid reusing the 'tevent_req' within smbd_smb2_create_send()
[sfrench/samba-autobuild/.git] / source3 / smbd / smb2_create.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "printing.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "../libcli/smb/smb_common.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "../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(struct smb_request *smb1req,
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         uint32_t ucf_flags;
383         NTSTATUS status;
384
385         if (lease_ptr == NULL) {
386                 if (fsp->oplock_type != LEASE_OPLOCK) {
387                         return NT_STATUS_OK;
388                 }
389                 DEBUG(10, ("Reopened file has lease, but no lease "
390                            "requested\n"));
391                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
392         }
393
394         if (fsp->oplock_type != LEASE_OPLOCK) {
395                 DEBUG(10, ("Lease requested, but reopened file has no "
396                            "lease\n"));
397                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
398         }
399
400         if (!smb2_lease_key_equal(&lease_ptr->lease_key,
401                                   &fsp->lease->lease.lease_key)) {
402                 DEBUG(10, ("Different lease key requested than found "
403                            "in reopened file\n"));
404                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
405         }
406
407         ucf_flags = filename_create_ucf_flags(smb1req, FILE_OPEN);
408         status = filename_convert(talloc_tos(), fsp->conn, false,
409                                   requested_filename, ucf_flags,
410                                   NULL, &smb_fname);
411         if (!NT_STATUS_IS_OK(status)) {
412                 DEBUG(10, ("filename_convert returned %s\n",
413                            nt_errstr(status)));
414                 return status;
415         }
416
417         if (!strequal(fsp->fsp_name->base_name, smb_fname->base_name)) {
418                 DEBUG(10, ("Lease requested for file %s, reopened file "
419                            "is named %s\n", smb_fname->base_name,
420                            fsp->fsp_name->base_name));
421                 TALLOC_FREE(smb_fname);
422                 return NT_STATUS_INVALID_PARAMETER;
423         }
424
425         TALLOC_FREE(smb_fname);
426
427         return NT_STATUS_OK;
428 }
429
430 struct smbd_smb2_create_state {
431         struct smbd_smb2_request *smb2req;
432         struct smb_request *smb1req;
433         bool open_was_deferred;
434         struct tevent_timer *te;
435         struct tevent_immediate *im;
436         struct timeval request_time;
437         struct file_id id;
438         struct deferred_open_record *open_rec;
439         uint8_t out_oplock_level;
440         uint32_t out_create_action;
441         struct timespec out_creation_ts;
442         struct timespec out_last_access_ts;
443         struct timespec out_last_write_ts;
444         struct timespec out_change_ts;
445         uint64_t out_allocation_size;
446         uint64_t out_end_of_file;
447         uint32_t out_file_attributes;
448         uint64_t out_file_id_persistent;
449         uint64_t out_file_id_volatile;
450         struct smb2_create_blobs *out_context_blobs;
451 };
452
453 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
454                         struct tevent_context *ev,
455                         struct smbd_smb2_request *smb2req,
456                         uint8_t in_oplock_level,
457                         uint32_t in_impersonation_level,
458                         uint32_t in_desired_access,
459                         uint32_t in_file_attributes,
460                         uint32_t in_share_access,
461                         uint32_t in_create_disposition,
462                         uint32_t in_create_options,
463                         const char *in_name,
464                         struct smb2_create_blobs in_context_blobs)
465 {
466         struct tevent_req *req = NULL;
467         struct smbd_smb2_create_state *state = NULL;
468         NTSTATUS status;
469         struct smb_request *smb1req = NULL;
470         files_struct *result = NULL;
471         int info;
472         int requested_oplock_level;
473         struct smb2_create_blob *dhnc = NULL;
474         struct smb2_create_blob *dh2c = NULL;
475         struct smb2_create_blob *dhnq = NULL;
476         struct smb2_create_blob *dh2q = NULL;
477         struct smb2_create_blob *rqls = NULL;
478         bool replay_operation = false;
479
480         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
481                 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
482         } else {
483                 requested_oplock_level = in_oplock_level;
484         }
485
486         req = tevent_req_create(mem_ctx, &state,
487                                 struct smbd_smb2_create_state);
488         if (req == NULL) {
489                 return NULL;
490         }
491         state->smb2req = smb2req;
492
493         smb1req = smbd_smb2_fake_smb_request(smb2req);
494         if (tevent_req_nomem(smb1req, req)) {
495                 return tevent_req_post(req, ev);
496         }
497         state->smb1req = smb1req;
498
499         if (smb2req->subreq == NULL) {
500                 DEBUG(10,("smbd_smb2_create: name[%s]\n",
501                         in_name));
502         } else {
503                 struct smbd_smb2_create_state *old_state = tevent_req_data(
504                         smb2req->subreq, struct smbd_smb2_create_state);
505
506                 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
507                         in_name ));
508
509                 state->id = old_state->id;
510                 state->request_time = old_state->request_time;
511                 state->open_rec = talloc_move(state, &old_state->open_rec);
512                 state->open_was_deferred = old_state->open_was_deferred;
513         }
514
515         TALLOC_FREE(smb2req->subreq);
516         smb2req->subreq = req;
517
518         state->out_context_blobs = talloc_zero(state, struct smb2_create_blobs);
519         if (tevent_req_nomem(state->out_context_blobs, req)) {
520                 return tevent_req_post(req, ev);
521         }
522
523         dhnq = smb2_create_blob_find(&in_context_blobs,
524                                      SMB2_CREATE_TAG_DHNQ);
525         dhnc = smb2_create_blob_find(&in_context_blobs,
526                                      SMB2_CREATE_TAG_DHNC);
527         dh2q = smb2_create_blob_find(&in_context_blobs,
528                                      SMB2_CREATE_TAG_DH2Q);
529         dh2c = smb2_create_blob_find(&in_context_blobs,
530                                      SMB2_CREATE_TAG_DH2C);
531         if (smb2req->xconn->smb2.server.capabilities & SMB2_CAP_LEASING) {
532                 rqls = smb2_create_blob_find(&in_context_blobs,
533                                              SMB2_CREATE_TAG_RQLS);
534         }
535
536         if ((dhnc && dh2c) || (dhnc && dh2q) || (dh2c && dhnq) ||
537             (dh2q && dh2c))
538         {
539                 /* not both are allowed at the same time */
540                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
541                 return tevent_req_post(req, ev);
542         }
543
544         if (dhnc) {
545                 uint32_t num_blobs_allowed;
546
547                 if (dhnc->data.length != 16) {
548                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
549                         return tevent_req_post(req, ev);
550                 }
551
552                 /*
553                  * According to MS-SMB2: 3.3.5.9.7, "Handling the
554                  * SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context",
555                  * we should ignore an additional dhnq blob, but fail
556                  * the request (with status OBJECT_NAME_NOT_FOUND) if
557                  * any other extra create blob has been provided.
558                  *
559                  * (Note that the cases of an additional dh2q or dh2c blob
560                  *  which require a different error code, have been treated
561                  *  above.)
562                  */
563
564                 if (dhnq) {
565                         num_blobs_allowed = 2;
566                 } else {
567                         num_blobs_allowed = 1;
568                 }
569
570                 if (rqls != NULL) {
571                         num_blobs_allowed += 1;
572                 }
573
574                 if (in_context_blobs.num_blobs != num_blobs_allowed) {
575                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
576                         return tevent_req_post(req, ev);
577                 }
578         }
579
580         if (dh2c) {
581                 uint32_t num_blobs_allowed;
582
583                 if (dh2c->data.length != 36) {
584                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
585                         return tevent_req_post(req, ev);
586                 }
587
588                 /*
589                  * According to MS-SMB2: 3.3.5.9.12, "Handling the
590                  * SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 Create Context",
591                  * we should fail the request with status
592                  * OBJECT_NAME_NOT_FOUND if any other create blob has been
593                  * provided.
594                  *
595                  * (Note that the cases of an additional dhnq, dhnc or dh2q
596                  *  blob which require a different error code, have been
597                  *  treated above.)
598                  */
599
600                 num_blobs_allowed = 1;
601
602                 if (rqls != NULL) {
603                         num_blobs_allowed += 1;
604                 }
605
606                 if (in_context_blobs.num_blobs != num_blobs_allowed) {
607                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
608                         return tevent_req_post(req, ev);
609                 }
610         }
611
612         if (IS_IPC(smb1req->conn)) {
613                 const char *pipe_name = in_name;
614
615                 if (dhnc || dh2c) {
616                         /* durable handles are not supported on IPC$ */
617                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
618                         return tevent_req_post(req, ev);
619                 }
620
621                 if (!lp_nt_pipe_support()) {
622                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
623                         return tevent_req_post(req, ev);
624                 }
625
626                 status = open_np_file(smb1req, pipe_name, &result);
627                 if (!NT_STATUS_IS_OK(status)) {
628                         tevent_req_nterror(req, status);
629                         return tevent_req_post(req, ev);
630                 }
631                 info = FILE_WAS_OPENED;
632         } else if (CAN_PRINT(smb1req->conn)) {
633                 if (dhnc || dh2c) {
634                         /* durable handles are not supported on printers */
635                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
636                         return tevent_req_post(req, ev);
637                 }
638
639                 status = file_new(smb1req, smb1req->conn, &result);
640                 if(!NT_STATUS_IS_OK(status)) {
641                         tevent_req_nterror(req, status);
642                         return tevent_req_post(req, ev);
643                 }
644
645                 status = print_spool_open(result, in_name,
646                                           smb1req->vuid);
647                 if (!NT_STATUS_IS_OK(status)) {
648                         file_free(smb1req, result);
649                         tevent_req_nterror(req, status);
650                         return tevent_req_post(req, ev);
651                 }
652                 info = FILE_WAS_CREATED;
653         } else {
654                 char *fname;
655                 struct smb2_create_blob *exta = NULL;
656                 struct ea_list *ea_list = NULL;
657                 struct smb2_create_blob *mxac = NULL;
658                 NTTIME max_access_time = 0;
659                 struct smb2_create_blob *secd = NULL;
660                 struct security_descriptor *sec_desc = NULL;
661                 struct smb2_create_blob *alsi = NULL;
662                 uint64_t allocation_size = 0;
663                 struct smb2_create_blob *twrp = NULL;
664                 struct smb2_create_blob *qfid = NULL;
665                 struct GUID _create_guid = GUID_zero();
666                 struct GUID *create_guid = NULL;
667                 bool update_open = false;
668                 bool durable_requested = false;
669                 uint32_t durable_timeout_msec = 0;
670                 bool do_durable_reconnect = false;
671                 uint64_t persistent_id = 0;
672                 struct smb2_lease lease;
673                 struct smb2_lease *lease_ptr = NULL;
674                 ssize_t lease_len = -1;
675                 bool need_replay_cache = false;
676                 struct smbXsrv_open *op = NULL;
677 #if 0
678                 struct smb2_create_blob *svhdx = NULL;
679 #endif
680
681                 exta = smb2_create_blob_find(&in_context_blobs,
682                                              SMB2_CREATE_TAG_EXTA);
683                 mxac = smb2_create_blob_find(&in_context_blobs,
684                                              SMB2_CREATE_TAG_MXAC);
685                 secd = smb2_create_blob_find(&in_context_blobs,
686                                              SMB2_CREATE_TAG_SECD);
687                 alsi = smb2_create_blob_find(&in_context_blobs,
688                                              SMB2_CREATE_TAG_ALSI);
689                 twrp = smb2_create_blob_find(&in_context_blobs,
690                                              SMB2_CREATE_TAG_TWRP);
691                 qfid = smb2_create_blob_find(&in_context_blobs,
692                                              SMB2_CREATE_TAG_QFID);
693 #if 0
694                 if (smb2req->xconn->protocol >= PROTOCOL_SMB3_02) {
695                         /*
696                          * This was introduced with SMB3_02
697                          */
698                         svhdx = smb2_create_blob_find(&in_context_blobs,
699                                                       SVHDX_OPEN_DEVICE_CONTEXT);
700                 }
701 #endif
702
703                 fname = talloc_strdup(state, in_name);
704                 if (tevent_req_nomem(fname, req)) {
705                         return tevent_req_post(req, ev);
706                 }
707
708                 if (exta) {
709                         if (!lp_ea_support(SNUM(smb2req->tcon->compat))) {
710                                 tevent_req_nterror(req,
711                                         NT_STATUS_EAS_NOT_SUPPORTED);
712                                 return tevent_req_post(req, ev);
713                         }
714
715                         ea_list = read_nttrans_ea_list(mem_ctx,
716                                 (const char *)exta->data.data, exta->data.length);
717                         if (!ea_list) {
718                                 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
719                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
720                                 return tevent_req_post(req, ev);
721                         }
722
723                         /*
724                          * NB. When SMB2+ unix extensions are added,
725                          * we need to relax this check in invalid
726                          * names - we used to not do this if
727                          * lp_posix_pathnames() was false.
728                          */
729                         if (ea_list_has_invalid_name(ea_list)) {
730                                 tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
731                                 return tevent_req_post(req, ev);
732                         }
733                 }
734
735                 if (mxac) {
736                         if (mxac->data.length == 0) {
737                                 max_access_time = 0;
738                         } else if (mxac->data.length == 8) {
739                                 max_access_time = BVAL(mxac->data.data, 0);
740                         } else {
741                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
742                                 return tevent_req_post(req, ev);
743                         }
744                 }
745
746                 if (secd) {
747                         enum ndr_err_code ndr_err;
748
749                         sec_desc = talloc_zero(state, struct security_descriptor);
750                         if (tevent_req_nomem(sec_desc, req)) {
751                                 return tevent_req_post(req, ev);
752                         }
753
754                         ndr_err = ndr_pull_struct_blob(&secd->data,
755                                 sec_desc, sec_desc,
756                                 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
757                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
758                                 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
759                                          ndr_errstr(ndr_err)));
760                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
761                                 return tevent_req_post(req, ev);
762                         }
763                 }
764
765                 if (dhnq) {
766                         if (dhnq->data.length != 16) {
767                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
768                                 return tevent_req_post(req, ev);
769                         }
770
771                         if (dh2q) {
772                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
773                                 return tevent_req_post(req, ev);
774                         }
775
776                         /*
777                          * durable handle request is processed below.
778                          */
779                         durable_requested = true;
780                         /*
781                          * Set the timeout to 16 mins.
782                          *
783                          * TODO: test this against Windows 2012
784                          *       as the default for durable v2 is 1 min.
785                          */
786                         durable_timeout_msec = (16*60*1000);
787                 }
788
789                 if (dh2q) {
790                         const uint8_t *p = dh2q->data.data;
791                         uint32_t durable_v2_timeout = 0;
792                         DATA_BLOB create_guid_blob;
793                         const uint8_t *hdr;
794                         uint32_t flags;
795
796                         if (dh2q->data.length != 32) {
797                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
798                                 return tevent_req_post(req, ev);
799                         }
800
801                         if (dhnq) {
802                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
803                                 return tevent_req_post(req, ev);
804                         }
805
806                         durable_v2_timeout = IVAL(p, 0);
807                         create_guid_blob = data_blob_const(p + 16, 16);
808
809                         status = GUID_from_ndr_blob(&create_guid_blob,
810                                                     &_create_guid);
811                         if (tevent_req_nterror(req, status)) {
812                                 return tevent_req_post(req, ev);
813                         }
814                         create_guid = &_create_guid;
815                         /*
816                          * we need to store the create_guid later
817                          */
818                         update_open = true;
819
820                         /*
821                          * And we need to create a cache for replaying the
822                          * create.
823                          */
824                         need_replay_cache = true;
825
826                         /*
827                          * durable handle v2 request processed below
828                          */
829                         durable_requested = true;
830                         durable_timeout_msec = durable_v2_timeout;
831                         if (durable_timeout_msec == 0) {
832                                 /*
833                                  * Set the timeout to 1 min as default.
834                                  *
835                                  * This matches Windows 2012.
836                                  */
837                                 durable_timeout_msec = (60*1000);
838                         }
839
840                         /*
841                          * Check for replay operation.
842                          * Only consider it when we have dh2q.
843                          * If we do not have a replay operation, verify that
844                          * the create_guid is not cached for replay.
845                          */
846                         hdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
847                         flags = IVAL(hdr, SMB2_HDR_FLAGS);
848                         replay_operation =
849                                 flags & SMB2_HDR_FLAG_REPLAY_OPERATION;
850
851                         status = smb2srv_open_lookup_replay_cache(
852                                         smb2req->xconn, create_guid,
853                                         0 /* now */, &op);
854
855                         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
856                                 replay_operation = false;
857                         } else if (tevent_req_nterror(req, status)) {
858                                 DBG_WARNING("smb2srv_open_lookup_replay_cache "
859                                             "failed: %s\n", nt_errstr(status));
860                                 return tevent_req_post(req, ev);
861                         } else if (!replay_operation) {
862                                 /*
863                                  * If a create without replay operation flag
864                                  * is sent but with a create_guid that is
865                                  * currently in the replay cache -- fail.
866                                  */
867                                 status = NT_STATUS_DUPLICATE_OBJECTID;
868                                 (void)tevent_req_nterror(req, status);
869                                 return tevent_req_post(req, ev);
870                         }
871                 }
872
873                 if (dhnc) {
874                         persistent_id = BVAL(dhnc->data.data, 0);
875
876                         do_durable_reconnect = true;
877                 }
878
879                 if (dh2c) {
880                         const uint8_t *p = dh2c->data.data;
881                         DATA_BLOB create_guid_blob;
882
883                         persistent_id = BVAL(p, 0);
884                         create_guid_blob = data_blob_const(p + 16, 16);
885
886                         status = GUID_from_ndr_blob(&create_guid_blob,
887                                                     &_create_guid);
888                         if (tevent_req_nterror(req, status)) {
889                                 return tevent_req_post(req, ev);
890                         }
891                         create_guid = &_create_guid;
892
893                         do_durable_reconnect = true;
894                 }
895
896                 if (alsi) {
897                         if (alsi->data.length != 8) {
898                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
899                                 return tevent_req_post(req, ev);
900                         }
901                         allocation_size = BVAL(alsi->data.data, 0);
902                 }
903
904                 if (twrp) {
905                         NTTIME nttime;
906                         time_t t;
907                         struct tm *tm;
908
909                         if (twrp->data.length != 8) {
910                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
911                                 return tevent_req_post(req, ev);
912                         }
913
914                         nttime = BVAL(twrp->data.data, 0);
915                         t = nt_time_to_unix(nttime);
916                         tm = gmtime(&t);
917
918                         TALLOC_FREE(fname);
919                         fname = talloc_asprintf(state,
920                                         "%s\\@GMT-%04u.%02u.%02u-%02u.%02u.%02u",
921                                         in_name,
922                                         tm->tm_year + 1900,
923                                         tm->tm_mon + 1,
924                                         tm->tm_mday,
925                                         tm->tm_hour,
926                                         tm->tm_min,
927                                         tm->tm_sec);
928                         if (tevent_req_nomem(fname, req)) {
929                                 return tevent_req_post(req, ev);
930                         }
931                 }
932
933                 if (qfid) {
934                         if (qfid->data.length != 0) {
935                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
936                                 return tevent_req_post(req, ev);
937                         }
938                 }
939
940                 if (rqls) {
941                         lease_len = smb2_lease_pull(
942                                 rqls->data.data, rqls->data.length, &lease);
943                         if (lease_len == -1) {
944                                 tevent_req_nterror(
945                                         req, NT_STATUS_INVALID_PARAMETER);
946                                 return tevent_req_post(req, ev);
947                         }
948                         lease_ptr = &lease;
949
950                         if (DEBUGLEVEL >= 10) {
951                                 DEBUG(10, ("Got lease request size %d\n",
952                                            (int)lease_len));
953                                 NDR_PRINT_DEBUG(smb2_lease, lease_ptr);
954                         }
955
956                         if (!smb2_lease_key_valid(&lease.lease_key)) {
957                                 lease_ptr = NULL;
958                                 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
959                         }
960
961                         if ((smb2req->xconn->protocol < PROTOCOL_SMB3_00) &&
962                             (lease.lease_version != 1)) {
963                                 DEBUG(10, ("v2 lease key only for SMB3\n"));
964                                 lease_ptr = NULL;
965                         }
966
967                         /*
968                          * Replay with a lease is only allowed if the
969                          * established open carries a lease with the
970                          * same lease key.
971                          */
972                         if (replay_operation) {
973                                 struct smb2_lease *op_ls =
974                                                 &op->compat->lease->lease;
975                                 int op_oplock = op->compat->oplock_type;
976
977                                 if (map_samba_oplock_levels_to_smb2(op_oplock)
978                                     != SMB2_OPLOCK_LEVEL_LEASE)
979                                 {
980                                         status = NT_STATUS_ACCESS_DENIED;
981                                         (void)tevent_req_nterror(req, status);
982                                         return tevent_req_post(req, ev);
983                                 }
984                                 if (!smb2_lease_key_equal(&lease.lease_key,
985                                                           &op_ls->lease_key))
986                                 {
987                                         status = NT_STATUS_ACCESS_DENIED;
988                                         (void)tevent_req_nterror(req, status);
989                                         return tevent_req_post(req, ev);
990                                 }
991                         }
992                 }
993
994                 /* these are ignored for SMB2 */
995                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
996                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
997
998                 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
999
1000                 DEBUG(10, ("smbd_smb2_create_send: open execution phase\n"));
1001
1002                 /*
1003                  * For the backend file open procedure, there are
1004                  * three possible modes: replay operation (in which case
1005                  * there is nothing else to do), durable_reconnect or
1006                  * new open.
1007                  */
1008                 if (replay_operation) {
1009                         result = op->compat;
1010                         result->op = op;
1011                         update_open = false;
1012                         info = op->create_action;
1013                 } else if (do_durable_reconnect) {
1014                         DATA_BLOB new_cookie = data_blob_null;
1015                         NTTIME now = timeval_to_nttime(&smb2req->request_time);
1016
1017                         status = smb2srv_open_recreate(smb2req->xconn,
1018                                                 smb1req->conn->session_info,
1019                                                 persistent_id, create_guid,
1020                                                 now, &op);
1021                         if (!NT_STATUS_IS_OK(status)) {
1022                                 DEBUG(3, ("smbd_smb2_create_send: "
1023                                           "smb2srv_open_recreate failed: %s\n",
1024                                           nt_errstr(status)));
1025                                 tevent_req_nterror(req, status);
1026                                 return tevent_req_post(req, ev);
1027                         }
1028
1029                         DEBUG(10, ("smb2_create_send: %s to recreate the "
1030                                    "smb2srv_open struct for a durable handle.\n",
1031                                    op->global->durable ? "succeded" : "failed"));
1032
1033                         if (!op->global->durable) {
1034                                 talloc_free(op);
1035                                 tevent_req_nterror(req,
1036                                         NT_STATUS_OBJECT_NAME_NOT_FOUND);
1037                                 return tevent_req_post(req, ev);
1038                         }
1039
1040                         status = SMB_VFS_DURABLE_RECONNECT(smb1req->conn,
1041                                                 smb1req,
1042                                                 op, /* smbXsrv_open input */
1043                                                 op->global->backend_cookie,
1044                                                 op, /* TALLOC_CTX */
1045                                                 &result, &new_cookie);
1046                         if (!NT_STATUS_IS_OK(status)) {
1047                                 NTSTATUS return_status;
1048
1049                                 return_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1050
1051                                 DEBUG(3, ("smbd_smb2_create_send: "
1052                                           "durable_reconnect failed: %s => %s\n",
1053                                           nt_errstr(status),
1054                                           nt_errstr(return_status)));
1055
1056                                 tevent_req_nterror(req, return_status);
1057                                 return tevent_req_post(req, ev);
1058                         }
1059
1060                         DEBUG(10, ("result->oplock_type=%u, lease_ptr==%p\n",
1061                                    (unsigned)result->oplock_type, lease_ptr));
1062
1063                         status = smbd_smb2_create_durable_lease_check(
1064                                 smb1req, fname, result, lease_ptr);
1065                         if (!NT_STATUS_IS_OK(status)) {
1066                                 close_file(smb1req, result, SHUTDOWN_CLOSE);
1067                                 tevent_req_nterror(req, status);
1068                                 return tevent_req_post(req, ev);
1069                         }
1070
1071                         data_blob_free(&op->global->backend_cookie);
1072                         op->global->backend_cookie = new_cookie;
1073
1074                         op->status = NT_STATUS_OK;
1075                         op->global->disconnect_time = 0;
1076
1077                         /* save the timout for later update */
1078                         durable_timeout_msec = op->global->durable_timeout_msec;
1079
1080                         update_open = true;
1081
1082                         info = FILE_WAS_OPENED;
1083                 } else {
1084                         struct smb_filename *smb_fname = NULL;
1085                         uint32_t ucf_flags;
1086
1087                         if (requested_oplock_level == SMB2_OPLOCK_LEVEL_LEASE) {
1088                                 if (lease_ptr == NULL) {
1089                                         requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
1090                                 }
1091                         } else {
1092                                 lease_ptr = NULL;
1093                         }
1094
1095                         /*
1096                          * For a DFS path the function parse_dfs_path()
1097                          * will do the path processing.
1098                          */
1099
1100                         if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
1101                                 /* convert '\\' into '/' */
1102                                 status = check_path_syntax(fname);
1103                                 if (!NT_STATUS_IS_OK(status)) {
1104                                         tevent_req_nterror(req, status);
1105                                         return tevent_req_post(req, ev);
1106                                 }
1107                         }
1108
1109                         ucf_flags = filename_create_ucf_flags(smb1req, in_create_disposition);
1110                         status = filename_convert(req,
1111                                                   smb1req->conn,
1112                                                   smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
1113                                                   fname,
1114                                                   ucf_flags,
1115                                                   NULL, /* ppath_contains_wcards */
1116                                                   &smb_fname);
1117                         if (!NT_STATUS_IS_OK(status)) {
1118                                 tevent_req_nterror(req, status);
1119                                 return tevent_req_post(req, ev);
1120                         }
1121
1122                         /*
1123                          * MS-SMB2: 2.2.13 SMB2 CREATE Request
1124                          * ImpersonationLevel ... MUST contain one of the
1125                          * following values. The server MUST validate this
1126                          * field, but otherwise ignore it.
1127                          *
1128                          * NB. The source4/torture/smb2/durable_open.c test
1129                          * shows this check is only done on real opens, not
1130                          * on durable handle-reopens.
1131                          */
1132
1133                         if (in_impersonation_level >
1134                                         SMB2_IMPERSONATION_DELEGATE) {
1135                                 tevent_req_nterror(req,
1136                                         NT_STATUS_BAD_IMPERSONATION_LEVEL);
1137                                 return tevent_req_post(req, ev);
1138                         }
1139
1140                         /*
1141                          * We know we're going to do a local open, so now
1142                          * we must be protocol strict. JRA.
1143                          *
1144                          * MS-SMB2: 3.3.5.9 - Receiving an SMB2 CREATE Request
1145                          * If the file name length is greater than zero and the
1146                          * first character is a path separator character, the
1147                          * server MUST fail the request with
1148                          * STATUS_INVALID_PARAMETER.
1149                          */
1150                         if (in_name[0] == '\\' || in_name[0] == '/') {
1151                                 tevent_req_nterror(req,
1152                                         NT_STATUS_INVALID_PARAMETER);
1153                                 return tevent_req_post(req, ev);
1154                         }
1155
1156                         status = SMB_VFS_CREATE_FILE(smb1req->conn,
1157                                                      smb1req,
1158                                                      0, /* root_dir_fid */
1159                                                      smb_fname,
1160                                                      in_desired_access,
1161                                                      in_share_access,
1162                                                      in_create_disposition,
1163                                                      in_create_options,
1164                                                      in_file_attributes,
1165                                                      map_smb2_oplock_levels_to_samba(requested_oplock_level),
1166                                                      lease_ptr,
1167                                                      allocation_size,
1168                                                      0, /* private_flags */
1169                                                      sec_desc,
1170                                                      ea_list,
1171                                                      &result,
1172                                                      &info,
1173                                                      &in_context_blobs,
1174                                                      state->out_context_blobs);
1175                         if (!NT_STATUS_IS_OK(status)) {
1176                                 if (open_was_deferred(smb1req->xconn, smb1req->mid)) {
1177                                         SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
1178                                         return req;
1179                                 }
1180                                 tevent_req_nterror(req, status);
1181                                 return tevent_req_post(req, ev);
1182                         }
1183                         op = result->op;
1184                 }
1185
1186                 /*
1187                  * here we have op == result->op
1188                  */
1189
1190                 DEBUG(10, ("smbd_smb2_create_send: "
1191                            "response construction phase\n"));
1192
1193                 if (mxac) {
1194                         NTTIME last_write_time;
1195
1196                         last_write_time = unix_timespec_to_nt_time(
1197                                                  result->fsp_name->st.st_ex_mtime);
1198                         if (last_write_time != max_access_time) {
1199                                 uint8_t p[8];
1200                                 uint32_t max_access_granted;
1201                                 DATA_BLOB blob = data_blob_const(p, sizeof(p));
1202
1203                                 status = smbd_calculate_access_mask(smb1req->conn,
1204                                                         result->fsp_name,
1205                                                         false,
1206                                                         SEC_FLAG_MAXIMUM_ALLOWED,
1207                                                         &max_access_granted);
1208
1209                                 SIVAL(p, 0, NT_STATUS_V(status));
1210                                 SIVAL(p, 4, max_access_granted);
1211
1212                                 status = smb2_create_blob_add(
1213                                     state->out_context_blobs,
1214                                     state->out_context_blobs,
1215                                     SMB2_CREATE_TAG_MXAC,
1216                                     blob);
1217                                 if (!NT_STATUS_IS_OK(status)) {
1218                                         tevent_req_nterror(req, status);
1219                                         return tevent_req_post(req, ev);
1220                                 }
1221                         }
1222                 }
1223
1224                 if (!replay_operation && durable_requested &&
1225                     (fsp_lease_type(result) & SMB2_LEASE_HANDLE))
1226                 {
1227                         status = SMB_VFS_DURABLE_COOKIE(result,
1228                                                 op,
1229                                                 &op->global->backend_cookie);
1230                         if (!NT_STATUS_IS_OK(status)) {
1231                                 op->global->backend_cookie = data_blob_null;
1232                         }
1233                 }
1234                 if (!replay_operation && op->global->backend_cookie.length > 0)
1235                 {
1236                         update_open = true;
1237
1238                         op->global->durable = true;
1239                         op->global->durable_timeout_msec = durable_timeout_msec;
1240                 }
1241
1242                 if (update_open) {
1243                         op->global->create_guid = _create_guid;
1244                         if (need_replay_cache) {
1245                                 op->flags |= SMBXSRV_OPEN_NEED_REPLAY_CACHE;
1246                         }
1247
1248                         status = smbXsrv_open_update(op);
1249                         DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
1250                                    "returned %s\n",
1251                                    nt_errstr(status)));
1252                         if (!NT_STATUS_IS_OK(status)) {
1253                                 tevent_req_nterror(req, status);
1254                                 return tevent_req_post(req, ev);
1255                         }
1256                 }
1257
1258                 if (dhnq && op->global->durable) {
1259                         uint8_t p[8] = { 0, };
1260                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
1261
1262                         status = smb2_create_blob_add(state->out_context_blobs,
1263                                                       state->out_context_blobs,
1264                                                       SMB2_CREATE_TAG_DHNQ,
1265                                                       blob);
1266                         if (!NT_STATUS_IS_OK(status)) {
1267                                 tevent_req_nterror(req, status);
1268                                 return tevent_req_post(req, ev);
1269                         }
1270                 }
1271
1272                 if (dh2q && op->global->durable &&
1273                     /*
1274                      * For replay operations, we return the dh2q blob
1275                      * in the case of oplocks not based on the state of
1276                      * the open, but on whether it could have been granted
1277                      * for the request data. In the case of leases instead,
1278                      * the state of the open is used...
1279                      */
1280                     (!replay_operation ||
1281                      in_oplock_level == SMB2_OPLOCK_LEVEL_BATCH ||
1282                      in_oplock_level == SMB2_OPLOCK_LEVEL_LEASE))
1283                 {
1284                         uint8_t p[8] = { 0, };
1285                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
1286                         uint32_t durable_v2_response_flags = 0;
1287
1288                         SIVAL(p, 0, op->global->durable_timeout_msec);
1289                         SIVAL(p, 4, durable_v2_response_flags);
1290
1291                         status = smb2_create_blob_add(state->out_context_blobs,
1292                                                       state->out_context_blobs,
1293                                                       SMB2_CREATE_TAG_DH2Q,
1294                                                       blob);
1295                         if (!NT_STATUS_IS_OK(status)) {
1296                                 tevent_req_nterror(req, status);
1297                                 return tevent_req_post(req, ev);
1298                         }
1299                 }
1300
1301                 if (qfid) {
1302                         uint8_t p[32];
1303                         uint64_t file_index = get_FileIndex(result->conn,
1304                                                         &result->fsp_name->st);
1305                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
1306
1307                         ZERO_STRUCT(p);
1308
1309                         /* From conversations with Microsoft engineers at
1310                            the MS plugfest. The first 8 bytes are the "volume index"
1311                            == inode, the second 8 bytes are the "volume id",
1312                            == dev. This will be updated in the SMB2 doc. */
1313                         SBVAL(p, 0, file_index);
1314                         SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
1315
1316                         status = smb2_create_blob_add(state->out_context_blobs,
1317                                                       state->out_context_blobs,
1318                                                       SMB2_CREATE_TAG_QFID,
1319                                                       blob);
1320                         if (!NT_STATUS_IS_OK(status)) {
1321                                 tevent_req_nterror(req, status);
1322                                 return tevent_req_post(req, ev);
1323                         }
1324                 }
1325
1326                 if ((rqls != NULL) && (result->oplock_type == LEASE_OPLOCK)) {
1327                         uint8_t buf[52];
1328
1329                         lease = result->lease->lease;
1330
1331                         lease_len = sizeof(buf);
1332                         if (lease.lease_version == 1) {
1333                                 lease_len = 32;
1334                         }
1335
1336                         if (!smb2_lease_push(&lease, buf, lease_len)) {
1337                                 tevent_req_nterror(
1338                                         req, NT_STATUS_INTERNAL_ERROR);
1339                                 return tevent_req_post(req, ev);
1340                         }
1341
1342                         status = smb2_create_blob_add(
1343                                 state, state->out_context_blobs,
1344                                 SMB2_CREATE_TAG_RQLS,
1345                                 data_blob_const(buf, lease_len));
1346                         if (!NT_STATUS_IS_OK(status)) {
1347                                 tevent_req_nterror(req, status);
1348                                 return tevent_req_post(req, ev);
1349                         }
1350                 }
1351         }
1352
1353         smb2req->compat_chain_fsp = smb1req->chain_fsp;
1354
1355         if (replay_operation) {
1356                 state->out_oplock_level = in_oplock_level;
1357         } else if (lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
1358                 state->out_oplock_level = in_oplock_level;
1359         } else {
1360                 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
1361         }
1362
1363         if ((in_create_disposition == FILE_SUPERSEDE)
1364             && (info == FILE_WAS_OVERWRITTEN)) {
1365                 state->out_create_action = FILE_WAS_SUPERSEDED;
1366         } else {
1367                 state->out_create_action = info;
1368         }
1369         result->op->create_action = state->out_create_action;
1370         state->out_file_attributes = dos_mode(result->conn,
1371                                            result->fsp_name);
1372
1373         state->out_creation_ts = get_create_timespec(smb1req->conn,
1374                                         result, result->fsp_name);
1375         state->out_last_access_ts = result->fsp_name->st.st_ex_atime;
1376         state->out_last_write_ts = result->fsp_name->st.st_ex_mtime;
1377         state->out_change_ts = get_change_timespec(smb1req->conn,
1378                                         result, result->fsp_name);
1379
1380         if (lp_dos_filetime_resolution(SNUM(smb2req->tcon->compat))) {
1381                 dos_filetime_timespec(&state->out_creation_ts);
1382                 dos_filetime_timespec(&state->out_last_access_ts);
1383                 dos_filetime_timespec(&state->out_last_write_ts);
1384                 dos_filetime_timespec(&state->out_change_ts);
1385         }
1386
1387         state->out_allocation_size =
1388                         SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
1389                                                &(result->fsp_name->st));
1390         state->out_end_of_file = result->fsp_name->st.st_ex_size;
1391         if (state->out_file_attributes == 0) {
1392                 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
1393         }
1394         state->out_file_id_persistent = result->op->global->open_persistent_id;
1395         state->out_file_id_volatile = result->op->global->open_volatile_id;
1396
1397         DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
1398                   fsp_str_dbg(result), fsp_fnum_dbg(result)));
1399
1400         tevent_req_done(req);
1401         return tevent_req_post(req, ev);
1402 }
1403
1404 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
1405                         TALLOC_CTX *mem_ctx,
1406                         uint8_t *out_oplock_level,
1407                         uint32_t *out_create_action,
1408                         struct timespec *out_creation_ts,
1409                         struct timespec *out_last_access_ts,
1410                         struct timespec *out_last_write_ts,
1411                         struct timespec *out_change_ts,
1412                         uint64_t *out_allocation_size,
1413                         uint64_t *out_end_of_file,
1414                         uint32_t *out_file_attributes,
1415                         uint64_t *out_file_id_persistent,
1416                         uint64_t *out_file_id_volatile,
1417                         struct smb2_create_blobs *out_context_blobs)
1418 {
1419         NTSTATUS status;
1420         struct smbd_smb2_create_state *state = tevent_req_data(req,
1421                                                struct smbd_smb2_create_state);
1422
1423         if (tevent_req_is_nterror(req, &status)) {
1424                 tevent_req_received(req);
1425                 return status;
1426         }
1427
1428         *out_oplock_level       = state->out_oplock_level;
1429         *out_create_action      = state->out_create_action;
1430         *out_creation_ts        = state->out_creation_ts;
1431         *out_last_access_ts     = state->out_last_access_ts;
1432         *out_last_write_ts      = state->out_last_write_ts;
1433         *out_change_ts          = state->out_change_ts;
1434         *out_allocation_size    = state->out_allocation_size;
1435         *out_end_of_file        = state->out_end_of_file;
1436         *out_file_attributes    = state->out_file_attributes;
1437         *out_file_id_persistent = state->out_file_id_persistent;
1438         *out_file_id_volatile   = state->out_file_id_volatile;
1439         *out_context_blobs      = *(state->out_context_blobs);
1440
1441         talloc_steal(mem_ctx, state->out_context_blobs->blobs);
1442
1443         tevent_req_received(req);
1444         return NT_STATUS_OK;
1445 }
1446
1447 /*********************************************************
1448  Code for dealing with deferred opens.
1449 *********************************************************/
1450
1451 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
1452                         struct timeval *p_request_time,
1453                         struct deferred_open_record **open_rec)
1454 {
1455         struct smbd_smb2_create_state *state = NULL;
1456         struct tevent_req *req = NULL;
1457
1458         if (!smb2req) {
1459                 return false;
1460         }
1461         req = smb2req->subreq;
1462         if (!req) {
1463                 return false;
1464         }
1465         state = tevent_req_data(req, struct smbd_smb2_create_state);
1466         if (!state) {
1467                 return false;
1468         }
1469         if (!state->open_was_deferred) {
1470                 return false;
1471         }
1472         if (p_request_time) {
1473                 *p_request_time = state->request_time;
1474         }
1475         if (open_rec != NULL) {
1476                 *open_rec = state->open_rec;
1477         }
1478         return true;
1479 }
1480
1481 /*********************************************************
1482  Re-process this call early - requested by message or
1483  close.
1484 *********************************************************/
1485
1486 static struct smbd_smb2_request *find_open_smb2req(
1487         struct smbXsrv_connection *xconn, uint64_t mid)
1488 {
1489         struct smbd_smb2_request *smb2req;
1490
1491         for (smb2req = xconn->smb2.requests; smb2req; smb2req = smb2req->next) {
1492                 uint64_t message_id;
1493                 if (smb2req->subreq == NULL) {
1494                         /* This message has been processed. */
1495                         continue;
1496                 }
1497                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
1498                         /* This message has been processed. */
1499                         continue;
1500                 }
1501                 message_id = get_mid_from_smb2req(smb2req);
1502                 if (message_id == mid) {
1503                         return smb2req;
1504                 }
1505         }
1506         return NULL;
1507 }
1508
1509 bool open_was_deferred_smb2(struct smbXsrv_connection *xconn, uint64_t mid)
1510 {
1511         struct smbd_smb2_create_state *state = NULL;
1512         struct smbd_smb2_request *smb2req;
1513
1514         smb2req = find_open_smb2req(xconn, mid);
1515
1516         if (!smb2req) {
1517                 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
1518                         (unsigned long long)mid));
1519                 return false;
1520         }
1521         if (!smb2req->subreq) {
1522                 return false;
1523         }
1524         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1525                 return false;
1526         }
1527         state = tevent_req_data(smb2req->subreq,
1528                         struct smbd_smb2_create_state);
1529         if (!state) {
1530                 return false;
1531         }
1532         /* It's not in progress if there's no timeout event. */
1533         if (!state->open_was_deferred) {
1534                 return false;
1535         }
1536
1537         DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
1538                         (unsigned long long)mid));
1539
1540         return true;
1541 }
1542
1543 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
1544                                                         uint64_t mid)
1545 {
1546         struct smbd_smb2_create_state *state = NULL;
1547
1548         if (!smb2req->subreq) {
1549                 return;
1550         }
1551         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1552                 return;
1553         }
1554         state = tevent_req_data(smb2req->subreq,
1555                         struct smbd_smb2_create_state);
1556         if (!state) {
1557                 return;
1558         }
1559
1560         DEBUG(10,("remove_deferred_open_message_smb2_internal: "
1561                 "mid %llu\n",
1562                 (unsigned long long)mid ));
1563
1564         state->open_was_deferred = false;
1565         /* Ensure we don't have any outstanding timer event. */
1566         TALLOC_FREE(state->te);
1567         /* Ensure we don't have any outstanding immediate event. */
1568         TALLOC_FREE(state->im);
1569 }
1570
1571 void remove_deferred_open_message_smb2(
1572         struct smbXsrv_connection *xconn, uint64_t mid)
1573 {
1574         struct smbd_smb2_request *smb2req;
1575
1576         smb2req = find_open_smb2req(xconn, mid);
1577
1578         if (!smb2req) {
1579                 DEBUG(10,("remove_deferred_open_message_smb2: "
1580                         "can't find mid %llu\n",
1581                         (unsigned long long)mid ));
1582                 return;
1583         }
1584         remove_deferred_open_message_smb2_internal(smb2req, mid);
1585 }
1586
1587 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1588                                         struct tevent_immediate *im,
1589                                         void *private_data)
1590 {
1591         struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1592                                         struct smbd_smb2_request);
1593         uint64_t mid = get_mid_from_smb2req(smb2req);
1594         NTSTATUS status;
1595
1596         DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1597                 "re-dispatching mid %llu\n",
1598                 (unsigned long long)mid ));
1599
1600         status = smbd_smb2_request_dispatch(smb2req);
1601         if (!NT_STATUS_IS_OK(status)) {
1602                 smbd_server_connection_terminate(smb2req->xconn,
1603                                                  nt_errstr(status));
1604                 return;
1605         }
1606 }
1607
1608 bool schedule_deferred_open_message_smb2(
1609         struct smbXsrv_connection *xconn, uint64_t mid)
1610 {
1611         struct smbd_smb2_create_state *state = NULL;
1612         struct smbd_smb2_request *smb2req;
1613
1614         smb2req = find_open_smb2req(xconn, mid);
1615
1616         if (!smb2req) {
1617                 DEBUG(10,("schedule_deferred_open_message_smb2: "
1618                         "can't find mid %llu\n",
1619                         (unsigned long long)mid ));
1620                 return false;
1621         }
1622         if (!smb2req->subreq) {
1623                 return false;
1624         }
1625         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1626                 return false;
1627         }
1628         state = tevent_req_data(smb2req->subreq,
1629                         struct smbd_smb2_create_state);
1630         if (!state) {
1631                 return false;
1632         }
1633
1634         /* Ensure we don't have any outstanding timer event. */
1635         TALLOC_FREE(state->te);
1636         /* Ensure we don't have any outstanding immediate event. */
1637         TALLOC_FREE(state->im);
1638
1639         /*
1640          * This is subtle. We must null out the callback
1641          * before rescheduling, else the first call to
1642          * tevent_req_nterror() causes the _receive()
1643          * function to be called, this causing tevent_req_post()
1644          * to crash.
1645          */
1646         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1647
1648         state->im = tevent_create_immediate(smb2req);
1649         if (!state->im) {
1650                 smbd_server_connection_terminate(smb2req->xconn,
1651                         nt_errstr(NT_STATUS_NO_MEMORY));
1652                 return false;
1653         }
1654
1655         DEBUG(10,("schedule_deferred_open_message_smb2: "
1656                 "re-processing mid %llu\n",
1657                 (unsigned long long)mid ));
1658
1659         tevent_schedule_immediate(state->im,
1660                         smb2req->sconn->ev_ctx,
1661                         smbd_smb2_create_request_dispatch_immediate,
1662                         smb2req);
1663
1664         return true;
1665 }
1666
1667 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1668 {
1669         struct smbd_smb2_request *smb2req = NULL;
1670         struct smbd_smb2_create_state *state = tevent_req_data(req,
1671                                 struct smbd_smb2_create_state);
1672         uint64_t mid;
1673
1674         if (!state) {
1675                 return false;
1676         }
1677
1678         if (!state->smb2req) {
1679                 return false;
1680         }
1681
1682         smb2req = state->smb2req;
1683         mid = get_mid_from_smb2req(smb2req);
1684
1685         if (is_deferred_open_async(state->open_rec)) {
1686                 /* Can't cancel an async create. */
1687                 return false;
1688         }
1689
1690         remove_deferred_open_message_smb2_internal(smb2req, mid);
1691
1692         tevent_req_defer_callback(req, smb2req->sconn->ev_ctx);
1693         tevent_req_nterror(req, NT_STATUS_CANCELLED);
1694         return true;
1695 }
1696
1697 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1698                                 struct timeval request_time,
1699                                 struct timeval timeout,
1700                                 struct file_id id,
1701                                 struct deferred_open_record *open_rec)
1702 {
1703         struct tevent_req *req = NULL;
1704         struct smbd_smb2_create_state *state = NULL;
1705         struct timeval end_time;
1706
1707         if (!smb2req) {
1708                 return false;
1709         }
1710         req = smb2req->subreq;
1711         if (!req) {
1712                 return false;
1713         }
1714         state = tevent_req_data(req, struct smbd_smb2_create_state);
1715         if (!state) {
1716                 return false;
1717         }
1718         state->id = id;
1719         state->request_time = request_time;
1720         state->open_rec = talloc_move(state, &open_rec);
1721
1722         /* Re-schedule us to retry on timer expiry. */
1723         end_time = timeval_sum(&request_time, &timeout);
1724
1725         DEBUG(10,("push_deferred_open_message_smb2: "
1726                 "timeout at %s\n",
1727                 timeval_string(talloc_tos(),
1728                                 &end_time,
1729                                 true) ));
1730
1731         state->open_was_deferred = true;
1732
1733         /* allow this request to be canceled */
1734         tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1735
1736         return true;
1737 }