c1cfa0cef3e60fd896362e3e2319024e52de25db
[metze/samba/wip.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 "../lib/util/tevent_ntstatus.h"
29 #include "messages.h"
30
31 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
32 {
33         switch(in_oplock_level) {
34         case SMB2_OPLOCK_LEVEL_NONE:
35                 return NO_OPLOCK;
36         case SMB2_OPLOCK_LEVEL_II:
37                 return LEVEL_II_OPLOCK;
38         case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
39                 return EXCLUSIVE_OPLOCK;
40         case SMB2_OPLOCK_LEVEL_BATCH:
41                 return BATCH_OPLOCK;
42         case SMB2_OPLOCK_LEVEL_LEASE:
43                 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
44                         "LEASE_OPLOCK_REQUESTED\n"));
45                 return NO_OPLOCK;
46         default:
47                 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
48                         "unknown level %u\n",
49                         (unsigned int)in_oplock_level));
50                 return NO_OPLOCK;
51         }
52 }
53
54 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
55 {
56         if (BATCH_OPLOCK_TYPE(oplock_type)) {
57                 return SMB2_OPLOCK_LEVEL_BATCH;
58         } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
59                 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
60         } else if (oplock_type == LEVEL_II_OPLOCK) {
61                 /*
62                  * Don't use LEVEL_II_OPLOCK_TYPE here as
63                  * this also includes FAKE_LEVEL_II_OPLOCKs
64                  * which are internal only.
65                  */
66                 return SMB2_OPLOCK_LEVEL_II;
67         } else {
68                 return SMB2_OPLOCK_LEVEL_NONE;
69         }
70 }
71
72 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
73                         struct tevent_context *ev,
74                         struct smbd_smb2_request *smb2req,
75                         uint8_t in_oplock_level,
76                         uint32_t in_impersonation_level,
77                         uint32_t in_desired_access,
78                         uint32_t in_file_attributes,
79                         uint32_t in_share_access,
80                         uint32_t in_create_disposition,
81                         uint32_t in_create_options,
82                         const char *in_name,
83                         struct smb2_create_blobs in_context_blobs);
84 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
85                         TALLOC_CTX *mem_ctx,
86                         uint8_t *out_oplock_level,
87                         uint32_t *out_create_action,
88                         NTTIME *out_creation_time,
89                         NTTIME *out_last_access_time,
90                         NTTIME *out_last_write_time,
91                         NTTIME *out_change_time,
92                         uint64_t *out_allocation_size,
93                         uint64_t *out_end_of_file,
94                         uint32_t *out_file_attributes,
95                         uint64_t *out_file_id_persistent,
96                         uint64_t *out_file_id_volatile,
97                         struct smb2_create_blobs *out_context_blobs);
98
99 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
100 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
101 {
102         const uint8_t *inbody;
103         const struct iovec *indyniov;
104         uint8_t in_oplock_level;
105         uint32_t in_impersonation_level;
106         uint32_t in_desired_access;
107         uint32_t in_file_attributes;
108         uint32_t in_share_access;
109         uint32_t in_create_disposition;
110         uint32_t in_create_options;
111         uint16_t in_name_offset;
112         uint16_t in_name_length;
113         DATA_BLOB in_name_buffer;
114         char *in_name_string;
115         size_t in_name_string_size;
116         uint32_t name_offset = 0;
117         uint32_t name_available_length = 0;
118         uint32_t in_context_offset;
119         uint32_t in_context_length;
120         DATA_BLOB in_context_buffer;
121         struct smb2_create_blobs in_context_blobs;
122         uint32_t context_offset = 0;
123         uint32_t context_available_length = 0;
124         uint32_t dyn_offset;
125         NTSTATUS status;
126         bool ok;
127         struct tevent_req *tsubreq;
128
129         status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
130         if (!NT_STATUS_IS_OK(status)) {
131                 return smbd_smb2_request_error(smb2req, status);
132         }
133         inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
134
135         in_oplock_level         = CVAL(inbody, 0x03);
136         in_impersonation_level  = IVAL(inbody, 0x04);
137         in_desired_access       = IVAL(inbody, 0x18);
138         in_file_attributes      = IVAL(inbody, 0x1C);
139         in_share_access         = IVAL(inbody, 0x20);
140         in_create_disposition   = IVAL(inbody, 0x24);
141         in_create_options       = IVAL(inbody, 0x28);
142         in_name_offset          = SVAL(inbody, 0x2C);
143         in_name_length          = SVAL(inbody, 0x2E);
144         in_context_offset       = IVAL(inbody, 0x30);
145         in_context_length       = IVAL(inbody, 0x34);
146
147         /*
148          * First check if the dynamic name and context buffers
149          * are correctly specified.
150          *
151          * Note: That we don't check if the name and context buffers
152          *       overlap
153          */
154
155         dyn_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req);
156
157         if (in_name_offset == 0 && in_name_length == 0) {
158                 /* This is ok */
159                 name_offset = 0;
160         } else if (in_name_offset < dyn_offset) {
161                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
162         } else {
163                 name_offset = in_name_offset - dyn_offset;
164         }
165
166         indyniov = SMBD_SMB2_IN_DYN_IOV(smb2req);
167
168         if (name_offset > indyniov->iov_len) {
169                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
170         }
171
172         name_available_length = indyniov->iov_len - name_offset;
173
174         if (in_name_length > name_available_length) {
175                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
176         }
177
178         in_name_buffer.data = (uint8_t *)indyniov->iov_base + name_offset;
179         in_name_buffer.length = in_name_length;
180
181         if (in_context_offset == 0 && in_context_length == 0) {
182                 /* This is ok */
183                 context_offset = 0;
184         } else if (in_context_offset < dyn_offset) {
185                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
186         } else {
187                 context_offset = in_context_offset - dyn_offset;
188         }
189
190         if (context_offset > indyniov->iov_len) {
191                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
192         }
193
194         context_available_length = indyniov->iov_len - context_offset;
195
196         if (in_context_length > context_available_length) {
197                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
198         }
199
200         in_context_buffer.data = (uint8_t *)indyniov->iov_base +
201                 context_offset;
202         in_context_buffer.length = in_context_length;
203
204         /*
205          * Now interpret the name and context buffers
206          */
207
208         ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
209                                    in_name_buffer.data,
210                                    in_name_buffer.length,
211                                    &in_name_string,
212                                    &in_name_string_size);
213         if (!ok) {
214                 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
215         }
216
217         if (in_name_buffer.length == 0) {
218                 in_name_string_size = 0;
219         }
220
221         if (strlen(in_name_string) != in_name_string_size) {
222                 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
223         }
224
225         ZERO_STRUCT(in_context_blobs);
226         status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
227         if (!NT_STATUS_IS_OK(status)) {
228                 return smbd_smb2_request_error(smb2req, status);
229         }
230
231         tsubreq = smbd_smb2_create_send(smb2req,
232                                        smb2req->sconn->ev_ctx,
233                                        smb2req,
234                                        in_oplock_level,
235                                        in_impersonation_level,
236                                        in_desired_access,
237                                        in_file_attributes,
238                                        in_share_access,
239                                        in_create_disposition,
240                                        in_create_options,
241                                        in_name_string,
242                                        in_context_blobs);
243         if (tsubreq == NULL) {
244                 smb2req->subreq = NULL;
245                 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
246         }
247         tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
248
249         /*
250          * For now we keep the logic that we do not send STATUS_PENDING
251          * for sharing violations, so we just wait 2 seconds.
252          *
253          * TODO: we need more tests for this.
254          */
255         return smbd_smb2_request_pending_queue(smb2req, tsubreq, 2000000);
256 }
257
258 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
259 {
260         uint8_t *reqhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
261         return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
262 }
263
264 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
265 {
266         struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
267                                         struct smbd_smb2_request);
268         DATA_BLOB outbody;
269         DATA_BLOB outdyn;
270         uint8_t out_oplock_level = 0;
271         uint32_t out_create_action = 0;
272         NTTIME out_creation_time = 0;
273         NTTIME out_last_access_time = 0;
274         NTTIME out_last_write_time = 0;
275         NTTIME out_change_time = 0;
276         uint64_t out_allocation_size = 0;
277         uint64_t out_end_of_file = 0;
278         uint32_t out_file_attributes = 0;
279         uint64_t out_file_id_persistent = 0;
280         uint64_t out_file_id_volatile = 0;
281         struct smb2_create_blobs out_context_blobs;
282         DATA_BLOB out_context_buffer;
283         uint16_t out_context_buffer_offset = 0;
284         NTSTATUS status;
285         NTSTATUS error; /* transport error */
286
287         if (smb2req->cancelled) {
288                 uint64_t mid = get_mid_from_smb2req(smb2req);
289                 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
290                         (unsigned long long)mid ));
291                 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
292                 if (!NT_STATUS_IS_OK(error)) {
293                         smbd_server_connection_terminate(smb2req->sconn,
294                                 nt_errstr(error));
295                         return;
296                 }
297                 return;
298         }
299
300         status = smbd_smb2_create_recv(tsubreq,
301                                        smb2req,
302                                        &out_oplock_level,
303                                        &out_create_action,
304                                        &out_creation_time,
305                                        &out_last_access_time,
306                                        &out_last_write_time,
307                                        &out_change_time,
308                                        &out_allocation_size,
309                                        &out_end_of_file,
310                                        &out_file_attributes,
311                                        &out_file_id_persistent,
312                                        &out_file_id_volatile,
313                                        &out_context_blobs);
314         if (!NT_STATUS_IS_OK(status)) {
315                 error = smbd_smb2_request_error(smb2req, status);
316                 if (!NT_STATUS_IS_OK(error)) {
317                         smbd_server_connection_terminate(smb2req->sconn,
318                                                          nt_errstr(error));
319                         return;
320                 }
321                 return;
322         }
323
324         status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
325         if (!NT_STATUS_IS_OK(status)) {
326                 error = smbd_smb2_request_error(smb2req, status);
327                 if (!NT_STATUS_IS_OK(error)) {
328                         smbd_server_connection_terminate(smb2req->sconn,
329                                                          nt_errstr(error));
330                         return;
331                 }
332                 return;
333         }
334
335         if (out_context_buffer.length > 0) {
336                 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
337         }
338
339         outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
340         if (outbody.data == NULL) {
341                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
342                 if (!NT_STATUS_IS_OK(error)) {
343                         smbd_server_connection_terminate(smb2req->sconn,
344                                                          nt_errstr(error));
345                         return;
346                 }
347                 return;
348         }
349
350         SSVAL(outbody.data, 0x00, 0x58 + 1);    /* struct size */
351         SCVAL(outbody.data, 0x02,
352               out_oplock_level);                /* oplock level */
353         SCVAL(outbody.data, 0x03, 0);           /* reserved */
354         SIVAL(outbody.data, 0x04,
355               out_create_action);               /* create action */
356         SBVAL(outbody.data, 0x08,
357               out_creation_time);               /* creation time */
358         SBVAL(outbody.data, 0x10,
359               out_last_access_time);            /* last access time */
360         SBVAL(outbody.data, 0x18,
361               out_last_write_time);             /* last write time */
362         SBVAL(outbody.data, 0x20,
363               out_change_time);                 /* change time */
364         SBVAL(outbody.data, 0x28,
365               out_allocation_size);             /* allocation size */
366         SBVAL(outbody.data, 0x30,
367               out_end_of_file);                 /* end of file */
368         SIVAL(outbody.data, 0x38,
369               out_file_attributes);             /* file attributes */
370         SIVAL(outbody.data, 0x3C, 0);           /* reserved */
371         SBVAL(outbody.data, 0x40,
372               out_file_id_persistent);          /* file id (persistent) */
373         SBVAL(outbody.data, 0x48,
374               out_file_id_volatile);            /* file id (volatile) */
375         SIVAL(outbody.data, 0x50,
376               out_context_buffer_offset);       /* create contexts offset */
377         SIVAL(outbody.data, 0x54,
378               out_context_buffer.length);       /* create contexts length */
379
380         outdyn = out_context_buffer;
381
382         error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
383         if (!NT_STATUS_IS_OK(error)) {
384                 smbd_server_connection_terminate(smb2req->sconn,
385                                                  nt_errstr(error));
386                 return;
387         }
388 }
389
390 struct smbd_smb2_create_state {
391         struct smbd_smb2_request *smb2req;
392         struct smb_request *smb1req;
393         struct timed_event *te;
394         struct tevent_immediate *im;
395         struct timeval request_time;
396         struct file_id id;
397         DATA_BLOB private_data;
398         uint8_t out_oplock_level;
399         uint32_t out_create_action;
400         NTTIME out_creation_time;
401         NTTIME out_last_access_time;
402         NTTIME out_last_write_time;
403         NTTIME out_change_time;
404         uint64_t out_allocation_size;
405         uint64_t out_end_of_file;
406         uint32_t out_file_attributes;
407         uint64_t out_file_id_persistent;
408         uint64_t out_file_id_volatile;
409         struct smb2_create_blobs out_context_blobs;
410 };
411
412 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
413                         struct tevent_context *ev,
414                         struct smbd_smb2_request *smb2req,
415                         uint8_t in_oplock_level,
416                         uint32_t in_impersonation_level,
417                         uint32_t in_desired_access,
418                         uint32_t in_file_attributes,
419                         uint32_t in_share_access,
420                         uint32_t in_create_disposition,
421                         uint32_t in_create_options,
422                         const char *in_name,
423                         struct smb2_create_blobs in_context_blobs)
424 {
425         struct tevent_req *req = NULL;
426         struct smbd_smb2_create_state *state = NULL;
427         NTSTATUS status;
428         struct smb_request *smb1req = NULL;
429         files_struct *result = NULL;
430         int info;
431         struct timespec write_time_ts;
432         struct smb2_create_blobs out_context_blobs;
433         int requested_oplock_level;
434
435         ZERO_STRUCT(out_context_blobs);
436
437         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
438                 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
439         } else {
440                 requested_oplock_level = in_oplock_level;
441         }
442
443
444         if (smb2req->subreq == NULL) {
445                 /* New create call. */
446                 req = tevent_req_create(mem_ctx, &state,
447                                 struct smbd_smb2_create_state);
448                 if (req == NULL) {
449                         return NULL;
450                 }
451                 state->smb2req = smb2req;
452
453                 smb1req = smbd_smb2_fake_smb_request(smb2req);
454                 if (tevent_req_nomem(smb1req, req)) {
455                         return tevent_req_post(req, ev);
456                 }
457                 state->smb1req = smb1req;
458                 smb2req->subreq = req;
459                 DEBUG(10,("smbd_smb2_create: name[%s]\n",
460                         in_name));
461         } else {
462                 /* Re-entrant create call. */
463                 req = smb2req->subreq;
464                 state = tevent_req_data(req,
465                                 struct smbd_smb2_create_state);
466                 smb1req = state->smb1req;
467                 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
468                         in_name ));
469         }
470
471         if (IS_IPC(smb1req->conn)) {
472                 const char *pipe_name = in_name;
473
474                 if (!lp_nt_pipe_support()) {
475                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
476                         return tevent_req_post(req, ev);
477                 }
478
479                 status = open_np_file(smb1req, pipe_name, &result);
480                 if (!NT_STATUS_IS_OK(status)) {
481                         tevent_req_nterror(req, status);
482                         return tevent_req_post(req, ev);
483                 }
484                 info = FILE_WAS_OPENED;
485         } else if (CAN_PRINT(smb1req->conn)) {
486                 status = file_new(smb1req, smb1req->conn, &result);
487                 if(!NT_STATUS_IS_OK(status)) {
488                         tevent_req_nterror(req, status);
489                         return tevent_req_post(req, ev);
490                 }
491
492                 status = print_spool_open(result, in_name,
493                                           smb1req->vuid);
494                 if (!NT_STATUS_IS_OK(status)) {
495                         file_free(smb1req, result);
496                         tevent_req_nterror(req, status);
497                         return tevent_req_post(req, ev);
498                 }
499                 info = FILE_WAS_CREATED;
500         } else {
501                 char *fname;
502                 struct smb_filename *smb_fname = NULL;
503                 struct smb2_create_blob *exta = NULL;
504                 struct ea_list *ea_list = NULL;
505                 struct smb2_create_blob *mxac = NULL;
506                 NTTIME max_access_time = 0;
507                 struct smb2_create_blob *secd = NULL;
508                 struct security_descriptor *sec_desc = NULL;
509                 struct smb2_create_blob *dhnq = NULL;
510                 struct smb2_create_blob *dhnc = NULL;
511                 struct smb2_create_blob *alsi = NULL;
512                 uint64_t allocation_size = 0;
513                 struct smb2_create_blob *twrp = NULL;
514                 struct smb2_create_blob *qfid = NULL;
515
516                 exta = smb2_create_blob_find(&in_context_blobs,
517                                              SMB2_CREATE_TAG_EXTA);
518                 mxac = smb2_create_blob_find(&in_context_blobs,
519                                              SMB2_CREATE_TAG_MXAC);
520                 secd = smb2_create_blob_find(&in_context_blobs,
521                                              SMB2_CREATE_TAG_SECD);
522                 dhnq = smb2_create_blob_find(&in_context_blobs,
523                                              SMB2_CREATE_TAG_DHNQ);
524                 dhnc = smb2_create_blob_find(&in_context_blobs,
525                                              SMB2_CREATE_TAG_DHNC);
526                 alsi = smb2_create_blob_find(&in_context_blobs,
527                                              SMB2_CREATE_TAG_ALSI);
528                 twrp = smb2_create_blob_find(&in_context_blobs,
529                                              SMB2_CREATE_TAG_TWRP);
530                 qfid = smb2_create_blob_find(&in_context_blobs,
531                                              SMB2_CREATE_TAG_QFID);
532
533                 fname = talloc_strdup(state, in_name);
534                 if (tevent_req_nomem(fname, req)) {
535                         return tevent_req_post(req, ev);
536                 }
537
538                 if (exta) {
539                         if (dhnc) {
540                                 tevent_req_nterror(req,NT_STATUS_OBJECT_NAME_NOT_FOUND);
541                                 return tevent_req_post(req, ev);
542                         }
543
544                         ea_list = read_nttrans_ea_list(mem_ctx,
545                                 (const char *)exta->data.data, exta->data.length);
546                         if (!ea_list) {
547                                 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
548                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
549                                 return tevent_req_post(req, ev);
550                         }
551                 }
552
553                 if (mxac) {
554                         if (dhnc) {
555                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
556                                 return tevent_req_post(req, ev);
557                         }
558
559                         if (mxac->data.length == 0) {
560                                 max_access_time = 0;
561                         } else if (mxac->data.length == 8) {
562                                 max_access_time = BVAL(mxac->data.data, 0);
563                         } else {
564                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
565                                 return tevent_req_post(req, ev);
566                         }
567                 }
568
569                 if (secd) {
570                         enum ndr_err_code ndr_err;
571
572                         if (dhnc) {
573                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
574                                 return tevent_req_post(req, ev);
575                         }
576
577                         sec_desc = talloc_zero(state, struct security_descriptor);
578                         if (tevent_req_nomem(sec_desc, req)) {
579                                 return tevent_req_post(req, ev);
580                         }
581
582                         ndr_err = ndr_pull_struct_blob(&secd->data,
583                                 sec_desc, sec_desc,
584                                 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
585                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
586                                 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
587                                          ndr_errstr(ndr_err)));
588                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
589                                 return tevent_req_post(req, ev);
590                         }
591                 }
592
593                 if (dhnq) {
594                         if (dhnc) {
595                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
596                                 return tevent_req_post(req, ev);
597                         }
598
599                         if (dhnq->data.length != 16) {
600                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
601                                 return tevent_req_post(req, ev);
602                         }
603                         /*
604                          * we don't support durable handles yet
605                          * and have to ignore this
606                          */
607                 }
608
609                 if (dhnc) {
610                         if (dhnc->data.length != 16) {
611                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
612                                 return tevent_req_post(req, ev);
613                         }
614                         /* we don't support durable handles yet */
615                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
616                         return tevent_req_post(req, ev);
617                 }
618
619                 if (alsi) {
620                         if (dhnc) {
621                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
622                                 return tevent_req_post(req, ev);
623                         }
624
625                         if (alsi->data.length != 8) {
626                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
627                                 return tevent_req_post(req, ev);
628                         }
629                         allocation_size = BVAL(alsi->data.data, 0);
630                 }
631
632                 if (twrp) {
633                         NTTIME nttime;
634                         time_t t;
635                         struct tm *tm;
636
637                         if (dhnc) {
638                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
639                                 return tevent_req_post(req, ev);
640                         }
641
642                         if (twrp->data.length != 8) {
643                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
644                                 return tevent_req_post(req, ev);
645                         }
646
647                         nttime = BVAL(twrp->data.data, 0);
648                         t = nt_time_to_unix(nttime);
649                         tm = gmtime(&t);
650
651                         TALLOC_FREE(fname);
652                         fname = talloc_asprintf(state,
653                                         "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
654                                         tm->tm_year + 1900,
655                                         tm->tm_mon + 1,
656                                         tm->tm_mday,
657                                         tm->tm_hour,
658                                         tm->tm_min,
659                                         tm->tm_sec,
660                                         in_name);
661                         if (tevent_req_nomem(fname, req)) {
662                                 return tevent_req_post(req, ev);
663                         }
664                 }
665
666                 if (qfid) {
667                         if (qfid->data.length != 0) {
668                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
669                                 return tevent_req_post(req, ev);
670                         }
671                 }
672
673                 /* these are ignored for SMB2 */
674                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
675                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
676
677                 /*
678                  * For a DFS path the function parse_dfs_path()
679                  * will do the path processing.
680                  */
681
682                 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
683                         /* convert '\\' into '/' */
684                         status = check_path_syntax(fname);
685                         if (!NT_STATUS_IS_OK(status)) {
686                                 tevent_req_nterror(req, status);
687                                 return tevent_req_post(req, ev);
688                         }
689                 }
690
691                 status = filename_convert(req,
692                                           smb1req->conn,
693                                           smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
694                                           fname,
695                                           0,    /* unix_convert flags */
696                                           NULL, /* ppath_contains_wcards */
697                                           &smb_fname);
698                 if (!NT_STATUS_IS_OK(status)) {
699                         tevent_req_nterror(req, status);
700                         return tevent_req_post(req, ev);
701                 }
702
703                 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
704
705                 status = SMB_VFS_CREATE_FILE(smb1req->conn,
706                                              smb1req,
707                                              0, /* root_dir_fid */
708                                              smb_fname,
709                                              in_desired_access,
710                                              in_share_access,
711                                              in_create_disposition,
712                                              in_create_options,
713                                              in_file_attributes,
714                                              map_smb2_oplock_levels_to_samba(requested_oplock_level),
715                                              allocation_size,
716                                              0, /* private_flags */
717                                              sec_desc,
718                                              ea_list,
719                                              &result,
720                                              &info);
721                 if (!NT_STATUS_IS_OK(status)) {
722                         if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
723                                 return req;
724                         }
725                         tevent_req_nterror(req, status);
726                         return tevent_req_post(req, ev);
727                 }
728
729                 if (mxac) {
730                         NTTIME last_write_time;
731
732                         unix_timespec_to_nt_time(&last_write_time,
733                                                  result->fsp_name->st.st_ex_mtime);
734                         if (last_write_time != max_access_time) {
735                                 uint8_t p[8];
736                                 uint32_t max_access_granted;
737                                 DATA_BLOB blob = data_blob_const(p, sizeof(p));
738
739                                 status = smbd_calculate_access_mask(smb1req->conn,
740                                                         result->fsp_name,
741                                                         SEC_FLAG_MAXIMUM_ALLOWED,
742                                                         &max_access_granted);
743
744                                 SIVAL(p, 0, NT_STATUS_V(status));
745                                 SIVAL(p, 4, max_access_granted);
746
747                                 status = smb2_create_blob_add(state,
748                                                         &out_context_blobs,
749                                                         SMB2_CREATE_TAG_MXAC,
750                                                         blob);
751                                 if (!NT_STATUS_IS_OK(status)) {
752                                         tevent_req_nterror(req, status);
753                                         return tevent_req_post(req, ev);
754                                 }
755                         }
756                 }
757
758                 if (qfid) {
759                         uint8_t p[32];
760                         uint64_t file_index = get_FileIndex(result->conn,
761                                                         &result->fsp_name->st);
762                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
763
764                         ZERO_STRUCT(p);
765
766                         /* From conversations with Microsoft engineers at
767                            the MS plugfest. The first 8 bytes are the "volume index"
768                            == inode, the second 8 bytes are the "volume id",
769                            == dev. This will be updated in the SMB2 doc. */
770                         SBVAL(p, 0, file_index);
771                         SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
772
773                         status = smb2_create_blob_add(state, &out_context_blobs,
774                                                       SMB2_CREATE_TAG_QFID,
775                                                       blob);
776                         if (!NT_STATUS_IS_OK(status)) {
777                                 tevent_req_nterror(req, status);
778                                 return tevent_req_post(req, ev);
779                         }
780                 }
781         }
782
783         smb2req->compat_chain_fsp = smb1req->chain_fsp;
784
785         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
786                 state->out_oplock_level = in_oplock_level;
787         } else {
788                 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
789         }
790
791         if ((in_create_disposition == FILE_SUPERSEDE)
792             && (info == FILE_WAS_OVERWRITTEN)) {
793                 state->out_create_action = FILE_WAS_SUPERSEDED;
794         } else {
795                 state->out_create_action = info;
796         }
797         state->out_file_attributes = dos_mode(result->conn,
798                                            result->fsp_name);
799         /* Deal with other possible opens having a modified
800            write time. JRA. */
801         ZERO_STRUCT(write_time_ts);
802         get_file_infos(result->file_id, 0, NULL, &write_time_ts);
803         if (!null_timespec(write_time_ts)) {
804                 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
805         }
806
807         unix_timespec_to_nt_time(&state->out_creation_time,
808                         get_create_timespec(smb1req->conn, result,
809                                         result->fsp_name));
810         unix_timespec_to_nt_time(&state->out_last_access_time,
811                         result->fsp_name->st.st_ex_atime);
812         unix_timespec_to_nt_time(&state->out_last_write_time,
813                         result->fsp_name->st.st_ex_mtime);
814         unix_timespec_to_nt_time(&state->out_change_time,
815                         get_change_timespec(smb1req->conn, result,
816                                         result->fsp_name));
817         state->out_allocation_size =
818                         SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
819                                                &(result->fsp_name->st));
820         state->out_end_of_file = result->fsp_name->st.st_ex_size;
821         if (state->out_file_attributes == 0) {
822                 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
823         }
824         state->out_file_id_persistent = result->op->global->open_persistent_id;
825         state->out_file_id_volatile = result->op->global->open_volatile_id;
826         state->out_context_blobs = out_context_blobs;
827
828         DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
829                   fsp_str_dbg(result), fsp_fnum_dbg(result)));
830
831         tevent_req_done(req);
832         return tevent_req_post(req, ev);
833 }
834
835 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
836                         TALLOC_CTX *mem_ctx,
837                         uint8_t *out_oplock_level,
838                         uint32_t *out_create_action,
839                         NTTIME *out_creation_time,
840                         NTTIME *out_last_access_time,
841                         NTTIME *out_last_write_time,
842                         NTTIME *out_change_time,
843                         uint64_t *out_allocation_size,
844                         uint64_t *out_end_of_file,
845                         uint32_t *out_file_attributes,
846                         uint64_t *out_file_id_persistent,
847                         uint64_t *out_file_id_volatile,
848                         struct smb2_create_blobs *out_context_blobs)
849 {
850         NTSTATUS status;
851         struct smbd_smb2_create_state *state = tevent_req_data(req,
852                                                struct smbd_smb2_create_state);
853
854         if (tevent_req_is_nterror(req, &status)) {
855                 tevent_req_received(req);
856                 return status;
857         }
858
859         *out_oplock_level       = state->out_oplock_level;
860         *out_create_action      = state->out_create_action;
861         *out_creation_time      = state->out_creation_time;
862         *out_last_access_time   = state->out_last_access_time;
863         *out_last_write_time    = state->out_last_write_time;
864         *out_change_time        = state->out_change_time;
865         *out_allocation_size    = state->out_allocation_size;
866         *out_end_of_file        = state->out_end_of_file;
867         *out_file_attributes    = state->out_file_attributes;
868         *out_file_id_persistent = state->out_file_id_persistent;
869         *out_file_id_volatile   = state->out_file_id_volatile;
870         *out_context_blobs      = state->out_context_blobs;
871
872         talloc_steal(mem_ctx, state->out_context_blobs.blobs);
873
874         tevent_req_received(req);
875         return NT_STATUS_OK;
876 }
877
878 /*********************************************************
879  Code for dealing with deferred opens.
880 *********************************************************/
881
882 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
883                         struct timeval *p_request_time,
884                         void **pp_state)
885 {
886         struct smbd_smb2_create_state *state = NULL;
887         struct tevent_req *req = NULL;
888
889         if (!smb2req) {
890                 return false;
891         }
892         if (smb2req->async_te == NULL) {
893                 return false;
894         }
895         req = smb2req->subreq;
896         if (!req) {
897                 return false;
898         }
899         state = tevent_req_data(req, struct smbd_smb2_create_state);
900         if (!state) {
901                 return false;
902         }
903         if (p_request_time) {
904                 *p_request_time = state->request_time;
905         }
906         if (pp_state) {
907                 *pp_state = (void *)state->private_data.data;
908         }
909         return true;
910 }
911
912 /*********************************************************
913  Re-process this call early - requested by message or
914  close.
915 *********************************************************/
916
917 static struct smbd_smb2_request *find_open_smb2req(
918         struct smbd_server_connection *sconn, uint64_t mid)
919 {
920         struct smbd_smb2_request *smb2req;
921
922         for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
923                 uint64_t message_id;
924                 if (smb2req->subreq == NULL) {
925                         /* This message has been processed. */
926                         continue;
927                 }
928                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
929                         /* This message has been processed. */
930                         continue;
931                 }
932                 message_id = get_mid_from_smb2req(smb2req);
933                 if (message_id == mid) {
934                         return smb2req;
935                 }
936         }
937         return NULL;
938 }
939
940 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
941 {
942         struct smbd_smb2_create_state *state = NULL;
943         struct smbd_smb2_request *smb2req;
944
945         smb2req = find_open_smb2req(sconn, mid);
946
947         if (!smb2req) {
948                 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
949                         (unsigned long long)mid));
950                 return false;
951         }
952         if (!smb2req->subreq) {
953                 return false;
954         }
955         if (!tevent_req_is_in_progress(smb2req->subreq)) {
956                 return false;
957         }
958         state = tevent_req_data(smb2req->subreq,
959                         struct smbd_smb2_create_state);
960         if (!state) {
961                 return false;
962         }
963         /* It's not in progress if there's no timeout event. */
964         if (!state->te) {
965                 return false;
966         }
967
968         DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
969                         (unsigned long long)mid));
970
971         return true;
972 }
973
974 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
975                                                         uint64_t mid)
976 {
977         struct smbd_smb2_create_state *state = NULL;
978
979         if (!smb2req->subreq) {
980                 return;
981         }
982         if (!tevent_req_is_in_progress(smb2req->subreq)) {
983                 return;
984         }
985         state = tevent_req_data(smb2req->subreq,
986                         struct smbd_smb2_create_state);
987         if (!state) {
988                 return;
989         }
990
991         DEBUG(10,("remove_deferred_open_message_smb2_internal: "
992                 "mid %llu\n",
993                 (unsigned long long)mid ));
994
995         /* Ensure we don't have any outstanding timer event. */
996         TALLOC_FREE(state->te);
997         /* Ensure we don't have any outstanding immediate event. */
998         TALLOC_FREE(state->im);
999 }
1000
1001 void remove_deferred_open_message_smb2(
1002         struct smbd_server_connection *sconn, uint64_t mid)
1003 {
1004         struct smbd_smb2_request *smb2req;
1005
1006         smb2req = find_open_smb2req(sconn, mid);
1007
1008         if (!smb2req) {
1009                 DEBUG(10,("remove_deferred_open_message_smb2: "
1010                         "can't find mid %llu\n",
1011                         (unsigned long long)mid ));
1012                 return;
1013         }
1014         remove_deferred_open_message_smb2_internal(smb2req, mid);
1015 }
1016
1017 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1018                                         struct tevent_immediate *im,
1019                                         void *private_data)
1020 {
1021         struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1022                                         struct smbd_smb2_request);
1023         struct smbd_server_connection *sconn = smb2req->sconn;
1024         uint64_t mid = get_mid_from_smb2req(smb2req);
1025         NTSTATUS status;
1026
1027         DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1028                 "re-dispatching mid %llu\n",
1029                 (unsigned long long)mid ));
1030
1031         status = smbd_smb2_request_dispatch(smb2req);
1032         if (!NT_STATUS_IS_OK(status)) {
1033                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1034                 return;
1035         }
1036 }
1037
1038 bool schedule_deferred_open_message_smb2(
1039         struct smbd_server_connection *sconn, uint64_t mid)
1040 {
1041         struct smbd_smb2_create_state *state = NULL;
1042         struct smbd_smb2_request *smb2req;
1043
1044         smb2req = find_open_smb2req(sconn, mid);
1045
1046         if (!smb2req) {
1047                 DEBUG(10,("schedule_deferred_open_message_smb2: "
1048                         "can't find mid %llu\n",
1049                         (unsigned long long)mid ));
1050                 return false;
1051         }
1052         if (!smb2req->subreq) {
1053                 return false;
1054         }
1055         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1056                 return false;
1057         }
1058         state = tevent_req_data(smb2req->subreq,
1059                         struct smbd_smb2_create_state);
1060         if (!state) {
1061                 return false;
1062         }
1063
1064         /* Ensure we don't have any outstanding timer event. */
1065         TALLOC_FREE(state->te);
1066         /* Ensure we don't have any outstanding immediate event. */
1067         TALLOC_FREE(state->im);
1068
1069         /*
1070          * This is subtle. We must null out the callback
1071          * before rescheduling, else the first call to
1072          * tevent_req_nterror() causes the _receive()
1073          * function to be called, this causing tevent_req_post()
1074          * to crash.
1075          */
1076         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1077
1078         state->im = tevent_create_immediate(smb2req);
1079         if (!state->im) {
1080                 smbd_server_connection_terminate(smb2req->sconn,
1081                         nt_errstr(NT_STATUS_NO_MEMORY));
1082                 return false;
1083         }
1084
1085         DEBUG(10,("schedule_deferred_open_message_smb2: "
1086                 "re-processing mid %llu\n",
1087                 (unsigned long long)mid ));
1088
1089         tevent_schedule_immediate(state->im,
1090                         smb2req->sconn->ev_ctx,
1091                         smbd_smb2_create_request_dispatch_immediate,
1092                         smb2req);
1093
1094         return true;
1095 }
1096
1097 /*********************************************************
1098  Re-process this call.
1099 *********************************************************/
1100
1101 static void smb2_deferred_open_timer(struct event_context *ev,
1102                                         struct timed_event *te,
1103                                         struct timeval _tval,
1104                                         void *private_data)
1105 {
1106         NTSTATUS status;
1107         struct smbd_smb2_create_state *state = NULL;
1108         struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1109                                                 struct smbd_smb2_request);
1110
1111         DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1112                 smb2req->current_idx,
1113                 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1114
1115         state = tevent_req_data(smb2req->subreq,
1116                         struct smbd_smb2_create_state);
1117         if (!state) {
1118                 return;
1119         }
1120         /*
1121          * Null this out, don't talloc_free. It will
1122          * be talloc_free'd by the tevent library when
1123          * this returns.
1124          */
1125         state->te = NULL;
1126         /* Ensure we don't have any outstanding immediate event. */
1127         TALLOC_FREE(state->im);
1128
1129         /*
1130          * This is subtle. We must null out the callback
1131          * before rescheduling, else the first call to
1132          * tevent_req_nterror() causes the _receive()
1133          * function to be called, this causing tevent_req_post()
1134          * to crash.
1135          */
1136         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1137
1138         status = smbd_smb2_request_dispatch(smb2req);
1139
1140         if (!NT_STATUS_IS_OK(status)) {
1141                 smbd_server_connection_terminate(smb2req->sconn,
1142                                 nt_errstr(status));
1143         }
1144 }
1145
1146 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1147 {
1148         struct smbd_smb2_request *smb2req = NULL;
1149         struct smbd_smb2_create_state *state = tevent_req_data(req,
1150                                 struct smbd_smb2_create_state);
1151         uint64_t mid;
1152
1153         if (!state) {
1154                 return false;
1155         }
1156
1157         if (!state->smb2req) {
1158                 return false;
1159         }
1160
1161         smb2req = state->smb2req;
1162         mid = get_mid_from_smb2req(smb2req);
1163
1164         if (is_deferred_open_async(state->private_data.data)) {
1165                 /* Can't cancel an async create. */
1166                 return false;
1167         }
1168
1169         remove_deferred_open_entry(state->id, mid,
1170                                    messaging_server_id(smb2req->sconn->msg_ctx));
1171         remove_deferred_open_message_smb2_internal(smb2req, mid);
1172         smb2req->cancelled = true;
1173
1174         tevent_req_done(req);
1175         return true;
1176 }
1177
1178 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1179                                 struct timeval request_time,
1180                                 struct timeval timeout,
1181                                 struct file_id id,
1182                                 char *private_data,
1183                                 size_t priv_len)
1184 {
1185         struct tevent_req *req = NULL;
1186         struct smbd_smb2_create_state *state = NULL;
1187         struct timeval end_time;
1188
1189         if (!smb2req) {
1190                 return false;
1191         }
1192         req = smb2req->subreq;
1193         if (!req) {
1194                 return false;
1195         }
1196         state = tevent_req_data(req, struct smbd_smb2_create_state);
1197         if (!state) {
1198                 return false;
1199         }
1200         state->id = id;
1201         state->request_time = request_time;
1202         state->private_data = data_blob_talloc(state, private_data,
1203                                                 priv_len);
1204         if (!state->private_data.data) {
1205                 return false;
1206         }
1207
1208         /* Re-schedule us to retry on timer expiry. */
1209         end_time = timeval_sum(&request_time, &timeout);
1210
1211         DEBUG(10,("push_deferred_open_message_smb2: "
1212                 "timeout at %s\n",
1213                 timeval_string(talloc_tos(),
1214                                 &end_time,
1215                                 true) ));
1216
1217         state->te = tevent_add_timer(smb2req->sconn->ev_ctx,
1218                                 state,
1219                                 end_time,
1220                                 smb2_deferred_open_timer,
1221                                 smb2req);
1222         if (!state->te) {
1223                 return false;
1224         }
1225
1226         /* allow this request to be canceled */
1227         tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1228
1229         return true;
1230 }