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