f8833df5745092df894e4c2156dac8b741559486
[gd/samba-autobuild/.git] / source3 / smbd / open.c
1 /* 
2    Unix SMB/CIFS implementation.
3    file opening and share modes
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 2001-2004
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Ralph Boehme 2017
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "lib/util/server_id.h"
26 #include "printing.h"
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "fake_file.h"
30 #include "../libcli/security/security.h"
31 #include "../librpc/gen_ndr/ndr_security.h"
32 #include "../librpc/gen_ndr/open_files.h"
33 #include "../librpc/gen_ndr/idmap.h"
34 #include "../librpc/gen_ndr/ioctl.h"
35 #include "passdb/lookup_sid.h"
36 #include "auth.h"
37 #include "serverid.h"
38 #include "messages.h"
39 #include "source3/lib/dbwrap/dbwrap_watch.h"
40 #include "locking/leases_db.h"
41 #include "librpc/gen_ndr/ndr_leases_db.h"
42
43 extern const struct generic_mapping file_generic_mapping;
44
45 struct deferred_open_record {
46         bool delayed_for_oplocks;
47         bool async_open;
48         struct file_id id;
49
50         /*
51          * Timer for async opens, needed because they don't use a watch on
52          * a locking.tdb record. This is currently only used for real async
53          * opens and just terminates smbd if the async open times out.
54          */
55         struct tevent_timer *te;
56 };
57
58 /****************************************************************************
59  If the requester wanted DELETE_ACCESS and was rejected because
60  the file ACL didn't include DELETE_ACCESS, see if the parent ACL
61  overrides this.
62 ****************************************************************************/
63
64 static bool parent_override_delete(connection_struct *conn,
65                                         const struct smb_filename *smb_fname,
66                                         uint32_t access_mask,
67                                         uint32_t rejected_mask)
68 {
69         if ((access_mask & DELETE_ACCESS) &&
70                     (rejected_mask & DELETE_ACCESS) &&
71                     can_delete_file_in_directory(conn, smb_fname)) {
72                 return true;
73         }
74         return false;
75 }
76
77 /****************************************************************************
78  Check if we have open rights.
79 ****************************************************************************/
80
81 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
82                                 const struct smb_filename *smb_fname,
83                                 bool use_privs,
84                                 uint32_t access_mask)
85 {
86         /* Check if we have rights to open. */
87         NTSTATUS status;
88         struct security_descriptor *sd = NULL;
89         uint32_t rejected_share_access;
90         uint32_t rejected_mask = access_mask;
91         uint32_t do_not_check_mask = 0;
92
93         rejected_share_access = access_mask & ~(conn->share_access);
94
95         if (rejected_share_access) {
96                 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
97                         "on %s (0x%x)\n",
98                         (unsigned int)access_mask,
99                         smb_fname_str_dbg(smb_fname),
100                         (unsigned int)rejected_share_access ));
101                 return NT_STATUS_ACCESS_DENIED;
102         }
103
104         if (!use_privs && get_current_uid(conn) == (uid_t)0) {
105                 /* I'm sorry sir, I didn't know you were root... */
106                 DEBUG(10,("smbd_check_access_rights: root override "
107                         "on %s. Granting 0x%x\n",
108                         smb_fname_str_dbg(smb_fname),
109                         (unsigned int)access_mask ));
110                 return NT_STATUS_OK;
111         }
112
113         if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
114                 DEBUG(10,("smbd_check_access_rights: not checking ACL "
115                         "on DELETE_ACCESS on file %s. Granting 0x%x\n",
116                         smb_fname_str_dbg(smb_fname),
117                         (unsigned int)access_mask ));
118                 return NT_STATUS_OK;
119         }
120
121         if (access_mask == DELETE_ACCESS &&
122                         VALID_STAT(smb_fname->st) &&
123                         S_ISLNK(smb_fname->st.st_ex_mode)) {
124                 /* We can always delete a symlink. */
125                 DEBUG(10,("smbd_check_access_rights: not checking ACL "
126                         "on DELETE_ACCESS on symlink %s.\n",
127                         smb_fname_str_dbg(smb_fname) ));
128                 return NT_STATUS_OK;
129         }
130
131         status = SMB_VFS_GET_NT_ACL(conn, smb_fname,
132                         (SECINFO_OWNER |
133                         SECINFO_GROUP |
134                          SECINFO_DACL), talloc_tos(), &sd);
135
136         if (!NT_STATUS_IS_OK(status)) {
137                 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
138                         "on %s: %s\n",
139                         smb_fname_str_dbg(smb_fname),
140                         nt_errstr(status)));
141
142                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
143                         goto access_denied;
144                 }
145
146                 return status;
147         }
148
149         /*
150          * If we can access the path to this file, by
151          * default we have FILE_READ_ATTRIBUTES from the
152          * containing directory. See the section:
153          * "Algorithm to Check Access to an Existing File"
154          * in MS-FSA.pdf.
155          *
156          * se_file_access_check() also takes care of
157          * owner WRITE_DAC and READ_CONTROL.
158          */
159         do_not_check_mask = FILE_READ_ATTRIBUTES;
160
161         /*
162          * Samba 3.6 and earlier granted execute access even
163          * if the ACL did not contain execute rights.
164          * Samba 4.0 is more correct and checks it.
165          * The compatibilty mode allows one to skip this check
166          * to smoothen upgrades.
167          */
168         if (lp_acl_allow_execute_always(SNUM(conn))) {
169                 do_not_check_mask |= FILE_EXECUTE;
170         }
171
172         status = se_file_access_check(sd,
173                                 get_current_nttok(conn),
174                                 use_privs,
175                                 (access_mask & ~do_not_check_mask),
176                                 &rejected_mask);
177
178         DEBUG(10,("smbd_check_access_rights: file %s requesting "
179                 "0x%x returning 0x%x (%s)\n",
180                 smb_fname_str_dbg(smb_fname),
181                 (unsigned int)access_mask,
182                 (unsigned int)rejected_mask,
183                 nt_errstr(status) ));
184
185         if (!NT_STATUS_IS_OK(status)) {
186                 if (DEBUGLEVEL >= 10) {
187                         DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
188                                 smb_fname_str_dbg(smb_fname) ));
189                         NDR_PRINT_DEBUG(security_descriptor, sd);
190                 }
191         }
192
193         TALLOC_FREE(sd);
194
195         if (NT_STATUS_IS_OK(status) ||
196                         !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
197                 return status;
198         }
199
200         /* Here we know status == NT_STATUS_ACCESS_DENIED. */
201
202   access_denied:
203
204         if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
205                         (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
206                         !lp_store_dos_attributes(SNUM(conn)) &&
207                         (lp_map_readonly(SNUM(conn)) ||
208                         lp_map_archive(SNUM(conn)) ||
209                         lp_map_hidden(SNUM(conn)) ||
210                         lp_map_system(SNUM(conn)))) {
211                 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
212
213                 DEBUG(10,("smbd_check_access_rights: "
214                         "overrode "
215                         "FILE_WRITE_ATTRIBUTES "
216                         "on file %s\n",
217                         smb_fname_str_dbg(smb_fname)));
218         }
219
220         if (parent_override_delete(conn,
221                                 smb_fname,
222                                 access_mask,
223                                 rejected_mask)) {
224                 /* Were we trying to do an open
225                  * for delete and didn't get DELETE
226                  * access (only) ? Check if the
227                  * directory allows DELETE_CHILD.
228                  * See here:
229                  * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
230                  * for details. */
231
232                 rejected_mask &= ~DELETE_ACCESS;
233
234                 DEBUG(10,("smbd_check_access_rights: "
235                         "overrode "
236                         "DELETE_ACCESS on "
237                         "file %s\n",
238                         smb_fname_str_dbg(smb_fname)));
239         }
240
241         if (rejected_mask != 0) {
242                 return NT_STATUS_ACCESS_DENIED;
243         }
244         return NT_STATUS_OK;
245 }
246
247 NTSTATUS check_parent_access(struct connection_struct *conn,
248                                 struct smb_filename *smb_fname,
249                                 uint32_t access_mask)
250 {
251         NTSTATUS status;
252         char *parent_dir = NULL;
253         struct security_descriptor *parent_sd = NULL;
254         uint32_t access_granted = 0;
255         struct smb_filename *parent_smb_fname = NULL;
256         struct share_mode_lock *lck = NULL;
257         struct file_id id = {0};
258         uint32_t name_hash;
259         bool delete_on_close_set;
260         int ret;
261         TALLOC_CTX *frame = talloc_stackframe();
262
263         if (!parent_dirname(frame,
264                                 smb_fname->base_name,
265                                 &parent_dir,
266                                 NULL)) {
267                 status = NT_STATUS_NO_MEMORY;
268                 goto out;
269         }
270
271         parent_smb_fname = synthetic_smb_fname(frame,
272                                 parent_dir,
273                                 NULL,
274                                 NULL,
275                                 smb_fname->flags);
276         if (parent_smb_fname == NULL) {
277                 status = NT_STATUS_NO_MEMORY;
278                 goto out;
279         }
280
281         if (get_current_uid(conn) == (uid_t)0) {
282                 /* I'm sorry sir, I didn't know you were root... */
283                 DEBUG(10,("check_parent_access: root override "
284                         "on %s. Granting 0x%x\n",
285                         smb_fname_str_dbg(smb_fname),
286                         (unsigned int)access_mask ));
287                 status = NT_STATUS_OK;
288                 goto out;
289         }
290
291         status = SMB_VFS_GET_NT_ACL(conn,
292                                 parent_smb_fname,
293                                 SECINFO_DACL,
294                                 frame,
295                                 &parent_sd);
296
297         if (!NT_STATUS_IS_OK(status)) {
298                 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
299                         "%s with error %s\n",
300                         parent_dir,
301                         nt_errstr(status)));
302                 goto out;
303         }
304
305         /*
306          * If we can access the path to this file, by
307          * default we have FILE_READ_ATTRIBUTES from the
308          * containing directory. See the section:
309          * "Algorithm to Check Access to an Existing File"
310          * in MS-FSA.pdf.
311          *
312          * se_file_access_check() also takes care of
313          * owner WRITE_DAC and READ_CONTROL.
314          */
315         status = se_file_access_check(parent_sd,
316                                 get_current_nttok(conn),
317                                 false,
318                                 (access_mask & ~FILE_READ_ATTRIBUTES),
319                                 &access_granted);
320         if(!NT_STATUS_IS_OK(status)) {
321                 DEBUG(5,("check_parent_access: access check "
322                         "on directory %s for "
323                         "path %s for mask 0x%x returned (0x%x) %s\n",
324                         parent_dir,
325                         smb_fname->base_name,
326                         access_mask,
327                         access_granted,
328                         nt_errstr(status) ));
329                 goto out;
330         }
331
332         if (!(access_mask & (SEC_DIR_ADD_FILE | SEC_DIR_ADD_SUBDIR))) {
333                 status = NT_STATUS_OK;
334                 goto out;
335         }
336         if (!lp_check_parent_directory_delete_on_close(SNUM(conn))) {
337                 status = NT_STATUS_OK;
338                 goto out;
339         }
340
341         /* Check if the directory has delete-on-close set */
342         ret = SMB_VFS_STAT(conn, parent_smb_fname);
343         if (ret != 0) {
344                 status = map_nt_error_from_unix(errno);
345                 goto out;
346         }
347
348         id = SMB_VFS_FILE_ID_CREATE(conn, &parent_smb_fname->st);
349
350         status = file_name_hash(conn, parent_smb_fname->base_name, &name_hash);
351         if (!NT_STATUS_IS_OK(status)) {
352                 goto out;
353         }
354
355         lck = get_existing_share_mode_lock(frame, id);
356         if (lck == NULL) {
357                 status = NT_STATUS_OK;
358                 goto out;
359         }
360
361         delete_on_close_set = is_delete_on_close_set(lck, name_hash);
362         if (delete_on_close_set) {
363                 status = NT_STATUS_DELETE_PENDING;
364                 goto out;
365         }
366
367         status = NT_STATUS_OK;
368
369 out:
370         TALLOC_FREE(frame);
371         return status;
372 }
373
374 /****************************************************************************
375  Ensure when opening a base file for a stream open that we have permissions
376  to do so given the access mask on the base file.
377 ****************************************************************************/
378
379 static NTSTATUS check_base_file_access(struct connection_struct *conn,
380                                 struct smb_filename *smb_fname,
381                                 uint32_t access_mask)
382 {
383         NTSTATUS status;
384
385         status = smbd_calculate_access_mask(conn, smb_fname,
386                                         false,
387                                         access_mask,
388                                         &access_mask);
389         if (!NT_STATUS_IS_OK(status)) {
390                 DEBUG(10, ("smbd_calculate_access_mask "
391                         "on file %s returned %s\n",
392                         smb_fname_str_dbg(smb_fname),
393                         nt_errstr(status)));
394                 return status;
395         }
396
397         if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
398                 uint32_t dosattrs;
399                 if (!CAN_WRITE(conn)) {
400                         return NT_STATUS_ACCESS_DENIED;
401                 }
402                 dosattrs = dos_mode(conn, smb_fname);
403                 if (IS_DOS_READONLY(dosattrs)) {
404                         return NT_STATUS_ACCESS_DENIED;
405                 }
406         }
407
408         return smbd_check_access_rights(conn,
409                                         smb_fname,
410                                         false,
411                                         access_mask);
412 }
413
414 /****************************************************************************
415  Handle differing symlink errno's
416 ****************************************************************************/
417
418 static int link_errno_convert(int err)
419 {
420 #if defined(ENOTSUP) && defined(OSF1)
421         /* handle special Tru64 errno */
422         if (err == ENOTSUP) {
423                 err = ELOOP;
424         }
425 #endif /* ENOTSUP */
426 #ifdef EFTYPE
427         /* fix broken NetBSD errno */
428         if (err == EFTYPE) {
429                 err = ELOOP;
430         }
431 #endif /* EFTYPE */
432         /* fix broken FreeBSD errno */
433         if (err == EMLINK) {
434                 err = ELOOP;
435         }
436         return err;
437 }
438
439 static int non_widelink_open(struct connection_struct *conn,
440                         const struct smb_filename *conn_rootdir_fname,
441                         files_struct *fsp,
442                         struct smb_filename *smb_fname,
443                         int flags,
444                         mode_t mode,
445                         unsigned int link_depth);
446
447 /****************************************************************************
448  Follow a symlink in userspace.
449 ****************************************************************************/
450
451 static int process_symlink_open(struct connection_struct *conn,
452                         const struct smb_filename *conn_rootdir_fname,
453                         files_struct *fsp,
454                         struct smb_filename *smb_fname,
455                         int flags,
456                         mode_t mode,
457                         unsigned int link_depth)
458 {
459         int fd = -1;
460         char *link_target = NULL;
461         struct smb_filename target_fname = {0};
462         int link_len = -1;
463         struct smb_filename *oldwd_fname = NULL;
464         size_t rootdir_len = 0;
465         struct smb_filename *resolved_fname = NULL;
466         char *resolved_name = NULL;
467         bool matched = false;
468         int saved_errno = 0;
469
470         /*
471          * Ensure we don't get stuck in a symlink loop.
472          */
473         link_depth++;
474         if (link_depth >= 20) {
475                 errno = ELOOP;
476                 goto out;
477         }
478
479         /* Allocate space for the link target. */
480         link_target = talloc_array(talloc_tos(), char, PATH_MAX);
481         if (link_target == NULL) {
482                 errno = ENOMEM;
483                 goto out;
484         }
485
486         /* Read the link target. */
487         link_len = SMB_VFS_READLINK(conn,
488                                 smb_fname,
489                                 link_target,
490                                 PATH_MAX - 1);
491         if (link_len == -1) {
492                 goto out;
493         }
494
495         /* Ensure it's at least null terminated. */
496         link_target[link_len] = '\0';
497         target_fname = (struct smb_filename){ .base_name = link_target };
498
499         /* Convert to an absolute path. */
500         resolved_fname = SMB_VFS_REALPATH(conn, talloc_tos(), &target_fname);
501         if (resolved_fname == NULL) {
502                 goto out;
503         }
504         resolved_name = resolved_fname->base_name;
505
506         /*
507          * We know conn_rootdir starts with '/' and
508          * does not end in '/'. FIXME ! Should we
509          * smb_assert this ?
510          */
511         rootdir_len = strlen(conn_rootdir_fname->base_name);
512
513         matched = (strncmp(conn_rootdir_fname->base_name,
514                                 resolved_name,
515                                 rootdir_len) == 0);
516         if (!matched) {
517                 errno = EACCES;
518                 goto out;
519         }
520
521         /*
522          * Turn into a path relative to the share root.
523          */
524         if (resolved_name[rootdir_len] == '\0') {
525                 /* Link to the root of the share. */
526                 TALLOC_FREE(smb_fname->base_name);
527                 smb_fname->base_name = talloc_strdup(smb_fname, ".");
528         } else if (resolved_name[rootdir_len] == '/') {
529                 TALLOC_FREE(smb_fname->base_name);
530                 smb_fname->base_name = talloc_strdup(smb_fname,
531                                         &resolved_name[rootdir_len+1]);
532         } else {
533                 errno = EACCES;
534                 goto out;
535         }
536
537         if (smb_fname->base_name == NULL) {
538                 errno = ENOMEM;
539                 goto out;
540         }
541
542         oldwd_fname = vfs_GetWd(talloc_tos(), conn);
543         if (oldwd_fname == NULL) {
544                 goto out;
545         }
546
547         /* Ensure we operate from the root of the share. */
548         if (vfs_ChDir(conn, conn_rootdir_fname) == -1) {
549                 goto out;
550         }
551
552         /* And do it all again.. */
553         fd = non_widelink_open(conn,
554                                 conn_rootdir_fname,
555                                 fsp,
556                                 smb_fname,
557                                 flags,
558                                 mode,
559                                 link_depth);
560         if (fd == -1) {
561                 saved_errno = errno;
562         }
563
564   out:
565
566         TALLOC_FREE(resolved_fname);
567         TALLOC_FREE(link_target);
568         if (oldwd_fname != NULL) {
569                 int ret = vfs_ChDir(conn, oldwd_fname);
570                 if (ret == -1) {
571                         smb_panic("unable to get back to old directory\n");
572                 }
573                 TALLOC_FREE(oldwd_fname);
574         }
575         if (saved_errno != 0) {
576                 errno = saved_errno;
577         }
578         return fd;
579 }
580
581 /****************************************************************************
582  Non-widelink open.
583 ****************************************************************************/
584
585 static int non_widelink_open(struct connection_struct *conn,
586                         const struct smb_filename *conn_rootdir_fname,
587                         files_struct *fsp,
588                         struct smb_filename *smb_fname,
589                         int flags,
590                         mode_t mode,
591                         unsigned int link_depth)
592 {
593         NTSTATUS status;
594         int fd = -1;
595         struct smb_filename *smb_fname_rel = NULL;
596         int saved_errno = 0;
597         struct smb_filename *oldwd_fname = NULL;
598         char *parent_dir = NULL;
599         struct smb_filename parent_dir_fname = {0};
600         const char *final_component = NULL;
601         bool is_directory = false;
602         bool ok;
603
604 #ifdef O_DIRECTORY
605         if (flags & O_DIRECTORY) {
606                 is_directory = true;
607         }
608 #endif
609
610         if (is_directory) {
611                 parent_dir = talloc_strdup(talloc_tos(), smb_fname->base_name);
612                 if (parent_dir == NULL) {
613                         saved_errno = errno;
614                         goto out;
615                 }
616
617                 final_component = ".";
618         } else {
619                 ok = parent_dirname(talloc_tos(),
620                                     smb_fname->base_name,
621                                     &parent_dir,
622                                     &final_component);
623                 if (!ok) {
624                         saved_errno = errno;
625                         goto out;
626                 }
627         }
628
629         parent_dir_fname = (struct smb_filename) { .base_name = parent_dir };
630
631         oldwd_fname = vfs_GetWd(talloc_tos(), conn);
632         if (oldwd_fname == NULL) {
633                 goto out;
634         }
635
636         /* Pin parent directory in place. */
637         if (vfs_ChDir(conn, &parent_dir_fname) == -1) {
638                 goto out;
639         }
640
641         smb_fname_rel = synthetic_smb_fname(talloc_tos(),
642                                 final_component,
643                                 smb_fname->stream_name,
644                                 &smb_fname->st,
645                                 smb_fname->flags);
646         if (smb_fname_rel == NULL) {
647                 saved_errno = ENOMEM;
648                 goto out;
649         }
650
651         /* Ensure the relative path is below the share. */
652         status = check_reduced_name(conn, &parent_dir_fname, smb_fname_rel);
653         if (!NT_STATUS_IS_OK(status)) {
654                 saved_errno = map_errno_from_nt_status(status);
655                 goto out;
656         }
657
658         flags |= O_NOFOLLOW;
659
660         {
661                 struct smb_filename *tmp_name = fsp->fsp_name;
662                 fsp->fsp_name = smb_fname_rel;
663                 fd = SMB_VFS_OPEN(conn, smb_fname_rel, fsp, flags, mode);
664                 fsp->fsp_name = tmp_name;
665         }
666
667         if (fd == -1) {
668                 saved_errno = link_errno_convert(errno);
669                 /*
670                  * Trying to open a symlink to a directory with O_NOFOLLOW and
671                  * O_DIRECTORY can return either of ELOOP and ENOTDIR. So
672                  * ENOTDIR really means: might be a symlink, but we're not sure.
673                  * In this case, we just assume there's a symlink. If we were
674                  * wrong, process_symlink_open() will return EINVAL. We check
675                  * this below, and fall back to returning the initial
676                  * saved_errno.
677                  *
678                  * BUG: https://bugzilla.samba.org/show_bug.cgi?id=12860
679                  */
680                 if (saved_errno == ELOOP || saved_errno == ENOTDIR) {
681                         if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
682                                 /* Never follow symlinks on posix open. */
683                                 goto out;
684                         }
685                         if (!lp_follow_symlinks(SNUM(conn))) {
686                                 /* Explicitly no symlinks. */
687                                 goto out;
688                         }
689                         /*
690                          * We may have a symlink. Follow in userspace
691                          * to ensure it's under the share definition.
692                          */
693                         fd = process_symlink_open(conn,
694                                         conn_rootdir_fname,
695                                         fsp,
696                                         smb_fname_rel,
697                                         flags,
698                                         mode,
699                                         link_depth);
700                         if (fd == -1) {
701                                 if (saved_errno == ENOTDIR &&
702                                                 errno == EINVAL) {
703                                         /*
704                                          * O_DIRECTORY on neither a directory,
705                                          * nor a symlink. Just return
706                                          * saved_errno from initial open()
707                                          */
708                                         goto out;
709                                 }
710                                 saved_errno =
711                                         link_errno_convert(errno);
712                         }
713                 }
714         }
715
716   out:
717
718         TALLOC_FREE(parent_dir);
719         TALLOC_FREE(smb_fname_rel);
720
721         if (oldwd_fname != NULL) {
722                 int ret = vfs_ChDir(conn, oldwd_fname);
723                 if (ret == -1) {
724                         smb_panic("unable to get back to old directory\n");
725                 }
726                 TALLOC_FREE(oldwd_fname);
727         }
728         if (saved_errno != 0) {
729                 errno = saved_errno;
730         }
731         return fd;
732 }
733
734 /****************************************************************************
735  fd support routines - attempt to do a dos_open.
736 ****************************************************************************/
737
738 NTSTATUS fd_open(struct connection_struct *conn,
739                  files_struct *fsp,
740                  int flags,
741                  mode_t mode)
742 {
743         struct smb_filename *smb_fname = fsp->fsp_name;
744         NTSTATUS status = NT_STATUS_OK;
745
746         /*
747          * Never follow symlinks on a POSIX client. The
748          * client should be doing this.
749          */
750
751         if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) || !lp_follow_symlinks(SNUM(conn))) {
752                 flags |= O_NOFOLLOW;
753         }
754
755         /* Ensure path is below share definition. */
756         if (!lp_widelinks(SNUM(conn))) {
757                 struct smb_filename *conn_rootdir_fname = NULL;
758                 const char *conn_rootdir = SMB_VFS_CONNECTPATH(conn,
759                                                 smb_fname);
760                 int saved_errno = 0;
761
762                 if (conn_rootdir == NULL) {
763                         return NT_STATUS_NO_MEMORY;
764                 }
765                 conn_rootdir_fname = synthetic_smb_fname(talloc_tos(),
766                                                 conn_rootdir,
767                                                 NULL,
768                                                 NULL,
769                                                 0);
770                 if (conn_rootdir_fname == NULL) {
771                         return NT_STATUS_NO_MEMORY;
772                 }
773
774                 /*
775                  * Only follow symlinks within a share
776                  * definition.
777                  */
778                 fsp->fh->fd = non_widelink_open(conn,
779                                         conn_rootdir_fname,
780                                         fsp,
781                                         smb_fname,
782                                         flags,
783                                         mode,
784                                         0);
785                 if (fsp->fh->fd == -1) {
786                         saved_errno = errno;
787                 }
788                 TALLOC_FREE(conn_rootdir_fname);
789                 if (saved_errno != 0) {
790                         errno = saved_errno;
791                 }
792         } else {
793                 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
794         }
795
796         if (fsp->fh->fd == -1) {
797                 int posix_errno = link_errno_convert(errno);
798                 status = map_nt_error_from_unix(posix_errno);
799                 if (errno == EMFILE) {
800                         static time_t last_warned = 0L;
801
802                         if (time((time_t *) NULL) > last_warned) {
803                                 DEBUG(0,("Too many open files, unable "
804                                         "to open more!  smbd's max "
805                                         "open files = %d\n",
806                                         lp_max_open_files()));
807                                 last_warned = time((time_t *) NULL);
808                         }
809                 }
810
811         }
812
813         DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
814                   smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
815                 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
816
817         return status;
818 }
819
820 /****************************************************************************
821  Close the file associated with a fsp.
822 ****************************************************************************/
823
824 NTSTATUS fd_close(files_struct *fsp)
825 {
826         int ret;
827
828         if (fsp->dptr) {
829                 dptr_CloseDir(fsp);
830         }
831         if (fsp->fh->fd == -1) {
832                 return NT_STATUS_OK; /* What we used to call a stat open. */
833         }
834         if (fsp->fh->ref_count > 1) {
835                 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
836         }
837
838         ret = SMB_VFS_CLOSE(fsp);
839         fsp->fh->fd = -1;
840         if (ret == -1) {
841                 return map_nt_error_from_unix(errno);
842         }
843         return NT_STATUS_OK;
844 }
845
846 /****************************************************************************
847  Change the ownership of a file to that of the parent directory.
848  Do this by fd if possible.
849 ****************************************************************************/
850
851 void change_file_owner_to_parent(connection_struct *conn,
852                                         const char *inherit_from_dir,
853                                         files_struct *fsp)
854 {
855         struct smb_filename *smb_fname_parent;
856         int ret;
857
858         smb_fname_parent = synthetic_smb_fname(talloc_tos(),
859                                         inherit_from_dir,
860                                         NULL,
861                                         NULL,
862                                         0);
863         if (smb_fname_parent == NULL) {
864                 return;
865         }
866
867         ret = SMB_VFS_STAT(conn, smb_fname_parent);
868         if (ret == -1) {
869                 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
870                          "directory %s. Error was %s\n",
871                          smb_fname_str_dbg(smb_fname_parent),
872                          strerror(errno)));
873                 TALLOC_FREE(smb_fname_parent);
874                 return;
875         }
876
877         if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
878                 /* Already this uid - no need to change. */
879                 DEBUG(10,("change_file_owner_to_parent: file %s "
880                         "is already owned by uid %d\n",
881                         fsp_str_dbg(fsp),
882                         (int)fsp->fsp_name->st.st_ex_uid ));
883                 TALLOC_FREE(smb_fname_parent);
884                 return;
885         }
886
887         become_root();
888         ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
889         unbecome_root();
890         if (ret == -1) {
891                 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
892                          "file %s to parent directory uid %u. Error "
893                          "was %s\n", fsp_str_dbg(fsp),
894                          (unsigned int)smb_fname_parent->st.st_ex_uid,
895                          strerror(errno) ));
896         } else {
897                 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
898                         "parent directory uid %u.\n", fsp_str_dbg(fsp),
899                         (unsigned int)smb_fname_parent->st.st_ex_uid));
900                 /* Ensure the uid entry is updated. */
901                 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
902         }
903
904         TALLOC_FREE(smb_fname_parent);
905 }
906
907 static NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
908                                         const char *inherit_from_dir,
909                                         struct smb_filename *smb_dname,
910                                         SMB_STRUCT_STAT *psbuf)
911 {
912         struct smb_filename *smb_fname_parent;
913         struct smb_filename *smb_fname_cwd = NULL;
914         struct smb_filename *saved_dir_fname = NULL;
915         TALLOC_CTX *ctx = talloc_tos();
916         NTSTATUS status = NT_STATUS_OK;
917         int ret;
918
919         smb_fname_parent = synthetic_smb_fname(ctx,
920                                         inherit_from_dir,
921                                         NULL,
922                                         NULL,
923                                         0);
924         if (smb_fname_parent == NULL) {
925                 return NT_STATUS_NO_MEMORY;
926         }
927
928         ret = SMB_VFS_STAT(conn, smb_fname_parent);
929         if (ret == -1) {
930                 status = map_nt_error_from_unix(errno);
931                 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
932                          "directory %s. Error was %s\n",
933                          smb_fname_str_dbg(smb_fname_parent),
934                          strerror(errno)));
935                 goto out;
936         }
937
938         /* We've already done an lstat into psbuf, and we know it's a
939            directory. If we can cd into the directory and the dev/ino
940            are the same then we can safely chown without races as
941            we're locking the directory in place by being in it.  This
942            should work on any UNIX (thanks tridge :-). JRA.
943         */
944
945         saved_dir_fname = vfs_GetWd(ctx,conn);
946         if (!saved_dir_fname) {
947                 status = map_nt_error_from_unix(errno);
948                 DEBUG(0,("change_dir_owner_to_parent: failed to get "
949                          "current working directory. Error was %s\n",
950                          strerror(errno)));
951                 goto out;
952         }
953
954         /* Chdir into the new path. */
955         if (vfs_ChDir(conn, smb_dname) == -1) {
956                 status = map_nt_error_from_unix(errno);
957                 DEBUG(0,("change_dir_owner_to_parent: failed to change "
958                          "current working directory to %s. Error "
959                          "was %s\n", smb_dname->base_name, strerror(errno) ));
960                 goto chdir;
961         }
962
963         smb_fname_cwd = synthetic_smb_fname(ctx, ".", NULL, NULL, 0);
964         if (smb_fname_cwd == NULL) {
965                 status = NT_STATUS_NO_MEMORY;
966                 goto chdir;
967         }
968
969         ret = SMB_VFS_STAT(conn, smb_fname_cwd);
970         if (ret == -1) {
971                 status = map_nt_error_from_unix(errno);
972                 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
973                          "directory '.' (%s) Error was %s\n",
974                          smb_dname->base_name, strerror(errno)));
975                 goto chdir;
976         }
977
978         /* Ensure we're pointing at the same place. */
979         if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
980             smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
981                 DEBUG(0,("change_dir_owner_to_parent: "
982                          "device/inode on directory %s changed. "
983                          "Refusing to chown !\n",
984                         smb_dname->base_name ));
985                 status = NT_STATUS_ACCESS_DENIED;
986                 goto chdir;
987         }
988
989         if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
990                 /* Already this uid - no need to change. */
991                 DEBUG(10,("change_dir_owner_to_parent: directory %s "
992                         "is already owned by uid %d\n",
993                         smb_dname->base_name,
994                         (int)smb_fname_cwd->st.st_ex_uid ));
995                 status = NT_STATUS_OK;
996                 goto chdir;
997         }
998
999         become_root();
1000         ret = SMB_VFS_LCHOWN(conn,
1001                         smb_fname_cwd,
1002                         smb_fname_parent->st.st_ex_uid,
1003                         (gid_t)-1);
1004         unbecome_root();
1005         if (ret == -1) {
1006                 status = map_nt_error_from_unix(errno);
1007                 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
1008                           "directory %s to parent directory uid %u. "
1009                           "Error was %s\n",
1010                           smb_dname->base_name,
1011                           (unsigned int)smb_fname_parent->st.st_ex_uid,
1012                           strerror(errno) ));
1013         } else {
1014                 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
1015                         "directory %s to parent directory uid %u.\n",
1016                         smb_dname->base_name,
1017                         (unsigned int)smb_fname_parent->st.st_ex_uid ));
1018                 /* Ensure the uid entry is updated. */
1019                 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
1020         }
1021
1022  chdir:
1023         vfs_ChDir(conn, saved_dir_fname);
1024  out:
1025         TALLOC_FREE(saved_dir_fname);
1026         TALLOC_FREE(smb_fname_parent);
1027         TALLOC_FREE(smb_fname_cwd);
1028         return status;
1029 }
1030
1031 /****************************************************************************
1032  Open a file - returning a guaranteed ATOMIC indication of if the
1033  file was created or not.
1034 ****************************************************************************/
1035
1036 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
1037                         files_struct *fsp,
1038                         int flags,
1039                         mode_t mode,
1040                         bool *file_created)
1041 {
1042         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1043         NTSTATUS retry_status;
1044         bool file_existed = VALID_STAT(fsp->fsp_name->st);
1045         int curr_flags;
1046
1047         *file_created = false;
1048
1049         if (!(flags & O_CREAT)) {
1050                 /*
1051                  * We're not creating the file, just pass through.
1052                  */
1053                 return fd_open(conn, fsp, flags, mode);
1054         }
1055
1056         if (flags & O_EXCL) {
1057                 /*
1058                  * Fail if already exists, just pass through.
1059                  */
1060                 status = fd_open(conn, fsp, flags, mode);
1061
1062                 /*
1063                  * Here we've opened with O_CREAT|O_EXCL. If that went
1064                  * NT_STATUS_OK, we *know* we created this file.
1065                  */
1066                 *file_created = NT_STATUS_IS_OK(status);
1067
1068                 return status;
1069         }
1070
1071         /*
1072          * Now it gets tricky. We have O_CREAT, but not O_EXCL.
1073          * To know absolutely if we created the file or not,
1074          * we can never call O_CREAT without O_EXCL. So if
1075          * we think the file existed, try without O_CREAT|O_EXCL.
1076          * If we think the file didn't exist, try with
1077          * O_CREAT|O_EXCL.
1078          *
1079          * The big problem here is dangling symlinks. Opening
1080          * without O_NOFOLLOW means both bad symlink
1081          * and missing path return -1, ENOENT from open(). As POSIX
1082          * is pathname based it's not possible to tell
1083          * the difference between these two cases in a
1084          * non-racy way, so change to try only two attempts before
1085          * giving up.
1086          *
1087          * We don't have this problem for the O_NOFOLLOW
1088          * case as it just returns NT_STATUS_OBJECT_PATH_NOT_FOUND
1089          * mapped from the ELOOP POSIX error.
1090          */
1091
1092         curr_flags = flags;
1093
1094         if (file_existed) {
1095                 curr_flags &= ~(O_CREAT);
1096                 retry_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1097         } else {
1098                 curr_flags |= O_EXCL;
1099                 retry_status = NT_STATUS_OBJECT_NAME_COLLISION;
1100         }
1101
1102         status = fd_open(conn, fsp, curr_flags, mode);
1103         if (NT_STATUS_IS_OK(status)) {
1104                 if (!file_existed) {
1105                         *file_created = true;
1106                 }
1107                 return NT_STATUS_OK;
1108         }
1109         if (!NT_STATUS_EQUAL(status, retry_status)) {
1110                 return status;
1111         }
1112
1113         curr_flags = flags;
1114
1115         /*
1116          * Keep file_existed up to date for clarity.
1117          */
1118         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1119                 file_existed = false;
1120                 curr_flags |= O_EXCL;
1121                 DBG_DEBUG("file %s did not exist. Retry.\n",
1122                         smb_fname_str_dbg(fsp->fsp_name));
1123         } else {
1124                 file_existed = true;
1125                 curr_flags &= ~(O_CREAT);
1126                 DBG_DEBUG("file %s existed. Retry.\n",
1127                         smb_fname_str_dbg(fsp->fsp_name));
1128         }
1129
1130         status = fd_open(conn, fsp, curr_flags, mode);
1131
1132         if (NT_STATUS_IS_OK(status) && (!file_existed)) {
1133                 *file_created = true;
1134         }
1135
1136         return status;
1137 }
1138
1139 /****************************************************************************
1140  Open a file.
1141 ****************************************************************************/
1142
1143 static NTSTATUS open_file(files_struct *fsp,
1144                           connection_struct *conn,
1145                           struct smb_request *req,
1146                           const char *parent_dir,
1147                           int flags,
1148                           mode_t unx_mode,
1149                           uint32_t access_mask, /* client requested access mask. */
1150                           uint32_t open_access_mask, /* what we're actually using in the open. */
1151                           bool *p_file_created)
1152 {
1153         struct smb_filename *smb_fname = fsp->fsp_name;
1154         NTSTATUS status = NT_STATUS_OK;
1155         int accmode = (flags & O_ACCMODE);
1156         int local_flags = flags;
1157         bool file_existed = VALID_STAT(fsp->fsp_name->st);
1158
1159         fsp->fh->fd = -1;
1160         errno = EPERM;
1161
1162         /* Check permissions */
1163
1164         /*
1165          * This code was changed after seeing a client open request 
1166          * containing the open mode of (DENY_WRITE/read-only) with
1167          * the 'create if not exist' bit set. The previous code
1168          * would fail to open the file read only on a read-only share
1169          * as it was checking the flags parameter  directly against O_RDONLY,
1170          * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
1171          * JRA.
1172          */
1173
1174         if (!CAN_WRITE(conn)) {
1175                 /* It's a read-only share - fail if we wanted to write. */
1176                 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
1177                         DEBUG(3,("Permission denied opening %s\n",
1178                                  smb_fname_str_dbg(smb_fname)));
1179                         return NT_STATUS_ACCESS_DENIED;
1180                 }
1181                 if (flags & O_CREAT) {
1182                         /* We don't want to write - but we must make sure that
1183                            O_CREAT doesn't create the file if we have write
1184                            access into the directory.
1185                         */
1186                         flags &= ~(O_CREAT|O_EXCL);
1187                         local_flags &= ~(O_CREAT|O_EXCL);
1188                 }
1189         }
1190
1191         /*
1192          * This little piece of insanity is inspired by the
1193          * fact that an NT client can open a file for O_RDONLY,
1194          * but set the create disposition to FILE_EXISTS_TRUNCATE.
1195          * If the client *can* write to the file, then it expects to
1196          * truncate the file, even though it is opening for readonly.
1197          * Quicken uses this stupid trick in backup file creation...
1198          * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
1199          * for helping track this one down. It didn't bite us in 2.0.x
1200          * as we always opened files read-write in that release. JRA.
1201          */
1202
1203         if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
1204                 DEBUG(10,("open_file: truncate requested on read-only open "
1205                           "for file %s\n", smb_fname_str_dbg(smb_fname)));
1206                 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
1207         }
1208
1209         if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
1210             (!file_existed && (local_flags & O_CREAT)) ||
1211             ((local_flags & O_TRUNC) == O_TRUNC) ) {
1212                 const char *wild;
1213                 int ret;
1214
1215 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
1216                 /*
1217                  * We would block on opening a FIFO with no one else on the
1218                  * other end. Do what we used to do and add O_NONBLOCK to the
1219                  * open flags. JRA.
1220                  */
1221
1222                 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
1223                         local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
1224                         local_flags |= O_NONBLOCK;
1225                 }
1226 #endif
1227
1228                 /* Don't create files with Microsoft wildcard characters. */
1229                 if (fsp->base_fsp) {
1230                         /*
1231                          * wildcard characters are allowed in stream names
1232                          * only test the basefilename
1233                          */
1234                         wild = fsp->base_fsp->fsp_name->base_name;
1235                 } else {
1236                         wild = smb_fname->base_name;
1237                 }
1238                 if ((local_flags & O_CREAT) && !file_existed &&
1239                     !(fsp->posix_flags & FSP_POSIX_FLAGS_PATHNAMES) &&
1240                     ms_has_wild(wild))  {
1241                         return NT_STATUS_OBJECT_NAME_INVALID;
1242                 }
1243
1244                 /* Can we access this file ? */
1245                 if (!fsp->base_fsp) {
1246                         /* Only do this check on non-stream open. */
1247                         if (file_existed) {
1248                                 status = smbd_check_access_rights(conn,
1249                                                 smb_fname,
1250                                                 false,
1251                                                 access_mask);
1252
1253                                 if (!NT_STATUS_IS_OK(status)) {
1254                                         DEBUG(10, ("open_file: "
1255                                                    "smbd_check_access_rights "
1256                                                    "on file %s returned %s\n",
1257                                                    smb_fname_str_dbg(smb_fname),
1258                                                    nt_errstr(status)));
1259                                 }
1260
1261                                 if (!NT_STATUS_IS_OK(status) &&
1262                                     !NT_STATUS_EQUAL(status,
1263                                         NT_STATUS_OBJECT_NAME_NOT_FOUND))
1264                                 {
1265                                         return status;
1266                                 }
1267
1268                                 if (NT_STATUS_EQUAL(status,
1269                                         NT_STATUS_OBJECT_NAME_NOT_FOUND))
1270                                 {
1271                                         DEBUG(10, ("open_file: "
1272                                                 "file %s vanished since we "
1273                                                 "checked for existence.\n",
1274                                                 smb_fname_str_dbg(smb_fname)));
1275                                         file_existed = false;
1276                                         SET_STAT_INVALID(fsp->fsp_name->st);
1277                                 }
1278                         }
1279
1280                         if (!file_existed) {
1281                                 if (!(local_flags & O_CREAT)) {
1282                                         /* File didn't exist and no O_CREAT. */
1283                                         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1284                                 }
1285
1286                                 status = check_parent_access(conn,
1287                                                              smb_fname,
1288                                                              SEC_DIR_ADD_FILE);
1289                                 if (!NT_STATUS_IS_OK(status)) {
1290                                         DEBUG(10, ("open_file: "
1291                                                    "check_parent_access on "
1292                                                    "file %s returned %s\n",
1293                                                    smb_fname_str_dbg(smb_fname),
1294                                                    nt_errstr(status) ));
1295                                         return status;
1296                                 }
1297                         }
1298                 }
1299
1300                 /*
1301                  * Actually do the open - if O_TRUNC is needed handle it
1302                  * below under the share mode lock.
1303                  */
1304                 status = fd_open_atomic(conn, fsp, local_flags & ~O_TRUNC,
1305                                 unx_mode, p_file_created);
1306                 if (!NT_STATUS_IS_OK(status)) {
1307                         DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
1308                                  "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
1309                                  nt_errstr(status),local_flags,flags));
1310                         return status;
1311                 }
1312
1313                 if (local_flags & O_NONBLOCK) {
1314                         /*
1315                          * GPFS can return ETIMEDOUT for pread on
1316                          * nonblocking file descriptors when files
1317                          * migrated to tape need to be recalled. I
1318                          * could imagine this happens elsehwere
1319                          * too. With blocking file descriptors this
1320                          * does not happen.
1321                          */
1322                         ret = set_blocking(fsp->fh->fd, true);
1323                         if (ret == -1) {
1324                                 status = map_nt_error_from_unix(errno);
1325                                 DBG_WARNING("Could not set fd to blocking: "
1326                                             "%s\n", strerror(errno));
1327                                 fd_close(fsp);
1328                                 return status;
1329                         }
1330                 }
1331
1332                 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
1333                 if (ret == -1) {
1334                         /* If we have an fd, this stat should succeed. */
1335                         DEBUG(0,("Error doing fstat on open file %s "
1336                                 "(%s)\n",
1337                                 smb_fname_str_dbg(smb_fname),
1338                                 strerror(errno) ));
1339                         status = map_nt_error_from_unix(errno);
1340                         fd_close(fsp);
1341                         return status;
1342                 }
1343
1344                 if (*p_file_created) {
1345                         /* We created this file. */
1346
1347                         bool need_re_stat = false;
1348                         /* Do all inheritance work after we've
1349                            done a successful fstat call and filled
1350                            in the stat struct in fsp->fsp_name. */
1351
1352                         /* Inherit the ACL if required */
1353                         if (lp_inherit_permissions(SNUM(conn))) {
1354                                 inherit_access_posix_acl(conn, parent_dir,
1355                                                          smb_fname,
1356                                                          unx_mode);
1357                                 need_re_stat = true;
1358                         }
1359
1360                         /* Change the owner if required. */
1361                         if (lp_inherit_owner(SNUM(conn)) != INHERIT_OWNER_NO) {
1362                                 change_file_owner_to_parent(conn, parent_dir,
1363                                                             fsp);
1364                                 need_re_stat = true;
1365                         }
1366
1367                         if (need_re_stat) {
1368                                 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
1369                                 /* If we have an fd, this stat should succeed. */
1370                                 if (ret == -1) {
1371                                         DEBUG(0,("Error doing fstat on open file %s "
1372                                                  "(%s)\n",
1373                                                  smb_fname_str_dbg(smb_fname),
1374                                                  strerror(errno) ));
1375                                 }
1376                         }
1377
1378                         notify_fname(conn, NOTIFY_ACTION_ADDED,
1379                                      FILE_NOTIFY_CHANGE_FILE_NAME,
1380                                      smb_fname->base_name);
1381                 }
1382         } else {
1383                 fsp->fh->fd = -1; /* What we used to call a stat open. */
1384                 if (!file_existed) {
1385                         /* File must exist for a stat open. */
1386                         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1387                 }
1388
1389                 status = smbd_check_access_rights(conn,
1390                                 smb_fname,
1391                                 false,
1392                                 access_mask);
1393
1394                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1395                                 (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
1396                                 S_ISLNK(smb_fname->st.st_ex_mode)) {
1397                         /* This is a POSIX stat open for delete
1398                          * or rename on a symlink that points
1399                          * nowhere. Allow. */
1400                         DEBUG(10,("open_file: allowing POSIX "
1401                                   "open on bad symlink %s\n",
1402                                   smb_fname_str_dbg(smb_fname)));
1403                         status = NT_STATUS_OK;
1404                 }
1405
1406                 if (!NT_STATUS_IS_OK(status)) {
1407                         DEBUG(10,("open_file: "
1408                                 "smbd_check_access_rights on file "
1409                                 "%s returned %s\n",
1410                                 smb_fname_str_dbg(smb_fname),
1411                                 nt_errstr(status) ));
1412                         return status;
1413                 }
1414         }
1415
1416         /*
1417          * POSIX allows read-only opens of directories. We don't
1418          * want to do this (we use a different code path for this)
1419          * so catch a directory open and return an EISDIR. JRA.
1420          */
1421
1422         if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1423                 fd_close(fsp);
1424                 errno = EISDIR;
1425                 return NT_STATUS_FILE_IS_A_DIRECTORY;
1426         }
1427
1428         fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1429         fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
1430         fsp->file_pid = req ? req->smbpid : 0;
1431         fsp->can_lock = True;
1432         fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
1433         fsp->can_write =
1434                 CAN_WRITE(conn) &&
1435                 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1436         fsp->print_file = NULL;
1437         fsp->modified = False;
1438         fsp->sent_oplock_break = NO_BREAK_SENT;
1439         fsp->is_directory = False;
1440         if (conn->aio_write_behind_list &&
1441             is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
1442                        conn->case_sensitive)) {
1443                 fsp->aio_write_behind = True;
1444         }
1445
1446         fsp->wcp = NULL; /* Write cache pointer. */
1447
1448         DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1449                  conn->session_info->unix_info->unix_name,
1450                  smb_fname_str_dbg(smb_fname),
1451                  BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
1452                  conn->num_files_open));
1453
1454         errno = 0;
1455         return NT_STATUS_OK;
1456 }
1457
1458 /****************************************************************************
1459  Check if we can open a file with a share mode.
1460  Returns True if conflict, False if not.
1461 ****************************************************************************/
1462
1463 static bool share_conflict(struct share_mode_entry *entry,
1464                            uint32_t access_mask,
1465                            uint32_t share_access)
1466 {
1467         DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1468                   "entry->share_access = 0x%x, "
1469                   "entry->private_options = 0x%x\n",
1470                   (unsigned int)entry->access_mask,
1471                   (unsigned int)entry->share_access,
1472                   (unsigned int)entry->private_options));
1473
1474         if (server_id_is_disconnected(&entry->pid)) {
1475                 /*
1476                  * note: cleanup should have been done by
1477                  * delay_for_batch_oplocks()
1478                  */
1479                 return false;
1480         }
1481
1482         DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1483                   (unsigned int)access_mask, (unsigned int)share_access));
1484
1485         if ((entry->access_mask & (FILE_WRITE_DATA|
1486                                    FILE_APPEND_DATA|
1487                                    FILE_READ_DATA|
1488                                    FILE_EXECUTE|
1489                                    DELETE_ACCESS)) == 0) {
1490                 DEBUG(10,("share_conflict: No conflict due to "
1491                           "entry->access_mask = 0x%x\n",
1492                           (unsigned int)entry->access_mask ));
1493                 return False;
1494         }
1495
1496         if ((access_mask & (FILE_WRITE_DATA|
1497                             FILE_APPEND_DATA|
1498                             FILE_READ_DATA|
1499                             FILE_EXECUTE|
1500                             DELETE_ACCESS)) == 0) {
1501                 DEBUG(10,("share_conflict: No conflict due to "
1502                           "access_mask = 0x%x\n",
1503                           (unsigned int)access_mask ));
1504                 return False;
1505         }
1506
1507 #if 1 /* JRA TEST - Superdebug. */
1508 #define CHECK_MASK(num, am, right, sa, share) \
1509         DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1510                 (unsigned int)(num), (unsigned int)(am), \
1511                 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1512         DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1513                 (unsigned int)(num), (unsigned int)(sa), \
1514                 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1515         if (((am) & (right)) && !((sa) & (share))) { \
1516                 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1517 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1518                         (unsigned int)(share) )); \
1519                 return True; \
1520         }
1521 #else
1522 #define CHECK_MASK(num, am, right, sa, share) \
1523         if (((am) & (right)) && !((sa) & (share))) { \
1524                 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1525 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1526                         (unsigned int)(share) )); \
1527                 return True; \
1528         }
1529 #endif
1530
1531         CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1532                    share_access, FILE_SHARE_WRITE);
1533         CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1534                    entry->share_access, FILE_SHARE_WRITE);
1535
1536         CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1537                    share_access, FILE_SHARE_READ);
1538         CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1539                    entry->share_access, FILE_SHARE_READ);
1540
1541         CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1542                    share_access, FILE_SHARE_DELETE);
1543         CHECK_MASK(6, access_mask, DELETE_ACCESS,
1544                    entry->share_access, FILE_SHARE_DELETE);
1545
1546         DEBUG(10,("share_conflict: No conflict.\n"));
1547         return False;
1548 }
1549
1550 #if defined(DEVELOPER)
1551 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1552                                       const struct file_id id,
1553                                       int num,
1554                                       struct share_mode_entry *share_entry)
1555 {
1556         struct server_id self = messaging_server_id(sconn->msg_ctx);
1557         files_struct *fsp;
1558
1559         if (!serverid_equal(&self, &share_entry->pid)) {
1560                 return;
1561         }
1562
1563         if (share_entry->op_mid == 0) {
1564                 /* INTERNAL_OPEN_ONLY */
1565                 return;
1566         }
1567
1568         if (!is_valid_share_mode_entry(share_entry)) {
1569                 return;
1570         }
1571
1572         fsp = file_find_dif(sconn, id, share_entry->share_file_id);
1573         if (!fsp) {
1574                 DBG_ERR("PANIC : %s\n",
1575                         share_mode_str(talloc_tos(), num, &id,
1576                                        share_entry));
1577                 smb_panic("validate_my_share_entries: Cannot match a "
1578                           "share entry with an open file\n");
1579         }
1580
1581         if (((uint16_t)fsp->oplock_type) != share_entry->op_type) {
1582                 goto panic;
1583         }
1584
1585         return;
1586
1587  panic:
1588         {
1589                 char *str;
1590                 DBG_ERR("validate_my_share_entries: PANIC : %s\n",
1591                         share_mode_str(talloc_tos(), num, &id,
1592                                        share_entry));
1593                 str = talloc_asprintf(talloc_tos(),
1594                         "validate_my_share_entries: "
1595                         "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1596                          fsp->fsp_name->base_name,
1597                          (unsigned int)fsp->oplock_type,
1598                          (unsigned int)share_entry->op_type );
1599                 smb_panic(str);
1600         }
1601 }
1602 #endif
1603
1604 bool is_stat_open(uint32_t access_mask)
1605 {
1606         const uint32_t stat_open_bits =
1607                 (SYNCHRONIZE_ACCESS|
1608                  FILE_READ_ATTRIBUTES|
1609                  FILE_WRITE_ATTRIBUTES);
1610
1611         return (((access_mask &  stat_open_bits) != 0) &&
1612                 ((access_mask & ~stat_open_bits) == 0));
1613 }
1614
1615 static bool has_delete_on_close(struct share_mode_lock *lck,
1616                                 uint32_t name_hash)
1617 {
1618         struct share_mode_data *d = lck->data;
1619         uint32_t i;
1620
1621         if (d->num_share_modes == 0) {
1622                 return false;
1623         }
1624         if (!is_delete_on_close_set(lck, name_hash)) {
1625                 return false;
1626         }
1627         for (i=0; i<d->num_share_modes; i++) {
1628                 if (!share_mode_stale_pid(d, i)) {
1629                         return true;
1630                 }
1631         }
1632         return false;
1633 }
1634
1635 /****************************************************************************
1636  Deal with share modes
1637  Invariant: Share mode must be locked on entry and exit.
1638  Returns -1 on error, or number of share modes on success (may be zero).
1639 ****************************************************************************/
1640
1641 static NTSTATUS open_mode_check(connection_struct *conn,
1642                                 struct share_mode_lock *lck,
1643                                 uint32_t access_mask,
1644                                 uint32_t share_access)
1645 {
1646         uint32_t i;
1647
1648         if(lck->data->num_share_modes == 0) {
1649                 return NT_STATUS_OK;
1650         }
1651
1652         if (is_stat_open(access_mask)) {
1653                 /* Stat open that doesn't trigger oplock breaks or share mode
1654                  * checks... ! JRA. */
1655                 return NT_STATUS_OK;
1656         }
1657
1658         /*
1659          * Check if the share modes will give us access.
1660          */
1661
1662 #if defined(DEVELOPER)
1663         for(i = 0; i < lck->data->num_share_modes; i++) {
1664                 validate_my_share_entries(conn->sconn, lck->data->id, i,
1665                                           &lck->data->share_modes[i]);
1666         }
1667 #endif
1668
1669         /* Now we check the share modes, after any oplock breaks. */
1670         for(i = 0; i < lck->data->num_share_modes; i++) {
1671
1672                 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1673                         continue;
1674                 }
1675
1676                 /* someone else has a share lock on it, check to see if we can
1677                  * too */
1678                 if (share_conflict(&lck->data->share_modes[i],
1679                                    access_mask, share_access)) {
1680
1681                         if (share_mode_stale_pid(lck->data, i)) {
1682                                 continue;
1683                         }
1684
1685                         return NT_STATUS_SHARING_VIOLATION;
1686                 }
1687         }
1688
1689         return NT_STATUS_OK;
1690 }
1691
1692 /*
1693  * Send a break message to the oplock holder and delay the open for
1694  * our client.
1695  */
1696
1697 NTSTATUS send_break_message(struct messaging_context *msg_ctx,
1698                             const struct file_id *id,
1699                             const struct share_mode_entry *exclusive,
1700                             uint16_t break_to)
1701 {
1702         NTSTATUS status;
1703         char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1704         struct server_id_buf tmp;
1705
1706         DEBUG(10, ("Sending break request to PID %s\n",
1707                    server_id_str_buf(exclusive->pid, &tmp)));
1708
1709         /* Create the message. */
1710         share_mode_entry_to_message(msg, id, exclusive);
1711
1712         /* Overload entry->op_type */
1713         /*
1714          * This is a cut from uint32_t to uint16_t, but so far only the lower 3
1715          * bits (LEASE_WRITE/HANDLE/READ are used anyway.
1716          */
1717         SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, break_to);
1718
1719         status = messaging_send_buf(msg_ctx, exclusive->pid,
1720                                     MSG_SMB_BREAK_REQUEST,
1721                                     (uint8_t *)msg, sizeof(msg));
1722         if (!NT_STATUS_IS_OK(status)) {
1723                 DEBUG(3, ("Could not send oplock break message: %s\n",
1724                           nt_errstr(status)));
1725         }
1726
1727         return status;
1728 }
1729
1730 /*
1731  * Do internal consistency checks on the share mode for a file.
1732  */
1733
1734 static bool validate_oplock_types(struct share_mode_lock *lck)
1735 {
1736         struct share_mode_data *d = lck->data;
1737         bool batch = false;
1738         bool ex_or_batch = false;
1739         bool level2 = false;
1740         bool no_oplock = false;
1741         uint32_t num_non_stat_opens = 0;
1742         uint32_t i;
1743
1744         for (i=0; i<d->num_share_modes; i++) {
1745                 struct share_mode_entry *e = &d->share_modes[i];
1746
1747                 if (!is_valid_share_mode_entry(e)) {
1748                         continue;
1749                 }
1750
1751                 if (e->op_mid == 0) {
1752                         /* INTERNAL_OPEN_ONLY */
1753                         continue;
1754                 }
1755
1756                 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1757                         /* We ignore stat opens in the table - they
1758                            always have NO_OPLOCK and never get or
1759                            cause breaks. JRA. */
1760                         continue;
1761                 }
1762
1763                 num_non_stat_opens += 1;
1764
1765                 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1766                         /* batch - can only be one. */
1767                         if (share_mode_stale_pid(d, i)) {
1768                                 DEBUG(10, ("Found stale batch oplock\n"));
1769                                 continue;
1770                         }
1771                         if (ex_or_batch || batch || level2 || no_oplock) {
1772                                 DEBUG(0, ("Bad batch oplock entry %u.",
1773                                           (unsigned)i));
1774                                 return false;
1775                         }
1776                         batch = true;
1777                 }
1778
1779                 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1780                         if (share_mode_stale_pid(d, i)) {
1781                                 DEBUG(10, ("Found stale duplicate oplock\n"));
1782                                 continue;
1783                         }
1784                         /* Exclusive or batch - can only be one. */
1785                         if (ex_or_batch || level2 || no_oplock) {
1786                                 DEBUG(0, ("Bad exclusive or batch oplock "
1787                                           "entry %u.", (unsigned)i));
1788                                 return false;
1789                         }
1790                         ex_or_batch = true;
1791                 }
1792
1793                 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1794                         if (batch || ex_or_batch) {
1795                                 if (share_mode_stale_pid(d, i)) {
1796                                         DEBUG(10, ("Found stale LevelII "
1797                                                    "oplock\n"));
1798                                         continue;
1799                                 }
1800                                 DEBUG(0, ("Bad levelII oplock entry %u.",
1801                                           (unsigned)i));
1802                                 return false;
1803                         }
1804                         level2 = true;
1805                 }
1806
1807                 if (e->op_type == NO_OPLOCK) {
1808                         if (batch || ex_or_batch) {
1809                                 if (share_mode_stale_pid(d, i)) {
1810                                         DEBUG(10, ("Found stale NO_OPLOCK "
1811                                                    "entry\n"));
1812                                         continue;
1813                                 }
1814                                 DEBUG(0, ("Bad no oplock entry %u.",
1815                                           (unsigned)i));
1816                                 return false;
1817                         }
1818                         no_oplock = true;
1819                 }
1820         }
1821
1822         remove_stale_share_mode_entries(d);
1823
1824         if ((batch || ex_or_batch) && (num_non_stat_opens != 1)) {
1825                 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1826                           (int)batch, (int)ex_or_batch,
1827                           (int)d->num_share_modes));
1828                 return false;
1829         }
1830
1831         return true;
1832 }
1833
1834 static bool delay_for_oplock(files_struct *fsp,
1835                              int oplock_request,
1836                              const struct smb2_lease *lease,
1837                              struct share_mode_lock *lck,
1838                              bool have_sharing_violation,
1839                              uint32_t create_disposition,
1840                              bool first_open_attempt)
1841 {
1842         struct share_mode_data *d = lck->data;
1843         uint32_t i;
1844         bool delay = false;
1845         bool will_overwrite;
1846
1847         if ((oplock_request & INTERNAL_OPEN_ONLY) ||
1848             is_stat_open(fsp->access_mask)) {
1849                 return false;
1850         }
1851
1852         switch (create_disposition) {
1853         case FILE_SUPERSEDE:
1854         case FILE_OVERWRITE:
1855         case FILE_OVERWRITE_IF:
1856                 will_overwrite = true;
1857                 break;
1858         default:
1859                 will_overwrite = false;
1860                 break;
1861         }
1862
1863         for (i=0; i<d->num_share_modes; i++) {
1864                 struct share_mode_entry *e = &d->share_modes[i];
1865                 bool e_is_lease = (e->op_type == LEASE_OPLOCK);
1866                 struct share_mode_lease *l = NULL;
1867                 uint32_t e_lease_type = get_lease_type(d, e);
1868                 uint32_t break_to;
1869                 uint32_t delay_mask = 0;
1870                 bool lease_is_breaking = false;
1871
1872                 if (e_is_lease) {
1873                         l = &d->leases[e->lease_idx];
1874                         lease_is_breaking = l->breaking;
1875                 }
1876
1877                 if (have_sharing_violation) {
1878                         delay_mask = SMB2_LEASE_HANDLE;
1879                 } else {
1880                         delay_mask = SMB2_LEASE_WRITE;
1881                 }
1882
1883                 break_to = e_lease_type & ~delay_mask;
1884
1885                 if (will_overwrite) {
1886                         /*
1887                          * we'll decide about SMB2_LEASE_READ later.
1888                          *
1889                          * Maybe the break will be deferred
1890                          */
1891                         break_to &= ~SMB2_LEASE_HANDLE;
1892                 }
1893
1894                 DEBUG(10, ("entry %u: e_lease_type %u, will_overwrite: %u\n",
1895                            (unsigned)i, (unsigned)e_lease_type,
1896                            (unsigned)will_overwrite));
1897
1898                 if (e_is_lease && lease != NULL) {
1899                         bool ign;
1900
1901                         ign = smb2_lease_equal(fsp_client_guid(fsp),
1902                                                &lease->lease_key,
1903                                                &l->client_guid,
1904                                                &l->lease_key);
1905                         if (ign) {
1906                                 continue;
1907                         }
1908                 }
1909
1910                 if ((e_lease_type & ~break_to) == 0) {
1911                         if (lease_is_breaking) {
1912                                 delay = true;
1913                         }
1914                         continue;
1915                 }
1916
1917                 if (share_mode_stale_pid(d, i)) {
1918                         continue;
1919                 }
1920
1921                 if (will_overwrite) {
1922                         /*
1923                          * If we break anyway break to NONE directly.
1924                          * Otherwise vfs_set_filelen() will trigger the
1925                          * break.
1926                          */
1927                         break_to &= ~(SMB2_LEASE_READ|SMB2_LEASE_WRITE);
1928                 }
1929
1930                 if (!e_is_lease) {
1931                         /*
1932                          * Oplocks only support breaking to R or NONE.
1933                          */
1934                         break_to &= ~(SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1935                 }
1936
1937                 DEBUG(10, ("breaking from %d to %d\n",
1938                            (int)e_lease_type, (int)break_to));
1939                 send_break_message(fsp->conn->sconn->msg_ctx, &fsp->file_id,
1940                                    e, break_to);
1941                 if (e_lease_type & delay_mask) {
1942                         delay = true;
1943                 }
1944                 if (lease_is_breaking && !first_open_attempt) {
1945                         delay = true;
1946                 }
1947                 continue;
1948         }
1949
1950         return delay;
1951 }
1952
1953 static bool file_has_brlocks(files_struct *fsp)
1954 {
1955         struct byte_range_lock *br_lck;
1956
1957         br_lck = brl_get_locks_readonly(fsp);
1958         if (!br_lck)
1959                 return false;
1960
1961         return (brl_num_locks(br_lck) > 0);
1962 }
1963
1964 int find_share_mode_lease(struct share_mode_data *d,
1965                           const struct GUID *client_guid,
1966                           const struct smb2_lease_key *key)
1967 {
1968         uint32_t i;
1969
1970         for (i=0; i<d->num_leases; i++) {
1971                 struct share_mode_lease *l = &d->leases[i];
1972
1973                 if (smb2_lease_equal(client_guid,
1974                                      key,
1975                                      &l->client_guid,
1976                                      &l->lease_key)) {
1977                         return i;
1978                 }
1979         }
1980
1981         return -1;
1982 }
1983
1984 struct fsp_lease *find_fsp_lease(struct files_struct *new_fsp,
1985                                  const struct smb2_lease_key *key,
1986                                  uint32_t current_state,
1987                                  uint16_t lease_version,
1988                                  uint16_t lease_epoch)
1989 {
1990         struct files_struct *fsp;
1991
1992         /*
1993          * TODO: Measure how expensive this loop is with thousands of open
1994          * handles...
1995          */
1996
1997         for (fsp = file_find_di_first(new_fsp->conn->sconn, new_fsp->file_id);
1998              fsp != NULL;
1999              fsp = file_find_di_next(fsp)) {
2000
2001                 if (fsp == new_fsp) {
2002                         continue;
2003                 }
2004                 if (fsp->oplock_type != LEASE_OPLOCK) {
2005                         continue;
2006                 }
2007                 if (smb2_lease_key_equal(&fsp->lease->lease.lease_key, key)) {
2008                         fsp->lease->ref_count += 1;
2009                         return fsp->lease;
2010                 }
2011         }
2012
2013         /* Not found - must be leased in another smbd. */
2014         new_fsp->lease = talloc_zero(new_fsp->conn->sconn, struct fsp_lease);
2015         if (new_fsp->lease == NULL) {
2016                 return NULL;
2017         }
2018         new_fsp->lease->ref_count = 1;
2019         new_fsp->lease->sconn = new_fsp->conn->sconn;
2020         new_fsp->lease->lease.lease_key = *key;
2021         new_fsp->lease->lease.lease_state = current_state;
2022         /*
2023          * We internally treat all leases as V2 and update
2024          * the epoch, but when sending breaks it matters if
2025          * the requesting lease was v1 or v2.
2026          */
2027         new_fsp->lease->lease.lease_version = lease_version;
2028         new_fsp->lease->lease.lease_epoch = lease_epoch;
2029         return new_fsp->lease;
2030 }
2031
2032 static NTSTATUS grant_fsp_lease(struct files_struct *fsp,
2033                                 struct share_mode_lock *lck,
2034                                 const struct smb2_lease *lease,
2035                                 uint32_t *p_lease_idx,
2036                                 uint32_t granted)
2037 {
2038         struct share_mode_data *d = lck->data;
2039         const struct GUID *client_guid = fsp_client_guid(fsp);
2040         struct share_mode_lease *tmp;
2041         NTSTATUS status;
2042         int idx;
2043
2044         idx = find_share_mode_lease(d, client_guid, &lease->lease_key);
2045
2046         if (idx != -1) {
2047                 struct share_mode_lease *l = &d->leases[idx];
2048                 bool do_upgrade;
2049                 uint32_t existing, requested;
2050
2051                 fsp->lease = find_fsp_lease(
2052                         fsp,
2053                         &lease->lease_key,
2054                         l->current_state,
2055                         l->lease_version,
2056                         l->epoch);
2057                 if (fsp->lease == NULL) {
2058                         DEBUG(1, ("Did not find existing lease for file %s\n",
2059                                   fsp_str_dbg(fsp)));
2060                         return NT_STATUS_NO_MEMORY;
2061                 }
2062
2063                 *p_lease_idx = idx;
2064
2065                 /*
2066                  * Upgrade only if the requested lease is a strict upgrade.
2067                  */
2068                 existing = l->current_state;
2069                 requested = lease->lease_state;
2070
2071                 /*
2072                  * Tricky: This test makes sure that "requested" is a
2073                  * strict bitwise superset of "existing".
2074                  */
2075                 do_upgrade = ((existing & requested) == existing);
2076
2077                 /*
2078                  * Upgrade only if there's a change.
2079                  */
2080                 do_upgrade &= (granted != existing);
2081
2082                 /*
2083                  * Upgrade only if other leases don't prevent what was asked
2084                  * for.
2085                  */
2086                 do_upgrade &= (granted == requested);
2087
2088                 /*
2089                  * only upgrade if we are not in breaking state
2090                  */
2091                 do_upgrade &= !l->breaking;
2092
2093                 DEBUG(10, ("existing=%"PRIu32", requested=%"PRIu32", "
2094                            "granted=%"PRIu32", do_upgrade=%d\n",
2095                            existing, requested, granted, (int)do_upgrade));
2096
2097                 if (do_upgrade) {
2098                         l->current_state = granted;
2099                         l->epoch += 1;
2100                 }
2101
2102                 /* Ensure we're in sync with current lease state. */
2103                 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
2104                 return NT_STATUS_OK;
2105         }
2106
2107         /*
2108          * Create new lease
2109          */
2110
2111         tmp = talloc_realloc(d, d->leases, struct share_mode_lease,
2112                              d->num_leases+1);
2113         if (tmp == NULL) {
2114                 /*
2115                  * See [MS-SMB2]
2116                  */
2117                 return NT_STATUS_INSUFFICIENT_RESOURCES;
2118         }
2119         d->leases = tmp;
2120
2121         fsp->lease = talloc_zero(fsp->conn->sconn, struct fsp_lease);
2122         if (fsp->lease == NULL) {
2123                 return NT_STATUS_INSUFFICIENT_RESOURCES;
2124         }
2125         fsp->lease->ref_count = 1;
2126         fsp->lease->sconn = fsp->conn->sconn;
2127         fsp->lease->lease.lease_version = lease->lease_version;
2128         fsp->lease->lease.lease_key = lease->lease_key;
2129         fsp->lease->lease.lease_state = granted;
2130         fsp->lease->lease.lease_epoch = lease->lease_epoch + 1;
2131
2132         *p_lease_idx = d->num_leases;
2133
2134         d->leases[d->num_leases] = (struct share_mode_lease) {
2135                 .client_guid = *client_guid,
2136                 .lease_key = fsp->lease->lease.lease_key,
2137                 .current_state = fsp->lease->lease.lease_state,
2138                 .lease_version = fsp->lease->lease.lease_version,
2139                 .epoch = fsp->lease->lease.lease_epoch,
2140         };
2141
2142         status = leases_db_add(client_guid,
2143                                &lease->lease_key,
2144                                &fsp->file_id,
2145                                fsp->conn->connectpath,
2146                                fsp->fsp_name->base_name,
2147                                fsp->fsp_name->stream_name);
2148         if (!NT_STATUS_IS_OK(status)) {
2149                 DEBUG(10, ("%s: leases_db_add failed: %s\n", __func__,
2150                            nt_errstr(status)));
2151                 TALLOC_FREE(fsp->lease);
2152                 return NT_STATUS_INSUFFICIENT_RESOURCES;
2153         }
2154
2155         d->num_leases += 1;
2156         d->modified = true;
2157
2158         return NT_STATUS_OK;
2159 }
2160
2161 static bool is_same_lease(const files_struct *fsp,
2162                           const struct share_mode_data *d,
2163                           const struct share_mode_entry *e,
2164                           const struct smb2_lease *lease)
2165 {
2166         if (e->op_type != LEASE_OPLOCK) {
2167                 return false;
2168         }
2169         if (lease == NULL) {
2170                 return false;
2171         }
2172
2173         return smb2_lease_equal(fsp_client_guid(fsp),
2174                                 &lease->lease_key,
2175                                 &d->leases[e->lease_idx].client_guid,
2176                                 &d->leases[e->lease_idx].lease_key);
2177 }
2178
2179 static int map_lease_type_to_oplock(uint32_t lease_type)
2180 {
2181         int result = NO_OPLOCK;
2182
2183         switch (lease_type) {
2184         case SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE:
2185                 result = BATCH_OPLOCK|EXCLUSIVE_OPLOCK;
2186                 break;
2187         case SMB2_LEASE_READ|SMB2_LEASE_WRITE:
2188                 result = EXCLUSIVE_OPLOCK;
2189                 break;
2190         case SMB2_LEASE_READ|SMB2_LEASE_HANDLE:
2191         case SMB2_LEASE_READ:
2192                 result = LEVEL_II_OPLOCK;
2193                 break;
2194         }
2195
2196         return result;
2197 }
2198
2199 static NTSTATUS grant_fsp_oplock_type(struct smb_request *req,
2200                                       struct files_struct *fsp,
2201                                       struct share_mode_lock *lck,
2202                                       int oplock_request,
2203                                       struct smb2_lease *lease)
2204 {
2205         struct share_mode_data *d = lck->data;
2206         bool got_handle_lease = false;
2207         bool got_oplock = false;
2208         uint32_t i;
2209         uint32_t granted;
2210         uint32_t lease_idx = UINT32_MAX;
2211         bool ok;
2212         NTSTATUS status;
2213
2214         if (oplock_request & INTERNAL_OPEN_ONLY) {
2215                 /* No oplocks on internal open. */
2216                 oplock_request = NO_OPLOCK;
2217                 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
2218                         fsp->oplock_type, fsp_str_dbg(fsp)));
2219         }
2220
2221         if (oplock_request == LEASE_OPLOCK) {
2222                 if (lease == NULL) {
2223                         /*
2224                          * The SMB2 layer should have checked this
2225                          */
2226                         return NT_STATUS_INTERNAL_ERROR;
2227                 }
2228
2229                 granted = lease->lease_state;
2230
2231                 if (lp_kernel_oplocks(SNUM(fsp->conn))) {
2232                         DEBUG(10, ("No lease granted because kernel oplocks are enabled\n"));
2233                         granted = SMB2_LEASE_NONE;
2234                 }
2235                 if ((granted & (SMB2_LEASE_READ|SMB2_LEASE_WRITE)) == 0) {
2236                         DEBUG(10, ("No read or write lease requested\n"));
2237                         granted = SMB2_LEASE_NONE;
2238                 }
2239                 if (granted == SMB2_LEASE_WRITE) {
2240                         DEBUG(10, ("pure write lease requested\n"));
2241                         granted = SMB2_LEASE_NONE;
2242                 }
2243                 if (granted == (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
2244                         DEBUG(10, ("write and handle lease requested\n"));
2245                         granted = SMB2_LEASE_NONE;
2246                 }
2247         } else {
2248                 granted = map_oplock_to_lease_type(
2249                         oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2250         }
2251
2252         if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
2253                 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
2254                         fsp_str_dbg(fsp)));
2255                 granted &= ~SMB2_LEASE_READ;
2256         }
2257
2258         for (i=0; i<d->num_share_modes; i++) {
2259                 struct share_mode_entry *e = &d->share_modes[i];
2260                 uint32_t e_lease_type;
2261
2262                 e_lease_type = get_lease_type(d, e);
2263
2264                 if ((granted & SMB2_LEASE_WRITE) &&
2265                     !is_same_lease(fsp, d, e, lease) &&
2266                     !share_mode_stale_pid(d, i)) {
2267                         /*
2268                          * Can grant only one writer
2269                          */
2270                         granted &= ~SMB2_LEASE_WRITE;
2271                 }
2272
2273                 if ((e_lease_type & SMB2_LEASE_HANDLE) && !got_handle_lease &&
2274                     !share_mode_stale_pid(d, i)) {
2275                         got_handle_lease = true;
2276                 }
2277
2278                 if ((e->op_type != LEASE_OPLOCK) && !got_oplock &&
2279                     !share_mode_stale_pid(d, i)) {
2280                         got_oplock = true;
2281                 }
2282         }
2283
2284         if ((granted & SMB2_LEASE_READ) && !(granted & SMB2_LEASE_WRITE)) {
2285                 bool allow_level2 =
2286                         (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
2287                         lp_level2_oplocks(SNUM(fsp->conn));
2288
2289                 if (!allow_level2) {
2290                         granted = SMB2_LEASE_NONE;
2291                 }
2292         }
2293
2294         if (oplock_request == LEASE_OPLOCK) {
2295                 if (got_oplock) {
2296                         granted &= ~SMB2_LEASE_HANDLE;
2297                 }
2298
2299                 fsp->oplock_type = LEASE_OPLOCK;
2300
2301                 status = grant_fsp_lease(fsp, lck, lease, &lease_idx,
2302                                          granted);
2303                 if (!NT_STATUS_IS_OK(status)) {
2304                         return status;
2305
2306                 }
2307                 *lease = fsp->lease->lease;
2308                 DEBUG(10, ("lease_state=%d\n", lease->lease_state));
2309         } else {
2310                 if (got_handle_lease) {
2311                         granted = SMB2_LEASE_NONE;
2312                 }
2313
2314                 fsp->oplock_type = map_lease_type_to_oplock(granted);
2315
2316                 status = set_file_oplock(fsp);
2317                 if (!NT_STATUS_IS_OK(status)) {
2318                         /*
2319                          * Could not get the kernel oplock
2320                          */
2321                         fsp->oplock_type = NO_OPLOCK;
2322                 }
2323         }
2324
2325         ok = set_share_mode(lck, fsp, get_current_uid(fsp->conn),
2326                             req ? req->mid : 0,
2327                             fsp->oplock_type,
2328                             lease_idx);
2329         if (!ok) {
2330                 return NT_STATUS_NO_MEMORY;
2331         }
2332
2333         ok = update_num_read_oplocks(fsp, lck);
2334         if (!ok) {
2335                 del_share_mode(lck, fsp);
2336                 return NT_STATUS_INTERNAL_ERROR;
2337         }
2338
2339         DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
2340                   fsp->oplock_type, fsp_str_dbg(fsp)));
2341
2342         return NT_STATUS_OK;
2343 }
2344
2345 static bool request_timed_out(struct timeval request_time,
2346                               struct timeval timeout)
2347 {
2348         struct timeval now, end_time;
2349         GetTimeOfDay(&now);
2350         end_time = timeval_sum(&request_time, &timeout);
2351         return (timeval_compare(&end_time, &now) < 0);
2352 }
2353
2354 static struct deferred_open_record *deferred_open_record_create(
2355         bool delayed_for_oplocks,
2356         bool async_open,
2357         struct file_id id)
2358 {
2359         struct deferred_open_record *record = NULL;
2360
2361         record = talloc(NULL, struct deferred_open_record);
2362         if (record == NULL) {
2363                 return NULL;
2364         }
2365
2366         *record = (struct deferred_open_record) {
2367                 .delayed_for_oplocks = delayed_for_oplocks,
2368                 .async_open = async_open,
2369                 .id = id,
2370         };
2371
2372         return record;
2373 }
2374
2375 struct defer_open_state {
2376         struct smbXsrv_connection *xconn;
2377         uint64_t mid;
2378 };
2379
2380 static void defer_open_done(struct tevent_req *req);
2381
2382 /**
2383  * Defer an open and watch a locking.tdb record
2384  *
2385  * This defers an open that gets rescheduled once the locking.tdb record watch
2386  * is triggered by a change to the record.
2387  *
2388  * It is used to defer opens that triggered an oplock break and for the SMB1
2389  * sharing violation delay.
2390  **/
2391 static void defer_open(struct share_mode_lock *lck,
2392                        struct timeval request_time,
2393                        struct timeval timeout,
2394                        struct smb_request *req,
2395                        bool delayed_for_oplocks,
2396                        struct file_id id)
2397 {
2398         struct deferred_open_record *open_rec = NULL;
2399         struct timeval abs_timeout;
2400         struct defer_open_state *watch_state;
2401         struct tevent_req *watch_req;
2402         bool ok;
2403
2404         abs_timeout = timeval_sum(&request_time, &timeout);
2405
2406         DBG_DEBUG("request time [%s] timeout [%s] mid [%" PRIu64 "] "
2407                   "delayed_for_oplocks [%s] file_id [%s]\n",
2408                   timeval_string(talloc_tos(), &request_time, false),
2409                   timeval_string(talloc_tos(), &abs_timeout, false),
2410                   req->mid,
2411                   delayed_for_oplocks ? "yes" : "no",
2412                   file_id_string_tos(&id));
2413
2414         open_rec = deferred_open_record_create(delayed_for_oplocks,
2415                                                false,
2416                                                id);
2417         if (open_rec == NULL) {
2418                 TALLOC_FREE(lck);
2419                 exit_server("talloc failed");
2420         }
2421
2422         watch_state = talloc(open_rec, struct defer_open_state);
2423         if (watch_state == NULL) {
2424                 exit_server("talloc failed");
2425         }
2426         watch_state->xconn = req->xconn;
2427         watch_state->mid = req->mid;
2428
2429         DBG_DEBUG("defering mid %" PRIu64 "\n", req->mid);
2430
2431         watch_req = dbwrap_watched_watch_send(watch_state,
2432                                               req->sconn->ev_ctx,
2433                                               lck->data->record,
2434                                               (struct server_id){0});
2435         if (watch_req == NULL) {
2436                 exit_server("Could not watch share mode record");
2437         }
2438         tevent_req_set_callback(watch_req, defer_open_done, watch_state);
2439
2440         ok = tevent_req_set_endtime(watch_req, req->sconn->ev_ctx, abs_timeout);
2441         if (!ok) {
2442                 exit_server("tevent_req_set_endtime failed");
2443         }
2444
2445         ok = push_deferred_open_message_smb(req, request_time, timeout,
2446                                             open_rec->id, open_rec);
2447         if (!ok) {
2448                 TALLOC_FREE(lck);
2449                 exit_server("push_deferred_open_message_smb failed");
2450         }
2451 }
2452
2453 static void defer_open_done(struct tevent_req *req)
2454 {
2455         struct defer_open_state *state = tevent_req_callback_data(
2456                 req, struct defer_open_state);
2457         NTSTATUS status;
2458         bool ret;
2459
2460         status = dbwrap_watched_watch_recv(req, NULL, NULL);
2461         TALLOC_FREE(req);
2462         if (!NT_STATUS_IS_OK(status)) {
2463                 DEBUG(5, ("dbwrap_watched_watch_recv returned %s\n",
2464                           nt_errstr(status)));
2465                 /*
2466                  * Even if it failed, retry anyway. TODO: We need a way to
2467                  * tell a re-scheduled open about that error.
2468                  */
2469         }
2470
2471         DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
2472
2473         ret = schedule_deferred_open_message_smb(state->xconn, state->mid);
2474         SMB_ASSERT(ret);
2475         TALLOC_FREE(state);
2476 }
2477
2478 /**
2479  * Actually attempt the kernel oplock polling open.
2480  */
2481
2482 static void kernel_oplock_poll_open_timer(struct tevent_context *ev,
2483                                       struct tevent_timer *te,
2484                                       struct timeval current_time,
2485                                       void *private_data)
2486 {
2487         bool ok;
2488         struct smb_request *req = (struct smb_request *)private_data;
2489
2490         ok = schedule_deferred_open_message_smb(req->xconn, req->mid);
2491         if (!ok) {
2492                 exit_server("schedule_deferred_open_message_smb failed");
2493         }
2494         DBG_DEBUG("kernel_oplock_poll_open_timer fired. Retying open !\n");
2495 }
2496
2497 /**
2498  * Reschedule an open for 1 second from now, if not timed out.
2499  **/
2500 static void setup_kernel_oplock_poll_open(struct timeval request_time,
2501                        struct smb_request *req,
2502                        struct file_id id)
2503 {
2504
2505         bool ok;
2506         struct deferred_open_record *open_rec = NULL;
2507         /* Maximum wait time. */
2508         struct timeval timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
2509
2510         if (request_timed_out(request_time, timeout)) {
2511                 return;
2512         }
2513
2514         open_rec = deferred_open_record_create(false, false, id);
2515         if (open_rec == NULL) {
2516                 exit_server("talloc failed");
2517         }
2518
2519         ok = push_deferred_open_message_smb(req,
2520                                             request_time,
2521                                             timeout,
2522                                             id,
2523                                             open_rec);
2524         if (!ok) {
2525                 exit_server("push_deferred_open_message_smb failed");
2526         }
2527
2528         /*
2529          * As this timer event is owned by req, it will
2530          * disappear if req it talloc_freed.
2531          */
2532         open_rec->te = tevent_add_timer(req->sconn->ev_ctx,
2533                                         req,
2534                                         timeval_current_ofs(1, 0),
2535                                         kernel_oplock_poll_open_timer,
2536                                         req);
2537         if (open_rec->te == NULL) {
2538                 exit_server("tevent_add_timer failed");
2539         }
2540
2541         DBG_DEBUG("poll request time [%s] mid [%" PRIu64 "] file_id [%s]\n",
2542                   timeval_string(talloc_tos(), &request_time, false),
2543                   req->mid,
2544                   file_id_string_tos(&id));
2545 }
2546
2547 /****************************************************************************
2548  On overwrite open ensure that the attributes match.
2549 ****************************************************************************/
2550
2551 static bool open_match_attributes(connection_struct *conn,
2552                                   uint32_t old_dos_attr,
2553                                   uint32_t new_dos_attr,
2554                                   mode_t new_unx_mode,
2555                                   mode_t *returned_unx_mode)
2556 {
2557         uint32_t noarch_old_dos_attr, noarch_new_dos_attr;
2558
2559         noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
2560         noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
2561
2562         if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) || 
2563            (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
2564                 *returned_unx_mode = new_unx_mode;
2565         } else {
2566                 *returned_unx_mode = (mode_t)0;
2567         }
2568
2569         DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
2570                   "new_dos_attr = 0x%x "
2571                   "returned_unx_mode = 0%o\n",
2572                   (unsigned int)old_dos_attr,
2573                   (unsigned int)new_dos_attr,
2574                   (unsigned int)*returned_unx_mode ));
2575
2576         /* If we're mapping SYSTEM and HIDDEN ensure they match. */
2577         if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
2578                 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
2579                     !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
2580                         return False;
2581                 }
2582         }
2583         if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
2584                 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
2585                     !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
2586                         return False;
2587                 }
2588         }
2589         return True;
2590 }
2591
2592 /****************************************************************************
2593  Special FCB or DOS processing in the case of a sharing violation.
2594  Try and find a duplicated file handle.
2595 ****************************************************************************/
2596
2597 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
2598                                 connection_struct *conn,
2599                                 files_struct *fsp_to_dup_into,
2600                                 const struct smb_filename *smb_fname,
2601                                 struct file_id id,
2602                                 uint16_t file_pid,
2603                                 uint64_t vuid,
2604                                 uint32_t access_mask,
2605                                 uint32_t share_access,
2606                                 uint32_t create_options)
2607 {
2608         files_struct *fsp;
2609
2610         DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
2611                  "file %s.\n", smb_fname_str_dbg(smb_fname)));
2612
2613         for(fsp = file_find_di_first(conn->sconn, id); fsp;
2614             fsp = file_find_di_next(fsp)) {
2615
2616                 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
2617                           "vuid = %llu, file_pid = %u, private_options = 0x%x "
2618                           "access_mask = 0x%x\n", fsp_str_dbg(fsp),
2619                           fsp->fh->fd, (unsigned long long)fsp->vuid,
2620                           (unsigned int)fsp->file_pid,
2621                           (unsigned int)fsp->fh->private_options,
2622                           (unsigned int)fsp->access_mask ));
2623
2624                 if (fsp != fsp_to_dup_into &&
2625                     fsp->fh->fd != -1 &&
2626                     fsp->vuid == vuid &&
2627                     fsp->file_pid == file_pid &&
2628                     (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
2629                                                  NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
2630                     (fsp->access_mask & FILE_WRITE_DATA) &&
2631                     strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
2632                     strequal(fsp->fsp_name->stream_name,
2633                              smb_fname->stream_name)) {
2634                         DEBUG(10,("fcb_or_dos_open: file match\n"));
2635                         break;
2636                 }
2637         }
2638
2639         if (!fsp) {
2640                 return NT_STATUS_NOT_FOUND;
2641         }
2642
2643         /* quite an insane set of semantics ... */
2644         if (is_executable(smb_fname->base_name) &&
2645             (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
2646                 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
2647                 return NT_STATUS_INVALID_PARAMETER;
2648         }
2649
2650         /* We need to duplicate this fsp. */
2651         return dup_file_fsp(req, fsp, access_mask, share_access,
2652                             create_options, fsp_to_dup_into);
2653 }
2654
2655 static void schedule_defer_open(struct share_mode_lock *lck,
2656                                 struct file_id id,
2657                                 struct timeval request_time,
2658                                 struct smb_request *req)
2659 {
2660         /* This is a relative time, added to the absolute
2661            request_time value to get the absolute timeout time.
2662            Note that if this is the second or greater time we enter
2663            this codepath for this particular request mid then
2664            request_time is left as the absolute time of the *first*
2665            time this request mid was processed. This is what allows
2666            the request to eventually time out. */
2667
2668         struct timeval timeout;
2669
2670         /* Normally the smbd we asked should respond within
2671          * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
2672          * the client did, give twice the timeout as a safety
2673          * measure here in case the other smbd is stuck
2674          * somewhere else. */
2675
2676         timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
2677
2678         if (request_timed_out(request_time, timeout)) {
2679                 return;
2680         }
2681
2682         defer_open(lck, request_time, timeout, req, true, id);
2683 }
2684
2685 /****************************************************************************
2686  Reschedule an open call that went asynchronous.
2687 ****************************************************************************/
2688
2689 static void schedule_async_open_timer(struct tevent_context *ev,
2690                                       struct tevent_timer *te,
2691                                       struct timeval current_time,
2692                                       void *private_data)
2693 {
2694         exit_server("async open timeout");
2695 }
2696
2697 static void schedule_async_open(struct timeval request_time,
2698                                 struct smb_request *req)
2699 {
2700         struct deferred_open_record *open_rec = NULL;
2701         struct timeval timeout = timeval_set(20, 0);
2702         bool ok;
2703
2704         if (request_timed_out(request_time, timeout)) {
2705                 return;
2706         }
2707
2708         open_rec = deferred_open_record_create(false, true, (struct file_id){0});
2709         if (open_rec == NULL) {
2710                 exit_server("deferred_open_record_create failed");
2711         }
2712
2713         ok = push_deferred_open_message_smb(req, request_time, timeout,
2714                                             (struct file_id){0}, open_rec);
2715         if (!ok) {
2716                 exit_server("push_deferred_open_message_smb failed");
2717         }
2718
2719         open_rec->te = tevent_add_timer(req->sconn->ev_ctx,
2720                                         req,
2721                                         timeval_current_ofs(20, 0),
2722                                         schedule_async_open_timer,
2723                                         open_rec);
2724         if (open_rec->te == NULL) {
2725                 exit_server("tevent_add_timer failed");
2726         }
2727 }
2728
2729 /****************************************************************************
2730  Work out what access_mask to use from what the client sent us.
2731 ****************************************************************************/
2732
2733 static NTSTATUS smbd_calculate_maximum_allowed_access(
2734         connection_struct *conn,
2735         const struct smb_filename *smb_fname,
2736         bool use_privs,
2737         uint32_t *p_access_mask)
2738 {
2739         struct security_descriptor *sd;
2740         uint32_t access_granted;
2741         NTSTATUS status;
2742
2743         if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
2744                 *p_access_mask |= FILE_GENERIC_ALL;
2745                 return NT_STATUS_OK;
2746         }
2747
2748         status = SMB_VFS_GET_NT_ACL(conn, smb_fname,
2749                                     (SECINFO_OWNER |
2750                                      SECINFO_GROUP |
2751                                      SECINFO_DACL),
2752                                     talloc_tos(), &sd);
2753
2754         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2755                 /*
2756                  * File did not exist
2757                  */
2758                 *p_access_mask = FILE_GENERIC_ALL;
2759                 return NT_STATUS_OK;
2760         }
2761         if (!NT_STATUS_IS_OK(status)) {
2762                 DEBUG(10,("Could not get acl on file %s: %s\n",
2763                           smb_fname_str_dbg(smb_fname),
2764                           nt_errstr(status)));
2765                 return NT_STATUS_ACCESS_DENIED;
2766         }
2767
2768         /*
2769          * If we can access the path to this file, by
2770          * default we have FILE_READ_ATTRIBUTES from the
2771          * containing directory. See the section:
2772          * "Algorithm to Check Access to an Existing File"
2773          * in MS-FSA.pdf.
2774          *
2775          * se_file_access_check()
2776          * also takes care of owner WRITE_DAC and READ_CONTROL.
2777          */
2778         status = se_file_access_check(sd,
2779                                  get_current_nttok(conn),
2780                                  use_privs,
2781                                  (*p_access_mask & ~FILE_READ_ATTRIBUTES),
2782                                  &access_granted);
2783
2784         TALLOC_FREE(sd);
2785
2786         if (!NT_STATUS_IS_OK(status)) {
2787                 DEBUG(10, ("Access denied on file %s: "
2788                            "when calculating maximum access\n",
2789                            smb_fname_str_dbg(smb_fname)));
2790                 return NT_STATUS_ACCESS_DENIED;
2791         }
2792         *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
2793
2794         if (!(access_granted & DELETE_ACCESS)) {
2795                 if (can_delete_file_in_directory(conn, smb_fname)) {
2796                         *p_access_mask |= DELETE_ACCESS;
2797                 }
2798         }
2799
2800         return NT_STATUS_OK;
2801 }
2802
2803 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
2804                                     const struct smb_filename *smb_fname,
2805                                     bool use_privs,
2806                                     uint32_t access_mask,
2807                                     uint32_t *access_mask_out)
2808 {
2809         NTSTATUS status;
2810         uint32_t orig_access_mask = access_mask;
2811         uint32_t rejected_share_access;
2812
2813         if (access_mask & SEC_MASK_INVALID) {
2814                 DBG_DEBUG("access_mask [%8x] contains invalid bits\n",
2815                           access_mask);
2816                 return NT_STATUS_ACCESS_DENIED;
2817         }
2818
2819         /*
2820          * Convert GENERIC bits to specific bits.
2821          */
2822
2823         se_map_generic(&access_mask, &file_generic_mapping);
2824
2825         /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
2826         if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
2827
2828                 status = smbd_calculate_maximum_allowed_access(
2829                         conn, smb_fname, use_privs, &access_mask);
2830
2831                 if (!NT_STATUS_IS_OK(status)) {
2832                         return status;
2833                 }
2834
2835                 access_mask &= conn->share_access;
2836         }
2837
2838         rejected_share_access = access_mask & ~(conn->share_access);
2839
2840         if (rejected_share_access) {
2841                 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
2842                         "file %s: rejected by share access mask[0x%08X] "
2843                         "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
2844                         smb_fname_str_dbg(smb_fname),
2845                         conn->share_access,
2846                         orig_access_mask, access_mask,
2847                         rejected_share_access));
2848                 return NT_STATUS_ACCESS_DENIED;
2849         }
2850
2851         *access_mask_out = access_mask;
2852         return NT_STATUS_OK;
2853 }
2854
2855 /****************************************************************************
2856  Remove the deferred open entry under lock.
2857 ****************************************************************************/
2858
2859 /****************************************************************************
2860  Return true if this is a state pointer to an asynchronous create.
2861 ****************************************************************************/
2862
2863 bool is_deferred_open_async(const struct deferred_open_record *rec)
2864 {
2865         return rec->async_open;
2866 }
2867
2868 static bool clear_ads(uint32_t create_disposition)
2869 {
2870         bool ret = false;
2871
2872         switch (create_disposition) {
2873         case FILE_SUPERSEDE:
2874         case FILE_OVERWRITE_IF:
2875         case FILE_OVERWRITE:
2876                 ret = true;
2877                 break;
2878         default:
2879                 break;
2880         }
2881         return ret;
2882 }
2883
2884 static int disposition_to_open_flags(uint32_t create_disposition)
2885 {
2886         int ret = 0;
2887
2888         /*
2889          * Currently we're using FILE_SUPERSEDE as the same as
2890          * FILE_OVERWRITE_IF but they really are
2891          * different. FILE_SUPERSEDE deletes an existing file
2892          * (requiring delete access) then recreates it.
2893          */
2894
2895         switch (create_disposition) {
2896         case FILE_SUPERSEDE:
2897         case FILE_OVERWRITE_IF:
2898                 /*
2899                  * If file exists replace/overwrite. If file doesn't
2900                  * exist create.
2901                  */
2902                 ret = O_CREAT|O_TRUNC;
2903                 break;
2904
2905         case FILE_OPEN:
2906                 /*
2907                  * If file exists open. If file doesn't exist error.
2908                  */
2909                 ret = 0;
2910                 break;
2911
2912         case FILE_OVERWRITE:
2913                 /*
2914                  * If file exists overwrite. If file doesn't exist
2915                  * error.
2916                  */
2917                 ret = O_TRUNC;
2918                 break;
2919
2920         case FILE_CREATE:
2921                 /*
2922                  * If file exists error. If file doesn't exist create.
2923                  */
2924                 ret = O_CREAT|O_EXCL;
2925                 break;
2926
2927         case FILE_OPEN_IF:
2928                 /*
2929                  * If file exists open. If file doesn't exist create.
2930                  */
2931                 ret = O_CREAT;
2932                 break;
2933         }
2934         return ret;
2935 }
2936
2937 static int calculate_open_access_flags(uint32_t access_mask,
2938                                        uint32_t private_flags)
2939 {
2940         bool need_write, need_read;
2941
2942         /*
2943          * Note that we ignore the append flag as append does not
2944          * mean the same thing under DOS and Unix.
2945          */
2946
2947         need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
2948         if (!need_write) {
2949                 return O_RDONLY;
2950         }
2951
2952         /* DENY_DOS opens are always underlying read-write on the
2953            file handle, no matter what the requested access mask
2954            says. */
2955
2956         need_read =
2957                 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2958                  access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2959                                 FILE_READ_EA|FILE_EXECUTE));
2960
2961         if (!need_read) {
2962                 return O_WRONLY;
2963         }
2964         return O_RDWR;
2965 }
2966
2967 /****************************************************************************
2968  Open a file with a share mode. Passed in an already created files_struct *.
2969 ****************************************************************************/
2970
2971 static NTSTATUS open_file_ntcreate(connection_struct *conn,
2972                             struct smb_request *req,
2973                             uint32_t access_mask,               /* access bits (FILE_READ_DATA etc.) */
2974                             uint32_t share_access,      /* share constants (FILE_SHARE_READ etc) */
2975                             uint32_t create_disposition,        /* FILE_OPEN_IF etc. */
2976                             uint32_t create_options,    /* options such as delete on close. */
2977                             uint32_t new_dos_attributes,        /* attributes used for new file. */
2978                             int oplock_request,         /* internal Samba oplock codes. */
2979                             struct smb2_lease *lease,
2980                                                         /* Information (FILE_EXISTS etc.) */
2981                             uint32_t private_flags,     /* Samba specific flags. */
2982                             int *pinfo,
2983                             files_struct *fsp)
2984 {
2985         struct smb_filename *smb_fname = fsp->fsp_name;
2986         int flags=0;
2987         int flags2=0;
2988         bool file_existed = VALID_STAT(smb_fname->st);
2989         bool def_acl = False;
2990         bool posix_open = False;
2991         bool new_file_created = False;
2992         bool first_open_attempt = true;
2993         NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2994         mode_t new_unx_mode = (mode_t)0;
2995         mode_t unx_mode = (mode_t)0;
2996         int info;
2997         uint32_t existing_dos_attributes = 0;
2998         struct timeval request_time = timeval_zero();
2999         struct share_mode_lock *lck = NULL;
3000         uint32_t open_access_mask = access_mask;
3001         NTSTATUS status;
3002         char *parent_dir;
3003         SMB_STRUCT_STAT saved_stat = smb_fname->st;
3004         struct timespec old_write_time;
3005         struct file_id id;
3006
3007         if (conn->printer) {
3008                 /*
3009                  * Printers are handled completely differently.
3010                  * Most of the passed parameters are ignored.
3011                  */
3012
3013                 if (pinfo) {
3014                         *pinfo = FILE_WAS_CREATED;
3015                 }
3016
3017                 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
3018                            smb_fname_str_dbg(smb_fname)));
3019
3020                 if (!req) {
3021                         DEBUG(0,("open_file_ntcreate: printer open without "
3022                                 "an SMB request!\n"));
3023                         return NT_STATUS_INTERNAL_ERROR;
3024                 }
3025
3026                 return print_spool_open(fsp, smb_fname->base_name,
3027                                         req->vuid);
3028         }
3029
3030         if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
3031                             NULL)) {
3032                 return NT_STATUS_NO_MEMORY;
3033         }
3034
3035         if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3036                 posix_open = True;
3037                 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
3038                 new_dos_attributes = 0;
3039         } else {
3040                 /* Windows allows a new file to be created and
3041                    silently removes a FILE_ATTRIBUTE_DIRECTORY
3042                    sent by the client. Do the same. */
3043
3044                 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
3045
3046                 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
3047                  * created new. */
3048                 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
3049                                      smb_fname, parent_dir);
3050         }
3051
3052         DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
3053                    "access_mask=0x%x share_access=0x%x "
3054                    "create_disposition = 0x%x create_options=0x%x "
3055                    "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
3056                    smb_fname_str_dbg(smb_fname), new_dos_attributes,
3057                    access_mask, share_access, create_disposition,
3058                    create_options, (unsigned int)unx_mode, oplock_request,
3059                    (unsigned int)private_flags));
3060
3061         if (req == NULL) {
3062                 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
3063                 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
3064         } else {
3065                 /* And req != NULL means no INTERNAL_OPEN_ONLY */
3066                 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
3067         }
3068
3069         /*
3070          * Only non-internal opens can be deferred at all
3071          */
3072
3073         if (req) {
3074                 struct deferred_open_record *open_rec;
3075                 if (get_deferred_open_message_state(req,
3076                                 &request_time,
3077                                 &open_rec)) {
3078                         /* Remember the absolute time of the original
3079                            request with this mid. We'll use it later to
3080                            see if this has timed out. */
3081
3082                         /* If it was an async create retry, the file
3083                            didn't exist. */
3084
3085                         if (is_deferred_open_async(open_rec)) {
3086                                 SET_STAT_INVALID(smb_fname->st);
3087                                 file_existed = false;
3088                         }
3089
3090                         /* Ensure we don't reprocess this message. */
3091                         remove_deferred_open_message_smb(req->xconn, req->mid);
3092
3093                         first_open_attempt = false;
3094                 }
3095         }
3096
3097         if (!posix_open) {
3098                 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
3099                 if (file_existed) {
3100                         /*
3101                          * Only use strored DOS attributes for checks
3102                          * against requested attributes (below via
3103                          * open_match_attributes()), cf bug #11992
3104                          * for details. -slow
3105                          */
3106                         uint32_t attr = 0;
3107
3108                         status = SMB_VFS_GET_DOS_ATTRIBUTES(conn, smb_fname, &attr);
3109                         if (NT_STATUS_IS_OK(status)) {
3110                                 existing_dos_attributes = attr;
3111                         }
3112                 }
3113         }
3114
3115         /* ignore any oplock requests if oplocks are disabled */
3116         if (!lp_oplocks(SNUM(conn)) ||
3117             IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
3118                 /* Mask off everything except the private Samba bits. */
3119                 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
3120         }
3121
3122         /* this is for OS/2 long file names - say we don't support them */
3123         if (req != NULL && !req->posix_pathnames &&
3124                         strstr(smb_fname->base_name,".+,;=[].")) {
3125                 /* OS/2 Workplace shell fix may be main code stream in a later
3126                  * release. */
3127                 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
3128                          "supported.\n"));
3129                 if (use_nt_status()) {
3130                         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3131                 }
3132                 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
3133         }
3134
3135         switch( create_disposition ) {
3136                 case FILE_OPEN:
3137                         /* If file exists open. If file doesn't exist error. */
3138                         if (!file_existed) {
3139                                 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
3140                                          "requested for file %s and file "
3141                                          "doesn't exist.\n",
3142                                          smb_fname_str_dbg(smb_fname)));
3143                                 errno = ENOENT;
3144                                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3145                         }
3146                         break;
3147
3148                 case FILE_OVERWRITE:
3149                         /* If file exists overwrite. If file doesn't exist
3150                          * error. */
3151                         if (!file_existed) {
3152                                 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
3153                                          "requested for file %s and file "
3154                                          "doesn't exist.\n",
3155                                          smb_fname_str_dbg(smb_fname) ));
3156                                 errno = ENOENT;
3157                                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3158                         }
3159                         break;
3160
3161                 case FILE_CREATE:
3162                         /* If file exists error. If file doesn't exist
3163                          * create. */
3164                         if (file_existed) {
3165                                 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
3166                                          "requested for file %s and file "
3167                                          "already exists.\n",
3168                                          smb_fname_str_dbg(smb_fname)));
3169                                 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
3170                                         errno = EISDIR;
3171                                 } else {
3172                                         errno = EEXIST;
3173                                 }
3174                                 return map_nt_error_from_unix(errno);
3175                         }
3176                         break;
3177
3178                 case FILE_SUPERSEDE:
3179                 case FILE_OVERWRITE_IF:
3180                 case FILE_OPEN_IF:
3181                         break;
3182                 default:
3183                         return NT_STATUS_INVALID_PARAMETER;
3184         }
3185
3186         flags2 = disposition_to_open_flags(create_disposition);
3187
3188         /* We only care about matching attributes on file exists and
3189          * overwrite. */
3190
3191         if (!posix_open && file_existed &&
3192             ((create_disposition == FILE_OVERWRITE) ||
3193              (create_disposition == FILE_OVERWRITE_IF))) {
3194                 if (!open_match_attributes(conn, existing_dos_attributes,
3195                                            new_dos_attributes,
3196                                            unx_mode, &new_unx_mode)) {
3197                         DEBUG(5,("open_file_ntcreate: attributes mismatch "
3198                                  "for file %s (%x %x) (0%o, 0%o)\n",
3199                                  smb_fname_str_dbg(smb_fname),
3200                                  existing_dos_attributes,
3201                                  new_dos_attributes,
3202                                  (unsigned int)smb_fname->st.st_ex_mode,
3203                                  (unsigned int)unx_mode ));
3204                         errno = EACCES;
3205                         return NT_STATUS_ACCESS_DENIED;
3206                 }
3207         }
3208
3209         status = smbd_calculate_access_mask(conn, smb_fname,
3210                                         false,
3211                                         access_mask,
3212                                         &access_mask); 
3213         if (!NT_STATUS_IS_OK(status)) {
3214                 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
3215                         "on file %s returned %s\n",
3216                         smb_fname_str_dbg(smb_fname), nt_errstr(status)));
3217                 return status;
3218         }
3219
3220         open_access_mask = access_mask;
3221
3222         if (flags2 & O_TRUNC) {
3223                 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
3224         }
3225
3226         DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
3227                    "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
3228                     access_mask));
3229
3230         /*
3231          * Note that we ignore the append flag as append does not
3232          * mean the same thing under DOS and Unix.
3233          */
3234
3235         flags = calculate_open_access_flags(access_mask, private_flags);
3236
3237         /*
3238          * Currently we only look at FILE_WRITE_THROUGH for create options.
3239          */
3240
3241 #if defined(O_SYNC)
3242         if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
3243                 flags2 |= O_SYNC;
3244         }
3245 #endif /* O_SYNC */
3246
3247         if (posix_open && (access_mask & FILE_APPEND_DATA)) {
3248                 flags2 |= O_APPEND;
3249         }
3250
3251         if (!posix_open && !CAN_WRITE(conn)) {
3252                 /*
3253                  * We should really return a permission denied error if either
3254                  * O_CREAT or O_TRUNC are set, but for compatibility with
3255                  * older versions of Samba we just AND them out.
3256                  */
3257                 flags2 &= ~(O_CREAT|O_TRUNC);
3258         }
3259
3260         if (lp_kernel_oplocks(SNUM(conn))) {
3261                 /*
3262                  * With kernel oplocks the open breaking an oplock
3263                  * blocks until the oplock holder has given up the
3264                  * oplock or closed the file. We prevent this by always
3265                  * trying to open the file with O_NONBLOCK (see "man
3266                  * fcntl" on Linux).
3267                  *
3268                  * If a process that doesn't use the smbd open files
3269                  * database or communication methods holds a kernel
3270                  * oplock we must periodically poll for available open
3271                  * using O_NONBLOCK.
3272                  */
3273                 flags2 |= O_NONBLOCK;
3274         }
3275
3276         /*
3277          * Ensure we can't write on a read-only share or file.
3278          */
3279
3280         if (flags != O_RDONLY && file_existed &&
3281             (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
3282                 DEBUG(5,("open_file_ntcreate: write access requested for "
3283                          "file %s on read only %s\n",
3284                          smb_fname_str_dbg(smb_fname),
3285                          !CAN_WRITE(conn) ? "share" : "file" ));
3286                 errno = EACCES;
3287                 return NT_STATUS_ACCESS_DENIED;
3288         }
3289
3290         fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
3291         fsp->share_access = share_access;
3292         fsp->fh->private_options = private_flags;
3293         fsp->access_mask = open_access_mask; /* We change this to the
3294                                               * requested access_mask after
3295                                               * the open is done. */
3296         if (posix_open) {
3297                 fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
3298         }
3299
3300         if (timeval_is_zero(&request_time)) {
3301                 request_time = fsp->open_time;
3302         }
3303
3304         if ((create_options & FILE_DELETE_ON_CLOSE) &&
3305                         (flags2 & O_CREAT) &&
3306                         !file_existed) {
3307                 /* Delete on close semantics for new files. */
3308                 status = can_set_delete_on_close(fsp,
3309                                                 new_dos_attributes);
3310                 if (!NT_STATUS_IS_OK(status)) {
3311                         fd_close(fsp);
3312                         return status;
3313                 }
3314         }
3315
3316         /*
3317          * Ensure we pay attention to default ACLs on directories if required.
3318          */
3319
3320         if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
3321             (def_acl = directory_has_default_acl(conn, parent_dir))) {
3322                 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
3323         }
3324
3325         DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
3326                 "access_mask = 0x%x, open_access_mask = 0x%x\n",
3327                  (unsigned int)flags, (unsigned int)flags2,
3328                  (unsigned int)unx_mode, (unsigned int)access_mask,
3329                  (unsigned int)open_access_mask));
3330
3331         fsp_open = open_file(fsp, conn, req, parent_dir,
3332                              flags|flags2, unx_mode, access_mask,
3333                              open_access_mask, &new_file_created);
3334
3335         if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
3336                 bool delay;
3337
3338                 /*
3339                  * This handles the kernel oplock case:
3340                  *
3341                  * the file has an active kernel oplock and the open() returned
3342                  * EWOULDBLOCK/EAGAIN which maps to NETWORK_BUSY.
3343                  *
3344                  * "Samba locking.tdb oplocks" are handled below after acquiring
3345                  * the sharemode lock with get_share_mode_lock().
3346                  */
3347                 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
3348                         DEBUG(10, ("FIFO busy\n"));
3349                         return NT_STATUS_NETWORK_BUSY;
3350                 }
3351                 if (req == NULL) {
3352                         DEBUG(10, ("Internal open busy\n"));
3353                         return NT_STATUS_NETWORK_BUSY;
3354                 }
3355
3356                 /*
3357                  * From here on we assume this is an oplock break triggered
3358                  */
3359
3360                 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
3361                 if (lck == NULL) {
3362                         /*
3363                          * No oplock from Samba around. Set up a poll every 1
3364                          * second to retry a non-blocking open until the time
3365                          * expires.
3366                          */
3367                         setup_kernel_oplock_poll_open(request_time,
3368                                                 req,
3369                                                 fsp->file_id);
3370                         DBG_DEBUG("No Samba oplock around after EWOULDBLOCK. "
3371                                 "Retrying with poll\n");
3372                         return NT_STATUS_SHARING_VIOLATION;
3373                 }
3374
3375                 if (!validate_oplock_types(lck)) {
3376                         smb_panic("validate_oplock_types failed");
3377                 }
3378
3379                 delay = delay_for_oplock(fsp, 0, lease, lck, false,
3380                                          create_disposition,
3381                                          first_open_attempt);
3382                 if (delay) {
3383                         schedule_defer_open(lck, fsp->file_id, request_time,
3384                                             req);
3385                         TALLOC_FREE(lck);
3386                         DEBUG(10, ("Sent oplock break request to kernel "
3387                                    "oplock holder\n"));
3388                         return NT_STATUS_SHARING_VIOLATION;
3389                 }
3390
3391                 /*
3392                  * No oplock from Samba around. Set up a poll every 1
3393                  * second to retry a non-blocking open until the time
3394                  * expires.
3395                  */
3396                 setup_kernel_oplock_poll_open(request_time, req, fsp->file_id);
3397
3398                 TALLOC_FREE(lck);
3399                 DBG_DEBUG("No Samba oplock around after EWOULDBLOCK. "
3400                         "Retrying with poll\n");
3401                 return NT_STATUS_SHARING_VIOLATION;
3402         }
3403
3404         if (!NT_STATUS_IS_OK(fsp_open)) {
3405                 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
3406                         schedule_async_open(request_time, req);
3407                 }
3408                 return fsp_open;
3409         }
3410
3411         if (new_file_created) {
3412                 /*
3413                  * As we atomically create using O_CREAT|O_EXCL,
3414                  * then if new_file_created is true, then
3415                  * file_existed *MUST* have been false (even
3416                  * if the file was previously detected as being
3417                  * there).
3418                  */
3419                 file_existed = false;
3420         }
3421
3422         if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
3423                 /*
3424                  * The file did exist, but some other (local or NFS)
3425                  * process either renamed/unlinked and re-created the
3426                  * file with different dev/ino after we walked the path,
3427                  * but before we did the open. We could retry the
3428                  * open but it's a rare enough case it's easier to
3429                  * just fail the open to prevent creating any problems
3430                  * in the open file db having the wrong dev/ino key.
3431                  */
3432                 fd_close(fsp);
3433                 DBG_WARNING("file %s - dev/ino mismatch. "
3434                             "Old (dev=%ju, ino=%ju). "
3435                             "New (dev=%ju, ino=%ju). Failing open "
3436                             "with NT_STATUS_ACCESS_DENIED.\n",
3437                             smb_fname_str_dbg(smb_fname),
3438                             (uintmax_t)saved_stat.st_ex_dev,
3439                             (uintmax_t)saved_stat.st_ex_ino,
3440                             (uintmax_t)smb_fname->st.st_ex_dev,
3441                             (uintmax_t)smb_fname->st.st_ex_ino);
3442                 return NT_STATUS_ACCESS_DENIED;
3443         }
3444
3445         old_write_time = smb_fname->st.st_ex_mtime;
3446
3447         /*
3448          * Deal with the race condition where two smbd's detect the
3449          * file doesn't exist and do the create at the same time. One
3450          * of them will win and set a share mode, the other (ie. this
3451          * one) should check if the requested share mode for this
3452          * create is allowed.
3453          */
3454
3455         /*
3456          * Now the file exists and fsp is successfully opened,
3457          * fsp->dev and fsp->inode are valid and should replace the
3458          * dev=0,inode=0 from a non existent file. Spotted by
3459          * Nadav Danieli <nadavd@exanet.com>. JRA.
3460          */
3461
3462         id = fsp->file_id;
3463
3464         lck = get_share_mode_lock(talloc_tos(), id,
3465                                   conn->connectpath,
3466                                   smb_fname, &old_write_time);
3467
3468         if (lck == NULL) {
3469                 DEBUG(0, ("open_file_ntcreate: Could not get share "
3470                           "mode lock for %s\n",
3471                           smb_fname_str_dbg(smb_fname)));
3472                 fd_close(fsp);
3473                 return NT_STATUS_SHARING_VIOLATION;
3474         }
3475
3476         /* Get the types we need to examine. */
3477         if (!validate_oplock_types(lck)) {
3478                 smb_panic("validate_oplock_types failed");
3479         }
3480
3481         if (has_delete_on_close(lck, fsp->name_hash)) {
3482                 TALLOC_FREE(lck);
3483                 fd_close(fsp);
3484                 return NT_STATUS_DELETE_PENDING;
3485         }
3486
3487         status = open_mode_check(conn, lck,
3488                                  access_mask, share_access);
3489
3490         if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
3491             (lck->data->num_share_modes > 0)) {
3492                 /*
3493                  * This comes from ancient times out of open_mode_check. I
3494                  * have no clue whether this is still necessary. I can't think
3495                  * of a case where this would actually matter further down in
3496                  * this function. I leave it here for further investigation
3497                  * :-)
3498                  */
3499                 file_existed = true;
3500         }
3501
3502         if (req != NULL) {
3503                 /*
3504                  * Handle oplocks, deferring the request if delay_for_oplock()
3505                  * triggered a break message and we have to wait for the break
3506                  * response.
3507                  */
3508                 bool delay;
3509                 bool sharing_violation = NT_STATUS_EQUAL(
3510                         status, NT_STATUS_SHARING_VIOLATION);
3511
3512                 delay = delay_for_oplock(fsp, oplock_request, lease, lck,
3513                                          sharing_violation,
3514                                          create_disposition,
3515                                          first_open_attempt);
3516                 if (delay) {
3517                         schedule_defer_open(lck, fsp->file_id,
3518                                             request_time, req);
3519                         TALLOC_FREE(lck);
3520                         fd_close(fsp);
3521                         return NT_STATUS_SHARING_VIOLATION;
3522                 }
3523         }
3524
3525         if (!NT_STATUS_IS_OK(status)) {
3526                 uint32_t can_access_mask;
3527                 bool can_access = True;
3528
3529                 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
3530
3531                 /* Check if this can be done with the deny_dos and fcb
3532                  * calls. */
3533                 if (private_flags &
3534                     (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
3535                      NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
3536                         if (req == NULL) {
3537                                 DEBUG(0, ("DOS open without an SMB "
3538                                           "request!\n"));
3539                                 TALLOC_FREE(lck);
3540                                 fd_close(fsp);
3541                                 return NT_STATUS_INTERNAL_ERROR;
3542                         }
3543
3544                         /* Use the client requested access mask here,
3545                          * not the one we open with. */
3546                         status = fcb_or_dos_open(req,
3547                                                  conn,
3548                                                  fsp,
3549                                                  smb_fname,
3550                                                  id,
3551                                                  req->smbpid,
3552                                                  req->vuid,
3553                                                  access_mask,
3554                                                  share_access,
3555                                                  create_options);
3556
3557                         if (NT_STATUS_IS_OK(status)) {
3558                                 TALLOC_FREE(lck);
3559                                 if (pinfo) {
3560                                         *pinfo = FILE_WAS_OPENED;
3561                                 }
3562                                 return NT_STATUS_OK;
3563                         }
3564                 }
3565
3566                 /*
3567                  * This next line is a subtlety we need for
3568                  * MS-Access. If a file open will fail due to share
3569                  * permissions and also for security (access) reasons,
3570                  * we need to return the access failed error, not the
3571                  * share error. We can't open the file due to kernel
3572                  * oplock deadlock (it's possible we failed above on
3573                  * the open_mode_check()) so use a userspace check.
3574                  */
3575
3576                 if (flags & O_RDWR) {
3577                         can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
3578                 } else if (flags & O_WRONLY) {
3579                         can_access_mask = FILE_WRITE_DATA;
3580                 } else {
3581                         can_access_mask = FILE_READ_DATA;
3582                 }
3583
3584                 if (((can_access_mask & FILE_WRITE_DATA) &&
3585                      !CAN_WRITE(conn)) ||
3586                     !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
3587                                                               smb_fname,
3588                                                               false,
3589                                                               can_access_mask))) {
3590                         can_access = False;
3591                 }
3592
3593                 /*
3594                  * If we're returning a share violation, ensure we
3595                  * cope with the braindead 1 second delay (SMB1 only).
3596                  */
3597
3598                 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
3599                     !conn->sconn->using_smb2 &&
3600                     lp_defer_sharing_violations()) {
3601                         struct timeval timeout;
3602                         int timeout_usecs;
3603
3604                         /* this is a hack to speed up torture tests
3605                            in 'make test' */
3606                         timeout_usecs = lp_parm_int(SNUM(conn),
3607                                                     "smbd","sharedelay",
3608                                                     SHARING_VIOLATION_USEC_WAIT);
3609
3610                         /* This is a relative time, added to the absolute
3611                            request_time value to get the absolute timeout time.
3612                            Note that if this is the second or greater time we enter
3613                            this codepath for this particular request mid then
3614                            request_time is left as the absolute time of the *first*
3615                            time this request mid was processed. This is what allows
3616                            the request to eventually time out. */
3617
3618                         timeout = timeval_set(0, timeout_usecs);
3619
3620                         if (!request_timed_out(request_time, timeout)) {
3621                                 defer_open(lck, request_time, timeout, req,
3622                                            false, id);
3623                         }
3624                 }
3625
3626                 TALLOC_FREE(lck);
3627                 fd_close(fsp);
3628                 if (can_access) {
3629                         /*
3630                          * We have detected a sharing violation here
3631                          * so return the correct error code
3632                          */
3633                         status = NT_STATUS_SHARING_VIOLATION;
3634                 } else {
3635                         status = NT_STATUS_ACCESS_DENIED;
3636                 }
3637                 return status;
3638         }
3639
3640         /* Should we atomically (to the client at least) truncate ? */
3641         if ((!new_file_created) &&
3642             (flags2 & O_TRUNC) &&
3643             (!S_ISFIFO(fsp->fsp_name->st.st_ex_mode))) {
3644                 int ret;
3645
3646                 ret = vfs_set_filelen(fsp, 0);
3647                 if (ret != 0) {
3648                         status = map_nt_error_from_unix(errno);
3649                         TALLOC_FREE(lck);
3650                         fd_close(fsp);
3651                         return status;
3652                 }
3653         }
3654
3655         /*
3656          * We have the share entry *locked*.....
3657          */
3658
3659         /* Delete streams if create_disposition requires it */
3660         if (!new_file_created && clear_ads(create_disposition) &&
3661             !is_ntfs_stream_smb_fname(smb_fname)) {
3662                 status = delete_all_streams(conn, smb_fname);
3663                 if (!NT_STATUS_IS_OK(status)) {
3664                         TALLOC_FREE(lck);
3665                         fd_close(fsp);
3666                         return status;
3667                 }
3668         }
3669
3670         /* note that we ignore failure for the following. It is
3671            basically a hack for NFS, and NFS will never set one of
3672            these only read them. Nobody but Samba can ever set a deny
3673            mode and we have already checked our more authoritative
3674            locking database for permission to set this deny mode. If
3675            the kernel refuses the operations then the kernel is wrong.
3676            note that GPFS supports it as well - jmcd */
3677
3678         if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
3679                 int ret_flock;
3680                 /*
3681                  * Beware: streams implementing VFS modules may
3682                  * implement streams in a way that fsp will have the
3683                  * basefile open in the fsp fd, so lacking a distinct
3684                  * fd for the stream kernel_flock will apply on the
3685                  * basefile which is wrong. The actual check is
3686                  * deffered to the VFS module implementing the
3687                  * kernel_flock call.
3688                  */
3689                 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
3690                 if(ret_flock == -1 ){
3691
3692                         TALLOC_FREE(lck);
3693                         fd_close(fsp);
3694
3695                         return NT_STATUS_SHARING_VIOLATION;
3696                 }
3697
3698                 fsp->kernel_share_modes_taken = true;
3699         }
3700
3701         /*
3702          * At this point onwards, we can guarantee that the share entry
3703          * is locked, whether we created the file or not, and that the
3704          * deny mode is compatible with all current opens.
3705          */
3706
3707         /*
3708          * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3709          * but we don't have to store this - just ignore it on access check.
3710          */
3711         if (conn->sconn->using_smb2) {
3712                 /*
3713                  * SMB2 doesn't return it (according to Microsoft tests).
3714                  * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
3715                  * File created with access = 0x7 (Read, Write, Delete)
3716                  * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
3717                  */
3718                 fsp->access_mask = access_mask;
3719         } else {
3720                 /* But SMB1 does. */
3721                 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3722         }
3723
3724         if (file_existed) {
3725                 /*
3726                  * stat opens on existing files don't get oplocks.
3727                  * They can get leases.
3728                  *
3729                  * Note that we check for stat open on the *open_access_mask*,
3730                  * i.e. the access mask we actually used to do the open,
3731                  * not the one the client asked for (which is in
3732                  * fsp->access_mask). This is due to the fact that
3733                  * FILE_OVERWRITE and FILE_OVERWRITE_IF add in O_TRUNC,
3734                  * which adds FILE_WRITE_DATA to open_access_mask.
3735                  */
3736                 if (is_stat_open(open_access_mask) && lease == NULL) {
3737                         oplock_request = NO_OPLOCK;
3738                 }
3739         }
3740
3741         if (new_file_created) {
3742                 info = FILE_WAS_CREATED;
3743         } else {
3744                 if (flags2 & O_TRUNC) {
3745                         info = FILE_WAS_OVERWRITTEN;
3746                 } else {
3747                         info = FILE_WAS_OPENED;
3748                 }
3749         }
3750
3751         if (pinfo) {
3752                 *pinfo = info;
3753         }
3754
3755         /*
3756          * Setup the oplock info in both the shared memory and
3757          * file structs.
3758          */
3759         status = grant_fsp_oplock_type(req, fsp, lck, oplock_request, lease);
3760         if (!NT_STATUS_IS_OK(status)) {
3761                 TALLOC_FREE(lck);
3762                 fd_close(fsp);
3763                 return status;
3764         }
3765
3766         /* Handle strange delete on close create semantics. */
3767         if (create_options & FILE_DELETE_ON_CLOSE) {
3768                 if (!new_file_created) {
3769                         status = can_set_delete_on_close(fsp,
3770                                          existing_dos_attributes);
3771
3772                         if (!NT_STATUS_IS_OK(status)) {
3773                                 /* Remember to delete the mode we just added. */
3774                                 del_share_mode(lck, fsp);
3775                                 TALLOC_FREE(lck);
3776                                 fd_close(fsp);
3777                                 return status;
3778                         }
3779                 }
3780                 /* Note that here we set the *initial* delete on close flag,
3781                    not the regular one. The magic gets handled in close. */
3782                 fsp->initial_delete_on_close = True;
3783         }
3784
3785         if (info != FILE_WAS_OPENED) {
3786                 /* Overwritten files should be initially set as archive */
3787                 if ((info == FILE_WAS_OVERWRITTEN && lp_map_archive(SNUM(conn))) ||
3788                     lp_store_dos_attributes(SNUM(conn))) {
3789                         if (!posix_open) {
3790                                 if (file_set_dosmode(conn, smb_fname,
3791                                             new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
3792                                             parent_dir, true) == 0) {
3793                                         unx_mode = smb_fname->st.st_ex_mode;
3794                                 }
3795                         }
3796                 }
3797         }
3798
3799         /* Determine sparse flag. */
3800         if (posix_open) {
3801                 /* POSIX opens are sparse by default. */
3802                 fsp->is_sparse = true;
3803         } else {
3804                 fsp->is_sparse = (file_existed &&
3805                         (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
3806         }
3807
3808         /*
3809          * Take care of inherited ACLs on created files - if default ACL not
3810          * selected.
3811          */
3812
3813         if (!posix_open && new_file_created && !def_acl) {
3814                 if (unx_mode != smb_fname->st.st_ex_mode) {
3815                         int ret = SMB_VFS_FCHMOD(fsp, unx_mode);
3816                         if (ret == -1) {
3817                                 DBG_INFO("failed to reset "
3818                                   "attributes of file %s to 0%o\n",
3819                                   smb_fname_str_dbg(smb_fname),
3820                                   (unsigned int)unx_mode);
3821                         }
3822                 }
3823
3824         } else if (new_unx_mode) {
3825                 /*
3826                  * We only get here in the case of:
3827                  *
3828                  * a). Not a POSIX open.
3829                  * b). File already existed.
3830                  * c). File was overwritten.
3831                  * d). Requested DOS attributes didn't match
3832                  *     the DOS attributes on the existing file.
3833                  *
3834                  * In that case new_unx_mode has been set
3835                  * equal to the calculated mode (including
3836                  * possible inheritance of the mode from the
3837                  * containing directory).
3838                  *
3839                  * Note this mode was calculated with the
3840                  * DOS attribute FILE_ATTRIBUTE_ARCHIVE added,
3841                  * so the mode change here is suitable for
3842                  * an overwritten file.
3843                  */
3844
3845                 if (new_unx_mode != smb_fname->st.st_ex_mode) {
3846                         int ret = SMB_VFS_FCHMOD(fsp, new_unx_mode);
3847                         if (ret == -1) {
3848                                 DBG_INFO("failed to reset "
3849                                   "attributes of file %s to 0%o\n",
3850                                   smb_fname_str_dbg(smb_fname),
3851                                   (unsigned int)new_unx_mode);
3852                         }
3853                 }
3854         }
3855
3856         {
3857                 /*
3858                  * Deal with other opens having a modified write time.
3859                  */
3860                 struct timespec write_time = get_share_mode_write_time(lck);
3861
3862                 if (!null_timespec(write_time)) {
3863                         update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3864                 }
3865         }
3866
3867         TALLOC_FREE(lck);
3868
3869         return NT_STATUS_OK;
3870 }
3871
3872 static NTSTATUS mkdir_internal(connection_struct *conn,
3873                                struct smb_filename *smb_dname,
3874                                uint32_t file_attributes)
3875 {
3876         mode_t mode;
3877         char *parent_dir = NULL;
3878         NTSTATUS status;
3879         bool posix_open = false;
3880         bool need_re_stat = false;
3881         uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
3882
3883         if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
3884                 DEBUG(5,("mkdir_internal: failing share access "
3885                          "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
3886                 return NT_STATUS_ACCESS_DENIED;
3887         }
3888
3889         if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
3890                             NULL)) {
3891                 return NT_STATUS_NO_MEMORY;
3892         }
3893
3894         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3895                 posix_open = true;
3896                 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
3897         } else {
3898                 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
3899         }
3900
3901         status = check_parent_access(conn,
3902                                         smb_dname,
3903                                         access_mask);
3904         if(!NT_STATUS_IS_OK(status)) {
3905                 DEBUG(5,("mkdir_internal: check_parent_access "
3906                         "on directory %s for path %s returned %s\n",
3907                         parent_dir,
3908                         smb_dname->base_name,
3909                         nt_errstr(status) ));
3910                 return status;
3911         }
3912
3913         if (SMB_VFS_MKDIR(conn, smb_dname, mode) != 0) {
3914                 return map_nt_error_from_unix(errno);
3915         }
3916
3917         /* Ensure we're checking for a symlink here.... */
3918         /* We don't want to get caught by a symlink racer. */
3919
3920         if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3921                 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3922                           smb_fname_str_dbg(smb_dname), strerror(errno)));
3923                 return map_nt_error_from_unix(errno);
3924         }
3925
3926         if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
3927                 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3928                           smb_fname_str_dbg(smb_dname)));
3929                 return NT_STATUS_NOT_A_DIRECTORY;
3930         }
3931
3932         if (lp_store_dos_attributes(SNUM(conn))) {
3933                 if (!posix_open) {
3934                         file_set_dosmode(conn, smb_dname,
3935                                          file_attributes | FILE_ATTRIBUTE_DIRECTORY,
3936                                          parent_dir, true);
3937                 }
3938         }
3939
3940         if (lp_inherit_permissions(SNUM(conn))) {
3941                 inherit_access_posix_acl(conn, parent_dir,
3942                                          smb_dname, mode);
3943                 need_re_stat = true;
3944         }
3945
3946         if (!posix_open) {
3947                 /*
3948                  * Check if high bits should have been set,
3949                  * then (if bits are missing): add them.
3950                  * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3951                  * dir.
3952                  */
3953                 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
3954                     (mode & ~smb_dname->st.st_ex_mode)) {
3955                         SMB_VFS_CHMOD(conn, smb_dname,
3956                                       (smb_dname->st.st_ex_mode |
3957                                           (mode & ~smb_dname->st.st_ex_mode)));
3958                         need_re_stat = true;
3959                 }
3960         }
3961
3962         /* Change the owner if required. */
3963         if (lp_inherit_owner(SNUM(conn)) != INHERIT_OWNER_NO) {
3964                 change_dir_owner_to_parent(conn, parent_dir,
3965                                            smb_dname,
3966                                            &smb_dname->st);
3967                 need_re_stat = true;
3968         }
3969
3970         if (need_re_stat) {
3971                 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3972                         DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3973                           smb_fname_str_dbg(smb_dname), strerror(errno)));
3974                         return map_nt_error_from_unix(errno);
3975                 }
3976         }
3977
3978         notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
3979                      smb_dname->base_name);
3980
3981         return NT_STATUS_OK;
3982 }
3983
3984 /****************************************************************************
3985  Open a directory from an NT SMB call.
3986 ****************************************************************************/
3987
3988 static NTSTATUS open_directory(connection_struct *conn,
3989                                struct smb_request *req,
3990                                struct smb_filename *smb_dname,
3991                                uint32_t access_mask,
3992                                uint32_t share_access,
3993                                uint32_t create_disposition,
3994                                uint32_t create_options,
3995                                uint32_t file_attributes,
3996                                int *pinfo,
3997                                files_struct **result)
3998 {
3999         files_struct *fsp = NULL;
4000         bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
4001         struct share_mode_lock *lck = NULL;
4002         NTSTATUS status;
4003         struct timespec mtimespec;
4004         int info = 0;
4005         bool ok;
4006
4007         if (is_ntfs_stream_smb_fname(smb_dname)) {
4008                 DEBUG(2, ("open_directory: %s is a stream name!\n",
4009                           smb_fname_str_dbg(smb_dname)));
4010                 return NT_STATUS_NOT_A_DIRECTORY;
4011         }
4012
4013         if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
4014                 /* Ensure we have a directory attribute. */
4015                 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
4016         }
4017
4018         DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
4019                  "share_access = 0x%x create_options = 0x%x, "
4020                  "create_disposition = 0x%x, file_attributes = 0x%x\n",
4021                  smb_fname_str_dbg(smb_dname),
4022                  (unsigned int)access_mask,
4023                  (unsigned int)share_access,
4024                  (unsigned int)create_options,
4025                  (unsigned int)create_disposition,
4026                  (unsigned int)file_attributes));
4027
4028         status = smbd_calculate_access_mask(conn, smb_dname, false,
4029                                             access_mask, &access_mask);
4030         if (!NT_STATUS_IS_OK(status)) {
4031                 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
4032                         "on file %s returned %s\n",
4033                         smb_fname_str_dbg(smb_dname),
4034                         nt_errstr(status)));
4035                 return status;
4036         }
4037
4038         if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
4039                         !security_token_has_privilege(get_current_nttok(conn),
4040                                         SEC_PRIV_SECURITY)) {
4041                 DEBUG(10, ("open_directory: open on %s "
4042                         "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
4043                         smb_fname_str_dbg(smb_dname)));
4044                 return NT_STATUS_PRIVILEGE_NOT_HELD;
4045         }
4046
4047         switch( create_disposition ) {
4048                 case FILE_OPEN:
4049
4050                         if (!dir_existed) {
4051                                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4052                         }
4053
4054                         info = FILE_WAS_OPENED;
4055                         break;
4056
4057                 case FILE_CREATE:
4058
4059                         /* If directory exists error. If directory doesn't
4060                          * exist create. */
4061
4062                         if (dir_existed) {
4063                                 status = NT_STATUS_OBJECT_NAME_COLLISION;
4064                                 DEBUG(2, ("open_directory: unable to create "
4065                                           "%s. Error was %s\n",
4066                                           smb_fname_str_dbg(smb_dname),
4067                                           nt_errstr(status)));
4068                                 return status;
4069                         }
4070
4071                         status = mkdir_internal(conn, smb_dname,
4072                                                 file_attributes);
4073
4074                         if (!NT_STATUS_IS_OK(status)) {
4075                                 DEBUG(2, ("open_directory: unable to create "
4076                                           "%s. Error was %s\n",
4077                                           smb_fname_str_dbg(smb_dname),
4078                                           nt_errstr(status)));
4079                                 return status;
4080                         }
4081
4082                         info = FILE_WAS_CREATED;
4083                         break;
4084
4085                 case FILE_OPEN_IF:
4086                         /*
4087                          * If directory exists open. If directory doesn't
4088                          * exist create.
4089                          */
4090
4091                         if (dir_existed) {
4092                                 status = NT_STATUS_OK;
4093                                 info = FILE_WAS_OPENED;
4094                         } else {
4095                                 status = mkdir_internal(conn, smb_dname,
4096                                                 file_attributes);
4097
4098                                 if (NT_STATUS_IS_OK(status)) {
4099                                         info = FILE_WAS_CREATED;
4100                                 } else {
4101                                         /* Cope with create race. */
4102                                         if (!NT_STATUS_EQUAL(status,
4103                                                         NT_STATUS_OBJECT_NAME_COLLISION)) {
4104                                                 DEBUG(2, ("open_directory: unable to create "
4105                                                         "%s. Error was %s\n",
4106                                                         smb_fname_str_dbg(smb_dname),
4107                                                         nt_errstr(status)));
4108                                                 return status;
4109                                         }
4110
4111                                         /*
4112                                          * If mkdir_internal() returned
4113                                          * NT_STATUS_OBJECT_NAME_COLLISION
4114                                          * we still must lstat the path.
4115                                          */
4116
4117                                         if (SMB_VFS_LSTAT(conn, smb_dname)
4118                                                         == -1) {
4119                                                 DEBUG(2, ("Could not stat "
4120                                                         "directory '%s' just "
4121                                                         "opened: %s\n",
4122                                                         smb_fname_str_dbg(
4123                                                                 smb_dname),
4124                                                         strerror(errno)));
4125                                                 return map_nt_error_from_unix(
4126                                                                 errno);
4127                                         }
4128
4129                                         info = FILE_WAS_OPENED;
4130                                 }
4131                         }
4132
4133                         break;
4134
4135                 case FILE_SUPERSEDE:
4136                 case FILE_OVERWRITE:
4137                 case FILE_OVERWRITE_IF:
4138                 default:
4139                         DEBUG(5,("open_directory: invalid create_disposition "
4140                                  "0x%x for directory %s\n",
4141                                  (unsigned int)create_disposition,
4142                                  smb_fname_str_dbg(smb_dname)));
4143                         return NT_STATUS_INVALID_PARAMETER;
4144         }
4145
4146         if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
4147                 DEBUG(5,("open_directory: %s is not a directory !\n",
4148                          smb_fname_str_dbg(smb_dname)));
4149                 return NT_STATUS_NOT_A_DIRECTORY;
4150         }
4151
4152         if (info == FILE_WAS_OPENED) {
4153                 status = smbd_check_access_rights(conn,
4154                                                 smb_dname,
4155                                                 false,
4156                                                 access_mask);
4157                 if (!NT_STATUS_IS_OK(status)) {
4158                         DEBUG(10, ("open_directory: smbd_check_access_rights on "
4159                                 "file %s failed with %s\n",
4160                                 smb_fname_str_dbg(smb_dname),
4161                                 nt_errstr(status)));
4162                         return status;
4163                 }
4164         }
4165
4166         status = file_new(req, conn, &fsp);
4167         if(!NT_STATUS_IS_OK(status)) {
4168                 return status;
4169         }
4170
4171         /*
4172          * Setup the files_struct for it.
4173          */
4174
4175         fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
4176         fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
4177         fsp->file_pid = req ? req->smbpid : 0;
4178         fsp->can_lock = False;
4179         fsp->can_read = False;
4180         fsp->can_write = False;
4181
4182         fsp->share_access = share_access;
4183         fsp->fh->private_options = 0;
4184         /*
4185          * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
4186          */
4187         fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
4188         fsp->print_file = NULL;
4189         fsp->modified = False;
4190         fsp->oplock_type = NO_OPLOCK;
4191         fsp->sent_oplock_break = NO_BREAK_SENT;
4192         fsp->is_directory = True;
4193         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
4194                 fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
4195         }
4196         status = fsp_set_smb_fname(fsp, smb_dname);
4197         if (!NT_STATUS_IS_OK(status)) {
4198                 file_free(req, fsp);
4199                 return status;
4200         }
4201
4202         /* Don't store old timestamps for directory
4203            handles in the internal database. We don't
4204            update them in there if new objects
4205            are creaded in the directory. Currently
4206            we only update timestamps on file writes.
4207            See bug #9870.
4208         */
4209         ZERO_STRUCT(mtimespec);
4210
4211         if (access_mask & (FILE_LIST_DIRECTORY|
4212                            FILE_ADD_FILE|
4213                            FILE_ADD_SUBDIRECTORY|
4214                            FILE_TRAVERSE|
4215                            DELETE_ACCESS|
4216                            FILE_DELETE_CHILD)) {
4217 #ifdef O_DIRECTORY
4218                 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
4219 #else
4220                 /* POSIX allows us to open a directory with O_RDONLY. */
4221                 status = fd_open(conn, fsp, O_RDONLY, 0);
4222 #endif
4223                 if (!NT_STATUS_IS_OK(status)) {
4224                         DEBUG(5, ("open_directory: Could not open fd for "
4225                                 "%s (%s)\n",
4226                                 smb_fname_str_dbg(smb_dname),
4227                                 nt_errstr(status)));
4228                         file_free(req, fsp);
4229                         return status;
4230                 }
4231         } else {
4232                 fsp->fh->fd = -1;
4233                 DEBUG(10, ("Not opening Directory %s\n",
4234                         smb_fname_str_dbg(smb_dname)));
4235         }
4236
4237         status = vfs_stat_fsp(fsp);
4238         if (!NT_STATUS_IS_OK(status)) {
4239                 fd_close(fsp);
4240                 file_free(req, fsp);
4241                 return status;
4242         }
4243
4244         if(!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4245                 DEBUG(5,("open_directory: %s is not a directory !\n",
4246                          smb_fname_str_dbg(smb_dname)));
4247                 fd_close(fsp);
4248                 file_free(req, fsp);
4249                 return NT_STATUS_NOT_A_DIRECTORY;
4250         }
4251
4252         /* Ensure there was no race condition.  We need to check
4253          * dev/inode but not permissions, as these can change
4254          * legitimately */
4255         if (!check_same_dev_ino(&smb_dname->st, &fsp->fsp_name->st)) {
4256                 DEBUG(5,("open_directory: stat struct differs for "
4257                         "directory %s.\n",
4258                         smb_fname_str_dbg(smb_dname)));
4259                 fd_close(fsp);
4260                 file_free(req, fsp);
4261                 return NT_STATUS_ACCESS_DENIED;
4262         }
4263
4264         lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
4265                                   conn->connectpath, smb_dname,
4266                                   &mtimespec);
4267
4268         if (lck == NULL) {
4269                 DEBUG(0, ("open_directory: Could not get share mode lock for "
4270                           "%s\n", smb_fname_str_dbg(smb_dname)));
4271                 fd_close(fsp);
4272                 file_free(req, fsp);
4273                 return NT_STATUS_SHARING_VIOLATION;
4274         }
4275
4276         if (has_delete_on_close(lck, fsp->name_hash)) {
4277                 TALLOC_FREE(lck);
4278                 fd_close(fsp);
4279                 file_free(req, fsp);
4280                 return NT_STATUS_DELETE_PENDING;
4281         }
4282
4283         status = open_mode_check(conn, lck,
4284                                  access_mask, share_access);
4285
4286         if (!NT_STATUS_IS_OK(status)) {
4287                 TALLOC_FREE(lck);
4288                 fd_close(fsp);
4289                 file_free(req, fsp);
4290                 return status;
4291         }
4292
4293         ok = set_share_mode(lck, fsp, get_current_uid(conn),
4294                             req ? req->mid : 0, NO_OPLOCK,
4295                             UINT32_MAX);
4296         if (!ok) {
4297                 TALLOC_FREE(lck);
4298                 fd_close(fsp);
4299                 file_free(req, fsp);
4300                 return NT_STATUS_NO_MEMORY;
4301         }
4302
4303         /* For directories the delete on close bit at open time seems
4304            always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
4305         if (create_options & FILE_DELETE_ON_CLOSE) {
4306                 status = can_set_delete_on_close(fsp, 0);
4307                 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
4308                         del_share_mode(lck, fsp);
4309                         TALLOC_FREE(lck);
4310                         fd_close(fsp);
4311                         file_free(req, fsp);
4312                         return status;
4313                 }
4314
4315                 if (NT_STATUS_IS_OK(status)) {
4316                         /* Note that here we set the *initial* delete on close flag,
4317                            not the regular one. The magic gets handled in close. */
4318                         fsp->initial_delete_on_close = True;
4319                 }
4320         }
4321
4322         {
4323                 /*
4324                  * Deal with other opens having a modified write time. Is this
4325                  * possible for directories?
4326                  */
4327                 struct timespec write_time = get_share_mode_write_time(lck);
4328
4329                 if (!null_timespec(write_time)) {
4330                         update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
4331                 }
4332         }
4333
4334         TALLOC_FREE(lck);
4335
4336         if (pinfo) {
4337                 *pinfo = info;
4338         }
4339
4340         *result = fsp;
4341         return NT_STATUS_OK;
4342 }
4343
4344 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
4345                           struct smb_filename *smb_dname)
4346 {
4347         NTSTATUS status;
4348         files_struct *fsp;
4349
4350         status = SMB_VFS_CREATE_FILE(
4351                 conn,                                   /* conn */
4352                 req,                                    /* req */
4353                 0,                                      /* root_dir_fid */
4354                 smb_dname,                              /* fname */
4355                 FILE_READ_ATTRIBUTES,                   /* access_mask */
4356                 FILE_SHARE_NONE,                        /* share_access */
4357                 FILE_CREATE,                            /* create_disposition*/
4358                 FILE_DIRECTORY_FILE,                    /* create_options */
4359                 FILE_ATTRIBUTE_DIRECTORY,               /* file_attributes */
4360                 0,                                      /* oplock_request */
4361                 NULL,                                   /* lease */
4362                 0,                                      /* allocation_size */
4363                 0,                                      /* private_flags */
4364                 NULL,                                   /* sd */
4365                 NULL,                                   /* ea_list */
4366                 &fsp,                                   /* result */
4367                 NULL,                                   /* pinfo */
4368                 NULL, NULL);                            /* create context */
4369
4370         if (NT_STATUS_IS_OK(status)) {
4371                 close_file(req, fsp, NORMAL_CLOSE);
4372         }
4373
4374         return status;
4375 }
4376
4377 /****************************************************************************
4378  Receive notification that one of our open files has been renamed by another
4379  smbd process.
4380 ****************************************************************************/
4381
4382 void msg_file_was_renamed(struct messaging_context *msg,
4383                           void *private_data,
4384                           uint32_t msg_type,
4385                           struct server_id server_id,
4386                           DATA_BLOB *data)
4387 {
4388         files_struct *fsp;
4389         char *frm = (char *)data->data;
4390         struct file_id id;
4391         const char *sharepath;
4392         const char *base_name;
4393         const char *stream_name;
4394         struct smb_filename *smb_fname = NULL;
4395         size_t sp_len, bn_len;
4396         NTSTATUS status;
4397         struct smbd_server_connection *sconn =
4398                 talloc_get_type_abort(private_data,
4399                 struct smbd_server_connection);
4400
4401         if (data->data == NULL
4402             || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
4403                 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
4404                           (int)data->length));
4405                 return;
4406         }
4407
4408         /* Unpack the message. */
4409         pull_file_id_24(frm, &id);
4410         sharepath = &frm[24];
4411         sp_len = strlen(sharepath);
4412         base_name = sharepath + sp_len + 1;
4413         bn_len = strlen(base_name);
4414         stream_name = sharepath + sp_len + 1 + bn_len + 1;
4415
4416         /* stream_name must always be NULL if there is no stream. */
4417         if (stream_name[0] == '\0') {
4418                 stream_name = NULL;
4419         }
4420
4421         smb_fname = synthetic_smb_fname(talloc_tos(),
4422                                         base_name,
4423                                         stream_name,
4424                                         NULL,
4425                                         0);
4426         if (smb_fname == NULL) {
4427                 return;
4428         }
4429
4430         DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
4431                 "file_id %s\n",
4432                 sharepath, smb_fname_str_dbg(smb_fname),
4433                 file_id_string_tos(&id)));
4434
4435         for(fsp = file_find_di_first(sconn, id); fsp;
4436             fsp = file_find_di_next(fsp)) {
4437                 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
4438
4439                         DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
4440                                 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
4441                                 smb_fname_str_dbg(smb_fname)));
4442                         status = fsp_set_smb_fname(fsp, smb_fname);
4443                         if (!NT_STATUS_IS_OK(status)) {
4444                                 goto out;
4445                         }
4446                 } else {
4447                         /* TODO. JRA. */
4448                         /* Now we have the complete path we can work out if this is
4449                            actually within this share and adjust newname accordingly. */
4450                         DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
4451                                 "not sharepath %s) "
4452                                 "%s from %s -> %s\n",
4453                                 fsp->conn->connectpath,
4454                                 sharepath,
4455                                 fsp_fnum_dbg(fsp),
4456                                 fsp_str_dbg(fsp),
4457                                 smb_fname_str_dbg(smb_fname)));
4458                 }
4459         }
4460  out:
4461         TALLOC_FREE(smb_fname);
4462         return;
4463 }
4464
4465 /*
4466  * If a main file is opened for delete, all streams need to be checked for
4467  * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
4468  * If that works, delete them all by setting the delete on close and close.
4469  */
4470
4471 static NTSTATUS open_streams_for_delete(connection_struct *conn,
4472                                         const struct smb_filename *smb_fname)
4473 {
4474         struct stream_struct *stream_info = NULL;
4475         files_struct **streams = NULL;
4476         int i;
4477         unsigned int num_streams = 0;
4478         TALLOC_CTX *frame = talloc_stackframe();
4479         NTSTATUS status;
4480
4481         status = vfs_streaminfo(conn, NULL, smb_fname, talloc_tos(),
4482                                 &num_streams, &stream_info);
4483
4484         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
4485             || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
4486                 DEBUG(10, ("no streams around\n"));
4487                 TALLOC_FREE(frame);
4488                 return NT_STATUS_OK;
4489         }
4490
4491         if (!NT_STATUS_IS_OK(status)) {
4492                 DEBUG(10, ("vfs_streaminfo failed: %s\n",
4493                            nt_errstr(status)));
4494                 goto fail;
4495         }
4496
4497         DEBUG(10, ("open_streams_for_delete found %d streams\n",
4498                    num_streams));
4499
4500         if (num_streams == 0) {
4501                 TALLOC_FREE(frame);
4502                 return NT_STATUS_OK;
4503         }
4504
4505         streams = talloc_array(talloc_tos(), files_struct *, num_streams);
4506         if (streams == NULL) {
4507                 DEBUG(0, ("talloc failed\n"));
4508                 status = NT_STATUS_NO_MEMORY;
4509                 goto fail;
4510         }
4511
4512         for (i=0; i<num_streams; i++) {
4513                 struct smb_filename *smb_fname_cp;
4514
4515                 if (strequal(stream_info[i].name, "::$DATA")) {
4516                         streams[i] = NULL;
4517                         continue;
4518                 }
4519
4520                 smb_fname_cp = synthetic_smb_fname(talloc_tos(),
4521                                         smb_fname->base_name,
4522                                         stream_info[i].name,
4523                                         NULL,
4524                                         (smb_fname->flags &
4525                                                 ~SMB_FILENAME_POSIX_PATH));
4526                 if (smb_fname_cp == NULL) {
4527                         status = NT_STATUS_NO_MEMORY;
4528                         goto fail;
4529                 }
4530
4531                 if (SMB_VFS_STAT(conn, smb_fname_cp) == -1) {
4532                         DEBUG(10, ("Unable to stat stream: %s\n",
4533                                    smb_fname_str_dbg(smb_fname_cp)));
4534                 }
4535
4536                 status = SMB_VFS_CREATE_FILE(
4537                          conn,                  /* conn */
4538                          NULL,                  /* req */
4539                          0,                     /* root_dir_fid */
4540                          smb_fname_cp,          /* fname */
4541                          DELETE_ACCESS,         /* access_mask */
4542                          (FILE_SHARE_READ |     /* share_access */
4543                              FILE_SHARE_WRITE | FILE_SHARE_DELETE),
4544                          FILE_OPEN,             /* create_disposition*/
4545                          0,                     /* create_options */
4546                          FILE_ATTRIBUTE_NORMAL, /* file_attributes */
4547                          0,                     /* oplock_request */
4548                          NULL,                  /* lease */
4549                          0,                     /* allocation_size */
4550                          NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
4551                          NULL,                  /* sd */
4552                          NULL,                  /* ea_list */
4553                          &streams[i],           /* result */
4554                          NULL,                  /* pinfo */
4555                          NULL, NULL);           /* create context */
4556
4557                 if (!NT_STATUS_IS_OK(status)) {
4558                         DEBUG(10, ("Could not open stream %s: %s\n",
4559                                    smb_fname_str_dbg(smb_fname_cp),
4560                                    nt_errstr(status)));
4561
4562                         TALLOC_FREE(smb_fname_cp);
4563                         break;
4564                 }
4565                 TALLOC_FREE(smb_fname_cp);
4566         }
4567
4568         /*
4569          * don't touch the variable "status" beyond this point :-)
4570          */
4571
4572         for (i -= 1 ; i >= 0; i--) {
4573                 if (streams[i] == NULL) {
4574                         continue;
4575                 }
4576
4577                 DEBUG(10, ("Closing stream # %d, %s\n", i,
4578                            fsp_str_dbg(streams[i])));
4579                 close_file(NULL, streams[i], NORMAL_CLOSE);
4580         }
4581
4582  fail:
4583         TALLOC_FREE(frame);
4584         return status;
4585 }
4586
4587 /*********************************************************************
4588  Create a default ACL by inheriting from the parent. If no inheritance
4589  from the parent available, don't set anything. This will leave the actual
4590  permissions the new file or directory already got from the filesystem
4591  as the NT ACL when read.
4592 *********************************************************************/
4593
4594 static NTSTATUS inherit_new_acl(files_struct *fsp)
4595 {
4596         TALLOC_CTX *frame = talloc_stackframe();
4597         char *parent_name = NULL;
4598         struct security_descriptor *parent_desc = NULL;
4599         NTSTATUS status = NT_STATUS_OK;
4600         struct security_descriptor *psd = NULL;
4601         const struct dom_sid *owner_sid = NULL;
4602         const struct dom_sid *group_sid = NULL;
4603         uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
4604         struct security_token *token = fsp->conn->session_info->security_token;
4605         bool inherit_owner =
4606             (lp_inherit_owner(SNUM(fsp->conn)) == INHERIT_OWNER_WINDOWS_AND_UNIX);
4607         bool inheritable_components = false;
4608         bool try_builtin_administrators = false;
4609         const struct dom_sid *BA_U_sid = NULL;
4610         const struct dom_sid *BA_G_sid = NULL;
4611         bool try_system = false;
4612         const struct dom_sid *SY_U_sid = NULL;
4613         const struct dom_sid *SY_G_sid = NULL;
4614         size_t size = 0;
4615         struct smb_filename *parent_smb_fname = NULL;
4616
4617         if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
4618                 TALLOC_FREE(frame);
4619                 return NT_STATUS_NO_MEMORY;
4620         }
4621         parent_smb_fname = synthetic_smb_fname(talloc_tos(),
4622                                                 parent_name,
4623                                                 NULL,
4624                                                 NULL,
4625                                                 fsp->fsp_name->flags);
4626
4627         if (parent_smb_fname == NULL) {
4628                 TALLOC_FREE(frame);
4629                 return NT_STATUS_NO_MEMORY;
4630         }
4631
4632         status = SMB_VFS_GET_NT_ACL(fsp->conn,
4633                                     parent_smb_fname,
4634                                     (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
4635                                     frame,
4636                                     &parent_desc);
4637         if (!NT_STATUS_IS_OK(status)) {
4638                 TALLOC_FREE(frame);
4639                 return status;
4640         }
4641
4642         inheritable_components = sd_has_inheritable_components(parent_desc,
4643                                         fsp->is_directory);
4644
4645         if (!inheritable_components && !inherit_owner) {
4646                 TALLOC_FREE(frame);
4647                 /* Nothing to inherit and not setting owner. */
4648                 return NT_STATUS_OK;
4649         }
4650
4651         /* Create an inherited descriptor from the parent. */
4652
4653         if (DEBUGLEVEL >= 10) {
4654                 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
4655                         fsp_str_dbg(fsp) ));
4656                 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
4657         }
4658
4659         /* Inherit from parent descriptor if "inherit owner" set. */
4660         if (inherit_owner) {
4661                 owner_sid = parent_desc->owner_sid;
4662                 group_sid = parent_desc->group_sid;
4663         }
4664
4665         if (owner_sid == NULL) {
4666                 if (security_token_has_builtin_administrators(token)) {
4667                         try_builtin_administrators = true;
4668                 } else if (security_token_is_system(token)) {
4669                         try_builtin_administrators = true;
4670                         try_system = true;
4671                 }
4672         }
4673
4674         if (group_sid == NULL &&
4675             token->num_sids == PRIMARY_GROUP_SID_INDEX)
4676         {
4677                 if (security_token_is_system(token)) {
4678                         try_builtin_administrators = true;
4679                         try_system = true;
4680                 }
4681         }
4682
4683         if (try_builtin_administrators) {
4684                 struct unixid ids;
4685                 bool ok;
4686
4687                 ZERO_STRUCT(ids);
4688                 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
4689                 if (ok) {
4690                         switch (ids.type) {
4691                         case ID_TYPE_BOTH:
4692                                 BA_U_sid = &global_sid_Builtin_Administrators;
4693                                 BA_G_sid = &global_sid_Builtin_Administrators;
4694                                 break;
4695                         case ID_TYPE_UID:
4696                                 BA_U_sid = &global_sid_Builtin_Administrators;
4697                                 break;
4698                         case ID_TYPE_GID:
4699                                 BA_G_sid = &global_sid_Builtin_Administrators;
4700                                 break;
4701                         default:
4702                                 break;
4703                         }
4704                 }
4705         }
4706
4707         if (try_system) {
4708                 struct unixid ids;
4709                 bool ok;
4710
4711                 ZERO_STRUCT(ids);
4712                 ok = sids_to_unixids(&global_sid_System, 1, &ids);
4713                 if (ok) {
4714                         switch (ids.type) {
4715                         case ID_TYPE_BOTH:
4716                                 SY_U_sid = &global_sid_System;
4717                                 SY_G_sid = &global_sid_System;
4718                                 break;
4719                         case ID_TYPE_UID:
4720                                 SY_U_sid = &global_sid_System;
4721                                 break;
4722                         case ID_TYPE_GID:
4723                                 SY_G_sid = &global_sid_System;
4724                                 break;
4725                         default:
4726                                 break;
4727                         }
4728                 }
4729         }
4730
4731         if (owner_sid == NULL) {
4732                 owner_sid = BA_U_sid;
4733         }
4734
4735         if (owner_sid == NULL) {
4736                 owner_sid = SY_U_sid;
4737         }
4738
4739         if (group_sid == NULL) {
4740                 group_sid = SY_G_sid;
4741         }
4742
4743         if (try_system && group_sid == NULL) {
4744                 group_sid = BA_G_sid;
4745         }
4746
4747         if (owner_sid == NULL) {
4748                 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
4749         }
4750         if (group_sid == NULL) {
4751                 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
4752                         group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
4753                 } else {
4754                         group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
4755                 }
4756         }
4757
4758         status = se_create_child_secdesc(frame,
4759                         &psd,
4760                         &size,
4761                         parent_desc,
4762                         owner_sid,
4763                         group_sid,
4764                         fsp->is_directory);
4765         if (!NT_STATUS_IS_OK(status)) {
4766                 TALLOC_FREE(frame);
4767                 return status;
4768         }
4769
4770         /* If inheritable_components == false,
4771            se_create_child_secdesc()
4772            creates a security descriptor with a NULL dacl
4773            entry, but with SEC_DESC_DACL_PRESENT. We need
4774            to remove that flag. */
4775
4776         if (!inheritable_components) {
4777                 security_info_sent &= ~SECINFO_DACL;
4778                 psd->type &= ~SEC_DESC_DACL_PRESENT;
4779         }
4780
4781         if (DEBUGLEVEL >= 10) {
4782                 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
4783                         fsp_str_dbg(fsp) ));
4784                 NDR_PRINT_DEBUG(security_descriptor, psd);
4785         }
4786
4787         if (inherit_owner) {
4788                 /* We need to be root to force this. */
4789                 become_root();
4790         }
4791         status = SMB_VFS_FSET_NT_ACL(fsp,
4792                         security_info_sent,
4793                         psd);
4794         if (inherit_owner) {
4795                 unbecome_root();
4796         }
4797         TALLOC_FREE(frame);
4798         return status;
4799 }
4800
4801 /*
4802  * If we already have a lease, it must match the new file id. [MS-SMB2]
4803  * 3.3.5.9.8 speaks about INVALID_PARAMETER if an already used lease key is
4804  * used for a different file name.
4805  */
4806
4807 struct lease_match_state {
4808         /* Input parameters. */
4809         TALLOC_CTX *mem_ctx;
4810         const char *servicepath;
4811         const struct smb_filename *fname;
4812         bool file_existed;
4813         struct file_id id;
4814         /* Return parameters. */
4815         uint32_t num_file_ids;
4816         struct file_id *ids;
4817         NTSTATUS match_status;
4818 };
4819
4820 /*************************************************************
4821  File doesn't exist but this lease key+guid is already in use.
4822
4823  This is only allowable in the dynamic share case where the
4824  service path must be different.
4825
4826  There is a small race condition here in the multi-connection
4827  case where a client sends two create calls on different connections,
4828  where the file doesn't exist and one smbd creates the leases_db
4829  entry first, but this will get fixed by the multichannel cleanup
4830  when all identical client_guids get handled by a single smbd.
4831 **************************************************************/
4832
4833 static void lease_match_parser_new_file(
4834         uint32_t num_files,
4835         const struct leases_db_file *files,
4836         struct lease_match_state *state)
4837 {
4838         uint32_t i;
4839
4840         for (i = 0; i < num_files; i++) {
4841                 const struct leases_db_file *f = &files[i];
4842                 if (strequal(state->servicepath, f->servicepath)) {
4843                         state->match_status = NT_STATUS_INVALID_PARAMETER;
4844                         return;
4845                 }
4846         }
4847
4848         /* Dynamic share case. Break leases on all other files. */
4849         state->match_status = leases_db_copy_file_ids(state->mem_ctx,
4850                                         num_files,
4851                                         files,
4852                                         &state->ids);
4853         if (!NT_STATUS_IS_OK(state->match_status)) {
4854                 return;
4855         }
4856
4857         state->num_file_ids = num_files;
4858         state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4859         return;
4860 }
4861
4862 static void lease_match_parser(
4863         uint32_t num_files,
4864         const struct leases_db_file *files,
4865         void *private_data)
4866 {
4867         struct lease_match_state *state =
4868                 (struct lease_match_state *)private_data;
4869         uint32_t i;
4870
4871         if (!state->file_existed) {
4872                 /*
4873                  * Deal with name mismatch or
4874                  * possible dynamic share case separately
4875                  * to make code clearer.
4876                  */
4877                 lease_match_parser_new_file(num_files,
4878                                                 files,
4879                                                 state);
4880                 return;
4881         }
4882
4883         /* File existed. */
4884         state->match_status = NT_STATUS_OK;
4885
4886         for (i = 0; i < num_files; i++) {
4887                 const struct leases_db_file *f = &files[i];
4888
4889                 /* Everything should be the same. */
4890                 if (!file_id_equal(&state->id, &f->id)) {
4891                         /* This should catch all dynamic share cases. */
4892                         state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4893                         break;
4894                 }
4895                 if (!strequal(f->servicepath, state->servicepath)) {
4896                         state->match_status = NT_STATUS_INVALID_PARAMETER;
4897                         break;
4898                 }
4899                 if (!strequal(f->base_name, state->fname->base_name)) {
4900                         state->match_status = NT_STATUS_INVALID_PARAMETER;
4901                         break;
4902                 }
4903                 if (!strequal(f->stream_name, state->fname->stream_name)) {
4904                         state->match_status = NT_STATUS_INVALID_PARAMETER;
4905                         break;
4906                 }
4907         }
4908
4909         if (NT_STATUS_IS_OK(state->match_status)) {
4910                 /*
4911                  * Common case - just opening another handle on a
4912                  * file on a non-dynamic share.
4913                  */
4914                 return;
4915         }
4916
4917         if (NT_STATUS_EQUAL(state->match_status, NT_STATUS_INVALID_PARAMETER)) {
4918                 /* Mismatched path. Error back to client. */
4919                 return;
4920         }
4921
4922         /*
4923          * File id mismatch. Dynamic share case NT_STATUS_OPLOCK_NOT_GRANTED.
4924          * Don't allow leases.
4925          */
4926
4927         state->match_status = leases_db_copy_file_ids(state->mem_ctx,
4928                                         num_files,
4929                                         files,
4930                                         &state->ids);
4931         if (!NT_STATUS_IS_OK(state->match_status)) {
4932                 return;
4933         }
4934
4935         state->num_file_ids = num_files;
4936         state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4937         return;
4938 }
4939
4940 static NTSTATUS lease_match(connection_struct *conn,
4941                             struct smb_request *req,
4942                             struct smb2_lease_key *lease_key,
4943                             const char *servicepath,
4944                             const struct smb_filename *fname,
4945                             uint16_t *p_version,
4946                             uint16_t *p_epoch)
4947 {
4948         struct smbd_server_connection *sconn = req->sconn;
4949         TALLOC_CTX *tos = talloc_tos();
4950         struct lease_match_state state = {
4951                 .mem_ctx = tos,
4952                 .servicepath = servicepath,
4953                 .fname = fname,
4954                 .match_status = NT_STATUS_OK
4955         };
4956         uint32_t i;
4957         NTSTATUS status;
4958
4959         state.file_existed = VALID_STAT(fname->st);
4960         if (state.file_existed) {
4961                 state.id = vfs_file_id_from_sbuf(conn, &fname->st);
4962         }
4963
4964         status = leases_db_parse(&sconn->client->connections->smb2.client.guid,
4965                                  lease_key, lease_match_parser, &state);
4966         if (!NT_STATUS_IS_OK(status)) {
4967                 /*
4968                  * Not found or error means okay: We can make the lease pass
4969                  */
4970                 return NT_STATUS_OK;
4971         }
4972         if (!NT_STATUS_EQUAL(state.match_status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
4973                 /*
4974                  * Anything but NT_STATUS_OPLOCK_NOT_GRANTED, let the caller
4975                  * deal with it.
4976                  */
4977                 return state.match_status;
4978         }
4979
4980         /* We have to break all existing leases. */
4981         for (i = 0; i < state.num_file_ids; i++) {
4982                 struct share_mode_lock *lck;
4983                 struct share_mode_data *d;
4984                 uint32_t j;
4985
4986                 if (file_id_equal(&state.ids[i], &state.id)) {
4987                         /* Don't need to break our own file. */
4988                         continue;
4989                 }
4990
4991                 lck = get_existing_share_mode_lock(talloc_tos(), state.ids[i]);
4992                 if (lck == NULL) {
4993                         /* Race condition - file already closed. */
4994                         continue;
4995                 }
4996                 d = lck->data;
4997                 for (j=0; j<d->num_share_modes; j++) {
4998                         struct share_mode_entry *e = &d->share_modes[j];
4999                         uint32_t e_lease_type = get_lease_type(d, e);
5000
5001                         if (share_mode_stale_pid(d, j)) {
5002                                 continue;
5003                         }
5004
5005                         if (e->op_type == LEASE_OPLOCK) {
5006                                 struct share_mode_lease *l = NULL;
5007                                 l = &lck->data->leases[e->lease_idx];
5008                                 if (!smb2_lease_key_equal(&l->lease_key,
5009                                                           lease_key)) {
5010                                         continue;
5011                                 }
5012                                 *p_epoch = l->epoch;
5013                                 *p_version = l->lease_version;
5014                         }
5015
5016                         if (e_lease_type == SMB2_LEASE_NONE) {
5017                                 continue;
5018                         }
5019
5020                         send_break_message(conn->sconn->msg_ctx, &d->id, e,
5021                                            SMB2_LEASE_NONE);
5022
5023                         /*
5024                          * Windows 7 and 8 lease clients
5025                          * are broken in that they will not
5026                          * respond to lease break requests
5027                          * whilst waiting for an outstanding
5028                          * open request on that lease handle
5029                          * on the same TCP connection, due
5030                          * to holding an internal inode lock.
5031                          *
5032                          * This means we can't reschedule
5033                          * ourselves here, but must return
5034                          * from the create.
5035                          *
5036                          * Work around:
5037                          *
5038                          * Send the breaks and then return
5039                          * SMB2_LEASE_NONE in the lease handle
5040                          * to cause them to acknowledge the
5041                          * lease break. Consultation with
5042                          * Microsoft engineering confirmed
5043                          * this approach is safe.
5044                          */
5045
5046                 }
5047                 TALLOC_FREE(lck);
5048         }
5049         /*
5050          * Ensure we don't grant anything more so we
5051          * never upgrade.
5052          */
5053         return NT_STATUS_OPLOCK_NOT_GRANTED;
5054 }
5055
5056 /*
5057  * Wrapper around open_file_ntcreate and open_directory
5058  */
5059
5060 static NTSTATUS create_file_unixpath(connection_struct *conn,
5061                                      struct smb_request *req,
5062                                      struct smb_filename *smb_fname,
5063                                      uint32_t access_mask,
5064                                      uint32_t share_access,
5065                                      uint32_t create_disposition,
5066                                      uint32_t create_options,
5067                                      uint32_t file_attributes,
5068                                      uint32_t oplock_request,
5069                                      struct smb2_lease *lease,
5070                                      uint64_t allocation_size,
5071                                      uint32_t private_flags,
5072                                      struct security_descriptor *sd,
5073                                      struct ea_list *ea_list,
5074
5075                                      files_struct **result,
5076                                      int *pinfo)
5077 {
5078         int info = FILE_WAS_OPENED;
5079         files_struct *base_fsp = NULL;
5080         files_struct *fsp = NULL;
5081         NTSTATUS status;
5082
5083         DBG_DEBUG("create_file_unixpath: access_mask = 0x%x "
5084                   "file_attributes = 0x%x, share_access = 0x%x, "
5085                   "create_disposition = 0x%x create_options = 0x%x "
5086                   "oplock_request = 0x%x private_flags = 0x%x "
5087                   "ea_list = %p, sd = %p, "
5088                   "fname = %s\n",
5089                   (unsigned int)access_mask,
5090                   (unsigned int)file_attributes,
5091                   (unsigned int)share_access,
5092                   (unsigned int)create_disposition,
5093                   (unsigned int)create_options,
5094                   (unsigned int)oplock_request,
5095                   (unsigned int)private_flags,
5096                   ea_list, sd, smb_fname_str_dbg(smb_fname));
5097
5098         if (create_options & FILE_OPEN_BY_FILE_ID) {
5099                 status = NT_STATUS_NOT_SUPPORTED;
5100                 goto fail;
5101         }
5102
5103         if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
5104                 status = NT_STATUS_INVALID_PARAMETER;
5105                 goto fail;
5106         }
5107
5108         if (req == NULL) {
5109                 oplock_request |= INTERNAL_OPEN_ONLY;
5110         }
5111
5112         if (lease != NULL) {
5113                 uint16_t epoch = lease->lease_epoch;
5114                 uint16_t version = lease->lease_version;
5115
5116                 if (req == NULL) {
5117                         DBG_WARNING("Got lease on internal open\n");
5118                         status = NT_STATUS_INTERNAL_ERROR;
5119                         goto fail;
5120                 }
5121
5122                 status = lease_match(conn,
5123                                 req,
5124                                 &lease->lease_key,
5125                                 conn->connectpath,
5126                                 smb_fname,
5127                                 &version,
5128                                 &epoch);
5129                 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
5130                         /* Dynamic share file. No leases and update epoch... */
5131                         lease->lease_state = SMB2_LEASE_NONE;
5132                         lease->lease_epoch = epoch;
5133                         lease->lease_version = version;
5134                 } else if (!NT_STATUS_IS_OK(status)) {
5135                         goto fail;
5136                 }
5137         }
5138
5139         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
5140             && (access_mask & DELETE_ACCESS)
5141             && !is_ntfs_stream_smb_fname(smb_fname)) {
5142                 /*
5143                  * We can't open a file with DELETE access if any of the
5144                  * streams is open without FILE_SHARE_DELETE
5145                  */
5146                 status = open_streams_for_delete(conn, smb_fname);
5147
5148                 if (!NT_STATUS_IS_OK(status)) {
5149                         goto fail;
5150                 }
5151         }
5152
5153         if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
5154                         !security_token_has_privilege(get_current_nttok(conn),
5155                                         SEC_PRIV_SECURITY)) {
5156                 DEBUG(10, ("create_file_unixpath: open on %s "
5157                         "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
5158                         smb_fname_str_dbg(smb_fname)));
5159                 status = NT_STATUS_PRIVILEGE_NOT_HELD;
5160                 goto fail;
5161         }
5162
5163         /*
5164          * Files or directories can't be opened DELETE_ON_CLOSE without
5165          * delete access.
5166          * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13358
5167          */
5168         if (create_options & FILE_DELETE_ON_CLOSE) {
5169                 if ((access_mask & DELETE_ACCESS) == 0) {
5170                         status = NT_STATUS_INVALID_PARAMETER;
5171                         goto fail;
5172                 }
5173         }
5174
5175         if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
5176             && is_ntfs_stream_smb_fname(smb_fname)
5177             && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
5178                 uint32_t base_create_disposition;
5179                 struct smb_filename *smb_fname_base = NULL;
5180                 uint32_t base_privflags;
5181
5182                 if (create_options & FILE_DIRECTORY_FILE) {
5183                         status = NT_STATUS_NOT_A_DIRECTORY;
5184                         goto fail;
5185                 }
5186
5187                 switch (create_disposition) {
5188                 case FILE_OPEN:
5189                         base_create_disposition = FILE_OPEN;
5190                         break;
5191                 default:
5192                         base_create_disposition = FILE_OPEN_IF;
5193                         break;
5194                 }
5195
5196                 /* Create an smb_filename with stream_name == NULL. */
5197                 smb_fname_base = synthetic_smb_fname(talloc_tos(),
5198                                                 smb_fname->base_name,
5199                                                 NULL,
5200                                                 NULL,
5201                                                 smb_fname->flags);
5202                 if (smb_fname_base == NULL) {
5203                         status = NT_STATUS_NO_MEMORY;
5204                         goto fail;
5205                 }
5206
5207                 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
5208                         DEBUG(10, ("Unable to stat stream: %s\n",
5209                                    smb_fname_str_dbg(smb_fname_base)));
5210                 } else {
5211                         /*
5212                          * https://bugzilla.samba.org/show_bug.cgi?id=10229
5213                          * We need to check if the requested access mask
5214                          * could be used to open the underlying file (if
5215                          * it existed), as we're passing in zero for the
5216                          * access mask to the base filename.
5217                          */
5218                         status = check_base_file_access(conn,
5219                                                         smb_fname_base,
5220                                                         access_mask);
5221
5222                         if (!NT_STATUS_IS_OK(status)) {
5223                                 DEBUG(10, ("Permission check "
5224                                         "for base %s failed: "
5225                                         "%s\n", smb_fname->base_name,
5226                                         nt_errstr(status)));
5227                                 goto fail;
5228                         }
5229                 }
5230
5231                 base_privflags = NTCREATEX_OPTIONS_PRIVATE_STREAM_BASEOPEN;
5232
5233                 /* Open the base file. */
5234                 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
5235                                               FILE_SHARE_READ
5236                                               | FILE_SHARE_WRITE
5237                                               | FILE_SHARE_DELETE,
5238                                               base_create_disposition,
5239                                               0, 0, 0, NULL, 0,
5240                                               base_privflags,
5241                                               NULL, NULL,
5242                                               &base_fsp, NULL);
5243                 TALLOC_FREE(smb_fname_base);
5244
5245                 if (!NT_STATUS_IS_OK(status)) {
5246                         DEBUG(10, ("create_file_unixpath for base %s failed: "
5247                                    "%s\n", smb_fname->base_name,
5248                                    nt_errstr(status)));
5249                         goto fail;
5250                 }
5251                 /* we don't need the low level fd */
5252                 fd_close(base_fsp);
5253         }
5254
5255         /*
5256          * If it's a request for a directory open, deal with it separately.
5257          */
5258
5259         if (create_options & FILE_DIRECTORY_FILE) {
5260
5261                 if (create_options & FILE_NON_DIRECTORY_FILE) {
5262                         status = NT_STATUS_INVALID_PARAMETER;
5263                         goto fail;
5264                 }
5265
5266                 /* Can't open a temp directory. IFS kit test. */
5267                 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
5268                      (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
5269                         status = NT_STATUS_INVALID_PARAMETER;
5270                         goto fail;
5271                 }
5272
5273                 /*
5274                  * We will get a create directory here if the Win32
5275                  * app specified a security descriptor in the
5276                  * CreateDirectory() call.
5277                  */
5278
5279                 oplock_request = 0;
5280                 status = open_directory(
5281                         conn, req, smb_fname, access_mask, share_access,
5282                         create_disposition, create_options, file_attributes,
5283                         &info, &fsp);
5284         } else {
5285
5286                 /*
5287                  * Ordinary file case.
5288                  */
5289
5290                 status = file_new(req, conn, &fsp);
5291                 if(!NT_STATUS_IS_OK(status)) {
5292                         goto fail;
5293                 }
5294
5295                 status = fsp_set_smb_fname(fsp, smb_fname);
5296                 if (!NT_STATUS_IS_OK(status)) {
5297                         goto fail;
5298                 }
5299
5300                 if (base_fsp) {
5301                         /*
5302                          * We're opening the stream element of a
5303                          * base_fsp we already opened. Set up the
5304                          * base_fsp pointer.
5305                          */
5306                         fsp->base_fsp = base_fsp;
5307                 }
5308
5309                 if (allocation_size) {
5310                         fsp->initial_allocation_size = smb_roundup(fsp->conn,
5311                                                         allocation_size);
5312                 }
5313
5314                 status = open_file_ntcreate(conn,
5315                                             req,
5316                                             access_mask,
5317                                             share_access,
5318                                             create_disposition,
5319                                             create_options,
5320                                             file_attributes,
5321                                             oplock_request,
5322                                             lease,
5323                                             private_flags,
5324                                             &info,
5325                                             fsp);
5326
5327                 if(!NT_STATUS_IS_OK(status)) {
5328                         file_free(req, fsp);
5329                         fsp = NULL;
5330                 }
5331
5332                 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
5333
5334                         /* A stream open never opens a directory */
5335
5336                         if (base_fsp) {
5337                                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
5338                                 goto fail;
5339                         }
5340
5341                         /*
5342                          * Fail the open if it was explicitly a non-directory
5343                          * file.
5344                          */
5345
5346                         if (create_options & FILE_NON_DIRECTORY_FILE) {
5347                                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
5348                                 goto fail;
5349                         }
5350
5351                         oplock_request = 0;
5352                         status = open_directory(
5353                                 conn, req, smb_fname, access_mask,
5354                                 share_access, create_disposition,
5355                                 create_options, file_attributes,
5356                                 &info, &fsp);
5357                 }
5358         }
5359
5360         if (!NT_STATUS_IS_OK(status)) {
5361                 goto fail;
5362         }
5363
5364         fsp->base_fsp = base_fsp;
5365
5366         if ((ea_list != NULL) &&
5367             ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
5368                 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
5369                 if (!NT_STATUS_IS_OK(status)) {
5370                         goto fail;
5371                 }
5372         }
5373
5374         if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
5375                 status = NT_STATUS_ACCESS_DENIED;
5376                 goto fail;
5377         }
5378
5379         /* Save the requested allocation size. */
5380         if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
5381                 if ((allocation_size > fsp->fsp_name->st.st_ex_size)
5382                     && !(fsp->is_directory))
5383                 {
5384                         fsp->initial_allocation_size = smb_roundup(
5385                                 fsp->conn, allocation_size);
5386                         if (vfs_allocate_file_space(
5387                                     fsp, fsp->initial_allocation_size) == -1) {
5388                                 status = NT_STATUS_DISK_FULL;
5389                                 goto fail;
5390                         }
5391                 } else {
5392                         fsp->initial_allocation_size = smb_roundup(
5393                                 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
5394                 }
5395         } else {
5396                 fsp->initial_allocation_size = 0;
5397         }
5398
5399         if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
5400                                 fsp->base_fsp == NULL) {
5401                 if (sd != NULL) {
5402                         /*
5403                          * According to the MS documentation, the only time the security
5404                          * descriptor is applied to the opened file is iff we *created* the
5405                          * file; an existing file stays the same.
5406                          *
5407                          * Also, it seems (from observation) that you can open the file with
5408                          * any access mask but you can still write the sd. We need to override
5409                          * the granted access before we call set_sd
5410                          * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
5411                          */
5412
5413                         uint32_t sec_info_sent;
5414                         uint32_t saved_access_mask = fsp->access_mask;
5415
5416                         sec_info_sent = get_sec_info(sd);
5417
5418                         fsp->access_mask = FILE_GENERIC_ALL;
5419
5420                         if (sec_info_sent & (SECINFO_OWNER|
5421                                                 SECINFO_GROUP|
5422                                                 SECINFO_DACL|
5423                                                 SECINFO_SACL)) {
5424                                 status = set_sd(fsp, sd, sec_info_sent);
5425                         }
5426
5427                         fsp->access_mask = saved_access_mask;
5428
5429                         if (!NT_STATUS_IS_OK(status)) {
5430                                 goto fail;
5431                         }
5432                 } else if (lp_inherit_acls(SNUM(conn))) {
5433                         /* Inherit from parent. Errors here are not fatal. */
5434                         status = inherit_new_acl(fsp);
5435                         if (!NT_STATUS_IS_OK(status)) {
5436                                 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
5437                                         fsp_str_dbg(fsp),
5438                                         nt_errstr(status) ));
5439                         }
5440                 }
5441         }
5442
5443         if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
5444          && (create_options & FILE_NO_COMPRESSION)
5445          && (info == FILE_WAS_CREATED)) {
5446                 status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
5447                                                  COMPRESSION_FORMAT_NONE);
5448                 if (!NT_STATUS_IS_OK(status)) {
5449                         DEBUG(1, ("failed to disable compression: %s\n",
5450                                   nt_errstr(status)));
5451                 }
5452         }
5453
5454         DEBUG(10, ("create_file_unixpath: info=%d\n", info));
5455
5456         *result = fsp;
5457         if (pinfo != NULL) {
5458                 *pinfo = info;
5459         }
5460
5461         smb_fname->st = fsp->fsp_name->st;
5462
5463         return NT_STATUS_OK;
5464
5465  fail:
5466         DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
5467
5468         if (fsp != NULL) {
5469                 if (base_fsp && fsp->base_fsp == base_fsp) {
5470                         /*
5471                          * The close_file below will close
5472                          * fsp->base_fsp.
5473                          */
5474                         base_fsp = NULL;
5475                 }
5476                 close_file(req, fsp, ERROR_CLOSE);
5477                 fsp = NULL;
5478         }
5479         if (base_fsp != NULL) {
5480                 close_file(req, base_fsp, ERROR_CLOSE);
5481                 base_fsp = NULL;
5482         }
5483         return status;
5484 }
5485
5486 /*
5487  * Calculate the full path name given a relative fid.
5488  */
5489 NTSTATUS get_relative_fid_filename(connection_struct *conn,
5490                                    struct smb_request *req,
5491                                    uint16_t root_dir_fid,
5492                                    const struct smb_filename *smb_fname,
5493                                    struct smb_filename **smb_fname_out)
5494 {
5495         files_struct *dir_fsp;
5496         char *parent_fname = NULL;
5497         char *new_base_name = NULL;
5498         uint32_t ucf_flags = ucf_flags_from_smb_request(req);
5499         NTSTATUS status;
5500
5501         if (root_dir_fid == 0 || !smb_fname) {
5502                 status = NT_STATUS_INTERNAL_ERROR;
5503                 goto out;
5504         }
5505
5506         dir_fsp = file_fsp(req, root_dir_fid);
5507
5508         if (dir_fsp == NULL) {
5509                 status = NT_STATUS_INVALID_HANDLE;
5510                 goto out;
5511         }
5512
5513         if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
5514                 status = NT_STATUS_INVALID_HANDLE;
5515                 goto out;
5516         }
5517
5518         if (!dir_fsp->is_directory) {
5519
5520                 /*
5521                  * Check to see if this is a mac fork of some kind.
5522                  */
5523
5524                 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
5525                     is_ntfs_stream_smb_fname(smb_fname)) {
5526                         status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
5527                         goto out;
5528                 }
5529
5530                 /*
5531                   we need to handle the case when we get a
5532                   relative open relative to a file and the
5533                   pathname is blank - this is a reopen!
5534                   (hint from demyn plantenberg)
5535                 */
5536
5537                 status = NT_STATUS_INVALID_HANDLE;
5538                 goto out;
5539         }
5540
5541         if (ISDOT(dir_fsp->fsp_name->base_name)) {
5542                 /*
5543                  * We're at the toplevel dir, the final file name
5544                  * must not contain ./, as this is filtered out
5545                  * normally by srvstr_get_path and unix_convert
5546                  * explicitly rejects paths containing ./.
5547                  */
5548                 parent_fname = talloc_strdup(talloc_tos(), "");
5549                 if (parent_fname == NULL) {
5550                         status = NT_STATUS_NO_MEMORY;
5551                         goto out;
5552                 }
5553         } else {
5554                 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
5555
5556                 /*
5557                  * Copy in the base directory name.
5558                  */
5559
5560                 parent_fname = talloc_array(talloc_tos(), char,
5561                     dir_name_len+2);
5562                 if (parent_fname == NULL) {
5563                         status = NT_STATUS_NO_MEMORY;
5564                         goto out;
5565                 }
5566                 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
5567                     dir_name_len+1);
5568
5569                 /*
5570                  * Ensure it ends in a '/'.
5571                  * We used TALLOC_SIZE +2 to add space for the '/'.
5572                  */
5573
5574                 if(dir_name_len
5575                     && (parent_fname[dir_name_len-1] != '\\')
5576                     && (parent_fname[dir_name_len-1] != '/')) {
5577                         parent_fname[dir_name_len] = '/';
5578                         parent_fname[dir_name_len+1] = '\0';
5579                 }
5580         }
5581
5582         new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
5583                                         smb_fname->base_name);
5584         if (new_base_name == NULL) {
5585                 status = NT_STATUS_NO_MEMORY;
5586                 goto out;
5587         }
5588
5589         status = filename_convert(req,
5590                                 conn,
5591                                 new_base_name,
5592                                 ucf_flags,
5593                                 NULL,
5594                                 NULL,
5595                                 smb_fname_out);
5596         if (!NT_STATUS_IS_OK(status)) {
5597                 goto out;
5598         }
5599
5600  out:
5601         TALLOC_FREE(parent_fname);
5602         TALLOC_FREE(new_base_name);
5603         return status;
5604 }
5605
5606 NTSTATUS create_file_default(connection_struct *conn,
5607                              struct smb_request *req,
5608                              uint16_t root_dir_fid,
5609                              struct smb_filename *smb_fname,
5610                              uint32_t access_mask,
5611                              uint32_t share_access,
5612                              uint32_t create_disposition,
5613                              uint32_t create_options,
5614                              uint32_t file_attributes,
5615                              uint32_t oplock_request,
5616                              struct smb2_lease *lease,
5617                              uint64_t allocation_size,
5618                              uint32_t private_flags,
5619                              struct security_descriptor *sd,
5620                              struct ea_list *ea_list,
5621                              files_struct **result,
5622                              int *pinfo,
5623                              const struct smb2_create_blobs *in_context_blobs,
5624                              struct smb2_create_blobs *out_context_blobs)
5625 {
5626         int info = FILE_WAS_OPENED;
5627         files_struct *fsp = NULL;
5628         NTSTATUS status;
5629         bool stream_name = false;
5630
5631         DBG_DEBUG("create_file: access_mask = 0x%x "
5632                   "file_attributes = 0x%x, share_access = 0x%x, "
5633                   "create_disposition = 0x%x create_options = 0x%x "
5634                   "oplock_request = 0x%x "
5635                   "private_flags = 0x%x "
5636                   "root_dir_fid = 0x%x, ea_list = %p, sd = %p, "
5637                   "fname = %s\n",
5638                   (unsigned int)access_mask,
5639                   (unsigned int)file_attributes,
5640                   (unsigned int)share_access,
5641                   (unsigned int)create_disposition,
5642                   (unsigned int)create_options,
5643                   (unsigned int)oplock_request,
5644                   (unsigned int)private_flags,
5645                   (unsigned int)root_dir_fid,
5646                   ea_list, sd, smb_fname_str_dbg(smb_fname));
5647
5648         /*
5649          * Calculate the filename from the root_dir_if if necessary.
5650          */
5651
5652         if (root_dir_fid != 0) {
5653                 struct smb_filename *smb_fname_out = NULL;
5654                 status = get_relative_fid_filename(conn, req, root_dir_fid,
5655                                                    smb_fname, &smb_fname_out);
5656                 if (!NT_STATUS_IS_OK(status)) {
5657                         goto fail;
5658                 }
5659                 smb_fname = smb_fname_out;
5660         }
5661
5662         /*
5663          * Check to see if this is a mac fork of some kind.
5664          */
5665
5666         stream_name = is_ntfs_stream_smb_fname(smb_fname);
5667         if (stream_name) {
5668                 enum FAKE_FILE_TYPE fake_file_type;
5669
5670                 fake_file_type = is_fake_file(smb_fname);
5671
5672                 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
5673
5674                         /*
5675                          * Here we go! support for changing the disk quotas
5676                          * --metze
5677                          *
5678                          * We need to fake up to open this MAGIC QUOTA file
5679                          * and return a valid FID.
5680                          *
5681                          * w2k close this file directly after openening xp
5682                          * also tries a QUERY_FILE_INFO on the file and then
5683                          * close it
5684                          */
5685                         status = open_fake_file(req, conn, req->vuid,
5686                                                 fake_file_type, smb_fname,
5687                                                 access_mask, &fsp);
5688                         if (!NT_STATUS_IS_OK(status)) {
5689                                 goto fail;
5690                         }
5691
5692                         ZERO_STRUCT(smb_fname->st);
5693                         goto done;
5694                 }
5695
5696                 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
5697                         status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5698                         goto fail;
5699                 }
5700         }
5701
5702         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
5703                 int ret;
5704                 smb_fname->stream_name = NULL;
5705                 /* We have to handle this error here. */
5706                 if (create_options & FILE_DIRECTORY_FILE) {
5707                         status = NT_STATUS_NOT_A_DIRECTORY;
5708                         goto fail;
5709                 }
5710                 if (req != NULL && req->posix_pathnames) {
5711                         ret = SMB_VFS_LSTAT(conn, smb_fname);
5712                 } else {
5713                         ret = SMB_VFS_STAT(conn, smb_fname);
5714                 }
5715
5716                 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
5717                         status = NT_STATUS_FILE_IS_A_DIRECTORY;
5718                         goto fail;
5719                 }
5720         }
5721
5722         status = create_file_unixpath(
5723                 conn, req, smb_fname, access_mask, share_access,
5724                 create_disposition, create_options, file_attributes,
5725                 oplock_request, lease, allocation_size, private_flags,
5726                 sd, ea_list,
5727                 &fsp, &info);
5728
5729         if (!NT_STATUS_IS_OK(status)) {
5730                 goto fail;
5731         }
5732
5733  done:
5734         DEBUG(10, ("create_file: info=%d\n", info));
5735
5736         *result = fsp;
5737         if (pinfo != NULL) {
5738                 *pinfo = info;
5739         }
5740         return NT_STATUS_OK;
5741
5742  fail:
5743         DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
5744
5745         if (fsp != NULL) {
5746                 close_file(req, fsp, ERROR_CLOSE);
5747                 fsp = NULL;
5748         }
5749         return status;
5750 }