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"
31 #include "../librpc/gen_ndr/idmap.h"
32 #include "passdb/lookup_sid.h"
36 #include "source3/lib/dbwrap/dbwrap_watch.h"
38 extern const struct generic_mapping file_generic_mapping;
40 struct deferred_open_record {
41 bool delayed_for_oplocks;
46 /****************************************************************************
47 If the requester wanted DELETE_ACCESS and was rejected because
48 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
50 ****************************************************************************/
52 static bool parent_override_delete(connection_struct *conn,
53 const struct smb_filename *smb_fname,
55 uint32_t rejected_mask)
57 if ((access_mask & DELETE_ACCESS) &&
58 (rejected_mask & DELETE_ACCESS) &&
59 can_delete_file_in_directory(conn, smb_fname)) {
65 /****************************************************************************
66 Check if we have open rights.
67 ****************************************************************************/
69 NTSTATUS smbd_check_access_rights(struct connection_struct *conn,
70 const struct smb_filename *smb_fname,
74 /* Check if we have rights to open. */
76 struct security_descriptor *sd = NULL;
77 uint32_t rejected_share_access;
78 uint32_t rejected_mask = access_mask;
79 uint32_t do_not_check_mask = 0;
81 rejected_share_access = access_mask & ~(conn->share_access);
83 if (rejected_share_access) {
84 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
86 (unsigned int)access_mask,
87 smb_fname_str_dbg(smb_fname),
88 (unsigned int)rejected_share_access ));
89 return NT_STATUS_ACCESS_DENIED;
92 if (!use_privs && get_current_uid(conn) == (uid_t)0) {
93 /* I'm sorry sir, I didn't know you were root... */
94 DEBUG(10,("smbd_check_access_rights: root override "
95 "on %s. Granting 0x%x\n",
96 smb_fname_str_dbg(smb_fname),
97 (unsigned int)access_mask ));
101 if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) {
102 DEBUG(10,("smbd_check_access_rights: not checking ACL "
103 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
104 smb_fname_str_dbg(smb_fname),
105 (unsigned int)access_mask ));
109 if (access_mask == DELETE_ACCESS &&
110 VALID_STAT(smb_fname->st) &&
111 S_ISLNK(smb_fname->st.st_ex_mode)) {
112 /* We can always delete a symlink. */
113 DEBUG(10,("smbd_check_access_rights: not checking ACL "
114 "on DELETE_ACCESS on symlink %s.\n",
115 smb_fname_str_dbg(smb_fname) ));
119 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
122 SECINFO_DACL), talloc_tos(), &sd);
124 if (!NT_STATUS_IS_OK(status)) {
125 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
127 smb_fname_str_dbg(smb_fname),
130 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
138 * If we can access the path to this file, by
139 * default we have FILE_READ_ATTRIBUTES from the
140 * containing directory. See the section:
141 * "Algorithm to Check Access to an Existing File"
144 * se_file_access_check() also takes care of
145 * owner WRITE_DAC and READ_CONTROL.
147 do_not_check_mask = FILE_READ_ATTRIBUTES;
150 * Samba 3.6 and earlier granted execute access even
151 * if the ACL did not contain execute rights.
152 * Samba 4.0 is more correct and checks it.
153 * The compatibilty mode allows to skip this check
154 * to smoothen upgrades.
156 if (lp_acl_allow_execute_always(SNUM(conn))) {
157 do_not_check_mask |= FILE_EXECUTE;
160 status = se_file_access_check(sd,
161 get_current_nttok(conn),
163 (access_mask & ~do_not_check_mask),
166 DEBUG(10,("smbd_check_access_rights: file %s requesting "
167 "0x%x returning 0x%x (%s)\n",
168 smb_fname_str_dbg(smb_fname),
169 (unsigned int)access_mask,
170 (unsigned int)rejected_mask,
171 nt_errstr(status) ));
173 if (!NT_STATUS_IS_OK(status)) {
174 if (DEBUGLEVEL >= 10) {
175 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
176 smb_fname_str_dbg(smb_fname) ));
177 NDR_PRINT_DEBUG(security_descriptor, sd);
183 if (NT_STATUS_IS_OK(status) ||
184 !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
188 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
192 if ((access_mask & FILE_WRITE_ATTRIBUTES) &&
193 (rejected_mask & FILE_WRITE_ATTRIBUTES) &&
194 !lp_store_dos_attributes(SNUM(conn)) &&
195 (lp_map_readonly(SNUM(conn)) ||
196 lp_map_archive(SNUM(conn)) ||
197 lp_map_hidden(SNUM(conn)) ||
198 lp_map_system(SNUM(conn)))) {
199 rejected_mask &= ~FILE_WRITE_ATTRIBUTES;
201 DEBUG(10,("smbd_check_access_rights: "
203 "FILE_WRITE_ATTRIBUTES "
205 smb_fname_str_dbg(smb_fname)));
208 if (parent_override_delete(conn,
212 /* Were we trying to do an open
213 * for delete and didn't get DELETE
214 * access (only) ? Check if the
215 * directory allows DELETE_CHILD.
217 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
220 rejected_mask &= ~DELETE_ACCESS;
222 DEBUG(10,("smbd_check_access_rights: "
226 smb_fname_str_dbg(smb_fname)));
229 if (rejected_mask != 0) {
230 return NT_STATUS_ACCESS_DENIED;
235 static NTSTATUS check_parent_access(struct connection_struct *conn,
236 struct smb_filename *smb_fname,
237 uint32_t access_mask)
240 char *parent_dir = NULL;
241 struct security_descriptor *parent_sd = NULL;
242 uint32_t access_granted = 0;
244 if (!parent_dirname(talloc_tos(),
245 smb_fname->base_name,
248 return NT_STATUS_NO_MEMORY;
251 if (get_current_uid(conn) == (uid_t)0) {
252 /* I'm sorry sir, I didn't know you were root... */
253 DEBUG(10,("check_parent_access: root override "
254 "on %s. Granting 0x%x\n",
255 smb_fname_str_dbg(smb_fname),
256 (unsigned int)access_mask ));
260 status = SMB_VFS_GET_NT_ACL(conn,
266 if (!NT_STATUS_IS_OK(status)) {
267 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
268 "%s with error %s\n",
275 * If we can access the path to this file, by
276 * default we have FILE_READ_ATTRIBUTES from the
277 * containing directory. See the section:
278 * "Algorithm to Check Access to an Existing File"
281 * se_file_access_check() also takes care of
282 * owner WRITE_DAC and READ_CONTROL.
284 status = se_file_access_check(parent_sd,
285 get_current_nttok(conn),
287 (access_mask & ~FILE_READ_ATTRIBUTES),
289 if(!NT_STATUS_IS_OK(status)) {
290 DEBUG(5,("check_parent_access: access check "
291 "on directory %s for "
292 "path %s for mask 0x%x returned (0x%x) %s\n",
294 smb_fname->base_name,
297 nt_errstr(status) ));
304 /****************************************************************************
305 fd support routines - attempt to do a dos_open.
306 ****************************************************************************/
308 NTSTATUS fd_open(struct connection_struct *conn,
313 struct smb_filename *smb_fname = fsp->fsp_name;
314 NTSTATUS status = NT_STATUS_OK;
318 * Never follow symlinks on a POSIX client. The
319 * client should be doing this.
322 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
327 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode);
328 if (fsp->fh->fd == -1) {
329 int posix_errno = errno;
331 #if defined(ENOTSUP) && defined(OSF1)
332 /* handle special Tru64 errno */
333 if (errno == ENOTSUP) {
338 /* fix broken NetBSD errno */
339 if (errno == EFTYPE) {
343 /* fix broken FreeBSD errno */
344 if (errno == EMLINK) {
347 #endif /* O_NOFOLLOW */
348 status = map_nt_error_from_unix(posix_errno);
349 if (errno == EMFILE) {
350 static time_t last_warned = 0L;
352 if (time((time_t *) NULL) > last_warned) {
353 DEBUG(0,("Too many open files, unable "
354 "to open more! smbd's max "
356 lp_max_open_files()));
357 last_warned = time((time_t *) NULL);
363 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
364 smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd,
365 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
370 /****************************************************************************
371 Close the file associated with a fsp.
372 ****************************************************************************/
374 NTSTATUS fd_close(files_struct *fsp)
381 if (fsp->fh->fd == -1) {
382 return NT_STATUS_OK; /* What we used to call a stat open. */
384 if (fsp->fh->ref_count > 1) {
385 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
388 ret = SMB_VFS_CLOSE(fsp);
391 return map_nt_error_from_unix(errno);
396 /****************************************************************************
397 Change the ownership of a file to that of the parent directory.
398 Do this by fd if possible.
399 ****************************************************************************/
401 void change_file_owner_to_parent(connection_struct *conn,
402 const char *inherit_from_dir,
405 struct smb_filename *smb_fname_parent;
408 smb_fname_parent = synthetic_smb_fname(talloc_tos(), inherit_from_dir,
410 if (smb_fname_parent == NULL) {
414 ret = SMB_VFS_STAT(conn, smb_fname_parent);
416 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
417 "directory %s. Error was %s\n",
418 smb_fname_str_dbg(smb_fname_parent),
420 TALLOC_FREE(smb_fname_parent);
424 if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) {
425 /* Already this uid - no need to change. */
426 DEBUG(10,("change_file_owner_to_parent: file %s "
427 "is already owned by uid %d\n",
429 (int)fsp->fsp_name->st.st_ex_uid ));
430 TALLOC_FREE(smb_fname_parent);
435 ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1);
438 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
439 "file %s to parent directory uid %u. Error "
440 "was %s\n", fsp_str_dbg(fsp),
441 (unsigned int)smb_fname_parent->st.st_ex_uid,
444 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
445 "parent directory uid %u.\n", fsp_str_dbg(fsp),
446 (unsigned int)smb_fname_parent->st.st_ex_uid));
447 /* Ensure the uid entry is updated. */
448 fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid;
451 TALLOC_FREE(smb_fname_parent);
454 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
455 const char *inherit_from_dir,
457 SMB_STRUCT_STAT *psbuf)
459 struct smb_filename *smb_fname_parent;
460 struct smb_filename *smb_fname_cwd = NULL;
461 char *saved_dir = NULL;
462 TALLOC_CTX *ctx = talloc_tos();
463 NTSTATUS status = NT_STATUS_OK;
466 smb_fname_parent = synthetic_smb_fname(ctx, inherit_from_dir,
468 if (smb_fname_parent == NULL) {
469 return NT_STATUS_NO_MEMORY;
472 ret = SMB_VFS_STAT(conn, smb_fname_parent);
474 status = map_nt_error_from_unix(errno);
475 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
476 "directory %s. Error was %s\n",
477 smb_fname_str_dbg(smb_fname_parent),
482 /* We've already done an lstat into psbuf, and we know it's a
483 directory. If we can cd into the directory and the dev/ino
484 are the same then we can safely chown without races as
485 we're locking the directory in place by being in it. This
486 should work on any UNIX (thanks tridge :-). JRA.
489 saved_dir = vfs_GetWd(ctx,conn);
491 status = map_nt_error_from_unix(errno);
492 DEBUG(0,("change_dir_owner_to_parent: failed to get "
493 "current working directory. Error was %s\n",
498 /* Chdir into the new path. */
499 if (vfs_ChDir(conn, fname) == -1) {
500 status = map_nt_error_from_unix(errno);
501 DEBUG(0,("change_dir_owner_to_parent: failed to change "
502 "current working directory to %s. Error "
503 "was %s\n", fname, strerror(errno) ));
507 smb_fname_cwd = synthetic_smb_fname(ctx, ".", NULL, NULL);
508 if (smb_fname_cwd == NULL) {
509 status = NT_STATUS_NO_MEMORY;
513 ret = SMB_VFS_STAT(conn, smb_fname_cwd);
515 status = map_nt_error_from_unix(errno);
516 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
517 "directory '.' (%s) Error was %s\n",
518 fname, strerror(errno)));
522 /* Ensure we're pointing at the same place. */
523 if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev ||
524 smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) {
525 DEBUG(0,("change_dir_owner_to_parent: "
526 "device/inode on directory %s changed. "
527 "Refusing to chown !\n", fname ));
528 status = NT_STATUS_ACCESS_DENIED;
532 if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) {
533 /* Already this uid - no need to change. */
534 DEBUG(10,("change_dir_owner_to_parent: directory %s "
535 "is already owned by uid %d\n",
537 (int)smb_fname_cwd->st.st_ex_uid ));
538 status = NT_STATUS_OK;
543 ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid,
547 status = map_nt_error_from_unix(errno);
548 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
549 "directory %s to parent directory uid %u. "
550 "Error was %s\n", fname,
551 (unsigned int)smb_fname_parent->st.st_ex_uid,
554 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
555 "directory %s to parent directory uid %u.\n",
556 fname, (unsigned int)smb_fname_parent->st.st_ex_uid ));
557 /* Ensure the uid entry is updated. */
558 psbuf->st_ex_uid = smb_fname_parent->st.st_ex_uid;
562 vfs_ChDir(conn,saved_dir);
564 TALLOC_FREE(smb_fname_parent);
565 TALLOC_FREE(smb_fname_cwd);
569 /****************************************************************************
570 Open a file - returning a guaranteed ATOMIC indication of if the
571 file was created or not.
572 ****************************************************************************/
574 static NTSTATUS fd_open_atomic(struct connection_struct *conn,
580 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
581 bool file_existed = VALID_STAT(fsp->fsp_name->st);
583 *file_created = false;
585 if (!(flags & O_CREAT)) {
587 * We're not creating the file, just pass through.
589 return fd_open(conn, fsp, flags, mode);
592 if (flags & O_EXCL) {
594 * Fail if already exists, just pass through.
596 status = fd_open(conn, fsp, flags, mode);
599 * Here we've opened with O_CREAT|O_EXCL. If that went
600 * NT_STATUS_OK, we *know* we created this file.
602 *file_created = NT_STATUS_IS_OK(status);
608 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
609 * To know absolutely if we created the file or not,
610 * we can never call O_CREAT without O_EXCL. So if
611 * we think the file existed, try without O_CREAT|O_EXCL.
612 * If we think the file didn't exist, try with
613 * O_CREAT|O_EXCL. Keep bouncing between these two
614 * requests until either the file is created, or
615 * opened. Either way, we keep going until we get
616 * a returnable result (error, or open/create).
620 int curr_flags = flags;
623 /* Just try open, do not create. */
624 curr_flags &= ~(O_CREAT);
625 status = fd_open(conn, fsp, curr_flags, mode);
626 if (NT_STATUS_EQUAL(status,
627 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
629 * Someone deleted it in the meantime.
632 file_existed = false;
633 DEBUG(10,("fd_open_atomic: file %s existed. "
635 smb_fname_str_dbg(fsp->fsp_name)));
639 /* Try create exclusively, fail if it exists. */
640 curr_flags |= O_EXCL;
641 status = fd_open(conn, fsp, curr_flags, mode);
642 if (NT_STATUS_EQUAL(status,
643 NT_STATUS_OBJECT_NAME_COLLISION)) {
645 * Someone created it in the meantime.
646 * Retry without O_CREAT.
649 DEBUG(10,("fd_open_atomic: file %s "
650 "did not exist. Retry.\n",
651 smb_fname_str_dbg(fsp->fsp_name)));
654 if (NT_STATUS_IS_OK(status)) {
656 * Here we've opened with O_CREAT|O_EXCL
657 * and got success. We *know* we created
660 *file_created = true;
663 /* Create is done, or failed. */
669 /****************************************************************************
671 ****************************************************************************/
673 static NTSTATUS open_file(files_struct *fsp,
674 connection_struct *conn,
675 struct smb_request *req,
676 const char *parent_dir,
679 uint32 access_mask, /* client requested access mask. */
680 uint32 open_access_mask, /* what we're actually using in the open. */
681 bool *p_file_created)
683 struct smb_filename *smb_fname = fsp->fsp_name;
684 NTSTATUS status = NT_STATUS_OK;
685 int accmode = (flags & O_ACCMODE);
686 int local_flags = flags;
687 bool file_existed = VALID_STAT(fsp->fsp_name->st);
692 /* Check permissions */
695 * This code was changed after seeing a client open request
696 * containing the open mode of (DENY_WRITE/read-only) with
697 * the 'create if not exist' bit set. The previous code
698 * would fail to open the file read only on a read-only share
699 * as it was checking the flags parameter directly against O_RDONLY,
700 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
704 if (!CAN_WRITE(conn)) {
705 /* It's a read-only share - fail if we wanted to write. */
706 if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) {
707 DEBUG(3,("Permission denied opening %s\n",
708 smb_fname_str_dbg(smb_fname)));
709 return NT_STATUS_ACCESS_DENIED;
711 if (flags & O_CREAT) {
712 /* We don't want to write - but we must make sure that
713 O_CREAT doesn't create the file if we have write
714 access into the directory.
716 flags &= ~(O_CREAT|O_EXCL);
717 local_flags &= ~(O_CREAT|O_EXCL);
722 * This little piece of insanity is inspired by the
723 * fact that an NT client can open a file for O_RDONLY,
724 * but set the create disposition to FILE_EXISTS_TRUNCATE.
725 * If the client *can* write to the file, then it expects to
726 * truncate the file, even though it is opening for readonly.
727 * Quicken uses this stupid trick in backup file creation...
728 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
729 * for helping track this one down. It didn't bite us in 2.0.x
730 * as we always opened files read-write in that release. JRA.
733 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
734 DEBUG(10,("open_file: truncate requested on read-only open "
735 "for file %s\n", smb_fname_str_dbg(smb_fname)));
736 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
739 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
740 (!file_existed && (local_flags & O_CREAT)) ||
741 ((local_flags & O_TRUNC) == O_TRUNC) ) {
745 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
747 * We would block on opening a FIFO with no one else on the
748 * other end. Do what we used to do and add O_NONBLOCK to the
752 if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
753 local_flags &= ~O_TRUNC; /* Can't truncate a FIFO. */
754 local_flags |= O_NONBLOCK;
758 /* Don't create files with Microsoft wildcard characters. */
761 * wildcard characters are allowed in stream names
762 * only test the basefilename
764 wild = fsp->base_fsp->fsp_name->base_name;
766 wild = smb_fname->base_name;
768 if ((local_flags & O_CREAT) && !file_existed &&
770 return NT_STATUS_OBJECT_NAME_INVALID;
773 /* Can we access this file ? */
774 if (!fsp->base_fsp) {
775 /* Only do this check on non-stream open. */
777 status = smbd_check_access_rights(conn,
781 } else if (local_flags & O_CREAT){
782 status = check_parent_access(conn,
786 /* File didn't exist and no O_CREAT. */
787 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
789 if (!NT_STATUS_IS_OK(status)) {
790 DEBUG(10,("open_file: "
794 "smbd_check_access_rights" :
795 "check_parent_access",
796 smb_fname_str_dbg(smb_fname),
797 nt_errstr(status) ));
802 /* Actually do the open */
803 status = fd_open_atomic(conn, fsp, local_flags,
804 unx_mode, p_file_created);
805 if (!NT_STATUS_IS_OK(status)) {
806 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
807 "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
808 nt_errstr(status),local_flags,flags));
812 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
814 /* If we have an fd, this stat should succeed. */
815 DEBUG(0,("Error doing fstat on open file %s "
817 smb_fname_str_dbg(smb_fname),
819 status = map_nt_error_from_unix(errno);
824 if (*p_file_created) {
825 /* We created this file. */
827 bool need_re_stat = false;
828 /* Do all inheritance work after we've
829 done a successful fstat call and filled
830 in the stat struct in fsp->fsp_name. */
832 /* Inherit the ACL if required */
833 if (lp_inherit_perms(SNUM(conn))) {
834 inherit_access_posix_acl(conn, parent_dir,
835 smb_fname->base_name,
840 /* Change the owner if required. */
841 if (lp_inherit_owner(SNUM(conn))) {
842 change_file_owner_to_parent(conn, parent_dir,
848 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
849 /* If we have an fd, this stat should succeed. */
851 DEBUG(0,("Error doing fstat on open file %s "
853 smb_fname_str_dbg(smb_fname),
858 notify_fname(conn, NOTIFY_ACTION_ADDED,
859 FILE_NOTIFY_CHANGE_FILE_NAME,
860 smb_fname->base_name);
863 fsp->fh->fd = -1; /* What we used to call a stat open. */
865 /* File must exist for a stat open. */
866 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
869 status = smbd_check_access_rights(conn,
874 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
876 S_ISLNK(smb_fname->st.st_ex_mode)) {
877 /* This is a POSIX stat open for delete
878 * or rename on a symlink that points
880 DEBUG(10,("open_file: allowing POSIX "
881 "open on bad symlink %s\n",
882 smb_fname_str_dbg(smb_fname)));
883 status = NT_STATUS_OK;
886 if (!NT_STATUS_IS_OK(status)) {
887 DEBUG(10,("open_file: "
888 "smbd_check_access_rights on file "
890 smb_fname_str_dbg(smb_fname),
891 nt_errstr(status) ));
897 * POSIX allows read-only opens of directories. We don't
898 * want to do this (we use a different code path for this)
899 * so catch a directory open and return an EISDIR. JRA.
902 if(S_ISDIR(smb_fname->st.st_ex_mode)) {
905 return NT_STATUS_FILE_IS_A_DIRECTORY;
908 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
909 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
910 fsp->file_pid = req ? req->smbpid : 0;
911 fsp->can_lock = True;
912 fsp->can_read = ((access_mask & FILE_READ_DATA) != 0);
915 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
916 fsp->print_file = NULL;
917 fsp->modified = False;
918 fsp->sent_oplock_break = NO_BREAK_SENT;
919 fsp->is_directory = False;
920 if (conn->aio_write_behind_list &&
921 is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
922 conn->case_sensitive)) {
923 fsp->aio_write_behind = True;
926 fsp->wcp = NULL; /* Write cache pointer. */
928 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
929 conn->session_info->unix_info->unix_name,
930 smb_fname_str_dbg(smb_fname),
931 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
932 conn->num_files_open));
938 /****************************************************************************
939 Check if we can open a file with a share mode.
940 Returns True if conflict, False if not.
941 ****************************************************************************/
943 static bool share_conflict(struct share_mode_entry *entry,
947 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
948 "entry->share_access = 0x%x, "
949 "entry->private_options = 0x%x\n",
950 (unsigned int)entry->access_mask,
951 (unsigned int)entry->share_access,
952 (unsigned int)entry->private_options));
954 if (server_id_is_disconnected(&entry->pid)) {
956 * note: cleanup should have been done by
957 * delay_for_batch_oplocks()
962 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
963 (unsigned int)access_mask, (unsigned int)share_access));
965 if ((entry->access_mask & (FILE_WRITE_DATA|
969 DELETE_ACCESS)) == 0) {
970 DEBUG(10,("share_conflict: No conflict due to "
971 "entry->access_mask = 0x%x\n",
972 (unsigned int)entry->access_mask ));
976 if ((access_mask & (FILE_WRITE_DATA|
980 DELETE_ACCESS)) == 0) {
981 DEBUG(10,("share_conflict: No conflict due to "
982 "access_mask = 0x%x\n",
983 (unsigned int)access_mask ));
987 #if 1 /* JRA TEST - Superdebug. */
988 #define CHECK_MASK(num, am, right, sa, share) \
989 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
990 (unsigned int)(num), (unsigned int)(am), \
991 (unsigned int)(right), (unsigned int)(am)&(right) )); \
992 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
993 (unsigned int)(num), (unsigned int)(sa), \
994 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
995 if (((am) & (right)) && !((sa) & (share))) { \
996 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
997 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
998 (unsigned int)(share) )); \
1002 #define CHECK_MASK(num, am, right, sa, share) \
1003 if (((am) & (right)) && !((sa) & (share))) { \
1004 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1005 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1006 (unsigned int)(share) )); \
1011 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1012 share_access, FILE_SHARE_WRITE);
1013 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
1014 entry->share_access, FILE_SHARE_WRITE);
1016 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
1017 share_access, FILE_SHARE_READ);
1018 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
1019 entry->share_access, FILE_SHARE_READ);
1021 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
1022 share_access, FILE_SHARE_DELETE);
1023 CHECK_MASK(6, access_mask, DELETE_ACCESS,
1024 entry->share_access, FILE_SHARE_DELETE);
1026 DEBUG(10,("share_conflict: No conflict.\n"));
1030 #if defined(DEVELOPER)
1031 static void validate_my_share_entries(struct smbd_server_connection *sconn,
1033 struct share_mode_entry *share_entry)
1035 struct server_id self = messaging_server_id(sconn->msg_ctx);
1038 if (!serverid_equal(&self, &share_entry->pid)) {
1042 if (!is_valid_share_mode_entry(share_entry)) {
1046 fsp = file_find_dif(sconn, share_entry->id,
1047 share_entry->share_file_id);
1049 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1050 share_mode_str(talloc_tos(), num, share_entry) ));
1051 smb_panic("validate_my_share_entries: Cannot match a "
1052 "share entry with an open file\n");
1055 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
1064 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1065 share_mode_str(talloc_tos(), num, share_entry) ));
1066 str = talloc_asprintf(talloc_tos(),
1067 "validate_my_share_entries: "
1068 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1069 fsp->fsp_name->base_name,
1070 (unsigned int)fsp->oplock_type,
1071 (unsigned int)share_entry->op_type );
1077 bool is_stat_open(uint32 access_mask)
1079 const uint32_t stat_open_bits =
1080 (SYNCHRONIZE_ACCESS|
1081 FILE_READ_ATTRIBUTES|
1082 FILE_WRITE_ATTRIBUTES);
1084 return (((access_mask & stat_open_bits) != 0) &&
1085 ((access_mask & ~stat_open_bits) == 0));
1088 static bool has_delete_on_close(struct share_mode_lock *lck,
1091 struct share_mode_data *d = lck->data;
1094 if (d->num_share_modes == 0) {
1097 if (!is_delete_on_close_set(lck, name_hash)) {
1100 for (i=0; i<d->num_share_modes; i++) {
1101 if (!share_mode_stale_pid(d, i)) {
1108 /****************************************************************************
1109 Deal with share modes
1110 Invarient: Share mode must be locked on entry and exit.
1111 Returns -1 on error, or number of share modes on success (may be zero).
1112 ****************************************************************************/
1114 static NTSTATUS open_mode_check(connection_struct *conn,
1115 struct share_mode_lock *lck,
1117 uint32 share_access,
1122 if(lck->data->num_share_modes == 0) {
1123 return NT_STATUS_OK;
1126 if (is_stat_open(access_mask)) {
1127 /* Stat open that doesn't trigger oplock breaks or share mode
1128 * checks... ! JRA. */
1129 return NT_STATUS_OK;
1133 * Check if the share modes will give us access.
1136 #if defined(DEVELOPER)
1137 for(i = 0; i < lck->data->num_share_modes; i++) {
1138 validate_my_share_entries(conn->sconn, i,
1139 &lck->data->share_modes[i]);
1143 /* Now we check the share modes, after any oplock breaks. */
1144 for(i = 0; i < lck->data->num_share_modes; i++) {
1146 if (!is_valid_share_mode_entry(&lck->data->share_modes[i])) {
1150 /* someone else has a share lock on it, check to see if we can
1152 if (share_conflict(&lck->data->share_modes[i],
1153 access_mask, share_access)) {
1155 if (share_mode_stale_pid(lck->data, i)) {
1159 *file_existed = true;
1161 return NT_STATUS_SHARING_VIOLATION;
1165 if (lck->data->num_share_modes != 0) {
1166 *file_existed = true;
1169 return NT_STATUS_OK;
1173 * Send a break message to the oplock holder and delay the open for
1177 static NTSTATUS send_break_message(files_struct *fsp,
1178 struct share_mode_entry *exclusive,
1183 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1185 DEBUG(10, ("Sending break request to PID %s\n",
1186 procid_str_static(&exclusive->pid)));
1187 exclusive->op_mid = mid;
1189 /* Create the message. */
1190 share_mode_entry_to_message(msg, exclusive);
1192 status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid,
1193 MSG_SMB_BREAK_REQUEST,
1194 (uint8 *)msg, sizeof(msg));
1195 if (!NT_STATUS_IS_OK(status)) {
1196 DEBUG(3, ("Could not send oplock break message: %s\n",
1197 nt_errstr(status)));
1204 * Return share_mode_entry pointers for :
1205 * 1). Batch oplock entry.
1206 * 2). Batch or exclusive oplock entry (may be identical to #1).
1207 * bool have_level2_oplock
1208 * bool have_no_oplock.
1209 * Do internal consistency checks on the share mode for a file.
1212 static void find_oplock_types(files_struct *fsp,
1214 const struct share_mode_lock *lck,
1215 struct share_mode_entry **pp_batch,
1216 struct share_mode_entry **pp_ex_or_batch,
1218 bool *got_no_oplock)
1220 struct share_mode_data *d = lck->data;
1224 *pp_ex_or_batch = NULL;
1225 *got_level2 = false;
1226 *got_no_oplock = false;
1228 /* Ignore stat or internal opens, as is done in
1229 delay_for_batch_oplocks() and
1230 delay_for_exclusive_oplocks().
1232 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1236 for (i=0; i<d->num_share_modes; i++) {
1237 struct share_mode_entry *e = &d->share_modes[i];
1239 if (!is_valid_share_mode_entry(e)) {
1243 if (e->op_type == NO_OPLOCK && is_stat_open(e->access_mask)) {
1244 /* We ignore stat opens in the table - they
1245 always have NO_OPLOCK and never get or
1246 cause breaks. JRA. */
1250 if (BATCH_OPLOCK_TYPE(e->op_type)) {
1251 /* batch - can only be one. */
1252 if (share_mode_stale_pid(d, i)) {
1253 DEBUG(10, ("Found stale batch oplock\n"));
1256 if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) {
1257 smb_panic("Bad batch oplock entry.");
1262 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1263 if (share_mode_stale_pid(d, i)) {
1264 DEBUG(10, ("Found stale duplicate oplock\n"));
1267 /* Exclusive or batch - can only be one. */
1268 if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) {
1269 smb_panic("Bad exclusive or batch oplock entry.");
1271 *pp_ex_or_batch = e;
1274 if (LEVEL_II_OPLOCK_TYPE(e->op_type)) {
1275 if (*pp_batch || *pp_ex_or_batch) {
1276 if (share_mode_stale_pid(d, i)) {
1277 DEBUG(10, ("Found stale LevelII "
1281 smb_panic("Bad levelII oplock entry.");
1286 if (e->op_type == NO_OPLOCK) {
1287 if (*pp_batch || *pp_ex_or_batch) {
1288 if (share_mode_stale_pid(d, i)) {
1289 DEBUG(10, ("Found stale NO_OPLOCK "
1293 smb_panic("Bad no oplock entry.");
1295 *got_no_oplock = true;
1300 static bool delay_for_batch_oplocks(files_struct *fsp,
1303 struct share_mode_entry *batch_entry)
1305 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1308 if (batch_entry == NULL) {
1312 if (server_id_is_disconnected(&batch_entry->pid)) {
1315 * This could be achieved by sending a break message
1316 * to ourselves. Special considerations for files
1317 * with delete_on_close flag set!
1319 * For now we keep it simple and do not
1320 * allow delete on close for durable handles.
1325 /* Found a batch oplock */
1326 send_break_message(fsp, batch_entry, mid, oplock_request);
1330 static bool delay_for_exclusive_oplocks(files_struct *fsp,
1333 struct share_mode_entry *ex_entry)
1335 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
1338 if (ex_entry == NULL) {
1342 if (server_id_is_disconnected(&ex_entry->pid)) {
1344 * since only durable handles can get disconnected,
1345 * and we can only get durable handles with batch oplocks,
1346 * this should actually never be reached...
1351 send_break_message(fsp, ex_entry, mid, oplock_request);
1355 static bool file_has_brlocks(files_struct *fsp)
1357 struct byte_range_lock *br_lck;
1359 br_lck = brl_get_locks_readonly(fsp);
1363 return (brl_num_locks(br_lck) > 0);
1366 static void grant_fsp_oplock_type(files_struct *fsp,
1368 bool got_level2_oplock,
1369 bool got_a_none_oplock)
1371 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
1372 lp_level2_oplocks(SNUM(fsp->conn));
1374 /* Start by granting what the client asked for,
1375 but ensure no SAMBA_PRIVATE bits can be set. */
1376 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1378 if (oplock_request & INTERNAL_OPEN_ONLY) {
1379 /* No oplocks on internal open. */
1380 fsp->oplock_type = NO_OPLOCK;
1381 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1382 fsp->oplock_type, fsp_str_dbg(fsp)));
1386 if (lp_locking(fsp->conn->params) && file_has_brlocks(fsp)) {
1387 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1389 fsp->oplock_type = NO_OPLOCK;
1392 if (is_stat_open(fsp->access_mask)) {
1393 /* Leave the value already set. */
1394 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1395 fsp->oplock_type, fsp_str_dbg(fsp)));
1400 * Match what was requested (fsp->oplock_type) with
1401 * what was found in the existing share modes.
1404 if (got_level2_oplock || got_a_none_oplock) {
1405 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1406 fsp->oplock_type = LEVEL_II_OPLOCK;
1411 * Don't grant level2 to clients that don't want them
1412 * or if we've turned them off.
1414 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
1415 fsp->oplock_type = NO_OPLOCK;
1418 if (fsp->oplock_type == LEVEL_II_OPLOCK && !got_level2_oplock) {
1420 * We're the first level2 oplock. Indicate that in brlock.tdb.
1422 struct byte_range_lock *brl;
1424 brl = brl_get_locks(talloc_tos(), fsp);
1426 brl_set_have_read_oplocks(brl, true);
1431 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1432 fsp->oplock_type, fsp_str_dbg(fsp)));
1435 static bool request_timed_out(struct timeval request_time,
1436 struct timeval timeout)
1438 struct timeval now, end_time;
1440 end_time = timeval_sum(&request_time, &timeout);
1441 return (timeval_compare(&end_time, &now) < 0);
1444 struct defer_open_state {
1445 struct smbd_server_connection *sconn;
1449 static void defer_open_done(struct tevent_req *req);
1451 /****************************************************************************
1452 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1453 ****************************************************************************/
1455 static void defer_open(struct share_mode_lock *lck,
1456 struct timeval request_time,
1457 struct timeval timeout,
1458 struct smb_request *req,
1459 struct deferred_open_record *state)
1461 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1462 "open entry for mid %llu\n",
1463 (unsigned int)request_time.tv_sec,
1464 (unsigned int)request_time.tv_usec,
1465 (unsigned long long)req->mid));
1467 if (!push_deferred_open_message_smb(req, request_time, timeout,
1468 state->id, (char *)state, sizeof(*state))) {
1470 exit_server("push_deferred_open_message_smb failed");
1473 struct defer_open_state *watch_state;
1474 struct tevent_req *watch_req;
1477 watch_state = talloc(req->sconn, struct defer_open_state);
1478 if (watch_state == NULL) {
1479 exit_server("talloc failed");
1481 watch_state->sconn = req->sconn;
1482 watch_state->mid = req->mid;
1484 DEBUG(10, ("defering mid %llu\n",
1485 (unsigned long long)req->mid));
1487 watch_req = dbwrap_record_watch_send(
1488 watch_state, req->sconn->ev_ctx, lck->data->record,
1489 req->sconn->msg_ctx);
1490 if (watch_req == NULL) {
1491 exit_server("Could not watch share mode record");
1493 tevent_req_set_callback(watch_req, defer_open_done,
1496 ret = tevent_req_set_endtime(
1497 watch_req, req->sconn->ev_ctx,
1498 timeval_sum(&request_time, &timeout));
1503 static void defer_open_done(struct tevent_req *req)
1505 struct defer_open_state *state = tevent_req_callback_data(
1506 req, struct defer_open_state);
1510 status = dbwrap_record_watch_recv(req, talloc_tos(), NULL);
1512 if (!NT_STATUS_IS_OK(status)) {
1513 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1514 nt_errstr(status)));
1516 * Even if it failed, retry anyway. TODO: We need a way to
1517 * tell a re-scheduled open about that error.
1521 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state->mid));
1523 ret = schedule_deferred_open_message_smb(state->sconn, state->mid);
1529 /****************************************************************************
1530 On overwrite open ensure that the attributes match.
1531 ****************************************************************************/
1533 static bool open_match_attributes(connection_struct *conn,
1534 uint32 old_dos_attr,
1535 uint32 new_dos_attr,
1536 mode_t existing_unx_mode,
1537 mode_t new_unx_mode,
1538 mode_t *returned_unx_mode)
1540 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1542 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1543 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1545 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1546 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1547 *returned_unx_mode = new_unx_mode;
1549 *returned_unx_mode = (mode_t)0;
1552 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1553 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1554 "returned_unx_mode = 0%o\n",
1555 (unsigned int)old_dos_attr,
1556 (unsigned int)existing_unx_mode,
1557 (unsigned int)new_dos_attr,
1558 (unsigned int)*returned_unx_mode ));
1560 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1561 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1562 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1563 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1567 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1568 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1569 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1576 /****************************************************************************
1577 Special FCB or DOS processing in the case of a sharing violation.
1578 Try and find a duplicated file handle.
1579 ****************************************************************************/
1581 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
1582 connection_struct *conn,
1583 files_struct *fsp_to_dup_into,
1584 const struct smb_filename *smb_fname,
1589 uint32 share_access,
1590 uint32 create_options)
1594 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1595 "file %s.\n", smb_fname_str_dbg(smb_fname)));
1597 for(fsp = file_find_di_first(conn->sconn, id); fsp;
1598 fsp = file_find_di_next(fsp)) {
1600 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1601 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1602 "access_mask = 0x%x\n", fsp_str_dbg(fsp),
1603 fsp->fh->fd, (unsigned long long)fsp->vuid,
1604 (unsigned int)fsp->file_pid,
1605 (unsigned int)fsp->fh->private_options,
1606 (unsigned int)fsp->access_mask ));
1608 if (fsp != fsp_to_dup_into &&
1609 fsp->fh->fd != -1 &&
1610 fsp->vuid == vuid &&
1611 fsp->file_pid == file_pid &&
1612 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1613 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1614 (fsp->access_mask & FILE_WRITE_DATA) &&
1615 strequal(fsp->fsp_name->base_name, smb_fname->base_name) &&
1616 strequal(fsp->fsp_name->stream_name,
1617 smb_fname->stream_name)) {
1618 DEBUG(10,("fcb_or_dos_open: file match\n"));
1624 return NT_STATUS_NOT_FOUND;
1627 /* quite an insane set of semantics ... */
1628 if (is_executable(smb_fname->base_name) &&
1629 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1630 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1631 return NT_STATUS_INVALID_PARAMETER;
1634 /* We need to duplicate this fsp. */
1635 return dup_file_fsp(req, fsp, access_mask, share_access,
1636 create_options, fsp_to_dup_into);
1639 static void schedule_defer_open(struct share_mode_lock *lck,
1640 struct timeval request_time,
1641 struct smb_request *req)
1643 struct deferred_open_record state;
1645 /* This is a relative time, added to the absolute
1646 request_time value to get the absolute timeout time.
1647 Note that if this is the second or greater time we enter
1648 this codepath for this particular request mid then
1649 request_time is left as the absolute time of the *first*
1650 time this request mid was processed. This is what allows
1651 the request to eventually time out. */
1653 struct timeval timeout;
1655 /* Normally the smbd we asked should respond within
1656 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1657 * the client did, give twice the timeout as a safety
1658 * measure here in case the other smbd is stuck
1659 * somewhere else. */
1661 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1663 /* Nothing actually uses state.delayed_for_oplocks
1664 but it's handy to differentiate in debug messages
1665 between a 30 second delay due to oplock break, and
1666 a 1 second delay for share mode conflicts. */
1668 state.delayed_for_oplocks = True;
1669 state.async_open = false;
1670 state.id = lck->data->id;
1672 if (!request_timed_out(request_time, timeout)) {
1673 defer_open(lck, request_time, timeout, req, &state);
1677 /****************************************************************************
1678 Reschedule an open call that went asynchronous.
1679 ****************************************************************************/
1681 static void schedule_async_open(struct timeval request_time,
1682 struct smb_request *req)
1684 struct deferred_open_record state;
1685 struct timeval timeout;
1687 timeout = timeval_set(20, 0);
1690 state.delayed_for_oplocks = false;
1691 state.async_open = true;
1693 if (!request_timed_out(request_time, timeout)) {
1694 defer_open(NULL, request_time, timeout, req, &state);
1698 /****************************************************************************
1699 Work out what access_mask to use from what the client sent us.
1700 ****************************************************************************/
1702 static NTSTATUS smbd_calculate_maximum_allowed_access(
1703 connection_struct *conn,
1704 const struct smb_filename *smb_fname,
1706 uint32_t *p_access_mask)
1708 struct security_descriptor *sd;
1709 uint32_t access_granted;
1712 if (!use_privs && (get_current_uid(conn) == (uid_t)0)) {
1713 *p_access_mask |= FILE_GENERIC_ALL;
1714 return NT_STATUS_OK;
1717 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
1723 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1725 * File did not exist
1727 *p_access_mask = FILE_GENERIC_ALL;
1728 return NT_STATUS_OK;
1730 if (!NT_STATUS_IS_OK(status)) {
1731 DEBUG(10,("Could not get acl on file %s: %s\n",
1732 smb_fname_str_dbg(smb_fname),
1733 nt_errstr(status)));
1734 return NT_STATUS_ACCESS_DENIED;
1738 * If we can access the path to this file, by
1739 * default we have FILE_READ_ATTRIBUTES from the
1740 * containing directory. See the section:
1741 * "Algorithm to Check Access to an Existing File"
1744 * se_file_access_check()
1745 * also takes care of owner WRITE_DAC and READ_CONTROL.
1747 status = se_file_access_check(sd,
1748 get_current_nttok(conn),
1750 (*p_access_mask & ~FILE_READ_ATTRIBUTES),
1755 if (!NT_STATUS_IS_OK(status)) {
1756 DEBUG(10, ("Access denied on file %s: "
1757 "when calculating maximum access\n",
1758 smb_fname_str_dbg(smb_fname)));
1759 return NT_STATUS_ACCESS_DENIED;
1761 *p_access_mask = (access_granted | FILE_READ_ATTRIBUTES);
1763 if (!(access_granted & DELETE_ACCESS)) {
1764 if (can_delete_file_in_directory(conn, smb_fname)) {
1765 *p_access_mask |= DELETE_ACCESS;
1769 return NT_STATUS_OK;
1772 NTSTATUS smbd_calculate_access_mask(connection_struct *conn,
1773 const struct smb_filename *smb_fname,
1775 uint32_t access_mask,
1776 uint32_t *access_mask_out)
1779 uint32_t orig_access_mask = access_mask;
1780 uint32_t rejected_share_access;
1783 * Convert GENERIC bits to specific bits.
1786 se_map_generic(&access_mask, &file_generic_mapping);
1788 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1789 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1791 status = smbd_calculate_maximum_allowed_access(
1792 conn, smb_fname, use_privs, &access_mask);
1794 if (!NT_STATUS_IS_OK(status)) {
1798 access_mask &= conn->share_access;
1801 rejected_share_access = access_mask & ~(conn->share_access);
1803 if (rejected_share_access) {
1804 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1805 "file %s: rejected by share access mask[0x%08X] "
1806 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1807 smb_fname_str_dbg(smb_fname),
1809 orig_access_mask, access_mask,
1810 rejected_share_access));
1811 return NT_STATUS_ACCESS_DENIED;
1814 *access_mask_out = access_mask;
1815 return NT_STATUS_OK;
1818 /****************************************************************************
1819 Remove the deferred open entry under lock.
1820 ****************************************************************************/
1822 /****************************************************************************
1823 Return true if this is a state pointer to an asynchronous create.
1824 ****************************************************************************/
1826 bool is_deferred_open_async(const void *ptr)
1828 const struct deferred_open_record *state = (const struct deferred_open_record *)ptr;
1830 return state->async_open;
1833 static bool clear_ads(uint32_t create_disposition)
1837 switch (create_disposition) {
1838 case FILE_SUPERSEDE:
1839 case FILE_OVERWRITE_IF:
1840 case FILE_OVERWRITE:
1849 static int disposition_to_open_flags(uint32_t create_disposition)
1854 * Currently we're using FILE_SUPERSEDE as the same as
1855 * FILE_OVERWRITE_IF but they really are
1856 * different. FILE_SUPERSEDE deletes an existing file
1857 * (requiring delete access) then recreates it.
1860 switch (create_disposition) {
1861 case FILE_SUPERSEDE:
1862 case FILE_OVERWRITE_IF:
1864 * If file exists replace/overwrite. If file doesn't
1867 ret = O_CREAT|O_TRUNC;
1872 * If file exists open. If file doesn't exist error.
1877 case FILE_OVERWRITE:
1879 * If file exists overwrite. If file doesn't exist
1887 * If file exists error. If file doesn't exist create.
1889 ret = O_CREAT|O_EXCL;
1894 * If file exists open. If file doesn't exist create.
1902 static int calculate_open_access_flags(uint32_t access_mask,
1904 uint32_t private_flags)
1906 bool need_write, need_read;
1909 * Note that we ignore the append flag as append does not
1910 * mean the same thing under DOS and Unix.
1913 need_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA));
1918 /* DENY_DOS opens are always underlying read-write on the
1919 file handle, no matter what the requested access mask
1923 ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1924 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|
1925 FILE_READ_EA|FILE_EXECUTE));
1933 /****************************************************************************
1934 Open a file with a share mode. Passed in an already created files_struct *.
1935 ****************************************************************************/
1937 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1938 struct smb_request *req,
1939 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1940 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1941 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1942 uint32 create_options, /* options such as delete on close. */
1943 uint32 new_dos_attributes, /* attributes used for new file. */
1944 int oplock_request, /* internal Samba oplock codes. */
1945 /* Information (FILE_EXISTS etc.) */
1946 uint32_t private_flags, /* Samba specific flags. */
1950 struct smb_filename *smb_fname = fsp->fsp_name;
1953 bool file_existed = VALID_STAT(smb_fname->st);
1954 bool def_acl = False;
1955 bool posix_open = False;
1956 bool new_file_created = False;
1957 bool first_open_attempt = true;
1958 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1959 mode_t new_unx_mode = (mode_t)0;
1960 mode_t unx_mode = (mode_t)0;
1962 uint32 existing_dos_attributes = 0;
1963 struct timeval request_time = timeval_zero();
1964 struct share_mode_lock *lck = NULL;
1965 uint32 open_access_mask = access_mask;
1968 SMB_STRUCT_STAT saved_stat = smb_fname->st;
1969 struct share_mode_entry *batch_entry = NULL;
1970 struct share_mode_entry *exclusive_entry = NULL;
1971 bool got_level2_oplock = false;
1972 bool got_a_none_oplock = false;
1973 struct timespec old_write_time;
1976 if (conn->printer) {
1978 * Printers are handled completely differently.
1979 * Most of the passed parameters are ignored.
1983 *pinfo = FILE_WAS_CREATED;
1986 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1987 smb_fname_str_dbg(smb_fname)));
1990 DEBUG(0,("open_file_ntcreate: printer open without "
1991 "an SMB request!\n"));
1992 return NT_STATUS_INTERNAL_ERROR;
1995 return print_spool_open(fsp, smb_fname->base_name,
1999 if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
2001 return NT_STATUS_NO_MEMORY;
2004 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2006 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2007 new_dos_attributes = 0;
2009 /* Windows allows a new file to be created and
2010 silently removes a FILE_ATTRIBUTE_DIRECTORY
2011 sent by the client. Do the same. */
2013 new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
2015 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2017 unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2018 smb_fname, parent_dir);
2021 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2022 "access_mask=0x%x share_access=0x%x "
2023 "create_disposition = 0x%x create_options=0x%x "
2024 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2025 smb_fname_str_dbg(smb_fname), new_dos_attributes,
2026 access_mask, share_access, create_disposition,
2027 create_options, (unsigned int)unx_mode, oplock_request,
2028 (unsigned int)private_flags));
2030 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
2031 DEBUG(0, ("No smb request but not an internal only open!\n"));
2032 return NT_STATUS_INTERNAL_ERROR;
2036 * Only non-internal opens can be deferred at all
2041 if (get_deferred_open_message_state(req,
2044 /* Remember the absolute time of the original
2045 request with this mid. We'll use it later to
2046 see if this has timed out. */
2048 /* If it was an async create retry, the file
2051 if (is_deferred_open_async(ptr)) {
2052 SET_STAT_INVALID(smb_fname->st);
2053 file_existed = false;
2056 /* Ensure we don't reprocess this message. */
2057 remove_deferred_open_message_smb(req->sconn, req->mid);
2059 first_open_attempt = false;
2064 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
2066 existing_dos_attributes = dos_mode(conn, smb_fname);
2070 /* ignore any oplock requests if oplocks are disabled */
2071 if (!lp_oplocks(SNUM(conn)) ||
2072 IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
2073 /* Mask off everything except the private Samba bits. */
2074 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
2077 /* this is for OS/2 long file names - say we don't support them */
2078 if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
2079 /* OS/2 Workplace shell fix may be main code stream in a later
2081 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2083 if (use_nt_status()) {
2084 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2086 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
2089 switch( create_disposition ) {
2091 /* If file exists open. If file doesn't exist error. */
2092 if (!file_existed) {
2093 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2094 "requested for file %s and file "
2096 smb_fname_str_dbg(smb_fname)));
2098 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2102 case FILE_OVERWRITE:
2103 /* If file exists overwrite. If file doesn't exist
2105 if (!file_existed) {
2106 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2107 "requested for file %s and file "
2109 smb_fname_str_dbg(smb_fname) ));
2111 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2116 /* If file exists error. If file doesn't exist
2119 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2120 "requested for file %s and file "
2121 "already exists.\n",
2122 smb_fname_str_dbg(smb_fname)));
2123 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
2128 return map_nt_error_from_unix(errno);
2132 case FILE_SUPERSEDE:
2133 case FILE_OVERWRITE_IF:
2137 return NT_STATUS_INVALID_PARAMETER;
2140 flags2 = disposition_to_open_flags(create_disposition);
2142 /* We only care about matching attributes on file exists and
2145 if (!posix_open && file_existed &&
2146 ((create_disposition == FILE_OVERWRITE) ||
2147 (create_disposition == FILE_OVERWRITE_IF))) {
2148 if (!open_match_attributes(conn, existing_dos_attributes,
2150 smb_fname->st.st_ex_mode,
2151 unx_mode, &new_unx_mode)) {
2152 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2153 "for file %s (%x %x) (0%o, 0%o)\n",
2154 smb_fname_str_dbg(smb_fname),
2155 existing_dos_attributes,
2157 (unsigned int)smb_fname->st.st_ex_mode,
2158 (unsigned int)unx_mode ));
2160 return NT_STATUS_ACCESS_DENIED;
2164 status = smbd_calculate_access_mask(conn, smb_fname,
2168 if (!NT_STATUS_IS_OK(status)) {
2169 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2170 "on file %s returned %s\n",
2171 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
2175 open_access_mask = access_mask;
2177 if (flags2 & O_TRUNC) {
2178 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
2181 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2182 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname),
2186 * Note that we ignore the append flag as append does not
2187 * mean the same thing under DOS and Unix.
2190 flags = calculate_open_access_flags(access_mask, oplock_request,
2194 * Currently we only look at FILE_WRITE_THROUGH for create options.
2198 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
2203 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
2207 if (!posix_open && !CAN_WRITE(conn)) {
2209 * We should really return a permission denied error if either
2210 * O_CREAT or O_TRUNC are set, but for compatibility with
2211 * older versions of Samba we just AND them out.
2213 flags2 &= ~(O_CREAT|O_TRUNC);
2216 if (first_open_attempt && lp_kernel_oplocks(SNUM(conn))) {
2218 * With kernel oplocks the open breaking an oplock
2219 * blocks until the oplock holder has given up the
2220 * oplock or closed the file. We prevent this by first
2221 * trying to open the file with O_NONBLOCK (see "man
2222 * fcntl" on Linux). For the second try, triggered by
2223 * an oplock break response, we do not need this
2226 * This is true under the assumption that only Samba
2227 * requests kernel oplocks. Once someone else like
2228 * NFSv4 starts to use that API, we will have to
2229 * modify this by communicating with the NFSv4 server.
2231 flags2 |= O_NONBLOCK;
2235 * Ensure we can't write on a read-only share or file.
2238 if (flags != O_RDONLY && file_existed &&
2239 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
2240 DEBUG(5,("open_file_ntcreate: write access requested for "
2241 "file %s on read only %s\n",
2242 smb_fname_str_dbg(smb_fname),
2243 !CAN_WRITE(conn) ? "share" : "file" ));
2245 return NT_STATUS_ACCESS_DENIED;
2248 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
2249 fsp->share_access = share_access;
2250 fsp->fh->private_options = private_flags;
2251 fsp->access_mask = open_access_mask; /* We change this to the
2252 * requested access_mask after
2253 * the open is done. */
2254 fsp->posix_open = posix_open;
2256 /* Ensure no SAMBA_PRIVATE bits can be set. */
2257 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
2259 if (timeval_is_zero(&request_time)) {
2260 request_time = fsp->open_time;
2264 * Ensure we pay attention to default ACLs on directories if required.
2267 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
2268 (def_acl = directory_has_default_acl(conn, parent_dir))) {
2269 unx_mode = (0777 & lp_create_mask(SNUM(conn)));
2272 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2273 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2274 (unsigned int)flags, (unsigned int)flags2,
2275 (unsigned int)unx_mode, (unsigned int)access_mask,
2276 (unsigned int)open_access_mask));
2278 fsp_open = open_file(fsp, conn, req, parent_dir,
2279 flags|flags2, unx_mode, access_mask,
2280 open_access_mask, &new_file_created);
2282 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_NETWORK_BUSY)) {
2283 struct deferred_open_record state;
2286 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2288 if (file_existed && S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
2289 DEBUG(10, ("FIFO busy\n"));
2290 return NT_STATUS_NETWORK_BUSY;
2293 DEBUG(10, ("Internal open busy\n"));
2294 return NT_STATUS_NETWORK_BUSY;
2298 * From here on we assume this is an oplock break triggered
2301 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
2303 state.delayed_for_oplocks = false;
2304 state.async_open = false;
2305 state.id = fsp->file_id;
2306 defer_open(NULL, request_time, timeval_set(0, 0),
2308 DEBUG(10, ("No share mode lock found after "
2309 "EWOULDBLOCK, retrying sync\n"));
2310 return NT_STATUS_SHARING_VIOLATION;
2313 find_oplock_types(fsp, 0, lck, &batch_entry, &exclusive_entry,
2314 &got_level2_oplock, &got_a_none_oplock);
2316 if (delay_for_batch_oplocks(fsp, req->mid, 0, batch_entry) ||
2317 delay_for_exclusive_oplocks(fsp, req->mid, 0,
2319 schedule_defer_open(lck, request_time, req);
2321 DEBUG(10, ("Sent oplock break request to kernel "
2322 "oplock holder\n"));
2323 return NT_STATUS_SHARING_VIOLATION;
2327 * No oplock from Samba around. Immediately retry with
2330 state.delayed_for_oplocks = false;
2331 state.async_open = false;
2332 state.id = lck->data->id;
2333 defer_open(lck, request_time, timeval_set(0, 0), req, &state);
2335 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2336 "Retrying sync\n"));
2337 return NT_STATUS_SHARING_VIOLATION;
2340 if (!NT_STATUS_IS_OK(fsp_open)) {
2341 if (NT_STATUS_EQUAL(fsp_open, NT_STATUS_RETRY)) {
2342 schedule_async_open(request_time, req);
2347 if (file_existed && !check_same_dev_ino(&saved_stat, &smb_fname->st)) {
2349 * The file did exist, but some other (local or NFS)
2350 * process either renamed/unlinked and re-created the
2351 * file with different dev/ino after we walked the path,
2352 * but before we did the open. We could retry the
2353 * open but it's a rare enough case it's easier to
2354 * just fail the open to prevent creating any problems
2355 * in the open file db having the wrong dev/ino key.
2358 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2359 "Old (dev=0x%llu, ino =0x%llu). "
2360 "New (dev=0x%llu, ino=0x%llu). Failing open "
2361 " with NT_STATUS_ACCESS_DENIED.\n",
2362 smb_fname_str_dbg(smb_fname),
2363 (unsigned long long)saved_stat.st_ex_dev,
2364 (unsigned long long)saved_stat.st_ex_ino,
2365 (unsigned long long)smb_fname->st.st_ex_dev,
2366 (unsigned long long)smb_fname->st.st_ex_ino));
2367 return NT_STATUS_ACCESS_DENIED;
2370 old_write_time = smb_fname->st.st_ex_mtime;
2373 * Deal with the race condition where two smbd's detect the
2374 * file doesn't exist and do the create at the same time. One
2375 * of them will win and set a share mode, the other (ie. this
2376 * one) should check if the requested share mode for this
2377 * create is allowed.
2381 * Now the file exists and fsp is successfully opened,
2382 * fsp->dev and fsp->inode are valid and should replace the
2383 * dev=0,inode=0 from a non existent file. Spotted by
2384 * Nadav Danieli <nadavd@exanet.com>. JRA.
2389 lck = get_share_mode_lock(talloc_tos(), id,
2391 smb_fname, &old_write_time);
2394 DEBUG(0, ("open_file_ntcreate: Could not get share "
2395 "mode lock for %s\n",
2396 smb_fname_str_dbg(smb_fname)));
2398 return NT_STATUS_SHARING_VIOLATION;
2401 /* Get the types we need to examine. */
2402 find_oplock_types(fsp,
2408 &got_a_none_oplock);
2410 if (has_delete_on_close(lck, fsp->name_hash)) {
2413 return NT_STATUS_DELETE_PENDING;
2416 /* First pass - send break only on batch oplocks. */
2417 if ((req != NULL) &&
2418 delay_for_batch_oplocks(fsp,
2422 schedule_defer_open(lck, request_time, req);
2425 return NT_STATUS_SHARING_VIOLATION;
2428 status = open_mode_check(conn, lck,
2429 access_mask, share_access,
2432 if (NT_STATUS_IS_OK(status)) {
2433 /* We might be going to allow this open. Check oplock
2435 /* Second pass - send break for both batch or
2436 * exclusive oplocks. */
2437 if ((req != NULL) &&
2438 delay_for_exclusive_oplocks(
2443 schedule_defer_open(lck, request_time, req);
2446 return NT_STATUS_SHARING_VIOLATION;
2450 if (!NT_STATUS_IS_OK(status)) {
2451 uint32 can_access_mask;
2452 bool can_access = True;
2454 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
2456 /* Check if this can be done with the deny_dos and fcb
2459 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
2460 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
2462 DEBUG(0, ("DOS open without an SMB "
2466 return NT_STATUS_INTERNAL_ERROR;
2469 /* Use the client requested access mask here,
2470 * not the one we open with. */
2471 status = fcb_or_dos_open(req,
2482 if (NT_STATUS_IS_OK(status)) {
2485 *pinfo = FILE_WAS_OPENED;
2487 return NT_STATUS_OK;
2492 * This next line is a subtlety we need for
2493 * MS-Access. If a file open will fail due to share
2494 * permissions and also for security (access) reasons,
2495 * we need to return the access failed error, not the
2496 * share error. We can't open the file due to kernel
2497 * oplock deadlock (it's possible we failed above on
2498 * the open_mode_check()) so use a userspace check.
2501 if (flags & O_RDWR) {
2502 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
2503 } else if (flags & O_WRONLY) {
2504 can_access_mask = FILE_WRITE_DATA;
2506 can_access_mask = FILE_READ_DATA;
2509 if (((can_access_mask & FILE_WRITE_DATA) &&
2510 !CAN_WRITE(conn)) ||
2511 !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
2514 can_access_mask))) {
2519 * If we're returning a share violation, ensure we
2520 * cope with the braindead 1 second delay (SMB1 only).
2523 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
2524 !conn->sconn->using_smb2 &&
2525 lp_defer_sharing_violations()) {
2526 struct timeval timeout;
2527 struct deferred_open_record state;
2530 /* this is a hack to speed up torture tests
2532 timeout_usecs = lp_parm_int(SNUM(conn),
2533 "smbd","sharedelay",
2534 SHARING_VIOLATION_USEC_WAIT);
2536 /* This is a relative time, added to the absolute
2537 request_time value to get the absolute timeout time.
2538 Note that if this is the second or greater time we enter
2539 this codepath for this particular request mid then
2540 request_time is left as the absolute time of the *first*
2541 time this request mid was processed. This is what allows
2542 the request to eventually time out. */
2544 timeout = timeval_set(0, timeout_usecs);
2546 /* Nothing actually uses state.delayed_for_oplocks
2547 but it's handy to differentiate in debug messages
2548 between a 30 second delay due to oplock break, and
2549 a 1 second delay for share mode conflicts. */
2551 state.delayed_for_oplocks = False;
2552 state.async_open = false;
2556 && !request_timed_out(request_time,
2558 defer_open(lck, request_time, timeout,
2567 * We have detected a sharing violation here
2568 * so return the correct error code
2570 status = NT_STATUS_SHARING_VIOLATION;
2572 status = NT_STATUS_ACCESS_DENIED;
2577 grant_fsp_oplock_type(fsp,
2583 * We have the share entry *locked*.....
2586 /* Delete streams if create_disposition requires it */
2587 if (!new_file_created && clear_ads(create_disposition) &&
2588 !is_ntfs_stream_smb_fname(smb_fname)) {
2589 status = delete_all_streams(conn, smb_fname->base_name);
2590 if (!NT_STATUS_IS_OK(status)) {
2597 /* note that we ignore failure for the following. It is
2598 basically a hack for NFS, and NFS will never set one of
2599 these only read them. Nobody but Samba can ever set a deny
2600 mode and we have already checked our more authoritative
2601 locking database for permission to set this deny mode. If
2602 the kernel refuses the operations then the kernel is wrong.
2603 note that GPFS supports it as well - jmcd */
2605 if (fsp->fh->fd != -1 && lp_kernel_share_modes(SNUM(conn))) {
2607 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
2608 if(ret_flock == -1 ){
2613 return NT_STATUS_SHARING_VIOLATION;
2618 * At this point onwards, we can guarantee that the share entry
2619 * is locked, whether we created the file or not, and that the
2620 * deny mode is compatible with all current opens.
2624 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2625 * but we don't have to store this - just ignore it on access check.
2627 if (conn->sconn->using_smb2) {
2629 * SMB2 doesn't return it (according to Microsoft tests).
2630 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2631 * File created with access = 0x7 (Read, Write, Delete)
2632 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2634 fsp->access_mask = access_mask;
2636 /* But SMB1 does. */
2637 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2641 /* stat opens on existing files don't get oplocks. */
2642 if (is_stat_open(open_access_mask)) {
2643 fsp->oplock_type = NO_OPLOCK;
2647 if (new_file_created) {
2648 info = FILE_WAS_CREATED;
2650 if (flags2 & O_TRUNC) {
2651 info = FILE_WAS_OVERWRITTEN;
2653 info = FILE_WAS_OPENED;
2662 * Setup the oplock info in both the shared memory and
2666 status = set_file_oplock(fsp, fsp->oplock_type);
2667 if (!NT_STATUS_IS_OK(status)) {
2669 * Could not get the kernel oplock
2671 fsp->oplock_type = NO_OPLOCK;
2674 if (!set_share_mode(lck, fsp, get_current_uid(conn),
2676 fsp->oplock_type)) {
2679 return NT_STATUS_NO_MEMORY;
2682 /* Handle strange delete on close create semantics. */
2683 if (create_options & FILE_DELETE_ON_CLOSE) {
2685 status = can_set_delete_on_close(fsp, new_dos_attributes);
2687 if (!NT_STATUS_IS_OK(status)) {
2688 /* Remember to delete the mode we just added. */
2689 del_share_mode(lck, fsp);
2694 /* Note that here we set the *inital* delete on close flag,
2695 not the regular one. The magic gets handled in close. */
2696 fsp->initial_delete_on_close = True;
2699 if (info != FILE_WAS_OPENED) {
2700 /* Files should be initially set as archive */
2701 if (lp_map_archive(SNUM(conn)) ||
2702 lp_store_dos_attributes(SNUM(conn))) {
2704 if (file_set_dosmode(conn, smb_fname,
2705 new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
2706 parent_dir, true) == 0) {
2707 unx_mode = smb_fname->st.st_ex_mode;
2713 /* Determine sparse flag. */
2715 /* POSIX opens are sparse by default. */
2716 fsp->is_sparse = true;
2718 fsp->is_sparse = (file_existed &&
2719 (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE));
2723 * Take care of inherited ACLs on created files - if default ACL not
2727 if (!posix_open && new_file_created && !def_acl) {
2729 int saved_errno = errno; /* We might get ENOSYS in the next
2732 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2734 errno = saved_errno; /* Ignore ENOSYS */
2737 } else if (new_unx_mode) {
2741 /* Attributes need changing. File already existed. */
2744 int saved_errno = errno; /* We might get ENOSYS in the
2746 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2748 if (ret == -1 && errno == ENOSYS) {
2749 errno = saved_errno; /* Ignore ENOSYS */
2751 DEBUG(5, ("open_file_ntcreate: reset "
2752 "attributes of file %s to 0%o\n",
2753 smb_fname_str_dbg(smb_fname),
2754 (unsigned int)new_unx_mode));
2755 ret = 0; /* Don't do the fchmod below. */
2760 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2761 DEBUG(5, ("open_file_ntcreate: failed to reset "
2762 "attributes of file %s to 0%o\n",
2763 smb_fname_str_dbg(smb_fname),
2764 (unsigned int)new_unx_mode));
2769 return NT_STATUS_OK;
2773 /****************************************************************************
2774 Open a file for for write to ensure that we can fchmod it.
2775 ****************************************************************************/
2777 NTSTATUS open_file_fchmod(connection_struct *conn,
2778 struct smb_filename *smb_fname,
2779 files_struct **result)
2781 if (!VALID_STAT(smb_fname->st)) {
2782 return NT_STATUS_INVALID_PARAMETER;
2785 return SMB_VFS_CREATE_FILE(
2788 0, /* root_dir_fid */
2789 smb_fname, /* fname */
2790 FILE_WRITE_DATA, /* access_mask */
2791 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2793 FILE_OPEN, /* create_disposition*/
2794 0, /* create_options */
2795 0, /* file_attributes */
2796 INTERNAL_OPEN_ONLY, /* oplock_request */
2797 0, /* allocation_size */
2798 0, /* private_flags */
2801 result, /* result */
2805 static NTSTATUS mkdir_internal(connection_struct *conn,
2806 struct smb_filename *smb_dname,
2807 uint32 file_attributes)
2810 char *parent_dir = NULL;
2812 bool posix_open = false;
2813 bool need_re_stat = false;
2814 uint32_t access_mask = SEC_DIR_ADD_SUBDIR;
2816 if (!CAN_WRITE(conn) || (access_mask & ~(conn->share_access))) {
2817 DEBUG(5,("mkdir_internal: failing share access "
2818 "%s\n", lp_servicename(talloc_tos(), SNUM(conn))));
2819 return NT_STATUS_ACCESS_DENIED;
2822 if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
2824 return NT_STATUS_NO_MEMORY;
2827 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2829 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2831 mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
2834 status = check_parent_access(conn,
2837 if(!NT_STATUS_IS_OK(status)) {
2838 DEBUG(5,("mkdir_internal: check_parent_access "
2839 "on directory %s for path %s returned %s\n",
2841 smb_dname->base_name,
2842 nt_errstr(status) ));
2846 if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) {
2847 return map_nt_error_from_unix(errno);
2850 /* Ensure we're checking for a symlink here.... */
2851 /* We don't want to get caught by a symlink racer. */
2853 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2854 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2855 smb_fname_str_dbg(smb_dname), strerror(errno)));
2856 return map_nt_error_from_unix(errno);
2859 if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
2860 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2861 smb_fname_str_dbg(smb_dname)));
2862 return NT_STATUS_NOT_A_DIRECTORY;
2865 if (lp_store_dos_attributes(SNUM(conn))) {
2867 file_set_dosmode(conn, smb_dname,
2868 file_attributes | FILE_ATTRIBUTE_DIRECTORY,
2873 if (lp_inherit_perms(SNUM(conn))) {
2874 inherit_access_posix_acl(conn, parent_dir,
2875 smb_dname->base_name, mode);
2876 need_re_stat = true;
2881 * Check if high bits should have been set,
2882 * then (if bits are missing): add them.
2883 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2886 if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) &&
2887 (mode & ~smb_dname->st.st_ex_mode)) {
2888 SMB_VFS_CHMOD(conn, smb_dname->base_name,
2889 (smb_dname->st.st_ex_mode |
2890 (mode & ~smb_dname->st.st_ex_mode)));
2891 need_re_stat = true;
2895 /* Change the owner if required. */
2896 if (lp_inherit_owner(SNUM(conn))) {
2897 change_dir_owner_to_parent(conn, parent_dir,
2898 smb_dname->base_name,
2900 need_re_stat = true;
2904 if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
2905 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2906 smb_fname_str_dbg(smb_dname), strerror(errno)));
2907 return map_nt_error_from_unix(errno);
2911 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2912 smb_dname->base_name);
2914 return NT_STATUS_OK;
2917 /****************************************************************************
2918 Open a directory from an NT SMB call.
2919 ****************************************************************************/
2921 static NTSTATUS open_directory(connection_struct *conn,
2922 struct smb_request *req,
2923 struct smb_filename *smb_dname,
2925 uint32 share_access,
2926 uint32 create_disposition,
2927 uint32 create_options,
2928 uint32 file_attributes,
2930 files_struct **result)
2932 files_struct *fsp = NULL;
2933 bool dir_existed = VALID_STAT(smb_dname->st) ? True : False;
2934 struct share_mode_lock *lck = NULL;
2936 struct timespec mtimespec;
2939 if (is_ntfs_stream_smb_fname(smb_dname)) {
2940 DEBUG(2, ("open_directory: %s is a stream name!\n",
2941 smb_fname_str_dbg(smb_dname)));
2942 return NT_STATUS_NOT_A_DIRECTORY;
2945 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2946 /* Ensure we have a directory attribute. */
2947 file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
2950 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2951 "share_access = 0x%x create_options = 0x%x, "
2952 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2953 smb_fname_str_dbg(smb_dname),
2954 (unsigned int)access_mask,
2955 (unsigned int)share_access,
2956 (unsigned int)create_options,
2957 (unsigned int)create_disposition,
2958 (unsigned int)file_attributes));
2960 status = smbd_calculate_access_mask(conn, smb_dname, false,
2961 access_mask, &access_mask);
2962 if (!NT_STATUS_IS_OK(status)) {
2963 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2964 "on file %s returned %s\n",
2965 smb_fname_str_dbg(smb_dname),
2966 nt_errstr(status)));
2970 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
2971 !security_token_has_privilege(get_current_nttok(conn),
2972 SEC_PRIV_SECURITY)) {
2973 DEBUG(10, ("open_directory: open on %s "
2974 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2975 smb_fname_str_dbg(smb_dname)));
2976 return NT_STATUS_PRIVILEGE_NOT_HELD;
2979 switch( create_disposition ) {
2983 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
2986 info = FILE_WAS_OPENED;
2991 /* If directory exists error. If directory doesn't
2995 status = NT_STATUS_OBJECT_NAME_COLLISION;
2996 DEBUG(2, ("open_directory: unable to create "
2997 "%s. Error was %s\n",
2998 smb_fname_str_dbg(smb_dname),
2999 nt_errstr(status)));
3003 status = mkdir_internal(conn, smb_dname,
3006 if (!NT_STATUS_IS_OK(status)) {
3007 DEBUG(2, ("open_directory: unable to create "
3008 "%s. Error was %s\n",
3009 smb_fname_str_dbg(smb_dname),
3010 nt_errstr(status)));
3014 info = FILE_WAS_CREATED;
3019 * If directory exists open. If directory doesn't
3024 status = NT_STATUS_OK;
3025 info = FILE_WAS_OPENED;
3027 status = mkdir_internal(conn, smb_dname,
3030 if (NT_STATUS_IS_OK(status)) {
3031 info = FILE_WAS_CREATED;
3033 /* Cope with create race. */
3034 if (!NT_STATUS_EQUAL(status,
3035 NT_STATUS_OBJECT_NAME_COLLISION)) {
3036 DEBUG(2, ("open_directory: unable to create "
3037 "%s. Error was %s\n",
3038 smb_fname_str_dbg(smb_dname),
3039 nt_errstr(status)));
3042 info = FILE_WAS_OPENED;
3048 case FILE_SUPERSEDE:
3049 case FILE_OVERWRITE:
3050 case FILE_OVERWRITE_IF:
3052 DEBUG(5,("open_directory: invalid create_disposition "
3053 "0x%x for directory %s\n",
3054 (unsigned int)create_disposition,
3055 smb_fname_str_dbg(smb_dname)));
3056 return NT_STATUS_INVALID_PARAMETER;
3059 if(!S_ISDIR(smb_dname->st.st_ex_mode)) {
3060 DEBUG(5,("open_directory: %s is not a directory !\n",
3061 smb_fname_str_dbg(smb_dname)));
3062 return NT_STATUS_NOT_A_DIRECTORY;
3065 if (info == FILE_WAS_OPENED) {
3066 status = smbd_check_access_rights(conn,
3070 if (!NT_STATUS_IS_OK(status)) {
3071 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3072 "file %s failed with %s\n",
3073 smb_fname_str_dbg(smb_dname),
3074 nt_errstr(status)));
3079 status = file_new(req, conn, &fsp);
3080 if(!NT_STATUS_IS_OK(status)) {
3085 * Setup the files_struct for it.
3088 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
3089 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
3090 fsp->file_pid = req ? req->smbpid : 0;
3091 fsp->can_lock = False;
3092 fsp->can_read = False;
3093 fsp->can_write = False;
3095 fsp->share_access = share_access;
3096 fsp->fh->private_options = 0;
3098 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3100 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
3101 fsp->print_file = NULL;
3102 fsp->modified = False;
3103 fsp->oplock_type = NO_OPLOCK;
3104 fsp->sent_oplock_break = NO_BREAK_SENT;
3105 fsp->is_directory = True;
3106 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
3107 status = fsp_set_smb_fname(fsp, smb_dname);
3108 if (!NT_STATUS_IS_OK(status)) {
3109 file_free(req, fsp);
3113 mtimespec = smb_dname->st.st_ex_mtime;
3116 status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0);
3118 /* POSIX allows us to open a directory with O_RDONLY. */
3119 status = fd_open(conn, fsp, O_RDONLY, 0);
3121 if (!NT_STATUS_IS_OK(status)) {
3122 DEBUG(5, ("open_directory: Could not open fd for "
3124 smb_fname_str_dbg(smb_dname),
3125 nt_errstr(status)));
3126 file_free(req, fsp);
3130 status = vfs_stat_fsp(fsp);
3131 if (!NT_STATUS_IS_OK(status)) {
3133 file_free(req, fsp);
3137 /* Ensure there was no race condition. */
3138 if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) {
3139 DEBUG(5,("open_directory: stat struct differs for "
3141 smb_fname_str_dbg(smb_dname)));
3143 file_free(req, fsp);
3144 return NT_STATUS_ACCESS_DENIED;
3147 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
3148 conn->connectpath, smb_dname,
3152 DEBUG(0, ("open_directory: Could not get share mode lock for "
3153 "%s\n", smb_fname_str_dbg(smb_dname)));
3155 file_free(req, fsp);
3156 return NT_STATUS_SHARING_VIOLATION;
3159 if (has_delete_on_close(lck, fsp->name_hash)) {
3162 file_free(req, fsp);
3163 return NT_STATUS_DELETE_PENDING;
3166 status = open_mode_check(conn, lck,
3167 access_mask, share_access,
3170 if (!NT_STATUS_IS_OK(status)) {
3173 file_free(req, fsp);
3177 if (!set_share_mode(lck, fsp, get_current_uid(conn),
3178 req ? req->mid : 0, NO_OPLOCK)) {
3181 file_free(req, fsp);
3182 return NT_STATUS_NO_MEMORY;
3185 /* For directories the delete on close bit at open time seems
3186 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3187 if (create_options & FILE_DELETE_ON_CLOSE) {
3188 status = can_set_delete_on_close(fsp, 0);
3189 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
3190 del_share_mode(lck, fsp);
3193 file_free(req, fsp);
3197 if (NT_STATUS_IS_OK(status)) {
3198 /* Note that here we set the *inital* delete on close flag,
3199 not the regular one. The magic gets handled in close. */
3200 fsp->initial_delete_on_close = True;
3211 return NT_STATUS_OK;
3214 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req,
3215 struct smb_filename *smb_dname)
3220 status = SMB_VFS_CREATE_FILE(
3223 0, /* root_dir_fid */
3224 smb_dname, /* fname */
3225 FILE_READ_ATTRIBUTES, /* access_mask */
3226 FILE_SHARE_NONE, /* share_access */
3227 FILE_CREATE, /* create_disposition*/
3228 FILE_DIRECTORY_FILE, /* create_options */
3229 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
3230 0, /* oplock_request */
3231 0, /* allocation_size */
3232 0, /* private_flags */
3238 if (NT_STATUS_IS_OK(status)) {
3239 close_file(req, fsp, NORMAL_CLOSE);
3245 /****************************************************************************
3246 Receive notification that one of our open files has been renamed by another
3248 ****************************************************************************/
3250 void msg_file_was_renamed(struct messaging_context *msg,
3253 struct server_id server_id,
3257 char *frm = (char *)data->data;
3259 const char *sharepath;
3260 const char *base_name;
3261 const char *stream_name;
3262 struct smb_filename *smb_fname = NULL;
3263 size_t sp_len, bn_len;
3265 struct smbd_server_connection *sconn =
3266 talloc_get_type_abort(private_data,
3267 struct smbd_server_connection);
3269 if (data->data == NULL
3270 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
3271 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3272 (int)data->length));
3276 /* Unpack the message. */
3277 pull_file_id_24(frm, &id);
3278 sharepath = &frm[24];
3279 sp_len = strlen(sharepath);
3280 base_name = sharepath + sp_len + 1;
3281 bn_len = strlen(base_name);
3282 stream_name = sharepath + sp_len + 1 + bn_len + 1;
3284 /* stream_name must always be NULL if there is no stream. */
3285 if (stream_name[0] == '\0') {
3289 smb_fname = synthetic_smb_fname(talloc_tos(), base_name,
3291 if (smb_fname == NULL) {
3295 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3297 sharepath, smb_fname_str_dbg(smb_fname),
3298 file_id_string_tos(&id)));
3300 for(fsp = file_find_di_first(sconn, id); fsp;
3301 fsp = file_find_di_next(fsp)) {
3302 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
3304 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3305 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp),
3306 smb_fname_str_dbg(smb_fname)));
3307 status = fsp_set_smb_fname(fsp, smb_fname);
3308 if (!NT_STATUS_IS_OK(status)) {
3313 /* Now we have the complete path we can work out if this is
3314 actually within this share and adjust newname accordingly. */
3315 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3316 "not sharepath %s) "
3317 "%s from %s -> %s\n",
3318 fsp->conn->connectpath,
3322 smb_fname_str_dbg(smb_fname)));
3326 TALLOC_FREE(smb_fname);
3331 * If a main file is opened for delete, all streams need to be checked for
3332 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3333 * If that works, delete them all by setting the delete on close and close.
3336 NTSTATUS open_streams_for_delete(connection_struct *conn,
3339 struct stream_struct *stream_info = NULL;
3340 files_struct **streams = NULL;
3342 unsigned int num_streams = 0;
3343 TALLOC_CTX *frame = talloc_stackframe();
3346 status = vfs_streaminfo(conn, NULL, fname, talloc_tos(),
3347 &num_streams, &stream_info);
3349 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
3350 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3351 DEBUG(10, ("no streams around\n"));
3353 return NT_STATUS_OK;
3356 if (!NT_STATUS_IS_OK(status)) {
3357 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3358 nt_errstr(status)));
3362 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3365 if (num_streams == 0) {
3367 return NT_STATUS_OK;
3370 streams = talloc_array(talloc_tos(), files_struct *, num_streams);
3371 if (streams == NULL) {
3372 DEBUG(0, ("talloc failed\n"));
3373 status = NT_STATUS_NO_MEMORY;
3377 for (i=0; i<num_streams; i++) {
3378 struct smb_filename *smb_fname;
3380 if (strequal(stream_info[i].name, "::$DATA")) {
3385 smb_fname = synthetic_smb_fname(
3386 talloc_tos(), fname, stream_info[i].name, NULL);
3387 if (smb_fname == NULL) {
3388 status = NT_STATUS_NO_MEMORY;
3392 if (SMB_VFS_STAT(conn, smb_fname) == -1) {
3393 DEBUG(10, ("Unable to stat stream: %s\n",
3394 smb_fname_str_dbg(smb_fname)));
3397 status = SMB_VFS_CREATE_FILE(
3400 0, /* root_dir_fid */
3401 smb_fname, /* fname */
3402 DELETE_ACCESS, /* access_mask */
3403 (FILE_SHARE_READ | /* share_access */
3404 FILE_SHARE_WRITE | FILE_SHARE_DELETE),
3405 FILE_OPEN, /* create_disposition*/
3406 0, /* create_options */
3407 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
3408 0, /* oplock_request */
3409 0, /* allocation_size */
3410 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */
3413 &streams[i], /* result */
3416 if (!NT_STATUS_IS_OK(status)) {
3417 DEBUG(10, ("Could not open stream %s: %s\n",
3418 smb_fname_str_dbg(smb_fname),
3419 nt_errstr(status)));
3421 TALLOC_FREE(smb_fname);
3424 TALLOC_FREE(smb_fname);
3428 * don't touch the variable "status" beyond this point :-)
3431 for (i -= 1 ; i >= 0; i--) {
3432 if (streams[i] == NULL) {
3436 DEBUG(10, ("Closing stream # %d, %s\n", i,
3437 fsp_str_dbg(streams[i])));
3438 close_file(NULL, streams[i], NORMAL_CLOSE);
3446 /*********************************************************************
3447 Create a default ACL by inheriting from the parent. If no inheritance
3448 from the parent available, don't set anything. This will leave the actual
3449 permissions the new file or directory already got from the filesystem
3450 as the NT ACL when read.
3451 *********************************************************************/
3453 static NTSTATUS inherit_new_acl(files_struct *fsp)
3455 TALLOC_CTX *frame = talloc_stackframe();
3456 char *parent_name = NULL;
3457 struct security_descriptor *parent_desc = NULL;
3458 NTSTATUS status = NT_STATUS_OK;
3459 struct security_descriptor *psd = NULL;
3460 const struct dom_sid *owner_sid = NULL;
3461 const struct dom_sid *group_sid = NULL;
3462 uint32_t security_info_sent = (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL);
3463 struct security_token *token = fsp->conn->session_info->security_token;
3464 bool inherit_owner = lp_inherit_owner(SNUM(fsp->conn));
3465 bool inheritable_components = false;
3466 bool try_builtin_administrators = false;
3467 const struct dom_sid *BA_U_sid = NULL;
3468 const struct dom_sid *BA_G_sid = NULL;
3469 bool try_system = false;
3470 const struct dom_sid *SY_U_sid = NULL;
3471 const struct dom_sid *SY_G_sid = NULL;
3474 if (!parent_dirname(frame, fsp->fsp_name->base_name, &parent_name, NULL)) {
3476 return NT_STATUS_NO_MEMORY;
3479 status = SMB_VFS_GET_NT_ACL(fsp->conn,
3481 (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL),
3484 if (!NT_STATUS_IS_OK(status)) {
3489 inheritable_components = sd_has_inheritable_components(parent_desc,
3492 if (!inheritable_components && !inherit_owner) {
3494 /* Nothing to inherit and not setting owner. */
3495 return NT_STATUS_OK;
3498 /* Create an inherited descriptor from the parent. */
3500 if (DEBUGLEVEL >= 10) {
3501 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3502 fsp_str_dbg(fsp) ));
3503 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
3506 /* Inherit from parent descriptor if "inherit owner" set. */
3507 if (inherit_owner) {
3508 owner_sid = parent_desc->owner_sid;
3509 group_sid = parent_desc->group_sid;
3512 if (owner_sid == NULL) {
3513 if (security_token_has_builtin_administrators(token)) {
3514 try_builtin_administrators = true;
3515 } else if (security_token_is_system(token)) {
3516 try_builtin_administrators = true;
3521 if (group_sid == NULL &&
3522 token->num_sids == PRIMARY_GROUP_SID_INDEX)
3524 if (security_token_is_system(token)) {
3525 try_builtin_administrators = true;
3530 if (try_builtin_administrators) {
3535 ok = sids_to_unixids(&global_sid_Builtin_Administrators, 1, &ids);
3539 BA_U_sid = &global_sid_Builtin_Administrators;
3540 BA_G_sid = &global_sid_Builtin_Administrators;
3543 BA_U_sid = &global_sid_Builtin_Administrators;
3546 BA_G_sid = &global_sid_Builtin_Administrators;
3559 ok = sids_to_unixids(&global_sid_System, 1, &ids);
3563 SY_U_sid = &global_sid_System;
3564 SY_G_sid = &global_sid_System;
3567 SY_U_sid = &global_sid_System;
3570 SY_G_sid = &global_sid_System;
3578 if (owner_sid == NULL) {
3579 owner_sid = BA_U_sid;
3582 if (owner_sid == NULL) {
3583 owner_sid = SY_U_sid;
3586 if (group_sid == NULL) {
3587 group_sid = SY_G_sid;
3590 if (try_system && group_sid == NULL) {
3591 group_sid = BA_G_sid;
3594 if (owner_sid == NULL) {
3595 owner_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3597 if (group_sid == NULL) {
3598 if (token->num_sids == PRIMARY_GROUP_SID_INDEX) {
3599 group_sid = &token->sids[PRIMARY_USER_SID_INDEX];
3601 group_sid = &token->sids[PRIMARY_GROUP_SID_INDEX];
3605 status = se_create_child_secdesc(frame,
3612 if (!NT_STATUS_IS_OK(status)) {
3617 /* If inheritable_components == false,
3618 se_create_child_secdesc()
3619 creates a security desriptor with a NULL dacl
3620 entry, but with SEC_DESC_DACL_PRESENT. We need
3621 to remove that flag. */
3623 if (!inheritable_components) {
3624 security_info_sent &= ~SECINFO_DACL;
3625 psd->type &= ~SEC_DESC_DACL_PRESENT;
3628 if (DEBUGLEVEL >= 10) {
3629 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3630 fsp_str_dbg(fsp) ));
3631 NDR_PRINT_DEBUG(security_descriptor, psd);
3634 if (inherit_owner) {
3635 /* We need to be root to force this. */
3638 status = SMB_VFS_FSET_NT_ACL(fsp,
3641 if (inherit_owner) {
3649 * Wrapper around open_file_ntcreate and open_directory
3652 static NTSTATUS create_file_unixpath(connection_struct *conn,
3653 struct smb_request *req,
3654 struct smb_filename *smb_fname,
3655 uint32_t access_mask,
3656 uint32_t share_access,
3657 uint32_t create_disposition,
3658 uint32_t create_options,
3659 uint32_t file_attributes,
3660 uint32_t oplock_request,
3661 uint64_t allocation_size,
3662 uint32_t private_flags,
3663 struct security_descriptor *sd,
3664 struct ea_list *ea_list,
3666 files_struct **result,
3669 int info = FILE_WAS_OPENED;
3670 files_struct *base_fsp = NULL;
3671 files_struct *fsp = NULL;
3674 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3675 "file_attributes = 0x%x, share_access = 0x%x, "
3676 "create_disposition = 0x%x create_options = 0x%x "
3677 "oplock_request = 0x%x private_flags = 0x%x "
3678 "ea_list = 0x%p, sd = 0x%p, "
3680 (unsigned int)access_mask,
3681 (unsigned int)file_attributes,
3682 (unsigned int)share_access,
3683 (unsigned int)create_disposition,
3684 (unsigned int)create_options,
3685 (unsigned int)oplock_request,
3686 (unsigned int)private_flags,
3687 ea_list, sd, smb_fname_str_dbg(smb_fname)));
3689 if (create_options & FILE_OPEN_BY_FILE_ID) {
3690 status = NT_STATUS_NOT_SUPPORTED;
3694 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
3695 status = NT_STATUS_INVALID_PARAMETER;
3700 oplock_request |= INTERNAL_OPEN_ONLY;
3703 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3704 && (access_mask & DELETE_ACCESS)
3705 && !is_ntfs_stream_smb_fname(smb_fname)) {
3707 * We can't open a file with DELETE access if any of the
3708 * streams is open without FILE_SHARE_DELETE
3710 status = open_streams_for_delete(conn, smb_fname->base_name);
3712 if (!NT_STATUS_IS_OK(status)) {
3717 if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) &&
3718 !security_token_has_privilege(get_current_nttok(conn),
3719 SEC_PRIV_SECURITY)) {
3720 DEBUG(10, ("create_file_unixpath: open on %s "
3721 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3722 smb_fname_str_dbg(smb_fname)));
3723 status = NT_STATUS_PRIVILEGE_NOT_HELD;
3727 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
3728 && is_ntfs_stream_smb_fname(smb_fname)
3729 && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
3730 uint32 base_create_disposition;
3731 struct smb_filename *smb_fname_base = NULL;
3733 if (create_options & FILE_DIRECTORY_FILE) {
3734 status = NT_STATUS_NOT_A_DIRECTORY;
3738 switch (create_disposition) {
3740 base_create_disposition = FILE_OPEN;
3743 base_create_disposition = FILE_OPEN_IF;
3747 /* Create an smb_filename with stream_name == NULL. */
3748 smb_fname_base = synthetic_smb_fname(talloc_tos(),
3749 smb_fname->base_name,
3751 if (smb_fname_base == NULL) {
3752 status = NT_STATUS_NO_MEMORY;
3756 if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
3757 DEBUG(10, ("Unable to stat stream: %s\n",
3758 smb_fname_str_dbg(smb_fname_base)));
3761 /* Open the base file. */
3762 status = create_file_unixpath(conn, NULL, smb_fname_base, 0,
3765 | FILE_SHARE_DELETE,
3766 base_create_disposition,
3767 0, 0, 0, 0, 0, NULL, NULL,
3769 TALLOC_FREE(smb_fname_base);
3771 if (!NT_STATUS_IS_OK(status)) {
3772 DEBUG(10, ("create_file_unixpath for base %s failed: "
3773 "%s\n", smb_fname->base_name,
3774 nt_errstr(status)));
3777 /* we don't need to low level fd */
3782 * If it's a request for a directory open, deal with it separately.
3785 if (create_options & FILE_DIRECTORY_FILE) {
3787 if (create_options & FILE_NON_DIRECTORY_FILE) {
3788 status = NT_STATUS_INVALID_PARAMETER;
3792 /* Can't open a temp directory. IFS kit test. */
3793 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
3794 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
3795 status = NT_STATUS_INVALID_PARAMETER;
3800 * We will get a create directory here if the Win32
3801 * app specified a security descriptor in the
3802 * CreateDirectory() call.
3806 status = open_directory(
3807 conn, req, smb_fname, access_mask, share_access,
3808 create_disposition, create_options, file_attributes,
3813 * Ordinary file case.
3816 status = file_new(req, conn, &fsp);
3817 if(!NT_STATUS_IS_OK(status)) {
3821 status = fsp_set_smb_fname(fsp, smb_fname);
3822 if (!NT_STATUS_IS_OK(status)) {
3828 * We're opening the stream element of a
3829 * base_fsp we already opened. Set up the
3832 fsp->base_fsp = base_fsp;
3835 if (allocation_size) {
3836 fsp->initial_allocation_size = smb_roundup(fsp->conn,
3840 status = open_file_ntcreate(conn,
3852 if(!NT_STATUS_IS_OK(status)) {
3853 file_free(req, fsp);
3857 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3859 /* A stream open never opens a directory */
3862 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3867 * Fail the open if it was explicitly a non-directory
3871 if (create_options & FILE_NON_DIRECTORY_FILE) {
3872 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3877 status = open_directory(
3878 conn, req, smb_fname, access_mask,
3879 share_access, create_disposition,
3880 create_options, file_attributes,
3885 if (!NT_STATUS_IS_OK(status)) {
3889 fsp->base_fsp = base_fsp;
3891 if ((ea_list != NULL) &&
3892 ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) {
3893 status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
3894 if (!NT_STATUS_IS_OK(status)) {
3899 if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
3900 status = NT_STATUS_ACCESS_DENIED;
3904 /* Save the requested allocation size. */
3905 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3907 && (allocation_size > fsp->fsp_name->st.st_ex_size)) {
3908 fsp->initial_allocation_size = smb_roundup(
3909 fsp->conn, allocation_size);
3910 if (fsp->is_directory) {
3911 /* Can't set allocation size on a directory. */
3912 status = NT_STATUS_ACCESS_DENIED;
3915 if (vfs_allocate_file_space(
3916 fsp, fsp->initial_allocation_size) == -1) {
3917 status = NT_STATUS_DISK_FULL;
3921 fsp->initial_allocation_size = smb_roundup(
3922 fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size);
3925 fsp->initial_allocation_size = 0;
3928 if ((info == FILE_WAS_CREATED) && lp_nt_acl_support(SNUM(conn)) &&
3929 fsp->base_fsp == NULL) {
3932 * According to the MS documentation, the only time the security
3933 * descriptor is applied to the opened file is iff we *created* the
3934 * file; an existing file stays the same.
3936 * Also, it seems (from observation) that you can open the file with
3937 * any access mask but you can still write the sd. We need to override
3938 * the granted access before we call set_sd
3939 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3942 uint32_t sec_info_sent;
3943 uint32_t saved_access_mask = fsp->access_mask;
3945 sec_info_sent = get_sec_info(sd);
3947 fsp->access_mask = FILE_GENERIC_ALL;
3949 if (sec_info_sent & (SECINFO_OWNER|
3953 status = set_sd(fsp, sd, sec_info_sent);
3956 fsp->access_mask = saved_access_mask;
3958 if (!NT_STATUS_IS_OK(status)) {
3961 } else if (lp_inherit_acls(SNUM(conn))) {
3962 /* Inherit from parent. Errors here are not fatal. */
3963 status = inherit_new_acl(fsp);
3964 if (!NT_STATUS_IS_OK(status)) {
3965 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3967 nt_errstr(status) ));
3972 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3975 if (pinfo != NULL) {
3979 smb_fname->st = fsp->fsp_name->st;
3981 return NT_STATUS_OK;
3984 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3987 if (base_fsp && fsp->base_fsp == base_fsp) {
3989 * The close_file below will close
3994 close_file(req, fsp, ERROR_CLOSE);
3997 if (base_fsp != NULL) {
3998 close_file(req, base_fsp, ERROR_CLOSE);
4005 * Calculate the full path name given a relative fid.
4007 NTSTATUS get_relative_fid_filename(connection_struct *conn,
4008 struct smb_request *req,
4009 uint16_t root_dir_fid,
4010 const struct smb_filename *smb_fname,
4011 struct smb_filename **smb_fname_out)
4013 files_struct *dir_fsp;
4014 char *parent_fname = NULL;
4015 char *new_base_name = NULL;
4018 if (root_dir_fid == 0 || !smb_fname) {
4019 status = NT_STATUS_INTERNAL_ERROR;
4023 dir_fsp = file_fsp(req, root_dir_fid);
4025 if (dir_fsp == NULL) {
4026 status = NT_STATUS_INVALID_HANDLE;
4030 if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) {
4031 status = NT_STATUS_INVALID_HANDLE;
4035 if (!dir_fsp->is_directory) {
4038 * Check to see if this is a mac fork of some kind.
4041 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
4042 is_ntfs_stream_smb_fname(smb_fname)) {
4043 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
4048 we need to handle the case when we get a
4049 relative open relative to a file and the
4050 pathname is blank - this is a reopen!
4051 (hint from demyn plantenberg)
4054 status = NT_STATUS_INVALID_HANDLE;
4058 if (ISDOT(dir_fsp->fsp_name->base_name)) {
4060 * We're at the toplevel dir, the final file name
4061 * must not contain ./, as this is filtered out
4062 * normally by srvstr_get_path and unix_convert
4063 * explicitly rejects paths containing ./.
4065 parent_fname = talloc_strdup(talloc_tos(), "");
4066 if (parent_fname == NULL) {
4067 status = NT_STATUS_NO_MEMORY;
4071 size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name);
4074 * Copy in the base directory name.
4077 parent_fname = talloc_array(talloc_tos(), char,
4079 if (parent_fname == NULL) {
4080 status = NT_STATUS_NO_MEMORY;
4083 memcpy(parent_fname, dir_fsp->fsp_name->base_name,
4087 * Ensure it ends in a '/'.
4088 * We used TALLOC_SIZE +2 to add space for the '/'.
4092 && (parent_fname[dir_name_len-1] != '\\')
4093 && (parent_fname[dir_name_len-1] != '/')) {
4094 parent_fname[dir_name_len] = '/';
4095 parent_fname[dir_name_len+1] = '\0';
4099 new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
4100 smb_fname->base_name);
4101 if (new_base_name == NULL) {
4102 status = NT_STATUS_NO_MEMORY;
4106 status = filename_convert(req,
4108 req->flags2 & FLAGS2_DFS_PATHNAMES,
4113 if (!NT_STATUS_IS_OK(status)) {
4118 TALLOC_FREE(parent_fname);
4119 TALLOC_FREE(new_base_name);
4123 NTSTATUS create_file_default(connection_struct *conn,
4124 struct smb_request *req,
4125 uint16_t root_dir_fid,
4126 struct smb_filename *smb_fname,
4127 uint32_t access_mask,
4128 uint32_t share_access,
4129 uint32_t create_disposition,
4130 uint32_t create_options,
4131 uint32_t file_attributes,
4132 uint32_t oplock_request,
4133 uint64_t allocation_size,
4134 uint32_t private_flags,
4135 struct security_descriptor *sd,
4136 struct ea_list *ea_list,
4137 files_struct **result,
4140 int info = FILE_WAS_OPENED;
4141 files_struct *fsp = NULL;
4143 bool stream_name = false;
4145 DEBUG(10,("create_file: access_mask = 0x%x "
4146 "file_attributes = 0x%x, share_access = 0x%x, "
4147 "create_disposition = 0x%x create_options = 0x%x "
4148 "oplock_request = 0x%x "
4149 "private_flags = 0x%x "
4150 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4152 (unsigned int)access_mask,
4153 (unsigned int)file_attributes,
4154 (unsigned int)share_access,
4155 (unsigned int)create_disposition,
4156 (unsigned int)create_options,
4157 (unsigned int)oplock_request,
4158 (unsigned int)private_flags,
4159 (unsigned int)root_dir_fid,
4160 ea_list, sd, smb_fname_str_dbg(smb_fname)));
4163 * Calculate the filename from the root_dir_if if necessary.
4166 if (root_dir_fid != 0) {
4167 struct smb_filename *smb_fname_out = NULL;
4168 status = get_relative_fid_filename(conn, req, root_dir_fid,
4169 smb_fname, &smb_fname_out);
4170 if (!NT_STATUS_IS_OK(status)) {
4173 smb_fname = smb_fname_out;
4177 * Check to see if this is a mac fork of some kind.
4180 stream_name = is_ntfs_stream_smb_fname(smb_fname);
4182 enum FAKE_FILE_TYPE fake_file_type;
4184 fake_file_type = is_fake_file(smb_fname);
4186 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
4189 * Here we go! support for changing the disk quotas
4192 * We need to fake up to open this MAGIC QUOTA file
4193 * and return a valid FID.
4195 * w2k close this file directly after openening xp
4196 * also tries a QUERY_FILE_INFO on the file and then
4199 status = open_fake_file(req, conn, req->vuid,
4200 fake_file_type, smb_fname,
4202 if (!NT_STATUS_IS_OK(status)) {
4206 ZERO_STRUCT(smb_fname->st);
4210 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
4211 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4216 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
4218 smb_fname->stream_name = NULL;
4219 /* We have to handle this error here. */
4220 if (create_options & FILE_DIRECTORY_FILE) {
4221 status = NT_STATUS_NOT_A_DIRECTORY;
4224 if (lp_posix_pathnames()) {
4225 ret = SMB_VFS_LSTAT(conn, smb_fname);
4227 ret = SMB_VFS_STAT(conn, smb_fname);
4230 if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
4231 status = NT_STATUS_FILE_IS_A_DIRECTORY;
4236 status = create_file_unixpath(
4237 conn, req, smb_fname, access_mask, share_access,
4238 create_disposition, create_options, file_attributes,
4239 oplock_request, allocation_size, private_flags,
4243 if (!NT_STATUS_IS_OK(status)) {
4248 DEBUG(10, ("create_file: info=%d\n", info));
4251 if (pinfo != NULL) {
4254 return NT_STATUS_OK;
4257 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
4260 close_file(req, fsp, ERROR_CLOSE);