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