Make deferred opens (NT_STATUS_SHARING_VIOLATION) work over SMB2.
[samba.git] / source3 / smbd / smb2_create.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24
25 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
26                         struct tevent_context *ev,
27                         struct smbd_smb2_request *smb2req,
28                         uint8_t in_oplock_level,
29                         uint32_t in_impersonation_level,
30                         uint32_t in_desired_access,
31                         uint32_t in_file_attributes,
32                         uint32_t in_share_access,
33                         uint32_t in_create_disposition,
34                         uint32_t in_create_options,
35                         const char *in_name,
36                         struct smb2_create_blobs in_context_blobs);
37 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
38                         TALLOC_CTX *mem_ctx,
39                         uint8_t *out_oplock_level,
40                         uint32_t *out_create_action,
41                         NTTIME *out_creation_time,
42                         NTTIME *out_last_access_time,
43                         NTTIME *out_last_write_time,
44                         NTTIME *out_change_time,
45                         uint64_t *out_allocation_size,
46                         uint64_t *out_end_of_file,
47                         uint32_t *out_file_attributes,
48                         uint64_t *out_file_id_volatile,
49                         struct smb2_create_blobs *out_context_blobs);
50
51 static void smbd_smb2_request_create_done(struct tevent_req *subreq);
52 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *req)
53 {
54         const uint8_t *inbody;
55         int i = req->current_idx;
56         size_t expected_body_size = 0x39;
57         size_t body_size;
58         uint8_t in_oplock_level;
59         uint32_t in_impersonation_level;
60         uint32_t in_desired_access;
61         uint32_t in_file_attributes;
62         uint32_t in_share_access;
63         uint32_t in_create_disposition;
64         uint32_t in_create_options;
65         uint16_t in_name_offset;
66         uint16_t in_name_length;
67         DATA_BLOB in_name_buffer;
68         char *in_name_string;
69         size_t in_name_string_size;
70         uint32_t name_offset = 0;
71         uint32_t name_available_length = 0;
72         uint32_t in_context_offset;
73         uint32_t in_context_length;
74         DATA_BLOB in_context_buffer;
75         struct smb2_create_blobs in_context_blobs;
76         uint32_t context_offset = 0;
77         uint32_t context_available_length = 0;
78         uint32_t dyn_offset;
79         NTSTATUS status;
80         bool ok;
81         struct tevent_req *subreq;
82
83         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
84                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
85         }
86
87         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
88
89         body_size = SVAL(inbody, 0x00);
90         if (body_size != expected_body_size) {
91                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
92         }
93
94         in_oplock_level         = CVAL(inbody, 0x03);
95         in_impersonation_level  = IVAL(inbody, 0x04);
96         in_desired_access       = IVAL(inbody, 0x18);
97         in_file_attributes      = IVAL(inbody, 0x1C);
98         in_share_access         = IVAL(inbody, 0x20);
99         in_create_disposition   = IVAL(inbody, 0x24);
100         in_create_options       = IVAL(inbody, 0x28);
101         in_name_offset          = SVAL(inbody, 0x2C);
102         in_name_length          = SVAL(inbody, 0x2E);
103         in_context_offset       = IVAL(inbody, 0x30);
104         in_context_length       = IVAL(inbody, 0x34);
105
106         /*
107          * First check if the dynamic name and context buffers
108          * are correctly specified.
109          *
110          * Note: That we don't check if the name and context buffers
111          *       overlap
112          */
113
114         dyn_offset = SMB2_HDR_BODY + (body_size & 0xFFFFFFFE);
115
116         if (in_name_offset == 0 && in_name_length == 0) {
117                 /* This is ok */
118                 name_offset = 0;
119         } else if (in_name_offset < dyn_offset) {
120                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
121         } else {
122                 name_offset = in_name_offset - dyn_offset;
123         }
124
125         if (name_offset > req->in.vector[i+2].iov_len) {
126                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
127         }
128
129         name_available_length = req->in.vector[i+2].iov_len - name_offset;
130
131         if (in_name_length > name_available_length) {
132                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
133         }
134
135         in_name_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base +
136                               name_offset;
137         in_name_buffer.length = in_name_length;
138
139         if (in_context_offset == 0 && in_context_length == 0) {
140                 /* This is ok */
141                 context_offset = 0;
142         } else if (in_context_offset < dyn_offset) {
143                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
144         } else {
145                 context_offset = in_context_offset - dyn_offset;
146         }
147
148         if (context_offset > req->in.vector[i+2].iov_len) {
149                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
150         }
151
152         context_available_length = req->in.vector[i+2].iov_len - context_offset;
153
154         if (in_context_length > context_available_length) {
155                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
156         }
157
158         in_context_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base +
159                                   context_offset;
160         in_context_buffer.length = in_context_length;
161
162         /*
163          * Now interpret the name and context buffers
164          */
165
166         ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
167                                    in_name_buffer.data,
168                                    in_name_buffer.length,
169                                    &in_name_string,
170                                    &in_name_string_size, false);
171         if (!ok) {
172                 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
173         }
174
175         ZERO_STRUCT(in_context_blobs);
176         status = smb2_create_blob_parse(req, in_context_buffer, &in_context_blobs);
177         if (!NT_STATUS_IS_OK(status)) {
178                 return smbd_smb2_request_error(req, status);
179         }
180
181         subreq = smbd_smb2_create_send(req,
182                                        req->sconn->smb2.event_ctx,
183                                        req,
184                                        in_oplock_level,
185                                        in_impersonation_level,
186                                        in_desired_access,
187                                        in_file_attributes,
188                                        in_share_access,
189                                        in_create_disposition,
190                                        in_create_options,
191                                        in_name_string,
192                                        in_context_blobs);
193         if (subreq == NULL) {
194                 req->subreq = NULL;
195                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
196         }
197         tevent_req_set_callback(subreq, smbd_smb2_request_create_done, req);
198
199         return smbd_smb2_request_pending_queue(req, subreq);
200 }
201
202 static void smbd_smb2_request_create_done(struct tevent_req *subreq)
203 {
204         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
205                                         struct smbd_smb2_request);
206         int i = req->current_idx;
207         uint8_t *outhdr;
208         DATA_BLOB outbody;
209         DATA_BLOB outdyn;
210         uint8_t out_oplock_level = 0;
211         uint32_t out_create_action = 0;
212         NTTIME out_creation_time = 0;
213         NTTIME out_last_access_time = 0;
214         NTTIME out_last_write_time = 0;
215         NTTIME out_change_time = 0;
216         uint64_t out_allocation_size = 0;
217         uint64_t out_end_of_file = 0;
218         uint32_t out_file_attributes = 0;
219         uint64_t out_file_id_volatile = 0;
220         struct smb2_create_blobs out_context_blobs;
221         DATA_BLOB out_context_buffer;
222         uint16_t out_context_buffer_offset = 0;
223         NTSTATUS status;
224         NTSTATUS error; /* transport error */
225
226         status = smbd_smb2_create_recv(subreq,
227                                        req,
228                                        &out_oplock_level,
229                                        &out_create_action,
230                                        &out_creation_time,
231                                        &out_last_access_time,
232                                        &out_last_write_time,
233                                        &out_change_time,
234                                        &out_allocation_size,
235                                        &out_end_of_file,
236                                        &out_file_attributes,
237                                        &out_file_id_volatile,
238                                        &out_context_blobs);
239         if (!NT_STATUS_IS_OK(status)) {
240                 error = smbd_smb2_request_error(req, status);
241                 if (!NT_STATUS_IS_OK(error)) {
242                         smbd_server_connection_terminate(req->sconn,
243                                                          nt_errstr(error));
244                         return;
245                 }
246                 return;
247         }
248
249         status = smb2_create_blob_push(req, &out_context_buffer, out_context_blobs);
250         if (!NT_STATUS_IS_OK(status)) {
251                 error = smbd_smb2_request_error(req, status);
252                 if (!NT_STATUS_IS_OK(error)) {
253                         smbd_server_connection_terminate(req->sconn,
254                                                          nt_errstr(error));
255                         return;
256                 }
257                 return;
258         }
259
260         if (out_context_buffer.length > 0) {
261                 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
262         }
263
264         outhdr = (uint8_t *)req->out.vector[i].iov_base;
265
266         outbody = data_blob_talloc(req->out.vector, NULL, 0x58);
267         if (outbody.data == NULL) {
268                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
269                 if (!NT_STATUS_IS_OK(error)) {
270                         smbd_server_connection_terminate(req->sconn,
271                                                          nt_errstr(error));
272                         return;
273                 }
274                 return;
275         }
276
277         SSVAL(outbody.data, 0x00, 0x58 + 1);    /* struct size */
278         SCVAL(outbody.data, 0x02,
279               out_oplock_level);                /* oplock level */
280         SCVAL(outbody.data, 0x03, 0);           /* reserved */
281         SIVAL(outbody.data, 0x04,
282               out_create_action);               /* create action */
283         SBVAL(outbody.data, 0x08,
284               out_creation_time);               /* creation time */
285         SBVAL(outbody.data, 0x10,
286               out_last_access_time);            /* last access time */
287         SBVAL(outbody.data, 0x18,
288               out_last_write_time);             /* last write time */
289         SBVAL(outbody.data, 0x20,
290               out_change_time);                 /* change time */
291         SBVAL(outbody.data, 0x28,
292               out_allocation_size);             /* allocation size */
293         SBVAL(outbody.data, 0x30,
294               out_end_of_file);                 /* end of file */
295         SIVAL(outbody.data, 0x38,
296               out_file_attributes);             /* file attributes */
297         SIVAL(outbody.data, 0x3C, 0);           /* reserved */
298         SBVAL(outbody.data, 0x40, 0);           /* file id (persistent) */
299         SBVAL(outbody.data, 0x48,
300               out_file_id_volatile);            /* file id (volatile) */
301         SIVAL(outbody.data, 0x50,
302               out_context_buffer_offset);       /* create contexts offset */
303         SIVAL(outbody.data, 0x54,
304               out_context_buffer.length);       /* create contexts length */
305
306         outdyn = out_context_buffer;
307
308         error = smbd_smb2_request_done(req, outbody, &outdyn);
309         if (!NT_STATUS_IS_OK(error)) {
310                 smbd_server_connection_terminate(req->sconn,
311                                                  nt_errstr(error));
312                 return;
313         }
314 }
315
316 struct smbd_smb2_create_state {
317         struct smbd_smb2_request *smb2req;
318         struct smb_request *smb1req;
319         struct timed_event *te;
320         struct timeval request_time;
321         DATA_BLOB private_data;
322         uint8_t out_oplock_level;
323         uint32_t out_create_action;
324         NTTIME out_creation_time;
325         NTTIME out_last_access_time;
326         NTTIME out_last_write_time;
327         NTTIME out_change_time;
328         uint64_t out_allocation_size;
329         uint64_t out_end_of_file;
330         uint32_t out_file_attributes;
331         uint64_t out_file_id_volatile;
332         struct smb2_create_blobs out_context_blobs;
333 };
334
335 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
336                         struct tevent_context *ev,
337                         struct smbd_smb2_request *smb2req,
338                         uint8_t in_oplock_level,
339                         uint32_t in_impersonation_level,
340                         uint32_t in_desired_access,
341                         uint32_t in_file_attributes,
342                         uint32_t in_share_access,
343                         uint32_t in_create_disposition,
344                         uint32_t in_create_options,
345                         const char *in_name,
346                         struct smb2_create_blobs in_context_blobs)
347 {
348         struct tevent_req *req = NULL;
349         struct smbd_smb2_create_state *state = NULL;
350         NTSTATUS status;
351         struct smb_request *smb1req = NULL;
352         files_struct *result = NULL;
353         int info;
354         struct timespec write_time_ts;
355         struct smb2_create_blobs out_context_blobs;
356
357         ZERO_STRUCT(out_context_blobs);
358
359         if (!smb2req->async) {
360                 /* New create call. */
361                 req = tevent_req_create(mem_ctx, &state,
362                                 struct smbd_smb2_create_state);
363                 if (req == NULL) {
364                         return NULL;
365                 }
366                 state->smb2req = smb2req;
367                 smb2req->subreq = req; /* So we can find this when going async. */
368
369                 smb1req = smbd_smb2_fake_smb_request(smb2req);
370                 if (tevent_req_nomem(smb1req, req)) {
371                         return tevent_req_post(req, ev);
372                 }
373                 state->smb1req = smb1req;
374                 DEBUG(10,("smbd_smb2_create: name[%s]\n",
375                         in_name));
376         } else {
377                 /* Re-entrant create call. */
378                 req = smb2req->subreq;
379                 state = tevent_req_data(req,
380                                 struct smbd_smb2_create_state);
381                 smb1req = state->smb1req;
382                 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
383                         in_name ));
384         }
385
386         if (IS_IPC(smb1req->conn)) {
387                 const char *pipe_name = in_name;
388
389                 if (!lp_nt_pipe_support()) {
390                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
391                         return tevent_req_post(req, ev);
392                 }
393
394                 /* Strip \\ off the name. */
395                 if (pipe_name[0] == '\\') {
396                         pipe_name++;
397                 }
398
399                 status = open_np_file(smb1req, pipe_name, &result);
400                 if (!NT_STATUS_IS_OK(status)) {
401                         tevent_req_nterror(req, status);
402                         return tevent_req_post(req, ev);
403                 }
404                 info = FILE_WAS_OPENED;
405         } else if (CAN_PRINT(smb1req->conn)) {
406                 status = file_new(smb1req, smb1req->conn, &result);
407                 if(!NT_STATUS_IS_OK(status)) {
408                         tevent_req_nterror(req, status);
409                         return tevent_req_post(req, ev);
410                 }
411
412                 status = print_fsp_open(smb1req,
413                                         smb1req->conn,
414                                         in_name,
415                                         smb1req->vuid,
416                                         result);
417                 if (!NT_STATUS_IS_OK(status)) {
418                         file_free(smb1req, result);
419                         tevent_req_nterror(req, status);
420                         return tevent_req_post(req, ev);
421                 }
422                 info = FILE_WAS_CREATED;
423         } else {
424                 char *fname;
425                 struct smb_filename *smb_fname = NULL;
426                 struct smb2_create_blob *exta = NULL;
427                 struct ea_list *ea_list = NULL;
428                 struct smb2_create_blob *mxac = NULL;
429                 NTTIME max_access_time = 0;
430                 struct smb2_create_blob *secd = NULL;
431                 struct security_descriptor *sec_desc = NULL;
432                 struct smb2_create_blob *dhnq = NULL;
433                 struct smb2_create_blob *dhnc = NULL;
434                 struct smb2_create_blob *alsi = NULL;
435                 uint64_t allocation_size = 0;
436                 struct smb2_create_blob *twrp = NULL;
437                 struct smb2_create_blob *qfid = NULL;
438
439                 exta = smb2_create_blob_find(&in_context_blobs,
440                                              SMB2_CREATE_TAG_EXTA);
441                 mxac = smb2_create_blob_find(&in_context_blobs,
442                                              SMB2_CREATE_TAG_MXAC);
443                 secd = smb2_create_blob_find(&in_context_blobs,
444                                              SMB2_CREATE_TAG_SECD);
445                 dhnq = smb2_create_blob_find(&in_context_blobs,
446                                              SMB2_CREATE_TAG_DHNQ);
447                 dhnc = smb2_create_blob_find(&in_context_blobs,
448                                              SMB2_CREATE_TAG_DHNC);
449                 alsi = smb2_create_blob_find(&in_context_blobs,
450                                              SMB2_CREATE_TAG_ALSI);
451                 twrp = smb2_create_blob_find(&in_context_blobs,
452                                              SMB2_CREATE_TAG_TWRP);
453                 qfid = smb2_create_blob_find(&in_context_blobs,
454                                              SMB2_CREATE_TAG_QFID);
455
456                 fname = talloc_strdup(state, in_name);
457                 if (tevent_req_nomem(fname, req)) {
458                         return tevent_req_post(req, ev);
459                 }
460
461                 if (exta) {
462                         if (dhnc) {
463                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
464                                 return tevent_req_post(req, ev);
465                         }
466
467                         ea_list = read_nttrans_ea_list(mem_ctx,
468                                 (const char *)exta->data.data, exta->data.length);
469                         if (!ea_list) {
470                                 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
471                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
472                                 return tevent_req_post(req, ev);
473                         }
474                 }
475
476                 if (mxac) {
477                         if (dhnc) {
478                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
479                                 return tevent_req_post(req, ev);
480                         }
481
482                         if (mxac->data.length == 0) {
483                                 max_access_time = 0;
484                         } else if (mxac->data.length == 8) {
485                                 max_access_time = BVAL(mxac->data.data, 0);
486                         } else {
487                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
488                                 return tevent_req_post(req, ev);
489                         }
490                 }
491
492                 if (secd) {
493                         enum ndr_err_code ndr_err;
494
495                         if (dhnc) {
496                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
497                                 return tevent_req_post(req, ev);
498                         }
499
500                         sec_desc = talloc_zero(state, struct security_descriptor);
501                         if (tevent_req_nomem(sec_desc, req)) {
502                                 return tevent_req_post(req, ev);
503                         }
504
505                         ndr_err = ndr_pull_struct_blob(&secd->data,
506                                 sec_desc, NULL, sec_desc,
507                                 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
508                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
509                                 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
510                                          ndr_errstr(ndr_err)));
511                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
512                                 return tevent_req_post(req, ev);
513                         }
514                 }
515
516                 if (dhnq) {
517                         if (dhnc) {
518                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
519                                 return tevent_req_post(req, ev);
520                         }
521
522                         if (dhnq->data.length != 16) {
523                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
524                                 return tevent_req_post(req, ev);
525                         }
526                         /*
527                          * we don't support durable handles yet
528                          * and have to ignore this
529                          */
530                 }
531
532                 if (dhnc) {
533                         if (dhnc->data.length != 16) {
534                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
535                                 return tevent_req_post(req, ev);
536                         }
537                         /* we don't support durable handles yet */
538                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
539                         return tevent_req_post(req, ev);
540                 }
541
542                 if (alsi) {
543                         if (dhnc) {
544                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
545                                 return tevent_req_post(req, ev);
546                         }
547
548                         if (alsi->data.length != 8) {
549                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
550                                 return tevent_req_post(req, ev);
551                         }
552                         allocation_size = BVAL(alsi->data.data, 0);
553                 }
554
555                 if (twrp) {
556                         NTTIME nttime;
557                         time_t t;
558                         struct tm *tm;
559
560                         if (dhnc) {
561                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
562                                 return tevent_req_post(req, ev);
563                         }
564
565                         if (twrp->data.length != 8) {
566                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
567                                 return tevent_req_post(req, ev);
568                         }
569
570                         nttime = BVAL(twrp->data.data, 0);
571                         t = nt_time_to_unix(nttime);
572                         tm = gmtime(&t);
573
574                         TALLOC_FREE(fname);
575                         fname = talloc_asprintf(state,
576                                         "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
577                                         tm->tm_year + 1900,
578                                         tm->tm_mon + 1,
579                                         tm->tm_mday,
580                                         tm->tm_hour,
581                                         tm->tm_min,
582                                         tm->tm_sec,
583                                         in_name);
584                         if (tevent_req_nomem(fname, req)) {
585                                 return tevent_req_post(req, ev);
586                         }
587                 }
588
589                 if (qfid) {
590                         if (qfid->data.length != 0) {
591                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
592                                 return tevent_req_post(req, ev);
593                         }
594                 }
595
596                 /* these are ignored for SMB2 */
597                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
598                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
599
600                 /* convert '\\' into '/' */
601                 status = check_path_syntax(fname);
602                 if (!NT_STATUS_IS_OK(status)) {
603                         tevent_req_nterror(req, status);
604                         return tevent_req_post(req, ev);
605                 }
606
607                 status = filename_convert(req,
608                                           smb1req->conn,
609                                           smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
610                                           fname,
611                                           0,
612                                           NULL,
613                                           &smb_fname);
614                 if (!NT_STATUS_IS_OK(status)) {
615                         tevent_req_nterror(req, status);
616                         return tevent_req_post(req, ev);
617                 }
618
619                 status = SMB_VFS_CREATE_FILE(smb1req->conn,
620                                              smb1req,
621                                              0, /* root_dir_fid */
622                                              smb_fname,
623                                              in_desired_access,
624                                              in_share_access,
625                                              in_create_disposition,
626                                              in_create_options,
627                                              in_file_attributes,
628                                              0, /* oplock_request */
629                                              allocation_size,
630                                              0, /* private_flags */
631                                              sec_desc,
632                                              ea_list,
633                                              &result,
634                                              &info);
635                 if (!NT_STATUS_IS_OK(status)) {
636                         if (open_was_deferred(smb1req->mid)) {
637                                 return req;
638                         }
639                         tevent_req_nterror(req, status);
640                         req = tevent_req_post(req, ev);
641                         return req;
642                 }
643
644                 if (mxac) {
645                         NTTIME last_write_time;
646
647                         unix_timespec_to_nt_time(&last_write_time,
648                                                  result->fsp_name->st.st_ex_mtime);
649                         if (last_write_time != max_access_time) {
650                                 uint8_t p[8];
651                                 uint32_t max_access_granted;
652                                 DATA_BLOB blob = data_blob_const(p, sizeof(p));
653
654                                 status = smbd_check_open_rights(smb1req->conn,
655                                                         result->fsp_name,
656                                                         SEC_FLAG_MAXIMUM_ALLOWED,
657                                                         &max_access_granted);
658
659                                 SIVAL(p, 0, NT_STATUS_V(status));
660                                 SIVAL(p, 4, max_access_granted);
661
662                                 status = smb2_create_blob_add(state,
663                                                         &out_context_blobs,
664                                                         SMB2_CREATE_TAG_MXAC,
665                                                         blob);
666                                 if (!NT_STATUS_IS_OK(status)) {
667                                         tevent_req_nterror(req, status);
668                                         return tevent_req_post(req, ev);
669                                 }
670                         }
671                 }
672
673                 if (qfid) {
674                         uint8_t p[32];
675                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
676
677                         ZERO_STRUCT(p);
678
679                         /* TODO: maybe use result->file_id */
680                         SIVAL(p, 0, result->fsp_name->st.st_ex_ino);/* FileIndexLow */
681                         SIVAL(p, 4, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
682
683                         status = smb2_create_blob_add(state, &out_context_blobs,
684                                                       SMB2_CREATE_TAG_QFID,
685                                                       blob);
686                         if (!NT_STATUS_IS_OK(status)) {
687                                 tevent_req_nterror(req, status);
688                                 return tevent_req_post(req, ev);
689                         }
690                 }
691         }
692
693         smb2req->compat_chain_fsp = smb1req->chain_fsp;
694
695         state->out_oplock_level = 0;
696         if ((in_create_disposition == FILE_SUPERSEDE)
697             && (info == FILE_WAS_OVERWRITTEN)) {
698                 state->out_create_action = FILE_WAS_SUPERSEDED;
699         } else {
700                 state->out_create_action = info;
701         }
702         state->out_file_attributes = dos_mode(result->conn,
703                                            result->fsp_name);
704         /* Deal with other possible opens having a modified
705            write time. JRA. */
706         ZERO_STRUCT(write_time_ts);
707         get_file_infos(result->file_id, NULL, &write_time_ts);
708         if (!null_timespec(write_time_ts)) {
709                 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
710         }
711
712         unix_timespec_to_nt_time(&state->out_creation_time,
713                         get_create_timespec(smb1req->conn, result,
714                                         result->fsp_name));
715         unix_timespec_to_nt_time(&state->out_last_access_time,
716                         result->fsp_name->st.st_ex_atime);
717         unix_timespec_to_nt_time(&state->out_last_write_time,
718                         result->fsp_name->st.st_ex_mtime);
719         unix_timespec_to_nt_time(&state->out_change_time,
720                         get_change_timespec(smb1req->conn, result,
721                                         result->fsp_name));
722         state->out_allocation_size =
723                         result->fsp_name->st.st_ex_blksize *
724                         result->fsp_name->st.st_ex_blocks;
725         state->out_end_of_file = result->fsp_name->st.st_ex_size;
726         if (state->out_file_attributes == 0) {
727                 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
728         }
729         state->out_file_id_volatile = result->fnum;
730         state->out_context_blobs = out_context_blobs;
731
732         tevent_req_done(req);
733         return tevent_req_post(req, ev);
734 }
735
736 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
737                         TALLOC_CTX *mem_ctx,
738                         uint8_t *out_oplock_level,
739                         uint32_t *out_create_action,
740                         NTTIME *out_creation_time,
741                         NTTIME *out_last_access_time,
742                         NTTIME *out_last_write_time,
743                         NTTIME *out_change_time,
744                         uint64_t *out_allocation_size,
745                         uint64_t *out_end_of_file,
746                         uint32_t *out_file_attributes,
747                         uint64_t *out_file_id_volatile,
748                         struct smb2_create_blobs *out_context_blobs)
749 {
750         NTSTATUS status;
751         struct smbd_smb2_create_state *state = tevent_req_data(req,
752                                                struct smbd_smb2_create_state);
753
754         if (tevent_req_is_nterror(req, &status)) {
755                 tevent_req_received(req);
756                 return status;
757         }
758
759         *out_oplock_level       = state->out_oplock_level;
760         *out_create_action      = state->out_create_action;
761         *out_creation_time      = state->out_creation_time;
762         *out_last_access_time   = state->out_last_access_time;
763         *out_last_write_time    = state->out_last_write_time;
764         *out_change_time        = state->out_change_time;
765         *out_allocation_size    = state->out_allocation_size;
766         *out_end_of_file        = state->out_end_of_file;
767         *out_file_attributes    = state->out_file_attributes;
768         *out_file_id_volatile   = state->out_file_id_volatile;
769         *out_context_blobs      = state->out_context_blobs;
770
771         talloc_steal(mem_ctx, state->out_context_blobs.blobs);
772
773         tevent_req_received(req);
774         return NT_STATUS_OK;
775 }
776
777 /*********************************************************
778  Code for dealing with deferred opens.
779 *********************************************************/
780
781 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
782                         struct timeval *p_request_time,
783                         void **pp_state)
784 {
785         struct smbd_smb2_create_state *state = NULL;
786         struct tevent_req *req = NULL;
787
788         if (!smb2req) {
789                 return false;
790         }
791         if (!smb2req->async) {
792                 return false;
793         }
794         req = smb2req->subreq;
795         if (!req) {
796                 return false;
797         }
798         state = tevent_req_data(req, struct smbd_smb2_create_state);
799         if (!state) {
800                 return false;
801         }
802         if (p_request_time) {
803                 *p_request_time = state->request_time;
804         }
805         if (pp_state) {
806                 *pp_state = (void *)state->private_data.data;
807         }
808         return true;
809 }
810
811 /*********************************************************
812  Re-process this call early - requested by message or
813  close.
814 *********************************************************/
815
816 static struct smbd_smb2_request *find_open_smb2req(uint64_t mid)
817 {
818         struct smbd_server_connection *sconn = smbd_server_conn;
819         struct smbd_smb2_request *smb2req;
820
821         for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
822                 uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
823                 uint64_t message_id = BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
824                 if (message_id == mid) {
825                         return smb2req;
826                 }
827         }
828         return NULL;
829 }
830
831 bool open_was_deferred_smb2(uint64_t mid)
832 {
833         struct smbd_smb2_create_state *state = NULL;
834         struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
835
836         if (!smb2req) {
837                 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
838                         (unsigned long long)mid));
839                 return false;
840         }
841         if (!smb2req->subreq) {
842                 return false;
843         }
844         if (!tevent_req_is_in_progress(smb2req->subreq)) {
845                 return false;
846         }
847         state = tevent_req_data(smb2req->subreq,
848                         struct smbd_smb2_create_state);
849         if (!state) {
850                 return false;
851         }
852         /* It's not in progress if there's no timeout event. */
853         if (!state->te) {
854                 return false;
855         }
856
857         DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
858                         (unsigned long long)mid));
859         
860         return true;
861 }
862
863 void remove_deferred_open_message_smb2(uint64_t mid)
864 {
865         struct smbd_smb2_create_state *state = NULL;
866         struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
867
868         if (!smb2req) {
869                 DEBUG(10,("remove_deferred_open_message_smb2: "
870                         "can't find mid %llu\n",
871                         (unsigned long long)mid ));
872                 return;
873         }
874         if (!smb2req->subreq) {
875                 return;
876         }
877         if (!tevent_req_is_in_progress(smb2req->subreq)) {
878                 return;
879         }
880         state = tevent_req_data(smb2req->subreq,
881                         struct smbd_smb2_create_state);
882         if (!state) {
883                 return;
884         }
885
886         DEBUG(10,("remove_deferred_open_message_smb2: "
887                 "mid %llu\n",
888                 (unsigned long long)mid ));
889
890         /* Ensure we don't have any outstanding timer event. */
891         TALLOC_FREE(state->te);
892 }
893
894 void schedule_deferred_open_message_smb2(uint64_t mid)
895 {
896         struct tevent_immediate *im = NULL;
897         struct smbd_smb2_create_state *state = NULL;
898         struct smbd_smb2_request *smb2req = find_open_smb2req(mid);
899
900         if (!smb2req) {
901                 DEBUG(10,("schedule_deferred_open_message_smb2: "
902                         "can't find mid %llu\n",
903                         (unsigned long long)mid ));
904                 return;
905         }
906         if (!smb2req->subreq) {
907                 return;
908         }
909         if (!tevent_req_is_in_progress(smb2req->subreq)) {
910                 return;
911         }
912         state = tevent_req_data(smb2req->subreq,
913                         struct smbd_smb2_create_state);
914         if (!state) {
915                 return;
916         }
917         /* Ensure we don't have any outstanding timer event. */
918         TALLOC_FREE(state->te);
919
920         im = tevent_create_immediate(smb2req);
921         if (!im) {
922                 smbd_server_connection_terminate(smb2req->sconn,
923                         nt_errstr(NT_STATUS_NO_MEMORY));
924         }
925
926         DEBUG(10,("schedule_deferred_open_message_smb2: "
927                 "re-processing mid %llu\n",
928                 (unsigned long long)mid ));
929
930         tevent_schedule_immediate(im,
931                         smb2req->sconn->smb2.event_ctx,
932                         smbd_smb2_request_dispatch_immediate,
933                         smb2req);
934 }
935
936 /*********************************************************
937  Re-process this call.
938 *********************************************************/
939
940 static void smb2_deferred_open_timer(struct event_context *ev,
941                                         struct timed_event *te,
942                                         struct timeval _tval,
943                                         void *private_data)
944 {
945         NTSTATUS status;
946         struct smbd_smb2_create_state *state = NULL;
947         struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
948                                                 struct smbd_smb2_request);
949
950         DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
951                 smb2req->current_idx,
952                 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
953
954         state = tevent_req_data(smb2req->subreq,
955                         struct smbd_smb2_create_state);
956         if (!state) {
957                 return;
958         }
959         /*
960          * Null this out, don't talloc_free. It will
961          * be talloc_free'd by the tevent library when
962          * this returns.
963          */
964         state->te = NULL;
965
966         /*
967          * This is subtle. We must null out the callback
968          * before resheduling, else the first call to
969          * tevent_req_nterror() causes the _receive()
970          * function to be called, this causing tevent_req_post()
971          * to crash.
972          */
973         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
974
975         status = smbd_smb2_request_dispatch(smb2req);
976
977         if (!NT_STATUS_IS_OK(status)) {
978                 smbd_server_connection_terminate(smb2req->sconn,
979                                 nt_errstr(status));
980         }
981 }
982
983 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
984                                 struct timeval request_time,
985                                 struct timeval timeout,
986                                 char *private_data,
987                                 size_t priv_len)
988 {
989         struct tevent_req *req = NULL;
990         struct smbd_smb2_create_state *state = NULL;
991         struct timeval end_time;
992
993         if (!smb2req) {
994                 return false;
995         }
996         req = smb2req->subreq;
997         if (!req) {
998                 return false;
999         }
1000         state = tevent_req_data(req, struct smbd_smb2_create_state);
1001         if (!state) {
1002                 return false;
1003         }
1004         state->request_time = request_time;
1005         state->private_data = data_blob_talloc(state, private_data,
1006                                                 priv_len);
1007         if (!state->private_data.data) {
1008                 return false;
1009         }
1010 #if 0
1011         /* Boo - turns out this isn't what W2K8R2
1012            does. It actually sends the STATUS_PENDING
1013            message followed by the STATUS_SHARING_VIOLATION
1014            message. Surely this means that all open
1015            calls (even on directories) will potentially
1016            fail in a chain.... ? And I've seen directory
1017            opens as the start of a chain. JRA.
1018         */
1019         /*
1020          * More subtlety. To match W2K8R2 don't
1021          * send a "gone async" message if it's simply
1022          * a STATUS_SHARING_VIOLATION (short) wait, not
1023          * an oplock break wait. We do this by prematurely
1024          * setting smb2req->async flag.
1025          */
1026         if (timeout.tv_sec < 2) {
1027                 DEBUG(10,("push_deferred_open_message_smb2: "
1028                         "short timer wait (usec = %u). "
1029                         "Don't send async message.\n",
1030                         (unsigned int)timeout.tv_usec ));
1031                 smb2req->async = true;
1032         }
1033 #endif
1034
1035         /* Re-schedule us to retry on timer expiry. */
1036         end_time = timeval_sum(&request_time, &timeout);
1037
1038         DEBUG(10,("push_deferred_open_message_smb2: "
1039                 "timeout at %s\n",
1040                 timeval_string(talloc_tos(),
1041                                 &end_time,
1042                                 true) ));
1043
1044         state->te = event_add_timed(smb2req->sconn->smb2.event_ctx,
1045                                 state,
1046                                 end_time,
1047                                 smb2_deferred_open_timer,
1048                                 smb2req);
1049         if (!state->te) {
1050                 return false;
1051         }
1052         return true;
1053 }