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
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern struct current_user current_user;
25 extern userdom_struct current_user_info;
26 extern uint16 global_oplock_port;
27 extern uint16 global_smbpid;
28 extern BOOL global_client_failed_oplock_break;
30 struct dev_inode_bundle {
35 /****************************************************************************
36 fd support routines - attempt to do a dos_open.
37 ****************************************************************************/
39 static int fd_open(struct connection_struct *conn, const char *fname,
40 int flags, mode_t mode)
44 if (!lp_symlinks(SNUM(conn)))
48 fd = SMB_VFS_OPEN(conn,fname,flags,mode);
50 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n", fname,
51 flags, (int)mode, fd, (fd == -1) ? strerror(errno) : "" ));
56 /****************************************************************************
57 Close the file associated with a fsp.
58 ****************************************************************************/
60 int fd_close(struct connection_struct *conn, files_struct *fsp)
63 return 0; /* what we used to call a stat open. */
64 return fd_close_posix(conn, fsp);
68 /****************************************************************************
69 Check a filename for the pipe string.
70 ****************************************************************************/
72 static void check_for_pipe(const char *fname)
74 /* special case of pipe opens */
76 StrnCpy(s,fname,sizeof(s)-1);
78 if (strstr(s,"pipe/")) {
79 DEBUG(3,("Rejecting named pipe open for %s\n",fname));
80 set_saved_error_triple(ERRSRV, ERRaccess, NT_STATUS_ACCESS_DENIED);
84 /****************************************************************************
85 Change the ownership of a file to that of the parent directory.
86 Do this by fd if possible.
87 ****************************************************************************/
89 void change_owner_to_parent(connection_struct *conn, files_struct *fsp, const char *fname, SMB_STRUCT_STAT *psbuf)
91 const char *parent_path = parent_dirname(fname);
92 SMB_STRUCT_STAT parent_st;
95 ret = SMB_VFS_STAT(conn, parent_path, &parent_st);
97 DEBUG(0,("change_owner_to_parent: failed to stat parent directory %s. Error was %s\n",
98 parent_path, strerror(errno) ));
102 if (fsp && fsp->fd != -1) {
104 ret = SMB_VFS_FCHOWN(fsp, fsp->fd, parent_st.st_uid, (gid_t)-1);
107 DEBUG(0,("change_owner_to_parent: failed to fchown file %s to parent directory uid %u. \
109 fname, (unsigned int)parent_st.st_uid, strerror(errno) ));
112 DEBUG(10,("change_owner_to_parent: changed new file %s to parent directory uid %u.\n",
113 fname, (unsigned int)parent_st.st_uid ));
116 /* We've already done an lstat into psbuf, and we know it's a directory. If
117 we can cd into the directory and the dev/ino are the same then we can safely
118 chown without races as we're locking the directory in place by being in it.
119 This 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 current working directory\n"));
130 /* Chdir into the new path. */
131 if (vfs_ChDir(conn, fname) == -1) {
132 DEBUG(0,("change_owner_to_parent: failed to change current working directory to %s. \
133 Error was %s\n", fname, strerror(errno) ));
137 if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
138 DEBUG(0,("change_owner_to_parent: failed to stat directory '.' (%s) \
139 Error was %s\n", fname, strerror(errno)));
143 /* Ensure we're pointing at the same place. */
144 if (sbuf.st_dev != psbuf->st_dev || sbuf.st_ino != psbuf->st_ino || sbuf.st_mode != psbuf->st_mode ) {
145 DEBUG(0,("change_owner_to_parent: device/inode/mode on directory %s changed. Refusing to chown !\n",
151 ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
154 DEBUG(10,("change_owner_to_parent: failed to chown directory %s to parent directory uid %u. \
156 fname, (unsigned int)parent_st.st_uid, strerror(errno) ));
160 DEBUG(10,("change_owner_to_parent: changed ownership of new directory %s to parent directory uid %u.\n",
161 fname, (unsigned int)parent_st.st_uid ));
165 vfs_ChDir(conn,saved_dir);
169 /****************************************************************************
171 ****************************************************************************/
173 static BOOL open_file(files_struct *fsp,connection_struct *conn,
174 const char *fname,SMB_STRUCT_STAT *psbuf,int flags,mode_t mode, uint32 desired_access)
176 int accmode = (flags & O_ACCMODE);
177 int local_flags = flags;
180 fsp->oplock_type = NO_OPLOCK;
183 /* Check permissions */
186 * This code was changed after seeing a client open request
187 * containing the open mode of (DENY_WRITE/read-only) with
188 * the 'create if not exist' bit set. The previous code
189 * would fail to open the file read only on a read-only share
190 * as it was checking the flags parameter directly against O_RDONLY,
191 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
195 if (!CAN_WRITE(conn)) {
196 /* It's a read-only share - fail if we wanted to write. */
197 if(accmode != O_RDONLY) {
198 DEBUG(3,("Permission denied opening %s\n",fname));
199 check_for_pipe(fname);
201 } else if(flags & O_CREAT) {
202 /* We don't want to write - but we must make sure that O_CREAT
203 doesn't create the file if we have write access into the
207 local_flags &= ~O_CREAT;
212 * This little piece of insanity is inspired by the
213 * fact that an NT client can open a file for O_RDONLY,
214 * but set the create disposition to FILE_EXISTS_TRUNCATE.
215 * If the client *can* write to the file, then it expects to
216 * truncate the file, even though it is opening for readonly.
217 * Quicken uses this stupid trick in backup file creation...
218 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net>
219 * for helping track this one down. It didn't bite us in 2.0.x
220 * as we always opened files read-write in that release. JRA.
223 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
224 DEBUG(10,("open_file: truncate requested on read-only open for file %s\n",fname ));
225 local_flags = (flags & ~O_ACCMODE)|O_RDWR;
228 if ((desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
229 (local_flags & O_CREAT) || ((local_flags & O_TRUNC) == O_TRUNC) ) {
232 * We can't actually truncate here as the file may be locked.
233 * open_file_shared will take care of the truncate later. JRA.
236 local_flags &= ~O_TRUNC;
238 #if defined(O_NONBLOCK) && defined(S_ISFIFO)
240 * We would block on opening a FIFO with no one else on the
241 * other end. Do what we used to do and add O_NONBLOCK to the
245 if (VALID_STAT(*psbuf) && S_ISFIFO(psbuf->st_mode))
246 local_flags |= O_NONBLOCK;
249 /* Don't create files with Microsoft wildcard characters. */
250 if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf) && ms_has_wild(fname)) {
251 set_saved_error_triple(ERRDOS, ERRinvalidname, NT_STATUS_OBJECT_NAME_INVALID);
255 /* Actually do the open */
256 fsp->fd = fd_open(conn, fname, local_flags, mode);
258 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) (flags=%d)\n",
259 fname,strerror(errno),local_flags,flags));
260 check_for_pipe(fname);
264 /* Inherit the ACL if the file was created. */
265 if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf))
266 inherit_access_acl(conn, fname, mode);
269 fsp->fd = -1; /* What we used to call a stat open. */
271 if (!VALID_STAT(*psbuf)) {
275 ret = SMB_VFS_STAT(conn, fname, psbuf);
277 ret = SMB_VFS_FSTAT(fsp,fsp->fd,psbuf);
278 /* If we have an fd, this stat should succeed. */
280 DEBUG(0,("Error doing fstat on open file %s (%s)\n", fname,strerror(errno) ));
283 /* For a non-io open, this stat failing means file not found. JRA */
291 * POSIX allows read-only opens of directories. We don't
292 * want to do this (we use a different code path for this)
293 * so catch a directory open and return an EISDIR. JRA.
296 if(S_ISDIR(psbuf->st_mode)) {
302 fsp->mode = psbuf->st_mode;
303 fsp->inode = psbuf->st_ino;
304 fsp->dev = psbuf->st_dev;
305 fsp->vuid = current_user.vuid;
306 fsp->file_pid = global_smbpid;
307 fsp->can_lock = True;
308 fsp->can_read = ((flags & O_WRONLY)==0);
309 fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
311 fsp->desired_access = desired_access;
312 fsp->print_file = False;
313 fsp->modified = False;
314 fsp->oplock_type = NO_OPLOCK;
315 fsp->sent_oplock_break = NO_BREAK_SENT;
316 fsp->is_directory = False;
317 fsp->is_stat = False;
318 fsp->directory_delete_on_close = False;
319 string_set(&fsp->fsp_name,fname);
320 fsp->wcp = NULL; /* Write cache pointer. */
322 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
323 *current_user_info.smb_name ? current_user_info.smb_name : conn->user,fsp->fsp_name,
324 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
325 conn->num_files_open + 1));
331 /*******************************************************************
332 Return True if the filename is one of the special executable types.
333 ********************************************************************/
335 static BOOL is_executable(const char *fname)
337 if ((fname = strrchr_m(fname,'.'))) {
338 if (strequal(fname,".com") ||
339 strequal(fname,".dll") ||
340 strequal(fname,".exe") ||
341 strequal(fname,".sym")) {
348 enum {AFAIL,AREAD,AWRITE,AALL};
350 /*******************************************************************
351 Reproduce the share mode access table.
352 This is horrendoously complex, and really can't be justified on any
353 rational grounds except that this is _exactly_ what NT does. See
354 the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
356 ********************************************************************/
358 static int access_table(int new_deny,int old_deny,int old_mode,
359 BOOL same_pid, BOOL isexe)
361 if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
364 if (isexe && old_mode == DOS_OPEN_RDONLY &&
365 old_deny == DENY_DOS && new_deny == DENY_READ) {
368 if (!isexe && old_mode == DOS_OPEN_RDONLY &&
369 old_deny == DENY_DOS && new_deny == DENY_DOS) {
372 if (new_deny == DENY_FCB && old_deny == DENY_DOS) {
373 if (isexe) return AFAIL;
374 if (old_mode == DOS_OPEN_RDONLY) return AFAIL;
377 if (old_mode == DOS_OPEN_RDONLY && old_deny == DENY_DOS) {
378 if (new_deny == DENY_FCB || new_deny == DENY_READ) {
379 if (isexe) return AREAD;
383 if (old_deny == DENY_FCB) {
384 if (new_deny == DENY_DOS || new_deny == DENY_FCB) return AALL;
389 if (old_deny == DENY_DOS || new_deny == DENY_DOS ||
390 old_deny == DENY_FCB || new_deny == DENY_FCB) {
392 if (old_deny == DENY_FCB || new_deny == DENY_FCB) {
395 if (old_deny == DENY_DOS) {
396 if (new_deny == DENY_READ &&
397 (old_mode == DOS_OPEN_RDONLY ||
398 old_mode == DOS_OPEN_RDWR)) {
401 if (new_deny == DENY_WRITE &&
402 (old_mode == DOS_OPEN_WRONLY ||
403 old_mode == DOS_OPEN_RDWR)) {
408 if (old_deny == DENY_NONE) return AALL;
409 if (old_deny == DENY_READ) return AWRITE;
410 if (old_deny == DENY_WRITE) return AREAD;
412 /* it isn't a exe, dll, sym or com file */
413 if (old_deny == new_deny && same_pid)
416 if (old_deny == DENY_READ || new_deny == DENY_READ) return AFAIL;
417 if (old_mode == DOS_OPEN_RDONLY) return(AREAD);
425 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_RDONLY) return(AREAD);
426 if (old_deny==DENY_READ && old_mode==DOS_OPEN_RDONLY) return(AWRITE);
427 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_RDONLY) return(AALL);
430 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_WRONLY) return(AREAD);
431 if (old_deny==DENY_READ && old_mode==DOS_OPEN_WRONLY) return(AWRITE);
432 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_WRONLY) return(AALL);
435 if (old_deny==DENY_WRITE) return(AREAD);
436 if (old_deny==DENY_READ) return(AWRITE);
437 if (old_deny==DENY_NONE) return(AALL);
443 /****************************************************************************
444 Check if we can open a file with a share mode.
445 ****************************************************************************/
447 static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, int share_mode, uint32 desired_access,
448 const char *fname, BOOL fcbopen, int *flags)
450 int deny_mode = GET_DENY_MODE(share_mode);
451 int old_open_mode = GET_OPEN_MODE(share->share_mode);
452 int old_deny_mode = GET_DENY_MODE(share->share_mode);
453 BOOL non_io_open_request;
454 BOOL non_io_open_existing;
457 * share modes = false means don't bother to check for
458 * DENY mode conflict. This is a *really* bad idea :-). JRA.
461 if(!lp_share_modes(SNUM(conn)))
464 if (desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
465 non_io_open_request = False;
467 non_io_open_request = True;
470 if (share->desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
471 non_io_open_existing = False;
473 non_io_open_existing = True;
477 * Don't allow any opens once the delete on close flag has been
481 if (GET_DELETE_ON_CLOSE_FLAG(share->share_mode)) {
482 DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
484 /* Use errno to map to correct error. */
485 set_saved_error_triple(SMB_SUCCESS, 0, NT_STATUS_OK);
489 /* this is a nasty hack, but necessary until we rewrite our open
490 handling to use a NTCreateX call as the basic call.
491 NT may open a file with neither read nor write access, and in
492 this case it expects the open not to conflict with any
493 existing deny modes. This happens (for example) during a
494 "xcopy /o" where the second file descriptor is used for
500 * This is a bit wierd - the test for desired access not having the
501 * critical bits seems seems odd. Firstly, if both opens have no
502 * critical bits then always ignore. Then check the "allow delete"
503 * then check for either. This probably isn't quite right yet but
504 * gets us much closer. JRA.
508 * If desired_access doesn't contain READ_DATA,WRITE_DATA,APPEND_DATA or EXECUTE
509 * and the existing desired_acces then share modes don't conflict.
512 if (non_io_open_request && non_io_open_existing) {
515 * Wrinkle discovered by smbtorture....
516 * If both are non-io open and requester is asking for delete and current open has delete access
517 * but neither open has allowed file share delete then deny.... this is very strange and
518 * seems to be the only case in which non-io opens conflict. JRA.
521 if ((desired_access & DELETE_ACCESS) && (share->desired_access & DELETE_ACCESS) &&
522 (!GET_ALLOW_SHARE_DELETE(share->share_mode) || !GET_ALLOW_SHARE_DELETE(share_mode))) {
523 DEBUG(5,("check_share_mode: Failing open on file %s as delete access requests conflict.\n",
525 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
529 DEBUG(5,("check_share_mode: Allowing open on file %s as both desired access (0x%x) \
530 and existing desired access (0x%x) are non-data opens\n",
531 fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
533 } else if (non_io_open_request || non_io_open_existing) {
535 * If either are non-io opens then share modes don't conflict.
537 DEBUG(5,("check_share_mode: One non-io open. Allowing open on file %s as desired access (0x%x) doesn't conflict with\
538 existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
543 * If delete access was requested and the existing share mode doesn't have
544 * ALLOW_SHARE_DELETE then deny.
547 if ((desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share->share_mode)) {
548 DEBUG(5,("check_share_mode: Failing open on file %s as delete access requested and allow share delete not set.\n",
550 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
555 * The inverse of the above.
556 * If delete access was granted and the new share mode doesn't have
557 * ALLOW_SHARE_DELETE then deny.
560 if ((share->desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share_mode)) {
561 DEBUG(5,("check_share_mode: Failing open on file %s as delete access granted and allow share delete not requested.\n",
563 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
568 /* Bluarc test may need this ... needs further investigation. */
569 if (deny_mode == DENY_ALL || old_deny_mode == DENY_ALL) {
570 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
576 * If desired_access doesn't contain READ_DATA,WRITE_DATA,APPEND_DATA or EXECUTE
577 * then share modes don't conflict. Likewise with existing desired access.
580 if ( !(desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
581 !(share->desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ) {
582 DEBUG(5,("check_share_mode: Allowing open on file %s as desired access (0x%x) doesn't conflict with \
583 existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
588 int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
589 (share->pid == sys_getpid()),is_executable(fname));
591 if ((access_allowed == AFAIL) ||
592 (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
593 (access_allowed == AREAD && *flags != O_RDONLY) ||
594 (access_allowed == AWRITE && *flags != O_WRONLY)) {
596 DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n",
597 deny_mode,old_deny_mode,old_open_mode,
598 (int)share->pid,fname, fcbopen, *flags, access_allowed));
600 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
604 if (access_allowed == AREAD)
607 if (access_allowed == AWRITE)
616 #if defined(DEVELOPER)
617 static void validate_my_share_entries(int num, share_mode_entry *share_entry)
621 if (share_entry->pid != sys_getpid())
624 fsp = file_find_dif(share_entry->dev, share_entry->inode, share_entry->share_file_id);
626 DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) ));
627 smb_panic("validate_my_share_entries: Cannot match a share entry with an open file\n");
630 if (((uint16)fsp->oplock_type) != share_entry->op_type) {
632 DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) ));
633 slprintf(str, sizeof(str)-1, "validate_my_share_entries: file %s, oplock_type = 0x%x, op_type = 0x%x\n",
634 fsp->fsp_name, (unsigned int)fsp->oplock_type, (unsigned int)share_entry->op_type );
640 struct share_mode_entry_list {
641 struct share_mode_entry_list *next, *prev;
642 share_mode_entry entry;
645 static void free_broken_entry_list(struct share_mode_entry_list *broken_entry_list)
647 while (broken_entry_list) {
648 struct share_mode_entry_list *broken_entry = broken_entry_list;
649 DLIST_REMOVE(broken_entry_list, broken_entry);
650 SAFE_FREE(broken_entry);
654 /****************************************************************************
655 Deal with open deny mode and oplock break processing.
656 Invarient: Share mode must be locked on entry and exit.
657 Returns -1 on error, or number of share modes on success (may be zero).
658 ****************************************************************************/
660 static int open_mode_check(connection_struct *conn, const char *fname, SMB_DEV_T dev,
662 uint32 desired_access,
663 int share_mode, int *p_flags, int *p_oplock_request,
664 BOOL *p_all_current_opens_are_level_II)
668 int oplock_contention_count = 0;
669 share_mode_entry *old_shares = NULL;
670 BOOL fcbopen = False;
673 if(GET_OPEN_MODE(share_mode) == DOS_OPEN_FCB)
676 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
678 if(num_share_modes == 0) {
679 SAFE_FREE(old_shares);
683 if (desired_access && ((desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES))==0) &&
684 ((desired_access & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) != 0)) {
685 /* Stat open that doesn't trigger oplock breaks or share mode checks... ! JRA. */
686 SAFE_FREE(old_shares);
687 return num_share_modes;
691 * Check if the share modes will give us access.
695 struct share_mode_entry_list *broken_entry_list = NULL;
696 struct share_mode_entry_list *broken_entry = NULL;
698 broke_oplock = False;
699 *p_all_current_opens_are_level_II = True;
701 for(i = 0; i < num_share_modes; i++) {
702 BOOL cause_oplock_break = False;
703 share_mode_entry *share_entry = &old_shares[i];
705 #if defined(DEVELOPER)
706 validate_my_share_entries(i, share_entry);
710 * By observation of NetBench, oplocks are broken *before* share
711 * modes are checked. This allows a file to be closed by the client
712 * if the share mode would deny access and the client has an oplock.
713 * Check if someone has an oplock on this file. If so we must break
714 * it before continuing.
717 /* Was this a delete this file request ? */
718 if (!*p_oplock_request && desired_access == DELETE_ACCESS &&
719 !BATCH_OPLOCK_TYPE(share_entry->op_type)) {
720 /* Don't break the oplock in this case. */
721 cause_oplock_break = False;
722 } else if((*p_oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) ||
723 (!*p_oplock_request && (share_entry->op_type != NO_OPLOCK))) {
724 cause_oplock_break = True;
727 if(cause_oplock_break) {
730 DEBUG(5,("open_mode_check: oplock_request = %d, breaking oplock (%x) on file %s, \
731 dev = %x, inode = %.0f\n", *p_oplock_request, share_entry->op_type, fname, (unsigned int)dev, (double)inode));
733 /* Ensure the reply for the open uses the correct sequence number. */
734 /* This isn't a real deferred packet as it's response will also increment
737 srv_defer_sign_response(get_current_mid());
739 /* Oplock break - unlock to request it. */
740 unlock_share_entry(conn, dev, inode);
742 opb_ret = request_oplock_break(share_entry);
745 lock_share_entry(conn, dev, inode);
747 if(opb_ret == False) {
748 DEBUG(0,("open_mode_check: FAILED when breaking oplock (%x) on file %s, \
749 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
750 SAFE_FREE(old_shares);
751 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
755 broken_entry = SMB_MALLOC_P(struct share_mode_entry_list);
757 smb_panic("open_mode_check: malloc fail.\n");
759 broken_entry->entry = *share_entry;
760 DLIST_ADD(broken_entry_list, broken_entry);
763 } else if (!LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
764 *p_all_current_opens_are_level_II = False;
769 /* Update the current open table. */
770 SAFE_FREE(old_shares);
771 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
774 /* Now we check the share modes, after any oplock breaks. */
775 for(i = 0; i < num_share_modes; i++) {
776 share_mode_entry *share_entry = &old_shares[i];
778 /* someone else has a share lock on it, check to see if we can too */
779 if (!check_share_mode(conn, share_entry, share_mode, desired_access,
780 fname, fcbopen, p_flags)) {
781 SAFE_FREE(old_shares);
782 free_broken_entry_list(broken_entry_list);
788 for(broken_entry = broken_entry_list; broken_entry; broken_entry = broken_entry->next) {
789 oplock_contention_count++;
791 /* Paranoia check that this is no longer an exlusive entry. */
792 for(i = 0; i < num_share_modes; i++) {
793 share_mode_entry *share_entry = &old_shares[i];
795 if (share_modes_identical(&broken_entry->entry, share_entry) &&
796 EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type) ) {
799 * This should not happen. The target left this oplock
800 * as exlusive.... The process *must* be dead....
803 DEBUG(0,("open_mode_check: exlusive oplock left by process %d \
804 after break ! For file %s, dev = %x, inode = %.0f. Deleting it to continue...\n",
805 (int)broken_entry->entry.pid, fname, (unsigned int)dev, (double)inode));
807 if (process_exists(broken_entry->entry.pid)) {
808 DEBUG(0,("open_mode_check: Existent process %lu left active oplock.\n",
809 (unsigned long)broken_entry->entry.pid ));
812 if (del_share_entry(dev, inode, &broken_entry->entry, NULL) == -1) {
813 free_broken_entry_list(broken_entry_list);
815 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
820 * We must reload the share modes after deleting the
821 * other process's entry.
824 SAFE_FREE(old_shares);
825 num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
828 } /* end for paranoia... */
829 } /* end for broken_entry */
830 free_broken_entry_list(broken_entry_list);
831 } while(broke_oplock);
834 * Refuse to grant an oplock in case the contention limit is
835 * reached when going through the lock list multiple times.
838 if(oplock_contention_count >= lp_oplock_contention_limit(SNUM(conn))) {
839 *p_oplock_request = 0;
840 DEBUG(4,("open_mode_check: oplock contention = %d. Not granting oplock.\n",
841 oplock_contention_count ));
844 SAFE_FREE(old_shares);
845 return num_share_modes;
848 /****************************************************************************
849 Delete the record for a handled deferred open entry.
850 ****************************************************************************/
852 static void delete_defered_open_entry_record(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T inode)
854 uint16 mid = get_current_mid();
855 pid_t mypid = sys_getpid();
856 deferred_open_entry *de_array = NULL;
857 int num_de_entries, i;
859 if (!lp_defer_sharing_violations()) {
863 num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
864 for (i = 0; i < num_de_entries; i++) {
865 deferred_open_entry *entry = &de_array[i];
866 if (entry->pid == mypid && entry->mid == mid && entry->dev == dev &&
867 entry->inode == inode) {
869 /* Remove the deferred open entry from the array. */
870 delete_deferred_open_entry(entry);
878 /****************************************************************************
879 Handle the 1 second delay in returning a SHARING_VIOLATION error.
880 ****************************************************************************/
882 void defer_open_sharing_error(connection_struct *conn, struct timeval *ptv,
883 char *fname, SMB_DEV_T dev, SMB_INO_T inode)
885 uint16 mid = get_current_mid();
886 pid_t mypid = sys_getpid();
887 deferred_open_entry *de_array = NULL;
888 int num_de_entries, i;
889 struct dev_inode_bundle dib;
891 if (!lp_defer_sharing_violations()) {
898 num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
899 for (i = 0; i < num_de_entries; i++) {
900 deferred_open_entry *entry = &de_array[i];
901 if (entry->pid == mypid && entry->mid == mid) {
903 * Check if a 1 second timeout has expired.
905 if (usec_time_diff(ptv, &entry->time) > SHARING_VIOLATION_USEC_WAIT) {
906 DEBUG(10,("defer_open_sharing_error: Deleting deferred open entry for mid %u, \
908 (unsigned int)mid, fname ));
910 /* Expired, return a real error. */
911 /* Remove the deferred open entry from the array. */
913 delete_deferred_open_entry(entry);
918 * If the timeout hasn't expired yet and we still have a sharing violation,
919 * just leave the entry in the deferred open array alone. We do need to
920 * reschedule this open call though (with the original created time).
922 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] updating \
923 deferred open entry for mid %u, file %s\n",
924 (unsigned int)entry->time.tv_sec,
925 (unsigned int)entry->time.tv_usec,
926 (unsigned int)mid, fname ));
928 push_sharing_violation_open_smb_message(&entry->time, (char *)&dib, sizeof(dib));
934 DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred open entry for mid %u, file %s\n",
935 (unsigned int)ptv->tv_sec, (unsigned int)ptv->tv_usec, (unsigned int)mid, fname ));
937 if (!push_sharing_violation_open_smb_message(ptv, (char *)&dib, sizeof(dib))) {
941 if (!add_deferred_open(mid, ptv, dev, inode, global_oplock_port, fname)) {
942 remove_sharing_violation_open_smb_message(mid);
946 * Push the MID of this packet on the signing queue.
947 * We only do this once, the first time we push the packet
948 * onto the deferred open queue, as this has a side effect
949 * of incrementing the response sequence number.
952 srv_defer_sign_response(mid);
957 /****************************************************************************
958 Set a kernel flock on a file for NFS interoperability.
959 This requires a patch to Linux.
960 ****************************************************************************/
962 static void kernel_flock(files_struct *fsp, int deny_mode)
964 #if HAVE_KERNEL_SHARE_MODES
966 if (deny_mode == DENY_READ) kernel_mode = LOCK_MAND|LOCK_WRITE;
967 else if (deny_mode == DENY_WRITE) kernel_mode = LOCK_MAND|LOCK_READ;
968 else if (deny_mode == DENY_ALL) kernel_mode = LOCK_MAND;
969 if (kernel_mode) flock(fsp->fd, kernel_mode);
975 static BOOL open_match_attributes(connection_struct *conn, const char *path, uint32 old_dos_mode, uint32 new_dos_mode,
976 mode_t existing_mode, mode_t new_mode, mode_t *returned_mode)
978 uint32 noarch_old_dos_mode, noarch_new_dos_mode;
980 noarch_old_dos_mode = (old_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE);
981 noarch_new_dos_mode = (new_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE);
983 if((noarch_old_dos_mode == 0 && noarch_new_dos_mode != 0) ||
984 (noarch_old_dos_mode != 0 && ((noarch_old_dos_mode & noarch_new_dos_mode) == noarch_old_dos_mode)))
985 *returned_mode = new_mode;
987 *returned_mode = (mode_t)0;
989 DEBUG(10,("open_match_attributes: file %s old_dos_mode = 0x%x, existing_mode = 0%o, new_dos_mode = 0x%x returned_mode = 0%o\n",
991 old_dos_mode, (unsigned int)existing_mode, new_dos_mode, (unsigned int)*returned_mode ));
993 /* If we're mapping SYSTEM and HIDDEN ensure they match. */
994 if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
995 if ((old_dos_mode & FILE_ATTRIBUTE_SYSTEM) && !(new_dos_mode & FILE_ATTRIBUTE_SYSTEM))
998 if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
999 if ((old_dos_mode & FILE_ATTRIBUTE_HIDDEN) && !(new_dos_mode & FILE_ATTRIBUTE_HIDDEN))
1005 /****************************************************************************
1006 Open a file with a share mode.
1007 ****************************************************************************/
1009 files_struct *open_file_shared(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf,
1010 int share_mode,int ofun, uint32 new_dos_mode, int oplock_request,
1011 int *Access,int *action)
1013 return open_file_shared1(conn, fname, psbuf, 0, share_mode, ofun, new_dos_mode,
1014 oplock_request, Access, action);
1017 /****************************************************************************
1018 Open a file with a share mode.
1019 ****************************************************************************/
1021 files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf,
1022 uint32 desired_access,
1023 int share_mode,int ofun, uint32 new_dos_mode,
1025 int *Access,int *paction)
1029 int deny_mode = GET_DENY_MODE(share_mode);
1030 BOOL allow_share_delete = GET_ALLOW_SHARE_DELETE(share_mode);
1031 BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode);
1032 BOOL file_existed = VALID_STAT(*psbuf);
1033 BOOL fcbopen = False;
1034 BOOL def_acl = False;
1035 BOOL add_share_mode = True;
1036 BOOL internal_only_open = False;
1038 SMB_INO_T inode = 0;
1039 int num_share_modes = 0;
1040 BOOL all_current_opens_are_level_II = False;
1041 BOOL fsp_open = False;
1042 files_struct *fsp = NULL;
1045 mode_t new_mode = (mode_t)0;
1047 uint32 existing_dos_mode = 0;
1048 struct pending_message_list *pml = NULL;
1049 uint16 mid = get_current_mid();
1050 /* We add aARCH to this as this mode is only used if the file is created new. */
1051 mode_t mode = unix_mode(conn,new_dos_mode | aARCH,fname, True);
1053 if (oplock_request == INTERNAL_OPEN_ONLY) {
1054 internal_only_open = True;
1058 if ((pml = get_open_deferred_message(mid)) != NULL) {
1059 struct dev_inode_bundle dib;
1061 memcpy(&dib, pml->private_data.data, sizeof(dib));
1063 /* There could be a race condition where the dev/inode pair
1064 has changed since we deferred the message. If so, just
1065 remove the deferred open entry and return sharing violation. */
1067 /* If the timeout value is non-zero, we need to just
1068 return sharing violation. Don't retry the open
1069 as we were not notified of a close and we don't want to
1070 trigger another spurious oplock break. */
1072 if (!file_existed || dib.dev != psbuf->st_dev || dib.inode != psbuf->st_ino ||
1073 pml->msg_time.tv_sec || pml->msg_time.tv_usec) {
1074 /* Ensure we don't reprocess this message. */
1075 remove_sharing_violation_open_smb_message(mid);
1077 /* Now remove the deferred open entry under lock. */
1078 lock_share_entry(conn, dib.dev, dib.inode);
1079 delete_defered_open_entry_record(conn, dib.dev, dib.inode);
1080 unlock_share_entry(conn, dib.dev, dib.inode);
1082 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
1085 /* Ensure we don't reprocess this message. */
1086 remove_sharing_violation_open_smb_message(mid);
1090 if (conn->printer) {
1091 /* printers are handled completely differently. Most of the passed parameters are
1094 *Access = DOS_OPEN_WRONLY;
1096 *paction = FILE_WAS_CREATED;
1097 return print_fsp_open(conn, fname);
1100 DEBUG(10,("open_file_shared: fname = %s, dos_attrs = %x, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
1101 fname, new_dos_mode, share_mode, ofun, (int)mode, oplock_request ));
1103 if (!check_name(fname,conn)) {
1107 new_dos_mode &= SAMBA_ATTRIBUTES_MASK;
1109 existing_dos_mode = dos_mode(conn, fname, psbuf);
1112 /* ignore any oplock requests if oplocks are disabled */
1113 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break) {
1117 /* this is for OS/2 long file names - say we don't support them */
1118 if (strstr(fname,".+,;=[].")) {
1119 /* OS/2 Workplace shell fix may be main code stream in a later release. */
1120 set_saved_error_triple(ERRDOS, ERRcannotopen, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1121 DEBUG(5,("open_file_shared: OS/2 long filenames are not supported.\n"));
1125 if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed) {
1126 DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
1128 if (S_ISDIR(psbuf->st_mode)) {
1136 if (CAN_WRITE(conn) && (GET_FILE_CREATE_DISPOSITION(ofun) == FILE_CREATE_IF_NOT_EXIST))
1139 if (CAN_WRITE(conn) && (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE))
1142 /* We only care about matching attributes on file exists and truncate. */
1143 if (file_existed && (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)) {
1144 if (!open_match_attributes(conn, fname, existing_dos_mode, new_dos_mode,
1145 psbuf->st_mode, mode, &new_mode)) {
1146 DEBUG(5,("open_file_shared: attributes missmatch for file %s (%x %x) (0%o, 0%o)\n",
1147 fname, existing_dos_mode, new_dos_mode,
1148 (int)psbuf->st_mode, (int)mode ));
1154 if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL)
1157 /* note that we ignore the append flag as
1158 append does not mean the same thing under dos and unix */
1160 switch (GET_OPEN_MODE(share_mode)) {
1162 case DOS_OPEN_RDONLY:
1164 if (desired_access == 0)
1165 desired_access = FILE_READ_DATA;
1167 case DOS_OPEN_WRONLY:
1169 if (desired_access == 0)
1170 desired_access = FILE_WRITE_DATA;
1175 if (desired_access == 0)
1176 desired_access = FILE_READ_DATA|FILE_WRITE_DATA;
1180 if (desired_access == 0)
1181 desired_access = FILE_READ_DATA|FILE_WRITE_DATA;
1184 /* Force DOS error. */
1185 set_saved_error_triple(ERRDOS, ERRinvalidparam, NT_STATUS_INVALID);
1190 if (GET_FILE_SYNC_OPENMODE(share_mode)) {
1195 if (flags != O_RDONLY && file_existed &&
1196 (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_mode))) {
1198 DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
1199 fname, !CAN_WRITE(conn) ? "share" : "file" ));
1206 if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) {
1207 DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
1212 if (desired_access && ((desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES))==0) &&
1213 ((desired_access & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) != 0)) {
1214 /* Stat open that doesn't trigger oplock breaks or share mode checks... ! JRA. */
1215 deny_mode = DENY_NONE;
1218 add_share_mode = False;
1223 fsp = file_new(conn);
1229 dev = psbuf->st_dev;
1230 inode = psbuf->st_ino;
1232 lock_share_entry(conn, dev, inode);
1234 num_share_modes = open_mode_check(conn, fname, dev, inode,
1237 &flags, &oplock_request, &all_current_opens_are_level_II);
1238 if(num_share_modes == -1) {
1241 * This next line is a subtlety we need for MS-Access. If a file open will
1242 * fail due to share permissions and also for security (access)
1243 * reasons, we need to return the access failed error, not the
1244 * share error. This means we must attempt to open the file anyway
1245 * in order to get the UNIX access error - even if we're going to
1246 * fail the open for share reasons. This is bad, as we're burning
1247 * another fd if there are existing locks but there's nothing else
1248 * we can do. We also ensure we're not going to create or tuncate
1249 * the file as we only want an access decision at this stage. JRA.
1252 fsp_open = open_file(fsp,conn,fname,psbuf,
1253 flags|(flags2&~(O_TRUNC|O_CREAT)),mode,desired_access);
1255 DEBUG(4,("open_file_shared : share_mode deny - calling open_file with \
1256 flags=0x%X flags2=0x%X mode=0%o returned %d\n",
1257 flags,(flags2&~(O_TRUNC|O_CREAT)),(int)mode,(int)fsp_open ));
1259 if (!fsp_open && errno) {
1260 /* Default error. */
1261 set_saved_error_triple(ERRDOS, ERRnoaccess, NT_STATUS_ACCESS_DENIED);
1265 * If we're returning a share violation, ensure we cope with
1266 * the braindead 1 second delay.
1269 if (!internal_only_open) {
1271 get_saved_error_triple(NULL, NULL, &status);
1272 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION)) {
1273 /* The fsp->open_time here represents the current time of day. */
1274 defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
1278 unlock_share_entry(conn, dev, inode);
1280 fd_close(conn, fsp);
1282 * We have detected a sharing violation here
1283 * so return the correct error code
1285 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
1292 * We exit this block with the share entry *locked*.....
1297 * Ensure we pay attention to default ACLs on directories if required.
1300 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) &&
1301 (def_acl = directory_has_default_acl(conn, parent_dirname(fname))))
1304 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
1305 flags,flags2,(int)mode));
1308 * open_file strips any O_TRUNC flags itself.
1311 fsp_open = open_file(fsp,conn,fname,psbuf,flags|flags2,mode,desired_access);
1313 if (!fsp_open && (flags == O_RDWR) && (errno != ENOENT) && fcbopen) {
1314 if((fsp_open = open_file(fsp,conn,fname,psbuf,O_RDONLY,mode,desired_access)) == True)
1320 unlock_share_entry(conn, dev, inode);
1326 * Deal with the race condition where two smbd's detect the file doesn't
1327 * exist and do the create at the same time. One of them will win and
1328 * set a share mode, the other (ie. this one) should check if the
1329 * requested share mode for this create is allowed.
1332 if (!file_existed) {
1335 * Now the file exists and fsp is successfully opened,
1336 * fsp->dev and fsp->inode are valid and should replace the
1337 * dev=0,inode=0 from a non existent file. Spotted by
1338 * Nadav Danieli <nadavd@exanet.com>. JRA.
1344 lock_share_entry_fsp(fsp);
1346 num_share_modes = open_mode_check(conn, fname, dev, inode,
1349 &flags, &oplock_request, &all_current_opens_are_level_II);
1351 if(num_share_modes == -1) {
1353 * If we're returning a share violation, ensure we cope with
1354 * the braindead 1 second delay.
1358 get_saved_error_triple(NULL, NULL, &status);
1359 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION)) {
1360 /* The fsp->open_time here represents the current time of day. */
1361 defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
1364 unlock_share_entry_fsp(fsp);
1368 * We have detected a sharing violation here, so
1369 * return the correct code.
1371 set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
1376 * If there are any share modes set then the file *did*
1377 * exist. Ensure we return the correct value for action.
1380 if (num_share_modes > 0)
1381 file_existed = True;
1384 * We exit this block with the share entry *locked*.....
1388 /* note that we ignore failure for the following. It is
1389 basically a hack for NFS, and NFS will never set one of
1390 these only read them. Nobody but Samba can ever set a deny
1391 mode and we have already checked our more authoritative
1392 locking database for permission to set this deny mode. If
1393 the kernel refuses the operations then the kernel is wrong */
1394 kernel_flock(fsp, deny_mode);
1397 * At this point onwards, we can guarentee that the share entry
1398 * is locked, whether we created the file or not, and that the
1399 * deny mode is compatible with all current opens.
1403 * If requested, truncate the file.
1406 if (flags2&O_TRUNC) {
1408 * We are modifing the file after open - update the stat struct..
1410 if ((SMB_VFS_FTRUNCATE(fsp,fsp->fd,0) == -1) || (SMB_VFS_FSTAT(fsp,fsp->fd,psbuf)==-1)) {
1411 unlock_share_entry_fsp(fsp);
1420 open_mode = DOS_OPEN_RDONLY;
1423 open_mode = DOS_OPEN_RDWR;
1426 open_mode = DOS_OPEN_WRONLY;
1430 fsp->share_mode = SET_DENY_MODE(deny_mode) |
1431 SET_OPEN_MODE(open_mode) |
1432 SET_ALLOW_SHARE_DELETE(allow_share_delete);
1434 DEBUG(10,("open_file_shared : share_mode = %x\n", fsp->share_mode ));
1437 (*Access) = (SET_DENY_MODE(deny_mode) | SET_OPEN_MODE(open_mode));
1442 if (file_existed && !(flags2 & O_TRUNC))
1443 action = FILE_WAS_OPENED;
1444 if (file_existed && (flags2 & O_TRUNC))
1445 action = FILE_WAS_OVERWRITTEN;
1446 if (!file_existed) {
1447 action = FILE_WAS_CREATED;
1448 /* Change the owner if required. */
1449 if (lp_inherit_owner(SNUM(conn))) {
1450 change_owner_to_parent(conn, fsp, fsp->fsp_name, psbuf);
1459 * Setup the oplock info in both the shared memory and
1463 if(oplock_request && (num_share_modes == 0) &&
1464 !IS_VETO_OPLOCK_PATH(conn,fname) && set_file_oplock(fsp, oplock_request) ) {
1465 port = global_oplock_port;
1466 } else if (oplock_request && all_current_opens_are_level_II) {
1467 port = global_oplock_port;
1468 oplock_request = LEVEL_II_OPLOCK;
1469 set_file_oplock(fsp, oplock_request);
1475 if (add_share_mode) {
1476 set_share_mode(fsp, port, oplock_request);
1479 if (delete_on_close) {
1480 uint32 dosmode = existing_dos_mode;
1483 if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
1484 dosmode = new_dos_mode;
1486 result = set_delete_on_close_internal(fsp, delete_on_close, dosmode);
1488 if (NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_OK)) {
1491 /* Remember to delete the mode we just added. */
1492 if (add_share_mode) {
1493 del_share_mode(fsp, NULL);
1495 unlock_share_entry_fsp(fsp);
1498 ntstatus_to_dos(result, &u_e_c, &u_e_code);
1499 set_saved_error_triple(u_e_c, u_e_code, result);
1504 if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
1505 /* Files should be initially set as archive */
1506 if (lp_map_archive(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
1507 file_set_dosmode(conn, fname, new_dos_mode | aARCH, NULL, True);
1512 * Take care of inherited ACLs on created files - if default ACL not
1516 if (!file_existed && !def_acl) {
1518 int saved_errno = errno; /* We might get ENOSYS in the next call.. */
1520 if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, mode) == -1 && errno == ENOSYS)
1521 errno = saved_errno; /* Ignore ENOSYS */
1523 } else if (new_mode) {
1527 /* Attributes need changing. File already existed. */
1530 int saved_errno = errno; /* We might get ENOSYS in the next call.. */
1531 ret = SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, new_mode);
1533 if (ret == -1 && errno == ENOSYS) {
1534 errno = saved_errno; /* Ignore ENOSYS */
1536 DEBUG(5, ("open_file_shared: failed to reset attributes of file %s to 0%o\n",
1537 fname, (int)new_mode));
1538 ret = 0; /* Don't do the fchmod below. */
1542 if ((ret == -1) && (SMB_VFS_FCHMOD(fsp, fsp->fd, new_mode) == -1))
1543 DEBUG(5, ("open_file_shared: failed to reset attributes of file %s to 0%o\n",
1544 fname, (int)new_mode));
1547 /* If this is a successful open, we must remove any deferred open records. */
1548 delete_defered_open_entry_record(conn, fsp->dev, fsp->inode);
1549 unlock_share_entry_fsp(fsp);
1551 conn->num_files_open++;
1556 /****************************************************************************
1557 Open a file for for write to ensure that we can fchmod it.
1558 ****************************************************************************/
1560 files_struct *open_file_fchmod(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf)
1562 files_struct *fsp = NULL;
1565 if (!VALID_STAT(*psbuf))
1568 fsp = file_new(conn);
1572 /* note! we must use a non-zero desired access or we don't get
1573 a real file descriptor. Oh what a twisted web we weave. */
1574 fsp_open = open_file(fsp,conn,fname,psbuf,O_WRONLY,0,FILE_WRITE_DATA);
1577 * This is not a user visible file open.
1578 * Don't set a share mode and don't increment
1579 * the conn->num_files_open.
1590 /****************************************************************************
1591 Close the fchmod file fd - ensure no locks are lost.
1592 ****************************************************************************/
1594 int close_file_fchmod(files_struct *fsp)
1596 int ret = fd_close(fsp->conn, fsp);
1601 /****************************************************************************
1602 Open a directory from an NT SMB call.
1603 ****************************************************************************/
1605 files_struct *open_directory(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf,
1606 uint32 desired_access, int share_mode, int smb_ofun, int *action)
1608 BOOL got_stat = False;
1609 files_struct *fsp = file_new(conn);
1610 BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode);
1615 if (VALID_STAT(*psbuf))
1618 if (got_stat && (GET_FILE_OPEN_DISPOSITION(smb_ofun) == FILE_EXISTS_FAIL)) {
1620 errno = EEXIST; /* Setup so correct error is returned to client. */
1624 if (GET_FILE_CREATE_DISPOSITION(smb_ofun) == FILE_CREATE_IF_NOT_EXIST) {
1628 if(!S_ISDIR(psbuf->st_mode)) {
1629 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1634 *action = FILE_WAS_OPENED;
1639 * Try and create the directory.
1642 /* We know bad_path is false as it's caught earlier. */
1644 NTSTATUS status = mkdir_internal(conn, fname, False);
1646 if (!NT_STATUS_IS_OK(status)) {
1647 DEBUG(2,("open_directory: unable to create %s. Error was %s\n",
1648 fname, strerror(errno) ));
1650 /* Ensure we return the correct NT status to the client. */
1651 set_saved_error_triple(0, 0, status);
1655 /* Ensure we're checking for a symlink here.... */
1656 /* We don't want to get caught by a symlink racer. */
1658 if(SMB_VFS_LSTAT(conn,fname, psbuf) != 0) {
1663 if(!S_ISDIR(psbuf->st_mode)) {
1664 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1669 *action = FILE_WAS_CREATED;
1675 * Don't create - just check that it *was* a directory.
1679 DEBUG(3,("open_directory: unable to stat name = %s. Error was %s\n",
1680 fname, strerror(errno) ));
1685 if(!S_ISDIR(psbuf->st_mode)) {
1686 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1691 *action = FILE_WAS_OPENED;
1694 DEBUG(5,("open_directory: opening directory %s\n", fname));
1697 * Setup the files_struct for it.
1700 fsp->mode = psbuf->st_mode;
1701 fsp->inode = psbuf->st_ino;
1702 fsp->dev = psbuf->st_dev;
1703 fsp->vuid = current_user.vuid;
1704 fsp->file_pid = global_smbpid;
1705 fsp->can_lock = True;
1706 fsp->can_read = False;
1707 fsp->can_write = False;
1708 fsp->share_mode = share_mode;
1709 fsp->desired_access = desired_access;
1710 fsp->print_file = False;
1711 fsp->modified = False;
1712 fsp->oplock_type = NO_OPLOCK;
1713 fsp->sent_oplock_break = NO_BREAK_SENT;
1714 fsp->is_directory = True;
1715 fsp->is_stat = False;
1716 fsp->directory_delete_on_close = False;
1717 string_set(&fsp->fsp_name,fname);
1719 if (delete_on_close) {
1720 NTSTATUS status = set_delete_on_close_internal(fsp, delete_on_close, 0);
1722 if (!NT_STATUS_IS_OK(status)) {
1728 /* Change the owner if required. */
1729 if ((*action == FILE_WAS_CREATED) && lp_inherit_owner(SNUM(conn))) {
1730 change_owner_to_parent(conn, fsp, fsp->fsp_name, psbuf);
1733 conn->num_files_open++;
1738 /****************************************************************************
1739 Open a pseudo-file (no locking checks - a 'stat' open).
1740 ****************************************************************************/
1742 files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf)
1744 files_struct *fsp = NULL;
1746 if (!VALID_STAT(*psbuf))
1749 /* Can't 'stat' open directories. */
1750 if(S_ISDIR(psbuf->st_mode))
1753 fsp = file_new(conn);
1757 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
1760 * Setup the files_struct for it.
1763 fsp->mode = psbuf->st_mode;
1764 fsp->inode = psbuf->st_ino;
1765 fsp->dev = psbuf->st_dev;
1766 fsp->vuid = current_user.vuid;
1767 fsp->file_pid = global_smbpid;
1768 fsp->can_lock = False;
1769 fsp->can_read = False;
1770 fsp->can_write = False;
1771 fsp->share_mode = 0;
1772 fsp->desired_access = 0;
1773 fsp->print_file = False;
1774 fsp->modified = False;
1775 fsp->oplock_type = NO_OPLOCK;
1776 fsp->sent_oplock_break = NO_BREAK_SENT;
1777 fsp->is_directory = False;
1778 fsp->is_stat = True;
1779 fsp->directory_delete_on_close = False;
1780 string_set(&fsp->fsp_name,fname);
1782 conn->num_files_open++;