2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
33 extern const struct generic_mapping file_generic_mapping
;
35 struct deferred_open_record
{
36 bool delayed_for_oplocks
;
40 /****************************************************************************
41 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
42 ****************************************************************************/
44 NTSTATUS
smb1_file_se_access_check(struct connection_struct
*conn
,
45 const struct security_descriptor
*sd
,
46 const struct security_token
*token
,
47 uint32_t access_desired
,
48 uint32_t *access_granted
)
52 if (get_current_uid(conn
) == (uid_t
)0) {
53 /* I'm sorry sir, I didn't know you were root... */
54 *access_granted
= access_desired
;
55 if (access_desired
& SEC_FLAG_MAXIMUM_ALLOWED
) {
56 *access_granted
|= FILE_GENERIC_ALL
;
61 return se_access_check(sd
,
63 (access_desired
& ~FILE_READ_ATTRIBUTES
),
67 /****************************************************************************
68 Check if we have open rights.
69 ****************************************************************************/
71 NTSTATUS
smbd_check_open_rights(struct connection_struct
*conn
,
72 const struct smb_filename
*smb_fname
,
74 uint32_t *access_granted
)
76 /* Check if we have rights to open. */
78 struct security_descriptor
*sd
= NULL
;
80 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
85 if (!NT_STATUS_IS_OK(status
)) {
86 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
88 smb_fname_str_dbg(smb_fname
),
93 status
= smb1_file_se_access_check(conn
,
95 get_current_nttok(conn
),
99 DEBUG(10,("smbd_check_open_rights: file %s requesting "
100 "0x%x returning 0x%x (%s)\n",
101 smb_fname_str_dbg(smb_fname
),
102 (unsigned int)access_mask
,
103 (unsigned int)*access_granted
,
104 nt_errstr(status
) ));
106 if (!NT_STATUS_IS_OK(status
)) {
107 if (DEBUGLEVEL
>= 10) {
108 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
109 smb_fname_str_dbg(smb_fname
) ));
110 NDR_PRINT_DEBUG(security_descriptor
, sd
);
119 /****************************************************************************
120 fd support routines - attempt to do a dos_open.
121 ****************************************************************************/
123 static NTSTATUS
fd_open(struct connection_struct
*conn
,
128 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
129 NTSTATUS status
= NT_STATUS_OK
;
133 * Never follow symlinks on a POSIX client. The
134 * client should be doing this.
137 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
142 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
143 if (fsp
->fh
->fd
== -1) {
144 status
= map_nt_error_from_unix(errno
);
145 if (errno
== EMFILE
) {
146 static time_t last_warned
= 0L;
148 if (time((time_t *) NULL
) > last_warned
) {
149 DEBUG(0,("Too many open files, unable "
150 "to open more! smbd's max "
152 lp_max_open_files()));
153 last_warned
= time((time_t *) NULL
);
159 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
160 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
161 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
166 /****************************************************************************
167 Close the file associated with a fsp.
168 ****************************************************************************/
170 NTSTATUS
fd_close(files_struct
*fsp
)
177 if (fsp
->fh
->fd
== -1) {
178 return NT_STATUS_OK
; /* What we used to call a stat open. */
180 if (fsp
->fh
->ref_count
> 1) {
181 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
184 ret
= SMB_VFS_CLOSE(fsp
);
187 return map_nt_error_from_unix(errno
);
192 /****************************************************************************
193 Change the ownership of a file to that of the parent directory.
194 Do this by fd if possible.
195 ****************************************************************************/
197 void change_file_owner_to_parent(connection_struct
*conn
,
198 const char *inherit_from_dir
,
201 struct smb_filename
*smb_fname_parent
= NULL
;
205 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
206 NULL
, NULL
, &smb_fname_parent
);
207 if (!NT_STATUS_IS_OK(status
)) {
211 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
213 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
214 "directory %s. Error was %s\n",
215 smb_fname_str_dbg(smb_fname_parent
),
217 TALLOC_FREE(smb_fname_parent
);
221 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
222 /* Already this uid - no need to change. */
223 DEBUG(10,("change_file_owner_to_parent: file %s "
224 "is already owned by uid %d\n",
226 (int)fsp
->fsp_name
->st
.st_ex_uid
));
227 TALLOC_FREE(smb_fname_parent
);
232 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
235 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
236 "file %s to parent directory uid %u. Error "
237 "was %s\n", fsp_str_dbg(fsp
),
238 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
242 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
243 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
244 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
246 TALLOC_FREE(smb_fname_parent
);
249 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
250 const char *inherit_from_dir
,
252 SMB_STRUCT_STAT
*psbuf
)
254 struct smb_filename
*smb_fname_parent
= NULL
;
255 struct smb_filename
*smb_fname_cwd
= NULL
;
256 char *saved_dir
= NULL
;
257 TALLOC_CTX
*ctx
= talloc_tos();
258 NTSTATUS status
= NT_STATUS_OK
;
261 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
263 if (!NT_STATUS_IS_OK(status
)) {
267 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
269 status
= map_nt_error_from_unix(errno
);
270 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
271 "directory %s. Error was %s\n",
272 smb_fname_str_dbg(smb_fname_parent
),
277 /* We've already done an lstat into psbuf, and we know it's a
278 directory. If we can cd into the directory and the dev/ino
279 are the same then we can safely chown without races as
280 we're locking the directory in place by being in it. This
281 should work on any UNIX (thanks tridge :-). JRA.
284 saved_dir
= vfs_GetWd(ctx
,conn
);
286 status
= map_nt_error_from_unix(errno
);
287 DEBUG(0,("change_dir_owner_to_parent: failed to get "
288 "current working directory. Error was %s\n",
293 /* Chdir into the new path. */
294 if (vfs_ChDir(conn
, fname
) == -1) {
295 status
= map_nt_error_from_unix(errno
);
296 DEBUG(0,("change_dir_owner_to_parent: failed to change "
297 "current working directory to %s. Error "
298 "was %s\n", fname
, strerror(errno
) ));
302 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
304 if (!NT_STATUS_IS_OK(status
)) {
308 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
310 status
= map_nt_error_from_unix(errno
);
311 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
312 "directory '.' (%s) Error was %s\n",
313 fname
, strerror(errno
)));
317 /* Ensure we're pointing at the same place. */
318 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
319 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
||
320 smb_fname_cwd
->st
.st_ex_mode
!= psbuf
->st_ex_mode
) {
321 DEBUG(0,("change_dir_owner_to_parent: "
322 "device/inode/mode on directory %s changed. "
323 "Refusing to chown !\n", fname
));
324 status
= NT_STATUS_ACCESS_DENIED
;
328 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
329 /* Already this uid - no need to change. */
330 DEBUG(10,("change_dir_owner_to_parent: directory %s "
331 "is already owned by uid %d\n",
333 (int)smb_fname_cwd
->st
.st_ex_uid
));
334 status
= NT_STATUS_OK
;
339 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
343 status
= map_nt_error_from_unix(errno
);
344 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
345 "directory %s to parent directory uid %u. "
346 "Error was %s\n", fname
,
347 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
352 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
353 "directory %s to parent directory uid %u.\n",
354 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
357 vfs_ChDir(conn
,saved_dir
);
359 TALLOC_FREE(smb_fname_parent
);
360 TALLOC_FREE(smb_fname_cwd
);
364 /****************************************************************************
366 ****************************************************************************/
368 static NTSTATUS
open_file(files_struct
*fsp
,
369 connection_struct
*conn
,
370 struct smb_request
*req
,
371 const char *parent_dir
,
374 uint32 access_mask
, /* client requested access mask. */
375 uint32 open_access_mask
) /* what we're actually using in the open. */
377 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
378 NTSTATUS status
= NT_STATUS_OK
;
379 int accmode
= (flags
& O_ACCMODE
);
380 int local_flags
= flags
;
381 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
386 /* Check permissions */
389 * This code was changed after seeing a client open request
390 * containing the open mode of (DENY_WRITE/read-only) with
391 * the 'create if not exist' bit set. The previous code
392 * would fail to open the file read only on a read-only share
393 * as it was checking the flags parameter directly against O_RDONLY,
394 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
398 if (!CAN_WRITE(conn
)) {
399 /* It's a read-only share - fail if we wanted to write. */
400 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
401 DEBUG(3,("Permission denied opening %s\n",
402 smb_fname_str_dbg(smb_fname
)));
403 return NT_STATUS_ACCESS_DENIED
;
404 } else if(flags
& O_CREAT
) {
405 /* We don't want to write - but we must make sure that
406 O_CREAT doesn't create the file if we have write
407 access into the directory.
409 flags
&= ~(O_CREAT
|O_EXCL
);
410 local_flags
&= ~(O_CREAT
|O_EXCL
);
415 * This little piece of insanity is inspired by the
416 * fact that an NT client can open a file for O_RDONLY,
417 * but set the create disposition to FILE_EXISTS_TRUNCATE.
418 * If the client *can* write to the file, then it expects to
419 * truncate the file, even though it is opening for readonly.
420 * Quicken uses this stupid trick in backup file creation...
421 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
422 * for helping track this one down. It didn't bite us in 2.0.x
423 * as we always opened files read-write in that release. JRA.
426 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
427 DEBUG(10,("open_file: truncate requested on read-only open "
428 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
429 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
432 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
433 (!file_existed
&& (local_flags
& O_CREAT
)) ||
434 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
438 * We can't actually truncate here as the file may be locked.
439 * open_file_ntcreate will take care of the truncate later. JRA.
442 local_flags
&= ~O_TRUNC
;
444 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
446 * We would block on opening a FIFO with no one else on the
447 * other end. Do what we used to do and add O_NONBLOCK to the
451 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
452 local_flags
|= O_NONBLOCK
;
456 /* Don't create files with Microsoft wildcard characters. */
459 * wildcard characters are allowed in stream names
460 * only test the basefilename
462 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
464 wild
= smb_fname
->base_name
;
466 if ((local_flags
& O_CREAT
) && !file_existed
&&
468 return NT_STATUS_OBJECT_NAME_INVALID
;
471 /* Actually do the open */
472 status
= fd_open(conn
, fsp
, local_flags
, unx_mode
);
473 if (!NT_STATUS_IS_OK(status
)) {
474 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
475 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
476 nt_errstr(status
),local_flags
,flags
));
480 if ((local_flags
& O_CREAT
) && !file_existed
) {
482 /* Inherit the ACL if required */
483 if (lp_inherit_perms(SNUM(conn
))) {
484 inherit_access_posix_acl(conn
, parent_dir
,
485 smb_fname
->base_name
,
489 /* Change the owner if required. */
490 if (lp_inherit_owner(SNUM(conn
))) {
491 change_file_owner_to_parent(conn
, parent_dir
,
495 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
496 FILE_NOTIFY_CHANGE_FILE_NAME
,
497 smb_fname
->base_name
);
501 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
503 uint32_t access_granted
= 0;
505 status
= smbd_check_open_rights(conn
,
509 if (!NT_STATUS_IS_OK(status
)) {
510 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
512 * On NT_STATUS_ACCESS_DENIED, access_granted
513 * contains the denied bits.
516 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
517 (access_granted
& FILE_WRITE_ATTRIBUTES
) &&
518 (lp_map_readonly(SNUM(conn
)) ||
519 lp_map_archive(SNUM(conn
)) ||
520 lp_map_hidden(SNUM(conn
)) ||
521 lp_map_system(SNUM(conn
)))) {
522 access_granted
&= ~FILE_WRITE_ATTRIBUTES
;
524 DEBUG(10,("open_file: "
533 if ((access_mask
& DELETE_ACCESS
) &&
534 (access_granted
& DELETE_ACCESS
) &&
535 can_delete_file_in_directory(conn
,
537 /* Were we trying to do a stat open
538 * for delete and didn't get DELETE
539 * access (only) ? Check if the
540 * directory allows DELETE_CHILD.
542 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
545 access_granted
&= ~DELETE_ACCESS
;
547 DEBUG(10,("open_file: "
555 if (access_granted
!= 0) {
556 DEBUG(10,("open_file: Access "
563 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
565 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
566 /* This is a POSIX stat open for delete
567 * or rename on a symlink that points
569 DEBUG(10,("open_file: allowing POSIX "
570 "open on bad symlink %s\n",
574 DEBUG(10,("open_file: "
575 "smbd_check_open_rights on file "
577 smb_fname_str_dbg(smb_fname
),
578 nt_errstr(status
) ));
588 if (fsp
->fh
->fd
== -1) {
589 ret
= SMB_VFS_STAT(conn
, smb_fname
);
591 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
592 /* If we have an fd, this stat should succeed. */
594 DEBUG(0,("Error doing fstat on open file %s "
596 smb_fname_str_dbg(smb_fname
),
601 /* For a non-io open, this stat failing means file not found. JRA */
603 status
= map_nt_error_from_unix(errno
);
610 * POSIX allows read-only opens of directories. We don't
611 * want to do this (we use a different code path for this)
612 * so catch a directory open and return an EISDIR. JRA.
615 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
618 return NT_STATUS_FILE_IS_A_DIRECTORY
;
621 fsp
->mode
= smb_fname
->st
.st_ex_mode
;
622 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
623 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
624 fsp
->file_pid
= req
? req
->smbpid
: 0;
625 fsp
->can_lock
= True
;
626 fsp
->can_read
= (access_mask
& (FILE_READ_DATA
)) ? True
: False
;
627 if (!CAN_WRITE(conn
)) {
628 fsp
->can_write
= False
;
630 fsp
->can_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
633 fsp
->print_file
= NULL
;
634 fsp
->modified
= False
;
635 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
636 fsp
->is_directory
= False
;
637 if (conn
->aio_write_behind_list
&&
638 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
639 conn
->case_sensitive
)) {
640 fsp
->aio_write_behind
= True
;
643 fsp
->wcp
= NULL
; /* Write cache pointer. */
645 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
646 conn
->session_info
->unix_name
,
647 smb_fname_str_dbg(smb_fname
),
648 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
649 conn
->num_files_open
));
655 /*******************************************************************
656 Return True if the filename is one of the special executable types.
657 ********************************************************************/
659 bool is_executable(const char *fname
)
661 if ((fname
= strrchr_m(fname
,'.'))) {
662 if (strequal(fname
,".com") ||
663 strequal(fname
,".dll") ||
664 strequal(fname
,".exe") ||
665 strequal(fname
,".sym")) {
672 /****************************************************************************
673 Check if we can open a file with a share mode.
674 Returns True if conflict, False if not.
675 ****************************************************************************/
677 static bool share_conflict(struct share_mode_entry
*entry
,
681 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
682 "entry->share_access = 0x%x, "
683 "entry->private_options = 0x%x\n",
684 (unsigned int)entry
->access_mask
,
685 (unsigned int)entry
->share_access
,
686 (unsigned int)entry
->private_options
));
688 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
689 (unsigned int)access_mask
, (unsigned int)share_access
));
691 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
695 DELETE_ACCESS
)) == 0) {
696 DEBUG(10,("share_conflict: No conflict due to "
697 "entry->access_mask = 0x%x\n",
698 (unsigned int)entry
->access_mask
));
702 if ((access_mask
& (FILE_WRITE_DATA
|
706 DELETE_ACCESS
)) == 0) {
707 DEBUG(10,("share_conflict: No conflict due to "
708 "access_mask = 0x%x\n",
709 (unsigned int)access_mask
));
713 #if 1 /* JRA TEST - Superdebug. */
714 #define CHECK_MASK(num, am, right, sa, share) \
715 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
716 (unsigned int)(num), (unsigned int)(am), \
717 (unsigned int)(right), (unsigned int)(am)&(right) )); \
718 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
719 (unsigned int)(num), (unsigned int)(sa), \
720 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
721 if (((am) & (right)) && !((sa) & (share))) { \
722 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
723 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
724 (unsigned int)(share) )); \
728 #define CHECK_MASK(num, am, right, sa, share) \
729 if (((am) & (right)) && !((sa) & (share))) { \
730 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
731 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
732 (unsigned int)(share) )); \
737 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
738 share_access
, FILE_SHARE_WRITE
);
739 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
740 entry
->share_access
, FILE_SHARE_WRITE
);
742 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
743 share_access
, FILE_SHARE_READ
);
744 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
745 entry
->share_access
, FILE_SHARE_READ
);
747 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
748 share_access
, FILE_SHARE_DELETE
);
749 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
750 entry
->share_access
, FILE_SHARE_DELETE
);
752 DEBUG(10,("share_conflict: No conflict.\n"));
756 #if defined(DEVELOPER)
757 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
759 struct share_mode_entry
*share_entry
)
763 if (!procid_is_me(&share_entry
->pid
)) {
767 if (is_deferred_open_entry(share_entry
) &&
768 !open_was_deferred(share_entry
->op_mid
)) {
769 char *str
= talloc_asprintf(talloc_tos(),
770 "Got a deferred entry without a request: "
772 share_mode_str(talloc_tos(), num
, share_entry
));
776 if (!is_valid_share_mode_entry(share_entry
)) {
780 fsp
= file_find_dif(sconn
, share_entry
->id
,
781 share_entry
->share_file_id
);
783 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
784 share_mode_str(talloc_tos(), num
, share_entry
) ));
785 smb_panic("validate_my_share_entries: Cannot match a "
786 "share entry with an open file\n");
789 if (is_deferred_open_entry(share_entry
) ||
790 is_unused_share_mode_entry(share_entry
)) {
794 if ((share_entry
->op_type
== NO_OPLOCK
) &&
795 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
796 /* Someone has already written to it, but I haven't yet
801 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
810 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
811 share_mode_str(talloc_tos(), num
, share_entry
) ));
812 str
= talloc_asprintf(talloc_tos(),
813 "validate_my_share_entries: "
814 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
815 fsp
->fsp_name
->base_name
,
816 (unsigned int)fsp
->oplock_type
,
817 (unsigned int)share_entry
->op_type
);
823 bool is_stat_open(uint32 access_mask
)
825 return (access_mask
&&
826 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
827 FILE_WRITE_ATTRIBUTES
))==0) &&
828 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
829 FILE_WRITE_ATTRIBUTES
)) != 0));
832 /****************************************************************************
833 Deal with share modes
834 Invarient: Share mode must be locked on entry and exit.
835 Returns -1 on error, or number of share modes on success (may be zero).
836 ****************************************************************************/
838 static NTSTATUS
open_mode_check(connection_struct
*conn
,
839 struct share_mode_lock
*lck
,
843 uint32 create_options
,
848 if(lck
->num_share_modes
== 0) {
852 *file_existed
= True
;
854 /* A delete on close prohibits everything */
856 if (is_delete_on_close_set(lck
, name_hash
)) {
857 return NT_STATUS_DELETE_PENDING
;
860 if (is_stat_open(access_mask
)) {
861 /* Stat open that doesn't trigger oplock breaks or share mode
862 * checks... ! JRA. */
867 * Check if the share modes will give us access.
870 #if defined(DEVELOPER)
871 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
872 validate_my_share_entries(conn
->sconn
, i
,
873 &lck
->share_modes
[i
]);
877 if (!lp_share_modes(SNUM(conn
))) {
881 /* Now we check the share modes, after any oplock breaks. */
882 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
884 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
888 /* someone else has a share lock on it, check to see if we can
890 if (share_conflict(&lck
->share_modes
[i
],
891 access_mask
, share_access
)) {
892 return NT_STATUS_SHARING_VIOLATION
;
899 static bool is_delete_request(files_struct
*fsp
) {
900 return ((fsp
->access_mask
== DELETE_ACCESS
) &&
901 (fsp
->oplock_type
== NO_OPLOCK
));
905 * Send a break message to the oplock holder and delay the open for
909 static NTSTATUS
send_break_message(files_struct
*fsp
,
910 struct share_mode_entry
*exclusive
,
915 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
917 DEBUG(10, ("Sending break request to PID %s\n",
918 procid_str_static(&exclusive
->pid
)));
919 exclusive
->op_mid
= mid
;
921 /* Create the message. */
922 share_mode_entry_to_message(msg
, exclusive
);
924 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
925 don't want this set in the share mode struct pointed to by lck. */
927 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
928 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
929 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
932 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
933 MSG_SMB_BREAK_REQUEST
,
935 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
936 if (!NT_STATUS_IS_OK(status
)) {
937 DEBUG(3, ("Could not send oplock break message: %s\n",
945 * Return share_mode_entry pointers for :
946 * 1). Batch oplock entry.
947 * 2). Batch or exclusive oplock entry (may be identical to #1).
948 * bool have_level2_oplock
949 * bool have_no_oplock.
950 * Do internal consistency checks on the share mode for a file.
953 static void find_oplock_types(struct share_mode_lock
*lck
,
954 struct share_mode_entry
**pp_batch
,
955 struct share_mode_entry
**pp_ex_or_batch
,
962 *pp_ex_or_batch
= NULL
;
964 *got_no_oplock
= false;
966 for (i
=0; i
<lck
->num_share_modes
; i
++) {
967 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
971 if (BATCH_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
972 /* batch - can only be one. */
973 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
974 smb_panic("Bad batch oplock entry.");
976 *pp_batch
= &lck
->share_modes
[i
];
979 if (EXCLUSIVE_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
980 /* Exclusive or batch - can only be one. */
981 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
982 smb_panic("Bad exclusive or batch oplock entry.");
984 *pp_ex_or_batch
= &lck
->share_modes
[i
];
987 if (LEVEL_II_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
988 if (*pp_batch
|| *pp_ex_or_batch
) {
989 smb_panic("Bad levelII oplock entry.");
994 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
) {
995 if (*pp_batch
|| *pp_ex_or_batch
) {
996 smb_panic("Bad no oplock entry.");
998 *got_no_oplock
= true;
1003 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1006 struct share_mode_entry
*batch_entry
)
1008 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1012 if (batch_entry
!= NULL
) {
1013 /* Found a batch oplock */
1014 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1020 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1023 struct share_mode_entry
*ex_entry
)
1025 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1029 if (ex_entry
!= NULL
) {
1030 /* Found an exclusive or batch oplock */
1031 bool delay_it
= is_delete_request(fsp
) ?
1032 BATCH_OPLOCK_TYPE(ex_entry
->op_type
) : true;
1034 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1041 static bool file_has_brlocks(files_struct
*fsp
)
1043 struct byte_range_lock
*br_lck
;
1045 br_lck
= brl_get_locks_readonly(fsp
);
1049 return br_lck
->num_locks
> 0 ? true : false;
1052 static void grant_fsp_oplock_type(files_struct
*fsp
,
1054 bool got_level2_oplock
,
1055 bool got_a_none_oplock
)
1057 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1058 lp_level2_oplocks(SNUM(fsp
->conn
));
1060 /* Start by granting what the client asked for,
1061 but ensure no SAMBA_PRIVATE bits can be set. */
1062 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1064 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1065 /* No oplocks on internal open. */
1066 fsp
->oplock_type
= NO_OPLOCK
;
1067 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1068 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1070 } else if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1071 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1073 fsp
->oplock_type
= NO_OPLOCK
;
1076 if (is_stat_open(fsp
->access_mask
)) {
1077 /* Leave the value already set. */
1078 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1079 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1084 * Match what was requested (fsp->oplock_type) with
1085 * what was found in the existing share modes.
1088 if (got_a_none_oplock
) {
1089 fsp
->oplock_type
= NO_OPLOCK
;
1090 } else if (got_level2_oplock
) {
1091 if (fsp
->oplock_type
== NO_OPLOCK
||
1092 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1093 /* Store a level2 oplock, but don't tell the client */
1094 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1096 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1099 /* All share_mode_entries are placeholders or deferred.
1100 * Silently upgrade to fake levelII if the client didn't
1101 * ask for an oplock. */
1102 if (fsp
->oplock_type
== NO_OPLOCK
) {
1103 /* Store a level2 oplock, but don't tell the client */
1104 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1109 * Don't grant level2 to clients that don't want them
1110 * or if we've turned them off.
1112 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1113 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1116 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1117 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1120 bool request_timed_out(struct timeval request_time
,
1121 struct timeval timeout
)
1123 struct timeval now
, end_time
;
1125 end_time
= timeval_sum(&request_time
, &timeout
);
1126 return (timeval_compare(&end_time
, &now
) < 0);
1129 /****************************************************************************
1130 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1131 ****************************************************************************/
1133 static void defer_open(struct share_mode_lock
*lck
,
1134 struct timeval request_time
,
1135 struct timeval timeout
,
1136 struct smb_request
*req
,
1137 struct deferred_open_record
*state
)
1141 /* Paranoia check */
1143 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1144 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1146 if (!is_deferred_open_entry(e
)) {
1150 if (procid_is_me(&e
->pid
) && (e
->op_mid
== req
->mid
)) {
1151 DEBUG(0, ("Trying to defer an already deferred "
1152 "request: mid=%llu, exiting\n",
1153 (unsigned long long)req
->mid
));
1154 exit_server("attempt to defer a deferred request");
1158 /* End paranoia check */
1160 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1161 "open entry for mid %llu\n",
1162 (unsigned int)request_time
.tv_sec
,
1163 (unsigned int)request_time
.tv_usec
,
1164 (unsigned long long)req
->mid
));
1166 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1167 state
->id
, (char *)state
, sizeof(*state
))) {
1168 exit_server("push_deferred_open_message_smb failed");
1170 add_deferred_open(lck
, req
->mid
, request_time
,
1171 sconn_server_id(req
->sconn
), state
->id
);
1175 /****************************************************************************
1176 On overwrite open ensure that the attributes match.
1177 ****************************************************************************/
1179 bool open_match_attributes(connection_struct
*conn
,
1180 uint32 old_dos_attr
,
1181 uint32 new_dos_attr
,
1182 mode_t existing_unx_mode
,
1183 mode_t new_unx_mode
,
1184 mode_t
*returned_unx_mode
)
1186 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1188 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1189 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1191 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1192 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1193 *returned_unx_mode
= new_unx_mode
;
1195 *returned_unx_mode
= (mode_t
)0;
1198 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1199 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1200 "returned_unx_mode = 0%o\n",
1201 (unsigned int)old_dos_attr
,
1202 (unsigned int)existing_unx_mode
,
1203 (unsigned int)new_dos_attr
,
1204 (unsigned int)*returned_unx_mode
));
1206 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1207 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1208 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1209 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1213 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1214 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1215 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1222 /****************************************************************************
1223 Special FCB or DOS processing in the case of a sharing violation.
1224 Try and find a duplicated file handle.
1225 ****************************************************************************/
1227 NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1228 connection_struct
*conn
,
1229 files_struct
*fsp_to_dup_into
,
1230 const struct smb_filename
*smb_fname
,
1235 uint32 share_access
,
1236 uint32 create_options
)
1240 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1241 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1243 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1244 fsp
= file_find_di_next(fsp
)) {
1246 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1247 "vuid = %u, file_pid = %u, private_options = 0x%x "
1248 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1249 fsp
->fh
->fd
, (unsigned int)fsp
->vuid
,
1250 (unsigned int)fsp
->file_pid
,
1251 (unsigned int)fsp
->fh
->private_options
,
1252 (unsigned int)fsp
->access_mask
));
1254 if (fsp
->fh
->fd
!= -1 &&
1255 fsp
->vuid
== vuid
&&
1256 fsp
->file_pid
== file_pid
&&
1257 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1258 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1259 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1260 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1261 strequal(fsp
->fsp_name
->stream_name
,
1262 smb_fname
->stream_name
)) {
1263 DEBUG(10,("fcb_or_dos_open: file match\n"));
1269 return NT_STATUS_NOT_FOUND
;
1272 /* quite an insane set of semantics ... */
1273 if (is_executable(smb_fname
->base_name
) &&
1274 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1275 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1276 return NT_STATUS_INVALID_PARAMETER
;
1279 /* We need to duplicate this fsp. */
1280 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1281 create_options
, fsp_to_dup_into
);
1284 /****************************************************************************
1285 Open a file with a share mode - old openX method - map into NTCreate.
1286 ****************************************************************************/
1288 bool map_open_params_to_ntcreate(const struct smb_filename
*smb_fname
,
1289 int deny_mode
, int open_func
,
1290 uint32
*paccess_mask
,
1291 uint32
*pshare_mode
,
1292 uint32
*pcreate_disposition
,
1293 uint32
*pcreate_options
,
1294 uint32_t *pprivate_flags
)
1298 uint32 create_disposition
;
1299 uint32 create_options
= FILE_NON_DIRECTORY_FILE
;
1300 uint32_t private_flags
= 0;
1302 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1303 "open_func = 0x%x\n",
1304 smb_fname_str_dbg(smb_fname
), (unsigned int)deny_mode
,
1305 (unsigned int)open_func
));
1307 /* Create the NT compatible access_mask. */
1308 switch (GET_OPENX_MODE(deny_mode
)) {
1309 case DOS_OPEN_EXEC
: /* Implies read-only - used to be FILE_READ_DATA */
1310 case DOS_OPEN_RDONLY
:
1311 access_mask
= FILE_GENERIC_READ
;
1313 case DOS_OPEN_WRONLY
:
1314 access_mask
= FILE_GENERIC_WRITE
;
1318 access_mask
= FILE_GENERIC_READ
|FILE_GENERIC_WRITE
;
1321 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1322 (unsigned int)GET_OPENX_MODE(deny_mode
)));
1326 /* Create the NT compatible create_disposition. */
1327 switch (open_func
) {
1328 case OPENX_FILE_EXISTS_FAIL
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
1329 create_disposition
= FILE_CREATE
;
1332 case OPENX_FILE_EXISTS_OPEN
:
1333 create_disposition
= FILE_OPEN
;
1336 case OPENX_FILE_EXISTS_OPEN
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
1337 create_disposition
= FILE_OPEN_IF
;
1340 case OPENX_FILE_EXISTS_TRUNCATE
:
1341 create_disposition
= FILE_OVERWRITE
;
1344 case OPENX_FILE_EXISTS_TRUNCATE
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
1345 create_disposition
= FILE_OVERWRITE_IF
;
1349 /* From samba4 - to be confirmed. */
1350 if (GET_OPENX_MODE(deny_mode
) == DOS_OPEN_EXEC
) {
1351 create_disposition
= FILE_CREATE
;
1354 DEBUG(10,("map_open_params_to_ntcreate: bad "
1355 "open_func 0x%x\n", (unsigned int)open_func
));
1359 /* Create the NT compatible share modes. */
1360 switch (GET_DENY_MODE(deny_mode
)) {
1362 share_mode
= FILE_SHARE_NONE
;
1366 share_mode
= FILE_SHARE_READ
;
1370 share_mode
= FILE_SHARE_WRITE
;
1374 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
1378 private_flags
|= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
;
1379 if (is_executable(smb_fname
->base_name
)) {
1380 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
1382 if (GET_OPENX_MODE(deny_mode
) == DOS_OPEN_RDONLY
) {
1383 share_mode
= FILE_SHARE_READ
;
1385 share_mode
= FILE_SHARE_NONE
;
1391 private_flags
|= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
;
1392 share_mode
= FILE_SHARE_NONE
;
1396 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1397 (unsigned int)GET_DENY_MODE(deny_mode
) ));
1401 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1402 "share_mode = 0x%x, create_disposition = 0x%x, "
1403 "create_options = 0x%x private_flags = 0x%x\n",
1404 smb_fname_str_dbg(smb_fname
),
1405 (unsigned int)access_mask
,
1406 (unsigned int)share_mode
,
1407 (unsigned int)create_disposition
,
1408 (unsigned int)create_options
,
1409 (unsigned int)private_flags
));
1412 *paccess_mask
= access_mask
;
1415 *pshare_mode
= share_mode
;
1417 if (pcreate_disposition
) {
1418 *pcreate_disposition
= create_disposition
;
1420 if (pcreate_options
) {
1421 *pcreate_options
= create_options
;
1423 if (pprivate_flags
) {
1424 *pprivate_flags
= private_flags
;
1431 static void schedule_defer_open(struct share_mode_lock
*lck
,
1432 struct timeval request_time
,
1433 struct smb_request
*req
)
1435 struct deferred_open_record state
;
1437 /* This is a relative time, added to the absolute
1438 request_time value to get the absolute timeout time.
1439 Note that if this is the second or greater time we enter
1440 this codepath for this particular request mid then
1441 request_time is left as the absolute time of the *first*
1442 time this request mid was processed. This is what allows
1443 the request to eventually time out. */
1445 struct timeval timeout
;
1447 /* Normally the smbd we asked should respond within
1448 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1449 * the client did, give twice the timeout as a safety
1450 * measure here in case the other smbd is stuck
1451 * somewhere else. */
1453 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1455 /* Nothing actually uses state.delayed_for_oplocks
1456 but it's handy to differentiate in debug messages
1457 between a 30 second delay due to oplock break, and
1458 a 1 second delay for share mode conflicts. */
1460 state
.delayed_for_oplocks
= True
;
1463 if (!request_timed_out(request_time
, timeout
)) {
1464 defer_open(lck
, request_time
, timeout
, req
, &state
);
1468 /****************************************************************************
1469 Work out what access_mask to use from what the client sent us.
1470 ****************************************************************************/
1472 static NTSTATUS
calculate_access_mask(connection_struct
*conn
,
1473 const struct smb_filename
*smb_fname
,
1475 uint32_t access_mask
,
1476 uint32_t *access_mask_out
)
1481 * Convert GENERIC bits to specific bits.
1484 se_map_generic(&access_mask
, &file_generic_mapping
);
1486 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1487 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1490 struct security_descriptor
*sd
;
1491 uint32_t access_granted
= 0;
1493 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1498 if (!NT_STATUS_IS_OK(status
)) {
1499 DEBUG(10, ("calculate_access_mask: Could not get acl "
1501 smb_fname_str_dbg(smb_fname
),
1502 nt_errstr(status
)));
1503 return NT_STATUS_ACCESS_DENIED
;
1506 status
= smb1_file_se_access_check(conn
,
1508 get_current_nttok(conn
),
1514 if (!NT_STATUS_IS_OK(status
)) {
1515 DEBUG(10, ("calculate_access_mask: Access denied on "
1516 "file %s: when calculating maximum access\n",
1517 smb_fname_str_dbg(smb_fname
)));
1518 return NT_STATUS_ACCESS_DENIED
;
1521 access_mask
= access_granted
;
1523 access_mask
= FILE_GENERIC_ALL
;
1527 *access_mask_out
= access_mask
;
1528 return NT_STATUS_OK
;
1531 /****************************************************************************
1532 Remove the deferred open entry under lock.
1533 ****************************************************************************/
1535 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1536 struct server_id pid
)
1538 struct share_mode_lock
*lck
= get_share_mode_lock(talloc_tos(), id
,
1541 DEBUG(0, ("could not get share mode lock\n"));
1543 del_deferred_open_entry(lck
, mid
, pid
);
1548 /****************************************************************************
1549 Open a file with a share mode. Passed in an already created files_struct *.
1550 ****************************************************************************/
1552 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1553 struct smb_request
*req
,
1554 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1555 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1556 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1557 uint32 create_options
, /* options such as delete on close. */
1558 uint32 new_dos_attributes
, /* attributes used for new file. */
1559 int oplock_request
, /* internal Samba oplock codes. */
1560 /* Information (FILE_EXISTS etc.) */
1561 uint32_t private_flags
, /* Samba specific flags. */
1565 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1568 bool file_existed
= VALID_STAT(smb_fname
->st
);
1569 bool def_acl
= False
;
1570 bool posix_open
= False
;
1571 bool new_file_created
= False
;
1572 bool clear_ads
= false;
1574 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1575 mode_t new_unx_mode
= (mode_t
)0;
1576 mode_t unx_mode
= (mode_t
)0;
1578 uint32 existing_dos_attributes
= 0;
1579 struct timeval request_time
= timeval_zero();
1580 struct share_mode_lock
*lck
= NULL
;
1581 uint32 open_access_mask
= access_mask
;
1587 /* Windows allows a new file to be created and
1588 silently removes a FILE_ATTRIBUTE_DIRECTORY
1589 sent by the client. Do the same. */
1591 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
1593 if (conn
->printer
) {
1595 * Printers are handled completely differently.
1596 * Most of the passed parameters are ignored.
1600 *pinfo
= FILE_WAS_CREATED
;
1603 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1604 smb_fname_str_dbg(smb_fname
)));
1607 DEBUG(0,("open_file_ntcreate: printer open without "
1608 "an SMB request!\n"));
1609 return NT_STATUS_INTERNAL_ERROR
;
1612 return print_spool_open(fsp
, smb_fname
->base_name
,
1616 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1618 return NT_STATUS_NO_MEMORY
;
1621 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1623 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
1624 new_dos_attributes
= 0;
1626 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1628 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
1629 smb_fname
, parent_dir
);
1632 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1633 "access_mask=0x%x share_access=0x%x "
1634 "create_disposition = 0x%x create_options=0x%x "
1635 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1636 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
1637 access_mask
, share_access
, create_disposition
,
1638 create_options
, (unsigned int)unx_mode
, oplock_request
,
1639 (unsigned int)private_flags
));
1641 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
1642 DEBUG(0, ("No smb request but not an internal only open!\n"));
1643 return NT_STATUS_INTERNAL_ERROR
;
1647 * Only non-internal opens can be deferred at all
1652 if (get_deferred_open_message_state(req
,
1656 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
1657 /* Remember the absolute time of the original
1658 request with this mid. We'll use it later to
1659 see if this has timed out. */
1661 /* Remove the deferred open entry under lock. */
1662 remove_deferred_open_entry(
1663 state
->id
, req
->mid
,
1664 sconn_server_id(req
->sconn
));
1666 /* Ensure we don't reprocess this message. */
1667 remove_deferred_open_message_smb(req
->mid
);
1671 status
= check_name(conn
, smb_fname
->base_name
);
1672 if (!NT_STATUS_IS_OK(status
)) {
1677 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
1679 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
1683 /* ignore any oplock requests if oplocks are disabled */
1684 if (!lp_oplocks(SNUM(conn
)) ||
1685 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
1686 /* Mask off everything except the private Samba bits. */
1687 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
1690 /* this is for OS/2 long file names - say we don't support them */
1691 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
1692 /* OS/2 Workplace shell fix may be main code stream in a later
1694 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1696 if (use_nt_status()) {
1697 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1699 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
1702 switch( create_disposition
) {
1704 * Currently we're using FILE_SUPERSEDE as the same as
1705 * FILE_OVERWRITE_IF but they really are
1706 * different. FILE_SUPERSEDE deletes an existing file
1707 * (requiring delete access) then recreates it.
1709 case FILE_SUPERSEDE
:
1710 /* If file exists replace/overwrite. If file doesn't
1712 flags2
|= (O_CREAT
| O_TRUNC
);
1716 case FILE_OVERWRITE_IF
:
1717 /* If file exists replace/overwrite. If file doesn't
1719 flags2
|= (O_CREAT
| O_TRUNC
);
1724 /* If file exists open. If file doesn't exist error. */
1725 if (!file_existed
) {
1726 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1727 "requested for file %s and file "
1729 smb_fname_str_dbg(smb_fname
)));
1731 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1735 case FILE_OVERWRITE
:
1736 /* If file exists overwrite. If file doesn't exist
1738 if (!file_existed
) {
1739 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1740 "requested for file %s and file "
1742 smb_fname_str_dbg(smb_fname
) ));
1744 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1751 /* If file exists error. If file doesn't exist
1754 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1755 "requested for file %s and file "
1756 "already exists.\n",
1757 smb_fname_str_dbg(smb_fname
)));
1758 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1763 return map_nt_error_from_unix(errno
);
1765 flags2
|= (O_CREAT
|O_EXCL
);
1769 /* If file exists open. If file doesn't exist
1775 return NT_STATUS_INVALID_PARAMETER
;
1778 /* We only care about matching attributes on file exists and
1781 if (!posix_open
&& file_existed
&& ((create_disposition
== FILE_OVERWRITE
) ||
1782 (create_disposition
== FILE_OVERWRITE_IF
))) {
1783 if (!open_match_attributes(conn
, existing_dos_attributes
,
1785 smb_fname
->st
.st_ex_mode
,
1786 unx_mode
, &new_unx_mode
)) {
1787 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1788 "for file %s (%x %x) (0%o, 0%o)\n",
1789 smb_fname_str_dbg(smb_fname
),
1790 existing_dos_attributes
,
1792 (unsigned int)smb_fname
->st
.st_ex_mode
,
1793 (unsigned int)unx_mode
));
1795 return NT_STATUS_ACCESS_DENIED
;
1799 status
= calculate_access_mask(conn
, smb_fname
, file_existed
,
1802 if (!NT_STATUS_IS_OK(status
)) {
1803 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1804 "on file %s returned %s\n",
1805 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
1809 open_access_mask
= access_mask
;
1811 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1812 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
1815 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1816 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
1820 * Note that we ignore the append flag as append does not
1821 * mean the same thing under DOS and Unix.
1824 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
1825 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1826 /* DENY_DOS opens are always underlying read-write on the
1827 file handle, no matter what the requested access mask
1829 if ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
1830 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
1840 * Currently we only look at FILE_WRITE_THROUGH for create options.
1844 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
1849 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
1853 if (!posix_open
&& !CAN_WRITE(conn
)) {
1855 * We should really return a permission denied error if either
1856 * O_CREAT or O_TRUNC are set, but for compatibility with
1857 * older versions of Samba we just AND them out.
1859 flags2
&= ~(O_CREAT
|O_TRUNC
);
1863 * Ensure we can't write on a read-only share or file.
1866 if (flags
!= O_RDONLY
&& file_existed
&&
1867 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
1868 DEBUG(5,("open_file_ntcreate: write access requested for "
1869 "file %s on read only %s\n",
1870 smb_fname_str_dbg(smb_fname
),
1871 !CAN_WRITE(conn
) ? "share" : "file" ));
1873 return NT_STATUS_ACCESS_DENIED
;
1876 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1877 fsp
->share_access
= share_access
;
1878 fsp
->fh
->private_options
= private_flags
;
1879 fsp
->access_mask
= open_access_mask
; /* We change this to the
1880 * requested access_mask after
1881 * the open is done. */
1882 fsp
->posix_open
= posix_open
;
1884 /* Ensure no SAMBA_PRIVATE bits can be set. */
1885 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1887 if (timeval_is_zero(&request_time
)) {
1888 request_time
= fsp
->open_time
;
1892 struct share_mode_entry
*batch_entry
= NULL
;
1893 struct share_mode_entry
*exclusive_entry
= NULL
;
1894 bool got_level2_oplock
= false;
1895 bool got_a_none_oplock
= false;
1897 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
1898 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1900 lck
= get_share_mode_lock(talloc_tos(), id
,
1902 smb_fname
, &old_write_time
);
1905 DEBUG(0, ("Could not get share mode lock\n"));
1906 return NT_STATUS_SHARING_VIOLATION
;
1909 /* Get the types we need to examine. */
1910 find_oplock_types(lck
,
1914 &got_a_none_oplock
);
1916 /* First pass - send break only on batch oplocks. */
1917 if ((req
!= NULL
) &&
1918 delay_for_batch_oplocks(fsp
,
1922 schedule_defer_open(lck
, request_time
, req
);
1924 return NT_STATUS_SHARING_VIOLATION
;
1927 /* Use the client requested access mask here, not the one we
1929 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
1930 access_mask
, share_access
,
1931 create_options
, &file_existed
);
1933 if (NT_STATUS_IS_OK(status
)) {
1934 /* We might be going to allow this open. Check oplock
1936 /* Second pass - send break for both batch or
1937 * exclusive oplocks. */
1938 if ((req
!= NULL
) &&
1939 delay_for_exclusive_oplocks(
1944 schedule_defer_open(lck
, request_time
, req
);
1946 return NT_STATUS_SHARING_VIOLATION
;
1950 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
1951 /* DELETE_PENDING is not deferred for a second */
1956 grant_fsp_oplock_type(fsp
,
1961 if (!NT_STATUS_IS_OK(status
)) {
1962 uint32 can_access_mask
;
1963 bool can_access
= True
;
1965 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
1967 /* Check if this can be done with the deny_dos and fcb
1970 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1971 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
1973 DEBUG(0, ("DOS open without an SMB "
1976 return NT_STATUS_INTERNAL_ERROR
;
1979 /* Use the client requested access mask here,
1980 * not the one we open with. */
1981 status
= fcb_or_dos_open(req
,
1992 if (NT_STATUS_IS_OK(status
)) {
1995 *pinfo
= FILE_WAS_OPENED
;
1997 return NT_STATUS_OK
;
2002 * This next line is a subtlety we need for
2003 * MS-Access. If a file open will fail due to share
2004 * permissions and also for security (access) reasons,
2005 * we need to return the access failed error, not the
2006 * share error. We can't open the file due to kernel
2007 * oplock deadlock (it's possible we failed above on
2008 * the open_mode_check()) so use a userspace check.
2011 if (flags
& O_RDWR
) {
2012 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2013 } else if (flags
& O_WRONLY
) {
2014 can_access_mask
= FILE_WRITE_DATA
;
2016 can_access_mask
= FILE_READ_DATA
;
2019 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2020 !CAN_WRITE(conn
)) ||
2021 !can_access_file_data(conn
, smb_fname
,
2027 * If we're returning a share violation, ensure we
2028 * cope with the braindead 1 second delay.
2031 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2032 lp_defer_sharing_violations()) {
2033 struct timeval timeout
;
2034 struct deferred_open_record state
;
2037 /* this is a hack to speed up torture tests
2039 timeout_usecs
= lp_parm_int(SNUM(conn
),
2040 "smbd","sharedelay",
2041 SHARING_VIOLATION_USEC_WAIT
);
2043 /* This is a relative time, added to the absolute
2044 request_time value to get the absolute timeout time.
2045 Note that if this is the second or greater time we enter
2046 this codepath for this particular request mid then
2047 request_time is left as the absolute time of the *first*
2048 time this request mid was processed. This is what allows
2049 the request to eventually time out. */
2051 timeout
= timeval_set(0, timeout_usecs
);
2053 /* Nothing actually uses state.delayed_for_oplocks
2054 but it's handy to differentiate in debug messages
2055 between a 30 second delay due to oplock break, and
2056 a 1 second delay for share mode conflicts. */
2058 state
.delayed_for_oplocks
= False
;
2062 && !request_timed_out(request_time
,
2064 defer_open(lck
, request_time
, timeout
,
2072 * We have detected a sharing violation here
2073 * so return the correct error code
2075 status
= NT_STATUS_SHARING_VIOLATION
;
2077 status
= NT_STATUS_ACCESS_DENIED
;
2083 * We exit this block with the share entry *locked*.....
2087 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
2090 * Ensure we pay attention to default ACLs on directories if required.
2093 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2094 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2095 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2098 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2099 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2100 (unsigned int)flags
, (unsigned int)flags2
,
2101 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2102 (unsigned int)open_access_mask
));
2105 * open_file strips any O_TRUNC flags itself.
2108 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2109 flags
|flags2
, unx_mode
, access_mask
,
2112 if (!NT_STATUS_IS_OK(fsp_open
)) {
2119 if (!file_existed
) {
2120 struct share_mode_entry
*batch_entry
= NULL
;
2121 struct share_mode_entry
*exclusive_entry
= NULL
;
2122 bool got_level2_oplock
= false;
2123 bool got_a_none_oplock
= false;
2124 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2126 * Deal with the race condition where two smbd's detect the
2127 * file doesn't exist and do the create at the same time. One
2128 * of them will win and set a share mode, the other (ie. this
2129 * one) should check if the requested share mode for this
2130 * create is allowed.
2134 * Now the file exists and fsp is successfully opened,
2135 * fsp->dev and fsp->inode are valid and should replace the
2136 * dev=0,inode=0 from a non existent file. Spotted by
2137 * Nadav Danieli <nadavd@exanet.com>. JRA.
2142 lck
= get_share_mode_lock(talloc_tos(), id
,
2144 smb_fname
, &old_write_time
);
2147 DEBUG(0, ("open_file_ntcreate: Could not get share "
2148 "mode lock for %s\n",
2149 smb_fname_str_dbg(smb_fname
)));
2151 return NT_STATUS_SHARING_VIOLATION
;
2154 /* Get the types we need to examine. */
2155 find_oplock_types(lck
,
2159 &got_a_none_oplock
);
2161 /* First pass - send break only on batch oplocks. */
2162 if ((req
!= NULL
) &&
2163 delay_for_batch_oplocks(fsp
,
2167 schedule_defer_open(lck
, request_time
, req
);
2170 return NT_STATUS_SHARING_VIOLATION
;
2173 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2174 access_mask
, share_access
,
2175 create_options
, &file_existed
);
2177 if (NT_STATUS_IS_OK(status
)) {
2178 /* We might be going to allow this open. Check oplock
2180 /* Second pass - send break for both batch or
2181 * exclusive oplocks. */
2182 if ((req
!= NULL
) &&
2183 delay_for_exclusive_oplocks(
2188 schedule_defer_open(lck
, request_time
, req
);
2191 return NT_STATUS_SHARING_VIOLATION
;
2195 if (!NT_STATUS_IS_OK(status
)) {
2196 struct deferred_open_record state
;
2200 state
.delayed_for_oplocks
= False
;
2203 /* Do it all over again immediately. In the second
2204 * round we will find that the file existed and handle
2205 * the DELETE_PENDING and FCB cases correctly. No need
2206 * to duplicate the code here. Essentially this is a
2207 * "goto top of this function", but don't tell
2211 defer_open(lck
, request_time
, timeval_zero(),
2218 grant_fsp_oplock_type(fsp
,
2224 * We exit this block with the share entry *locked*.....
2229 SMB_ASSERT(lck
!= NULL
);
2231 /* Delete streams if create_disposition requires it */
2232 if (file_existed
&& clear_ads
&&
2233 !is_ntfs_stream_smb_fname(smb_fname
)) {
2234 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2235 if (!NT_STATUS_IS_OK(status
)) {
2242 /* note that we ignore failure for the following. It is
2243 basically a hack for NFS, and NFS will never set one of
2244 these only read them. Nobody but Samba can ever set a deny
2245 mode and we have already checked our more authoritative
2246 locking database for permission to set this deny mode. If
2247 the kernel refuses the operations then the kernel is wrong.
2248 note that GPFS supports it as well - jmcd */
2250 if (fsp
->fh
->fd
!= -1) {
2252 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2253 if(ret_flock
== -1 ){
2258 return NT_STATUS_SHARING_VIOLATION
;
2263 * At this point onwards, we can guarentee that the share entry
2264 * is locked, whether we created the file or not, and that the
2265 * deny mode is compatible with all current opens.
2269 * If requested, truncate the file.
2272 if (file_existed
&& (flags2
&O_TRUNC
)) {
2274 * We are modifing the file after open - update the stat
2277 if ((SMB_VFS_FTRUNCATE(fsp
, 0) == -1) ||
2278 (SMB_VFS_FSTAT(fsp
, &smb_fname
->st
)==-1)) {
2279 status
= map_nt_error_from_unix(errno
);
2287 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2288 * but we don't have to store this - just ignore it on access check.
2290 if (conn
->sconn
->using_smb2
) {
2292 * SMB2 doesn't return it (according to Microsoft tests).
2293 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2294 * File created with access = 0x7 (Read, Write, Delete)
2295 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2297 fsp
->access_mask
= access_mask
;
2299 /* But SMB1 does. */
2300 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2304 /* stat opens on existing files don't get oplocks. */
2305 if (is_stat_open(open_access_mask
)) {
2306 fsp
->oplock_type
= NO_OPLOCK
;
2309 if (!(flags2
& O_TRUNC
)) {
2310 info
= FILE_WAS_OPENED
;
2312 info
= FILE_WAS_OVERWRITTEN
;
2315 info
= FILE_WAS_CREATED
;
2323 * Setup the oplock info in both the shared memory and
2327 if (!set_file_oplock(fsp
, fsp
->oplock_type
)) {
2329 * Could not get the kernel oplock or there are byte-range
2330 * locks on the file.
2332 fsp
->oplock_type
= NO_OPLOCK
;
2335 if (info
== FILE_WAS_OVERWRITTEN
|| info
== FILE_WAS_CREATED
|| info
== FILE_WAS_SUPERSEDED
) {
2336 new_file_created
= True
;
2339 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2343 /* Handle strange delete on close create semantics. */
2344 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2346 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2348 if (!NT_STATUS_IS_OK(status
)) {
2349 /* Remember to delete the mode we just added. */
2350 del_share_mode(lck
, fsp
);
2355 /* Note that here we set the *inital* delete on close flag,
2356 not the regular one. The magic gets handled in close. */
2357 fsp
->initial_delete_on_close
= True
;
2360 if (new_file_created
) {
2361 /* Files should be initially set as archive */
2362 if (lp_map_archive(SNUM(conn
)) ||
2363 lp_store_dos_attributes(SNUM(conn
))) {
2365 if (file_set_dosmode(conn
, smb_fname
,
2366 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2367 parent_dir
, true) == 0) {
2368 unx_mode
= smb_fname
->st
.st_ex_mode
;
2374 /* Determine sparse flag. */
2376 /* POSIX opens are sparse by default. */
2377 fsp
->is_sparse
= true;
2379 fsp
->is_sparse
= (file_existed
&&
2380 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2384 * Take care of inherited ACLs on created files - if default ACL not
2388 if (!posix_open
&& !file_existed
&& !def_acl
) {
2390 int saved_errno
= errno
; /* We might get ENOSYS in the next
2393 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2395 errno
= saved_errno
; /* Ignore ENOSYS */
2398 } else if (new_unx_mode
) {
2402 /* Attributes need changing. File already existed. */
2405 int saved_errno
= errno
; /* We might get ENOSYS in the
2407 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2409 if (ret
== -1 && errno
== ENOSYS
) {
2410 errno
= saved_errno
; /* Ignore ENOSYS */
2412 DEBUG(5, ("open_file_ntcreate: reset "
2413 "attributes of file %s to 0%o\n",
2414 smb_fname_str_dbg(smb_fname
),
2415 (unsigned int)new_unx_mode
));
2416 ret
= 0; /* Don't do the fchmod below. */
2421 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2422 DEBUG(5, ("open_file_ntcreate: failed to reset "
2423 "attributes of file %s to 0%o\n",
2424 smb_fname_str_dbg(smb_fname
),
2425 (unsigned int)new_unx_mode
));
2428 /* If this is a successful open, we must remove any deferred open
2431 del_deferred_open_entry(lck
, req
->mid
,
2432 sconn_server_id(req
->sconn
));
2436 return NT_STATUS_OK
;
2440 /****************************************************************************
2441 Open a file for for write to ensure that we can fchmod it.
2442 ****************************************************************************/
2444 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2445 struct smb_filename
*smb_fname
,
2446 files_struct
**result
)
2448 if (!VALID_STAT(smb_fname
->st
)) {
2449 return NT_STATUS_INVALID_PARAMETER
;
2452 return SMB_VFS_CREATE_FILE(
2455 0, /* root_dir_fid */
2456 smb_fname
, /* fname */
2457 FILE_WRITE_DATA
, /* access_mask */
2458 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2460 FILE_OPEN
, /* create_disposition*/
2461 0, /* create_options */
2462 0, /* file_attributes */
2463 INTERNAL_OPEN_ONLY
, /* oplock_request */
2464 0, /* allocation_size */
2465 0, /* private_flags */
2468 result
, /* result */
2472 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2473 struct smb_filename
*smb_dname
,
2474 uint32 file_attributes
)
2479 bool posix_open
= false;
2481 if(!CAN_WRITE(conn
)) {
2482 DEBUG(5,("mkdir_internal: failing create on read-only share "
2483 "%s\n", lp_servicename(SNUM(conn
))));
2484 return NT_STATUS_ACCESS_DENIED
;
2487 status
= check_name(conn
, smb_dname
->base_name
);
2488 if (!NT_STATUS_IS_OK(status
)) {
2492 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2494 return NT_STATUS_NO_MEMORY
;
2497 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2499 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2501 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2504 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2505 return map_nt_error_from_unix(errno
);
2508 /* Ensure we're checking for a symlink here.... */
2509 /* We don't want to get caught by a symlink racer. */
2511 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2512 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2513 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2514 return map_nt_error_from_unix(errno
);
2517 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2518 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2519 smb_fname_str_dbg(smb_dname
)));
2520 return NT_STATUS_ACCESS_DENIED
;
2523 if (lp_store_dos_attributes(SNUM(conn
))) {
2525 file_set_dosmode(conn
, smb_dname
,
2526 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2531 if (lp_inherit_perms(SNUM(conn
))) {
2532 inherit_access_posix_acl(conn
, parent_dir
,
2533 smb_dname
->base_name
, mode
);
2538 * Check if high bits should have been set,
2539 * then (if bits are missing): add them.
2540 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2543 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2544 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2545 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2546 (smb_dname
->st
.st_ex_mode
|
2547 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2551 /* Change the owner if required. */
2552 if (lp_inherit_owner(SNUM(conn
))) {
2553 change_dir_owner_to_parent(conn
, parent_dir
,
2554 smb_dname
->base_name
,
2558 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2559 smb_dname
->base_name
);
2561 return NT_STATUS_OK
;
2564 /****************************************************************************
2565 Ensure we didn't get symlink raced on opening a directory.
2566 ****************************************************************************/
2568 bool check_same_stat(const SMB_STRUCT_STAT
*sbuf1
,
2569 const SMB_STRUCT_STAT
*sbuf2
)
2571 if (sbuf1
->st_ex_uid
!= sbuf2
->st_ex_uid
||
2572 sbuf1
->st_ex_gid
!= sbuf2
->st_ex_gid
||
2573 sbuf1
->st_ex_dev
!= sbuf2
->st_ex_dev
||
2574 sbuf1
->st_ex_ino
!= sbuf2
->st_ex_ino
) {
2580 /****************************************************************************
2581 Open a directory from an NT SMB call.
2582 ****************************************************************************/
2584 static NTSTATUS
open_directory(connection_struct
*conn
,
2585 struct smb_request
*req
,
2586 struct smb_filename
*smb_dname
,
2588 uint32 share_access
,
2589 uint32 create_disposition
,
2590 uint32 create_options
,
2591 uint32 file_attributes
,
2593 files_struct
**result
)
2595 files_struct
*fsp
= NULL
;
2596 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2597 struct share_mode_lock
*lck
= NULL
;
2599 struct timespec mtimespec
;
2602 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
2604 /* Ensure we have a directory attribute. */
2605 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2607 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2608 "share_access = 0x%x create_options = 0x%x, "
2609 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2610 smb_fname_str_dbg(smb_dname
),
2611 (unsigned int)access_mask
,
2612 (unsigned int)share_access
,
2613 (unsigned int)create_options
,
2614 (unsigned int)create_disposition
,
2615 (unsigned int)file_attributes
));
2617 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
2618 (conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
2619 is_ntfs_stream_smb_fname(smb_dname
)) {
2620 DEBUG(2, ("open_directory: %s is a stream name!\n",
2621 smb_fname_str_dbg(smb_dname
)));
2622 return NT_STATUS_NOT_A_DIRECTORY
;
2625 status
= calculate_access_mask(conn
, smb_dname
, dir_existed
,
2626 access_mask
, &access_mask
);
2627 if (!NT_STATUS_IS_OK(status
)) {
2628 DEBUG(10, ("open_directory: calculate_access_mask "
2629 "on file %s returned %s\n",
2630 smb_fname_str_dbg(smb_dname
),
2631 nt_errstr(status
)));
2635 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2636 !security_token_has_privilege(get_current_nttok(conn
),
2637 SEC_PRIV_SECURITY
)) {
2638 DEBUG(10, ("open_directory: open on %s "
2639 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2640 smb_fname_str_dbg(smb_dname
)));
2641 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2644 switch( create_disposition
) {
2648 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2651 info
= FILE_WAS_OPENED
;
2656 /* If directory exists error. If directory doesn't
2659 status
= mkdir_internal(conn
, smb_dname
,
2662 if (!NT_STATUS_IS_OK(status
)) {
2663 DEBUG(2, ("open_directory: unable to create "
2664 "%s. Error was %s\n",
2665 smb_fname_str_dbg(smb_dname
),
2666 nt_errstr(status
)));
2670 info
= FILE_WAS_CREATED
;
2675 * If directory exists open. If directory doesn't
2679 status
= mkdir_internal(conn
, smb_dname
,
2682 if (NT_STATUS_IS_OK(status
)) {
2683 info
= FILE_WAS_CREATED
;
2686 if (NT_STATUS_EQUAL(status
,
2687 NT_STATUS_OBJECT_NAME_COLLISION
)) {
2688 info
= FILE_WAS_OPENED
;
2689 status
= NT_STATUS_OK
;
2693 case FILE_SUPERSEDE
:
2694 case FILE_OVERWRITE
:
2695 case FILE_OVERWRITE_IF
:
2697 DEBUG(5,("open_directory: invalid create_disposition "
2698 "0x%x for directory %s\n",
2699 (unsigned int)create_disposition
,
2700 smb_fname_str_dbg(smb_dname
)));
2701 return NT_STATUS_INVALID_PARAMETER
;
2704 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2705 DEBUG(5,("open_directory: %s is not a directory !\n",
2706 smb_fname_str_dbg(smb_dname
)));
2707 return NT_STATUS_NOT_A_DIRECTORY
;
2710 if (info
== FILE_WAS_OPENED
) {
2711 uint32_t access_granted
= 0;
2712 status
= smbd_check_open_rights(conn
, smb_dname
, access_mask
,
2715 /* Were we trying to do a directory open
2716 * for delete and didn't get DELETE
2717 * access (only) ? Check if the
2718 * directory allows DELETE_CHILD.
2720 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2723 if ((NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
) &&
2724 (access_mask
& DELETE_ACCESS
) &&
2725 (access_granted
== DELETE_ACCESS
) &&
2726 can_delete_file_in_directory(conn
, smb_dname
))) {
2727 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2728 "on directory %s\n",
2729 smb_fname_str_dbg(smb_dname
)));
2730 status
= NT_STATUS_OK
;
2733 if (!NT_STATUS_IS_OK(status
)) {
2734 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2735 "file %s failed with %s\n",
2736 smb_fname_str_dbg(smb_dname
),
2737 nt_errstr(status
)));
2742 status
= file_new(req
, conn
, &fsp
);
2743 if(!NT_STATUS_IS_OK(status
)) {
2748 * Setup the files_struct for it.
2751 fsp
->mode
= smb_dname
->st
.st_ex_mode
;
2752 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
2753 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
2754 fsp
->file_pid
= req
? req
->smbpid
: 0;
2755 fsp
->can_lock
= False
;
2756 fsp
->can_read
= False
;
2757 fsp
->can_write
= False
;
2759 fsp
->share_access
= share_access
;
2760 fsp
->fh
->private_options
= 0;
2762 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2764 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2765 fsp
->print_file
= NULL
;
2766 fsp
->modified
= False
;
2767 fsp
->oplock_type
= NO_OPLOCK
;
2768 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
2769 fsp
->is_directory
= True
;
2770 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
2771 status
= fsp_set_smb_fname(fsp
, smb_dname
);
2772 if (!NT_STATUS_IS_OK(status
)) {
2773 file_free(req
, fsp
);
2777 mtimespec
= smb_dname
->st
.st_ex_mtime
;
2780 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
2782 /* POSIX allows us to open a directory with O_RDONLY. */
2783 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
2785 if (!NT_STATUS_IS_OK(status
)) {
2786 DEBUG(5, ("open_directory: Could not open fd for "
2788 smb_fname_str_dbg(smb_dname
),
2789 nt_errstr(status
)));
2790 file_free(req
, fsp
);
2794 status
= vfs_stat_fsp(fsp
);
2795 if (!NT_STATUS_IS_OK(status
)) {
2797 file_free(req
, fsp
);
2801 /* Ensure there was no race condition. */
2802 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
2803 DEBUG(5,("open_directory: stat struct differs for "
2805 smb_fname_str_dbg(smb_dname
)));
2807 file_free(req
, fsp
);
2808 return NT_STATUS_ACCESS_DENIED
;
2811 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
2812 conn
->connectpath
, smb_dname
, &mtimespec
);
2815 DEBUG(0, ("open_directory: Could not get share mode lock for "
2816 "%s\n", smb_fname_str_dbg(smb_dname
)));
2818 file_free(req
, fsp
);
2819 return NT_STATUS_SHARING_VIOLATION
;
2822 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2823 access_mask
, share_access
,
2824 create_options
, &dir_existed
);
2826 if (!NT_STATUS_IS_OK(status
)) {
2829 file_free(req
, fsp
);
2833 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2834 req
? req
->mid
: 0, NO_OPLOCK
);
2836 /* For directories the delete on close bit at open time seems
2837 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2838 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2839 status
= can_set_delete_on_close(fsp
, 0);
2840 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
2843 file_free(req
, fsp
);
2847 if (NT_STATUS_IS_OK(status
)) {
2848 /* Note that here we set the *inital* delete on close flag,
2849 not the regular one. The magic gets handled in close. */
2850 fsp
->initial_delete_on_close
= True
;
2861 return NT_STATUS_OK
;
2864 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
2865 struct smb_filename
*smb_dname
)
2870 status
= SMB_VFS_CREATE_FILE(
2873 0, /* root_dir_fid */
2874 smb_dname
, /* fname */
2875 FILE_READ_ATTRIBUTES
, /* access_mask */
2876 FILE_SHARE_NONE
, /* share_access */
2877 FILE_CREATE
, /* create_disposition*/
2878 FILE_DIRECTORY_FILE
, /* create_options */
2879 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
2880 0, /* oplock_request */
2881 0, /* allocation_size */
2882 0, /* private_flags */
2888 if (NT_STATUS_IS_OK(status
)) {
2889 close_file(req
, fsp
, NORMAL_CLOSE
);
2895 /****************************************************************************
2896 Receive notification that one of our open files has been renamed by another
2898 ****************************************************************************/
2900 void msg_file_was_renamed(struct messaging_context
*msg
,
2903 struct server_id server_id
,
2906 struct smbd_server_connection
*sconn
;
2908 char *frm
= (char *)data
->data
;
2910 const char *sharepath
;
2911 const char *base_name
;
2912 const char *stream_name
;
2913 struct smb_filename
*smb_fname
= NULL
;
2914 size_t sp_len
, bn_len
;
2917 sconn
= msg_ctx_to_sconn(msg
);
2918 if (sconn
== NULL
) {
2919 DEBUG(1, ("could not find sconn\n"));
2923 if (data
->data
== NULL
2924 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
2925 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2926 (int)data
->length
));
2930 /* Unpack the message. */
2931 pull_file_id_24(frm
, &id
);
2932 sharepath
= &frm
[24];
2933 sp_len
= strlen(sharepath
);
2934 base_name
= sharepath
+ sp_len
+ 1;
2935 bn_len
= strlen(base_name
);
2936 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
2938 /* stream_name must always be NULL if there is no stream. */
2939 if (stream_name
[0] == '\0') {
2943 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
2944 stream_name
, NULL
, &smb_fname
);
2945 if (!NT_STATUS_IS_OK(status
)) {
2949 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2951 sharepath
, smb_fname_str_dbg(smb_fname
),
2952 file_id_string_tos(&id
)));
2954 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
2955 fsp
= file_find_di_next(fsp
)) {
2956 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
2958 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2959 fsp
->fnum
, fsp_str_dbg(fsp
),
2960 smb_fname_str_dbg(smb_fname
)));
2961 status
= fsp_set_smb_fname(fsp
, smb_fname
);
2962 if (!NT_STATUS_IS_OK(status
)) {
2967 /* Now we have the complete path we can work out if this is
2968 actually within this share and adjust newname accordingly. */
2969 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2970 "not sharepath %s) "
2971 "fnum %d from %s -> %s\n",
2972 fsp
->conn
->connectpath
,
2976 smb_fname_str_dbg(smb_fname
)));
2980 TALLOC_FREE(smb_fname
);
2985 * If a main file is opened for delete, all streams need to be checked for
2986 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2987 * If that works, delete them all by setting the delete on close and close.
2990 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
2993 struct stream_struct
*stream_info
;
2994 files_struct
**streams
;
2996 unsigned int num_streams
;
2997 TALLOC_CTX
*frame
= talloc_stackframe();
3000 status
= SMB_VFS_STREAMINFO(conn
, NULL
, fname
, talloc_tos(),
3001 &num_streams
, &stream_info
);
3003 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3004 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3005 DEBUG(10, ("no streams around\n"));
3007 return NT_STATUS_OK
;
3010 if (!NT_STATUS_IS_OK(status
)) {
3011 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
3012 nt_errstr(status
)));
3016 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3019 if (num_streams
== 0) {
3021 return NT_STATUS_OK
;
3024 streams
= TALLOC_ARRAY(talloc_tos(), files_struct
*, num_streams
);
3025 if (streams
== NULL
) {
3026 DEBUG(0, ("talloc failed\n"));
3027 status
= NT_STATUS_NO_MEMORY
;
3031 for (i
=0; i
<num_streams
; i
++) {
3032 struct smb_filename
*smb_fname
= NULL
;
3034 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3039 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3040 stream_info
[i
].name
,
3042 if (!NT_STATUS_IS_OK(status
)) {
3046 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3047 DEBUG(10, ("Unable to stat stream: %s\n",
3048 smb_fname_str_dbg(smb_fname
)));
3051 status
= SMB_VFS_CREATE_FILE(
3054 0, /* root_dir_fid */
3055 smb_fname
, /* fname */
3056 DELETE_ACCESS
, /* access_mask */
3057 (FILE_SHARE_READ
| /* share_access */
3058 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3059 FILE_OPEN
, /* create_disposition*/
3060 0, /* create_options */
3061 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3062 0, /* oplock_request */
3063 0, /* allocation_size */
3064 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3067 &streams
[i
], /* result */
3070 if (!NT_STATUS_IS_OK(status
)) {
3071 DEBUG(10, ("Could not open stream %s: %s\n",
3072 smb_fname_str_dbg(smb_fname
),
3073 nt_errstr(status
)));
3075 TALLOC_FREE(smb_fname
);
3078 TALLOC_FREE(smb_fname
);
3082 * don't touch the variable "status" beyond this point :-)
3085 for (i
-= 1 ; i
>= 0; i
--) {
3086 if (streams
[i
] == NULL
) {
3090 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3091 fsp_str_dbg(streams
[i
])));
3092 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3101 * Wrapper around open_file_ntcreate and open_directory
3104 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3105 struct smb_request
*req
,
3106 struct smb_filename
*smb_fname
,
3107 uint32_t access_mask
,
3108 uint32_t share_access
,
3109 uint32_t create_disposition
,
3110 uint32_t create_options
,
3111 uint32_t file_attributes
,
3112 uint32_t oplock_request
,
3113 uint64_t allocation_size
,
3114 uint32_t private_flags
,
3115 struct security_descriptor
*sd
,
3116 struct ea_list
*ea_list
,
3118 files_struct
**result
,
3121 int info
= FILE_WAS_OPENED
;
3122 files_struct
*base_fsp
= NULL
;
3123 files_struct
*fsp
= NULL
;
3126 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3127 "file_attributes = 0x%x, share_access = 0x%x, "
3128 "create_disposition = 0x%x create_options = 0x%x "
3129 "oplock_request = 0x%x private_flags = 0x%x "
3130 "ea_list = 0x%p, sd = 0x%p, "
3132 (unsigned int)access_mask
,
3133 (unsigned int)file_attributes
,
3134 (unsigned int)share_access
,
3135 (unsigned int)create_disposition
,
3136 (unsigned int)create_options
,
3137 (unsigned int)oplock_request
,
3138 (unsigned int)private_flags
,
3139 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3141 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3142 status
= NT_STATUS_NOT_SUPPORTED
;
3146 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3147 status
= NT_STATUS_INVALID_PARAMETER
;
3152 oplock_request
|= INTERNAL_OPEN_ONLY
;
3155 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3156 && (access_mask
& DELETE_ACCESS
)
3157 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3159 * We can't open a file with DELETE access if any of the
3160 * streams is open without FILE_SHARE_DELETE
3162 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3164 if (!NT_STATUS_IS_OK(status
)) {
3169 /* This is the correct thing to do (check every time) but can_delete
3170 * is expensive (it may have to read the parent directory
3171 * permissions). So for now we're not doing it unless we have a strong
3172 * hint the client is really going to delete this file. If the client
3173 * is forcing FILE_CREATE let the filesystem take care of the
3176 /* Setting FILE_SHARE_DELETE is the hint. */
3178 if (lp_acl_check_permissions(SNUM(conn
))
3179 && (create_disposition
!= FILE_CREATE
)
3180 && (access_mask
& DELETE_ACCESS
)
3181 && (!(can_delete_file_in_directory(conn
, smb_fname
) ||
3182 can_access_file_acl(conn
, smb_fname
, DELETE_ACCESS
)))) {
3183 status
= NT_STATUS_ACCESS_DENIED
;
3184 DEBUG(10,("create_file_unixpath: open file %s "
3185 "for delete ACCESS_DENIED\n",
3186 smb_fname_str_dbg(smb_fname
)));
3190 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3191 !security_token_has_privilege(get_current_nttok(conn
),
3192 SEC_PRIV_SECURITY
)) {
3193 DEBUG(10, ("create_file_unixpath: open on %s "
3194 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3195 smb_fname_str_dbg(smb_fname
)));
3196 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3200 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3201 && is_ntfs_stream_smb_fname(smb_fname
)
3202 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3203 uint32 base_create_disposition
;
3204 struct smb_filename
*smb_fname_base
= NULL
;
3206 if (create_options
& FILE_DIRECTORY_FILE
) {
3207 status
= NT_STATUS_NOT_A_DIRECTORY
;
3211 switch (create_disposition
) {
3213 base_create_disposition
= FILE_OPEN
;
3216 base_create_disposition
= FILE_OPEN_IF
;
3220 /* Create an smb_filename with stream_name == NULL. */
3221 status
= create_synthetic_smb_fname(talloc_tos(),
3222 smb_fname
->base_name
,
3225 if (!NT_STATUS_IS_OK(status
)) {
3229 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3230 DEBUG(10, ("Unable to stat stream: %s\n",
3231 smb_fname_str_dbg(smb_fname_base
)));
3234 /* Open the base file. */
3235 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3238 | FILE_SHARE_DELETE
,
3239 base_create_disposition
,
3240 0, 0, 0, 0, 0, NULL
, NULL
,
3242 TALLOC_FREE(smb_fname_base
);
3244 if (!NT_STATUS_IS_OK(status
)) {
3245 DEBUG(10, ("create_file_unixpath for base %s failed: "
3246 "%s\n", smb_fname
->base_name
,
3247 nt_errstr(status
)));
3250 /* we don't need to low level fd */
3255 * If it's a request for a directory open, deal with it separately.
3258 if (create_options
& FILE_DIRECTORY_FILE
) {
3260 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3261 status
= NT_STATUS_INVALID_PARAMETER
;
3265 /* Can't open a temp directory. IFS kit test. */
3266 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3267 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3268 status
= NT_STATUS_INVALID_PARAMETER
;
3273 * We will get a create directory here if the Win32
3274 * app specified a security descriptor in the
3275 * CreateDirectory() call.
3279 status
= open_directory(
3280 conn
, req
, smb_fname
, access_mask
, share_access
,
3281 create_disposition
, create_options
, file_attributes
,
3286 * Ordinary file case.
3289 status
= file_new(req
, conn
, &fsp
);
3290 if(!NT_STATUS_IS_OK(status
)) {
3294 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3295 if (!NT_STATUS_IS_OK(status
)) {
3300 * We're opening the stream element of a base_fsp
3301 * we already opened. Set up the base_fsp pointer.
3304 fsp
->base_fsp
= base_fsp
;
3307 status
= open_file_ntcreate(conn
,
3319 if(!NT_STATUS_IS_OK(status
)) {
3320 file_free(req
, fsp
);
3324 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3326 /* A stream open never opens a directory */
3329 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3334 * Fail the open if it was explicitly a non-directory
3338 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3339 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3344 status
= open_directory(
3345 conn
, req
, smb_fname
, access_mask
,
3346 share_access
, create_disposition
,
3347 create_options
, file_attributes
,
3352 if (!NT_STATUS_IS_OK(status
)) {
3356 fsp
->base_fsp
= base_fsp
;
3359 * According to the MS documentation, the only time the security
3360 * descriptor is applied to the opened file is iff we *created* the
3361 * file; an existing file stays the same.
3363 * Also, it seems (from observation) that you can open the file with
3364 * any access mask but you can still write the sd. We need to override
3365 * the granted access before we call set_sd
3366 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3369 if ((sd
!= NULL
) && (info
== FILE_WAS_CREATED
)
3370 && lp_nt_acl_support(SNUM(conn
))) {
3372 uint32_t sec_info_sent
;
3373 uint32_t saved_access_mask
= fsp
->access_mask
;
3375 sec_info_sent
= get_sec_info(sd
);
3377 fsp
->access_mask
= FILE_GENERIC_ALL
;
3379 /* Convert all the generic bits. */
3380 security_acl_map_generic(sd
->dacl
, &file_generic_mapping
);
3381 security_acl_map_generic(sd
->sacl
, &file_generic_mapping
);
3383 if (sec_info_sent
& (SECINFO_OWNER
|
3387 status
= SMB_VFS_FSET_NT_ACL(fsp
, sec_info_sent
, sd
);
3390 fsp
->access_mask
= saved_access_mask
;
3392 if (!NT_STATUS_IS_OK(status
)) {
3397 if ((ea_list
!= NULL
) &&
3398 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3399 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3400 if (!NT_STATUS_IS_OK(status
)) {
3405 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3406 status
= NT_STATUS_ACCESS_DENIED
;
3410 /* Save the requested allocation size. */
3411 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3413 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3414 fsp
->initial_allocation_size
= smb_roundup(
3415 fsp
->conn
, allocation_size
);
3416 if (fsp
->is_directory
) {
3417 /* Can't set allocation size on a directory. */
3418 status
= NT_STATUS_ACCESS_DENIED
;
3421 if (vfs_allocate_file_space(
3422 fsp
, fsp
->initial_allocation_size
) == -1) {
3423 status
= NT_STATUS_DISK_FULL
;
3427 fsp
->initial_allocation_size
= smb_roundup(
3428 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3432 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3435 if (pinfo
!= NULL
) {
3439 smb_fname
->st
= fsp
->fsp_name
->st
;
3441 return NT_STATUS_OK
;
3444 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3447 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3449 * The close_file below will close
3454 close_file(req
, fsp
, ERROR_CLOSE
);
3457 if (base_fsp
!= NULL
) {
3458 close_file(req
, base_fsp
, ERROR_CLOSE
);
3465 * Calculate the full path name given a relative fid.
3467 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
3468 struct smb_request
*req
,
3469 uint16_t root_dir_fid
,
3470 const struct smb_filename
*smb_fname
,
3471 struct smb_filename
**smb_fname_out
)
3473 files_struct
*dir_fsp
;
3474 char *parent_fname
= NULL
;
3475 char *new_base_name
= NULL
;
3478 if (root_dir_fid
== 0 || !smb_fname
) {
3479 status
= NT_STATUS_INTERNAL_ERROR
;
3483 dir_fsp
= file_fsp(req
, root_dir_fid
);
3485 if (dir_fsp
== NULL
) {
3486 status
= NT_STATUS_INVALID_HANDLE
;
3490 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
3491 status
= NT_STATUS_INVALID_HANDLE
;
3495 if (!dir_fsp
->is_directory
) {
3498 * Check to see if this is a mac fork of some kind.
3501 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
3502 is_ntfs_stream_smb_fname(smb_fname
)) {
3503 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3508 we need to handle the case when we get a
3509 relative open relative to a file and the
3510 pathname is blank - this is a reopen!
3511 (hint from demyn plantenberg)
3514 status
= NT_STATUS_INVALID_HANDLE
;
3518 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
3520 * We're at the toplevel dir, the final file name
3521 * must not contain ./, as this is filtered out
3522 * normally by srvstr_get_path and unix_convert
3523 * explicitly rejects paths containing ./.
3525 parent_fname
= talloc_strdup(talloc_tos(), "");
3526 if (parent_fname
== NULL
) {
3527 status
= NT_STATUS_NO_MEMORY
;
3531 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
3534 * Copy in the base directory name.
3537 parent_fname
= TALLOC_ARRAY(talloc_tos(), char,
3539 if (parent_fname
== NULL
) {
3540 status
= NT_STATUS_NO_MEMORY
;
3543 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
3547 * Ensure it ends in a '/'.
3548 * We used TALLOC_SIZE +2 to add space for the '/'.
3552 && (parent_fname
[dir_name_len
-1] != '\\')
3553 && (parent_fname
[dir_name_len
-1] != '/')) {
3554 parent_fname
[dir_name_len
] = '/';
3555 parent_fname
[dir_name_len
+1] = '\0';
3559 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
3560 smb_fname
->base_name
);
3561 if (new_base_name
== NULL
) {
3562 status
= NT_STATUS_NO_MEMORY
;
3566 status
= filename_convert(req
,
3568 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
3573 if (!NT_STATUS_IS_OK(status
)) {
3578 TALLOC_FREE(parent_fname
);
3579 TALLOC_FREE(new_base_name
);
3583 NTSTATUS
create_file_default(connection_struct
*conn
,
3584 struct smb_request
*req
,
3585 uint16_t root_dir_fid
,
3586 struct smb_filename
*smb_fname
,
3587 uint32_t access_mask
,
3588 uint32_t share_access
,
3589 uint32_t create_disposition
,
3590 uint32_t create_options
,
3591 uint32_t file_attributes
,
3592 uint32_t oplock_request
,
3593 uint64_t allocation_size
,
3594 uint32_t private_flags
,
3595 struct security_descriptor
*sd
,
3596 struct ea_list
*ea_list
,
3597 files_struct
**result
,
3600 int info
= FILE_WAS_OPENED
;
3601 files_struct
*fsp
= NULL
;
3603 bool stream_name
= false;
3605 DEBUG(10,("create_file: access_mask = 0x%x "
3606 "file_attributes = 0x%x, share_access = 0x%x, "
3607 "create_disposition = 0x%x create_options = 0x%x "
3608 "oplock_request = 0x%x "
3609 "private_flags = 0x%x "
3610 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3612 (unsigned int)access_mask
,
3613 (unsigned int)file_attributes
,
3614 (unsigned int)share_access
,
3615 (unsigned int)create_disposition
,
3616 (unsigned int)create_options
,
3617 (unsigned int)oplock_request
,
3618 (unsigned int)private_flags
,
3619 (unsigned int)root_dir_fid
,
3620 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3623 * Calculate the filename from the root_dir_if if necessary.
3626 if (root_dir_fid
!= 0) {
3627 struct smb_filename
*smb_fname_out
= NULL
;
3628 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
3629 smb_fname
, &smb_fname_out
);
3630 if (!NT_STATUS_IS_OK(status
)) {
3633 smb_fname
= smb_fname_out
;
3637 * Check to see if this is a mac fork of some kind.
3640 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
3642 enum FAKE_FILE_TYPE fake_file_type
;
3644 fake_file_type
= is_fake_file(smb_fname
);
3646 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
3649 * Here we go! support for changing the disk quotas
3652 * We need to fake up to open this MAGIC QUOTA file
3653 * and return a valid FID.
3655 * w2k close this file directly after openening xp
3656 * also tries a QUERY_FILE_INFO on the file and then
3659 status
= open_fake_file(req
, conn
, req
->vuid
,
3660 fake_file_type
, smb_fname
,
3662 if (!NT_STATUS_IS_OK(status
)) {
3666 ZERO_STRUCT(smb_fname
->st
);
3670 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
3671 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3676 /* All file access must go through check_name() */
3678 status
= check_name(conn
, smb_fname
->base_name
);
3679 if (!NT_STATUS_IS_OK(status
)) {
3683 if (stream_name
&& is_ntfs_default_stream_smb_fname(smb_fname
)) {
3685 smb_fname
->stream_name
= NULL
;
3686 /* We have to handle this error here. */
3687 if (create_options
& FILE_DIRECTORY_FILE
) {
3688 status
= NT_STATUS_NOT_A_DIRECTORY
;
3691 if (lp_posix_pathnames()) {
3692 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
3694 ret
= SMB_VFS_STAT(conn
, smb_fname
);
3697 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
3698 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3703 status
= create_file_unixpath(
3704 conn
, req
, smb_fname
, access_mask
, share_access
,
3705 create_disposition
, create_options
, file_attributes
,
3706 oplock_request
, allocation_size
, private_flags
,
3710 if (!NT_STATUS_IS_OK(status
)) {
3715 DEBUG(10, ("create_file: info=%d\n", info
));
3718 if (pinfo
!= NULL
) {
3721 return NT_STATUS_OK
;
3724 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
3727 close_file(req
, fsp
, ERROR_CLOSE
);