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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
24 #include "lib/util/server_id.h"
26 #include "smbd/smbd.h"
27 #include "smbd/globals.h"
28 #include "fake_file.h"
29 #include "../libcli/security/security.h"
30 #include "../librpc/gen_ndr/ndr_security.h"
31 #include "../librpc/gen_ndr/open_files.h"
32 #include "../librpc/gen_ndr/idmap.h"
33 #include "../librpc/gen_ndr/ioctl.h"
34 #include "passdb/lookup_sid.h"
38 #include "source3/lib/dbwrap/dbwrap_watch.h"
39 #include "locking/leases_db.h"
40 #include "librpc/gen_ndr/ndr_leases_db.h"
42 extern const struct generic_mapping file_generic_mapping;
44 struct deferred_open_record {
45 bool delayed_for_oplocks;
50 /****************************************************************************
51 If the requester wanted DELETE_ACCESS and was rejected because
52 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
54 ****************************************************************************/
56 static bool parent_override_delete(connection_struct *conn,
57 const struct smb_filename *smb_fname,
59 uint32_t rejected_mask)
61 if ((access_mask & DELETE_ACCESS) &&
62 (rejected_mask & DELETE_ACCESS) &&
63 can_delete_file_in_directory(conn, smb_fname)) {
69 /****************************************************************************
70 Check if we have open rights.
71 ****************************************************************************/
73 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
74 const struct smb_filename *smb_fname,
78 /* Check if we have rights to open. */
80 struct security_descriptor *sd = NULL;
81 uint32_t rejected_share_access;
82 uint32_t rejected_mask = access_mask;
83 uint32_t do_not_check_mask = 0;
85 rejected_share_access = access_mask & ~(conn->share_access);
87 if (rejected_share_access) {
88 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
90 (unsigned int)access_mask,
91 smb_fname_str_dbg(smb_fname),
92 (unsigned int)rejected_share_access ));
93 return NT_STATUS_ACCESS_DENIED;
96 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
97 /* I'm sorry sir, I didn't know you were root... */
98 DEBUG(10,("smbd_check_access_rights: root override "
99 "on %s. Granting 0x%x\n",
100 smb_fname_str_dbg(smb_fname),
101 (unsigned int)access_mask ));
105 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
106 DEBUG(10,("smbd_check_access_rights: not checking ACL "
107 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
108 smb_fname_str_dbg(smb_fname),
109 (unsigned int)access_mask ));
113 if (access_mask == DELETE_ACCESS &&
114 VALID_STAT(smb_fname->st) &&
115 S_ISLNK(smb_fname->st.st_ex_mode)) {
116 /* We can always delete a symlink. */
117 DEBUG(10,("smbd_check_access_rights: not checking ACL "
118 "on DELETE_ACCESS on symlink %s.\n",
119 smb_fname_str_dbg(smb_fname) ));
123 status = SMB_VFS_GET_NT_ACL(conn, smb_fname,
126 SECINFO_DACL), talloc_tos(), &sd);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
131 smb_fname_str_dbg(smb_fname),
134 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
142 * If we can access the path to this file, by
143 * default we have FILE_READ_ATTRIBUTES from the
144 * containing directory. See the section:
145 * "Algorithm to Check Access to an Existing File"
148 * se_file_access_check() also takes care of
149 * owner WRITE_DAC and READ_CONTROL.
151 do_not_check_mask = FILE_READ_ATTRIBUTES;
154 * Samba 3.6 and earlier granted execute access even
155 * if the ACL did not contain execute rights.
156 * Samba 4.0 is more correct and checks it.
157 * The compatibilty mode allows one to skip this check
158 * to smoothen upgrades.
160 if (lp_acl_allow_execute_always(SNUM(conn))) {
161 do_not_check_mask |= FILE_EXECUTE;
164 status = se_file_access_check(sd,
165 get_current_nttok(conn),
167 (access_mask & ~do_not_check_mask),
170 DEBUG(10,("smbd_check_access_rights: file %s requesting "
171 "0x%x returning 0x%x (%s)\n",
172 smb_fname_str_dbg(smb_fname),
173 (unsigned int)access_mask,
174 (unsigned int)rejected_mask,
175 nt_errstr(status) ));
177 if (!NT_STATUS_IS_OK(status)) {
178 if (DEBUGLEVEL >= 10) {
179 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
180 smb_fname_str_dbg(smb_fname) ));
181 NDR_PRINT_DEBUG(security_descriptor, sd);
187 if (NT_STATUS_IS_OK(status) ||
188 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
192 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
196 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
197 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
198 !lp_store_dos_attributes(SNUM(conn)) &&
199 (lp_map_readonly(SNUM(conn)) ||
200 lp_map_archive(SNUM(conn)) ||
201 lp_map_hidden(SNUM(conn)) ||
202 lp_map_system(SNUM(conn)))) {
203 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
205 DEBUG(10,("smbd_check_access_rights: "
207 "FILE_WRITE_ATTRIBUTES "
209 smb_fname_str_dbg(smb_fname)));
212 if (parent_override_delete(conn,
216 /* Were we trying to do an open
217 * for delete and didn't get DELETE
218 * access (only) ? Check if the
219 * directory allows DELETE_CHILD.
221 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
224 rejected_mask &= ~DELETE_ACCESS;
226 DEBUG(10,("smbd_check_access_rights: "
230 smb_fname_str_dbg(smb_fname)));
233 if (rejected_mask != 0) {
234 return NT_STATUS_ACCESS_DENIED;
239 NTSTATUS check_parent_access(struct connection_struct *conn,
240 struct smb_filename *smb_fname,
241 uint32_t access_mask)
244 char *parent_dir = NULL;
245 struct security_descriptor *parent_sd = NULL;
246 uint32_t access_granted = 0;
247 struct smb_filename *parent_smb_fname = NULL;
249 if (!parent_dirname(talloc_tos(),
250 smb_fname->base_name,
253 return NT_STATUS_NO_MEMORY;
256 parent_smb_fname = synthetic_smb_fname(talloc_tos(),
261 if (parent_smb_fname == NULL) {
262 return NT_STATUS_NO_MEMORY;
265 if (get_current_uid(conn) == (uid_t)0) {
266 /* I'm sorry sir, I didn't know you were root... */
267 DEBUG(10,("check_parent_access: root override "
268 "on %s. Granting 0x%x\n",
269 smb_fname_str_dbg(smb_fname),
270 (unsigned int)access_mask ));
274 status = SMB_VFS_GET_NT_ACL(conn,
280 if (!NT_STATUS_IS_OK(status)) {
281 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
282 "%s with error %s\n",
289 * If we can access the path to this file, by
290 * default we have FILE_READ_ATTRIBUTES from the
291 * containing directory. See the section:
292 * "Algorithm to Check Access to an Existing File"
295 * se_file_access_check() also takes care of
296 * owner WRITE_DAC and READ_CONTROL.
298 status = se_file_access_check(parent_sd,
299 get_current_nttok(conn),
301 (access_mask & ~FILE_READ_ATTRIBUTES),
303 if(!NT_STATUS_IS_OK(status)) {
304 DEBUG(5,("check_parent_access: access check "
305 "on directory %s for "
306 "path %s for mask 0x%x returned (0x%x) %s\n",
308 smb_fname->base_name,
311 nt_errstr(status) ));
318 /****************************************************************************
319 Ensure when opening a base file for a stream open that we have permissions
320 to do so given the access mask on the base file.
321 ****************************************************************************/
323 static NTSTATUS check_base_file_access(struct connection_struct *conn,
324 struct smb_filename *smb_fname,
325 uint32_t access_mask)
329 status = smbd_calculate_access_mask(conn, smb_fname,
333 if (!NT_STATUS_IS_OK(status)) {
334 DEBUG(10, ("smbd_calculate_access_mask "
335 "on file %s returned %s\n",
336 smb_fname_str_dbg(smb_fname),
341 if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
343 if (!CAN_WRITE(conn)) {
344 return NT_STATUS_ACCESS_DENIED;
346 dosattrs = dos_mode(conn, smb_fname);
347 if (IS_DOS_READONLY(dosattrs)) {
348 return NT_STATUS_ACCESS_DENIED;
352 return smbd_check_access_rights(conn,
358 /****************************************************************************
359 fd support routines - attempt to do a dos_open.
360 ****************************************************************************/
362 NTSTATUS fd_open(struct connection_struct *conn,
367 struct smb_filename *smb_fname = fsp->fsp_name;
368 NTSTATUS status = NT_STATUS_OK;
372 * Never follow symlinks on a POSIX client. The
373 * client should be doing this.
376 if ((fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) || !lp_follow_symlinks(SNUM(conn))) {
381 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
382 if (fsp->fh->fd == -1) {
383 int posix_errno = errno;
385 #if defined(ENOTSUP) && defined(OSF1)
386 /* handle special Tru64 errno */
387 if (errno == ENOTSUP) {
392 /* fix broken NetBSD errno */
393 if (errno == EFTYPE) {
397 /* fix broken FreeBSD errno */
398 if (errno == EMLINK) {
401 #endif /* O_NOFOLLOW */
402 status = map_nt_error_from_unix(posix_errno);
403 if (errno == EMFILE) {
404 static time_t last_warned = 0L;
406 if (time((time_t *) NULL) > last_warned) {
407 DEBUG(0,("Too many open files, unable "
408 "to open more! smbd's max "
410 lp_max_open_files()));
411 last_warned = time((time_t *) NULL);
417 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
418 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
419 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
424 /****************************************************************************
425 Close the file associated with a fsp.
426 ****************************************************************************/
428 NTSTATUS fd_close(files_struct *fsp)
435 if (fsp->fh->fd == -1) {
436 return NT_STATUS_OK; /* What we used to call a stat open. */
438 if (fsp->fh->ref_count > 1) {
439 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
442 ret = SMB_VFS_CLOSE(fsp);
445 return map_nt_error_from_unix(errno);
450 /****************************************************************************
451 Change the ownership of a file to that of the parent directory.
452 Do this by fd if possible.
453 ****************************************************************************/
455 void change_file_owner_to_parent(connection_struct *conn,
456 const char *inherit_from_dir,
459 struct smb_filename *smb_fname_parent;
462 smb_fname_parent = synthetic_smb_fname(talloc_tos(),
467 if (smb_fname_parent == NULL) {
471 ret = SMB_VFS_STAT(conn, smb_fname_parent);
473 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
474 "directory %s. Error was %s\n",
475 smb_fname_str_dbg(smb_fname_parent),
477 TALLOC_FREE(smb_fname_parent);
481 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
482 /* Already this uid - no need to change. */
483 DEBUG(10,("change_file_owner_to_parent: file %s "
484 "is already owned by uid %d\n",
486 (int)fsp->fsp_name->st.st_ex_uid ));
487 TALLOC_FREE(smb_fname_parent);
492 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
495 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
496 "file %s to parent directory uid %u. Error "
497 "was %s\n", fsp_str_dbg(fsp),
498 (unsigned int)smb_fname_parent->st.st_ex_uid,
501 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
502 "parent directory uid %u.\n", fsp_str_dbg(fsp),
503 (unsigned int)smb_fname_parent->st.st_ex_uid));
504 /* Ensure the uid entry is updated. */
505 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
508 TALLOC_FREE(smb_fname_parent);
511 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
512 const char *inherit_from_dir,
514 SMB_STRUCT_STAT *psbuf)
516 struct smb_filename *smb_fname_parent;
517 struct smb_filename *smb_fname_cwd = NULL;
518 char *saved_dir = NULL;
519 TALLOC_CTX *ctx = talloc_tos();
520 NTSTATUS status = NT_STATUS_OK;
523 smb_fname_parent = synthetic_smb_fname(ctx,
528 if (smb_fname_parent == NULL) {
529 return NT_STATUS_NO_MEMORY;
532 ret = SMB_VFS_STAT(conn, smb_fname_parent);
534 status = map_nt_error_from_unix(errno);
535 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
536 "directory %s. Error was %s\n",
537 smb_fname_str_dbg(smb_fname_parent),
542 /* We've already done an lstat into psbuf, and we know it's a
543 directory. If we can cd into the directory and the dev/ino
544 are the same then we can safely chown without races as
545 we're locking the directory in place by being in it. This
546 should work on any UNIX (thanks tridge :-). JRA.
549 saved_dir = vfs_GetWd(ctx,conn);
551 status = map_nt_error_from_unix(errno);
552 DEBUG(0,("change_dir_owner_to_parent: failed to get "
553 "current working directory. Error was %s\n",
558 /* Chdir into the new path. */
559 if (vfs_ChDir(conn, fname) == -1) {
560 status = map_nt_error_from_unix(errno);
561 DEBUG(0,("change_dir_owner_to_parent: failed to change "
562 "current working directory to %s. Error "
563 "was %s\n", fname, strerror(errno) ));
567 smb_fname_cwd = synthetic_smb_fname(ctx, ".", NULL, NULL, 0);
568 if (smb_fname_cwd == NULL) {
569 status = NT_STATUS_NO_MEMORY;
573 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
575 status = map_nt_error_from_unix(errno);
576 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
577 "directory '.' (%s) Error was %s\n",
578 fname, strerror(errno)));
582 /* Ensure we're pointing at the same place. */
583 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
584 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
585 DEBUG(0,("change_dir_owner_to_parent: "
586 "device/inode on directory %s changed. "
587 "Refusing to chown !\n", fname ));
588 status = NT_STATUS_ACCESS_DENIED;
592 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
593 /* Already this uid - no need to change. */
594 DEBUG(10,("change_dir_owner_to_parent: directory %s "
595 "is already owned by uid %d\n",
597 (int)smb_fname_cwd->st.st_ex_uid ));
598 status = NT_STATUS_OK;
603 ret = SMB_VFS_LCHOWN(conn,
605 smb_fname_parent->st.st_ex_uid,
609 status = map_nt_error_from_unix(errno);
610 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
611 "directory %s to parent directory uid %u. "
612 "Error was %s\n", fname,
613 (unsigned int)smb_fname_parent->st.st_ex_uid,
616 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
617 "directory %s to parent directory uid %u.\n",
618 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
619 /* Ensure the uid entry is updated. */
620 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
624 vfs_ChDir(conn,saved_dir);
626 TALLOC_FREE(smb_fname_parent);
627 TALLOC_FREE(smb_fname_cwd);
631 /****************************************************************************
632 Open a file - returning a guaranteed ATOMIC indication of if the
633 file was created or not.
634 ****************************************************************************/
636 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
642 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
643 NTSTATUS retry_status;
644 bool file_existed = VALID_STAT(fsp->fsp_name->st);
647 *file_created = false;
649 if (!(flags & O_CREAT)) {
651 * We're not creating the file, just pass through.
653 return fd_open(conn, fsp, flags, mode);
656 if (flags & O_EXCL) {
658 * Fail if already exists, just pass through.
660 status = fd_open(conn, fsp, flags, mode);
663 * Here we've opened with O_CREAT|O_EXCL. If that went
664 * NT_STATUS_OK, we *know* we created this file.
666 *file_created = NT_STATUS_IS_OK(status);
672 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
673 * To know absolutely if we created the file or not,
674 * we can never call O_CREAT without O_EXCL. So if
675 * we think the file existed, try without O_CREAT|O_EXCL.
676 * If we think the file didn't exist, try with
679 * The big problem here is dangling symlinks. Opening
680 * without O_NOFOLLOW means both bad symlink
681 * and missing path return -1, ENOENT from open(). As POSIX
682 * is pathname based it's not possible to tell
683 * the difference between these two cases in a
684 * non-racy way, so change to try only two attempts before
687 * We don't have this problem for the O_NOFOLLOW
688 * case as it just returns NT_STATUS_OBJECT_PATH_NOT_FOUND
689 * mapped from the ELOOP POSIX error.
695 curr_flags &= ~(O_CREAT);
696 retry_status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
698 curr_flags |= O_EXCL;
699 retry_status = NT_STATUS_OBJECT_NAME_COLLISION;
702 status = fd_open(conn, fsp, curr_flags, mode);
703 if (NT_STATUS_IS_OK(status)) {
705 *file_created = true;
709 if (!NT_STATUS_EQUAL(status, retry_status)) {
716 * Keep file_existed up to date for clarity.
718 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
719 file_existed = false;
720 curr_flags |= O_EXCL;
721 DBG_DEBUG("file %s did not exist. Retry.\n",
722 smb_fname_str_dbg(fsp->fsp_name));
725 curr_flags &= ~(O_CREAT);
726 DBG_DEBUG("file %s existed. Retry.\n",
727 smb_fname_str_dbg(fsp->fsp_name));
730 status = fd_open(conn, fsp, curr_flags, mode);
732 if (NT_STATUS_IS_OK(status) && (!file_existed)) {
733 *file_created = true;
739 /****************************************************************************
741 ****************************************************************************/
743 static NTSTATUS open_file(files_struct *fsp,
744 connection_struct *conn,
745 struct smb_request *req,
746 const char *parent_dir,
749 uint32_t access_mask, /* client requested access mask. */
750 uint32_t open_access_mask, /* what we're actually using in the open. */
751 bool *p_file_created)
753 struct smb_filename *smb_fname = fsp->fsp_name;
754 NTSTATUS status = NT_STATUS_OK;
755 int accmode = (flags & O_ACCMODE);
756 int local_flags = flags;
757 bool file_existed = VALID_STAT(fsp->fsp_name->st);
762 /* Check permissions */
765 * This code was changed after seeing a client open request
766 * containing the open mode of (DENY_WRITE/read-only) with
767 * the 'create if not exist' bit set. The previous code
768 * would fail to open the file read only on a read-only share
769 * as it was checking the flags parameter directly against O_RDONLY,
770 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
774 if (!CAN_WRITE(conn)) {
775 /* It's a read-only share - fail if we wanted to write. */
776 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
777 DEBUG(3,("Permission denied opening %s\n",
778 smb_fname_str_dbg(smb_fname)));
779 return NT_STATUS_ACCESS_DENIED;
781 if (flags & O_CREAT) {
782 /* We don't want to write - but we must make sure that
783 O_CREAT doesn't create the file if we have write
784 access into the directory.
786 flags &= ~(O_CREAT|O_EXCL);
787 local_flags &= ~(O_CREAT|O_EXCL);
792 * This little piece of insanity is inspired by the
793 * fact that an NT client can open a file for O_RDONLY,
794 * but set the create disposition to FILE_EXISTS_TRUNCATE.
795 * If the client *can* write to the file, then it expects to
796 * truncate the file, even though it is opening for readonly.
797 * Quicken uses this stupid trick in backup file creation...
798 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
799 * for helping track this one down. It didn't bite us in 2.0.x
800 * as we always opened files read-write in that release. JRA.
803 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
804 DEBUG(10,("open_file: truncate requested on read-only open "
805 "for file %s\n", smb_fname_str_dbg(smb_fname)));
806 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
809 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
810 (!file_existed && (local_flags & O_CREAT)) ||
811 ((local_flags & O_TRUNC) == O_TRUNC) ) {
815 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
817 * We would block on opening a FIFO with no one else on the
818 * other end. Do what we used to do and add O_NONBLOCK to the
822 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
823 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
824 local_flags |= O_NONBLOCK;
828 /* Don't create files with Microsoft wildcard characters. */
831 * wildcard characters are allowed in stream names
832 * only test the basefilename
834 wild = fsp->base_fsp->fsp_name->base_name;
836 wild = smb_fname->base_name;
838 if ((local_flags & O_CREAT) && !file_existed &&
839 !(fsp->posix_flags & FSP_POSIX_FLAGS_PATHNAMES) &&
841 return NT_STATUS_OBJECT_NAME_INVALID;
844 /* Can we access this file ? */
845 if (!fsp->base_fsp) {
846 /* Only do this check on non-stream open. */
848 status = smbd_check_access_rights(conn,
853 if (!NT_STATUS_IS_OK(status)) {
854 DEBUG(10, ("open_file: "
855 "smbd_check_access_rights "
856 "on file %s returned %s\n",
857 smb_fname_str_dbg(smb_fname),
861 if (!NT_STATUS_IS_OK(status) &&
862 !NT_STATUS_EQUAL(status,
863 NT_STATUS_OBJECT_NAME_NOT_FOUND))
868 if (NT_STATUS_EQUAL(status,
869 NT_STATUS_OBJECT_NAME_NOT_FOUND))
871 DEBUG(10, ("open_file: "
872 "file %s vanished since we "
873 "checked for existence.\n",
874 smb_fname_str_dbg(smb_fname)));
875 file_existed = false;
876 SET_STAT_INVALID(fsp->fsp_name->st);
881 if (!(local_flags & O_CREAT)) {
882 /* File didn't exist and no O_CREAT. */
883 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
886 status = check_parent_access(conn,
889 if (!NT_STATUS_IS_OK(status)) {
890 DEBUG(10, ("open_file: "
891 "check_parent_access on "
892 "file %s returned %s\n",
893 smb_fname_str_dbg(smb_fname),
894 nt_errstr(status) ));
901 * Actually do the open - if O_TRUNC is needed handle it
902 * below under the share mode lock.
904 status = fd_open_atomic(conn, fsp, local_flags & ~O_TRUNC,
905 unx_mode, p_file_created);
906 if (!NT_STATUS_IS_OK(status)) {
907 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
908 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
909 nt_errstr(status),local_flags,flags));
913 if (local_flags & O_NONBLOCK) {
915 * GPFS can return ETIMEDOUT for pread on
916 * nonblocking file descriptors when files
917 * migrated to tape need to be recalled. I
918 * could imagine this happens elsehwere
919 * too. With blocking file descriptors this
922 ret = set_blocking(fsp->fh->fd, true);
924 status = map_nt_error_from_unix(errno);
925 DBG_WARNING("Could not set fd to blocking: "
926 "%s\n", strerror(errno));
932 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
934 /* If we have an fd, this stat should succeed. */
935 DEBUG(0,("Error doing fstat on open file %s "
937 smb_fname_str_dbg(smb_fname),
939 status = map_nt_error_from_unix(errno);
944 if (*p_file_created) {
945 /* We created this file. */
947 bool need_re_stat = false;
948 /* Do all inheritance work after we've
949 done a successful fstat call and filled
950 in the stat struct in fsp->fsp_name. */
952 /* Inherit the ACL if required */
953 if (lp_inherit_permissions(SNUM(conn))) {
954 inherit_access_posix_acl(conn, parent_dir,
955 smb_fname->base_name,
960 /* Change the owner if required. */
961 if (lp_inherit_owner(SNUM(conn)) != INHERIT_OWNER_NO) {
962 change_file_owner_to_parent(conn, parent_dir,
968 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
969 /* If we have an fd, this stat should succeed. */
971 DEBUG(0,("Error doing fstat on open file %s "
973 smb_fname_str_dbg(smb_fname),
978 notify_fname(conn, NOTIFY_ACTION_ADDED,
979 FILE_NOTIFY_CHANGE_FILE_NAME,
980 smb_fname->base_name);
983 fsp->fh->fd = -1; /* What we used to call a stat open. */
985 /* File must exist for a stat open. */
986 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
989 status = smbd_check_access_rights(conn,
994 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
995 (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) &&
996 S_ISLNK(smb_fname->st.st_ex_mode)) {
997 /* This is a POSIX stat open for delete
998 * or rename on a symlink that points
1000 DEBUG(10,("open_file: allowing POSIX "
1001 "open on bad symlink %s\n",
1002 smb_fname_str_dbg(smb_fname)));
1003 status = NT_STATUS_OK;
1006 if (!NT_STATUS_IS_OK(status)) {
1007 DEBUG(10,("open_file: "
1008 "smbd_check_access_rights on file "
1010 smb_fname_str_dbg(smb_fname),
1011 nt_errstr(status) ));
1017 * POSIX allows read-only opens of directories. We don't
1018 * want to do this (we use a different code path for this)
1019 * so catch a directory open and return an EISDIR. JRA.
1022 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
1025 return NT_STATUS_FILE_IS_A_DIRECTORY;
1028 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1029 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
1030 fsp->file_pid = req ? req->smbpid : 0;
1031 fsp->can_lock = True;
1032 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
1035 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1036 fsp->print_file = NULL;
1037 fsp->modified = False;
1038 fsp->sent_oplock_break = NO_BREAK_SENT;
1039 fsp->is_directory = False;
1040 if (conn->aio_write_behind_list &&
1041 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
1042 conn->case_sensitive)) {
1043 fsp->aio_write_behind = True;
1046 fsp->wcp = NULL; /* Write cache pointer. */
1048 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1049 conn->session_info->unix_info->unix_name,
1050 smb_fname_str_dbg(smb_fname),
1051 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
1052 conn->num_files_open));
1055 return NT_STATUS_OK;
1058 /****************************************************************************
1059 Check if we can open a file with a share mode.
1060 Returns True if conflict, False if not.
1061 ****************************************************************************/
1063 static bool share_conflict(struct share_mode_entry *entry,
1064 uint32_t access_mask,
1065 uint32_t share_access)
1067 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1068 "entry->share_access = 0x%x, "
1069 "entry->private_options = 0x%x\n",
1070 (unsigned int)entry->access_mask,
1071 (unsigned int)entry->share_access,
1072 (unsigned int)entry->private_options));
1074 if (server_id_is_disconnected(&entry->pid)) {
1076 * note: cleanup should have been done by
1077 * delay_for_batch_oplocks()
1082 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1083 (unsigned int)access_mask, (unsigned int)share_access));
1085 if ((entry->access_mask & (FILE_WRITE_DATA|
1089 DELETE_ACCESS)) == 0) {
1090 DEBUG(10,("share_conflict: No conflict due to "
1091 "entry->access_mask = 0x%x\n",
1092 (unsigned int)entry->access_mask ));
1096 if ((access_mask & (FILE_WRITE_DATA|
1100 DELETE_ACCESS)) == 0) {
1101 DEBUG(10,("share_conflict: No conflict due to "
1102 "access_mask = 0x%x\n",
1103 (unsigned int)access_mask ));
1107 #if 1 /* JRA TEST - Superdebug. */
1108 #define CHECK_MASK(num, am, right, sa, share) \
1109 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1110 (unsigned int)(num), (unsigned int)(am), \
1111 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1112 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1113 (unsigned int)(num), (unsigned int)(sa), \
1114 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1115 if (((am) & (right)) && !((sa) & (share))) { \
1116 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1117 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1118 (unsigned int)(share) )); \
1122 #define CHECK_MASK(num, am, right, sa, share) \
1123 if (((am) & (right)) && !((sa) & (share))) { \
1124 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1125 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1126 (unsigned int)(share) )); \
1131 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1132 share_access, FILE_SHARE_WRITE);
1133 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1134 entry->share_access, FILE_SHARE_WRITE);
1136 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1137 share_access, FILE_SHARE_READ);
1138 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1139 entry->share_access, FILE_SHARE_READ);
1141 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1142 share_access, FILE_SHARE_DELETE);
1143 CHECK_MASK(6, access_mask, DELETE_ACCESS,
1144 entry->share_access, FILE_SHARE_DELETE);
1146 DEBUG(10,("share_conflict: No conflict.\n"));
1150 #if defined(DEVELOPER)
1151 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1153 struct share_mode_entry *share_entry)
1155 struct server_id self = messaging_server_id(sconn->msg_ctx);
1158 if (!serverid_equal(&self, &share_entry->pid)) {
1162 if (share_entry->op_mid == 0) {
1163 /* INTERNAL_OPEN_ONLY */
1167 if (!is_valid_share_mode_entry(share_entry)) {
1171 fsp = file_find_dif(sconn, share_entry->id,
1172 share_entry->share_file_id);
1174 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1175 share_mode_str(talloc_tos(), num, share_entry) ));
1176 smb_panic("validate_my_share_entries: Cannot match a "
1177 "share entry with an open file\n");
1180 if (((uint16_t)fsp->oplock_type) != share_entry->op_type) {
1189 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1190 share_mode_str(talloc_tos(), num, share_entry) ));
1191 str = talloc_asprintf(talloc_tos(),
1192 "validate_my_share_entries: "
1193 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1194 fsp->fsp_name->base_name,
1195 (unsigned int)fsp->oplock_type,
1196 (unsigned int)share_entry->op_type );
1202 bool is_stat_open(uint32_t access_mask)
1204 const uint32_t stat_open_bits =
1205 (SYNCHRONIZE_ACCESS|
1206 FILE_READ_ATTRIBUTES|
1207 FILE_WRITE_ATTRIBUTES);
1209 return (((access_mask & stat_open_bits) != 0) &&
1210 ((access_mask & ~stat_open_bits) == 0));
1213 static bool has_delete_on_close(struct share_mode_lock *lck,
1216 struct share_mode_data *d = lck->data;
1219 if (d->num_share_modes == 0) {
1222 if (!is_delete_on_close_set(lck, name_hash)) {
1225 for (i=0; i<d->num_share_modes; i++) {
1226 if (!share_mode_stale_pid(d, i)) {
1233 /****************************************************************************
1234 Deal with share modes
1235 Invariant: Share mode must be locked on entry and exit.
1236 Returns -1 on error, or number of share modes on success (may be zero).
1237 ****************************************************************************/
1239 static NTSTATUS open_mode_check(connection_struct *conn,
1240 struct share_mode_lock *lck,
1241 uint32_t access_mask,
1242 uint32_t share_access)
1246 if(lck->data->num_share_modes == 0) {
1247 return NT_STATUS_OK;
1250 if (is_stat_open(access_mask)) {
1251 /* Stat open that doesn't trigger oplock breaks or share mode
1252 * checks... ! JRA. */
1253 return NT_STATUS_OK;
1257 * Check if the share modes will give us access.
1260 #if defined(DEVELOPER)
1261 for(i = 0; i < lck->data->num_share_modes; i++) {
1262 validate_my_share_entries(conn->sconn, i,
1263 &lck->data->share_modes[i]);
1267 /* Now we check the share modes, after any oplock breaks. */
1268 for(i = 0; i < lck->data->num_share_modes; i++) {
1270 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1274 /* someone else has a share lock on it, check to see if we can
1276 if (share_conflict(&lck->data->share_modes[i],
1277 access_mask, share_access)) {
1279 if (share_mode_stale_pid(lck->data, i)) {
1283 return NT_STATUS_SHARING_VIOLATION;
1287 return NT_STATUS_OK;
1291 * Send a break message to the oplock holder and delay the open for
1295 NTSTATUS send_break_message(struct messaging_context *msg_ctx,
1296 const struct share_mode_entry *exclusive,
1300 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1301 struct server_id_buf tmp;
1303 DEBUG(10, ("Sending break request to PID %s\n",
1304 server_id_str_buf(exclusive->pid, &tmp)));
1306 /* Create the message. */
1307 share_mode_entry_to_message(msg, exclusive);
1309 /* Overload entry->op_type */
1311 * This is a cut from uint32_t to uint16_t, but so far only the lower 3
1312 * bits (LEASE_WRITE/HANDLE/READ are used anyway.
1314 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, break_to);
1316 status = messaging_send_buf(msg_ctx, exclusive->pid,
1317 MSG_SMB_BREAK_REQUEST,
1318 (uint8_t *)msg, sizeof(msg));
1319 if (!NT_STATUS_IS_OK(status)) {
1320 DEBUG(3, ("Could not send oplock break message: %s\n",
1321 nt_errstr(status)));
1328 * Do internal consistency checks on the share mode for a file.
1331 static bool validate_oplock_types(struct share_mode_lock *lck)
1333 struct share_mode_data *d = lck->data;
1335 bool ex_or_batch = false;
1336 bool level2 = false;
1337 bool no_oplock = false;
1338 uint32_t num_non_stat_opens = 0;
1341 for (i=0; i<d->num_share_modes; i++) {
1342 struct share_mode_entry *e = &d->share_modes[i];
1344 if (!is_valid_share_mode_entry(e)) {
1348 if (e->op_mid == 0) {
1349 /* INTERNAL_OPEN_ONLY */
1353 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1354 /* We ignore stat opens in the table - they
1355 always have NO_OPLOCK and never get or
1356 cause breaks. JRA. */
1360 num_non_stat_opens += 1;
1362 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1363 /* batch - can only be one. */
1364 if (share_mode_stale_pid(d, i)) {
1365 DEBUG(10, ("Found stale batch oplock\n"));
1368 if (ex_or_batch || batch || level2 || no_oplock) {
1369 DEBUG(0, ("Bad batch oplock entry %u.",
1376 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1377 if (share_mode_stale_pid(d, i)) {
1378 DEBUG(10, ("Found stale duplicate oplock\n"));
1381 /* Exclusive or batch - can only be one. */
1382 if (ex_or_batch || level2 || no_oplock) {
1383 DEBUG(0, ("Bad exclusive or batch oplock "
1384 "entry %u.", (unsigned)i));
1390 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1391 if (batch || ex_or_batch) {
1392 if (share_mode_stale_pid(d, i)) {
1393 DEBUG(10, ("Found stale LevelII "
1397 DEBUG(0, ("Bad levelII oplock entry %u.",
1404 if (e->op_type == NO_OPLOCK) {
1405 if (batch || ex_or_batch) {
1406 if (share_mode_stale_pid(d, i)) {
1407 DEBUG(10, ("Found stale NO_OPLOCK "
1411 DEBUG(0, ("Bad no oplock entry %u.",
1419 remove_stale_share_mode_entries(d);
1421 if ((batch || ex_or_batch) && (num_non_stat_opens != 1)) {
1422 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1423 (int)batch, (int)ex_or_batch,
1424 (int)d->num_share_modes));
1431 static bool delay_for_oplock(files_struct *fsp,
1433 const struct smb2_lease *lease,
1434 struct share_mode_lock *lck,
1435 bool have_sharing_violation,
1436 uint32_t create_disposition,
1437 bool first_open_attempt)
1439 struct share_mode_data *d = lck->data;
1442 bool will_overwrite;
1444 if ((oplock_request & INTERNAL_OPEN_ONLY) ||
1445 is_stat_open(fsp->access_mask)) {
1449 switch (create_disposition) {
1450 case FILE_SUPERSEDE:
1451 case FILE_OVERWRITE:
1452 case FILE_OVERWRITE_IF:
1453 will_overwrite = true;
1456 will_overwrite = false;
1460 for (i=0; i<d->num_share_modes; i++) {
1461 struct share_mode_entry *e = &d->share_modes[i];
1462 struct share_mode_lease *l = NULL;
1463 uint32_t e_lease_type = get_lease_type(d, e);
1465 uint32_t delay_mask = 0;
1467 if (e->op_type == LEASE_OPLOCK) {
1468 l = &d->leases[e->lease_idx];
1471 if (have_sharing_violation) {
1472 delay_mask = SMB2_LEASE_HANDLE;
1474 delay_mask = SMB2_LEASE_WRITE;
1477 break_to = e_lease_type & ~delay_mask;
1479 if (will_overwrite) {
1481 * we'll decide about SMB2_LEASE_READ later.
1483 * Maybe the break will be deferred
1485 break_to &= ~SMB2_LEASE_HANDLE;
1488 DEBUG(10, ("entry %u: e_lease_type %u, will_overwrite: %u\n",
1489 (unsigned)i, (unsigned)e_lease_type,
1490 (unsigned)will_overwrite));
1492 if (lease != NULL && l != NULL) {
1495 ign = smb2_lease_equal(fsp_client_guid(fsp),
1504 if ((e_lease_type & ~break_to) == 0) {
1505 if (l != NULL && l->breaking) {
1511 if (share_mode_stale_pid(d, i)) {
1515 if (will_overwrite) {
1517 * If we break anyway break to NONE directly.
1518 * Otherwise vfs_set_filelen() will trigger the
1521 break_to &= ~(SMB2_LEASE_READ|SMB2_LEASE_WRITE);
1524 if (e->op_type != LEASE_OPLOCK) {
1526 * Oplocks only support breaking to R or NONE.
1528 break_to &= ~(SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1531 DEBUG(10, ("breaking from %d to %d\n",
1532 (int)e_lease_type, (int)break_to));
1533 send_break_message(fsp->conn->sconn->msg_ctx, e,
1535 if (e_lease_type & delay_mask) {
1538 if (l != NULL && l->breaking && !first_open_attempt) {
1547 static bool file_has_brlocks(files_struct *fsp)
1549 struct byte_range_lock *br_lck;
1551 br_lck = brl_get_locks_readonly(fsp);
1555 return (brl_num_locks(br_lck) > 0);
1558 int find_share_mode_lease(struct share_mode_data *d,
1559 const struct GUID *client_guid,
1560 const struct smb2_lease_key *key)
1564 for (i=0; i<d->num_leases; i++) {
1565 struct share_mode_lease *l = &d->leases[i];
1567 if (smb2_lease_equal(client_guid,
1578 struct fsp_lease *find_fsp_lease(struct files_struct *new_fsp,
1579 const struct smb2_lease_key *key,
1580 const struct share_mode_lease *l)
1582 struct files_struct *fsp;
1585 * TODO: Measure how expensive this loop is with thousands of open
1589 for (fsp = file_find_di_first(new_fsp->conn->sconn, new_fsp->file_id);
1591 fsp = file_find_di_next(fsp)) {
1593 if (fsp == new_fsp) {
1596 if (fsp->oplock_type != LEASE_OPLOCK) {
1599 if (smb2_lease_key_equal(&fsp->lease->lease.lease_key, key)) {
1600 fsp->lease->ref_count += 1;
1605 /* Not found - must be leased in another smbd. */
1606 new_fsp->lease = talloc_zero(new_fsp->conn->sconn, struct fsp_lease);
1607 if (new_fsp->lease == NULL) {
1610 new_fsp->lease->ref_count = 1;
1611 new_fsp->lease->sconn = new_fsp->conn->sconn;
1612 new_fsp->lease->lease.lease_key = *key;
1613 new_fsp->lease->lease.lease_state = l->current_state;
1615 * We internally treat all leases as V2 and update
1616 * the epoch, but when sending breaks it matters if
1617 * the requesting lease was v1 or v2.
1619 new_fsp->lease->lease.lease_version = l->lease_version;
1620 new_fsp->lease->lease.lease_epoch = l->epoch;
1621 return new_fsp->lease;
1624 static NTSTATUS grant_fsp_lease(struct files_struct *fsp,
1625 struct share_mode_lock *lck,
1626 const struct smb2_lease *lease,
1627 uint32_t *p_lease_idx,
1630 struct share_mode_data *d = lck->data;
1631 const struct GUID *client_guid = fsp_client_guid(fsp);
1632 struct share_mode_lease *tmp;
1636 idx = find_share_mode_lease(d, client_guid, &lease->lease_key);
1639 struct share_mode_lease *l = &d->leases[idx];
1641 uint32_t existing, requested;
1643 fsp->lease = find_fsp_lease(fsp, &lease->lease_key, l);
1644 if (fsp->lease == NULL) {
1645 DEBUG(1, ("Did not find existing lease for file %s\n",
1647 return NT_STATUS_NO_MEMORY;
1653 * Upgrade only if the requested lease is a strict upgrade.
1655 existing = l->current_state;
1656 requested = lease->lease_state;
1659 * Tricky: This test makes sure that "requested" is a
1660 * strict bitwise superset of "existing".
1662 do_upgrade = ((existing & requested) == existing);
1665 * Upgrade only if there's a change.
1667 do_upgrade &= (granted != existing);
1670 * Upgrade only if other leases don't prevent what was asked
1673 do_upgrade &= (granted == requested);
1676 * only upgrade if we are not in breaking state
1678 do_upgrade &= !l->breaking;
1680 DEBUG(10, ("existing=%"PRIu32", requested=%"PRIu32", "
1681 "granted=%"PRIu32", do_upgrade=%d\n",
1682 existing, requested, granted, (int)do_upgrade));
1685 l->current_state = granted;
1689 /* Ensure we're in sync with current lease state. */
1690 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
1691 return NT_STATUS_OK;
1698 tmp = talloc_realloc(d, d->leases, struct share_mode_lease,
1704 return NT_STATUS_INSUFFICIENT_RESOURCES;
1708 fsp->lease = talloc_zero(fsp->conn->sconn, struct fsp_lease);
1709 if (fsp->lease == NULL) {
1710 return NT_STATUS_INSUFFICIENT_RESOURCES;
1712 fsp->lease->ref_count = 1;
1713 fsp->lease->sconn = fsp->conn->sconn;
1714 fsp->lease->lease.lease_version = lease->lease_version;
1715 fsp->lease->lease.lease_key = lease->lease_key;
1716 fsp->lease->lease.lease_state = granted;
1717 fsp->lease->lease.lease_epoch = lease->lease_epoch + 1;
1719 *p_lease_idx = d->num_leases;
1721 d->leases[d->num_leases] = (struct share_mode_lease) {
1722 .client_guid = *client_guid,
1723 .lease_key = fsp->lease->lease.lease_key,
1724 .current_state = fsp->lease->lease.lease_state,
1725 .lease_version = fsp->lease->lease.lease_version,
1726 .epoch = fsp->lease->lease.lease_epoch,
1729 status = leases_db_add(client_guid,
1732 fsp->conn->connectpath,
1733 fsp->fsp_name->base_name,
1734 fsp->fsp_name->stream_name);
1735 if (!NT_STATUS_IS_OK(status)) {
1736 DEBUG(10, ("%s: leases_db_add failed: %s\n", __func__,
1737 nt_errstr(status)));
1738 TALLOC_FREE(fsp->lease);
1739 return NT_STATUS_INSUFFICIENT_RESOURCES;
1745 return NT_STATUS_OK;
1748 static bool is_same_lease(const files_struct *fsp,
1749 const struct share_mode_data *d,
1750 const struct share_mode_entry *e,
1751 const struct smb2_lease *lease)
1753 if (e->op_type != LEASE_OPLOCK) {
1756 if (lease == NULL) {
1760 return smb2_lease_equal(fsp_client_guid(fsp),
1762 &d->leases[e->lease_idx].client_guid,
1763 &d->leases[e->lease_idx].lease_key);
1766 static NTSTATUS grant_fsp_oplock_type(struct smb_request *req,
1767 struct files_struct *fsp,
1768 struct share_mode_lock *lck,
1770 struct smb2_lease *lease)
1772 struct share_mode_data *d = lck->data;
1773 bool got_handle_lease = false;
1774 bool got_oplock = false;
1777 uint32_t lease_idx = UINT32_MAX;
1781 if (oplock_request & INTERNAL_OPEN_ONLY) {
1782 /* No oplocks on internal open. */
1783 oplock_request = NO_OPLOCK;
1784 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1785 fsp->oplock_type, fsp_str_dbg(fsp)));
1788 if (oplock_request == LEASE_OPLOCK) {
1789 if (lease == NULL) {
1791 * The SMB2 layer should have checked this
1793 return NT_STATUS_INTERNAL_ERROR;
1796 granted = lease->lease_state;
1798 if (lp_kernel_oplocks(SNUM(fsp->conn))) {
1799 DEBUG(10, ("No lease granted because kernel oplocks are enabled\n"));
1800 granted = SMB2_LEASE_NONE;
1802 if ((granted & (SMB2_LEASE_READ|SMB2_LEASE_WRITE)) == 0) {
1803 DEBUG(10, ("No read or write lease requested\n"));
1804 granted = SMB2_LEASE_NONE;
1806 if (granted == SMB2_LEASE_WRITE) {
1807 DEBUG(10, ("pure write lease requested\n"));
1808 granted = SMB2_LEASE_NONE;
1810 if (granted == (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
1811 DEBUG(10, ("write and handle lease requested\n"));
1812 granted = SMB2_LEASE_NONE;
1815 granted = map_oplock_to_lease_type(
1816 oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1819 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1820 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1822 granted &= ~SMB2_LEASE_READ;
1825 for (i=0; i<d->num_share_modes; i++) {
1826 struct share_mode_entry *e = &d->share_modes[i];
1827 uint32_t e_lease_type;
1829 e_lease_type = get_lease_type(d, e);
1831 if ((granted & SMB2_LEASE_WRITE) &&
1832 !is_same_lease(fsp, d, e, lease) &&
1833 !share_mode_stale_pid(d, i)) {
1835 * Can grant only one writer
1837 granted &= ~SMB2_LEASE_WRITE;
1840 if ((e_lease_type & SMB2_LEASE_HANDLE) && !got_handle_lease &&
1841 !share_mode_stale_pid(d, i)) {
1842 got_handle_lease = true;
1845 if ((e->op_type != LEASE_OPLOCK) && !got_oplock &&
1846 !share_mode_stale_pid(d, i)) {
1851 if ((granted & SMB2_LEASE_READ) && !(granted & SMB2_LEASE_WRITE)) {
1853 (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1854 lp_level2_oplocks(SNUM(fsp->conn));
1856 if (!allow_level2) {
1857 granted = SMB2_LEASE_NONE;
1861 if (oplock_request == LEASE_OPLOCK) {
1863 granted &= ~SMB2_LEASE_HANDLE;
1866 fsp->oplock_type = LEASE_OPLOCK;
1868 status = grant_fsp_lease(fsp, lck, lease, &lease_idx,
1870 if (!NT_STATUS_IS_OK(status)) {
1874 *lease = fsp->lease->lease;
1875 DEBUG(10, ("lease_state=%d\n", lease->lease_state));
1877 if (got_handle_lease) {
1878 granted = SMB2_LEASE_NONE;
1882 case SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE:
1883 fsp->oplock_type = BATCH_OPLOCK|EXCLUSIVE_OPLOCK;
1885 case SMB2_LEASE_READ|SMB2_LEASE_WRITE:
1886 fsp->oplock_type = EXCLUSIVE_OPLOCK;
1888 case SMB2_LEASE_READ|SMB2_LEASE_HANDLE:
1889 case SMB2_LEASE_READ:
1890 fsp->oplock_type = LEVEL_II_OPLOCK;
1893 fsp->oplock_type = NO_OPLOCK;
1897 status = set_file_oplock(fsp);
1898 if (!NT_STATUS_IS_OK(status)) {
1900 * Could not get the kernel oplock
1902 fsp->oplock_type = NO_OPLOCK;
1906 ok = set_share_mode(lck, fsp, get_current_uid(fsp->conn),
1911 return NT_STATUS_NO_MEMORY;
1914 ok = update_num_read_oplocks(fsp, lck);
1916 del_share_mode(lck, fsp);
1917 return NT_STATUS_INTERNAL_ERROR;
1920 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1921 fsp->oplock_type, fsp_str_dbg(fsp)));
1923 return NT_STATUS_OK;
1926 static bool request_timed_out(struct timeval request_time,
1927 struct timeval timeout)
1929 struct timeval now, end_time;
1931 end_time = timeval_sum(&request_time, &timeout);
1932 return (timeval_compare(&end_time, &now) < 0);
1935 static struct deferred_open_record *deferred_open_record_create(
1936 bool delayed_for_oplocks,
1940 struct deferred_open_record *record = NULL;
1942 record = talloc(NULL, struct deferred_open_record);
1943 if (record == NULL) {
1947 *record = (struct deferred_open_record) {
1948 .delayed_for_oplocks = delayed_for_oplocks,
1949 .async_open = async_open,
1956 struct defer_open_state {
1957 struct smbXsrv_connection *xconn;
1961 static void defer_open_done(struct tevent_req *req);
1963 /****************************************************************************
1964 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1965 ****************************************************************************/
1967 static void defer_open(struct share_mode_lock *lck,
1968 struct timeval request_time,
1969 struct timeval timeout,
1970 struct smb_request *req,
1971 bool delayed_for_oplocks,
1975 struct deferred_open_record *open_rec = NULL;
1976 struct timeval abs_timeout;
1978 abs_timeout = timeval_sum(&request_time, &timeout);
1980 DBG_DEBUG("request time [%s] timeout [%s] mid [%" PRIu64 "] "
1981 "delayed_for_oplocks [%s] async_open [%s] file_id [%s]\n",
1982 timeval_string(talloc_tos(), &request_time, false),
1983 timeval_string(talloc_tos(), &abs_timeout, false),
1985 delayed_for_oplocks ? "yes" : "no",
1986 async_open ? "yes" : "no",
1987 file_id_string_tos(&id));
1989 open_rec = deferred_open_record_create(delayed_for_oplocks,
1992 if (open_rec == NULL) {
1994 exit_server("talloc failed");
1998 struct defer_open_state *watch_state;
1999 struct tevent_req *watch_req;
2002 watch_state = talloc(open_rec, struct defer_open_state);
2003 if (watch_state == NULL) {
2004 exit_server("talloc failed");
2006 watch_state->xconn = req->xconn;
2007 watch_state->mid = req->mid;
2009 DEBUG(10, ("defering mid %llu\n",
2010 (unsigned long long)req->mid));
2012 watch_req = dbwrap_watched_watch_send(
2013 watch_state, req->sconn->ev_ctx, lck->data->record,
2014 (struct server_id){0});
2015 if (watch_req == NULL) {
2016 exit_server("Could not watch share mode record");
2018 tevent_req_set_callback(watch_req, defer_open_done,
2021 ret = tevent_req_set_endtime(
2022 watch_req, req->sconn->ev_ctx,
2027 if (!push_deferred_open_message_smb(req, request_time, timeout,
2028 open_rec->id, open_rec)) {
2030 exit_server("push_deferred_open_message_smb failed");
2034 static void defer_open_done(struct tevent_req *req)
2036 struct defer_open_state *state = tevent_req_callback_data(
2037 req, struct defer_open_state);
2041 status = dbwrap_watched_watch_recv(req, talloc_tos(), NULL, NULL,
2044 if (!NT_STATUS_IS_OK(status)) {
2045 DEBUG(5, ("dbwrap_watched_watch_recv returned %s\n",
2046 nt_errstr(status)));
2048 * Even if it failed, retry anyway. TODO: We need a way to
2049 * tell a re-scheduled open about that error.
2053 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
2055 ret = schedule_deferred_open_message_smb(state->xconn, state->mid);
2061 /****************************************************************************
2062 On overwrite open ensure that the attributes match.
2063 ****************************************************************************/
2065 static bool open_match_attributes(connection_struct *conn,
2066 uint32_t old_dos_attr,
2067 uint32_t new_dos_attr,
2068 mode_t existing_unx_mode,
2069 mode_t new_unx_mode,
2070 mode_t *returned_unx_mode)
2072 uint32_t noarch_old_dos_attr, noarch_new_dos_attr;
2074 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
2075 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
2077 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
2078 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
2079 *returned_unx_mode = new_unx_mode;
2081 *returned_unx_mode = (mode_t)0;
2084 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
2085 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
2086 "returned_unx_mode = 0%o\n",
2087 (unsigned int)old_dos_attr,
2088 (unsigned int)existing_unx_mode,
2089 (unsigned int)new_dos_attr,
2090 (unsigned int)*returned_unx_mode ));
2092 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
2093 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
2094 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
2095 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
2099 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
2100 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
2101 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
2108 /****************************************************************************
2109 Special FCB or DOS processing in the case of a sharing violation.
2110 Try and find a duplicated file handle.
2111 ****************************************************************************/
2113 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
2114 connection_struct *conn,
2115 files_struct *fsp_to_dup_into,
2116 const struct smb_filename *smb_fname,
2120 uint32_t access_mask,
2121 uint32_t share_access,
2122 uint32_t create_options)
2126 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
2127 "file %s.\n", smb_fname_str_dbg(smb_fname)));
2129 for(fsp = file_find_di_first(conn->sconn, id); fsp;
2130 fsp = file_find_di_next(fsp)) {
2132 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
2133 "vuid = %llu, file_pid = %u, private_options = 0x%x "
2134 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
2135 fsp->fh->fd, (unsigned long long)fsp->vuid,
2136 (unsigned int)fsp->file_pid,
2137 (unsigned int)fsp->fh->private_options,
2138 (unsigned int)fsp->access_mask ));
2140 if (fsp != fsp_to_dup_into &&
2141 fsp->fh->fd != -1 &&
2142 fsp->vuid == vuid &&
2143 fsp->file_pid == file_pid &&
2144 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
2145 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
2146 (fsp->access_mask & FILE_WRITE_DATA) &&
2147 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
2148 strequal(fsp->fsp_name->stream_name,
2149 smb_fname->stream_name)) {
2150 DEBUG(10,("fcb_or_dos_open: file match\n"));
2156 return NT_STATUS_NOT_FOUND;
2159 /* quite an insane set of semantics ... */
2160 if (is_executable(smb_fname->base_name) &&
2161 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
2162 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
2163 return NT_STATUS_INVALID_PARAMETER;
2166 /* We need to duplicate this fsp. */
2167 return dup_file_fsp(req, fsp, access_mask, share_access,
2168 create_options, fsp_to_dup_into);
2171 static void schedule_defer_open(struct share_mode_lock *lck,
2173 struct timeval request_time,
2174 struct smb_request *req)
2176 /* This is a relative time, added to the absolute
2177 request_time value to get the absolute timeout time.
2178 Note that if this is the second or greater time we enter
2179 this codepath for this particular request mid then
2180 request_time is left as the absolute time of the *first*
2181 time this request mid was processed. This is what allows
2182 the request to eventually time out. */
2184 struct timeval timeout;
2186 /* Normally the smbd we asked should respond within
2187 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
2188 * the client did, give twice the timeout as a safety
2189 * measure here in case the other smbd is stuck
2190 * somewhere else. */
2192 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
2194 if (request_timed_out(request_time, timeout)) {
2198 defer_open(lck, request_time, timeout, req, true, false, id);
2201 /****************************************************************************
2202 Reschedule an open call that went asynchronous.
2203 ****************************************************************************/
2205 static void schedule_async_open(struct timeval request_time,
2206 struct smb_request *req)
2208 struct timeval timeout;
2210 timeout = timeval_set(20, 0);
2212 if (request_timed_out(request_time, timeout)) {
2216 defer_open(NULL, request_time, timeout, req,
2217 false, true, (struct file_id){0});
2220 /****************************************************************************
2221 Work out what access_mask to use from what the client sent us.
2222 ****************************************************************************/
2224 static NTSTATUS smbd_calculate_maximum_allowed_access(
2225 connection_struct *conn,
2226 const struct smb_filename *smb_fname,
2228 uint32_t *p_access_mask)
2230 struct security_descriptor *sd;
2231 uint32_t access_granted;
2234 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
2235 *p_access_mask |= FILE_GENERIC_ALL;
2236 return NT_STATUS_OK;
2239 status = SMB_VFS_GET_NT_ACL(conn, smb_fname,
2245 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2247 * File did not exist
2249 *p_access_mask = FILE_GENERIC_ALL;
2250 return NT_STATUS_OK;
2252 if (!NT_STATUS_IS_OK(status)) {
2253 DEBUG(10,("Could not get acl on file %s: %s\n",
2254 smb_fname_str_dbg(smb_fname),
2255 nt_errstr(status)));
2256 return NT_STATUS_ACCESS_DENIED;
2260 * If we can access the path to this file, by
2261 * default we have FILE_READ_ATTRIBUTES from the
2262 * containing directory. See the section:
2263 * "Algorithm to Check Access to an Existing File"
2266 * se_file_access_check()
2267 * also takes care of owner WRITE_DAC and READ_CONTROL.
2269 status = se_file_access_check(sd,
2270 get_current_nttok(conn),
2272 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
2277 if (!NT_STATUS_IS_OK(status)) {
2278 DEBUG(10, ("Access denied on file %s: "
2279 "when calculating maximum access\n",
2280 smb_fname_str_dbg(smb_fname)));
2281 return NT_STATUS_ACCESS_DENIED;
2283 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
2285 if (!(access_granted & DELETE_ACCESS)) {
2286 if (can_delete_file_in_directory(conn, smb_fname)) {
2287 *p_access_mask |= DELETE_ACCESS;
2291 return NT_STATUS_OK;
2294 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
2295 const struct smb_filename *smb_fname,
2297 uint32_t access_mask,
2298 uint32_t *access_mask_out)
2301 uint32_t orig_access_mask = access_mask;
2302 uint32_t rejected_share_access;
2304 if (access_mask & SEC_MASK_INVALID) {
2305 DBG_DEBUG("access_mask [%8x] contains invalid bits\n",
2307 return NT_STATUS_ACCESS_DENIED;
2311 * Convert GENERIC bits to specific bits.
2314 se_map_generic(&access_mask, &file_generic_mapping);
2316 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
2317 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
2319 status = smbd_calculate_maximum_allowed_access(
2320 conn, smb_fname, use_privs, &access_mask);
2322 if (!NT_STATUS_IS_OK(status)) {
2326 access_mask &= conn->share_access;
2329 rejected_share_access = access_mask & ~(conn->share_access);
2331 if (rejected_share_access) {
2332 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
2333 "file %s: rejected by share access mask[0x%08X] "
2334 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
2335 smb_fname_str_dbg(smb_fname),
2337 orig_access_mask, access_mask,
2338 rejected_share_access));
2339 return NT_STATUS_ACCESS_DENIED;
2342 *access_mask_out = access_mask;
2343 return NT_STATUS_OK;
2346 /****************************************************************************
2347 Remove the deferred open entry under lock.
2348 ****************************************************************************/
2350 /****************************************************************************
2351 Return true if this is a state pointer to an asynchronous create.
2352 ****************************************************************************/
2354 bool is_deferred_open_async(const struct deferred_open_record *rec)
2356 return rec->async_open;
2359 static bool clear_ads(uint32_t create_disposition)
2363 switch (create_disposition) {
2364 case FILE_SUPERSEDE:
2365 case FILE_OVERWRITE_IF:
2366 case FILE_OVERWRITE:
2375 static int disposition_to_open_flags(uint32_t create_disposition)
2380 * Currently we're using FILE_SUPERSEDE as the same as
2381 * FILE_OVERWRITE_IF but they really are
2382 * different. FILE_SUPERSEDE deletes an existing file
2383 * (requiring delete access) then recreates it.
2386 switch (create_disposition) {
2387 case FILE_SUPERSEDE:
2388 case FILE_OVERWRITE_IF:
2390 * If file exists replace/overwrite. If file doesn't
2393 ret = O_CREAT|O_TRUNC;
2398 * If file exists open. If file doesn't exist error.
2403 case FILE_OVERWRITE:
2405 * If file exists overwrite. If file doesn't exist
2413 * If file exists error. If file doesn't exist create.
2415 ret = O_CREAT|O_EXCL;
2420 * If file exists open. If file doesn't exist create.
2428 static int calculate_open_access_flags(uint32_t access_mask,
2429 uint32_t private_flags)
2431 bool need_write, need_read;
2434 * Note that we ignore the append flag as append does not
2435 * mean the same thing under DOS and Unix.
2438 need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
2443 /* DENY_DOS opens are always underlying read-write on the
2444 file handle, no matter what the requested access mask
2448 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2449 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
2450 FILE_READ_EA|FILE_EXECUTE));
2458 /****************************************************************************
2459 Open a file with a share mode. Passed in an already created files_struct *.
2460 ****************************************************************************/
2462 static NTSTATUS open_file_ntcreate(connection_struct *conn,
2463 struct smb_request *req,
2464 uint32_t access_mask, /* access bits (FILE_READ_DATA etc.) */
2465 uint32_t share_access, /* share constants (FILE_SHARE_READ etc) */
2466 uint32_t create_disposition, /* FILE_OPEN_IF etc. */
2467 uint32_t create_options, /* options such as delete on close. */
2468 uint32_t new_dos_attributes, /* attributes used for new file. */
2469 int oplock_request, /* internal Samba oplock codes. */
2470 struct smb2_lease *lease,
2471 /* Information (FILE_EXISTS etc.) */
2472 uint32_t private_flags, /* Samba specific flags. */
2476 struct smb_filename *smb_fname = fsp->fsp_name;
2479 bool file_existed = VALID_STAT(smb_fname->st);
2480 bool def_acl = False;
2481 bool posix_open = False;
2482 bool new_file_created = False;
2483 bool first_open_attempt = true;
2484 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
2485 mode_t new_unx_mode = (mode_t)0;
2486 mode_t unx_mode = (mode_t)0;
2488 uint32_t existing_dos_attributes = 0;
2489 struct timeval request_time = timeval_zero();
2490 struct share_mode_lock *lck = NULL;
2491 uint32_t open_access_mask = access_mask;
2494 SMB_STRUCT_STAT saved_stat = smb_fname->st;
2495 struct timespec old_write_time;
2498 if (conn->printer) {
2500 * Printers are handled completely differently.
2501 * Most of the passed parameters are ignored.
2505 *pinfo = FILE_WAS_CREATED;
2508 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2509 smb_fname_str_dbg(smb_fname)));
2512 DEBUG(0,("open_file_ntcreate: printer open without "
2513 "an SMB request!\n"));
2514 return NT_STATUS_INTERNAL_ERROR;
2517 return print_spool_open(fsp, smb_fname->base_name,
2521 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2523 return NT_STATUS_NO_MEMORY;
2526 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2528 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2529 new_dos_attributes = 0;
2531 /* Windows allows a new file to be created and
2532 silently removes a FILE_ATTRIBUTE_DIRECTORY
2533 sent by the client. Do the same. */
2535 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2537 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2539 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2540 smb_fname, parent_dir);
2543 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2544 "access_mask=0x%x share_access=0x%x "
2545 "create_disposition = 0x%x create_options=0x%x "
2546 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2547 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2548 access_mask, share_access, create_disposition,
2549 create_options, (unsigned int)unx_mode, oplock_request,
2550 (unsigned int)private_flags));
2553 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2554 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) != 0));
2556 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2557 SMB_ASSERT(((oplock_request & INTERNAL_OPEN_ONLY) == 0));
2561 * Only non-internal opens can be deferred at all
2565 struct deferred_open_record *open_rec;
2566 if (get_deferred_open_message_state(req,
2569 /* Remember the absolute time of the original
2570 request with this mid. We'll use it later to
2571 see if this has timed out. */
2573 /* If it was an async create retry, the file
2576 if (is_deferred_open_async(open_rec)) {
2577 SET_STAT_INVALID(smb_fname->st);
2578 file_existed = false;
2581 /* Ensure we don't reprocess this message. */
2582 remove_deferred_open_message_smb(req->xconn, req->mid);
2584 first_open_attempt = false;
2589 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2592 * Only use strored DOS attributes for checks
2593 * against requested attributes (below via
2594 * open_match_attributes()), cf bug #11992
2595 * for details. -slow
2599 status = SMB_VFS_GET_DOS_ATTRIBUTES(conn, smb_fname, &attr);
2600 if (NT_STATUS_IS_OK(status)) {
2601 existing_dos_attributes = attr;
2606 /* ignore any oplock requests if oplocks are disabled */
2607 if (!lp_oplocks(SNUM(conn)) ||
2608 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2609 /* Mask off everything except the private Samba bits. */
2610 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2613 /* this is for OS/2 long file names - say we don't support them */
2614 if (req != NULL && !req->posix_pathnames &&
2615 strstr(smb_fname->base_name,".+,;=[].")) {
2616 /* OS/2 Workplace shell fix may be main code stream in a later
2618 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2620 if (use_nt_status()) {
2621 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2623 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2626 switch( create_disposition ) {
2628 /* If file exists open. If file doesn't exist error. */
2629 if (!file_existed) {
2630 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2631 "requested for file %s and file "
2633 smb_fname_str_dbg(smb_fname)));
2635 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2639 case FILE_OVERWRITE:
2640 /* If file exists overwrite. If file doesn't exist
2642 if (!file_existed) {
2643 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2644 "requested for file %s and file "
2646 smb_fname_str_dbg(smb_fname) ));
2648 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2653 /* If file exists error. If file doesn't exist
2656 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2657 "requested for file %s and file "
2658 "already exists.\n",
2659 smb_fname_str_dbg(smb_fname)));
2660 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2665 return map_nt_error_from_unix(errno);
2669 case FILE_SUPERSEDE:
2670 case FILE_OVERWRITE_IF:
2674 return NT_STATUS_INVALID_PARAMETER;
2677 flags2 = disposition_to_open_flags(create_disposition);
2679 /* We only care about matching attributes on file exists and
2682 if (!posix_open && file_existed &&
2683 ((create_disposition == FILE_OVERWRITE) ||
2684 (create_disposition == FILE_OVERWRITE_IF))) {
2685 if (!open_match_attributes(conn, existing_dos_attributes,
2687 smb_fname->st.st_ex_mode,
2688 unx_mode, &new_unx_mode)) {
2689 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2690 "for file %s (%x %x) (0%o, 0%o)\n",
2691 smb_fname_str_dbg(smb_fname),
2692 existing_dos_attributes,
2694 (unsigned int)smb_fname->st.st_ex_mode,
2695 (unsigned int)unx_mode ));
2697 return NT_STATUS_ACCESS_DENIED;
2701 status = smbd_calculate_access_mask(conn, smb_fname,
2705 if (!NT_STATUS_IS_OK(status)) {
2706 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2707 "on file %s returned %s\n",
2708 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2712 open_access_mask = access_mask;
2714 if (flags2 & O_TRUNC) {
2715 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2718 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2719 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2723 * Note that we ignore the append flag as append does not
2724 * mean the same thing under DOS and Unix.
2727 flags = calculate_open_access_flags(access_mask, private_flags);
2730 * Currently we only look at FILE_WRITE_THROUGH for create options.
2734 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2739 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2743 if (!posix_open && !CAN_WRITE(conn)) {
2745 * We should really return a permission denied error if either
2746 * O_CREAT or O_TRUNC are set, but for compatibility with
2747 * older versions of Samba we just AND them out.
2749 flags2 &= ~(O_CREAT|O_TRUNC);
2752 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2754 * With kernel oplocks the open breaking an oplock
2755 * blocks until the oplock holder has given up the
2756 * oplock or closed the file. We prevent this by first
2757 * trying to open the file with O_NONBLOCK (see "man
2758 * fcntl" on Linux). For the second try, triggered by
2759 * an oplock break response, we do not need this
2762 * This is true under the assumption that only Samba
2763 * requests kernel oplocks. Once someone else like
2764 * NFSv4 starts to use that API, we will have to
2765 * modify this by communicating with the NFSv4 server.
2767 flags2 |= O_NONBLOCK;
2771 * Ensure we can't write on a read-only share or file.
2774 if (flags != O_RDONLY && file_existed &&
2775 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2776 DEBUG(5,("open_file_ntcreate: write access requested for "
2777 "file %s on read only %s\n",
2778 smb_fname_str_dbg(smb_fname),
2779 !CAN_WRITE(conn) ? "share" : "file" ));
2781 return NT_STATUS_ACCESS_DENIED;
2784 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2785 fsp->share_access = share_access;
2786 fsp->fh->private_options = private_flags;
2787 fsp->access_mask = open_access_mask; /* We change this to the
2788 * requested access_mask after
2789 * the open is done. */
2791 fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
2794 if (timeval_is_zero(&request_time)) {
2795 request_time = fsp->open_time;
2799 * Ensure we pay attention to default ACLs on directories if required.
2802 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2803 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2804 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2807 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2808 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2809 (unsigned int)flags, (unsigned int)flags2,
2810 (unsigned int)unx_mode, (unsigned int)access_mask,
2811 (unsigned int)open_access_mask));
2813 fsp_open = open_file(fsp, conn, req, parent_dir,
2814 flags|flags2, unx_mode, access_mask,
2815 open_access_mask, &new_file_created);
2817 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2821 * This handles the kernel oplock case:
2823 * the file has an active kernel oplock and the open() returned
2824 * EWOULDBLOCK/EAGAIN which maps to NETWORK_BUSY.
2826 * "Samba locking.tdb oplocks" are handled below after acquiring
2827 * the sharemode lock with get_share_mode_lock().
2829 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2830 DEBUG(10, ("FIFO busy\n"));
2831 return NT_STATUS_NETWORK_BUSY;
2834 DEBUG(10, ("Internal open busy\n"));
2835 return NT_STATUS_NETWORK_BUSY;
2839 * From here on we assume this is an oplock break triggered
2842 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2844 defer_open(NULL, request_time, timeval_set(0, 0),
2845 req, false, false, fsp->file_id);
2846 DEBUG(10, ("No share mode lock found after "
2847 "EWOULDBLOCK, retrying sync\n"));
2848 return NT_STATUS_SHARING_VIOLATION;
2851 if (!validate_oplock_types(lck)) {
2852 smb_panic("validate_oplock_types failed");
2855 delay = delay_for_oplock(fsp, 0, lease, lck, false,
2857 first_open_attempt);
2859 schedule_defer_open(lck, fsp->file_id, request_time,
2862 DEBUG(10, ("Sent oplock break request to kernel "
2863 "oplock holder\n"));
2864 return NT_STATUS_SHARING_VIOLATION;
2868 * No oplock from Samba around. Immediately retry with
2871 defer_open(lck, request_time, timeval_set(0, 0), req,
2872 false, false, fsp->file_id);
2875 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2876 "Retrying sync\n"));
2877 return NT_STATUS_SHARING_VIOLATION;
2880 if (!NT_STATUS_IS_OK(fsp_open)) {
2881 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2882 schedule_async_open(request_time, req);
2887 if (new_file_created) {
2889 * As we atomically create using O_CREAT|O_EXCL,
2890 * then if new_file_created is true, then
2891 * file_existed *MUST* have been false (even
2892 * if the file was previously detected as being
2895 file_existed = false;
2898 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2900 * The file did exist, but some other (local or NFS)
2901 * process either renamed/unlinked and re-created the
2902 * file with different dev/ino after we walked the path,
2903 * but before we did the open. We could retry the
2904 * open but it's a rare enough case it's easier to
2905 * just fail the open to prevent creating any problems
2906 * in the open file db having the wrong dev/ino key.
2909 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2910 "Old (dev=0x%llu, ino =0x%llu). "
2911 "New (dev=0x%llu, ino=0x%llu). Failing open "
2912 " with NT_STATUS_ACCESS_DENIED.\n",
2913 smb_fname_str_dbg(smb_fname),
2914 (unsigned long long)saved_stat.st_ex_dev,
2915 (unsigned long long)saved_stat.st_ex_ino,
2916 (unsigned long long)smb_fname->st.st_ex_dev,
2917 (unsigned long long)smb_fname->st.st_ex_ino));
2918 return NT_STATUS_ACCESS_DENIED;
2921 old_write_time = smb_fname->st.st_ex_mtime;
2924 * Deal with the race condition where two smbd's detect the
2925 * file doesn't exist and do the create at the same time. One
2926 * of them will win and set a share mode, the other (ie. this
2927 * one) should check if the requested share mode for this
2928 * create is allowed.
2932 * Now the file exists and fsp is successfully opened,
2933 * fsp->dev and fsp->inode are valid and should replace the
2934 * dev=0,inode=0 from a non existent file. Spotted by
2935 * Nadav Danieli <nadavd@exanet.com>. JRA.
2940 lck = get_share_mode_lock(talloc_tos(), id,
2942 smb_fname, &old_write_time);
2945 DEBUG(0, ("open_file_ntcreate: Could not get share "
2946 "mode lock for %s\n",
2947 smb_fname_str_dbg(smb_fname)));
2949 return NT_STATUS_SHARING_VIOLATION;
2952 /* Get the types we need to examine. */
2953 if (!validate_oplock_types(lck)) {
2954 smb_panic("validate_oplock_types failed");
2957 if (has_delete_on_close(lck, fsp->name_hash)) {
2960 return NT_STATUS_DELETE_PENDING;
2963 status = open_mode_check(conn, lck,
2964 access_mask, share_access);
2966 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION) ||
2967 (lck->data->num_share_modes > 0)) {
2969 * This comes from ancient times out of open_mode_check. I
2970 * have no clue whether this is still necessary. I can't think
2971 * of a case where this would actually matter further down in
2972 * this function. I leave it here for further investigation
2975 file_existed = true;
2980 * Handle oplocks, deferring the request if delay_for_oplock()
2981 * triggered a break message and we have to wait for the break
2985 bool sharing_violation = NT_STATUS_EQUAL(
2986 status, NT_STATUS_SHARING_VIOLATION);
2988 delay = delay_for_oplock(fsp, oplock_request, lease, lck,
2991 first_open_attempt);
2993 schedule_defer_open(lck, fsp->file_id,
2997 return NT_STATUS_SHARING_VIOLATION;
3001 if (!NT_STATUS_IS_OK(status)) {
3002 uint32_t can_access_mask;
3003 bool can_access = True;
3005 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
3007 /* Check if this can be done with the deny_dos and fcb
3010 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
3011 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
3013 DEBUG(0, ("DOS open without an SMB "
3017 return NT_STATUS_INTERNAL_ERROR;
3020 /* Use the client requested access mask here,
3021 * not the one we open with. */
3022 status = fcb_or_dos_open(req,
3033 if (NT_STATUS_IS_OK(status)) {
3036 *pinfo = FILE_WAS_OPENED;
3038 return NT_STATUS_OK;
3043 * This next line is a subtlety we need for
3044 * MS-Access. If a file open will fail due to share
3045 * permissions and also for security (access) reasons,
3046 * we need to return the access failed error, not the
3047 * share error. We can't open the file due to kernel
3048 * oplock deadlock (it's possible we failed above on
3049 * the open_mode_check()) so use a userspace check.
3052 if (flags & O_RDWR) {
3053 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
3054 } else if (flags & O_WRONLY) {
3055 can_access_mask = FILE_WRITE_DATA;
3057 can_access_mask = FILE_READ_DATA;
3060 if (((can_access_mask & FILE_WRITE_DATA) &&
3061 !CAN_WRITE(conn)) ||
3062 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
3065 can_access_mask))) {
3070 * If we're returning a share violation, ensure we
3071 * cope with the braindead 1 second delay (SMB1 only).
3074 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
3075 !conn->sconn->using_smb2 &&
3076 lp_defer_sharing_violations()) {
3077 struct timeval timeout;
3080 /* this is a hack to speed up torture tests
3082 timeout_usecs = lp_parm_int(SNUM(conn),
3083 "smbd","sharedelay",
3084 SHARING_VIOLATION_USEC_WAIT);
3086 /* This is a relative time, added to the absolute
3087 request_time value to get the absolute timeout time.
3088 Note that if this is the second or greater time we enter
3089 this codepath for this particular request mid then
3090 request_time is left as the absolute time of the *first*
3091 time this request mid was processed. This is what allows
3092 the request to eventually time out. */
3094 timeout = timeval_set(0, timeout_usecs);
3096 if (!request_timed_out(request_time, timeout)) {
3097 defer_open(lck, request_time, timeout, req,
3106 * We have detected a sharing violation here
3107 * so return the correct error code
3109 status = NT_STATUS_SHARING_VIOLATION;
3111 status = NT_STATUS_ACCESS_DENIED;
3116 /* Should we atomically (to the client at least) truncate ? */
3117 if ((!new_file_created) &&
3118 (flags2 & O_TRUNC) &&
3119 (!S_ISFIFO(fsp->fsp_name->st.st_ex_mode))) {
3122 ret = vfs_set_filelen(fsp, 0);
3124 status = map_nt_error_from_unix(errno);
3132 * We have the share entry *locked*.....
3135 /* Delete streams if create_disposition requires it */
3136 if (!new_file_created && clear_ads(create_disposition) &&
3137 !is_ntfs_stream_smb_fname(smb_fname)) {
3138 status = delete_all_streams(conn, smb_fname);
3139 if (!NT_STATUS_IS_OK(status)) {
3146 /* note that we ignore failure for the following. It is
3147 basically a hack for NFS, and NFS will never set one of
3148 these only read them. Nobody but Samba can ever set a deny
3149 mode and we have already checked our more authoritative
3150 locking database for permission to set this deny mode. If
3151 the kernel refuses the operations then the kernel is wrong.
3152 note that GPFS supports it as well - jmcd */
3154 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
3157 * Beware: streams implementing VFS modules may
3158 * implement streams in a way that fsp will have the
3159 * basefile open in the fsp fd, so lacking a distinct
3160 * fd for the stream kernel_flock will apply on the
3161 * basefile which is wrong. The actual check is
3162 * deffered to the VFS module implementing the
3163 * kernel_flock call.
3165 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
3166 if(ret_flock == -1 ){
3171 return NT_STATUS_SHARING_VIOLATION;
3174 fsp->kernel_share_modes_taken = true;
3178 * At this point onwards, we can guarantee that the share entry
3179 * is locked, whether we created the file or not, and that the
3180 * deny mode is compatible with all current opens.
3184 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3185 * but we don't have to store this - just ignore it on access check.
3187 if (conn->sconn->using_smb2) {
3189 * SMB2 doesn't return it (according to Microsoft tests).
3190 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
3191 * File created with access = 0x7 (Read, Write, Delete)
3192 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
3194 fsp->access_mask = access_mask;
3196 /* But SMB1 does. */
3197 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3202 * stat opens on existing files don't get oplocks.
3203 * They can get leases.
3205 * Note that we check for stat open on the *open_access_mask*,
3206 * i.e. the access mask we actually used to do the open,
3207 * not the one the client asked for (which is in
3208 * fsp->access_mask). This is due to the fact that
3209 * FILE_OVERWRITE and FILE_OVERWRITE_IF add in O_TRUNC,
3210 * which adds FILE_WRITE_DATA to open_access_mask.
3212 if (is_stat_open(open_access_mask) && lease == NULL) {
3213 oplock_request = NO_OPLOCK;
3217 if (new_file_created) {
3218 info = FILE_WAS_CREATED;
3220 if (flags2 & O_TRUNC) {
3221 info = FILE_WAS_OVERWRITTEN;
3223 info = FILE_WAS_OPENED;
3232 * Setup the oplock info in both the shared memory and
3235 status = grant_fsp_oplock_type(req, fsp, lck, oplock_request, lease);
3236 if (!NT_STATUS_IS_OK(status)) {
3242 /* Handle strange delete on close create semantics. */
3243 if (create_options & FILE_DELETE_ON_CLOSE) {
3245 status = can_set_delete_on_close(fsp, new_dos_attributes);
3247 if (!NT_STATUS_IS_OK(status)) {
3248 /* Remember to delete the mode we just added. */
3249 del_share_mode(lck, fsp);
3254 /* Note that here we set the *inital* delete on close flag,
3255 not the regular one. The magic gets handled in close. */
3256 fsp->initial_delete_on_close = True;
3259 if (info != FILE_WAS_OPENED) {
3260 /* Overwritten files should be initially set as archive */
3261 if ((info == FILE_WAS_OVERWRITTEN && lp_map_archive(SNUM(conn))) ||
3262 lp_store_dos_attributes(SNUM(conn))) {
3264 if (file_set_dosmode(conn, smb_fname,
3265 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
3266 parent_dir, true) == 0) {
3267 unx_mode = smb_fname->st.st_ex_mode;
3273 /* Determine sparse flag. */
3275 /* POSIX opens are sparse by default. */
3276 fsp->is_sparse = true;
3278 fsp->is_sparse = (file_existed &&
3279 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
3283 * Take care of inherited ACLs on created files - if default ACL not
3287 if (!posix_open && new_file_created && !def_acl) {
3289 int saved_errno = errno; /* We might get ENOSYS in the next
3292 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
3294 errno = saved_errno; /* Ignore ENOSYS */
3297 } else if (new_unx_mode) {
3301 /* Attributes need changing. File already existed. */
3304 int saved_errno = errno; /* We might get ENOSYS in the
3306 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
3308 if (ret == -1 && errno == ENOSYS) {
3309 errno = saved_errno; /* Ignore ENOSYS */
3311 DEBUG(5, ("open_file_ntcreate: reset "
3312 "attributes of file %s to 0%o\n",
3313 smb_fname_str_dbg(smb_fname),
3314 (unsigned int)new_unx_mode));
3315 ret = 0; /* Don't do the fchmod below. */
3320 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
3321 DEBUG(5, ("open_file_ntcreate: failed to reset "
3322 "attributes of file %s to 0%o\n",
3323 smb_fname_str_dbg(smb_fname),
3324 (unsigned int)new_unx_mode));
3329 * Deal with other opens having a modified write time.
3331 struct timespec write_time = get_share_mode_write_time(lck);
3333 if (!null_timespec(write_time)) {
3334 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3340 return NT_STATUS_OK;
3343 static NTSTATUS mkdir_internal(connection_struct *conn,
3344 struct smb_filename *smb_dname,
3345 uint32_t file_attributes)
3348 char *parent_dir = NULL;
3350 bool posix_open = false;
3351 bool need_re_stat = false;
3352 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
3354 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
3355 DEBUG(5,("mkdir_internal: failing share access "
3356 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
3357 return NT_STATUS_ACCESS_DENIED;
3360 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
3362 return NT_STATUS_NO_MEMORY;
3365 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3367 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
3369 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
3372 status = check_parent_access(conn,
3375 if(!NT_STATUS_IS_OK(status)) {
3376 DEBUG(5,("mkdir_internal: check_parent_access "
3377 "on directory %s for path %s returned %s\n",
3379 smb_dname->base_name,
3380 nt_errstr(status) ));
3384 if (SMB_VFS_MKDIR(conn, smb_dname, mode) != 0) {
3385 return map_nt_error_from_unix(errno);
3388 /* Ensure we're checking for a symlink here.... */
3389 /* We don't want to get caught by a symlink racer. */
3391 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3392 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3393 smb_fname_str_dbg(smb_dname), strerror(errno)));
3394 return map_nt_error_from_unix(errno);
3397 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
3398 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3399 smb_fname_str_dbg(smb_dname)));
3400 return NT_STATUS_NOT_A_DIRECTORY;
3403 if (lp_store_dos_attributes(SNUM(conn))) {
3405 file_set_dosmode(conn, smb_dname,
3406 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
3411 if (lp_inherit_permissions(SNUM(conn))) {
3412 inherit_access_posix_acl(conn, parent_dir,
3413 smb_dname->base_name, mode);
3414 need_re_stat = true;
3419 * Check if high bits should have been set,
3420 * then (if bits are missing): add them.
3421 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3424 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
3425 (mode & ~smb_dname->st.st_ex_mode)) {
3426 SMB_VFS_CHMOD(conn, smb_dname,
3427 (smb_dname->st.st_ex_mode |
3428 (mode & ~smb_dname->st.st_ex_mode)));
3429 need_re_stat = true;
3433 /* Change the owner if required. */
3434 if (lp_inherit_owner(SNUM(conn)) != INHERIT_OWNER_NO) {
3435 change_dir_owner_to_parent(conn, parent_dir,
3436 smb_dname->base_name,
3438 need_re_stat = true;
3442 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
3443 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3444 smb_fname_str_dbg(smb_dname), strerror(errno)));
3445 return map_nt_error_from_unix(errno);
3449 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
3450 smb_dname->base_name);
3452 return NT_STATUS_OK;
3455 /****************************************************************************
3456 Open a directory from an NT SMB call.
3457 ****************************************************************************/
3459 static NTSTATUS open_directory(connection_struct *conn,
3460 struct smb_request *req,
3461 struct smb_filename *smb_dname,
3462 uint32_t access_mask,
3463 uint32_t share_access,
3464 uint32_t create_disposition,
3465 uint32_t create_options,
3466 uint32_t file_attributes,
3468 files_struct **result)
3470 files_struct *fsp = NULL;
3471 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
3472 struct share_mode_lock *lck = NULL;
3474 struct timespec mtimespec;
3478 if (is_ntfs_stream_smb_fname(smb_dname)) {
3479 DEBUG(2, ("open_directory: %s is a stream name!\n",
3480 smb_fname_str_dbg(smb_dname)));
3481 return NT_STATUS_NOT_A_DIRECTORY;
3484 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
3485 /* Ensure we have a directory attribute. */
3486 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
3489 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3490 "share_access = 0x%x create_options = 0x%x, "
3491 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3492 smb_fname_str_dbg(smb_dname),
3493 (unsigned int)access_mask,
3494 (unsigned int)share_access,
3495 (unsigned int)create_options,
3496 (unsigned int)create_disposition,
3497 (unsigned int)file_attributes));
3499 status = smbd_calculate_access_mask(conn, smb_dname, false,
3500 access_mask, &access_mask);
3501 if (!NT_STATUS_IS_OK(status)) {
3502 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3503 "on file %s returned %s\n",
3504 smb_fname_str_dbg(smb_dname),
3505 nt_errstr(status)));
3509 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3510 !security_token_has_privilege(get_current_nttok(conn),
3511 SEC_PRIV_SECURITY)) {
3512 DEBUG(10, ("open_directory: open on %s "
3513 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3514 smb_fname_str_dbg(smb_dname)));
3515 return NT_STATUS_PRIVILEGE_NOT_HELD;
3518 switch( create_disposition ) {
3522 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3525 info = FILE_WAS_OPENED;
3530 /* If directory exists error. If directory doesn't
3534 status = NT_STATUS_OBJECT_NAME_COLLISION;
3535 DEBUG(2, ("open_directory: unable to create "
3536 "%s. Error was %s\n",
3537 smb_fname_str_dbg(smb_dname),
3538 nt_errstr(status)));
3542 status = mkdir_internal(conn, smb_dname,
3545 if (!NT_STATUS_IS_OK(status)) {
3546 DEBUG(2, ("open_directory: unable to create "
3547 "%s. Error was %s\n",
3548 smb_fname_str_dbg(smb_dname),
3549 nt_errstr(status)));
3553 info = FILE_WAS_CREATED;
3558 * If directory exists open. If directory doesn't
3563 status = NT_STATUS_OK;
3564 info = FILE_WAS_OPENED;
3566 status = mkdir_internal(conn, smb_dname,
3569 if (NT_STATUS_IS_OK(status)) {
3570 info = FILE_WAS_CREATED;
3572 /* Cope with create race. */
3573 if (!NT_STATUS_EQUAL(status,
3574 NT_STATUS_OBJECT_NAME_COLLISION)) {
3575 DEBUG(2, ("open_directory: unable to create "
3576 "%s. Error was %s\n",
3577 smb_fname_str_dbg(smb_dname),
3578 nt_errstr(status)));
3583 * If mkdir_internal() returned
3584 * NT_STATUS_OBJECT_NAME_COLLISION
3585 * we still must lstat the path.
3588 if (SMB_VFS_LSTAT(conn, smb_dname)
3590 DEBUG(2, ("Could not stat "
3591 "directory '%s' just "
3596 return map_nt_error_from_unix(
3600 info = FILE_WAS_OPENED;
3606 case FILE_SUPERSEDE:
3607 case FILE_OVERWRITE:
3608 case FILE_OVERWRITE_IF:
3610 DEBUG(5,("open_directory: invalid create_disposition "
3611 "0x%x for directory %s\n",
3612 (unsigned int)create_disposition,
3613 smb_fname_str_dbg(smb_dname)));
3614 return NT_STATUS_INVALID_PARAMETER;
3617 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3618 DEBUG(5,("open_directory: %s is not a directory !\n",
3619 smb_fname_str_dbg(smb_dname)));
3620 return NT_STATUS_NOT_A_DIRECTORY;
3623 if (info == FILE_WAS_OPENED) {
3624 status = smbd_check_access_rights(conn,
3628 if (!NT_STATUS_IS_OK(status)) {
3629 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3630 "file %s failed with %s\n",
3631 smb_fname_str_dbg(smb_dname),
3632 nt_errstr(status)));
3637 status = file_new(req, conn, &fsp);
3638 if(!NT_STATUS_IS_OK(status)) {
3643 * Setup the files_struct for it.
3646 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3647 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3648 fsp->file_pid = req ? req->smbpid : 0;
3649 fsp->can_lock = False;
3650 fsp->can_read = False;
3651 fsp->can_write = False;
3653 fsp->share_access = share_access;
3654 fsp->fh->private_options = 0;
3656 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3658 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3659 fsp->print_file = NULL;
3660 fsp->modified = False;
3661 fsp->oplock_type = NO_OPLOCK;
3662 fsp->sent_oplock_break = NO_BREAK_SENT;
3663 fsp->is_directory = True;
3664 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3665 fsp->posix_flags |= FSP_POSIX_FLAGS_ALL;
3667 status = fsp_set_smb_fname(fsp, smb_dname);
3668 if (!NT_STATUS_IS_OK(status)) {
3669 file_free(req, fsp);
3673 /* Don't store old timestamps for directory
3674 handles in the internal database. We don't
3675 update them in there if new objects
3676 are creaded in the directory. Currently
3677 we only update timestamps on file writes.
3680 ZERO_STRUCT(mtimespec);
3682 if (access_mask & (FILE_LIST_DIRECTORY|
3684 FILE_ADD_SUBDIRECTORY|
3687 FILE_DELETE_CHILD)) {
3689 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3691 /* POSIX allows us to open a directory with O_RDONLY. */
3692 status = fd_open(conn, fsp, O_RDONLY, 0);
3694 if (!NT_STATUS_IS_OK(status)) {
3695 DEBUG(5, ("open_directory: Could not open fd for "
3697 smb_fname_str_dbg(smb_dname),
3698 nt_errstr(status)));
3699 file_free(req, fsp);
3704 DEBUG(10, ("Not opening Directory %s\n",
3705 smb_fname_str_dbg(smb_dname)));
3708 status = vfs_stat_fsp(fsp);
3709 if (!NT_STATUS_IS_OK(status)) {
3711 file_free(req, fsp);
3715 if(!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3716 DEBUG(5,("open_directory: %s is not a directory !\n",
3717 smb_fname_str_dbg(smb_dname)));
3719 file_free(req, fsp);
3720 return NT_STATUS_NOT_A_DIRECTORY;
3723 /* Ensure there was no race condition. We need to check
3724 * dev/inode but not permissions, as these can change
3726 if (!check_same_dev_ino(&smb_dname->st, &fsp->fsp_name->st)) {
3727 DEBUG(5,("open_directory: stat struct differs for "
3729 smb_fname_str_dbg(smb_dname)));
3731 file_free(req, fsp);
3732 return NT_STATUS_ACCESS_DENIED;
3735 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3736 conn->connectpath, smb_dname,
3740 DEBUG(0, ("open_directory: Could not get share mode lock for "
3741 "%s\n", smb_fname_str_dbg(smb_dname)));
3743 file_free(req, fsp);
3744 return NT_STATUS_SHARING_VIOLATION;
3747 if (has_delete_on_close(lck, fsp->name_hash)) {
3750 file_free(req, fsp);
3751 return NT_STATUS_DELETE_PENDING;
3754 status = open_mode_check(conn, lck,
3755 access_mask, share_access);
3757 if (!NT_STATUS_IS_OK(status)) {
3760 file_free(req, fsp);
3764 ok = set_share_mode(lck, fsp, get_current_uid(conn),
3765 req ? req->mid : 0, NO_OPLOCK,
3770 file_free(req, fsp);
3771 return NT_STATUS_NO_MEMORY;
3774 /* For directories the delete on close bit at open time seems
3775 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3776 if (create_options & FILE_DELETE_ON_CLOSE) {
3777 status = can_set_delete_on_close(fsp, 0);
3778 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3779 del_share_mode(lck, fsp);
3782 file_free(req, fsp);
3786 if (NT_STATUS_IS_OK(status)) {
3787 /* Note that here we set the *inital* delete on close flag,
3788 not the regular one. The magic gets handled in close. */
3789 fsp->initial_delete_on_close = True;
3795 * Deal with other opens having a modified write time. Is this
3796 * possible for directories?
3798 struct timespec write_time = get_share_mode_write_time(lck);
3800 if (!null_timespec(write_time)) {
3801 update_stat_ex_mtime(&fsp->fsp_name->st, write_time);
3812 return NT_STATUS_OK;
3815 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3816 struct smb_filename *smb_dname)
3821 status = SMB_VFS_CREATE_FILE(
3824 0, /* root_dir_fid */
3825 smb_dname, /* fname */
3826 FILE_READ_ATTRIBUTES, /* access_mask */
3827 FILE_SHARE_NONE, /* share_access */
3828 FILE_CREATE, /* create_disposition*/
3829 FILE_DIRECTORY_FILE, /* create_options */
3830 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3831 0, /* oplock_request */
3833 0, /* allocation_size */
3834 0, /* private_flags */
3839 NULL, NULL); /* create context */
3841 if (NT_STATUS_IS_OK(status)) {
3842 close_file(req, fsp, NORMAL_CLOSE);
3848 /****************************************************************************
3849 Receive notification that one of our open files has been renamed by another
3851 ****************************************************************************/
3853 void msg_file_was_renamed(struct messaging_context *msg,
3856 struct server_id server_id,
3860 char *frm = (char *)data->data;
3862 const char *sharepath;
3863 const char *base_name;
3864 const char *stream_name;
3865 struct smb_filename *smb_fname = NULL;
3866 size_t sp_len, bn_len;
3868 struct smbd_server_connection *sconn =
3869 talloc_get_type_abort(private_data,
3870 struct smbd_server_connection);
3872 if (data->data == NULL
3873 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3874 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3875 (int)data->length));
3879 /* Unpack the message. */
3880 pull_file_id_24(frm, &id);
3881 sharepath = &frm[24];
3882 sp_len = strlen(sharepath);
3883 base_name = sharepath + sp_len + 1;
3884 bn_len = strlen(base_name);
3885 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3887 /* stream_name must always be NULL if there is no stream. */
3888 if (stream_name[0] == '\0') {
3892 smb_fname = synthetic_smb_fname(talloc_tos(),
3897 if (smb_fname == NULL) {
3901 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3903 sharepath, smb_fname_str_dbg(smb_fname),
3904 file_id_string_tos(&id)));
3906 for(fsp = file_find_di_first(sconn, id); fsp;
3907 fsp = file_find_di_next(fsp)) {
3908 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3910 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3911 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3912 smb_fname_str_dbg(smb_fname)));
3913 status = fsp_set_smb_fname(fsp, smb_fname);
3914 if (!NT_STATUS_IS_OK(status)) {
3919 /* Now we have the complete path we can work out if this is
3920 actually within this share and adjust newname accordingly. */
3921 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3922 "not sharepath %s) "
3923 "%s from %s -> %s\n",
3924 fsp->conn->connectpath,
3928 smb_fname_str_dbg(smb_fname)));
3932 TALLOC_FREE(smb_fname);
3937 * If a main file is opened for delete, all streams need to be checked for
3938 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3939 * If that works, delete them all by setting the delete on close and close.
3942 static NTSTATUS open_streams_for_delete(connection_struct *conn,
3943 const struct smb_filename *smb_fname)
3945 struct stream_struct *stream_info = NULL;
3946 files_struct **streams = NULL;
3948 unsigned int num_streams = 0;
3949 TALLOC_CTX *frame = talloc_stackframe();
3952 status = vfs_streaminfo(conn, NULL, smb_fname, talloc_tos(),
3953 &num_streams, &stream_info);
3955 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3956 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3957 DEBUG(10, ("no streams around\n"));
3959 return NT_STATUS_OK;
3962 if (!NT_STATUS_IS_OK(status)) {
3963 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3964 nt_errstr(status)));
3968 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3971 if (num_streams == 0) {
3973 return NT_STATUS_OK;
3976 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3977 if (streams == NULL) {
3978 DEBUG(0, ("talloc failed\n"));
3979 status = NT_STATUS_NO_MEMORY;
3983 for (i=0; i<num_streams; i++) {
3984 struct smb_filename *smb_fname_cp;
3986 if (strequal(stream_info[i].name, "::$DATA")) {
3991 smb_fname_cp = synthetic_smb_fname(talloc_tos(),
3992 smb_fname->base_name,
3993 stream_info[i].name,
3996 ~SMB_FILENAME_POSIX_PATH));
3997 if (smb_fname_cp == NULL) {
3998 status = NT_STATUS_NO_MEMORY;
4002 if (SMB_VFS_STAT(conn, smb_fname_cp) == -1) {
4003 DEBUG(10, ("Unable to stat stream: %s\n",
4004 smb_fname_str_dbg(smb_fname_cp)));
4007 status = SMB_VFS_CREATE_FILE(
4010 0, /* root_dir_fid */
4011 smb_fname_cp, /* fname */
4012 DELETE_ACCESS, /* access_mask */
4013 (FILE_SHARE_READ | /* share_access */
4014 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
4015 FILE_OPEN, /* create_disposition*/
4016 0, /* create_options */
4017 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
4018 0, /* oplock_request */
4020 0, /* allocation_size */
4021 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
4024 &streams[i], /* result */
4026 NULL, NULL); /* create context */
4028 if (!NT_STATUS_IS_OK(status)) {
4029 DEBUG(10, ("Could not open stream %s: %s\n",
4030 smb_fname_str_dbg(smb_fname_cp),
4031 nt_errstr(status)));
4033 TALLOC_FREE(smb_fname_cp);
4036 TALLOC_FREE(smb_fname_cp);
4040 * don't touch the variable "status" beyond this point :-)
4043 for (i -= 1 ; i >= 0; i--) {
4044 if (streams[i] == NULL) {
4048 DEBUG(10, ("Closing stream # %d, %s\n", i,
4049 fsp_str_dbg(streams[i])));
4050 close_file(NULL, streams[i], NORMAL_CLOSE);
4058 /*********************************************************************
4059 Create a default ACL by inheriting from the parent. If no inheritance
4060 from the parent available, don't set anything. This will leave the actual
4061 permissions the new file or directory already got from the filesystem
4062 as the NT ACL when read.
4063 *********************************************************************/
4065 static NTSTATUS inherit_new_acl(files_struct *fsp)
4067 TALLOC_CTX *frame = talloc_stackframe();
4068 char *parent_name = NULL;
4069 struct security_descriptor *parent_desc = NULL;
4070 NTSTATUS status = NT_STATUS_OK;
4071 struct security_descriptor *psd = NULL;
4072 const struct dom_sid *owner_sid = NULL;
4073 const struct dom_sid *group_sid = NULL;
4074 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
4075 struct security_token *token = fsp->conn->session_info->security_token;
4076 bool inherit_owner =
4077 (lp_inherit_owner(SNUM(fsp->conn)) == INHERIT_OWNER_WINDOWS_AND_UNIX);
4078 bool inheritable_components = false;
4079 bool try_builtin_administrators = false;
4080 const struct dom_sid *BA_U_sid = NULL;
4081 const struct dom_sid *BA_G_sid = NULL;
4082 bool try_system = false;
4083 const struct dom_sid *SY_U_sid = NULL;
4084 const struct dom_sid *SY_G_sid = NULL;
4086 struct smb_filename *parent_smb_fname = NULL;
4088 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
4090 return NT_STATUS_NO_MEMORY;
4092 parent_smb_fname = synthetic_smb_fname(talloc_tos(),
4096 fsp->fsp_name->flags);
4098 if (parent_smb_fname == NULL) {
4100 return NT_STATUS_NO_MEMORY;
4103 status = SMB_VFS_GET_NT_ACL(fsp->conn,
4105 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
4108 if (!NT_STATUS_IS_OK(status)) {
4113 inheritable_components = sd_has_inheritable_components(parent_desc,
4116 if (!inheritable_components && !inherit_owner) {
4118 /* Nothing to inherit and not setting owner. */
4119 return NT_STATUS_OK;
4122 /* Create an inherited descriptor from the parent. */
4124 if (DEBUGLEVEL >= 10) {
4125 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
4126 fsp_str_dbg(fsp) ));
4127 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
4130 /* Inherit from parent descriptor if "inherit owner" set. */
4131 if (inherit_owner) {
4132 owner_sid = parent_desc->owner_sid;
4133 group_sid = parent_desc->group_sid;
4136 if (owner_sid == NULL) {
4137 if (security_token_has_builtin_administrators(token)) {
4138 try_builtin_administrators = true;
4139 } else if (security_token_is_system(token)) {
4140 try_builtin_administrators = true;
4145 if (group_sid == NULL &&
4146 token->num_sids == PRIMARY_GROUP_SID_INDEX)
4148 if (security_token_is_system(token)) {
4149 try_builtin_administrators = true;
4154 if (try_builtin_administrators) {
4159 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
4163 BA_U_sid = &global_sid_Builtin_Administrators;
4164 BA_G_sid = &global_sid_Builtin_Administrators;
4167 BA_U_sid = &global_sid_Builtin_Administrators;
4170 BA_G_sid = &global_sid_Builtin_Administrators;
4183 ok = sids_to_unixids(&global_sid_System, 1, &ids);
4187 SY_U_sid = &global_sid_System;
4188 SY_G_sid = &global_sid_System;
4191 SY_U_sid = &global_sid_System;
4194 SY_G_sid = &global_sid_System;
4202 if (owner_sid == NULL) {
4203 owner_sid = BA_U_sid;
4206 if (owner_sid == NULL) {
4207 owner_sid = SY_U_sid;
4210 if (group_sid == NULL) {
4211 group_sid = SY_G_sid;
4214 if (try_system && group_sid == NULL) {
4215 group_sid = BA_G_sid;
4218 if (owner_sid == NULL) {
4219 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
4221 if (group_sid == NULL) {
4222 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
4223 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
4225 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
4229 status = se_create_child_secdesc(frame,
4236 if (!NT_STATUS_IS_OK(status)) {
4241 /* If inheritable_components == false,
4242 se_create_child_secdesc()
4243 creates a security desriptor with a NULL dacl
4244 entry, but with SEC_DESC_DACL_PRESENT. We need
4245 to remove that flag. */
4247 if (!inheritable_components) {
4248 security_info_sent &= ~SECINFO_DACL;
4249 psd->type &= ~SEC_DESC_DACL_PRESENT;
4252 if (DEBUGLEVEL >= 10) {
4253 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
4254 fsp_str_dbg(fsp) ));
4255 NDR_PRINT_DEBUG(security_descriptor, psd);
4258 if (inherit_owner) {
4259 /* We need to be root to force this. */
4262 status = SMB_VFS_FSET_NT_ACL(fsp,
4265 if (inherit_owner) {
4273 * If we already have a lease, it must match the new file id. [MS-SMB2]
4274 * 3.3.5.9.8 speaks about INVALID_PARAMETER if an already used lease key is
4275 * used for a different file name.
4278 struct lease_match_state {
4279 /* Input parameters. */
4280 TALLOC_CTX *mem_ctx;
4281 const char *servicepath;
4282 const struct smb_filename *fname;
4285 /* Return parameters. */
4286 uint32_t num_file_ids;
4287 struct file_id *ids;
4288 NTSTATUS match_status;
4291 /*************************************************************
4292 File doesn't exist but this lease key+guid is already in use.
4294 This is only allowable in the dynamic share case where the
4295 service path must be different.
4297 There is a small race condition here in the multi-connection
4298 case where a client sends two create calls on different connections,
4299 where the file doesn't exist and one smbd creates the leases_db
4300 entry first, but this will get fixed by the multichannel cleanup
4301 when all identical client_guids get handled by a single smbd.
4302 **************************************************************/
4304 static void lease_match_parser_new_file(
4306 const struct leases_db_file *files,
4307 struct lease_match_state *state)
4311 for (i = 0; i < num_files; i++) {
4312 const struct leases_db_file *f = &files[i];
4313 if (strequal(state->servicepath, f->servicepath)) {
4314 state->match_status = NT_STATUS_INVALID_PARAMETER;
4319 /* Dynamic share case. Break leases on all other files. */
4320 state->match_status = leases_db_copy_file_ids(state->mem_ctx,
4324 if (!NT_STATUS_IS_OK(state->match_status)) {
4328 state->num_file_ids = num_files;
4329 state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4333 static void lease_match_parser(
4335 const struct leases_db_file *files,
4338 struct lease_match_state *state =
4339 (struct lease_match_state *)private_data;
4342 if (!state->file_existed) {
4344 * Deal with name mismatch or
4345 * possible dynamic share case separately
4346 * to make code clearer.
4348 lease_match_parser_new_file(num_files,
4355 state->match_status = NT_STATUS_OK;
4357 for (i = 0; i < num_files; i++) {
4358 const struct leases_db_file *f = &files[i];
4360 /* Everything should be the same. */
4361 if (!file_id_equal(&state->id, &f->id)) {
4362 /* This should catch all dynamic share cases. */
4363 state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4366 if (!strequal(f->servicepath, state->servicepath)) {
4367 state->match_status = NT_STATUS_INVALID_PARAMETER;
4370 if (!strequal(f->base_name, state->fname->base_name)) {
4371 state->match_status = NT_STATUS_INVALID_PARAMETER;
4374 if (!strequal(f->stream_name, state->fname->stream_name)) {
4375 state->match_status = NT_STATUS_INVALID_PARAMETER;
4380 if (NT_STATUS_IS_OK(state->match_status)) {
4382 * Common case - just opening another handle on a
4383 * file on a non-dynamic share.
4388 if (NT_STATUS_EQUAL(state->match_status, NT_STATUS_INVALID_PARAMETER)) {
4389 /* Mismatched path. Error back to client. */
4394 * File id mismatch. Dynamic share case NT_STATUS_OPLOCK_NOT_GRANTED.
4395 * Don't allow leases.
4398 state->match_status = leases_db_copy_file_ids(state->mem_ctx,
4402 if (!NT_STATUS_IS_OK(state->match_status)) {
4406 state->num_file_ids = num_files;
4407 state->match_status = NT_STATUS_OPLOCK_NOT_GRANTED;
4411 static NTSTATUS lease_match(connection_struct *conn,
4412 struct smb_request *req,
4413 struct smb2_lease_key *lease_key,
4414 const char *servicepath,
4415 const struct smb_filename *fname,
4416 uint16_t *p_version,
4419 struct smbd_server_connection *sconn = req->sconn;
4420 TALLOC_CTX *tos = talloc_tos();
4421 struct lease_match_state state = {
4423 .servicepath = servicepath,
4425 .match_status = NT_STATUS_OK
4430 state.file_existed = VALID_STAT(fname->st);
4431 if (state.file_existed) {
4432 state.id = vfs_file_id_from_sbuf(conn, &fname->st);
4434 memset(&state.id, '\0', sizeof(state.id));
4437 status = leases_db_parse(&sconn->client->connections->smb2.client.guid,
4438 lease_key, lease_match_parser, &state);
4439 if (!NT_STATUS_IS_OK(status)) {
4441 * Not found or error means okay: We can make the lease pass
4443 return NT_STATUS_OK;
4445 if (!NT_STATUS_EQUAL(state.match_status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
4447 * Anything but NT_STATUS_OPLOCK_NOT_GRANTED, let the caller
4450 return state.match_status;
4453 /* We have to break all existing leases. */
4454 for (i = 0; i < state.num_file_ids; i++) {
4455 struct share_mode_lock *lck;
4456 struct share_mode_data *d;
4459 if (file_id_equal(&state.ids[i], &state.id)) {
4460 /* Don't need to break our own file. */
4464 lck = get_existing_share_mode_lock(talloc_tos(), state.ids[i]);
4466 /* Race condition - file already closed. */
4470 for (j=0; j<d->num_share_modes; j++) {
4471 struct share_mode_entry *e = &d->share_modes[j];
4472 uint32_t e_lease_type = get_lease_type(d, e);
4473 struct share_mode_lease *l = NULL;
4475 if (share_mode_stale_pid(d, j)) {
4479 if (e->op_type == LEASE_OPLOCK) {
4480 l = &lck->data->leases[e->lease_idx];
4481 if (!smb2_lease_key_equal(&l->lease_key,
4485 *p_epoch = l->epoch;
4486 *p_version = l->lease_version;
4489 if (e_lease_type == SMB2_LEASE_NONE) {
4493 send_break_message(conn->sconn->msg_ctx, e,
4497 * Windows 7 and 8 lease clients
4498 * are broken in that they will not
4499 * respond to lease break requests
4500 * whilst waiting for an outstanding
4501 * open request on that lease handle
4502 * on the same TCP connection, due
4503 * to holding an internal inode lock.
4505 * This means we can't reschedule
4506 * ourselves here, but must return
4511 * Send the breaks and then return
4512 * SMB2_LEASE_NONE in the lease handle
4513 * to cause them to acknowledge the
4514 * lease break. Consulatation with
4515 * Microsoft engineering confirmed
4516 * this approach is safe.
4523 * Ensure we don't grant anything more so we
4526 return NT_STATUS_OPLOCK_NOT_GRANTED;
4530 * Wrapper around open_file_ntcreate and open_directory
4533 static NTSTATUS create_file_unixpath(connection_struct *conn,
4534 struct smb_request *req,
4535 struct smb_filename *smb_fname,
4536 uint32_t access_mask,
4537 uint32_t share_access,
4538 uint32_t create_disposition,
4539 uint32_t create_options,
4540 uint32_t file_attributes,
4541 uint32_t oplock_request,
4542 struct smb2_lease *lease,
4543 uint64_t allocation_size,
4544 uint32_t private_flags,
4545 struct security_descriptor *sd,
4546 struct ea_list *ea_list,
4548 files_struct **result,
4551 int info = FILE_WAS_OPENED;
4552 files_struct *base_fsp = NULL;
4553 files_struct *fsp = NULL;
4556 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
4557 "file_attributes = 0x%x, share_access = 0x%x, "
4558 "create_disposition = 0x%x create_options = 0x%x "
4559 "oplock_request = 0x%x private_flags = 0x%x "
4560 "ea_list = 0x%p, sd = 0x%p, "
4562 (unsigned int)access_mask,
4563 (unsigned int)file_attributes,
4564 (unsigned int)share_access,
4565 (unsigned int)create_disposition,
4566 (unsigned int)create_options,
4567 (unsigned int)oplock_request,
4568 (unsigned int)private_flags,
4569 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4571 if (create_options & FILE_OPEN_BY_FILE_ID) {
4572 status = NT_STATUS_NOT_SUPPORTED;
4576 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
4577 status = NT_STATUS_INVALID_PARAMETER;
4582 oplock_request |= INTERNAL_OPEN_ONLY;
4585 if (lease != NULL) {
4586 uint16_t epoch = lease->lease_epoch;
4587 uint16_t version = lease->lease_version;
4588 status = lease_match(conn,
4595 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_NOT_GRANTED)) {
4596 /* Dynamic share file. No leases and update epoch... */
4597 lease->lease_state = SMB2_LEASE_NONE;
4598 lease->lease_epoch = epoch;
4599 lease->lease_version = version;
4600 } else if (!NT_STATUS_IS_OK(status)) {
4605 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
4606 && (access_mask & DELETE_ACCESS)
4607 && !is_ntfs_stream_smb_fname(smb_fname)) {
4609 * We can't open a file with DELETE access if any of the
4610 * streams is open without FILE_SHARE_DELETE
4612 status = open_streams_for_delete(conn, smb_fname);
4614 if (!NT_STATUS_IS_OK(status)) {
4619 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
4620 !security_token_has_privilege(get_current_nttok(conn),
4621 SEC_PRIV_SECURITY)) {
4622 DEBUG(10, ("create_file_unixpath: open on %s "
4623 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
4624 smb_fname_str_dbg(smb_fname)));
4625 status = NT_STATUS_PRIVILEGE_NOT_HELD;
4629 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
4630 && is_ntfs_stream_smb_fname(smb_fname)
4631 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
4632 uint32_t base_create_disposition;
4633 struct smb_filename *smb_fname_base = NULL;
4635 if (create_options & FILE_DIRECTORY_FILE) {
4636 status = NT_STATUS_NOT_A_DIRECTORY;
4640 switch (create_disposition) {
4642 base_create_disposition = FILE_OPEN;
4645 base_create_disposition = FILE_OPEN_IF;
4649 /* Create an smb_filename with stream_name == NULL. */
4650 smb_fname_base = synthetic_smb_fname(talloc_tos(),
4651 smb_fname->base_name,
4655 if (smb_fname_base == NULL) {
4656 status = NT_STATUS_NO_MEMORY;
4660 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
4661 DEBUG(10, ("Unable to stat stream: %s\n",
4662 smb_fname_str_dbg(smb_fname_base)));
4665 * https://bugzilla.samba.org/show_bug.cgi?id=10229
4666 * We need to check if the requested access mask
4667 * could be used to open the underlying file (if
4668 * it existed), as we're passing in zero for the
4669 * access mask to the base filename.
4671 status = check_base_file_access(conn,
4675 if (!NT_STATUS_IS_OK(status)) {
4676 DEBUG(10, ("Permission check "
4677 "for base %s failed: "
4678 "%s\n", smb_fname->base_name,
4679 nt_errstr(status)));
4684 /* Open the base file. */
4685 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
4688 | FILE_SHARE_DELETE,
4689 base_create_disposition,
4690 0, 0, 0, NULL, 0, 0, NULL, NULL,
4692 TALLOC_FREE(smb_fname_base);
4694 if (!NT_STATUS_IS_OK(status)) {
4695 DEBUG(10, ("create_file_unixpath for base %s failed: "
4696 "%s\n", smb_fname->base_name,
4697 nt_errstr(status)));
4700 /* we don't need the low level fd */
4705 * If it's a request for a directory open, deal with it separately.
4708 if (create_options & FILE_DIRECTORY_FILE) {
4710 if (create_options & FILE_NON_DIRECTORY_FILE) {
4711 status = NT_STATUS_INVALID_PARAMETER;
4715 /* Can't open a temp directory. IFS kit test. */
4716 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
4717 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
4718 status = NT_STATUS_INVALID_PARAMETER;
4723 * We will get a create directory here if the Win32
4724 * app specified a security descriptor in the
4725 * CreateDirectory() call.
4729 status = open_directory(
4730 conn, req, smb_fname, access_mask, share_access,
4731 create_disposition, create_options, file_attributes,
4736 * Ordinary file case.
4739 status = file_new(req, conn, &fsp);
4740 if(!NT_STATUS_IS_OK(status)) {
4744 status = fsp_set_smb_fname(fsp, smb_fname);
4745 if (!NT_STATUS_IS_OK(status)) {
4751 * We're opening the stream element of a
4752 * base_fsp we already opened. Set up the
4755 fsp->base_fsp = base_fsp;
4758 if (allocation_size) {
4759 fsp->initial_allocation_size = smb_roundup(fsp->conn,
4763 status = open_file_ntcreate(conn,
4776 if(!NT_STATUS_IS_OK(status)) {
4777 file_free(req, fsp);
4781 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
4783 /* A stream open never opens a directory */
4786 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4791 * Fail the open if it was explicitly a non-directory
4795 if (create_options & FILE_NON_DIRECTORY_FILE) {
4796 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4801 status = open_directory(
4802 conn, req, smb_fname, access_mask,
4803 share_access, create_disposition,
4804 create_options, file_attributes,
4809 if (!NT_STATUS_IS_OK(status)) {
4813 fsp->base_fsp = base_fsp;
4815 if ((ea_list != NULL) &&
4816 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
4817 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
4818 if (!NT_STATUS_IS_OK(status)) {
4823 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
4824 status = NT_STATUS_ACCESS_DENIED;
4828 /* Save the requested allocation size. */
4829 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
4830 if ((allocation_size > fsp->fsp_name->st.st_ex_size)
4831 && !(fsp->is_directory))
4833 fsp->initial_allocation_size = smb_roundup(
4834 fsp->conn, allocation_size);
4835 if (vfs_allocate_file_space(
4836 fsp, fsp->initial_allocation_size) == -1) {
4837 status = NT_STATUS_DISK_FULL;
4841 fsp->initial_allocation_size = smb_roundup(
4842 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
4845 fsp->initial_allocation_size = 0;
4848 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
4849 fsp->base_fsp == NULL) {
4852 * According to the MS documentation, the only time the security
4853 * descriptor is applied to the opened file is iff we *created* the
4854 * file; an existing file stays the same.
4856 * Also, it seems (from observation) that you can open the file with
4857 * any access mask but you can still write the sd. We need to override
4858 * the granted access before we call set_sd
4859 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4862 uint32_t sec_info_sent;
4863 uint32_t saved_access_mask = fsp->access_mask;
4865 sec_info_sent = get_sec_info(sd);
4867 fsp->access_mask = FILE_GENERIC_ALL;
4869 if (sec_info_sent & (SECINFO_OWNER|
4873 status = set_sd(fsp, sd, sec_info_sent);
4876 fsp->access_mask = saved_access_mask;
4878 if (!NT_STATUS_IS_OK(status)) {
4881 } else if (lp_inherit_acls(SNUM(conn))) {
4882 /* Inherit from parent. Errors here are not fatal. */
4883 status = inherit_new_acl(fsp);
4884 if (!NT_STATUS_IS_OK(status)) {
4885 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4887 nt_errstr(status) ));
4892 if ((conn->fs_capabilities & FILE_FILE_COMPRESSION)
4893 && (create_options & FILE_NO_COMPRESSION)
4894 && (info == FILE_WAS_CREATED)) {
4895 status = SMB_VFS_SET_COMPRESSION(conn, fsp, fsp,
4896 COMPRESSION_FORMAT_NONE);
4897 if (!NT_STATUS_IS_OK(status)) {
4898 DEBUG(1, ("failed to disable compression: %s\n",
4899 nt_errstr(status)));
4903 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
4906 if (pinfo != NULL) {
4910 smb_fname->st = fsp->fsp_name->st;
4912 return NT_STATUS_OK;
4915 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
4918 if (base_fsp && fsp->base_fsp == base_fsp) {
4920 * The close_file below will close
4925 close_file(req, fsp, ERROR_CLOSE);
4928 if (base_fsp != NULL) {
4929 close_file(req, base_fsp, ERROR_CLOSE);
4936 * Calculate the full path name given a relative fid.
4938 NTSTATUS get_relative_fid_filename(connection_struct *conn,
4939 struct smb_request *req,
4940 uint16_t root_dir_fid,
4941 const struct smb_filename *smb_fname,
4942 struct smb_filename **smb_fname_out)
4944 files_struct *dir_fsp;
4945 char *parent_fname = NULL;
4946 char *new_base_name = NULL;
4947 uint32_t ucf_flags = ((req != NULL && req->posix_pathnames) ?
4948 UCF_POSIX_PATHNAMES : 0);
4951 if (root_dir_fid == 0 || !smb_fname) {
4952 status = NT_STATUS_INTERNAL_ERROR;
4956 dir_fsp = file_fsp(req, root_dir_fid);
4958 if (dir_fsp == NULL) {
4959 status = NT_STATUS_INVALID_HANDLE;
4963 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4964 status = NT_STATUS_INVALID_HANDLE;
4968 if (!dir_fsp->is_directory) {
4971 * Check to see if this is a mac fork of some kind.
4974 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4975 is_ntfs_stream_smb_fname(smb_fname)) {
4976 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4981 we need to handle the case when we get a
4982 relative open relative to a file and the
4983 pathname is blank - this is a reopen!
4984 (hint from demyn plantenberg)
4987 status = NT_STATUS_INVALID_HANDLE;
4991 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4993 * We're at the toplevel dir, the final file name
4994 * must not contain ./, as this is filtered out
4995 * normally by srvstr_get_path and unix_convert
4996 * explicitly rejects paths containing ./.
4998 parent_fname = talloc_strdup(talloc_tos(), "");
4999 if (parent_fname == NULL) {
5000 status = NT_STATUS_NO_MEMORY;
5004 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
5007 * Copy in the base directory name.
5010 parent_fname = talloc_array(talloc_tos(), char,
5012 if (parent_fname == NULL) {
5013 status = NT_STATUS_NO_MEMORY;
5016 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
5020 * Ensure it ends in a '/'.
5021 * We used TALLOC_SIZE +2 to add space for the '/'.
5025 && (parent_fname[dir_name_len-1] != '\\')
5026 && (parent_fname[dir_name_len-1] != '/')) {
5027 parent_fname[dir_name_len] = '/';
5028 parent_fname[dir_name_len+1] = '\0';
5032 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
5033 smb_fname->base_name);
5034 if (new_base_name == NULL) {
5035 status = NT_STATUS_NO_MEMORY;
5039 status = filename_convert(req,
5041 req->flags2 & FLAGS2_DFS_PATHNAMES,
5046 if (!NT_STATUS_IS_OK(status)) {
5051 TALLOC_FREE(parent_fname);
5052 TALLOC_FREE(new_base_name);
5056 NTSTATUS create_file_default(connection_struct *conn,
5057 struct smb_request *req,
5058 uint16_t root_dir_fid,
5059 struct smb_filename *smb_fname,
5060 uint32_t access_mask,
5061 uint32_t share_access,
5062 uint32_t create_disposition,
5063 uint32_t create_options,
5064 uint32_t file_attributes,
5065 uint32_t oplock_request,
5066 struct smb2_lease *lease,
5067 uint64_t allocation_size,
5068 uint32_t private_flags,
5069 struct security_descriptor *sd,
5070 struct ea_list *ea_list,
5071 files_struct **result,
5073 const struct smb2_create_blobs *in_context_blobs,
5074 struct smb2_create_blobs *out_context_blobs)
5076 int info = FILE_WAS_OPENED;
5077 files_struct *fsp = NULL;
5079 bool stream_name = false;
5081 DEBUG(10,("create_file: access_mask = 0x%x "
5082 "file_attributes = 0x%x, share_access = 0x%x, "
5083 "create_disposition = 0x%x create_options = 0x%x "
5084 "oplock_request = 0x%x "
5085 "private_flags = 0x%x "
5086 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
5088 (unsigned int)access_mask,
5089 (unsigned int)file_attributes,
5090 (unsigned int)share_access,
5091 (unsigned int)create_disposition,
5092 (unsigned int)create_options,
5093 (unsigned int)oplock_request,
5094 (unsigned int)private_flags,
5095 (unsigned int)root_dir_fid,
5096 ea_list, sd, smb_fname_str_dbg(smb_fname)));
5099 * Calculate the filename from the root_dir_if if necessary.
5102 if (root_dir_fid != 0) {
5103 struct smb_filename *smb_fname_out = NULL;
5104 status = get_relative_fid_filename(conn, req, root_dir_fid,
5105 smb_fname, &smb_fname_out);
5106 if (!NT_STATUS_IS_OK(status)) {
5109 smb_fname = smb_fname_out;
5113 * Check to see if this is a mac fork of some kind.
5116 stream_name = is_ntfs_stream_smb_fname(smb_fname);
5118 enum FAKE_FILE_TYPE fake_file_type;
5120 fake_file_type = is_fake_file(smb_fname);
5122 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
5125 * Here we go! support for changing the disk quotas
5128 * We need to fake up to open this MAGIC QUOTA file
5129 * and return a valid FID.
5131 * w2k close this file directly after openening xp
5132 * also tries a QUERY_FILE_INFO on the file and then
5135 status = open_fake_file(req, conn, req->vuid,
5136 fake_file_type, smb_fname,
5138 if (!NT_STATUS_IS_OK(status)) {
5142 ZERO_STRUCT(smb_fname->st);
5146 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
5147 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5152 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
5154 smb_fname->stream_name = NULL;
5155 /* We have to handle this error here. */
5156 if (create_options & FILE_DIRECTORY_FILE) {
5157 status = NT_STATUS_NOT_A_DIRECTORY;
5160 if (req != NULL && req->posix_pathnames) {
5161 ret = SMB_VFS_LSTAT(conn, smb_fname);
5163 ret = SMB_VFS_STAT(conn, smb_fname);
5166 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
5167 status = NT_STATUS_FILE_IS_A_DIRECTORY;
5172 status = create_file_unixpath(
5173 conn, req, smb_fname, access_mask, share_access,
5174 create_disposition, create_options, file_attributes,
5175 oplock_request, lease, allocation_size, private_flags,
5179 if (!NT_STATUS_IS_OK(status)) {
5184 DEBUG(10, ("create_file: info=%d\n", info));
5187 if (pinfo != NULL) {
5190 return NT_STATUS_OK;
5193 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
5196 close_file(req, fsp, ERROR_CLOSE);