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_map_readonly(SNUM(conn)) ||
168 lp_map_archive(SNUM(conn)) ||
169 lp_map_hidden(SNUM(conn)) ||
170 lp_map_system(SNUM(conn)))) {
171 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
173 DEBUG(10,("smbd_check_access_rights: "
175 "FILE_WRITE_ATTRIBUTES "
177 smb_fname_str_dbg(smb_fname)));
180 if (parent_override_delete(conn,
184 /* Were we trying to do an open
185 * for delete and didn't get DELETE
186 * access (only) ? Check if the
187 * directory allows DELETE_CHILD.
189 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
192 rejected_mask &= ~DELETE_ACCESS;
194 DEBUG(10,("smbd_check_access_rights: "
198 smb_fname_str_dbg(smb_fname)));
201 if (rejected_mask != 0) {
202 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,
211 char **pp_parent_dir)
214 char *parent_dir = NULL;
215 struct security_descriptor *parent_sd = NULL;
216 uint32_t access_granted = 0;
218 if (!parent_dirname(talloc_tos(),
219 smb_fname->base_name,
222 return NT_STATUS_NO_MEMORY;
226 *pp_parent_dir = parent_dir;
229 if (get_current_uid(conn) == (uid_t)0) {
230 /* I'm sorry sir, I didn't know you were root... */
231 DEBUG(10,("check_parent_access: root override "
232 "on %s. Granting 0x%x\n",
233 smb_fname_str_dbg(smb_fname),
234 (unsigned int)access_mask ));
238 status = SMB_VFS_GET_NT_ACL(conn,
243 if (!NT_STATUS_IS_OK(status)) {
244 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
245 "%s with error %s\n",
252 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
253 * owner WRITE_DAC and READ_CONTROL.
255 status = se_access_check(parent_sd,
256 get_current_nttok(conn),
257 (access_mask & ~FILE_READ_ATTRIBUTES),
259 if(!NT_STATUS_IS_OK(status)) {
260 DEBUG(5,("check_parent_access: access check "
261 "on directory %s for "
262 "path %s for mask 0x%x returned (0x%x) %s\n",
264 smb_fname->base_name,
267 nt_errstr(status) ));
274 /****************************************************************************
275 fd support routines - attempt to do a dos_open.
276 ****************************************************************************/
278 static NTSTATUS fd_open(struct connection_struct *conn,
283 struct smb_filename *smb_fname = fsp->fsp_name;
284 NTSTATUS status = NT_STATUS_OK;
288 * Never follow symlinks on a POSIX client. The
289 * client should be doing this.
292 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
297 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
298 if (fsp->fh->fd == -1) {
299 status = map_nt_error_from_unix(errno);
300 if (errno == EMFILE) {
301 static time_t last_warned = 0L;
303 if (time((time_t *) NULL) > last_warned) {
304 DEBUG(0,("Too many open files, unable "
305 "to open more! smbd's max "
307 lp_max_open_files()));
308 last_warned = time((time_t *) NULL);
314 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
315 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
316 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
321 /****************************************************************************
322 Close the file associated with a fsp.
323 ****************************************************************************/
325 NTSTATUS fd_close(files_struct *fsp)
332 if (fsp->fh->fd == -1) {
333 return NT_STATUS_OK; /* What we used to call a stat open. */
335 if (fsp->fh->ref_count > 1) {
336 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
339 ret = SMB_VFS_CLOSE(fsp);
342 return map_nt_error_from_unix(errno);
347 /****************************************************************************
348 Change the ownership of a file to that of the parent directory.
349 Do this by fd if possible.
350 ****************************************************************************/
352 void change_file_owner_to_parent(connection_struct *conn,
353 const char *inherit_from_dir,
356 struct smb_filename *smb_fname_parent = NULL;
360 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
361 NULL, NULL, &smb_fname_parent);
362 if (!NT_STATUS_IS_OK(status)) {
366 ret = SMB_VFS_STAT(conn, smb_fname_parent);
368 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
369 "directory %s. Error was %s\n",
370 smb_fname_str_dbg(smb_fname_parent),
372 TALLOC_FREE(smb_fname_parent);
376 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
377 /* Already this uid - no need to change. */
378 DEBUG(10,("change_file_owner_to_parent: file %s "
379 "is already owned by uid %d\n",
381 (int)fsp->fsp_name->st.st_ex_uid ));
382 TALLOC_FREE(smb_fname_parent);
387 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
390 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
391 "file %s to parent directory uid %u. Error "
392 "was %s\n", fsp_str_dbg(fsp),
393 (unsigned int)smb_fname_parent->st.st_ex_uid,
396 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
397 "parent directory uid %u.\n", fsp_str_dbg(fsp),
398 (unsigned int)smb_fname_parent->st.st_ex_uid));
399 /* Ensure the uid entry is updated. */
400 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
403 TALLOC_FREE(smb_fname_parent);
406 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
407 const char *inherit_from_dir,
409 SMB_STRUCT_STAT *psbuf)
411 struct smb_filename *smb_fname_parent = NULL;
412 struct smb_filename *smb_fname_cwd = NULL;
413 char *saved_dir = NULL;
414 TALLOC_CTX *ctx = talloc_tos();
415 NTSTATUS status = NT_STATUS_OK;
418 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
420 if (!NT_STATUS_IS_OK(status)) {
424 ret = SMB_VFS_STAT(conn, smb_fname_parent);
426 status = map_nt_error_from_unix(errno);
427 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
428 "directory %s. Error was %s\n",
429 smb_fname_str_dbg(smb_fname_parent),
434 /* We've already done an lstat into psbuf, and we know it's a
435 directory. If we can cd into the directory and the dev/ino
436 are the same then we can safely chown without races as
437 we're locking the directory in place by being in it. This
438 should work on any UNIX (thanks tridge :-). JRA.
441 saved_dir = vfs_GetWd(ctx,conn);
443 status = map_nt_error_from_unix(errno);
444 DEBUG(0,("change_dir_owner_to_parent: failed to get "
445 "current working directory. Error was %s\n",
450 /* Chdir into the new path. */
451 if (vfs_ChDir(conn, fname) == -1) {
452 status = map_nt_error_from_unix(errno);
453 DEBUG(0,("change_dir_owner_to_parent: failed to change "
454 "current working directory to %s. Error "
455 "was %s\n", fname, strerror(errno) ));
459 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
461 if (!NT_STATUS_IS_OK(status)) {
465 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
467 status = map_nt_error_from_unix(errno);
468 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
469 "directory '.' (%s) Error was %s\n",
470 fname, strerror(errno)));
474 /* Ensure we're pointing at the same place. */
475 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
476 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
477 DEBUG(0,("change_dir_owner_to_parent: "
478 "device/inode on directory %s changed. "
479 "Refusing to chown !\n", fname ));
480 status = NT_STATUS_ACCESS_DENIED;
484 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
485 /* Already this uid - no need to change. */
486 DEBUG(10,("change_dir_owner_to_parent: directory %s "
487 "is already owned by uid %d\n",
489 (int)smb_fname_cwd->st.st_ex_uid ));
490 status = NT_STATUS_OK;
495 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
499 status = map_nt_error_from_unix(errno);
500 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
501 "directory %s to parent directory uid %u. "
502 "Error was %s\n", fname,
503 (unsigned int)smb_fname_parent->st.st_ex_uid,
506 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
507 "directory %s to parent directory uid %u.\n",
508 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
509 /* Ensure the uid entry is updated. */
510 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
514 vfs_ChDir(conn,saved_dir);
516 TALLOC_FREE(smb_fname_parent);
517 TALLOC_FREE(smb_fname_cwd);
521 /****************************************************************************
523 ****************************************************************************/
525 static NTSTATUS open_file(files_struct *fsp,
526 connection_struct *conn,
527 struct smb_request *req,
528 const char *parent_dir,
531 uint32 access_mask, /* client requested access mask. */
532 uint32 open_access_mask) /* what we're actually using in the open. */
534 struct smb_filename *smb_fname = fsp->fsp_name;
535 NTSTATUS status = NT_STATUS_OK;
536 int accmode = (flags & O_ACCMODE);
537 int local_flags = flags;
538 bool file_existed = VALID_STAT(fsp->fsp_name->st);
539 bool file_created = false;
544 /* Check permissions */
547 * This code was changed after seeing a client open request
548 * containing the open mode of (DENY_WRITE/read-only) with
549 * the 'create if not exist' bit set. The previous code
550 * would fail to open the file read only on a read-only share
551 * as it was checking the flags parameter directly against O_RDONLY,
552 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
556 if (!CAN_WRITE(conn)) {
557 /* It's a read-only share - fail if we wanted to write. */
558 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
559 DEBUG(3,("Permission denied opening %s\n",
560 smb_fname_str_dbg(smb_fname)));
561 return NT_STATUS_ACCESS_DENIED;
562 } else if(flags & O_CREAT) {
563 /* We don't want to write - but we must make sure that
564 O_CREAT doesn't create the file if we have write
565 access into the directory.
567 flags &= ~(O_CREAT|O_EXCL);
568 local_flags &= ~(O_CREAT|O_EXCL);
573 * This little piece of insanity is inspired by the
574 * fact that an NT client can open a file for O_RDONLY,
575 * but set the create disposition to FILE_EXISTS_TRUNCATE.
576 * If the client *can* write to the file, then it expects to
577 * truncate the file, even though it is opening for readonly.
578 * Quicken uses this stupid trick in backup file creation...
579 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
580 * for helping track this one down. It didn't bite us in 2.0.x
581 * as we always opened files read-write in that release. JRA.
584 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
585 DEBUG(10,("open_file: truncate requested on read-only open "
586 "for file %s\n", smb_fname_str_dbg(smb_fname)));
587 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
590 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
591 (!file_existed && (local_flags & O_CREAT)) ||
592 ((local_flags & O_TRUNC) == O_TRUNC) ) {
596 * We can't actually truncate here as the file may be locked.
597 * open_file_ntcreate will take care of the truncate later. JRA.
600 local_flags &= ~O_TRUNC;
602 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
604 * We would block on opening a FIFO with no one else on the
605 * other end. Do what we used to do and add O_NONBLOCK to the
609 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
610 local_flags |= O_NONBLOCK;
614 /* Don't create files with Microsoft wildcard characters. */
617 * wildcard characters are allowed in stream names
618 * only test the basefilename
620 wild = fsp->base_fsp->fsp_name->base_name;
622 wild = smb_fname->base_name;
624 if ((local_flags & O_CREAT) && !file_existed &&
626 return NT_STATUS_OBJECT_NAME_INVALID;
629 /* Can we access this file ? */
630 if (!fsp->base_fsp) {
631 /* Only do this check on non-stream open. */
633 status = smbd_check_access_rights(conn,
636 } else if (local_flags & O_CREAT){
637 status = check_parent_access(conn,
642 /* File didn't exist and no O_CREAT. */
643 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
645 if (!NT_STATUS_IS_OK(status)) {
646 DEBUG(10,("open_file: "
650 "smbd_check_access_rights" :
651 "check_parent_access",
652 smb_fname_str_dbg(smb_fname),
653 nt_errstr(status) ));
658 /* Actually do the open */
659 status = fd_open(conn, fsp, local_flags, unx_mode);
660 if (!NT_STATUS_IS_OK(status)) {
661 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
662 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
663 nt_errstr(status),local_flags,flags));
667 if ((local_flags & O_CREAT) && !file_existed) {
672 fsp->fh->fd = -1; /* What we used to call a stat open. */
674 /* File must exist for a stat open. */
675 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
678 status = smbd_check_access_rights(conn,
682 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
684 S_ISLNK(smb_fname->st.st_ex_mode)) {
685 /* This is a POSIX stat open for delete
686 * or rename on a symlink that points
688 DEBUG(10,("open_file: allowing POSIX "
689 "open on bad symlink %s\n",
690 smb_fname_str_dbg(smb_fname)));
691 status = NT_STATUS_OK;
694 if (!NT_STATUS_IS_OK(status)) {
695 DEBUG(10,("open_file: "
696 "smbd_check_access_rights on file "
698 smb_fname_str_dbg(smb_fname),
699 nt_errstr(status) ));
707 if (fsp->fh->fd == -1) {
708 ret = SMB_VFS_STAT(conn, smb_fname);
710 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
711 /* If we have an fd, this stat should succeed. */
713 DEBUG(0,("Error doing fstat on open file %s "
715 smb_fname_str_dbg(smb_fname),
720 /* For a non-io open, this stat failing means file not found. JRA */
722 status = map_nt_error_from_unix(errno);
728 bool need_re_stat = false;
729 /* Do all inheritance work after we've
730 done a successful stat call and filled
731 in the stat struct in fsp->fsp_name. */
733 /* Inherit the ACL if required */
734 if (lp_inherit_perms(SNUM(conn))) {
735 inherit_access_posix_acl(conn, parent_dir,
736 smb_fname->base_name,
741 /* Change the owner if required. */
742 if (lp_inherit_owner(SNUM(conn))) {
743 change_file_owner_to_parent(conn, parent_dir,
749 if (fsp->fh->fd == -1) {
750 ret = SMB_VFS_STAT(conn, smb_fname);
752 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
753 /* If we have an fd, this stat should succeed. */
755 DEBUG(0,("Error doing fstat on open file %s "
757 smb_fname_str_dbg(smb_fname),
763 notify_fname(conn, NOTIFY_ACTION_ADDED,
764 FILE_NOTIFY_CHANGE_FILE_NAME,
765 smb_fname->base_name);
770 * POSIX allows read-only opens of directories. We don't
771 * want to do this (we use a different code path for this)
772 * so catch a directory open and return an EISDIR. JRA.
775 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
778 return NT_STATUS_FILE_IS_A_DIRECTORY;
781 fsp->mode = smb_fname->st.st_ex_mode;
782 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
783 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
784 fsp->file_pid = req ? req->smbpid : 0;
785 fsp->can_lock = True;
786 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
787 if (!CAN_WRITE(conn)) {
788 fsp->can_write = False;
790 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
793 fsp->print_file = NULL;
794 fsp->modified = False;
795 fsp->sent_oplock_break = NO_BREAK_SENT;
796 fsp->is_directory = False;
797 if (conn->aio_write_behind_list &&
798 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
799 conn->case_sensitive)) {
800 fsp->aio_write_behind = True;
803 fsp->wcp = NULL; /* Write cache pointer. */
805 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
806 conn->session_info->unix_info->unix_name,
807 smb_fname_str_dbg(smb_fname),
808 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
809 conn->num_files_open));
815 /****************************************************************************
816 Check if we can open a file with a share mode.
817 Returns True if conflict, False if not.
818 ****************************************************************************/
820 static bool share_conflict(struct share_mode_entry *entry,
824 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
825 "entry->share_access = 0x%x, "
826 "entry->private_options = 0x%x\n",
827 (unsigned int)entry->access_mask,
828 (unsigned int)entry->share_access,
829 (unsigned int)entry->private_options));
831 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
832 (unsigned int)access_mask, (unsigned int)share_access));
834 if ((entry->access_mask & (FILE_WRITE_DATA|
838 DELETE_ACCESS)) == 0) {
839 DEBUG(10,("share_conflict: No conflict due to "
840 "entry->access_mask = 0x%x\n",
841 (unsigned int)entry->access_mask ));
845 if ((access_mask & (FILE_WRITE_DATA|
849 DELETE_ACCESS)) == 0) {
850 DEBUG(10,("share_conflict: No conflict due to "
851 "access_mask = 0x%x\n",
852 (unsigned int)access_mask ));
856 #if 1 /* JRA TEST - Superdebug. */
857 #define CHECK_MASK(num, am, right, sa, share) \
858 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
859 (unsigned int)(num), (unsigned int)(am), \
860 (unsigned int)(right), (unsigned int)(am)&(right) )); \
861 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
862 (unsigned int)(num), (unsigned int)(sa), \
863 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
864 if (((am) & (right)) && !((sa) & (share))) { \
865 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
866 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
867 (unsigned int)(share) )); \
871 #define CHECK_MASK(num, am, right, sa, share) \
872 if (((am) & (right)) && !((sa) & (share))) { \
873 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
874 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
875 (unsigned int)(share) )); \
880 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
881 share_access, FILE_SHARE_WRITE);
882 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
883 entry->share_access, FILE_SHARE_WRITE);
885 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
886 share_access, FILE_SHARE_READ);
887 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
888 entry->share_access, FILE_SHARE_READ);
890 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
891 share_access, FILE_SHARE_DELETE);
892 CHECK_MASK(6, access_mask, DELETE_ACCESS,
893 entry->share_access, FILE_SHARE_DELETE);
895 DEBUG(10,("share_conflict: No conflict.\n"));
899 #if defined(DEVELOPER)
900 static void validate_my_share_entries(struct smbd_server_connection *sconn,
902 struct share_mode_entry *share_entry)
906 if (!procid_is_me(&share_entry->pid)) {
910 if (is_deferred_open_entry(share_entry) &&
911 !open_was_deferred(sconn, share_entry->op_mid)) {
912 char *str = talloc_asprintf(talloc_tos(),
913 "Got a deferred entry without a request: "
915 share_mode_str(talloc_tos(), num, share_entry));
919 if (!is_valid_share_mode_entry(share_entry)) {
923 fsp = file_find_dif(sconn, share_entry->id,
924 share_entry->share_file_id);
926 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
927 share_mode_str(talloc_tos(), num, share_entry) ));
928 smb_panic("validate_my_share_entries: Cannot match a "
929 "share entry with an open file\n");
932 if (is_deferred_open_entry(share_entry)) {
936 if ((share_entry->op_type == NO_OPLOCK) &&
937 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
938 /* Someone has already written to it, but I haven't yet
943 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
952 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
953 share_mode_str(talloc_tos(), num, share_entry) ));
954 str = talloc_asprintf(talloc_tos(),
955 "validate_my_share_entries: "
956 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
957 fsp->fsp_name->base_name,
958 (unsigned int)fsp->oplock_type,
959 (unsigned int)share_entry->op_type );
965 bool is_stat_open(uint32 access_mask)
967 return (access_mask &&
968 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
969 FILE_WRITE_ATTRIBUTES))==0) &&
970 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
971 FILE_WRITE_ATTRIBUTES)) != 0));
974 /****************************************************************************
975 Deal with share modes
976 Invarient: Share mode must be locked on entry and exit.
977 Returns -1 on error, or number of share modes on success (may be zero).
978 ****************************************************************************/
980 static NTSTATUS open_mode_check(connection_struct *conn,
981 struct share_mode_lock *lck,
985 uint32 create_options,
990 if(lck->num_share_modes == 0) {
994 *file_existed = True;
996 /* A delete on close prohibits everything */
998 if (is_delete_on_close_set(lck, name_hash)) {
999 return NT_STATUS_DELETE_PENDING;
1002 if (is_stat_open(access_mask)) {
1003 /* Stat open that doesn't trigger oplock breaks or share mode
1004 * checks... ! JRA. */
1005 return NT_STATUS_OK;
1009 * Check if the share modes will give us access.
1012 #if defined(DEVELOPER)
1013 for(i = 0; i < lck->num_share_modes; i++) {
1014 validate_my_share_entries(conn->sconn, i,
1015 &lck->share_modes[i]);
1019 if (!lp_share_modes(SNUM(conn))) {
1020 return NT_STATUS_OK;
1023 /* Now we check the share modes, after any oplock breaks. */
1024 for(i = 0; i < lck->num_share_modes; i++) {
1026 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1030 /* someone else has a share lock on it, check to see if we can
1032 if (share_conflict(&lck->share_modes[i],
1033 access_mask, share_access)) {
1034 return NT_STATUS_SHARING_VIOLATION;
1038 return NT_STATUS_OK;
1041 static bool is_delete_request(files_struct *fsp) {
1042 return ((fsp->access_mask == DELETE_ACCESS) &&
1043 (fsp->oplock_type == NO_OPLOCK));
1047 * Send a break message to the oplock holder and delay the open for
1051 static NTSTATUS send_break_message(files_struct *fsp,
1052 struct share_mode_entry *exclusive,
1057 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1059 DEBUG(10, ("Sending break request to PID %s\n",
1060 procid_str_static(&exclusive->pid)));
1061 exclusive->op_mid = mid;
1063 /* Create the message. */
1064 share_mode_entry_to_message(msg, exclusive);
1066 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1067 don't want this set in the share mode struct pointed to by lck. */
1069 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1070 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1071 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1074 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1075 MSG_SMB_BREAK_REQUEST,
1077 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1078 if (!NT_STATUS_IS_OK(status)) {
1079 DEBUG(3, ("Could not send oplock break message: %s\n",
1080 nt_errstr(status)));
1087 * Return share_mode_entry pointers for :
1088 * 1). Batch oplock entry.
1089 * 2). Batch or exclusive oplock entry (may be identical to #1).
1090 * bool have_level2_oplock
1091 * bool have_no_oplock.
1092 * Do internal consistency checks on the share mode for a file.
1095 static void find_oplock_types(files_struct *fsp,
1097 const struct share_mode_lock *lck,
1098 struct share_mode_entry **pp_batch,
1099 struct share_mode_entry **pp_ex_or_batch,
1101 bool *got_no_oplock)
1106 *pp_ex_or_batch = NULL;
1107 *got_level2 = false;
1108 *got_no_oplock = false;
1110 /* Ignore stat or internal opens, as is done in
1111 delay_for_batch_oplocks() and
1112 delay_for_exclusive_oplocks().
1114 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1118 for (i=0; i<lck->num_share_modes; i++) {
1119 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1123 if (lck->share_modes[i].op_type == NO_OPLOCK &&
1124 is_stat_open(lck->share_modes[i].access_mask)) {
1125 /* We ignore stat opens in the table - they
1126 always have NO_OPLOCK and never get or
1127 cause breaks. JRA. */
1131 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1132 /* batch - can only be one. */
1133 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1134 smb_panic("Bad batch oplock entry.");
1136 *pp_batch = &lck->share_modes[i];
1139 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1140 /* Exclusive or batch - can only be one. */
1141 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1142 smb_panic("Bad exclusive or batch oplock entry.");
1144 *pp_ex_or_batch = &lck->share_modes[i];
1147 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1148 if (*pp_batch || *pp_ex_or_batch) {
1149 smb_panic("Bad levelII oplock entry.");
1154 if (lck->share_modes[i].op_type == NO_OPLOCK) {
1155 if (*pp_batch || *pp_ex_or_batch) {
1156 smb_panic("Bad no oplock entry.");
1158 *got_no_oplock = true;
1163 static bool delay_for_batch_oplocks(files_struct *fsp,
1166 struct share_mode_entry *batch_entry)
1168 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1171 if (batch_entry == NULL) {
1175 /* Found a batch oplock */
1176 send_break_message(fsp, batch_entry, mid, oplock_request);
1180 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1183 struct share_mode_entry *ex_entry)
1187 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1190 if (ex_entry == NULL) {
1194 /* Found an exclusive or batch oplock */
1196 delay_it = is_delete_request(fsp) ?
1197 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1203 send_break_message(fsp, ex_entry, mid, oplock_request);
1207 static void grant_fsp_oplock_type(files_struct *fsp,
1208 const struct byte_range_lock *br_lck,
1210 bool got_level2_oplock,
1211 bool got_a_none_oplock)
1213 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1214 lp_level2_oplocks(SNUM(fsp->conn));
1216 /* Start by granting what the client asked for,
1217 but ensure no SAMBA_PRIVATE bits can be set. */
1218 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1220 if (oplock_request & INTERNAL_OPEN_ONLY) {
1221 /* No oplocks on internal open. */
1222 fsp->oplock_type = NO_OPLOCK;
1223 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1224 fsp->oplock_type, fsp_str_dbg(fsp)));
1226 } else if (br_lck && br_lck->num_locks > 0) {
1227 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1229 fsp->oplock_type = NO_OPLOCK;
1232 if (is_stat_open(fsp->access_mask)) {
1233 /* Leave the value already set. */
1234 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1235 fsp->oplock_type, fsp_str_dbg(fsp)));
1240 * Match what was requested (fsp->oplock_type) with
1241 * what was found in the existing share modes.
1244 if (got_a_none_oplock) {
1245 fsp->oplock_type = NO_OPLOCK;
1246 } else if (got_level2_oplock) {
1247 if (fsp->oplock_type == NO_OPLOCK ||
1248 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1249 /* Store a level2 oplock, but don't tell the client */
1250 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1252 fsp->oplock_type = LEVEL_II_OPLOCK;
1255 /* All share_mode_entries are placeholders or deferred.
1256 * Silently upgrade to fake levelII if the client didn't
1257 * ask for an oplock. */
1258 if (fsp->oplock_type == NO_OPLOCK) {
1259 /* Store a level2 oplock, but don't tell the client */
1260 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1265 * Don't grant level2 to clients that don't want them
1266 * or if we've turned them off.
1268 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1269 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1272 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1273 fsp->oplock_type, fsp_str_dbg(fsp)));
1276 bool request_timed_out(struct timeval request_time,
1277 struct timeval timeout)
1279 struct timeval now, end_time;
1281 end_time = timeval_sum(&request_time, &timeout);
1282 return (timeval_compare(&end_time, &now) < 0);
1285 /****************************************************************************
1286 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1287 ****************************************************************************/
1289 static void defer_open(struct share_mode_lock *lck,
1290 struct timeval request_time,
1291 struct timeval timeout,
1292 struct smb_request *req,
1293 struct deferred_open_record *state)
1297 /* Paranoia check */
1299 for (i=0; i<lck->num_share_modes; i++) {
1300 struct share_mode_entry *e = &lck->share_modes[i];
1302 if (is_deferred_open_entry(e) &&
1303 procid_is_me(&e->pid) &&
1304 (e->op_mid == req->mid)) {
1305 DEBUG(0, ("Trying to defer an already deferred "
1306 "request: mid=%llu, exiting\n",
1307 (unsigned long long)req->mid));
1308 exit_server("attempt to defer a deferred request");
1312 /* End paranoia check */
1314 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1315 "open entry for mid %llu\n",
1316 (unsigned int)request_time.tv_sec,
1317 (unsigned int)request_time.tv_usec,
1318 (unsigned long long)req->mid));
1320 if (!push_deferred_open_message_smb(req, request_time, timeout,
1321 state->id, (char *)state, sizeof(*state))) {
1322 exit_server("push_deferred_open_message_smb failed");
1324 add_deferred_open(lck, req->mid, request_time,
1325 messaging_server_id(req->sconn->msg_ctx), state->id);
1329 /****************************************************************************
1330 On overwrite open ensure that the attributes match.
1331 ****************************************************************************/
1333 bool open_match_attributes(connection_struct *conn,
1334 uint32 old_dos_attr,
1335 uint32 new_dos_attr,
1336 mode_t existing_unx_mode,
1337 mode_t new_unx_mode,
1338 mode_t *returned_unx_mode)
1340 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1342 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1343 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1345 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1346 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1347 *returned_unx_mode = new_unx_mode;
1349 *returned_unx_mode = (mode_t)0;
1352 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1353 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1354 "returned_unx_mode = 0%o\n",
1355 (unsigned int)old_dos_attr,
1356 (unsigned int)existing_unx_mode,
1357 (unsigned int)new_dos_attr,
1358 (unsigned int)*returned_unx_mode ));
1360 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1361 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1362 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1363 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1367 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1368 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1369 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1376 /****************************************************************************
1377 Special FCB or DOS processing in the case of a sharing violation.
1378 Try and find a duplicated file handle.
1379 ****************************************************************************/
1381 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1382 connection_struct *conn,
1383 files_struct *fsp_to_dup_into,
1384 const struct smb_filename *smb_fname,
1389 uint32 share_access,
1390 uint32 create_options)
1394 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1395 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1397 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1398 fsp = file_find_di_next(fsp)) {
1400 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1401 "vuid = %u, file_pid = %u, private_options = 0x%x "
1402 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1403 fsp->fh->fd, (unsigned int)fsp->vuid,
1404 (unsigned int)fsp->file_pid,
1405 (unsigned int)fsp->fh->private_options,
1406 (unsigned int)fsp->access_mask ));
1408 if (fsp->fh->fd != -1 &&
1409 fsp->vuid == vuid &&
1410 fsp->file_pid == file_pid &&
1411 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1412 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1413 (fsp->access_mask & FILE_WRITE_DATA) &&
1414 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1415 strequal(fsp->fsp_name->stream_name,
1416 smb_fname->stream_name)) {
1417 DEBUG(10,("fcb_or_dos_open: file match\n"));
1423 return NT_STATUS_NOT_FOUND;
1426 /* quite an insane set of semantics ... */
1427 if (is_executable(smb_fname->base_name) &&
1428 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1429 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1430 return NT_STATUS_INVALID_PARAMETER;
1433 /* We need to duplicate this fsp. */
1434 return dup_file_fsp(req, fsp, access_mask, share_access,
1435 create_options, fsp_to_dup_into);
1438 static void schedule_defer_open(struct share_mode_lock *lck,
1439 struct timeval request_time,
1440 struct smb_request *req)
1442 struct deferred_open_record state;
1444 /* This is a relative time, added to the absolute
1445 request_time value to get the absolute timeout time.
1446 Note that if this is the second or greater time we enter
1447 this codepath for this particular request mid then
1448 request_time is left as the absolute time of the *first*
1449 time this request mid was processed. This is what allows
1450 the request to eventually time out. */
1452 struct timeval timeout;
1454 /* Normally the smbd we asked should respond within
1455 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1456 * the client did, give twice the timeout as a safety
1457 * measure here in case the other smbd is stuck
1458 * somewhere else. */
1460 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1462 /* Nothing actually uses state.delayed_for_oplocks
1463 but it's handy to differentiate in debug messages
1464 between a 30 second delay due to oplock break, and
1465 a 1 second delay for share mode conflicts. */
1467 state.delayed_for_oplocks = True;
1470 if (!request_timed_out(request_time, timeout)) {
1471 defer_open(lck, request_time, timeout, req, &state);
1475 /****************************************************************************
1476 Work out what access_mask to use from what the client sent us.
1477 ****************************************************************************/
1479 static NTSTATUS smbd_calculate_maximum_allowed_access(
1480 connection_struct *conn,
1481 const struct smb_filename *smb_fname,
1482 uint32_t *p_access_mask)
1484 struct security_descriptor *sd;
1485 uint32_t access_granted;
1488 if (get_current_uid(conn) == (uid_t)0) {
1489 *p_access_mask |= FILE_GENERIC_ALL;
1490 return NT_STATUS_OK;
1493 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1498 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1500 * File did not exist
1502 *p_access_mask = FILE_GENERIC_ALL;
1503 return NT_STATUS_OK;
1505 if (!NT_STATUS_IS_OK(status)) {
1506 DEBUG(10,("smbd_calculate_access_mask: "
1507 "Could not get acl on file %s: %s\n",
1508 smb_fname_str_dbg(smb_fname),
1509 nt_errstr(status)));
1510 return NT_STATUS_ACCESS_DENIED;
1514 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1515 * also takes care of owner WRITE_DAC and READ_CONTROL.
1517 status = se_access_check(sd,
1518 get_current_nttok(conn),
1519 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1524 if (!NT_STATUS_IS_OK(status)) {
1525 DEBUG(10, ("smbd_calculate_access_mask: "
1526 "Access denied on file %s: "
1527 "when calculating maximum access\n",
1528 smb_fname_str_dbg(smb_fname)));
1529 return NT_STATUS_ACCESS_DENIED;
1531 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1532 return NT_STATUS_OK;
1535 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1536 const struct smb_filename *smb_fname,
1538 uint32_t access_mask,
1539 uint32_t *access_mask_out)
1542 uint32_t orig_access_mask = access_mask;
1543 uint32_t rejected_share_access;
1546 * Convert GENERIC bits to specific bits.
1549 se_map_generic(&access_mask, &file_generic_mapping);
1551 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1552 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1554 status = smbd_calculate_maximum_allowed_access(
1555 conn, smb_fname, &access_mask);
1557 if (!NT_STATUS_IS_OK(status)) {
1561 access_mask &= conn->share_access;
1564 rejected_share_access = access_mask & ~(conn->share_access);
1566 if (rejected_share_access) {
1567 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1568 "file %s: rejected by share access mask[0x%08X] "
1569 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1570 smb_fname_str_dbg(smb_fname),
1572 orig_access_mask, access_mask,
1573 rejected_share_access));
1574 return NT_STATUS_ACCESS_DENIED;
1577 *access_mask_out = access_mask;
1578 return NT_STATUS_OK;
1581 /****************************************************************************
1582 Remove the deferred open entry under lock.
1583 ****************************************************************************/
1585 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1586 struct server_id pid)
1588 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1591 DEBUG(0, ("could not get share mode lock\n"));
1594 del_deferred_open_entry(lck, mid, pid);
1598 /****************************************************************
1599 Ensure we get the brlock lock followed by the share mode lock
1600 in the correct order to prevent deadlocks if other smbd's are
1601 using the brlock database on this file simultaneously with this open
1602 (that code also gets the locks in brlock -> share mode lock order).
1603 ****************************************************************/
1605 static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx,
1607 const struct file_id id,
1608 const char *connectpath,
1609 const struct smb_filename *smb_fname,
1610 const struct timespec *p_old_write_time,
1611 struct share_mode_lock **p_lck,
1612 struct byte_range_lock **p_br_lck)
1614 /* Ordering - we must get the br_lck for this
1615 file before the share mode. */
1616 if (lp_locking(fsp->conn->params)) {
1617 *p_br_lck = brl_get_locks_readonly(fsp);
1618 if (*p_br_lck == NULL) {
1619 DEBUG(0, ("Could not get br_lock\n"));
1622 /* Note - we don't need to free the returned
1623 br_lck explicitly as it was allocated on talloc_tos()
1624 and so will be autofreed (and release the lock)
1625 once the frame context disappears.
1627 If it was set to fsp->brlock_rec then it was
1628 talloc_move'd to hang off the fsp pointer and
1629 in this case is guarenteed to not be holding the
1630 lock on the brlock database. */
1633 *p_lck = get_share_mode_lock(mem_ctx,
1639 if (*p_lck == NULL) {
1640 DEBUG(0, ("Could not get share mode lock\n"));
1641 TALLOC_FREE(*p_br_lck);
1647 /****************************************************************************
1648 Open a file with a share mode. Passed in an already created files_struct *.
1649 ****************************************************************************/
1651 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1652 struct smb_request *req,
1653 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1654 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1655 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1656 uint32 create_options, /* options such as delete on close. */
1657 uint32 new_dos_attributes, /* attributes used for new file. */
1658 int oplock_request, /* internal Samba oplock codes. */
1659 /* Information (FILE_EXISTS etc.) */
1660 uint32_t private_flags, /* Samba specific flags. */
1664 struct smb_filename *smb_fname = fsp->fsp_name;
1667 bool file_existed = VALID_STAT(smb_fname->st);
1668 bool def_acl = False;
1669 bool posix_open = False;
1670 bool new_file_created = False;
1671 bool clear_ads = false;
1672 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1673 mode_t new_unx_mode = (mode_t)0;
1674 mode_t unx_mode = (mode_t)0;
1676 uint32 existing_dos_attributes = 0;
1677 struct timeval request_time = timeval_zero();
1678 struct share_mode_lock *lck = NULL;
1679 uint32 open_access_mask = access_mask;
1683 if (conn->printer) {
1685 * Printers are handled completely differently.
1686 * Most of the passed parameters are ignored.
1690 *pinfo = FILE_WAS_CREATED;
1693 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1694 smb_fname_str_dbg(smb_fname)));
1697 DEBUG(0,("open_file_ntcreate: printer open without "
1698 "an SMB request!\n"));
1699 return NT_STATUS_INTERNAL_ERROR;
1702 return print_spool_open(fsp, smb_fname->base_name,
1706 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1708 return NT_STATUS_NO_MEMORY;
1711 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1713 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1714 new_dos_attributes = 0;
1716 /* Windows allows a new file to be created and
1717 silently removes a FILE_ATTRIBUTE_DIRECTORY
1718 sent by the client. Do the same. */
1720 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1722 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1724 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1725 smb_fname, parent_dir);
1728 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1729 "access_mask=0x%x share_access=0x%x "
1730 "create_disposition = 0x%x create_options=0x%x "
1731 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1732 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1733 access_mask, share_access, create_disposition,
1734 create_options, (unsigned int)unx_mode, oplock_request,
1735 (unsigned int)private_flags));
1737 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1738 DEBUG(0, ("No smb request but not an internal only open!\n"));
1739 return NT_STATUS_INTERNAL_ERROR;
1743 * Only non-internal opens can be deferred at all
1748 if (get_deferred_open_message_state(req,
1752 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1753 /* Remember the absolute time of the original
1754 request with this mid. We'll use it later to
1755 see if this has timed out. */
1757 /* Remove the deferred open entry under lock. */
1758 remove_deferred_open_entry(
1759 state->id, req->mid,
1760 messaging_server_id(req->sconn->msg_ctx));
1762 /* Ensure we don't reprocess this message. */
1763 remove_deferred_open_message_smb(req->sconn, req->mid);
1768 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1770 existing_dos_attributes = dos_mode(conn, smb_fname);
1774 /* ignore any oplock requests if oplocks are disabled */
1775 if (!lp_oplocks(SNUM(conn)) ||
1776 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1777 /* Mask off everything except the private Samba bits. */
1778 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1781 /* this is for OS/2 long file names - say we don't support them */
1782 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1783 /* OS/2 Workplace shell fix may be main code stream in a later
1785 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1787 if (use_nt_status()) {
1788 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1790 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1793 switch( create_disposition ) {
1795 * Currently we're using FILE_SUPERSEDE as the same as
1796 * FILE_OVERWRITE_IF but they really are
1797 * different. FILE_SUPERSEDE deletes an existing file
1798 * (requiring delete access) then recreates it.
1800 case FILE_SUPERSEDE:
1801 /* If file exists replace/overwrite. If file doesn't
1803 flags2 |= (O_CREAT | O_TRUNC);
1807 case FILE_OVERWRITE_IF:
1808 /* If file exists replace/overwrite. If file doesn't
1810 flags2 |= (O_CREAT | O_TRUNC);
1815 /* If file exists open. If file doesn't exist error. */
1816 if (!file_existed) {
1817 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1818 "requested for file %s and file "
1820 smb_fname_str_dbg(smb_fname)));
1822 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1826 case FILE_OVERWRITE:
1827 /* If file exists overwrite. If file doesn't exist
1829 if (!file_existed) {
1830 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1831 "requested for file %s and file "
1833 smb_fname_str_dbg(smb_fname) ));
1835 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1842 /* If file exists error. If file doesn't exist
1845 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1846 "requested for file %s and file "
1847 "already exists.\n",
1848 smb_fname_str_dbg(smb_fname)));
1849 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1854 return map_nt_error_from_unix(errno);
1856 flags2 |= (O_CREAT|O_EXCL);
1860 /* If file exists open. If file doesn't exist
1866 return NT_STATUS_INVALID_PARAMETER;
1869 /* We only care about matching attributes on file exists and
1872 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1873 (create_disposition == FILE_OVERWRITE_IF))) {
1874 if (!open_match_attributes(conn, existing_dos_attributes,
1876 smb_fname->st.st_ex_mode,
1877 unx_mode, &new_unx_mode)) {
1878 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1879 "for file %s (%x %x) (0%o, 0%o)\n",
1880 smb_fname_str_dbg(smb_fname),
1881 existing_dos_attributes,
1883 (unsigned int)smb_fname->st.st_ex_mode,
1884 (unsigned int)unx_mode ));
1886 return NT_STATUS_ACCESS_DENIED;
1890 status = smbd_calculate_access_mask(conn, smb_fname, file_existed,
1893 if (!NT_STATUS_IS_OK(status)) {
1894 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1895 "on file %s returned %s\n",
1896 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1900 open_access_mask = access_mask;
1902 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1903 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1906 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1907 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1911 * Note that we ignore the append flag as append does not
1912 * mean the same thing under DOS and Unix.
1915 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1916 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1917 /* DENY_DOS opens are always underlying read-write on the
1918 file handle, no matter what the requested access mask
1920 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1921 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1931 * Currently we only look at FILE_WRITE_THROUGH for create options.
1935 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1940 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1944 if (!posix_open && !CAN_WRITE(conn)) {
1946 * We should really return a permission denied error if either
1947 * O_CREAT or O_TRUNC are set, but for compatibility with
1948 * older versions of Samba we just AND them out.
1950 flags2 &= ~(O_CREAT|O_TRUNC);
1954 * Ensure we can't write on a read-only share or file.
1957 if (flags != O_RDONLY && file_existed &&
1958 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1959 DEBUG(5,("open_file_ntcreate: write access requested for "
1960 "file %s on read only %s\n",
1961 smb_fname_str_dbg(smb_fname),
1962 !CAN_WRITE(conn) ? "share" : "file" ));
1964 return NT_STATUS_ACCESS_DENIED;
1967 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1968 fsp->share_access = share_access;
1969 fsp->fh->private_options = private_flags;
1970 fsp->access_mask = open_access_mask; /* We change this to the
1971 * requested access_mask after
1972 * the open is done. */
1973 fsp->posix_open = posix_open;
1975 /* Ensure no SAMBA_PRIVATE bits can be set. */
1976 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1978 if (timeval_is_zero(&request_time)) {
1979 request_time = fsp->open_time;
1983 struct byte_range_lock *br_lck = NULL;
1984 struct share_mode_entry *batch_entry = NULL;
1985 struct share_mode_entry *exclusive_entry = NULL;
1986 bool got_level2_oplock = false;
1987 bool got_a_none_oplock = false;
1990 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1991 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1993 if (!acquire_ordered_locks(talloc_tos(),
2001 return NT_STATUS_SHARING_VIOLATION;
2004 /* Get the types we need to examine. */
2005 find_oplock_types(fsp,
2011 &got_a_none_oplock);
2013 /* First pass - send break only on batch oplocks. */
2014 if ((req != NULL) &&
2015 delay_for_batch_oplocks(fsp,
2019 schedule_defer_open(lck, request_time, req);
2021 return NT_STATUS_SHARING_VIOLATION;
2024 /* Use the client requested access mask here, not the one we
2026 status = open_mode_check(conn, lck, fsp->name_hash,
2027 access_mask, share_access,
2028 create_options, &file_existed);
2030 if (NT_STATUS_IS_OK(status)) {
2031 /* We might be going to allow this open. Check oplock
2033 /* Second pass - send break for both batch or
2034 * exclusive oplocks. */
2035 if ((req != NULL) &&
2036 delay_for_exclusive_oplocks(
2041 schedule_defer_open(lck, request_time, req);
2043 return NT_STATUS_SHARING_VIOLATION;
2047 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2048 /* DELETE_PENDING is not deferred for a second */
2053 grant_fsp_oplock_type(fsp,
2059 if (!NT_STATUS_IS_OK(status)) {
2060 uint32 can_access_mask;
2061 bool can_access = True;
2063 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2065 /* Check if this can be done with the deny_dos and fcb
2068 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2069 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2071 DEBUG(0, ("DOS open without an SMB "
2074 return NT_STATUS_INTERNAL_ERROR;
2077 /* Use the client requested access mask here,
2078 * not the one we open with. */
2079 status = fcb_or_dos_open(req,
2090 if (NT_STATUS_IS_OK(status)) {
2093 *pinfo = FILE_WAS_OPENED;
2095 return NT_STATUS_OK;
2100 * This next line is a subtlety we need for
2101 * MS-Access. If a file open will fail due to share
2102 * permissions and also for security (access) reasons,
2103 * we need to return the access failed error, not the
2104 * share error. We can't open the file due to kernel
2105 * oplock deadlock (it's possible we failed above on
2106 * the open_mode_check()) so use a userspace check.
2109 if (flags & O_RDWR) {
2110 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2111 } else if (flags & O_WRONLY) {
2112 can_access_mask = FILE_WRITE_DATA;
2114 can_access_mask = FILE_READ_DATA;
2117 if (((can_access_mask & FILE_WRITE_DATA) &&
2118 !CAN_WRITE(conn)) ||
2119 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2120 smb_fname, can_access_mask))) {
2125 * If we're returning a share violation, ensure we
2126 * cope with the braindead 1 second delay.
2129 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2130 lp_defer_sharing_violations()) {
2131 struct timeval timeout;
2132 struct deferred_open_record state;
2135 /* this is a hack to speed up torture tests
2137 timeout_usecs = lp_parm_int(SNUM(conn),
2138 "smbd","sharedelay",
2139 SHARING_VIOLATION_USEC_WAIT);
2141 /* This is a relative time, added to the absolute
2142 request_time value to get the absolute timeout time.
2143 Note that if this is the second or greater time we enter
2144 this codepath for this particular request mid then
2145 request_time is left as the absolute time of the *first*
2146 time this request mid was processed. This is what allows
2147 the request to eventually time out. */
2149 timeout = timeval_set(0, timeout_usecs);
2151 /* Nothing actually uses state.delayed_for_oplocks
2152 but it's handy to differentiate in debug messages
2153 between a 30 second delay due to oplock break, and
2154 a 1 second delay for share mode conflicts. */
2156 state.delayed_for_oplocks = False;
2160 && !request_timed_out(request_time,
2162 defer_open(lck, request_time, timeout,
2170 * We have detected a sharing violation here
2171 * so return the correct error code
2173 status = NT_STATUS_SHARING_VIOLATION;
2175 status = NT_STATUS_ACCESS_DENIED;
2181 * We exit this block with the share entry *locked*.....
2185 SMB_ASSERT(!file_existed || (lck != NULL));
2188 * Ensure we pay attention to default ACLs on directories if required.
2191 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2192 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2193 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2196 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2197 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2198 (unsigned int)flags, (unsigned int)flags2,
2199 (unsigned int)unx_mode, (unsigned int)access_mask,
2200 (unsigned int)open_access_mask));
2203 * open_file strips any O_TRUNC flags itself.
2206 fsp_open = open_file(fsp, conn, req, parent_dir,
2207 flags|flags2, unx_mode, access_mask,
2210 if (!NT_STATUS_IS_OK(fsp_open)) {
2215 if (!file_existed) {
2216 struct byte_range_lock *br_lck = NULL;
2217 struct share_mode_entry *batch_entry = NULL;
2218 struct share_mode_entry *exclusive_entry = NULL;
2219 bool got_level2_oplock = false;
2220 bool got_a_none_oplock = false;
2221 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2224 * Deal with the race condition where two smbd's detect the
2225 * file doesn't exist and do the create at the same time. One
2226 * of them will win and set a share mode, the other (ie. this
2227 * one) should check if the requested share mode for this
2228 * create is allowed.
2232 * Now the file exists and fsp is successfully opened,
2233 * fsp->dev and fsp->inode are valid and should replace the
2234 * dev=0,inode=0 from a non existent file. Spotted by
2235 * Nadav Danieli <nadavd@exanet.com>. JRA.
2240 if (!acquire_ordered_locks(talloc_tos(),
2248 return NT_STATUS_SHARING_VIOLATION;
2251 /* Get the types we need to examine. */
2252 find_oplock_types(fsp,
2258 &got_a_none_oplock);
2260 /* First pass - send break only on batch oplocks. */
2261 if ((req != NULL) &&
2262 delay_for_batch_oplocks(fsp,
2266 schedule_defer_open(lck, request_time, req);
2269 return NT_STATUS_SHARING_VIOLATION;
2272 status = open_mode_check(conn, lck, fsp->name_hash,
2273 access_mask, share_access,
2274 create_options, &file_existed);
2276 if (NT_STATUS_IS_OK(status)) {
2277 /* We might be going to allow this open. Check oplock
2279 /* Second pass - send break for both batch or
2280 * exclusive oplocks. */
2281 if ((req != NULL) &&
2282 delay_for_exclusive_oplocks(
2287 schedule_defer_open(lck, request_time, req);
2290 return NT_STATUS_SHARING_VIOLATION;
2294 if (!NT_STATUS_IS_OK(status)) {
2295 struct deferred_open_record state;
2297 state.delayed_for_oplocks = False;
2300 /* Do it all over again immediately. In the second
2301 * round we will find that the file existed and handle
2302 * the DELETE_PENDING and FCB cases correctly. No need
2303 * to duplicate the code here. Essentially this is a
2304 * "goto top of this function", but don't tell
2308 defer_open(lck, request_time, timeval_zero(),
2316 grant_fsp_oplock_type(fsp,
2323 * We exit this block with the share entry *locked*.....
2328 SMB_ASSERT(lck != NULL);
2330 /* Delete streams if create_disposition requires it */
2331 if (file_existed && clear_ads &&
2332 !is_ntfs_stream_smb_fname(smb_fname)) {
2333 status = delete_all_streams(conn, smb_fname->base_name);
2334 if (!NT_STATUS_IS_OK(status)) {
2341 /* note that we ignore failure for the following. It is
2342 basically a hack for NFS, and NFS will never set one of
2343 these only read them. Nobody but Samba can ever set a deny
2344 mode and we have already checked our more authoritative
2345 locking database for permission to set this deny mode. If
2346 the kernel refuses the operations then the kernel is wrong.
2347 note that GPFS supports it as well - jmcd */
2349 if (fsp->fh->fd != -1) {
2351 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2352 if(ret_flock == -1 ){
2357 return NT_STATUS_SHARING_VIOLATION;
2362 * At this point onwards, we can guarentee that the share entry
2363 * is locked, whether we created the file or not, and that the
2364 * deny mode is compatible with all current opens.
2368 * If requested, truncate the file.
2371 if (file_existed && (flags2&O_TRUNC)) {
2373 * We are modifing the file after open - update the stat
2376 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2377 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2378 status = map_nt_error_from_unix(errno);
2386 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2387 * but we don't have to store this - just ignore it on access check.
2389 if (conn->sconn->using_smb2) {
2391 * SMB2 doesn't return it (according to Microsoft tests).
2392 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2393 * File created with access = 0x7 (Read, Write, Delete)
2394 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2396 fsp->access_mask = access_mask;
2398 /* But SMB1 does. */
2399 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2403 /* stat opens on existing files don't get oplocks. */
2404 if (is_stat_open(open_access_mask)) {
2405 fsp->oplock_type = NO_OPLOCK;
2408 if (flags2 & O_TRUNC) {
2409 info = FILE_WAS_OVERWRITTEN;
2411 info = FILE_WAS_OPENED;
2414 info = FILE_WAS_CREATED;
2422 * Setup the oplock info in both the shared memory and
2426 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2428 * Could not get the kernel oplock or there are byte-range
2429 * locks on the file.
2431 fsp->oplock_type = NO_OPLOCK;
2434 set_share_mode(lck, fsp, get_current_uid(conn),
2438 /* Handle strange delete on close create semantics. */
2439 if (create_options & FILE_DELETE_ON_CLOSE) {
2441 status = can_set_delete_on_close(fsp, new_dos_attributes);
2443 if (!NT_STATUS_IS_OK(status)) {
2444 /* Remember to delete the mode we just added. */
2445 del_share_mode(lck, fsp);
2450 /* Note that here we set the *inital* delete on close flag,
2451 not the regular one. The magic gets handled in close. */
2452 fsp->initial_delete_on_close = True;
2455 if (info == FILE_WAS_OVERWRITTEN
2456 || info == FILE_WAS_CREATED
2457 || info == FILE_WAS_SUPERSEDED) {
2458 new_file_created = True;
2461 if (new_file_created) {
2462 /* Files should be initially set as archive */
2463 if (lp_map_archive(SNUM(conn)) ||
2464 lp_store_dos_attributes(SNUM(conn))) {
2466 if (file_set_dosmode(conn, smb_fname,
2467 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2468 parent_dir, true) == 0) {
2469 unx_mode = smb_fname->st.st_ex_mode;
2475 /* Determine sparse flag. */
2477 /* POSIX opens are sparse by default. */
2478 fsp->is_sparse = true;
2480 fsp->is_sparse = (file_existed &&
2481 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2485 * Take care of inherited ACLs on created files - if default ACL not
2489 if (!posix_open && !file_existed && !def_acl) {
2491 int saved_errno = errno; /* We might get ENOSYS in the next
2494 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2496 errno = saved_errno; /* Ignore ENOSYS */
2499 } else if (new_unx_mode) {
2503 /* Attributes need changing. File already existed. */
2506 int saved_errno = errno; /* We might get ENOSYS in the
2508 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2510 if (ret == -1 && errno == ENOSYS) {
2511 errno = saved_errno; /* Ignore ENOSYS */
2513 DEBUG(5, ("open_file_ntcreate: reset "
2514 "attributes of file %s to 0%o\n",
2515 smb_fname_str_dbg(smb_fname),
2516 (unsigned int)new_unx_mode));
2517 ret = 0; /* Don't do the fchmod below. */
2522 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2523 DEBUG(5, ("open_file_ntcreate: failed to reset "
2524 "attributes of file %s to 0%o\n",
2525 smb_fname_str_dbg(smb_fname),
2526 (unsigned int)new_unx_mode));
2529 /* If this is a successful open, we must remove any deferred open
2532 del_deferred_open_entry(lck, req->mid,
2533 messaging_server_id(req->sconn->msg_ctx));
2537 return NT_STATUS_OK;
2541 /****************************************************************************
2542 Open a file for for write to ensure that we can fchmod it.
2543 ****************************************************************************/
2545 NTSTATUS open_file_fchmod(connection_struct *conn,
2546 struct smb_filename *smb_fname,
2547 files_struct **result)
2549 if (!VALID_STAT(smb_fname->st)) {
2550 return NT_STATUS_INVALID_PARAMETER;
2553 return SMB_VFS_CREATE_FILE(
2556 0, /* root_dir_fid */
2557 smb_fname, /* fname */
2558 FILE_WRITE_DATA, /* access_mask */
2559 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2561 FILE_OPEN, /* create_disposition*/
2562 0, /* create_options */
2563 0, /* file_attributes */
2564 INTERNAL_OPEN_ONLY, /* oplock_request */
2565 0, /* allocation_size */
2566 0, /* private_flags */
2569 result, /* result */
2573 static NTSTATUS mkdir_internal(connection_struct *conn,
2574 struct smb_filename *smb_dname,
2575 uint32 file_attributes)
2578 char *parent_dir = NULL;
2580 bool posix_open = false;
2581 bool need_re_stat = false;
2582 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2584 if(access_mask & ~(conn->share_access)) {
2585 DEBUG(5,("mkdir_internal: failing share access "
2586 "%s\n", lp_servicename(SNUM(conn))));
2587 return NT_STATUS_ACCESS_DENIED;
2590 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2592 return NT_STATUS_NO_MEMORY;
2595 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2597 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2599 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2602 status = check_parent_access(conn,
2606 if(!NT_STATUS_IS_OK(status)) {
2607 DEBUG(5,("mkdir_internal: check_parent_access "
2608 "on directory %s for path %s returned %s\n",
2610 smb_dname->base_name,
2611 nt_errstr(status) ));
2615 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2616 return map_nt_error_from_unix(errno);
2619 /* Ensure we're checking for a symlink here.... */
2620 /* We don't want to get caught by a symlink racer. */
2622 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2623 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2624 smb_fname_str_dbg(smb_dname), strerror(errno)));
2625 return map_nt_error_from_unix(errno);
2628 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2629 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2630 smb_fname_str_dbg(smb_dname)));
2631 return NT_STATUS_NOT_A_DIRECTORY;
2634 if (lp_store_dos_attributes(SNUM(conn))) {
2636 file_set_dosmode(conn, smb_dname,
2637 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2642 if (lp_inherit_perms(SNUM(conn))) {
2643 inherit_access_posix_acl(conn, parent_dir,
2644 smb_dname->base_name, mode);
2645 need_re_stat = true;
2650 * Check if high bits should have been set,
2651 * then (if bits are missing): add them.
2652 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2655 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2656 (mode & ~smb_dname->st.st_ex_mode)) {
2657 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2658 (smb_dname->st.st_ex_mode |
2659 (mode & ~smb_dname->st.st_ex_mode)));
2660 need_re_stat = true;
2664 /* Change the owner if required. */
2665 if (lp_inherit_owner(SNUM(conn))) {
2666 change_dir_owner_to_parent(conn, parent_dir,
2667 smb_dname->base_name,
2669 need_re_stat = true;
2673 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2674 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2675 smb_fname_str_dbg(smb_dname), strerror(errno)));
2676 return map_nt_error_from_unix(errno);
2680 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2681 smb_dname->base_name);
2683 return NT_STATUS_OK;
2686 /****************************************************************************
2687 Ensure we didn't get symlink raced on opening a directory.
2688 ****************************************************************************/
2690 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2691 const SMB_STRUCT_STAT *sbuf2)
2693 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2694 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2695 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2696 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2702 /****************************************************************************
2703 Open a directory from an NT SMB call.
2704 ****************************************************************************/
2706 static NTSTATUS open_directory(connection_struct *conn,
2707 struct smb_request *req,
2708 struct smb_filename *smb_dname,
2710 uint32 share_access,
2711 uint32 create_disposition,
2712 uint32 create_options,
2713 uint32 file_attributes,
2715 files_struct **result)
2717 files_struct *fsp = NULL;
2718 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2719 struct share_mode_lock *lck = NULL;
2721 struct timespec mtimespec;
2724 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2726 /* Ensure we have a directory attribute. */
2727 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2729 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2730 "share_access = 0x%x create_options = 0x%x, "
2731 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2732 smb_fname_str_dbg(smb_dname),
2733 (unsigned int)access_mask,
2734 (unsigned int)share_access,
2735 (unsigned int)create_options,
2736 (unsigned int)create_disposition,
2737 (unsigned int)file_attributes));
2739 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2740 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2741 is_ntfs_stream_smb_fname(smb_dname)) {
2742 DEBUG(2, ("open_directory: %s is a stream name!\n",
2743 smb_fname_str_dbg(smb_dname)));
2744 return NT_STATUS_NOT_A_DIRECTORY;
2747 status = smbd_calculate_access_mask(conn, smb_dname, dir_existed,
2748 access_mask, &access_mask);
2749 if (!NT_STATUS_IS_OK(status)) {
2750 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2751 "on file %s returned %s\n",
2752 smb_fname_str_dbg(smb_dname),
2753 nt_errstr(status)));
2757 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2758 !security_token_has_privilege(get_current_nttok(conn),
2759 SEC_PRIV_SECURITY)) {
2760 DEBUG(10, ("open_directory: open on %s "
2761 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2762 smb_fname_str_dbg(smb_dname)));
2763 return NT_STATUS_PRIVILEGE_NOT_HELD;
2766 switch( create_disposition ) {
2770 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2773 info = FILE_WAS_OPENED;
2778 /* If directory exists error. If directory doesn't
2782 status = NT_STATUS_OBJECT_NAME_COLLISION;
2783 DEBUG(2, ("open_directory: unable to create "
2784 "%s. Error was %s\n",
2785 smb_fname_str_dbg(smb_dname),
2786 nt_errstr(status)));
2790 status = mkdir_internal(conn, smb_dname,
2793 if (!NT_STATUS_IS_OK(status)) {
2794 DEBUG(2, ("open_directory: unable to create "
2795 "%s. Error was %s\n",
2796 smb_fname_str_dbg(smb_dname),
2797 nt_errstr(status)));
2801 info = FILE_WAS_CREATED;
2806 * If directory exists open. If directory doesn't
2811 status = NT_STATUS_OK;
2812 info = FILE_WAS_OPENED;
2814 status = mkdir_internal(conn, smb_dname,
2817 if (NT_STATUS_IS_OK(status)) {
2818 info = FILE_WAS_CREATED;
2820 /* Cope with create race. */
2821 if (!NT_STATUS_EQUAL(status,
2822 NT_STATUS_OBJECT_NAME_COLLISION)) {
2823 DEBUG(2, ("open_directory: unable to create "
2824 "%s. Error was %s\n",
2825 smb_fname_str_dbg(smb_dname),
2826 nt_errstr(status)));
2829 info = FILE_WAS_OPENED;
2835 case FILE_SUPERSEDE:
2836 case FILE_OVERWRITE:
2837 case FILE_OVERWRITE_IF:
2839 DEBUG(5,("open_directory: invalid create_disposition "
2840 "0x%x for directory %s\n",
2841 (unsigned int)create_disposition,
2842 smb_fname_str_dbg(smb_dname)));
2843 return NT_STATUS_INVALID_PARAMETER;
2846 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2847 DEBUG(5,("open_directory: %s is not a directory !\n",
2848 smb_fname_str_dbg(smb_dname)));
2849 return NT_STATUS_NOT_A_DIRECTORY;
2852 if (info == FILE_WAS_OPENED) {
2853 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2854 if (!NT_STATUS_IS_OK(status)) {
2855 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2856 "file %s failed with %s\n",
2857 smb_fname_str_dbg(smb_dname),
2858 nt_errstr(status)));
2863 status = file_new(req, conn, &fsp);
2864 if(!NT_STATUS_IS_OK(status)) {
2869 * Setup the files_struct for it.
2872 fsp->mode = smb_dname->st.st_ex_mode;
2873 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2874 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2875 fsp->file_pid = req ? req->smbpid : 0;
2876 fsp->can_lock = False;
2877 fsp->can_read = False;
2878 fsp->can_write = False;
2880 fsp->share_access = share_access;
2881 fsp->fh->private_options = 0;
2883 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2885 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2886 fsp->print_file = NULL;
2887 fsp->modified = False;
2888 fsp->oplock_type = NO_OPLOCK;
2889 fsp->sent_oplock_break = NO_BREAK_SENT;
2890 fsp->is_directory = True;
2891 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2892 status = fsp_set_smb_fname(fsp, smb_dname);
2893 if (!NT_STATUS_IS_OK(status)) {
2894 file_free(req, fsp);
2898 mtimespec = smb_dname->st.st_ex_mtime;
2901 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2903 /* POSIX allows us to open a directory with O_RDONLY. */
2904 status = fd_open(conn, fsp, O_RDONLY, 0);
2906 if (!NT_STATUS_IS_OK(status)) {
2907 DEBUG(5, ("open_directory: Could not open fd for "
2909 smb_fname_str_dbg(smb_dname),
2910 nt_errstr(status)));
2911 file_free(req, fsp);
2915 status = vfs_stat_fsp(fsp);
2916 if (!NT_STATUS_IS_OK(status)) {
2918 file_free(req, fsp);
2922 /* Ensure there was no race condition. */
2923 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2924 DEBUG(5,("open_directory: stat struct differs for "
2926 smb_fname_str_dbg(smb_dname)));
2928 file_free(req, fsp);
2929 return NT_STATUS_ACCESS_DENIED;
2932 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2933 conn->connectpath, smb_dname, &mtimespec);
2936 DEBUG(0, ("open_directory: Could not get share mode lock for "
2937 "%s\n", smb_fname_str_dbg(smb_dname)));
2939 file_free(req, fsp);
2940 return NT_STATUS_SHARING_VIOLATION;
2943 status = open_mode_check(conn, lck, fsp->name_hash,
2944 access_mask, share_access,
2945 create_options, &dir_existed);
2947 if (!NT_STATUS_IS_OK(status)) {
2950 file_free(req, fsp);
2954 set_share_mode(lck, fsp, get_current_uid(conn),
2955 req ? req->mid : 0, NO_OPLOCK);
2957 /* For directories the delete on close bit at open time seems
2958 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2959 if (create_options & FILE_DELETE_ON_CLOSE) {
2960 status = can_set_delete_on_close(fsp, 0);
2961 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2964 file_free(req, fsp);
2968 if (NT_STATUS_IS_OK(status)) {
2969 /* Note that here we set the *inital* delete on close flag,
2970 not the regular one. The magic gets handled in close. */
2971 fsp->initial_delete_on_close = True;
2982 return NT_STATUS_OK;
2985 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2986 struct smb_filename *smb_dname)
2991 status = SMB_VFS_CREATE_FILE(
2994 0, /* root_dir_fid */
2995 smb_dname, /* fname */
2996 FILE_READ_ATTRIBUTES, /* access_mask */
2997 FILE_SHARE_NONE, /* share_access */
2998 FILE_CREATE, /* create_disposition*/
2999 FILE_DIRECTORY_FILE, /* create_options */
3000 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3001 0, /* oplock_request */
3002 0, /* allocation_size */
3003 0, /* private_flags */
3009 if (NT_STATUS_IS_OK(status)) {
3010 close_file(req, fsp, NORMAL_CLOSE);
3016 /****************************************************************************
3017 Receive notification that one of our open files has been renamed by another
3019 ****************************************************************************/
3021 void msg_file_was_renamed(struct messaging_context *msg,
3024 struct server_id server_id,
3028 char *frm = (char *)data->data;
3030 const char *sharepath;
3031 const char *base_name;
3032 const char *stream_name;
3033 struct smb_filename *smb_fname = NULL;
3034 size_t sp_len, bn_len;
3036 struct smbd_server_connection *sconn =
3037 talloc_get_type_abort(private_data,
3038 struct smbd_server_connection);
3040 if (data->data == NULL
3041 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3042 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3043 (int)data->length));
3047 /* Unpack the message. */
3048 pull_file_id_24(frm, &id);
3049 sharepath = &frm[24];
3050 sp_len = strlen(sharepath);
3051 base_name = sharepath + sp_len + 1;
3052 bn_len = strlen(base_name);
3053 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3055 /* stream_name must always be NULL if there is no stream. */
3056 if (stream_name[0] == '\0') {
3060 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3061 stream_name, NULL, &smb_fname);
3062 if (!NT_STATUS_IS_OK(status)) {
3066 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3068 sharepath, smb_fname_str_dbg(smb_fname),
3069 file_id_string_tos(&id)));
3071 for(fsp = file_find_di_first(sconn, id); fsp;
3072 fsp = file_find_di_next(fsp)) {
3073 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3075 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3076 fsp->fnum, fsp_str_dbg(fsp),
3077 smb_fname_str_dbg(smb_fname)));
3078 status = fsp_set_smb_fname(fsp, smb_fname);
3079 if (!NT_STATUS_IS_OK(status)) {
3084 /* Now we have the complete path we can work out if this is
3085 actually within this share and adjust newname accordingly. */
3086 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3087 "not sharepath %s) "
3088 "fnum %d from %s -> %s\n",
3089 fsp->conn->connectpath,
3093 smb_fname_str_dbg(smb_fname)));
3097 TALLOC_FREE(smb_fname);
3102 * If a main file is opened for delete, all streams need to be checked for
3103 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3104 * If that works, delete them all by setting the delete on close and close.
3107 NTSTATUS open_streams_for_delete(connection_struct *conn,
3110 struct stream_struct *stream_info = NULL;
3111 files_struct **streams = NULL;
3113 unsigned int num_streams = 0;
3114 TALLOC_CTX *frame = talloc_stackframe();
3117 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3118 &num_streams, &stream_info);
3120 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3121 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3122 DEBUG(10, ("no streams around\n"));
3124 return NT_STATUS_OK;
3127 if (!NT_STATUS_IS_OK(status)) {
3128 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3129 nt_errstr(status)));
3133 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3136 if (num_streams == 0) {
3138 return NT_STATUS_OK;
3141 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3142 if (streams == NULL) {
3143 DEBUG(0, ("talloc failed\n"));
3144 status = NT_STATUS_NO_MEMORY;
3148 for (i=0; i<num_streams; i++) {
3149 struct smb_filename *smb_fname = NULL;
3151 if (strequal(stream_info[i].name, "::$DATA")) {
3156 status = create_synthetic_smb_fname(talloc_tos(), fname,
3157 stream_info[i].name,
3159 if (!NT_STATUS_IS_OK(status)) {
3163 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3164 DEBUG(10, ("Unable to stat stream: %s\n",
3165 smb_fname_str_dbg(smb_fname)));
3168 status = SMB_VFS_CREATE_FILE(
3171 0, /* root_dir_fid */
3172 smb_fname, /* fname */
3173 DELETE_ACCESS, /* access_mask */
3174 (FILE_SHARE_READ | /* share_access */
3175 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3176 FILE_OPEN, /* create_disposition*/
3177 0, /* create_options */
3178 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3179 0, /* oplock_request */
3180 0, /* allocation_size */
3181 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3184 &streams[i], /* result */
3187 if (!NT_STATUS_IS_OK(status)) {
3188 DEBUG(10, ("Could not open stream %s: %s\n",
3189 smb_fname_str_dbg(smb_fname),
3190 nt_errstr(status)));
3192 TALLOC_FREE(smb_fname);
3195 TALLOC_FREE(smb_fname);
3199 * don't touch the variable "status" beyond this point :-)
3202 for (i -= 1 ; i >= 0; i--) {
3203 if (streams[i] == NULL) {
3207 DEBUG(10, ("Closing stream # %d, %s\n", i,
3208 fsp_str_dbg(streams[i])));
3209 close_file(NULL, streams[i], NORMAL_CLOSE);
3217 /*********************************************************************
3218 Create a default ACL by inheriting from the parent. If no inheritance
3219 from the parent available, don't set anything. This will leave the actual
3220 permissions the new file or directory already got from the filesystem
3221 as the NT ACL when read.
3222 *********************************************************************/
3224 static NTSTATUS inherit_new_acl(files_struct *fsp)
3226 TALLOC_CTX *ctx = talloc_tos();
3227 char *parent_name = NULL;
3228 struct security_descriptor *parent_desc = NULL;
3229 NTSTATUS status = NT_STATUS_OK;
3230 struct security_descriptor *psd = NULL;
3231 struct dom_sid *owner_sid = NULL;
3232 struct dom_sid *group_sid = NULL;
3233 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3234 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3235 bool inheritable_components = false;
3238 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3239 return NT_STATUS_NO_MEMORY;
3242 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3244 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3246 if (!NT_STATUS_IS_OK(status)) {
3250 inheritable_components = sd_has_inheritable_components(parent_desc,
3253 if (!inheritable_components && !inherit_owner) {
3254 /* Nothing to inherit and not setting owner. */
3255 return NT_STATUS_OK;
3258 /* Create an inherited descriptor from the parent. */
3260 if (DEBUGLEVEL >= 10) {
3261 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3262 fsp_str_dbg(fsp) ));
3263 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3266 /* Inherit from parent descriptor if "inherit owner" set. */
3267 if (inherit_owner) {
3268 owner_sid = parent_desc->owner_sid;
3269 group_sid = parent_desc->group_sid;
3272 if (owner_sid == NULL) {
3273 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3275 if (group_sid == NULL) {
3276 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3279 status = se_create_child_secdesc(ctx,
3286 if (!NT_STATUS_IS_OK(status)) {
3290 /* If inheritable_components == false,
3291 se_create_child_secdesc()
3292 creates a security desriptor with a NULL dacl
3293 entry, but with SEC_DESC_DACL_PRESENT. We need
3294 to remove that flag. */
3296 if (!inheritable_components) {
3297 security_info_sent &= ~SECINFO_DACL;
3298 psd->type &= ~SEC_DESC_DACL_PRESENT;
3301 if (DEBUGLEVEL >= 10) {
3302 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3303 fsp_str_dbg(fsp) ));
3304 NDR_PRINT_DEBUG(security_descriptor, psd);
3307 if (inherit_owner) {
3308 /* We need to be root to force this. */
3311 status = SMB_VFS_FSET_NT_ACL(fsp,
3314 if (inherit_owner) {
3321 * Wrapper around open_file_ntcreate and open_directory
3324 static NTSTATUS create_file_unixpath(connection_struct *conn,
3325 struct smb_request *req,
3326 struct smb_filename *smb_fname,
3327 uint32_t access_mask,
3328 uint32_t share_access,
3329 uint32_t create_disposition,
3330 uint32_t create_options,
3331 uint32_t file_attributes,
3332 uint32_t oplock_request,
3333 uint64_t allocation_size,
3334 uint32_t private_flags,
3335 struct security_descriptor *sd,
3336 struct ea_list *ea_list,
3338 files_struct **result,
3341 int info = FILE_WAS_OPENED;
3342 files_struct *base_fsp = NULL;
3343 files_struct *fsp = NULL;
3346 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3347 "file_attributes = 0x%x, share_access = 0x%x, "
3348 "create_disposition = 0x%x create_options = 0x%x "
3349 "oplock_request = 0x%x private_flags = 0x%x "
3350 "ea_list = 0x%p, sd = 0x%p, "
3352 (unsigned int)access_mask,
3353 (unsigned int)file_attributes,
3354 (unsigned int)share_access,
3355 (unsigned int)create_disposition,
3356 (unsigned int)create_options,
3357 (unsigned int)oplock_request,
3358 (unsigned int)private_flags,
3359 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3361 if (create_options & FILE_OPEN_BY_FILE_ID) {
3362 status = NT_STATUS_NOT_SUPPORTED;
3366 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3367 status = NT_STATUS_INVALID_PARAMETER;
3372 oplock_request |= INTERNAL_OPEN_ONLY;
3375 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3376 && (access_mask & DELETE_ACCESS)
3377 && !is_ntfs_stream_smb_fname(smb_fname)) {
3379 * We can't open a file with DELETE access if any of the
3380 * streams is open without FILE_SHARE_DELETE
3382 status = open_streams_for_delete(conn, smb_fname->base_name);
3384 if (!NT_STATUS_IS_OK(status)) {
3389 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3390 !security_token_has_privilege(get_current_nttok(conn),
3391 SEC_PRIV_SECURITY)) {
3392 DEBUG(10, ("create_file_unixpath: open on %s "
3393 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3394 smb_fname_str_dbg(smb_fname)));
3395 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3399 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3400 && is_ntfs_stream_smb_fname(smb_fname)
3401 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3402 uint32 base_create_disposition;
3403 struct smb_filename *smb_fname_base = NULL;
3405 if (create_options & FILE_DIRECTORY_FILE) {
3406 status = NT_STATUS_NOT_A_DIRECTORY;
3410 switch (create_disposition) {
3412 base_create_disposition = FILE_OPEN;
3415 base_create_disposition = FILE_OPEN_IF;
3419 /* Create an smb_filename with stream_name == NULL. */
3420 status = create_synthetic_smb_fname(talloc_tos(),
3421 smb_fname->base_name,
3424 if (!NT_STATUS_IS_OK(status)) {
3428 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3429 DEBUG(10, ("Unable to stat stream: %s\n",
3430 smb_fname_str_dbg(smb_fname_base)));
3433 /* Open the base file. */
3434 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3437 | FILE_SHARE_DELETE,
3438 base_create_disposition,
3439 0, 0, 0, 0, 0, NULL, NULL,
3441 TALLOC_FREE(smb_fname_base);
3443 if (!NT_STATUS_IS_OK(status)) {
3444 DEBUG(10, ("create_file_unixpath for base %s failed: "
3445 "%s\n", smb_fname->base_name,
3446 nt_errstr(status)));
3449 /* we don't need to low level fd */
3454 * If it's a request for a directory open, deal with it separately.
3457 if (create_options & FILE_DIRECTORY_FILE) {
3459 if (create_options & FILE_NON_DIRECTORY_FILE) {
3460 status = NT_STATUS_INVALID_PARAMETER;
3464 /* Can't open a temp directory. IFS kit test. */
3465 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3466 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3467 status = NT_STATUS_INVALID_PARAMETER;
3472 * We will get a create directory here if the Win32
3473 * app specified a security descriptor in the
3474 * CreateDirectory() call.
3478 status = open_directory(
3479 conn, req, smb_fname, access_mask, share_access,
3480 create_disposition, create_options, file_attributes,
3485 * Ordinary file case.
3488 status = file_new(req, conn, &fsp);
3489 if(!NT_STATUS_IS_OK(status)) {
3493 status = fsp_set_smb_fname(fsp, smb_fname);
3494 if (!NT_STATUS_IS_OK(status)) {
3499 * We're opening the stream element of a base_fsp
3500 * we already opened. Set up the base_fsp pointer.
3503 fsp->base_fsp = base_fsp;
3506 status = open_file_ntcreate(conn,
3518 if(!NT_STATUS_IS_OK(status)) {
3519 file_free(req, fsp);
3523 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3525 /* A stream open never opens a directory */
3528 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3533 * Fail the open if it was explicitly a non-directory
3537 if (create_options & FILE_NON_DIRECTORY_FILE) {
3538 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3543 status = open_directory(
3544 conn, req, smb_fname, access_mask,
3545 share_access, create_disposition,
3546 create_options, file_attributes,
3551 if (!NT_STATUS_IS_OK(status)) {
3555 fsp->base_fsp = base_fsp;
3557 if ((ea_list != NULL) &&
3558 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3559 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3560 if (!NT_STATUS_IS_OK(status)) {
3565 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3566 status = NT_STATUS_ACCESS_DENIED;
3570 /* Save the requested allocation size. */
3571 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3573 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3574 fsp->initial_allocation_size = smb_roundup(
3575 fsp->conn, allocation_size);
3576 if (fsp->is_directory) {
3577 /* Can't set allocation size on a directory. */
3578 status = NT_STATUS_ACCESS_DENIED;
3581 if (vfs_allocate_file_space(
3582 fsp, fsp->initial_allocation_size) == -1) {
3583 status = NT_STATUS_DISK_FULL;
3587 fsp->initial_allocation_size = smb_roundup(
3588 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3592 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3593 fsp->base_fsp == NULL) {
3596 * According to the MS documentation, the only time the security
3597 * descriptor is applied to the opened file is iff we *created* the
3598 * file; an existing file stays the same.
3600 * Also, it seems (from observation) that you can open the file with
3601 * any access mask but you can still write the sd. We need to override
3602 * the granted access before we call set_sd
3603 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3606 uint32_t sec_info_sent;
3607 uint32_t saved_access_mask = fsp->access_mask;
3609 sec_info_sent = get_sec_info(sd);
3611 fsp->access_mask = FILE_GENERIC_ALL;
3613 /* Convert all the generic bits. */
3614 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3615 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3617 if (sec_info_sent & (SECINFO_OWNER|
3621 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3624 fsp->access_mask = saved_access_mask;
3626 if (!NT_STATUS_IS_OK(status)) {
3629 } else if (lp_inherit_acls(SNUM(conn))) {
3630 /* Inherit from parent. Errors here are not fatal. */
3631 status = inherit_new_acl(fsp);
3632 if (!NT_STATUS_IS_OK(status)) {
3633 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3635 nt_errstr(status) ));
3640 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3643 if (pinfo != NULL) {
3647 smb_fname->st = fsp->fsp_name->st;
3649 return NT_STATUS_OK;
3652 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3655 if (base_fsp && fsp->base_fsp == base_fsp) {
3657 * The close_file below will close
3662 close_file(req, fsp, ERROR_CLOSE);
3665 if (base_fsp != NULL) {
3666 close_file(req, base_fsp, ERROR_CLOSE);
3673 * Calculate the full path name given a relative fid.
3675 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3676 struct smb_request *req,
3677 uint16_t root_dir_fid,
3678 const struct smb_filename *smb_fname,
3679 struct smb_filename **smb_fname_out)
3681 files_struct *dir_fsp;
3682 char *parent_fname = NULL;
3683 char *new_base_name = NULL;
3686 if (root_dir_fid == 0 || !smb_fname) {
3687 status = NT_STATUS_INTERNAL_ERROR;
3691 dir_fsp = file_fsp(req, root_dir_fid);
3693 if (dir_fsp == NULL) {
3694 status = NT_STATUS_INVALID_HANDLE;
3698 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3699 status = NT_STATUS_INVALID_HANDLE;
3703 if (!dir_fsp->is_directory) {
3706 * Check to see if this is a mac fork of some kind.
3709 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3710 is_ntfs_stream_smb_fname(smb_fname)) {
3711 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3716 we need to handle the case when we get a
3717 relative open relative to a file and the
3718 pathname is blank - this is a reopen!
3719 (hint from demyn plantenberg)
3722 status = NT_STATUS_INVALID_HANDLE;
3726 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3728 * We're at the toplevel dir, the final file name
3729 * must not contain ./, as this is filtered out
3730 * normally by srvstr_get_path and unix_convert
3731 * explicitly rejects paths containing ./.
3733 parent_fname = talloc_strdup(talloc_tos(), "");
3734 if (parent_fname == NULL) {
3735 status = NT_STATUS_NO_MEMORY;
3739 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3742 * Copy in the base directory name.
3745 parent_fname = talloc_array(talloc_tos(), char,
3747 if (parent_fname == NULL) {
3748 status = NT_STATUS_NO_MEMORY;
3751 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3755 * Ensure it ends in a '/'.
3756 * We used TALLOC_SIZE +2 to add space for the '/'.
3760 && (parent_fname[dir_name_len-1] != '\\')
3761 && (parent_fname[dir_name_len-1] != '/')) {
3762 parent_fname[dir_name_len] = '/';
3763 parent_fname[dir_name_len+1] = '\0';
3767 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3768 smb_fname->base_name);
3769 if (new_base_name == NULL) {
3770 status = NT_STATUS_NO_MEMORY;
3774 status = filename_convert(req,
3776 req->flags2 & FLAGS2_DFS_PATHNAMES,
3781 if (!NT_STATUS_IS_OK(status)) {
3786 TALLOC_FREE(parent_fname);
3787 TALLOC_FREE(new_base_name);
3791 NTSTATUS create_file_default(connection_struct *conn,
3792 struct smb_request *req,
3793 uint16_t root_dir_fid,
3794 struct smb_filename *smb_fname,
3795 uint32_t access_mask,
3796 uint32_t share_access,
3797 uint32_t create_disposition,
3798 uint32_t create_options,
3799 uint32_t file_attributes,
3800 uint32_t oplock_request,
3801 uint64_t allocation_size,
3802 uint32_t private_flags,
3803 struct security_descriptor *sd,
3804 struct ea_list *ea_list,
3805 files_struct **result,
3808 int info = FILE_WAS_OPENED;
3809 files_struct *fsp = NULL;
3811 bool stream_name = false;
3813 DEBUG(10,("create_file: access_mask = 0x%x "
3814 "file_attributes = 0x%x, share_access = 0x%x, "
3815 "create_disposition = 0x%x create_options = 0x%x "
3816 "oplock_request = 0x%x "
3817 "private_flags = 0x%x "
3818 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3820 (unsigned int)access_mask,
3821 (unsigned int)file_attributes,
3822 (unsigned int)share_access,
3823 (unsigned int)create_disposition,
3824 (unsigned int)create_options,
3825 (unsigned int)oplock_request,
3826 (unsigned int)private_flags,
3827 (unsigned int)root_dir_fid,
3828 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3831 * Calculate the filename from the root_dir_if if necessary.
3834 if (root_dir_fid != 0) {
3835 struct smb_filename *smb_fname_out = NULL;
3836 status = get_relative_fid_filename(conn, req, root_dir_fid,
3837 smb_fname, &smb_fname_out);
3838 if (!NT_STATUS_IS_OK(status)) {
3841 smb_fname = smb_fname_out;
3845 * Check to see if this is a mac fork of some kind.
3848 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3850 enum FAKE_FILE_TYPE fake_file_type;
3852 fake_file_type = is_fake_file(smb_fname);
3854 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3857 * Here we go! support for changing the disk quotas
3860 * We need to fake up to open this MAGIC QUOTA file
3861 * and return a valid FID.
3863 * w2k close this file directly after openening xp
3864 * also tries a QUERY_FILE_INFO on the file and then
3867 status = open_fake_file(req, conn, req->vuid,
3868 fake_file_type, smb_fname,
3870 if (!NT_STATUS_IS_OK(status)) {
3874 ZERO_STRUCT(smb_fname->st);
3878 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3879 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3884 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3886 smb_fname->stream_name = NULL;
3887 /* We have to handle this error here. */
3888 if (create_options & FILE_DIRECTORY_FILE) {
3889 status = NT_STATUS_NOT_A_DIRECTORY;
3892 if (lp_posix_pathnames()) {
3893 ret = SMB_VFS_LSTAT(conn, smb_fname);
3895 ret = SMB_VFS_STAT(conn, smb_fname);
3898 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3899 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3904 status = create_file_unixpath(
3905 conn, req, smb_fname, access_mask, share_access,
3906 create_disposition, create_options, file_attributes,
3907 oplock_request, allocation_size, private_flags,
3911 if (!NT_STATUS_IS_OK(status)) {
3916 DEBUG(10, ("create_file: info=%d\n", info));
3919 if (pinfo != NULL) {
3922 return NT_STATUS_OK;
3925 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3928 close_file(req, fsp, ERROR_CLOSE);