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