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"
35 extern const struct generic_mapping file_generic_mapping;
37 struct deferred_open_record {
38 bool delayed_for_oplocks;
43 /****************************************************************************
44 If the requester wanted DELETE_ACCESS and was rejected because
45 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
47 ****************************************************************************/
49 static bool parent_override_delete(connection_struct *conn,
50 const struct smb_filename *smb_fname,
52 uint32_t rejected_mask)
54 if ((access_mask & DELETE_ACCESS) &&
55 (rejected_mask & DELETE_ACCESS) &&
56 can_delete_file_in_directory(conn, smb_fname)) {
62 /****************************************************************************
63 Check if we have open rights.
64 ****************************************************************************/
66 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
67 const struct smb_filename *smb_fname,
71 /* Check if we have rights to open. */
73 struct security_descriptor *sd = NULL;
74 uint32_t rejected_share_access;
75 uint32_t rejected_mask = access_mask;
77 rejected_share_access = access_mask & ~(conn->share_access);
79 if (rejected_share_access) {
80 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
82 (unsigned int)access_mask,
83 smb_fname_str_dbg(smb_fname),
84 (unsigned int)rejected_share_access ));
85 return NT_STATUS_ACCESS_DENIED;
88 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
89 /* I'm sorry sir, I didn't know you were root... */
90 DEBUG(10,("smbd_check_access_rights: root override "
91 "on %s. Granting 0x%x\n",
92 smb_fname_str_dbg(smb_fname),
93 (unsigned int)access_mask ));
97 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
98 DEBUG(10,("smbd_check_access_rights: not checking ACL "
99 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
100 smb_fname_str_dbg(smb_fname),
101 (unsigned int)access_mask ));
105 if (access_mask == DELETE_ACCESS &&
106 VALID_STAT(smb_fname->st) &&
107 S_ISLNK(smb_fname->st.st_ex_mode)) {
108 /* We can always delete a symlink. */
109 DEBUG(10,("smbd_check_access_rights: not checking ACL "
110 "on DELETE_ACCESS on symlink %s.\n",
111 smb_fname_str_dbg(smb_fname) ));
115 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
123 smb_fname_str_dbg(smb_fname),
126 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
134 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
135 * owner WRITE_DAC and READ_CONTROL.
137 status = se_file_access_check(sd,
138 get_current_nttok(conn),
140 (access_mask & ~FILE_READ_ATTRIBUTES),
143 DEBUG(10,("smbd_check_access_rights: file %s requesting "
144 "0x%x returning 0x%x (%s)\n",
145 smb_fname_str_dbg(smb_fname),
146 (unsigned int)access_mask,
147 (unsigned int)rejected_mask,
148 nt_errstr(status) ));
150 if (!NT_STATUS_IS_OK(status)) {
151 if (DEBUGLEVEL >= 10) {
152 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
153 smb_fname_str_dbg(smb_fname) ));
154 NDR_PRINT_DEBUG(security_descriptor, sd);
160 if (NT_STATUS_IS_OK(status) ||
161 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
165 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
169 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
170 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
171 !lp_store_dos_attributes(SNUM(conn)) &&
172 (lp_map_readonly(SNUM(conn)) ||
173 lp_map_archive(SNUM(conn)) ||
174 lp_map_hidden(SNUM(conn)) ||
175 lp_map_system(SNUM(conn)))) {
176 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
178 DEBUG(10,("smbd_check_access_rights: "
180 "FILE_WRITE_ATTRIBUTES "
182 smb_fname_str_dbg(smb_fname)));
185 if (parent_override_delete(conn,
189 /* Were we trying to do an open
190 * for delete and didn't get DELETE
191 * access (only) ? Check if the
192 * directory allows DELETE_CHILD.
194 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
197 rejected_mask &= ~DELETE_ACCESS;
199 DEBUG(10,("smbd_check_access_rights: "
203 smb_fname_str_dbg(smb_fname)));
206 if (rejected_mask != 0) {
207 return NT_STATUS_ACCESS_DENIED;
212 static NTSTATUS check_parent_access(struct connection_struct *conn,
213 struct smb_filename *smb_fname,
214 uint32_t access_mask)
217 char *parent_dir = NULL;
218 struct security_descriptor *parent_sd = NULL;
219 uint32_t access_granted = 0;
221 if (!parent_dirname(talloc_tos(),
222 smb_fname->base_name,
225 return NT_STATUS_NO_MEMORY;
228 if (get_current_uid(conn) == (uid_t)0) {
229 /* I'm sorry sir, I didn't know you were root... */
230 DEBUG(10,("check_parent_access: root override "
231 "on %s. Granting 0x%x\n",
232 smb_fname_str_dbg(smb_fname),
233 (unsigned int)access_mask ));
237 status = SMB_VFS_GET_NT_ACL(conn,
242 if (!NT_STATUS_IS_OK(status)) {
243 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
244 "%s with error %s\n",
251 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
252 * owner WRITE_DAC and READ_CONTROL.
254 status = se_file_access_check(parent_sd,
255 get_current_nttok(conn),
257 (access_mask & ~FILE_READ_ATTRIBUTES),
259 if(!NT_STATUS_IS_OK(status)) {
260 DEBUG(5,("check_parent_access: access check "
261 "on directory %s for "
262 "path %s for mask 0x%x returned (0x%x) %s\n",
264 smb_fname->base_name,
267 nt_errstr(status) ));
274 /****************************************************************************
275 fd support routines - attempt to do a dos_open.
276 ****************************************************************************/
278 NTSTATUS fd_open(struct connection_struct *conn,
283 struct smb_filename *smb_fname = fsp->fsp_name;
284 NTSTATUS status = NT_STATUS_OK;
288 * Never follow symlinks on a POSIX client. The
289 * client should be doing this.
292 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
297 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
298 if (fsp->fh->fd == -1) {
299 int posix_errno = errno;
301 #if defined(ENOTSUP) && defined(OSF1)
302 /* handle special Tru64 errno */
303 if (errno == ENOTSUP) {
308 /* fix broken NetBSD errno */
309 if (errno == EFTYPE) {
313 /* fix broken FreeBSD errno */
314 if (errno == EMLINK) {
317 #endif /* O_NOFOLLOW */
318 status = map_nt_error_from_unix(posix_errno);
319 if (errno == EMFILE) {
320 static time_t last_warned = 0L;
322 if (time((time_t *) NULL) > last_warned) {
323 DEBUG(0,("Too many open files, unable "
324 "to open more! smbd's max "
326 lp_max_open_files()));
327 last_warned = time((time_t *) NULL);
333 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
334 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
335 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
340 /****************************************************************************
341 Close the file associated with a fsp.
342 ****************************************************************************/
344 NTSTATUS fd_close(files_struct *fsp)
351 if (fsp->fh->fd == -1) {
352 return NT_STATUS_OK; /* What we used to call a stat open. */
354 if (fsp->fh->ref_count > 1) {
355 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
358 ret = SMB_VFS_CLOSE(fsp);
361 return map_nt_error_from_unix(errno);
366 /****************************************************************************
367 Change the ownership of a file to that of the parent directory.
368 Do this by fd if possible.
369 ****************************************************************************/
371 void change_file_owner_to_parent(connection_struct *conn,
372 const char *inherit_from_dir,
375 struct smb_filename *smb_fname_parent = NULL;
379 status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir,
380 NULL, NULL, &smb_fname_parent);
381 if (!NT_STATUS_IS_OK(status)) {
385 ret = SMB_VFS_STAT(conn, smb_fname_parent);
387 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
388 "directory %s. Error was %s\n",
389 smb_fname_str_dbg(smb_fname_parent),
391 TALLOC_FREE(smb_fname_parent);
395 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
396 /* Already this uid - no need to change. */
397 DEBUG(10,("change_file_owner_to_parent: file %s "
398 "is already owned by uid %d\n",
400 (int)fsp->fsp_name->st.st_ex_uid ));
401 TALLOC_FREE(smb_fname_parent);
406 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
409 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
410 "file %s to parent directory uid %u. Error "
411 "was %s\n", fsp_str_dbg(fsp),
412 (unsigned int)smb_fname_parent->st.st_ex_uid,
415 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
416 "parent directory uid %u.\n", fsp_str_dbg(fsp),
417 (unsigned int)smb_fname_parent->st.st_ex_uid));
418 /* Ensure the uid entry is updated. */
419 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
422 TALLOC_FREE(smb_fname_parent);
425 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
426 const char *inherit_from_dir,
428 SMB_STRUCT_STAT *psbuf)
430 struct smb_filename *smb_fname_parent = NULL;
431 struct smb_filename *smb_fname_cwd = NULL;
432 char *saved_dir = NULL;
433 TALLOC_CTX *ctx = talloc_tos();
434 NTSTATUS status = NT_STATUS_OK;
437 status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL,
439 if (!NT_STATUS_IS_OK(status)) {
443 ret = SMB_VFS_STAT(conn, smb_fname_parent);
445 status = map_nt_error_from_unix(errno);
446 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
447 "directory %s. Error was %s\n",
448 smb_fname_str_dbg(smb_fname_parent),
453 /* We've already done an lstat into psbuf, and we know it's a
454 directory. If we can cd into the directory and the dev/ino
455 are the same then we can safely chown without races as
456 we're locking the directory in place by being in it. This
457 should work on any UNIX (thanks tridge :-). JRA.
460 saved_dir = vfs_GetWd(ctx,conn);
462 status = map_nt_error_from_unix(errno);
463 DEBUG(0,("change_dir_owner_to_parent: failed to get "
464 "current working directory. Error was %s\n",
469 /* Chdir into the new path. */
470 if (vfs_ChDir(conn, fname) == -1) {
471 status = map_nt_error_from_unix(errno);
472 DEBUG(0,("change_dir_owner_to_parent: failed to change "
473 "current working directory to %s. Error "
474 "was %s\n", fname, strerror(errno) ));
478 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
480 if (!NT_STATUS_IS_OK(status)) {
484 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
486 status = map_nt_error_from_unix(errno);
487 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
488 "directory '.' (%s) Error was %s\n",
489 fname, strerror(errno)));
493 /* Ensure we're pointing at the same place. */
494 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
495 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
496 DEBUG(0,("change_dir_owner_to_parent: "
497 "device/inode on directory %s changed. "
498 "Refusing to chown !\n", fname ));
499 status = NT_STATUS_ACCESS_DENIED;
503 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
504 /* Already this uid - no need to change. */
505 DEBUG(10,("change_dir_owner_to_parent: directory %s "
506 "is already owned by uid %d\n",
508 (int)smb_fname_cwd->st.st_ex_uid ));
509 status = NT_STATUS_OK;
514 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
518 status = map_nt_error_from_unix(errno);
519 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
520 "directory %s to parent directory uid %u. "
521 "Error was %s\n", fname,
522 (unsigned int)smb_fname_parent->st.st_ex_uid,
525 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
526 "directory %s to parent directory uid %u.\n",
527 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
528 /* Ensure the uid entry is updated. */
529 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
533 vfs_ChDir(conn,saved_dir);
535 TALLOC_FREE(smb_fname_parent);
536 TALLOC_FREE(smb_fname_cwd);
540 /****************************************************************************
541 Open a file - returning a guaranteed ATOMIC indication of if the
542 file was created or not.
543 ****************************************************************************/
545 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
551 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
552 bool file_existed = VALID_STAT(fsp->fsp_name->st);
554 *file_created = false;
556 if (!(flags & O_CREAT)) {
558 * We're not creating the file, just pass through.
560 return fd_open(conn, fsp, flags, mode);
563 if (flags & O_EXCL) {
565 * Fail if already exists, just pass through.
567 status = fd_open(conn, fsp, flags, mode);
570 * Here we've opened with O_CREAT|O_EXCL. If that went
571 * NT_STATUS_OK, we *know* we created this file.
573 *file_created = NT_STATUS_IS_OK(status);
579 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
580 * To know absolutely if we created the file or not,
581 * we can never call O_CREAT without O_EXCL. So if
582 * we think the file existed, try without O_CREAT|O_EXCL.
583 * If we think the file didn't exist, try with
584 * O_CREAT|O_EXCL. Keep bouncing between these two
585 * requests until either the file is created, or
586 * opened. Either way, we keep going until we get
587 * a returnable result (error, or open/create).
591 int curr_flags = flags;
594 /* Just try open, do not create. */
595 curr_flags &= ~(O_CREAT);
596 status = fd_open(conn, fsp, curr_flags, mode);
597 if (NT_STATUS_EQUAL(status,
598 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
600 * Someone deleted it in the meantime.
603 file_existed = false;
604 DEBUG(10,("fd_open_atomic: file %s existed. "
606 smb_fname_str_dbg(fsp->fsp_name)));
610 /* Try create exclusively, fail if it exists. */
611 curr_flags |= O_EXCL;
612 status = fd_open(conn, fsp, curr_flags, mode);
613 if (NT_STATUS_EQUAL(status,
614 NT_STATUS_OBJECT_NAME_COLLISION)) {
616 * Someone created it in the meantime.
617 * Retry without O_CREAT.
620 DEBUG(10,("fd_open_atomic: file %s "
621 "did not exist. Retry.\n",
622 smb_fname_str_dbg(fsp->fsp_name)));
625 if (NT_STATUS_IS_OK(status)) {
627 * Here we've opened with O_CREAT|O_EXCL
628 * and got success. We *know* we created
631 *file_created = true;
634 /* Create is done, or failed. */
640 /****************************************************************************
642 ****************************************************************************/
644 static NTSTATUS open_file(files_struct *fsp,
645 connection_struct *conn,
646 struct smb_request *req,
647 const char *parent_dir,
650 uint32 access_mask, /* client requested access mask. */
651 uint32 open_access_mask, /* what we're actually using in the open. */
652 bool *p_file_created)
654 struct smb_filename *smb_fname = fsp->fsp_name;
655 NTSTATUS status = NT_STATUS_OK;
656 int accmode = (flags & O_ACCMODE);
657 int local_flags = flags;
658 bool file_existed = VALID_STAT(fsp->fsp_name->st);
663 /* Check permissions */
666 * This code was changed after seeing a client open request
667 * containing the open mode of (DENY_WRITE/read-only) with
668 * the 'create if not exist' bit set. The previous code
669 * would fail to open the file read only on a read-only share
670 * as it was checking the flags parameter directly against O_RDONLY,
671 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
675 if (!CAN_WRITE(conn)) {
676 /* It's a read-only share - fail if we wanted to write. */
677 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
678 DEBUG(3,("Permission denied opening %s\n",
679 smb_fname_str_dbg(smb_fname)));
680 return NT_STATUS_ACCESS_DENIED;
681 } else if(flags & O_CREAT) {
682 /* We don't want to write - but we must make sure that
683 O_CREAT doesn't create the file if we have write
684 access into the directory.
686 flags &= ~(O_CREAT|O_EXCL);
687 local_flags &= ~(O_CREAT|O_EXCL);
692 * This little piece of insanity is inspired by the
693 * fact that an NT client can open a file for O_RDONLY,
694 * but set the create disposition to FILE_EXISTS_TRUNCATE.
695 * If the client *can* write to the file, then it expects to
696 * truncate the file, even though it is opening for readonly.
697 * Quicken uses this stupid trick in backup file creation...
698 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
699 * for helping track this one down. It didn't bite us in 2.0.x
700 * as we always opened files read-write in that release. JRA.
703 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
704 DEBUG(10,("open_file: truncate requested on read-only open "
705 "for file %s\n", smb_fname_str_dbg(smb_fname)));
706 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
709 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
710 (!file_existed && (local_flags & O_CREAT)) ||
711 ((local_flags & O_TRUNC) == O_TRUNC) ) {
715 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
717 * We would block on opening a FIFO with no one else on the
718 * other end. Do what we used to do and add O_NONBLOCK to the
722 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
723 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
724 local_flags |= O_NONBLOCK;
728 /* Don't create files with Microsoft wildcard characters. */
731 * wildcard characters are allowed in stream names
732 * only test the basefilename
734 wild = fsp->base_fsp->fsp_name->base_name;
736 wild = smb_fname->base_name;
738 if ((local_flags & O_CREAT) && !file_existed &&
740 return NT_STATUS_OBJECT_NAME_INVALID;
743 /* Can we access this file ? */
744 if (!fsp->base_fsp) {
745 /* Only do this check on non-stream open. */
747 status = smbd_check_access_rights(conn,
751 } else if (local_flags & O_CREAT){
752 status = check_parent_access(conn,
756 /* File didn't exist and no O_CREAT. */
757 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
759 if (!NT_STATUS_IS_OK(status)) {
760 DEBUG(10,("open_file: "
764 "smbd_check_access_rights" :
765 "check_parent_access",
766 smb_fname_str_dbg(smb_fname),
767 nt_errstr(status) ));
772 /* Actually do the open */
773 status = fd_open_atomic(conn, fsp, local_flags,
774 unx_mode, p_file_created);
775 if (!NT_STATUS_IS_OK(status)) {
776 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
777 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
778 nt_errstr(status),local_flags,flags));
782 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
784 /* If we have an fd, this stat should succeed. */
785 DEBUG(0,("Error doing fstat on open file %s "
787 smb_fname_str_dbg(smb_fname),
789 status = map_nt_error_from_unix(errno);
794 if (*p_file_created) {
795 /* We created this file. */
797 bool need_re_stat = false;
798 /* Do all inheritance work after we've
799 done a successful fstat call and filled
800 in the stat struct in fsp->fsp_name. */
802 /* Inherit the ACL if required */
803 if (lp_inherit_perms(SNUM(conn))) {
804 inherit_access_posix_acl(conn, parent_dir,
805 smb_fname->base_name,
810 /* Change the owner if required. */
811 if (lp_inherit_owner(SNUM(conn))) {
812 change_file_owner_to_parent(conn, parent_dir,
818 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
819 /* If we have an fd, this stat should succeed. */
821 DEBUG(0,("Error doing fstat on open file %s "
823 smb_fname_str_dbg(smb_fname),
828 notify_fname(conn, NOTIFY_ACTION_ADDED,
829 FILE_NOTIFY_CHANGE_FILE_NAME,
830 smb_fname->base_name);
833 fsp->fh->fd = -1; /* What we used to call a stat open. */
835 /* File must exist for a stat open. */
836 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
839 status = smbd_check_access_rights(conn,
844 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
846 S_ISLNK(smb_fname->st.st_ex_mode)) {
847 /* This is a POSIX stat open for delete
848 * or rename on a symlink that points
850 DEBUG(10,("open_file: allowing POSIX "
851 "open on bad symlink %s\n",
852 smb_fname_str_dbg(smb_fname)));
853 status = NT_STATUS_OK;
856 if (!NT_STATUS_IS_OK(status)) {
857 DEBUG(10,("open_file: "
858 "smbd_check_access_rights on file "
860 smb_fname_str_dbg(smb_fname),
861 nt_errstr(status) ));
867 * POSIX allows read-only opens of directories. We don't
868 * want to do this (we use a different code path for this)
869 * so catch a directory open and return an EISDIR. JRA.
872 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
875 return NT_STATUS_FILE_IS_A_DIRECTORY;
878 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
879 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
880 fsp->file_pid = req ? req->smbpid : 0;
881 fsp->can_lock = True;
882 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
885 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
886 fsp->print_file = NULL;
887 fsp->modified = False;
888 fsp->sent_oplock_break = NO_BREAK_SENT;
889 fsp->is_directory = False;
890 if (conn->aio_write_behind_list &&
891 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
892 conn->case_sensitive)) {
893 fsp->aio_write_behind = True;
896 fsp->wcp = NULL; /* Write cache pointer. */
898 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
899 conn->session_info->unix_info->unix_name,
900 smb_fname_str_dbg(smb_fname),
901 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
902 conn->num_files_open));
908 /****************************************************************************
909 Check if we can open a file with a share mode.
910 Returns True if conflict, False if not.
911 ****************************************************************************/
913 static bool share_conflict(struct share_mode_entry *entry,
917 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
918 "entry->share_access = 0x%x, "
919 "entry->private_options = 0x%x\n",
920 (unsigned int)entry->access_mask,
921 (unsigned int)entry->share_access,
922 (unsigned int)entry->private_options));
924 if (server_id_is_disconnected(&entry->pid)) {
926 * note: cleanup should have been done by
927 * delay_for_batch_oplocks()
932 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
933 (unsigned int)access_mask, (unsigned int)share_access));
935 if ((entry->access_mask & (FILE_WRITE_DATA|
939 DELETE_ACCESS)) == 0) {
940 DEBUG(10,("share_conflict: No conflict due to "
941 "entry->access_mask = 0x%x\n",
942 (unsigned int)entry->access_mask ));
946 if ((access_mask & (FILE_WRITE_DATA|
950 DELETE_ACCESS)) == 0) {
951 DEBUG(10,("share_conflict: No conflict due to "
952 "access_mask = 0x%x\n",
953 (unsigned int)access_mask ));
957 #if 1 /* JRA TEST - Superdebug. */
958 #define CHECK_MASK(num, am, right, sa, share) \
959 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
960 (unsigned int)(num), (unsigned int)(am), \
961 (unsigned int)(right), (unsigned int)(am)&(right) )); \
962 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
963 (unsigned int)(num), (unsigned int)(sa), \
964 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
965 if (((am) & (right)) && !((sa) & (share))) { \
966 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
967 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
968 (unsigned int)(share) )); \
972 #define CHECK_MASK(num, am, right, sa, share) \
973 if (((am) & (right)) && !((sa) & (share))) { \
974 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
975 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
976 (unsigned int)(share) )); \
981 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
982 share_access, FILE_SHARE_WRITE);
983 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
984 entry->share_access, FILE_SHARE_WRITE);
986 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
987 share_access, FILE_SHARE_READ);
988 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
989 entry->share_access, FILE_SHARE_READ);
991 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
992 share_access, FILE_SHARE_DELETE);
993 CHECK_MASK(6, access_mask, DELETE_ACCESS,
994 entry->share_access, FILE_SHARE_DELETE);
996 DEBUG(10,("share_conflict: No conflict.\n"));
1000 #if defined(DEVELOPER)
1001 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1003 struct share_mode_entry *share_entry)
1005 struct server_id self = messaging_server_id(sconn->msg_ctx);
1008 if (!serverid_equal(&self, &share_entry->pid)) {
1012 if (is_deferred_open_entry(share_entry) &&
1013 !open_was_deferred(sconn, share_entry->op_mid)) {
1014 char *str = talloc_asprintf(talloc_tos(),
1015 "Got a deferred entry without a request: "
1017 share_mode_str(talloc_tos(), num, share_entry));
1021 if (!is_valid_share_mode_entry(share_entry)) {
1025 fsp = file_find_dif(sconn, share_entry->id,
1026 share_entry->share_file_id);
1028 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1029 share_mode_str(talloc_tos(), num, share_entry) ));
1030 smb_panic("validate_my_share_entries: Cannot match a "
1031 "share entry with an open file\n");
1034 if (is_deferred_open_entry(share_entry)) {
1038 if ((share_entry->op_type == NO_OPLOCK) &&
1039 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
1040 /* Someone has already written to it, but I haven't yet
1045 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1054 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1055 share_mode_str(talloc_tos(), num, share_entry) ));
1056 str = talloc_asprintf(talloc_tos(),
1057 "validate_my_share_entries: "
1058 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1059 fsp->fsp_name->base_name,
1060 (unsigned int)fsp->oplock_type,
1061 (unsigned int)share_entry->op_type );
1067 bool is_stat_open(uint32 access_mask)
1069 return (access_mask &&
1070 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
1071 FILE_WRITE_ATTRIBUTES))==0) &&
1072 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
1073 FILE_WRITE_ATTRIBUTES)) != 0));
1076 /****************************************************************************
1077 Deal with share modes
1078 Invarient: Share mode must be locked on entry and exit.
1079 Returns -1 on error, or number of share modes on success (may be zero).
1080 ****************************************************************************/
1082 static NTSTATUS open_mode_check(connection_struct *conn,
1083 struct share_mode_lock *lck,
1086 uint32 share_access,
1087 uint32 create_options,
1092 if(lck->data->num_share_modes == 0) {
1093 return NT_STATUS_OK;
1096 /* A delete on close prohibits everything */
1098 if (is_delete_on_close_set(lck, name_hash)) {
1100 * Check the delete on close token
1101 * is valid. It could have been left
1102 * after a server crash.
1104 for(i = 0; i < lck->data->num_share_modes; i++) {
1105 if (!share_mode_stale_pid(lck->data, i)) {
1107 *file_existed = true;
1109 return NT_STATUS_DELETE_PENDING;
1112 return NT_STATUS_OK;
1115 if (is_stat_open(access_mask)) {
1116 /* Stat open that doesn't trigger oplock breaks or share mode
1117 * checks... ! JRA. */
1118 return NT_STATUS_OK;
1122 * Check if the share modes will give us access.
1125 #if defined(DEVELOPER)
1126 for(i = 0; i < lck->data->num_share_modes; i++) {
1127 validate_my_share_entries(conn->sconn, i,
1128 &lck->data->share_modes[i]);
1132 /* Now we check the share modes, after any oplock breaks. */
1133 for(i = 0; i < lck->data->num_share_modes; i++) {
1135 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1139 /* someone else has a share lock on it, check to see if we can
1141 if (share_conflict(&lck->data->share_modes[i],
1142 access_mask, share_access)) {
1144 if (share_mode_stale_pid(lck->data, i)) {
1148 *file_existed = true;
1150 return NT_STATUS_SHARING_VIOLATION;
1154 if (lck->data->num_share_modes != 0) {
1155 *file_existed = true;
1158 return NT_STATUS_OK;
1162 * Send a break message to the oplock holder and delay the open for
1166 static NTSTATUS send_break_message(files_struct *fsp,
1167 struct share_mode_entry *exclusive,
1172 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1174 DEBUG(10, ("Sending break request to PID %s\n",
1175 procid_str_static(&exclusive->pid)));
1176 exclusive->op_mid = mid;
1178 /* Create the message. */
1179 share_mode_entry_to_message(msg, exclusive);
1181 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1182 don't want this set in the share mode struct pointed to by lck. */
1184 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
1185 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,
1186 exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
1189 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1190 MSG_SMB_BREAK_REQUEST,
1192 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
1193 if (!NT_STATUS_IS_OK(status)) {
1194 DEBUG(3, ("Could not send oplock break message: %s\n",
1195 nt_errstr(status)));
1202 * Return share_mode_entry pointers for :
1203 * 1). Batch oplock entry.
1204 * 2). Batch or exclusive oplock entry (may be identical to #1).
1205 * bool have_level2_oplock
1206 * bool have_no_oplock.
1207 * Do internal consistency checks on the share mode for a file.
1210 static void find_oplock_types(files_struct *fsp,
1212 const struct share_mode_lock *lck,
1213 struct share_mode_entry **pp_batch,
1214 struct share_mode_entry **pp_ex_or_batch,
1216 bool *got_no_oplock)
1221 *pp_ex_or_batch = NULL;
1222 *got_level2 = false;
1223 *got_no_oplock = false;
1225 /* Ignore stat or internal opens, as is done in
1226 delay_for_batch_oplocks() and
1227 delay_for_exclusive_oplocks().
1229 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1233 for (i=0; i<lck->data->num_share_modes; i++) {
1234 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1238 if (lck->data->share_modes[i].op_type == NO_OPLOCK &&
1239 is_stat_open(lck->data->share_modes[i].access_mask)) {
1240 /* We ignore stat opens in the table - they
1241 always have NO_OPLOCK and never get or
1242 cause breaks. JRA. */
1246 if (BATCH_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1247 /* batch - can only be one. */
1248 if (share_mode_stale_pid(lck->data, i)) {
1249 DEBUG(10, ("Found stale batch oplock\n"));
1252 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1253 smb_panic("Bad batch oplock entry.");
1255 *pp_batch = &lck->data->share_modes[i];
1258 if (EXCLUSIVE_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1259 if (share_mode_stale_pid(lck->data, i)) {
1260 DEBUG(10, ("Found stale duplicate oplock\n"));
1263 /* Exclusive or batch - can only be one. */
1264 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1265 smb_panic("Bad exclusive or batch oplock entry.");
1267 *pp_ex_or_batch = &lck->data->share_modes[i];
1270 if (LEVEL_II_OPLOCK_TYPE(lck->data->share_modes[i].op_type)) {
1271 if (*pp_batch || *pp_ex_or_batch) {
1272 if (share_mode_stale_pid(lck->data, i)) {
1273 DEBUG(10, ("Found stale LevelII "
1277 smb_panic("Bad levelII oplock entry.");
1282 if (lck->data->share_modes[i].op_type == NO_OPLOCK) {
1283 if (*pp_batch || *pp_ex_or_batch) {
1284 if (share_mode_stale_pid(lck->data, i)) {
1285 DEBUG(10, ("Found stale NO_OPLOCK "
1289 smb_panic("Bad no oplock entry.");
1291 *got_no_oplock = true;
1296 static bool delay_for_batch_oplocks(files_struct *fsp,
1299 struct share_mode_entry *batch_entry)
1301 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1304 if (batch_entry == NULL) {
1308 if (server_id_is_disconnected(&batch_entry->pid)) {
1311 * This could be achieved by sending a break message
1312 * to ourselves. Special considerations for files
1313 * with delete_on_close flag set!
1315 * For now we keep it simple and do not
1316 * allow delete on close for durable handles.
1321 /* Found a batch oplock */
1322 send_break_message(fsp, batch_entry, mid, oplock_request);
1326 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1329 struct share_mode_entry *ex_entry)
1331 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1334 if (ex_entry == NULL) {
1338 if (server_id_is_disconnected(&ex_entry->pid)) {
1340 * since only durable handles can get disconnected,
1341 * and we can only get durable handles with batch oplocks,
1342 * this should actually never be reached...
1347 send_break_message(fsp, ex_entry, mid, oplock_request);
1351 static bool file_has_brlocks(files_struct *fsp)
1353 struct byte_range_lock *br_lck;
1355 br_lck = brl_get_locks_readonly(fsp);
1359 return br_lck->num_locks > 0 ? true : false;
1362 static void grant_fsp_oplock_type(files_struct *fsp,
1364 bool got_level2_oplock,
1365 bool got_a_none_oplock)
1367 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1368 lp_level2_oplocks(SNUM(fsp->conn));
1370 /* Start by granting what the client asked for,
1371 but ensure no SAMBA_PRIVATE bits can be set. */
1372 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1374 if (oplock_request & INTERNAL_OPEN_ONLY) {
1375 /* No oplocks on internal open. */
1376 fsp->oplock_type = NO_OPLOCK;
1377 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1378 fsp->oplock_type, fsp_str_dbg(fsp)));
1382 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1383 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1385 fsp->oplock_type = NO_OPLOCK;
1388 if (is_stat_open(fsp->access_mask)) {
1389 /* Leave the value already set. */
1390 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1391 fsp->oplock_type, fsp_str_dbg(fsp)));
1396 * Match what was requested (fsp->oplock_type) with
1397 * what was found in the existing share modes.
1400 if (got_a_none_oplock) {
1401 fsp->oplock_type = NO_OPLOCK;
1402 } else if (got_level2_oplock) {
1403 if (fsp->oplock_type == NO_OPLOCK ||
1404 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
1405 /* Store a level2 oplock, but don't tell the client */
1406 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1408 fsp->oplock_type = LEVEL_II_OPLOCK;
1411 /* All share_mode_entries are placeholders or deferred.
1412 * Silently upgrade to fake levelII if the client didn't
1413 * ask for an oplock. */
1414 if (fsp->oplock_type == NO_OPLOCK) {
1415 /* Store a level2 oplock, but don't tell the client */
1416 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1421 * Don't grant level2 to clients that don't want them
1422 * or if we've turned them off.
1424 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1425 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
1428 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1429 fsp->oplock_type, fsp_str_dbg(fsp)));
1432 static bool request_timed_out(struct timeval request_time,
1433 struct timeval timeout)
1435 struct timeval now, end_time;
1437 end_time = timeval_sum(&request_time, &timeout);
1438 return (timeval_compare(&end_time, &now) < 0);
1441 /****************************************************************************
1442 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1443 ****************************************************************************/
1445 static void defer_open(struct share_mode_lock *lck,
1446 struct timeval request_time,
1447 struct timeval timeout,
1448 struct smb_request *req,
1449 struct deferred_open_record *state)
1451 struct server_id self = messaging_server_id(req->sconn->msg_ctx);
1453 /* Paranoia check */
1458 for (i=0; i<lck->data->num_share_modes; i++) {
1459 struct share_mode_entry *e = &lck->data->share_modes[i];
1461 if (is_deferred_open_entry(e) &&
1462 serverid_equal(&self, &e->pid) &&
1463 (e->op_mid == req->mid)) {
1464 DEBUG(0, ("Trying to defer an already deferred "
1465 "request: mid=%llu, exiting\n",
1466 (unsigned long long)req->mid));
1468 exit_server("attempt to defer a deferred request");
1473 /* End paranoia check */
1475 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1476 "open entry for mid %llu\n",
1477 (unsigned int)request_time.tv_sec,
1478 (unsigned int)request_time.tv_usec,
1479 (unsigned long long)req->mid));
1481 if (!push_deferred_open_message_smb(req, request_time, timeout,
1482 state->id, (char *)state, sizeof(*state))) {
1484 exit_server("push_deferred_open_message_smb failed");
1487 add_deferred_open(lck, req->mid, request_time, self, state->id);
1492 /****************************************************************************
1493 On overwrite open ensure that the attributes match.
1494 ****************************************************************************/
1496 static bool open_match_attributes(connection_struct *conn,
1497 uint32 old_dos_attr,
1498 uint32 new_dos_attr,
1499 mode_t existing_unx_mode,
1500 mode_t new_unx_mode,
1501 mode_t *returned_unx_mode)
1503 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1505 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1506 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1508 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1509 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1510 *returned_unx_mode = new_unx_mode;
1512 *returned_unx_mode = (mode_t)0;
1515 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1516 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1517 "returned_unx_mode = 0%o\n",
1518 (unsigned int)old_dos_attr,
1519 (unsigned int)existing_unx_mode,
1520 (unsigned int)new_dos_attr,
1521 (unsigned int)*returned_unx_mode ));
1523 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1524 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1525 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1526 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1530 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1531 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1532 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1539 /****************************************************************************
1540 Special FCB or DOS processing in the case of a sharing violation.
1541 Try and find a duplicated file handle.
1542 ****************************************************************************/
1544 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1545 connection_struct *conn,
1546 files_struct *fsp_to_dup_into,
1547 const struct smb_filename *smb_fname,
1552 uint32 share_access,
1553 uint32 create_options)
1557 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1558 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1560 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1561 fsp = file_find_di_next(fsp)) {
1563 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1564 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1565 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1566 fsp->fh->fd, (unsigned long long)fsp->vuid,
1567 (unsigned int)fsp->file_pid,
1568 (unsigned int)fsp->fh->private_options,
1569 (unsigned int)fsp->access_mask ));
1571 if (fsp->fh->fd != -1 &&
1572 fsp->vuid == vuid &&
1573 fsp->file_pid == file_pid &&
1574 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1575 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1576 (fsp->access_mask & FILE_WRITE_DATA) &&
1577 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1578 strequal(fsp->fsp_name->stream_name,
1579 smb_fname->stream_name)) {
1580 DEBUG(10,("fcb_or_dos_open: file match\n"));
1586 return NT_STATUS_NOT_FOUND;
1589 /* quite an insane set of semantics ... */
1590 if (is_executable(smb_fname->base_name) &&
1591 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1592 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1593 return NT_STATUS_INVALID_PARAMETER;
1596 /* We need to duplicate this fsp. */
1597 return dup_file_fsp(req, fsp, access_mask, share_access,
1598 create_options, fsp_to_dup_into);
1601 static void schedule_defer_open(struct share_mode_lock *lck,
1602 struct timeval request_time,
1603 struct smb_request *req)
1605 struct deferred_open_record state;
1607 /* This is a relative time, added to the absolute
1608 request_time value to get the absolute timeout time.
1609 Note that if this is the second or greater time we enter
1610 this codepath for this particular request mid then
1611 request_time is left as the absolute time of the *first*
1612 time this request mid was processed. This is what allows
1613 the request to eventually time out. */
1615 struct timeval timeout;
1617 /* Normally the smbd we asked should respond within
1618 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1619 * the client did, give twice the timeout as a safety
1620 * measure here in case the other smbd is stuck
1621 * somewhere else. */
1623 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1625 /* Nothing actually uses state.delayed_for_oplocks
1626 but it's handy to differentiate in debug messages
1627 between a 30 second delay due to oplock break, and
1628 a 1 second delay for share mode conflicts. */
1630 state.delayed_for_oplocks = True;
1631 state.async_open = false;
1632 state.id = lck->data->id;
1634 if (!request_timed_out(request_time, timeout)) {
1635 defer_open(lck, request_time, timeout, req, &state);
1639 /****************************************************************************
1640 Reschedule an open call that went asynchronous.
1641 ****************************************************************************/
1643 static void schedule_async_open(struct timeval request_time,
1644 struct smb_request *req)
1646 struct deferred_open_record state;
1647 struct timeval timeout;
1649 timeout = timeval_set(20, 0);
1652 state.delayed_for_oplocks = false;
1653 state.async_open = true;
1655 if (!request_timed_out(request_time, timeout)) {
1656 defer_open(NULL, request_time, timeout, req, &state);
1660 /****************************************************************************
1661 Work out what access_mask to use from what the client sent us.
1662 ****************************************************************************/
1664 static NTSTATUS smbd_calculate_maximum_allowed_access(
1665 connection_struct *conn,
1666 const struct smb_filename *smb_fname,
1668 uint32_t *p_access_mask)
1670 struct security_descriptor *sd;
1671 uint32_t access_granted;
1674 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1675 *p_access_mask |= FILE_GENERIC_ALL;
1676 return NT_STATUS_OK;
1679 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1684 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1686 * File did not exist
1688 *p_access_mask = FILE_GENERIC_ALL;
1689 return NT_STATUS_OK;
1691 if (!NT_STATUS_IS_OK(status)) {
1692 DEBUG(10,("Could not get acl on file %s: %s\n",
1693 smb_fname_str_dbg(smb_fname),
1694 nt_errstr(status)));
1695 return NT_STATUS_ACCESS_DENIED;
1699 * Never test FILE_READ_ATTRIBUTES. se_file_access_check()
1700 * also takes care of owner WRITE_DAC and READ_CONTROL.
1702 status = se_file_access_check(sd,
1703 get_current_nttok(conn),
1705 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1710 if (!NT_STATUS_IS_OK(status)) {
1711 DEBUG(10, ("Access denied on file %s: "
1712 "when calculating maximum access\n",
1713 smb_fname_str_dbg(smb_fname)));
1714 return NT_STATUS_ACCESS_DENIED;
1716 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1718 if (!(access_granted & DELETE_ACCESS)) {
1719 if (can_delete_file_in_directory(conn, smb_fname)) {
1720 *p_access_mask |= DELETE_ACCESS;
1724 return NT_STATUS_OK;
1727 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1728 const struct smb_filename *smb_fname,
1730 uint32_t access_mask,
1731 uint32_t *access_mask_out)
1734 uint32_t orig_access_mask = access_mask;
1735 uint32_t rejected_share_access;
1738 * Convert GENERIC bits to specific bits.
1741 se_map_generic(&access_mask, &file_generic_mapping);
1743 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1744 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1746 status = smbd_calculate_maximum_allowed_access(
1747 conn, smb_fname, use_privs, &access_mask);
1749 if (!NT_STATUS_IS_OK(status)) {
1753 access_mask &= conn->share_access;
1756 rejected_share_access = access_mask & ~(conn->share_access);
1758 if (rejected_share_access) {
1759 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1760 "file %s: rejected by share access mask[0x%08X] "
1761 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1762 smb_fname_str_dbg(smb_fname),
1764 orig_access_mask, access_mask,
1765 rejected_share_access));
1766 return NT_STATUS_ACCESS_DENIED;
1769 *access_mask_out = access_mask;
1770 return NT_STATUS_OK;
1773 /****************************************************************************
1774 Remove the deferred open entry under lock.
1775 ****************************************************************************/
1777 void remove_deferred_open_entry(struct file_id id, uint64_t mid,
1778 struct server_id pid)
1780 struct share_mode_lock *lck = get_existing_share_mode_lock(
1783 DEBUG(0, ("could not get share mode lock\n"));
1786 del_deferred_open_entry(lck, mid, pid);
1790 /****************************************************************************
1791 Return true if this is a state pointer to an asynchronous create.
1792 ****************************************************************************/
1794 bool is_deferred_open_async(const void *ptr)
1796 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1798 return state->async_open;
1801 static bool clear_ads(uint32_t create_disposition)
1805 switch (create_disposition) {
1806 case FILE_SUPERSEDE:
1807 case FILE_OVERWRITE_IF:
1808 case FILE_OVERWRITE:
1817 static int disposition_to_open_flags(uint32_t create_disposition)
1822 * Currently we're using FILE_SUPERSEDE as the same as
1823 * FILE_OVERWRITE_IF but they really are
1824 * different. FILE_SUPERSEDE deletes an existing file
1825 * (requiring delete access) then recreates it.
1828 switch (create_disposition) {
1829 case FILE_SUPERSEDE:
1830 case FILE_OVERWRITE_IF:
1832 * If file exists replace/overwrite. If file doesn't
1835 ret = O_CREAT|O_TRUNC;
1840 * If file exists open. If file doesn't exist error.
1845 case FILE_OVERWRITE:
1847 * If file exists overwrite. If file doesn't exist
1855 * If file exists error. If file doesn't exist create.
1857 ret = O_CREAT|O_EXCL;
1862 * If file exists open. If file doesn't exist create.
1870 /****************************************************************************
1871 Open a file with a share mode. Passed in an already created files_struct *.
1872 ****************************************************************************/
1874 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1875 struct smb_request *req,
1876 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1877 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1878 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1879 uint32 create_options, /* options such as delete on close. */
1880 uint32 new_dos_attributes, /* attributes used for new file. */
1881 int oplock_request, /* internal Samba oplock codes. */
1882 /* Information (FILE_EXISTS etc.) */
1883 uint32_t private_flags, /* Samba specific flags. */
1887 struct smb_filename *smb_fname = fsp->fsp_name;
1890 bool file_existed = VALID_STAT(smb_fname->st);
1891 bool def_acl = False;
1892 bool posix_open = False;
1893 bool new_file_created = False;
1894 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1895 mode_t new_unx_mode = (mode_t)0;
1896 mode_t unx_mode = (mode_t)0;
1898 uint32 existing_dos_attributes = 0;
1899 struct timeval request_time = timeval_zero();
1900 struct share_mode_lock *lck = NULL;
1901 uint32 open_access_mask = access_mask;
1904 SMB_STRUCT_STAT saved_stat = smb_fname->st;
1906 if (conn->printer) {
1908 * Printers are handled completely differently.
1909 * Most of the passed parameters are ignored.
1913 *pinfo = FILE_WAS_CREATED;
1916 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1917 smb_fname_str_dbg(smb_fname)));
1920 DEBUG(0,("open_file_ntcreate: printer open without "
1921 "an SMB request!\n"));
1922 return NT_STATUS_INTERNAL_ERROR;
1925 return print_spool_open(fsp, smb_fname->base_name,
1929 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
1931 return NT_STATUS_NO_MEMORY;
1934 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1936 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1937 new_dos_attributes = 0;
1939 /* Windows allows a new file to be created and
1940 silently removes a FILE_ATTRIBUTE_DIRECTORY
1941 sent by the client. Do the same. */
1943 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
1945 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1947 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
1948 smb_fname, parent_dir);
1951 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1952 "access_mask=0x%x share_access=0x%x "
1953 "create_disposition = 0x%x create_options=0x%x "
1954 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1955 smb_fname_str_dbg(smb_fname), new_dos_attributes,
1956 access_mask, share_access, create_disposition,
1957 create_options, (unsigned int)unx_mode, oplock_request,
1958 (unsigned int)private_flags));
1960 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1961 DEBUG(0, ("No smb request but not an internal only open!\n"));
1962 return NT_STATUS_INTERNAL_ERROR;
1966 * Only non-internal opens can be deferred at all
1971 if (get_deferred_open_message_state(req,
1974 /* Remember the absolute time of the original
1975 request with this mid. We'll use it later to
1976 see if this has timed out. */
1978 /* If it was an async create retry, the file
1981 if (is_deferred_open_async(ptr)) {
1982 SET_STAT_INVALID(smb_fname->st);
1983 file_existed = false;
1985 struct deferred_open_record *state = (struct deferred_open_record *)ptr;
1986 /* Remove the deferred open entry under lock. */
1987 remove_deferred_open_entry(
1988 state->id, req->mid,
1989 messaging_server_id(req->sconn->msg_ctx));
1992 /* Ensure we don't reprocess this message. */
1993 remove_deferred_open_message_smb(req->sconn, req->mid);
1998 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2000 existing_dos_attributes = dos_mode(conn, smb_fname);
2004 /* ignore any oplock requests if oplocks are disabled */
2005 if (!lp_oplocks(SNUM(conn)) ||
2006 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2007 /* Mask off everything except the private Samba bits. */
2008 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2011 /* this is for OS/2 long file names - say we don't support them */
2012 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2013 /* OS/2 Workplace shell fix may be main code stream in a later
2015 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2017 if (use_nt_status()) {
2018 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2020 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2023 switch( create_disposition ) {
2025 /* If file exists open. If file doesn't exist error. */
2026 if (!file_existed) {
2027 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2028 "requested for file %s and file "
2030 smb_fname_str_dbg(smb_fname)));
2032 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2036 case FILE_OVERWRITE:
2037 /* If file exists overwrite. If file doesn't exist
2039 if (!file_existed) {
2040 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2041 "requested for file %s and file "
2043 smb_fname_str_dbg(smb_fname) ));
2045 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2050 /* If file exists error. If file doesn't exist
2053 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2054 "requested for file %s and file "
2055 "already exists.\n",
2056 smb_fname_str_dbg(smb_fname)));
2057 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2062 return map_nt_error_from_unix(errno);
2066 case FILE_SUPERSEDE:
2067 case FILE_OVERWRITE_IF:
2071 return NT_STATUS_INVALID_PARAMETER;
2074 flags2 = disposition_to_open_flags(create_disposition);
2076 /* We only care about matching attributes on file exists and
2079 if (!posix_open && file_existed &&
2080 ((create_disposition == FILE_OVERWRITE) ||
2081 (create_disposition == FILE_OVERWRITE_IF))) {
2082 if (!open_match_attributes(conn, existing_dos_attributes,
2084 smb_fname->st.st_ex_mode,
2085 unx_mode, &new_unx_mode)) {
2086 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2087 "for file %s (%x %x) (0%o, 0%o)\n",
2088 smb_fname_str_dbg(smb_fname),
2089 existing_dos_attributes,
2091 (unsigned int)smb_fname->st.st_ex_mode,
2092 (unsigned int)unx_mode ));
2094 return NT_STATUS_ACCESS_DENIED;
2098 status = smbd_calculate_access_mask(conn, smb_fname,
2102 if (!NT_STATUS_IS_OK(status)) {
2103 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2104 "on file %s returned %s\n",
2105 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2109 open_access_mask = access_mask;
2111 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2112 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2115 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2116 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2120 * Note that we ignore the append flag as append does not
2121 * mean the same thing under DOS and Unix.
2124 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
2125 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
2126 /* DENY_DOS opens are always underlying read-write on the
2127 file handle, no matter what the requested access mask
2129 if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
2130 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
2140 * Currently we only look at FILE_WRITE_THROUGH for create options.
2144 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2149 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2153 if (!posix_open && !CAN_WRITE(conn)) {
2155 * We should really return a permission denied error if either
2156 * O_CREAT or O_TRUNC are set, but for compatibility with
2157 * older versions of Samba we just AND them out.
2159 flags2 &= ~(O_CREAT|O_TRUNC);
2163 * Ensure we can't write on a read-only share or file.
2166 if (flags != O_RDONLY && file_existed &&
2167 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2168 DEBUG(5,("open_file_ntcreate: write access requested for "
2169 "file %s on read only %s\n",
2170 smb_fname_str_dbg(smb_fname),
2171 !CAN_WRITE(conn) ? "share" : "file" ));
2173 return NT_STATUS_ACCESS_DENIED;
2176 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2177 fsp->share_access = share_access;
2178 fsp->fh->private_options = private_flags;
2179 fsp->access_mask = open_access_mask; /* We change this to the
2180 * requested access_mask after
2181 * the open is done. */
2182 fsp->posix_open = posix_open;
2184 /* Ensure no SAMBA_PRIVATE bits can be set. */
2185 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2187 if (timeval_is_zero(&request_time)) {
2188 request_time = fsp->open_time;
2192 struct share_mode_entry *batch_entry = NULL;
2193 struct share_mode_entry *exclusive_entry = NULL;
2194 bool got_level2_oplock = false;
2195 bool got_a_none_oplock = false;
2198 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2199 id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2201 lck = get_share_mode_lock(talloc_tos(), id,
2203 smb_fname, &old_write_time);
2205 DEBUG(0, ("Could not get share mode lock\n"));
2206 return NT_STATUS_SHARING_VIOLATION;
2209 /* Get the types we need to examine. */
2210 find_oplock_types(fsp,
2216 &got_a_none_oplock);
2218 /* First pass - send break only on batch oplocks. */
2219 if ((req != NULL) &&
2220 delay_for_batch_oplocks(fsp,
2224 schedule_defer_open(lck, request_time, req);
2226 return NT_STATUS_SHARING_VIOLATION;
2229 /* Use the client requested access mask here, not the one we
2231 status = open_mode_check(conn, lck, fsp->name_hash,
2232 access_mask, share_access,
2233 create_options, &file_existed);
2235 if (NT_STATUS_IS_OK(status)) {
2236 /* We might be going to allow this open. Check oplock
2238 /* Second pass - send break for both batch or
2239 * exclusive oplocks. */
2240 if ((req != NULL) &&
2241 delay_for_exclusive_oplocks(
2246 schedule_defer_open(lck, request_time, req);
2248 return NT_STATUS_SHARING_VIOLATION;
2252 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
2253 /* DELETE_PENDING is not deferred for a second */
2258 grant_fsp_oplock_type(fsp,
2263 if (!NT_STATUS_IS_OK(status)) {
2264 uint32 can_access_mask;
2265 bool can_access = True;
2267 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2269 /* Check if this can be done with the deny_dos and fcb
2272 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2273 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2275 DEBUG(0, ("DOS open without an SMB "
2278 return NT_STATUS_INTERNAL_ERROR;
2281 /* Use the client requested access mask here,
2282 * not the one we open with. */
2283 status = fcb_or_dos_open(req,
2294 if (NT_STATUS_IS_OK(status)) {
2297 *pinfo = FILE_WAS_OPENED;
2299 return NT_STATUS_OK;
2304 * This next line is a subtlety we need for
2305 * MS-Access. If a file open will fail due to share
2306 * permissions and also for security (access) reasons,
2307 * we need to return the access failed error, not the
2308 * share error. We can't open the file due to kernel
2309 * oplock deadlock (it's possible we failed above on
2310 * the open_mode_check()) so use a userspace check.
2313 if (flags & O_RDWR) {
2314 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2315 } else if (flags & O_WRONLY) {
2316 can_access_mask = FILE_WRITE_DATA;
2318 can_access_mask = FILE_READ_DATA;
2321 if (((can_access_mask & FILE_WRITE_DATA) &&
2322 !CAN_WRITE(conn)) ||
2323 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2326 can_access_mask))) {
2331 * If we're returning a share violation, ensure we
2332 * cope with the braindead 1 second delay.
2335 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2336 lp_defer_sharing_violations()) {
2337 struct timeval timeout;
2338 struct deferred_open_record state;
2341 /* this is a hack to speed up torture tests
2343 timeout_usecs = lp_parm_int(SNUM(conn),
2344 "smbd","sharedelay",
2345 SHARING_VIOLATION_USEC_WAIT);
2347 /* This is a relative time, added to the absolute
2348 request_time value to get the absolute timeout time.
2349 Note that if this is the second or greater time we enter
2350 this codepath for this particular request mid then
2351 request_time is left as the absolute time of the *first*
2352 time this request mid was processed. This is what allows
2353 the request to eventually time out. */
2355 timeout = timeval_set(0, timeout_usecs);
2357 /* Nothing actually uses state.delayed_for_oplocks
2358 but it's handy to differentiate in debug messages
2359 between a 30 second delay due to oplock break, and
2360 a 1 second delay for share mode conflicts. */
2362 state.delayed_for_oplocks = False;
2363 state.async_open = false;
2367 && !request_timed_out(request_time,
2369 defer_open(lck, request_time, timeout,
2377 * We have detected a sharing violation here
2378 * so return the correct error code
2380 status = NT_STATUS_SHARING_VIOLATION;
2382 status = NT_STATUS_ACCESS_DENIED;
2388 * We exit this block with the share entry *locked*.....
2392 SMB_ASSERT(!file_existed || (lck != NULL));
2395 * Ensure we pay attention to default ACLs on directories if required.
2398 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2399 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2400 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2403 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2404 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2405 (unsigned int)flags, (unsigned int)flags2,
2406 (unsigned int)unx_mode, (unsigned int)access_mask,
2407 (unsigned int)open_access_mask));
2409 fsp_open = open_file(fsp, conn, req, parent_dir,
2410 flags|flags2, unx_mode, access_mask,
2411 open_access_mask, &new_file_created);
2413 if (!NT_STATUS_IS_OK(fsp_open)) {
2414 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2415 schedule_async_open(request_time, req);
2421 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2423 * The file did exist, but some other (local or NFS)
2424 * process either renamed/unlinked and re-created the
2425 * file with different dev/ino after we walked the path,
2426 * but before we did the open. We could retry the
2427 * open but it's a rare enough case it's easier to
2428 * just fail the open to prevent creating any problems
2429 * in the open file db having the wrong dev/ino key.
2433 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2434 "Old (dev=0x%llu, ino =0x%llu). "
2435 "New (dev=0x%llu, ino=0x%llu). Failing open "
2436 " with NT_STATUS_ACCESS_DENIED.\n",
2437 smb_fname_str_dbg(smb_fname),
2438 (unsigned long long)saved_stat.st_ex_dev,
2439 (unsigned long long)saved_stat.st_ex_ino,
2440 (unsigned long long)smb_fname->st.st_ex_dev,
2441 (unsigned long long)smb_fname->st.st_ex_ino));
2442 return NT_STATUS_ACCESS_DENIED;
2445 if (!file_existed) {
2446 struct share_mode_entry *batch_entry = NULL;
2447 struct share_mode_entry *exclusive_entry = NULL;
2448 bool got_level2_oplock = false;
2449 bool got_a_none_oplock = false;
2450 struct timespec old_write_time = smb_fname->st.st_ex_mtime;
2453 * Deal with the race condition where two smbd's detect the
2454 * file doesn't exist and do the create at the same time. One
2455 * of them will win and set a share mode, the other (ie. this
2456 * one) should check if the requested share mode for this
2457 * create is allowed.
2461 * Now the file exists and fsp is successfully opened,
2462 * fsp->dev and fsp->inode are valid and should replace the
2463 * dev=0,inode=0 from a non existent file. Spotted by
2464 * Nadav Danieli <nadavd@exanet.com>. JRA.
2469 lck = get_share_mode_lock(talloc_tos(), id,
2471 smb_fname, &old_write_time);
2474 DEBUG(0, ("open_file_ntcreate: Could not get share "
2475 "mode lock for %s\n",
2476 smb_fname_str_dbg(smb_fname)));
2478 return NT_STATUS_SHARING_VIOLATION;
2481 /* Get the types we need to examine. */
2482 find_oplock_types(fsp,
2488 &got_a_none_oplock);
2490 /* First pass - send break only on batch oplocks. */
2491 if ((req != NULL) &&
2492 delay_for_batch_oplocks(fsp,
2496 schedule_defer_open(lck, request_time, req);
2499 return NT_STATUS_SHARING_VIOLATION;
2502 status = open_mode_check(conn, lck, fsp->name_hash,
2503 access_mask, share_access,
2504 create_options, &file_existed);
2506 if (NT_STATUS_IS_OK(status)) {
2507 /* We might be going to allow this open. Check oplock
2509 /* Second pass - send break for both batch or
2510 * exclusive oplocks. */
2511 if ((req != NULL) &&
2512 delay_for_exclusive_oplocks(
2517 schedule_defer_open(lck, request_time, req);
2520 return NT_STATUS_SHARING_VIOLATION;
2524 if (!NT_STATUS_IS_OK(status)) {
2525 struct deferred_open_record state;
2527 state.delayed_for_oplocks = False;
2528 state.async_open = false;
2531 /* Do it all over again immediately. In the second
2532 * round we will find that the file existed and handle
2533 * the DELETE_PENDING and FCB cases correctly. No need
2534 * to duplicate the code here. Essentially this is a
2535 * "goto top of this function", but don't tell
2539 defer_open(lck, request_time, timeval_zero(),
2547 grant_fsp_oplock_type(fsp,
2553 * We exit this block with the share entry *locked*.....
2558 SMB_ASSERT(lck != NULL);
2560 /* Delete streams if create_disposition requires it */
2561 if (!new_file_created && clear_ads(create_disposition) &&
2562 !is_ntfs_stream_smb_fname(smb_fname)) {
2563 status = delete_all_streams(conn, smb_fname->base_name);
2564 if (!NT_STATUS_IS_OK(status)) {
2571 /* note that we ignore failure for the following. It is
2572 basically a hack for NFS, and NFS will never set one of
2573 these only read them. Nobody but Samba can ever set a deny
2574 mode and we have already checked our more authoritative
2575 locking database for permission to set this deny mode. If
2576 the kernel refuses the operations then the kernel is wrong.
2577 note that GPFS supports it as well - jmcd */
2579 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2581 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2582 if(ret_flock == -1 ){
2587 return NT_STATUS_SHARING_VIOLATION;
2592 * At this point onwards, we can guarentee that the share entry
2593 * is locked, whether we created the file or not, and that the
2594 * deny mode is compatible with all current opens.
2598 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2599 * but we don't have to store this - just ignore it on access check.
2601 if (conn->sconn->using_smb2) {
2603 * SMB2 doesn't return it (according to Microsoft tests).
2604 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2605 * File created with access = 0x7 (Read, Write, Delete)
2606 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2608 fsp->access_mask = access_mask;
2610 /* But SMB1 does. */
2611 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2615 /* stat opens on existing files don't get oplocks. */
2616 if (is_stat_open(open_access_mask)) {
2617 fsp->oplock_type = NO_OPLOCK;
2621 if (new_file_created) {
2622 info = FILE_WAS_CREATED;
2624 if (flags2 & O_TRUNC) {
2625 info = FILE_WAS_OVERWRITTEN;
2627 info = FILE_WAS_OPENED;
2636 * Setup the oplock info in both the shared memory and
2640 status = set_file_oplock(fsp, fsp->oplock_type);
2641 if (!NT_STATUS_IS_OK(status)) {
2643 * Could not get the kernel oplock or there are byte-range
2644 * locks on the file.
2646 fsp->oplock_type = NO_OPLOCK;
2649 set_share_mode(lck, fsp, get_current_uid(conn),
2653 /* Handle strange delete on close create semantics. */
2654 if (create_options & FILE_DELETE_ON_CLOSE) {
2656 status = can_set_delete_on_close(fsp, new_dos_attributes);
2658 if (!NT_STATUS_IS_OK(status)) {
2659 /* Remember to delete the mode we just added. */
2660 del_share_mode(lck, fsp);
2665 /* Note that here we set the *inital* delete on close flag,
2666 not the regular one. The magic gets handled in close. */
2667 fsp->initial_delete_on_close = True;
2670 if (info != FILE_WAS_OPENED) {
2671 /* Files should be initially set as archive */
2672 if (lp_map_archive(SNUM(conn)) ||
2673 lp_store_dos_attributes(SNUM(conn))) {
2675 if (file_set_dosmode(conn, smb_fname,
2676 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2677 parent_dir, true) == 0) {
2678 unx_mode = smb_fname->st.st_ex_mode;
2684 /* Determine sparse flag. */
2686 /* POSIX opens are sparse by default. */
2687 fsp->is_sparse = true;
2689 fsp->is_sparse = (file_existed &&
2690 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2694 * Take care of inherited ACLs on created files - if default ACL not
2698 if (!posix_open && new_file_created && !def_acl) {
2700 int saved_errno = errno; /* We might get ENOSYS in the next
2703 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2705 errno = saved_errno; /* Ignore ENOSYS */
2708 } else if (new_unx_mode) {
2712 /* Attributes need changing. File already existed. */
2715 int saved_errno = errno; /* We might get ENOSYS in the
2717 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2719 if (ret == -1 && errno == ENOSYS) {
2720 errno = saved_errno; /* Ignore ENOSYS */
2722 DEBUG(5, ("open_file_ntcreate: reset "
2723 "attributes of file %s to 0%o\n",
2724 smb_fname_str_dbg(smb_fname),
2725 (unsigned int)new_unx_mode));
2726 ret = 0; /* Don't do the fchmod below. */
2731 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2732 DEBUG(5, ("open_file_ntcreate: failed to reset "
2733 "attributes of file %s to 0%o\n",
2734 smb_fname_str_dbg(smb_fname),
2735 (unsigned int)new_unx_mode));
2738 /* If this is a successful open, we must remove any deferred open
2741 del_deferred_open_entry(lck, req->mid,
2742 messaging_server_id(req->sconn->msg_ctx));
2746 return NT_STATUS_OK;
2750 /****************************************************************************
2751 Open a file for for write to ensure that we can fchmod it.
2752 ****************************************************************************/
2754 NTSTATUS open_file_fchmod(connection_struct *conn,
2755 struct smb_filename *smb_fname,
2756 files_struct **result)
2758 if (!VALID_STAT(smb_fname->st)) {
2759 return NT_STATUS_INVALID_PARAMETER;
2762 return SMB_VFS_CREATE_FILE(
2765 0, /* root_dir_fid */
2766 smb_fname, /* fname */
2767 FILE_WRITE_DATA, /* access_mask */
2768 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2770 FILE_OPEN, /* create_disposition*/
2771 0, /* create_options */
2772 0, /* file_attributes */
2773 INTERNAL_OPEN_ONLY, /* oplock_request */
2774 0, /* allocation_size */
2775 0, /* private_flags */
2778 result, /* result */
2782 static NTSTATUS mkdir_internal(connection_struct *conn,
2783 struct smb_filename *smb_dname,
2784 uint32 file_attributes)
2787 char *parent_dir = NULL;
2789 bool posix_open = false;
2790 bool need_re_stat = false;
2791 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2793 if(access_mask & ~(conn->share_access)) {
2794 DEBUG(5,("mkdir_internal: failing share access "
2795 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2796 return NT_STATUS_ACCESS_DENIED;
2799 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2801 return NT_STATUS_NO_MEMORY;
2804 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2806 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2808 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2811 status = check_parent_access(conn,
2814 if(!NT_STATUS_IS_OK(status)) {
2815 DEBUG(5,("mkdir_internal: check_parent_access "
2816 "on directory %s for path %s returned %s\n",
2818 smb_dname->base_name,
2819 nt_errstr(status) ));
2823 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2824 return map_nt_error_from_unix(errno);
2827 /* Ensure we're checking for a symlink here.... */
2828 /* We don't want to get caught by a symlink racer. */
2830 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2831 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2832 smb_fname_str_dbg(smb_dname), strerror(errno)));
2833 return map_nt_error_from_unix(errno);
2836 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2837 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2838 smb_fname_str_dbg(smb_dname)));
2839 return NT_STATUS_NOT_A_DIRECTORY;
2842 if (lp_store_dos_attributes(SNUM(conn))) {
2844 file_set_dosmode(conn, smb_dname,
2845 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2850 if (lp_inherit_perms(SNUM(conn))) {
2851 inherit_access_posix_acl(conn, parent_dir,
2852 smb_dname->base_name, mode);
2853 need_re_stat = true;
2858 * Check if high bits should have been set,
2859 * then (if bits are missing): add them.
2860 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2863 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2864 (mode & ~smb_dname->st.st_ex_mode)) {
2865 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2866 (smb_dname->st.st_ex_mode |
2867 (mode & ~smb_dname->st.st_ex_mode)));
2868 need_re_stat = true;
2872 /* Change the owner if required. */
2873 if (lp_inherit_owner(SNUM(conn))) {
2874 change_dir_owner_to_parent(conn, parent_dir,
2875 smb_dname->base_name,
2877 need_re_stat = true;
2881 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2882 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2883 smb_fname_str_dbg(smb_dname), strerror(errno)));
2884 return map_nt_error_from_unix(errno);
2888 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2889 smb_dname->base_name);
2891 return NT_STATUS_OK;
2894 /****************************************************************************
2895 Open a directory from an NT SMB call.
2896 ****************************************************************************/
2898 static NTSTATUS open_directory(connection_struct *conn,
2899 struct smb_request *req,
2900 struct smb_filename *smb_dname,
2902 uint32 share_access,
2903 uint32 create_disposition,
2904 uint32 create_options,
2905 uint32 file_attributes,
2907 files_struct **result)
2909 files_struct *fsp = NULL;
2910 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2911 struct share_mode_lock *lck = NULL;
2913 struct timespec mtimespec;
2916 if (is_ntfs_stream_smb_fname(smb_dname)) {
2917 DEBUG(2, ("open_directory: %s is a stream name!\n",
2918 smb_fname_str_dbg(smb_dname)));
2919 return NT_STATUS_NOT_A_DIRECTORY;
2922 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2923 /* Ensure we have a directory attribute. */
2924 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2927 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2928 "share_access = 0x%x create_options = 0x%x, "
2929 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2930 smb_fname_str_dbg(smb_dname),
2931 (unsigned int)access_mask,
2932 (unsigned int)share_access,
2933 (unsigned int)create_options,
2934 (unsigned int)create_disposition,
2935 (unsigned int)file_attributes));
2937 status = smbd_calculate_access_mask(conn, smb_dname, false,
2938 access_mask, &access_mask);
2939 if (!NT_STATUS_IS_OK(status)) {
2940 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2941 "on file %s returned %s\n",
2942 smb_fname_str_dbg(smb_dname),
2943 nt_errstr(status)));
2947 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2948 !security_token_has_privilege(get_current_nttok(conn),
2949 SEC_PRIV_SECURITY)) {
2950 DEBUG(10, ("open_directory: open on %s "
2951 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2952 smb_fname_str_dbg(smb_dname)));
2953 return NT_STATUS_PRIVILEGE_NOT_HELD;
2956 switch( create_disposition ) {
2960 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2963 info = FILE_WAS_OPENED;
2968 /* If directory exists error. If directory doesn't
2972 status = NT_STATUS_OBJECT_NAME_COLLISION;
2973 DEBUG(2, ("open_directory: unable to create "
2974 "%s. Error was %s\n",
2975 smb_fname_str_dbg(smb_dname),
2976 nt_errstr(status)));
2980 status = mkdir_internal(conn, smb_dname,
2983 if (!NT_STATUS_IS_OK(status)) {
2984 DEBUG(2, ("open_directory: unable to create "
2985 "%s. Error was %s\n",
2986 smb_fname_str_dbg(smb_dname),
2987 nt_errstr(status)));
2991 info = FILE_WAS_CREATED;
2996 * If directory exists open. If directory doesn't
3001 status = NT_STATUS_OK;
3002 info = FILE_WAS_OPENED;
3004 status = mkdir_internal(conn, smb_dname,
3007 if (NT_STATUS_IS_OK(status)) {
3008 info = FILE_WAS_CREATED;
3010 /* Cope with create race. */
3011 if (!NT_STATUS_EQUAL(status,
3012 NT_STATUS_OBJECT_NAME_COLLISION)) {
3013 DEBUG(2, ("open_directory: unable to create "
3014 "%s. Error was %s\n",
3015 smb_fname_str_dbg(smb_dname),
3016 nt_errstr(status)));
3019 info = FILE_WAS_OPENED;
3025 case FILE_SUPERSEDE:
3026 case FILE_OVERWRITE:
3027 case FILE_OVERWRITE_IF:
3029 DEBUG(5,("open_directory: invalid create_disposition "
3030 "0x%x for directory %s\n",
3031 (unsigned int)create_disposition,
3032 smb_fname_str_dbg(smb_dname)));
3033 return NT_STATUS_INVALID_PARAMETER;
3036 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3037 DEBUG(5,("open_directory: %s is not a directory !\n",
3038 smb_fname_str_dbg(smb_dname)));
3039 return NT_STATUS_NOT_A_DIRECTORY;
3042 if (info == FILE_WAS_OPENED) {
3043 status = smbd_check_access_rights(conn,
3047 if (!NT_STATUS_IS_OK(status)) {
3048 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3049 "file %s failed with %s\n",
3050 smb_fname_str_dbg(smb_dname),
3051 nt_errstr(status)));
3056 status = file_new(req, conn, &fsp);
3057 if(!NT_STATUS_IS_OK(status)) {
3062 * Setup the files_struct for it.
3065 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3066 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3067 fsp->file_pid = req ? req->smbpid : 0;
3068 fsp->can_lock = False;
3069 fsp->can_read = False;
3070 fsp->can_write = False;
3072 fsp->share_access = share_access;
3073 fsp->fh->private_options = 0;
3075 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3077 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3078 fsp->print_file = NULL;
3079 fsp->modified = False;
3080 fsp->oplock_type = NO_OPLOCK;
3081 fsp->sent_oplock_break = NO_BREAK_SENT;
3082 fsp->is_directory = True;
3083 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3084 status = fsp_set_smb_fname(fsp, smb_dname);
3085 if (!NT_STATUS_IS_OK(status)) {
3086 file_free(req, fsp);
3090 mtimespec = smb_dname->st.st_ex_mtime;
3093 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3095 /* POSIX allows us to open a directory with O_RDONLY. */
3096 status = fd_open(conn, fsp, O_RDONLY, 0);
3098 if (!NT_STATUS_IS_OK(status)) {
3099 DEBUG(5, ("open_directory: Could not open fd for "
3101 smb_fname_str_dbg(smb_dname),
3102 nt_errstr(status)));
3103 file_free(req, fsp);
3107 status = vfs_stat_fsp(fsp);
3108 if (!NT_STATUS_IS_OK(status)) {
3110 file_free(req, fsp);
3114 /* Ensure there was no race condition. */
3115 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3116 DEBUG(5,("open_directory: stat struct differs for "
3118 smb_fname_str_dbg(smb_dname)));
3120 file_free(req, fsp);
3121 return NT_STATUS_ACCESS_DENIED;
3124 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3125 conn->connectpath, smb_dname,
3129 DEBUG(0, ("open_directory: Could not get share mode lock for "
3130 "%s\n", smb_fname_str_dbg(smb_dname)));
3132 file_free(req, fsp);
3133 return NT_STATUS_SHARING_VIOLATION;
3136 status = open_mode_check(conn, lck, fsp->name_hash,
3137 access_mask, share_access,
3138 create_options, &dir_existed);
3140 if (!NT_STATUS_IS_OK(status)) {
3143 file_free(req, fsp);
3147 set_share_mode(lck, fsp, get_current_uid(conn),
3148 req ? req->mid : 0, NO_OPLOCK);
3150 /* For directories the delete on close bit at open time seems
3151 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3152 if (create_options & FILE_DELETE_ON_CLOSE) {
3153 status = can_set_delete_on_close(fsp, 0);
3154 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3157 file_free(req, fsp);
3161 if (NT_STATUS_IS_OK(status)) {
3162 /* Note that here we set the *inital* delete on close flag,
3163 not the regular one. The magic gets handled in close. */
3164 fsp->initial_delete_on_close = True;
3175 return NT_STATUS_OK;
3178 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3179 struct smb_filename *smb_dname)
3184 status = SMB_VFS_CREATE_FILE(
3187 0, /* root_dir_fid */
3188 smb_dname, /* fname */
3189 FILE_READ_ATTRIBUTES, /* access_mask */
3190 FILE_SHARE_NONE, /* share_access */
3191 FILE_CREATE, /* create_disposition*/
3192 FILE_DIRECTORY_FILE, /* create_options */
3193 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3194 0, /* oplock_request */
3195 0, /* allocation_size */
3196 0, /* private_flags */
3202 if (NT_STATUS_IS_OK(status)) {
3203 close_file(req, fsp, NORMAL_CLOSE);
3209 /****************************************************************************
3210 Receive notification that one of our open files has been renamed by another
3212 ****************************************************************************/
3214 void msg_file_was_renamed(struct messaging_context *msg,
3217 struct server_id server_id,
3221 char *frm = (char *)data->data;
3223 const char *sharepath;
3224 const char *base_name;
3225 const char *stream_name;
3226 struct smb_filename *smb_fname = NULL;
3227 size_t sp_len, bn_len;
3229 struct smbd_server_connection *sconn =
3230 talloc_get_type_abort(private_data,
3231 struct smbd_server_connection);
3233 if (data->data == NULL
3234 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3235 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3236 (int)data->length));
3240 /* Unpack the message. */
3241 pull_file_id_24(frm, &id);
3242 sharepath = &frm[24];
3243 sp_len = strlen(sharepath);
3244 base_name = sharepath + sp_len + 1;
3245 bn_len = strlen(base_name);
3246 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3248 /* stream_name must always be NULL if there is no stream. */
3249 if (stream_name[0] == '\0') {
3253 status = create_synthetic_smb_fname(talloc_tos(), base_name,
3254 stream_name, NULL, &smb_fname);
3255 if (!NT_STATUS_IS_OK(status)) {
3259 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3261 sharepath, smb_fname_str_dbg(smb_fname),
3262 file_id_string_tos(&id)));
3264 for(fsp = file_find_di_first(sconn, id); fsp;
3265 fsp = file_find_di_next(fsp)) {
3266 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3268 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3269 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3270 smb_fname_str_dbg(smb_fname)));
3271 status = fsp_set_smb_fname(fsp, smb_fname);
3272 if (!NT_STATUS_IS_OK(status)) {
3277 /* Now we have the complete path we can work out if this is
3278 actually within this share and adjust newname accordingly. */
3279 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3280 "not sharepath %s) "
3281 "%s from %s -> %s\n",
3282 fsp->conn->connectpath,
3286 smb_fname_str_dbg(smb_fname)));
3290 TALLOC_FREE(smb_fname);
3295 * If a main file is opened for delete, all streams need to be checked for
3296 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3297 * If that works, delete them all by setting the delete on close and close.
3300 NTSTATUS open_streams_for_delete(connection_struct *conn,
3303 struct stream_struct *stream_info = NULL;
3304 files_struct **streams = NULL;
3306 unsigned int num_streams = 0;
3307 TALLOC_CTX *frame = talloc_stackframe();
3310 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3311 &num_streams, &stream_info);
3313 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3314 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3315 DEBUG(10, ("no streams around\n"));
3317 return NT_STATUS_OK;
3320 if (!NT_STATUS_IS_OK(status)) {
3321 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3322 nt_errstr(status)));
3326 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3329 if (num_streams == 0) {
3331 return NT_STATUS_OK;
3334 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3335 if (streams == NULL) {
3336 DEBUG(0, ("talloc failed\n"));
3337 status = NT_STATUS_NO_MEMORY;
3341 for (i=0; i<num_streams; i++) {
3342 struct smb_filename *smb_fname = NULL;
3344 if (strequal(stream_info[i].name, "::$DATA")) {
3349 status = create_synthetic_smb_fname(talloc_tos(), fname,
3350 stream_info[i].name,
3352 if (!NT_STATUS_IS_OK(status)) {
3356 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3357 DEBUG(10, ("Unable to stat stream: %s\n",
3358 smb_fname_str_dbg(smb_fname)));
3361 status = SMB_VFS_CREATE_FILE(
3364 0, /* root_dir_fid */
3365 smb_fname, /* fname */
3366 DELETE_ACCESS, /* access_mask */
3367 (FILE_SHARE_READ | /* share_access */
3368 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3369 FILE_OPEN, /* create_disposition*/
3370 0, /* create_options */
3371 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3372 0, /* oplock_request */
3373 0, /* allocation_size */
3374 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3377 &streams[i], /* result */
3380 if (!NT_STATUS_IS_OK(status)) {
3381 DEBUG(10, ("Could not open stream %s: %s\n",
3382 smb_fname_str_dbg(smb_fname),
3383 nt_errstr(status)));
3385 TALLOC_FREE(smb_fname);
3388 TALLOC_FREE(smb_fname);
3392 * don't touch the variable "status" beyond this point :-)
3395 for (i -= 1 ; i >= 0; i--) {
3396 if (streams[i] == NULL) {
3400 DEBUG(10, ("Closing stream # %d, %s\n", i,
3401 fsp_str_dbg(streams[i])));
3402 close_file(NULL, streams[i], NORMAL_CLOSE);
3410 /*********************************************************************
3411 Create a default ACL by inheriting from the parent. If no inheritance
3412 from the parent available, don't set anything. This will leave the actual
3413 permissions the new file or directory already got from the filesystem
3414 as the NT ACL when read.
3415 *********************************************************************/
3417 static NTSTATUS inherit_new_acl(files_struct *fsp)
3419 TALLOC_CTX *ctx = talloc_tos();
3420 char *parent_name = NULL;
3421 struct security_descriptor *parent_desc = NULL;
3422 NTSTATUS status = NT_STATUS_OK;
3423 struct security_descriptor *psd = NULL;
3424 struct dom_sid *owner_sid = NULL;
3425 struct dom_sid *group_sid = NULL;
3426 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3427 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3428 bool inheritable_components = false;
3431 if (!parent_dirname(ctx, fsp->fsp_name->base_name, &parent_name, NULL)) {
3432 return NT_STATUS_NO_MEMORY;
3435 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3437 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3439 if (!NT_STATUS_IS_OK(status)) {
3443 inheritable_components = sd_has_inheritable_components(parent_desc,
3446 if (!inheritable_components && !inherit_owner) {
3447 /* Nothing to inherit and not setting owner. */
3448 return NT_STATUS_OK;
3451 /* Create an inherited descriptor from the parent. */
3453 if (DEBUGLEVEL >= 10) {
3454 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3455 fsp_str_dbg(fsp) ));
3456 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3459 /* Inherit from parent descriptor if "inherit owner" set. */
3460 if (inherit_owner) {
3461 owner_sid = parent_desc->owner_sid;
3462 group_sid = parent_desc->group_sid;
3465 if (owner_sid == NULL) {
3466 owner_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
3468 if (group_sid == NULL) {
3469 group_sid = &fsp->conn->session_info->security_token->sids[PRIMARY_GROUP_SID_INDEX];
3472 status = se_create_child_secdesc(ctx,
3479 if (!NT_STATUS_IS_OK(status)) {
3483 /* If inheritable_components == false,
3484 se_create_child_secdesc()
3485 creates a security desriptor with a NULL dacl
3486 entry, but with SEC_DESC_DACL_PRESENT. We need
3487 to remove that flag. */
3489 if (!inheritable_components) {
3490 security_info_sent &= ~SECINFO_DACL;
3491 psd->type &= ~SEC_DESC_DACL_PRESENT;
3494 if (DEBUGLEVEL >= 10) {
3495 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3496 fsp_str_dbg(fsp) ));
3497 NDR_PRINT_DEBUG(security_descriptor, psd);
3500 if (inherit_owner) {
3501 /* We need to be root to force this. */
3504 status = SMB_VFS_FSET_NT_ACL(fsp,
3507 if (inherit_owner) {
3514 * Wrapper around open_file_ntcreate and open_directory
3517 static NTSTATUS create_file_unixpath(connection_struct *conn,
3518 struct smb_request *req,
3519 struct smb_filename *smb_fname,
3520 uint32_t access_mask,
3521 uint32_t share_access,
3522 uint32_t create_disposition,
3523 uint32_t create_options,
3524 uint32_t file_attributes,
3525 uint32_t oplock_request,
3526 uint64_t allocation_size,
3527 uint32_t private_flags,
3528 struct security_descriptor *sd,
3529 struct ea_list *ea_list,
3531 files_struct **result,
3534 int info = FILE_WAS_OPENED;
3535 files_struct *base_fsp = NULL;
3536 files_struct *fsp = NULL;
3539 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3540 "file_attributes = 0x%x, share_access = 0x%x, "
3541 "create_disposition = 0x%x create_options = 0x%x "
3542 "oplock_request = 0x%x private_flags = 0x%x "
3543 "ea_list = 0x%p, sd = 0x%p, "
3545 (unsigned int)access_mask,
3546 (unsigned int)file_attributes,
3547 (unsigned int)share_access,
3548 (unsigned int)create_disposition,
3549 (unsigned int)create_options,
3550 (unsigned int)oplock_request,
3551 (unsigned int)private_flags,
3552 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3554 if (create_options & FILE_OPEN_BY_FILE_ID) {
3555 status = NT_STATUS_NOT_SUPPORTED;
3559 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3560 status = NT_STATUS_INVALID_PARAMETER;
3565 oplock_request |= INTERNAL_OPEN_ONLY;
3568 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3569 && (access_mask & DELETE_ACCESS)
3570 && !is_ntfs_stream_smb_fname(smb_fname)) {
3572 * We can't open a file with DELETE access if any of the
3573 * streams is open without FILE_SHARE_DELETE
3575 status = open_streams_for_delete(conn, smb_fname->base_name);
3577 if (!NT_STATUS_IS_OK(status)) {
3582 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3583 !security_token_has_privilege(get_current_nttok(conn),
3584 SEC_PRIV_SECURITY)) {
3585 DEBUG(10, ("create_file_unixpath: open on %s "
3586 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3587 smb_fname_str_dbg(smb_fname)));
3588 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3592 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3593 && is_ntfs_stream_smb_fname(smb_fname)
3594 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3595 uint32 base_create_disposition;
3596 struct smb_filename *smb_fname_base = NULL;
3598 if (create_options & FILE_DIRECTORY_FILE) {
3599 status = NT_STATUS_NOT_A_DIRECTORY;
3603 switch (create_disposition) {
3605 base_create_disposition = FILE_OPEN;
3608 base_create_disposition = FILE_OPEN_IF;
3612 /* Create an smb_filename with stream_name == NULL. */
3613 status = create_synthetic_smb_fname(talloc_tos(),
3614 smb_fname->base_name,
3617 if (!NT_STATUS_IS_OK(status)) {
3621 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3622 DEBUG(10, ("Unable to stat stream: %s\n",
3623 smb_fname_str_dbg(smb_fname_base)));
3626 /* Open the base file. */
3627 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3630 | FILE_SHARE_DELETE,
3631 base_create_disposition,
3632 0, 0, 0, 0, 0, NULL, NULL,
3634 TALLOC_FREE(smb_fname_base);
3636 if (!NT_STATUS_IS_OK(status)) {
3637 DEBUG(10, ("create_file_unixpath for base %s failed: "
3638 "%s\n", smb_fname->base_name,
3639 nt_errstr(status)));
3642 /* we don't need to low level fd */
3647 * If it's a request for a directory open, deal with it separately.
3650 if (create_options & FILE_DIRECTORY_FILE) {
3652 if (create_options & FILE_NON_DIRECTORY_FILE) {
3653 status = NT_STATUS_INVALID_PARAMETER;
3657 /* Can't open a temp directory. IFS kit test. */
3658 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3659 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3660 status = NT_STATUS_INVALID_PARAMETER;
3665 * We will get a create directory here if the Win32
3666 * app specified a security descriptor in the
3667 * CreateDirectory() call.
3671 status = open_directory(
3672 conn, req, smb_fname, access_mask, share_access,
3673 create_disposition, create_options, file_attributes,
3678 * Ordinary file case.
3681 status = file_new(req, conn, &fsp);
3682 if(!NT_STATUS_IS_OK(status)) {
3686 status = fsp_set_smb_fname(fsp, smb_fname);
3687 if (!NT_STATUS_IS_OK(status)) {
3693 * We're opening the stream element of a
3694 * base_fsp we already opened. Set up the
3697 fsp->base_fsp = base_fsp;
3700 if (allocation_size) {
3701 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3705 status = open_file_ntcreate(conn,
3717 if(!NT_STATUS_IS_OK(status)) {
3718 file_free(req, fsp);
3722 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3724 /* A stream open never opens a directory */
3727 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3732 * Fail the open if it was explicitly a non-directory
3736 if (create_options & FILE_NON_DIRECTORY_FILE) {
3737 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3742 status = open_directory(
3743 conn, req, smb_fname, access_mask,
3744 share_access, create_disposition,
3745 create_options, file_attributes,
3750 if (!NT_STATUS_IS_OK(status)) {
3754 fsp->base_fsp = base_fsp;
3756 if ((ea_list != NULL) &&
3757 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3758 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3759 if (!NT_STATUS_IS_OK(status)) {
3764 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3765 status = NT_STATUS_ACCESS_DENIED;
3769 /* Save the requested allocation size. */
3770 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3772 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3773 fsp->initial_allocation_size = smb_roundup(
3774 fsp->conn, allocation_size);
3775 if (fsp->is_directory) {
3776 /* Can't set allocation size on a directory. */
3777 status = NT_STATUS_ACCESS_DENIED;
3780 if (vfs_allocate_file_space(
3781 fsp, fsp->initial_allocation_size) == -1) {
3782 status = NT_STATUS_DISK_FULL;
3786 fsp->initial_allocation_size = smb_roundup(
3787 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3790 fsp->initial_allocation_size = 0;
3793 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3794 fsp->base_fsp == NULL) {
3797 * According to the MS documentation, the only time the security
3798 * descriptor is applied to the opened file is iff we *created* the
3799 * file; an existing file stays the same.
3801 * Also, it seems (from observation) that you can open the file with
3802 * any access mask but you can still write the sd. We need to override
3803 * the granted access before we call set_sd
3804 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3807 uint32_t sec_info_sent;
3808 uint32_t saved_access_mask = fsp->access_mask;
3810 sec_info_sent = get_sec_info(sd);
3812 fsp->access_mask = FILE_GENERIC_ALL;
3814 if (sec_info_sent & (SECINFO_OWNER|
3818 status = set_sd(fsp, sd, sec_info_sent);
3821 fsp->access_mask = saved_access_mask;
3823 if (!NT_STATUS_IS_OK(status)) {
3826 } else if (lp_inherit_acls(SNUM(conn))) {
3827 /* Inherit from parent. Errors here are not fatal. */
3828 status = inherit_new_acl(fsp);
3829 if (!NT_STATUS_IS_OK(status)) {
3830 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3832 nt_errstr(status) ));
3837 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3840 if (pinfo != NULL) {
3844 smb_fname->st = fsp->fsp_name->st;
3846 return NT_STATUS_OK;
3849 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3852 if (base_fsp && fsp->base_fsp == base_fsp) {
3854 * The close_file below will close
3859 close_file(req, fsp, ERROR_CLOSE);
3862 if (base_fsp != NULL) {
3863 close_file(req, base_fsp, ERROR_CLOSE);
3870 * Calculate the full path name given a relative fid.
3872 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3873 struct smb_request *req,
3874 uint16_t root_dir_fid,
3875 const struct smb_filename *smb_fname,
3876 struct smb_filename **smb_fname_out)
3878 files_struct *dir_fsp;
3879 char *parent_fname = NULL;
3880 char *new_base_name = NULL;
3883 if (root_dir_fid == 0 || !smb_fname) {
3884 status = NT_STATUS_INTERNAL_ERROR;
3888 dir_fsp = file_fsp(req, root_dir_fid);
3890 if (dir_fsp == NULL) {
3891 status = NT_STATUS_INVALID_HANDLE;
3895 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
3896 status = NT_STATUS_INVALID_HANDLE;
3900 if (!dir_fsp->is_directory) {
3903 * Check to see if this is a mac fork of some kind.
3906 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3907 is_ntfs_stream_smb_fname(smb_fname)) {
3908 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3913 we need to handle the case when we get a
3914 relative open relative to a file and the
3915 pathname is blank - this is a reopen!
3916 (hint from demyn plantenberg)
3919 status = NT_STATUS_INVALID_HANDLE;
3923 if (ISDOT(dir_fsp->fsp_name->base_name)) {
3925 * We're at the toplevel dir, the final file name
3926 * must not contain ./, as this is filtered out
3927 * normally by srvstr_get_path and unix_convert
3928 * explicitly rejects paths containing ./.
3930 parent_fname = talloc_strdup(talloc_tos(), "");
3931 if (parent_fname == NULL) {
3932 status = NT_STATUS_NO_MEMORY;
3936 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
3939 * Copy in the base directory name.
3942 parent_fname = talloc_array(talloc_tos(), char,
3944 if (parent_fname == NULL) {
3945 status = NT_STATUS_NO_MEMORY;
3948 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
3952 * Ensure it ends in a '/'.
3953 * We used TALLOC_SIZE +2 to add space for the '/'.
3957 && (parent_fname[dir_name_len-1] != '\\')
3958 && (parent_fname[dir_name_len-1] != '/')) {
3959 parent_fname[dir_name_len] = '/';
3960 parent_fname[dir_name_len+1] = '\0';
3964 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3965 smb_fname->base_name);
3966 if (new_base_name == NULL) {
3967 status = NT_STATUS_NO_MEMORY;
3971 status = filename_convert(req,
3973 req->flags2 & FLAGS2_DFS_PATHNAMES,
3978 if (!NT_STATUS_IS_OK(status)) {
3983 TALLOC_FREE(parent_fname);
3984 TALLOC_FREE(new_base_name);
3988 NTSTATUS create_file_default(connection_struct *conn,
3989 struct smb_request *req,
3990 uint16_t root_dir_fid,
3991 struct smb_filename *smb_fname,
3992 uint32_t access_mask,
3993 uint32_t share_access,
3994 uint32_t create_disposition,
3995 uint32_t create_options,
3996 uint32_t file_attributes,
3997 uint32_t oplock_request,
3998 uint64_t allocation_size,
3999 uint32_t private_flags,
4000 struct security_descriptor *sd,
4001 struct ea_list *ea_list,
4002 files_struct **result,
4005 int info = FILE_WAS_OPENED;
4006 files_struct *fsp = NULL;
4008 bool stream_name = false;
4010 DEBUG(10,("create_file: access_mask = 0x%x "
4011 "file_attributes = 0x%x, share_access = 0x%x, "
4012 "create_disposition = 0x%x create_options = 0x%x "
4013 "oplock_request = 0x%x "
4014 "private_flags = 0x%x "
4015 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4017 (unsigned int)access_mask,
4018 (unsigned int)file_attributes,
4019 (unsigned int)share_access,
4020 (unsigned int)create_disposition,
4021 (unsigned int)create_options,
4022 (unsigned int)oplock_request,
4023 (unsigned int)private_flags,
4024 (unsigned int)root_dir_fid,
4025 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4028 * Calculate the filename from the root_dir_if if necessary.
4031 if (root_dir_fid != 0) {
4032 struct smb_filename *smb_fname_out = NULL;
4033 status = get_relative_fid_filename(conn, req, root_dir_fid,
4034 smb_fname, &smb_fname_out);
4035 if (!NT_STATUS_IS_OK(status)) {
4038 smb_fname = smb_fname_out;
4042 * Check to see if this is a mac fork of some kind.
4045 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4047 enum FAKE_FILE_TYPE fake_file_type;
4049 fake_file_type = is_fake_file(smb_fname);
4051 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4054 * Here we go! support for changing the disk quotas
4057 * We need to fake up to open this MAGIC QUOTA file
4058 * and return a valid FID.
4060 * w2k close this file directly after openening xp
4061 * also tries a QUERY_FILE_INFO on the file and then
4064 status = open_fake_file(req, conn, req->vuid,
4065 fake_file_type, smb_fname,
4067 if (!NT_STATUS_IS_OK(status)) {
4071 ZERO_STRUCT(smb_fname->st);
4075 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4076 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4081 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4083 smb_fname->stream_name = NULL;
4084 /* We have to handle this error here. */
4085 if (create_options & FILE_DIRECTORY_FILE) {
4086 status = NT_STATUS_NOT_A_DIRECTORY;
4089 if (lp_posix_pathnames()) {
4090 ret = SMB_VFS_LSTAT(conn, smb_fname);
4092 ret = SMB_VFS_STAT(conn, smb_fname);
4095 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4096 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4101 status = create_file_unixpath(
4102 conn, req, smb_fname, access_mask, share_access,
4103 create_disposition, create_options, file_attributes,
4104 oplock_request, allocation_size, private_flags,
4108 if (!NT_STATUS_IS_OK(status)) {
4113 DEBUG(10, ("create_file: info=%d\n", info));
4116 if (pinfo != NULL) {
4119 return NT_STATUS_OK;
4122 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4125 close_file(req, fsp, ERROR_CLOSE);