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