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