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"
38 #include "locking/leases_db.h"
39 #include "librpc/gen_ndr/ndr_leases_db.h"
41 extern const struct generic_mapping file_generic_mapping
;
43 struct deferred_open_record
{
44 bool delayed_for_oplocks
;
49 /****************************************************************************
50 If the requester wanted DELETE_ACCESS and was rejected because
51 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
53 ****************************************************************************/
55 static bool parent_override_delete(connection_struct
*conn
,
56 const struct smb_filename
*smb_fname
,
58 uint32_t rejected_mask
)
60 if ((access_mask
& DELETE_ACCESS
) &&
61 (rejected_mask
& DELETE_ACCESS
) &&
62 can_delete_file_in_directory(conn
, smb_fname
)) {
68 /****************************************************************************
69 Check if we have open rights.
70 ****************************************************************************/
72 NTSTATUS
smbd_check_access_rights(struct connection_struct
*conn
,
73 const struct smb_filename
*smb_fname
,
77 /* Check if we have rights to open. */
79 struct security_descriptor
*sd
= NULL
;
80 uint32_t rejected_share_access
;
81 uint32_t rejected_mask
= access_mask
;
82 uint32_t do_not_check_mask
= 0;
84 rejected_share_access
= access_mask
& ~(conn
->share_access
);
86 if (rejected_share_access
) {
87 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
89 (unsigned int)access_mask
,
90 smb_fname_str_dbg(smb_fname
),
91 (unsigned int)rejected_share_access
));
92 return NT_STATUS_ACCESS_DENIED
;
95 if (!use_privs
&& get_current_uid(conn
) == (uid_t
)0) {
96 /* I'm sorry sir, I didn't know you were root... */
97 DEBUG(10,("smbd_check_access_rights: root override "
98 "on %s. Granting 0x%x\n",
99 smb_fname_str_dbg(smb_fname
),
100 (unsigned int)access_mask
));
104 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
105 DEBUG(10,("smbd_check_access_rights: not checking ACL "
106 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
107 smb_fname_str_dbg(smb_fname
),
108 (unsigned int)access_mask
));
112 if (access_mask
== DELETE_ACCESS
&&
113 VALID_STAT(smb_fname
->st
) &&
114 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
115 /* We can always delete a symlink. */
116 DEBUG(10,("smbd_check_access_rights: not checking ACL "
117 "on DELETE_ACCESS on symlink %s.\n",
118 smb_fname_str_dbg(smb_fname
) ));
122 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
,
125 SECINFO_DACL
), talloc_tos(), &sd
);
127 if (!NT_STATUS_IS_OK(status
)) {
128 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
130 smb_fname_str_dbg(smb_fname
),
133 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
141 * If we can access the path to this file, by
142 * default we have FILE_READ_ATTRIBUTES from the
143 * containing directory. See the section:
144 * "Algorithm to Check Access to an Existing File"
147 * se_file_access_check() also takes care of
148 * owner WRITE_DAC and READ_CONTROL.
150 do_not_check_mask
= FILE_READ_ATTRIBUTES
;
153 * Samba 3.6 and earlier granted execute access even
154 * if the ACL did not contain execute rights.
155 * Samba 4.0 is more correct and checks it.
156 * The compatibilty mode allows one to skip this check
157 * to smoothen upgrades.
159 if (lp_acl_allow_execute_always(SNUM(conn
))) {
160 do_not_check_mask
|= FILE_EXECUTE
;
163 status
= se_file_access_check(sd
,
164 get_current_nttok(conn
),
166 (access_mask
& ~do_not_check_mask
),
169 DEBUG(10,("smbd_check_access_rights: file %s requesting "
170 "0x%x returning 0x%x (%s)\n",
171 smb_fname_str_dbg(smb_fname
),
172 (unsigned int)access_mask
,
173 (unsigned int)rejected_mask
,
174 nt_errstr(status
) ));
176 if (!NT_STATUS_IS_OK(status
)) {
177 if (DEBUGLEVEL
>= 10) {
178 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
179 smb_fname_str_dbg(smb_fname
) ));
180 NDR_PRINT_DEBUG(security_descriptor
, sd
);
186 if (NT_STATUS_IS_OK(status
) ||
187 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
191 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
195 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
196 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
197 !lp_store_dos_attributes(SNUM(conn
)) &&
198 (lp_map_readonly(SNUM(conn
)) ||
199 lp_map_archive(SNUM(conn
)) ||
200 lp_map_hidden(SNUM(conn
)) ||
201 lp_map_system(SNUM(conn
)))) {
202 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
204 DEBUG(10,("smbd_check_access_rights: "
206 "FILE_WRITE_ATTRIBUTES "
208 smb_fname_str_dbg(smb_fname
)));
211 if (parent_override_delete(conn
,
215 /* Were we trying to do an open
216 * for delete and didn't get DELETE
217 * access (only) ? Check if the
218 * directory allows DELETE_CHILD.
220 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
223 rejected_mask
&= ~DELETE_ACCESS
;
225 DEBUG(10,("smbd_check_access_rights: "
229 smb_fname_str_dbg(smb_fname
)));
232 if (rejected_mask
!= 0) {
233 return NT_STATUS_ACCESS_DENIED
;
238 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
239 struct smb_filename
*smb_fname
,
240 uint32_t access_mask
)
243 char *parent_dir
= NULL
;
244 struct security_descriptor
*parent_sd
= NULL
;
245 uint32_t access_granted
= 0;
246 struct smb_filename
*parent_smb_fname
= NULL
;
248 if (!parent_dirname(talloc_tos(),
249 smb_fname
->base_name
,
252 return NT_STATUS_NO_MEMORY
;
255 parent_smb_fname
= synthetic_smb_fname(talloc_tos(),
260 if (parent_smb_fname
== NULL
) {
261 return NT_STATUS_NO_MEMORY
;
264 if (get_current_uid(conn
) == (uid_t
)0) {
265 /* I'm sorry sir, I didn't know you were root... */
266 DEBUG(10,("check_parent_access: root override "
267 "on %s. Granting 0x%x\n",
268 smb_fname_str_dbg(smb_fname
),
269 (unsigned int)access_mask
));
273 status
= SMB_VFS_GET_NT_ACL(conn
,
279 if (!NT_STATUS_IS_OK(status
)) {
280 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
281 "%s with error %s\n",
288 * If we can access the path to this file, by
289 * default we have FILE_READ_ATTRIBUTES from the
290 * containing directory. See the section:
291 * "Algorithm to Check Access to an Existing File"
294 * se_file_access_check() also takes care of
295 * owner WRITE_DAC and READ_CONTROL.
297 status
= se_file_access_check(parent_sd
,
298 get_current_nttok(conn
),
300 (access_mask
& ~FILE_READ_ATTRIBUTES
),
302 if(!NT_STATUS_IS_OK(status
)) {
303 DEBUG(5,("check_parent_access: access check "
304 "on directory %s for "
305 "path %s for mask 0x%x returned (0x%x) %s\n",
307 smb_fname
->base_name
,
310 nt_errstr(status
) ));
317 /****************************************************************************
318 Ensure when opening a base file for a stream open that we have permissions
319 to do so given the access mask on the base file.
320 ****************************************************************************/
322 static NTSTATUS
check_base_file_access(struct connection_struct
*conn
,
323 struct smb_filename
*smb_fname
,
324 uint32_t access_mask
)
328 status
= smbd_calculate_access_mask(conn
, smb_fname
,
332 if (!NT_STATUS_IS_OK(status
)) {
333 DEBUG(10, ("smbd_calculate_access_mask "
334 "on file %s returned %s\n",
335 smb_fname_str_dbg(smb_fname
),
340 if (access_mask
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) {
342 if (!CAN_WRITE(conn
)) {
343 return NT_STATUS_ACCESS_DENIED
;
345 dosattrs
= dos_mode(conn
, smb_fname
);
346 if (IS_DOS_READONLY(dosattrs
)) {
347 return NT_STATUS_ACCESS_DENIED
;
351 return smbd_check_access_rights(conn
,
357 /****************************************************************************
358 fd support routines - attempt to do a dos_open.
359 ****************************************************************************/
361 NTSTATUS
fd_open(struct connection_struct
*conn
,
366 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
367 NTSTATUS status
= NT_STATUS_OK
;
371 * Never follow symlinks on a POSIX client. The
372 * client should be doing this.
375 if ((fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) || !lp_follow_symlinks(SNUM(conn
))) {
380 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
381 if (fsp
->fh
->fd
== -1) {
382 int posix_errno
= errno
;
384 #if defined(ENOTSUP) && defined(OSF1)
385 /* handle special Tru64 errno */
386 if (errno
== ENOTSUP
) {
391 /* fix broken NetBSD errno */
392 if (errno
== EFTYPE
) {
396 /* fix broken FreeBSD errno */
397 if (errno
== EMLINK
) {
400 #endif /* O_NOFOLLOW */
401 status
= map_nt_error_from_unix(posix_errno
);
402 if (errno
== EMFILE
) {
403 static time_t last_warned
= 0L;
405 if (time((time_t *) NULL
) > last_warned
) {
406 DEBUG(0,("Too many open files, unable "
407 "to open more! smbd's max "
409 lp_max_open_files()));
410 last_warned
= time((time_t *) NULL
);
416 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
417 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
418 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
423 /****************************************************************************
424 Close the file associated with a fsp.
425 ****************************************************************************/
427 NTSTATUS
fd_close(files_struct
*fsp
)
434 if (fsp
->fh
->fd
== -1) {
435 return NT_STATUS_OK
; /* What we used to call a stat open. */
437 if (fsp
->fh
->ref_count
> 1) {
438 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
441 ret
= SMB_VFS_CLOSE(fsp
);
444 return map_nt_error_from_unix(errno
);
449 /****************************************************************************
450 Change the ownership of a file to that of the parent directory.
451 Do this by fd if possible.
452 ****************************************************************************/
454 void change_file_owner_to_parent(connection_struct
*conn
,
455 const char *inherit_from_dir
,
458 struct smb_filename
*smb_fname_parent
;
461 smb_fname_parent
= synthetic_smb_fname(talloc_tos(),
466 if (smb_fname_parent
== NULL
) {
470 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
472 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
473 "directory %s. Error was %s\n",
474 smb_fname_str_dbg(smb_fname_parent
),
476 TALLOC_FREE(smb_fname_parent
);
480 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
481 /* Already this uid - no need to change. */
482 DEBUG(10,("change_file_owner_to_parent: file %s "
483 "is already owned by uid %d\n",
485 (int)fsp
->fsp_name
->st
.st_ex_uid
));
486 TALLOC_FREE(smb_fname_parent
);
491 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
494 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
495 "file %s to parent directory uid %u. Error "
496 "was %s\n", fsp_str_dbg(fsp
),
497 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
500 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
501 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
502 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
503 /* Ensure the uid entry is updated. */
504 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
507 TALLOC_FREE(smb_fname_parent
);
510 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
511 const char *inherit_from_dir
,
513 SMB_STRUCT_STAT
*psbuf
)
515 struct smb_filename
*smb_fname_parent
;
516 struct smb_filename
*smb_fname_cwd
= NULL
;
517 char *saved_dir
= NULL
;
518 TALLOC_CTX
*ctx
= talloc_tos();
519 NTSTATUS status
= NT_STATUS_OK
;
522 smb_fname_parent
= synthetic_smb_fname(ctx
,
527 if (smb_fname_parent
== NULL
) {
528 return NT_STATUS_NO_MEMORY
;
531 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
533 status
= map_nt_error_from_unix(errno
);
534 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
535 "directory %s. Error was %s\n",
536 smb_fname_str_dbg(smb_fname_parent
),
541 /* We've already done an lstat into psbuf, and we know it's a
542 directory. If we can cd into the directory and the dev/ino
543 are the same then we can safely chown without races as
544 we're locking the directory in place by being in it. This
545 should work on any UNIX (thanks tridge :-). JRA.
548 saved_dir
= vfs_GetWd(ctx
,conn
);
550 status
= map_nt_error_from_unix(errno
);
551 DEBUG(0,("change_dir_owner_to_parent: failed to get "
552 "current working directory. Error was %s\n",
557 /* Chdir into the new path. */
558 if (vfs_ChDir(conn
, fname
) == -1) {
559 status
= map_nt_error_from_unix(errno
);
560 DEBUG(0,("change_dir_owner_to_parent: failed to change "
561 "current working directory to %s. Error "
562 "was %s\n", fname
, strerror(errno
) ));
566 smb_fname_cwd
= synthetic_smb_fname(ctx
, ".", NULL
, NULL
, 0);
567 if (smb_fname_cwd
== NULL
) {
568 status
= NT_STATUS_NO_MEMORY
;
572 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
574 status
= map_nt_error_from_unix(errno
);
575 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
576 "directory '.' (%s) Error was %s\n",
577 fname
, strerror(errno
)));
581 /* Ensure we're pointing at the same place. */
582 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
583 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
584 DEBUG(0,("change_dir_owner_to_parent: "
585 "device/inode on directory %s changed. "
586 "Refusing to chown !\n", fname
));
587 status
= NT_STATUS_ACCESS_DENIED
;
591 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
592 /* Already this uid - no need to change. */
593 DEBUG(10,("change_dir_owner_to_parent: directory %s "
594 "is already owned by uid %d\n",
596 (int)smb_fname_cwd
->st
.st_ex_uid
));
597 status
= NT_STATUS_OK
;
602 ret
= SMB_VFS_LCHOWN(conn
,
604 smb_fname_parent
->st
.st_ex_uid
,
608 status
= map_nt_error_from_unix(errno
);
609 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
610 "directory %s to parent directory uid %u. "
611 "Error was %s\n", fname
,
612 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
615 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
616 "directory %s to parent directory uid %u.\n",
617 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
618 /* Ensure the uid entry is updated. */
619 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
623 vfs_ChDir(conn
,saved_dir
);
625 TALLOC_FREE(smb_fname_parent
);
626 TALLOC_FREE(smb_fname_cwd
);
630 /****************************************************************************
631 Open a file - returning a guaranteed ATOMIC indication of if the
632 file was created or not.
633 ****************************************************************************/
635 static NTSTATUS
fd_open_atomic(struct connection_struct
*conn
,
641 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
642 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
644 *file_created
= false;
646 if (!(flags
& O_CREAT
)) {
648 * We're not creating the file, just pass through.
650 return fd_open(conn
, fsp
, flags
, mode
);
653 if (flags
& O_EXCL
) {
655 * Fail if already exists, just pass through.
657 status
= fd_open(conn
, fsp
, flags
, mode
);
660 * Here we've opened with O_CREAT|O_EXCL. If that went
661 * NT_STATUS_OK, we *know* we created this file.
663 *file_created
= NT_STATUS_IS_OK(status
);
669 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
670 * To know absolutely if we created the file or not,
671 * we can never call O_CREAT without O_EXCL. So if
672 * we think the file existed, try without O_CREAT|O_EXCL.
673 * If we think the file didn't exist, try with
674 * O_CREAT|O_EXCL. Keep bouncing between these two
675 * requests until either the file is created, or
676 * opened. Either way, we keep going until we get
677 * a returnable result (error, or open/create).
681 int curr_flags
= flags
;
684 /* Just try open, do not create. */
685 curr_flags
&= ~(O_CREAT
);
686 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
687 if (NT_STATUS_EQUAL(status
,
688 NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
690 * Someone deleted it in the meantime.
693 file_existed
= false;
694 DEBUG(10,("fd_open_atomic: file %s existed. "
696 smb_fname_str_dbg(fsp
->fsp_name
)));
700 /* Try create exclusively, fail if it exists. */
701 curr_flags
|= O_EXCL
;
702 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
703 if (NT_STATUS_EQUAL(status
,
704 NT_STATUS_OBJECT_NAME_COLLISION
)) {
706 * Someone created it in the meantime.
707 * Retry without O_CREAT.
710 DEBUG(10,("fd_open_atomic: file %s "
711 "did not exist. Retry.\n",
712 smb_fname_str_dbg(fsp
->fsp_name
)));
715 if (NT_STATUS_IS_OK(status
)) {
717 * Here we've opened with O_CREAT|O_EXCL
718 * and got success. We *know* we created
721 *file_created
= true;
724 /* Create is done, or failed. */
730 /****************************************************************************
732 ****************************************************************************/
734 static NTSTATUS
open_file(files_struct
*fsp
,
735 connection_struct
*conn
,
736 struct smb_request
*req
,
737 const char *parent_dir
,
740 uint32_t access_mask
, /* client requested access mask. */
741 uint32_t open_access_mask
, /* what we're actually using in the open. */
742 bool *p_file_created
)
744 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
745 NTSTATUS status
= NT_STATUS_OK
;
746 int accmode
= (flags
& O_ACCMODE
);
747 int local_flags
= flags
;
748 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
753 /* Check permissions */
756 * This code was changed after seeing a client open request
757 * containing the open mode of (DENY_WRITE/read-only) with
758 * the 'create if not exist' bit set. The previous code
759 * would fail to open the file read only on a read-only share
760 * as it was checking the flags parameter directly against O_RDONLY,
761 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
765 if (!CAN_WRITE(conn
)) {
766 /* It's a read-only share - fail if we wanted to write. */
767 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
768 DEBUG(3,("Permission denied opening %s\n",
769 smb_fname_str_dbg(smb_fname
)));
770 return NT_STATUS_ACCESS_DENIED
;
772 if (flags
& O_CREAT
) {
773 /* We don't want to write - but we must make sure that
774 O_CREAT doesn't create the file if we have write
775 access into the directory.
777 flags
&= ~(O_CREAT
|O_EXCL
);
778 local_flags
&= ~(O_CREAT
|O_EXCL
);
783 * This little piece of insanity is inspired by the
784 * fact that an NT client can open a file for O_RDONLY,
785 * but set the create disposition to FILE_EXISTS_TRUNCATE.
786 * If the client *can* write to the file, then it expects to
787 * truncate the file, even though it is opening for readonly.
788 * Quicken uses this stupid trick in backup file creation...
789 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
790 * for helping track this one down. It didn't bite us in 2.0.x
791 * as we always opened files read-write in that release. JRA.
794 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
795 DEBUG(10,("open_file: truncate requested on read-only open "
796 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
797 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
800 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
801 (!file_existed
&& (local_flags
& O_CREAT
)) ||
802 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
806 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
808 * We would block on opening a FIFO with no one else on the
809 * other end. Do what we used to do and add O_NONBLOCK to the
813 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
814 local_flags
&= ~O_TRUNC
; /* Can't truncate a FIFO. */
815 local_flags
|= O_NONBLOCK
;
819 /* Don't create files with Microsoft wildcard characters. */
822 * wildcard characters are allowed in stream names
823 * only test the basefilename
825 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
827 wild
= smb_fname
->base_name
;
829 if ((local_flags
& O_CREAT
) && !file_existed
&&
830 !(fsp
->posix_flags
& FSP_POSIX_FLAGS_PATHNAMES
) &&
832 return NT_STATUS_OBJECT_NAME_INVALID
;
835 /* Can we access this file ? */
836 if (!fsp
->base_fsp
) {
837 /* Only do this check on non-stream open. */
839 status
= smbd_check_access_rights(conn
,
844 if (!NT_STATUS_IS_OK(status
)) {
845 DEBUG(10, ("open_file: "
846 "smbd_check_access_rights "
847 "on file %s returned %s\n",
848 smb_fname_str_dbg(smb_fname
),
852 if (!NT_STATUS_IS_OK(status
) &&
853 !NT_STATUS_EQUAL(status
,
854 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
859 if (NT_STATUS_EQUAL(status
,
860 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
862 DEBUG(10, ("open_file: "
863 "file %s vanished since we "
864 "checked for existence.\n",
865 smb_fname_str_dbg(smb_fname
)));
866 file_existed
= false;
867 SET_STAT_INVALID(fsp
->fsp_name
->st
);
872 if (!(local_flags
& O_CREAT
)) {
873 /* File didn't exist and no O_CREAT. */
874 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
877 status
= check_parent_access(conn
,
880 if (!NT_STATUS_IS_OK(status
)) {
881 DEBUG(10, ("open_file: "
882 "check_parent_access on "
883 "file %s returned %s\n",
884 smb_fname_str_dbg(smb_fname
),
885 nt_errstr(status
) ));
892 * Actually do the open - if O_TRUNC is needed handle it
893 * below under the share mode lock.
895 status
= fd_open_atomic(conn
, fsp
, local_flags
& ~O_TRUNC
,
896 unx_mode
, p_file_created
);
897 if (!NT_STATUS_IS_OK(status
)) {
898 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
899 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
900 nt_errstr(status
),local_flags
,flags
));
904 if (local_flags
& O_NONBLOCK
) {
906 * GPFS can return ETIMEDOUT for pread on
907 * nonblocking file descriptors when files
908 * migrated to tape need to be recalled. I
909 * could imagine this happens elsehwere
910 * too. With blocking file descriptors this
913 ret
= set_blocking(fsp
->fh
->fd
, true);
915 status
= map_nt_error_from_unix(errno
);
916 DBG_WARNING("Could not set fd to blocking: "
917 "%s\n", strerror(errno
));
923 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
925 /* If we have an fd, this stat should succeed. */
926 DEBUG(0,("Error doing fstat on open file %s "
928 smb_fname_str_dbg(smb_fname
),
930 status
= map_nt_error_from_unix(errno
);
935 if (*p_file_created
) {
936 /* We created this file. */
938 bool need_re_stat
= false;
939 /* Do all inheritance work after we've
940 done a successful fstat call and filled
941 in the stat struct in fsp->fsp_name. */
943 /* Inherit the ACL if required */
944 if (lp_inherit_permissions(SNUM(conn
))) {
945 inherit_access_posix_acl(conn
, parent_dir
,
946 smb_fname
->base_name
,
951 /* Change the owner if required. */
952 if (lp_inherit_owner(SNUM(conn
)) != INHERIT_OWNER_NO
) {
953 change_file_owner_to_parent(conn
, parent_dir
,
959 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
960 /* If we have an fd, this stat should succeed. */
962 DEBUG(0,("Error doing fstat on open file %s "
964 smb_fname_str_dbg(smb_fname
),
969 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
970 FILE_NOTIFY_CHANGE_FILE_NAME
,
971 smb_fname
->base_name
);
974 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
976 /* File must exist for a stat open. */
977 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
980 status
= smbd_check_access_rights(conn
,
985 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
986 (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) &&
987 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
988 /* This is a POSIX stat open for delete
989 * or rename on a symlink that points
991 DEBUG(10,("open_file: allowing POSIX "
992 "open on bad symlink %s\n",
993 smb_fname_str_dbg(smb_fname
)));
994 status
= NT_STATUS_OK
;
997 if (!NT_STATUS_IS_OK(status
)) {
998 DEBUG(10,("open_file: "
999 "smbd_check_access_rights on file "
1001 smb_fname_str_dbg(smb_fname
),
1002 nt_errstr(status
) ));
1008 * POSIX allows read-only opens of directories. We don't
1009 * want to do this (we use a different code path for this)
1010 * so catch a directory open and return an EISDIR. JRA.
1013 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1016 return NT_STATUS_FILE_IS_A_DIRECTORY
;
1019 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1020 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
1021 fsp
->file_pid
= req
? req
->smbpid
: 0;
1022 fsp
->can_lock
= True
;
1023 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
1026 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
1027 fsp
->print_file
= NULL
;
1028 fsp
->modified
= False
;
1029 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
1030 fsp
->is_directory
= False
;
1031 if (conn
->aio_write_behind_list
&&
1032 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
1033 conn
->case_sensitive
)) {
1034 fsp
->aio_write_behind
= True
;
1037 fsp
->wcp
= NULL
; /* Write cache pointer. */
1039 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1040 conn
->session_info
->unix_info
->unix_name
,
1041 smb_fname_str_dbg(smb_fname
),
1042 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
1043 conn
->num_files_open
));
1046 return NT_STATUS_OK
;
1049 /****************************************************************************
1050 Check if we can open a file with a share mode.
1051 Returns True if conflict, False if not.
1052 ****************************************************************************/
1054 static bool share_conflict(struct share_mode_entry
*entry
,
1055 uint32_t access_mask
,
1056 uint32_t share_access
)
1058 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1059 "entry->share_access = 0x%x, "
1060 "entry->private_options = 0x%x\n",
1061 (unsigned int)entry
->access_mask
,
1062 (unsigned int)entry
->share_access
,
1063 (unsigned int)entry
->private_options
));
1065 if (server_id_is_disconnected(&entry
->pid
)) {
1067 * note: cleanup should have been done by
1068 * delay_for_batch_oplocks()
1073 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1074 (unsigned int)access_mask
, (unsigned int)share_access
));
1076 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
1080 DELETE_ACCESS
)) == 0) {
1081 DEBUG(10,("share_conflict: No conflict due to "
1082 "entry->access_mask = 0x%x\n",
1083 (unsigned int)entry
->access_mask
));
1087 if ((access_mask
& (FILE_WRITE_DATA
|
1091 DELETE_ACCESS
)) == 0) {
1092 DEBUG(10,("share_conflict: No conflict due to "
1093 "access_mask = 0x%x\n",
1094 (unsigned int)access_mask
));
1098 #if 1 /* JRA TEST - Superdebug. */
1099 #define CHECK_MASK(num, am, right, sa, share) \
1100 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1101 (unsigned int)(num), (unsigned int)(am), \
1102 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1103 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1104 (unsigned int)(num), (unsigned int)(sa), \
1105 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1106 if (((am) & (right)) && !((sa) & (share))) { \
1107 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1108 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1109 (unsigned int)(share) )); \
1113 #define CHECK_MASK(num, am, right, sa, share) \
1114 if (((am) & (right)) && !((sa) & (share))) { \
1115 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1116 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1117 (unsigned int)(share) )); \
1122 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1123 share_access
, FILE_SHARE_WRITE
);
1124 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1125 entry
->share_access
, FILE_SHARE_WRITE
);
1127 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1128 share_access
, FILE_SHARE_READ
);
1129 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1130 entry
->share_access
, FILE_SHARE_READ
);
1132 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
1133 share_access
, FILE_SHARE_DELETE
);
1134 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
1135 entry
->share_access
, FILE_SHARE_DELETE
);
1137 DEBUG(10,("share_conflict: No conflict.\n"));
1141 #if defined(DEVELOPER)
1142 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
1144 struct share_mode_entry
*share_entry
)
1146 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1149 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1153 if (share_entry
->op_mid
== 0) {
1154 /* INTERNAL_OPEN_ONLY */
1158 if (!is_valid_share_mode_entry(share_entry
)) {
1162 fsp
= file_find_dif(sconn
, share_entry
->id
,
1163 share_entry
->share_file_id
);
1165 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1166 share_mode_str(talloc_tos(), num
, share_entry
) ));
1167 smb_panic("validate_my_share_entries: Cannot match a "
1168 "share entry with an open file\n");
1171 if (((uint16_t)fsp
->oplock_type
) != share_entry
->op_type
) {
1180 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1181 share_mode_str(talloc_tos(), num
, share_entry
) ));
1182 str
= talloc_asprintf(talloc_tos(),
1183 "validate_my_share_entries: "
1184 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1185 fsp
->fsp_name
->base_name
,
1186 (unsigned int)fsp
->oplock_type
,
1187 (unsigned int)share_entry
->op_type
);
1193 bool is_stat_open(uint32_t access_mask
)
1195 const uint32_t stat_open_bits
=
1196 (SYNCHRONIZE_ACCESS
|
1197 FILE_READ_ATTRIBUTES
|
1198 FILE_WRITE_ATTRIBUTES
);
1200 return (((access_mask
& stat_open_bits
) != 0) &&
1201 ((access_mask
& ~stat_open_bits
) == 0));
1204 static bool has_delete_on_close(struct share_mode_lock
*lck
,
1207 struct share_mode_data
*d
= lck
->data
;
1210 if (d
->num_share_modes
== 0) {
1213 if (!is_delete_on_close_set(lck
, name_hash
)) {
1216 for (i
=0; i
<d
->num_share_modes
; i
++) {
1217 if (!share_mode_stale_pid(d
, i
)) {
1224 /****************************************************************************
1225 Deal with share modes
1226 Invariant: Share mode must be locked on entry and exit.
1227 Returns -1 on error, or number of share modes on success (may be zero).
1228 ****************************************************************************/
1230 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1231 struct share_mode_lock
*lck
,
1232 uint32_t access_mask
,
1233 uint32_t share_access
)
1237 if(lck
->data
->num_share_modes
== 0) {
1238 return NT_STATUS_OK
;
1241 if (is_stat_open(access_mask
)) {
1242 /* Stat open that doesn't trigger oplock breaks or share mode
1243 * checks... ! JRA. */
1244 return NT_STATUS_OK
;
1248 * Check if the share modes will give us access.
1251 #if defined(DEVELOPER)
1252 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1253 validate_my_share_entries(conn
->sconn
, i
,
1254 &lck
->data
->share_modes
[i
]);
1258 /* Now we check the share modes, after any oplock breaks. */
1259 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1261 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1265 /* someone else has a share lock on it, check to see if we can
1267 if (share_conflict(&lck
->data
->share_modes
[i
],
1268 access_mask
, share_access
)) {
1270 if (share_mode_stale_pid(lck
->data
, i
)) {
1274 return NT_STATUS_SHARING_VIOLATION
;
1278 return NT_STATUS_OK
;
1282 * Send a break message to the oplock holder and delay the open for
1286 NTSTATUS
send_break_message(struct messaging_context
*msg_ctx
,
1287 const struct share_mode_entry
*exclusive
,
1291 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1292 struct server_id_buf tmp
;
1294 DEBUG(10, ("Sending break request to PID %s\n",
1295 server_id_str_buf(exclusive
->pid
, &tmp
)));
1297 /* Create the message. */
1298 share_mode_entry_to_message(msg
, exclusive
);
1300 /* Overload entry->op_type */
1302 * This is a cut from uint32_t to uint16_t, but so far only the lower 3
1303 * bits (LEASE_WRITE/HANDLE/READ are used anyway.
1305 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
, break_to
);
1307 status
= messaging_send_buf(msg_ctx
, exclusive
->pid
,
1308 MSG_SMB_BREAK_REQUEST
,
1309 (uint8_t *)msg
, sizeof(msg
));
1310 if (!NT_STATUS_IS_OK(status
)) {
1311 DEBUG(3, ("Could not send oplock break message: %s\n",
1312 nt_errstr(status
)));
1319 * Do internal consistency checks on the share mode for a file.
1322 static bool validate_oplock_types(struct share_mode_lock
*lck
)
1324 struct share_mode_data
*d
= lck
->data
;
1326 bool ex_or_batch
= false;
1327 bool level2
= false;
1328 bool no_oplock
= false;
1329 uint32_t num_non_stat_opens
= 0;
1332 for (i
=0; i
<d
->num_share_modes
; i
++) {
1333 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1335 if (!is_valid_share_mode_entry(e
)) {
1339 if (e
->op_mid
== 0) {
1340 /* INTERNAL_OPEN_ONLY */
1344 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1345 /* We ignore stat opens in the table - they
1346 always have NO_OPLOCK and never get or
1347 cause breaks. JRA. */
1351 num_non_stat_opens
+= 1;
1353 if (BATCH_OPLOCK_TYPE(e
->op_type
)) {
1354 /* batch - can only be one. */
1355 if (share_mode_stale_pid(d
, i
)) {
1356 DEBUG(10, ("Found stale batch oplock\n"));
1359 if (ex_or_batch
|| batch
|| level2
|| no_oplock
) {
1360 DEBUG(0, ("Bad batch oplock entry %u.",
1367 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1368 if (share_mode_stale_pid(d
, i
)) {
1369 DEBUG(10, ("Found stale duplicate oplock\n"));
1372 /* Exclusive or batch - can only be one. */
1373 if (ex_or_batch
|| level2
|| no_oplock
) {
1374 DEBUG(0, ("Bad exclusive or batch oplock "
1375 "entry %u.", (unsigned)i
));
1381 if (LEVEL_II_OPLOCK_TYPE(e
->op_type
)) {
1382 if (batch
|| ex_or_batch
) {
1383 if (share_mode_stale_pid(d
, i
)) {
1384 DEBUG(10, ("Found stale LevelII "
1388 DEBUG(0, ("Bad levelII oplock entry %u.",
1395 if (e
->op_type
== NO_OPLOCK
) {
1396 if (batch
|| ex_or_batch
) {
1397 if (share_mode_stale_pid(d
, i
)) {
1398 DEBUG(10, ("Found stale NO_OPLOCK "
1402 DEBUG(0, ("Bad no oplock entry %u.",
1410 remove_stale_share_mode_entries(d
);
1412 if ((batch
|| ex_or_batch
) && (num_non_stat_opens
!= 1)) {
1413 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1414 (int)batch
, (int)ex_or_batch
,
1415 (int)d
->num_share_modes
));
1422 static bool delay_for_oplock(files_struct
*fsp
,
1424 const struct smb2_lease
*lease
,
1425 struct share_mode_lock
*lck
,
1426 bool have_sharing_violation
,
1427 uint32_t create_disposition
,
1428 bool first_open_attempt
)
1430 struct share_mode_data
*d
= lck
->data
;
1433 bool will_overwrite
;
1435 if ((oplock_request
& INTERNAL_OPEN_ONLY
) ||
1436 is_stat_open(fsp
->access_mask
)) {
1440 switch (create_disposition
) {
1441 case FILE_SUPERSEDE
:
1442 case FILE_OVERWRITE
:
1443 case FILE_OVERWRITE_IF
:
1444 will_overwrite
= true;
1447 will_overwrite
= false;
1451 for (i
=0; i
<d
->num_share_modes
; i
++) {
1452 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1453 struct share_mode_lease
*l
= NULL
;
1454 uint32_t e_lease_type
= get_lease_type(d
, e
);
1456 uint32_t delay_mask
= 0;
1458 if (e
->op_type
== LEASE_OPLOCK
) {
1459 l
= &d
->leases
[e
->lease_idx
];
1462 if (have_sharing_violation
) {
1463 delay_mask
= SMB2_LEASE_HANDLE
;
1465 delay_mask
= SMB2_LEASE_WRITE
;
1468 break_to
= e_lease_type
& ~delay_mask
;
1470 if (will_overwrite
) {
1472 * we'll decide about SMB2_LEASE_READ later.
1474 * Maybe the break will be defered
1476 break_to
&= ~SMB2_LEASE_HANDLE
;
1479 DEBUG(10, ("entry %u: e_lease_type %u, will_overwrite: %u\n",
1480 (unsigned)i
, (unsigned)e_lease_type
,
1481 (unsigned)will_overwrite
));
1483 if (lease
!= NULL
&& l
!= NULL
) {
1486 ign
= smb2_lease_equal(fsp_client_guid(fsp
),
1495 if ((e_lease_type
& ~break_to
) == 0) {
1496 if (l
!= NULL
&& l
->breaking
) {
1502 if (share_mode_stale_pid(d
, i
)) {
1506 if (will_overwrite
) {
1508 * If we break anyway break to NONE directly.
1509 * Otherwise vfs_set_filelen() will trigger the
1512 break_to
&= ~(SMB2_LEASE_READ
|SMB2_LEASE_WRITE
);
1515 if (e
->op_type
!= LEASE_OPLOCK
) {
1517 * Oplocks only support breaking to R or NONE.
1519 break_to
&= ~(SMB2_LEASE_HANDLE
|SMB2_LEASE_WRITE
);
1522 DEBUG(10, ("breaking from %d to %d\n",
1523 (int)e_lease_type
, (int)break_to
));
1524 send_break_message(fsp
->conn
->sconn
->msg_ctx
, e
,
1526 if (e_lease_type
& delay_mask
) {
1529 if (l
!= NULL
&& l
->breaking
&& !first_open_attempt
) {
1538 static bool file_has_brlocks(files_struct
*fsp
)
1540 struct byte_range_lock
*br_lck
;
1542 br_lck
= brl_get_locks_readonly(fsp
);
1546 return (brl_num_locks(br_lck
) > 0);
1549 int find_share_mode_lease(struct share_mode_data
*d
,
1550 const struct GUID
*client_guid
,
1551 const struct smb2_lease_key
*key
)
1555 for (i
=0; i
<d
->num_leases
; i
++) {
1556 struct share_mode_lease
*l
= &d
->leases
[i
];
1558 if (smb2_lease_equal(client_guid
,
1569 struct fsp_lease
*find_fsp_lease(struct files_struct
*new_fsp
,
1570 const struct smb2_lease_key
*key
,
1571 const struct share_mode_lease
*l
)
1573 struct files_struct
*fsp
;
1576 * TODO: Measure how expensive this loop is with thousands of open
1580 for (fsp
= file_find_di_first(new_fsp
->conn
->sconn
, new_fsp
->file_id
);
1582 fsp
= file_find_di_next(fsp
)) {
1584 if (fsp
== new_fsp
) {
1587 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
1590 if (smb2_lease_key_equal(&fsp
->lease
->lease
.lease_key
, key
)) {
1591 fsp
->lease
->ref_count
+= 1;
1596 /* Not found - must be leased in another smbd. */
1597 new_fsp
->lease
= talloc_zero(new_fsp
->conn
->sconn
, struct fsp_lease
);
1598 if (new_fsp
->lease
== NULL
) {
1601 new_fsp
->lease
->ref_count
= 1;
1602 new_fsp
->lease
->sconn
= new_fsp
->conn
->sconn
;
1603 new_fsp
->lease
->lease
.lease_key
= *key
;
1604 new_fsp
->lease
->lease
.lease_state
= l
->current_state
;
1606 * We internally treat all leases as V2 and update
1607 * the epoch, but when sending breaks it matters if
1608 * the requesting lease was v1 or v2.
1610 new_fsp
->lease
->lease
.lease_version
= l
->lease_version
;
1611 new_fsp
->lease
->lease
.lease_epoch
= l
->epoch
;
1612 return new_fsp
->lease
;
1615 static NTSTATUS
grant_fsp_lease(struct files_struct
*fsp
,
1616 struct share_mode_lock
*lck
,
1617 const struct smb2_lease
*lease
,
1618 uint32_t *p_lease_idx
,
1621 struct share_mode_data
*d
= lck
->data
;
1622 const struct GUID
*client_guid
= fsp_client_guid(fsp
);
1623 struct share_mode_lease
*tmp
;
1627 idx
= find_share_mode_lease(d
, client_guid
, &lease
->lease_key
);
1630 struct share_mode_lease
*l
= &d
->leases
[idx
];
1632 uint32_t existing
, requested
;
1634 fsp
->lease
= find_fsp_lease(fsp
, &lease
->lease_key
, l
);
1635 if (fsp
->lease
== NULL
) {
1636 DEBUG(1, ("Did not find existing lease for file %s\n",
1638 return NT_STATUS_NO_MEMORY
;
1644 * Upgrade only if the requested lease is a strict upgrade.
1646 existing
= l
->current_state
;
1647 requested
= lease
->lease_state
;
1650 * Tricky: This test makes sure that "requested" is a
1651 * strict bitwise superset of "existing".
1653 do_upgrade
= ((existing
& requested
) == existing
);
1656 * Upgrade only if there's a change.
1658 do_upgrade
&= (granted
!= existing
);
1661 * Upgrade only if other leases don't prevent what was asked
1664 do_upgrade
&= (granted
== requested
);
1667 * only upgrade if we are not in breaking state
1669 do_upgrade
&= !l
->breaking
;
1671 DEBUG(10, ("existing=%"PRIu32
", requested=%"PRIu32
", "
1672 "granted=%"PRIu32
", do_upgrade=%d\n",
1673 existing
, requested
, granted
, (int)do_upgrade
));
1676 l
->current_state
= granted
;
1680 /* Ensure we're in sync with current lease state. */
1681 fsp_lease_update(lck
, fsp_client_guid(fsp
), fsp
->lease
);
1682 return NT_STATUS_OK
;
1689 tmp
= talloc_realloc(d
, d
->leases
, struct share_mode_lease
,
1695 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1699 fsp
->lease
= talloc_zero(fsp
->conn
->sconn
, struct fsp_lease
);
1700 if (fsp
->lease
== NULL
) {
1701 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1703 fsp
->lease
->ref_count
= 1;
1704 fsp
->lease
->sconn
= fsp
->conn
->sconn
;
1705 fsp
->lease
->lease
.lease_version
= lease
->lease_version
;
1706 fsp
->lease
->lease
.lease_key
= lease
->lease_key
;
1707 fsp
->lease
->lease
.lease_state
= granted
;
1708 fsp
->lease
->lease
.lease_epoch
= lease
->lease_epoch
+ 1;
1710 *p_lease_idx
= d
->num_leases
;
1712 d
->leases
[d
->num_leases
] = (struct share_mode_lease
) {
1713 .client_guid
= *client_guid
,
1714 .lease_key
= fsp
->lease
->lease
.lease_key
,
1715 .current_state
= fsp
->lease
->lease
.lease_state
,
1716 .lease_version
= fsp
->lease
->lease
.lease_version
,
1717 .epoch
= fsp
->lease
->lease
.lease_epoch
,
1720 status
= leases_db_add(client_guid
,
1723 fsp
->conn
->connectpath
,
1724 fsp
->fsp_name
->base_name
,
1725 fsp
->fsp_name
->stream_name
);
1726 if (!NT_STATUS_IS_OK(status
)) {
1727 DEBUG(10, ("%s: leases_db_add failed: %s\n", __func__
,
1728 nt_errstr(status
)));
1729 TALLOC_FREE(fsp
->lease
);
1730 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1736 return NT_STATUS_OK
;
1739 static bool is_same_lease(const files_struct
*fsp
,
1740 const struct share_mode_data
*d
,
1741 const struct share_mode_entry
*e
,
1742 const struct smb2_lease
*lease
)
1744 if (e
->op_type
!= LEASE_OPLOCK
) {
1747 if (lease
== NULL
) {
1751 return smb2_lease_equal(fsp_client_guid(fsp
),
1753 &d
->leases
[e
->lease_idx
].client_guid
,
1754 &d
->leases
[e
->lease_idx
].lease_key
);
1757 static NTSTATUS
grant_fsp_oplock_type(struct smb_request
*req
,
1758 struct files_struct
*fsp
,
1759 struct share_mode_lock
*lck
,
1761 struct smb2_lease
*lease
)
1763 struct share_mode_data
*d
= lck
->data
;
1764 bool got_handle_lease
= false;
1765 bool got_oplock
= false;
1768 uint32_t lease_idx
= UINT32_MAX
;
1772 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1773 /* No oplocks on internal open. */
1774 oplock_request
= NO_OPLOCK
;
1775 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1776 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1779 if (oplock_request
== LEASE_OPLOCK
) {
1780 if (lease
== NULL
) {
1782 * The SMB2 layer should have checked this
1784 return NT_STATUS_INTERNAL_ERROR
;
1787 granted
= lease
->lease_state
;
1789 if (lp_kernel_oplocks(SNUM(fsp
->conn
))) {
1790 DEBUG(10, ("No lease granted because kernel oplocks are enabled\n"));
1791 granted
= SMB2_LEASE_NONE
;
1793 if ((granted
& (SMB2_LEASE_READ
|SMB2_LEASE_WRITE
)) == 0) {
1794 DEBUG(10, ("No read or write lease requested\n"));
1795 granted
= SMB2_LEASE_NONE
;
1797 if (granted
== SMB2_LEASE_WRITE
) {
1798 DEBUG(10, ("pure write lease requested\n"));
1799 granted
= SMB2_LEASE_NONE
;
1801 if (granted
== (SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
)) {
1802 DEBUG(10, ("write and handle lease requested\n"));
1803 granted
= SMB2_LEASE_NONE
;
1806 granted
= map_oplock_to_lease_type(
1807 oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1810 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1811 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1813 granted
&= ~SMB2_LEASE_READ
;
1816 for (i
=0; i
<d
->num_share_modes
; i
++) {
1817 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1818 uint32_t e_lease_type
;
1820 e_lease_type
= get_lease_type(d
, e
);
1822 if ((granted
& SMB2_LEASE_WRITE
) &&
1823 !is_same_lease(fsp
, d
, e
, lease
) &&
1824 !share_mode_stale_pid(d
, i
)) {
1826 * Can grant only one writer
1828 granted
&= ~SMB2_LEASE_WRITE
;
1831 if ((e_lease_type
& SMB2_LEASE_HANDLE
) && !got_handle_lease
&&
1832 !share_mode_stale_pid(d
, i
)) {
1833 got_handle_lease
= true;
1836 if ((e
->op_type
!= LEASE_OPLOCK
) && !got_oplock
&&
1837 !share_mode_stale_pid(d
, i
)) {
1842 if ((granted
& SMB2_LEASE_READ
) && !(granted
& SMB2_LEASE_WRITE
)) {
1844 (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1845 lp_level2_oplocks(SNUM(fsp
->conn
));
1847 if (!allow_level2
) {
1848 granted
= SMB2_LEASE_NONE
;
1852 if (oplock_request
== LEASE_OPLOCK
) {
1854 granted
&= ~SMB2_LEASE_HANDLE
;
1857 fsp
->oplock_type
= LEASE_OPLOCK
;
1859 status
= grant_fsp_lease(fsp
, lck
, lease
, &lease_idx
,
1861 if (!NT_STATUS_IS_OK(status
)) {
1865 *lease
= fsp
->lease
->lease
;
1866 DEBUG(10, ("lease_state=%d\n", lease
->lease_state
));
1868 if (got_handle_lease
) {
1869 granted
= SMB2_LEASE_NONE
;
1873 case SMB2_LEASE_READ
|SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
:
1874 fsp
->oplock_type
= BATCH_OPLOCK
|EXCLUSIVE_OPLOCK
;
1876 case SMB2_LEASE_READ
|SMB2_LEASE_WRITE
:
1877 fsp
->oplock_type
= EXCLUSIVE_OPLOCK
;
1879 case SMB2_LEASE_READ
|SMB2_LEASE_HANDLE
:
1880 case SMB2_LEASE_READ
:
1881 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1884 fsp
->oplock_type
= NO_OPLOCK
;
1888 status
= set_file_oplock(fsp
);
1889 if (!NT_STATUS_IS_OK(status
)) {
1891 * Could not get the kernel oplock
1893 fsp
->oplock_type
= NO_OPLOCK
;
1897 ok
= set_share_mode(lck
, fsp
, get_current_uid(fsp
->conn
),
1902 return NT_STATUS_NO_MEMORY
;
1905 ok
= update_num_read_oplocks(fsp
, lck
);
1907 del_share_mode(lck
, fsp
);
1908 return NT_STATUS_INTERNAL_ERROR
;
1911 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1912 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1914 return NT_STATUS_OK
;
1917 static bool request_timed_out(struct timeval request_time
,
1918 struct timeval timeout
)
1920 struct timeval now
, end_time
;
1922 end_time
= timeval_sum(&request_time
, &timeout
);
1923 return (timeval_compare(&end_time
, &now
) < 0);
1926 struct defer_open_state
{
1927 struct smbXsrv_connection
*xconn
;
1931 static void defer_open_done(struct tevent_req
*req
);
1933 /****************************************************************************
1934 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1935 ****************************************************************************/
1937 static void defer_open(struct share_mode_lock
*lck
,
1938 struct timeval request_time
,
1939 struct timeval timeout
,
1940 struct smb_request
*req
,
1941 struct deferred_open_record
*state
)
1943 struct deferred_open_record
*open_rec
;
1945 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1946 "open entry for mid %llu\n",
1947 (unsigned int)request_time
.tv_sec
,
1948 (unsigned int)request_time
.tv_usec
,
1949 (unsigned long long)req
->mid
));
1951 open_rec
= talloc(NULL
, struct deferred_open_record
);
1952 if (open_rec
== NULL
) {
1954 exit_server("talloc failed");
1960 struct defer_open_state
*watch_state
;
1961 struct tevent_req
*watch_req
;
1964 watch_state
= talloc(open_rec
, struct defer_open_state
);
1965 if (watch_state
== NULL
) {
1966 exit_server("talloc failed");
1968 watch_state
->xconn
= req
->xconn
;
1969 watch_state
->mid
= req
->mid
;
1971 DEBUG(10, ("defering mid %llu\n",
1972 (unsigned long long)req
->mid
));
1974 watch_req
= dbwrap_watched_watch_send(
1975 watch_state
, req
->sconn
->ev_ctx
, lck
->data
->record
,
1976 (struct server_id
){0});
1977 if (watch_req
== NULL
) {
1978 exit_server("Could not watch share mode record");
1980 tevent_req_set_callback(watch_req
, defer_open_done
,
1983 ret
= tevent_req_set_endtime(
1984 watch_req
, req
->sconn
->ev_ctx
,
1985 timeval_sum(&request_time
, &timeout
));
1989 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1990 state
->id
, open_rec
)) {
1992 exit_server("push_deferred_open_message_smb failed");
1996 static void defer_open_done(struct tevent_req
*req
)
1998 struct defer_open_state
*state
= tevent_req_callback_data(
1999 req
, struct defer_open_state
);
2003 status
= dbwrap_watched_watch_recv(req
, talloc_tos(), NULL
, NULL
,
2006 if (!NT_STATUS_IS_OK(status
)) {
2007 DEBUG(5, ("dbwrap_watched_watch_recv returned %s\n",
2008 nt_errstr(status
)));
2010 * Even if it failed, retry anyway. TODO: We need a way to
2011 * tell a re-scheduled open about that error.
2015 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state
->mid
));
2017 ret
= schedule_deferred_open_message_smb(state
->xconn
, state
->mid
);
2023 /****************************************************************************
2024 On overwrite open ensure that the attributes match.
2025 ****************************************************************************/
2027 static bool open_match_attributes(connection_struct
*conn
,
2028 uint32_t old_dos_attr
,
2029 uint32_t new_dos_attr
,
2030 mode_t existing_unx_mode
,
2031 mode_t new_unx_mode
,
2032 mode_t
*returned_unx_mode
)
2034 uint32_t noarch_old_dos_attr
, noarch_new_dos_attr
;
2036 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
2037 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
2039 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
2040 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
2041 *returned_unx_mode
= new_unx_mode
;
2043 *returned_unx_mode
= (mode_t
)0;
2046 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
2047 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
2048 "returned_unx_mode = 0%o\n",
2049 (unsigned int)old_dos_attr
,
2050 (unsigned int)existing_unx_mode
,
2051 (unsigned int)new_dos_attr
,
2052 (unsigned int)*returned_unx_mode
));
2054 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
2055 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
2056 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
2057 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
2061 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
2062 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
2063 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
2070 /****************************************************************************
2071 Special FCB or DOS processing in the case of a sharing violation.
2072 Try and find a duplicated file handle.
2073 ****************************************************************************/
2075 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
2076 connection_struct
*conn
,
2077 files_struct
*fsp_to_dup_into
,
2078 const struct smb_filename
*smb_fname
,
2082 uint32_t access_mask
,
2083 uint32_t share_access
,
2084 uint32_t create_options
)
2088 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
2089 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
2091 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
2092 fsp
= file_find_di_next(fsp
)) {
2094 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
2095 "vuid = %llu, file_pid = %u, private_options = 0x%x "
2096 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
2097 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
2098 (unsigned int)fsp
->file_pid
,
2099 (unsigned int)fsp
->fh
->private_options
,
2100 (unsigned int)fsp
->access_mask
));
2102 if (fsp
!= fsp_to_dup_into
&&
2103 fsp
->fh
->fd
!= -1 &&
2104 fsp
->vuid
== vuid
&&
2105 fsp
->file_pid
== file_pid
&&
2106 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2107 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
2108 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
2109 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
2110 strequal(fsp
->fsp_name
->stream_name
,
2111 smb_fname
->stream_name
)) {
2112 DEBUG(10,("fcb_or_dos_open: file match\n"));
2118 return NT_STATUS_NOT_FOUND
;
2121 /* quite an insane set of semantics ... */
2122 if (is_executable(smb_fname
->base_name
) &&
2123 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
2124 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
2125 return NT_STATUS_INVALID_PARAMETER
;
2128 /* We need to duplicate this fsp. */
2129 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
2130 create_options
, fsp_to_dup_into
);
2133 static void schedule_defer_open(struct share_mode_lock
*lck
,
2135 struct timeval request_time
,
2136 struct smb_request
*req
)
2138 struct deferred_open_record state
;
2140 /* This is a relative time, added to the absolute
2141 request_time value to get the absolute timeout time.
2142 Note that if this is the second or greater time we enter
2143 this codepath for this particular request mid then
2144 request_time is left as the absolute time of the *first*
2145 time this request mid was processed. This is what allows
2146 the request to eventually time out. */
2148 struct timeval timeout
;
2150 /* Normally the smbd we asked should respond within
2151 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
2152 * the client did, give twice the timeout as a safety
2153 * measure here in case the other smbd is stuck
2154 * somewhere else. */
2156 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
2158 /* Nothing actually uses state.delayed_for_oplocks
2159 but it's handy to differentiate in debug messages
2160 between a 30 second delay due to oplock break, and
2161 a 1 second delay for share mode conflicts. */
2163 state
.delayed_for_oplocks
= True
;
2164 state
.async_open
= false;
2167 if (!request_timed_out(request_time
, timeout
)) {
2168 defer_open(lck
, request_time
, timeout
, req
, &state
);
2172 /****************************************************************************
2173 Reschedule an open call that went asynchronous.
2174 ****************************************************************************/
2176 static void schedule_async_open(struct timeval request_time
,
2177 struct smb_request
*req
)
2179 struct deferred_open_record state
;
2180 struct timeval timeout
;
2182 timeout
= timeval_set(20, 0);
2185 state
.delayed_for_oplocks
= false;
2186 state
.async_open
= true;
2188 if (!request_timed_out(request_time
, timeout
)) {
2189 defer_open(NULL
, request_time
, timeout
, req
, &state
);
2193 /****************************************************************************
2194 Work out what access_mask to use from what the client sent us.
2195 ****************************************************************************/
2197 static NTSTATUS
smbd_calculate_maximum_allowed_access(
2198 connection_struct
*conn
,
2199 const struct smb_filename
*smb_fname
,
2201 uint32_t *p_access_mask
)
2203 struct security_descriptor
*sd
;
2204 uint32_t access_granted
;
2207 if (!use_privs
&& (get_current_uid(conn
) == (uid_t
)0)) {
2208 *p_access_mask
|= FILE_GENERIC_ALL
;
2209 return NT_STATUS_OK
;
2212 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
,
2218 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
2220 * File did not exist
2222 *p_access_mask
= FILE_GENERIC_ALL
;
2223 return NT_STATUS_OK
;
2225 if (!NT_STATUS_IS_OK(status
)) {
2226 DEBUG(10,("Could not get acl on file %s: %s\n",
2227 smb_fname_str_dbg(smb_fname
),
2228 nt_errstr(status
)));
2229 return NT_STATUS_ACCESS_DENIED
;
2233 * If we can access the path to this file, by
2234 * default we have FILE_READ_ATTRIBUTES from the
2235 * containing directory. See the section:
2236 * "Algorithm to Check Access to an Existing File"
2239 * se_file_access_check()
2240 * also takes care of owner WRITE_DAC and READ_CONTROL.
2242 status
= se_file_access_check(sd
,
2243 get_current_nttok(conn
),
2245 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
2250 if (!NT_STATUS_IS_OK(status
)) {
2251 DEBUG(10, ("Access denied on file %s: "
2252 "when calculating maximum access\n",
2253 smb_fname_str_dbg(smb_fname
)));
2254 return NT_STATUS_ACCESS_DENIED
;
2256 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
2258 if (!(access_granted
& DELETE_ACCESS
)) {
2259 if (can_delete_file_in_directory(conn
, smb_fname
)) {
2260 *p_access_mask
|= DELETE_ACCESS
;
2264 return NT_STATUS_OK
;
2267 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
2268 const struct smb_filename
*smb_fname
,
2270 uint32_t access_mask
,
2271 uint32_t *access_mask_out
)
2274 uint32_t orig_access_mask
= access_mask
;
2275 uint32_t rejected_share_access
;
2278 * Convert GENERIC bits to specific bits.
2281 se_map_generic(&access_mask
, &file_generic_mapping
);
2283 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
2284 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
2286 status
= smbd_calculate_maximum_allowed_access(
2287 conn
, smb_fname
, use_privs
, &access_mask
);
2289 if (!NT_STATUS_IS_OK(status
)) {
2293 access_mask
&= conn
->share_access
;
2296 rejected_share_access
= access_mask
& ~(conn
->share_access
);
2298 if (rejected_share_access
) {
2299 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
2300 "file %s: rejected by share access mask[0x%08X] "
2301 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
2302 smb_fname_str_dbg(smb_fname
),
2304 orig_access_mask
, access_mask
,
2305 rejected_share_access
));
2306 return NT_STATUS_ACCESS_DENIED
;
2309 *access_mask_out
= access_mask
;
2310 return NT_STATUS_OK
;
2313 /****************************************************************************
2314 Remove the deferred open entry under lock.
2315 ****************************************************************************/
2317 /****************************************************************************
2318 Return true if this is a state pointer to an asynchronous create.
2319 ****************************************************************************/
2321 bool is_deferred_open_async(const struct deferred_open_record
*rec
)
2323 return rec
->async_open
;
2326 static bool clear_ads(uint32_t create_disposition
)
2330 switch (create_disposition
) {
2331 case FILE_SUPERSEDE
:
2332 case FILE_OVERWRITE_IF
:
2333 case FILE_OVERWRITE
:
2342 static int disposition_to_open_flags(uint32_t create_disposition
)
2347 * Currently we're using FILE_SUPERSEDE as the same as
2348 * FILE_OVERWRITE_IF but they really are
2349 * different. FILE_SUPERSEDE deletes an existing file
2350 * (requiring delete access) then recreates it.
2353 switch (create_disposition
) {
2354 case FILE_SUPERSEDE
:
2355 case FILE_OVERWRITE_IF
:
2357 * If file exists replace/overwrite. If file doesn't
2360 ret
= O_CREAT
|O_TRUNC
;
2365 * If file exists open. If file doesn't exist error.
2370 case FILE_OVERWRITE
:
2372 * If file exists overwrite. If file doesn't exist
2380 * If file exists error. If file doesn't exist create.
2382 ret
= O_CREAT
|O_EXCL
;
2387 * If file exists open. If file doesn't exist create.
2395 static int calculate_open_access_flags(uint32_t access_mask
,
2396 uint32_t private_flags
)
2398 bool need_write
, need_read
;
2401 * Note that we ignore the append flag as append does not
2402 * mean the same thing under DOS and Unix.
2405 need_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
));
2410 /* DENY_DOS opens are always underlying read-write on the
2411 file handle, no matter what the requested access mask
2415 ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
2416 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|
2417 FILE_READ_EA
|FILE_EXECUTE
));
2425 /****************************************************************************
2426 Open a file with a share mode. Passed in an already created files_struct *.
2427 ****************************************************************************/
2429 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
2430 struct smb_request
*req
,
2431 uint32_t access_mask
, /* access bits (FILE_READ_DATA etc.) */
2432 uint32_t share_access
, /* share constants (FILE_SHARE_READ etc) */
2433 uint32_t create_disposition
, /* FILE_OPEN_IF etc. */
2434 uint32_t create_options
, /* options such as delete on close. */
2435 uint32_t new_dos_attributes
, /* attributes used for new file. */
2436 int oplock_request
, /* internal Samba oplock codes. */
2437 struct smb2_lease
*lease
,
2438 /* Information (FILE_EXISTS etc.) */
2439 uint32_t private_flags
, /* Samba specific flags. */
2443 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
2446 bool file_existed
= VALID_STAT(smb_fname
->st
);
2447 bool def_acl
= False
;
2448 bool posix_open
= False
;
2449 bool new_file_created
= False
;
2450 bool first_open_attempt
= true;
2451 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
2452 mode_t new_unx_mode
= (mode_t
)0;
2453 mode_t unx_mode
= (mode_t
)0;
2455 uint32_t existing_dos_attributes
= 0;
2456 struct timeval request_time
= timeval_zero();
2457 struct share_mode_lock
*lck
= NULL
;
2458 uint32_t open_access_mask
= access_mask
;
2461 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
2462 struct timespec old_write_time
;
2465 if (conn
->printer
) {
2467 * Printers are handled completely differently.
2468 * Most of the passed parameters are ignored.
2472 *pinfo
= FILE_WAS_CREATED
;
2475 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2476 smb_fname_str_dbg(smb_fname
)));
2479 DEBUG(0,("open_file_ntcreate: printer open without "
2480 "an SMB request!\n"));
2481 return NT_STATUS_INTERNAL_ERROR
;
2484 return print_spool_open(fsp
, smb_fname
->base_name
,
2488 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
2490 return NT_STATUS_NO_MEMORY
;
2493 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2495 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2496 new_dos_attributes
= 0;
2498 /* Windows allows a new file to be created and
2499 silently removes a FILE_ATTRIBUTE_DIRECTORY
2500 sent by the client. Do the same. */
2502 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
2504 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2506 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2507 smb_fname
, parent_dir
);
2510 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2511 "access_mask=0x%x share_access=0x%x "
2512 "create_disposition = 0x%x create_options=0x%x "
2513 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2514 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
2515 access_mask
, share_access
, create_disposition
,
2516 create_options
, (unsigned int)unx_mode
, oplock_request
,
2517 (unsigned int)private_flags
));
2520 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2521 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) != 0));
2523 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2524 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) == 0));
2528 * Only non-internal opens can be deferred at all
2532 struct deferred_open_record
*open_rec
;
2533 if (get_deferred_open_message_state(req
,
2536 /* Remember the absolute time of the original
2537 request with this mid. We'll use it later to
2538 see if this has timed out. */
2540 /* If it was an async create retry, the file
2543 if (is_deferred_open_async(open_rec
)) {
2544 SET_STAT_INVALID(smb_fname
->st
);
2545 file_existed
= false;
2548 /* Ensure we don't reprocess this message. */
2549 remove_deferred_open_message_smb(req
->xconn
, req
->mid
);
2551 first_open_attempt
= false;
2556 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
2559 * Only use strored DOS attributes for checks
2560 * against requested attributes (below via
2561 * open_match_attributes()), cf bug #11992
2562 * for details. -slow
2566 status
= SMB_VFS_GET_DOS_ATTRIBUTES(conn
, smb_fname
, &attr
);
2567 if (NT_STATUS_IS_OK(status
)) {
2568 existing_dos_attributes
= attr
;
2573 /* ignore any oplock requests if oplocks are disabled */
2574 if (!lp_oplocks(SNUM(conn
)) ||
2575 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
2576 /* Mask off everything except the private Samba bits. */
2577 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
2580 /* this is for OS/2 long file names - say we don't support them */
2581 if (req
!= NULL
&& !req
->posix_pathnames
&&
2582 strstr(smb_fname
->base_name
,".+,;=[].")) {
2583 /* OS/2 Workplace shell fix may be main code stream in a later
2585 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2587 if (use_nt_status()) {
2588 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2590 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
2593 switch( create_disposition
) {
2595 /* If file exists open. If file doesn't exist error. */
2596 if (!file_existed
) {
2597 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2598 "requested for file %s and file "
2600 smb_fname_str_dbg(smb_fname
)));
2602 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2606 case FILE_OVERWRITE
:
2607 /* If file exists overwrite. If file doesn't exist
2609 if (!file_existed
) {
2610 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2611 "requested for file %s and file "
2613 smb_fname_str_dbg(smb_fname
) ));
2615 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2620 /* If file exists error. If file doesn't exist
2623 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2624 "requested for file %s and file "
2625 "already exists.\n",
2626 smb_fname_str_dbg(smb_fname
)));
2627 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2632 return map_nt_error_from_unix(errno
);
2636 case FILE_SUPERSEDE
:
2637 case FILE_OVERWRITE_IF
:
2641 return NT_STATUS_INVALID_PARAMETER
;
2644 flags2
= disposition_to_open_flags(create_disposition
);
2646 /* We only care about matching attributes on file exists and
2649 if (!posix_open
&& file_existed
&&
2650 ((create_disposition
== FILE_OVERWRITE
) ||
2651 (create_disposition
== FILE_OVERWRITE_IF
))) {
2652 if (!open_match_attributes(conn
, existing_dos_attributes
,
2654 smb_fname
->st
.st_ex_mode
,
2655 unx_mode
, &new_unx_mode
)) {
2656 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2657 "for file %s (%x %x) (0%o, 0%o)\n",
2658 smb_fname_str_dbg(smb_fname
),
2659 existing_dos_attributes
,
2661 (unsigned int)smb_fname
->st
.st_ex_mode
,
2662 (unsigned int)unx_mode
));
2664 return NT_STATUS_ACCESS_DENIED
;
2668 status
= smbd_calculate_access_mask(conn
, smb_fname
,
2672 if (!NT_STATUS_IS_OK(status
)) {
2673 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2674 "on file %s returned %s\n",
2675 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
2679 open_access_mask
= access_mask
;
2681 if (flags2
& O_TRUNC
) {
2682 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
2685 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2686 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
2690 * Note that we ignore the append flag as append does not
2691 * mean the same thing under DOS and Unix.
2694 flags
= calculate_open_access_flags(access_mask
, private_flags
);
2697 * Currently we only look at FILE_WRITE_THROUGH for create options.
2701 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
2706 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
2710 if (!posix_open
&& !CAN_WRITE(conn
)) {
2712 * We should really return a permission denied error if either
2713 * O_CREAT or O_TRUNC are set, but for compatibility with
2714 * older versions of Samba we just AND them out.
2716 flags2
&= ~(O_CREAT
|O_TRUNC
);
2719 if (first_open_attempt
&& lp_kernel_oplocks(SNUM(conn
))) {
2721 * With kernel oplocks the open breaking an oplock
2722 * blocks until the oplock holder has given up the
2723 * oplock or closed the file. We prevent this by first
2724 * trying to open the file with O_NONBLOCK (see "man
2725 * fcntl" on Linux). For the second try, triggered by
2726 * an oplock break response, we do not need this
2729 * This is true under the assumption that only Samba
2730 * requests kernel oplocks. Once someone else like
2731 * NFSv4 starts to use that API, we will have to
2732 * modify this by communicating with the NFSv4 server.
2734 flags2
|= O_NONBLOCK
;
2738 * Ensure we can't write on a read-only share or file.
2741 if (flags
!= O_RDONLY
&& file_existed
&&
2742 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
2743 DEBUG(5,("open_file_ntcreate: write access requested for "
2744 "file %s on read only %s\n",
2745 smb_fname_str_dbg(smb_fname
),
2746 !CAN_WRITE(conn
) ? "share" : "file" ));
2748 return NT_STATUS_ACCESS_DENIED
;
2751 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2752 fsp
->share_access
= share_access
;
2753 fsp
->fh
->private_options
= private_flags
;
2754 fsp
->access_mask
= open_access_mask
; /* We change this to the
2755 * requested access_mask after
2756 * the open is done. */
2758 fsp
->posix_flags
|= FSP_POSIX_FLAGS_ALL
;
2761 if (timeval_is_zero(&request_time
)) {
2762 request_time
= fsp
->open_time
;
2766 * Ensure we pay attention to default ACLs on directories if required.
2769 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2770 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2771 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2774 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2775 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2776 (unsigned int)flags
, (unsigned int)flags2
,
2777 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2778 (unsigned int)open_access_mask
));
2780 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2781 flags
|flags2
, unx_mode
, access_mask
,
2782 open_access_mask
, &new_file_created
);
2784 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_NETWORK_BUSY
)) {
2785 struct deferred_open_record state
;
2788 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2790 if (file_existed
&& S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
)) {
2791 DEBUG(10, ("FIFO busy\n"));
2792 return NT_STATUS_NETWORK_BUSY
;
2795 DEBUG(10, ("Internal open busy\n"));
2796 return NT_STATUS_NETWORK_BUSY
;
2800 * From here on we assume this is an oplock break triggered
2803 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
2805 state
.delayed_for_oplocks
= false;
2806 state
.async_open
= false;
2807 state
.id
= fsp
->file_id
;
2808 defer_open(NULL
, request_time
, timeval_set(0, 0),
2810 DEBUG(10, ("No share mode lock found after "
2811 "EWOULDBLOCK, retrying sync\n"));
2812 return NT_STATUS_SHARING_VIOLATION
;
2815 if (!validate_oplock_types(lck
)) {
2816 smb_panic("validate_oplock_types failed");
2819 if (delay_for_oplock(fsp
, 0, lease
, lck
, false,
2820 create_disposition
, first_open_attempt
)) {
2821 schedule_defer_open(lck
, fsp
->file_id
, request_time
,
2824 DEBUG(10, ("Sent oplock break request to kernel "
2825 "oplock holder\n"));
2826 return NT_STATUS_SHARING_VIOLATION
;
2830 * No oplock from Samba around. Immediately retry with
2833 state
.delayed_for_oplocks
= false;
2834 state
.async_open
= false;
2835 state
.id
= fsp
->file_id
;
2836 defer_open(lck
, request_time
, timeval_set(0, 0), req
, &state
);
2838 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2839 "Retrying sync\n"));
2840 return NT_STATUS_SHARING_VIOLATION
;
2843 if (!NT_STATUS_IS_OK(fsp_open
)) {
2844 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
2845 schedule_async_open(request_time
, req
);
2850 if (new_file_created
) {
2852 * As we atomically create using O_CREAT|O_EXCL,
2853 * then if new_file_created is true, then
2854 * file_existed *MUST* have been false (even
2855 * if the file was previously detected as being
2858 file_existed
= false;
2861 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
2863 * The file did exist, but some other (local or NFS)
2864 * process either renamed/unlinked and re-created the
2865 * file with different dev/ino after we walked the path,
2866 * but before we did the open. We could retry the
2867 * open but it's a rare enough case it's easier to
2868 * just fail the open to prevent creating any problems
2869 * in the open file db having the wrong dev/ino key.
2872 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2873 "Old (dev=0x%llu, ino =0x%llu). "
2874 "New (dev=0x%llu, ino=0x%llu). Failing open "
2875 " with NT_STATUS_ACCESS_DENIED.\n",
2876 smb_fname_str_dbg(smb_fname
),
2877 (unsigned long long)saved_stat
.st_ex_dev
,
2878 (unsigned long long)saved_stat
.st_ex_ino
,
2879 (unsigned long long)smb_fname
->st
.st_ex_dev
,
2880 (unsigned long long)smb_fname
->st
.st_ex_ino
));
2881 return NT_STATUS_ACCESS_DENIED
;
2884 old_write_time
= smb_fname
->st
.st_ex_mtime
;
2887 * Deal with the race condition where two smbd's detect the
2888 * file doesn't exist and do the create at the same time. One
2889 * of them will win and set a share mode, the other (ie. this
2890 * one) should check if the requested share mode for this
2891 * create is allowed.
2895 * Now the file exists and fsp is successfully opened,
2896 * fsp->dev and fsp->inode are valid and should replace the
2897 * dev=0,inode=0 from a non existent file. Spotted by
2898 * Nadav Danieli <nadavd@exanet.com>. JRA.
2903 lck
= get_share_mode_lock(talloc_tos(), id
,
2905 smb_fname
, &old_write_time
);
2908 DEBUG(0, ("open_file_ntcreate: Could not get share "
2909 "mode lock for %s\n",
2910 smb_fname_str_dbg(smb_fname
)));
2912 return NT_STATUS_SHARING_VIOLATION
;
2915 /* Get the types we need to examine. */
2916 if (!validate_oplock_types(lck
)) {
2917 smb_panic("validate_oplock_types failed");
2920 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
2923 return NT_STATUS_DELETE_PENDING
;
2926 status
= open_mode_check(conn
, lck
,
2927 access_mask
, share_access
);
2929 if (NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
) ||
2930 (lck
->data
->num_share_modes
> 0)) {
2932 * This comes from ancient times out of open_mode_check. I
2933 * have no clue whether this is still necessary. I can't think
2934 * of a case where this would actually matter further down in
2935 * this function. I leave it here for further investigation
2938 file_existed
= true;
2941 if ((req
!= NULL
) &&
2943 fsp
, oplock_request
, lease
, lck
,
2944 NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
),
2945 create_disposition
, first_open_attempt
)) {
2946 schedule_defer_open(lck
, fsp
->file_id
, request_time
, req
);
2949 return NT_STATUS_SHARING_VIOLATION
;
2952 if (!NT_STATUS_IS_OK(status
)) {
2953 uint32_t can_access_mask
;
2954 bool can_access
= True
;
2956 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2958 /* Check if this can be done with the deny_dos and fcb
2961 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2962 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2964 DEBUG(0, ("DOS open without an SMB "
2968 return NT_STATUS_INTERNAL_ERROR
;
2971 /* Use the client requested access mask here,
2972 * not the one we open with. */
2973 status
= fcb_or_dos_open(req
,
2984 if (NT_STATUS_IS_OK(status
)) {
2987 *pinfo
= FILE_WAS_OPENED
;
2989 return NT_STATUS_OK
;
2994 * This next line is a subtlety we need for
2995 * MS-Access. If a file open will fail due to share
2996 * permissions and also for security (access) reasons,
2997 * we need to return the access failed error, not the
2998 * share error. We can't open the file due to kernel
2999 * oplock deadlock (it's possible we failed above on
3000 * the open_mode_check()) so use a userspace check.
3003 if (flags
& O_RDWR
) {
3004 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
3005 } else if (flags
& O_WRONLY
) {
3006 can_access_mask
= FILE_WRITE_DATA
;
3008 can_access_mask
= FILE_READ_DATA
;
3011 if (((can_access_mask
& FILE_WRITE_DATA
) &&
3012 !CAN_WRITE(conn
)) ||
3013 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
3016 can_access_mask
))) {
3021 * If we're returning a share violation, ensure we
3022 * cope with the braindead 1 second delay (SMB1 only).
3025 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
3026 !conn
->sconn
->using_smb2
&&
3027 lp_defer_sharing_violations()) {
3028 struct timeval timeout
;
3029 struct deferred_open_record state
;
3032 /* this is a hack to speed up torture tests
3034 timeout_usecs
= lp_parm_int(SNUM(conn
),
3035 "smbd","sharedelay",
3036 SHARING_VIOLATION_USEC_WAIT
);
3038 /* This is a relative time, added to the absolute
3039 request_time value to get the absolute timeout time.
3040 Note that if this is the second or greater time we enter
3041 this codepath for this particular request mid then
3042 request_time is left as the absolute time of the *first*
3043 time this request mid was processed. This is what allows
3044 the request to eventually time out. */
3046 timeout
= timeval_set(0, timeout_usecs
);
3048 /* Nothing actually uses state.delayed_for_oplocks
3049 but it's handy to differentiate in debug messages
3050 between a 30 second delay due to oplock break, and
3051 a 1 second delay for share mode conflicts. */
3053 state
.delayed_for_oplocks
= False
;
3054 state
.async_open
= false;
3058 && !request_timed_out(request_time
,
3060 defer_open(lck
, request_time
, timeout
,
3069 * We have detected a sharing violation here
3070 * so return the correct error code
3072 status
= NT_STATUS_SHARING_VIOLATION
;
3074 status
= NT_STATUS_ACCESS_DENIED
;
3079 /* Should we atomically (to the client at least) truncate ? */
3080 if ((!new_file_created
) &&
3081 (flags2
& O_TRUNC
) &&
3082 (!S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
))) {
3085 ret
= vfs_set_filelen(fsp
, 0);
3087 status
= map_nt_error_from_unix(errno
);
3095 * We have the share entry *locked*.....
3098 /* Delete streams if create_disposition requires it */
3099 if (!new_file_created
&& clear_ads(create_disposition
) &&
3100 !is_ntfs_stream_smb_fname(smb_fname
)) {
3101 status
= delete_all_streams(conn
, smb_fname
);
3102 if (!NT_STATUS_IS_OK(status
)) {
3109 /* note that we ignore failure for the following. It is
3110 basically a hack for NFS, and NFS will never set one of
3111 these only read them. Nobody but Samba can ever set a deny
3112 mode and we have already checked our more authoritative
3113 locking database for permission to set this deny mode. If
3114 the kernel refuses the operations then the kernel is wrong.
3115 note that GPFS supports it as well - jmcd */
3117 if (fsp
->fh
->fd
!= -1 && lp_kernel_share_modes(SNUM(conn
))) {
3120 * Beware: streams implementing VFS modules may
3121 * implement streams in a way that fsp will have the
3122 * basefile open in the fsp fd, so lacking a distinct
3123 * fd for the stream kernel_flock will apply on the
3124 * basefile which is wrong. The actual check is
3125 * deffered to the VFS module implementing the
3126 * kernel_flock call.
3128 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
3129 if(ret_flock
== -1 ){
3134 return NT_STATUS_SHARING_VIOLATION
;
3137 fsp
->kernel_share_modes_taken
= true;
3141 * At this point onwards, we can guarantee that the share entry
3142 * is locked, whether we created the file or not, and that the
3143 * deny mode is compatible with all current opens.
3147 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3148 * but we don't have to store this - just ignore it on access check.
3150 if (conn
->sconn
->using_smb2
) {
3152 * SMB2 doesn't return it (according to Microsoft tests).
3153 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
3154 * File created with access = 0x7 (Read, Write, Delete)
3155 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
3157 fsp
->access_mask
= access_mask
;
3159 /* But SMB1 does. */
3160 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3165 * stat opens on existing files don't get oplocks.
3166 * They can get leases.
3168 * Note that we check for stat open on the *open_access_mask*,
3169 * i.e. the access mask we actually used to do the open,
3170 * not the one the client asked for (which is in
3171 * fsp->access_mask). This is due to the fact that
3172 * FILE_OVERWRITE and FILE_OVERWRITE_IF add in O_TRUNC,
3173 * which adds FILE_WRITE_DATA to open_access_mask.
3175 if (is_stat_open(open_access_mask
) && lease
== NULL
) {
3176 oplock_request
= NO_OPLOCK
;
3180 if (new_file_created
) {
3181 info
= FILE_WAS_CREATED
;
3183 if (flags2
& O_TRUNC
) {
3184 info
= FILE_WAS_OVERWRITTEN
;
3186 info
= FILE_WAS_OPENED
;
3195 * Setup the oplock info in both the shared memory and
3198 status
= grant_fsp_oplock_type(req
, fsp
, lck
, oplock_request
, lease
);
3199 if (!NT_STATUS_IS_OK(status
)) {
3205 /* Handle strange delete on close create semantics. */
3206 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3208 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
3210 if (!NT_STATUS_IS_OK(status
)) {
3211 /* Remember to delete the mode we just added. */
3212 del_share_mode(lck
, fsp
);
3217 /* Note that here we set the *inital* delete on close flag,
3218 not the regular one. The magic gets handled in close. */
3219 fsp
->initial_delete_on_close
= True
;
3222 if (info
!= FILE_WAS_OPENED
) {
3223 /* Overwritten files should be initially set as archive */
3224 if ((info
== FILE_WAS_OVERWRITTEN
&& lp_map_archive(SNUM(conn
))) ||
3225 lp_store_dos_attributes(SNUM(conn
))) {
3227 if (file_set_dosmode(conn
, smb_fname
,
3228 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
3229 parent_dir
, true) == 0) {
3230 unx_mode
= smb_fname
->st
.st_ex_mode
;
3236 /* Determine sparse flag. */
3238 /* POSIX opens are sparse by default. */
3239 fsp
->is_sparse
= true;
3241 fsp
->is_sparse
= (file_existed
&&
3242 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
3246 * Take care of inherited ACLs on created files - if default ACL not
3250 if (!posix_open
&& new_file_created
&& !def_acl
) {
3252 int saved_errno
= errno
; /* We might get ENOSYS in the next
3255 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
3257 errno
= saved_errno
; /* Ignore ENOSYS */
3260 } else if (new_unx_mode
) {
3264 /* Attributes need changing. File already existed. */
3267 int saved_errno
= errno
; /* We might get ENOSYS in the
3269 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
3271 if (ret
== -1 && errno
== ENOSYS
) {
3272 errno
= saved_errno
; /* Ignore ENOSYS */
3274 DEBUG(5, ("open_file_ntcreate: reset "
3275 "attributes of file %s to 0%o\n",
3276 smb_fname_str_dbg(smb_fname
),
3277 (unsigned int)new_unx_mode
));
3278 ret
= 0; /* Don't do the fchmod below. */
3283 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
3284 DEBUG(5, ("open_file_ntcreate: failed to reset "
3285 "attributes of file %s to 0%o\n",
3286 smb_fname_str_dbg(smb_fname
),
3287 (unsigned int)new_unx_mode
));
3292 * Deal with other opens having a modified write time.
3294 struct timespec write_time
= get_share_mode_write_time(lck
);
3296 if (!null_timespec(write_time
)) {
3297 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
3303 return NT_STATUS_OK
;
3306 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
3307 struct smb_filename
*smb_dname
,
3308 uint32_t file_attributes
)
3311 char *parent_dir
= NULL
;
3313 bool posix_open
= false;
3314 bool need_re_stat
= false;
3315 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
3317 if (!CAN_WRITE(conn
) || (access_mask
& ~(conn
->share_access
))) {
3318 DEBUG(5,("mkdir_internal: failing share access "
3319 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
3320 return NT_STATUS_ACCESS_DENIED
;
3323 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
3325 return NT_STATUS_NO_MEMORY
;
3328 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
3330 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
3332 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
3335 status
= check_parent_access(conn
,
3338 if(!NT_STATUS_IS_OK(status
)) {
3339 DEBUG(5,("mkdir_internal: check_parent_access "
3340 "on directory %s for path %s returned %s\n",
3342 smb_dname
->base_name
,
3343 nt_errstr(status
) ));
3347 if (SMB_VFS_MKDIR(conn
, smb_dname
, mode
) != 0) {
3348 return map_nt_error_from_unix(errno
);
3351 /* Ensure we're checking for a symlink here.... */
3352 /* We don't want to get caught by a symlink racer. */
3354 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
3355 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3356 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3357 return map_nt_error_from_unix(errno
);
3360 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3361 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3362 smb_fname_str_dbg(smb_dname
)));
3363 return NT_STATUS_NOT_A_DIRECTORY
;
3366 if (lp_store_dos_attributes(SNUM(conn
))) {
3368 file_set_dosmode(conn
, smb_dname
,
3369 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
3374 if (lp_inherit_permissions(SNUM(conn
))) {
3375 inherit_access_posix_acl(conn
, parent_dir
,
3376 smb_dname
->base_name
, mode
);
3377 need_re_stat
= true;
3382 * Check if high bits should have been set,
3383 * then (if bits are missing): add them.
3384 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3387 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
3388 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
3389 SMB_VFS_CHMOD(conn
, smb_dname
,
3390 (smb_dname
->st
.st_ex_mode
|
3391 (mode
& ~smb_dname
->st
.st_ex_mode
)));
3392 need_re_stat
= true;
3396 /* Change the owner if required. */
3397 if (lp_inherit_owner(SNUM(conn
)) != INHERIT_OWNER_NO
) {
3398 change_dir_owner_to_parent(conn
, parent_dir
,
3399 smb_dname
->base_name
,
3401 need_re_stat
= true;
3405 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
3406 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3407 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3408 return map_nt_error_from_unix(errno
);
3412 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
3413 smb_dname
->base_name
);
3415 return NT_STATUS_OK
;
3418 /****************************************************************************
3419 Open a directory from an NT SMB call.
3420 ****************************************************************************/
3422 static NTSTATUS
open_directory(connection_struct
*conn
,
3423 struct smb_request
*req
,
3424 struct smb_filename
*smb_dname
,
3425 uint32_t access_mask
,
3426 uint32_t share_access
,
3427 uint32_t create_disposition
,
3428 uint32_t create_options
,
3429 uint32_t file_attributes
,
3431 files_struct
**result
)
3433 files_struct
*fsp
= NULL
;
3434 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
3435 struct share_mode_lock
*lck
= NULL
;
3437 struct timespec mtimespec
;
3441 if (is_ntfs_stream_smb_fname(smb_dname
)) {
3442 DEBUG(2, ("open_directory: %s is a stream name!\n",
3443 smb_fname_str_dbg(smb_dname
)));
3444 return NT_STATUS_NOT_A_DIRECTORY
;
3447 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
3448 /* Ensure we have a directory attribute. */
3449 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3452 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3453 "share_access = 0x%x create_options = 0x%x, "
3454 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3455 smb_fname_str_dbg(smb_dname
),
3456 (unsigned int)access_mask
,
3457 (unsigned int)share_access
,
3458 (unsigned int)create_options
,
3459 (unsigned int)create_disposition
,
3460 (unsigned int)file_attributes
));
3462 status
= smbd_calculate_access_mask(conn
, smb_dname
, false,
3463 access_mask
, &access_mask
);
3464 if (!NT_STATUS_IS_OK(status
)) {
3465 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3466 "on file %s returned %s\n",
3467 smb_fname_str_dbg(smb_dname
),
3468 nt_errstr(status
)));
3472 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3473 !security_token_has_privilege(get_current_nttok(conn
),
3474 SEC_PRIV_SECURITY
)) {
3475 DEBUG(10, ("open_directory: open on %s "
3476 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3477 smb_fname_str_dbg(smb_dname
)));
3478 return NT_STATUS_PRIVILEGE_NOT_HELD
;
3481 switch( create_disposition
) {
3485 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3488 info
= FILE_WAS_OPENED
;
3493 /* If directory exists error. If directory doesn't
3497 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
3498 DEBUG(2, ("open_directory: unable to create "
3499 "%s. Error was %s\n",
3500 smb_fname_str_dbg(smb_dname
),
3501 nt_errstr(status
)));
3505 status
= mkdir_internal(conn
, smb_dname
,
3508 if (!NT_STATUS_IS_OK(status
)) {
3509 DEBUG(2, ("open_directory: unable to create "
3510 "%s. Error was %s\n",
3511 smb_fname_str_dbg(smb_dname
),
3512 nt_errstr(status
)));
3516 info
= FILE_WAS_CREATED
;
3521 * If directory exists open. If directory doesn't
3526 status
= NT_STATUS_OK
;
3527 info
= FILE_WAS_OPENED
;
3529 status
= mkdir_internal(conn
, smb_dname
,
3532 if (NT_STATUS_IS_OK(status
)) {
3533 info
= FILE_WAS_CREATED
;
3535 /* Cope with create race. */
3536 if (!NT_STATUS_EQUAL(status
,
3537 NT_STATUS_OBJECT_NAME_COLLISION
)) {
3538 DEBUG(2, ("open_directory: unable to create "
3539 "%s. Error was %s\n",
3540 smb_fname_str_dbg(smb_dname
),
3541 nt_errstr(status
)));
3546 * If mkdir_internal() returned
3547 * NT_STATUS_OBJECT_NAME_COLLISION
3548 * we still must lstat the path.
3551 if (SMB_VFS_LSTAT(conn
, smb_dname
)
3553 DEBUG(2, ("Could not stat "
3554 "directory '%s' just "
3559 return map_nt_error_from_unix(
3563 info
= FILE_WAS_OPENED
;
3569 case FILE_SUPERSEDE
:
3570 case FILE_OVERWRITE
:
3571 case FILE_OVERWRITE_IF
:
3573 DEBUG(5,("open_directory: invalid create_disposition "
3574 "0x%x for directory %s\n",
3575 (unsigned int)create_disposition
,
3576 smb_fname_str_dbg(smb_dname
)));
3577 return NT_STATUS_INVALID_PARAMETER
;
3580 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3581 DEBUG(5,("open_directory: %s is not a directory !\n",
3582 smb_fname_str_dbg(smb_dname
)));
3583 return NT_STATUS_NOT_A_DIRECTORY
;
3586 if (info
== FILE_WAS_OPENED
) {
3587 status
= smbd_check_access_rights(conn
,
3591 if (!NT_STATUS_IS_OK(status
)) {
3592 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3593 "file %s failed with %s\n",
3594 smb_fname_str_dbg(smb_dname
),
3595 nt_errstr(status
)));
3600 status
= file_new(req
, conn
, &fsp
);
3601 if(!NT_STATUS_IS_OK(status
)) {
3606 * Setup the files_struct for it.
3609 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
3610 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
3611 fsp
->file_pid
= req
? req
->smbpid
: 0;
3612 fsp
->can_lock
= False
;
3613 fsp
->can_read
= False
;
3614 fsp
->can_write
= False
;
3616 fsp
->share_access
= share_access
;
3617 fsp
->fh
->private_options
= 0;
3619 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3621 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3622 fsp
->print_file
= NULL
;
3623 fsp
->modified
= False
;
3624 fsp
->oplock_type
= NO_OPLOCK
;
3625 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
3626 fsp
->is_directory
= True
;
3627 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
3628 fsp
->posix_flags
|= FSP_POSIX_FLAGS_ALL
;
3630 status
= fsp_set_smb_fname(fsp
, smb_dname
);
3631 if (!NT_STATUS_IS_OK(status
)) {
3632 file_free(req
, fsp
);
3636 /* Don't store old timestamps for directory
3637 handles in the internal database. We don't
3638 update them in there if new objects
3639 are creaded in the directory. Currently
3640 we only update timestamps on file writes.
3643 ZERO_STRUCT(mtimespec
);
3645 if (access_mask
& (FILE_LIST_DIRECTORY
|
3647 FILE_ADD_SUBDIRECTORY
|
3650 FILE_DELETE_CHILD
)) {
3652 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
3654 /* POSIX allows us to open a directory with O_RDONLY. */
3655 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
3657 if (!NT_STATUS_IS_OK(status
)) {
3658 DEBUG(5, ("open_directory: Could not open fd for "
3660 smb_fname_str_dbg(smb_dname
),
3661 nt_errstr(status
)));
3662 file_free(req
, fsp
);
3667 DEBUG(10, ("Not opening Directory %s\n",
3668 smb_fname_str_dbg(smb_dname
)));
3671 status
= vfs_stat_fsp(fsp
);
3672 if (!NT_STATUS_IS_OK(status
)) {
3674 file_free(req
, fsp
);
3678 if(!S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3679 DEBUG(5,("open_directory: %s is not a directory !\n",
3680 smb_fname_str_dbg(smb_dname
)));
3682 file_free(req
, fsp
);
3683 return NT_STATUS_NOT_A_DIRECTORY
;
3686 /* Ensure there was no race condition. We need to check
3687 * dev/inode but not permissions, as these can change
3689 if (!check_same_dev_ino(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
3690 DEBUG(5,("open_directory: stat struct differs for "
3692 smb_fname_str_dbg(smb_dname
)));
3694 file_free(req
, fsp
);
3695 return NT_STATUS_ACCESS_DENIED
;
3698 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
3699 conn
->connectpath
, smb_dname
,
3703 DEBUG(0, ("open_directory: Could not get share mode lock for "
3704 "%s\n", smb_fname_str_dbg(smb_dname
)));
3706 file_free(req
, fsp
);
3707 return NT_STATUS_SHARING_VIOLATION
;
3710 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
3713 file_free(req
, fsp
);
3714 return NT_STATUS_DELETE_PENDING
;
3717 status
= open_mode_check(conn
, lck
,
3718 access_mask
, share_access
);
3720 if (!NT_STATUS_IS_OK(status
)) {
3723 file_free(req
, fsp
);
3727 ok
= set_share_mode(lck
, fsp
, get_current_uid(conn
),
3728 req
? req
->mid
: 0, NO_OPLOCK
,
3733 file_free(req
, fsp
);
3734 return NT_STATUS_NO_MEMORY
;
3737 /* For directories the delete on close bit at open time seems
3738 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3739 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3740 status
= can_set_delete_on_close(fsp
, 0);
3741 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
3742 del_share_mode(lck
, fsp
);
3745 file_free(req
, fsp
);
3749 if (NT_STATUS_IS_OK(status
)) {
3750 /* Note that here we set the *inital* delete on close flag,
3751 not the regular one. The magic gets handled in close. */
3752 fsp
->initial_delete_on_close
= True
;
3758 * Deal with other opens having a modified write time. Is this
3759 * possible for directories?
3761 struct timespec write_time
= get_share_mode_write_time(lck
);
3763 if (!null_timespec(write_time
)) {
3764 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
3775 return NT_STATUS_OK
;
3778 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
3779 struct smb_filename
*smb_dname
)
3784 status
= SMB_VFS_CREATE_FILE(
3787 0, /* root_dir_fid */
3788 smb_dname
, /* fname */
3789 FILE_READ_ATTRIBUTES
, /* access_mask */
3790 FILE_SHARE_NONE
, /* share_access */
3791 FILE_CREATE
, /* create_disposition*/
3792 FILE_DIRECTORY_FILE
, /* create_options */
3793 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
3794 0, /* oplock_request */
3796 0, /* allocation_size */
3797 0, /* private_flags */
3802 NULL
, NULL
); /* create context */
3804 if (NT_STATUS_IS_OK(status
)) {
3805 close_file(req
, fsp
, NORMAL_CLOSE
);
3811 /****************************************************************************
3812 Receive notification that one of our open files has been renamed by another
3814 ****************************************************************************/
3816 void msg_file_was_renamed(struct messaging_context
*msg
,
3819 struct server_id server_id
,
3823 char *frm
= (char *)data
->data
;
3825 const char *sharepath
;
3826 const char *base_name
;
3827 const char *stream_name
;
3828 struct smb_filename
*smb_fname
= NULL
;
3829 size_t sp_len
, bn_len
;
3831 struct smbd_server_connection
*sconn
=
3832 talloc_get_type_abort(private_data
,
3833 struct smbd_server_connection
);
3835 if (data
->data
== NULL
3836 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3837 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3838 (int)data
->length
));
3842 /* Unpack the message. */
3843 pull_file_id_24(frm
, &id
);
3844 sharepath
= &frm
[24];
3845 sp_len
= strlen(sharepath
);
3846 base_name
= sharepath
+ sp_len
+ 1;
3847 bn_len
= strlen(base_name
);
3848 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3850 /* stream_name must always be NULL if there is no stream. */
3851 if (stream_name
[0] == '\0') {
3855 smb_fname
= synthetic_smb_fname(talloc_tos(),
3860 if (smb_fname
== NULL
) {
3864 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3866 sharepath
, smb_fname_str_dbg(smb_fname
),
3867 file_id_string_tos(&id
)));
3869 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3870 fsp
= file_find_di_next(fsp
)) {
3871 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3873 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3874 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
3875 smb_fname_str_dbg(smb_fname
)));
3876 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3877 if (!NT_STATUS_IS_OK(status
)) {
3882 /* Now we have the complete path we can work out if this is
3883 actually within this share and adjust newname accordingly. */
3884 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3885 "not sharepath %s) "
3886 "%s from %s -> %s\n",
3887 fsp
->conn
->connectpath
,
3891 smb_fname_str_dbg(smb_fname
)));
3895 TALLOC_FREE(smb_fname
);
3900 * If a main file is opened for delete, all streams need to be checked for
3901 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3902 * If that works, delete them all by setting the delete on close and close.
3905 static NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3906 const struct smb_filename
*smb_fname
)
3908 struct stream_struct
*stream_info
= NULL
;
3909 files_struct
**streams
= NULL
;
3911 unsigned int num_streams
= 0;
3912 TALLOC_CTX
*frame
= talloc_stackframe();
3915 status
= vfs_streaminfo(conn
, NULL
, smb_fname
, talloc_tos(),
3916 &num_streams
, &stream_info
);
3918 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3919 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3920 DEBUG(10, ("no streams around\n"));
3922 return NT_STATUS_OK
;
3925 if (!NT_STATUS_IS_OK(status
)) {
3926 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3927 nt_errstr(status
)));
3931 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3934 if (num_streams
== 0) {
3936 return NT_STATUS_OK
;
3939 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3940 if (streams
== NULL
) {
3941 DEBUG(0, ("talloc failed\n"));
3942 status
= NT_STATUS_NO_MEMORY
;
3946 for (i
=0; i
<num_streams
; i
++) {
3947 struct smb_filename
*smb_fname_cp
;
3949 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3954 smb_fname_cp
= synthetic_smb_fname(talloc_tos(),
3955 smb_fname
->base_name
,
3956 stream_info
[i
].name
,
3959 ~SMB_FILENAME_POSIX_PATH
));
3960 if (smb_fname_cp
== NULL
) {
3961 status
= NT_STATUS_NO_MEMORY
;
3965 if (SMB_VFS_STAT(conn
, smb_fname_cp
) == -1) {
3966 DEBUG(10, ("Unable to stat stream: %s\n",
3967 smb_fname_str_dbg(smb_fname_cp
)));
3970 status
= SMB_VFS_CREATE_FILE(
3973 0, /* root_dir_fid */
3974 smb_fname_cp
, /* fname */
3975 DELETE_ACCESS
, /* access_mask */
3976 (FILE_SHARE_READ
| /* share_access */
3977 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3978 FILE_OPEN
, /* create_disposition*/
3979 0, /* create_options */
3980 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3981 0, /* oplock_request */
3983 0, /* allocation_size */
3984 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3987 &streams
[i
], /* result */
3989 NULL
, NULL
); /* create context */
3991 if (!NT_STATUS_IS_OK(status
)) {
3992 DEBUG(10, ("Could not open stream %s: %s\n",
3993 smb_fname_str_dbg(smb_fname_cp
),
3994 nt_errstr(status
)));
3996 TALLOC_FREE(smb_fname_cp
);
3999 TALLOC_FREE(smb_fname_cp
);
4003 * don't touch the variable "status" beyond this point :-)
4006 for (i
-= 1 ; i
>= 0; i
--) {
4007 if (streams
[i
] == NULL
) {
4011 DEBUG(10, ("Closing stream # %d, %s\n", i
,
4012 fsp_str_dbg(streams
[i
])));
4013 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
4021 /*********************************************************************
4022 Create a default ACL by inheriting from the parent. If no inheritance
4023 from the parent available, don't set anything. This will leave the actual
4024 permissions the new file or directory already got from the filesystem
4025 as the NT ACL when read.
4026 *********************************************************************/
4028 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
4030 TALLOC_CTX
*frame
= talloc_stackframe();
4031 char *parent_name
= NULL
;
4032 struct security_descriptor
*parent_desc
= NULL
;
4033 NTSTATUS status
= NT_STATUS_OK
;
4034 struct security_descriptor
*psd
= NULL
;
4035 const struct dom_sid
*owner_sid
= NULL
;
4036 const struct dom_sid
*group_sid
= NULL
;
4037 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
4038 struct security_token
*token
= fsp
->conn
->session_info
->security_token
;
4039 bool inherit_owner
=
4040 (lp_inherit_owner(SNUM(fsp
->conn
)) == INHERIT_OWNER_WINDOWS_AND_UNIX
);
4041 bool inheritable_components
= false;
4042 bool try_builtin_administrators
= false;
4043 const struct dom_sid
*BA_U_sid
= NULL
;
4044 const struct dom_sid
*BA_G_sid
= NULL
;
4045 bool try_system
= false;
4046 const struct dom_sid
*SY_U_sid
= NULL
;
4047 const struct dom_sid
*SY_G_sid
= NULL
;
4049 struct smb_filename
*parent_smb_fname
= NULL
;
4051 if (!parent_dirname(frame
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
4053 return NT_STATUS_NO_MEMORY
;
4055 parent_smb_fname
= synthetic_smb_fname(talloc_tos(),
4059 fsp
->fsp_name
->flags
);
4061 if (parent_smb_fname
== NULL
) {
4063 return NT_STATUS_NO_MEMORY
;
4066 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
4068 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
4071 if (!NT_STATUS_IS_OK(status
)) {
4076 inheritable_components
= sd_has_inheritable_components(parent_desc
,
4079 if (!inheritable_components
&& !inherit_owner
) {
4081 /* Nothing to inherit and not setting owner. */
4082 return NT_STATUS_OK
;
4085 /* Create an inherited descriptor from the parent. */
4087 if (DEBUGLEVEL
>= 10) {
4088 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
4089 fsp_str_dbg(fsp
) ));
4090 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
4093 /* Inherit from parent descriptor if "inherit owner" set. */
4094 if (inherit_owner
) {
4095 owner_sid
= parent_desc
->owner_sid
;
4096 group_sid
= parent_desc
->group_sid
;
4099 if (owner_sid
== NULL
) {
4100 if (security_token_has_builtin_administrators(token
)) {
4101 try_builtin_administrators
= true;
4102 } else if (security_token_is_system(token
)) {
4103 try_builtin_administrators
= true;
4108 if (group_sid
== NULL
&&
4109 token
->num_sids
== PRIMARY_GROUP_SID_INDEX
)
4111 if (security_token_is_system(token
)) {
4112 try_builtin_administrators
= true;
4117 if (try_builtin_administrators
) {
4122 ok
= sids_to_unixids(&global_sid_Builtin_Administrators
, 1, &ids
);
4126 BA_U_sid
= &global_sid_Builtin_Administrators
;
4127 BA_G_sid
= &global_sid_Builtin_Administrators
;
4130 BA_U_sid
= &global_sid_Builtin_Administrators
;
4133 BA_G_sid
= &global_sid_Builtin_Administrators
;
4146 ok
= sids_to_unixids(&global_sid_System
, 1, &ids
);
4150 SY_U_sid
= &global_sid_System
;
4151 SY_G_sid
= &global_sid_System
;
4154 SY_U_sid
= &global_sid_System
;
4157 SY_G_sid
= &global_sid_System
;
4165 if (owner_sid
== NULL
) {
4166 owner_sid
= BA_U_sid
;
4169 if (owner_sid
== NULL
) {
4170 owner_sid
= SY_U_sid
;
4173 if (group_sid
== NULL
) {
4174 group_sid
= SY_G_sid
;
4177 if (try_system
&& group_sid
== NULL
) {
4178 group_sid
= BA_G_sid
;
4181 if (owner_sid
== NULL
) {
4182 owner_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
4184 if (group_sid
== NULL
) {
4185 if (token
->num_sids
== PRIMARY_GROUP_SID_INDEX
) {
4186 group_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
4188 group_sid
= &token
->sids
[PRIMARY_GROUP_SID_INDEX
];
4192 status
= se_create_child_secdesc(frame
,
4199 if (!NT_STATUS_IS_OK(status
)) {
4204 /* If inheritable_components == false,
4205 se_create_child_secdesc()
4206 creates a security desriptor with a NULL dacl
4207 entry, but with SEC_DESC_DACL_PRESENT. We need
4208 to remove that flag. */
4210 if (!inheritable_components
) {
4211 security_info_sent
&= ~SECINFO_DACL
;
4212 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
4215 if (DEBUGLEVEL
>= 10) {
4216 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
4217 fsp_str_dbg(fsp
) ));
4218 NDR_PRINT_DEBUG(security_descriptor
, psd
);
4221 if (inherit_owner
) {
4222 /* We need to be root to force this. */
4225 status
= SMB_VFS_FSET_NT_ACL(fsp
,
4228 if (inherit_owner
) {
4236 * If we already have a lease, it must match the new file id. [MS-SMB2]
4237 * 3.3.5.9.8 speaks about INVALID_PARAMETER if an already used lease key is
4238 * used for a different file name.
4241 struct lease_match_state
{
4242 /* Input parameters. */
4243 TALLOC_CTX
*mem_ctx
;
4244 const char *servicepath
;
4245 const struct smb_filename
*fname
;
4248 /* Return parameters. */
4249 uint32_t num_file_ids
;
4250 struct file_id
*ids
;
4251 NTSTATUS match_status
;
4254 /*************************************************************
4255 File doesn't exist but this lease key+guid is already in use.
4257 This is only allowable in the dynamic share case where the
4258 service path must be different.
4260 There is a small race condition here in the multi-connection
4261 case where a client sends two create calls on different connections,
4262 where the file doesn't exist and one smbd creates the leases_db
4263 entry first, but this will get fixed by the multichannel cleanup
4264 when all identical client_guids get handled by a single smbd.
4265 **************************************************************/
4267 static void lease_match_parser_new_file(
4269 const struct leases_db_file
*files
,
4270 struct lease_match_state
*state
)
4274 for (i
= 0; i
< num_files
; i
++) {
4275 const struct leases_db_file
*f
= &files
[i
];
4276 if (strequal(state
->servicepath
, f
->servicepath
)) {
4277 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4282 /* Dynamic share case. Break leases on all other files. */
4283 state
->match_status
= leases_db_copy_file_ids(state
->mem_ctx
,
4287 if (!NT_STATUS_IS_OK(state
->match_status
)) {
4291 state
->num_file_ids
= num_files
;
4292 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4296 static void lease_match_parser(
4298 const struct leases_db_file
*files
,
4301 struct lease_match_state
*state
=
4302 (struct lease_match_state
*)private_data
;
4305 if (!state
->file_existed
) {
4307 * Deal with name mismatch or
4308 * possible dynamic share case separately
4309 * to make code clearer.
4311 lease_match_parser_new_file(num_files
,
4318 state
->match_status
= NT_STATUS_OK
;
4320 for (i
= 0; i
< num_files
; i
++) {
4321 const struct leases_db_file
*f
= &files
[i
];
4323 /* Everything should be the same. */
4324 if (!file_id_equal(&state
->id
, &f
->id
)) {
4325 /* This should catch all dynamic share cases. */
4326 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4329 if (!strequal(f
->servicepath
, state
->servicepath
)) {
4330 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4333 if (!strequal(f
->base_name
, state
->fname
->base_name
)) {
4334 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4337 if (!strequal(f
->stream_name
, state
->fname
->stream_name
)) {
4338 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4343 if (NT_STATUS_IS_OK(state
->match_status
)) {
4345 * Common case - just opening another handle on a
4346 * file on a non-dynamic share.
4351 if (NT_STATUS_EQUAL(state
->match_status
, NT_STATUS_INVALID_PARAMETER
)) {
4352 /* Mismatched path. Error back to client. */
4357 * File id mismatch. Dynamic share case NT_STATUS_OPLOCK_NOT_GRANTED.
4358 * Don't allow leases.
4361 state
->match_status
= leases_db_copy_file_ids(state
->mem_ctx
,
4365 if (!NT_STATUS_IS_OK(state
->match_status
)) {
4369 state
->num_file_ids
= num_files
;
4370 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4374 static NTSTATUS
lease_match(connection_struct
*conn
,
4375 struct smb_request
*req
,
4376 struct smb2_lease_key
*lease_key
,
4377 const char *servicepath
,
4378 const struct smb_filename
*fname
,
4379 uint16_t *p_version
,
4382 struct smbd_server_connection
*sconn
= req
->sconn
;
4383 TALLOC_CTX
*tos
= talloc_tos();
4384 struct lease_match_state state
= {
4386 .servicepath
= servicepath
,
4388 .match_status
= NT_STATUS_OK
4393 state
.file_existed
= VALID_STAT(fname
->st
);
4394 if (state
.file_existed
) {
4395 state
.id
= vfs_file_id_from_sbuf(conn
, &fname
->st
);
4397 memset(&state
.id
, '\0', sizeof(state
.id
));
4400 status
= leases_db_parse(&sconn
->client
->connections
->smb2
.client
.guid
,
4401 lease_key
, lease_match_parser
, &state
);
4402 if (!NT_STATUS_IS_OK(status
)) {
4404 * Not found or error means okay: We can make the lease pass
4406 return NT_STATUS_OK
;
4408 if (!NT_STATUS_EQUAL(state
.match_status
, NT_STATUS_OPLOCK_NOT_GRANTED
)) {
4410 * Anything but NT_STATUS_OPLOCK_NOT_GRANTED, let the caller
4413 return state
.match_status
;
4416 /* We have to break all existing leases. */
4417 for (i
= 0; i
< state
.num_file_ids
; i
++) {
4418 struct share_mode_lock
*lck
;
4419 struct share_mode_data
*d
;
4422 if (file_id_equal(&state
.ids
[i
], &state
.id
)) {
4423 /* Don't need to break our own file. */
4427 lck
= get_existing_share_mode_lock(talloc_tos(), state
.ids
[i
]);
4429 /* Race condition - file already closed. */
4433 for (j
=0; j
<d
->num_share_modes
; j
++) {
4434 struct share_mode_entry
*e
= &d
->share_modes
[j
];
4435 uint32_t e_lease_type
= get_lease_type(d
, e
);
4436 struct share_mode_lease
*l
= NULL
;
4438 if (share_mode_stale_pid(d
, j
)) {
4442 if (e
->op_type
== LEASE_OPLOCK
) {
4443 l
= &lck
->data
->leases
[e
->lease_idx
];
4444 if (!smb2_lease_key_equal(&l
->lease_key
,
4448 *p_epoch
= l
->epoch
;
4449 *p_version
= l
->lease_version
;
4452 if (e_lease_type
== SMB2_LEASE_NONE
) {
4456 send_break_message(conn
->sconn
->msg_ctx
, e
,
4460 * Windows 7 and 8 lease clients
4461 * are broken in that they will not
4462 * respond to lease break requests
4463 * whilst waiting for an outstanding
4464 * open request on that lease handle
4465 * on the same TCP connection, due
4466 * to holding an internal inode lock.
4468 * This means we can't reschedule
4469 * ourselves here, but must return
4474 * Send the breaks and then return
4475 * SMB2_LEASE_NONE in the lease handle
4476 * to cause them to acknowledge the
4477 * lease break. Consulatation with
4478 * Microsoft engineering confirmed
4479 * this approach is safe.
4486 * Ensure we don't grant anything more so we
4489 return NT_STATUS_OPLOCK_NOT_GRANTED
;
4493 * Wrapper around open_file_ntcreate and open_directory
4496 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
4497 struct smb_request
*req
,
4498 struct smb_filename
*smb_fname
,
4499 uint32_t access_mask
,
4500 uint32_t share_access
,
4501 uint32_t create_disposition
,
4502 uint32_t create_options
,
4503 uint32_t file_attributes
,
4504 uint32_t oplock_request
,
4505 struct smb2_lease
*lease
,
4506 uint64_t allocation_size
,
4507 uint32_t private_flags
,
4508 struct security_descriptor
*sd
,
4509 struct ea_list
*ea_list
,
4511 files_struct
**result
,
4514 int info
= FILE_WAS_OPENED
;
4515 files_struct
*base_fsp
= NULL
;
4516 files_struct
*fsp
= NULL
;
4519 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
4520 "file_attributes = 0x%x, share_access = 0x%x, "
4521 "create_disposition = 0x%x create_options = 0x%x "
4522 "oplock_request = 0x%x private_flags = 0x%x "
4523 "ea_list = 0x%p, sd = 0x%p, "
4525 (unsigned int)access_mask
,
4526 (unsigned int)file_attributes
,
4527 (unsigned int)share_access
,
4528 (unsigned int)create_disposition
,
4529 (unsigned int)create_options
,
4530 (unsigned int)oplock_request
,
4531 (unsigned int)private_flags
,
4532 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4534 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
4535 status
= NT_STATUS_NOT_SUPPORTED
;
4539 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
4540 status
= NT_STATUS_INVALID_PARAMETER
;
4545 oplock_request
|= INTERNAL_OPEN_ONLY
;
4548 if (lease
!= NULL
) {
4549 uint16_t epoch
= lease
->lease_epoch
;
4550 uint16_t version
= lease
->lease_version
;
4551 status
= lease_match(conn
,
4558 if (NT_STATUS_EQUAL(status
, NT_STATUS_OPLOCK_NOT_GRANTED
)) {
4559 /* Dynamic share file. No leases and update epoch... */
4560 lease
->lease_state
= SMB2_LEASE_NONE
;
4561 lease
->lease_epoch
= epoch
;
4562 lease
->lease_version
= version
;
4563 } else if (!NT_STATUS_IS_OK(status
)) {
4568 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
4569 && (access_mask
& DELETE_ACCESS
)
4570 && !is_ntfs_stream_smb_fname(smb_fname
)) {
4572 * We can't open a file with DELETE access if any of the
4573 * streams is open without FILE_SHARE_DELETE
4575 status
= open_streams_for_delete(conn
, smb_fname
);
4577 if (!NT_STATUS_IS_OK(status
)) {
4582 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
4583 !security_token_has_privilege(get_current_nttok(conn
),
4584 SEC_PRIV_SECURITY
)) {
4585 DEBUG(10, ("create_file_unixpath: open on %s "
4586 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
4587 smb_fname_str_dbg(smb_fname
)));
4588 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
4592 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
4593 && is_ntfs_stream_smb_fname(smb_fname
)
4594 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
4595 uint32_t base_create_disposition
;
4596 struct smb_filename
*smb_fname_base
= NULL
;
4598 if (create_options
& FILE_DIRECTORY_FILE
) {
4599 status
= NT_STATUS_NOT_A_DIRECTORY
;
4603 switch (create_disposition
) {
4605 base_create_disposition
= FILE_OPEN
;
4608 base_create_disposition
= FILE_OPEN_IF
;
4612 /* Create an smb_filename with stream_name == NULL. */
4613 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
4614 smb_fname
->base_name
,
4618 if (smb_fname_base
== NULL
) {
4619 status
= NT_STATUS_NO_MEMORY
;
4623 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
4624 DEBUG(10, ("Unable to stat stream: %s\n",
4625 smb_fname_str_dbg(smb_fname_base
)));
4628 * https://bugzilla.samba.org/show_bug.cgi?id=10229
4629 * We need to check if the requested access mask
4630 * could be used to open the underlying file (if
4631 * it existed), as we're passing in zero for the
4632 * access mask to the base filename.
4634 status
= check_base_file_access(conn
,
4638 if (!NT_STATUS_IS_OK(status
)) {
4639 DEBUG(10, ("Permission check "
4640 "for base %s failed: "
4641 "%s\n", smb_fname
->base_name
,
4642 nt_errstr(status
)));
4647 /* Open the base file. */
4648 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
4651 | FILE_SHARE_DELETE
,
4652 base_create_disposition
,
4653 0, 0, 0, NULL
, 0, 0, NULL
, NULL
,
4655 TALLOC_FREE(smb_fname_base
);
4657 if (!NT_STATUS_IS_OK(status
)) {
4658 DEBUG(10, ("create_file_unixpath for base %s failed: "
4659 "%s\n", smb_fname
->base_name
,
4660 nt_errstr(status
)));
4663 /* we don't need the low level fd */
4668 * If it's a request for a directory open, deal with it separately.
4671 if (create_options
& FILE_DIRECTORY_FILE
) {
4673 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
4674 status
= NT_STATUS_INVALID_PARAMETER
;
4678 /* Can't open a temp directory. IFS kit test. */
4679 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
4680 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
4681 status
= NT_STATUS_INVALID_PARAMETER
;
4686 * We will get a create directory here if the Win32
4687 * app specified a security descriptor in the
4688 * CreateDirectory() call.
4692 status
= open_directory(
4693 conn
, req
, smb_fname
, access_mask
, share_access
,
4694 create_disposition
, create_options
, file_attributes
,
4699 * Ordinary file case.
4702 status
= file_new(req
, conn
, &fsp
);
4703 if(!NT_STATUS_IS_OK(status
)) {
4707 status
= fsp_set_smb_fname(fsp
, smb_fname
);
4708 if (!NT_STATUS_IS_OK(status
)) {
4714 * We're opening the stream element of a
4715 * base_fsp we already opened. Set up the
4718 fsp
->base_fsp
= base_fsp
;
4721 if (allocation_size
) {
4722 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
4726 status
= open_file_ntcreate(conn
,
4739 if(!NT_STATUS_IS_OK(status
)) {
4740 file_free(req
, fsp
);
4744 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
4746 /* A stream open never opens a directory */
4749 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4754 * Fail the open if it was explicitly a non-directory
4758 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
4759 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4764 status
= open_directory(
4765 conn
, req
, smb_fname
, access_mask
,
4766 share_access
, create_disposition
,
4767 create_options
, file_attributes
,
4772 if (!NT_STATUS_IS_OK(status
)) {
4776 fsp
->base_fsp
= base_fsp
;
4778 if ((ea_list
!= NULL
) &&
4779 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
4780 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
4781 if (!NT_STATUS_IS_OK(status
)) {
4786 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
4787 status
= NT_STATUS_ACCESS_DENIED
;
4791 /* Save the requested allocation size. */
4792 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
4793 if ((allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)
4794 && !(fsp
->is_directory
))
4796 fsp
->initial_allocation_size
= smb_roundup(
4797 fsp
->conn
, allocation_size
);
4798 if (vfs_allocate_file_space(
4799 fsp
, fsp
->initial_allocation_size
) == -1) {
4800 status
= NT_STATUS_DISK_FULL
;
4804 fsp
->initial_allocation_size
= smb_roundup(
4805 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
4808 fsp
->initial_allocation_size
= 0;
4811 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
4812 fsp
->base_fsp
== NULL
) {
4815 * According to the MS documentation, the only time the security
4816 * descriptor is applied to the opened file is iff we *created* the
4817 * file; an existing file stays the same.
4819 * Also, it seems (from observation) that you can open the file with
4820 * any access mask but you can still write the sd. We need to override
4821 * the granted access before we call set_sd
4822 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4825 uint32_t sec_info_sent
;
4826 uint32_t saved_access_mask
= fsp
->access_mask
;
4828 sec_info_sent
= get_sec_info(sd
);
4830 fsp
->access_mask
= FILE_GENERIC_ALL
;
4832 if (sec_info_sent
& (SECINFO_OWNER
|
4836 status
= set_sd(fsp
, sd
, sec_info_sent
);
4839 fsp
->access_mask
= saved_access_mask
;
4841 if (!NT_STATUS_IS_OK(status
)) {
4844 } else if (lp_inherit_acls(SNUM(conn
))) {
4845 /* Inherit from parent. Errors here are not fatal. */
4846 status
= inherit_new_acl(fsp
);
4847 if (!NT_STATUS_IS_OK(status
)) {
4848 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4850 nt_errstr(status
) ));
4855 if ((conn
->fs_capabilities
& FILE_FILE_COMPRESSION
)
4856 && (create_options
& FILE_NO_COMPRESSION
)
4857 && (info
== FILE_WAS_CREATED
)) {
4858 status
= SMB_VFS_SET_COMPRESSION(conn
, fsp
, fsp
,
4859 COMPRESSION_FORMAT_NONE
);
4860 if (!NT_STATUS_IS_OK(status
)) {
4861 DEBUG(1, ("failed to disable compression: %s\n",
4862 nt_errstr(status
)));
4866 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
4869 if (pinfo
!= NULL
) {
4873 smb_fname
->st
= fsp
->fsp_name
->st
;
4875 return NT_STATUS_OK
;
4878 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
4881 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
4883 * The close_file below will close
4888 close_file(req
, fsp
, ERROR_CLOSE
);
4891 if (base_fsp
!= NULL
) {
4892 close_file(req
, base_fsp
, ERROR_CLOSE
);
4899 * Calculate the full path name given a relative fid.
4901 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
4902 struct smb_request
*req
,
4903 uint16_t root_dir_fid
,
4904 const struct smb_filename
*smb_fname
,
4905 struct smb_filename
**smb_fname_out
)
4907 files_struct
*dir_fsp
;
4908 char *parent_fname
= NULL
;
4909 char *new_base_name
= NULL
;
4910 uint32_t ucf_flags
= ((req
!= NULL
&& req
->posix_pathnames
) ?
4911 UCF_POSIX_PATHNAMES
: 0);
4914 if (root_dir_fid
== 0 || !smb_fname
) {
4915 status
= NT_STATUS_INTERNAL_ERROR
;
4919 dir_fsp
= file_fsp(req
, root_dir_fid
);
4921 if (dir_fsp
== NULL
) {
4922 status
= NT_STATUS_INVALID_HANDLE
;
4926 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
4927 status
= NT_STATUS_INVALID_HANDLE
;
4931 if (!dir_fsp
->is_directory
) {
4934 * Check to see if this is a mac fork of some kind.
4937 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
4938 is_ntfs_stream_smb_fname(smb_fname
)) {
4939 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4944 we need to handle the case when we get a
4945 relative open relative to a file and the
4946 pathname is blank - this is a reopen!
4947 (hint from demyn plantenberg)
4950 status
= NT_STATUS_INVALID_HANDLE
;
4954 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
4956 * We're at the toplevel dir, the final file name
4957 * must not contain ./, as this is filtered out
4958 * normally by srvstr_get_path and unix_convert
4959 * explicitly rejects paths containing ./.
4961 parent_fname
= talloc_strdup(talloc_tos(), "");
4962 if (parent_fname
== NULL
) {
4963 status
= NT_STATUS_NO_MEMORY
;
4967 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
4970 * Copy in the base directory name.
4973 parent_fname
= talloc_array(talloc_tos(), char,
4975 if (parent_fname
== NULL
) {
4976 status
= NT_STATUS_NO_MEMORY
;
4979 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
4983 * Ensure it ends in a '/'.
4984 * We used TALLOC_SIZE +2 to add space for the '/'.
4988 && (parent_fname
[dir_name_len
-1] != '\\')
4989 && (parent_fname
[dir_name_len
-1] != '/')) {
4990 parent_fname
[dir_name_len
] = '/';
4991 parent_fname
[dir_name_len
+1] = '\0';
4995 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
4996 smb_fname
->base_name
);
4997 if (new_base_name
== NULL
) {
4998 status
= NT_STATUS_NO_MEMORY
;
5002 status
= filename_convert(req
,
5004 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
5009 if (!NT_STATUS_IS_OK(status
)) {
5014 TALLOC_FREE(parent_fname
);
5015 TALLOC_FREE(new_base_name
);
5019 NTSTATUS
create_file_default(connection_struct
*conn
,
5020 struct smb_request
*req
,
5021 uint16_t root_dir_fid
,
5022 struct smb_filename
*smb_fname
,
5023 uint32_t access_mask
,
5024 uint32_t share_access
,
5025 uint32_t create_disposition
,
5026 uint32_t create_options
,
5027 uint32_t file_attributes
,
5028 uint32_t oplock_request
,
5029 struct smb2_lease
*lease
,
5030 uint64_t allocation_size
,
5031 uint32_t private_flags
,
5032 struct security_descriptor
*sd
,
5033 struct ea_list
*ea_list
,
5034 files_struct
**result
,
5036 const struct smb2_create_blobs
*in_context_blobs
,
5037 struct smb2_create_blobs
*out_context_blobs
)
5039 int info
= FILE_WAS_OPENED
;
5040 files_struct
*fsp
= NULL
;
5042 bool stream_name
= false;
5044 DEBUG(10,("create_file: access_mask = 0x%x "
5045 "file_attributes = 0x%x, share_access = 0x%x, "
5046 "create_disposition = 0x%x create_options = 0x%x "
5047 "oplock_request = 0x%x "
5048 "private_flags = 0x%x "
5049 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
5051 (unsigned int)access_mask
,
5052 (unsigned int)file_attributes
,
5053 (unsigned int)share_access
,
5054 (unsigned int)create_disposition
,
5055 (unsigned int)create_options
,
5056 (unsigned int)oplock_request
,
5057 (unsigned int)private_flags
,
5058 (unsigned int)root_dir_fid
,
5059 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
5062 * Calculate the filename from the root_dir_if if necessary.
5065 if (root_dir_fid
!= 0) {
5066 struct smb_filename
*smb_fname_out
= NULL
;
5067 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
5068 smb_fname
, &smb_fname_out
);
5069 if (!NT_STATUS_IS_OK(status
)) {
5072 smb_fname
= smb_fname_out
;
5076 * Check to see if this is a mac fork of some kind.
5079 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
5081 enum FAKE_FILE_TYPE fake_file_type
;
5083 fake_file_type
= is_fake_file(smb_fname
);
5085 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
5088 * Here we go! support for changing the disk quotas
5091 * We need to fake up to open this MAGIC QUOTA file
5092 * and return a valid FID.
5094 * w2k close this file directly after openening xp
5095 * also tries a QUERY_FILE_INFO on the file and then
5098 status
= open_fake_file(req
, conn
, req
->vuid
,
5099 fake_file_type
, smb_fname
,
5101 if (!NT_STATUS_IS_OK(status
)) {
5105 ZERO_STRUCT(smb_fname
->st
);
5109 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
5110 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5115 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
5117 smb_fname
->stream_name
= NULL
;
5118 /* We have to handle this error here. */
5119 if (create_options
& FILE_DIRECTORY_FILE
) {
5120 status
= NT_STATUS_NOT_A_DIRECTORY
;
5123 if (req
!= NULL
&& req
->posix_pathnames
) {
5124 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
5126 ret
= SMB_VFS_STAT(conn
, smb_fname
);
5129 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
5130 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
5135 status
= create_file_unixpath(
5136 conn
, req
, smb_fname
, access_mask
, share_access
,
5137 create_disposition
, create_options
, file_attributes
,
5138 oplock_request
, lease
, allocation_size
, private_flags
,
5142 if (!NT_STATUS_IS_OK(status
)) {
5147 DEBUG(10, ("create_file: info=%d\n", info
));
5150 if (pinfo
!= NULL
) {
5153 return NT_STATUS_OK
;
5156 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
5159 close_file(req
, fsp
, ERROR_CLOSE
);