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 = 0;
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 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
107 if (!NT_STATUS_IS_OK(status)) {
108 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
110 smb_fname_str_dbg(smb_fname),
116 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
117 * owner WRITE_DAC and READ_CONTROL.
119 status = se_access_check(sd,
120 get_current_nttok(conn),
121 (access_mask & ~FILE_READ_ATTRIBUTES),
124 DEBUG(10,("smbd_check_access_rights: file %s requesting "
125 "0x%x returning 0x%x (%s)\n",
126 smb_fname_str_dbg(smb_fname),
127 (unsigned int)access_mask,
128 (unsigned int)rejected_mask,
129 nt_errstr(status) ));
131 if (!NT_STATUS_IS_OK(status)) {
132 if (DEBUGLEVEL >= 10) {
133 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
134 smb_fname_str_dbg(smb_fname) ));
135 NDR_PRINT_DEBUG(security_descriptor, sd);
141 if (NT_STATUS_IS_OK(status) ||
142 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
146 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
147 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
148 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
149 (lp_map_readonly(SNUM(conn)) ||
150 lp_map_archive(SNUM(conn)) ||
151 lp_map_hidden(SNUM(conn)) ||
152 lp_map_system(SNUM(conn)))) {
153 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
155 DEBUG(10,("smbd_check_access_rights: "
157 "FILE_WRITE_ATTRIBUTES "
159 smb_fname_str_dbg(smb_fname)));
162 if (parent_override_delete(conn,
166 /* Were we trying to do an open
167 * for delete and didn't get DELETE
168 * access (only) ? Check if the
169 * directory allows DELETE_CHILD.
171 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
174 rejected_mask &= ~DELETE_ACCESS;
176 DEBUG(10,("smbd_check_access_rights: "
180 smb_fname_str_dbg(smb_fname)));
183 if (rejected_mask != 0) {
184 return NT_STATUS_ACCESS_DENIED;
190 static NTSTATUS check_parent_access(struct connection_struct *conn,
191 struct smb_filename *smb_fname,
192 uint32_t access_mask,
193 char **pp_parent_dir)
196 char *parent_dir = NULL;
197 struct security_descriptor *parent_sd = NULL;
198 uint32_t access_granted = 0;
200 if (!parent_dirname(talloc_tos(),
201 smb_fname->base_name,
204 return NT_STATUS_NO_MEMORY;
208 *pp_parent_dir = parent_dir;
211 if (get_current_uid(conn) == (uid_t)0) {
212 /* I'm sorry sir, I didn't know you were root... */
213 DEBUG(10,("check_parent_access: root override "
214 "on %s. Granting 0x%x\n",
215 smb_fname_str_dbg(smb_fname),
216 (unsigned int)access_mask ));
220 status = SMB_VFS_GET_NT_ACL(conn,
225 if (!NT_STATUS_IS_OK(status)) {
226 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
227 "%s with error %s\n",
234 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
235 * owner WRITE_DAC and READ_CONTROL.
237 status = se_access_check(parent_sd,
238 get_current_nttok(conn),
239 (access_mask & ~FILE_READ_ATTRIBUTES),
241 if(!NT_STATUS_IS_OK(status)) {
242 DEBUG(5,("check_parent_access: access check "
243 "on directory %s for "
244 "path %s for mask 0x%x returned (0x%x) %s\n",
246 smb_fname->base_name,
249 nt_errstr(status) ));
256 /****************************************************************************
257 fd support routines - attempt to do a dos_open.
258 ****************************************************************************/
260 static NTSTATUS fd_open(struct connection_struct *conn,
265 struct smb_filename *smb_fname = fsp->fsp_name;
266 NTSTATUS status = NT_STATUS_OK;
270 * Never follow symlinks on a POSIX client. The
271 * client should be doing this.
274 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
279 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
280 if (fsp->fh->fd == -1) {
281 status = map_nt_error_from_unix(errno);
282 if (errno == EMFILE) {
283 static time_t last_warned = 0L;
285 if (time((time_t *) NULL) > last_warned) {
286 DEBUG(0,("Too many open files, unable "
287 "to open more! smbd's max "
289 lp_max_open_files()));
290 last_warned = time((time_t *) NULL);
296 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
297 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
298 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
303 /****************************************************************************
304 Close the file associated with a fsp.
305 ****************************************************************************/
307 NTSTATUS fd_close(files_struct *fsp)
314 if (fsp->fh->fd == -1) {
315 return NT_STATUS_OK; /* What we used to call a stat open. */
317 if (fsp->fh->ref_count > 1) {
318 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
321 ret = SMB_VFS_CLOSE(fsp);
324 return map_nt_error_from_unix(errno);
329 /****************************************************************************
330 Change the ownership of a file to that of the parent directory.
331 Do this by fd if possible.
332 ****************************************************************************/
334 void change_file_owner_to_parent(connection_struct *conn,
335 const char *inherit_from_dir,
338 struct smb_filename *smb_fname_parent = NULL;
342 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
343 NULL, NULL, &smb_fname_parent);
344 if (!NT_STATUS_IS_OK(status)) {
348 ret = SMB_VFS_STAT(conn, smb_fname_parent);
350 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
351 "directory %s. Error was %s\n",
352 smb_fname_str_dbg(smb_fname_parent),
354 TALLOC_FREE(smb_fname_parent);
358 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
359 /* Already this uid - no need to change. */
360 DEBUG(10,("change_file_owner_to_parent: file %s "
361 "is already owned by uid %d\n",
363 (int)fsp->fsp_name->st.st_ex_uid ));
364 TALLOC_FREE(smb_fname_parent);
369 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
372 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
373 "file %s to parent directory uid %u. Error "
374 "was %s\n", fsp_str_dbg(fsp),
375 (unsigned int)smb_fname_parent->st.st_ex_uid,
378 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
379 "parent directory uid %u.\n", fsp_str_dbg(fsp),
380 (unsigned int)smb_fname_parent->st.st_ex_uid));
381 /* Ensure the uid entry is updated. */
382 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
385 TALLOC_FREE(smb_fname_parent);
388 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
389 const char *inherit_from_dir,
391 SMB_STRUCT_STAT *psbuf)
393 struct smb_filename *smb_fname_parent = NULL;
394 struct smb_filename *smb_fname_cwd = NULL;
395 char *saved_dir = NULL;
396 TALLOC_CTX *ctx = talloc_tos();
397 NTSTATUS status = NT_STATUS_OK;
400 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
402 if (!NT_STATUS_IS_OK(status)) {
406 ret = SMB_VFS_STAT(conn, smb_fname_parent);
408 status = map_nt_error_from_unix(errno);
409 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
410 "directory %s. Error was %s\n",
411 smb_fname_str_dbg(smb_fname_parent),
416 /* We've already done an lstat into psbuf, and we know it's a
417 directory. If we can cd into the directory and the dev/ino
418 are the same then we can safely chown without races as
419 we're locking the directory in place by being in it. This
420 should work on any UNIX (thanks tridge :-). JRA.
423 saved_dir = vfs_GetWd(ctx,conn);
425 status = map_nt_error_from_unix(errno);
426 DEBUG(0,("change_dir_owner_to_parent: failed to get "
427 "current working directory. Error was %s\n",
432 /* Chdir into the new path. */
433 if (vfs_ChDir(conn, fname) == -1) {
434 status = map_nt_error_from_unix(errno);
435 DEBUG(0,("change_dir_owner_to_parent: failed to change "
436 "current working directory to %s. Error "
437 "was %s\n", fname, strerror(errno) ));
441 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
443 if (!NT_STATUS_IS_OK(status)) {
447 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
449 status = map_nt_error_from_unix(errno);
450 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
451 "directory '.' (%s) Error was %s\n",
452 fname, strerror(errno)));
456 /* Ensure we're pointing at the same place. */
457 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
458 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
459 DEBUG(0,("change_dir_owner_to_parent: "
460 "device/inode on directory %s changed. "
461 "Refusing to chown !\n", fname ));
462 status = NT_STATUS_ACCESS_DENIED;
466 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
467 /* Already this uid - no need to change. */
468 DEBUG(10,("change_dir_owner_to_parent: directory %s "
469 "is already owned by uid %d\n",
471 (int)smb_fname_cwd->st.st_ex_uid ));
472 status = NT_STATUS_OK;
477 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
481 status = map_nt_error_from_unix(errno);
482 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
483 "directory %s to parent directory uid %u. "
484 "Error was %s\n", fname,
485 (unsigned int)smb_fname_parent->st.st_ex_uid,
488 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
489 "directory %s to parent directory uid %u.\n",
490 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
491 /* Ensure the uid entry is updated. */
492 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
496 vfs_ChDir(conn,saved_dir);
498 TALLOC_FREE(smb_fname_parent);
499 TALLOC_FREE(smb_fname_cwd);
503 /****************************************************************************
505 ****************************************************************************/
507 static NTSTATUS open_file(files_struct *fsp,
508 connection_struct *conn,
509 struct smb_request *req,
510 const char *parent_dir,
513 uint32 access_mask, /* client requested access mask. */
514 uint32 open_access_mask) /* what we're actually using in the open. */
516 struct smb_filename *smb_fname = fsp->fsp_name;
517 NTSTATUS status = NT_STATUS_OK;
518 int accmode = (flags & O_ACCMODE);
519 int local_flags = flags;
520 bool file_existed = VALID_STAT(fsp->fsp_name->st);
521 bool file_created = false;
526 /* Check permissions */
529 * This code was changed after seeing a client open request
530 * containing the open mode of (DENY_WRITE/read-only) with
531 * the 'create if not exist' bit set. The previous code
532 * would fail to open the file read only on a read-only share
533 * as it was checking the flags parameter directly against O_RDONLY,
534 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
538 if (!CAN_WRITE(conn)) {
539 /* It's a read-only share - fail if we wanted to write. */
540 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
541 DEBUG(3,("Permission denied opening %s\n",
542 smb_fname_str_dbg(smb_fname)));
543 return NT_STATUS_ACCESS_DENIED;
544 } else if(flags & O_CREAT) {
545 /* We don't want to write - but we must make sure that
546 O_CREAT doesn't create the file if we have write
547 access into the directory.
549 flags &= ~(O_CREAT|O_EXCL);
550 local_flags &= ~(O_CREAT|O_EXCL);
555 * This little piece of insanity is inspired by the
556 * fact that an NT client can open a file for O_RDONLY,
557 * but set the create disposition to FILE_EXISTS_TRUNCATE.
558 * If the client *can* write to the file, then it expects to
559 * truncate the file, even though it is opening for readonly.
560 * Quicken uses this stupid trick in backup file creation...
561 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
562 * for helping track this one down. It didn't bite us in 2.0.x
563 * as we always opened files read-write in that release. JRA.
566 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
567 DEBUG(10,("open_file: truncate requested on read-only open "
568 "for file %s\n", smb_fname_str_dbg(smb_fname)));
569 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
572 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
573 (!file_existed && (local_flags & O_CREAT)) ||
574 ((local_flags & O_TRUNC) == O_TRUNC) ) {
578 * We can't actually truncate here as the file may be locked.
579 * open_file_ntcreate will take care of the truncate later. JRA.
582 local_flags &= ~O_TRUNC;
584 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
586 * We would block on opening a FIFO with no one else on the
587 * other end. Do what we used to do and add O_NONBLOCK to the
591 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
592 local_flags |= O_NONBLOCK;
596 /* Don't create files with Microsoft wildcard characters. */
599 * wildcard characters are allowed in stream names
600 * only test the basefilename
602 wild = fsp->base_fsp->fsp_name->base_name;
604 wild = smb_fname->base_name;
606 if ((local_flags & O_CREAT) && !file_existed &&
608 return NT_STATUS_OBJECT_NAME_INVALID;
611 /* Can we access this file ? */
612 if (!fsp->base_fsp) {
613 /* Only do this check on non-stream open. */
615 status = smbd_check_access_rights(conn,
618 } else if (local_flags & O_CREAT){
619 status = check_parent_access(conn,
624 /* File didn't exist and no O_CREAT. */
625 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
627 if (!NT_STATUS_IS_OK(status)) {
628 DEBUG(10,("open_file: "
632 "smbd_check_access_rights" :
633 "check_parent_access",
634 smb_fname_str_dbg(smb_fname),
635 nt_errstr(status) ));
640 /* Actually do the open */
641 status = fd_open(conn, fsp, local_flags, unx_mode);
642 if (!NT_STATUS_IS_OK(status)) {
643 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
644 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
645 nt_errstr(status),local_flags,flags));
649 if ((local_flags & O_CREAT) && !file_existed) {
654 fsp->fh->fd = -1; /* What we used to call a stat open. */
656 /* File must exist for a stat open. */
657 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
660 status = smbd_check_access_rights(conn,
664 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
666 S_ISLNK(smb_fname->st.st_ex_mode)) {
667 /* This is a POSIX stat open for delete
668 * or rename on a symlink that points
670 DEBUG(10,("open_file: allowing POSIX "
671 "open on bad symlink %s\n",
672 smb_fname_str_dbg(smb_fname)));
673 status = NT_STATUS_OK;
676 if (!NT_STATUS_IS_OK(status)) {
677 DEBUG(10,("open_file: "
678 "smbd_check_access_rights on file "
680 smb_fname_str_dbg(smb_fname),
681 nt_errstr(status) ));
689 if (fsp->fh->fd == -1) {
690 ret = SMB_VFS_STAT(conn, smb_fname);
692 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
693 /* If we have an fd, this stat should succeed. */
695 DEBUG(0,("Error doing fstat on open file %s "
697 smb_fname_str_dbg(smb_fname),
702 /* For a non-io open, this stat failing means file not found. JRA */
704 status = map_nt_error_from_unix(errno);
710 bool need_re_stat = false;
711 /* Do all inheritance work after we've
712 done a successful stat call and filled
713 in the stat struct in fsp->fsp_name. */
715 /* Inherit the ACL if required */
716 if (lp_inherit_perms(SNUM(conn))) {
717 inherit_access_posix_acl(conn, parent_dir,
718 smb_fname->base_name,
723 /* Change the owner if required. */
724 if (lp_inherit_owner(SNUM(conn))) {
725 change_file_owner_to_parent(conn, parent_dir,
731 if (fsp->fh->fd == -1) {
732 ret = SMB_VFS_STAT(conn, smb_fname);
734 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
735 /* If we have an fd, this stat should succeed. */
737 DEBUG(0,("Error doing fstat on open file %s "
739 smb_fname_str_dbg(smb_fname),
745 notify_fname(conn, NOTIFY_ACTION_ADDED,
746 FILE_NOTIFY_CHANGE_FILE_NAME,
747 smb_fname->base_name);
752 * POSIX allows read-only opens of directories. We don't
753 * want to do this (we use a different code path for this)
754 * so catch a directory open and return an EISDIR. JRA.
757 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
760 return NT_STATUS_FILE_IS_A_DIRECTORY;
763 fsp->mode = smb_fname->st.st_ex_mode;
764 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
765 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
766 fsp->file_pid = req ? req->smbpid : 0;
767 fsp->can_lock = True;
768 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
769 if (!CAN_WRITE(conn)) {
770 fsp->can_write = False;
772 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
775 fsp->print_file = NULL;
776 fsp->modified = False;
777 fsp->sent_oplock_break = NO_BREAK_SENT;
778 fsp->is_directory = False;
779 if (conn->aio_write_behind_list &&
780 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
781 conn->case_sensitive)) {
782 fsp->aio_write_behind = True;
785 fsp->wcp = NULL; /* Write cache pointer. */
787 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
788 conn->session_info->unix_info->unix_name,
789 smb_fname_str_dbg(smb_fname),
790 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
791 conn->num_files_open));
797 /****************************************************************************
798 Check if we can open a file with a share mode.
799 Returns True if conflict, False if not.
800 ****************************************************************************/
802 static bool share_conflict(struct share_mode_entry *entry,
806 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
807 "entry->share_access = 0x%x, "
808 "entry->private_options = 0x%x\n",
809 (unsigned int)entry->access_mask,
810 (unsigned int)entry->share_access,
811 (unsigned int)entry->private_options));
813 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
814 (unsigned int)access_mask, (unsigned int)share_access));
816 if ((entry->access_mask & (FILE_WRITE_DATA|
820 DELETE_ACCESS)) == 0) {
821 DEBUG(10,("share_conflict: No conflict due to "
822 "entry->access_mask = 0x%x\n",
823 (unsigned int)entry->access_mask ));
827 if ((access_mask & (FILE_WRITE_DATA|
831 DELETE_ACCESS)) == 0) {
832 DEBUG(10,("share_conflict: No conflict due to "
833 "access_mask = 0x%x\n",
834 (unsigned int)access_mask ));
838 #if 1 /* JRA TEST - Superdebug. */
839 #define CHECK_MASK(num, am, right, sa, share) \
840 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
841 (unsigned int)(num), (unsigned int)(am), \
842 (unsigned int)(right), (unsigned int)(am)&(right) )); \
843 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
844 (unsigned int)(num), (unsigned int)(sa), \
845 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
846 if (((am) & (right)) && !((sa) & (share))) { \
847 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
848 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
849 (unsigned int)(share) )); \
853 #define CHECK_MASK(num, am, right, sa, share) \
854 if (((am) & (right)) && !((sa) & (share))) { \
855 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
856 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
857 (unsigned int)(share) )); \
862 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
863 share_access, FILE_SHARE_WRITE);
864 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
865 entry->share_access, FILE_SHARE_WRITE);
867 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
868 share_access, FILE_SHARE_READ);
869 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
870 entry->share_access, FILE_SHARE_READ);
872 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
873 share_access, FILE_SHARE_DELETE);
874 CHECK_MASK(6, access_mask, DELETE_ACCESS,
875 entry->share_access, FILE_SHARE_DELETE);
877 DEBUG(10,("share_conflict: No conflict.\n"));
881 #if defined(DEVELOPER)
882 static void validate_my_share_entries(struct smbd_server_connection *sconn,
884 struct share_mode_entry *share_entry)
888 if (!procid_is_me(&share_entry->pid)) {
892 if (is_deferred_open_entry(share_entry) &&
893 !open_was_deferred(sconn, share_entry->op_mid)) {
894 char *str = talloc_asprintf(talloc_tos(),
895 "Got a deferred entry without a request: "
897 share_mode_str(talloc_tos(), num, share_entry));
901 if (!is_valid_share_mode_entry(share_entry)) {
905 fsp = file_find_dif(sconn, share_entry->id,
906 share_entry->share_file_id);
908 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
909 share_mode_str(talloc_tos(), num, share_entry) ));
910 smb_panic("validate_my_share_entries: Cannot match a "
911 "share entry with an open file\n");
914 if (is_deferred_open_entry(share_entry)) {
918 if ((share_entry->op_type == NO_OPLOCK) &&
919 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
920 /* Someone has already written to it, but I haven't yet
925 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
934 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
935 share_mode_str(talloc_tos(), num, share_entry) ));
936 str = talloc_asprintf(talloc_tos(),
937 "validate_my_share_entries: "
938 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
939 fsp->fsp_name->base_name,
940 (unsigned int)fsp->oplock_type,
941 (unsigned int)share_entry->op_type );
947 bool is_stat_open(uint32 access_mask)
949 return (access_mask &&
950 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
951 FILE_WRITE_ATTRIBUTES))==0) &&
952 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
953 FILE_WRITE_ATTRIBUTES)) != 0));
956 /****************************************************************************
957 Deal with share modes
958 Invarient: Share mode must be locked on entry and exit.
959 Returns -1 on error, or number of share modes on success (may be zero).
960 ****************************************************************************/
962 static NTSTATUS open_mode_check(connection_struct *conn,
963 struct share_mode_lock *lck,
967 uint32 create_options,
972 if(lck->num_share_modes == 0) {
976 *file_existed = True;
978 /* A delete on close prohibits everything */
980 if (is_delete_on_close_set(lck, name_hash)) {
981 return NT_STATUS_DELETE_PENDING;
984 if (is_stat_open(access_mask)) {
985 /* Stat open that doesn't trigger oplock breaks or share mode
986 * checks... ! JRA. */
991 * Check if the share modes will give us access.
994 #if defined(DEVELOPER)
995 for(i = 0; i < lck->num_share_modes; i++) {
996 validate_my_share_entries(conn->sconn, i,
997 &lck->share_modes[i]);
1001 if (!lp_share_modes(SNUM(conn))) {
1002 return NT_STATUS_OK;
1005 /* Now we check the share modes, after any oplock breaks. */
1006 for(i = 0; i < lck->num_share_modes; i++) {
1008 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1012 /* someone else has a share lock on it, check to see if we can
1014 if (share_conflict(&lck->share_modes[i],
1015 access_mask, share_access)) {
1016 return NT_STATUS_SHARING_VIOLATION;
1020 return NT_STATUS_OK;
1023 static bool is_delete_request(files_struct *fsp) {
1024 return ((fsp->access_mask == DELETE_ACCESS) &&
1025 (fsp->oplock_type == NO_OPLOCK));
1029 * Send a break message to the oplock holder and delay the open for
1033 static NTSTATUS send_break_message(files_struct *fsp,
1034 struct share_mode_entry *exclusive,
1039 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1041 DEBUG(10, ("Sending break request to PID %s\n",
1042 procid_str_static(&exclusive->pid)));
1043 exclusive->op_mid = mid;
1045 /* Create the message. */
1046 share_mode_entry_to_message(msg, exclusive);
1048 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1049 don't want this set in the share mode struct pointed to by lck. */
1051 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1052 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1053 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1056 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1057 MSG_SMB_BREAK_REQUEST,
1059 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 DEBUG(3, ("Could not send oplock break message: %s\n",
1062 nt_errstr(status)));
1069 * Return share_mode_entry pointers for :
1070 * 1). Batch oplock entry.
1071 * 2). Batch or exclusive oplock entry (may be identical to #1).
1072 * bool have_level2_oplock
1073 * bool have_no_oplock.
1074 * Do internal consistency checks on the share mode for a file.
1077 static void find_oplock_types(files_struct *fsp,
1079 struct share_mode_lock *lck,
1080 struct share_mode_entry **pp_batch,
1081 struct share_mode_entry **pp_ex_or_batch,
1083 bool *got_no_oplock)
1088 *pp_ex_or_batch = NULL;
1089 *got_level2 = false;
1090 *got_no_oplock = false;
1092 /* Ignore stat or internal opens, as is done in
1093 delay_for_batch_oplocks() and
1094 delay_for_exclusive_oplocks().
1096 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1100 for (i=0; i<lck->num_share_modes; i++) {
1101 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
1105 if (lck->share_modes[i].op_type == NO_OPLOCK &&
1106 is_stat_open(lck->share_modes[i].access_mask)) {
1107 /* We ignore stat opens in the table - they
1108 always have NO_OPLOCK and never get or
1109 cause breaks. JRA. */
1113 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1114 /* batch - can only be one. */
1115 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1116 smb_panic("Bad batch oplock entry.");
1118 *pp_batch = &lck->share_modes[i];
1121 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1122 /* Exclusive or batch - can only be one. */
1123 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1124 smb_panic("Bad exclusive or batch oplock entry.");
1126 *pp_ex_or_batch = &lck->share_modes[i];
1129 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
1130 if (*pp_batch || *pp_ex_or_batch) {
1131 smb_panic("Bad levelII oplock entry.");
1136 if (lck->share_modes[i].op_type == NO_OPLOCK) {
1137 if (*pp_batch || *pp_ex_or_batch) {
1138 smb_panic("Bad no oplock entry.");
1140 *got_no_oplock = true;
1145 static bool delay_for_batch_oplocks(files_struct *fsp,
1148 struct share_mode_entry *batch_entry)
1150 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1154 if (batch_entry != NULL) {
1155 /* Found a batch oplock */
1156 send_break_message(fsp, batch_entry, mid, oplock_request);
1162 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1165 struct share_mode_entry *ex_entry)
1167 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1171 if (ex_entry != NULL) {
1172 /* Found an exclusive or batch oplock */
1173 bool delay_it = is_delete_request(fsp) ?
1174 BATCH_OPLOCK_TYPE(ex_entry->op_type) : true;
1176 send_break_message(fsp, ex_entry, mid, oplock_request);
1183 static void grant_fsp_oplock_type(files_struct *fsp,
1184 const struct byte_range_lock *br_lck,
1186 bool got_level2_oplock,
1187 bool got_a_none_oplock)
1189 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1190 lp_level2_oplocks(SNUM(fsp->conn));
1192 /* Start by granting what the client asked for,
1193 but ensure no SAMBA_PRIVATE bits can be set. */
1194 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1196 if (oplock_request & INTERNAL_OPEN_ONLY) {
1197 /* No oplocks on internal open. */
1198 fsp->oplock_type = NO_OPLOCK;
1199 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1200 fsp->oplock_type, fsp_str_dbg(fsp)));
1202 } else if (br_lck && br_lck->num_locks > 0) {
1203 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1205 fsp->oplock_type = NO_OPLOCK;
1208 if (is_stat_open(fsp->access_mask)) {
1209 /* Leave the value already set. */
1210 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1211 fsp->oplock_type, fsp_str_dbg(fsp)));
1216 * Match what was requested (fsp->oplock_type) with
1217 * what was found in the existing share modes.
1220 if (got_a_none_oplock) {
1221 fsp->oplock_type = NO_OPLOCK;
1222 } else if (got_level2_oplock) {
1223 if (fsp->oplock_type == NO_OPLOCK ||
1224 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1225 /* Store a level2 oplock, but don't tell the client */
1226 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1228 fsp->oplock_type = LEVEL_II_OPLOCK;
1231 /* All share_mode_entries are placeholders or deferred.
1232 * Silently upgrade to fake levelII if the client didn't
1233 * ask for an oplock. */
1234 if (fsp->oplock_type == NO_OPLOCK) {
1235 /* Store a level2 oplock, but don't tell the client */
1236 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1241 * Don't grant level2 to clients that don't want them
1242 * or if we've turned them off.
1244 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1245 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1248 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1249 fsp->oplock_type, fsp_str_dbg(fsp)));
1252 bool request_timed_out(struct timeval request_time,
1253 struct timeval timeout)
1255 struct timeval now, end_time;
1257 end_time = timeval_sum(&request_time, &timeout);
1258 return (timeval_compare(&end_time, &now) < 0);
1261 /****************************************************************************
1262 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1263 ****************************************************************************/
1265 static void defer_open(struct share_mode_lock *lck,
1266 struct timeval request_time,
1267 struct timeval timeout,
1268 struct smb_request *req,
1269 struct deferred_open_record *state)
1273 /* Paranoia check */
1275 for (i=0; i<lck->num_share_modes; i++) {
1276 struct share_mode_entry *e = &lck->share_modes[i];
1278 if (!is_deferred_open_entry(e)) {
1282 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
1283 DEBUG(0, ("Trying to defer an already deferred "
1284 "request: mid=%llu, exiting\n",
1285 (unsigned long long)req->mid));
1286 exit_server("attempt to defer a deferred request");
1290 /* End paranoia check */
1292 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1293 "open entry for mid %llu\n",
1294 (unsigned int)request_time.tv_sec,
1295 (unsigned int)request_time.tv_usec,
1296 (unsigned long long)req->mid));
1298 if (!push_deferred_open_message_smb(req, request_time, timeout,
1299 state->id, (char *)state, sizeof(*state))) {
1300 exit_server("push_deferred_open_message_smb failed");
1302 add_deferred_open(lck, req->mid, request_time,
1303 sconn_server_id(req->sconn), state->id);
1307 /****************************************************************************
1308 On overwrite open ensure that the attributes match.
1309 ****************************************************************************/
1311 bool open_match_attributes(connection_struct *conn,
1312 uint32 old_dos_attr,
1313 uint32 new_dos_attr,
1314 mode_t existing_unx_mode,
1315 mode_t new_unx_mode,
1316 mode_t *returned_unx_mode)
1318 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1320 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1321 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1323 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1324 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1325 *returned_unx_mode = new_unx_mode;
1327 *returned_unx_mode = (mode_t)0;
1330 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1331 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1332 "returned_unx_mode = 0%o\n",
1333 (unsigned int)old_dos_attr,
1334 (unsigned int)existing_unx_mode,
1335 (unsigned int)new_dos_attr,
1336 (unsigned int)*returned_unx_mode ));
1338 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1339 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1340 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1341 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1345 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1346 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1347 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1354 /****************************************************************************
1355 Special FCB or DOS processing in the case of a sharing violation.
1356 Try and find a duplicated file handle.
1357 ****************************************************************************/
1359 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1360 connection_struct *conn,
1361 files_struct *fsp_to_dup_into,
1362 const struct smb_filename *smb_fname,
1367 uint32 share_access,
1368 uint32 create_options)
1372 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1373 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1375 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1376 fsp = file_find_di_next(fsp)) {
1378 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1379 "vuid = %u, file_pid = %u, private_options = 0x%x "
1380 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1381 fsp->fh->fd, (unsigned int)fsp->vuid,
1382 (unsigned int)fsp->file_pid,
1383 (unsigned int)fsp->fh->private_options,
1384 (unsigned int)fsp->access_mask ));
1386 if (fsp->fh->fd != -1 &&
1387 fsp->vuid == vuid &&
1388 fsp->file_pid == file_pid &&
1389 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1390 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1391 (fsp->access_mask & FILE_WRITE_DATA) &&
1392 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1393 strequal(fsp->fsp_name->stream_name,
1394 smb_fname->stream_name)) {
1395 DEBUG(10,("fcb_or_dos_open: file match\n"));
1401 return NT_STATUS_NOT_FOUND;
1404 /* quite an insane set of semantics ... */
1405 if (is_executable(smb_fname->base_name) &&
1406 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1407 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1408 return NT_STATUS_INVALID_PARAMETER;
1411 /* We need to duplicate this fsp. */
1412 return dup_file_fsp(req, fsp, access_mask, share_access,
1413 create_options, fsp_to_dup_into);
1416 static void schedule_defer_open(struct share_mode_lock *lck,
1417 struct timeval request_time,
1418 struct smb_request *req)
1420 struct deferred_open_record state;
1422 /* This is a relative time, added to the absolute
1423 request_time value to get the absolute timeout time.
1424 Note that if this is the second or greater time we enter
1425 this codepath for this particular request mid then
1426 request_time is left as the absolute time of the *first*
1427 time this request mid was processed. This is what allows
1428 the request to eventually time out. */
1430 struct timeval timeout;
1432 /* Normally the smbd we asked should respond within
1433 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1434 * the client did, give twice the timeout as a safety
1435 * measure here in case the other smbd is stuck
1436 * somewhere else. */
1438 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1440 /* Nothing actually uses state.delayed_for_oplocks
1441 but it's handy to differentiate in debug messages
1442 between a 30 second delay due to oplock break, and
1443 a 1 second delay for share mode conflicts. */
1445 state.delayed_for_oplocks = True;
1448 if (!request_timed_out(request_time, timeout)) {
1449 defer_open(lck, request_time, timeout, req, &state);
1453 /****************************************************************************
1454 Work out what access_mask to use from what the client sent us.
1455 ****************************************************************************/
1457 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1458 const struct smb_filename *smb_fname,
1460 uint32_t access_mask,
1461 uint32_t *access_mask_out)
1464 uint32_t orig_access_mask = access_mask;
1465 uint32_t rejected_share_access;
1468 * Convert GENERIC bits to specific bits.
1471 se_map_generic(&access_mask, &file_generic_mapping);
1473 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1474 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1475 if (get_current_uid(conn) == (uid_t)0) {
1476 access_mask |= FILE_GENERIC_ALL;
1477 } else if (file_existed) {
1479 struct security_descriptor *sd;
1480 uint32_t access_granted = 0;
1482 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1487 if (!NT_STATUS_IS_OK(status)) {
1488 DEBUG(10,("smbd_calculate_access_mask: "
1489 "Could not get acl on file %s: %s\n",
1490 smb_fname_str_dbg(smb_fname),
1491 nt_errstr(status)));
1492 return NT_STATUS_ACCESS_DENIED;
1496 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1497 * also takes care of owner WRITE_DAC and READ_CONTROL.
1499 status = se_access_check(sd,
1500 get_current_nttok(conn),
1501 (access_mask & ~FILE_READ_ATTRIBUTES),
1506 if (!NT_STATUS_IS_OK(status)) {
1507 DEBUG(10, ("smbd_calculate_access_mask: "
1508 "Access denied on file %s: "
1509 "when calculating maximum access\n",
1510 smb_fname_str_dbg(smb_fname)));
1511 return NT_STATUS_ACCESS_DENIED;
1514 access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1516 access_mask = FILE_GENERIC_ALL;
1519 access_mask &= conn->share_access;
1522 rejected_share_access = access_mask & ~(conn->share_access);
1524 if (rejected_share_access) {
1525 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1526 "file %s: rejected by share access mask[0x%08X] "
1527 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1528 smb_fname_str_dbg(smb_fname),
1530 orig_access_mask, access_mask,
1531 rejected_share_access));
1532 return NT_STATUS_ACCESS_DENIED;
1535 *access_mask_out = access_mask;
1536 return NT_STATUS_OK;
1539 /****************************************************************************
1540 Remove the deferred open entry under lock.
1541 ****************************************************************************/
1543 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1544 struct server_id pid)
1546 struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id,
1549 DEBUG(0, ("could not get share mode lock\n"));
1552 del_deferred_open_entry(lck, mid, pid);
1556 /****************************************************************
1557 Ensure we get the brlock lock followed by the share mode lock
1558 in the correct order to prevent deadlocks if other smbd's are
1559 using the brlock database on this file simultaneously with this open
1560 (that code also gets the locks in brlock -> share mode lock order).
1561 ****************************************************************/
1563 static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx,
1565 const struct file_id id,
1566 const char *connectpath,
1567 const struct smb_filename *smb_fname,
1568 const struct timespec *p_old_write_time,
1569 struct share_mode_lock **p_lck,
1570 struct byte_range_lock **p_br_lck)
1572 /* Ordering - we must get the br_lck for this
1573 file before the share mode. */
1574 if (lp_locking(fsp->conn->params)) {
1575 *p_br_lck = brl_get_locks_readonly(fsp);
1576 if (*p_br_lck == NULL) {
1577 DEBUG(0, ("Could not get br_lock\n"));
1580 /* Note - we don't need to free the returned
1581 br_lck explicitly as it was allocated on talloc_tos()
1582 and so will be autofreed (and release the lock)
1583 once the frame context disappears.
1585 If it was set to fsp->brlock_rec then it was
1586 talloc_move'd to hang off the fsp pointer and
1587 in this case is guarenteed to not be holding the
1588 lock on the brlock database. */
1591 *p_lck = get_share_mode_lock(mem_ctx,
1597 if (*p_lck == NULL) {
1598 DEBUG(0, ("Could not get share mode lock\n"));
1599 TALLOC_FREE(*p_br_lck);
1605 /****************************************************************************
1606 Open a file with a share mode. Passed in an already created files_struct *.
1607 ****************************************************************************/
1609 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1610 struct smb_request *req,
1611 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1612 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1613 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1614 uint32 create_options, /* options such as delete on close. */
1615 uint32 new_dos_attributes, /* attributes used for new file. */
1616 int oplock_request, /* internal Samba oplock codes. */
1617 /* Information (FILE_EXISTS etc.) */
1618 uint32_t private_flags, /* Samba specific flags. */
1622 struct smb_filename *smb_fname = fsp->fsp_name;
1625 bool file_existed = VALID_STAT(smb_fname->st);
1626 bool def_acl = False;
1627 bool posix_open = False;
1628 bool new_file_created = False;
1629 bool clear_ads = false;
1631 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1632 mode_t new_unx_mode = (mode_t)0;
1633 mode_t unx_mode = (mode_t)0;
1635 uint32 existing_dos_attributes = 0;
1636 struct timeval request_time = timeval_zero();
1637 struct share_mode_lock *lck = NULL;
1638 uint32 open_access_mask = access_mask;
1644 if (conn->printer) {
1646 * Printers are handled completely differently.
1647 * Most of the passed parameters are ignored.
1651 *pinfo = FILE_WAS_CREATED;
1654 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1655 smb_fname_str_dbg(smb_fname)));
1658 DEBUG(0,("open_file_ntcreate: printer open without "
1659 "an SMB request!\n"));
1660 return NT_STATUS_INTERNAL_ERROR;
1663 return print_spool_open(fsp, smb_fname->base_name,
1667 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1669 return NT_STATUS_NO_MEMORY;
1672 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1674 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1675 new_dos_attributes = 0;
1677 /* Windows allows a new file to be created and
1678 silently removes a FILE_ATTRIBUTE_DIRECTORY
1679 sent by the client. Do the same. */
1681 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1683 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1685 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1686 smb_fname, parent_dir);
1689 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1690 "access_mask=0x%x share_access=0x%x "
1691 "create_disposition = 0x%x create_options=0x%x "
1692 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1693 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1694 access_mask, share_access, create_disposition,
1695 create_options, (unsigned int)unx_mode, oplock_request,
1696 (unsigned int)private_flags));
1698 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1699 DEBUG(0, ("No smb request but not an internal only open!\n"));
1700 return NT_STATUS_INTERNAL_ERROR;
1704 * Only non-internal opens can be deferred at all
1709 if (get_deferred_open_message_state(req,
1713 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1714 /* Remember the absolute time of the original
1715 request with this mid. We'll use it later to
1716 see if this has timed out. */
1718 /* Remove the deferred open entry under lock. */
1719 remove_deferred_open_entry(
1720 state->id, req->mid,
1721 sconn_server_id(req->sconn));
1723 /* Ensure we don't reprocess this message. */
1724 remove_deferred_open_message_smb(req->sconn, req->mid);
1728 status = check_name(conn, smb_fname->base_name);
1729 if (!NT_STATUS_IS_OK(status)) {
1734 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1736 existing_dos_attributes = dos_mode(conn, smb_fname);
1740 /* ignore any oplock requests if oplocks are disabled */
1741 if (!lp_oplocks(SNUM(conn)) ||
1742 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
1743 /* Mask off everything except the private Samba bits. */
1744 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1747 /* this is for OS/2 long file names - say we don't support them */
1748 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
1749 /* OS/2 Workplace shell fix may be main code stream in a later
1751 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1753 if (use_nt_status()) {
1754 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1756 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1759 switch( create_disposition ) {
1761 * Currently we're using FILE_SUPERSEDE as the same as
1762 * FILE_OVERWRITE_IF but they really are
1763 * different. FILE_SUPERSEDE deletes an existing file
1764 * (requiring delete access) then recreates it.
1766 case FILE_SUPERSEDE:
1767 /* If file exists replace/overwrite. If file doesn't
1769 flags2 |= (O_CREAT | O_TRUNC);
1773 case FILE_OVERWRITE_IF:
1774 /* If file exists replace/overwrite. If file doesn't
1776 flags2 |= (O_CREAT | O_TRUNC);
1781 /* If file exists open. If file doesn't exist error. */
1782 if (!file_existed) {
1783 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1784 "requested for file %s and file "
1786 smb_fname_str_dbg(smb_fname)));
1788 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1792 case FILE_OVERWRITE:
1793 /* If file exists overwrite. If file doesn't exist
1795 if (!file_existed) {
1796 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1797 "requested for file %s and file "
1799 smb_fname_str_dbg(smb_fname) ));
1801 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1808 /* If file exists error. If file doesn't exist
1811 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1812 "requested for file %s and file "
1813 "already exists.\n",
1814 smb_fname_str_dbg(smb_fname)));
1815 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
1820 return map_nt_error_from_unix(errno);
1822 flags2 |= (O_CREAT|O_EXCL);
1826 /* If file exists open. If file doesn't exist
1832 return NT_STATUS_INVALID_PARAMETER;
1835 /* We only care about matching attributes on file exists and
1838 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1839 (create_disposition == FILE_OVERWRITE_IF))) {
1840 if (!open_match_attributes(conn, existing_dos_attributes,
1842 smb_fname->st.st_ex_mode,
1843 unx_mode, &new_unx_mode)) {
1844 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1845 "for file %s (%x %x) (0%o, 0%o)\n",
1846 smb_fname_str_dbg(smb_fname),
1847 existing_dos_attributes,
1849 (unsigned int)smb_fname->st.st_ex_mode,
1850 (unsigned int)unx_mode ));
1852 return NT_STATUS_ACCESS_DENIED;
1856 status = smbd_calculate_access_mask(conn, smb_fname, file_existed,
1859 if (!NT_STATUS_IS_OK(status)) {
1860 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1861 "on file %s returned %s\n",
1862 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
1866 open_access_mask = access_mask;
1868 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1869 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1872 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1873 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
1877 * Note that we ignore the append flag as append does not
1878 * mean the same thing under DOS and Unix.
1881 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1882 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1883 /* DENY_DOS opens are always underlying read-write on the
1884 file handle, no matter what the requested access mask
1886 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1887 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1897 * Currently we only look at FILE_WRITE_THROUGH for create options.
1901 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1906 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1910 if (!posix_open && !CAN_WRITE(conn)) {
1912 * We should really return a permission denied error if either
1913 * O_CREAT or O_TRUNC are set, but for compatibility with
1914 * older versions of Samba we just AND them out.
1916 flags2 &= ~(O_CREAT|O_TRUNC);
1920 * Ensure we can't write on a read-only share or file.
1923 if (flags != O_RDONLY && file_existed &&
1924 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1925 DEBUG(5,("open_file_ntcreate: write access requested for "
1926 "file %s on read only %s\n",
1927 smb_fname_str_dbg(smb_fname),
1928 !CAN_WRITE(conn) ? "share" : "file" ));
1930 return NT_STATUS_ACCESS_DENIED;
1933 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1934 fsp->share_access = share_access;
1935 fsp->fh->private_options = private_flags;
1936 fsp->access_mask = open_access_mask; /* We change this to the
1937 * requested access_mask after
1938 * the open is done. */
1939 fsp->posix_open = posix_open;
1941 /* Ensure no SAMBA_PRIVATE bits can be set. */
1942 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1944 if (timeval_is_zero(&request_time)) {
1945 request_time = fsp->open_time;
1949 struct byte_range_lock *br_lck = NULL;
1950 struct share_mode_entry *batch_entry = NULL;
1951 struct share_mode_entry *exclusive_entry = NULL;
1952 bool got_level2_oplock = false;
1953 bool got_a_none_oplock = false;
1955 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
1956 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1958 if (!acquire_ordered_locks(talloc_tos(),
1966 return NT_STATUS_SHARING_VIOLATION;
1969 /* Get the types we need to examine. */
1970 find_oplock_types(fsp,
1976 &got_a_none_oplock);
1978 /* First pass - send break only on batch oplocks. */
1979 if ((req != NULL) &&
1980 delay_for_batch_oplocks(fsp,
1984 schedule_defer_open(lck, request_time, req);
1986 return NT_STATUS_SHARING_VIOLATION;
1989 /* Use the client requested access mask here, not the one we
1991 status = open_mode_check(conn, lck, fsp->name_hash,
1992 access_mask, share_access,
1993 create_options, &file_existed);
1995 if (NT_STATUS_IS_OK(status)) {
1996 /* We might be going to allow this open. Check oplock
1998 /* Second pass - send break for both batch or
1999 * exclusive oplocks. */
2000 if ((req != NULL) &&
2001 delay_for_exclusive_oplocks(
2006 schedule_defer_open(lck, request_time, req);
2008 return NT_STATUS_SHARING_VIOLATION;
2012 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2013 /* DELETE_PENDING is not deferred for a second */
2018 grant_fsp_oplock_type(fsp,
2024 if (!NT_STATUS_IS_OK(status)) {
2025 uint32 can_access_mask;
2026 bool can_access = True;
2028 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2030 /* Check if this can be done with the deny_dos and fcb
2033 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2034 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2036 DEBUG(0, ("DOS open without an SMB "
2039 return NT_STATUS_INTERNAL_ERROR;
2042 /* Use the client requested access mask here,
2043 * not the one we open with. */
2044 status = fcb_or_dos_open(req,
2055 if (NT_STATUS_IS_OK(status)) {
2058 *pinfo = FILE_WAS_OPENED;
2060 return NT_STATUS_OK;
2065 * This next line is a subtlety we need for
2066 * MS-Access. If a file open will fail due to share
2067 * permissions and also for security (access) reasons,
2068 * we need to return the access failed error, not the
2069 * share error. We can't open the file due to kernel
2070 * oplock deadlock (it's possible we failed above on
2071 * the open_mode_check()) so use a userspace check.
2074 if (flags & O_RDWR) {
2075 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2076 } else if (flags & O_WRONLY) {
2077 can_access_mask = FILE_WRITE_DATA;
2079 can_access_mask = FILE_READ_DATA;
2082 if (((can_access_mask & FILE_WRITE_DATA) &&
2083 !CAN_WRITE(conn)) ||
2084 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2085 smb_fname, can_access_mask))) {
2090 * If we're returning a share violation, ensure we
2091 * cope with the braindead 1 second delay.
2094 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2095 lp_defer_sharing_violations()) {
2096 struct timeval timeout;
2097 struct deferred_open_record state;
2100 /* this is a hack to speed up torture tests
2102 timeout_usecs = lp_parm_int(SNUM(conn),
2103 "smbd","sharedelay",
2104 SHARING_VIOLATION_USEC_WAIT);
2106 /* This is a relative time, added to the absolute
2107 request_time value to get the absolute timeout time.
2108 Note that if this is the second or greater time we enter
2109 this codepath for this particular request mid then
2110 request_time is left as the absolute time of the *first*
2111 time this request mid was processed. This is what allows
2112 the request to eventually time out. */
2114 timeout = timeval_set(0, timeout_usecs);
2116 /* Nothing actually uses state.delayed_for_oplocks
2117 but it's handy to differentiate in debug messages
2118 between a 30 second delay due to oplock break, and
2119 a 1 second delay for share mode conflicts. */
2121 state.delayed_for_oplocks = False;
2125 && !request_timed_out(request_time,
2127 defer_open(lck, request_time, timeout,
2135 * We have detected a sharing violation here
2136 * so return the correct error code
2138 status = NT_STATUS_SHARING_VIOLATION;
2140 status = NT_STATUS_ACCESS_DENIED;
2146 * We exit this block with the share entry *locked*.....
2150 SMB_ASSERT(!file_existed || (lck != NULL));
2153 * Ensure we pay attention to default ACLs on directories if required.
2156 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2157 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2158 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2161 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2162 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2163 (unsigned int)flags, (unsigned int)flags2,
2164 (unsigned int)unx_mode, (unsigned int)access_mask,
2165 (unsigned int)open_access_mask));
2168 * open_file strips any O_TRUNC flags itself.
2171 fsp_open = open_file(fsp, conn, req, parent_dir,
2172 flags|flags2, unx_mode, access_mask,
2175 if (!NT_STATUS_IS_OK(fsp_open)) {
2180 if (!file_existed) {
2181 struct byte_range_lock *br_lck = NULL;
2182 struct share_mode_entry *batch_entry = NULL;
2183 struct share_mode_entry *exclusive_entry = NULL;
2184 bool got_level2_oplock = false;
2185 bool got_a_none_oplock = false;
2186 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2188 * Deal with the race condition where two smbd's detect the
2189 * file doesn't exist and do the create at the same time. One
2190 * of them will win and set a share mode, the other (ie. this
2191 * one) should check if the requested share mode for this
2192 * create is allowed.
2196 * Now the file exists and fsp is successfully opened,
2197 * fsp->dev and fsp->inode are valid and should replace the
2198 * dev=0,inode=0 from a non existent file. Spotted by
2199 * Nadav Danieli <nadavd@exanet.com>. JRA.
2204 if (!acquire_ordered_locks(talloc_tos(),
2212 return NT_STATUS_SHARING_VIOLATION;
2215 /* Get the types we need to examine. */
2216 find_oplock_types(fsp,
2222 &got_a_none_oplock);
2224 /* First pass - send break only on batch oplocks. */
2225 if ((req != NULL) &&
2226 delay_for_batch_oplocks(fsp,
2230 schedule_defer_open(lck, request_time, req);
2233 return NT_STATUS_SHARING_VIOLATION;
2236 status = open_mode_check(conn, lck, fsp->name_hash,
2237 access_mask, share_access,
2238 create_options, &file_existed);
2240 if (NT_STATUS_IS_OK(status)) {
2241 /* We might be going to allow this open. Check oplock
2243 /* Second pass - send break for both batch or
2244 * exclusive oplocks. */
2245 if ((req != NULL) &&
2246 delay_for_exclusive_oplocks(
2251 schedule_defer_open(lck, request_time, req);
2254 return NT_STATUS_SHARING_VIOLATION;
2258 if (!NT_STATUS_IS_OK(status)) {
2259 struct deferred_open_record state;
2261 state.delayed_for_oplocks = False;
2264 /* Do it all over again immediately. In the second
2265 * round we will find that the file existed and handle
2266 * the DELETE_PENDING and FCB cases correctly. No need
2267 * to duplicate the code here. Essentially this is a
2268 * "goto top of this function", but don't tell
2272 defer_open(lck, request_time, timeval_zero(),
2280 grant_fsp_oplock_type(fsp,
2287 * We exit this block with the share entry *locked*.....
2292 SMB_ASSERT(lck != NULL);
2294 /* Delete streams if create_disposition requires it */
2295 if (file_existed && clear_ads &&
2296 !is_ntfs_stream_smb_fname(smb_fname)) {
2297 status = delete_all_streams(conn, smb_fname->base_name);
2298 if (!NT_STATUS_IS_OK(status)) {
2305 /* note that we ignore failure for the following. It is
2306 basically a hack for NFS, and NFS will never set one of
2307 these only read them. Nobody but Samba can ever set a deny
2308 mode and we have already checked our more authoritative
2309 locking database for permission to set this deny mode. If
2310 the kernel refuses the operations then the kernel is wrong.
2311 note that GPFS supports it as well - jmcd */
2313 if (fsp->fh->fd != -1) {
2315 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2316 if(ret_flock == -1 ){
2321 return NT_STATUS_SHARING_VIOLATION;
2326 * At this point onwards, we can guarentee that the share entry
2327 * is locked, whether we created the file or not, and that the
2328 * deny mode is compatible with all current opens.
2332 * If requested, truncate the file.
2335 if (file_existed && (flags2&O_TRUNC)) {
2337 * We are modifing the file after open - update the stat
2340 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2341 (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) {
2342 status = map_nt_error_from_unix(errno);
2350 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2351 * but we don't have to store this - just ignore it on access check.
2353 if (conn->sconn->using_smb2) {
2355 * SMB2 doesn't return it (according to Microsoft tests).
2356 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2357 * File created with access = 0x7 (Read, Write, Delete)
2358 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2360 fsp->access_mask = access_mask;
2362 /* But SMB1 does. */
2363 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2367 /* stat opens on existing files don't get oplocks. */
2368 if (is_stat_open(open_access_mask)) {
2369 fsp->oplock_type = NO_OPLOCK;
2372 if (!(flags2 & O_TRUNC)) {
2373 info = FILE_WAS_OPENED;
2375 info = FILE_WAS_OVERWRITTEN;
2378 info = FILE_WAS_CREATED;
2386 * Setup the oplock info in both the shared memory and
2390 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2392 * Could not get the kernel oplock or there are byte-range
2393 * locks on the file.
2395 fsp->oplock_type = NO_OPLOCK;
2398 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2399 new_file_created = True;
2402 set_share_mode(lck, fsp, get_current_uid(conn),
2406 /* Handle strange delete on close create semantics. */
2407 if (create_options & FILE_DELETE_ON_CLOSE) {
2409 status = can_set_delete_on_close(fsp, new_dos_attributes);
2411 if (!NT_STATUS_IS_OK(status)) {
2412 /* Remember to delete the mode we just added. */
2413 del_share_mode(lck, fsp);
2418 /* Note that here we set the *inital* delete on close flag,
2419 not the regular one. The magic gets handled in close. */
2420 fsp->initial_delete_on_close = True;
2423 if (new_file_created) {
2424 /* Files should be initially set as archive */
2425 if (lp_map_archive(SNUM(conn)) ||
2426 lp_store_dos_attributes(SNUM(conn))) {
2428 if (file_set_dosmode(conn, smb_fname,
2429 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2430 parent_dir, true) == 0) {
2431 unx_mode = smb_fname->st.st_ex_mode;
2437 /* Determine sparse flag. */
2439 /* POSIX opens are sparse by default. */
2440 fsp->is_sparse = true;
2442 fsp->is_sparse = (file_existed &&
2443 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2447 * Take care of inherited ACLs on created files - if default ACL not
2451 if (!posix_open && !file_existed && !def_acl) {
2453 int saved_errno = errno; /* We might get ENOSYS in the next
2456 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2458 errno = saved_errno; /* Ignore ENOSYS */
2461 } else if (new_unx_mode) {
2465 /* Attributes need changing. File already existed. */
2468 int saved_errno = errno; /* We might get ENOSYS in the
2470 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2472 if (ret == -1 && errno == ENOSYS) {
2473 errno = saved_errno; /* Ignore ENOSYS */
2475 DEBUG(5, ("open_file_ntcreate: reset "
2476 "attributes of file %s to 0%o\n",
2477 smb_fname_str_dbg(smb_fname),
2478 (unsigned int)new_unx_mode));
2479 ret = 0; /* Don't do the fchmod below. */
2484 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2485 DEBUG(5, ("open_file_ntcreate: failed to reset "
2486 "attributes of file %s to 0%o\n",
2487 smb_fname_str_dbg(smb_fname),
2488 (unsigned int)new_unx_mode));
2491 /* If this is a successful open, we must remove any deferred open
2494 del_deferred_open_entry(lck, req->mid,
2495 sconn_server_id(req->sconn));
2499 return NT_STATUS_OK;
2503 /****************************************************************************
2504 Open a file for for write to ensure that we can fchmod it.
2505 ****************************************************************************/
2507 NTSTATUS open_file_fchmod(connection_struct *conn,
2508 struct smb_filename *smb_fname,
2509 files_struct **result)
2511 if (!VALID_STAT(smb_fname->st)) {
2512 return NT_STATUS_INVALID_PARAMETER;
2515 return SMB_VFS_CREATE_FILE(
2518 0, /* root_dir_fid */
2519 smb_fname, /* fname */
2520 FILE_WRITE_DATA, /* access_mask */
2521 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2523 FILE_OPEN, /* create_disposition*/
2524 0, /* create_options */
2525 0, /* file_attributes */
2526 INTERNAL_OPEN_ONLY, /* oplock_request */
2527 0, /* allocation_size */
2528 0, /* private_flags */
2531 result, /* result */
2535 static NTSTATUS mkdir_internal(connection_struct *conn,
2536 struct smb_filename *smb_dname,
2537 uint32 file_attributes)
2540 char *parent_dir = NULL;
2542 bool posix_open = false;
2543 bool need_re_stat = false;
2544 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2546 if(access_mask & ~(conn->share_access)) {
2547 DEBUG(5,("mkdir_internal: failing share access "
2548 "%s\n", lp_servicename(SNUM(conn))));
2549 return NT_STATUS_ACCESS_DENIED;
2552 status = check_name(conn, smb_dname->base_name);
2553 if (!NT_STATUS_IS_OK(status)) {
2557 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2559 return NT_STATUS_NO_MEMORY;
2562 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2564 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2566 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2569 status = check_parent_access(conn,
2573 if(!NT_STATUS_IS_OK(status)) {
2574 DEBUG(5,("mkdir_internal: check_parent_access "
2575 "on directory %s for path %s returned %s\n",
2577 smb_dname->base_name,
2578 nt_errstr(status) ));
2582 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2583 return map_nt_error_from_unix(errno);
2586 /* Ensure we're checking for a symlink here.... */
2587 /* We don't want to get caught by a symlink racer. */
2589 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2590 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2591 smb_fname_str_dbg(smb_dname), strerror(errno)));
2592 return map_nt_error_from_unix(errno);
2595 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2596 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2597 smb_fname_str_dbg(smb_dname)));
2598 return NT_STATUS_NOT_A_DIRECTORY;
2601 if (lp_store_dos_attributes(SNUM(conn))) {
2603 file_set_dosmode(conn, smb_dname,
2604 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2609 if (lp_inherit_perms(SNUM(conn))) {
2610 inherit_access_posix_acl(conn, parent_dir,
2611 smb_dname->base_name, mode);
2612 need_re_stat = true;
2617 * Check if high bits should have been set,
2618 * then (if bits are missing): add them.
2619 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2622 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2623 (mode & ~smb_dname->st.st_ex_mode)) {
2624 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2625 (smb_dname->st.st_ex_mode |
2626 (mode & ~smb_dname->st.st_ex_mode)));
2627 need_re_stat = true;
2631 /* Change the owner if required. */
2632 if (lp_inherit_owner(SNUM(conn))) {
2633 change_dir_owner_to_parent(conn, parent_dir,
2634 smb_dname->base_name,
2636 need_re_stat = true;
2640 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2641 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2642 smb_fname_str_dbg(smb_dname), strerror(errno)));
2643 return map_nt_error_from_unix(errno);
2647 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2648 smb_dname->base_name);
2650 return NT_STATUS_OK;
2653 /****************************************************************************
2654 Ensure we didn't get symlink raced on opening a directory.
2655 ****************************************************************************/
2657 bool check_same_stat(const SMB_STRUCT_STAT *sbuf1,
2658 const SMB_STRUCT_STAT *sbuf2)
2660 if (sbuf1->st_ex_uid != sbuf2->st_ex_uid ||
2661 sbuf1->st_ex_gid != sbuf2->st_ex_gid ||
2662 sbuf1->st_ex_dev != sbuf2->st_ex_dev ||
2663 sbuf1->st_ex_ino != sbuf2->st_ex_ino) {
2669 /****************************************************************************
2670 Open a directory from an NT SMB call.
2671 ****************************************************************************/
2673 static NTSTATUS open_directory(connection_struct *conn,
2674 struct smb_request *req,
2675 struct smb_filename *smb_dname,
2677 uint32 share_access,
2678 uint32 create_disposition,
2679 uint32 create_options,
2680 uint32 file_attributes,
2682 files_struct **result)
2684 files_struct *fsp = NULL;
2685 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2686 struct share_mode_lock *lck = NULL;
2688 struct timespec mtimespec;
2691 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
2693 /* Ensure we have a directory attribute. */
2694 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2696 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2697 "share_access = 0x%x create_options = 0x%x, "
2698 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2699 smb_fname_str_dbg(smb_dname),
2700 (unsigned int)access_mask,
2701 (unsigned int)share_access,
2702 (unsigned int)create_options,
2703 (unsigned int)create_disposition,
2704 (unsigned int)file_attributes));
2706 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2707 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2708 is_ntfs_stream_smb_fname(smb_dname)) {
2709 DEBUG(2, ("open_directory: %s is a stream name!\n",
2710 smb_fname_str_dbg(smb_dname)));
2711 return NT_STATUS_NOT_A_DIRECTORY;
2714 status = smbd_calculate_access_mask(conn, smb_dname, dir_existed,
2715 access_mask, &access_mask);
2716 if (!NT_STATUS_IS_OK(status)) {
2717 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2718 "on file %s returned %s\n",
2719 smb_fname_str_dbg(smb_dname),
2720 nt_errstr(status)));
2724 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2725 !security_token_has_privilege(get_current_nttok(conn),
2726 SEC_PRIV_SECURITY)) {
2727 DEBUG(10, ("open_directory: open on %s "
2728 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2729 smb_fname_str_dbg(smb_dname)));
2730 return NT_STATUS_PRIVILEGE_NOT_HELD;
2733 switch( create_disposition ) {
2737 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2740 info = FILE_WAS_OPENED;
2745 /* If directory exists error. If directory doesn't
2749 status = NT_STATUS_OBJECT_NAME_COLLISION;
2750 DEBUG(2, ("open_directory: unable to create "
2751 "%s. Error was %s\n",
2752 smb_fname_str_dbg(smb_dname),
2753 nt_errstr(status)));
2757 status = mkdir_internal(conn, smb_dname,
2760 if (!NT_STATUS_IS_OK(status)) {
2761 DEBUG(2, ("open_directory: unable to create "
2762 "%s. Error was %s\n",
2763 smb_fname_str_dbg(smb_dname),
2764 nt_errstr(status)));
2768 info = FILE_WAS_CREATED;
2773 * If directory exists open. If directory doesn't
2778 status = NT_STATUS_OK;
2779 info = FILE_WAS_OPENED;
2781 status = mkdir_internal(conn, smb_dname,
2784 if (NT_STATUS_IS_OK(status)) {
2785 info = FILE_WAS_CREATED;
2787 /* Cope with create race. */
2788 if (!NT_STATUS_EQUAL(status,
2789 NT_STATUS_OBJECT_NAME_COLLISION)) {
2790 DEBUG(2, ("open_directory: unable to create "
2791 "%s. Error was %s\n",
2792 smb_fname_str_dbg(smb_dname),
2793 nt_errstr(status)));
2796 info = FILE_WAS_OPENED;
2802 case FILE_SUPERSEDE:
2803 case FILE_OVERWRITE:
2804 case FILE_OVERWRITE_IF:
2806 DEBUG(5,("open_directory: invalid create_disposition "
2807 "0x%x for directory %s\n",
2808 (unsigned int)create_disposition,
2809 smb_fname_str_dbg(smb_dname)));
2810 return NT_STATUS_INVALID_PARAMETER;
2813 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
2814 DEBUG(5,("open_directory: %s is not a directory !\n",
2815 smb_fname_str_dbg(smb_dname)));
2816 return NT_STATUS_NOT_A_DIRECTORY;
2819 if (info == FILE_WAS_OPENED) {
2820 status = smbd_check_access_rights(conn, smb_dname, access_mask);
2821 if (!NT_STATUS_IS_OK(status)) {
2822 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2823 "file %s failed with %s\n",
2824 smb_fname_str_dbg(smb_dname),
2825 nt_errstr(status)));
2830 status = file_new(req, conn, &fsp);
2831 if(!NT_STATUS_IS_OK(status)) {
2836 * Setup the files_struct for it.
2839 fsp->mode = smb_dname->st.st_ex_mode;
2840 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
2841 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2842 fsp->file_pid = req ? req->smbpid : 0;
2843 fsp->can_lock = False;
2844 fsp->can_read = False;
2845 fsp->can_write = False;
2847 fsp->share_access = share_access;
2848 fsp->fh->private_options = 0;
2850 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2852 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2853 fsp->print_file = NULL;
2854 fsp->modified = False;
2855 fsp->oplock_type = NO_OPLOCK;
2856 fsp->sent_oplock_break = NO_BREAK_SENT;
2857 fsp->is_directory = True;
2858 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2859 status = fsp_set_smb_fname(fsp, smb_dname);
2860 if (!NT_STATUS_IS_OK(status)) {
2861 file_free(req, fsp);
2865 mtimespec = smb_dname->st.st_ex_mtime;
2868 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
2870 /* POSIX allows us to open a directory with O_RDONLY. */
2871 status = fd_open(conn, fsp, O_RDONLY, 0);
2873 if (!NT_STATUS_IS_OK(status)) {
2874 DEBUG(5, ("open_directory: Could not open fd for "
2876 smb_fname_str_dbg(smb_dname),
2877 nt_errstr(status)));
2878 file_free(req, fsp);
2882 status = vfs_stat_fsp(fsp);
2883 if (!NT_STATUS_IS_OK(status)) {
2885 file_free(req, fsp);
2889 /* Ensure there was no race condition. */
2890 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
2891 DEBUG(5,("open_directory: stat struct differs for "
2893 smb_fname_str_dbg(smb_dname)));
2895 file_free(req, fsp);
2896 return NT_STATUS_ACCESS_DENIED;
2899 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2900 conn->connectpath, smb_dname, &mtimespec);
2903 DEBUG(0, ("open_directory: Could not get share mode lock for "
2904 "%s\n", smb_fname_str_dbg(smb_dname)));
2906 file_free(req, fsp);
2907 return NT_STATUS_SHARING_VIOLATION;
2910 status = open_mode_check(conn, lck, fsp->name_hash,
2911 access_mask, share_access,
2912 create_options, &dir_existed);
2914 if (!NT_STATUS_IS_OK(status)) {
2917 file_free(req, fsp);
2921 set_share_mode(lck, fsp, get_current_uid(conn),
2922 req ? req->mid : 0, NO_OPLOCK);
2924 /* For directories the delete on close bit at open time seems
2925 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2926 if (create_options & FILE_DELETE_ON_CLOSE) {
2927 status = can_set_delete_on_close(fsp, 0);
2928 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2931 file_free(req, fsp);
2935 if (NT_STATUS_IS_OK(status)) {
2936 /* Note that here we set the *inital* delete on close flag,
2937 not the regular one. The magic gets handled in close. */
2938 fsp->initial_delete_on_close = True;
2949 return NT_STATUS_OK;
2952 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
2953 struct smb_filename *smb_dname)
2958 status = SMB_VFS_CREATE_FILE(
2961 0, /* root_dir_fid */
2962 smb_dname, /* fname */
2963 FILE_READ_ATTRIBUTES, /* access_mask */
2964 FILE_SHARE_NONE, /* share_access */
2965 FILE_CREATE, /* create_disposition*/
2966 FILE_DIRECTORY_FILE, /* create_options */
2967 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2968 0, /* oplock_request */
2969 0, /* allocation_size */
2970 0, /* private_flags */
2976 if (NT_STATUS_IS_OK(status)) {
2977 close_file(req, fsp, NORMAL_CLOSE);
2983 /****************************************************************************
2984 Receive notification that one of our open files has been renamed by another
2986 ****************************************************************************/
2988 void msg_file_was_renamed(struct messaging_context *msg,
2991 struct server_id server_id,
2995 char *frm = (char *)data->data;
2997 const char *sharepath;
2998 const char *base_name;
2999 const char *stream_name;
3000 struct smb_filename *smb_fname = NULL;
3001 size_t sp_len, bn_len;
3003 struct smbd_server_connection *sconn =
3004 talloc_get_type_abort(private_data,
3005 struct smbd_server_connection);
3007 if (data->data == NULL
3008 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3009 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3010 (int)data->length));
3014 /* Unpack the message. */
3015 pull_file_id_24(frm, &id);
3016 sharepath = &frm[24];
3017 sp_len = strlen(sharepath);
3018 base_name = sharepath + sp_len + 1;
3019 bn_len = strlen(base_name);
3020 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3022 /* stream_name must always be NULL if there is no stream. */
3023 if (stream_name[0] == '\0') {
3027 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3028 stream_name, NULL, &smb_fname);
3029 if (!NT_STATUS_IS_OK(status)) {
3033 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3035 sharepath, smb_fname_str_dbg(smb_fname),
3036 file_id_string_tos(&id)));
3038 for(fsp = file_find_di_first(sconn, id); fsp;
3039 fsp = file_find_di_next(fsp)) {
3040 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3042 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3043 fsp->fnum, fsp_str_dbg(fsp),
3044 smb_fname_str_dbg(smb_fname)));
3045 status = fsp_set_smb_fname(fsp, smb_fname);
3046 if (!NT_STATUS_IS_OK(status)) {
3051 /* Now we have the complete path we can work out if this is
3052 actually within this share and adjust newname accordingly. */
3053 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3054 "not sharepath %s) "
3055 "fnum %d from %s -> %s\n",
3056 fsp->conn->connectpath,
3060 smb_fname_str_dbg(smb_fname)));
3064 TALLOC_FREE(smb_fname);
3069 * If a main file is opened for delete, all streams need to be checked for
3070 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3071 * If that works, delete them all by setting the delete on close and close.
3074 NTSTATUS open_streams_for_delete(connection_struct *conn,
3077 struct stream_struct *stream_info = NULL;
3078 files_struct **streams = NULL;
3080 unsigned int num_streams = 0;
3081 TALLOC_CTX *frame = talloc_stackframe();
3084 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3085 &num_streams, &stream_info);
3087 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3088 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3089 DEBUG(10, ("no streams around\n"));
3091 return NT_STATUS_OK;
3094 if (!NT_STATUS_IS_OK(status)) {
3095 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3096 nt_errstr(status)));
3100 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3103 if (num_streams == 0) {
3105 return NT_STATUS_OK;
3108 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3109 if (streams == NULL) {
3110 DEBUG(0, ("talloc failed\n"));
3111 status = NT_STATUS_NO_MEMORY;
3115 for (i=0; i<num_streams; i++) {
3116 struct smb_filename *smb_fname = NULL;
3118 if (strequal(stream_info[i].name, "::$DATA")) {
3123 status = create_synthetic_smb_fname(talloc_tos(), fname,
3124 stream_info[i].name,
3126 if (!NT_STATUS_IS_OK(status)) {
3130 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3131 DEBUG(10, ("Unable to stat stream: %s\n",
3132 smb_fname_str_dbg(smb_fname)));
3135 status = SMB_VFS_CREATE_FILE(
3138 0, /* root_dir_fid */
3139 smb_fname, /* fname */
3140 DELETE_ACCESS, /* access_mask */
3141 (FILE_SHARE_READ | /* share_access */
3142 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3143 FILE_OPEN, /* create_disposition*/
3144 0, /* create_options */
3145 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3146 0, /* oplock_request */
3147 0, /* allocation_size */
3148 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3151 &streams[i], /* result */
3154 if (!NT_STATUS_IS_OK(status)) {
3155 DEBUG(10, ("Could not open stream %s: %s\n",
3156 smb_fname_str_dbg(smb_fname),
3157 nt_errstr(status)));
3159 TALLOC_FREE(smb_fname);
3162 TALLOC_FREE(smb_fname);
3166 * don't touch the variable "status" beyond this point :-)
3169 for (i -= 1 ; i >= 0; i--) {
3170 if (streams[i] == NULL) {
3174 DEBUG(10, ("Closing stream # %d, %s\n", i,
3175 fsp_str_dbg(streams[i])));
3176 close_file(NULL, streams[i], NORMAL_CLOSE);
3184 /*********************************************************************
3185 Create a default ACL by inheriting from the parent. If no inheritance
3186 from the parent available, don't set anything. This will leave the actual
3187 permissions the new file or directory already got from the filesystem
3188 as the NT ACL when read.
3189 *********************************************************************/
3191 static NTSTATUS inherit_new_acl(files_struct *fsp)
3193 TALLOC_CTX *ctx = talloc_tos();
3194 char *parent_name = NULL;
3195 struct security_descriptor *parent_desc = NULL;
3196 NTSTATUS status = NT_STATUS_OK;
3197 struct security_descriptor *psd = NULL;
3198 struct dom_sid *owner_sid = NULL;
3199 struct dom_sid *group_sid = NULL;
3200 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3201 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3202 bool inheritable_components = false;
3205 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3206 return NT_STATUS_NO_MEMORY;
3209 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3211 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3213 if (!NT_STATUS_IS_OK(status)) {
3217 inheritable_components = sd_has_inheritable_components(parent_desc,
3220 if (!inheritable_components && !inherit_owner) {
3221 /* Nothing to inherit and not setting owner. */
3222 return NT_STATUS_OK;
3225 /* Create an inherited descriptor from the parent. */
3227 if (DEBUGLEVEL >= 10) {
3228 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3229 fsp_str_dbg(fsp) ));
3230 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3233 /* Inherit from parent descriptor if "inherit owner" set. */
3234 if (inherit_owner) {
3235 owner_sid = parent_desc->owner_sid;
3236 group_sid = parent_desc->group_sid;
3239 if (owner_sid == NULL) {
3240 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3242 if (group_sid == NULL) {
3243 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3246 status = se_create_child_secdesc(ctx,
3253 if (!NT_STATUS_IS_OK(status)) {
3257 /* If inheritable_components == false,
3258 se_create_child_secdesc()
3259 creates a security desriptor with a NULL dacl
3260 entry, but with SEC_DESC_DACL_PRESENT. We need
3261 to remove that flag. */
3263 if (!inheritable_components) {
3264 security_info_sent &= ~SECINFO_DACL;
3265 psd->type &= ~SEC_DESC_DACL_PRESENT;
3268 if (DEBUGLEVEL >= 10) {
3269 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3270 fsp_str_dbg(fsp) ));
3271 NDR_PRINT_DEBUG(security_descriptor, psd);
3274 if (inherit_owner) {
3275 /* We need to be root to force this. */
3278 status = SMB_VFS_FSET_NT_ACL(fsp,
3281 if (inherit_owner) {
3288 * Wrapper around open_file_ntcreate and open_directory
3291 static NTSTATUS create_file_unixpath(connection_struct *conn,
3292 struct smb_request *req,
3293 struct smb_filename *smb_fname,
3294 uint32_t access_mask,
3295 uint32_t share_access,
3296 uint32_t create_disposition,
3297 uint32_t create_options,
3298 uint32_t file_attributes,
3299 uint32_t oplock_request,
3300 uint64_t allocation_size,
3301 uint32_t private_flags,
3302 struct security_descriptor *sd,
3303 struct ea_list *ea_list,
3305 files_struct **result,
3308 int info = FILE_WAS_OPENED;
3309 files_struct *base_fsp = NULL;
3310 files_struct *fsp = NULL;
3313 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3314 "file_attributes = 0x%x, share_access = 0x%x, "
3315 "create_disposition = 0x%x create_options = 0x%x "
3316 "oplock_request = 0x%x private_flags = 0x%x "
3317 "ea_list = 0x%p, sd = 0x%p, "
3319 (unsigned int)access_mask,
3320 (unsigned int)file_attributes,
3321 (unsigned int)share_access,
3322 (unsigned int)create_disposition,
3323 (unsigned int)create_options,
3324 (unsigned int)oplock_request,
3325 (unsigned int)private_flags,
3326 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3328 if (create_options & FILE_OPEN_BY_FILE_ID) {
3329 status = NT_STATUS_NOT_SUPPORTED;
3333 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3334 status = NT_STATUS_INVALID_PARAMETER;
3339 oplock_request |= INTERNAL_OPEN_ONLY;
3342 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3343 && (access_mask & DELETE_ACCESS)
3344 && !is_ntfs_stream_smb_fname(smb_fname)) {
3346 * We can't open a file with DELETE access if any of the
3347 * streams is open without FILE_SHARE_DELETE
3349 status = open_streams_for_delete(conn, smb_fname->base_name);
3351 if (!NT_STATUS_IS_OK(status)) {
3356 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3357 !security_token_has_privilege(get_current_nttok(conn),
3358 SEC_PRIV_SECURITY)) {
3359 DEBUG(10, ("create_file_unixpath: open on %s "
3360 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3361 smb_fname_str_dbg(smb_fname)));
3362 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3366 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3367 && is_ntfs_stream_smb_fname(smb_fname)
3368 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3369 uint32 base_create_disposition;
3370 struct smb_filename *smb_fname_base = NULL;
3372 if (create_options & FILE_DIRECTORY_FILE) {
3373 status = NT_STATUS_NOT_A_DIRECTORY;
3377 switch (create_disposition) {
3379 base_create_disposition = FILE_OPEN;
3382 base_create_disposition = FILE_OPEN_IF;
3386 /* Create an smb_filename with stream_name == NULL. */
3387 status = create_synthetic_smb_fname(talloc_tos(),
3388 smb_fname->base_name,
3391 if (!NT_STATUS_IS_OK(status)) {
3395 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3396 DEBUG(10, ("Unable to stat stream: %s\n",
3397 smb_fname_str_dbg(smb_fname_base)));
3400 /* Open the base file. */
3401 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3404 | FILE_SHARE_DELETE,
3405 base_create_disposition,
3406 0, 0, 0, 0, 0, NULL, NULL,
3408 TALLOC_FREE(smb_fname_base);
3410 if (!NT_STATUS_IS_OK(status)) {
3411 DEBUG(10, ("create_file_unixpath for base %s failed: "
3412 "%s\n", smb_fname->base_name,
3413 nt_errstr(status)));
3416 /* we don't need to low level fd */
3421 * If it's a request for a directory open, deal with it separately.
3424 if (create_options & FILE_DIRECTORY_FILE) {
3426 if (create_options & FILE_NON_DIRECTORY_FILE) {
3427 status = NT_STATUS_INVALID_PARAMETER;
3431 /* Can't open a temp directory. IFS kit test. */
3432 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3433 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3434 status = NT_STATUS_INVALID_PARAMETER;
3439 * We will get a create directory here if the Win32
3440 * app specified a security descriptor in the
3441 * CreateDirectory() call.
3445 status = open_directory(
3446 conn, req, smb_fname, access_mask, share_access,
3447 create_disposition, create_options, file_attributes,
3452 * Ordinary file case.
3455 status = file_new(req, conn, &fsp);
3456 if(!NT_STATUS_IS_OK(status)) {
3460 status = fsp_set_smb_fname(fsp, smb_fname);
3461 if (!NT_STATUS_IS_OK(status)) {
3466 * We're opening the stream element of a base_fsp
3467 * we already opened. Set up the base_fsp pointer.
3470 fsp->base_fsp = base_fsp;
3473 status = open_file_ntcreate(conn,
3485 if(!NT_STATUS_IS_OK(status)) {
3486 file_free(req, fsp);
3490 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3492 /* A stream open never opens a directory */
3495 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3500 * Fail the open if it was explicitly a non-directory
3504 if (create_options & FILE_NON_DIRECTORY_FILE) {
3505 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3510 status = open_directory(
3511 conn, req, smb_fname, access_mask,
3512 share_access, create_disposition,
3513 create_options, file_attributes,
3518 if (!NT_STATUS_IS_OK(status)) {
3522 fsp->base_fsp = base_fsp;
3524 if ((ea_list != NULL) &&
3525 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3526 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3527 if (!NT_STATUS_IS_OK(status)) {
3532 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3533 status = NT_STATUS_ACCESS_DENIED;
3537 /* Save the requested allocation size. */
3538 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3540 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3541 fsp->initial_allocation_size = smb_roundup(
3542 fsp->conn, allocation_size);
3543 if (fsp->is_directory) {
3544 /* Can't set allocation size on a directory. */
3545 status = NT_STATUS_ACCESS_DENIED;
3548 if (vfs_allocate_file_space(
3549 fsp, fsp->initial_allocation_size) == -1) {
3550 status = NT_STATUS_DISK_FULL;
3554 fsp->initial_allocation_size = smb_roundup(
3555 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3559 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3560 fsp->base_fsp == NULL) {
3563 * According to the MS documentation, the only time the security
3564 * descriptor is applied to the opened file is iff we *created* the
3565 * file; an existing file stays the same.
3567 * Also, it seems (from observation) that you can open the file with
3568 * any access mask but you can still write the sd. We need to override
3569 * the granted access before we call set_sd
3570 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3573 uint32_t sec_info_sent;
3574 uint32_t saved_access_mask = fsp->access_mask;
3576 sec_info_sent = get_sec_info(sd);
3578 fsp->access_mask = FILE_GENERIC_ALL;
3580 /* Convert all the generic bits. */
3581 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3582 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3584 if (sec_info_sent & (SECINFO_OWNER|
3588 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3591 fsp->access_mask = saved_access_mask;
3593 if (!NT_STATUS_IS_OK(status)) {
3596 } else if (lp_inherit_acls(SNUM(conn))) {
3597 /* Inherit from parent. Errors here are not fatal. */
3598 status = inherit_new_acl(fsp);
3599 if (!NT_STATUS_IS_OK(status)) {
3600 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3602 nt_errstr(status) ));
3607 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3610 if (pinfo != NULL) {
3614 smb_fname->st = fsp->fsp_name->st;
3616 return NT_STATUS_OK;
3619 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3622 if (base_fsp && fsp->base_fsp == base_fsp) {
3624 * The close_file below will close
3629 close_file(req, fsp, ERROR_CLOSE);
3632 if (base_fsp != NULL) {
3633 close_file(req, base_fsp, ERROR_CLOSE);
3640 * Calculate the full path name given a relative fid.
3642 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3643 struct smb_request *req,
3644 uint16_t root_dir_fid,
3645 const struct smb_filename *smb_fname,
3646 struct smb_filename **smb_fname_out)
3648 files_struct *dir_fsp;
3649 char *parent_fname = NULL;
3650 char *new_base_name = NULL;
3653 if (root_dir_fid == 0 || !smb_fname) {
3654 status = NT_STATUS_INTERNAL_ERROR;
3658 dir_fsp = file_fsp(req, root_dir_fid);
3660 if (dir_fsp == NULL) {
3661 status = NT_STATUS_INVALID_HANDLE;
3665 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3666 status = NT_STATUS_INVALID_HANDLE;
3670 if (!dir_fsp->is_directory) {
3673 * Check to see if this is a mac fork of some kind.
3676 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3677 is_ntfs_stream_smb_fname(smb_fname)) {
3678 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3683 we need to handle the case when we get a
3684 relative open relative to a file and the
3685 pathname is blank - this is a reopen!
3686 (hint from demyn plantenberg)
3689 status = NT_STATUS_INVALID_HANDLE;
3693 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3695 * We're at the toplevel dir, the final file name
3696 * must not contain ./, as this is filtered out
3697 * normally by srvstr_get_path and unix_convert
3698 * explicitly rejects paths containing ./.
3700 parent_fname = talloc_strdup(talloc_tos(), "");
3701 if (parent_fname == NULL) {
3702 status = NT_STATUS_NO_MEMORY;
3706 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3709 * Copy in the base directory name.
3712 parent_fname = talloc_array(talloc_tos(), char,
3714 if (parent_fname == NULL) {
3715 status = NT_STATUS_NO_MEMORY;
3718 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3722 * Ensure it ends in a '/'.
3723 * We used TALLOC_SIZE +2 to add space for the '/'.
3727 && (parent_fname[dir_name_len-1] != '\\')
3728 && (parent_fname[dir_name_len-1] != '/')) {
3729 parent_fname[dir_name_len] = '/';
3730 parent_fname[dir_name_len+1] = '\0';
3734 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3735 smb_fname->base_name);
3736 if (new_base_name == NULL) {
3737 status = NT_STATUS_NO_MEMORY;
3741 status = filename_convert(req,
3743 req->flags2 & FLAGS2_DFS_PATHNAMES,
3748 if (!NT_STATUS_IS_OK(status)) {
3753 TALLOC_FREE(parent_fname);
3754 TALLOC_FREE(new_base_name);
3758 NTSTATUS create_file_default(connection_struct *conn,
3759 struct smb_request *req,
3760 uint16_t root_dir_fid,
3761 struct smb_filename *smb_fname,
3762 uint32_t access_mask,
3763 uint32_t share_access,
3764 uint32_t create_disposition,
3765 uint32_t create_options,
3766 uint32_t file_attributes,
3767 uint32_t oplock_request,
3768 uint64_t allocation_size,
3769 uint32_t private_flags,
3770 struct security_descriptor *sd,
3771 struct ea_list *ea_list,
3772 files_struct **result,
3775 int info = FILE_WAS_OPENED;
3776 files_struct *fsp = NULL;
3778 bool stream_name = false;
3780 DEBUG(10,("create_file: access_mask = 0x%x "
3781 "file_attributes = 0x%x, share_access = 0x%x, "
3782 "create_disposition = 0x%x create_options = 0x%x "
3783 "oplock_request = 0x%x "
3784 "private_flags = 0x%x "
3785 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3787 (unsigned int)access_mask,
3788 (unsigned int)file_attributes,
3789 (unsigned int)share_access,
3790 (unsigned int)create_disposition,
3791 (unsigned int)create_options,
3792 (unsigned int)oplock_request,
3793 (unsigned int)private_flags,
3794 (unsigned int)root_dir_fid,
3795 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3798 * Calculate the filename from the root_dir_if if necessary.
3801 if (root_dir_fid != 0) {
3802 struct smb_filename *smb_fname_out = NULL;
3803 status = get_relative_fid_filename(conn, req, root_dir_fid,
3804 smb_fname, &smb_fname_out);
3805 if (!NT_STATUS_IS_OK(status)) {
3808 smb_fname = smb_fname_out;
3812 * Check to see if this is a mac fork of some kind.
3815 stream_name = is_ntfs_stream_smb_fname(smb_fname);
3817 enum FAKE_FILE_TYPE fake_file_type;
3819 fake_file_type = is_fake_file(smb_fname);
3821 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3824 * Here we go! support for changing the disk quotas
3827 * We need to fake up to open this MAGIC QUOTA file
3828 * and return a valid FID.
3830 * w2k close this file directly after openening xp
3831 * also tries a QUERY_FILE_INFO on the file and then
3834 status = open_fake_file(req, conn, req->vuid,
3835 fake_file_type, smb_fname,
3837 if (!NT_STATUS_IS_OK(status)) {
3841 ZERO_STRUCT(smb_fname->st);
3845 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3846 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
3851 /* All file access must go through check_name() */
3853 status = check_name(conn, smb_fname->base_name);
3854 if (!NT_STATUS_IS_OK(status)) {
3858 if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) {
3860 smb_fname->stream_name = NULL;
3861 /* We have to handle this error here. */
3862 if (create_options & FILE_DIRECTORY_FILE) {
3863 status = NT_STATUS_NOT_A_DIRECTORY;
3866 if (lp_posix_pathnames()) {
3867 ret = SMB_VFS_LSTAT(conn, smb_fname);
3869 ret = SMB_VFS_STAT(conn, smb_fname);
3872 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
3873 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3878 status = create_file_unixpath(
3879 conn, req, smb_fname, access_mask, share_access,
3880 create_disposition, create_options, file_attributes,
3881 oplock_request, allocation_size, private_flags,
3885 if (!NT_STATUS_IS_OK(status)) {
3890 DEBUG(10, ("create_file: info=%d\n", info));
3893 if (pinfo != NULL) {
3896 return NT_STATUS_OK;
3899 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3902 close_file(req, fsp, ERROR_CLOSE);