r7963: Add aio support to 3.0.
[jra/samba/.git] / source3 / smbd / open.c
index aca9756628648fe10fc33193e6ccb65d608a50d1..9da53a5057a96116e457157010bfbd7cb6ff5c4d 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "includes.h"
 
+extern struct current_user current_user;
 extern userdom_struct current_user_info;
 extern uint16 global_oplock_port;
 extern uint16 global_smbpid;
@@ -76,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);
        }
 }
 
@@ -89,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;
 
@@ -165,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;
                }
 
@@ -223,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);
@@ -236,6 +316,10 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
        fsp->is_directory = False;
        fsp->is_stat = False;
        fsp->directory_delete_on_close = False;
+       if (conn->aio_write_behind_list && is_in_path(fname, conn->aio_write_behind_list, conn->case_sensitive)) {
+               fsp->aio_write_behind = True;
+       }
+
        string_set(&fsp->fsp_name,fname);
        fsp->wcp = NULL; /* Write cache pointer. */
 
@@ -244,6 +328,7 @@ 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;
 }
 
@@ -380,13 +465,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;
@@ -401,9 +486,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;
        }
 
@@ -443,10 +526,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;
                }
 
@@ -471,10 +551,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;
        }
 
@@ -487,12 +564,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
@@ -501,7 +583,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;
        }
@@ -519,10 +601,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;
                }
 
@@ -591,7 +670,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;
 
@@ -600,12 +679,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;
        }
 
@@ -661,7 +743,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);
@@ -670,14 +752,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");
                                }
@@ -737,9 +816,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;
                                        }
                                        
@@ -757,9 +834,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.
@@ -771,6 +845,7 @@ 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;
 }
 
@@ -785,6 +860,10 @@ static void delete_defered_open_entry_record(connection_struct *conn, SMB_DEV_T
        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];
@@ -813,6 +892,10 @@ void defer_open_sharing_error(connection_struct *conn, struct timeval *ptv,
        int num_de_entries, i;
        struct dev_inode_bundle dib;
 
+       if (!lp_defer_sharing_violations()) {
+               return;
+       }
+
        dib.dev = dev;
        dib.inode = inode;
 
@@ -969,7 +1052,7 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
        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;
@@ -1000,9 +1083,7 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                        delete_defered_open_entry_record(conn, dib.dev, dib.inode);
                        unlock_share_entry(conn, dib.dev, dib.inode);
 
-                       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;
                }
                /* Ensure we don't reprocess this message. */
@@ -1015,20 +1096,15 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                        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;
        } 
 
@@ -1042,25 +1118,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 {
@@ -1082,7 +1150,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;
                }
@@ -1095,6 +1162,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)
@@ -1112,10 +1185,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)
@@ -1129,7 +1201,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;
                }
@@ -1138,7 +1209,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;
        }
@@ -1154,6 +1224,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;
@@ -1187,9 +1261,8 @@ 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);
                        }
 
                        /* 
@@ -1197,9 +1270,13 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                         * the braindead 1 second delay.
                         */
 
-                       if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,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);
+                       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);
@@ -1209,9 +1286,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                 * 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;
+                               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        }
                        file_free(fsp);
                        return NULL;
@@ -1283,7 +1358,9 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                         * the braindead 1 second delay.
                         */
 
-                       if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,NT_STATUS_SHARING_VIOLATION)) {
+                       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);
                        }
@@ -1295,9 +1372,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;
                }
 
@@ -1363,15 +1438,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;
@@ -1399,9 +1481,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);
@@ -1409,6 +1499,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;
                }
        }
@@ -1416,7 +1508,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);
                }
        }
 
@@ -1514,10 +1606,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);
@@ -1552,39 +1643,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;
                        }
@@ -1623,7 +1704,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;
@@ -1641,13 +1721,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;
@@ -1659,7 +1745,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))
@@ -1682,7 +1767,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;