r6673: Fix the write cache based on some VERY good detective work
[tprouty/samba.git] / source / smbd / open.c
index ab5ea236faa426a4f404eefb31cac5cff19ff894..8b30776fdd6d35d2fb7893f35d9c51e8f65fbe69 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/CIFS implementation.
    file opening and share modes
    Copyright (C) Andrew Tridgell 1992-1998
-   Copyright (C) Jeremy Allison 2001
+   Copyright (C) Jeremy Allison 2001-2004
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include "includes.h"
 
+extern struct current_user current_user;
 extern userdom_struct current_user_info;
 extern uint16 global_oplock_port;
 extern uint16 global_smbpid;
 extern BOOL global_client_failed_oplock_break;
 
+struct dev_inode_bundle {
+       SMB_DEV_T dev;
+       SMB_INO_T inode;
+};
+
 /****************************************************************************
  fd support routines - attempt to do a dos_open.
 ****************************************************************************/
@@ -71,9 +77,92 @@ static void check_for_pipe(const char *fname)
        strlower_m(s);
        if (strstr(s,"pipe/")) {
                DEBUG(3,("Rejecting named pipe open for %s\n",fname));
-               unix_ERR_class = ERRSRV;
-               unix_ERR_code = ERRaccess;
-               unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
+               set_saved_error_triple(ERRSRV, ERRaccess, NT_STATUS_ACCESS_DENIED);
+       }
+}
+
+/****************************************************************************
+ Change the ownership of a file to that of the parent directory.
+ Do this by fd if possible.
+****************************************************************************/
+
+void change_owner_to_parent(connection_struct *conn, files_struct *fsp, const char *fname, SMB_STRUCT_STAT *psbuf)
+{
+       const char *parent_path = parent_dirname(fname);
+       SMB_STRUCT_STAT parent_st;
+       int ret;
+
+       ret = SMB_VFS_STAT(conn, parent_path, &parent_st);
+       if (ret == -1) {
+               DEBUG(0,("change_owner_to_parent: failed to stat parent directory %s. Error was %s\n",
+                       parent_path, strerror(errno) ));
+               return;
+       }
+
+       if (fsp && fsp->fd != -1) {
+               become_root();
+               ret = SMB_VFS_FCHOWN(fsp, fsp->fd, parent_st.st_uid, (gid_t)-1);
+               unbecome_root();
+               if (ret == -1) {
+                       DEBUG(0,("change_owner_to_parent: failed to fchown file %s to parent directory uid %u. \
+Error was %s\n",
+                               fname, (unsigned int)parent_st.st_uid, strerror(errno) ));
+               }
+
+               DEBUG(10,("change_owner_to_parent: changed new file %s to parent directory uid %u.\n",
+                       fname, (unsigned int)parent_st.st_uid ));
+
+       } else {
+               /* We've already done an lstat into psbuf, and we know it's a directory. If
+                  we can cd into the directory and the dev/ino are the same then we can safely
+                  chown without races as we're locking the directory in place by being in it.
+                  This should work on any UNIX (thanks tridge :-). JRA.
+               */
+
+               pstring saved_dir;
+               SMB_STRUCT_STAT sbuf;
+
+               if (!vfs_GetWd(conn,saved_dir)) {
+                       DEBUG(0,("change_owner_to_parent: failed to get current working directory\n"));
+                       return;
+               }
+
+               /* Chdir into the new path. */
+               if (vfs_ChDir(conn, fname) == -1) {
+                       DEBUG(0,("change_owner_to_parent: failed to change current working directory to %s. \
+Error was %s\n", fname, strerror(errno) ));
+                       goto out;
+               }
+
+               if (SMB_VFS_STAT(conn,".",&sbuf) == -1) {
+                       DEBUG(0,("change_owner_to_parent: failed to stat directory '.' (%s) \
+Error was %s\n", fname, strerror(errno)));
+                       goto out;
+               }
+
+               /* Ensure we're pointing at the same place. */
+               if (sbuf.st_dev != psbuf->st_dev || sbuf.st_ino != psbuf->st_ino || sbuf.st_mode != psbuf->st_mode ) {
+                       DEBUG(0,("change_owner_to_parent: device/inode/mode on directory %s changed. Refusing to chown !\n",
+                               fname ));
+                       goto out;
+               }
+
+               become_root();
+               ret = SMB_VFS_CHOWN(conn, ".", parent_st.st_uid, (gid_t)-1);
+               unbecome_root();
+               if (ret == -1) {
+                       DEBUG(10,("change_owner_to_parent: failed to chown directory %s to parent directory uid %u. \
+Error was %s\n",
+                               fname, (unsigned int)parent_st.st_uid, strerror(errno) ));
+                       goto out;
+               }
+
+               DEBUG(10,("change_owner_to_parent: changed ownership of new directory %s to parent directory uid %u.\n",
+                       fname, (unsigned int)parent_st.st_uid ));
+
+  out:
+
+               vfs_ChDir(conn,saved_dir);
        }
 }
 
@@ -84,7 +173,6 @@ static void check_for_pipe(const char *fname)
 static BOOL open_file(files_struct *fsp,connection_struct *conn,
                      const char *fname,SMB_STRUCT_STAT *psbuf,int flags,mode_t mode, uint32 desired_access)
 {
-       extern struct current_user current_user;
        int accmode = (flags & O_ACCMODE);
        int local_flags = flags;
 
@@ -160,9 +248,7 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
 
                /* Don't create files with Microsoft wildcard characters. */
                if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf) && ms_has_wild(fname))  {
-                       unix_ERR_class = ERRDOS;
-                       unix_ERR_code = ERRinvalidname;
-                       unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID;
+                       set_saved_error_triple(ERRDOS, ERRinvalidname, NT_STATUS_OBJECT_NAME_INVALID);
                        return False;
                }
 
@@ -218,7 +304,6 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
        fsp->dev = psbuf->st_dev;
        fsp->vuid = current_user.vuid;
        fsp->file_pid = global_smbpid;
-       fsp->size = psbuf->st_size;
        fsp->can_lock = True;
        fsp->can_read = ((flags & O_WRONLY)==0);
        fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
@@ -239,11 +324,12 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
                 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
                 conn->num_files_open + 1));
 
+       errno = 0;
        return True;
 }
 
 /*******************************************************************
-return True if the filename is one of the special executable types
+ Return True if the filename is one of the special executable types.
 ********************************************************************/
 
 static BOOL is_executable(const char *fname)
@@ -262,12 +348,13 @@ static BOOL is_executable(const char *fname)
 enum {AFAIL,AREAD,AWRITE,AALL};
 
 /*******************************************************************
-reproduce the share mode access table
-this is horrendoously complex, and really can't be justified on any
-rational grounds except that this is _exactly_ what NT does. See
-the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
-test routines.
+ Reproduce the share mode access table.
+ This is horrendoously complex, and really can't be justified on any
+ rational grounds except that this is _exactly_ what NT does. See
+ the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
+ test routines.
 ********************************************************************/
+
 static int access_table(int new_deny,int old_deny,int old_mode,
                        BOOL same_pid, BOOL isexe)
 {
@@ -353,9 +440,8 @@ static int access_table(int new_deny,int old_deny,int old_mode,
          return(AFAIL);      
 }
 
-
 /****************************************************************************
-check if we can open a file with a share mode
+ Check if we can open a file with a share mode.
 ****************************************************************************/
 
 static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, int share_mode, uint32 desired_access,
@@ -375,13 +461,13 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
        if(!lp_share_modes(SNUM(conn)))
                return True;
 
-       if (desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
+       if (desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
                non_io_open_request = False;
        } else {
                non_io_open_request = True;
        }
 
-       if (share->desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
+       if (share->desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
                non_io_open_existing = False;
        } else {
                non_io_open_existing = True;
@@ -396,9 +482,7 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
                DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
                        fname ));
                /* Use errno to map to correct error. */
-               unix_ERR_class = SMB_SUCCESS;
-               unix_ERR_code = 0;
-               unix_ERR_ntstatus = NT_STATUS_OK;
+               set_saved_error_triple(SMB_SUCCESS, 0, NT_STATUS_OK);
                return False;
        }
 
@@ -438,10 +522,7 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
                                (!GET_ALLOW_SHARE_DELETE(share->share_mode) || !GET_ALLOW_SHARE_DELETE(share_mode))) {
                        DEBUG(5,("check_share_mode: Failing open on file %s as delete access requests conflict.\n",
                                fname ));
-                       unix_ERR_class = ERRDOS;
-                       unix_ERR_code = ERRbadshare;
-                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
-
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        return False;
                }
 
@@ -466,10 +547,7 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
        if ((desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share->share_mode)) {
                DEBUG(5,("check_share_mode: Failing open on file %s as delete access requested and allow share delete not set.\n",
                        fname ));
-               unix_ERR_class = ERRDOS;
-               unix_ERR_code = ERRbadshare;
-               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
-
+               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                return False;
        }
 
@@ -482,12 +560,17 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
        if ((share->desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share_mode)) {
                DEBUG(5,("check_share_mode: Failing open on file %s as delete access granted and allow share delete not requested.\n",
                        fname ));
-               unix_ERR_class = ERRDOS;
-               unix_ERR_code = ERRbadshare;
-               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
+               return False;
+       }
 
+#if 0
+       /* Bluarc test may need this ... needs further investigation. */
+       if (deny_mode == DENY_ALL || old_deny_mode == DENY_ALL) {
+               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                return False;
        }
+#endif
 
        /*
         * If desired_access doesn't contain READ_DATA,WRITE_DATA,APPEND_DATA or EXECUTE
@@ -496,7 +579,7 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
 
        if ( !(desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ||
                !(share->desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ) {
-               DEBUG(5,("check_share_mode: Allowing open on file %s as desired access (0x%x) doesn't conflict with\
+               DEBUG(5,("check_share_mode: Allowing open on file %s as desired access (0x%x) doesn't conflict with \
 existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
                return True;
        }
@@ -514,10 +597,7 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
                                deny_mode,old_deny_mode,old_open_mode,
                                (int)share->pid,fname, fcbopen, *flags, access_allowed));
 
-                       unix_ERR_class = ERRDOS;
-                       unix_ERR_code = ERRbadshare;
-                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
-
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        return False;
                }
 
@@ -586,7 +666,7 @@ static int open_mode_check(connection_struct *conn, const char *fname, SMB_DEV_T
        int i;
        int num_share_modes;
        int oplock_contention_count = 0;
-       share_mode_entry *old_shares = 0;
+       share_mode_entry *old_shares = NULL;
        BOOL fcbopen = False;
        BOOL broke_oplock;
 
@@ -595,12 +675,15 @@ static int open_mode_check(connection_struct *conn, const char *fname, SMB_DEV_T
        
        num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
        
-       if(num_share_modes == 0)
+       if(num_share_modes == 0) {
+               SAFE_FREE(old_shares);
                return 0;
+       }
        
        if (desired_access && ((desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES))==0) &&
                ((desired_access & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) != 0)) {
                /* Stat open that doesn't trigger oplock breaks or share mode checks... ! JRA. */
+               SAFE_FREE(old_shares);
                return num_share_modes;
        }
 
@@ -656,7 +739,7 @@ dev = %x, inode = %.0f\n", *p_oplock_request, share_entry->op_type, fname, (unsi
                                /* Oplock break - unlock to request it. */
                                unlock_share_entry(conn, dev, inode);
                                
-                               opb_ret = request_oplock_break(share_entry, False);
+                               opb_ret = request_oplock_break(share_entry);
                                
                                /* Now relock. */
                                lock_share_entry(conn, dev, inode);
@@ -665,14 +748,11 @@ dev = %x, inode = %.0f\n", *p_oplock_request, share_entry->op_type, fname, (unsi
                                        DEBUG(0,("open_mode_check: FAILED when breaking oplock (%x) on file %s, \
 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
                                        SAFE_FREE(old_shares);
-                                       errno = EACCES;
-                                       unix_ERR_class = ERRDOS;
-                                       unix_ERR_code = ERRbadshare;
-                                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                                        return -1;
                                }
                                
-                               broken_entry = malloc(sizeof(struct share_mode_entry_list));
+                               broken_entry = SMB_MALLOC_P(struct share_mode_entry_list);
                                if (!broken_entry) {
                                        smb_panic("open_mode_check: malloc fail.\n");
                                }
@@ -732,9 +812,7 @@ after break ! For file %s, dev = %x, inode = %.0f. Deleting it to continue...\n"
                                        if (del_share_entry(dev, inode, &broken_entry->entry, NULL) == -1) {
                                                free_broken_entry_list(broken_entry_list);
                                                errno = EACCES;
-                                               unix_ERR_class = ERRDOS;
-                                               unix_ERR_code = ERRbadshare;
-                                               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                                               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                                                return -1;
                                        }
                                        
@@ -752,9 +830,6 @@ after break ! For file %s, dev = %x, inode = %.0f. Deleting it to continue...\n"
                free_broken_entry_list(broken_entry_list);
        } while(broke_oplock);
        
-       if(old_shares != 0)
-               SAFE_FREE(old_shares);
-       
        /*
         * Refuse to grant an oplock in case the contention limit is
         * reached when going through the lock list multiple times.
@@ -766,13 +841,124 @@ after break ! For file %s, dev = %x, inode = %.0f. Deleting it to continue...\n"
                         oplock_contention_count ));
        }
        
+       SAFE_FREE(old_shares);
        return num_share_modes;
 }
 
 /****************************************************************************
-set a kernel flock on a file for NFS interoperability
-this requires a patch to Linux
+ Delete the record for a handled deferred open entry.
+****************************************************************************/
+
+static void delete_defered_open_entry_record(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T inode)
+{
+       uint16 mid = get_current_mid();
+       pid_t mypid = sys_getpid();
+       deferred_open_entry *de_array = NULL;
+       int num_de_entries, i;
+
+       if (!lp_defer_sharing_violations()) {
+               return;
+       }
+
+       num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
+       for (i = 0; i < num_de_entries; i++) {
+               deferred_open_entry *entry = &de_array[i];
+               if (entry->pid == mypid && entry->mid == mid && entry->dev == dev &&
+                       entry->inode == inode) {
+
+                       /* Remove the deferred open entry from the array. */
+                       delete_deferred_open_entry(entry);
+                       SAFE_FREE(de_array);
+                       return;
+               }
+       }
+       SAFE_FREE(de_array);
+}
+
+/****************************************************************************
+ Handle the 1 second delay in returning a SHARING_VIOLATION error.
+****************************************************************************/
+
+void defer_open_sharing_error(connection_struct *conn, struct timeval *ptv,
+               char *fname, SMB_DEV_T dev, SMB_INO_T inode)
+{
+       uint16 mid = get_current_mid();
+       pid_t mypid = sys_getpid();
+       deferred_open_entry *de_array = NULL;
+       int num_de_entries, i;
+       struct dev_inode_bundle dib;
+
+       if (!lp_defer_sharing_violations()) {
+               return;
+       }
+
+       dib.dev = dev;
+       dib.inode = inode;
+
+       num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
+       for (i = 0; i < num_de_entries; i++) {
+               deferred_open_entry *entry = &de_array[i];
+               if (entry->pid == mypid && entry->mid == mid) {
+                       /*
+                        * Check if a 1 second timeout has expired.
+                        */
+                       if (usec_time_diff(ptv, &entry->time) > SHARING_VIOLATION_USEC_WAIT) {
+                               DEBUG(10,("defer_open_sharing_error: Deleting deferred open entry for mid %u, \
+file %s\n",
+                                       (unsigned int)mid, fname ));
+
+                               /* Expired, return a real error. */
+                               /* Remove the deferred open entry from the array. */
+
+                               delete_deferred_open_entry(entry);
+                               SAFE_FREE(de_array);
+                               return;
+                       }
+                       /*
+                        * If the timeout hasn't expired yet and we still have a sharing violation,
+                        * just leave the entry in the deferred open array alone. We do need to
+                        * reschedule this open call though (with the original created time).
+                        */
+                       DEBUG(10,("defer_open_sharing_error: time [%u.%06u] updating \
+deferred open entry for mid %u, file %s\n",
+                               (unsigned int)entry->time.tv_sec,
+                               (unsigned int)entry->time.tv_usec,
+                               (unsigned int)mid, fname ));
+
+                       push_sharing_violation_open_smb_message(&entry->time, (char *)&dib, sizeof(dib));
+                       SAFE_FREE(de_array);
+                       return;
+               }
+       }
+
+       DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred open entry for mid %u, file %s\n",
+               (unsigned int)ptv->tv_sec, (unsigned int)ptv->tv_usec, (unsigned int)mid, fname ));
+
+       if (!push_sharing_violation_open_smb_message(ptv, (char *)&dib, sizeof(dib))) {
+               SAFE_FREE(de_array);
+               return;
+       }
+       if (!add_deferred_open(mid, ptv, dev, inode, global_oplock_port, fname)) {
+               remove_sharing_violation_open_smb_message(mid);
+       }
+
+       /*
+        * Push the MID of this packet on the signing queue.
+        * We only do this once, the first time we push the packet
+        * onto the deferred open queue, as this has a side effect
+        * of incrementing the response sequence number.
+        */
+
+       srv_defer_sign_response(mid);
+
+       SAFE_FREE(de_array);
+}
+
+/****************************************************************************
+ Set a kernel flock on a file for NFS interoperability.
+ This requires a patch to Linux.
 ****************************************************************************/
+
 static void kernel_flock(files_struct *fsp, int deny_mode)
 {
 #if HAVE_KERNEL_SHARE_MODES
@@ -847,6 +1033,7 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
        BOOL fcbopen = False;
        BOOL def_acl = False;
        BOOL add_share_mode = True;
+       BOOL internal_only_open = False;
        SMB_DEV_T dev = 0;
        SMB_INO_T inode = 0;
        int num_share_modes = 0;
@@ -858,28 +1045,62 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
        mode_t new_mode = (mode_t)0;
        int action;
        uint32 existing_dos_mode = 0;
+       struct pending_message_list *pml = NULL;
+       uint16 mid = get_current_mid();
        /* We add aARCH to this as this mode is only used if the file is created new. */
-       mode_t mode = unix_mode(conn,new_dos_mode | aARCH,fname);
+       mode_t mode = unix_mode(conn,new_dos_mode | aARCH,fname, True);
+
+       if (oplock_request == INTERNAL_OPEN_ONLY) {
+               internal_only_open = True;
+               oplock_request = 0;
+       }
+
+       if ((pml = get_open_deferred_message(mid)) != NULL) {
+               struct dev_inode_bundle dib;
+
+               memcpy(&dib, pml->private_data.data, sizeof(dib));
+
+               /* There could be a race condition where the dev/inode pair
+                       has changed since we deferred the message. If so, just
+                       remove the deferred open entry and return sharing violation. */
+
+               /* If the timeout value is non-zero, we need to just
+                       return sharing violation. Don't retry the open
+                       as we were not notified of a close and we don't want to
+                       trigger another spurious oplock break. */
+
+               if (!file_existed || dib.dev != psbuf->st_dev || dib.inode != psbuf->st_ino ||
+                               pml->msg_time.tv_sec || pml->msg_time.tv_usec) {
+                       /* Ensure we don't reprocess this message. */
+                       remove_sharing_violation_open_smb_message(mid);
+
+                       /* Now remove the deferred open entry under lock. */
+                       lock_share_entry(conn, dib.dev, dib.inode);
+                       delete_defered_open_entry_record(conn, dib.dev, dib.inode);
+                       unlock_share_entry(conn, dib.dev, dib.inode);
+
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
+                       return NULL;
+               }
+               /* Ensure we don't reprocess this message. */
+               remove_sharing_violation_open_smb_message(mid);
+
+       }
 
        if (conn->printer) {
                /* printers are handled completely differently. Most of the passed parameters are
                        ignored */
                if (Access)
                        *Access = DOS_OPEN_WRONLY;
-               if (action)
+               if (paction)
                        *paction = FILE_WAS_CREATED;
                return print_fsp_open(conn, fname);
        }
 
-       fsp = file_new(conn);
-       if(!fsp)
-               return NULL;
-
        DEBUG(10,("open_file_shared: fname = %s, dos_attrs = %x, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
                fname, new_dos_mode, share_mode, ofun, (int)mode,  oplock_request ));
 
        if (!check_name(fname,conn)) {
-               file_free(fsp);
                return NULL;
        } 
 
@@ -893,25 +1114,17 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                oplock_request = 0;
        }
 
-       /* this is for OS/2 EAs - try and say we don't support them */
+       /* this is for OS/2 long file names - say we don't support them */
        if (strstr(fname,".+,;=[].")) {
-               unix_ERR_class = ERRDOS;
                /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
-#if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */
-               unix_ERR_code = ERRcannotopen;
-#else /* OS2_WPS_FIX */
-               unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
-#endif /* OS2_WPS_FIX */
-
-               DEBUG(5,("open_file_shared: OS/2 EA's are not supported.\n"));
-               file_free(fsp);
+               set_saved_error_triple(ERRDOS, ERRcannotopen, NT_STATUS_OBJECT_NAME_NOT_FOUND);
+               DEBUG(5,("open_file_shared: OS/2 long filenames are not supported.\n"));
                return NULL;
        }
 
        if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed)  {
                DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
                        fname ));
-               file_free(fsp);
                if (S_ISDIR(psbuf->st_mode)) {
                        errno = EISDIR;
                } else {
@@ -933,7 +1146,6 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                        DEBUG(5,("open_file_shared: attributes missmatch for file %s (%x %x) (0%o, 0%o)\n",
                                                fname, existing_dos_mode, new_dos_mode,
                                                (int)psbuf->st_mode, (int)mode ));
-                       file_free(fsp);
                        errno = EACCES;
                        return NULL;
                }
@@ -946,6 +1158,12 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                append does not mean the same thing under dos and unix */
 
        switch (GET_OPEN_MODE(share_mode)) {
+               case DOS_OPEN_EXEC:
+               case DOS_OPEN_RDONLY:
+                       flags = O_RDONLY;
+                       if (desired_access == 0)
+                               desired_access = FILE_READ_DATA;
+                       break;
                case DOS_OPEN_WRONLY: 
                        flags = O_WRONLY; 
                        if (desired_access == 0)
@@ -963,10 +1181,9 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                                desired_access = FILE_READ_DATA|FILE_WRITE_DATA;
                        break;
                default:
-                       flags = O_RDONLY;
-                       if (desired_access == 0)
-                               desired_access = FILE_READ_DATA;
-                       break;
+                       /* Force DOS error. */
+                       set_saved_error_triple(ERRDOS, ERRinvalidparam, NT_STATUS_INVALID);
+                       return NULL;
        }
 
 #if defined(O_SYNC)
@@ -980,7 +1197,6 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                if (!fcbopen) {
                        DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
                                fname, !CAN_WRITE(conn) ? "share" : "file" ));
-                       file_free(fsp);
                        errno = EACCES;
                        return NULL;
                }
@@ -989,7 +1205,6 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
 
        if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) {
                DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
-               file_free(fsp);
                errno = EINVAL;
                return NULL;
        }
@@ -1005,6 +1220,10 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                }
        }
 
+       fsp = file_new(conn);
+       if(!fsp)
+               return NULL;
+
        if (file_existed) {
 
                dev = psbuf->st_dev;
@@ -1038,22 +1257,34 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                flags,(flags2&~(O_TRUNC|O_CREAT)),(int)mode,(int)fsp_open ));
 
                        if (!fsp_open && errno) {
-                               unix_ERR_class = ERRDOS;
-                               unix_ERR_code = ERRnoaccess;
-                               unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
+                               /* Default error. */
+                               set_saved_error_triple(ERRDOS, ERRnoaccess, NT_STATUS_ACCESS_DENIED);
+                       }
+
+                       /* 
+                        * If we're returning a share violation, ensure we cope with
+                        * the braindead 1 second delay.
+                        */
+
+                       if (!internal_only_open) {
+                               NTSTATUS status;
+                               get_saved_error_triple(NULL, NULL, &status);
+                               if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION)) {
+                                       /* The fsp->open_time here represents the current time of day. */
+                                       defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
+                               }
                        }
 
                        unlock_share_entry(conn, dev, inode);
-                       if (fsp_open)
+                       if (fsp_open) {
                                fd_close(conn, fsp);
+                               /*
+                                * We have detected a sharing violation here
+                                * so return the correct error code
+                                */
+                               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
+                       }
                        file_free(fsp);
-                       /*
-                        * We have detected a sharing violation here
-                        * so return the correct error code
-                        */
-                        unix_ERR_class = ERRDOS;
-                        unix_ERR_code = ERRbadshare;
-                        unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
                        return NULL;
                }
 
@@ -1118,6 +1349,18 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                                  &flags, &oplock_request, &all_current_opens_are_level_II);
 
                if(num_share_modes == -1) {
+                       /* 
+                        * If we're returning a share violation, ensure we cope with
+                        * the braindead 1 second delay.
+                        */
+
+                       NTSTATUS status;
+                       get_saved_error_triple(NULL, NULL, &status);
+                       if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION)) {
+                               /* The fsp->open_time here represents the current time of day. */
+                               defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
+                       }
+
                        unlock_share_entry_fsp(fsp);
                        fd_close(conn,fsp);
                        file_free(fsp);
@@ -1125,9 +1368,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                         * We have detected a sharing violation here, so
                         * return the correct code.
                         */
-                        unix_ERR_class = ERRDOS;
-                        unix_ERR_code = ERRbadshare;
-                        unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        return NULL;
                }
 
@@ -1193,15 +1434,22 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
        DEBUG(10,("open_file_shared : share_mode = %x\n", fsp->share_mode ));
 
        if (Access) {
-               (*Access) = open_mode;
+               (*Access) = (SET_DENY_MODE(deny_mode) | SET_OPEN_MODE(open_mode));
        }
 
+       action = 0;
+
        if (file_existed && !(flags2 & O_TRUNC))
                action = FILE_WAS_OPENED;
        if (file_existed && (flags2 & O_TRUNC))
                action = FILE_WAS_OVERWRITTEN;
-       if (!file_existed) 
+       if (!file_existed) {
                action = FILE_WAS_CREATED;
+               /* Change the owner if required. */
+               if (lp_inherit_owner(SNUM(conn))) {
+                       change_owner_to_parent(conn, fsp, fsp->fsp_name, psbuf);
+               }
+       }
 
        if (paction) {
                *paction = action;
@@ -1229,9 +1477,17 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
        }
 
        if (delete_on_close) {
-               NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close);
+               uint32 dosmode = existing_dos_mode;
+               NTSTATUS result;
+
+               if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
+                       dosmode = new_dos_mode;
+               }
+               result = set_delete_on_close_internal(fsp, delete_on_close, dosmode);
 
                if (NT_STATUS_V(result) !=  NT_STATUS_V(NT_STATUS_OK)) {
+                       uint8 u_e_c;
+                       uint32 u_e_code;
                        /* Remember to delete the mode we just added. */
                        if (add_share_mode) {
                                del_share_mode(fsp, NULL);
@@ -1239,6 +1495,8 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                        unlock_share_entry_fsp(fsp);
                        fd_close(conn,fsp);
                        file_free(fsp);
+                       ntstatus_to_dos(result, &u_e_c, &u_e_code);
+                       set_saved_error_triple(u_e_c, u_e_code, result);
                        return NULL;
                }
        }
@@ -1246,7 +1504,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
        if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
                /* Files should be initially set as archive */
                if (lp_map_archive(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
-                       file_set_dosmode(conn, fname, new_dos_mode | aARCH, NULL);
+                       file_set_dosmode(conn, fname, new_dos_mode | aARCH, NULL, True);
                }
        }
 
@@ -1286,6 +1544,8 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                fname, (int)new_mode));
        }
 
+       /* If this is a successful open, we must remove any deferred open records. */
+       delete_defered_open_entry_record(conn, fsp->dev, fsp->inode);
        unlock_share_entry_fsp(fsp);
 
        conn->num_files_open++;
@@ -1342,10 +1602,9 @@ int close_file_fchmod(files_struct *fsp)
  Open a directory from an NT SMB call.
 ****************************************************************************/
 
-files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf,
+files_struct *open_directory(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf,
                        uint32 desired_access, int share_mode, int smb_ofun, int *action)
 {
-       extern struct current_user current_user;
        BOOL got_stat = False;
        files_struct *fsp = file_new(conn);
        BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode);
@@ -1380,39 +1639,29 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
                         * Try and create the directory.
                         */
 
-                       if(!CAN_WRITE(conn)) {
-                               DEBUG(2,("open_directory: failing create on read-only share\n"));
-                               file_free(fsp);
-                               errno = EACCES;
-                               return NULL;
-                       }
+                       /* We know bad_path is false as it's caught earlier. */
 
-                       if (ms_has_wild(fname))  {
-                               file_free(fsp);
-                               DEBUG(5,("open_directory: failing create on filename %s with wildcards\n", fname));
-                               unix_ERR_class = ERRDOS;
-                               unix_ERR_code = ERRinvalidname;
-                               unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID;
-                               return NULL;
-                       }
+                       NTSTATUS status = mkdir_internal(conn, fname, False);
 
-                       if( strchr_m(fname, ':')) {
+                       if (!NT_STATUS_IS_OK(status)) {
+                               DEBUG(2,("open_directory: unable to create %s. Error was %s\n",
+                                        fname, strerror(errno) ));
                                file_free(fsp);
-                               DEBUG(5,("open_directory: failing create on filename %s with colon in name\n", fname));
-                               unix_ERR_class = ERRDOS;
-                               unix_ERR_code = ERRinvalidname;
-                               unix_ERR_ntstatus = NT_STATUS_NOT_A_DIRECTORY;
+                               /* Ensure we return the correct NT status to the client. */
+                               set_saved_error_triple(0, 0, status);
                                return NULL;
                        }
 
-                       if(vfs_MkDir(conn,fname, unix_mode(conn,aDIR, fname)) < 0) {
-                               DEBUG(2,("open_directory: unable to create %s. Error was %s\n",
-                                        fname, strerror(errno) ));
+                       /* Ensure we're checking for a symlink here.... */
+                       /* We don't want to get caught by a symlink racer. */
+
+                       if(SMB_VFS_LSTAT(conn,fname, psbuf) != 0) {
                                file_free(fsp);
                                return NULL;
                        }
 
-                       if(SMB_VFS_STAT(conn,fname, psbuf) != 0) {
+                       if(!S_ISDIR(psbuf->st_mode)) {
+                               DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
                                file_free(fsp);
                                return NULL;
                        }
@@ -1451,7 +1700,6 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
        fsp->mode = psbuf->st_mode;
        fsp->inode = psbuf->st_ino;
        fsp->dev = psbuf->st_dev;
-       fsp->size = psbuf->st_size;
        fsp->vuid = current_user.vuid;
        fsp->file_pid = global_smbpid;
        fsp->can_lock = True;
@@ -1469,13 +1717,19 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
        string_set(&fsp->fsp_name,fname);
 
        if (delete_on_close) {
-               NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close);
+               NTSTATUS status = set_delete_on_close_internal(fsp, delete_on_close, 0);
 
-               if (NT_STATUS_V(result) !=  NT_STATUS_V(NT_STATUS_OK)) {
+               if (!NT_STATUS_IS_OK(status)) {
                        file_free(fsp);
                        return NULL;
                }
        }
+
+       /* Change the owner if required. */
+       if ((*action == FILE_WAS_CREATED) && lp_inherit_owner(SNUM(conn))) {
+               change_owner_to_parent(conn, fsp, fsp->fsp_name, psbuf);
+       }
+
        conn->num_files_open++;
 
        return fsp;
@@ -1487,7 +1741,6 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
 
 files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf)
 {
-       extern struct current_user current_user;
        files_struct *fsp = NULL;
 
        if (!VALID_STAT(*psbuf))
@@ -1510,7 +1763,6 @@ files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_ST
        fsp->mode = psbuf->st_mode;
        fsp->inode = psbuf->st_ino;
        fsp->dev = psbuf->st_dev;
-       fsp->size = psbuf->st_size;
        fsp->vuid = current_user.vuid;
        fsp->file_pid = global_smbpid;
        fsp->can_lock = False;