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 const struct generic_mapping file_generic_mapping;
27 struct deferred_open_record {
28 bool delayed_for_oplocks;
32 static NTSTATUS create_file_unixpath(connection_struct *conn,
33 struct smb_request *req,
36 uint32_t share_access,
37 uint32_t create_disposition,
38 uint32_t create_options,
39 uint32_t file_attributes,
40 uint32_t oplock_request,
41 uint64_t allocation_size,
42 struct security_descriptor *sd,
43 struct ea_list *ea_list,
45 files_struct **result,
47 SMB_STRUCT_STAT *psbuf);
49 /****************************************************************************
50 SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES.
51 ****************************************************************************/
53 NTSTATUS smb1_file_se_access_check(const struct security_descriptor *sd,
54 const NT_USER_TOKEN *token,
55 uint32_t access_desired,
56 uint32_t *access_granted)
58 return se_access_check(sd,
60 (access_desired & ~FILE_READ_ATTRIBUTES),
64 /****************************************************************************
65 Check if we have open rights.
66 ****************************************************************************/
68 static NTSTATUS check_open_rights(struct connection_struct *conn,
71 uint32_t *access_granted)
73 /* Check if we have rights to open. */
75 struct security_descriptor *sd;
79 status = SMB_VFS_GET_NT_ACL(conn, fname,
80 (OWNER_SECURITY_INFORMATION |
81 GROUP_SECURITY_INFORMATION |
82 DACL_SECURITY_INFORMATION),&sd);
84 if (!NT_STATUS_IS_OK(status)) {
85 DEBUG(10, ("check_open_rights: Could not get acl "
92 status = smb1_file_se_access_check(sd,
93 conn->server_info->ptok,
99 DEBUG(10,("check_open_rights: file %s requesting "
100 "0x%x returning 0x%x (%s)\n",
102 (unsigned int)access_mask,
103 (unsigned int)*access_granted,
104 nt_errstr(status) ));
109 /****************************************************************************
110 fd support routines - attempt to do a dos_open.
111 ****************************************************************************/
113 static NTSTATUS fd_open(struct connection_struct *conn,
119 NTSTATUS status = NT_STATUS_OK;
123 * Never follow symlinks on a POSIX client. The
124 * client should be doing this.
127 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
132 fsp->fh->fd = SMB_VFS_OPEN(conn,fname,fsp,flags,mode);
133 if (fsp->fh->fd == -1) {
134 status = map_nt_error_from_unix(errno);
137 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
138 fname, flags, (int)mode, fsp->fh->fd,
139 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
144 /****************************************************************************
145 Close the file associated with a fsp.
146 ****************************************************************************/
148 NTSTATUS fd_close(files_struct *fsp)
152 if (fsp->fh->fd == -1) {
153 return NT_STATUS_OK; /* What we used to call a stat open. */
155 if (fsp->fh->ref_count > 1) {
156 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
159 ret = SMB_VFS_CLOSE(fsp);
162 return map_nt_error_from_unix(errno);
167 /****************************************************************************
168 Change the ownership of a file to that of the parent directory.
169 Do this by fd if possible.
170 ****************************************************************************/
172 void change_file_owner_to_parent(connection_struct *conn,
173 const char *inherit_from_dir,
176 SMB_STRUCT_STAT parent_st;
179 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
181 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
182 "directory %s. Error was %s\n",
183 inherit_from_dir, strerror(errno) ));
188 ret = SMB_VFS_FCHOWN(fsp, parent_st.st_uid, (gid_t)-1);
191 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
192 "file %s to parent directory uid %u. Error "
193 "was %s\n", fsp->fsp_name,
194 (unsigned int)parent_st.st_uid,
198 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
199 "parent directory uid %u.\n", fsp->fsp_name,
200 (unsigned int)parent_st.st_uid ));
203 NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
204 const char *inherit_from_dir,
206 SMB_STRUCT_STAT *psbuf)
208 char *saved_dir = NULL;
209 SMB_STRUCT_STAT sbuf;
210 SMB_STRUCT_STAT parent_st;
211 TALLOC_CTX *ctx = talloc_tos();
212 NTSTATUS status = NT_STATUS_OK;
215 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
217 status = map_nt_error_from_unix(errno);
218 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
219 "directory %s. Error was %s\n",
220 inherit_from_dir, strerror(errno) ));
224 /* We've already done an lstat into psbuf, and we know it's a
225 directory. If we can cd into the directory and the dev/ino
226 are the same then we can safely chown without races as
227 we're locking the directory in place by being in it. This
228 should work on any UNIX (thanks tridge :-). JRA.
231 saved_dir = vfs_GetWd(ctx,conn);
233 status = map_nt_error_from_unix(errno);
234 DEBUG(0,("change_dir_owner_to_parent: failed to get "
235 "current working directory. Error was %s\n",
240 /* Chdir into the new path. */
241 if (vfs_ChDir(conn, fname) == -1) {
242 status = map_nt_error_from_unix(errno);
243 DEBUG(0,("change_dir_owner_to_parent: failed to change "
244 "current working directory to %s. Error "
245 "was %s\n", fname, strerror(errno) ));
249 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
250 status = map_nt_error_from_unix(errno);
251 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
252 "directory '.' (%s) Error was %s\n",
253 fname, strerror(errno)));
257 /* Ensure we're pointing at the same place. */
258 if (sbuf.st_dev != psbuf->st_dev ||
259 sbuf.st_ino != psbuf->st_ino ||
260 sbuf.st_mode != psbuf->st_mode ) {
261 DEBUG(0,("change_dir_owner_to_parent: "
262 "device/inode/mode on directory %s changed. "
263 "Refusing to chown !\n", fname ));
264 status = NT_STATUS_ACCESS_DENIED;
269 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
272 status = map_nt_error_from_unix(errno);
273 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
274 "directory %s to parent directory uid %u. "
275 "Error was %s\n", fname,
276 (unsigned int)parent_st.st_uid, strerror(errno) ));
280 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
281 "directory %s to parent directory uid %u.\n",
282 fname, (unsigned int)parent_st.st_uid ));
286 vfs_ChDir(conn,saved_dir);
290 /****************************************************************************
292 ****************************************************************************/
294 static NTSTATUS open_file(files_struct *fsp,
295 connection_struct *conn,
296 struct smb_request *req,
297 const char *parent_dir,
300 SMB_STRUCT_STAT *psbuf,
303 uint32 access_mask, /* client requested access mask. */
304 uint32 open_access_mask) /* what we're actually using in the open. */
306 NTSTATUS status = NT_STATUS_OK;
307 int accmode = (flags & O_ACCMODE);
308 int local_flags = flags;
309 bool file_existed = VALID_STAT(*psbuf);
314 /* Check permissions */
317 * This code was changed after seeing a client open request
318 * containing the open mode of (DENY_WRITE/read-only) with
319 * the 'create if not exist' bit set. The previous code
320 * would fail to open the file read only on a read-only share
321 * as it was checking the flags parameter directly against O_RDONLY,
322 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
326 if (!CAN_WRITE(conn)) {
327 /* It's a read-only share - fail if we wanted to write. */
328 if(accmode != O_RDONLY) {
329 DEBUG(3,("Permission denied opening %s\n", path));
330 return NT_STATUS_ACCESS_DENIED;
331 } else if(flags & O_CREAT) {
332 /* We don't want to write - but we must make sure that
333 O_CREAT doesn't create the file if we have write
334 access into the directory.
337 local_flags &= ~O_CREAT;
342 * This little piece of insanity is inspired by the
343 * fact that an NT client can open a file for O_RDONLY,
344 * but set the create disposition to FILE_EXISTS_TRUNCATE.
345 * If the client *can* write to the file, then it expects to
346 * truncate the file, even though it is opening for readonly.
347 * Quicken uses this stupid trick in backup file creation...
348 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
349 * for helping track this one down. It didn't bite us in 2.0.x
350 * as we always opened files read-write in that release. JRA.
353 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
354 DEBUG(10,("open_file: truncate requested on read-only open "
355 "for file %s\n", path));
356 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
359 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
360 (!file_existed && (local_flags & O_CREAT)) ||
361 ((local_flags & O_TRUNC) == O_TRUNC) ) {
365 * We can't actually truncate here as the file may be locked.
366 * open_file_ntcreate will take care of the truncate later. JRA.
369 local_flags &= ~O_TRUNC;
371 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
373 * We would block on opening a FIFO with no one else on the
374 * other end. Do what we used to do and add O_NONBLOCK to the
378 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
379 local_flags |= O_NONBLOCK;
383 /* Don't create files with Microsoft wildcard characters. */
386 * wildcard characters are allowed in stream names
387 * only test the basefilename
389 wild = fsp->base_fsp->fsp_name;
393 if ((local_flags & O_CREAT) && !file_existed &&
395 return NT_STATUS_OBJECT_NAME_INVALID;
398 /* Actually do the open */
399 status = fd_open(conn, path, fsp, local_flags, unx_mode);
400 if (!NT_STATUS_IS_OK(status)) {
401 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
403 path,nt_errstr(status),local_flags,flags));
407 if ((local_flags & O_CREAT) && !file_existed) {
409 /* Inherit the ACL if required */
410 if (lp_inherit_perms(SNUM(conn))) {
411 inherit_access_posix_acl(conn, parent_dir, path,
415 /* Change the owner if required. */
416 if (lp_inherit_owner(SNUM(conn))) {
417 change_file_owner_to_parent(conn, parent_dir,
421 notify_fname(conn, NOTIFY_ACTION_ADDED,
422 FILE_NOTIFY_CHANGE_FILE_NAME, path);
426 fsp->fh->fd = -1; /* What we used to call a stat open. */
428 uint32_t access_granted = 0;
430 status = check_open_rights(conn,
434 if (!NT_STATUS_IS_OK(status)) {
435 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
436 if ((access_mask & DELETE_ACCESS) &&
437 (access_granted == DELETE_ACCESS) &&
438 can_delete_file_in_directory(conn, path)) {
439 /* Were we trying to do a stat open
440 * for delete and didn't get DELETE
441 * access (only) ? Check if the
442 * directory allows DELETE_CHILD.
444 * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx
447 DEBUG(10,("open_file: overrode ACCESS_DENIED "
451 DEBUG(10, ("open_file: Access denied on "
456 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
458 S_ISLNK(psbuf->st_mode)) {
459 /* This is a POSIX stat open for delete
460 * or rename on a symlink that points
462 DEBUG(10, ("open_file: allowing POSIX open "
463 "on bad symlink %s\n",
466 DEBUG(10, ("open_file: check_open_rights "
467 "on file %s returned %s\n",
468 path, nt_errstr(status) ));
478 if (fsp->fh->fd == -1) {
479 ret = SMB_VFS_STAT(conn, path, psbuf);
481 ret = SMB_VFS_FSTAT(fsp, psbuf);
482 /* If we have an fd, this stat should succeed. */
484 DEBUG(0,("Error doing fstat on open file %s "
485 "(%s)\n", path,strerror(errno) ));
489 /* For a non-io open, this stat failing means file not found. JRA */
491 status = map_nt_error_from_unix(errno);
498 * POSIX allows read-only opens of directories. We don't
499 * want to do this (we use a different code path for this)
500 * so catch a directory open and return an EISDIR. JRA.
503 if(S_ISDIR(psbuf->st_mode)) {
506 return NT_STATUS_FILE_IS_A_DIRECTORY;
509 fsp->mode = psbuf->st_mode;
510 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
511 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
512 fsp->file_pid = req ? req->smbpid : 0;
513 fsp->can_lock = True;
514 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
515 if (!CAN_WRITE(conn)) {
516 fsp->can_write = False;
518 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
521 fsp->print_file = False;
522 fsp->modified = False;
523 fsp->sent_oplock_break = NO_BREAK_SENT;
524 fsp->is_directory = False;
525 if (conn->aio_write_behind_list &&
526 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
527 fsp->aio_write_behind = True;
530 string_set(&fsp->fsp_name, path);
531 fsp->wcp = NULL; /* Write cache pointer. */
533 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
534 conn->server_info->unix_name,
536 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
537 conn->num_files_open));
543 /*******************************************************************
544 Return True if the filename is one of the special executable types.
545 ********************************************************************/
547 bool is_executable(const char *fname)
549 if ((fname = strrchr_m(fname,'.'))) {
550 if (strequal(fname,".com") ||
551 strequal(fname,".dll") ||
552 strequal(fname,".exe") ||
553 strequal(fname,".sym")) {
560 /****************************************************************************
561 Check if we can open a file with a share mode.
562 Returns True if conflict, False if not.
563 ****************************************************************************/
565 static bool share_conflict(struct share_mode_entry *entry,
569 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
570 "entry->share_access = 0x%x, "
571 "entry->private_options = 0x%x\n",
572 (unsigned int)entry->access_mask,
573 (unsigned int)entry->share_access,
574 (unsigned int)entry->private_options));
576 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
577 (unsigned int)access_mask, (unsigned int)share_access));
579 if ((entry->access_mask & (FILE_WRITE_DATA|
583 DELETE_ACCESS)) == 0) {
584 DEBUG(10,("share_conflict: No conflict due to "
585 "entry->access_mask = 0x%x\n",
586 (unsigned int)entry->access_mask ));
590 if ((access_mask & (FILE_WRITE_DATA|
594 DELETE_ACCESS)) == 0) {
595 DEBUG(10,("share_conflict: No conflict due to "
596 "access_mask = 0x%x\n",
597 (unsigned int)access_mask ));
601 #if 1 /* JRA TEST - Superdebug. */
602 #define CHECK_MASK(num, am, right, sa, share) \
603 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
604 (unsigned int)(num), (unsigned int)(am), \
605 (unsigned int)(right), (unsigned int)(am)&(right) )); \
606 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
607 (unsigned int)(num), (unsigned int)(sa), \
608 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
609 if (((am) & (right)) && !((sa) & (share))) { \
610 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
611 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
612 (unsigned int)(share) )); \
616 #define CHECK_MASK(num, am, right, sa, share) \
617 if (((am) & (right)) && !((sa) & (share))) { \
618 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
619 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
620 (unsigned int)(share) )); \
625 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
626 share_access, FILE_SHARE_WRITE);
627 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
628 entry->share_access, FILE_SHARE_WRITE);
630 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
631 share_access, FILE_SHARE_READ);
632 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
633 entry->share_access, FILE_SHARE_READ);
635 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
636 share_access, FILE_SHARE_DELETE);
637 CHECK_MASK(6, access_mask, DELETE_ACCESS,
638 entry->share_access, FILE_SHARE_DELETE);
640 DEBUG(10,("share_conflict: No conflict.\n"));
644 #if defined(DEVELOPER)
645 static void validate_my_share_entries(int num,
646 struct share_mode_entry *share_entry)
650 if (!procid_is_me(&share_entry->pid)) {
654 if (is_deferred_open_entry(share_entry) &&
655 !open_was_deferred(share_entry->op_mid)) {
656 char *str = talloc_asprintf(talloc_tos(),
657 "Got a deferred entry without a request: "
659 share_mode_str(talloc_tos(), num, share_entry));
663 if (!is_valid_share_mode_entry(share_entry)) {
667 fsp = file_find_dif(share_entry->id,
668 share_entry->share_file_id);
670 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
671 share_mode_str(talloc_tos(), num, share_entry) ));
672 smb_panic("validate_my_share_entries: Cannot match a "
673 "share entry with an open file\n");
676 if (is_deferred_open_entry(share_entry) ||
677 is_unused_share_mode_entry(share_entry)) {
681 if ((share_entry->op_type == NO_OPLOCK) &&
682 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
683 /* Someone has already written to it, but I haven't yet
688 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
697 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
698 share_mode_str(talloc_tos(), num, share_entry) ));
699 str = talloc_asprintf(talloc_tos(),
700 "validate_my_share_entries: "
701 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
702 fsp->fsp_name, (unsigned int)fsp->oplock_type,
703 (unsigned int)share_entry->op_type );
709 bool is_stat_open(uint32 access_mask)
711 return (access_mask &&
712 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
713 FILE_WRITE_ATTRIBUTES))==0) &&
714 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
715 FILE_WRITE_ATTRIBUTES)) != 0));
718 /****************************************************************************
719 Deal with share modes
720 Invarient: Share mode must be locked on entry and exit.
721 Returns -1 on error, or number of share modes on success (may be zero).
722 ****************************************************************************/
724 static NTSTATUS open_mode_check(connection_struct *conn,
726 struct share_mode_lock *lck,
729 uint32 create_options,
734 if(lck->num_share_modes == 0) {
738 *file_existed = True;
740 /* A delete on close prohibits everything */
742 if (lck->delete_on_close) {
743 return NT_STATUS_DELETE_PENDING;
746 if (is_stat_open(access_mask)) {
747 /* Stat open that doesn't trigger oplock breaks or share mode
748 * checks... ! JRA. */
753 * Check if the share modes will give us access.
756 #if defined(DEVELOPER)
757 for(i = 0; i < lck->num_share_modes; i++) {
758 validate_my_share_entries(i, &lck->share_modes[i]);
762 if (!lp_share_modes(SNUM(conn))) {
766 /* Now we check the share modes, after any oplock breaks. */
767 for(i = 0; i < lck->num_share_modes; i++) {
769 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
773 /* someone else has a share lock on it, check to see if we can
775 if (share_conflict(&lck->share_modes[i],
776 access_mask, share_access)) {
777 return NT_STATUS_SHARING_VIOLATION;
784 static bool is_delete_request(files_struct *fsp) {
785 return ((fsp->access_mask == DELETE_ACCESS) &&
786 (fsp->oplock_type == NO_OPLOCK));
790 * Send a break message to the oplock holder and delay the open for
794 static NTSTATUS send_break_message(files_struct *fsp,
795 struct share_mode_entry *exclusive,
800 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
802 DEBUG(10, ("Sending break request to PID %s\n",
803 procid_str_static(&exclusive->pid)));
804 exclusive->op_mid = mid;
806 /* Create the message. */
807 share_mode_entry_to_message(msg, exclusive);
809 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
810 don't want this set in the share mode struct pointed to by lck. */
812 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
813 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
816 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
817 MSG_SMB_BREAK_REQUEST,
819 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
820 if (!NT_STATUS_IS_OK(status)) {
821 DEBUG(3, ("Could not send oplock break message: %s\n",
829 * 1) No files open at all or internal open: Grant whatever the client wants.
831 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
832 * request, break if the oplock around is a batch oplock. If it's another
833 * requested access type, break.
835 * 3) Only level2 around: Grant level2 and do nothing else.
838 static bool delay_for_oplocks(struct share_mode_lock *lck,
845 struct share_mode_entry *exclusive = NULL;
846 bool valid_entry = false;
847 bool have_level2 = false;
848 bool have_a_none_oplock = false;
849 bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
850 lp_level2_oplocks(SNUM(fsp->conn));
852 if (oplock_request & INTERNAL_OPEN_ONLY) {
853 fsp->oplock_type = NO_OPLOCK;
856 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
860 for (i=0; i<lck->num_share_modes; i++) {
862 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
866 /* At least one entry is not an invalid or deferred entry. */
869 if (pass_number == 1) {
870 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
871 SMB_ASSERT(exclusive == NULL);
872 exclusive = &lck->share_modes[i];
875 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
876 SMB_ASSERT(exclusive == NULL);
877 exclusive = &lck->share_modes[i];
881 if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
882 SMB_ASSERT(exclusive == NULL);
886 if (lck->share_modes[i].op_type == NO_OPLOCK) {
887 have_a_none_oplock = true;
891 if (exclusive != NULL) { /* Found an exclusive oplock */
892 bool delay_it = is_delete_request(fsp) ?
893 BATCH_OPLOCK_TYPE(exclusive->op_type) : true;
894 SMB_ASSERT(!have_level2);
896 send_break_message(fsp, exclusive, mid, oplock_request);
902 * Match what was requested (fsp->oplock_type) with
903 * what was found in the existing share modes.
907 /* All entries are placeholders or deferred.
908 * Directly grant whatever the client wants. */
909 if (fsp->oplock_type == NO_OPLOCK) {
910 /* Store a level2 oplock, but don't tell the client */
911 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
913 } else if (have_a_none_oplock) {
914 fsp->oplock_type = NO_OPLOCK;
915 } else if (have_level2) {
916 if (fsp->oplock_type == NO_OPLOCK ||
917 fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
918 /* Store a level2 oplock, but don't tell the client */
919 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
921 fsp->oplock_type = LEVEL_II_OPLOCK;
924 /* This case can never happen. */
929 * Don't grant level2 to clients that don't want them
930 * or if we've turned them off.
932 if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) {
933 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
936 DEBUG(10,("delay_for_oplocks: oplock type 0x%x on file %s\n",
937 fsp->oplock_type, fsp->fsp_name));
943 bool request_timed_out(struct timeval request_time,
944 struct timeval timeout)
946 struct timeval now, end_time;
948 end_time = timeval_sum(&request_time, &timeout);
949 return (timeval_compare(&end_time, &now) < 0);
952 /****************************************************************************
953 Handle the 1 second delay in returning a SHARING_VIOLATION error.
954 ****************************************************************************/
956 static void defer_open(struct share_mode_lock *lck,
957 struct timeval request_time,
958 struct timeval timeout,
959 struct smb_request *req,
960 struct deferred_open_record *state)
966 for (i=0; i<lck->num_share_modes; i++) {
967 struct share_mode_entry *e = &lck->share_modes[i];
969 if (!is_deferred_open_entry(e)) {
973 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
974 DEBUG(0, ("Trying to defer an already deferred "
975 "request: mid=%d, exiting\n", req->mid));
976 exit_server("attempt to defer a deferred request");
980 /* End paranoia check */
982 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
983 "open entry for mid %u\n",
984 (unsigned int)request_time.tv_sec,
985 (unsigned int)request_time.tv_usec,
986 (unsigned int)req->mid));
988 if (!push_deferred_smb_message(req, request_time, timeout,
989 (char *)state, sizeof(*state))) {
990 exit_server("push_deferred_smb_message failed");
992 add_deferred_open(lck, req->mid, request_time, state->id);
995 * Push the MID of this packet on the signing queue.
996 * We only do this once, the first time we push the packet
997 * onto the deferred open queue, as this has a side effect
998 * of incrementing the response sequence number.
1001 srv_defer_sign_response(req->mid);
1005 /****************************************************************************
1006 On overwrite open ensure that the attributes match.
1007 ****************************************************************************/
1009 bool open_match_attributes(connection_struct *conn,
1011 uint32 old_dos_attr,
1012 uint32 new_dos_attr,
1013 mode_t existing_unx_mode,
1014 mode_t new_unx_mode,
1015 mode_t *returned_unx_mode)
1017 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
1019 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1020 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
1022 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
1023 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
1024 *returned_unx_mode = new_unx_mode;
1026 *returned_unx_mode = (mode_t)0;
1029 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
1030 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
1031 "returned_unx_mode = 0%o\n",
1033 (unsigned int)old_dos_attr,
1034 (unsigned int)existing_unx_mode,
1035 (unsigned int)new_dos_attr,
1036 (unsigned int)*returned_unx_mode ));
1038 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
1039 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1040 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
1041 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
1045 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1046 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
1047 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
1054 /****************************************************************************
1055 Special FCB or DOS processing in the case of a sharing violation.
1056 Try and find a duplicated file handle.
1057 ****************************************************************************/
1059 NTSTATUS fcb_or_dos_open(struct smb_request *req,
1060 connection_struct *conn,
1061 files_struct *fsp_to_dup_into,
1067 uint32 share_access,
1068 uint32 create_options)
1072 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
1073 "file %s.\n", fname ));
1075 for(fsp = file_find_di_first(id); fsp;
1076 fsp = file_find_di_next(fsp)) {
1078 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1079 "vuid = %u, file_pid = %u, private_options = 0x%x "
1080 "access_mask = 0x%x\n", fsp->fsp_name,
1081 fsp->fh->fd, (unsigned int)fsp->vuid,
1082 (unsigned int)fsp->file_pid,
1083 (unsigned int)fsp->fh->private_options,
1084 (unsigned int)fsp->access_mask ));
1086 if (fsp->fh->fd != -1 &&
1087 fsp->vuid == vuid &&
1088 fsp->file_pid == file_pid &&
1089 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1090 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1091 (fsp->access_mask & FILE_WRITE_DATA) &&
1092 strequal(fsp->fsp_name, fname)) {
1093 DEBUG(10,("fcb_or_dos_open: file match\n"));
1099 return NT_STATUS_NOT_FOUND;
1102 /* quite an insane set of semantics ... */
1103 if (is_executable(fname) &&
1104 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1105 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1106 return NT_STATUS_INVALID_PARAMETER;
1109 /* We need to duplicate this fsp. */
1110 dup_file_fsp(req, fsp, access_mask, share_access,
1111 create_options, fsp_to_dup_into);
1113 return NT_STATUS_OK;
1116 /****************************************************************************
1117 Open a file with a share mode - old openX method - map into NTCreate.
1118 ****************************************************************************/
1120 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
1121 uint32 *paccess_mask,
1122 uint32 *pshare_mode,
1123 uint32 *pcreate_disposition,
1124 uint32 *pcreate_options)
1128 uint32 create_disposition;
1129 uint32 create_options = FILE_NON_DIRECTORY_FILE;
1131 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1132 "open_func = 0x%x\n",
1133 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
1135 /* Create the NT compatible access_mask. */
1136 switch (GET_OPENX_MODE(deny_mode)) {
1137 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1138 case DOS_OPEN_RDONLY:
1139 access_mask = FILE_GENERIC_READ;
1141 case DOS_OPEN_WRONLY:
1142 access_mask = FILE_GENERIC_WRITE;
1146 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1149 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1150 (unsigned int)GET_OPENX_MODE(deny_mode)));
1154 /* Create the NT compatible create_disposition. */
1155 switch (open_func) {
1156 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1157 create_disposition = FILE_CREATE;
1160 case OPENX_FILE_EXISTS_OPEN:
1161 create_disposition = FILE_OPEN;
1164 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1165 create_disposition = FILE_OPEN_IF;
1168 case OPENX_FILE_EXISTS_TRUNCATE:
1169 create_disposition = FILE_OVERWRITE;
1172 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1173 create_disposition = FILE_OVERWRITE_IF;
1177 /* From samba4 - to be confirmed. */
1178 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1179 create_disposition = FILE_CREATE;
1182 DEBUG(10,("map_open_params_to_ntcreate: bad "
1183 "open_func 0x%x\n", (unsigned int)open_func));
1187 /* Create the NT compatible share modes. */
1188 switch (GET_DENY_MODE(deny_mode)) {
1190 share_mode = FILE_SHARE_NONE;
1194 share_mode = FILE_SHARE_READ;
1198 share_mode = FILE_SHARE_WRITE;
1202 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1206 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1207 if (is_executable(fname)) {
1208 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1210 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1211 share_mode = FILE_SHARE_READ;
1213 share_mode = FILE_SHARE_NONE;
1219 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1220 share_mode = FILE_SHARE_NONE;
1224 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1225 (unsigned int)GET_DENY_MODE(deny_mode) ));
1229 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1230 "share_mode = 0x%x, create_disposition = 0x%x, "
1231 "create_options = 0x%x\n",
1233 (unsigned int)access_mask,
1234 (unsigned int)share_mode,
1235 (unsigned int)create_disposition,
1236 (unsigned int)create_options ));
1239 *paccess_mask = access_mask;
1242 *pshare_mode = share_mode;
1244 if (pcreate_disposition) {
1245 *pcreate_disposition = create_disposition;
1247 if (pcreate_options) {
1248 *pcreate_options = create_options;
1255 static void schedule_defer_open(struct share_mode_lock *lck,
1256 struct timeval request_time,
1257 struct smb_request *req)
1259 struct deferred_open_record state;
1261 /* This is a relative time, added to the absolute
1262 request_time value to get the absolute timeout time.
1263 Note that if this is the second or greater time we enter
1264 this codepath for this particular request mid then
1265 request_time is left as the absolute time of the *first*
1266 time this request mid was processed. This is what allows
1267 the request to eventually time out. */
1269 struct timeval timeout;
1271 /* Normally the smbd we asked should respond within
1272 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1273 * the client did, give twice the timeout as a safety
1274 * measure here in case the other smbd is stuck
1275 * somewhere else. */
1277 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1279 /* Nothing actually uses state.delayed_for_oplocks
1280 but it's handy to differentiate in debug messages
1281 between a 30 second delay due to oplock break, and
1282 a 1 second delay for share mode conflicts. */
1284 state.delayed_for_oplocks = True;
1287 if (!request_timed_out(request_time, timeout)) {
1288 defer_open(lck, request_time, timeout, req, &state);
1292 /****************************************************************************
1293 Work out what access_mask to use from what the client sent us.
1294 ****************************************************************************/
1296 static NTSTATUS calculate_access_mask(connection_struct *conn,
1299 uint32_t access_mask,
1300 uint32_t *access_mask_out)
1305 * Convert GENERIC bits to specific bits.
1308 se_map_generic(&access_mask, &file_generic_mapping);
1310 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1311 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1314 struct security_descriptor *sd;
1315 uint32_t access_granted = 0;
1317 status = SMB_VFS_GET_NT_ACL(conn, fname,
1318 (OWNER_SECURITY_INFORMATION |
1319 GROUP_SECURITY_INFORMATION |
1320 DACL_SECURITY_INFORMATION),&sd);
1322 if (!NT_STATUS_IS_OK(status)) {
1323 DEBUG(10, ("calculate_access_mask: Could not get acl "
1326 nt_errstr(status)));
1327 return NT_STATUS_ACCESS_DENIED;
1330 status = smb1_file_se_access_check(sd,
1331 conn->server_info->ptok,
1337 if (!NT_STATUS_IS_OK(status)) {
1338 DEBUG(10, ("calculate_access_mask: Access denied on "
1339 "file %s: when calculating maximum access\n",
1341 return NT_STATUS_ACCESS_DENIED;
1344 access_mask = access_granted;
1346 access_mask = FILE_GENERIC_ALL;
1350 *access_mask_out = access_mask;
1351 return NT_STATUS_OK;
1354 /****************************************************************************
1355 Open a file with a share mode. Passed in an already created files_struct *.
1356 ****************************************************************************/
1358 static NTSTATUS open_file_ntcreate(connection_struct *conn,
1359 struct smb_request *req,
1361 SMB_STRUCT_STAT *psbuf,
1362 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1363 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1364 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1365 uint32 create_options, /* options such as delete on close. */
1366 uint32 new_dos_attributes, /* attributes used for new file. */
1367 int oplock_request, /* internal Samba oplock codes. */
1368 /* Information (FILE_EXISTS etc.) */
1374 bool file_existed = VALID_STAT(*psbuf);
1375 bool def_acl = False;
1376 bool posix_open = False;
1377 bool new_file_created = False;
1378 bool clear_ads = false;
1380 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1381 mode_t new_unx_mode = (mode_t)0;
1382 mode_t unx_mode = (mode_t)0;
1384 uint32 existing_dos_attributes = 0;
1385 struct pending_message_list *pml = NULL;
1386 struct timeval request_time = timeval_zero();
1387 struct share_mode_lock *lck = NULL;
1388 uint32 open_access_mask = access_mask;
1392 const char *newname;
1396 if (conn->printer) {
1398 * Printers are handled completely differently.
1399 * Most of the passed parameters are ignored.
1403 *pinfo = FILE_WAS_CREATED;
1406 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1408 return print_fsp_open(req, conn, fname, req->vuid, fsp, psbuf);
1411 if (!parent_dirname(talloc_tos(), fname, &parent_dir, &newname)) {
1412 return NT_STATUS_NO_MEMORY;
1415 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1417 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1418 new_dos_attributes = 0;
1420 /* We add aARCH to this as this mode is only used if the file is
1422 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1426 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1427 "access_mask=0x%x share_access=0x%x "
1428 "create_disposition = 0x%x create_options=0x%x "
1429 "unix mode=0%o oplock_request=%d\n",
1430 fname, new_dos_attributes, access_mask, share_access,
1431 create_disposition, create_options, unx_mode,
1434 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1435 DEBUG(0, ("No smb request but not an internal only open!\n"));
1436 return NT_STATUS_INTERNAL_ERROR;
1440 * Only non-internal opens can be deferred at all
1444 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1445 struct deferred_open_record *state =
1446 (struct deferred_open_record *)pml->private_data.data;
1448 /* Remember the absolute time of the original
1449 request with this mid. We'll use it later to
1450 see if this has timed out. */
1452 request_time = pml->request_time;
1454 /* Remove the deferred open entry under lock. */
1455 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1458 DEBUG(0, ("could not get share mode lock\n"));
1460 del_deferred_open_entry(lck, req->mid);
1464 /* Ensure we don't reprocess this message. */
1465 remove_deferred_open_smb_message(req->mid);
1468 status = check_name(conn, fname);
1469 if (!NT_STATUS_IS_OK(status)) {
1474 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1476 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1480 /* ignore any oplock requests if oplocks are disabled */
1481 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1482 IS_VETO_OPLOCK_PATH(conn, fname)) {
1483 /* Mask off everything except the private Samba bits. */
1484 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1487 /* this is for OS/2 long file names - say we don't support them */
1488 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1489 /* OS/2 Workplace shell fix may be main code stream in a later
1491 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1493 if (use_nt_status()) {
1494 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1496 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1499 switch( create_disposition ) {
1501 * Currently we're using FILE_SUPERSEDE as the same as
1502 * FILE_OVERWRITE_IF but they really are
1503 * different. FILE_SUPERSEDE deletes an existing file
1504 * (requiring delete access) then recreates it.
1506 case FILE_SUPERSEDE:
1507 /* If file exists replace/overwrite. If file doesn't
1509 flags2 |= (O_CREAT | O_TRUNC);
1513 case FILE_OVERWRITE_IF:
1514 /* If file exists replace/overwrite. If file doesn't
1516 flags2 |= (O_CREAT | O_TRUNC);
1521 /* If file exists open. If file doesn't exist error. */
1522 if (!file_existed) {
1523 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1524 "requested for file %s and file "
1525 "doesn't exist.\n", fname ));
1527 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1531 case FILE_OVERWRITE:
1532 /* If file exists overwrite. If file doesn't exist
1534 if (!file_existed) {
1535 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1536 "requested for file %s and file "
1537 "doesn't exist.\n", fname ));
1539 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1546 /* If file exists error. If file doesn't exist
1549 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1550 "requested for file %s and file "
1551 "already exists.\n", fname ));
1552 if (S_ISDIR(psbuf->st_mode)) {
1557 return map_nt_error_from_unix(errno);
1559 flags2 |= (O_CREAT|O_EXCL);
1563 /* If file exists open. If file doesn't exist
1569 return NT_STATUS_INVALID_PARAMETER;
1572 /* We only care about matching attributes on file exists and
1575 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1576 (create_disposition == FILE_OVERWRITE_IF))) {
1577 if (!open_match_attributes(conn, fname,
1578 existing_dos_attributes,
1579 new_dos_attributes, psbuf->st_mode,
1580 unx_mode, &new_unx_mode)) {
1581 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1582 "for file %s (%x %x) (0%o, 0%o)\n",
1583 fname, existing_dos_attributes,
1585 (unsigned int)psbuf->st_mode,
1586 (unsigned int)unx_mode ));
1588 return NT_STATUS_ACCESS_DENIED;
1592 status = calculate_access_mask(conn, fname, file_existed,
1595 if (!NT_STATUS_IS_OK(status)) {
1596 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1597 "on file %s returned %s\n",
1599 nt_errstr(status)));
1603 open_access_mask = access_mask;
1605 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1606 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1609 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1610 "access_mask=0x%x\n", fname, access_mask ));
1613 * Note that we ignore the append flag as append does not
1614 * mean the same thing under DOS and Unix.
1617 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1618 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1619 /* DENY_DOS opens are always underlying read-write on the
1620 file handle, no matter what the requested access mask
1622 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1623 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1633 * Currently we only look at FILE_WRITE_THROUGH for create options.
1637 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1642 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1646 if (!posix_open && !CAN_WRITE(conn)) {
1648 * We should really return a permission denied error if either
1649 * O_CREAT or O_TRUNC are set, but for compatibility with
1650 * older versions of Samba we just AND them out.
1652 flags2 &= ~(O_CREAT|O_TRUNC);
1656 * Ensure we can't write on a read-only share or file.
1659 if (flags != O_RDONLY && file_existed &&
1660 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1661 DEBUG(5,("open_file_ntcreate: write access requested for "
1662 "file %s on read only %s\n",
1663 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1665 return NT_STATUS_ACCESS_DENIED;
1668 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1669 fsp->share_access = share_access;
1670 fsp->fh->private_options = create_options;
1671 fsp->access_mask = open_access_mask; /* We change this to the
1672 * requested access_mask after
1673 * the open is done. */
1674 fsp->posix_open = posix_open;
1676 /* Ensure no SAMBA_PRIVATE bits can be set. */
1677 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1679 if (timeval_is_zero(&request_time)) {
1680 request_time = fsp->open_time;
1684 struct timespec old_write_time = get_mtimespec(psbuf);
1685 id = vfs_file_id_from_sbuf(conn, psbuf);
1687 lck = get_share_mode_lock(talloc_tos(), id,
1689 fname, &old_write_time);
1692 DEBUG(0, ("Could not get share mode lock\n"));
1693 return NT_STATUS_SHARING_VIOLATION;
1696 /* First pass - send break only on batch oplocks. */
1698 && delay_for_oplocks(lck, fsp, req->mid, 1,
1700 schedule_defer_open(lck, request_time, req);
1702 return NT_STATUS_SHARING_VIOLATION;
1705 /* Use the client requested access mask here, not the one we
1707 status = open_mode_check(conn, fname, lck,
1708 access_mask, share_access,
1709 create_options, &file_existed);
1711 if (NT_STATUS_IS_OK(status)) {
1712 /* We might be going to allow this open. Check oplock
1714 /* Second pass - send break for both batch or
1715 * exclusive oplocks. */
1717 && delay_for_oplocks(lck, fsp, req->mid, 2,
1719 schedule_defer_open(lck, request_time, req);
1721 return NT_STATUS_SHARING_VIOLATION;
1725 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1726 /* DELETE_PENDING is not deferred for a second */
1731 if (!NT_STATUS_IS_OK(status)) {
1732 uint32 can_access_mask;
1733 bool can_access = True;
1735 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1737 /* Check if this can be done with the deny_dos and fcb
1739 if (create_options &
1740 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1741 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1743 DEBUG(0, ("DOS open without an SMB "
1746 return NT_STATUS_INTERNAL_ERROR;
1749 /* Use the client requested access mask here,
1750 * not the one we open with. */
1751 status = fcb_or_dos_open(req,
1762 if (NT_STATUS_IS_OK(status)) {
1765 *pinfo = FILE_WAS_OPENED;
1767 return NT_STATUS_OK;
1772 * This next line is a subtlety we need for
1773 * MS-Access. If a file open will fail due to share
1774 * permissions and also for security (access) reasons,
1775 * we need to return the access failed error, not the
1776 * share error. We can't open the file due to kernel
1777 * oplock deadlock (it's possible we failed above on
1778 * the open_mode_check()) so use a userspace check.
1781 if (flags & O_RDWR) {
1782 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1783 } else if (flags & O_WRONLY) {
1784 can_access_mask = FILE_WRITE_DATA;
1786 can_access_mask = FILE_READ_DATA;
1789 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1790 !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
1795 * If we're returning a share violation, ensure we
1796 * cope with the braindead 1 second delay.
1799 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1800 lp_defer_sharing_violations()) {
1801 struct timeval timeout;
1802 struct deferred_open_record state;
1805 /* this is a hack to speed up torture tests
1807 timeout_usecs = lp_parm_int(SNUM(conn),
1808 "smbd","sharedelay",
1809 SHARING_VIOLATION_USEC_WAIT);
1811 /* This is a relative time, added to the absolute
1812 request_time value to get the absolute timeout time.
1813 Note that if this is the second or greater time we enter
1814 this codepath for this particular request mid then
1815 request_time is left as the absolute time of the *first*
1816 time this request mid was processed. This is what allows
1817 the request to eventually time out. */
1819 timeout = timeval_set(0, timeout_usecs);
1821 /* Nothing actually uses state.delayed_for_oplocks
1822 but it's handy to differentiate in debug messages
1823 between a 30 second delay due to oplock break, and
1824 a 1 second delay for share mode conflicts. */
1826 state.delayed_for_oplocks = False;
1830 && !request_timed_out(request_time,
1832 defer_open(lck, request_time, timeout,
1840 * We have detected a sharing violation here
1841 * so return the correct error code
1843 status = NT_STATUS_SHARING_VIOLATION;
1845 status = NT_STATUS_ACCESS_DENIED;
1851 * We exit this block with the share entry *locked*.....
1855 SMB_ASSERT(!file_existed || (lck != NULL));
1858 * Ensure we pay attention to default ACLs on directories if required.
1861 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1862 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1866 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1867 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1868 (unsigned int)flags, (unsigned int)flags2,
1869 (unsigned int)unx_mode, (unsigned int)access_mask,
1870 (unsigned int)open_access_mask));
1873 * open_file strips any O_TRUNC flags itself.
1876 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1877 flags|flags2, unx_mode, access_mask,
1880 if (!NT_STATUS_IS_OK(fsp_open)) {
1887 if (!file_existed) {
1888 struct timespec old_write_time = get_mtimespec(psbuf);
1890 * Deal with the race condition where two smbd's detect the
1891 * file doesn't exist and do the create at the same time. One
1892 * of them will win and set a share mode, the other (ie. this
1893 * one) should check if the requested share mode for this
1894 * create is allowed.
1898 * Now the file exists and fsp is successfully opened,
1899 * fsp->dev and fsp->inode are valid and should replace the
1900 * dev=0,inode=0 from a non existent file. Spotted by
1901 * Nadav Danieli <nadavd@exanet.com>. JRA.
1906 lck = get_share_mode_lock(talloc_tos(), id,
1908 fname, &old_write_time);
1911 DEBUG(0, ("open_file_ntcreate: Could not get share "
1912 "mode lock for %s\n", fname));
1914 return NT_STATUS_SHARING_VIOLATION;
1917 /* First pass - send break only on batch oplocks. */
1919 && delay_for_oplocks(lck, fsp, req->mid, 1,
1921 schedule_defer_open(lck, request_time, req);
1924 return NT_STATUS_SHARING_VIOLATION;
1927 status = open_mode_check(conn, fname, lck,
1928 access_mask, share_access,
1929 create_options, &file_existed);
1931 if (NT_STATUS_IS_OK(status)) {
1932 /* We might be going to allow this open. Check oplock
1934 /* Second pass - send break for both batch or
1935 * exclusive oplocks. */
1937 && delay_for_oplocks(lck, fsp, req->mid, 2,
1939 schedule_defer_open(lck, request_time, req);
1942 return NT_STATUS_SHARING_VIOLATION;
1946 if (!NT_STATUS_IS_OK(status)) {
1947 struct deferred_open_record state;
1951 state.delayed_for_oplocks = False;
1954 /* Do it all over again immediately. In the second
1955 * round we will find that the file existed and handle
1956 * the DELETE_PENDING and FCB cases correctly. No need
1957 * to duplicate the code here. Essentially this is a
1958 * "goto top of this function", but don't tell
1962 defer_open(lck, request_time, timeval_zero(),
1970 * We exit this block with the share entry *locked*.....
1975 SMB_ASSERT(lck != NULL);
1977 /* Delete streams if create_disposition requires it */
1978 if (file_existed && clear_ads) {
1979 status = delete_all_streams(conn, fname);
1980 if (!NT_STATUS_IS_OK(status)) {
1987 /* note that we ignore failure for the following. It is
1988 basically a hack for NFS, and NFS will never set one of
1989 these only read them. Nobody but Samba can ever set a deny
1990 mode and we have already checked our more authoritative
1991 locking database for permission to set this deny mode. If
1992 the kernel refuses the operations then the kernel is wrong.
1993 note that GPFS supports it as well - jmcd */
1995 if (fsp->fh->fd != -1) {
1996 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1997 if(ret_flock == -1 ){
2002 return NT_STATUS_SHARING_VIOLATION;
2007 * At this point onwards, we can guarentee that the share entry
2008 * is locked, whether we created the file or not, and that the
2009 * deny mode is compatible with all current opens.
2013 * If requested, truncate the file.
2016 if (flags2&O_TRUNC) {
2018 * We are modifing the file after open - update the stat
2021 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
2022 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
2023 status = map_nt_error_from_unix(errno);
2030 /* Record the options we were opened with. */
2031 fsp->share_access = share_access;
2032 fsp->fh->private_options = create_options;
2034 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2036 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2039 /* stat opens on existing files don't get oplocks. */
2040 if (is_stat_open(open_access_mask)) {
2041 fsp->oplock_type = NO_OPLOCK;
2044 if (!(flags2 & O_TRUNC)) {
2045 info = FILE_WAS_OPENED;
2047 info = FILE_WAS_OVERWRITTEN;
2050 info = FILE_WAS_CREATED;
2058 * Setup the oplock info in both the shared memory and
2062 if (!set_file_oplock(fsp, fsp->oplock_type)) {
2063 /* Could not get the kernel oplock */
2064 fsp->oplock_type = NO_OPLOCK;
2067 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
2068 new_file_created = True;
2071 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
2074 /* Handle strange delete on close create semantics. */
2075 if (create_options & FILE_DELETE_ON_CLOSE) {
2077 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
2079 if (!NT_STATUS_IS_OK(status)) {
2080 /* Remember to delete the mode we just added. */
2081 del_share_mode(lck, fsp);
2086 /* Note that here we set the *inital* delete on close flag,
2087 not the regular one. The magic gets handled in close. */
2088 fsp->initial_delete_on_close = True;
2091 if (new_file_created) {
2092 /* Files should be initially set as archive */
2093 if (lp_map_archive(SNUM(conn)) ||
2094 lp_store_dos_attributes(SNUM(conn))) {
2096 SMB_STRUCT_STAT tmp_sbuf;
2097 SET_STAT_INVALID(tmp_sbuf);
2098 if (file_set_dosmode(
2100 new_dos_attributes | aARCH,
2101 &tmp_sbuf, parent_dir,
2103 unx_mode = tmp_sbuf.st_mode;
2110 * Take care of inherited ACLs on created files - if default ACL not
2114 if (!posix_open && !file_existed && !def_acl) {
2116 int saved_errno = errno; /* We might get ENOSYS in the next
2119 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2121 errno = saved_errno; /* Ignore ENOSYS */
2124 } else if (new_unx_mode) {
2128 /* Attributes need changing. File already existed. */
2131 int saved_errno = errno; /* We might get ENOSYS in the
2133 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2135 if (ret == -1 && errno == ENOSYS) {
2136 errno = saved_errno; /* Ignore ENOSYS */
2138 DEBUG(5, ("open_file_ntcreate: reset "
2139 "attributes of file %s to 0%o\n",
2140 fname, (unsigned int)new_unx_mode));
2141 ret = 0; /* Don't do the fchmod below. */
2146 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2147 DEBUG(5, ("open_file_ntcreate: failed to reset "
2148 "attributes of file %s to 0%o\n",
2149 fname, (unsigned int)new_unx_mode));
2152 /* If this is a successful open, we must remove any deferred open
2155 del_deferred_open_entry(lck, req->mid);
2159 return NT_STATUS_OK;
2163 /****************************************************************************
2164 Open a file for for write to ensure that we can fchmod it.
2165 ****************************************************************************/
2167 NTSTATUS open_file_fchmod(struct smb_request *req, connection_struct *conn,
2169 SMB_STRUCT_STAT *psbuf, files_struct **result)
2171 files_struct *fsp = NULL;
2174 if (!VALID_STAT(*psbuf)) {
2175 return NT_STATUS_INVALID_PARAMETER;
2178 status = file_new(req, conn, &fsp);
2179 if(!NT_STATUS_IS_OK(status)) {
2183 status = SMB_VFS_CREATE_FILE(
2186 0, /* root_dir_fid */
2188 0, /* create_file_flags */
2189 FILE_WRITE_DATA, /* access_mask */
2190 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
2192 FILE_OPEN, /* create_disposition*/
2193 0, /* create_options */
2194 0, /* file_attributes */
2195 0, /* oplock_request */
2196 0, /* allocation_size */
2204 * This is not a user visible file open.
2205 * Don't set a share mode.
2208 if (!NT_STATUS_IS_OK(status)) {
2209 file_free(req, fsp);
2214 return NT_STATUS_OK;
2217 /****************************************************************************
2218 Close the fchmod file fd - ensure no locks are lost.
2219 ****************************************************************************/
2221 NTSTATUS close_file_fchmod(struct smb_request *req, files_struct *fsp)
2223 NTSTATUS status = fd_close(fsp);
2224 file_free(req, fsp);
2228 static NTSTATUS mkdir_internal(connection_struct *conn,
2230 uint32 file_attributes,
2231 SMB_STRUCT_STAT *psbuf)
2235 const char *dirname;
2237 bool posix_open = false;
2239 if(!CAN_WRITE(conn)) {
2240 DEBUG(5,("mkdir_internal: failing create on read-only share "
2241 "%s\n", lp_servicename(SNUM(conn))));
2242 return NT_STATUS_ACCESS_DENIED;
2245 status = check_name(conn, name);
2246 if (!NT_STATUS_IS_OK(status)) {
2250 if (!parent_dirname(talloc_tos(), name, &parent_dir, &dirname)) {
2251 return NT_STATUS_NO_MEMORY;
2254 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2256 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2258 mode = unix_mode(conn, aDIR, name, parent_dir);
2261 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2262 return map_nt_error_from_unix(errno);
2265 /* Ensure we're checking for a symlink here.... */
2266 /* We don't want to get caught by a symlink racer. */
2268 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2269 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2270 name, strerror(errno)));
2271 return map_nt_error_from_unix(errno);
2274 if (!S_ISDIR(psbuf->st_mode)) {
2275 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2277 return NT_STATUS_ACCESS_DENIED;
2280 if (lp_store_dos_attributes(SNUM(conn))) {
2282 file_set_dosmode(conn, name,
2283 file_attributes | aDIR, NULL,
2289 if (lp_inherit_perms(SNUM(conn))) {
2290 inherit_access_posix_acl(conn, parent_dir, name, mode);
2293 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2295 * Check if high bits should have been set,
2296 * then (if bits are missing): add them.
2297 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2300 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2301 SMB_VFS_CHMOD(conn, name,
2302 psbuf->st_mode | (mode & ~psbuf->st_mode));
2306 /* Change the owner if required. */
2307 if (lp_inherit_owner(SNUM(conn))) {
2308 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2311 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2314 return NT_STATUS_OK;
2317 /****************************************************************************
2318 Open a directory from an NT SMB call.
2319 ****************************************************************************/
2321 static NTSTATUS open_directory(connection_struct *conn,
2322 struct smb_request *req,
2324 SMB_STRUCT_STAT *psbuf,
2326 uint32 share_access,
2327 uint32 create_disposition,
2328 uint32 create_options,
2329 uint32 file_attributes,
2331 files_struct **result)
2333 files_struct *fsp = NULL;
2334 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2335 struct share_mode_lock *lck = NULL;
2337 struct timespec mtimespec;
2340 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2341 "share_access = 0x%x create_options = 0x%x, "
2342 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2344 (unsigned int)access_mask,
2345 (unsigned int)share_access,
2346 (unsigned int)create_options,
2347 (unsigned int)create_disposition,
2348 (unsigned int)file_attributes));
2350 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2351 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2352 is_ntfs_stream_name(fname)) {
2353 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2354 return NT_STATUS_NOT_A_DIRECTORY;
2357 status = calculate_access_mask(conn, fname, dir_existed,
2360 if (!NT_STATUS_IS_OK(status)) {
2361 DEBUG(10, ("open_directory: calculate_access_mask "
2362 "on file %s returned %s\n",
2364 nt_errstr(status)));
2368 switch( create_disposition ) {
2371 info = FILE_WAS_OPENED;
2374 * We want to follow symlinks here.
2377 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2378 return map_nt_error_from_unix(errno);
2385 /* If directory exists error. If directory doesn't
2388 status = mkdir_internal(conn,
2393 if (!NT_STATUS_IS_OK(status)) {
2394 DEBUG(2, ("open_directory: unable to create "
2395 "%s. Error was %s\n", fname,
2396 nt_errstr(status)));
2400 info = FILE_WAS_CREATED;
2405 * If directory exists open. If directory doesn't
2409 status = mkdir_internal(conn,
2414 if (NT_STATUS_IS_OK(status)) {
2415 info = FILE_WAS_CREATED;
2418 if (NT_STATUS_EQUAL(status,
2419 NT_STATUS_OBJECT_NAME_COLLISION)) {
2420 info = FILE_WAS_OPENED;
2421 status = NT_STATUS_OK;
2426 case FILE_SUPERSEDE:
2427 case FILE_OVERWRITE:
2428 case FILE_OVERWRITE_IF:
2430 DEBUG(5,("open_directory: invalid create_disposition "
2431 "0x%x for directory %s\n",
2432 (unsigned int)create_disposition, fname));
2433 return NT_STATUS_INVALID_PARAMETER;
2436 if(!S_ISDIR(psbuf->st_mode)) {
2437 DEBUG(5,("open_directory: %s is not a directory !\n",
2439 return NT_STATUS_NOT_A_DIRECTORY;
2442 if (info == FILE_WAS_OPENED) {
2443 uint32_t access_granted = 0;
2444 status = check_open_rights(conn,
2448 if (!NT_STATUS_IS_OK(status)) {
2449 DEBUG(10, ("open_directory: check_open_rights on "
2450 "file %s failed with %s\n",
2452 nt_errstr(status)));
2457 status = file_new(req, conn, &fsp);
2458 if(!NT_STATUS_IS_OK(status)) {
2463 * Setup the files_struct for it.
2466 fsp->mode = psbuf->st_mode;
2467 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2468 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2469 fsp->file_pid = req ? req->smbpid : 0;
2470 fsp->can_lock = False;
2471 fsp->can_read = False;
2472 fsp->can_write = False;
2474 fsp->share_access = share_access;
2475 fsp->fh->private_options = create_options;
2477 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2479 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2480 fsp->print_file = False;
2481 fsp->modified = False;
2482 fsp->oplock_type = NO_OPLOCK;
2483 fsp->sent_oplock_break = NO_BREAK_SENT;
2484 fsp->is_directory = True;
2485 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2487 string_set(&fsp->fsp_name,fname);
2489 mtimespec = get_mtimespec(psbuf);
2491 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2496 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2497 file_free(req, fsp);
2498 return NT_STATUS_SHARING_VIOLATION;
2501 status = open_mode_check(conn, fname, lck,
2502 access_mask, share_access,
2503 create_options, &dir_existed);
2505 if (!NT_STATUS_IS_OK(status)) {
2507 file_free(req, fsp);
2511 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK);
2513 /* For directories the delete on close bit at open time seems
2514 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2515 if (create_options & FILE_DELETE_ON_CLOSE) {
2516 status = can_set_delete_on_close(fsp, True, 0);
2517 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2519 file_free(req, fsp);
2523 if (NT_STATUS_IS_OK(status)) {
2524 /* Note that here we set the *inital* delete on close flag,
2525 not the regular one. The magic gets handled in close. */
2526 fsp->initial_delete_on_close = True;
2537 return NT_STATUS_OK;
2540 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2543 SMB_STRUCT_STAT sbuf;
2546 SET_STAT_INVALID(sbuf);
2548 status = SMB_VFS_CREATE_FILE(
2551 0, /* root_dir_fid */
2552 directory, /* fname */
2553 0, /* create_file_flags */
2554 FILE_READ_ATTRIBUTES, /* access_mask */
2555 FILE_SHARE_NONE, /* share_access */
2556 FILE_CREATE, /* create_disposition*/
2557 FILE_DIRECTORY_FILE, /* create_options */
2558 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
2559 0, /* oplock_request */
2560 0, /* allocation_size */
2567 if (NT_STATUS_IS_OK(status)) {
2568 close_file(req, fsp, NORMAL_CLOSE);
2574 /****************************************************************************
2575 Receive notification that one of our open files has been renamed by another
2577 ****************************************************************************/
2579 void msg_file_was_renamed(struct messaging_context *msg,
2582 struct server_id server_id,
2586 char *frm = (char *)data->data;
2588 const char *sharepath;
2589 const char *newname;
2592 if (data->data == NULL
2593 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2594 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2595 (int)data->length));
2599 /* Unpack the message. */
2600 pull_file_id_16(frm, &id);
2601 sharepath = &frm[16];
2602 newname = sharepath + strlen(sharepath) + 1;
2603 sp_len = strlen(sharepath);
2605 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2607 sharepath, newname, file_id_string_tos(&id)));
2609 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2610 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2611 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2612 fsp->fnum, fsp->fsp_name, newname ));
2613 string_set(&fsp->fsp_name, newname);
2616 /* Now we have the complete path we can work out if this is
2617 actually within this share and adjust newname accordingly. */
2618 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2619 "not sharepath %s) "
2620 "fnum %d from %s -> %s\n",
2621 fsp->conn->connectpath,
2630 struct case_semantics_state {
2631 connection_struct *conn;
2632 bool case_sensitive;
2634 bool short_case_preserve;
2637 /****************************************************************************
2638 Restore case semantics.
2639 ****************************************************************************/
2640 static int restore_case_semantics(struct case_semantics_state *state)
2642 state->conn->case_sensitive = state->case_sensitive;
2643 state->conn->case_preserve = state->case_preserve;
2644 state->conn->short_case_preserve = state->short_case_preserve;
2648 /****************************************************************************
2649 Save case semantics.
2650 ****************************************************************************/
2651 struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2652 connection_struct *conn)
2654 struct case_semantics_state *result;
2656 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2657 DEBUG(0, ("talloc failed\n"));
2661 result->conn = conn;
2662 result->case_sensitive = conn->case_sensitive;
2663 result->case_preserve = conn->case_preserve;
2664 result->short_case_preserve = conn->short_case_preserve;
2667 conn->case_sensitive = True;
2668 conn->case_preserve = True;
2669 conn->short_case_preserve = True;
2671 talloc_set_destructor(result, restore_case_semantics);
2677 * If a main file is opened for delete, all streams need to be checked for
2678 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2679 * If that works, delete them all by setting the delete on close and close.
2682 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2685 struct stream_struct *stream_info;
2686 files_struct **streams;
2688 unsigned int num_streams;
2689 TALLOC_CTX *frame = talloc_stackframe();
2692 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2693 &num_streams, &stream_info);
2695 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2696 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2697 DEBUG(10, ("no streams around\n"));
2699 return NT_STATUS_OK;
2702 if (!NT_STATUS_IS_OK(status)) {
2703 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2704 nt_errstr(status)));
2708 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2711 if (num_streams == 0) {
2713 return NT_STATUS_OK;
2716 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2717 if (streams == NULL) {
2718 DEBUG(0, ("talloc failed\n"));
2719 status = NT_STATUS_NO_MEMORY;
2723 for (i=0; i<num_streams; i++) {
2726 if (strequal(stream_info[i].name, "::$DATA")) {
2731 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2732 stream_info[i].name);
2734 if (streamname == NULL) {
2735 DEBUG(0, ("talloc_aprintf failed\n"));
2736 status = NT_STATUS_NO_MEMORY;
2740 status = create_file_unixpath
2743 streamname, /* fname */
2744 DELETE_ACCESS, /* access_mask */
2745 FILE_SHARE_READ | FILE_SHARE_WRITE
2746 | FILE_SHARE_DELETE, /* share_access */
2747 FILE_OPEN, /* create_disposition*/
2748 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2749 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2750 0, /* oplock_request */
2751 0, /* allocation_size */
2754 &streams[i], /* result */
2758 TALLOC_FREE(streamname);
2760 if (!NT_STATUS_IS_OK(status)) {
2761 DEBUG(10, ("Could not open stream %s: %s\n",
2762 streamname, nt_errstr(status)));
2768 * don't touch the variable "status" beyond this point :-)
2771 for (i -= 1 ; i >= 0; i--) {
2772 if (streams[i] == NULL) {
2776 DEBUG(10, ("Closing stream # %d, %s\n", i,
2777 streams[i]->fsp_name));
2778 close_file(NULL, streams[i], NORMAL_CLOSE);
2787 * Wrapper around open_file_ntcreate and open_directory
2790 static NTSTATUS create_file_unixpath(connection_struct *conn,
2791 struct smb_request *req,
2793 uint32_t access_mask,
2794 uint32_t share_access,
2795 uint32_t create_disposition,
2796 uint32_t create_options,
2797 uint32_t file_attributes,
2798 uint32_t oplock_request,
2799 uint64_t allocation_size,
2800 struct security_descriptor *sd,
2801 struct ea_list *ea_list,
2803 files_struct **result,
2805 SMB_STRUCT_STAT *psbuf)
2807 SMB_STRUCT_STAT sbuf;
2808 int info = FILE_WAS_OPENED;
2809 files_struct *base_fsp = NULL;
2810 files_struct *fsp = NULL;
2813 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2814 "file_attributes = 0x%x, share_access = 0x%x, "
2815 "create_disposition = 0x%x create_options = 0x%x "
2816 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2818 (unsigned int)access_mask,
2819 (unsigned int)file_attributes,
2820 (unsigned int)share_access,
2821 (unsigned int)create_disposition,
2822 (unsigned int)create_options,
2823 (unsigned int)oplock_request,
2824 ea_list, sd, fname));
2826 if (create_options & FILE_OPEN_BY_FILE_ID) {
2827 status = NT_STATUS_NOT_SUPPORTED;
2831 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2832 status = NT_STATUS_INVALID_PARAMETER;
2837 oplock_request |= INTERNAL_OPEN_ONLY;
2840 if (psbuf != NULL) {
2844 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2845 SET_STAT_INVALID(sbuf);
2849 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2850 && (access_mask & DELETE_ACCESS)
2851 && !is_ntfs_stream_name(fname)) {
2853 * We can't open a file with DELETE access if any of the
2854 * streams is open without FILE_SHARE_DELETE
2856 status = open_streams_for_delete(conn, fname);
2858 if (!NT_STATUS_IS_OK(status)) {
2863 /* This is the correct thing to do (check every time) but can_delete
2864 * is expensive (it may have to read the parent directory
2865 * permissions). So for now we're not doing it unless we have a strong
2866 * hint the client is really going to delete this file. If the client
2867 * is forcing FILE_CREATE let the filesystem take care of the
2870 /* Setting FILE_SHARE_DELETE is the hint. */
2872 if (lp_acl_check_permissions(SNUM(conn))
2873 && (create_disposition != FILE_CREATE)
2874 && (share_access & FILE_SHARE_DELETE)
2875 && (access_mask & DELETE_ACCESS)
2876 && (!(can_delete_file_in_directory(conn, fname) ||
2877 can_access_file_acl(conn, fname, DELETE_ACCESS)))) {
2878 status = NT_STATUS_ACCESS_DENIED;
2879 DEBUG(10,("create_file_unixpath: open file %s "
2880 "for delete ACCESS_DENIED\n", fname ));
2885 /* We need to support SeSecurityPrivilege for this. */
2886 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2887 !user_has_privileges(current_user.nt_user_token,
2889 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2894 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2895 && is_ntfs_stream_name(fname)
2896 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2898 uint32 base_create_disposition;
2900 if (create_options & FILE_DIRECTORY_FILE) {
2901 status = NT_STATUS_NOT_A_DIRECTORY;
2905 status = split_ntfs_stream_name(talloc_tos(), fname,
2907 if (!NT_STATUS_IS_OK(status)) {
2908 DEBUG(10, ("create_file_unixpath: "
2909 "split_ntfs_stream_name failed: %s\n",
2910 nt_errstr(status)));
2914 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2916 switch (create_disposition) {
2918 base_create_disposition = FILE_OPEN;
2921 base_create_disposition = FILE_OPEN_IF;
2925 status = create_file_unixpath(conn, NULL, base, 0,
2928 | FILE_SHARE_DELETE,
2929 base_create_disposition,
2930 0, 0, 0, 0, NULL, NULL,
2931 &base_fsp, NULL, NULL);
2932 if (!NT_STATUS_IS_OK(status)) {
2933 DEBUG(10, ("create_file_unixpath for base %s failed: "
2934 "%s\n", base, nt_errstr(status)));
2937 /* we don't need to low level fd */
2942 * If it's a request for a directory open, deal with it separately.
2945 if (create_options & FILE_DIRECTORY_FILE) {
2947 if (create_options & FILE_NON_DIRECTORY_FILE) {
2948 status = NT_STATUS_INVALID_PARAMETER;
2952 /* Can't open a temp directory. IFS kit test. */
2953 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2954 (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) {
2955 status = NT_STATUS_INVALID_PARAMETER;
2960 * We will get a create directory here if the Win32
2961 * app specified a security descriptor in the
2962 * CreateDirectory() call.
2966 status = open_directory(
2967 conn, req, fname, &sbuf, access_mask, share_access,
2968 create_disposition, create_options, file_attributes,
2973 * Ordinary file case.
2976 status = file_new(req, conn, &fsp);
2977 if(!NT_STATUS_IS_OK(status)) {
2982 * We're opening the stream element of a base_fsp
2983 * we already opened. Set up the base_fsp pointer.
2986 fsp->base_fsp = base_fsp;
2989 status = open_file_ntcreate(conn,
3002 if(!NT_STATUS_IS_OK(status)) {
3003 file_free(req, fsp);
3007 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
3009 /* A stream open never opens a directory */
3012 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3017 * Fail the open if it was explicitly a non-directory
3021 if (create_options & FILE_NON_DIRECTORY_FILE) {
3022 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3027 status = open_directory(
3028 conn, req, fname, &sbuf, access_mask,
3029 share_access, create_disposition,
3030 create_options, file_attributes,
3035 if (!NT_STATUS_IS_OK(status)) {
3039 fsp->base_fsp = base_fsp;
3042 * According to the MS documentation, the only time the security
3043 * descriptor is applied to the opened file is iff we *created* the
3044 * file; an existing file stays the same.
3046 * Also, it seems (from observation) that you can open the file with
3047 * any access mask but you can still write the sd. We need to override
3048 * the granted access before we call set_sd
3049 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
3052 if ((sd != NULL) && (info == FILE_WAS_CREATED)
3053 && lp_nt_acl_support(SNUM(conn))) {
3055 uint32_t sec_info_sent;
3056 uint32_t saved_access_mask = fsp->access_mask;
3058 sec_info_sent = get_sec_info(sd);
3060 fsp->access_mask = FILE_GENERIC_ALL;
3062 /* Convert all the generic bits. */
3063 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3064 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3066 if (sec_info_sent & (OWNER_SECURITY_INFORMATION|
3067 GROUP_SECURITY_INFORMATION|
3068 DACL_SECURITY_INFORMATION|
3069 SACL_SECURITY_INFORMATION)) {
3070 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3073 fsp->access_mask = saved_access_mask;
3075 if (!NT_STATUS_IS_OK(status)) {
3080 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
3081 status = set_ea(conn, fsp, fname, ea_list);
3082 if (!NT_STATUS_IS_OK(status)) {
3087 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
3088 status = NT_STATUS_ACCESS_DENIED;
3092 /* Save the requested allocation size. */
3093 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3095 && (allocation_size > sbuf.st_size)) {
3096 fsp->initial_allocation_size = smb_roundup(
3097 fsp->conn, allocation_size);
3098 if (fsp->is_directory) {
3099 /* Can't set allocation size on a directory. */
3100 status = NT_STATUS_ACCESS_DENIED;
3103 if (vfs_allocate_file_space(
3104 fsp, fsp->initial_allocation_size) == -1) {
3105 status = NT_STATUS_DISK_FULL;
3109 fsp->initial_allocation_size = smb_roundup(
3110 fsp->conn, (uint64_t)sbuf.st_size);
3114 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3117 if (pinfo != NULL) {
3120 if (psbuf != NULL) {
3121 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
3125 SMB_VFS_FSTAT(fsp, psbuf);
3128 return NT_STATUS_OK;
3131 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3134 if (base_fsp && fsp->base_fsp == base_fsp) {
3136 * The close_file below will close
3141 close_file(req, fsp, ERROR_CLOSE);
3144 if (base_fsp != NULL) {
3145 close_file(req, base_fsp, ERROR_CLOSE);
3152 * Calculate the full path name given a relative fid.
3154 NTSTATUS get_relative_fid_filename(connection_struct *conn,
3155 struct smb_request *req,
3156 uint16_t root_dir_fid,
3157 const char *fname, char **new_fname)
3159 files_struct *dir_fsp;
3160 char *parent_fname = NULL;
3162 if (root_dir_fid == 0 || !fname || !new_fname) {
3163 return NT_STATUS_INTERNAL_ERROR;
3166 dir_fsp = file_fsp(req, root_dir_fid);
3168 if (dir_fsp == NULL) {
3169 return NT_STATUS_INVALID_HANDLE;
3172 if (!dir_fsp->is_directory) {
3175 * Check to see if this is a mac fork of some kind.
3178 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3179 is_ntfs_stream_name(fname)) {
3180 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
3184 we need to handle the case when we get a
3185 relative open relative to a file and the
3186 pathname is blank - this is a reopen!
3187 (hint from demyn plantenberg)
3190 return NT_STATUS_INVALID_HANDLE;
3193 if (ISDOT(dir_fsp->fsp_name)) {
3195 * We're at the toplevel dir, the final file name
3196 * must not contain ./, as this is filtered out
3197 * normally by srvstr_get_path and unix_convert
3198 * explicitly rejects paths containing ./.
3200 parent_fname = talloc_strdup(talloc_tos(), "");
3201 if (parent_fname == NULL) {
3202 return NT_STATUS_NO_MEMORY;
3205 size_t dir_name_len = strlen(dir_fsp->fsp_name);
3208 * Copy in the base directory name.
3211 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3213 if (parent_fname == NULL) {
3214 return NT_STATUS_NO_MEMORY;
3216 memcpy(parent_fname, dir_fsp->fsp_name,
3220 * Ensure it ends in a '/'.
3221 * We used TALLOC_SIZE +2 to add space for the '/'.
3225 && (parent_fname[dir_name_len-1] != '\\')
3226 && (parent_fname[dir_name_len-1] != '/')) {
3227 parent_fname[dir_name_len] = '/';
3228 parent_fname[dir_name_len+1] = '\0';
3232 *new_fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3234 if (*new_fname == NULL) {
3235 return NT_STATUS_NO_MEMORY;
3238 return NT_STATUS_OK;
3241 NTSTATUS create_file_default(connection_struct *conn,
3242 struct smb_request *req,
3243 uint16_t root_dir_fid,
3245 uint32_t create_file_flags,
3246 uint32_t access_mask,
3247 uint32_t share_access,
3248 uint32_t create_disposition,
3249 uint32_t create_options,
3250 uint32_t file_attributes,
3251 uint32_t oplock_request,
3252 uint64_t allocation_size,
3253 struct security_descriptor *sd,
3254 struct ea_list *ea_list,
3256 files_struct **result,
3258 SMB_STRUCT_STAT *psbuf)
3260 struct case_semantics_state *case_state = NULL;
3261 SMB_STRUCT_STAT sbuf;
3262 int info = FILE_WAS_OPENED;
3263 files_struct *fsp = NULL;
3266 DEBUG(10,("create_file: access_mask = 0x%x "
3267 "file_attributes = 0x%x, share_access = 0x%x, "
3268 "create_disposition = 0x%x create_options = 0x%x "
3269 "oplock_request = 0x%x "
3270 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3271 "create_file_flags = 0x%x, fname = %s\n",
3272 (unsigned int)access_mask,
3273 (unsigned int)file_attributes,
3274 (unsigned int)share_access,
3275 (unsigned int)create_disposition,
3276 (unsigned int)create_options,
3277 (unsigned int)oplock_request,
3278 (unsigned int)root_dir_fid,
3279 ea_list, sd, create_file_flags, fname));
3282 * Calculate the filename from the root_dir_if if necessary.
3285 if (root_dir_fid != 0) {
3288 status = get_relative_fid_filename(conn, req, root_dir_fid,
3290 if (!NT_STATUS_IS_OK(status)) {
3298 * Check to see if this is a mac fork of some kind.
3301 if (is_ntfs_stream_name(fname)) {
3302 enum FAKE_FILE_TYPE fake_file_type;
3304 fake_file_type = is_fake_file(fname);
3306 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3309 * Here we go! support for changing the disk quotas
3312 * We need to fake up to open this MAGIC QUOTA file
3313 * and return a valid FID.
3315 * w2k close this file directly after openening xp
3316 * also tries a QUERY_FILE_INFO on the file and then
3319 status = open_fake_file(req, conn, req->vuid,
3320 fake_file_type, fname,
3322 if (!NT_STATUS_IS_OK(status)) {
3330 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3331 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3336 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3337 char *resolved_fname;
3339 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3342 if (!NT_STATUS_IS_OK(status)) {
3344 * For PATH_NOT_COVERED we had
3345 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3346 * ERRSRV, ERRbadpath);
3347 * Need to fix in callers
3351 fname = resolved_fname;
3355 * Check if POSIX semantics are wanted.
3358 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3359 case_state = set_posix_case_semantics(talloc_tos(), conn);
3362 if (create_file_flags & CFF_DOS_PATH) {
3363 char *converted_fname;
3365 SET_STAT_INVALID(sbuf);
3367 status = unix_convert(talloc_tos(), conn, fname, False,
3368 &converted_fname, NULL, &sbuf);
3369 if (!NT_STATUS_IS_OK(status)) {
3372 fname = converted_fname;
3374 if (psbuf != NULL) {
3377 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
3378 SET_STAT_INVALID(sbuf);
3384 TALLOC_FREE(case_state);
3386 /* All file access must go through check_name() */
3388 status = check_name(conn, fname);
3389 if (!NT_STATUS_IS_OK(status)) {
3393 status = create_file_unixpath(
3394 conn, req, fname, access_mask, share_access,
3395 create_disposition, create_options, file_attributes,
3396 oplock_request, allocation_size, sd, ea_list,
3397 &fsp, &info, &sbuf);
3399 if (!NT_STATUS_IS_OK(status)) {
3404 DEBUG(10, ("create_file: info=%d\n", info));
3407 if (pinfo != NULL) {
3410 if (psbuf != NULL) {
3413 return NT_STATUS_OK;
3416 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3419 close_file(req, fsp, ERROR_CLOSE);