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
,
73 /* Check if we have rights to open. */
75 struct security_descriptor
*sd
= NULL
;
76 uint32_t rejected_share_access
;
77 uint32_t rejected_mask
= access_mask
;
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 (!use_privs
&& 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 status
= se_file_access_check(sd
,
146 get_current_nttok(conn
),
148 (access_mask
& ~FILE_READ_ATTRIBUTES
),
151 DEBUG(10,("smbd_check_access_rights: file %s requesting "
152 "0x%x returning 0x%x (%s)\n",
153 smb_fname_str_dbg(smb_fname
),
154 (unsigned int)access_mask
,
155 (unsigned int)rejected_mask
,
156 nt_errstr(status
) ));
158 if (!NT_STATUS_IS_OK(status
)) {
159 if (DEBUGLEVEL
>= 10) {
160 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
161 smb_fname_str_dbg(smb_fname
) ));
162 NDR_PRINT_DEBUG(security_descriptor
, sd
);
168 if (NT_STATUS_IS_OK(status
) ||
169 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
173 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
177 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
178 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
179 !lp_store_dos_attributes(SNUM(conn
)) &&
180 (lp_map_readonly(SNUM(conn
)) ||
181 lp_map_archive(SNUM(conn
)) ||
182 lp_map_hidden(SNUM(conn
)) ||
183 lp_map_system(SNUM(conn
)))) {
184 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
186 DEBUG(10,("smbd_check_access_rights: "
188 "FILE_WRITE_ATTRIBUTES "
190 smb_fname_str_dbg(smb_fname
)));
193 if (parent_override_delete(conn
,
197 /* Were we trying to do an open
198 * for delete and didn't get DELETE
199 * access (only) ? Check if the
200 * directory allows DELETE_CHILD.
202 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
205 rejected_mask
&= ~DELETE_ACCESS
;
207 DEBUG(10,("smbd_check_access_rights: "
211 smb_fname_str_dbg(smb_fname
)));
214 if (rejected_mask
!= 0) {
215 return NT_STATUS_ACCESS_DENIED
;
220 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
221 struct smb_filename
*smb_fname
,
222 uint32_t access_mask
)
225 char *parent_dir
= NULL
;
226 struct security_descriptor
*parent_sd
= NULL
;
227 uint32_t access_granted
= 0;
229 if (!parent_dirname(talloc_tos(),
230 smb_fname
->base_name
,
233 return NT_STATUS_NO_MEMORY
;
236 if (get_current_uid(conn
) == (uid_t
)0) {
237 /* I'm sorry sir, I didn't know you were root... */
238 DEBUG(10,("check_parent_access: root override "
239 "on %s. Granting 0x%x\n",
240 smb_fname_str_dbg(smb_fname
),
241 (unsigned int)access_mask
));
245 status
= SMB_VFS_GET_NT_ACL(conn
,
251 if (!NT_STATUS_IS_OK(status
)) {
252 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
253 "%s with error %s\n",
260 * If we can access the path to this file, by
261 * default we have FILE_READ_ATTRIBUTES from the
262 * containing directory. See the section:
263 * "Algorithm to Check Access to an Existing File"
266 * se_file_access_check() also takes care of
267 * owner WRITE_DAC and READ_CONTROL.
269 status
= se_file_access_check(parent_sd
,
270 get_current_nttok(conn
),
272 (access_mask
& ~FILE_READ_ATTRIBUTES
),
274 if(!NT_STATUS_IS_OK(status
)) {
275 DEBUG(5,("check_parent_access: access check "
276 "on directory %s for "
277 "path %s for mask 0x%x returned (0x%x) %s\n",
279 smb_fname
->base_name
,
282 nt_errstr(status
) ));
289 /****************************************************************************
290 fd support routines - attempt to do a dos_open.
291 ****************************************************************************/
293 NTSTATUS
fd_open(struct connection_struct
*conn
,
298 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
299 NTSTATUS status
= NT_STATUS_OK
;
303 * Never follow symlinks on a POSIX client. The
304 * client should be doing this.
307 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
312 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
313 if (fsp
->fh
->fd
== -1) {
314 int posix_errno
= errno
;
316 #if defined(ENOTSUP) && defined(OSF1)
317 /* handle special Tru64 errno */
318 if (errno
== ENOTSUP
) {
323 /* fix broken NetBSD errno */
324 if (errno
== EFTYPE
) {
328 /* fix broken FreeBSD errno */
329 if (errno
== EMLINK
) {
332 #endif /* O_NOFOLLOW */
333 status
= map_nt_error_from_unix(posix_errno
);
334 if (errno
== EMFILE
) {
335 static time_t last_warned
= 0L;
337 if (time((time_t *) NULL
) > last_warned
) {
338 DEBUG(0,("Too many open files, unable "
339 "to open more! smbd's max "
341 lp_max_open_files()));
342 last_warned
= time((time_t *) NULL
);
348 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
349 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
350 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
355 /****************************************************************************
356 Close the file associated with a fsp.
357 ****************************************************************************/
359 NTSTATUS
fd_close(files_struct
*fsp
)
366 if (fsp
->fh
->fd
== -1) {
367 return NT_STATUS_OK
; /* What we used to call a stat open. */
369 if (fsp
->fh
->ref_count
> 1) {
370 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
373 ret
= SMB_VFS_CLOSE(fsp
);
376 return map_nt_error_from_unix(errno
);
381 /****************************************************************************
382 Change the ownership of a file to that of the parent directory.
383 Do this by fd if possible.
384 ****************************************************************************/
386 void change_file_owner_to_parent(connection_struct
*conn
,
387 const char *inherit_from_dir
,
390 struct smb_filename
*smb_fname_parent
;
393 smb_fname_parent
= synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
395 if (smb_fname_parent
== NULL
) {
399 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
401 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
402 "directory %s. Error was %s\n",
403 smb_fname_str_dbg(smb_fname_parent
),
405 TALLOC_FREE(smb_fname_parent
);
409 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
410 /* Already this uid - no need to change. */
411 DEBUG(10,("change_file_owner_to_parent: file %s "
412 "is already owned by uid %d\n",
414 (int)fsp
->fsp_name
->st
.st_ex_uid
));
415 TALLOC_FREE(smb_fname_parent
);
420 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
423 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
424 "file %s to parent directory uid %u. Error "
425 "was %s\n", fsp_str_dbg(fsp
),
426 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
429 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
430 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
431 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
432 /* Ensure the uid entry is updated. */
433 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
436 TALLOC_FREE(smb_fname_parent
);
439 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
440 const char *inherit_from_dir
,
442 SMB_STRUCT_STAT
*psbuf
)
444 struct smb_filename
*smb_fname_parent
;
445 struct smb_filename
*smb_fname_cwd
= NULL
;
446 char *saved_dir
= NULL
;
447 TALLOC_CTX
*ctx
= talloc_tos();
448 NTSTATUS status
= NT_STATUS_OK
;
451 smb_fname_parent
= synthetic_smb_fname(ctx
, inherit_from_dir
,
453 if (smb_fname_parent
== NULL
) {
454 return NT_STATUS_NO_MEMORY
;
457 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
459 status
= map_nt_error_from_unix(errno
);
460 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
461 "directory %s. Error was %s\n",
462 smb_fname_str_dbg(smb_fname_parent
),
467 /* We've already done an lstat into psbuf, and we know it's a
468 directory. If we can cd into the directory and the dev/ino
469 are the same then we can safely chown without races as
470 we're locking the directory in place by being in it. This
471 should work on any UNIX (thanks tridge :-). JRA.
474 saved_dir
= vfs_GetWd(ctx
,conn
);
476 status
= map_nt_error_from_unix(errno
);
477 DEBUG(0,("change_dir_owner_to_parent: failed to get "
478 "current working directory. Error was %s\n",
483 /* Chdir into the new path. */
484 if (vfs_ChDir(conn
, fname
) == -1) {
485 status
= map_nt_error_from_unix(errno
);
486 DEBUG(0,("change_dir_owner_to_parent: failed to change "
487 "current working directory to %s. Error "
488 "was %s\n", fname
, strerror(errno
) ));
492 smb_fname_cwd
= synthetic_smb_fname(ctx
, ".", NULL
, NULL
);
493 if (smb_fname_cwd
== NULL
) {
494 status
= NT_STATUS_NO_MEMORY
;
498 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
500 status
= map_nt_error_from_unix(errno
);
501 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
502 "directory '.' (%s) Error was %s\n",
503 fname
, strerror(errno
)));
507 /* Ensure we're pointing at the same place. */
508 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
509 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
510 DEBUG(0,("change_dir_owner_to_parent: "
511 "device/inode on directory %s changed. "
512 "Refusing to chown !\n", fname
));
513 status
= NT_STATUS_ACCESS_DENIED
;
517 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
518 /* Already this uid - no need to change. */
519 DEBUG(10,("change_dir_owner_to_parent: directory %s "
520 "is already owned by uid %d\n",
522 (int)smb_fname_cwd
->st
.st_ex_uid
));
523 status
= NT_STATUS_OK
;
528 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
532 status
= map_nt_error_from_unix(errno
);
533 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
534 "directory %s to parent directory uid %u. "
535 "Error was %s\n", fname
,
536 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
539 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
540 "directory %s to parent directory uid %u.\n",
541 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
542 /* Ensure the uid entry is updated. */
543 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
547 vfs_ChDir(conn
,saved_dir
);
549 TALLOC_FREE(smb_fname_parent
);
550 TALLOC_FREE(smb_fname_cwd
);
554 /****************************************************************************
555 Open a file - returning a guaranteed ATOMIC indication of if the
556 file was created or not.
557 ****************************************************************************/
559 static NTSTATUS
fd_open_atomic(struct connection_struct
*conn
,
565 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
566 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
568 *file_created
= false;
570 if (!(flags
& O_CREAT
)) {
572 * We're not creating the file, just pass through.
574 return fd_open(conn
, fsp
, flags
, mode
);
577 if (flags
& O_EXCL
) {
579 * Fail if already exists, just pass through.
581 status
= fd_open(conn
, fsp
, flags
, mode
);
584 * Here we've opened with O_CREAT|O_EXCL. If that went
585 * NT_STATUS_OK, we *know* we created this file.
587 *file_created
= NT_STATUS_IS_OK(status
);
593 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
594 * To know absolutely if we created the file or not,
595 * we can never call O_CREAT without O_EXCL. So if
596 * we think the file existed, try without O_CREAT|O_EXCL.
597 * If we think the file didn't exist, try with
598 * O_CREAT|O_EXCL. Keep bouncing between these two
599 * requests until either the file is created, or
600 * opened. Either way, we keep going until we get
601 * a returnable result (error, or open/create).
605 int curr_flags
= flags
;
608 /* Just try open, do not create. */
609 curr_flags
&= ~(O_CREAT
);
610 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
611 if (NT_STATUS_EQUAL(status
,
612 NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
614 * Someone deleted it in the meantime.
617 file_existed
= false;
618 DEBUG(10,("fd_open_atomic: file %s existed. "
620 smb_fname_str_dbg(fsp
->fsp_name
)));
624 /* Try create exclusively, fail if it exists. */
625 curr_flags
|= O_EXCL
;
626 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
627 if (NT_STATUS_EQUAL(status
,
628 NT_STATUS_OBJECT_NAME_COLLISION
)) {
630 * Someone created it in the meantime.
631 * Retry without O_CREAT.
634 DEBUG(10,("fd_open_atomic: file %s "
635 "did not exist. Retry.\n",
636 smb_fname_str_dbg(fsp
->fsp_name
)));
639 if (NT_STATUS_IS_OK(status
)) {
641 * Here we've opened with O_CREAT|O_EXCL
642 * and got success. We *know* we created
645 *file_created
= true;
648 /* Create is done, or failed. */
654 /****************************************************************************
656 ****************************************************************************/
658 static NTSTATUS
open_file(files_struct
*fsp
,
659 connection_struct
*conn
,
660 struct smb_request
*req
,
661 const char *parent_dir
,
664 uint32 access_mask
, /* client requested access mask. */
665 uint32 open_access_mask
, /* what we're actually using in the open. */
666 bool *p_file_created
)
668 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
669 NTSTATUS status
= NT_STATUS_OK
;
670 int accmode
= (flags
& O_ACCMODE
);
671 int local_flags
= flags
;
672 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
677 /* Check permissions */
680 * This code was changed after seeing a client open request
681 * containing the open mode of (DENY_WRITE/read-only) with
682 * the 'create if not exist' bit set. The previous code
683 * would fail to open the file read only on a read-only share
684 * as it was checking the flags parameter directly against O_RDONLY,
685 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
689 if (!CAN_WRITE(conn
)) {
690 /* It's a read-only share - fail if we wanted to write. */
691 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
692 DEBUG(3,("Permission denied opening %s\n",
693 smb_fname_str_dbg(smb_fname
)));
694 return NT_STATUS_ACCESS_DENIED
;
696 if (flags
& O_CREAT
) {
697 /* We don't want to write - but we must make sure that
698 O_CREAT doesn't create the file if we have write
699 access into the directory.
701 flags
&= ~(O_CREAT
|O_EXCL
);
702 local_flags
&= ~(O_CREAT
|O_EXCL
);
707 * This little piece of insanity is inspired by the
708 * fact that an NT client can open a file for O_RDONLY,
709 * but set the create disposition to FILE_EXISTS_TRUNCATE.
710 * If the client *can* write to the file, then it expects to
711 * truncate the file, even though it is opening for readonly.
712 * Quicken uses this stupid trick in backup file creation...
713 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
714 * for helping track this one down. It didn't bite us in 2.0.x
715 * as we always opened files read-write in that release. JRA.
718 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
719 DEBUG(10,("open_file: truncate requested on read-only open "
720 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
721 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
724 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
725 (!file_existed
&& (local_flags
& O_CREAT
)) ||
726 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
730 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
732 * We would block on opening a FIFO with no one else on the
733 * other end. Do what we used to do and add O_NONBLOCK to the
737 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
738 local_flags
&= ~O_TRUNC
; /* Can't truncate a FIFO. */
739 local_flags
|= O_NONBLOCK
;
743 /* Don't create files with Microsoft wildcard characters. */
746 * wildcard characters are allowed in stream names
747 * only test the basefilename
749 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
751 wild
= smb_fname
->base_name
;
753 if ((local_flags
& O_CREAT
) && !file_existed
&&
755 return NT_STATUS_OBJECT_NAME_INVALID
;
758 /* Can we access this file ? */
759 if (!fsp
->base_fsp
) {
760 /* Only do this check on non-stream open. */
762 status
= smbd_check_access_rights(conn
,
766 } else if (local_flags
& O_CREAT
){
767 status
= check_parent_access(conn
,
771 /* File didn't exist and no O_CREAT. */
772 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
774 if (!NT_STATUS_IS_OK(status
)) {
775 DEBUG(10,("open_file: "
779 "smbd_check_access_rights" :
780 "check_parent_access",
781 smb_fname_str_dbg(smb_fname
),
782 nt_errstr(status
) ));
787 /* Actually do the open */
788 status
= fd_open_atomic(conn
, fsp
, local_flags
,
789 unx_mode
, p_file_created
);
790 if (!NT_STATUS_IS_OK(status
)) {
791 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
792 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
793 nt_errstr(status
),local_flags
,flags
));
797 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
799 /* If we have an fd, this stat should succeed. */
800 DEBUG(0,("Error doing fstat on open file %s "
802 smb_fname_str_dbg(smb_fname
),
804 status
= map_nt_error_from_unix(errno
);
809 if (*p_file_created
) {
810 /* We created this file. */
812 bool need_re_stat
= false;
813 /* Do all inheritance work after we've
814 done a successful fstat call and filled
815 in the stat struct in fsp->fsp_name. */
817 /* Inherit the ACL if required */
818 if (lp_inherit_perms(SNUM(conn
))) {
819 inherit_access_posix_acl(conn
, parent_dir
,
820 smb_fname
->base_name
,
825 /* Change the owner if required. */
826 if (lp_inherit_owner(SNUM(conn
))) {
827 change_file_owner_to_parent(conn
, parent_dir
,
833 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
834 /* If we have an fd, this stat should succeed. */
836 DEBUG(0,("Error doing fstat on open file %s "
838 smb_fname_str_dbg(smb_fname
),
843 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
844 FILE_NOTIFY_CHANGE_FILE_NAME
,
845 smb_fname
->base_name
);
848 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
850 /* File must exist for a stat open. */
851 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
854 status
= smbd_check_access_rights(conn
,
859 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
861 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
862 /* This is a POSIX stat open for delete
863 * or rename on a symlink that points
865 DEBUG(10,("open_file: allowing POSIX "
866 "open on bad symlink %s\n",
867 smb_fname_str_dbg(smb_fname
)));
868 status
= NT_STATUS_OK
;
871 if (!NT_STATUS_IS_OK(status
)) {
872 DEBUG(10,("open_file: "
873 "smbd_check_access_rights on file "
875 smb_fname_str_dbg(smb_fname
),
876 nt_errstr(status
) ));
882 * POSIX allows read-only opens of directories. We don't
883 * want to do this (we use a different code path for this)
884 * so catch a directory open and return an EISDIR. JRA.
887 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
890 return NT_STATUS_FILE_IS_A_DIRECTORY
;
893 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
894 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
895 fsp
->file_pid
= req
? req
->smbpid
: 0;
896 fsp
->can_lock
= True
;
897 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
900 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
901 fsp
->print_file
= NULL
;
902 fsp
->modified
= False
;
903 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
904 fsp
->is_directory
= False
;
905 if (conn
->aio_write_behind_list
&&
906 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
907 conn
->case_sensitive
)) {
908 fsp
->aio_write_behind
= True
;
911 fsp
->wcp
= NULL
; /* Write cache pointer. */
913 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
914 conn
->session_info
->unix_info
->unix_name
,
915 smb_fname_str_dbg(smb_fname
),
916 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
917 conn
->num_files_open
));
923 /****************************************************************************
924 Check if we can open a file with a share mode.
925 Returns True if conflict, False if not.
926 ****************************************************************************/
928 static bool share_conflict(struct share_mode_entry
*entry
,
932 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
933 "entry->share_access = 0x%x, "
934 "entry->private_options = 0x%x\n",
935 (unsigned int)entry
->access_mask
,
936 (unsigned int)entry
->share_access
,
937 (unsigned int)entry
->private_options
));
939 if (server_id_is_disconnected(&entry
->pid
)) {
941 * note: cleanup should have been done by
942 * delay_for_batch_oplocks()
947 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
948 (unsigned int)access_mask
, (unsigned int)share_access
));
950 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
954 DELETE_ACCESS
)) == 0) {
955 DEBUG(10,("share_conflict: No conflict due to "
956 "entry->access_mask = 0x%x\n",
957 (unsigned int)entry
->access_mask
));
961 if ((access_mask
& (FILE_WRITE_DATA
|
965 DELETE_ACCESS
)) == 0) {
966 DEBUG(10,("share_conflict: No conflict due to "
967 "access_mask = 0x%x\n",
968 (unsigned int)access_mask
));
972 #if 1 /* JRA TEST - Superdebug. */
973 #define CHECK_MASK(num, am, right, sa, share) \
974 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
975 (unsigned int)(num), (unsigned int)(am), \
976 (unsigned int)(right), (unsigned int)(am)&(right) )); \
977 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
978 (unsigned int)(num), (unsigned int)(sa), \
979 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
980 if (((am) & (right)) && !((sa) & (share))) { \
981 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
982 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
983 (unsigned int)(share) )); \
987 #define CHECK_MASK(num, am, right, sa, share) \
988 if (((am) & (right)) && !((sa) & (share))) { \
989 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
990 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
991 (unsigned int)(share) )); \
996 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
997 share_access
, FILE_SHARE_WRITE
);
998 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
999 entry
->share_access
, FILE_SHARE_WRITE
);
1001 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1002 share_access
, FILE_SHARE_READ
);
1003 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
1004 entry
->share_access
, FILE_SHARE_READ
);
1006 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
1007 share_access
, FILE_SHARE_DELETE
);
1008 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
1009 entry
->share_access
, FILE_SHARE_DELETE
);
1011 DEBUG(10,("share_conflict: No conflict.\n"));
1015 #if defined(DEVELOPER)
1016 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
1018 struct share_mode_entry
*share_entry
)
1020 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1023 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1027 if (is_deferred_open_entry(share_entry
) &&
1028 !open_was_deferred(sconn
, share_entry
->op_mid
))
1030 char *str
= talloc_asprintf(talloc_tos(),
1031 "Got a deferred entry without a request: "
1033 share_mode_str(talloc_tos(), num
, share_entry
));
1037 if (!is_valid_share_mode_entry(share_entry
)) {
1041 fsp
= file_find_dif(sconn
, share_entry
->id
,
1042 share_entry
->share_file_id
);
1044 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1045 share_mode_str(talloc_tos(), num
, share_entry
) ));
1046 smb_panic("validate_my_share_entries: Cannot match a "
1047 "share entry with an open file\n");
1050 if (is_deferred_open_entry(share_entry
)) {
1054 if ((share_entry
->op_type
== NO_OPLOCK
) &&
1055 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
))
1057 /* Someone has already written to it, but I haven't yet
1062 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
1071 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1072 share_mode_str(talloc_tos(), num
, share_entry
) ));
1073 str
= talloc_asprintf(talloc_tos(),
1074 "validate_my_share_entries: "
1075 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1076 fsp
->fsp_name
->base_name
,
1077 (unsigned int)fsp
->oplock_type
,
1078 (unsigned int)share_entry
->op_type
);
1084 bool is_stat_open(uint32 access_mask
)
1086 const uint32_t stat_open_bits
=
1087 (SYNCHRONIZE_ACCESS
|
1088 FILE_READ_ATTRIBUTES
|
1089 FILE_WRITE_ATTRIBUTES
);
1091 return (((access_mask
& stat_open_bits
) != 0) &&
1092 ((access_mask
& ~stat_open_bits
) == 0));
1095 /****************************************************************************
1096 Deal with share modes
1097 Invarient: Share mode must be locked on entry and exit.
1098 Returns -1 on error, or number of share modes on success (may be zero).
1099 ****************************************************************************/
1101 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1102 struct share_mode_lock
*lck
,
1105 uint32 share_access
,
1106 uint32 create_options
,
1111 if(lck
->data
->num_share_modes
== 0) {
1112 return NT_STATUS_OK
;
1115 /* A delete on close prohibits everything */
1117 if (is_delete_on_close_set(lck
, name_hash
)) {
1119 * Check the delete on close token
1120 * is valid. It could have been left
1121 * after a server crash.
1123 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1124 if (!share_mode_stale_pid(lck
->data
, i
)) {
1126 *file_existed
= true;
1128 return NT_STATUS_DELETE_PENDING
;
1131 return NT_STATUS_OK
;
1134 if (is_stat_open(access_mask
)) {
1135 /* Stat open that doesn't trigger oplock breaks or share mode
1136 * checks... ! JRA. */
1137 return NT_STATUS_OK
;
1141 * Check if the share modes will give us access.
1144 #if defined(DEVELOPER)
1145 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1146 validate_my_share_entries(conn
->sconn
, i
,
1147 &lck
->data
->share_modes
[i
]);
1151 /* Now we check the share modes, after any oplock breaks. */
1152 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1154 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1158 /* someone else has a share lock on it, check to see if we can
1160 if (share_conflict(&lck
->data
->share_modes
[i
],
1161 access_mask
, share_access
)) {
1163 if (share_mode_stale_pid(lck
->data
, i
)) {
1167 *file_existed
= true;
1169 return NT_STATUS_SHARING_VIOLATION
;
1173 if (lck
->data
->num_share_modes
!= 0) {
1174 *file_existed
= true;
1177 return NT_STATUS_OK
;
1181 * Send a break message to the oplock holder and delay the open for
1185 static NTSTATUS
send_break_message(files_struct
*fsp
,
1186 struct share_mode_entry
*exclusive
,
1191 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1193 DEBUG(10, ("Sending break request to PID %s\n",
1194 procid_str_static(&exclusive
->pid
)));
1195 exclusive
->op_mid
= mid
;
1197 /* Create the message. */
1198 share_mode_entry_to_message(msg
, exclusive
);
1200 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1201 don't want this set in the share mode struct pointed to by lck. */
1203 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
1204 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
1205 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
1208 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
1209 MSG_SMB_BREAK_REQUEST
,
1211 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
1212 if (!NT_STATUS_IS_OK(status
)) {
1213 DEBUG(3, ("Could not send oplock break message: %s\n",
1214 nt_errstr(status
)));
1221 * Return share_mode_entry pointers for :
1222 * 1). Batch oplock entry.
1223 * 2). Batch or exclusive oplock entry (may be identical to #1).
1224 * bool have_level2_oplock
1225 * bool have_no_oplock.
1226 * Do internal consistency checks on the share mode for a file.
1229 static void find_oplock_types(files_struct
*fsp
,
1231 const struct share_mode_lock
*lck
,
1232 struct share_mode_entry
**pp_batch
,
1233 struct share_mode_entry
**pp_ex_or_batch
,
1235 bool *got_no_oplock
)
1240 *pp_ex_or_batch
= NULL
;
1241 *got_level2
= false;
1242 *got_no_oplock
= false;
1244 /* Ignore stat or internal opens, as is done in
1245 delay_for_batch_oplocks() and
1246 delay_for_exclusive_oplocks().
1248 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1252 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1253 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1257 if (lck
->data
->share_modes
[i
].op_type
== NO_OPLOCK
&&
1258 is_stat_open(lck
->data
->share_modes
[i
].access_mask
)) {
1259 /* We ignore stat opens in the table - they
1260 always have NO_OPLOCK and never get or
1261 cause breaks. JRA. */
1265 if (BATCH_OPLOCK_TYPE(lck
->data
->share_modes
[i
].op_type
)) {
1266 /* batch - can only be one. */
1267 if (share_mode_stale_pid(lck
->data
, i
)) {
1268 DEBUG(10, ("Found stale batch oplock\n"));
1271 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
1272 smb_panic("Bad batch oplock entry.");
1274 *pp_batch
= &lck
->data
->share_modes
[i
];
1277 if (EXCLUSIVE_OPLOCK_TYPE(lck
->data
->share_modes
[i
].op_type
)) {
1278 if (share_mode_stale_pid(lck
->data
, i
)) {
1279 DEBUG(10, ("Found stale duplicate oplock\n"));
1282 /* Exclusive or batch - can only be one. */
1283 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
1284 smb_panic("Bad exclusive or batch oplock entry.");
1286 *pp_ex_or_batch
= &lck
->data
->share_modes
[i
];
1289 if (LEVEL_II_OPLOCK_TYPE(lck
->data
->share_modes
[i
].op_type
)) {
1290 if (*pp_batch
|| *pp_ex_or_batch
) {
1291 if (share_mode_stale_pid(lck
->data
, i
)) {
1292 DEBUG(10, ("Found stale LevelII "
1296 smb_panic("Bad levelII oplock entry.");
1301 if (lck
->data
->share_modes
[i
].op_type
== NO_OPLOCK
) {
1302 if (*pp_batch
|| *pp_ex_or_batch
) {
1303 if (share_mode_stale_pid(lck
->data
, i
)) {
1304 DEBUG(10, ("Found stale NO_OPLOCK "
1308 smb_panic("Bad no oplock entry.");
1310 *got_no_oplock
= true;
1315 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1318 struct share_mode_entry
*batch_entry
)
1320 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1323 if (batch_entry
== NULL
) {
1327 if (server_id_is_disconnected(&batch_entry
->pid
)) {
1330 * This could be achieved by sending a break message
1331 * to ourselves. Special considerations for files
1332 * with delete_on_close flag set!
1334 * For now we keep it simple and do not
1335 * allow delete on close for durable handles.
1340 /* Found a batch oplock */
1341 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1345 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1348 struct share_mode_entry
*ex_entry
)
1350 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1353 if (ex_entry
== NULL
) {
1357 if (server_id_is_disconnected(&ex_entry
->pid
)) {
1359 * since only durable handles can get disconnected,
1360 * and we can only get durable handles with batch oplocks,
1361 * this should actually never be reached...
1366 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1370 static bool file_has_brlocks(files_struct
*fsp
)
1372 struct byte_range_lock
*br_lck
;
1374 br_lck
= brl_get_locks_readonly(fsp
);
1378 return br_lck
->num_locks
> 0 ? true : false;
1381 static void grant_fsp_oplock_type(files_struct
*fsp
,
1383 bool got_level2_oplock
,
1384 bool got_a_none_oplock
)
1386 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1387 lp_level2_oplocks(SNUM(fsp
->conn
));
1389 /* Start by granting what the client asked for,
1390 but ensure no SAMBA_PRIVATE bits can be set. */
1391 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1393 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1394 /* No oplocks on internal open. */
1395 fsp
->oplock_type
= NO_OPLOCK
;
1396 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1397 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1401 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1402 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1404 fsp
->oplock_type
= NO_OPLOCK
;
1407 if (is_stat_open(fsp
->access_mask
)) {
1408 /* Leave the value already set. */
1409 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1410 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1415 * Match what was requested (fsp->oplock_type) with
1416 * what was found in the existing share modes.
1419 if (got_a_none_oplock
) {
1420 fsp
->oplock_type
= NO_OPLOCK
;
1421 } else if (got_level2_oplock
) {
1422 if (fsp
->oplock_type
== NO_OPLOCK
||
1423 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1424 /* Store a level2 oplock, but don't tell the client */
1425 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1427 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1430 /* All share_mode_entries are placeholders or deferred.
1431 * Silently upgrade to fake levelII if the client didn't
1432 * ask for an oplock. */
1433 if (fsp
->oplock_type
== NO_OPLOCK
) {
1434 /* Store a level2 oplock, but don't tell the client */
1435 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1440 * Don't grant level2 to clients that don't want them
1441 * or if we've turned them off.
1443 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1444 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1447 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1448 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1451 static bool request_timed_out(struct timeval request_time
,
1452 struct timeval timeout
)
1454 struct timeval now
, end_time
;
1456 end_time
= timeval_sum(&request_time
, &timeout
);
1457 return (timeval_compare(&end_time
, &now
) < 0);
1460 /****************************************************************************
1461 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1462 ****************************************************************************/
1464 static void defer_open(struct share_mode_lock
*lck
,
1465 struct timeval request_time
,
1466 struct timeval timeout
,
1467 struct smb_request
*req
,
1468 struct deferred_open_record
*state
)
1470 struct server_id self
= messaging_server_id(req
->sconn
->msg_ctx
);
1472 /* Paranoia check */
1477 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1478 struct share_mode_entry
*e
= &lck
->data
->share_modes
[i
];
1480 if (is_deferred_open_entry(e
) &&
1481 serverid_equal(&self
, &e
->pid
) &&
1482 (e
->op_mid
== req
->mid
)) {
1483 DEBUG(0, ("Trying to defer an already deferred "
1484 "request: mid=%llu, exiting\n",
1485 (unsigned long long)req
->mid
));
1487 exit_server("attempt to defer a deferred request");
1492 /* End paranoia check */
1494 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1495 "open entry for mid %llu\n",
1496 (unsigned int)request_time
.tv_sec
,
1497 (unsigned int)request_time
.tv_usec
,
1498 (unsigned long long)req
->mid
));
1500 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1501 state
->id
, (char *)state
, sizeof(*state
))) {
1503 exit_server("push_deferred_open_message_smb failed");
1506 add_deferred_open(lck
, req
->mid
, request_time
, self
, state
->id
);
1511 /****************************************************************************
1512 On overwrite open ensure that the attributes match.
1513 ****************************************************************************/
1515 static bool open_match_attributes(connection_struct
*conn
,
1516 uint32 old_dos_attr
,
1517 uint32 new_dos_attr
,
1518 mode_t existing_unx_mode
,
1519 mode_t new_unx_mode
,
1520 mode_t
*returned_unx_mode
)
1522 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1524 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1525 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1527 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1528 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1529 *returned_unx_mode
= new_unx_mode
;
1531 *returned_unx_mode
= (mode_t
)0;
1534 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1535 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1536 "returned_unx_mode = 0%o\n",
1537 (unsigned int)old_dos_attr
,
1538 (unsigned int)existing_unx_mode
,
1539 (unsigned int)new_dos_attr
,
1540 (unsigned int)*returned_unx_mode
));
1542 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1543 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1544 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1545 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1549 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1550 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1551 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1558 /****************************************************************************
1559 Special FCB or DOS processing in the case of a sharing violation.
1560 Try and find a duplicated file handle.
1561 ****************************************************************************/
1563 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1564 connection_struct
*conn
,
1565 files_struct
*fsp_to_dup_into
,
1566 const struct smb_filename
*smb_fname
,
1571 uint32 share_access
,
1572 uint32 create_options
)
1576 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1577 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1579 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1580 fsp
= file_find_di_next(fsp
)) {
1582 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1583 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1584 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1585 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
1586 (unsigned int)fsp
->file_pid
,
1587 (unsigned int)fsp
->fh
->private_options
,
1588 (unsigned int)fsp
->access_mask
));
1590 if (fsp
!= fsp_to_dup_into
&&
1591 fsp
->fh
->fd
!= -1 &&
1592 fsp
->vuid
== vuid
&&
1593 fsp
->file_pid
== file_pid
&&
1594 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1595 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1596 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1597 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1598 strequal(fsp
->fsp_name
->stream_name
,
1599 smb_fname
->stream_name
)) {
1600 DEBUG(10,("fcb_or_dos_open: file match\n"));
1606 return NT_STATUS_NOT_FOUND
;
1609 /* quite an insane set of semantics ... */
1610 if (is_executable(smb_fname
->base_name
) &&
1611 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1612 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1613 return NT_STATUS_INVALID_PARAMETER
;
1616 /* We need to duplicate this fsp. */
1617 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1618 create_options
, fsp_to_dup_into
);
1621 static void schedule_defer_open(struct share_mode_lock
*lck
,
1622 struct timeval request_time
,
1623 struct smb_request
*req
)
1625 struct deferred_open_record state
;
1627 /* This is a relative time, added to the absolute
1628 request_time value to get the absolute timeout time.
1629 Note that if this is the second or greater time we enter
1630 this codepath for this particular request mid then
1631 request_time is left as the absolute time of the *first*
1632 time this request mid was processed. This is what allows
1633 the request to eventually time out. */
1635 struct timeval timeout
;
1637 /* Normally the smbd we asked should respond within
1638 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1639 * the client did, give twice the timeout as a safety
1640 * measure here in case the other smbd is stuck
1641 * somewhere else. */
1643 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1645 /* Nothing actually uses state.delayed_for_oplocks
1646 but it's handy to differentiate in debug messages
1647 between a 30 second delay due to oplock break, and
1648 a 1 second delay for share mode conflicts. */
1650 state
.delayed_for_oplocks
= True
;
1651 state
.async_open
= false;
1652 state
.id
= lck
->data
->id
;
1654 if (!request_timed_out(request_time
, timeout
)) {
1655 defer_open(lck
, request_time
, timeout
, req
, &state
);
1659 /****************************************************************************
1660 Reschedule an open call that went asynchronous.
1661 ****************************************************************************/
1663 static void schedule_async_open(struct timeval request_time
,
1664 struct smb_request
*req
)
1666 struct deferred_open_record state
;
1667 struct timeval timeout
;
1669 timeout
= timeval_set(20, 0);
1672 state
.delayed_for_oplocks
= false;
1673 state
.async_open
= true;
1675 if (!request_timed_out(request_time
, timeout
)) {
1676 defer_open(NULL
, request_time
, timeout
, req
, &state
);
1680 /****************************************************************************
1681 Work out what access_mask to use from what the client sent us.
1682 ****************************************************************************/
1684 static NTSTATUS
smbd_calculate_maximum_allowed_access(
1685 connection_struct
*conn
,
1686 const struct smb_filename
*smb_fname
,
1688 uint32_t *p_access_mask
)
1690 struct security_descriptor
*sd
;
1691 uint32_t access_granted
;
1694 if (!use_privs
&& (get_current_uid(conn
) == (uid_t
)0)) {
1695 *p_access_mask
|= FILE_GENERIC_ALL
;
1696 return NT_STATUS_OK
;
1699 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1705 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1707 * File did not exist
1709 *p_access_mask
= FILE_GENERIC_ALL
;
1710 return NT_STATUS_OK
;
1712 if (!NT_STATUS_IS_OK(status
)) {
1713 DEBUG(10,("Could not get acl on file %s: %s\n",
1714 smb_fname_str_dbg(smb_fname
),
1715 nt_errstr(status
)));
1716 return NT_STATUS_ACCESS_DENIED
;
1720 * If we can access the path to this file, by
1721 * default we have FILE_READ_ATTRIBUTES from the
1722 * containing directory. See the section:
1723 * "Algorithm to Check Access to an Existing File"
1726 * se_file_access_check()
1727 * also takes care of owner WRITE_DAC and READ_CONTROL.
1729 status
= se_file_access_check(sd
,
1730 get_current_nttok(conn
),
1732 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
1737 if (!NT_STATUS_IS_OK(status
)) {
1738 DEBUG(10, ("Access denied on file %s: "
1739 "when calculating maximum access\n",
1740 smb_fname_str_dbg(smb_fname
)));
1741 return NT_STATUS_ACCESS_DENIED
;
1743 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
1745 if (!(access_granted
& DELETE_ACCESS
)) {
1746 if (can_delete_file_in_directory(conn
, smb_fname
)) {
1747 *p_access_mask
|= DELETE_ACCESS
;
1751 return NT_STATUS_OK
;
1754 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1755 const struct smb_filename
*smb_fname
,
1757 uint32_t access_mask
,
1758 uint32_t *access_mask_out
)
1761 uint32_t orig_access_mask
= access_mask
;
1762 uint32_t rejected_share_access
;
1765 * Convert GENERIC bits to specific bits.
1768 se_map_generic(&access_mask
, &file_generic_mapping
);
1770 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1771 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1773 status
= smbd_calculate_maximum_allowed_access(
1774 conn
, smb_fname
, use_privs
, &access_mask
);
1776 if (!NT_STATUS_IS_OK(status
)) {
1780 access_mask
&= conn
->share_access
;
1783 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1785 if (rejected_share_access
) {
1786 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1787 "file %s: rejected by share access mask[0x%08X] "
1788 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1789 smb_fname_str_dbg(smb_fname
),
1791 orig_access_mask
, access_mask
,
1792 rejected_share_access
));
1793 return NT_STATUS_ACCESS_DENIED
;
1796 *access_mask_out
= access_mask
;
1797 return NT_STATUS_OK
;
1800 /****************************************************************************
1801 Remove the deferred open entry under lock.
1802 ****************************************************************************/
1804 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1805 struct server_id pid
)
1807 struct share_mode_lock
*lck
= get_existing_share_mode_lock(
1810 DEBUG(0, ("could not get share mode lock\n"));
1813 del_deferred_open_entry(lck
, mid
, pid
);
1817 /****************************************************************************
1818 Return true if this is a state pointer to an asynchronous create.
1819 ****************************************************************************/
1821 bool is_deferred_open_async(const void *ptr
)
1823 const struct deferred_open_record
*state
= (const struct deferred_open_record
*)ptr
;
1825 return state
->async_open
;
1828 static bool clear_ads(uint32_t create_disposition
)
1832 switch (create_disposition
) {
1833 case FILE_SUPERSEDE
:
1834 case FILE_OVERWRITE_IF
:
1835 case FILE_OVERWRITE
:
1844 static int disposition_to_open_flags(uint32_t create_disposition
)
1849 * Currently we're using FILE_SUPERSEDE as the same as
1850 * FILE_OVERWRITE_IF but they really are
1851 * different. FILE_SUPERSEDE deletes an existing file
1852 * (requiring delete access) then recreates it.
1855 switch (create_disposition
) {
1856 case FILE_SUPERSEDE
:
1857 case FILE_OVERWRITE_IF
:
1859 * If file exists replace/overwrite. If file doesn't
1862 ret
= O_CREAT
|O_TRUNC
;
1867 * If file exists open. If file doesn't exist error.
1872 case FILE_OVERWRITE
:
1874 * If file exists overwrite. If file doesn't exist
1882 * If file exists error. If file doesn't exist create.
1884 ret
= O_CREAT
|O_EXCL
;
1889 * If file exists open. If file doesn't exist create.
1897 static int calculate_open_access_flags(uint32_t access_mask
,
1899 uint32_t private_flags
)
1901 bool need_write
, need_read
;
1904 * Note that we ignore the append flag as append does not
1905 * mean the same thing under DOS and Unix.
1909 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
1910 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
));
1916 /* DENY_DOS opens are always underlying read-write on the
1917 file handle, no matter what the requested access mask
1921 ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
1922 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|
1923 FILE_READ_EA
|FILE_EXECUTE
));
1931 /****************************************************************************
1932 Open a file with a share mode. Passed in an already created files_struct *.
1933 ****************************************************************************/
1935 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1936 struct smb_request
*req
,
1937 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1938 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1939 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1940 uint32 create_options
, /* options such as delete on close. */
1941 uint32 new_dos_attributes
, /* attributes used for new file. */
1942 int oplock_request
, /* internal Samba oplock codes. */
1943 /* Information (FILE_EXISTS etc.) */
1944 uint32_t private_flags
, /* Samba specific flags. */
1948 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1951 bool file_existed
= VALID_STAT(smb_fname
->st
);
1952 bool def_acl
= False
;
1953 bool posix_open
= False
;
1954 bool new_file_created
= False
;
1955 bool first_open_attempt
= true;
1956 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1957 mode_t new_unx_mode
= (mode_t
)0;
1958 mode_t unx_mode
= (mode_t
)0;
1960 uint32 existing_dos_attributes
= 0;
1961 struct timeval request_time
= timeval_zero();
1962 struct share_mode_lock
*lck
= NULL
;
1963 uint32 open_access_mask
= access_mask
;
1966 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
1967 struct share_mode_entry
*batch_entry
= NULL
;
1968 struct share_mode_entry
*exclusive_entry
= NULL
;
1969 bool got_level2_oplock
= false;
1970 bool got_a_none_oplock
= false;
1971 struct timespec old_write_time
;
1974 if (conn
->printer
) {
1976 * Printers are handled completely differently.
1977 * Most of the passed parameters are ignored.
1981 *pinfo
= FILE_WAS_CREATED
;
1984 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1985 smb_fname_str_dbg(smb_fname
)));
1988 DEBUG(0,("open_file_ntcreate: printer open without "
1989 "an SMB request!\n"));
1990 return NT_STATUS_INTERNAL_ERROR
;
1993 return print_spool_open(fsp
, smb_fname
->base_name
,
1997 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1999 return NT_STATUS_NO_MEMORY
;
2002 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2004 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2005 new_dos_attributes
= 0;
2007 /* Windows allows a new file to be created and
2008 silently removes a FILE_ATTRIBUTE_DIRECTORY
2009 sent by the client. Do the same. */
2011 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
2013 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
2015 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2016 smb_fname
, parent_dir
);
2019 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
2020 "access_mask=0x%x share_access=0x%x "
2021 "create_disposition = 0x%x create_options=0x%x "
2022 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
2023 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
2024 access_mask
, share_access
, create_disposition
,
2025 create_options
, (unsigned int)unx_mode
, oplock_request
,
2026 (unsigned int)private_flags
));
2028 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
2029 DEBUG(0, ("No smb request but not an internal only open!\n"));
2030 return NT_STATUS_INTERNAL_ERROR
;
2034 * Only non-internal opens can be deferred at all
2039 if (get_deferred_open_message_state(req
,
2042 /* Remember the absolute time of the original
2043 request with this mid. We'll use it later to
2044 see if this has timed out. */
2046 /* If it was an async create retry, the file
2049 if (is_deferred_open_async(ptr
)) {
2050 SET_STAT_INVALID(smb_fname
->st
);
2051 file_existed
= false;
2053 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
2054 /* Remove the deferred open entry under lock. */
2055 remove_deferred_open_entry(
2056 state
->id
, req
->mid
,
2057 messaging_server_id(req
->sconn
->msg_ctx
));
2060 /* Ensure we don't reprocess this message. */
2061 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
2063 first_open_attempt
= false;
2068 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
2070 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
2074 /* ignore any oplock requests if oplocks are disabled */
2075 if (!lp_oplocks(SNUM(conn
)) ||
2076 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
2077 /* Mask off everything except the private Samba bits. */
2078 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
2081 /* this is for OS/2 long file names - say we don't support them */
2082 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
2083 /* OS/2 Workplace shell fix may be main code stream in a later
2085 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
2087 if (use_nt_status()) {
2088 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2090 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
2093 switch( create_disposition
) {
2095 /* If file exists open. If file doesn't exist error. */
2096 if (!file_existed
) {
2097 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2098 "requested for file %s and file "
2100 smb_fname_str_dbg(smb_fname
)));
2102 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2106 case FILE_OVERWRITE
:
2107 /* If file exists overwrite. If file doesn't exist
2109 if (!file_existed
) {
2110 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2111 "requested for file %s and file "
2113 smb_fname_str_dbg(smb_fname
) ));
2115 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2120 /* If file exists error. If file doesn't exist
2123 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2124 "requested for file %s and file "
2125 "already exists.\n",
2126 smb_fname_str_dbg(smb_fname
)));
2127 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2132 return map_nt_error_from_unix(errno
);
2136 case FILE_SUPERSEDE
:
2137 case FILE_OVERWRITE_IF
:
2141 return NT_STATUS_INVALID_PARAMETER
;
2144 flags2
= disposition_to_open_flags(create_disposition
);
2146 /* We only care about matching attributes on file exists and
2149 if (!posix_open
&& file_existed
&&
2150 ((create_disposition
== FILE_OVERWRITE
) ||
2151 (create_disposition
== FILE_OVERWRITE_IF
))) {
2152 if (!open_match_attributes(conn
, existing_dos_attributes
,
2154 smb_fname
->st
.st_ex_mode
,
2155 unx_mode
, &new_unx_mode
)) {
2156 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2157 "for file %s (%x %x) (0%o, 0%o)\n",
2158 smb_fname_str_dbg(smb_fname
),
2159 existing_dos_attributes
,
2161 (unsigned int)smb_fname
->st
.st_ex_mode
,
2162 (unsigned int)unx_mode
));
2164 return NT_STATUS_ACCESS_DENIED
;
2168 status
= smbd_calculate_access_mask(conn
, smb_fname
,
2172 if (!NT_STATUS_IS_OK(status
)) {
2173 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2174 "on file %s returned %s\n",
2175 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
2179 open_access_mask
= access_mask
;
2181 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
2182 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
2185 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2186 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
2190 * Note that we ignore the append flag as append does not
2191 * mean the same thing under DOS and Unix.
2194 flags
= calculate_open_access_flags(access_mask
, oplock_request
,
2198 * Currently we only look at FILE_WRITE_THROUGH for create options.
2202 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
2207 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
2211 if (!posix_open
&& !CAN_WRITE(conn
)) {
2213 * We should really return a permission denied error if either
2214 * O_CREAT or O_TRUNC are set, but for compatibility with
2215 * older versions of Samba we just AND them out.
2217 flags2
&= ~(O_CREAT
|O_TRUNC
);
2220 if (first_open_attempt
&& lp_kernel_oplocks(SNUM(conn
))) {
2222 * With kernel oplocks the open breaking an oplock
2223 * blocks until the oplock holder has given up the
2224 * oplock or closed the file. We prevent this by first
2225 * trying to open the file with O_NONBLOCK (see "man
2226 * fcntl" on Linux). For the second try, triggered by
2227 * an oplock break response, we do not need this
2230 * This is true under the assumption that only Samba
2231 * requests kernel oplocks. Once someone else like
2232 * NFSv4 starts to use that API, we will have to
2233 * modify this by communicating with the NFSv4 server.
2235 flags2
|= O_NONBLOCK
;
2239 * Ensure we can't write on a read-only share or file.
2242 if (flags
!= O_RDONLY
&& file_existed
&&
2243 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
2244 DEBUG(5,("open_file_ntcreate: write access requested for "
2245 "file %s on read only %s\n",
2246 smb_fname_str_dbg(smb_fname
),
2247 !CAN_WRITE(conn
) ? "share" : "file" ));
2249 return NT_STATUS_ACCESS_DENIED
;
2252 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2253 fsp
->share_access
= share_access
;
2254 fsp
->fh
->private_options
= private_flags
;
2255 fsp
->access_mask
= open_access_mask
; /* We change this to the
2256 * requested access_mask after
2257 * the open is done. */
2258 fsp
->posix_open
= posix_open
;
2260 /* Ensure no SAMBA_PRIVATE bits can be set. */
2261 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
2263 if (timeval_is_zero(&request_time
)) {
2264 request_time
= fsp
->open_time
;
2268 * Ensure we pay attention to default ACLs on directories if required.
2271 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2272 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2273 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2276 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2277 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2278 (unsigned int)flags
, (unsigned int)flags2
,
2279 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2280 (unsigned int)open_access_mask
));
2282 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2283 flags
|flags2
, unx_mode
, access_mask
,
2284 open_access_mask
, &new_file_created
);
2286 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_NETWORK_BUSY
)) {
2287 struct deferred_open_record state
;
2290 * EWOULDBLOCK/EAGAIN maps to NETWORK_BUSY.
2292 if (file_existed
&& S_ISFIFO(fsp
->fsp_name
->st
.st_ex_mode
)) {
2293 DEBUG(10, ("FIFO busy\n"));
2294 return NT_STATUS_NETWORK_BUSY
;
2297 DEBUG(10, ("Internal open busy\n"));
2298 return NT_STATUS_NETWORK_BUSY
;
2302 * From here on we assume this is an oplock break triggered
2305 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
2307 state
.delayed_for_oplocks
= false;
2308 state
.async_open
= false;
2309 state
.id
= fsp
->file_id
;
2310 defer_open(NULL
, request_time
, timeval_set(0, 0),
2312 DEBUG(10, ("No share mode lock found after "
2313 "EWOULDBLOCK, retrying sync\n"));
2314 return NT_STATUS_SHARING_VIOLATION
;
2317 find_oplock_types(fsp
, 0, lck
, &batch_entry
, &exclusive_entry
,
2318 &got_level2_oplock
, &got_a_none_oplock
);
2320 if (delay_for_batch_oplocks(fsp
, req
->mid
, 0, batch_entry
) ||
2321 delay_for_exclusive_oplocks(fsp
, req
->mid
, 0,
2323 schedule_defer_open(lck
, request_time
, req
);
2325 DEBUG(10, ("Sent oplock break request to kernel "
2326 "oplock holder\n"));
2327 return NT_STATUS_SHARING_VIOLATION
;
2331 * No oplock from Samba around. Immediately retry with
2334 state
.delayed_for_oplocks
= false;
2335 state
.async_open
= false;
2336 state
.id
= lck
->data
->id
;
2337 defer_open(lck
, request_time
, timeval_set(0, 0), req
, &state
);
2339 DEBUG(10, ("No Samba oplock around after EWOULDBLOCK. "
2340 "Retrying sync\n"));
2341 return NT_STATUS_SHARING_VIOLATION
;
2344 if (!NT_STATUS_IS_OK(fsp_open
)) {
2345 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
2346 schedule_async_open(request_time
, req
);
2352 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
2354 * The file did exist, but some other (local or NFS)
2355 * process either renamed/unlinked and re-created the
2356 * file with different dev/ino after we walked the path,
2357 * but before we did the open. We could retry the
2358 * open but it's a rare enough case it's easier to
2359 * just fail the open to prevent creating any problems
2360 * in the open file db having the wrong dev/ino key.
2364 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2365 "Old (dev=0x%llu, ino =0x%llu). "
2366 "New (dev=0x%llu, ino=0x%llu). Failing open "
2367 " with NT_STATUS_ACCESS_DENIED.\n",
2368 smb_fname_str_dbg(smb_fname
),
2369 (unsigned long long)saved_stat
.st_ex_dev
,
2370 (unsigned long long)saved_stat
.st_ex_ino
,
2371 (unsigned long long)smb_fname
->st
.st_ex_dev
,
2372 (unsigned long long)smb_fname
->st
.st_ex_ino
));
2373 return NT_STATUS_ACCESS_DENIED
;
2376 old_write_time
= smb_fname
->st
.st_ex_mtime
;
2379 * Deal with the race condition where two smbd's detect the
2380 * file doesn't exist and do the create at the same time. One
2381 * of them will win and set a share mode, the other (ie. this
2382 * one) should check if the requested share mode for this
2383 * create is allowed.
2387 * Now the file exists and fsp is successfully opened,
2388 * fsp->dev and fsp->inode are valid and should replace the
2389 * dev=0,inode=0 from a non existent file. Spotted by
2390 * Nadav Danieli <nadavd@exanet.com>. JRA.
2395 lck
= get_share_mode_lock(talloc_tos(), id
,
2397 smb_fname
, &old_write_time
);
2400 DEBUG(0, ("open_file_ntcreate: Could not get share "
2401 "mode lock for %s\n",
2402 smb_fname_str_dbg(smb_fname
)));
2404 return NT_STATUS_SHARING_VIOLATION
;
2407 /* Get the types we need to examine. */
2408 find_oplock_types(fsp
,
2414 &got_a_none_oplock
);
2416 /* First pass - send break only on batch oplocks. */
2417 if ((req
!= NULL
) &&
2418 delay_for_batch_oplocks(fsp
,
2422 schedule_defer_open(lck
, request_time
, req
);
2425 return NT_STATUS_SHARING_VIOLATION
;
2428 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2429 access_mask
, share_access
,
2430 create_options
, &file_existed
);
2432 if (NT_STATUS_IS_OK(status
)) {
2433 /* We might be going to allow this open. Check oplock
2435 /* Second pass - send break for both batch or
2436 * exclusive oplocks. */
2437 if ((req
!= NULL
) &&
2438 delay_for_exclusive_oplocks(
2443 schedule_defer_open(lck
, request_time
, req
);
2446 return NT_STATUS_SHARING_VIOLATION
;
2450 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
2451 /* DELETE_PENDING is not deferred for a second */
2457 if (!NT_STATUS_IS_OK(status
)) {
2458 uint32 can_access_mask
;
2459 bool can_access
= True
;
2461 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2463 /* Check if this can be done with the deny_dos and fcb
2466 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2467 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2469 DEBUG(0, ("DOS open without an SMB "
2473 return NT_STATUS_INTERNAL_ERROR
;
2476 /* Use the client requested access mask here,
2477 * not the one we open with. */
2478 status
= fcb_or_dos_open(req
,
2489 if (NT_STATUS_IS_OK(status
)) {
2492 *pinfo
= FILE_WAS_OPENED
;
2494 return NT_STATUS_OK
;
2499 * This next line is a subtlety we need for
2500 * MS-Access. If a file open will fail due to share
2501 * permissions and also for security (access) reasons,
2502 * we need to return the access failed error, not the
2503 * share error. We can't open the file due to kernel
2504 * oplock deadlock (it's possible we failed above on
2505 * the open_mode_check()) so use a userspace check.
2508 if (flags
& O_RDWR
) {
2509 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2510 } else if (flags
& O_WRONLY
) {
2511 can_access_mask
= FILE_WRITE_DATA
;
2513 can_access_mask
= FILE_READ_DATA
;
2516 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2517 !CAN_WRITE(conn
)) ||
2518 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
2521 can_access_mask
))) {
2526 * If we're returning a share violation, ensure we
2527 * cope with the braindead 1 second delay.
2530 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2531 lp_defer_sharing_violations()) {
2532 struct timeval timeout
;
2533 struct deferred_open_record state
;
2536 /* this is a hack to speed up torture tests
2538 timeout_usecs
= lp_parm_int(SNUM(conn
),
2539 "smbd","sharedelay",
2540 SHARING_VIOLATION_USEC_WAIT
);
2542 /* This is a relative time, added to the absolute
2543 request_time value to get the absolute timeout time.
2544 Note that if this is the second or greater time we enter
2545 this codepath for this particular request mid then
2546 request_time is left as the absolute time of the *first*
2547 time this request mid was processed. This is what allows
2548 the request to eventually time out. */
2550 timeout
= timeval_set(0, timeout_usecs
);
2552 /* Nothing actually uses state.delayed_for_oplocks
2553 but it's handy to differentiate in debug messages
2554 between a 30 second delay due to oplock break, and
2555 a 1 second delay for share mode conflicts. */
2557 state
.delayed_for_oplocks
= False
;
2558 state
.async_open
= false;
2562 && !request_timed_out(request_time
,
2564 defer_open(lck
, request_time
, timeout
,
2573 * We have detected a sharing violation here
2574 * so return the correct error code
2576 status
= NT_STATUS_SHARING_VIOLATION
;
2578 status
= NT_STATUS_ACCESS_DENIED
;
2583 grant_fsp_oplock_type(fsp
,
2589 * We have the share entry *locked*.....
2592 /* Delete streams if create_disposition requires it */
2593 if (!new_file_created
&& clear_ads(create_disposition
) &&
2594 !is_ntfs_stream_smb_fname(smb_fname
)) {
2595 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2596 if (!NT_STATUS_IS_OK(status
)) {
2603 /* note that we ignore failure for the following. It is
2604 basically a hack for NFS, and NFS will never set one of
2605 these only read them. Nobody but Samba can ever set a deny
2606 mode and we have already checked our more authoritative
2607 locking database for permission to set this deny mode. If
2608 the kernel refuses the operations then the kernel is wrong.
2609 note that GPFS supports it as well - jmcd */
2611 if (fsp
->fh
->fd
!= -1 && lp_kernel_share_modes(SNUM(conn
))) {
2613 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2614 if(ret_flock
== -1 ){
2619 return NT_STATUS_SHARING_VIOLATION
;
2624 * At this point onwards, we can guarantee that the share entry
2625 * is locked, whether we created the file or not, and that the
2626 * deny mode is compatible with all current opens.
2630 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2631 * but we don't have to store this - just ignore it on access check.
2633 if (conn
->sconn
->using_smb2
) {
2635 * SMB2 doesn't return it (according to Microsoft tests).
2636 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2637 * File created with access = 0x7 (Read, Write, Delete)
2638 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2640 fsp
->access_mask
= access_mask
;
2642 /* But SMB1 does. */
2643 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2647 /* stat opens on existing files don't get oplocks. */
2648 if (is_stat_open(open_access_mask
)) {
2649 fsp
->oplock_type
= NO_OPLOCK
;
2653 if (new_file_created
) {
2654 info
= FILE_WAS_CREATED
;
2656 if (flags2
& O_TRUNC
) {
2657 info
= FILE_WAS_OVERWRITTEN
;
2659 info
= FILE_WAS_OPENED
;
2668 * Setup the oplock info in both the shared memory and
2672 status
= set_file_oplock(fsp
, fsp
->oplock_type
);
2673 if (!NT_STATUS_IS_OK(status
)) {
2675 * Could not get the kernel oplock or there are byte-range
2676 * locks on the file.
2678 fsp
->oplock_type
= NO_OPLOCK
;
2681 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2685 /* Handle strange delete on close create semantics. */
2686 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2688 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2690 if (!NT_STATUS_IS_OK(status
)) {
2691 /* Remember to delete the mode we just added. */
2692 del_share_mode(lck
, fsp
);
2697 /* Note that here we set the *inital* delete on close flag,
2698 not the regular one. The magic gets handled in close. */
2699 fsp
->initial_delete_on_close
= True
;
2702 if (info
!= FILE_WAS_OPENED
) {
2703 /* Files should be initially set as archive */
2704 if (lp_map_archive(SNUM(conn
)) ||
2705 lp_store_dos_attributes(SNUM(conn
))) {
2707 if (file_set_dosmode(conn
, smb_fname
,
2708 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2709 parent_dir
, true) == 0) {
2710 unx_mode
= smb_fname
->st
.st_ex_mode
;
2716 /* Determine sparse flag. */
2718 /* POSIX opens are sparse by default. */
2719 fsp
->is_sparse
= true;
2721 fsp
->is_sparse
= (file_existed
&&
2722 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2726 * Take care of inherited ACLs on created files - if default ACL not
2730 if (!posix_open
&& new_file_created
&& !def_acl
) {
2732 int saved_errno
= errno
; /* We might get ENOSYS in the next
2735 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2737 errno
= saved_errno
; /* Ignore ENOSYS */
2740 } else if (new_unx_mode
) {
2744 /* Attributes need changing. File already existed. */
2747 int saved_errno
= errno
; /* We might get ENOSYS in the
2749 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2751 if (ret
== -1 && errno
== ENOSYS
) {
2752 errno
= saved_errno
; /* Ignore ENOSYS */
2754 DEBUG(5, ("open_file_ntcreate: reset "
2755 "attributes of file %s to 0%o\n",
2756 smb_fname_str_dbg(smb_fname
),
2757 (unsigned int)new_unx_mode
));
2758 ret
= 0; /* Don't do the fchmod below. */
2763 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2764 DEBUG(5, ("open_file_ntcreate: failed to reset "
2765 "attributes of file %s to 0%o\n",
2766 smb_fname_str_dbg(smb_fname
),
2767 (unsigned int)new_unx_mode
));
2770 /* If this is a successful open, we must remove any deferred open
2773 del_deferred_open_entry(lck
, req
->mid
,
2774 messaging_server_id(req
->sconn
->msg_ctx
));
2778 return NT_STATUS_OK
;
2782 /****************************************************************************
2783 Open a file for for write to ensure that we can fchmod it.
2784 ****************************************************************************/
2786 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2787 struct smb_filename
*smb_fname
,
2788 files_struct
**result
)
2790 if (!VALID_STAT(smb_fname
->st
)) {
2791 return NT_STATUS_INVALID_PARAMETER
;
2794 return SMB_VFS_CREATE_FILE(
2797 0, /* root_dir_fid */
2798 smb_fname
, /* fname */
2799 FILE_WRITE_DATA
, /* access_mask */
2800 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2802 FILE_OPEN
, /* create_disposition*/
2803 0, /* create_options */
2804 0, /* file_attributes */
2805 INTERNAL_OPEN_ONLY
, /* oplock_request */
2806 0, /* allocation_size */
2807 0, /* private_flags */
2810 result
, /* result */
2814 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2815 struct smb_filename
*smb_dname
,
2816 uint32 file_attributes
)
2819 char *parent_dir
= NULL
;
2821 bool posix_open
= false;
2822 bool need_re_stat
= false;
2823 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2825 if (!CAN_WRITE(conn
) || (access_mask
& ~(conn
->share_access
))) {
2826 DEBUG(5,("mkdir_internal: failing share access "
2827 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
2828 return NT_STATUS_ACCESS_DENIED
;
2831 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2833 return NT_STATUS_NO_MEMORY
;
2836 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2838 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2840 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2843 status
= check_parent_access(conn
,
2846 if(!NT_STATUS_IS_OK(status
)) {
2847 DEBUG(5,("mkdir_internal: check_parent_access "
2848 "on directory %s for path %s returned %s\n",
2850 smb_dname
->base_name
,
2851 nt_errstr(status
) ));
2855 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2856 return map_nt_error_from_unix(errno
);
2859 /* Ensure we're checking for a symlink here.... */
2860 /* We don't want to get caught by a symlink racer. */
2862 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2863 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2864 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2865 return map_nt_error_from_unix(errno
);
2868 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2869 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2870 smb_fname_str_dbg(smb_dname
)));
2871 return NT_STATUS_NOT_A_DIRECTORY
;
2874 if (lp_store_dos_attributes(SNUM(conn
))) {
2876 file_set_dosmode(conn
, smb_dname
,
2877 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2882 if (lp_inherit_perms(SNUM(conn
))) {
2883 inherit_access_posix_acl(conn
, parent_dir
,
2884 smb_dname
->base_name
, mode
);
2885 need_re_stat
= true;
2890 * Check if high bits should have been set,
2891 * then (if bits are missing): add them.
2892 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2895 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2896 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2897 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2898 (smb_dname
->st
.st_ex_mode
|
2899 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2900 need_re_stat
= true;
2904 /* Change the owner if required. */
2905 if (lp_inherit_owner(SNUM(conn
))) {
2906 change_dir_owner_to_parent(conn
, parent_dir
,
2907 smb_dname
->base_name
,
2909 need_re_stat
= true;
2913 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2914 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2915 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2916 return map_nt_error_from_unix(errno
);
2920 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2921 smb_dname
->base_name
);
2923 return NT_STATUS_OK
;
2926 /****************************************************************************
2927 Open a directory from an NT SMB call.
2928 ****************************************************************************/
2930 static NTSTATUS
open_directory(connection_struct
*conn
,
2931 struct smb_request
*req
,
2932 struct smb_filename
*smb_dname
,
2934 uint32 share_access
,
2935 uint32 create_disposition
,
2936 uint32 create_options
,
2937 uint32 file_attributes
,
2939 files_struct
**result
)
2941 files_struct
*fsp
= NULL
;
2942 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2943 struct share_mode_lock
*lck
= NULL
;
2945 struct timespec mtimespec
;
2948 if (is_ntfs_stream_smb_fname(smb_dname
)) {
2949 DEBUG(2, ("open_directory: %s is a stream name!\n",
2950 smb_fname_str_dbg(smb_dname
)));
2951 return NT_STATUS_NOT_A_DIRECTORY
;
2954 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
2955 /* Ensure we have a directory attribute. */
2956 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2959 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2960 "share_access = 0x%x create_options = 0x%x, "
2961 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2962 smb_fname_str_dbg(smb_dname
),
2963 (unsigned int)access_mask
,
2964 (unsigned int)share_access
,
2965 (unsigned int)create_options
,
2966 (unsigned int)create_disposition
,
2967 (unsigned int)file_attributes
));
2969 status
= smbd_calculate_access_mask(conn
, smb_dname
, false,
2970 access_mask
, &access_mask
);
2971 if (!NT_STATUS_IS_OK(status
)) {
2972 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2973 "on file %s returned %s\n",
2974 smb_fname_str_dbg(smb_dname
),
2975 nt_errstr(status
)));
2979 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2980 !security_token_has_privilege(get_current_nttok(conn
),
2981 SEC_PRIV_SECURITY
)) {
2982 DEBUG(10, ("open_directory: open on %s "
2983 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2984 smb_fname_str_dbg(smb_dname
)));
2985 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2988 switch( create_disposition
) {
2992 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2995 info
= FILE_WAS_OPENED
;
3000 /* If directory exists error. If directory doesn't
3004 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
3005 DEBUG(2, ("open_directory: unable to create "
3006 "%s. Error was %s\n",
3007 smb_fname_str_dbg(smb_dname
),
3008 nt_errstr(status
)));
3012 status
= mkdir_internal(conn
, smb_dname
,
3015 if (!NT_STATUS_IS_OK(status
)) {
3016 DEBUG(2, ("open_directory: unable to create "
3017 "%s. Error was %s\n",
3018 smb_fname_str_dbg(smb_dname
),
3019 nt_errstr(status
)));
3023 info
= FILE_WAS_CREATED
;
3028 * If directory exists open. If directory doesn't
3033 status
= NT_STATUS_OK
;
3034 info
= FILE_WAS_OPENED
;
3036 status
= mkdir_internal(conn
, smb_dname
,
3039 if (NT_STATUS_IS_OK(status
)) {
3040 info
= FILE_WAS_CREATED
;
3042 /* Cope with create race. */
3043 if (!NT_STATUS_EQUAL(status
,
3044 NT_STATUS_OBJECT_NAME_COLLISION
)) {
3045 DEBUG(2, ("open_directory: unable to create "
3046 "%s. Error was %s\n",
3047 smb_fname_str_dbg(smb_dname
),
3048 nt_errstr(status
)));
3051 info
= FILE_WAS_OPENED
;
3057 case FILE_SUPERSEDE
:
3058 case FILE_OVERWRITE
:
3059 case FILE_OVERWRITE_IF
:
3061 DEBUG(5,("open_directory: invalid create_disposition "
3062 "0x%x for directory %s\n",
3063 (unsigned int)create_disposition
,
3064 smb_fname_str_dbg(smb_dname
)));
3065 return NT_STATUS_INVALID_PARAMETER
;
3068 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3069 DEBUG(5,("open_directory: %s is not a directory !\n",
3070 smb_fname_str_dbg(smb_dname
)));
3071 return NT_STATUS_NOT_A_DIRECTORY
;
3074 if (info
== FILE_WAS_OPENED
) {
3075 status
= smbd_check_access_rights(conn
,
3079 if (!NT_STATUS_IS_OK(status
)) {
3080 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3081 "file %s failed with %s\n",
3082 smb_fname_str_dbg(smb_dname
),
3083 nt_errstr(status
)));
3088 status
= file_new(req
, conn
, &fsp
);
3089 if(!NT_STATUS_IS_OK(status
)) {
3094 * Setup the files_struct for it.
3097 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
3098 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
3099 fsp
->file_pid
= req
? req
->smbpid
: 0;
3100 fsp
->can_lock
= False
;
3101 fsp
->can_read
= False
;
3102 fsp
->can_write
= False
;
3104 fsp
->share_access
= share_access
;
3105 fsp
->fh
->private_options
= 0;
3107 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3109 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3110 fsp
->print_file
= NULL
;
3111 fsp
->modified
= False
;
3112 fsp
->oplock_type
= NO_OPLOCK
;
3113 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
3114 fsp
->is_directory
= True
;
3115 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
3116 status
= fsp_set_smb_fname(fsp
, smb_dname
);
3117 if (!NT_STATUS_IS_OK(status
)) {
3118 file_free(req
, fsp
);
3122 mtimespec
= smb_dname
->st
.st_ex_mtime
;
3125 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
3127 /* POSIX allows us to open a directory with O_RDONLY. */
3128 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
3130 if (!NT_STATUS_IS_OK(status
)) {
3131 DEBUG(5, ("open_directory: Could not open fd for "
3133 smb_fname_str_dbg(smb_dname
),
3134 nt_errstr(status
)));
3135 file_free(req
, fsp
);
3139 status
= vfs_stat_fsp(fsp
);
3140 if (!NT_STATUS_IS_OK(status
)) {
3142 file_free(req
, fsp
);
3146 /* Ensure there was no race condition. */
3147 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
3148 DEBUG(5,("open_directory: stat struct differs for "
3150 smb_fname_str_dbg(smb_dname
)));
3152 file_free(req
, fsp
);
3153 return NT_STATUS_ACCESS_DENIED
;
3156 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
3157 conn
->connectpath
, smb_dname
,
3161 DEBUG(0, ("open_directory: Could not get share mode lock for "
3162 "%s\n", smb_fname_str_dbg(smb_dname
)));
3164 file_free(req
, fsp
);
3165 return NT_STATUS_SHARING_VIOLATION
;
3168 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
3169 access_mask
, share_access
,
3170 create_options
, &dir_existed
);
3172 if (!NT_STATUS_IS_OK(status
)) {
3175 file_free(req
, fsp
);
3179 set_share_mode(lck
, fsp
, get_current_uid(conn
),
3180 req
? req
->mid
: 0, NO_OPLOCK
);
3182 /* For directories the delete on close bit at open time seems
3183 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3184 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3185 status
= can_set_delete_on_close(fsp
, 0);
3186 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
3189 file_free(req
, fsp
);
3193 if (NT_STATUS_IS_OK(status
)) {
3194 /* Note that here we set the *inital* delete on close flag,
3195 not the regular one. The magic gets handled in close. */
3196 fsp
->initial_delete_on_close
= True
;
3207 return NT_STATUS_OK
;
3210 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
3211 struct smb_filename
*smb_dname
)
3216 status
= SMB_VFS_CREATE_FILE(
3219 0, /* root_dir_fid */
3220 smb_dname
, /* fname */
3221 FILE_READ_ATTRIBUTES
, /* access_mask */
3222 FILE_SHARE_NONE
, /* share_access */
3223 FILE_CREATE
, /* create_disposition*/
3224 FILE_DIRECTORY_FILE
, /* create_options */
3225 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
3226 0, /* oplock_request */
3227 0, /* allocation_size */
3228 0, /* private_flags */
3234 if (NT_STATUS_IS_OK(status
)) {
3235 close_file(req
, fsp
, NORMAL_CLOSE
);
3241 /****************************************************************************
3242 Receive notification that one of our open files has been renamed by another
3244 ****************************************************************************/
3246 void msg_file_was_renamed(struct messaging_context
*msg
,
3249 struct server_id server_id
,
3253 char *frm
= (char *)data
->data
;
3255 const char *sharepath
;
3256 const char *base_name
;
3257 const char *stream_name
;
3258 struct smb_filename
*smb_fname
= NULL
;
3259 size_t sp_len
, bn_len
;
3261 struct smbd_server_connection
*sconn
=
3262 talloc_get_type_abort(private_data
,
3263 struct smbd_server_connection
);
3265 if (data
->data
== NULL
3266 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3267 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3268 (int)data
->length
));
3272 /* Unpack the message. */
3273 pull_file_id_24(frm
, &id
);
3274 sharepath
= &frm
[24];
3275 sp_len
= strlen(sharepath
);
3276 base_name
= sharepath
+ sp_len
+ 1;
3277 bn_len
= strlen(base_name
);
3278 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3280 /* stream_name must always be NULL if there is no stream. */
3281 if (stream_name
[0] == '\0') {
3285 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
3286 stream_name
, NULL
, &smb_fname
);
3287 if (!NT_STATUS_IS_OK(status
)) {
3291 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3293 sharepath
, smb_fname_str_dbg(smb_fname
),
3294 file_id_string_tos(&id
)));
3296 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3297 fsp
= file_find_di_next(fsp
)) {
3298 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3300 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3301 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
3302 smb_fname_str_dbg(smb_fname
)));
3303 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3304 if (!NT_STATUS_IS_OK(status
)) {
3309 /* Now we have the complete path we can work out if this is
3310 actually within this share and adjust newname accordingly. */
3311 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3312 "not sharepath %s) "
3313 "%s from %s -> %s\n",
3314 fsp
->conn
->connectpath
,
3318 smb_fname_str_dbg(smb_fname
)));
3322 TALLOC_FREE(smb_fname
);
3327 * If a main file is opened for delete, all streams need to be checked for
3328 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3329 * If that works, delete them all by setting the delete on close and close.
3332 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3335 struct stream_struct
*stream_info
= NULL
;
3336 files_struct
**streams
= NULL
;
3338 unsigned int num_streams
= 0;
3339 TALLOC_CTX
*frame
= talloc_stackframe();
3342 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3343 &num_streams
, &stream_info
);
3345 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3346 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3347 DEBUG(10, ("no streams around\n"));
3349 return NT_STATUS_OK
;
3352 if (!NT_STATUS_IS_OK(status
)) {
3353 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3354 nt_errstr(status
)));
3358 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3361 if (num_streams
== 0) {
3363 return NT_STATUS_OK
;
3366 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3367 if (streams
== NULL
) {
3368 DEBUG(0, ("talloc failed\n"));
3369 status
= NT_STATUS_NO_MEMORY
;
3373 for (i
=0; i
<num_streams
; i
++) {
3374 struct smb_filename
*smb_fname
= NULL
;
3376 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3381 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3382 stream_info
[i
].name
,
3384 if (!NT_STATUS_IS_OK(status
)) {
3388 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3389 DEBUG(10, ("Unable to stat stream: %s\n",
3390 smb_fname_str_dbg(smb_fname
)));
3393 status
= SMB_VFS_CREATE_FILE(
3396 0, /* root_dir_fid */
3397 smb_fname
, /* fname */
3398 DELETE_ACCESS
, /* access_mask */
3399 (FILE_SHARE_READ
| /* share_access */
3400 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3401 FILE_OPEN
, /* create_disposition*/
3402 0, /* create_options */
3403 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3404 0, /* oplock_request */
3405 0, /* allocation_size */
3406 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3409 &streams
[i
], /* result */
3412 if (!NT_STATUS_IS_OK(status
)) {
3413 DEBUG(10, ("Could not open stream %s: %s\n",
3414 smb_fname_str_dbg(smb_fname
),
3415 nt_errstr(status
)));
3417 TALLOC_FREE(smb_fname
);
3420 TALLOC_FREE(smb_fname
);
3424 * don't touch the variable "status" beyond this point :-)
3427 for (i
-= 1 ; i
>= 0; i
--) {
3428 if (streams
[i
] == NULL
) {
3432 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3433 fsp_str_dbg(streams
[i
])));
3434 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3442 /*********************************************************************
3443 Create a default ACL by inheriting from the parent. If no inheritance
3444 from the parent available, don't set anything. This will leave the actual
3445 permissions the new file or directory already got from the filesystem
3446 as the NT ACL when read.
3447 *********************************************************************/
3449 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
3451 TALLOC_CTX
*frame
= talloc_stackframe();
3452 char *parent_name
= NULL
;
3453 struct security_descriptor
*parent_desc
= NULL
;
3454 NTSTATUS status
= NT_STATUS_OK
;
3455 struct security_descriptor
*psd
= NULL
;
3456 const struct dom_sid
*owner_sid
= NULL
;
3457 const struct dom_sid
*group_sid
= NULL
;
3458 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
3459 struct security_token
*token
= fsp
->conn
->session_info
->security_token
;
3460 bool inherit_owner
= lp_inherit_owner(SNUM(fsp
->conn
));
3461 bool inheritable_components
= false;
3462 bool try_builtin_administrators
= false;
3463 const struct dom_sid
*BA_U_sid
= NULL
;
3464 const struct dom_sid
*BA_G_sid
= NULL
;
3465 bool try_system
= false;
3466 const struct dom_sid
*SY_U_sid
= NULL
;
3467 const struct dom_sid
*SY_G_sid
= NULL
;
3470 if (!parent_dirname(frame
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
3472 return NT_STATUS_NO_MEMORY
;
3475 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
3477 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
3480 if (!NT_STATUS_IS_OK(status
)) {
3485 inheritable_components
= sd_has_inheritable_components(parent_desc
,
3488 if (!inheritable_components
&& !inherit_owner
) {
3490 /* Nothing to inherit and not setting owner. */
3491 return NT_STATUS_OK
;
3494 /* Create an inherited descriptor from the parent. */
3496 if (DEBUGLEVEL
>= 10) {
3497 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3498 fsp_str_dbg(fsp
) ));
3499 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
3502 /* Inherit from parent descriptor if "inherit owner" set. */
3503 if (inherit_owner
) {
3504 owner_sid
= parent_desc
->owner_sid
;
3505 group_sid
= parent_desc
->group_sid
;
3508 if (owner_sid
== NULL
) {
3509 if (security_token_has_builtin_administrators(token
)) {
3510 try_builtin_administrators
= true;
3511 } else if (security_token_is_system(token
)) {
3512 try_builtin_administrators
= true;
3517 if (group_sid
== NULL
&&
3518 token
->num_sids
== PRIMARY_GROUP_SID_INDEX
)
3520 if (security_token_is_system(token
)) {
3521 try_builtin_administrators
= true;
3526 if (try_builtin_administrators
) {
3531 ok
= sids_to_unixids(&global_sid_Builtin_Administrators
, 1, &ids
);
3535 BA_U_sid
= &global_sid_Builtin_Administrators
;
3536 BA_G_sid
= &global_sid_Builtin_Administrators
;
3539 BA_U_sid
= &global_sid_Builtin_Administrators
;
3542 BA_G_sid
= &global_sid_Builtin_Administrators
;
3555 ok
= sids_to_unixids(&global_sid_System
, 1, &ids
);
3559 SY_U_sid
= &global_sid_System
;
3560 SY_G_sid
= &global_sid_System
;
3563 SY_U_sid
= &global_sid_System
;
3566 SY_G_sid
= &global_sid_System
;
3574 if (owner_sid
== NULL
) {
3575 owner_sid
= BA_U_sid
;
3578 if (owner_sid
== NULL
) {
3579 owner_sid
= SY_U_sid
;
3582 if (group_sid
== NULL
) {
3583 group_sid
= SY_G_sid
;
3586 if (try_system
&& group_sid
== NULL
) {
3587 group_sid
= BA_G_sid
;
3590 if (owner_sid
== NULL
) {
3591 owner_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3593 if (group_sid
== NULL
) {
3594 if (token
->num_sids
== PRIMARY_GROUP_SID_INDEX
) {
3595 group_sid
= &token
->sids
[PRIMARY_USER_SID_INDEX
];
3597 group_sid
= &token
->sids
[PRIMARY_GROUP_SID_INDEX
];
3601 status
= se_create_child_secdesc(frame
,
3608 if (!NT_STATUS_IS_OK(status
)) {
3613 /* If inheritable_components == false,
3614 se_create_child_secdesc()
3615 creates a security desriptor with a NULL dacl
3616 entry, but with SEC_DESC_DACL_PRESENT. We need
3617 to remove that flag. */
3619 if (!inheritable_components
) {
3620 security_info_sent
&= ~SECINFO_DACL
;
3621 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
3624 if (DEBUGLEVEL
>= 10) {
3625 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3626 fsp_str_dbg(fsp
) ));
3627 NDR_PRINT_DEBUG(security_descriptor
, psd
);
3630 if (inherit_owner
) {
3631 /* We need to be root to force this. */
3634 status
= SMB_VFS_FSET_NT_ACL(fsp
,
3637 if (inherit_owner
) {
3645 * Wrapper around open_file_ntcreate and open_directory
3648 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3649 struct smb_request
*req
,
3650 struct smb_filename
*smb_fname
,
3651 uint32_t access_mask
,
3652 uint32_t share_access
,
3653 uint32_t create_disposition
,
3654 uint32_t create_options
,
3655 uint32_t file_attributes
,
3656 uint32_t oplock_request
,
3657 uint64_t allocation_size
,
3658 uint32_t private_flags
,
3659 struct security_descriptor
*sd
,
3660 struct ea_list
*ea_list
,
3662 files_struct
**result
,
3665 int info
= FILE_WAS_OPENED
;
3666 files_struct
*base_fsp
= NULL
;
3667 files_struct
*fsp
= NULL
;
3670 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3671 "file_attributes = 0x%x, share_access = 0x%x, "
3672 "create_disposition = 0x%x create_options = 0x%x "
3673 "oplock_request = 0x%x private_flags = 0x%x "
3674 "ea_list = 0x%p, sd = 0x%p, "
3676 (unsigned int)access_mask
,
3677 (unsigned int)file_attributes
,
3678 (unsigned int)share_access
,
3679 (unsigned int)create_disposition
,
3680 (unsigned int)create_options
,
3681 (unsigned int)oplock_request
,
3682 (unsigned int)private_flags
,
3683 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3685 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3686 status
= NT_STATUS_NOT_SUPPORTED
;
3690 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3691 status
= NT_STATUS_INVALID_PARAMETER
;
3696 oplock_request
|= INTERNAL_OPEN_ONLY
;
3699 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3700 && (access_mask
& DELETE_ACCESS
)
3701 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3703 * We can't open a file with DELETE access if any of the
3704 * streams is open without FILE_SHARE_DELETE
3706 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3708 if (!NT_STATUS_IS_OK(status
)) {
3713 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3714 !security_token_has_privilege(get_current_nttok(conn
),
3715 SEC_PRIV_SECURITY
)) {
3716 DEBUG(10, ("create_file_unixpath: open on %s "
3717 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3718 smb_fname_str_dbg(smb_fname
)));
3719 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3723 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3724 && is_ntfs_stream_smb_fname(smb_fname
)
3725 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3726 uint32 base_create_disposition
;
3727 struct smb_filename
*smb_fname_base
= NULL
;
3729 if (create_options
& FILE_DIRECTORY_FILE
) {
3730 status
= NT_STATUS_NOT_A_DIRECTORY
;
3734 switch (create_disposition
) {
3736 base_create_disposition
= FILE_OPEN
;
3739 base_create_disposition
= FILE_OPEN_IF
;
3743 /* Create an smb_filename with stream_name == NULL. */
3744 status
= create_synthetic_smb_fname(talloc_tos(),
3745 smb_fname
->base_name
,
3748 if (!NT_STATUS_IS_OK(status
)) {
3752 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3753 DEBUG(10, ("Unable to stat stream: %s\n",
3754 smb_fname_str_dbg(smb_fname_base
)));
3757 /* Open the base file. */
3758 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3761 | FILE_SHARE_DELETE
,
3762 base_create_disposition
,
3763 0, 0, 0, 0, 0, NULL
, NULL
,
3765 TALLOC_FREE(smb_fname_base
);
3767 if (!NT_STATUS_IS_OK(status
)) {
3768 DEBUG(10, ("create_file_unixpath for base %s failed: "
3769 "%s\n", smb_fname
->base_name
,
3770 nt_errstr(status
)));
3773 /* we don't need to low level fd */
3778 * If it's a request for a directory open, deal with it separately.
3781 if (create_options
& FILE_DIRECTORY_FILE
) {
3783 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3784 status
= NT_STATUS_INVALID_PARAMETER
;
3788 /* Can't open a temp directory. IFS kit test. */
3789 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3790 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3791 status
= NT_STATUS_INVALID_PARAMETER
;
3796 * We will get a create directory here if the Win32
3797 * app specified a security descriptor in the
3798 * CreateDirectory() call.
3802 status
= open_directory(
3803 conn
, req
, smb_fname
, access_mask
, share_access
,
3804 create_disposition
, create_options
, file_attributes
,
3809 * Ordinary file case.
3812 status
= file_new(req
, conn
, &fsp
);
3813 if(!NT_STATUS_IS_OK(status
)) {
3817 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3818 if (!NT_STATUS_IS_OK(status
)) {
3824 * We're opening the stream element of a
3825 * base_fsp we already opened. Set up the
3828 fsp
->base_fsp
= base_fsp
;
3831 if (allocation_size
) {
3832 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
3836 status
= open_file_ntcreate(conn
,
3848 if(!NT_STATUS_IS_OK(status
)) {
3849 file_free(req
, fsp
);
3853 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3855 /* A stream open never opens a directory */
3858 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3863 * Fail the open if it was explicitly a non-directory
3867 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3868 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3873 status
= open_directory(
3874 conn
, req
, smb_fname
, access_mask
,
3875 share_access
, create_disposition
,
3876 create_options
, file_attributes
,
3881 if (!NT_STATUS_IS_OK(status
)) {
3885 fsp
->base_fsp
= base_fsp
;
3887 if ((ea_list
!= NULL
) &&
3888 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3889 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3890 if (!NT_STATUS_IS_OK(status
)) {
3895 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3896 status
= NT_STATUS_ACCESS_DENIED
;
3900 /* Save the requested allocation size. */
3901 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3903 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3904 fsp
->initial_allocation_size
= smb_roundup(
3905 fsp
->conn
, allocation_size
);
3906 if (fsp
->is_directory
) {
3907 /* Can't set allocation size on a directory. */
3908 status
= NT_STATUS_ACCESS_DENIED
;
3911 if (vfs_allocate_file_space(
3912 fsp
, fsp
->initial_allocation_size
) == -1) {
3913 status
= NT_STATUS_DISK_FULL
;
3917 fsp
->initial_allocation_size
= smb_roundup(
3918 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3921 fsp
->initial_allocation_size
= 0;
3924 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
3925 fsp
->base_fsp
== NULL
) {
3928 * According to the MS documentation, the only time the security
3929 * descriptor is applied to the opened file is iff we *created* the
3930 * file; an existing file stays the same.
3932 * Also, it seems (from observation) that you can open the file with
3933 * any access mask but you can still write the sd. We need to override
3934 * the granted access before we call set_sd
3935 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3938 uint32_t sec_info_sent
;
3939 uint32_t saved_access_mask
= fsp
->access_mask
;
3941 sec_info_sent
= get_sec_info(sd
);
3943 fsp
->access_mask
= FILE_GENERIC_ALL
;
3945 if (sec_info_sent
& (SECINFO_OWNER
|
3949 status
= set_sd(fsp
, sd
, sec_info_sent
);
3952 fsp
->access_mask
= saved_access_mask
;
3954 if (!NT_STATUS_IS_OK(status
)) {
3957 } else if (lp_inherit_acls(SNUM(conn
))) {
3958 /* Inherit from parent. Errors here are not fatal. */
3959 status
= inherit_new_acl(fsp
);
3960 if (!NT_STATUS_IS_OK(status
)) {
3961 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3963 nt_errstr(status
) ));
3968 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3971 if (pinfo
!= NULL
) {
3975 smb_fname
->st
= fsp
->fsp_name
->st
;
3977 return NT_STATUS_OK
;
3980 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3983 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3985 * The close_file below will close
3990 close_file(req
, fsp
, ERROR_CLOSE
);
3993 if (base_fsp
!= NULL
) {
3994 close_file(req
, base_fsp
, ERROR_CLOSE
);
4001 * Calculate the full path name given a relative fid.
4003 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
4004 struct smb_request
*req
,
4005 uint16_t root_dir_fid
,
4006 const struct smb_filename
*smb_fname
,
4007 struct smb_filename
**smb_fname_out
)
4009 files_struct
*dir_fsp
;
4010 char *parent_fname
= NULL
;
4011 char *new_base_name
= NULL
;
4014 if (root_dir_fid
== 0 || !smb_fname
) {
4015 status
= NT_STATUS_INTERNAL_ERROR
;
4019 dir_fsp
= file_fsp(req
, root_dir_fid
);
4021 if (dir_fsp
== NULL
) {
4022 status
= NT_STATUS_INVALID_HANDLE
;
4026 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
4027 status
= NT_STATUS_INVALID_HANDLE
;
4031 if (!dir_fsp
->is_directory
) {
4034 * Check to see if this is a mac fork of some kind.
4037 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
4038 is_ntfs_stream_smb_fname(smb_fname
)) {
4039 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4044 we need to handle the case when we get a
4045 relative open relative to a file and the
4046 pathname is blank - this is a reopen!
4047 (hint from demyn plantenberg)
4050 status
= NT_STATUS_INVALID_HANDLE
;
4054 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
4056 * We're at the toplevel dir, the final file name
4057 * must not contain ./, as this is filtered out
4058 * normally by srvstr_get_path and unix_convert
4059 * explicitly rejects paths containing ./.
4061 parent_fname
= talloc_strdup(talloc_tos(), "");
4062 if (parent_fname
== NULL
) {
4063 status
= NT_STATUS_NO_MEMORY
;
4067 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
4070 * Copy in the base directory name.
4073 parent_fname
= talloc_array(talloc_tos(), char,
4075 if (parent_fname
== NULL
) {
4076 status
= NT_STATUS_NO_MEMORY
;
4079 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
4083 * Ensure it ends in a '/'.
4084 * We used TALLOC_SIZE +2 to add space for the '/'.
4088 && (parent_fname
[dir_name_len
-1] != '\\')
4089 && (parent_fname
[dir_name_len
-1] != '/')) {
4090 parent_fname
[dir_name_len
] = '/';
4091 parent_fname
[dir_name_len
+1] = '\0';
4095 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
4096 smb_fname
->base_name
);
4097 if (new_base_name
== NULL
) {
4098 status
= NT_STATUS_NO_MEMORY
;
4102 status
= filename_convert(req
,
4104 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
4109 if (!NT_STATUS_IS_OK(status
)) {
4114 TALLOC_FREE(parent_fname
);
4115 TALLOC_FREE(new_base_name
);
4119 NTSTATUS
create_file_default(connection_struct
*conn
,
4120 struct smb_request
*req
,
4121 uint16_t root_dir_fid
,
4122 struct smb_filename
*smb_fname
,
4123 uint32_t access_mask
,
4124 uint32_t share_access
,
4125 uint32_t create_disposition
,
4126 uint32_t create_options
,
4127 uint32_t file_attributes
,
4128 uint32_t oplock_request
,
4129 uint64_t allocation_size
,
4130 uint32_t private_flags
,
4131 struct security_descriptor
*sd
,
4132 struct ea_list
*ea_list
,
4133 files_struct
**result
,
4136 int info
= FILE_WAS_OPENED
;
4137 files_struct
*fsp
= NULL
;
4139 bool stream_name
= false;
4141 DEBUG(10,("create_file: access_mask = 0x%x "
4142 "file_attributes = 0x%x, share_access = 0x%x, "
4143 "create_disposition = 0x%x create_options = 0x%x "
4144 "oplock_request = 0x%x "
4145 "private_flags = 0x%x "
4146 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4148 (unsigned int)access_mask
,
4149 (unsigned int)file_attributes
,
4150 (unsigned int)share_access
,
4151 (unsigned int)create_disposition
,
4152 (unsigned int)create_options
,
4153 (unsigned int)oplock_request
,
4154 (unsigned int)private_flags
,
4155 (unsigned int)root_dir_fid
,
4156 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4159 * Calculate the filename from the root_dir_if if necessary.
4162 if (root_dir_fid
!= 0) {
4163 struct smb_filename
*smb_fname_out
= NULL
;
4164 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
4165 smb_fname
, &smb_fname_out
);
4166 if (!NT_STATUS_IS_OK(status
)) {
4169 smb_fname
= smb_fname_out
;
4173 * Check to see if this is a mac fork of some kind.
4176 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
4178 enum FAKE_FILE_TYPE fake_file_type
;
4180 fake_file_type
= is_fake_file(smb_fname
);
4182 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
4185 * Here we go! support for changing the disk quotas
4188 * We need to fake up to open this MAGIC QUOTA file
4189 * and return a valid FID.
4191 * w2k close this file directly after openening xp
4192 * also tries a QUERY_FILE_INFO on the file and then
4195 status
= open_fake_file(req
, conn
, req
->vuid
,
4196 fake_file_type
, smb_fname
,
4198 if (!NT_STATUS_IS_OK(status
)) {
4202 ZERO_STRUCT(smb_fname
->st
);
4206 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
4207 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4212 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
4214 smb_fname
->stream_name
= NULL
;
4215 /* We have to handle this error here. */
4216 if (create_options
& FILE_DIRECTORY_FILE
) {
4217 status
= NT_STATUS_NOT_A_DIRECTORY
;
4220 if (lp_posix_pathnames()) {
4221 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
4223 ret
= SMB_VFS_STAT(conn
, smb_fname
);
4226 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
4227 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4232 status
= create_file_unixpath(
4233 conn
, req
, smb_fname
, access_mask
, share_access
,
4234 create_disposition
, create_options
, file_attributes
,
4235 oplock_request
, allocation_size
, private_flags
,
4239 if (!NT_STATUS_IS_OK(status
)) {
4244 DEBUG(10, ("create_file: info=%d\n", info
));
4247 if (pinfo
!= NULL
) {
4250 return NT_STATUS_OK
;
4253 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
4256 close_file(req
, fsp
, ERROR_CLOSE
);