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 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 NTSTATUS
check_parent_access(struct connection_struct
*conn
,
206 uint32_t access_mask
,
207 char **pp_parent_dir
)
210 char *parent_dir
= NULL
;
211 struct security_descriptor
*parent_sd
= NULL
;
212 uint32_t access_granted
= 0;
214 if (!parent_dirname(talloc_tos(),
218 return NT_STATUS_NO_MEMORY
;
221 status
= SMB_VFS_GET_NT_ACL(conn
,
226 if (!NT_STATUS_IS_OK(status
)) {
227 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
228 "%s with error %s\n",
234 status
= smb1_file_se_access_check(conn
,
236 get_current_nttok(conn
),
239 if(!NT_STATUS_IS_OK(status
)) {
240 DEBUG(5,("check_parent_access: access check "
241 "on directory %s for "
242 "path %s for mask 0x%x returned (0x%x) %s\n",
247 nt_errstr(status
) ));
252 *pp_parent_dir
= parent_dir
;
257 /****************************************************************************
258 fd support routines - attempt to do a dos_open.
259 ****************************************************************************/
261 static NTSTATUS
fd_open(struct connection_struct
*conn
,
266 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
267 NTSTATUS status
= NT_STATUS_OK
;
271 * Never follow symlinks on a POSIX client. The
272 * client should be doing this.
275 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
280 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
281 if (fsp
->fh
->fd
== -1) {
282 status
= map_nt_error_from_unix(errno
);
283 if (errno
== EMFILE
) {
284 static time_t last_warned
= 0L;
286 if (time((time_t *) NULL
) > last_warned
) {
287 DEBUG(0,("Too many open files, unable "
288 "to open more! smbd's max "
290 lp_max_open_files()));
291 last_warned
= time((time_t *) NULL
);
297 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
298 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
299 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
304 /****************************************************************************
305 Close the file associated with a fsp.
306 ****************************************************************************/
308 NTSTATUS
fd_close(files_struct
*fsp
)
315 if (fsp
->fh
->fd
== -1) {
316 return NT_STATUS_OK
; /* What we used to call a stat open. */
318 if (fsp
->fh
->ref_count
> 1) {
319 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
322 ret
= SMB_VFS_CLOSE(fsp
);
325 return map_nt_error_from_unix(errno
);
330 /****************************************************************************
331 Change the ownership of a file to that of the parent directory.
332 Do this by fd if possible.
333 ****************************************************************************/
335 void change_file_owner_to_parent(connection_struct
*conn
,
336 const char *inherit_from_dir
,
339 struct smb_filename
*smb_fname_parent
= NULL
;
343 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
344 NULL
, NULL
, &smb_fname_parent
);
345 if (!NT_STATUS_IS_OK(status
)) {
349 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
351 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
352 "directory %s. Error was %s\n",
353 smb_fname_str_dbg(smb_fname_parent
),
355 TALLOC_FREE(smb_fname_parent
);
359 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
360 /* Already this uid - no need to change. */
361 DEBUG(10,("change_file_owner_to_parent: file %s "
362 "is already owned by uid %d\n",
364 (int)fsp
->fsp_name
->st
.st_ex_uid
));
365 TALLOC_FREE(smb_fname_parent
);
370 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
373 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
374 "file %s to parent directory uid %u. Error "
375 "was %s\n", fsp_str_dbg(fsp
),
376 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
379 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
380 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
381 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
382 /* Ensure the uid entry is updated. */
383 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
386 TALLOC_FREE(smb_fname_parent
);
389 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
390 const char *inherit_from_dir
,
392 SMB_STRUCT_STAT
*psbuf
)
394 struct smb_filename
*smb_fname_parent
= NULL
;
395 struct smb_filename
*smb_fname_cwd
= NULL
;
396 char *saved_dir
= NULL
;
397 TALLOC_CTX
*ctx
= talloc_tos();
398 NTSTATUS status
= NT_STATUS_OK
;
401 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
403 if (!NT_STATUS_IS_OK(status
)) {
407 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
409 status
= map_nt_error_from_unix(errno
);
410 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
411 "directory %s. Error was %s\n",
412 smb_fname_str_dbg(smb_fname_parent
),
417 /* We've already done an lstat into psbuf, and we know it's a
418 directory. If we can cd into the directory and the dev/ino
419 are the same then we can safely chown without races as
420 we're locking the directory in place by being in it. This
421 should work on any UNIX (thanks tridge :-). JRA.
424 saved_dir
= vfs_GetWd(ctx
,conn
);
426 status
= map_nt_error_from_unix(errno
);
427 DEBUG(0,("change_dir_owner_to_parent: failed to get "
428 "current working directory. Error was %s\n",
433 /* Chdir into the new path. */
434 if (vfs_ChDir(conn
, fname
) == -1) {
435 status
= map_nt_error_from_unix(errno
);
436 DEBUG(0,("change_dir_owner_to_parent: failed to change "
437 "current working directory to %s. Error "
438 "was %s\n", fname
, strerror(errno
) ));
442 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
444 if (!NT_STATUS_IS_OK(status
)) {
448 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
450 status
= map_nt_error_from_unix(errno
);
451 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
452 "directory '.' (%s) Error was %s\n",
453 fname
, strerror(errno
)));
457 /* Ensure we're pointing at the same place. */
458 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
459 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
460 DEBUG(0,("change_dir_owner_to_parent: "
461 "device/inode on directory %s changed. "
462 "Refusing to chown !\n", fname
));
463 status
= NT_STATUS_ACCESS_DENIED
;
467 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
468 /* Already this uid - no need to change. */
469 DEBUG(10,("change_dir_owner_to_parent: directory %s "
470 "is already owned by uid %d\n",
472 (int)smb_fname_cwd
->st
.st_ex_uid
));
473 status
= NT_STATUS_OK
;
478 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
482 status
= map_nt_error_from_unix(errno
);
483 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
484 "directory %s to parent directory uid %u. "
485 "Error was %s\n", fname
,
486 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
489 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
490 "directory %s to parent directory uid %u.\n",
491 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
492 /* Ensure the uid entry is updated. */
493 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
497 vfs_ChDir(conn
,saved_dir
);
499 TALLOC_FREE(smb_fname_parent
);
500 TALLOC_FREE(smb_fname_cwd
);
504 /****************************************************************************
506 ****************************************************************************/
508 static NTSTATUS
open_file(files_struct
*fsp
,
509 connection_struct
*conn
,
510 struct smb_request
*req
,
511 const char *parent_dir
,
514 uint32 access_mask
, /* client requested access mask. */
515 uint32 open_access_mask
) /* what we're actually using in the open. */
517 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
518 NTSTATUS status
= NT_STATUS_OK
;
519 int accmode
= (flags
& O_ACCMODE
);
520 int local_flags
= flags
;
521 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
522 bool file_created
= false;
527 /* Check permissions */
530 * This code was changed after seeing a client open request
531 * containing the open mode of (DENY_WRITE/read-only) with
532 * the 'create if not exist' bit set. The previous code
533 * would fail to open the file read only on a read-only share
534 * as it was checking the flags parameter directly against O_RDONLY,
535 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
539 if (!CAN_WRITE(conn
)) {
540 /* It's a read-only share - fail if we wanted to write. */
541 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
542 DEBUG(3,("Permission denied opening %s\n",
543 smb_fname_str_dbg(smb_fname
)));
544 return NT_STATUS_ACCESS_DENIED
;
545 } else if(flags
& O_CREAT
) {
546 /* We don't want to write - but we must make sure that
547 O_CREAT doesn't create the file if we have write
548 access into the directory.
550 flags
&= ~(O_CREAT
|O_EXCL
);
551 local_flags
&= ~(O_CREAT
|O_EXCL
);
556 * This little piece of insanity is inspired by the
557 * fact that an NT client can open a file for O_RDONLY,
558 * but set the create disposition to FILE_EXISTS_TRUNCATE.
559 * If the client *can* write to the file, then it expects to
560 * truncate the file, even though it is opening for readonly.
561 * Quicken uses this stupid trick in backup file creation...
562 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
563 * for helping track this one down. It didn't bite us in 2.0.x
564 * as we always opened files read-write in that release. JRA.
567 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
568 DEBUG(10,("open_file: truncate requested on read-only open "
569 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
570 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
573 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
574 (!file_existed
&& (local_flags
& O_CREAT
)) ||
575 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
579 * We can't actually truncate here as the file may be locked.
580 * open_file_ntcreate will take care of the truncate later. JRA.
583 local_flags
&= ~O_TRUNC
;
585 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
587 * We would block on opening a FIFO with no one else on the
588 * other end. Do what we used to do and add O_NONBLOCK to the
592 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
593 local_flags
|= O_NONBLOCK
;
597 /* Don't create files with Microsoft wildcard characters. */
600 * wildcard characters are allowed in stream names
601 * only test the basefilename
603 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
605 wild
= smb_fname
->base_name
;
607 if ((local_flags
& O_CREAT
) && !file_existed
&&
609 return NT_STATUS_OBJECT_NAME_INVALID
;
612 /* Can we access this file ? */
613 if (!fsp
->base_fsp
) {
614 /* Only do this check on non-stream open. */
616 status
= smbd_check_open_rights(conn
,
619 } else if (local_flags
& O_CREAT
){
620 status
= check_parent_access(conn
,
621 smb_fname
->base_name
,
625 /* File didn't exist and no O_CREAT. */
626 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
628 if (!NT_STATUS_IS_OK(status
)) {
629 DEBUG(10,("open_file: "
633 "smbd_check_open_rights" :
634 "check_parent_access",
635 smb_fname_str_dbg(smb_fname
),
636 nt_errstr(status
) ));
641 /* Actually do the open */
642 status
= fd_open(conn
, fsp
, local_flags
, unx_mode
);
643 if (!NT_STATUS_IS_OK(status
)) {
644 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
645 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
646 nt_errstr(status
),local_flags
,flags
));
650 if ((local_flags
& O_CREAT
) && !file_existed
) {
655 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
657 /* File must exist for a stat open. */
658 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
661 status
= smbd_check_open_rights(conn
,
665 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
667 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
668 /* This is a POSIX stat open for delete
669 * or rename on a symlink that points
671 DEBUG(10,("open_file: allowing POSIX "
672 "open on bad symlink %s\n",
673 smb_fname_str_dbg(smb_fname
)));
674 status
= NT_STATUS_OK
;
677 if (!NT_STATUS_IS_OK(status
)) {
678 DEBUG(10,("open_file: "
679 "smbd_check_open_rights on file "
681 smb_fname_str_dbg(smb_fname
),
682 nt_errstr(status
) ));
690 if (fsp
->fh
->fd
== -1) {
691 ret
= SMB_VFS_STAT(conn
, smb_fname
);
693 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
694 /* If we have an fd, this stat should succeed. */
696 DEBUG(0,("Error doing fstat on open file %s "
698 smb_fname_str_dbg(smb_fname
),
703 /* For a non-io open, this stat failing means file not found. JRA */
705 status
= map_nt_error_from_unix(errno
);
711 bool need_re_stat
= false;
712 /* Do all inheritance work after we've
713 done a successful stat call and filled
714 in the stat struct in fsp->fsp_name. */
716 /* Inherit the ACL if required */
717 if (lp_inherit_perms(SNUM(conn
))) {
718 inherit_access_posix_acl(conn
, parent_dir
,
719 smb_fname
->base_name
,
724 /* Change the owner if required. */
725 if (lp_inherit_owner(SNUM(conn
))) {
726 change_file_owner_to_parent(conn
, parent_dir
,
732 if (fsp
->fh
->fd
== -1) {
733 ret
= SMB_VFS_STAT(conn
, smb_fname
);
735 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
736 /* If we have an fd, this stat should succeed. */
738 DEBUG(0,("Error doing fstat on open file %s "
740 smb_fname_str_dbg(smb_fname
),
746 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
747 FILE_NOTIFY_CHANGE_FILE_NAME
,
748 smb_fname
->base_name
);
753 * POSIX allows read-only opens of directories. We don't
754 * want to do this (we use a different code path for this)
755 * so catch a directory open and return an EISDIR. JRA.
758 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
761 return NT_STATUS_FILE_IS_A_DIRECTORY
;
764 fsp
->mode
= smb_fname
->st
.st_ex_mode
;
765 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
766 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
767 fsp
->file_pid
= req
? req
->smbpid
: 0;
768 fsp
->can_lock
= True
;
769 fsp
->can_read
= (access_mask
& (FILE_READ_DATA
)) ? True
: False
;
770 if (!CAN_WRITE(conn
)) {
771 fsp
->can_write
= False
;
773 fsp
->can_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
776 fsp
->print_file
= NULL
;
777 fsp
->modified
= False
;
778 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
779 fsp
->is_directory
= False
;
780 if (conn
->aio_write_behind_list
&&
781 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
782 conn
->case_sensitive
)) {
783 fsp
->aio_write_behind
= True
;
786 fsp
->wcp
= NULL
; /* Write cache pointer. */
788 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
789 conn
->session_info
->unix_info
->unix_name
,
790 smb_fname_str_dbg(smb_fname
),
791 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
792 conn
->num_files_open
));
798 /****************************************************************************
799 Check if we can open a file with a share mode.
800 Returns True if conflict, False if not.
801 ****************************************************************************/
803 static bool share_conflict(struct share_mode_entry
*entry
,
807 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
808 "entry->share_access = 0x%x, "
809 "entry->private_options = 0x%x\n",
810 (unsigned int)entry
->access_mask
,
811 (unsigned int)entry
->share_access
,
812 (unsigned int)entry
->private_options
));
814 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
815 (unsigned int)access_mask
, (unsigned int)share_access
));
817 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
821 DELETE_ACCESS
)) == 0) {
822 DEBUG(10,("share_conflict: No conflict due to "
823 "entry->access_mask = 0x%x\n",
824 (unsigned int)entry
->access_mask
));
828 if ((access_mask
& (FILE_WRITE_DATA
|
832 DELETE_ACCESS
)) == 0) {
833 DEBUG(10,("share_conflict: No conflict due to "
834 "access_mask = 0x%x\n",
835 (unsigned int)access_mask
));
839 #if 1 /* JRA TEST - Superdebug. */
840 #define CHECK_MASK(num, am, right, sa, share) \
841 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
842 (unsigned int)(num), (unsigned int)(am), \
843 (unsigned int)(right), (unsigned int)(am)&(right) )); \
844 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
845 (unsigned int)(num), (unsigned int)(sa), \
846 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
847 if (((am) & (right)) && !((sa) & (share))) { \
848 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
849 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
850 (unsigned int)(share) )); \
854 #define CHECK_MASK(num, am, right, sa, share) \
855 if (((am) & (right)) && !((sa) & (share))) { \
856 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
857 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
858 (unsigned int)(share) )); \
863 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
864 share_access
, FILE_SHARE_WRITE
);
865 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
866 entry
->share_access
, FILE_SHARE_WRITE
);
868 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
869 share_access
, FILE_SHARE_READ
);
870 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
871 entry
->share_access
, FILE_SHARE_READ
);
873 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
874 share_access
, FILE_SHARE_DELETE
);
875 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
876 entry
->share_access
, FILE_SHARE_DELETE
);
878 DEBUG(10,("share_conflict: No conflict.\n"));
882 #if defined(DEVELOPER)
883 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
885 struct share_mode_entry
*share_entry
)
889 if (!procid_is_me(&share_entry
->pid
)) {
893 if (is_deferred_open_entry(share_entry
) &&
894 !open_was_deferred(sconn
, share_entry
->op_mid
)) {
895 char *str
= talloc_asprintf(talloc_tos(),
896 "Got a deferred entry without a request: "
898 share_mode_str(talloc_tos(), num
, share_entry
));
902 if (!is_valid_share_mode_entry(share_entry
)) {
906 fsp
= file_find_dif(sconn
, share_entry
->id
,
907 share_entry
->share_file_id
);
909 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
910 share_mode_str(talloc_tos(), num
, share_entry
) ));
911 smb_panic("validate_my_share_entries: Cannot match a "
912 "share entry with an open file\n");
915 if (is_deferred_open_entry(share_entry
) ||
916 is_unused_share_mode_entry(share_entry
)) {
920 if ((share_entry
->op_type
== NO_OPLOCK
) &&
921 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
922 /* Someone has already written to it, but I haven't yet
927 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
936 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
937 share_mode_str(talloc_tos(), num
, share_entry
) ));
938 str
= talloc_asprintf(talloc_tos(),
939 "validate_my_share_entries: "
940 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
941 fsp
->fsp_name
->base_name
,
942 (unsigned int)fsp
->oplock_type
,
943 (unsigned int)share_entry
->op_type
);
949 bool is_stat_open(uint32 access_mask
)
951 return (access_mask
&&
952 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
953 FILE_WRITE_ATTRIBUTES
))==0) &&
954 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
955 FILE_WRITE_ATTRIBUTES
)) != 0));
958 /****************************************************************************
959 Deal with share modes
960 Invarient: Share mode must be locked on entry and exit.
961 Returns -1 on error, or number of share modes on success (may be zero).
962 ****************************************************************************/
964 static NTSTATUS
open_mode_check(connection_struct
*conn
,
965 struct share_mode_lock
*lck
,
969 uint32 create_options
,
974 if(lck
->num_share_modes
== 0) {
978 *file_existed
= True
;
980 /* A delete on close prohibits everything */
982 if (is_delete_on_close_set(lck
, name_hash
)) {
983 return NT_STATUS_DELETE_PENDING
;
986 if (is_stat_open(access_mask
)) {
987 /* Stat open that doesn't trigger oplock breaks or share mode
988 * checks... ! JRA. */
993 * Check if the share modes will give us access.
996 #if defined(DEVELOPER)
997 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
998 validate_my_share_entries(conn
->sconn
, i
,
999 &lck
->share_modes
[i
]);
1003 if (!lp_share_modes(SNUM(conn
))) {
1004 return NT_STATUS_OK
;
1007 /* Now we check the share modes, after any oplock breaks. */
1008 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
1010 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
1014 /* someone else has a share lock on it, check to see if we can
1016 if (share_conflict(&lck
->share_modes
[i
],
1017 access_mask
, share_access
)) {
1018 return NT_STATUS_SHARING_VIOLATION
;
1022 return NT_STATUS_OK
;
1025 static bool is_delete_request(files_struct
*fsp
) {
1026 return ((fsp
->access_mask
== DELETE_ACCESS
) &&
1027 (fsp
->oplock_type
== NO_OPLOCK
));
1031 * Send a break message to the oplock holder and delay the open for
1035 static NTSTATUS
send_break_message(files_struct
*fsp
,
1036 struct share_mode_entry
*exclusive
,
1041 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1043 DEBUG(10, ("Sending break request to PID %s\n",
1044 procid_str_static(&exclusive
->pid
)));
1045 exclusive
->op_mid
= mid
;
1047 /* Create the message. */
1048 share_mode_entry_to_message(msg
, exclusive
);
1050 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1051 don't want this set in the share mode struct pointed to by lck. */
1053 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
1054 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
1055 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
1058 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
1059 MSG_SMB_BREAK_REQUEST
,
1061 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
1062 if (!NT_STATUS_IS_OK(status
)) {
1063 DEBUG(3, ("Could not send oplock break message: %s\n",
1064 nt_errstr(status
)));
1071 * Return share_mode_entry pointers for :
1072 * 1). Batch oplock entry.
1073 * 2). Batch or exclusive oplock entry (may be identical to #1).
1074 * bool have_level2_oplock
1075 * bool have_no_oplock.
1076 * Do internal consistency checks on the share mode for a file.
1079 static void find_oplock_types(files_struct
*fsp
,
1081 struct share_mode_lock
*lck
,
1082 struct share_mode_entry
**pp_batch
,
1083 struct share_mode_entry
**pp_ex_or_batch
,
1085 bool *got_no_oplock
)
1090 *pp_ex_or_batch
= NULL
;
1091 *got_level2
= false;
1092 *got_no_oplock
= false;
1094 /* Ignore stat or internal opens, as is done in
1095 delay_for_batch_oplocks() and
1096 delay_for_exclusive_oplocks().
1098 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1102 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1103 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
1107 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
&&
1108 is_stat_open(lck
->share_modes
[i
].access_mask
)) {
1109 /* We ignore stat opens in the table - they
1110 always have NO_OPLOCK and never get or
1111 cause breaks. JRA. */
1115 if (BATCH_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1116 /* batch - can only be one. */
1117 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
1118 smb_panic("Bad batch oplock entry.");
1120 *pp_batch
= &lck
->share_modes
[i
];
1123 if (EXCLUSIVE_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1124 /* Exclusive or batch - can only be one. */
1125 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
1126 smb_panic("Bad exclusive or batch oplock entry.");
1128 *pp_ex_or_batch
= &lck
->share_modes
[i
];
1131 if (LEVEL_II_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1132 if (*pp_batch
|| *pp_ex_or_batch
) {
1133 smb_panic("Bad levelII oplock entry.");
1138 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
) {
1139 if (*pp_batch
|| *pp_ex_or_batch
) {
1140 smb_panic("Bad no oplock entry.");
1142 *got_no_oplock
= true;
1147 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1150 struct share_mode_entry
*batch_entry
)
1152 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1156 if (batch_entry
!= NULL
) {
1157 /* Found a batch oplock */
1158 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1164 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1167 struct share_mode_entry
*ex_entry
)
1169 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1173 if (ex_entry
!= NULL
) {
1174 /* Found an exclusive or batch oplock */
1175 bool delay_it
= is_delete_request(fsp
) ?
1176 BATCH_OPLOCK_TYPE(ex_entry
->op_type
) : true;
1178 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1185 static void grant_fsp_oplock_type(files_struct
*fsp
,
1186 const struct byte_range_lock
*br_lck
,
1188 bool got_level2_oplock
,
1189 bool got_a_none_oplock
)
1191 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1192 lp_level2_oplocks(SNUM(fsp
->conn
));
1194 /* Start by granting what the client asked for,
1195 but ensure no SAMBA_PRIVATE bits can be set. */
1196 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1198 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1199 /* No oplocks on internal open. */
1200 fsp
->oplock_type
= NO_OPLOCK
;
1201 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1202 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1204 } else if (br_lck
&& br_lck
->num_locks
> 0) {
1205 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1207 fsp
->oplock_type
= NO_OPLOCK
;
1210 if (is_stat_open(fsp
->access_mask
)) {
1211 /* Leave the value already set. */
1212 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1213 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1218 * Match what was requested (fsp->oplock_type) with
1219 * what was found in the existing share modes.
1222 if (got_a_none_oplock
) {
1223 fsp
->oplock_type
= NO_OPLOCK
;
1224 } else if (got_level2_oplock
) {
1225 if (fsp
->oplock_type
== NO_OPLOCK
||
1226 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1227 /* Store a level2 oplock, but don't tell the client */
1228 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1230 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1233 /* All share_mode_entries are placeholders or deferred.
1234 * Silently upgrade to fake levelII if the client didn't
1235 * ask for an oplock. */
1236 if (fsp
->oplock_type
== NO_OPLOCK
) {
1237 /* Store a level2 oplock, but don't tell the client */
1238 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1243 * Don't grant level2 to clients that don't want them
1244 * or if we've turned them off.
1246 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1247 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1250 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1251 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1254 bool request_timed_out(struct timeval request_time
,
1255 struct timeval timeout
)
1257 struct timeval now
, end_time
;
1259 end_time
= timeval_sum(&request_time
, &timeout
);
1260 return (timeval_compare(&end_time
, &now
) < 0);
1263 /****************************************************************************
1264 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1265 ****************************************************************************/
1267 static void defer_open(struct share_mode_lock
*lck
,
1268 struct timeval request_time
,
1269 struct timeval timeout
,
1270 struct smb_request
*req
,
1271 struct deferred_open_record
*state
)
1275 /* Paranoia check */
1277 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1278 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1280 if (!is_deferred_open_entry(e
)) {
1284 if (procid_is_me(&e
->pid
) && (e
->op_mid
== req
->mid
)) {
1285 DEBUG(0, ("Trying to defer an already deferred "
1286 "request: mid=%llu, exiting\n",
1287 (unsigned long long)req
->mid
));
1288 exit_server("attempt to defer a deferred request");
1292 /* End paranoia check */
1294 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1295 "open entry for mid %llu\n",
1296 (unsigned int)request_time
.tv_sec
,
1297 (unsigned int)request_time
.tv_usec
,
1298 (unsigned long long)req
->mid
));
1300 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1301 state
->id
, (char *)state
, sizeof(*state
))) {
1302 exit_server("push_deferred_open_message_smb failed");
1304 add_deferred_open(lck
, req
->mid
, request_time
,
1305 sconn_server_id(req
->sconn
), state
->id
);
1309 /****************************************************************************
1310 On overwrite open ensure that the attributes match.
1311 ****************************************************************************/
1313 bool open_match_attributes(connection_struct
*conn
,
1314 uint32 old_dos_attr
,
1315 uint32 new_dos_attr
,
1316 mode_t existing_unx_mode
,
1317 mode_t new_unx_mode
,
1318 mode_t
*returned_unx_mode
)
1320 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1322 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1323 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1325 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1326 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1327 *returned_unx_mode
= new_unx_mode
;
1329 *returned_unx_mode
= (mode_t
)0;
1332 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1333 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1334 "returned_unx_mode = 0%o\n",
1335 (unsigned int)old_dos_attr
,
1336 (unsigned int)existing_unx_mode
,
1337 (unsigned int)new_dos_attr
,
1338 (unsigned int)*returned_unx_mode
));
1340 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1341 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1342 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1343 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1347 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1348 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1349 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1356 /****************************************************************************
1357 Special FCB or DOS processing in the case of a sharing violation.
1358 Try and find a duplicated file handle.
1359 ****************************************************************************/
1361 NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1362 connection_struct
*conn
,
1363 files_struct
*fsp_to_dup_into
,
1364 const struct smb_filename
*smb_fname
,
1369 uint32 share_access
,
1370 uint32 create_options
)
1374 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1375 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1377 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1378 fsp
= file_find_di_next(fsp
)) {
1380 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1381 "vuid = %u, file_pid = %u, private_options = 0x%x "
1382 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1383 fsp
->fh
->fd
, (unsigned int)fsp
->vuid
,
1384 (unsigned int)fsp
->file_pid
,
1385 (unsigned int)fsp
->fh
->private_options
,
1386 (unsigned int)fsp
->access_mask
));
1388 if (fsp
->fh
->fd
!= -1 &&
1389 fsp
->vuid
== vuid
&&
1390 fsp
->file_pid
== file_pid
&&
1391 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1392 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1393 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1394 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1395 strequal(fsp
->fsp_name
->stream_name
,
1396 smb_fname
->stream_name
)) {
1397 DEBUG(10,("fcb_or_dos_open: file match\n"));
1403 return NT_STATUS_NOT_FOUND
;
1406 /* quite an insane set of semantics ... */
1407 if (is_executable(smb_fname
->base_name
) &&
1408 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1409 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1410 return NT_STATUS_INVALID_PARAMETER
;
1413 /* We need to duplicate this fsp. */
1414 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1415 create_options
, fsp_to_dup_into
);
1418 static void schedule_defer_open(struct share_mode_lock
*lck
,
1419 struct timeval request_time
,
1420 struct smb_request
*req
)
1422 struct deferred_open_record state
;
1424 /* This is a relative time, added to the absolute
1425 request_time value to get the absolute timeout time.
1426 Note that if this is the second or greater time we enter
1427 this codepath for this particular request mid then
1428 request_time is left as the absolute time of the *first*
1429 time this request mid was processed. This is what allows
1430 the request to eventually time out. */
1432 struct timeval timeout
;
1434 /* Normally the smbd we asked should respond within
1435 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1436 * the client did, give twice the timeout as a safety
1437 * measure here in case the other smbd is stuck
1438 * somewhere else. */
1440 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1442 /* Nothing actually uses state.delayed_for_oplocks
1443 but it's handy to differentiate in debug messages
1444 between a 30 second delay due to oplock break, and
1445 a 1 second delay for share mode conflicts. */
1447 state
.delayed_for_oplocks
= True
;
1450 if (!request_timed_out(request_time
, timeout
)) {
1451 defer_open(lck
, request_time
, timeout
, req
, &state
);
1455 /****************************************************************************
1456 Work out what access_mask to use from what the client sent us.
1457 ****************************************************************************/
1459 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1460 const struct smb_filename
*smb_fname
,
1462 uint32_t access_mask
,
1463 uint32_t *access_mask_out
)
1466 uint32_t orig_access_mask
= access_mask
;
1467 uint32_t rejected_share_access
;
1470 * Convert GENERIC bits to specific bits.
1473 se_map_generic(&access_mask
, &file_generic_mapping
);
1475 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1476 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1479 struct security_descriptor
*sd
;
1480 uint32_t access_granted
= 0;
1482 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1487 if (!NT_STATUS_IS_OK(status
)) {
1488 DEBUG(10,("smbd_calculate_access_mask: "
1489 "Could not get acl on file %s: %s\n",
1490 smb_fname_str_dbg(smb_fname
),
1491 nt_errstr(status
)));
1492 return NT_STATUS_ACCESS_DENIED
;
1495 status
= smb1_file_se_access_check(conn
,
1497 get_current_nttok(conn
),
1503 if (!NT_STATUS_IS_OK(status
)) {
1504 DEBUG(10, ("smbd_calculate_access_mask: "
1505 "Access denied on file %s: "
1506 "when calculating maximum access\n",
1507 smb_fname_str_dbg(smb_fname
)));
1508 return NT_STATUS_ACCESS_DENIED
;
1511 access_mask
= access_granted
;
1513 access_mask
= FILE_GENERIC_ALL
;
1516 access_mask
&= conn
->share_access
;
1519 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1521 if (rejected_share_access
) {
1522 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1523 "file %s: rejected by share access mask[0x%08X] "
1524 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1525 smb_fname_str_dbg(smb_fname
),
1527 orig_access_mask
, access_mask
,
1528 rejected_share_access
));
1529 return NT_STATUS_ACCESS_DENIED
;
1532 *access_mask_out
= access_mask
;
1533 return NT_STATUS_OK
;
1536 /****************************************************************************
1537 Remove the deferred open entry under lock.
1538 ****************************************************************************/
1540 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1541 struct server_id pid
)
1543 struct share_mode_lock
*lck
= get_share_mode_lock(talloc_tos(), id
,
1546 DEBUG(0, ("could not get share mode lock\n"));
1548 del_deferred_open_entry(lck
, mid
, pid
);
1553 /****************************************************************
1554 Ensure we get the brlock lock followed by the share mode lock
1555 in the correct order to prevent deadlocks if other smbd's are
1556 using the brlock database on this file simultaneously with this open
1557 (that code also gets the locks in brlock -> share mode lock order).
1558 ****************************************************************/
1560 static bool acquire_ordered_locks(TALLOC_CTX
*mem_ctx
,
1562 const struct file_id id
,
1563 const char *connectpath
,
1564 const struct smb_filename
*smb_fname
,
1565 const struct timespec
*p_old_write_time
,
1566 struct share_mode_lock
**p_lck
,
1567 struct byte_range_lock
**p_br_lck
)
1569 /* Ordering - we must get the br_lck for this
1570 file before the share mode. */
1571 if (lp_locking(fsp
->conn
->params
)) {
1572 *p_br_lck
= brl_get_locks_readonly(fsp
);
1573 if (*p_br_lck
== NULL
) {
1574 DEBUG(0, ("Could not get br_lock\n"));
1577 /* Note - we don't need to free the returned
1578 br_lck explicitly as it was allocated on talloc_tos()
1579 and so will be autofreed (and release the lock)
1580 once the frame context disappears.
1582 If it was set to fsp->brlock_rec then it was
1583 talloc_move'd to hang off the fsp pointer and
1584 in this case is guarenteed to not be holding the
1585 lock on the brlock database. */
1588 *p_lck
= get_share_mode_lock(mem_ctx
,
1594 if (*p_lck
== NULL
) {
1595 DEBUG(0, ("Could not get share mode lock\n"));
1596 TALLOC_FREE(*p_br_lck
);
1602 /****************************************************************************
1603 Open a file with a share mode. Passed in an already created files_struct *.
1604 ****************************************************************************/
1606 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1607 struct smb_request
*req
,
1608 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1609 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1610 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1611 uint32 create_options
, /* options such as delete on close. */
1612 uint32 new_dos_attributes
, /* attributes used for new file. */
1613 int oplock_request
, /* internal Samba oplock codes. */
1614 /* Information (FILE_EXISTS etc.) */
1615 uint32_t private_flags
, /* Samba specific flags. */
1619 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1622 bool file_existed
= VALID_STAT(smb_fname
->st
);
1623 bool def_acl
= False
;
1624 bool posix_open
= False
;
1625 bool new_file_created
= False
;
1626 bool clear_ads
= false;
1628 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1629 mode_t new_unx_mode
= (mode_t
)0;
1630 mode_t unx_mode
= (mode_t
)0;
1632 uint32 existing_dos_attributes
= 0;
1633 struct timeval request_time
= timeval_zero();
1634 struct share_mode_lock
*lck
= NULL
;
1635 uint32 open_access_mask
= access_mask
;
1641 if (conn
->printer
) {
1643 * Printers are handled completely differently.
1644 * Most of the passed parameters are ignored.
1648 *pinfo
= FILE_WAS_CREATED
;
1651 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1652 smb_fname_str_dbg(smb_fname
)));
1655 DEBUG(0,("open_file_ntcreate: printer open without "
1656 "an SMB request!\n"));
1657 return NT_STATUS_INTERNAL_ERROR
;
1660 return print_spool_open(fsp
, smb_fname
->base_name
,
1664 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1666 return NT_STATUS_NO_MEMORY
;
1669 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1671 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
1672 new_dos_attributes
= 0;
1674 /* Windows allows a new file to be created and
1675 silently removes a FILE_ATTRIBUTE_DIRECTORY
1676 sent by the client. Do the same. */
1678 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
1680 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1682 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
1683 smb_fname
, parent_dir
);
1686 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1687 "access_mask=0x%x share_access=0x%x "
1688 "create_disposition = 0x%x create_options=0x%x "
1689 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1690 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
1691 access_mask
, share_access
, create_disposition
,
1692 create_options
, (unsigned int)unx_mode
, oplock_request
,
1693 (unsigned int)private_flags
));
1695 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
1696 DEBUG(0, ("No smb request but not an internal only open!\n"));
1697 return NT_STATUS_INTERNAL_ERROR
;
1701 * Only non-internal opens can be deferred at all
1706 if (get_deferred_open_message_state(req
,
1710 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
1711 /* Remember the absolute time of the original
1712 request with this mid. We'll use it later to
1713 see if this has timed out. */
1715 /* Remove the deferred open entry under lock. */
1716 remove_deferred_open_entry(
1717 state
->id
, req
->mid
,
1718 sconn_server_id(req
->sconn
));
1720 /* Ensure we don't reprocess this message. */
1721 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
1725 status
= check_name(conn
, smb_fname
->base_name
);
1726 if (!NT_STATUS_IS_OK(status
)) {
1731 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
1733 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
1737 /* ignore any oplock requests if oplocks are disabled */
1738 if (!lp_oplocks(SNUM(conn
)) ||
1739 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
1740 /* Mask off everything except the private Samba bits. */
1741 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
1744 /* this is for OS/2 long file names - say we don't support them */
1745 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
1746 /* OS/2 Workplace shell fix may be main code stream in a later
1748 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1750 if (use_nt_status()) {
1751 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1753 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
1756 switch( create_disposition
) {
1758 * Currently we're using FILE_SUPERSEDE as the same as
1759 * FILE_OVERWRITE_IF but they really are
1760 * different. FILE_SUPERSEDE deletes an existing file
1761 * (requiring delete access) then recreates it.
1763 case FILE_SUPERSEDE
:
1764 /* If file exists replace/overwrite. If file doesn't
1766 flags2
|= (O_CREAT
| O_TRUNC
);
1770 case FILE_OVERWRITE_IF
:
1771 /* If file exists replace/overwrite. If file doesn't
1773 flags2
|= (O_CREAT
| O_TRUNC
);
1778 /* If file exists open. If file doesn't exist error. */
1779 if (!file_existed
) {
1780 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1781 "requested for file %s and file "
1783 smb_fname_str_dbg(smb_fname
)));
1785 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1789 case FILE_OVERWRITE
:
1790 /* If file exists overwrite. If file doesn't exist
1792 if (!file_existed
) {
1793 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1794 "requested for file %s and file "
1796 smb_fname_str_dbg(smb_fname
) ));
1798 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1805 /* If file exists error. If file doesn't exist
1808 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1809 "requested for file %s and file "
1810 "already exists.\n",
1811 smb_fname_str_dbg(smb_fname
)));
1812 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1817 return map_nt_error_from_unix(errno
);
1819 flags2
|= (O_CREAT
|O_EXCL
);
1823 /* If file exists open. If file doesn't exist
1829 return NT_STATUS_INVALID_PARAMETER
;
1832 /* We only care about matching attributes on file exists and
1835 if (!posix_open
&& file_existed
&& ((create_disposition
== FILE_OVERWRITE
) ||
1836 (create_disposition
== FILE_OVERWRITE_IF
))) {
1837 if (!open_match_attributes(conn
, existing_dos_attributes
,
1839 smb_fname
->st
.st_ex_mode
,
1840 unx_mode
, &new_unx_mode
)) {
1841 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1842 "for file %s (%x %x) (0%o, 0%o)\n",
1843 smb_fname_str_dbg(smb_fname
),
1844 existing_dos_attributes
,
1846 (unsigned int)smb_fname
->st
.st_ex_mode
,
1847 (unsigned int)unx_mode
));
1849 return NT_STATUS_ACCESS_DENIED
;
1853 status
= smbd_calculate_access_mask(conn
, smb_fname
, file_existed
,
1856 if (!NT_STATUS_IS_OK(status
)) {
1857 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1858 "on file %s returned %s\n",
1859 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
1863 open_access_mask
= access_mask
;
1865 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1866 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
1869 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1870 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
1874 * Note that we ignore the append flag as append does not
1875 * mean the same thing under DOS and Unix.
1878 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
1879 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1880 /* DENY_DOS opens are always underlying read-write on the
1881 file handle, no matter what the requested access mask
1883 if ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
1884 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
1894 * Currently we only look at FILE_WRITE_THROUGH for create options.
1898 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
1903 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
1907 if (!posix_open
&& !CAN_WRITE(conn
)) {
1909 * We should really return a permission denied error if either
1910 * O_CREAT or O_TRUNC are set, but for compatibility with
1911 * older versions of Samba we just AND them out.
1913 flags2
&= ~(O_CREAT
|O_TRUNC
);
1917 * Ensure we can't write on a read-only share or file.
1920 if (flags
!= O_RDONLY
&& file_existed
&&
1921 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
1922 DEBUG(5,("open_file_ntcreate: write access requested for "
1923 "file %s on read only %s\n",
1924 smb_fname_str_dbg(smb_fname
),
1925 !CAN_WRITE(conn
) ? "share" : "file" ));
1927 return NT_STATUS_ACCESS_DENIED
;
1930 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1931 fsp
->share_access
= share_access
;
1932 fsp
->fh
->private_options
= private_flags
;
1933 fsp
->access_mask
= open_access_mask
; /* We change this to the
1934 * requested access_mask after
1935 * the open is done. */
1936 fsp
->posix_open
= posix_open
;
1938 /* Ensure no SAMBA_PRIVATE bits can be set. */
1939 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1941 if (timeval_is_zero(&request_time
)) {
1942 request_time
= fsp
->open_time
;
1946 struct byte_range_lock
*br_lck
= NULL
;
1947 struct share_mode_entry
*batch_entry
= NULL
;
1948 struct share_mode_entry
*exclusive_entry
= NULL
;
1949 bool got_level2_oplock
= false;
1950 bool got_a_none_oplock
= false;
1952 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
1953 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1955 if (!acquire_ordered_locks(talloc_tos(),
1963 return NT_STATUS_SHARING_VIOLATION
;
1966 /* Get the types we need to examine. */
1967 find_oplock_types(fsp
,
1973 &got_a_none_oplock
);
1975 /* First pass - send break only on batch oplocks. */
1976 if ((req
!= NULL
) &&
1977 delay_for_batch_oplocks(fsp
,
1981 schedule_defer_open(lck
, request_time
, req
);
1983 return NT_STATUS_SHARING_VIOLATION
;
1986 /* Use the client requested access mask here, not the one we
1988 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
1989 access_mask
, share_access
,
1990 create_options
, &file_existed
);
1992 if (NT_STATUS_IS_OK(status
)) {
1993 /* We might be going to allow this open. Check oplock
1995 /* Second pass - send break for both batch or
1996 * exclusive oplocks. */
1997 if ((req
!= NULL
) &&
1998 delay_for_exclusive_oplocks(
2003 schedule_defer_open(lck
, request_time
, req
);
2005 return NT_STATUS_SHARING_VIOLATION
;
2009 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
2010 /* DELETE_PENDING is not deferred for a second */
2015 grant_fsp_oplock_type(fsp
,
2021 if (!NT_STATUS_IS_OK(status
)) {
2022 uint32 can_access_mask
;
2023 bool can_access
= True
;
2025 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2027 /* Check if this can be done with the deny_dos and fcb
2030 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2031 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2033 DEBUG(0, ("DOS open without an SMB "
2036 return NT_STATUS_INTERNAL_ERROR
;
2039 /* Use the client requested access mask here,
2040 * not the one we open with. */
2041 status
= fcb_or_dos_open(req
,
2052 if (NT_STATUS_IS_OK(status
)) {
2055 *pinfo
= FILE_WAS_OPENED
;
2057 return NT_STATUS_OK
;
2062 * This next line is a subtlety we need for
2063 * MS-Access. If a file open will fail due to share
2064 * permissions and also for security (access) reasons,
2065 * we need to return the access failed error, not the
2066 * share error. We can't open the file due to kernel
2067 * oplock deadlock (it's possible we failed above on
2068 * the open_mode_check()) so use a userspace check.
2071 if (flags
& O_RDWR
) {
2072 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2073 } else if (flags
& O_WRONLY
) {
2074 can_access_mask
= FILE_WRITE_DATA
;
2076 can_access_mask
= FILE_READ_DATA
;
2079 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2080 !CAN_WRITE(conn
)) ||
2081 !can_access_file_data(conn
, smb_fname
,
2087 * If we're returning a share violation, ensure we
2088 * cope with the braindead 1 second delay.
2091 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2092 lp_defer_sharing_violations()) {
2093 struct timeval timeout
;
2094 struct deferred_open_record state
;
2097 /* this is a hack to speed up torture tests
2099 timeout_usecs
= lp_parm_int(SNUM(conn
),
2100 "smbd","sharedelay",
2101 SHARING_VIOLATION_USEC_WAIT
);
2103 /* This is a relative time, added to the absolute
2104 request_time value to get the absolute timeout time.
2105 Note that if this is the second or greater time we enter
2106 this codepath for this particular request mid then
2107 request_time is left as the absolute time of the *first*
2108 time this request mid was processed. This is what allows
2109 the request to eventually time out. */
2111 timeout
= timeval_set(0, timeout_usecs
);
2113 /* Nothing actually uses state.delayed_for_oplocks
2114 but it's handy to differentiate in debug messages
2115 between a 30 second delay due to oplock break, and
2116 a 1 second delay for share mode conflicts. */
2118 state
.delayed_for_oplocks
= False
;
2122 && !request_timed_out(request_time
,
2124 defer_open(lck
, request_time
, timeout
,
2132 * We have detected a sharing violation here
2133 * so return the correct error code
2135 status
= NT_STATUS_SHARING_VIOLATION
;
2137 status
= NT_STATUS_ACCESS_DENIED
;
2143 * We exit this block with the share entry *locked*.....
2147 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
2150 * Ensure we pay attention to default ACLs on directories if required.
2153 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2154 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2155 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2158 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2159 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2160 (unsigned int)flags
, (unsigned int)flags2
,
2161 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2162 (unsigned int)open_access_mask
));
2165 * open_file strips any O_TRUNC flags itself.
2168 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2169 flags
|flags2
, unx_mode
, access_mask
,
2172 if (!NT_STATUS_IS_OK(fsp_open
)) {
2177 if (!file_existed
) {
2178 struct byte_range_lock
*br_lck
= NULL
;
2179 struct share_mode_entry
*batch_entry
= NULL
;
2180 struct share_mode_entry
*exclusive_entry
= NULL
;
2181 bool got_level2_oplock
= false;
2182 bool got_a_none_oplock
= false;
2183 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2185 * Deal with the race condition where two smbd's detect the
2186 * file doesn't exist and do the create at the same time. One
2187 * of them will win and set a share mode, the other (ie. this
2188 * one) should check if the requested share mode for this
2189 * create is allowed.
2193 * Now the file exists and fsp is successfully opened,
2194 * fsp->dev and fsp->inode are valid and should replace the
2195 * dev=0,inode=0 from a non existent file. Spotted by
2196 * Nadav Danieli <nadavd@exanet.com>. JRA.
2201 if (!acquire_ordered_locks(talloc_tos(),
2209 return NT_STATUS_SHARING_VIOLATION
;
2212 /* Get the types we need to examine. */
2213 find_oplock_types(fsp
,
2219 &got_a_none_oplock
);
2221 /* First pass - send break only on batch oplocks. */
2222 if ((req
!= NULL
) &&
2223 delay_for_batch_oplocks(fsp
,
2227 schedule_defer_open(lck
, request_time
, req
);
2230 return NT_STATUS_SHARING_VIOLATION
;
2233 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2234 access_mask
, share_access
,
2235 create_options
, &file_existed
);
2237 if (NT_STATUS_IS_OK(status
)) {
2238 /* We might be going to allow this open. Check oplock
2240 /* Second pass - send break for both batch or
2241 * exclusive oplocks. */
2242 if ((req
!= NULL
) &&
2243 delay_for_exclusive_oplocks(
2248 schedule_defer_open(lck
, request_time
, req
);
2251 return NT_STATUS_SHARING_VIOLATION
;
2255 if (!NT_STATUS_IS_OK(status
)) {
2256 struct deferred_open_record state
;
2258 state
.delayed_for_oplocks
= False
;
2261 /* Do it all over again immediately. In the second
2262 * round we will find that the file existed and handle
2263 * the DELETE_PENDING and FCB cases correctly. No need
2264 * to duplicate the code here. Essentially this is a
2265 * "goto top of this function", but don't tell
2269 defer_open(lck
, request_time
, timeval_zero(),
2277 grant_fsp_oplock_type(fsp
,
2284 * We exit this block with the share entry *locked*.....
2289 SMB_ASSERT(lck
!= NULL
);
2291 /* Delete streams if create_disposition requires it */
2292 if (file_existed
&& clear_ads
&&
2293 !is_ntfs_stream_smb_fname(smb_fname
)) {
2294 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2295 if (!NT_STATUS_IS_OK(status
)) {
2302 /* note that we ignore failure for the following. It is
2303 basically a hack for NFS, and NFS will never set one of
2304 these only read them. Nobody but Samba can ever set a deny
2305 mode and we have already checked our more authoritative
2306 locking database for permission to set this deny mode. If
2307 the kernel refuses the operations then the kernel is wrong.
2308 note that GPFS supports it as well - jmcd */
2310 if (fsp
->fh
->fd
!= -1) {
2312 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2313 if(ret_flock
== -1 ){
2318 return NT_STATUS_SHARING_VIOLATION
;
2323 * At this point onwards, we can guarentee that the share entry
2324 * is locked, whether we created the file or not, and that the
2325 * deny mode is compatible with all current opens.
2329 * If requested, truncate the file.
2332 if (file_existed
&& (flags2
&O_TRUNC
)) {
2334 * We are modifing the file after open - update the stat
2337 if ((SMB_VFS_FTRUNCATE(fsp
, 0) == -1) ||
2338 (SMB_VFS_FSTAT(fsp
, &smb_fname
->st
)==-1)) {
2339 status
= map_nt_error_from_unix(errno
);
2347 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2348 * but we don't have to store this - just ignore it on access check.
2350 if (conn
->sconn
->using_smb2
) {
2352 * SMB2 doesn't return it (according to Microsoft tests).
2353 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2354 * File created with access = 0x7 (Read, Write, Delete)
2355 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2357 fsp
->access_mask
= access_mask
;
2359 /* But SMB1 does. */
2360 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2364 /* stat opens on existing files don't get oplocks. */
2365 if (is_stat_open(open_access_mask
)) {
2366 fsp
->oplock_type
= NO_OPLOCK
;
2369 if (!(flags2
& O_TRUNC
)) {
2370 info
= FILE_WAS_OPENED
;
2372 info
= FILE_WAS_OVERWRITTEN
;
2375 info
= FILE_WAS_CREATED
;
2383 * Setup the oplock info in both the shared memory and
2387 if (!set_file_oplock(fsp
, fsp
->oplock_type
)) {
2389 * Could not get the kernel oplock or there are byte-range
2390 * locks on the file.
2392 fsp
->oplock_type
= NO_OPLOCK
;
2395 if (info
== FILE_WAS_OVERWRITTEN
|| info
== FILE_WAS_CREATED
|| info
== FILE_WAS_SUPERSEDED
) {
2396 new_file_created
= True
;
2399 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2403 /* Handle strange delete on close create semantics. */
2404 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2406 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2408 if (!NT_STATUS_IS_OK(status
)) {
2409 /* Remember to delete the mode we just added. */
2410 del_share_mode(lck
, fsp
);
2415 /* Note that here we set the *inital* delete on close flag,
2416 not the regular one. The magic gets handled in close. */
2417 fsp
->initial_delete_on_close
= True
;
2420 if (new_file_created
) {
2421 /* Files should be initially set as archive */
2422 if (lp_map_archive(SNUM(conn
)) ||
2423 lp_store_dos_attributes(SNUM(conn
))) {
2425 if (file_set_dosmode(conn
, smb_fname
,
2426 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2427 parent_dir
, true) == 0) {
2428 unx_mode
= smb_fname
->st
.st_ex_mode
;
2434 /* Determine sparse flag. */
2436 /* POSIX opens are sparse by default. */
2437 fsp
->is_sparse
= true;
2439 fsp
->is_sparse
= (file_existed
&&
2440 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2444 * Take care of inherited ACLs on created files - if default ACL not
2448 if (!posix_open
&& !file_existed
&& !def_acl
) {
2450 int saved_errno
= errno
; /* We might get ENOSYS in the next
2453 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2455 errno
= saved_errno
; /* Ignore ENOSYS */
2458 } else if (new_unx_mode
) {
2462 /* Attributes need changing. File already existed. */
2465 int saved_errno
= errno
; /* We might get ENOSYS in the
2467 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2469 if (ret
== -1 && errno
== ENOSYS
) {
2470 errno
= saved_errno
; /* Ignore ENOSYS */
2472 DEBUG(5, ("open_file_ntcreate: reset "
2473 "attributes of file %s to 0%o\n",
2474 smb_fname_str_dbg(smb_fname
),
2475 (unsigned int)new_unx_mode
));
2476 ret
= 0; /* Don't do the fchmod below. */
2481 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2482 DEBUG(5, ("open_file_ntcreate: failed to reset "
2483 "attributes of file %s to 0%o\n",
2484 smb_fname_str_dbg(smb_fname
),
2485 (unsigned int)new_unx_mode
));
2488 /* If this is a successful open, we must remove any deferred open
2491 del_deferred_open_entry(lck
, req
->mid
,
2492 sconn_server_id(req
->sconn
));
2496 return NT_STATUS_OK
;
2500 /****************************************************************************
2501 Open a file for for write to ensure that we can fchmod it.
2502 ****************************************************************************/
2504 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2505 struct smb_filename
*smb_fname
,
2506 files_struct
**result
)
2508 if (!VALID_STAT(smb_fname
->st
)) {
2509 return NT_STATUS_INVALID_PARAMETER
;
2512 return SMB_VFS_CREATE_FILE(
2515 0, /* root_dir_fid */
2516 smb_fname
, /* fname */
2517 FILE_WRITE_DATA
, /* access_mask */
2518 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2520 FILE_OPEN
, /* create_disposition*/
2521 0, /* create_options */
2522 0, /* file_attributes */
2523 INTERNAL_OPEN_ONLY
, /* oplock_request */
2524 0, /* allocation_size */
2525 0, /* private_flags */
2528 result
, /* result */
2532 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2533 struct smb_filename
*smb_dname
,
2534 uint32 file_attributes
)
2537 char *parent_dir
= NULL
;
2539 bool posix_open
= false;
2540 bool need_re_stat
= false;
2541 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2543 if(access_mask
& ~(conn
->share_access
)) {
2544 DEBUG(5,("mkdir_internal: failing share access "
2545 "%s\n", lp_servicename(SNUM(conn
))));
2546 return NT_STATUS_ACCESS_DENIED
;
2549 status
= check_name(conn
, smb_dname
->base_name
);
2550 if (!NT_STATUS_IS_OK(status
)) {
2554 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2556 return NT_STATUS_NO_MEMORY
;
2559 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2561 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2563 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2566 status
= check_parent_access(conn
,
2567 smb_dname
->base_name
,
2570 if(!NT_STATUS_IS_OK(status
)) {
2571 DEBUG(5,("mkdir_internal: check_parent_access "
2572 "on directory %s for path %s returned %s\n",
2574 smb_dname
->base_name
,
2575 nt_errstr(status
) ));
2579 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2580 return map_nt_error_from_unix(errno
);
2583 /* Ensure we're checking for a symlink here.... */
2584 /* We don't want to get caught by a symlink racer. */
2586 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2587 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2588 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2589 return map_nt_error_from_unix(errno
);
2592 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2593 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2594 smb_fname_str_dbg(smb_dname
)));
2595 return NT_STATUS_NOT_A_DIRECTORY
;
2598 if (lp_store_dos_attributes(SNUM(conn
))) {
2600 file_set_dosmode(conn
, smb_dname
,
2601 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2606 if (lp_inherit_perms(SNUM(conn
))) {
2607 inherit_access_posix_acl(conn
, parent_dir
,
2608 smb_dname
->base_name
, mode
);
2609 need_re_stat
= true;
2614 * Check if high bits should have been set,
2615 * then (if bits are missing): add them.
2616 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2619 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2620 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2621 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2622 (smb_dname
->st
.st_ex_mode
|
2623 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2624 need_re_stat
= true;
2628 /* Change the owner if required. */
2629 if (lp_inherit_owner(SNUM(conn
))) {
2630 change_dir_owner_to_parent(conn
, parent_dir
,
2631 smb_dname
->base_name
,
2633 need_re_stat
= true;
2637 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2638 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2639 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2640 return map_nt_error_from_unix(errno
);
2644 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2645 smb_dname
->base_name
);
2647 return NT_STATUS_OK
;
2650 /****************************************************************************
2651 Ensure we didn't get symlink raced on opening a directory.
2652 ****************************************************************************/
2654 bool check_same_stat(const SMB_STRUCT_STAT
*sbuf1
,
2655 const SMB_STRUCT_STAT
*sbuf2
)
2657 if (sbuf1
->st_ex_uid
!= sbuf2
->st_ex_uid
||
2658 sbuf1
->st_ex_gid
!= sbuf2
->st_ex_gid
||
2659 sbuf1
->st_ex_dev
!= sbuf2
->st_ex_dev
||
2660 sbuf1
->st_ex_ino
!= sbuf2
->st_ex_ino
) {
2666 /****************************************************************************
2667 Open a directory from an NT SMB call.
2668 ****************************************************************************/
2670 static NTSTATUS
open_directory(connection_struct
*conn
,
2671 struct smb_request
*req
,
2672 struct smb_filename
*smb_dname
,
2674 uint32 share_access
,
2675 uint32 create_disposition
,
2676 uint32 create_options
,
2677 uint32 file_attributes
,
2679 files_struct
**result
)
2681 files_struct
*fsp
= NULL
;
2682 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2683 struct share_mode_lock
*lck
= NULL
;
2685 struct timespec mtimespec
;
2688 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
2690 /* Ensure we have a directory attribute. */
2691 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2693 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2694 "share_access = 0x%x create_options = 0x%x, "
2695 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2696 smb_fname_str_dbg(smb_dname
),
2697 (unsigned int)access_mask
,
2698 (unsigned int)share_access
,
2699 (unsigned int)create_options
,
2700 (unsigned int)create_disposition
,
2701 (unsigned int)file_attributes
));
2703 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
2704 (conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
2705 is_ntfs_stream_smb_fname(smb_dname
)) {
2706 DEBUG(2, ("open_directory: %s is a stream name!\n",
2707 smb_fname_str_dbg(smb_dname
)));
2708 return NT_STATUS_NOT_A_DIRECTORY
;
2711 status
= smbd_calculate_access_mask(conn
, smb_dname
, dir_existed
,
2712 access_mask
, &access_mask
);
2713 if (!NT_STATUS_IS_OK(status
)) {
2714 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2715 "on file %s returned %s\n",
2716 smb_fname_str_dbg(smb_dname
),
2717 nt_errstr(status
)));
2721 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2722 !security_token_has_privilege(get_current_nttok(conn
),
2723 SEC_PRIV_SECURITY
)) {
2724 DEBUG(10, ("open_directory: open on %s "
2725 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2726 smb_fname_str_dbg(smb_dname
)));
2727 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2730 switch( create_disposition
) {
2734 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2737 info
= FILE_WAS_OPENED
;
2742 /* If directory exists error. If directory doesn't
2746 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
2747 DEBUG(2, ("open_directory: unable to create "
2748 "%s. Error was %s\n",
2749 smb_fname_str_dbg(smb_dname
),
2750 nt_errstr(status
)));
2754 status
= mkdir_internal(conn
, smb_dname
,
2757 if (!NT_STATUS_IS_OK(status
)) {
2758 DEBUG(2, ("open_directory: unable to create "
2759 "%s. Error was %s\n",
2760 smb_fname_str_dbg(smb_dname
),
2761 nt_errstr(status
)));
2765 info
= FILE_WAS_CREATED
;
2770 * If directory exists open. If directory doesn't
2775 status
= NT_STATUS_OK
;
2776 info
= FILE_WAS_OPENED
;
2778 status
= mkdir_internal(conn
, smb_dname
,
2781 if (NT_STATUS_IS_OK(status
)) {
2782 info
= FILE_WAS_CREATED
;
2784 /* Cope with create race. */
2785 if (!NT_STATUS_EQUAL(status
,
2786 NT_STATUS_OBJECT_NAME_COLLISION
)) {
2787 DEBUG(2, ("open_directory: unable to create "
2788 "%s. Error was %s\n",
2789 smb_fname_str_dbg(smb_dname
),
2790 nt_errstr(status
)));
2793 info
= FILE_WAS_OPENED
;
2799 case FILE_SUPERSEDE
:
2800 case FILE_OVERWRITE
:
2801 case FILE_OVERWRITE_IF
:
2803 DEBUG(5,("open_directory: invalid create_disposition "
2804 "0x%x for directory %s\n",
2805 (unsigned int)create_disposition
,
2806 smb_fname_str_dbg(smb_dname
)));
2807 return NT_STATUS_INVALID_PARAMETER
;
2810 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2811 DEBUG(5,("open_directory: %s is not a directory !\n",
2812 smb_fname_str_dbg(smb_dname
)));
2813 return NT_STATUS_NOT_A_DIRECTORY
;
2816 if (info
== FILE_WAS_OPENED
) {
2817 status
= smbd_check_open_rights(conn
, smb_dname
, access_mask
);
2818 if (!NT_STATUS_IS_OK(status
)) {
2819 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2820 "file %s failed with %s\n",
2821 smb_fname_str_dbg(smb_dname
),
2822 nt_errstr(status
)));
2827 status
= file_new(req
, conn
, &fsp
);
2828 if(!NT_STATUS_IS_OK(status
)) {
2833 * Setup the files_struct for it.
2836 fsp
->mode
= smb_dname
->st
.st_ex_mode
;
2837 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
2838 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
2839 fsp
->file_pid
= req
? req
->smbpid
: 0;
2840 fsp
->can_lock
= False
;
2841 fsp
->can_read
= False
;
2842 fsp
->can_write
= False
;
2844 fsp
->share_access
= share_access
;
2845 fsp
->fh
->private_options
= 0;
2847 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2849 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2850 fsp
->print_file
= NULL
;
2851 fsp
->modified
= False
;
2852 fsp
->oplock_type
= NO_OPLOCK
;
2853 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
2854 fsp
->is_directory
= True
;
2855 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
2856 status
= fsp_set_smb_fname(fsp
, smb_dname
);
2857 if (!NT_STATUS_IS_OK(status
)) {
2858 file_free(req
, fsp
);
2862 mtimespec
= smb_dname
->st
.st_ex_mtime
;
2865 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
2867 /* POSIX allows us to open a directory with O_RDONLY. */
2868 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
2870 if (!NT_STATUS_IS_OK(status
)) {
2871 DEBUG(5, ("open_directory: Could not open fd for "
2873 smb_fname_str_dbg(smb_dname
),
2874 nt_errstr(status
)));
2875 file_free(req
, fsp
);
2879 status
= vfs_stat_fsp(fsp
);
2880 if (!NT_STATUS_IS_OK(status
)) {
2882 file_free(req
, fsp
);
2886 /* Ensure there was no race condition. */
2887 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
2888 DEBUG(5,("open_directory: stat struct differs for "
2890 smb_fname_str_dbg(smb_dname
)));
2892 file_free(req
, fsp
);
2893 return NT_STATUS_ACCESS_DENIED
;
2896 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
2897 conn
->connectpath
, smb_dname
, &mtimespec
);
2900 DEBUG(0, ("open_directory: Could not get share mode lock for "
2901 "%s\n", smb_fname_str_dbg(smb_dname
)));
2903 file_free(req
, fsp
);
2904 return NT_STATUS_SHARING_VIOLATION
;
2907 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2908 access_mask
, share_access
,
2909 create_options
, &dir_existed
);
2911 if (!NT_STATUS_IS_OK(status
)) {
2914 file_free(req
, fsp
);
2918 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2919 req
? req
->mid
: 0, NO_OPLOCK
);
2921 /* For directories the delete on close bit at open time seems
2922 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2923 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2924 status
= can_set_delete_on_close(fsp
, 0);
2925 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
2928 file_free(req
, fsp
);
2932 if (NT_STATUS_IS_OK(status
)) {
2933 /* Note that here we set the *inital* delete on close flag,
2934 not the regular one. The magic gets handled in close. */
2935 fsp
->initial_delete_on_close
= True
;
2946 return NT_STATUS_OK
;
2949 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
2950 struct smb_filename
*smb_dname
)
2955 status
= SMB_VFS_CREATE_FILE(
2958 0, /* root_dir_fid */
2959 smb_dname
, /* fname */
2960 FILE_READ_ATTRIBUTES
, /* access_mask */
2961 FILE_SHARE_NONE
, /* share_access */
2962 FILE_CREATE
, /* create_disposition*/
2963 FILE_DIRECTORY_FILE
, /* create_options */
2964 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
2965 0, /* oplock_request */
2966 0, /* allocation_size */
2967 0, /* private_flags */
2973 if (NT_STATUS_IS_OK(status
)) {
2974 close_file(req
, fsp
, NORMAL_CLOSE
);
2980 /****************************************************************************
2981 Receive notification that one of our open files has been renamed by another
2983 ****************************************************************************/
2985 void msg_file_was_renamed(struct messaging_context
*msg
,
2988 struct server_id server_id
,
2991 struct smbd_server_connection
*sconn
;
2993 char *frm
= (char *)data
->data
;
2995 const char *sharepath
;
2996 const char *base_name
;
2997 const char *stream_name
;
2998 struct smb_filename
*smb_fname
= NULL
;
2999 size_t sp_len
, bn_len
;
3002 sconn
= msg_ctx_to_sconn(msg
);
3003 if (sconn
== NULL
) {
3004 DEBUG(1, ("could not find sconn\n"));
3008 if (data
->data
== NULL
3009 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3010 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3011 (int)data
->length
));
3015 /* Unpack the message. */
3016 pull_file_id_24(frm
, &id
);
3017 sharepath
= &frm
[24];
3018 sp_len
= strlen(sharepath
);
3019 base_name
= sharepath
+ sp_len
+ 1;
3020 bn_len
= strlen(base_name
);
3021 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3023 /* stream_name must always be NULL if there is no stream. */
3024 if (stream_name
[0] == '\0') {
3028 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
3029 stream_name
, NULL
, &smb_fname
);
3030 if (!NT_STATUS_IS_OK(status
)) {
3034 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3036 sharepath
, smb_fname_str_dbg(smb_fname
),
3037 file_id_string_tos(&id
)));
3039 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3040 fsp
= file_find_di_next(fsp
)) {
3041 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3043 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3044 fsp
->fnum
, fsp_str_dbg(fsp
),
3045 smb_fname_str_dbg(smb_fname
)));
3046 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3047 if (!NT_STATUS_IS_OK(status
)) {
3052 /* Now we have the complete path we can work out if this is
3053 actually within this share and adjust newname accordingly. */
3054 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3055 "not sharepath %s) "
3056 "fnum %d from %s -> %s\n",
3057 fsp
->conn
->connectpath
,
3061 smb_fname_str_dbg(smb_fname
)));
3065 TALLOC_FREE(smb_fname
);
3070 * If a main file is opened for delete, all streams need to be checked for
3071 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3072 * If that works, delete them all by setting the delete on close and close.
3075 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3078 struct stream_struct
*stream_info
= NULL
;
3079 files_struct
**streams
= NULL
;
3081 unsigned int num_streams
= 0;
3082 TALLOC_CTX
*frame
= talloc_stackframe();
3085 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3086 &num_streams
, &stream_info
);
3088 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3089 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3090 DEBUG(10, ("no streams around\n"));
3092 return NT_STATUS_OK
;
3095 if (!NT_STATUS_IS_OK(status
)) {
3096 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3097 nt_errstr(status
)));
3101 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3104 if (num_streams
== 0) {
3106 return NT_STATUS_OK
;
3109 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3110 if (streams
== NULL
) {
3111 DEBUG(0, ("talloc failed\n"));
3112 status
= NT_STATUS_NO_MEMORY
;
3116 for (i
=0; i
<num_streams
; i
++) {
3117 struct smb_filename
*smb_fname
= NULL
;
3119 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3124 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3125 stream_info
[i
].name
,
3127 if (!NT_STATUS_IS_OK(status
)) {
3131 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3132 DEBUG(10, ("Unable to stat stream: %s\n",
3133 smb_fname_str_dbg(smb_fname
)));
3136 status
= SMB_VFS_CREATE_FILE(
3139 0, /* root_dir_fid */
3140 smb_fname
, /* fname */
3141 DELETE_ACCESS
, /* access_mask */
3142 (FILE_SHARE_READ
| /* share_access */
3143 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3144 FILE_OPEN
, /* create_disposition*/
3145 0, /* create_options */
3146 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3147 0, /* oplock_request */
3148 0, /* allocation_size */
3149 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3152 &streams
[i
], /* result */
3155 if (!NT_STATUS_IS_OK(status
)) {
3156 DEBUG(10, ("Could not open stream %s: %s\n",
3157 smb_fname_str_dbg(smb_fname
),
3158 nt_errstr(status
)));
3160 TALLOC_FREE(smb_fname
);
3163 TALLOC_FREE(smb_fname
);
3167 * don't touch the variable "status" beyond this point :-)
3170 for (i
-= 1 ; i
>= 0; i
--) {
3171 if (streams
[i
] == NULL
) {
3175 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3176 fsp_str_dbg(streams
[i
])));
3177 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3186 * Wrapper around open_file_ntcreate and open_directory
3189 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3190 struct smb_request
*req
,
3191 struct smb_filename
*smb_fname
,
3192 uint32_t access_mask
,
3193 uint32_t share_access
,
3194 uint32_t create_disposition
,
3195 uint32_t create_options
,
3196 uint32_t file_attributes
,
3197 uint32_t oplock_request
,
3198 uint64_t allocation_size
,
3199 uint32_t private_flags
,
3200 struct security_descriptor
*sd
,
3201 struct ea_list
*ea_list
,
3203 files_struct
**result
,
3206 int info
= FILE_WAS_OPENED
;
3207 files_struct
*base_fsp
= NULL
;
3208 files_struct
*fsp
= NULL
;
3211 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3212 "file_attributes = 0x%x, share_access = 0x%x, "
3213 "create_disposition = 0x%x create_options = 0x%x "
3214 "oplock_request = 0x%x private_flags = 0x%x "
3215 "ea_list = 0x%p, sd = 0x%p, "
3217 (unsigned int)access_mask
,
3218 (unsigned int)file_attributes
,
3219 (unsigned int)share_access
,
3220 (unsigned int)create_disposition
,
3221 (unsigned int)create_options
,
3222 (unsigned int)oplock_request
,
3223 (unsigned int)private_flags
,
3224 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3226 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3227 status
= NT_STATUS_NOT_SUPPORTED
;
3231 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3232 status
= NT_STATUS_INVALID_PARAMETER
;
3237 oplock_request
|= INTERNAL_OPEN_ONLY
;
3240 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3241 && (access_mask
& DELETE_ACCESS
)
3242 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3244 * We can't open a file with DELETE access if any of the
3245 * streams is open without FILE_SHARE_DELETE
3247 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3249 if (!NT_STATUS_IS_OK(status
)) {
3254 /* This is the correct thing to do (check every time) but can_delete
3255 * is expensive (it may have to read the parent directory
3256 * permissions). So for now we're not doing it unless we have a strong
3257 * hint the client is really going to delete this file. If the client
3258 * is forcing FILE_CREATE let the filesystem take care of the
3261 /* Setting FILE_SHARE_DELETE is the hint. */
3263 if ((create_disposition
!= FILE_CREATE
)
3264 && (access_mask
& DELETE_ACCESS
)
3265 && (!(can_delete_file_in_directory(conn
, smb_fname
) ||
3266 can_access_file_acl(conn
, smb_fname
, DELETE_ACCESS
)))) {
3267 status
= NT_STATUS_ACCESS_DENIED
;
3268 DEBUG(10,("create_file_unixpath: open file %s "
3269 "for delete ACCESS_DENIED\n",
3270 smb_fname_str_dbg(smb_fname
)));
3274 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3275 !security_token_has_privilege(get_current_nttok(conn
),
3276 SEC_PRIV_SECURITY
)) {
3277 DEBUG(10, ("create_file_unixpath: open on %s "
3278 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3279 smb_fname_str_dbg(smb_fname
)));
3280 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3284 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3285 && is_ntfs_stream_smb_fname(smb_fname
)
3286 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3287 uint32 base_create_disposition
;
3288 struct smb_filename
*smb_fname_base
= NULL
;
3290 if (create_options
& FILE_DIRECTORY_FILE
) {
3291 status
= NT_STATUS_NOT_A_DIRECTORY
;
3295 switch (create_disposition
) {
3297 base_create_disposition
= FILE_OPEN
;
3300 base_create_disposition
= FILE_OPEN_IF
;
3304 /* Create an smb_filename with stream_name == NULL. */
3305 status
= create_synthetic_smb_fname(talloc_tos(),
3306 smb_fname
->base_name
,
3309 if (!NT_STATUS_IS_OK(status
)) {
3313 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3314 DEBUG(10, ("Unable to stat stream: %s\n",
3315 smb_fname_str_dbg(smb_fname_base
)));
3318 /* Open the base file. */
3319 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3322 | FILE_SHARE_DELETE
,
3323 base_create_disposition
,
3324 0, 0, 0, 0, 0, NULL
, NULL
,
3326 TALLOC_FREE(smb_fname_base
);
3328 if (!NT_STATUS_IS_OK(status
)) {
3329 DEBUG(10, ("create_file_unixpath for base %s failed: "
3330 "%s\n", smb_fname
->base_name
,
3331 nt_errstr(status
)));
3334 /* we don't need to low level fd */
3339 * If it's a request for a directory open, deal with it separately.
3342 if (create_options
& FILE_DIRECTORY_FILE
) {
3344 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3345 status
= NT_STATUS_INVALID_PARAMETER
;
3349 /* Can't open a temp directory. IFS kit test. */
3350 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3351 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3352 status
= NT_STATUS_INVALID_PARAMETER
;
3357 * We will get a create directory here if the Win32
3358 * app specified a security descriptor in the
3359 * CreateDirectory() call.
3363 status
= open_directory(
3364 conn
, req
, smb_fname
, access_mask
, share_access
,
3365 create_disposition
, create_options
, file_attributes
,
3370 * Ordinary file case.
3373 status
= file_new(req
, conn
, &fsp
);
3374 if(!NT_STATUS_IS_OK(status
)) {
3378 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3379 if (!NT_STATUS_IS_OK(status
)) {
3384 * We're opening the stream element of a base_fsp
3385 * we already opened. Set up the base_fsp pointer.
3388 fsp
->base_fsp
= base_fsp
;
3391 status
= open_file_ntcreate(conn
,
3403 if(!NT_STATUS_IS_OK(status
)) {
3404 file_free(req
, fsp
);
3408 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3410 /* A stream open never opens a directory */
3413 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3418 * Fail the open if it was explicitly a non-directory
3422 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3423 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3428 status
= open_directory(
3429 conn
, req
, smb_fname
, access_mask
,
3430 share_access
, create_disposition
,
3431 create_options
, file_attributes
,
3436 if (!NT_STATUS_IS_OK(status
)) {
3440 fsp
->base_fsp
= base_fsp
;
3443 * According to the MS documentation, the only time the security
3444 * descriptor is applied to the opened file is iff we *created* the
3445 * file; an existing file stays the same.
3447 * Also, it seems (from observation) that you can open the file with
3448 * any access mask but you can still write the sd. We need to override
3449 * the granted access before we call set_sd
3450 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3453 if ((sd
!= NULL
) && (info
== FILE_WAS_CREATED
)
3454 && lp_nt_acl_support(SNUM(conn
))) {
3456 uint32_t sec_info_sent
;
3457 uint32_t saved_access_mask
= fsp
->access_mask
;
3459 sec_info_sent
= get_sec_info(sd
);
3461 fsp
->access_mask
= FILE_GENERIC_ALL
;
3463 /* Convert all the generic bits. */
3464 security_acl_map_generic(sd
->dacl
, &file_generic_mapping
);
3465 security_acl_map_generic(sd
->sacl
, &file_generic_mapping
);
3467 if (sec_info_sent
& (SECINFO_OWNER
|
3471 status
= SMB_VFS_FSET_NT_ACL(fsp
, sec_info_sent
, sd
);
3474 fsp
->access_mask
= saved_access_mask
;
3476 if (!NT_STATUS_IS_OK(status
)) {
3481 if ((ea_list
!= NULL
) &&
3482 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3483 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3484 if (!NT_STATUS_IS_OK(status
)) {
3489 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3490 status
= NT_STATUS_ACCESS_DENIED
;
3494 /* Save the requested allocation size. */
3495 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3497 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3498 fsp
->initial_allocation_size
= smb_roundup(
3499 fsp
->conn
, allocation_size
);
3500 if (fsp
->is_directory
) {
3501 /* Can't set allocation size on a directory. */
3502 status
= NT_STATUS_ACCESS_DENIED
;
3505 if (vfs_allocate_file_space(
3506 fsp
, fsp
->initial_allocation_size
) == -1) {
3507 status
= NT_STATUS_DISK_FULL
;
3511 fsp
->initial_allocation_size
= smb_roundup(
3512 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3516 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3519 if (pinfo
!= NULL
) {
3523 smb_fname
->st
= fsp
->fsp_name
->st
;
3525 return NT_STATUS_OK
;
3528 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3531 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3533 * The close_file below will close
3538 close_file(req
, fsp
, ERROR_CLOSE
);
3541 if (base_fsp
!= NULL
) {
3542 close_file(req
, base_fsp
, ERROR_CLOSE
);
3549 * Calculate the full path name given a relative fid.
3551 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
3552 struct smb_request
*req
,
3553 uint16_t root_dir_fid
,
3554 const struct smb_filename
*smb_fname
,
3555 struct smb_filename
**smb_fname_out
)
3557 files_struct
*dir_fsp
;
3558 char *parent_fname
= NULL
;
3559 char *new_base_name
= NULL
;
3562 if (root_dir_fid
== 0 || !smb_fname
) {
3563 status
= NT_STATUS_INTERNAL_ERROR
;
3567 dir_fsp
= file_fsp(req
, root_dir_fid
);
3569 if (dir_fsp
== NULL
) {
3570 status
= NT_STATUS_INVALID_HANDLE
;
3574 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
3575 status
= NT_STATUS_INVALID_HANDLE
;
3579 if (!dir_fsp
->is_directory
) {
3582 * Check to see if this is a mac fork of some kind.
3585 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
3586 is_ntfs_stream_smb_fname(smb_fname
)) {
3587 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3592 we need to handle the case when we get a
3593 relative open relative to a file and the
3594 pathname is blank - this is a reopen!
3595 (hint from demyn plantenberg)
3598 status
= NT_STATUS_INVALID_HANDLE
;
3602 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
3604 * We're at the toplevel dir, the final file name
3605 * must not contain ./, as this is filtered out
3606 * normally by srvstr_get_path and unix_convert
3607 * explicitly rejects paths containing ./.
3609 parent_fname
= talloc_strdup(talloc_tos(), "");
3610 if (parent_fname
== NULL
) {
3611 status
= NT_STATUS_NO_MEMORY
;
3615 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
3618 * Copy in the base directory name.
3621 parent_fname
= talloc_array(talloc_tos(), char,
3623 if (parent_fname
== NULL
) {
3624 status
= NT_STATUS_NO_MEMORY
;
3627 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
3631 * Ensure it ends in a '/'.
3632 * We used TALLOC_SIZE +2 to add space for the '/'.
3636 && (parent_fname
[dir_name_len
-1] != '\\')
3637 && (parent_fname
[dir_name_len
-1] != '/')) {
3638 parent_fname
[dir_name_len
] = '/';
3639 parent_fname
[dir_name_len
+1] = '\0';
3643 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
3644 smb_fname
->base_name
);
3645 if (new_base_name
== NULL
) {
3646 status
= NT_STATUS_NO_MEMORY
;
3650 status
= filename_convert(req
,
3652 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
3657 if (!NT_STATUS_IS_OK(status
)) {
3662 TALLOC_FREE(parent_fname
);
3663 TALLOC_FREE(new_base_name
);
3667 NTSTATUS
create_file_default(connection_struct
*conn
,
3668 struct smb_request
*req
,
3669 uint16_t root_dir_fid
,
3670 struct smb_filename
*smb_fname
,
3671 uint32_t access_mask
,
3672 uint32_t share_access
,
3673 uint32_t create_disposition
,
3674 uint32_t create_options
,
3675 uint32_t file_attributes
,
3676 uint32_t oplock_request
,
3677 uint64_t allocation_size
,
3678 uint32_t private_flags
,
3679 struct security_descriptor
*sd
,
3680 struct ea_list
*ea_list
,
3681 files_struct
**result
,
3684 int info
= FILE_WAS_OPENED
;
3685 files_struct
*fsp
= NULL
;
3687 bool stream_name
= false;
3689 DEBUG(10,("create_file: access_mask = 0x%x "
3690 "file_attributes = 0x%x, share_access = 0x%x, "
3691 "create_disposition = 0x%x create_options = 0x%x "
3692 "oplock_request = 0x%x "
3693 "private_flags = 0x%x "
3694 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3696 (unsigned int)access_mask
,
3697 (unsigned int)file_attributes
,
3698 (unsigned int)share_access
,
3699 (unsigned int)create_disposition
,
3700 (unsigned int)create_options
,
3701 (unsigned int)oplock_request
,
3702 (unsigned int)private_flags
,
3703 (unsigned int)root_dir_fid
,
3704 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3707 * Calculate the filename from the root_dir_if if necessary.
3710 if (root_dir_fid
!= 0) {
3711 struct smb_filename
*smb_fname_out
= NULL
;
3712 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
3713 smb_fname
, &smb_fname_out
);
3714 if (!NT_STATUS_IS_OK(status
)) {
3717 smb_fname
= smb_fname_out
;
3721 * Check to see if this is a mac fork of some kind.
3724 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
3726 enum FAKE_FILE_TYPE fake_file_type
;
3728 fake_file_type
= is_fake_file(smb_fname
);
3730 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
3733 * Here we go! support for changing the disk quotas
3736 * We need to fake up to open this MAGIC QUOTA file
3737 * and return a valid FID.
3739 * w2k close this file directly after openening xp
3740 * also tries a QUERY_FILE_INFO on the file and then
3743 status
= open_fake_file(req
, conn
, req
->vuid
,
3744 fake_file_type
, smb_fname
,
3746 if (!NT_STATUS_IS_OK(status
)) {
3750 ZERO_STRUCT(smb_fname
->st
);
3754 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
3755 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3760 /* All file access must go through check_name() */
3762 status
= check_name(conn
, smb_fname
->base_name
);
3763 if (!NT_STATUS_IS_OK(status
)) {
3767 if (stream_name
&& is_ntfs_default_stream_smb_fname(smb_fname
)) {
3769 smb_fname
->stream_name
= NULL
;
3770 /* We have to handle this error here. */
3771 if (create_options
& FILE_DIRECTORY_FILE
) {
3772 status
= NT_STATUS_NOT_A_DIRECTORY
;
3775 if (lp_posix_pathnames()) {
3776 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
3778 ret
= SMB_VFS_STAT(conn
, smb_fname
);
3781 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
3782 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3787 status
= create_file_unixpath(
3788 conn
, req
, smb_fname
, access_mask
, share_access
,
3789 create_disposition
, create_options
, file_attributes
,
3790 oplock_request
, allocation_size
, private_flags
,
3794 if (!NT_STATUS_IS_OK(status
)) {
3799 DEBUG(10, ("create_file: info=%d\n", info
));
3802 if (pinfo
!= NULL
) {
3805 return NT_STATUS_OK
;
3808 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
3811 close_file(req
, fsp
, ERROR_CLOSE
);