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"
33 extern const struct generic_mapping file_generic_mapping
;
35 struct deferred_open_record
{
36 bool delayed_for_oplocks
;
40 /****************************************************************************
41 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
42 ****************************************************************************/
44 NTSTATUS
smb1_file_se_access_check(struct connection_struct
*conn
,
45 const struct security_descriptor
*sd
,
46 const struct security_token
*token
,
47 uint32_t access_desired
,
48 uint32_t *access_granted
)
52 if (get_current_uid(conn
) == (uid_t
)0) {
53 /* I'm sorry sir, I didn't know you were root... */
54 *access_granted
= access_desired
;
55 if (access_desired
& SEC_FLAG_MAXIMUM_ALLOWED
) {
56 *access_granted
|= FILE_GENERIC_ALL
;
61 return se_access_check(sd
,
63 (access_desired
& ~FILE_READ_ATTRIBUTES
),
67 /****************************************************************************
68 If the requester wanted DELETE_ACCESS and was only rejected because
69 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
71 ****************************************************************************/
73 static bool parent_override_delete(connection_struct
*conn
,
74 struct smb_filename
*smb_fname
,
76 uint32_t rejected_mask
)
78 if ((access_mask
& DELETE_ACCESS
) &&
79 (rejected_mask
== DELETE_ACCESS
) &&
80 can_delete_file_in_directory(conn
, smb_fname
)) {
86 /****************************************************************************
87 Check if we have open rights.
88 ****************************************************************************/
90 static NTSTATUS
smbd_check_open_rights(struct connection_struct
*conn
,
91 struct smb_filename
*smb_fname
,
94 /* Check if we have rights to open. */
96 struct security_descriptor
*sd
= NULL
;
97 uint32_t rejected_share_access
;
98 uint32_t rejected_mask
= 0;
100 rejected_share_access
= access_mask
& ~(conn
->share_access
);
102 if (rejected_share_access
) {
103 DEBUG(10, ("smbd_check_open_rights: rejected share access 0x%x "
105 (unsigned int)access_mask
,
106 smb_fname_str_dbg(smb_fname
),
107 (unsigned int)rejected_share_access
));
108 return NT_STATUS_ACCESS_DENIED
;
111 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
112 DEBUG(10,("smbd_check_open_rights: not checking ACL "
113 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
114 smb_fname_str_dbg(smb_fname
),
115 (unsigned int)access_mask
));
119 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
124 if (!NT_STATUS_IS_OK(status
)) {
125 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
127 smb_fname_str_dbg(smb_fname
),
132 status
= smb1_file_se_access_check(conn
,
134 get_current_nttok(conn
),
138 DEBUG(10,("smbd_check_open_rights: file %s requesting "
139 "0x%x returning 0x%x (%s)\n",
140 smb_fname_str_dbg(smb_fname
),
141 (unsigned int)access_mask
,
142 (unsigned int)rejected_mask
,
143 nt_errstr(status
) ));
145 if (!NT_STATUS_IS_OK(status
)) {
146 if (DEBUGLEVEL
>= 10) {
147 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
148 smb_fname_str_dbg(smb_fname
) ));
149 NDR_PRINT_DEBUG(security_descriptor
, sd
);
155 if (NT_STATUS_IS_OK(status
) ||
156 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
160 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
161 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
162 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
163 (lp_map_readonly(SNUM(conn
)) ||
164 lp_map_archive(SNUM(conn
)) ||
165 lp_map_hidden(SNUM(conn
)) ||
166 lp_map_system(SNUM(conn
)))) {
167 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
169 DEBUG(10,("smbd_check_open_rights: "
171 "FILE_WRITE_ATTRIBUTES "
173 smb_fname_str_dbg(smb_fname
)));
176 if (parent_override_delete(conn
,
180 /* Were we trying to do an open
181 * for delete and didn't get DELETE
182 * access (only) ? Check if the
183 * directory allows DELETE_CHILD.
185 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
188 rejected_mask
&= ~DELETE_ACCESS
;
190 DEBUG(10,("smbd_check_open_rights: "
194 smb_fname_str_dbg(smb_fname
)));
197 if (rejected_mask
!= 0) {
198 return NT_STATUS_ACCESS_DENIED
;
204 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
205 struct smb_filename
*smb_fname
,
206 uint32_t access_mask
,
207 char **pp_parent_dir
,
208 struct security_descriptor
**pp_parent_sd
)
211 char *parent_dir
= NULL
;
212 struct security_descriptor
*parent_sd
= NULL
;
213 uint32_t access_granted
= 0;
215 if (!parent_dirname(talloc_tos(),
216 smb_fname
->base_name
,
219 return NT_STATUS_NO_MEMORY
;
222 status
= SMB_VFS_GET_NT_ACL(conn
,
227 if (!NT_STATUS_IS_OK(status
)) {
228 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
229 "%s with error %s\n",
235 status
= smb1_file_se_access_check(conn
,
237 get_current_nttok(conn
),
240 if(!NT_STATUS_IS_OK(status
)) {
241 DEBUG(5,("check_parent_access: access check "
242 "on directory %s for "
243 "path %s for mask 0x%x returned (0x%x) %s\n",
245 smb_fname
->base_name
,
248 nt_errstr(status
) ));
253 *pp_parent_dir
= parent_dir
;
256 *pp_parent_sd
= parent_sd
;
261 /****************************************************************************
262 fd support routines - attempt to do a dos_open.
263 ****************************************************************************/
265 static NTSTATUS
fd_open(struct connection_struct
*conn
,
270 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
271 NTSTATUS status
= NT_STATUS_OK
;
275 * Never follow symlinks on a POSIX client. The
276 * client should be doing this.
279 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
284 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
285 if (fsp
->fh
->fd
== -1) {
286 status
= map_nt_error_from_unix(errno
);
287 if (errno
== EMFILE
) {
288 static time_t last_warned
= 0L;
290 if (time((time_t *) NULL
) > last_warned
) {
291 DEBUG(0,("Too many open files, unable "
292 "to open more! smbd's max "
294 lp_max_open_files()));
295 last_warned
= time((time_t *) NULL
);
301 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
302 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
303 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
308 /****************************************************************************
309 Close the file associated with a fsp.
310 ****************************************************************************/
312 NTSTATUS
fd_close(files_struct
*fsp
)
319 if (fsp
->fh
->fd
== -1) {
320 return NT_STATUS_OK
; /* What we used to call a stat open. */
322 if (fsp
->fh
->ref_count
> 1) {
323 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
326 ret
= SMB_VFS_CLOSE(fsp
);
329 return map_nt_error_from_unix(errno
);
334 /****************************************************************************
335 Change the ownership of a file to that of the parent directory.
336 Do this by fd if possible.
337 ****************************************************************************/
339 void change_file_owner_to_parent(connection_struct
*conn
,
340 const char *inherit_from_dir
,
343 struct smb_filename
*smb_fname_parent
= NULL
;
347 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
348 NULL
, NULL
, &smb_fname_parent
);
349 if (!NT_STATUS_IS_OK(status
)) {
353 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
355 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
356 "directory %s. Error was %s\n",
357 smb_fname_str_dbg(smb_fname_parent
),
359 TALLOC_FREE(smb_fname_parent
);
363 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
364 /* Already this uid - no need to change. */
365 DEBUG(10,("change_file_owner_to_parent: file %s "
366 "is already owned by uid %d\n",
368 (int)fsp
->fsp_name
->st
.st_ex_uid
));
369 TALLOC_FREE(smb_fname_parent
);
374 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
377 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
378 "file %s to parent directory uid %u. Error "
379 "was %s\n", fsp_str_dbg(fsp
),
380 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
383 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
384 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
385 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
386 /* Ensure the uid entry is updated. */
387 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
390 TALLOC_FREE(smb_fname_parent
);
393 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
394 const char *inherit_from_dir
,
396 SMB_STRUCT_STAT
*psbuf
)
398 struct smb_filename
*smb_fname_parent
= NULL
;
399 struct smb_filename
*smb_fname_cwd
= NULL
;
400 char *saved_dir
= NULL
;
401 TALLOC_CTX
*ctx
= talloc_tos();
402 NTSTATUS status
= NT_STATUS_OK
;
405 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
407 if (!NT_STATUS_IS_OK(status
)) {
411 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
413 status
= map_nt_error_from_unix(errno
);
414 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
415 "directory %s. Error was %s\n",
416 smb_fname_str_dbg(smb_fname_parent
),
421 /* We've already done an lstat into psbuf, and we know it's a
422 directory. If we can cd into the directory and the dev/ino
423 are the same then we can safely chown without races as
424 we're locking the directory in place by being in it. This
425 should work on any UNIX (thanks tridge :-). JRA.
428 saved_dir
= vfs_GetWd(ctx
,conn
);
430 status
= map_nt_error_from_unix(errno
);
431 DEBUG(0,("change_dir_owner_to_parent: failed to get "
432 "current working directory. Error was %s\n",
437 /* Chdir into the new path. */
438 if (vfs_ChDir(conn
, fname
) == -1) {
439 status
= map_nt_error_from_unix(errno
);
440 DEBUG(0,("change_dir_owner_to_parent: failed to change "
441 "current working directory to %s. Error "
442 "was %s\n", fname
, strerror(errno
) ));
446 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
448 if (!NT_STATUS_IS_OK(status
)) {
452 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
454 status
= map_nt_error_from_unix(errno
);
455 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
456 "directory '.' (%s) Error was %s\n",
457 fname
, strerror(errno
)));
461 /* Ensure we're pointing at the same place. */
462 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
463 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
464 DEBUG(0,("change_dir_owner_to_parent: "
465 "device/inode on directory %s changed. "
466 "Refusing to chown !\n", fname
));
467 status
= NT_STATUS_ACCESS_DENIED
;
471 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
472 /* Already this uid - no need to change. */
473 DEBUG(10,("change_dir_owner_to_parent: directory %s "
474 "is already owned by uid %d\n",
476 (int)smb_fname_cwd
->st
.st_ex_uid
));
477 status
= NT_STATUS_OK
;
482 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
486 status
= map_nt_error_from_unix(errno
);
487 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
488 "directory %s to parent directory uid %u. "
489 "Error was %s\n", fname
,
490 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
493 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
494 "directory %s to parent directory uid %u.\n",
495 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
496 /* Ensure the uid entry is updated. */
497 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
501 vfs_ChDir(conn
,saved_dir
);
503 TALLOC_FREE(smb_fname_parent
);
504 TALLOC_FREE(smb_fname_cwd
);
508 /****************************************************************************
510 ****************************************************************************/
512 static NTSTATUS
open_file(files_struct
*fsp
,
513 connection_struct
*conn
,
514 struct smb_request
*req
,
515 const char *parent_dir
,
518 uint32 access_mask
, /* client requested access mask. */
519 uint32 open_access_mask
) /* what we're actually using in the open. */
521 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
522 NTSTATUS status
= NT_STATUS_OK
;
523 int accmode
= (flags
& O_ACCMODE
);
524 int local_flags
= flags
;
525 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
526 bool file_created
= false;
531 /* Check permissions */
534 * This code was changed after seeing a client open request
535 * containing the open mode of (DENY_WRITE/read-only) with
536 * the 'create if not exist' bit set. The previous code
537 * would fail to open the file read only on a read-only share
538 * as it was checking the flags parameter directly against O_RDONLY,
539 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
543 if (!CAN_WRITE(conn
)) {
544 /* It's a read-only share - fail if we wanted to write. */
545 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
546 DEBUG(3,("Permission denied opening %s\n",
547 smb_fname_str_dbg(smb_fname
)));
548 return NT_STATUS_ACCESS_DENIED
;
549 } else if(flags
& O_CREAT
) {
550 /* We don't want to write - but we must make sure that
551 O_CREAT doesn't create the file if we have write
552 access into the directory.
554 flags
&= ~(O_CREAT
|O_EXCL
);
555 local_flags
&= ~(O_CREAT
|O_EXCL
);
560 * This little piece of insanity is inspired by the
561 * fact that an NT client can open a file for O_RDONLY,
562 * but set the create disposition to FILE_EXISTS_TRUNCATE.
563 * If the client *can* write to the file, then it expects to
564 * truncate the file, even though it is opening for readonly.
565 * Quicken uses this stupid trick in backup file creation...
566 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
567 * for helping track this one down. It didn't bite us in 2.0.x
568 * as we always opened files read-write in that release. JRA.
571 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
572 DEBUG(10,("open_file: truncate requested on read-only open "
573 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
574 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
577 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
578 (!file_existed
&& (local_flags
& O_CREAT
)) ||
579 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
583 * We can't actually truncate here as the file may be locked.
584 * open_file_ntcreate will take care of the truncate later. JRA.
587 local_flags
&= ~O_TRUNC
;
589 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
591 * We would block on opening a FIFO with no one else on the
592 * other end. Do what we used to do and add O_NONBLOCK to the
596 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
597 local_flags
|= O_NONBLOCK
;
601 /* Don't create files with Microsoft wildcard characters. */
604 * wildcard characters are allowed in stream names
605 * only test the basefilename
607 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
609 wild
= smb_fname
->base_name
;
611 if ((local_flags
& O_CREAT
) && !file_existed
&&
613 return NT_STATUS_OBJECT_NAME_INVALID
;
616 /* Actually do the open */
617 status
= fd_open(conn
, fsp
, local_flags
, unx_mode
);
618 if (!NT_STATUS_IS_OK(status
)) {
619 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
620 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
621 nt_errstr(status
),local_flags
,flags
));
625 if ((local_flags
& O_CREAT
) && !file_existed
) {
630 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
632 /* File must exist for a stat open. */
633 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
636 status
= smbd_check_open_rights(conn
,
640 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
642 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
643 /* This is a POSIX stat open for delete
644 * or rename on a symlink that points
646 DEBUG(10,("open_file: allowing POSIX "
647 "open on bad symlink %s\n",
648 smb_fname_str_dbg(smb_fname
)));
649 status
= NT_STATUS_OK
;
652 if (!NT_STATUS_IS_OK(status
)) {
653 DEBUG(10,("open_file: "
654 "smbd_check_open_rights on file "
656 smb_fname_str_dbg(smb_fname
),
657 nt_errstr(status
) ));
665 if (fsp
->fh
->fd
== -1) {
666 ret
= SMB_VFS_STAT(conn
, smb_fname
);
668 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
669 /* If we have an fd, this stat should succeed. */
671 DEBUG(0,("Error doing fstat on open file %s "
673 smb_fname_str_dbg(smb_fname
),
678 /* For a non-io open, this stat failing means file not found. JRA */
680 status
= map_nt_error_from_unix(errno
);
686 bool need_re_stat
= false;
687 /* Do all inheritance work after we've
688 done a successful stat call and filled
689 in the stat struct in fsp->fsp_name. */
691 /* Inherit the ACL if required */
692 if (lp_inherit_perms(SNUM(conn
))) {
693 inherit_access_posix_acl(conn
, parent_dir
,
694 smb_fname
->base_name
,
699 /* Change the owner if required. */
700 if (lp_inherit_owner(SNUM(conn
))) {
701 change_file_owner_to_parent(conn
, parent_dir
,
707 if (fsp
->fh
->fd
== -1) {
708 ret
= SMB_VFS_STAT(conn
, smb_fname
);
710 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
711 /* If we have an fd, this stat should succeed. */
713 DEBUG(0,("Error doing fstat on open file %s "
715 smb_fname_str_dbg(smb_fname
),
721 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
722 FILE_NOTIFY_CHANGE_FILE_NAME
,
723 smb_fname
->base_name
);
728 * POSIX allows read-only opens of directories. We don't
729 * want to do this (we use a different code path for this)
730 * so catch a directory open and return an EISDIR. JRA.
733 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
736 return NT_STATUS_FILE_IS_A_DIRECTORY
;
739 fsp
->mode
= smb_fname
->st
.st_ex_mode
;
740 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
741 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
742 fsp
->file_pid
= req
? req
->smbpid
: 0;
743 fsp
->can_lock
= True
;
744 fsp
->can_read
= (access_mask
& (FILE_READ_DATA
)) ? True
: False
;
745 if (!CAN_WRITE(conn
)) {
746 fsp
->can_write
= False
;
748 fsp
->can_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
751 fsp
->print_file
= NULL
;
752 fsp
->modified
= False
;
753 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
754 fsp
->is_directory
= False
;
755 if (conn
->aio_write_behind_list
&&
756 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
757 conn
->case_sensitive
)) {
758 fsp
->aio_write_behind
= True
;
761 fsp
->wcp
= NULL
; /* Write cache pointer. */
763 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
764 conn
->session_info
->unix_info
->unix_name
,
765 smb_fname_str_dbg(smb_fname
),
766 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
767 conn
->num_files_open
));
773 /****************************************************************************
774 Check if we can open a file with a share mode.
775 Returns True if conflict, False if not.
776 ****************************************************************************/
778 static bool share_conflict(struct share_mode_entry
*entry
,
782 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
783 "entry->share_access = 0x%x, "
784 "entry->private_options = 0x%x\n",
785 (unsigned int)entry
->access_mask
,
786 (unsigned int)entry
->share_access
,
787 (unsigned int)entry
->private_options
));
789 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
790 (unsigned int)access_mask
, (unsigned int)share_access
));
792 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
796 DELETE_ACCESS
)) == 0) {
797 DEBUG(10,("share_conflict: No conflict due to "
798 "entry->access_mask = 0x%x\n",
799 (unsigned int)entry
->access_mask
));
803 if ((access_mask
& (FILE_WRITE_DATA
|
807 DELETE_ACCESS
)) == 0) {
808 DEBUG(10,("share_conflict: No conflict due to "
809 "access_mask = 0x%x\n",
810 (unsigned int)access_mask
));
814 #if 1 /* JRA TEST - Superdebug. */
815 #define CHECK_MASK(num, am, right, sa, share) \
816 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
817 (unsigned int)(num), (unsigned int)(am), \
818 (unsigned int)(right), (unsigned int)(am)&(right) )); \
819 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
820 (unsigned int)(num), (unsigned int)(sa), \
821 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
822 if (((am) & (right)) && !((sa) & (share))) { \
823 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
824 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
825 (unsigned int)(share) )); \
829 #define CHECK_MASK(num, am, right, sa, share) \
830 if (((am) & (right)) && !((sa) & (share))) { \
831 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
832 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
833 (unsigned int)(share) )); \
838 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
839 share_access
, FILE_SHARE_WRITE
);
840 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
841 entry
->share_access
, FILE_SHARE_WRITE
);
843 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
844 share_access
, FILE_SHARE_READ
);
845 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
846 entry
->share_access
, FILE_SHARE_READ
);
848 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
849 share_access
, FILE_SHARE_DELETE
);
850 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
851 entry
->share_access
, FILE_SHARE_DELETE
);
853 DEBUG(10,("share_conflict: No conflict.\n"));
857 #if defined(DEVELOPER)
858 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
860 struct share_mode_entry
*share_entry
)
864 if (!procid_is_me(&share_entry
->pid
)) {
868 if (is_deferred_open_entry(share_entry
) &&
869 !open_was_deferred(sconn
, share_entry
->op_mid
)) {
870 char *str
= talloc_asprintf(talloc_tos(),
871 "Got a deferred entry without a request: "
873 share_mode_str(talloc_tos(), num
, share_entry
));
877 if (!is_valid_share_mode_entry(share_entry
)) {
881 fsp
= file_find_dif(sconn
, share_entry
->id
,
882 share_entry
->share_file_id
);
884 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
885 share_mode_str(talloc_tos(), num
, share_entry
) ));
886 smb_panic("validate_my_share_entries: Cannot match a "
887 "share entry with an open file\n");
890 if (is_deferred_open_entry(share_entry
) ||
891 is_unused_share_mode_entry(share_entry
)) {
895 if ((share_entry
->op_type
== NO_OPLOCK
) &&
896 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
897 /* Someone has already written to it, but I haven't yet
902 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
911 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
912 share_mode_str(talloc_tos(), num
, share_entry
) ));
913 str
= talloc_asprintf(talloc_tos(),
914 "validate_my_share_entries: "
915 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
916 fsp
->fsp_name
->base_name
,
917 (unsigned int)fsp
->oplock_type
,
918 (unsigned int)share_entry
->op_type
);
924 bool is_stat_open(uint32 access_mask
)
926 return (access_mask
&&
927 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
928 FILE_WRITE_ATTRIBUTES
))==0) &&
929 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
930 FILE_WRITE_ATTRIBUTES
)) != 0));
933 /****************************************************************************
934 Deal with share modes
935 Invarient: Share mode must be locked on entry and exit.
936 Returns -1 on error, or number of share modes on success (may be zero).
937 ****************************************************************************/
939 static NTSTATUS
open_mode_check(connection_struct
*conn
,
940 struct share_mode_lock
*lck
,
944 uint32 create_options
,
949 if(lck
->num_share_modes
== 0) {
953 *file_existed
= True
;
955 /* A delete on close prohibits everything */
957 if (is_delete_on_close_set(lck
, name_hash
)) {
958 return NT_STATUS_DELETE_PENDING
;
961 if (is_stat_open(access_mask
)) {
962 /* Stat open that doesn't trigger oplock breaks or share mode
963 * checks... ! JRA. */
968 * Check if the share modes will give us access.
971 #if defined(DEVELOPER)
972 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
973 validate_my_share_entries(conn
->sconn
, i
,
974 &lck
->share_modes
[i
]);
978 if (!lp_share_modes(SNUM(conn
))) {
982 /* Now we check the share modes, after any oplock breaks. */
983 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
985 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
989 /* someone else has a share lock on it, check to see if we can
991 if (share_conflict(&lck
->share_modes
[i
],
992 access_mask
, share_access
)) {
993 return NT_STATUS_SHARING_VIOLATION
;
1000 static bool is_delete_request(files_struct
*fsp
) {
1001 return ((fsp
->access_mask
== DELETE_ACCESS
) &&
1002 (fsp
->oplock_type
== NO_OPLOCK
));
1006 * Send a break message to the oplock holder and delay the open for
1010 static NTSTATUS
send_break_message(files_struct
*fsp
,
1011 struct share_mode_entry
*exclusive
,
1016 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1018 DEBUG(10, ("Sending break request to PID %s\n",
1019 procid_str_static(&exclusive
->pid
)));
1020 exclusive
->op_mid
= mid
;
1022 /* Create the message. */
1023 share_mode_entry_to_message(msg
, exclusive
);
1025 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1026 don't want this set in the share mode struct pointed to by lck. */
1028 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
1029 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
1030 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
1033 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
1034 MSG_SMB_BREAK_REQUEST
,
1036 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
1037 if (!NT_STATUS_IS_OK(status
)) {
1038 DEBUG(3, ("Could not send oplock break message: %s\n",
1039 nt_errstr(status
)));
1046 * Return share_mode_entry pointers for :
1047 * 1). Batch oplock entry.
1048 * 2). Batch or exclusive oplock entry (may be identical to #1).
1049 * bool have_level2_oplock
1050 * bool have_no_oplock.
1051 * Do internal consistency checks on the share mode for a file.
1054 static void find_oplock_types(files_struct
*fsp
,
1056 struct share_mode_lock
*lck
,
1057 struct share_mode_entry
**pp_batch
,
1058 struct share_mode_entry
**pp_ex_or_batch
,
1060 bool *got_no_oplock
)
1065 *pp_ex_or_batch
= NULL
;
1066 *got_level2
= false;
1067 *got_no_oplock
= false;
1069 /* Ignore stat or internal opens, as is done in
1070 delay_for_batch_oplocks() and
1071 delay_for_exclusive_oplocks().
1073 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1077 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1078 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
1082 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
&&
1083 is_stat_open(lck
->share_modes
[i
].access_mask
)) {
1084 /* We ignore stat opens in the table - they
1085 always have NO_OPLOCK and never get or
1086 cause breaks. JRA. */
1090 if (BATCH_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1091 /* batch - can only be one. */
1092 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
1093 smb_panic("Bad batch oplock entry.");
1095 *pp_batch
= &lck
->share_modes
[i
];
1098 if (EXCLUSIVE_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1099 /* Exclusive or batch - can only be one. */
1100 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
1101 smb_panic("Bad exclusive or batch oplock entry.");
1103 *pp_ex_or_batch
= &lck
->share_modes
[i
];
1106 if (LEVEL_II_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1107 if (*pp_batch
|| *pp_ex_or_batch
) {
1108 smb_panic("Bad levelII oplock entry.");
1113 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
) {
1114 if (*pp_batch
|| *pp_ex_or_batch
) {
1115 smb_panic("Bad no oplock entry.");
1117 *got_no_oplock
= true;
1122 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1125 struct share_mode_entry
*batch_entry
)
1127 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1131 if (batch_entry
!= NULL
) {
1132 /* Found a batch oplock */
1133 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1139 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1142 struct share_mode_entry
*ex_entry
)
1144 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1148 if (ex_entry
!= NULL
) {
1149 /* Found an exclusive or batch oplock */
1150 bool delay_it
= is_delete_request(fsp
) ?
1151 BATCH_OPLOCK_TYPE(ex_entry
->op_type
) : true;
1153 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1160 static void grant_fsp_oplock_type(files_struct
*fsp
,
1161 const struct byte_range_lock
*br_lck
,
1163 bool got_level2_oplock
,
1164 bool got_a_none_oplock
)
1166 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1167 lp_level2_oplocks(SNUM(fsp
->conn
));
1169 /* Start by granting what the client asked for,
1170 but ensure no SAMBA_PRIVATE bits can be set. */
1171 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1173 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1174 /* No oplocks on internal open. */
1175 fsp
->oplock_type
= NO_OPLOCK
;
1176 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1177 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1179 } else if (br_lck
&& br_lck
->num_locks
> 0) {
1180 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1182 fsp
->oplock_type
= NO_OPLOCK
;
1185 if (is_stat_open(fsp
->access_mask
)) {
1186 /* Leave the value already set. */
1187 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1188 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1193 * Match what was requested (fsp->oplock_type) with
1194 * what was found in the existing share modes.
1197 if (got_a_none_oplock
) {
1198 fsp
->oplock_type
= NO_OPLOCK
;
1199 } else if (got_level2_oplock
) {
1200 if (fsp
->oplock_type
== NO_OPLOCK
||
1201 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1202 /* Store a level2 oplock, but don't tell the client */
1203 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1205 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1208 /* All share_mode_entries are placeholders or deferred.
1209 * Silently upgrade to fake levelII if the client didn't
1210 * ask for an oplock. */
1211 if (fsp
->oplock_type
== NO_OPLOCK
) {
1212 /* Store a level2 oplock, but don't tell the client */
1213 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1218 * Don't grant level2 to clients that don't want them
1219 * or if we've turned them off.
1221 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1222 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1225 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1226 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1229 bool request_timed_out(struct timeval request_time
,
1230 struct timeval timeout
)
1232 struct timeval now
, end_time
;
1234 end_time
= timeval_sum(&request_time
, &timeout
);
1235 return (timeval_compare(&end_time
, &now
) < 0);
1238 /****************************************************************************
1239 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1240 ****************************************************************************/
1242 static void defer_open(struct share_mode_lock
*lck
,
1243 struct timeval request_time
,
1244 struct timeval timeout
,
1245 struct smb_request
*req
,
1246 struct deferred_open_record
*state
)
1250 /* Paranoia check */
1252 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1253 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1255 if (!is_deferred_open_entry(e
)) {
1259 if (procid_is_me(&e
->pid
) && (e
->op_mid
== req
->mid
)) {
1260 DEBUG(0, ("Trying to defer an already deferred "
1261 "request: mid=%llu, exiting\n",
1262 (unsigned long long)req
->mid
));
1263 exit_server("attempt to defer a deferred request");
1267 /* End paranoia check */
1269 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1270 "open entry for mid %llu\n",
1271 (unsigned int)request_time
.tv_sec
,
1272 (unsigned int)request_time
.tv_usec
,
1273 (unsigned long long)req
->mid
));
1275 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1276 state
->id
, (char *)state
, sizeof(*state
))) {
1277 exit_server("push_deferred_open_message_smb failed");
1279 add_deferred_open(lck
, req
->mid
, request_time
,
1280 sconn_server_id(req
->sconn
), state
->id
);
1284 /****************************************************************************
1285 On overwrite open ensure that the attributes match.
1286 ****************************************************************************/
1288 bool open_match_attributes(connection_struct
*conn
,
1289 uint32 old_dos_attr
,
1290 uint32 new_dos_attr
,
1291 mode_t existing_unx_mode
,
1292 mode_t new_unx_mode
,
1293 mode_t
*returned_unx_mode
)
1295 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1297 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1298 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1300 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1301 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1302 *returned_unx_mode
= new_unx_mode
;
1304 *returned_unx_mode
= (mode_t
)0;
1307 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1308 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1309 "returned_unx_mode = 0%o\n",
1310 (unsigned int)old_dos_attr
,
1311 (unsigned int)existing_unx_mode
,
1312 (unsigned int)new_dos_attr
,
1313 (unsigned int)*returned_unx_mode
));
1315 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1316 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1317 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1318 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1322 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1323 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1324 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1331 /****************************************************************************
1332 Special FCB or DOS processing in the case of a sharing violation.
1333 Try and find a duplicated file handle.
1334 ****************************************************************************/
1336 NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1337 connection_struct
*conn
,
1338 files_struct
*fsp_to_dup_into
,
1339 const struct smb_filename
*smb_fname
,
1344 uint32 share_access
,
1345 uint32 create_options
)
1349 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1350 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1352 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1353 fsp
= file_find_di_next(fsp
)) {
1355 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1356 "vuid = %u, file_pid = %u, private_options = 0x%x "
1357 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1358 fsp
->fh
->fd
, (unsigned int)fsp
->vuid
,
1359 (unsigned int)fsp
->file_pid
,
1360 (unsigned int)fsp
->fh
->private_options
,
1361 (unsigned int)fsp
->access_mask
));
1363 if (fsp
->fh
->fd
!= -1 &&
1364 fsp
->vuid
== vuid
&&
1365 fsp
->file_pid
== file_pid
&&
1366 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1367 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1368 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1369 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1370 strequal(fsp
->fsp_name
->stream_name
,
1371 smb_fname
->stream_name
)) {
1372 DEBUG(10,("fcb_or_dos_open: file match\n"));
1378 return NT_STATUS_NOT_FOUND
;
1381 /* quite an insane set of semantics ... */
1382 if (is_executable(smb_fname
->base_name
) &&
1383 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1384 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1385 return NT_STATUS_INVALID_PARAMETER
;
1388 /* We need to duplicate this fsp. */
1389 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1390 create_options
, fsp_to_dup_into
);
1393 static void schedule_defer_open(struct share_mode_lock
*lck
,
1394 struct timeval request_time
,
1395 struct smb_request
*req
)
1397 struct deferred_open_record state
;
1399 /* This is a relative time, added to the absolute
1400 request_time value to get the absolute timeout time.
1401 Note that if this is the second or greater time we enter
1402 this codepath for this particular request mid then
1403 request_time is left as the absolute time of the *first*
1404 time this request mid was processed. This is what allows
1405 the request to eventually time out. */
1407 struct timeval timeout
;
1409 /* Normally the smbd we asked should respond within
1410 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1411 * the client did, give twice the timeout as a safety
1412 * measure here in case the other smbd is stuck
1413 * somewhere else. */
1415 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1417 /* Nothing actually uses state.delayed_for_oplocks
1418 but it's handy to differentiate in debug messages
1419 between a 30 second delay due to oplock break, and
1420 a 1 second delay for share mode conflicts. */
1422 state
.delayed_for_oplocks
= True
;
1425 if (!request_timed_out(request_time
, timeout
)) {
1426 defer_open(lck
, request_time
, timeout
, req
, &state
);
1430 /****************************************************************************
1431 Work out what access_mask to use from what the client sent us.
1432 ****************************************************************************/
1434 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1435 const struct smb_filename
*smb_fname
,
1437 uint32_t access_mask
,
1438 uint32_t *access_mask_out
)
1441 uint32_t orig_access_mask
= access_mask
;
1442 uint32_t rejected_share_access
;
1445 * Convert GENERIC bits to specific bits.
1448 se_map_generic(&access_mask
, &file_generic_mapping
);
1450 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1451 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1454 struct security_descriptor
*sd
;
1455 uint32_t access_granted
= 0;
1457 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1462 if (!NT_STATUS_IS_OK(status
)) {
1463 DEBUG(10,("smbd_calculate_access_mask: "
1464 "Could not get acl on file %s: %s\n",
1465 smb_fname_str_dbg(smb_fname
),
1466 nt_errstr(status
)));
1467 return NT_STATUS_ACCESS_DENIED
;
1470 status
= smb1_file_se_access_check(conn
,
1472 get_current_nttok(conn
),
1478 if (!NT_STATUS_IS_OK(status
)) {
1479 DEBUG(10, ("smbd_calculate_access_mask: "
1480 "Access denied on file %s: "
1481 "when calculating maximum access\n",
1482 smb_fname_str_dbg(smb_fname
)));
1483 return NT_STATUS_ACCESS_DENIED
;
1486 access_mask
= access_granted
;
1488 access_mask
= FILE_GENERIC_ALL
;
1491 access_mask
&= conn
->share_access
;
1494 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1496 if (rejected_share_access
) {
1497 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1498 "file %s: rejected by share access mask[0x%08X] "
1499 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1500 smb_fname_str_dbg(smb_fname
),
1502 orig_access_mask
, access_mask
,
1503 rejected_share_access
));
1504 return NT_STATUS_ACCESS_DENIED
;
1507 *access_mask_out
= access_mask
;
1508 return NT_STATUS_OK
;
1511 /****************************************************************************
1512 Remove the deferred open entry under lock.
1513 ****************************************************************************/
1515 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1516 struct server_id pid
)
1518 struct share_mode_lock
*lck
= get_share_mode_lock(talloc_tos(), id
,
1521 DEBUG(0, ("could not get share mode lock\n"));
1523 del_deferred_open_entry(lck
, mid
, pid
);
1528 /****************************************************************
1529 Ensure we get the brlock lock followed by the share mode lock
1530 in the correct order to prevent deadlocks if other smbd's are
1531 using the brlock database on this file simultaneously with this open
1532 (that code also gets the locks in brlock -> share mode lock order).
1533 ****************************************************************/
1535 static bool acquire_ordered_locks(TALLOC_CTX
*mem_ctx
,
1537 const struct file_id id
,
1538 const char *connectpath
,
1539 const struct smb_filename
*smb_fname
,
1540 const struct timespec
*p_old_write_time
,
1541 struct share_mode_lock
**p_lck
,
1542 struct byte_range_lock
**p_br_lck
)
1544 /* Ordering - we must get the br_lck for this
1545 file before the share mode. */
1546 if (lp_locking(fsp
->conn
->params
)) {
1547 *p_br_lck
= brl_get_locks_readonly(fsp
);
1548 if (*p_br_lck
== NULL
) {
1549 DEBUG(0, ("Could not get br_lock\n"));
1552 /* Note - we don't need to free the returned
1553 br_lck explicitly as it was allocated on talloc_tos()
1554 and so will be autofreed (and release the lock)
1555 once the frame context disappears.
1557 If it was set to fsp->brlock_rec then it was
1558 talloc_move'd to hang off the fsp pointer and
1559 in this case is guarenteed to not be holding the
1560 lock on the brlock database. */
1563 *p_lck
= get_share_mode_lock(mem_ctx
,
1569 if (*p_lck
== NULL
) {
1570 DEBUG(0, ("Could not get share mode lock\n"));
1571 TALLOC_FREE(*p_br_lck
);
1577 /****************************************************************************
1578 Open a file with a share mode. Passed in an already created files_struct *.
1579 ****************************************************************************/
1581 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1582 struct smb_request
*req
,
1583 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1584 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1585 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1586 uint32 create_options
, /* options such as delete on close. */
1587 uint32 new_dos_attributes
, /* attributes used for new file. */
1588 int oplock_request
, /* internal Samba oplock codes. */
1589 /* Information (FILE_EXISTS etc.) */
1590 uint32_t private_flags
, /* Samba specific flags. */
1594 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1597 bool file_existed
= VALID_STAT(smb_fname
->st
);
1598 bool def_acl
= False
;
1599 bool posix_open
= False
;
1600 bool new_file_created
= False
;
1601 bool clear_ads
= false;
1603 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1604 mode_t new_unx_mode
= (mode_t
)0;
1605 mode_t unx_mode
= (mode_t
)0;
1607 uint32 existing_dos_attributes
= 0;
1608 struct timeval request_time
= timeval_zero();
1609 struct share_mode_lock
*lck
= NULL
;
1610 uint32 open_access_mask
= access_mask
;
1616 if (conn
->printer
) {
1618 * Printers are handled completely differently.
1619 * Most of the passed parameters are ignored.
1623 *pinfo
= FILE_WAS_CREATED
;
1626 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1627 smb_fname_str_dbg(smb_fname
)));
1630 DEBUG(0,("open_file_ntcreate: printer open without "
1631 "an SMB request!\n"));
1632 return NT_STATUS_INTERNAL_ERROR
;
1635 return print_spool_open(fsp
, smb_fname
->base_name
,
1639 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1641 return NT_STATUS_NO_MEMORY
;
1644 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1646 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
1647 new_dos_attributes
= 0;
1649 /* Windows allows a new file to be created and
1650 silently removes a FILE_ATTRIBUTE_DIRECTORY
1651 sent by the client. Do the same. */
1653 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
1655 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1657 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
1658 smb_fname
, parent_dir
);
1661 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1662 "access_mask=0x%x share_access=0x%x "
1663 "create_disposition = 0x%x create_options=0x%x "
1664 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1665 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
1666 access_mask
, share_access
, create_disposition
,
1667 create_options
, (unsigned int)unx_mode
, oplock_request
,
1668 (unsigned int)private_flags
));
1670 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
1671 DEBUG(0, ("No smb request but not an internal only open!\n"));
1672 return NT_STATUS_INTERNAL_ERROR
;
1676 * Only non-internal opens can be deferred at all
1681 if (get_deferred_open_message_state(req
,
1685 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
1686 /* Remember the absolute time of the original
1687 request with this mid. We'll use it later to
1688 see if this has timed out. */
1690 /* Remove the deferred open entry under lock. */
1691 remove_deferred_open_entry(
1692 state
->id
, req
->mid
,
1693 sconn_server_id(req
->sconn
));
1695 /* Ensure we don't reprocess this message. */
1696 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
1700 status
= check_name(conn
, smb_fname
->base_name
);
1701 if (!NT_STATUS_IS_OK(status
)) {
1706 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
1708 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
1712 /* ignore any oplock requests if oplocks are disabled */
1713 if (!lp_oplocks(SNUM(conn
)) ||
1714 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
1715 /* Mask off everything except the private Samba bits. */
1716 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
1719 /* this is for OS/2 long file names - say we don't support them */
1720 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
1721 /* OS/2 Workplace shell fix may be main code stream in a later
1723 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1725 if (use_nt_status()) {
1726 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1728 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
1731 switch( create_disposition
) {
1733 * Currently we're using FILE_SUPERSEDE as the same as
1734 * FILE_OVERWRITE_IF but they really are
1735 * different. FILE_SUPERSEDE deletes an existing file
1736 * (requiring delete access) then recreates it.
1738 case FILE_SUPERSEDE
:
1739 /* If file exists replace/overwrite. If file doesn't
1741 flags2
|= (O_CREAT
| O_TRUNC
);
1745 case FILE_OVERWRITE_IF
:
1746 /* If file exists replace/overwrite. If file doesn't
1748 flags2
|= (O_CREAT
| O_TRUNC
);
1753 /* If file exists open. If file doesn't exist error. */
1754 if (!file_existed
) {
1755 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1756 "requested for file %s and file "
1758 smb_fname_str_dbg(smb_fname
)));
1760 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1764 case FILE_OVERWRITE
:
1765 /* If file exists overwrite. If file doesn't exist
1767 if (!file_existed
) {
1768 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1769 "requested for file %s and file "
1771 smb_fname_str_dbg(smb_fname
) ));
1773 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1780 /* If file exists error. If file doesn't exist
1783 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1784 "requested for file %s and file "
1785 "already exists.\n",
1786 smb_fname_str_dbg(smb_fname
)));
1787 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1792 return map_nt_error_from_unix(errno
);
1794 flags2
|= (O_CREAT
|O_EXCL
);
1798 /* If file exists open. If file doesn't exist
1804 return NT_STATUS_INVALID_PARAMETER
;
1807 /* We only care about matching attributes on file exists and
1810 if (!posix_open
&& file_existed
&& ((create_disposition
== FILE_OVERWRITE
) ||
1811 (create_disposition
== FILE_OVERWRITE_IF
))) {
1812 if (!open_match_attributes(conn
, existing_dos_attributes
,
1814 smb_fname
->st
.st_ex_mode
,
1815 unx_mode
, &new_unx_mode
)) {
1816 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1817 "for file %s (%x %x) (0%o, 0%o)\n",
1818 smb_fname_str_dbg(smb_fname
),
1819 existing_dos_attributes
,
1821 (unsigned int)smb_fname
->st
.st_ex_mode
,
1822 (unsigned int)unx_mode
));
1824 return NT_STATUS_ACCESS_DENIED
;
1828 status
= smbd_calculate_access_mask(conn
, smb_fname
, file_existed
,
1831 if (!NT_STATUS_IS_OK(status
)) {
1832 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1833 "on file %s returned %s\n",
1834 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
1838 open_access_mask
= access_mask
;
1840 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1841 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
1844 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1845 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
1849 * Note that we ignore the append flag as append does not
1850 * mean the same thing under DOS and Unix.
1853 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
1854 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1855 /* DENY_DOS opens are always underlying read-write on the
1856 file handle, no matter what the requested access mask
1858 if ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
1859 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
1869 * Currently we only look at FILE_WRITE_THROUGH for create options.
1873 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
1878 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
1882 if (!posix_open
&& !CAN_WRITE(conn
)) {
1884 * We should really return a permission denied error if either
1885 * O_CREAT or O_TRUNC are set, but for compatibility with
1886 * older versions of Samba we just AND them out.
1888 flags2
&= ~(O_CREAT
|O_TRUNC
);
1892 * Ensure we can't write on a read-only share or file.
1895 if (flags
!= O_RDONLY
&& file_existed
&&
1896 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
1897 DEBUG(5,("open_file_ntcreate: write access requested for "
1898 "file %s on read only %s\n",
1899 smb_fname_str_dbg(smb_fname
),
1900 !CAN_WRITE(conn
) ? "share" : "file" ));
1902 return NT_STATUS_ACCESS_DENIED
;
1905 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1906 fsp
->share_access
= share_access
;
1907 fsp
->fh
->private_options
= private_flags
;
1908 fsp
->access_mask
= open_access_mask
; /* We change this to the
1909 * requested access_mask after
1910 * the open is done. */
1911 fsp
->posix_open
= posix_open
;
1913 /* Ensure no SAMBA_PRIVATE bits can be set. */
1914 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1916 if (timeval_is_zero(&request_time
)) {
1917 request_time
= fsp
->open_time
;
1921 struct byte_range_lock
*br_lck
= NULL
;
1922 struct share_mode_entry
*batch_entry
= NULL
;
1923 struct share_mode_entry
*exclusive_entry
= NULL
;
1924 bool got_level2_oplock
= false;
1925 bool got_a_none_oplock
= false;
1927 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
1928 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1930 if (!acquire_ordered_locks(talloc_tos(),
1938 return NT_STATUS_SHARING_VIOLATION
;
1941 /* Get the types we need to examine. */
1942 find_oplock_types(fsp
,
1948 &got_a_none_oplock
);
1950 /* First pass - send break only on batch oplocks. */
1951 if ((req
!= NULL
) &&
1952 delay_for_batch_oplocks(fsp
,
1956 schedule_defer_open(lck
, request_time
, req
);
1958 return NT_STATUS_SHARING_VIOLATION
;
1961 /* Use the client requested access mask here, not the one we
1963 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
1964 access_mask
, share_access
,
1965 create_options
, &file_existed
);
1967 if (NT_STATUS_IS_OK(status
)) {
1968 /* We might be going to allow this open. Check oplock
1970 /* Second pass - send break for both batch or
1971 * exclusive oplocks. */
1972 if ((req
!= NULL
) &&
1973 delay_for_exclusive_oplocks(
1978 schedule_defer_open(lck
, request_time
, req
);
1980 return NT_STATUS_SHARING_VIOLATION
;
1984 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
1985 /* DELETE_PENDING is not deferred for a second */
1990 grant_fsp_oplock_type(fsp
,
1996 if (!NT_STATUS_IS_OK(status
)) {
1997 uint32 can_access_mask
;
1998 bool can_access
= True
;
2000 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2002 /* Check if this can be done with the deny_dos and fcb
2005 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2006 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2008 DEBUG(0, ("DOS open without an SMB "
2011 return NT_STATUS_INTERNAL_ERROR
;
2014 /* Use the client requested access mask here,
2015 * not the one we open with. */
2016 status
= fcb_or_dos_open(req
,
2027 if (NT_STATUS_IS_OK(status
)) {
2030 *pinfo
= FILE_WAS_OPENED
;
2032 return NT_STATUS_OK
;
2037 * This next line is a subtlety we need for
2038 * MS-Access. If a file open will fail due to share
2039 * permissions and also for security (access) reasons,
2040 * we need to return the access failed error, not the
2041 * share error. We can't open the file due to kernel
2042 * oplock deadlock (it's possible we failed above on
2043 * the open_mode_check()) so use a userspace check.
2046 if (flags
& O_RDWR
) {
2047 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2048 } else if (flags
& O_WRONLY
) {
2049 can_access_mask
= FILE_WRITE_DATA
;
2051 can_access_mask
= FILE_READ_DATA
;
2054 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2055 !CAN_WRITE(conn
)) ||
2056 !can_access_file_data(conn
, smb_fname
,
2062 * If we're returning a share violation, ensure we
2063 * cope with the braindead 1 second delay.
2066 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2067 lp_defer_sharing_violations()) {
2068 struct timeval timeout
;
2069 struct deferred_open_record state
;
2072 /* this is a hack to speed up torture tests
2074 timeout_usecs
= lp_parm_int(SNUM(conn
),
2075 "smbd","sharedelay",
2076 SHARING_VIOLATION_USEC_WAIT
);
2078 /* This is a relative time, added to the absolute
2079 request_time value to get the absolute timeout time.
2080 Note that if this is the second or greater time we enter
2081 this codepath for this particular request mid then
2082 request_time is left as the absolute time of the *first*
2083 time this request mid was processed. This is what allows
2084 the request to eventually time out. */
2086 timeout
= timeval_set(0, timeout_usecs
);
2088 /* Nothing actually uses state.delayed_for_oplocks
2089 but it's handy to differentiate in debug messages
2090 between a 30 second delay due to oplock break, and
2091 a 1 second delay for share mode conflicts. */
2093 state
.delayed_for_oplocks
= False
;
2097 && !request_timed_out(request_time
,
2099 defer_open(lck
, request_time
, timeout
,
2107 * We have detected a sharing violation here
2108 * so return the correct error code
2110 status
= NT_STATUS_SHARING_VIOLATION
;
2112 status
= NT_STATUS_ACCESS_DENIED
;
2118 * We exit this block with the share entry *locked*.....
2122 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
2125 * Ensure we pay attention to default ACLs on directories if required.
2128 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2129 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2130 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2133 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2134 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2135 (unsigned int)flags
, (unsigned int)flags2
,
2136 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2137 (unsigned int)open_access_mask
));
2140 * open_file strips any O_TRUNC flags itself.
2143 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2144 flags
|flags2
, unx_mode
, access_mask
,
2147 if (!NT_STATUS_IS_OK(fsp_open
)) {
2152 if (!file_existed
) {
2153 struct byte_range_lock
*br_lck
= NULL
;
2154 struct share_mode_entry
*batch_entry
= NULL
;
2155 struct share_mode_entry
*exclusive_entry
= NULL
;
2156 bool got_level2_oplock
= false;
2157 bool got_a_none_oplock
= false;
2158 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2160 * Deal with the race condition where two smbd's detect the
2161 * file doesn't exist and do the create at the same time. One
2162 * of them will win and set a share mode, the other (ie. this
2163 * one) should check if the requested share mode for this
2164 * create is allowed.
2168 * Now the file exists and fsp is successfully opened,
2169 * fsp->dev and fsp->inode are valid and should replace the
2170 * dev=0,inode=0 from a non existent file. Spotted by
2171 * Nadav Danieli <nadavd@exanet.com>. JRA.
2176 if (!acquire_ordered_locks(talloc_tos(),
2184 return NT_STATUS_SHARING_VIOLATION
;
2187 /* Get the types we need to examine. */
2188 find_oplock_types(fsp
,
2194 &got_a_none_oplock
);
2196 /* First pass - send break only on batch oplocks. */
2197 if ((req
!= NULL
) &&
2198 delay_for_batch_oplocks(fsp
,
2202 schedule_defer_open(lck
, request_time
, req
);
2205 return NT_STATUS_SHARING_VIOLATION
;
2208 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2209 access_mask
, share_access
,
2210 create_options
, &file_existed
);
2212 if (NT_STATUS_IS_OK(status
)) {
2213 /* We might be going to allow this open. Check oplock
2215 /* Second pass - send break for both batch or
2216 * exclusive oplocks. */
2217 if ((req
!= NULL
) &&
2218 delay_for_exclusive_oplocks(
2223 schedule_defer_open(lck
, request_time
, req
);
2226 return NT_STATUS_SHARING_VIOLATION
;
2230 if (!NT_STATUS_IS_OK(status
)) {
2231 struct deferred_open_record state
;
2233 state
.delayed_for_oplocks
= False
;
2236 /* Do it all over again immediately. In the second
2237 * round we will find that the file existed and handle
2238 * the DELETE_PENDING and FCB cases correctly. No need
2239 * to duplicate the code here. Essentially this is a
2240 * "goto top of this function", but don't tell
2244 defer_open(lck
, request_time
, timeval_zero(),
2252 grant_fsp_oplock_type(fsp
,
2259 * We exit this block with the share entry *locked*.....
2264 SMB_ASSERT(lck
!= NULL
);
2266 /* Delete streams if create_disposition requires it */
2267 if (file_existed
&& clear_ads
&&
2268 !is_ntfs_stream_smb_fname(smb_fname
)) {
2269 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2270 if (!NT_STATUS_IS_OK(status
)) {
2277 /* note that we ignore failure for the following. It is
2278 basically a hack for NFS, and NFS will never set one of
2279 these only read them. Nobody but Samba can ever set a deny
2280 mode and we have already checked our more authoritative
2281 locking database for permission to set this deny mode. If
2282 the kernel refuses the operations then the kernel is wrong.
2283 note that GPFS supports it as well - jmcd */
2285 if (fsp
->fh
->fd
!= -1) {
2287 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2288 if(ret_flock
== -1 ){
2293 return NT_STATUS_SHARING_VIOLATION
;
2298 * At this point onwards, we can guarentee that the share entry
2299 * is locked, whether we created the file or not, and that the
2300 * deny mode is compatible with all current opens.
2304 * If requested, truncate the file.
2307 if (file_existed
&& (flags2
&O_TRUNC
)) {
2309 * We are modifing the file after open - update the stat
2312 if ((SMB_VFS_FTRUNCATE(fsp
, 0) == -1) ||
2313 (SMB_VFS_FSTAT(fsp
, &smb_fname
->st
)==-1)) {
2314 status
= map_nt_error_from_unix(errno
);
2322 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2323 * but we don't have to store this - just ignore it on access check.
2325 if (conn
->sconn
->using_smb2
) {
2327 * SMB2 doesn't return it (according to Microsoft tests).
2328 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2329 * File created with access = 0x7 (Read, Write, Delete)
2330 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2332 fsp
->access_mask
= access_mask
;
2334 /* But SMB1 does. */
2335 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2339 /* stat opens on existing files don't get oplocks. */
2340 if (is_stat_open(open_access_mask
)) {
2341 fsp
->oplock_type
= NO_OPLOCK
;
2344 if (!(flags2
& O_TRUNC
)) {
2345 info
= FILE_WAS_OPENED
;
2347 info
= FILE_WAS_OVERWRITTEN
;
2350 info
= FILE_WAS_CREATED
;
2358 * Setup the oplock info in both the shared memory and
2362 if (!set_file_oplock(fsp
, fsp
->oplock_type
)) {
2364 * Could not get the kernel oplock or there are byte-range
2365 * locks on the file.
2367 fsp
->oplock_type
= NO_OPLOCK
;
2370 if (info
== FILE_WAS_OVERWRITTEN
|| info
== FILE_WAS_CREATED
|| info
== FILE_WAS_SUPERSEDED
) {
2371 new_file_created
= True
;
2374 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2378 /* Handle strange delete on close create semantics. */
2379 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2381 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2383 if (!NT_STATUS_IS_OK(status
)) {
2384 /* Remember to delete the mode we just added. */
2385 del_share_mode(lck
, fsp
);
2390 /* Note that here we set the *inital* delete on close flag,
2391 not the regular one. The magic gets handled in close. */
2392 fsp
->initial_delete_on_close
= True
;
2395 if (new_file_created
) {
2396 /* Files should be initially set as archive */
2397 if (lp_map_archive(SNUM(conn
)) ||
2398 lp_store_dos_attributes(SNUM(conn
))) {
2400 if (file_set_dosmode(conn
, smb_fname
,
2401 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2402 parent_dir
, true) == 0) {
2403 unx_mode
= smb_fname
->st
.st_ex_mode
;
2409 /* Determine sparse flag. */
2411 /* POSIX opens are sparse by default. */
2412 fsp
->is_sparse
= true;
2414 fsp
->is_sparse
= (file_existed
&&
2415 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2419 * Take care of inherited ACLs on created files - if default ACL not
2423 if (!posix_open
&& !file_existed
&& !def_acl
) {
2425 int saved_errno
= errno
; /* We might get ENOSYS in the next
2428 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2430 errno
= saved_errno
; /* Ignore ENOSYS */
2433 } else if (new_unx_mode
) {
2437 /* Attributes need changing. File already existed. */
2440 int saved_errno
= errno
; /* We might get ENOSYS in the
2442 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2444 if (ret
== -1 && errno
== ENOSYS
) {
2445 errno
= saved_errno
; /* Ignore ENOSYS */
2447 DEBUG(5, ("open_file_ntcreate: reset "
2448 "attributes of file %s to 0%o\n",
2449 smb_fname_str_dbg(smb_fname
),
2450 (unsigned int)new_unx_mode
));
2451 ret
= 0; /* Don't do the fchmod below. */
2456 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2457 DEBUG(5, ("open_file_ntcreate: failed to reset "
2458 "attributes of file %s to 0%o\n",
2459 smb_fname_str_dbg(smb_fname
),
2460 (unsigned int)new_unx_mode
));
2463 /* If this is a successful open, we must remove any deferred open
2466 del_deferred_open_entry(lck
, req
->mid
,
2467 sconn_server_id(req
->sconn
));
2471 return NT_STATUS_OK
;
2475 /****************************************************************************
2476 Open a file for for write to ensure that we can fchmod it.
2477 ****************************************************************************/
2479 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2480 struct smb_filename
*smb_fname
,
2481 files_struct
**result
)
2483 if (!VALID_STAT(smb_fname
->st
)) {
2484 return NT_STATUS_INVALID_PARAMETER
;
2487 return SMB_VFS_CREATE_FILE(
2490 0, /* root_dir_fid */
2491 smb_fname
, /* fname */
2492 FILE_WRITE_DATA
, /* access_mask */
2493 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2495 FILE_OPEN
, /* create_disposition*/
2496 0, /* create_options */
2497 0, /* file_attributes */
2498 INTERNAL_OPEN_ONLY
, /* oplock_request */
2499 0, /* allocation_size */
2500 0, /* private_flags */
2503 result
, /* result */
2507 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2508 struct smb_filename
*smb_dname
,
2509 uint32 file_attributes
)
2512 char *parent_dir
= NULL
;
2514 bool posix_open
= false;
2515 bool need_re_stat
= false;
2516 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2518 if(access_mask
& ~(conn
->share_access
)) {
2519 DEBUG(5,("mkdir_internal: failing share access "
2520 "%s\n", lp_servicename(SNUM(conn
))));
2521 return NT_STATUS_ACCESS_DENIED
;
2524 status
= check_name(conn
, smb_dname
->base_name
);
2525 if (!NT_STATUS_IS_OK(status
)) {
2529 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2531 return NT_STATUS_NO_MEMORY
;
2534 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2536 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2538 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2541 status
= check_parent_access(conn
,
2546 if(!NT_STATUS_IS_OK(status
)) {
2547 DEBUG(5,("mkdir_internal: check_parent_access "
2548 "on directory %s for path %s returned %s\n",
2550 smb_dname
->base_name
,
2551 nt_errstr(status
) ));
2555 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2556 return map_nt_error_from_unix(errno
);
2559 /* Ensure we're checking for a symlink here.... */
2560 /* We don't want to get caught by a symlink racer. */
2562 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2563 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2564 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2565 return map_nt_error_from_unix(errno
);
2568 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2569 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2570 smb_fname_str_dbg(smb_dname
)));
2571 return NT_STATUS_NOT_A_DIRECTORY
;
2574 if (lp_store_dos_attributes(SNUM(conn
))) {
2576 file_set_dosmode(conn
, smb_dname
,
2577 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2582 if (lp_inherit_perms(SNUM(conn
))) {
2583 inherit_access_posix_acl(conn
, parent_dir
,
2584 smb_dname
->base_name
, mode
);
2585 need_re_stat
= true;
2590 * Check if high bits should have been set,
2591 * then (if bits are missing): add them.
2592 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2595 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2596 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2597 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2598 (smb_dname
->st
.st_ex_mode
|
2599 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2600 need_re_stat
= true;
2604 /* Change the owner if required. */
2605 if (lp_inherit_owner(SNUM(conn
))) {
2606 change_dir_owner_to_parent(conn
, parent_dir
,
2607 smb_dname
->base_name
,
2609 need_re_stat
= true;
2613 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2614 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2615 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2616 return map_nt_error_from_unix(errno
);
2620 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2621 smb_dname
->base_name
);
2623 return NT_STATUS_OK
;
2626 /****************************************************************************
2627 Ensure we didn't get symlink raced on opening a directory.
2628 ****************************************************************************/
2630 bool check_same_stat(const SMB_STRUCT_STAT
*sbuf1
,
2631 const SMB_STRUCT_STAT
*sbuf2
)
2633 if (sbuf1
->st_ex_uid
!= sbuf2
->st_ex_uid
||
2634 sbuf1
->st_ex_gid
!= sbuf2
->st_ex_gid
||
2635 sbuf1
->st_ex_dev
!= sbuf2
->st_ex_dev
||
2636 sbuf1
->st_ex_ino
!= sbuf2
->st_ex_ino
) {
2642 /****************************************************************************
2643 Open a directory from an NT SMB call.
2644 ****************************************************************************/
2646 static NTSTATUS
open_directory(connection_struct
*conn
,
2647 struct smb_request
*req
,
2648 struct smb_filename
*smb_dname
,
2650 uint32 share_access
,
2651 uint32 create_disposition
,
2652 uint32 create_options
,
2653 uint32 file_attributes
,
2655 files_struct
**result
)
2657 files_struct
*fsp
= NULL
;
2658 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2659 struct share_mode_lock
*lck
= NULL
;
2661 struct timespec mtimespec
;
2664 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
2666 /* Ensure we have a directory attribute. */
2667 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2669 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2670 "share_access = 0x%x create_options = 0x%x, "
2671 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2672 smb_fname_str_dbg(smb_dname
),
2673 (unsigned int)access_mask
,
2674 (unsigned int)share_access
,
2675 (unsigned int)create_options
,
2676 (unsigned int)create_disposition
,
2677 (unsigned int)file_attributes
));
2679 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
2680 (conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
2681 is_ntfs_stream_smb_fname(smb_dname
)) {
2682 DEBUG(2, ("open_directory: %s is a stream name!\n",
2683 smb_fname_str_dbg(smb_dname
)));
2684 return NT_STATUS_NOT_A_DIRECTORY
;
2687 status
= smbd_calculate_access_mask(conn
, smb_dname
, dir_existed
,
2688 access_mask
, &access_mask
);
2689 if (!NT_STATUS_IS_OK(status
)) {
2690 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2691 "on file %s returned %s\n",
2692 smb_fname_str_dbg(smb_dname
),
2693 nt_errstr(status
)));
2697 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2698 !security_token_has_privilege(get_current_nttok(conn
),
2699 SEC_PRIV_SECURITY
)) {
2700 DEBUG(10, ("open_directory: open on %s "
2701 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2702 smb_fname_str_dbg(smb_dname
)));
2703 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2706 switch( create_disposition
) {
2710 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2713 info
= FILE_WAS_OPENED
;
2718 /* If directory exists error. If directory doesn't
2722 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
2723 DEBUG(2, ("open_directory: unable to create "
2724 "%s. Error was %s\n",
2725 smb_fname_str_dbg(smb_dname
),
2726 nt_errstr(status
)));
2730 status
= mkdir_internal(conn
, smb_dname
,
2733 if (!NT_STATUS_IS_OK(status
)) {
2734 DEBUG(2, ("open_directory: unable to create "
2735 "%s. Error was %s\n",
2736 smb_fname_str_dbg(smb_dname
),
2737 nt_errstr(status
)));
2741 info
= FILE_WAS_CREATED
;
2746 * If directory exists open. If directory doesn't
2751 status
= NT_STATUS_OK
;
2752 info
= FILE_WAS_OPENED
;
2754 status
= mkdir_internal(conn
, smb_dname
,
2757 if (NT_STATUS_IS_OK(status
)) {
2758 info
= FILE_WAS_CREATED
;
2760 /* Cope with create race. */
2761 if (!NT_STATUS_EQUAL(status
,
2762 NT_STATUS_OBJECT_NAME_COLLISION
)) {
2763 DEBUG(2, ("open_directory: unable to create "
2764 "%s. Error was %s\n",
2765 smb_fname_str_dbg(smb_dname
),
2766 nt_errstr(status
)));
2769 info
= FILE_WAS_OPENED
;
2775 case FILE_SUPERSEDE
:
2776 case FILE_OVERWRITE
:
2777 case FILE_OVERWRITE_IF
:
2779 DEBUG(5,("open_directory: invalid create_disposition "
2780 "0x%x for directory %s\n",
2781 (unsigned int)create_disposition
,
2782 smb_fname_str_dbg(smb_dname
)));
2783 return NT_STATUS_INVALID_PARAMETER
;
2786 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2787 DEBUG(5,("open_directory: %s is not a directory !\n",
2788 smb_fname_str_dbg(smb_dname
)));
2789 return NT_STATUS_NOT_A_DIRECTORY
;
2792 if (info
== FILE_WAS_OPENED
) {
2793 status
= smbd_check_open_rights(conn
, smb_dname
, access_mask
);
2794 if (!NT_STATUS_IS_OK(status
)) {
2795 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2796 "file %s failed with %s\n",
2797 smb_fname_str_dbg(smb_dname
),
2798 nt_errstr(status
)));
2803 status
= file_new(req
, conn
, &fsp
);
2804 if(!NT_STATUS_IS_OK(status
)) {
2809 * Setup the files_struct for it.
2812 fsp
->mode
= smb_dname
->st
.st_ex_mode
;
2813 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
2814 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
2815 fsp
->file_pid
= req
? req
->smbpid
: 0;
2816 fsp
->can_lock
= False
;
2817 fsp
->can_read
= False
;
2818 fsp
->can_write
= False
;
2820 fsp
->share_access
= share_access
;
2821 fsp
->fh
->private_options
= 0;
2823 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2825 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2826 fsp
->print_file
= NULL
;
2827 fsp
->modified
= False
;
2828 fsp
->oplock_type
= NO_OPLOCK
;
2829 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
2830 fsp
->is_directory
= True
;
2831 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
2832 status
= fsp_set_smb_fname(fsp
, smb_dname
);
2833 if (!NT_STATUS_IS_OK(status
)) {
2834 file_free(req
, fsp
);
2838 mtimespec
= smb_dname
->st
.st_ex_mtime
;
2841 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
2843 /* POSIX allows us to open a directory with O_RDONLY. */
2844 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
2846 if (!NT_STATUS_IS_OK(status
)) {
2847 DEBUG(5, ("open_directory: Could not open fd for "
2849 smb_fname_str_dbg(smb_dname
),
2850 nt_errstr(status
)));
2851 file_free(req
, fsp
);
2855 status
= vfs_stat_fsp(fsp
);
2856 if (!NT_STATUS_IS_OK(status
)) {
2858 file_free(req
, fsp
);
2862 /* Ensure there was no race condition. */
2863 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
2864 DEBUG(5,("open_directory: stat struct differs for "
2866 smb_fname_str_dbg(smb_dname
)));
2868 file_free(req
, fsp
);
2869 return NT_STATUS_ACCESS_DENIED
;
2872 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
2873 conn
->connectpath
, smb_dname
, &mtimespec
);
2876 DEBUG(0, ("open_directory: Could not get share mode lock for "
2877 "%s\n", smb_fname_str_dbg(smb_dname
)));
2879 file_free(req
, fsp
);
2880 return NT_STATUS_SHARING_VIOLATION
;
2883 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2884 access_mask
, share_access
,
2885 create_options
, &dir_existed
);
2887 if (!NT_STATUS_IS_OK(status
)) {
2890 file_free(req
, fsp
);
2894 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2895 req
? req
->mid
: 0, NO_OPLOCK
);
2897 /* For directories the delete on close bit at open time seems
2898 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2899 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2900 status
= can_set_delete_on_close(fsp
, 0);
2901 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
2904 file_free(req
, fsp
);
2908 if (NT_STATUS_IS_OK(status
)) {
2909 /* Note that here we set the *inital* delete on close flag,
2910 not the regular one. The magic gets handled in close. */
2911 fsp
->initial_delete_on_close
= True
;
2922 return NT_STATUS_OK
;
2925 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
2926 struct smb_filename
*smb_dname
)
2931 status
= SMB_VFS_CREATE_FILE(
2934 0, /* root_dir_fid */
2935 smb_dname
, /* fname */
2936 FILE_READ_ATTRIBUTES
, /* access_mask */
2937 FILE_SHARE_NONE
, /* share_access */
2938 FILE_CREATE
, /* create_disposition*/
2939 FILE_DIRECTORY_FILE
, /* create_options */
2940 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
2941 0, /* oplock_request */
2942 0, /* allocation_size */
2943 0, /* private_flags */
2949 if (NT_STATUS_IS_OK(status
)) {
2950 close_file(req
, fsp
, NORMAL_CLOSE
);
2956 /****************************************************************************
2957 Receive notification that one of our open files has been renamed by another
2959 ****************************************************************************/
2961 void msg_file_was_renamed(struct messaging_context
*msg
,
2964 struct server_id server_id
,
2967 struct smbd_server_connection
*sconn
;
2969 char *frm
= (char *)data
->data
;
2971 const char *sharepath
;
2972 const char *base_name
;
2973 const char *stream_name
;
2974 struct smb_filename
*smb_fname
= NULL
;
2975 size_t sp_len
, bn_len
;
2978 sconn
= msg_ctx_to_sconn(msg
);
2979 if (sconn
== NULL
) {
2980 DEBUG(1, ("could not find sconn\n"));
2984 if (data
->data
== NULL
2985 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
2986 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2987 (int)data
->length
));
2991 /* Unpack the message. */
2992 pull_file_id_24(frm
, &id
);
2993 sharepath
= &frm
[24];
2994 sp_len
= strlen(sharepath
);
2995 base_name
= sharepath
+ sp_len
+ 1;
2996 bn_len
= strlen(base_name
);
2997 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
2999 /* stream_name must always be NULL if there is no stream. */
3000 if (stream_name
[0] == '\0') {
3004 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
3005 stream_name
, NULL
, &smb_fname
);
3006 if (!NT_STATUS_IS_OK(status
)) {
3010 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3012 sharepath
, smb_fname_str_dbg(smb_fname
),
3013 file_id_string_tos(&id
)));
3015 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3016 fsp
= file_find_di_next(fsp
)) {
3017 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3019 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3020 fsp
->fnum
, fsp_str_dbg(fsp
),
3021 smb_fname_str_dbg(smb_fname
)));
3022 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3023 if (!NT_STATUS_IS_OK(status
)) {
3028 /* Now we have the complete path we can work out if this is
3029 actually within this share and adjust newname accordingly. */
3030 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3031 "not sharepath %s) "
3032 "fnum %d from %s -> %s\n",
3033 fsp
->conn
->connectpath
,
3037 smb_fname_str_dbg(smb_fname
)));
3041 TALLOC_FREE(smb_fname
);
3046 * If a main file is opened for delete, all streams need to be checked for
3047 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3048 * If that works, delete them all by setting the delete on close and close.
3051 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3054 struct stream_struct
*stream_info
= NULL
;
3055 files_struct
**streams
= NULL
;
3057 unsigned int num_streams
= 0;
3058 TALLOC_CTX
*frame
= talloc_stackframe();
3061 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3062 &num_streams
, &stream_info
);
3064 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3065 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3066 DEBUG(10, ("no streams around\n"));
3068 return NT_STATUS_OK
;
3071 if (!NT_STATUS_IS_OK(status
)) {
3072 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3073 nt_errstr(status
)));
3077 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3080 if (num_streams
== 0) {
3082 return NT_STATUS_OK
;
3085 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3086 if (streams
== NULL
) {
3087 DEBUG(0, ("talloc failed\n"));
3088 status
= NT_STATUS_NO_MEMORY
;
3092 for (i
=0; i
<num_streams
; i
++) {
3093 struct smb_filename
*smb_fname
= NULL
;
3095 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3100 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3101 stream_info
[i
].name
,
3103 if (!NT_STATUS_IS_OK(status
)) {
3107 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3108 DEBUG(10, ("Unable to stat stream: %s\n",
3109 smb_fname_str_dbg(smb_fname
)));
3112 status
= SMB_VFS_CREATE_FILE(
3115 0, /* root_dir_fid */
3116 smb_fname
, /* fname */
3117 DELETE_ACCESS
, /* access_mask */
3118 (FILE_SHARE_READ
| /* share_access */
3119 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3120 FILE_OPEN
, /* create_disposition*/
3121 0, /* create_options */
3122 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3123 0, /* oplock_request */
3124 0, /* allocation_size */
3125 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3128 &streams
[i
], /* result */
3131 if (!NT_STATUS_IS_OK(status
)) {
3132 DEBUG(10, ("Could not open stream %s: %s\n",
3133 smb_fname_str_dbg(smb_fname
),
3134 nt_errstr(status
)));
3136 TALLOC_FREE(smb_fname
);
3139 TALLOC_FREE(smb_fname
);
3143 * don't touch the variable "status" beyond this point :-)
3146 for (i
-= 1 ; i
>= 0; i
--) {
3147 if (streams
[i
] == NULL
) {
3151 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3152 fsp_str_dbg(streams
[i
])));
3153 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3162 * Wrapper around open_file_ntcreate and open_directory
3165 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3166 struct smb_request
*req
,
3167 struct smb_filename
*smb_fname
,
3168 uint32_t access_mask
,
3169 uint32_t share_access
,
3170 uint32_t create_disposition
,
3171 uint32_t create_options
,
3172 uint32_t file_attributes
,
3173 uint32_t oplock_request
,
3174 uint64_t allocation_size
,
3175 uint32_t private_flags
,
3176 struct security_descriptor
*sd
,
3177 struct ea_list
*ea_list
,
3179 files_struct
**result
,
3182 int info
= FILE_WAS_OPENED
;
3183 files_struct
*base_fsp
= NULL
;
3184 files_struct
*fsp
= NULL
;
3187 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3188 "file_attributes = 0x%x, share_access = 0x%x, "
3189 "create_disposition = 0x%x create_options = 0x%x "
3190 "oplock_request = 0x%x private_flags = 0x%x "
3191 "ea_list = 0x%p, sd = 0x%p, "
3193 (unsigned int)access_mask
,
3194 (unsigned int)file_attributes
,
3195 (unsigned int)share_access
,
3196 (unsigned int)create_disposition
,
3197 (unsigned int)create_options
,
3198 (unsigned int)oplock_request
,
3199 (unsigned int)private_flags
,
3200 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3202 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3203 status
= NT_STATUS_NOT_SUPPORTED
;
3207 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3208 status
= NT_STATUS_INVALID_PARAMETER
;
3213 oplock_request
|= INTERNAL_OPEN_ONLY
;
3216 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3217 && (access_mask
& DELETE_ACCESS
)
3218 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3220 * We can't open a file with DELETE access if any of the
3221 * streams is open without FILE_SHARE_DELETE
3223 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3225 if (!NT_STATUS_IS_OK(status
)) {
3230 /* This is the correct thing to do (check every time) but can_delete
3231 * is expensive (it may have to read the parent directory
3232 * permissions). So for now we're not doing it unless we have a strong
3233 * hint the client is really going to delete this file. If the client
3234 * is forcing FILE_CREATE let the filesystem take care of the
3237 /* Setting FILE_SHARE_DELETE is the hint. */
3239 if ((create_disposition
!= FILE_CREATE
)
3240 && (access_mask
& DELETE_ACCESS
)
3241 && (!(can_delete_file_in_directory(conn
, smb_fname
) ||
3242 can_access_file_acl(conn
, smb_fname
, DELETE_ACCESS
)))) {
3243 status
= NT_STATUS_ACCESS_DENIED
;
3244 DEBUG(10,("create_file_unixpath: open file %s "
3245 "for delete ACCESS_DENIED\n",
3246 smb_fname_str_dbg(smb_fname
)));
3250 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3251 !security_token_has_privilege(get_current_nttok(conn
),
3252 SEC_PRIV_SECURITY
)) {
3253 DEBUG(10, ("create_file_unixpath: open on %s "
3254 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3255 smb_fname_str_dbg(smb_fname
)));
3256 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3260 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3261 && is_ntfs_stream_smb_fname(smb_fname
)
3262 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3263 uint32 base_create_disposition
;
3264 struct smb_filename
*smb_fname_base
= NULL
;
3266 if (create_options
& FILE_DIRECTORY_FILE
) {
3267 status
= NT_STATUS_NOT_A_DIRECTORY
;
3271 switch (create_disposition
) {
3273 base_create_disposition
= FILE_OPEN
;
3276 base_create_disposition
= FILE_OPEN_IF
;
3280 /* Create an smb_filename with stream_name == NULL. */
3281 status
= create_synthetic_smb_fname(talloc_tos(),
3282 smb_fname
->base_name
,
3285 if (!NT_STATUS_IS_OK(status
)) {
3289 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3290 DEBUG(10, ("Unable to stat stream: %s\n",
3291 smb_fname_str_dbg(smb_fname_base
)));
3294 /* Open the base file. */
3295 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3298 | FILE_SHARE_DELETE
,
3299 base_create_disposition
,
3300 0, 0, 0, 0, 0, NULL
, NULL
,
3302 TALLOC_FREE(smb_fname_base
);
3304 if (!NT_STATUS_IS_OK(status
)) {
3305 DEBUG(10, ("create_file_unixpath for base %s failed: "
3306 "%s\n", smb_fname
->base_name
,
3307 nt_errstr(status
)));
3310 /* we don't need to low level fd */
3315 * If it's a request for a directory open, deal with it separately.
3318 if (create_options
& FILE_DIRECTORY_FILE
) {
3320 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3321 status
= NT_STATUS_INVALID_PARAMETER
;
3325 /* Can't open a temp directory. IFS kit test. */
3326 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3327 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3328 status
= NT_STATUS_INVALID_PARAMETER
;
3333 * We will get a create directory here if the Win32
3334 * app specified a security descriptor in the
3335 * CreateDirectory() call.
3339 status
= open_directory(
3340 conn
, req
, smb_fname
, access_mask
, share_access
,
3341 create_disposition
, create_options
, file_attributes
,
3346 * Ordinary file case.
3349 status
= file_new(req
, conn
, &fsp
);
3350 if(!NT_STATUS_IS_OK(status
)) {
3354 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3355 if (!NT_STATUS_IS_OK(status
)) {
3360 * We're opening the stream element of a base_fsp
3361 * we already opened. Set up the base_fsp pointer.
3364 fsp
->base_fsp
= base_fsp
;
3367 status
= open_file_ntcreate(conn
,
3379 if(!NT_STATUS_IS_OK(status
)) {
3380 file_free(req
, fsp
);
3384 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3386 /* A stream open never opens a directory */
3389 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3394 * Fail the open if it was explicitly a non-directory
3398 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3399 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3404 status
= open_directory(
3405 conn
, req
, smb_fname
, access_mask
,
3406 share_access
, create_disposition
,
3407 create_options
, file_attributes
,
3412 if (!NT_STATUS_IS_OK(status
)) {
3416 fsp
->base_fsp
= base_fsp
;
3419 * According to the MS documentation, the only time the security
3420 * descriptor is applied to the opened file is iff we *created* the
3421 * file; an existing file stays the same.
3423 * Also, it seems (from observation) that you can open the file with
3424 * any access mask but you can still write the sd. We need to override
3425 * the granted access before we call set_sd
3426 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3429 if ((sd
!= NULL
) && (info
== FILE_WAS_CREATED
)
3430 && lp_nt_acl_support(SNUM(conn
))) {
3432 uint32_t sec_info_sent
;
3433 uint32_t saved_access_mask
= fsp
->access_mask
;
3435 sec_info_sent
= get_sec_info(sd
);
3437 fsp
->access_mask
= FILE_GENERIC_ALL
;
3439 /* Convert all the generic bits. */
3440 security_acl_map_generic(sd
->dacl
, &file_generic_mapping
);
3441 security_acl_map_generic(sd
->sacl
, &file_generic_mapping
);
3443 if (sec_info_sent
& (SECINFO_OWNER
|
3447 status
= SMB_VFS_FSET_NT_ACL(fsp
, sec_info_sent
, sd
);
3450 fsp
->access_mask
= saved_access_mask
;
3452 if (!NT_STATUS_IS_OK(status
)) {
3457 if ((ea_list
!= NULL
) &&
3458 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3459 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3460 if (!NT_STATUS_IS_OK(status
)) {
3465 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3466 status
= NT_STATUS_ACCESS_DENIED
;
3470 /* Save the requested allocation size. */
3471 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3473 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3474 fsp
->initial_allocation_size
= smb_roundup(
3475 fsp
->conn
, allocation_size
);
3476 if (fsp
->is_directory
) {
3477 /* Can't set allocation size on a directory. */
3478 status
= NT_STATUS_ACCESS_DENIED
;
3481 if (vfs_allocate_file_space(
3482 fsp
, fsp
->initial_allocation_size
) == -1) {
3483 status
= NT_STATUS_DISK_FULL
;
3487 fsp
->initial_allocation_size
= smb_roundup(
3488 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3492 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3495 if (pinfo
!= NULL
) {
3499 smb_fname
->st
= fsp
->fsp_name
->st
;
3501 return NT_STATUS_OK
;
3504 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3507 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3509 * The close_file below will close
3514 close_file(req
, fsp
, ERROR_CLOSE
);
3517 if (base_fsp
!= NULL
) {
3518 close_file(req
, base_fsp
, ERROR_CLOSE
);
3525 * Calculate the full path name given a relative fid.
3527 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
3528 struct smb_request
*req
,
3529 uint16_t root_dir_fid
,
3530 const struct smb_filename
*smb_fname
,
3531 struct smb_filename
**smb_fname_out
)
3533 files_struct
*dir_fsp
;
3534 char *parent_fname
= NULL
;
3535 char *new_base_name
= NULL
;
3538 if (root_dir_fid
== 0 || !smb_fname
) {
3539 status
= NT_STATUS_INTERNAL_ERROR
;
3543 dir_fsp
= file_fsp(req
, root_dir_fid
);
3545 if (dir_fsp
== NULL
) {
3546 status
= NT_STATUS_INVALID_HANDLE
;
3550 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
3551 status
= NT_STATUS_INVALID_HANDLE
;
3555 if (!dir_fsp
->is_directory
) {
3558 * Check to see if this is a mac fork of some kind.
3561 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
3562 is_ntfs_stream_smb_fname(smb_fname
)) {
3563 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3568 we need to handle the case when we get a
3569 relative open relative to a file and the
3570 pathname is blank - this is a reopen!
3571 (hint from demyn plantenberg)
3574 status
= NT_STATUS_INVALID_HANDLE
;
3578 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
3580 * We're at the toplevel dir, the final file name
3581 * must not contain ./, as this is filtered out
3582 * normally by srvstr_get_path and unix_convert
3583 * explicitly rejects paths containing ./.
3585 parent_fname
= talloc_strdup(talloc_tos(), "");
3586 if (parent_fname
== NULL
) {
3587 status
= NT_STATUS_NO_MEMORY
;
3591 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
3594 * Copy in the base directory name.
3597 parent_fname
= talloc_array(talloc_tos(), char,
3599 if (parent_fname
== NULL
) {
3600 status
= NT_STATUS_NO_MEMORY
;
3603 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
3607 * Ensure it ends in a '/'.
3608 * We used TALLOC_SIZE +2 to add space for the '/'.
3612 && (parent_fname
[dir_name_len
-1] != '\\')
3613 && (parent_fname
[dir_name_len
-1] != '/')) {
3614 parent_fname
[dir_name_len
] = '/';
3615 parent_fname
[dir_name_len
+1] = '\0';
3619 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
3620 smb_fname
->base_name
);
3621 if (new_base_name
== NULL
) {
3622 status
= NT_STATUS_NO_MEMORY
;
3626 status
= filename_convert(req
,
3628 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
3633 if (!NT_STATUS_IS_OK(status
)) {
3638 TALLOC_FREE(parent_fname
);
3639 TALLOC_FREE(new_base_name
);
3643 NTSTATUS
create_file_default(connection_struct
*conn
,
3644 struct smb_request
*req
,
3645 uint16_t root_dir_fid
,
3646 struct smb_filename
*smb_fname
,
3647 uint32_t access_mask
,
3648 uint32_t share_access
,
3649 uint32_t create_disposition
,
3650 uint32_t create_options
,
3651 uint32_t file_attributes
,
3652 uint32_t oplock_request
,
3653 uint64_t allocation_size
,
3654 uint32_t private_flags
,
3655 struct security_descriptor
*sd
,
3656 struct ea_list
*ea_list
,
3657 files_struct
**result
,
3660 int info
= FILE_WAS_OPENED
;
3661 files_struct
*fsp
= NULL
;
3663 bool stream_name
= false;
3665 DEBUG(10,("create_file: access_mask = 0x%x "
3666 "file_attributes = 0x%x, share_access = 0x%x, "
3667 "create_disposition = 0x%x create_options = 0x%x "
3668 "oplock_request = 0x%x "
3669 "private_flags = 0x%x "
3670 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3672 (unsigned int)access_mask
,
3673 (unsigned int)file_attributes
,
3674 (unsigned int)share_access
,
3675 (unsigned int)create_disposition
,
3676 (unsigned int)create_options
,
3677 (unsigned int)oplock_request
,
3678 (unsigned int)private_flags
,
3679 (unsigned int)root_dir_fid
,
3680 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3683 * Calculate the filename from the root_dir_if if necessary.
3686 if (root_dir_fid
!= 0) {
3687 struct smb_filename
*smb_fname_out
= NULL
;
3688 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
3689 smb_fname
, &smb_fname_out
);
3690 if (!NT_STATUS_IS_OK(status
)) {
3693 smb_fname
= smb_fname_out
;
3697 * Check to see if this is a mac fork of some kind.
3700 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
3702 enum FAKE_FILE_TYPE fake_file_type
;
3704 fake_file_type
= is_fake_file(smb_fname
);
3706 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
3709 * Here we go! support for changing the disk quotas
3712 * We need to fake up to open this MAGIC QUOTA file
3713 * and return a valid FID.
3715 * w2k close this file directly after openening xp
3716 * also tries a QUERY_FILE_INFO on the file and then
3719 status
= open_fake_file(req
, conn
, req
->vuid
,
3720 fake_file_type
, smb_fname
,
3722 if (!NT_STATUS_IS_OK(status
)) {
3726 ZERO_STRUCT(smb_fname
->st
);
3730 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
3731 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3736 /* All file access must go through check_name() */
3738 status
= check_name(conn
, smb_fname
->base_name
);
3739 if (!NT_STATUS_IS_OK(status
)) {
3743 if (stream_name
&& is_ntfs_default_stream_smb_fname(smb_fname
)) {
3745 smb_fname
->stream_name
= NULL
;
3746 /* We have to handle this error here. */
3747 if (create_options
& FILE_DIRECTORY_FILE
) {
3748 status
= NT_STATUS_NOT_A_DIRECTORY
;
3751 if (lp_posix_pathnames()) {
3752 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
3754 ret
= SMB_VFS_STAT(conn
, smb_fname
);
3757 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
3758 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3763 status
= create_file_unixpath(
3764 conn
, req
, smb_fname
, access_mask
, share_access
,
3765 create_disposition
, create_options
, file_attributes
,
3766 oplock_request
, allocation_size
, private_flags
,
3770 if (!NT_STATUS_IS_OK(status
)) {
3775 DEBUG(10, ("create_file: info=%d\n", info
));
3778 if (pinfo
!= NULL
) {
3781 return NT_STATUS_OK
;
3784 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
3787 close_file(req
, fsp
, ERROR_CLOSE
);