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