2 Unix SMB/CIFS implementation.
3 file opening and share modes
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2004
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "fake_file.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../librpc/gen_ndr/open_files.h"
34 extern const struct generic_mapping file_generic_mapping
;
36 struct deferred_open_record
{
37 bool delayed_for_oplocks
;
42 /****************************************************************************
43 If the requester wanted DELETE_ACCESS and was rejected because
44 the file ACL didn't include DELETE_ACCESS, see if the parent ACL
46 ****************************************************************************/
48 static bool parent_override_delete(connection_struct
*conn
,
49 const struct smb_filename
*smb_fname
,
51 uint32_t rejected_mask
)
53 if ((access_mask
& DELETE_ACCESS
) &&
54 (rejected_mask
& DELETE_ACCESS
) &&
55 can_delete_file_in_directory(conn
, smb_fname
)) {
61 /****************************************************************************
62 Check if we have open rights.
63 ****************************************************************************/
65 NTSTATUS
smbd_check_access_rights(struct connection_struct
*conn
,
66 const struct smb_filename
*smb_fname
,
69 /* Check if we have rights to open. */
71 struct security_descriptor
*sd
= NULL
;
72 uint32_t rejected_share_access
;
73 uint32_t rejected_mask
= access_mask
;
75 rejected_share_access
= access_mask
& ~(conn
->share_access
);
77 if (rejected_share_access
) {
78 DEBUG(10, ("smbd_check_access_rights: rejected share access 0x%x "
80 (unsigned int)access_mask
,
81 smb_fname_str_dbg(smb_fname
),
82 (unsigned int)rejected_share_access
));
83 return NT_STATUS_ACCESS_DENIED
;
86 if (get_current_uid(conn
) == (uid_t
)0) {
87 /* I'm sorry sir, I didn't know you were root... */
88 DEBUG(10,("smbd_check_access_rights: root override "
89 "on %s. Granting 0x%x\n",
90 smb_fname_str_dbg(smb_fname
),
91 (unsigned int)access_mask
));
95 if ((access_mask
& DELETE_ACCESS
) && !lp_acl_check_permissions(SNUM(conn
))) {
96 DEBUG(10,("smbd_check_access_rights: not checking ACL "
97 "on DELETE_ACCESS on file %s. Granting 0x%x\n",
98 smb_fname_str_dbg(smb_fname
),
99 (unsigned int)access_mask
));
103 if (access_mask
== DELETE_ACCESS
&&
104 VALID_STAT(smb_fname
->st
) &&
105 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
106 /* We can always delete a symlink. */
107 DEBUG(10,("smbd_check_access_rights: not checking ACL "
108 "on DELETE_ACCESS on symlink %s.\n",
109 smb_fname_str_dbg(smb_fname
) ));
113 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
118 if (!NT_STATUS_IS_OK(status
)) {
119 DEBUG(10, ("smbd_check_access_rights: Could not get acl "
121 smb_fname_str_dbg(smb_fname
),
124 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
132 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
133 * owner WRITE_DAC and READ_CONTROL.
135 status
= se_file_access_check(sd
,
136 get_current_nttok(conn
),
138 (access_mask
& ~FILE_READ_ATTRIBUTES
),
141 DEBUG(10,("smbd_check_access_rights: file %s requesting "
142 "0x%x returning 0x%x (%s)\n",
143 smb_fname_str_dbg(smb_fname
),
144 (unsigned int)access_mask
,
145 (unsigned int)rejected_mask
,
146 nt_errstr(status
) ));
148 if (!NT_STATUS_IS_OK(status
)) {
149 if (DEBUGLEVEL
>= 10) {
150 DEBUG(10,("smbd_check_access_rights: acl for %s is:\n",
151 smb_fname_str_dbg(smb_fname
) ));
152 NDR_PRINT_DEBUG(security_descriptor
, sd
);
158 if (NT_STATUS_IS_OK(status
) ||
159 !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
163 /* Here we know status == NT_STATUS_ACCESS_DENIED. */
167 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
168 (rejected_mask
& FILE_WRITE_ATTRIBUTES
) &&
169 !lp_store_dos_attributes(SNUM(conn
)) &&
170 (lp_map_readonly(SNUM(conn
)) ||
171 lp_map_archive(SNUM(conn
)) ||
172 lp_map_hidden(SNUM(conn
)) ||
173 lp_map_system(SNUM(conn
)))) {
174 rejected_mask
&= ~FILE_WRITE_ATTRIBUTES
;
176 DEBUG(10,("smbd_check_access_rights: "
178 "FILE_WRITE_ATTRIBUTES "
180 smb_fname_str_dbg(smb_fname
)));
183 if (parent_override_delete(conn
,
187 /* Were we trying to do an open
188 * for delete and didn't get DELETE
189 * access (only) ? Check if the
190 * directory allows DELETE_CHILD.
192 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
195 rejected_mask
&= ~DELETE_ACCESS
;
197 DEBUG(10,("smbd_check_access_rights: "
201 smb_fname_str_dbg(smb_fname
)));
204 if (rejected_mask
!= 0) {
205 return NT_STATUS_ACCESS_DENIED
;
210 static NTSTATUS
check_parent_access(struct connection_struct
*conn
,
211 struct smb_filename
*smb_fname
,
212 uint32_t access_mask
)
215 char *parent_dir
= NULL
;
216 struct security_descriptor
*parent_sd
= NULL
;
217 uint32_t access_granted
= 0;
219 if (!parent_dirname(talloc_tos(),
220 smb_fname
->base_name
,
223 return NT_STATUS_NO_MEMORY
;
226 if (get_current_uid(conn
) == (uid_t
)0) {
227 /* I'm sorry sir, I didn't know you were root... */
228 DEBUG(10,("check_parent_access: root override "
229 "on %s. Granting 0x%x\n",
230 smb_fname_str_dbg(smb_fname
),
231 (unsigned int)access_mask
));
235 status
= SMB_VFS_GET_NT_ACL(conn
,
240 if (!NT_STATUS_IS_OK(status
)) {
241 DEBUG(5,("check_parent_access: SMB_VFS_GET_NT_ACL failed for "
242 "%s with error %s\n",
249 * Never test FILE_READ_ATTRIBUTES. se_file_access_check() also takes care of
250 * owner WRITE_DAC and READ_CONTROL.
252 status
= se_file_access_check(parent_sd
,
253 get_current_nttok(conn
),
255 (access_mask
& ~FILE_READ_ATTRIBUTES
),
257 if(!NT_STATUS_IS_OK(status
)) {
258 DEBUG(5,("check_parent_access: access check "
259 "on directory %s for "
260 "path %s for mask 0x%x returned (0x%x) %s\n",
262 smb_fname
->base_name
,
265 nt_errstr(status
) ));
272 /****************************************************************************
273 fd support routines - attempt to do a dos_open.
274 ****************************************************************************/
276 static NTSTATUS
fd_open(struct connection_struct
*conn
,
281 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
282 NTSTATUS status
= NT_STATUS_OK
;
286 * Never follow symlinks on a POSIX client. The
287 * client should be doing this.
290 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
295 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
296 if (fsp
->fh
->fd
== -1) {
297 int posix_errno
= errno
;
299 #if defined(ENOTSUP) && defined(OSF1)
300 /* handle special Tru64 errno */
301 if (errno
== ENOTSUP
) {
306 /* fix broken NetBSD errno */
307 if (errno
== EFTYPE
) {
311 /* fix broken FreeBSD errno */
312 if (errno
== EMLINK
) {
315 #endif /* O_NOFOLLOW */
316 status
= map_nt_error_from_unix(posix_errno
);
317 if (errno
== EMFILE
) {
318 static time_t last_warned
= 0L;
320 if (time((time_t *) NULL
) > last_warned
) {
321 DEBUG(0,("Too many open files, unable "
322 "to open more! smbd's max "
324 lp_max_open_files()));
325 last_warned
= time((time_t *) NULL
);
331 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
332 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
333 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
338 /****************************************************************************
339 Close the file associated with a fsp.
340 ****************************************************************************/
342 NTSTATUS
fd_close(files_struct
*fsp
)
349 if (fsp
->fh
->fd
== -1) {
350 return NT_STATUS_OK
; /* What we used to call a stat open. */
352 if (fsp
->fh
->ref_count
> 1) {
353 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
356 ret
= SMB_VFS_CLOSE(fsp
);
359 return map_nt_error_from_unix(errno
);
364 /****************************************************************************
365 Change the ownership of a file to that of the parent directory.
366 Do this by fd if possible.
367 ****************************************************************************/
369 void change_file_owner_to_parent(connection_struct
*conn
,
370 const char *inherit_from_dir
,
373 struct smb_filename
*smb_fname_parent
= NULL
;
377 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
378 NULL
, NULL
, &smb_fname_parent
);
379 if (!NT_STATUS_IS_OK(status
)) {
383 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
385 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
386 "directory %s. Error was %s\n",
387 smb_fname_str_dbg(smb_fname_parent
),
389 TALLOC_FREE(smb_fname_parent
);
393 if (smb_fname_parent
->st
.st_ex_uid
== fsp
->fsp_name
->st
.st_ex_uid
) {
394 /* Already this uid - no need to change. */
395 DEBUG(10,("change_file_owner_to_parent: file %s "
396 "is already owned by uid %d\n",
398 (int)fsp
->fsp_name
->st
.st_ex_uid
));
399 TALLOC_FREE(smb_fname_parent
);
404 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
407 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
408 "file %s to parent directory uid %u. Error "
409 "was %s\n", fsp_str_dbg(fsp
),
410 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
413 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
414 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
415 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
416 /* Ensure the uid entry is updated. */
417 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
420 TALLOC_FREE(smb_fname_parent
);
423 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
424 const char *inherit_from_dir
,
426 SMB_STRUCT_STAT
*psbuf
)
428 struct smb_filename
*smb_fname_parent
= NULL
;
429 struct smb_filename
*smb_fname_cwd
= NULL
;
430 char *saved_dir
= NULL
;
431 TALLOC_CTX
*ctx
= talloc_tos();
432 NTSTATUS status
= NT_STATUS_OK
;
435 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
437 if (!NT_STATUS_IS_OK(status
)) {
441 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
443 status
= map_nt_error_from_unix(errno
);
444 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
445 "directory %s. Error was %s\n",
446 smb_fname_str_dbg(smb_fname_parent
),
451 /* We've already done an lstat into psbuf, and we know it's a
452 directory. If we can cd into the directory and the dev/ino
453 are the same then we can safely chown without races as
454 we're locking the directory in place by being in it. This
455 should work on any UNIX (thanks tridge :-). JRA.
458 saved_dir
= vfs_GetWd(ctx
,conn
);
460 status
= map_nt_error_from_unix(errno
);
461 DEBUG(0,("change_dir_owner_to_parent: failed to get "
462 "current working directory. Error was %s\n",
467 /* Chdir into the new path. */
468 if (vfs_ChDir(conn
, fname
) == -1) {
469 status
= map_nt_error_from_unix(errno
);
470 DEBUG(0,("change_dir_owner_to_parent: failed to change "
471 "current working directory to %s. Error "
472 "was %s\n", fname
, strerror(errno
) ));
476 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
478 if (!NT_STATUS_IS_OK(status
)) {
482 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
484 status
= map_nt_error_from_unix(errno
);
485 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
486 "directory '.' (%s) Error was %s\n",
487 fname
, strerror(errno
)));
491 /* Ensure we're pointing at the same place. */
492 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
493 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
) {
494 DEBUG(0,("change_dir_owner_to_parent: "
495 "device/inode on directory %s changed. "
496 "Refusing to chown !\n", fname
));
497 status
= NT_STATUS_ACCESS_DENIED
;
501 if (smb_fname_parent
->st
.st_ex_uid
== smb_fname_cwd
->st
.st_ex_uid
) {
502 /* Already this uid - no need to change. */
503 DEBUG(10,("change_dir_owner_to_parent: directory %s "
504 "is already owned by uid %d\n",
506 (int)smb_fname_cwd
->st
.st_ex_uid
));
507 status
= NT_STATUS_OK
;
512 ret
= SMB_VFS_LCHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
516 status
= map_nt_error_from_unix(errno
);
517 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
518 "directory %s to parent directory uid %u. "
519 "Error was %s\n", fname
,
520 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
523 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
524 "directory %s to parent directory uid %u.\n",
525 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
526 /* Ensure the uid entry is updated. */
527 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
531 vfs_ChDir(conn
,saved_dir
);
533 TALLOC_FREE(smb_fname_parent
);
534 TALLOC_FREE(smb_fname_cwd
);
538 /****************************************************************************
539 Open a file - returning a guaranteed ATOMIC indication of if the
540 file was created or not.
541 ****************************************************************************/
543 static NTSTATUS
fd_open_atomic(struct connection_struct
*conn
,
549 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
550 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
552 *file_created
= false;
554 if (!(flags
& O_CREAT
)) {
556 * We're not creating the file, just pass through.
558 return fd_open(conn
, fsp
, flags
, mode
);
561 if (flags
& O_EXCL
) {
563 * Fail if already exists, just pass through.
565 status
= fd_open(conn
, fsp
, flags
, mode
);
568 * Here we've opened with O_CREAT|O_EXCL. If that went
569 * NT_STATUS_OK, we *know* we created this file.
571 *file_created
= NT_STATUS_IS_OK(status
);
577 * Now it gets tricky. We have O_CREAT, but not O_EXCL.
578 * To know absolutely if we created the file or not,
579 * we can never call O_CREAT without O_EXCL. So if
580 * we think the file existed, try without O_CREAT|O_EXCL.
581 * If we think the file didn't exist, try with
582 * O_CREAT|O_EXCL. Keep bouncing between these two
583 * requests until either the file is created, or
584 * opened. Either way, we keep going until we get
585 * a returnable result (error, or open/create).
589 int curr_flags
= flags
;
592 /* Just try open, do not create. */
593 curr_flags
&= ~(O_CREAT
);
594 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
595 if (NT_STATUS_EQUAL(status
,
596 NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
598 * Someone deleted it in the meantime.
601 file_existed
= false;
602 DEBUG(10,("fd_open_atomic: file %s existed. "
604 smb_fname_str_dbg(fsp
->fsp_name
)));
608 /* Try create exclusively, fail if it exists. */
609 curr_flags
|= O_EXCL
;
610 status
= fd_open(conn
, fsp
, curr_flags
, mode
);
611 if (NT_STATUS_EQUAL(status
,
612 NT_STATUS_OBJECT_NAME_COLLISION
)) {
614 * Someone created it in the meantime.
615 * Retry without O_CREAT.
618 DEBUG(10,("fd_open_atomic: file %s "
619 "did not exist. Retry.\n",
620 smb_fname_str_dbg(fsp
->fsp_name
)));
623 if (NT_STATUS_IS_OK(status
)) {
625 * Here we've opened with O_CREAT|O_EXCL
626 * and got success. We *know* we created
629 *file_created
= true;
632 /* Create is done, or failed. */
638 /****************************************************************************
640 ****************************************************************************/
642 static NTSTATUS
open_file(files_struct
*fsp
,
643 connection_struct
*conn
,
644 struct smb_request
*req
,
645 const char *parent_dir
,
648 uint32 access_mask
, /* client requested access mask. */
649 uint32 open_access_mask
, /* what we're actually using in the open. */
650 bool *p_file_created
)
652 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
653 NTSTATUS status
= NT_STATUS_OK
;
654 int accmode
= (flags
& O_ACCMODE
);
655 int local_flags
= flags
;
656 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
661 /* Check permissions */
664 * This code was changed after seeing a client open request
665 * containing the open mode of (DENY_WRITE/read-only) with
666 * the 'create if not exist' bit set. The previous code
667 * would fail to open the file read only on a read-only share
668 * as it was checking the flags parameter directly against O_RDONLY,
669 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
673 if (!CAN_WRITE(conn
)) {
674 /* It's a read-only share - fail if we wanted to write. */
675 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
676 DEBUG(3,("Permission denied opening %s\n",
677 smb_fname_str_dbg(smb_fname
)));
678 return NT_STATUS_ACCESS_DENIED
;
679 } else if(flags
& O_CREAT
) {
680 /* We don't want to write - but we must make sure that
681 O_CREAT doesn't create the file if we have write
682 access into the directory.
684 flags
&= ~(O_CREAT
|O_EXCL
);
685 local_flags
&= ~(O_CREAT
|O_EXCL
);
690 * This little piece of insanity is inspired by the
691 * fact that an NT client can open a file for O_RDONLY,
692 * but set the create disposition to FILE_EXISTS_TRUNCATE.
693 * If the client *can* write to the file, then it expects to
694 * truncate the file, even though it is opening for readonly.
695 * Quicken uses this stupid trick in backup file creation...
696 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
697 * for helping track this one down. It didn't bite us in 2.0.x
698 * as we always opened files read-write in that release. JRA.
701 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
702 DEBUG(10,("open_file: truncate requested on read-only open "
703 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
704 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
707 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
708 (!file_existed
&& (local_flags
& O_CREAT
)) ||
709 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
714 * We can't actually truncate here as the file may be locked.
715 * open_file_ntcreate will take care of the truncate later. JRA.
718 local_flags
&= ~O_TRUNC
;
720 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
722 * We would block on opening a FIFO with no one else on the
723 * other end. Do what we used to do and add O_NONBLOCK to the
727 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
728 local_flags
|= O_NONBLOCK
;
732 /* Don't create files with Microsoft wildcard characters. */
735 * wildcard characters are allowed in stream names
736 * only test the basefilename
738 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
740 wild
= smb_fname
->base_name
;
742 if ((local_flags
& O_CREAT
) && !file_existed
&&
744 return NT_STATUS_OBJECT_NAME_INVALID
;
747 /* Can we access this file ? */
748 if (!fsp
->base_fsp
) {
749 /* Only do this check on non-stream open. */
751 status
= smbd_check_access_rights(conn
,
754 } else if (local_flags
& O_CREAT
){
755 status
= check_parent_access(conn
,
759 /* File didn't exist and no O_CREAT. */
760 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
762 if (!NT_STATUS_IS_OK(status
)) {
763 DEBUG(10,("open_file: "
767 "smbd_check_access_rights" :
768 "check_parent_access",
769 smb_fname_str_dbg(smb_fname
),
770 nt_errstr(status
) ));
775 /* Actually do the open */
776 status
= fd_open_atomic(conn
, fsp
, local_flags
,
777 unx_mode
, p_file_created
);
778 if (!NT_STATUS_IS_OK(status
)) {
779 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
780 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
781 nt_errstr(status
),local_flags
,flags
));
785 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
787 /* If we have an fd, this stat should succeed. */
788 DEBUG(0,("Error doing fstat on open file %s "
790 smb_fname_str_dbg(smb_fname
),
792 status
= map_nt_error_from_unix(errno
);
797 if (*p_file_created
) {
798 /* We created this file. */
800 bool need_re_stat
= false;
801 /* Do all inheritance work after we've
802 done a successful fstat call and filled
803 in the stat struct in fsp->fsp_name. */
805 /* Inherit the ACL if required */
806 if (lp_inherit_perms(SNUM(conn
))) {
807 inherit_access_posix_acl(conn
, parent_dir
,
808 smb_fname
->base_name
,
813 /* Change the owner if required. */
814 if (lp_inherit_owner(SNUM(conn
))) {
815 change_file_owner_to_parent(conn
, parent_dir
,
821 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
822 /* If we have an fd, this stat should succeed. */
824 DEBUG(0,("Error doing fstat on open file %s "
826 smb_fname_str_dbg(smb_fname
),
831 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
832 FILE_NOTIFY_CHANGE_FILE_NAME
,
833 smb_fname
->base_name
);
836 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
838 /* File must exist for a stat open. */
839 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
842 status
= smbd_check_access_rights(conn
,
846 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
848 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
849 /* This is a POSIX stat open for delete
850 * or rename on a symlink that points
852 DEBUG(10,("open_file: allowing POSIX "
853 "open on bad symlink %s\n",
854 smb_fname_str_dbg(smb_fname
)));
855 status
= NT_STATUS_OK
;
858 if (!NT_STATUS_IS_OK(status
)) {
859 DEBUG(10,("open_file: "
860 "smbd_check_access_rights on file "
862 smb_fname_str_dbg(smb_fname
),
863 nt_errstr(status
) ));
869 * POSIX allows read-only opens of directories. We don't
870 * want to do this (we use a different code path for this)
871 * so catch a directory open and return an EISDIR. JRA.
874 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
877 return NT_STATUS_FILE_IS_A_DIRECTORY
;
880 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
881 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
882 fsp
->file_pid
= req
? req
->smbpid
: 0;
883 fsp
->can_lock
= True
;
884 fsp
->can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
887 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
888 fsp
->print_file
= NULL
;
889 fsp
->modified
= False
;
890 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
891 fsp
->is_directory
= False
;
892 if (conn
->aio_write_behind_list
&&
893 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
894 conn
->case_sensitive
)) {
895 fsp
->aio_write_behind
= True
;
898 fsp
->wcp
= NULL
; /* Write cache pointer. */
900 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
901 conn
->session_info
->unix_info
->unix_name
,
902 smb_fname_str_dbg(smb_fname
),
903 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
904 conn
->num_files_open
));
910 /****************************************************************************
911 Check if we can open a file with a share mode.
912 Returns True if conflict, False if not.
913 ****************************************************************************/
915 static bool share_conflict(struct share_mode_entry
*entry
,
919 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
920 "entry->share_access = 0x%x, "
921 "entry->private_options = 0x%x\n",
922 (unsigned int)entry
->access_mask
,
923 (unsigned int)entry
->share_access
,
924 (unsigned int)entry
->private_options
));
926 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
927 (unsigned int)access_mask
, (unsigned int)share_access
));
929 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
933 DELETE_ACCESS
)) == 0) {
934 DEBUG(10,("share_conflict: No conflict due to "
935 "entry->access_mask = 0x%x\n",
936 (unsigned int)entry
->access_mask
));
940 if ((access_mask
& (FILE_WRITE_DATA
|
944 DELETE_ACCESS
)) == 0) {
945 DEBUG(10,("share_conflict: No conflict due to "
946 "access_mask = 0x%x\n",
947 (unsigned int)access_mask
));
951 #if 1 /* JRA TEST - Superdebug. */
952 #define CHECK_MASK(num, am, right, sa, share) \
953 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
954 (unsigned int)(num), (unsigned int)(am), \
955 (unsigned int)(right), (unsigned int)(am)&(right) )); \
956 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
957 (unsigned int)(num), (unsigned int)(sa), \
958 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
959 if (((am) & (right)) && !((sa) & (share))) { \
960 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
961 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
962 (unsigned int)(share) )); \
966 #define CHECK_MASK(num, am, right, sa, share) \
967 if (((am) & (right)) && !((sa) & (share))) { \
968 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
969 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
970 (unsigned int)(share) )); \
975 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
976 share_access
, FILE_SHARE_WRITE
);
977 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
978 entry
->share_access
, FILE_SHARE_WRITE
);
980 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
981 share_access
, FILE_SHARE_READ
);
982 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
983 entry
->share_access
, FILE_SHARE_READ
);
985 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
986 share_access
, FILE_SHARE_DELETE
);
987 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
988 entry
->share_access
, FILE_SHARE_DELETE
);
990 DEBUG(10,("share_conflict: No conflict.\n"));
994 #if defined(DEVELOPER)
995 static void validate_my_share_entries(struct smbd_server_connection
*sconn
,
997 struct share_mode_entry
*share_entry
)
999 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
1002 if (!serverid_equal(&self
, &share_entry
->pid
)) {
1006 if (is_deferred_open_entry(share_entry
) &&
1007 !open_was_deferred(sconn
, share_entry
->op_mid
)) {
1008 char *str
= talloc_asprintf(talloc_tos(),
1009 "Got a deferred entry without a request: "
1011 share_mode_str(talloc_tos(), num
, share_entry
));
1015 if (!is_valid_share_mode_entry(share_entry
)) {
1019 fsp
= file_find_dif(sconn
, share_entry
->id
,
1020 share_entry
->share_file_id
);
1022 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1023 share_mode_str(talloc_tos(), num
, share_entry
) ));
1024 smb_panic("validate_my_share_entries: Cannot match a "
1025 "share entry with an open file\n");
1028 if (is_deferred_open_entry(share_entry
)) {
1032 if ((share_entry
->op_type
== NO_OPLOCK
) &&
1033 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
1034 /* Someone has already written to it, but I haven't yet
1039 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
1048 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
1049 share_mode_str(talloc_tos(), num
, share_entry
) ));
1050 str
= talloc_asprintf(talloc_tos(),
1051 "validate_my_share_entries: "
1052 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
1053 fsp
->fsp_name
->base_name
,
1054 (unsigned int)fsp
->oplock_type
,
1055 (unsigned int)share_entry
->op_type
);
1061 bool is_stat_open(uint32 access_mask
)
1063 return (access_mask
&&
1064 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
1065 FILE_WRITE_ATTRIBUTES
))==0) &&
1066 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
1067 FILE_WRITE_ATTRIBUTES
)) != 0));
1070 /****************************************************************************
1071 Deal with share modes
1072 Invarient: Share mode must be locked on entry and exit.
1073 Returns -1 on error, or number of share modes on success (may be zero).
1074 ****************************************************************************/
1076 static NTSTATUS
open_mode_check(connection_struct
*conn
,
1077 struct share_mode_lock
*lck
,
1080 uint32 share_access
,
1081 uint32 create_options
,
1086 if(lck
->data
->num_share_modes
== 0) {
1087 return NT_STATUS_OK
;
1090 /* A delete on close prohibits everything */
1092 if (is_delete_on_close_set(lck
, name_hash
)) {
1094 * Check the delete on close token
1095 * is valid. It could have been left
1096 * after a server crash.
1098 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1099 if (!share_mode_stale_pid(lck
->data
, i
)) {
1101 *file_existed
= true;
1103 return NT_STATUS_DELETE_PENDING
;
1106 return NT_STATUS_OK
;
1109 if (is_stat_open(access_mask
)) {
1110 /* Stat open that doesn't trigger oplock breaks or share mode
1111 * checks... ! JRA. */
1112 return NT_STATUS_OK
;
1116 * Check if the share modes will give us access.
1119 #if defined(DEVELOPER)
1120 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1121 validate_my_share_entries(conn
->sconn
, i
,
1122 &lck
->data
->share_modes
[i
]);
1126 /* Now we check the share modes, after any oplock breaks. */
1127 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
1129 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1133 /* someone else has a share lock on it, check to see if we can
1135 if (share_conflict(&lck
->data
->share_modes
[i
],
1136 access_mask
, share_access
)) {
1138 if (share_mode_stale_pid(lck
->data
, i
)) {
1142 *file_existed
= true;
1144 return NT_STATUS_SHARING_VIOLATION
;
1148 if (lck
->data
->num_share_modes
!= 0) {
1149 *file_existed
= true;
1152 return NT_STATUS_OK
;
1155 static bool is_delete_request(files_struct
*fsp
) {
1156 return ((fsp
->access_mask
== DELETE_ACCESS
) &&
1157 (fsp
->oplock_type
== NO_OPLOCK
));
1161 * Send a break message to the oplock holder and delay the open for
1165 static NTSTATUS
send_break_message(files_struct
*fsp
,
1166 struct share_mode_entry
*exclusive
,
1171 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
1173 DEBUG(10, ("Sending break request to PID %s\n",
1174 procid_str_static(&exclusive
->pid
)));
1175 exclusive
->op_mid
= mid
;
1177 /* Create the message. */
1178 share_mode_entry_to_message(msg
, exclusive
);
1180 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
1181 don't want this set in the share mode struct pointed to by lck. */
1183 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
1184 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,
1185 exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
1188 status
= messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, exclusive
->pid
,
1189 MSG_SMB_BREAK_REQUEST
,
1191 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
1192 if (!NT_STATUS_IS_OK(status
)) {
1193 DEBUG(3, ("Could not send oplock break message: %s\n",
1194 nt_errstr(status
)));
1201 * Return share_mode_entry pointers for :
1202 * 1). Batch oplock entry.
1203 * 2). Batch or exclusive oplock entry (may be identical to #1).
1204 * bool have_level2_oplock
1205 * bool have_no_oplock.
1206 * Do internal consistency checks on the share mode for a file.
1209 static void find_oplock_types(files_struct
*fsp
,
1211 const struct share_mode_lock
*lck
,
1212 struct share_mode_entry
**pp_batch
,
1213 struct share_mode_entry
**pp_ex_or_batch
,
1215 bool *got_no_oplock
)
1220 *pp_ex_or_batch
= NULL
;
1221 *got_level2
= false;
1222 *got_no_oplock
= false;
1224 /* Ignore stat or internal opens, as is done in
1225 delay_for_batch_oplocks() and
1226 delay_for_exclusive_oplocks().
1228 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1232 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1233 if (!is_valid_share_mode_entry(&lck
->data
->share_modes
[i
])) {
1237 if (lck
->data
->share_modes
[i
].op_type
== NO_OPLOCK
&&
1238 is_stat_open(lck
->data
->share_modes
[i
].access_mask
)) {
1239 /* We ignore stat opens in the table - they
1240 always have NO_OPLOCK and never get or
1241 cause breaks. JRA. */
1245 if (BATCH_OPLOCK_TYPE(lck
->data
->share_modes
[i
].op_type
)) {
1246 /* batch - can only be one. */
1247 if (share_mode_stale_pid(lck
->data
, i
)) {
1248 DEBUG(10, ("Found stale batch oplock\n"));
1251 if (*pp_ex_or_batch
|| *pp_batch
|| *got_level2
|| *got_no_oplock
) {
1252 smb_panic("Bad batch oplock entry.");
1254 *pp_batch
= &lck
->data
->share_modes
[i
];
1257 if (EXCLUSIVE_OPLOCK_TYPE(lck
->data
->share_modes
[i
].op_type
)) {
1258 if (share_mode_stale_pid(lck
->data
, i
)) {
1259 DEBUG(10, ("Found stale duplicate oplock\n"));
1262 /* Exclusive or batch - can only be one. */
1263 if (*pp_ex_or_batch
|| *got_level2
|| *got_no_oplock
) {
1264 smb_panic("Bad exclusive or batch oplock entry.");
1266 *pp_ex_or_batch
= &lck
->data
->share_modes
[i
];
1269 if (LEVEL_II_OPLOCK_TYPE(lck
->data
->share_modes
[i
].op_type
)) {
1270 if (*pp_batch
|| *pp_ex_or_batch
) {
1271 if (share_mode_stale_pid(lck
->data
, i
)) {
1272 DEBUG(10, ("Found stale LevelII "
1276 smb_panic("Bad levelII oplock entry.");
1281 if (lck
->data
->share_modes
[i
].op_type
== NO_OPLOCK
) {
1282 if (*pp_batch
|| *pp_ex_or_batch
) {
1283 if (share_mode_stale_pid(lck
->data
, i
)) {
1284 DEBUG(10, ("Found stale NO_OPLOCK "
1288 smb_panic("Bad no oplock entry.");
1290 *got_no_oplock
= true;
1295 static bool delay_for_batch_oplocks(files_struct
*fsp
,
1298 struct share_mode_entry
*batch_entry
)
1300 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1303 if (batch_entry
== NULL
) {
1307 /* Found a batch oplock */
1308 send_break_message(fsp
, batch_entry
, mid
, oplock_request
);
1312 static bool delay_for_exclusive_oplocks(files_struct
*fsp
,
1315 struct share_mode_entry
*ex_entry
)
1319 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
1322 if (ex_entry
== NULL
) {
1326 /* Found an exclusive or batch oplock */
1328 delay_it
= is_delete_request(fsp
) ?
1329 BATCH_OPLOCK_TYPE(ex_entry
->op_type
) : true;
1335 send_break_message(fsp
, ex_entry
, mid
, oplock_request
);
1339 static bool file_has_brlocks(files_struct
*fsp
)
1341 struct byte_range_lock
*br_lck
;
1343 br_lck
= brl_get_locks_readonly(fsp
);
1347 return br_lck
->num_locks
> 0 ? true : false;
1350 static void grant_fsp_oplock_type(files_struct
*fsp
,
1352 bool got_level2_oplock
,
1353 bool got_a_none_oplock
)
1355 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
1356 lp_level2_oplocks(SNUM(fsp
->conn
));
1358 /* Start by granting what the client asked for,
1359 but ensure no SAMBA_PRIVATE bits can be set. */
1360 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1362 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
1363 /* No oplocks on internal open. */
1364 fsp
->oplock_type
= NO_OPLOCK
;
1365 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1366 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1370 if (lp_locking(fsp
->conn
->params
) && file_has_brlocks(fsp
)) {
1371 DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n",
1373 fsp
->oplock_type
= NO_OPLOCK
;
1376 if (is_stat_open(fsp
->access_mask
)) {
1377 /* Leave the value already set. */
1378 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1379 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1384 * Match what was requested (fsp->oplock_type) with
1385 * what was found in the existing share modes.
1388 if (got_a_none_oplock
) {
1389 fsp
->oplock_type
= NO_OPLOCK
;
1390 } else if (got_level2_oplock
) {
1391 if (fsp
->oplock_type
== NO_OPLOCK
||
1392 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1393 /* Store a level2 oplock, but don't tell the client */
1394 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1396 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1399 /* All share_mode_entries are placeholders or deferred.
1400 * Silently upgrade to fake levelII if the client didn't
1401 * ask for an oplock. */
1402 if (fsp
->oplock_type
== NO_OPLOCK
) {
1403 /* Store a level2 oplock, but don't tell the client */
1404 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1409 * Don't grant level2 to clients that don't want them
1410 * or if we've turned them off.
1412 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1413 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1416 DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n",
1417 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1420 static bool request_timed_out(struct timeval request_time
,
1421 struct timeval timeout
)
1423 struct timeval now
, end_time
;
1425 end_time
= timeval_sum(&request_time
, &timeout
);
1426 return (timeval_compare(&end_time
, &now
) < 0);
1429 /****************************************************************************
1430 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1431 ****************************************************************************/
1433 static void defer_open(struct share_mode_lock
*lck
,
1434 struct timeval request_time
,
1435 struct timeval timeout
,
1436 struct smb_request
*req
,
1437 struct deferred_open_record
*state
)
1439 struct server_id self
= messaging_server_id(req
->sconn
->msg_ctx
);
1441 /* Paranoia check */
1446 for (i
=0; i
<lck
->data
->num_share_modes
; i
++) {
1447 struct share_mode_entry
*e
= &lck
->data
->share_modes
[i
];
1449 if (is_deferred_open_entry(e
) &&
1450 serverid_equal(&self
, &e
->pid
) &&
1451 (e
->op_mid
== req
->mid
)) {
1452 DEBUG(0, ("Trying to defer an already deferred "
1453 "request: mid=%llu, exiting\n",
1454 (unsigned long long)req
->mid
));
1455 exit_server("attempt to defer a deferred request");
1460 /* End paranoia check */
1462 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1463 "open entry for mid %llu\n",
1464 (unsigned int)request_time
.tv_sec
,
1465 (unsigned int)request_time
.tv_usec
,
1466 (unsigned long long)req
->mid
));
1468 if (!push_deferred_open_message_smb(req
, request_time
, timeout
,
1469 state
->id
, (char *)state
, sizeof(*state
))) {
1470 exit_server("push_deferred_open_message_smb failed");
1473 add_deferred_open(lck
, req
->mid
, request_time
, self
, state
->id
);
1478 /****************************************************************************
1479 On overwrite open ensure that the attributes match.
1480 ****************************************************************************/
1482 static bool open_match_attributes(connection_struct
*conn
,
1483 uint32 old_dos_attr
,
1484 uint32 new_dos_attr
,
1485 mode_t existing_unx_mode
,
1486 mode_t new_unx_mode
,
1487 mode_t
*returned_unx_mode
)
1489 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1491 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1492 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1494 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1495 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1496 *returned_unx_mode
= new_unx_mode
;
1498 *returned_unx_mode
= (mode_t
)0;
1501 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1502 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1503 "returned_unx_mode = 0%o\n",
1504 (unsigned int)old_dos_attr
,
1505 (unsigned int)existing_unx_mode
,
1506 (unsigned int)new_dos_attr
,
1507 (unsigned int)*returned_unx_mode
));
1509 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1510 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1511 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1512 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1516 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1517 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1518 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1525 /****************************************************************************
1526 Special FCB or DOS processing in the case of a sharing violation.
1527 Try and find a duplicated file handle.
1528 ****************************************************************************/
1530 static NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1531 connection_struct
*conn
,
1532 files_struct
*fsp_to_dup_into
,
1533 const struct smb_filename
*smb_fname
,
1538 uint32 share_access
,
1539 uint32 create_options
)
1543 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1544 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1546 for(fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1547 fsp
= file_find_di_next(fsp
)) {
1549 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1550 "vuid = %llu, file_pid = %u, private_options = 0x%x "
1551 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1552 fsp
->fh
->fd
, (unsigned long long)fsp
->vuid
,
1553 (unsigned int)fsp
->file_pid
,
1554 (unsigned int)fsp
->fh
->private_options
,
1555 (unsigned int)fsp
->access_mask
));
1557 if (fsp
->fh
->fd
!= -1 &&
1558 fsp
->vuid
== vuid
&&
1559 fsp
->file_pid
== file_pid
&&
1560 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1561 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1562 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1563 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1564 strequal(fsp
->fsp_name
->stream_name
,
1565 smb_fname
->stream_name
)) {
1566 DEBUG(10,("fcb_or_dos_open: file match\n"));
1572 return NT_STATUS_NOT_FOUND
;
1575 /* quite an insane set of semantics ... */
1576 if (is_executable(smb_fname
->base_name
) &&
1577 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1578 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1579 return NT_STATUS_INVALID_PARAMETER
;
1582 /* We need to duplicate this fsp. */
1583 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1584 create_options
, fsp_to_dup_into
);
1587 static void schedule_defer_open(struct share_mode_lock
*lck
,
1588 struct timeval request_time
,
1589 struct smb_request
*req
)
1591 struct deferred_open_record state
;
1593 /* This is a relative time, added to the absolute
1594 request_time value to get the absolute timeout time.
1595 Note that if this is the second or greater time we enter
1596 this codepath for this particular request mid then
1597 request_time is left as the absolute time of the *first*
1598 time this request mid was processed. This is what allows
1599 the request to eventually time out. */
1601 struct timeval timeout
;
1603 /* Normally the smbd we asked should respond within
1604 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1605 * the client did, give twice the timeout as a safety
1606 * measure here in case the other smbd is stuck
1607 * somewhere else. */
1609 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1611 /* Nothing actually uses state.delayed_for_oplocks
1612 but it's handy to differentiate in debug messages
1613 between a 30 second delay due to oplock break, and
1614 a 1 second delay for share mode conflicts. */
1616 state
.delayed_for_oplocks
= True
;
1617 state
.async_open
= false;
1618 state
.id
= lck
->data
->id
;
1620 if (!request_timed_out(request_time
, timeout
)) {
1621 defer_open(lck
, request_time
, timeout
, req
, &state
);
1625 /****************************************************************************
1626 Reschedule an open call that went asynchronous.
1627 ****************************************************************************/
1629 static void schedule_async_open(struct timeval request_time
,
1630 struct smb_request
*req
)
1632 struct deferred_open_record state
;
1633 struct timeval timeout
;
1635 timeout
= timeval_set(20, 0);
1638 state
.delayed_for_oplocks
= false;
1639 state
.async_open
= true;
1641 if (!request_timed_out(request_time
, timeout
)) {
1642 defer_open(NULL
, request_time
, timeout
, req
, &state
);
1646 /****************************************************************************
1647 Work out what access_mask to use from what the client sent us.
1648 ****************************************************************************/
1650 static NTSTATUS
smbd_calculate_maximum_allowed_access(
1651 connection_struct
*conn
,
1652 const struct smb_filename
*smb_fname
,
1653 uint32_t *p_access_mask
)
1655 struct security_descriptor
*sd
;
1656 uint32_t access_granted
;
1659 if (get_current_uid(conn
) == (uid_t
)0) {
1660 *p_access_mask
|= FILE_GENERIC_ALL
;
1661 return NT_STATUS_OK
;
1664 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1669 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1671 * File did not exist
1673 *p_access_mask
= FILE_GENERIC_ALL
;
1674 return NT_STATUS_OK
;
1676 if (!NT_STATUS_IS_OK(status
)) {
1677 DEBUG(10,("smbd_calculate_access_mask: "
1678 "Could not get acl on file %s: %s\n",
1679 smb_fname_str_dbg(smb_fname
),
1680 nt_errstr(status
)));
1681 return NT_STATUS_ACCESS_DENIED
;
1685 * Never test FILE_READ_ATTRIBUTES. se_file_access_check()
1686 * also takes care of owner WRITE_DAC and READ_CONTROL.
1688 status
= se_file_access_check(sd
,
1689 get_current_nttok(conn
),
1691 (*p_access_mask
& ~FILE_READ_ATTRIBUTES
),
1696 if (!NT_STATUS_IS_OK(status
)) {
1697 DEBUG(10, ("smbd_calculate_access_mask: "
1698 "Access denied on file %s: "
1699 "when calculating maximum access\n",
1700 smb_fname_str_dbg(smb_fname
)));
1701 return NT_STATUS_ACCESS_DENIED
;
1703 *p_access_mask
= (access_granted
| FILE_READ_ATTRIBUTES
);
1704 return NT_STATUS_OK
;
1707 NTSTATUS
smbd_calculate_access_mask(connection_struct
*conn
,
1708 const struct smb_filename
*smb_fname
,
1709 uint32_t access_mask
,
1710 uint32_t *access_mask_out
)
1713 uint32_t orig_access_mask
= access_mask
;
1714 uint32_t rejected_share_access
;
1717 * Convert GENERIC bits to specific bits.
1720 se_map_generic(&access_mask
, &file_generic_mapping
);
1722 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1723 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1725 status
= smbd_calculate_maximum_allowed_access(
1726 conn
, smb_fname
, &access_mask
);
1728 if (!NT_STATUS_IS_OK(status
)) {
1732 access_mask
&= conn
->share_access
;
1735 rejected_share_access
= access_mask
& ~(conn
->share_access
);
1737 if (rejected_share_access
) {
1738 DEBUG(10, ("smbd_calculate_access_mask: Access denied on "
1739 "file %s: rejected by share access mask[0x%08X] "
1740 "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n",
1741 smb_fname_str_dbg(smb_fname
),
1743 orig_access_mask
, access_mask
,
1744 rejected_share_access
));
1745 return NT_STATUS_ACCESS_DENIED
;
1748 *access_mask_out
= access_mask
;
1749 return NT_STATUS_OK
;
1752 /****************************************************************************
1753 Remove the deferred open entry under lock.
1754 ****************************************************************************/
1756 void remove_deferred_open_entry(struct file_id id
, uint64_t mid
,
1757 struct server_id pid
)
1759 struct share_mode_lock
*lck
= get_existing_share_mode_lock(
1762 DEBUG(0, ("could not get share mode lock\n"));
1765 del_deferred_open_entry(lck
, mid
, pid
);
1769 /****************************************************************************
1770 Return true if this is a state pointer to an asynchronous create.
1771 ****************************************************************************/
1773 bool is_deferred_open_async(const void *ptr
)
1775 const struct deferred_open_record
*state
= (const struct deferred_open_record
*)ptr
;
1777 return state
->async_open
;
1780 static bool clear_ads(uint32_t create_disposition
)
1784 switch (create_disposition
) {
1785 case FILE_SUPERSEDE
:
1786 case FILE_OVERWRITE_IF
:
1787 case FILE_OVERWRITE
:
1796 static int disposition_to_open_flags(uint32_t create_disposition
)
1801 * Currently we're using FILE_SUPERSEDE as the same as
1802 * FILE_OVERWRITE_IF but they really are
1803 * different. FILE_SUPERSEDE deletes an existing file
1804 * (requiring delete access) then recreates it.
1807 switch (create_disposition
) {
1808 case FILE_SUPERSEDE
:
1809 case FILE_OVERWRITE_IF
:
1811 * If file exists replace/overwrite. If file doesn't
1814 ret
= O_CREAT
|O_TRUNC
;
1819 * If file exists open. If file doesn't exist error.
1824 case FILE_OVERWRITE
:
1826 * If file exists overwrite. If file doesn't exist
1834 * If file exists error. If file doesn't exist create.
1836 ret
= O_CREAT
|O_EXCL
;
1841 * If file exists open. If file doesn't exist create.
1849 /****************************************************************************
1850 Open a file with a share mode. Passed in an already created files_struct *.
1851 ****************************************************************************/
1853 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1854 struct smb_request
*req
,
1855 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1856 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1857 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1858 uint32 create_options
, /* options such as delete on close. */
1859 uint32 new_dos_attributes
, /* attributes used for new file. */
1860 int oplock_request
, /* internal Samba oplock codes. */
1861 /* Information (FILE_EXISTS etc.) */
1862 uint32_t private_flags
, /* Samba specific flags. */
1866 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1869 bool file_existed
= VALID_STAT(smb_fname
->st
);
1870 bool def_acl
= False
;
1871 bool posix_open
= False
;
1872 bool new_file_created
= False
;
1873 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1874 mode_t new_unx_mode
= (mode_t
)0;
1875 mode_t unx_mode
= (mode_t
)0;
1877 uint32 existing_dos_attributes
= 0;
1878 struct timeval request_time
= timeval_zero();
1879 struct share_mode_lock
*lck
= NULL
;
1880 uint32 open_access_mask
= access_mask
;
1883 SMB_STRUCT_STAT saved_stat
= smb_fname
->st
;
1885 if (conn
->printer
) {
1887 * Printers are handled completely differently.
1888 * Most of the passed parameters are ignored.
1892 *pinfo
= FILE_WAS_CREATED
;
1895 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1896 smb_fname_str_dbg(smb_fname
)));
1899 DEBUG(0,("open_file_ntcreate: printer open without "
1900 "an SMB request!\n"));
1901 return NT_STATUS_INTERNAL_ERROR
;
1904 return print_spool_open(fsp
, smb_fname
->base_name
,
1908 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1910 return NT_STATUS_NO_MEMORY
;
1913 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1915 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
1916 new_dos_attributes
= 0;
1918 /* Windows allows a new file to be created and
1919 silently removes a FILE_ATTRIBUTE_DIRECTORY
1920 sent by the client. Do the same. */
1922 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
1924 /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
1926 unx_mode
= unix_mode(conn
, new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
1927 smb_fname
, parent_dir
);
1930 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1931 "access_mask=0x%x share_access=0x%x "
1932 "create_disposition = 0x%x create_options=0x%x "
1933 "unix mode=0%o oplock_request=%d private_flags = 0x%x\n",
1934 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
1935 access_mask
, share_access
, create_disposition
,
1936 create_options
, (unsigned int)unx_mode
, oplock_request
,
1937 (unsigned int)private_flags
));
1939 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
1940 DEBUG(0, ("No smb request but not an internal only open!\n"));
1941 return NT_STATUS_INTERNAL_ERROR
;
1945 * Only non-internal opens can be deferred at all
1950 if (get_deferred_open_message_state(req
,
1953 /* Remember the absolute time of the original
1954 request with this mid. We'll use it later to
1955 see if this has timed out. */
1957 /* If it was an async create retry, the file
1960 if (is_deferred_open_async(ptr
)) {
1961 SET_STAT_INVALID(smb_fname
->st
);
1962 file_existed
= false;
1964 struct deferred_open_record
*state
= (struct deferred_open_record
*)ptr
;
1965 /* Remove the deferred open entry under lock. */
1966 remove_deferred_open_entry(
1967 state
->id
, req
->mid
,
1968 messaging_server_id(req
->sconn
->msg_ctx
));
1971 /* Ensure we don't reprocess this message. */
1972 remove_deferred_open_message_smb(req
->sconn
, req
->mid
);
1977 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
1979 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
1983 /* ignore any oplock requests if oplocks are disabled */
1984 if (!lp_oplocks(SNUM(conn
)) ||
1985 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
1986 /* Mask off everything except the private Samba bits. */
1987 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
1990 /* this is for OS/2 long file names - say we don't support them */
1991 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
1992 /* OS/2 Workplace shell fix may be main code stream in a later
1994 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1996 if (use_nt_status()) {
1997 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1999 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
2002 switch( create_disposition
) {
2004 /* If file exists open. If file doesn't exist error. */
2005 if (!file_existed
) {
2006 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
2007 "requested for file %s and file "
2009 smb_fname_str_dbg(smb_fname
)));
2011 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2015 case FILE_OVERWRITE
:
2016 /* If file exists overwrite. If file doesn't exist
2018 if (!file_existed
) {
2019 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
2020 "requested for file %s and file "
2022 smb_fname_str_dbg(smb_fname
) ));
2024 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2029 /* If file exists error. If file doesn't exist
2032 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
2033 "requested for file %s and file "
2034 "already exists.\n",
2035 smb_fname_str_dbg(smb_fname
)));
2036 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
2041 return map_nt_error_from_unix(errno
);
2045 case FILE_SUPERSEDE
:
2046 case FILE_OVERWRITE_IF
:
2050 return NT_STATUS_INVALID_PARAMETER
;
2053 flags2
= disposition_to_open_flags(create_disposition
);
2055 /* We only care about matching attributes on file exists and
2058 if (!posix_open
&& file_existed
&&
2059 ((create_disposition
== FILE_OVERWRITE
) ||
2060 (create_disposition
== FILE_OVERWRITE_IF
))) {
2061 if (!open_match_attributes(conn
, existing_dos_attributes
,
2063 smb_fname
->st
.st_ex_mode
,
2064 unx_mode
, &new_unx_mode
)) {
2065 DEBUG(5,("open_file_ntcreate: attributes missmatch "
2066 "for file %s (%x %x) (0%o, 0%o)\n",
2067 smb_fname_str_dbg(smb_fname
),
2068 existing_dos_attributes
,
2070 (unsigned int)smb_fname
->st
.st_ex_mode
,
2071 (unsigned int)unx_mode
));
2073 return NT_STATUS_ACCESS_DENIED
;
2077 status
= smbd_calculate_access_mask(conn
, smb_fname
,
2080 if (!NT_STATUS_IS_OK(status
)) {
2081 DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask "
2082 "on file %s returned %s\n",
2083 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
2087 open_access_mask
= access_mask
;
2089 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
2090 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
2093 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
2094 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
2098 * Note that we ignore the append flag as append does not
2099 * mean the same thing under DOS and Unix.
2102 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
2103 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
2104 /* DENY_DOS opens are always underlying read-write on the
2105 file handle, no matter what the requested access mask
2107 if ((private_flags
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
2108 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
2118 * Currently we only look at FILE_WRITE_THROUGH for create options.
2122 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
2127 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
2131 if (!posix_open
&& !CAN_WRITE(conn
)) {
2133 * We should really return a permission denied error if either
2134 * O_CREAT or O_TRUNC are set, but for compatibility with
2135 * older versions of Samba we just AND them out.
2137 flags2
&= ~(O_CREAT
|O_TRUNC
);
2141 * Ensure we can't write on a read-only share or file.
2144 if (flags
!= O_RDONLY
&& file_existed
&&
2145 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
2146 DEBUG(5,("open_file_ntcreate: write access requested for "
2147 "file %s on read only %s\n",
2148 smb_fname_str_dbg(smb_fname
),
2149 !CAN_WRITE(conn
) ? "share" : "file" ));
2151 return NT_STATUS_ACCESS_DENIED
;
2154 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2155 fsp
->share_access
= share_access
;
2156 fsp
->fh
->private_options
= private_flags
;
2157 fsp
->access_mask
= open_access_mask
; /* We change this to the
2158 * requested access_mask after
2159 * the open is done. */
2160 fsp
->posix_open
= posix_open
;
2162 /* Ensure no SAMBA_PRIVATE bits can be set. */
2163 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
2165 if (timeval_is_zero(&request_time
)) {
2166 request_time
= fsp
->open_time
;
2170 struct share_mode_entry
*batch_entry
= NULL
;
2171 struct share_mode_entry
*exclusive_entry
= NULL
;
2172 bool got_level2_oplock
= false;
2173 bool got_a_none_oplock
= false;
2176 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2177 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
2179 lck
= get_share_mode_lock(talloc_tos(), id
,
2181 smb_fname
, &old_write_time
);
2183 DEBUG(0, ("Could not get share mode lock\n"));
2184 return NT_STATUS_SHARING_VIOLATION
;
2187 /* Get the types we need to examine. */
2188 find_oplock_types(fsp
,
2194 &got_a_none_oplock
);
2196 /* First pass - send break only on batch oplocks. */
2197 if ((req
!= NULL
) &&
2198 delay_for_batch_oplocks(fsp
,
2202 schedule_defer_open(lck
, request_time
, req
);
2204 return NT_STATUS_SHARING_VIOLATION
;
2207 /* Use the client requested access mask here, not the one we
2209 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2210 access_mask
, share_access
,
2211 create_options
, &file_existed
);
2213 if (NT_STATUS_IS_OK(status
)) {
2214 /* We might be going to allow this open. Check oplock
2216 /* Second pass - send break for both batch or
2217 * exclusive oplocks. */
2218 if ((req
!= NULL
) &&
2219 delay_for_exclusive_oplocks(
2224 schedule_defer_open(lck
, request_time
, req
);
2226 return NT_STATUS_SHARING_VIOLATION
;
2230 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
2231 /* DELETE_PENDING is not deferred for a second */
2236 grant_fsp_oplock_type(fsp
,
2241 if (!NT_STATUS_IS_OK(status
)) {
2242 uint32 can_access_mask
;
2243 bool can_access
= True
;
2245 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
2247 /* Check if this can be done with the deny_dos and fcb
2250 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
2251 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
2253 DEBUG(0, ("DOS open without an SMB "
2256 return NT_STATUS_INTERNAL_ERROR
;
2259 /* Use the client requested access mask here,
2260 * not the one we open with. */
2261 status
= fcb_or_dos_open(req
,
2272 if (NT_STATUS_IS_OK(status
)) {
2275 *pinfo
= FILE_WAS_OPENED
;
2277 return NT_STATUS_OK
;
2282 * This next line is a subtlety we need for
2283 * MS-Access. If a file open will fail due to share
2284 * permissions and also for security (access) reasons,
2285 * we need to return the access failed error, not the
2286 * share error. We can't open the file due to kernel
2287 * oplock deadlock (it's possible we failed above on
2288 * the open_mode_check()) so use a userspace check.
2291 if (flags
& O_RDWR
) {
2292 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
2293 } else if (flags
& O_WRONLY
) {
2294 can_access_mask
= FILE_WRITE_DATA
;
2296 can_access_mask
= FILE_READ_DATA
;
2299 if (((can_access_mask
& FILE_WRITE_DATA
) &&
2300 !CAN_WRITE(conn
)) ||
2301 !NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
2302 smb_fname
, can_access_mask
))) {
2307 * If we're returning a share violation, ensure we
2308 * cope with the braindead 1 second delay.
2311 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
2312 lp_defer_sharing_violations()) {
2313 struct timeval timeout
;
2314 struct deferred_open_record state
;
2317 /* this is a hack to speed up torture tests
2319 timeout_usecs
= lp_parm_int(SNUM(conn
),
2320 "smbd","sharedelay",
2321 SHARING_VIOLATION_USEC_WAIT
);
2323 /* This is a relative time, added to the absolute
2324 request_time value to get the absolute timeout time.
2325 Note that if this is the second or greater time we enter
2326 this codepath for this particular request mid then
2327 request_time is left as the absolute time of the *first*
2328 time this request mid was processed. This is what allows
2329 the request to eventually time out. */
2331 timeout
= timeval_set(0, timeout_usecs
);
2333 /* Nothing actually uses state.delayed_for_oplocks
2334 but it's handy to differentiate in debug messages
2335 between a 30 second delay due to oplock break, and
2336 a 1 second delay for share mode conflicts. */
2338 state
.delayed_for_oplocks
= False
;
2339 state
.async_open
= false;
2343 && !request_timed_out(request_time
,
2345 defer_open(lck
, request_time
, timeout
,
2353 * We have detected a sharing violation here
2354 * so return the correct error code
2356 status
= NT_STATUS_SHARING_VIOLATION
;
2358 status
= NT_STATUS_ACCESS_DENIED
;
2364 * We exit this block with the share entry *locked*.....
2368 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
2371 * Ensure we pay attention to default ACLs on directories if required.
2374 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
2375 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
2376 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
2379 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
2380 "access_mask = 0x%x, open_access_mask = 0x%x\n",
2381 (unsigned int)flags
, (unsigned int)flags2
,
2382 (unsigned int)unx_mode
, (unsigned int)access_mask
,
2383 (unsigned int)open_access_mask
));
2386 * open_file strips any O_TRUNC flags itself.
2389 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
2390 flags
|flags2
, unx_mode
, access_mask
,
2391 open_access_mask
, &new_file_created
);
2393 if (!NT_STATUS_IS_OK(fsp_open
)) {
2394 if (NT_STATUS_EQUAL(fsp_open
, NT_STATUS_RETRY
)) {
2395 schedule_async_open(request_time
, req
);
2401 if (file_existed
&& !check_same_dev_ino(&saved_stat
, &smb_fname
->st
)) {
2403 * The file did exist, but some other (local or NFS)
2404 * process either renamed/unlinked and re-created the
2405 * file with different dev/ino after we walked the path,
2406 * but before we did the open. We could retry the
2407 * open but it's a rare enough case it's easier to
2408 * just fail the open to prevent creating any problems
2409 * in the open file db having the wrong dev/ino key.
2413 DEBUG(1,("open_file_ntcreate: file %s - dev/ino mismatch. "
2414 "Old (dev=0x%llu, ino =0x%llu). "
2415 "New (dev=0x%llu, ino=0x%llu). Failing open "
2416 " with NT_STATUS_ACCESS_DENIED.\n",
2417 smb_fname_str_dbg(smb_fname
),
2418 (unsigned long long)saved_stat
.st_ex_dev
,
2419 (unsigned long long)saved_stat
.st_ex_ino
,
2420 (unsigned long long)smb_fname
->st
.st_ex_dev
,
2421 (unsigned long long)smb_fname
->st
.st_ex_ino
));
2422 return NT_STATUS_ACCESS_DENIED
;
2425 if (!file_existed
) {
2426 struct share_mode_entry
*batch_entry
= NULL
;
2427 struct share_mode_entry
*exclusive_entry
= NULL
;
2428 bool got_level2_oplock
= false;
2429 bool got_a_none_oplock
= false;
2430 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2433 * Deal with the race condition where two smbd's detect the
2434 * file doesn't exist and do the create at the same time. One
2435 * of them will win and set a share mode, the other (ie. this
2436 * one) should check if the requested share mode for this
2437 * create is allowed.
2441 * Now the file exists and fsp is successfully opened,
2442 * fsp->dev and fsp->inode are valid and should replace the
2443 * dev=0,inode=0 from a non existent file. Spotted by
2444 * Nadav Danieli <nadavd@exanet.com>. JRA.
2449 lck
= get_share_mode_lock(talloc_tos(), id
,
2451 smb_fname
, &old_write_time
);
2454 DEBUG(0, ("open_file_ntcreate: Could not get share "
2455 "mode lock for %s\n",
2456 smb_fname_str_dbg(smb_fname
)));
2458 return NT_STATUS_SHARING_VIOLATION
;
2461 /* Get the types we need to examine. */
2462 find_oplock_types(fsp
,
2468 &got_a_none_oplock
);
2470 /* First pass - send break only on batch oplocks. */
2471 if ((req
!= NULL
) &&
2472 delay_for_batch_oplocks(fsp
,
2476 schedule_defer_open(lck
, request_time
, req
);
2479 return NT_STATUS_SHARING_VIOLATION
;
2482 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
2483 access_mask
, share_access
,
2484 create_options
, &file_existed
);
2486 if (NT_STATUS_IS_OK(status
)) {
2487 /* We might be going to allow this open. Check oplock
2489 /* Second pass - send break for both batch or
2490 * exclusive oplocks. */
2491 if ((req
!= NULL
) &&
2492 delay_for_exclusive_oplocks(
2497 schedule_defer_open(lck
, request_time
, req
);
2500 return NT_STATUS_SHARING_VIOLATION
;
2504 if (!NT_STATUS_IS_OK(status
)) {
2505 struct deferred_open_record state
;
2507 state
.delayed_for_oplocks
= False
;
2508 state
.async_open
= false;
2511 /* Do it all over again immediately. In the second
2512 * round we will find that the file existed and handle
2513 * the DELETE_PENDING and FCB cases correctly. No need
2514 * to duplicate the code here. Essentially this is a
2515 * "goto top of this function", but don't tell
2519 defer_open(lck
, request_time
, timeval_zero(),
2527 grant_fsp_oplock_type(fsp
,
2533 * We exit this block with the share entry *locked*.....
2538 SMB_ASSERT(lck
!= NULL
);
2540 /* Delete streams if create_disposition requires it */
2541 if (!new_file_created
&& clear_ads(create_disposition
) &&
2542 !is_ntfs_stream_smb_fname(smb_fname
)) {
2543 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2544 if (!NT_STATUS_IS_OK(status
)) {
2551 /* note that we ignore failure for the following. It is
2552 basically a hack for NFS, and NFS will never set one of
2553 these only read them. Nobody but Samba can ever set a deny
2554 mode and we have already checked our more authoritative
2555 locking database for permission to set this deny mode. If
2556 the kernel refuses the operations then the kernel is wrong.
2557 note that GPFS supports it as well - jmcd */
2559 if (fsp
->fh
->fd
!= -1) {
2561 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2562 if(ret_flock
== -1 ){
2567 return NT_STATUS_SHARING_VIOLATION
;
2572 * At this point onwards, we can guarentee that the share entry
2573 * is locked, whether we created the file or not, and that the
2574 * deny mode is compatible with all current opens.
2578 * If requested, truncate the file.
2581 if (!new_file_created
&& (flags2
&O_TRUNC
)) {
2583 * We are modifying the file after open - update the stat
2586 if ((SMB_VFS_FTRUNCATE(fsp
, 0) == -1) ||
2587 (SMB_VFS_FSTAT(fsp
, &smb_fname
->st
)==-1)) {
2588 status
= map_nt_error_from_unix(errno
);
2596 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2597 * but we don't have to store this - just ignore it on access check.
2599 if (conn
->sconn
->using_smb2
) {
2601 * SMB2 doesn't return it (according to Microsoft tests).
2602 * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0
2603 * File created with access = 0x7 (Read, Write, Delete)
2604 * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes)
2606 fsp
->access_mask
= access_mask
;
2608 /* But SMB1 does. */
2609 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2613 /* stat opens on existing files don't get oplocks. */
2614 if (is_stat_open(open_access_mask
)) {
2615 fsp
->oplock_type
= NO_OPLOCK
;
2619 if (new_file_created
) {
2620 info
= FILE_WAS_CREATED
;
2622 if (flags2
& O_TRUNC
) {
2623 info
= FILE_WAS_OVERWRITTEN
;
2625 info
= FILE_WAS_OPENED
;
2634 * Setup the oplock info in both the shared memory and
2638 status
= set_file_oplock(fsp
, fsp
->oplock_type
);
2639 if (!NT_STATUS_IS_OK(status
)) {
2641 * Could not get the kernel oplock or there are byte-range
2642 * locks on the file.
2644 fsp
->oplock_type
= NO_OPLOCK
;
2647 set_share_mode(lck
, fsp
, get_current_uid(conn
),
2651 /* Handle strange delete on close create semantics. */
2652 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2654 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2656 if (!NT_STATUS_IS_OK(status
)) {
2657 /* Remember to delete the mode we just added. */
2658 del_share_mode(lck
, fsp
);
2663 /* Note that here we set the *inital* delete on close flag,
2664 not the regular one. The magic gets handled in close. */
2665 fsp
->initial_delete_on_close
= True
;
2668 if (info
!= FILE_WAS_OPENED
) {
2669 /* Files should be initially set as archive */
2670 if (lp_map_archive(SNUM(conn
)) ||
2671 lp_store_dos_attributes(SNUM(conn
))) {
2673 if (file_set_dosmode(conn
, smb_fname
,
2674 new_dos_attributes
| FILE_ATTRIBUTE_ARCHIVE
,
2675 parent_dir
, true) == 0) {
2676 unx_mode
= smb_fname
->st
.st_ex_mode
;
2682 /* Determine sparse flag. */
2684 /* POSIX opens are sparse by default. */
2685 fsp
->is_sparse
= true;
2687 fsp
->is_sparse
= (file_existed
&&
2688 (existing_dos_attributes
& FILE_ATTRIBUTE_SPARSE
));
2692 * Take care of inherited ACLs on created files - if default ACL not
2696 if (!posix_open
&& new_file_created
&& !def_acl
) {
2698 int saved_errno
= errno
; /* We might get ENOSYS in the next
2701 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2703 errno
= saved_errno
; /* Ignore ENOSYS */
2706 } else if (new_unx_mode
) {
2710 /* Attributes need changing. File already existed. */
2713 int saved_errno
= errno
; /* We might get ENOSYS in the
2715 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2717 if (ret
== -1 && errno
== ENOSYS
) {
2718 errno
= saved_errno
; /* Ignore ENOSYS */
2720 DEBUG(5, ("open_file_ntcreate: reset "
2721 "attributes of file %s to 0%o\n",
2722 smb_fname_str_dbg(smb_fname
),
2723 (unsigned int)new_unx_mode
));
2724 ret
= 0; /* Don't do the fchmod below. */
2729 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2730 DEBUG(5, ("open_file_ntcreate: failed to reset "
2731 "attributes of file %s to 0%o\n",
2732 smb_fname_str_dbg(smb_fname
),
2733 (unsigned int)new_unx_mode
));
2736 /* If this is a successful open, we must remove any deferred open
2739 del_deferred_open_entry(lck
, req
->mid
,
2740 messaging_server_id(req
->sconn
->msg_ctx
));
2744 return NT_STATUS_OK
;
2748 /****************************************************************************
2749 Open a file for for write to ensure that we can fchmod it.
2750 ****************************************************************************/
2752 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2753 struct smb_filename
*smb_fname
,
2754 files_struct
**result
)
2756 if (!VALID_STAT(smb_fname
->st
)) {
2757 return NT_STATUS_INVALID_PARAMETER
;
2760 return SMB_VFS_CREATE_FILE(
2763 0, /* root_dir_fid */
2764 smb_fname
, /* fname */
2765 FILE_WRITE_DATA
, /* access_mask */
2766 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2768 FILE_OPEN
, /* create_disposition*/
2769 0, /* create_options */
2770 0, /* file_attributes */
2771 INTERNAL_OPEN_ONLY
, /* oplock_request */
2772 0, /* allocation_size */
2773 0, /* private_flags */
2776 result
, /* result */
2780 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2781 struct smb_filename
*smb_dname
,
2782 uint32 file_attributes
)
2785 char *parent_dir
= NULL
;
2787 bool posix_open
= false;
2788 bool need_re_stat
= false;
2789 uint32_t access_mask
= SEC_DIR_ADD_SUBDIR
;
2791 if(access_mask
& ~(conn
->share_access
)) {
2792 DEBUG(5,("mkdir_internal: failing share access "
2793 "%s\n", lp_servicename(talloc_tos(), SNUM(conn
))));
2794 return NT_STATUS_ACCESS_DENIED
;
2797 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2799 return NT_STATUS_NO_MEMORY
;
2802 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2804 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2806 mode
= unix_mode(conn
, FILE_ATTRIBUTE_DIRECTORY
, smb_dname
, parent_dir
);
2809 status
= check_parent_access(conn
,
2812 if(!NT_STATUS_IS_OK(status
)) {
2813 DEBUG(5,("mkdir_internal: check_parent_access "
2814 "on directory %s for path %s returned %s\n",
2816 smb_dname
->base_name
,
2817 nt_errstr(status
) ));
2821 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2822 return map_nt_error_from_unix(errno
);
2825 /* Ensure we're checking for a symlink here.... */
2826 /* We don't want to get caught by a symlink racer. */
2828 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2829 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2830 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2831 return map_nt_error_from_unix(errno
);
2834 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2835 DEBUG(0, ("Directory '%s' just created is not a directory !\n",
2836 smb_fname_str_dbg(smb_dname
)));
2837 return NT_STATUS_NOT_A_DIRECTORY
;
2840 if (lp_store_dos_attributes(SNUM(conn
))) {
2842 file_set_dosmode(conn
, smb_dname
,
2843 file_attributes
| FILE_ATTRIBUTE_DIRECTORY
,
2848 if (lp_inherit_perms(SNUM(conn
))) {
2849 inherit_access_posix_acl(conn
, parent_dir
,
2850 smb_dname
->base_name
, mode
);
2851 need_re_stat
= true;
2856 * Check if high bits should have been set,
2857 * then (if bits are missing): add them.
2858 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2861 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2862 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2863 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2864 (smb_dname
->st
.st_ex_mode
|
2865 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2866 need_re_stat
= true;
2870 /* Change the owner if required. */
2871 if (lp_inherit_owner(SNUM(conn
))) {
2872 change_dir_owner_to_parent(conn
, parent_dir
,
2873 smb_dname
->base_name
,
2875 need_re_stat
= true;
2879 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2880 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2881 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2882 return map_nt_error_from_unix(errno
);
2886 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2887 smb_dname
->base_name
);
2889 return NT_STATUS_OK
;
2892 /****************************************************************************
2893 Open a directory from an NT SMB call.
2894 ****************************************************************************/
2896 static NTSTATUS
open_directory(connection_struct
*conn
,
2897 struct smb_request
*req
,
2898 struct smb_filename
*smb_dname
,
2900 uint32 share_access
,
2901 uint32 create_disposition
,
2902 uint32 create_options
,
2903 uint32 file_attributes
,
2905 files_struct
**result
)
2907 files_struct
*fsp
= NULL
;
2908 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2909 struct share_mode_lock
*lck
= NULL
;
2911 struct timespec mtimespec
;
2914 if (is_ntfs_stream_smb_fname(smb_dname
)) {
2915 DEBUG(2, ("open_directory: %s is a stream name!\n",
2916 smb_fname_str_dbg(smb_dname
)));
2917 return NT_STATUS_NOT_A_DIRECTORY
;
2920 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
)) {
2921 /* Ensure we have a directory attribute. */
2922 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2925 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2926 "share_access = 0x%x create_options = 0x%x, "
2927 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2928 smb_fname_str_dbg(smb_dname
),
2929 (unsigned int)access_mask
,
2930 (unsigned int)share_access
,
2931 (unsigned int)create_options
,
2932 (unsigned int)create_disposition
,
2933 (unsigned int)file_attributes
));
2935 status
= smbd_calculate_access_mask(conn
, smb_dname
,
2936 access_mask
, &access_mask
);
2937 if (!NT_STATUS_IS_OK(status
)) {
2938 DEBUG(10, ("open_directory: smbd_calculate_access_mask "
2939 "on file %s returned %s\n",
2940 smb_fname_str_dbg(smb_dname
),
2941 nt_errstr(status
)));
2945 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2946 !security_token_has_privilege(get_current_nttok(conn
),
2947 SEC_PRIV_SECURITY
)) {
2948 DEBUG(10, ("open_directory: open on %s "
2949 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2950 smb_fname_str_dbg(smb_dname
)));
2951 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2954 switch( create_disposition
) {
2958 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
2961 info
= FILE_WAS_OPENED
;
2966 /* If directory exists error. If directory doesn't
2970 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
2971 DEBUG(2, ("open_directory: unable to create "
2972 "%s. Error was %s\n",
2973 smb_fname_str_dbg(smb_dname
),
2974 nt_errstr(status
)));
2978 status
= mkdir_internal(conn
, smb_dname
,
2981 if (!NT_STATUS_IS_OK(status
)) {
2982 DEBUG(2, ("open_directory: unable to create "
2983 "%s. Error was %s\n",
2984 smb_fname_str_dbg(smb_dname
),
2985 nt_errstr(status
)));
2989 info
= FILE_WAS_CREATED
;
2994 * If directory exists open. If directory doesn't
2999 status
= NT_STATUS_OK
;
3000 info
= FILE_WAS_OPENED
;
3002 status
= mkdir_internal(conn
, smb_dname
,
3005 if (NT_STATUS_IS_OK(status
)) {
3006 info
= FILE_WAS_CREATED
;
3008 /* Cope with create race. */
3009 if (!NT_STATUS_EQUAL(status
,
3010 NT_STATUS_OBJECT_NAME_COLLISION
)) {
3011 DEBUG(2, ("open_directory: unable to create "
3012 "%s. Error was %s\n",
3013 smb_fname_str_dbg(smb_dname
),
3014 nt_errstr(status
)));
3017 info
= FILE_WAS_OPENED
;
3023 case FILE_SUPERSEDE
:
3024 case FILE_OVERWRITE
:
3025 case FILE_OVERWRITE_IF
:
3027 DEBUG(5,("open_directory: invalid create_disposition "
3028 "0x%x for directory %s\n",
3029 (unsigned int)create_disposition
,
3030 smb_fname_str_dbg(smb_dname
)));
3031 return NT_STATUS_INVALID_PARAMETER
;
3034 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
3035 DEBUG(5,("open_directory: %s is not a directory !\n",
3036 smb_fname_str_dbg(smb_dname
)));
3037 return NT_STATUS_NOT_A_DIRECTORY
;
3040 if (info
== FILE_WAS_OPENED
) {
3041 status
= smbd_check_access_rights(conn
, smb_dname
, access_mask
);
3042 if (!NT_STATUS_IS_OK(status
)) {
3043 DEBUG(10, ("open_directory: smbd_check_access_rights on "
3044 "file %s failed with %s\n",
3045 smb_fname_str_dbg(smb_dname
),
3046 nt_errstr(status
)));
3051 status
= file_new(req
, conn
, &fsp
);
3052 if(!NT_STATUS_IS_OK(status
)) {
3057 * Setup the files_struct for it.
3060 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
3061 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
3062 fsp
->file_pid
= req
? req
->smbpid
: 0;
3063 fsp
->can_lock
= False
;
3064 fsp
->can_read
= False
;
3065 fsp
->can_write
= False
;
3067 fsp
->share_access
= share_access
;
3068 fsp
->fh
->private_options
= 0;
3070 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
3072 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
3073 fsp
->print_file
= NULL
;
3074 fsp
->modified
= False
;
3075 fsp
->oplock_type
= NO_OPLOCK
;
3076 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
3077 fsp
->is_directory
= True
;
3078 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
3079 status
= fsp_set_smb_fname(fsp
, smb_dname
);
3080 if (!NT_STATUS_IS_OK(status
)) {
3081 file_free(req
, fsp
);
3085 mtimespec
= smb_dname
->st
.st_ex_mtime
;
3088 status
= fd_open(conn
, fsp
, O_RDONLY
|O_DIRECTORY
, 0);
3090 /* POSIX allows us to open a directory with O_RDONLY. */
3091 status
= fd_open(conn
, fsp
, O_RDONLY
, 0);
3093 if (!NT_STATUS_IS_OK(status
)) {
3094 DEBUG(5, ("open_directory: Could not open fd for "
3096 smb_fname_str_dbg(smb_dname
),
3097 nt_errstr(status
)));
3098 file_free(req
, fsp
);
3102 status
= vfs_stat_fsp(fsp
);
3103 if (!NT_STATUS_IS_OK(status
)) {
3105 file_free(req
, fsp
);
3109 /* Ensure there was no race condition. */
3110 if (!check_same_stat(&smb_dname
->st
, &fsp
->fsp_name
->st
)) {
3111 DEBUG(5,("open_directory: stat struct differs for "
3113 smb_fname_str_dbg(smb_dname
)));
3115 file_free(req
, fsp
);
3116 return NT_STATUS_ACCESS_DENIED
;
3119 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
3120 conn
->connectpath
, smb_dname
,
3124 DEBUG(0, ("open_directory: Could not get share mode lock for "
3125 "%s\n", smb_fname_str_dbg(smb_dname
)));
3127 file_free(req
, fsp
);
3128 return NT_STATUS_SHARING_VIOLATION
;
3131 status
= open_mode_check(conn
, lck
, fsp
->name_hash
,
3132 access_mask
, share_access
,
3133 create_options
, &dir_existed
);
3135 if (!NT_STATUS_IS_OK(status
)) {
3138 file_free(req
, fsp
);
3142 set_share_mode(lck
, fsp
, get_current_uid(conn
),
3143 req
? req
->mid
: 0, NO_OPLOCK
);
3145 /* For directories the delete on close bit at open time seems
3146 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
3147 if (create_options
& FILE_DELETE_ON_CLOSE
) {
3148 status
= can_set_delete_on_close(fsp
, 0);
3149 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
3152 file_free(req
, fsp
);
3156 if (NT_STATUS_IS_OK(status
)) {
3157 /* Note that here we set the *inital* delete on close flag,
3158 not the regular one. The magic gets handled in close. */
3159 fsp
->initial_delete_on_close
= True
;
3170 return NT_STATUS_OK
;
3173 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
3174 struct smb_filename
*smb_dname
)
3179 status
= SMB_VFS_CREATE_FILE(
3182 0, /* root_dir_fid */
3183 smb_dname
, /* fname */
3184 FILE_READ_ATTRIBUTES
, /* access_mask */
3185 FILE_SHARE_NONE
, /* share_access */
3186 FILE_CREATE
, /* create_disposition*/
3187 FILE_DIRECTORY_FILE
, /* create_options */
3188 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
3189 0, /* oplock_request */
3190 0, /* allocation_size */
3191 0, /* private_flags */
3197 if (NT_STATUS_IS_OK(status
)) {
3198 close_file(req
, fsp
, NORMAL_CLOSE
);
3204 /****************************************************************************
3205 Receive notification that one of our open files has been renamed by another
3207 ****************************************************************************/
3209 void msg_file_was_renamed(struct messaging_context
*msg
,
3212 struct server_id server_id
,
3216 char *frm
= (char *)data
->data
;
3218 const char *sharepath
;
3219 const char *base_name
;
3220 const char *stream_name
;
3221 struct smb_filename
*smb_fname
= NULL
;
3222 size_t sp_len
, bn_len
;
3224 struct smbd_server_connection
*sconn
=
3225 talloc_get_type_abort(private_data
,
3226 struct smbd_server_connection
);
3228 if (data
->data
== NULL
3229 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
3230 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
3231 (int)data
->length
));
3235 /* Unpack the message. */
3236 pull_file_id_24(frm
, &id
);
3237 sharepath
= &frm
[24];
3238 sp_len
= strlen(sharepath
);
3239 base_name
= sharepath
+ sp_len
+ 1;
3240 bn_len
= strlen(base_name
);
3241 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
3243 /* stream_name must always be NULL if there is no stream. */
3244 if (stream_name
[0] == '\0') {
3248 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
3249 stream_name
, NULL
, &smb_fname
);
3250 if (!NT_STATUS_IS_OK(status
)) {
3254 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
3256 sharepath
, smb_fname_str_dbg(smb_fname
),
3257 file_id_string_tos(&id
)));
3259 for(fsp
= file_find_di_first(sconn
, id
); fsp
;
3260 fsp
= file_find_di_next(fsp
)) {
3261 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
3263 DEBUG(10,("msg_file_was_renamed: renaming file %s from %s -> %s\n",
3264 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
),
3265 smb_fname_str_dbg(smb_fname
)));
3266 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3267 if (!NT_STATUS_IS_OK(status
)) {
3272 /* Now we have the complete path we can work out if this is
3273 actually within this share and adjust newname accordingly. */
3274 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
3275 "not sharepath %s) "
3276 "%s from %s -> %s\n",
3277 fsp
->conn
->connectpath
,
3281 smb_fname_str_dbg(smb_fname
)));
3285 TALLOC_FREE(smb_fname
);
3290 * If a main file is opened for delete, all streams need to be checked for
3291 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
3292 * If that works, delete them all by setting the delete on close and close.
3295 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
3298 struct stream_struct
*stream_info
= NULL
;
3299 files_struct
**streams
= NULL
;
3301 unsigned int num_streams
= 0;
3302 TALLOC_CTX
*frame
= talloc_stackframe();
3305 status
= vfs_streaminfo(conn
, NULL
, fname
, talloc_tos(),
3306 &num_streams
, &stream_info
);
3308 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
3309 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
3310 DEBUG(10, ("no streams around\n"));
3312 return NT_STATUS_OK
;
3315 if (!NT_STATUS_IS_OK(status
)) {
3316 DEBUG(10, ("vfs_streaminfo failed: %s\n",
3317 nt_errstr(status
)));
3321 DEBUG(10, ("open_streams_for_delete found %d streams\n",
3324 if (num_streams
== 0) {
3326 return NT_STATUS_OK
;
3329 streams
= talloc_array(talloc_tos(), files_struct
*, num_streams
);
3330 if (streams
== NULL
) {
3331 DEBUG(0, ("talloc failed\n"));
3332 status
= NT_STATUS_NO_MEMORY
;
3336 for (i
=0; i
<num_streams
; i
++) {
3337 struct smb_filename
*smb_fname
= NULL
;
3339 if (strequal(stream_info
[i
].name
, "::$DATA")) {
3344 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
3345 stream_info
[i
].name
,
3347 if (!NT_STATUS_IS_OK(status
)) {
3351 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
3352 DEBUG(10, ("Unable to stat stream: %s\n",
3353 smb_fname_str_dbg(smb_fname
)));
3356 status
= SMB_VFS_CREATE_FILE(
3359 0, /* root_dir_fid */
3360 smb_fname
, /* fname */
3361 DELETE_ACCESS
, /* access_mask */
3362 (FILE_SHARE_READ
| /* share_access */
3363 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
3364 FILE_OPEN
, /* create_disposition*/
3365 0, /* create_options */
3366 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
3367 0, /* oplock_request */
3368 0, /* allocation_size */
3369 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* private_flags */
3372 &streams
[i
], /* result */
3375 if (!NT_STATUS_IS_OK(status
)) {
3376 DEBUG(10, ("Could not open stream %s: %s\n",
3377 smb_fname_str_dbg(smb_fname
),
3378 nt_errstr(status
)));
3380 TALLOC_FREE(smb_fname
);
3383 TALLOC_FREE(smb_fname
);
3387 * don't touch the variable "status" beyond this point :-)
3390 for (i
-= 1 ; i
>= 0; i
--) {
3391 if (streams
[i
] == NULL
) {
3395 DEBUG(10, ("Closing stream # %d, %s\n", i
,
3396 fsp_str_dbg(streams
[i
])));
3397 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
3405 /*********************************************************************
3406 Create a default ACL by inheriting from the parent. If no inheritance
3407 from the parent available, don't set anything. This will leave the actual
3408 permissions the new file or directory already got from the filesystem
3409 as the NT ACL when read.
3410 *********************************************************************/
3412 static NTSTATUS
inherit_new_acl(files_struct
*fsp
)
3414 TALLOC_CTX
*ctx
= talloc_tos();
3415 char *parent_name
= NULL
;
3416 struct security_descriptor
*parent_desc
= NULL
;
3417 NTSTATUS status
= NT_STATUS_OK
;
3418 struct security_descriptor
*psd
= NULL
;
3419 struct dom_sid
*owner_sid
= NULL
;
3420 struct dom_sid
*group_sid
= NULL
;
3421 uint32_t security_info_sent
= (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
);
3422 bool inherit_owner
= lp_inherit_owner(SNUM(fsp
->conn
));
3423 bool inheritable_components
= false;
3426 if (!parent_dirname(ctx
, fsp
->fsp_name
->base_name
, &parent_name
, NULL
)) {
3427 return NT_STATUS_NO_MEMORY
;
3430 status
= SMB_VFS_GET_NT_ACL(fsp
->conn
,
3432 (SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_DACL
),
3434 if (!NT_STATUS_IS_OK(status
)) {
3438 inheritable_components
= sd_has_inheritable_components(parent_desc
,
3441 if (!inheritable_components
&& !inherit_owner
) {
3442 /* Nothing to inherit and not setting owner. */
3443 return NT_STATUS_OK
;
3446 /* Create an inherited descriptor from the parent. */
3448 if (DEBUGLEVEL
>= 10) {
3449 DEBUG(10,("inherit_new_acl: parent acl for %s is:\n",
3450 fsp_str_dbg(fsp
) ));
3451 NDR_PRINT_DEBUG(security_descriptor
, parent_desc
);
3454 /* Inherit from parent descriptor if "inherit owner" set. */
3455 if (inherit_owner
) {
3456 owner_sid
= parent_desc
->owner_sid
;
3457 group_sid
= parent_desc
->group_sid
;
3460 if (owner_sid
== NULL
) {
3461 owner_sid
= &fsp
->conn
->session_info
->security_token
->sids
[PRIMARY_USER_SID_INDEX
];
3463 if (group_sid
== NULL
) {
3464 group_sid
= &fsp
->conn
->session_info
->security_token
->sids
[PRIMARY_GROUP_SID_INDEX
];
3467 status
= se_create_child_secdesc(ctx
,
3474 if (!NT_STATUS_IS_OK(status
)) {
3478 /* If inheritable_components == false,
3479 se_create_child_secdesc()
3480 creates a security desriptor with a NULL dacl
3481 entry, but with SEC_DESC_DACL_PRESENT. We need
3482 to remove that flag. */
3484 if (!inheritable_components
) {
3485 security_info_sent
&= ~SECINFO_DACL
;
3486 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
3489 if (DEBUGLEVEL
>= 10) {
3490 DEBUG(10,("inherit_new_acl: child acl for %s is:\n",
3491 fsp_str_dbg(fsp
) ));
3492 NDR_PRINT_DEBUG(security_descriptor
, psd
);
3495 if (inherit_owner
) {
3496 /* We need to be root to force this. */
3499 status
= SMB_VFS_FSET_NT_ACL(fsp
,
3502 if (inherit_owner
) {
3509 * Wrapper around open_file_ntcreate and open_directory
3512 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
3513 struct smb_request
*req
,
3514 struct smb_filename
*smb_fname
,
3515 uint32_t access_mask
,
3516 uint32_t share_access
,
3517 uint32_t create_disposition
,
3518 uint32_t create_options
,
3519 uint32_t file_attributes
,
3520 uint32_t oplock_request
,
3521 uint64_t allocation_size
,
3522 uint32_t private_flags
,
3523 struct security_descriptor
*sd
,
3524 struct ea_list
*ea_list
,
3526 files_struct
**result
,
3529 int info
= FILE_WAS_OPENED
;
3530 files_struct
*base_fsp
= NULL
;
3531 files_struct
*fsp
= NULL
;
3534 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
3535 "file_attributes = 0x%x, share_access = 0x%x, "
3536 "create_disposition = 0x%x create_options = 0x%x "
3537 "oplock_request = 0x%x private_flags = 0x%x "
3538 "ea_list = 0x%p, sd = 0x%p, "
3540 (unsigned int)access_mask
,
3541 (unsigned int)file_attributes
,
3542 (unsigned int)share_access
,
3543 (unsigned int)create_disposition
,
3544 (unsigned int)create_options
,
3545 (unsigned int)oplock_request
,
3546 (unsigned int)private_flags
,
3547 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3549 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
3550 status
= NT_STATUS_NOT_SUPPORTED
;
3554 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
3555 status
= NT_STATUS_INVALID_PARAMETER
;
3560 oplock_request
|= INTERNAL_OPEN_ONLY
;
3563 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3564 && (access_mask
& DELETE_ACCESS
)
3565 && !is_ntfs_stream_smb_fname(smb_fname
)) {
3567 * We can't open a file with DELETE access if any of the
3568 * streams is open without FILE_SHARE_DELETE
3570 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
3572 if (!NT_STATUS_IS_OK(status
)) {
3577 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
3578 !security_token_has_privilege(get_current_nttok(conn
),
3579 SEC_PRIV_SECURITY
)) {
3580 DEBUG(10, ("create_file_unixpath: open on %s "
3581 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
3582 smb_fname_str_dbg(smb_fname
)));
3583 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
3587 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
3588 && is_ntfs_stream_smb_fname(smb_fname
)
3589 && (!(private_flags
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
3590 uint32 base_create_disposition
;
3591 struct smb_filename
*smb_fname_base
= NULL
;
3593 if (create_options
& FILE_DIRECTORY_FILE
) {
3594 status
= NT_STATUS_NOT_A_DIRECTORY
;
3598 switch (create_disposition
) {
3600 base_create_disposition
= FILE_OPEN
;
3603 base_create_disposition
= FILE_OPEN_IF
;
3607 /* Create an smb_filename with stream_name == NULL. */
3608 status
= create_synthetic_smb_fname(talloc_tos(),
3609 smb_fname
->base_name
,
3612 if (!NT_STATUS_IS_OK(status
)) {
3616 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3617 DEBUG(10, ("Unable to stat stream: %s\n",
3618 smb_fname_str_dbg(smb_fname_base
)));
3621 /* Open the base file. */
3622 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3625 | FILE_SHARE_DELETE
,
3626 base_create_disposition
,
3627 0, 0, 0, 0, 0, NULL
, NULL
,
3629 TALLOC_FREE(smb_fname_base
);
3631 if (!NT_STATUS_IS_OK(status
)) {
3632 DEBUG(10, ("create_file_unixpath for base %s failed: "
3633 "%s\n", smb_fname
->base_name
,
3634 nt_errstr(status
)));
3637 /* we don't need to low level fd */
3642 * If it's a request for a directory open, deal with it separately.
3645 if (create_options
& FILE_DIRECTORY_FILE
) {
3647 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3648 status
= NT_STATUS_INVALID_PARAMETER
;
3652 /* Can't open a temp directory. IFS kit test. */
3653 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3654 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3655 status
= NT_STATUS_INVALID_PARAMETER
;
3660 * We will get a create directory here if the Win32
3661 * app specified a security descriptor in the
3662 * CreateDirectory() call.
3666 status
= open_directory(
3667 conn
, req
, smb_fname
, access_mask
, share_access
,
3668 create_disposition
, create_options
, file_attributes
,
3673 * Ordinary file case.
3676 status
= file_new(req
, conn
, &fsp
);
3677 if(!NT_STATUS_IS_OK(status
)) {
3681 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3682 if (!NT_STATUS_IS_OK(status
)) {
3688 * We're opening the stream element of a
3689 * base_fsp we already opened. Set up the
3692 fsp
->base_fsp
= base_fsp
;
3695 if (allocation_size
) {
3696 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
,
3700 status
= open_file_ntcreate(conn
,
3712 if(!NT_STATUS_IS_OK(status
)) {
3713 file_free(req
, fsp
);
3717 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3719 /* A stream open never opens a directory */
3722 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3727 * Fail the open if it was explicitly a non-directory
3731 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3732 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3737 status
= open_directory(
3738 conn
, req
, smb_fname
, access_mask
,
3739 share_access
, create_disposition
,
3740 create_options
, file_attributes
,
3745 if (!NT_STATUS_IS_OK(status
)) {
3749 fsp
->base_fsp
= base_fsp
;
3751 if ((ea_list
!= NULL
) &&
3752 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3753 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3754 if (!NT_STATUS_IS_OK(status
)) {
3759 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3760 status
= NT_STATUS_ACCESS_DENIED
;
3764 /* Save the requested allocation size. */
3765 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3767 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3768 fsp
->initial_allocation_size
= smb_roundup(
3769 fsp
->conn
, allocation_size
);
3770 if (fsp
->is_directory
) {
3771 /* Can't set allocation size on a directory. */
3772 status
= NT_STATUS_ACCESS_DENIED
;
3775 if (vfs_allocate_file_space(
3776 fsp
, fsp
->initial_allocation_size
) == -1) {
3777 status
= NT_STATUS_DISK_FULL
;
3781 fsp
->initial_allocation_size
= smb_roundup(
3782 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3785 fsp
->initial_allocation_size
= 0;
3788 if ((info
== FILE_WAS_CREATED
) && lp_nt_acl_support(SNUM(conn
)) &&
3789 fsp
->base_fsp
== NULL
) {
3792 * According to the MS documentation, the only time the security
3793 * descriptor is applied to the opened file is iff we *created* the
3794 * file; an existing file stays the same.
3796 * Also, it seems (from observation) that you can open the file with
3797 * any access mask but you can still write the sd. We need to override
3798 * the granted access before we call set_sd
3799 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3802 uint32_t sec_info_sent
;
3803 uint32_t saved_access_mask
= fsp
->access_mask
;
3805 sec_info_sent
= get_sec_info(sd
);
3807 fsp
->access_mask
= FILE_GENERIC_ALL
;
3809 if (sec_info_sent
& (SECINFO_OWNER
|
3813 status
= set_sd(fsp
, sd
, sec_info_sent
);
3816 fsp
->access_mask
= saved_access_mask
;
3818 if (!NT_STATUS_IS_OK(status
)) {
3821 } else if (lp_inherit_acls(SNUM(conn
))) {
3822 /* Inherit from parent. Errors here are not fatal. */
3823 status
= inherit_new_acl(fsp
);
3824 if (!NT_STATUS_IS_OK(status
)) {
3825 DEBUG(10,("inherit_new_acl: failed for %s with %s\n",
3827 nt_errstr(status
) ));
3832 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3835 if (pinfo
!= NULL
) {
3839 smb_fname
->st
= fsp
->fsp_name
->st
;
3841 return NT_STATUS_OK
;
3844 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3847 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3849 * The close_file below will close
3854 close_file(req
, fsp
, ERROR_CLOSE
);
3857 if (base_fsp
!= NULL
) {
3858 close_file(req
, base_fsp
, ERROR_CLOSE
);
3865 * Calculate the full path name given a relative fid.
3867 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
3868 struct smb_request
*req
,
3869 uint16_t root_dir_fid
,
3870 const struct smb_filename
*smb_fname
,
3871 struct smb_filename
**smb_fname_out
)
3873 files_struct
*dir_fsp
;
3874 char *parent_fname
= NULL
;
3875 char *new_base_name
= NULL
;
3878 if (root_dir_fid
== 0 || !smb_fname
) {
3879 status
= NT_STATUS_INTERNAL_ERROR
;
3883 dir_fsp
= file_fsp(req
, root_dir_fid
);
3885 if (dir_fsp
== NULL
) {
3886 status
= NT_STATUS_INVALID_HANDLE
;
3890 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
3891 status
= NT_STATUS_INVALID_HANDLE
;
3895 if (!dir_fsp
->is_directory
) {
3898 * Check to see if this is a mac fork of some kind.
3901 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
3902 is_ntfs_stream_smb_fname(smb_fname
)) {
3903 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3908 we need to handle the case when we get a
3909 relative open relative to a file and the
3910 pathname is blank - this is a reopen!
3911 (hint from demyn plantenberg)
3914 status
= NT_STATUS_INVALID_HANDLE
;
3918 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
3920 * We're at the toplevel dir, the final file name
3921 * must not contain ./, as this is filtered out
3922 * normally by srvstr_get_path and unix_convert
3923 * explicitly rejects paths containing ./.
3925 parent_fname
= talloc_strdup(talloc_tos(), "");
3926 if (parent_fname
== NULL
) {
3927 status
= NT_STATUS_NO_MEMORY
;
3931 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
3934 * Copy in the base directory name.
3937 parent_fname
= talloc_array(talloc_tos(), char,
3939 if (parent_fname
== NULL
) {
3940 status
= NT_STATUS_NO_MEMORY
;
3943 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
3947 * Ensure it ends in a '/'.
3948 * We used TALLOC_SIZE +2 to add space for the '/'.
3952 && (parent_fname
[dir_name_len
-1] != '\\')
3953 && (parent_fname
[dir_name_len
-1] != '/')) {
3954 parent_fname
[dir_name_len
] = '/';
3955 parent_fname
[dir_name_len
+1] = '\0';
3959 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
3960 smb_fname
->base_name
);
3961 if (new_base_name
== NULL
) {
3962 status
= NT_STATUS_NO_MEMORY
;
3966 status
= filename_convert(req
,
3968 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
3973 if (!NT_STATUS_IS_OK(status
)) {
3978 TALLOC_FREE(parent_fname
);
3979 TALLOC_FREE(new_base_name
);
3983 NTSTATUS
create_file_default(connection_struct
*conn
,
3984 struct smb_request
*req
,
3985 uint16_t root_dir_fid
,
3986 struct smb_filename
*smb_fname
,
3987 uint32_t access_mask
,
3988 uint32_t share_access
,
3989 uint32_t create_disposition
,
3990 uint32_t create_options
,
3991 uint32_t file_attributes
,
3992 uint32_t oplock_request
,
3993 uint64_t allocation_size
,
3994 uint32_t private_flags
,
3995 struct security_descriptor
*sd
,
3996 struct ea_list
*ea_list
,
3997 files_struct
**result
,
4000 int info
= FILE_WAS_OPENED
;
4001 files_struct
*fsp
= NULL
;
4003 bool stream_name
= false;
4005 DEBUG(10,("create_file: access_mask = 0x%x "
4006 "file_attributes = 0x%x, share_access = 0x%x, "
4007 "create_disposition = 0x%x create_options = 0x%x "
4008 "oplock_request = 0x%x "
4009 "private_flags = 0x%x "
4010 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
4012 (unsigned int)access_mask
,
4013 (unsigned int)file_attributes
,
4014 (unsigned int)share_access
,
4015 (unsigned int)create_disposition
,
4016 (unsigned int)create_options
,
4017 (unsigned int)oplock_request
,
4018 (unsigned int)private_flags
,
4019 (unsigned int)root_dir_fid
,
4020 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
4023 * Calculate the filename from the root_dir_if if necessary.
4026 if (root_dir_fid
!= 0) {
4027 struct smb_filename
*smb_fname_out
= NULL
;
4028 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
4029 smb_fname
, &smb_fname_out
);
4030 if (!NT_STATUS_IS_OK(status
)) {
4033 smb_fname
= smb_fname_out
;
4037 * Check to see if this is a mac fork of some kind.
4040 stream_name
= is_ntfs_stream_smb_fname(smb_fname
);
4042 enum FAKE_FILE_TYPE fake_file_type
;
4044 fake_file_type
= is_fake_file(smb_fname
);
4046 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
4049 * Here we go! support for changing the disk quotas
4052 * We need to fake up to open this MAGIC QUOTA file
4053 * and return a valid FID.
4055 * w2k close this file directly after openening xp
4056 * also tries a QUERY_FILE_INFO on the file and then
4059 status
= open_fake_file(req
, conn
, req
->vuid
,
4060 fake_file_type
, smb_fname
,
4062 if (!NT_STATUS_IS_OK(status
)) {
4066 ZERO_STRUCT(smb_fname
->st
);
4070 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
4071 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4076 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
4078 smb_fname
->stream_name
= NULL
;
4079 /* We have to handle this error here. */
4080 if (create_options
& FILE_DIRECTORY_FILE
) {
4081 status
= NT_STATUS_NOT_A_DIRECTORY
;
4084 if (lp_posix_pathnames()) {
4085 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
4087 ret
= SMB_VFS_STAT(conn
, smb_fname
);
4090 if (ret
== 0 && VALID_STAT_OF_DIR(smb_fname
->st
)) {
4091 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
4096 status
= create_file_unixpath(
4097 conn
, req
, smb_fname
, access_mask
, share_access
,
4098 create_disposition
, create_options
, file_attributes
,
4099 oplock_request
, allocation_size
, private_flags
,
4103 if (!NT_STATUS_IS_OK(status
)) {
4108 DEBUG(10, ("create_file: info=%d\n", info
));
4111 if (pinfo
!= NULL
) {
4114 return NT_STATUS_OK
;
4117 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
4120 close_file(req
, fsp
, ERROR_CLOSE
);