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
->base_name
,
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 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;
247 if (!parent_dirname(talloc_tos(),
248 smb_fname
->base_name
,
251 return NT_STATUS_NO_MEMORY
;
254 if (get_current_uid(conn
) == (uid_t
)0) {
255 /* I'm sorry sir, I didn't know you were root... */
256 DEBUG(10,("check_parent_access: root override "
257 "on %s. Granting 0x%x\n",
258 smb_fname_str_dbg(smb_fname
),
259 (unsigned int)access_mask
));
263 status
= SMB_VFS_GET_NT_ACL(conn
,
269 if (!NT_STATUS_IS_OK(status
)) {
270 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
271 "%s with error %s\n",
278 * If we can access the path to this file, by
279 * default we have FILE_READ_ATTRIBUTES from the
280 * containing directory. See the section:
281 * "Algorithm to Check Access to an Existing File"
284 * se_file_access_check() also takes care of
285 * owner WRITE_DAC and READ_CONTROL.
287 status
= se_file_access_check(parent_sd
,
288 get_current_nttok(conn
),
290 (access_mask
& ~FILE_READ_ATTRIBUTES
),
292 if(!NT_STATUS_IS_OK(status
)) {
293 DEBUG(5,("check_parent_access: access check "
294 "on directory %s for "
295 "path %s for mask 0x%x returned (0x%x) %s\n",
297 smb_fname
->base_name
,
300 nt_errstr(status
) ));
307 /****************************************************************************
308 Ensure when opening a base file for a stream open that we have permissions
309 to do so given the access mask on the base file.
310 ****************************************************************************/
312 static NTSTATUS
check_base_file_access(struct connection_struct
*conn
,
313 struct smb_filename
*smb_fname
,
314 uint32_t access_mask
)
318 status
= smbd_calculate_access_mask(conn
, smb_fname
,
322 if (!NT_STATUS_IS_OK(status
)) {
323 DEBUG(10, ("smbd_calculate_access_mask "
324 "on file %s returned %s\n",
325 smb_fname_str_dbg(smb_fname
),
330 if (access_mask
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) {
332 if (!CAN_WRITE(conn
)) {
333 return NT_STATUS_ACCESS_DENIED
;
335 dosattrs
= dos_mode(conn
, smb_fname
);
336 if (IS_DOS_READONLY(dosattrs
)) {
337 return NT_STATUS_ACCESS_DENIED
;
341 return smbd_check_access_rights(conn
,
347 /****************************************************************************
348 fd support routines - attempt to do a dos_open.
349 ****************************************************************************/
351 NTSTATUS
fd_open(struct connection_struct
*conn
,
356 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
357 NTSTATUS status
= NT_STATUS_OK
;
361 * Never follow symlinks on a POSIX client. The
362 * client should be doing this.
365 if (fsp
->posix_open
|| !lp_follow_symlinks(SNUM(conn
))) {
370 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
371 if (fsp
->fh
->fd
== -1) {
372 int posix_errno
= errno
;
374 #if defined(ENOTSUP) && defined(OSF1)
375 /* handle special Tru64 errno */
376 if (errno
== ENOTSUP
) {
381 /* fix broken NetBSD errno */
382 if (errno
== EFTYPE
) {
386 /* fix broken FreeBSD errno */
387 if (errno
== EMLINK
) {
390 #endif /* O_NOFOLLOW */
391 status
= map_nt_error_from_unix(posix_errno
);
392 if (errno
== EMFILE
) {
393 static time_t last_warned
= 0L;
395 if (time((time_t *) NULL
) > last_warned
) {
396 DEBUG(0,("Too many open files, unable "
397 "to open more! smbd's max "
399 lp_max_open_files()));
400 last_warned
= time((time_t *) NULL
);
406 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
407 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
408 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
413 /****************************************************************************
414 Close the file associated with a fsp.
415 ****************************************************************************/
417 NTSTATUS
fd_close(files_struct
*fsp
)
424 if (fsp
->fh
->fd
== -1) {
425 return NT_STATUS_OK
; /* What we used to call a stat open. */
427 if (fsp
->fh
->ref_count
> 1) {
428 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
431 ret
= SMB_VFS_CLOSE(fsp
);
434 return map_nt_error_from_unix(errno
);
439 /****************************************************************************
440 Change the ownership of a file to that of the parent directory.
441 Do this by fd if possible.
442 ****************************************************************************/
444 void change_file_owner_to_parent(connection_struct
*conn
,
445 const char *inherit_from_dir
,
448 struct smb_filename
*smb_fname_parent
;
451 smb_fname_parent
= synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
453 if (smb_fname_parent
== NULL
) {
457 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
459 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
460 "directory %s. Error was %s\n",
461 smb_fname_str_dbg(smb_fname_parent
),
463 TALLOC_FREE(smb_fname_parent
);
467 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
468 /* Already this uid - no need to change. */
469 DEBUG(10,("change_file_owner_to_parent: file %s "
470 "is already owned by uid %d\n",
472 (int)fsp
->fsp_name
->st
.st_ex_uid
));
473 TALLOC_FREE(smb_fname_parent
);
478 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
481 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
482 "file %s to parent directory uid %u. Error "
483 "was %s\n", fsp_str_dbg(fsp
),
484 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
487 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
488 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
489 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
490 /* Ensure the uid entry is updated. */
491 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
494 TALLOC_FREE(smb_fname_parent
);
497 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
498 const char *inherit_from_dir
,
500 SMB_STRUCT_STAT
*psbuf
)
502 struct smb_filename
*smb_fname_parent
;
503 struct smb_filename
*smb_fname_cwd
= NULL
;
504 char *saved_dir
= NULL
;
505 TALLOC_CTX
*ctx
= talloc_tos();
506 NTSTATUS status
= NT_STATUS_OK
;
509 smb_fname_parent
= synthetic_smb_fname(ctx
, inherit_from_dir
,
511 if (smb_fname_parent
== NULL
) {
512 return NT_STATUS_NO_MEMORY
;
515 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
517 status
= map_nt_error_from_unix(errno
);
518 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
519 "directory %s. Error was %s\n",
520 smb_fname_str_dbg(smb_fname_parent
),
525 /* We've already done an lstat into psbuf, and we know it's a
526 directory. If we can cd into the directory and the dev/ino
527 are the same then we can safely chown without races as
528 we're locking the directory in place by being in it. This
529 should work on any UNIX (thanks tridge :-). JRA.
532 saved_dir
= vfs_GetWd(ctx
,conn
);
534 status
= map_nt_error_from_unix(errno
);
535 DEBUG(0,("change_dir_owner_to_parent: failed to get "
536 "current working directory. Error was %s\n",
541 /* Chdir into the new path. */
542 if (vfs_ChDir(conn
, fname
) == -1) {
543 status
= map_nt_error_from_unix(errno
);
544 DEBUG(0,("change_dir_owner_to_parent: failed to change "
545 "current working directory to %s. Error "
546 "was %s\n", fname
, strerror(errno
) ));
550 smb_fname_cwd
= synthetic_smb_fname(ctx
, ".", NULL
, NULL
);
551 if (smb_fname_cwd
== NULL
) {
552 status
= NT_STATUS_NO_MEMORY
;
556 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
558 status
= map_nt_error_from_unix(errno
);
559 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
560 "directory '.' (%s) Error was %s\n",
561 fname
, strerror(errno
)));
565 /* Ensure we're pointing at the same place. */
566 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
567 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
568 DEBUG(0,("change_dir_owner_to_parent: "
569 "device/inode on directory %s changed. "
570 "Refusing to chown !\n", fname
));
571 status
= NT_STATUS_ACCESS_DENIED
;
575 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
576 /* Already this uid - no need to change. */
577 DEBUG(10,("change_dir_owner_to_parent: directory %s "
578 "is already owned by uid %d\n",
580 (int)smb_fname_cwd
->st
.st_ex_uid
));
581 status
= NT_STATUS_OK
;
586 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
590 status
= map_nt_error_from_unix(errno
);
591 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
592 "directory %s to parent directory uid %u. "
593 "Error was %s\n", fname
,
594 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
597 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
598 "directory %s to parent directory uid %u.\n",
599 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
600 /* Ensure the uid entry is updated. */
601 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
605 vfs_ChDir(conn
,saved_dir
);
607 TALLOC_FREE(smb_fname_parent
);
608 TALLOC_FREE(smb_fname_cwd
);
612 /****************************************************************************
613 Open a file - returning a guaranteed ATOMIC indication of if the
614 file was created or not.
615 ****************************************************************************/
617 static NTSTATUS
fd_open_atomic(struct connection_struct
*conn
,
623 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
624 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
626 *file_created
= false;
628 if (!(flags
& O_CREAT
)) {
630 * We're not creating the file, just pass through.
632 return fd_open(conn
, fsp
, flags
, mode
);
635 if (flags
& O_EXCL
) {
637 * Fail if already exists, just pass through.
639 status
= fd_open(conn
, fsp
, flags
, mode
);
642 * Here we've opened with O_CREAT|O_EXCL. If that went
643 * NT_STATUS_OK, we *know* we created this file.
645 *file_created
= NT_STATUS_IS_OK(status
);
651 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
652 * To know absolutely if we created the file or not,
653 * we can never call O_CREAT without O_EXCL. So if
654 * we think the file existed, try without O_CREAT|O_EXCL.
655 * If we think the file didn't exist, try with
656 * O_CREAT|O_EXCL. Keep bouncing between these two
657 * requests until either the file is created, or
658 * opened. Either way, we keep going until we get
659 * a returnable result (error, or open/create).
663 int curr_flags
= flags
;
666 /* Just try open, do not create. */
667 curr_flags
&= ~(O_CREAT
);
668 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
669 if (NT_STATUS_EQUAL(status
,
670 NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
672 * Someone deleted it in the meantime.
675 file_existed
= false;
676 DEBUG(10,("fd_open_atomic: file %s existed. "
678 smb_fname_str_dbg(fsp
->fsp_name
)));
682 /* Try create exclusively, fail if it exists. */
683 curr_flags
|= O_EXCL
;
684 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
685 if (NT_STATUS_EQUAL(status
,
686 NT_STATUS_OBJECT_NAME_COLLISION
)) {
688 * Someone created it in the meantime.
689 * Retry without O_CREAT.
692 DEBUG(10,("fd_open_atomic: file %s "
693 "did not exist. Retry.\n",
694 smb_fname_str_dbg(fsp
->fsp_name
)));
697 if (NT_STATUS_IS_OK(status
)) {
699 * Here we've opened with O_CREAT|O_EXCL
700 * and got success. We *know* we created
703 *file_created
= true;
706 /* Create is done, or failed. */
712 /****************************************************************************
714 ****************************************************************************/
716 static NTSTATUS
open_file(files_struct
*fsp
,
717 connection_struct
*conn
,
718 struct smb_request
*req
,
719 const char *parent_dir
,
722 uint32_t access_mask
, /* client requested access mask. */
723 uint32_t open_access_mask
, /* what we're actually using in the open. */
724 bool *p_file_created
)
726 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
727 NTSTATUS status
= NT_STATUS_OK
;
728 int accmode
= (flags
& O_ACCMODE
);
729 int local_flags
= flags
;
730 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
735 /* Check permissions */
738 * This code was changed after seeing a client open request
739 * containing the open mode of (DENY_WRITE/read-only) with
740 * the 'create if not exist' bit set. The previous code
741 * would fail to open the file read only on a read-only share
742 * as it was checking the flags parameter directly against O_RDONLY,
743 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
747 if (!CAN_WRITE(conn
)) {
748 /* It's a read-only share - fail if we wanted to write. */
749 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
750 DEBUG(3,("Permission denied opening %s\n",
751 smb_fname_str_dbg(smb_fname
)));
752 return NT_STATUS_ACCESS_DENIED
;
754 if (flags
& O_CREAT
) {
755 /* We don't want to write - but we must make sure that
756 O_CREAT doesn't create the file if we have write
757 access into the directory.
759 flags
&= ~(O_CREAT
|O_EXCL
);
760 local_flags
&= ~(O_CREAT
|O_EXCL
);
765 * This little piece of insanity is inspired by the
766 * fact that an NT client can open a file for O_RDONLY,
767 * but set the create disposition to FILE_EXISTS_TRUNCATE.
768 * If the client *can* write to the file, then it expects to
769 * truncate the file, even though it is opening for readonly.
770 * Quicken uses this stupid trick in backup file creation...
771 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
772 * for helping track this one down. It didn't bite us in 2.0.x
773 * as we always opened files read-write in that release. JRA.
776 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
777 DEBUG(10,("open_file: truncate requested on read-only open "
778 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
779 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
782 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
783 (!file_existed
&& (local_flags
& O_CREAT
)) ||
784 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
788 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
790 * We would block on opening a FIFO with no one else on the
791 * other end. Do what we used to do and add O_NONBLOCK to the
795 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
796 local_flags
&= ~O_TRUNC
; /* Can't truncate a FIFO. */
797 local_flags
|= O_NONBLOCK
;
801 /* Don't create files with Microsoft wildcard characters. */
804 * wildcard characters are allowed in stream names
805 * only test the basefilename
807 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
809 wild
= smb_fname
->base_name
;
811 if ((local_flags
& O_CREAT
) && !file_existed
&&
813 return NT_STATUS_OBJECT_NAME_INVALID
;
816 /* Can we access this file ? */
817 if (!fsp
->base_fsp
) {
818 /* Only do this check on non-stream open. */
820 status
= smbd_check_access_rights(conn
,
825 if (!NT_STATUS_IS_OK(status
)) {
826 DEBUG(10, ("open_file: "
827 "smbd_check_access_rights "
828 "on file %s returned %s\n",
829 smb_fname_str_dbg(smb_fname
),
833 if (!NT_STATUS_IS_OK(status
) &&
834 !NT_STATUS_EQUAL(status
,
835 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
840 if (NT_STATUS_EQUAL(status
,
841 NT_STATUS_OBJECT_NAME_NOT_FOUND
))
843 DEBUG(10, ("open_file: "
844 "file %s vanished since we "
845 "checked for existence.\n",
846 smb_fname_str_dbg(smb_fname
)));
847 file_existed
= false;
848 SET_STAT_INVALID(fsp
->fsp_name
->st
);
853 if (!(local_flags
& O_CREAT
)) {
854 /* File didn't exist and no O_CREAT. */
855 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
858 status
= check_parent_access(conn
,
861 if (!NT_STATUS_IS_OK(status
)) {
862 DEBUG(10, ("open_file: "
863 "check_parent_access on "
864 "file %s returned %s\n",
865 smb_fname_str_dbg(smb_fname
),
866 nt_errstr(status
) ));
873 * Actually do the open - if O_TRUNC is needed handle it
874 * below under the share mode lock.
876 status
= fd_open_atomic(conn
, fsp
, local_flags
& ~O_TRUNC
,
877 unx_mode
, p_file_created
);
878 if (!NT_STATUS_IS_OK(status
)) {
879 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
880 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
881 nt_errstr(status
),local_flags
,flags
));
885 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
887 /* If we have an fd, this stat should succeed. */
888 DEBUG(0,("Error doing fstat on open file %s "
890 smb_fname_str_dbg(smb_fname
),
892 status
= map_nt_error_from_unix(errno
);
897 if (*p_file_created
) {
898 /* We created this file. */
900 bool need_re_stat
= false;
901 /* Do all inheritance work after we've
902 done a successful fstat call and filled
903 in the stat struct in fsp->fsp_name. */
905 /* Inherit the ACL if required */
906 if (lp_inherit_permissions(SNUM(conn
))) {
907 inherit_access_posix_acl(conn
, parent_dir
,
908 smb_fname
->base_name
,
913 /* Change the owner if required. */
914 if (lp_inherit_owner(SNUM(conn
))) {
915 change_file_owner_to_parent(conn
, parent_dir
,
921 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
922 /* If we have an fd, this stat should succeed. */
924 DEBUG(0,("Error doing fstat on open file %s "
926 smb_fname_str_dbg(smb_fname
),
931 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
932 FILE_NOTIFY_CHANGE_FILE_NAME
,
933 smb_fname
->base_name
);
936 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
938 /* File must exist for a stat open. */
939 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
942 status
= smbd_check_access_rights(conn
,
947 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
949 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
950 /* This is a POSIX stat open for delete
951 * or rename on a symlink that points
953 DEBUG(10,("open_file: allowing POSIX "
954 "open on bad symlink %s\n",
955 smb_fname_str_dbg(smb_fname
)));
956 status
= NT_STATUS_OK
;
959 if (!NT_STATUS_IS_OK(status
)) {
960 DEBUG(10,("open_file: "
961 "smbd_check_access_rights on file "
963 smb_fname_str_dbg(smb_fname
),
964 nt_errstr(status
) ));
970 * POSIX allows read-only opens of directories. We don't
971 * want to do this (we use a different code path for this)
972 * so catch a directory open and return an EISDIR. JRA.
975 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
978 return NT_STATUS_FILE_IS_A_DIRECTORY
;
981 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
982 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
983 fsp
->file_pid
= req
? req
->smbpid
: 0;
984 fsp
->can_lock
= True
;
985 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
988 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
989 fsp
->print_file
= NULL
;
990 fsp
->modified
= False
;
991 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
992 fsp
->is_directory
= False
;
993 if (conn
->aio_write_behind_list
&&
994 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
995 conn
->case_sensitive
)) {
996 fsp
->aio_write_behind
= True
;
999 fsp
->wcp
= NULL
; /* Write cache pointer. */
1001 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
1002 conn
->session_info
->unix_info
->unix_name
,
1003 smb_fname_str_dbg(smb_fname
),
1004 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
1005 conn
->num_files_open
));
1008 return NT_STATUS_OK
;
1011 /****************************************************************************
1012 Check if we can open a file with a share mode.
1013 Returns True if conflict, False if not.
1014 ****************************************************************************/
1016 static bool share_conflict(struct share_mode_entry
*entry
,
1017 uint32_t access_mask
,
1018 uint32_t share_access
)
1020 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
1021 "entry->share_access = 0x%x, "
1022 "entry->private_options = 0x%x\n",
1023 (unsigned int)entry
->access_mask
,
1024 (unsigned int)entry
->share_access
,
1025 (unsigned int)entry
->private_options
));
1027 if (server_id_is_disconnected(&entry
->pid
)) {
1029 * note: cleanup should have been done by
1030 * delay_for_batch_oplocks()
1035 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
1036 (unsigned int)access_mask
, (unsigned int)share_access
));
1038 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
1042 DELETE_ACCESS
)) == 0) {
1043 DEBUG(10,("share_conflict: No conflict due to "
1044 "entry->access_mask = 0x%x\n",
1045 (unsigned int)entry
->access_mask
));
1049 if ((access_mask
& (FILE_WRITE_DATA
|
1053 DELETE_ACCESS
)) == 0) {
1054 DEBUG(10,("share_conflict: No conflict due to "
1055 "access_mask = 0x%x\n",
1056 (unsigned int)access_mask
));
1060 #if 1 /* JRA TEST - Superdebug. */
1061 #define CHECK_MASK(num, am, right, sa, share) \
1062 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1063 (unsigned int)(num), (unsigned int)(am), \
1064 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1065 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1066 (unsigned int)(num), (unsigned int)(sa), \
1067 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1068 if (((am) & (right)) && !((sa) & (share))) { \
1069 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1070 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1071 (unsigned int)(share) )); \
1075 #define CHECK_MASK(num, am, right, sa, share) \
1076 if (((am) & (right)) && !((sa) & (share))) { \
1077 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1078 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1079 (unsigned int)(share) )); \
1084 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1085 share_access
, FILE_SHARE_WRITE
);
1086 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1087 entry
->share_access
, FILE_SHARE_WRITE
);
1089 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1090 share_access
, FILE_SHARE_READ
);
1091 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1092 entry
->share_access
, FILE_SHARE_READ
);
1094 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
1095 share_access
, FILE_SHARE_DELETE
);
1096 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
1097 entry
->share_access
, FILE_SHARE_DELETE
);
1099 DEBUG(10,("share_conflict: No conflict.\n"));
1103 #if defined(DEVELOPER)
1104 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
1106 struct share_mode_entry
*share_entry
)
1108 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1111 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1115 if (share_entry
->op_mid
== 0) {
1116 /* INTERNAL_OPEN_ONLY */
1120 if (!is_valid_share_mode_entry(share_entry
)) {
1124 fsp
= file_find_dif(sconn
, share_entry
->id
,
1125 share_entry
->share_file_id
);
1127 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1128 share_mode_str(talloc_tos(), num
, share_entry
) ));
1129 smb_panic("validate_my_share_entries: Cannot match a "
1130 "share entry with an open file\n");
1133 if (((uint16_t)fsp
->oplock_type
) != share_entry
->op_type
) {
1142 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1143 share_mode_str(talloc_tos(), num
, share_entry
) ));
1144 str
= talloc_asprintf(talloc_tos(),
1145 "validate_my_share_entries: "
1146 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1147 fsp
->fsp_name
->base_name
,
1148 (unsigned int)fsp
->oplock_type
,
1149 (unsigned int)share_entry
->op_type
);
1155 bool is_stat_open(uint32_t access_mask
)
1157 const uint32_t stat_open_bits
=
1158 (SYNCHRONIZE_ACCESS
|
1159 FILE_READ_ATTRIBUTES
|
1160 FILE_WRITE_ATTRIBUTES
);
1162 return (((access_mask
& stat_open_bits
) != 0) &&
1163 ((access_mask
& ~stat_open_bits
) == 0));
1166 static bool has_delete_on_close(struct share_mode_lock
*lck
,
1169 struct share_mode_data
*d
= lck
->data
;
1172 if (d
->num_share_modes
== 0) {
1175 if (!is_delete_on_close_set(lck
, name_hash
)) {
1178 for (i
=0; i
<d
->num_share_modes
; i
++) {
1179 if (!share_mode_stale_pid(d
, i
)) {
1186 /****************************************************************************
1187 Deal with share modes
1188 Invariant: Share mode must be locked on entry and exit.
1189 Returns -1 on error, or number of share modes on success (may be zero).
1190 ****************************************************************************/
1192 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1193 struct share_mode_lock
*lck
,
1194 uint32_t access_mask
,
1195 uint32_t share_access
)
1199 if(lck
->data
->num_share_modes
== 0) {
1200 return NT_STATUS_OK
;
1203 if (is_stat_open(access_mask
)) {
1204 /* Stat open that doesn't trigger oplock breaks or share mode
1205 * checks... ! JRA. */
1206 return NT_STATUS_OK
;
1210 * Check if the share modes will give us access.
1213 #if defined(DEVELOPER)
1214 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1215 validate_my_share_entries(conn
->sconn
, i
,
1216 &lck
->data
->share_modes
[i
]);
1220 /* Now we check the share modes, after any oplock breaks. */
1221 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1223 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1227 /* someone else has a share lock on it, check to see if we can
1229 if (share_conflict(&lck
->data
->share_modes
[i
],
1230 access_mask
, share_access
)) {
1232 if (share_mode_stale_pid(lck
->data
, i
)) {
1236 return NT_STATUS_SHARING_VIOLATION
;
1240 return NT_STATUS_OK
;
1244 * Send a break message to the oplock holder and delay the open for
1248 NTSTATUS
send_break_message(struct messaging_context
*msg_ctx
,
1249 const struct share_mode_entry
*exclusive
,
1253 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1254 struct server_id_buf tmp
;
1256 DEBUG(10, ("Sending break request to PID %s\n",
1257 server_id_str_buf(exclusive
->pid
, &tmp
)));
1259 /* Create the message. */
1260 share_mode_entry_to_message(msg
, exclusive
);
1262 /* Overload entry->op_type */
1264 * This is a cut from uint32_t to uint16_t, but so far only the lower 3
1265 * bits (LEASE_WRITE/HANDLE/READ are used anyway.
1267 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
, break_to
);
1269 status
= messaging_send_buf(msg_ctx
, exclusive
->pid
,
1270 MSG_SMB_BREAK_REQUEST
,
1271 (uint8_t *)msg
, sizeof(msg
));
1272 if (!NT_STATUS_IS_OK(status
)) {
1273 DEBUG(3, ("Could not send oplock break message: %s\n",
1274 nt_errstr(status
)));
1281 * Do internal consistency checks on the share mode for a file.
1284 static bool validate_oplock_types(struct share_mode_lock
*lck
)
1286 struct share_mode_data
*d
= lck
->data
;
1288 bool ex_or_batch
= false;
1289 bool level2
= false;
1290 bool no_oplock
= false;
1291 uint32_t num_non_stat_opens
= 0;
1294 for (i
=0; i
<d
->num_share_modes
; i
++) {
1295 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1297 if (!is_valid_share_mode_entry(e
)) {
1301 if (e
->op_mid
== 0) {
1302 /* INTERNAL_OPEN_ONLY */
1306 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1307 /* We ignore stat opens in the table - they
1308 always have NO_OPLOCK and never get or
1309 cause breaks. JRA. */
1313 num_non_stat_opens
+= 1;
1315 if (BATCH_OPLOCK_TYPE(e
->op_type
)) {
1316 /* batch - can only be one. */
1317 if (share_mode_stale_pid(d
, i
)) {
1318 DEBUG(10, ("Found stale batch oplock\n"));
1321 if (ex_or_batch
|| batch
|| level2
|| no_oplock
) {
1322 DEBUG(0, ("Bad batch oplock entry %u.",
1329 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1330 if (share_mode_stale_pid(d
, i
)) {
1331 DEBUG(10, ("Found stale duplicate oplock\n"));
1334 /* Exclusive or batch - can only be one. */
1335 if (ex_or_batch
|| level2
|| no_oplock
) {
1336 DEBUG(0, ("Bad exclusive or batch oplock "
1337 "entry %u.", (unsigned)i
));
1343 if (LEVEL_II_OPLOCK_TYPE(e
->op_type
)) {
1344 if (batch
|| ex_or_batch
) {
1345 if (share_mode_stale_pid(d
, i
)) {
1346 DEBUG(10, ("Found stale LevelII "
1350 DEBUG(0, ("Bad levelII oplock entry %u.",
1357 if (e
->op_type
== NO_OPLOCK
) {
1358 if (batch
|| ex_or_batch
) {
1359 if (share_mode_stale_pid(d
, i
)) {
1360 DEBUG(10, ("Found stale NO_OPLOCK "
1364 DEBUG(0, ("Bad no oplock entry %u.",
1372 remove_stale_share_mode_entries(d
);
1374 if ((batch
|| ex_or_batch
) && (num_non_stat_opens
!= 1)) {
1375 DEBUG(1, ("got batch (%d) or ex (%d) non-exclusively (%d)\n",
1376 (int)batch
, (int)ex_or_batch
,
1377 (int)d
->num_share_modes
));
1384 static bool delay_for_oplock(files_struct
*fsp
,
1386 const struct smb2_lease
*lease
,
1387 struct share_mode_lock
*lck
,
1388 bool have_sharing_violation
,
1389 uint32_t create_disposition
,
1390 bool first_open_attempt
)
1392 struct share_mode_data
*d
= lck
->data
;
1395 bool will_overwrite
;
1397 if ((oplock_request
& INTERNAL_OPEN_ONLY
) ||
1398 is_stat_open(fsp
->access_mask
)) {
1402 switch (create_disposition
) {
1403 case FILE_SUPERSEDE
:
1404 case FILE_OVERWRITE
:
1405 case FILE_OVERWRITE_IF
:
1406 will_overwrite
= true;
1409 will_overwrite
= false;
1413 for (i
=0; i
<d
->num_share_modes
; i
++) {
1414 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1415 struct share_mode_lease
*l
= NULL
;
1416 uint32_t e_lease_type
= get_lease_type(d
, e
);
1418 uint32_t delay_mask
= 0;
1420 if (e
->op_type
== LEASE_OPLOCK
) {
1421 l
= &d
->leases
[e
->lease_idx
];
1424 if (have_sharing_violation
) {
1425 delay_mask
= SMB2_LEASE_HANDLE
;
1427 delay_mask
= SMB2_LEASE_WRITE
;
1430 break_to
= e_lease_type
& ~delay_mask
;
1432 if (will_overwrite
) {
1434 * we'll decide about SMB2_LEASE_READ later.
1436 * Maybe the break will be defered
1438 break_to
&= ~SMB2_LEASE_HANDLE
;
1441 DEBUG(10, ("entry %u: e_lease_type %u, will_overwrite: %u\n",
1442 (unsigned)i
, (unsigned)e_lease_type
,
1443 (unsigned)will_overwrite
));
1445 if (lease
!= NULL
&& l
!= NULL
) {
1448 ign
= smb2_lease_equal(fsp_client_guid(fsp
),
1457 if ((e_lease_type
& ~break_to
) == 0) {
1458 if (l
!= NULL
&& l
->breaking
) {
1464 if (share_mode_stale_pid(d
, i
)) {
1468 if (will_overwrite
) {
1470 * If we break anyway break to NONE directly.
1471 * Otherwise vfs_set_filelen() will trigger the
1474 break_to
&= ~(SMB2_LEASE_READ
|SMB2_LEASE_WRITE
);
1477 if (e
->op_type
!= LEASE_OPLOCK
) {
1479 * Oplocks only support breaking to R or NONE.
1481 break_to
&= ~(SMB2_LEASE_HANDLE
|SMB2_LEASE_WRITE
);
1484 DEBUG(10, ("breaking from %d to %d\n",
1485 (int)e_lease_type
, (int)break_to
));
1486 send_break_message(fsp
->conn
->sconn
->msg_ctx
, e
,
1488 if (e_lease_type
& delay_mask
) {
1491 if (l
!= NULL
&& l
->breaking
&& !first_open_attempt
) {
1500 static bool file_has_brlocks(files_struct
*fsp
)
1502 struct byte_range_lock
*br_lck
;
1504 br_lck
= brl_get_locks_readonly(fsp
);
1508 return (brl_num_locks(br_lck
) > 0);
1511 int find_share_mode_lease(struct share_mode_data
*d
,
1512 const struct GUID
*client_guid
,
1513 const struct smb2_lease_key
*key
)
1517 for (i
=0; i
<d
->num_leases
; i
++) {
1518 struct share_mode_lease
*l
= &d
->leases
[i
];
1520 if (smb2_lease_equal(client_guid
,
1531 struct fsp_lease
*find_fsp_lease(struct files_struct
*new_fsp
,
1532 const struct smb2_lease_key
*key
,
1533 const struct share_mode_lease
*l
)
1535 struct files_struct
*fsp
;
1538 * TODO: Measure how expensive this loop is with thousands of open
1542 for (fsp
= file_find_di_first(new_fsp
->conn
->sconn
, new_fsp
->file_id
);
1544 fsp
= file_find_di_next(fsp
)) {
1546 if (fsp
== new_fsp
) {
1549 if (fsp
->oplock_type
!= LEASE_OPLOCK
) {
1552 if (smb2_lease_key_equal(&fsp
->lease
->lease
.lease_key
, key
)) {
1553 fsp
->lease
->ref_count
+= 1;
1558 /* Not found - must be leased in another smbd. */
1559 new_fsp
->lease
= talloc_zero(new_fsp
->conn
->sconn
, struct fsp_lease
);
1560 if (new_fsp
->lease
== NULL
) {
1563 new_fsp
->lease
->ref_count
= 1;
1564 new_fsp
->lease
->sconn
= new_fsp
->conn
->sconn
;
1565 new_fsp
->lease
->lease
.lease_key
= *key
;
1566 new_fsp
->lease
->lease
.lease_state
= l
->current_state
;
1568 * We internally treat all leases as V2 and update
1569 * the epoch, but when sending breaks it matters if
1570 * the requesting lease was v1 or v2.
1572 new_fsp
->lease
->lease
.lease_version
= l
->lease_version
;
1573 new_fsp
->lease
->lease
.lease_epoch
= l
->epoch
;
1574 return new_fsp
->lease
;
1577 static NTSTATUS
grant_fsp_lease(struct files_struct
*fsp
,
1578 struct share_mode_lock
*lck
,
1579 const struct smb2_lease
*lease
,
1580 uint32_t *p_lease_idx
,
1583 struct share_mode_data
*d
= lck
->data
;
1584 const struct GUID
*client_guid
= fsp_client_guid(fsp
);
1585 struct share_mode_lease
*tmp
;
1589 idx
= find_share_mode_lease(d
, client_guid
, &lease
->lease_key
);
1592 struct share_mode_lease
*l
= &d
->leases
[idx
];
1594 uint32_t existing
, requested
;
1596 fsp
->lease
= find_fsp_lease(fsp
, &lease
->lease_key
, l
);
1597 if (fsp
->lease
== NULL
) {
1598 DEBUG(1, ("Did not find existing lease for file %s\n",
1600 return NT_STATUS_NO_MEMORY
;
1606 * Upgrade only if the requested lease is a strict upgrade.
1608 existing
= l
->current_state
;
1609 requested
= lease
->lease_state
;
1612 * Tricky: This test makes sure that "requested" is a
1613 * strict bitwise superset of "existing".
1615 do_upgrade
= ((existing
& requested
) == existing
);
1618 * Upgrade only if there's a change.
1620 do_upgrade
&= (granted
!= existing
);
1623 * Upgrade only if other leases don't prevent what was asked
1626 do_upgrade
&= (granted
== requested
);
1629 * only upgrade if we are not in breaking state
1631 do_upgrade
&= !l
->breaking
;
1633 DEBUG(10, ("existing=%"PRIu32
", requested=%"PRIu32
", "
1634 "granted=%"PRIu32
", do_upgrade=%d\n",
1635 existing
, requested
, granted
, (int)do_upgrade
));
1638 l
->current_state
= granted
;
1642 /* Ensure we're in sync with current lease state. */
1643 fsp_lease_update(lck
, fsp_client_guid(fsp
), fsp
->lease
);
1644 return NT_STATUS_OK
;
1651 tmp
= talloc_realloc(d
, d
->leases
, struct share_mode_lease
,
1657 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1661 fsp
->lease
= talloc_zero(fsp
->conn
->sconn
, struct fsp_lease
);
1662 if (fsp
->lease
== NULL
) {
1663 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1665 fsp
->lease
->ref_count
= 1;
1666 fsp
->lease
->sconn
= fsp
->conn
->sconn
;
1667 fsp
->lease
->lease
.lease_version
= lease
->lease_version
;
1668 fsp
->lease
->lease
.lease_key
= lease
->lease_key
;
1669 fsp
->lease
->lease
.lease_state
= granted
;
1670 fsp
->lease
->lease
.lease_epoch
= lease
->lease_epoch
+ 1;
1672 *p_lease_idx
= d
->num_leases
;
1674 d
->leases
[d
->num_leases
] = (struct share_mode_lease
) {
1675 .client_guid
= *client_guid
,
1676 .lease_key
= fsp
->lease
->lease
.lease_key
,
1677 .current_state
= fsp
->lease
->lease
.lease_state
,
1678 .lease_version
= fsp
->lease
->lease
.lease_version
,
1679 .epoch
= fsp
->lease
->lease
.lease_epoch
,
1682 status
= leases_db_add(client_guid
,
1685 fsp
->conn
->connectpath
,
1686 fsp
->fsp_name
->base_name
,
1687 fsp
->fsp_name
->stream_name
);
1688 if (!NT_STATUS_IS_OK(status
)) {
1689 DEBUG(10, ("%s: leases_db_add failed: %s\n", __func__
,
1690 nt_errstr(status
)));
1691 TALLOC_FREE(fsp
->lease
);
1692 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1698 return NT_STATUS_OK
;
1701 static bool is_same_lease(const files_struct
*fsp
,
1702 const struct share_mode_data
*d
,
1703 const struct share_mode_entry
*e
,
1704 const struct smb2_lease
*lease
)
1706 if (e
->op_type
!= LEASE_OPLOCK
) {
1709 if (lease
== NULL
) {
1713 return smb2_lease_equal(fsp_client_guid(fsp
),
1715 &d
->leases
[e
->lease_idx
].client_guid
,
1716 &d
->leases
[e
->lease_idx
].lease_key
);
1719 static NTSTATUS
grant_fsp_oplock_type(struct smb_request
*req
,
1720 struct files_struct
*fsp
,
1721 struct share_mode_lock
*lck
,
1723 struct smb2_lease
*lease
)
1725 struct share_mode_data
*d
= lck
->data
;
1726 bool got_handle_lease
= false;
1727 bool got_oplock
= false;
1730 uint32_t lease_idx
= UINT32_MAX
;
1734 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1735 /* No oplocks on internal open. */
1736 oplock_request
= NO_OPLOCK
;
1737 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1738 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1741 if (oplock_request
== LEASE_OPLOCK
) {
1742 if (lease
== NULL
) {
1744 * The SMB2 layer should have checked this
1746 return NT_STATUS_INTERNAL_ERROR
;
1749 granted
= lease
->lease_state
;
1751 if (lp_kernel_oplocks(SNUM(fsp
->conn
))) {
1752 DEBUG(10, ("No lease granted because kernel oplocks are enabled\n"));
1753 granted
= SMB2_LEASE_NONE
;
1755 if ((granted
& (SMB2_LEASE_READ
|SMB2_LEASE_WRITE
)) == 0) {
1756 DEBUG(10, ("No read or write lease requested\n"));
1757 granted
= SMB2_LEASE_NONE
;
1759 if (granted
== SMB2_LEASE_WRITE
) {
1760 DEBUG(10, ("pure write lease requested\n"));
1761 granted
= SMB2_LEASE_NONE
;
1763 if (granted
== (SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
)) {
1764 DEBUG(10, ("write and handle lease requested\n"));
1765 granted
= SMB2_LEASE_NONE
;
1768 granted
= map_oplock_to_lease_type(
1769 oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1772 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1773 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1775 granted
&= ~SMB2_LEASE_READ
;
1778 for (i
=0; i
<d
->num_share_modes
; i
++) {
1779 struct share_mode_entry
*e
= &d
->share_modes
[i
];
1780 uint32_t e_lease_type
;
1782 e_lease_type
= get_lease_type(d
, e
);
1784 if ((granted
& SMB2_LEASE_WRITE
) &&
1785 !is_same_lease(fsp
, d
, e
, lease
) &&
1786 !share_mode_stale_pid(d
, i
)) {
1788 * Can grant only one writer
1790 granted
&= ~SMB2_LEASE_WRITE
;
1793 if ((e_lease_type
& SMB2_LEASE_HANDLE
) && !got_handle_lease
&&
1794 !share_mode_stale_pid(d
, i
)) {
1795 got_handle_lease
= true;
1798 if ((e
->op_type
!= LEASE_OPLOCK
) && !got_oplock
&&
1799 !share_mode_stale_pid(d
, i
)) {
1804 if ((granted
& SMB2_LEASE_READ
) && !(granted
& SMB2_LEASE_WRITE
)) {
1806 (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1807 lp_level2_oplocks(SNUM(fsp
->conn
));
1809 if (!allow_level2
) {
1810 granted
= SMB2_LEASE_NONE
;
1814 if (oplock_request
== LEASE_OPLOCK
) {
1816 granted
&= ~SMB2_LEASE_HANDLE
;
1819 fsp
->oplock_type
= LEASE_OPLOCK
;
1821 status
= grant_fsp_lease(fsp
, lck
, lease
, &lease_idx
,
1823 if (!NT_STATUS_IS_OK(status
)) {
1827 *lease
= fsp
->lease
->lease
;
1828 DEBUG(10, ("lease_state=%d\n", lease
->lease_state
));
1830 if (got_handle_lease
) {
1831 granted
= SMB2_LEASE_NONE
;
1835 case SMB2_LEASE_READ
|SMB2_LEASE_WRITE
|SMB2_LEASE_HANDLE
:
1836 fsp
->oplock_type
= BATCH_OPLOCK
|EXCLUSIVE_OPLOCK
;
1838 case SMB2_LEASE_READ
|SMB2_LEASE_WRITE
:
1839 fsp
->oplock_type
= EXCLUSIVE_OPLOCK
;
1841 case SMB2_LEASE_READ
|SMB2_LEASE_HANDLE
:
1842 case SMB2_LEASE_READ
:
1843 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1846 fsp
->oplock_type
= NO_OPLOCK
;
1850 status
= set_file_oplock(fsp
);
1851 if (!NT_STATUS_IS_OK(status
)) {
1853 * Could not get the kernel oplock
1855 fsp
->oplock_type
= NO_OPLOCK
;
1859 ok
= set_share_mode(lck
, fsp
, get_current_uid(fsp
->conn
),
1864 return NT_STATUS_NO_MEMORY
;
1867 ok
= update_num_read_oplocks(fsp
, lck
);
1869 del_share_mode(lck
, fsp
);
1870 return NT_STATUS_INTERNAL_ERROR
;
1873 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1874 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1876 return NT_STATUS_OK
;
1879 static bool request_timed_out(struct timeval request_time
,
1880 struct timeval timeout
)
1882 struct timeval now
, end_time
;
1884 end_time
= timeval_sum(&request_time
, &timeout
);
1885 return (timeval_compare(&end_time
, &now
) < 0);
1888 struct defer_open_state
{
1889 struct smbXsrv_connection
*xconn
;
1893 static void defer_open_done(struct tevent_req
*req
);
1895 /****************************************************************************
1896 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1897 ****************************************************************************/
1899 static void defer_open(struct share_mode_lock
*lck
,
1900 struct timeval request_time
,
1901 struct timeval timeout
,
1902 struct smb_request
*req
,
1903 struct deferred_open_record
*state
)
1905 struct deferred_open_record
*open_rec
;
1907 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1908 "open entry for mid %llu\n",
1909 (unsigned int)request_time
.tv_sec
,
1910 (unsigned int)request_time
.tv_usec
,
1911 (unsigned long long)req
->mid
));
1913 open_rec
= talloc(NULL
, struct deferred_open_record
);
1914 if (open_rec
== NULL
) {
1916 exit_server("talloc failed");
1922 struct defer_open_state
*watch_state
;
1923 struct tevent_req
*watch_req
;
1926 watch_state
= talloc(open_rec
, struct defer_open_state
);
1927 if (watch_state
== NULL
) {
1928 exit_server("talloc failed");
1930 watch_state
->xconn
= req
->xconn
;
1931 watch_state
->mid
= req
->mid
;
1933 DEBUG(10, ("defering mid %llu\n",
1934 (unsigned long long)req
->mid
));
1936 watch_req
= dbwrap_record_watch_send(
1937 watch_state
, req
->sconn
->ev_ctx
, lck
->data
->record
,
1938 req
->sconn
->msg_ctx
);
1939 if (watch_req
== NULL
) {
1940 exit_server("Could not watch share mode record");
1942 tevent_req_set_callback(watch_req
, defer_open_done
,
1945 ret
= tevent_req_set_endtime(
1946 watch_req
, req
->sconn
->ev_ctx
,
1947 timeval_sum(&request_time
, &timeout
));
1951 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1952 state
->id
, open_rec
)) {
1954 exit_server("push_deferred_open_message_smb failed");
1958 static void defer_open_done(struct tevent_req
*req
)
1960 struct defer_open_state
*state
= tevent_req_callback_data(
1961 req
, struct defer_open_state
);
1965 status
= dbwrap_record_watch_recv(req
, talloc_tos(), NULL
);
1967 if (!NT_STATUS_IS_OK(status
)) {
1968 DEBUG(5, ("dbwrap_record_watch_recv returned %s\n",
1969 nt_errstr(status
)));
1971 * Even if it failed, retry anyway. TODO: We need a way to
1972 * tell a re-scheduled open about that error.
1976 DEBUG(10, ("scheduling mid %llu\n", (unsigned long long)state
->mid
));
1978 ret
= schedule_deferred_open_message_smb(state
->xconn
, state
->mid
);
1984 /****************************************************************************
1985 On overwrite open ensure that the attributes match.
1986 ****************************************************************************/
1988 static bool open_match_attributes(connection_struct
*conn
,
1989 uint32_t old_dos_attr
,
1990 uint32_t new_dos_attr
,
1991 mode_t existing_unx_mode
,
1992 mode_t new_unx_mode
,
1993 mode_t
*returned_unx_mode
)
1995 uint32_t noarch_old_dos_attr
, noarch_new_dos_attr
;
1997 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1998 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
2000 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
2001 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
2002 *returned_unx_mode
= new_unx_mode
;
2004 *returned_unx_mode
= (mode_t
)0;
2007 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
2008 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
2009 "returned_unx_mode = 0%o\n",
2010 (unsigned int)old_dos_attr
,
2011 (unsigned int)existing_unx_mode
,
2012 (unsigned int)new_dos_attr
,
2013 (unsigned int)*returned_unx_mode
));
2015 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
2016 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
2017 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
2018 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
2022 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
2023 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
2024 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
2031 /****************************************************************************
2032 Special FCB or DOS processing in the case of a sharing violation.
2033 Try and find a duplicated file handle.
2034 ****************************************************************************/
2036 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
2037 connection_struct
*conn
,
2038 files_struct
*fsp_to_dup_into
,
2039 const struct smb_filename
*smb_fname
,
2043 uint32_t access_mask
,
2044 uint32_t share_access
,
2045 uint32_t create_options
)
2049 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
2050 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
2052 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
2053 fsp
= file_find_di_next(fsp
)) {
2055 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
2056 "vuid = %llu, file_pid = %u, private_options = 0x%x "
2057 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
2058 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
2059 (unsigned int)fsp
->file_pid
,
2060 (unsigned int)fsp
->fh
->private_options
,
2061 (unsigned int)fsp
->access_mask
));
2063 if (fsp
!= fsp_to_dup_into
&&
2064 fsp
->fh
->fd
!= -1 &&
2065 fsp
->vuid
== vuid
&&
2066 fsp
->file_pid
== file_pid
&&
2067 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2068 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
2069 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
2070 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
2071 strequal(fsp
->fsp_name
->stream_name
,
2072 smb_fname
->stream_name
)) {
2073 DEBUG(10,("fcb_or_dos_open: file match\n"));
2079 return NT_STATUS_NOT_FOUND
;
2082 /* quite an insane set of semantics ... */
2083 if (is_executable(smb_fname
->base_name
) &&
2084 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
2085 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
2086 return NT_STATUS_INVALID_PARAMETER
;
2089 /* We need to duplicate this fsp. */
2090 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
2091 create_options
, fsp_to_dup_into
);
2094 static void schedule_defer_open(struct share_mode_lock
*lck
,
2096 struct timeval request_time
,
2097 struct smb_request
*req
)
2099 struct deferred_open_record state
;
2101 /* This is a relative time, added to the absolute
2102 request_time value to get the absolute timeout time.
2103 Note that if this is the second or greater time we enter
2104 this codepath for this particular request mid then
2105 request_time is left as the absolute time of the *first*
2106 time this request mid was processed. This is what allows
2107 the request to eventually time out. */
2109 struct timeval timeout
;
2111 /* Normally the smbd we asked should respond within
2112 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
2113 * the client did, give twice the timeout as a safety
2114 * measure here in case the other smbd is stuck
2115 * somewhere else. */
2117 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
2119 /* Nothing actually uses state.delayed_for_oplocks
2120 but it's handy to differentiate in debug messages
2121 between a 30 second delay due to oplock break, and
2122 a 1 second delay for share mode conflicts. */
2124 state
.delayed_for_oplocks
= True
;
2125 state
.async_open
= false;
2128 if (!request_timed_out(request_time
, timeout
)) {
2129 defer_open(lck
, request_time
, timeout
, req
, &state
);
2133 /****************************************************************************
2134 Reschedule an open call that went asynchronous.
2135 ****************************************************************************/
2137 static void schedule_async_open(struct timeval request_time
,
2138 struct smb_request
*req
)
2140 struct deferred_open_record state
;
2141 struct timeval timeout
;
2143 timeout
= timeval_set(20, 0);
2146 state
.delayed_for_oplocks
= false;
2147 state
.async_open
= true;
2149 if (!request_timed_out(request_time
, timeout
)) {
2150 defer_open(NULL
, request_time
, timeout
, req
, &state
);
2154 /****************************************************************************
2155 Work out what access_mask to use from what the client sent us.
2156 ****************************************************************************/
2158 static NTSTATUS
smbd_calculate_maximum_allowed_access(
2159 connection_struct
*conn
,
2160 const struct smb_filename
*smb_fname
,
2162 uint32_t *p_access_mask
)
2164 struct security_descriptor
*sd
;
2165 uint32_t access_granted
;
2168 if (!use_privs
&& (get_current_uid(conn
) == (uid_t
)0)) {
2169 *p_access_mask
|= FILE_GENERIC_ALL
;
2170 return NT_STATUS_OK
;
2173 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
2179 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
2181 * File did not exist
2183 *p_access_mask
= FILE_GENERIC_ALL
;
2184 return NT_STATUS_OK
;
2186 if (!NT_STATUS_IS_OK(status
)) {
2187 DEBUG(10,("Could not get acl on file %s: %s\n",
2188 smb_fname_str_dbg(smb_fname
),
2189 nt_errstr(status
)));
2190 return NT_STATUS_ACCESS_DENIED
;
2194 * If we can access the path to this file, by
2195 * default we have FILE_READ_ATTRIBUTES from the
2196 * containing directory. See the section:
2197 * "Algorithm to Check Access to an Existing File"
2200 * se_file_access_check()
2201 * also takes care of owner WRITE_DAC and READ_CONTROL.
2203 status
= se_file_access_check(sd
,
2204 get_current_nttok(conn
),
2206 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
2211 if (!NT_STATUS_IS_OK(status
)) {
2212 DEBUG(10, ("Access denied on file %s: "
2213 "when calculating maximum access\n",
2214 smb_fname_str_dbg(smb_fname
)));
2215 return NT_STATUS_ACCESS_DENIED
;
2217 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
2219 if (!(access_granted
& DELETE_ACCESS
)) {
2220 if (can_delete_file_in_directory(conn
, smb_fname
)) {
2221 *p_access_mask
|= DELETE_ACCESS
;
2225 return NT_STATUS_OK
;
2228 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
2229 const struct smb_filename
*smb_fname
,
2231 uint32_t access_mask
,
2232 uint32_t *access_mask_out
)
2235 uint32_t orig_access_mask
= access_mask
;
2236 uint32_t rejected_share_access
;
2239 * Convert GENERIC bits to specific bits.
2242 se_map_generic(&access_mask
, &file_generic_mapping
);
2244 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
2245 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
2247 status
= smbd_calculate_maximum_allowed_access(
2248 conn
, smb_fname
, use_privs
, &access_mask
);
2250 if (!NT_STATUS_IS_OK(status
)) {
2254 access_mask
&= conn
->share_access
;
2257 rejected_share_access
= access_mask
& ~(conn
->share_access
);
2259 if (rejected_share_access
) {
2260 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
2261 "file %s: rejected by share access mask[0x%08X] "
2262 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
2263 smb_fname_str_dbg(smb_fname
),
2265 orig_access_mask
, access_mask
,
2266 rejected_share_access
));
2267 return NT_STATUS_ACCESS_DENIED
;
2270 *access_mask_out
= access_mask
;
2271 return NT_STATUS_OK
;
2274 /****************************************************************************
2275 Remove the deferred open entry under lock.
2276 ****************************************************************************/
2278 /****************************************************************************
2279 Return true if this is a state pointer to an asynchronous create.
2280 ****************************************************************************/
2282 bool is_deferred_open_async(const struct deferred_open_record
*rec
)
2284 return rec
->async_open
;
2287 static bool clear_ads(uint32_t create_disposition
)
2291 switch (create_disposition
) {
2292 case FILE_SUPERSEDE
:
2293 case FILE_OVERWRITE_IF
:
2294 case FILE_OVERWRITE
:
2303 static int disposition_to_open_flags(uint32_t create_disposition
)
2308 * Currently we're using FILE_SUPERSEDE as the same as
2309 * FILE_OVERWRITE_IF but they really are
2310 * different. FILE_SUPERSEDE deletes an existing file
2311 * (requiring delete access) then recreates it.
2314 switch (create_disposition
) {
2315 case FILE_SUPERSEDE
:
2316 case FILE_OVERWRITE_IF
:
2318 * If file exists replace/overwrite. If file doesn't
2321 ret
= O_CREAT
|O_TRUNC
;
2326 * If file exists open. If file doesn't exist error.
2331 case FILE_OVERWRITE
:
2333 * If file exists overwrite. If file doesn't exist
2341 * If file exists error. If file doesn't exist create.
2343 ret
= O_CREAT
|O_EXCL
;
2348 * If file exists open. If file doesn't exist create.
2356 static int calculate_open_access_flags(uint32_t access_mask
,
2358 uint32_t private_flags
)
2360 bool need_write
, need_read
;
2363 * Note that we ignore the append flag as append does not
2364 * mean the same thing under DOS and Unix.
2367 need_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
));
2372 /* DENY_DOS opens are always underlying read-write on the
2373 file handle, no matter what the requested access mask
2377 ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
2378 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|
2379 FILE_READ_EA
|FILE_EXECUTE
));
2387 /****************************************************************************
2388 Open a file with a share mode. Passed in an already created files_struct *.
2389 ****************************************************************************/
2391 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
2392 struct smb_request
*req
,
2393 uint32_t access_mask
, /* access bits (FILE_READ_DATA etc.) */
2394 uint32_t share_access
, /* share constants (FILE_SHARE_READ etc) */
2395 uint32_t create_disposition
, /* FILE_OPEN_IF etc. */
2396 uint32_t create_options
, /* options such as delete on close. */
2397 uint32_t new_dos_attributes
, /* attributes used for new file. */
2398 int oplock_request
, /* internal Samba oplock codes. */
2399 struct smb2_lease
*lease
,
2400 /* Information (FILE_EXISTS etc.) */
2401 uint32_t private_flags
, /* Samba specific flags. */
2405 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
2408 bool file_existed
= VALID_STAT(smb_fname
->st
);
2409 bool def_acl
= False
;
2410 bool posix_open
= False
;
2411 bool new_file_created
= False
;
2412 bool first_open_attempt
= true;
2413 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
2414 mode_t new_unx_mode
= (mode_t
)0;
2415 mode_t unx_mode
= (mode_t
)0;
2417 uint32_t existing_dos_attributes
= 0;
2418 struct timeval request_time
= timeval_zero();
2419 struct share_mode_lock
*lck
= NULL
;
2420 uint32_t open_access_mask
= access_mask
;
2423 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
2424 struct timespec old_write_time
;
2427 if (conn
->printer
) {
2429 * Printers are handled completely differently.
2430 * Most of the passed parameters are ignored.
2434 *pinfo
= FILE_WAS_CREATED
;
2437 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
2438 smb_fname_str_dbg(smb_fname
)));
2441 DEBUG(0,("open_file_ntcreate: printer open without "
2442 "an SMB request!\n"));
2443 return NT_STATUS_INTERNAL_ERROR
;
2446 return print_spool_open(fsp
, smb_fname
->base_name
,
2450 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
2452 return NT_STATUS_NO_MEMORY
;
2455 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2457 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2458 new_dos_attributes
= 0;
2460 /* Windows allows a new file to be created and
2461 silently removes a FILE_ATTRIBUTE_DIRECTORY
2462 sent by the client. Do the same. */
2464 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
2466 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2468 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2469 smb_fname
, parent_dir
);
2472 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2473 "access_mask=0x%x share_access=0x%x "
2474 "create_disposition = 0x%x create_options=0x%x "
2475 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2476 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
2477 access_mask
, share_access
, create_disposition
,
2478 create_options
, (unsigned int)unx_mode
, oplock_request
,
2479 (unsigned int)private_flags
));
2482 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2483 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) != 0));
2485 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2486 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) == 0));
2490 * Only non-internal opens can be deferred at all
2494 struct deferred_open_record
*open_rec
;
2495 if (get_deferred_open_message_state(req
,
2498 /* Remember the absolute time of the original
2499 request with this mid. We'll use it later to
2500 see if this has timed out. */
2502 /* If it was an async create retry, the file
2505 if (is_deferred_open_async(open_rec
)) {
2506 SET_STAT_INVALID(smb_fname
->st
);
2507 file_existed
= false;
2510 /* Ensure we don't reprocess this message. */
2511 remove_deferred_open_message_smb(req
->xconn
, req
->mid
);
2513 first_open_attempt
= false;
2518 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
2520 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
2524 /* ignore any oplock requests if oplocks are disabled */
2525 if (!lp_oplocks(SNUM(conn
)) ||
2526 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
2527 /* Mask off everything except the private Samba bits. */
2528 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
2531 /* this is for OS/2 long file names - say we don't support them */
2532 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
2533 /* OS/2 Workplace shell fix may be main code stream in a later
2535 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2537 if (use_nt_status()) {
2538 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2540 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
2543 switch( create_disposition
) {
2545 /* If file exists open. If file doesn't exist error. */
2546 if (!file_existed
) {
2547 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2548 "requested for file %s and file "
2550 smb_fname_str_dbg(smb_fname
)));
2552 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2556 case FILE_OVERWRITE
:
2557 /* If file exists overwrite. If file doesn't exist
2559 if (!file_existed
) {
2560 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2561 "requested for file %s and file "
2563 smb_fname_str_dbg(smb_fname
) ));
2565 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2570 /* If file exists error. If file doesn't exist
2573 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2574 "requested for file %s and file "
2575 "already exists.\n",
2576 smb_fname_str_dbg(smb_fname
)));
2577 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2582 return map_nt_error_from_unix(errno
);
2586 case FILE_SUPERSEDE
:
2587 case FILE_OVERWRITE_IF
:
2591 return NT_STATUS_INVALID_PARAMETER
;
2594 flags2
= disposition_to_open_flags(create_disposition
);
2596 /* We only care about matching attributes on file exists and
2599 if (!posix_open
&& file_existed
&&
2600 ((create_disposition
== FILE_OVERWRITE
) ||
2601 (create_disposition
== FILE_OVERWRITE_IF
))) {
2602 if (!open_match_attributes(conn
, existing_dos_attributes
,
2604 smb_fname
->st
.st_ex_mode
,
2605 unx_mode
, &new_unx_mode
)) {
2606 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2607 "for file %s (%x %x) (0%o, 0%o)\n",
2608 smb_fname_str_dbg(smb_fname
),
2609 existing_dos_attributes
,
2611 (unsigned int)smb_fname
->st
.st_ex_mode
,
2612 (unsigned int)unx_mode
));
2614 return NT_STATUS_ACCESS_DENIED
;
2618 status
= smbd_calculate_access_mask(conn
, smb_fname
,
2622 if (!NT_STATUS_IS_OK(status
)) {
2623 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2624 "on file %s returned %s\n",
2625 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
2629 open_access_mask
= access_mask
;
2631 if (flags2
& O_TRUNC
) {
2632 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
2635 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2636 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
2640 * Note that we ignore the append flag as append does not
2641 * mean the same thing under DOS and Unix.
2644 flags
= calculate_open_access_flags(access_mask
, oplock_request
,
2648 * Currently we only look at FILE_WRITE_THROUGH for create options.
2652 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
2657 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
2661 if (!posix_open
&& !CAN_WRITE(conn
)) {
2663 * We should really return a permission denied error if either
2664 * O_CREAT or O_TRUNC are set, but for compatibility with
2665 * older versions of Samba we just AND them out.
2667 flags2
&= ~(O_CREAT
|O_TRUNC
);
2670 if (first_open_attempt
&& lp_kernel_oplocks(SNUM(conn
))) {
2672 * With kernel oplocks the open breaking an oplock
2673 * blocks until the oplock holder has given up the
2674 * oplock or closed the file. We prevent this by first
2675 * trying to open the file with O_NONBLOCK (see "man
2676 * fcntl" on Linux). For the second try, triggered by
2677 * an oplock break response, we do not need this
2680 * This is true under the assumption that only Samba
2681 * requests kernel oplocks. Once someone else like
2682 * NFSv4 starts to use that API, we will have to
2683 * modify this by communicating with the NFSv4 server.
2685 flags2
|= O_NONBLOCK
;
2689 * Ensure we can't write on a read-only share or file.
2692 if (flags
!= O_RDONLY
&& file_existed
&&
2693 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
2694 DEBUG(5,("open_file_ntcreate: write access requested for "
2695 "file %s on read only %s\n",
2696 smb_fname_str_dbg(smb_fname
),
2697 !CAN_WRITE(conn
) ? "share" : "file" ));
2699 return NT_STATUS_ACCESS_DENIED
;
2702 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2703 fsp
->share_access
= share_access
;
2704 fsp
->fh
->private_options
= private_flags
;
2705 fsp
->access_mask
= open_access_mask
; /* We change this to the
2706 * requested access_mask after
2707 * the open is done. */
2708 fsp
->posix_open
= posix_open
;
2710 if (timeval_is_zero(&request_time
)) {
2711 request_time
= fsp
->open_time
;
2715 * Ensure we pay attention to default ACLs on directories if required.
2718 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2719 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2720 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2723 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2724 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2725 (unsigned int)flags
, (unsigned int)flags2
,
2726 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2727 (unsigned int)open_access_mask
));
2729 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2730 flags
|flags2
, unx_mode
, access_mask
,
2731 open_access_mask
, &new_file_created
);
2733 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_NETWORK_BUSY
)) {
2734 struct deferred_open_record state
;
2737 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2739 if (file_existed
&& S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
)) {
2740 DEBUG(10, ("FIFO busy\n"));
2741 return NT_STATUS_NETWORK_BUSY
;
2744 DEBUG(10, ("Internal open busy\n"));
2745 return NT_STATUS_NETWORK_BUSY
;
2749 * From here on we assume this is an oplock break triggered
2752 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
2754 state
.delayed_for_oplocks
= false;
2755 state
.async_open
= false;
2756 state
.id
= fsp
->file_id
;
2757 defer_open(NULL
, request_time
, timeval_set(0, 0),
2759 DEBUG(10, ("No share mode lock found after "
2760 "EWOULDBLOCK, retrying sync\n"));
2761 return NT_STATUS_SHARING_VIOLATION
;
2764 if (!validate_oplock_types(lck
)) {
2765 smb_panic("validate_oplock_types failed");
2768 if (delay_for_oplock(fsp
, 0, lease
, lck
, false,
2769 create_disposition
, first_open_attempt
)) {
2770 schedule_defer_open(lck
, fsp
->file_id
, request_time
,
2773 DEBUG(10, ("Sent oplock break request to kernel "
2774 "oplock holder\n"));
2775 return NT_STATUS_SHARING_VIOLATION
;
2779 * No oplock from Samba around. Immediately retry with
2782 state
.delayed_for_oplocks
= false;
2783 state
.async_open
= false;
2784 state
.id
= fsp
->file_id
;
2785 defer_open(lck
, request_time
, timeval_set(0, 0), req
, &state
);
2787 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2788 "Retrying sync\n"));
2789 return NT_STATUS_SHARING_VIOLATION
;
2792 if (!NT_STATUS_IS_OK(fsp_open
)) {
2793 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
2794 schedule_async_open(request_time
, req
);
2799 if (new_file_created
) {
2801 * As we atomically create using O_CREAT|O_EXCL,
2802 * then if new_file_created is true, then
2803 * file_existed *MUST* have been false (even
2804 * if the file was previously detected as being
2807 file_existed
= false;
2810 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
2812 * The file did exist, but some other (local or NFS)
2813 * process either renamed/unlinked and re-created the
2814 * file with different dev/ino after we walked the path,
2815 * but before we did the open. We could retry the
2816 * open but it's a rare enough case it's easier to
2817 * just fail the open to prevent creating any problems
2818 * in the open file db having the wrong dev/ino key.
2821 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2822 "Old (dev=0x%llu, ino =0x%llu). "
2823 "New (dev=0x%llu, ino=0x%llu). Failing open "
2824 " with NT_STATUS_ACCESS_DENIED.\n",
2825 smb_fname_str_dbg(smb_fname
),
2826 (unsigned long long)saved_stat
.st_ex_dev
,
2827 (unsigned long long)saved_stat
.st_ex_ino
,
2828 (unsigned long long)smb_fname
->st
.st_ex_dev
,
2829 (unsigned long long)smb_fname
->st
.st_ex_ino
));
2830 return NT_STATUS_ACCESS_DENIED
;
2833 old_write_time
= smb_fname
->st
.st_ex_mtime
;
2836 * Deal with the race condition where two smbd's detect the
2837 * file doesn't exist and do the create at the same time. One
2838 * of them will win and set a share mode, the other (ie. this
2839 * one) should check if the requested share mode for this
2840 * create is allowed.
2844 * Now the file exists and fsp is successfully opened,
2845 * fsp->dev and fsp->inode are valid and should replace the
2846 * dev=0,inode=0 from a non existent file. Spotted by
2847 * Nadav Danieli <nadavd@exanet.com>. JRA.
2852 lck
= get_share_mode_lock(talloc_tos(), id
,
2854 smb_fname
, &old_write_time
);
2857 DEBUG(0, ("open_file_ntcreate: Could not get share "
2858 "mode lock for %s\n",
2859 smb_fname_str_dbg(smb_fname
)));
2861 return NT_STATUS_SHARING_VIOLATION
;
2864 /* Get the types we need to examine. */
2865 if (!validate_oplock_types(lck
)) {
2866 smb_panic("validate_oplock_types failed");
2869 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
2872 return NT_STATUS_DELETE_PENDING
;
2875 status
= open_mode_check(conn
, lck
,
2876 access_mask
, share_access
);
2878 if (NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
) ||
2879 (lck
->data
->num_share_modes
> 0)) {
2881 * This comes from ancient times out of open_mode_check. I
2882 * have no clue whether this is still necessary. I can't think
2883 * of a case where this would actually matter further down in
2884 * this function. I leave it here for further investigation
2887 file_existed
= true;
2890 if ((req
!= NULL
) &&
2892 fsp
, oplock_request
, lease
, lck
,
2893 NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
),
2894 create_disposition
, first_open_attempt
)) {
2895 schedule_defer_open(lck
, fsp
->file_id
, request_time
, req
);
2898 return NT_STATUS_SHARING_VIOLATION
;
2901 if (!NT_STATUS_IS_OK(status
)) {
2902 uint32_t can_access_mask
;
2903 bool can_access
= True
;
2905 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2907 /* Check if this can be done with the deny_dos and fcb
2910 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2911 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2913 DEBUG(0, ("DOS open without an SMB "
2917 return NT_STATUS_INTERNAL_ERROR
;
2920 /* Use the client requested access mask here,
2921 * not the one we open with. */
2922 status
= fcb_or_dos_open(req
,
2933 if (NT_STATUS_IS_OK(status
)) {
2936 *pinfo
= FILE_WAS_OPENED
;
2938 return NT_STATUS_OK
;
2943 * This next line is a subtlety we need for
2944 * MS-Access. If a file open will fail due to share
2945 * permissions and also for security (access) reasons,
2946 * we need to return the access failed error, not the
2947 * share error. We can't open the file due to kernel
2948 * oplock deadlock (it's possible we failed above on
2949 * the open_mode_check()) so use a userspace check.
2952 if (flags
& O_RDWR
) {
2953 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2954 } else if (flags
& O_WRONLY
) {
2955 can_access_mask
= FILE_WRITE_DATA
;
2957 can_access_mask
= FILE_READ_DATA
;
2960 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2961 !CAN_WRITE(conn
)) ||
2962 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
2965 can_access_mask
))) {
2970 * If we're returning a share violation, ensure we
2971 * cope with the braindead 1 second delay (SMB1 only).
2974 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2975 !conn
->sconn
->using_smb2
&&
2976 lp_defer_sharing_violations()) {
2977 struct timeval timeout
;
2978 struct deferred_open_record state
;
2981 /* this is a hack to speed up torture tests
2983 timeout_usecs
= lp_parm_int(SNUM(conn
),
2984 "smbd","sharedelay",
2985 SHARING_VIOLATION_USEC_WAIT
);
2987 /* This is a relative time, added to the absolute
2988 request_time value to get the absolute timeout time.
2989 Note that if this is the second or greater time we enter
2990 this codepath for this particular request mid then
2991 request_time is left as the absolute time of the *first*
2992 time this request mid was processed. This is what allows
2993 the request to eventually time out. */
2995 timeout
= timeval_set(0, timeout_usecs
);
2997 /* Nothing actually uses state.delayed_for_oplocks
2998 but it's handy to differentiate in debug messages
2999 between a 30 second delay due to oplock break, and
3000 a 1 second delay for share mode conflicts. */
3002 state
.delayed_for_oplocks
= False
;
3003 state
.async_open
= false;
3007 && !request_timed_out(request_time
,
3009 defer_open(lck
, request_time
, timeout
,
3018 * We have detected a sharing violation here
3019 * so return the correct error code
3021 status
= NT_STATUS_SHARING_VIOLATION
;
3023 status
= NT_STATUS_ACCESS_DENIED
;
3028 /* Should we atomically (to the client at least) truncate ? */
3029 if ((!new_file_created
) &&
3030 (flags2
& O_TRUNC
) &&
3031 (!S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
))) {
3034 ret
= vfs_set_filelen(fsp
, 0);
3036 status
= map_nt_error_from_unix(errno
);
3044 * We have the share entry *locked*.....
3047 /* Delete streams if create_disposition requires it */
3048 if (!new_file_created
&& clear_ads(create_disposition
) &&
3049 !is_ntfs_stream_smb_fname(smb_fname
)) {
3050 status
= delete_all_streams(conn
, smb_fname
->base_name
);
3051 if (!NT_STATUS_IS_OK(status
)) {
3058 /* note that we ignore failure for the following. It is
3059 basically a hack for NFS, and NFS will never set one of
3060 these only read them. Nobody but Samba can ever set a deny
3061 mode and we have already checked our more authoritative
3062 locking database for permission to set this deny mode. If
3063 the kernel refuses the operations then the kernel is wrong.
3064 note that GPFS supports it as well - jmcd */
3066 if (fsp
->fh
->fd
!= -1 && lp_kernel_share_modes(SNUM(conn
))) {
3069 * Beware: streams implementing VFS modules may
3070 * implement streams in a way that fsp will have the
3071 * basefile open in the fsp fd, so lacking a distinct
3072 * fd for the stream kernel_flock will apply on the
3073 * basefile which is wrong. The actual check is
3074 * deffered to the VFS module implementing the
3075 * kernel_flock call.
3077 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
3078 if(ret_flock
== -1 ){
3083 return NT_STATUS_SHARING_VIOLATION
;
3088 * At this point onwards, we can guarantee that the share entry
3089 * is locked, whether we created the file or not, and that the
3090 * deny mode is compatible with all current opens.
3094 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3095 * but we don't have to store this - just ignore it on access check.
3097 if (conn
->sconn
->using_smb2
) {
3099 * SMB2 doesn't return it (according to Microsoft tests).
3100 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
3101 * File created with access = 0x7 (Read, Write, Delete)
3102 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
3104 fsp
->access_mask
= access_mask
;
3106 /* But SMB1 does. */
3107 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3112 * stat opens on existing files don't get oplocks.
3113 * They can get leases.
3115 * Note that we check for stat open on the *open_access_mask*,
3116 * i.e. the access mask we actually used to do the open,
3117 * not the one the client asked for (which is in
3118 * fsp->access_mask). This is due to the fact that
3119 * FILE_OVERWRITE and FILE_OVERWRITE_IF add in O_TRUNC,
3120 * which adds FILE_WRITE_DATA to open_access_mask.
3122 if (is_stat_open(open_access_mask
) && lease
== NULL
) {
3123 oplock_request
= NO_OPLOCK
;
3127 if (new_file_created
) {
3128 info
= FILE_WAS_CREATED
;
3130 if (flags2
& O_TRUNC
) {
3131 info
= FILE_WAS_OVERWRITTEN
;
3133 info
= FILE_WAS_OPENED
;
3142 * Setup the oplock info in both the shared memory and
3145 status
= grant_fsp_oplock_type(req
, fsp
, lck
, oplock_request
, lease
);
3146 if (!NT_STATUS_IS_OK(status
)) {
3152 /* Handle strange delete on close create semantics. */
3153 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3155 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
3157 if (!NT_STATUS_IS_OK(status
)) {
3158 /* Remember to delete the mode we just added. */
3159 del_share_mode(lck
, fsp
);
3164 /* Note that here we set the *inital* delete on close flag,
3165 not the regular one. The magic gets handled in close. */
3166 fsp
->initial_delete_on_close
= True
;
3169 if (info
!= FILE_WAS_OPENED
) {
3170 /* Files should be initially set as archive */
3171 if (lp_map_archive(SNUM(conn
)) ||
3172 lp_store_dos_attributes(SNUM(conn
))) {
3174 if (file_set_dosmode(conn
, smb_fname
,
3175 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
3176 parent_dir
, true) == 0) {
3177 unx_mode
= smb_fname
->st
.st_ex_mode
;
3183 /* Determine sparse flag. */
3185 /* POSIX opens are sparse by default. */
3186 fsp
->is_sparse
= true;
3188 fsp
->is_sparse
= (file_existed
&&
3189 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
3193 * Take care of inherited ACLs on created files - if default ACL not
3197 if (!posix_open
&& new_file_created
&& !def_acl
) {
3199 int saved_errno
= errno
; /* We might get ENOSYS in the next
3202 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
3204 errno
= saved_errno
; /* Ignore ENOSYS */
3207 } else if (new_unx_mode
) {
3211 /* Attributes need changing. File already existed. */
3214 int saved_errno
= errno
; /* We might get ENOSYS in the
3216 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
3218 if (ret
== -1 && errno
== ENOSYS
) {
3219 errno
= saved_errno
; /* Ignore ENOSYS */
3221 DEBUG(5, ("open_file_ntcreate: reset "
3222 "attributes of file %s to 0%o\n",
3223 smb_fname_str_dbg(smb_fname
),
3224 (unsigned int)new_unx_mode
));
3225 ret
= 0; /* Don't do the fchmod below. */
3230 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
3231 DEBUG(5, ("open_file_ntcreate: failed to reset "
3232 "attributes of file %s to 0%o\n",
3233 smb_fname_str_dbg(smb_fname
),
3234 (unsigned int)new_unx_mode
));
3239 * Deal with other opens having a modified write time.
3241 struct timespec write_time
= get_share_mode_write_time(lck
);
3243 if (!null_timespec(write_time
)) {
3244 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
3250 return NT_STATUS_OK
;
3253 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
3254 struct smb_filename
*smb_dname
,
3255 uint32_t file_attributes
)
3258 char *parent_dir
= NULL
;
3260 bool posix_open
= false;
3261 bool need_re_stat
= false;
3262 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
3264 if (!CAN_WRITE(conn
) || (access_mask
& ~(conn
->share_access
))) {
3265 DEBUG(5,("mkdir_internal: failing share access "
3266 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
3267 return NT_STATUS_ACCESS_DENIED
;
3270 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
3272 return NT_STATUS_NO_MEMORY
;
3275 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
3277 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
3279 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
3282 status
= check_parent_access(conn
,
3285 if(!NT_STATUS_IS_OK(status
)) {
3286 DEBUG(5,("mkdir_internal: check_parent_access "
3287 "on directory %s for path %s returned %s\n",
3289 smb_dname
->base_name
,
3290 nt_errstr(status
) ));
3294 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
3295 return map_nt_error_from_unix(errno
);
3298 /* Ensure we're checking for a symlink here.... */
3299 /* We don't want to get caught by a symlink racer. */
3301 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
3302 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3303 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3304 return map_nt_error_from_unix(errno
);
3307 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3308 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
3309 smb_fname_str_dbg(smb_dname
)));
3310 return NT_STATUS_NOT_A_DIRECTORY
;
3313 if (lp_store_dos_attributes(SNUM(conn
))) {
3315 file_set_dosmode(conn
, smb_dname
,
3316 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
3321 if (lp_inherit_permissions(SNUM(conn
))) {
3322 inherit_access_posix_acl(conn
, parent_dir
,
3323 smb_dname
->base_name
, mode
);
3324 need_re_stat
= true;
3329 * Check if high bits should have been set,
3330 * then (if bits are missing): add them.
3331 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
3334 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
3335 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
3336 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
3337 (smb_dname
->st
.st_ex_mode
|
3338 (mode
& ~smb_dname
->st
.st_ex_mode
)));
3339 need_re_stat
= true;
3343 /* Change the owner if required. */
3344 if (lp_inherit_owner(SNUM(conn
))) {
3345 change_dir_owner_to_parent(conn
, parent_dir
,
3346 smb_dname
->base_name
,
3348 need_re_stat
= true;
3352 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
3353 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
3354 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
3355 return map_nt_error_from_unix(errno
);
3359 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
3360 smb_dname
->base_name
);
3362 return NT_STATUS_OK
;
3365 /****************************************************************************
3366 Open a directory from an NT SMB call.
3367 ****************************************************************************/
3369 static NTSTATUS
open_directory(connection_struct
*conn
,
3370 struct smb_request
*req
,
3371 struct smb_filename
*smb_dname
,
3372 uint32_t access_mask
,
3373 uint32_t share_access
,
3374 uint32_t create_disposition
,
3375 uint32_t create_options
,
3376 uint32_t file_attributes
,
3378 files_struct
**result
)
3380 files_struct
*fsp
= NULL
;
3381 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
3382 struct share_mode_lock
*lck
= NULL
;
3384 struct timespec mtimespec
;
3388 if (is_ntfs_stream_smb_fname(smb_dname
)) {
3389 DEBUG(2, ("open_directory: %s is a stream name!\n",
3390 smb_fname_str_dbg(smb_dname
)));
3391 return NT_STATUS_NOT_A_DIRECTORY
;
3394 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
3395 /* Ensure we have a directory attribute. */
3396 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
3399 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
3400 "share_access = 0x%x create_options = 0x%x, "
3401 "create_disposition = 0x%x, file_attributes = 0x%x\n",
3402 smb_fname_str_dbg(smb_dname
),
3403 (unsigned int)access_mask
,
3404 (unsigned int)share_access
,
3405 (unsigned int)create_options
,
3406 (unsigned int)create_disposition
,
3407 (unsigned int)file_attributes
));
3409 status
= smbd_calculate_access_mask(conn
, smb_dname
, false,
3410 access_mask
, &access_mask
);
3411 if (!NT_STATUS_IS_OK(status
)) {
3412 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
3413 "on file %s returned %s\n",
3414 smb_fname_str_dbg(smb_dname
),
3415 nt_errstr(status
)));
3419 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3420 !security_token_has_privilege(get_current_nttok(conn
),
3421 SEC_PRIV_SECURITY
)) {
3422 DEBUG(10, ("open_directory: open on %s "
3423 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3424 smb_fname_str_dbg(smb_dname
)));
3425 return NT_STATUS_PRIVILEGE_NOT_HELD
;
3428 switch( create_disposition
) {
3432 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3435 info
= FILE_WAS_OPENED
;
3440 /* If directory exists error. If directory doesn't
3444 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
3445 DEBUG(2, ("open_directory: unable to create "
3446 "%s. Error was %s\n",
3447 smb_fname_str_dbg(smb_dname
),
3448 nt_errstr(status
)));
3452 status
= mkdir_internal(conn
, smb_dname
,
3455 if (!NT_STATUS_IS_OK(status
)) {
3456 DEBUG(2, ("open_directory: unable to create "
3457 "%s. Error was %s\n",
3458 smb_fname_str_dbg(smb_dname
),
3459 nt_errstr(status
)));
3463 info
= FILE_WAS_CREATED
;
3468 * If directory exists open. If directory doesn't
3473 status
= NT_STATUS_OK
;
3474 info
= FILE_WAS_OPENED
;
3476 status
= mkdir_internal(conn
, smb_dname
,
3479 if (NT_STATUS_IS_OK(status
)) {
3480 info
= FILE_WAS_CREATED
;
3482 /* Cope with create race. */
3483 if (!NT_STATUS_EQUAL(status
,
3484 NT_STATUS_OBJECT_NAME_COLLISION
)) {
3485 DEBUG(2, ("open_directory: unable to create "
3486 "%s. Error was %s\n",
3487 smb_fname_str_dbg(smb_dname
),
3488 nt_errstr(status
)));
3491 info
= FILE_WAS_OPENED
;
3497 case FILE_SUPERSEDE
:
3498 case FILE_OVERWRITE
:
3499 case FILE_OVERWRITE_IF
:
3501 DEBUG(5,("open_directory: invalid create_disposition "
3502 "0x%x for directory %s\n",
3503 (unsigned int)create_disposition
,
3504 smb_fname_str_dbg(smb_dname
)));
3505 return NT_STATUS_INVALID_PARAMETER
;
3508 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3509 DEBUG(5,("open_directory: %s is not a directory !\n",
3510 smb_fname_str_dbg(smb_dname
)));
3511 return NT_STATUS_NOT_A_DIRECTORY
;
3514 if (info
== FILE_WAS_OPENED
) {
3515 status
= smbd_check_access_rights(conn
,
3519 if (!NT_STATUS_IS_OK(status
)) {
3520 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3521 "file %s failed with %s\n",
3522 smb_fname_str_dbg(smb_dname
),
3523 nt_errstr(status
)));
3528 status
= file_new(req
, conn
, &fsp
);
3529 if(!NT_STATUS_IS_OK(status
)) {
3534 * Setup the files_struct for it.
3537 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
3538 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
3539 fsp
->file_pid
= req
? req
->smbpid
: 0;
3540 fsp
->can_lock
= False
;
3541 fsp
->can_read
= False
;
3542 fsp
->can_write
= False
;
3544 fsp
->share_access
= share_access
;
3545 fsp
->fh
->private_options
= 0;
3547 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3549 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3550 fsp
->print_file
= NULL
;
3551 fsp
->modified
= False
;
3552 fsp
->oplock_type
= NO_OPLOCK
;
3553 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
3554 fsp
->is_directory
= True
;
3555 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
3556 status
= fsp_set_smb_fname(fsp
, smb_dname
);
3557 if (!NT_STATUS_IS_OK(status
)) {
3558 file_free(req
, fsp
);
3562 /* Don't store old timestamps for directory
3563 handles in the internal database. We don't
3564 update them in there if new objects
3565 are creaded in the directory. Currently
3566 we only update timestamps on file writes.
3569 ZERO_STRUCT(mtimespec
);
3571 if (access_mask
& (FILE_LIST_DIRECTORY
|
3573 FILE_ADD_SUBDIRECTORY
|
3576 FILE_DELETE_CHILD
)) {
3578 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
3580 /* POSIX allows us to open a directory with O_RDONLY. */
3581 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
3583 if (!NT_STATUS_IS_OK(status
)) {
3584 DEBUG(5, ("open_directory: Could not open fd for "
3586 smb_fname_str_dbg(smb_dname
),
3587 nt_errstr(status
)));
3588 file_free(req
, fsp
);
3593 DEBUG(10, ("Not opening Directory %s\n",
3594 smb_fname_str_dbg(smb_dname
)));
3597 status
= vfs_stat_fsp(fsp
);
3598 if (!NT_STATUS_IS_OK(status
)) {
3600 file_free(req
, fsp
);
3604 /* Ensure there was no race condition. */
3605 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
3606 DEBUG(5,("open_directory: stat struct differs for "
3608 smb_fname_str_dbg(smb_dname
)));
3610 file_free(req
, fsp
);
3611 return NT_STATUS_ACCESS_DENIED
;
3614 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
3615 conn
->connectpath
, smb_dname
,
3619 DEBUG(0, ("open_directory: Could not get share mode lock for "
3620 "%s\n", smb_fname_str_dbg(smb_dname
)));
3622 file_free(req
, fsp
);
3623 return NT_STATUS_SHARING_VIOLATION
;
3626 if (has_delete_on_close(lck
, fsp
->name_hash
)) {
3629 file_free(req
, fsp
);
3630 return NT_STATUS_DELETE_PENDING
;
3633 status
= open_mode_check(conn
, lck
,
3634 access_mask
, share_access
);
3636 if (!NT_STATUS_IS_OK(status
)) {
3639 file_free(req
, fsp
);
3643 ok
= set_share_mode(lck
, fsp
, get_current_uid(conn
),
3644 req
? req
->mid
: 0, NO_OPLOCK
,
3649 file_free(req
, fsp
);
3650 return NT_STATUS_NO_MEMORY
;
3653 /* For directories the delete on close bit at open time seems
3654 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3655 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3656 status
= can_set_delete_on_close(fsp
, 0);
3657 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
3658 del_share_mode(lck
, fsp
);
3661 file_free(req
, fsp
);
3665 if (NT_STATUS_IS_OK(status
)) {
3666 /* Note that here we set the *inital* delete on close flag,
3667 not the regular one. The magic gets handled in close. */
3668 fsp
->initial_delete_on_close
= True
;
3674 * Deal with other opens having a modified write time. Is this
3675 * possible for directories?
3677 struct timespec write_time
= get_share_mode_write_time(lck
);
3679 if (!null_timespec(write_time
)) {
3680 update_stat_ex_mtime(&fsp
->fsp_name
->st
, write_time
);
3691 return NT_STATUS_OK
;
3694 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
3695 struct smb_filename
*smb_dname
)
3700 status
= SMB_VFS_CREATE_FILE(
3703 0, /* root_dir_fid */
3704 smb_dname
, /* fname */
3705 FILE_READ_ATTRIBUTES
, /* access_mask */
3706 FILE_SHARE_NONE
, /* share_access */
3707 FILE_CREATE
, /* create_disposition*/
3708 FILE_DIRECTORY_FILE
, /* create_options */
3709 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
3710 0, /* oplock_request */
3712 0, /* allocation_size */
3713 0, /* private_flags */
3718 NULL
, NULL
); /* create context */
3720 if (NT_STATUS_IS_OK(status
)) {
3721 close_file(req
, fsp
, NORMAL_CLOSE
);
3727 /****************************************************************************
3728 Receive notification that one of our open files has been renamed by another
3730 ****************************************************************************/
3732 void msg_file_was_renamed(struct messaging_context
*msg
,
3735 struct server_id server_id
,
3739 char *frm
= (char *)data
->data
;
3741 const char *sharepath
;
3742 const char *base_name
;
3743 const char *stream_name
;
3744 struct smb_filename
*smb_fname
= NULL
;
3745 size_t sp_len
, bn_len
;
3747 struct smbd_server_connection
*sconn
=
3748 talloc_get_type_abort(private_data
,
3749 struct smbd_server_connection
);
3751 if (data
->data
== NULL
3752 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3753 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3754 (int)data
->length
));
3758 /* Unpack the message. */
3759 pull_file_id_24(frm
, &id
);
3760 sharepath
= &frm
[24];
3761 sp_len
= strlen(sharepath
);
3762 base_name
= sharepath
+ sp_len
+ 1;
3763 bn_len
= strlen(base_name
);
3764 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3766 /* stream_name must always be NULL if there is no stream. */
3767 if (stream_name
[0] == '\0') {
3771 smb_fname
= synthetic_smb_fname(talloc_tos(), base_name
,
3773 if (smb_fname
== NULL
) {
3777 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3779 sharepath
, smb_fname_str_dbg(smb_fname
),
3780 file_id_string_tos(&id
)));
3782 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3783 fsp
= file_find_di_next(fsp
)) {
3784 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3786 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3787 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
3788 smb_fname_str_dbg(smb_fname
)));
3789 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3790 if (!NT_STATUS_IS_OK(status
)) {
3795 /* Now we have the complete path we can work out if this is
3796 actually within this share and adjust newname accordingly. */
3797 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3798 "not sharepath %s) "
3799 "%s from %s -> %s\n",
3800 fsp
->conn
->connectpath
,
3804 smb_fname_str_dbg(smb_fname
)));
3808 TALLOC_FREE(smb_fname
);
3813 * If a main file is opened for delete, all streams need to be checked for
3814 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3815 * If that works, delete them all by setting the delete on close and close.
3818 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3821 struct stream_struct
*stream_info
= NULL
;
3822 files_struct
**streams
= NULL
;
3824 unsigned int num_streams
= 0;
3825 TALLOC_CTX
*frame
= talloc_stackframe();
3828 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3829 &num_streams
, &stream_info
);
3831 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3832 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3833 DEBUG(10, ("no streams around\n"));
3835 return NT_STATUS_OK
;
3838 if (!NT_STATUS_IS_OK(status
)) {
3839 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3840 nt_errstr(status
)));
3844 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3847 if (num_streams
== 0) {
3849 return NT_STATUS_OK
;
3852 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3853 if (streams
== NULL
) {
3854 DEBUG(0, ("talloc failed\n"));
3855 status
= NT_STATUS_NO_MEMORY
;
3859 for (i
=0; i
<num_streams
; i
++) {
3860 struct smb_filename
*smb_fname
;
3862 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3867 smb_fname
= synthetic_smb_fname(
3868 talloc_tos(), fname
, stream_info
[i
].name
, NULL
);
3869 if (smb_fname
== NULL
) {
3870 status
= NT_STATUS_NO_MEMORY
;
3874 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3875 DEBUG(10, ("Unable to stat stream: %s\n",
3876 smb_fname_str_dbg(smb_fname
)));
3879 status
= SMB_VFS_CREATE_FILE(
3882 0, /* root_dir_fid */
3883 smb_fname
, /* fname */
3884 DELETE_ACCESS
, /* access_mask */
3885 (FILE_SHARE_READ
| /* share_access */
3886 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3887 FILE_OPEN
, /* create_disposition*/
3888 0, /* create_options */
3889 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3890 0, /* oplock_request */
3892 0, /* allocation_size */
3893 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3896 &streams
[i
], /* result */
3898 NULL
, NULL
); /* create context */
3900 if (!NT_STATUS_IS_OK(status
)) {
3901 DEBUG(10, ("Could not open stream %s: %s\n",
3902 smb_fname_str_dbg(smb_fname
),
3903 nt_errstr(status
)));
3905 TALLOC_FREE(smb_fname
);
3908 TALLOC_FREE(smb_fname
);
3912 * don't touch the variable "status" beyond this point :-)
3915 for (i
-= 1 ; i
>= 0; i
--) {
3916 if (streams
[i
] == NULL
) {
3920 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3921 fsp_str_dbg(streams
[i
])));
3922 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3930 /*********************************************************************
3931 Create a default ACL by inheriting from the parent. If no inheritance
3932 from the parent available, don't set anything. This will leave the actual
3933 permissions the new file or directory already got from the filesystem
3934 as the NT ACL when read.
3935 *********************************************************************/
3937 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
3939 TALLOC_CTX
*frame
= talloc_stackframe();
3940 char *parent_name
= NULL
;
3941 struct security_descriptor
*parent_desc
= NULL
;
3942 NTSTATUS status
= NT_STATUS_OK
;
3943 struct security_descriptor
*psd
= NULL
;
3944 const struct dom_sid
*owner_sid
= NULL
;
3945 const struct dom_sid
*group_sid
= NULL
;
3946 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
3947 struct security_token
*token
= fsp
->conn
->session_info
->security_token
;
3948 bool inherit_owner
= lp_inherit_owner(SNUM(fsp
->conn
));
3949 bool inheritable_components
= false;
3950 bool try_builtin_administrators
= false;
3951 const struct dom_sid
*BA_U_sid
= NULL
;
3952 const struct dom_sid
*BA_G_sid
= NULL
;
3953 bool try_system
= false;
3954 const struct dom_sid
*SY_U_sid
= NULL
;
3955 const struct dom_sid
*SY_G_sid
= NULL
;
3958 if (!parent_dirname(frame
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
3960 return NT_STATUS_NO_MEMORY
;
3963 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
3965 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
3968 if (!NT_STATUS_IS_OK(status
)) {
3973 inheritable_components
= sd_has_inheritable_components(parent_desc
,
3976 if (!inheritable_components
&& !inherit_owner
) {
3978 /* Nothing to inherit and not setting owner. */
3979 return NT_STATUS_OK
;
3982 /* Create an inherited descriptor from the parent. */
3984 if (DEBUGLEVEL
>= 10) {
3985 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3986 fsp_str_dbg(fsp
) ));
3987 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
3990 /* Inherit from parent descriptor if "inherit owner" set. */
3991 if (inherit_owner
) {
3992 owner_sid
= parent_desc
->owner_sid
;
3993 group_sid
= parent_desc
->group_sid
;
3996 if (owner_sid
== NULL
) {
3997 if (security_token_has_builtin_administrators(token
)) {
3998 try_builtin_administrators
= true;
3999 } else if (security_token_is_system(token
)) {
4000 try_builtin_administrators
= true;
4005 if (group_sid
== NULL
&&
4006 token
->num_sids
== PRIMARY_GROUP_SID_INDEX
)
4008 if (security_token_is_system(token
)) {
4009 try_builtin_administrators
= true;
4014 if (try_builtin_administrators
) {
4019 ok
= sids_to_unixids(&global_sid_Builtin_Administrators
, 1, &ids
);
4023 BA_U_sid
= &global_sid_Builtin_Administrators
;
4024 BA_G_sid
= &global_sid_Builtin_Administrators
;
4027 BA_U_sid
= &global_sid_Builtin_Administrators
;
4030 BA_G_sid
= &global_sid_Builtin_Administrators
;
4043 ok
= sids_to_unixids(&global_sid_System
, 1, &ids
);
4047 SY_U_sid
= &global_sid_System
;
4048 SY_G_sid
= &global_sid_System
;
4051 SY_U_sid
= &global_sid_System
;
4054 SY_G_sid
= &global_sid_System
;
4062 if (owner_sid
== NULL
) {
4063 owner_sid
= BA_U_sid
;
4066 if (owner_sid
== NULL
) {
4067 owner_sid
= SY_U_sid
;
4070 if (group_sid
== NULL
) {
4071 group_sid
= SY_G_sid
;
4074 if (try_system
&& group_sid
== NULL
) {
4075 group_sid
= BA_G_sid
;
4078 if (owner_sid
== NULL
) {
4079 owner_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
4081 if (group_sid
== NULL
) {
4082 if (token
->num_sids
== PRIMARY_GROUP_SID_INDEX
) {
4083 group_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
4085 group_sid
= &token
->sids
[PRIMARY_GROUP_SID_INDEX
];
4089 status
= se_create_child_secdesc(frame
,
4096 if (!NT_STATUS_IS_OK(status
)) {
4101 /* If inheritable_components == false,
4102 se_create_child_secdesc()
4103 creates a security desriptor with a NULL dacl
4104 entry, but with SEC_DESC_DACL_PRESENT. We need
4105 to remove that flag. */
4107 if (!inheritable_components
) {
4108 security_info_sent
&= ~SECINFO_DACL
;
4109 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
4112 if (DEBUGLEVEL
>= 10) {
4113 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
4114 fsp_str_dbg(fsp
) ));
4115 NDR_PRINT_DEBUG(security_descriptor
, psd
);
4118 if (inherit_owner
) {
4119 /* We need to be root to force this. */
4122 status
= SMB_VFS_FSET_NT_ACL(fsp
,
4125 if (inherit_owner
) {
4133 * If we already have a lease, it must match the new file id. [MS-SMB2]
4134 * 3.3.5.9.8 speaks about INVALID_PARAMETER if an already used lease key is
4135 * used for a different file name.
4138 struct lease_match_state
{
4139 /* Input parameters. */
4140 TALLOC_CTX
*mem_ctx
;
4141 const char *servicepath
;
4142 const struct smb_filename
*fname
;
4145 /* Return parameters. */
4146 uint32_t num_file_ids
;
4147 struct file_id
*ids
;
4148 NTSTATUS match_status
;
4151 /*************************************************************
4152 File doesn't exist but this lease key+guid is already in use.
4154 This is only allowable in the dynamic share case where the
4155 service path must be different.
4157 There is a small race condition here in the multi-connection
4158 case where a client sends two create calls on different connections,
4159 where the file doesn't exist and one smbd creates the leases_db
4160 entry first, but this will get fixed by the multichannel cleanup
4161 when all identical client_guids get handled by a single smbd.
4162 **************************************************************/
4164 static void lease_match_parser_new_file(
4166 const struct leases_db_file
*files
,
4167 struct lease_match_state
*state
)
4171 for (i
= 0; i
< num_files
; i
++) {
4172 const struct leases_db_file
*f
= &files
[i
];
4173 if (strequal(state
->servicepath
, f
->servicepath
)) {
4174 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4179 /* Dynamic share case. Break leases on all other files. */
4180 state
->match_status
= leases_db_copy_file_ids(state
->mem_ctx
,
4184 if (!NT_STATUS_IS_OK(state
->match_status
)) {
4188 state
->num_file_ids
= num_files
;
4189 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4193 static void lease_match_parser(
4195 const struct leases_db_file
*files
,
4198 struct lease_match_state
*state
=
4199 (struct lease_match_state
*)private_data
;
4202 if (!state
->file_existed
) {
4204 * Deal with name mismatch or
4205 * possible dynamic share case separately
4206 * to make code clearer.
4208 lease_match_parser_new_file(num_files
,
4215 state
->match_status
= NT_STATUS_OK
;
4217 for (i
= 0; i
< num_files
; i
++) {
4218 const struct leases_db_file
*f
= &files
[i
];
4220 /* Everything should be the same. */
4221 if (!file_id_equal(&state
->id
, &f
->id
)) {
4222 /* This should catch all dynamic share cases. */
4223 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4226 if (!strequal(f
->servicepath
, state
->servicepath
)) {
4227 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4230 if (!strequal(f
->base_name
, state
->fname
->base_name
)) {
4231 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4234 if (!strequal(f
->stream_name
, state
->fname
->stream_name
)) {
4235 state
->match_status
= NT_STATUS_INVALID_PARAMETER
;
4240 if (NT_STATUS_IS_OK(state
->match_status
)) {
4242 * Common case - just opening another handle on a
4243 * file on a non-dynamic share.
4248 if (NT_STATUS_EQUAL(state
->match_status
, NT_STATUS_INVALID_PARAMETER
)) {
4249 /* Mismatched path. Error back to client. */
4254 * File id mismatch. Dynamic share case NT_STATUS_OPLOCK_NOT_GRANTED.
4255 * Don't allow leases.
4258 state
->match_status
= leases_db_copy_file_ids(state
->mem_ctx
,
4262 if (!NT_STATUS_IS_OK(state
->match_status
)) {
4266 state
->num_file_ids
= num_files
;
4267 state
->match_status
= NT_STATUS_OPLOCK_NOT_GRANTED
;
4271 static NTSTATUS
lease_match(connection_struct
*conn
,
4272 struct smb_request
*req
,
4273 struct smb2_lease_key
*lease_key
,
4274 const char *servicepath
,
4275 const struct smb_filename
*fname
,
4276 uint16_t *p_version
,
4279 struct smbd_server_connection
*sconn
= req
->sconn
;
4280 TALLOC_CTX
*tos
= talloc_tos();
4281 struct lease_match_state state
= {
4283 .servicepath
= servicepath
,
4285 .match_status
= NT_STATUS_OK
4290 state
.file_existed
= VALID_STAT(fname
->st
);
4291 if (state
.file_existed
) {
4292 state
.id
= vfs_file_id_from_sbuf(conn
, &fname
->st
);
4294 memset(&state
.id
, '\0', sizeof(state
.id
));
4297 status
= leases_db_parse(&sconn
->client
->connections
->smb2
.client
.guid
,
4298 lease_key
, lease_match_parser
, &state
);
4299 if (!NT_STATUS_IS_OK(status
)) {
4301 * Not found or error means okay: We can make the lease pass
4303 return NT_STATUS_OK
;
4305 if (!NT_STATUS_EQUAL(state
.match_status
, NT_STATUS_OPLOCK_NOT_GRANTED
)) {
4307 * Anything but NT_STATUS_OPLOCK_NOT_GRANTED, let the caller
4310 return state
.match_status
;
4313 /* We have to break all existing leases. */
4314 for (i
= 0; i
< state
.num_file_ids
; i
++) {
4315 struct share_mode_lock
*lck
;
4316 struct share_mode_data
*d
;
4319 if (file_id_equal(&state
.ids
[i
], &state
.id
)) {
4320 /* Don't need to break our own file. */
4324 lck
= get_existing_share_mode_lock(talloc_tos(), state
.ids
[i
]);
4326 /* Race condition - file already closed. */
4330 for (j
=0; j
<d
->num_share_modes
; j
++) {
4331 struct share_mode_entry
*e
= &d
->share_modes
[j
];
4332 uint32_t e_lease_type
= get_lease_type(d
, e
);
4333 struct share_mode_lease
*l
= NULL
;
4335 if (share_mode_stale_pid(d
, j
)) {
4339 if (e
->op_type
== LEASE_OPLOCK
) {
4340 l
= &lck
->data
->leases
[e
->lease_idx
];
4341 if (!smb2_lease_key_equal(&l
->lease_key
,
4345 *p_epoch
= l
->epoch
;
4346 *p_version
= l
->lease_version
;
4349 if (e_lease_type
== SMB2_LEASE_NONE
) {
4353 send_break_message(conn
->sconn
->msg_ctx
, e
,
4357 * Windows 7 and 8 lease clients
4358 * are broken in that they will not
4359 * respond to lease break requests
4360 * whilst waiting for an outstanding
4361 * open request on that lease handle
4362 * on the same TCP connection, due
4363 * to holding an internal inode lock.
4365 * This means we can't reschedule
4366 * ourselves here, but must return
4371 * Send the breaks and then return
4372 * SMB2_LEASE_NONE in the lease handle
4373 * to cause them to acknowledge the
4374 * lease break. Consulatation with
4375 * Microsoft engineering confirmed
4376 * this approach is safe.
4383 * Ensure we don't grant anything more so we
4386 return NT_STATUS_OPLOCK_NOT_GRANTED
;
4390 * Wrapper around open_file_ntcreate and open_directory
4393 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
4394 struct smb_request
*req
,
4395 struct smb_filename
*smb_fname
,
4396 uint32_t access_mask
,
4397 uint32_t share_access
,
4398 uint32_t create_disposition
,
4399 uint32_t create_options
,
4400 uint32_t file_attributes
,
4401 uint32_t oplock_request
,
4402 struct smb2_lease
*lease
,
4403 uint64_t allocation_size
,
4404 uint32_t private_flags
,
4405 struct security_descriptor
*sd
,
4406 struct ea_list
*ea_list
,
4408 files_struct
**result
,
4411 int info
= FILE_WAS_OPENED
;
4412 files_struct
*base_fsp
= NULL
;
4413 files_struct
*fsp
= NULL
;
4416 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
4417 "file_attributes = 0x%x, share_access = 0x%x, "
4418 "create_disposition = 0x%x create_options = 0x%x "
4419 "oplock_request = 0x%x private_flags = 0x%x "
4420 "ea_list = 0x%p, sd = 0x%p, "
4422 (unsigned int)access_mask
,
4423 (unsigned int)file_attributes
,
4424 (unsigned int)share_access
,
4425 (unsigned int)create_disposition
,
4426 (unsigned int)create_options
,
4427 (unsigned int)oplock_request
,
4428 (unsigned int)private_flags
,
4429 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4431 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
4432 status
= NT_STATUS_NOT_SUPPORTED
;
4436 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
4437 status
= NT_STATUS_INVALID_PARAMETER
;
4442 oplock_request
|= INTERNAL_OPEN_ONLY
;
4445 if (lease
!= NULL
) {
4446 uint16_t epoch
= lease
->lease_epoch
;
4447 uint16_t version
= lease
->lease_version
;
4448 status
= lease_match(conn
,
4455 if (NT_STATUS_EQUAL(status
, NT_STATUS_OPLOCK_NOT_GRANTED
)) {
4456 /* Dynamic share file. No leases and update epoch... */
4457 lease
->lease_state
= SMB2_LEASE_NONE
;
4458 lease
->lease_epoch
= epoch
;
4459 lease
->lease_version
= version
;
4460 } else if (!NT_STATUS_IS_OK(status
)) {
4465 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
4466 && (access_mask
& DELETE_ACCESS
)
4467 && !is_ntfs_stream_smb_fname(smb_fname
)) {
4469 * We can't open a file with DELETE access if any of the
4470 * streams is open without FILE_SHARE_DELETE
4472 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
4474 if (!NT_STATUS_IS_OK(status
)) {
4479 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
4480 !security_token_has_privilege(get_current_nttok(conn
),
4481 SEC_PRIV_SECURITY
)) {
4482 DEBUG(10, ("create_file_unixpath: open on %s "
4483 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
4484 smb_fname_str_dbg(smb_fname
)));
4485 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
4489 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
4490 && is_ntfs_stream_smb_fname(smb_fname
)
4491 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
4492 uint32_t base_create_disposition
;
4493 struct smb_filename
*smb_fname_base
= NULL
;
4495 if (create_options
& FILE_DIRECTORY_FILE
) {
4496 status
= NT_STATUS_NOT_A_DIRECTORY
;
4500 switch (create_disposition
) {
4502 base_create_disposition
= FILE_OPEN
;
4505 base_create_disposition
= FILE_OPEN_IF
;
4509 /* Create an smb_filename with stream_name == NULL. */
4510 smb_fname_base
= synthetic_smb_fname(talloc_tos(),
4511 smb_fname
->base_name
,
4513 if (smb_fname_base
== NULL
) {
4514 status
= NT_STATUS_NO_MEMORY
;
4518 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
4519 DEBUG(10, ("Unable to stat stream: %s\n",
4520 smb_fname_str_dbg(smb_fname_base
)));
4523 * https://bugzilla.samba.org/show_bug.cgi?id=10229
4524 * We need to check if the requested access mask
4525 * could be used to open the underlying file (if
4526 * it existed), as we're passing in zero for the
4527 * access mask to the base filename.
4529 status
= check_base_file_access(conn
,
4533 if (!NT_STATUS_IS_OK(status
)) {
4534 DEBUG(10, ("Permission check "
4535 "for base %s failed: "
4536 "%s\n", smb_fname
->base_name
,
4537 nt_errstr(status
)));
4542 /* Open the base file. */
4543 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
4546 | FILE_SHARE_DELETE
,
4547 base_create_disposition
,
4548 0, 0, 0, NULL
, 0, 0, NULL
, NULL
,
4550 TALLOC_FREE(smb_fname_base
);
4552 if (!NT_STATUS_IS_OK(status
)) {
4553 DEBUG(10, ("create_file_unixpath for base %s failed: "
4554 "%s\n", smb_fname
->base_name
,
4555 nt_errstr(status
)));
4558 /* we don't need the low level fd */
4563 * If it's a request for a directory open, deal with it separately.
4566 if (create_options
& FILE_DIRECTORY_FILE
) {
4568 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
4569 status
= NT_STATUS_INVALID_PARAMETER
;
4573 /* Can't open a temp directory. IFS kit test. */
4574 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
4575 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
4576 status
= NT_STATUS_INVALID_PARAMETER
;
4581 * We will get a create directory here if the Win32
4582 * app specified a security descriptor in the
4583 * CreateDirectory() call.
4587 status
= open_directory(
4588 conn
, req
, smb_fname
, access_mask
, share_access
,
4589 create_disposition
, create_options
, file_attributes
,
4594 * Ordinary file case.
4597 status
= file_new(req
, conn
, &fsp
);
4598 if(!NT_STATUS_IS_OK(status
)) {
4602 status
= fsp_set_smb_fname(fsp
, smb_fname
);
4603 if (!NT_STATUS_IS_OK(status
)) {
4609 * We're opening the stream element of a
4610 * base_fsp we already opened. Set up the
4613 fsp
->base_fsp
= base_fsp
;
4616 if (allocation_size
) {
4617 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
4621 status
= open_file_ntcreate(conn
,
4634 if(!NT_STATUS_IS_OK(status
)) {
4635 file_free(req
, fsp
);
4639 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
4641 /* A stream open never opens a directory */
4644 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4649 * Fail the open if it was explicitly a non-directory
4653 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
4654 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4659 status
= open_directory(
4660 conn
, req
, smb_fname
, access_mask
,
4661 share_access
, create_disposition
,
4662 create_options
, file_attributes
,
4667 if (!NT_STATUS_IS_OK(status
)) {
4671 fsp
->base_fsp
= base_fsp
;
4673 if ((ea_list
!= NULL
) &&
4674 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
4675 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
4676 if (!NT_STATUS_IS_OK(status
)) {
4681 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
4682 status
= NT_STATUS_ACCESS_DENIED
;
4686 /* Save the requested allocation size. */
4687 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
4689 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
4690 fsp
->initial_allocation_size
= smb_roundup(
4691 fsp
->conn
, allocation_size
);
4692 if (fsp
->is_directory
) {
4693 /* Can't set allocation size on a directory. */
4694 status
= NT_STATUS_ACCESS_DENIED
;
4697 if (vfs_allocate_file_space(
4698 fsp
, fsp
->initial_allocation_size
) == -1) {
4699 status
= NT_STATUS_DISK_FULL
;
4703 fsp
->initial_allocation_size
= smb_roundup(
4704 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
4707 fsp
->initial_allocation_size
= 0;
4710 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
4711 fsp
->base_fsp
== NULL
) {
4714 * According to the MS documentation, the only time the security
4715 * descriptor is applied to the opened file is iff we *created* the
4716 * file; an existing file stays the same.
4718 * Also, it seems (from observation) that you can open the file with
4719 * any access mask but you can still write the sd. We need to override
4720 * the granted access before we call set_sd
4721 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
4724 uint32_t sec_info_sent
;
4725 uint32_t saved_access_mask
= fsp
->access_mask
;
4727 sec_info_sent
= get_sec_info(sd
);
4729 fsp
->access_mask
= FILE_GENERIC_ALL
;
4731 if (sec_info_sent
& (SECINFO_OWNER
|
4735 status
= set_sd(fsp
, sd
, sec_info_sent
);
4738 fsp
->access_mask
= saved_access_mask
;
4740 if (!NT_STATUS_IS_OK(status
)) {
4743 } else if (lp_inherit_acls(SNUM(conn
))) {
4744 /* Inherit from parent. Errors here are not fatal. */
4745 status
= inherit_new_acl(fsp
);
4746 if (!NT_STATUS_IS_OK(status
)) {
4747 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
4749 nt_errstr(status
) ));
4754 if ((conn
->fs_capabilities
& FILE_FILE_COMPRESSION
)
4755 && (create_options
& FILE_NO_COMPRESSION
)
4756 && (info
== FILE_WAS_CREATED
)) {
4757 status
= SMB_VFS_SET_COMPRESSION(conn
, fsp
, fsp
,
4758 COMPRESSION_FORMAT_NONE
);
4759 if (!NT_STATUS_IS_OK(status
)) {
4760 DEBUG(1, ("failed to disable compression: %s\n",
4761 nt_errstr(status
)));
4765 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
4768 if (pinfo
!= NULL
) {
4772 smb_fname
->st
= fsp
->fsp_name
->st
;
4774 return NT_STATUS_OK
;
4777 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
4780 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
4782 * The close_file below will close
4787 close_file(req
, fsp
, ERROR_CLOSE
);
4790 if (base_fsp
!= NULL
) {
4791 close_file(req
, base_fsp
, ERROR_CLOSE
);
4798 * Calculate the full path name given a relative fid.
4800 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
4801 struct smb_request
*req
,
4802 uint16_t root_dir_fid
,
4803 const struct smb_filename
*smb_fname
,
4804 struct smb_filename
**smb_fname_out
)
4806 files_struct
*dir_fsp
;
4807 char *parent_fname
= NULL
;
4808 char *new_base_name
= NULL
;
4811 if (root_dir_fid
== 0 || !smb_fname
) {
4812 status
= NT_STATUS_INTERNAL_ERROR
;
4816 dir_fsp
= file_fsp(req
, root_dir_fid
);
4818 if (dir_fsp
== NULL
) {
4819 status
= NT_STATUS_INVALID_HANDLE
;
4823 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
4824 status
= NT_STATUS_INVALID_HANDLE
;
4828 if (!dir_fsp
->is_directory
) {
4831 * Check to see if this is a mac fork of some kind.
4834 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
4835 is_ntfs_stream_smb_fname(smb_fname
)) {
4836 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4841 we need to handle the case when we get a
4842 relative open relative to a file and the
4843 pathname is blank - this is a reopen!
4844 (hint from demyn plantenberg)
4847 status
= NT_STATUS_INVALID_HANDLE
;
4851 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
4853 * We're at the toplevel dir, the final file name
4854 * must not contain ./, as this is filtered out
4855 * normally by srvstr_get_path and unix_convert
4856 * explicitly rejects paths containing ./.
4858 parent_fname
= talloc_strdup(talloc_tos(), "");
4859 if (parent_fname
== NULL
) {
4860 status
= NT_STATUS_NO_MEMORY
;
4864 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
4867 * Copy in the base directory name.
4870 parent_fname
= talloc_array(talloc_tos(), char,
4872 if (parent_fname
== NULL
) {
4873 status
= NT_STATUS_NO_MEMORY
;
4876 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
4880 * Ensure it ends in a '/'.
4881 * We used TALLOC_SIZE +2 to add space for the '/'.
4885 && (parent_fname
[dir_name_len
-1] != '\\')
4886 && (parent_fname
[dir_name_len
-1] != '/')) {
4887 parent_fname
[dir_name_len
] = '/';
4888 parent_fname
[dir_name_len
+1] = '\0';
4892 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
4893 smb_fname
->base_name
);
4894 if (new_base_name
== NULL
) {
4895 status
= NT_STATUS_NO_MEMORY
;
4899 status
= filename_convert(req
,
4901 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
4906 if (!NT_STATUS_IS_OK(status
)) {
4911 TALLOC_FREE(parent_fname
);
4912 TALLOC_FREE(new_base_name
);
4916 NTSTATUS
create_file_default(connection_struct
*conn
,
4917 struct smb_request
*req
,
4918 uint16_t root_dir_fid
,
4919 struct smb_filename
*smb_fname
,
4920 uint32_t access_mask
,
4921 uint32_t share_access
,
4922 uint32_t create_disposition
,
4923 uint32_t create_options
,
4924 uint32_t file_attributes
,
4925 uint32_t oplock_request
,
4926 struct smb2_lease
*lease
,
4927 uint64_t allocation_size
,
4928 uint32_t private_flags
,
4929 struct security_descriptor
*sd
,
4930 struct ea_list
*ea_list
,
4931 files_struct
**result
,
4933 const struct smb2_create_blobs
*in_context_blobs
,
4934 struct smb2_create_blobs
*out_context_blobs
)
4936 int info
= FILE_WAS_OPENED
;
4937 files_struct
*fsp
= NULL
;
4939 bool stream_name
= false;
4941 DEBUG(10,("create_file: access_mask = 0x%x "
4942 "file_attributes = 0x%x, share_access = 0x%x, "
4943 "create_disposition = 0x%x create_options = 0x%x "
4944 "oplock_request = 0x%x "
4945 "private_flags = 0x%x "
4946 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4948 (unsigned int)access_mask
,
4949 (unsigned int)file_attributes
,
4950 (unsigned int)share_access
,
4951 (unsigned int)create_disposition
,
4952 (unsigned int)create_options
,
4953 (unsigned int)oplock_request
,
4954 (unsigned int)private_flags
,
4955 (unsigned int)root_dir_fid
,
4956 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4959 * Calculate the filename from the root_dir_if if necessary.
4962 if (root_dir_fid
!= 0) {
4963 struct smb_filename
*smb_fname_out
= NULL
;
4964 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
4965 smb_fname
, &smb_fname_out
);
4966 if (!NT_STATUS_IS_OK(status
)) {
4969 smb_fname
= smb_fname_out
;
4973 * Check to see if this is a mac fork of some kind.
4976 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
4978 enum FAKE_FILE_TYPE fake_file_type
;
4980 fake_file_type
= is_fake_file(smb_fname
);
4982 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
4985 * Here we go! support for changing the disk quotas
4988 * We need to fake up to open this MAGIC QUOTA file
4989 * and return a valid FID.
4991 * w2k close this file directly after openening xp
4992 * also tries a QUERY_FILE_INFO on the file and then
4995 status
= open_fake_file(req
, conn
, req
->vuid
,
4996 fake_file_type
, smb_fname
,
4998 if (!NT_STATUS_IS_OK(status
)) {
5002 ZERO_STRUCT(smb_fname
->st
);
5006 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
5007 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
5012 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
5014 smb_fname
->stream_name
= NULL
;
5015 /* We have to handle this error here. */
5016 if (create_options
& FILE_DIRECTORY_FILE
) {
5017 status
= NT_STATUS_NOT_A_DIRECTORY
;
5020 if (lp_posix_pathnames()) {
5021 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
5023 ret
= SMB_VFS_STAT(conn
, smb_fname
);
5026 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
5027 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
5032 status
= create_file_unixpath(
5033 conn
, req
, smb_fname
, access_mask
, share_access
,
5034 create_disposition
, create_options
, file_attributes
,
5035 oplock_request
, lease
, allocation_size
, private_flags
,
5039 if (!NT_STATUS_IS_OK(status
)) {
5044 DEBUG(10, ("create_file: info=%d\n", info
));
5047 if (pinfo
!= NULL
) {
5050 return NT_STATUS_OK
;
5053 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
5056 close_file(req
, fsp
, ERROR_CLOSE
);