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