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/>.
24 extern const struct generic_mapping file_generic_mapping;
25 extern bool global_client_failed_oplock_break;
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,
72 /* Check if we have rights to open. */
74 uint32_t access_granted = 0;
75 struct security_descriptor *sd;
77 status = SMB_VFS_GET_NT_ACL(conn, fname,
78 (OWNER_SECURITY_INFORMATION |
79 GROUP_SECURITY_INFORMATION |
80 DACL_SECURITY_INFORMATION),&sd);
82 if (!NT_STATUS_IS_OK(status)) {
83 DEBUG(10, ("check_open_rights: Could not get acl "
90 status = smb1_file_se_access_check(sd,
91 conn->server_info->ptok,
99 /****************************************************************************
100 fd support routines - attempt to do a dos_open.
101 ****************************************************************************/
103 static NTSTATUS fd_open(struct connection_struct *conn,
109 NTSTATUS status = NT_STATUS_OK;
113 * Never follow symlinks on a POSIX client. The
114 * client should be doing this.
117 if (fsp->posix_open || !lp_symlinks(SNUM(conn))) {
122 fsp->fh->fd = SMB_VFS_OPEN(conn,fname,fsp,flags,mode);
123 if (fsp->fh->fd == -1) {
124 status = map_nt_error_from_unix(errno);
127 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n",
128 fname, flags, (int)mode, fsp->fh->fd,
129 (fsp->fh->fd == -1) ? strerror(errno) : "" ));
134 /****************************************************************************
135 Close the file associated with a fsp.
136 ****************************************************************************/
138 NTSTATUS fd_close(files_struct *fsp)
142 if (fsp->fh->fd == -1) {
143 return NT_STATUS_OK; /* What we used to call a stat open. */
145 if (fsp->fh->ref_count > 1) {
146 return NT_STATUS_OK; /* Shared handle. Only close last reference. */
149 ret = SMB_VFS_CLOSE(fsp);
152 return map_nt_error_from_unix(errno);
157 /****************************************************************************
158 Change the ownership of a file to that of the parent directory.
159 Do this by fd if possible.
160 ****************************************************************************/
162 static void change_file_owner_to_parent(connection_struct *conn,
163 const char *inherit_from_dir,
166 SMB_STRUCT_STAT parent_st;
169 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
171 DEBUG(0,("change_file_owner_to_parent: failed to stat parent "
172 "directory %s. Error was %s\n",
173 inherit_from_dir, strerror(errno) ));
178 ret = SMB_VFS_FCHOWN(fsp, parent_st.st_uid, (gid_t)-1);
181 DEBUG(0,("change_file_owner_to_parent: failed to fchown "
182 "file %s to parent directory uid %u. Error "
183 "was %s\n", fsp->fsp_name,
184 (unsigned int)parent_st.st_uid,
188 DEBUG(10,("change_file_owner_to_parent: changed new file %s to "
189 "parent directory uid %u.\n", fsp->fsp_name,
190 (unsigned int)parent_st.st_uid ));
193 static NTSTATUS change_dir_owner_to_parent(connection_struct *conn,
194 const char *inherit_from_dir,
196 SMB_STRUCT_STAT *psbuf)
198 char *saved_dir = NULL;
199 SMB_STRUCT_STAT sbuf;
200 SMB_STRUCT_STAT parent_st;
201 TALLOC_CTX *ctx = talloc_tos();
202 NTSTATUS status = NT_STATUS_OK;
205 ret = SMB_VFS_STAT(conn, inherit_from_dir, &parent_st);
207 status = map_nt_error_from_unix(errno);
208 DEBUG(0,("change_dir_owner_to_parent: failed to stat parent "
209 "directory %s. Error was %s\n",
210 inherit_from_dir, strerror(errno) ));
214 /* We've already done an lstat into psbuf, and we know it's a
215 directory. If we can cd into the directory and the dev/ino
216 are the same then we can safely chown without races as
217 we're locking the directory in place by being in it. This
218 should work on any UNIX (thanks tridge :-). JRA.
221 saved_dir = vfs_GetWd(ctx,conn);
223 status = map_nt_error_from_unix(errno);
224 DEBUG(0,("change_dir_owner_to_parent: failed to get "
225 "current working directory. Error was %s\n",
230 /* Chdir into the new path. */
231 if (vfs_ChDir(conn, fname) == -1) {
232 status = map_nt_error_from_unix(errno);
233 DEBUG(0,("change_dir_owner_to_parent: failed to change "
234 "current working directory to %s. Error "
235 "was %s\n", fname, strerror(errno) ));
239 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
240 status = map_nt_error_from_unix(errno);
241 DEBUG(0,("change_dir_owner_to_parent: failed to stat "
242 "directory '.' (%s) Error was %s\n",
243 fname, strerror(errno)));
247 /* Ensure we're pointing at the same place. */
248 if (sbuf.st_dev != psbuf->st_dev ||
249 sbuf.st_ino != psbuf->st_ino ||
250 sbuf.st_mode != psbuf->st_mode ) {
251 DEBUG(0,("change_dir_owner_to_parent: "
252 "device/inode/mode on directory %s changed. "
253 "Refusing to chown !\n", fname ));
254 status = NT_STATUS_ACCESS_DENIED;
259 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
262 status = map_nt_error_from_unix(errno);
263 DEBUG(10,("change_dir_owner_to_parent: failed to chown "
264 "directory %s to parent directory uid %u. "
265 "Error was %s\n", fname,
266 (unsigned int)parent_st.st_uid, strerror(errno) ));
270 DEBUG(10,("change_dir_owner_to_parent: changed ownership of new "
271 "directory %s to parent directory uid %u.\n",
272 fname, (unsigned int)parent_st.st_uid ));
276 vfs_ChDir(conn,saved_dir);
280 /****************************************************************************
282 ****************************************************************************/
284 static NTSTATUS open_file(files_struct *fsp,
285 connection_struct *conn,
286 struct smb_request *req,
287 const char *parent_dir,
290 SMB_STRUCT_STAT *psbuf,
293 uint32 access_mask, /* client requested access mask. */
294 uint32 open_access_mask) /* what we're actually using in the open. */
296 NTSTATUS status = NT_STATUS_OK;
297 int accmode = (flags & O_ACCMODE);
298 int local_flags = flags;
299 bool file_existed = VALID_STAT(*psbuf);
304 /* Check permissions */
307 * This code was changed after seeing a client open request
308 * containing the open mode of (DENY_WRITE/read-only) with
309 * the 'create if not exist' bit set. The previous code
310 * would fail to open the file read only on a read-only share
311 * as it was checking the flags parameter directly against O_RDONLY,
312 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
316 if (!CAN_WRITE(conn)) {
317 /* It's a read-only share - fail if we wanted to write. */
318 if(accmode != O_RDONLY) {
319 DEBUG(3,("Permission denied opening %s\n", path));
320 return NT_STATUS_ACCESS_DENIED;
321 } else if(flags & O_CREAT) {
322 /* We don't want to write - but we must make sure that
323 O_CREAT doesn't create the file if we have write
324 access into the directory.
327 local_flags &= ~O_CREAT;
332 * This little piece of insanity is inspired by the
333 * fact that an NT client can open a file for O_RDONLY,
334 * but set the create disposition to FILE_EXISTS_TRUNCATE.
335 * If the client *can* write to the file, then it expects to
336 * truncate the file, even though it is opening for readonly.
337 * Quicken uses this stupid trick in backup file creation...
338 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
339 * for helping track this one down. It didn't bite us in 2.0.x
340 * as we always opened files read-write in that release. JRA.
343 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
344 DEBUG(10,("open_file: truncate requested on read-only open "
345 "for file %s\n", path));
346 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
349 if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
350 (!file_existed && (local_flags & O_CREAT)) ||
351 ((local_flags & O_TRUNC) == O_TRUNC) ) {
355 * We can't actually truncate here as the file may be locked.
356 * open_file_ntcreate will take care of the truncate later. JRA.
359 local_flags &= ~O_TRUNC;
361 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
363 * We would block on opening a FIFO with no one else on the
364 * other end. Do what we used to do and add O_NONBLOCK to the
368 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
369 local_flags |= O_NONBLOCK;
373 /* Don't create files with Microsoft wildcard characters. */
376 * wildcard characters are allowed in stream names
377 * only test the basefilename
379 wild = fsp->base_fsp->fsp_name;
383 if ((local_flags & O_CREAT) && !file_existed &&
385 return NT_STATUS_OBJECT_NAME_INVALID;
388 /* Actually do the open */
389 status = fd_open(conn, path, fsp, local_flags, unx_mode);
390 if (!NT_STATUS_IS_OK(status)) {
391 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
393 path,nt_errstr(status),local_flags,flags));
397 if ((local_flags & O_CREAT) && !file_existed) {
399 /* Inherit the ACL if required */
400 if (lp_inherit_perms(SNUM(conn))) {
401 inherit_access_posix_acl(conn, parent_dir, path,
405 /* Change the owner if required. */
406 if (lp_inherit_owner(SNUM(conn))) {
407 change_file_owner_to_parent(conn, parent_dir,
411 notify_fname(conn, NOTIFY_ACTION_ADDED,
412 FILE_NOTIFY_CHANGE_FILE_NAME, path);
416 fsp->fh->fd = -1; /* What we used to call a stat open. */
418 status = check_open_rights(conn,
421 if (!NT_STATUS_IS_OK(status)) {
422 DEBUG(10, ("open_file: Access denied on "
433 if (fsp->fh->fd == -1) {
434 ret = SMB_VFS_STAT(conn, path, psbuf);
436 ret = SMB_VFS_FSTAT(fsp, psbuf);
437 /* If we have an fd, this stat should succeed. */
439 DEBUG(0,("Error doing fstat on open file %s "
440 "(%s)\n", path,strerror(errno) ));
444 /* For a non-io open, this stat failing means file not found. JRA */
446 status = map_nt_error_from_unix(errno);
453 * POSIX allows read-only opens of directories. We don't
454 * want to do this (we use a different code path for this)
455 * so catch a directory open and return an EISDIR. JRA.
458 if(S_ISDIR(psbuf->st_mode)) {
461 return NT_STATUS_FILE_IS_A_DIRECTORY;
464 fsp->mode = psbuf->st_mode;
465 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
466 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
467 fsp->file_pid = req ? req->smbpid : 0;
468 fsp->can_lock = True;
469 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
470 if (!CAN_WRITE(conn)) {
471 fsp->can_write = False;
473 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
476 fsp->print_file = False;
477 fsp->modified = False;
478 fsp->sent_oplock_break = NO_BREAK_SENT;
479 fsp->is_directory = False;
480 if (conn->aio_write_behind_list &&
481 is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
482 fsp->aio_write_behind = True;
485 string_set(&fsp->fsp_name, path);
486 fsp->wcp = NULL; /* Write cache pointer. */
488 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
489 conn->server_info->unix_name,
491 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
492 conn->num_files_open));
498 /*******************************************************************
499 Return True if the filename is one of the special executable types.
500 ********************************************************************/
502 static bool is_executable(const char *fname)
504 if ((fname = strrchr_m(fname,'.'))) {
505 if (strequal(fname,".com") ||
506 strequal(fname,".dll") ||
507 strequal(fname,".exe") ||
508 strequal(fname,".sym")) {
515 /****************************************************************************
516 Check if we can open a file with a share mode.
517 Returns True if conflict, False if not.
518 ****************************************************************************/
520 static bool share_conflict(struct share_mode_entry *entry,
524 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
525 "entry->share_access = 0x%x, "
526 "entry->private_options = 0x%x\n",
527 (unsigned int)entry->access_mask,
528 (unsigned int)entry->share_access,
529 (unsigned int)entry->private_options));
531 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
532 (unsigned int)access_mask, (unsigned int)share_access));
534 if ((entry->access_mask & (FILE_WRITE_DATA|
538 DELETE_ACCESS)) == 0) {
539 DEBUG(10,("share_conflict: No conflict due to "
540 "entry->access_mask = 0x%x\n",
541 (unsigned int)entry->access_mask ));
545 if ((access_mask & (FILE_WRITE_DATA|
549 DELETE_ACCESS)) == 0) {
550 DEBUG(10,("share_conflict: No conflict due to "
551 "access_mask = 0x%x\n",
552 (unsigned int)access_mask ));
556 #if 1 /* JRA TEST - Superdebug. */
557 #define CHECK_MASK(num, am, right, sa, share) \
558 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
559 (unsigned int)(num), (unsigned int)(am), \
560 (unsigned int)(right), (unsigned int)(am)&(right) )); \
561 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
562 (unsigned int)(num), (unsigned int)(sa), \
563 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
564 if (((am) & (right)) && !((sa) & (share))) { \
565 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
566 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
567 (unsigned int)(share) )); \
571 #define CHECK_MASK(num, am, right, sa, share) \
572 if (((am) & (right)) && !((sa) & (share))) { \
573 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
574 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
575 (unsigned int)(share) )); \
580 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
581 share_access, FILE_SHARE_WRITE);
582 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
583 entry->share_access, FILE_SHARE_WRITE);
585 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
586 share_access, FILE_SHARE_READ);
587 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
588 entry->share_access, FILE_SHARE_READ);
590 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
591 share_access, FILE_SHARE_DELETE);
592 CHECK_MASK(6, access_mask, DELETE_ACCESS,
593 entry->share_access, FILE_SHARE_DELETE);
595 DEBUG(10,("share_conflict: No conflict.\n"));
599 #if defined(DEVELOPER)
600 static void validate_my_share_entries(int num,
601 struct share_mode_entry *share_entry)
605 if (!procid_is_me(&share_entry->pid)) {
609 if (is_deferred_open_entry(share_entry) &&
610 !open_was_deferred(share_entry->op_mid)) {
611 char *str = talloc_asprintf(talloc_tos(),
612 "Got a deferred entry without a request: "
614 share_mode_str(talloc_tos(), num, share_entry));
618 if (!is_valid_share_mode_entry(share_entry)) {
622 fsp = file_find_dif(share_entry->id,
623 share_entry->share_file_id);
625 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
626 share_mode_str(talloc_tos(), num, share_entry) ));
627 smb_panic("validate_my_share_entries: Cannot match a "
628 "share entry with an open file\n");
631 if (is_deferred_open_entry(share_entry) ||
632 is_unused_share_mode_entry(share_entry)) {
636 if ((share_entry->op_type == NO_OPLOCK) &&
637 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
638 /* Someone has already written to it, but I haven't yet
643 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
652 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
653 share_mode_str(talloc_tos(), num, share_entry) ));
654 str = talloc_asprintf(talloc_tos(),
655 "validate_my_share_entries: "
656 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
657 fsp->fsp_name, (unsigned int)fsp->oplock_type,
658 (unsigned int)share_entry->op_type );
664 static bool is_stat_open(uint32 access_mask)
666 return (access_mask &&
667 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
668 FILE_WRITE_ATTRIBUTES))==0) &&
669 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
670 FILE_WRITE_ATTRIBUTES)) != 0));
673 /****************************************************************************
674 Deal with share modes
675 Invarient: Share mode must be locked on entry and exit.
676 Returns -1 on error, or number of share modes on success (may be zero).
677 ****************************************************************************/
679 static NTSTATUS open_mode_check(connection_struct *conn,
681 struct share_mode_lock *lck,
684 uint32 create_options,
689 if(lck->num_share_modes == 0) {
693 *file_existed = True;
695 /* A delete on close prohibits everything */
697 if (lck->delete_on_close) {
698 return NT_STATUS_DELETE_PENDING;
701 if (is_stat_open(access_mask)) {
702 /* Stat open that doesn't trigger oplock breaks or share mode
703 * checks... ! JRA. */
708 * Check if the share modes will give us access.
711 #if defined(DEVELOPER)
712 for(i = 0; i < lck->num_share_modes; i++) {
713 validate_my_share_entries(i, &lck->share_modes[i]);
717 if (!lp_share_modes(SNUM(conn))) {
721 /* Now we check the share modes, after any oplock breaks. */
722 for(i = 0; i < lck->num_share_modes; i++) {
724 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
728 /* someone else has a share lock on it, check to see if we can
730 if (share_conflict(&lck->share_modes[i],
731 access_mask, share_access)) {
732 return NT_STATUS_SHARING_VIOLATION;
739 static bool is_delete_request(files_struct *fsp) {
740 return ((fsp->access_mask == DELETE_ACCESS) &&
741 (fsp->oplock_type == NO_OPLOCK));
745 * 1) No files open at all or internal open: Grant whatever the client wants.
747 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
748 * request, break if the oplock around is a batch oplock. If it's another
749 * requested access type, break.
751 * 3) Only level2 around: Grant level2 and do nothing else.
754 static bool delay_for_oplocks(struct share_mode_lock *lck,
761 struct share_mode_entry *exclusive = NULL;
762 bool valid_entry = False;
763 bool delay_it = False;
764 bool have_level2 = False;
766 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
768 if (oplock_request & INTERNAL_OPEN_ONLY) {
769 fsp->oplock_type = NO_OPLOCK;
772 if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) {
776 for (i=0; i<lck->num_share_modes; i++) {
778 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
782 /* At least one entry is not an invalid or deferred entry. */
785 if (pass_number == 1) {
786 if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
787 SMB_ASSERT(exclusive == NULL);
788 exclusive = &lck->share_modes[i];
791 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
792 SMB_ASSERT(exclusive == NULL);
793 exclusive = &lck->share_modes[i];
797 if (lck->share_modes[i].op_type == LEVEL_II_OPLOCK) {
798 SMB_ASSERT(exclusive == NULL);
804 /* All entries are placeholders or deferred.
805 * Directly grant whatever the client wants. */
806 if (fsp->oplock_type == NO_OPLOCK) {
807 /* Store a level2 oplock, but don't tell the client */
808 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
813 if (exclusive != NULL) { /* Found an exclusive oplock */
814 SMB_ASSERT(!have_level2);
815 delay_it = is_delete_request(fsp) ?
816 BATCH_OPLOCK_TYPE(exclusive->op_type) : True;
819 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
820 /* We can at most grant level2 as there are other
821 * level2 or NO_OPLOCK entries. */
822 fsp->oplock_type = LEVEL_II_OPLOCK;
825 if ((fsp->oplock_type == NO_OPLOCK) && have_level2) {
826 /* Store a level2 oplock, but don't tell the client */
827 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
835 * Send a break message to the oplock holder and delay the open for
839 DEBUG(10, ("Sending break request to PID %s\n",
840 procid_str_static(&exclusive->pid)));
841 exclusive->op_mid = mid;
843 /* Create the message. */
844 share_mode_entry_to_message(msg, exclusive);
846 /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We
847 don't want this set in the share mode struct pointed to by lck. */
849 if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) {
850 SSVAL(msg,6,exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE);
853 status = messaging_send_buf(smbd_messaging_context(), exclusive->pid,
854 MSG_SMB_BREAK_REQUEST,
856 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
857 if (!NT_STATUS_IS_OK(status)) {
858 DEBUG(3, ("Could not send oplock break message: %s\n",
865 static bool request_timed_out(struct timeval request_time,
866 struct timeval timeout)
868 struct timeval now, end_time;
870 end_time = timeval_sum(&request_time, &timeout);
871 return (timeval_compare(&end_time, &now) < 0);
874 /****************************************************************************
875 Handle the 1 second delay in returning a SHARING_VIOLATION error.
876 ****************************************************************************/
878 static void defer_open(struct share_mode_lock *lck,
879 struct timeval request_time,
880 struct timeval timeout,
881 struct smb_request *req,
882 struct deferred_open_record *state)
888 for (i=0; i<lck->num_share_modes; i++) {
889 struct share_mode_entry *e = &lck->share_modes[i];
891 if (!is_deferred_open_entry(e)) {
895 if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
896 DEBUG(0, ("Trying to defer an already deferred "
897 "request: mid=%d, exiting\n", req->mid));
898 exit_server("attempt to defer a deferred request");
902 /* End paranoia check */
904 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
905 "open entry for mid %u\n",
906 (unsigned int)request_time.tv_sec,
907 (unsigned int)request_time.tv_usec,
908 (unsigned int)req->mid));
910 if (!push_deferred_smb_message(req, request_time, timeout,
911 (char *)state, sizeof(*state))) {
912 exit_server("push_deferred_smb_message failed");
914 add_deferred_open(lck, req->mid, request_time, state->id);
917 * Push the MID of this packet on the signing queue.
918 * We only do this once, the first time we push the packet
919 * onto the deferred open queue, as this has a side effect
920 * of incrementing the response sequence number.
923 srv_defer_sign_response(req->mid);
927 /****************************************************************************
928 On overwrite open ensure that the attributes match.
929 ****************************************************************************/
931 static bool open_match_attributes(connection_struct *conn,
935 mode_t existing_unx_mode,
937 mode_t *returned_unx_mode)
939 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
941 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
942 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
944 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
945 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
946 *returned_unx_mode = new_unx_mode;
948 *returned_unx_mode = (mode_t)0;
951 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
952 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
953 "returned_unx_mode = 0%o\n",
955 (unsigned int)old_dos_attr,
956 (unsigned int)existing_unx_mode,
957 (unsigned int)new_dos_attr,
958 (unsigned int)*returned_unx_mode ));
960 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
961 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
962 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
963 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
967 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
968 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
969 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
976 /****************************************************************************
977 Special FCB or DOS processing in the case of a sharing violation.
978 Try and find a duplicated file handle.
979 ****************************************************************************/
981 static NTSTATUS fcb_or_dos_open(struct smb_request *req,
982 connection_struct *conn,
983 files_struct *fsp_to_dup_into,
990 uint32 create_options)
994 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
995 "file %s.\n", fname ));
997 for(fsp = file_find_di_first(id); fsp;
998 fsp = file_find_di_next(fsp)) {
1000 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
1001 "vuid = %u, file_pid = %u, private_options = 0x%x "
1002 "access_mask = 0x%x\n", fsp->fsp_name,
1003 fsp->fh->fd, (unsigned int)fsp->vuid,
1004 (unsigned int)fsp->file_pid,
1005 (unsigned int)fsp->fh->private_options,
1006 (unsigned int)fsp->access_mask ));
1008 if (fsp->fh->fd != -1 &&
1009 fsp->vuid == vuid &&
1010 fsp->file_pid == file_pid &&
1011 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
1012 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
1013 (fsp->access_mask & FILE_WRITE_DATA) &&
1014 strequal(fsp->fsp_name, fname)) {
1015 DEBUG(10,("fcb_or_dos_open: file match\n"));
1021 return NT_STATUS_NOT_FOUND;
1024 /* quite an insane set of semantics ... */
1025 if (is_executable(fname) &&
1026 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
1027 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
1028 return NT_STATUS_INVALID_PARAMETER;
1031 /* We need to duplicate this fsp. */
1032 dup_file_fsp(req, fsp, access_mask, share_access,
1033 create_options, fsp_to_dup_into);
1035 return NT_STATUS_OK;
1038 /****************************************************************************
1039 Open a file with a share mode - old openX method - map into NTCreate.
1040 ****************************************************************************/
1042 bool map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
1043 uint32 *paccess_mask,
1044 uint32 *pshare_mode,
1045 uint32 *pcreate_disposition,
1046 uint32 *pcreate_options)
1050 uint32 create_disposition;
1051 uint32 create_options = 0;
1053 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
1054 "open_func = 0x%x\n",
1055 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
1057 /* Create the NT compatible access_mask. */
1058 switch (GET_OPENX_MODE(deny_mode)) {
1059 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
1060 case DOS_OPEN_RDONLY:
1061 access_mask = FILE_GENERIC_READ;
1063 case DOS_OPEN_WRONLY:
1064 access_mask = FILE_GENERIC_WRITE;
1068 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
1071 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
1072 (unsigned int)GET_OPENX_MODE(deny_mode)));
1076 /* Create the NT compatible create_disposition. */
1077 switch (open_func) {
1078 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
1079 create_disposition = FILE_CREATE;
1082 case OPENX_FILE_EXISTS_OPEN:
1083 create_disposition = FILE_OPEN;
1086 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
1087 create_disposition = FILE_OPEN_IF;
1090 case OPENX_FILE_EXISTS_TRUNCATE:
1091 create_disposition = FILE_OVERWRITE;
1094 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
1095 create_disposition = FILE_OVERWRITE_IF;
1099 /* From samba4 - to be confirmed. */
1100 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
1101 create_disposition = FILE_CREATE;
1104 DEBUG(10,("map_open_params_to_ntcreate: bad "
1105 "open_func 0x%x\n", (unsigned int)open_func));
1109 /* Create the NT compatible share modes. */
1110 switch (GET_DENY_MODE(deny_mode)) {
1112 share_mode = FILE_SHARE_NONE;
1116 share_mode = FILE_SHARE_READ;
1120 share_mode = FILE_SHARE_WRITE;
1124 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1128 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
1129 if (is_executable(fname)) {
1130 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1132 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
1133 share_mode = FILE_SHARE_READ;
1135 share_mode = FILE_SHARE_NONE;
1141 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
1142 share_mode = FILE_SHARE_NONE;
1146 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
1147 (unsigned int)GET_DENY_MODE(deny_mode) ));
1151 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
1152 "share_mode = 0x%x, create_disposition = 0x%x, "
1153 "create_options = 0x%x\n",
1155 (unsigned int)access_mask,
1156 (unsigned int)share_mode,
1157 (unsigned int)create_disposition,
1158 (unsigned int)create_options ));
1161 *paccess_mask = access_mask;
1164 *pshare_mode = share_mode;
1166 if (pcreate_disposition) {
1167 *pcreate_disposition = create_disposition;
1169 if (pcreate_options) {
1170 *pcreate_options = create_options;
1177 static void schedule_defer_open(struct share_mode_lock *lck,
1178 struct timeval request_time,
1179 struct smb_request *req)
1181 struct deferred_open_record state;
1183 /* This is a relative time, added to the absolute
1184 request_time value to get the absolute timeout time.
1185 Note that if this is the second or greater time we enter
1186 this codepath for this particular request mid then
1187 request_time is left as the absolute time of the *first*
1188 time this request mid was processed. This is what allows
1189 the request to eventually time out. */
1191 struct timeval timeout;
1193 /* Normally the smbd we asked should respond within
1194 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1195 * the client did, give twice the timeout as a safety
1196 * measure here in case the other smbd is stuck
1197 * somewhere else. */
1199 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1201 /* Nothing actually uses state.delayed_for_oplocks
1202 but it's handy to differentiate in debug messages
1203 between a 30 second delay due to oplock break, and
1204 a 1 second delay for share mode conflicts. */
1206 state.delayed_for_oplocks = True;
1209 if (!request_timed_out(request_time, timeout)) {
1210 defer_open(lck, request_time, timeout, req, &state);
1214 /****************************************************************************
1215 Work out what access_mask to use from what the client sent us.
1216 ****************************************************************************/
1218 static NTSTATUS calculate_access_mask(connection_struct *conn,
1221 uint32_t access_mask,
1222 uint32_t *access_mask_out)
1227 * Convert GENERIC bits to specific bits.
1230 se_map_generic(&access_mask, &file_generic_mapping);
1232 /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */
1233 if (access_mask & MAXIMUM_ALLOWED_ACCESS) {
1236 struct security_descriptor *sd;
1237 uint32_t access_granted = 0;
1239 status = SMB_VFS_GET_NT_ACL(conn, fname,
1240 (OWNER_SECURITY_INFORMATION |
1241 GROUP_SECURITY_INFORMATION |
1242 DACL_SECURITY_INFORMATION),&sd);
1244 if (!NT_STATUS_IS_OK(status)) {
1245 DEBUG(10, ("calculate_access_mask: Could not get acl "
1248 nt_errstr(status)));
1249 return NT_STATUS_ACCESS_DENIED;
1252 status = smb1_file_se_access_check(sd,
1253 conn->server_info->ptok,
1259 if (!NT_STATUS_IS_OK(status)) {
1260 DEBUG(10, ("calculate_access_mask: Access denied on "
1261 "file %s: when calculating maximum access\n",
1263 return NT_STATUS_ACCESS_DENIED;
1266 access_mask = access_granted;
1268 access_mask = FILE_GENERIC_ALL;
1272 *access_mask_out = access_mask;
1273 return NT_STATUS_OK;
1276 /****************************************************************************
1277 Open a file with a share mode. Passed in an already created files_struct *.
1278 ****************************************************************************/
1280 static NTSTATUS open_file_ntcreate_internal(connection_struct *conn,
1281 struct smb_request *req,
1283 SMB_STRUCT_STAT *psbuf,
1284 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1285 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
1286 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1287 uint32 create_options, /* options such as delete on close. */
1288 uint32 new_dos_attributes, /* attributes used for new file. */
1289 int oplock_request, /* internal Samba oplock codes. */
1290 /* Information (FILE_EXISTS etc.) */
1296 bool file_existed = VALID_STAT(*psbuf);
1297 bool def_acl = False;
1298 bool posix_open = False;
1299 bool new_file_created = False;
1301 NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED;
1302 mode_t new_unx_mode = (mode_t)0;
1303 mode_t unx_mode = (mode_t)0;
1305 uint32 existing_dos_attributes = 0;
1306 struct pending_message_list *pml = NULL;
1307 struct timeval request_time = timeval_zero();
1308 struct share_mode_lock *lck = NULL;
1309 uint32 open_access_mask = access_mask;
1313 const char *newname;
1317 if (conn->printer) {
1319 * Printers are handled completely differently.
1320 * Most of the passed parameters are ignored.
1324 *pinfo = FILE_WAS_CREATED;
1327 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1329 return print_fsp_open(req, conn, fname, req->vuid, fsp);
1332 if (!parent_dirname_talloc(talloc_tos(), fname, &parent_dir,
1334 return NT_STATUS_NO_MEMORY;
1337 if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1339 unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
1340 new_dos_attributes = 0;
1342 /* We add aARCH to this as this mode is only used if the file is
1344 unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
1348 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1349 "access_mask=0x%x share_access=0x%x "
1350 "create_disposition = 0x%x create_options=0x%x "
1351 "unix mode=0%o oplock_request=%d\n",
1352 fname, new_dos_attributes, access_mask, share_access,
1353 create_disposition, create_options, unx_mode,
1356 if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) {
1357 DEBUG(0, ("No smb request but not an internal only open!\n"));
1358 return NT_STATUS_INTERNAL_ERROR;
1362 * Only non-internal opens can be deferred at all
1366 && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
1367 struct deferred_open_record *state =
1368 (struct deferred_open_record *)pml->private_data.data;
1370 /* Remember the absolute time of the original
1371 request with this mid. We'll use it later to
1372 see if this has timed out. */
1374 request_time = pml->request_time;
1376 /* Remove the deferred open entry under lock. */
1377 lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
1380 DEBUG(0, ("could not get share mode lock\n"));
1382 del_deferred_open_entry(lck, req->mid);
1386 /* Ensure we don't reprocess this message. */
1387 remove_deferred_open_smb_message(req->mid);
1390 status = check_name(conn, fname);
1391 if (!NT_STATUS_IS_OK(status)) {
1396 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1398 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1402 /* ignore any oplock requests if oplocks are disabled */
1403 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1404 IS_VETO_OPLOCK_PATH(conn, fname)) {
1405 /* Mask off everything except the private Samba bits. */
1406 oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
1409 /* this is for OS/2 long file names - say we don't support them */
1410 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1411 /* OS/2 Workplace shell fix may be main code stream in a later
1413 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1415 if (use_nt_status()) {
1416 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1418 return NT_STATUS_DOS(ERRDOS, ERRcannotopen);
1421 switch( create_disposition ) {
1423 * Currently we're using FILE_SUPERSEDE as the same as
1424 * FILE_OVERWRITE_IF but they really are
1425 * different. FILE_SUPERSEDE deletes an existing file
1426 * (requiring delete access) then recreates it.
1428 case FILE_SUPERSEDE:
1429 /* If file exists replace/overwrite. If file doesn't
1431 flags2 |= (O_CREAT | O_TRUNC);
1434 case FILE_OVERWRITE_IF:
1435 /* If file exists replace/overwrite. If file doesn't
1437 flags2 |= (O_CREAT | O_TRUNC);
1441 /* If file exists open. If file doesn't exist error. */
1442 if (!file_existed) {
1443 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1444 "requested for file %s and file "
1445 "doesn't exist.\n", fname ));
1447 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1451 case FILE_OVERWRITE:
1452 /* If file exists overwrite. If file doesn't exist
1454 if (!file_existed) {
1455 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1456 "requested for file %s and file "
1457 "doesn't exist.\n", fname ));
1459 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1465 /* If file exists error. If file doesn't exist
1468 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1469 "requested for file %s and file "
1470 "already exists.\n", fname ));
1471 if (S_ISDIR(psbuf->st_mode)) {
1476 return map_nt_error_from_unix(errno);
1478 flags2 |= (O_CREAT|O_EXCL);
1482 /* If file exists open. If file doesn't exist
1488 return NT_STATUS_INVALID_PARAMETER;
1491 /* We only care about matching attributes on file exists and
1494 if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) ||
1495 (create_disposition == FILE_OVERWRITE_IF))) {
1496 if (!open_match_attributes(conn, fname,
1497 existing_dos_attributes,
1498 new_dos_attributes, psbuf->st_mode,
1499 unx_mode, &new_unx_mode)) {
1500 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1501 "for file %s (%x %x) (0%o, 0%o)\n",
1502 fname, existing_dos_attributes,
1504 (unsigned int)psbuf->st_mode,
1505 (unsigned int)unx_mode ));
1507 return NT_STATUS_ACCESS_DENIED;
1511 status = calculate_access_mask(conn, fname, file_existed,
1514 if (!NT_STATUS_IS_OK(status)) {
1515 DEBUG(10, ("open_file_ntcreate: calculate_access_mask "
1516 "on file %s returned %s\n",
1518 nt_errstr(status)));
1522 open_access_mask = access_mask;
1524 if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1525 open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */
1528 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1529 "access_mask=0x%x\n", fname, access_mask ));
1532 * Note that we ignore the append flag as append does not
1533 * mean the same thing under DOS and Unix.
1536 if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ||
1537 (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) {
1538 /* DENY_DOS opens are always underlying read-write on the
1539 file handle, no matter what the requested access mask
1541 if ((create_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) ||
1542 access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) {
1552 * Currently we only look at FILE_WRITE_THROUGH for create options.
1556 if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) {
1561 if (posix_open && (access_mask & FILE_APPEND_DATA)) {
1565 if (!posix_open && !CAN_WRITE(conn)) {
1567 * We should really return a permission denied error if either
1568 * O_CREAT or O_TRUNC are set, but for compatibility with
1569 * older versions of Samba we just AND them out.
1571 flags2 &= ~(O_CREAT|O_TRUNC);
1575 * Ensure we can't write on a read-only share or file.
1578 if (flags != O_RDONLY && file_existed &&
1579 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1580 DEBUG(5,("open_file_ntcreate: write access requested for "
1581 "file %s on read only %s\n",
1582 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1584 return NT_STATUS_ACCESS_DENIED;
1587 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
1588 fsp->share_access = share_access;
1589 fsp->fh->private_options = create_options;
1590 fsp->access_mask = open_access_mask; /* We change this to the
1591 * requested access_mask after
1592 * the open is done. */
1593 fsp->posix_open = posix_open;
1595 /* Ensure no SAMBA_PRIVATE bits can be set. */
1596 fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK);
1598 if (timeval_is_zero(&request_time)) {
1599 request_time = fsp->open_time;
1603 struct timespec old_write_time = get_mtimespec(psbuf);
1604 id = vfs_file_id_from_sbuf(conn, psbuf);
1606 lck = get_share_mode_lock(talloc_tos(), id,
1608 fname, &old_write_time);
1611 DEBUG(0, ("Could not get share mode lock\n"));
1612 return NT_STATUS_SHARING_VIOLATION;
1615 /* First pass - send break only on batch oplocks. */
1617 && delay_for_oplocks(lck, fsp, req->mid, 1,
1619 schedule_defer_open(lck, request_time, req);
1621 return NT_STATUS_SHARING_VIOLATION;
1624 /* Use the client requested access mask here, not the one we
1626 status = open_mode_check(conn, fname, lck,
1627 access_mask, share_access,
1628 create_options, &file_existed);
1630 if (NT_STATUS_IS_OK(status)) {
1631 /* We might be going to allow this open. Check oplock
1633 /* Second pass - send break for both batch or
1634 * exclusive oplocks. */
1636 && delay_for_oplocks(lck, fsp, req->mid, 2,
1638 schedule_defer_open(lck, request_time, req);
1640 return NT_STATUS_SHARING_VIOLATION;
1644 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1645 /* DELETE_PENDING is not deferred for a second */
1650 if (!NT_STATUS_IS_OK(status)) {
1651 uint32 can_access_mask;
1652 bool can_access = True;
1654 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1656 /* Check if this can be done with the deny_dos and fcb
1658 if (create_options &
1659 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1660 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1662 DEBUG(0, ("DOS open without an SMB "
1665 return NT_STATUS_INTERNAL_ERROR;
1668 /* Use the client requested access mask here,
1669 * not the one we open with. */
1670 status = fcb_or_dos_open(req,
1681 if (NT_STATUS_IS_OK(status)) {
1684 *pinfo = FILE_WAS_OPENED;
1686 return NT_STATUS_OK;
1691 * This next line is a subtlety we need for
1692 * MS-Access. If a file open will fail due to share
1693 * permissions and also for security (access) reasons,
1694 * we need to return the access failed error, not the
1695 * share error. We can't open the file due to kernel
1696 * oplock deadlock (it's possible we failed above on
1697 * the open_mode_check()) so use a userspace check.
1700 if (flags & O_RDWR) {
1701 can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
1702 } else if (flags & O_WRONLY) {
1703 can_access_mask = FILE_WRITE_DATA;
1705 can_access_mask = FILE_READ_DATA;
1708 if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
1709 !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
1714 * If we're returning a share violation, ensure we
1715 * cope with the braindead 1 second delay.
1718 if (!(oplock_request & INTERNAL_OPEN_ONLY) &&
1719 lp_defer_sharing_violations()) {
1720 struct timeval timeout;
1721 struct deferred_open_record state;
1724 /* this is a hack to speed up torture tests
1726 timeout_usecs = lp_parm_int(SNUM(conn),
1727 "smbd","sharedelay",
1728 SHARING_VIOLATION_USEC_WAIT);
1730 /* This is a relative time, added to the absolute
1731 request_time value to get the absolute timeout time.
1732 Note that if this is the second or greater time we enter
1733 this codepath for this particular request mid then
1734 request_time is left as the absolute time of the *first*
1735 time this request mid was processed. This is what allows
1736 the request to eventually time out. */
1738 timeout = timeval_set(0, timeout_usecs);
1740 /* Nothing actually uses state.delayed_for_oplocks
1741 but it's handy to differentiate in debug messages
1742 between a 30 second delay due to oplock break, and
1743 a 1 second delay for share mode conflicts. */
1745 state.delayed_for_oplocks = False;
1749 && !request_timed_out(request_time,
1751 defer_open(lck, request_time, timeout,
1759 * We have detected a sharing violation here
1760 * so return the correct error code
1762 status = NT_STATUS_SHARING_VIOLATION;
1764 status = NT_STATUS_ACCESS_DENIED;
1770 * We exit this block with the share entry *locked*.....
1774 SMB_ASSERT(!file_existed || (lck != NULL));
1777 * Ensure we pay attention to default ACLs on directories if required.
1780 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1781 (def_acl = directory_has_default_acl(conn, parent_dir))) {
1785 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, "
1786 "access_mask = 0x%x, open_access_mask = 0x%x\n",
1787 (unsigned int)flags, (unsigned int)flags2,
1788 (unsigned int)unx_mode, (unsigned int)access_mask,
1789 (unsigned int)open_access_mask));
1792 * open_file strips any O_TRUNC flags itself.
1795 fsp_open = open_file(fsp, conn, req, parent_dir, newname, fname, psbuf,
1796 flags|flags2, unx_mode, access_mask,
1799 if (!NT_STATUS_IS_OK(fsp_open)) {
1806 if (!file_existed) {
1807 struct timespec old_write_time = get_mtimespec(psbuf);
1809 * Deal with the race condition where two smbd's detect the
1810 * file doesn't exist and do the create at the same time. One
1811 * of them will win and set a share mode, the other (ie. this
1812 * one) should check if the requested share mode for this
1813 * create is allowed.
1817 * Now the file exists and fsp is successfully opened,
1818 * fsp->dev and fsp->inode are valid and should replace the
1819 * dev=0,inode=0 from a non existent file. Spotted by
1820 * Nadav Danieli <nadavd@exanet.com>. JRA.
1825 lck = get_share_mode_lock(talloc_tos(), id,
1827 fname, &old_write_time);
1830 DEBUG(0, ("open_file_ntcreate: Could not get share "
1831 "mode lock for %s\n", fname));
1833 return NT_STATUS_SHARING_VIOLATION;
1836 /* First pass - send break only on batch oplocks. */
1838 && delay_for_oplocks(lck, fsp, req->mid, 1,
1840 schedule_defer_open(lck, request_time, req);
1843 return NT_STATUS_SHARING_VIOLATION;
1846 status = open_mode_check(conn, fname, lck,
1847 access_mask, share_access,
1848 create_options, &file_existed);
1850 if (NT_STATUS_IS_OK(status)) {
1851 /* We might be going to allow this open. Check oplock
1853 /* Second pass - send break for both batch or
1854 * exclusive oplocks. */
1856 && delay_for_oplocks(lck, fsp, req->mid, 2,
1858 schedule_defer_open(lck, request_time, req);
1861 return NT_STATUS_SHARING_VIOLATION;
1865 if (!NT_STATUS_IS_OK(status)) {
1866 struct deferred_open_record state;
1870 state.delayed_for_oplocks = False;
1873 /* Do it all over again immediately. In the second
1874 * round we will find that the file existed and handle
1875 * the DELETE_PENDING and FCB cases correctly. No need
1876 * to duplicate the code here. Essentially this is a
1877 * "goto top of this function", but don't tell
1881 defer_open(lck, request_time, timeval_zero(),
1889 * We exit this block with the share entry *locked*.....
1894 SMB_ASSERT(lck != NULL);
1896 /* note that we ignore failure for the following. It is
1897 basically a hack for NFS, and NFS will never set one of
1898 these only read them. Nobody but Samba can ever set a deny
1899 mode and we have already checked our more authoritative
1900 locking database for permission to set this deny mode. If
1901 the kernel refuses the operations then the kernel is wrong.
1902 note that GPFS supports it as well - jmcd */
1904 if (fsp->fh->fd != -1) {
1905 ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
1906 if(ret_flock == -1 ){
1911 return NT_STATUS_SHARING_VIOLATION;
1916 * At this point onwards, we can guarentee that the share entry
1917 * is locked, whether we created the file or not, and that the
1918 * deny mode is compatible with all current opens.
1922 * If requested, truncate the file.
1925 if (flags2&O_TRUNC) {
1927 * We are modifing the file after open - update the stat
1930 if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) ||
1931 (SMB_VFS_FSTAT(fsp, psbuf)==-1)) {
1932 status = map_nt_error_from_unix(errno);
1939 /* Record the options we were opened with. */
1940 fsp->share_access = share_access;
1941 fsp->fh->private_options = create_options;
1943 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
1945 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
1948 /* stat opens on existing files don't get oplocks. */
1949 if (is_stat_open(open_access_mask)) {
1950 fsp->oplock_type = NO_OPLOCK;
1953 if (!(flags2 & O_TRUNC)) {
1954 info = FILE_WAS_OPENED;
1956 info = FILE_WAS_OVERWRITTEN;
1959 info = FILE_WAS_CREATED;
1967 * Setup the oplock info in both the shared memory and
1971 if ((fsp->oplock_type != NO_OPLOCK) &&
1972 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1973 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1974 /* Could not get the kernel oplock */
1975 fsp->oplock_type = NO_OPLOCK;
1979 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) {
1980 new_file_created = True;
1983 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
1984 fsp->oplock_type, new_file_created);
1986 /* Handle strange delete on close create semantics. */
1987 if ((create_options & FILE_DELETE_ON_CLOSE)
1988 && (((conn->fs_capabilities & FILE_NAMED_STREAMS)
1989 && is_ntfs_stream_name(fname))
1990 || can_set_initial_delete_on_close(lck))) {
1991 status = can_set_delete_on_close(fsp, True, new_dos_attributes);
1993 if (!NT_STATUS_IS_OK(status)) {
1994 /* Remember to delete the mode we just added. */
1995 del_share_mode(lck, fsp);
2000 /* Note that here we set the *inital* delete on close flag,
2001 not the regular one. The magic gets handled in close. */
2002 fsp->initial_delete_on_close = True;
2005 if (new_file_created) {
2006 /* Files should be initially set as archive */
2007 if (lp_map_archive(SNUM(conn)) ||
2008 lp_store_dos_attributes(SNUM(conn))) {
2010 SMB_STRUCT_STAT tmp_sbuf;
2011 SET_STAT_INVALID(tmp_sbuf);
2012 if (file_set_dosmode(
2014 new_dos_attributes | aARCH,
2015 &tmp_sbuf, parent_dir,
2017 unx_mode = tmp_sbuf.st_mode;
2024 * Take care of inherited ACLs on created files - if default ACL not
2028 if (!posix_open && !file_existed && !def_acl) {
2030 int saved_errno = errno; /* We might get ENOSYS in the next
2033 if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 &&
2035 errno = saved_errno; /* Ignore ENOSYS */
2038 } else if (new_unx_mode) {
2042 /* Attributes need changing. File already existed. */
2045 int saved_errno = errno; /* We might get ENOSYS in the
2047 ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode);
2049 if (ret == -1 && errno == ENOSYS) {
2050 errno = saved_errno; /* Ignore ENOSYS */
2052 DEBUG(5, ("open_file_ntcreate: reset "
2053 "attributes of file %s to 0%o\n",
2054 fname, (unsigned int)new_unx_mode));
2055 ret = 0; /* Don't do the fchmod below. */
2060 (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
2061 DEBUG(5, ("open_file_ntcreate: failed to reset "
2062 "attributes of file %s to 0%o\n",
2063 fname, (unsigned int)new_unx_mode));
2066 /* If this is a successful open, we must remove any deferred open
2069 del_deferred_open_entry(lck, req->mid);
2073 return NT_STATUS_OK;
2076 /****************************************************************************
2077 Open a file with a share mode.
2078 ****************************************************************************/
2080 NTSTATUS open_file_ntcreate(connection_struct *conn,
2081 struct smb_request *req,
2083 SMB_STRUCT_STAT *psbuf,
2084 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
2085 uint32 share_access, /* share constants (FILE_SHARE_READ etc) */
2086 uint32 create_disposition, /* FILE_OPEN_IF etc. */
2087 uint32 create_options, /* options such as delete on close. */
2088 uint32 new_dos_attributes, /* attributes used for new file. */
2089 int oplock_request, /* internal Samba oplock codes. */
2090 /* Information (FILE_EXISTS etc.) */
2092 files_struct **result)
2095 files_struct *fsp = NULL;
2099 status = file_new(req, conn, &fsp);
2100 if(!NT_STATUS_IS_OK(status)) {
2104 status = open_file_ntcreate_internal(conn,
2117 if(!NT_STATUS_IS_OK(status)) {
2118 file_free(req, fsp);
2126 /****************************************************************************
2127 Open a file for for write to ensure that we can fchmod it.
2128 ****************************************************************************/
2130 NTSTATUS open_file_fchmod(struct smb_request *req, connection_struct *conn,
2132 SMB_STRUCT_STAT *psbuf, files_struct **result)
2134 files_struct *fsp = NULL;
2137 if (!VALID_STAT(*psbuf)) {
2138 return NT_STATUS_INVALID_PARAMETER;
2141 status = file_new(req, conn, &fsp);
2142 if(!NT_STATUS_IS_OK(status)) {
2146 /* note! we must use a non-zero desired access or we don't get
2147 a real file descriptor. Oh what a twisted web we weave. */
2148 status = open_file(fsp, conn, NULL, NULL, NULL, fname, psbuf, O_WRONLY,
2149 0, FILE_WRITE_DATA, FILE_WRITE_DATA);
2152 * This is not a user visible file open.
2153 * Don't set a share mode.
2156 if (!NT_STATUS_IS_OK(status)) {
2157 file_free(req, fsp);
2162 return NT_STATUS_OK;
2165 /****************************************************************************
2166 Close the fchmod file fd - ensure no locks are lost.
2167 ****************************************************************************/
2169 NTSTATUS close_file_fchmod(struct smb_request *req, files_struct *fsp)
2171 NTSTATUS status = fd_close(fsp);
2172 file_free(req, fsp);
2176 static NTSTATUS mkdir_internal(connection_struct *conn,
2178 uint32 file_attributes,
2179 SMB_STRUCT_STAT *psbuf)
2183 const char *dirname;
2185 bool posix_open = false;
2187 if(!CAN_WRITE(conn)) {
2188 DEBUG(5,("mkdir_internal: failing create on read-only share "
2189 "%s\n", lp_servicename(SNUM(conn))));
2190 return NT_STATUS_ACCESS_DENIED;
2193 status = check_name(conn, name);
2194 if (!NT_STATUS_IS_OK(status)) {
2198 if (!parent_dirname_talloc(talloc_tos(), name, &parent_dir,
2200 return NT_STATUS_NO_MEMORY;
2203 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
2205 mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
2207 mode = unix_mode(conn, aDIR, name, parent_dir);
2210 if (SMB_VFS_MKDIR(conn, name, mode) != 0) {
2211 return map_nt_error_from_unix(errno);
2214 /* Ensure we're checking for a symlink here.... */
2215 /* We don't want to get caught by a symlink racer. */
2217 if (SMB_VFS_LSTAT(conn, name, psbuf) == -1) {
2218 DEBUG(2, ("Could not stat directory '%s' just created: %s\n",
2219 name, strerror(errno)));
2220 return map_nt_error_from_unix(errno);
2223 if (!S_ISDIR(psbuf->st_mode)) {
2224 DEBUG(0, ("Directory just '%s' created is not a directory\n",
2226 return NT_STATUS_ACCESS_DENIED;
2229 if (lp_store_dos_attributes(SNUM(conn))) {
2231 file_set_dosmode(conn, name,
2232 file_attributes | aDIR, NULL,
2238 if (lp_inherit_perms(SNUM(conn))) {
2239 inherit_access_posix_acl(conn, parent_dir, name, mode);
2242 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
2244 * Check if high bits should have been set,
2245 * then (if bits are missing): add them.
2246 * Consider bits automagically set by UNIX, i.e. SGID bit from parent
2249 if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) && (mode & ~psbuf->st_mode)) {
2250 SMB_VFS_CHMOD(conn, name,
2251 psbuf->st_mode | (mode & ~psbuf->st_mode));
2255 /* Change the owner if required. */
2256 if (lp_inherit_owner(SNUM(conn))) {
2257 change_dir_owner_to_parent(conn, parent_dir, name, psbuf);
2260 notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME,
2263 return NT_STATUS_OK;
2266 /****************************************************************************
2267 Open a directory from an NT SMB call.
2268 ****************************************************************************/
2270 NTSTATUS open_directory(connection_struct *conn,
2271 struct smb_request *req,
2273 SMB_STRUCT_STAT *psbuf,
2275 uint32 share_access,
2276 uint32 create_disposition,
2277 uint32 create_options,
2278 uint32 file_attributes,
2280 files_struct **result)
2282 files_struct *fsp = NULL;
2283 bool dir_existed = VALID_STAT(*psbuf) ? True : False;
2284 struct share_mode_lock *lck = NULL;
2286 struct timespec mtimespec;
2289 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
2290 "share_access = 0x%x create_options = 0x%x, "
2291 "create_disposition = 0x%x, file_attributes = 0x%x\n",
2293 (unsigned int)access_mask,
2294 (unsigned int)share_access,
2295 (unsigned int)create_options,
2296 (unsigned int)create_disposition,
2297 (unsigned int)file_attributes));
2299 if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
2300 (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
2301 is_ntfs_stream_name(fname)) {
2302 DEBUG(2, ("open_directory: %s is a stream name!\n", fname));
2303 return NT_STATUS_NOT_A_DIRECTORY;
2306 status = calculate_access_mask(conn, fname, dir_existed,
2309 if (!NT_STATUS_IS_OK(status)) {
2310 DEBUG(10, ("open_directory: calculate_access_mask "
2311 "on file %s returned %s\n",
2313 nt_errstr(status)));
2317 switch( create_disposition ) {
2320 info = FILE_WAS_OPENED;
2323 * We want to follow symlinks here.
2326 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
2327 return map_nt_error_from_unix(errno);
2334 /* If directory exists error. If directory doesn't
2337 status = mkdir_internal(conn,
2342 if (!NT_STATUS_IS_OK(status)) {
2343 DEBUG(2, ("open_directory: unable to create "
2344 "%s. Error was %s\n", fname,
2345 nt_errstr(status)));
2349 info = FILE_WAS_CREATED;
2354 * If directory exists open. If directory doesn't
2358 status = mkdir_internal(conn,
2363 if (NT_STATUS_IS_OK(status)) {
2364 info = FILE_WAS_CREATED;
2367 if (NT_STATUS_EQUAL(status,
2368 NT_STATUS_OBJECT_NAME_COLLISION)) {
2369 info = FILE_WAS_OPENED;
2370 status = NT_STATUS_OK;
2375 case FILE_SUPERSEDE:
2376 case FILE_OVERWRITE:
2377 case FILE_OVERWRITE_IF:
2379 DEBUG(5,("open_directory: invalid create_disposition "
2380 "0x%x for directory %s\n",
2381 (unsigned int)create_disposition, fname));
2382 return NT_STATUS_INVALID_PARAMETER;
2385 if(!S_ISDIR(psbuf->st_mode)) {
2386 DEBUG(5,("open_directory: %s is not a directory !\n",
2388 return NT_STATUS_NOT_A_DIRECTORY;
2391 if (info == FILE_WAS_OPENED) {
2392 status = check_open_rights(conn,
2395 if (!NT_STATUS_IS_OK(status)) {
2396 DEBUG(10, ("open_directory: check_open_rights on "
2397 "file %s failed with %s\n",
2399 nt_errstr(status)));
2404 status = file_new(req, conn, &fsp);
2405 if(!NT_STATUS_IS_OK(status)) {
2410 * Setup the files_struct for it.
2413 fsp->mode = psbuf->st_mode;
2414 fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
2415 fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
2416 fsp->file_pid = req ? req->smbpid : 0;
2417 fsp->can_lock = False;
2418 fsp->can_read = False;
2419 fsp->can_write = False;
2421 fsp->share_access = share_access;
2422 fsp->fh->private_options = create_options;
2424 * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
2426 fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
2427 fsp->print_file = False;
2428 fsp->modified = False;
2429 fsp->oplock_type = NO_OPLOCK;
2430 fsp->sent_oplock_break = NO_BREAK_SENT;
2431 fsp->is_directory = True;
2432 fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False;
2434 string_set(&fsp->fsp_name,fname);
2436 mtimespec = get_mtimespec(psbuf);
2438 lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
2443 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
2444 file_free(req, fsp);
2445 return NT_STATUS_SHARING_VIOLATION;
2448 status = open_mode_check(conn, fname, lck,
2449 access_mask, share_access,
2450 create_options, &dir_existed);
2452 if (!NT_STATUS_IS_OK(status)) {
2454 file_free(req, fsp);
2458 set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK,
2461 /* For directories the delete on close bit at open time seems
2462 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
2463 if (create_options & FILE_DELETE_ON_CLOSE) {
2464 status = can_set_delete_on_close(fsp, True, 0);
2465 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
2467 file_free(req, fsp);
2471 if (NT_STATUS_IS_OK(status)) {
2472 /* Note that here we set the *inital* delete on close flag,
2473 not the regular one. The magic gets handled in close. */
2474 fsp->initial_delete_on_close = True;
2485 return NT_STATUS_OK;
2488 NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, const char *directory)
2491 SMB_STRUCT_STAT sbuf;
2494 SET_STAT_INVALID(sbuf);
2496 status = open_directory(conn, req, directory, &sbuf,
2497 FILE_READ_ATTRIBUTES, /* Just a stat open */
2498 FILE_SHARE_NONE, /* Ignored for stat opens */
2501 FILE_ATTRIBUTE_DIRECTORY,
2505 if (NT_STATUS_IS_OK(status)) {
2506 close_file(req, fsp, NORMAL_CLOSE);
2512 /****************************************************************************
2513 Receive notification that one of our open files has been renamed by another
2515 ****************************************************************************/
2517 void msg_file_was_renamed(struct messaging_context *msg,
2520 struct server_id server_id,
2524 char *frm = (char *)data->data;
2526 const char *sharepath;
2527 const char *newname;
2530 if (data->data == NULL
2531 || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2532 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n",
2533 (int)data->length));
2537 /* Unpack the message. */
2538 pull_file_id_16(frm, &id);
2539 sharepath = &frm[16];
2540 newname = sharepath + strlen(sharepath) + 1;
2541 sp_len = strlen(sharepath);
2543 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2545 sharepath, newname, file_id_string_tos(&id)));
2547 for(fsp = file_find_di_first(id); fsp; fsp = file_find_di_next(fsp)) {
2548 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2549 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2550 fsp->fnum, fsp->fsp_name, newname ));
2551 string_set(&fsp->fsp_name, newname);
2554 /* Now we have the complete path we can work out if this is
2555 actually within this share and adjust newname accordingly. */
2556 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2557 "not sharepath %s) "
2558 "fnum %d from %s -> %s\n",
2559 fsp->conn->connectpath,
2568 struct case_semantics_state {
2569 connection_struct *conn;
2570 bool case_sensitive;
2572 bool short_case_preserve;
2575 /****************************************************************************
2576 Restore case semantics.
2577 ****************************************************************************/
2578 static int restore_case_semantics(struct case_semantics_state *state)
2580 state->conn->case_sensitive = state->case_sensitive;
2581 state->conn->case_preserve = state->case_preserve;
2582 state->conn->short_case_preserve = state->short_case_preserve;
2586 /****************************************************************************
2587 Save case semantics.
2588 ****************************************************************************/
2589 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
2590 connection_struct *conn)
2592 struct case_semantics_state *result;
2594 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
2595 DEBUG(0, ("talloc failed\n"));
2599 result->conn = conn;
2600 result->case_sensitive = conn->case_sensitive;
2601 result->case_preserve = conn->case_preserve;
2602 result->short_case_preserve = conn->short_case_preserve;
2605 conn->case_sensitive = True;
2606 conn->case_preserve = True;
2607 conn->short_case_preserve = True;
2609 talloc_set_destructor(result, restore_case_semantics);
2615 * If a main file is opened for delete, all streams need to be checked for
2616 * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS.
2617 * If that works, delete them all by setting the delete on close and close.
2620 static NTSTATUS open_streams_for_delete(connection_struct *conn,
2623 struct stream_struct *stream_info;
2624 files_struct **streams;
2626 unsigned int num_streams;
2627 TALLOC_CTX *frame = talloc_stackframe();
2630 status = SMB_VFS_STREAMINFO(conn, NULL, fname, talloc_tos(),
2631 &num_streams, &stream_info);
2633 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)
2634 || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2635 DEBUG(10, ("no streams around\n"));
2637 return NT_STATUS_OK;
2640 if (!NT_STATUS_IS_OK(status)) {
2641 DEBUG(10, ("SMB_VFS_STREAMINFO failed: %s\n",
2642 nt_errstr(status)));
2646 DEBUG(10, ("open_streams_for_delete found %d streams\n",
2649 if (num_streams == 0) {
2651 return NT_STATUS_OK;
2654 streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams);
2655 if (streams == NULL) {
2656 DEBUG(0, ("talloc failed\n"));
2657 status = NT_STATUS_NO_MEMORY;
2661 for (i=0; i<num_streams; i++) {
2664 if (strequal(stream_info[i].name, "::$DATA")) {
2669 streamname = talloc_asprintf(talloc_tos(), "%s%s", fname,
2670 stream_info[i].name);
2672 if (streamname == NULL) {
2673 DEBUG(0, ("talloc_aprintf failed\n"));
2674 status = NT_STATUS_NO_MEMORY;
2678 status = create_file_unixpath
2681 streamname, /* fname */
2682 DELETE_ACCESS, /* access_mask */
2683 FILE_SHARE_READ | FILE_SHARE_WRITE
2684 | FILE_SHARE_DELETE, /* share_access */
2685 FILE_OPEN, /* create_disposition*/
2686 NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* create_options */
2687 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2688 0, /* oplock_request */
2689 0, /* allocation_size */
2692 &streams[i], /* result */
2696 TALLOC_FREE(streamname);
2698 if (!NT_STATUS_IS_OK(status)) {
2699 DEBUG(10, ("Could not open stream %s: %s\n",
2700 streamname, nt_errstr(status)));
2706 * don't touch the variable "status" beyond this point :-)
2709 for (i -= 1 ; i >= 0; i--) {
2710 if (streams[i] == NULL) {
2714 DEBUG(10, ("Closing stream # %d, %s\n", i,
2715 streams[i]->fsp_name));
2716 close_file(NULL, streams[i], NORMAL_CLOSE);
2725 * Wrapper around open_file_ntcreate and open_directory
2728 static NTSTATUS create_file_unixpath(connection_struct *conn,
2729 struct smb_request *req,
2731 uint32_t access_mask,
2732 uint32_t share_access,
2733 uint32_t create_disposition,
2734 uint32_t create_options,
2735 uint32_t file_attributes,
2736 uint32_t oplock_request,
2737 uint64_t allocation_size,
2738 struct security_descriptor *sd,
2739 struct ea_list *ea_list,
2741 files_struct **result,
2743 SMB_STRUCT_STAT *psbuf)
2745 SMB_STRUCT_STAT sbuf;
2746 int info = FILE_WAS_OPENED;
2747 files_struct *base_fsp = NULL;
2748 files_struct *fsp = NULL;
2751 DEBUG(10,("create_file_unixpath: access_mask = 0x%x "
2752 "file_attributes = 0x%x, share_access = 0x%x, "
2753 "create_disposition = 0x%x create_options = 0x%x "
2754 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
2756 (unsigned int)access_mask,
2757 (unsigned int)file_attributes,
2758 (unsigned int)share_access,
2759 (unsigned int)create_disposition,
2760 (unsigned int)create_options,
2761 (unsigned int)oplock_request,
2762 ea_list, sd, fname));
2764 if (create_options & FILE_OPEN_BY_FILE_ID) {
2765 status = NT_STATUS_NOT_SUPPORTED;
2769 if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) {
2770 status = NT_STATUS_INVALID_PARAMETER;
2775 oplock_request |= INTERNAL_OPEN_ONLY;
2778 if (psbuf != NULL) {
2782 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
2783 SET_STAT_INVALID(sbuf);
2787 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2788 && (access_mask & DELETE_ACCESS)
2789 && !is_ntfs_stream_name(fname)) {
2791 * We can't open a file with DELETE access if any of the
2792 * streams is open without FILE_SHARE_DELETE
2794 status = open_streams_for_delete(conn, fname);
2796 if (!NT_STATUS_IS_OK(status)) {
2801 /* This is the correct thing to do (check every time) but can_delete
2802 * is expensive (it may have to read the parent directory
2803 * permissions). So for now we're not doing it unless we have a strong
2804 * hint the client is really going to delete this file. If the client
2805 * is forcing FILE_CREATE let the filesystem take care of the
2808 /* Setting FILE_SHARE_DELETE is the hint. */
2810 if (lp_acl_check_permissions(SNUM(conn))
2811 && (create_disposition != FILE_CREATE)
2812 && (share_access & FILE_SHARE_DELETE)
2813 && (access_mask & DELETE_ACCESS)
2814 && (!can_delete_file_in_directory(conn, fname))) {
2815 status = NT_STATUS_ACCESS_DENIED;
2820 /* We need to support SeSecurityPrivilege for this. */
2821 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
2822 !user_has_privileges(current_user.nt_user_token,
2824 status = NT_STATUS_PRIVILEGE_NOT_HELD;
2829 if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
2830 && is_ntfs_stream_name(fname)
2831 && (!(create_options & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) {
2833 uint32 base_create_disposition;
2835 if (create_options & FILE_DIRECTORY_FILE) {
2836 status = NT_STATUS_NOT_A_DIRECTORY;
2840 status = split_ntfs_stream_name(talloc_tos(), fname,
2842 if (!NT_STATUS_IS_OK(status)) {
2843 DEBUG(10, ("create_file_unixpath: "
2844 "split_ntfs_stream_name failed: %s\n",
2845 nt_errstr(status)));
2849 SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
2851 switch (create_disposition) {
2853 base_create_disposition = FILE_OPEN;
2856 base_create_disposition = FILE_OPEN_IF;
2860 status = create_file_unixpath(conn, NULL, base, 0,
2863 | FILE_SHARE_DELETE,
2864 base_create_disposition,
2865 0, 0, 0, 0, NULL, NULL,
2866 &base_fsp, NULL, NULL);
2867 if (!NT_STATUS_IS_OK(status)) {
2868 DEBUG(10, ("create_file_unixpath for base %s failed: "
2869 "%s\n", base, nt_errstr(status)));
2872 /* we don't need to low level fd */
2877 * If it's a request for a directory open, deal with it separately.
2880 if (create_options & FILE_DIRECTORY_FILE) {
2882 if (create_options & FILE_NON_DIRECTORY_FILE) {
2883 status = NT_STATUS_INVALID_PARAMETER;
2887 /* Can't open a temp directory. IFS kit test. */
2888 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
2889 status = NT_STATUS_INVALID_PARAMETER;
2894 * We will get a create directory here if the Win32
2895 * app specified a security descriptor in the
2896 * CreateDirectory() call.
2900 status = open_directory(
2901 conn, req, fname, &sbuf, access_mask, share_access,
2902 create_disposition, create_options, file_attributes,
2907 * Ordinary file case.
2912 * We're opening the stream element of a base_fsp
2913 * we already opened. We need to initialize
2914 * the fsp first, and set up the base_fsp pointer.
2916 status = file_new(req, conn, &fsp);
2917 if(!NT_STATUS_IS_OK(status)) {
2921 fsp->base_fsp = base_fsp;
2923 status = open_file_ntcreate_internal(conn,
2936 if(!NT_STATUS_IS_OK(status)) {
2937 file_free(req, fsp);
2941 status = open_file_ntcreate(
2942 conn, req, fname, &sbuf, access_mask, share_access,
2943 create_disposition, create_options, file_attributes,
2944 oplock_request, &info, &fsp);
2947 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
2949 /* A stream open never opens a directory */
2952 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2957 * Fail the open if it was explicitly a non-directory
2961 if (create_options & FILE_NON_DIRECTORY_FILE) {
2962 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2967 status = open_directory(
2968 conn, req, fname, &sbuf, access_mask,
2969 share_access, create_disposition,
2970 create_options, file_attributes,
2975 if (!NT_STATUS_IS_OK(status)) {
2979 fsp->base_fsp = base_fsp;
2982 * According to the MS documentation, the only time the security
2983 * descriptor is applied to the opened file is iff we *created* the
2984 * file; an existing file stays the same.
2986 * Also, it seems (from observation) that you can open the file with
2987 * any access mask but you can still write the sd. We need to override
2988 * the granted access before we call set_sd
2989 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
2992 if ((sd != NULL) && (info == FILE_WAS_CREATED)
2993 && lp_nt_acl_support(SNUM(conn))) {
2995 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
2996 uint32_t saved_access_mask = fsp->access_mask;
2998 if (sd->owner_sid == NULL) {
2999 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
3001 if (sd->group_sid == NULL) {
3002 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
3004 if (sd->sacl == NULL) {
3005 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
3007 if (sd->dacl == NULL) {
3008 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
3011 fsp->access_mask = FILE_GENERIC_ALL;
3013 /* Convert all the generic bits. */
3014 security_acl_map_generic(sd->dacl, &file_generic_mapping);
3015 security_acl_map_generic(sd->sacl, &file_generic_mapping);
3017 if (sec_info_sent & (OWNER_SECURITY_INFORMATION|
3018 GROUP_SECURITY_INFORMATION|
3019 DACL_SECURITY_INFORMATION|
3020 SACL_SECURITY_INFORMATION)) {
3021 status = SMB_VFS_FSET_NT_ACL(fsp, sec_info_sent, sd);
3024 fsp->access_mask = saved_access_mask;
3026 if (!NT_STATUS_IS_OK(status)) {
3031 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
3032 status = set_ea(conn, fsp, fname, ea_list);
3033 if (!NT_STATUS_IS_OK(status)) {
3038 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
3039 status = NT_STATUS_ACCESS_DENIED;
3043 /* Save the requested allocation size. */
3044 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
3046 && (allocation_size > sbuf.st_size)) {
3047 fsp->initial_allocation_size = smb_roundup(
3048 fsp->conn, allocation_size);
3049 if (fsp->is_directory) {
3050 /* Can't set allocation size on a directory. */
3051 status = NT_STATUS_ACCESS_DENIED;
3054 if (vfs_allocate_file_space(
3055 fsp, fsp->initial_allocation_size) == -1) {
3056 status = NT_STATUS_DISK_FULL;
3060 fsp->initial_allocation_size = smb_roundup(
3061 fsp->conn, (uint64_t)sbuf.st_size);
3065 DEBUG(10, ("create_file_unixpath: info=%d\n", info));
3068 if (pinfo != NULL) {
3071 if (psbuf != NULL) {
3072 if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
3076 SMB_VFS_FSTAT(fsp, psbuf);
3079 return NT_STATUS_OK;
3082 DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status)));
3085 if (base_fsp && fsp->base_fsp == base_fsp) {
3087 * The close_file below will close
3092 close_file(req, fsp, ERROR_CLOSE);
3095 if (base_fsp != NULL) {
3096 close_file(req, base_fsp, ERROR_CLOSE);
3102 NTSTATUS create_file_default(connection_struct *conn,
3103 struct smb_request *req,
3104 uint16_t root_dir_fid,
3107 uint32_t access_mask,
3108 uint32_t share_access,
3109 uint32_t create_disposition,
3110 uint32_t create_options,
3111 uint32_t file_attributes,
3112 uint32_t oplock_request,
3113 uint64_t allocation_size,
3114 struct security_descriptor *sd,
3115 struct ea_list *ea_list,
3117 files_struct **result,
3119 SMB_STRUCT_STAT *psbuf)
3121 struct case_semantics_state *case_state = NULL;
3122 SMB_STRUCT_STAT sbuf;
3123 int info = FILE_WAS_OPENED;
3124 files_struct *fsp = NULL;
3127 DEBUG(10,("create_file: access_mask = 0x%x "
3128 "file_attributes = 0x%x, share_access = 0x%x, "
3129 "create_disposition = 0x%x create_options = 0x%x "
3130 "oplock_request = 0x%x "
3131 "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
3132 "is_dos_path = %s, fname = %s\n",
3133 (unsigned int)access_mask,
3134 (unsigned int)file_attributes,
3135 (unsigned int)share_access,
3136 (unsigned int)create_disposition,
3137 (unsigned int)create_options,
3138 (unsigned int)oplock_request,
3139 (unsigned int)root_dir_fid,
3140 ea_list, sd, fname, is_dos_path ? "true" : "false"));
3143 * Get the file name.
3146 if (root_dir_fid != 0) {
3148 * This filename is relative to a directory fid.
3150 char *parent_fname = NULL;
3151 files_struct *dir_fsp = file_fsp(req, root_dir_fid);
3153 if (dir_fsp == NULL) {
3154 status = NT_STATUS_INVALID_HANDLE;
3158 if (!dir_fsp->is_directory) {
3161 * Check to see if this is a mac fork of some kind.
3164 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
3165 is_ntfs_stream_name(fname)) {
3166 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3171 we need to handle the case when we get a
3172 relative open relative to a file and the
3173 pathname is blank - this is a reopen!
3174 (hint from demyn plantenberg)
3177 status = NT_STATUS_INVALID_HANDLE;
3181 if (ISDOT(dir_fsp->fsp_name)) {
3183 * We're at the toplevel dir, the final file name
3184 * must not contain ./, as this is filtered out
3185 * normally by srvstr_get_path and unix_convert
3186 * explicitly rejects paths containing ./.
3188 parent_fname = talloc_strdup(talloc_tos(), "");
3189 if (parent_fname == NULL) {
3190 status = NT_STATUS_NO_MEMORY;
3194 size_t dir_name_len = strlen(dir_fsp->fsp_name);
3197 * Copy in the base directory name.
3200 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
3202 if (parent_fname == NULL) {
3203 status = NT_STATUS_NO_MEMORY;
3206 memcpy(parent_fname, dir_fsp->fsp_name,
3210 * Ensure it ends in a '/'.
3211 * We used TALLOC_SIZE +2 to add space for the '/'.
3215 && (parent_fname[dir_name_len-1] != '\\')
3216 && (parent_fname[dir_name_len-1] != '/')) {
3217 parent_fname[dir_name_len] = '/';
3218 parent_fname[dir_name_len+1] = '\0';
3222 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
3224 if (fname == NULL) {
3225 status = NT_STATUS_NO_MEMORY;
3231 * Check to see if this is a mac fork of some kind.
3234 if (is_ntfs_stream_name(fname)) {
3235 enum FAKE_FILE_TYPE fake_file_type;
3237 fake_file_type = is_fake_file(fname);
3239 if (fake_file_type != FAKE_FILE_TYPE_NONE) {
3242 * Here we go! support for changing the disk quotas
3245 * We need to fake up to open this MAGIC QUOTA file
3246 * and return a valid FID.
3248 * w2k close this file directly after openening xp
3249 * also tries a QUERY_FILE_INFO on the file and then
3252 status = open_fake_file(req, conn, req->vuid,
3253 fake_file_type, fname,
3255 if (!NT_STATUS_IS_OK(status)) {
3263 if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
3264 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
3269 if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
3270 char *resolved_fname;
3272 status = resolve_dfspath(talloc_tos(), conn, true, fname,
3275 if (!NT_STATUS_IS_OK(status)) {
3277 * For PATH_NOT_COVERED we had
3278 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
3279 * ERRSRV, ERRbadpath);
3280 * Need to fix in callers
3284 fname = resolved_fname;
3288 * Check if POSIX semantics are wanted.
3291 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
3292 case_state = set_posix_case_semantics(talloc_tos(), conn);
3293 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
3297 char *converted_fname;
3299 SET_STAT_INVALID(sbuf);
3301 status = unix_convert(talloc_tos(), conn, fname, False,
3302 &converted_fname, NULL, &sbuf);
3303 if (!NT_STATUS_IS_OK(status)) {
3306 fname = converted_fname;
3308 if (psbuf != NULL) {
3311 if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
3312 SET_STAT_INVALID(sbuf);
3318 TALLOC_FREE(case_state);
3320 /* All file access must go through check_name() */
3322 status = check_name(conn, fname);
3323 if (!NT_STATUS_IS_OK(status)) {
3327 status = create_file_unixpath(
3328 conn, req, fname, access_mask, share_access,
3329 create_disposition, create_options, file_attributes,
3330 oplock_request, allocation_size, sd, ea_list,
3331 &fsp, &info, &sbuf);
3333 if (!NT_STATUS_IS_OK(status)) {
3338 DEBUG(10, ("create_file: info=%d\n", info));
3341 if (pinfo != NULL) {
3344 if (psbuf != NULL) {
3347 return NT_STATUS_OK;
3350 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
3353 close_file(req, fsp, ERROR_CLOSE);