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