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 static 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 return se_access_check(sd
,
54 (access_desired
& ~FILE_READ_ATTRIBUTES
),
58 /****************************************************************************
59 If the requester wanted DELETE_ACCESS and was rejected because
60 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
62 ****************************************************************************/
64 static bool parent_override_delete(connection_struct
*conn
,
65 struct smb_filename
*smb_fname
,
67 uint32_t rejected_mask
)
69 if ((access_mask
& DELETE_ACCESS
) &&
70 (rejected_mask
& DELETE_ACCESS
) &&
71 can_delete_file_in_directory(conn
, smb_fname
)) {
77 /****************************************************************************
78 Check if we have open rights.
79 ****************************************************************************/
81 static NTSTATUS
smbd_check_open_rights(struct connection_struct
*conn
,
82 struct smb_filename
*smb_fname
,
85 /* Check if we have rights to open. */
87 struct security_descriptor
*sd
= NULL
;
88 uint32_t rejected_share_access
;
89 uint32_t rejected_mask
= 0;
91 rejected_share_access
= access_mask
& ~(conn
->share_access
);
93 if (rejected_share_access
) {
94 DEBUG(10, ("smbd_check_open_rights: rejected share access 0x%x "
96 (unsigned int)access_mask
,
97 smb_fname_str_dbg(smb_fname
),
98 (unsigned int)rejected_share_access
));
99 return NT_STATUS_ACCESS_DENIED
;
102 if (get_current_uid(conn
) == (uid_t
)0) {
103 /* I'm sorry sir, I didn't know you were root... */
104 DEBUG(10,("smbd_check_open_rights: root override "
105 "on %s. Granting 0x%x\n",
106 smb_fname_str_dbg(smb_fname
),
107 (unsigned int)access_mask
));
111 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
112 DEBUG(10,("smbd_check_open_rights: not checking ACL "
113 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
114 smb_fname_str_dbg(smb_fname
),
115 (unsigned int)access_mask
));
119 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
124 if (!NT_STATUS_IS_OK(status
)) {
125 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
127 smb_fname_str_dbg(smb_fname
),
132 status
= smb1_file_se_access_check(conn
,
134 get_current_nttok(conn
),
138 DEBUG(10,("smbd_check_open_rights: file %s requesting "
139 "0x%x returning 0x%x (%s)\n",
140 smb_fname_str_dbg(smb_fname
),
141 (unsigned int)access_mask
,
142 (unsigned int)rejected_mask
,
143 nt_errstr(status
) ));
145 if (!NT_STATUS_IS_OK(status
)) {
146 if (DEBUGLEVEL
>= 10) {
147 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
148 smb_fname_str_dbg(smb_fname
) ));
149 NDR_PRINT_DEBUG(security_descriptor
, sd
);
155 if (NT_STATUS_IS_OK(status
) ||
156 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
160 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
161 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
162 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
163 (lp_map_readonly(SNUM(conn
)) ||
164 lp_map_archive(SNUM(conn
)) ||
165 lp_map_hidden(SNUM(conn
)) ||
166 lp_map_system(SNUM(conn
)))) {
167 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
169 DEBUG(10,("smbd_check_open_rights: "
171 "FILE_WRITE_ATTRIBUTES "
173 smb_fname_str_dbg(smb_fname
)));
176 if (parent_override_delete(conn
,
180 /* Were we trying to do an open
181 * for delete and didn't get DELETE
182 * access (only) ? Check if the
183 * directory allows DELETE_CHILD.
185 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
188 rejected_mask
&= ~DELETE_ACCESS
;
190 DEBUG(10,("smbd_check_open_rights: "
194 smb_fname_str_dbg(smb_fname
)));
197 if (rejected_mask
!= 0) {
198 return NT_STATUS_ACCESS_DENIED
;
204 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
205 struct smb_filename
*smb_fname
,
206 uint32_t access_mask
,
207 char **pp_parent_dir
)
210 char *parent_dir
= NULL
;
211 struct security_descriptor
*parent_sd
= NULL
;
212 uint32_t access_granted
= 0;
214 if (!parent_dirname(talloc_tos(),
215 smb_fname
->base_name
,
218 return NT_STATUS_NO_MEMORY
;
222 *pp_parent_dir
= parent_dir
;
225 if (get_current_uid(conn
) == (uid_t
)0) {
226 /* I'm sorry sir, I didn't know you were root... */
227 DEBUG(10,("check_parent_access: root override "
228 "on %s. Granting 0x%x\n",
229 smb_fname_str_dbg(smb_fname
),
230 (unsigned int)access_mask
));
234 status
= SMB_VFS_GET_NT_ACL(conn
,
239 if (!NT_STATUS_IS_OK(status
)) {
240 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
241 "%s with error %s\n",
247 status
= smb1_file_se_access_check(conn
,
249 get_current_nttok(conn
),
252 if(!NT_STATUS_IS_OK(status
)) {
253 DEBUG(5,("check_parent_access: access check "
254 "on directory %s for "
255 "path %s for mask 0x%x returned (0x%x) %s\n",
257 smb_fname
->base_name
,
260 nt_errstr(status
) ));
267 /****************************************************************************
268 fd support routines - attempt to do a dos_open.
269 ****************************************************************************/
271 static NTSTATUS
fd_open(struct connection_struct
*conn
,
276 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
277 NTSTATUS status
= NT_STATUS_OK
;
281 * Never follow symlinks on a POSIX client. The
282 * client should be doing this.
285 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
290 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
291 if (fsp
->fh
->fd
== -1) {
292 status
= map_nt_error_from_unix(errno
);
293 if (errno
== EMFILE
) {
294 static time_t last_warned
= 0L;
296 if (time((time_t *) NULL
) > last_warned
) {
297 DEBUG(0,("Too many open files, unable "
298 "to open more! smbd's max "
300 lp_max_open_files()));
301 last_warned
= time((time_t *) NULL
);
307 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
308 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
309 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
314 /****************************************************************************
315 Close the file associated with a fsp.
316 ****************************************************************************/
318 NTSTATUS
fd_close(files_struct
*fsp
)
325 if (fsp
->fh
->fd
== -1) {
326 return NT_STATUS_OK
; /* What we used to call a stat open. */
328 if (fsp
->fh
->ref_count
> 1) {
329 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
332 ret
= SMB_VFS_CLOSE(fsp
);
335 return map_nt_error_from_unix(errno
);
340 /****************************************************************************
341 Change the ownership of a file to that of the parent directory.
342 Do this by fd if possible.
343 ****************************************************************************/
345 void change_file_owner_to_parent(connection_struct
*conn
,
346 const char *inherit_from_dir
,
349 struct smb_filename
*smb_fname_parent
= NULL
;
353 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
354 NULL
, NULL
, &smb_fname_parent
);
355 if (!NT_STATUS_IS_OK(status
)) {
359 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
361 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
362 "directory %s. Error was %s\n",
363 smb_fname_str_dbg(smb_fname_parent
),
365 TALLOC_FREE(smb_fname_parent
);
369 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
370 /* Already this uid - no need to change. */
371 DEBUG(10,("change_file_owner_to_parent: file %s "
372 "is already owned by uid %d\n",
374 (int)fsp
->fsp_name
->st
.st_ex_uid
));
375 TALLOC_FREE(smb_fname_parent
);
380 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
383 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
384 "file %s to parent directory uid %u. Error "
385 "was %s\n", fsp_str_dbg(fsp
),
386 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
389 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
390 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
391 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
392 /* Ensure the uid entry is updated. */
393 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
396 TALLOC_FREE(smb_fname_parent
);
399 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
400 const char *inherit_from_dir
,
402 SMB_STRUCT_STAT
*psbuf
)
404 struct smb_filename
*smb_fname_parent
= NULL
;
405 struct smb_filename
*smb_fname_cwd
= NULL
;
406 char *saved_dir
= NULL
;
407 TALLOC_CTX
*ctx
= talloc_tos();
408 NTSTATUS status
= NT_STATUS_OK
;
411 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
413 if (!NT_STATUS_IS_OK(status
)) {
417 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
419 status
= map_nt_error_from_unix(errno
);
420 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
421 "directory %s. Error was %s\n",
422 smb_fname_str_dbg(smb_fname_parent
),
427 /* We've already done an lstat into psbuf, and we know it's a
428 directory. If we can cd into the directory and the dev/ino
429 are the same then we can safely chown without races as
430 we're locking the directory in place by being in it. This
431 should work on any UNIX (thanks tridge :-). JRA.
434 saved_dir
= vfs_GetWd(ctx
,conn
);
436 status
= map_nt_error_from_unix(errno
);
437 DEBUG(0,("change_dir_owner_to_parent: failed to get "
438 "current working directory. Error was %s\n",
443 /* Chdir into the new path. */
444 if (vfs_ChDir(conn
, fname
) == -1) {
445 status
= map_nt_error_from_unix(errno
);
446 DEBUG(0,("change_dir_owner_to_parent: failed to change "
447 "current working directory to %s. Error "
448 "was %s\n", fname
, strerror(errno
) ));
452 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
454 if (!NT_STATUS_IS_OK(status
)) {
458 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
460 status
= map_nt_error_from_unix(errno
);
461 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
462 "directory '.' (%s) Error was %s\n",
463 fname
, strerror(errno
)));
467 /* Ensure we're pointing at the same place. */
468 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
469 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
470 DEBUG(0,("change_dir_owner_to_parent: "
471 "device/inode on directory %s changed. "
472 "Refusing to chown !\n", fname
));
473 status
= NT_STATUS_ACCESS_DENIED
;
477 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
478 /* Already this uid - no need to change. */
479 DEBUG(10,("change_dir_owner_to_parent: directory %s "
480 "is already owned by uid %d\n",
482 (int)smb_fname_cwd
->st
.st_ex_uid
));
483 status
= NT_STATUS_OK
;
488 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
492 status
= map_nt_error_from_unix(errno
);
493 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
494 "directory %s to parent directory uid %u. "
495 "Error was %s\n", fname
,
496 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
499 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
500 "directory %s to parent directory uid %u.\n",
501 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
502 /* Ensure the uid entry is updated. */
503 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
507 vfs_ChDir(conn
,saved_dir
);
509 TALLOC_FREE(smb_fname_parent
);
510 TALLOC_FREE(smb_fname_cwd
);
514 /****************************************************************************
516 ****************************************************************************/
518 static NTSTATUS
open_file(files_struct
*fsp
,
519 connection_struct
*conn
,
520 struct smb_request
*req
,
521 const char *parent_dir
,
524 uint32 access_mask
, /* client requested access mask. */
525 uint32 open_access_mask
) /* what we're actually using in the open. */
527 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
528 NTSTATUS status
= NT_STATUS_OK
;
529 int accmode
= (flags
& O_ACCMODE
);
530 int local_flags
= flags
;
531 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
532 bool file_created
= false;
537 /* Check permissions */
540 * This code was changed after seeing a client open request
541 * containing the open mode of (DENY_WRITE/read-only) with
542 * the 'create if not exist' bit set. The previous code
543 * would fail to open the file read only on a read-only share
544 * as it was checking the flags parameter directly against O_RDONLY,
545 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
549 if (!CAN_WRITE(conn
)) {
550 /* It's a read-only share - fail if we wanted to write. */
551 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
552 DEBUG(3,("Permission denied opening %s\n",
553 smb_fname_str_dbg(smb_fname
)));
554 return NT_STATUS_ACCESS_DENIED
;
555 } else if(flags
& O_CREAT
) {
556 /* We don't want to write - but we must make sure that
557 O_CREAT doesn't create the file if we have write
558 access into the directory.
560 flags
&= ~(O_CREAT
|O_EXCL
);
561 local_flags
&= ~(O_CREAT
|O_EXCL
);
566 * This little piece of insanity is inspired by the
567 * fact that an NT client can open a file for O_RDONLY,
568 * but set the create disposition to FILE_EXISTS_TRUNCATE.
569 * If the client *can* write to the file, then it expects to
570 * truncate the file, even though it is opening for readonly.
571 * Quicken uses this stupid trick in backup file creation...
572 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
573 * for helping track this one down. It didn't bite us in 2.0.x
574 * as we always opened files read-write in that release. JRA.
577 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
578 DEBUG(10,("open_file: truncate requested on read-only open "
579 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
580 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
583 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
584 (!file_existed
&& (local_flags
& O_CREAT
)) ||
585 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
589 * We can't actually truncate here as the file may be locked.
590 * open_file_ntcreate will take care of the truncate later. JRA.
593 local_flags
&= ~O_TRUNC
;
595 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
597 * We would block on opening a FIFO with no one else on the
598 * other end. Do what we used to do and add O_NONBLOCK to the
602 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
603 local_flags
|= O_NONBLOCK
;
607 /* Don't create files with Microsoft wildcard characters. */
610 * wildcard characters are allowed in stream names
611 * only test the basefilename
613 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
615 wild
= smb_fname
->base_name
;
617 if ((local_flags
& O_CREAT
) && !file_existed
&&
619 return NT_STATUS_OBJECT_NAME_INVALID
;
622 /* Can we access this file ? */
623 if (!fsp
->base_fsp
) {
624 /* Only do this check on non-stream open. */
626 status
= smbd_check_open_rights(conn
,
629 } else if (local_flags
& O_CREAT
){
630 status
= check_parent_access(conn
,
635 /* File didn't exist and no O_CREAT. */
636 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
638 if (!NT_STATUS_IS_OK(status
)) {
639 DEBUG(10,("open_file: "
643 "smbd_check_open_rights" :
644 "check_parent_access",
645 smb_fname_str_dbg(smb_fname
),
646 nt_errstr(status
) ));
651 /* Actually do the open */
652 status
= fd_open(conn
, fsp
, local_flags
, unx_mode
);
653 if (!NT_STATUS_IS_OK(status
)) {
654 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
655 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
656 nt_errstr(status
),local_flags
,flags
));
660 if ((local_flags
& O_CREAT
) && !file_existed
) {
665 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
667 /* File must exist for a stat open. */
668 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
671 status
= smbd_check_open_rights(conn
,
675 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
677 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
678 /* This is a POSIX stat open for delete
679 * or rename on a symlink that points
681 DEBUG(10,("open_file: allowing POSIX "
682 "open on bad symlink %s\n",
683 smb_fname_str_dbg(smb_fname
)));
684 status
= NT_STATUS_OK
;
687 if (!NT_STATUS_IS_OK(status
)) {
688 DEBUG(10,("open_file: "
689 "smbd_check_open_rights on file "
691 smb_fname_str_dbg(smb_fname
),
692 nt_errstr(status
) ));
700 if (fsp
->fh
->fd
== -1) {
701 ret
= SMB_VFS_STAT(conn
, smb_fname
);
703 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
704 /* If we have an fd, this stat should succeed. */
706 DEBUG(0,("Error doing fstat on open file %s "
708 smb_fname_str_dbg(smb_fname
),
713 /* For a non-io open, this stat failing means file not found. JRA */
715 status
= map_nt_error_from_unix(errno
);
721 bool need_re_stat
= false;
722 /* Do all inheritance work after we've
723 done a successful stat call and filled
724 in the stat struct in fsp->fsp_name. */
726 /* Inherit the ACL if required */
727 if (lp_inherit_perms(SNUM(conn
))) {
728 inherit_access_posix_acl(conn
, parent_dir
,
729 smb_fname
->base_name
,
734 /* Change the owner if required. */
735 if (lp_inherit_owner(SNUM(conn
))) {
736 change_file_owner_to_parent(conn
, parent_dir
,
742 if (fsp
->fh
->fd
== -1) {
743 ret
= SMB_VFS_STAT(conn
, smb_fname
);
745 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
746 /* If we have an fd, this stat should succeed. */
748 DEBUG(0,("Error doing fstat on open file %s "
750 smb_fname_str_dbg(smb_fname
),
756 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
757 FILE_NOTIFY_CHANGE_FILE_NAME
,
758 smb_fname
->base_name
);
763 * POSIX allows read-only opens of directories. We don't
764 * want to do this (we use a different code path for this)
765 * so catch a directory open and return an EISDIR. JRA.
768 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
771 return NT_STATUS_FILE_IS_A_DIRECTORY
;
774 fsp
->mode
= smb_fname
->st
.st_ex_mode
;
775 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
776 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
777 fsp
->file_pid
= req
? req
->smbpid
: 0;
778 fsp
->can_lock
= True
;
779 fsp
->can_read
= (access_mask
& (FILE_READ_DATA
)) ? True
: False
;
780 if (!CAN_WRITE(conn
)) {
781 fsp
->can_write
= False
;
783 fsp
->can_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
786 fsp
->print_file
= NULL
;
787 fsp
->modified
= False
;
788 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
789 fsp
->is_directory
= False
;
790 if (conn
->aio_write_behind_list
&&
791 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
792 conn
->case_sensitive
)) {
793 fsp
->aio_write_behind
= True
;
796 fsp
->wcp
= NULL
; /* Write cache pointer. */
798 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
799 conn
->session_info
->unix_info
->unix_name
,
800 smb_fname_str_dbg(smb_fname
),
801 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
802 conn
->num_files_open
));
808 /****************************************************************************
809 Check if we can open a file with a share mode.
810 Returns True if conflict, False if not.
811 ****************************************************************************/
813 static bool share_conflict(struct share_mode_entry
*entry
,
817 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
818 "entry->share_access = 0x%x, "
819 "entry->private_options = 0x%x\n",
820 (unsigned int)entry
->access_mask
,
821 (unsigned int)entry
->share_access
,
822 (unsigned int)entry
->private_options
));
824 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
825 (unsigned int)access_mask
, (unsigned int)share_access
));
827 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
831 DELETE_ACCESS
)) == 0) {
832 DEBUG(10,("share_conflict: No conflict due to "
833 "entry->access_mask = 0x%x\n",
834 (unsigned int)entry
->access_mask
));
838 if ((access_mask
& (FILE_WRITE_DATA
|
842 DELETE_ACCESS
)) == 0) {
843 DEBUG(10,("share_conflict: No conflict due to "
844 "access_mask = 0x%x\n",
845 (unsigned int)access_mask
));
849 #if 1 /* JRA TEST - Superdebug. */
850 #define CHECK_MASK(num, am, right, sa, share) \
851 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
852 (unsigned int)(num), (unsigned int)(am), \
853 (unsigned int)(right), (unsigned int)(am)&(right) )); \
854 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
855 (unsigned int)(num), (unsigned int)(sa), \
856 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
857 if (((am) & (right)) && !((sa) & (share))) { \
858 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
859 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
860 (unsigned int)(share) )); \
864 #define CHECK_MASK(num, am, right, sa, share) \
865 if (((am) & (right)) && !((sa) & (share))) { \
866 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
867 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
868 (unsigned int)(share) )); \
873 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
874 share_access
, FILE_SHARE_WRITE
);
875 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
876 entry
->share_access
, FILE_SHARE_WRITE
);
878 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
879 share_access
, FILE_SHARE_READ
);
880 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
881 entry
->share_access
, FILE_SHARE_READ
);
883 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
884 share_access
, FILE_SHARE_DELETE
);
885 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
886 entry
->share_access
, FILE_SHARE_DELETE
);
888 DEBUG(10,("share_conflict: No conflict.\n"));
892 #if defined(DEVELOPER)
893 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
895 struct share_mode_entry
*share_entry
)
899 if (!procid_is_me(&share_entry
->pid
)) {
903 if (is_deferred_open_entry(share_entry
) &&
904 !open_was_deferred(sconn
, share_entry
->op_mid
)) {
905 char *str
= talloc_asprintf(talloc_tos(),
906 "Got a deferred entry without a request: "
908 share_mode_str(talloc_tos(), num
, share_entry
));
912 if (!is_valid_share_mode_entry(share_entry
)) {
916 fsp
= file_find_dif(sconn
, share_entry
->id
,
917 share_entry
->share_file_id
);
919 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
920 share_mode_str(talloc_tos(), num
, share_entry
) ));
921 smb_panic("validate_my_share_entries: Cannot match a "
922 "share entry with an open file\n");
925 if (is_deferred_open_entry(share_entry
) ||
926 is_unused_share_mode_entry(share_entry
)) {
930 if ((share_entry
->op_type
== NO_OPLOCK
) &&
931 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
932 /* Someone has already written to it, but I haven't yet
937 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
946 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
947 share_mode_str(talloc_tos(), num
, share_entry
) ));
948 str
= talloc_asprintf(talloc_tos(),
949 "validate_my_share_entries: "
950 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
951 fsp
->fsp_name
->base_name
,
952 (unsigned int)fsp
->oplock_type
,
953 (unsigned int)share_entry
->op_type
);
959 bool is_stat_open(uint32 access_mask
)
961 return (access_mask
&&
962 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
963 FILE_WRITE_ATTRIBUTES
))==0) &&
964 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
965 FILE_WRITE_ATTRIBUTES
)) != 0));
968 /****************************************************************************
969 Deal with share modes
970 Invarient: Share mode must be locked on entry and exit.
971 Returns -1 on error, or number of share modes on success (may be zero).
972 ****************************************************************************/
974 static NTSTATUS
open_mode_check(connection_struct
*conn
,
975 struct share_mode_lock
*lck
,
979 uint32 create_options
,
984 if(lck
->num_share_modes
== 0) {
988 *file_existed
= True
;
990 /* A delete on close prohibits everything */
992 if (is_delete_on_close_set(lck
, name_hash
)) {
993 return NT_STATUS_DELETE_PENDING
;
996 if (is_stat_open(access_mask
)) {
997 /* Stat open that doesn't trigger oplock breaks or share mode
998 * checks... ! JRA. */
1003 * Check if the share modes will give us access.
1006 #if defined(DEVELOPER)
1007 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
1008 validate_my_share_entries(conn
->sconn
, i
,
1009 &lck
->share_modes
[i
]);
1013 if (!lp_share_modes(SNUM(conn
))) {
1014 return NT_STATUS_OK
;
1017 /* Now we check the share modes, after any oplock breaks. */
1018 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
1020 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
1024 /* someone else has a share lock on it, check to see if we can
1026 if (share_conflict(&lck
->share_modes
[i
],
1027 access_mask
, share_access
)) {
1028 return NT_STATUS_SHARING_VIOLATION
;
1032 return NT_STATUS_OK
;
1035 static bool is_delete_request(files_struct
*fsp
) {
1036 return ((fsp
->access_mask
== DELETE_ACCESS
) &&
1037 (fsp
->oplock_type
== NO_OPLOCK
));
1041 * Send a break message to the oplock holder and delay the open for
1045 static NTSTATUS
send_break_message(files_struct
*fsp
,
1046 struct share_mode_entry
*exclusive
,
1051 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1053 DEBUG(10, ("Sending break request to PID %s\n",
1054 procid_str_static(&exclusive
->pid
)));
1055 exclusive
->op_mid
= mid
;
1057 /* Create the message. */
1058 share_mode_entry_to_message(msg
, exclusive
);
1060 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1061 don't want this set in the share mode struct pointed to by lck. */
1063 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
1064 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
1065 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
1068 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
1069 MSG_SMB_BREAK_REQUEST
,
1071 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
1072 if (!NT_STATUS_IS_OK(status
)) {
1073 DEBUG(3, ("Could not send oplock break message: %s\n",
1074 nt_errstr(status
)));
1081 * Return share_mode_entry pointers for :
1082 * 1). Batch oplock entry.
1083 * 2). Batch or exclusive oplock entry (may be identical to #1).
1084 * bool have_level2_oplock
1085 * bool have_no_oplock.
1086 * Do internal consistency checks on the share mode for a file.
1089 static void find_oplock_types(files_struct
*fsp
,
1091 struct share_mode_lock
*lck
,
1092 struct share_mode_entry
**pp_batch
,
1093 struct share_mode_entry
**pp_ex_or_batch
,
1095 bool *got_no_oplock
)
1100 *pp_ex_or_batch
= NULL
;
1101 *got_level2
= false;
1102 *got_no_oplock
= false;
1104 /* Ignore stat or internal opens, as is done in
1105 delay_for_batch_oplocks() and
1106 delay_for_exclusive_oplocks().
1108 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1112 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1113 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
1117 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
&&
1118 is_stat_open(lck
->share_modes
[i
].access_mask
)) {
1119 /* We ignore stat opens in the table - they
1120 always have NO_OPLOCK and never get or
1121 cause breaks. JRA. */
1125 if (BATCH_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1126 /* batch - can only be one. */
1127 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
1128 smb_panic("Bad batch oplock entry.");
1130 *pp_batch
= &lck
->share_modes
[i
];
1133 if (EXCLUSIVE_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1134 /* Exclusive or batch - can only be one. */
1135 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
1136 smb_panic("Bad exclusive or batch oplock entry.");
1138 *pp_ex_or_batch
= &lck
->share_modes
[i
];
1141 if (LEVEL_II_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1142 if (*pp_batch
|| *pp_ex_or_batch
) {
1143 smb_panic("Bad levelII oplock entry.");
1148 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
) {
1149 if (*pp_batch
|| *pp_ex_or_batch
) {
1150 smb_panic("Bad no oplock entry.");
1152 *got_no_oplock
= true;
1157 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1160 struct share_mode_entry
*batch_entry
)
1162 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1166 if (batch_entry
!= NULL
) {
1167 /* Found a batch oplock */
1168 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1174 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1177 struct share_mode_entry
*ex_entry
)
1179 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1183 if (ex_entry
!= NULL
) {
1184 /* Found an exclusive or batch oplock */
1185 bool delay_it
= is_delete_request(fsp
) ?
1186 BATCH_OPLOCK_TYPE(ex_entry
->op_type
) : true;
1188 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1195 static void grant_fsp_oplock_type(files_struct
*fsp
,
1196 const struct byte_range_lock
*br_lck
,
1198 bool got_level2_oplock
,
1199 bool got_a_none_oplock
)
1201 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1202 lp_level2_oplocks(SNUM(fsp
->conn
));
1204 /* Start by granting what the client asked for,
1205 but ensure no SAMBA_PRIVATE bits can be set. */
1206 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1208 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1209 /* No oplocks on internal open. */
1210 fsp
->oplock_type
= NO_OPLOCK
;
1211 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1212 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1214 } else if (br_lck
&& br_lck
->num_locks
> 0) {
1215 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1217 fsp
->oplock_type
= NO_OPLOCK
;
1220 if (is_stat_open(fsp
->access_mask
)) {
1221 /* Leave the value already set. */
1222 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1223 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1228 * Match what was requested (fsp->oplock_type) with
1229 * what was found in the existing share modes.
1232 if (got_a_none_oplock
) {
1233 fsp
->oplock_type
= NO_OPLOCK
;
1234 } else if (got_level2_oplock
) {
1235 if (fsp
->oplock_type
== NO_OPLOCK
||
1236 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1237 /* Store a level2 oplock, but don't tell the client */
1238 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1240 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1243 /* All share_mode_entries are placeholders or deferred.
1244 * Silently upgrade to fake levelII if the client didn't
1245 * ask for an oplock. */
1246 if (fsp
->oplock_type
== NO_OPLOCK
) {
1247 /* Store a level2 oplock, but don't tell the client */
1248 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1253 * Don't grant level2 to clients that don't want them
1254 * or if we've turned them off.
1256 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1257 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1260 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1261 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1264 bool request_timed_out(struct timeval request_time
,
1265 struct timeval timeout
)
1267 struct timeval now
, end_time
;
1269 end_time
= timeval_sum(&request_time
, &timeout
);
1270 return (timeval_compare(&end_time
, &now
) < 0);
1273 /****************************************************************************
1274 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1275 ****************************************************************************/
1277 static void defer_open(struct share_mode_lock
*lck
,
1278 struct timeval request_time
,
1279 struct timeval timeout
,
1280 struct smb_request
*req
,
1281 struct deferred_open_record
*state
)
1285 /* Paranoia check */
1287 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1288 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1290 if (!is_deferred_open_entry(e
)) {
1294 if (procid_is_me(&e
->pid
) && (e
->op_mid
== req
->mid
)) {
1295 DEBUG(0, ("Trying to defer an already deferred "
1296 "request: mid=%llu, exiting\n",
1297 (unsigned long long)req
->mid
));
1298 exit_server("attempt to defer a deferred request");
1302 /* End paranoia check */
1304 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1305 "open entry for mid %llu\n",
1306 (unsigned int)request_time
.tv_sec
,
1307 (unsigned int)request_time
.tv_usec
,
1308 (unsigned long long)req
->mid
));
1310 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1311 state
->id
, (char *)state
, sizeof(*state
))) {
1312 exit_server("push_deferred_open_message_smb failed");
1314 add_deferred_open(lck
, req
->mid
, request_time
,
1315 sconn_server_id(req
->sconn
), state
->id
);
1319 /****************************************************************************
1320 On overwrite open ensure that the attributes match.
1321 ****************************************************************************/
1323 bool open_match_attributes(connection_struct
*conn
,
1324 uint32 old_dos_attr
,
1325 uint32 new_dos_attr
,
1326 mode_t existing_unx_mode
,
1327 mode_t new_unx_mode
,
1328 mode_t
*returned_unx_mode
)
1330 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1332 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1333 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1335 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1336 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1337 *returned_unx_mode
= new_unx_mode
;
1339 *returned_unx_mode
= (mode_t
)0;
1342 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1343 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1344 "returned_unx_mode = 0%o\n",
1345 (unsigned int)old_dos_attr
,
1346 (unsigned int)existing_unx_mode
,
1347 (unsigned int)new_dos_attr
,
1348 (unsigned int)*returned_unx_mode
));
1350 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1351 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1352 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1353 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1357 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1358 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1359 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1366 /****************************************************************************
1367 Special FCB or DOS processing in the case of a sharing violation.
1368 Try and find a duplicated file handle.
1369 ****************************************************************************/
1371 NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1372 connection_struct
*conn
,
1373 files_struct
*fsp_to_dup_into
,
1374 const struct smb_filename
*smb_fname
,
1379 uint32 share_access
,
1380 uint32 create_options
)
1384 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1385 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1387 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1388 fsp
= file_find_di_next(fsp
)) {
1390 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1391 "vuid = %u, file_pid = %u, private_options = 0x%x "
1392 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1393 fsp
->fh
->fd
, (unsigned int)fsp
->vuid
,
1394 (unsigned int)fsp
->file_pid
,
1395 (unsigned int)fsp
->fh
->private_options
,
1396 (unsigned int)fsp
->access_mask
));
1398 if (fsp
->fh
->fd
!= -1 &&
1399 fsp
->vuid
== vuid
&&
1400 fsp
->file_pid
== file_pid
&&
1401 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1402 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1403 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1404 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1405 strequal(fsp
->fsp_name
->stream_name
,
1406 smb_fname
->stream_name
)) {
1407 DEBUG(10,("fcb_or_dos_open: file match\n"));
1413 return NT_STATUS_NOT_FOUND
;
1416 /* quite an insane set of semantics ... */
1417 if (is_executable(smb_fname
->base_name
) &&
1418 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1419 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1420 return NT_STATUS_INVALID_PARAMETER
;
1423 /* We need to duplicate this fsp. */
1424 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1425 create_options
, fsp_to_dup_into
);
1428 static void schedule_defer_open(struct share_mode_lock
*lck
,
1429 struct timeval request_time
,
1430 struct smb_request
*req
)
1432 struct deferred_open_record state
;
1434 /* This is a relative time, added to the absolute
1435 request_time value to get the absolute timeout time.
1436 Note that if this is the second or greater time we enter
1437 this codepath for this particular request mid then
1438 request_time is left as the absolute time of the *first*
1439 time this request mid was processed. This is what allows
1440 the request to eventually time out. */
1442 struct timeval timeout
;
1444 /* Normally the smbd we asked should respond within
1445 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1446 * the client did, give twice the timeout as a safety
1447 * measure here in case the other smbd is stuck
1448 * somewhere else. */
1450 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1452 /* Nothing actually uses state.delayed_for_oplocks
1453 but it's handy to differentiate in debug messages
1454 between a 30 second delay due to oplock break, and
1455 a 1 second delay for share mode conflicts. */
1457 state
.delayed_for_oplocks
= True
;
1460 if (!request_timed_out(request_time
, timeout
)) {
1461 defer_open(lck
, request_time
, timeout
, req
, &state
);
1465 /****************************************************************************
1466 Work out what access_mask to use from what the client sent us.
1467 ****************************************************************************/
1469 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1470 const struct smb_filename
*smb_fname
,
1472 uint32_t access_mask
,
1473 uint32_t *access_mask_out
)
1476 uint32_t orig_access_mask
= access_mask
;
1477 uint32_t rejected_share_access
;
1480 * Convert GENERIC bits to specific bits.
1483 se_map_generic(&access_mask
, &file_generic_mapping
);
1485 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1486 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1487 if (get_current_uid(conn
) == (uid_t
)0) {
1488 access_mask
|= FILE_GENERIC_ALL
;
1489 } else if (file_existed
) {
1491 struct security_descriptor
*sd
;
1492 uint32_t access_granted
= 0;
1494 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1499 if (!NT_STATUS_IS_OK(status
)) {
1500 DEBUG(10,("smbd_calculate_access_mask: "
1501 "Could not get acl on file %s: %s\n",
1502 smb_fname_str_dbg(smb_fname
),
1503 nt_errstr(status
)));
1504 return NT_STATUS_ACCESS_DENIED
;
1507 status
= smb1_file_se_access_check(conn
,
1509 get_current_nttok(conn
),
1515 if (!NT_STATUS_IS_OK(status
)) {
1516 DEBUG(10, ("smbd_calculate_access_mask: "
1517 "Access denied on file %s: "
1518 "when calculating maximum access\n",
1519 smb_fname_str_dbg(smb_fname
)));
1520 return NT_STATUS_ACCESS_DENIED
;
1523 access_mask
= access_granted
;
1525 access_mask
= FILE_GENERIC_ALL
;
1528 access_mask
&= conn
->share_access
;
1531 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1533 if (rejected_share_access
) {
1534 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1535 "file %s: rejected by share access mask[0x%08X] "
1536 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1537 smb_fname_str_dbg(smb_fname
),
1539 orig_access_mask
, access_mask
,
1540 rejected_share_access
));
1541 return NT_STATUS_ACCESS_DENIED
;
1544 *access_mask_out
= access_mask
;
1545 return NT_STATUS_OK
;
1548 /****************************************************************************
1549 Remove the deferred open entry under lock.
1550 ****************************************************************************/
1552 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1553 struct server_id pid
)
1555 struct share_mode_lock
*lck
= get_share_mode_lock(talloc_tos(), id
,
1558 DEBUG(0, ("could not get share mode lock\n"));
1560 del_deferred_open_entry(lck
, mid
, pid
);
1565 /****************************************************************
1566 Ensure we get the brlock lock followed by the share mode lock
1567 in the correct order to prevent deadlocks if other smbd's are
1568 using the brlock database on this file simultaneously with this open
1569 (that code also gets the locks in brlock -> share mode lock order).
1570 ****************************************************************/
1572 static bool acquire_ordered_locks(TALLOC_CTX
*mem_ctx
,
1574 const struct file_id id
,
1575 const char *connectpath
,
1576 const struct smb_filename
*smb_fname
,
1577 const struct timespec
*p_old_write_time
,
1578 struct share_mode_lock
**p_lck
,
1579 struct byte_range_lock
**p_br_lck
)
1581 /* Ordering - we must get the br_lck for this
1582 file before the share mode. */
1583 if (lp_locking(fsp
->conn
->params
)) {
1584 *p_br_lck
= brl_get_locks_readonly(fsp
);
1585 if (*p_br_lck
== NULL
) {
1586 DEBUG(0, ("Could not get br_lock\n"));
1589 /* Note - we don't need to free the returned
1590 br_lck explicitly as it was allocated on talloc_tos()
1591 and so will be autofreed (and release the lock)
1592 once the frame context disappears.
1594 If it was set to fsp->brlock_rec then it was
1595 talloc_move'd to hang off the fsp pointer and
1596 in this case is guarenteed to not be holding the
1597 lock on the brlock database. */
1600 *p_lck
= get_share_mode_lock(mem_ctx
,
1606 if (*p_lck
== NULL
) {
1607 DEBUG(0, ("Could not get share mode lock\n"));
1608 TALLOC_FREE(*p_br_lck
);
1614 /****************************************************************************
1615 Open a file with a share mode. Passed in an already created files_struct *.
1616 ****************************************************************************/
1618 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1619 struct smb_request
*req
,
1620 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1621 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1622 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1623 uint32 create_options
, /* options such as delete on close. */
1624 uint32 new_dos_attributes
, /* attributes used for new file. */
1625 int oplock_request
, /* internal Samba oplock codes. */
1626 /* Information (FILE_EXISTS etc.) */
1627 uint32_t private_flags
, /* Samba specific flags. */
1631 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1634 bool file_existed
= VALID_STAT(smb_fname
->st
);
1635 bool def_acl
= False
;
1636 bool posix_open
= False
;
1637 bool new_file_created
= False
;
1638 bool clear_ads
= false;
1640 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1641 mode_t new_unx_mode
= (mode_t
)0;
1642 mode_t unx_mode
= (mode_t
)0;
1644 uint32 existing_dos_attributes
= 0;
1645 struct timeval request_time
= timeval_zero();
1646 struct share_mode_lock
*lck
= NULL
;
1647 uint32 open_access_mask
= access_mask
;
1653 if (conn
->printer
) {
1655 * Printers are handled completely differently.
1656 * Most of the passed parameters are ignored.
1660 *pinfo
= FILE_WAS_CREATED
;
1663 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1664 smb_fname_str_dbg(smb_fname
)));
1667 DEBUG(0,("open_file_ntcreate: printer open without "
1668 "an SMB request!\n"));
1669 return NT_STATUS_INTERNAL_ERROR
;
1672 return print_spool_open(fsp
, smb_fname
->base_name
,
1676 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1678 return NT_STATUS_NO_MEMORY
;
1681 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1683 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
1684 new_dos_attributes
= 0;
1686 /* Windows allows a new file to be created and
1687 silently removes a FILE_ATTRIBUTE_DIRECTORY
1688 sent by the client. Do the same. */
1690 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
1692 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1694 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
1695 smb_fname
, parent_dir
);
1698 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1699 "access_mask=0x%x share_access=0x%x "
1700 "create_disposition = 0x%x create_options=0x%x "
1701 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1702 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
1703 access_mask
, share_access
, create_disposition
,
1704 create_options
, (unsigned int)unx_mode
, oplock_request
,
1705 (unsigned int)private_flags
));
1707 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
1708 DEBUG(0, ("No smb request but not an internal only open!\n"));
1709 return NT_STATUS_INTERNAL_ERROR
;
1713 * Only non-internal opens can be deferred at all
1718 if (get_deferred_open_message_state(req
,
1722 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
1723 /* Remember the absolute time of the original
1724 request with this mid. We'll use it later to
1725 see if this has timed out. */
1727 /* Remove the deferred open entry under lock. */
1728 remove_deferred_open_entry(
1729 state
->id
, req
->mid
,
1730 sconn_server_id(req
->sconn
));
1732 /* Ensure we don't reprocess this message. */
1733 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
1737 status
= check_name(conn
, smb_fname
->base_name
);
1738 if (!NT_STATUS_IS_OK(status
)) {
1743 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
1745 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
1749 /* ignore any oplock requests if oplocks are disabled */
1750 if (!lp_oplocks(SNUM(conn
)) ||
1751 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
1752 /* Mask off everything except the private Samba bits. */
1753 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
1756 /* this is for OS/2 long file names - say we don't support them */
1757 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
1758 /* OS/2 Workplace shell fix may be main code stream in a later
1760 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1762 if (use_nt_status()) {
1763 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1765 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
1768 switch( create_disposition
) {
1770 * Currently we're using FILE_SUPERSEDE as the same as
1771 * FILE_OVERWRITE_IF but they really are
1772 * different. FILE_SUPERSEDE deletes an existing file
1773 * (requiring delete access) then recreates it.
1775 case FILE_SUPERSEDE
:
1776 /* If file exists replace/overwrite. If file doesn't
1778 flags2
|= (O_CREAT
| O_TRUNC
);
1782 case FILE_OVERWRITE_IF
:
1783 /* If file exists replace/overwrite. If file doesn't
1785 flags2
|= (O_CREAT
| O_TRUNC
);
1790 /* If file exists open. If file doesn't exist error. */
1791 if (!file_existed
) {
1792 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1793 "requested for file %s and file "
1795 smb_fname_str_dbg(smb_fname
)));
1797 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1801 case FILE_OVERWRITE
:
1802 /* If file exists overwrite. If file doesn't exist
1804 if (!file_existed
) {
1805 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1806 "requested for file %s and file "
1808 smb_fname_str_dbg(smb_fname
) ));
1810 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1817 /* If file exists error. If file doesn't exist
1820 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1821 "requested for file %s and file "
1822 "already exists.\n",
1823 smb_fname_str_dbg(smb_fname
)));
1824 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1829 return map_nt_error_from_unix(errno
);
1831 flags2
|= (O_CREAT
|O_EXCL
);
1835 /* If file exists open. If file doesn't exist
1841 return NT_STATUS_INVALID_PARAMETER
;
1844 /* We only care about matching attributes on file exists and
1847 if (!posix_open
&& file_existed
&& ((create_disposition
== FILE_OVERWRITE
) ||
1848 (create_disposition
== FILE_OVERWRITE_IF
))) {
1849 if (!open_match_attributes(conn
, existing_dos_attributes
,
1851 smb_fname
->st
.st_ex_mode
,
1852 unx_mode
, &new_unx_mode
)) {
1853 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1854 "for file %s (%x %x) (0%o, 0%o)\n",
1855 smb_fname_str_dbg(smb_fname
),
1856 existing_dos_attributes
,
1858 (unsigned int)smb_fname
->st
.st_ex_mode
,
1859 (unsigned int)unx_mode
));
1861 return NT_STATUS_ACCESS_DENIED
;
1865 status
= smbd_calculate_access_mask(conn
, smb_fname
, file_existed
,
1868 if (!NT_STATUS_IS_OK(status
)) {
1869 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1870 "on file %s returned %s\n",
1871 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
1875 open_access_mask
= access_mask
;
1877 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1878 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
1881 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1882 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
1886 * Note that we ignore the append flag as append does not
1887 * mean the same thing under DOS and Unix.
1890 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
1891 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1892 /* DENY_DOS opens are always underlying read-write on the
1893 file handle, no matter what the requested access mask
1895 if ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
1896 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
1906 * Currently we only look at FILE_WRITE_THROUGH for create options.
1910 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
1915 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
1919 if (!posix_open
&& !CAN_WRITE(conn
)) {
1921 * We should really return a permission denied error if either
1922 * O_CREAT or O_TRUNC are set, but for compatibility with
1923 * older versions of Samba we just AND them out.
1925 flags2
&= ~(O_CREAT
|O_TRUNC
);
1929 * Ensure we can't write on a read-only share or file.
1932 if (flags
!= O_RDONLY
&& file_existed
&&
1933 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
1934 DEBUG(5,("open_file_ntcreate: write access requested for "
1935 "file %s on read only %s\n",
1936 smb_fname_str_dbg(smb_fname
),
1937 !CAN_WRITE(conn
) ? "share" : "file" ));
1939 return NT_STATUS_ACCESS_DENIED
;
1942 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1943 fsp
->share_access
= share_access
;
1944 fsp
->fh
->private_options
= private_flags
;
1945 fsp
->access_mask
= open_access_mask
; /* We change this to the
1946 * requested access_mask after
1947 * the open is done. */
1948 fsp
->posix_open
= posix_open
;
1950 /* Ensure no SAMBA_PRIVATE bits can be set. */
1951 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1953 if (timeval_is_zero(&request_time
)) {
1954 request_time
= fsp
->open_time
;
1958 struct byte_range_lock
*br_lck
= NULL
;
1959 struct share_mode_entry
*batch_entry
= NULL
;
1960 struct share_mode_entry
*exclusive_entry
= NULL
;
1961 bool got_level2_oplock
= false;
1962 bool got_a_none_oplock
= false;
1964 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
1965 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1967 if (!acquire_ordered_locks(talloc_tos(),
1975 return NT_STATUS_SHARING_VIOLATION
;
1978 /* Get the types we need to examine. */
1979 find_oplock_types(fsp
,
1985 &got_a_none_oplock
);
1987 /* First pass - send break only on batch oplocks. */
1988 if ((req
!= NULL
) &&
1989 delay_for_batch_oplocks(fsp
,
1993 schedule_defer_open(lck
, request_time
, req
);
1995 return NT_STATUS_SHARING_VIOLATION
;
1998 /* Use the client requested access mask here, not the one we
2000 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2001 access_mask
, share_access
,
2002 create_options
, &file_existed
);
2004 if (NT_STATUS_IS_OK(status
)) {
2005 /* We might be going to allow this open. Check oplock
2007 /* Second pass - send break for both batch or
2008 * exclusive oplocks. */
2009 if ((req
!= NULL
) &&
2010 delay_for_exclusive_oplocks(
2015 schedule_defer_open(lck
, request_time
, req
);
2017 return NT_STATUS_SHARING_VIOLATION
;
2021 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
2022 /* DELETE_PENDING is not deferred for a second */
2027 grant_fsp_oplock_type(fsp
,
2033 if (!NT_STATUS_IS_OK(status
)) {
2034 uint32 can_access_mask
;
2035 bool can_access
= True
;
2037 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2039 /* Check if this can be done with the deny_dos and fcb
2042 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2043 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2045 DEBUG(0, ("DOS open without an SMB "
2048 return NT_STATUS_INTERNAL_ERROR
;
2051 /* Use the client requested access mask here,
2052 * not the one we open with. */
2053 status
= fcb_or_dos_open(req
,
2064 if (NT_STATUS_IS_OK(status
)) {
2067 *pinfo
= FILE_WAS_OPENED
;
2069 return NT_STATUS_OK
;
2074 * This next line is a subtlety we need for
2075 * MS-Access. If a file open will fail due to share
2076 * permissions and also for security (access) reasons,
2077 * we need to return the access failed error, not the
2078 * share error. We can't open the file due to kernel
2079 * oplock deadlock (it's possible we failed above on
2080 * the open_mode_check()) so use a userspace check.
2083 if (flags
& O_RDWR
) {
2084 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2085 } else if (flags
& O_WRONLY
) {
2086 can_access_mask
= FILE_WRITE_DATA
;
2088 can_access_mask
= FILE_READ_DATA
;
2091 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2092 !CAN_WRITE(conn
)) ||
2093 !can_access_file_data(conn
, smb_fname
,
2099 * If we're returning a share violation, ensure we
2100 * cope with the braindead 1 second delay.
2103 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2104 lp_defer_sharing_violations()) {
2105 struct timeval timeout
;
2106 struct deferred_open_record state
;
2109 /* this is a hack to speed up torture tests
2111 timeout_usecs
= lp_parm_int(SNUM(conn
),
2112 "smbd","sharedelay",
2113 SHARING_VIOLATION_USEC_WAIT
);
2115 /* This is a relative time, added to the absolute
2116 request_time value to get the absolute timeout time.
2117 Note that if this is the second or greater time we enter
2118 this codepath for this particular request mid then
2119 request_time is left as the absolute time of the *first*
2120 time this request mid was processed. This is what allows
2121 the request to eventually time out. */
2123 timeout
= timeval_set(0, timeout_usecs
);
2125 /* Nothing actually uses state.delayed_for_oplocks
2126 but it's handy to differentiate in debug messages
2127 between a 30 second delay due to oplock break, and
2128 a 1 second delay for share mode conflicts. */
2130 state
.delayed_for_oplocks
= False
;
2134 && !request_timed_out(request_time
,
2136 defer_open(lck
, request_time
, timeout
,
2144 * We have detected a sharing violation here
2145 * so return the correct error code
2147 status
= NT_STATUS_SHARING_VIOLATION
;
2149 status
= NT_STATUS_ACCESS_DENIED
;
2155 * We exit this block with the share entry *locked*.....
2159 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
2162 * Ensure we pay attention to default ACLs on directories if required.
2165 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2166 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2167 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2170 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2171 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2172 (unsigned int)flags
, (unsigned int)flags2
,
2173 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2174 (unsigned int)open_access_mask
));
2177 * open_file strips any O_TRUNC flags itself.
2180 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2181 flags
|flags2
, unx_mode
, access_mask
,
2184 if (!NT_STATUS_IS_OK(fsp_open
)) {
2189 if (!file_existed
) {
2190 struct byte_range_lock
*br_lck
= NULL
;
2191 struct share_mode_entry
*batch_entry
= NULL
;
2192 struct share_mode_entry
*exclusive_entry
= NULL
;
2193 bool got_level2_oplock
= false;
2194 bool got_a_none_oplock
= false;
2195 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2197 * Deal with the race condition where two smbd's detect the
2198 * file doesn't exist and do the create at the same time. One
2199 * of them will win and set a share mode, the other (ie. this
2200 * one) should check if the requested share mode for this
2201 * create is allowed.
2205 * Now the file exists and fsp is successfully opened,
2206 * fsp->dev and fsp->inode are valid and should replace the
2207 * dev=0,inode=0 from a non existent file. Spotted by
2208 * Nadav Danieli <nadavd@exanet.com>. JRA.
2213 if (!acquire_ordered_locks(talloc_tos(),
2221 return NT_STATUS_SHARING_VIOLATION
;
2224 /* Get the types we need to examine. */
2225 find_oplock_types(fsp
,
2231 &got_a_none_oplock
);
2233 /* First pass - send break only on batch oplocks. */
2234 if ((req
!= NULL
) &&
2235 delay_for_batch_oplocks(fsp
,
2239 schedule_defer_open(lck
, request_time
, req
);
2242 return NT_STATUS_SHARING_VIOLATION
;
2245 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2246 access_mask
, share_access
,
2247 create_options
, &file_existed
);
2249 if (NT_STATUS_IS_OK(status
)) {
2250 /* We might be going to allow this open. Check oplock
2252 /* Second pass - send break for both batch or
2253 * exclusive oplocks. */
2254 if ((req
!= NULL
) &&
2255 delay_for_exclusive_oplocks(
2260 schedule_defer_open(lck
, request_time
, req
);
2263 return NT_STATUS_SHARING_VIOLATION
;
2267 if (!NT_STATUS_IS_OK(status
)) {
2268 struct deferred_open_record state
;
2270 state
.delayed_for_oplocks
= False
;
2273 /* Do it all over again immediately. In the second
2274 * round we will find that the file existed and handle
2275 * the DELETE_PENDING and FCB cases correctly. No need
2276 * to duplicate the code here. Essentially this is a
2277 * "goto top of this function", but don't tell
2281 defer_open(lck
, request_time
, timeval_zero(),
2289 grant_fsp_oplock_type(fsp
,
2296 * We exit this block with the share entry *locked*.....
2301 SMB_ASSERT(lck
!= NULL
);
2303 /* Delete streams if create_disposition requires it */
2304 if (file_existed
&& clear_ads
&&
2305 !is_ntfs_stream_smb_fname(smb_fname
)) {
2306 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2307 if (!NT_STATUS_IS_OK(status
)) {
2314 /* note that we ignore failure for the following. It is
2315 basically a hack for NFS, and NFS will never set one of
2316 these only read them. Nobody but Samba can ever set a deny
2317 mode and we have already checked our more authoritative
2318 locking database for permission to set this deny mode. If
2319 the kernel refuses the operations then the kernel is wrong.
2320 note that GPFS supports it as well - jmcd */
2322 if (fsp
->fh
->fd
!= -1) {
2324 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2325 if(ret_flock
== -1 ){
2330 return NT_STATUS_SHARING_VIOLATION
;
2335 * At this point onwards, we can guarentee that the share entry
2336 * is locked, whether we created the file or not, and that the
2337 * deny mode is compatible with all current opens.
2341 * If requested, truncate the file.
2344 if (file_existed
&& (flags2
&O_TRUNC
)) {
2346 * We are modifing the file after open - update the stat
2349 if ((SMB_VFS_FTRUNCATE(fsp
, 0) == -1) ||
2350 (SMB_VFS_FSTAT(fsp
, &smb_fname
->st
)==-1)) {
2351 status
= map_nt_error_from_unix(errno
);
2359 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2360 * but we don't have to store this - just ignore it on access check.
2362 if (conn
->sconn
->using_smb2
) {
2364 * SMB2 doesn't return it (according to Microsoft tests).
2365 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2366 * File created with access = 0x7 (Read, Write, Delete)
2367 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2369 fsp
->access_mask
= access_mask
;
2371 /* But SMB1 does. */
2372 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2376 /* stat opens on existing files don't get oplocks. */
2377 if (is_stat_open(open_access_mask
)) {
2378 fsp
->oplock_type
= NO_OPLOCK
;
2381 if (!(flags2
& O_TRUNC
)) {
2382 info
= FILE_WAS_OPENED
;
2384 info
= FILE_WAS_OVERWRITTEN
;
2387 info
= FILE_WAS_CREATED
;
2395 * Setup the oplock info in both the shared memory and
2399 if (!set_file_oplock(fsp
, fsp
->oplock_type
)) {
2401 * Could not get the kernel oplock or there are byte-range
2402 * locks on the file.
2404 fsp
->oplock_type
= NO_OPLOCK
;
2407 if (info
== FILE_WAS_OVERWRITTEN
|| info
== FILE_WAS_CREATED
|| info
== FILE_WAS_SUPERSEDED
) {
2408 new_file_created
= True
;
2411 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2415 /* Handle strange delete on close create semantics. */
2416 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2418 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2420 if (!NT_STATUS_IS_OK(status
)) {
2421 /* Remember to delete the mode we just added. */
2422 del_share_mode(lck
, fsp
);
2427 /* Note that here we set the *inital* delete on close flag,
2428 not the regular one. The magic gets handled in close. */
2429 fsp
->initial_delete_on_close
= True
;
2432 if (new_file_created
) {
2433 /* Files should be initially set as archive */
2434 if (lp_map_archive(SNUM(conn
)) ||
2435 lp_store_dos_attributes(SNUM(conn
))) {
2437 if (file_set_dosmode(conn
, smb_fname
,
2438 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2439 parent_dir
, true) == 0) {
2440 unx_mode
= smb_fname
->st
.st_ex_mode
;
2446 /* Determine sparse flag. */
2448 /* POSIX opens are sparse by default. */
2449 fsp
->is_sparse
= true;
2451 fsp
->is_sparse
= (file_existed
&&
2452 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2456 * Take care of inherited ACLs on created files - if default ACL not
2460 if (!posix_open
&& !file_existed
&& !def_acl
) {
2462 int saved_errno
= errno
; /* We might get ENOSYS in the next
2465 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2467 errno
= saved_errno
; /* Ignore ENOSYS */
2470 } else if (new_unx_mode
) {
2474 /* Attributes need changing. File already existed. */
2477 int saved_errno
= errno
; /* We might get ENOSYS in the
2479 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2481 if (ret
== -1 && errno
== ENOSYS
) {
2482 errno
= saved_errno
; /* Ignore ENOSYS */
2484 DEBUG(5, ("open_file_ntcreate: reset "
2485 "attributes of file %s to 0%o\n",
2486 smb_fname_str_dbg(smb_fname
),
2487 (unsigned int)new_unx_mode
));
2488 ret
= 0; /* Don't do the fchmod below. */
2493 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2494 DEBUG(5, ("open_file_ntcreate: failed to reset "
2495 "attributes of file %s to 0%o\n",
2496 smb_fname_str_dbg(smb_fname
),
2497 (unsigned int)new_unx_mode
));
2500 /* If this is a successful open, we must remove any deferred open
2503 del_deferred_open_entry(lck
, req
->mid
,
2504 sconn_server_id(req
->sconn
));
2508 return NT_STATUS_OK
;
2512 /****************************************************************************
2513 Open a file for for write to ensure that we can fchmod it.
2514 ****************************************************************************/
2516 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2517 struct smb_filename
*smb_fname
,
2518 files_struct
**result
)
2520 if (!VALID_STAT(smb_fname
->st
)) {
2521 return NT_STATUS_INVALID_PARAMETER
;
2524 return SMB_VFS_CREATE_FILE(
2527 0, /* root_dir_fid */
2528 smb_fname
, /* fname */
2529 FILE_WRITE_DATA
, /* access_mask */
2530 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2532 FILE_OPEN
, /* create_disposition*/
2533 0, /* create_options */
2534 0, /* file_attributes */
2535 INTERNAL_OPEN_ONLY
, /* oplock_request */
2536 0, /* allocation_size */
2537 0, /* private_flags */
2540 result
, /* result */
2544 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2545 struct smb_filename
*smb_dname
,
2546 uint32 file_attributes
)
2549 char *parent_dir
= NULL
;
2551 bool posix_open
= false;
2552 bool need_re_stat
= false;
2553 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2555 if(access_mask
& ~(conn
->share_access
)) {
2556 DEBUG(5,("mkdir_internal: failing share access "
2557 "%s\n", lp_servicename(SNUM(conn
))));
2558 return NT_STATUS_ACCESS_DENIED
;
2561 status
= check_name(conn
, smb_dname
->base_name
);
2562 if (!NT_STATUS_IS_OK(status
)) {
2566 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2568 return NT_STATUS_NO_MEMORY
;
2571 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2573 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2575 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2578 status
= check_parent_access(conn
,
2582 if(!NT_STATUS_IS_OK(status
)) {
2583 DEBUG(5,("mkdir_internal: check_parent_access "
2584 "on directory %s for path %s returned %s\n",
2586 smb_dname
->base_name
,
2587 nt_errstr(status
) ));
2591 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2592 return map_nt_error_from_unix(errno
);
2595 /* Ensure we're checking for a symlink here.... */
2596 /* We don't want to get caught by a symlink racer. */
2598 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2599 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2600 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2601 return map_nt_error_from_unix(errno
);
2604 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2605 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2606 smb_fname_str_dbg(smb_dname
)));
2607 return NT_STATUS_NOT_A_DIRECTORY
;
2610 if (lp_store_dos_attributes(SNUM(conn
))) {
2612 file_set_dosmode(conn
, smb_dname
,
2613 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2618 if (lp_inherit_perms(SNUM(conn
))) {
2619 inherit_access_posix_acl(conn
, parent_dir
,
2620 smb_dname
->base_name
, mode
);
2621 need_re_stat
= true;
2626 * Check if high bits should have been set,
2627 * then (if bits are missing): add them.
2628 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2631 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2632 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2633 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2634 (smb_dname
->st
.st_ex_mode
|
2635 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2636 need_re_stat
= true;
2640 /* Change the owner if required. */
2641 if (lp_inherit_owner(SNUM(conn
))) {
2642 change_dir_owner_to_parent(conn
, parent_dir
,
2643 smb_dname
->base_name
,
2645 need_re_stat
= true;
2649 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2650 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2651 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2652 return map_nt_error_from_unix(errno
);
2656 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2657 smb_dname
->base_name
);
2659 return NT_STATUS_OK
;
2662 /****************************************************************************
2663 Ensure we didn't get symlink raced on opening a directory.
2664 ****************************************************************************/
2666 bool check_same_stat(const SMB_STRUCT_STAT
*sbuf1
,
2667 const SMB_STRUCT_STAT
*sbuf2
)
2669 if (sbuf1
->st_ex_uid
!= sbuf2
->st_ex_uid
||
2670 sbuf1
->st_ex_gid
!= sbuf2
->st_ex_gid
||
2671 sbuf1
->st_ex_dev
!= sbuf2
->st_ex_dev
||
2672 sbuf1
->st_ex_ino
!= sbuf2
->st_ex_ino
) {
2678 /****************************************************************************
2679 Open a directory from an NT SMB call.
2680 ****************************************************************************/
2682 static NTSTATUS
open_directory(connection_struct
*conn
,
2683 struct smb_request
*req
,
2684 struct smb_filename
*smb_dname
,
2686 uint32 share_access
,
2687 uint32 create_disposition
,
2688 uint32 create_options
,
2689 uint32 file_attributes
,
2691 files_struct
**result
)
2693 files_struct
*fsp
= NULL
;
2694 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2695 struct share_mode_lock
*lck
= NULL
;
2697 struct timespec mtimespec
;
2700 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
2702 /* Ensure we have a directory attribute. */
2703 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2705 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2706 "share_access = 0x%x create_options = 0x%x, "
2707 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2708 smb_fname_str_dbg(smb_dname
),
2709 (unsigned int)access_mask
,
2710 (unsigned int)share_access
,
2711 (unsigned int)create_options
,
2712 (unsigned int)create_disposition
,
2713 (unsigned int)file_attributes
));
2715 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
2716 (conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
2717 is_ntfs_stream_smb_fname(smb_dname
)) {
2718 DEBUG(2, ("open_directory: %s is a stream name!\n",
2719 smb_fname_str_dbg(smb_dname
)));
2720 return NT_STATUS_NOT_A_DIRECTORY
;
2723 status
= smbd_calculate_access_mask(conn
, smb_dname
, dir_existed
,
2724 access_mask
, &access_mask
);
2725 if (!NT_STATUS_IS_OK(status
)) {
2726 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2727 "on file %s returned %s\n",
2728 smb_fname_str_dbg(smb_dname
),
2729 nt_errstr(status
)));
2733 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2734 !security_token_has_privilege(get_current_nttok(conn
),
2735 SEC_PRIV_SECURITY
)) {
2736 DEBUG(10, ("open_directory: open on %s "
2737 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2738 smb_fname_str_dbg(smb_dname
)));
2739 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2742 switch( create_disposition
) {
2746 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2749 info
= FILE_WAS_OPENED
;
2754 /* If directory exists error. If directory doesn't
2758 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
2759 DEBUG(2, ("open_directory: unable to create "
2760 "%s. Error was %s\n",
2761 smb_fname_str_dbg(smb_dname
),
2762 nt_errstr(status
)));
2766 status
= mkdir_internal(conn
, smb_dname
,
2769 if (!NT_STATUS_IS_OK(status
)) {
2770 DEBUG(2, ("open_directory: unable to create "
2771 "%s. Error was %s\n",
2772 smb_fname_str_dbg(smb_dname
),
2773 nt_errstr(status
)));
2777 info
= FILE_WAS_CREATED
;
2782 * If directory exists open. If directory doesn't
2787 status
= NT_STATUS_OK
;
2788 info
= FILE_WAS_OPENED
;
2790 status
= mkdir_internal(conn
, smb_dname
,
2793 if (NT_STATUS_IS_OK(status
)) {
2794 info
= FILE_WAS_CREATED
;
2796 /* Cope with create race. */
2797 if (!NT_STATUS_EQUAL(status
,
2798 NT_STATUS_OBJECT_NAME_COLLISION
)) {
2799 DEBUG(2, ("open_directory: unable to create "
2800 "%s. Error was %s\n",
2801 smb_fname_str_dbg(smb_dname
),
2802 nt_errstr(status
)));
2805 info
= FILE_WAS_OPENED
;
2811 case FILE_SUPERSEDE
:
2812 case FILE_OVERWRITE
:
2813 case FILE_OVERWRITE_IF
:
2815 DEBUG(5,("open_directory: invalid create_disposition "
2816 "0x%x for directory %s\n",
2817 (unsigned int)create_disposition
,
2818 smb_fname_str_dbg(smb_dname
)));
2819 return NT_STATUS_INVALID_PARAMETER
;
2822 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2823 DEBUG(5,("open_directory: %s is not a directory !\n",
2824 smb_fname_str_dbg(smb_dname
)));
2825 return NT_STATUS_NOT_A_DIRECTORY
;
2828 if (info
== FILE_WAS_OPENED
) {
2829 status
= smbd_check_open_rights(conn
, smb_dname
, access_mask
);
2830 if (!NT_STATUS_IS_OK(status
)) {
2831 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2832 "file %s failed with %s\n",
2833 smb_fname_str_dbg(smb_dname
),
2834 nt_errstr(status
)));
2839 status
= file_new(req
, conn
, &fsp
);
2840 if(!NT_STATUS_IS_OK(status
)) {
2845 * Setup the files_struct for it.
2848 fsp
->mode
= smb_dname
->st
.st_ex_mode
;
2849 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
2850 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
2851 fsp
->file_pid
= req
? req
->smbpid
: 0;
2852 fsp
->can_lock
= False
;
2853 fsp
->can_read
= False
;
2854 fsp
->can_write
= False
;
2856 fsp
->share_access
= share_access
;
2857 fsp
->fh
->private_options
= 0;
2859 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2861 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2862 fsp
->print_file
= NULL
;
2863 fsp
->modified
= False
;
2864 fsp
->oplock_type
= NO_OPLOCK
;
2865 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
2866 fsp
->is_directory
= True
;
2867 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
2868 status
= fsp_set_smb_fname(fsp
, smb_dname
);
2869 if (!NT_STATUS_IS_OK(status
)) {
2870 file_free(req
, fsp
);
2874 mtimespec
= smb_dname
->st
.st_ex_mtime
;
2877 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
2879 /* POSIX allows us to open a directory with O_RDONLY. */
2880 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
2882 if (!NT_STATUS_IS_OK(status
)) {
2883 DEBUG(5, ("open_directory: Could not open fd for "
2885 smb_fname_str_dbg(smb_dname
),
2886 nt_errstr(status
)));
2887 file_free(req
, fsp
);
2891 status
= vfs_stat_fsp(fsp
);
2892 if (!NT_STATUS_IS_OK(status
)) {
2894 file_free(req
, fsp
);
2898 /* Ensure there was no race condition. */
2899 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
2900 DEBUG(5,("open_directory: stat struct differs for "
2902 smb_fname_str_dbg(smb_dname
)));
2904 file_free(req
, fsp
);
2905 return NT_STATUS_ACCESS_DENIED
;
2908 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
2909 conn
->connectpath
, smb_dname
, &mtimespec
);
2912 DEBUG(0, ("open_directory: Could not get share mode lock for "
2913 "%s\n", smb_fname_str_dbg(smb_dname
)));
2915 file_free(req
, fsp
);
2916 return NT_STATUS_SHARING_VIOLATION
;
2919 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2920 access_mask
, share_access
,
2921 create_options
, &dir_existed
);
2923 if (!NT_STATUS_IS_OK(status
)) {
2926 file_free(req
, fsp
);
2930 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2931 req
? req
->mid
: 0, NO_OPLOCK
);
2933 /* For directories the delete on close bit at open time seems
2934 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2935 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2936 status
= can_set_delete_on_close(fsp
, 0);
2937 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
2940 file_free(req
, fsp
);
2944 if (NT_STATUS_IS_OK(status
)) {
2945 /* Note that here we set the *inital* delete on close flag,
2946 not the regular one. The magic gets handled in close. */
2947 fsp
->initial_delete_on_close
= True
;
2958 return NT_STATUS_OK
;
2961 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
2962 struct smb_filename
*smb_dname
)
2967 status
= SMB_VFS_CREATE_FILE(
2970 0, /* root_dir_fid */
2971 smb_dname
, /* fname */
2972 FILE_READ_ATTRIBUTES
, /* access_mask */
2973 FILE_SHARE_NONE
, /* share_access */
2974 FILE_CREATE
, /* create_disposition*/
2975 FILE_DIRECTORY_FILE
, /* create_options */
2976 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
2977 0, /* oplock_request */
2978 0, /* allocation_size */
2979 0, /* private_flags */
2985 if (NT_STATUS_IS_OK(status
)) {
2986 close_file(req
, fsp
, NORMAL_CLOSE
);
2992 /****************************************************************************
2993 Receive notification that one of our open files has been renamed by another
2995 ****************************************************************************/
2997 void msg_file_was_renamed(struct messaging_context
*msg
,
3000 struct server_id server_id
,
3003 struct smbd_server_connection
*sconn
;
3005 char *frm
= (char *)data
->data
;
3007 const char *sharepath
;
3008 const char *base_name
;
3009 const char *stream_name
;
3010 struct smb_filename
*smb_fname
= NULL
;
3011 size_t sp_len
, bn_len
;
3014 sconn
= msg_ctx_to_sconn(msg
);
3015 if (sconn
== NULL
) {
3016 DEBUG(1, ("could not find sconn\n"));
3020 if (data
->data
== NULL
3021 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3022 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3023 (int)data
->length
));
3027 /* Unpack the message. */
3028 pull_file_id_24(frm
, &id
);
3029 sharepath
= &frm
[24];
3030 sp_len
= strlen(sharepath
);
3031 base_name
= sharepath
+ sp_len
+ 1;
3032 bn_len
= strlen(base_name
);
3033 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3035 /* stream_name must always be NULL if there is no stream. */
3036 if (stream_name
[0] == '\0') {
3040 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
3041 stream_name
, NULL
, &smb_fname
);
3042 if (!NT_STATUS_IS_OK(status
)) {
3046 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3048 sharepath
, smb_fname_str_dbg(smb_fname
),
3049 file_id_string_tos(&id
)));
3051 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3052 fsp
= file_find_di_next(fsp
)) {
3053 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3055 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3056 fsp
->fnum
, fsp_str_dbg(fsp
),
3057 smb_fname_str_dbg(smb_fname
)));
3058 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3059 if (!NT_STATUS_IS_OK(status
)) {
3064 /* Now we have the complete path we can work out if this is
3065 actually within this share and adjust newname accordingly. */
3066 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3067 "not sharepath %s) "
3068 "fnum %d from %s -> %s\n",
3069 fsp
->conn
->connectpath
,
3073 smb_fname_str_dbg(smb_fname
)));
3077 TALLOC_FREE(smb_fname
);
3082 * If a main file is opened for delete, all streams need to be checked for
3083 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3084 * If that works, delete them all by setting the delete on close and close.
3087 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3090 struct stream_struct
*stream_info
= NULL
;
3091 files_struct
**streams
= NULL
;
3093 unsigned int num_streams
= 0;
3094 TALLOC_CTX
*frame
= talloc_stackframe();
3097 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3098 &num_streams
, &stream_info
);
3100 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3101 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3102 DEBUG(10, ("no streams around\n"));
3104 return NT_STATUS_OK
;
3107 if (!NT_STATUS_IS_OK(status
)) {
3108 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3109 nt_errstr(status
)));
3113 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3116 if (num_streams
== 0) {
3118 return NT_STATUS_OK
;
3121 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3122 if (streams
== NULL
) {
3123 DEBUG(0, ("talloc failed\n"));
3124 status
= NT_STATUS_NO_MEMORY
;
3128 for (i
=0; i
<num_streams
; i
++) {
3129 struct smb_filename
*smb_fname
= NULL
;
3131 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3136 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3137 stream_info
[i
].name
,
3139 if (!NT_STATUS_IS_OK(status
)) {
3143 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3144 DEBUG(10, ("Unable to stat stream: %s\n",
3145 smb_fname_str_dbg(smb_fname
)));
3148 status
= SMB_VFS_CREATE_FILE(
3151 0, /* root_dir_fid */
3152 smb_fname
, /* fname */
3153 DELETE_ACCESS
, /* access_mask */
3154 (FILE_SHARE_READ
| /* share_access */
3155 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3156 FILE_OPEN
, /* create_disposition*/
3157 0, /* create_options */
3158 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3159 0, /* oplock_request */
3160 0, /* allocation_size */
3161 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3164 &streams
[i
], /* result */
3167 if (!NT_STATUS_IS_OK(status
)) {
3168 DEBUG(10, ("Could not open stream %s: %s\n",
3169 smb_fname_str_dbg(smb_fname
),
3170 nt_errstr(status
)));
3172 TALLOC_FREE(smb_fname
);
3175 TALLOC_FREE(smb_fname
);
3179 * don't touch the variable "status" beyond this point :-)
3182 for (i
-= 1 ; i
>= 0; i
--) {
3183 if (streams
[i
] == NULL
) {
3187 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3188 fsp_str_dbg(streams
[i
])));
3189 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3198 * Wrapper around open_file_ntcreate and open_directory
3201 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3202 struct smb_request
*req
,
3203 struct smb_filename
*smb_fname
,
3204 uint32_t access_mask
,
3205 uint32_t share_access
,
3206 uint32_t create_disposition
,
3207 uint32_t create_options
,
3208 uint32_t file_attributes
,
3209 uint32_t oplock_request
,
3210 uint64_t allocation_size
,
3211 uint32_t private_flags
,
3212 struct security_descriptor
*sd
,
3213 struct ea_list
*ea_list
,
3215 files_struct
**result
,
3218 int info
= FILE_WAS_OPENED
;
3219 files_struct
*base_fsp
= NULL
;
3220 files_struct
*fsp
= NULL
;
3223 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3224 "file_attributes = 0x%x, share_access = 0x%x, "
3225 "create_disposition = 0x%x create_options = 0x%x "
3226 "oplock_request = 0x%x private_flags = 0x%x "
3227 "ea_list = 0x%p, sd = 0x%p, "
3229 (unsigned int)access_mask
,
3230 (unsigned int)file_attributes
,
3231 (unsigned int)share_access
,
3232 (unsigned int)create_disposition
,
3233 (unsigned int)create_options
,
3234 (unsigned int)oplock_request
,
3235 (unsigned int)private_flags
,
3236 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3238 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3239 status
= NT_STATUS_NOT_SUPPORTED
;
3243 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3244 status
= NT_STATUS_INVALID_PARAMETER
;
3249 oplock_request
|= INTERNAL_OPEN_ONLY
;
3252 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3253 && (access_mask
& DELETE_ACCESS
)
3254 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3256 * We can't open a file with DELETE access if any of the
3257 * streams is open without FILE_SHARE_DELETE
3259 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3261 if (!NT_STATUS_IS_OK(status
)) {
3266 /* This is the correct thing to do (check every time) but can_delete
3267 * is expensive (it may have to read the parent directory
3268 * permissions). So for now we're not doing it unless we have a strong
3269 * hint the client is really going to delete this file. If the client
3270 * is forcing FILE_CREATE let the filesystem take care of the
3273 /* Setting FILE_SHARE_DELETE is the hint. */
3275 if ((create_disposition
!= FILE_CREATE
)
3276 && (access_mask
& DELETE_ACCESS
)
3277 && (!(can_delete_file_in_directory(conn
, smb_fname
) ||
3278 can_access_file_acl(conn
, smb_fname
, DELETE_ACCESS
)))) {
3279 status
= NT_STATUS_ACCESS_DENIED
;
3280 DEBUG(10,("create_file_unixpath: open file %s "
3281 "for delete ACCESS_DENIED\n",
3282 smb_fname_str_dbg(smb_fname
)));
3286 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3287 !security_token_has_privilege(get_current_nttok(conn
),
3288 SEC_PRIV_SECURITY
)) {
3289 DEBUG(10, ("create_file_unixpath: open on %s "
3290 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3291 smb_fname_str_dbg(smb_fname
)));
3292 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3296 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3297 && is_ntfs_stream_smb_fname(smb_fname
)
3298 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3299 uint32 base_create_disposition
;
3300 struct smb_filename
*smb_fname_base
= NULL
;
3302 if (create_options
& FILE_DIRECTORY_FILE
) {
3303 status
= NT_STATUS_NOT_A_DIRECTORY
;
3307 switch (create_disposition
) {
3309 base_create_disposition
= FILE_OPEN
;
3312 base_create_disposition
= FILE_OPEN_IF
;
3316 /* Create an smb_filename with stream_name == NULL. */
3317 status
= create_synthetic_smb_fname(talloc_tos(),
3318 smb_fname
->base_name
,
3321 if (!NT_STATUS_IS_OK(status
)) {
3325 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3326 DEBUG(10, ("Unable to stat stream: %s\n",
3327 smb_fname_str_dbg(smb_fname_base
)));
3330 /* Open the base file. */
3331 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3334 | FILE_SHARE_DELETE
,
3335 base_create_disposition
,
3336 0, 0, 0, 0, 0, NULL
, NULL
,
3338 TALLOC_FREE(smb_fname_base
);
3340 if (!NT_STATUS_IS_OK(status
)) {
3341 DEBUG(10, ("create_file_unixpath for base %s failed: "
3342 "%s\n", smb_fname
->base_name
,
3343 nt_errstr(status
)));
3346 /* we don't need to low level fd */
3351 * If it's a request for a directory open, deal with it separately.
3354 if (create_options
& FILE_DIRECTORY_FILE
) {
3356 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3357 status
= NT_STATUS_INVALID_PARAMETER
;
3361 /* Can't open a temp directory. IFS kit test. */
3362 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3363 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3364 status
= NT_STATUS_INVALID_PARAMETER
;
3369 * We will get a create directory here if the Win32
3370 * app specified a security descriptor in the
3371 * CreateDirectory() call.
3375 status
= open_directory(
3376 conn
, req
, smb_fname
, access_mask
, share_access
,
3377 create_disposition
, create_options
, file_attributes
,
3382 * Ordinary file case.
3385 status
= file_new(req
, conn
, &fsp
);
3386 if(!NT_STATUS_IS_OK(status
)) {
3390 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3391 if (!NT_STATUS_IS_OK(status
)) {
3396 * We're opening the stream element of a base_fsp
3397 * we already opened. Set up the base_fsp pointer.
3400 fsp
->base_fsp
= base_fsp
;
3403 status
= open_file_ntcreate(conn
,
3415 if(!NT_STATUS_IS_OK(status
)) {
3416 file_free(req
, fsp
);
3420 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3422 /* A stream open never opens a directory */
3425 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3430 * Fail the open if it was explicitly a non-directory
3434 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3435 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3440 status
= open_directory(
3441 conn
, req
, smb_fname
, access_mask
,
3442 share_access
, create_disposition
,
3443 create_options
, file_attributes
,
3448 if (!NT_STATUS_IS_OK(status
)) {
3452 fsp
->base_fsp
= base_fsp
;
3455 * According to the MS documentation, the only time the security
3456 * descriptor is applied to the opened file is iff we *created* the
3457 * file; an existing file stays the same.
3459 * Also, it seems (from observation) that you can open the file with
3460 * any access mask but you can still write the sd. We need to override
3461 * the granted access before we call set_sd
3462 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3465 if ((sd
!= NULL
) && (info
== FILE_WAS_CREATED
)
3466 && lp_nt_acl_support(SNUM(conn
))) {
3468 uint32_t sec_info_sent
;
3469 uint32_t saved_access_mask
= fsp
->access_mask
;
3471 sec_info_sent
= get_sec_info(sd
);
3473 fsp
->access_mask
= FILE_GENERIC_ALL
;
3475 /* Convert all the generic bits. */
3476 security_acl_map_generic(sd
->dacl
, &file_generic_mapping
);
3477 security_acl_map_generic(sd
->sacl
, &file_generic_mapping
);
3479 if (sec_info_sent
& (SECINFO_OWNER
|
3483 status
= SMB_VFS_FSET_NT_ACL(fsp
, sec_info_sent
, sd
);
3486 fsp
->access_mask
= saved_access_mask
;
3488 if (!NT_STATUS_IS_OK(status
)) {
3493 if ((ea_list
!= NULL
) &&
3494 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3495 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3496 if (!NT_STATUS_IS_OK(status
)) {
3501 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3502 status
= NT_STATUS_ACCESS_DENIED
;
3506 /* Save the requested allocation size. */
3507 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3509 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3510 fsp
->initial_allocation_size
= smb_roundup(
3511 fsp
->conn
, allocation_size
);
3512 if (fsp
->is_directory
) {
3513 /* Can't set allocation size on a directory. */
3514 status
= NT_STATUS_ACCESS_DENIED
;
3517 if (vfs_allocate_file_space(
3518 fsp
, fsp
->initial_allocation_size
) == -1) {
3519 status
= NT_STATUS_DISK_FULL
;
3523 fsp
->initial_allocation_size
= smb_roundup(
3524 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3528 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3531 if (pinfo
!= NULL
) {
3535 smb_fname
->st
= fsp
->fsp_name
->st
;
3537 return NT_STATUS_OK
;
3540 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3543 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3545 * The close_file below will close
3550 close_file(req
, fsp
, ERROR_CLOSE
);
3553 if (base_fsp
!= NULL
) {
3554 close_file(req
, base_fsp
, ERROR_CLOSE
);
3561 * Calculate the full path name given a relative fid.
3563 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
3564 struct smb_request
*req
,
3565 uint16_t root_dir_fid
,
3566 const struct smb_filename
*smb_fname
,
3567 struct smb_filename
**smb_fname_out
)
3569 files_struct
*dir_fsp
;
3570 char *parent_fname
= NULL
;
3571 char *new_base_name
= NULL
;
3574 if (root_dir_fid
== 0 || !smb_fname
) {
3575 status
= NT_STATUS_INTERNAL_ERROR
;
3579 dir_fsp
= file_fsp(req
, root_dir_fid
);
3581 if (dir_fsp
== NULL
) {
3582 status
= NT_STATUS_INVALID_HANDLE
;
3586 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
3587 status
= NT_STATUS_INVALID_HANDLE
;
3591 if (!dir_fsp
->is_directory
) {
3594 * Check to see if this is a mac fork of some kind.
3597 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
3598 is_ntfs_stream_smb_fname(smb_fname
)) {
3599 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3604 we need to handle the case when we get a
3605 relative open relative to a file and the
3606 pathname is blank - this is a reopen!
3607 (hint from demyn plantenberg)
3610 status
= NT_STATUS_INVALID_HANDLE
;
3614 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
3616 * We're at the toplevel dir, the final file name
3617 * must not contain ./, as this is filtered out
3618 * normally by srvstr_get_path and unix_convert
3619 * explicitly rejects paths containing ./.
3621 parent_fname
= talloc_strdup(talloc_tos(), "");
3622 if (parent_fname
== NULL
) {
3623 status
= NT_STATUS_NO_MEMORY
;
3627 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
3630 * Copy in the base directory name.
3633 parent_fname
= talloc_array(talloc_tos(), char,
3635 if (parent_fname
== NULL
) {
3636 status
= NT_STATUS_NO_MEMORY
;
3639 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
3643 * Ensure it ends in a '/'.
3644 * We used TALLOC_SIZE +2 to add space for the '/'.
3648 && (parent_fname
[dir_name_len
-1] != '\\')
3649 && (parent_fname
[dir_name_len
-1] != '/')) {
3650 parent_fname
[dir_name_len
] = '/';
3651 parent_fname
[dir_name_len
+1] = '\0';
3655 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
3656 smb_fname
->base_name
);
3657 if (new_base_name
== NULL
) {
3658 status
= NT_STATUS_NO_MEMORY
;
3662 status
= filename_convert(req
,
3664 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
3669 if (!NT_STATUS_IS_OK(status
)) {
3674 TALLOC_FREE(parent_fname
);
3675 TALLOC_FREE(new_base_name
);
3679 NTSTATUS
create_file_default(connection_struct
*conn
,
3680 struct smb_request
*req
,
3681 uint16_t root_dir_fid
,
3682 struct smb_filename
*smb_fname
,
3683 uint32_t access_mask
,
3684 uint32_t share_access
,
3685 uint32_t create_disposition
,
3686 uint32_t create_options
,
3687 uint32_t file_attributes
,
3688 uint32_t oplock_request
,
3689 uint64_t allocation_size
,
3690 uint32_t private_flags
,
3691 struct security_descriptor
*sd
,
3692 struct ea_list
*ea_list
,
3693 files_struct
**result
,
3696 int info
= FILE_WAS_OPENED
;
3697 files_struct
*fsp
= NULL
;
3699 bool stream_name
= false;
3701 DEBUG(10,("create_file: access_mask = 0x%x "
3702 "file_attributes = 0x%x, share_access = 0x%x, "
3703 "create_disposition = 0x%x create_options = 0x%x "
3704 "oplock_request = 0x%x "
3705 "private_flags = 0x%x "
3706 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3708 (unsigned int)access_mask
,
3709 (unsigned int)file_attributes
,
3710 (unsigned int)share_access
,
3711 (unsigned int)create_disposition
,
3712 (unsigned int)create_options
,
3713 (unsigned int)oplock_request
,
3714 (unsigned int)private_flags
,
3715 (unsigned int)root_dir_fid
,
3716 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3719 * Calculate the filename from the root_dir_if if necessary.
3722 if (root_dir_fid
!= 0) {
3723 struct smb_filename
*smb_fname_out
= NULL
;
3724 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
3725 smb_fname
, &smb_fname_out
);
3726 if (!NT_STATUS_IS_OK(status
)) {
3729 smb_fname
= smb_fname_out
;
3733 * Check to see if this is a mac fork of some kind.
3736 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
3738 enum FAKE_FILE_TYPE fake_file_type
;
3740 fake_file_type
= is_fake_file(smb_fname
);
3742 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
3745 * Here we go! support for changing the disk quotas
3748 * We need to fake up to open this MAGIC QUOTA file
3749 * and return a valid FID.
3751 * w2k close this file directly after openening xp
3752 * also tries a QUERY_FILE_INFO on the file and then
3755 status
= open_fake_file(req
, conn
, req
->vuid
,
3756 fake_file_type
, smb_fname
,
3758 if (!NT_STATUS_IS_OK(status
)) {
3762 ZERO_STRUCT(smb_fname
->st
);
3766 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
3767 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3772 /* All file access must go through check_name() */
3774 status
= check_name(conn
, smb_fname
->base_name
);
3775 if (!NT_STATUS_IS_OK(status
)) {
3779 if (stream_name
&& is_ntfs_default_stream_smb_fname(smb_fname
)) {
3781 smb_fname
->stream_name
= NULL
;
3782 /* We have to handle this error here. */
3783 if (create_options
& FILE_DIRECTORY_FILE
) {
3784 status
= NT_STATUS_NOT_A_DIRECTORY
;
3787 if (lp_posix_pathnames()) {
3788 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
3790 ret
= SMB_VFS_STAT(conn
, smb_fname
);
3793 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
3794 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3799 status
= create_file_unixpath(
3800 conn
, req
, smb_fname
, access_mask
, share_access
,
3801 create_disposition
, create_options
, file_attributes
,
3802 oplock_request
, allocation_size
, private_flags
,
3806 if (!NT_STATUS_IS_OK(status
)) {
3811 DEBUG(10, ("create_file: info=%d\n", info
));
3814 if (pinfo
!= NULL
) {
3817 return NT_STATUS_OK
;
3820 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
3823 close_file(req
, fsp
, ERROR_CLOSE
);