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 "smbd/globals.h"
25 extern struct current_user current_user
;
26 extern const struct generic_mapping file_generic_mapping
;
28 struct deferred_open_record
{
29 bool delayed_for_oplocks
;
33 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
34 struct smb_request
*req
,
35 struct smb_filename
*smb_fname
,
37 uint32_t share_access
,
38 uint32_t create_disposition
,
39 uint32_t create_options
,
40 uint32_t file_attributes
,
41 uint32_t oplock_request
,
42 uint64_t allocation_size
,
43 struct security_descriptor
*sd
,
44 struct ea_list
*ea_list
,
46 files_struct
**result
,
49 /****************************************************************************
50 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
51 ****************************************************************************/
53 NTSTATUS
smb1_file_se_access_check(connection_struct
*conn
,
54 const struct security_descriptor
*sd
,
55 const NT_USER_TOKEN
*token
,
56 uint32_t access_desired
,
57 uint32_t *access_granted
)
61 if (conn
->server_info
->utok
.uid
== 0 || conn
->admin_user
) {
62 /* I'm sorry sir, I didn't know you were root... */
63 *access_granted
= access_desired
;
64 if (access_desired
& SEC_FLAG_MAXIMUM_ALLOWED
) {
65 *access_granted
|= FILE_GENERIC_ALL
;
70 return se_access_check(sd
,
72 (access_desired
& ~FILE_READ_ATTRIBUTES
),
76 /****************************************************************************
77 Check if we have open rights.
78 ****************************************************************************/
80 NTSTATUS
smbd_check_open_rights(struct connection_struct
*conn
,
81 const struct smb_filename
*smb_fname
,
83 uint32_t *access_granted
)
85 /* Check if we have rights to open. */
87 struct security_descriptor
*sd
= NULL
;
89 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
90 (OWNER_SECURITY_INFORMATION
|
91 GROUP_SECURITY_INFORMATION
|
92 DACL_SECURITY_INFORMATION
),&sd
);
94 if (!NT_STATUS_IS_OK(status
)) {
95 DEBUG(10, ("smbd_check_open_rights: Could not get acl "
97 smb_fname_str_dbg(smb_fname
),
102 status
= smb1_file_se_access_check(conn
,
104 conn
->server_info
->ptok
,
108 DEBUG(10,("smbd_check_open_rights: file %s requesting "
109 "0x%x returning 0x%x (%s)\n",
110 smb_fname_str_dbg(smb_fname
),
111 (unsigned int)access_mask
,
112 (unsigned int)*access_granted
,
113 nt_errstr(status
) ));
115 if (!NT_STATUS_IS_OK(status
)) {
116 if (DEBUGLEVEL
>= 10) {
117 DEBUG(10,("smbd_check_open_rights: acl for %s is:\n",
118 smb_fname_str_dbg(smb_fname
) ));
119 NDR_PRINT_DEBUG(security_descriptor
, sd
);
128 /****************************************************************************
129 fd support routines - attempt to do a dos_open.
130 ****************************************************************************/
132 static NTSTATUS
fd_open(struct connection_struct
*conn
,
137 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
138 NTSTATUS status
= NT_STATUS_OK
;
142 * Never follow symlinks on a POSIX client. The
143 * client should be doing this.
146 if (fsp
->posix_open
|| !lp_symlinks(SNUM(conn
))) {
151 fsp
->fh
->fd
= SMB_VFS_OPEN(conn
, smb_fname
, fsp
, flags
, mode
);
152 if (fsp
->fh
->fd
== -1) {
153 status
= map_nt_error_from_unix(errno
);
154 if (errno
== EMFILE
) {
155 static time_t last_warned
= 0L;
157 if (time((time_t *) NULL
) > last_warned
) {
158 DEBUG(0,("Too many open files, unable "
159 "to open more! smbd's max "
161 lp_max_open_files()));
162 last_warned
= time((time_t *) NULL
);
168 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
169 smb_fname_str_dbg(smb_fname
), flags
, (int)mode
, fsp
->fh
->fd
,
170 (fsp
->fh
->fd
== -1) ? strerror(errno
) : "" ));
175 /****************************************************************************
176 Close the file associated with a fsp.
177 ****************************************************************************/
179 NTSTATUS
fd_close(files_struct
*fsp
)
183 if (fsp
->fh
->fd
== -1) {
184 return NT_STATUS_OK
; /* What we used to call a stat open. */
186 if (fsp
->fh
->ref_count
> 1) {
187 return NT_STATUS_OK
; /* Shared handle. Only close last reference. */
190 ret
= SMB_VFS_CLOSE(fsp
);
193 return map_nt_error_from_unix(errno
);
198 /****************************************************************************
199 Change the ownership of a file to that of the parent directory.
200 Do this by fd if possible.
201 ****************************************************************************/
203 void change_file_owner_to_parent(connection_struct
*conn
,
204 const char *inherit_from_dir
,
207 struct smb_filename
*smb_fname_parent
= NULL
;
211 status
= create_synthetic_smb_fname(talloc_tos(), inherit_from_dir
,
212 NULL
, NULL
, &smb_fname_parent
);
213 if (!NT_STATUS_IS_OK(status
)) {
217 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
219 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
220 "directory %s. Error was %s\n",
221 smb_fname_str_dbg(smb_fname_parent
),
227 ret
= SMB_VFS_FCHOWN(fsp
, smb_fname_parent
->st
.st_ex_uid
, (gid_t
)-1);
230 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
231 "file %s to parent directory uid %u. Error "
232 "was %s\n", fsp_str_dbg(fsp
),
233 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
236 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
237 "parent directory uid %u.\n", fsp_str_dbg(fsp
),
238 (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
240 /* Ensure the uid entry is updated. */
241 fsp
->fsp_name
->st
.st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
244 TALLOC_FREE(smb_fname_parent
);
247 NTSTATUS
change_dir_owner_to_parent(connection_struct
*conn
,
248 const char *inherit_from_dir
,
250 SMB_STRUCT_STAT
*psbuf
)
252 struct smb_filename
*smb_fname_parent
= NULL
;
253 struct smb_filename
*smb_fname_cwd
= NULL
;
254 char *saved_dir
= NULL
;
255 TALLOC_CTX
*ctx
= talloc_tos();
256 NTSTATUS status
= NT_STATUS_OK
;
259 status
= create_synthetic_smb_fname(ctx
, inherit_from_dir
, NULL
, NULL
,
261 if (!NT_STATUS_IS_OK(status
)) {
265 ret
= SMB_VFS_STAT(conn
, smb_fname_parent
);
267 status
= map_nt_error_from_unix(errno
);
268 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
269 "directory %s. Error was %s\n",
270 smb_fname_str_dbg(smb_fname_parent
),
275 /* We've already done an lstat into psbuf, and we know it's a
276 directory. If we can cd into the directory and the dev/ino
277 are the same then we can safely chown without races as
278 we're locking the directory in place by being in it. This
279 should work on any UNIX (thanks tridge :-). JRA.
282 saved_dir
= vfs_GetWd(ctx
,conn
);
284 status
= map_nt_error_from_unix(errno
);
285 DEBUG(0,("change_dir_owner_to_parent: failed to get "
286 "current working directory. Error was %s\n",
291 /* Chdir into the new path. */
292 if (vfs_ChDir(conn
, fname
) == -1) {
293 status
= map_nt_error_from_unix(errno
);
294 DEBUG(0,("change_dir_owner_to_parent: failed to change "
295 "current working directory to %s. Error "
296 "was %s\n", fname
, strerror(errno
) ));
300 status
= create_synthetic_smb_fname(ctx
, ".", NULL
, NULL
,
302 if (!NT_STATUS_IS_OK(status
)) {
306 ret
= SMB_VFS_STAT(conn
, smb_fname_cwd
);
308 status
= map_nt_error_from_unix(errno
);
309 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
310 "directory '.' (%s) Error was %s\n",
311 fname
, strerror(errno
)));
315 /* Ensure we're pointing at the same place. */
316 if (smb_fname_cwd
->st
.st_ex_dev
!= psbuf
->st_ex_dev
||
317 smb_fname_cwd
->st
.st_ex_ino
!= psbuf
->st_ex_ino
||
318 smb_fname_cwd
->st
.st_ex_mode
!= psbuf
->st_ex_mode
) {
319 DEBUG(0,("change_dir_owner_to_parent: "
320 "device/inode/mode on directory %s changed. "
321 "Refusing to chown !\n", fname
));
322 status
= NT_STATUS_ACCESS_DENIED
;
327 ret
= SMB_VFS_CHOWN(conn
, ".", smb_fname_parent
->st
.st_ex_uid
,
331 status
= map_nt_error_from_unix(errno
);
332 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
333 "directory %s to parent directory uid %u. "
334 "Error was %s\n", fname
,
335 (unsigned int)smb_fname_parent
->st
.st_ex_uid
,
340 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
341 "directory %s to parent directory uid %u.\n",
342 fname
, (unsigned int)smb_fname_parent
->st
.st_ex_uid
));
344 /* Ensure the uid entry is updated. */
345 psbuf
->st_ex_uid
= smb_fname_parent
->st
.st_ex_uid
;
348 vfs_ChDir(conn
,saved_dir
);
350 TALLOC_FREE(smb_fname_parent
);
351 TALLOC_FREE(smb_fname_cwd
);
355 /****************************************************************************
357 ****************************************************************************/
359 static NTSTATUS
open_file(files_struct
*fsp
,
360 connection_struct
*conn
,
361 struct smb_request
*req
,
362 const char *parent_dir
,
365 uint32 access_mask
, /* client requested access mask. */
366 uint32 open_access_mask
) /* what we're actually using in the open. */
368 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
369 NTSTATUS status
= NT_STATUS_OK
;
370 int accmode
= (flags
& O_ACCMODE
);
371 int local_flags
= flags
;
372 bool file_existed
= VALID_STAT(fsp
->fsp_name
->st
);
377 /* Check permissions */
380 * This code was changed after seeing a client open request
381 * containing the open mode of (DENY_WRITE/read-only) with
382 * the 'create if not exist' bit set. The previous code
383 * would fail to open the file read only on a read-only share
384 * as it was checking the flags parameter directly against O_RDONLY,
385 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
389 if (!CAN_WRITE(conn
)) {
390 /* It's a read-only share - fail if we wanted to write. */
391 if(accmode
!= O_RDONLY
|| (flags
& O_TRUNC
) || (flags
& O_APPEND
)) {
392 DEBUG(3,("Permission denied opening %s\n",
393 smb_fname_str_dbg(smb_fname
)));
394 return NT_STATUS_ACCESS_DENIED
;
395 } else if(flags
& O_CREAT
) {
396 /* We don't want to write - but we must make sure that
397 O_CREAT doesn't create the file if we have write
398 access into the directory.
400 flags
&= ~(O_CREAT
|O_EXCL
);
401 local_flags
&= ~(O_CREAT
|O_EXCL
);
406 * This little piece of insanity is inspired by the
407 * fact that an NT client can open a file for O_RDONLY,
408 * but set the create disposition to FILE_EXISTS_TRUNCATE.
409 * If the client *can* write to the file, then it expects to
410 * truncate the file, even though it is opening for readonly.
411 * Quicken uses this stupid trick in backup file creation...
412 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
413 * for helping track this one down. It didn't bite us in 2.0.x
414 * as we always opened files read-write in that release. JRA.
417 if ((accmode
== O_RDONLY
) && ((flags
& O_TRUNC
) == O_TRUNC
)) {
418 DEBUG(10,("open_file: truncate requested on read-only open "
419 "for file %s\n", smb_fname_str_dbg(smb_fname
)));
420 local_flags
= (flags
& ~O_ACCMODE
)|O_RDWR
;
423 if ((open_access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_EXECUTE
)) ||
424 (!file_existed
&& (local_flags
& O_CREAT
)) ||
425 ((local_flags
& O_TRUNC
) == O_TRUNC
) ) {
429 * We can't actually truncate here as the file may be locked.
430 * open_file_ntcreate will take care of the truncate later. JRA.
433 local_flags
&= ~O_TRUNC
;
435 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
437 * We would block on opening a FIFO with no one else on the
438 * other end. Do what we used to do and add O_NONBLOCK to the
442 if (file_existed
&& S_ISFIFO(smb_fname
->st
.st_ex_mode
)) {
443 local_flags
|= O_NONBLOCK
;
447 /* Don't create files with Microsoft wildcard characters. */
450 * wildcard characters are allowed in stream names
451 * only test the basefilename
453 wild
= fsp
->base_fsp
->fsp_name
->base_name
;
455 wild
= smb_fname
->base_name
;
457 if ((local_flags
& O_CREAT
) && !file_existed
&&
459 return NT_STATUS_OBJECT_NAME_INVALID
;
462 /* Actually do the open */
463 status
= fd_open(conn
, fsp
, local_flags
, unx_mode
);
464 if (!NT_STATUS_IS_OK(status
)) {
465 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
466 "(flags=%d)\n", smb_fname_str_dbg(smb_fname
),
467 nt_errstr(status
),local_flags
,flags
));
471 if ((local_flags
& O_CREAT
) && !file_existed
) {
473 /* Inherit the ACL if required */
474 if (lp_inherit_perms(SNUM(conn
))) {
475 inherit_access_posix_acl(conn
, parent_dir
,
476 smb_fname
->base_name
,
480 /* Change the owner if required. */
481 if (lp_inherit_owner(SNUM(conn
))) {
482 change_file_owner_to_parent(conn
, parent_dir
,
486 notify_fname(conn
, NOTIFY_ACTION_ADDED
,
487 FILE_NOTIFY_CHANGE_FILE_NAME
,
488 smb_fname
->base_name
);
492 fsp
->fh
->fd
= -1; /* What we used to call a stat open. */
494 uint32_t access_granted
= 0;
496 status
= smbd_check_open_rights(conn
,
500 if (!NT_STATUS_IS_OK(status
)) {
501 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
503 * On NT_STATUS_ACCESS_DENIED, access_granted
504 * contains the denied bits.
507 if ((access_mask
& FILE_WRITE_ATTRIBUTES
) &&
508 (access_granted
& FILE_WRITE_ATTRIBUTES
) &&
509 (lp_map_readonly(SNUM(conn
)) ||
510 lp_map_archive(SNUM(conn
)) ||
511 lp_map_hidden(SNUM(conn
)) ||
512 lp_map_system(SNUM(conn
)))) {
513 access_granted
&= ~FILE_WRITE_ATTRIBUTES
;
515 DEBUG(10,("open_file: "
524 if ((access_mask
& DELETE_ACCESS
) &&
525 (access_granted
& DELETE_ACCESS
) &&
526 can_delete_file_in_directory(conn
,
528 /* Were we trying to do a stat open
529 * for delete and didn't get DELETE
530 * access (only) ? Check if the
531 * directory allows DELETE_CHILD.
533 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
536 access_granted
&= ~DELETE_ACCESS
;
538 DEBUG(10,("open_file: "
546 if (access_granted
!= 0) {
547 DEBUG(10,("open_file: Access "
554 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) &&
556 S_ISLNK(smb_fname
->st
.st_ex_mode
)) {
557 /* This is a POSIX stat open for delete
558 * or rename on a symlink that points
560 DEBUG(10,("open_file: allowing POSIX "
561 "open on bad symlink %s\n",
565 DEBUG(10,("open_file: "
566 "smbd_check_open_rights on file "
568 smb_fname_str_dbg(smb_fname
),
569 nt_errstr(status
) ));
579 if (fsp
->fh
->fd
== -1) {
580 ret
= SMB_VFS_STAT(conn
, smb_fname
);
582 ret
= SMB_VFS_FSTAT(fsp
, &smb_fname
->st
);
583 /* If we have an fd, this stat should succeed. */
585 DEBUG(0,("Error doing fstat on open file %s "
587 smb_fname_str_dbg(smb_fname
),
592 /* For a non-io open, this stat failing means file not found. JRA */
594 status
= map_nt_error_from_unix(errno
);
601 * POSIX allows read-only opens of directories. We don't
602 * want to do this (we use a different code path for this)
603 * so catch a directory open and return an EISDIR. JRA.
606 if(S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
609 return NT_STATUS_FILE_IS_A_DIRECTORY
;
612 fsp
->mode
= smb_fname
->st
.st_ex_mode
;
613 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
614 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
615 fsp
->file_pid
= req
? req
->smbpid
: 0;
616 fsp
->can_lock
= True
;
617 fsp
->can_read
= (access_mask
& (FILE_READ_DATA
)) ? True
: False
;
618 if (!CAN_WRITE(conn
)) {
619 fsp
->can_write
= False
;
621 fsp
->can_write
= (access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ?
624 fsp
->print_file
= False
;
625 fsp
->modified
= False
;
626 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
627 fsp
->is_directory
= False
;
628 if (conn
->aio_write_behind_list
&&
629 is_in_path(smb_fname
->base_name
, conn
->aio_write_behind_list
,
630 conn
->case_sensitive
)) {
631 fsp
->aio_write_behind
= True
;
634 fsp
->wcp
= NULL
; /* Write cache pointer. */
636 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
637 conn
->server_info
->unix_name
,
638 smb_fname_str_dbg(smb_fname
),
639 BOOLSTR(fsp
->can_read
), BOOLSTR(fsp
->can_write
),
640 conn
->num_files_open
));
646 /*******************************************************************
647 Return True if the filename is one of the special executable types.
648 ********************************************************************/
650 bool is_executable(const char *fname
)
652 if ((fname
= strrchr_m(fname
,'.'))) {
653 if (strequal(fname
,".com") ||
654 strequal(fname
,".dll") ||
655 strequal(fname
,".exe") ||
656 strequal(fname
,".sym")) {
663 /****************************************************************************
664 Check if we can open a file with a share mode.
665 Returns True if conflict, False if not.
666 ****************************************************************************/
668 static bool share_conflict(struct share_mode_entry
*entry
,
672 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
673 "entry->share_access = 0x%x, "
674 "entry->private_options = 0x%x\n",
675 (unsigned int)entry
->access_mask
,
676 (unsigned int)entry
->share_access
,
677 (unsigned int)entry
->private_options
));
679 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
680 (unsigned int)access_mask
, (unsigned int)share_access
));
682 if ((entry
->access_mask
& (FILE_WRITE_DATA
|
686 DELETE_ACCESS
)) == 0) {
687 DEBUG(10,("share_conflict: No conflict due to "
688 "entry->access_mask = 0x%x\n",
689 (unsigned int)entry
->access_mask
));
693 if ((access_mask
& (FILE_WRITE_DATA
|
697 DELETE_ACCESS
)) == 0) {
698 DEBUG(10,("share_conflict: No conflict due to "
699 "access_mask = 0x%x\n",
700 (unsigned int)access_mask
));
704 #if 1 /* JRA TEST - Superdebug. */
705 #define CHECK_MASK(num, am, right, sa, share) \
706 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
707 (unsigned int)(num), (unsigned int)(am), \
708 (unsigned int)(right), (unsigned int)(am)&(right) )); \
709 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
710 (unsigned int)(num), (unsigned int)(sa), \
711 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
712 if (((am) & (right)) && !((sa) & (share))) { \
713 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
714 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
715 (unsigned int)(share) )); \
719 #define CHECK_MASK(num, am, right, sa, share) \
720 if (((am) & (right)) && !((sa) & (share))) { \
721 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
722 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
723 (unsigned int)(share) )); \
728 CHECK_MASK(1, entry
->access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
729 share_access
, FILE_SHARE_WRITE
);
730 CHECK_MASK(2, access_mask
, FILE_WRITE_DATA
| FILE_APPEND_DATA
,
731 entry
->share_access
, FILE_SHARE_WRITE
);
733 CHECK_MASK(3, entry
->access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
734 share_access
, FILE_SHARE_READ
);
735 CHECK_MASK(4, access_mask
, FILE_READ_DATA
| FILE_EXECUTE
,
736 entry
->share_access
, FILE_SHARE_READ
);
738 CHECK_MASK(5, entry
->access_mask
, DELETE_ACCESS
,
739 share_access
, FILE_SHARE_DELETE
);
740 CHECK_MASK(6, access_mask
, DELETE_ACCESS
,
741 entry
->share_access
, FILE_SHARE_DELETE
);
743 DEBUG(10,("share_conflict: No conflict.\n"));
747 #if defined(DEVELOPER)
748 static void validate_my_share_entries(int num
,
749 struct share_mode_entry
*share_entry
)
753 if (!procid_is_me(&share_entry
->pid
)) {
757 if (is_deferred_open_entry(share_entry
) &&
758 !open_was_deferred(share_entry
->op_mid
)) {
759 char *str
= talloc_asprintf(talloc_tos(),
760 "Got a deferred entry without a request: "
762 share_mode_str(talloc_tos(), num
, share_entry
));
766 if (!is_valid_share_mode_entry(share_entry
)) {
770 fsp
= file_find_dif(share_entry
->id
,
771 share_entry
->share_file_id
);
773 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
774 share_mode_str(talloc_tos(), num
, share_entry
) ));
775 smb_panic("validate_my_share_entries: Cannot match a "
776 "share entry with an open file\n");
779 if (is_deferred_open_entry(share_entry
) ||
780 is_unused_share_mode_entry(share_entry
)) {
784 if ((share_entry
->op_type
== NO_OPLOCK
) &&
785 (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
)) {
786 /* Someone has already written to it, but I haven't yet
791 if (((uint16
)fsp
->oplock_type
) != share_entry
->op_type
) {
800 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
801 share_mode_str(talloc_tos(), num
, share_entry
) ));
802 str
= talloc_asprintf(talloc_tos(),
803 "validate_my_share_entries: "
804 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
805 fsp
->fsp_name
->base_name
,
806 (unsigned int)fsp
->oplock_type
,
807 (unsigned int)share_entry
->op_type
);
813 bool is_stat_open(uint32 access_mask
)
815 return (access_mask
&&
816 ((access_mask
& ~(SYNCHRONIZE_ACCESS
| FILE_READ_ATTRIBUTES
|
817 FILE_WRITE_ATTRIBUTES
))==0) &&
818 ((access_mask
& (SYNCHRONIZE_ACCESS
|FILE_READ_ATTRIBUTES
|
819 FILE_WRITE_ATTRIBUTES
)) != 0));
822 /****************************************************************************
823 Deal with share modes
824 Invarient: Share mode must be locked on entry and exit.
825 Returns -1 on error, or number of share modes on success (may be zero).
826 ****************************************************************************/
828 static NTSTATUS
open_mode_check(connection_struct
*conn
,
829 struct share_mode_lock
*lck
,
832 uint32 create_options
,
837 if(lck
->num_share_modes
== 0) {
841 *file_existed
= True
;
843 /* A delete on close prohibits everything */
845 if (lck
->delete_on_close
) {
846 return NT_STATUS_DELETE_PENDING
;
849 if (is_stat_open(access_mask
)) {
850 /* Stat open that doesn't trigger oplock breaks or share mode
851 * checks... ! JRA. */
856 * Check if the share modes will give us access.
859 #if defined(DEVELOPER)
860 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
861 validate_my_share_entries(i
, &lck
->share_modes
[i
]);
865 if (!lp_share_modes(SNUM(conn
))) {
869 /* Now we check the share modes, after any oplock breaks. */
870 for(i
= 0; i
< lck
->num_share_modes
; i
++) {
872 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
876 /* someone else has a share lock on it, check to see if we can
878 if (share_conflict(&lck
->share_modes
[i
],
879 access_mask
, share_access
)) {
880 return NT_STATUS_SHARING_VIOLATION
;
887 static bool is_delete_request(files_struct
*fsp
) {
888 return ((fsp
->access_mask
== DELETE_ACCESS
) &&
889 (fsp
->oplock_type
== NO_OPLOCK
));
893 * Send a break message to the oplock holder and delay the open for
897 static NTSTATUS
send_break_message(files_struct
*fsp
,
898 struct share_mode_entry
*exclusive
,
903 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
905 DEBUG(10, ("Sending break request to PID %s\n",
906 procid_str_static(&exclusive
->pid
)));
907 exclusive
->op_mid
= mid
;
909 /* Create the message. */
910 share_mode_entry_to_message(msg
, exclusive
);
912 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
913 don't want this set in the share mode struct pointed to by lck. */
915 if (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
) {
916 SSVAL(msg
,6,exclusive
->op_type
| FORCE_OPLOCK_BREAK_TO_NONE
);
919 status
= messaging_send_buf(smbd_messaging_context(), exclusive
->pid
,
920 MSG_SMB_BREAK_REQUEST
,
922 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
923 if (!NT_STATUS_IS_OK(status
)) {
924 DEBUG(3, ("Could not send oplock break message: %s\n",
932 * 1) No files open at all or internal open: Grant whatever the client wants.
934 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
935 * request, break if the oplock around is a batch oplock. If it's another
936 * requested access type, break.
938 * 3) Only level2 around: Grant level2 and do nothing else.
941 static bool delay_for_oplocks(struct share_mode_lock
*lck
,
948 struct share_mode_entry
*exclusive
= NULL
;
949 bool valid_entry
= false;
950 bool have_level2
= false;
951 bool have_a_none_oplock
= false;
952 bool allow_level2
= (global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
953 lp_level2_oplocks(SNUM(fsp
->conn
));
955 if (oplock_request
& INTERNAL_OPEN_ONLY
) {
956 fsp
->oplock_type
= NO_OPLOCK
;
959 if ((oplock_request
& INTERNAL_OPEN_ONLY
) || is_stat_open(fsp
->access_mask
)) {
963 for (i
=0; i
<lck
->num_share_modes
; i
++) {
965 if (!is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
969 /* At least one entry is not an invalid or deferred entry. */
972 if (pass_number
== 1) {
973 if (BATCH_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
974 SMB_ASSERT(exclusive
== NULL
);
975 exclusive
= &lck
->share_modes
[i
];
978 if (EXCLUSIVE_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
979 SMB_ASSERT(exclusive
== NULL
);
980 exclusive
= &lck
->share_modes
[i
];
984 if (LEVEL_II_OPLOCK_TYPE(lck
->share_modes
[i
].op_type
)) {
985 SMB_ASSERT(exclusive
== NULL
);
989 if (lck
->share_modes
[i
].op_type
== NO_OPLOCK
) {
990 have_a_none_oplock
= true;
994 if (exclusive
!= NULL
) { /* Found an exclusive oplock */
995 bool delay_it
= is_delete_request(fsp
) ?
996 BATCH_OPLOCK_TYPE(exclusive
->op_type
) : true;
997 SMB_ASSERT(!have_level2
);
999 send_break_message(fsp
, exclusive
, mid
, oplock_request
);
1005 * Match what was requested (fsp->oplock_type) with
1006 * what was found in the existing share modes.
1010 /* All entries are placeholders or deferred.
1011 * Directly grant whatever the client wants. */
1012 if (fsp
->oplock_type
== NO_OPLOCK
) {
1013 /* Store a level2 oplock, but don't tell the client */
1014 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1016 } else if (have_a_none_oplock
) {
1017 fsp
->oplock_type
= NO_OPLOCK
;
1018 } else if (have_level2
) {
1019 if (fsp
->oplock_type
== NO_OPLOCK
||
1020 fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
1021 /* Store a level2 oplock, but don't tell the client */
1022 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1024 fsp
->oplock_type
= LEVEL_II_OPLOCK
;
1027 /* This case can never happen. */
1032 * Don't grant level2 to clients that don't want them
1033 * or if we've turned them off.
1035 if (fsp
->oplock_type
== LEVEL_II_OPLOCK
&& !allow_level2
) {
1036 fsp
->oplock_type
= FAKE_LEVEL_II_OPLOCK
;
1039 DEBUG(10,("delay_for_oplocks: oplock type 0x%x on file %s\n",
1040 fsp
->oplock_type
, fsp_str_dbg(fsp
)));
1046 bool request_timed_out(struct timeval request_time
,
1047 struct timeval timeout
)
1049 struct timeval now
, end_time
;
1051 end_time
= timeval_sum(&request_time
, &timeout
);
1052 return (timeval_compare(&end_time
, &now
) < 0);
1055 /****************************************************************************
1056 Handle the 1 second delay in returning a SHARING_VIOLATION error.
1057 ****************************************************************************/
1059 static void defer_open(struct share_mode_lock
*lck
,
1060 struct timeval request_time
,
1061 struct timeval timeout
,
1062 struct smb_request
*req
,
1063 struct deferred_open_record
*state
)
1067 /* Paranoia check */
1069 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1070 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1072 if (!is_deferred_open_entry(e
)) {
1076 if (procid_is_me(&e
->pid
) && (e
->op_mid
== req
->mid
)) {
1077 DEBUG(0, ("Trying to defer an already deferred "
1078 "request: mid=%d, exiting\n", req
->mid
));
1079 exit_server("attempt to defer a deferred request");
1083 /* End paranoia check */
1085 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
1086 "open entry for mid %u\n",
1087 (unsigned int)request_time
.tv_sec
,
1088 (unsigned int)request_time
.tv_usec
,
1089 (unsigned int)req
->mid
));
1091 if (!push_deferred_smb_message(req
, request_time
, timeout
,
1092 (char *)state
, sizeof(*state
))) {
1093 exit_server("push_deferred_smb_message failed");
1095 add_deferred_open(lck
, req
->mid
, request_time
, state
->id
);
1099 /****************************************************************************
1100 On overwrite open ensure that the attributes match.
1101 ****************************************************************************/
1103 bool open_match_attributes(connection_struct
*conn
,
1104 uint32 old_dos_attr
,
1105 uint32 new_dos_attr
,
1106 mode_t existing_unx_mode
,
1107 mode_t new_unx_mode
,
1108 mode_t
*returned_unx_mode
)
1110 uint32 noarch_old_dos_attr
, noarch_new_dos_attr
;
1112 noarch_old_dos_attr
= (old_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1113 noarch_new_dos_attr
= (new_dos_attr
& ~FILE_ATTRIBUTE_ARCHIVE
);
1115 if((noarch_old_dos_attr
== 0 && noarch_new_dos_attr
!= 0) ||
1116 (noarch_old_dos_attr
!= 0 && ((noarch_old_dos_attr
& noarch_new_dos_attr
) == noarch_old_dos_attr
))) {
1117 *returned_unx_mode
= new_unx_mode
;
1119 *returned_unx_mode
= (mode_t
)0;
1122 DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, "
1123 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1124 "returned_unx_mode = 0%o\n",
1125 (unsigned int)old_dos_attr
,
1126 (unsigned int)existing_unx_mode
,
1127 (unsigned int)new_dos_attr
,
1128 (unsigned int)*returned_unx_mode
));
1130 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1131 if (lp_map_system(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1132 if ((old_dos_attr
& FILE_ATTRIBUTE_SYSTEM
) &&
1133 !(new_dos_attr
& FILE_ATTRIBUTE_SYSTEM
)) {
1137 if (lp_map_hidden(SNUM(conn
)) || lp_store_dos_attributes(SNUM(conn
))) {
1138 if ((old_dos_attr
& FILE_ATTRIBUTE_HIDDEN
) &&
1139 !(new_dos_attr
& FILE_ATTRIBUTE_HIDDEN
)) {
1146 /****************************************************************************
1147 Special FCB or DOS processing in the case of a sharing violation.
1148 Try and find a duplicated file handle.
1149 ****************************************************************************/
1151 NTSTATUS
fcb_or_dos_open(struct smb_request
*req
,
1152 connection_struct
*conn
,
1153 files_struct
*fsp_to_dup_into
,
1154 const struct smb_filename
*smb_fname
,
1159 uint32 share_access
,
1160 uint32 create_options
)
1164 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1165 "file %s.\n", smb_fname_str_dbg(smb_fname
)));
1167 for(fsp
= file_find_di_first(id
); fsp
;
1168 fsp
= file_find_di_next(fsp
)) {
1170 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1171 "vuid = %u, file_pid = %u, private_options = 0x%x "
1172 "access_mask = 0x%x\n", fsp_str_dbg(fsp
),
1173 fsp
->fh
->fd
, (unsigned int)fsp
->vuid
,
1174 (unsigned int)fsp
->file_pid
,
1175 (unsigned int)fsp
->fh
->private_options
,
1176 (unsigned int)fsp
->access_mask
));
1178 if (fsp
->fh
->fd
!= -1 &&
1179 fsp
->vuid
== vuid
&&
1180 fsp
->file_pid
== file_pid
&&
1181 (fsp
->fh
->private_options
& (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1182 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) &&
1183 (fsp
->access_mask
& FILE_WRITE_DATA
) &&
1184 strequal(fsp
->fsp_name
->base_name
, smb_fname
->base_name
) &&
1185 strequal(fsp
->fsp_name
->stream_name
,
1186 smb_fname
->stream_name
)) {
1187 DEBUG(10,("fcb_or_dos_open: file match\n"));
1193 return NT_STATUS_NOT_FOUND
;
1196 /* quite an insane set of semantics ... */
1197 if (is_executable(smb_fname
->base_name
) &&
1198 (fsp
->fh
->private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
)) {
1199 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1200 return NT_STATUS_INVALID_PARAMETER
;
1203 /* We need to duplicate this fsp. */
1204 return dup_file_fsp(req
, fsp
, access_mask
, share_access
,
1205 create_options
, fsp_to_dup_into
);
1208 /****************************************************************************
1209 Open a file with a share mode - old openX method - map into NTCreate.
1210 ****************************************************************************/
1212 bool map_open_params_to_ntcreate(const struct smb_filename
*smb_fname
,
1213 int deny_mode
, int open_func
,
1214 uint32
*paccess_mask
,
1215 uint32
*pshare_mode
,
1216 uint32
*pcreate_disposition
,
1217 uint32
*pcreate_options
)
1221 uint32 create_disposition
;
1222 uint32 create_options
= FILE_NON_DIRECTORY_FILE
;
1224 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1225 "open_func = 0x%x\n",
1226 smb_fname_str_dbg(smb_fname
), (unsigned int)deny_mode
,
1227 (unsigned int)open_func
));
1229 /* Create the NT compatible access_mask. */
1230 switch (GET_OPENX_MODE(deny_mode
)) {
1231 case DOS_OPEN_EXEC
: /* Implies read-only - used to be FILE_READ_DATA */
1232 case DOS_OPEN_RDONLY
:
1233 access_mask
= FILE_GENERIC_READ
;
1235 case DOS_OPEN_WRONLY
:
1236 access_mask
= FILE_GENERIC_WRITE
;
1240 access_mask
= FILE_GENERIC_READ
|FILE_GENERIC_WRITE
;
1243 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1244 (unsigned int)GET_OPENX_MODE(deny_mode
)));
1248 /* Create the NT compatible create_disposition. */
1249 switch (open_func
) {
1250 case OPENX_FILE_EXISTS_FAIL
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
1251 create_disposition
= FILE_CREATE
;
1254 case OPENX_FILE_EXISTS_OPEN
:
1255 create_disposition
= FILE_OPEN
;
1258 case OPENX_FILE_EXISTS_OPEN
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
1259 create_disposition
= FILE_OPEN_IF
;
1262 case OPENX_FILE_EXISTS_TRUNCATE
:
1263 create_disposition
= FILE_OVERWRITE
;
1266 case OPENX_FILE_EXISTS_TRUNCATE
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
1267 create_disposition
= FILE_OVERWRITE_IF
;
1271 /* From samba4 - to be confirmed. */
1272 if (GET_OPENX_MODE(deny_mode
) == DOS_OPEN_EXEC
) {
1273 create_disposition
= FILE_CREATE
;
1276 DEBUG(10,("map_open_params_to_ntcreate: bad "
1277 "open_func 0x%x\n", (unsigned int)open_func
));
1281 /* Create the NT compatible share modes. */
1282 switch (GET_DENY_MODE(deny_mode
)) {
1284 share_mode
= FILE_SHARE_NONE
;
1288 share_mode
= FILE_SHARE_READ
;
1292 share_mode
= FILE_SHARE_WRITE
;
1296 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
1300 create_options
|= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
;
1301 if (is_executable(smb_fname
->base_name
)) {
1302 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
1304 if (GET_OPENX_MODE(deny_mode
) == DOS_OPEN_RDONLY
) {
1305 share_mode
= FILE_SHARE_READ
;
1307 share_mode
= FILE_SHARE_NONE
;
1313 create_options
|= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
;
1314 share_mode
= FILE_SHARE_NONE
;
1318 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1319 (unsigned int)GET_DENY_MODE(deny_mode
) ));
1323 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1324 "share_mode = 0x%x, create_disposition = 0x%x, "
1325 "create_options = 0x%x\n",
1326 smb_fname_str_dbg(smb_fname
),
1327 (unsigned int)access_mask
,
1328 (unsigned int)share_mode
,
1329 (unsigned int)create_disposition
,
1330 (unsigned int)create_options
));
1333 *paccess_mask
= access_mask
;
1336 *pshare_mode
= share_mode
;
1338 if (pcreate_disposition
) {
1339 *pcreate_disposition
= create_disposition
;
1341 if (pcreate_options
) {
1342 *pcreate_options
= create_options
;
1349 static void schedule_defer_open(struct share_mode_lock
*lck
,
1350 struct timeval request_time
,
1351 struct smb_request
*req
)
1353 struct deferred_open_record state
;
1355 /* This is a relative time, added to the absolute
1356 request_time value to get the absolute timeout time.
1357 Note that if this is the second or greater time we enter
1358 this codepath for this particular request mid then
1359 request_time is left as the absolute time of the *first*
1360 time this request mid was processed. This is what allows
1361 the request to eventually time out. */
1363 struct timeval timeout
;
1365 /* Normally the smbd we asked should respond within
1366 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1367 * the client did, give twice the timeout as a safety
1368 * measure here in case the other smbd is stuck
1369 * somewhere else. */
1371 timeout
= timeval_set(OPLOCK_BREAK_TIMEOUT
*2, 0);
1373 /* Nothing actually uses state.delayed_for_oplocks
1374 but it's handy to differentiate in debug messages
1375 between a 30 second delay due to oplock break, and
1376 a 1 second delay for share mode conflicts. */
1378 state
.delayed_for_oplocks
= True
;
1381 if (!request_timed_out(request_time
, timeout
)) {
1382 defer_open(lck
, request_time
, timeout
, req
, &state
);
1386 /****************************************************************************
1387 Work out what access_mask to use from what the client sent us.
1388 ****************************************************************************/
1390 static NTSTATUS
calculate_access_mask(connection_struct
*conn
,
1391 const struct smb_filename
*smb_fname
,
1393 uint32_t access_mask
,
1394 uint32_t *access_mask_out
)
1399 * Convert GENERIC bits to specific bits.
1402 se_map_generic(&access_mask
, &file_generic_mapping
);
1404 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1405 if (access_mask
& MAXIMUM_ALLOWED_ACCESS
) {
1408 struct security_descriptor
*sd
;
1409 uint32_t access_granted
= 0;
1411 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
1412 (OWNER_SECURITY_INFORMATION
|
1413 GROUP_SECURITY_INFORMATION
|
1414 DACL_SECURITY_INFORMATION
),&sd
);
1416 if (!NT_STATUS_IS_OK(status
)) {
1417 DEBUG(10, ("calculate_access_mask: Could not get acl "
1419 smb_fname_str_dbg(smb_fname
),
1420 nt_errstr(status
)));
1421 return NT_STATUS_ACCESS_DENIED
;
1424 status
= smb1_file_se_access_check(conn
,
1426 conn
->server_info
->ptok
,
1432 if (!NT_STATUS_IS_OK(status
)) {
1433 DEBUG(10, ("calculate_access_mask: Access denied on "
1434 "file %s: when calculating maximum access\n",
1435 smb_fname_str_dbg(smb_fname
)));
1436 return NT_STATUS_ACCESS_DENIED
;
1439 access_mask
= access_granted
;
1441 access_mask
= FILE_GENERIC_ALL
;
1445 *access_mask_out
= access_mask
;
1446 return NT_STATUS_OK
;
1449 /****************************************************************************
1450 Open a file with a share mode. Passed in an already created files_struct *.
1451 ****************************************************************************/
1453 static NTSTATUS
open_file_ntcreate(connection_struct
*conn
,
1454 struct smb_request
*req
,
1455 uint32 access_mask
, /* access bits (FILE_READ_DATA etc.) */
1456 uint32 share_access
, /* share constants (FILE_SHARE_READ etc) */
1457 uint32 create_disposition
, /* FILE_OPEN_IF etc. */
1458 uint32 create_options
, /* options such as delete on close. */
1459 uint32 new_dos_attributes
, /* attributes used for new file. */
1460 int oplock_request
, /* internal Samba oplock codes. */
1461 /* Information (FILE_EXISTS etc.) */
1465 struct smb_filename
*smb_fname
= fsp
->fsp_name
;
1468 bool file_existed
= VALID_STAT(smb_fname
->st
);
1469 bool def_acl
= False
;
1470 bool posix_open
= False
;
1471 bool new_file_created
= False
;
1472 bool clear_ads
= false;
1474 NTSTATUS fsp_open
= NT_STATUS_ACCESS_DENIED
;
1475 mode_t new_unx_mode
= (mode_t
)0;
1476 mode_t unx_mode
= (mode_t
)0;
1478 uint32 existing_dos_attributes
= 0;
1479 struct pending_message_list
*pml
= NULL
;
1480 struct timeval request_time
= timeval_zero();
1481 struct share_mode_lock
*lck
= NULL
;
1482 uint32 open_access_mask
= access_mask
;
1488 /* Windows allows a new file to be created and
1489 silently removes a FILE_ATTRIBUTE_DIRECTORY
1490 sent by the client. Do the same. */
1492 new_dos_attributes
&= ~FILE_ATTRIBUTE_DIRECTORY
;
1494 if (conn
->printer
) {
1496 * Printers are handled completely differently.
1497 * Most of the passed parameters are ignored.
1501 *pinfo
= FILE_WAS_CREATED
;
1504 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n",
1505 smb_fname_str_dbg(smb_fname
)));
1508 DEBUG(0,("open_file_ntcreate: printer open without "
1509 "an SMB request!\n"));
1510 return NT_STATUS_INTERNAL_ERROR
;
1513 return print_fsp_open(req
, conn
, smb_fname
->base_name
,
1517 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
, &parent_dir
,
1519 return NT_STATUS_NO_MEMORY
;
1522 if (new_dos_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
1524 unx_mode
= (mode_t
)(new_dos_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
1525 new_dos_attributes
= 0;
1527 /* We add aARCH to this as this mode is only used if the file is
1529 unx_mode
= unix_mode(conn
, new_dos_attributes
| aARCH
,
1530 smb_fname
, parent_dir
);
1533 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1534 "access_mask=0x%x share_access=0x%x "
1535 "create_disposition = 0x%x create_options=0x%x "
1536 "unix mode=0%o oplock_request=%d\n",
1537 smb_fname_str_dbg(smb_fname
), new_dos_attributes
,
1538 access_mask
, share_access
, create_disposition
,
1539 create_options
, (unsigned int)unx_mode
, oplock_request
));
1541 if ((req
== NULL
) && ((oplock_request
& INTERNAL_OPEN_ONLY
) == 0)) {
1542 DEBUG(0, ("No smb request but not an internal only open!\n"));
1543 return NT_STATUS_INTERNAL_ERROR
;
1547 * Only non-internal opens can be deferred at all
1551 && ((pml
= get_open_deferred_message(req
->mid
)) != NULL
)) {
1552 struct deferred_open_record
*state
=
1553 (struct deferred_open_record
*)pml
->private_data
.data
;
1555 /* Remember the absolute time of the original
1556 request with this mid. We'll use it later to
1557 see if this has timed out. */
1559 request_time
= pml
->request_time
;
1561 /* Remove the deferred open entry under lock. */
1562 lck
= get_share_mode_lock(talloc_tos(), state
->id
, NULL
, NULL
,
1565 DEBUG(0, ("could not get share mode lock\n"));
1567 del_deferred_open_entry(lck
, req
->mid
);
1571 /* Ensure we don't reprocess this message. */
1572 remove_deferred_open_smb_message(req
->mid
);
1575 status
= check_name(conn
, smb_fname
->base_name
);
1576 if (!NT_STATUS_IS_OK(status
)) {
1581 new_dos_attributes
&= SAMBA_ATTRIBUTES_MASK
;
1583 existing_dos_attributes
= dos_mode(conn
, smb_fname
);
1587 /* ignore any oplock requests if oplocks are disabled */
1588 if (!lp_oplocks(SNUM(conn
)) || global_client_failed_oplock_break
||
1589 IS_VETO_OPLOCK_PATH(conn
, smb_fname
->base_name
)) {
1590 /* Mask off everything except the private Samba bits. */
1591 oplock_request
&= SAMBA_PRIVATE_OPLOCK_MASK
;
1594 /* this is for OS/2 long file names - say we don't support them */
1595 if (!lp_posix_pathnames() && strstr(smb_fname
->base_name
,".+,;=[].")) {
1596 /* OS/2 Workplace shell fix may be main code stream in a later
1598 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1600 if (use_nt_status()) {
1601 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1603 return NT_STATUS_DOS(ERRDOS
, ERRcannotopen
);
1606 switch( create_disposition
) {
1608 * Currently we're using FILE_SUPERSEDE as the same as
1609 * FILE_OVERWRITE_IF but they really are
1610 * different. FILE_SUPERSEDE deletes an existing file
1611 * (requiring delete access) then recreates it.
1613 case FILE_SUPERSEDE
:
1614 /* If file exists replace/overwrite. If file doesn't
1616 flags2
|= (O_CREAT
| O_TRUNC
);
1620 case FILE_OVERWRITE_IF
:
1621 /* If file exists replace/overwrite. If file doesn't
1623 flags2
|= (O_CREAT
| O_TRUNC
);
1628 /* If file exists open. If file doesn't exist error. */
1629 if (!file_existed
) {
1630 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1631 "requested for file %s and file "
1633 smb_fname_str_dbg(smb_fname
)));
1635 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1639 case FILE_OVERWRITE
:
1640 /* If file exists overwrite. If file doesn't exist
1642 if (!file_existed
) {
1643 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1644 "requested for file %s and file "
1646 smb_fname_str_dbg(smb_fname
) ));
1648 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1655 /* If file exists error. If file doesn't exist
1658 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1659 "requested for file %s and file "
1660 "already exists.\n",
1661 smb_fname_str_dbg(smb_fname
)));
1662 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
1667 return map_nt_error_from_unix(errno
);
1669 flags2
|= (O_CREAT
|O_EXCL
);
1673 /* If file exists open. If file doesn't exist
1679 return NT_STATUS_INVALID_PARAMETER
;
1682 /* We only care about matching attributes on file exists and
1685 if (!posix_open
&& file_existed
&& ((create_disposition
== FILE_OVERWRITE
) ||
1686 (create_disposition
== FILE_OVERWRITE_IF
))) {
1687 if (!open_match_attributes(conn
, existing_dos_attributes
,
1689 smb_fname
->st
.st_ex_mode
,
1690 unx_mode
, &new_unx_mode
)) {
1691 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1692 "for file %s (%x %x) (0%o, 0%o)\n",
1693 smb_fname_str_dbg(smb_fname
),
1694 existing_dos_attributes
,
1696 (unsigned int)smb_fname
->st
.st_ex_mode
,
1697 (unsigned int)unx_mode
));
1699 return NT_STATUS_ACCESS_DENIED
;
1703 status
= calculate_access_mask(conn
, smb_fname
, file_existed
,
1706 if (!NT_STATUS_IS_OK(status
)) {
1707 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1708 "on file %s returned %s\n",
1709 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
1713 open_access_mask
= access_mask
;
1715 if ((flags2
& O_TRUNC
) || (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1716 open_access_mask
|= FILE_WRITE_DATA
; /* This will cause oplock breaks. */
1719 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1720 "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname
),
1724 * Note that we ignore the append flag as append does not
1725 * mean the same thing under DOS and Unix.
1728 if ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) ||
1729 (oplock_request
& FORCE_OPLOCK_BREAK_TO_NONE
)) {
1730 /* DENY_DOS opens are always underlying read-write on the
1731 file handle, no matter what the requested access mask
1733 if ((create_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) ||
1734 access_mask
& (FILE_READ_ATTRIBUTES
|FILE_READ_DATA
|FILE_READ_EA
|FILE_EXECUTE
)) {
1744 * Currently we only look at FILE_WRITE_THROUGH for create options.
1748 if ((create_options
& FILE_WRITE_THROUGH
) && lp_strict_sync(SNUM(conn
))) {
1753 if (posix_open
&& (access_mask
& FILE_APPEND_DATA
)) {
1757 if (!posix_open
&& !CAN_WRITE(conn
)) {
1759 * We should really return a permission denied error if either
1760 * O_CREAT or O_TRUNC are set, but for compatibility with
1761 * older versions of Samba we just AND them out.
1763 flags2
&= ~(O_CREAT
|O_TRUNC
);
1767 * Ensure we can't write on a read-only share or file.
1770 if (flags
!= O_RDONLY
&& file_existed
&&
1771 (!CAN_WRITE(conn
) || IS_DOS_READONLY(existing_dos_attributes
))) {
1772 DEBUG(5,("open_file_ntcreate: write access requested for "
1773 "file %s on read only %s\n",
1774 smb_fname_str_dbg(smb_fname
),
1775 !CAN_WRITE(conn
) ? "share" : "file" ));
1777 return NT_STATUS_ACCESS_DENIED
;
1780 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1781 fsp
->share_access
= share_access
;
1782 fsp
->fh
->private_options
= create_options
;
1783 fsp
->access_mask
= open_access_mask
; /* We change this to the
1784 * requested access_mask after
1785 * the open is done. */
1786 fsp
->posix_open
= posix_open
;
1788 /* Ensure no SAMBA_PRIVATE bits can be set. */
1789 fsp
->oplock_type
= (oplock_request
& ~SAMBA_PRIVATE_OPLOCK_MASK
);
1791 if (timeval_is_zero(&request_time
)) {
1792 request_time
= fsp
->open_time
;
1796 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
1797 id
= vfs_file_id_from_sbuf(conn
, &smb_fname
->st
);
1799 lck
= get_share_mode_lock(talloc_tos(), id
,
1801 smb_fname
, &old_write_time
);
1804 DEBUG(0, ("Could not get share mode lock\n"));
1805 return NT_STATUS_SHARING_VIOLATION
;
1808 /* First pass - send break only on batch oplocks. */
1810 && delay_for_oplocks(lck
, fsp
, req
->mid
, 1,
1812 schedule_defer_open(lck
, request_time
, req
);
1814 return NT_STATUS_SHARING_VIOLATION
;
1817 /* Use the client requested access mask here, not the one we
1819 status
= open_mode_check(conn
, lck
, access_mask
, share_access
,
1820 create_options
, &file_existed
);
1822 if (NT_STATUS_IS_OK(status
)) {
1823 /* We might be going to allow this open. Check oplock
1825 /* Second pass - send break for both batch or
1826 * exclusive oplocks. */
1828 && delay_for_oplocks(lck
, fsp
, req
->mid
, 2,
1830 schedule_defer_open(lck
, request_time
, req
);
1832 return NT_STATUS_SHARING_VIOLATION
;
1836 if (NT_STATUS_EQUAL(status
, NT_STATUS_DELETE_PENDING
)) {
1837 /* DELETE_PENDING is not deferred for a second */
1842 if (!NT_STATUS_IS_OK(status
)) {
1843 uint32 can_access_mask
;
1844 bool can_access
= True
;
1846 SMB_ASSERT(NT_STATUS_EQUAL(status
, NT_STATUS_SHARING_VIOLATION
));
1848 /* Check if this can be done with the deny_dos and fcb
1850 if (create_options
&
1851 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
|
1852 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
)) {
1854 DEBUG(0, ("DOS open without an SMB "
1857 return NT_STATUS_INTERNAL_ERROR
;
1860 /* Use the client requested access mask here,
1861 * not the one we open with. */
1862 status
= fcb_or_dos_open(req
,
1873 if (NT_STATUS_IS_OK(status
)) {
1876 *pinfo
= FILE_WAS_OPENED
;
1878 return NT_STATUS_OK
;
1883 * This next line is a subtlety we need for
1884 * MS-Access. If a file open will fail due to share
1885 * permissions and also for security (access) reasons,
1886 * we need to return the access failed error, not the
1887 * share error. We can't open the file due to kernel
1888 * oplock deadlock (it's possible we failed above on
1889 * the open_mode_check()) so use a userspace check.
1892 if (flags
& O_RDWR
) {
1893 can_access_mask
= FILE_READ_DATA
|FILE_WRITE_DATA
;
1894 } else if (flags
& O_WRONLY
) {
1895 can_access_mask
= FILE_WRITE_DATA
;
1897 can_access_mask
= FILE_READ_DATA
;
1900 if (((can_access_mask
& FILE_WRITE_DATA
) &&
1901 !CAN_WRITE(conn
)) ||
1902 !can_access_file_data(conn
, smb_fname
,
1908 * If we're returning a share violation, ensure we
1909 * cope with the braindead 1 second delay.
1912 if (!(oplock_request
& INTERNAL_OPEN_ONLY
) &&
1913 lp_defer_sharing_violations()) {
1914 struct timeval timeout
;
1915 struct deferred_open_record state
;
1918 /* this is a hack to speed up torture tests
1920 timeout_usecs
= lp_parm_int(SNUM(conn
),
1921 "smbd","sharedelay",
1922 SHARING_VIOLATION_USEC_WAIT
);
1924 /* This is a relative time, added to the absolute
1925 request_time value to get the absolute timeout time.
1926 Note that if this is the second or greater time we enter
1927 this codepath for this particular request mid then
1928 request_time is left as the absolute time of the *first*
1929 time this request mid was processed. This is what allows
1930 the request to eventually time out. */
1932 timeout
= timeval_set(0, timeout_usecs
);
1934 /* Nothing actually uses state.delayed_for_oplocks
1935 but it's handy to differentiate in debug messages
1936 between a 30 second delay due to oplock break, and
1937 a 1 second delay for share mode conflicts. */
1939 state
.delayed_for_oplocks
= False
;
1943 && !request_timed_out(request_time
,
1945 defer_open(lck
, request_time
, timeout
,
1953 * We have detected a sharing violation here
1954 * so return the correct error code
1956 status
= NT_STATUS_SHARING_VIOLATION
;
1958 status
= NT_STATUS_ACCESS_DENIED
;
1964 * We exit this block with the share entry *locked*.....
1968 SMB_ASSERT(!file_existed
|| (lck
!= NULL
));
1971 * Ensure we pay attention to default ACLs on directories if required.
1974 if ((flags2
& O_CREAT
) && lp_inherit_acls(SNUM(conn
)) &&
1975 (def_acl
= directory_has_default_acl(conn
, parent_dir
))) {
1976 unx_mode
= (0777 & lp_create_mask(SNUM(conn
)));
1979 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1980 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1981 (unsigned int)flags
, (unsigned int)flags2
,
1982 (unsigned int)unx_mode
, (unsigned int)access_mask
,
1983 (unsigned int)open_access_mask
));
1986 * open_file strips any O_TRUNC flags itself.
1989 fsp_open
= open_file(fsp
, conn
, req
, parent_dir
,
1990 flags
|flags2
, unx_mode
, access_mask
,
1993 if (!NT_STATUS_IS_OK(fsp_open
)) {
2000 if (!file_existed
) {
2001 struct timespec old_write_time
= smb_fname
->st
.st_ex_mtime
;
2003 * Deal with the race condition where two smbd's detect the
2004 * file doesn't exist and do the create at the same time. One
2005 * of them will win and set a share mode, the other (ie. this
2006 * one) should check if the requested share mode for this
2007 * create is allowed.
2011 * Now the file exists and fsp is successfully opened,
2012 * fsp->dev and fsp->inode are valid and should replace the
2013 * dev=0,inode=0 from a non existent file. Spotted by
2014 * Nadav Danieli <nadavd@exanet.com>. JRA.
2019 lck
= get_share_mode_lock(talloc_tos(), id
,
2021 smb_fname
, &old_write_time
);
2024 DEBUG(0, ("open_file_ntcreate: Could not get share "
2025 "mode lock for %s\n",
2026 smb_fname_str_dbg(smb_fname
)));
2028 return NT_STATUS_SHARING_VIOLATION
;
2031 /* First pass - send break only on batch oplocks. */
2033 && delay_for_oplocks(lck
, fsp
, req
->mid
, 1,
2035 schedule_defer_open(lck
, request_time
, req
);
2038 return NT_STATUS_SHARING_VIOLATION
;
2041 status
= open_mode_check(conn
, lck
, access_mask
, share_access
,
2042 create_options
, &file_existed
);
2044 if (NT_STATUS_IS_OK(status
)) {
2045 /* We might be going to allow this open. Check oplock
2047 /* Second pass - send break for both batch or
2048 * exclusive oplocks. */
2050 && delay_for_oplocks(lck
, fsp
, req
->mid
, 2,
2052 schedule_defer_open(lck
, request_time
, req
);
2055 return NT_STATUS_SHARING_VIOLATION
;
2059 if (!NT_STATUS_IS_OK(status
)) {
2060 struct deferred_open_record state
;
2064 state
.delayed_for_oplocks
= False
;
2067 /* Do it all over again immediately. In the second
2068 * round we will find that the file existed and handle
2069 * the DELETE_PENDING and FCB cases correctly. No need
2070 * to duplicate the code here. Essentially this is a
2071 * "goto top of this function", but don't tell
2075 defer_open(lck
, request_time
, timeval_zero(),
2083 * We exit this block with the share entry *locked*.....
2088 SMB_ASSERT(lck
!= NULL
);
2090 /* Delete streams if create_disposition requires it */
2091 if (file_existed
&& clear_ads
&&
2092 !is_ntfs_stream_smb_fname(smb_fname
)) {
2093 status
= delete_all_streams(conn
, smb_fname
->base_name
);
2094 if (!NT_STATUS_IS_OK(status
)) {
2101 /* note that we ignore failure for the following. It is
2102 basically a hack for NFS, and NFS will never set one of
2103 these only read them. Nobody but Samba can ever set a deny
2104 mode and we have already checked our more authoritative
2105 locking database for permission to set this deny mode. If
2106 the kernel refuses the operations then the kernel is wrong.
2107 note that GPFS supports it as well - jmcd */
2109 if (fsp
->fh
->fd
!= -1) {
2111 ret_flock
= SMB_VFS_KERNEL_FLOCK(fsp
, share_access
, access_mask
);
2112 if(ret_flock
== -1 ){
2117 return NT_STATUS_SHARING_VIOLATION
;
2122 * At this point onwards, we can guarentee that the share entry
2123 * is locked, whether we created the file or not, and that the
2124 * deny mode is compatible with all current opens.
2128 * If requested, truncate the file.
2131 if (flags2
&O_TRUNC
) {
2133 * We are modifing the file after open - update the stat
2136 if ((SMB_VFS_FTRUNCATE(fsp
, 0) == -1) ||
2137 (SMB_VFS_FSTAT(fsp
, &smb_fname
->st
)==-1)) {
2138 status
= map_nt_error_from_unix(errno
);
2145 /* Record the options we were opened with. */
2146 fsp
->share_access
= share_access
;
2147 fsp
->fh
->private_options
= create_options
;
2149 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2151 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2154 /* stat opens on existing files don't get oplocks. */
2155 if (is_stat_open(open_access_mask
)) {
2156 fsp
->oplock_type
= NO_OPLOCK
;
2159 if (!(flags2
& O_TRUNC
)) {
2160 info
= FILE_WAS_OPENED
;
2162 info
= FILE_WAS_OVERWRITTEN
;
2165 info
= FILE_WAS_CREATED
;
2173 * Setup the oplock info in both the shared memory and
2177 if (!set_file_oplock(fsp
, fsp
->oplock_type
)) {
2178 /* Could not get the kernel oplock */
2179 fsp
->oplock_type
= NO_OPLOCK
;
2182 if (info
== FILE_WAS_OVERWRITTEN
|| info
== FILE_WAS_CREATED
|| info
== FILE_WAS_SUPERSEDED
) {
2183 new_file_created
= True
;
2186 set_share_mode(lck
, fsp
, conn
->server_info
->utok
.uid
, 0,
2189 /* Handle strange delete on close create semantics. */
2190 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2192 status
= can_set_delete_on_close(fsp
, new_dos_attributes
);
2194 if (!NT_STATUS_IS_OK(status
)) {
2195 /* Remember to delete the mode we just added. */
2196 del_share_mode(lck
, fsp
);
2201 /* Note that here we set the *inital* delete on close flag,
2202 not the regular one. The magic gets handled in close. */
2203 fsp
->initial_delete_on_close
= True
;
2206 if (new_file_created
) {
2207 /* Files should be initially set as archive */
2208 if (lp_map_archive(SNUM(conn
)) ||
2209 lp_store_dos_attributes(SNUM(conn
))) {
2211 if (file_set_dosmode(conn
, smb_fname
,
2212 new_dos_attributes
| aARCH
,
2213 parent_dir
, true) == 0) {
2214 unx_mode
= smb_fname
->st
.st_ex_mode
;
2221 * Take care of inherited ACLs on created files - if default ACL not
2225 if (!posix_open
&& !file_existed
&& !def_acl
) {
2227 int saved_errno
= errno
; /* We might get ENOSYS in the next
2230 if (SMB_VFS_FCHMOD_ACL(fsp
, unx_mode
) == -1 &&
2232 errno
= saved_errno
; /* Ignore ENOSYS */
2235 } else if (new_unx_mode
) {
2239 /* Attributes need changing. File already existed. */
2242 int saved_errno
= errno
; /* We might get ENOSYS in the
2244 ret
= SMB_VFS_FCHMOD_ACL(fsp
, new_unx_mode
);
2246 if (ret
== -1 && errno
== ENOSYS
) {
2247 errno
= saved_errno
; /* Ignore ENOSYS */
2249 DEBUG(5, ("open_file_ntcreate: reset "
2250 "attributes of file %s to 0%o\n",
2251 smb_fname_str_dbg(smb_fname
),
2252 (unsigned int)new_unx_mode
));
2253 ret
= 0; /* Don't do the fchmod below. */
2258 (SMB_VFS_FCHMOD(fsp
, new_unx_mode
) == -1))
2259 DEBUG(5, ("open_file_ntcreate: failed to reset "
2260 "attributes of file %s to 0%o\n",
2261 smb_fname_str_dbg(smb_fname
),
2262 (unsigned int)new_unx_mode
));
2265 /* If this is a successful open, we must remove any deferred open
2268 del_deferred_open_entry(lck
, req
->mid
);
2272 return NT_STATUS_OK
;
2276 /****************************************************************************
2277 Open a file for for write to ensure that we can fchmod it.
2278 ****************************************************************************/
2280 NTSTATUS
open_file_fchmod(connection_struct
*conn
,
2281 struct smb_filename
*smb_fname
,
2282 files_struct
**result
)
2284 if (!VALID_STAT(smb_fname
->st
)) {
2285 return NT_STATUS_INVALID_PARAMETER
;
2288 return SMB_VFS_CREATE_FILE(
2291 0, /* root_dir_fid */
2292 smb_fname
, /* fname */
2293 FILE_WRITE_DATA
, /* access_mask */
2294 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
2296 FILE_OPEN
, /* create_disposition*/
2297 0, /* create_options */
2298 0, /* file_attributes */
2299 INTERNAL_OPEN_ONLY
, /* oplock_request */
2300 0, /* allocation_size */
2303 result
, /* result */
2307 static NTSTATUS
mkdir_internal(connection_struct
*conn
,
2308 struct smb_filename
*smb_dname
,
2309 uint32 file_attributes
)
2314 bool posix_open
= false;
2316 if(!CAN_WRITE(conn
)) {
2317 DEBUG(5,("mkdir_internal: failing create on read-only share "
2318 "%s\n", lp_servicename(SNUM(conn
))));
2319 return NT_STATUS_ACCESS_DENIED
;
2322 status
= check_name(conn
, smb_dname
->base_name
);
2323 if (!NT_STATUS_IS_OK(status
)) {
2327 if (!parent_dirname(talloc_tos(), smb_dname
->base_name
, &parent_dir
,
2329 return NT_STATUS_NO_MEMORY
;
2332 if (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) {
2334 mode
= (mode_t
)(file_attributes
& ~FILE_FLAG_POSIX_SEMANTICS
);
2336 mode
= unix_mode(conn
, aDIR
, smb_dname
, parent_dir
);
2339 if (SMB_VFS_MKDIR(conn
, smb_dname
->base_name
, mode
) != 0) {
2340 return map_nt_error_from_unix(errno
);
2343 /* Ensure we're checking for a symlink here.... */
2344 /* We don't want to get caught by a symlink racer. */
2346 if (SMB_VFS_LSTAT(conn
, smb_dname
) == -1) {
2347 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2348 smb_fname_str_dbg(smb_dname
), strerror(errno
)));
2349 return map_nt_error_from_unix(errno
);
2352 if (!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2353 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2354 smb_fname_str_dbg(smb_dname
)));
2355 return NT_STATUS_ACCESS_DENIED
;
2358 if (lp_store_dos_attributes(SNUM(conn
))) {
2360 file_set_dosmode(conn
, smb_dname
,
2361 file_attributes
| aDIR
,
2366 if (lp_inherit_perms(SNUM(conn
))) {
2367 inherit_access_posix_acl(conn
, parent_dir
,
2368 smb_dname
->base_name
, mode
);
2373 * Check if high bits should have been set,
2374 * then (if bits are missing): add them.
2375 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2378 if ((mode
& ~(S_IRWXU
|S_IRWXG
|S_IRWXO
)) &&
2379 (mode
& ~smb_dname
->st
.st_ex_mode
)) {
2380 SMB_VFS_CHMOD(conn
, smb_dname
->base_name
,
2381 (smb_dname
->st
.st_ex_mode
|
2382 (mode
& ~smb_dname
->st
.st_ex_mode
)));
2386 /* Change the owner if required. */
2387 if (lp_inherit_owner(SNUM(conn
))) {
2388 change_dir_owner_to_parent(conn
, parent_dir
,
2389 smb_dname
->base_name
,
2393 notify_fname(conn
, NOTIFY_ACTION_ADDED
, FILE_NOTIFY_CHANGE_DIR_NAME
,
2394 smb_dname
->base_name
);
2396 return NT_STATUS_OK
;
2399 /****************************************************************************
2400 Open a directory from an NT SMB call.
2401 ****************************************************************************/
2403 static NTSTATUS
open_directory(connection_struct
*conn
,
2404 struct smb_request
*req
,
2405 struct smb_filename
*smb_dname
,
2407 uint32 share_access
,
2408 uint32 create_disposition
,
2409 uint32 create_options
,
2410 uint32 file_attributes
,
2412 files_struct
**result
)
2414 files_struct
*fsp
= NULL
;
2415 bool dir_existed
= VALID_STAT(smb_dname
->st
) ? True
: False
;
2416 struct share_mode_lock
*lck
= NULL
;
2418 struct timespec mtimespec
;
2421 SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname
));
2423 /* Ensure we have a directory attribute. */
2424 file_attributes
|= FILE_ATTRIBUTE_DIRECTORY
;
2426 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2427 "share_access = 0x%x create_options = 0x%x, "
2428 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2429 smb_fname_str_dbg(smb_dname
),
2430 (unsigned int)access_mask
,
2431 (unsigned int)share_access
,
2432 (unsigned int)create_options
,
2433 (unsigned int)create_disposition
,
2434 (unsigned int)file_attributes
));
2436 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
2437 (conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
2438 is_ntfs_stream_smb_fname(smb_dname
)) {
2439 DEBUG(2, ("open_directory: %s is a stream name!\n",
2440 smb_fname_str_dbg(smb_dname
)));
2441 return NT_STATUS_NOT_A_DIRECTORY
;
2444 status
= calculate_access_mask(conn
, smb_dname
, dir_existed
,
2445 access_mask
, &access_mask
);
2446 if (!NT_STATUS_IS_OK(status
)) {
2447 DEBUG(10, ("open_directory: calculate_access_mask "
2448 "on file %s returned %s\n",
2449 smb_fname_str_dbg(smb_dname
),
2450 nt_errstr(status
)));
2454 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2455 !user_has_privileges(current_user
.nt_user_token
, &se_security
)) {
2456 DEBUG(10, ("open_directory: open on %s "
2457 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2458 smb_fname_str_dbg(smb_dname
)));
2459 return NT_STATUS_PRIVILEGE_NOT_HELD
;
2462 switch( create_disposition
) {
2465 info
= FILE_WAS_OPENED
;
2468 * We want to follow symlinks here.
2471 if (SMB_VFS_STAT(conn
, smb_dname
) != 0) {
2472 return map_nt_error_from_unix(errno
);
2479 /* If directory exists error. If directory doesn't
2482 status
= mkdir_internal(conn
, smb_dname
,
2485 if (!NT_STATUS_IS_OK(status
)) {
2486 DEBUG(2, ("open_directory: unable to create "
2487 "%s. Error was %s\n",
2488 smb_fname_str_dbg(smb_dname
),
2489 nt_errstr(status
)));
2493 info
= FILE_WAS_CREATED
;
2498 * If directory exists open. If directory doesn't
2502 status
= mkdir_internal(conn
, smb_dname
,
2505 if (NT_STATUS_IS_OK(status
)) {
2506 info
= FILE_WAS_CREATED
;
2509 if (NT_STATUS_EQUAL(status
,
2510 NT_STATUS_OBJECT_NAME_COLLISION
)) {
2511 info
= FILE_WAS_OPENED
;
2512 status
= NT_STATUS_OK
;
2517 case FILE_SUPERSEDE
:
2518 case FILE_OVERWRITE
:
2519 case FILE_OVERWRITE_IF
:
2521 DEBUG(5,("open_directory: invalid create_disposition "
2522 "0x%x for directory %s\n",
2523 (unsigned int)create_disposition
,
2524 smb_fname_str_dbg(smb_dname
)));
2525 return NT_STATUS_INVALID_PARAMETER
;
2528 if(!S_ISDIR(smb_dname
->st
.st_ex_mode
)) {
2529 DEBUG(5,("open_directory: %s is not a directory !\n",
2530 smb_fname_str_dbg(smb_dname
)));
2531 return NT_STATUS_NOT_A_DIRECTORY
;
2534 if (info
== FILE_WAS_OPENED
) {
2535 uint32_t access_granted
= 0;
2536 status
= smbd_check_open_rights(conn
, smb_dname
, access_mask
,
2539 /* Were we trying to do a directory open
2540 * for delete and didn't get DELETE
2541 * access (only) ? Check if the
2542 * directory allows DELETE_CHILD.
2544 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
2547 if ((NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
) &&
2548 (access_mask
& DELETE_ACCESS
) &&
2549 (access_granted
== DELETE_ACCESS
) &&
2550 can_delete_file_in_directory(conn
, smb_dname
))) {
2551 DEBUG(10,("open_directory: overrode ACCESS_DENIED "
2552 "on directory %s\n",
2553 smb_fname_str_dbg(smb_dname
)));
2554 status
= NT_STATUS_OK
;
2557 if (!NT_STATUS_IS_OK(status
)) {
2558 DEBUG(10, ("open_directory: smbd_check_open_rights on "
2559 "file %s failed with %s\n",
2560 smb_fname_str_dbg(smb_dname
),
2561 nt_errstr(status
)));
2566 status
= file_new(req
, conn
, &fsp
);
2567 if(!NT_STATUS_IS_OK(status
)) {
2572 * Setup the files_struct for it.
2575 fsp
->mode
= smb_dname
->st
.st_ex_mode
;
2576 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &smb_dname
->st
);
2577 fsp
->vuid
= req
? req
->vuid
: UID_FIELD_INVALID
;
2578 fsp
->file_pid
= req
? req
->smbpid
: 0;
2579 fsp
->can_lock
= False
;
2580 fsp
->can_read
= False
;
2581 fsp
->can_write
= False
;
2583 fsp
->share_access
= share_access
;
2584 fsp
->fh
->private_options
= create_options
;
2586 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2588 fsp
->access_mask
= access_mask
| FILE_READ_ATTRIBUTES
;
2589 fsp
->print_file
= False
;
2590 fsp
->modified
= False
;
2591 fsp
->oplock_type
= NO_OPLOCK
;
2592 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
2593 fsp
->is_directory
= True
;
2594 fsp
->posix_open
= (file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) ? True
: False
;
2595 status
= fsp_set_smb_fname(fsp
, smb_dname
);
2596 if (!NT_STATUS_IS_OK(status
)) {
2600 mtimespec
= smb_dname
->st
.st_ex_mtime
;
2602 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
,
2603 conn
->connectpath
, smb_dname
, &mtimespec
);
2606 DEBUG(0, ("open_directory: Could not get share mode lock for "
2607 "%s\n", smb_fname_str_dbg(smb_dname
)));
2608 file_free(req
, fsp
);
2609 return NT_STATUS_SHARING_VIOLATION
;
2612 status
= open_mode_check(conn
, lck
, access_mask
, share_access
,
2613 create_options
, &dir_existed
);
2615 if (!NT_STATUS_IS_OK(status
)) {
2617 file_free(req
, fsp
);
2621 set_share_mode(lck
, fsp
, conn
->server_info
->utok
.uid
, 0, NO_OPLOCK
);
2623 /* For directories the delete on close bit at open time seems
2624 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2625 if (create_options
& FILE_DELETE_ON_CLOSE
) {
2626 status
= can_set_delete_on_close(fsp
, 0);
2627 if (!NT_STATUS_IS_OK(status
) && !NT_STATUS_EQUAL(status
, NT_STATUS_DIRECTORY_NOT_EMPTY
)) {
2629 file_free(req
, fsp
);
2633 if (NT_STATUS_IS_OK(status
)) {
2634 /* Note that here we set the *inital* delete on close flag,
2635 not the regular one. The magic gets handled in close. */
2636 fsp
->initial_delete_on_close
= True
;
2647 return NT_STATUS_OK
;
2650 NTSTATUS
create_directory(connection_struct
*conn
, struct smb_request
*req
,
2651 struct smb_filename
*smb_dname
)
2656 status
= SMB_VFS_CREATE_FILE(
2659 0, /* root_dir_fid */
2660 smb_dname
, /* fname */
2661 FILE_READ_ATTRIBUTES
, /* access_mask */
2662 FILE_SHARE_NONE
, /* share_access */
2663 FILE_CREATE
, /* create_disposition*/
2664 FILE_DIRECTORY_FILE
, /* create_options */
2665 FILE_ATTRIBUTE_DIRECTORY
, /* file_attributes */
2666 0, /* oplock_request */
2667 0, /* allocation_size */
2673 if (NT_STATUS_IS_OK(status
)) {
2674 close_file(req
, fsp
, NORMAL_CLOSE
);
2680 /****************************************************************************
2681 Receive notification that one of our open files has been renamed by another
2683 ****************************************************************************/
2685 void msg_file_was_renamed(struct messaging_context
*msg
,
2688 struct server_id server_id
,
2692 char *frm
= (char *)data
->data
;
2694 const char *sharepath
;
2695 const char *base_name
;
2696 const char *stream_name
;
2697 struct smb_filename
*smb_fname
= NULL
;
2698 size_t sp_len
, bn_len
;
2701 if (data
->data
== NULL
2702 || data
->length
< MSG_FILE_RENAMED_MIN_SIZE
+ 2) {
2703 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2704 (int)data
->length
));
2708 /* Unpack the message. */
2709 pull_file_id_24(frm
, &id
);
2710 sharepath
= &frm
[24];
2711 sp_len
= strlen(sharepath
);
2712 base_name
= sharepath
+ sp_len
+ 1;
2713 bn_len
= strlen(base_name
);
2714 stream_name
= sharepath
+ sp_len
+ 1 + bn_len
+ 1;
2716 /* stream_name must always be NULL if there is no stream. */
2717 if (stream_name
[0] == '\0') {
2721 status
= create_synthetic_smb_fname(talloc_tos(), base_name
,
2722 stream_name
, NULL
, &smb_fname
);
2723 if (!NT_STATUS_IS_OK(status
)) {
2727 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2729 sharepath
, smb_fname_str_dbg(smb_fname
),
2730 file_id_string_tos(&id
)));
2732 for(fsp
= file_find_di_first(id
); fsp
; fsp
= file_find_di_next(fsp
)) {
2733 if (memcmp(fsp
->conn
->connectpath
, sharepath
, sp_len
) == 0) {
2735 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2736 fsp
->fnum
, fsp_str_dbg(fsp
),
2737 smb_fname_str_dbg(smb_fname
)));
2738 status
= fsp_set_smb_fname(fsp
, smb_fname
);
2739 if (!NT_STATUS_IS_OK(status
)) {
2744 /* Now we have the complete path we can work out if this is
2745 actually within this share and adjust newname accordingly. */
2746 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2747 "not sharepath %s) "
2748 "fnum %d from %s -> %s\n",
2749 fsp
->conn
->connectpath
,
2753 smb_fname_str_dbg(smb_fname
)));
2757 TALLOC_FREE(smb_fname
);
2762 * If a main file is opened for delete, all streams need to be checked for
2763 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2764 * If that works, delete them all by setting the delete on close and close.
2767 NTSTATUS
open_streams_for_delete(connection_struct
*conn
,
2770 struct stream_struct
*stream_info
;
2771 files_struct
**streams
;
2773 unsigned int num_streams
;
2774 TALLOC_CTX
*frame
= talloc_stackframe();
2777 status
= SMB_VFS_STREAMINFO(conn
, NULL
, fname
, talloc_tos(),
2778 &num_streams
, &stream_info
);
2780 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)
2781 || NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
2782 DEBUG(10, ("no streams around\n"));
2784 return NT_STATUS_OK
;
2787 if (!NT_STATUS_IS_OK(status
)) {
2788 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2789 nt_errstr(status
)));
2793 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2796 if (num_streams
== 0) {
2798 return NT_STATUS_OK
;
2801 streams
= TALLOC_ARRAY(talloc_tos(), files_struct
*, num_streams
);
2802 if (streams
== NULL
) {
2803 DEBUG(0, ("talloc failed\n"));
2804 status
= NT_STATUS_NO_MEMORY
;
2808 for (i
=0; i
<num_streams
; i
++) {
2809 struct smb_filename
*smb_fname
= NULL
;
2811 if (strequal(stream_info
[i
].name
, "::$DATA")) {
2816 status
= create_synthetic_smb_fname(talloc_tos(), fname
,
2817 stream_info
[i
].name
,
2819 if (!NT_STATUS_IS_OK(status
)) {
2823 if (SMB_VFS_STAT(conn
, smb_fname
) == -1) {
2824 DEBUG(10, ("Unable to stat stream: %s\n",
2825 smb_fname_str_dbg(smb_fname
)));
2828 status
= SMB_VFS_CREATE_FILE(
2831 0, /* root_dir_fid */
2832 smb_fname
, /* fname */
2833 DELETE_ACCESS
, /* access_mask */
2834 (FILE_SHARE_READ
| /* share_access */
2835 FILE_SHARE_WRITE
| FILE_SHARE_DELETE
),
2836 FILE_OPEN
, /* create_disposition*/
2837 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
, /* create_options */
2838 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
2839 0, /* oplock_request */
2840 0, /* allocation_size */
2843 &streams
[i
], /* result */
2846 if (!NT_STATUS_IS_OK(status
)) {
2847 DEBUG(10, ("Could not open stream %s: %s\n",
2848 smb_fname_str_dbg(smb_fname
),
2849 nt_errstr(status
)));
2851 TALLOC_FREE(smb_fname
);
2854 TALLOC_FREE(smb_fname
);
2858 * don't touch the variable "status" beyond this point :-)
2861 for (i
-= 1 ; i
>= 0; i
--) {
2862 if (streams
[i
] == NULL
) {
2866 DEBUG(10, ("Closing stream # %d, %s\n", i
,
2867 fsp_str_dbg(streams
[i
])));
2868 close_file(NULL
, streams
[i
], NORMAL_CLOSE
);
2877 * Wrapper around open_file_ntcreate and open_directory
2880 static NTSTATUS
create_file_unixpath(connection_struct
*conn
,
2881 struct smb_request
*req
,
2882 struct smb_filename
*smb_fname
,
2883 uint32_t access_mask
,
2884 uint32_t share_access
,
2885 uint32_t create_disposition
,
2886 uint32_t create_options
,
2887 uint32_t file_attributes
,
2888 uint32_t oplock_request
,
2889 uint64_t allocation_size
,
2890 struct security_descriptor
*sd
,
2891 struct ea_list
*ea_list
,
2893 files_struct
**result
,
2896 int info
= FILE_WAS_OPENED
;
2897 files_struct
*base_fsp
= NULL
;
2898 files_struct
*fsp
= NULL
;
2901 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2902 "file_attributes = 0x%x, share_access = 0x%x, "
2903 "create_disposition = 0x%x create_options = 0x%x "
2904 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2906 (unsigned int)access_mask
,
2907 (unsigned int)file_attributes
,
2908 (unsigned int)share_access
,
2909 (unsigned int)create_disposition
,
2910 (unsigned int)create_options
,
2911 (unsigned int)oplock_request
,
2912 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
2914 if (create_options
& FILE_OPEN_BY_FILE_ID
) {
2915 status
= NT_STATUS_NOT_SUPPORTED
;
2919 if (create_options
& NTCREATEX_OPTIONS_INVALID_PARAM_MASK
) {
2920 status
= NT_STATUS_INVALID_PARAMETER
;
2925 oplock_request
|= INTERNAL_OPEN_ONLY
;
2928 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
2929 && (access_mask
& DELETE_ACCESS
)
2930 && !is_ntfs_stream_smb_fname(smb_fname
)) {
2932 * We can't open a file with DELETE access if any of the
2933 * streams is open without FILE_SHARE_DELETE
2935 status
= open_streams_for_delete(conn
, smb_fname
->base_name
);
2937 if (!NT_STATUS_IS_OK(status
)) {
2942 /* This is the correct thing to do (check every time) but can_delete
2943 * is expensive (it may have to read the parent directory
2944 * permissions). So for now we're not doing it unless we have a strong
2945 * hint the client is really going to delete this file. If the client
2946 * is forcing FILE_CREATE let the filesystem take care of the
2949 /* Setting FILE_SHARE_DELETE is the hint. */
2951 if (lp_acl_check_permissions(SNUM(conn
))
2952 && (create_disposition
!= FILE_CREATE
)
2953 && (access_mask
& DELETE_ACCESS
)
2954 && (!(can_delete_file_in_directory(conn
, smb_fname
) ||
2955 can_access_file_acl(conn
, smb_fname
, DELETE_ACCESS
)))) {
2956 status
= NT_STATUS_ACCESS_DENIED
;
2957 DEBUG(10,("create_file_unixpath: open file %s "
2958 "for delete ACCESS_DENIED\n",
2959 smb_fname_str_dbg(smb_fname
)));
2963 if ((access_mask
& SEC_FLAG_SYSTEM_SECURITY
) &&
2964 !user_has_privileges(current_user
.nt_user_token
, &se_security
)) {
2965 DEBUG(10, ("create_file_unixpath:: open on %s "
2966 "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n",
2967 smb_fname_str_dbg(smb_fname
)));
2968 status
= NT_STATUS_PRIVILEGE_NOT_HELD
;
2972 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
)
2973 && is_ntfs_stream_smb_fname(smb_fname
)
2974 && (!(create_options
& NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE
))) {
2975 uint32 base_create_disposition
;
2976 struct smb_filename
*smb_fname_base
= NULL
;
2978 if (create_options
& FILE_DIRECTORY_FILE
) {
2979 status
= NT_STATUS_NOT_A_DIRECTORY
;
2983 switch (create_disposition
) {
2985 base_create_disposition
= FILE_OPEN
;
2988 base_create_disposition
= FILE_OPEN_IF
;
2992 /* Create an smb_filename with stream_name == NULL. */
2993 status
= create_synthetic_smb_fname(talloc_tos(),
2994 smb_fname
->base_name
,
2997 if (!NT_STATUS_IS_OK(status
)) {
3001 if (SMB_VFS_STAT(conn
, smb_fname_base
) == -1) {
3002 DEBUG(10, ("Unable to stat stream: %s\n",
3003 smb_fname_str_dbg(smb_fname_base
)));
3006 /* Open the base file. */
3007 status
= create_file_unixpath(conn
, NULL
, smb_fname_base
, 0,
3010 | FILE_SHARE_DELETE
,
3011 base_create_disposition
,
3012 0, 0, 0, 0, NULL
, NULL
,
3014 TALLOC_FREE(smb_fname_base
);
3016 if (!NT_STATUS_IS_OK(status
)) {
3017 DEBUG(10, ("create_file_unixpath for base %s failed: "
3018 "%s\n", smb_fname
->base_name
,
3019 nt_errstr(status
)));
3022 /* we don't need to low level fd */
3027 * If it's a request for a directory open, deal with it separately.
3030 if (create_options
& FILE_DIRECTORY_FILE
) {
3032 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3033 status
= NT_STATUS_INVALID_PARAMETER
;
3037 /* Can't open a temp directory. IFS kit test. */
3038 if (!(file_attributes
& FILE_FLAG_POSIX_SEMANTICS
) &&
3039 (file_attributes
& FILE_ATTRIBUTE_TEMPORARY
)) {
3040 status
= NT_STATUS_INVALID_PARAMETER
;
3045 * We will get a create directory here if the Win32
3046 * app specified a security descriptor in the
3047 * CreateDirectory() call.
3051 status
= open_directory(
3052 conn
, req
, smb_fname
, access_mask
, share_access
,
3053 create_disposition
, create_options
, file_attributes
,
3058 * Ordinary file case.
3061 status
= file_new(req
, conn
, &fsp
);
3062 if(!NT_STATUS_IS_OK(status
)) {
3066 status
= fsp_set_smb_fname(fsp
, smb_fname
);
3067 if (!NT_STATUS_IS_OK(status
)) {
3072 * We're opening the stream element of a base_fsp
3073 * we already opened. Set up the base_fsp pointer.
3076 fsp
->base_fsp
= base_fsp
;
3079 status
= open_file_ntcreate(conn
,
3090 if(!NT_STATUS_IS_OK(status
)) {
3091 file_free(req
, fsp
);
3095 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_IS_A_DIRECTORY
)) {
3097 /* A stream open never opens a directory */
3100 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3105 * Fail the open if it was explicitly a non-directory
3109 if (create_options
& FILE_NON_DIRECTORY_FILE
) {
3110 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
3115 status
= open_directory(
3116 conn
, req
, smb_fname
, access_mask
,
3117 share_access
, create_disposition
,
3118 create_options
, file_attributes
,
3123 if (!NT_STATUS_IS_OK(status
)) {
3127 fsp
->base_fsp
= base_fsp
;
3130 * According to the MS documentation, the only time the security
3131 * descriptor is applied to the opened file is iff we *created* the
3132 * file; an existing file stays the same.
3134 * Also, it seems (from observation) that you can open the file with
3135 * any access mask but you can still write the sd. We need to override
3136 * the granted access before we call set_sd
3137 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3140 if ((sd
!= NULL
) && (info
== FILE_WAS_CREATED
)
3141 && lp_nt_acl_support(SNUM(conn
))) {
3143 uint32_t sec_info_sent
;
3144 uint32_t saved_access_mask
= fsp
->access_mask
;
3146 sec_info_sent
= get_sec_info(sd
);
3148 fsp
->access_mask
= FILE_GENERIC_ALL
;
3150 /* Convert all the generic bits. */
3151 security_acl_map_generic(sd
->dacl
, &file_generic_mapping
);
3152 security_acl_map_generic(sd
->sacl
, &file_generic_mapping
);
3154 if (sec_info_sent
& (OWNER_SECURITY_INFORMATION
|
3155 GROUP_SECURITY_INFORMATION
|
3156 DACL_SECURITY_INFORMATION
|
3157 SACL_SECURITY_INFORMATION
)) {
3158 status
= SMB_VFS_FSET_NT_ACL(fsp
, sec_info_sent
, sd
);
3161 fsp
->access_mask
= saved_access_mask
;
3163 if (!NT_STATUS_IS_OK(status
)) {
3168 if ((ea_list
!= NULL
) &&
3169 ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
))) {
3170 status
= set_ea(conn
, fsp
, fsp
->fsp_name
, ea_list
);
3171 if (!NT_STATUS_IS_OK(status
)) {
3176 if (!fsp
->is_directory
&& S_ISDIR(fsp
->fsp_name
->st
.st_ex_mode
)) {
3177 status
= NT_STATUS_ACCESS_DENIED
;
3181 /* Save the requested allocation size. */
3182 if ((info
== FILE_WAS_CREATED
) || (info
== FILE_WAS_OVERWRITTEN
)) {
3184 && (allocation_size
> fsp
->fsp_name
->st
.st_ex_size
)) {
3185 fsp
->initial_allocation_size
= smb_roundup(
3186 fsp
->conn
, allocation_size
);
3187 if (fsp
->is_directory
) {
3188 /* Can't set allocation size on a directory. */
3189 status
= NT_STATUS_ACCESS_DENIED
;
3192 if (vfs_allocate_file_space(
3193 fsp
, fsp
->initial_allocation_size
) == -1) {
3194 status
= NT_STATUS_DISK_FULL
;
3198 fsp
->initial_allocation_size
= smb_roundup(
3199 fsp
->conn
, (uint64_t)fsp
->fsp_name
->st
.st_ex_size
);
3203 DEBUG(10, ("create_file_unixpath: info=%d\n", info
));
3206 if (pinfo
!= NULL
) {
3210 smb_fname
->st
= fsp
->fsp_name
->st
;
3212 return NT_STATUS_OK
;
3215 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status
)));
3218 if (base_fsp
&& fsp
->base_fsp
== base_fsp
) {
3220 * The close_file below will close
3225 close_file(req
, fsp
, ERROR_CLOSE
);
3228 if (base_fsp
!= NULL
) {
3229 close_file(req
, base_fsp
, ERROR_CLOSE
);
3236 * Calculate the full path name given a relative fid.
3238 NTSTATUS
get_relative_fid_filename(connection_struct
*conn
,
3239 struct smb_request
*req
,
3240 uint16_t root_dir_fid
,
3241 const struct smb_filename
*smb_fname
,
3242 struct smb_filename
**smb_fname_out
)
3244 files_struct
*dir_fsp
;
3245 char *parent_fname
= NULL
;
3246 char *new_base_name
= NULL
;
3249 if (root_dir_fid
== 0 || !smb_fname
) {
3250 status
= NT_STATUS_INTERNAL_ERROR
;
3254 dir_fsp
= file_fsp(req
, root_dir_fid
);
3256 if (dir_fsp
== NULL
) {
3257 status
= NT_STATUS_INVALID_HANDLE
;
3261 if (is_ntfs_stream_smb_fname(dir_fsp
->fsp_name
)) {
3262 status
= NT_STATUS_INVALID_HANDLE
;
3266 if (!dir_fsp
->is_directory
) {
3269 * Check to see if this is a mac fork of some kind.
3272 if ((conn
->fs_capabilities
& FILE_NAMED_STREAMS
) &&
3273 is_ntfs_stream_smb_fname(smb_fname
)) {
3274 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3279 we need to handle the case when we get a
3280 relative open relative to a file and the
3281 pathname is blank - this is a reopen!
3282 (hint from demyn plantenberg)
3285 status
= NT_STATUS_INVALID_HANDLE
;
3289 if (ISDOT(dir_fsp
->fsp_name
->base_name
)) {
3291 * We're at the toplevel dir, the final file name
3292 * must not contain ./, as this is filtered out
3293 * normally by srvstr_get_path and unix_convert
3294 * explicitly rejects paths containing ./.
3296 parent_fname
= talloc_strdup(talloc_tos(), "");
3297 if (parent_fname
== NULL
) {
3298 status
= NT_STATUS_NO_MEMORY
;
3302 size_t dir_name_len
= strlen(dir_fsp
->fsp_name
->base_name
);
3305 * Copy in the base directory name.
3308 parent_fname
= TALLOC_ARRAY(talloc_tos(), char,
3310 if (parent_fname
== NULL
) {
3311 status
= NT_STATUS_NO_MEMORY
;
3314 memcpy(parent_fname
, dir_fsp
->fsp_name
->base_name
,
3318 * Ensure it ends in a '/'.
3319 * We used TALLOC_SIZE +2 to add space for the '/'.
3323 && (parent_fname
[dir_name_len
-1] != '\\')
3324 && (parent_fname
[dir_name_len
-1] != '/')) {
3325 parent_fname
[dir_name_len
] = '/';
3326 parent_fname
[dir_name_len
+1] = '\0';
3330 new_base_name
= talloc_asprintf(talloc_tos(), "%s%s", parent_fname
,
3331 smb_fname
->base_name
);
3332 if (new_base_name
== NULL
) {
3333 status
= NT_STATUS_NO_MEMORY
;
3337 status
= filename_convert(req
,
3339 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
3344 if (!NT_STATUS_IS_OK(status
)) {
3349 TALLOC_FREE(parent_fname
);
3353 NTSTATUS
create_file_default(connection_struct
*conn
,
3354 struct smb_request
*req
,
3355 uint16_t root_dir_fid
,
3356 struct smb_filename
*smb_fname
,
3357 uint32_t access_mask
,
3358 uint32_t share_access
,
3359 uint32_t create_disposition
,
3360 uint32_t create_options
,
3361 uint32_t file_attributes
,
3362 uint32_t oplock_request
,
3363 uint64_t allocation_size
,
3364 struct security_descriptor
*sd
,
3365 struct ea_list
*ea_list
,
3366 files_struct
**result
,
3369 int info
= FILE_WAS_OPENED
;
3370 files_struct
*fsp
= NULL
;
3373 DEBUG(10,("create_file: access_mask = 0x%x "
3374 "file_attributes = 0x%x, share_access = 0x%x, "
3375 "create_disposition = 0x%x create_options = 0x%x "
3376 "oplock_request = 0x%x "
3377 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3379 (unsigned int)access_mask
,
3380 (unsigned int)file_attributes
,
3381 (unsigned int)share_access
,
3382 (unsigned int)create_disposition
,
3383 (unsigned int)create_options
,
3384 (unsigned int)oplock_request
,
3385 (unsigned int)root_dir_fid
,
3386 ea_list
, sd
, smb_fname_str_dbg(smb_fname
)));
3389 * Calculate the filename from the root_dir_if if necessary.
3392 if (root_dir_fid
!= 0) {
3393 struct smb_filename
*smb_fname_out
= NULL
;
3394 status
= get_relative_fid_filename(conn
, req
, root_dir_fid
,
3395 smb_fname
, &smb_fname_out
);
3396 if (!NT_STATUS_IS_OK(status
)) {
3399 smb_fname
= smb_fname_out
;
3403 * Check to see if this is a mac fork of some kind.
3406 if (is_ntfs_stream_smb_fname(smb_fname
)) {
3407 enum FAKE_FILE_TYPE fake_file_type
;
3409 fake_file_type
= is_fake_file(smb_fname
);
3411 if (fake_file_type
!= FAKE_FILE_TYPE_NONE
) {
3414 * Here we go! support for changing the disk quotas
3417 * We need to fake up to open this MAGIC QUOTA file
3418 * and return a valid FID.
3420 * w2k close this file directly after openening xp
3421 * also tries a QUERY_FILE_INFO on the file and then
3424 status
= open_fake_file(req
, conn
, req
->vuid
,
3425 fake_file_type
, smb_fname
,
3427 if (!NT_STATUS_IS_OK(status
)) {
3431 ZERO_STRUCT(smb_fname
->st
);
3435 if (!(conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
3436 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3441 /* All file access must go through check_name() */
3443 status
= check_name(conn
, smb_fname
->base_name
);
3444 if (!NT_STATUS_IS_OK(status
)) {
3448 status
= create_file_unixpath(
3449 conn
, req
, smb_fname
, access_mask
, share_access
,
3450 create_disposition
, create_options
, file_attributes
,
3451 oplock_request
, allocation_size
, sd
, ea_list
,
3454 if (!NT_STATUS_IS_OK(status
)) {
3459 DEBUG(10, ("create_file: info=%d\n", info
));
3462 if (pinfo
!= NULL
) {
3465 return NT_STATUS_OK
;
3468 DEBUG(10, ("create_file: %s\n", nt_errstr(status
)));
3471 close_file(req
, fsp
, ERROR_CLOSE
);