smbd: Fix a profile problem
[samba.git] / source3 / smbd / smb2_create.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    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
30 int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
31 {
32         switch(in_oplock_level) {
33         case SMB2_OPLOCK_LEVEL_NONE:
34                 return NO_OPLOCK;
35         case SMB2_OPLOCK_LEVEL_II:
36                 return LEVEL_II_OPLOCK;
37         case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
38                 return EXCLUSIVE_OPLOCK;
39         case SMB2_OPLOCK_LEVEL_BATCH:
40                 return BATCH_OPLOCK;
41         case SMB2_OPLOCK_LEVEL_LEASE:
42                 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
43                         "LEASE_OPLOCK_REQUESTED\n"));
44                 return NO_OPLOCK;
45         default:
46                 DEBUG(2,("map_smb2_oplock_levels_to_samba: "
47                         "unknown level %u\n",
48                         (unsigned int)in_oplock_level));
49                 return NO_OPLOCK;
50         }
51 }
52
53 static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
54 {
55         if (BATCH_OPLOCK_TYPE(oplock_type)) {
56                 return SMB2_OPLOCK_LEVEL_BATCH;
57         } else if (EXCLUSIVE_OPLOCK_TYPE(oplock_type)) {
58                 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
59         } else if (oplock_type == LEVEL_II_OPLOCK) {
60                 /*
61                  * Don't use LEVEL_II_OPLOCK_TYPE here as
62                  * this also includes FAKE_LEVEL_II_OPLOCKs
63                  * which are internal only.
64                  */
65                 return SMB2_OPLOCK_LEVEL_II;
66         } else {
67                 return SMB2_OPLOCK_LEVEL_NONE;
68         }
69 }
70
71 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
72                         struct tevent_context *ev,
73                         struct smbd_smb2_request *smb2req,
74                         uint8_t in_oplock_level,
75                         uint32_t in_impersonation_level,
76                         uint32_t in_desired_access,
77                         uint32_t in_file_attributes,
78                         uint32_t in_share_access,
79                         uint32_t in_create_disposition,
80                         uint32_t in_create_options,
81                         const char *in_name,
82                         struct smb2_create_blobs in_context_blobs);
83 static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
84                         TALLOC_CTX *mem_ctx,
85                         uint8_t *out_oplock_level,
86                         uint32_t *out_create_action,
87                         NTTIME *out_creation_time,
88                         NTTIME *out_last_access_time,
89                         NTTIME *out_last_write_time,
90                         NTTIME *out_change_time,
91                         uint64_t *out_allocation_size,
92                         uint64_t *out_end_of_file,
93                         uint32_t *out_file_attributes,
94                         uint64_t *out_file_id_persistent,
95                         uint64_t *out_file_id_volatile,
96                         struct smb2_create_blobs *out_context_blobs);
97
98 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq);
99 NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
100 {
101         const uint8_t *inbody;
102         int i = smb2req->current_idx;
103         uint8_t in_oplock_level;
104         uint32_t in_impersonation_level;
105         uint32_t in_desired_access;
106         uint32_t in_file_attributes;
107         uint32_t in_share_access;
108         uint32_t in_create_disposition;
109         uint32_t in_create_options;
110         uint16_t in_name_offset;
111         uint16_t in_name_length;
112         DATA_BLOB in_name_buffer;
113         char *in_name_string;
114         size_t in_name_string_size;
115         uint32_t name_offset = 0;
116         uint32_t name_available_length = 0;
117         uint32_t in_context_offset;
118         uint32_t in_context_length;
119         DATA_BLOB in_context_buffer;
120         struct smb2_create_blobs in_context_blobs;
121         uint32_t context_offset = 0;
122         uint32_t context_available_length = 0;
123         uint32_t dyn_offset;
124         NTSTATUS status;
125         bool ok;
126         struct tevent_req *tsubreq;
127
128         status = smbd_smb2_request_verify_sizes(smb2req, 0x39);
129         if (!NT_STATUS_IS_OK(status)) {
130                 return smbd_smb2_request_error(smb2req, status);
131         }
132         inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
133
134         in_oplock_level         = CVAL(inbody, 0x03);
135         in_impersonation_level  = IVAL(inbody, 0x04);
136         in_desired_access       = IVAL(inbody, 0x18);
137         in_file_attributes      = IVAL(inbody, 0x1C);
138         in_share_access         = IVAL(inbody, 0x20);
139         in_create_disposition   = IVAL(inbody, 0x24);
140         in_create_options       = IVAL(inbody, 0x28);
141         in_name_offset          = SVAL(inbody, 0x2C);
142         in_name_length          = SVAL(inbody, 0x2E);
143         in_context_offset       = IVAL(inbody, 0x30);
144         in_context_length       = IVAL(inbody, 0x34);
145
146         /*
147          * First check if the dynamic name and context buffers
148          * are correctly specified.
149          *
150          * Note: That we don't check if the name and context buffers
151          *       overlap
152          */
153
154         dyn_offset = SMB2_HDR_BODY + smb2req->in.vector[i+1].iov_len;
155
156         if (in_name_offset == 0 && in_name_length == 0) {
157                 /* This is ok */
158                 name_offset = 0;
159         } else if (in_name_offset < dyn_offset) {
160                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
161         } else {
162                 name_offset = in_name_offset - dyn_offset;
163         }
164
165         if (name_offset > smb2req->in.vector[i+2].iov_len) {
166                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
167         }
168
169         name_available_length = smb2req->in.vector[i+2].iov_len - name_offset;
170
171         if (in_name_length > name_available_length) {
172                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
173         }
174
175         in_name_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
176                               name_offset;
177         in_name_buffer.length = in_name_length;
178
179         if (in_context_offset == 0 && in_context_length == 0) {
180                 /* This is ok */
181                 context_offset = 0;
182         } else if (in_context_offset < dyn_offset) {
183                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
184         } else {
185                 context_offset = in_context_offset - dyn_offset;
186         }
187
188         if (context_offset > smb2req->in.vector[i+2].iov_len) {
189                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
190         }
191
192         context_available_length = smb2req->in.vector[i+2].iov_len - context_offset;
193
194         if (in_context_length > context_available_length) {
195                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
196         }
197
198         in_context_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base +
199                                   context_offset;
200         in_context_buffer.length = in_context_length;
201
202         /*
203          * Now interpret the name and context buffers
204          */
205
206         ok = convert_string_talloc(smb2req, CH_UTF16, CH_UNIX,
207                                    in_name_buffer.data,
208                                    in_name_buffer.length,
209                                    &in_name_string,
210                                    &in_name_string_size, false);
211         if (!ok) {
212                 return smbd_smb2_request_error(smb2req, NT_STATUS_ILLEGAL_CHARACTER);
213         }
214
215         if (in_name_buffer.length == 0) {
216                 in_name_string_size = 0;
217         }
218
219         if (strlen(in_name_string) != in_name_string_size) {
220                 return smbd_smb2_request_error(smb2req, NT_STATUS_OBJECT_NAME_INVALID);
221         }
222
223         ZERO_STRUCT(in_context_blobs);
224         status = smb2_create_blob_parse(smb2req, in_context_buffer, &in_context_blobs);
225         if (!NT_STATUS_IS_OK(status)) {
226                 return smbd_smb2_request_error(smb2req, status);
227         }
228
229         tsubreq = smbd_smb2_create_send(smb2req,
230                                        smb2req->sconn->smb2.event_ctx,
231                                        smb2req,
232                                        in_oplock_level,
233                                        in_impersonation_level,
234                                        in_desired_access,
235                                        in_file_attributes,
236                                        in_share_access,
237                                        in_create_disposition,
238                                        in_create_options,
239                                        in_name_string,
240                                        in_context_blobs);
241         if (tsubreq == NULL) {
242                 smb2req->subreq = NULL;
243                 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
244         }
245         tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
246
247         return smbd_smb2_request_pending_queue(smb2req, tsubreq);
248 }
249
250 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
251 {
252         uint8_t *reqhdr = (uint8_t *)smb2req->out.vector[smb2req->current_idx].iov_base;
253         return BVAL(reqhdr, SMB2_HDR_MESSAGE_ID);
254 }
255
256 static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
257 {
258         struct smbd_smb2_request *smb2req = tevent_req_callback_data(tsubreq,
259                                         struct smbd_smb2_request);
260         int i = smb2req->current_idx;
261         uint8_t *outhdr;
262         DATA_BLOB outbody;
263         DATA_BLOB outdyn;
264         uint8_t out_oplock_level = 0;
265         uint32_t out_create_action = 0;
266         NTTIME out_creation_time = 0;
267         NTTIME out_last_access_time = 0;
268         NTTIME out_last_write_time = 0;
269         NTTIME out_change_time = 0;
270         uint64_t out_allocation_size = 0;
271         uint64_t out_end_of_file = 0;
272         uint32_t out_file_attributes = 0;
273         uint64_t out_file_id_persistent = 0;
274         uint64_t out_file_id_volatile = 0;
275         struct smb2_create_blobs out_context_blobs;
276         DATA_BLOB out_context_buffer;
277         uint16_t out_context_buffer_offset = 0;
278         NTSTATUS status;
279         NTSTATUS error; /* transport error */
280
281         if (smb2req->cancelled) {
282                 uint64_t mid = get_mid_from_smb2req(smb2req);
283                 DEBUG(10,("smbd_smb2_request_create_done: cancelled mid %llu\n",
284                         (unsigned long long)mid ));
285                 error = smbd_smb2_request_error(smb2req, NT_STATUS_CANCELLED);
286                 if (!NT_STATUS_IS_OK(error)) {
287                         smbd_server_connection_terminate(smb2req->sconn,
288                                 nt_errstr(error));
289                         return;
290                 }
291                 return;
292         }
293
294         status = smbd_smb2_create_recv(tsubreq,
295                                        smb2req,
296                                        &out_oplock_level,
297                                        &out_create_action,
298                                        &out_creation_time,
299                                        &out_last_access_time,
300                                        &out_last_write_time,
301                                        &out_change_time,
302                                        &out_allocation_size,
303                                        &out_end_of_file,
304                                        &out_file_attributes,
305                                        &out_file_id_persistent,
306                                        &out_file_id_volatile,
307                                        &out_context_blobs);
308         if (!NT_STATUS_IS_OK(status)) {
309                 error = smbd_smb2_request_error(smb2req, status);
310                 if (!NT_STATUS_IS_OK(error)) {
311                         smbd_server_connection_terminate(smb2req->sconn,
312                                                          nt_errstr(error));
313                         return;
314                 }
315                 return;
316         }
317
318         status = smb2_create_blob_push(smb2req, &out_context_buffer, out_context_blobs);
319         if (!NT_STATUS_IS_OK(status)) {
320                 error = smbd_smb2_request_error(smb2req, status);
321                 if (!NT_STATUS_IS_OK(error)) {
322                         smbd_server_connection_terminate(smb2req->sconn,
323                                                          nt_errstr(error));
324                         return;
325                 }
326                 return;
327         }
328
329         if (out_context_buffer.length > 0) {
330                 out_context_buffer_offset = SMB2_HDR_BODY + 0x58;
331         }
332
333         outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
334
335         outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x58);
336         if (outbody.data == NULL) {
337                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
338                 if (!NT_STATUS_IS_OK(error)) {
339                         smbd_server_connection_terminate(smb2req->sconn,
340                                                          nt_errstr(error));
341                         return;
342                 }
343                 return;
344         }
345
346         SSVAL(outbody.data, 0x00, 0x58 + 1);    /* struct size */
347         SCVAL(outbody.data, 0x02,
348               out_oplock_level);                /* oplock level */
349         SCVAL(outbody.data, 0x03, 0);           /* reserved */
350         SIVAL(outbody.data, 0x04,
351               out_create_action);               /* create action */
352         SBVAL(outbody.data, 0x08,
353               out_creation_time);               /* creation time */
354         SBVAL(outbody.data, 0x10,
355               out_last_access_time);            /* last access time */
356         SBVAL(outbody.data, 0x18,
357               out_last_write_time);             /* last write time */
358         SBVAL(outbody.data, 0x20,
359               out_change_time);                 /* change time */
360         SBVAL(outbody.data, 0x28,
361               out_allocation_size);             /* allocation size */
362         SBVAL(outbody.data, 0x30,
363               out_end_of_file);                 /* end of file */
364         SIVAL(outbody.data, 0x38,
365               out_file_attributes);             /* file attributes */
366         SIVAL(outbody.data, 0x3C, 0);           /* reserved */
367         SBVAL(outbody.data, 0x40,
368               out_file_id_persistent);          /* file id (persistent) */
369         SBVAL(outbody.data, 0x48,
370               out_file_id_volatile);            /* file id (volatile) */
371         SIVAL(outbody.data, 0x50,
372               out_context_buffer_offset);       /* create contexts offset */
373         SIVAL(outbody.data, 0x54,
374               out_context_buffer.length);       /* create contexts length */
375
376         outdyn = out_context_buffer;
377
378         error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
379         if (!NT_STATUS_IS_OK(error)) {
380                 smbd_server_connection_terminate(smb2req->sconn,
381                                                  nt_errstr(error));
382                 return;
383         }
384 }
385
386 struct smbd_smb2_create_state {
387         struct smbd_smb2_request *smb2req;
388         struct smb_request *smb1req;
389         bool open_was_deferred;
390         struct timed_event *te;
391         struct tevent_immediate *im;
392         struct timeval request_time;
393         struct file_id id;
394         DATA_BLOB private_data;
395         uint8_t out_oplock_level;
396         uint32_t out_create_action;
397         NTTIME out_creation_time;
398         NTTIME out_last_access_time;
399         NTTIME out_last_write_time;
400         NTTIME out_change_time;
401         uint64_t out_allocation_size;
402         uint64_t out_end_of_file;
403         uint32_t out_file_attributes;
404         uint64_t out_file_id_persistent;
405         uint64_t out_file_id_volatile;
406         struct smb2_create_blobs out_context_blobs;
407 };
408
409 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
410                         struct tevent_context *ev,
411                         struct smbd_smb2_request *smb2req,
412                         uint8_t in_oplock_level,
413                         uint32_t in_impersonation_level,
414                         uint32_t in_desired_access,
415                         uint32_t in_file_attributes,
416                         uint32_t in_share_access,
417                         uint32_t in_create_disposition,
418                         uint32_t in_create_options,
419                         const char *in_name,
420                         struct smb2_create_blobs in_context_blobs)
421 {
422         struct tevent_req *req = NULL;
423         struct smbd_smb2_create_state *state = NULL;
424         NTSTATUS status;
425         struct smb_request *smb1req = NULL;
426         files_struct *result = NULL;
427         int info;
428         struct timespec write_time_ts;
429         struct smb2_create_blobs out_context_blobs;
430         int requested_oplock_level;
431
432         ZERO_STRUCT(out_context_blobs);
433
434         if(lp_fake_oplocks(SNUM(smb2req->tcon->compat_conn))) {
435                 requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
436         } else {
437                 requested_oplock_level = in_oplock_level;
438         }
439
440
441         if (!smb2req->async) {
442                 /* New create call. */
443                 req = tevent_req_create(mem_ctx, &state,
444                                 struct smbd_smb2_create_state);
445                 if (req == NULL) {
446                         return NULL;
447                 }
448                 state->smb2req = smb2req;
449                 smb2req->subreq = req; /* So we can find this when going async. */
450
451                 smb1req = smbd_smb2_fake_smb_request(smb2req);
452                 if (tevent_req_nomem(smb1req, req)) {
453                         return tevent_req_post(req, ev);
454                 }
455                 state->smb1req = smb1req;
456                 DEBUG(10,("smbd_smb2_create: name[%s]\n",
457                         in_name));
458         } else {
459                 /* Re-entrant create call. */
460                 req = smb2req->subreq;
461                 state = tevent_req_data(req,
462                                 struct smbd_smb2_create_state);
463                 smb1req = state->smb1req;
464                 DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
465                         in_name ));
466         }
467
468         if (IS_IPC(smb1req->conn)) {
469                 const char *pipe_name = in_name;
470
471                 if (!lp_nt_pipe_support()) {
472                         tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
473                         return tevent_req_post(req, ev);
474                 }
475
476                 /* Strip \\ off the name. */
477                 if (pipe_name[0] == '\\') {
478                         pipe_name++;
479                 }
480
481                 status = open_np_file(smb1req, pipe_name, &result);
482                 if (!NT_STATUS_IS_OK(status)) {
483                         tevent_req_nterror(req, status);
484                         return tevent_req_post(req, ev);
485                 }
486                 info = FILE_WAS_OPENED;
487         } else if (CAN_PRINT(smb1req->conn)) {
488                 status = file_new(smb1req, smb1req->conn, &result);
489                 if(!NT_STATUS_IS_OK(status)) {
490                         tevent_req_nterror(req, status);
491                         return tevent_req_post(req, ev);
492                 }
493
494                 status = print_spool_open(result, in_name,
495                                           smb1req->vuid);
496                 if (!NT_STATUS_IS_OK(status)) {
497                         file_free(smb1req, result);
498                         tevent_req_nterror(req, status);
499                         return tevent_req_post(req, ev);
500                 }
501                 info = FILE_WAS_CREATED;
502         } else {
503                 char *fname;
504                 struct smb_filename *smb_fname = NULL;
505                 struct smb2_create_blob *exta = NULL;
506                 struct ea_list *ea_list = NULL;
507                 struct smb2_create_blob *mxac = NULL;
508                 NTTIME max_access_time = 0;
509                 struct smb2_create_blob *secd = NULL;
510                 struct security_descriptor *sec_desc = NULL;
511                 struct smb2_create_blob *dhnq = NULL;
512                 struct smb2_create_blob *dhnc = NULL;
513                 struct smb2_create_blob *alsi = NULL;
514                 uint64_t allocation_size = 0;
515                 struct smb2_create_blob *twrp = NULL;
516                 struct smb2_create_blob *qfid = NULL;
517
518                 exta = smb2_create_blob_find(&in_context_blobs,
519                                              SMB2_CREATE_TAG_EXTA);
520                 mxac = smb2_create_blob_find(&in_context_blobs,
521                                              SMB2_CREATE_TAG_MXAC);
522                 secd = smb2_create_blob_find(&in_context_blobs,
523                                              SMB2_CREATE_TAG_SECD);
524                 dhnq = smb2_create_blob_find(&in_context_blobs,
525                                              SMB2_CREATE_TAG_DHNQ);
526                 dhnc = smb2_create_blob_find(&in_context_blobs,
527                                              SMB2_CREATE_TAG_DHNC);
528                 alsi = smb2_create_blob_find(&in_context_blobs,
529                                              SMB2_CREATE_TAG_ALSI);
530                 twrp = smb2_create_blob_find(&in_context_blobs,
531                                              SMB2_CREATE_TAG_TWRP);
532                 qfid = smb2_create_blob_find(&in_context_blobs,
533                                              SMB2_CREATE_TAG_QFID);
534
535                 fname = talloc_strdup(state, in_name);
536                 if (tevent_req_nomem(fname, req)) {
537                         return tevent_req_post(req, ev);
538                 }
539
540                 if (exta) {
541                         if (dhnc) {
542                                 tevent_req_nterror(req,NT_STATUS_OBJECT_NAME_NOT_FOUND);
543                                 return tevent_req_post(req, ev);
544                         }
545
546                         ea_list = read_nttrans_ea_list(mem_ctx,
547                                 (const char *)exta->data.data, exta->data.length);
548                         if (!ea_list) {
549                                 DEBUG(10,("smbd_smb2_create_send: read_ea_name_list failed.\n"));
550                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
551                                 return tevent_req_post(req, ev);
552                         }
553                 }
554
555                 if (mxac) {
556                         if (dhnc) {
557                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
558                                 return tevent_req_post(req, ev);
559                         }
560
561                         if (mxac->data.length == 0) {
562                                 max_access_time = 0;
563                         } else if (mxac->data.length == 8) {
564                                 max_access_time = BVAL(mxac->data.data, 0);
565                         } else {
566                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
567                                 return tevent_req_post(req, ev);
568                         }
569                 }
570
571                 if (secd) {
572                         enum ndr_err_code ndr_err;
573
574                         if (dhnc) {
575                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
576                                 return tevent_req_post(req, ev);
577                         }
578
579                         sec_desc = talloc_zero(state, struct security_descriptor);
580                         if (tevent_req_nomem(sec_desc, req)) {
581                                 return tevent_req_post(req, ev);
582                         }
583
584                         ndr_err = ndr_pull_struct_blob(&secd->data,
585                                 sec_desc, sec_desc,
586                                 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
587                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
588                                 DEBUG(2,("ndr_pull_security_descriptor failed: %s\n",
589                                          ndr_errstr(ndr_err)));
590                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
591                                 return tevent_req_post(req, ev);
592                         }
593                 }
594
595                 if (dhnq) {
596                         if (dhnc) {
597                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
598                                 return tevent_req_post(req, ev);
599                         }
600
601                         if (dhnq->data.length != 16) {
602                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
603                                 return tevent_req_post(req, ev);
604                         }
605                         /*
606                          * we don't support durable handles yet
607                          * and have to ignore this
608                          */
609                 }
610
611                 if (dhnc) {
612                         if (dhnc->data.length != 16) {
613                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
614                                 return tevent_req_post(req, ev);
615                         }
616                         /* we don't support durable handles yet */
617                         tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
618                         return tevent_req_post(req, ev);
619                 }
620
621                 if (alsi) {
622                         if (dhnc) {
623                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
624                                 return tevent_req_post(req, ev);
625                         }
626
627                         if (alsi->data.length != 8) {
628                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
629                                 return tevent_req_post(req, ev);
630                         }
631                         allocation_size = BVAL(alsi->data.data, 0);
632                 }
633
634                 if (twrp) {
635                         NTTIME nttime;
636                         time_t t;
637                         struct tm *tm;
638
639                         if (dhnc) {
640                                 tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
641                                 return tevent_req_post(req, ev);
642                         }
643
644                         if (twrp->data.length != 8) {
645                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
646                                 return tevent_req_post(req, ev);
647                         }
648
649                         nttime = BVAL(twrp->data.data, 0);
650                         t = nt_time_to_unix(nttime);
651                         tm = gmtime(&t);
652
653                         TALLOC_FREE(fname);
654                         fname = talloc_asprintf(state,
655                                         "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
656                                         tm->tm_year + 1900,
657                                         tm->tm_mon + 1,
658                                         tm->tm_mday,
659                                         tm->tm_hour,
660                                         tm->tm_min,
661                                         tm->tm_sec,
662                                         in_name);
663                         if (tevent_req_nomem(fname, req)) {
664                                 return tevent_req_post(req, ev);
665                         }
666                 }
667
668                 if (qfid) {
669                         if (qfid->data.length != 0) {
670                                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
671                                 return tevent_req_post(req, ev);
672                         }
673                 }
674
675                 /* these are ignored for SMB2 */
676                 in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
677                 in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
678
679                 /*
680                  * For a DFS path the function parse_dfs_path()
681                  * will do the path processing.
682                  */
683
684                 if (!(smb1req->flags2 & FLAGS2_DFS_PATHNAMES)) {
685                         /* convert '\\' into '/' */
686                         status = check_path_syntax(fname);
687                         if (!NT_STATUS_IS_OK(status)) {
688                                 tevent_req_nterror(req, status);
689                                 return tevent_req_post(req, ev);
690                         }
691                 }
692
693                 status = filename_convert(req,
694                                           smb1req->conn,
695                                           smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
696                                           fname,
697                                           (in_create_disposition == FILE_CREATE) ?
698                                                 UCF_CREATING_FILE : 0,
699                                           NULL,
700                                           &smb_fname);
701                 if (!NT_STATUS_IS_OK(status)) {
702                         tevent_req_nterror(req, status);
703                         return tevent_req_post(req, ev);
704                 }
705
706                 in_file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
707
708                 status = SMB_VFS_CREATE_FILE(smb1req->conn,
709                                              smb1req,
710                                              0, /* root_dir_fid */
711                                              smb_fname,
712                                              in_desired_access,
713                                              in_share_access,
714                                              in_create_disposition,
715                                              in_create_options,
716                                              in_file_attributes,
717                                              map_smb2_oplock_levels_to_samba(requested_oplock_level),
718                                              allocation_size,
719                                              0, /* private_flags */
720                                              sec_desc,
721                                              ea_list,
722                                              &result,
723                                              &info);
724                 if (!NT_STATUS_IS_OK(status)) {
725                         if (open_was_deferred(smb1req->mid)) {
726                                 return req;
727                         }
728                         tevent_req_nterror(req, status);
729                         return tevent_req_post(req, ev);
730                 }
731
732                 if (mxac) {
733                         NTTIME last_write_time;
734
735                         unix_timespec_to_nt_time(&last_write_time,
736                                                  result->fsp_name->st.st_ex_mtime);
737                         if (last_write_time != max_access_time) {
738                                 uint8_t p[8];
739                                 uint32_t max_access_granted;
740                                 DATA_BLOB blob = data_blob_const(p, sizeof(p));
741
742                                 status = smbd_calculate_access_mask(smb1req->conn,
743                                                         result->fsp_name,
744                                                         /*
745                                                          * at this stage
746                                                          * it exists
747                                                          */
748                                                         true,
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 = fsp_persistent_id(result);
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         req = smb2req->subreq;
898         if (!req) {
899                 return false;
900         }
901         state = tevent_req_data(req, struct smbd_smb2_create_state);
902         if (!state) {
903                 return false;
904         }
905         if (!state->open_was_deferred) {
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->open_was_deferred) {
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         state->open_was_deferred = false;
1001         /* Ensure we don't have any outstanding timer event. */
1002         TALLOC_FREE(state->te);
1003         /* Ensure we don't have any outstanding immediate event. */
1004         TALLOC_FREE(state->im);
1005 }
1006
1007 void remove_deferred_open_message_smb2(
1008         struct smbd_server_connection *sconn, uint64_t mid)
1009 {
1010         struct smbd_smb2_request *smb2req;
1011
1012         smb2req = find_open_smb2req(sconn, mid);
1013
1014         if (!smb2req) {
1015                 DEBUG(10,("remove_deferred_open_message_smb2: "
1016                         "can't find mid %llu\n",
1017                         (unsigned long long)mid ));
1018                 return;
1019         }
1020         remove_deferred_open_message_smb2_internal(smb2req, mid);
1021 }
1022
1023 static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *ctx,
1024                                         struct tevent_immediate *im,
1025                                         void *private_data)
1026 {
1027         struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
1028                                         struct smbd_smb2_request);
1029         struct smbd_server_connection *sconn = smb2req->sconn;
1030         uint64_t mid = get_mid_from_smb2req(smb2req);
1031         NTSTATUS status;
1032
1033         DEBUG(10,("smbd_smb2_create_request_dispatch_immediate: "
1034                 "re-dispatching mid %llu\n",
1035                 (unsigned long long)mid ));
1036
1037         status = smbd_smb2_request_dispatch(smb2req);
1038         if (!NT_STATUS_IS_OK(status)) {
1039                 smbd_server_connection_terminate(sconn, nt_errstr(status));
1040                 return;
1041         }
1042 }
1043
1044 void schedule_deferred_open_message_smb2(
1045         struct smbd_server_connection *sconn, uint64_t mid)
1046 {
1047         struct smbd_smb2_create_state *state = NULL;
1048         struct smbd_smb2_request *smb2req;
1049
1050         smb2req = find_open_smb2req(sconn, mid);
1051
1052         if (!smb2req) {
1053                 DEBUG(10,("schedule_deferred_open_message_smb2: "
1054                         "can't find mid %llu\n",
1055                         (unsigned long long)mid ));
1056                 return;
1057         }
1058         if (!smb2req->subreq) {
1059                 return;
1060         }
1061         if (!tevent_req_is_in_progress(smb2req->subreq)) {
1062                 return;
1063         }
1064         state = tevent_req_data(smb2req->subreq,
1065                         struct smbd_smb2_create_state);
1066         if (!state) {
1067                 return;
1068         }
1069
1070         /* Ensure we don't have any outstanding timer event. */
1071         TALLOC_FREE(state->te);
1072         /* Ensure we don't have any outstanding immediate event. */
1073         TALLOC_FREE(state->im);
1074
1075         /*
1076          * This is subtle. We must null out the callback
1077          * before resheduling, else the first call to
1078          * tevent_req_nterror() causes the _receive()
1079          * function to be called, this causing tevent_req_post()
1080          * to crash.
1081          */
1082         tevent_req_set_callback(smb2req->subreq, NULL, NULL);
1083
1084         state->im = tevent_create_immediate(smb2req);
1085         if (!state->im) {
1086                 smbd_server_connection_terminate(smb2req->sconn,
1087                         nt_errstr(NT_STATUS_NO_MEMORY));
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->smb2.event_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                                    sconn_server_id(smb2req->sconn));
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 #if 1
1207         /* Boo - turns out this isn't what W2K8R2
1208            does. It actually sends the STATUS_PENDING
1209            message followed by the STATUS_SHARING_VIOLATION
1210            message. Surely this means that all open
1211            calls (even on directories) will potentially
1212            fail in a chain.... ? And I've seen directory
1213            opens as the start of a chain. JRA.
1214
1215            Update: 19th May 2010. Talking with Microsoft
1216            engineers at the plugfest this is a bug in
1217            Windows. Re-enable this code.
1218         */
1219         /*
1220          * More subtlety. To match W2K8R2 don't
1221          * send a "gone async" message if it's simply
1222          * a STATUS_SHARING_VIOLATION (short) wait, not
1223          * an oplock break wait. We do this by prematurely
1224          * setting smb2req->async flag.
1225          */
1226         if (timeout.tv_sec < 2) {
1227                 DEBUG(10,("push_deferred_open_message_smb2: "
1228                         "short timer wait (usec = %u). "
1229                         "Don't send async message.\n",
1230                         (unsigned int)timeout.tv_usec ));
1231                 smb2req->async = true;
1232         }
1233 #endif
1234
1235         /* Re-schedule us to retry on timer expiry. */
1236         end_time = timeval_sum(&request_time, &timeout);
1237
1238         DEBUG(10,("push_deferred_open_message_smb2: "
1239                 "timeout at %s\n",
1240                 timeval_string(talloc_tos(),
1241                                 &end_time,
1242                                 true) ));
1243
1244         state->open_was_deferred = true;
1245         state->te = event_add_timed(smb2req->sconn->smb2.event_ctx,
1246                                 state,
1247                                 end_time,
1248                                 smb2_deferred_open_timer,
1249                                 smb2req);
1250         if (!state->te) {
1251                 return false;
1252         }
1253
1254         /* allow this request to be canceled */
1255         tevent_req_set_cancel_fn(req, smbd_smb2_create_cancel);
1256
1257         return true;
1258 }