2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
34 extern const struct generic_mapping file_generic_mapping;
36 struct deferred_open_record {
37 bool delayed_for_oplocks;
41 /****************************************************************************
42 If the requester wanted DELETE_ACCESS and was rejected because
43 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
45 ****************************************************************************/
47 static bool parent_override_delete(connection_struct *conn,
48 const struct smb_filename *smb_fname,
50 uint32_t rejected_mask)
52 if ((access_mask & DELETE_ACCESS) &&
53 (rejected_mask & DELETE_ACCESS) &&
54 can_delete_file_in_directory(conn, smb_fname)) {
60 /****************************************************************************
61 Check if we have open rights.
62 ****************************************************************************/
64 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
65 const struct smb_filename *smb_fname,
68 /* Check if we have rights to open. */
70 struct security_descriptor *sd = NULL;
71 uint32_t rejected_share_access;
72 uint32_t rejected_mask = access_mask;
74 rejected_share_access = access_mask & ~(conn->share_access);
76 if (rejected_share_access) {
77 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
79 (unsigned int)access_mask,
80 smb_fname_str_dbg(smb_fname),
81 (unsigned int)rejected_share_access ));
82 return NT_STATUS_ACCESS_DENIED;
85 if (get_current_uid(conn) == (uid_t)0) {
86 /* I'm sorry sir, I didn't know you were root... */
87 DEBUG(10,("smbd_check_access_rights: root override "
88 "on %s. Granting 0x%x\n",
89 smb_fname_str_dbg(smb_fname),
90 (unsigned int)access_mask ));
94 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
95 DEBUG(10,("smbd_check_access_rights: not checking ACL "
96 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
97 smb_fname_str_dbg(smb_fname),
98 (unsigned int)access_mask ));
102 if (access_mask == DELETE_ACCESS &&
103 VALID_STAT(smb_fname->st) &&
104 S_ISLNK(smb_fname->st.st_ex_mode)) {
105 /* We can always delete a symlink. */
106 DEBUG(10,("smbd_check_access_rights: not checking ACL "
107 "on DELETE_ACCESS on symlink %s.\n",
108 smb_fname_str_dbg(smb_fname) ));
112 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
120 smb_fname_str_dbg(smb_fname),
123 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
131 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
132 * owner WRITE_DAC and READ_CONTROL.
134 status = se_access_check(sd,
135 get_current_nttok(conn),
136 (access_mask & ~FILE_READ_ATTRIBUTES),
139 DEBUG(10,("smbd_check_access_rights: file %s requesting "
140 "0x%x returning 0x%x (%s)\n",
141 smb_fname_str_dbg(smb_fname),
142 (unsigned int)access_mask,
143 (unsigned int)rejected_mask,
144 nt_errstr(status) ));
146 if (!NT_STATUS_IS_OK(status)) {
147 if (DEBUGLEVEL >= 10) {
148 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
149 smb_fname_str_dbg(smb_fname) ));
150 NDR_PRINT_DEBUG(security_descriptor, sd);
156 if (NT_STATUS_IS_OK(status) ||
157 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
161 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
165 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
166 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
167 !lp_store_dos_attributes(SNUM(conn)) &&
168 (lp_map_readonly(SNUM(conn)) ||
169 lp_map_archive(SNUM(conn)) ||
170 lp_map_hidden(SNUM(conn)) ||
171 lp_map_system(SNUM(conn)))) {
172 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
174 DEBUG(10,("smbd_check_access_rights: "
176 "FILE_WRITE_ATTRIBUTES "
178 smb_fname_str_dbg(smb_fname)));
181 if (parent_override_delete(conn,
185 /* Were we trying to do an open
186 * for delete and didn't get DELETE
187 * access (only) ? Check if the
188 * directory allows DELETE_CHILD.
190 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
193 rejected_mask &= ~DELETE_ACCESS;
195 DEBUG(10,("smbd_check_access_rights: "
199 smb_fname_str_dbg(smb_fname)));
202 if (rejected_mask != 0) {
203 return NT_STATUS_ACCESS_DENIED;
208 static NTSTATUS check_parent_access(struct connection_struct *conn,
209 struct smb_filename *smb_fname,
210 uint32_t access_mask)
213 char *parent_dir = NULL;
214 struct security_descriptor *parent_sd = NULL;
215 uint32_t access_granted = 0;
217 if (!parent_dirname(talloc_tos(),
218 smb_fname->base_name,
221 return NT_STATUS_NO_MEMORY;
224 if (get_current_uid(conn) == (uid_t)0) {
225 /* I'm sorry sir, I didn't know you were root... */
226 DEBUG(10,("check_parent_access: root override "
227 "on %s. Granting 0x%x\n",
228 smb_fname_str_dbg(smb_fname),
229 (unsigned int)access_mask ));
233 status = SMB_VFS_GET_NT_ACL(conn,
238 if (!NT_STATUS_IS_OK(status)) {
239 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
240 "%s with error %s\n",
247 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
248 * owner WRITE_DAC and READ_CONTROL.
250 status = se_access_check(parent_sd,
251 get_current_nttok(conn),
252 (access_mask & ~FILE_READ_ATTRIBUTES),
254 if(!NT_STATUS_IS_OK(status)) {
255 DEBUG(5,("check_parent_access: access check "
256 "on directory %s for "
257 "path %s for mask 0x%x returned (0x%x) %s\n",
259 smb_fname->base_name,
262 nt_errstr(status) ));
269 /****************************************************************************
270 fd support routines - attempt to do a dos_open.
271 ****************************************************************************/
273 static NTSTATUS fd_open(struct connection_struct *conn,
278 struct smb_filename *smb_fname = fsp->fsp_name;
279 NTSTATUS status = NT_STATUS_OK;
283 * Never follow symlinks on a POSIX client. The
284 * client should be doing this.
287 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
292 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
293 if (fsp->fh->fd == -1) {
294 int posix_errno = errno;
296 #if defined(ENOTSUP) && defined(OSF1)
297 /* handle special Tru64 errno */
298 if (errno == ENOTSUP) {
303 /* fix broken NetBSD errno */
304 if (errno == EFTYPE) {
308 /* fix broken FreeBSD errno */
309 if (errno == EMLINK) {
312 #endif /* O_NOFOLLOW */
313 status = map_nt_error_from_unix(posix_errno);
314 if (errno == EMFILE) {
315 static time_t last_warned = 0L;
317 if (time((time_t *) NULL) > last_warned) {
318 DEBUG(0,("Too many open files, unable "
319 "to open more! smbd's max "
321 lp_max_open_files()));
322 last_warned = time((time_t *) NULL);
328 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
329 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
330 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
335 /****************************************************************************
336 Close the file associated with a fsp.
337 ****************************************************************************/
339 NTSTATUS fd_close(files_struct *fsp)
346 if (fsp->fh->fd == -1) {
347 return NT_STATUS_OK; /* What we used to call a stat open. */
349 if (fsp->fh->ref_count > 1) {
350 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
353 ret = SMB_VFS_CLOSE(fsp);
356 return map_nt_error_from_unix(errno);
361 /****************************************************************************
362 Change the ownership of a file to that of the parent directory.
363 Do this by fd if possible.
364 ****************************************************************************/
366 void change_file_owner_to_parent(connection_struct *conn,
367 const char *inherit_from_dir,
370 struct smb_filename *smb_fname_parent = NULL;
374 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
375 NULL, NULL, &smb_fname_parent);
376 if (!NT_STATUS_IS_OK(status)) {
380 ret = SMB_VFS_STAT(conn, smb_fname_parent);
382 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
383 "directory %s. Error was %s\n",
384 smb_fname_str_dbg(smb_fname_parent),
386 TALLOC_FREE(smb_fname_parent);
390 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
391 /* Already this uid - no need to change. */
392 DEBUG(10,("change_file_owner_to_parent: file %s "
393 "is already owned by uid %d\n",
395 (int)fsp->fsp_name->st.st_ex_uid ));
396 TALLOC_FREE(smb_fname_parent);
401 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
404 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
405 "file %s to parent directory uid %u. Error "
406 "was %s\n", fsp_str_dbg(fsp),
407 (unsigned int)smb_fname_parent->st.st_ex_uid,
410 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
411 "parent directory uid %u.\n", fsp_str_dbg(fsp),
412 (unsigned int)smb_fname_parent->st.st_ex_uid));
413 /* Ensure the uid entry is updated. */
414 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
417 TALLOC_FREE(smb_fname_parent);
420 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
421 const char *inherit_from_dir,
423 SMB_STRUCT_STAT *psbuf)
425 struct smb_filename *smb_fname_parent = NULL;
426 struct smb_filename *smb_fname_cwd = NULL;
427 char *saved_dir = NULL;
428 TALLOC_CTX *ctx = talloc_tos();
429 NTSTATUS status = NT_STATUS_OK;
432 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
434 if (!NT_STATUS_IS_OK(status)) {
438 ret = SMB_VFS_STAT(conn, smb_fname_parent);
440 status = map_nt_error_from_unix(errno);
441 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
442 "directory %s. Error was %s\n",
443 smb_fname_str_dbg(smb_fname_parent),
448 /* We've already done an lstat into psbuf, and we know it's a
449 directory. If we can cd into the directory and the dev/ino
450 are the same then we can safely chown without races as
451 we're locking the directory in place by being in it. This
452 should work on any UNIX (thanks tridge :-). JRA.
455 saved_dir = vfs_GetWd(ctx,conn);
457 status = map_nt_error_from_unix(errno);
458 DEBUG(0,("change_dir_owner_to_parent: failed to get "
459 "current working directory. Error was %s\n",
464 /* Chdir into the new path. */
465 if (vfs_ChDir(conn, fname) == -1) {
466 status = map_nt_error_from_unix(errno);
467 DEBUG(0,("change_dir_owner_to_parent: failed to change "
468 "current working directory to %s. Error "
469 "was %s\n", fname, strerror(errno) ));
473 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
475 if (!NT_STATUS_IS_OK(status)) {
479 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
481 status = map_nt_error_from_unix(errno);
482 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
483 "directory '.' (%s) Error was %s\n",
484 fname, strerror(errno)));
488 /* Ensure we're pointing at the same place. */
489 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
490 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
491 DEBUG(0,("change_dir_owner_to_parent: "
492 "device/inode on directory %s changed. "
493 "Refusing to chown !\n", fname ));
494 status = NT_STATUS_ACCESS_DENIED;
498 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
499 /* Already this uid - no need to change. */
500 DEBUG(10,("change_dir_owner_to_parent: directory %s "
501 "is already owned by uid %d\n",
503 (int)smb_fname_cwd->st.st_ex_uid ));
504 status = NT_STATUS_OK;
509 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
513 status = map_nt_error_from_unix(errno);
514 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
515 "directory %s to parent directory uid %u. "
516 "Error was %s\n", fname,
517 (unsigned int)smb_fname_parent->st.st_ex_uid,
520 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
521 "directory %s to parent directory uid %u.\n",
522 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
523 /* Ensure the uid entry is updated. */
524 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
528 vfs_ChDir(conn,saved_dir);
530 TALLOC_FREE(smb_fname_parent);
531 TALLOC_FREE(smb_fname_cwd);
535 /****************************************************************************
537 ****************************************************************************/
539 static NTSTATUS open_file(files_struct *fsp,
540 connection_struct *conn,
541 struct smb_request *req,
542 const char *parent_dir,
545 uint32 access_mask, /* client requested access mask. */
546 uint32 open_access_mask) /* what we're actually using in the open. */
548 struct smb_filename *smb_fname = fsp->fsp_name;
549 NTSTATUS status = NT_STATUS_OK;
550 int accmode = (flags & O_ACCMODE);
551 int local_flags = flags;
552 bool file_existed = VALID_STAT(fsp->fsp_name->st);
553 bool file_created = false;
558 /* Check permissions */
561 * This code was changed after seeing a client open request
562 * containing the open mode of (DENY_WRITE/read-only) with
563 * the 'create if not exist' bit set. The previous code
564 * would fail to open the file read only on a read-only share
565 * as it was checking the flags parameter directly against O_RDONLY,
566 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
570 if (!CAN_WRITE(conn)) {
571 /* It's a read-only share - fail if we wanted to write. */
572 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
573 DEBUG(3,("Permission denied opening %s\n",
574 smb_fname_str_dbg(smb_fname)));
575 return NT_STATUS_ACCESS_DENIED;
576 } else if(flags & O_CREAT) {
577 /* We don't want to write - but we must make sure that
578 O_CREAT doesn't create the file if we have write
579 access into the directory.
581 flags &= ~(O_CREAT|O_EXCL);
582 local_flags &= ~(O_CREAT|O_EXCL);
587 * This little piece of insanity is inspired by the
588 * fact that an NT client can open a file for O_RDONLY,
589 * but set the create disposition to FILE_EXISTS_TRUNCATE.
590 * If the client *can* write to the file, then it expects to
591 * truncate the file, even though it is opening for readonly.
592 * Quicken uses this stupid trick in backup file creation...
593 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
594 * for helping track this one down. It didn't bite us in 2.0.x
595 * as we always opened files read-write in that release. JRA.
598 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
599 DEBUG(10,("open_file: truncate requested on read-only open "
600 "for file %s\n", smb_fname_str_dbg(smb_fname)));
601 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
604 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
605 (!file_existed && (local_flags & O_CREAT)) ||
606 ((local_flags & O_TRUNC) == O_TRUNC) ) {
610 * We can't actually truncate here as the file may be locked.
611 * open_file_ntcreate will take care of the truncate later. JRA.
614 local_flags &= ~O_TRUNC;
616 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
618 * We would block on opening a FIFO with no one else on the
619 * other end. Do what we used to do and add O_NONBLOCK to the
623 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
624 local_flags |= O_NONBLOCK;
628 /* Don't create files with Microsoft wildcard characters. */
631 * wildcard characters are allowed in stream names
632 * only test the basefilename
634 wild = fsp->base_fsp->fsp_name->base_name;
636 wild = smb_fname->base_name;
638 if ((local_flags & O_CREAT) && !file_existed &&
640 return NT_STATUS_OBJECT_NAME_INVALID;
643 /* Can we access this file ? */
644 if (!fsp->base_fsp) {
645 /* Only do this check on non-stream open. */
647 status = smbd_check_access_rights(conn,
650 } else if (local_flags & O_CREAT){
651 status = check_parent_access(conn,
655 /* File didn't exist and no O_CREAT. */
656 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
658 if (!NT_STATUS_IS_OK(status)) {
659 DEBUG(10,("open_file: "
663 "smbd_check_access_rights" :
664 "check_parent_access",
665 smb_fname_str_dbg(smb_fname),
666 nt_errstr(status) ));
671 /* Actually do the open */
672 status = fd_open(conn, fsp, local_flags, unx_mode);
673 if (!NT_STATUS_IS_OK(status)) {
674 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
675 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
676 nt_errstr(status),local_flags,flags));
680 if ((local_flags & O_CREAT) && !file_existed) {
685 fsp->fh->fd = -1; /* What we used to call a stat open. */
687 /* File must exist for a stat open. */
688 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
691 status = smbd_check_access_rights(conn,
695 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
697 S_ISLNK(smb_fname->st.st_ex_mode)) {
698 /* This is a POSIX stat open for delete
699 * or rename on a symlink that points
701 DEBUG(10,("open_file: allowing POSIX "
702 "open on bad symlink %s\n",
703 smb_fname_str_dbg(smb_fname)));
704 status = NT_STATUS_OK;
707 if (!NT_STATUS_IS_OK(status)) {
708 DEBUG(10,("open_file: "
709 "smbd_check_access_rights on file "
711 smb_fname_str_dbg(smb_fname),
712 nt_errstr(status) ));
720 if (fsp->fh->fd == -1) {
721 ret = SMB_VFS_STAT(conn, smb_fname);
723 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
724 /* If we have an fd, this stat should succeed. */
726 DEBUG(0,("Error doing fstat on open file %s "
728 smb_fname_str_dbg(smb_fname),
733 /* For a non-io open, this stat failing means file not found. JRA */
735 status = map_nt_error_from_unix(errno);
741 bool need_re_stat = false;
742 /* Do all inheritance work after we've
743 done a successful stat call and filled
744 in the stat struct in fsp->fsp_name. */
746 /* Inherit the ACL if required */
747 if (lp_inherit_perms(SNUM(conn))) {
748 inherit_access_posix_acl(conn, parent_dir,
749 smb_fname->base_name,
754 /* Change the owner if required. */
755 if (lp_inherit_owner(SNUM(conn))) {
756 change_file_owner_to_parent(conn, parent_dir,
762 if (fsp->fh->fd == -1) {
763 ret = SMB_VFS_STAT(conn, smb_fname);
765 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
766 /* If we have an fd, this stat should succeed. */
768 DEBUG(0,("Error doing fstat on open file %s "
770 smb_fname_str_dbg(smb_fname),
776 notify_fname(conn, NOTIFY_ACTION_ADDED,
777 FILE_NOTIFY_CHANGE_FILE_NAME,
778 smb_fname->base_name);
783 * POSIX allows read-only opens of directories. We don't
784 * want to do this (we use a different code path for this)
785 * so catch a directory open and return an EISDIR. JRA.
788 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
791 return NT_STATUS_FILE_IS_A_DIRECTORY;
794 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
795 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
796 fsp->file_pid = req ? req->smbpid : 0;
797 fsp->can_lock = True;
798 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
801 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
802 fsp->print_file = NULL;
803 fsp->modified = False;
804 fsp->sent_oplock_break = NO_BREAK_SENT;
805 fsp->is_directory = False;
806 if (conn->aio_write_behind_list &&
807 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
808 conn->case_sensitive)) {
809 fsp->aio_write_behind = True;
812 fsp->wcp = NULL; /* Write cache pointer. */
814 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
815 conn->session_info->unix_info->unix_name,
816 smb_fname_str_dbg(smb_fname),
817 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
818 conn->num_files_open));
824 /****************************************************************************
825 Check if we can open a file with a share mode.
826 Returns True if conflict, False if not.
827 ****************************************************************************/
829 static bool share_conflict(struct share_mode_entry *entry,
833 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
834 "entry->share_access = 0x%x, "
835 "entry->private_options = 0x%x\n",
836 (unsigned int)entry->access_mask,
837 (unsigned int)entry->share_access,
838 (unsigned int)entry->private_options));
840 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
841 (unsigned int)access_mask, (unsigned int)share_access));
843 if ((entry->access_mask & (FILE_WRITE_DATA|
847 DELETE_ACCESS)) == 0) {
848 DEBUG(10,("share_conflict: No conflict due to "
849 "entry->access_mask = 0x%x\n",
850 (unsigned int)entry->access_mask ));
854 if ((access_mask & (FILE_WRITE_DATA|
858 DELETE_ACCESS)) == 0) {
859 DEBUG(10,("share_conflict: No conflict due to "
860 "access_mask = 0x%x\n",
861 (unsigned int)access_mask ));
865 #if 1 /* JRA TEST - Superdebug. */
866 #define CHECK_MASK(num, am, right, sa, share) \
867 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
868 (unsigned int)(num), (unsigned int)(am), \
869 (unsigned int)(right), (unsigned int)(am)&(right) )); \
870 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
871 (unsigned int)(num), (unsigned int)(sa), \
872 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
873 if (((am) & (right)) && !((sa) & (share))) { \
874 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
875 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
876 (unsigned int)(share) )); \
880 #define CHECK_MASK(num, am, right, sa, share) \
881 if (((am) & (right)) && !((sa) & (share))) { \
882 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
883 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
884 (unsigned int)(share) )); \
889 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
890 share_access, FILE_SHARE_WRITE);
891 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
892 entry->share_access, FILE_SHARE_WRITE);
894 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
895 share_access, FILE_SHARE_READ);
896 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
897 entry->share_access, FILE_SHARE_READ);
899 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
900 share_access, FILE_SHARE_DELETE);
901 CHECK_MASK(6, access_mask, DELETE_ACCESS,
902 entry->share_access, FILE_SHARE_DELETE);
904 DEBUG(10,("share_conflict: No conflict.\n"));
908 #if defined(DEVELOPER)
909 static void validate_my_share_entries(struct smbd_server_connection *sconn,
911 struct share_mode_entry *share_entry)
913 struct server_id self = messaging_server_id(sconn->msg_ctx);
916 if (!serverid_equal(&self, &share_entry->pid)) {
920 if (is_deferred_open_entry(share_entry) &&
921 !open_was_deferred(sconn, share_entry->op_mid)) {
922 char *str = talloc_asprintf(talloc_tos(),
923 "Got a deferred entry without a request: "
925 share_mode_str(talloc_tos(), num, share_entry));
929 if (!is_valid_share_mode_entry(share_entry)) {
933 fsp = file_find_dif(sconn, share_entry->id,
934 share_entry->share_file_id);
936 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
937 share_mode_str(talloc_tos(), num, share_entry) ));
938 smb_panic("validate_my_share_entries: Cannot match a "
939 "share entry with an open file\n");
942 if (is_deferred_open_entry(share_entry)) {
946 if ((share_entry->op_type == NO_OPLOCK) &&
947 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
948 /* Someone has already written to it, but I haven't yet
953 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
962 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
963 share_mode_str(talloc_tos(), num, share_entry) ));
964 str = talloc_asprintf(talloc_tos(),
965 "validate_my_share_entries: "
966 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
967 fsp->fsp_name->base_name,
968 (unsigned int)fsp->oplock_type,
969 (unsigned int)share_entry->op_type );
975 bool is_stat_open(uint32 access_mask)
977 return (access_mask &&
978 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
979 FILE_WRITE_ATTRIBUTES))==0) &&
980 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
981 FILE_WRITE_ATTRIBUTES)) != 0));
984 /****************************************************************************
985 Deal with share modes
986 Invarient: Share mode must be locked on entry and exit.
987 Returns -1 on error, or number of share modes on success (may be zero).
988 ****************************************************************************/
990 static NTSTATUS open_mode_check(connection_struct *conn,
991 struct share_mode_lock *lck,
995 uint32 create_options,
1000 if(lck->data->num_share_modes == 0) {
1001 return NT_STATUS_OK;
1004 /* A delete on close prohibits everything */
1006 if (is_delete_on_close_set(lck, name_hash)) {
1008 * Check the delete on close token
1009 * is valid. It could have been left
1010 * after a server crash.
1012 for(i = 0; i < lck->data->num_share_modes; i++) {
1013 if (!share_mode_stale_pid(lck->data, i)) {
1015 *file_existed = true;
1017 return NT_STATUS_DELETE_PENDING;
1020 return NT_STATUS_OK;
1023 if (is_stat_open(access_mask)) {
1024 /* Stat open that doesn't trigger oplock breaks or share mode
1025 * checks... ! JRA. */
1026 return NT_STATUS_OK;
1030 * Check if the share modes will give us access.
1033 #if defined(DEVELOPER)
1034 for(i = 0; i < lck->data->num_share_modes; i++) {
1035 validate_my_share_entries(conn->sconn, i,
1036 &lck->data->share_modes[i]);
1040 if (!lp_share_modes(SNUM(conn))) {
1041 return NT_STATUS_OK;
1044 /* Now we check the share modes, after any oplock breaks. */
1045 for(i = 0; i < lck->data->num_share_modes; i++) {
1047 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1051 /* someone else has a share lock on it, check to see if we can
1053 if (share_conflict(&lck->data->share_modes[i],
1054 access_mask, share_access)) {
1056 if (share_mode_stale_pid(lck->data, i)) {
1060 *file_existed = true;
1062 return NT_STATUS_SHARING_VIOLATION;
1066 if (lck->data->num_share_modes != 0) {
1067 *file_existed = true;
1070 return NT_STATUS_OK;
1073 static bool is_delete_request(files_struct *fsp) {
1074 return ((fsp->access_mask == DELETE_ACCESS) &&
1075 (fsp->oplock_type == NO_OPLOCK));
1079 * Send a break message to the oplock holder and delay the open for
1083 static NTSTATUS send_break_message(files_struct *fsp,
1084 struct share_mode_entry *exclusive,
1089 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1091 DEBUG(10, ("Sending break request to PID %s\n",
1092 procid_str_static(&exclusive->pid)));
1093 exclusive->op_mid = mid;
1095 /* Create the message. */
1096 share_mode_entry_to_message(msg, exclusive);
1098 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1099 don't want this set in the share mode struct pointed to by lck. */
1101 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1102 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1103 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1106 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1107 MSG_SMB_BREAK_REQUEST,
1109 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1110 if (!NT_STATUS_IS_OK(status)) {
1111 DEBUG(3, ("Could not send oplock break message: %s\n",
1112 nt_errstr(status)));
1119 * Return share_mode_entry pointers for :
1120 * 1). Batch oplock entry.
1121 * 2). Batch or exclusive oplock entry (may be identical to #1).
1122 * bool have_level2_oplock
1123 * bool have_no_oplock.
1124 * Do internal consistency checks on the share mode for a file.
1127 static void find_oplock_types(files_struct *fsp,
1129 const struct share_mode_lock *lck,
1130 struct share_mode_entry **pp_batch,
1131 struct share_mode_entry **pp_ex_or_batch,
1133 bool *got_no_oplock)
1138 *pp_ex_or_batch = NULL;
1139 *got_level2 = false;
1140 *got_no_oplock = false;
1142 /* Ignore stat or internal opens, as is done in
1143 delay_for_batch_oplocks() and
1144 delay_for_exclusive_oplocks().
1146 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1150 for (i=0; i<lck->data->num_share_modes; i++) {
1151 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1155 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1156 is_stat_open(lck->data->share_modes[i].access_mask)) {
1157 /* We ignore stat opens in the table - they
1158 always have NO_OPLOCK and never get or
1159 cause breaks. JRA. */
1163 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1164 /* batch - can only be one. */
1165 if (share_mode_stale_pid(lck->data, i)) {
1166 DEBUG(10, ("Found stale batch oplock\n"));
1169 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1170 smb_panic("Bad batch oplock entry.");
1172 *pp_batch = &lck->data->share_modes[i];
1175 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1176 if (share_mode_stale_pid(lck->data, i)) {
1177 DEBUG(10, ("Found stale duplicate oplock\n"));
1180 /* Exclusive or batch - can only be one. */
1181 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1182 smb_panic("Bad exclusive or batch oplock entry.");
1184 *pp_ex_or_batch = &lck->data->share_modes[i];
1187 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1188 if (*pp_batch || *pp_ex_or_batch) {
1189 if (share_mode_stale_pid(lck->data, i)) {
1190 DEBUG(10, ("Found stale LevelII "
1194 smb_panic("Bad levelII oplock entry.");
1199 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1200 if (*pp_batch || *pp_ex_or_batch) {
1201 if (share_mode_stale_pid(lck->data, i)) {
1202 DEBUG(10, ("Found stale NO_OPLOCK "
1206 smb_panic("Bad no oplock entry.");
1208 *got_no_oplock = true;
1213 static bool delay_for_batch_oplocks(files_struct *fsp,
1216 struct share_mode_entry *batch_entry)
1218 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1221 if (batch_entry == NULL) {
1225 /* Found a batch oplock */
1226 send_break_message(fsp, batch_entry, mid, oplock_request);
1230 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1233 struct share_mode_entry *ex_entry)
1237 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1240 if (ex_entry == NULL) {
1244 /* Found an exclusive or batch oplock */
1246 delay_it = is_delete_request(fsp) ?
1247 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1253 send_break_message(fsp, ex_entry, mid, oplock_request);
1257 static bool file_has_brlocks(files_struct *fsp)
1259 struct byte_range_lock *br_lck;
1261 br_lck = brl_get_locks_readonly(fsp);
1265 return br_lck->num_locks > 0 ? true : false;
1268 static void grant_fsp_oplock_type(files_struct *fsp,
1270 bool got_level2_oplock,
1271 bool got_a_none_oplock)
1273 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1274 lp_level2_oplocks(SNUM(fsp->conn));
1276 /* Start by granting what the client asked for,
1277 but ensure no SAMBA_PRIVATE bits can be set. */
1278 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1280 if (oplock_request & INTERNAL_OPEN_ONLY) {
1281 /* No oplocks on internal open. */
1282 fsp->oplock_type = NO_OPLOCK;
1283 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1284 fsp->oplock_type, fsp_str_dbg(fsp)));
1288 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1289 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1291 fsp->oplock_type = NO_OPLOCK;
1294 if (is_stat_open(fsp->access_mask)) {
1295 /* Leave the value already set. */
1296 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1297 fsp->oplock_type, fsp_str_dbg(fsp)));
1302 * Match what was requested (fsp->oplock_type) with
1303 * what was found in the existing share modes.
1306 if (got_a_none_oplock) {
1307 fsp->oplock_type = NO_OPLOCK;
1308 } else if (got_level2_oplock) {
1309 if (fsp->oplock_type == NO_OPLOCK ||
1310 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1311 /* Store a level2 oplock, but don't tell the client */
1312 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1314 fsp->oplock_type = LEVEL_II_OPLOCK;
1317 /* All share_mode_entries are placeholders or deferred.
1318 * Silently upgrade to fake levelII if the client didn't
1319 * ask for an oplock. */
1320 if (fsp->oplock_type == NO_OPLOCK) {
1321 /* Store a level2 oplock, but don't tell the client */
1322 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1327 * Don't grant level2 to clients that don't want them
1328 * or if we've turned them off.
1330 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1331 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1334 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1335 fsp->oplock_type, fsp_str_dbg(fsp)));
1338 bool request_timed_out(struct timeval request_time,
1339 struct timeval timeout)
1341 struct timeval now, end_time;
1343 end_time = timeval_sum(&request_time, &timeout);
1344 return (timeval_compare(&end_time, &now) < 0);
1347 /****************************************************************************
1348 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1349 ****************************************************************************/
1351 static void defer_open(struct share_mode_lock *lck,
1352 struct timeval request_time,
1353 struct timeval timeout,
1354 struct smb_request *req,
1355 struct deferred_open_record *state)
1357 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1359 /* Paranoia check */
1364 for (i=0; i<lck->data->num_share_modes; i++) {
1365 struct share_mode_entry *e = &lck->data->share_modes[i];
1367 if (is_deferred_open_entry(e) &&
1368 serverid_equal(&self, &e->pid) &&
1369 (e->op_mid == req->mid)) {
1370 DEBUG(0, ("Trying to defer an already deferred "
1371 "request: mid=%llu, exiting\n",
1372 (unsigned long long)req->mid));
1373 exit_server("attempt to defer a deferred request");
1378 /* End paranoia check */
1380 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1381 "open entry for mid %llu\n",
1382 (unsigned int)request_time.tv_sec,
1383 (unsigned int)request_time.tv_usec,
1384 (unsigned long long)req->mid));
1386 if (!push_deferred_open_message_smb(req, request_time, timeout,
1387 state->id, (char *)state, sizeof(*state))) {
1388 exit_server("push_deferred_open_message_smb failed");
1391 add_deferred_open(lck, req->mid, request_time, self, state->id);
1396 /****************************************************************************
1397 On overwrite open ensure that the attributes match.
1398 ****************************************************************************/
1400 bool open_match_attributes(connection_struct *conn,
1401 uint32 old_dos_attr,
1402 uint32 new_dos_attr,
1403 mode_t existing_unx_mode,
1404 mode_t new_unx_mode,
1405 mode_t *returned_unx_mode)
1407 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1409 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1410 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1412 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1413 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1414 *returned_unx_mode = new_unx_mode;
1416 *returned_unx_mode = (mode_t)0;
1419 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1420 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1421 "returned_unx_mode = 0%o\n",
1422 (unsigned int)old_dos_attr,
1423 (unsigned int)existing_unx_mode,
1424 (unsigned int)new_dos_attr,
1425 (unsigned int)*returned_unx_mode ));
1427 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1428 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1429 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1430 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1434 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1435 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1436 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1443 /****************************************************************************
1444 Special FCB or DOS processing in the case of a sharing violation.
1445 Try and find a duplicated file handle.
1446 ****************************************************************************/
1448 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1449 connection_struct *conn,
1450 files_struct *fsp_to_dup_into,
1451 const struct smb_filename *smb_fname,
1456 uint32 share_access,
1457 uint32 create_options)
1461 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1462 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1464 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1465 fsp = file_find_di_next(fsp)) {
1467 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1468 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1469 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1470 fsp->fh->fd, (unsigned long long)fsp->vuid,
1471 (unsigned int)fsp->file_pid,
1472 (unsigned int)fsp->fh->private_options,
1473 (unsigned int)fsp->access_mask ));
1475 if (fsp->fh->fd != -1 &&
1476 fsp->vuid == vuid &&
1477 fsp->file_pid == file_pid &&
1478 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1479 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1480 (fsp->access_mask & FILE_WRITE_DATA) &&
1481 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1482 strequal(fsp->fsp_name->stream_name,
1483 smb_fname->stream_name)) {
1484 DEBUG(10,("fcb_or_dos_open: file match\n"));
1490 return NT_STATUS_NOT_FOUND;
1493 /* quite an insane set of semantics ... */
1494 if (is_executable(smb_fname->base_name) &&
1495 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1496 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1497 return NT_STATUS_INVALID_PARAMETER;
1500 /* We need to duplicate this fsp. */
1501 return dup_file_fsp(req, fsp, access_mask, share_access,
1502 create_options, fsp_to_dup_into);
1505 static void schedule_defer_open(struct share_mode_lock *lck,
1506 struct timeval request_time,
1507 struct smb_request *req)
1509 struct deferred_open_record state;
1511 /* This is a relative time, added to the absolute
1512 request_time value to get the absolute timeout time.
1513 Note that if this is the second or greater time we enter
1514 this codepath for this particular request mid then
1515 request_time is left as the absolute time of the *first*
1516 time this request mid was processed. This is what allows
1517 the request to eventually time out. */
1519 struct timeval timeout;
1521 /* Normally the smbd we asked should respond within
1522 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1523 * the client did, give twice the timeout as a safety
1524 * measure here in case the other smbd is stuck
1525 * somewhere else. */
1527 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1529 /* Nothing actually uses state.delayed_for_oplocks
1530 but it's handy to differentiate in debug messages
1531 between a 30 second delay due to oplock break, and
1532 a 1 second delay for share mode conflicts. */
1534 state.delayed_for_oplocks = True;
1535 state.id = lck->data->id;
1537 if (!request_timed_out(request_time, timeout)) {
1538 defer_open(lck, request_time, timeout, req, &state);
1542 /****************************************************************************
1543 Work out what access_mask to use from what the client sent us.
1544 ****************************************************************************/
1546 static NTSTATUS smbd_calculate_maximum_allowed_access(
1547 connection_struct *conn,
1548 const struct smb_filename *smb_fname,
1549 uint32_t *p_access_mask)
1551 struct security_descriptor *sd;
1552 uint32_t access_granted;
1555 if (get_current_uid(conn) == (uid_t)0) {
1556 *p_access_mask |= FILE_GENERIC_ALL;
1557 return NT_STATUS_OK;
1560 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1565 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1567 * File did not exist
1569 *p_access_mask = FILE_GENERIC_ALL;
1570 return NT_STATUS_OK;
1572 if (!NT_STATUS_IS_OK(status)) {
1573 DEBUG(10,("smbd_calculate_access_mask: "
1574 "Could not get acl on file %s: %s\n",
1575 smb_fname_str_dbg(smb_fname),
1576 nt_errstr(status)));
1577 return NT_STATUS_ACCESS_DENIED;
1581 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1582 * also takes care of owner WRITE_DAC and READ_CONTROL.
1584 status = se_access_check(sd,
1585 get_current_nttok(conn),
1586 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1591 if (!NT_STATUS_IS_OK(status)) {
1592 DEBUG(10, ("smbd_calculate_access_mask: "
1593 "Access denied on file %s: "
1594 "when calculating maximum access\n",
1595 smb_fname_str_dbg(smb_fname)));
1596 return NT_STATUS_ACCESS_DENIED;
1598 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1599 return NT_STATUS_OK;
1602 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1603 const struct smb_filename *smb_fname,
1604 uint32_t access_mask,
1605 uint32_t *access_mask_out)
1608 uint32_t orig_access_mask = access_mask;
1609 uint32_t rejected_share_access;
1612 * Convert GENERIC bits to specific bits.
1615 se_map_generic(&access_mask, &file_generic_mapping);
1617 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1618 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1620 status = smbd_calculate_maximum_allowed_access(
1621 conn, smb_fname, &access_mask);
1623 if (!NT_STATUS_IS_OK(status)) {
1627 access_mask &= conn->share_access;
1630 rejected_share_access = access_mask & ~(conn->share_access);
1632 if (rejected_share_access) {
1633 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1634 "file %s: rejected by share access mask[0x%08X] "
1635 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1636 smb_fname_str_dbg(smb_fname),
1638 orig_access_mask, access_mask,
1639 rejected_share_access));
1640 return NT_STATUS_ACCESS_DENIED;
1643 *access_mask_out = access_mask;
1644 return NT_STATUS_OK;
1647 /****************************************************************************
1648 Remove the deferred open entry under lock.
1649 ****************************************************************************/
1651 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1652 struct server_id pid)
1654 struct share_mode_lock *lck = get_existing_share_mode_lock(
1657 DEBUG(0, ("could not get share mode lock\n"));
1660 del_deferred_open_entry(lck, mid, pid);
1664 /****************************************************************************
1665 Open a file with a share mode. Passed in an already created files_struct *.
1666 ****************************************************************************/
1668 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1669 struct smb_request *req,
1670 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1671 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1672 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1673 uint32 create_options, /* options such as delete on close. */
1674 uint32 new_dos_attributes, /* attributes used for new file. */
1675 int oplock_request, /* internal Samba oplock codes. */
1676 /* Information (FILE_EXISTS etc.) */
1677 uint32_t private_flags, /* Samba specific flags. */
1681 struct smb_filename *smb_fname = fsp->fsp_name;
1684 bool file_existed = VALID_STAT(smb_fname->st);
1685 bool def_acl = False;
1686 bool posix_open = False;
1687 bool new_file_created = False;
1688 bool clear_ads = false;
1689 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1690 mode_t new_unx_mode = (mode_t)0;
1691 mode_t unx_mode = (mode_t)0;
1693 uint32 existing_dos_attributes = 0;
1694 struct timeval request_time = timeval_zero();
1695 struct share_mode_lock *lck = NULL;
1696 uint32 open_access_mask = access_mask;
1700 if (conn->printer) {
1702 * Printers are handled completely differently.
1703 * Most of the passed parameters are ignored.
1707 *pinfo = FILE_WAS_CREATED;
1710 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1711 smb_fname_str_dbg(smb_fname)));
1714 DEBUG(0,("open_file_ntcreate: printer open without "
1715 "an SMB request!\n"));
1716 return NT_STATUS_INTERNAL_ERROR;
1719 return print_spool_open(fsp, smb_fname->base_name,
1723 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1725 return NT_STATUS_NO_MEMORY;
1728 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1730 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1731 new_dos_attributes = 0;
1733 /* Windows allows a new file to be created and
1734 silently removes a FILE_ATTRIBUTE_DIRECTORY
1735 sent by the client. Do the same. */
1737 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1739 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1741 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1742 smb_fname, parent_dir);
1745 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1746 "access_mask=0x%x share_access=0x%x "
1747 "create_disposition = 0x%x create_options=0x%x "
1748 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1749 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1750 access_mask, share_access, create_disposition,
1751 create_options, (unsigned int)unx_mode, oplock_request,
1752 (unsigned int)private_flags));
1754 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1755 DEBUG(0, ("No smb request but not an internal only open!\n"));
1756 return NT_STATUS_INTERNAL_ERROR;
1760 * Only non-internal opens can be deferred at all
1765 if (get_deferred_open_message_state(req,
1769 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1770 /* Remember the absolute time of the original
1771 request with this mid. We'll use it later to
1772 see if this has timed out. */
1774 /* Remove the deferred open entry under lock. */
1775 remove_deferred_open_entry(
1776 state->id, req->mid,
1777 messaging_server_id(req->sconn->msg_ctx));
1779 /* Ensure we don't reprocess this message. */
1780 remove_deferred_open_message_smb(req->sconn, req->mid);
1785 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1787 existing_dos_attributes = dos_mode(conn, smb_fname);
1791 /* ignore any oplock requests if oplocks are disabled */
1792 if (!lp_oplocks(SNUM(conn)) ||
1793 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1794 /* Mask off everything except the private Samba bits. */
1795 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1798 /* this is for OS/2 long file names - say we don't support them */
1799 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1800 /* OS/2 Workplace shell fix may be main code stream in a later
1802 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1804 if (use_nt_status()) {
1805 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1807 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1810 switch( create_disposition ) {
1812 * Currently we're using FILE_SUPERSEDE as the same as
1813 * FILE_OVERWRITE_IF but they really are
1814 * different. FILE_SUPERSEDE deletes an existing file
1815 * (requiring delete access) then recreates it.
1817 case FILE_SUPERSEDE:
1818 /* If file exists replace/overwrite. If file doesn't
1820 flags2 |= (O_CREAT | O_TRUNC);
1824 case FILE_OVERWRITE_IF:
1825 /* If file exists replace/overwrite. If file doesn't
1827 flags2 |= (O_CREAT | O_TRUNC);
1832 /* If file exists open. If file doesn't exist error. */
1833 if (!file_existed) {
1834 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1835 "requested for file %s and file "
1837 smb_fname_str_dbg(smb_fname)));
1839 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1843 case FILE_OVERWRITE:
1844 /* If file exists overwrite. If file doesn't exist
1846 if (!file_existed) {
1847 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1848 "requested for file %s and file "
1850 smb_fname_str_dbg(smb_fname) ));
1852 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1859 /* If file exists error. If file doesn't exist
1862 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1863 "requested for file %s and file "
1864 "already exists.\n",
1865 smb_fname_str_dbg(smb_fname)));
1866 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1871 return map_nt_error_from_unix(errno);
1873 flags2 |= (O_CREAT|O_EXCL);
1877 /* If file exists open. If file doesn't exist
1883 return NT_STATUS_INVALID_PARAMETER;
1886 /* We only care about matching attributes on file exists and
1889 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1890 (create_disposition == FILE_OVERWRITE_IF))) {
1891 if (!open_match_attributes(conn, existing_dos_attributes,
1893 smb_fname->st.st_ex_mode,
1894 unx_mode, &new_unx_mode)) {
1895 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1896 "for file %s (%x %x) (0%o, 0%o)\n",
1897 smb_fname_str_dbg(smb_fname),
1898 existing_dos_attributes,
1900 (unsigned int)smb_fname->st.st_ex_mode,
1901 (unsigned int)unx_mode ));
1903 return NT_STATUS_ACCESS_DENIED;
1907 status = smbd_calculate_access_mask(conn, smb_fname,
1910 if (!NT_STATUS_IS_OK(status)) {
1911 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1912 "on file %s returned %s\n",
1913 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1917 open_access_mask = access_mask;
1919 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1920 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1923 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1924 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1928 * Note that we ignore the append flag as append does not
1929 * mean the same thing under DOS and Unix.
1932 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1933 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1934 /* DENY_DOS opens are always underlying read-write on the
1935 file handle, no matter what the requested access mask
1937 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1938 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1948 * Currently we only look at FILE_WRITE_THROUGH for create options.
1952 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1957 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1961 if (!posix_open && !CAN_WRITE(conn)) {
1963 * We should really return a permission denied error if either
1964 * O_CREAT or O_TRUNC are set, but for compatibility with
1965 * older versions of Samba we just AND them out.
1967 flags2 &= ~(O_CREAT|O_TRUNC);
1971 * Ensure we can't write on a read-only share or file.
1974 if (flags != O_RDONLY && file_existed &&
1975 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1976 DEBUG(5,("open_file_ntcreate: write access requested for "
1977 "file %s on read only %s\n",
1978 smb_fname_str_dbg(smb_fname),
1979 !CAN_WRITE(conn) ? "share" : "file" ));
1981 return NT_STATUS_ACCESS_DENIED;
1984 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1985 fsp->share_access = share_access;
1986 fsp->fh->private_options = private_flags;
1987 fsp->access_mask = open_access_mask; /* We change this to the
1988 * requested access_mask after
1989 * the open is done. */
1990 fsp->posix_open = posix_open;
1992 /* Ensure no SAMBA_PRIVATE bits can be set. */
1993 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1995 if (timeval_is_zero(&request_time)) {
1996 request_time = fsp->open_time;
2000 struct share_mode_entry *batch_entry = NULL;
2001 struct share_mode_entry *exclusive_entry = NULL;
2002 bool got_level2_oplock = false;
2003 bool got_a_none_oplock = false;
2006 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2007 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2009 lck = get_share_mode_lock(talloc_tos(), id,
2011 smb_fname, &old_write_time);
2013 DEBUG(0, ("Could not get share mode lock\n"));
2014 return NT_STATUS_SHARING_VIOLATION;
2017 /* Get the types we need to examine. */
2018 find_oplock_types(fsp,
2024 &got_a_none_oplock);
2026 /* First pass - send break only on batch oplocks. */
2027 if ((req != NULL) &&
2028 delay_for_batch_oplocks(fsp,
2032 schedule_defer_open(lck, request_time, req);
2034 return NT_STATUS_SHARING_VIOLATION;
2037 /* Use the client requested access mask here, not the one we
2039 status = open_mode_check(conn, lck, fsp->name_hash,
2040 access_mask, share_access,
2041 create_options, &file_existed);
2043 if (NT_STATUS_IS_OK(status)) {
2044 /* We might be going to allow this open. Check oplock
2046 /* Second pass - send break for both batch or
2047 * exclusive oplocks. */
2048 if ((req != NULL) &&
2049 delay_for_exclusive_oplocks(
2054 schedule_defer_open(lck, request_time, req);
2056 return NT_STATUS_SHARING_VIOLATION;
2060 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2061 /* DELETE_PENDING is not deferred for a second */
2066 grant_fsp_oplock_type(fsp,
2071 if (!NT_STATUS_IS_OK(status)) {
2072 uint32 can_access_mask;
2073 bool can_access = True;
2075 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2077 /* Check if this can be done with the deny_dos and fcb
2080 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2081 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2083 DEBUG(0, ("DOS open without an SMB "
2086 return NT_STATUS_INTERNAL_ERROR;
2089 /* Use the client requested access mask here,
2090 * not the one we open with. */
2091 status = fcb_or_dos_open(req,
2102 if (NT_STATUS_IS_OK(status)) {
2105 *pinfo = FILE_WAS_OPENED;
2107 return NT_STATUS_OK;
2112 * This next line is a subtlety we need for
2113 * MS-Access. If a file open will fail due to share
2114 * permissions and also for security (access) reasons,
2115 * we need to return the access failed error, not the
2116 * share error. We can't open the file due to kernel
2117 * oplock deadlock (it's possible we failed above on
2118 * the open_mode_check()) so use a userspace check.
2121 if (flags & O_RDWR) {
2122 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2123 } else if (flags & O_WRONLY) {
2124 can_access_mask = FILE_WRITE_DATA;
2126 can_access_mask = FILE_READ_DATA;
2129 if (((can_access_mask & FILE_WRITE_DATA) &&
2130 !CAN_WRITE(conn)) ||
2131 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2132 smb_fname, can_access_mask))) {
2137 * If we're returning a share violation, ensure we
2138 * cope with the braindead 1 second delay.
2141 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2142 lp_defer_sharing_violations()) {
2143 struct timeval timeout;
2144 struct deferred_open_record state;
2147 /* this is a hack to speed up torture tests
2149 timeout_usecs = lp_parm_int(SNUM(conn),
2150 "smbd","sharedelay",
2151 SHARING_VIOLATION_USEC_WAIT);
2153 /* This is a relative time, added to the absolute
2154 request_time value to get the absolute timeout time.
2155 Note that if this is the second or greater time we enter
2156 this codepath for this particular request mid then
2157 request_time is left as the absolute time of the *first*
2158 time this request mid was processed. This is what allows
2159 the request to eventually time out. */
2161 timeout = timeval_set(0, timeout_usecs);
2163 /* Nothing actually uses state.delayed_for_oplocks
2164 but it's handy to differentiate in debug messages
2165 between a 30 second delay due to oplock break, and
2166 a 1 second delay for share mode conflicts. */
2168 state.delayed_for_oplocks = False;
2172 && !request_timed_out(request_time,
2174 defer_open(lck, request_time, timeout,
2182 * We have detected a sharing violation here
2183 * so return the correct error code
2185 status = NT_STATUS_SHARING_VIOLATION;
2187 status = NT_STATUS_ACCESS_DENIED;
2193 * We exit this block with the share entry *locked*.....
2197 SMB_ASSERT(!file_existed || (lck != NULL));
2200 * Ensure we pay attention to default ACLs on directories if required.
2203 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2204 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2205 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2208 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2209 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2210 (unsigned int)flags, (unsigned int)flags2,
2211 (unsigned int)unx_mode, (unsigned int)access_mask,
2212 (unsigned int)open_access_mask));
2215 * open_file strips any O_TRUNC flags itself.
2218 fsp_open = open_file(fsp, conn, req, parent_dir,
2219 flags|flags2, unx_mode, access_mask,
2222 if (!NT_STATUS_IS_OK(fsp_open)) {
2227 if (!file_existed) {
2228 struct share_mode_entry *batch_entry = NULL;
2229 struct share_mode_entry *exclusive_entry = NULL;
2230 bool got_level2_oplock = false;
2231 bool got_a_none_oplock = false;
2232 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2235 * Deal with the race condition where two smbd's detect the
2236 * file doesn't exist and do the create at the same time. One
2237 * of them will win and set a share mode, the other (ie. this
2238 * one) should check if the requested share mode for this
2239 * create is allowed.
2243 * Now the file exists and fsp is successfully opened,
2244 * fsp->dev and fsp->inode are valid and should replace the
2245 * dev=0,inode=0 from a non existent file. Spotted by
2246 * Nadav Danieli <nadavd@exanet.com>. JRA.
2251 lck = get_share_mode_lock(talloc_tos(), id,
2253 smb_fname, &old_write_time);
2256 DEBUG(0, ("open_file_ntcreate: Could not get share "
2257 "mode lock for %s\n",
2258 smb_fname_str_dbg(smb_fname)));
2260 return NT_STATUS_SHARING_VIOLATION;
2263 /* Get the types we need to examine. */
2264 find_oplock_types(fsp,
2270 &got_a_none_oplock);
2272 /* First pass - send break only on batch oplocks. */
2273 if ((req != NULL) &&
2274 delay_for_batch_oplocks(fsp,
2278 schedule_defer_open(lck, request_time, req);
2281 return NT_STATUS_SHARING_VIOLATION;
2284 status = open_mode_check(conn, lck, fsp->name_hash,
2285 access_mask, share_access,
2286 create_options, &file_existed);
2288 if (NT_STATUS_IS_OK(status)) {
2289 /* We might be going to allow this open. Check oplock
2291 /* Second pass - send break for both batch or
2292 * exclusive oplocks. */
2293 if ((req != NULL) &&
2294 delay_for_exclusive_oplocks(
2299 schedule_defer_open(lck, request_time, req);
2302 return NT_STATUS_SHARING_VIOLATION;
2306 if (!NT_STATUS_IS_OK(status)) {
2307 struct deferred_open_record state;
2309 state.delayed_for_oplocks = False;
2312 /* Do it all over again immediately. In the second
2313 * round we will find that the file existed and handle
2314 * the DELETE_PENDING and FCB cases correctly. No need
2315 * to duplicate the code here. Essentially this is a
2316 * "goto top of this function", but don't tell
2320 defer_open(lck, request_time, timeval_zero(),
2328 grant_fsp_oplock_type(fsp,
2334 * We exit this block with the share entry *locked*.....
2339 SMB_ASSERT(lck != NULL);
2341 /* Delete streams if create_disposition requires it */
2342 if (file_existed && clear_ads &&
2343 !is_ntfs_stream_smb_fname(smb_fname)) {
2344 status = delete_all_streams(conn, smb_fname->base_name);
2345 if (!NT_STATUS_IS_OK(status)) {
2352 /* note that we ignore failure for the following. It is
2353 basically a hack for NFS, and NFS will never set one of
2354 these only read them. Nobody but Samba can ever set a deny
2355 mode and we have already checked our more authoritative
2356 locking database for permission to set this deny mode. If
2357 the kernel refuses the operations then the kernel is wrong.
2358 note that GPFS supports it as well - jmcd */
2360 if (fsp->fh->fd != -1) {
2362 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2363 if(ret_flock == -1 ){
2368 return NT_STATUS_SHARING_VIOLATION;
2373 * At this point onwards, we can guarentee that the share entry
2374 * is locked, whether we created the file or not, and that the
2375 * deny mode is compatible with all current opens.
2379 * If requested, truncate the file.
2382 if (file_existed && (flags2&O_TRUNC)) {
2384 * We are modifying the file after open - update the stat
2387 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2388 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2389 status = map_nt_error_from_unix(errno);
2397 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2398 * but we don't have to store this - just ignore it on access check.
2400 if (conn->sconn->using_smb2) {
2402 * SMB2 doesn't return it (according to Microsoft tests).
2403 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2404 * File created with access = 0x7 (Read, Write, Delete)
2405 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2407 fsp->access_mask = access_mask;
2409 /* But SMB1 does. */
2410 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2414 /* stat opens on existing files don't get oplocks. */
2415 if (is_stat_open(open_access_mask)) {
2416 fsp->oplock_type = NO_OPLOCK;
2419 if (flags2 & O_TRUNC) {
2420 info = FILE_WAS_OVERWRITTEN;
2422 info = FILE_WAS_OPENED;
2425 info = FILE_WAS_CREATED;
2433 * Setup the oplock info in both the shared memory and
2437 status = set_file_oplock(fsp, fsp->oplock_type);
2438 if (!NT_STATUS_IS_OK(status)) {
2440 * Could not get the kernel oplock or there are byte-range
2441 * locks on the file.
2443 fsp->oplock_type = NO_OPLOCK;
2446 set_share_mode(lck, fsp, get_current_uid(conn),
2450 /* Handle strange delete on close create semantics. */
2451 if (create_options & FILE_DELETE_ON_CLOSE) {
2453 status = can_set_delete_on_close(fsp, new_dos_attributes);
2455 if (!NT_STATUS_IS_OK(status)) {
2456 /* Remember to delete the mode we just added. */
2457 del_share_mode(lck, fsp);
2462 /* Note that here we set the *inital* delete on close flag,
2463 not the regular one. The magic gets handled in close. */
2464 fsp->initial_delete_on_close = True;
2467 if (info == FILE_WAS_OVERWRITTEN
2468 || info == FILE_WAS_CREATED
2469 || info == FILE_WAS_SUPERSEDED) {
2470 new_file_created = True;
2473 if (new_file_created) {
2474 /* Files should be initially set as archive */
2475 if (lp_map_archive(SNUM(conn)) ||
2476 lp_store_dos_attributes(SNUM(conn))) {
2478 if (file_set_dosmode(conn, smb_fname,
2479 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2480 parent_dir, true) == 0) {
2481 unx_mode = smb_fname->st.st_ex_mode;
2487 /* Determine sparse flag. */
2489 /* POSIX opens are sparse by default. */
2490 fsp->is_sparse = true;
2492 fsp->is_sparse = (file_existed &&
2493 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2497 * Take care of inherited ACLs on created files - if default ACL not
2501 if (!posix_open && !file_existed && !def_acl) {
2503 int saved_errno = errno; /* We might get ENOSYS in the next
2506 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2508 errno = saved_errno; /* Ignore ENOSYS */
2511 } else if (new_unx_mode) {
2515 /* Attributes need changing. File already existed. */
2518 int saved_errno = errno; /* We might get ENOSYS in the
2520 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2522 if (ret == -1 && errno == ENOSYS) {
2523 errno = saved_errno; /* Ignore ENOSYS */
2525 DEBUG(5, ("open_file_ntcreate: reset "
2526 "attributes of file %s to 0%o\n",
2527 smb_fname_str_dbg(smb_fname),
2528 (unsigned int)new_unx_mode));
2529 ret = 0; /* Don't do the fchmod below. */
2534 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2535 DEBUG(5, ("open_file_ntcreate: failed to reset "
2536 "attributes of file %s to 0%o\n",
2537 smb_fname_str_dbg(smb_fname),
2538 (unsigned int)new_unx_mode));
2541 /* If this is a successful open, we must remove any deferred open
2544 del_deferred_open_entry(lck, req->mid,
2545 messaging_server_id(req->sconn->msg_ctx));
2549 return NT_STATUS_OK;
2553 /****************************************************************************
2554 Open a file for for write to ensure that we can fchmod it.
2555 ****************************************************************************/
2557 NTSTATUS open_file_fchmod(connection_struct *conn,
2558 struct smb_filename *smb_fname,
2559 files_struct **result)
2561 if (!VALID_STAT(smb_fname->st)) {
2562 return NT_STATUS_INVALID_PARAMETER;
2565 return SMB_VFS_CREATE_FILE(
2568 0, /* root_dir_fid */
2569 smb_fname, /* fname */
2570 FILE_WRITE_DATA, /* access_mask */
2571 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2573 FILE_OPEN, /* create_disposition*/
2574 0, /* create_options */
2575 0, /* file_attributes */
2576 INTERNAL_OPEN_ONLY, /* oplock_request */
2577 0, /* allocation_size */
2578 0, /* private_flags */
2581 result, /* result */
2585 static NTSTATUS mkdir_internal(connection_struct *conn,
2586 struct smb_filename *smb_dname,
2587 uint32 file_attributes)
2590 char *parent_dir = NULL;
2592 bool posix_open = false;
2593 bool need_re_stat = false;
2594 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2596 if(access_mask & ~(conn->share_access)) {
2597 DEBUG(5,("mkdir_internal: failing share access "
2598 "%s\n", lp_servicename(SNUM(conn))));
2599 return NT_STATUS_ACCESS_DENIED;
2602 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2604 return NT_STATUS_NO_MEMORY;
2607 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2609 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2611 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2614 status = check_parent_access(conn,
2617 if(!NT_STATUS_IS_OK(status)) {
2618 DEBUG(5,("mkdir_internal: check_parent_access "
2619 "on directory %s for path %s returned %s\n",
2621 smb_dname->base_name,
2622 nt_errstr(status) ));
2626 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2627 return map_nt_error_from_unix(errno);
2630 /* Ensure we're checking for a symlink here.... */
2631 /* We don't want to get caught by a symlink racer. */
2633 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2634 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2635 smb_fname_str_dbg(smb_dname), strerror(errno)));
2636 return map_nt_error_from_unix(errno);
2639 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2640 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2641 smb_fname_str_dbg(smb_dname)));
2642 return NT_STATUS_NOT_A_DIRECTORY;
2645 if (lp_store_dos_attributes(SNUM(conn))) {
2647 file_set_dosmode(conn, smb_dname,
2648 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2653 if (lp_inherit_perms(SNUM(conn))) {
2654 inherit_access_posix_acl(conn, parent_dir,
2655 smb_dname->base_name, mode);
2656 need_re_stat = true;
2661 * Check if high bits should have been set,
2662 * then (if bits are missing): add them.
2663 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2666 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2667 (mode & ~smb_dname->st.st_ex_mode)) {
2668 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2669 (smb_dname->st.st_ex_mode |
2670 (mode & ~smb_dname->st.st_ex_mode)));
2671 need_re_stat = true;
2675 /* Change the owner if required. */
2676 if (lp_inherit_owner(SNUM(conn))) {
2677 change_dir_owner_to_parent(conn, parent_dir,
2678 smb_dname->base_name,
2680 need_re_stat = true;
2684 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2685 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2686 smb_fname_str_dbg(smb_dname), strerror(errno)));
2687 return map_nt_error_from_unix(errno);
2691 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2692 smb_dname->base_name);
2694 return NT_STATUS_OK;
2697 /****************************************************************************
2698 Ensure we didn't get symlink raced on opening a directory.
2699 ****************************************************************************/
2701 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2702 const SMB_STRUCT_STAT *sbuf2)
2704 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2705 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2706 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2707 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2713 /****************************************************************************
2714 Open a directory from an NT SMB call.
2715 ****************************************************************************/
2717 static NTSTATUS open_directory(connection_struct *conn,
2718 struct smb_request *req,
2719 struct smb_filename *smb_dname,
2721 uint32 share_access,
2722 uint32 create_disposition,
2723 uint32 create_options,
2724 uint32 file_attributes,
2726 files_struct **result)
2728 files_struct *fsp = NULL;
2729 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2730 struct share_mode_lock *lck = NULL;
2732 struct timespec mtimespec;
2735 if (is_ntfs_stream_smb_fname(smb_dname)) {
2736 DEBUG(2, ("open_directory: %s is a stream name!\n",
2737 smb_fname_str_dbg(smb_dname)));
2738 return NT_STATUS_NOT_A_DIRECTORY;
2741 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2742 /* Ensure we have a directory attribute. */
2743 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2746 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2747 "share_access = 0x%x create_options = 0x%x, "
2748 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2749 smb_fname_str_dbg(smb_dname),
2750 (unsigned int)access_mask,
2751 (unsigned int)share_access,
2752 (unsigned int)create_options,
2753 (unsigned int)create_disposition,
2754 (unsigned int)file_attributes));
2756 status = smbd_calculate_access_mask(conn, smb_dname,
2757 access_mask, &access_mask);
2758 if (!NT_STATUS_IS_OK(status)) {
2759 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2760 "on file %s returned %s\n",
2761 smb_fname_str_dbg(smb_dname),
2762 nt_errstr(status)));
2766 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2767 !security_token_has_privilege(get_current_nttok(conn),
2768 SEC_PRIV_SECURITY)) {
2769 DEBUG(10, ("open_directory: open on %s "
2770 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2771 smb_fname_str_dbg(smb_dname)));
2772 return NT_STATUS_PRIVILEGE_NOT_HELD;
2775 switch( create_disposition ) {
2779 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2782 info = FILE_WAS_OPENED;
2787 /* If directory exists error. If directory doesn't
2791 status = NT_STATUS_OBJECT_NAME_COLLISION;
2792 DEBUG(2, ("open_directory: unable to create "
2793 "%s. Error was %s\n",
2794 smb_fname_str_dbg(smb_dname),
2795 nt_errstr(status)));
2799 status = mkdir_internal(conn, smb_dname,
2802 if (!NT_STATUS_IS_OK(status)) {
2803 DEBUG(2, ("open_directory: unable to create "
2804 "%s. Error was %s\n",
2805 smb_fname_str_dbg(smb_dname),
2806 nt_errstr(status)));
2810 info = FILE_WAS_CREATED;
2815 * If directory exists open. If directory doesn't
2820 status = NT_STATUS_OK;
2821 info = FILE_WAS_OPENED;
2823 status = mkdir_internal(conn, smb_dname,
2826 if (NT_STATUS_IS_OK(status)) {
2827 info = FILE_WAS_CREATED;
2829 /* Cope with create race. */
2830 if (!NT_STATUS_EQUAL(status,
2831 NT_STATUS_OBJECT_NAME_COLLISION)) {
2832 DEBUG(2, ("open_directory: unable to create "
2833 "%s. Error was %s\n",
2834 smb_fname_str_dbg(smb_dname),
2835 nt_errstr(status)));
2838 info = FILE_WAS_OPENED;
2844 case FILE_SUPERSEDE:
2845 case FILE_OVERWRITE:
2846 case FILE_OVERWRITE_IF:
2848 DEBUG(5,("open_directory: invalid create_disposition "
2849 "0x%x for directory %s\n",
2850 (unsigned int)create_disposition,
2851 smb_fname_str_dbg(smb_dname)));
2852 return NT_STATUS_INVALID_PARAMETER;
2855 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2856 DEBUG(5,("open_directory: %s is not a directory !\n",
2857 smb_fname_str_dbg(smb_dname)));
2858 return NT_STATUS_NOT_A_DIRECTORY;
2861 if (info == FILE_WAS_OPENED) {
2862 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2863 if (!NT_STATUS_IS_OK(status)) {
2864 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2865 "file %s failed with %s\n",
2866 smb_fname_str_dbg(smb_dname),
2867 nt_errstr(status)));
2872 status = file_new(req, conn, &fsp);
2873 if(!NT_STATUS_IS_OK(status)) {
2878 * Setup the files_struct for it.
2881 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2882 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2883 fsp->file_pid = req ? req->smbpid : 0;
2884 fsp->can_lock = False;
2885 fsp->can_read = False;
2886 fsp->can_write = False;
2888 fsp->share_access = share_access;
2889 fsp->fh->private_options = 0;
2891 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2893 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2894 fsp->print_file = NULL;
2895 fsp->modified = False;
2896 fsp->oplock_type = NO_OPLOCK;
2897 fsp->sent_oplock_break = NO_BREAK_SENT;
2898 fsp->is_directory = True;
2899 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2900 status = fsp_set_smb_fname(fsp, smb_dname);
2901 if (!NT_STATUS_IS_OK(status)) {
2902 file_free(req, fsp);
2906 mtimespec = smb_dname->st.st_ex_mtime;
2909 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2911 /* POSIX allows us to open a directory with O_RDONLY. */
2912 status = fd_open(conn, fsp, O_RDONLY, 0);
2914 if (!NT_STATUS_IS_OK(status)) {
2915 DEBUG(5, ("open_directory: Could not open fd for "
2917 smb_fname_str_dbg(smb_dname),
2918 nt_errstr(status)));
2919 file_free(req, fsp);
2923 status = vfs_stat_fsp(fsp);
2924 if (!NT_STATUS_IS_OK(status)) {
2926 file_free(req, fsp);
2930 /* Ensure there was no race condition. */
2931 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2932 DEBUG(5,("open_directory: stat struct differs for "
2934 smb_fname_str_dbg(smb_dname)));
2936 file_free(req, fsp);
2937 return NT_STATUS_ACCESS_DENIED;
2940 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2941 conn->connectpath, smb_dname,
2945 DEBUG(0, ("open_directory: Could not get share mode lock for "
2946 "%s\n", smb_fname_str_dbg(smb_dname)));
2948 file_free(req, fsp);
2949 return NT_STATUS_SHARING_VIOLATION;
2952 status = open_mode_check(conn, lck, fsp->name_hash,
2953 access_mask, share_access,
2954 create_options, &dir_existed);
2956 if (!NT_STATUS_IS_OK(status)) {
2959 file_free(req, fsp);
2963 set_share_mode(lck, fsp, get_current_uid(conn),
2964 req ? req->mid : 0, NO_OPLOCK);
2966 /* For directories the delete on close bit at open time seems
2967 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2968 if (create_options & FILE_DELETE_ON_CLOSE) {
2969 status = can_set_delete_on_close(fsp, 0);
2970 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2973 file_free(req, fsp);
2977 if (NT_STATUS_IS_OK(status)) {
2978 /* Note that here we set the *inital* delete on close flag,
2979 not the regular one. The magic gets handled in close. */
2980 fsp->initial_delete_on_close = True;
2991 return NT_STATUS_OK;
2994 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2995 struct smb_filename *smb_dname)
3000 status = SMB_VFS_CREATE_FILE(
3003 0, /* root_dir_fid */
3004 smb_dname, /* fname */
3005 FILE_READ_ATTRIBUTES, /* access_mask */
3006 FILE_SHARE_NONE, /* share_access */
3007 FILE_CREATE, /* create_disposition*/
3008 FILE_DIRECTORY_FILE, /* create_options */
3009 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3010 0, /* oplock_request */
3011 0, /* allocation_size */
3012 0, /* private_flags */
3018 if (NT_STATUS_IS_OK(status)) {
3019 close_file(req, fsp, NORMAL_CLOSE);
3025 /****************************************************************************
3026 Receive notification that one of our open files has been renamed by another
3028 ****************************************************************************/
3030 void msg_file_was_renamed(struct messaging_context *msg,
3033 struct server_id server_id,
3037 char *frm = (char *)data->data;
3039 const char *sharepath;
3040 const char *base_name;
3041 const char *stream_name;
3042 struct smb_filename *smb_fname = NULL;
3043 size_t sp_len, bn_len;
3045 struct smbd_server_connection *sconn =
3046 talloc_get_type_abort(private_data,
3047 struct smbd_server_connection);
3049 if (data->data == NULL
3050 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3051 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3052 (int)data->length));
3056 /* Unpack the message. */
3057 pull_file_id_24(frm, &id);
3058 sharepath = &frm[24];
3059 sp_len = strlen(sharepath);
3060 base_name = sharepath + sp_len + 1;
3061 bn_len = strlen(base_name);
3062 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3064 /* stream_name must always be NULL if there is no stream. */
3065 if (stream_name[0] == '\0') {
3069 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3070 stream_name, NULL, &smb_fname);
3071 if (!NT_STATUS_IS_OK(status)) {
3075 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3077 sharepath, smb_fname_str_dbg(smb_fname),
3078 file_id_string_tos(&id)));
3080 for(fsp = file_find_di_first(sconn, id); fsp;
3081 fsp = file_find_di_next(fsp)) {
3082 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3084 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3085 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3086 smb_fname_str_dbg(smb_fname)));
3087 status = fsp_set_smb_fname(fsp, smb_fname);
3088 if (!NT_STATUS_IS_OK(status)) {
3093 /* Now we have the complete path we can work out if this is
3094 actually within this share and adjust newname accordingly. */
3095 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3096 "not sharepath %s) "
3097 "%s from %s -> %s\n",
3098 fsp->conn->connectpath,
3102 smb_fname_str_dbg(smb_fname)));
3106 TALLOC_FREE(smb_fname);
3111 * If a main file is opened for delete, all streams need to be checked for
3112 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3113 * If that works, delete them all by setting the delete on close and close.
3116 NTSTATUS open_streams_for_delete(connection_struct *conn,
3119 struct stream_struct *stream_info = NULL;
3120 files_struct **streams = NULL;
3122 unsigned int num_streams = 0;
3123 TALLOC_CTX *frame = talloc_stackframe();
3126 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3127 &num_streams, &stream_info);
3129 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3130 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3131 DEBUG(10, ("no streams around\n"));
3133 return NT_STATUS_OK;
3136 if (!NT_STATUS_IS_OK(status)) {
3137 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3138 nt_errstr(status)));
3142 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3145 if (num_streams == 0) {
3147 return NT_STATUS_OK;
3150 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3151 if (streams == NULL) {
3152 DEBUG(0, ("talloc failed\n"));
3153 status = NT_STATUS_NO_MEMORY;
3157 for (i=0; i<num_streams; i++) {
3158 struct smb_filename *smb_fname = NULL;
3160 if (strequal(stream_info[i].name, "::$DATA")) {
3165 status = create_synthetic_smb_fname(talloc_tos(), fname,
3166 stream_info[i].name,
3168 if (!NT_STATUS_IS_OK(status)) {
3172 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3173 DEBUG(10, ("Unable to stat stream: %s\n",
3174 smb_fname_str_dbg(smb_fname)));
3177 status = SMB_VFS_CREATE_FILE(
3180 0, /* root_dir_fid */
3181 smb_fname, /* fname */
3182 DELETE_ACCESS, /* access_mask */
3183 (FILE_SHARE_READ | /* share_access */
3184 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3185 FILE_OPEN, /* create_disposition*/
3186 0, /* create_options */
3187 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3188 0, /* oplock_request */
3189 0, /* allocation_size */
3190 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3193 &streams[i], /* result */
3196 if (!NT_STATUS_IS_OK(status)) {
3197 DEBUG(10, ("Could not open stream %s: %s\n",
3198 smb_fname_str_dbg(smb_fname),
3199 nt_errstr(status)));
3201 TALLOC_FREE(smb_fname);
3204 TALLOC_FREE(smb_fname);
3208 * don't touch the variable "status" beyond this point :-)
3211 for (i -= 1 ; i >= 0; i--) {
3212 if (streams[i] == NULL) {
3216 DEBUG(10, ("Closing stream # %d, %s\n", i,
3217 fsp_str_dbg(streams[i])));
3218 close_file(NULL, streams[i], NORMAL_CLOSE);
3226 /*********************************************************************
3227 Create a default ACL by inheriting from the parent. If no inheritance
3228 from the parent available, don't set anything. This will leave the actual
3229 permissions the new file or directory already got from the filesystem
3230 as the NT ACL when read.
3231 *********************************************************************/
3233 static NTSTATUS inherit_new_acl(files_struct *fsp)
3235 TALLOC_CTX *ctx = talloc_tos();
3236 char *parent_name = NULL;
3237 struct security_descriptor *parent_desc = NULL;
3238 NTSTATUS status = NT_STATUS_OK;
3239 struct security_descriptor *psd = NULL;
3240 struct dom_sid *owner_sid = NULL;
3241 struct dom_sid *group_sid = NULL;
3242 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3243 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3244 bool inheritable_components = false;
3247 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3248 return NT_STATUS_NO_MEMORY;
3251 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3253 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3255 if (!NT_STATUS_IS_OK(status)) {
3259 inheritable_components = sd_has_inheritable_components(parent_desc,
3262 if (!inheritable_components && !inherit_owner) {
3263 /* Nothing to inherit and not setting owner. */
3264 return NT_STATUS_OK;
3267 /* Create an inherited descriptor from the parent. */
3269 if (DEBUGLEVEL >= 10) {
3270 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3271 fsp_str_dbg(fsp) ));
3272 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3275 /* Inherit from parent descriptor if "inherit owner" set. */
3276 if (inherit_owner) {
3277 owner_sid = parent_desc->owner_sid;
3278 group_sid = parent_desc->group_sid;
3281 if (owner_sid == NULL) {
3282 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3284 if (group_sid == NULL) {
3285 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3288 status = se_create_child_secdesc(ctx,
3295 if (!NT_STATUS_IS_OK(status)) {
3299 /* If inheritable_components == false,
3300 se_create_child_secdesc()
3301 creates a security desriptor with a NULL dacl
3302 entry, but with SEC_DESC_DACL_PRESENT. We need
3303 to remove that flag. */
3305 if (!inheritable_components) {
3306 security_info_sent &= ~SECINFO_DACL;
3307 psd->type &= ~SEC_DESC_DACL_PRESENT;
3310 if (DEBUGLEVEL >= 10) {
3311 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3312 fsp_str_dbg(fsp) ));
3313 NDR_PRINT_DEBUG(security_descriptor, psd);
3316 if (inherit_owner) {
3317 /* We need to be root to force this. */
3320 status = SMB_VFS_FSET_NT_ACL(fsp,
3323 if (inherit_owner) {
3330 * Wrapper around open_file_ntcreate and open_directory
3333 static NTSTATUS create_file_unixpath(connection_struct *conn,
3334 struct smb_request *req,
3335 struct smb_filename *smb_fname,
3336 uint32_t access_mask,
3337 uint32_t share_access,
3338 uint32_t create_disposition,
3339 uint32_t create_options,
3340 uint32_t file_attributes,
3341 uint32_t oplock_request,
3342 uint64_t allocation_size,
3343 uint32_t private_flags,
3344 struct security_descriptor *sd,
3345 struct ea_list *ea_list,
3347 files_struct **result,
3350 int info = FILE_WAS_OPENED;
3351 files_struct *base_fsp = NULL;
3352 files_struct *fsp = NULL;
3355 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3356 "file_attributes = 0x%x, share_access = 0x%x, "
3357 "create_disposition = 0x%x create_options = 0x%x "
3358 "oplock_request = 0x%x private_flags = 0x%x "
3359 "ea_list = 0x%p, sd = 0x%p, "
3361 (unsigned int)access_mask,
3362 (unsigned int)file_attributes,
3363 (unsigned int)share_access,
3364 (unsigned int)create_disposition,
3365 (unsigned int)create_options,
3366 (unsigned int)oplock_request,
3367 (unsigned int)private_flags,
3368 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3370 if (create_options & FILE_OPEN_BY_FILE_ID) {
3371 status = NT_STATUS_NOT_SUPPORTED;
3375 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3376 status = NT_STATUS_INVALID_PARAMETER;
3381 oplock_request |= INTERNAL_OPEN_ONLY;
3384 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3385 && (access_mask & DELETE_ACCESS)
3386 && !is_ntfs_stream_smb_fname(smb_fname)) {
3388 * We can't open a file with DELETE access if any of the
3389 * streams is open without FILE_SHARE_DELETE
3391 status = open_streams_for_delete(conn, smb_fname->base_name);
3393 if (!NT_STATUS_IS_OK(status)) {
3398 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3399 !security_token_has_privilege(get_current_nttok(conn),
3400 SEC_PRIV_SECURITY)) {
3401 DEBUG(10, ("create_file_unixpath: open on %s "
3402 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3403 smb_fname_str_dbg(smb_fname)));
3404 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3408 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3409 && is_ntfs_stream_smb_fname(smb_fname)
3410 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3411 uint32 base_create_disposition;
3412 struct smb_filename *smb_fname_base = NULL;
3414 if (create_options & FILE_DIRECTORY_FILE) {
3415 status = NT_STATUS_NOT_A_DIRECTORY;
3419 switch (create_disposition) {
3421 base_create_disposition = FILE_OPEN;
3424 base_create_disposition = FILE_OPEN_IF;
3428 /* Create an smb_filename with stream_name == NULL. */
3429 status = create_synthetic_smb_fname(talloc_tos(),
3430 smb_fname->base_name,
3433 if (!NT_STATUS_IS_OK(status)) {
3437 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3438 DEBUG(10, ("Unable to stat stream: %s\n",
3439 smb_fname_str_dbg(smb_fname_base)));
3442 /* Open the base file. */
3443 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3446 | FILE_SHARE_DELETE,
3447 base_create_disposition,
3448 0, 0, 0, 0, 0, NULL, NULL,
3450 TALLOC_FREE(smb_fname_base);
3452 if (!NT_STATUS_IS_OK(status)) {
3453 DEBUG(10, ("create_file_unixpath for base %s failed: "
3454 "%s\n", smb_fname->base_name,
3455 nt_errstr(status)));
3458 /* we don't need to low level fd */
3463 * If it's a request for a directory open, deal with it separately.
3466 if (create_options & FILE_DIRECTORY_FILE) {
3468 if (create_options & FILE_NON_DIRECTORY_FILE) {
3469 status = NT_STATUS_INVALID_PARAMETER;
3473 /* Can't open a temp directory. IFS kit test. */
3474 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3475 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3476 status = NT_STATUS_INVALID_PARAMETER;
3481 * We will get a create directory here if the Win32
3482 * app specified a security descriptor in the
3483 * CreateDirectory() call.
3487 status = open_directory(
3488 conn, req, smb_fname, access_mask, share_access,
3489 create_disposition, create_options, file_attributes,
3494 * Ordinary file case.
3497 status = file_new(req, conn, &fsp);
3498 if(!NT_STATUS_IS_OK(status)) {
3502 status = fsp_set_smb_fname(fsp, smb_fname);
3503 if (!NT_STATUS_IS_OK(status)) {
3508 * We're opening the stream element of a base_fsp
3509 * we already opened. Set up the base_fsp pointer.
3512 fsp->base_fsp = base_fsp;
3515 status = open_file_ntcreate(conn,
3527 if(!NT_STATUS_IS_OK(status)) {
3528 file_free(req, fsp);
3532 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3534 /* A stream open never opens a directory */
3537 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3542 * Fail the open if it was explicitly a non-directory
3546 if (create_options & FILE_NON_DIRECTORY_FILE) {
3547 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3552 status = open_directory(
3553 conn, req, smb_fname, access_mask,
3554 share_access, create_disposition,
3555 create_options, file_attributes,
3560 if (!NT_STATUS_IS_OK(status)) {
3564 fsp->base_fsp = base_fsp;
3566 if ((ea_list != NULL) &&
3567 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3568 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3569 if (!NT_STATUS_IS_OK(status)) {
3574 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3575 status = NT_STATUS_ACCESS_DENIED;
3579 /* Save the requested allocation size. */
3580 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3582 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3583 fsp->initial_allocation_size = smb_roundup(
3584 fsp->conn, allocation_size);
3585 if (fsp->is_directory) {
3586 /* Can't set allocation size on a directory. */
3587 status = NT_STATUS_ACCESS_DENIED;
3590 if (vfs_allocate_file_space(
3591 fsp, fsp->initial_allocation_size) == -1) {
3592 status = NT_STATUS_DISK_FULL;
3596 fsp->initial_allocation_size = smb_roundup(
3597 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3601 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3602 fsp->base_fsp == NULL) {
3605 * According to the MS documentation, the only time the security
3606 * descriptor is applied to the opened file is iff we *created* the
3607 * file; an existing file stays the same.
3609 * Also, it seems (from observation) that you can open the file with
3610 * any access mask but you can still write the sd. We need to override
3611 * the granted access before we call set_sd
3612 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3615 uint32_t sec_info_sent;
3616 uint32_t saved_access_mask = fsp->access_mask;
3618 sec_info_sent = get_sec_info(sd);
3620 fsp->access_mask = FILE_GENERIC_ALL;
3622 /* Convert all the generic bits. */
3623 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3624 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3626 if (sec_info_sent & (SECINFO_OWNER|
3630 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3633 fsp->access_mask = saved_access_mask;
3635 if (!NT_STATUS_IS_OK(status)) {
3638 } else if (lp_inherit_acls(SNUM(conn))) {
3639 /* Inherit from parent. Errors here are not fatal. */
3640 status = inherit_new_acl(fsp);
3641 if (!NT_STATUS_IS_OK(status)) {
3642 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3644 nt_errstr(status) ));
3649 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3652 if (pinfo != NULL) {
3656 smb_fname->st = fsp->fsp_name->st;
3658 return NT_STATUS_OK;
3661 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3664 if (base_fsp && fsp->base_fsp == base_fsp) {
3666 * The close_file below will close
3671 close_file(req, fsp, ERROR_CLOSE);
3674 if (base_fsp != NULL) {
3675 close_file(req, base_fsp, ERROR_CLOSE);
3682 * Calculate the full path name given a relative fid.
3684 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3685 struct smb_request *req,
3686 uint16_t root_dir_fid,
3687 const struct smb_filename *smb_fname,
3688 struct smb_filename **smb_fname_out)
3690 files_struct *dir_fsp;
3691 char *parent_fname = NULL;
3692 char *new_base_name = NULL;
3695 if (root_dir_fid == 0 || !smb_fname) {
3696 status = NT_STATUS_INTERNAL_ERROR;
3700 dir_fsp = file_fsp(req, root_dir_fid);
3702 if (dir_fsp == NULL) {
3703 status = NT_STATUS_INVALID_HANDLE;
3707 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3708 status = NT_STATUS_INVALID_HANDLE;
3712 if (!dir_fsp->is_directory) {
3715 * Check to see if this is a mac fork of some kind.
3718 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3719 is_ntfs_stream_smb_fname(smb_fname)) {
3720 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3725 we need to handle the case when we get a
3726 relative open relative to a file and the
3727 pathname is blank - this is a reopen!
3728 (hint from demyn plantenberg)
3731 status = NT_STATUS_INVALID_HANDLE;
3735 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3737 * We're at the toplevel dir, the final file name
3738 * must not contain ./, as this is filtered out
3739 * normally by srvstr_get_path and unix_convert
3740 * explicitly rejects paths containing ./.
3742 parent_fname = talloc_strdup(talloc_tos(), "");
3743 if (parent_fname == NULL) {
3744 status = NT_STATUS_NO_MEMORY;
3748 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3751 * Copy in the base directory name.
3754 parent_fname = talloc_array(talloc_tos(), char,
3756 if (parent_fname == NULL) {
3757 status = NT_STATUS_NO_MEMORY;
3760 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3764 * Ensure it ends in a '/'.
3765 * We used TALLOC_SIZE +2 to add space for the '/'.
3769 && (parent_fname[dir_name_len-1] != '\\')
3770 && (parent_fname[dir_name_len-1] != '/')) {
3771 parent_fname[dir_name_len] = '/';
3772 parent_fname[dir_name_len+1] = '\0';
3776 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3777 smb_fname->base_name);
3778 if (new_base_name == NULL) {
3779 status = NT_STATUS_NO_MEMORY;
3783 status = filename_convert(req,
3785 req->flags2 & FLAGS2_DFS_PATHNAMES,
3790 if (!NT_STATUS_IS_OK(status)) {
3795 TALLOC_FREE(parent_fname);
3796 TALLOC_FREE(new_base_name);
3800 NTSTATUS create_file_default(connection_struct *conn,
3801 struct smb_request *req,
3802 uint16_t root_dir_fid,
3803 struct smb_filename *smb_fname,
3804 uint32_t access_mask,
3805 uint32_t share_access,
3806 uint32_t create_disposition,
3807 uint32_t create_options,
3808 uint32_t file_attributes,
3809 uint32_t oplock_request,
3810 uint64_t allocation_size,
3811 uint32_t private_flags,
3812 struct security_descriptor *sd,
3813 struct ea_list *ea_list,
3814 files_struct **result,
3817 int info = FILE_WAS_OPENED;
3818 files_struct *fsp = NULL;
3820 bool stream_name = false;
3822 DEBUG(10,("create_file: access_mask = 0x%x "
3823 "file_attributes = 0x%x, share_access = 0x%x, "
3824 "create_disposition = 0x%x create_options = 0x%x "
3825 "oplock_request = 0x%x "
3826 "private_flags = 0x%x "
3827 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3829 (unsigned int)access_mask,
3830 (unsigned int)file_attributes,
3831 (unsigned int)share_access,
3832 (unsigned int)create_disposition,
3833 (unsigned int)create_options,
3834 (unsigned int)oplock_request,
3835 (unsigned int)private_flags,
3836 (unsigned int)root_dir_fid,
3837 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3840 * Calculate the filename from the root_dir_if if necessary.
3843 if (root_dir_fid != 0) {
3844 struct smb_filename *smb_fname_out = NULL;
3845 status = get_relative_fid_filename(conn, req, root_dir_fid,
3846 smb_fname, &smb_fname_out);
3847 if (!NT_STATUS_IS_OK(status)) {
3850 smb_fname = smb_fname_out;
3854 * Check to see if this is a mac fork of some kind.
3857 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3859 enum FAKE_FILE_TYPE fake_file_type;
3861 fake_file_type = is_fake_file(smb_fname);
3863 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3866 * Here we go! support for changing the disk quotas
3869 * We need to fake up to open this MAGIC QUOTA file
3870 * and return a valid FID.
3872 * w2k close this file directly after openening xp
3873 * also tries a QUERY_FILE_INFO on the file and then
3876 status = open_fake_file(req, conn, req->vuid,
3877 fake_file_type, smb_fname,
3879 if (!NT_STATUS_IS_OK(status)) {
3883 ZERO_STRUCT(smb_fname->st);
3887 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3888 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3893 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
3895 smb_fname->stream_name = NULL;
3896 /* We have to handle this error here. */
3897 if (create_options & FILE_DIRECTORY_FILE) {
3898 status = NT_STATUS_NOT_A_DIRECTORY;
3901 if (lp_posix_pathnames()) {
3902 ret = SMB_VFS_LSTAT(conn, smb_fname);
3904 ret = SMB_VFS_STAT(conn, smb_fname);
3907 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3908 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3913 status = create_file_unixpath(
3914 conn, req, smb_fname, access_mask, share_access,
3915 create_disposition, create_options, file_attributes,
3916 oplock_request, allocation_size, private_flags,
3920 if (!NT_STATUS_IS_OK(status)) {
3925 DEBUG(10, ("create_file: info=%d\n", info));
3928 if (pinfo != NULL) {
3931 return NT_STATUS_OK;
3934 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3937 close_file(req, fsp, ERROR_CLOSE);