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 "../librpc/gen_ndr/ioctl.h"
33 #include "passdb/lookup_sid.h"
37 #include "source3/lib/dbwrap/dbwrap_watch.h"
39 extern const struct generic_mapping file_generic_mapping
;
41 struct deferred_open_record
{
42 bool delayed_for_oplocks
;
47 /****************************************************************************
48 If the requester wanted DELETE_ACCESS and was rejected because
49 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
51 ****************************************************************************/
53 static bool parent_override_delete(connection_struct
*conn
,
54 const struct smb_filename
*smb_fname
,
56 uint32_t rejected_mask
)
58 if ((access_mask
& DELETE_ACCESS
) &&
59 (rejected_mask
& DELETE_ACCESS
) &&
60 can_delete_file_in_directory(conn
, smb_fname
)) {
66 /****************************************************************************
67 Check if we have open rights.
68 ****************************************************************************/
70 NTSTATUS
smbd_check_access_rights(struct connection_struct
*conn
,
71 const struct smb_filename
*smb_fname
,
75 /* Check if we have rights to open. */
77 struct security_descriptor
*sd
= NULL
;
78 uint32_t rejected_share_access
;
79 uint32_t rejected_mask
= access_mask
;
80 uint32_t do_not_check_mask
= 0;
82 rejected_share_access
= access_mask
& ~(conn
->share_access
);
84 if (rejected_share_access
) {
85 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
87 (unsigned int)access_mask
,
88 smb_fname_str_dbg(smb_fname
),
89 (unsigned int)rejected_share_access
));
90 return NT_STATUS_ACCESS_DENIED
;
93 if (!use_privs
&& get_current_uid(conn
) == (uid_t
)0) {
94 /* I'm sorry sir, I didn't know you were root... */
95 DEBUG(10,("smbd_check_access_rights: root override "
96 "on %s. Granting 0x%x\n",
97 smb_fname_str_dbg(smb_fname
),
98 (unsigned int)access_mask
));
102 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
103 DEBUG(10,("smbd_check_access_rights: not checking ACL "
104 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
105 smb_fname_str_dbg(smb_fname
),
106 (unsigned int)access_mask
));
110 if (access_mask
== DELETE_ACCESS
&&
111 VALID_STAT(smb_fname
->st
) &&
112 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
113 /* We can always delete a symlink. */
114 DEBUG(10,("smbd_check_access_rights: not checking ACL "
115 "on DELETE_ACCESS on symlink %s.\n",
116 smb_fname_str_dbg(smb_fname
) ));
120 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
123 SECINFO_DACL
), talloc_tos(), &sd
);
125 if (!NT_STATUS_IS_OK(status
)) {
126 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
128 smb_fname_str_dbg(smb_fname
),
131 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
139 * If we can access the path to this file, by
140 * default we have FILE_READ_ATTRIBUTES from the
141 * containing directory. See the section:
142 * "Algorithm to Check Access to an Existing File"
145 * se_file_access_check() also takes care of
146 * owner WRITE_DAC and READ_CONTROL.
148 do_not_check_mask
= FILE_READ_ATTRIBUTES
;
151 * Samba 3.6 and earlier granted execute access even
152 * if the ACL did not contain execute rights.
153 * Samba 4.0 is more correct and checks it.
154 * The compatibilty mode allows to skip this check
155 * to smoothen upgrades.
157 if (lp_acl_allow_execute_always(SNUM(conn
))) {
158 do_not_check_mask
|= FILE_EXECUTE
;
161 status
= se_file_access_check(sd
,
162 get_current_nttok(conn
),
164 (access_mask
& ~do_not_check_mask
),
167 DEBUG(10,("smbd_check_access_rights: file %s requesting "
168 "0x%x returning 0x%x (%s)\n",
169 smb_fname_str_dbg(smb_fname
),
170 (unsigned int)access_mask
,
171 (unsigned int)rejected_mask
,
172 nt_errstr(status
) ));
174 if (!NT_STATUS_IS_OK(status
)) {
175 if (DEBUGLEVEL
>= 10) {
176 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
177 smb_fname_str_dbg(smb_fname
) ));
178 NDR_PRINT_DEBUG(security_descriptor
, sd
);
184 if (NT_STATUS_IS_OK(status
) ||
185 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
189 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
193 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
194 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
195 !lp_store_dos_attributes(SNUM(conn
)) &&
196 (lp_map_readonly(SNUM(conn
)) ||
197 lp_map_archive(SNUM(conn
)) ||
198 lp_map_hidden(SNUM(conn
)) ||
199 lp_map_system(SNUM(conn
)))) {
200 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
202 DEBUG(10,("smbd_check_access_rights: "
204 "FILE_WRITE_ATTRIBUTES "
206 smb_fname_str_dbg(smb_fname
)));
209 if (parent_override_delete(conn
,
213 /* Were we trying to do an open
214 * for delete and didn't get DELETE
215 * access (only) ? Check if the
216 * directory allows DELETE_CHILD.
218 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
221 rejected_mask
&= ~DELETE_ACCESS
;
223 DEBUG(10,("smbd_check_access_rights: "
227 smb_fname_str_dbg(smb_fname
)));
230 if (rejected_mask
!= 0) {
231 return NT_STATUS_ACCESS_DENIED
;
236 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
237 struct smb_filename
*smb_fname
,
238 uint32_t access_mask
)
241 char *parent_dir
= NULL
;
242 struct security_descriptor
*parent_sd
= NULL
;
243 uint32_t access_granted
= 0;
245 if (!parent_dirname(talloc_tos(),
246 smb_fname
->base_name
,
249 return NT_STATUS_NO_MEMORY
;
252 if (get_current_uid(conn
) == (uid_t
)0) {
253 /* I'm sorry sir, I didn't know you were root... */
254 DEBUG(10,("check_parent_access: root override "
255 "on %s. Granting 0x%x\n",
256 smb_fname_str_dbg(smb_fname
),
257 (unsigned int)access_mask
));
261 status
= SMB_VFS_GET_NT_ACL(conn
,
267 if (!NT_STATUS_IS_OK(status
)) {
268 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
269 "%s with error %s\n",
276 * If we can access the path to this file, by
277 * default we have FILE_READ_ATTRIBUTES from the
278 * containing directory. See the section:
279 * "Algorithm to Check Access to an Existing File"
282 * se_file_access_check() also takes care of
283 * owner WRITE_DAC and READ_CONTROL.
285 status
= se_file_access_check(parent_sd
,
286 get_current_nttok(conn
),
288 (access_mask
& ~FILE_READ_ATTRIBUTES
),
290 if(!NT_STATUS_IS_OK(status
)) {
291 DEBUG(5,("check_parent_access: access check "
292 "on directory %s for "
293 "path %s for mask 0x%x returned (0x%x) %s\n",
295 smb_fname
->base_name
,
298 nt_errstr(status
) ));
305 /****************************************************************************
306 Ensure when opening a base file for a stream open that we have permissions
307 to do so given the access mask on the base file.
308 ****************************************************************************/
310 static NTSTATUS
check_base_file_access(struct connection_struct
*conn
,
311 struct smb_filename
*smb_fname
,
312 uint32_t access_mask
)
316 status
= smbd_calculate_access_mask(conn
, smb_fname
,
320 if (!NT_STATUS_IS_OK(status
)) {
321 DEBUG(10, ("smbd_calculate_access_mask "
322 "on file %s returned %s\n",
323 smb_fname_str_dbg(smb_fname
),
328 if (access_mask
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) {
330 if (!CAN_WRITE(conn
)) {
331 return NT_STATUS_ACCESS_DENIED
;
333 dosattrs
= dos_mode(conn
, smb_fname
);
334 if (IS_DOS_READONLY(dosattrs
)) {
335 return NT_STATUS_ACCESS_DENIED
;
339 return smbd_check_access_rights(conn
,
345 /****************************************************************************
346 fd support routines - attempt to do a dos_open.
347 ****************************************************************************/
349 NTSTATUS
fd_open(struct connection_struct
*conn
,
354 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
355 NTSTATUS status
= NT_STATUS_OK
;
359 * Never follow symlinks on a POSIX client. The
360 * client should be doing this.
363 if (fsp
->posix_open
|| !lp_follow_symlinks(SNUM(conn
))) {
368 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
369 if (fsp
->fh
->fd
== -1) {
370 int posix_errno
= errno
;
372 #if defined(ENOTSUP) && defined(OSF1)
373 /* handle special Tru64 errno */
374 if (errno
== ENOTSUP
) {
379 /* fix broken NetBSD errno */
380 if (errno
== EFTYPE
) {
384 /* fix broken FreeBSD errno */
385 if (errno
== EMLINK
) {
388 #endif /* O_NOFOLLOW */
389 status
= map_nt_error_from_unix(posix_errno
);
390 if (errno
== EMFILE
) {
391 static time_t last_warned
= 0L;
393 if (time((time_t *) NULL
) > last_warned
) {
394 DEBUG(0,("Too many open files, unable "
395 "to open more! smbd's max "
397 lp_max_open_files()));
398 last_warned
= time((time_t *) NULL
);
404 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
405 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
406 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
411 /****************************************************************************
412 Close the file associated with a fsp.
413 ****************************************************************************/
415 NTSTATUS
fd_close(files_struct
*fsp
)
422 if (fsp
->fh
->fd
== -1) {
423 return NT_STATUS_OK
; /* What we used to call a stat open. */
425 if (fsp
->fh
->ref_count
> 1) {
426 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
429 ret
= SMB_VFS_CLOSE(fsp
);
432 return map_nt_error_from_unix(errno
);
437 /****************************************************************************
438 Change the ownership of a file to that of the parent directory.
439 Do this by fd if possible.
440 ****************************************************************************/
442 void change_file_owner_to_parent(connection_struct
*conn
,
443 const char *inherit_from_dir
,
446 struct smb_filename
*smb_fname_parent
;
449 smb_fname_parent
= synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
451 if (smb_fname_parent
== NULL
) {
455 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
457 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
458 "directory %s. Error was %s\n",
459 smb_fname_str_dbg(smb_fname_parent
),
461 TALLOC_FREE(smb_fname_parent
);
465 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
466 /* Already this uid - no need to change. */
467 DEBUG(10,("change_file_owner_to_parent: file %s "
468 "is already owned by uid %d\n",
470 (int)fsp
->fsp_name
->st
.st_ex_uid
));
471 TALLOC_FREE(smb_fname_parent
);
476 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
479 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
480 "file %s to parent directory uid %u. Error "
481 "was %s\n", fsp_str_dbg(fsp
),
482 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
485 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
486 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
487 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
488 /* Ensure the uid entry is updated. */
489 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
492 TALLOC_FREE(smb_fname_parent
);
495 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
496 const char *inherit_from_dir
,
498 SMB_STRUCT_STAT
*psbuf
)
500 struct smb_filename
*smb_fname_parent
;
501 struct smb_filename
*smb_fname_cwd
= NULL
;
502 char *saved_dir
= NULL
;
503 TALLOC_CTX
*ctx
= talloc_tos();
504 NTSTATUS status
= NT_STATUS_OK
;
507 smb_fname_parent
= synthetic_smb_fname(ctx
, inherit_from_dir
,
509 if (smb_fname_parent
== NULL
) {
510 return NT_STATUS_NO_MEMORY
;
513 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
515 status
= map_nt_error_from_unix(errno
);
516 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
517 "directory %s. Error was %s\n",
518 smb_fname_str_dbg(smb_fname_parent
),
523 /* We've already done an lstat into psbuf, and we know it's a
524 directory. If we can cd into the directory and the dev/ino
525 are the same then we can safely chown without races as
526 we're locking the directory in place by being in it. This
527 should work on any UNIX (thanks tridge :-). JRA.
530 saved_dir
= vfs_GetWd(ctx
,conn
);
532 status
= map_nt_error_from_unix(errno
);
533 DEBUG(0,("change_dir_owner_to_parent: failed to get "
534 "current working directory. Error was %s\n",
539 /* Chdir into the new path. */
540 if (vfs_ChDir(conn
, fname
) == -1) {
541 status
= map_nt_error_from_unix(errno
);
542 DEBUG(0,("change_dir_owner_to_parent: failed to change "
543 "current working directory to %s. Error "
544 "was %s\n", fname
, strerror(errno
) ));
548 smb_fname_cwd
= synthetic_smb_fname(ctx
, ".", NULL
, NULL
);
549 if (smb_fname_cwd
== NULL
) {
550 status
= NT_STATUS_NO_MEMORY
;
554 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
556 status
= map_nt_error_from_unix(errno
);
557 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
558 "directory '.' (%s) Error was %s\n",
559 fname
, strerror(errno
)));
563 /* Ensure we're pointing at the same place. */
564 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
565 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
566 DEBUG(0,("change_dir_owner_to_parent: "
567 "device/inode on directory %s changed. "
568 "Refusing to chown !\n", fname
));
569 status
= NT_STATUS_ACCESS_DENIED
;
573 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
574 /* Already this uid - no need to change. */
575 DEBUG(10,("change_dir_owner_to_parent: directory %s "
576 "is already owned by uid %d\n",
578 (int)smb_fname_cwd
->st
.st_ex_uid
));
579 status
= NT_STATUS_OK
;
584 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
588 status
= map_nt_error_from_unix(errno
);
589 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
590 "directory %s to parent directory uid %u. "
591 "Error was %s\n", fname
,
592 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
595 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
596 "directory %s to parent directory uid %u.\n",
597 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
598 /* Ensure the uid entry is updated. */
599 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
603 vfs_ChDir(conn
,saved_dir
);
605 TALLOC_FREE(smb_fname_parent
);
606 TALLOC_FREE(smb_fname_cwd
);
610 /****************************************************************************
611 Open a file - returning a guaranteed ATOMIC indication of if the
612 file was created or not.
613 ****************************************************************************/
615 static NTSTATUS
fd_open_atomic(struct connection_struct
*conn
,
621 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
622 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
624 *file_created
= false;
626 if (!(flags
& O_CREAT
)) {
628 * We're not creating the file, just pass through.
630 return fd_open(conn
, fsp
, flags
, mode
);
633 if (flags
& O_EXCL
) {
635 * Fail if already exists, just pass through.
637 status
= fd_open(conn
, fsp
, flags
, mode
);
640 * Here we've opened with O_CREAT|O_EXCL. If that went
641 * NT_STATUS_OK, we *know* we created this file.
643 *file_created
= NT_STATUS_IS_OK(status
);
649 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
650 * To know absolutely if we created the file or not,
651 * we can never call O_CREAT without O_EXCL. So if
652 * we think the file existed, try without O_CREAT|O_EXCL.
653 * If we think the file didn't exist, try with
654 * O_CREAT|O_EXCL. Keep bouncing between these two
655 * requests until either the file is created, or
656 * opened. Either way, we keep going until we get
657 * a returnable result (error, or open/create).
661 int curr_flags
= flags
;
664 /* Just try open, do not create. */
665 curr_flags
&= ~(O_CREAT
);
666 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
667 if (NT_STATUS_EQUAL(status
,
668 NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
670 * Someone deleted it in the meantime.
673 file_existed
= false;
674 DEBUG(10,("fd_open_atomic: file %s existed. "
676 smb_fname_str_dbg(fsp
->fsp_name
)));
680 /* Try create exclusively, fail if it exists. */
681 curr_flags
|= O_EXCL
;
682 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
683 if (NT_STATUS_EQUAL(status
,
684 NT_STATUS_OBJECT_NAME_COLLISION
)) {
686 * Someone created it in the meantime.
687 * Retry without O_CREAT.
690 DEBUG(10,("fd_open_atomic: file %s "
691 "did not exist. Retry.\n",
692 smb_fname_str_dbg(fsp
->fsp_name
)));
695 if (NT_STATUS_IS_OK(status
)) {
697 * Here we've opened with O_CREAT|O_EXCL
698 * and got success. We *know* we created
701 *file_created
= true;
704 /* Create is done, or failed. */
710 /****************************************************************************
712 ****************************************************************************/
714 static NTSTATUS
open_file(files_struct
*fsp
,
715 connection_struct
*conn
,
716 struct smb_request
*req
,
717 const char *parent_dir
,
720 uint32 access_mask
, /* client requested access mask. */
721 uint32 open_access_mask
, /* what we're actually using in the open. */
722 bool *p_file_created
)
724 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
725 NTSTATUS status
= NT_STATUS_OK
;
726 int accmode
= (flags
& O_ACCMODE
);
727 int local_flags
= flags
;
728 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
733 /* Check permissions */
736 * This code was changed after seeing a client open request
737 * containing the open mode of (DENY_WRITE/read-only) with
738 * the 'create if not exist' bit set. The previous code
739 * would fail to open the file read only on a read-only share
740 * as it was checking the flags parameter directly against O_RDONLY,
741 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
745 if (!CAN_WRITE(conn
)) {
746 /* It's a read-only share - fail if we wanted to write. */
747 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
748 DEBUG(3,("Permission denied opening %s\n",
749 smb_fname_str_dbg(smb_fname
)));
750 return NT_STATUS_ACCESS_DENIED
;
752 if (flags
& O_CREAT
) {
753 /* We don't want to write - but we must make sure that
754 O_CREAT doesn't create the file if we have write
755 access into the directory.
757 flags
&= ~(O_CREAT
|O_EXCL
);
758 local_flags
&= ~(O_CREAT
|O_EXCL
);
763 * This little piece of insanity is inspired by the
764 * fact that an NT client can open a file for O_RDONLY,
765 * but set the create disposition to FILE_EXISTS_TRUNCATE.
766 * If the client *can* write to the file, then it expects to
767 * truncate the file, even though it is opening for readonly.
768 * Quicken uses this stupid trick in backup file creation...
769 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
770 * for helping track this one down. It didn't bite us in 2.0.x
771 * as we always opened files read-write in that release. JRA.
774 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
775 DEBUG(10,("open_file: truncate requested on read-only open "
776 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
777 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
780 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
781 (!file_existed
&& (local_flags
& O_CREAT
)) ||
782 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
786 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
788 * We would block on opening a FIFO with no one else on the
789 * other end. Do what we used to do and add O_NONBLOCK to the
793 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
794 local_flags
&= ~O_TRUNC
; /* Can't truncate a FIFO. */
795 local_flags
|= O_NONBLOCK
;
799 /* Don't create files with Microsoft wildcard characters. */
802 * wildcard characters are allowed in stream names
803 * only test the basefilename
805 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
807 wild
= smb_fname
->base_name
;
809 if ((local_flags
& O_CREAT
) && !file_existed
&&
811 return NT_STATUS_OBJECT_NAME_INVALID
;
814 /* Can we access this file ? */
815 if (!fsp
->base_fsp
) {
816 /* Only do this check on non-stream open. */
818 status
= smbd_check_access_rights(conn
,
822 } else if (local_flags
& O_CREAT
){
823 status
= check_parent_access(conn
,
827 /* File didn't exist and no O_CREAT. */
828 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
830 if (!NT_STATUS_IS_OK(status
)) {
831 DEBUG(10,("open_file: "
835 "smbd_check_access_rights" :
836 "check_parent_access",
837 smb_fname_str_dbg(smb_fname
),
838 nt_errstr(status
) ));
843 /* Actually do the open */
844 status
= fd_open_atomic(conn
, fsp
, local_flags
,
845 unx_mode
, p_file_created
);
846 if (!NT_STATUS_IS_OK(status
)) {
847 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
848 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
849 nt_errstr(status
),local_flags
,flags
));
853 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
855 /* If we have an fd, this stat should succeed. */
856 DEBUG(0,("Error doing fstat on open file %s "
858 smb_fname_str_dbg(smb_fname
),
860 status
= map_nt_error_from_unix(errno
);
865 if (*p_file_created
) {
866 /* We created this file. */
868 bool need_re_stat
= false;
869 /* Do all inheritance work after we've
870 done a successful fstat call and filled
871 in the stat struct in fsp->fsp_name. */
873 /* Inherit the ACL if required */
874 if (lp_inherit_permissions(SNUM(conn
))) {
875 inherit_access_posix_acl(conn
, parent_dir
,
876 smb_fname
->base_name
,
881 /* Change the owner if required. */
882 if (lp_inherit_owner(SNUM(conn
))) {
883 change_file_owner_to_parent(conn
, parent_dir
,
889 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
890 /* If we have an fd, this stat should succeed. */
892 DEBUG(0,("Error doing fstat on open file %s "
894 smb_fname_str_dbg(smb_fname
),
899 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
900 FILE_NOTIFY_CHANGE_FILE_NAME
,
901 smb_fname
->base_name
);
904 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
906 /* File must exist for a stat open. */
907 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
910 status
= smbd_check_access_rights(conn
,
915 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
917 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
918 /* This is a POSIX stat open for delete
919 * or rename on a symlink that points
921 DEBUG(10,("open_file: allowing POSIX "
922 "open on bad symlink %s\n",
923 smb_fname_str_dbg(smb_fname
)));
924 status
= NT_STATUS_OK
;
927 if (!NT_STATUS_IS_OK(status
)) {
928 DEBUG(10,("open_file: "
929 "smbd_check_access_rights on file "
931 smb_fname_str_dbg(smb_fname
),
932 nt_errstr(status
) ));
938 * POSIX allows read-only opens of directories. We don't
939 * want to do this (we use a different code path for this)
940 * so catch a directory open and return an EISDIR. JRA.
943 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
946 return NT_STATUS_FILE_IS_A_DIRECTORY
;
949 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
950 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
951 fsp
->file_pid
= req
? req
->smbpid
: 0;
952 fsp
->can_lock
= True
;
953 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
956 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
957 fsp
->print_file
= NULL
;
958 fsp
->modified
= False
;
959 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
960 fsp
->is_directory
= False
;
961 if (conn
->aio_write_behind_list
&&
962 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
963 conn
->case_sensitive
)) {
964 fsp
->aio_write_behind
= True
;
967 fsp
->wcp
= NULL
; /* Write cache pointer. */
969 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
970 conn
->session_info
->unix_info
->unix_name
,
971 smb_fname_str_dbg(smb_fname
),
972 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
973 conn
->num_files_open
));
979 /****************************************************************************
980 Check if we can open a file with a share mode.
981 Returns True if conflict, False if not.
982 ****************************************************************************/
984 static bool share_conflict(struct share_mode_entry
*entry
,
988 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
989 "entry->share_access = 0x%x, "
990 "entry->private_options = 0x%x\n",
991 (unsigned int)entry
->access_mask
,
992 (unsigned int)entry
->share_access
,
993 (unsigned int)entry
->private_options
));
995 if (server_id_is_disconnected(&entry
->pid
)) {
997 * note: cleanup should have been done by
998 * delay_for_batch_oplocks()
1003 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1004 (unsigned int)access_mask
, (unsigned int)share_access
));
1006 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
1010 DELETE_ACCESS
)) == 0) {
1011 DEBUG(10,("share_conflict: No conflict due to "
1012 "entry->access_mask = 0x%x\n",
1013 (unsigned int)entry
->access_mask
));
1017 if ((access_mask
& (FILE_WRITE_DATA
|
1021 DELETE_ACCESS
)) == 0) {
1022 DEBUG(10,("share_conflict: No conflict due to "
1023 "access_mask = 0x%x\n",
1024 (unsigned int)access_mask
));
1028 #if 1 /* JRA TEST - Superdebug. */
1029 #define CHECK_MASK(num, am, right, sa, share) \
1030 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1031 (unsigned int)(num), (unsigned int)(am), \
1032 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1033 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1034 (unsigned int)(num), (unsigned int)(sa), \
1035 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1036 if (((am) & (right)) && !((sa) & (share))) { \
1037 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1038 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1039 (unsigned int)(share) )); \
1043 #define CHECK_MASK(num, am, right, sa, share) \
1044 if (((am) & (right)) && !((sa) & (share))) { \
1045 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1046 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1047 (unsigned int)(share) )); \
1052 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1053 share_access
, FILE_SHARE_WRITE
);
1054 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1055 entry
->share_access
, FILE_SHARE_WRITE
);
1057 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1058 share_access
, FILE_SHARE_READ
);
1059 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1060 entry
->share_access
, FILE_SHARE_READ
);
1062 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
1063 share_access
, FILE_SHARE_DELETE
);
1064 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
1065 entry
->share_access
, FILE_SHARE_DELETE
);
1067 DEBUG(10,("share_conflict: No conflict.\n"));
1071 #if defined(DEVELOPER)
1072 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
1074 struct share_mode_entry
*share_entry
)
1076 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1079 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1083 if (!is_valid_share_mode_entry(share_entry
)) {
1087 fsp
= file_find_dif(sconn
, share_entry
->id
,
1088 share_entry
->share_file_id
);
1090 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1091 share_mode_str(talloc_tos(), num
, share_entry
) ));
1092 smb_panic("validate_my_share_entries: Cannot match a "
1093 "share entry with an open file\n");
1096 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
1105 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1106 share_mode_str(talloc_tos(), num
, share_entry
) ));
1107 str
= talloc_asprintf(talloc_tos(),
1108 "validate_my_share_entries: "
1109 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1110 fsp
->fsp_name
->base_name
,
1111 (unsigned int)fsp
->oplock_type
,
1112 (unsigned int)share_entry
->op_type
);
1118 bool is_stat_open(uint32 access_mask
)
1120 const uint32_t stat_open_bits
=
1121 (SYNCHRONIZE_ACCESS
|
1122 FILE_READ_ATTRIBUTES
|
1123 FILE_WRITE_ATTRIBUTES
);
1125 return (((access_mask
& stat_open_bits
) != 0) &&
1126 ((access_mask
& ~stat_open_bits
) == 0));
1129 static bool has_delete_on_close(struct share_mode_lock
*lck
,
1132 struct share_mode_data
*d
= lck
->data
;
1135 if (d
->num_share_modes
== 0) {
1138 if (!is_delete_on_close_set(lck
, name_hash
)) {
1141 for (i
=0; i
<d
->num_share_modes
; i
++) {
1142 if (!share_mode_stale_pid(d
, i
)) {
1149 /****************************************************************************
1150 Deal with share modes
1151 Invarient: Share mode must be locked on entry and exit.
1152 Returns -1 on error, or number of share modes on success (may be zero).
1153 ****************************************************************************/
1155 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1156 struct share_mode_lock
*lck
,
1158 uint32 share_access
)
1162 if(lck
->data
->num_share_modes
== 0) {
1163 return NT_STATUS_OK
;
1166 if (is_stat_open(access_mask
)) {
1167 /* Stat open that doesn't trigger oplock breaks or share mode
1168 * checks... ! JRA. */
1169 return NT_STATUS_OK
;
1173 * Check if the share modes will give us access.
1176 #if defined(DEVELOPER)
1177 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1178 validate_my_share_entries(conn
->sconn
, i
,
1179 &lck
->data
->share_modes
[i
]);
1183 /* Now we check the share modes, after any oplock breaks. */
1184 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1186 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1190 /* someone else has a share lock on it, check to see if we can
1192 if (share_conflict(&lck
->data
->share_modes
[i
],
1193 access_mask
, share_access
)) {
1195 if (share_mode_stale_pid(lck
->data
, i
)) {
1199 return NT_STATUS_SHARING_VIOLATION
;
1203 return NT_STATUS_OK
;
1207 * Send a break message to the oplock holder and delay the open for
1211 static NTSTATUS
send_break_message(struct messaging_context
*msg_ctx
,
1212 const struct share_mode_entry
*exclusive
,
1216 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1218 DEBUG(10, ("Sending break request to PID %s\n",
1219 procid_str_static(&exclusive
->pid
)));
1221 /* Create the message. */
1222 share_mode_entry_to_message(msg
, exclusive
);
1224 /* Overload entry->op_type */
1225 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
, break_to
);
1227 status
= messaging_send_buf(msg_ctx
, exclusive
->pid
,
1228 MSG_SMB_BREAK_REQUEST
,
1229 (uint8
*)msg
, sizeof(msg
));
1230 if (!NT_STATUS_IS_OK(status
)) {
1231 DEBUG(3, ("Could not send oplock break message: %s\n",
1232 nt_errstr(status
)));
1239 * Do internal consistency checks on the share mode for a file.
1242 static bool validate_oplock_types(struct share_mode_lock
*lck
)
1244 struct share_mode_data
*d
= lck
->data
;
1246 bool ex_or_batch
= false;
1247 bool level2
= false;
1248 bool no_oplock
= false;
1249 uint32_t num_non_stat_opens
= 0;
1252 for (i
=0; i
<d
->num_share_modes
; i
++) {
1253 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1255 if (!is_valid_share_mode_entry(e
)) {
1259 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1260 /* We ignore stat opens in the table - they
1261 always have NO_OPLOCK and never get or
1262 cause breaks. JRA. */
1266 num_non_stat_opens
+= 1;
1268 if (BATCH_OPLOCK_TYPE(e
->op_type
)) {
1269 /* batch - can only be one. */
1270 if (share_mode_stale_pid(d
, i
)) {
1271 DEBUG(10, ("Found stale batch oplock\n"));
1274 if (ex_or_batch
|| batch
|| level2
|| no_oplock
) {
1275 DEBUG(0, ("Bad batch oplock entry %u.",
1282 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1283 if (share_mode_stale_pid(d
, i
)) {
1284 DEBUG(10, ("Found stale duplicate oplock\n"));
1287 /* Exclusive or batch - can only be one. */
1288 if (ex_or_batch
|| level2
|| no_oplock
) {
1289 DEBUG(0, ("Bad exclusive or batch oplock "
1290 "entry %u.", (unsigned)i
));
1296 if (LEVEL_II_OPLOCK_TYPE(e
->op_type
)) {
1297 if (batch
|| ex_or_batch
) {
1298 if (share_mode_stale_pid(d
, i
)) {
1299 DEBUG(10, ("Found stale LevelII "
1303 DEBUG(0, ("Bad levelII oplock entry %u.",
1310 if (e
->op_type
== NO_OPLOCK
) {
1311 if (batch
|| ex_or_batch
) {
1312 if (share_mode_stale_pid(d
, i
)) {
1313 DEBUG(10, ("Found stale NO_OPLOCK "
1317 DEBUG(0, ("Bad no oplock entry %u.",
1325 remove_stale_share_mode_entries(d
);
1327 if ((batch
|| ex_or_batch
) && (num_non_stat_opens
!= 1)) {
1328 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1329 (int)batch
, (int)ex_or_batch
,
1330 (int)d
->num_share_modes
));
1337 static bool delay_for_oplock(files_struct
*fsp
,
1339 struct share_mode_lock
*lck
,
1340 bool have_sharing_violation
,
1341 uint32_t create_disposition
)
1343 struct share_mode_data
*d
= lck
->data
;
1344 struct share_mode_entry
*entry
;
1345 uint32_t num_non_stat_opens
= 0;
1349 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1352 for (i
=0; i
<d
->num_share_modes
; i
++) {
1353 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1354 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1357 num_non_stat_opens
+= 1;
1360 * We found the a non-stat open, which in the exclusive/batch
1361 * case will be inspected further down.
1365 if (num_non_stat_opens
== 0) {
1367 * Nothing to wait for around
1371 if (num_non_stat_opens
!= 1) {
1373 * More than one open around. There can't be any exclusive or
1374 * batch left, this is all level2.
1379 if (server_id_is_disconnected(&entry
->pid
)) {
1382 * This could be achieved by sending a break message
1383 * to ourselves. Special considerations for files
1384 * with delete_on_close flag set!
1386 * For now we keep it simple and do not
1387 * allow delete on close for durable handles.
1392 switch (create_disposition
) {
1393 case FILE_SUPERSEDE
:
1394 case FILE_OVERWRITE_IF
:
1395 break_to
= NO_OPLOCK
;
1398 break_to
= LEVEL_II_OPLOCK
;
1402 if (have_sharing_violation
&& (entry
->op_type
& BATCH_OPLOCK
)) {
1403 if (share_mode_stale_pid(d
, 0)) {
1406 send_break_message(fsp
->conn
->sconn
->msg_ctx
, entry
, break_to
);
1409 if (have_sharing_violation
) {
1411 * Non-batch exclusive is not broken if we have a sharing
1416 if (LEVEL_II_OPLOCK_TYPE(entry
->op_type
) &&
1417 (break_to
== NO_OPLOCK
)) {
1418 if (share_mode_stale_pid(d
, 0)) {
1421 DEBUG(10, ("Asynchronously breaking level2 oplock for "
1422 "create_disposition=%u\n",
1423 (unsigned)create_disposition
));
1424 send_break_message(fsp
->conn
->sconn
->msg_ctx
, entry
, break_to
);
1427 if (!EXCLUSIVE_OPLOCK_TYPE(entry
->op_type
)) {
1429 * No break for NO_OPLOCK or LEVEL2_OPLOCK oplocks
1433 if (share_mode_stale_pid(d
, 0)) {
1437 send_break_message(fsp
->conn
->sconn
->msg_ctx
, entry
, break_to
);
1441 static bool file_has_brlocks(files_struct
*fsp
)
1443 struct byte_range_lock
*br_lck
;
1445 br_lck
= brl_get_locks_readonly(fsp
);
1449 return (brl_num_locks(br_lck
) > 0);
1452 static void grant_fsp_oplock_type(files_struct
*fsp
,
1453 struct share_mode_lock
*lck
,
1456 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1457 lp_level2_oplocks(SNUM(fsp
->conn
));
1458 bool got_level2_oplock
, got_a_none_oplock
;
1461 /* Start by granting what the client asked for,
1462 but ensure no SAMBA_PRIVATE bits can be set. */
1463 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1465 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1466 /* No oplocks on internal open. */
1467 fsp
->oplock_type
= NO_OPLOCK
;
1468 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1469 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1473 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1474 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1476 fsp
->oplock_type
= NO_OPLOCK
;
1479 if (is_stat_open(fsp
->access_mask
)) {
1480 /* Leave the value already set. */
1481 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1482 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1486 got_level2_oplock
= false;
1487 got_a_none_oplock
= false;
1489 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1490 int op_type
= lck
->data
->share_modes
[i
].op_type
;
1492 if (LEVEL_II_OPLOCK_TYPE(op_type
)) {
1493 got_level2_oplock
= true;
1495 if (op_type
== NO_OPLOCK
) {
1496 got_a_none_oplock
= true;
1501 * Match what was requested (fsp->oplock_type) with
1502 * what was found in the existing share modes.
1505 if (got_level2_oplock
|| got_a_none_oplock
) {
1506 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1507 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1512 * Don't grant level2 to clients that don't want them
1513 * or if we've turned them off.
1515 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1516 fsp
->oplock_type
= NO_OPLOCK
;
1519 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !got_level2_oplock
) {
1521 * We're the first level2 oplock. Indicate that in brlock.tdb.
1523 struct byte_range_lock
*brl
;
1525 brl
= brl_get_locks(talloc_tos(), fsp
);
1527 brl_set_have_read_oplocks(brl
, true);
1532 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1533 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1536 static bool request_timed_out(struct timeval request_time
,
1537 struct timeval timeout
)
1539 struct timeval now
, end_time
;
1541 end_time
= timeval_sum(&request_time
, &timeout
);
1542 return (timeval_compare(&end_time
, &now
) < 0);
1545 struct defer_open_state
{
1546 struct smbd_server_connection
*sconn
;
1550 static void defer_open_done(struct tevent_req
*req
);
1552 /****************************************************************************
1553 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1554 ****************************************************************************/
1556 static void defer_open(struct share_mode_lock
*lck
,
1557 struct timeval request_time
,
1558 struct timeval timeout
,
1559 struct smb_request
*req
,
1560 struct deferred_open_record
*state
)
1562 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1563 "open entry for mid %llu\n",
1564 (unsigned int)request_time
.tv_sec
,
1565 (unsigned int)request_time
.tv_usec
,
1566 (unsigned long long)req
->mid
));
1568 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1569 state
->id
, (char *)state
, sizeof(*state
))) {
1571 exit_server("push_deferred_open_message_smb failed");
1574 struct defer_open_state
*watch_state
;
1575 struct tevent_req
*watch_req
;
1578 watch_state
= talloc(req
->sconn
, struct defer_open_state
);
1579 if (watch_state
== NULL
) {
1580 exit_server("talloc failed");
1582 watch_state
->sconn
= req
->sconn
;
1583 watch_state
->mid
= req
->mid
;
1585 DEBUG(10, ("defering mid %llu\n",
1586 (unsigned long long)req
->mid
));
1588 watch_req
= dbwrap_record_watch_send(
1589 watch_state
, req
->sconn
->ev_ctx
, lck
->data
->record
,
1590 req
->sconn
->msg_ctx
);
1591 if (watch_req
== NULL
) {
1592 exit_server("Could not watch share mode record");
1594 tevent_req_set_callback(watch_req
, defer_open_done
,
1597 ret
= tevent_req_set_endtime(
1598 watch_req
, req
->sconn
->ev_ctx
,
1599 timeval_sum(&request_time
, &timeout
));
1604 static void defer_open_done(struct tevent_req
*req
)
1606 struct defer_open_state
*state
= tevent_req_callback_data(
1607 req
, struct defer_open_state
);
1611 status
= dbwrap_record_watch_recv(req
, talloc_tos(), NULL
);
1613 if (!NT_STATUS_IS_OK(status
)) {
1614 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1615 nt_errstr(status
)));
1617 * Even if it failed, retry anyway. TODO: We need a way to
1618 * tell a re-scheduled open about that error.
1622 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state
->mid
));
1624 ret
= schedule_deferred_open_message_smb(state
->sconn
, state
->mid
);
1630 /****************************************************************************
1631 On overwrite open ensure that the attributes match.
1632 ****************************************************************************/
1634 static bool open_match_attributes(connection_struct
*conn
,
1635 uint32 old_dos_attr
,
1636 uint32 new_dos_attr
,
1637 mode_t existing_unx_mode
,
1638 mode_t new_unx_mode
,
1639 mode_t
*returned_unx_mode
)
1641 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1643 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1644 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1646 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1647 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1648 *returned_unx_mode
= new_unx_mode
;
1650 *returned_unx_mode
= (mode_t
)0;
1653 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1654 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1655 "returned_unx_mode = 0%o\n",
1656 (unsigned int)old_dos_attr
,
1657 (unsigned int)existing_unx_mode
,
1658 (unsigned int)new_dos_attr
,
1659 (unsigned int)*returned_unx_mode
));
1661 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1662 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1663 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1664 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1668 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1669 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1670 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1677 /****************************************************************************
1678 Special FCB or DOS processing in the case of a sharing violation.
1679 Try and find a duplicated file handle.
1680 ****************************************************************************/
1682 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1683 connection_struct
*conn
,
1684 files_struct
*fsp_to_dup_into
,
1685 const struct smb_filename
*smb_fname
,
1690 uint32 share_access
,
1691 uint32 create_options
)
1695 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1696 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1698 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1699 fsp
= file_find_di_next(fsp
)) {
1701 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1702 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1703 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1704 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
1705 (unsigned int)fsp
->file_pid
,
1706 (unsigned int)fsp
->fh
->private_options
,
1707 (unsigned int)fsp
->access_mask
));
1709 if (fsp
!= fsp_to_dup_into
&&
1710 fsp
->fh
->fd
!= -1 &&
1711 fsp
->vuid
== vuid
&&
1712 fsp
->file_pid
== file_pid
&&
1713 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1714 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1715 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1716 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1717 strequal(fsp
->fsp_name
->stream_name
,
1718 smb_fname
->stream_name
)) {
1719 DEBUG(10,("fcb_or_dos_open: file match\n"));
1725 return NT_STATUS_NOT_FOUND
;
1728 /* quite an insane set of semantics ... */
1729 if (is_executable(smb_fname
->base_name
) &&
1730 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1731 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1732 return NT_STATUS_INVALID_PARAMETER
;
1735 /* We need to duplicate this fsp. */
1736 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1737 create_options
, fsp_to_dup_into
);
1740 static void schedule_defer_open(struct share_mode_lock
*lck
,
1742 struct timeval request_time
,
1743 struct smb_request
*req
)
1745 struct deferred_open_record state
;
1747 /* This is a relative time, added to the absolute
1748 request_time value to get the absolute timeout time.
1749 Note that if this is the second or greater time we enter
1750 this codepath for this particular request mid then
1751 request_time is left as the absolute time of the *first*
1752 time this request mid was processed. This is what allows
1753 the request to eventually time out. */
1755 struct timeval timeout
;
1757 /* Normally the smbd we asked should respond within
1758 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1759 * the client did, give twice the timeout as a safety
1760 * measure here in case the other smbd is stuck
1761 * somewhere else. */
1763 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1765 /* Nothing actually uses state.delayed_for_oplocks
1766 but it's handy to differentiate in debug messages
1767 between a 30 second delay due to oplock break, and
1768 a 1 second delay for share mode conflicts. */
1770 state
.delayed_for_oplocks
= True
;
1771 state
.async_open
= false;
1774 if (!request_timed_out(request_time
, timeout
)) {
1775 defer_open(lck
, request_time
, timeout
, req
, &state
);
1779 /****************************************************************************
1780 Reschedule an open call that went asynchronous.
1781 ****************************************************************************/
1783 static void schedule_async_open(struct timeval request_time
,
1784 struct smb_request
*req
)
1786 struct deferred_open_record state
;
1787 struct timeval timeout
;
1789 timeout
= timeval_set(20, 0);
1792 state
.delayed_for_oplocks
= false;
1793 state
.async_open
= true;
1795 if (!request_timed_out(request_time
, timeout
)) {
1796 defer_open(NULL
, request_time
, timeout
, req
, &state
);
1800 /****************************************************************************
1801 Work out what access_mask to use from what the client sent us.
1802 ****************************************************************************/
1804 static NTSTATUS
smbd_calculate_maximum_allowed_access(
1805 connection_struct
*conn
,
1806 const struct smb_filename
*smb_fname
,
1808 uint32_t *p_access_mask
)
1810 struct security_descriptor
*sd
;
1811 uint32_t access_granted
;
1814 if (!use_privs
&& (get_current_uid(conn
) == (uid_t
)0)) {
1815 *p_access_mask
|= FILE_GENERIC_ALL
;
1816 return NT_STATUS_OK
;
1819 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1825 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1827 * File did not exist
1829 *p_access_mask
= FILE_GENERIC_ALL
;
1830 return NT_STATUS_OK
;
1832 if (!NT_STATUS_IS_OK(status
)) {
1833 DEBUG(10,("Could not get acl on file %s: %s\n",
1834 smb_fname_str_dbg(smb_fname
),
1835 nt_errstr(status
)));
1836 return NT_STATUS_ACCESS_DENIED
;
1840 * If we can access the path to this file, by
1841 * default we have FILE_READ_ATTRIBUTES from the
1842 * containing directory. See the section:
1843 * "Algorithm to Check Access to an Existing File"
1846 * se_file_access_check()
1847 * also takes care of owner WRITE_DAC and READ_CONTROL.
1849 status
= se_file_access_check(sd
,
1850 get_current_nttok(conn
),
1852 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
1857 if (!NT_STATUS_IS_OK(status
)) {
1858 DEBUG(10, ("Access denied on file %s: "
1859 "when calculating maximum access\n",
1860 smb_fname_str_dbg(smb_fname
)));
1861 return NT_STATUS_ACCESS_DENIED
;
1863 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
1865 if (!(access_granted
& DELETE_ACCESS
)) {
1866 if (can_delete_file_in_directory(conn
, smb_fname
)) {
1867 *p_access_mask
|= DELETE_ACCESS
;
1871 return NT_STATUS_OK
;
1874 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1875 const struct smb_filename
*smb_fname
,
1877 uint32_t access_mask
,
1878 uint32_t *access_mask_out
)
1881 uint32_t orig_access_mask
= access_mask
;
1882 uint32_t rejected_share_access
;
1885 * Convert GENERIC bits to specific bits.
1888 se_map_generic(&access_mask
, &file_generic_mapping
);
1890 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1891 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1893 status
= smbd_calculate_maximum_allowed_access(
1894 conn
, smb_fname
, use_privs
, &access_mask
);
1896 if (!NT_STATUS_IS_OK(status
)) {
1900 access_mask
&= conn
->share_access
;
1903 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1905 if (rejected_share_access
) {
1906 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1907 "file %s: rejected by share access mask[0x%08X] "
1908 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1909 smb_fname_str_dbg(smb_fname
),
1911 orig_access_mask
, access_mask
,
1912 rejected_share_access
));
1913 return NT_STATUS_ACCESS_DENIED
;
1916 *access_mask_out
= access_mask
;
1917 return NT_STATUS_OK
;
1920 /****************************************************************************
1921 Remove the deferred open entry under lock.
1922 ****************************************************************************/
1924 /****************************************************************************
1925 Return true if this is a state pointer to an asynchronous create.
1926 ****************************************************************************/
1928 bool is_deferred_open_async(const void *ptr
)
1930 const struct deferred_open_record
*state
= (const struct deferred_open_record
*)ptr
;
1932 return state
->async_open
;
1935 static bool clear_ads(uint32_t create_disposition
)
1939 switch (create_disposition
) {
1940 case FILE_SUPERSEDE
:
1941 case FILE_OVERWRITE_IF
:
1942 case FILE_OVERWRITE
:
1951 static int disposition_to_open_flags(uint32_t create_disposition
)
1956 * Currently we're using FILE_SUPERSEDE as the same as
1957 * FILE_OVERWRITE_IF but they really are
1958 * different. FILE_SUPERSEDE deletes an existing file
1959 * (requiring delete access) then recreates it.
1962 switch (create_disposition
) {
1963 case FILE_SUPERSEDE
:
1964 case FILE_OVERWRITE_IF
:
1966 * If file exists replace/overwrite. If file doesn't
1969 ret
= O_CREAT
|O_TRUNC
;
1974 * If file exists open. If file doesn't exist error.
1979 case FILE_OVERWRITE
:
1981 * If file exists overwrite. If file doesn't exist
1989 * If file exists error. If file doesn't exist create.
1991 ret
= O_CREAT
|O_EXCL
;
1996 * If file exists open. If file doesn't exist create.
2004 static int calculate_open_access_flags(uint32_t access_mask
,
2006 uint32_t private_flags
)
2008 bool need_write
, need_read
;
2011 * Note that we ignore the append flag as append does not
2012 * mean the same thing under DOS and Unix.
2015 need_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
));
2020 /* DENY_DOS opens are always underlying read-write on the
2021 file handle, no matter what the requested access mask
2025 ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
2026 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|
2027 FILE_READ_EA
|FILE_EXECUTE
));
2035 /****************************************************************************
2036 Open a file with a share mode. Passed in an already created files_struct *.
2037 ****************************************************************************/
2039 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
2040 struct smb_request
*req
,
2041 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
2042 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
2043 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
2044 uint32 create_options
, /* options such as delete on close. */
2045 uint32 new_dos_attributes
, /* attributes used for new file. */
2046 int oplock_request
, /* internal Samba oplock codes. */
2047 /* Information (FILE_EXISTS etc.) */
2048 uint32_t private_flags
, /* Samba specific flags. */
2052 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
2055 bool file_existed
= VALID_STAT(smb_fname
->st
);
2056 bool def_acl
= False
;
2057 bool posix_open
= False
;
2058 bool new_file_created
= False
;
2059 bool first_open_attempt
= true;
2060 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
2061 mode_t new_unx_mode
= (mode_t
)0;
2062 mode_t unx_mode
= (mode_t
)0;
2064 uint32 existing_dos_attributes
= 0;
2065 struct timeval request_time
= timeval_zero();
2066 struct share_mode_lock
*lck
= NULL
;
2067 uint32 open_access_mask
= access_mask
;
2070 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
2071 struct timespec old_write_time
;
2074 if (conn
->printer
) {
2076 * Printers are handled completely differently.
2077 * Most of the passed parameters are ignored.
2081 *pinfo
= FILE_WAS_CREATED
;
2084 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2085 smb_fname_str_dbg(smb_fname
)));
2088 DEBUG(0,("open_file_ntcreate: printer open without "
2089 "an SMB request!\n"));
2090 return NT_STATUS_INTERNAL_ERROR
;
2093 return print_spool_open(fsp
, smb_fname
->base_name
,
2097 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
2099 return NT_STATUS_NO_MEMORY
;
2102 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2104 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2105 new_dos_attributes
= 0;
2107 /* Windows allows a new file to be created and
2108 silently removes a FILE_ATTRIBUTE_DIRECTORY
2109 sent by the client. Do the same. */
2111 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
2113 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2115 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2116 smb_fname
, parent_dir
);
2119 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2120 "access_mask=0x%x share_access=0x%x "
2121 "create_disposition = 0x%x create_options=0x%x "
2122 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2123 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
2124 access_mask
, share_access
, create_disposition
,
2125 create_options
, (unsigned int)unx_mode
, oplock_request
,
2126 (unsigned int)private_flags
));
2128 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
2129 DEBUG(0, ("No smb request but not an internal only open!\n"));
2130 return NT_STATUS_INTERNAL_ERROR
;
2134 * Only non-internal opens can be deferred at all
2139 if (get_deferred_open_message_state(req
,
2142 /* Remember the absolute time of the original
2143 request with this mid. We'll use it later to
2144 see if this has timed out. */
2146 /* If it was an async create retry, the file
2149 if (is_deferred_open_async(ptr
)) {
2150 SET_STAT_INVALID(smb_fname
->st
);
2151 file_existed
= false;
2154 /* Ensure we don't reprocess this message. */
2155 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
2157 first_open_attempt
= false;
2162 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
2164 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
2168 /* ignore any oplock requests if oplocks are disabled */
2169 if (!lp_oplocks(SNUM(conn
)) ||
2170 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
2171 /* Mask off everything except the private Samba bits. */
2172 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
2175 /* this is for OS/2 long file names - say we don't support them */
2176 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
2177 /* OS/2 Workplace shell fix may be main code stream in a later
2179 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2181 if (use_nt_status()) {
2182 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2184 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
2187 switch( create_disposition
) {
2189 /* If file exists open. If file doesn't exist error. */
2190 if (!file_existed
) {
2191 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2192 "requested for file %s and file "
2194 smb_fname_str_dbg(smb_fname
)));
2196 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2200 case FILE_OVERWRITE
:
2201 /* If file exists overwrite. If file doesn't exist
2203 if (!file_existed
) {
2204 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2205 "requested for file %s and file "
2207 smb_fname_str_dbg(smb_fname
) ));
2209 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2214 /* If file exists error. If file doesn't exist
2217 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2218 "requested for file %s and file "
2219 "already exists.\n",
2220 smb_fname_str_dbg(smb_fname
)));
2221 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2226 return map_nt_error_from_unix(errno
);
2230 case FILE_SUPERSEDE
:
2231 case FILE_OVERWRITE_IF
:
2235 return NT_STATUS_INVALID_PARAMETER
;
2238 flags2
= disposition_to_open_flags(create_disposition
);
2240 /* We only care about matching attributes on file exists and
2243 if (!posix_open
&& file_existed
&&
2244 ((create_disposition
== FILE_OVERWRITE
) ||
2245 (create_disposition
== FILE_OVERWRITE_IF
))) {
2246 if (!open_match_attributes(conn
, existing_dos_attributes
,
2248 smb_fname
->st
.st_ex_mode
,
2249 unx_mode
, &new_unx_mode
)) {
2250 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2251 "for file %s (%x %x) (0%o, 0%o)\n",
2252 smb_fname_str_dbg(smb_fname
),
2253 existing_dos_attributes
,
2255 (unsigned int)smb_fname
->st
.st_ex_mode
,
2256 (unsigned int)unx_mode
));
2258 return NT_STATUS_ACCESS_DENIED
;
2262 status
= smbd_calculate_access_mask(conn
, smb_fname
,
2266 if (!NT_STATUS_IS_OK(status
)) {
2267 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2268 "on file %s returned %s\n",
2269 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
2273 open_access_mask
= access_mask
;
2275 if (flags2
& O_TRUNC
) {
2276 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
2279 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2280 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
2284 * Note that we ignore the append flag as append does not
2285 * mean the same thing under DOS and Unix.
2288 flags
= calculate_open_access_flags(access_mask
, oplock_request
,
2292 * Currently we only look at FILE_WRITE_THROUGH for create options.
2296 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
2301 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
2305 if (!posix_open
&& !CAN_WRITE(conn
)) {
2307 * We should really return a permission denied error if either
2308 * O_CREAT or O_TRUNC are set, but for compatibility with
2309 * older versions of Samba we just AND them out.
2311 flags2
&= ~(O_CREAT
|O_TRUNC
);
2314 if (first_open_attempt
&& lp_kernel_oplocks(SNUM(conn
))) {
2316 * With kernel oplocks the open breaking an oplock
2317 * blocks until the oplock holder has given up the
2318 * oplock or closed the file. We prevent this by first
2319 * trying to open the file with O_NONBLOCK (see "man
2320 * fcntl" on Linux). For the second try, triggered by
2321 * an oplock break response, we do not need this
2324 * This is true under the assumption that only Samba
2325 * requests kernel oplocks. Once someone else like
2326 * NFSv4 starts to use that API, we will have to
2327 * modify this by communicating with the NFSv4 server.
2329 flags2
|= O_NONBLOCK
;
2333 * Ensure we can't write on a read-only share or file.
2336 if (flags
!= O_RDONLY
&& file_existed
&&
2337 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
2338 DEBUG(5,("open_file_ntcreate: write access requested for "
2339 "file %s on read only %s\n",
2340 smb_fname_str_dbg(smb_fname
),
2341 !CAN_WRITE(conn
) ? "share" : "file" ));
2343 return NT_STATUS_ACCESS_DENIED
;
2346 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2347 fsp
->share_access
= share_access
;
2348 fsp
->fh
->private_options
= private_flags
;
2349 fsp
->access_mask
= open_access_mask
; /* We change this to the
2350 * requested access_mask after
2351 * the open is done. */
2352 fsp
->posix_open
= posix_open
;
2354 /* Ensure no SAMBA_PRIVATE bits can be set. */
2355 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
2357 if (timeval_is_zero(&request_time
)) {
2358 request_time
= fsp
->open_time
;
2362 * Ensure we pay attention to default ACLs on directories if required.
2365 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2366 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2367 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2370 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2371 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2372 (unsigned int)flags
, (unsigned int)flags2
,
2373 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2374 (unsigned int)open_access_mask
));
2376 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2377 flags
|flags2
, unx_mode
, access_mask
,
2378 open_access_mask
, &new_file_created
);
2380 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_NETWORK_BUSY
)) {
2381 struct deferred_open_record state
;
2384 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2386 if (file_existed
&& S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
)) {
2387 DEBUG(10, ("FIFO busy\n"));
2388 return NT_STATUS_NETWORK_BUSY
;
2391 DEBUG(10, ("Internal open busy\n"));
2392 return NT_STATUS_NETWORK_BUSY
;
2396 * From here on we assume this is an oplock break triggered
2399 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
2401 state
.delayed_for_oplocks
= false;
2402 state
.async_open
= false;
2403 state
.id
= fsp
->file_id
;
2404 defer_open(NULL
, request_time
, timeval_set(0, 0),
2406 DEBUG(10, ("No share mode lock found after "
2407 "EWOULDBLOCK, retrying sync\n"));
2408 return NT_STATUS_SHARING_VIOLATION
;
2411 if (!validate_oplock_types(lck
)) {
2412 smb_panic("validate_oplock_types failed");
2415 if (delay_for_oplock(fsp
, 0, lck
, false, create_disposition
)) {
2416 schedule_defer_open(lck
, fsp
->file_id
, request_time
, req
);
2418 DEBUG(10, ("Sent oplock break request to kernel "
2419 "oplock holder\n"));
2420 return NT_STATUS_SHARING_VIOLATION
;
2424 * No oplock from Samba around. Immediately retry with
2427 state
.delayed_for_oplocks
= false;
2428 state
.async_open
= false;
2429 state
.id
= fsp
->file_id
;
2430 defer_open(lck
, request_time
, timeval_set(0, 0), req
, &state
);
2432 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2433 "Retrying sync\n"));
2434 return NT_STATUS_SHARING_VIOLATION
;
2437 if (!NT_STATUS_IS_OK(fsp_open
)) {
2438 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
2439 schedule_async_open(request_time
, req
);
2444 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
2446 * The file did exist, but some other (local or NFS)
2447 * process either renamed/unlinked and re-created the
2448 * file with different dev/ino after we walked the path,
2449 * but before we did the open. We could retry the
2450 * open but it's a rare enough case it's easier to
2451 * just fail the open to prevent creating any problems
2452 * in the open file db having the wrong dev/ino key.
2455 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2456 "Old (dev=0x%llu, ino =0x%llu). "
2457 "New (dev=0x%llu, ino=0x%llu). Failing open "
2458 " with NT_STATUS_ACCESS_DENIED.\n",
2459 smb_fname_str_dbg(smb_fname
),
2460 (unsigned long long)saved_stat
.st_ex_dev
,
2461 (unsigned long long)saved_stat
.st_ex_ino
,
2462 (unsigned long long)smb_fname
->st
.st_ex_dev
,
2463 (unsigned long long)smb_fname
->st
.st_ex_ino
));
2464 return NT_STATUS_ACCESS_DENIED
;
2467 old_write_time
= smb_fname
->st
.st_ex_mtime
;
2470 * Deal with the race condition where two smbd's detect the
2471 * file doesn't exist and do the create at the same time. One
2472 * of them will win and set a share mode, the other (ie. this
2473 * one) should check if the requested share mode for this
2474 * create is allowed.
2478 * Now the file exists and fsp is successfully opened,
2479 * fsp->dev and fsp->inode are valid and should replace the
2480 * dev=0,inode=0 from a non existent file. Spotted by
2481 * Nadav Danieli <nadavd@exanet.com>. JRA.
2486 lck
= get_share_mode_lock(talloc_tos(), id
,
2488 smb_fname
, &old_write_time
);
2491 DEBUG(0, ("open_file_ntcreate: Could not get share "
2492 "mode lock for %s\n",
2493 smb_fname_str_dbg(smb_fname
)));
2495 return NT_STATUS_SHARING_VIOLATION
;
2498 /* Get the types we need to examine. */
2499 if (!validate_oplock_types(lck
)) {
2500 smb_panic("validate_oplock_types failed");
2503 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
2506 return NT_STATUS_DELETE_PENDING
;
2509 status
= open_mode_check(conn
, lck
,
2510 access_mask
, share_access
);
2512 if (NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
) ||
2513 (lck
->data
->num_share_modes
> 0)) {
2515 * This comes from ancient times out of open_mode_check. I
2516 * have no clue whether this is still necessary. I can't think
2517 * of a case where this would actually matter further down in
2518 * this function. I leave it here for further investigation
2521 file_existed
= true;
2524 if ((req
!= NULL
) &&
2526 fsp
, oplock_request
, lck
,
2527 NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
),
2528 create_disposition
)) {
2529 schedule_defer_open(lck
, fsp
->file_id
, request_time
, req
);
2532 return NT_STATUS_SHARING_VIOLATION
;
2535 if (!NT_STATUS_IS_OK(status
)) {
2536 uint32 can_access_mask
;
2537 bool can_access
= True
;
2539 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2541 /* Check if this can be done with the deny_dos and fcb
2544 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2545 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2547 DEBUG(0, ("DOS open without an SMB "
2551 return NT_STATUS_INTERNAL_ERROR
;
2554 /* Use the client requested access mask here,
2555 * not the one we open with. */
2556 status
= fcb_or_dos_open(req
,
2567 if (NT_STATUS_IS_OK(status
)) {
2570 *pinfo
= FILE_WAS_OPENED
;
2572 return NT_STATUS_OK
;
2577 * This next line is a subtlety we need for
2578 * MS-Access. If a file open will fail due to share
2579 * permissions and also for security (access) reasons,
2580 * we need to return the access failed error, not the
2581 * share error. We can't open the file due to kernel
2582 * oplock deadlock (it's possible we failed above on
2583 * the open_mode_check()) so use a userspace check.
2586 if (flags
& O_RDWR
) {
2587 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2588 } else if (flags
& O_WRONLY
) {
2589 can_access_mask
= FILE_WRITE_DATA
;
2591 can_access_mask
= FILE_READ_DATA
;
2594 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2595 !CAN_WRITE(conn
)) ||
2596 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
2599 can_access_mask
))) {
2604 * If we're returning a share violation, ensure we
2605 * cope with the braindead 1 second delay (SMB1 only).
2608 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2609 !conn
->sconn
->using_smb2
&&
2610 lp_defer_sharing_violations()) {
2611 struct timeval timeout
;
2612 struct deferred_open_record state
;
2615 /* this is a hack to speed up torture tests
2617 timeout_usecs
= lp_parm_int(SNUM(conn
),
2618 "smbd","sharedelay",
2619 SHARING_VIOLATION_USEC_WAIT
);
2621 /* This is a relative time, added to the absolute
2622 request_time value to get the absolute timeout time.
2623 Note that if this is the second or greater time we enter
2624 this codepath for this particular request mid then
2625 request_time is left as the absolute time of the *first*
2626 time this request mid was processed. This is what allows
2627 the request to eventually time out. */
2629 timeout
= timeval_set(0, timeout_usecs
);
2631 /* Nothing actually uses state.delayed_for_oplocks
2632 but it's handy to differentiate in debug messages
2633 between a 30 second delay due to oplock break, and
2634 a 1 second delay for share mode conflicts. */
2636 state
.delayed_for_oplocks
= False
;
2637 state
.async_open
= false;
2641 && !request_timed_out(request_time
,
2643 defer_open(lck
, request_time
, timeout
,
2652 * We have detected a sharing violation here
2653 * so return the correct error code
2655 status
= NT_STATUS_SHARING_VIOLATION
;
2657 status
= NT_STATUS_ACCESS_DENIED
;
2662 grant_fsp_oplock_type(fsp
, lck
, oplock_request
);
2665 * We have the share entry *locked*.....
2668 /* Delete streams if create_disposition requires it */
2669 if (!new_file_created
&& clear_ads(create_disposition
) &&
2670 !is_ntfs_stream_smb_fname(smb_fname
)) {
2671 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2672 if (!NT_STATUS_IS_OK(status
)) {
2679 /* note that we ignore failure for the following. It is
2680 basically a hack for NFS, and NFS will never set one of
2681 these only read them. Nobody but Samba can ever set a deny
2682 mode and we have already checked our more authoritative
2683 locking database for permission to set this deny mode. If
2684 the kernel refuses the operations then the kernel is wrong.
2685 note that GPFS supports it as well - jmcd */
2687 if (fsp
->fh
->fd
!= -1 && lp_kernel_share_modes(SNUM(conn
))) {
2689 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2690 if(ret_flock
== -1 ){
2695 return NT_STATUS_SHARING_VIOLATION
;
2700 * At this point onwards, we can guarantee that the share entry
2701 * is locked, whether we created the file or not, and that the
2702 * deny mode is compatible with all current opens.
2706 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2707 * but we don't have to store this - just ignore it on access check.
2709 if (conn
->sconn
->using_smb2
) {
2711 * SMB2 doesn't return it (according to Microsoft tests).
2712 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2713 * File created with access = 0x7 (Read, Write, Delete)
2714 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2716 fsp
->access_mask
= access_mask
;
2718 /* But SMB1 does. */
2719 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2723 /* stat opens on existing files don't get oplocks. */
2724 if (is_stat_open(open_access_mask
)) {
2725 fsp
->oplock_type
= NO_OPLOCK
;
2729 if (new_file_created
) {
2730 info
= FILE_WAS_CREATED
;
2732 if (flags2
& O_TRUNC
) {
2733 info
= FILE_WAS_OVERWRITTEN
;
2735 info
= FILE_WAS_OPENED
;
2744 * Setup the oplock info in both the shared memory and
2748 status
= set_file_oplock(fsp
);
2749 if (!NT_STATUS_IS_OK(status
)) {
2751 * Could not get the kernel oplock
2753 fsp
->oplock_type
= NO_OPLOCK
;
2756 if (!set_share_mode(lck
, fsp
, get_current_uid(conn
),
2758 fsp
->oplock_type
)) {
2761 return NT_STATUS_NO_MEMORY
;
2764 /* Handle strange delete on close create semantics. */
2765 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2767 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2769 if (!NT_STATUS_IS_OK(status
)) {
2770 /* Remember to delete the mode we just added. */
2771 del_share_mode(lck
, fsp
);
2776 /* Note that here we set the *inital* delete on close flag,
2777 not the regular one. The magic gets handled in close. */
2778 fsp
->initial_delete_on_close
= True
;
2781 if (info
!= FILE_WAS_OPENED
) {
2782 /* Files should be initially set as archive */
2783 if (lp_map_archive(SNUM(conn
)) ||
2784 lp_store_dos_attributes(SNUM(conn
))) {
2786 if (file_set_dosmode(conn
, smb_fname
,
2787 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2788 parent_dir
, true) == 0) {
2789 unx_mode
= smb_fname
->st
.st_ex_mode
;
2795 /* Determine sparse flag. */
2797 /* POSIX opens are sparse by default. */
2798 fsp
->is_sparse
= true;
2800 fsp
->is_sparse
= (file_existed
&&
2801 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2805 * Take care of inherited ACLs on created files - if default ACL not
2809 if (!posix_open
&& new_file_created
&& !def_acl
) {
2811 int saved_errno
= errno
; /* We might get ENOSYS in the next
2814 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2816 errno
= saved_errno
; /* Ignore ENOSYS */
2819 } else if (new_unx_mode
) {
2823 /* Attributes need changing. File already existed. */
2826 int saved_errno
= errno
; /* We might get ENOSYS in the
2828 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2830 if (ret
== -1 && errno
== ENOSYS
) {
2831 errno
= saved_errno
; /* Ignore ENOSYS */
2833 DEBUG(5, ("open_file_ntcreate: reset "
2834 "attributes of file %s to 0%o\n",
2835 smb_fname_str_dbg(smb_fname
),
2836 (unsigned int)new_unx_mode
));
2837 ret
= 0; /* Don't do the fchmod below. */
2842 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2843 DEBUG(5, ("open_file_ntcreate: failed to reset "
2844 "attributes of file %s to 0%o\n",
2845 smb_fname_str_dbg(smb_fname
),
2846 (unsigned int)new_unx_mode
));
2851 * Deal with other opens having a modified write time.
2853 struct timespec write_time
= get_share_mode_write_time(lck
);
2855 if (!null_timespec(write_time
)) {
2856 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
2862 return NT_STATUS_OK
;
2866 /****************************************************************************
2867 Open a file for for write to ensure that we can fchmod it.
2868 ****************************************************************************/
2870 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2871 struct smb_filename
*smb_fname
,
2872 files_struct
**result
)
2874 if (!VALID_STAT(smb_fname
->st
)) {
2875 return NT_STATUS_INVALID_PARAMETER
;
2878 return SMB_VFS_CREATE_FILE(
2881 0, /* root_dir_fid */
2882 smb_fname
, /* fname */
2883 FILE_WRITE_DATA
, /* access_mask */
2884 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2886 FILE_OPEN
, /* create_disposition*/
2887 0, /* create_options */
2888 0, /* file_attributes */
2889 INTERNAL_OPEN_ONLY
, /* oplock_request */
2890 0, /* allocation_size */
2891 0, /* private_flags */
2894 result
, /* result */
2898 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2899 struct smb_filename
*smb_dname
,
2900 uint32 file_attributes
)
2903 char *parent_dir
= NULL
;
2905 bool posix_open
= false;
2906 bool need_re_stat
= false;
2907 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2909 if (!CAN_WRITE(conn
) || (access_mask
& ~(conn
->share_access
))) {
2910 DEBUG(5,("mkdir_internal: failing share access "
2911 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
2912 return NT_STATUS_ACCESS_DENIED
;
2915 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2917 return NT_STATUS_NO_MEMORY
;
2920 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2922 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2924 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2927 status
= check_parent_access(conn
,
2930 if(!NT_STATUS_IS_OK(status
)) {
2931 DEBUG(5,("mkdir_internal: check_parent_access "
2932 "on directory %s for path %s returned %s\n",
2934 smb_dname
->base_name
,
2935 nt_errstr(status
) ));
2939 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2940 return map_nt_error_from_unix(errno
);
2943 /* Ensure we're checking for a symlink here.... */
2944 /* We don't want to get caught by a symlink racer. */
2946 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2947 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2948 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2949 return map_nt_error_from_unix(errno
);
2952 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2953 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2954 smb_fname_str_dbg(smb_dname
)));
2955 return NT_STATUS_NOT_A_DIRECTORY
;
2958 if (lp_store_dos_attributes(SNUM(conn
))) {
2960 file_set_dosmode(conn
, smb_dname
,
2961 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2966 if (lp_inherit_permissions(SNUM(conn
))) {
2967 inherit_access_posix_acl(conn
, parent_dir
,
2968 smb_dname
->base_name
, mode
);
2969 need_re_stat
= true;
2974 * Check if high bits should have been set,
2975 * then (if bits are missing): add them.
2976 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2979 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2980 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2981 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2982 (smb_dname
->st
.st_ex_mode
|
2983 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2984 need_re_stat
= true;
2988 /* Change the owner if required. */
2989 if (lp_inherit_owner(SNUM(conn
))) {
2990 change_dir_owner_to_parent(conn
, parent_dir
,
2991 smb_dname
->base_name
,
2993 need_re_stat
= true;
2997 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2998 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2999 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3000 return map_nt_error_from_unix(errno
);
3004 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
3005 smb_dname
->base_name
);
3007 return NT_STATUS_OK
;
3010 /****************************************************************************
3011 Open a directory from an NT SMB call.
3012 ****************************************************************************/
3014 static NTSTATUS
open_directory(connection_struct
*conn
,
3015 struct smb_request
*req
,
3016 struct smb_filename
*smb_dname
,
3018 uint32 share_access
,
3019 uint32 create_disposition
,
3020 uint32 create_options
,
3021 uint32 file_attributes
,
3023 files_struct
**result
)
3025 files_struct
*fsp
= NULL
;
3026 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
3027 struct share_mode_lock
*lck
= NULL
;
3029 struct timespec mtimespec
;
3032 if (is_ntfs_stream_smb_fname(smb_dname
)) {
3033 DEBUG(2, ("open_directory: %s is a stream name!\n",
3034 smb_fname_str_dbg(smb_dname
)));
3035 return NT_STATUS_NOT_A_DIRECTORY
;
3038 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
3039 /* Ensure we have a directory attribute. */
3040 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3043 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3044 "share_access = 0x%x create_options = 0x%x, "
3045 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3046 smb_fname_str_dbg(smb_dname
),
3047 (unsigned int)access_mask
,
3048 (unsigned int)share_access
,
3049 (unsigned int)create_options
,
3050 (unsigned int)create_disposition
,
3051 (unsigned int)file_attributes
));
3053 status
= smbd_calculate_access_mask(conn
, smb_dname
, false,
3054 access_mask
, &access_mask
);
3055 if (!NT_STATUS_IS_OK(status
)) {
3056 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3057 "on file %s returned %s\n",
3058 smb_fname_str_dbg(smb_dname
),
3059 nt_errstr(status
)));
3063 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3064 !security_token_has_privilege(get_current_nttok(conn
),
3065 SEC_PRIV_SECURITY
)) {
3066 DEBUG(10, ("open_directory: open on %s "
3067 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3068 smb_fname_str_dbg(smb_dname
)));
3069 return NT_STATUS_PRIVILEGE_NOT_HELD
;
3072 switch( create_disposition
) {
3076 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3079 info
= FILE_WAS_OPENED
;
3084 /* If directory exists error. If directory doesn't
3088 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
3089 DEBUG(2, ("open_directory: unable to create "
3090 "%s. Error was %s\n",
3091 smb_fname_str_dbg(smb_dname
),
3092 nt_errstr(status
)));
3096 status
= mkdir_internal(conn
, smb_dname
,
3099 if (!NT_STATUS_IS_OK(status
)) {
3100 DEBUG(2, ("open_directory: unable to create "
3101 "%s. Error was %s\n",
3102 smb_fname_str_dbg(smb_dname
),
3103 nt_errstr(status
)));
3107 info
= FILE_WAS_CREATED
;
3112 * If directory exists open. If directory doesn't
3117 status
= NT_STATUS_OK
;
3118 info
= FILE_WAS_OPENED
;
3120 status
= mkdir_internal(conn
, smb_dname
,
3123 if (NT_STATUS_IS_OK(status
)) {
3124 info
= FILE_WAS_CREATED
;
3126 /* Cope with create race. */
3127 if (!NT_STATUS_EQUAL(status
,
3128 NT_STATUS_OBJECT_NAME_COLLISION
)) {
3129 DEBUG(2, ("open_directory: unable to create "
3130 "%s. Error was %s\n",
3131 smb_fname_str_dbg(smb_dname
),
3132 nt_errstr(status
)));
3135 info
= FILE_WAS_OPENED
;
3141 case FILE_SUPERSEDE
:
3142 case FILE_OVERWRITE
:
3143 case FILE_OVERWRITE_IF
:
3145 DEBUG(5,("open_directory: invalid create_disposition "
3146 "0x%x for directory %s\n",
3147 (unsigned int)create_disposition
,
3148 smb_fname_str_dbg(smb_dname
)));
3149 return NT_STATUS_INVALID_PARAMETER
;
3152 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3153 DEBUG(5,("open_directory: %s is not a directory !\n",
3154 smb_fname_str_dbg(smb_dname
)));
3155 return NT_STATUS_NOT_A_DIRECTORY
;
3158 if (info
== FILE_WAS_OPENED
) {
3159 status
= smbd_check_access_rights(conn
,
3163 if (!NT_STATUS_IS_OK(status
)) {
3164 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3165 "file %s failed with %s\n",
3166 smb_fname_str_dbg(smb_dname
),
3167 nt_errstr(status
)));
3172 status
= file_new(req
, conn
, &fsp
);
3173 if(!NT_STATUS_IS_OK(status
)) {
3178 * Setup the files_struct for it.
3181 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
3182 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
3183 fsp
->file_pid
= req
? req
->smbpid
: 0;
3184 fsp
->can_lock
= False
;
3185 fsp
->can_read
= False
;
3186 fsp
->can_write
= False
;
3188 fsp
->share_access
= share_access
;
3189 fsp
->fh
->private_options
= 0;
3191 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3193 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3194 fsp
->print_file
= NULL
;
3195 fsp
->modified
= False
;
3196 fsp
->oplock_type
= NO_OPLOCK
;
3197 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
3198 fsp
->is_directory
= True
;
3199 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
3200 status
= fsp_set_smb_fname(fsp
, smb_dname
);
3201 if (!NT_STATUS_IS_OK(status
)) {
3202 file_free(req
, fsp
);
3206 /* Don't store old timestamps for directory
3207 handles in the internal database. We don't
3208 update them in there if new objects
3209 are creaded in the directory. Currently
3210 we only update timestamps on file writes.
3213 ZERO_STRUCT(mtimespec
);
3215 if (access_mask
& (FILE_LIST_DIRECTORY
|
3217 FILE_ADD_SUBDIRECTORY
|
3220 FILE_DELETE_CHILD
)) {
3222 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
3224 /* POSIX allows us to open a directory with O_RDONLY. */
3225 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
3227 if (!NT_STATUS_IS_OK(status
)) {
3228 DEBUG(5, ("open_directory: Could not open fd for "
3230 smb_fname_str_dbg(smb_dname
),
3231 nt_errstr(status
)));
3232 file_free(req
, fsp
);
3237 DEBUG(10, ("Not opening Directory %s\n",
3238 smb_fname_str_dbg(smb_dname
)));
3241 status
= vfs_stat_fsp(fsp
);
3242 if (!NT_STATUS_IS_OK(status
)) {
3244 file_free(req
, fsp
);
3248 /* Ensure there was no race condition. */
3249 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
3250 DEBUG(5,("open_directory: stat struct differs for "
3252 smb_fname_str_dbg(smb_dname
)));
3254 file_free(req
, fsp
);
3255 return NT_STATUS_ACCESS_DENIED
;
3258 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
3259 conn
->connectpath
, smb_dname
,
3263 DEBUG(0, ("open_directory: Could not get share mode lock for "
3264 "%s\n", smb_fname_str_dbg(smb_dname
)));
3266 file_free(req
, fsp
);
3267 return NT_STATUS_SHARING_VIOLATION
;
3270 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
3273 file_free(req
, fsp
);
3274 return NT_STATUS_DELETE_PENDING
;
3277 status
= open_mode_check(conn
, lck
,
3278 access_mask
, share_access
);
3280 if (!NT_STATUS_IS_OK(status
)) {
3283 file_free(req
, fsp
);
3287 if (!set_share_mode(lck
, fsp
, get_current_uid(conn
),
3288 req
? req
->mid
: 0, NO_OPLOCK
)) {
3291 file_free(req
, fsp
);
3292 return NT_STATUS_NO_MEMORY
;
3295 /* For directories the delete on close bit at open time seems
3296 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3297 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3298 status
= can_set_delete_on_close(fsp
, 0);
3299 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
3300 del_share_mode(lck
, fsp
);
3303 file_free(req
, fsp
);
3307 if (NT_STATUS_IS_OK(status
)) {
3308 /* Note that here we set the *inital* delete on close flag,
3309 not the regular one. The magic gets handled in close. */
3310 fsp
->initial_delete_on_close
= True
;
3316 * Deal with other opens having a modified write time. Is this
3317 * possible for directories?
3319 struct timespec write_time
= get_share_mode_write_time(lck
);
3321 if (!null_timespec(write_time
)) {
3322 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
3333 return NT_STATUS_OK
;
3336 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
3337 struct smb_filename
*smb_dname
)
3342 status
= SMB_VFS_CREATE_FILE(
3345 0, /* root_dir_fid */
3346 smb_dname
, /* fname */
3347 FILE_READ_ATTRIBUTES
, /* access_mask */
3348 FILE_SHARE_NONE
, /* share_access */
3349 FILE_CREATE
, /* create_disposition*/
3350 FILE_DIRECTORY_FILE
, /* create_options */
3351 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
3352 0, /* oplock_request */
3353 0, /* allocation_size */
3354 0, /* private_flags */
3360 if (NT_STATUS_IS_OK(status
)) {
3361 close_file(req
, fsp
, NORMAL_CLOSE
);
3367 /****************************************************************************
3368 Receive notification that one of our open files has been renamed by another
3370 ****************************************************************************/
3372 void msg_file_was_renamed(struct messaging_context
*msg
,
3375 struct server_id server_id
,
3379 char *frm
= (char *)data
->data
;
3381 const char *sharepath
;
3382 const char *base_name
;
3383 const char *stream_name
;
3384 struct smb_filename
*smb_fname
= NULL
;
3385 size_t sp_len
, bn_len
;
3387 struct smbd_server_connection
*sconn
=
3388 talloc_get_type_abort(private_data
,
3389 struct smbd_server_connection
);
3391 if (data
->data
== NULL
3392 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3393 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3394 (int)data
->length
));
3398 /* Unpack the message. */
3399 pull_file_id_24(frm
, &id
);
3400 sharepath
= &frm
[24];
3401 sp_len
= strlen(sharepath
);
3402 base_name
= sharepath
+ sp_len
+ 1;
3403 bn_len
= strlen(base_name
);
3404 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3406 /* stream_name must always be NULL if there is no stream. */
3407 if (stream_name
[0] == '\0') {
3411 smb_fname
= synthetic_smb_fname(talloc_tos(), base_name
,
3413 if (smb_fname
== NULL
) {
3417 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3419 sharepath
, smb_fname_str_dbg(smb_fname
),
3420 file_id_string_tos(&id
)));
3422 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3423 fsp
= file_find_di_next(fsp
)) {
3424 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3426 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3427 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
3428 smb_fname_str_dbg(smb_fname
)));
3429 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3430 if (!NT_STATUS_IS_OK(status
)) {
3435 /* Now we have the complete path we can work out if this is
3436 actually within this share and adjust newname accordingly. */
3437 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3438 "not sharepath %s) "
3439 "%s from %s -> %s\n",
3440 fsp
->conn
->connectpath
,
3444 smb_fname_str_dbg(smb_fname
)));
3448 TALLOC_FREE(smb_fname
);
3453 * If a main file is opened for delete, all streams need to be checked for
3454 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3455 * If that works, delete them all by setting the delete on close and close.
3458 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3461 struct stream_struct
*stream_info
= NULL
;
3462 files_struct
**streams
= NULL
;
3464 unsigned int num_streams
= 0;
3465 TALLOC_CTX
*frame
= talloc_stackframe();
3468 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3469 &num_streams
, &stream_info
);
3471 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3472 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3473 DEBUG(10, ("no streams around\n"));
3475 return NT_STATUS_OK
;
3478 if (!NT_STATUS_IS_OK(status
)) {
3479 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3480 nt_errstr(status
)));
3484 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3487 if (num_streams
== 0) {
3489 return NT_STATUS_OK
;
3492 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3493 if (streams
== NULL
) {
3494 DEBUG(0, ("talloc failed\n"));
3495 status
= NT_STATUS_NO_MEMORY
;
3499 for (i
=0; i
<num_streams
; i
++) {
3500 struct smb_filename
*smb_fname
;
3502 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3507 smb_fname
= synthetic_smb_fname(
3508 talloc_tos(), fname
, stream_info
[i
].name
, NULL
);
3509 if (smb_fname
== NULL
) {
3510 status
= NT_STATUS_NO_MEMORY
;
3514 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3515 DEBUG(10, ("Unable to stat stream: %s\n",
3516 smb_fname_str_dbg(smb_fname
)));
3519 status
= SMB_VFS_CREATE_FILE(
3522 0, /* root_dir_fid */
3523 smb_fname
, /* fname */
3524 DELETE_ACCESS
, /* access_mask */
3525 (FILE_SHARE_READ
| /* share_access */
3526 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3527 FILE_OPEN
, /* create_disposition*/
3528 0, /* create_options */
3529 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3530 0, /* oplock_request */
3531 0, /* allocation_size */
3532 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3535 &streams
[i
], /* result */
3538 if (!NT_STATUS_IS_OK(status
)) {
3539 DEBUG(10, ("Could not open stream %s: %s\n",
3540 smb_fname_str_dbg(smb_fname
),
3541 nt_errstr(status
)));
3543 TALLOC_FREE(smb_fname
);
3546 TALLOC_FREE(smb_fname
);
3550 * don't touch the variable "status" beyond this point :-)
3553 for (i
-= 1 ; i
>= 0; i
--) {
3554 if (streams
[i
] == NULL
) {
3558 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3559 fsp_str_dbg(streams
[i
])));
3560 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3568 /*********************************************************************
3569 Create a default ACL by inheriting from the parent. If no inheritance
3570 from the parent available, don't set anything. This will leave the actual
3571 permissions the new file or directory already got from the filesystem
3572 as the NT ACL when read.
3573 *********************************************************************/
3575 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
3577 TALLOC_CTX
*frame
= talloc_stackframe();
3578 char *parent_name
= NULL
;
3579 struct security_descriptor
*parent_desc
= NULL
;
3580 NTSTATUS status
= NT_STATUS_OK
;
3581 struct security_descriptor
*psd
= NULL
;
3582 const struct dom_sid
*owner_sid
= NULL
;
3583 const struct dom_sid
*group_sid
= NULL
;
3584 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
3585 struct security_token
*token
= fsp
->conn
->session_info
->security_token
;
3586 bool inherit_owner
= lp_inherit_owner(SNUM(fsp
->conn
));
3587 bool inheritable_components
= false;
3588 bool try_builtin_administrators
= false;
3589 const struct dom_sid
*BA_U_sid
= NULL
;
3590 const struct dom_sid
*BA_G_sid
= NULL
;
3591 bool try_system
= false;
3592 const struct dom_sid
*SY_U_sid
= NULL
;
3593 const struct dom_sid
*SY_G_sid
= NULL
;
3596 if (!parent_dirname(frame
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
3598 return NT_STATUS_NO_MEMORY
;
3601 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
3603 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
3606 if (!NT_STATUS_IS_OK(status
)) {
3611 inheritable_components
= sd_has_inheritable_components(parent_desc
,
3614 if (!inheritable_components
&& !inherit_owner
) {
3616 /* Nothing to inherit and not setting owner. */
3617 return NT_STATUS_OK
;
3620 /* Create an inherited descriptor from the parent. */
3622 if (DEBUGLEVEL
>= 10) {
3623 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3624 fsp_str_dbg(fsp
) ));
3625 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
3628 /* Inherit from parent descriptor if "inherit owner" set. */
3629 if (inherit_owner
) {
3630 owner_sid
= parent_desc
->owner_sid
;
3631 group_sid
= parent_desc
->group_sid
;
3634 if (owner_sid
== NULL
) {
3635 if (security_token_has_builtin_administrators(token
)) {
3636 try_builtin_administrators
= true;
3637 } else if (security_token_is_system(token
)) {
3638 try_builtin_administrators
= true;
3643 if (group_sid
== NULL
&&
3644 token
->num_sids
== PRIMARY_GROUP_SID_INDEX
)
3646 if (security_token_is_system(token
)) {
3647 try_builtin_administrators
= true;
3652 if (try_builtin_administrators
) {
3657 ok
= sids_to_unixids(&global_sid_Builtin_Administrators
, 1, &ids
);
3661 BA_U_sid
= &global_sid_Builtin_Administrators
;
3662 BA_G_sid
= &global_sid_Builtin_Administrators
;
3665 BA_U_sid
= &global_sid_Builtin_Administrators
;
3668 BA_G_sid
= &global_sid_Builtin_Administrators
;
3681 ok
= sids_to_unixids(&global_sid_System
, 1, &ids
);
3685 SY_U_sid
= &global_sid_System
;
3686 SY_G_sid
= &global_sid_System
;
3689 SY_U_sid
= &global_sid_System
;
3692 SY_G_sid
= &global_sid_System
;
3700 if (owner_sid
== NULL
) {
3701 owner_sid
= BA_U_sid
;
3704 if (owner_sid
== NULL
) {
3705 owner_sid
= SY_U_sid
;
3708 if (group_sid
== NULL
) {
3709 group_sid
= SY_G_sid
;
3712 if (try_system
&& group_sid
== NULL
) {
3713 group_sid
= BA_G_sid
;
3716 if (owner_sid
== NULL
) {
3717 owner_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3719 if (group_sid
== NULL
) {
3720 if (token
->num_sids
== PRIMARY_GROUP_SID_INDEX
) {
3721 group_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3723 group_sid
= &token
->sids
[PRIMARY_GROUP_SID_INDEX
];
3727 status
= se_create_child_secdesc(frame
,
3734 if (!NT_STATUS_IS_OK(status
)) {
3739 /* If inheritable_components == false,
3740 se_create_child_secdesc()
3741 creates a security desriptor with a NULL dacl
3742 entry, but with SEC_DESC_DACL_PRESENT. We need
3743 to remove that flag. */
3745 if (!inheritable_components
) {
3746 security_info_sent
&= ~SECINFO_DACL
;
3747 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
3750 if (DEBUGLEVEL
>= 10) {
3751 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3752 fsp_str_dbg(fsp
) ));
3753 NDR_PRINT_DEBUG(security_descriptor
, psd
);
3756 if (inherit_owner
) {
3757 /* We need to be root to force this. */
3760 status
= SMB_VFS_FSET_NT_ACL(fsp
,
3763 if (inherit_owner
) {
3771 * Wrapper around open_file_ntcreate and open_directory
3774 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3775 struct smb_request
*req
,
3776 struct smb_filename
*smb_fname
,
3777 uint32_t access_mask
,
3778 uint32_t share_access
,
3779 uint32_t create_disposition
,
3780 uint32_t create_options
,
3781 uint32_t file_attributes
,
3782 uint32_t oplock_request
,
3783 uint64_t allocation_size
,
3784 uint32_t private_flags
,
3785 struct security_descriptor
*sd
,
3786 struct ea_list
*ea_list
,
3788 files_struct
**result
,
3791 int info
= FILE_WAS_OPENED
;
3792 files_struct
*base_fsp
= NULL
;
3793 files_struct
*fsp
= NULL
;
3796 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3797 "file_attributes = 0x%x, share_access = 0x%x, "
3798 "create_disposition = 0x%x create_options = 0x%x "
3799 "oplock_request = 0x%x private_flags = 0x%x "
3800 "ea_list = 0x%p, sd = 0x%p, "
3802 (unsigned int)access_mask
,
3803 (unsigned int)file_attributes
,
3804 (unsigned int)share_access
,
3805 (unsigned int)create_disposition
,
3806 (unsigned int)create_options
,
3807 (unsigned int)oplock_request
,
3808 (unsigned int)private_flags
,
3809 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3811 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3812 status
= NT_STATUS_NOT_SUPPORTED
;
3816 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3817 status
= NT_STATUS_INVALID_PARAMETER
;
3822 oplock_request
|= INTERNAL_OPEN_ONLY
;
3825 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3826 && (access_mask
& DELETE_ACCESS
)
3827 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3829 * We can't open a file with DELETE access if any of the
3830 * streams is open without FILE_SHARE_DELETE
3832 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3834 if (!NT_STATUS_IS_OK(status
)) {
3839 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3840 !security_token_has_privilege(get_current_nttok(conn
),
3841 SEC_PRIV_SECURITY
)) {
3842 DEBUG(10, ("create_file_unixpath: open on %s "
3843 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3844 smb_fname_str_dbg(smb_fname
)));
3845 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3849 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3850 && is_ntfs_stream_smb_fname(smb_fname
)
3851 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3852 uint32 base_create_disposition
;
3853 struct smb_filename
*smb_fname_base
= NULL
;
3855 if (create_options
& FILE_DIRECTORY_FILE
) {
3856 status
= NT_STATUS_NOT_A_DIRECTORY
;
3860 switch (create_disposition
) {
3862 base_create_disposition
= FILE_OPEN
;
3865 base_create_disposition
= FILE_OPEN_IF
;
3869 /* Create an smb_filename with stream_name == NULL. */
3870 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
3871 smb_fname
->base_name
,
3873 if (smb_fname_base
== NULL
) {
3874 status
= NT_STATUS_NO_MEMORY
;
3878 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3879 DEBUG(10, ("Unable to stat stream: %s\n",
3880 smb_fname_str_dbg(smb_fname_base
)));
3883 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3884 * We need to check if the requested access mask
3885 * could be used to open the underlying file (if
3886 * it existed), as we're passing in zero for the
3887 * access mask to the base filename.
3889 status
= check_base_file_access(conn
,
3893 if (!NT_STATUS_IS_OK(status
)) {
3894 DEBUG(10, ("Permission check "
3895 "for base %s failed: "
3896 "%s\n", smb_fname
->base_name
,
3897 nt_errstr(status
)));
3902 /* Open the base file. */
3903 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3906 | FILE_SHARE_DELETE
,
3907 base_create_disposition
,
3908 0, 0, 0, 0, 0, NULL
, NULL
,
3910 TALLOC_FREE(smb_fname_base
);
3912 if (!NT_STATUS_IS_OK(status
)) {
3913 DEBUG(10, ("create_file_unixpath for base %s failed: "
3914 "%s\n", smb_fname
->base_name
,
3915 nt_errstr(status
)));
3918 /* we don't need to low level fd */
3923 * If it's a request for a directory open, deal with it separately.
3926 if (create_options
& FILE_DIRECTORY_FILE
) {
3928 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3929 status
= NT_STATUS_INVALID_PARAMETER
;
3933 /* Can't open a temp directory. IFS kit test. */
3934 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3935 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3936 status
= NT_STATUS_INVALID_PARAMETER
;
3941 * We will get a create directory here if the Win32
3942 * app specified a security descriptor in the
3943 * CreateDirectory() call.
3947 status
= open_directory(
3948 conn
, req
, smb_fname
, access_mask
, share_access
,
3949 create_disposition
, create_options
, file_attributes
,
3954 * Ordinary file case.
3957 status
= file_new(req
, conn
, &fsp
);
3958 if(!NT_STATUS_IS_OK(status
)) {
3962 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3963 if (!NT_STATUS_IS_OK(status
)) {
3969 * We're opening the stream element of a
3970 * base_fsp we already opened. Set up the
3973 fsp
->base_fsp
= base_fsp
;
3976 if (allocation_size
) {
3977 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
3981 status
= open_file_ntcreate(conn
,
3993 if(!NT_STATUS_IS_OK(status
)) {
3994 file_free(req
, fsp
);
3998 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
4000 /* A stream open never opens a directory */
4003 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4008 * Fail the open if it was explicitly a non-directory
4012 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
4013 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4018 status
= open_directory(
4019 conn
, req
, smb_fname
, access_mask
,
4020 share_access
, create_disposition
,
4021 create_options
, file_attributes
,
4026 if (!NT_STATUS_IS_OK(status
)) {
4030 fsp
->base_fsp
= base_fsp
;
4032 if ((ea_list
!= NULL
) &&
4033 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
4034 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
4035 if (!NT_STATUS_IS_OK(status
)) {
4040 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
4041 status
= NT_STATUS_ACCESS_DENIED
;
4045 /* Save the requested allocation size. */
4046 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
4048 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
4049 fsp
->initial_allocation_size
= smb_roundup(
4050 fsp
->conn
, allocation_size
);
4051 if (fsp
->is_directory
) {
4052 /* Can't set allocation size on a directory. */
4053 status
= NT_STATUS_ACCESS_DENIED
;
4056 if (vfs_allocate_file_space(
4057 fsp
, fsp
->initial_allocation_size
) == -1) {
4058 status
= NT_STATUS_DISK_FULL
;
4062 fsp
->initial_allocation_size
= smb_roundup(
4063 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
4066 fsp
->initial_allocation_size
= 0;
4069 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
4070 fsp
->base_fsp
== NULL
) {
4073 * According to the MS documentation, the only time the security
4074 * descriptor is applied to the opened file is iff we *created* the
4075 * file; an existing file stays the same.
4077 * Also, it seems (from observation) that you can open the file with
4078 * any access mask but you can still write the sd. We need to override
4079 * the granted access before we call set_sd
4080 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4083 uint32_t sec_info_sent
;
4084 uint32_t saved_access_mask
= fsp
->access_mask
;
4086 sec_info_sent
= get_sec_info(sd
);
4088 fsp
->access_mask
= FILE_GENERIC_ALL
;
4090 if (sec_info_sent
& (SECINFO_OWNER
|
4094 status
= set_sd(fsp
, sd
, sec_info_sent
);
4097 fsp
->access_mask
= saved_access_mask
;
4099 if (!NT_STATUS_IS_OK(status
)) {
4102 } else if (lp_inherit_acls(SNUM(conn
))) {
4103 /* Inherit from parent. Errors here are not fatal. */
4104 status
= inherit_new_acl(fsp
);
4105 if (!NT_STATUS_IS_OK(status
)) {
4106 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4108 nt_errstr(status
) ));
4113 if ((conn
->fs_capabilities
& FILE_FILE_COMPRESSION
)
4114 && (create_options
& FILE_NO_COMPRESSION
)
4115 && (info
== FILE_WAS_CREATED
)) {
4116 status
= SMB_VFS_SET_COMPRESSION(conn
, fsp
, fsp
,
4117 COMPRESSION_FORMAT_NONE
);
4118 if (!NT_STATUS_IS_OK(status
)) {
4119 DEBUG(1, ("failed to disable compression: %s\n",
4120 nt_errstr(status
)));
4124 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
4127 if (pinfo
!= NULL
) {
4131 smb_fname
->st
= fsp
->fsp_name
->st
;
4133 return NT_STATUS_OK
;
4136 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
4139 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
4141 * The close_file below will close
4146 close_file(req
, fsp
, ERROR_CLOSE
);
4149 if (base_fsp
!= NULL
) {
4150 close_file(req
, base_fsp
, ERROR_CLOSE
);
4157 * Calculate the full path name given a relative fid.
4159 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
4160 struct smb_request
*req
,
4161 uint16_t root_dir_fid
,
4162 const struct smb_filename
*smb_fname
,
4163 struct smb_filename
**smb_fname_out
)
4165 files_struct
*dir_fsp
;
4166 char *parent_fname
= NULL
;
4167 char *new_base_name
= NULL
;
4170 if (root_dir_fid
== 0 || !smb_fname
) {
4171 status
= NT_STATUS_INTERNAL_ERROR
;
4175 dir_fsp
= file_fsp(req
, root_dir_fid
);
4177 if (dir_fsp
== NULL
) {
4178 status
= NT_STATUS_INVALID_HANDLE
;
4182 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
4183 status
= NT_STATUS_INVALID_HANDLE
;
4187 if (!dir_fsp
->is_directory
) {
4190 * Check to see if this is a mac fork of some kind.
4193 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
4194 is_ntfs_stream_smb_fname(smb_fname
)) {
4195 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4200 we need to handle the case when we get a
4201 relative open relative to a file and the
4202 pathname is blank - this is a reopen!
4203 (hint from demyn plantenberg)
4206 status
= NT_STATUS_INVALID_HANDLE
;
4210 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
4212 * We're at the toplevel dir, the final file name
4213 * must not contain ./, as this is filtered out
4214 * normally by srvstr_get_path and unix_convert
4215 * explicitly rejects paths containing ./.
4217 parent_fname
= talloc_strdup(talloc_tos(), "");
4218 if (parent_fname
== NULL
) {
4219 status
= NT_STATUS_NO_MEMORY
;
4223 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
4226 * Copy in the base directory name.
4229 parent_fname
= talloc_array(talloc_tos(), char,
4231 if (parent_fname
== NULL
) {
4232 status
= NT_STATUS_NO_MEMORY
;
4235 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
4239 * Ensure it ends in a '/'.
4240 * We used TALLOC_SIZE +2 to add space for the '/'.
4244 && (parent_fname
[dir_name_len
-1] != '\\')
4245 && (parent_fname
[dir_name_len
-1] != '/')) {
4246 parent_fname
[dir_name_len
] = '/';
4247 parent_fname
[dir_name_len
+1] = '\0';
4251 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
4252 smb_fname
->base_name
);
4253 if (new_base_name
== NULL
) {
4254 status
= NT_STATUS_NO_MEMORY
;
4258 status
= filename_convert(req
,
4260 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
4265 if (!NT_STATUS_IS_OK(status
)) {
4270 TALLOC_FREE(parent_fname
);
4271 TALLOC_FREE(new_base_name
);
4275 NTSTATUS
create_file_default(connection_struct
*conn
,
4276 struct smb_request
*req
,
4277 uint16_t root_dir_fid
,
4278 struct smb_filename
*smb_fname
,
4279 uint32_t access_mask
,
4280 uint32_t share_access
,
4281 uint32_t create_disposition
,
4282 uint32_t create_options
,
4283 uint32_t file_attributes
,
4284 uint32_t oplock_request
,
4285 uint64_t allocation_size
,
4286 uint32_t private_flags
,
4287 struct security_descriptor
*sd
,
4288 struct ea_list
*ea_list
,
4289 files_struct
**result
,
4292 int info
= FILE_WAS_OPENED
;
4293 files_struct
*fsp
= NULL
;
4295 bool stream_name
= false;
4297 DEBUG(10,("create_file: access_mask = 0x%x "
4298 "file_attributes = 0x%x, share_access = 0x%x, "
4299 "create_disposition = 0x%x create_options = 0x%x "
4300 "oplock_request = 0x%x "
4301 "private_flags = 0x%x "
4302 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4304 (unsigned int)access_mask
,
4305 (unsigned int)file_attributes
,
4306 (unsigned int)share_access
,
4307 (unsigned int)create_disposition
,
4308 (unsigned int)create_options
,
4309 (unsigned int)oplock_request
,
4310 (unsigned int)private_flags
,
4311 (unsigned int)root_dir_fid
,
4312 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4315 * Calculate the filename from the root_dir_if if necessary.
4318 if (root_dir_fid
!= 0) {
4319 struct smb_filename
*smb_fname_out
= NULL
;
4320 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
4321 smb_fname
, &smb_fname_out
);
4322 if (!NT_STATUS_IS_OK(status
)) {
4325 smb_fname
= smb_fname_out
;
4329 * Check to see if this is a mac fork of some kind.
4332 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
4334 enum FAKE_FILE_TYPE fake_file_type
;
4336 fake_file_type
= is_fake_file(smb_fname
);
4338 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
4341 * Here we go! support for changing the disk quotas
4344 * We need to fake up to open this MAGIC QUOTA file
4345 * and return a valid FID.
4347 * w2k close this file directly after openening xp
4348 * also tries a QUERY_FILE_INFO on the file and then
4351 status
= open_fake_file(req
, conn
, req
->vuid
,
4352 fake_file_type
, smb_fname
,
4354 if (!NT_STATUS_IS_OK(status
)) {
4358 ZERO_STRUCT(smb_fname
->st
);
4362 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
4363 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4368 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
4370 smb_fname
->stream_name
= NULL
;
4371 /* We have to handle this error here. */
4372 if (create_options
& FILE_DIRECTORY_FILE
) {
4373 status
= NT_STATUS_NOT_A_DIRECTORY
;
4376 if (lp_posix_pathnames()) {
4377 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
4379 ret
= SMB_VFS_STAT(conn
, smb_fname
);
4382 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
4383 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4388 status
= create_file_unixpath(
4389 conn
, req
, smb_fname
, access_mask
, share_access
,
4390 create_disposition
, create_options
, file_attributes
,
4391 oplock_request
, allocation_size
, private_flags
,
4395 if (!NT_STATUS_IS_OK(status
)) {
4400 DEBUG(10, ("create_file: info=%d\n", info
));
4403 if (pinfo
!= NULL
) {
4406 return NT_STATUS_OK
;
4409 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
4412 close_file(req
, fsp
, ERROR_CLOSE
);