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