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