2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
34 extern const struct generic_mapping file_generic_mapping;
36 struct deferred_open_record {
37 bool delayed_for_oplocks;
41 /****************************************************************************
42 If the requester wanted DELETE_ACCESS and was rejected because
43 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
45 ****************************************************************************/
47 static bool parent_override_delete(connection_struct *conn,
48 const struct smb_filename *smb_fname,
50 uint32_t rejected_mask)
52 if ((access_mask & DELETE_ACCESS) &&
53 (rejected_mask & DELETE_ACCESS) &&
54 can_delete_file_in_directory(conn, smb_fname)) {
60 /****************************************************************************
61 Check if we have open rights.
62 ****************************************************************************/
64 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
65 const struct smb_filename *smb_fname,
68 /* Check if we have rights to open. */
70 struct security_descriptor *sd = NULL;
71 uint32_t rejected_share_access;
72 uint32_t rejected_mask = access_mask;
74 rejected_share_access = access_mask & ~(conn->share_access);
76 if (rejected_share_access) {
77 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
79 (unsigned int)access_mask,
80 smb_fname_str_dbg(smb_fname),
81 (unsigned int)rejected_share_access ));
82 return NT_STATUS_ACCESS_DENIED;
85 if (get_current_uid(conn) == (uid_t)0) {
86 /* I'm sorry sir, I didn't know you were root... */
87 DEBUG(10,("smbd_check_access_rights: root override "
88 "on %s. Granting 0x%x\n",
89 smb_fname_str_dbg(smb_fname),
90 (unsigned int)access_mask ));
94 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
95 DEBUG(10,("smbd_check_access_rights: not checking ACL "
96 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
97 smb_fname_str_dbg(smb_fname),
98 (unsigned int)access_mask ));
102 if (access_mask == DELETE_ACCESS &&
103 VALID_STAT(smb_fname->st) &&
104 S_ISLNK(smb_fname->st.st_ex_mode)) {
105 /* We can always delete a symlink. */
106 DEBUG(10,("smbd_check_access_rights: not checking ACL "
107 "on DELETE_ACCESS on symlink %s.\n",
108 smb_fname_str_dbg(smb_fname) ));
112 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
120 smb_fname_str_dbg(smb_fname),
123 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
131 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
132 * owner WRITE_DAC and READ_CONTROL.
134 status = se_access_check(sd,
135 get_current_nttok(conn),
136 (access_mask & ~FILE_READ_ATTRIBUTES),
139 DEBUG(10,("smbd_check_access_rights: file %s requesting "
140 "0x%x returning 0x%x (%s)\n",
141 smb_fname_str_dbg(smb_fname),
142 (unsigned int)access_mask,
143 (unsigned int)rejected_mask,
144 nt_errstr(status) ));
146 if (!NT_STATUS_IS_OK(status)) {
147 if (DEBUGLEVEL >= 10) {
148 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
149 smb_fname_str_dbg(smb_fname) ));
150 NDR_PRINT_DEBUG(security_descriptor, sd);
156 if (NT_STATUS_IS_OK(status) ||
157 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
161 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
165 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
166 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
167 !lp_store_dos_attributes(SNUM(conn)) &&
168 (lp_map_readonly(SNUM(conn)) ||
169 lp_map_archive(SNUM(conn)) ||
170 lp_map_hidden(SNUM(conn)) ||
171 lp_map_system(SNUM(conn)))) {
172 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
174 DEBUG(10,("smbd_check_access_rights: "
176 "FILE_WRITE_ATTRIBUTES "
178 smb_fname_str_dbg(smb_fname)));
181 if (parent_override_delete(conn,
185 /* Were we trying to do an open
186 * for delete and didn't get DELETE
187 * access (only) ? Check if the
188 * directory allows DELETE_CHILD.
190 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
193 rejected_mask &= ~DELETE_ACCESS;
195 DEBUG(10,("smbd_check_access_rights: "
199 smb_fname_str_dbg(smb_fname)));
202 if (rejected_mask != 0) {
203 return NT_STATUS_ACCESS_DENIED;
209 static NTSTATUS check_parent_access(struct connection_struct *conn,
210 struct smb_filename *smb_fname,
211 uint32_t access_mask,
212 char **pp_parent_dir)
215 char *parent_dir = NULL;
216 struct security_descriptor *parent_sd = NULL;
217 uint32_t access_granted = 0;
219 if (!parent_dirname(talloc_tos(),
220 smb_fname->base_name,
223 return NT_STATUS_NO_MEMORY;
227 *pp_parent_dir = parent_dir;
230 if (get_current_uid(conn) == (uid_t)0) {
231 /* I'm sorry sir, I didn't know you were root... */
232 DEBUG(10,("check_parent_access: root override "
233 "on %s. Granting 0x%x\n",
234 smb_fname_str_dbg(smb_fname),
235 (unsigned int)access_mask ));
239 status = SMB_VFS_GET_NT_ACL(conn,
244 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
246 "%s with error %s\n",
253 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
254 * owner WRITE_DAC and READ_CONTROL.
256 status = se_access_check(parent_sd,
257 get_current_nttok(conn),
258 (access_mask & ~FILE_READ_ATTRIBUTES),
260 if(!NT_STATUS_IS_OK(status)) {
261 DEBUG(5,("check_parent_access: access check "
262 "on directory %s for "
263 "path %s for mask 0x%x returned (0x%x) %s\n",
265 smb_fname->base_name,
268 nt_errstr(status) ));
275 /****************************************************************************
276 fd support routines - attempt to do a dos_open.
277 ****************************************************************************/
279 static NTSTATUS fd_open(struct connection_struct *conn,
284 struct smb_filename *smb_fname = fsp->fsp_name;
285 NTSTATUS status = NT_STATUS_OK;
289 * Never follow symlinks on a POSIX client. The
290 * client should be doing this.
293 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
298 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
299 if (fsp->fh->fd == -1) {
300 status = map_nt_error_from_unix(errno);
301 if (errno == EMFILE) {
302 static time_t last_warned = 0L;
304 if (time((time_t *) NULL) > last_warned) {
305 DEBUG(0,("Too many open files, unable "
306 "to open more! smbd's max "
308 lp_max_open_files()));
309 last_warned = time((time_t *) NULL);
315 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
316 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
317 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
322 /****************************************************************************
323 Close the file associated with a fsp.
324 ****************************************************************************/
326 NTSTATUS fd_close(files_struct *fsp)
333 if (fsp->fh->fd == -1) {
334 return NT_STATUS_OK; /* What we used to call a stat open. */
336 if (fsp->fh->ref_count > 1) {
337 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
340 ret = SMB_VFS_CLOSE(fsp);
343 return map_nt_error_from_unix(errno);
348 /****************************************************************************
349 Change the ownership of a file to that of the parent directory.
350 Do this by fd if possible.
351 ****************************************************************************/
353 void change_file_owner_to_parent(connection_struct *conn,
354 const char *inherit_from_dir,
357 struct smb_filename *smb_fname_parent = NULL;
361 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
362 NULL, NULL, &smb_fname_parent);
363 if (!NT_STATUS_IS_OK(status)) {
367 ret = SMB_VFS_STAT(conn, smb_fname_parent);
369 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
370 "directory %s. Error was %s\n",
371 smb_fname_str_dbg(smb_fname_parent),
373 TALLOC_FREE(smb_fname_parent);
377 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
378 /* Already this uid - no need to change. */
379 DEBUG(10,("change_file_owner_to_parent: file %s "
380 "is already owned by uid %d\n",
382 (int)fsp->fsp_name->st.st_ex_uid ));
383 TALLOC_FREE(smb_fname_parent);
388 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
391 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
392 "file %s to parent directory uid %u. Error "
393 "was %s\n", fsp_str_dbg(fsp),
394 (unsigned int)smb_fname_parent->st.st_ex_uid,
397 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
398 "parent directory uid %u.\n", fsp_str_dbg(fsp),
399 (unsigned int)smb_fname_parent->st.st_ex_uid));
400 /* Ensure the uid entry is updated. */
401 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
404 TALLOC_FREE(smb_fname_parent);
407 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
408 const char *inherit_from_dir,
410 SMB_STRUCT_STAT *psbuf)
412 struct smb_filename *smb_fname_parent = NULL;
413 struct smb_filename *smb_fname_cwd = NULL;
414 char *saved_dir = NULL;
415 TALLOC_CTX *ctx = talloc_tos();
416 NTSTATUS status = NT_STATUS_OK;
419 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
421 if (!NT_STATUS_IS_OK(status)) {
425 ret = SMB_VFS_STAT(conn, smb_fname_parent);
427 status = map_nt_error_from_unix(errno);
428 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
429 "directory %s. Error was %s\n",
430 smb_fname_str_dbg(smb_fname_parent),
435 /* We've already done an lstat into psbuf, and we know it's a
436 directory. If we can cd into the directory and the dev/ino
437 are the same then we can safely chown without races as
438 we're locking the directory in place by being in it. This
439 should work on any UNIX (thanks tridge :-). JRA.
442 saved_dir = vfs_GetWd(ctx,conn);
444 status = map_nt_error_from_unix(errno);
445 DEBUG(0,("change_dir_owner_to_parent: failed to get "
446 "current working directory. Error was %s\n",
451 /* Chdir into the new path. */
452 if (vfs_ChDir(conn, fname) == -1) {
453 status = map_nt_error_from_unix(errno);
454 DEBUG(0,("change_dir_owner_to_parent: failed to change "
455 "current working directory to %s. Error "
456 "was %s\n", fname, strerror(errno) ));
460 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
462 if (!NT_STATUS_IS_OK(status)) {
466 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
468 status = map_nt_error_from_unix(errno);
469 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
470 "directory '.' (%s) Error was %s\n",
471 fname, strerror(errno)));
475 /* Ensure we're pointing at the same place. */
476 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
477 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
478 DEBUG(0,("change_dir_owner_to_parent: "
479 "device/inode on directory %s changed. "
480 "Refusing to chown !\n", fname ));
481 status = NT_STATUS_ACCESS_DENIED;
485 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
486 /* Already this uid - no need to change. */
487 DEBUG(10,("change_dir_owner_to_parent: directory %s "
488 "is already owned by uid %d\n",
490 (int)smb_fname_cwd->st.st_ex_uid ));
491 status = NT_STATUS_OK;
496 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
500 status = map_nt_error_from_unix(errno);
501 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
502 "directory %s to parent directory uid %u. "
503 "Error was %s\n", fname,
504 (unsigned int)smb_fname_parent->st.st_ex_uid,
507 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
508 "directory %s to parent directory uid %u.\n",
509 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
510 /* Ensure the uid entry is updated. */
511 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
515 vfs_ChDir(conn,saved_dir);
517 TALLOC_FREE(smb_fname_parent);
518 TALLOC_FREE(smb_fname_cwd);
522 /****************************************************************************
524 ****************************************************************************/
526 static NTSTATUS open_file(files_struct *fsp,
527 connection_struct *conn,
528 struct smb_request *req,
529 const char *parent_dir,
532 uint32 access_mask, /* client requested access mask. */
533 uint32 open_access_mask) /* what we're actually using in the open. */
535 struct smb_filename *smb_fname = fsp->fsp_name;
536 NTSTATUS status = NT_STATUS_OK;
537 int accmode = (flags & O_ACCMODE);
538 int local_flags = flags;
539 bool file_existed = VALID_STAT(fsp->fsp_name->st);
540 bool file_created = false;
545 /* Check permissions */
548 * This code was changed after seeing a client open request
549 * containing the open mode of (DENY_WRITE/read-only) with
550 * the 'create if not exist' bit set. The previous code
551 * would fail to open the file read only on a read-only share
552 * as it was checking the flags parameter directly against O_RDONLY,
553 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
557 if (!CAN_WRITE(conn)) {
558 /* It's a read-only share - fail if we wanted to write. */
559 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
560 DEBUG(3,("Permission denied opening %s\n",
561 smb_fname_str_dbg(smb_fname)));
562 return NT_STATUS_ACCESS_DENIED;
563 } else if(flags & O_CREAT) {
564 /* We don't want to write - but we must make sure that
565 O_CREAT doesn't create the file if we have write
566 access into the directory.
568 flags &= ~(O_CREAT|O_EXCL);
569 local_flags &= ~(O_CREAT|O_EXCL);
574 * This little piece of insanity is inspired by the
575 * fact that an NT client can open a file for O_RDONLY,
576 * but set the create disposition to FILE_EXISTS_TRUNCATE.
577 * If the client *can* write to the file, then it expects to
578 * truncate the file, even though it is opening for readonly.
579 * Quicken uses this stupid trick in backup file creation...
580 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
581 * for helping track this one down. It didn't bite us in 2.0.x
582 * as we always opened files read-write in that release. JRA.
585 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
586 DEBUG(10,("open_file: truncate requested on read-only open "
587 "for file %s\n", smb_fname_str_dbg(smb_fname)));
588 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
591 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
592 (!file_existed && (local_flags & O_CREAT)) ||
593 ((local_flags & O_TRUNC) == O_TRUNC) ) {
597 * We can't actually truncate here as the file may be locked.
598 * open_file_ntcreate will take care of the truncate later. JRA.
601 local_flags &= ~O_TRUNC;
603 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
605 * We would block on opening a FIFO with no one else on the
606 * other end. Do what we used to do and add O_NONBLOCK to the
610 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
611 local_flags |= O_NONBLOCK;
615 /* Don't create files with Microsoft wildcard characters. */
618 * wildcard characters are allowed in stream names
619 * only test the basefilename
621 wild = fsp->base_fsp->fsp_name->base_name;
623 wild = smb_fname->base_name;
625 if ((local_flags & O_CREAT) && !file_existed &&
627 return NT_STATUS_OBJECT_NAME_INVALID;
630 /* Can we access this file ? */
631 if (!fsp->base_fsp) {
632 /* Only do this check on non-stream open. */
634 status = smbd_check_access_rights(conn,
637 } else if (local_flags & O_CREAT){
638 status = check_parent_access(conn,
643 /* File didn't exist and no O_CREAT. */
644 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
646 if (!NT_STATUS_IS_OK(status)) {
647 DEBUG(10,("open_file: "
651 "smbd_check_access_rights" :
652 "check_parent_access",
653 smb_fname_str_dbg(smb_fname),
654 nt_errstr(status) ));
659 /* Actually do the open */
660 status = fd_open(conn, fsp, local_flags, unx_mode);
661 if (!NT_STATUS_IS_OK(status)) {
662 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
663 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
664 nt_errstr(status),local_flags,flags));
668 if ((local_flags & O_CREAT) && !file_existed) {
673 fsp->fh->fd = -1; /* What we used to call a stat open. */
675 /* File must exist for a stat open. */
676 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
679 status = smbd_check_access_rights(conn,
683 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
685 S_ISLNK(smb_fname->st.st_ex_mode)) {
686 /* This is a POSIX stat open for delete
687 * or rename on a symlink that points
689 DEBUG(10,("open_file: allowing POSIX "
690 "open on bad symlink %s\n",
691 smb_fname_str_dbg(smb_fname)));
692 status = NT_STATUS_OK;
695 if (!NT_STATUS_IS_OK(status)) {
696 DEBUG(10,("open_file: "
697 "smbd_check_access_rights on file "
699 smb_fname_str_dbg(smb_fname),
700 nt_errstr(status) ));
708 if (fsp->fh->fd == -1) {
709 ret = SMB_VFS_STAT(conn, smb_fname);
711 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
712 /* If we have an fd, this stat should succeed. */
714 DEBUG(0,("Error doing fstat on open file %s "
716 smb_fname_str_dbg(smb_fname),
721 /* For a non-io open, this stat failing means file not found. JRA */
723 status = map_nt_error_from_unix(errno);
729 bool need_re_stat = false;
730 /* Do all inheritance work after we've
731 done a successful stat call and filled
732 in the stat struct in fsp->fsp_name. */
734 /* Inherit the ACL if required */
735 if (lp_inherit_perms(SNUM(conn))) {
736 inherit_access_posix_acl(conn, parent_dir,
737 smb_fname->base_name,
742 /* Change the owner if required. */
743 if (lp_inherit_owner(SNUM(conn))) {
744 change_file_owner_to_parent(conn, parent_dir,
750 if (fsp->fh->fd == -1) {
751 ret = SMB_VFS_STAT(conn, smb_fname);
753 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
754 /* If we have an fd, this stat should succeed. */
756 DEBUG(0,("Error doing fstat on open file %s "
758 smb_fname_str_dbg(smb_fname),
764 notify_fname(conn, NOTIFY_ACTION_ADDED,
765 FILE_NOTIFY_CHANGE_FILE_NAME,
766 smb_fname->base_name);
771 * POSIX allows read-only opens of directories. We don't
772 * want to do this (we use a different code path for this)
773 * so catch a directory open and return an EISDIR. JRA.
776 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
779 return NT_STATUS_FILE_IS_A_DIRECTORY;
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->data->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->data->num_share_modes; i++) {
1014 validate_my_share_entries(conn->sconn, i,
1015 &lck->data->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->data->num_share_modes; i++) {
1026 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1030 /* someone else has a share lock on it, check to see if we can
1032 if (share_conflict(&lck->data->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->data->num_share_modes; i++) {
1119 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1123 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1124 is_stat_open(lck->data->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->data->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->data->share_modes[i];
1139 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->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->data->share_modes[i];
1147 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1148 if (*pp_batch || *pp_ex_or_batch) {
1149 smb_panic("Bad levelII oplock entry.");
1154 if (lck->data->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 bool file_has_brlocks(files_struct *fsp)
1209 struct byte_range_lock *br_lck;
1211 br_lck = brl_get_locks_readonly(fsp);
1215 return br_lck->num_locks > 0 ? true : false;
1218 static void grant_fsp_oplock_type(files_struct *fsp,
1220 bool got_level2_oplock,
1221 bool got_a_none_oplock)
1223 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1224 lp_level2_oplocks(SNUM(fsp->conn));
1226 /* Start by granting what the client asked for,
1227 but ensure no SAMBA_PRIVATE bits can be set. */
1228 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1230 if (oplock_request & INTERNAL_OPEN_ONLY) {
1231 /* No oplocks on internal open. */
1232 fsp->oplock_type = NO_OPLOCK;
1233 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1234 fsp->oplock_type, fsp_str_dbg(fsp)));
1236 } else if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1237 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1239 fsp->oplock_type = NO_OPLOCK;
1242 if (is_stat_open(fsp->access_mask)) {
1243 /* Leave the value already set. */
1244 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1245 fsp->oplock_type, fsp_str_dbg(fsp)));
1250 * Match what was requested (fsp->oplock_type) with
1251 * what was found in the existing share modes.
1254 if (got_a_none_oplock) {
1255 fsp->oplock_type = NO_OPLOCK;
1256 } else if (got_level2_oplock) {
1257 if (fsp->oplock_type == NO_OPLOCK ||
1258 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1259 /* Store a level2 oplock, but don't tell the client */
1260 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1262 fsp->oplock_type = LEVEL_II_OPLOCK;
1265 /* All share_mode_entries are placeholders or deferred.
1266 * Silently upgrade to fake levelII if the client didn't
1267 * ask for an oplock. */
1268 if (fsp->oplock_type == NO_OPLOCK) {
1269 /* Store a level2 oplock, but don't tell the client */
1270 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1275 * Don't grant level2 to clients that don't want them
1276 * or if we've turned them off.
1278 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1279 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1282 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1283 fsp->oplock_type, fsp_str_dbg(fsp)));
1286 bool request_timed_out(struct timeval request_time,
1287 struct timeval timeout)
1289 struct timeval now, end_time;
1291 end_time = timeval_sum(&request_time, &timeout);
1292 return (timeval_compare(&end_time, &now) < 0);
1295 /****************************************************************************
1296 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1297 ****************************************************************************/
1299 static void defer_open(struct share_mode_lock *lck,
1300 struct timeval request_time,
1301 struct timeval timeout,
1302 struct smb_request *req,
1303 struct deferred_open_record *state)
1307 /* Paranoia check */
1309 for (i=0; i<lck->data->num_share_modes; i++) {
1310 struct share_mode_entry *e = &lck->data->share_modes[i];
1312 if (is_deferred_open_entry(e) &&
1313 procid_is_me(&e->pid) &&
1314 (e->op_mid == req->mid)) {
1315 DEBUG(0, ("Trying to defer an already deferred "
1316 "request: mid=%llu, exiting\n",
1317 (unsigned long long)req->mid));
1318 exit_server("attempt to defer a deferred request");
1322 /* End paranoia check */
1324 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1325 "open entry for mid %llu\n",
1326 (unsigned int)request_time.tv_sec,
1327 (unsigned int)request_time.tv_usec,
1328 (unsigned long long)req->mid));
1330 if (!push_deferred_open_message_smb(req, request_time, timeout,
1331 state->id, (char *)state, sizeof(*state))) {
1332 exit_server("push_deferred_open_message_smb failed");
1334 add_deferred_open(lck, req->mid, request_time,
1335 messaging_server_id(req->sconn->msg_ctx), state->id);
1339 /****************************************************************************
1340 On overwrite open ensure that the attributes match.
1341 ****************************************************************************/
1343 bool open_match_attributes(connection_struct *conn,
1344 uint32 old_dos_attr,
1345 uint32 new_dos_attr,
1346 mode_t existing_unx_mode,
1347 mode_t new_unx_mode,
1348 mode_t *returned_unx_mode)
1350 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1352 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1353 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1355 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1356 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1357 *returned_unx_mode = new_unx_mode;
1359 *returned_unx_mode = (mode_t)0;
1362 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1363 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1364 "returned_unx_mode = 0%o\n",
1365 (unsigned int)old_dos_attr,
1366 (unsigned int)existing_unx_mode,
1367 (unsigned int)new_dos_attr,
1368 (unsigned int)*returned_unx_mode ));
1370 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1371 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1372 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1373 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1377 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1378 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1379 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1386 /****************************************************************************
1387 Special FCB or DOS processing in the case of a sharing violation.
1388 Try and find a duplicated file handle.
1389 ****************************************************************************/
1391 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1392 connection_struct *conn,
1393 files_struct *fsp_to_dup_into,
1394 const struct smb_filename *smb_fname,
1399 uint32 share_access,
1400 uint32 create_options)
1404 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1405 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1407 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1408 fsp = file_find_di_next(fsp)) {
1410 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1411 "vuid = %u, file_pid = %u, private_options = 0x%x "
1412 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1413 fsp->fh->fd, (unsigned int)fsp->vuid,
1414 (unsigned int)fsp->file_pid,
1415 (unsigned int)fsp->fh->private_options,
1416 (unsigned int)fsp->access_mask ));
1418 if (fsp->fh->fd != -1 &&
1419 fsp->vuid == vuid &&
1420 fsp->file_pid == file_pid &&
1421 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1422 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1423 (fsp->access_mask & FILE_WRITE_DATA) &&
1424 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1425 strequal(fsp->fsp_name->stream_name,
1426 smb_fname->stream_name)) {
1427 DEBUG(10,("fcb_or_dos_open: file match\n"));
1433 return NT_STATUS_NOT_FOUND;
1436 /* quite an insane set of semantics ... */
1437 if (is_executable(smb_fname->base_name) &&
1438 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1439 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1440 return NT_STATUS_INVALID_PARAMETER;
1443 /* We need to duplicate this fsp. */
1444 return dup_file_fsp(req, fsp, access_mask, share_access,
1445 create_options, fsp_to_dup_into);
1448 static void schedule_defer_open(struct share_mode_lock *lck,
1449 struct timeval request_time,
1450 struct smb_request *req)
1452 struct deferred_open_record state;
1454 /* This is a relative time, added to the absolute
1455 request_time value to get the absolute timeout time.
1456 Note that if this is the second or greater time we enter
1457 this codepath for this particular request mid then
1458 request_time is left as the absolute time of the *first*
1459 time this request mid was processed. This is what allows
1460 the request to eventually time out. */
1462 struct timeval timeout;
1464 /* Normally the smbd we asked should respond within
1465 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1466 * the client did, give twice the timeout as a safety
1467 * measure here in case the other smbd is stuck
1468 * somewhere else. */
1470 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1472 /* Nothing actually uses state.delayed_for_oplocks
1473 but it's handy to differentiate in debug messages
1474 between a 30 second delay due to oplock break, and
1475 a 1 second delay for share mode conflicts. */
1477 state.delayed_for_oplocks = True;
1478 state.id = lck->data->id;
1480 if (!request_timed_out(request_time, timeout)) {
1481 defer_open(lck, request_time, timeout, req, &state);
1485 /****************************************************************************
1486 Work out what access_mask to use from what the client sent us.
1487 ****************************************************************************/
1489 static NTSTATUS smbd_calculate_maximum_allowed_access(
1490 connection_struct *conn,
1491 const struct smb_filename *smb_fname,
1492 uint32_t *p_access_mask)
1494 struct security_descriptor *sd;
1495 uint32_t access_granted;
1498 if (get_current_uid(conn) == (uid_t)0) {
1499 *p_access_mask |= FILE_GENERIC_ALL;
1500 return NT_STATUS_OK;
1503 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1508 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1510 * File did not exist
1512 *p_access_mask = FILE_GENERIC_ALL;
1513 return NT_STATUS_OK;
1515 if (!NT_STATUS_IS_OK(status)) {
1516 DEBUG(10,("smbd_calculate_access_mask: "
1517 "Could not get acl on file %s: %s\n",
1518 smb_fname_str_dbg(smb_fname),
1519 nt_errstr(status)));
1520 return NT_STATUS_ACCESS_DENIED;
1524 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1525 * also takes care of owner WRITE_DAC and READ_CONTROL.
1527 status = se_access_check(sd,
1528 get_current_nttok(conn),
1529 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1534 if (!NT_STATUS_IS_OK(status)) {
1535 DEBUG(10, ("smbd_calculate_access_mask: "
1536 "Access denied on file %s: "
1537 "when calculating maximum access\n",
1538 smb_fname_str_dbg(smb_fname)));
1539 return NT_STATUS_ACCESS_DENIED;
1541 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1542 return NT_STATUS_OK;
1545 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1546 const struct smb_filename *smb_fname,
1547 uint32_t access_mask,
1548 uint32_t *access_mask_out)
1551 uint32_t orig_access_mask = access_mask;
1552 uint32_t rejected_share_access;
1555 * Convert GENERIC bits to specific bits.
1558 se_map_generic(&access_mask, &file_generic_mapping);
1560 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1561 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1563 status = smbd_calculate_maximum_allowed_access(
1564 conn, smb_fname, &access_mask);
1566 if (!NT_STATUS_IS_OK(status)) {
1570 access_mask &= conn->share_access;
1573 rejected_share_access = access_mask & ~(conn->share_access);
1575 if (rejected_share_access) {
1576 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1577 "file %s: rejected by share access mask[0x%08X] "
1578 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1579 smb_fname_str_dbg(smb_fname),
1581 orig_access_mask, access_mask,
1582 rejected_share_access));
1583 return NT_STATUS_ACCESS_DENIED;
1586 *access_mask_out = access_mask;
1587 return NT_STATUS_OK;
1590 /****************************************************************************
1591 Remove the deferred open entry under lock.
1592 ****************************************************************************/
1594 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1595 struct server_id pid)
1597 struct share_mode_lock *lck = get_existing_share_mode_lock(
1600 DEBUG(0, ("could not get share mode lock\n"));
1603 del_deferred_open_entry(lck, mid, pid);
1607 /****************************************************************************
1608 Open a file with a share mode. Passed in an already created files_struct *.
1609 ****************************************************************************/
1611 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1612 struct smb_request *req,
1613 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1614 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1615 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1616 uint32 create_options, /* options such as delete on close. */
1617 uint32 new_dos_attributes, /* attributes used for new file. */
1618 int oplock_request, /* internal Samba oplock codes. */
1619 /* Information (FILE_EXISTS etc.) */
1620 uint32_t private_flags, /* Samba specific flags. */
1624 struct smb_filename *smb_fname = fsp->fsp_name;
1627 bool file_existed = VALID_STAT(smb_fname->st);
1628 bool def_acl = False;
1629 bool posix_open = False;
1630 bool new_file_created = False;
1631 bool clear_ads = false;
1632 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1633 mode_t new_unx_mode = (mode_t)0;
1634 mode_t unx_mode = (mode_t)0;
1636 uint32 existing_dos_attributes = 0;
1637 struct timeval request_time = timeval_zero();
1638 struct share_mode_lock *lck = NULL;
1639 uint32 open_access_mask = access_mask;
1643 if (conn->printer) {
1645 * Printers are handled completely differently.
1646 * Most of the passed parameters are ignored.
1650 *pinfo = FILE_WAS_CREATED;
1653 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1654 smb_fname_str_dbg(smb_fname)));
1657 DEBUG(0,("open_file_ntcreate: printer open without "
1658 "an SMB request!\n"));
1659 return NT_STATUS_INTERNAL_ERROR;
1662 return print_spool_open(fsp, smb_fname->base_name,
1666 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1668 return NT_STATUS_NO_MEMORY;
1671 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1673 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1674 new_dos_attributes = 0;
1676 /* Windows allows a new file to be created and
1677 silently removes a FILE_ATTRIBUTE_DIRECTORY
1678 sent by the client. Do the same. */
1680 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1682 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1684 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1685 smb_fname, parent_dir);
1688 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1689 "access_mask=0x%x share_access=0x%x "
1690 "create_disposition = 0x%x create_options=0x%x "
1691 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1692 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1693 access_mask, share_access, create_disposition,
1694 create_options, (unsigned int)unx_mode, oplock_request,
1695 (unsigned int)private_flags));
1697 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1698 DEBUG(0, ("No smb request but not an internal only open!\n"));
1699 return NT_STATUS_INTERNAL_ERROR;
1703 * Only non-internal opens can be deferred at all
1708 if (get_deferred_open_message_state(req,
1712 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1713 /* Remember the absolute time of the original
1714 request with this mid. We'll use it later to
1715 see if this has timed out. */
1717 /* Remove the deferred open entry under lock. */
1718 remove_deferred_open_entry(
1719 state->id, req->mid,
1720 messaging_server_id(req->sconn->msg_ctx));
1722 /* Ensure we don't reprocess this message. */
1723 remove_deferred_open_message_smb(req->sconn, req->mid);
1728 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1730 existing_dos_attributes = dos_mode(conn, smb_fname);
1734 /* ignore any oplock requests if oplocks are disabled */
1735 if (!lp_oplocks(SNUM(conn)) ||
1736 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1737 /* Mask off everything except the private Samba bits. */
1738 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1741 /* this is for OS/2 long file names - say we don't support them */
1742 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1743 /* OS/2 Workplace shell fix may be main code stream in a later
1745 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1747 if (use_nt_status()) {
1748 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1750 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1753 switch( create_disposition ) {
1755 * Currently we're using FILE_SUPERSEDE as the same as
1756 * FILE_OVERWRITE_IF but they really are
1757 * different. FILE_SUPERSEDE deletes an existing file
1758 * (requiring delete access) then recreates it.
1760 case FILE_SUPERSEDE:
1761 /* If file exists replace/overwrite. If file doesn't
1763 flags2 |= (O_CREAT | O_TRUNC);
1767 case FILE_OVERWRITE_IF:
1768 /* If file exists replace/overwrite. If file doesn't
1770 flags2 |= (O_CREAT | O_TRUNC);
1775 /* If file exists open. If file doesn't exist error. */
1776 if (!file_existed) {
1777 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1778 "requested for file %s and file "
1780 smb_fname_str_dbg(smb_fname)));
1782 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1786 case FILE_OVERWRITE:
1787 /* If file exists overwrite. If file doesn't exist
1789 if (!file_existed) {
1790 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1791 "requested for file %s and file "
1793 smb_fname_str_dbg(smb_fname) ));
1795 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1802 /* If file exists error. If file doesn't exist
1805 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1806 "requested for file %s and file "
1807 "already exists.\n",
1808 smb_fname_str_dbg(smb_fname)));
1809 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1814 return map_nt_error_from_unix(errno);
1816 flags2 |= (O_CREAT|O_EXCL);
1820 /* If file exists open. If file doesn't exist
1826 return NT_STATUS_INVALID_PARAMETER;
1829 /* We only care about matching attributes on file exists and
1832 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1833 (create_disposition == FILE_OVERWRITE_IF))) {
1834 if (!open_match_attributes(conn, existing_dos_attributes,
1836 smb_fname->st.st_ex_mode,
1837 unx_mode, &new_unx_mode)) {
1838 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1839 "for file %s (%x %x) (0%o, 0%o)\n",
1840 smb_fname_str_dbg(smb_fname),
1841 existing_dos_attributes,
1843 (unsigned int)smb_fname->st.st_ex_mode,
1844 (unsigned int)unx_mode ));
1846 return NT_STATUS_ACCESS_DENIED;
1850 status = smbd_calculate_access_mask(conn, smb_fname,
1853 if (!NT_STATUS_IS_OK(status)) {
1854 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1855 "on file %s returned %s\n",
1856 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1860 open_access_mask = access_mask;
1862 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1863 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1866 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1867 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1871 * Note that we ignore the append flag as append does not
1872 * mean the same thing under DOS and Unix.
1875 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1876 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1877 /* DENY_DOS opens are always underlying read-write on the
1878 file handle, no matter what the requested access mask
1880 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1881 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1891 * Currently we only look at FILE_WRITE_THROUGH for create options.
1895 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1900 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1904 if (!posix_open && !CAN_WRITE(conn)) {
1906 * We should really return a permission denied error if either
1907 * O_CREAT or O_TRUNC are set, but for compatibility with
1908 * older versions of Samba we just AND them out.
1910 flags2 &= ~(O_CREAT|O_TRUNC);
1914 * Ensure we can't write on a read-only share or file.
1917 if (flags != O_RDONLY && file_existed &&
1918 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1919 DEBUG(5,("open_file_ntcreate: write access requested for "
1920 "file %s on read only %s\n",
1921 smb_fname_str_dbg(smb_fname),
1922 !CAN_WRITE(conn) ? "share" : "file" ));
1924 return NT_STATUS_ACCESS_DENIED;
1927 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1928 fsp->share_access = share_access;
1929 fsp->fh->private_options = private_flags;
1930 fsp->access_mask = open_access_mask; /* We change this to the
1931 * requested access_mask after
1932 * the open is done. */
1933 fsp->posix_open = posix_open;
1935 /* Ensure no SAMBA_PRIVATE bits can be set. */
1936 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1938 if (timeval_is_zero(&request_time)) {
1939 request_time = fsp->open_time;
1943 struct share_mode_entry *batch_entry = NULL;
1944 struct share_mode_entry *exclusive_entry = NULL;
1945 bool got_level2_oplock = false;
1946 bool got_a_none_oplock = false;
1949 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1950 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1952 lck = get_share_mode_lock(talloc_tos(), id,
1954 smb_fname, &old_write_time);
1956 DEBUG(0, ("Could not get share mode lock\n"));
1957 return NT_STATUS_SHARING_VIOLATION;
1960 /* Get the types we need to examine. */
1961 find_oplock_types(fsp,
1967 &got_a_none_oplock);
1969 /* First pass - send break only on batch oplocks. */
1970 if ((req != NULL) &&
1971 delay_for_batch_oplocks(fsp,
1975 schedule_defer_open(lck, request_time, req);
1977 return NT_STATUS_SHARING_VIOLATION;
1980 /* Use the client requested access mask here, not the one we
1982 status = open_mode_check(conn, lck, fsp->name_hash,
1983 access_mask, share_access,
1984 create_options, &file_existed);
1986 if (NT_STATUS_IS_OK(status)) {
1987 /* We might be going to allow this open. Check oplock
1989 /* Second pass - send break for both batch or
1990 * exclusive oplocks. */
1991 if ((req != NULL) &&
1992 delay_for_exclusive_oplocks(
1997 schedule_defer_open(lck, request_time, req);
1999 return NT_STATUS_SHARING_VIOLATION;
2003 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2004 /* DELETE_PENDING is not deferred for a second */
2009 grant_fsp_oplock_type(fsp,
2014 if (!NT_STATUS_IS_OK(status)) {
2015 uint32 can_access_mask;
2016 bool can_access = True;
2018 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2020 /* Check if this can be done with the deny_dos and fcb
2023 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2024 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2026 DEBUG(0, ("DOS open without an SMB "
2029 return NT_STATUS_INTERNAL_ERROR;
2032 /* Use the client requested access mask here,
2033 * not the one we open with. */
2034 status = fcb_or_dos_open(req,
2045 if (NT_STATUS_IS_OK(status)) {
2048 *pinfo = FILE_WAS_OPENED;
2050 return NT_STATUS_OK;
2055 * This next line is a subtlety we need for
2056 * MS-Access. If a file open will fail due to share
2057 * permissions and also for security (access) reasons,
2058 * we need to return the access failed error, not the
2059 * share error. We can't open the file due to kernel
2060 * oplock deadlock (it's possible we failed above on
2061 * the open_mode_check()) so use a userspace check.
2064 if (flags & O_RDWR) {
2065 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2066 } else if (flags & O_WRONLY) {
2067 can_access_mask = FILE_WRITE_DATA;
2069 can_access_mask = FILE_READ_DATA;
2072 if (((can_access_mask & FILE_WRITE_DATA) &&
2073 !CAN_WRITE(conn)) ||
2074 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2075 smb_fname, can_access_mask))) {
2080 * If we're returning a share violation, ensure we
2081 * cope with the braindead 1 second delay.
2084 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2085 lp_defer_sharing_violations()) {
2086 struct timeval timeout;
2087 struct deferred_open_record state;
2090 /* this is a hack to speed up torture tests
2092 timeout_usecs = lp_parm_int(SNUM(conn),
2093 "smbd","sharedelay",
2094 SHARING_VIOLATION_USEC_WAIT);
2096 /* This is a relative time, added to the absolute
2097 request_time value to get the absolute timeout time.
2098 Note that if this is the second or greater time we enter
2099 this codepath for this particular request mid then
2100 request_time is left as the absolute time of the *first*
2101 time this request mid was processed. This is what allows
2102 the request to eventually time out. */
2104 timeout = timeval_set(0, timeout_usecs);
2106 /* Nothing actually uses state.delayed_for_oplocks
2107 but it's handy to differentiate in debug messages
2108 between a 30 second delay due to oplock break, and
2109 a 1 second delay for share mode conflicts. */
2111 state.delayed_for_oplocks = False;
2115 && !request_timed_out(request_time,
2117 defer_open(lck, request_time, timeout,
2125 * We have detected a sharing violation here
2126 * so return the correct error code
2128 status = NT_STATUS_SHARING_VIOLATION;
2130 status = NT_STATUS_ACCESS_DENIED;
2136 * We exit this block with the share entry *locked*.....
2140 SMB_ASSERT(!file_existed || (lck != NULL));
2143 * Ensure we pay attention to default ACLs on directories if required.
2146 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2147 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2148 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2151 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2152 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2153 (unsigned int)flags, (unsigned int)flags2,
2154 (unsigned int)unx_mode, (unsigned int)access_mask,
2155 (unsigned int)open_access_mask));
2158 * open_file strips any O_TRUNC flags itself.
2161 fsp_open = open_file(fsp, conn, req, parent_dir,
2162 flags|flags2, unx_mode, access_mask,
2165 if (!NT_STATUS_IS_OK(fsp_open)) {
2170 if (!file_existed) {
2171 struct share_mode_entry *batch_entry = NULL;
2172 struct share_mode_entry *exclusive_entry = NULL;
2173 bool got_level2_oplock = false;
2174 bool got_a_none_oplock = false;
2175 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2178 * Deal with the race condition where two smbd's detect the
2179 * file doesn't exist and do the create at the same time. One
2180 * of them will win and set a share mode, the other (ie. this
2181 * one) should check if the requested share mode for this
2182 * create is allowed.
2186 * Now the file exists and fsp is successfully opened,
2187 * fsp->dev and fsp->inode are valid and should replace the
2188 * dev=0,inode=0 from a non existent file. Spotted by
2189 * Nadav Danieli <nadavd@exanet.com>. JRA.
2194 lck = get_share_mode_lock(talloc_tos(), id,
2196 smb_fname, &old_write_time);
2199 DEBUG(0, ("open_file_ntcreate: Could not get share "
2200 "mode lock for %s\n",
2201 smb_fname_str_dbg(smb_fname)));
2203 return NT_STATUS_SHARING_VIOLATION;
2206 /* Get the types we need to examine. */
2207 find_oplock_types(fsp,
2213 &got_a_none_oplock);
2215 /* First pass - send break only on batch oplocks. */
2216 if ((req != NULL) &&
2217 delay_for_batch_oplocks(fsp,
2221 schedule_defer_open(lck, request_time, req);
2224 return NT_STATUS_SHARING_VIOLATION;
2227 status = open_mode_check(conn, lck, fsp->name_hash,
2228 access_mask, share_access,
2229 create_options, &file_existed);
2231 if (NT_STATUS_IS_OK(status)) {
2232 /* We might be going to allow this open. Check oplock
2234 /* Second pass - send break for both batch or
2235 * exclusive oplocks. */
2236 if ((req != NULL) &&
2237 delay_for_exclusive_oplocks(
2242 schedule_defer_open(lck, request_time, req);
2245 return NT_STATUS_SHARING_VIOLATION;
2249 if (!NT_STATUS_IS_OK(status)) {
2250 struct deferred_open_record state;
2252 state.delayed_for_oplocks = False;
2255 /* Do it all over again immediately. In the second
2256 * round we will find that the file existed and handle
2257 * the DELETE_PENDING and FCB cases correctly. No need
2258 * to duplicate the code here. Essentially this is a
2259 * "goto top of this function", but don't tell
2263 defer_open(lck, request_time, timeval_zero(),
2271 grant_fsp_oplock_type(fsp,
2277 * We exit this block with the share entry *locked*.....
2282 SMB_ASSERT(lck != NULL);
2284 /* Delete streams if create_disposition requires it */
2285 if (file_existed && clear_ads &&
2286 !is_ntfs_stream_smb_fname(smb_fname)) {
2287 status = delete_all_streams(conn, smb_fname->base_name);
2288 if (!NT_STATUS_IS_OK(status)) {
2295 /* note that we ignore failure for the following. It is
2296 basically a hack for NFS, and NFS will never set one of
2297 these only read them. Nobody but Samba can ever set a deny
2298 mode and we have already checked our more authoritative
2299 locking database for permission to set this deny mode. If
2300 the kernel refuses the operations then the kernel is wrong.
2301 note that GPFS supports it as well - jmcd */
2303 if (fsp->fh->fd != -1) {
2305 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2306 if(ret_flock == -1 ){
2311 return NT_STATUS_SHARING_VIOLATION;
2316 * At this point onwards, we can guarentee that the share entry
2317 * is locked, whether we created the file or not, and that the
2318 * deny mode is compatible with all current opens.
2322 * If requested, truncate the file.
2325 if (file_existed && (flags2&O_TRUNC)) {
2327 * We are modifing the file after open - update the stat
2330 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2331 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2332 status = map_nt_error_from_unix(errno);
2340 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2341 * but we don't have to store this - just ignore it on access check.
2343 if (conn->sconn->using_smb2) {
2345 * SMB2 doesn't return it (according to Microsoft tests).
2346 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2347 * File created with access = 0x7 (Read, Write, Delete)
2348 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2350 fsp->access_mask = access_mask;
2352 /* But SMB1 does. */
2353 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2357 /* stat opens on existing files don't get oplocks. */
2358 if (is_stat_open(open_access_mask)) {
2359 fsp->oplock_type = NO_OPLOCK;
2362 if (flags2 & O_TRUNC) {
2363 info = FILE_WAS_OVERWRITTEN;
2365 info = FILE_WAS_OPENED;
2368 info = FILE_WAS_CREATED;
2376 * Setup the oplock info in both the shared memory and
2380 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2382 * Could not get the kernel oplock or there are byte-range
2383 * locks on the file.
2385 fsp->oplock_type = NO_OPLOCK;
2388 set_share_mode(lck, fsp, get_current_uid(conn),
2392 /* Handle strange delete on close create semantics. */
2393 if (create_options & FILE_DELETE_ON_CLOSE) {
2395 status = can_set_delete_on_close(fsp, new_dos_attributes);
2397 if (!NT_STATUS_IS_OK(status)) {
2398 /* Remember to delete the mode we just added. */
2399 del_share_mode(lck, fsp);
2404 /* Note that here we set the *inital* delete on close flag,
2405 not the regular one. The magic gets handled in close. */
2406 fsp->initial_delete_on_close = True;
2409 if (info == FILE_WAS_OVERWRITTEN
2410 || info == FILE_WAS_CREATED
2411 || info == FILE_WAS_SUPERSEDED) {
2412 new_file_created = True;
2415 if (new_file_created) {
2416 /* Files should be initially set as archive */
2417 if (lp_map_archive(SNUM(conn)) ||
2418 lp_store_dos_attributes(SNUM(conn))) {
2420 if (file_set_dosmode(conn, smb_fname,
2421 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2422 parent_dir, true) == 0) {
2423 unx_mode = smb_fname->st.st_ex_mode;
2429 /* Determine sparse flag. */
2431 /* POSIX opens are sparse by default. */
2432 fsp->is_sparse = true;
2434 fsp->is_sparse = (file_existed &&
2435 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2439 * Take care of inherited ACLs on created files - if default ACL not
2443 if (!posix_open && !file_existed && !def_acl) {
2445 int saved_errno = errno; /* We might get ENOSYS in the next
2448 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2450 errno = saved_errno; /* Ignore ENOSYS */
2453 } else if (new_unx_mode) {
2457 /* Attributes need changing. File already existed. */
2460 int saved_errno = errno; /* We might get ENOSYS in the
2462 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2464 if (ret == -1 && errno == ENOSYS) {
2465 errno = saved_errno; /* Ignore ENOSYS */
2467 DEBUG(5, ("open_file_ntcreate: reset "
2468 "attributes of file %s to 0%o\n",
2469 smb_fname_str_dbg(smb_fname),
2470 (unsigned int)new_unx_mode));
2471 ret = 0; /* Don't do the fchmod below. */
2476 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2477 DEBUG(5, ("open_file_ntcreate: failed to reset "
2478 "attributes of file %s to 0%o\n",
2479 smb_fname_str_dbg(smb_fname),
2480 (unsigned int)new_unx_mode));
2483 /* If this is a successful open, we must remove any deferred open
2486 del_deferred_open_entry(lck, req->mid,
2487 messaging_server_id(req->sconn->msg_ctx));
2491 return NT_STATUS_OK;
2495 /****************************************************************************
2496 Open a file for for write to ensure that we can fchmod it.
2497 ****************************************************************************/
2499 NTSTATUS open_file_fchmod(connection_struct *conn,
2500 struct smb_filename *smb_fname,
2501 files_struct **result)
2503 if (!VALID_STAT(smb_fname->st)) {
2504 return NT_STATUS_INVALID_PARAMETER;
2507 return SMB_VFS_CREATE_FILE(
2510 0, /* root_dir_fid */
2511 smb_fname, /* fname */
2512 FILE_WRITE_DATA, /* access_mask */
2513 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2515 FILE_OPEN, /* create_disposition*/
2516 0, /* create_options */
2517 0, /* file_attributes */
2518 INTERNAL_OPEN_ONLY, /* oplock_request */
2519 0, /* allocation_size */
2520 0, /* private_flags */
2523 result, /* result */
2527 static NTSTATUS mkdir_internal(connection_struct *conn,
2528 struct smb_filename *smb_dname,
2529 uint32 file_attributes)
2532 char *parent_dir = NULL;
2534 bool posix_open = false;
2535 bool need_re_stat = false;
2536 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2538 if(access_mask & ~(conn->share_access)) {
2539 DEBUG(5,("mkdir_internal: failing share access "
2540 "%s\n", lp_servicename(SNUM(conn))));
2541 return NT_STATUS_ACCESS_DENIED;
2544 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2546 return NT_STATUS_NO_MEMORY;
2549 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2551 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2553 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2556 status = check_parent_access(conn,
2560 if(!NT_STATUS_IS_OK(status)) {
2561 DEBUG(5,("mkdir_internal: check_parent_access "
2562 "on directory %s for path %s returned %s\n",
2564 smb_dname->base_name,
2565 nt_errstr(status) ));
2569 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2570 return map_nt_error_from_unix(errno);
2573 /* Ensure we're checking for a symlink here.... */
2574 /* We don't want to get caught by a symlink racer. */
2576 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2577 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2578 smb_fname_str_dbg(smb_dname), strerror(errno)));
2579 return map_nt_error_from_unix(errno);
2582 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2583 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2584 smb_fname_str_dbg(smb_dname)));
2585 return NT_STATUS_NOT_A_DIRECTORY;
2588 if (lp_store_dos_attributes(SNUM(conn))) {
2590 file_set_dosmode(conn, smb_dname,
2591 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2596 if (lp_inherit_perms(SNUM(conn))) {
2597 inherit_access_posix_acl(conn, parent_dir,
2598 smb_dname->base_name, mode);
2599 need_re_stat = true;
2604 * Check if high bits should have been set,
2605 * then (if bits are missing): add them.
2606 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2609 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2610 (mode & ~smb_dname->st.st_ex_mode)) {
2611 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2612 (smb_dname->st.st_ex_mode |
2613 (mode & ~smb_dname->st.st_ex_mode)));
2614 need_re_stat = true;
2618 /* Change the owner if required. */
2619 if (lp_inherit_owner(SNUM(conn))) {
2620 change_dir_owner_to_parent(conn, parent_dir,
2621 smb_dname->base_name,
2623 need_re_stat = true;
2627 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2628 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2629 smb_fname_str_dbg(smb_dname), strerror(errno)));
2630 return map_nt_error_from_unix(errno);
2634 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2635 smb_dname->base_name);
2637 return NT_STATUS_OK;
2640 /****************************************************************************
2641 Ensure we didn't get symlink raced on opening a directory.
2642 ****************************************************************************/
2644 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2645 const SMB_STRUCT_STAT *sbuf2)
2647 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2648 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2649 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2650 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2656 /****************************************************************************
2657 Open a directory from an NT SMB call.
2658 ****************************************************************************/
2660 static NTSTATUS open_directory(connection_struct *conn,
2661 struct smb_request *req,
2662 struct smb_filename *smb_dname,
2664 uint32 share_access,
2665 uint32 create_disposition,
2666 uint32 create_options,
2667 uint32 file_attributes,
2669 files_struct **result)
2671 files_struct *fsp = NULL;
2672 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2673 struct share_mode_lock *lck = NULL;
2675 struct timespec mtimespec;
2678 if (is_ntfs_stream_smb_fname(smb_dname)) {
2679 DEBUG(2, ("open_directory: %s is a stream name!\n",
2680 smb_fname_str_dbg(smb_dname)));
2681 return NT_STATUS_NOT_A_DIRECTORY;
2684 /* Ensure we have a directory attribute. */
2685 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2687 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2688 "share_access = 0x%x create_options = 0x%x, "
2689 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2690 smb_fname_str_dbg(smb_dname),
2691 (unsigned int)access_mask,
2692 (unsigned int)share_access,
2693 (unsigned int)create_options,
2694 (unsigned int)create_disposition,
2695 (unsigned int)file_attributes));
2697 status = smbd_calculate_access_mask(conn, smb_dname,
2698 access_mask, &access_mask);
2699 if (!NT_STATUS_IS_OK(status)) {
2700 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2701 "on file %s returned %s\n",
2702 smb_fname_str_dbg(smb_dname),
2703 nt_errstr(status)));
2707 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2708 !security_token_has_privilege(get_current_nttok(conn),
2709 SEC_PRIV_SECURITY)) {
2710 DEBUG(10, ("open_directory: open on %s "
2711 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2712 smb_fname_str_dbg(smb_dname)));
2713 return NT_STATUS_PRIVILEGE_NOT_HELD;
2716 switch( create_disposition ) {
2720 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2723 info = FILE_WAS_OPENED;
2728 /* If directory exists error. If directory doesn't
2732 status = NT_STATUS_OBJECT_NAME_COLLISION;
2733 DEBUG(2, ("open_directory: unable to create "
2734 "%s. Error was %s\n",
2735 smb_fname_str_dbg(smb_dname),
2736 nt_errstr(status)));
2740 status = mkdir_internal(conn, smb_dname,
2743 if (!NT_STATUS_IS_OK(status)) {
2744 DEBUG(2, ("open_directory: unable to create "
2745 "%s. Error was %s\n",
2746 smb_fname_str_dbg(smb_dname),
2747 nt_errstr(status)));
2751 info = FILE_WAS_CREATED;
2756 * If directory exists open. If directory doesn't
2761 status = NT_STATUS_OK;
2762 info = FILE_WAS_OPENED;
2764 status = mkdir_internal(conn, smb_dname,
2767 if (NT_STATUS_IS_OK(status)) {
2768 info = FILE_WAS_CREATED;
2770 /* Cope with create race. */
2771 if (!NT_STATUS_EQUAL(status,
2772 NT_STATUS_OBJECT_NAME_COLLISION)) {
2773 DEBUG(2, ("open_directory: unable to create "
2774 "%s. Error was %s\n",
2775 smb_fname_str_dbg(smb_dname),
2776 nt_errstr(status)));
2779 info = FILE_WAS_OPENED;
2785 case FILE_SUPERSEDE:
2786 case FILE_OVERWRITE:
2787 case FILE_OVERWRITE_IF:
2789 DEBUG(5,("open_directory: invalid create_disposition "
2790 "0x%x for directory %s\n",
2791 (unsigned int)create_disposition,
2792 smb_fname_str_dbg(smb_dname)));
2793 return NT_STATUS_INVALID_PARAMETER;
2796 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2797 DEBUG(5,("open_directory: %s is not a directory !\n",
2798 smb_fname_str_dbg(smb_dname)));
2799 return NT_STATUS_NOT_A_DIRECTORY;
2802 if (info == FILE_WAS_OPENED) {
2803 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2804 if (!NT_STATUS_IS_OK(status)) {
2805 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2806 "file %s failed with %s\n",
2807 smb_fname_str_dbg(smb_dname),
2808 nt_errstr(status)));
2813 status = file_new(req, conn, &fsp);
2814 if(!NT_STATUS_IS_OK(status)) {
2819 * Setup the files_struct for it.
2822 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2823 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2824 fsp->file_pid = req ? req->smbpid : 0;
2825 fsp->can_lock = False;
2826 fsp->can_read = False;
2827 fsp->can_write = False;
2829 fsp->share_access = share_access;
2830 fsp->fh->private_options = 0;
2832 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2834 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2835 fsp->print_file = NULL;
2836 fsp->modified = False;
2837 fsp->oplock_type = NO_OPLOCK;
2838 fsp->sent_oplock_break = NO_BREAK_SENT;
2839 fsp->is_directory = True;
2840 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2841 status = fsp_set_smb_fname(fsp, smb_dname);
2842 if (!NT_STATUS_IS_OK(status)) {
2843 file_free(req, fsp);
2847 mtimespec = smb_dname->st.st_ex_mtime;
2850 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2852 /* POSIX allows us to open a directory with O_RDONLY. */
2853 status = fd_open(conn, fsp, O_RDONLY, 0);
2855 if (!NT_STATUS_IS_OK(status)) {
2856 DEBUG(5, ("open_directory: Could not open fd for "
2858 smb_fname_str_dbg(smb_dname),
2859 nt_errstr(status)));
2860 file_free(req, fsp);
2864 status = vfs_stat_fsp(fsp);
2865 if (!NT_STATUS_IS_OK(status)) {
2867 file_free(req, fsp);
2871 /* Ensure there was no race condition. */
2872 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2873 DEBUG(5,("open_directory: stat struct differs for "
2875 smb_fname_str_dbg(smb_dname)));
2877 file_free(req, fsp);
2878 return NT_STATUS_ACCESS_DENIED;
2881 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2882 conn->connectpath, smb_dname,
2886 DEBUG(0, ("open_directory: Could not get share mode lock for "
2887 "%s\n", smb_fname_str_dbg(smb_dname)));
2889 file_free(req, fsp);
2890 return NT_STATUS_SHARING_VIOLATION;
2893 status = open_mode_check(conn, lck, fsp->name_hash,
2894 access_mask, share_access,
2895 create_options, &dir_existed);
2897 if (!NT_STATUS_IS_OK(status)) {
2900 file_free(req, fsp);
2904 set_share_mode(lck, fsp, get_current_uid(conn),
2905 req ? req->mid : 0, NO_OPLOCK);
2907 /* For directories the delete on close bit at open time seems
2908 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2909 if (create_options & FILE_DELETE_ON_CLOSE) {
2910 status = can_set_delete_on_close(fsp, 0);
2911 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2914 file_free(req, fsp);
2918 if (NT_STATUS_IS_OK(status)) {
2919 /* Note that here we set the *inital* delete on close flag,
2920 not the regular one. The magic gets handled in close. */
2921 fsp->initial_delete_on_close = True;
2932 return NT_STATUS_OK;
2935 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2936 struct smb_filename *smb_dname)
2941 status = SMB_VFS_CREATE_FILE(
2944 0, /* root_dir_fid */
2945 smb_dname, /* fname */
2946 FILE_READ_ATTRIBUTES, /* access_mask */
2947 FILE_SHARE_NONE, /* share_access */
2948 FILE_CREATE, /* create_disposition*/
2949 FILE_DIRECTORY_FILE, /* create_options */
2950 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2951 0, /* oplock_request */
2952 0, /* allocation_size */
2953 0, /* private_flags */
2959 if (NT_STATUS_IS_OK(status)) {
2960 close_file(req, fsp, NORMAL_CLOSE);
2966 /****************************************************************************
2967 Receive notification that one of our open files has been renamed by another
2969 ****************************************************************************/
2971 void msg_file_was_renamed(struct messaging_context *msg,
2974 struct server_id server_id,
2978 char *frm = (char *)data->data;
2980 const char *sharepath;
2981 const char *base_name;
2982 const char *stream_name;
2983 struct smb_filename *smb_fname = NULL;
2984 size_t sp_len, bn_len;
2986 struct smbd_server_connection *sconn =
2987 talloc_get_type_abort(private_data,
2988 struct smbd_server_connection);
2990 if (data->data == NULL
2991 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2992 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2993 (int)data->length));
2997 /* Unpack the message. */
2998 pull_file_id_24(frm, &id);
2999 sharepath = &frm[24];
3000 sp_len = strlen(sharepath);
3001 base_name = sharepath + sp_len + 1;
3002 bn_len = strlen(base_name);
3003 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3005 /* stream_name must always be NULL if there is no stream. */
3006 if (stream_name[0] == '\0') {
3010 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3011 stream_name, NULL, &smb_fname);
3012 if (!NT_STATUS_IS_OK(status)) {
3016 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3018 sharepath, smb_fname_str_dbg(smb_fname),
3019 file_id_string_tos(&id)));
3021 for(fsp = file_find_di_first(sconn, id); fsp;
3022 fsp = file_find_di_next(fsp)) {
3023 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3025 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3026 fsp->fnum, fsp_str_dbg(fsp),
3027 smb_fname_str_dbg(smb_fname)));
3028 status = fsp_set_smb_fname(fsp, smb_fname);
3029 if (!NT_STATUS_IS_OK(status)) {
3034 /* Now we have the complete path we can work out if this is
3035 actually within this share and adjust newname accordingly. */
3036 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3037 "not sharepath %s) "
3038 "fnum %d from %s -> %s\n",
3039 fsp->conn->connectpath,
3043 smb_fname_str_dbg(smb_fname)));
3047 TALLOC_FREE(smb_fname);
3052 * If a main file is opened for delete, all streams need to be checked for
3053 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3054 * If that works, delete them all by setting the delete on close and close.
3057 NTSTATUS open_streams_for_delete(connection_struct *conn,
3060 struct stream_struct *stream_info = NULL;
3061 files_struct **streams = NULL;
3063 unsigned int num_streams = 0;
3064 TALLOC_CTX *frame = talloc_stackframe();
3067 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3068 &num_streams, &stream_info);
3070 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3071 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3072 DEBUG(10, ("no streams around\n"));
3074 return NT_STATUS_OK;
3077 if (!NT_STATUS_IS_OK(status)) {
3078 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3079 nt_errstr(status)));
3083 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3086 if (num_streams == 0) {
3088 return NT_STATUS_OK;
3091 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3092 if (streams == NULL) {
3093 DEBUG(0, ("talloc failed\n"));
3094 status = NT_STATUS_NO_MEMORY;
3098 for (i=0; i<num_streams; i++) {
3099 struct smb_filename *smb_fname = NULL;
3101 if (strequal(stream_info[i].name, "::$DATA")) {
3106 status = create_synthetic_smb_fname(talloc_tos(), fname,
3107 stream_info[i].name,
3109 if (!NT_STATUS_IS_OK(status)) {
3113 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3114 DEBUG(10, ("Unable to stat stream: %s\n",
3115 smb_fname_str_dbg(smb_fname)));
3118 status = SMB_VFS_CREATE_FILE(
3121 0, /* root_dir_fid */
3122 smb_fname, /* fname */
3123 DELETE_ACCESS, /* access_mask */
3124 (FILE_SHARE_READ | /* share_access */
3125 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3126 FILE_OPEN, /* create_disposition*/
3127 0, /* create_options */
3128 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3129 0, /* oplock_request */
3130 0, /* allocation_size */
3131 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3134 &streams[i], /* result */
3137 if (!NT_STATUS_IS_OK(status)) {
3138 DEBUG(10, ("Could not open stream %s: %s\n",
3139 smb_fname_str_dbg(smb_fname),
3140 nt_errstr(status)));
3142 TALLOC_FREE(smb_fname);
3145 TALLOC_FREE(smb_fname);
3149 * don't touch the variable "status" beyond this point :-)
3152 for (i -= 1 ; i >= 0; i--) {
3153 if (streams[i] == NULL) {
3157 DEBUG(10, ("Closing stream # %d, %s\n", i,
3158 fsp_str_dbg(streams[i])));
3159 close_file(NULL, streams[i], NORMAL_CLOSE);
3167 /*********************************************************************
3168 Create a default ACL by inheriting from the parent. If no inheritance
3169 from the parent available, don't set anything. This will leave the actual
3170 permissions the new file or directory already got from the filesystem
3171 as the NT ACL when read.
3172 *********************************************************************/
3174 static NTSTATUS inherit_new_acl(files_struct *fsp)
3176 TALLOC_CTX *ctx = talloc_tos();
3177 char *parent_name = NULL;
3178 struct security_descriptor *parent_desc = NULL;
3179 NTSTATUS status = NT_STATUS_OK;
3180 struct security_descriptor *psd = NULL;
3181 struct dom_sid *owner_sid = NULL;
3182 struct dom_sid *group_sid = NULL;
3183 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3184 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3185 bool inheritable_components = false;
3188 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3189 return NT_STATUS_NO_MEMORY;
3192 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3194 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3196 if (!NT_STATUS_IS_OK(status)) {
3200 inheritable_components = sd_has_inheritable_components(parent_desc,
3203 if (!inheritable_components && !inherit_owner) {
3204 /* Nothing to inherit and not setting owner. */
3205 return NT_STATUS_OK;
3208 /* Create an inherited descriptor from the parent. */
3210 if (DEBUGLEVEL >= 10) {
3211 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3212 fsp_str_dbg(fsp) ));
3213 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3216 /* Inherit from parent descriptor if "inherit owner" set. */
3217 if (inherit_owner) {
3218 owner_sid = parent_desc->owner_sid;
3219 group_sid = parent_desc->group_sid;
3222 if (owner_sid == NULL) {
3223 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3225 if (group_sid == NULL) {
3226 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3229 status = se_create_child_secdesc(ctx,
3236 if (!NT_STATUS_IS_OK(status)) {
3240 /* If inheritable_components == false,
3241 se_create_child_secdesc()
3242 creates a security desriptor with a NULL dacl
3243 entry, but with SEC_DESC_DACL_PRESENT. We need
3244 to remove that flag. */
3246 if (!inheritable_components) {
3247 security_info_sent &= ~SECINFO_DACL;
3248 psd->type &= ~SEC_DESC_DACL_PRESENT;
3251 if (DEBUGLEVEL >= 10) {
3252 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3253 fsp_str_dbg(fsp) ));
3254 NDR_PRINT_DEBUG(security_descriptor, psd);
3257 if (inherit_owner) {
3258 /* We need to be root to force this. */
3261 status = SMB_VFS_FSET_NT_ACL(fsp,
3264 if (inherit_owner) {
3271 * Wrapper around open_file_ntcreate and open_directory
3274 static NTSTATUS create_file_unixpath(connection_struct *conn,
3275 struct smb_request *req,
3276 struct smb_filename *smb_fname,
3277 uint32_t access_mask,
3278 uint32_t share_access,
3279 uint32_t create_disposition,
3280 uint32_t create_options,
3281 uint32_t file_attributes,
3282 uint32_t oplock_request,
3283 uint64_t allocation_size,
3284 uint32_t private_flags,
3285 struct security_descriptor *sd,
3286 struct ea_list *ea_list,
3288 files_struct **result,
3291 int info = FILE_WAS_OPENED;
3292 files_struct *base_fsp = NULL;
3293 files_struct *fsp = NULL;
3296 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3297 "file_attributes = 0x%x, share_access = 0x%x, "
3298 "create_disposition = 0x%x create_options = 0x%x "
3299 "oplock_request = 0x%x private_flags = 0x%x "
3300 "ea_list = 0x%p, sd = 0x%p, "
3302 (unsigned int)access_mask,
3303 (unsigned int)file_attributes,
3304 (unsigned int)share_access,
3305 (unsigned int)create_disposition,
3306 (unsigned int)create_options,
3307 (unsigned int)oplock_request,
3308 (unsigned int)private_flags,
3309 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3311 if (create_options & FILE_OPEN_BY_FILE_ID) {
3312 status = NT_STATUS_NOT_SUPPORTED;
3316 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3317 status = NT_STATUS_INVALID_PARAMETER;
3322 oplock_request |= INTERNAL_OPEN_ONLY;
3325 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3326 && (access_mask & DELETE_ACCESS)
3327 && !is_ntfs_stream_smb_fname(smb_fname)) {
3329 * We can't open a file with DELETE access if any of the
3330 * streams is open without FILE_SHARE_DELETE
3332 status = open_streams_for_delete(conn, smb_fname->base_name);
3334 if (!NT_STATUS_IS_OK(status)) {
3339 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3340 !security_token_has_privilege(get_current_nttok(conn),
3341 SEC_PRIV_SECURITY)) {
3342 DEBUG(10, ("create_file_unixpath: open on %s "
3343 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3344 smb_fname_str_dbg(smb_fname)));
3345 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3349 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3350 && is_ntfs_stream_smb_fname(smb_fname)
3351 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3352 uint32 base_create_disposition;
3353 struct smb_filename *smb_fname_base = NULL;
3355 if (create_options & FILE_DIRECTORY_FILE) {
3356 status = NT_STATUS_NOT_A_DIRECTORY;
3360 switch (create_disposition) {
3362 base_create_disposition = FILE_OPEN;
3365 base_create_disposition = FILE_OPEN_IF;
3369 /* Create an smb_filename with stream_name == NULL. */
3370 status = create_synthetic_smb_fname(talloc_tos(),
3371 smb_fname->base_name,
3374 if (!NT_STATUS_IS_OK(status)) {
3378 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3379 DEBUG(10, ("Unable to stat stream: %s\n",
3380 smb_fname_str_dbg(smb_fname_base)));
3383 /* Open the base file. */
3384 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3387 | FILE_SHARE_DELETE,
3388 base_create_disposition,
3389 0, 0, 0, 0, 0, NULL, NULL,
3391 TALLOC_FREE(smb_fname_base);
3393 if (!NT_STATUS_IS_OK(status)) {
3394 DEBUG(10, ("create_file_unixpath for base %s failed: "
3395 "%s\n", smb_fname->base_name,
3396 nt_errstr(status)));
3399 /* we don't need to low level fd */
3404 * If it's a request for a directory open, deal with it separately.
3407 if (create_options & FILE_DIRECTORY_FILE) {
3409 if (create_options & FILE_NON_DIRECTORY_FILE) {
3410 status = NT_STATUS_INVALID_PARAMETER;
3414 /* Can't open a temp directory. IFS kit test. */
3415 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3416 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3417 status = NT_STATUS_INVALID_PARAMETER;
3422 * We will get a create directory here if the Win32
3423 * app specified a security descriptor in the
3424 * CreateDirectory() call.
3428 status = open_directory(
3429 conn, req, smb_fname, access_mask, share_access,
3430 create_disposition, create_options, file_attributes,
3435 * Ordinary file case.
3438 status = file_new(req, conn, &fsp);
3439 if(!NT_STATUS_IS_OK(status)) {
3443 status = fsp_set_smb_fname(fsp, smb_fname);
3444 if (!NT_STATUS_IS_OK(status)) {
3449 * We're opening the stream element of a base_fsp
3450 * we already opened. Set up the base_fsp pointer.
3453 fsp->base_fsp = base_fsp;
3456 status = open_file_ntcreate(conn,
3468 if(!NT_STATUS_IS_OK(status)) {
3469 file_free(req, fsp);
3473 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3475 /* A stream open never opens a directory */
3478 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3483 * Fail the open if it was explicitly a non-directory
3487 if (create_options & FILE_NON_DIRECTORY_FILE) {
3488 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3493 status = open_directory(
3494 conn, req, smb_fname, access_mask,
3495 share_access, create_disposition,
3496 create_options, file_attributes,
3501 if (!NT_STATUS_IS_OK(status)) {
3505 fsp->base_fsp = base_fsp;
3507 if ((ea_list != NULL) &&
3508 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3509 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3510 if (!NT_STATUS_IS_OK(status)) {
3515 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3516 status = NT_STATUS_ACCESS_DENIED;
3520 /* Save the requested allocation size. */
3521 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3523 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3524 fsp->initial_allocation_size = smb_roundup(
3525 fsp->conn, allocation_size);
3526 if (fsp->is_directory) {
3527 /* Can't set allocation size on a directory. */
3528 status = NT_STATUS_ACCESS_DENIED;
3531 if (vfs_allocate_file_space(
3532 fsp, fsp->initial_allocation_size) == -1) {
3533 status = NT_STATUS_DISK_FULL;
3537 fsp->initial_allocation_size = smb_roundup(
3538 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3542 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3543 fsp->base_fsp == NULL) {
3546 * According to the MS documentation, the only time the security
3547 * descriptor is applied to the opened file is iff we *created* the
3548 * file; an existing file stays the same.
3550 * Also, it seems (from observation) that you can open the file with
3551 * any access mask but you can still write the sd. We need to override
3552 * the granted access before we call set_sd
3553 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3556 uint32_t sec_info_sent;
3557 uint32_t saved_access_mask = fsp->access_mask;
3559 sec_info_sent = get_sec_info(sd);
3561 fsp->access_mask = FILE_GENERIC_ALL;
3563 /* Convert all the generic bits. */
3564 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3565 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3567 if (sec_info_sent & (SECINFO_OWNER|
3571 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3574 fsp->access_mask = saved_access_mask;
3576 if (!NT_STATUS_IS_OK(status)) {
3579 } else if (lp_inherit_acls(SNUM(conn))) {
3580 /* Inherit from parent. Errors here are not fatal. */
3581 status = inherit_new_acl(fsp);
3582 if (!NT_STATUS_IS_OK(status)) {
3583 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3585 nt_errstr(status) ));
3590 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3593 if (pinfo != NULL) {
3597 smb_fname->st = fsp->fsp_name->st;
3599 return NT_STATUS_OK;
3602 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3605 if (base_fsp && fsp->base_fsp == base_fsp) {
3607 * The close_file below will close
3612 close_file(req, fsp, ERROR_CLOSE);
3615 if (base_fsp != NULL) {
3616 close_file(req, base_fsp, ERROR_CLOSE);
3623 * Calculate the full path name given a relative fid.
3625 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3626 struct smb_request *req,
3627 uint16_t root_dir_fid,
3628 const struct smb_filename *smb_fname,
3629 struct smb_filename **smb_fname_out)
3631 files_struct *dir_fsp;
3632 char *parent_fname = NULL;
3633 char *new_base_name = NULL;
3636 if (root_dir_fid == 0 || !smb_fname) {
3637 status = NT_STATUS_INTERNAL_ERROR;
3641 dir_fsp = file_fsp(req, root_dir_fid);
3643 if (dir_fsp == NULL) {
3644 status = NT_STATUS_INVALID_HANDLE;
3648 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3649 status = NT_STATUS_INVALID_HANDLE;
3653 if (!dir_fsp->is_directory) {
3656 * Check to see if this is a mac fork of some kind.
3659 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3660 is_ntfs_stream_smb_fname(smb_fname)) {
3661 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3666 we need to handle the case when we get a
3667 relative open relative to a file and the
3668 pathname is blank - this is a reopen!
3669 (hint from demyn plantenberg)
3672 status = NT_STATUS_INVALID_HANDLE;
3676 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3678 * We're at the toplevel dir, the final file name
3679 * must not contain ./, as this is filtered out
3680 * normally by srvstr_get_path and unix_convert
3681 * explicitly rejects paths containing ./.
3683 parent_fname = talloc_strdup(talloc_tos(), "");
3684 if (parent_fname == NULL) {
3685 status = NT_STATUS_NO_MEMORY;
3689 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3692 * Copy in the base directory name.
3695 parent_fname = talloc_array(talloc_tos(), char,
3697 if (parent_fname == NULL) {
3698 status = NT_STATUS_NO_MEMORY;
3701 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3705 * Ensure it ends in a '/'.
3706 * We used TALLOC_SIZE +2 to add space for the '/'.
3710 && (parent_fname[dir_name_len-1] != '\\')
3711 && (parent_fname[dir_name_len-1] != '/')) {
3712 parent_fname[dir_name_len] = '/';
3713 parent_fname[dir_name_len+1] = '\0';
3717 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3718 smb_fname->base_name);
3719 if (new_base_name == NULL) {
3720 status = NT_STATUS_NO_MEMORY;
3724 status = filename_convert(req,
3726 req->flags2 & FLAGS2_DFS_PATHNAMES,
3731 if (!NT_STATUS_IS_OK(status)) {
3736 TALLOC_FREE(parent_fname);
3737 TALLOC_FREE(new_base_name);
3741 NTSTATUS create_file_default(connection_struct *conn,
3742 struct smb_request *req,
3743 uint16_t root_dir_fid,
3744 struct smb_filename *smb_fname,
3745 uint32_t access_mask,
3746 uint32_t share_access,
3747 uint32_t create_disposition,
3748 uint32_t create_options,
3749 uint32_t file_attributes,
3750 uint32_t oplock_request,
3751 uint64_t allocation_size,
3752 uint32_t private_flags,
3753 struct security_descriptor *sd,
3754 struct ea_list *ea_list,
3755 files_struct **result,
3758 int info = FILE_WAS_OPENED;
3759 files_struct *fsp = NULL;
3761 bool stream_name = false;
3763 DEBUG(10,("create_file: access_mask = 0x%x "
3764 "file_attributes = 0x%x, share_access = 0x%x, "
3765 "create_disposition = 0x%x create_options = 0x%x "
3766 "oplock_request = 0x%x "
3767 "private_flags = 0x%x "
3768 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3770 (unsigned int)access_mask,
3771 (unsigned int)file_attributes,
3772 (unsigned int)share_access,
3773 (unsigned int)create_disposition,
3774 (unsigned int)create_options,
3775 (unsigned int)oplock_request,
3776 (unsigned int)private_flags,
3777 (unsigned int)root_dir_fid,
3778 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3781 * Calculate the filename from the root_dir_if if necessary.
3784 if (root_dir_fid != 0) {
3785 struct smb_filename *smb_fname_out = NULL;
3786 status = get_relative_fid_filename(conn, req, root_dir_fid,
3787 smb_fname, &smb_fname_out);
3788 if (!NT_STATUS_IS_OK(status)) {
3791 smb_fname = smb_fname_out;
3795 * Check to see if this is a mac fork of some kind.
3798 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3800 enum FAKE_FILE_TYPE fake_file_type;
3802 fake_file_type = is_fake_file(smb_fname);
3804 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3807 * Here we go! support for changing the disk quotas
3810 * We need to fake up to open this MAGIC QUOTA file
3811 * and return a valid FID.
3813 * w2k close this file directly after openening xp
3814 * also tries a QUERY_FILE_INFO on the file and then
3817 status = open_fake_file(req, conn, req->vuid,
3818 fake_file_type, smb_fname,
3820 if (!NT_STATUS_IS_OK(status)) {
3824 ZERO_STRUCT(smb_fname->st);
3828 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3829 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3834 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
3836 smb_fname->stream_name = NULL;
3837 /* We have to handle this error here. */
3838 if (create_options & FILE_DIRECTORY_FILE) {
3839 status = NT_STATUS_NOT_A_DIRECTORY;
3842 if (lp_posix_pathnames()) {
3843 ret = SMB_VFS_LSTAT(conn, smb_fname);
3845 ret = SMB_VFS_STAT(conn, smb_fname);
3848 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3849 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3854 status = create_file_unixpath(
3855 conn, req, smb_fname, access_mask, share_access,
3856 create_disposition, create_options, file_attributes,
3857 oplock_request, allocation_size, private_flags,
3861 if (!NT_STATUS_IS_OK(status)) {
3866 DEBUG(10, ("create_file: info=%d\n", info));
3869 if (pinfo != NULL) {
3872 return NT_STATUS_OK;
3875 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3878 close_file(req, fsp, ERROR_CLOSE);