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 status = map_nt_error_from_unix(errno);
295 if (errno == EMFILE) {
296 static time_t last_warned = 0L;
298 if (time((time_t *) NULL) > last_warned) {
299 DEBUG(0,("Too many open files, unable "
300 "to open more! smbd's max "
302 lp_max_open_files()));
303 last_warned = time((time_t *) NULL);
309 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
310 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
311 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
316 /****************************************************************************
317 Close the file associated with a fsp.
318 ****************************************************************************/
320 NTSTATUS fd_close(files_struct *fsp)
327 if (fsp->fh->fd == -1) {
328 return NT_STATUS_OK; /* What we used to call a stat open. */
330 if (fsp->fh->ref_count > 1) {
331 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
334 ret = SMB_VFS_CLOSE(fsp);
337 return map_nt_error_from_unix(errno);
342 /****************************************************************************
343 Change the ownership of a file to that of the parent directory.
344 Do this by fd if possible.
345 ****************************************************************************/
347 void change_file_owner_to_parent(connection_struct *conn,
348 const char *inherit_from_dir,
351 struct smb_filename *smb_fname_parent = NULL;
355 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
356 NULL, NULL, &smb_fname_parent);
357 if (!NT_STATUS_IS_OK(status)) {
361 ret = SMB_VFS_STAT(conn, smb_fname_parent);
363 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
364 "directory %s. Error was %s\n",
365 smb_fname_str_dbg(smb_fname_parent),
367 TALLOC_FREE(smb_fname_parent);
371 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
372 /* Already this uid - no need to change. */
373 DEBUG(10,("change_file_owner_to_parent: file %s "
374 "is already owned by uid %d\n",
376 (int)fsp->fsp_name->st.st_ex_uid ));
377 TALLOC_FREE(smb_fname_parent);
382 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
385 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
386 "file %s to parent directory uid %u. Error "
387 "was %s\n", fsp_str_dbg(fsp),
388 (unsigned int)smb_fname_parent->st.st_ex_uid,
391 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
392 "parent directory uid %u.\n", fsp_str_dbg(fsp),
393 (unsigned int)smb_fname_parent->st.st_ex_uid));
394 /* Ensure the uid entry is updated. */
395 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
398 TALLOC_FREE(smb_fname_parent);
401 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
402 const char *inherit_from_dir,
404 SMB_STRUCT_STAT *psbuf)
406 struct smb_filename *smb_fname_parent = NULL;
407 struct smb_filename *smb_fname_cwd = NULL;
408 char *saved_dir = NULL;
409 TALLOC_CTX *ctx = talloc_tos();
410 NTSTATUS status = NT_STATUS_OK;
413 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
415 if (!NT_STATUS_IS_OK(status)) {
419 ret = SMB_VFS_STAT(conn, smb_fname_parent);
421 status = map_nt_error_from_unix(errno);
422 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
423 "directory %s. Error was %s\n",
424 smb_fname_str_dbg(smb_fname_parent),
429 /* We've already done an lstat into psbuf, and we know it's a
430 directory. If we can cd into the directory and the dev/ino
431 are the same then we can safely chown without races as
432 we're locking the directory in place by being in it. This
433 should work on any UNIX (thanks tridge :-). JRA.
436 saved_dir = vfs_GetWd(ctx,conn);
438 status = map_nt_error_from_unix(errno);
439 DEBUG(0,("change_dir_owner_to_parent: failed to get "
440 "current working directory. Error was %s\n",
445 /* Chdir into the new path. */
446 if (vfs_ChDir(conn, fname) == -1) {
447 status = map_nt_error_from_unix(errno);
448 DEBUG(0,("change_dir_owner_to_parent: failed to change "
449 "current working directory to %s. Error "
450 "was %s\n", fname, strerror(errno) ));
454 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
456 if (!NT_STATUS_IS_OK(status)) {
460 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
462 status = map_nt_error_from_unix(errno);
463 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
464 "directory '.' (%s) Error was %s\n",
465 fname, strerror(errno)));
469 /* Ensure we're pointing at the same place. */
470 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
471 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
472 DEBUG(0,("change_dir_owner_to_parent: "
473 "device/inode on directory %s changed. "
474 "Refusing to chown !\n", fname ));
475 status = NT_STATUS_ACCESS_DENIED;
479 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
480 /* Already this uid - no need to change. */
481 DEBUG(10,("change_dir_owner_to_parent: directory %s "
482 "is already owned by uid %d\n",
484 (int)smb_fname_cwd->st.st_ex_uid ));
485 status = NT_STATUS_OK;
490 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
494 status = map_nt_error_from_unix(errno);
495 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
496 "directory %s to parent directory uid %u. "
497 "Error was %s\n", fname,
498 (unsigned int)smb_fname_parent->st.st_ex_uid,
501 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
502 "directory %s to parent directory uid %u.\n",
503 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
504 /* Ensure the uid entry is updated. */
505 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
509 vfs_ChDir(conn,saved_dir);
511 TALLOC_FREE(smb_fname_parent);
512 TALLOC_FREE(smb_fname_cwd);
516 /****************************************************************************
518 ****************************************************************************/
520 static NTSTATUS open_file(files_struct *fsp,
521 connection_struct *conn,
522 struct smb_request *req,
523 const char *parent_dir,
526 uint32 access_mask, /* client requested access mask. */
527 uint32 open_access_mask) /* what we're actually using in the open. */
529 struct smb_filename *smb_fname = fsp->fsp_name;
530 NTSTATUS status = NT_STATUS_OK;
531 int accmode = (flags & O_ACCMODE);
532 int local_flags = flags;
533 bool file_existed = VALID_STAT(fsp->fsp_name->st);
534 bool file_created = false;
539 /* Check permissions */
542 * This code was changed after seeing a client open request
543 * containing the open mode of (DENY_WRITE/read-only) with
544 * the 'create if not exist' bit set. The previous code
545 * would fail to open the file read only on a read-only share
546 * as it was checking the flags parameter directly against O_RDONLY,
547 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
551 if (!CAN_WRITE(conn)) {
552 /* It's a read-only share - fail if we wanted to write. */
553 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
554 DEBUG(3,("Permission denied opening %s\n",
555 smb_fname_str_dbg(smb_fname)));
556 return NT_STATUS_ACCESS_DENIED;
557 } else if(flags & O_CREAT) {
558 /* We don't want to write - but we must make sure that
559 O_CREAT doesn't create the file if we have write
560 access into the directory.
562 flags &= ~(O_CREAT|O_EXCL);
563 local_flags &= ~(O_CREAT|O_EXCL);
568 * This little piece of insanity is inspired by the
569 * fact that an NT client can open a file for O_RDONLY,
570 * but set the create disposition to FILE_EXISTS_TRUNCATE.
571 * If the client *can* write to the file, then it expects to
572 * truncate the file, even though it is opening for readonly.
573 * Quicken uses this stupid trick in backup file creation...
574 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
575 * for helping track this one down. It didn't bite us in 2.0.x
576 * as we always opened files read-write in that release. JRA.
579 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
580 DEBUG(10,("open_file: truncate requested on read-only open "
581 "for file %s\n", smb_fname_str_dbg(smb_fname)));
582 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
585 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
586 (!file_existed && (local_flags & O_CREAT)) ||
587 ((local_flags & O_TRUNC) == O_TRUNC) ) {
591 * We can't actually truncate here as the file may be locked.
592 * open_file_ntcreate will take care of the truncate later. JRA.
595 local_flags &= ~O_TRUNC;
597 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
599 * We would block on opening a FIFO with no one else on the
600 * other end. Do what we used to do and add O_NONBLOCK to the
604 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
605 local_flags |= O_NONBLOCK;
609 /* Don't create files with Microsoft wildcard characters. */
612 * wildcard characters are allowed in stream names
613 * only test the basefilename
615 wild = fsp->base_fsp->fsp_name->base_name;
617 wild = smb_fname->base_name;
619 if ((local_flags & O_CREAT) && !file_existed &&
621 return NT_STATUS_OBJECT_NAME_INVALID;
624 /* Can we access this file ? */
625 if (!fsp->base_fsp) {
626 /* Only do this check on non-stream open. */
628 status = smbd_check_access_rights(conn,
631 } else if (local_flags & O_CREAT){
632 status = check_parent_access(conn,
636 /* File didn't exist and no O_CREAT. */
637 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
639 if (!NT_STATUS_IS_OK(status)) {
640 DEBUG(10,("open_file: "
644 "smbd_check_access_rights" :
645 "check_parent_access",
646 smb_fname_str_dbg(smb_fname),
647 nt_errstr(status) ));
652 /* Actually do the open */
653 status = fd_open(conn, fsp, local_flags, unx_mode);
654 if (!NT_STATUS_IS_OK(status)) {
655 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
656 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
657 nt_errstr(status),local_flags,flags));
661 if ((local_flags & O_CREAT) && !file_existed) {
666 fsp->fh->fd = -1; /* What we used to call a stat open. */
668 /* File must exist for a stat open. */
669 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
672 status = smbd_check_access_rights(conn,
676 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
678 S_ISLNK(smb_fname->st.st_ex_mode)) {
679 /* This is a POSIX stat open for delete
680 * or rename on a symlink that points
682 DEBUG(10,("open_file: allowing POSIX "
683 "open on bad symlink %s\n",
684 smb_fname_str_dbg(smb_fname)));
685 status = NT_STATUS_OK;
688 if (!NT_STATUS_IS_OK(status)) {
689 DEBUG(10,("open_file: "
690 "smbd_check_access_rights on file "
692 smb_fname_str_dbg(smb_fname),
693 nt_errstr(status) ));
701 if (fsp->fh->fd == -1) {
702 ret = SMB_VFS_STAT(conn, smb_fname);
704 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
705 /* If we have an fd, this stat should succeed. */
707 DEBUG(0,("Error doing fstat on open file %s "
709 smb_fname_str_dbg(smb_fname),
714 /* For a non-io open, this stat failing means file not found. JRA */
716 status = map_nt_error_from_unix(errno);
722 bool need_re_stat = false;
723 /* Do all inheritance work after we've
724 done a successful stat call and filled
725 in the stat struct in fsp->fsp_name. */
727 /* Inherit the ACL if required */
728 if (lp_inherit_perms(SNUM(conn))) {
729 inherit_access_posix_acl(conn, parent_dir,
730 smb_fname->base_name,
735 /* Change the owner if required. */
736 if (lp_inherit_owner(SNUM(conn))) {
737 change_file_owner_to_parent(conn, parent_dir,
743 if (fsp->fh->fd == -1) {
744 ret = SMB_VFS_STAT(conn, smb_fname);
746 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
747 /* If we have an fd, this stat should succeed. */
749 DEBUG(0,("Error doing fstat on open file %s "
751 smb_fname_str_dbg(smb_fname),
757 notify_fname(conn, NOTIFY_ACTION_ADDED,
758 FILE_NOTIFY_CHANGE_FILE_NAME,
759 smb_fname->base_name);
764 * POSIX allows read-only opens of directories. We don't
765 * want to do this (we use a different code path for this)
766 * so catch a directory open and return an EISDIR. JRA.
769 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
772 return NT_STATUS_FILE_IS_A_DIRECTORY;
775 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
776 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
777 fsp->file_pid = req ? req->smbpid : 0;
778 fsp->can_lock = True;
779 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
780 if (!CAN_WRITE(conn)) {
781 fsp->can_write = False;
783 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
786 fsp->print_file = NULL;
787 fsp->modified = False;
788 fsp->sent_oplock_break = NO_BREAK_SENT;
789 fsp->is_directory = False;
790 if (conn->aio_write_behind_list &&
791 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
792 conn->case_sensitive)) {
793 fsp->aio_write_behind = True;
796 fsp->wcp = NULL; /* Write cache pointer. */
798 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
799 conn->session_info->unix_info->unix_name,
800 smb_fname_str_dbg(smb_fname),
801 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
802 conn->num_files_open));
808 /****************************************************************************
809 Check if we can open a file with a share mode.
810 Returns True if conflict, False if not.
811 ****************************************************************************/
813 static bool share_conflict(struct share_mode_entry *entry,
817 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
818 "entry->share_access = 0x%x, "
819 "entry->private_options = 0x%x\n",
820 (unsigned int)entry->access_mask,
821 (unsigned int)entry->share_access,
822 (unsigned int)entry->private_options));
824 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
825 (unsigned int)access_mask, (unsigned int)share_access));
827 if ((entry->access_mask & (FILE_WRITE_DATA|
831 DELETE_ACCESS)) == 0) {
832 DEBUG(10,("share_conflict: No conflict due to "
833 "entry->access_mask = 0x%x\n",
834 (unsigned int)entry->access_mask ));
838 if ((access_mask & (FILE_WRITE_DATA|
842 DELETE_ACCESS)) == 0) {
843 DEBUG(10,("share_conflict: No conflict due to "
844 "access_mask = 0x%x\n",
845 (unsigned int)access_mask ));
849 #if 1 /* JRA TEST - Superdebug. */
850 #define CHECK_MASK(num, am, right, sa, share) \
851 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
852 (unsigned int)(num), (unsigned int)(am), \
853 (unsigned int)(right), (unsigned int)(am)&(right) )); \
854 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
855 (unsigned int)(num), (unsigned int)(sa), \
856 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
857 if (((am) & (right)) && !((sa) & (share))) { \
858 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
859 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
860 (unsigned int)(share) )); \
864 #define CHECK_MASK(num, am, right, sa, share) \
865 if (((am) & (right)) && !((sa) & (share))) { \
866 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
867 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
868 (unsigned int)(share) )); \
873 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
874 share_access, FILE_SHARE_WRITE);
875 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
876 entry->share_access, FILE_SHARE_WRITE);
878 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
879 share_access, FILE_SHARE_READ);
880 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
881 entry->share_access, FILE_SHARE_READ);
883 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
884 share_access, FILE_SHARE_DELETE);
885 CHECK_MASK(6, access_mask, DELETE_ACCESS,
886 entry->share_access, FILE_SHARE_DELETE);
888 DEBUG(10,("share_conflict: No conflict.\n"));
892 #if defined(DEVELOPER)
893 static void validate_my_share_entries(struct smbd_server_connection *sconn,
895 struct share_mode_entry *share_entry)
899 if (!procid_is_me(&share_entry->pid)) {
903 if (is_deferred_open_entry(share_entry) &&
904 !open_was_deferred(sconn, share_entry->op_mid)) {
905 char *str = talloc_asprintf(talloc_tos(),
906 "Got a deferred entry without a request: "
908 share_mode_str(talloc_tos(), num, share_entry));
912 if (!is_valid_share_mode_entry(share_entry)) {
916 fsp = file_find_dif(sconn, share_entry->id,
917 share_entry->share_file_id);
919 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
920 share_mode_str(talloc_tos(), num, share_entry) ));
921 smb_panic("validate_my_share_entries: Cannot match a "
922 "share entry with an open file\n");
925 if (is_deferred_open_entry(share_entry)) {
929 if ((share_entry->op_type == NO_OPLOCK) &&
930 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
931 /* Someone has already written to it, but I haven't yet
936 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
945 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
946 share_mode_str(talloc_tos(), num, share_entry) ));
947 str = talloc_asprintf(talloc_tos(),
948 "validate_my_share_entries: "
949 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
950 fsp->fsp_name->base_name,
951 (unsigned int)fsp->oplock_type,
952 (unsigned int)share_entry->op_type );
958 bool is_stat_open(uint32 access_mask)
960 return (access_mask &&
961 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
962 FILE_WRITE_ATTRIBUTES))==0) &&
963 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
964 FILE_WRITE_ATTRIBUTES)) != 0));
967 /****************************************************************************
968 Deal with share modes
969 Invarient: Share mode must be locked on entry and exit.
970 Returns -1 on error, or number of share modes on success (may be zero).
971 ****************************************************************************/
973 static NTSTATUS open_mode_check(connection_struct *conn,
974 struct share_mode_lock *lck,
978 uint32 create_options,
983 if(lck->data->num_share_modes == 0) {
987 *file_existed = True;
989 /* A delete on close prohibits everything */
991 if (is_delete_on_close_set(lck, name_hash)) {
992 return NT_STATUS_DELETE_PENDING;
995 if (is_stat_open(access_mask)) {
996 /* Stat open that doesn't trigger oplock breaks or share mode
997 * checks... ! JRA. */
1002 * Check if the share modes will give us access.
1005 #if defined(DEVELOPER)
1006 for(i = 0; i < lck->data->num_share_modes; i++) {
1007 validate_my_share_entries(conn->sconn, i,
1008 &lck->data->share_modes[i]);
1012 if (!lp_share_modes(SNUM(conn))) {
1013 return NT_STATUS_OK;
1016 /* Now we check the share modes, after any oplock breaks. */
1017 for(i = 0; i < lck->data->num_share_modes; i++) {
1019 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1023 /* someone else has a share lock on it, check to see if we can
1025 if (share_conflict(&lck->data->share_modes[i],
1026 access_mask, share_access)) {
1028 if (share_mode_stale_pid(lck->data, i)) {
1032 return NT_STATUS_SHARING_VIOLATION;
1036 return NT_STATUS_OK;
1039 static bool is_delete_request(files_struct *fsp) {
1040 return ((fsp->access_mask == DELETE_ACCESS) &&
1041 (fsp->oplock_type == NO_OPLOCK));
1045 * Send a break message to the oplock holder and delay the open for
1049 static NTSTATUS send_break_message(files_struct *fsp,
1050 struct share_mode_entry *exclusive,
1055 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1057 DEBUG(10, ("Sending break request to PID %s\n",
1058 procid_str_static(&exclusive->pid)));
1059 exclusive->op_mid = mid;
1061 /* Create the message. */
1062 share_mode_entry_to_message(msg, exclusive);
1064 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1065 don't want this set in the share mode struct pointed to by lck. */
1067 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1068 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1069 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1072 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1073 MSG_SMB_BREAK_REQUEST,
1075 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 DEBUG(3, ("Could not send oplock break message: %s\n",
1078 nt_errstr(status)));
1085 * Return share_mode_entry pointers for :
1086 * 1). Batch oplock entry.
1087 * 2). Batch or exclusive oplock entry (may be identical to #1).
1088 * bool have_level2_oplock
1089 * bool have_no_oplock.
1090 * Do internal consistency checks on the share mode for a file.
1093 static void find_oplock_types(files_struct *fsp,
1095 const struct share_mode_lock *lck,
1096 struct share_mode_entry **pp_batch,
1097 struct share_mode_entry **pp_ex_or_batch,
1099 bool *got_no_oplock)
1104 *pp_ex_or_batch = NULL;
1105 *got_level2 = false;
1106 *got_no_oplock = false;
1108 /* Ignore stat or internal opens, as is done in
1109 delay_for_batch_oplocks() and
1110 delay_for_exclusive_oplocks().
1112 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1116 for (i=0; i<lck->data->num_share_modes; i++) {
1117 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1121 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1122 is_stat_open(lck->data->share_modes[i].access_mask)) {
1123 /* We ignore stat opens in the table - they
1124 always have NO_OPLOCK and never get or
1125 cause breaks. JRA. */
1129 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1130 /* batch - can only be one. */
1131 if (share_mode_stale_pid(lck->data, i)) {
1132 DEBUG(10, ("Found stale batch oplock\n"));
1135 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1136 smb_panic("Bad batch oplock entry.");
1138 *pp_batch = &lck->data->share_modes[i];
1141 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1142 if (share_mode_stale_pid(lck->data, i)) {
1143 DEBUG(10, ("Found stale duplicate oplock\n"));
1146 /* Exclusive or batch - can only be one. */
1147 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1148 smb_panic("Bad exclusive or batch oplock entry.");
1150 *pp_ex_or_batch = &lck->data->share_modes[i];
1153 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1154 if (*pp_batch || *pp_ex_or_batch) {
1155 if (share_mode_stale_pid(lck->data, i)) {
1156 DEBUG(10, ("Found stale LevelII "
1160 smb_panic("Bad levelII oplock entry.");
1165 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1166 if (*pp_batch || *pp_ex_or_batch) {
1167 if (share_mode_stale_pid(lck->data, i)) {
1168 DEBUG(10, ("Found stale NO_OPLOCK "
1172 smb_panic("Bad no oplock entry.");
1174 *got_no_oplock = true;
1179 static bool delay_for_batch_oplocks(files_struct *fsp,
1182 struct share_mode_entry *batch_entry)
1184 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1187 if (batch_entry == NULL) {
1191 /* Found a batch oplock */
1192 send_break_message(fsp, batch_entry, mid, oplock_request);
1196 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1199 struct share_mode_entry *ex_entry)
1203 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1206 if (ex_entry == NULL) {
1210 /* Found an exclusive or batch oplock */
1212 delay_it = is_delete_request(fsp) ?
1213 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1219 send_break_message(fsp, ex_entry, mid, oplock_request);
1223 static bool file_has_brlocks(files_struct *fsp)
1225 struct byte_range_lock *br_lck;
1227 br_lck = brl_get_locks_readonly(fsp);
1231 return br_lck->num_locks > 0 ? true : false;
1234 static void grant_fsp_oplock_type(files_struct *fsp,
1236 bool got_level2_oplock,
1237 bool got_a_none_oplock)
1239 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1240 lp_level2_oplocks(SNUM(fsp->conn));
1242 /* Start by granting what the client asked for,
1243 but ensure no SAMBA_PRIVATE bits can be set. */
1244 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1246 if (oplock_request & INTERNAL_OPEN_ONLY) {
1247 /* No oplocks on internal open. */
1248 fsp->oplock_type = NO_OPLOCK;
1249 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1250 fsp->oplock_type, fsp_str_dbg(fsp)));
1252 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1253 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1255 fsp->oplock_type = NO_OPLOCK;
1258 if (is_stat_open(fsp->access_mask)) {
1259 /* Leave the value already set. */
1260 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1261 fsp->oplock_type, fsp_str_dbg(fsp)));
1266 * Match what was requested (fsp->oplock_type) with
1267 * what was found in the existing share modes.
1270 if (got_a_none_oplock) {
1271 fsp->oplock_type = NO_OPLOCK;
1272 } else if (got_level2_oplock) {
1273 if (fsp->oplock_type == NO_OPLOCK ||
1274 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1275 /* Store a level2 oplock, but don't tell the client */
1276 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1278 fsp->oplock_type = LEVEL_II_OPLOCK;
1281 /* All share_mode_entries are placeholders or deferred.
1282 * Silently upgrade to fake levelII if the client didn't
1283 * ask for an oplock. */
1284 if (fsp->oplock_type == NO_OPLOCK) {
1285 /* Store a level2 oplock, but don't tell the client */
1286 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1291 * Don't grant level2 to clients that don't want them
1292 * or if we've turned them off.
1294 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1295 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1298 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1299 fsp->oplock_type, fsp_str_dbg(fsp)));
1302 bool request_timed_out(struct timeval request_time,
1303 struct timeval timeout)
1305 struct timeval now, end_time;
1307 end_time = timeval_sum(&request_time, &timeout);
1308 return (timeval_compare(&end_time, &now) < 0);
1311 /****************************************************************************
1312 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1313 ****************************************************************************/
1315 static void defer_open(struct share_mode_lock *lck,
1316 struct timeval request_time,
1317 struct timeval timeout,
1318 struct smb_request *req,
1319 struct deferred_open_record *state)
1323 /* Paranoia check */
1325 for (i=0; i<lck->data->num_share_modes; i++) {
1326 struct share_mode_entry *e = &lck->data->share_modes[i];
1328 if (is_deferred_open_entry(e) &&
1329 procid_is_me(&e->pid) &&
1330 (e->op_mid == req->mid)) {
1331 DEBUG(0, ("Trying to defer an already deferred "
1332 "request: mid=%llu, exiting\n",
1333 (unsigned long long)req->mid));
1334 exit_server("attempt to defer a deferred request");
1338 /* End paranoia check */
1340 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1341 "open entry for mid %llu\n",
1342 (unsigned int)request_time.tv_sec,
1343 (unsigned int)request_time.tv_usec,
1344 (unsigned long long)req->mid));
1346 if (!push_deferred_open_message_smb(req, request_time, timeout,
1347 state->id, (char *)state, sizeof(*state))) {
1348 exit_server("push_deferred_open_message_smb failed");
1350 add_deferred_open(lck, req->mid, request_time,
1351 messaging_server_id(req->sconn->msg_ctx), state->id);
1355 /****************************************************************************
1356 On overwrite open ensure that the attributes match.
1357 ****************************************************************************/
1359 bool open_match_attributes(connection_struct *conn,
1360 uint32 old_dos_attr,
1361 uint32 new_dos_attr,
1362 mode_t existing_unx_mode,
1363 mode_t new_unx_mode,
1364 mode_t *returned_unx_mode)
1366 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1368 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1369 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1371 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1372 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1373 *returned_unx_mode = new_unx_mode;
1375 *returned_unx_mode = (mode_t)0;
1378 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1379 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1380 "returned_unx_mode = 0%o\n",
1381 (unsigned int)old_dos_attr,
1382 (unsigned int)existing_unx_mode,
1383 (unsigned int)new_dos_attr,
1384 (unsigned int)*returned_unx_mode ));
1386 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1387 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1388 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1389 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1393 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1394 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1395 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1402 /****************************************************************************
1403 Special FCB or DOS processing in the case of a sharing violation.
1404 Try and find a duplicated file handle.
1405 ****************************************************************************/
1407 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1408 connection_struct *conn,
1409 files_struct *fsp_to_dup_into,
1410 const struct smb_filename *smb_fname,
1415 uint32 share_access,
1416 uint32 create_options)
1420 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1421 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1423 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1424 fsp = file_find_di_next(fsp)) {
1426 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1427 "vuid = %u, file_pid = %u, private_options = 0x%x "
1428 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1429 fsp->fh->fd, (unsigned int)fsp->vuid,
1430 (unsigned int)fsp->file_pid,
1431 (unsigned int)fsp->fh->private_options,
1432 (unsigned int)fsp->access_mask ));
1434 if (fsp->fh->fd != -1 &&
1435 fsp->vuid == vuid &&
1436 fsp->file_pid == file_pid &&
1437 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1438 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1439 (fsp->access_mask & FILE_WRITE_DATA) &&
1440 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1441 strequal(fsp->fsp_name->stream_name,
1442 smb_fname->stream_name)) {
1443 DEBUG(10,("fcb_or_dos_open: file match\n"));
1449 return NT_STATUS_NOT_FOUND;
1452 /* quite an insane set of semantics ... */
1453 if (is_executable(smb_fname->base_name) &&
1454 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1455 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1456 return NT_STATUS_INVALID_PARAMETER;
1459 /* We need to duplicate this fsp. */
1460 return dup_file_fsp(req, fsp, access_mask, share_access,
1461 create_options, fsp_to_dup_into);
1464 static void schedule_defer_open(struct share_mode_lock *lck,
1465 struct timeval request_time,
1466 struct smb_request *req)
1468 struct deferred_open_record state;
1470 /* This is a relative time, added to the absolute
1471 request_time value to get the absolute timeout time.
1472 Note that if this is the second or greater time we enter
1473 this codepath for this particular request mid then
1474 request_time is left as the absolute time of the *first*
1475 time this request mid was processed. This is what allows
1476 the request to eventually time out. */
1478 struct timeval timeout;
1480 /* Normally the smbd we asked should respond within
1481 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1482 * the client did, give twice the timeout as a safety
1483 * measure here in case the other smbd is stuck
1484 * somewhere else. */
1486 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1488 /* Nothing actually uses state.delayed_for_oplocks
1489 but it's handy to differentiate in debug messages
1490 between a 30 second delay due to oplock break, and
1491 a 1 second delay for share mode conflicts. */
1493 state.delayed_for_oplocks = True;
1494 state.id = lck->data->id;
1496 if (!request_timed_out(request_time, timeout)) {
1497 defer_open(lck, request_time, timeout, req, &state);
1501 /****************************************************************************
1502 Work out what access_mask to use from what the client sent us.
1503 ****************************************************************************/
1505 static NTSTATUS smbd_calculate_maximum_allowed_access(
1506 connection_struct *conn,
1507 const struct smb_filename *smb_fname,
1508 uint32_t *p_access_mask)
1510 struct security_descriptor *sd;
1511 uint32_t access_granted;
1514 if (get_current_uid(conn) == (uid_t)0) {
1515 *p_access_mask |= FILE_GENERIC_ALL;
1516 return NT_STATUS_OK;
1519 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1524 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1526 * File did not exist
1528 *p_access_mask = FILE_GENERIC_ALL;
1529 return NT_STATUS_OK;
1531 if (!NT_STATUS_IS_OK(status)) {
1532 DEBUG(10,("smbd_calculate_access_mask: "
1533 "Could not get acl on file %s: %s\n",
1534 smb_fname_str_dbg(smb_fname),
1535 nt_errstr(status)));
1536 return NT_STATUS_ACCESS_DENIED;
1540 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1541 * also takes care of owner WRITE_DAC and READ_CONTROL.
1543 status = se_access_check(sd,
1544 get_current_nttok(conn),
1545 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1550 if (!NT_STATUS_IS_OK(status)) {
1551 DEBUG(10, ("smbd_calculate_access_mask: "
1552 "Access denied on file %s: "
1553 "when calculating maximum access\n",
1554 smb_fname_str_dbg(smb_fname)));
1555 return NT_STATUS_ACCESS_DENIED;
1557 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1558 return NT_STATUS_OK;
1561 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1562 const struct smb_filename *smb_fname,
1563 uint32_t access_mask,
1564 uint32_t *access_mask_out)
1567 uint32_t orig_access_mask = access_mask;
1568 uint32_t rejected_share_access;
1571 * Convert GENERIC bits to specific bits.
1574 se_map_generic(&access_mask, &file_generic_mapping);
1576 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1577 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1579 status = smbd_calculate_maximum_allowed_access(
1580 conn, smb_fname, &access_mask);
1582 if (!NT_STATUS_IS_OK(status)) {
1586 access_mask &= conn->share_access;
1589 rejected_share_access = access_mask & ~(conn->share_access);
1591 if (rejected_share_access) {
1592 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1593 "file %s: rejected by share access mask[0x%08X] "
1594 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1595 smb_fname_str_dbg(smb_fname),
1597 orig_access_mask, access_mask,
1598 rejected_share_access));
1599 return NT_STATUS_ACCESS_DENIED;
1602 *access_mask_out = access_mask;
1603 return NT_STATUS_OK;
1606 /****************************************************************************
1607 Remove the deferred open entry under lock.
1608 ****************************************************************************/
1610 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1611 struct server_id pid)
1613 struct share_mode_lock *lck = get_existing_share_mode_lock(
1616 DEBUG(0, ("could not get share mode lock\n"));
1619 del_deferred_open_entry(lck, mid, pid);
1623 /****************************************************************************
1624 Open a file with a share mode. Passed in an already created files_struct *.
1625 ****************************************************************************/
1627 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1628 struct smb_request *req,
1629 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1630 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1631 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1632 uint32 create_options, /* options such as delete on close. */
1633 uint32 new_dos_attributes, /* attributes used for new file. */
1634 int oplock_request, /* internal Samba oplock codes. */
1635 /* Information (FILE_EXISTS etc.) */
1636 uint32_t private_flags, /* Samba specific flags. */
1640 struct smb_filename *smb_fname = fsp->fsp_name;
1643 bool file_existed = VALID_STAT(smb_fname->st);
1644 bool def_acl = False;
1645 bool posix_open = False;
1646 bool new_file_created = False;
1647 bool clear_ads = false;
1648 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1649 mode_t new_unx_mode = (mode_t)0;
1650 mode_t unx_mode = (mode_t)0;
1652 uint32 existing_dos_attributes = 0;
1653 struct timeval request_time = timeval_zero();
1654 struct share_mode_lock *lck = NULL;
1655 uint32 open_access_mask = access_mask;
1659 if (conn->printer) {
1661 * Printers are handled completely differently.
1662 * Most of the passed parameters are ignored.
1666 *pinfo = FILE_WAS_CREATED;
1669 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1670 smb_fname_str_dbg(smb_fname)));
1673 DEBUG(0,("open_file_ntcreate: printer open without "
1674 "an SMB request!\n"));
1675 return NT_STATUS_INTERNAL_ERROR;
1678 return print_spool_open(fsp, smb_fname->base_name,
1682 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1684 return NT_STATUS_NO_MEMORY;
1687 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1689 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1690 new_dos_attributes = 0;
1692 /* Windows allows a new file to be created and
1693 silently removes a FILE_ATTRIBUTE_DIRECTORY
1694 sent by the client. Do the same. */
1696 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1698 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1700 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1701 smb_fname, parent_dir);
1704 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1705 "access_mask=0x%x share_access=0x%x "
1706 "create_disposition = 0x%x create_options=0x%x "
1707 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1708 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1709 access_mask, share_access, create_disposition,
1710 create_options, (unsigned int)unx_mode, oplock_request,
1711 (unsigned int)private_flags));
1713 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1714 DEBUG(0, ("No smb request but not an internal only open!\n"));
1715 return NT_STATUS_INTERNAL_ERROR;
1719 * Only non-internal opens can be deferred at all
1724 if (get_deferred_open_message_state(req,
1728 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1729 /* Remember the absolute time of the original
1730 request with this mid. We'll use it later to
1731 see if this has timed out. */
1733 /* Remove the deferred open entry under lock. */
1734 remove_deferred_open_entry(
1735 state->id, req->mid,
1736 messaging_server_id(req->sconn->msg_ctx));
1738 /* Ensure we don't reprocess this message. */
1739 remove_deferred_open_message_smb(req->sconn, req->mid);
1744 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1746 existing_dos_attributes = dos_mode(conn, smb_fname);
1750 /* ignore any oplock requests if oplocks are disabled */
1751 if (!lp_oplocks(SNUM(conn)) ||
1752 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1753 /* Mask off everything except the private Samba bits. */
1754 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1757 /* this is for OS/2 long file names - say we don't support them */
1758 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1759 /* OS/2 Workplace shell fix may be main code stream in a later
1761 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1763 if (use_nt_status()) {
1764 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1766 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1769 switch( create_disposition ) {
1771 * Currently we're using FILE_SUPERSEDE as the same as
1772 * FILE_OVERWRITE_IF but they really are
1773 * different. FILE_SUPERSEDE deletes an existing file
1774 * (requiring delete access) then recreates it.
1776 case FILE_SUPERSEDE:
1777 /* If file exists replace/overwrite. If file doesn't
1779 flags2 |= (O_CREAT | O_TRUNC);
1783 case FILE_OVERWRITE_IF:
1784 /* If file exists replace/overwrite. If file doesn't
1786 flags2 |= (O_CREAT | O_TRUNC);
1791 /* If file exists open. If file doesn't exist error. */
1792 if (!file_existed) {
1793 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1794 "requested for file %s and file "
1796 smb_fname_str_dbg(smb_fname)));
1798 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1802 case FILE_OVERWRITE:
1803 /* If file exists overwrite. If file doesn't exist
1805 if (!file_existed) {
1806 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1807 "requested for file %s and file "
1809 smb_fname_str_dbg(smb_fname) ));
1811 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1818 /* If file exists error. If file doesn't exist
1821 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1822 "requested for file %s and file "
1823 "already exists.\n",
1824 smb_fname_str_dbg(smb_fname)));
1825 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1830 return map_nt_error_from_unix(errno);
1832 flags2 |= (O_CREAT|O_EXCL);
1836 /* If file exists open. If file doesn't exist
1842 return NT_STATUS_INVALID_PARAMETER;
1845 /* We only care about matching attributes on file exists and
1848 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1849 (create_disposition == FILE_OVERWRITE_IF))) {
1850 if (!open_match_attributes(conn, existing_dos_attributes,
1852 smb_fname->st.st_ex_mode,
1853 unx_mode, &new_unx_mode)) {
1854 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1855 "for file %s (%x %x) (0%o, 0%o)\n",
1856 smb_fname_str_dbg(smb_fname),
1857 existing_dos_attributes,
1859 (unsigned int)smb_fname->st.st_ex_mode,
1860 (unsigned int)unx_mode ));
1862 return NT_STATUS_ACCESS_DENIED;
1866 status = smbd_calculate_access_mask(conn, smb_fname,
1869 if (!NT_STATUS_IS_OK(status)) {
1870 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1871 "on file %s returned %s\n",
1872 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1876 open_access_mask = access_mask;
1878 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1879 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1882 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1883 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1887 * Note that we ignore the append flag as append does not
1888 * mean the same thing under DOS and Unix.
1891 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1892 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1893 /* DENY_DOS opens are always underlying read-write on the
1894 file handle, no matter what the requested access mask
1896 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1897 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1907 * Currently we only look at FILE_WRITE_THROUGH for create options.
1911 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1916 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1920 if (!posix_open && !CAN_WRITE(conn)) {
1922 * We should really return a permission denied error if either
1923 * O_CREAT or O_TRUNC are set, but for compatibility with
1924 * older versions of Samba we just AND them out.
1926 flags2 &= ~(O_CREAT|O_TRUNC);
1930 * Ensure we can't write on a read-only share or file.
1933 if (flags != O_RDONLY && file_existed &&
1934 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1935 DEBUG(5,("open_file_ntcreate: write access requested for "
1936 "file %s on read only %s\n",
1937 smb_fname_str_dbg(smb_fname),
1938 !CAN_WRITE(conn) ? "share" : "file" ));
1940 return NT_STATUS_ACCESS_DENIED;
1943 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1944 fsp->share_access = share_access;
1945 fsp->fh->private_options = private_flags;
1946 fsp->access_mask = open_access_mask; /* We change this to the
1947 * requested access_mask after
1948 * the open is done. */
1949 fsp->posix_open = posix_open;
1951 /* Ensure no SAMBA_PRIVATE bits can be set. */
1952 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1954 if (timeval_is_zero(&request_time)) {
1955 request_time = fsp->open_time;
1959 struct share_mode_entry *batch_entry = NULL;
1960 struct share_mode_entry *exclusive_entry = NULL;
1961 bool got_level2_oplock = false;
1962 bool got_a_none_oplock = false;
1965 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1966 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1968 lck = get_share_mode_lock(talloc_tos(), id,
1970 smb_fname, &old_write_time);
1972 DEBUG(0, ("Could not get share mode lock\n"));
1973 return NT_STATUS_SHARING_VIOLATION;
1976 /* Get the types we need to examine. */
1977 find_oplock_types(fsp,
1983 &got_a_none_oplock);
1985 /* First pass - send break only on batch oplocks. */
1986 if ((req != NULL) &&
1987 delay_for_batch_oplocks(fsp,
1991 schedule_defer_open(lck, request_time, req);
1993 return NT_STATUS_SHARING_VIOLATION;
1996 /* Use the client requested access mask here, not the one we
1998 status = open_mode_check(conn, lck, fsp->name_hash,
1999 access_mask, share_access,
2000 create_options, &file_existed);
2002 if (NT_STATUS_IS_OK(status)) {
2003 /* We might be going to allow this open. Check oplock
2005 /* Second pass - send break for both batch or
2006 * exclusive oplocks. */
2007 if ((req != NULL) &&
2008 delay_for_exclusive_oplocks(
2013 schedule_defer_open(lck, request_time, req);
2015 return NT_STATUS_SHARING_VIOLATION;
2019 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2020 /* DELETE_PENDING is not deferred for a second */
2025 grant_fsp_oplock_type(fsp,
2030 if (!NT_STATUS_IS_OK(status)) {
2031 uint32 can_access_mask;
2032 bool can_access = True;
2034 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2036 /* Check if this can be done with the deny_dos and fcb
2039 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2040 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2042 DEBUG(0, ("DOS open without an SMB "
2045 return NT_STATUS_INTERNAL_ERROR;
2048 /* Use the client requested access mask here,
2049 * not the one we open with. */
2050 status = fcb_or_dos_open(req,
2061 if (NT_STATUS_IS_OK(status)) {
2064 *pinfo = FILE_WAS_OPENED;
2066 return NT_STATUS_OK;
2071 * This next line is a subtlety we need for
2072 * MS-Access. If a file open will fail due to share
2073 * permissions and also for security (access) reasons,
2074 * we need to return the access failed error, not the
2075 * share error. We can't open the file due to kernel
2076 * oplock deadlock (it's possible we failed above on
2077 * the open_mode_check()) so use a userspace check.
2080 if (flags & O_RDWR) {
2081 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2082 } else if (flags & O_WRONLY) {
2083 can_access_mask = FILE_WRITE_DATA;
2085 can_access_mask = FILE_READ_DATA;
2088 if (((can_access_mask & FILE_WRITE_DATA) &&
2089 !CAN_WRITE(conn)) ||
2090 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2091 smb_fname, can_access_mask))) {
2096 * If we're returning a share violation, ensure we
2097 * cope with the braindead 1 second delay.
2100 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2101 lp_defer_sharing_violations()) {
2102 struct timeval timeout;
2103 struct deferred_open_record state;
2106 /* this is a hack to speed up torture tests
2108 timeout_usecs = lp_parm_int(SNUM(conn),
2109 "smbd","sharedelay",
2110 SHARING_VIOLATION_USEC_WAIT);
2112 /* This is a relative time, added to the absolute
2113 request_time value to get the absolute timeout time.
2114 Note that if this is the second or greater time we enter
2115 this codepath for this particular request mid then
2116 request_time is left as the absolute time of the *first*
2117 time this request mid was processed. This is what allows
2118 the request to eventually time out. */
2120 timeout = timeval_set(0, timeout_usecs);
2122 /* Nothing actually uses state.delayed_for_oplocks
2123 but it's handy to differentiate in debug messages
2124 between a 30 second delay due to oplock break, and
2125 a 1 second delay for share mode conflicts. */
2127 state.delayed_for_oplocks = False;
2131 && !request_timed_out(request_time,
2133 defer_open(lck, request_time, timeout,
2141 * We have detected a sharing violation here
2142 * so return the correct error code
2144 status = NT_STATUS_SHARING_VIOLATION;
2146 status = NT_STATUS_ACCESS_DENIED;
2152 * We exit this block with the share entry *locked*.....
2156 SMB_ASSERT(!file_existed || (lck != NULL));
2159 * Ensure we pay attention to default ACLs on directories if required.
2162 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2163 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2164 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2167 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2168 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2169 (unsigned int)flags, (unsigned int)flags2,
2170 (unsigned int)unx_mode, (unsigned int)access_mask,
2171 (unsigned int)open_access_mask));
2174 * open_file strips any O_TRUNC flags itself.
2177 fsp_open = open_file(fsp, conn, req, parent_dir,
2178 flags|flags2, unx_mode, access_mask,
2181 if (!NT_STATUS_IS_OK(fsp_open)) {
2186 if (!file_existed) {
2187 struct share_mode_entry *batch_entry = NULL;
2188 struct share_mode_entry *exclusive_entry = NULL;
2189 bool got_level2_oplock = false;
2190 bool got_a_none_oplock = false;
2191 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2194 * Deal with the race condition where two smbd's detect the
2195 * file doesn't exist and do the create at the same time. One
2196 * of them will win and set a share mode, the other (ie. this
2197 * one) should check if the requested share mode for this
2198 * create is allowed.
2202 * Now the file exists and fsp is successfully opened,
2203 * fsp->dev and fsp->inode are valid and should replace the
2204 * dev=0,inode=0 from a non existent file. Spotted by
2205 * Nadav Danieli <nadavd@exanet.com>. JRA.
2210 lck = get_share_mode_lock(talloc_tos(), id,
2212 smb_fname, &old_write_time);
2215 DEBUG(0, ("open_file_ntcreate: Could not get share "
2216 "mode lock for %s\n",
2217 smb_fname_str_dbg(smb_fname)));
2219 return NT_STATUS_SHARING_VIOLATION;
2222 /* Get the types we need to examine. */
2223 find_oplock_types(fsp,
2229 &got_a_none_oplock);
2231 /* First pass - send break only on batch oplocks. */
2232 if ((req != NULL) &&
2233 delay_for_batch_oplocks(fsp,
2237 schedule_defer_open(lck, request_time, req);
2240 return NT_STATUS_SHARING_VIOLATION;
2243 status = open_mode_check(conn, lck, fsp->name_hash,
2244 access_mask, share_access,
2245 create_options, &file_existed);
2247 if (NT_STATUS_IS_OK(status)) {
2248 /* We might be going to allow this open. Check oplock
2250 /* Second pass - send break for both batch or
2251 * exclusive oplocks. */
2252 if ((req != NULL) &&
2253 delay_for_exclusive_oplocks(
2258 schedule_defer_open(lck, request_time, req);
2261 return NT_STATUS_SHARING_VIOLATION;
2265 if (!NT_STATUS_IS_OK(status)) {
2266 struct deferred_open_record state;
2268 state.delayed_for_oplocks = False;
2271 /* Do it all over again immediately. In the second
2272 * round we will find that the file existed and handle
2273 * the DELETE_PENDING and FCB cases correctly. No need
2274 * to duplicate the code here. Essentially this is a
2275 * "goto top of this function", but don't tell
2279 defer_open(lck, request_time, timeval_zero(),
2287 grant_fsp_oplock_type(fsp,
2293 * We exit this block with the share entry *locked*.....
2298 SMB_ASSERT(lck != NULL);
2300 /* Delete streams if create_disposition requires it */
2301 if (file_existed && clear_ads &&
2302 !is_ntfs_stream_smb_fname(smb_fname)) {
2303 status = delete_all_streams(conn, smb_fname->base_name);
2304 if (!NT_STATUS_IS_OK(status)) {
2311 /* note that we ignore failure for the following. It is
2312 basically a hack for NFS, and NFS will never set one of
2313 these only read them. Nobody but Samba can ever set a deny
2314 mode and we have already checked our more authoritative
2315 locking database for permission to set this deny mode. If
2316 the kernel refuses the operations then the kernel is wrong.
2317 note that GPFS supports it as well - jmcd */
2319 if (fsp->fh->fd != -1) {
2321 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2322 if(ret_flock == -1 ){
2327 return NT_STATUS_SHARING_VIOLATION;
2332 * At this point onwards, we can guarentee that the share entry
2333 * is locked, whether we created the file or not, and that the
2334 * deny mode is compatible with all current opens.
2338 * If requested, truncate the file.
2341 if (file_existed && (flags2&O_TRUNC)) {
2343 * We are modifing the file after open - update the stat
2346 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2347 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2348 status = map_nt_error_from_unix(errno);
2356 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2357 * but we don't have to store this - just ignore it on access check.
2359 if (conn->sconn->using_smb2) {
2361 * SMB2 doesn't return it (according to Microsoft tests).
2362 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2363 * File created with access = 0x7 (Read, Write, Delete)
2364 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2366 fsp->access_mask = access_mask;
2368 /* But SMB1 does. */
2369 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2373 /* stat opens on existing files don't get oplocks. */
2374 if (is_stat_open(open_access_mask)) {
2375 fsp->oplock_type = NO_OPLOCK;
2378 if (flags2 & O_TRUNC) {
2379 info = FILE_WAS_OVERWRITTEN;
2381 info = FILE_WAS_OPENED;
2384 info = FILE_WAS_CREATED;
2392 * Setup the oplock info in both the shared memory and
2396 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2398 * Could not get the kernel oplock or there are byte-range
2399 * locks on the file.
2401 fsp->oplock_type = NO_OPLOCK;
2404 set_share_mode(lck, fsp, get_current_uid(conn),
2408 /* Handle strange delete on close create semantics. */
2409 if (create_options & FILE_DELETE_ON_CLOSE) {
2411 status = can_set_delete_on_close(fsp, new_dos_attributes);
2413 if (!NT_STATUS_IS_OK(status)) {
2414 /* Remember to delete the mode we just added. */
2415 del_share_mode(lck, fsp);
2420 /* Note that here we set the *inital* delete on close flag,
2421 not the regular one. The magic gets handled in close. */
2422 fsp->initial_delete_on_close = True;
2425 if (info == FILE_WAS_OVERWRITTEN
2426 || info == FILE_WAS_CREATED
2427 || info == FILE_WAS_SUPERSEDED) {
2428 new_file_created = True;
2431 if (new_file_created) {
2432 /* Files should be initially set as archive */
2433 if (lp_map_archive(SNUM(conn)) ||
2434 lp_store_dos_attributes(SNUM(conn))) {
2436 if (file_set_dosmode(conn, smb_fname,
2437 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2438 parent_dir, true) == 0) {
2439 unx_mode = smb_fname->st.st_ex_mode;
2445 /* Determine sparse flag. */
2447 /* POSIX opens are sparse by default. */
2448 fsp->is_sparse = true;
2450 fsp->is_sparse = (file_existed &&
2451 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2455 * Take care of inherited ACLs on created files - if default ACL not
2459 if (!posix_open && !file_existed && !def_acl) {
2461 int saved_errno = errno; /* We might get ENOSYS in the next
2464 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2466 errno = saved_errno; /* Ignore ENOSYS */
2469 } else if (new_unx_mode) {
2473 /* Attributes need changing. File already existed. */
2476 int saved_errno = errno; /* We might get ENOSYS in the
2478 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2480 if (ret == -1 && errno == ENOSYS) {
2481 errno = saved_errno; /* Ignore ENOSYS */
2483 DEBUG(5, ("open_file_ntcreate: reset "
2484 "attributes of file %s to 0%o\n",
2485 smb_fname_str_dbg(smb_fname),
2486 (unsigned int)new_unx_mode));
2487 ret = 0; /* Don't do the fchmod below. */
2492 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2493 DEBUG(5, ("open_file_ntcreate: failed to reset "
2494 "attributes of file %s to 0%o\n",
2495 smb_fname_str_dbg(smb_fname),
2496 (unsigned int)new_unx_mode));
2499 /* If this is a successful open, we must remove any deferred open
2502 del_deferred_open_entry(lck, req->mid,
2503 messaging_server_id(req->sconn->msg_ctx));
2507 return NT_STATUS_OK;
2511 /****************************************************************************
2512 Open a file for for write to ensure that we can fchmod it.
2513 ****************************************************************************/
2515 NTSTATUS open_file_fchmod(connection_struct *conn,
2516 struct smb_filename *smb_fname,
2517 files_struct **result)
2519 if (!VALID_STAT(smb_fname->st)) {
2520 return NT_STATUS_INVALID_PARAMETER;
2523 return SMB_VFS_CREATE_FILE(
2526 0, /* root_dir_fid */
2527 smb_fname, /* fname */
2528 FILE_WRITE_DATA, /* access_mask */
2529 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2531 FILE_OPEN, /* create_disposition*/
2532 0, /* create_options */
2533 0, /* file_attributes */
2534 INTERNAL_OPEN_ONLY, /* oplock_request */
2535 0, /* allocation_size */
2536 0, /* private_flags */
2539 result, /* result */
2543 static NTSTATUS mkdir_internal(connection_struct *conn,
2544 struct smb_filename *smb_dname,
2545 uint32 file_attributes)
2548 char *parent_dir = NULL;
2550 bool posix_open = false;
2551 bool need_re_stat = false;
2552 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2554 if(access_mask & ~(conn->share_access)) {
2555 DEBUG(5,("mkdir_internal: failing share access "
2556 "%s\n", lp_servicename(SNUM(conn))));
2557 return NT_STATUS_ACCESS_DENIED;
2560 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2562 return NT_STATUS_NO_MEMORY;
2565 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2567 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2569 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2572 status = check_parent_access(conn,
2575 if(!NT_STATUS_IS_OK(status)) {
2576 DEBUG(5,("mkdir_internal: check_parent_access "
2577 "on directory %s for path %s returned %s\n",
2579 smb_dname->base_name,
2580 nt_errstr(status) ));
2584 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2585 return map_nt_error_from_unix(errno);
2588 /* Ensure we're checking for a symlink here.... */
2589 /* We don't want to get caught by a symlink racer. */
2591 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2592 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2593 smb_fname_str_dbg(smb_dname), strerror(errno)));
2594 return map_nt_error_from_unix(errno);
2597 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2598 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2599 smb_fname_str_dbg(smb_dname)));
2600 return NT_STATUS_NOT_A_DIRECTORY;
2603 if (lp_store_dos_attributes(SNUM(conn))) {
2605 file_set_dosmode(conn, smb_dname,
2606 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2611 if (lp_inherit_perms(SNUM(conn))) {
2612 inherit_access_posix_acl(conn, parent_dir,
2613 smb_dname->base_name, mode);
2614 need_re_stat = true;
2619 * Check if high bits should have been set,
2620 * then (if bits are missing): add them.
2621 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2624 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2625 (mode & ~smb_dname->st.st_ex_mode)) {
2626 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2627 (smb_dname->st.st_ex_mode |
2628 (mode & ~smb_dname->st.st_ex_mode)));
2629 need_re_stat = true;
2633 /* Change the owner if required. */
2634 if (lp_inherit_owner(SNUM(conn))) {
2635 change_dir_owner_to_parent(conn, parent_dir,
2636 smb_dname->base_name,
2638 need_re_stat = true;
2642 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2643 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2644 smb_fname_str_dbg(smb_dname), strerror(errno)));
2645 return map_nt_error_from_unix(errno);
2649 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2650 smb_dname->base_name);
2652 return NT_STATUS_OK;
2655 /****************************************************************************
2656 Ensure we didn't get symlink raced on opening a directory.
2657 ****************************************************************************/
2659 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2660 const SMB_STRUCT_STAT *sbuf2)
2662 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2663 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2664 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2665 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2671 /****************************************************************************
2672 Open a directory from an NT SMB call.
2673 ****************************************************************************/
2675 static NTSTATUS open_directory(connection_struct *conn,
2676 struct smb_request *req,
2677 struct smb_filename *smb_dname,
2679 uint32 share_access,
2680 uint32 create_disposition,
2681 uint32 create_options,
2682 uint32 file_attributes,
2684 files_struct **result)
2686 files_struct *fsp = NULL;
2687 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2688 struct share_mode_lock *lck = NULL;
2690 struct timespec mtimespec;
2693 if (is_ntfs_stream_smb_fname(smb_dname)) {
2694 DEBUG(2, ("open_directory: %s is a stream name!\n",
2695 smb_fname_str_dbg(smb_dname)));
2696 return NT_STATUS_NOT_A_DIRECTORY;
2699 /* Ensure we have a directory attribute. */
2700 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2702 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2703 "share_access = 0x%x create_options = 0x%x, "
2704 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2705 smb_fname_str_dbg(smb_dname),
2706 (unsigned int)access_mask,
2707 (unsigned int)share_access,
2708 (unsigned int)create_options,
2709 (unsigned int)create_disposition,
2710 (unsigned int)file_attributes));
2712 status = smbd_calculate_access_mask(conn, smb_dname,
2713 access_mask, &access_mask);
2714 if (!NT_STATUS_IS_OK(status)) {
2715 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2716 "on file %s returned %s\n",
2717 smb_fname_str_dbg(smb_dname),
2718 nt_errstr(status)));
2722 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2723 !security_token_has_privilege(get_current_nttok(conn),
2724 SEC_PRIV_SECURITY)) {
2725 DEBUG(10, ("open_directory: open on %s "
2726 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2727 smb_fname_str_dbg(smb_dname)));
2728 return NT_STATUS_PRIVILEGE_NOT_HELD;
2731 switch( create_disposition ) {
2735 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2738 info = FILE_WAS_OPENED;
2743 /* If directory exists error. If directory doesn't
2747 status = NT_STATUS_OBJECT_NAME_COLLISION;
2748 DEBUG(2, ("open_directory: unable to create "
2749 "%s. Error was %s\n",
2750 smb_fname_str_dbg(smb_dname),
2751 nt_errstr(status)));
2755 status = mkdir_internal(conn, smb_dname,
2758 if (!NT_STATUS_IS_OK(status)) {
2759 DEBUG(2, ("open_directory: unable to create "
2760 "%s. Error was %s\n",
2761 smb_fname_str_dbg(smb_dname),
2762 nt_errstr(status)));
2766 info = FILE_WAS_CREATED;
2771 * If directory exists open. If directory doesn't
2776 status = NT_STATUS_OK;
2777 info = FILE_WAS_OPENED;
2779 status = mkdir_internal(conn, smb_dname,
2782 if (NT_STATUS_IS_OK(status)) {
2783 info = FILE_WAS_CREATED;
2785 /* Cope with create race. */
2786 if (!NT_STATUS_EQUAL(status,
2787 NT_STATUS_OBJECT_NAME_COLLISION)) {
2788 DEBUG(2, ("open_directory: unable to create "
2789 "%s. Error was %s\n",
2790 smb_fname_str_dbg(smb_dname),
2791 nt_errstr(status)));
2794 info = FILE_WAS_OPENED;
2800 case FILE_SUPERSEDE:
2801 case FILE_OVERWRITE:
2802 case FILE_OVERWRITE_IF:
2804 DEBUG(5,("open_directory: invalid create_disposition "
2805 "0x%x for directory %s\n",
2806 (unsigned int)create_disposition,
2807 smb_fname_str_dbg(smb_dname)));
2808 return NT_STATUS_INVALID_PARAMETER;
2811 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2812 DEBUG(5,("open_directory: %s is not a directory !\n",
2813 smb_fname_str_dbg(smb_dname)));
2814 return NT_STATUS_NOT_A_DIRECTORY;
2817 if (info == FILE_WAS_OPENED) {
2818 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2819 if (!NT_STATUS_IS_OK(status)) {
2820 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2821 "file %s failed with %s\n",
2822 smb_fname_str_dbg(smb_dname),
2823 nt_errstr(status)));
2828 status = file_new(req, conn, &fsp);
2829 if(!NT_STATUS_IS_OK(status)) {
2834 * Setup the files_struct for it.
2837 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2838 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2839 fsp->file_pid = req ? req->smbpid : 0;
2840 fsp->can_lock = False;
2841 fsp->can_read = False;
2842 fsp->can_write = False;
2844 fsp->share_access = share_access;
2845 fsp->fh->private_options = 0;
2847 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2849 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2850 fsp->print_file = NULL;
2851 fsp->modified = False;
2852 fsp->oplock_type = NO_OPLOCK;
2853 fsp->sent_oplock_break = NO_BREAK_SENT;
2854 fsp->is_directory = True;
2855 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2856 status = fsp_set_smb_fname(fsp, smb_dname);
2857 if (!NT_STATUS_IS_OK(status)) {
2858 file_free(req, fsp);
2862 mtimespec = smb_dname->st.st_ex_mtime;
2865 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2867 /* POSIX allows us to open a directory with O_RDONLY. */
2868 status = fd_open(conn, fsp, O_RDONLY, 0);
2870 if (!NT_STATUS_IS_OK(status)) {
2871 DEBUG(5, ("open_directory: Could not open fd for "
2873 smb_fname_str_dbg(smb_dname),
2874 nt_errstr(status)));
2875 file_free(req, fsp);
2879 status = vfs_stat_fsp(fsp);
2880 if (!NT_STATUS_IS_OK(status)) {
2882 file_free(req, fsp);
2886 /* Ensure there was no race condition. */
2887 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2888 DEBUG(5,("open_directory: stat struct differs for "
2890 smb_fname_str_dbg(smb_dname)));
2892 file_free(req, fsp);
2893 return NT_STATUS_ACCESS_DENIED;
2896 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2897 conn->connectpath, smb_dname,
2901 DEBUG(0, ("open_directory: Could not get share mode lock for "
2902 "%s\n", smb_fname_str_dbg(smb_dname)));
2904 file_free(req, fsp);
2905 return NT_STATUS_SHARING_VIOLATION;
2908 status = open_mode_check(conn, lck, fsp->name_hash,
2909 access_mask, share_access,
2910 create_options, &dir_existed);
2912 if (!NT_STATUS_IS_OK(status)) {
2915 file_free(req, fsp);
2919 set_share_mode(lck, fsp, get_current_uid(conn),
2920 req ? req->mid : 0, NO_OPLOCK);
2922 /* For directories the delete on close bit at open time seems
2923 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2924 if (create_options & FILE_DELETE_ON_CLOSE) {
2925 status = can_set_delete_on_close(fsp, 0);
2926 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2929 file_free(req, fsp);
2933 if (NT_STATUS_IS_OK(status)) {
2934 /* Note that here we set the *inital* delete on close flag,
2935 not the regular one. The magic gets handled in close. */
2936 fsp->initial_delete_on_close = True;
2947 return NT_STATUS_OK;
2950 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2951 struct smb_filename *smb_dname)
2956 status = SMB_VFS_CREATE_FILE(
2959 0, /* root_dir_fid */
2960 smb_dname, /* fname */
2961 FILE_READ_ATTRIBUTES, /* access_mask */
2962 FILE_SHARE_NONE, /* share_access */
2963 FILE_CREATE, /* create_disposition*/
2964 FILE_DIRECTORY_FILE, /* create_options */
2965 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2966 0, /* oplock_request */
2967 0, /* allocation_size */
2968 0, /* private_flags */
2974 if (NT_STATUS_IS_OK(status)) {
2975 close_file(req, fsp, NORMAL_CLOSE);
2981 /****************************************************************************
2982 Receive notification that one of our open files has been renamed by another
2984 ****************************************************************************/
2986 void msg_file_was_renamed(struct messaging_context *msg,
2989 struct server_id server_id,
2993 char *frm = (char *)data->data;
2995 const char *sharepath;
2996 const char *base_name;
2997 const char *stream_name;
2998 struct smb_filename *smb_fname = NULL;
2999 size_t sp_len, bn_len;
3001 struct smbd_server_connection *sconn =
3002 talloc_get_type_abort(private_data,
3003 struct smbd_server_connection);
3005 if (data->data == NULL
3006 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3007 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3008 (int)data->length));
3012 /* Unpack the message. */
3013 pull_file_id_24(frm, &id);
3014 sharepath = &frm[24];
3015 sp_len = strlen(sharepath);
3016 base_name = sharepath + sp_len + 1;
3017 bn_len = strlen(base_name);
3018 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3020 /* stream_name must always be NULL if there is no stream. */
3021 if (stream_name[0] == '\0') {
3025 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3026 stream_name, NULL, &smb_fname);
3027 if (!NT_STATUS_IS_OK(status)) {
3031 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3033 sharepath, smb_fname_str_dbg(smb_fname),
3034 file_id_string_tos(&id)));
3036 for(fsp = file_find_di_first(sconn, id); fsp;
3037 fsp = file_find_di_next(fsp)) {
3038 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3040 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3041 fsp->fnum, fsp_str_dbg(fsp),
3042 smb_fname_str_dbg(smb_fname)));
3043 status = fsp_set_smb_fname(fsp, smb_fname);
3044 if (!NT_STATUS_IS_OK(status)) {
3049 /* Now we have the complete path we can work out if this is
3050 actually within this share and adjust newname accordingly. */
3051 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3052 "not sharepath %s) "
3053 "fnum %d from %s -> %s\n",
3054 fsp->conn->connectpath,
3058 smb_fname_str_dbg(smb_fname)));
3062 TALLOC_FREE(smb_fname);
3067 * If a main file is opened for delete, all streams need to be checked for
3068 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3069 * If that works, delete them all by setting the delete on close and close.
3072 NTSTATUS open_streams_for_delete(connection_struct *conn,
3075 struct stream_struct *stream_info = NULL;
3076 files_struct **streams = NULL;
3078 unsigned int num_streams = 0;
3079 TALLOC_CTX *frame = talloc_stackframe();
3082 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3083 &num_streams, &stream_info);
3085 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3086 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3087 DEBUG(10, ("no streams around\n"));
3089 return NT_STATUS_OK;
3092 if (!NT_STATUS_IS_OK(status)) {
3093 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3094 nt_errstr(status)));
3098 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3101 if (num_streams == 0) {
3103 return NT_STATUS_OK;
3106 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3107 if (streams == NULL) {
3108 DEBUG(0, ("talloc failed\n"));
3109 status = NT_STATUS_NO_MEMORY;
3113 for (i=0; i<num_streams; i++) {
3114 struct smb_filename *smb_fname = NULL;
3116 if (strequal(stream_info[i].name, "::$DATA")) {
3121 status = create_synthetic_smb_fname(talloc_tos(), fname,
3122 stream_info[i].name,
3124 if (!NT_STATUS_IS_OK(status)) {
3128 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3129 DEBUG(10, ("Unable to stat stream: %s\n",
3130 smb_fname_str_dbg(smb_fname)));
3133 status = SMB_VFS_CREATE_FILE(
3136 0, /* root_dir_fid */
3137 smb_fname, /* fname */
3138 DELETE_ACCESS, /* access_mask */
3139 (FILE_SHARE_READ | /* share_access */
3140 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3141 FILE_OPEN, /* create_disposition*/
3142 0, /* create_options */
3143 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3144 0, /* oplock_request */
3145 0, /* allocation_size */
3146 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3149 &streams[i], /* result */
3152 if (!NT_STATUS_IS_OK(status)) {
3153 DEBUG(10, ("Could not open stream %s: %s\n",
3154 smb_fname_str_dbg(smb_fname),
3155 nt_errstr(status)));
3157 TALLOC_FREE(smb_fname);
3160 TALLOC_FREE(smb_fname);
3164 * don't touch the variable "status" beyond this point :-)
3167 for (i -= 1 ; i >= 0; i--) {
3168 if (streams[i] == NULL) {
3172 DEBUG(10, ("Closing stream # %d, %s\n", i,
3173 fsp_str_dbg(streams[i])));
3174 close_file(NULL, streams[i], NORMAL_CLOSE);
3182 /*********************************************************************
3183 Create a default ACL by inheriting from the parent. If no inheritance
3184 from the parent available, don't set anything. This will leave the actual
3185 permissions the new file or directory already got from the filesystem
3186 as the NT ACL when read.
3187 *********************************************************************/
3189 static NTSTATUS inherit_new_acl(files_struct *fsp)
3191 TALLOC_CTX *ctx = talloc_tos();
3192 char *parent_name = NULL;
3193 struct security_descriptor *parent_desc = NULL;
3194 NTSTATUS status = NT_STATUS_OK;
3195 struct security_descriptor *psd = NULL;
3196 struct dom_sid *owner_sid = NULL;
3197 struct dom_sid *group_sid = NULL;
3198 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3199 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3200 bool inheritable_components = false;
3203 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3204 return NT_STATUS_NO_MEMORY;
3207 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3209 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3211 if (!NT_STATUS_IS_OK(status)) {
3215 inheritable_components = sd_has_inheritable_components(parent_desc,
3218 if (!inheritable_components && !inherit_owner) {
3219 /* Nothing to inherit and not setting owner. */
3220 return NT_STATUS_OK;
3223 /* Create an inherited descriptor from the parent. */
3225 if (DEBUGLEVEL >= 10) {
3226 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3227 fsp_str_dbg(fsp) ));
3228 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3231 /* Inherit from parent descriptor if "inherit owner" set. */
3232 if (inherit_owner) {
3233 owner_sid = parent_desc->owner_sid;
3234 group_sid = parent_desc->group_sid;
3237 if (owner_sid == NULL) {
3238 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3240 if (group_sid == NULL) {
3241 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3244 status = se_create_child_secdesc(ctx,
3251 if (!NT_STATUS_IS_OK(status)) {
3255 /* If inheritable_components == false,
3256 se_create_child_secdesc()
3257 creates a security desriptor with a NULL dacl
3258 entry, but with SEC_DESC_DACL_PRESENT. We need
3259 to remove that flag. */
3261 if (!inheritable_components) {
3262 security_info_sent &= ~SECINFO_DACL;
3263 psd->type &= ~SEC_DESC_DACL_PRESENT;
3266 if (DEBUGLEVEL >= 10) {
3267 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3268 fsp_str_dbg(fsp) ));
3269 NDR_PRINT_DEBUG(security_descriptor, psd);
3272 if (inherit_owner) {
3273 /* We need to be root to force this. */
3276 status = SMB_VFS_FSET_NT_ACL(fsp,
3279 if (inherit_owner) {
3286 * Wrapper around open_file_ntcreate and open_directory
3289 static NTSTATUS create_file_unixpath(connection_struct *conn,
3290 struct smb_request *req,
3291 struct smb_filename *smb_fname,
3292 uint32_t access_mask,
3293 uint32_t share_access,
3294 uint32_t create_disposition,
3295 uint32_t create_options,
3296 uint32_t file_attributes,
3297 uint32_t oplock_request,
3298 uint64_t allocation_size,
3299 uint32_t private_flags,
3300 struct security_descriptor *sd,
3301 struct ea_list *ea_list,
3303 files_struct **result,
3306 int info = FILE_WAS_OPENED;
3307 files_struct *base_fsp = NULL;
3308 files_struct *fsp = NULL;
3311 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3312 "file_attributes = 0x%x, share_access = 0x%x, "
3313 "create_disposition = 0x%x create_options = 0x%x "
3314 "oplock_request = 0x%x private_flags = 0x%x "
3315 "ea_list = 0x%p, sd = 0x%p, "
3317 (unsigned int)access_mask,
3318 (unsigned int)file_attributes,
3319 (unsigned int)share_access,
3320 (unsigned int)create_disposition,
3321 (unsigned int)create_options,
3322 (unsigned int)oplock_request,
3323 (unsigned int)private_flags,
3324 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3326 if (create_options & FILE_OPEN_BY_FILE_ID) {
3327 status = NT_STATUS_NOT_SUPPORTED;
3331 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3332 status = NT_STATUS_INVALID_PARAMETER;
3337 oplock_request |= INTERNAL_OPEN_ONLY;
3340 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3341 && (access_mask & DELETE_ACCESS)
3342 && !is_ntfs_stream_smb_fname(smb_fname)) {
3344 * We can't open a file with DELETE access if any of the
3345 * streams is open without FILE_SHARE_DELETE
3347 status = open_streams_for_delete(conn, smb_fname->base_name);
3349 if (!NT_STATUS_IS_OK(status)) {
3354 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3355 !security_token_has_privilege(get_current_nttok(conn),
3356 SEC_PRIV_SECURITY)) {
3357 DEBUG(10, ("create_file_unixpath: open on %s "
3358 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3359 smb_fname_str_dbg(smb_fname)));
3360 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3364 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3365 && is_ntfs_stream_smb_fname(smb_fname)
3366 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3367 uint32 base_create_disposition;
3368 struct smb_filename *smb_fname_base = NULL;
3370 if (create_options & FILE_DIRECTORY_FILE) {
3371 status = NT_STATUS_NOT_A_DIRECTORY;
3375 switch (create_disposition) {
3377 base_create_disposition = FILE_OPEN;
3380 base_create_disposition = FILE_OPEN_IF;
3384 /* Create an smb_filename with stream_name == NULL. */
3385 status = create_synthetic_smb_fname(talloc_tos(),
3386 smb_fname->base_name,
3389 if (!NT_STATUS_IS_OK(status)) {
3393 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3394 DEBUG(10, ("Unable to stat stream: %s\n",
3395 smb_fname_str_dbg(smb_fname_base)));
3398 /* Open the base file. */
3399 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3402 | FILE_SHARE_DELETE,
3403 base_create_disposition,
3404 0, 0, 0, 0, 0, NULL, NULL,
3406 TALLOC_FREE(smb_fname_base);
3408 if (!NT_STATUS_IS_OK(status)) {
3409 DEBUG(10, ("create_file_unixpath for base %s failed: "
3410 "%s\n", smb_fname->base_name,
3411 nt_errstr(status)));
3414 /* we don't need to low level fd */
3419 * If it's a request for a directory open, deal with it separately.
3422 if (create_options & FILE_DIRECTORY_FILE) {
3424 if (create_options & FILE_NON_DIRECTORY_FILE) {
3425 status = NT_STATUS_INVALID_PARAMETER;
3429 /* Can't open a temp directory. IFS kit test. */
3430 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3431 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3432 status = NT_STATUS_INVALID_PARAMETER;
3437 * We will get a create directory here if the Win32
3438 * app specified a security descriptor in the
3439 * CreateDirectory() call.
3443 status = open_directory(
3444 conn, req, smb_fname, access_mask, share_access,
3445 create_disposition, create_options, file_attributes,
3450 * Ordinary file case.
3453 status = file_new(req, conn, &fsp);
3454 if(!NT_STATUS_IS_OK(status)) {
3458 status = fsp_set_smb_fname(fsp, smb_fname);
3459 if (!NT_STATUS_IS_OK(status)) {
3464 * We're opening the stream element of a base_fsp
3465 * we already opened. Set up the base_fsp pointer.
3468 fsp->base_fsp = base_fsp;
3471 status = open_file_ntcreate(conn,
3483 if(!NT_STATUS_IS_OK(status)) {
3484 file_free(req, fsp);
3488 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3490 /* A stream open never opens a directory */
3493 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3498 * Fail the open if it was explicitly a non-directory
3502 if (create_options & FILE_NON_DIRECTORY_FILE) {
3503 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3508 status = open_directory(
3509 conn, req, smb_fname, access_mask,
3510 share_access, create_disposition,
3511 create_options, file_attributes,
3516 if (!NT_STATUS_IS_OK(status)) {
3520 fsp->base_fsp = base_fsp;
3522 if ((ea_list != NULL) &&
3523 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3524 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3525 if (!NT_STATUS_IS_OK(status)) {
3530 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3531 status = NT_STATUS_ACCESS_DENIED;
3535 /* Save the requested allocation size. */
3536 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3538 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3539 fsp->initial_allocation_size = smb_roundup(
3540 fsp->conn, allocation_size);
3541 if (fsp->is_directory) {
3542 /* Can't set allocation size on a directory. */
3543 status = NT_STATUS_ACCESS_DENIED;
3546 if (vfs_allocate_file_space(
3547 fsp, fsp->initial_allocation_size) == -1) {
3548 status = NT_STATUS_DISK_FULL;
3552 fsp->initial_allocation_size = smb_roundup(
3553 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3557 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3558 fsp->base_fsp == NULL) {
3561 * According to the MS documentation, the only time the security
3562 * descriptor is applied to the opened file is iff we *created* the
3563 * file; an existing file stays the same.
3565 * Also, it seems (from observation) that you can open the file with
3566 * any access mask but you can still write the sd. We need to override
3567 * the granted access before we call set_sd
3568 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3571 uint32_t sec_info_sent;
3572 uint32_t saved_access_mask = fsp->access_mask;
3574 sec_info_sent = get_sec_info(sd);
3576 fsp->access_mask = FILE_GENERIC_ALL;
3578 /* Convert all the generic bits. */
3579 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3580 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3582 if (sec_info_sent & (SECINFO_OWNER|
3586 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3589 fsp->access_mask = saved_access_mask;
3591 if (!NT_STATUS_IS_OK(status)) {
3594 } else if (lp_inherit_acls(SNUM(conn))) {
3595 /* Inherit from parent. Errors here are not fatal. */
3596 status = inherit_new_acl(fsp);
3597 if (!NT_STATUS_IS_OK(status)) {
3598 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3600 nt_errstr(status) ));
3605 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3608 if (pinfo != NULL) {
3612 smb_fname->st = fsp->fsp_name->st;
3614 return NT_STATUS_OK;
3617 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3620 if (base_fsp && fsp->base_fsp == base_fsp) {
3622 * The close_file below will close
3627 close_file(req, fsp, ERROR_CLOSE);
3630 if (base_fsp != NULL) {
3631 close_file(req, base_fsp, ERROR_CLOSE);
3638 * Calculate the full path name given a relative fid.
3640 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3641 struct smb_request *req,
3642 uint16_t root_dir_fid,
3643 const struct smb_filename *smb_fname,
3644 struct smb_filename **smb_fname_out)
3646 files_struct *dir_fsp;
3647 char *parent_fname = NULL;
3648 char *new_base_name = NULL;
3651 if (root_dir_fid == 0 || !smb_fname) {
3652 status = NT_STATUS_INTERNAL_ERROR;
3656 dir_fsp = file_fsp(req, root_dir_fid);
3658 if (dir_fsp == NULL) {
3659 status = NT_STATUS_INVALID_HANDLE;
3663 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3664 status = NT_STATUS_INVALID_HANDLE;
3668 if (!dir_fsp->is_directory) {
3671 * Check to see if this is a mac fork of some kind.
3674 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3675 is_ntfs_stream_smb_fname(smb_fname)) {
3676 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3681 we need to handle the case when we get a
3682 relative open relative to a file and the
3683 pathname is blank - this is a reopen!
3684 (hint from demyn plantenberg)
3687 status = NT_STATUS_INVALID_HANDLE;
3691 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3693 * We're at the toplevel dir, the final file name
3694 * must not contain ./, as this is filtered out
3695 * normally by srvstr_get_path and unix_convert
3696 * explicitly rejects paths containing ./.
3698 parent_fname = talloc_strdup(talloc_tos(), "");
3699 if (parent_fname == NULL) {
3700 status = NT_STATUS_NO_MEMORY;
3704 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3707 * Copy in the base directory name.
3710 parent_fname = talloc_array(talloc_tos(), char,
3712 if (parent_fname == NULL) {
3713 status = NT_STATUS_NO_MEMORY;
3716 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3720 * Ensure it ends in a '/'.
3721 * We used TALLOC_SIZE +2 to add space for the '/'.
3725 && (parent_fname[dir_name_len-1] != '\\')
3726 && (parent_fname[dir_name_len-1] != '/')) {
3727 parent_fname[dir_name_len] = '/';
3728 parent_fname[dir_name_len+1] = '\0';
3732 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3733 smb_fname->base_name);
3734 if (new_base_name == NULL) {
3735 status = NT_STATUS_NO_MEMORY;
3739 status = filename_convert(req,
3741 req->flags2 & FLAGS2_DFS_PATHNAMES,
3746 if (!NT_STATUS_IS_OK(status)) {
3751 TALLOC_FREE(parent_fname);
3752 TALLOC_FREE(new_base_name);
3756 NTSTATUS create_file_default(connection_struct *conn,
3757 struct smb_request *req,
3758 uint16_t root_dir_fid,
3759 struct smb_filename *smb_fname,
3760 uint32_t access_mask,
3761 uint32_t share_access,
3762 uint32_t create_disposition,
3763 uint32_t create_options,
3764 uint32_t file_attributes,
3765 uint32_t oplock_request,
3766 uint64_t allocation_size,
3767 uint32_t private_flags,
3768 struct security_descriptor *sd,
3769 struct ea_list *ea_list,
3770 files_struct **result,
3773 int info = FILE_WAS_OPENED;
3774 files_struct *fsp = NULL;
3776 bool stream_name = false;
3778 DEBUG(10,("create_file: access_mask = 0x%x "
3779 "file_attributes = 0x%x, share_access = 0x%x, "
3780 "create_disposition = 0x%x create_options = 0x%x "
3781 "oplock_request = 0x%x "
3782 "private_flags = 0x%x "
3783 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3785 (unsigned int)access_mask,
3786 (unsigned int)file_attributes,
3787 (unsigned int)share_access,
3788 (unsigned int)create_disposition,
3789 (unsigned int)create_options,
3790 (unsigned int)oplock_request,
3791 (unsigned int)private_flags,
3792 (unsigned int)root_dir_fid,
3793 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3796 * Calculate the filename from the root_dir_if if necessary.
3799 if (root_dir_fid != 0) {
3800 struct smb_filename *smb_fname_out = NULL;
3801 status = get_relative_fid_filename(conn, req, root_dir_fid,
3802 smb_fname, &smb_fname_out);
3803 if (!NT_STATUS_IS_OK(status)) {
3806 smb_fname = smb_fname_out;
3810 * Check to see if this is a mac fork of some kind.
3813 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3815 enum FAKE_FILE_TYPE fake_file_type;
3817 fake_file_type = is_fake_file(smb_fname);
3819 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3822 * Here we go! support for changing the disk quotas
3825 * We need to fake up to open this MAGIC QUOTA file
3826 * and return a valid FID.
3828 * w2k close this file directly after openening xp
3829 * also tries a QUERY_FILE_INFO on the file and then
3832 status = open_fake_file(req, conn, req->vuid,
3833 fake_file_type, smb_fname,
3835 if (!NT_STATUS_IS_OK(status)) {
3839 ZERO_STRUCT(smb_fname->st);
3843 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3844 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3849 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
3851 smb_fname->stream_name = NULL;
3852 /* We have to handle this error here. */
3853 if (create_options & FILE_DIRECTORY_FILE) {
3854 status = NT_STATUS_NOT_A_DIRECTORY;
3857 if (lp_posix_pathnames()) {
3858 ret = SMB_VFS_LSTAT(conn, smb_fname);
3860 ret = SMB_VFS_STAT(conn, smb_fname);
3863 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3864 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3869 status = create_file_unixpath(
3870 conn, req, smb_fname, access_mask, share_access,
3871 create_disposition, create_options, file_attributes,
3872 oplock_request, allocation_size, private_flags,
3876 if (!NT_STATUS_IS_OK(status)) {
3881 DEBUG(10, ("create_file: info=%d\n", info));
3884 if (pinfo != NULL) {
3887 return NT_STATUS_OK;
3890 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3893 close_file(req, fsp, ERROR_CLOSE);