s3:smb2_create: check for SMB2_CREATE_TAG_DHNC first
[obnox/samba/samba-obnox.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         struct smb2_create_blob *dhnc = NULL;
435
436         ZERO_STRUCT(out_context_blobs);
437
438         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
439                 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
440         } else {
441                 requested_oplock_level = in_oplock_level;
442         }
443
444
445         if (smb2req->subreq == NULL) {
446                 /* New create call. */
447                 req = tevent_req_create(mem_ctx, &state,
448                                 struct smbd_smb2_create_state);
449                 if (req == NULL) {
450                         return NULL;
451                 }
452                 state->smb2req = smb2req;
453
454                 smb1req = smbd_smb2_fake_smb_request(smb2req);
455                 if (tevent_req_nomem(smb1req, req)) {
456                         return tevent_req_post(req, ev);
457                 }
458                 state->smb1req = smb1req;
459                 smb2req->subreq = req;
460                 DEBUG(10,("smbd_smb2_create: name[%s]\n",
461                         in_name));
462         } else {
463                 /* Re-entrant create call. */
464                 req = smb2req->subreq;
465                 state = tevent_req_data(req,
466                                 struct smbd_smb2_create_state);
467                 smb1req = state->smb1req;
468                 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
469                         in_name ));
470         }
471
472         dhnc = smb2_create_blob_find(&in_context_blobs,
473                                      SMB2_CREATE_TAG_DHNC);
474
475         if (dhnc) {
476                 if (dhnc->data.length != 16) {
477                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
478                         return tevent_req_post(req, ev);
479                 }
480                 /* we don't support durable handles yet */
481                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
482                 return tevent_req_post(req, ev);
483         }
484
485         if (IS_IPC(smb1req->conn)) {
486                 const char *pipe_name = in_name;
487
488                 if (!lp_nt_pipe_support()) {
489                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
490                         return tevent_req_post(req, ev);
491                 }
492
493                 status = open_np_file(smb1req, pipe_name, &result);
494                 if (!NT_STATUS_IS_OK(status)) {
495                         tevent_req_nterror(req, status);
496                         return tevent_req_post(req, ev);
497                 }
498                 info = FILE_WAS_OPENED;
499         } else if (CAN_PRINT(smb1req->conn)) {
500                 status = file_new(smb1req, smb1req->conn, &result);
501                 if(!NT_STATUS_IS_OK(status)) {
502                         tevent_req_nterror(req, status);
503                         return tevent_req_post(req, ev);
504                 }
505
506                 status = print_spool_open(result, in_name,
507                                           smb1req->vuid);
508                 if (!NT_STATUS_IS_OK(status)) {
509                         file_free(smb1req, result);
510                         tevent_req_nterror(req, status);
511                         return tevent_req_post(req, ev);
512                 }
513                 info = FILE_WAS_CREATED;
514         } else {
515                 char *fname;
516                 struct smb_filename *smb_fname = NULL;
517                 struct smb2_create_blob *exta = NULL;
518                 struct ea_list *ea_list = NULL;
519                 struct smb2_create_blob *mxac = NULL;
520                 NTTIME max_access_time = 0;
521                 struct smb2_create_blob *secd = NULL;
522                 struct security_descriptor *sec_desc = NULL;
523                 struct smb2_create_blob *dhnq = NULL;
524                 struct smb2_create_blob *alsi = NULL;
525                 uint64_t allocation_size = 0;
526                 struct smb2_create_blob *twrp = NULL;
527                 struct smb2_create_blob *qfid = NULL;
528
529                 exta = smb2_create_blob_find(&in_context_blobs,
530                                              SMB2_CREATE_TAG_EXTA);
531                 mxac = smb2_create_blob_find(&in_context_blobs,
532                                              SMB2_CREATE_TAG_MXAC);
533                 secd = smb2_create_blob_find(&in_context_blobs,
534                                              SMB2_CREATE_TAG_SECD);
535                 dhnq = smb2_create_blob_find(&in_context_blobs,
536                                              SMB2_CREATE_TAG_DHNQ);
537                 alsi = smb2_create_blob_find(&in_context_blobs,
538                                              SMB2_CREATE_TAG_ALSI);
539                 twrp = smb2_create_blob_find(&in_context_blobs,
540                                              SMB2_CREATE_TAG_TWRP);
541                 qfid = smb2_create_blob_find(&in_context_blobs,
542                                              SMB2_CREATE_TAG_QFID);
543
544                 fname = talloc_strdup(state, in_name);
545                 if (tevent_req_nomem(fname, req)) {
546                         return tevent_req_post(req, ev);
547                 }
548
549                 if (exta) {
550                         ea_list = read_nttrans_ea_list(mem_ctx,
551                                 (const char *)exta->data.data, exta->data.length);
552                         if (!ea_list) {
553                                 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
554                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
555                                 return tevent_req_post(req, ev);
556                         }
557                 }
558
559                 if (mxac) {
560                         if (mxac->data.length == 0) {
561                                 max_access_time = 0;
562                         } else if (mxac->data.length == 8) {
563                                 max_access_time = BVAL(mxac->data.data, 0);
564                         } else {
565                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
566                                 return tevent_req_post(req, ev);
567                         }
568                 }
569
570                 if (secd) {
571                         enum ndr_err_code ndr_err;
572
573                         sec_desc = talloc_zero(state, struct security_descriptor);
574                         if (tevent_req_nomem(sec_desc, req)) {
575                                 return tevent_req_post(req, ev);
576                         }
577
578                         ndr_err = ndr_pull_struct_blob(&secd->data,
579                                 sec_desc, sec_desc,
580                                 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
581                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
582                                 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
583                                          ndr_errstr(ndr_err)));
584                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
585                                 return tevent_req_post(req, ev);
586                         }
587                 }
588
589                 if (dhnq) {
590                         if (dhnq->data.length != 16) {
591                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
592                                 return tevent_req_post(req, ev);
593                         }
594                         /*
595                          * we don't support durable handles yet
596                          * and have to ignore this
597                          */
598                 }
599
600                 if (alsi) {
601                         if (alsi->data.length != 8) {
602                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
603                                 return tevent_req_post(req, ev);
604                         }
605                         allocation_size = BVAL(alsi->data.data, 0);
606                 }
607
608                 if (twrp) {
609                         NTTIME nttime;
610                         time_t t;
611                         struct tm *tm;
612
613                         if (twrp->data.length != 8) {
614                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
615                                 return tevent_req_post(req, ev);
616                         }
617
618                         nttime = BVAL(twrp->data.data, 0);
619                         t = nt_time_to_unix(nttime);
620                         tm = gmtime(&t);
621
622                         TALLOC_FREE(fname);
623                         fname = talloc_asprintf(state,
624                                         "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
625                                         tm->tm_year + 1900,
626                                         tm->tm_mon + 1,
627                                         tm->tm_mday,
628                                         tm->tm_hour,
629                                         tm->tm_min,
630                                         tm->tm_sec,
631                                         in_name);
632                         if (tevent_req_nomem(fname, req)) {
633                                 return tevent_req_post(req, ev);
634                         }
635                 }
636
637                 if (qfid) {
638                         if (qfid->data.length != 0) {
639                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
640                                 return tevent_req_post(req, ev);
641                         }
642                 }
643
644                 /* these are ignored for SMB2 */
645                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
646                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
647
648                 /*
649                  * For a DFS path the function parse_dfs_path()
650                  * will do the path processing.
651                  */
652
653                 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
654                         /* convert '\\' into '/' */
655                         status = check_path_syntax(fname);
656                         if (!NT_STATUS_IS_OK(status)) {
657                                 tevent_req_nterror(req, status);
658                                 return tevent_req_post(req, ev);
659                         }
660                 }
661
662                 status = filename_convert(req,
663                                           smb1req->conn,
664                                           smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
665                                           fname,
666                                           0,    /* unix_convert flags */
667                                           NULL, /* ppath_contains_wcards */
668                                           &smb_fname);
669                 if (!NT_STATUS_IS_OK(status)) {
670                         tevent_req_nterror(req, status);
671                         return tevent_req_post(req, ev);
672                 }
673
674                 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
675
676                 status = SMB_VFS_CREATE_FILE(smb1req->conn,
677                                              smb1req,
678                                              0, /* root_dir_fid */
679                                              smb_fname,
680                                              in_desired_access,
681                                              in_share_access,
682                                              in_create_disposition,
683                                              in_create_options,
684                                              in_file_attributes,
685                                              map_smb2_oplock_levels_to_samba(requested_oplock_level),
686                                              allocation_size,
687                                              0, /* private_flags */
688                                              sec_desc,
689                                              ea_list,
690                                              &result,
691                                              &info);
692                 if (!NT_STATUS_IS_OK(status)) {
693                         if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
694                                 return req;
695                         }
696                         tevent_req_nterror(req, status);
697                         return tevent_req_post(req, ev);
698                 }
699
700                 if (mxac) {
701                         NTTIME last_write_time;
702
703                         unix_timespec_to_nt_time(&last_write_time,
704                                                  result->fsp_name->st.st_ex_mtime);
705                         if (last_write_time != max_access_time) {
706                                 uint8_t p[8];
707                                 uint32_t max_access_granted;
708                                 DATA_BLOB blob = data_blob_const(p, sizeof(p));
709
710                                 status = smbd_calculate_access_mask(smb1req->conn,
711                                                         result->fsp_name,
712                                                         SEC_FLAG_MAXIMUM_ALLOWED,
713                                                         &max_access_granted);
714
715                                 SIVAL(p, 0, NT_STATUS_V(status));
716                                 SIVAL(p, 4, max_access_granted);
717
718                                 status = smb2_create_blob_add(state,
719                                                         &out_context_blobs,
720                                                         SMB2_CREATE_TAG_MXAC,
721                                                         blob);
722                                 if (!NT_STATUS_IS_OK(status)) {
723                                         tevent_req_nterror(req, status);
724                                         return tevent_req_post(req, ev);
725                                 }
726                         }
727                 }
728
729                 if (qfid) {
730                         uint8_t p[32];
731                         uint64_t file_index = get_FileIndex(result->conn,
732                                                         &result->fsp_name->st);
733                         DATA_BLOB blob = data_blob_const(p, sizeof(p));
734
735                         ZERO_STRUCT(p);
736
737                         /* From conversations with Microsoft engineers at
738                            the MS plugfest. The first 8 bytes are the "volume index"
739                            == inode, the second 8 bytes are the "volume id",
740                            == dev. This will be updated in the SMB2 doc. */
741                         SBVAL(p, 0, file_index);
742                         SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
743
744                         status = smb2_create_blob_add(state, &out_context_blobs,
745                                                       SMB2_CREATE_TAG_QFID,
746                                                       blob);
747                         if (!NT_STATUS_IS_OK(status)) {
748                                 tevent_req_nterror(req, status);
749                                 return tevent_req_post(req, ev);
750                         }
751                 }
752         }
753
754         smb2req->compat_chain_fsp = smb1req->chain_fsp;
755
756         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
757                 state->out_oplock_level = in_oplock_level;
758         } else {
759                 state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
760         }
761
762         if ((in_create_disposition == FILE_SUPERSEDE)
763             && (info == FILE_WAS_OVERWRITTEN)) {
764                 state->out_create_action = FILE_WAS_SUPERSEDED;
765         } else {
766                 state->out_create_action = info;
767         }
768         state->out_file_attributes = dos_mode(result->conn,
769                                            result->fsp_name);
770         /* Deal with other possible opens having a modified
771            write time. JRA. */
772         ZERO_STRUCT(write_time_ts);
773         get_file_infos(result->file_id, 0, NULL, &write_time_ts);
774         if (!null_timespec(write_time_ts)) {
775                 update_stat_ex_mtime(&result->fsp_name->st, write_time_ts);
776         }
777
778         unix_timespec_to_nt_time(&state->out_creation_time,
779                         get_create_timespec(smb1req->conn, result,
780                                         result->fsp_name));
781         unix_timespec_to_nt_time(&state->out_last_access_time,
782                         result->fsp_name->st.st_ex_atime);
783         unix_timespec_to_nt_time(&state->out_last_write_time,
784                         result->fsp_name->st.st_ex_mtime);
785         unix_timespec_to_nt_time(&state->out_change_time,
786                         get_change_timespec(smb1req->conn, result,
787                                         result->fsp_name));
788         state->out_allocation_size =
789                         SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
790                                                &(result->fsp_name->st));
791         state->out_end_of_file = result->fsp_name->st.st_ex_size;
792         if (state->out_file_attributes == 0) {
793                 state->out_file_attributes = FILE_ATTRIBUTE_NORMAL;
794         }
795         state->out_file_id_persistent = result->op->global->open_persistent_id;
796         state->out_file_id_volatile = result->op->global->open_volatile_id;
797         state->out_context_blobs = out_context_blobs;
798
799         DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
800                   fsp_str_dbg(result), fsp_fnum_dbg(result)));
801
802         tevent_req_done(req);
803         return tevent_req_post(req, ev);
804 }
805
806 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
807                         TALLOC_CTX *mem_ctx,
808                         uint8_t *out_oplock_level,
809                         uint32_t *out_create_action,
810                         NTTIME *out_creation_time,
811                         NTTIME *out_last_access_time,
812                         NTTIME *out_last_write_time,
813                         NTTIME *out_change_time,
814                         uint64_t *out_allocation_size,
815                         uint64_t *out_end_of_file,
816                         uint32_t *out_file_attributes,
817                         uint64_t *out_file_id_persistent,
818                         uint64_t *out_file_id_volatile,
819                         struct smb2_create_blobs *out_context_blobs)
820 {
821         NTSTATUS status;
822         struct smbd_smb2_create_state *state = tevent_req_data(req,
823                                                struct smbd_smb2_create_state);
824
825         if (tevent_req_is_nterror(req, &status)) {
826                 tevent_req_received(req);
827                 return status;
828         }
829
830         *out_oplock_level       = state->out_oplock_level;
831         *out_create_action      = state->out_create_action;
832         *out_creation_time      = state->out_creation_time;
833         *out_last_access_time   = state->out_last_access_time;
834         *out_last_write_time    = state->out_last_write_time;
835         *out_change_time        = state->out_change_time;
836         *out_allocation_size    = state->out_allocation_size;
837         *out_end_of_file        = state->out_end_of_file;
838         *out_file_attributes    = state->out_file_attributes;
839         *out_file_id_persistent = state->out_file_id_persistent;
840         *out_file_id_volatile   = state->out_file_id_volatile;
841         *out_context_blobs      = state->out_context_blobs;
842
843         talloc_steal(mem_ctx, state->out_context_blobs.blobs);
844
845         tevent_req_received(req);
846         return NT_STATUS_OK;
847 }
848
849 /*********************************************************
850  Code for dealing with deferred opens.
851 *********************************************************/
852
853 bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
854                         struct timeval *p_request_time,
855                         void **pp_state)
856 {
857         struct smbd_smb2_create_state *state = NULL;
858         struct tevent_req *req = NULL;
859
860         if (!smb2req) {
861                 return false;
862         }
863         if (smb2req->async_te == NULL) {
864                 return false;
865         }
866         req = smb2req->subreq;
867         if (!req) {
868                 return false;
869         }
870         state = tevent_req_data(req, struct smbd_smb2_create_state);
871         if (!state) {
872                 return false;
873         }
874         if (p_request_time) {
875                 *p_request_time = state->request_time;
876         }
877         if (pp_state) {
878                 *pp_state = (void *)state->private_data.data;
879         }
880         return true;
881 }
882
883 /*********************************************************
884  Re-process this call early - requested by message or
885  close.
886 *********************************************************/
887
888 static struct smbd_smb2_request *find_open_smb2req(
889         struct smbd_server_connection *sconn, uint64_t mid)
890 {
891         struct smbd_smb2_request *smb2req;
892
893         for (smb2req = sconn->smb2.requests; smb2req; smb2req = smb2req->next) {
894                 uint64_t message_id;
895                 if (smb2req->subreq == NULL) {
896                         /* This message has been processed. */
897                         continue;
898                 }
899                 if (!tevent_req_is_in_progress(smb2req->subreq)) {
900                         /* This message has been processed. */
901                         continue;
902                 }
903                 message_id = get_mid_from_smb2req(smb2req);
904                 if (message_id == mid) {
905                         return smb2req;
906                 }
907         }
908         return NULL;
909 }
910
911 bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
912 {
913         struct smbd_smb2_create_state *state = NULL;
914         struct smbd_smb2_request *smb2req;
915
916         smb2req = find_open_smb2req(sconn, mid);
917
918         if (!smb2req) {
919                 DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
920                         (unsigned long long)mid));
921                 return false;
922         }
923         if (!smb2req->subreq) {
924                 return false;
925         }
926         if (!tevent_req_is_in_progress(smb2req->subreq)) {
927                 return false;
928         }
929         state = tevent_req_data(smb2req->subreq,
930                         struct smbd_smb2_create_state);
931         if (!state) {
932                 return false;
933         }
934         /* It's not in progress if there's no timeout event. */
935         if (!state->te) {
936                 return false;
937         }
938
939         DEBUG(10,("open_was_deferred_smb2: mid = %llu\n",
940                         (unsigned long long)mid));
941
942         return true;
943 }
944
945 static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request *smb2req,
946                                                         uint64_t mid)
947 {
948         struct smbd_smb2_create_state *state = NULL;
949
950         if (!smb2req->subreq) {
951                 return;
952         }
953         if (!tevent_req_is_in_progress(smb2req->subreq)) {
954                 return;
955         }
956         state = tevent_req_data(smb2req->subreq,
957                         struct smbd_smb2_create_state);
958         if (!state) {
959                 return;
960         }
961
962         DEBUG(10,("remove_deferred_open_message_smb2_internal: "
963                 "mid %llu\n",
964                 (unsigned long long)mid ));
965
966         /* Ensure we don't have any outstanding timer event. */
967         TALLOC_FREE(state->te);
968         /* Ensure we don't have any outstanding immediate event. */
969         TALLOC_FREE(state->im);
970 }
971
972 void remove_deferred_open_message_smb2(
973         struct smbd_server_connection *sconn, uint64_t mid)
974 {
975         struct smbd_smb2_request *smb2req;
976
977         smb2req = find_open_smb2req(sconn, mid);
978
979         if (!smb2req) {
980                 DEBUG(10,("remove_deferred_open_message_smb2: "
981                         "can't find mid %llu\n",
982                         (unsigned long long)mid ));
983                 return;
984         }
985         remove_deferred_open_message_smb2_internal(smb2req, mid);
986 }
987
988 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
989                                         struct tevent_immediate *im,
990                                         void *private_data)
991 {
992         struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
993                                         struct smbd_smb2_request);
994         struct smbd_server_connection *sconn = smb2req->sconn;
995         uint64_t mid = get_mid_from_smb2req(smb2req);
996         NTSTATUS status;
997
998         DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
999                 "re-dispatching mid %llu\n",
1000                 (unsigned long long)mid ));
1001
1002         status = smbd_smb2_request_dispatch(smb2req);
1003         if (!NT_STATUS_IS_OK(status)) {
1004                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1005                 return;
1006         }
1007 }
1008
1009 bool schedule_deferred_open_message_smb2(
1010         struct smbd_server_connection *sconn, uint64_t mid)
1011 {
1012         struct smbd_smb2_create_state *state = NULL;
1013         struct smbd_smb2_request *smb2req;
1014
1015         smb2req = find_open_smb2req(sconn, mid);
1016
1017         if (!smb2req) {
1018                 DEBUG(10,("schedule_deferred_open_message_smb2: "
1019                         "can't find mid %llu\n",
1020                         (unsigned long long)mid ));
1021                 return false;
1022         }
1023         if (!smb2req->subreq) {
1024                 return false;
1025         }
1026         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1027                 return false;
1028         }
1029         state = tevent_req_data(smb2req->subreq,
1030                         struct smbd_smb2_create_state);
1031         if (!state) {
1032                 return false;
1033         }
1034
1035         /* Ensure we don't have any outstanding timer event. */
1036         TALLOC_FREE(state->te);
1037         /* Ensure we don't have any outstanding immediate event. */
1038         TALLOC_FREE(state->im);
1039
1040         /*
1041          * This is subtle. We must null out the callback
1042          * before rescheduling, else the first call to
1043          * tevent_req_nterror() causes the _receive()
1044          * function to be called, this causing tevent_req_post()
1045          * to crash.
1046          */
1047         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1048
1049         state->im = tevent_create_immediate(smb2req);
1050         if (!state->im) {
1051                 smbd_server_connection_terminate(smb2req->sconn,
1052                         nt_errstr(NT_STATUS_NO_MEMORY));
1053                 return false;
1054         }
1055
1056         DEBUG(10,("schedule_deferred_open_message_smb2: "
1057                 "re-processing mid %llu\n",
1058                 (unsigned long long)mid ));
1059
1060         tevent_schedule_immediate(state->im,
1061                         smb2req->sconn->ev_ctx,
1062                         smbd_smb2_create_request_dispatch_immediate,
1063                         smb2req);
1064
1065         return true;
1066 }
1067
1068 /*********************************************************
1069  Re-process this call.
1070 *********************************************************/
1071
1072 static void smb2_deferred_open_timer(struct event_context *ev,
1073                                         struct timed_event *te,
1074                                         struct timeval _tval,
1075                                         void *private_data)
1076 {
1077         NTSTATUS status;
1078         struct smbd_smb2_create_state *state = NULL;
1079         struct smbd_smb2_request *smb2req = talloc_get_type(private_data,
1080                                                 struct smbd_smb2_request);
1081
1082         DEBUG(10,("smb2_deferred_open_timer: [idx=%d], %s\n",
1083                 smb2req->current_idx,
1084                 tevent_req_default_print(smb2req->subreq, talloc_tos()) ));
1085
1086         state = tevent_req_data(smb2req->subreq,
1087                         struct smbd_smb2_create_state);
1088         if (!state) {
1089                 return;
1090         }
1091         /*
1092          * Null this out, don't talloc_free. It will
1093          * be talloc_free'd by the tevent library when
1094          * this returns.
1095          */
1096         state->te = NULL;
1097         /* Ensure we don't have any outstanding immediate event. */
1098         TALLOC_FREE(state->im);
1099
1100         /*
1101          * This is subtle. We must null out the callback
1102          * before rescheduling, else the first call to
1103          * tevent_req_nterror() causes the _receive()
1104          * function to be called, this causing tevent_req_post()
1105          * to crash.
1106          */
1107         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1108
1109         status = smbd_smb2_request_dispatch(smb2req);
1110
1111         if (!NT_STATUS_IS_OK(status)) {
1112                 smbd_server_connection_terminate(smb2req->sconn,
1113                                 nt_errstr(status));
1114         }
1115 }
1116
1117 static bool smbd_smb2_create_cancel(struct tevent_req *req)
1118 {
1119         struct smbd_smb2_request *smb2req = NULL;
1120         struct smbd_smb2_create_state *state = tevent_req_data(req,
1121                                 struct smbd_smb2_create_state);
1122         uint64_t mid;
1123
1124         if (!state) {
1125                 return false;
1126         }
1127
1128         if (!state->smb2req) {
1129                 return false;
1130         }
1131
1132         smb2req = state->smb2req;
1133         mid = get_mid_from_smb2req(smb2req);
1134
1135         if (is_deferred_open_async(state->private_data.data)) {
1136                 /* Can't cancel an async create. */
1137                 return false;
1138         }
1139
1140         remove_deferred_open_entry(state->id, mid,
1141                                    messaging_server_id(smb2req->sconn->msg_ctx));
1142         remove_deferred_open_message_smb2_internal(smb2req, mid);
1143         smb2req->cancelled = true;
1144
1145         tevent_req_done(req);
1146         return true;
1147 }
1148
1149 bool push_deferred_open_message_smb2(struct smbd_smb2_request *smb2req,
1150                                 struct timeval request_time,
1151                                 struct timeval timeout,
1152                                 struct file_id id,
1153                                 char *private_data,
1154                                 size_t priv_len)
1155 {
1156         struct tevent_req *req = NULL;
1157         struct smbd_smb2_create_state *state = NULL;
1158         struct timeval end_time;
1159
1160         if (!smb2req) {
1161                 return false;
1162         }
1163         req = smb2req->subreq;
1164         if (!req) {
1165                 return false;
1166         }
1167         state = tevent_req_data(req, struct smbd_smb2_create_state);
1168         if (!state) {
1169                 return false;
1170         }
1171         state->id = id;
1172         state->request_time = request_time;
1173         state->private_data = data_blob_talloc(state, private_data,
1174                                                 priv_len);
1175         if (!state->private_data.data) {
1176                 return false;
1177         }
1178
1179         /* Re-schedule us to retry on timer expiry. */
1180         end_time = timeval_sum(&request_time, &timeout);
1181
1182         DEBUG(10,("push_deferred_open_message_smb2: "
1183                 "timeout at %s\n",
1184                 timeval_string(talloc_tos(),
1185                                 &end_time,
1186                                 true) ));
1187
1188         state->te = tevent_add_timer(smb2req->sconn->ev_ctx,
1189                                 state,
1190                                 end_time,
1191                                 smb2_deferred_open_timer,
1192                                 smb2req);
1193         if (!state->te) {
1194                 return false;
1195         }
1196
1197         /* allow this request to be canceled */
1198         tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1199
1200         return true;
1201 }