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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern struct current_user current_user;
26 extern userdom_struct current_user_info;
27 extern uint16 global_smbpid;
28 extern BOOL global_client_failed_oplock_break;
30 struct deferred_open_record {
31 BOOL delayed_for_oplocks;
36 /****************************************************************************
37 fd support routines - attempt to do a dos_open.
38 ****************************************************************************/
40 static int fd_open(struct connection_struct *conn,
47 if (!lp_symlinks(SNUM(conn))) {
52 fd = SMB_VFS_OPEN(conn,fname,flags,mode);
54 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n", fname,
55 flags, (int)mode, fd, (fd == -1) ? strerror(errno) : "" ));
60 /****************************************************************************
61 Close the file associated with a fsp.
62 ****************************************************************************/
64 int fd_close(struct connection_struct *conn,
67 if (fsp->fh->fd == -1) {
68 return 0; /* What we used to call a stat open. */
70 if (fsp->fh->ref_count > 1) {
71 return 0; /* Shared handle. Only close last reference. */
73 return fd_close_posix(conn, fsp);
76 /****************************************************************************
77 Change the ownership of a file to that of the parent directory.
78 Do this by fd if possible.
79 ****************************************************************************/
81 void change_owner_to_parent(connection_struct *conn,
84 SMB_STRUCT_STAT *psbuf)
86 const char *parent_path = parent_dirname(fname);
87 SMB_STRUCT_STAT parent_st;
90 ret = SMB_VFS_STAT(conn, parent_path, &parent_st);
92 DEBUG(0,("change_owner_to_parent: failed to stat parent "
93 "directory %s. Error was %s\n",
94 parent_path, strerror(errno) ));
98 if (fsp && fsp->fh->fd != -1) {
100 ret = SMB_VFS_FCHOWN(fsp, fsp->fh->fd, parent_st.st_uid, (gid_t)-1);
103 DEBUG(0,("change_owner_to_parent: failed to fchown "
104 "file %s to parent directory uid %u. Error "
106 (unsigned int)parent_st.st_uid,
110 DEBUG(10,("change_owner_to_parent: changed new file %s to "
111 "parent directory uid %u.\n", fname,
112 (unsigned int)parent_st.st_uid ));
115 /* We've already done an lstat into psbuf, and we know it's a
116 directory. If we can cd into the directory and the dev/ino
117 are the same then we can safely chown without races as
118 we're locking the directory in place by being in it. This
119 should work on any UNIX (thanks tridge :-). JRA.
123 SMB_STRUCT_STAT sbuf;
125 if (!vfs_GetWd(conn,saved_dir)) {
126 DEBUG(0,("change_owner_to_parent: failed to get "
127 "current working directory\n"));
131 /* Chdir into the new path. */
132 if (vfs_ChDir(conn, fname) == -1) {
133 DEBUG(0,("change_owner_to_parent: failed to change "
134 "current working directory to %s. Error "
135 "was %s\n", fname, strerror(errno) ));
139 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
140 DEBUG(0,("change_owner_to_parent: failed to stat "
141 "directory '.' (%s) Error was %s\n",
142 fname, strerror(errno)));
146 /* Ensure we're pointing at the same place. */
147 if (sbuf.st_dev != psbuf->st_dev ||
148 sbuf.st_ino != psbuf->st_ino ||
149 sbuf.st_mode != psbuf->st_mode ) {
150 DEBUG(0,("change_owner_to_parent: "
151 "device/inode/mode on directory %s changed. "
152 "Refusing to chown !\n", fname ));
157 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
160 DEBUG(10,("change_owner_to_parent: failed to chown "
161 "directory %s to parent directory uid %u. "
162 "Error was %s\n", fname,
163 (unsigned int)parent_st.st_uid, strerror(errno) ));
167 DEBUG(10,("change_owner_to_parent: changed ownership of new "
168 "directory %s to parent directory uid %u.\n",
169 fname, (unsigned int)parent_st.st_uid ));
173 vfs_ChDir(conn,saved_dir);
177 /****************************************************************************
179 ****************************************************************************/
181 static BOOL open_file(files_struct *fsp,
182 connection_struct *conn,
184 SMB_STRUCT_STAT *psbuf,
189 int accmode = (flags & O_ACCMODE);
190 int local_flags = flags;
191 BOOL file_existed = VALID_STAT(*psbuf);
196 /* Check permissions */
199 * This code was changed after seeing a client open request
200 * containing the open mode of (DENY_WRITE/read-only) with
201 * the 'create if not exist' bit set. The previous code
202 * would fail to open the file read only on a read-only share
203 * as it was checking the flags parameter directly against O_RDONLY,
204 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
208 if (!CAN_WRITE(conn)) {
209 /* It's a read-only share - fail if we wanted to write. */
210 if(accmode != O_RDONLY) {
211 DEBUG(3,("Permission denied opening %s\n",fname));
213 } else if(flags & O_CREAT) {
214 /* We don't want to write - but we must make sure that
215 O_CREAT doesn't create the file if we have write
216 access into the directory.
219 local_flags &= ~O_CREAT;
224 * This little piece of insanity is inspired by the
225 * fact that an NT client can open a file for O_RDONLY,
226 * but set the create disposition to FILE_EXISTS_TRUNCATE.
227 * If the client *can* write to the file, then it expects to
228 * truncate the file, even though it is opening for readonly.
229 * Quicken uses this stupid trick in backup file creation...
230 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
231 * for helping track this one down. It didn't bite us in 2.0.x
232 * as we always opened files read-write in that release. JRA.
235 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
236 DEBUG(10,("open_file: truncate requested on read-only open "
237 "for file %s\n",fname ));
238 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
241 if ((access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
242 (local_flags & O_CREAT) ||
243 ((local_flags & O_TRUNC) == O_TRUNC) ) {
246 * We can't actually truncate here as the file may be locked.
247 * open_file_shared will take care of the truncate later. JRA.
250 local_flags &= ~O_TRUNC;
252 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
254 * We would block on opening a FIFO with no one else on the
255 * other end. Do what we used to do and add O_NONBLOCK to the
259 if (file_existed && S_ISFIFO(psbuf->st_mode)) {
260 local_flags |= O_NONBLOCK;
264 /* Don't create files with Microsoft wildcard characters. */
265 if ((local_flags & O_CREAT) && !file_existed &&
266 ms_has_wild(fname)) {
267 set_saved_ntstatus(NT_STATUS_OBJECT_NAME_INVALID);
271 /* Actually do the open */
272 fsp->fh->fd = fd_open(conn, fname, local_flags, unx_mode);
273 if (fsp->fh->fd == -1) {
274 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
276 fname,strerror(errno),local_flags,flags));
280 /* Inherit the ACL if the file was created. */
281 if ((local_flags & O_CREAT) && !file_existed) {
282 inherit_access_acl(conn, fname, unx_mode);
286 fsp->fh->fd = -1; /* What we used to call a stat open. */
292 if (fsp->fh->fd == -1) {
293 ret = SMB_VFS_STAT(conn, fname, psbuf);
295 ret = SMB_VFS_FSTAT(fsp,fsp->fh->fd,psbuf);
296 /* If we have an fd, this stat should succeed. */
298 DEBUG(0,("Error doing fstat on open file %s "
299 "(%s)\n", fname,strerror(errno) ));
303 /* For a non-io open, this stat failing means file not found. JRA */
311 * POSIX allows read-only opens of directories. We don't
312 * want to do this (we use a different code path for this)
313 * so catch a directory open and return an EISDIR. JRA.
316 if(S_ISDIR(psbuf->st_mode)) {
322 fsp->mode = psbuf->st_mode;
323 fsp->inode = psbuf->st_ino;
324 fsp->dev = psbuf->st_dev;
325 fsp->vuid = current_user.vuid;
326 fsp->file_pid = global_smbpid;
327 fsp->can_lock = True;
328 fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False;
329 if (!CAN_WRITE(conn)) {
330 fsp->can_write = False;
332 fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? True : False;
334 fsp->print_file = False;
335 fsp->modified = False;
336 fsp->sent_oplock_break = NO_BREAK_SENT;
337 fsp->is_directory = False;
338 fsp->is_stat = False;
339 if (conn->aio_write_behind_list &&
340 is_in_path(fname, conn->aio_write_behind_list, conn->case_sensitive)) {
341 fsp->aio_write_behind = True;
344 string_set(&fsp->fsp_name,fname);
345 fsp->wcp = NULL; /* Write cache pointer. */
347 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
348 *current_user_info.smb_name ? current_user_info.smb_name : conn->user,fsp->fsp_name,
349 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
350 conn->num_files_open + 1));
356 /*******************************************************************
357 Return True if the filename is one of the special executable types.
358 ********************************************************************/
360 static BOOL is_executable(const char *fname)
362 if ((fname = strrchr_m(fname,'.'))) {
363 if (strequal(fname,".com") ||
364 strequal(fname,".dll") ||
365 strequal(fname,".exe") ||
366 strequal(fname,".sym")) {
373 /****************************************************************************
374 Check if we can open a file with a share mode.
375 Returns True if conflict, False if not.
376 ****************************************************************************/
378 static BOOL share_conflict(struct share_mode_entry *entry,
382 DEBUG(10,("share_conflict: entry->access_mask = 0x%x, "
383 "entry->share_access = 0x%x, "
384 "entry->private_options = 0x%x\n",
385 (unsigned int)entry->access_mask,
386 (unsigned int)entry->share_access,
387 (unsigned int)entry->private_options));
389 DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n",
390 (unsigned int)access_mask, (unsigned int)share_access));
392 if ((entry->access_mask & (FILE_WRITE_DATA|
396 DELETE_ACCESS)) == 0) {
397 DEBUG(10,("share_conflict: No conflict due to "
398 "entry->access_mask = 0x%x\n",
399 (unsigned int)entry->access_mask ));
403 if ((access_mask & (FILE_WRITE_DATA|
407 DELETE_ACCESS)) == 0) {
408 DEBUG(10,("share_conflict: No conflict due to "
409 "access_mask = 0x%x\n",
410 (unsigned int)access_mask ));
414 #if 1 /* JRA TEST - Superdebug. */
415 #define CHECK_MASK(num, am, right, sa, share) \
416 DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \
417 (unsigned int)(num), (unsigned int)(am), \
418 (unsigned int)(right), (unsigned int)(am)&(right) )); \
419 DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \
420 (unsigned int)(num), (unsigned int)(sa), \
421 (unsigned int)(share), (unsigned int)(sa)&(share) )); \
422 if (((am) & (right)) && !((sa) & (share))) { \
423 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
424 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
425 (unsigned int)(share) )); \
429 #define CHECK_MASK(num, am, right, sa, share) \
430 if (((am) & (right)) && !((sa) & (share))) { \
431 DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \
432 sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \
433 (unsigned int)(share) )); \
438 CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
439 share_access, FILE_SHARE_WRITE);
440 CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA,
441 entry->share_access, FILE_SHARE_WRITE);
443 CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE,
444 share_access, FILE_SHARE_READ);
445 CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE,
446 entry->share_access, FILE_SHARE_READ);
448 CHECK_MASK(5, entry->access_mask, DELETE_ACCESS,
449 share_access, FILE_SHARE_DELETE);
450 CHECK_MASK(6, access_mask, DELETE_ACCESS,
451 entry->share_access, FILE_SHARE_DELETE);
453 DEBUG(10,("share_conflict: No conflict.\n"));
457 #if defined(DEVELOPER)
458 static void validate_my_share_entries(int num,
459 struct share_mode_entry *share_entry)
463 if (!procid_is_me(&share_entry->pid)) {
467 if (is_deferred_open_entry(share_entry) &&
468 !open_was_deferred(share_entry->op_mid)) {
470 DEBUG(0, ("Got a deferred entry without a request: "
471 "PANIC: %s\n", share_mode_str(num, share_entry)));
475 if (!is_valid_share_mode_entry(share_entry)) {
479 fsp = file_find_dif(share_entry->dev, share_entry->inode,
480 share_entry->share_file_id);
482 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
483 share_mode_str(num, share_entry) ));
484 smb_panic("validate_my_share_entries: Cannot match a "
485 "share entry with an open file\n");
488 if (is_deferred_open_entry(share_entry) ||
489 is_unused_share_mode_entry(share_entry)) {
493 if ((share_entry->op_type == NO_OPLOCK) &&
494 (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) {
495 /* Someone has already written to it, but I haven't yet
500 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
509 DEBUG(0,("validate_my_share_entries: PANIC : %s\n",
510 share_mode_str(num, share_entry) ));
511 slprintf(str, sizeof(str)-1, "validate_my_share_entries: "
512 "file %s, oplock_type = 0x%x, op_type = 0x%x\n",
513 fsp->fsp_name, (unsigned int)fsp->oplock_type,
514 (unsigned int)share_entry->op_type );
520 static BOOL is_stat_open(uint32 access_mask)
522 return (access_mask &&
523 ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES|
524 FILE_WRITE_ATTRIBUTES))==0) &&
525 ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|
526 FILE_WRITE_ATTRIBUTES)) != 0));
529 /****************************************************************************
530 Deal with share modes
531 Invarient: Share mode must be locked on entry and exit.
532 Returns -1 on error, or number of share modes on success (may be zero).
533 ****************************************************************************/
535 static NTSTATUS open_mode_check(connection_struct *conn,
537 struct share_mode_lock *lck,
540 uint32 create_options,
545 if(lck->num_share_modes == 0) {
549 *file_existed = True;
551 if (is_stat_open(access_mask)) {
552 /* Stat open that doesn't trigger oplock breaks or share mode
553 * checks... ! JRA. */
557 /* A delete on close prohibits everything */
559 if (lck->delete_on_close) {
560 return NT_STATUS_DELETE_PENDING;
564 * Check if the share modes will give us access.
567 #if defined(DEVELOPER)
568 for(i = 0; i < lck->num_share_modes; i++) {
569 validate_my_share_entries(i, &lck->share_modes[i]);
573 if (!lp_share_modes(SNUM(conn))) {
577 /* Now we check the share modes, after any oplock breaks. */
578 for(i = 0; i < lck->num_share_modes; i++) {
580 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
584 /* someone else has a share lock on it, check to see if we can
586 if (share_conflict(&lck->share_modes[i],
587 access_mask, share_access)) {
588 return NT_STATUS_SHARING_VIOLATION;
595 static BOOL is_delete_request(files_struct *fsp) {
596 return ((fsp->access_mask == DELETE_ACCESS) &&
597 (fsp->oplock_type == NO_OPLOCK));
601 * 1) No files open at all: Grant whatever the client wants.
603 * 2) Exclusive (or batch) oplock around: If the requested access is a delete
604 * request, break if the oplock around is a batch oplock. If it's another
605 * requested access type, break.
607 * 3) Only level2 around: Grant level2 and do nothing else.
610 static BOOL delay_for_oplocks(struct share_mode_lock *lck, files_struct *fsp)
613 struct share_mode_entry *exclusive = NULL;
614 BOOL delay_it = False;
615 BOOL have_level2 = False;
617 if (is_stat_open(fsp->access_mask)) {
618 fsp->oplock_type = NO_OPLOCK;
622 if (lck->num_share_modes == 0) {
623 /* No files open at all: Directly grant whatever the client
626 if (fsp->oplock_type == NO_OPLOCK) {
627 /* Store a level2 oplock, but don't tell the client */
628 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
633 for (i=0; i<lck->num_share_modes; i++) {
635 if (!is_valid_share_mode_entry(&lck->share_modes[i])) {
639 if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) {
640 SMB_ASSERT(exclusive == NULL);
641 exclusive = &lck->share_modes[i];
644 if (lck->share_modes[i].op_type == LEVEL_II_OPLOCK) {
649 if (exclusive != NULL) { /* Found an exclusive oplock */
650 SMB_ASSERT(!have_level2);
651 delay_it = is_delete_request(fsp) ?
652 BATCH_OPLOCK_TYPE(exclusive->op_type) : True;
655 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
656 /* We can at most grant level2 */
657 fsp->oplock_type = LEVEL_II_OPLOCK;
660 if ((fsp->oplock_type == NO_OPLOCK) && have_level2) {
661 /* Store a level2 oplock, but don't tell the client */
662 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
667 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
669 DEBUG(10, ("Sending break request to PID %s\n",
670 procid_str_static(&exclusive->pid)));
671 exclusive->op_mid = get_current_mid();
673 share_mode_entry_to_message(msg, exclusive);
676 ret = message_send_pid(exclusive->pid, MSG_SMB_BREAK_REQUEST,
677 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
680 DEBUG(3, ("Could not send oplock break message\n"));
688 static BOOL request_timed_out(struct timeval request_time,
689 struct timeval timeout)
691 struct timeval now, end_time;
693 end_time = timeval_sum(&request_time, &timeout);
694 return (timeval_compare(&end_time, &now) < 0);
697 /****************************************************************************
698 Handle the 1 second delay in returning a SHARING_VIOLATION error.
699 ****************************************************************************/
701 static void defer_open(struct share_mode_lock *lck,
702 struct timeval request_time,
703 struct timeval timeout,
704 struct deferred_open_record *state)
706 uint16 mid = get_current_mid();
711 for (i=0; i<lck->num_share_modes; i++) {
712 struct share_mode_entry *e = &lck->share_modes[i];
714 if (!is_deferred_open_entry(e)) {
718 if (procid_is_me(&e->pid) && (e->op_mid == mid)) {
719 DEBUG(0, ("Trying to defer an already deferred "
720 "request: mid=%d, exiting\n", mid));
721 exit_server("exiting");
725 /* End paranoia check */
727 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
728 "open entry for mid %u\n",
729 (unsigned int)request_time.tv_sec,
730 (unsigned int)request_time.tv_usec,
733 if (!push_deferred_smb_message(mid, request_time, timeout,
734 (char *)state, sizeof(*state))) {
735 exit_server("push_deferred_smb_message failed\n");
737 add_deferred_open(lck, mid, request_time, state->dev, state->inode);
740 * Push the MID of this packet on the signing queue.
741 * We only do this once, the first time we push the packet
742 * onto the deferred open queue, as this has a side effect
743 * of incrementing the response sequence number.
746 srv_defer_sign_response(mid);
749 /****************************************************************************
750 Set a kernel flock on a file for NFS interoperability.
751 This requires a patch to Linux.
752 ****************************************************************************/
754 static void kernel_flock(files_struct *fsp, uint32 share_mode)
756 #if HAVE_KERNEL_SHARE_MODES
758 if (share_mode == FILE_SHARE_WRITE) {
759 kernel_mode = LOCK_MAND|LOCK_WRITE;
760 } else if (share_mode == FILE_SHARE_READ) {
761 kernel_mode = LOCK_MAND|LOCK_READ;
762 } else if (share_mode == FILE_SHARE_NONE) {
763 kernel_mode = LOCK_MAND;
766 flock(fsp->fh->fd, kernel_mode);
772 /****************************************************************************
773 On overwrite open ensure that the attributes match.
774 ****************************************************************************/
776 static BOOL open_match_attributes(connection_struct *conn,
780 mode_t existing_unx_mode,
782 mode_t *returned_unx_mode)
784 uint32 noarch_old_dos_attr, noarch_new_dos_attr;
786 noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
787 noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE);
789 if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) ||
790 (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) {
791 *returned_unx_mode = new_unx_mode;
793 *returned_unx_mode = (mode_t)0;
796 DEBUG(10,("open_match_attributes: file %s old_dos_attr = 0x%x, "
797 "existing_unx_mode = 0%o, new_dos_attr = 0x%x "
798 "returned_unx_mode = 0%o\n",
800 (unsigned int)old_dos_attr,
801 (unsigned int)existing_unx_mode,
802 (unsigned int)new_dos_attr,
803 (unsigned int)*returned_unx_mode ));
805 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
806 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
807 if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) &&
808 !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) {
812 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
813 if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) &&
814 !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) {
821 /****************************************************************************
822 Special FCB or DOS processing in the case of a sharing violation.
823 Try and find a duplicated file handle.
824 ****************************************************************************/
826 static files_struct *fcb_or_dos_open(connection_struct *conn,
827 const char *fname, SMB_DEV_T dev,
831 uint32 create_options)
834 files_struct *dup_fsp;
836 DEBUG(5,("fcb_or_dos_open: attempting old open semantics for "
837 "file %s.\n", fname ));
839 for(fsp = file_find_di_first(dev, inode); fsp;
840 fsp = file_find_di_next(fsp)) {
842 DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, "
843 "vuid = %u, file_pid = %u, private_options = 0x%x "
844 "access_mask = 0x%x\n", fsp->fsp_name,
845 fsp->fh->fd, (unsigned int)fsp->vuid,
846 (unsigned int)fsp->file_pid,
847 (unsigned int)fsp->fh->private_options,
848 (unsigned int)fsp->access_mask ));
850 if (fsp->fh->fd != -1 &&
851 fsp->vuid == current_user.vuid &&
852 fsp->file_pid == global_smbpid &&
853 (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS |
854 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) &&
855 (fsp->access_mask & FILE_WRITE_DATA) &&
856 strequal(fsp->fsp_name, fname)) {
857 DEBUG(10,("fcb_or_dos_open: file match\n"));
866 /* quite an insane set of semantics ... */
867 if (is_executable(fname) &&
868 (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) {
869 DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n"));
873 /* We need to duplicate this fsp. */
874 dup_fsp = dup_file_fsp(fsp, access_mask, share_access, create_options);
882 /****************************************************************************
883 Open a file with a share mode - old openX method - map into NTCreate.
884 ****************************************************************************/
886 BOOL map_open_params_to_ntcreate(const char *fname, int deny_mode, int open_func,
887 uint32 *paccess_mask,
889 uint32 *pcreate_disposition,
890 uint32 *pcreate_options)
894 uint32 create_disposition;
895 uint32 create_options = 0;
897 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
898 "open_func = 0x%x\n",
899 fname, (unsigned int)deny_mode, (unsigned int)open_func ));
901 /* Create the NT compatible access_mask. */
902 switch (GET_OPENX_MODE(deny_mode)) {
903 case DOS_OPEN_EXEC: /* Implies read-only - used to be FILE_READ_DATA */
904 case DOS_OPEN_RDONLY:
905 access_mask = FILE_GENERIC_READ;
907 case DOS_OPEN_WRONLY:
908 access_mask = FILE_GENERIC_WRITE;
912 access_mask = FILE_GENERIC_READ|FILE_GENERIC_WRITE;
915 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
916 (unsigned int)GET_OPENX_MODE(deny_mode)));
920 /* Create the NT compatible create_disposition. */
922 case OPENX_FILE_EXISTS_FAIL|OPENX_FILE_CREATE_IF_NOT_EXIST:
923 create_disposition = FILE_CREATE;
926 case OPENX_FILE_EXISTS_OPEN:
927 create_disposition = FILE_OPEN;
930 case OPENX_FILE_EXISTS_OPEN|OPENX_FILE_CREATE_IF_NOT_EXIST:
931 create_disposition = FILE_OPEN_IF;
934 case OPENX_FILE_EXISTS_TRUNCATE:
935 create_disposition = FILE_OVERWRITE;
938 case OPENX_FILE_EXISTS_TRUNCATE|OPENX_FILE_CREATE_IF_NOT_EXIST:
939 create_disposition = FILE_OVERWRITE_IF;
943 /* From samba4 - to be confirmed. */
944 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_EXEC) {
945 create_disposition = FILE_CREATE;
948 DEBUG(10,("map_open_params_to_ntcreate: bad "
949 "open_func 0x%x\n", (unsigned int)open_func));
953 /* Create the NT compatible share modes. */
954 switch (GET_DENY_MODE(deny_mode)) {
956 share_mode = FILE_SHARE_NONE;
960 share_mode = FILE_SHARE_READ;
964 share_mode = FILE_SHARE_WRITE;
968 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
972 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
973 if (is_executable(fname)) {
974 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
976 if (GET_OPENX_MODE(deny_mode) == DOS_OPEN_RDONLY) {
977 share_mode = FILE_SHARE_READ;
979 share_mode = FILE_SHARE_NONE;
985 create_options |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
986 share_mode = FILE_SHARE_NONE;
990 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
991 (unsigned int)GET_DENY_MODE(deny_mode) ));
995 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
996 "share_mode = 0x%x, create_disposition = 0x%x, "
997 "create_options = 0x%x\n",
999 (unsigned int)access_mask,
1000 (unsigned int)share_mode,
1001 (unsigned int)create_disposition,
1002 (unsigned int)create_options ));
1005 *paccess_mask = access_mask;
1008 *pshare_mode = share_mode;
1010 if (pcreate_disposition) {
1011 *pcreate_disposition = create_disposition;
1013 if (pcreate_options) {
1014 *pcreate_options = create_options;
1021 /* Map generic permissions to file object specific permissions */
1023 struct generic_mapping file_generic_mapping = {
1026 FILE_GENERIC_EXECUTE,
1030 /****************************************************************************
1031 Open a file with a share mode.
1032 ****************************************************************************/
1034 files_struct *open_file_ntcreate(connection_struct *conn,
1036 SMB_STRUCT_STAT *psbuf,
1037 uint32 access_mask, /* access bits (FILE_READ_DATA etc.) */
1038 uint32 share_access, /* share constants (FILE_SHARE_READ etc). */
1039 uint32 create_disposition, /* FILE_OPEN_IF etc. */
1040 uint32 create_options, /* options such as delete on close. */
1041 uint32 new_dos_attributes, /* attributes used for new file. */
1042 int oplock_request, /* internal Samba oplock codes. */
1043 /* Information (FILE_EXISTS etc.) */
1048 BOOL file_existed = VALID_STAT(*psbuf);
1049 BOOL def_acl = False;
1050 BOOL internal_only_open = False;
1052 SMB_INO_T inode = 0;
1053 BOOL fsp_open = False;
1054 files_struct *fsp = NULL;
1055 mode_t new_unx_mode = (mode_t)0;
1056 mode_t unx_mode = (mode_t)0;
1058 uint32 existing_dos_attributes = 0;
1059 struct pending_message_list *pml = NULL;
1060 uint16 mid = get_current_mid();
1061 BOOL delayed_for_oplocks = False;
1062 struct timeval request_time = timeval_zero();
1063 struct share_mode_lock *lck = NULL;
1066 if (conn->printer) {
1068 * Printers are handled completely differently.
1069 * Most of the passed parameters are ignored.
1073 *pinfo = FILE_WAS_CREATED;
1076 DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", fname));
1078 return print_fsp_open(conn, fname);
1081 /* We add aARCH to this as this mode is only used if the file is
1083 unx_mode = unix_mode(conn, new_dos_attributes | aARCH,fname, True);
1085 DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x "
1086 "access_mask=0x%x share_access=0x%x "
1087 "create_disposition = 0x%x create_options=0x%x "
1088 "unix mode=0%o oplock_request=%d\n",
1089 fname, new_dos_attributes, access_mask, share_access,
1090 create_disposition, create_options, unx_mode,
1093 if (oplock_request == INTERNAL_OPEN_ONLY) {
1094 internal_only_open = True;
1098 if ((pml = get_open_deferred_message(mid)) != NULL) {
1099 struct deferred_open_record *state =
1100 (struct deferred_open_record *)pml->private_data.data;
1102 request_time = pml->request_time;
1103 delayed_for_oplocks = state->delayed_for_oplocks;
1105 /* There could be a race condition where the dev/inode pair
1106 has changed since we deferred the message. If so, just
1107 remove the deferred open entry and return sharing
1110 /* If the timeout value is non-zero, we need to just return
1111 sharing violation. Don't retry the open as we were not
1112 notified of a close and we don't want to trigger another
1113 spurious oplock break. */
1115 /* Now remove the deferred open entry under lock. */
1116 lck = get_share_mode_lock(NULL, state->dev, state->inode, NULL, NULL);
1118 DEBUG(0, ("could not get share mode lock\n"));
1120 del_deferred_open_entry(lck, mid);
1121 talloc_destroy(lck);
1124 /* Ensure we don't reprocess this message. */
1125 remove_deferred_open_smb_message(mid);
1128 if (!check_name(fname,conn)) {
1132 new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
1134 existing_dos_attributes = dos_mode(conn, fname, psbuf);
1137 /* ignore any oplock requests if oplocks are disabled */
1138 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
1139 IS_VETO_OPLOCK_PATH(conn, fname)) {
1143 /* this is for OS/2 long file names - say we don't support them */
1144 if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
1145 /* OS/2 Workplace shell fix may be main code stream in a later
1147 set_saved_error_triple(ERRDOS, ERRcannotopen,
1148 NT_STATUS_OBJECT_NAME_NOT_FOUND);
1149 DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not "
1154 switch( create_disposition ) {
1156 * Currently we're using FILE_SUPERSEDE as the same as
1157 * FILE_OVERWRITE_IF but they really are
1158 * different. FILE_SUPERSEDE deletes an existing file
1159 * (requiring delete access) then recreates it.
1161 case FILE_SUPERSEDE:
1162 /* If file exists replace/overwrite. If file doesn't
1164 flags2 |= (O_CREAT | O_TRUNC);
1167 case FILE_OVERWRITE_IF:
1168 /* If file exists replace/overwrite. If file doesn't
1170 flags2 |= (O_CREAT | O_TRUNC);
1174 /* If file exists open. If file doesn't exist error. */
1175 if (!file_existed) {
1176 DEBUG(5,("open_file_ntcreate: FILE_OPEN "
1177 "requested for file %s and file "
1178 "doesn't exist.\n", fname ));
1179 set_saved_ntstatus(NT_STATUS_OBJECT_NAME_NOT_FOUND);
1185 case FILE_OVERWRITE:
1186 /* If file exists overwrite. If file doesn't exist
1188 if (!file_existed) {
1189 DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE "
1190 "requested for file %s and file "
1191 "doesn't exist.\n", fname ));
1192 set_saved_ntstatus(NT_STATUS_OBJECT_NAME_NOT_FOUND);
1200 /* If file exists error. If file doesn't exist
1203 DEBUG(5,("open_file_ntcreate: FILE_CREATE "
1204 "requested for file %s and file "
1205 "already exists.\n", fname ));
1206 if (S_ISDIR(psbuf->st_mode)) {
1213 flags2 |= (O_CREAT|O_EXCL);
1217 /* If file exists open. If file doesn't exist
1223 set_saved_ntstatus(NT_STATUS_INVALID_PARAMETER);
1227 /* We only care about matching attributes on file exists and
1230 if (file_existed && ((create_disposition == FILE_OVERWRITE) ||
1231 (create_disposition == FILE_OVERWRITE_IF))) {
1232 if (!open_match_attributes(conn, fname,
1233 existing_dos_attributes,
1234 new_dos_attributes, psbuf->st_mode,
1235 unx_mode, &new_unx_mode)) {
1236 DEBUG(5,("open_file_ntcreate: attributes missmatch "
1237 "for file %s (%x %x) (0%o, 0%o)\n",
1238 fname, existing_dos_attributes,
1240 (unsigned int)psbuf->st_mode,
1241 (unsigned int)unx_mode ));
1247 /* This is a nasty hack - must fix... JRA. */
1248 if (access_mask == MAXIMUM_ALLOWED_ACCESS) {
1249 access_mask = FILE_GENERIC_ALL;
1253 * Convert GENERIC bits to specific bits.
1256 se_map_generic(&access_mask, &file_generic_mapping);
1258 DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping "
1259 "access_mask=0x%x\n", fname, access_mask ));
1262 * Note that we ignore the append flag as append does not
1263 * mean the same thing under DOS and Unix.
1266 if (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) {
1273 * Currently we only look at FILE_WRITE_THROUGH for create options.
1277 if (create_options & FILE_WRITE_THROUGH) {
1282 if (!CAN_WRITE(conn)) {
1284 * We should really return a permission denied error if either
1285 * O_CREAT or O_TRUNC are set, but for compatibility with
1286 * older versions of Samba we just AND them out.
1288 flags2 &= ~(O_CREAT|O_TRUNC);
1292 * Ensure we can't write on a read-only share or file.
1295 if (flags != O_RDONLY && file_existed &&
1296 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
1297 DEBUG(5,("open_file_ntcreate: write access requested for "
1298 "file %s on read only %s\n",
1299 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1300 set_saved_ntstatus(NT_STATUS_ACCESS_DENIED);
1305 fsp = file_new(conn);
1310 fsp->dev = psbuf->st_dev;
1311 fsp->inode = psbuf->st_ino;
1312 fsp->share_access = share_access;
1313 fsp->fh->private_options = create_options;
1314 fsp->access_mask = access_mask;
1315 fsp->oplock_type = oplock_request;
1317 if (timeval_is_zero(&request_time)) {
1318 request_time = fsp->open_time;
1323 dev = psbuf->st_dev;
1324 inode = psbuf->st_ino;
1326 lck = get_share_mode_lock(NULL, dev, inode,
1331 DEBUG(0, ("Could not get share mode lock\n"));
1332 set_saved_ntstatus(NT_STATUS_SHARING_VIOLATION);
1336 if (delay_for_oplocks(lck, fsp)) {
1337 struct deferred_open_record state;
1338 struct timeval timeout;
1340 if (delayed_for_oplocks) {
1341 DEBUG(0, ("Trying to delay for oplocks "
1343 exit_server("exiting");
1346 timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0);
1348 /* Normally the smbd we asked should respond within
1349 * OPLOCK_BREAK_TIMEOUT seconds regardless of whether
1350 * the client did, give twice the timeout as a safety
1351 * measure here in case the other smbd is stuck
1352 * somewhere else. */
1354 state.delayed_for_oplocks = True;
1356 state.inode = inode;
1358 if (!request_timed_out(request_time, timeout)) {
1359 defer_open(lck, request_time, timeout,
1367 status = open_mode_check(conn, fname, lck,
1368 access_mask, share_access,
1369 create_options, &file_existed);
1371 if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) {
1372 /* DELETE_PENDING is not deferred for a second */
1373 set_saved_ntstatus(status);
1379 if (!NT_STATUS_IS_OK(status)) {
1381 SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION));
1383 /* Check if this can be done with the deny_dos and fcb
1385 if (create_options &
1386 (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
1387 NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
1388 files_struct *fsp_dup;
1389 fsp_dup = fcb_or_dos_open(conn, fname, dev,
1398 *pinfo = FILE_WAS_OPENED;
1400 conn->num_files_open++;
1406 * This next line is a subtlety we need for
1407 * MS-Access. If a file open will fail due to share
1408 * permissions and also for security (access) reasons,
1409 * we need to return the access failed error, not the
1410 * share error. This means we must attempt to open the
1411 * file anyway in order to get the UNIX access error -
1412 * even if we're going to fail the open for share
1413 * reasons. This is bad, as we're burning another fd
1414 * if there are existing locks but there's nothing
1415 * else we can do. We also ensure we're not going to
1416 * create or tuncate the file as we only want an
1417 * access decision at this stage. JRA.
1420 fsp_open = open_file(fsp,conn,fname,psbuf,
1421 flags|(flags2&~(O_TRUNC|O_CREAT)),
1422 unx_mode,access_mask);
1424 DEBUG(4,("open_file_ntcreate : share_mode deny - "
1425 "calling open_file with flags=0x%X "
1426 "flags2=0x%X mode=0%o returned %d\n",
1427 flags, (flags2&~(O_TRUNC|O_CREAT)),
1428 (unsigned int)unx_mode, (int)fsp_open ));
1430 if (!fsp_open && errno) {
1431 /* Default error. */
1432 set_saved_ntstatus(NT_STATUS_ACCESS_DENIED);
1436 * If we're returning a share violation, ensure we
1437 * cope with the braindead 1 second delay.
1440 if (!internal_only_open &&
1441 lp_defer_sharing_violations()) {
1442 struct timeval timeout;
1443 struct deferred_open_record state;
1445 timeout = timeval_set(0, SHARING_VIOLATION_USEC_WAIT);
1447 state.delayed_for_oplocks = False;
1449 state.inode = inode;
1451 if (!request_timed_out(request_time,
1453 defer_open(lck, request_time, timeout,
1460 fd_close(conn, fsp);
1462 * We have detected a sharing violation here
1463 * so return the correct error code
1465 set_saved_ntstatus(NT_STATUS_SHARING_VIOLATION);
1472 * We exit this block with the share entry *locked*.....
1476 SMB_ASSERT(!file_existed || (lck != NULL));
1479 * Ensure we pay attention to default ACLs on directories if required.
1482 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1483 (def_acl = directory_has_default_acl(conn,
1484 parent_dirname(fname)))) {
1488 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
1489 (unsigned int)flags, (unsigned int)flags2,
1490 (unsigned int)unx_mode));
1493 * open_file strips any O_TRUNC flags itself.
1496 fsp_open = open_file(fsp,conn,fname,psbuf,flags|flags2,unx_mode,
1507 if (!file_existed) {
1510 * Deal with the race condition where two smbd's detect the
1511 * file doesn't exist and do the create at the same time. One
1512 * of them will win and set a share mode, the other (ie. this
1513 * one) should check if the requested share mode for this
1514 * create is allowed.
1518 * Now the file exists and fsp is successfully opened,
1519 * fsp->dev and fsp->inode are valid and should replace the
1520 * dev=0,inode=0 from a non existent file. Spotted by
1521 * Nadav Danieli <nadavd@exanet.com>. JRA.
1527 lck = get_share_mode_lock(NULL, dev, inode,
1532 DEBUG(0, ("open_file_ntcreate: Could not get share mode lock for %s\n", fname));
1533 fd_close(conn, fsp);
1535 set_saved_ntstatus(NT_STATUS_SHARING_VIOLATION);
1539 status = open_mode_check(conn, fname, lck,
1540 access_mask, share_access,
1541 create_options, &file_existed);
1543 if (!NT_STATUS_IS_OK(status)) {
1544 struct deferred_open_record state;
1546 fd_close(conn, fsp);
1549 state.delayed_for_oplocks = False;
1551 state.inode = inode;
1553 /* Do it all over again immediately. In the second
1554 * round we will find that the file existed and handle
1555 * the DELETE_PENDING and FCB cases correctly. No need
1556 * to duplicate the code here. Essentially this is a
1557 * "goto top of this function", but don't tell
1560 defer_open(lck, request_time, timeval_zero(),
1567 * We exit this block with the share entry *locked*.....
1571 SMB_ASSERT(lck != NULL);
1573 /* note that we ignore failure for the following. It is
1574 basically a hack for NFS, and NFS will never set one of
1575 these only read them. Nobody but Samba can ever set a deny
1576 mode and we have already checked our more authoritative
1577 locking database for permission to set this deny mode. If
1578 the kernel refuses the operations then the kernel is wrong */
1580 kernel_flock(fsp, share_access);
1583 * At this point onwards, we can guarentee that the share entry
1584 * is locked, whether we created the file or not, and that the
1585 * deny mode is compatible with all current opens.
1589 * If requested, truncate the file.
1592 if (flags2&O_TRUNC) {
1594 * We are modifing the file after open - update the stat
1597 if ((SMB_VFS_FTRUNCATE(fsp,fsp->fh->fd,0) == -1) ||
1598 (SMB_VFS_FSTAT(fsp,fsp->fh->fd,psbuf)==-1)) {
1606 /* Record the options we were opened with. */
1607 fsp->share_access = share_access;
1608 fsp->fh->private_options = create_options;
1609 fsp->access_mask = access_mask;
1612 if (!(flags2 & O_TRUNC)) {
1613 info = FILE_WAS_OPENED;
1615 info = FILE_WAS_OVERWRITTEN;
1618 info = FILE_WAS_CREATED;
1619 /* Change the owner if required. */
1620 if (lp_inherit_owner(SNUM(conn))) {
1621 change_owner_to_parent(conn, fsp, fsp->fsp_name,
1631 * Setup the oplock info in both the shared memory and
1635 if ((fsp->oplock_type != NO_OPLOCK) &&
1636 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK)) {
1637 if (!set_file_oplock(fsp, fsp->oplock_type)) {
1638 /* Could not get the kernel oplock */
1639 fsp->oplock_type = NO_OPLOCK;
1642 set_share_mode(lck, fsp, 0, fsp->oplock_type);
1644 if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED ||
1645 info == FILE_WAS_SUPERSEDED) {
1647 /* Handle strange delete on close create semantics. */
1648 if (create_options & FILE_DELETE_ON_CLOSE) {
1649 NTSTATUS result = can_set_delete_on_close(fsp, True, new_dos_attributes);
1651 if (!NT_STATUS_IS_OK(result)) {
1652 /* Remember to delete the mode we just added. */
1653 del_share_mode(lck, fsp);
1657 set_saved_ntstatus(result);
1660 /* Note that here we set the *inital* delete on close flag,
1661 not the regular one. */
1662 set_delete_on_close_token(lck, ¤t_user.ut);
1663 lck->initial_delete_on_close = True;
1664 lck->modified = True;
1667 /* Files should be initially set as archive */
1668 if (lp_map_archive(SNUM(conn)) ||
1669 lp_store_dos_attributes(SNUM(conn))) {
1670 file_set_dosmode(conn, fname,
1671 new_dos_attributes | aARCH, NULL,
1677 * Take care of inherited ACLs on created files - if default ACL not
1681 if (!file_existed && !def_acl) {
1683 int saved_errno = errno; /* We might get ENOSYS in the next
1686 if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fh->fd, unx_mode) == -1
1687 && errno == ENOSYS) {
1688 errno = saved_errno; /* Ignore ENOSYS */
1691 } else if (new_unx_mode) {
1695 /* Attributes need changing. File already existed. */
1698 int saved_errno = errno; /* We might get ENOSYS in the
1700 ret = SMB_VFS_FCHMOD_ACL(fsp, fsp->fh->fd,
1703 if (ret == -1 && errno == ENOSYS) {
1704 errno = saved_errno; /* Ignore ENOSYS */
1706 DEBUG(5, ("open_file_shared: reset "
1707 "attributes of file %s to 0%o\n",
1708 fname, (unsigned int)new_unx_mode));
1709 ret = 0; /* Don't do the fchmod below. */
1714 (SMB_VFS_FCHMOD(fsp, fsp->fh->fd, new_unx_mode) == -1))
1715 DEBUG(5, ("open_file_shared: failed to reset "
1716 "attributes of file %s to 0%o\n",
1717 fname, (unsigned int)new_unx_mode));
1720 /* If this is a successful open, we must remove any deferred open
1722 del_deferred_open_entry(lck, mid);
1725 conn->num_files_open++;
1730 /****************************************************************************
1731 Open a file for for write to ensure that we can fchmod it.
1732 ****************************************************************************/
1734 files_struct *open_file_fchmod(connection_struct *conn, const char *fname,
1735 SMB_STRUCT_STAT *psbuf)
1737 files_struct *fsp = NULL;
1740 if (!VALID_STAT(*psbuf)) {
1744 fsp = file_new(conn);
1749 /* note! we must use a non-zero desired access or we don't get
1750 a real file descriptor. Oh what a twisted web we weave. */
1751 fsp_open = open_file(fsp,conn,fname,psbuf,O_WRONLY,0,FILE_WRITE_DATA);
1754 * This is not a user visible file open.
1755 * Don't set a share mode and don't increment
1756 * the conn->num_files_open.
1767 /****************************************************************************
1768 Close the fchmod file fd - ensure no locks are lost.
1769 ****************************************************************************/
1771 int close_file_fchmod(files_struct *fsp)
1773 int ret = fd_close(fsp->conn, fsp);
1778 /****************************************************************************
1779 Open a directory from an NT SMB call.
1780 ****************************************************************************/
1782 files_struct *open_directory(connection_struct *conn,
1784 SMB_STRUCT_STAT *psbuf,
1786 uint32 share_access,
1787 uint32 create_disposition,
1788 uint32 create_options,
1791 files_struct *fsp = NULL;
1792 BOOL dir_existed = VALID_STAT(*psbuf) ? True : False;
1793 BOOL create_dir = False;
1794 struct share_mode_lock *lck = NULL;
1798 DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, "
1799 "share_access = 0x%x create_options = 0x%x, "
1800 "create_disposition = 0x%x\n",
1802 (unsigned int)access_mask,
1803 (unsigned int)share_access,
1804 (unsigned int)create_options,
1805 (unsigned int)create_disposition));
1807 if (is_ntfs_stream_name(fname)) {
1808 DEBUG(0,("open_directory: %s is a stream name!\n", fname ));
1809 set_saved_ntstatus(NT_STATUS_NOT_A_DIRECTORY);
1813 if (dir_existed && !S_ISDIR(psbuf->st_mode)) {
1814 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1815 set_saved_ntstatus(NT_STATUS_NOT_A_DIRECTORY);
1819 switch( create_disposition ) {
1821 /* If directory exists open. If directory doesn't
1824 DEBUG(5,("open_directory: FILE_OPEN requested "
1825 "for directory %s and it doesn't "
1826 "exist.\n", fname ));
1827 set_saved_ntstatus(NT_STATUS_OBJECT_NAME_NOT_FOUND);
1830 info = FILE_WAS_OPENED;
1834 /* If directory exists error. If directory doesn't
1837 DEBUG(5,("open_directory: FILE_CREATE "
1838 "requested for directory %s and it "
1839 "already exists.\n", fname ));
1840 set_saved_error_triple(ERRDOS, ERRfilexists,
1841 NT_STATUS_OBJECT_NAME_COLLISION);
1845 info = FILE_WAS_CREATED;
1849 /* If directory exists open. If directory doesn't
1853 info = FILE_WAS_CREATED;
1855 info = FILE_WAS_OPENED;
1859 case FILE_SUPERSEDE:
1860 case FILE_OVERWRITE:
1861 case FILE_OVERWRITE_IF:
1863 DEBUG(5,("open_directory: invalid create_disposition "
1864 "0x%x for directory %s\n",
1865 (unsigned int)create_disposition, fname));
1867 set_saved_ntstatus(NT_STATUS_INVALID_PARAMETER);
1873 * Try and create the directory.
1876 /* We know bad_path is false as it's caught earlier. */
1878 status = mkdir_internal(conn, fname, False);
1880 if (!NT_STATUS_IS_OK(status)) {
1881 DEBUG(2,("open_directory: unable to create %s. "
1882 "Error was %s\n", fname, strerror(errno) ));
1883 /* Ensure we return the correct NT status to the
1885 set_saved_error_triple(0, 0, status);
1889 /* Ensure we're checking for a symlink here.... */
1890 /* We don't want to get caught by a symlink racer. */
1892 if(SMB_VFS_LSTAT(conn,fname, psbuf) != 0) {
1896 if(!S_ISDIR(psbuf->st_mode)) {
1897 DEBUG(0,("open_directory: %s is not a directory !\n",
1903 fsp = file_new(conn);
1909 * Setup the files_struct for it.
1912 fsp->mode = psbuf->st_mode;
1913 fsp->inode = psbuf->st_ino;
1914 fsp->dev = psbuf->st_dev;
1915 fsp->vuid = current_user.vuid;
1916 fsp->file_pid = global_smbpid;
1917 fsp->can_lock = True;
1918 fsp->can_read = False;
1919 fsp->can_write = False;
1921 fsp->share_access = share_access;
1922 fsp->fh->private_options = create_options;
1923 fsp->access_mask = access_mask;
1925 fsp->print_file = False;
1926 fsp->modified = False;
1927 fsp->oplock_type = NO_OPLOCK;
1928 fsp->sent_oplock_break = NO_BREAK_SENT;
1929 fsp->is_directory = True;
1930 fsp->is_stat = False;
1931 string_set(&fsp->fsp_name,fname);
1933 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode,
1938 DEBUG(0, ("open_directory: Could not get share mode lock for %s\n", fname));
1940 set_saved_ntstatus(NT_STATUS_SHARING_VIOLATION);
1944 status = open_mode_check(conn, fname, lck,
1945 access_mask, share_access,
1946 create_options, &dir_existed);
1948 if (!NT_STATUS_IS_OK(status)) {
1949 set_saved_ntstatus(status);
1955 set_share_mode(lck, fsp, 0, NO_OPLOCK);
1957 /* For directories the delete on close bit at open time seems
1958 always to be honored on close... See test 19 in Samba4 BASE-DELETE. */
1959 if (create_options & FILE_DELETE_ON_CLOSE) {
1960 status = can_set_delete_on_close(fsp, True, 0);
1961 if (!NT_STATUS_IS_OK(status)) {
1962 set_saved_ntstatus(status);
1968 set_delete_on_close_token(lck, ¤t_user.ut);
1969 lck->initial_delete_on_close = True;
1970 lck->modified = True;
1975 /* Change the owner if required. */
1976 if ((info == FILE_WAS_CREATED) && lp_inherit_owner(SNUM(conn))) {
1977 change_owner_to_parent(conn, fsp, fsp->fsp_name, psbuf);
1984 conn->num_files_open++;
1989 /****************************************************************************
1990 Open a pseudo-file (no locking checks - a 'stat' open).
1991 ****************************************************************************/
1993 files_struct *open_file_stat(connection_struct *conn, char *fname,
1994 SMB_STRUCT_STAT *psbuf)
1996 files_struct *fsp = NULL;
1998 if (!VALID_STAT(*psbuf))
2001 /* Can't 'stat' open directories. */
2002 if(S_ISDIR(psbuf->st_mode))
2005 fsp = file_new(conn);
2009 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
2012 * Setup the files_struct for it.
2015 fsp->mode = psbuf->st_mode;
2016 fsp->inode = psbuf->st_ino;
2017 fsp->dev = psbuf->st_dev;
2018 fsp->vuid = current_user.vuid;
2019 fsp->file_pid = global_smbpid;
2020 fsp->can_lock = False;
2021 fsp->can_read = False;
2022 fsp->can_write = False;
2023 fsp->print_file = False;
2024 fsp->modified = False;
2025 fsp->oplock_type = NO_OPLOCK;
2026 fsp->sent_oplock_break = NO_BREAK_SENT;
2027 fsp->is_directory = False;
2028 fsp->is_stat = True;
2029 string_set(&fsp->fsp_name,fname);
2031 conn->num_files_open++;
2036 /****************************************************************************
2037 Receive notification that one of our open files has been renamed by another
2039 ****************************************************************************/
2041 void msg_file_was_renamed(int msg_type, struct process_id src, void *buf, size_t len)
2044 char *frm = (char *)buf;
2047 const char *sharepath;
2048 const char *newname;
2051 if (buf == NULL || len < MSG_FILE_RENAMED_MIN_SIZE + 2) {
2052 DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n", (int)len));
2056 /* Unpack the message. */
2057 dev = DEV_T_VAL(frm,0);
2058 inode = INO_T_VAL(frm,8);
2059 sharepath = &frm[16];
2060 newname = sharepath + strlen(sharepath) + 1;
2061 sp_len = strlen(sharepath);
2063 DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, "
2064 "dev %x, inode %.0f\n",
2065 sharepath, newname, (unsigned int)dev, (double)inode ));
2067 for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
2068 if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) {
2069 DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n",
2070 fsp->fnum, fsp->fsp_name, newname ));
2071 string_set(&fsp->fsp_name, newname);
2074 /* Now we have the complete path we can work out if this is
2075 actually within this share and adjust newname accordingly. */
2076 DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s "
2077 "not sharepath %s) "
2078 "fnum %d from %s -> %s\n",
2079 fsp->conn->connectpath,