2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
34 extern const struct generic_mapping file_generic_mapping
;
36 struct deferred_open_record
{
37 bool delayed_for_oplocks
;
41 /****************************************************************************
42 If the requester wanted DELETE_ACCESS and was rejected because
43 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
45 ****************************************************************************/
47 static bool parent_override_delete(connection_struct
*conn
,
48 const struct smb_filename
*smb_fname
,
50 uint32_t rejected_mask
)
52 if ((access_mask
& DELETE_ACCESS
) &&
53 (rejected_mask
& DELETE_ACCESS
) &&
54 can_delete_file_in_directory(conn
, smb_fname
)) {
60 /****************************************************************************
61 Check if we have open rights.
62 ****************************************************************************/
64 NTSTATUS
smbd_check_access_rights(struct connection_struct
*conn
,
65 const struct smb_filename
*smb_fname
,
68 /* Check if we have rights to open. */
70 struct security_descriptor
*sd
= NULL
;
71 uint32_t rejected_share_access
;
72 uint32_t rejected_mask
= 0;
74 rejected_share_access
= access_mask
& ~(conn
->share_access
);
76 if (rejected_share_access
) {
77 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
79 (unsigned int)access_mask
,
80 smb_fname_str_dbg(smb_fname
),
81 (unsigned int)rejected_share_access
));
82 return NT_STATUS_ACCESS_DENIED
;
85 if (get_current_uid(conn
) == (uid_t
)0) {
86 /* I'm sorry sir, I didn't know you were root... */
87 DEBUG(10,("smbd_check_access_rights: root override "
88 "on %s. Granting 0x%x\n",
89 smb_fname_str_dbg(smb_fname
),
90 (unsigned int)access_mask
));
94 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
95 DEBUG(10,("smbd_check_access_rights: not checking ACL "
96 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
97 smb_fname_str_dbg(smb_fname
),
98 (unsigned int)access_mask
));
102 if (access_mask
== DELETE_ACCESS
&& S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
103 /* We can always delete a symlink. */
104 DEBUG(10,("smbd_check_access_rights: not checking ACL "
105 "on DELETE_ACCESS on symlink %s.\n",
106 smb_fname_str_dbg(smb_fname
) ));
110 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
115 if (!NT_STATUS_IS_OK(status
)) {
116 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
118 smb_fname_str_dbg(smb_fname
),
124 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
125 * owner WRITE_DAC and READ_CONTROL.
127 status
= se_access_check(sd
,
128 get_current_nttok(conn
),
129 (access_mask
& ~FILE_READ_ATTRIBUTES
),
132 DEBUG(10,("smbd_check_access_rights: file %s requesting "
133 "0x%x returning 0x%x (%s)\n",
134 smb_fname_str_dbg(smb_fname
),
135 (unsigned int)access_mask
,
136 (unsigned int)rejected_mask
,
137 nt_errstr(status
) ));
139 if (!NT_STATUS_IS_OK(status
)) {
140 if (DEBUGLEVEL
>= 10) {
141 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
142 smb_fname_str_dbg(smb_fname
) ));
143 NDR_PRINT_DEBUG(security_descriptor
, sd
);
149 if (NT_STATUS_IS_OK(status
) ||
150 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
154 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
155 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
156 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
157 (lp_map_readonly(SNUM(conn
)) ||
158 lp_map_archive(SNUM(conn
)) ||
159 lp_map_hidden(SNUM(conn
)) ||
160 lp_map_system(SNUM(conn
)))) {
161 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
163 DEBUG(10,("smbd_check_access_rights: "
165 "FILE_WRITE_ATTRIBUTES "
167 smb_fname_str_dbg(smb_fname
)));
170 if (parent_override_delete(conn
,
174 /* Were we trying to do an open
175 * for delete and didn't get DELETE
176 * access (only) ? Check if the
177 * directory allows DELETE_CHILD.
179 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
182 rejected_mask
&= ~DELETE_ACCESS
;
184 DEBUG(10,("smbd_check_access_rights: "
188 smb_fname_str_dbg(smb_fname
)));
191 if (rejected_mask
!= 0) {
192 return NT_STATUS_ACCESS_DENIED
;
198 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
199 struct smb_filename
*smb_fname
,
200 uint32_t access_mask
,
201 char **pp_parent_dir
)
204 char *parent_dir
= NULL
;
205 struct security_descriptor
*parent_sd
= NULL
;
206 uint32_t access_granted
= 0;
208 if (!parent_dirname(talloc_tos(),
209 smb_fname
->base_name
,
212 return NT_STATUS_NO_MEMORY
;
216 *pp_parent_dir
= parent_dir
;
219 if (get_current_uid(conn
) == (uid_t
)0) {
220 /* I'm sorry sir, I didn't know you were root... */
221 DEBUG(10,("check_parent_access: root override "
222 "on %s. Granting 0x%x\n",
223 smb_fname_str_dbg(smb_fname
),
224 (unsigned int)access_mask
));
228 status
= SMB_VFS_GET_NT_ACL(conn
,
233 if (!NT_STATUS_IS_OK(status
)) {
234 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
235 "%s with error %s\n",
242 * Never test FILE_READ_ATTRIBUTES. se_access_check() also takes care of
243 * owner WRITE_DAC and READ_CONTROL.
245 status
= se_access_check(parent_sd
,
246 get_current_nttok(conn
),
247 (access_mask
& ~FILE_READ_ATTRIBUTES
),
249 if(!NT_STATUS_IS_OK(status
)) {
250 DEBUG(5,("check_parent_access: access check "
251 "on directory %s for "
252 "path %s for mask 0x%x returned (0x%x) %s\n",
254 smb_fname
->base_name
,
257 nt_errstr(status
) ));
264 /****************************************************************************
265 fd support routines - attempt to do a dos_open.
266 ****************************************************************************/
268 static NTSTATUS
fd_open(struct connection_struct
*conn
,
273 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
274 NTSTATUS status
= NT_STATUS_OK
;
278 * Never follow symlinks on a POSIX client. The
279 * client should be doing this.
282 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
287 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
288 if (fsp
->fh
->fd
== -1) {
289 status
= map_nt_error_from_unix(errno
);
290 if (errno
== EMFILE
) {
291 static time_t last_warned
= 0L;
293 if (time((time_t *) NULL
) > last_warned
) {
294 DEBUG(0,("Too many open files, unable "
295 "to open more! smbd's max "
297 lp_max_open_files()));
298 last_warned
= time((time_t *) NULL
);
304 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
305 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
306 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
311 /****************************************************************************
312 Close the file associated with a fsp.
313 ****************************************************************************/
315 NTSTATUS
fd_close(files_struct
*fsp
)
322 if (fsp
->fh
->fd
== -1) {
323 return NT_STATUS_OK
; /* What we used to call a stat open. */
325 if (fsp
->fh
->ref_count
> 1) {
326 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
329 ret
= SMB_VFS_CLOSE(fsp
);
332 return map_nt_error_from_unix(errno
);
337 /****************************************************************************
338 Change the ownership of a file to that of the parent directory.
339 Do this by fd if possible.
340 ****************************************************************************/
342 void change_file_owner_to_parent(connection_struct
*conn
,
343 const char *inherit_from_dir
,
346 struct smb_filename
*smb_fname_parent
= NULL
;
350 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
351 NULL
, NULL
, &smb_fname_parent
);
352 if (!NT_STATUS_IS_OK(status
)) {
356 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
358 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
359 "directory %s. Error was %s\n",
360 smb_fname_str_dbg(smb_fname_parent
),
362 TALLOC_FREE(smb_fname_parent
);
366 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
367 /* Already this uid - no need to change. */
368 DEBUG(10,("change_file_owner_to_parent: file %s "
369 "is already owned by uid %d\n",
371 (int)fsp
->fsp_name
->st
.st_ex_uid
));
372 TALLOC_FREE(smb_fname_parent
);
377 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
380 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
381 "file %s to parent directory uid %u. Error "
382 "was %s\n", fsp_str_dbg(fsp
),
383 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
386 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
387 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
388 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
389 /* Ensure the uid entry is updated. */
390 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
393 TALLOC_FREE(smb_fname_parent
);
396 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
397 const char *inherit_from_dir
,
399 SMB_STRUCT_STAT
*psbuf
)
401 struct smb_filename
*smb_fname_parent
= NULL
;
402 struct smb_filename
*smb_fname_cwd
= NULL
;
403 char *saved_dir
= NULL
;
404 TALLOC_CTX
*ctx
= talloc_tos();
405 NTSTATUS status
= NT_STATUS_OK
;
408 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
410 if (!NT_STATUS_IS_OK(status
)) {
414 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
416 status
= map_nt_error_from_unix(errno
);
417 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
418 "directory %s. Error was %s\n",
419 smb_fname_str_dbg(smb_fname_parent
),
424 /* We've already done an lstat into psbuf, and we know it's a
425 directory. If we can cd into the directory and the dev/ino
426 are the same then we can safely chown without races as
427 we're locking the directory in place by being in it. This
428 should work on any UNIX (thanks tridge :-). JRA.
431 saved_dir
= vfs_GetWd(ctx
,conn
);
433 status
= map_nt_error_from_unix(errno
);
434 DEBUG(0,("change_dir_owner_to_parent: failed to get "
435 "current working directory. Error was %s\n",
440 /* Chdir into the new path. */
441 if (vfs_ChDir(conn
, fname
) == -1) {
442 status
= map_nt_error_from_unix(errno
);
443 DEBUG(0,("change_dir_owner_to_parent: failed to change "
444 "current working directory to %s. Error "
445 "was %s\n", fname
, strerror(errno
) ));
449 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
451 if (!NT_STATUS_IS_OK(status
)) {
455 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
457 status
= map_nt_error_from_unix(errno
);
458 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
459 "directory '.' (%s) Error was %s\n",
460 fname
, strerror(errno
)));
464 /* Ensure we're pointing at the same place. */
465 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
466 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
467 DEBUG(0,("change_dir_owner_to_parent: "
468 "device/inode on directory %s changed. "
469 "Refusing to chown !\n", fname
));
470 status
= NT_STATUS_ACCESS_DENIED
;
474 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
475 /* Already this uid - no need to change. */
476 DEBUG(10,("change_dir_owner_to_parent: directory %s "
477 "is already owned by uid %d\n",
479 (int)smb_fname_cwd
->st
.st_ex_uid
));
480 status
= NT_STATUS_OK
;
485 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
489 status
= map_nt_error_from_unix(errno
);
490 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
491 "directory %s to parent directory uid %u. "
492 "Error was %s\n", fname
,
493 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
496 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
497 "directory %s to parent directory uid %u.\n",
498 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
499 /* Ensure the uid entry is updated. */
500 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
504 vfs_ChDir(conn
,saved_dir
);
506 TALLOC_FREE(smb_fname_parent
);
507 TALLOC_FREE(smb_fname_cwd
);
511 /****************************************************************************
513 ****************************************************************************/
515 static NTSTATUS
open_file(files_struct
*fsp
,
516 connection_struct
*conn
,
517 struct smb_request
*req
,
518 const char *parent_dir
,
521 uint32 access_mask
, /* client requested access mask. */
522 uint32 open_access_mask
) /* what we're actually using in the open. */
524 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
525 NTSTATUS status
= NT_STATUS_OK
;
526 int accmode
= (flags
& O_ACCMODE
);
527 int local_flags
= flags
;
528 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
529 bool file_created
= false;
534 /* Check permissions */
537 * This code was changed after seeing a client open request
538 * containing the open mode of (DENY_WRITE/read-only) with
539 * the 'create if not exist' bit set. The previous code
540 * would fail to open the file read only on a read-only share
541 * as it was checking the flags parameter directly against O_RDONLY,
542 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
546 if (!CAN_WRITE(conn
)) {
547 /* It's a read-only share - fail if we wanted to write. */
548 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
549 DEBUG(3,("Permission denied opening %s\n",
550 smb_fname_str_dbg(smb_fname
)));
551 return NT_STATUS_ACCESS_DENIED
;
552 } else if(flags
& O_CREAT
) {
553 /* We don't want to write - but we must make sure that
554 O_CREAT doesn't create the file if we have write
555 access into the directory.
557 flags
&= ~(O_CREAT
|O_EXCL
);
558 local_flags
&= ~(O_CREAT
|O_EXCL
);
563 * This little piece of insanity is inspired by the
564 * fact that an NT client can open a file for O_RDONLY,
565 * but set the create disposition to FILE_EXISTS_TRUNCATE.
566 * If the client *can* write to the file, then it expects to
567 * truncate the file, even though it is opening for readonly.
568 * Quicken uses this stupid trick in backup file creation...
569 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
570 * for helping track this one down. It didn't bite us in 2.0.x
571 * as we always opened files read-write in that release. JRA.
574 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
575 DEBUG(10,("open_file: truncate requested on read-only open "
576 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
577 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
580 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
581 (!file_existed
&& (local_flags
& O_CREAT
)) ||
582 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
586 * We can't actually truncate here as the file may be locked.
587 * open_file_ntcreate will take care of the truncate later. JRA.
590 local_flags
&= ~O_TRUNC
;
592 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
594 * We would block on opening a FIFO with no one else on the
595 * other end. Do what we used to do and add O_NONBLOCK to the
599 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
600 local_flags
|= O_NONBLOCK
;
604 /* Don't create files with Microsoft wildcard characters. */
607 * wildcard characters are allowed in stream names
608 * only test the basefilename
610 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
612 wild
= smb_fname
->base_name
;
614 if ((local_flags
& O_CREAT
) && !file_existed
&&
616 return NT_STATUS_OBJECT_NAME_INVALID
;
619 /* Can we access this file ? */
620 if (!fsp
->base_fsp
) {
621 /* Only do this check on non-stream open. */
623 status
= smbd_check_access_rights(conn
,
626 } else if (local_flags
& O_CREAT
){
627 status
= check_parent_access(conn
,
632 /* File didn't exist and no O_CREAT. */
633 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
635 if (!NT_STATUS_IS_OK(status
)) {
636 DEBUG(10,("open_file: "
640 "smbd_check_access_rights" :
641 "check_parent_access",
642 smb_fname_str_dbg(smb_fname
),
643 nt_errstr(status
) ));
648 /* Actually do the open */
649 status
= fd_open(conn
, fsp
, local_flags
, unx_mode
);
650 if (!NT_STATUS_IS_OK(status
)) {
651 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
652 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
653 nt_errstr(status
),local_flags
,flags
));
657 if ((local_flags
& O_CREAT
) && !file_existed
) {
662 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
664 /* File must exist for a stat open. */
665 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
668 status
= smbd_check_access_rights(conn
,
672 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
674 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
675 /* This is a POSIX stat open for delete
676 * or rename on a symlink that points
678 DEBUG(10,("open_file: allowing POSIX "
679 "open on bad symlink %s\n",
680 smb_fname_str_dbg(smb_fname
)));
681 status
= NT_STATUS_OK
;
684 if (!NT_STATUS_IS_OK(status
)) {
685 DEBUG(10,("open_file: "
686 "smbd_check_access_rights on file "
688 smb_fname_str_dbg(smb_fname
),
689 nt_errstr(status
) ));
697 if (fsp
->fh
->fd
== -1) {
698 ret
= SMB_VFS_STAT(conn
, smb_fname
);
700 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
701 /* If we have an fd, this stat should succeed. */
703 DEBUG(0,("Error doing fstat on open file %s "
705 smb_fname_str_dbg(smb_fname
),
710 /* For a non-io open, this stat failing means file not found. JRA */
712 status
= map_nt_error_from_unix(errno
);
718 bool need_re_stat
= false;
719 /* Do all inheritance work after we've
720 done a successful stat call and filled
721 in the stat struct in fsp->fsp_name. */
723 /* Inherit the ACL if required */
724 if (lp_inherit_perms(SNUM(conn
))) {
725 inherit_access_posix_acl(conn
, parent_dir
,
726 smb_fname
->base_name
,
731 /* Change the owner if required. */
732 if (lp_inherit_owner(SNUM(conn
))) {
733 change_file_owner_to_parent(conn
, parent_dir
,
739 if (fsp
->fh
->fd
== -1) {
740 ret
= SMB_VFS_STAT(conn
, smb_fname
);
742 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
743 /* If we have an fd, this stat should succeed. */
745 DEBUG(0,("Error doing fstat on open file %s "
747 smb_fname_str_dbg(smb_fname
),
753 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
754 FILE_NOTIFY_CHANGE_FILE_NAME
,
755 smb_fname
->base_name
);
760 * POSIX allows read-only opens of directories. We don't
761 * want to do this (we use a different code path for this)
762 * so catch a directory open and return an EISDIR. JRA.
765 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
768 return NT_STATUS_FILE_IS_A_DIRECTORY
;
771 fsp
->mode
= smb_fname
->st
.st_ex_mode
;
772 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
773 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
774 fsp
->file_pid
= req
? req
->smbpid
: 0;
775 fsp
->can_lock
= True
;
776 fsp
->can_read
= (access_mask
& (FILE_READ_DATA
)) ? True
: False
;
777 if (!CAN_WRITE(conn
)) {
778 fsp
->can_write
= False
;
780 fsp
->can_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
783 fsp
->print_file
= NULL
;
784 fsp
->modified
= False
;
785 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
786 fsp
->is_directory
= False
;
787 if (conn
->aio_write_behind_list
&&
788 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
789 conn
->case_sensitive
)) {
790 fsp
->aio_write_behind
= True
;
793 fsp
->wcp
= NULL
; /* Write cache pointer. */
795 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
796 conn
->session_info
->unix_info
->unix_name
,
797 smb_fname_str_dbg(smb_fname
),
798 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
799 conn
->num_files_open
));
805 /****************************************************************************
806 Check if we can open a file with a share mode.
807 Returns True if conflict, False if not.
808 ****************************************************************************/
810 static bool share_conflict(struct share_mode_entry
*entry
,
814 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
815 "entry->share_access = 0x%x, "
816 "entry->private_options = 0x%x\n",
817 (unsigned int)entry
->access_mask
,
818 (unsigned int)entry
->share_access
,
819 (unsigned int)entry
->private_options
));
821 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
822 (unsigned int)access_mask
, (unsigned int)share_access
));
824 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
828 DELETE_ACCESS
)) == 0) {
829 DEBUG(10,("share_conflict: No conflict due to "
830 "entry->access_mask = 0x%x\n",
831 (unsigned int)entry
->access_mask
));
835 if ((access_mask
& (FILE_WRITE_DATA
|
839 DELETE_ACCESS
)) == 0) {
840 DEBUG(10,("share_conflict: No conflict due to "
841 "access_mask = 0x%x\n",
842 (unsigned int)access_mask
));
846 #if 1 /* JRA TEST - Superdebug. */
847 #define CHECK_MASK(num, am, right, sa, share) \
848 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
849 (unsigned int)(num), (unsigned int)(am), \
850 (unsigned int)(right), (unsigned int)(am)&(right) )); \
851 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
852 (unsigned int)(num), (unsigned int)(sa), \
853 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
854 if (((am) & (right)) && !((sa) & (share))) { \
855 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
856 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
857 (unsigned int)(share) )); \
861 #define CHECK_MASK(num, am, right, sa, share) \
862 if (((am) & (right)) && !((sa) & (share))) { \
863 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
864 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
865 (unsigned int)(share) )); \
870 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
871 share_access
, FILE_SHARE_WRITE
);
872 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
873 entry
->share_access
, FILE_SHARE_WRITE
);
875 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
876 share_access
, FILE_SHARE_READ
);
877 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
878 entry
->share_access
, FILE_SHARE_READ
);
880 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
881 share_access
, FILE_SHARE_DELETE
);
882 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
883 entry
->share_access
, FILE_SHARE_DELETE
);
885 DEBUG(10,("share_conflict: No conflict.\n"));
889 #if defined(DEVELOPER)
890 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
892 struct share_mode_entry
*share_entry
)
896 if (!procid_is_me(&share_entry
->pid
)) {
900 if (is_deferred_open_entry(share_entry
) &&
901 !open_was_deferred(sconn
, share_entry
->op_mid
)) {
902 char *str
= talloc_asprintf(talloc_tos(),
903 "Got a deferred entry without a request: "
905 share_mode_str(talloc_tos(), num
, share_entry
));
909 if (!is_valid_share_mode_entry(share_entry
)) {
913 fsp
= file_find_dif(sconn
, share_entry
->id
,
914 share_entry
->share_file_id
);
916 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
917 share_mode_str(talloc_tos(), num
, share_entry
) ));
918 smb_panic("validate_my_share_entries: Cannot match a "
919 "share entry with an open file\n");
922 if (is_deferred_open_entry(share_entry
)) {
926 if ((share_entry
->op_type
== NO_OPLOCK
) &&
927 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
928 /* Someone has already written to it, but I haven't yet
933 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
942 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
943 share_mode_str(talloc_tos(), num
, share_entry
) ));
944 str
= talloc_asprintf(talloc_tos(),
945 "validate_my_share_entries: "
946 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
947 fsp
->fsp_name
->base_name
,
948 (unsigned int)fsp
->oplock_type
,
949 (unsigned int)share_entry
->op_type
);
955 bool is_stat_open(uint32 access_mask
)
957 return (access_mask
&&
958 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
959 FILE_WRITE_ATTRIBUTES
))==0) &&
960 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
961 FILE_WRITE_ATTRIBUTES
)) != 0));
964 /****************************************************************************
965 Deal with share modes
966 Invarient: Share mode must be locked on entry and exit.
967 Returns -1 on error, or number of share modes on success (may be zero).
968 ****************************************************************************/
970 static NTSTATUS
open_mode_check(connection_struct
*conn
,
971 struct share_mode_lock
*lck
,
975 uint32 create_options
,
980 if(lck
->num_share_modes
== 0) {
984 *file_existed
= True
;
986 /* A delete on close prohibits everything */
988 if (is_delete_on_close_set(lck
, name_hash
)) {
989 return NT_STATUS_DELETE_PENDING
;
992 if (is_stat_open(access_mask
)) {
993 /* Stat open that doesn't trigger oplock breaks or share mode
994 * checks... ! JRA. */
999 * Check if the share modes will give us access.
1002 #if defined(DEVELOPER)
1003 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
1004 validate_my_share_entries(conn
->sconn
, i
,
1005 &lck
->share_modes
[i
]);
1009 if (!lp_share_modes(SNUM(conn
))) {
1010 return NT_STATUS_OK
;
1013 /* Now we check the share modes, after any oplock breaks. */
1014 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
1016 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
1020 /* someone else has a share lock on it, check to see if we can
1022 if (share_conflict(&lck
->share_modes
[i
],
1023 access_mask
, share_access
)) {
1024 return NT_STATUS_SHARING_VIOLATION
;
1028 return NT_STATUS_OK
;
1031 static bool is_delete_request(files_struct
*fsp
) {
1032 return ((fsp
->access_mask
== DELETE_ACCESS
) &&
1033 (fsp
->oplock_type
== NO_OPLOCK
));
1037 * Send a break message to the oplock holder and delay the open for
1041 static NTSTATUS
send_break_message(files_struct
*fsp
,
1042 struct share_mode_entry
*exclusive
,
1047 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1049 DEBUG(10, ("Sending break request to PID %s\n",
1050 procid_str_static(&exclusive
->pid
)));
1051 exclusive
->op_mid
= mid
;
1053 /* Create the message. */
1054 share_mode_entry_to_message(msg
, exclusive
);
1056 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1057 don't want this set in the share mode struct pointed to by lck. */
1059 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
1060 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
1061 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
1064 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
1065 MSG_SMB_BREAK_REQUEST
,
1067 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
1068 if (!NT_STATUS_IS_OK(status
)) {
1069 DEBUG(3, ("Could not send oplock break message: %s\n",
1070 nt_errstr(status
)));
1077 * Return share_mode_entry pointers for :
1078 * 1). Batch oplock entry.
1079 * 2). Batch or exclusive oplock entry (may be identical to #1).
1080 * bool have_level2_oplock
1081 * bool have_no_oplock.
1082 * Do internal consistency checks on the share mode for a file.
1085 static void find_oplock_types(files_struct
*fsp
,
1087 struct share_mode_lock
*lck
,
1088 struct share_mode_entry
**pp_batch
,
1089 struct share_mode_entry
**pp_ex_or_batch
,
1091 bool *got_no_oplock
)
1096 *pp_ex_or_batch
= NULL
;
1097 *got_level2
= false;
1098 *got_no_oplock
= false;
1100 /* Ignore stat or internal opens, as is done in
1101 delay_for_batch_oplocks() and
1102 delay_for_exclusive_oplocks().
1104 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1108 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1109 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
1113 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
&&
1114 is_stat_open(lck
->share_modes
[i
].access_mask
)) {
1115 /* We ignore stat opens in the table - they
1116 always have NO_OPLOCK and never get or
1117 cause breaks. JRA. */
1121 if (BATCH_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1122 /* batch - can only be one. */
1123 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
1124 smb_panic("Bad batch oplock entry.");
1126 *pp_batch
= &lck
->share_modes
[i
];
1129 if (EXCLUSIVE_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1130 /* Exclusive or batch - can only be one. */
1131 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
1132 smb_panic("Bad exclusive or batch oplock entry.");
1134 *pp_ex_or_batch
= &lck
->share_modes
[i
];
1137 if (LEVEL_II_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
1138 if (*pp_batch
|| *pp_ex_or_batch
) {
1139 smb_panic("Bad levelII oplock entry.");
1144 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
) {
1145 if (*pp_batch
|| *pp_ex_or_batch
) {
1146 smb_panic("Bad no oplock entry.");
1148 *got_no_oplock
= true;
1153 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1156 struct share_mode_entry
*batch_entry
)
1158 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1162 if (batch_entry
!= NULL
) {
1163 /* Found a batch oplock */
1164 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1170 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1173 struct share_mode_entry
*ex_entry
)
1175 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1179 if (ex_entry
!= NULL
) {
1180 /* Found an exclusive or batch oplock */
1181 bool delay_it
= is_delete_request(fsp
) ?
1182 BATCH_OPLOCK_TYPE(ex_entry
->op_type
) : true;
1184 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1191 static void grant_fsp_oplock_type(files_struct
*fsp
,
1192 const struct byte_range_lock
*br_lck
,
1194 bool got_level2_oplock
,
1195 bool got_a_none_oplock
)
1197 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1198 lp_level2_oplocks(SNUM(fsp
->conn
));
1200 /* Start by granting what the client asked for,
1201 but ensure no SAMBA_PRIVATE bits can be set. */
1202 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1204 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1205 /* No oplocks on internal open. */
1206 fsp
->oplock_type
= NO_OPLOCK
;
1207 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1208 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1210 } else if (br_lck
&& br_lck
->num_locks
> 0) {
1211 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1213 fsp
->oplock_type
= NO_OPLOCK
;
1216 if (is_stat_open(fsp
->access_mask
)) {
1217 /* Leave the value already set. */
1218 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1219 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1224 * Match what was requested (fsp->oplock_type) with
1225 * what was found in the existing share modes.
1228 if (got_a_none_oplock
) {
1229 fsp
->oplock_type
= NO_OPLOCK
;
1230 } else if (got_level2_oplock
) {
1231 if (fsp
->oplock_type
== NO_OPLOCK
||
1232 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1233 /* Store a level2 oplock, but don't tell the client */
1234 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1236 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1239 /* All share_mode_entries are placeholders or deferred.
1240 * Silently upgrade to fake levelII if the client didn't
1241 * ask for an oplock. */
1242 if (fsp
->oplock_type
== NO_OPLOCK
) {
1243 /* Store a level2 oplock, but don't tell the client */
1244 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1249 * Don't grant level2 to clients that don't want them
1250 * or if we've turned them off.
1252 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1253 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1256 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1257 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1260 bool request_timed_out(struct timeval request_time
,
1261 struct timeval timeout
)
1263 struct timeval now
, end_time
;
1265 end_time
= timeval_sum(&request_time
, &timeout
);
1266 return (timeval_compare(&end_time
, &now
) < 0);
1269 /****************************************************************************
1270 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1271 ****************************************************************************/
1273 static void defer_open(struct share_mode_lock
*lck
,
1274 struct timeval request_time
,
1275 struct timeval timeout
,
1276 struct smb_request
*req
,
1277 struct deferred_open_record
*state
)
1281 /* Paranoia check */
1283 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1284 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1286 if (is_deferred_open_entry(e
) &&
1287 procid_is_me(&e
->pid
) &&
1288 (e
->op_mid
== req
->mid
)) {
1289 DEBUG(0, ("Trying to defer an already deferred "
1290 "request: mid=%llu, exiting\n",
1291 (unsigned long long)req
->mid
));
1292 exit_server("attempt to defer a deferred request");
1296 /* End paranoia check */
1298 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1299 "open entry for mid %llu\n",
1300 (unsigned int)request_time
.tv_sec
,
1301 (unsigned int)request_time
.tv_usec
,
1302 (unsigned long long)req
->mid
));
1304 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1305 state
->id
, (char *)state
, sizeof(*state
))) {
1306 exit_server("push_deferred_open_message_smb failed");
1308 add_deferred_open(lck
, req
->mid
, request_time
,
1309 sconn_server_id(req
->sconn
), state
->id
);
1313 /****************************************************************************
1314 On overwrite open ensure that the attributes match.
1315 ****************************************************************************/
1317 bool open_match_attributes(connection_struct
*conn
,
1318 uint32 old_dos_attr
,
1319 uint32 new_dos_attr
,
1320 mode_t existing_unx_mode
,
1321 mode_t new_unx_mode
,
1322 mode_t
*returned_unx_mode
)
1324 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1326 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1327 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1329 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1330 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1331 *returned_unx_mode
= new_unx_mode
;
1333 *returned_unx_mode
= (mode_t
)0;
1336 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1337 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1338 "returned_unx_mode = 0%o\n",
1339 (unsigned int)old_dos_attr
,
1340 (unsigned int)existing_unx_mode
,
1341 (unsigned int)new_dos_attr
,
1342 (unsigned int)*returned_unx_mode
));
1344 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1345 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1346 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1347 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1351 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1352 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1353 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1360 /****************************************************************************
1361 Special FCB or DOS processing in the case of a sharing violation.
1362 Try and find a duplicated file handle.
1363 ****************************************************************************/
1365 NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1366 connection_struct
*conn
,
1367 files_struct
*fsp_to_dup_into
,
1368 const struct smb_filename
*smb_fname
,
1373 uint32 share_access
,
1374 uint32 create_options
)
1378 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1379 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1381 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1382 fsp
= file_find_di_next(fsp
)) {
1384 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1385 "vuid = %u, file_pid = %u, private_options = 0x%x "
1386 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1387 fsp
->fh
->fd
, (unsigned int)fsp
->vuid
,
1388 (unsigned int)fsp
->file_pid
,
1389 (unsigned int)fsp
->fh
->private_options
,
1390 (unsigned int)fsp
->access_mask
));
1392 if (fsp
->fh
->fd
!= -1 &&
1393 fsp
->vuid
== vuid
&&
1394 fsp
->file_pid
== file_pid
&&
1395 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1396 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1397 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1398 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1399 strequal(fsp
->fsp_name
->stream_name
,
1400 smb_fname
->stream_name
)) {
1401 DEBUG(10,("fcb_or_dos_open: file match\n"));
1407 return NT_STATUS_NOT_FOUND
;
1410 /* quite an insane set of semantics ... */
1411 if (is_executable(smb_fname
->base_name
) &&
1412 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1413 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1414 return NT_STATUS_INVALID_PARAMETER
;
1417 /* We need to duplicate this fsp. */
1418 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1419 create_options
, fsp_to_dup_into
);
1422 static void schedule_defer_open(struct share_mode_lock
*lck
,
1423 struct timeval request_time
,
1424 struct smb_request
*req
)
1426 struct deferred_open_record state
;
1428 /* This is a relative time, added to the absolute
1429 request_time value to get the absolute timeout time.
1430 Note that if this is the second or greater time we enter
1431 this codepath for this particular request mid then
1432 request_time is left as the absolute time of the *first*
1433 time this request mid was processed. This is what allows
1434 the request to eventually time out. */
1436 struct timeval timeout
;
1438 /* Normally the smbd we asked should respond within
1439 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1440 * the client did, give twice the timeout as a safety
1441 * measure here in case the other smbd is stuck
1442 * somewhere else. */
1444 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1446 /* Nothing actually uses state.delayed_for_oplocks
1447 but it's handy to differentiate in debug messages
1448 between a 30 second delay due to oplock break, and
1449 a 1 second delay for share mode conflicts. */
1451 state
.delayed_for_oplocks
= True
;
1454 if (!request_timed_out(request_time
, timeout
)) {
1455 defer_open(lck
, request_time
, timeout
, req
, &state
);
1459 /****************************************************************************
1460 Work out what access_mask to use from what the client sent us.
1461 ****************************************************************************/
1463 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1464 const struct smb_filename
*smb_fname
,
1466 uint32_t access_mask
,
1467 uint32_t *access_mask_out
)
1470 uint32_t orig_access_mask
= access_mask
;
1471 uint32_t rejected_share_access
;
1474 * Convert GENERIC bits to specific bits.
1477 se_map_generic(&access_mask
, &file_generic_mapping
);
1479 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1480 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1481 if (get_current_uid(conn
) == (uid_t
)0) {
1482 access_mask
|= FILE_GENERIC_ALL
;
1483 } else if (file_existed
) {
1485 struct security_descriptor
*sd
;
1486 uint32_t access_granted
= 0;
1488 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1493 if (!NT_STATUS_IS_OK(status
)) {
1494 DEBUG(10,("smbd_calculate_access_mask: "
1495 "Could not get acl on file %s: %s\n",
1496 smb_fname_str_dbg(smb_fname
),
1497 nt_errstr(status
)));
1498 return NT_STATUS_ACCESS_DENIED
;
1502 * Never test FILE_READ_ATTRIBUTES. se_access_check()
1503 * also takes care of owner WRITE_DAC and READ_CONTROL.
1505 status
= se_access_check(sd
,
1506 get_current_nttok(conn
),
1507 (access_mask
& ~FILE_READ_ATTRIBUTES
),
1512 if (!NT_STATUS_IS_OK(status
)) {
1513 DEBUG(10, ("smbd_calculate_access_mask: "
1514 "Access denied on file %s: "
1515 "when calculating maximum access\n",
1516 smb_fname_str_dbg(smb_fname
)));
1517 return NT_STATUS_ACCESS_DENIED
;
1520 access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
1522 access_mask
= FILE_GENERIC_ALL
;
1525 access_mask
&= conn
->share_access
;
1528 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1530 if (rejected_share_access
) {
1531 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1532 "file %s: rejected by share access mask[0x%08X] "
1533 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1534 smb_fname_str_dbg(smb_fname
),
1536 orig_access_mask
, access_mask
,
1537 rejected_share_access
));
1538 return NT_STATUS_ACCESS_DENIED
;
1541 *access_mask_out
= access_mask
;
1542 return NT_STATUS_OK
;
1545 /****************************************************************************
1546 Remove the deferred open entry under lock.
1547 ****************************************************************************/
1549 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1550 struct server_id pid
)
1552 struct share_mode_lock
*lck
= get_share_mode_lock(talloc_tos(), id
,
1555 DEBUG(0, ("could not get share mode lock\n"));
1558 del_deferred_open_entry(lck
, mid
, pid
);
1562 /****************************************************************
1563 Ensure we get the brlock lock followed by the share mode lock
1564 in the correct order to prevent deadlocks if other smbd's are
1565 using the brlock database on this file simultaneously with this open
1566 (that code also gets the locks in brlock -> share mode lock order).
1567 ****************************************************************/
1569 static bool acquire_ordered_locks(TALLOC_CTX
*mem_ctx
,
1571 const struct file_id id
,
1572 const char *connectpath
,
1573 const struct smb_filename
*smb_fname
,
1574 const struct timespec
*p_old_write_time
,
1575 struct share_mode_lock
**p_lck
,
1576 struct byte_range_lock
**p_br_lck
)
1578 /* Ordering - we must get the br_lck for this
1579 file before the share mode. */
1580 if (lp_locking(fsp
->conn
->params
)) {
1581 *p_br_lck
= brl_get_locks_readonly(fsp
);
1582 if (*p_br_lck
== NULL
) {
1583 DEBUG(0, ("Could not get br_lock\n"));
1586 /* Note - we don't need to free the returned
1587 br_lck explicitly as it was allocated on talloc_tos()
1588 and so will be autofreed (and release the lock)
1589 once the frame context disappears.
1591 If it was set to fsp->brlock_rec then it was
1592 talloc_move'd to hang off the fsp pointer and
1593 in this case is guarenteed to not be holding the
1594 lock on the brlock database. */
1597 *p_lck
= get_share_mode_lock(mem_ctx
,
1603 if (*p_lck
== NULL
) {
1604 DEBUG(0, ("Could not get share mode lock\n"));
1605 TALLOC_FREE(*p_br_lck
);
1611 /****************************************************************************
1612 Open a file with a share mode. Passed in an already created files_struct *.
1613 ****************************************************************************/
1615 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1616 struct smb_request
*req
,
1617 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1618 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1619 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1620 uint32 create_options
, /* options such as delete on close. */
1621 uint32 new_dos_attributes
, /* attributes used for new file. */
1622 int oplock_request
, /* internal Samba oplock codes. */
1623 /* Information (FILE_EXISTS etc.) */
1624 uint32_t private_flags
, /* Samba specific flags. */
1628 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1631 bool file_existed
= VALID_STAT(smb_fname
->st
);
1632 bool def_acl
= False
;
1633 bool posix_open
= False
;
1634 bool new_file_created
= False
;
1635 bool clear_ads
= false;
1637 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1638 mode_t new_unx_mode
= (mode_t
)0;
1639 mode_t unx_mode
= (mode_t
)0;
1641 uint32 existing_dos_attributes
= 0;
1642 struct timeval request_time
= timeval_zero();
1643 struct share_mode_lock
*lck
= NULL
;
1644 uint32 open_access_mask
= access_mask
;
1650 if (conn
->printer
) {
1652 * Printers are handled completely differently.
1653 * Most of the passed parameters are ignored.
1657 *pinfo
= FILE_WAS_CREATED
;
1660 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1661 smb_fname_str_dbg(smb_fname
)));
1664 DEBUG(0,("open_file_ntcreate: printer open without "
1665 "an SMB request!\n"));
1666 return NT_STATUS_INTERNAL_ERROR
;
1669 return print_spool_open(fsp
, smb_fname
->base_name
,
1673 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1675 return NT_STATUS_NO_MEMORY
;
1678 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1680 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
1681 new_dos_attributes
= 0;
1683 /* Windows allows a new file to be created and
1684 silently removes a FILE_ATTRIBUTE_DIRECTORY
1685 sent by the client. Do the same. */
1687 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
1689 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1691 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
1692 smb_fname
, parent_dir
);
1695 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1696 "access_mask=0x%x share_access=0x%x "
1697 "create_disposition = 0x%x create_options=0x%x "
1698 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1699 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
1700 access_mask
, share_access
, create_disposition
,
1701 create_options
, (unsigned int)unx_mode
, oplock_request
,
1702 (unsigned int)private_flags
));
1704 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
1705 DEBUG(0, ("No smb request but not an internal only open!\n"));
1706 return NT_STATUS_INTERNAL_ERROR
;
1710 * Only non-internal opens can be deferred at all
1715 if (get_deferred_open_message_state(req
,
1719 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
1720 /* Remember the absolute time of the original
1721 request with this mid. We'll use it later to
1722 see if this has timed out. */
1724 /* Remove the deferred open entry under lock. */
1725 remove_deferred_open_entry(
1726 state
->id
, req
->mid
,
1727 sconn_server_id(req
->sconn
));
1729 /* Ensure we don't reprocess this message. */
1730 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
1735 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
1737 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
1741 /* ignore any oplock requests if oplocks are disabled */
1742 if (!lp_oplocks(SNUM(conn
)) ||
1743 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
1744 /* Mask off everything except the private Samba bits. */
1745 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
1748 /* this is for OS/2 long file names - say we don't support them */
1749 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
1750 /* OS/2 Workplace shell fix may be main code stream in a later
1752 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1754 if (use_nt_status()) {
1755 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1757 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
1760 switch( create_disposition
) {
1762 * Currently we're using FILE_SUPERSEDE as the same as
1763 * FILE_OVERWRITE_IF but they really are
1764 * different. FILE_SUPERSEDE deletes an existing file
1765 * (requiring delete access) then recreates it.
1767 case FILE_SUPERSEDE
:
1768 /* If file exists replace/overwrite. If file doesn't
1770 flags2
|= (O_CREAT
| O_TRUNC
);
1774 case FILE_OVERWRITE_IF
:
1775 /* If file exists replace/overwrite. If file doesn't
1777 flags2
|= (O_CREAT
| O_TRUNC
);
1782 /* If file exists open. If file doesn't exist error. */
1783 if (!file_existed
) {
1784 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1785 "requested for file %s and file "
1787 smb_fname_str_dbg(smb_fname
)));
1789 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1793 case FILE_OVERWRITE
:
1794 /* If file exists overwrite. If file doesn't exist
1796 if (!file_existed
) {
1797 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1798 "requested for file %s and file "
1800 smb_fname_str_dbg(smb_fname
) ));
1802 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1809 /* If file exists error. If file doesn't exist
1812 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1813 "requested for file %s and file "
1814 "already exists.\n",
1815 smb_fname_str_dbg(smb_fname
)));
1816 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1821 return map_nt_error_from_unix(errno
);
1823 flags2
|= (O_CREAT
|O_EXCL
);
1827 /* If file exists open. If file doesn't exist
1833 return NT_STATUS_INVALID_PARAMETER
;
1836 /* We only care about matching attributes on file exists and
1839 if (!posix_open
&& file_existed
&& ((create_disposition
== FILE_OVERWRITE
) ||
1840 (create_disposition
== FILE_OVERWRITE_IF
))) {
1841 if (!open_match_attributes(conn
, existing_dos_attributes
,
1843 smb_fname
->st
.st_ex_mode
,
1844 unx_mode
, &new_unx_mode
)) {
1845 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1846 "for file %s (%x %x) (0%o, 0%o)\n",
1847 smb_fname_str_dbg(smb_fname
),
1848 existing_dos_attributes
,
1850 (unsigned int)smb_fname
->st
.st_ex_mode
,
1851 (unsigned int)unx_mode
));
1853 return NT_STATUS_ACCESS_DENIED
;
1857 status
= smbd_calculate_access_mask(conn
, smb_fname
, file_existed
,
1860 if (!NT_STATUS_IS_OK(status
)) {
1861 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
1862 "on file %s returned %s\n",
1863 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
1867 open_access_mask
= access_mask
;
1869 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1870 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
1873 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1874 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
1878 * Note that we ignore the append flag as append does not
1879 * mean the same thing under DOS and Unix.
1882 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
1883 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1884 /* DENY_DOS opens are always underlying read-write on the
1885 file handle, no matter what the requested access mask
1887 if ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
1888 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
1898 * Currently we only look at FILE_WRITE_THROUGH for create options.
1902 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
1907 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
1911 if (!posix_open
&& !CAN_WRITE(conn
)) {
1913 * We should really return a permission denied error if either
1914 * O_CREAT or O_TRUNC are set, but for compatibility with
1915 * older versions of Samba we just AND them out.
1917 flags2
&= ~(O_CREAT
|O_TRUNC
);
1921 * Ensure we can't write on a read-only share or file.
1924 if (flags
!= O_RDONLY
&& file_existed
&&
1925 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
1926 DEBUG(5,("open_file_ntcreate: write access requested for "
1927 "file %s on read only %s\n",
1928 smb_fname_str_dbg(smb_fname
),
1929 !CAN_WRITE(conn
) ? "share" : "file" ));
1931 return NT_STATUS_ACCESS_DENIED
;
1934 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1935 fsp
->share_access
= share_access
;
1936 fsp
->fh
->private_options
= private_flags
;
1937 fsp
->access_mask
= open_access_mask
; /* We change this to the
1938 * requested access_mask after
1939 * the open is done. */
1940 fsp
->posix_open
= posix_open
;
1942 /* Ensure no SAMBA_PRIVATE bits can be set. */
1943 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1945 if (timeval_is_zero(&request_time
)) {
1946 request_time
= fsp
->open_time
;
1950 struct byte_range_lock
*br_lck
= NULL
;
1951 struct share_mode_entry
*batch_entry
= NULL
;
1952 struct share_mode_entry
*exclusive_entry
= NULL
;
1953 bool got_level2_oplock
= false;
1954 bool got_a_none_oplock
= false;
1956 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
1957 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1959 if (!acquire_ordered_locks(talloc_tos(),
1967 return NT_STATUS_SHARING_VIOLATION
;
1970 /* Get the types we need to examine. */
1971 find_oplock_types(fsp
,
1977 &got_a_none_oplock
);
1979 /* First pass - send break only on batch oplocks. */
1980 if ((req
!= NULL
) &&
1981 delay_for_batch_oplocks(fsp
,
1985 schedule_defer_open(lck
, request_time
, req
);
1987 return NT_STATUS_SHARING_VIOLATION
;
1990 /* Use the client requested access mask here, not the one we
1992 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
1993 access_mask
, share_access
,
1994 create_options
, &file_existed
);
1996 if (NT_STATUS_IS_OK(status
)) {
1997 /* We might be going to allow this open. Check oplock
1999 /* Second pass - send break for both batch or
2000 * exclusive oplocks. */
2001 if ((req
!= NULL
) &&
2002 delay_for_exclusive_oplocks(
2007 schedule_defer_open(lck
, request_time
, req
);
2009 return NT_STATUS_SHARING_VIOLATION
;
2013 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
2014 /* DELETE_PENDING is not deferred for a second */
2019 grant_fsp_oplock_type(fsp
,
2025 if (!NT_STATUS_IS_OK(status
)) {
2026 uint32 can_access_mask
;
2027 bool can_access
= True
;
2029 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2031 /* Check if this can be done with the deny_dos and fcb
2034 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2035 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2037 DEBUG(0, ("DOS open without an SMB "
2040 return NT_STATUS_INTERNAL_ERROR
;
2043 /* Use the client requested access mask here,
2044 * not the one we open with. */
2045 status
= fcb_or_dos_open(req
,
2056 if (NT_STATUS_IS_OK(status
)) {
2059 *pinfo
= FILE_WAS_OPENED
;
2061 return NT_STATUS_OK
;
2066 * This next line is a subtlety we need for
2067 * MS-Access. If a file open will fail due to share
2068 * permissions and also for security (access) reasons,
2069 * we need to return the access failed error, not the
2070 * share error. We can't open the file due to kernel
2071 * oplock deadlock (it's possible we failed above on
2072 * the open_mode_check()) so use a userspace check.
2075 if (flags
& O_RDWR
) {
2076 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2077 } else if (flags
& O_WRONLY
) {
2078 can_access_mask
= FILE_WRITE_DATA
;
2080 can_access_mask
= FILE_READ_DATA
;
2083 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2084 !CAN_WRITE(conn
)) ||
2085 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
2086 smb_fname
, can_access_mask
))) {
2091 * If we're returning a share violation, ensure we
2092 * cope with the braindead 1 second delay.
2095 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2096 lp_defer_sharing_violations()) {
2097 struct timeval timeout
;
2098 struct deferred_open_record state
;
2101 /* this is a hack to speed up torture tests
2103 timeout_usecs
= lp_parm_int(SNUM(conn
),
2104 "smbd","sharedelay",
2105 SHARING_VIOLATION_USEC_WAIT
);
2107 /* This is a relative time, added to the absolute
2108 request_time value to get the absolute timeout time.
2109 Note that if this is the second or greater time we enter
2110 this codepath for this particular request mid then
2111 request_time is left as the absolute time of the *first*
2112 time this request mid was processed. This is what allows
2113 the request to eventually time out. */
2115 timeout
= timeval_set(0, timeout_usecs
);
2117 /* Nothing actually uses state.delayed_for_oplocks
2118 but it's handy to differentiate in debug messages
2119 between a 30 second delay due to oplock break, and
2120 a 1 second delay for share mode conflicts. */
2122 state
.delayed_for_oplocks
= False
;
2126 && !request_timed_out(request_time
,
2128 defer_open(lck
, request_time
, timeout
,
2136 * We have detected a sharing violation here
2137 * so return the correct error code
2139 status
= NT_STATUS_SHARING_VIOLATION
;
2141 status
= NT_STATUS_ACCESS_DENIED
;
2147 * We exit this block with the share entry *locked*.....
2151 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
2154 * Ensure we pay attention to default ACLs on directories if required.
2157 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2158 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2159 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2162 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2163 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2164 (unsigned int)flags
, (unsigned int)flags2
,
2165 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2166 (unsigned int)open_access_mask
));
2169 * open_file strips any O_TRUNC flags itself.
2172 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2173 flags
|flags2
, unx_mode
, access_mask
,
2176 if (!NT_STATUS_IS_OK(fsp_open
)) {
2181 if (!file_existed
) {
2182 struct byte_range_lock
*br_lck
= NULL
;
2183 struct share_mode_entry
*batch_entry
= NULL
;
2184 struct share_mode_entry
*exclusive_entry
= NULL
;
2185 bool got_level2_oplock
= false;
2186 bool got_a_none_oplock
= false;
2187 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2189 * Deal with the race condition where two smbd's detect the
2190 * file doesn't exist and do the create at the same time. One
2191 * of them will win and set a share mode, the other (ie. this
2192 * one) should check if the requested share mode for this
2193 * create is allowed.
2197 * Now the file exists and fsp is successfully opened,
2198 * fsp->dev and fsp->inode are valid and should replace the
2199 * dev=0,inode=0 from a non existent file. Spotted by
2200 * Nadav Danieli <nadavd@exanet.com>. JRA.
2205 if (!acquire_ordered_locks(talloc_tos(),
2213 return NT_STATUS_SHARING_VIOLATION
;
2216 /* Get the types we need to examine. */
2217 find_oplock_types(fsp
,
2223 &got_a_none_oplock
);
2225 /* First pass - send break only on batch oplocks. */
2226 if ((req
!= NULL
) &&
2227 delay_for_batch_oplocks(fsp
,
2231 schedule_defer_open(lck
, request_time
, req
);
2234 return NT_STATUS_SHARING_VIOLATION
;
2237 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2238 access_mask
, share_access
,
2239 create_options
, &file_existed
);
2241 if (NT_STATUS_IS_OK(status
)) {
2242 /* We might be going to allow this open. Check oplock
2244 /* Second pass - send break for both batch or
2245 * exclusive oplocks. */
2246 if ((req
!= NULL
) &&
2247 delay_for_exclusive_oplocks(
2252 schedule_defer_open(lck
, request_time
, req
);
2255 return NT_STATUS_SHARING_VIOLATION
;
2259 if (!NT_STATUS_IS_OK(status
)) {
2260 struct deferred_open_record state
;
2262 state
.delayed_for_oplocks
= False
;
2265 /* Do it all over again immediately. In the second
2266 * round we will find that the file existed and handle
2267 * the DELETE_PENDING and FCB cases correctly. No need
2268 * to duplicate the code here. Essentially this is a
2269 * "goto top of this function", but don't tell
2273 defer_open(lck
, request_time
, timeval_zero(),
2281 grant_fsp_oplock_type(fsp
,
2288 * We exit this block with the share entry *locked*.....
2293 SMB_ASSERT(lck
!= NULL
);
2295 /* Delete streams if create_disposition requires it */
2296 if (file_existed
&& clear_ads
&&
2297 !is_ntfs_stream_smb_fname(smb_fname
)) {
2298 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2299 if (!NT_STATUS_IS_OK(status
)) {
2306 /* note that we ignore failure for the following. It is
2307 basically a hack for NFS, and NFS will never set one of
2308 these only read them. Nobody but Samba can ever set a deny
2309 mode and we have already checked our more authoritative
2310 locking database for permission to set this deny mode. If
2311 the kernel refuses the operations then the kernel is wrong.
2312 note that GPFS supports it as well - jmcd */
2314 if (fsp
->fh
->fd
!= -1) {
2316 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2317 if(ret_flock
== -1 ){
2322 return NT_STATUS_SHARING_VIOLATION
;
2327 * At this point onwards, we can guarentee that the share entry
2328 * is locked, whether we created the file or not, and that the
2329 * deny mode is compatible with all current opens.
2333 * If requested, truncate the file.
2336 if (file_existed
&& (flags2
&O_TRUNC
)) {
2338 * We are modifing the file after open - update the stat
2341 if ((SMB_VFS_FTRUNCATE(fsp
, 0) == -1) ||
2342 (SMB_VFS_FSTAT(fsp
, &smb_fname
->st
)==-1)) {
2343 status
= map_nt_error_from_unix(errno
);
2351 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2352 * but we don't have to store this - just ignore it on access check.
2354 if (conn
->sconn
->using_smb2
) {
2356 * SMB2 doesn't return it (according to Microsoft tests).
2357 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2358 * File created with access = 0x7 (Read, Write, Delete)
2359 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2361 fsp
->access_mask
= access_mask
;
2363 /* But SMB1 does. */
2364 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2368 /* stat opens on existing files don't get oplocks. */
2369 if (is_stat_open(open_access_mask
)) {
2370 fsp
->oplock_type
= NO_OPLOCK
;
2373 if (!(flags2
& O_TRUNC
)) {
2374 info
= FILE_WAS_OPENED
;
2376 info
= FILE_WAS_OVERWRITTEN
;
2379 info
= FILE_WAS_CREATED
;
2387 * Setup the oplock info in both the shared memory and
2391 if (!set_file_oplock(fsp
, fsp
->oplock_type
)) {
2393 * Could not get the kernel oplock or there are byte-range
2394 * locks on the file.
2396 fsp
->oplock_type
= NO_OPLOCK
;
2399 if (info
== FILE_WAS_OVERWRITTEN
|| info
== FILE_WAS_CREATED
|| info
== FILE_WAS_SUPERSEDED
) {
2400 new_file_created
= True
;
2403 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2407 /* Handle strange delete on close create semantics. */
2408 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2410 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2412 if (!NT_STATUS_IS_OK(status
)) {
2413 /* Remember to delete the mode we just added. */
2414 del_share_mode(lck
, fsp
);
2419 /* Note that here we set the *inital* delete on close flag,
2420 not the regular one. The magic gets handled in close. */
2421 fsp
->initial_delete_on_close
= True
;
2424 if (new_file_created
) {
2425 /* Files should be initially set as archive */
2426 if (lp_map_archive(SNUM(conn
)) ||
2427 lp_store_dos_attributes(SNUM(conn
))) {
2429 if (file_set_dosmode(conn
, smb_fname
,
2430 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2431 parent_dir
, true) == 0) {
2432 unx_mode
= smb_fname
->st
.st_ex_mode
;
2438 /* Determine sparse flag. */
2440 /* POSIX opens are sparse by default. */
2441 fsp
->is_sparse
= true;
2443 fsp
->is_sparse
= (file_existed
&&
2444 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2448 * Take care of inherited ACLs on created files - if default ACL not
2452 if (!posix_open
&& !file_existed
&& !def_acl
) {
2454 int saved_errno
= errno
; /* We might get ENOSYS in the next
2457 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2459 errno
= saved_errno
; /* Ignore ENOSYS */
2462 } else if (new_unx_mode
) {
2466 /* Attributes need changing. File already existed. */
2469 int saved_errno
= errno
; /* We might get ENOSYS in the
2471 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2473 if (ret
== -1 && errno
== ENOSYS
) {
2474 errno
= saved_errno
; /* Ignore ENOSYS */
2476 DEBUG(5, ("open_file_ntcreate: reset "
2477 "attributes of file %s to 0%o\n",
2478 smb_fname_str_dbg(smb_fname
),
2479 (unsigned int)new_unx_mode
));
2480 ret
= 0; /* Don't do the fchmod below. */
2485 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2486 DEBUG(5, ("open_file_ntcreate: failed to reset "
2487 "attributes of file %s to 0%o\n",
2488 smb_fname_str_dbg(smb_fname
),
2489 (unsigned int)new_unx_mode
));
2492 /* If this is a successful open, we must remove any deferred open
2495 del_deferred_open_entry(lck
, req
->mid
,
2496 sconn_server_id(req
->sconn
));
2500 return NT_STATUS_OK
;
2504 /****************************************************************************
2505 Open a file for for write to ensure that we can fchmod it.
2506 ****************************************************************************/
2508 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2509 struct smb_filename
*smb_fname
,
2510 files_struct
**result
)
2512 if (!VALID_STAT(smb_fname
->st
)) {
2513 return NT_STATUS_INVALID_PARAMETER
;
2516 return SMB_VFS_CREATE_FILE(
2519 0, /* root_dir_fid */
2520 smb_fname
, /* fname */
2521 FILE_WRITE_DATA
, /* access_mask */
2522 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2524 FILE_OPEN
, /* create_disposition*/
2525 0, /* create_options */
2526 0, /* file_attributes */
2527 INTERNAL_OPEN_ONLY
, /* oplock_request */
2528 0, /* allocation_size */
2529 0, /* private_flags */
2532 result
, /* result */
2536 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2537 struct smb_filename
*smb_dname
,
2538 uint32 file_attributes
)
2541 char *parent_dir
= NULL
;
2543 bool posix_open
= false;
2544 bool need_re_stat
= false;
2545 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2547 if(access_mask
& ~(conn
->share_access
)) {
2548 DEBUG(5,("mkdir_internal: failing share access "
2549 "%s\n", lp_servicename(SNUM(conn
))));
2550 return NT_STATUS_ACCESS_DENIED
;
2553 status
= check_name(conn
, smb_dname
->base_name
);
2554 if (!NT_STATUS_IS_OK(status
)) {
2558 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2560 return NT_STATUS_NO_MEMORY
;
2563 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2565 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2567 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2570 status
= check_parent_access(conn
,
2574 if(!NT_STATUS_IS_OK(status
)) {
2575 DEBUG(5,("mkdir_internal: check_parent_access "
2576 "on directory %s for path %s returned %s\n",
2578 smb_dname
->base_name
,
2579 nt_errstr(status
) ));
2583 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2584 return map_nt_error_from_unix(errno
);
2587 /* Ensure we're checking for a symlink here.... */
2588 /* We don't want to get caught by a symlink racer. */
2590 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2591 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2592 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2593 return map_nt_error_from_unix(errno
);
2596 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2597 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2598 smb_fname_str_dbg(smb_dname
)));
2599 return NT_STATUS_NOT_A_DIRECTORY
;
2602 if (lp_store_dos_attributes(SNUM(conn
))) {
2604 file_set_dosmode(conn
, smb_dname
,
2605 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2610 if (lp_inherit_perms(SNUM(conn
))) {
2611 inherit_access_posix_acl(conn
, parent_dir
,
2612 smb_dname
->base_name
, mode
);
2613 need_re_stat
= true;
2618 * Check if high bits should have been set,
2619 * then (if bits are missing): add them.
2620 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2623 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2624 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2625 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2626 (smb_dname
->st
.st_ex_mode
|
2627 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2628 need_re_stat
= true;
2632 /* Change the owner if required. */
2633 if (lp_inherit_owner(SNUM(conn
))) {
2634 change_dir_owner_to_parent(conn
, parent_dir
,
2635 smb_dname
->base_name
,
2637 need_re_stat
= true;
2641 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2642 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2643 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2644 return map_nt_error_from_unix(errno
);
2648 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2649 smb_dname
->base_name
);
2651 return NT_STATUS_OK
;
2654 /****************************************************************************
2655 Ensure we didn't get symlink raced on opening a directory.
2656 ****************************************************************************/
2658 bool check_same_stat(const SMB_STRUCT_STAT
*sbuf1
,
2659 const SMB_STRUCT_STAT
*sbuf2
)
2661 if (sbuf1
->st_ex_uid
!= sbuf2
->st_ex_uid
||
2662 sbuf1
->st_ex_gid
!= sbuf2
->st_ex_gid
||
2663 sbuf1
->st_ex_dev
!= sbuf2
->st_ex_dev
||
2664 sbuf1
->st_ex_ino
!= sbuf2
->st_ex_ino
) {
2670 /****************************************************************************
2671 Open a directory from an NT SMB call.
2672 ****************************************************************************/
2674 static NTSTATUS
open_directory(connection_struct
*conn
,
2675 struct smb_request
*req
,
2676 struct smb_filename
*smb_dname
,
2678 uint32 share_access
,
2679 uint32 create_disposition
,
2680 uint32 create_options
,
2681 uint32 file_attributes
,
2683 files_struct
**result
)
2685 files_struct
*fsp
= NULL
;
2686 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2687 struct share_mode_lock
*lck
= NULL
;
2689 struct timespec mtimespec
;
2692 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
2694 /* Ensure we have a directory attribute. */
2695 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2697 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2698 "share_access = 0x%x create_options = 0x%x, "
2699 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2700 smb_fname_str_dbg(smb_dname
),
2701 (unsigned int)access_mask
,
2702 (unsigned int)share_access
,
2703 (unsigned int)create_options
,
2704 (unsigned int)create_disposition
,
2705 (unsigned int)file_attributes
));
2707 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
2708 (conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
2709 is_ntfs_stream_smb_fname(smb_dname
)) {
2710 DEBUG(2, ("open_directory: %s is a stream name!\n",
2711 smb_fname_str_dbg(smb_dname
)));
2712 return NT_STATUS_NOT_A_DIRECTORY
;
2715 status
= smbd_calculate_access_mask(conn
, smb_dname
, dir_existed
,
2716 access_mask
, &access_mask
);
2717 if (!NT_STATUS_IS_OK(status
)) {
2718 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2719 "on file %s returned %s\n",
2720 smb_fname_str_dbg(smb_dname
),
2721 nt_errstr(status
)));
2725 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2726 !security_token_has_privilege(get_current_nttok(conn
),
2727 SEC_PRIV_SECURITY
)) {
2728 DEBUG(10, ("open_directory: open on %s "
2729 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2730 smb_fname_str_dbg(smb_dname
)));
2731 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2734 switch( create_disposition
) {
2738 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2741 info
= FILE_WAS_OPENED
;
2746 /* If directory exists error. If directory doesn't
2750 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
2751 DEBUG(2, ("open_directory: unable to create "
2752 "%s. Error was %s\n",
2753 smb_fname_str_dbg(smb_dname
),
2754 nt_errstr(status
)));
2758 status
= mkdir_internal(conn
, smb_dname
,
2761 if (!NT_STATUS_IS_OK(status
)) {
2762 DEBUG(2, ("open_directory: unable to create "
2763 "%s. Error was %s\n",
2764 smb_fname_str_dbg(smb_dname
),
2765 nt_errstr(status
)));
2769 info
= FILE_WAS_CREATED
;
2774 * If directory exists open. If directory doesn't
2779 status
= NT_STATUS_OK
;
2780 info
= FILE_WAS_OPENED
;
2782 status
= mkdir_internal(conn
, smb_dname
,
2785 if (NT_STATUS_IS_OK(status
)) {
2786 info
= FILE_WAS_CREATED
;
2788 /* Cope with create race. */
2789 if (!NT_STATUS_EQUAL(status
,
2790 NT_STATUS_OBJECT_NAME_COLLISION
)) {
2791 DEBUG(2, ("open_directory: unable to create "
2792 "%s. Error was %s\n",
2793 smb_fname_str_dbg(smb_dname
),
2794 nt_errstr(status
)));
2797 info
= FILE_WAS_OPENED
;
2803 case FILE_SUPERSEDE
:
2804 case FILE_OVERWRITE
:
2805 case FILE_OVERWRITE_IF
:
2807 DEBUG(5,("open_directory: invalid create_disposition "
2808 "0x%x for directory %s\n",
2809 (unsigned int)create_disposition
,
2810 smb_fname_str_dbg(smb_dname
)));
2811 return NT_STATUS_INVALID_PARAMETER
;
2814 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2815 DEBUG(5,("open_directory: %s is not a directory !\n",
2816 smb_fname_str_dbg(smb_dname
)));
2817 return NT_STATUS_NOT_A_DIRECTORY
;
2820 if (info
== FILE_WAS_OPENED
) {
2821 status
= smbd_check_access_rights(conn
, smb_dname
, access_mask
);
2822 if (!NT_STATUS_IS_OK(status
)) {
2823 DEBUG(10, ("open_directory: smbd_check_access_rights on "
2824 "file %s failed with %s\n",
2825 smb_fname_str_dbg(smb_dname
),
2826 nt_errstr(status
)));
2831 status
= file_new(req
, conn
, &fsp
);
2832 if(!NT_STATUS_IS_OK(status
)) {
2837 * Setup the files_struct for it.
2840 fsp
->mode
= smb_dname
->st
.st_ex_mode
;
2841 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
2842 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
2843 fsp
->file_pid
= req
? req
->smbpid
: 0;
2844 fsp
->can_lock
= False
;
2845 fsp
->can_read
= False
;
2846 fsp
->can_write
= False
;
2848 fsp
->share_access
= share_access
;
2849 fsp
->fh
->private_options
= 0;
2851 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2853 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2854 fsp
->print_file
= NULL
;
2855 fsp
->modified
= False
;
2856 fsp
->oplock_type
= NO_OPLOCK
;
2857 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
2858 fsp
->is_directory
= True
;
2859 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
2860 status
= fsp_set_smb_fname(fsp
, smb_dname
);
2861 if (!NT_STATUS_IS_OK(status
)) {
2862 file_free(req
, fsp
);
2866 mtimespec
= smb_dname
->st
.st_ex_mtime
;
2869 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
2871 /* POSIX allows us to open a directory with O_RDONLY. */
2872 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
2874 if (!NT_STATUS_IS_OK(status
)) {
2875 DEBUG(5, ("open_directory: Could not open fd for "
2877 smb_fname_str_dbg(smb_dname
),
2878 nt_errstr(status
)));
2879 file_free(req
, fsp
);
2883 status
= vfs_stat_fsp(fsp
);
2884 if (!NT_STATUS_IS_OK(status
)) {
2886 file_free(req
, fsp
);
2890 /* Ensure there was no race condition. */
2891 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
2892 DEBUG(5,("open_directory: stat struct differs for "
2894 smb_fname_str_dbg(smb_dname
)));
2896 file_free(req
, fsp
);
2897 return NT_STATUS_ACCESS_DENIED
;
2900 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
2901 conn
->connectpath
, smb_dname
, &mtimespec
);
2904 DEBUG(0, ("open_directory: Could not get share mode lock for "
2905 "%s\n", smb_fname_str_dbg(smb_dname
)));
2907 file_free(req
, fsp
);
2908 return NT_STATUS_SHARING_VIOLATION
;
2911 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2912 access_mask
, share_access
,
2913 create_options
, &dir_existed
);
2915 if (!NT_STATUS_IS_OK(status
)) {
2918 file_free(req
, fsp
);
2922 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2923 req
? req
->mid
: 0, NO_OPLOCK
);
2925 /* For directories the delete on close bit at open time seems
2926 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2927 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2928 status
= can_set_delete_on_close(fsp
, 0);
2929 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
2932 file_free(req
, fsp
);
2936 if (NT_STATUS_IS_OK(status
)) {
2937 /* Note that here we set the *inital* delete on close flag,
2938 not the regular one. The magic gets handled in close. */
2939 fsp
->initial_delete_on_close
= True
;
2950 return NT_STATUS_OK
;
2953 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
2954 struct smb_filename
*smb_dname
)
2959 status
= SMB_VFS_CREATE_FILE(
2962 0, /* root_dir_fid */
2963 smb_dname
, /* fname */
2964 FILE_READ_ATTRIBUTES
, /* access_mask */
2965 FILE_SHARE_NONE
, /* share_access */
2966 FILE_CREATE
, /* create_disposition*/
2967 FILE_DIRECTORY_FILE
, /* create_options */
2968 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
2969 0, /* oplock_request */
2970 0, /* allocation_size */
2971 0, /* private_flags */
2977 if (NT_STATUS_IS_OK(status
)) {
2978 close_file(req
, fsp
, NORMAL_CLOSE
);
2984 /****************************************************************************
2985 Receive notification that one of our open files has been renamed by another
2987 ****************************************************************************/
2989 void msg_file_was_renamed(struct messaging_context
*msg
,
2992 struct server_id server_id
,
2996 char *frm
= (char *)data
->data
;
2998 const char *sharepath
;
2999 const char *base_name
;
3000 const char *stream_name
;
3001 struct smb_filename
*smb_fname
= NULL
;
3002 size_t sp_len
, bn_len
;
3004 struct smbd_server_connection
*sconn
=
3005 talloc_get_type_abort(private_data
,
3006 struct smbd_server_connection
);
3008 if (data
->data
== NULL
3009 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3010 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3011 (int)data
->length
));
3015 /* Unpack the message. */
3016 pull_file_id_24(frm
, &id
);
3017 sharepath
= &frm
[24];
3018 sp_len
= strlen(sharepath
);
3019 base_name
= sharepath
+ sp_len
+ 1;
3020 bn_len
= strlen(base_name
);
3021 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3023 /* stream_name must always be NULL if there is no stream. */
3024 if (stream_name
[0] == '\0') {
3028 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
3029 stream_name
, NULL
, &smb_fname
);
3030 if (!NT_STATUS_IS_OK(status
)) {
3034 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3036 sharepath
, smb_fname_str_dbg(smb_fname
),
3037 file_id_string_tos(&id
)));
3039 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3040 fsp
= file_find_di_next(fsp
)) {
3041 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3043 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
3044 fsp
->fnum
, fsp_str_dbg(fsp
),
3045 smb_fname_str_dbg(smb_fname
)));
3046 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3047 if (!NT_STATUS_IS_OK(status
)) {
3052 /* Now we have the complete path we can work out if this is
3053 actually within this share and adjust newname accordingly. */
3054 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3055 "not sharepath %s) "
3056 "fnum %d from %s -> %s\n",
3057 fsp
->conn
->connectpath
,
3061 smb_fname_str_dbg(smb_fname
)));
3065 TALLOC_FREE(smb_fname
);
3070 * If a main file is opened for delete, all streams need to be checked for
3071 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3072 * If that works, delete them all by setting the delete on close and close.
3075 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3078 struct stream_struct
*stream_info
= NULL
;
3079 files_struct
**streams
= NULL
;
3081 unsigned int num_streams
= 0;
3082 TALLOC_CTX
*frame
= talloc_stackframe();
3085 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3086 &num_streams
, &stream_info
);
3088 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3089 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3090 DEBUG(10, ("no streams around\n"));
3092 return NT_STATUS_OK
;
3095 if (!NT_STATUS_IS_OK(status
)) {
3096 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3097 nt_errstr(status
)));
3101 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3104 if (num_streams
== 0) {
3106 return NT_STATUS_OK
;
3109 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3110 if (streams
== NULL
) {
3111 DEBUG(0, ("talloc failed\n"));
3112 status
= NT_STATUS_NO_MEMORY
;
3116 for (i
=0; i
<num_streams
; i
++) {
3117 struct smb_filename
*smb_fname
= NULL
;
3119 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3124 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3125 stream_info
[i
].name
,
3127 if (!NT_STATUS_IS_OK(status
)) {
3131 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3132 DEBUG(10, ("Unable to stat stream: %s\n",
3133 smb_fname_str_dbg(smb_fname
)));
3136 status
= SMB_VFS_CREATE_FILE(
3139 0, /* root_dir_fid */
3140 smb_fname
, /* fname */
3141 DELETE_ACCESS
, /* access_mask */
3142 (FILE_SHARE_READ
| /* share_access */
3143 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3144 FILE_OPEN
, /* create_disposition*/
3145 0, /* create_options */
3146 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3147 0, /* oplock_request */
3148 0, /* allocation_size */
3149 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3152 &streams
[i
], /* result */
3155 if (!NT_STATUS_IS_OK(status
)) {
3156 DEBUG(10, ("Could not open stream %s: %s\n",
3157 smb_fname_str_dbg(smb_fname
),
3158 nt_errstr(status
)));
3160 TALLOC_FREE(smb_fname
);
3163 TALLOC_FREE(smb_fname
);
3167 * don't touch the variable "status" beyond this point :-)
3170 for (i
-= 1 ; i
>= 0; i
--) {
3171 if (streams
[i
] == NULL
) {
3175 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3176 fsp_str_dbg(streams
[i
])));
3177 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3185 /*********************************************************************
3186 Create a default ACL by inheriting from the parent. If no inheritance
3187 from the parent available, don't set anything. This will leave the actual
3188 permissions the new file or directory already got from the filesystem
3189 as the NT ACL when read.
3190 *********************************************************************/
3192 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
3194 TALLOC_CTX
*ctx
= talloc_tos();
3195 char *parent_name
= NULL
;
3196 struct security_descriptor
*parent_desc
= NULL
;
3197 NTSTATUS status
= NT_STATUS_OK
;
3198 struct security_descriptor
*psd
= NULL
;
3199 struct dom_sid
*owner_sid
= NULL
;
3200 struct dom_sid
*group_sid
= NULL
;
3201 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
3202 bool inherit_owner
= lp_inherit_owner(SNUM(fsp
->conn
));
3203 bool inheritable_components
= false;
3206 if (!parent_dirname(ctx
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
3207 return NT_STATUS_NO_MEMORY
;
3210 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
3212 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
3214 if (!NT_STATUS_IS_OK(status
)) {
3218 inheritable_components
= sd_has_inheritable_components(parent_desc
,
3221 if (!inheritable_components
&& !inherit_owner
) {
3222 /* Nothing to inherit and not setting owner. */
3223 return NT_STATUS_OK
;
3226 /* Create an inherited descriptor from the parent. */
3228 if (DEBUGLEVEL
>= 10) {
3229 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3230 fsp_str_dbg(fsp
) ));
3231 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
3234 /* Inherit from parent descriptor if "inherit owner" set. */
3235 if (inherit_owner
) {
3236 owner_sid
= parent_desc
->owner_sid
;
3237 group_sid
= parent_desc
->group_sid
;
3240 if (owner_sid
== NULL
) {
3241 owner_sid
= &fsp
->conn
->session_info
->security_token
->sids
[PRIMARY_USER_SID_INDEX
];
3243 if (group_sid
== NULL
) {
3244 group_sid
= &fsp
->conn
->session_info
->security_token
->sids
[PRIMARY_GROUP_SID_INDEX
];
3247 status
= se_create_child_secdesc(ctx
,
3254 if (!NT_STATUS_IS_OK(status
)) {
3258 /* If inheritable_components == false,
3259 se_create_child_secdesc()
3260 creates a security desriptor with a NULL dacl
3261 entry, but with SEC_DESC_DACL_PRESENT. We need
3262 to remove that flag. */
3264 if (!inheritable_components
) {
3265 security_info_sent
&= ~SECINFO_DACL
;
3266 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
3269 if (DEBUGLEVEL
>= 10) {
3270 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3271 fsp_str_dbg(fsp
) ));
3272 NDR_PRINT_DEBUG(security_descriptor
, psd
);
3275 if (inherit_owner
) {
3276 /* We need to be root to force this. */
3279 status
= SMB_VFS_FSET_NT_ACL(fsp
,
3282 if (inherit_owner
) {
3289 * Wrapper around open_file_ntcreate and open_directory
3292 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3293 struct smb_request
*req
,
3294 struct smb_filename
*smb_fname
,
3295 uint32_t access_mask
,
3296 uint32_t share_access
,
3297 uint32_t create_disposition
,
3298 uint32_t create_options
,
3299 uint32_t file_attributes
,
3300 uint32_t oplock_request
,
3301 uint64_t allocation_size
,
3302 uint32_t private_flags
,
3303 struct security_descriptor
*sd
,
3304 struct ea_list
*ea_list
,
3306 files_struct
**result
,
3309 int info
= FILE_WAS_OPENED
;
3310 files_struct
*base_fsp
= NULL
;
3311 files_struct
*fsp
= NULL
;
3314 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3315 "file_attributes = 0x%x, share_access = 0x%x, "
3316 "create_disposition = 0x%x create_options = 0x%x "
3317 "oplock_request = 0x%x private_flags = 0x%x "
3318 "ea_list = 0x%p, sd = 0x%p, "
3320 (unsigned int)access_mask
,
3321 (unsigned int)file_attributes
,
3322 (unsigned int)share_access
,
3323 (unsigned int)create_disposition
,
3324 (unsigned int)create_options
,
3325 (unsigned int)oplock_request
,
3326 (unsigned int)private_flags
,
3327 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3329 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3330 status
= NT_STATUS_NOT_SUPPORTED
;
3334 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3335 status
= NT_STATUS_INVALID_PARAMETER
;
3340 oplock_request
|= INTERNAL_OPEN_ONLY
;
3343 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3344 && (access_mask
& DELETE_ACCESS
)
3345 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3347 * We can't open a file with DELETE access if any of the
3348 * streams is open without FILE_SHARE_DELETE
3350 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3352 if (!NT_STATUS_IS_OK(status
)) {
3357 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3358 !security_token_has_privilege(get_current_nttok(conn
),
3359 SEC_PRIV_SECURITY
)) {
3360 DEBUG(10, ("create_file_unixpath: open on %s "
3361 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3362 smb_fname_str_dbg(smb_fname
)));
3363 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3367 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3368 && is_ntfs_stream_smb_fname(smb_fname
)
3369 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3370 uint32 base_create_disposition
;
3371 struct smb_filename
*smb_fname_base
= NULL
;
3373 if (create_options
& FILE_DIRECTORY_FILE
) {
3374 status
= NT_STATUS_NOT_A_DIRECTORY
;
3378 switch (create_disposition
) {
3380 base_create_disposition
= FILE_OPEN
;
3383 base_create_disposition
= FILE_OPEN_IF
;
3387 /* Create an smb_filename with stream_name == NULL. */
3388 status
= create_synthetic_smb_fname(talloc_tos(),
3389 smb_fname
->base_name
,
3392 if (!NT_STATUS_IS_OK(status
)) {
3396 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3397 DEBUG(10, ("Unable to stat stream: %s\n",
3398 smb_fname_str_dbg(smb_fname_base
)));
3401 /* Open the base file. */
3402 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3405 | FILE_SHARE_DELETE
,
3406 base_create_disposition
,
3407 0, 0, 0, 0, 0, NULL
, NULL
,
3409 TALLOC_FREE(smb_fname_base
);
3411 if (!NT_STATUS_IS_OK(status
)) {
3412 DEBUG(10, ("create_file_unixpath for base %s failed: "
3413 "%s\n", smb_fname
->base_name
,
3414 nt_errstr(status
)));
3417 /* we don't need to low level fd */
3422 * If it's a request for a directory open, deal with it separately.
3425 if (create_options
& FILE_DIRECTORY_FILE
) {
3427 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3428 status
= NT_STATUS_INVALID_PARAMETER
;
3432 /* Can't open a temp directory. IFS kit test. */
3433 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3434 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3435 status
= NT_STATUS_INVALID_PARAMETER
;
3440 * We will get a create directory here if the Win32
3441 * app specified a security descriptor in the
3442 * CreateDirectory() call.
3446 status
= open_directory(
3447 conn
, req
, smb_fname
, access_mask
, share_access
,
3448 create_disposition
, create_options
, file_attributes
,
3453 * Ordinary file case.
3456 status
= file_new(req
, conn
, &fsp
);
3457 if(!NT_STATUS_IS_OK(status
)) {
3461 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3462 if (!NT_STATUS_IS_OK(status
)) {
3467 * We're opening the stream element of a base_fsp
3468 * we already opened. Set up the base_fsp pointer.
3471 fsp
->base_fsp
= base_fsp
;
3474 status
= open_file_ntcreate(conn
,
3486 if(!NT_STATUS_IS_OK(status
)) {
3487 file_free(req
, fsp
);
3491 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3493 /* A stream open never opens a directory */
3496 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3501 * Fail the open if it was explicitly a non-directory
3505 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3506 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3511 status
= open_directory(
3512 conn
, req
, smb_fname
, access_mask
,
3513 share_access
, create_disposition
,
3514 create_options
, file_attributes
,
3519 if (!NT_STATUS_IS_OK(status
)) {
3523 fsp
->base_fsp
= base_fsp
;
3525 if ((ea_list
!= NULL
) &&
3526 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3527 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3528 if (!NT_STATUS_IS_OK(status
)) {
3533 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3534 status
= NT_STATUS_ACCESS_DENIED
;
3538 /* Save the requested allocation size. */
3539 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3541 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3542 fsp
->initial_allocation_size
= smb_roundup(
3543 fsp
->conn
, allocation_size
);
3544 if (fsp
->is_directory
) {
3545 /* Can't set allocation size on a directory. */
3546 status
= NT_STATUS_ACCESS_DENIED
;
3549 if (vfs_allocate_file_space(
3550 fsp
, fsp
->initial_allocation_size
) == -1) {
3551 status
= NT_STATUS_DISK_FULL
;
3555 fsp
->initial_allocation_size
= smb_roundup(
3556 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3560 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
3561 fsp
->base_fsp
== NULL
) {
3564 * According to the MS documentation, the only time the security
3565 * descriptor is applied to the opened file is iff we *created* the
3566 * file; an existing file stays the same.
3568 * Also, it seems (from observation) that you can open the file with
3569 * any access mask but you can still write the sd. We need to override
3570 * the granted access before we call set_sd
3571 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3574 uint32_t sec_info_sent
;
3575 uint32_t saved_access_mask
= fsp
->access_mask
;
3577 sec_info_sent
= get_sec_info(sd
);
3579 fsp
->access_mask
= FILE_GENERIC_ALL
;
3581 /* Convert all the generic bits. */
3582 security_acl_map_generic(sd
->dacl
, &file_generic_mapping
);
3583 security_acl_map_generic(sd
->sacl
, &file_generic_mapping
);
3585 if (sec_info_sent
& (SECINFO_OWNER
|
3589 status
= SMB_VFS_FSET_NT_ACL(fsp
, sec_info_sent
, sd
);
3592 fsp
->access_mask
= saved_access_mask
;
3594 if (!NT_STATUS_IS_OK(status
)) {
3597 } else if (lp_inherit_acls(SNUM(conn
))) {
3598 /* Inherit from parent. Errors here are not fatal. */
3599 status
= inherit_new_acl(fsp
);
3600 if (!NT_STATUS_IS_OK(status
)) {
3601 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3603 nt_errstr(status
) ));
3608 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3611 if (pinfo
!= NULL
) {
3615 smb_fname
->st
= fsp
->fsp_name
->st
;
3617 return NT_STATUS_OK
;
3620 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3623 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3625 * The close_file below will close
3630 close_file(req
, fsp
, ERROR_CLOSE
);
3633 if (base_fsp
!= NULL
) {
3634 close_file(req
, base_fsp
, ERROR_CLOSE
);
3641 * Calculate the full path name given a relative fid.
3643 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
3644 struct smb_request
*req
,
3645 uint16_t root_dir_fid
,
3646 const struct smb_filename
*smb_fname
,
3647 struct smb_filename
**smb_fname_out
)
3649 files_struct
*dir_fsp
;
3650 char *parent_fname
= NULL
;
3651 char *new_base_name
= NULL
;
3654 if (root_dir_fid
== 0 || !smb_fname
) {
3655 status
= NT_STATUS_INTERNAL_ERROR
;
3659 dir_fsp
= file_fsp(req
, root_dir_fid
);
3661 if (dir_fsp
== NULL
) {
3662 status
= NT_STATUS_INVALID_HANDLE
;
3666 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
3667 status
= NT_STATUS_INVALID_HANDLE
;
3671 if (!dir_fsp
->is_directory
) {
3674 * Check to see if this is a mac fork of some kind.
3677 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
3678 is_ntfs_stream_smb_fname(smb_fname
)) {
3679 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3684 we need to handle the case when we get a
3685 relative open relative to a file and the
3686 pathname is blank - this is a reopen!
3687 (hint from demyn plantenberg)
3690 status
= NT_STATUS_INVALID_HANDLE
;
3694 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
3696 * We're at the toplevel dir, the final file name
3697 * must not contain ./, as this is filtered out
3698 * normally by srvstr_get_path and unix_convert
3699 * explicitly rejects paths containing ./.
3701 parent_fname
= talloc_strdup(talloc_tos(), "");
3702 if (parent_fname
== NULL
) {
3703 status
= NT_STATUS_NO_MEMORY
;
3707 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
3710 * Copy in the base directory name.
3713 parent_fname
= talloc_array(talloc_tos(), char,
3715 if (parent_fname
== NULL
) {
3716 status
= NT_STATUS_NO_MEMORY
;
3719 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
3723 * Ensure it ends in a '/'.
3724 * We used TALLOC_SIZE +2 to add space for the '/'.
3728 && (parent_fname
[dir_name_len
-1] != '\\')
3729 && (parent_fname
[dir_name_len
-1] != '/')) {
3730 parent_fname
[dir_name_len
] = '/';
3731 parent_fname
[dir_name_len
+1] = '\0';
3735 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
3736 smb_fname
->base_name
);
3737 if (new_base_name
== NULL
) {
3738 status
= NT_STATUS_NO_MEMORY
;
3742 status
= filename_convert(req
,
3744 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
3749 if (!NT_STATUS_IS_OK(status
)) {
3754 TALLOC_FREE(parent_fname
);
3755 TALLOC_FREE(new_base_name
);
3759 NTSTATUS
create_file_default(connection_struct
*conn
,
3760 struct smb_request
*req
,
3761 uint16_t root_dir_fid
,
3762 struct smb_filename
*smb_fname
,
3763 uint32_t access_mask
,
3764 uint32_t share_access
,
3765 uint32_t create_disposition
,
3766 uint32_t create_options
,
3767 uint32_t file_attributes
,
3768 uint32_t oplock_request
,
3769 uint64_t allocation_size
,
3770 uint32_t private_flags
,
3771 struct security_descriptor
*sd
,
3772 struct ea_list
*ea_list
,
3773 files_struct
**result
,
3776 int info
= FILE_WAS_OPENED
;
3777 files_struct
*fsp
= NULL
;
3779 bool stream_name
= false;
3781 DEBUG(10,("create_file: access_mask = 0x%x "
3782 "file_attributes = 0x%x, share_access = 0x%x, "
3783 "create_disposition = 0x%x create_options = 0x%x "
3784 "oplock_request = 0x%x "
3785 "private_flags = 0x%x "
3786 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3788 (unsigned int)access_mask
,
3789 (unsigned int)file_attributes
,
3790 (unsigned int)share_access
,
3791 (unsigned int)create_disposition
,
3792 (unsigned int)create_options
,
3793 (unsigned int)oplock_request
,
3794 (unsigned int)private_flags
,
3795 (unsigned int)root_dir_fid
,
3796 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3799 * Calculate the filename from the root_dir_if if necessary.
3802 if (root_dir_fid
!= 0) {
3803 struct smb_filename
*smb_fname_out
= NULL
;
3804 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
3805 smb_fname
, &smb_fname_out
);
3806 if (!NT_STATUS_IS_OK(status
)) {
3809 smb_fname
= smb_fname_out
;
3813 * Check to see if this is a mac fork of some kind.
3816 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
3818 enum FAKE_FILE_TYPE fake_file_type
;
3820 fake_file_type
= is_fake_file(smb_fname
);
3822 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
3825 * Here we go! support for changing the disk quotas
3828 * We need to fake up to open this MAGIC QUOTA file
3829 * and return a valid FID.
3831 * w2k close this file directly after openening xp
3832 * also tries a QUERY_FILE_INFO on the file and then
3835 status
= open_fake_file(req
, conn
, req
->vuid
,
3836 fake_file_type
, smb_fname
,
3838 if (!NT_STATUS_IS_OK(status
)) {
3842 ZERO_STRUCT(smb_fname
->st
);
3846 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
3847 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3852 if (stream_name
&& is_ntfs_default_stream_smb_fname(smb_fname
)) {
3854 smb_fname
->stream_name
= NULL
;
3855 /* We have to handle this error here. */
3856 if (create_options
& FILE_DIRECTORY_FILE
) {
3857 status
= NT_STATUS_NOT_A_DIRECTORY
;
3860 if (lp_posix_pathnames()) {
3861 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
3863 ret
= SMB_VFS_STAT(conn
, smb_fname
);
3866 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
3867 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3872 status
= create_file_unixpath(
3873 conn
, req
, smb_fname
, access_mask
, share_access
,
3874 create_disposition
, create_options
, file_attributes
,
3875 oplock_request
, allocation_size
, private_flags
,
3879 if (!NT_STATUS_IS_OK(status
)) {
3884 DEBUG(10, ("create_file: info=%d\n", info
));
3887 if (pinfo
!= NULL
) {
3890 return NT_STATUS_OK
;
3893 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
3896 close_file(req
, fsp
, ERROR_CLOSE
);