2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Ralph Boehme 2017
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
25 #include "lib/util/server_id.h"
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "fake_file.h"
30 #include "../libcli/security/security.h"
31 #include "../librpc/gen_ndr/ndr_security.h"
32 #include "../librpc/gen_ndr/open_files.h"
33 #include "../librpc/gen_ndr/idmap.h"
34 #include "../librpc/gen_ndr/ioctl.h"
35 #include "passdb/lookup_sid.h"
39 #include "source3/lib/dbwrap/dbwrap_watch.h"
40 #include "locking/leases_db.h"
41 #include "librpc/gen_ndr/ndr_leases_db.h"
43 extern const struct generic_mapping file_generic_mapping
;
45 struct deferred_open_record
{
46 bool delayed_for_oplocks
;
51 * Timer for async opens, needed because they don't use a watch on
52 * a locking.tdb record. This is currently only used for real async
53 * opens and just terminates smbd if the async open times out.
55 struct tevent_timer
*te
;
58 /****************************************************************************
59 If the requester wanted DELETE_ACCESS and was rejected because
60 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
62 ****************************************************************************/
64 static bool parent_override_delete(connection_struct
*conn
,
65 const struct smb_filename
*smb_fname
,
67 uint32_t rejected_mask
)
69 if ((access_mask
& DELETE_ACCESS
) &&
70 (rejected_mask
& DELETE_ACCESS
) &&
71 can_delete_file_in_directory(conn
, smb_fname
)) {
77 /****************************************************************************
78 Check if we have open rights.
79 ****************************************************************************/
81 NTSTATUS
smbd_check_access_rights(struct connection_struct
*conn
,
82 const struct smb_filename
*smb_fname
,
86 /* Check if we have rights to open. */
88 struct security_descriptor
*sd
= NULL
;
89 uint32_t rejected_share_access
;
90 uint32_t rejected_mask
= access_mask
;
91 uint32_t do_not_check_mask
= 0;
93 rejected_share_access
= access_mask
& ~(conn
->share_access
);
95 if (rejected_share_access
) {
96 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
98 (unsigned int)access_mask
,
99 smb_fname_str_dbg(smb_fname
),
100 (unsigned int)rejected_share_access
));
101 return NT_STATUS_ACCESS_DENIED
;
104 if (!use_privs
&& get_current_uid(conn
) == (uid_t
)0) {
105 /* I'm sorry sir, I didn't know you were root... */
106 DEBUG(10,("smbd_check_access_rights: root override "
107 "on %s. Granting 0x%x\n",
108 smb_fname_str_dbg(smb_fname
),
109 (unsigned int)access_mask
));
113 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
114 DEBUG(10,("smbd_check_access_rights: not checking ACL "
115 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
116 smb_fname_str_dbg(smb_fname
),
117 (unsigned int)access_mask
));
121 if (access_mask
== DELETE_ACCESS
&&
122 VALID_STAT(smb_fname
->st
) &&
123 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
124 /* We can always delete a symlink. */
125 DEBUG(10,("smbd_check_access_rights: not checking ACL "
126 "on DELETE_ACCESS on symlink %s.\n",
127 smb_fname_str_dbg(smb_fname
) ));
131 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
,
134 SECINFO_DACL
), talloc_tos(), &sd
);
136 if (!NT_STATUS_IS_OK(status
)) {
137 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
139 smb_fname_str_dbg(smb_fname
),
142 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
150 * If we can access the path to this file, by
151 * default we have FILE_READ_ATTRIBUTES from the
152 * containing directory. See the section:
153 * "Algorithm to Check Access to an Existing File"
156 * se_file_access_check() also takes care of
157 * owner WRITE_DAC and READ_CONTROL.
159 do_not_check_mask
= FILE_READ_ATTRIBUTES
;
162 * Samba 3.6 and earlier granted execute access even
163 * if the ACL did not contain execute rights.
164 * Samba 4.0 is more correct and checks it.
165 * The compatibilty mode allows one to skip this check
166 * to smoothen upgrades.
168 if (lp_acl_allow_execute_always(SNUM(conn
))) {
169 do_not_check_mask
|= FILE_EXECUTE
;
172 status
= se_file_access_check(sd
,
173 get_current_nttok(conn
),
175 (access_mask
& ~do_not_check_mask
),
178 DEBUG(10,("smbd_check_access_rights: file %s requesting "
179 "0x%x returning 0x%x (%s)\n",
180 smb_fname_str_dbg(smb_fname
),
181 (unsigned int)access_mask
,
182 (unsigned int)rejected_mask
,
183 nt_errstr(status
) ));
185 if (!NT_STATUS_IS_OK(status
)) {
186 if (DEBUGLEVEL
>= 10) {
187 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
188 smb_fname_str_dbg(smb_fname
) ));
189 NDR_PRINT_DEBUG(security_descriptor
, sd
);
195 if (NT_STATUS_IS_OK(status
) ||
196 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
200 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
204 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
205 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
206 !lp_store_dos_attributes(SNUM(conn
)) &&
207 (lp_map_readonly(SNUM(conn
)) ||
208 lp_map_archive(SNUM(conn
)) ||
209 lp_map_hidden(SNUM(conn
)) ||
210 lp_map_system(SNUM(conn
)))) {
211 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
213 DEBUG(10,("smbd_check_access_rights: "
215 "FILE_WRITE_ATTRIBUTES "
217 smb_fname_str_dbg(smb_fname
)));
220 if (parent_override_delete(conn
,
224 /* Were we trying to do an open
225 * for delete and didn't get DELETE
226 * access (only) ? Check if the
227 * directory allows DELETE_CHILD.
229 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
232 rejected_mask
&= ~DELETE_ACCESS
;
234 DEBUG(10,("smbd_check_access_rights: "
238 smb_fname_str_dbg(smb_fname
)));
241 if (rejected_mask
!= 0) {
242 return NT_STATUS_ACCESS_DENIED
;
247 NTSTATUS
check_parent_access(struct connection_struct
*conn
,
248 struct smb_filename
*smb_fname
,
249 uint32_t access_mask
)
252 char *parent_dir
= NULL
;
253 struct security_descriptor
*parent_sd
= NULL
;
254 uint32_t access_granted
= 0;
255 struct smb_filename
*parent_smb_fname
= NULL
;
256 struct share_mode_lock
*lck
= NULL
;
257 struct file_id id
= {0};
259 bool delete_on_close_set
;
262 if (!parent_dirname(talloc_tos(),
263 smb_fname
->base_name
,
266 return NT_STATUS_NO_MEMORY
;
269 parent_smb_fname
= synthetic_smb_fname(talloc_tos(),
274 if (parent_smb_fname
== NULL
) {
275 return NT_STATUS_NO_MEMORY
;
278 if (get_current_uid(conn
) == (uid_t
)0) {
279 /* I'm sorry sir, I didn't know you were root... */
280 DEBUG(10,("check_parent_access: root override "
281 "on %s. Granting 0x%x\n",
282 smb_fname_str_dbg(smb_fname
),
283 (unsigned int)access_mask
));
287 status
= SMB_VFS_GET_NT_ACL(conn
,
293 if (!NT_STATUS_IS_OK(status
)) {
294 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
295 "%s with error %s\n",
302 * If we can access the path to this file, by
303 * default we have FILE_READ_ATTRIBUTES from the
304 * containing directory. See the section:
305 * "Algorithm to Check Access to an Existing File"
308 * se_file_access_check() also takes care of
309 * owner WRITE_DAC and READ_CONTROL.
311 status
= se_file_access_check(parent_sd
,
312 get_current_nttok(conn
),
314 (access_mask
& ~FILE_READ_ATTRIBUTES
),
316 if(!NT_STATUS_IS_OK(status
)) {
317 DEBUG(5,("check_parent_access: access check "
318 "on directory %s for "
319 "path %s for mask 0x%x returned (0x%x) %s\n",
321 smb_fname
->base_name
,
324 nt_errstr(status
) ));
328 if (!(access_mask
& (SEC_DIR_ADD_FILE
| SEC_DIR_ADD_SUBDIR
))) {
331 if (!lp_check_parent_directory_delete_on_close(SNUM(conn
))) {
335 /* Check if the directory has delete-on-close set */
336 ret
= SMB_VFS_STAT(conn
, parent_smb_fname
);
338 status
= map_nt_error_from_unix(errno
);
342 id
= SMB_VFS_FILE_ID_CREATE(conn
, &parent_smb_fname
->st
);
344 status
= file_name_hash(conn
, parent_smb_fname
->base_name
, &name_hash
);
345 if (!NT_STATUS_IS_OK(status
)) {
349 lck
= get_existing_share_mode_lock(talloc_tos(), id
);
351 status
= NT_STATUS_OK
;
355 delete_on_close_set
= is_delete_on_close_set(lck
, name_hash
);
356 if (delete_on_close_set
) {
357 status
= NT_STATUS_DELETE_PENDING
;
361 status
= NT_STATUS_OK
;
365 TALLOC_FREE(parent_smb_fname
);
369 /****************************************************************************
370 Ensure when opening a base file for a stream open that we have permissions
371 to do so given the access mask on the base file.
372 ****************************************************************************/
374 static NTSTATUS
check_base_file_access(struct connection_struct
*conn
,
375 struct smb_filename
*smb_fname
,
376 uint32_t access_mask
)
380 status
= smbd_calculate_access_mask(conn
, smb_fname
,
384 if (!NT_STATUS_IS_OK(status
)) {
385 DEBUG(10, ("smbd_calculate_access_mask "
386 "on file %s returned %s\n",
387 smb_fname_str_dbg(smb_fname
),
392 if (access_mask
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) {
394 if (!CAN_WRITE(conn
)) {
395 return NT_STATUS_ACCESS_DENIED
;
397 dosattrs
= dos_mode(conn
, smb_fname
);
398 if (IS_DOS_READONLY(dosattrs
)) {
399 return NT_STATUS_ACCESS_DENIED
;
403 return smbd_check_access_rights(conn
,
409 /****************************************************************************
410 Handle differing symlink errno's
411 ****************************************************************************/
413 static int link_errno_convert(int err
)
415 #if defined(ENOTSUP) && defined(OSF1)
416 /* handle special Tru64 errno */
417 if (err
== ENOTSUP
) {
422 /* fix broken NetBSD errno */
427 /* fix broken FreeBSD errno */
434 static int non_widelink_open(struct connection_struct
*conn
,
435 const struct smb_filename
*conn_rootdir_fname
,
437 struct smb_filename
*smb_fname
,
440 unsigned int link_depth
);
442 /****************************************************************************
443 Follow a symlink in userspace.
444 ****************************************************************************/
446 static int process_symlink_open(struct connection_struct
*conn
,
447 const struct smb_filename
*conn_rootdir_fname
,
449 struct smb_filename
*smb_fname
,
452 unsigned int link_depth
)
455 char *link_target
= NULL
;
456 struct smb_filename target_fname
= {0};
458 struct smb_filename
*oldwd_fname
= NULL
;
459 size_t rootdir_len
= 0;
460 struct smb_filename
*resolved_fname
= NULL
;
461 char *resolved_name
= NULL
;
462 bool matched
= false;
466 * Ensure we don't get stuck in a symlink loop.
469 if (link_depth
>= 20) {
474 /* Allocate space for the link target. */
475 link_target
= talloc_array(talloc_tos(), char, PATH_MAX
);
476 if (link_target
== NULL
) {
481 /* Read the link target. */
482 link_len
= SMB_VFS_READLINK(conn
,
486 if (link_len
== -1) {
490 /* Ensure it's at least null terminated. */
491 link_target
[link_len
] = '\0';
492 target_fname
= (struct smb_filename
){ .base_name
= link_target
};
494 /* Convert to an absolute path. */
495 resolved_fname
= SMB_VFS_REALPATH(conn
, talloc_tos(), &target_fname
);
496 if (resolved_fname
== NULL
) {
499 resolved_name
= resolved_fname
->base_name
;
502 * We know conn_rootdir starts with '/' and
503 * does not end in '/'. FIXME ! Should we
506 rootdir_len
= strlen(conn_rootdir_fname
->base_name
);
508 matched
= (strncmp(conn_rootdir_fname
->base_name
,
517 * Turn into a path relative to the share root.
519 if (resolved_name
[rootdir_len
] == '\0') {
520 /* Link to the root of the share. */
521 TALLOC_FREE(smb_fname
->base_name
);
522 smb_fname
->base_name
= talloc_strdup(smb_fname
, ".");
523 } else if (resolved_name
[rootdir_len
] == '/') {
524 TALLOC_FREE(smb_fname
->base_name
);
525 smb_fname
->base_name
= talloc_strdup(smb_fname
,
526 &resolved_name
[rootdir_len
+1]);
532 if (smb_fname
->base_name
== NULL
) {
537 oldwd_fname
= vfs_GetWd(talloc_tos(), conn
);
538 if (oldwd_fname
== NULL
) {
542 /* Ensure we operate from the root of the share. */
543 if (vfs_ChDir(conn
, conn_rootdir_fname
) == -1) {
547 /* And do it all again.. */
548 fd
= non_widelink_open(conn
,
561 TALLOC_FREE(resolved_fname
);
562 TALLOC_FREE(link_target
);
563 if (oldwd_fname
!= NULL
) {
564 int ret
= vfs_ChDir(conn
, oldwd_fname
);
566 smb_panic("unable to get back to old directory\n");
568 TALLOC_FREE(oldwd_fname
);
570 if (saved_errno
!= 0) {
576 /****************************************************************************
578 ****************************************************************************/
580 static int non_widelink_open(struct connection_struct
*conn
,
581 const struct smb_filename
*conn_rootdir_fname
,
583 struct smb_filename
*smb_fname
,
586 unsigned int link_depth
)
590 struct smb_filename
*smb_fname_rel
= NULL
;
592 struct smb_filename
*oldwd_fname
= NULL
;
593 char *parent_dir
= NULL
;
594 struct smb_filename parent_dir_fname
= {0};
595 const char *final_component
= NULL
;
596 bool is_directory
= false;
600 if (flags
& O_DIRECTORY
) {
606 parent_dir
= talloc_strdup(talloc_tos(), smb_fname
->base_name
);
607 if (parent_dir
== NULL
) {
612 final_component
= ".";
614 ok
= parent_dirname(talloc_tos(),
615 smb_fname
->base_name
,
624 parent_dir_fname
= (struct smb_filename
) { .base_name
= parent_dir
};
626 oldwd_fname
= vfs_GetWd(talloc_tos(), conn
);
627 if (oldwd_fname
== NULL
) {
631 /* Pin parent directory in place. */
632 if (vfs_ChDir(conn
, &parent_dir_fname
) == -1) {
636 smb_fname_rel
= synthetic_smb_fname(talloc_tos(),
638 smb_fname
->stream_name
,
641 if (smb_fname_rel
== NULL
) {
642 saved_errno
= ENOMEM
;
646 /* Ensure the relative path is below the share. */
647 status
= check_reduced_name(conn
, &parent_dir_fname
, smb_fname_rel
);
648 if (!NT_STATUS_IS_OK(status
)) {
649 saved_errno
= map_errno_from_nt_status(status
);
656 struct smb_filename
*tmp_name
= fsp
->fsp_name
;
657 fsp
->fsp_name
= smb_fname_rel
;
658 fd
= SMB_VFS_OPEN(conn
, smb_fname_rel
, fsp
, flags
, mode
);
659 fsp
->fsp_name
= tmp_name
;
663 saved_errno
= link_errno_convert(errno
);
665 * Trying to open a symlink to a directory with O_NOFOLLOW and
666 * O_DIRECTORY can return either of ELOOP and ENOTDIR. So
667 * ENOTDIR really means: might be a symlink, but we're not sure.
668 * In this case, we just assume there's a symlink. If we were
669 * wrong, process_symlink_open() will return EINVAL. We check
670 * this below, and fall back to returning the initial
673 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=12860
675 if (saved_errno
== ELOOP
|| saved_errno
== ENOTDIR
) {
676 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
677 /* Never follow symlinks on posix open. */
680 if (!lp_follow_symlinks(SNUM(conn
))) {
681 /* Explicitly no symlinks. */
685 * We may have a symlink. Follow in userspace
686 * to ensure it's under the share definition.
688 fd
= process_symlink_open(conn
,
696 if (saved_errno
== ENOTDIR
&&
699 * O_DIRECTORY on neither a directory,
700 * nor a symlink. Just return
701 * saved_errno from initial open()
706 link_errno_convert(errno
);
713 TALLOC_FREE(parent_dir
);
714 TALLOC_FREE(smb_fname_rel
);
716 if (oldwd_fname
!= NULL
) {
717 int ret
= vfs_ChDir(conn
, oldwd_fname
);
719 smb_panic("unable to get back to old directory\n");
721 TALLOC_FREE(oldwd_fname
);
723 if (saved_errno
!= 0) {
729 /****************************************************************************
730 fd support routines - attempt to do a dos_open.
731 ****************************************************************************/
733 NTSTATUS
fd_open(struct connection_struct
*conn
,
738 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
739 NTSTATUS status
= NT_STATUS_OK
;
742 * Never follow symlinks on a POSIX client. The
743 * client should be doing this.
746 if ((fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) || !lp_follow_symlinks(SNUM(conn
))) {
750 /* Ensure path is below share definition. */
751 if (!lp_widelinks(SNUM(conn
))) {
752 struct smb_filename
*conn_rootdir_fname
= NULL
;
753 const char *conn_rootdir
= SMB_VFS_CONNECTPATH(conn
,
757 if (conn_rootdir
== NULL
) {
758 return NT_STATUS_NO_MEMORY
;
760 conn_rootdir_fname
= synthetic_smb_fname(talloc_tos(),
765 if (conn_rootdir_fname
== NULL
) {
766 return NT_STATUS_NO_MEMORY
;
770 * Only follow symlinks within a share
773 fsp
->fh
->fd
= non_widelink_open(conn
,
780 if (fsp
->fh
->fd
== -1) {
783 TALLOC_FREE(conn_rootdir_fname
);
784 if (saved_errno
!= 0) {
788 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
791 if (fsp
->fh
->fd
== -1) {
792 int posix_errno
= link_errno_convert(errno
);
793 status
= map_nt_error_from_unix(posix_errno
);
794 if (errno
== EMFILE
) {
795 static time_t last_warned
= 0L;
797 if (time((time_t *) NULL
) > last_warned
) {
798 DEBUG(0,("Too many open files, unable "
799 "to open more! smbd's max "
801 lp_max_open_files()));
802 last_warned
= time((time_t *) NULL
);
808 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
809 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
810 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
815 /****************************************************************************
816 Close the file associated with a fsp.
817 ****************************************************************************/
819 NTSTATUS
fd_close(files_struct
*fsp
)
826 if (fsp
->fh
->fd
== -1) {
827 return NT_STATUS_OK
; /* What we used to call a stat open. */
829 if (fsp
->fh
->ref_count
> 1) {
830 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
833 ret
= SMB_VFS_CLOSE(fsp
);
836 return map_nt_error_from_unix(errno
);
841 /****************************************************************************
842 Change the ownership of a file to that of the parent directory.
843 Do this by fd if possible.
844 ****************************************************************************/
846 void change_file_owner_to_parent(connection_struct
*conn
,
847 const char *inherit_from_dir
,
850 struct smb_filename
*smb_fname_parent
;
853 smb_fname_parent
= synthetic_smb_fname(talloc_tos(),
858 if (smb_fname_parent
== NULL
) {
862 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
864 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
865 "directory %s. Error was %s\n",
866 smb_fname_str_dbg(smb_fname_parent
),
868 TALLOC_FREE(smb_fname_parent
);
872 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
873 /* Already this uid - no need to change. */
874 DEBUG(10,("change_file_owner_to_parent: file %s "
875 "is already owned by uid %d\n",
877 (int)fsp
->fsp_name
->st
.st_ex_uid
));
878 TALLOC_FREE(smb_fname_parent
);
883 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
886 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
887 "file %s to parent directory uid %u. Error "
888 "was %s\n", fsp_str_dbg(fsp
),
889 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
892 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
893 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
894 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
895 /* Ensure the uid entry is updated. */
896 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
899 TALLOC_FREE(smb_fname_parent
);
902 static NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
903 const char *inherit_from_dir
,
904 struct smb_filename
*smb_dname
,
905 SMB_STRUCT_STAT
*psbuf
)
907 struct smb_filename
*smb_fname_parent
;
908 struct smb_filename
*smb_fname_cwd
= NULL
;
909 struct smb_filename
*saved_dir_fname
= NULL
;
910 TALLOC_CTX
*ctx
= talloc_tos();
911 NTSTATUS status
= NT_STATUS_OK
;
914 smb_fname_parent
= synthetic_smb_fname(ctx
,
919 if (smb_fname_parent
== NULL
) {
920 return NT_STATUS_NO_MEMORY
;
923 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
925 status
= map_nt_error_from_unix(errno
);
926 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
927 "directory %s. Error was %s\n",
928 smb_fname_str_dbg(smb_fname_parent
),
933 /* We've already done an lstat into psbuf, and we know it's a
934 directory. If we can cd into the directory and the dev/ino
935 are the same then we can safely chown without races as
936 we're locking the directory in place by being in it. This
937 should work on any UNIX (thanks tridge :-). JRA.
940 saved_dir_fname
= vfs_GetWd(ctx
,conn
);
941 if (!saved_dir_fname
) {
942 status
= map_nt_error_from_unix(errno
);
943 DEBUG(0,("change_dir_owner_to_parent: failed to get "
944 "current working directory. Error was %s\n",
949 /* Chdir into the new path. */
950 if (vfs_ChDir(conn
, smb_dname
) == -1) {
951 status
= map_nt_error_from_unix(errno
);
952 DEBUG(0,("change_dir_owner_to_parent: failed to change "
953 "current working directory to %s. Error "
954 "was %s\n", smb_dname
->base_name
, strerror(errno
) ));
958 smb_fname_cwd
= synthetic_smb_fname(ctx
, ".", NULL
, NULL
, 0);
959 if (smb_fname_cwd
== NULL
) {
960 status
= NT_STATUS_NO_MEMORY
;
964 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
966 status
= map_nt_error_from_unix(errno
);
967 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
968 "directory '.' (%s) Error was %s\n",
969 smb_dname
->base_name
, strerror(errno
)));
973 /* Ensure we're pointing at the same place. */
974 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
975 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
976 DEBUG(0,("change_dir_owner_to_parent: "
977 "device/inode on directory %s changed. "
978 "Refusing to chown !\n",
979 smb_dname
->base_name
));
980 status
= NT_STATUS_ACCESS_DENIED
;
984 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
985 /* Already this uid - no need to change. */
986 DEBUG(10,("change_dir_owner_to_parent: directory %s "
987 "is already owned by uid %d\n",
988 smb_dname
->base_name
,
989 (int)smb_fname_cwd
->st
.st_ex_uid
));
990 status
= NT_STATUS_OK
;
995 ret
= SMB_VFS_LCHOWN(conn
,
997 smb_fname_parent
->st
.st_ex_uid
,
1001 status
= map_nt_error_from_unix(errno
);
1002 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
1003 "directory %s to parent directory uid %u. "
1005 smb_dname
->base_name
,
1006 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
1009 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
1010 "directory %s to parent directory uid %u.\n",
1011 smb_dname
->base_name
,
1012 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
1013 /* Ensure the uid entry is updated. */
1014 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
1018 vfs_ChDir(conn
, saved_dir_fname
);
1020 TALLOC_FREE(saved_dir_fname
);
1021 TALLOC_FREE(smb_fname_parent
);
1022 TALLOC_FREE(smb_fname_cwd
);
1026 /****************************************************************************
1027 Open a file - returning a guaranteed ATOMIC indication of if the
1028 file was created or not.
1029 ****************************************************************************/
1031 static NTSTATUS
fd_open_atomic(struct connection_struct
*conn
,
1037 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
1038 NTSTATUS retry_status
;
1039 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
1042 *file_created
= false;
1044 if (!(flags
& O_CREAT
)) {
1046 * We're not creating the file, just pass through.
1048 return fd_open(conn
, fsp
, flags
, mode
);
1051 if (flags
& O_EXCL
) {
1053 * Fail if already exists, just pass through.
1055 status
= fd_open(conn
, fsp
, flags
, mode
);
1058 * Here we've opened with O_CREAT|O_EXCL. If that went
1059 * NT_STATUS_OK, we *know* we created this file.
1061 *file_created
= NT_STATUS_IS_OK(status
);
1067 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
1068 * To know absolutely if we created the file or not,
1069 * we can never call O_CREAT without O_EXCL. So if
1070 * we think the file existed, try without O_CREAT|O_EXCL.
1071 * If we think the file didn't exist, try with
1074 * The big problem here is dangling symlinks. Opening
1075 * without O_NOFOLLOW means both bad symlink
1076 * and missing path return -1, ENOENT from open(). As POSIX
1077 * is pathname based it's not possible to tell
1078 * the difference between these two cases in a
1079 * non-racy way, so change to try only two attempts before
1082 * We don't have this problem for the O_NOFOLLOW
1083 * case as it just returns NT_STATUS_OBJECT_PATH_NOT_FOUND
1084 * mapped from the ELOOP POSIX error.
1090 curr_flags
&= ~(O_CREAT
);
1091 retry_status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1093 curr_flags
|= O_EXCL
;
1094 retry_status
= NT_STATUS_OBJECT_NAME_COLLISION
;
1097 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
1098 if (NT_STATUS_IS_OK(status
)) {
1099 if (!file_existed
) {
1100 *file_created
= true;
1102 return NT_STATUS_OK
;
1104 if (!NT_STATUS_EQUAL(status
, retry_status
)) {
1111 * Keep file_existed up to date for clarity.
1113 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1114 file_existed
= false;
1115 curr_flags
|= O_EXCL
;
1116 DBG_DEBUG("file %s did not exist. Retry.\n",
1117 smb_fname_str_dbg(fsp
->fsp_name
));
1119 file_existed
= true;
1120 curr_flags
&= ~(O_CREAT
);
1121 DBG_DEBUG("file %s existed. Retry.\n",
1122 smb_fname_str_dbg(fsp
->fsp_name
));
1125 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
1127 if (NT_STATUS_IS_OK(status
) && (!file_existed
)) {
1128 *file_created
= true;
1134 /****************************************************************************
1136 ****************************************************************************/
1138 static NTSTATUS
open_file(files_struct
*fsp
,
1139 connection_struct
*conn
,
1140 struct smb_request
*req
,
1141 const char *parent_dir
,
1144 uint32_t access_mask
, /* client requested access mask. */
1145 uint32_t open_access_mask
, /* what we're actually using in the open. */
1146 bool *p_file_created
)
1148 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1149 NTSTATUS status
= NT_STATUS_OK
;
1150 int accmode
= (flags
& O_ACCMODE
);
1151 int local_flags
= flags
;
1152 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
1157 /* Check permissions */
1160 * This code was changed after seeing a client open request
1161 * containing the open mode of (DENY_WRITE/read-only) with
1162 * the 'create if not exist' bit set. The previous code
1163 * would fail to open the file read only on a read-only share
1164 * as it was checking the flags parameter directly against O_RDONLY,
1165 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
1169 if (!CAN_WRITE(conn
)) {
1170 /* It's a read-only share - fail if we wanted to write. */
1171 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
1172 DEBUG(3,("Permission denied opening %s\n",
1173 smb_fname_str_dbg(smb_fname
)));
1174 return NT_STATUS_ACCESS_DENIED
;
1176 if (flags
& O_CREAT
) {
1177 /* We don't want to write - but we must make sure that
1178 O_CREAT doesn't create the file if we have write
1179 access into the directory.
1181 flags
&= ~(O_CREAT
|O_EXCL
);
1182 local_flags
&= ~(O_CREAT
|O_EXCL
);
1187 * This little piece of insanity is inspired by the
1188 * fact that an NT client can open a file for O_RDONLY,
1189 * but set the create disposition to FILE_EXISTS_TRUNCATE.
1190 * If the client *can* write to the file, then it expects to
1191 * truncate the file, even though it is opening for readonly.
1192 * Quicken uses this stupid trick in backup file creation...
1193 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
1194 * for helping track this one down. It didn't bite us in 2.0.x
1195 * as we always opened files read-write in that release. JRA.
1198 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
1199 DEBUG(10,("open_file: truncate requested on read-only open "
1200 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
1201 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
1204 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
1205 (!file_existed
&& (local_flags
& O_CREAT
)) ||
1206 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
1210 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
1212 * We would block on opening a FIFO with no one else on the
1213 * other end. Do what we used to do and add O_NONBLOCK to the
1217 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
1218 local_flags
&= ~O_TRUNC
; /* Can't truncate a FIFO. */
1219 local_flags
|= O_NONBLOCK
;
1223 /* Don't create files with Microsoft wildcard characters. */
1224 if (fsp
->base_fsp
) {
1226 * wildcard characters are allowed in stream names
1227 * only test the basefilename
1229 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
1231 wild
= smb_fname
->base_name
;
1233 if ((local_flags
& O_CREAT
) && !file_existed
&&
1234 !(fsp
->posix_flags
& FSP_POSIX_FLAGS_PATHNAMES
) &&
1235 ms_has_wild(wild
)) {
1236 return NT_STATUS_OBJECT_NAME_INVALID
;
1239 /* Can we access this file ? */
1240 if (!fsp
->base_fsp
) {
1241 /* Only do this check on non-stream open. */
1243 status
= smbd_check_access_rights(conn
,
1248 if (!NT_STATUS_IS_OK(status
)) {
1249 DEBUG(10, ("open_file: "
1250 "smbd_check_access_rights "
1251 "on file %s returned %s\n",
1252 smb_fname_str_dbg(smb_fname
),
1253 nt_errstr(status
)));
1256 if (!NT_STATUS_IS_OK(status
) &&
1257 !NT_STATUS_EQUAL(status
,
1258 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
1263 if (NT_STATUS_EQUAL(status
,
1264 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
1266 DEBUG(10, ("open_file: "
1267 "file %s vanished since we "
1268 "checked for existence.\n",
1269 smb_fname_str_dbg(smb_fname
)));
1270 file_existed
= false;
1271 SET_STAT_INVALID(fsp
->fsp_name
->st
);
1275 if (!file_existed
) {
1276 if (!(local_flags
& O_CREAT
)) {
1277 /* File didn't exist and no O_CREAT. */
1278 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1281 status
= check_parent_access(conn
,
1284 if (!NT_STATUS_IS_OK(status
)) {
1285 DEBUG(10, ("open_file: "
1286 "check_parent_access on "
1287 "file %s returned %s\n",
1288 smb_fname_str_dbg(smb_fname
),
1289 nt_errstr(status
) ));
1296 * Actually do the open - if O_TRUNC is needed handle it
1297 * below under the share mode lock.
1299 status
= fd_open_atomic(conn
, fsp
, local_flags
& ~O_TRUNC
,
1300 unx_mode
, p_file_created
);
1301 if (!NT_STATUS_IS_OK(status
)) {
1302 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
1303 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
1304 nt_errstr(status
),local_flags
,flags
));
1308 if (local_flags
& O_NONBLOCK
) {
1310 * GPFS can return ETIMEDOUT for pread on
1311 * nonblocking file descriptors when files
1312 * migrated to tape need to be recalled. I
1313 * could imagine this happens elsehwere
1314 * too. With blocking file descriptors this
1317 ret
= set_blocking(fsp
->fh
->fd
, true);
1319 status
= map_nt_error_from_unix(errno
);
1320 DBG_WARNING("Could not set fd to blocking: "
1321 "%s\n", strerror(errno
));
1327 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
1329 /* If we have an fd, this stat should succeed. */
1330 DEBUG(0,("Error doing fstat on open file %s "
1332 smb_fname_str_dbg(smb_fname
),
1334 status
= map_nt_error_from_unix(errno
);
1339 if (*p_file_created
) {
1340 /* We created this file. */
1342 bool need_re_stat
= false;
1343 /* Do all inheritance work after we've
1344 done a successful fstat call and filled
1345 in the stat struct in fsp->fsp_name. */
1347 /* Inherit the ACL if required */
1348 if (lp_inherit_permissions(SNUM(conn
))) {
1349 inherit_access_posix_acl(conn
, parent_dir
,
1352 need_re_stat
= true;
1355 /* Change the owner if required. */
1356 if (lp_inherit_owner(SNUM(conn
)) != INHERIT_OWNER_NO
) {
1357 change_file_owner_to_parent(conn
, parent_dir
,
1359 need_re_stat
= true;
1363 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
1364 /* If we have an fd, this stat should succeed. */
1366 DEBUG(0,("Error doing fstat on open file %s "
1368 smb_fname_str_dbg(smb_fname
),
1373 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
1374 FILE_NOTIFY_CHANGE_FILE_NAME
,
1375 smb_fname
->base_name
);
1378 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
1379 if (!file_existed
) {
1380 /* File must exist for a stat open. */
1381 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1384 status
= smbd_check_access_rights(conn
,
1389 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
1390 (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) &&
1391 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
1392 /* This is a POSIX stat open for delete
1393 * or rename on a symlink that points
1394 * nowhere. Allow. */
1395 DEBUG(10,("open_file: allowing POSIX "
1396 "open on bad symlink %s\n",
1397 smb_fname_str_dbg(smb_fname
)));
1398 status
= NT_STATUS_OK
;
1401 if (!NT_STATUS_IS_OK(status
)) {
1402 DEBUG(10,("open_file: "
1403 "smbd_check_access_rights on file "
1405 smb_fname_str_dbg(smb_fname
),
1406 nt_errstr(status
) ));
1412 * POSIX allows read-only opens of directories. We don't
1413 * want to do this (we use a different code path for this)
1414 * so catch a directory open and return an EISDIR. JRA.
1417 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1420 return NT_STATUS_FILE_IS_A_DIRECTORY
;
1423 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1424 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
1425 fsp
->file_pid
= req
? req
->smbpid
: 0;
1426 fsp
->can_lock
= True
;
1427 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
1430 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
1431 fsp
->print_file
= NULL
;
1432 fsp
->modified
= False
;
1433 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
1434 fsp
->is_directory
= False
;
1435 if (conn
->aio_write_behind_list
&&
1436 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
1437 conn
->case_sensitive
)) {
1438 fsp
->aio_write_behind
= True
;
1441 fsp
->wcp
= NULL
; /* Write cache pointer. */
1443 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1444 conn
->session_info
->unix_info
->unix_name
,
1445 smb_fname_str_dbg(smb_fname
),
1446 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
1447 conn
->num_files_open
));
1450 return NT_STATUS_OK
;
1453 /****************************************************************************
1454 Check if we can open a file with a share mode.
1455 Returns True if conflict, False if not.
1456 ****************************************************************************/
1458 static bool share_conflict(struct share_mode_entry
*entry
,
1459 uint32_t access_mask
,
1460 uint32_t share_access
)
1462 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1463 "entry->share_access = 0x%x, "
1464 "entry->private_options = 0x%x\n",
1465 (unsigned int)entry
->access_mask
,
1466 (unsigned int)entry
->share_access
,
1467 (unsigned int)entry
->private_options
));
1469 if (server_id_is_disconnected(&entry
->pid
)) {
1471 * note: cleanup should have been done by
1472 * delay_for_batch_oplocks()
1477 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1478 (unsigned int)access_mask
, (unsigned int)share_access
));
1480 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
1484 DELETE_ACCESS
)) == 0) {
1485 DEBUG(10,("share_conflict: No conflict due to "
1486 "entry->access_mask = 0x%x\n",
1487 (unsigned int)entry
->access_mask
));
1491 if ((access_mask
& (FILE_WRITE_DATA
|
1495 DELETE_ACCESS
)) == 0) {
1496 DEBUG(10,("share_conflict: No conflict due to "
1497 "access_mask = 0x%x\n",
1498 (unsigned int)access_mask
));
1502 #if 1 /* JRA TEST - Superdebug. */
1503 #define CHECK_MASK(num, am, right, sa, share) \
1504 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1505 (unsigned int)(num), (unsigned int)(am), \
1506 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1507 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1508 (unsigned int)(num), (unsigned int)(sa), \
1509 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1510 if (((am) & (right)) && !((sa) & (share))) { \
1511 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1512 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1513 (unsigned int)(share) )); \
1517 #define CHECK_MASK(num, am, right, sa, share) \
1518 if (((am) & (right)) && !((sa) & (share))) { \
1519 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1520 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1521 (unsigned int)(share) )); \
1526 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1527 share_access
, FILE_SHARE_WRITE
);
1528 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1529 entry
->share_access
, FILE_SHARE_WRITE
);
1531 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1532 share_access
, FILE_SHARE_READ
);
1533 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1534 entry
->share_access
, FILE_SHARE_READ
);
1536 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
1537 share_access
, FILE_SHARE_DELETE
);
1538 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
1539 entry
->share_access
, FILE_SHARE_DELETE
);
1541 DEBUG(10,("share_conflict: No conflict.\n"));
1545 #if defined(DEVELOPER)
1546 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
1547 const struct file_id id
,
1549 struct share_mode_entry
*share_entry
)
1551 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1554 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1558 if (share_entry
->op_mid
== 0) {
1559 /* INTERNAL_OPEN_ONLY */
1563 if (!is_valid_share_mode_entry(share_entry
)) {
1567 fsp
= file_find_dif(sconn
, id
, share_entry
->share_file_id
);
1569 DBG_ERR("PANIC : %s\n",
1570 share_mode_str(talloc_tos(), num
, &id
,
1572 smb_panic("validate_my_share_entries: Cannot match a "
1573 "share entry with an open file\n");
1576 if (((uint16_t)fsp
->oplock_type
) != share_entry
->op_type
) {
1585 DBG_ERR("validate_my_share_entries: PANIC : %s\n",
1586 share_mode_str(talloc_tos(), num
, &id
,
1588 str
= talloc_asprintf(talloc_tos(),
1589 "validate_my_share_entries: "
1590 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1591 fsp
->fsp_name
->base_name
,
1592 (unsigned int)fsp
->oplock_type
,
1593 (unsigned int)share_entry
->op_type
);
1599 bool is_stat_open(uint32_t access_mask
)
1601 const uint32_t stat_open_bits
=
1602 (SYNCHRONIZE_ACCESS
|
1603 FILE_READ_ATTRIBUTES
|
1604 FILE_WRITE_ATTRIBUTES
);
1606 return (((access_mask
& stat_open_bits
) != 0) &&
1607 ((access_mask
& ~stat_open_bits
) == 0));
1610 static bool has_delete_on_close(struct share_mode_lock
*lck
,
1613 struct share_mode_data
*d
= lck
->data
;
1616 if (d
->num_share_modes
== 0) {
1619 if (!is_delete_on_close_set(lck
, name_hash
)) {
1622 for (i
=0; i
<d
->num_share_modes
; i
++) {
1623 if (!share_mode_stale_pid(d
, i
)) {
1630 /****************************************************************************
1631 Deal with share modes
1632 Invariant: Share mode must be locked on entry and exit.
1633 Returns -1 on error, or number of share modes on success (may be zero).
1634 ****************************************************************************/
1636 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1637 struct share_mode_lock
*lck
,
1638 uint32_t access_mask
,
1639 uint32_t share_access
)
1643 if(lck
->data
->num_share_modes
== 0) {
1644 return NT_STATUS_OK
;
1647 if (is_stat_open(access_mask
)) {
1648 /* Stat open that doesn't trigger oplock breaks or share mode
1649 * checks... ! JRA. */
1650 return NT_STATUS_OK
;
1654 * Check if the share modes will give us access.
1657 #if defined(DEVELOPER)
1658 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1659 validate_my_share_entries(conn
->sconn
, lck
->data
->id
, i
,
1660 &lck
->data
->share_modes
[i
]);
1664 /* Now we check the share modes, after any oplock breaks. */
1665 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1667 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1671 /* someone else has a share lock on it, check to see if we can
1673 if (share_conflict(&lck
->data
->share_modes
[i
],
1674 access_mask
, share_access
)) {
1676 if (share_mode_stale_pid(lck
->data
, i
)) {
1680 return NT_STATUS_SHARING_VIOLATION
;
1684 return NT_STATUS_OK
;
1688 * Send a break message to the oplock holder and delay the open for
1692 NTSTATUS
send_break_message(struct messaging_context
*msg_ctx
,
1693 const struct file_id
*id
,
1694 const struct share_mode_entry
*exclusive
,
1698 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1699 struct server_id_buf tmp
;
1701 DEBUG(10, ("Sending break request to PID %s\n",
1702 server_id_str_buf(exclusive
->pid
, &tmp
)));
1704 /* Create the message. */
1705 share_mode_entry_to_message(msg
, id
, exclusive
);
1707 /* Overload entry->op_type */
1709 * This is a cut from uint32_t to uint16_t, but so far only the lower 3
1710 * bits (LEASE_WRITE/HANDLE/READ are used anyway.
1712 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
, break_to
);
1714 status
= messaging_send_buf(msg_ctx
, exclusive
->pid
,
1715 MSG_SMB_BREAK_REQUEST
,
1716 (uint8_t *)msg
, sizeof(msg
));
1717 if (!NT_STATUS_IS_OK(status
)) {
1718 DEBUG(3, ("Could not send oplock break message: %s\n",
1719 nt_errstr(status
)));
1726 * Do internal consistency checks on the share mode for a file.
1729 static bool validate_oplock_types(struct share_mode_lock
*lck
)
1731 struct share_mode_data
*d
= lck
->data
;
1733 bool ex_or_batch
= false;
1734 bool level2
= false;
1735 bool no_oplock
= false;
1736 uint32_t num_non_stat_opens
= 0;
1739 for (i
=0; i
<d
->num_share_modes
; i
++) {
1740 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1742 if (!is_valid_share_mode_entry(e
)) {
1746 if (e
->op_mid
== 0) {
1747 /* INTERNAL_OPEN_ONLY */
1751 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1752 /* We ignore stat opens in the table - they
1753 always have NO_OPLOCK and never get or
1754 cause breaks. JRA. */
1758 num_non_stat_opens
+= 1;
1760 if (BATCH_OPLOCK_TYPE(e
->op_type
)) {
1761 /* batch - can only be one. */
1762 if (share_mode_stale_pid(d
, i
)) {
1763 DEBUG(10, ("Found stale batch oplock\n"));
1766 if (ex_or_batch
|| batch
|| level2
|| no_oplock
) {
1767 DEBUG(0, ("Bad batch oplock entry %u.",
1774 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1775 if (share_mode_stale_pid(d
, i
)) {
1776 DEBUG(10, ("Found stale duplicate oplock\n"));
1779 /* Exclusive or batch - can only be one. */
1780 if (ex_or_batch
|| level2
|| no_oplock
) {
1781 DEBUG(0, ("Bad exclusive or batch oplock "
1782 "entry %u.", (unsigned)i
));
1788 if (LEVEL_II_OPLOCK_TYPE(e
->op_type
)) {
1789 if (batch
|| ex_or_batch
) {
1790 if (share_mode_stale_pid(d
, i
)) {
1791 DEBUG(10, ("Found stale LevelII "
1795 DEBUG(0, ("Bad levelII oplock entry %u.",
1802 if (e
->op_type
== NO_OPLOCK
) {
1803 if (batch
|| ex_or_batch
) {
1804 if (share_mode_stale_pid(d
, i
)) {
1805 DEBUG(10, ("Found stale NO_OPLOCK "
1809 DEBUG(0, ("Bad no oplock entry %u.",
1817 remove_stale_share_mode_entries(d
);
1819 if ((batch
|| ex_or_batch
) && (num_non_stat_opens
!= 1)) {
1820 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1821 (int)batch
, (int)ex_or_batch
,
1822 (int)d
->num_share_modes
));
1829 static bool delay_for_oplock(files_struct
*fsp
,
1831 const struct smb2_lease
*lease
,
1832 struct share_mode_lock
*lck
,
1833 bool have_sharing_violation
,
1834 uint32_t create_disposition
,
1835 bool first_open_attempt
)
1837 struct share_mode_data
*d
= lck
->data
;
1840 bool will_overwrite
;
1842 if ((oplock_request
& INTERNAL_OPEN_ONLY
) ||
1843 is_stat_open(fsp
->access_mask
)) {
1847 switch (create_disposition
) {
1848 case FILE_SUPERSEDE
:
1849 case FILE_OVERWRITE
:
1850 case FILE_OVERWRITE_IF
:
1851 will_overwrite
= true;
1854 will_overwrite
= false;
1858 for (i
=0; i
<d
->num_share_modes
; i
++) {
1859 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1860 struct share_mode_lease
*l
= NULL
;
1861 uint32_t e_lease_type
= get_lease_type(d
, e
);
1863 uint32_t delay_mask
= 0;
1865 if (e
->op_type
== LEASE_OPLOCK
) {
1866 l
= &d
->leases
[e
->lease_idx
];
1869 if (have_sharing_violation
) {
1870 delay_mask
= SMB2_LEASE_HANDLE
;
1872 delay_mask
= SMB2_LEASE_WRITE
;
1875 break_to
= e_lease_type
& ~delay_mask
;
1877 if (will_overwrite
) {
1879 * we'll decide about SMB2_LEASE_READ later.
1881 * Maybe the break will be deferred
1883 break_to
&= ~SMB2_LEASE_HANDLE
;
1886 DEBUG(10, ("entry %u: e_lease_type %u, will_overwrite: %u\n",
1887 (unsigned)i
, (unsigned)e_lease_type
,
1888 (unsigned)will_overwrite
));
1890 if (lease
!= NULL
&& l
!= NULL
) {
1893 ign
= smb2_lease_equal(fsp_client_guid(fsp
),
1902 if ((e_lease_type
& ~break_to
) == 0) {
1903 if (l
!= NULL
&& l
->breaking
) {
1909 if (share_mode_stale_pid(d
, i
)) {
1913 if (will_overwrite
) {
1915 * If we break anyway break to NONE directly.
1916 * Otherwise vfs_set_filelen() will trigger the
1919 break_to
&= ~(SMB2_LEASE_READ
|SMB2_LEASE_WRITE
);
1922 if (e
->op_type
!= LEASE_OPLOCK
) {
1924 * Oplocks only support breaking to R or NONE.
1926 break_to
&= ~(SMB2_LEASE_HANDLE
|SMB2_LEASE_WRITE
);
1929 DEBUG(10, ("breaking from %d to %d\n",
1930 (int)e_lease_type
, (int)break_to
));
1931 send_break_message(fsp
->conn
->sconn
->msg_ctx
, &fsp
->file_id
,
1933 if (e_lease_type
& delay_mask
) {
1936 if (l
!= NULL
&& l
->breaking
&& !first_open_attempt
) {
1945 static bool file_has_brlocks(files_struct
*fsp
)
1947 struct byte_range_lock
*br_lck
;
1949 br_lck
= brl_get_locks_readonly(fsp
);
1953 return (brl_num_locks(br_lck
) > 0);
1956 int find_share_mode_lease(struct share_mode_data
*d
,
1957 const struct GUID
*client_guid
,
1958 const struct smb2_lease_key
*key
)
1962 for (i
=0; i
<d
->num_leases
; i
++) {
1963 struct share_mode_lease
*l
= &d
->leases
[i
];
1965 if (smb2_lease_equal(client_guid
,
1976 struct fsp_lease
*find_fsp_lease(struct files_struct
*new_fsp
,
1977 const struct smb2_lease_key
*key
,
1978 const struct share_mode_lease
*l
)
1980 struct files_struct
*fsp
;
1983 * TODO: Measure how expensive this loop is with thousands of open
1987 for (fsp
= file_find_di_first(new_fsp
->conn
->sconn
, new_fsp
->file_id
);
1989 fsp
= file_find_di_next(fsp
)) {
1991 if (fsp
== new_fsp
) {
1994 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
1997 if (smb2_lease_key_equal(&fsp
->lease
->lease
.lease_key
, key
)) {
1998 fsp
->lease
->ref_count
+= 1;
2003 /* Not found - must be leased in another smbd. */
2004 new_fsp
->lease
= talloc_zero(new_fsp
->conn
->sconn
, struct fsp_lease
);
2005 if (new_fsp
->lease
== NULL
) {
2008 new_fsp
->lease
->ref_count
= 1;
2009 new_fsp
->lease
->sconn
= new_fsp
->conn
->sconn
;
2010 new_fsp
->lease
->lease
.lease_key
= *key
;
2011 new_fsp
->lease
->lease
.lease_state
= l
->current_state
;
2013 * We internally treat all leases as V2 and update
2014 * the epoch, but when sending breaks it matters if
2015 * the requesting lease was v1 or v2.
2017 new_fsp
->lease
->lease
.lease_version
= l
->lease_version
;
2018 new_fsp
->lease
->lease
.lease_epoch
= l
->epoch
;
2019 return new_fsp
->lease
;
2022 static NTSTATUS
grant_fsp_lease(struct files_struct
*fsp
,
2023 struct share_mode_lock
*lck
,
2024 const struct smb2_lease
*lease
,
2025 uint32_t *p_lease_idx
,
2028 struct share_mode_data
*d
= lck
->data
;
2029 const struct GUID
*client_guid
= fsp_client_guid(fsp
);
2030 struct share_mode_lease
*tmp
;
2034 idx
= find_share_mode_lease(d
, client_guid
, &lease
->lease_key
);
2037 struct share_mode_lease
*l
= &d
->leases
[idx
];
2039 uint32_t existing
, requested
;
2041 fsp
->lease
= find_fsp_lease(fsp
, &lease
->lease_key
, l
);
2042 if (fsp
->lease
== NULL
) {
2043 DEBUG(1, ("Did not find existing lease for file %s\n",
2045 return NT_STATUS_NO_MEMORY
;
2051 * Upgrade only if the requested lease is a strict upgrade.
2053 existing
= l
->current_state
;
2054 requested
= lease
->lease_state
;
2057 * Tricky: This test makes sure that "requested" is a
2058 * strict bitwise superset of "existing".
2060 do_upgrade
= ((existing
& requested
) == existing
);
2063 * Upgrade only if there's a change.
2065 do_upgrade
&= (granted
!= existing
);
2068 * Upgrade only if other leases don't prevent what was asked
2071 do_upgrade
&= (granted
== requested
);
2074 * only upgrade if we are not in breaking state
2076 do_upgrade
&= !l
->breaking
;
2078 DEBUG(10, ("existing=%"PRIu32
", requested=%"PRIu32
", "
2079 "granted=%"PRIu32
", do_upgrade=%d\n",
2080 existing
, requested
, granted
, (int)do_upgrade
));
2083 l
->current_state
= granted
;
2087 /* Ensure we're in sync with current lease state. */
2088 fsp_lease_update(lck
, fsp_client_guid(fsp
), fsp
->lease
);
2089 return NT_STATUS_OK
;
2096 tmp
= talloc_realloc(d
, d
->leases
, struct share_mode_lease
,
2102 return NT_STATUS_INSUFFICIENT_RESOURCES
;
2106 fsp
->lease
= talloc_zero(fsp
->conn
->sconn
, struct fsp_lease
);
2107 if (fsp
->lease
== NULL
) {
2108 return NT_STATUS_INSUFFICIENT_RESOURCES
;
2110 fsp
->lease
->ref_count
= 1;
2111 fsp
->lease
->sconn
= fsp
->conn
->sconn
;
2112 fsp
->lease
->lease
.lease_version
= lease
->lease_version
;
2113 fsp
->lease
->lease
.lease_key
= lease
->lease_key
;
2114 fsp
->lease
->lease
.lease_state
= granted
;
2115 fsp
->lease
->lease
.lease_epoch
= lease
->lease_epoch
+ 1;
2117 *p_lease_idx
= d
->num_leases
;
2119 d
->leases
[d
->num_leases
] = (struct share_mode_lease
) {
2120 .client_guid
= *client_guid
,
2121 .lease_key
= fsp
->lease
->lease
.lease_key
,
2122 .current_state
= fsp
->lease
->lease
.lease_state
,
2123 .lease_version
= fsp
->lease
->lease
.lease_version
,
2124 .epoch
= fsp
->lease
->lease
.lease_epoch
,
2127 status
= leases_db_add(client_guid
,
2130 fsp
->conn
->connectpath
,
2131 fsp
->fsp_name
->base_name
,
2132 fsp
->fsp_name
->stream_name
);
2133 if (!NT_STATUS_IS_OK(status
)) {
2134 DEBUG(10, ("%s: leases_db_add failed: %s\n", __func__
,
2135 nt_errstr(status
)));
2136 TALLOC_FREE(fsp
->lease
);
2137 return NT_STATUS_INSUFFICIENT_RESOURCES
;
2143 return NT_STATUS_OK
;
2146 static bool is_same_lease(const files_struct
*fsp
,
2147 const struct share_mode_data
*d
,
2148 const struct share_mode_entry
*e
,
2149 const struct smb2_lease
*lease
)
2151 if (e
->op_type
!= LEASE_OPLOCK
) {
2154 if (lease
== NULL
) {
2158 return smb2_lease_equal(fsp_client_guid(fsp
),
2160 &d
->leases
[e
->lease_idx
].client_guid
,
2161 &d
->leases
[e
->lease_idx
].lease_key
);
2164 static NTSTATUS
grant_fsp_oplock_type(struct smb_request
*req
,
2165 struct files_struct
*fsp
,
2166 struct share_mode_lock
*lck
,
2168 struct smb2_lease
*lease
)
2170 struct share_mode_data
*d
= lck
->data
;
2171 bool got_handle_lease
= false;
2172 bool got_oplock
= false;
2175 uint32_t lease_idx
= UINT32_MAX
;
2179 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
2180 /* No oplocks on internal open. */
2181 oplock_request
= NO_OPLOCK
;
2182 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
2183 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
2186 if (oplock_request
== LEASE_OPLOCK
) {
2187 if (lease
== NULL
) {
2189 * The SMB2 layer should have checked this
2191 return NT_STATUS_INTERNAL_ERROR
;
2194 granted
= lease
->lease_state
;
2196 if (lp_kernel_oplocks(SNUM(fsp
->conn
))) {
2197 DEBUG(10, ("No lease granted because kernel oplocks are enabled\n"));
2198 granted
= SMB2_LEASE_NONE
;
2200 if ((granted
& (SMB2_LEASE_READ
|SMB2_LEASE_WRITE
)) == 0) {
2201 DEBUG(10, ("No read or write lease requested\n"));
2202 granted
= SMB2_LEASE_NONE
;
2204 if (granted
== SMB2_LEASE_WRITE
) {
2205 DEBUG(10, ("pure write lease requested\n"));
2206 granted
= SMB2_LEASE_NONE
;
2208 if (granted
== (SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
)) {
2209 DEBUG(10, ("write and handle lease requested\n"));
2210 granted
= SMB2_LEASE_NONE
;
2213 granted
= map_oplock_to_lease_type(
2214 oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
2217 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
2218 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
2220 granted
&= ~SMB2_LEASE_READ
;
2223 for (i
=0; i
<d
->num_share_modes
; i
++) {
2224 struct share_mode_entry
*e
= &d
->share_modes
[i
];
2225 uint32_t e_lease_type
;
2227 e_lease_type
= get_lease_type(d
, e
);
2229 if ((granted
& SMB2_LEASE_WRITE
) &&
2230 !is_same_lease(fsp
, d
, e
, lease
) &&
2231 !share_mode_stale_pid(d
, i
)) {
2233 * Can grant only one writer
2235 granted
&= ~SMB2_LEASE_WRITE
;
2238 if ((e_lease_type
& SMB2_LEASE_HANDLE
) && !got_handle_lease
&&
2239 !share_mode_stale_pid(d
, i
)) {
2240 got_handle_lease
= true;
2243 if ((e
->op_type
!= LEASE_OPLOCK
) && !got_oplock
&&
2244 !share_mode_stale_pid(d
, i
)) {
2249 if ((granted
& SMB2_LEASE_READ
) && !(granted
& SMB2_LEASE_WRITE
)) {
2251 (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
2252 lp_level2_oplocks(SNUM(fsp
->conn
));
2254 if (!allow_level2
) {
2255 granted
= SMB2_LEASE_NONE
;
2259 if (oplock_request
== LEASE_OPLOCK
) {
2261 granted
&= ~SMB2_LEASE_HANDLE
;
2264 fsp
->oplock_type
= LEASE_OPLOCK
;
2266 status
= grant_fsp_lease(fsp
, lck
, lease
, &lease_idx
,
2268 if (!NT_STATUS_IS_OK(status
)) {
2272 *lease
= fsp
->lease
->lease
;
2273 DEBUG(10, ("lease_state=%d\n", lease
->lease_state
));
2275 if (got_handle_lease
) {
2276 granted
= SMB2_LEASE_NONE
;
2280 case SMB2_LEASE_READ
|SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
:
2281 fsp
->oplock_type
= BATCH_OPLOCK
|EXCLUSIVE_OPLOCK
;
2283 case SMB2_LEASE_READ
|SMB2_LEASE_WRITE
:
2284 fsp
->oplock_type
= EXCLUSIVE_OPLOCK
;
2286 case SMB2_LEASE_READ
|SMB2_LEASE_HANDLE
:
2287 case SMB2_LEASE_READ
:
2288 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
2291 fsp
->oplock_type
= NO_OPLOCK
;
2295 status
= set_file_oplock(fsp
);
2296 if (!NT_STATUS_IS_OK(status
)) {
2298 * Could not get the kernel oplock
2300 fsp
->oplock_type
= NO_OPLOCK
;
2304 ok
= set_share_mode(lck
, fsp
, get_current_uid(fsp
->conn
),
2309 return NT_STATUS_NO_MEMORY
;
2312 ok
= update_num_read_oplocks(fsp
, lck
);
2314 del_share_mode(lck
, fsp
);
2315 return NT_STATUS_INTERNAL_ERROR
;
2318 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
2319 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
2321 return NT_STATUS_OK
;
2324 static bool request_timed_out(struct timeval request_time
,
2325 struct timeval timeout
)
2327 struct timeval now
, end_time
;
2329 end_time
= timeval_sum(&request_time
, &timeout
);
2330 return (timeval_compare(&end_time
, &now
) < 0);
2333 static struct deferred_open_record
*deferred_open_record_create(
2334 bool delayed_for_oplocks
,
2338 struct deferred_open_record
*record
= NULL
;
2340 record
= talloc(NULL
, struct deferred_open_record
);
2341 if (record
== NULL
) {
2345 *record
= (struct deferred_open_record
) {
2346 .delayed_for_oplocks
= delayed_for_oplocks
,
2347 .async_open
= async_open
,
2354 struct defer_open_state
{
2355 struct smbXsrv_connection
*xconn
;
2359 static void defer_open_done(struct tevent_req
*req
);
2362 * Defer an open and watch a locking.tdb record
2364 * This defers an open that gets rescheduled once the locking.tdb record watch
2365 * is triggered by a change to the record.
2367 * It is used to defer opens that triggered an oplock break and for the SMB1
2368 * sharing violation delay.
2370 static void defer_open(struct share_mode_lock
*lck
,
2371 struct timeval request_time
,
2372 struct timeval timeout
,
2373 struct smb_request
*req
,
2374 bool delayed_for_oplocks
,
2377 struct deferred_open_record
*open_rec
= NULL
;
2378 struct timeval abs_timeout
;
2379 struct defer_open_state
*watch_state
;
2380 struct tevent_req
*watch_req
;
2383 abs_timeout
= timeval_sum(&request_time
, &timeout
);
2385 DBG_DEBUG("request time [%s] timeout [%s] mid [%" PRIu64
"] "
2386 "delayed_for_oplocks [%s] file_id [%s]\n",
2387 timeval_string(talloc_tos(), &request_time
, false),
2388 timeval_string(talloc_tos(), &abs_timeout
, false),
2390 delayed_for_oplocks
? "yes" : "no",
2391 file_id_string_tos(&id
));
2393 open_rec
= deferred_open_record_create(delayed_for_oplocks
,
2396 if (open_rec
== NULL
) {
2398 exit_server("talloc failed");
2401 watch_state
= talloc(open_rec
, struct defer_open_state
);
2402 if (watch_state
== NULL
) {
2403 exit_server("talloc failed");
2405 watch_state
->xconn
= req
->xconn
;
2406 watch_state
->mid
= req
->mid
;
2408 DBG_DEBUG("defering mid %" PRIu64
"\n", req
->mid
);
2410 watch_req
= dbwrap_watched_watch_send(watch_state
,
2413 (struct server_id
){0});
2414 if (watch_req
== NULL
) {
2415 exit_server("Could not watch share mode record");
2417 tevent_req_set_callback(watch_req
, defer_open_done
, watch_state
);
2419 ok
= tevent_req_set_endtime(watch_req
, req
->sconn
->ev_ctx
, abs_timeout
);
2421 exit_server("tevent_req_set_endtime failed");
2424 ok
= push_deferred_open_message_smb(req
, request_time
, timeout
,
2425 open_rec
->id
, open_rec
);
2428 exit_server("push_deferred_open_message_smb failed");
2432 static void defer_open_done(struct tevent_req
*req
)
2434 struct defer_open_state
*state
= tevent_req_callback_data(
2435 req
, struct defer_open_state
);
2439 status
= dbwrap_watched_watch_recv(req
, NULL
, NULL
);
2441 if (!NT_STATUS_IS_OK(status
)) {
2442 DEBUG(5, ("dbwrap_watched_watch_recv returned %s\n",
2443 nt_errstr(status
)));
2445 * Even if it failed, retry anyway. TODO: We need a way to
2446 * tell a re-scheduled open about that error.
2450 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state
->mid
));
2452 ret
= schedule_deferred_open_message_smb(state
->xconn
, state
->mid
);
2458 * Actually attempt the kernel oplock polling open.
2461 static void kernel_oplock_poll_open_timer(struct tevent_context
*ev
,
2462 struct tevent_timer
*te
,
2463 struct timeval current_time
,
2467 struct smb_request
*req
= (struct smb_request
*)private_data
;
2469 ok
= schedule_deferred_open_message_smb(req
->xconn
, req
->mid
);
2471 exit_server("schedule_deferred_open_message_smb failed");
2473 DBG_DEBUG("kernel_oplock_poll_open_timer fired. Retying open !\n");
2477 * Reschedule an open for 1 second from now, if not timed out.
2479 static void setup_kernel_oplock_poll_open(struct timeval request_time
,
2480 struct smb_request
*req
,
2485 struct deferred_open_record
*open_rec
= NULL
;
2486 /* Maximum wait time. */
2487 struct timeval timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
2489 if (request_timed_out(request_time
, timeout
)) {
2493 open_rec
= deferred_open_record_create(false, false, id
);
2494 if (open_rec
== NULL
) {
2495 exit_server("talloc failed");
2498 ok
= push_deferred_open_message_smb(req
,
2504 exit_server("push_deferred_open_message_smb failed");
2508 * As this timer event is owned by req, it will
2509 * disappear if req it talloc_freed.
2511 open_rec
->te
= tevent_add_timer(req
->sconn
->ev_ctx
,
2513 timeval_current_ofs(1, 0),
2514 kernel_oplock_poll_open_timer
,
2516 if (open_rec
->te
== NULL
) {
2517 exit_server("tevent_add_timer failed");
2520 DBG_DEBUG("poll request time [%s] mid [%" PRIu64
"] file_id [%s]\n",
2521 timeval_string(talloc_tos(), &request_time
, false),
2523 file_id_string_tos(&id
));
2526 /****************************************************************************
2527 On overwrite open ensure that the attributes match.
2528 ****************************************************************************/
2530 static bool open_match_attributes(connection_struct
*conn
,
2531 uint32_t old_dos_attr
,
2532 uint32_t new_dos_attr
,
2533 mode_t new_unx_mode
,
2534 mode_t
*returned_unx_mode
)
2536 uint32_t noarch_old_dos_attr
, noarch_new_dos_attr
;
2538 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
2539 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
2541 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
2542 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
2543 *returned_unx_mode
= new_unx_mode
;
2545 *returned_unx_mode
= (mode_t
)0;
2548 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
2549 "new_dos_attr = 0x%x "
2550 "returned_unx_mode = 0%o\n",
2551 (unsigned int)old_dos_attr
,
2552 (unsigned int)new_dos_attr
,
2553 (unsigned int)*returned_unx_mode
));
2555 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
2556 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
2557 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
2558 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
2562 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
2563 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
2564 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
2571 /****************************************************************************
2572 Special FCB or DOS processing in the case of a sharing violation.
2573 Try and find a duplicated file handle.
2574 ****************************************************************************/
2576 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
2577 connection_struct
*conn
,
2578 files_struct
*fsp_to_dup_into
,
2579 const struct smb_filename
*smb_fname
,
2583 uint32_t access_mask
,
2584 uint32_t share_access
,
2585 uint32_t create_options
)
2589 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
2590 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
2592 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
2593 fsp
= file_find_di_next(fsp
)) {
2595 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
2596 "vuid = %llu, file_pid = %u, private_options = 0x%x "
2597 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
2598 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
2599 (unsigned int)fsp
->file_pid
,
2600 (unsigned int)fsp
->fh
->private_options
,
2601 (unsigned int)fsp
->access_mask
));
2603 if (fsp
!= fsp_to_dup_into
&&
2604 fsp
->fh
->fd
!= -1 &&
2605 fsp
->vuid
== vuid
&&
2606 fsp
->file_pid
== file_pid
&&
2607 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2608 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
2609 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
2610 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
2611 strequal(fsp
->fsp_name
->stream_name
,
2612 smb_fname
->stream_name
)) {
2613 DEBUG(10,("fcb_or_dos_open: file match\n"));
2619 return NT_STATUS_NOT_FOUND
;
2622 /* quite an insane set of semantics ... */
2623 if (is_executable(smb_fname
->base_name
) &&
2624 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
2625 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
2626 return NT_STATUS_INVALID_PARAMETER
;
2629 /* We need to duplicate this fsp. */
2630 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
2631 create_options
, fsp_to_dup_into
);
2634 static void schedule_defer_open(struct share_mode_lock
*lck
,
2636 struct timeval request_time
,
2637 struct smb_request
*req
)
2639 /* This is a relative time, added to the absolute
2640 request_time value to get the absolute timeout time.
2641 Note that if this is the second or greater time we enter
2642 this codepath for this particular request mid then
2643 request_time is left as the absolute time of the *first*
2644 time this request mid was processed. This is what allows
2645 the request to eventually time out. */
2647 struct timeval timeout
;
2649 /* Normally the smbd we asked should respond within
2650 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
2651 * the client did, give twice the timeout as a safety
2652 * measure here in case the other smbd is stuck
2653 * somewhere else. */
2655 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
2657 if (request_timed_out(request_time
, timeout
)) {
2661 defer_open(lck
, request_time
, timeout
, req
, true, id
);
2664 /****************************************************************************
2665 Reschedule an open call that went asynchronous.
2666 ****************************************************************************/
2668 static void schedule_async_open_timer(struct tevent_context
*ev
,
2669 struct tevent_timer
*te
,
2670 struct timeval current_time
,
2673 exit_server("async open timeout");
2676 static void schedule_async_open(struct timeval request_time
,
2677 struct smb_request
*req
)
2679 struct deferred_open_record
*open_rec
= NULL
;
2680 struct timeval timeout
= timeval_set(20, 0);
2683 if (request_timed_out(request_time
, timeout
)) {
2687 open_rec
= deferred_open_record_create(false, true, (struct file_id
){0});
2688 if (open_rec
== NULL
) {
2689 exit_server("deferred_open_record_create failed");
2692 ok
= push_deferred_open_message_smb(req
, request_time
, timeout
,
2693 (struct file_id
){0}, open_rec
);
2695 exit_server("push_deferred_open_message_smb failed");
2698 open_rec
->te
= tevent_add_timer(req
->sconn
->ev_ctx
,
2700 timeval_current_ofs(20, 0),
2701 schedule_async_open_timer
,
2703 if (open_rec
->te
== NULL
) {
2704 exit_server("tevent_add_timer failed");
2708 /****************************************************************************
2709 Work out what access_mask to use from what the client sent us.
2710 ****************************************************************************/
2712 static NTSTATUS
smbd_calculate_maximum_allowed_access(
2713 connection_struct
*conn
,
2714 const struct smb_filename
*smb_fname
,
2716 uint32_t *p_access_mask
)
2718 struct security_descriptor
*sd
;
2719 uint32_t access_granted
;
2722 if (!use_privs
&& (get_current_uid(conn
) == (uid_t
)0)) {
2723 *p_access_mask
|= FILE_GENERIC_ALL
;
2724 return NT_STATUS_OK
;
2727 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
,
2733 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
2735 * File did not exist
2737 *p_access_mask
= FILE_GENERIC_ALL
;
2738 return NT_STATUS_OK
;
2740 if (!NT_STATUS_IS_OK(status
)) {
2741 DEBUG(10,("Could not get acl on file %s: %s\n",
2742 smb_fname_str_dbg(smb_fname
),
2743 nt_errstr(status
)));
2744 return NT_STATUS_ACCESS_DENIED
;
2748 * If we can access the path to this file, by
2749 * default we have FILE_READ_ATTRIBUTES from the
2750 * containing directory. See the section:
2751 * "Algorithm to Check Access to an Existing File"
2754 * se_file_access_check()
2755 * also takes care of owner WRITE_DAC and READ_CONTROL.
2757 status
= se_file_access_check(sd
,
2758 get_current_nttok(conn
),
2760 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
2765 if (!NT_STATUS_IS_OK(status
)) {
2766 DEBUG(10, ("Access denied on file %s: "
2767 "when calculating maximum access\n",
2768 smb_fname_str_dbg(smb_fname
)));
2769 return NT_STATUS_ACCESS_DENIED
;
2771 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
2773 if (!(access_granted
& DELETE_ACCESS
)) {
2774 if (can_delete_file_in_directory(conn
, smb_fname
)) {
2775 *p_access_mask
|= DELETE_ACCESS
;
2779 return NT_STATUS_OK
;
2782 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
2783 const struct smb_filename
*smb_fname
,
2785 uint32_t access_mask
,
2786 uint32_t *access_mask_out
)
2789 uint32_t orig_access_mask
= access_mask
;
2790 uint32_t rejected_share_access
;
2792 if (access_mask
& SEC_MASK_INVALID
) {
2793 DBG_DEBUG("access_mask [%8x] contains invalid bits\n",
2795 return NT_STATUS_ACCESS_DENIED
;
2799 * Convert GENERIC bits to specific bits.
2802 se_map_generic(&access_mask
, &file_generic_mapping
);
2804 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
2805 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
2807 status
= smbd_calculate_maximum_allowed_access(
2808 conn
, smb_fname
, use_privs
, &access_mask
);
2810 if (!NT_STATUS_IS_OK(status
)) {
2814 access_mask
&= conn
->share_access
;
2817 rejected_share_access
= access_mask
& ~(conn
->share_access
);
2819 if (rejected_share_access
) {
2820 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
2821 "file %s: rejected by share access mask[0x%08X] "
2822 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
2823 smb_fname_str_dbg(smb_fname
),
2825 orig_access_mask
, access_mask
,
2826 rejected_share_access
));
2827 return NT_STATUS_ACCESS_DENIED
;
2830 *access_mask_out
= access_mask
;
2831 return NT_STATUS_OK
;
2834 /****************************************************************************
2835 Remove the deferred open entry under lock.
2836 ****************************************************************************/
2838 /****************************************************************************
2839 Return true if this is a state pointer to an asynchronous create.
2840 ****************************************************************************/
2842 bool is_deferred_open_async(const struct deferred_open_record
*rec
)
2844 return rec
->async_open
;
2847 static bool clear_ads(uint32_t create_disposition
)
2851 switch (create_disposition
) {
2852 case FILE_SUPERSEDE
:
2853 case FILE_OVERWRITE_IF
:
2854 case FILE_OVERWRITE
:
2863 static int disposition_to_open_flags(uint32_t create_disposition
)
2868 * Currently we're using FILE_SUPERSEDE as the same as
2869 * FILE_OVERWRITE_IF but they really are
2870 * different. FILE_SUPERSEDE deletes an existing file
2871 * (requiring delete access) then recreates it.
2874 switch (create_disposition
) {
2875 case FILE_SUPERSEDE
:
2876 case FILE_OVERWRITE_IF
:
2878 * If file exists replace/overwrite. If file doesn't
2881 ret
= O_CREAT
|O_TRUNC
;
2886 * If file exists open. If file doesn't exist error.
2891 case FILE_OVERWRITE
:
2893 * If file exists overwrite. If file doesn't exist
2901 * If file exists error. If file doesn't exist create.
2903 ret
= O_CREAT
|O_EXCL
;
2908 * If file exists open. If file doesn't exist create.
2916 static int calculate_open_access_flags(uint32_t access_mask
,
2917 uint32_t private_flags
)
2919 bool need_write
, need_read
;
2922 * Note that we ignore the append flag as append does not
2923 * mean the same thing under DOS and Unix.
2926 need_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
));
2931 /* DENY_DOS opens are always underlying read-write on the
2932 file handle, no matter what the requested access mask
2936 ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
2937 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|
2938 FILE_READ_EA
|FILE_EXECUTE
));
2946 /****************************************************************************
2947 Open a file with a share mode. Passed in an already created files_struct *.
2948 ****************************************************************************/
2950 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
2951 struct smb_request
*req
,
2952 uint32_t access_mask
, /* access bits (FILE_READ_DATA etc.) */
2953 uint32_t share_access
, /* share constants (FILE_SHARE_READ etc) */
2954 uint32_t create_disposition
, /* FILE_OPEN_IF etc. */
2955 uint32_t create_options
, /* options such as delete on close. */
2956 uint32_t new_dos_attributes
, /* attributes used for new file. */
2957 int oplock_request
, /* internal Samba oplock codes. */
2958 struct smb2_lease
*lease
,
2959 /* Information (FILE_EXISTS etc.) */
2960 uint32_t private_flags
, /* Samba specific flags. */
2964 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
2967 bool file_existed
= VALID_STAT(smb_fname
->st
);
2968 bool def_acl
= False
;
2969 bool posix_open
= False
;
2970 bool new_file_created
= False
;
2971 bool first_open_attempt
= true;
2972 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
2973 mode_t new_unx_mode
= (mode_t
)0;
2974 mode_t unx_mode
= (mode_t
)0;
2976 uint32_t existing_dos_attributes
= 0;
2977 struct timeval request_time
= timeval_zero();
2978 struct share_mode_lock
*lck
= NULL
;
2979 uint32_t open_access_mask
= access_mask
;
2982 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
2983 struct timespec old_write_time
;
2986 if (conn
->printer
) {
2988 * Printers are handled completely differently.
2989 * Most of the passed parameters are ignored.
2993 *pinfo
= FILE_WAS_CREATED
;
2996 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2997 smb_fname_str_dbg(smb_fname
)));
3000 DEBUG(0,("open_file_ntcreate: printer open without "
3001 "an SMB request!\n"));
3002 return NT_STATUS_INTERNAL_ERROR
;
3005 return print_spool_open(fsp
, smb_fname
->base_name
,
3009 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
3011 return NT_STATUS_NO_MEMORY
;
3014 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
3016 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
3017 new_dos_attributes
= 0;
3019 /* Windows allows a new file to be created and
3020 silently removes a FILE_ATTRIBUTE_DIRECTORY
3021 sent by the client. Do the same. */
3023 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
3025 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
3027 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
3028 smb_fname
, parent_dir
);
3031 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
3032 "access_mask=0x%x share_access=0x%x "
3033 "create_disposition = 0x%x create_options=0x%x "
3034 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
3035 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
3036 access_mask
, share_access
, create_disposition
,
3037 create_options
, (unsigned int)unx_mode
, oplock_request
,
3038 (unsigned int)private_flags
));
3041 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
3042 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) != 0));
3044 /* And req != NULL means no INTERNAL_OPEN_ONLY */
3045 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) == 0));
3049 * Only non-internal opens can be deferred at all
3053 struct deferred_open_record
*open_rec
;
3054 if (get_deferred_open_message_state(req
,
3057 /* Remember the absolute time of the original
3058 request with this mid. We'll use it later to
3059 see if this has timed out. */
3061 /* If it was an async create retry, the file
3064 if (is_deferred_open_async(open_rec
)) {
3065 SET_STAT_INVALID(smb_fname
->st
);
3066 file_existed
= false;
3069 /* Ensure we don't reprocess this message. */
3070 remove_deferred_open_message_smb(req
->xconn
, req
->mid
);
3072 first_open_attempt
= false;
3077 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
3080 * Only use strored DOS attributes for checks
3081 * against requested attributes (below via
3082 * open_match_attributes()), cf bug #11992
3083 * for details. -slow
3087 status
= SMB_VFS_GET_DOS_ATTRIBUTES(conn
, smb_fname
, &attr
);
3088 if (NT_STATUS_IS_OK(status
)) {
3089 existing_dos_attributes
= attr
;
3094 /* ignore any oplock requests if oplocks are disabled */
3095 if (!lp_oplocks(SNUM(conn
)) ||
3096 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
3097 /* Mask off everything except the private Samba bits. */
3098 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
3101 /* this is for OS/2 long file names - say we don't support them */
3102 if (req
!= NULL
&& !req
->posix_pathnames
&&
3103 strstr(smb_fname
->base_name
,".+,;=[].")) {
3104 /* OS/2 Workplace shell fix may be main code stream in a later
3106 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
3108 if (use_nt_status()) {
3109 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3111 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
3114 switch( create_disposition
) {
3116 /* If file exists open. If file doesn't exist error. */
3117 if (!file_existed
) {
3118 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
3119 "requested for file %s and file "
3121 smb_fname_str_dbg(smb_fname
)));
3123 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3127 case FILE_OVERWRITE
:
3128 /* If file exists overwrite. If file doesn't exist
3130 if (!file_existed
) {
3131 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
3132 "requested for file %s and file "
3134 smb_fname_str_dbg(smb_fname
) ));
3136 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3141 /* If file exists error. If file doesn't exist
3144 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
3145 "requested for file %s and file "
3146 "already exists.\n",
3147 smb_fname_str_dbg(smb_fname
)));
3148 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
3153 return map_nt_error_from_unix(errno
);
3157 case FILE_SUPERSEDE
:
3158 case FILE_OVERWRITE_IF
:
3162 return NT_STATUS_INVALID_PARAMETER
;
3165 flags2
= disposition_to_open_flags(create_disposition
);
3167 /* We only care about matching attributes on file exists and
3170 if (!posix_open
&& file_existed
&&
3171 ((create_disposition
== FILE_OVERWRITE
) ||
3172 (create_disposition
== FILE_OVERWRITE_IF
))) {
3173 if (!open_match_attributes(conn
, existing_dos_attributes
,
3175 unx_mode
, &new_unx_mode
)) {
3176 DEBUG(5,("open_file_ntcreate: attributes mismatch "
3177 "for file %s (%x %x) (0%o, 0%o)\n",
3178 smb_fname_str_dbg(smb_fname
),
3179 existing_dos_attributes
,
3181 (unsigned int)smb_fname
->st
.st_ex_mode
,
3182 (unsigned int)unx_mode
));
3184 return NT_STATUS_ACCESS_DENIED
;
3188 status
= smbd_calculate_access_mask(conn
, smb_fname
,
3192 if (!NT_STATUS_IS_OK(status
)) {
3193 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
3194 "on file %s returned %s\n",
3195 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
3199 open_access_mask
= access_mask
;
3201 if (flags2
& O_TRUNC
) {
3202 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
3205 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
3206 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
3210 * Note that we ignore the append flag as append does not
3211 * mean the same thing under DOS and Unix.
3214 flags
= calculate_open_access_flags(access_mask
, private_flags
);
3217 * Currently we only look at FILE_WRITE_THROUGH for create options.
3221 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
3226 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
3230 if (!posix_open
&& !CAN_WRITE(conn
)) {
3232 * We should really return a permission denied error if either
3233 * O_CREAT or O_TRUNC are set, but for compatibility with
3234 * older versions of Samba we just AND them out.
3236 flags2
&= ~(O_CREAT
|O_TRUNC
);
3239 if (lp_kernel_oplocks(SNUM(conn
))) {
3241 * With kernel oplocks the open breaking an oplock
3242 * blocks until the oplock holder has given up the
3243 * oplock or closed the file. We prevent this by always
3244 * trying to open the file with O_NONBLOCK (see "man
3247 * If a process that doesn't use the smbd open files
3248 * database or communication methods holds a kernel
3249 * oplock we must periodically poll for available open
3252 flags2
|= O_NONBLOCK
;
3256 * Ensure we can't write on a read-only share or file.
3259 if (flags
!= O_RDONLY
&& file_existed
&&
3260 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
3261 DEBUG(5,("open_file_ntcreate: write access requested for "
3262 "file %s on read only %s\n",
3263 smb_fname_str_dbg(smb_fname
),
3264 !CAN_WRITE(conn
) ? "share" : "file" ));
3266 return NT_STATUS_ACCESS_DENIED
;
3269 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
3270 fsp
->share_access
= share_access
;
3271 fsp
->fh
->private_options
= private_flags
;
3272 fsp
->access_mask
= open_access_mask
; /* We change this to the
3273 * requested access_mask after
3274 * the open is done. */
3276 fsp
->posix_flags
|= FSP_POSIX_FLAGS_ALL
;
3279 if (timeval_is_zero(&request_time
)) {
3280 request_time
= fsp
->open_time
;
3284 * Ensure we pay attention to default ACLs on directories if required.
3287 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
3288 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
3289 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
3292 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
3293 "access_mask = 0x%x, open_access_mask = 0x%x\n",
3294 (unsigned int)flags
, (unsigned int)flags2
,
3295 (unsigned int)unx_mode
, (unsigned int)access_mask
,
3296 (unsigned int)open_access_mask
));
3298 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
3299 flags
|flags2
, unx_mode
, access_mask
,
3300 open_access_mask
, &new_file_created
);
3302 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_NETWORK_BUSY
)) {
3306 * This handles the kernel oplock case:
3308 * the file has an active kernel oplock and the open() returned
3309 * EWOULDBLOCK/EAGAIN which maps to NETWORK_BUSY.
3311 * "Samba locking.tdb oplocks" are handled below after acquiring
3312 * the sharemode lock with get_share_mode_lock().
3314 if (file_existed
&& S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
)) {
3315 DEBUG(10, ("FIFO busy\n"));
3316 return NT_STATUS_NETWORK_BUSY
;
3319 DEBUG(10, ("Internal open busy\n"));
3320 return NT_STATUS_NETWORK_BUSY
;
3324 * From here on we assume this is an oplock break triggered
3327 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
3330 * No oplock from Samba around. Set up a poll every 1
3331 * second to retry a non-blocking open until the time
3334 setup_kernel_oplock_poll_open(request_time
,
3337 DBG_DEBUG("No Samba oplock around after EWOULDBLOCK. "
3338 "Retrying with poll\n");
3339 return NT_STATUS_SHARING_VIOLATION
;
3342 if (!validate_oplock_types(lck
)) {
3343 smb_panic("validate_oplock_types failed");
3346 delay
= delay_for_oplock(fsp
, 0, lease
, lck
, false,
3348 first_open_attempt
);
3350 schedule_defer_open(lck
, fsp
->file_id
, request_time
,
3353 DEBUG(10, ("Sent oplock break request to kernel "
3354 "oplock holder\n"));
3355 return NT_STATUS_SHARING_VIOLATION
;
3359 * No oplock from Samba around. Set up a poll every 1
3360 * second to retry a non-blocking open until the time
3363 setup_kernel_oplock_poll_open(request_time
, req
, fsp
->file_id
);
3366 DBG_DEBUG("No Samba oplock around after EWOULDBLOCK. "
3367 "Retrying with poll\n");
3368 return NT_STATUS_SHARING_VIOLATION
;
3371 if (!NT_STATUS_IS_OK(fsp_open
)) {
3372 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
3373 schedule_async_open(request_time
, req
);
3378 if (new_file_created
) {
3380 * As we atomically create using O_CREAT|O_EXCL,
3381 * then if new_file_created is true, then
3382 * file_existed *MUST* have been false (even
3383 * if the file was previously detected as being
3386 file_existed
= false;
3389 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
3391 * The file did exist, but some other (local or NFS)
3392 * process either renamed/unlinked and re-created the
3393 * file with different dev/ino after we walked the path,
3394 * but before we did the open. We could retry the
3395 * open but it's a rare enough case it's easier to
3396 * just fail the open to prevent creating any problems
3397 * in the open file db having the wrong dev/ino key.
3400 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
3401 "Old (dev=0x%llu, ino =0x%llu). "
3402 "New (dev=0x%llu, ino=0x%llu). Failing open "
3403 " with NT_STATUS_ACCESS_DENIED.\n",
3404 smb_fname_str_dbg(smb_fname
),
3405 (unsigned long long)saved_stat
.st_ex_dev
,
3406 (unsigned long long)saved_stat
.st_ex_ino
,
3407 (unsigned long long)smb_fname
->st
.st_ex_dev
,
3408 (unsigned long long)smb_fname
->st
.st_ex_ino
));
3409 return NT_STATUS_ACCESS_DENIED
;
3412 old_write_time
= smb_fname
->st
.st_ex_mtime
;
3415 * Deal with the race condition where two smbd's detect the
3416 * file doesn't exist and do the create at the same time. One
3417 * of them will win and set a share mode, the other (ie. this
3418 * one) should check if the requested share mode for this
3419 * create is allowed.
3423 * Now the file exists and fsp is successfully opened,
3424 * fsp->dev and fsp->inode are valid and should replace the
3425 * dev=0,inode=0 from a non existent file. Spotted by
3426 * Nadav Danieli <nadavd@exanet.com>. JRA.
3431 lck
= get_share_mode_lock(talloc_tos(), id
,
3433 smb_fname
, &old_write_time
);
3436 DEBUG(0, ("open_file_ntcreate: Could not get share "
3437 "mode lock for %s\n",
3438 smb_fname_str_dbg(smb_fname
)));
3440 return NT_STATUS_SHARING_VIOLATION
;
3443 /* Get the types we need to examine. */
3444 if (!validate_oplock_types(lck
)) {
3445 smb_panic("validate_oplock_types failed");
3448 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
3451 return NT_STATUS_DELETE_PENDING
;
3454 status
= open_mode_check(conn
, lck
,
3455 access_mask
, share_access
);
3457 if (NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
) ||
3458 (lck
->data
->num_share_modes
> 0)) {
3460 * This comes from ancient times out of open_mode_check. I
3461 * have no clue whether this is still necessary. I can't think
3462 * of a case where this would actually matter further down in
3463 * this function. I leave it here for further investigation
3466 file_existed
= true;
3471 * Handle oplocks, deferring the request if delay_for_oplock()
3472 * triggered a break message and we have to wait for the break
3476 bool sharing_violation
= NT_STATUS_EQUAL(
3477 status
, NT_STATUS_SHARING_VIOLATION
);
3479 delay
= delay_for_oplock(fsp
, oplock_request
, lease
, lck
,
3482 first_open_attempt
);
3484 schedule_defer_open(lck
, fsp
->file_id
,
3488 return NT_STATUS_SHARING_VIOLATION
;
3492 if (!NT_STATUS_IS_OK(status
)) {
3493 uint32_t can_access_mask
;
3494 bool can_access
= True
;
3496 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
3498 /* Check if this can be done with the deny_dos and fcb
3501 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
3502 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
3504 DEBUG(0, ("DOS open without an SMB "
3508 return NT_STATUS_INTERNAL_ERROR
;
3511 /* Use the client requested access mask here,
3512 * not the one we open with. */
3513 status
= fcb_or_dos_open(req
,
3524 if (NT_STATUS_IS_OK(status
)) {
3527 *pinfo
= FILE_WAS_OPENED
;
3529 return NT_STATUS_OK
;
3534 * This next line is a subtlety we need for
3535 * MS-Access. If a file open will fail due to share
3536 * permissions and also for security (access) reasons,
3537 * we need to return the access failed error, not the
3538 * share error. We can't open the file due to kernel
3539 * oplock deadlock (it's possible we failed above on
3540 * the open_mode_check()) so use a userspace check.
3543 if (flags
& O_RDWR
) {
3544 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
3545 } else if (flags
& O_WRONLY
) {
3546 can_access_mask
= FILE_WRITE_DATA
;
3548 can_access_mask
= FILE_READ_DATA
;
3551 if (((can_access_mask
& FILE_WRITE_DATA
) &&
3552 !CAN_WRITE(conn
)) ||
3553 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
3556 can_access_mask
))) {
3561 * If we're returning a share violation, ensure we
3562 * cope with the braindead 1 second delay (SMB1 only).
3565 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
3566 !conn
->sconn
->using_smb2
&&
3567 lp_defer_sharing_violations()) {
3568 struct timeval timeout
;
3571 /* this is a hack to speed up torture tests
3573 timeout_usecs
= lp_parm_int(SNUM(conn
),
3574 "smbd","sharedelay",
3575 SHARING_VIOLATION_USEC_WAIT
);
3577 /* This is a relative time, added to the absolute
3578 request_time value to get the absolute timeout time.
3579 Note that if this is the second or greater time we enter
3580 this codepath for this particular request mid then
3581 request_time is left as the absolute time of the *first*
3582 time this request mid was processed. This is what allows
3583 the request to eventually time out. */
3585 timeout
= timeval_set(0, timeout_usecs
);
3587 if (!request_timed_out(request_time
, timeout
)) {
3588 defer_open(lck
, request_time
, timeout
, req
,
3597 * We have detected a sharing violation here
3598 * so return the correct error code
3600 status
= NT_STATUS_SHARING_VIOLATION
;
3602 status
= NT_STATUS_ACCESS_DENIED
;
3607 /* Should we atomically (to the client at least) truncate ? */
3608 if ((!new_file_created
) &&
3609 (flags2
& O_TRUNC
) &&
3610 (!S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
))) {
3613 ret
= vfs_set_filelen(fsp
, 0);
3615 status
= map_nt_error_from_unix(errno
);
3623 * We have the share entry *locked*.....
3626 /* Delete streams if create_disposition requires it */
3627 if (!new_file_created
&& clear_ads(create_disposition
) &&
3628 !is_ntfs_stream_smb_fname(smb_fname
)) {
3629 status
= delete_all_streams(conn
, smb_fname
);
3630 if (!NT_STATUS_IS_OK(status
)) {
3637 /* note that we ignore failure for the following. It is
3638 basically a hack for NFS, and NFS will never set one of
3639 these only read them. Nobody but Samba can ever set a deny
3640 mode and we have already checked our more authoritative
3641 locking database for permission to set this deny mode. If
3642 the kernel refuses the operations then the kernel is wrong.
3643 note that GPFS supports it as well - jmcd */
3645 if (fsp
->fh
->fd
!= -1 && lp_kernel_share_modes(SNUM(conn
))) {
3648 * Beware: streams implementing VFS modules may
3649 * implement streams in a way that fsp will have the
3650 * basefile open in the fsp fd, so lacking a distinct
3651 * fd for the stream kernel_flock will apply on the
3652 * basefile which is wrong. The actual check is
3653 * deffered to the VFS module implementing the
3654 * kernel_flock call.
3656 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
3657 if(ret_flock
== -1 ){
3662 return NT_STATUS_SHARING_VIOLATION
;
3665 fsp
->kernel_share_modes_taken
= true;
3669 * At this point onwards, we can guarantee that the share entry
3670 * is locked, whether we created the file or not, and that the
3671 * deny mode is compatible with all current opens.
3675 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3676 * but we don't have to store this - just ignore it on access check.
3678 if (conn
->sconn
->using_smb2
) {
3680 * SMB2 doesn't return it (according to Microsoft tests).
3681 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
3682 * File created with access = 0x7 (Read, Write, Delete)
3683 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
3685 fsp
->access_mask
= access_mask
;
3687 /* But SMB1 does. */
3688 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3693 * stat opens on existing files don't get oplocks.
3694 * They can get leases.
3696 * Note that we check for stat open on the *open_access_mask*,
3697 * i.e. the access mask we actually used to do the open,
3698 * not the one the client asked for (which is in
3699 * fsp->access_mask). This is due to the fact that
3700 * FILE_OVERWRITE and FILE_OVERWRITE_IF add in O_TRUNC,
3701 * which adds FILE_WRITE_DATA to open_access_mask.
3703 if (is_stat_open(open_access_mask
) && lease
== NULL
) {
3704 oplock_request
= NO_OPLOCK
;
3708 if (new_file_created
) {
3709 info
= FILE_WAS_CREATED
;
3711 if (flags2
& O_TRUNC
) {
3712 info
= FILE_WAS_OVERWRITTEN
;
3714 info
= FILE_WAS_OPENED
;
3723 * Setup the oplock info in both the shared memory and
3726 status
= grant_fsp_oplock_type(req
, fsp
, lck
, oplock_request
, lease
);
3727 if (!NT_STATUS_IS_OK(status
)) {
3733 /* Handle strange delete on close create semantics. */
3734 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3736 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
3738 if (!NT_STATUS_IS_OK(status
)) {
3739 /* Remember to delete the mode we just added. */
3740 del_share_mode(lck
, fsp
);
3745 /* Note that here we set the *inital* delete on close flag,
3746 not the regular one. The magic gets handled in close. */
3747 fsp
->initial_delete_on_close
= True
;
3750 if (info
!= FILE_WAS_OPENED
) {
3751 /* Overwritten files should be initially set as archive */
3752 if ((info
== FILE_WAS_OVERWRITTEN
&& lp_map_archive(SNUM(conn
))) ||
3753 lp_store_dos_attributes(SNUM(conn
))) {
3755 if (file_set_dosmode(conn
, smb_fname
,
3756 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
3757 parent_dir
, true) == 0) {
3758 unx_mode
= smb_fname
->st
.st_ex_mode
;
3764 /* Determine sparse flag. */
3766 /* POSIX opens are sparse by default. */
3767 fsp
->is_sparse
= true;
3769 fsp
->is_sparse
= (file_existed
&&
3770 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
3774 * Take care of inherited ACLs on created files - if default ACL not
3778 if (!posix_open
&& new_file_created
&& !def_acl
) {
3780 int saved_errno
= errno
; /* We might get ENOSYS in the next
3783 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
3785 errno
= saved_errno
; /* Ignore ENOSYS */
3788 } else if (new_unx_mode
) {
3790 * We only get here in the case of:
3792 * a). Not a POSIX open.
3793 * b). File already existed.
3794 * c). File was overwritten.
3795 * d). Requested DOS attributes didn't match
3796 * the DOS attributes on the existing file.
3798 * In that case new_unx_mode has been set
3799 * equal to the calculated mode (including
3800 * possible inheritance of the mode from the
3801 * containing directory).
3803 * Note this mode was calculated with the
3804 * DOS attribute FILE_ATTRIBUTE_ARCHIVE added,
3805 * so the mode change here is suitable for
3806 * an overwritten file.
3811 /* Attributes need changing. File already existed. */
3814 int saved_errno
= errno
; /* We might get ENOSYS in the
3816 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
3818 if (ret
== -1 && errno
== ENOSYS
) {
3819 errno
= saved_errno
; /* Ignore ENOSYS */
3821 DEBUG(5, ("open_file_ntcreate: reset "
3822 "attributes of file %s to 0%o\n",
3823 smb_fname_str_dbg(smb_fname
),
3824 (unsigned int)new_unx_mode
));
3825 ret
= 0; /* Don't do the fchmod below. */
3830 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
3831 DEBUG(5, ("open_file_ntcreate: failed to reset "
3832 "attributes of file %s to 0%o\n",
3833 smb_fname_str_dbg(smb_fname
),
3834 (unsigned int)new_unx_mode
));
3839 * Deal with other opens having a modified write time.
3841 struct timespec write_time
= get_share_mode_write_time(lck
);
3843 if (!null_timespec(write_time
)) {
3844 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
3850 return NT_STATUS_OK
;
3853 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
3854 struct smb_filename
*smb_dname
,
3855 uint32_t file_attributes
)
3858 char *parent_dir
= NULL
;
3860 bool posix_open
= false;
3861 bool need_re_stat
= false;
3862 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
3864 if (!CAN_WRITE(conn
) || (access_mask
& ~(conn
->share_access
))) {
3865 DEBUG(5,("mkdir_internal: failing share access "
3866 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
3867 return NT_STATUS_ACCESS_DENIED
;
3870 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
3872 return NT_STATUS_NO_MEMORY
;
3875 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
3877 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
3879 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
3882 status
= check_parent_access(conn
,
3885 if(!NT_STATUS_IS_OK(status
)) {
3886 DEBUG(5,("mkdir_internal: check_parent_access "
3887 "on directory %s for path %s returned %s\n",
3889 smb_dname
->base_name
,
3890 nt_errstr(status
) ));
3894 if (SMB_VFS_MKDIR(conn
, smb_dname
, mode
) != 0) {
3895 return map_nt_error_from_unix(errno
);
3898 /* Ensure we're checking for a symlink here.... */
3899 /* We don't want to get caught by a symlink racer. */
3901 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
3902 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3903 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3904 return map_nt_error_from_unix(errno
);
3907 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3908 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3909 smb_fname_str_dbg(smb_dname
)));
3910 return NT_STATUS_NOT_A_DIRECTORY
;
3913 if (lp_store_dos_attributes(SNUM(conn
))) {
3915 file_set_dosmode(conn
, smb_dname
,
3916 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
3921 if (lp_inherit_permissions(SNUM(conn
))) {
3922 inherit_access_posix_acl(conn
, parent_dir
,
3924 need_re_stat
= true;
3929 * Check if high bits should have been set,
3930 * then (if bits are missing): add them.
3931 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3934 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
3935 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
3936 SMB_VFS_CHMOD(conn
, smb_dname
,
3937 (smb_dname
->st
.st_ex_mode
|
3938 (mode
& ~smb_dname
->st
.st_ex_mode
)));
3939 need_re_stat
= true;
3943 /* Change the owner if required. */
3944 if (lp_inherit_owner(SNUM(conn
)) != INHERIT_OWNER_NO
) {
3945 change_dir_owner_to_parent(conn
, parent_dir
,
3948 need_re_stat
= true;
3952 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
3953 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3954 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3955 return map_nt_error_from_unix(errno
);
3959 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
3960 smb_dname
->base_name
);
3962 return NT_STATUS_OK
;
3965 /****************************************************************************
3966 Open a directory from an NT SMB call.
3967 ****************************************************************************/
3969 static NTSTATUS
open_directory(connection_struct
*conn
,
3970 struct smb_request
*req
,
3971 struct smb_filename
*smb_dname
,
3972 uint32_t access_mask
,
3973 uint32_t share_access
,
3974 uint32_t create_disposition
,
3975 uint32_t create_options
,
3976 uint32_t file_attributes
,
3978 files_struct
**result
)
3980 files_struct
*fsp
= NULL
;
3981 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
3982 struct share_mode_lock
*lck
= NULL
;
3984 struct timespec mtimespec
;
3988 if (is_ntfs_stream_smb_fname(smb_dname
)) {
3989 DEBUG(2, ("open_directory: %s is a stream name!\n",
3990 smb_fname_str_dbg(smb_dname
)));
3991 return NT_STATUS_NOT_A_DIRECTORY
;
3994 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
3995 /* Ensure we have a directory attribute. */
3996 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3999 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
4000 "share_access = 0x%x create_options = 0x%x, "
4001 "create_disposition = 0x%x, file_attributes = 0x%x\n",
4002 smb_fname_str_dbg(smb_dname
),
4003 (unsigned int)access_mask
,
4004 (unsigned int)share_access
,
4005 (unsigned int)create_options
,
4006 (unsigned int)create_disposition
,
4007 (unsigned int)file_attributes
));
4009 status
= smbd_calculate_access_mask(conn
, smb_dname
, false,
4010 access_mask
, &access_mask
);
4011 if (!NT_STATUS_IS_OK(status
)) {
4012 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
4013 "on file %s returned %s\n",
4014 smb_fname_str_dbg(smb_dname
),
4015 nt_errstr(status
)));
4019 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
4020 !security_token_has_privilege(get_current_nttok(conn
),
4021 SEC_PRIV_SECURITY
)) {
4022 DEBUG(10, ("open_directory: open on %s "
4023 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
4024 smb_fname_str_dbg(smb_dname
)));
4025 return NT_STATUS_PRIVILEGE_NOT_HELD
;
4028 switch( create_disposition
) {
4032 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4035 info
= FILE_WAS_OPENED
;
4040 /* If directory exists error. If directory doesn't
4044 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
4045 DEBUG(2, ("open_directory: unable to create "
4046 "%s. Error was %s\n",
4047 smb_fname_str_dbg(smb_dname
),
4048 nt_errstr(status
)));
4052 status
= mkdir_internal(conn
, smb_dname
,
4055 if (!NT_STATUS_IS_OK(status
)) {
4056 DEBUG(2, ("open_directory: unable to create "
4057 "%s. Error was %s\n",
4058 smb_fname_str_dbg(smb_dname
),
4059 nt_errstr(status
)));
4063 info
= FILE_WAS_CREATED
;
4068 * If directory exists open. If directory doesn't
4073 status
= NT_STATUS_OK
;
4074 info
= FILE_WAS_OPENED
;
4076 status
= mkdir_internal(conn
, smb_dname
,
4079 if (NT_STATUS_IS_OK(status
)) {
4080 info
= FILE_WAS_CREATED
;
4082 /* Cope with create race. */
4083 if (!NT_STATUS_EQUAL(status
,
4084 NT_STATUS_OBJECT_NAME_COLLISION
)) {
4085 DEBUG(2, ("open_directory: unable to create "
4086 "%s. Error was %s\n",
4087 smb_fname_str_dbg(smb_dname
),
4088 nt_errstr(status
)));
4093 * If mkdir_internal() returned
4094 * NT_STATUS_OBJECT_NAME_COLLISION
4095 * we still must lstat the path.
4098 if (SMB_VFS_LSTAT(conn
, smb_dname
)
4100 DEBUG(2, ("Could not stat "
4101 "directory '%s' just "
4106 return map_nt_error_from_unix(
4110 info
= FILE_WAS_OPENED
;
4116 case FILE_SUPERSEDE
:
4117 case FILE_OVERWRITE
:
4118 case FILE_OVERWRITE_IF
:
4120 DEBUG(5,("open_directory: invalid create_disposition "
4121 "0x%x for directory %s\n",
4122 (unsigned int)create_disposition
,
4123 smb_fname_str_dbg(smb_dname
)));
4124 return NT_STATUS_INVALID_PARAMETER
;
4127 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
4128 DEBUG(5,("open_directory: %s is not a directory !\n",
4129 smb_fname_str_dbg(smb_dname
)));
4130 return NT_STATUS_NOT_A_DIRECTORY
;
4133 if (info
== FILE_WAS_OPENED
) {
4134 status
= smbd_check_access_rights(conn
,
4138 if (!NT_STATUS_IS_OK(status
)) {
4139 DEBUG(10, ("open_directory: smbd_check_access_rights on "
4140 "file %s failed with %s\n",
4141 smb_fname_str_dbg(smb_dname
),
4142 nt_errstr(status
)));
4147 status
= file_new(req
, conn
, &fsp
);
4148 if(!NT_STATUS_IS_OK(status
)) {
4153 * Setup the files_struct for it.
4156 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
4157 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
4158 fsp
->file_pid
= req
? req
->smbpid
: 0;
4159 fsp
->can_lock
= False
;
4160 fsp
->can_read
= False
;
4161 fsp
->can_write
= False
;
4163 fsp
->share_access
= share_access
;
4164 fsp
->fh
->private_options
= 0;
4166 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
4168 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
4169 fsp
->print_file
= NULL
;
4170 fsp
->modified
= False
;
4171 fsp
->oplock_type
= NO_OPLOCK
;
4172 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
4173 fsp
->is_directory
= True
;
4174 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
4175 fsp
->posix_flags
|= FSP_POSIX_FLAGS_ALL
;
4177 status
= fsp_set_smb_fname(fsp
, smb_dname
);
4178 if (!NT_STATUS_IS_OK(status
)) {
4179 file_free(req
, fsp
);
4183 /* Don't store old timestamps for directory
4184 handles in the internal database. We don't
4185 update them in there if new objects
4186 are creaded in the directory. Currently
4187 we only update timestamps on file writes.
4190 ZERO_STRUCT(mtimespec
);
4192 if (access_mask
& (FILE_LIST_DIRECTORY
|
4194 FILE_ADD_SUBDIRECTORY
|
4197 FILE_DELETE_CHILD
)) {
4199 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
4201 /* POSIX allows us to open a directory with O_RDONLY. */
4202 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
4204 if (!NT_STATUS_IS_OK(status
)) {
4205 DEBUG(5, ("open_directory: Could not open fd for "
4207 smb_fname_str_dbg(smb_dname
),
4208 nt_errstr(status
)));
4209 file_free(req
, fsp
);
4214 DEBUG(10, ("Not opening Directory %s\n",
4215 smb_fname_str_dbg(smb_dname
)));
4218 status
= vfs_stat_fsp(fsp
);
4219 if (!NT_STATUS_IS_OK(status
)) {
4221 file_free(req
, fsp
);
4225 if(!S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
4226 DEBUG(5,("open_directory: %s is not a directory !\n",
4227 smb_fname_str_dbg(smb_dname
)));
4229 file_free(req
, fsp
);
4230 return NT_STATUS_NOT_A_DIRECTORY
;
4233 /* Ensure there was no race condition. We need to check
4234 * dev/inode but not permissions, as these can change
4236 if (!check_same_dev_ino(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
4237 DEBUG(5,("open_directory: stat struct differs for "
4239 smb_fname_str_dbg(smb_dname
)));
4241 file_free(req
, fsp
);
4242 return NT_STATUS_ACCESS_DENIED
;
4245 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
4246 conn
->connectpath
, smb_dname
,
4250 DEBUG(0, ("open_directory: Could not get share mode lock for "
4251 "%s\n", smb_fname_str_dbg(smb_dname
)));
4253 file_free(req
, fsp
);
4254 return NT_STATUS_SHARING_VIOLATION
;
4257 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
4260 file_free(req
, fsp
);
4261 return NT_STATUS_DELETE_PENDING
;
4264 status
= open_mode_check(conn
, lck
,
4265 access_mask
, share_access
);
4267 if (!NT_STATUS_IS_OK(status
)) {
4270 file_free(req
, fsp
);
4274 ok
= set_share_mode(lck
, fsp
, get_current_uid(conn
),
4275 req
? req
->mid
: 0, NO_OPLOCK
,
4280 file_free(req
, fsp
);
4281 return NT_STATUS_NO_MEMORY
;
4284 /* For directories the delete on close bit at open time seems
4285 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
4286 if (create_options
& FILE_DELETE_ON_CLOSE
) {
4287 status
= can_set_delete_on_close(fsp
, 0);
4288 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
4289 del_share_mode(lck
, fsp
);
4292 file_free(req
, fsp
);
4296 if (NT_STATUS_IS_OK(status
)) {
4297 /* Note that here we set the *inital* delete on close flag,
4298 not the regular one. The magic gets handled in close. */
4299 fsp
->initial_delete_on_close
= True
;
4305 * Deal with other opens having a modified write time. Is this
4306 * possible for directories?
4308 struct timespec write_time
= get_share_mode_write_time(lck
);
4310 if (!null_timespec(write_time
)) {
4311 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
4322 return NT_STATUS_OK
;
4325 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
4326 struct smb_filename
*smb_dname
)
4331 status
= SMB_VFS_CREATE_FILE(
4334 0, /* root_dir_fid */
4335 smb_dname
, /* fname */
4336 FILE_READ_ATTRIBUTES
, /* access_mask */
4337 FILE_SHARE_NONE
, /* share_access */
4338 FILE_CREATE
, /* create_disposition*/
4339 FILE_DIRECTORY_FILE
, /* create_options */
4340 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
4341 0, /* oplock_request */
4343 0, /* allocation_size */
4344 0, /* private_flags */
4349 NULL
, NULL
); /* create context */
4351 if (NT_STATUS_IS_OK(status
)) {
4352 close_file(req
, fsp
, NORMAL_CLOSE
);
4358 /****************************************************************************
4359 Receive notification that one of our open files has been renamed by another
4361 ****************************************************************************/
4363 void msg_file_was_renamed(struct messaging_context
*msg
,
4366 struct server_id server_id
,
4370 char *frm
= (char *)data
->data
;
4372 const char *sharepath
;
4373 const char *base_name
;
4374 const char *stream_name
;
4375 struct smb_filename
*smb_fname
= NULL
;
4376 size_t sp_len
, bn_len
;
4378 struct smbd_server_connection
*sconn
=
4379 talloc_get_type_abort(private_data
,
4380 struct smbd_server_connection
);
4382 if (data
->data
== NULL
4383 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
4384 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
4385 (int)data
->length
));
4389 /* Unpack the message. */
4390 pull_file_id_24(frm
, &id
);
4391 sharepath
= &frm
[24];
4392 sp_len
= strlen(sharepath
);
4393 base_name
= sharepath
+ sp_len
+ 1;
4394 bn_len
= strlen(base_name
);
4395 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
4397 /* stream_name must always be NULL if there is no stream. */
4398 if (stream_name
[0] == '\0') {
4402 smb_fname
= synthetic_smb_fname(talloc_tos(),
4407 if (smb_fname
== NULL
) {
4411 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
4413 sharepath
, smb_fname_str_dbg(smb_fname
),
4414 file_id_string_tos(&id
)));
4416 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
4417 fsp
= file_find_di_next(fsp
)) {
4418 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
4420 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
4421 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
4422 smb_fname_str_dbg(smb_fname
)));
4423 status
= fsp_set_smb_fname(fsp
, smb_fname
);
4424 if (!NT_STATUS_IS_OK(status
)) {
4429 /* Now we have the complete path we can work out if this is
4430 actually within this share and adjust newname accordingly. */
4431 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
4432 "not sharepath %s) "
4433 "%s from %s -> %s\n",
4434 fsp
->conn
->connectpath
,
4438 smb_fname_str_dbg(smb_fname
)));
4442 TALLOC_FREE(smb_fname
);
4447 * If a main file is opened for delete, all streams need to be checked for
4448 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
4449 * If that works, delete them all by setting the delete on close and close.
4452 static NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
4453 const struct smb_filename
*smb_fname
)
4455 struct stream_struct
*stream_info
= NULL
;
4456 files_struct
**streams
= NULL
;
4458 unsigned int num_streams
= 0;
4459 TALLOC_CTX
*frame
= talloc_stackframe();
4462 status
= vfs_streaminfo(conn
, NULL
, smb_fname
, talloc_tos(),
4463 &num_streams
, &stream_info
);
4465 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
4466 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
4467 DEBUG(10, ("no streams around\n"));
4469 return NT_STATUS_OK
;
4472 if (!NT_STATUS_IS_OK(status
)) {
4473 DEBUG(10, ("vfs_streaminfo failed: %s\n",
4474 nt_errstr(status
)));
4478 DEBUG(10, ("open_streams_for_delete found %d streams\n",
4481 if (num_streams
== 0) {
4483 return NT_STATUS_OK
;
4486 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
4487 if (streams
== NULL
) {
4488 DEBUG(0, ("talloc failed\n"));
4489 status
= NT_STATUS_NO_MEMORY
;
4493 for (i
=0; i
<num_streams
; i
++) {
4494 struct smb_filename
*smb_fname_cp
;
4496 if (strequal(stream_info
[i
].name
, "::$DATA")) {
4501 smb_fname_cp
= synthetic_smb_fname(talloc_tos(),
4502 smb_fname
->base_name
,
4503 stream_info
[i
].name
,
4506 ~SMB_FILENAME_POSIX_PATH
));
4507 if (smb_fname_cp
== NULL
) {
4508 status
= NT_STATUS_NO_MEMORY
;
4512 if (SMB_VFS_STAT(conn
, smb_fname_cp
) == -1) {
4513 DEBUG(10, ("Unable to stat stream: %s\n",
4514 smb_fname_str_dbg(smb_fname_cp
)));
4517 status
= SMB_VFS_CREATE_FILE(
4520 0, /* root_dir_fid */
4521 smb_fname_cp
, /* fname */
4522 DELETE_ACCESS
, /* access_mask */
4523 (FILE_SHARE_READ
| /* share_access */
4524 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
4525 FILE_OPEN
, /* create_disposition*/
4526 0, /* create_options */
4527 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
4528 0, /* oplock_request */
4530 0, /* allocation_size */
4531 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
4534 &streams
[i
], /* result */
4536 NULL
, NULL
); /* create context */
4538 if (!NT_STATUS_IS_OK(status
)) {
4539 DEBUG(10, ("Could not open stream %s: %s\n",
4540 smb_fname_str_dbg(smb_fname_cp
),
4541 nt_errstr(status
)));
4543 TALLOC_FREE(smb_fname_cp
);
4546 TALLOC_FREE(smb_fname_cp
);
4550 * don't touch the variable "status" beyond this point :-)
4553 for (i
-= 1 ; i
>= 0; i
--) {
4554 if (streams
[i
] == NULL
) {
4558 DEBUG(10, ("Closing stream # %d, %s\n", i
,
4559 fsp_str_dbg(streams
[i
])));
4560 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
4568 /*********************************************************************
4569 Create a default ACL by inheriting from the parent. If no inheritance
4570 from the parent available, don't set anything. This will leave the actual
4571 permissions the new file or directory already got from the filesystem
4572 as the NT ACL when read.
4573 *********************************************************************/
4575 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
4577 TALLOC_CTX
*frame
= talloc_stackframe();
4578 char *parent_name
= NULL
;
4579 struct security_descriptor
*parent_desc
= NULL
;
4580 NTSTATUS status
= NT_STATUS_OK
;
4581 struct security_descriptor
*psd
= NULL
;
4582 const struct dom_sid
*owner_sid
= NULL
;
4583 const struct dom_sid
*group_sid
= NULL
;
4584 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
4585 struct security_token
*token
= fsp
->conn
->session_info
->security_token
;
4586 bool inherit_owner
=
4587 (lp_inherit_owner(SNUM(fsp
->conn
)) == INHERIT_OWNER_WINDOWS_AND_UNIX
);
4588 bool inheritable_components
= false;
4589 bool try_builtin_administrators
= false;
4590 const struct dom_sid
*BA_U_sid
= NULL
;
4591 const struct dom_sid
*BA_G_sid
= NULL
;
4592 bool try_system
= false;
4593 const struct dom_sid
*SY_U_sid
= NULL
;
4594 const struct dom_sid
*SY_G_sid
= NULL
;
4596 struct smb_filename
*parent_smb_fname
= NULL
;
4598 if (!parent_dirname(frame
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
4600 return NT_STATUS_NO_MEMORY
;
4602 parent_smb_fname
= synthetic_smb_fname(talloc_tos(),
4606 fsp
->fsp_name
->flags
);
4608 if (parent_smb_fname
== NULL
) {
4610 return NT_STATUS_NO_MEMORY
;
4613 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
4615 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
4618 if (!NT_STATUS_IS_OK(status
)) {
4623 inheritable_components
= sd_has_inheritable_components(parent_desc
,
4626 if (!inheritable_components
&& !inherit_owner
) {
4628 /* Nothing to inherit and not setting owner. */
4629 return NT_STATUS_OK
;
4632 /* Create an inherited descriptor from the parent. */
4634 if (DEBUGLEVEL
>= 10) {
4635 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
4636 fsp_str_dbg(fsp
) ));
4637 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
4640 /* Inherit from parent descriptor if "inherit owner" set. */
4641 if (inherit_owner
) {
4642 owner_sid
= parent_desc
->owner_sid
;
4643 group_sid
= parent_desc
->group_sid
;
4646 if (owner_sid
== NULL
) {
4647 if (security_token_has_builtin_administrators(token
)) {
4648 try_builtin_administrators
= true;
4649 } else if (security_token_is_system(token
)) {
4650 try_builtin_administrators
= true;
4655 if (group_sid
== NULL
&&
4656 token
->num_sids
== PRIMARY_GROUP_SID_INDEX
)
4658 if (security_token_is_system(token
)) {
4659 try_builtin_administrators
= true;
4664 if (try_builtin_administrators
) {
4669 ok
= sids_to_unixids(&global_sid_Builtin_Administrators
, 1, &ids
);
4673 BA_U_sid
= &global_sid_Builtin_Administrators
;
4674 BA_G_sid
= &global_sid_Builtin_Administrators
;
4677 BA_U_sid
= &global_sid_Builtin_Administrators
;
4680 BA_G_sid
= &global_sid_Builtin_Administrators
;
4693 ok
= sids_to_unixids(&global_sid_System
, 1, &ids
);
4697 SY_U_sid
= &global_sid_System
;
4698 SY_G_sid
= &global_sid_System
;
4701 SY_U_sid
= &global_sid_System
;
4704 SY_G_sid
= &global_sid_System
;
4712 if (owner_sid
== NULL
) {
4713 owner_sid
= BA_U_sid
;
4716 if (owner_sid
== NULL
) {
4717 owner_sid
= SY_U_sid
;
4720 if (group_sid
== NULL
) {
4721 group_sid
= SY_G_sid
;
4724 if (try_system
&& group_sid
== NULL
) {
4725 group_sid
= BA_G_sid
;
4728 if (owner_sid
== NULL
) {
4729 owner_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
4731 if (group_sid
== NULL
) {
4732 if (token
->num_sids
== PRIMARY_GROUP_SID_INDEX
) {
4733 group_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
4735 group_sid
= &token
->sids
[PRIMARY_GROUP_SID_INDEX
];
4739 status
= se_create_child_secdesc(frame
,
4746 if (!NT_STATUS_IS_OK(status
)) {
4751 /* If inheritable_components == false,
4752 se_create_child_secdesc()
4753 creates a security descriptor with a NULL dacl
4754 entry, but with SEC_DESC_DACL_PRESENT. We need
4755 to remove that flag. */
4757 if (!inheritable_components
) {
4758 security_info_sent
&= ~SECINFO_DACL
;
4759 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
4762 if (DEBUGLEVEL
>= 10) {
4763 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
4764 fsp_str_dbg(fsp
) ));
4765 NDR_PRINT_DEBUG(security_descriptor
, psd
);
4768 if (inherit_owner
) {
4769 /* We need to be root to force this. */
4772 status
= SMB_VFS_FSET_NT_ACL(fsp
,
4775 if (inherit_owner
) {
4783 * If we already have a lease, it must match the new file id. [MS-SMB2]
4784 * 3.3.5.9.8 speaks about INVALID_PARAMETER if an already used lease key is
4785 * used for a different file name.
4788 struct lease_match_state
{
4789 /* Input parameters. */
4790 TALLOC_CTX
*mem_ctx
;
4791 const char *servicepath
;
4792 const struct smb_filename
*fname
;
4795 /* Return parameters. */
4796 uint32_t num_file_ids
;
4797 struct file_id
*ids
;
4798 NTSTATUS match_status
;
4801 /*************************************************************
4802 File doesn't exist but this lease key+guid is already in use.
4804 This is only allowable in the dynamic share case where the
4805 service path must be different.
4807 There is a small race condition here in the multi-connection
4808 case where a client sends two create calls on different connections,
4809 where the file doesn't exist and one smbd creates the leases_db
4810 entry first, but this will get fixed by the multichannel cleanup
4811 when all identical client_guids get handled by a single smbd.
4812 **************************************************************/
4814 static void lease_match_parser_new_file(
4816 const struct leases_db_file
*files
,
4817 struct lease_match_state
*state
)
4821 for (i
= 0; i
< num_files
; i
++) {
4822 const struct leases_db_file
*f
= &files
[i
];
4823 if (strequal(state
->servicepath
, f
->servicepath
)) {
4824 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4829 /* Dynamic share case. Break leases on all other files. */
4830 state
->match_status
= leases_db_copy_file_ids(state
->mem_ctx
,
4834 if (!NT_STATUS_IS_OK(state
->match_status
)) {
4838 state
->num_file_ids
= num_files
;
4839 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4843 static void lease_match_parser(
4845 const struct leases_db_file
*files
,
4848 struct lease_match_state
*state
=
4849 (struct lease_match_state
*)private_data
;
4852 if (!state
->file_existed
) {
4854 * Deal with name mismatch or
4855 * possible dynamic share case separately
4856 * to make code clearer.
4858 lease_match_parser_new_file(num_files
,
4865 state
->match_status
= NT_STATUS_OK
;
4867 for (i
= 0; i
< num_files
; i
++) {
4868 const struct leases_db_file
*f
= &files
[i
];
4870 /* Everything should be the same. */
4871 if (!file_id_equal(&state
->id
, &f
->id
)) {
4872 /* This should catch all dynamic share cases. */
4873 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4876 if (!strequal(f
->servicepath
, state
->servicepath
)) {
4877 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4880 if (!strequal(f
->base_name
, state
->fname
->base_name
)) {
4881 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4884 if (!strequal(f
->stream_name
, state
->fname
->stream_name
)) {
4885 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4890 if (NT_STATUS_IS_OK(state
->match_status
)) {
4892 * Common case - just opening another handle on a
4893 * file on a non-dynamic share.
4898 if (NT_STATUS_EQUAL(state
->match_status
, NT_STATUS_INVALID_PARAMETER
)) {
4899 /* Mismatched path. Error back to client. */
4904 * File id mismatch. Dynamic share case NT_STATUS_OPLOCK_NOT_GRANTED.
4905 * Don't allow leases.
4908 state
->match_status
= leases_db_copy_file_ids(state
->mem_ctx
,
4912 if (!NT_STATUS_IS_OK(state
->match_status
)) {
4916 state
->num_file_ids
= num_files
;
4917 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4921 static NTSTATUS
lease_match(connection_struct
*conn
,
4922 struct smb_request
*req
,
4923 struct smb2_lease_key
*lease_key
,
4924 const char *servicepath
,
4925 const struct smb_filename
*fname
,
4926 uint16_t *p_version
,
4929 struct smbd_server_connection
*sconn
= req
->sconn
;
4930 TALLOC_CTX
*tos
= talloc_tos();
4931 struct lease_match_state state
= {
4933 .servicepath
= servicepath
,
4935 .match_status
= NT_STATUS_OK
4940 state
.file_existed
= VALID_STAT(fname
->st
);
4941 if (state
.file_existed
) {
4942 state
.id
= vfs_file_id_from_sbuf(conn
, &fname
->st
);
4944 memset(&state
.id
, '\0', sizeof(state
.id
));
4947 status
= leases_db_parse(&sconn
->client
->connections
->smb2
.client
.guid
,
4948 lease_key
, lease_match_parser
, &state
);
4949 if (!NT_STATUS_IS_OK(status
)) {
4951 * Not found or error means okay: We can make the lease pass
4953 return NT_STATUS_OK
;
4955 if (!NT_STATUS_EQUAL(state
.match_status
, NT_STATUS_OPLOCK_NOT_GRANTED
)) {
4957 * Anything but NT_STATUS_OPLOCK_NOT_GRANTED, let the caller
4960 return state
.match_status
;
4963 /* We have to break all existing leases. */
4964 for (i
= 0; i
< state
.num_file_ids
; i
++) {
4965 struct share_mode_lock
*lck
;
4966 struct share_mode_data
*d
;
4969 if (file_id_equal(&state
.ids
[i
], &state
.id
)) {
4970 /* Don't need to break our own file. */
4974 lck
= get_existing_share_mode_lock(talloc_tos(), state
.ids
[i
]);
4976 /* Race condition - file already closed. */
4980 for (j
=0; j
<d
->num_share_modes
; j
++) {
4981 struct share_mode_entry
*e
= &d
->share_modes
[j
];
4982 uint32_t e_lease_type
= get_lease_type(d
, e
);
4983 struct share_mode_lease
*l
= NULL
;
4985 if (share_mode_stale_pid(d
, j
)) {
4989 if (e
->op_type
== LEASE_OPLOCK
) {
4990 l
= &lck
->data
->leases
[e
->lease_idx
];
4991 if (!smb2_lease_key_equal(&l
->lease_key
,
4995 *p_epoch
= l
->epoch
;
4996 *p_version
= l
->lease_version
;
4999 if (e_lease_type
== SMB2_LEASE_NONE
) {
5003 send_break_message(conn
->sconn
->msg_ctx
, &d
->id
, e
,
5007 * Windows 7 and 8 lease clients
5008 * are broken in that they will not
5009 * respond to lease break requests
5010 * whilst waiting for an outstanding
5011 * open request on that lease handle
5012 * on the same TCP connection, due
5013 * to holding an internal inode lock.
5015 * This means we can't reschedule
5016 * ourselves here, but must return
5021 * Send the breaks and then return
5022 * SMB2_LEASE_NONE in the lease handle
5023 * to cause them to acknowledge the
5024 * lease break. Consulatation with
5025 * Microsoft engineering confirmed
5026 * this approach is safe.
5033 * Ensure we don't grant anything more so we
5036 return NT_STATUS_OPLOCK_NOT_GRANTED
;
5040 * Wrapper around open_file_ntcreate and open_directory
5043 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
5044 struct smb_request
*req
,
5045 struct smb_filename
*smb_fname
,
5046 uint32_t access_mask
,
5047 uint32_t share_access
,
5048 uint32_t create_disposition
,
5049 uint32_t create_options
,
5050 uint32_t file_attributes
,
5051 uint32_t oplock_request
,
5052 struct smb2_lease
*lease
,
5053 uint64_t allocation_size
,
5054 uint32_t private_flags
,
5055 struct security_descriptor
*sd
,
5056 struct ea_list
*ea_list
,
5058 files_struct
**result
,
5061 int info
= FILE_WAS_OPENED
;
5062 files_struct
*base_fsp
= NULL
;
5063 files_struct
*fsp
= NULL
;
5066 DBG_DEBUG("create_file_unixpath: access_mask = 0x%x "
5067 "file_attributes = 0x%x, share_access = 0x%x, "
5068 "create_disposition = 0x%x create_options = 0x%x "
5069 "oplock_request = 0x%x private_flags = 0x%x "
5070 "ea_list = %p, sd = %p, "
5072 (unsigned int)access_mask
,
5073 (unsigned int)file_attributes
,
5074 (unsigned int)share_access
,
5075 (unsigned int)create_disposition
,
5076 (unsigned int)create_options
,
5077 (unsigned int)oplock_request
,
5078 (unsigned int)private_flags
,
5079 ea_list
, sd
, smb_fname_str_dbg(smb_fname
));
5081 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
5082 status
= NT_STATUS_NOT_SUPPORTED
;
5086 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
5087 status
= NT_STATUS_INVALID_PARAMETER
;
5092 oplock_request
|= INTERNAL_OPEN_ONLY
;
5095 if (lease
!= NULL
) {
5096 uint16_t epoch
= lease
->lease_epoch
;
5097 uint16_t version
= lease
->lease_version
;
5100 DBG_WARNING("Got lease on internal open\n");
5101 status
= NT_STATUS_INTERNAL_ERROR
;
5105 status
= lease_match(conn
,
5112 if (NT_STATUS_EQUAL(status
, NT_STATUS_OPLOCK_NOT_GRANTED
)) {
5113 /* Dynamic share file. No leases and update epoch... */
5114 lease
->lease_state
= SMB2_LEASE_NONE
;
5115 lease
->lease_epoch
= epoch
;
5116 lease
->lease_version
= version
;
5117 } else if (!NT_STATUS_IS_OK(status
)) {
5122 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
5123 && (access_mask
& DELETE_ACCESS
)
5124 && !is_ntfs_stream_smb_fname(smb_fname
)) {
5126 * We can't open a file with DELETE access if any of the
5127 * streams is open without FILE_SHARE_DELETE
5129 status
= open_streams_for_delete(conn
, smb_fname
);
5131 if (!NT_STATUS_IS_OK(status
)) {
5136 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
5137 !security_token_has_privilege(get_current_nttok(conn
),
5138 SEC_PRIV_SECURITY
)) {
5139 DEBUG(10, ("create_file_unixpath: open on %s "
5140 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
5141 smb_fname_str_dbg(smb_fname
)));
5142 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
5147 * Files or directories can't be opened DELETE_ON_CLOSE without
5149 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13358
5151 if (create_options
& FILE_DELETE_ON_CLOSE
) {
5152 if ((access_mask
& DELETE_ACCESS
) == 0) {
5153 status
= NT_STATUS_INVALID_PARAMETER
;
5158 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
5159 && is_ntfs_stream_smb_fname(smb_fname
)
5160 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
5161 uint32_t base_create_disposition
;
5162 struct smb_filename
*smb_fname_base
= NULL
;
5164 if (create_options
& FILE_DIRECTORY_FILE
) {
5165 status
= NT_STATUS_NOT_A_DIRECTORY
;
5169 switch (create_disposition
) {
5171 base_create_disposition
= FILE_OPEN
;
5174 base_create_disposition
= FILE_OPEN_IF
;
5178 /* Create an smb_filename with stream_name == NULL. */
5179 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
5180 smb_fname
->base_name
,
5184 if (smb_fname_base
== NULL
) {
5185 status
= NT_STATUS_NO_MEMORY
;
5189 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
5190 DEBUG(10, ("Unable to stat stream: %s\n",
5191 smb_fname_str_dbg(smb_fname_base
)));
5194 * https://bugzilla.samba.org/show_bug.cgi?id=10229
5195 * We need to check if the requested access mask
5196 * could be used to open the underlying file (if
5197 * it existed), as we're passing in zero for the
5198 * access mask to the base filename.
5200 status
= check_base_file_access(conn
,
5204 if (!NT_STATUS_IS_OK(status
)) {
5205 DEBUG(10, ("Permission check "
5206 "for base %s failed: "
5207 "%s\n", smb_fname
->base_name
,
5208 nt_errstr(status
)));
5213 /* Open the base file. */
5214 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
5217 | FILE_SHARE_DELETE
,
5218 base_create_disposition
,
5219 0, 0, 0, NULL
, 0, 0, NULL
, NULL
,
5221 TALLOC_FREE(smb_fname_base
);
5223 if (!NT_STATUS_IS_OK(status
)) {
5224 DEBUG(10, ("create_file_unixpath for base %s failed: "
5225 "%s\n", smb_fname
->base_name
,
5226 nt_errstr(status
)));
5229 /* we don't need the low level fd */
5234 * If it's a request for a directory open, deal with it separately.
5237 if (create_options
& FILE_DIRECTORY_FILE
) {
5239 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
5240 status
= NT_STATUS_INVALID_PARAMETER
;
5244 /* Can't open a temp directory. IFS kit test. */
5245 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
5246 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
5247 status
= NT_STATUS_INVALID_PARAMETER
;
5252 * We will get a create directory here if the Win32
5253 * app specified a security descriptor in the
5254 * CreateDirectory() call.
5258 status
= open_directory(
5259 conn
, req
, smb_fname
, access_mask
, share_access
,
5260 create_disposition
, create_options
, file_attributes
,
5265 * Ordinary file case.
5268 status
= file_new(req
, conn
, &fsp
);
5269 if(!NT_STATUS_IS_OK(status
)) {
5273 status
= fsp_set_smb_fname(fsp
, smb_fname
);
5274 if (!NT_STATUS_IS_OK(status
)) {
5280 * We're opening the stream element of a
5281 * base_fsp we already opened. Set up the
5284 fsp
->base_fsp
= base_fsp
;
5287 if (allocation_size
) {
5288 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
5292 status
= open_file_ntcreate(conn
,
5305 if(!NT_STATUS_IS_OK(status
)) {
5306 file_free(req
, fsp
);
5310 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
5312 /* A stream open never opens a directory */
5315 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
5320 * Fail the open if it was explicitly a non-directory
5324 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
5325 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
5330 status
= open_directory(
5331 conn
, req
, smb_fname
, access_mask
,
5332 share_access
, create_disposition
,
5333 create_options
, file_attributes
,
5338 if (!NT_STATUS_IS_OK(status
)) {
5342 fsp
->base_fsp
= base_fsp
;
5344 if ((ea_list
!= NULL
) &&
5345 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
5346 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
5347 if (!NT_STATUS_IS_OK(status
)) {
5352 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
5353 status
= NT_STATUS_ACCESS_DENIED
;
5357 /* Save the requested allocation size. */
5358 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
5359 if ((allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)
5360 && !(fsp
->is_directory
))
5362 fsp
->initial_allocation_size
= smb_roundup(
5363 fsp
->conn
, allocation_size
);
5364 if (vfs_allocate_file_space(
5365 fsp
, fsp
->initial_allocation_size
) == -1) {
5366 status
= NT_STATUS_DISK_FULL
;
5370 fsp
->initial_allocation_size
= smb_roundup(
5371 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
5374 fsp
->initial_allocation_size
= 0;
5377 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
5378 fsp
->base_fsp
== NULL
) {
5381 * According to the MS documentation, the only time the security
5382 * descriptor is applied to the opened file is iff we *created* the
5383 * file; an existing file stays the same.
5385 * Also, it seems (from observation) that you can open the file with
5386 * any access mask but you can still write the sd. We need to override
5387 * the granted access before we call set_sd
5388 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
5391 uint32_t sec_info_sent
;
5392 uint32_t saved_access_mask
= fsp
->access_mask
;
5394 sec_info_sent
= get_sec_info(sd
);
5396 fsp
->access_mask
= FILE_GENERIC_ALL
;
5398 if (sec_info_sent
& (SECINFO_OWNER
|
5402 status
= set_sd(fsp
, sd
, sec_info_sent
);
5405 fsp
->access_mask
= saved_access_mask
;
5407 if (!NT_STATUS_IS_OK(status
)) {
5410 } else if (lp_inherit_acls(SNUM(conn
))) {
5411 /* Inherit from parent. Errors here are not fatal. */
5412 status
= inherit_new_acl(fsp
);
5413 if (!NT_STATUS_IS_OK(status
)) {
5414 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
5416 nt_errstr(status
) ));
5421 if ((conn
->fs_capabilities
& FILE_FILE_COMPRESSION
)
5422 && (create_options
& FILE_NO_COMPRESSION
)
5423 && (info
== FILE_WAS_CREATED
)) {
5424 status
= SMB_VFS_SET_COMPRESSION(conn
, fsp
, fsp
,
5425 COMPRESSION_FORMAT_NONE
);
5426 if (!NT_STATUS_IS_OK(status
)) {
5427 DEBUG(1, ("failed to disable compression: %s\n",
5428 nt_errstr(status
)));
5432 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
5435 if (pinfo
!= NULL
) {
5439 smb_fname
->st
= fsp
->fsp_name
->st
;
5441 return NT_STATUS_OK
;
5444 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
5447 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
5449 * The close_file below will close
5454 close_file(req
, fsp
, ERROR_CLOSE
);
5457 if (base_fsp
!= NULL
) {
5458 close_file(req
, base_fsp
, ERROR_CLOSE
);
5465 * Calculate the full path name given a relative fid.
5467 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
5468 struct smb_request
*req
,
5469 uint16_t root_dir_fid
,
5470 const struct smb_filename
*smb_fname
,
5471 struct smb_filename
**smb_fname_out
)
5473 files_struct
*dir_fsp
;
5474 char *parent_fname
= NULL
;
5475 char *new_base_name
= NULL
;
5476 uint32_t ucf_flags
= ucf_flags_from_smb_request(req
);
5479 if (root_dir_fid
== 0 || !smb_fname
) {
5480 status
= NT_STATUS_INTERNAL_ERROR
;
5484 dir_fsp
= file_fsp(req
, root_dir_fid
);
5486 if (dir_fsp
== NULL
) {
5487 status
= NT_STATUS_INVALID_HANDLE
;
5491 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
5492 status
= NT_STATUS_INVALID_HANDLE
;
5496 if (!dir_fsp
->is_directory
) {
5499 * Check to see if this is a mac fork of some kind.
5502 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
5503 is_ntfs_stream_smb_fname(smb_fname
)) {
5504 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
5509 we need to handle the case when we get a
5510 relative open relative to a file and the
5511 pathname is blank - this is a reopen!
5512 (hint from demyn plantenberg)
5515 status
= NT_STATUS_INVALID_HANDLE
;
5519 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
5521 * We're at the toplevel dir, the final file name
5522 * must not contain ./, as this is filtered out
5523 * normally by srvstr_get_path and unix_convert
5524 * explicitly rejects paths containing ./.
5526 parent_fname
= talloc_strdup(talloc_tos(), "");
5527 if (parent_fname
== NULL
) {
5528 status
= NT_STATUS_NO_MEMORY
;
5532 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
5535 * Copy in the base directory name.
5538 parent_fname
= talloc_array(talloc_tos(), char,
5540 if (parent_fname
== NULL
) {
5541 status
= NT_STATUS_NO_MEMORY
;
5544 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
5548 * Ensure it ends in a '/'.
5549 * We used TALLOC_SIZE +2 to add space for the '/'.
5553 && (parent_fname
[dir_name_len
-1] != '\\')
5554 && (parent_fname
[dir_name_len
-1] != '/')) {
5555 parent_fname
[dir_name_len
] = '/';
5556 parent_fname
[dir_name_len
+1] = '\0';
5560 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
5561 smb_fname
->base_name
);
5562 if (new_base_name
== NULL
) {
5563 status
= NT_STATUS_NO_MEMORY
;
5567 status
= filename_convert(req
,
5573 if (!NT_STATUS_IS_OK(status
)) {
5578 TALLOC_FREE(parent_fname
);
5579 TALLOC_FREE(new_base_name
);
5583 NTSTATUS
create_file_default(connection_struct
*conn
,
5584 struct smb_request
*req
,
5585 uint16_t root_dir_fid
,
5586 struct smb_filename
*smb_fname
,
5587 uint32_t access_mask
,
5588 uint32_t share_access
,
5589 uint32_t create_disposition
,
5590 uint32_t create_options
,
5591 uint32_t file_attributes
,
5592 uint32_t oplock_request
,
5593 struct smb2_lease
*lease
,
5594 uint64_t allocation_size
,
5595 uint32_t private_flags
,
5596 struct security_descriptor
*sd
,
5597 struct ea_list
*ea_list
,
5598 files_struct
**result
,
5600 const struct smb2_create_blobs
*in_context_blobs
,
5601 struct smb2_create_blobs
*out_context_blobs
)
5603 int info
= FILE_WAS_OPENED
;
5604 files_struct
*fsp
= NULL
;
5606 bool stream_name
= false;
5608 DBG_DEBUG("create_file: access_mask = 0x%x "
5609 "file_attributes = 0x%x, share_access = 0x%x, "
5610 "create_disposition = 0x%x create_options = 0x%x "
5611 "oplock_request = 0x%x "
5612 "private_flags = 0x%x "
5613 "root_dir_fid = 0x%x, ea_list = %p, sd = %p, "
5615 (unsigned int)access_mask
,
5616 (unsigned int)file_attributes
,
5617 (unsigned int)share_access
,
5618 (unsigned int)create_disposition
,
5619 (unsigned int)create_options
,
5620 (unsigned int)oplock_request
,
5621 (unsigned int)private_flags
,
5622 (unsigned int)root_dir_fid
,
5623 ea_list
, sd
, smb_fname_str_dbg(smb_fname
));
5626 * Calculate the filename from the root_dir_if if necessary.
5629 if (root_dir_fid
!= 0) {
5630 struct smb_filename
*smb_fname_out
= NULL
;
5631 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
5632 smb_fname
, &smb_fname_out
);
5633 if (!NT_STATUS_IS_OK(status
)) {
5636 smb_fname
= smb_fname_out
;
5640 * Check to see if this is a mac fork of some kind.
5643 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
5645 enum FAKE_FILE_TYPE fake_file_type
;
5647 fake_file_type
= is_fake_file(smb_fname
);
5649 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
5652 * Here we go! support for changing the disk quotas
5655 * We need to fake up to open this MAGIC QUOTA file
5656 * and return a valid FID.
5658 * w2k close this file directly after openening xp
5659 * also tries a QUERY_FILE_INFO on the file and then
5662 status
= open_fake_file(req
, conn
, req
->vuid
,
5663 fake_file_type
, smb_fname
,
5665 if (!NT_STATUS_IS_OK(status
)) {
5669 ZERO_STRUCT(smb_fname
->st
);
5673 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
5674 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5679 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
5681 smb_fname
->stream_name
= NULL
;
5682 /* We have to handle this error here. */
5683 if (create_options
& FILE_DIRECTORY_FILE
) {
5684 status
= NT_STATUS_NOT_A_DIRECTORY
;
5687 if (req
!= NULL
&& req
->posix_pathnames
) {
5688 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
5690 ret
= SMB_VFS_STAT(conn
, smb_fname
);
5693 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
5694 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
5699 status
= create_file_unixpath(
5700 conn
, req
, smb_fname
, access_mask
, share_access
,
5701 create_disposition
, create_options
, file_attributes
,
5702 oplock_request
, lease
, allocation_size
, private_flags
,
5706 if (!NT_STATUS_IS_OK(status
)) {
5711 DEBUG(10, ("create_file: info=%d\n", info
));
5714 if (pinfo
!= NULL
) {
5717 return NT_STATUS_OK
;
5720 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
5723 close_file(req
, fsp
, ERROR_CLOSE
);