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
,
823 if (!NT_STATUS_IS_OK(status
)) {
824 DEBUG(10, ("open_file: "
825 "smbd_check_access_rights "
826 "on file %s returned %s\n",
827 smb_fname_str_dbg(smb_fname
),
831 if (!NT_STATUS_IS_OK(status
) &&
832 !NT_STATUS_EQUAL(status
,
833 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
838 if (NT_STATUS_EQUAL(status
,
839 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
841 DEBUG(10, ("open_file: "
842 "file %s vanished since we "
843 "checked for existence.\n",
844 smb_fname_str_dbg(smb_fname
)));
845 file_existed
= false;
846 SET_STAT_INVALID(fsp
->fsp_name
->st
);
851 if (!(local_flags
& O_CREAT
)) {
852 /* File didn't exist and no O_CREAT. */
853 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
856 status
= check_parent_access(conn
,
859 if (!NT_STATUS_IS_OK(status
)) {
860 DEBUG(10, ("open_file: "
861 "check_parent_access on "
862 "file %s returned %s\n",
863 smb_fname_str_dbg(smb_fname
),
864 nt_errstr(status
) ));
871 * Actually do the open - if O_TRUNC is needed handle it
872 * below under the share mode lock.
874 status
= fd_open_atomic(conn
, fsp
, local_flags
& ~O_TRUNC
,
875 unx_mode
, p_file_created
);
876 if (!NT_STATUS_IS_OK(status
)) {
877 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
878 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
879 nt_errstr(status
),local_flags
,flags
));
883 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
885 /* If we have an fd, this stat should succeed. */
886 DEBUG(0,("Error doing fstat on open file %s "
888 smb_fname_str_dbg(smb_fname
),
890 status
= map_nt_error_from_unix(errno
);
895 if (*p_file_created
) {
896 /* We created this file. */
898 bool need_re_stat
= false;
899 /* Do all inheritance work after we've
900 done a successful fstat call and filled
901 in the stat struct in fsp->fsp_name. */
903 /* Inherit the ACL if required */
904 if (lp_inherit_permissions(SNUM(conn
))) {
905 inherit_access_posix_acl(conn
, parent_dir
,
906 smb_fname
->base_name
,
911 /* Change the owner if required. */
912 if (lp_inherit_owner(SNUM(conn
))) {
913 change_file_owner_to_parent(conn
, parent_dir
,
919 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
920 /* If we have an fd, this stat should succeed. */
922 DEBUG(0,("Error doing fstat on open file %s "
924 smb_fname_str_dbg(smb_fname
),
929 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
930 FILE_NOTIFY_CHANGE_FILE_NAME
,
931 smb_fname
->base_name
);
934 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
936 /* File must exist for a stat open. */
937 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
940 status
= smbd_check_access_rights(conn
,
945 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
947 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
948 /* This is a POSIX stat open for delete
949 * or rename on a symlink that points
951 DEBUG(10,("open_file: allowing POSIX "
952 "open on bad symlink %s\n",
953 smb_fname_str_dbg(smb_fname
)));
954 status
= NT_STATUS_OK
;
957 if (!NT_STATUS_IS_OK(status
)) {
958 DEBUG(10,("open_file: "
959 "smbd_check_access_rights on file "
961 smb_fname_str_dbg(smb_fname
),
962 nt_errstr(status
) ));
968 * POSIX allows read-only opens of directories. We don't
969 * want to do this (we use a different code path for this)
970 * so catch a directory open and return an EISDIR. JRA.
973 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
976 return NT_STATUS_FILE_IS_A_DIRECTORY
;
979 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
980 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
981 fsp
->file_pid
= req
? req
->smbpid
: 0;
982 fsp
->can_lock
= True
;
983 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
986 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
987 fsp
->print_file
= NULL
;
988 fsp
->modified
= False
;
989 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
990 fsp
->is_directory
= False
;
991 if (conn
->aio_write_behind_list
&&
992 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
993 conn
->case_sensitive
)) {
994 fsp
->aio_write_behind
= True
;
997 fsp
->wcp
= NULL
; /* Write cache pointer. */
999 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1000 conn
->session_info
->unix_info
->unix_name
,
1001 smb_fname_str_dbg(smb_fname
),
1002 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
1003 conn
->num_files_open
));
1006 return NT_STATUS_OK
;
1009 /****************************************************************************
1010 Check if we can open a file with a share mode.
1011 Returns True if conflict, False if not.
1012 ****************************************************************************/
1014 static bool share_conflict(struct share_mode_entry
*entry
,
1016 uint32 share_access
)
1018 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1019 "entry->share_access = 0x%x, "
1020 "entry->private_options = 0x%x\n",
1021 (unsigned int)entry
->access_mask
,
1022 (unsigned int)entry
->share_access
,
1023 (unsigned int)entry
->private_options
));
1025 if (server_id_is_disconnected(&entry
->pid
)) {
1027 * note: cleanup should have been done by
1028 * delay_for_batch_oplocks()
1033 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1034 (unsigned int)access_mask
, (unsigned int)share_access
));
1036 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
1040 DELETE_ACCESS
)) == 0) {
1041 DEBUG(10,("share_conflict: No conflict due to "
1042 "entry->access_mask = 0x%x\n",
1043 (unsigned int)entry
->access_mask
));
1047 if ((access_mask
& (FILE_WRITE_DATA
|
1051 DELETE_ACCESS
)) == 0) {
1052 DEBUG(10,("share_conflict: No conflict due to "
1053 "access_mask = 0x%x\n",
1054 (unsigned int)access_mask
));
1058 #if 1 /* JRA TEST - Superdebug. */
1059 #define CHECK_MASK(num, am, right, sa, share) \
1060 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1061 (unsigned int)(num), (unsigned int)(am), \
1062 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1063 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1064 (unsigned int)(num), (unsigned int)(sa), \
1065 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1066 if (((am) & (right)) && !((sa) & (share))) { \
1067 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1068 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1069 (unsigned int)(share) )); \
1073 #define CHECK_MASK(num, am, right, sa, share) \
1074 if (((am) & (right)) && !((sa) & (share))) { \
1075 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1076 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1077 (unsigned int)(share) )); \
1082 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1083 share_access
, FILE_SHARE_WRITE
);
1084 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1085 entry
->share_access
, FILE_SHARE_WRITE
);
1087 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1088 share_access
, FILE_SHARE_READ
);
1089 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1090 entry
->share_access
, FILE_SHARE_READ
);
1092 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
1093 share_access
, FILE_SHARE_DELETE
);
1094 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
1095 entry
->share_access
, FILE_SHARE_DELETE
);
1097 DEBUG(10,("share_conflict: No conflict.\n"));
1101 #if defined(DEVELOPER)
1102 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
1104 struct share_mode_entry
*share_entry
)
1106 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1109 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1113 if (share_entry
->op_mid
== 0) {
1114 /* INTERNAL_OPEN_ONLY */
1118 if (!is_valid_share_mode_entry(share_entry
)) {
1122 fsp
= file_find_dif(sconn
, share_entry
->id
,
1123 share_entry
->share_file_id
);
1125 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1126 share_mode_str(talloc_tos(), num
, share_entry
) ));
1127 smb_panic("validate_my_share_entries: Cannot match a "
1128 "share entry with an open file\n");
1131 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
1140 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1141 share_mode_str(talloc_tos(), num
, share_entry
) ));
1142 str
= talloc_asprintf(talloc_tos(),
1143 "validate_my_share_entries: "
1144 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1145 fsp
->fsp_name
->base_name
,
1146 (unsigned int)fsp
->oplock_type
,
1147 (unsigned int)share_entry
->op_type
);
1153 bool is_stat_open(uint32 access_mask
)
1155 const uint32_t stat_open_bits
=
1156 (SYNCHRONIZE_ACCESS
|
1157 FILE_READ_ATTRIBUTES
|
1158 FILE_WRITE_ATTRIBUTES
);
1160 return (((access_mask
& stat_open_bits
) != 0) &&
1161 ((access_mask
& ~stat_open_bits
) == 0));
1164 static bool has_delete_on_close(struct share_mode_lock
*lck
,
1167 struct share_mode_data
*d
= lck
->data
;
1170 if (d
->num_share_modes
== 0) {
1173 if (!is_delete_on_close_set(lck
, name_hash
)) {
1176 for (i
=0; i
<d
->num_share_modes
; i
++) {
1177 if (!share_mode_stale_pid(d
, i
)) {
1184 /****************************************************************************
1185 Deal with share modes
1186 Invariant: Share mode must be locked on entry and exit.
1187 Returns -1 on error, or number of share modes on success (may be zero).
1188 ****************************************************************************/
1190 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1191 struct share_mode_lock
*lck
,
1193 uint32 share_access
)
1197 if(lck
->data
->num_share_modes
== 0) {
1198 return NT_STATUS_OK
;
1201 if (is_stat_open(access_mask
)) {
1202 /* Stat open that doesn't trigger oplock breaks or share mode
1203 * checks... ! JRA. */
1204 return NT_STATUS_OK
;
1208 * Check if the share modes will give us access.
1211 #if defined(DEVELOPER)
1212 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1213 validate_my_share_entries(conn
->sconn
, i
,
1214 &lck
->data
->share_modes
[i
]);
1218 /* Now we check the share modes, after any oplock breaks. */
1219 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1221 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1225 /* someone else has a share lock on it, check to see if we can
1227 if (share_conflict(&lck
->data
->share_modes
[i
],
1228 access_mask
, share_access
)) {
1230 if (share_mode_stale_pid(lck
->data
, i
)) {
1234 return NT_STATUS_SHARING_VIOLATION
;
1238 return NT_STATUS_OK
;
1242 * Send a break message to the oplock holder and delay the open for
1246 static NTSTATUS
send_break_message(struct messaging_context
*msg_ctx
,
1247 const struct share_mode_entry
*exclusive
,
1251 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1253 DEBUG(10, ("Sending break request to PID %s\n",
1254 procid_str_static(&exclusive
->pid
)));
1256 /* Create the message. */
1257 share_mode_entry_to_message(msg
, exclusive
);
1259 /* Overload entry->op_type */
1260 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
, break_to
);
1262 status
= messaging_send_buf(msg_ctx
, exclusive
->pid
,
1263 MSG_SMB_BREAK_REQUEST
,
1264 (uint8
*)msg
, sizeof(msg
));
1265 if (!NT_STATUS_IS_OK(status
)) {
1266 DEBUG(3, ("Could not send oplock break message: %s\n",
1267 nt_errstr(status
)));
1274 * Do internal consistency checks on the share mode for a file.
1277 static bool validate_oplock_types(struct share_mode_lock
*lck
)
1279 struct share_mode_data
*d
= lck
->data
;
1281 bool ex_or_batch
= false;
1282 bool level2
= false;
1283 bool no_oplock
= false;
1284 uint32_t num_non_stat_opens
= 0;
1287 for (i
=0; i
<d
->num_share_modes
; i
++) {
1288 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1290 if (!is_valid_share_mode_entry(e
)) {
1294 if (e
->op_mid
== 0) {
1295 /* INTERNAL_OPEN_ONLY */
1299 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1300 /* We ignore stat opens in the table - they
1301 always have NO_OPLOCK and never get or
1302 cause breaks. JRA. */
1306 num_non_stat_opens
+= 1;
1308 if (BATCH_OPLOCK_TYPE(e
->op_type
)) {
1309 /* batch - can only be one. */
1310 if (share_mode_stale_pid(d
, i
)) {
1311 DEBUG(10, ("Found stale batch oplock\n"));
1314 if (ex_or_batch
|| batch
|| level2
|| no_oplock
) {
1315 DEBUG(0, ("Bad batch oplock entry %u.",
1322 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1323 if (share_mode_stale_pid(d
, i
)) {
1324 DEBUG(10, ("Found stale duplicate oplock\n"));
1327 /* Exclusive or batch - can only be one. */
1328 if (ex_or_batch
|| level2
|| no_oplock
) {
1329 DEBUG(0, ("Bad exclusive or batch oplock "
1330 "entry %u.", (unsigned)i
));
1336 if (LEVEL_II_OPLOCK_TYPE(e
->op_type
)) {
1337 if (batch
|| ex_or_batch
) {
1338 if (share_mode_stale_pid(d
, i
)) {
1339 DEBUG(10, ("Found stale LevelII "
1343 DEBUG(0, ("Bad levelII oplock entry %u.",
1350 if (e
->op_type
== NO_OPLOCK
) {
1351 if (batch
|| ex_or_batch
) {
1352 if (share_mode_stale_pid(d
, i
)) {
1353 DEBUG(10, ("Found stale NO_OPLOCK "
1357 DEBUG(0, ("Bad no oplock entry %u.",
1365 remove_stale_share_mode_entries(d
);
1367 if ((batch
|| ex_or_batch
) && (num_non_stat_opens
!= 1)) {
1368 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1369 (int)batch
, (int)ex_or_batch
,
1370 (int)d
->num_share_modes
));
1377 static bool delay_for_oplock(files_struct
*fsp
,
1379 struct share_mode_lock
*lck
,
1380 bool have_sharing_violation
,
1381 uint32_t create_disposition
)
1383 struct share_mode_data
*d
= lck
->data
;
1384 struct share_mode_entry
*entry
;
1385 uint32_t num_non_stat_opens
= 0;
1389 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1392 for (i
=0; i
<d
->num_share_modes
; i
++) {
1393 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1394 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1397 num_non_stat_opens
+= 1;
1400 * We found the a non-stat open, which in the exclusive/batch
1401 * case will be inspected further down.
1405 if (num_non_stat_opens
== 0) {
1407 * Nothing to wait for around
1411 if (num_non_stat_opens
!= 1) {
1413 * More than one open around. There can't be any exclusive or
1414 * batch left, this is all level2.
1419 if (server_id_is_disconnected(&entry
->pid
)) {
1422 * This could be achieved by sending a break message
1423 * to ourselves. Special considerations for files
1424 * with delete_on_close flag set!
1426 * For now we keep it simple and do not
1427 * allow delete on close for durable handles.
1432 switch (create_disposition
) {
1433 case FILE_SUPERSEDE
:
1434 case FILE_OVERWRITE
:
1435 case FILE_OVERWRITE_IF
:
1436 break_to
= NO_OPLOCK
;
1439 break_to
= LEVEL_II_OPLOCK
;
1443 if (have_sharing_violation
&& (entry
->op_type
& BATCH_OPLOCK
)) {
1444 if (share_mode_stale_pid(d
, 0)) {
1447 send_break_message(fsp
->conn
->sconn
->msg_ctx
, entry
, break_to
);
1450 if (have_sharing_violation
) {
1452 * Non-batch exclusive is not broken if we have a sharing
1457 if (LEVEL_II_OPLOCK_TYPE(entry
->op_type
) &&
1458 (break_to
== NO_OPLOCK
)) {
1459 if (share_mode_stale_pid(d
, 0)) {
1462 DEBUG(10, ("Asynchronously breaking level2 oplock for "
1463 "create_disposition=%u\n",
1464 (unsigned)create_disposition
));
1465 send_break_message(fsp
->conn
->sconn
->msg_ctx
, entry
, break_to
);
1468 if (!EXCLUSIVE_OPLOCK_TYPE(entry
->op_type
)) {
1470 * No break for NO_OPLOCK or LEVEL2_OPLOCK oplocks
1474 if (share_mode_stale_pid(d
, 0)) {
1478 send_break_message(fsp
->conn
->sconn
->msg_ctx
, entry
, break_to
);
1482 static bool file_has_brlocks(files_struct
*fsp
)
1484 struct byte_range_lock
*br_lck
;
1486 br_lck
= brl_get_locks_readonly(fsp
);
1490 return (brl_num_locks(br_lck
) > 0);
1493 static void grant_fsp_oplock_type(files_struct
*fsp
,
1494 struct share_mode_lock
*lck
,
1497 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1498 lp_level2_oplocks(SNUM(fsp
->conn
));
1499 bool got_level2_oplock
, got_a_none_oplock
;
1502 /* Start by granting what the client asked for,
1503 but ensure no SAMBA_PRIVATE bits can be set. */
1504 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1506 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1507 /* No oplocks on internal open. */
1508 fsp
->oplock_type
= NO_OPLOCK
;
1509 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1510 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1514 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1515 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1517 fsp
->oplock_type
= NO_OPLOCK
;
1520 if (is_stat_open(fsp
->access_mask
)) {
1521 /* Leave the value already set. */
1522 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1523 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1527 got_level2_oplock
= false;
1528 got_a_none_oplock
= false;
1530 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1531 int op_type
= lck
->data
->share_modes
[i
].op_type
;
1533 if (LEVEL_II_OPLOCK_TYPE(op_type
)) {
1534 got_level2_oplock
= true;
1536 if (op_type
== NO_OPLOCK
) {
1537 got_a_none_oplock
= true;
1542 * Match what was requested (fsp->oplock_type) with
1543 * what was found in the existing share modes.
1546 if (got_level2_oplock
|| got_a_none_oplock
) {
1547 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1548 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1553 * Don't grant level2 to clients that don't want them
1554 * or if we've turned them off.
1556 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1557 fsp
->oplock_type
= NO_OPLOCK
;
1560 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !got_level2_oplock
) {
1562 * We're the first level2 oplock. Indicate that in brlock.tdb.
1564 struct byte_range_lock
*brl
;
1566 brl
= brl_get_locks(talloc_tos(), fsp
);
1568 brl_set_have_read_oplocks(brl
, true);
1573 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1574 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1577 static bool request_timed_out(struct timeval request_time
,
1578 struct timeval timeout
)
1580 struct timeval now
, end_time
;
1582 end_time
= timeval_sum(&request_time
, &timeout
);
1583 return (timeval_compare(&end_time
, &now
) < 0);
1586 struct defer_open_state
{
1587 struct smbXsrv_connection
*xconn
;
1591 static void defer_open_done(struct tevent_req
*req
);
1593 /****************************************************************************
1594 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1595 ****************************************************************************/
1597 static void defer_open(struct share_mode_lock
*lck
,
1598 struct timeval request_time
,
1599 struct timeval timeout
,
1600 struct smb_request
*req
,
1601 struct deferred_open_record
*state
)
1603 struct deferred_open_record
*open_rec
;
1605 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1606 "open entry for mid %llu\n",
1607 (unsigned int)request_time
.tv_sec
,
1608 (unsigned int)request_time
.tv_usec
,
1609 (unsigned long long)req
->mid
));
1611 open_rec
= talloc(NULL
, struct deferred_open_record
);
1612 if (open_rec
== NULL
) {
1614 exit_server("talloc failed");
1620 struct defer_open_state
*watch_state
;
1621 struct tevent_req
*watch_req
;
1624 watch_state
= talloc(open_rec
, struct defer_open_state
);
1625 if (watch_state
== NULL
) {
1626 exit_server("talloc failed");
1628 watch_state
->xconn
= req
->xconn
;
1629 watch_state
->mid
= req
->mid
;
1631 DEBUG(10, ("defering mid %llu\n",
1632 (unsigned long long)req
->mid
));
1634 watch_req
= dbwrap_record_watch_send(
1635 watch_state
, req
->sconn
->ev_ctx
, lck
->data
->record
,
1636 req
->sconn
->msg_ctx
);
1637 if (watch_req
== NULL
) {
1638 exit_server("Could not watch share mode record");
1640 tevent_req_set_callback(watch_req
, defer_open_done
,
1643 ret
= tevent_req_set_endtime(
1644 watch_req
, req
->sconn
->ev_ctx
,
1645 timeval_sum(&request_time
, &timeout
));
1649 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1650 state
->id
, open_rec
)) {
1652 exit_server("push_deferred_open_message_smb failed");
1656 static void defer_open_done(struct tevent_req
*req
)
1658 struct defer_open_state
*state
= tevent_req_callback_data(
1659 req
, struct defer_open_state
);
1663 status
= dbwrap_record_watch_recv(req
, talloc_tos(), NULL
);
1665 if (!NT_STATUS_IS_OK(status
)) {
1666 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1667 nt_errstr(status
)));
1669 * Even if it failed, retry anyway. TODO: We need a way to
1670 * tell a re-scheduled open about that error.
1674 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state
->mid
));
1676 ret
= schedule_deferred_open_message_smb(state
->xconn
, state
->mid
);
1682 /****************************************************************************
1683 On overwrite open ensure that the attributes match.
1684 ****************************************************************************/
1686 static bool open_match_attributes(connection_struct
*conn
,
1687 uint32 old_dos_attr
,
1688 uint32 new_dos_attr
,
1689 mode_t existing_unx_mode
,
1690 mode_t new_unx_mode
,
1691 mode_t
*returned_unx_mode
)
1693 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1695 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1696 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1698 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1699 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1700 *returned_unx_mode
= new_unx_mode
;
1702 *returned_unx_mode
= (mode_t
)0;
1705 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1706 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1707 "returned_unx_mode = 0%o\n",
1708 (unsigned int)old_dos_attr
,
1709 (unsigned int)existing_unx_mode
,
1710 (unsigned int)new_dos_attr
,
1711 (unsigned int)*returned_unx_mode
));
1713 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1714 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1715 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1716 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1720 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1721 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1722 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1729 /****************************************************************************
1730 Special FCB or DOS processing in the case of a sharing violation.
1731 Try and find a duplicated file handle.
1732 ****************************************************************************/
1734 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1735 connection_struct
*conn
,
1736 files_struct
*fsp_to_dup_into
,
1737 const struct smb_filename
*smb_fname
,
1742 uint32 share_access
,
1743 uint32 create_options
)
1747 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1748 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1750 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1751 fsp
= file_find_di_next(fsp
)) {
1753 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1754 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1755 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1756 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
1757 (unsigned int)fsp
->file_pid
,
1758 (unsigned int)fsp
->fh
->private_options
,
1759 (unsigned int)fsp
->access_mask
));
1761 if (fsp
!= fsp_to_dup_into
&&
1762 fsp
->fh
->fd
!= -1 &&
1763 fsp
->vuid
== vuid
&&
1764 fsp
->file_pid
== file_pid
&&
1765 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1766 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1767 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1768 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1769 strequal(fsp
->fsp_name
->stream_name
,
1770 smb_fname
->stream_name
)) {
1771 DEBUG(10,("fcb_or_dos_open: file match\n"));
1777 return NT_STATUS_NOT_FOUND
;
1780 /* quite an insane set of semantics ... */
1781 if (is_executable(smb_fname
->base_name
) &&
1782 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1783 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1784 return NT_STATUS_INVALID_PARAMETER
;
1787 /* We need to duplicate this fsp. */
1788 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1789 create_options
, fsp_to_dup_into
);
1792 static void schedule_defer_open(struct share_mode_lock
*lck
,
1794 struct timeval request_time
,
1795 struct smb_request
*req
)
1797 struct deferred_open_record state
;
1799 /* This is a relative time, added to the absolute
1800 request_time value to get the absolute timeout time.
1801 Note that if this is the second or greater time we enter
1802 this codepath for this particular request mid then
1803 request_time is left as the absolute time of the *first*
1804 time this request mid was processed. This is what allows
1805 the request to eventually time out. */
1807 struct timeval timeout
;
1809 /* Normally the smbd we asked should respond within
1810 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1811 * the client did, give twice the timeout as a safety
1812 * measure here in case the other smbd is stuck
1813 * somewhere else. */
1815 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1817 /* Nothing actually uses state.delayed_for_oplocks
1818 but it's handy to differentiate in debug messages
1819 between a 30 second delay due to oplock break, and
1820 a 1 second delay for share mode conflicts. */
1822 state
.delayed_for_oplocks
= True
;
1823 state
.async_open
= false;
1826 if (!request_timed_out(request_time
, timeout
)) {
1827 defer_open(lck
, request_time
, timeout
, req
, &state
);
1831 /****************************************************************************
1832 Reschedule an open call that went asynchronous.
1833 ****************************************************************************/
1835 static void schedule_async_open(struct timeval request_time
,
1836 struct smb_request
*req
)
1838 struct deferred_open_record state
;
1839 struct timeval timeout
;
1841 timeout
= timeval_set(20, 0);
1844 state
.delayed_for_oplocks
= false;
1845 state
.async_open
= true;
1847 if (!request_timed_out(request_time
, timeout
)) {
1848 defer_open(NULL
, request_time
, timeout
, req
, &state
);
1852 /****************************************************************************
1853 Work out what access_mask to use from what the client sent us.
1854 ****************************************************************************/
1856 static NTSTATUS
smbd_calculate_maximum_allowed_access(
1857 connection_struct
*conn
,
1858 const struct smb_filename
*smb_fname
,
1860 uint32_t *p_access_mask
)
1862 struct security_descriptor
*sd
;
1863 uint32_t access_granted
;
1866 if (!use_privs
&& (get_current_uid(conn
) == (uid_t
)0)) {
1867 *p_access_mask
|= FILE_GENERIC_ALL
;
1868 return NT_STATUS_OK
;
1871 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1877 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1879 * File did not exist
1881 *p_access_mask
= FILE_GENERIC_ALL
;
1882 return NT_STATUS_OK
;
1884 if (!NT_STATUS_IS_OK(status
)) {
1885 DEBUG(10,("Could not get acl on file %s: %s\n",
1886 smb_fname_str_dbg(smb_fname
),
1887 nt_errstr(status
)));
1888 return NT_STATUS_ACCESS_DENIED
;
1892 * If we can access the path to this file, by
1893 * default we have FILE_READ_ATTRIBUTES from the
1894 * containing directory. See the section:
1895 * "Algorithm to Check Access to an Existing File"
1898 * se_file_access_check()
1899 * also takes care of owner WRITE_DAC and READ_CONTROL.
1901 status
= se_file_access_check(sd
,
1902 get_current_nttok(conn
),
1904 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
1909 if (!NT_STATUS_IS_OK(status
)) {
1910 DEBUG(10, ("Access denied on file %s: "
1911 "when calculating maximum access\n",
1912 smb_fname_str_dbg(smb_fname
)));
1913 return NT_STATUS_ACCESS_DENIED
;
1915 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
1917 if (!(access_granted
& DELETE_ACCESS
)) {
1918 if (can_delete_file_in_directory(conn
, smb_fname
)) {
1919 *p_access_mask
|= DELETE_ACCESS
;
1923 return NT_STATUS_OK
;
1926 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1927 const struct smb_filename
*smb_fname
,
1929 uint32_t access_mask
,
1930 uint32_t *access_mask_out
)
1933 uint32_t orig_access_mask
= access_mask
;
1934 uint32_t rejected_share_access
;
1937 * Convert GENERIC bits to specific bits.
1940 se_map_generic(&access_mask
, &file_generic_mapping
);
1942 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1943 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1945 status
= smbd_calculate_maximum_allowed_access(
1946 conn
, smb_fname
, use_privs
, &access_mask
);
1948 if (!NT_STATUS_IS_OK(status
)) {
1952 access_mask
&= conn
->share_access
;
1955 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1957 if (rejected_share_access
) {
1958 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1959 "file %s: rejected by share access mask[0x%08X] "
1960 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1961 smb_fname_str_dbg(smb_fname
),
1963 orig_access_mask
, access_mask
,
1964 rejected_share_access
));
1965 return NT_STATUS_ACCESS_DENIED
;
1968 *access_mask_out
= access_mask
;
1969 return NT_STATUS_OK
;
1972 /****************************************************************************
1973 Remove the deferred open entry under lock.
1974 ****************************************************************************/
1976 /****************************************************************************
1977 Return true if this is a state pointer to an asynchronous create.
1978 ****************************************************************************/
1980 bool is_deferred_open_async(const struct deferred_open_record
*rec
)
1982 return rec
->async_open
;
1985 static bool clear_ads(uint32_t create_disposition
)
1989 switch (create_disposition
) {
1990 case FILE_SUPERSEDE
:
1991 case FILE_OVERWRITE_IF
:
1992 case FILE_OVERWRITE
:
2001 static int disposition_to_open_flags(uint32_t create_disposition
)
2006 * Currently we're using FILE_SUPERSEDE as the same as
2007 * FILE_OVERWRITE_IF but they really are
2008 * different. FILE_SUPERSEDE deletes an existing file
2009 * (requiring delete access) then recreates it.
2012 switch (create_disposition
) {
2013 case FILE_SUPERSEDE
:
2014 case FILE_OVERWRITE_IF
:
2016 * If file exists replace/overwrite. If file doesn't
2019 ret
= O_CREAT
|O_TRUNC
;
2024 * If file exists open. If file doesn't exist error.
2029 case FILE_OVERWRITE
:
2031 * If file exists overwrite. If file doesn't exist
2039 * If file exists error. If file doesn't exist create.
2041 ret
= O_CREAT
|O_EXCL
;
2046 * If file exists open. If file doesn't exist create.
2054 static int calculate_open_access_flags(uint32_t access_mask
,
2056 uint32_t private_flags
)
2058 bool need_write
, need_read
;
2061 * Note that we ignore the append flag as append does not
2062 * mean the same thing under DOS and Unix.
2065 need_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
));
2070 /* DENY_DOS opens are always underlying read-write on the
2071 file handle, no matter what the requested access mask
2075 ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
2076 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|
2077 FILE_READ_EA
|FILE_EXECUTE
));
2085 /****************************************************************************
2086 Open a file with a share mode. Passed in an already created files_struct *.
2087 ****************************************************************************/
2089 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
2090 struct smb_request
*req
,
2091 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
2092 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
2093 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
2094 uint32 create_options
, /* options such as delete on close. */
2095 uint32 new_dos_attributes
, /* attributes used for new file. */
2096 int oplock_request
, /* internal Samba oplock codes. */
2097 struct smb2_lease
*lease
,
2098 /* Information (FILE_EXISTS etc.) */
2099 uint32_t private_flags
, /* Samba specific flags. */
2103 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
2106 bool file_existed
= VALID_STAT(smb_fname
->st
);
2107 bool def_acl
= False
;
2108 bool posix_open
= False
;
2109 bool new_file_created
= False
;
2110 bool first_open_attempt
= true;
2111 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
2112 mode_t new_unx_mode
= (mode_t
)0;
2113 mode_t unx_mode
= (mode_t
)0;
2115 uint32 existing_dos_attributes
= 0;
2116 struct timeval request_time
= timeval_zero();
2117 struct share_mode_lock
*lck
= NULL
;
2118 uint32 open_access_mask
= access_mask
;
2121 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
2122 struct timespec old_write_time
;
2125 if (conn
->printer
) {
2127 * Printers are handled completely differently.
2128 * Most of the passed parameters are ignored.
2132 *pinfo
= FILE_WAS_CREATED
;
2135 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2136 smb_fname_str_dbg(smb_fname
)));
2139 DEBUG(0,("open_file_ntcreate: printer open without "
2140 "an SMB request!\n"));
2141 return NT_STATUS_INTERNAL_ERROR
;
2144 return print_spool_open(fsp
, smb_fname
->base_name
,
2148 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
2150 return NT_STATUS_NO_MEMORY
;
2153 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2155 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2156 new_dos_attributes
= 0;
2158 /* Windows allows a new file to be created and
2159 silently removes a FILE_ATTRIBUTE_DIRECTORY
2160 sent by the client. Do the same. */
2162 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
2164 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2166 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2167 smb_fname
, parent_dir
);
2170 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2171 "access_mask=0x%x share_access=0x%x "
2172 "create_disposition = 0x%x create_options=0x%x "
2173 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2174 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
2175 access_mask
, share_access
, create_disposition
,
2176 create_options
, (unsigned int)unx_mode
, oplock_request
,
2177 (unsigned int)private_flags
));
2180 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2181 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) != 0));
2183 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2184 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) == 0));
2188 * Only non-internal opens can be deferred at all
2192 struct deferred_open_record
*open_rec
;
2193 if (get_deferred_open_message_state(req
,
2196 /* Remember the absolute time of the original
2197 request with this mid. We'll use it later to
2198 see if this has timed out. */
2200 /* If it was an async create retry, the file
2203 if (is_deferred_open_async(open_rec
)) {
2204 SET_STAT_INVALID(smb_fname
->st
);
2205 file_existed
= false;
2208 /* Ensure we don't reprocess this message. */
2209 remove_deferred_open_message_smb(req
->xconn
, req
->mid
);
2211 first_open_attempt
= false;
2216 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
2218 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
2222 /* ignore any oplock requests if oplocks are disabled */
2223 if (!lp_oplocks(SNUM(conn
)) ||
2224 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
2225 /* Mask off everything except the private Samba bits. */
2226 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
2229 /* this is for OS/2 long file names - say we don't support them */
2230 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
2231 /* OS/2 Workplace shell fix may be main code stream in a later
2233 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2235 if (use_nt_status()) {
2236 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2238 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
2241 switch( create_disposition
) {
2243 /* If file exists open. If file doesn't exist error. */
2244 if (!file_existed
) {
2245 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2246 "requested for file %s and file "
2248 smb_fname_str_dbg(smb_fname
)));
2250 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2254 case FILE_OVERWRITE
:
2255 /* If file exists overwrite. If file doesn't exist
2257 if (!file_existed
) {
2258 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2259 "requested for file %s and file "
2261 smb_fname_str_dbg(smb_fname
) ));
2263 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2268 /* If file exists error. If file doesn't exist
2271 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2272 "requested for file %s and file "
2273 "already exists.\n",
2274 smb_fname_str_dbg(smb_fname
)));
2275 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2280 return map_nt_error_from_unix(errno
);
2284 case FILE_SUPERSEDE
:
2285 case FILE_OVERWRITE_IF
:
2289 return NT_STATUS_INVALID_PARAMETER
;
2292 flags2
= disposition_to_open_flags(create_disposition
);
2294 /* We only care about matching attributes on file exists and
2297 if (!posix_open
&& file_existed
&&
2298 ((create_disposition
== FILE_OVERWRITE
) ||
2299 (create_disposition
== FILE_OVERWRITE_IF
))) {
2300 if (!open_match_attributes(conn
, existing_dos_attributes
,
2302 smb_fname
->st
.st_ex_mode
,
2303 unx_mode
, &new_unx_mode
)) {
2304 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2305 "for file %s (%x %x) (0%o, 0%o)\n",
2306 smb_fname_str_dbg(smb_fname
),
2307 existing_dos_attributes
,
2309 (unsigned int)smb_fname
->st
.st_ex_mode
,
2310 (unsigned int)unx_mode
));
2312 return NT_STATUS_ACCESS_DENIED
;
2316 status
= smbd_calculate_access_mask(conn
, smb_fname
,
2320 if (!NT_STATUS_IS_OK(status
)) {
2321 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2322 "on file %s returned %s\n",
2323 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
2327 open_access_mask
= access_mask
;
2329 if (flags2
& O_TRUNC
) {
2330 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
2333 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2334 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
2338 * Note that we ignore the append flag as append does not
2339 * mean the same thing under DOS and Unix.
2342 flags
= calculate_open_access_flags(access_mask
, oplock_request
,
2346 * Currently we only look at FILE_WRITE_THROUGH for create options.
2350 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
2355 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
2359 if (!posix_open
&& !CAN_WRITE(conn
)) {
2361 * We should really return a permission denied error if either
2362 * O_CREAT or O_TRUNC are set, but for compatibility with
2363 * older versions of Samba we just AND them out.
2365 flags2
&= ~(O_CREAT
|O_TRUNC
);
2368 if (first_open_attempt
&& lp_kernel_oplocks(SNUM(conn
))) {
2370 * With kernel oplocks the open breaking an oplock
2371 * blocks until the oplock holder has given up the
2372 * oplock or closed the file. We prevent this by first
2373 * trying to open the file with O_NONBLOCK (see "man
2374 * fcntl" on Linux). For the second try, triggered by
2375 * an oplock break response, we do not need this
2378 * This is true under the assumption that only Samba
2379 * requests kernel oplocks. Once someone else like
2380 * NFSv4 starts to use that API, we will have to
2381 * modify this by communicating with the NFSv4 server.
2383 flags2
|= O_NONBLOCK
;
2387 * Ensure we can't write on a read-only share or file.
2390 if (flags
!= O_RDONLY
&& file_existed
&&
2391 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
2392 DEBUG(5,("open_file_ntcreate: write access requested for "
2393 "file %s on read only %s\n",
2394 smb_fname_str_dbg(smb_fname
),
2395 !CAN_WRITE(conn
) ? "share" : "file" ));
2397 return NT_STATUS_ACCESS_DENIED
;
2400 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2401 fsp
->share_access
= share_access
;
2402 fsp
->fh
->private_options
= private_flags
;
2403 fsp
->access_mask
= open_access_mask
; /* We change this to the
2404 * requested access_mask after
2405 * the open is done. */
2406 fsp
->posix_open
= posix_open
;
2408 /* Ensure no SAMBA_PRIVATE bits can be set. */
2409 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
2411 if (timeval_is_zero(&request_time
)) {
2412 request_time
= fsp
->open_time
;
2416 * Ensure we pay attention to default ACLs on directories if required.
2419 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2420 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2421 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2424 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2425 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2426 (unsigned int)flags
, (unsigned int)flags2
,
2427 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2428 (unsigned int)open_access_mask
));
2430 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2431 flags
|flags2
, unx_mode
, access_mask
,
2432 open_access_mask
, &new_file_created
);
2434 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_NETWORK_BUSY
)) {
2435 struct deferred_open_record state
;
2438 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2440 if (file_existed
&& S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
)) {
2441 DEBUG(10, ("FIFO busy\n"));
2442 return NT_STATUS_NETWORK_BUSY
;
2445 DEBUG(10, ("Internal open busy\n"));
2446 return NT_STATUS_NETWORK_BUSY
;
2450 * From here on we assume this is an oplock break triggered
2453 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
2455 state
.delayed_for_oplocks
= false;
2456 state
.async_open
= false;
2457 state
.id
= fsp
->file_id
;
2458 defer_open(NULL
, request_time
, timeval_set(0, 0),
2460 DEBUG(10, ("No share mode lock found after "
2461 "EWOULDBLOCK, retrying sync\n"));
2462 return NT_STATUS_SHARING_VIOLATION
;
2465 if (!validate_oplock_types(lck
)) {
2466 smb_panic("validate_oplock_types failed");
2469 if (delay_for_oplock(fsp
, 0, lck
, false, create_disposition
)) {
2470 schedule_defer_open(lck
, fsp
->file_id
, request_time
, req
);
2472 DEBUG(10, ("Sent oplock break request to kernel "
2473 "oplock holder\n"));
2474 return NT_STATUS_SHARING_VIOLATION
;
2478 * No oplock from Samba around. Immediately retry with
2481 state
.delayed_for_oplocks
= false;
2482 state
.async_open
= false;
2483 state
.id
= fsp
->file_id
;
2484 defer_open(lck
, request_time
, timeval_set(0, 0), req
, &state
);
2486 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2487 "Retrying sync\n"));
2488 return NT_STATUS_SHARING_VIOLATION
;
2491 if (!NT_STATUS_IS_OK(fsp_open
)) {
2492 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
2493 schedule_async_open(request_time
, req
);
2498 if (new_file_created
) {
2500 * As we atomically create using O_CREAT|O_EXCL,
2501 * then if new_file_created is true, then
2502 * file_existed *MUST* have been false (even
2503 * if the file was previously detected as being
2506 file_existed
= false;
2509 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
2511 * The file did exist, but some other (local or NFS)
2512 * process either renamed/unlinked and re-created the
2513 * file with different dev/ino after we walked the path,
2514 * but before we did the open. We could retry the
2515 * open but it's a rare enough case it's easier to
2516 * just fail the open to prevent creating any problems
2517 * in the open file db having the wrong dev/ino key.
2520 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2521 "Old (dev=0x%llu, ino =0x%llu). "
2522 "New (dev=0x%llu, ino=0x%llu). Failing open "
2523 " with NT_STATUS_ACCESS_DENIED.\n",
2524 smb_fname_str_dbg(smb_fname
),
2525 (unsigned long long)saved_stat
.st_ex_dev
,
2526 (unsigned long long)saved_stat
.st_ex_ino
,
2527 (unsigned long long)smb_fname
->st
.st_ex_dev
,
2528 (unsigned long long)smb_fname
->st
.st_ex_ino
));
2529 return NT_STATUS_ACCESS_DENIED
;
2532 old_write_time
= smb_fname
->st
.st_ex_mtime
;
2535 * Deal with the race condition where two smbd's detect the
2536 * file doesn't exist and do the create at the same time. One
2537 * of them will win and set a share mode, the other (ie. this
2538 * one) should check if the requested share mode for this
2539 * create is allowed.
2543 * Now the file exists and fsp is successfully opened,
2544 * fsp->dev and fsp->inode are valid and should replace the
2545 * dev=0,inode=0 from a non existent file. Spotted by
2546 * Nadav Danieli <nadavd@exanet.com>. JRA.
2551 lck
= get_share_mode_lock(talloc_tos(), id
,
2553 smb_fname
, &old_write_time
);
2556 DEBUG(0, ("open_file_ntcreate: Could not get share "
2557 "mode lock for %s\n",
2558 smb_fname_str_dbg(smb_fname
)));
2560 return NT_STATUS_SHARING_VIOLATION
;
2563 /* Get the types we need to examine. */
2564 if (!validate_oplock_types(lck
)) {
2565 smb_panic("validate_oplock_types failed");
2568 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
2571 return NT_STATUS_DELETE_PENDING
;
2574 status
= open_mode_check(conn
, lck
,
2575 access_mask
, share_access
);
2577 if (NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
) ||
2578 (lck
->data
->num_share_modes
> 0)) {
2580 * This comes from ancient times out of open_mode_check. I
2581 * have no clue whether this is still necessary. I can't think
2582 * of a case where this would actually matter further down in
2583 * this function. I leave it here for further investigation
2586 file_existed
= true;
2589 if ((req
!= NULL
) &&
2591 fsp
, oplock_request
, lck
,
2592 NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
),
2593 create_disposition
)) {
2594 schedule_defer_open(lck
, fsp
->file_id
, request_time
, req
);
2597 return NT_STATUS_SHARING_VIOLATION
;
2600 if (!NT_STATUS_IS_OK(status
)) {
2601 uint32 can_access_mask
;
2602 bool can_access
= True
;
2604 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2606 /* Check if this can be done with the deny_dos and fcb
2609 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2610 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2612 DEBUG(0, ("DOS open without an SMB "
2616 return NT_STATUS_INTERNAL_ERROR
;
2619 /* Use the client requested access mask here,
2620 * not the one we open with. */
2621 status
= fcb_or_dos_open(req
,
2632 if (NT_STATUS_IS_OK(status
)) {
2635 *pinfo
= FILE_WAS_OPENED
;
2637 return NT_STATUS_OK
;
2642 * This next line is a subtlety we need for
2643 * MS-Access. If a file open will fail due to share
2644 * permissions and also for security (access) reasons,
2645 * we need to return the access failed error, not the
2646 * share error. We can't open the file due to kernel
2647 * oplock deadlock (it's possible we failed above on
2648 * the open_mode_check()) so use a userspace check.
2651 if (flags
& O_RDWR
) {
2652 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2653 } else if (flags
& O_WRONLY
) {
2654 can_access_mask
= FILE_WRITE_DATA
;
2656 can_access_mask
= FILE_READ_DATA
;
2659 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2660 !CAN_WRITE(conn
)) ||
2661 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
2664 can_access_mask
))) {
2669 * If we're returning a share violation, ensure we
2670 * cope with the braindead 1 second delay (SMB1 only).
2673 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2674 !conn
->sconn
->using_smb2
&&
2675 lp_defer_sharing_violations()) {
2676 struct timeval timeout
;
2677 struct deferred_open_record state
;
2680 /* this is a hack to speed up torture tests
2682 timeout_usecs
= lp_parm_int(SNUM(conn
),
2683 "smbd","sharedelay",
2684 SHARING_VIOLATION_USEC_WAIT
);
2686 /* This is a relative time, added to the absolute
2687 request_time value to get the absolute timeout time.
2688 Note that if this is the second or greater time we enter
2689 this codepath for this particular request mid then
2690 request_time is left as the absolute time of the *first*
2691 time this request mid was processed. This is what allows
2692 the request to eventually time out. */
2694 timeout
= timeval_set(0, timeout_usecs
);
2696 /* Nothing actually uses state.delayed_for_oplocks
2697 but it's handy to differentiate in debug messages
2698 between a 30 second delay due to oplock break, and
2699 a 1 second delay for share mode conflicts. */
2701 state
.delayed_for_oplocks
= False
;
2702 state
.async_open
= false;
2706 && !request_timed_out(request_time
,
2708 defer_open(lck
, request_time
, timeout
,
2717 * We have detected a sharing violation here
2718 * so return the correct error code
2720 status
= NT_STATUS_SHARING_VIOLATION
;
2722 status
= NT_STATUS_ACCESS_DENIED
;
2727 /* Should we atomically (to the client at least) truncate ? */
2728 if ((!new_file_created
) &&
2729 (flags2
& O_TRUNC
) &&
2730 (!S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
))) {
2733 ret
= vfs_set_filelen(fsp
, 0);
2735 status
= map_nt_error_from_unix(errno
);
2742 grant_fsp_oplock_type(fsp
, lck
, oplock_request
);
2745 * We have the share entry *locked*.....
2748 /* Delete streams if create_disposition requires it */
2749 if (!new_file_created
&& clear_ads(create_disposition
) &&
2750 !is_ntfs_stream_smb_fname(smb_fname
)) {
2751 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2752 if (!NT_STATUS_IS_OK(status
)) {
2759 /* note that we ignore failure for the following. It is
2760 basically a hack for NFS, and NFS will never set one of
2761 these only read them. Nobody but Samba can ever set a deny
2762 mode and we have already checked our more authoritative
2763 locking database for permission to set this deny mode. If
2764 the kernel refuses the operations then the kernel is wrong.
2765 note that GPFS supports it as well - jmcd */
2767 if (fsp
->fh
->fd
!= -1 && lp_kernel_share_modes(SNUM(conn
))) {
2769 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2770 if(ret_flock
== -1 ){
2775 return NT_STATUS_SHARING_VIOLATION
;
2780 * At this point onwards, we can guarantee that the share entry
2781 * is locked, whether we created the file or not, and that the
2782 * deny mode is compatible with all current opens.
2786 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2787 * but we don't have to store this - just ignore it on access check.
2789 if (conn
->sconn
->using_smb2
) {
2791 * SMB2 doesn't return it (according to Microsoft tests).
2792 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2793 * File created with access = 0x7 (Read, Write, Delete)
2794 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2796 fsp
->access_mask
= access_mask
;
2798 /* But SMB1 does. */
2799 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2803 /* stat opens on existing files don't get oplocks. */
2804 if (is_stat_open(open_access_mask
)) {
2805 fsp
->oplock_type
= NO_OPLOCK
;
2809 if (new_file_created
) {
2810 info
= FILE_WAS_CREATED
;
2812 if (flags2
& O_TRUNC
) {
2813 info
= FILE_WAS_OVERWRITTEN
;
2815 info
= FILE_WAS_OPENED
;
2824 * Setup the oplock info in both the shared memory and
2828 status
= set_file_oplock(fsp
);
2829 if (!NT_STATUS_IS_OK(status
)) {
2831 * Could not get the kernel oplock
2833 fsp
->oplock_type
= NO_OPLOCK
;
2836 if (!set_share_mode(lck
, fsp
, get_current_uid(conn
),
2838 fsp
->oplock_type
)) {
2841 return NT_STATUS_NO_MEMORY
;
2844 /* Handle strange delete on close create semantics. */
2845 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2847 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2849 if (!NT_STATUS_IS_OK(status
)) {
2850 /* Remember to delete the mode we just added. */
2851 del_share_mode(lck
, fsp
);
2856 /* Note that here we set the *inital* delete on close flag,
2857 not the regular one. The magic gets handled in close. */
2858 fsp
->initial_delete_on_close
= True
;
2861 if (info
!= FILE_WAS_OPENED
) {
2862 /* Files should be initially set as archive */
2863 if (lp_map_archive(SNUM(conn
)) ||
2864 lp_store_dos_attributes(SNUM(conn
))) {
2866 if (file_set_dosmode(conn
, smb_fname
,
2867 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2868 parent_dir
, true) == 0) {
2869 unx_mode
= smb_fname
->st
.st_ex_mode
;
2875 /* Determine sparse flag. */
2877 /* POSIX opens are sparse by default. */
2878 fsp
->is_sparse
= true;
2880 fsp
->is_sparse
= (file_existed
&&
2881 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2885 * Take care of inherited ACLs on created files - if default ACL not
2889 if (!posix_open
&& new_file_created
&& !def_acl
) {
2891 int saved_errno
= errno
; /* We might get ENOSYS in the next
2894 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2896 errno
= saved_errno
; /* Ignore ENOSYS */
2899 } else if (new_unx_mode
) {
2903 /* Attributes need changing. File already existed. */
2906 int saved_errno
= errno
; /* We might get ENOSYS in the
2908 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2910 if (ret
== -1 && errno
== ENOSYS
) {
2911 errno
= saved_errno
; /* Ignore ENOSYS */
2913 DEBUG(5, ("open_file_ntcreate: reset "
2914 "attributes of file %s to 0%o\n",
2915 smb_fname_str_dbg(smb_fname
),
2916 (unsigned int)new_unx_mode
));
2917 ret
= 0; /* Don't do the fchmod below. */
2922 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2923 DEBUG(5, ("open_file_ntcreate: failed to reset "
2924 "attributes of file %s to 0%o\n",
2925 smb_fname_str_dbg(smb_fname
),
2926 (unsigned int)new_unx_mode
));
2931 * Deal with other opens having a modified write time.
2933 struct timespec write_time
= get_share_mode_write_time(lck
);
2935 if (!null_timespec(write_time
)) {
2936 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
2942 return NT_STATUS_OK
;
2945 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2946 struct smb_filename
*smb_dname
,
2947 uint32 file_attributes
)
2950 char *parent_dir
= NULL
;
2952 bool posix_open
= false;
2953 bool need_re_stat
= false;
2954 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2956 if (!CAN_WRITE(conn
) || (access_mask
& ~(conn
->share_access
))) {
2957 DEBUG(5,("mkdir_internal: failing share access "
2958 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
2959 return NT_STATUS_ACCESS_DENIED
;
2962 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2964 return NT_STATUS_NO_MEMORY
;
2967 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2969 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2971 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2974 status
= check_parent_access(conn
,
2977 if(!NT_STATUS_IS_OK(status
)) {
2978 DEBUG(5,("mkdir_internal: check_parent_access "
2979 "on directory %s for path %s returned %s\n",
2981 smb_dname
->base_name
,
2982 nt_errstr(status
) ));
2986 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2987 return map_nt_error_from_unix(errno
);
2990 /* Ensure we're checking for a symlink here.... */
2991 /* We don't want to get caught by a symlink racer. */
2993 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2994 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2995 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2996 return map_nt_error_from_unix(errno
);
2999 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3000 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3001 smb_fname_str_dbg(smb_dname
)));
3002 return NT_STATUS_NOT_A_DIRECTORY
;
3005 if (lp_store_dos_attributes(SNUM(conn
))) {
3007 file_set_dosmode(conn
, smb_dname
,
3008 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
3013 if (lp_inherit_permissions(SNUM(conn
))) {
3014 inherit_access_posix_acl(conn
, parent_dir
,
3015 smb_dname
->base_name
, mode
);
3016 need_re_stat
= true;
3021 * Check if high bits should have been set,
3022 * then (if bits are missing): add them.
3023 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3026 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
3027 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
3028 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
3029 (smb_dname
->st
.st_ex_mode
|
3030 (mode
& ~smb_dname
->st
.st_ex_mode
)));
3031 need_re_stat
= true;
3035 /* Change the owner if required. */
3036 if (lp_inherit_owner(SNUM(conn
))) {
3037 change_dir_owner_to_parent(conn
, parent_dir
,
3038 smb_dname
->base_name
,
3040 need_re_stat
= true;
3044 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
3045 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3046 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3047 return map_nt_error_from_unix(errno
);
3051 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
3052 smb_dname
->base_name
);
3054 return NT_STATUS_OK
;
3057 /****************************************************************************
3058 Open a directory from an NT SMB call.
3059 ****************************************************************************/
3061 static NTSTATUS
open_directory(connection_struct
*conn
,
3062 struct smb_request
*req
,
3063 struct smb_filename
*smb_dname
,
3065 uint32 share_access
,
3066 uint32 create_disposition
,
3067 uint32 create_options
,
3068 uint32 file_attributes
,
3070 files_struct
**result
)
3072 files_struct
*fsp
= NULL
;
3073 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
3074 struct share_mode_lock
*lck
= NULL
;
3076 struct timespec mtimespec
;
3079 if (is_ntfs_stream_smb_fname(smb_dname
)) {
3080 DEBUG(2, ("open_directory: %s is a stream name!\n",
3081 smb_fname_str_dbg(smb_dname
)));
3082 return NT_STATUS_NOT_A_DIRECTORY
;
3085 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
3086 /* Ensure we have a directory attribute. */
3087 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3090 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3091 "share_access = 0x%x create_options = 0x%x, "
3092 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3093 smb_fname_str_dbg(smb_dname
),
3094 (unsigned int)access_mask
,
3095 (unsigned int)share_access
,
3096 (unsigned int)create_options
,
3097 (unsigned int)create_disposition
,
3098 (unsigned int)file_attributes
));
3100 status
= smbd_calculate_access_mask(conn
, smb_dname
, false,
3101 access_mask
, &access_mask
);
3102 if (!NT_STATUS_IS_OK(status
)) {
3103 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3104 "on file %s returned %s\n",
3105 smb_fname_str_dbg(smb_dname
),
3106 nt_errstr(status
)));
3110 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3111 !security_token_has_privilege(get_current_nttok(conn
),
3112 SEC_PRIV_SECURITY
)) {
3113 DEBUG(10, ("open_directory: open on %s "
3114 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3115 smb_fname_str_dbg(smb_dname
)));
3116 return NT_STATUS_PRIVILEGE_NOT_HELD
;
3119 switch( create_disposition
) {
3123 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3126 info
= FILE_WAS_OPENED
;
3131 /* If directory exists error. If directory doesn't
3135 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
3136 DEBUG(2, ("open_directory: unable to create "
3137 "%s. Error was %s\n",
3138 smb_fname_str_dbg(smb_dname
),
3139 nt_errstr(status
)));
3143 status
= mkdir_internal(conn
, smb_dname
,
3146 if (!NT_STATUS_IS_OK(status
)) {
3147 DEBUG(2, ("open_directory: unable to create "
3148 "%s. Error was %s\n",
3149 smb_fname_str_dbg(smb_dname
),
3150 nt_errstr(status
)));
3154 info
= FILE_WAS_CREATED
;
3159 * If directory exists open. If directory doesn't
3164 status
= NT_STATUS_OK
;
3165 info
= FILE_WAS_OPENED
;
3167 status
= mkdir_internal(conn
, smb_dname
,
3170 if (NT_STATUS_IS_OK(status
)) {
3171 info
= FILE_WAS_CREATED
;
3173 /* Cope with create race. */
3174 if (!NT_STATUS_EQUAL(status
,
3175 NT_STATUS_OBJECT_NAME_COLLISION
)) {
3176 DEBUG(2, ("open_directory: unable to create "
3177 "%s. Error was %s\n",
3178 smb_fname_str_dbg(smb_dname
),
3179 nt_errstr(status
)));
3182 info
= FILE_WAS_OPENED
;
3188 case FILE_SUPERSEDE
:
3189 case FILE_OVERWRITE
:
3190 case FILE_OVERWRITE_IF
:
3192 DEBUG(5,("open_directory: invalid create_disposition "
3193 "0x%x for directory %s\n",
3194 (unsigned int)create_disposition
,
3195 smb_fname_str_dbg(smb_dname
)));
3196 return NT_STATUS_INVALID_PARAMETER
;
3199 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3200 DEBUG(5,("open_directory: %s is not a directory !\n",
3201 smb_fname_str_dbg(smb_dname
)));
3202 return NT_STATUS_NOT_A_DIRECTORY
;
3205 if (info
== FILE_WAS_OPENED
) {
3206 status
= smbd_check_access_rights(conn
,
3210 if (!NT_STATUS_IS_OK(status
)) {
3211 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3212 "file %s failed with %s\n",
3213 smb_fname_str_dbg(smb_dname
),
3214 nt_errstr(status
)));
3219 status
= file_new(req
, conn
, &fsp
);
3220 if(!NT_STATUS_IS_OK(status
)) {
3225 * Setup the files_struct for it.
3228 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
3229 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
3230 fsp
->file_pid
= req
? req
->smbpid
: 0;
3231 fsp
->can_lock
= False
;
3232 fsp
->can_read
= False
;
3233 fsp
->can_write
= False
;
3235 fsp
->share_access
= share_access
;
3236 fsp
->fh
->private_options
= 0;
3238 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3240 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3241 fsp
->print_file
= NULL
;
3242 fsp
->modified
= False
;
3243 fsp
->oplock_type
= NO_OPLOCK
;
3244 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
3245 fsp
->is_directory
= True
;
3246 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
3247 status
= fsp_set_smb_fname(fsp
, smb_dname
);
3248 if (!NT_STATUS_IS_OK(status
)) {
3249 file_free(req
, fsp
);
3253 /* Don't store old timestamps for directory
3254 handles in the internal database. We don't
3255 update them in there if new objects
3256 are creaded in the directory. Currently
3257 we only update timestamps on file writes.
3260 ZERO_STRUCT(mtimespec
);
3262 if (access_mask
& (FILE_LIST_DIRECTORY
|
3264 FILE_ADD_SUBDIRECTORY
|
3267 FILE_DELETE_CHILD
)) {
3269 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
3271 /* POSIX allows us to open a directory with O_RDONLY. */
3272 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
3274 if (!NT_STATUS_IS_OK(status
)) {
3275 DEBUG(5, ("open_directory: Could not open fd for "
3277 smb_fname_str_dbg(smb_dname
),
3278 nt_errstr(status
)));
3279 file_free(req
, fsp
);
3284 DEBUG(10, ("Not opening Directory %s\n",
3285 smb_fname_str_dbg(smb_dname
)));
3288 status
= vfs_stat_fsp(fsp
);
3289 if (!NT_STATUS_IS_OK(status
)) {
3291 file_free(req
, fsp
);
3295 /* Ensure there was no race condition. */
3296 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
3297 DEBUG(5,("open_directory: stat struct differs for "
3299 smb_fname_str_dbg(smb_dname
)));
3301 file_free(req
, fsp
);
3302 return NT_STATUS_ACCESS_DENIED
;
3305 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
3306 conn
->connectpath
, smb_dname
,
3310 DEBUG(0, ("open_directory: Could not get share mode lock for "
3311 "%s\n", smb_fname_str_dbg(smb_dname
)));
3313 file_free(req
, fsp
);
3314 return NT_STATUS_SHARING_VIOLATION
;
3317 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
3320 file_free(req
, fsp
);
3321 return NT_STATUS_DELETE_PENDING
;
3324 status
= open_mode_check(conn
, lck
,
3325 access_mask
, share_access
);
3327 if (!NT_STATUS_IS_OK(status
)) {
3330 file_free(req
, fsp
);
3334 if (!set_share_mode(lck
, fsp
, get_current_uid(conn
),
3335 req
? req
->mid
: 0, NO_OPLOCK
)) {
3338 file_free(req
, fsp
);
3339 return NT_STATUS_NO_MEMORY
;
3342 /* For directories the delete on close bit at open time seems
3343 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3344 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3345 status
= can_set_delete_on_close(fsp
, 0);
3346 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
3347 del_share_mode(lck
, fsp
);
3350 file_free(req
, fsp
);
3354 if (NT_STATUS_IS_OK(status
)) {
3355 /* Note that here we set the *inital* delete on close flag,
3356 not the regular one. The magic gets handled in close. */
3357 fsp
->initial_delete_on_close
= True
;
3363 * Deal with other opens having a modified write time. Is this
3364 * possible for directories?
3366 struct timespec write_time
= get_share_mode_write_time(lck
);
3368 if (!null_timespec(write_time
)) {
3369 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
3380 return NT_STATUS_OK
;
3383 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
3384 struct smb_filename
*smb_dname
)
3389 status
= SMB_VFS_CREATE_FILE(
3392 0, /* root_dir_fid */
3393 smb_dname
, /* fname */
3394 FILE_READ_ATTRIBUTES
, /* access_mask */
3395 FILE_SHARE_NONE
, /* share_access */
3396 FILE_CREATE
, /* create_disposition*/
3397 FILE_DIRECTORY_FILE
, /* create_options */
3398 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
3399 0, /* oplock_request */
3401 0, /* allocation_size */
3402 0, /* private_flags */
3408 if (NT_STATUS_IS_OK(status
)) {
3409 close_file(req
, fsp
, NORMAL_CLOSE
);
3415 /****************************************************************************
3416 Receive notification that one of our open files has been renamed by another
3418 ****************************************************************************/
3420 void msg_file_was_renamed(struct messaging_context
*msg
,
3423 struct server_id server_id
,
3427 char *frm
= (char *)data
->data
;
3429 const char *sharepath
;
3430 const char *base_name
;
3431 const char *stream_name
;
3432 struct smb_filename
*smb_fname
= NULL
;
3433 size_t sp_len
, bn_len
;
3435 struct smbd_server_connection
*sconn
=
3436 talloc_get_type_abort(private_data
,
3437 struct smbd_server_connection
);
3439 if (data
->data
== NULL
3440 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3441 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3442 (int)data
->length
));
3446 /* Unpack the message. */
3447 pull_file_id_24(frm
, &id
);
3448 sharepath
= &frm
[24];
3449 sp_len
= strlen(sharepath
);
3450 base_name
= sharepath
+ sp_len
+ 1;
3451 bn_len
= strlen(base_name
);
3452 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3454 /* stream_name must always be NULL if there is no stream. */
3455 if (stream_name
[0] == '\0') {
3459 smb_fname
= synthetic_smb_fname(talloc_tos(), base_name
,
3461 if (smb_fname
== NULL
) {
3465 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3467 sharepath
, smb_fname_str_dbg(smb_fname
),
3468 file_id_string_tos(&id
)));
3470 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3471 fsp
= file_find_di_next(fsp
)) {
3472 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3474 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3475 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
3476 smb_fname_str_dbg(smb_fname
)));
3477 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3478 if (!NT_STATUS_IS_OK(status
)) {
3483 /* Now we have the complete path we can work out if this is
3484 actually within this share and adjust newname accordingly. */
3485 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3486 "not sharepath %s) "
3487 "%s from %s -> %s\n",
3488 fsp
->conn
->connectpath
,
3492 smb_fname_str_dbg(smb_fname
)));
3496 TALLOC_FREE(smb_fname
);
3501 * If a main file is opened for delete, all streams need to be checked for
3502 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3503 * If that works, delete them all by setting the delete on close and close.
3506 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3509 struct stream_struct
*stream_info
= NULL
;
3510 files_struct
**streams
= NULL
;
3512 unsigned int num_streams
= 0;
3513 TALLOC_CTX
*frame
= talloc_stackframe();
3516 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3517 &num_streams
, &stream_info
);
3519 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3520 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3521 DEBUG(10, ("no streams around\n"));
3523 return NT_STATUS_OK
;
3526 if (!NT_STATUS_IS_OK(status
)) {
3527 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3528 nt_errstr(status
)));
3532 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3535 if (num_streams
== 0) {
3537 return NT_STATUS_OK
;
3540 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3541 if (streams
== NULL
) {
3542 DEBUG(0, ("talloc failed\n"));
3543 status
= NT_STATUS_NO_MEMORY
;
3547 for (i
=0; i
<num_streams
; i
++) {
3548 struct smb_filename
*smb_fname
;
3550 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3555 smb_fname
= synthetic_smb_fname(
3556 talloc_tos(), fname
, stream_info
[i
].name
, NULL
);
3557 if (smb_fname
== NULL
) {
3558 status
= NT_STATUS_NO_MEMORY
;
3562 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3563 DEBUG(10, ("Unable to stat stream: %s\n",
3564 smb_fname_str_dbg(smb_fname
)));
3567 status
= SMB_VFS_CREATE_FILE(
3570 0, /* root_dir_fid */
3571 smb_fname
, /* fname */
3572 DELETE_ACCESS
, /* access_mask */
3573 (FILE_SHARE_READ
| /* share_access */
3574 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3575 FILE_OPEN
, /* create_disposition*/
3576 0, /* create_options */
3577 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3578 0, /* oplock_request */
3580 0, /* allocation_size */
3581 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3584 &streams
[i
], /* result */
3587 if (!NT_STATUS_IS_OK(status
)) {
3588 DEBUG(10, ("Could not open stream %s: %s\n",
3589 smb_fname_str_dbg(smb_fname
),
3590 nt_errstr(status
)));
3592 TALLOC_FREE(smb_fname
);
3595 TALLOC_FREE(smb_fname
);
3599 * don't touch the variable "status" beyond this point :-)
3602 for (i
-= 1 ; i
>= 0; i
--) {
3603 if (streams
[i
] == NULL
) {
3607 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3608 fsp_str_dbg(streams
[i
])));
3609 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3617 /*********************************************************************
3618 Create a default ACL by inheriting from the parent. If no inheritance
3619 from the parent available, don't set anything. This will leave the actual
3620 permissions the new file or directory already got from the filesystem
3621 as the NT ACL when read.
3622 *********************************************************************/
3624 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
3626 TALLOC_CTX
*frame
= talloc_stackframe();
3627 char *parent_name
= NULL
;
3628 struct security_descriptor
*parent_desc
= NULL
;
3629 NTSTATUS status
= NT_STATUS_OK
;
3630 struct security_descriptor
*psd
= NULL
;
3631 const struct dom_sid
*owner_sid
= NULL
;
3632 const struct dom_sid
*group_sid
= NULL
;
3633 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
3634 struct security_token
*token
= fsp
->conn
->session_info
->security_token
;
3635 bool inherit_owner
= lp_inherit_owner(SNUM(fsp
->conn
));
3636 bool inheritable_components
= false;
3637 bool try_builtin_administrators
= false;
3638 const struct dom_sid
*BA_U_sid
= NULL
;
3639 const struct dom_sid
*BA_G_sid
= NULL
;
3640 bool try_system
= false;
3641 const struct dom_sid
*SY_U_sid
= NULL
;
3642 const struct dom_sid
*SY_G_sid
= NULL
;
3645 if (!parent_dirname(frame
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
3647 return NT_STATUS_NO_MEMORY
;
3650 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
3652 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
3655 if (!NT_STATUS_IS_OK(status
)) {
3660 inheritable_components
= sd_has_inheritable_components(parent_desc
,
3663 if (!inheritable_components
&& !inherit_owner
) {
3665 /* Nothing to inherit and not setting owner. */
3666 return NT_STATUS_OK
;
3669 /* Create an inherited descriptor from the parent. */
3671 if (DEBUGLEVEL
>= 10) {
3672 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3673 fsp_str_dbg(fsp
) ));
3674 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
3677 /* Inherit from parent descriptor if "inherit owner" set. */
3678 if (inherit_owner
) {
3679 owner_sid
= parent_desc
->owner_sid
;
3680 group_sid
= parent_desc
->group_sid
;
3683 if (owner_sid
== NULL
) {
3684 if (security_token_has_builtin_administrators(token
)) {
3685 try_builtin_administrators
= true;
3686 } else if (security_token_is_system(token
)) {
3687 try_builtin_administrators
= true;
3692 if (group_sid
== NULL
&&
3693 token
->num_sids
== PRIMARY_GROUP_SID_INDEX
)
3695 if (security_token_is_system(token
)) {
3696 try_builtin_administrators
= true;
3701 if (try_builtin_administrators
) {
3706 ok
= sids_to_unixids(&global_sid_Builtin_Administrators
, 1, &ids
);
3710 BA_U_sid
= &global_sid_Builtin_Administrators
;
3711 BA_G_sid
= &global_sid_Builtin_Administrators
;
3714 BA_U_sid
= &global_sid_Builtin_Administrators
;
3717 BA_G_sid
= &global_sid_Builtin_Administrators
;
3730 ok
= sids_to_unixids(&global_sid_System
, 1, &ids
);
3734 SY_U_sid
= &global_sid_System
;
3735 SY_G_sid
= &global_sid_System
;
3738 SY_U_sid
= &global_sid_System
;
3741 SY_G_sid
= &global_sid_System
;
3749 if (owner_sid
== NULL
) {
3750 owner_sid
= BA_U_sid
;
3753 if (owner_sid
== NULL
) {
3754 owner_sid
= SY_U_sid
;
3757 if (group_sid
== NULL
) {
3758 group_sid
= SY_G_sid
;
3761 if (try_system
&& group_sid
== NULL
) {
3762 group_sid
= BA_G_sid
;
3765 if (owner_sid
== NULL
) {
3766 owner_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3768 if (group_sid
== NULL
) {
3769 if (token
->num_sids
== PRIMARY_GROUP_SID_INDEX
) {
3770 group_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3772 group_sid
= &token
->sids
[PRIMARY_GROUP_SID_INDEX
];
3776 status
= se_create_child_secdesc(frame
,
3783 if (!NT_STATUS_IS_OK(status
)) {
3788 /* If inheritable_components == false,
3789 se_create_child_secdesc()
3790 creates a security desriptor with a NULL dacl
3791 entry, but with SEC_DESC_DACL_PRESENT. We need
3792 to remove that flag. */
3794 if (!inheritable_components
) {
3795 security_info_sent
&= ~SECINFO_DACL
;
3796 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
3799 if (DEBUGLEVEL
>= 10) {
3800 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3801 fsp_str_dbg(fsp
) ));
3802 NDR_PRINT_DEBUG(security_descriptor
, psd
);
3805 if (inherit_owner
) {
3806 /* We need to be root to force this. */
3809 status
= SMB_VFS_FSET_NT_ACL(fsp
,
3812 if (inherit_owner
) {
3820 * Wrapper around open_file_ntcreate and open_directory
3823 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3824 struct smb_request
*req
,
3825 struct smb_filename
*smb_fname
,
3826 uint32_t access_mask
,
3827 uint32_t share_access
,
3828 uint32_t create_disposition
,
3829 uint32_t create_options
,
3830 uint32_t file_attributes
,
3831 uint32_t oplock_request
,
3832 struct smb2_lease
*lease
,
3833 uint64_t allocation_size
,
3834 uint32_t private_flags
,
3835 struct security_descriptor
*sd
,
3836 struct ea_list
*ea_list
,
3838 files_struct
**result
,
3841 int info
= FILE_WAS_OPENED
;
3842 files_struct
*base_fsp
= NULL
;
3843 files_struct
*fsp
= NULL
;
3846 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3847 "file_attributes = 0x%x, share_access = 0x%x, "
3848 "create_disposition = 0x%x create_options = 0x%x "
3849 "oplock_request = 0x%x private_flags = 0x%x "
3850 "ea_list = 0x%p, sd = 0x%p, "
3852 (unsigned int)access_mask
,
3853 (unsigned int)file_attributes
,
3854 (unsigned int)share_access
,
3855 (unsigned int)create_disposition
,
3856 (unsigned int)create_options
,
3857 (unsigned int)oplock_request
,
3858 (unsigned int)private_flags
,
3859 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3861 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3862 status
= NT_STATUS_NOT_SUPPORTED
;
3866 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3867 status
= NT_STATUS_INVALID_PARAMETER
;
3872 oplock_request
|= INTERNAL_OPEN_ONLY
;
3875 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3876 && (access_mask
& DELETE_ACCESS
)
3877 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3879 * We can't open a file with DELETE access if any of the
3880 * streams is open without FILE_SHARE_DELETE
3882 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3884 if (!NT_STATUS_IS_OK(status
)) {
3889 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3890 !security_token_has_privilege(get_current_nttok(conn
),
3891 SEC_PRIV_SECURITY
)) {
3892 DEBUG(10, ("create_file_unixpath: open on %s "
3893 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3894 smb_fname_str_dbg(smb_fname
)));
3895 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3899 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3900 && is_ntfs_stream_smb_fname(smb_fname
)
3901 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3902 uint32 base_create_disposition
;
3903 struct smb_filename
*smb_fname_base
= NULL
;
3905 if (create_options
& FILE_DIRECTORY_FILE
) {
3906 status
= NT_STATUS_NOT_A_DIRECTORY
;
3910 switch (create_disposition
) {
3912 base_create_disposition
= FILE_OPEN
;
3915 base_create_disposition
= FILE_OPEN_IF
;
3919 /* Create an smb_filename with stream_name == NULL. */
3920 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
3921 smb_fname
->base_name
,
3923 if (smb_fname_base
== NULL
) {
3924 status
= NT_STATUS_NO_MEMORY
;
3928 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3929 DEBUG(10, ("Unable to stat stream: %s\n",
3930 smb_fname_str_dbg(smb_fname_base
)));
3933 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3934 * We need to check if the requested access mask
3935 * could be used to open the underlying file (if
3936 * it existed), as we're passing in zero for the
3937 * access mask to the base filename.
3939 status
= check_base_file_access(conn
,
3943 if (!NT_STATUS_IS_OK(status
)) {
3944 DEBUG(10, ("Permission check "
3945 "for base %s failed: "
3946 "%s\n", smb_fname
->base_name
,
3947 nt_errstr(status
)));
3952 /* Open the base file. */
3953 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3956 | FILE_SHARE_DELETE
,
3957 base_create_disposition
,
3958 0, 0, 0, NULL
, 0, 0, NULL
, NULL
,
3960 TALLOC_FREE(smb_fname_base
);
3962 if (!NT_STATUS_IS_OK(status
)) {
3963 DEBUG(10, ("create_file_unixpath for base %s failed: "
3964 "%s\n", smb_fname
->base_name
,
3965 nt_errstr(status
)));
3968 /* we don't need the low level fd */
3973 * If it's a request for a directory open, deal with it separately.
3976 if (create_options
& FILE_DIRECTORY_FILE
) {
3978 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3979 status
= NT_STATUS_INVALID_PARAMETER
;
3983 /* Can't open a temp directory. IFS kit test. */
3984 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3985 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3986 status
= NT_STATUS_INVALID_PARAMETER
;
3991 * We will get a create directory here if the Win32
3992 * app specified a security descriptor in the
3993 * CreateDirectory() call.
3997 status
= open_directory(
3998 conn
, req
, smb_fname
, access_mask
, share_access
,
3999 create_disposition
, create_options
, file_attributes
,
4004 * Ordinary file case.
4007 status
= file_new(req
, conn
, &fsp
);
4008 if(!NT_STATUS_IS_OK(status
)) {
4012 status
= fsp_set_smb_fname(fsp
, smb_fname
);
4013 if (!NT_STATUS_IS_OK(status
)) {
4019 * We're opening the stream element of a
4020 * base_fsp we already opened. Set up the
4023 fsp
->base_fsp
= base_fsp
;
4026 if (allocation_size
) {
4027 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
4031 status
= open_file_ntcreate(conn
,
4044 if(!NT_STATUS_IS_OK(status
)) {
4045 file_free(req
, fsp
);
4049 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
4051 /* A stream open never opens a directory */
4054 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4059 * Fail the open if it was explicitly a non-directory
4063 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
4064 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4069 status
= open_directory(
4070 conn
, req
, smb_fname
, access_mask
,
4071 share_access
, create_disposition
,
4072 create_options
, file_attributes
,
4077 if (!NT_STATUS_IS_OK(status
)) {
4081 fsp
->base_fsp
= base_fsp
;
4083 if ((ea_list
!= NULL
) &&
4084 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
4085 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
4086 if (!NT_STATUS_IS_OK(status
)) {
4091 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
4092 status
= NT_STATUS_ACCESS_DENIED
;
4096 /* Save the requested allocation size. */
4097 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
4099 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
4100 fsp
->initial_allocation_size
= smb_roundup(
4101 fsp
->conn
, allocation_size
);
4102 if (fsp
->is_directory
) {
4103 /* Can't set allocation size on a directory. */
4104 status
= NT_STATUS_ACCESS_DENIED
;
4107 if (vfs_allocate_file_space(
4108 fsp
, fsp
->initial_allocation_size
) == -1) {
4109 status
= NT_STATUS_DISK_FULL
;
4113 fsp
->initial_allocation_size
= smb_roundup(
4114 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
4117 fsp
->initial_allocation_size
= 0;
4120 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
4121 fsp
->base_fsp
== NULL
) {
4124 * According to the MS documentation, the only time the security
4125 * descriptor is applied to the opened file is iff we *created* the
4126 * file; an existing file stays the same.
4128 * Also, it seems (from observation) that you can open the file with
4129 * any access mask but you can still write the sd. We need to override
4130 * the granted access before we call set_sd
4131 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4134 uint32_t sec_info_sent
;
4135 uint32_t saved_access_mask
= fsp
->access_mask
;
4137 sec_info_sent
= get_sec_info(sd
);
4139 fsp
->access_mask
= FILE_GENERIC_ALL
;
4141 if (sec_info_sent
& (SECINFO_OWNER
|
4145 status
= set_sd(fsp
, sd
, sec_info_sent
);
4148 fsp
->access_mask
= saved_access_mask
;
4150 if (!NT_STATUS_IS_OK(status
)) {
4153 } else if (lp_inherit_acls(SNUM(conn
))) {
4154 /* Inherit from parent. Errors here are not fatal. */
4155 status
= inherit_new_acl(fsp
);
4156 if (!NT_STATUS_IS_OK(status
)) {
4157 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4159 nt_errstr(status
) ));
4164 if ((conn
->fs_capabilities
& FILE_FILE_COMPRESSION
)
4165 && (create_options
& FILE_NO_COMPRESSION
)
4166 && (info
== FILE_WAS_CREATED
)) {
4167 status
= SMB_VFS_SET_COMPRESSION(conn
, fsp
, fsp
,
4168 COMPRESSION_FORMAT_NONE
);
4169 if (!NT_STATUS_IS_OK(status
)) {
4170 DEBUG(1, ("failed to disable compression: %s\n",
4171 nt_errstr(status
)));
4175 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
4178 if (pinfo
!= NULL
) {
4182 smb_fname
->st
= fsp
->fsp_name
->st
;
4184 return NT_STATUS_OK
;
4187 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
4190 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
4192 * The close_file below will close
4197 close_file(req
, fsp
, ERROR_CLOSE
);
4200 if (base_fsp
!= NULL
) {
4201 close_file(req
, base_fsp
, ERROR_CLOSE
);
4208 * Calculate the full path name given a relative fid.
4210 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
4211 struct smb_request
*req
,
4212 uint16_t root_dir_fid
,
4213 const struct smb_filename
*smb_fname
,
4214 struct smb_filename
**smb_fname_out
)
4216 files_struct
*dir_fsp
;
4217 char *parent_fname
= NULL
;
4218 char *new_base_name
= NULL
;
4221 if (root_dir_fid
== 0 || !smb_fname
) {
4222 status
= NT_STATUS_INTERNAL_ERROR
;
4226 dir_fsp
= file_fsp(req
, root_dir_fid
);
4228 if (dir_fsp
== NULL
) {
4229 status
= NT_STATUS_INVALID_HANDLE
;
4233 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
4234 status
= NT_STATUS_INVALID_HANDLE
;
4238 if (!dir_fsp
->is_directory
) {
4241 * Check to see if this is a mac fork of some kind.
4244 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
4245 is_ntfs_stream_smb_fname(smb_fname
)) {
4246 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4251 we need to handle the case when we get a
4252 relative open relative to a file and the
4253 pathname is blank - this is a reopen!
4254 (hint from demyn plantenberg)
4257 status
= NT_STATUS_INVALID_HANDLE
;
4261 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
4263 * We're at the toplevel dir, the final file name
4264 * must not contain ./, as this is filtered out
4265 * normally by srvstr_get_path and unix_convert
4266 * explicitly rejects paths containing ./.
4268 parent_fname
= talloc_strdup(talloc_tos(), "");
4269 if (parent_fname
== NULL
) {
4270 status
= NT_STATUS_NO_MEMORY
;
4274 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
4277 * Copy in the base directory name.
4280 parent_fname
= talloc_array(talloc_tos(), char,
4282 if (parent_fname
== NULL
) {
4283 status
= NT_STATUS_NO_MEMORY
;
4286 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
4290 * Ensure it ends in a '/'.
4291 * We used TALLOC_SIZE +2 to add space for the '/'.
4295 && (parent_fname
[dir_name_len
-1] != '\\')
4296 && (parent_fname
[dir_name_len
-1] != '/')) {
4297 parent_fname
[dir_name_len
] = '/';
4298 parent_fname
[dir_name_len
+1] = '\0';
4302 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
4303 smb_fname
->base_name
);
4304 if (new_base_name
== NULL
) {
4305 status
= NT_STATUS_NO_MEMORY
;
4309 status
= filename_convert(req
,
4311 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
4316 if (!NT_STATUS_IS_OK(status
)) {
4321 TALLOC_FREE(parent_fname
);
4322 TALLOC_FREE(new_base_name
);
4326 NTSTATUS
create_file_default(connection_struct
*conn
,
4327 struct smb_request
*req
,
4328 uint16_t root_dir_fid
,
4329 struct smb_filename
*smb_fname
,
4330 uint32_t access_mask
,
4331 uint32_t share_access
,
4332 uint32_t create_disposition
,
4333 uint32_t create_options
,
4334 uint32_t file_attributes
,
4335 uint32_t oplock_request
,
4336 struct smb2_lease
*lease
,
4337 uint64_t allocation_size
,
4338 uint32_t private_flags
,
4339 struct security_descriptor
*sd
,
4340 struct ea_list
*ea_list
,
4341 files_struct
**result
,
4344 int info
= FILE_WAS_OPENED
;
4345 files_struct
*fsp
= NULL
;
4347 bool stream_name
= false;
4349 DEBUG(10,("create_file: access_mask = 0x%x "
4350 "file_attributes = 0x%x, share_access = 0x%x, "
4351 "create_disposition = 0x%x create_options = 0x%x "
4352 "oplock_request = 0x%x "
4353 "private_flags = 0x%x "
4354 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4356 (unsigned int)access_mask
,
4357 (unsigned int)file_attributes
,
4358 (unsigned int)share_access
,
4359 (unsigned int)create_disposition
,
4360 (unsigned int)create_options
,
4361 (unsigned int)oplock_request
,
4362 (unsigned int)private_flags
,
4363 (unsigned int)root_dir_fid
,
4364 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4367 * Calculate the filename from the root_dir_if if necessary.
4370 if (root_dir_fid
!= 0) {
4371 struct smb_filename
*smb_fname_out
= NULL
;
4372 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
4373 smb_fname
, &smb_fname_out
);
4374 if (!NT_STATUS_IS_OK(status
)) {
4377 smb_fname
= smb_fname_out
;
4381 * Check to see if this is a mac fork of some kind.
4384 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
4386 enum FAKE_FILE_TYPE fake_file_type
;
4388 fake_file_type
= is_fake_file(smb_fname
);
4390 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
4393 * Here we go! support for changing the disk quotas
4396 * We need to fake up to open this MAGIC QUOTA file
4397 * and return a valid FID.
4399 * w2k close this file directly after openening xp
4400 * also tries a QUERY_FILE_INFO on the file and then
4403 status
= open_fake_file(req
, conn
, req
->vuid
,
4404 fake_file_type
, smb_fname
,
4406 if (!NT_STATUS_IS_OK(status
)) {
4410 ZERO_STRUCT(smb_fname
->st
);
4414 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
4415 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4420 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
4422 smb_fname
->stream_name
= NULL
;
4423 /* We have to handle this error here. */
4424 if (create_options
& FILE_DIRECTORY_FILE
) {
4425 status
= NT_STATUS_NOT_A_DIRECTORY
;
4428 if (lp_posix_pathnames()) {
4429 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
4431 ret
= SMB_VFS_STAT(conn
, smb_fname
);
4434 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
4435 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4440 status
= create_file_unixpath(
4441 conn
, req
, smb_fname
, access_mask
, share_access
,
4442 create_disposition
, create_options
, file_attributes
,
4443 oplock_request
, lease
, allocation_size
, private_flags
,
4447 if (!NT_STATUS_IS_OK(status
)) {
4452 DEBUG(10, ("create_file: info=%d\n", info
));
4455 if (pinfo
!= NULL
) {
4458 return NT_STATUS_OK
;
4461 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
4464 close_file(req
, fsp
, ERROR_CLOSE
);