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"
31 #include "../librpc/gen_ndr/idmap.h"
32 #include "passdb/lookup_sid.h"
37 extern const struct generic_mapping file_generic_mapping
;
39 struct deferred_open_record
{
40 bool delayed_for_oplocks
;
45 /****************************************************************************
46 If the requester wanted DELETE_ACCESS and was rejected because
47 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
49 ****************************************************************************/
51 static bool parent_override_delete(connection_struct
*conn
,
52 const struct smb_filename
*smb_fname
,
54 uint32_t rejected_mask
)
56 if ((access_mask
& DELETE_ACCESS
) &&
57 (rejected_mask
& DELETE_ACCESS
) &&
58 can_delete_file_in_directory(conn
, smb_fname
)) {
64 /****************************************************************************
65 Check if we have open rights.
66 ****************************************************************************/
68 NTSTATUS
smbd_check_access_rights(struct connection_struct
*conn
,
69 const struct smb_filename
*smb_fname
,
72 /* Check if we have rights to open. */
74 struct security_descriptor
*sd
= NULL
;
75 uint32_t rejected_share_access
;
76 uint32_t rejected_mask
= access_mask
;
77 uint32_t do_not_check_mask
= 0;
79 rejected_share_access
= access_mask
& ~(conn
->share_access
);
81 if (rejected_share_access
) {
82 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
84 (unsigned int)access_mask
,
85 smb_fname_str_dbg(smb_fname
),
86 (unsigned int)rejected_share_access
));
87 return NT_STATUS_ACCESS_DENIED
;
90 if (get_current_uid(conn
) == (uid_t
)0) {
91 /* I'm sorry sir, I didn't know you were root... */
92 DEBUG(10,("smbd_check_access_rights: root override "
93 "on %s. Granting 0x%x\n",
94 smb_fname_str_dbg(smb_fname
),
95 (unsigned int)access_mask
));
99 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
100 DEBUG(10,("smbd_check_access_rights: not checking ACL "
101 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
102 smb_fname_str_dbg(smb_fname
),
103 (unsigned int)access_mask
));
107 if (access_mask
== DELETE_ACCESS
&&
108 VALID_STAT(smb_fname
->st
) &&
109 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
110 /* We can always delete a symlink. */
111 DEBUG(10,("smbd_check_access_rights: not checking ACL "
112 "on DELETE_ACCESS on symlink %s.\n",
113 smb_fname_str_dbg(smb_fname
) ));
117 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
120 SECINFO_DACL
), talloc_tos(), &sd
);
122 if (!NT_STATUS_IS_OK(status
)) {
123 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
125 smb_fname_str_dbg(smb_fname
),
128 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
136 * If we can access the path to this file, by
137 * default we have FILE_READ_ATTRIBUTES from the
138 * containing directory. See the section:
139 * "Algorithm to Check Access to an Existing File"
142 * se_file_access_check() also takes care of
143 * owner WRITE_DAC and READ_CONTROL.
145 do_not_check_mask
= FILE_READ_ATTRIBUTES
;
148 * Samba 3.6 and earlier granted execute access even
149 * if the ACL did not contain execute rights.
150 * Samba 4.0 is more correct and checks it.
151 * The compatibilty mode allows to skip this check
152 * to smoothen upgrades.
154 if (lp_acl_allow_execute_always(SNUM(conn
))) {
155 do_not_check_mask
|= FILE_EXECUTE
;
158 status
= se_file_access_check(sd
,
159 get_current_nttok(conn
),
161 (access_mask
& ~do_not_check_mask
),
164 DEBUG(10,("smbd_check_access_rights: file %s requesting "
165 "0x%x returning 0x%x (%s)\n",
166 smb_fname_str_dbg(smb_fname
),
167 (unsigned int)access_mask
,
168 (unsigned int)rejected_mask
,
169 nt_errstr(status
) ));
171 if (!NT_STATUS_IS_OK(status
)) {
172 if (DEBUGLEVEL
>= 10) {
173 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
174 smb_fname_str_dbg(smb_fname
) ));
175 NDR_PRINT_DEBUG(security_descriptor
, sd
);
181 if (NT_STATUS_IS_OK(status
) ||
182 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
186 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
190 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
191 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
192 !lp_store_dos_attributes(SNUM(conn
)) &&
193 (lp_map_readonly(SNUM(conn
)) ||
194 lp_map_archive(SNUM(conn
)) ||
195 lp_map_hidden(SNUM(conn
)) ||
196 lp_map_system(SNUM(conn
)))) {
197 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
199 DEBUG(10,("smbd_check_access_rights: "
201 "FILE_WRITE_ATTRIBUTES "
203 smb_fname_str_dbg(smb_fname
)));
206 if (parent_override_delete(conn
,
210 /* Were we trying to do an open
211 * for delete and didn't get DELETE
212 * access (only) ? Check if the
213 * directory allows DELETE_CHILD.
215 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
218 rejected_mask
&= ~DELETE_ACCESS
;
220 DEBUG(10,("smbd_check_access_rights: "
224 smb_fname_str_dbg(smb_fname
)));
227 if (rejected_mask
!= 0) {
228 return NT_STATUS_ACCESS_DENIED
;
233 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
234 struct smb_filename
*smb_fname
,
235 uint32_t access_mask
)
238 char *parent_dir
= NULL
;
239 struct security_descriptor
*parent_sd
= NULL
;
240 uint32_t access_granted
= 0;
242 if (!parent_dirname(talloc_tos(),
243 smb_fname
->base_name
,
246 return NT_STATUS_NO_MEMORY
;
249 if (get_current_uid(conn
) == (uid_t
)0) {
250 /* I'm sorry sir, I didn't know you were root... */
251 DEBUG(10,("check_parent_access: root override "
252 "on %s. Granting 0x%x\n",
253 smb_fname_str_dbg(smb_fname
),
254 (unsigned int)access_mask
));
258 status
= SMB_VFS_GET_NT_ACL(conn
,
264 if (!NT_STATUS_IS_OK(status
)) {
265 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
266 "%s with error %s\n",
273 * If we can access the path to this file, by
274 * default we have FILE_READ_ATTRIBUTES from the
275 * containing directory. See the section:
276 * "Algorithm to Check Access to an Existing File"
279 * se_file_access_check() also takes care of
280 * owner WRITE_DAC and READ_CONTROL.
282 status
= se_file_access_check(parent_sd
,
283 get_current_nttok(conn
),
285 (access_mask
& ~FILE_READ_ATTRIBUTES
),
287 if(!NT_STATUS_IS_OK(status
)) {
288 DEBUG(5,("check_parent_access: access check "
289 "on directory %s for "
290 "path %s for mask 0x%x returned (0x%x) %s\n",
292 smb_fname
->base_name
,
295 nt_errstr(status
) ));
302 /****************************************************************************
303 Ensure when opening a base file for a stream open that we have permissions
304 to do so given the access mask on the base file.
305 ****************************************************************************/
307 static NTSTATUS
check_base_file_access(struct connection_struct
*conn
,
308 struct smb_filename
*smb_fname
,
309 uint32_t access_mask
)
313 status
= smbd_calculate_access_mask(conn
, smb_fname
,
316 if (!NT_STATUS_IS_OK(status
)) {
317 DEBUG(10, ("smbd_calculate_access_mask "
318 "on file %s returned %s\n",
319 smb_fname_str_dbg(smb_fname
),
324 if (access_mask
& (FILE_WRITE_DATA
|FILE_APPEND_DATA
)) {
326 if (!CAN_WRITE(conn
)) {
327 return NT_STATUS_ACCESS_DENIED
;
329 dosattrs
= dos_mode(conn
, smb_fname
);
330 if (IS_DOS_READONLY(dosattrs
)) {
331 return NT_STATUS_ACCESS_DENIED
;
335 return smbd_check_access_rights(conn
,
340 /****************************************************************************
341 fd support routines - attempt to do a dos_open.
342 ****************************************************************************/
344 NTSTATUS
fd_open(struct connection_struct
*conn
,
349 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
350 NTSTATUS status
= NT_STATUS_OK
;
354 * Never follow symlinks on a POSIX client. The
355 * client should be doing this.
358 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
363 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
364 if (fsp
->fh
->fd
== -1) {
365 int posix_errno
= errno
;
367 #if defined(ENOTSUP) && defined(OSF1)
368 /* handle special Tru64 errno */
369 if (errno
== ENOTSUP
) {
374 /* fix broken NetBSD errno */
375 if (errno
== EFTYPE
) {
379 /* fix broken FreeBSD errno */
380 if (errno
== EMLINK
) {
383 #endif /* O_NOFOLLOW */
384 status
= map_nt_error_from_unix(posix_errno
);
385 if (errno
== EMFILE
) {
386 static time_t last_warned
= 0L;
388 if (time((time_t *) NULL
) > last_warned
) {
389 DEBUG(0,("Too many open files, unable "
390 "to open more! smbd's max "
392 lp_max_open_files()));
393 last_warned
= time((time_t *) NULL
);
399 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
400 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
401 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
406 /****************************************************************************
407 Close the file associated with a fsp.
408 ****************************************************************************/
410 NTSTATUS
fd_close(files_struct
*fsp
)
417 if (fsp
->fh
->fd
== -1) {
418 return NT_STATUS_OK
; /* What we used to call a stat open. */
420 if (fsp
->fh
->ref_count
> 1) {
421 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
424 ret
= SMB_VFS_CLOSE(fsp
);
427 return map_nt_error_from_unix(errno
);
432 /****************************************************************************
433 Change the ownership of a file to that of the parent directory.
434 Do this by fd if possible.
435 ****************************************************************************/
437 void change_file_owner_to_parent(connection_struct
*conn
,
438 const char *inherit_from_dir
,
441 struct smb_filename
*smb_fname_parent
= NULL
;
445 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
446 NULL
, NULL
, &smb_fname_parent
);
447 if (!NT_STATUS_IS_OK(status
)) {
451 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
453 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
454 "directory %s. Error was %s\n",
455 smb_fname_str_dbg(smb_fname_parent
),
457 TALLOC_FREE(smb_fname_parent
);
461 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
462 /* Already this uid - no need to change. */
463 DEBUG(10,("change_file_owner_to_parent: file %s "
464 "is already owned by uid %d\n",
466 (int)fsp
->fsp_name
->st
.st_ex_uid
));
467 TALLOC_FREE(smb_fname_parent
);
472 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
475 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
476 "file %s to parent directory uid %u. Error "
477 "was %s\n", fsp_str_dbg(fsp
),
478 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
481 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
482 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
483 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
484 /* Ensure the uid entry is updated. */
485 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
488 TALLOC_FREE(smb_fname_parent
);
491 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
492 const char *inherit_from_dir
,
494 SMB_STRUCT_STAT
*psbuf
)
496 struct smb_filename
*smb_fname_parent
= NULL
;
497 struct smb_filename
*smb_fname_cwd
= NULL
;
498 char *saved_dir
= NULL
;
499 TALLOC_CTX
*ctx
= talloc_tos();
500 NTSTATUS status
= NT_STATUS_OK
;
503 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
505 if (!NT_STATUS_IS_OK(status
)) {
509 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
511 status
= map_nt_error_from_unix(errno
);
512 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
513 "directory %s. Error was %s\n",
514 smb_fname_str_dbg(smb_fname_parent
),
519 /* We've already done an lstat into psbuf, and we know it's a
520 directory. If we can cd into the directory and the dev/ino
521 are the same then we can safely chown without races as
522 we're locking the directory in place by being in it. This
523 should work on any UNIX (thanks tridge :-). JRA.
526 saved_dir
= vfs_GetWd(ctx
,conn
);
528 status
= map_nt_error_from_unix(errno
);
529 DEBUG(0,("change_dir_owner_to_parent: failed to get "
530 "current working directory. Error was %s\n",
535 /* Chdir into the new path. */
536 if (vfs_ChDir(conn
, fname
) == -1) {
537 status
= map_nt_error_from_unix(errno
);
538 DEBUG(0,("change_dir_owner_to_parent: failed to change "
539 "current working directory to %s. Error "
540 "was %s\n", fname
, strerror(errno
) ));
544 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
546 if (!NT_STATUS_IS_OK(status
)) {
550 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
552 status
= map_nt_error_from_unix(errno
);
553 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
554 "directory '.' (%s) Error was %s\n",
555 fname
, strerror(errno
)));
559 /* Ensure we're pointing at the same place. */
560 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
561 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
562 DEBUG(0,("change_dir_owner_to_parent: "
563 "device/inode on directory %s changed. "
564 "Refusing to chown !\n", fname
));
565 status
= NT_STATUS_ACCESS_DENIED
;
569 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
570 /* Already this uid - no need to change. */
571 DEBUG(10,("change_dir_owner_to_parent: directory %s "
572 "is already owned by uid %d\n",
574 (int)smb_fname_cwd
->st
.st_ex_uid
));
575 status
= NT_STATUS_OK
;
580 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
584 status
= map_nt_error_from_unix(errno
);
585 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
586 "directory %s to parent directory uid %u. "
587 "Error was %s\n", fname
,
588 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
591 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
592 "directory %s to parent directory uid %u.\n",
593 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
594 /* Ensure the uid entry is updated. */
595 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
599 vfs_ChDir(conn
,saved_dir
);
601 TALLOC_FREE(smb_fname_parent
);
602 TALLOC_FREE(smb_fname_cwd
);
606 /****************************************************************************
607 Open a file - returning a guaranteed ATOMIC indication of if the
608 file was created or not.
609 ****************************************************************************/
611 static NTSTATUS
fd_open_atomic(struct connection_struct
*conn
,
617 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
618 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
620 *file_created
= false;
622 if (!(flags
& O_CREAT
)) {
624 * We're not creating the file, just pass through.
626 return fd_open(conn
, fsp
, flags
, mode
);
629 if (flags
& O_EXCL
) {
631 * Fail if already exists, just pass through.
633 status
= fd_open(conn
, fsp
, flags
, mode
);
636 * Here we've opened with O_CREAT|O_EXCL. If that went
637 * NT_STATUS_OK, we *know* we created this file.
639 *file_created
= NT_STATUS_IS_OK(status
);
645 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
646 * To know absolutely if we created the file or not,
647 * we can never call O_CREAT without O_EXCL. So if
648 * we think the file existed, try without O_CREAT|O_EXCL.
649 * If we think the file didn't exist, try with
650 * O_CREAT|O_EXCL. Keep bouncing between these two
651 * requests until either the file is created, or
652 * opened. Either way, we keep going until we get
653 * a returnable result (error, or open/create).
657 int curr_flags
= flags
;
660 /* Just try open, do not create. */
661 curr_flags
&= ~(O_CREAT
);
662 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
663 if (NT_STATUS_EQUAL(status
,
664 NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
666 * Someone deleted it in the meantime.
669 file_existed
= false;
670 DEBUG(10,("fd_open_atomic: file %s existed. "
672 smb_fname_str_dbg(fsp
->fsp_name
)));
676 /* Try create exclusively, fail if it exists. */
677 curr_flags
|= O_EXCL
;
678 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
679 if (NT_STATUS_EQUAL(status
,
680 NT_STATUS_OBJECT_NAME_COLLISION
)) {
682 * Someone created it in the meantime.
683 * Retry without O_CREAT.
686 DEBUG(10,("fd_open_atomic: file %s "
687 "did not exist. Retry.\n",
688 smb_fname_str_dbg(fsp
->fsp_name
)));
691 if (NT_STATUS_IS_OK(status
)) {
693 * Here we've opened with O_CREAT|O_EXCL
694 * and got success. We *know* we created
697 *file_created
= true;
700 /* Create is done, or failed. */
706 /****************************************************************************
708 ****************************************************************************/
710 static NTSTATUS
open_file(files_struct
*fsp
,
711 connection_struct
*conn
,
712 struct smb_request
*req
,
713 const char *parent_dir
,
716 uint32 access_mask
, /* client requested access mask. */
717 uint32 open_access_mask
, /* what we're actually using in the open. */
718 bool *p_file_created
)
720 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
721 NTSTATUS status
= NT_STATUS_OK
;
722 int accmode
= (flags
& O_ACCMODE
);
723 int local_flags
= flags
;
724 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
729 /* Check permissions */
732 * This code was changed after seeing a client open request
733 * containing the open mode of (DENY_WRITE/read-only) with
734 * the 'create if not exist' bit set. The previous code
735 * would fail to open the file read only on a read-only share
736 * as it was checking the flags parameter directly against O_RDONLY,
737 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
741 if (!CAN_WRITE(conn
)) {
742 /* It's a read-only share - fail if we wanted to write. */
743 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
744 DEBUG(3,("Permission denied opening %s\n",
745 smb_fname_str_dbg(smb_fname
)));
746 return NT_STATUS_ACCESS_DENIED
;
747 } else if(flags
& O_CREAT
) {
748 /* We don't want to write - but we must make sure that
749 O_CREAT doesn't create the file if we have write
750 access into the directory.
752 flags
&= ~(O_CREAT
|O_EXCL
);
753 local_flags
&= ~(O_CREAT
|O_EXCL
);
758 * This little piece of insanity is inspired by the
759 * fact that an NT client can open a file for O_RDONLY,
760 * but set the create disposition to FILE_EXISTS_TRUNCATE.
761 * If the client *can* write to the file, then it expects to
762 * truncate the file, even though it is opening for readonly.
763 * Quicken uses this stupid trick in backup file creation...
764 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
765 * for helping track this one down. It didn't bite us in 2.0.x
766 * as we always opened files read-write in that release. JRA.
769 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
770 DEBUG(10,("open_file: truncate requested on read-only open "
771 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
772 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
775 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
776 (!file_existed
&& (local_flags
& O_CREAT
)) ||
777 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
781 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
783 * We would block on opening a FIFO with no one else on the
784 * other end. Do what we used to do and add O_NONBLOCK to the
788 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
789 local_flags
&= ~O_TRUNC
; /* Can't truncate a FIFO. */
790 local_flags
|= O_NONBLOCK
;
794 /* Don't create files with Microsoft wildcard characters. */
797 * wildcard characters are allowed in stream names
798 * only test the basefilename
800 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
802 wild
= smb_fname
->base_name
;
804 if ((local_flags
& O_CREAT
) && !file_existed
&&
806 return NT_STATUS_OBJECT_NAME_INVALID
;
809 /* Can we access this file ? */
810 if (!fsp
->base_fsp
) {
811 /* Only do this check on non-stream open. */
813 status
= smbd_check_access_rights(conn
,
816 } else if (local_flags
& O_CREAT
){
817 status
= check_parent_access(conn
,
821 /* File didn't exist and no O_CREAT. */
822 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
824 if (!NT_STATUS_IS_OK(status
)) {
825 DEBUG(10,("open_file: "
829 "smbd_check_access_rights" :
830 "check_parent_access",
831 smb_fname_str_dbg(smb_fname
),
832 nt_errstr(status
) ));
837 /* Actually do the open */
838 status
= fd_open_atomic(conn
, fsp
, local_flags
,
839 unx_mode
, p_file_created
);
840 if (!NT_STATUS_IS_OK(status
)) {
841 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
842 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
843 nt_errstr(status
),local_flags
,flags
));
847 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
849 /* If we have an fd, this stat should succeed. */
850 DEBUG(0,("Error doing fstat on open file %s "
852 smb_fname_str_dbg(smb_fname
),
854 status
= map_nt_error_from_unix(errno
);
859 if (*p_file_created
) {
860 /* We created this file. */
862 bool need_re_stat
= false;
863 /* Do all inheritance work after we've
864 done a successful fstat call and filled
865 in the stat struct in fsp->fsp_name. */
867 /* Inherit the ACL if required */
868 if (lp_inherit_perms(SNUM(conn
))) {
869 inherit_access_posix_acl(conn
, parent_dir
,
870 smb_fname
->base_name
,
875 /* Change the owner if required. */
876 if (lp_inherit_owner(SNUM(conn
))) {
877 change_file_owner_to_parent(conn
, parent_dir
,
883 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
884 /* If we have an fd, this stat should succeed. */
886 DEBUG(0,("Error doing fstat on open file %s "
888 smb_fname_str_dbg(smb_fname
),
893 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
894 FILE_NOTIFY_CHANGE_FILE_NAME
,
895 smb_fname
->base_name
);
898 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
900 /* File must exist for a stat open. */
901 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
904 status
= smbd_check_access_rights(conn
,
908 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
910 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
911 /* This is a POSIX stat open for delete
912 * or rename on a symlink that points
914 DEBUG(10,("open_file: allowing POSIX "
915 "open on bad symlink %s\n",
916 smb_fname_str_dbg(smb_fname
)));
917 status
= NT_STATUS_OK
;
920 if (!NT_STATUS_IS_OK(status
)) {
921 DEBUG(10,("open_file: "
922 "smbd_check_access_rights on file "
924 smb_fname_str_dbg(smb_fname
),
925 nt_errstr(status
) ));
931 * POSIX allows read-only opens of directories. We don't
932 * want to do this (we use a different code path for this)
933 * so catch a directory open and return an EISDIR. JRA.
936 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
939 return NT_STATUS_FILE_IS_A_DIRECTORY
;
942 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
943 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
944 fsp
->file_pid
= req
? req
->smbpid
: 0;
945 fsp
->can_lock
= True
;
946 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
949 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
950 fsp
->print_file
= NULL
;
951 fsp
->modified
= False
;
952 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
953 fsp
->is_directory
= False
;
954 if (conn
->aio_write_behind_list
&&
955 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
956 conn
->case_sensitive
)) {
957 fsp
->aio_write_behind
= True
;
960 fsp
->wcp
= NULL
; /* Write cache pointer. */
962 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
963 conn
->session_info
->unix_info
->unix_name
,
964 smb_fname_str_dbg(smb_fname
),
965 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
966 conn
->num_files_open
));
972 /****************************************************************************
973 Check if we can open a file with a share mode.
974 Returns True if conflict, False if not.
975 ****************************************************************************/
977 static bool share_conflict(struct share_mode_entry
*entry
,
981 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
982 "entry->share_access = 0x%x, "
983 "entry->private_options = 0x%x\n",
984 (unsigned int)entry
->access_mask
,
985 (unsigned int)entry
->share_access
,
986 (unsigned int)entry
->private_options
));
988 if (server_id_is_disconnected(&entry
->pid
)) {
990 * note: cleanup should have been done by
991 * delay_for_batch_oplocks()
996 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
997 (unsigned int)access_mask
, (unsigned int)share_access
));
999 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
1003 DELETE_ACCESS
)) == 0) {
1004 DEBUG(10,("share_conflict: No conflict due to "
1005 "entry->access_mask = 0x%x\n",
1006 (unsigned int)entry
->access_mask
));
1010 if ((access_mask
& (FILE_WRITE_DATA
|
1014 DELETE_ACCESS
)) == 0) {
1015 DEBUG(10,("share_conflict: No conflict due to "
1016 "access_mask = 0x%x\n",
1017 (unsigned int)access_mask
));
1021 #if 1 /* JRA TEST - Superdebug. */
1022 #define CHECK_MASK(num, am, right, sa, share) \
1023 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
1024 (unsigned int)(num), (unsigned int)(am), \
1025 (unsigned int)(right), (unsigned int)(am)&(right) )); \
1026 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
1027 (unsigned int)(num), (unsigned int)(sa), \
1028 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
1029 if (((am) & (right)) && !((sa) & (share))) { \
1030 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1031 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1032 (unsigned int)(share) )); \
1036 #define CHECK_MASK(num, am, right, sa, share) \
1037 if (((am) & (right)) && !((sa) & (share))) { \
1038 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
1039 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
1040 (unsigned int)(share) )); \
1045 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1046 share_access
, FILE_SHARE_WRITE
);
1047 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
1048 entry
->share_access
, FILE_SHARE_WRITE
);
1050 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1051 share_access
, FILE_SHARE_READ
);
1052 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1053 entry
->share_access
, FILE_SHARE_READ
);
1055 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
1056 share_access
, FILE_SHARE_DELETE
);
1057 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
1058 entry
->share_access
, FILE_SHARE_DELETE
);
1060 DEBUG(10,("share_conflict: No conflict.\n"));
1064 #if defined(DEVELOPER)
1065 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
1067 struct share_mode_entry
*share_entry
)
1069 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1072 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1076 if (is_deferred_open_entry(share_entry
) &&
1077 !open_was_deferred(sconn
, share_entry
->op_mid
)) {
1078 char *str
= talloc_asprintf(talloc_tos(),
1079 "Got a deferred entry without a request: "
1081 share_mode_str(talloc_tos(), num
, share_entry
));
1085 if (share_entry
->share_file_id
== 0) {
1086 /* INTERNAL_OPEN_ONLY */
1090 if (!is_valid_share_mode_entry(share_entry
)) {
1094 fsp
= file_find_dif(sconn
, share_entry
->id
,
1095 share_entry
->share_file_id
);
1097 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1098 share_mode_str(talloc_tos(), num
, share_entry
) ));
1099 smb_panic("validate_my_share_entries: Cannot match a "
1100 "share entry with an open file\n");
1103 if (is_deferred_open_entry(share_entry
)) {
1107 if ((share_entry
->op_type
== NO_OPLOCK
) &&
1108 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
1109 /* Someone has already written to it, but I haven't yet
1114 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
1123 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1124 share_mode_str(talloc_tos(), num
, share_entry
) ));
1125 str
= talloc_asprintf(talloc_tos(),
1126 "validate_my_share_entries: "
1127 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1128 fsp
->fsp_name
->base_name
,
1129 (unsigned int)fsp
->oplock_type
,
1130 (unsigned int)share_entry
->op_type
);
1136 bool is_stat_open(uint32 access_mask
)
1138 return (access_mask
&&
1139 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
1140 FILE_WRITE_ATTRIBUTES
))==0) &&
1141 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
1142 FILE_WRITE_ATTRIBUTES
)) != 0));
1145 /****************************************************************************
1146 Deal with share modes
1147 Invarient: Share mode must be locked on entry and exit.
1148 Returns -1 on error, or number of share modes on success (may be zero).
1149 ****************************************************************************/
1151 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1152 struct share_mode_lock
*lck
,
1155 uint32 share_access
,
1156 uint32 create_options
,
1161 if(lck
->data
->num_share_modes
== 0) {
1162 return NT_STATUS_OK
;
1165 /* A delete on close prohibits everything */
1167 if (is_delete_on_close_set(lck
, name_hash
)) {
1169 * Check the delete on close token
1170 * is valid. It could have been left
1171 * after a server crash.
1173 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1174 if (!share_mode_stale_pid(lck
->data
, i
)) {
1176 *file_existed
= true;
1178 return NT_STATUS_DELETE_PENDING
;
1181 return NT_STATUS_OK
;
1184 if (is_stat_open(access_mask
)) {
1185 /* Stat open that doesn't trigger oplock breaks or share mode
1186 * checks... ! JRA. */
1187 return NT_STATUS_OK
;
1191 * Check if the share modes will give us access.
1194 #if defined(DEVELOPER)
1195 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1196 validate_my_share_entries(conn
->sconn
, i
,
1197 &lck
->data
->share_modes
[i
]);
1201 /* Now we check the share modes, after any oplock breaks. */
1202 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1204 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1208 /* someone else has a share lock on it, check to see if we can
1210 if (share_conflict(&lck
->data
->share_modes
[i
],
1211 access_mask
, share_access
)) {
1213 if (share_mode_stale_pid(lck
->data
, i
)) {
1217 *file_existed
= true;
1219 return NT_STATUS_SHARING_VIOLATION
;
1223 if (lck
->data
->num_share_modes
!= 0) {
1224 *file_existed
= true;
1227 return NT_STATUS_OK
;
1231 * Send a break message to the oplock holder and delay the open for
1235 static NTSTATUS
send_break_message(files_struct
*fsp
,
1236 struct share_mode_entry
*exclusive
,
1241 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1243 DEBUG(10, ("Sending break request to PID %s\n",
1244 procid_str_static(&exclusive
->pid
)));
1245 exclusive
->op_mid
= mid
;
1247 /* Create the message. */
1248 share_mode_entry_to_message(msg
, exclusive
);
1250 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1251 don't want this set in the share mode struct pointed to by lck. */
1253 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
1254 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
1255 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
1258 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
1259 MSG_SMB_BREAK_REQUEST
,
1261 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
1262 if (!NT_STATUS_IS_OK(status
)) {
1263 DEBUG(3, ("Could not send oplock break message: %s\n",
1264 nt_errstr(status
)));
1271 * Return share_mode_entry pointers for :
1272 * 1). Batch oplock entry.
1273 * 2). Batch or exclusive oplock entry (may be identical to #1).
1274 * bool have_level2_oplock
1275 * bool have_no_oplock.
1276 * Do internal consistency checks on the share mode for a file.
1279 static void find_oplock_types(files_struct
*fsp
,
1281 const struct share_mode_lock
*lck
,
1282 struct share_mode_entry
**pp_batch
,
1283 struct share_mode_entry
**pp_ex_or_batch
,
1285 bool *got_no_oplock
)
1290 *pp_ex_or_batch
= NULL
;
1291 *got_level2
= false;
1292 *got_no_oplock
= false;
1294 /* Ignore stat or internal opens, as is done in
1295 delay_for_batch_oplocks() and
1296 delay_for_exclusive_oplocks().
1298 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1302 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1303 struct share_mode_entry
*e
= &lck
->data
->share_modes
[i
];
1305 if (!is_valid_share_mode_entry(e
)) {
1309 if (e
->op_type
== NO_OPLOCK
&& is_stat_open(e
->access_mask
)) {
1310 /* We ignore stat opens in the table - they
1311 always have NO_OPLOCK and never get or
1312 cause breaks. JRA. */
1316 if (BATCH_OPLOCK_TYPE(e
->op_type
)) {
1317 /* batch - can only be one. */
1318 if (share_mode_stale_pid(lck
->data
, i
)) {
1319 DEBUG(10, ("Found stale batch oplock\n"));
1322 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
1323 smb_panic("Bad batch oplock entry.");
1328 if (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
)) {
1329 if (share_mode_stale_pid(lck
->data
, i
)) {
1330 DEBUG(10, ("Found stale duplicate oplock\n"));
1333 /* Exclusive or batch - can only be one. */
1334 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
1335 smb_panic("Bad exclusive or batch oplock entry.");
1337 *pp_ex_or_batch
= e
;
1340 if (LEVEL_II_OPLOCK_TYPE(e
->op_type
)) {
1341 if (*pp_batch
|| *pp_ex_or_batch
) {
1342 if (share_mode_stale_pid(lck
->data
, i
)) {
1343 DEBUG(10, ("Found stale LevelII "
1347 smb_panic("Bad levelII oplock entry.");
1352 if (e
->op_type
== NO_OPLOCK
) {
1353 if (*pp_batch
|| *pp_ex_or_batch
) {
1354 if (share_mode_stale_pid(lck
->data
, i
)) {
1355 DEBUG(10, ("Found stale NO_OPLOCK "
1359 smb_panic("Bad no oplock entry.");
1361 *got_no_oplock
= true;
1366 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1369 struct share_mode_entry
*batch_entry
)
1371 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1374 if (batch_entry
== NULL
) {
1378 if (server_id_is_disconnected(&batch_entry
->pid
)) {
1381 * This could be achieved by sending a break message
1382 * to ourselves. Special considerations for files
1383 * with delete_on_close flag set!
1385 * For now we keep it simple and do not
1386 * allow delete on close for durable handles.
1391 /* Found a batch oplock */
1392 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1396 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1399 struct share_mode_entry
*ex_entry
)
1401 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1404 if (ex_entry
== NULL
) {
1408 if (server_id_is_disconnected(&ex_entry
->pid
)) {
1410 * since only durable handles can get disconnected,
1411 * and we can only get durable handles with batch oplocks,
1412 * this should actually never be reached...
1417 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1421 static bool file_has_brlocks(files_struct
*fsp
)
1423 struct byte_range_lock
*br_lck
;
1425 br_lck
= brl_get_locks_readonly(fsp
);
1429 return br_lck
->num_locks
> 0 ? true : false;
1432 static void grant_fsp_oplock_type(files_struct
*fsp
,
1434 bool got_level2_oplock
,
1435 bool got_a_none_oplock
)
1437 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1438 lp_level2_oplocks(SNUM(fsp
->conn
));
1440 /* Start by granting what the client asked for,
1441 but ensure no SAMBA_PRIVATE bits can be set. */
1442 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1444 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1445 /* No oplocks on internal open. */
1446 fsp
->oplock_type
= NO_OPLOCK
;
1447 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1448 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1452 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1453 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1455 fsp
->oplock_type
= NO_OPLOCK
;
1458 if (is_stat_open(fsp
->access_mask
)) {
1459 /* Leave the value already set. */
1460 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1461 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1466 * Match what was requested (fsp->oplock_type) with
1467 * what was found in the existing share modes.
1470 if (got_a_none_oplock
) {
1471 fsp
->oplock_type
= NO_OPLOCK
;
1472 } else if (got_level2_oplock
) {
1473 if (fsp
->oplock_type
== NO_OPLOCK
||
1474 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1475 /* Store a level2 oplock, but don't tell the client */
1476 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1478 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1481 /* All share_mode_entries are placeholders or deferred.
1482 * Silently upgrade to fake levelII if the client didn't
1483 * ask for an oplock. */
1484 if (fsp
->oplock_type
== NO_OPLOCK
) {
1485 /* Store a level2 oplock, but don't tell the client */
1486 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1491 * Don't grant level2 to clients that don't want them
1492 * or if we've turned them off.
1494 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1495 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1498 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1499 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1502 static bool request_timed_out(struct timeval request_time
,
1503 struct timeval timeout
)
1505 struct timeval now
, end_time
;
1507 end_time
= timeval_sum(&request_time
, &timeout
);
1508 return (timeval_compare(&end_time
, &now
) < 0);
1511 /****************************************************************************
1512 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1513 ****************************************************************************/
1515 static void defer_open(struct share_mode_lock
*lck
,
1516 struct timeval request_time
,
1517 struct timeval timeout
,
1518 struct smb_request
*req
,
1519 struct deferred_open_record
*state
)
1521 struct server_id self
= messaging_server_id(req
->sconn
->msg_ctx
);
1523 /* Paranoia check */
1528 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1529 struct share_mode_entry
*e
= &lck
->data
->share_modes
[i
];
1531 if (is_deferred_open_entry(e
) &&
1532 serverid_equal(&self
, &e
->pid
) &&
1533 (e
->op_mid
== req
->mid
)) {
1534 DEBUG(0, ("Trying to defer an already deferred "
1535 "request: mid=%llu, exiting\n",
1536 (unsigned long long)req
->mid
));
1538 exit_server("attempt to defer a deferred request");
1543 /* End paranoia check */
1545 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1546 "open entry for mid %llu\n",
1547 (unsigned int)request_time
.tv_sec
,
1548 (unsigned int)request_time
.tv_usec
,
1549 (unsigned long long)req
->mid
));
1551 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1552 state
->id
, (char *)state
, sizeof(*state
))) {
1554 exit_server("push_deferred_open_message_smb failed");
1557 add_deferred_open(lck
, req
->mid
, request_time
, self
, state
->id
);
1562 /****************************************************************************
1563 On overwrite open ensure that the attributes match.
1564 ****************************************************************************/
1566 static bool open_match_attributes(connection_struct
*conn
,
1567 uint32 old_dos_attr
,
1568 uint32 new_dos_attr
,
1569 mode_t existing_unx_mode
,
1570 mode_t new_unx_mode
,
1571 mode_t
*returned_unx_mode
)
1573 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1575 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1576 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1578 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1579 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1580 *returned_unx_mode
= new_unx_mode
;
1582 *returned_unx_mode
= (mode_t
)0;
1585 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1586 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1587 "returned_unx_mode = 0%o\n",
1588 (unsigned int)old_dos_attr
,
1589 (unsigned int)existing_unx_mode
,
1590 (unsigned int)new_dos_attr
,
1591 (unsigned int)*returned_unx_mode
));
1593 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1594 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1595 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1596 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1600 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1601 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1602 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1609 /****************************************************************************
1610 Special FCB or DOS processing in the case of a sharing violation.
1611 Try and find a duplicated file handle.
1612 ****************************************************************************/
1614 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1615 connection_struct
*conn
,
1616 files_struct
*fsp_to_dup_into
,
1617 const struct smb_filename
*smb_fname
,
1622 uint32 share_access
,
1623 uint32 create_options
)
1627 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1628 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1630 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1631 fsp
= file_find_di_next(fsp
)) {
1633 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1634 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1635 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1636 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
1637 (unsigned int)fsp
->file_pid
,
1638 (unsigned int)fsp
->fh
->private_options
,
1639 (unsigned int)fsp
->access_mask
));
1641 if (fsp
->fh
->fd
!= -1 &&
1642 fsp
->vuid
== vuid
&&
1643 fsp
->file_pid
== file_pid
&&
1644 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1645 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1646 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1647 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1648 strequal(fsp
->fsp_name
->stream_name
,
1649 smb_fname
->stream_name
)) {
1650 DEBUG(10,("fcb_or_dos_open: file match\n"));
1656 return NT_STATUS_NOT_FOUND
;
1659 /* quite an insane set of semantics ... */
1660 if (is_executable(smb_fname
->base_name
) &&
1661 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1662 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1663 return NT_STATUS_INVALID_PARAMETER
;
1666 /* We need to duplicate this fsp. */
1667 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1668 create_options
, fsp_to_dup_into
);
1671 static void schedule_defer_open(struct share_mode_lock
*lck
,
1672 struct timeval request_time
,
1673 struct smb_request
*req
)
1675 struct deferred_open_record state
;
1677 /* This is a relative time, added to the absolute
1678 request_time value to get the absolute timeout time.
1679 Note that if this is the second or greater time we enter
1680 this codepath for this particular request mid then
1681 request_time is left as the absolute time of the *first*
1682 time this request mid was processed. This is what allows
1683 the request to eventually time out. */
1685 struct timeval timeout
;
1687 /* Normally the smbd we asked should respond within
1688 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1689 * the client did, give twice the timeout as a safety
1690 * measure here in case the other smbd is stuck
1691 * somewhere else. */
1693 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1695 /* Nothing actually uses state.delayed_for_oplocks
1696 but it's handy to differentiate in debug messages
1697 between a 30 second delay due to oplock break, and
1698 a 1 second delay for share mode conflicts. */
1700 state
.delayed_for_oplocks
= True
;
1701 state
.async_open
= false;
1702 state
.id
= lck
->data
->id
;
1704 if (!request_timed_out(request_time
, timeout
)) {
1705 defer_open(lck
, request_time
, timeout
, req
, &state
);
1709 /****************************************************************************
1710 Reschedule an open call that went asynchronous.
1711 ****************************************************************************/
1713 static void schedule_async_open(struct timeval request_time
,
1714 struct smb_request
*req
)
1716 struct deferred_open_record state
;
1717 struct timeval timeout
;
1719 timeout
= timeval_set(20, 0);
1722 state
.delayed_for_oplocks
= false;
1723 state
.async_open
= true;
1725 if (!request_timed_out(request_time
, timeout
)) {
1726 defer_open(NULL
, request_time
, timeout
, req
, &state
);
1730 /****************************************************************************
1731 Work out what access_mask to use from what the client sent us.
1732 ****************************************************************************/
1734 static NTSTATUS
smbd_calculate_maximum_allowed_access(
1735 connection_struct
*conn
,
1736 const struct smb_filename
*smb_fname
,
1737 uint32_t *p_access_mask
)
1739 struct security_descriptor
*sd
;
1740 uint32_t access_granted
;
1743 if (get_current_uid(conn
) == (uid_t
)0) {
1744 *p_access_mask
|= FILE_GENERIC_ALL
;
1745 return NT_STATUS_OK
;
1748 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1754 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1756 * File did not exist
1758 *p_access_mask
= FILE_GENERIC_ALL
;
1759 return NT_STATUS_OK
;
1761 if (!NT_STATUS_IS_OK(status
)) {
1762 DEBUG(10,("smbd_calculate_access_mask: "
1763 "Could not get acl on file %s: %s\n",
1764 smb_fname_str_dbg(smb_fname
),
1765 nt_errstr(status
)));
1766 return NT_STATUS_ACCESS_DENIED
;
1770 * If we can access the path to this file, by
1771 * default we have FILE_READ_ATTRIBUTES from the
1772 * containing directory. See the section:
1773 * "Algorithm to Check Access to an Existing File"
1776 * se_file_access_check()
1777 * also takes care of owner WRITE_DAC and READ_CONTROL.
1779 status
= se_file_access_check(sd
,
1780 get_current_nttok(conn
),
1782 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
1787 if (!NT_STATUS_IS_OK(status
)) {
1788 DEBUG(10, ("smbd_calculate_access_mask: "
1789 "Access denied on file %s: "
1790 "when calculating maximum access\n",
1791 smb_fname_str_dbg(smb_fname
)));
1792 return NT_STATUS_ACCESS_DENIED
;
1794 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
1796 if (!(access_granted
& DELETE_ACCESS
)) {
1797 if (can_delete_file_in_directory(conn
, smb_fname
)) {
1798 *p_access_mask
|= DELETE_ACCESS
;
1802 return NT_STATUS_OK
;
1805 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1806 const struct smb_filename
*smb_fname
,
1807 uint32_t access_mask
,
1808 uint32_t *access_mask_out
)
1811 uint32_t orig_access_mask
= access_mask
;
1812 uint32_t rejected_share_access
;
1815 * Convert GENERIC bits to specific bits.
1818 se_map_generic(&access_mask
, &file_generic_mapping
);
1820 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1821 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1823 status
= smbd_calculate_maximum_allowed_access(
1824 conn
, smb_fname
, &access_mask
);
1826 if (!NT_STATUS_IS_OK(status
)) {
1830 access_mask
&= conn
->share_access
;
1833 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1835 if (rejected_share_access
) {
1836 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1837 "file %s: rejected by share access mask[0x%08X] "
1838 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1839 smb_fname_str_dbg(smb_fname
),
1841 orig_access_mask
, access_mask
,
1842 rejected_share_access
));
1843 return NT_STATUS_ACCESS_DENIED
;
1846 *access_mask_out
= access_mask
;
1847 return NT_STATUS_OK
;
1850 /****************************************************************************
1851 Remove the deferred open entry under lock.
1852 ****************************************************************************/
1854 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1855 struct server_id pid
)
1857 struct share_mode_lock
*lck
= get_existing_share_mode_lock(
1860 DEBUG(0, ("could not get share mode lock\n"));
1863 del_deferred_open_entry(lck
, mid
, pid
);
1867 /****************************************************************************
1868 Return true if this is a state pointer to an asynchronous create.
1869 ****************************************************************************/
1871 bool is_deferred_open_async(const void *ptr
)
1873 const struct deferred_open_record
*state
= (const struct deferred_open_record
*)ptr
;
1875 return state
->async_open
;
1878 static bool clear_ads(uint32_t create_disposition
)
1882 switch (create_disposition
) {
1883 case FILE_SUPERSEDE
:
1884 case FILE_OVERWRITE_IF
:
1885 case FILE_OVERWRITE
:
1894 static int disposition_to_open_flags(uint32_t create_disposition
)
1899 * Currently we're using FILE_SUPERSEDE as the same as
1900 * FILE_OVERWRITE_IF but they really are
1901 * different. FILE_SUPERSEDE deletes an existing file
1902 * (requiring delete access) then recreates it.
1905 switch (create_disposition
) {
1906 case FILE_SUPERSEDE
:
1907 case FILE_OVERWRITE_IF
:
1909 * If file exists replace/overwrite. If file doesn't
1912 ret
= O_CREAT
|O_TRUNC
;
1917 * If file exists open. If file doesn't exist error.
1922 case FILE_OVERWRITE
:
1924 * If file exists overwrite. If file doesn't exist
1932 * If file exists error. If file doesn't exist create.
1934 ret
= O_CREAT
|O_EXCL
;
1939 * If file exists open. If file doesn't exist create.
1947 /****************************************************************************
1948 Open a file with a share mode. Passed in an already created files_struct *.
1949 ****************************************************************************/
1951 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1952 struct smb_request
*req
,
1953 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1954 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1955 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1956 uint32 create_options
, /* options such as delete on close. */
1957 uint32 new_dos_attributes
, /* attributes used for new file. */
1958 int oplock_request
, /* internal Samba oplock codes. */
1959 /* Information (FILE_EXISTS etc.) */
1960 uint32_t private_flags
, /* Samba specific flags. */
1964 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1967 bool file_existed
= VALID_STAT(smb_fname
->st
);
1968 bool def_acl
= False
;
1969 bool posix_open
= False
;
1970 bool new_file_created
= False
;
1971 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1972 mode_t new_unx_mode
= (mode_t
)0;
1973 mode_t unx_mode
= (mode_t
)0;
1975 uint32 existing_dos_attributes
= 0;
1976 struct timeval request_time
= timeval_zero();
1977 struct share_mode_lock
*lck
= NULL
;
1978 uint32 open_access_mask
= access_mask
;
1981 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
1983 if (conn
->printer
) {
1985 * Printers are handled completely differently.
1986 * Most of the passed parameters are ignored.
1990 *pinfo
= FILE_WAS_CREATED
;
1993 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1994 smb_fname_str_dbg(smb_fname
)));
1997 DEBUG(0,("open_file_ntcreate: printer open without "
1998 "an SMB request!\n"));
1999 return NT_STATUS_INTERNAL_ERROR
;
2002 return print_spool_open(fsp
, smb_fname
->base_name
,
2006 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
2008 return NT_STATUS_NO_MEMORY
;
2011 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2013 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2014 new_dos_attributes
= 0;
2016 /* Windows allows a new file to be created and
2017 silently removes a FILE_ATTRIBUTE_DIRECTORY
2018 sent by the client. Do the same. */
2020 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
2022 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2024 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2025 smb_fname
, parent_dir
);
2028 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2029 "access_mask=0x%x share_access=0x%x "
2030 "create_disposition = 0x%x create_options=0x%x "
2031 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2032 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
2033 access_mask
, share_access
, create_disposition
,
2034 create_options
, (unsigned int)unx_mode
, oplock_request
,
2035 (unsigned int)private_flags
));
2038 /* Ensure req == NULL means INTERNAL_OPEN_ONLY */
2039 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) != 0));
2041 /* And req != NULL means no INTERNAL_OPEN_ONLY */
2042 SMB_ASSERT(((oplock_request
& INTERNAL_OPEN_ONLY
) == 0));
2046 * Only non-internal opens can be deferred at all
2051 if (get_deferred_open_message_state(req
,
2054 /* Remember the absolute time of the original
2055 request with this mid. We'll use it later to
2056 see if this has timed out. */
2058 /* If it was an async create retry, the file
2061 if (is_deferred_open_async(ptr
)) {
2062 SET_STAT_INVALID(smb_fname
->st
);
2063 file_existed
= false;
2065 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
2066 /* Remove the deferred open entry under lock. */
2067 remove_deferred_open_entry(
2068 state
->id
, req
->mid
,
2069 messaging_server_id(req
->sconn
->msg_ctx
));
2072 /* Ensure we don't reprocess this message. */
2073 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
2078 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
2080 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
2084 /* ignore any oplock requests if oplocks are disabled */
2085 if (!lp_oplocks(SNUM(conn
)) ||
2086 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
2087 /* Mask off everything except the private Samba bits. */
2088 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
2091 /* this is for OS/2 long file names - say we don't support them */
2092 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
2093 /* OS/2 Workplace shell fix may be main code stream in a later
2095 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2097 if (use_nt_status()) {
2098 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2100 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
2103 switch( create_disposition
) {
2105 /* If file exists open. If file doesn't exist error. */
2106 if (!file_existed
) {
2107 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2108 "requested for file %s and file "
2110 smb_fname_str_dbg(smb_fname
)));
2112 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2116 case FILE_OVERWRITE
:
2117 /* If file exists overwrite. If file doesn't exist
2119 if (!file_existed
) {
2120 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2121 "requested for file %s and file "
2123 smb_fname_str_dbg(smb_fname
) ));
2125 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2130 /* If file exists error. If file doesn't exist
2133 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2134 "requested for file %s and file "
2135 "already exists.\n",
2136 smb_fname_str_dbg(smb_fname
)));
2137 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2142 return map_nt_error_from_unix(errno
);
2146 case FILE_SUPERSEDE
:
2147 case FILE_OVERWRITE_IF
:
2151 return NT_STATUS_INVALID_PARAMETER
;
2154 flags2
= disposition_to_open_flags(create_disposition
);
2156 /* We only care about matching attributes on file exists and
2159 if (!posix_open
&& file_existed
&&
2160 ((create_disposition
== FILE_OVERWRITE
) ||
2161 (create_disposition
== FILE_OVERWRITE_IF
))) {
2162 if (!open_match_attributes(conn
, existing_dos_attributes
,
2164 smb_fname
->st
.st_ex_mode
,
2165 unx_mode
, &new_unx_mode
)) {
2166 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2167 "for file %s (%x %x) (0%o, 0%o)\n",
2168 smb_fname_str_dbg(smb_fname
),
2169 existing_dos_attributes
,
2171 (unsigned int)smb_fname
->st
.st_ex_mode
,
2172 (unsigned int)unx_mode
));
2174 return NT_STATUS_ACCESS_DENIED
;
2178 status
= smbd_calculate_access_mask(conn
, smb_fname
,
2181 if (!NT_STATUS_IS_OK(status
)) {
2182 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2183 "on file %s returned %s\n",
2184 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
2188 open_access_mask
= access_mask
;
2190 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
2191 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
2194 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2195 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
2199 * Note that we ignore the append flag as append does not
2200 * mean the same thing under DOS and Unix.
2203 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
2204 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
2205 /* DENY_DOS opens are always underlying read-write on the
2206 file handle, no matter what the requested access mask
2208 if ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
2209 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
2219 * Currently we only look at FILE_WRITE_THROUGH for create options.
2223 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
2228 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
2232 if (!posix_open
&& !CAN_WRITE(conn
)) {
2234 * We should really return a permission denied error if either
2235 * O_CREAT or O_TRUNC are set, but for compatibility with
2236 * older versions of Samba we just AND them out.
2238 flags2
&= ~(O_CREAT
|O_TRUNC
);
2242 * Ensure we can't write on a read-only share or file.
2245 if (flags
!= O_RDONLY
&& file_existed
&&
2246 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
2247 DEBUG(5,("open_file_ntcreate: write access requested for "
2248 "file %s on read only %s\n",
2249 smb_fname_str_dbg(smb_fname
),
2250 !CAN_WRITE(conn
) ? "share" : "file" ));
2252 return NT_STATUS_ACCESS_DENIED
;
2255 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2256 fsp
->share_access
= share_access
;
2257 fsp
->fh
->private_options
= private_flags
;
2258 fsp
->access_mask
= open_access_mask
; /* We change this to the
2259 * requested access_mask after
2260 * the open is done. */
2261 fsp
->posix_open
= posix_open
;
2263 /* Ensure no SAMBA_PRIVATE bits can be set. */
2264 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
2266 if (timeval_is_zero(&request_time
)) {
2267 request_time
= fsp
->open_time
;
2271 struct share_mode_entry
*batch_entry
= NULL
;
2272 struct share_mode_entry
*exclusive_entry
= NULL
;
2273 bool got_level2_oplock
= false;
2274 bool got_a_none_oplock
= false;
2277 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2278 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2280 lck
= get_share_mode_lock(talloc_tos(), id
,
2282 smb_fname
, &old_write_time
);
2284 DEBUG(0, ("Could not get share mode lock\n"));
2285 return NT_STATUS_SHARING_VIOLATION
;
2288 /* Get the types we need to examine. */
2289 find_oplock_types(fsp
,
2295 &got_a_none_oplock
);
2297 /* First pass - send break only on batch oplocks. */
2298 if ((req
!= NULL
) &&
2299 delay_for_batch_oplocks(fsp
,
2303 schedule_defer_open(lck
, request_time
, req
);
2305 return NT_STATUS_SHARING_VIOLATION
;
2308 /* Use the client requested access mask here, not the one we
2310 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2311 access_mask
, share_access
,
2312 create_options
, &file_existed
);
2314 if (NT_STATUS_IS_OK(status
)) {
2315 /* We might be going to allow this open. Check oplock
2317 /* Second pass - send break for both batch or
2318 * exclusive oplocks. */
2319 if ((req
!= NULL
) &&
2320 delay_for_exclusive_oplocks(
2325 schedule_defer_open(lck
, request_time
, req
);
2327 return NT_STATUS_SHARING_VIOLATION
;
2331 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
2332 /* DELETE_PENDING is not deferred for a second */
2337 grant_fsp_oplock_type(fsp
,
2342 if (!NT_STATUS_IS_OK(status
)) {
2343 uint32 can_access_mask
;
2344 bool can_access
= True
;
2346 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2348 /* Check if this can be done with the deny_dos and fcb
2351 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2352 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2354 DEBUG(0, ("DOS open without an SMB "
2357 return NT_STATUS_INTERNAL_ERROR
;
2360 /* Use the client requested access mask here,
2361 * not the one we open with. */
2362 status
= fcb_or_dos_open(req
,
2373 if (NT_STATUS_IS_OK(status
)) {
2376 *pinfo
= FILE_WAS_OPENED
;
2378 return NT_STATUS_OK
;
2383 * This next line is a subtlety we need for
2384 * MS-Access. If a file open will fail due to share
2385 * permissions and also for security (access) reasons,
2386 * we need to return the access failed error, not the
2387 * share error. We can't open the file due to kernel
2388 * oplock deadlock (it's possible we failed above on
2389 * the open_mode_check()) so use a userspace check.
2392 if (flags
& O_RDWR
) {
2393 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2394 } else if (flags
& O_WRONLY
) {
2395 can_access_mask
= FILE_WRITE_DATA
;
2397 can_access_mask
= FILE_READ_DATA
;
2400 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2401 !CAN_WRITE(conn
)) ||
2402 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
2403 smb_fname
, can_access_mask
))) {
2408 * If we're returning a share violation, ensure we
2409 * cope with the braindead 1 second delay (SMB1 only).
2412 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2413 !conn
->sconn
->using_smb2
&&
2414 lp_defer_sharing_violations()) {
2415 struct timeval timeout
;
2416 struct deferred_open_record state
;
2419 /* this is a hack to speed up torture tests
2421 timeout_usecs
= lp_parm_int(SNUM(conn
),
2422 "smbd","sharedelay",
2423 SHARING_VIOLATION_USEC_WAIT
);
2425 /* This is a relative time, added to the absolute
2426 request_time value to get the absolute timeout time.
2427 Note that if this is the second or greater time we enter
2428 this codepath for this particular request mid then
2429 request_time is left as the absolute time of the *first*
2430 time this request mid was processed. This is what allows
2431 the request to eventually time out. */
2433 timeout
= timeval_set(0, timeout_usecs
);
2435 /* Nothing actually uses state.delayed_for_oplocks
2436 but it's handy to differentiate in debug messages
2437 between a 30 second delay due to oplock break, and
2438 a 1 second delay for share mode conflicts. */
2440 state
.delayed_for_oplocks
= False
;
2441 state
.async_open
= false;
2445 && !request_timed_out(request_time
,
2447 defer_open(lck
, request_time
, timeout
,
2455 * We have detected a sharing violation here
2456 * so return the correct error code
2458 status
= NT_STATUS_SHARING_VIOLATION
;
2460 status
= NT_STATUS_ACCESS_DENIED
;
2466 * We exit this block with the share entry *locked*.....
2470 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
2473 * Ensure we pay attention to default ACLs on directories if required.
2476 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2477 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2478 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2481 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2482 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2483 (unsigned int)flags
, (unsigned int)flags2
,
2484 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2485 (unsigned int)open_access_mask
));
2487 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2488 flags
|flags2
, unx_mode
, access_mask
,
2489 open_access_mask
, &new_file_created
);
2491 if (!NT_STATUS_IS_OK(fsp_open
)) {
2492 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
2493 schedule_async_open(request_time
, req
);
2499 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
2501 * The file did exist, but some other (local or NFS)
2502 * process either renamed/unlinked and re-created the
2503 * file with different dev/ino after we walked the path,
2504 * but before we did the open. We could retry the
2505 * open but it's a rare enough case it's easier to
2506 * just fail the open to prevent creating any problems
2507 * in the open file db having the wrong dev/ino key.
2511 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2512 "Old (dev=0x%llu, ino =0x%llu). "
2513 "New (dev=0x%llu, ino=0x%llu). Failing open "
2514 " with NT_STATUS_ACCESS_DENIED.\n",
2515 smb_fname_str_dbg(smb_fname
),
2516 (unsigned long long)saved_stat
.st_ex_dev
,
2517 (unsigned long long)saved_stat
.st_ex_ino
,
2518 (unsigned long long)smb_fname
->st
.st_ex_dev
,
2519 (unsigned long long)smb_fname
->st
.st_ex_ino
));
2520 return NT_STATUS_ACCESS_DENIED
;
2523 if (!file_existed
) {
2524 struct share_mode_entry
*batch_entry
= NULL
;
2525 struct share_mode_entry
*exclusive_entry
= NULL
;
2526 bool got_level2_oplock
= false;
2527 bool got_a_none_oplock
= false;
2528 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2531 * Deal with the race condition where two smbd's detect the
2532 * file doesn't exist and do the create at the same time. One
2533 * of them will win and set a share mode, the other (ie. this
2534 * one) should check if the requested share mode for this
2535 * create is allowed.
2539 * Now the file exists and fsp is successfully opened,
2540 * fsp->dev and fsp->inode are valid and should replace the
2541 * dev=0,inode=0 from a non existent file. Spotted by
2542 * Nadav Danieli <nadavd@exanet.com>. JRA.
2547 lck
= get_share_mode_lock(talloc_tos(), id
,
2549 smb_fname
, &old_write_time
);
2552 DEBUG(0, ("open_file_ntcreate: Could not get share "
2553 "mode lock for %s\n",
2554 smb_fname_str_dbg(smb_fname
)));
2556 return NT_STATUS_SHARING_VIOLATION
;
2559 /* Get the types we need to examine. */
2560 find_oplock_types(fsp
,
2566 &got_a_none_oplock
);
2568 /* First pass - send break only on batch oplocks. */
2569 if ((req
!= NULL
) &&
2570 delay_for_batch_oplocks(fsp
,
2574 schedule_defer_open(lck
, request_time
, req
);
2577 return NT_STATUS_SHARING_VIOLATION
;
2580 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2581 access_mask
, share_access
,
2582 create_options
, &file_existed
);
2584 if (NT_STATUS_IS_OK(status
)) {
2585 /* We might be going to allow this open. Check oplock
2587 /* Second pass - send break for both batch or
2588 * exclusive oplocks. */
2589 if ((req
!= NULL
) &&
2590 delay_for_exclusive_oplocks(
2595 schedule_defer_open(lck
, request_time
, req
);
2598 return NT_STATUS_SHARING_VIOLATION
;
2602 if (!NT_STATUS_IS_OK(status
)) {
2603 struct deferred_open_record state
;
2605 state
.delayed_for_oplocks
= False
;
2606 state
.async_open
= false;
2609 /* Do it all over again immediately. In the second
2610 * round we will find that the file existed and handle
2611 * the DELETE_PENDING and FCB cases correctly. No need
2612 * to duplicate the code here. Essentially this is a
2613 * "goto top of this function", but don't tell
2617 defer_open(lck
, request_time
, timeval_zero(),
2625 grant_fsp_oplock_type(fsp
,
2631 * We exit this block with the share entry *locked*.....
2636 SMB_ASSERT(lck
!= NULL
);
2638 /* Delete streams if create_disposition requires it */
2639 if (!new_file_created
&& clear_ads(create_disposition
) &&
2640 !is_ntfs_stream_smb_fname(smb_fname
)) {
2641 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2642 if (!NT_STATUS_IS_OK(status
)) {
2649 /* note that we ignore failure for the following. It is
2650 basically a hack for NFS, and NFS will never set one of
2651 these only read them. Nobody but Samba can ever set a deny
2652 mode and we have already checked our more authoritative
2653 locking database for permission to set this deny mode. If
2654 the kernel refuses the operations then the kernel is wrong.
2655 note that GPFS supports it as well - jmcd */
2657 if (fsp
->fh
->fd
!= -1 && lp_kernel_share_modes(SNUM(conn
))) {
2659 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2660 if(ret_flock
== -1 ){
2665 return NT_STATUS_SHARING_VIOLATION
;
2670 * At this point onwards, we can guarentee that the share entry
2671 * is locked, whether we created the file or not, and that the
2672 * deny mode is compatible with all current opens.
2676 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2677 * but we don't have to store this - just ignore it on access check.
2679 if (conn
->sconn
->using_smb2
) {
2681 * SMB2 doesn't return it (according to Microsoft tests).
2682 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2683 * File created with access = 0x7 (Read, Write, Delete)
2684 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2686 fsp
->access_mask
= access_mask
;
2688 /* But SMB1 does. */
2689 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2693 /* stat opens on existing files don't get oplocks. */
2694 if (is_stat_open(open_access_mask
)) {
2695 fsp
->oplock_type
= NO_OPLOCK
;
2699 if (new_file_created
) {
2700 info
= FILE_WAS_CREATED
;
2702 if (flags2
& O_TRUNC
) {
2703 info
= FILE_WAS_OVERWRITTEN
;
2705 info
= FILE_WAS_OPENED
;
2714 * Setup the oplock info in both the shared memory and
2718 status
= set_file_oplock(fsp
, fsp
->oplock_type
);
2719 if (!NT_STATUS_IS_OK(status
)) {
2721 * Could not get the kernel oplock or there are byte-range
2722 * locks on the file.
2724 fsp
->oplock_type
= NO_OPLOCK
;
2727 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2731 /* Handle strange delete on close create semantics. */
2732 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2734 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2736 if (!NT_STATUS_IS_OK(status
)) {
2737 /* Remember to delete the mode we just added. */
2738 del_share_mode(lck
, fsp
);
2743 /* Note that here we set the *inital* delete on close flag,
2744 not the regular one. The magic gets handled in close. */
2745 fsp
->initial_delete_on_close
= True
;
2748 if (info
!= FILE_WAS_OPENED
) {
2749 /* Files should be initially set as archive */
2750 if (lp_map_archive(SNUM(conn
)) ||
2751 lp_store_dos_attributes(SNUM(conn
))) {
2753 if (file_set_dosmode(conn
, smb_fname
,
2754 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2755 parent_dir
, true) == 0) {
2756 unx_mode
= smb_fname
->st
.st_ex_mode
;
2762 /* Determine sparse flag. */
2764 /* POSIX opens are sparse by default. */
2765 fsp
->is_sparse
= true;
2767 fsp
->is_sparse
= (file_existed
&&
2768 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2772 * Take care of inherited ACLs on created files - if default ACL not
2776 if (!posix_open
&& new_file_created
&& !def_acl
) {
2778 int saved_errno
= errno
; /* We might get ENOSYS in the next
2781 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2783 errno
= saved_errno
; /* Ignore ENOSYS */
2786 } else if (new_unx_mode
) {
2790 /* Attributes need changing. File already existed. */
2793 int saved_errno
= errno
; /* We might get ENOSYS in the
2795 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2797 if (ret
== -1 && errno
== ENOSYS
) {
2798 errno
= saved_errno
; /* Ignore ENOSYS */
2800 DEBUG(5, ("open_file_ntcreate: reset "
2801 "attributes of file %s to 0%o\n",
2802 smb_fname_str_dbg(smb_fname
),
2803 (unsigned int)new_unx_mode
));
2804 ret
= 0; /* Don't do the fchmod below. */
2809 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2810 DEBUG(5, ("open_file_ntcreate: failed to reset "
2811 "attributes of file %s to 0%o\n",
2812 smb_fname_str_dbg(smb_fname
),
2813 (unsigned int)new_unx_mode
));
2816 /* If this is a successful open, we must remove any deferred open
2819 del_deferred_open_entry(lck
, req
->mid
,
2820 messaging_server_id(req
->sconn
->msg_ctx
));
2824 return NT_STATUS_OK
;
2827 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2828 struct smb_filename
*smb_dname
,
2829 uint32 file_attributes
)
2832 char *parent_dir
= NULL
;
2834 bool posix_open
= false;
2835 bool need_re_stat
= false;
2836 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2838 if (!CAN_WRITE(conn
) || (access_mask
& ~(conn
->share_access
))) {
2839 DEBUG(5,("mkdir_internal: failing share access "
2840 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
2841 return NT_STATUS_ACCESS_DENIED
;
2844 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2846 return NT_STATUS_NO_MEMORY
;
2849 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2851 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2853 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2856 status
= check_parent_access(conn
,
2859 if(!NT_STATUS_IS_OK(status
)) {
2860 DEBUG(5,("mkdir_internal: check_parent_access "
2861 "on directory %s for path %s returned %s\n",
2863 smb_dname
->base_name
,
2864 nt_errstr(status
) ));
2868 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2869 return map_nt_error_from_unix(errno
);
2872 /* Ensure we're checking for a symlink here.... */
2873 /* We don't want to get caught by a symlink racer. */
2875 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2876 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2877 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2878 return map_nt_error_from_unix(errno
);
2881 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2882 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2883 smb_fname_str_dbg(smb_dname
)));
2884 return NT_STATUS_NOT_A_DIRECTORY
;
2887 if (lp_store_dos_attributes(SNUM(conn
))) {
2889 file_set_dosmode(conn
, smb_dname
,
2890 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2895 if (lp_inherit_perms(SNUM(conn
))) {
2896 inherit_access_posix_acl(conn
, parent_dir
,
2897 smb_dname
->base_name
, mode
);
2898 need_re_stat
= true;
2903 * Check if high bits should have been set,
2904 * then (if bits are missing): add them.
2905 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2908 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2909 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2910 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2911 (smb_dname
->st
.st_ex_mode
|
2912 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2913 need_re_stat
= true;
2917 /* Change the owner if required. */
2918 if (lp_inherit_owner(SNUM(conn
))) {
2919 change_dir_owner_to_parent(conn
, parent_dir
,
2920 smb_dname
->base_name
,
2922 need_re_stat
= true;
2926 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2927 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2928 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2929 return map_nt_error_from_unix(errno
);
2933 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2934 smb_dname
->base_name
);
2936 return NT_STATUS_OK
;
2939 /****************************************************************************
2940 Open a directory from an NT SMB call.
2941 ****************************************************************************/
2943 static NTSTATUS
open_directory(connection_struct
*conn
,
2944 struct smb_request
*req
,
2945 struct smb_filename
*smb_dname
,
2947 uint32 share_access
,
2948 uint32 create_disposition
,
2949 uint32 create_options
,
2950 uint32 file_attributes
,
2952 files_struct
**result
)
2954 files_struct
*fsp
= NULL
;
2955 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2956 struct share_mode_lock
*lck
= NULL
;
2958 struct timespec mtimespec
;
2961 if (is_ntfs_stream_smb_fname(smb_dname
)) {
2962 DEBUG(2, ("open_directory: %s is a stream name!\n",
2963 smb_fname_str_dbg(smb_dname
)));
2964 return NT_STATUS_NOT_A_DIRECTORY
;
2967 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
2968 /* Ensure we have a directory attribute. */
2969 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2972 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2973 "share_access = 0x%x create_options = 0x%x, "
2974 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2975 smb_fname_str_dbg(smb_dname
),
2976 (unsigned int)access_mask
,
2977 (unsigned int)share_access
,
2978 (unsigned int)create_options
,
2979 (unsigned int)create_disposition
,
2980 (unsigned int)file_attributes
));
2982 status
= smbd_calculate_access_mask(conn
, smb_dname
,
2983 access_mask
, &access_mask
);
2984 if (!NT_STATUS_IS_OK(status
)) {
2985 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2986 "on file %s returned %s\n",
2987 smb_fname_str_dbg(smb_dname
),
2988 nt_errstr(status
)));
2992 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2993 !security_token_has_privilege(get_current_nttok(conn
),
2994 SEC_PRIV_SECURITY
)) {
2995 DEBUG(10, ("open_directory: open on %s "
2996 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2997 smb_fname_str_dbg(smb_dname
)));
2998 return NT_STATUS_PRIVILEGE_NOT_HELD
;
3001 switch( create_disposition
) {
3005 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3008 info
= FILE_WAS_OPENED
;
3013 /* If directory exists error. If directory doesn't
3017 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
3018 DEBUG(2, ("open_directory: unable to create "
3019 "%s. Error was %s\n",
3020 smb_fname_str_dbg(smb_dname
),
3021 nt_errstr(status
)));
3025 status
= mkdir_internal(conn
, smb_dname
,
3028 if (!NT_STATUS_IS_OK(status
)) {
3029 DEBUG(2, ("open_directory: unable to create "
3030 "%s. Error was %s\n",
3031 smb_fname_str_dbg(smb_dname
),
3032 nt_errstr(status
)));
3036 info
= FILE_WAS_CREATED
;
3041 * If directory exists open. If directory doesn't
3046 status
= NT_STATUS_OK
;
3047 info
= FILE_WAS_OPENED
;
3049 status
= mkdir_internal(conn
, smb_dname
,
3052 if (NT_STATUS_IS_OK(status
)) {
3053 info
= FILE_WAS_CREATED
;
3055 /* Cope with create race. */
3056 if (!NT_STATUS_EQUAL(status
,
3057 NT_STATUS_OBJECT_NAME_COLLISION
)) {
3058 DEBUG(2, ("open_directory: unable to create "
3059 "%s. Error was %s\n",
3060 smb_fname_str_dbg(smb_dname
),
3061 nt_errstr(status
)));
3064 info
= FILE_WAS_OPENED
;
3070 case FILE_SUPERSEDE
:
3071 case FILE_OVERWRITE
:
3072 case FILE_OVERWRITE_IF
:
3074 DEBUG(5,("open_directory: invalid create_disposition "
3075 "0x%x for directory %s\n",
3076 (unsigned int)create_disposition
,
3077 smb_fname_str_dbg(smb_dname
)));
3078 return NT_STATUS_INVALID_PARAMETER
;
3081 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3082 DEBUG(5,("open_directory: %s is not a directory !\n",
3083 smb_fname_str_dbg(smb_dname
)));
3084 return NT_STATUS_NOT_A_DIRECTORY
;
3087 if (info
== FILE_WAS_OPENED
) {
3088 status
= smbd_check_access_rights(conn
, smb_dname
, access_mask
);
3089 if (!NT_STATUS_IS_OK(status
)) {
3090 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3091 "file %s failed with %s\n",
3092 smb_fname_str_dbg(smb_dname
),
3093 nt_errstr(status
)));
3098 status
= file_new(req
, conn
, &fsp
);
3099 if(!NT_STATUS_IS_OK(status
)) {
3104 * Setup the files_struct for it.
3107 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
3108 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
3109 fsp
->file_pid
= req
? req
->smbpid
: 0;
3110 fsp
->can_lock
= False
;
3111 fsp
->can_read
= False
;
3112 fsp
->can_write
= False
;
3114 fsp
->share_access
= share_access
;
3115 fsp
->fh
->private_options
= 0;
3117 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3119 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3120 fsp
->print_file
= NULL
;
3121 fsp
->modified
= False
;
3122 fsp
->oplock_type
= NO_OPLOCK
;
3123 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
3124 fsp
->is_directory
= True
;
3125 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
3126 status
= fsp_set_smb_fname(fsp
, smb_dname
);
3127 if (!NT_STATUS_IS_OK(status
)) {
3128 file_free(req
, fsp
);
3132 /* Don't store old timestamps for directory
3133 handles in the internal database. We don't
3134 update them in there if new objects
3135 are creaded in the directory. Currently
3136 we only update timestamps on file writes.
3139 ZERO_STRUCT(mtimespec
);
3142 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
3144 /* POSIX allows us to open a directory with O_RDONLY. */
3145 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
3147 if (!NT_STATUS_IS_OK(status
)) {
3148 DEBUG(5, ("open_directory: Could not open fd for "
3150 smb_fname_str_dbg(smb_dname
),
3151 nt_errstr(status
)));
3152 file_free(req
, fsp
);
3156 status
= vfs_stat_fsp(fsp
);
3157 if (!NT_STATUS_IS_OK(status
)) {
3159 file_free(req
, fsp
);
3163 /* Ensure there was no race condition. */
3164 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
3165 DEBUG(5,("open_directory: stat struct differs for "
3167 smb_fname_str_dbg(smb_dname
)));
3169 file_free(req
, fsp
);
3170 return NT_STATUS_ACCESS_DENIED
;
3173 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
3174 conn
->connectpath
, smb_dname
,
3178 DEBUG(0, ("open_directory: Could not get share mode lock for "
3179 "%s\n", smb_fname_str_dbg(smb_dname
)));
3181 file_free(req
, fsp
);
3182 return NT_STATUS_SHARING_VIOLATION
;
3185 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
3186 access_mask
, share_access
,
3187 create_options
, &dir_existed
);
3189 if (!NT_STATUS_IS_OK(status
)) {
3192 file_free(req
, fsp
);
3196 set_share_mode(lck
, fsp
, get_current_uid(conn
),
3197 req
? req
->mid
: 0, NO_OPLOCK
);
3199 /* For directories the delete on close bit at open time seems
3200 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3201 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3202 status
= can_set_delete_on_close(fsp
, 0);
3203 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
3206 file_free(req
, fsp
);
3210 if (NT_STATUS_IS_OK(status
)) {
3211 /* Note that here we set the *inital* delete on close flag,
3212 not the regular one. The magic gets handled in close. */
3213 fsp
->initial_delete_on_close
= True
;
3224 return NT_STATUS_OK
;
3227 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
3228 struct smb_filename
*smb_dname
)
3233 status
= SMB_VFS_CREATE_FILE(
3236 0, /* root_dir_fid */
3237 smb_dname
, /* fname */
3238 FILE_READ_ATTRIBUTES
, /* access_mask */
3239 FILE_SHARE_NONE
, /* share_access */
3240 FILE_CREATE
, /* create_disposition*/
3241 FILE_DIRECTORY_FILE
, /* create_options */
3242 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
3243 0, /* oplock_request */
3244 0, /* allocation_size */
3245 0, /* private_flags */
3251 if (NT_STATUS_IS_OK(status
)) {
3252 close_file(req
, fsp
, NORMAL_CLOSE
);
3258 /****************************************************************************
3259 Receive notification that one of our open files has been renamed by another
3261 ****************************************************************************/
3263 void msg_file_was_renamed(struct messaging_context
*msg
,
3266 struct server_id server_id
,
3270 char *frm
= (char *)data
->data
;
3272 const char *sharepath
;
3273 const char *base_name
;
3274 const char *stream_name
;
3275 struct smb_filename
*smb_fname
= NULL
;
3276 size_t sp_len
, bn_len
;
3278 struct smbd_server_connection
*sconn
=
3279 talloc_get_type_abort(private_data
,
3280 struct smbd_server_connection
);
3282 if (data
->data
== NULL
3283 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3284 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3285 (int)data
->length
));
3289 /* Unpack the message. */
3290 pull_file_id_24(frm
, &id
);
3291 sharepath
= &frm
[24];
3292 sp_len
= strlen(sharepath
);
3293 base_name
= sharepath
+ sp_len
+ 1;
3294 bn_len
= strlen(base_name
);
3295 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3297 /* stream_name must always be NULL if there is no stream. */
3298 if (stream_name
[0] == '\0') {
3302 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
3303 stream_name
, NULL
, &smb_fname
);
3304 if (!NT_STATUS_IS_OK(status
)) {
3308 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3310 sharepath
, smb_fname_str_dbg(smb_fname
),
3311 file_id_string_tos(&id
)));
3313 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3314 fsp
= file_find_di_next(fsp
)) {
3315 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3317 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3318 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
3319 smb_fname_str_dbg(smb_fname
)));
3320 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3321 if (!NT_STATUS_IS_OK(status
)) {
3326 /* Now we have the complete path we can work out if this is
3327 actually within this share and adjust newname accordingly. */
3328 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3329 "not sharepath %s) "
3330 "%s from %s -> %s\n",
3331 fsp
->conn
->connectpath
,
3335 smb_fname_str_dbg(smb_fname
)));
3339 TALLOC_FREE(smb_fname
);
3344 * If a main file is opened for delete, all streams need to be checked for
3345 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3346 * If that works, delete them all by setting the delete on close and close.
3349 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3352 struct stream_struct
*stream_info
= NULL
;
3353 files_struct
**streams
= NULL
;
3355 unsigned int num_streams
= 0;
3356 TALLOC_CTX
*frame
= talloc_stackframe();
3359 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3360 &num_streams
, &stream_info
);
3362 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3363 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3364 DEBUG(10, ("no streams around\n"));
3366 return NT_STATUS_OK
;
3369 if (!NT_STATUS_IS_OK(status
)) {
3370 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3371 nt_errstr(status
)));
3375 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3378 if (num_streams
== 0) {
3380 return NT_STATUS_OK
;
3383 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3384 if (streams
== NULL
) {
3385 DEBUG(0, ("talloc failed\n"));
3386 status
= NT_STATUS_NO_MEMORY
;
3390 for (i
=0; i
<num_streams
; i
++) {
3391 struct smb_filename
*smb_fname
= NULL
;
3393 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3398 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3399 stream_info
[i
].name
,
3401 if (!NT_STATUS_IS_OK(status
)) {
3405 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3406 DEBUG(10, ("Unable to stat stream: %s\n",
3407 smb_fname_str_dbg(smb_fname
)));
3410 status
= SMB_VFS_CREATE_FILE(
3413 0, /* root_dir_fid */
3414 smb_fname
, /* fname */
3415 DELETE_ACCESS
, /* access_mask */
3416 (FILE_SHARE_READ
| /* share_access */
3417 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3418 FILE_OPEN
, /* create_disposition*/
3419 0, /* create_options */
3420 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3421 0, /* oplock_request */
3422 0, /* allocation_size */
3423 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3426 &streams
[i
], /* result */
3429 if (!NT_STATUS_IS_OK(status
)) {
3430 DEBUG(10, ("Could not open stream %s: %s\n",
3431 smb_fname_str_dbg(smb_fname
),
3432 nt_errstr(status
)));
3434 TALLOC_FREE(smb_fname
);
3437 TALLOC_FREE(smb_fname
);
3441 * don't touch the variable "status" beyond this point :-)
3444 for (i
-= 1 ; i
>= 0; i
--) {
3445 if (streams
[i
] == NULL
) {
3449 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3450 fsp_str_dbg(streams
[i
])));
3451 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3459 /*********************************************************************
3460 Create a default ACL by inheriting from the parent. If no inheritance
3461 from the parent available, don't set anything. This will leave the actual
3462 permissions the new file or directory already got from the filesystem
3463 as the NT ACL when read.
3464 *********************************************************************/
3466 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
3468 TALLOC_CTX
*frame
= talloc_stackframe();
3469 char *parent_name
= NULL
;
3470 struct security_descriptor
*parent_desc
= NULL
;
3471 NTSTATUS status
= NT_STATUS_OK
;
3472 struct security_descriptor
*psd
= NULL
;
3473 const struct dom_sid
*owner_sid
= NULL
;
3474 const struct dom_sid
*group_sid
= NULL
;
3475 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
3476 struct security_token
*token
= fsp
->conn
->session_info
->security_token
;
3477 bool inherit_owner
= lp_inherit_owner(SNUM(fsp
->conn
));
3478 bool inheritable_components
= false;
3479 bool try_builtin_administrators
= false;
3480 const struct dom_sid
*BA_U_sid
= NULL
;
3481 const struct dom_sid
*BA_G_sid
= NULL
;
3482 bool try_system
= false;
3483 const struct dom_sid
*SY_U_sid
= NULL
;
3484 const struct dom_sid
*SY_G_sid
= NULL
;
3487 if (!parent_dirname(frame
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
3489 return NT_STATUS_NO_MEMORY
;
3492 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
3494 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
3497 if (!NT_STATUS_IS_OK(status
)) {
3502 inheritable_components
= sd_has_inheritable_components(parent_desc
,
3505 if (!inheritable_components
&& !inherit_owner
) {
3507 /* Nothing to inherit and not setting owner. */
3508 return NT_STATUS_OK
;
3511 /* Create an inherited descriptor from the parent. */
3513 if (DEBUGLEVEL
>= 10) {
3514 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3515 fsp_str_dbg(fsp
) ));
3516 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
3519 /* Inherit from parent descriptor if "inherit owner" set. */
3520 if (inherit_owner
) {
3521 owner_sid
= parent_desc
->owner_sid
;
3522 group_sid
= parent_desc
->group_sid
;
3525 if (owner_sid
== NULL
) {
3526 if (security_token_has_builtin_administrators(token
)) {
3527 try_builtin_administrators
= true;
3528 } else if (security_token_is_system(token
)) {
3529 try_builtin_administrators
= true;
3534 if (group_sid
== NULL
&&
3535 token
->num_sids
== PRIMARY_GROUP_SID_INDEX
)
3537 if (security_token_is_system(token
)) {
3538 try_builtin_administrators
= true;
3543 if (try_builtin_administrators
) {
3548 ok
= sids_to_unixids(&global_sid_Builtin_Administrators
, 1, &ids
);
3552 BA_U_sid
= &global_sid_Builtin_Administrators
;
3553 BA_G_sid
= &global_sid_Builtin_Administrators
;
3556 BA_U_sid
= &global_sid_Builtin_Administrators
;
3559 BA_G_sid
= &global_sid_Builtin_Administrators
;
3572 ok
= sids_to_unixids(&global_sid_System
, 1, &ids
);
3576 SY_U_sid
= &global_sid_System
;
3577 SY_G_sid
= &global_sid_System
;
3580 SY_U_sid
= &global_sid_System
;
3583 SY_G_sid
= &global_sid_System
;
3591 if (owner_sid
== NULL
) {
3592 owner_sid
= BA_U_sid
;
3595 if (owner_sid
== NULL
) {
3596 owner_sid
= SY_U_sid
;
3599 if (group_sid
== NULL
) {
3600 group_sid
= SY_G_sid
;
3603 if (try_system
&& group_sid
== NULL
) {
3604 group_sid
= BA_G_sid
;
3607 if (owner_sid
== NULL
) {
3608 owner_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3610 if (group_sid
== NULL
) {
3611 if (token
->num_sids
== PRIMARY_GROUP_SID_INDEX
) {
3612 group_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3614 group_sid
= &token
->sids
[PRIMARY_GROUP_SID_INDEX
];
3618 status
= se_create_child_secdesc(frame
,
3625 if (!NT_STATUS_IS_OK(status
)) {
3630 /* If inheritable_components == false,
3631 se_create_child_secdesc()
3632 creates a security desriptor with a NULL dacl
3633 entry, but with SEC_DESC_DACL_PRESENT. We need
3634 to remove that flag. */
3636 if (!inheritable_components
) {
3637 security_info_sent
&= ~SECINFO_DACL
;
3638 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
3641 if (DEBUGLEVEL
>= 10) {
3642 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3643 fsp_str_dbg(fsp
) ));
3644 NDR_PRINT_DEBUG(security_descriptor
, psd
);
3647 if (inherit_owner
) {
3648 /* We need to be root to force this. */
3651 status
= SMB_VFS_FSET_NT_ACL(fsp
,
3654 if (inherit_owner
) {
3662 * Wrapper around open_file_ntcreate and open_directory
3665 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3666 struct smb_request
*req
,
3667 struct smb_filename
*smb_fname
,
3668 uint32_t access_mask
,
3669 uint32_t share_access
,
3670 uint32_t create_disposition
,
3671 uint32_t create_options
,
3672 uint32_t file_attributes
,
3673 uint32_t oplock_request
,
3674 uint64_t allocation_size
,
3675 uint32_t private_flags
,
3676 struct security_descriptor
*sd
,
3677 struct ea_list
*ea_list
,
3679 files_struct
**result
,
3682 int info
= FILE_WAS_OPENED
;
3683 files_struct
*base_fsp
= NULL
;
3684 files_struct
*fsp
= NULL
;
3687 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3688 "file_attributes = 0x%x, share_access = 0x%x, "
3689 "create_disposition = 0x%x create_options = 0x%x "
3690 "oplock_request = 0x%x private_flags = 0x%x "
3691 "ea_list = 0x%p, sd = 0x%p, "
3693 (unsigned int)access_mask
,
3694 (unsigned int)file_attributes
,
3695 (unsigned int)share_access
,
3696 (unsigned int)create_disposition
,
3697 (unsigned int)create_options
,
3698 (unsigned int)oplock_request
,
3699 (unsigned int)private_flags
,
3700 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3702 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3703 status
= NT_STATUS_NOT_SUPPORTED
;
3707 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3708 status
= NT_STATUS_INVALID_PARAMETER
;
3713 oplock_request
|= INTERNAL_OPEN_ONLY
;
3716 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3717 && (access_mask
& DELETE_ACCESS
)
3718 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3720 * We can't open a file with DELETE access if any of the
3721 * streams is open without FILE_SHARE_DELETE
3723 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3725 if (!NT_STATUS_IS_OK(status
)) {
3730 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3731 !security_token_has_privilege(get_current_nttok(conn
),
3732 SEC_PRIV_SECURITY
)) {
3733 DEBUG(10, ("create_file_unixpath: open on %s "
3734 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3735 smb_fname_str_dbg(smb_fname
)));
3736 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3740 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3741 && is_ntfs_stream_smb_fname(smb_fname
)
3742 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3743 uint32 base_create_disposition
;
3744 struct smb_filename
*smb_fname_base
= NULL
;
3746 if (create_options
& FILE_DIRECTORY_FILE
) {
3747 status
= NT_STATUS_NOT_A_DIRECTORY
;
3751 switch (create_disposition
) {
3753 base_create_disposition
= FILE_OPEN
;
3756 base_create_disposition
= FILE_OPEN_IF
;
3760 /* Create an smb_filename with stream_name == NULL. */
3761 status
= create_synthetic_smb_fname(talloc_tos(),
3762 smb_fname
->base_name
,
3765 if (!NT_STATUS_IS_OK(status
)) {
3769 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3770 DEBUG(10, ("Unable to stat stream: %s\n",
3771 smb_fname_str_dbg(smb_fname_base
)));
3774 * https://bugzilla.samba.org/show_bug.cgi?id=10229
3775 * We need to check if the requested access mask
3776 * could be used to open the underlying file (if
3777 * it existed), as we're passing in zero for the
3778 * access mask to the base filename.
3780 status
= check_base_file_access(conn
,
3784 if (!NT_STATUS_IS_OK(status
)) {
3785 DEBUG(10, ("Permission check "
3786 "for base %s failed: "
3787 "%s\n", smb_fname
->base_name
,
3788 nt_errstr(status
)));
3793 /* Open the base file. */
3794 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3797 | FILE_SHARE_DELETE
,
3798 base_create_disposition
,
3799 0, 0, 0, 0, 0, NULL
, NULL
,
3801 TALLOC_FREE(smb_fname_base
);
3803 if (!NT_STATUS_IS_OK(status
)) {
3804 DEBUG(10, ("create_file_unixpath for base %s failed: "
3805 "%s\n", smb_fname
->base_name
,
3806 nt_errstr(status
)));
3809 /* we don't need to low level fd */
3814 * If it's a request for a directory open, deal with it separately.
3817 if (create_options
& FILE_DIRECTORY_FILE
) {
3819 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3820 status
= NT_STATUS_INVALID_PARAMETER
;
3824 /* Can't open a temp directory. IFS kit test. */
3825 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3826 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3827 status
= NT_STATUS_INVALID_PARAMETER
;
3832 * We will get a create directory here if the Win32
3833 * app specified a security descriptor in the
3834 * CreateDirectory() call.
3838 status
= open_directory(
3839 conn
, req
, smb_fname
, access_mask
, share_access
,
3840 create_disposition
, create_options
, file_attributes
,
3845 * Ordinary file case.
3848 status
= file_new(req
, conn
, &fsp
);
3849 if(!NT_STATUS_IS_OK(status
)) {
3853 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3854 if (!NT_STATUS_IS_OK(status
)) {
3860 * We're opening the stream element of a
3861 * base_fsp we already opened. Set up the
3864 fsp
->base_fsp
= base_fsp
;
3867 if (allocation_size
) {
3868 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
3872 status
= open_file_ntcreate(conn
,
3884 if(!NT_STATUS_IS_OK(status
)) {
3885 file_free(req
, fsp
);
3889 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3891 /* A stream open never opens a directory */
3894 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3899 * Fail the open if it was explicitly a non-directory
3903 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3904 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3909 status
= open_directory(
3910 conn
, req
, smb_fname
, access_mask
,
3911 share_access
, create_disposition
,
3912 create_options
, file_attributes
,
3917 if (!NT_STATUS_IS_OK(status
)) {
3921 fsp
->base_fsp
= base_fsp
;
3923 if ((ea_list
!= NULL
) &&
3924 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3925 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3926 if (!NT_STATUS_IS_OK(status
)) {
3931 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3932 status
= NT_STATUS_ACCESS_DENIED
;
3936 /* Save the requested allocation size. */
3937 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3939 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3940 fsp
->initial_allocation_size
= smb_roundup(
3941 fsp
->conn
, allocation_size
);
3942 if (fsp
->is_directory
) {
3943 /* Can't set allocation size on a directory. */
3944 status
= NT_STATUS_ACCESS_DENIED
;
3947 if (vfs_allocate_file_space(
3948 fsp
, fsp
->initial_allocation_size
) == -1) {
3949 status
= NT_STATUS_DISK_FULL
;
3953 fsp
->initial_allocation_size
= smb_roundup(
3954 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3957 fsp
->initial_allocation_size
= 0;
3960 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
3961 fsp
->base_fsp
== NULL
) {
3964 * According to the MS documentation, the only time the security
3965 * descriptor is applied to the opened file is iff we *created* the
3966 * file; an existing file stays the same.
3968 * Also, it seems (from observation) that you can open the file with
3969 * any access mask but you can still write the sd. We need to override
3970 * the granted access before we call set_sd
3971 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3974 uint32_t sec_info_sent
;
3975 uint32_t saved_access_mask
= fsp
->access_mask
;
3977 sec_info_sent
= get_sec_info(sd
);
3979 fsp
->access_mask
= FILE_GENERIC_ALL
;
3981 if (sec_info_sent
& (SECINFO_OWNER
|
3985 status
= set_sd(fsp
, sd
, sec_info_sent
);
3988 fsp
->access_mask
= saved_access_mask
;
3990 if (!NT_STATUS_IS_OK(status
)) {
3993 } else if (lp_inherit_acls(SNUM(conn
))) {
3994 /* Inherit from parent. Errors here are not fatal. */
3995 status
= inherit_new_acl(fsp
);
3996 if (!NT_STATUS_IS_OK(status
)) {
3997 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3999 nt_errstr(status
) ));
4004 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
4007 if (pinfo
!= NULL
) {
4011 smb_fname
->st
= fsp
->fsp_name
->st
;
4013 return NT_STATUS_OK
;
4016 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
4019 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
4021 * The close_file below will close
4026 close_file(req
, fsp
, ERROR_CLOSE
);
4029 if (base_fsp
!= NULL
) {
4030 close_file(req
, base_fsp
, ERROR_CLOSE
);
4037 * Calculate the full path name given a relative fid.
4039 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
4040 struct smb_request
*req
,
4041 uint16_t root_dir_fid
,
4042 const struct smb_filename
*smb_fname
,
4043 struct smb_filename
**smb_fname_out
)
4045 files_struct
*dir_fsp
;
4046 char *parent_fname
= NULL
;
4047 char *new_base_name
= NULL
;
4050 if (root_dir_fid
== 0 || !smb_fname
) {
4051 status
= NT_STATUS_INTERNAL_ERROR
;
4055 dir_fsp
= file_fsp(req
, root_dir_fid
);
4057 if (dir_fsp
== NULL
) {
4058 status
= NT_STATUS_INVALID_HANDLE
;
4062 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
4063 status
= NT_STATUS_INVALID_HANDLE
;
4067 if (!dir_fsp
->is_directory
) {
4070 * Check to see if this is a mac fork of some kind.
4073 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
4074 is_ntfs_stream_smb_fname(smb_fname
)) {
4075 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4080 we need to handle the case when we get a
4081 relative open relative to a file and the
4082 pathname is blank - this is a reopen!
4083 (hint from demyn plantenberg)
4086 status
= NT_STATUS_INVALID_HANDLE
;
4090 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
4092 * We're at the toplevel dir, the final file name
4093 * must not contain ./, as this is filtered out
4094 * normally by srvstr_get_path and unix_convert
4095 * explicitly rejects paths containing ./.
4097 parent_fname
= talloc_strdup(talloc_tos(), "");
4098 if (parent_fname
== NULL
) {
4099 status
= NT_STATUS_NO_MEMORY
;
4103 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
4106 * Copy in the base directory name.
4109 parent_fname
= talloc_array(talloc_tos(), char,
4111 if (parent_fname
== NULL
) {
4112 status
= NT_STATUS_NO_MEMORY
;
4115 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
4119 * Ensure it ends in a '/'.
4120 * We used TALLOC_SIZE +2 to add space for the '/'.
4124 && (parent_fname
[dir_name_len
-1] != '\\')
4125 && (parent_fname
[dir_name_len
-1] != '/')) {
4126 parent_fname
[dir_name_len
] = '/';
4127 parent_fname
[dir_name_len
+1] = '\0';
4131 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
4132 smb_fname
->base_name
);
4133 if (new_base_name
== NULL
) {
4134 status
= NT_STATUS_NO_MEMORY
;
4138 status
= filename_convert(req
,
4140 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
4145 if (!NT_STATUS_IS_OK(status
)) {
4150 TALLOC_FREE(parent_fname
);
4151 TALLOC_FREE(new_base_name
);
4155 NTSTATUS
create_file_default(connection_struct
*conn
,
4156 struct smb_request
*req
,
4157 uint16_t root_dir_fid
,
4158 struct smb_filename
*smb_fname
,
4159 uint32_t access_mask
,
4160 uint32_t share_access
,
4161 uint32_t create_disposition
,
4162 uint32_t create_options
,
4163 uint32_t file_attributes
,
4164 uint32_t oplock_request
,
4165 uint64_t allocation_size
,
4166 uint32_t private_flags
,
4167 struct security_descriptor
*sd
,
4168 struct ea_list
*ea_list
,
4169 files_struct
**result
,
4172 int info
= FILE_WAS_OPENED
;
4173 files_struct
*fsp
= NULL
;
4175 bool stream_name
= false;
4177 DEBUG(10,("create_file: access_mask = 0x%x "
4178 "file_attributes = 0x%x, share_access = 0x%x, "
4179 "create_disposition = 0x%x create_options = 0x%x "
4180 "oplock_request = 0x%x "
4181 "private_flags = 0x%x "
4182 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4184 (unsigned int)access_mask
,
4185 (unsigned int)file_attributes
,
4186 (unsigned int)share_access
,
4187 (unsigned int)create_disposition
,
4188 (unsigned int)create_options
,
4189 (unsigned int)oplock_request
,
4190 (unsigned int)private_flags
,
4191 (unsigned int)root_dir_fid
,
4192 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4195 * Calculate the filename from the root_dir_if if necessary.
4198 if (root_dir_fid
!= 0) {
4199 struct smb_filename
*smb_fname_out
= NULL
;
4200 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
4201 smb_fname
, &smb_fname_out
);
4202 if (!NT_STATUS_IS_OK(status
)) {
4205 smb_fname
= smb_fname_out
;
4209 * Check to see if this is a mac fork of some kind.
4212 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
4214 enum FAKE_FILE_TYPE fake_file_type
;
4216 fake_file_type
= is_fake_file(smb_fname
);
4218 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
4221 * Here we go! support for changing the disk quotas
4224 * We need to fake up to open this MAGIC QUOTA file
4225 * and return a valid FID.
4227 * w2k close this file directly after openening xp
4228 * also tries a QUERY_FILE_INFO on the file and then
4231 status
= open_fake_file(req
, conn
, req
->vuid
,
4232 fake_file_type
, smb_fname
,
4234 if (!NT_STATUS_IS_OK(status
)) {
4238 ZERO_STRUCT(smb_fname
->st
);
4242 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
4243 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4248 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
4250 smb_fname
->stream_name
= NULL
;
4251 /* We have to handle this error here. */
4252 if (create_options
& FILE_DIRECTORY_FILE
) {
4253 status
= NT_STATUS_NOT_A_DIRECTORY
;
4256 if (lp_posix_pathnames()) {
4257 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
4259 ret
= SMB_VFS_STAT(conn
, smb_fname
);
4262 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
4263 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4268 status
= create_file_unixpath(
4269 conn
, req
, smb_fname
, access_mask
, share_access
,
4270 create_disposition
, create_options
, file_attributes
,
4271 oplock_request
, allocation_size
, private_flags
,
4275 if (!NT_STATUS_IS_OK(status
)) {
4280 DEBUG(10, ("create_file: info=%d\n", info
));
4283 if (pinfo
!= NULL
) {
4286 return NT_STATUS_OK
;
4289 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
4292 close_file(req
, fsp
, ERROR_CLOSE
);