Fix bug #10010 - Missing integer wrap protection in EA list reading can cause server...
[samba.git] / source3 / smbd / nttrans.c
index 7e75eea6b4da93f4ae5faf238955e1c0f1ce17d6..25597696b0ba99455863e84fed717b7674917dde 100644 (file)
@@ -21,7 +21,6 @@
 #include "includes.h"
 #include "smbd/globals.h"
 
-extern enum protocol_types Protocol;
 extern const struct generic_mapping file_generic_mapping;
 
 static char *nttrans_realloc(char **ptr, size_t size)
@@ -58,6 +57,8 @@ void send_nt_replies(connection_struct *conn,
        int params_sent_thistime, data_sent_thistime, total_sent_thistime;
        int alignment_offset = 3;
        int data_alignment_offset = 0;
+       struct smbd_server_connection *sconn = smbd_server_conn;
+       int max_send = sconn->smb1.sessions.max_send;
 
        /*
         * If there genuinely are no parameters or data to send just send
@@ -66,7 +67,20 @@ void send_nt_replies(connection_struct *conn,
 
        if(params_to_send == 0 && data_to_send == 0) {
                reply_outbuf(req, 18, 0);
+               if (NT_STATUS_V(nt_error)) {
+                       error_packet_set((char *)req->outbuf,
+                                        0, 0, nt_error,
+                                        __LINE__,__FILE__);
+               }
                show_msg((char *)req->outbuf);
+               if (!srv_send_smb(smbd_server_fd(),
+                               (char *)req->outbuf,
+                               true, req->seqnum+1,
+                               IS_CONN_ENCRYPTED(conn),
+                               &req->pcd)) {
+                       exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
+               }
+               TALLOC_FREE(req->outbuf);
                return;
        }
 
@@ -119,11 +133,6 @@ void send_nt_replies(connection_struct *conn,
                             total_sent_thistime + alignment_offset
                             + data_alignment_offset);
 
-               /*
-                * We might have had SMBnttranss in req->inbuf, fix that.
-                */
-               SCVAL(req->outbuf, smb_com, SMBnttrans);
-
                /*
                 * Set total params and data to be sent.
                 */
@@ -256,53 +265,6 @@ void send_nt_replies(connection_struct *conn,
        }
 }
 
-/****************************************************************************
- Is it an NTFS stream name ?
- An NTFS file name is <path>.<extention>:<stream name>:<stream type>
- $DATA can be used as both a stream name and a stream type. A missing stream
- name or type implies $DATA.
-
- Both Windows stream names and POSIX files can contain the ':' character.
- This function first checks for the existence of a colon in the last component
- of the given name.  If the name contains a colon we differentiate between a
- stream and POSIX file by checking if the latter exists through a POSIX stat.
-
- Function assumes we've already chdir() to the "root" directory of fname.
-****************************************************************************/
-
-bool is_ntfs_stream_name(const char *fname)
-{
-       const char *lastcomp;
-       SMB_STRUCT_STAT sbuf;
-
-       /* If all pathnames are treated as POSIX we ignore streams. */
-       if (lp_posix_pathnames()) {
-               return false;
-       }
-
-       /* Find the last component of the name. */
-       if ((lastcomp = strrchr_m(fname, '/')) != NULL)
-               ++lastcomp;
-       else
-               lastcomp = fname;
-
-       /* If there is no colon in the last component, it's not a stream. */
-       if (strchr_m(lastcomp, ':') == NULL)
-               return false;
-
-       /*
-        * If file already exists on disk, it's not a stream. The stat must
-        * bypass the vfs layer so streams modules don't intefere.
-        */
-       if (sys_stat(fname, &sbuf) == 0) {
-               DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
-                       "not a stream\n", fname));
-               return false;
-       }
-
-       return true;
-}
-
 /****************************************************************************
  Reply to an NT create and X call on a pipe
 ****************************************************************************/
@@ -407,6 +369,53 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
        chain_reply(req);
 }
 
+struct case_semantics_state {
+       connection_struct *conn;
+       bool case_sensitive;
+       bool case_preserve;
+       bool short_case_preserve;
+};
+
+/****************************************************************************
+ Restore case semantics.
+****************************************************************************/
+
+static int restore_case_semantics(struct case_semantics_state *state)
+{
+       state->conn->case_sensitive = state->case_sensitive;
+       state->conn->case_preserve = state->case_preserve;
+       state->conn->short_case_preserve = state->short_case_preserve;
+       return 0;
+}
+
+/****************************************************************************
+ Save case semantics.
+****************************************************************************/
+
+static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
+                                               connection_struct *conn)
+{
+       struct case_semantics_state *result;
+
+       if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
+               return NULL;
+       }
+
+       result->conn = conn;
+       result->case_sensitive = conn->case_sensitive;
+       result->case_preserve = conn->case_preserve;
+       result->short_case_preserve = conn->short_case_preserve;
+
+       /* Set to POSIX. */
+       conn->case_sensitive = True;
+       conn->case_preserve = True;
+       conn->short_case_preserve = True;
+
+       talloc_set_destructor(result, restore_case_semantics);
+
+       return result;
+}
+
 /****************************************************************************
  Reply to an NT create and X call.
 ****************************************************************************/
@@ -414,6 +423,7 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
 void reply_ntcreate_and_X(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
        uint32 flags;
        uint32 access_mask;
@@ -427,25 +437,25 @@ void reply_ntcreate_and_X(struct smb_request *req)
           reply bits separately. */
        uint32 fattr=0;
        SMB_OFF_T file_len = 0;
-       SMB_STRUCT_STAT sbuf;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
+       struct timespec create_timespec;
        struct timespec c_timespec;
        struct timespec a_timespec;
        struct timespec m_timespec;
+       struct timespec write_time_ts;
        NTSTATUS status;
        int oplock_request;
        uint8_t oplock_granted = NO_OPLOCK_RETURN;
+       struct case_semantics_state *case_state = NULL;
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBntcreateX);
 
-       SET_STAT_INVALID(sbuf);
-
        if (req->wct < 24) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
+               goto out;
        }
 
        flags = IVAL(req->vwv+3, 1);
@@ -466,8 +476,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
 
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBntcreateX);
-               return;
+               goto out;
        }
 
        DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
@@ -496,12 +505,10 @@ void reply_ntcreate_and_X(struct smb_request *req)
        if (IS_IPC(conn)) {
                if (lp_nt_pipe_support()) {
                        do_ntcreate_pipe_open(conn, req);
-                       END_PROFILE(SMBntcreateX);
-                       return;
+                       goto out;
                }
-               reply_doserror(req, ERRDOS, ERRnoaccess);
-               END_PROFILE(SMBntcreateX);
-               return;
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+               goto out;
        }
 
        oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
@@ -510,12 +517,47 @@ void reply_ntcreate_and_X(struct smb_request *req)
                        ? BATCH_OPLOCK : 0;
        }
 
+       if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
+               case_state = set_posix_case_semantics(ctx, conn);
+               if (!case_state) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       goto out;
+               }
+       }
+
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               0,
+                               NULL,
+                               &smb_fname);
+
+       TALLOC_FREE(case_state);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
+                       reply_botherror(req,
+                               NT_STATUS_PATH_NOT_COVERED,
+                               ERRSRV, ERRbadpath);
+                       goto out;
+               }
+               reply_nterror(req, status);
+               goto out;
+       }
+
+       /*
+        * Bug #6898 - clients using Windows opens should
+        * never be able to set this attribute into the
+        * VFS.
+        */
+       file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
+
        status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                root_dir_fid,                           /* root_dir_fid */
-               fname,                                  /* fname */
-               CFF_DOS_PATH,                           /* create_file_flags */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_access,                           /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -526,25 +568,21 @@ void reply_ntcreate_and_X(struct smb_request *req)
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               &info,                                  /* pinfo */
-               &sbuf);                                 /* psbuf */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call, no error. */
-                       END_PROFILE(SMBntcreateX);
-                       return;
+                       goto out;
                }
-               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
-                       reply_botherror(req, status, ERRDOS, ERRfilexists);
-               }
-               else {
-                       reply_nterror(req, status);
-               }
-               END_PROFILE(SMBntcreateX);
-               return;
+               reply_openerror(req, status);
+               goto out;
        }
 
+       /* Ensure we're pointing at the correct stat struct. */
+       TALLOC_FREE(smb_fname);
+       smb_fname = fsp->fsp_name;
+
        /*
         * If the caller set the extended oplock request bit
         * and we granted one (by whatever means) - set the
@@ -570,11 +608,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
                oplock_granted = NO_OPLOCK_RETURN;
        }
 
-       file_len = sbuf.st_size;
-       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
-       if (fattr == 0) {
-               fattr = FILE_ATTRIBUTE_NORMAL;
-       }
+       file_len = smb_fname->st.st_ex_size;
 
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                /* This is very strange. We
@@ -603,34 +637,66 @@ void reply_ntcreate_and_X(struct smb_request *req)
        }
        p += 4;
 
+       fattr = dos_mode(conn, smb_fname);
+       if (fattr == 0) {
+               fattr = FILE_ATTRIBUTE_NORMAL;
+       }
+
+       /* Deal with other possible opens having a modified
+          write time. JRA. */
+       ZERO_STRUCT(write_time_ts);
+       get_file_infos(fsp->file_id, NULL, &write_time_ts);
+       if (!null_timespec(write_time_ts)) {
+               update_stat_ex_mtime(&smb_fname->st, write_time_ts);
+       }
+
        /* Create time. */
-       c_timespec = get_create_timespec(
-               &sbuf,lp_fake_dir_create_times(SNUM(conn)));
-       a_timespec = get_atimespec(&sbuf);
-       m_timespec = get_mtimespec(&sbuf);
+       create_timespec = get_create_timespec(conn, fsp, smb_fname);
+       a_timespec = smb_fname->st.st_ex_atime;
+       m_timespec = smb_fname->st.st_ex_mtime;
+       c_timespec = get_change_timespec(conn, fsp, smb_fname);
 
        if (lp_dos_filetime_resolution(SNUM(conn))) {
-               dos_filetime_timespec(&c_timespec);
+               dos_filetime_timespec(&create_timespec);
                dos_filetime_timespec(&a_timespec);
                dos_filetime_timespec(&m_timespec);
+               dos_filetime_timespec(&c_timespec);
        }
 
-       put_long_date_timespec(p, c_timespec); /* create time. */
+       put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
        p += 8;
-       put_long_date_timespec(p, a_timespec); /* access time */
+       put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* write time */
+       put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* change time */
+       put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
        p += 8;
        SIVAL(p,0,fattr); /* File Attributes. */
        p += 4;
-       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
+       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
        p += 8;
        SOFF_T(p,0,file_len);
        p += 8;
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
-               SSVAL(p,2,0x7);
+               uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
+               size_t num_names = 0;
+               unsigned int num_streams;
+               struct stream_struct *streams = NULL;
+
+               /* Do we have any EA's ? */
+               status = get_ea_names_from_file(ctx, conn, fsp,
+                               smb_fname->base_name, NULL, &num_names);
+               if (NT_STATUS_IS_OK(status) && num_names) {
+                       file_status &= ~NO_EAS;
+               }
+               status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
+                       &num_streams, &streams);
+               /* There is always one stream, ::$DATA. */
+               if (NT_STATUS_IS_OK(status) && num_streams > 1) {
+                       file_status &= ~NO_SUBSTREAMS;
+               }
+               TALLOC_FREE(streams);
+               SSVAL(p,2,file_status);
        }
        p += 4;
        SCVAL(p,0,fsp->is_directory ? 1 : 0);
@@ -638,8 +704,8 @@ void reply_ntcreate_and_X(struct smb_request *req)
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                uint32 perms = 0;
                p += 25;
-               if (fsp->is_directory
-                   || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
+               if (fsp->is_directory ||
+                   can_write_to_file(conn, smb_fname)) {
                        perms = FILE_GENERIC_ALL;
                } else {
                        perms = FILE_GENERIC_READ|FILE_EXECUTE;
@@ -648,9 +714,10 @@ void reply_ntcreate_and_X(struct smb_request *req)
        }
 
        DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
-                fsp->fnum, fsp->fsp_name));
+               fsp->fnum, smb_fname_str_dbg(smb_fname)));
 
        chain_reply(req);
+ out:
        END_PROFILE(SMBntcreateX);
        return;
 }
@@ -680,7 +747,7 @@ static void do_nt_transact_create_pipe(connection_struct *conn,
 
        if(parameter_count < 54) {
                DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -710,7 +777,7 @@ static void do_nt_transact_create_pipe(connection_struct *conn,
        }
        params = nttrans_realloc(ppparams, param_len);
        if(params == NULL) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
@@ -761,7 +828,15 @@ static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
        SEC_DESC *psd = NULL;
        NTSTATUS status;
 
-       if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
+       if (sd_len == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (!CAN_WRITE(fsp->conn)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if (!lp_nt_acl_support(SNUM(fsp->conn))) {
                return NT_STATUS_OK;
        }
 
@@ -778,12 +853,52 @@ static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
                security_info_sent &= ~GROUP_SECURITY_INFORMATION;
        }
 
-       /* Convert all the generic bits. */
-       security_acl_map_generic(psd->dacl, &file_generic_mapping);
-       security_acl_map_generic(psd->sacl, &file_generic_mapping);
+       /* Ensure we have at least one thing set. */
+       if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
+               if (security_info_sent & SECINFO_LABEL) {
+                       /* Only consider SECINFO_LABEL if no other
+                          bits are set. Just like W2K3 we don't
+                          store this. */
+                       return NT_STATUS_OK;
+               }
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       /* Ensure we have the rights to do this. */
+       if (security_info_sent & SECINFO_OWNER) {
+               if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+       }
+
+       if (security_info_sent & SECINFO_GROUP) {
+               if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+       }
+
+       if (security_info_sent & SECINFO_DACL) {
+               if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+               /* Convert all the generic bits. */
+               if (psd->dacl) {
+                       security_acl_map_generic(psd->dacl, &file_generic_mapping);
+               }
+       }
+
+       if (security_info_sent & SECINFO_SACL) {
+               if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+               /* Convert all the generic bits. */
+               if (psd->sacl) {
+                       security_acl_map_generic(psd->sacl, &file_generic_mapping);
+               }
+       }
 
        if (DEBUGLEVEL >= 10) {
-               DEBUG(10,("set_sd for file %s\n", fsp->fsp_name ));
+               DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
                NDR_PRINT_DEBUG(security_descriptor, psd);
        }
 
@@ -798,7 +913,7 @@ static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
 ****************************************************************************/
 
-static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
+struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
 {
        struct ea_list *ea_list_head = NULL;
        size_t offset = 0;
@@ -819,7 +934,19 @@ static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata,
                if (next_offset == 0) {
                        break;
                }
+
+               /* Integer wrap protection for the increment. */
+               if (offset + next_offset < offset) {
+                       break;
+               }
+
                offset += next_offset;
+
+               /* Integer wrap protection for while loop. */
+               if (offset + 4 < offset) {
+                       break;
+               }
+
        }
 
        return ea_list_head;
@@ -836,13 +963,13 @@ static void call_nt_transact_create(connection_struct *conn,
                                    char **ppdata, uint32 data_count,
                                    uint32 max_data_count)
 {
+       struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
        char *params = *ppparams;
        char *data = *ppdata;
        /* Breakout the oplock request bits so we can set the reply bits separately. */
        uint32 fattr=0;
        SMB_OFF_T file_len = 0;
-       SMB_STRUCT_STAT sbuf;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
@@ -856,19 +983,20 @@ static void call_nt_transact_create(connection_struct *conn,
        struct security_descriptor *sd = NULL;
        uint32 ea_len;
        uint16 root_dir_fid;
+       struct timespec create_timespec;
        struct timespec c_timespec;
        struct timespec a_timespec;
        struct timespec m_timespec;
+       struct timespec write_time_ts;
        struct ea_list *ea_list = NULL;
        NTSTATUS status;
        size_t param_len;
        uint64_t allocation_size;
        int oplock_request;
        uint8_t oplock_granted;
+       struct case_semantics_state *case_state = NULL;
        TALLOC_CTX *ctx = talloc_tos();
 
-       SET_STAT_INVALID(sbuf);
-
        DEBUG(5,("call_nt_transact_create\n"));
 
        /*
@@ -882,10 +1010,10 @@ static void call_nt_transact_create(connection_struct *conn,
                                ppsetup, setup_count,
                                ppparams, parameter_count,
                                ppdata, data_count);
-                       return;
+                       goto out;
                }
-               reply_doserror(req, ERRDOS, ERRnoaccess);
-               return;
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+               goto out;
        }
 
        /*
@@ -895,7 +1023,7 @@ static void call_nt_transact_create(connection_struct *conn,
        if(parameter_count < 54) {
                DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
+               goto out;
        }
 
        flags = IVAL(params,0);
@@ -926,7 +1054,7 @@ static void call_nt_transact_create(connection_struct *conn,
                           "%u, data_count = %u\n", (unsigned int)ea_len,
                           (unsigned int)sd_len, (unsigned int)data_count));
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
+               goto out;
        }
 
        if (sd_len) {
@@ -940,7 +1068,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                   "unmarshall_sec_desc failed: %s\n",
                                   nt_errstr(status)));
                        reply_nterror(req, status);
-                       return;
+                       goto out;
                }
        }
 
@@ -950,7 +1078,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                   "EA's not supported.\n",
                                   (unsigned int)ea_len));
                        reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
-                       return;
+                       goto out;
                }
 
                if (ea_len < 10) {
@@ -958,7 +1086,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                  "too small (should be more than 10)\n",
                                  (unsigned int)ea_len ));
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
+                       goto out;
                }
 
                /* We have already checked that ea_len <= data_count here. */
@@ -966,7 +1094,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                               ea_len);
                if (ea_list == NULL) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
+                       goto out;
                }
        }
 
@@ -975,7 +1103,36 @@ static void call_nt_transact_create(connection_struct *conn,
                        STR_TERMINATE, &status);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               return;
+               goto out;
+       }
+
+       if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
+               case_state = set_posix_case_semantics(ctx, conn);
+               if (!case_state) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       goto out;
+               }
+       }
+
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               0,
+                               NULL,
+                               &smb_fname);
+
+       TALLOC_FREE(case_state);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
+                       reply_botherror(req,
+                               NT_STATUS_PATH_NOT_COVERED,
+                               ERRSRV, ERRbadpath);
+                       goto out;
+               }
+               reply_nterror(req, status);
+               goto out;
        }
 
        oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
@@ -984,12 +1141,18 @@ static void call_nt_transact_create(connection_struct *conn,
                        ? BATCH_OPLOCK : 0;
        }
 
+       /*
+        * Bug #6898 - clients using Windows opens should
+        * never be able to set this attribute into the
+        * VFS.
+        */
+       file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
+
        status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                root_dir_fid,                           /* root_dir_fid */
-               fname,                                  /* fname */
-               CFF_DOS_PATH,                           /* create_file_flags */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_access,                           /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -1000,8 +1163,7 @@ static void call_nt_transact_create(connection_struct *conn,
                sd,                                     /* sd */
                ea_list,                                /* ea_list */
                &fsp,                                   /* result */
-               &info,                                  /* pinfo */
-               &sbuf);                                 /* psbuf */
+               &info);                                 /* pinfo */
 
        if(!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -1009,9 +1171,13 @@ static void call_nt_transact_create(connection_struct *conn,
                        return;
                }
                reply_openerror(req, status);
-               return;
+               goto out;
        }
 
+       /* Ensure we're pointing at the correct stat struct. */
+       TALLOC_FREE(smb_fname);
+       smb_fname = fsp->fsp_name;
+
        /*
         * If the caller set the extended oplock request bit
         * and we granted one (by whatever means) - set the
@@ -1037,11 +1203,7 @@ static void call_nt_transact_create(connection_struct *conn,
                oplock_granted = NO_OPLOCK_RETURN;
        }
 
-       file_len = sbuf.st_size;
-       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
-       if (fattr == 0) {
-               fattr = FILE_ATTRIBUTE_NORMAL;
-       }
+       file_len = smb_fname->st.st_ex_size;
 
        /* Realloc the size of parameters and data we will return */
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
@@ -1052,8 +1214,8 @@ static void call_nt_transact_create(connection_struct *conn,
        }
        params = nttrans_realloc(ppparams, param_len);
        if(params == NULL) {
-               reply_doserror(req, ERRDOS, ERRnomem);
-               return;
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               goto out;
        }
 
        p = params;
@@ -1070,29 +1232,43 @@ static void call_nt_transact_create(connection_struct *conn,
        }
        p += 8;
 
+       fattr = dos_mode(conn, smb_fname);
+       if (fattr == 0) {
+               fattr = FILE_ATTRIBUTE_NORMAL;
+       }
+
+       /* Deal with other possible opens having a modified
+          write time. JRA. */
+       ZERO_STRUCT(write_time_ts);
+       get_file_infos(fsp->file_id, NULL, &write_time_ts);
+       if (!null_timespec(write_time_ts)) {
+               update_stat_ex_mtime(&smb_fname->st, write_time_ts);
+       }
+
        /* Create time. */
-       c_timespec = get_create_timespec(
-               &sbuf,lp_fake_dir_create_times(SNUM(conn)));
-       a_timespec = get_atimespec(&sbuf);
-       m_timespec = get_mtimespec(&sbuf);
+       create_timespec = get_create_timespec(conn, fsp, smb_fname);
+       a_timespec = smb_fname->st.st_ex_atime;
+       m_timespec = smb_fname->st.st_ex_mtime;
+       c_timespec = get_change_timespec(conn, fsp, smb_fname);
 
        if (lp_dos_filetime_resolution(SNUM(conn))) {
-               dos_filetime_timespec(&c_timespec);
+               dos_filetime_timespec(&create_timespec);
                dos_filetime_timespec(&a_timespec);
                dos_filetime_timespec(&m_timespec);
+               dos_filetime_timespec(&c_timespec);
        }
 
-       put_long_date_timespec(p, c_timespec); /* create time. */
+       put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
        p += 8;
-       put_long_date_timespec(p, a_timespec); /* access time */
+       put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* write time */
+       put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* change time */
+       put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
        p += 8;
        SIVAL(p,0,fattr); /* File Attributes. */
        p += 4;
-       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf));
+       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
        p += 8;
        SOFF_T(p,0,file_len);
        p += 8;
@@ -1105,8 +1281,8 @@ static void call_nt_transact_create(connection_struct *conn,
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                uint32 perms = 0;
                p += 25;
-               if (fsp->is_directory
-                   || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
+               if (fsp->is_directory ||
+                   can_write_to_file(conn, smb_fname)) {
                        perms = FILE_GENERIC_ALL;
                } else {
                        perms = FILE_GENERIC_READ|FILE_EXECUTE;
@@ -1114,11 +1290,12 @@ static void call_nt_transact_create(connection_struct *conn,
                SIVAL(p,0,perms);
        }
 
-       DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
+       DEBUG(5,("call_nt_transact_create: open name = %s\n",
+                smb_fname_str_dbg(smb_fname)));
 
        /* Send the required number of replies */
        send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
-
+ out:
        return;
 }
 
@@ -1151,15 +1328,10 @@ void reply_ntcancel(struct smb_request *req)
 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                                connection_struct *conn,
                                struct smb_request *req,
-                               const char *oldname_in,
-                               const char *newname_in,
+                               struct smb_filename *smb_fname_src,
+                               struct smb_filename *smb_fname_dst,
                                uint32 attrs)
 {
-       SMB_STRUCT_STAT sbuf1, sbuf2;
-       char *oldname = NULL;
-       char *newname = NULL;
-       char *last_component_oldname = NULL;
-       char *last_component_newname = NULL;
        files_struct *fsp1,*fsp2;
        uint32 fattr;
        int info;
@@ -1167,70 +1339,45 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        NTSTATUS status = NT_STATUS_OK;
        char *parent;
 
-       ZERO_STRUCT(sbuf1);
-       ZERO_STRUCT(sbuf2);
-
        if (!CAN_WRITE(conn)) {
-               return NT_STATUS_MEDIA_WRITE_PROTECTED;
-       }
-
-       status = unix_convert(ctx, conn, oldname_in, False, &oldname,
-                       &last_component_oldname, &sbuf1);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       status = check_name(conn, oldname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               status = NT_STATUS_MEDIA_WRITE_PROTECTED;
+               goto out;
        }
 
         /* Source must already exist. */
-       if (!VALID_STAT(sbuf1)) {
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       if (!VALID_STAT(smb_fname_src->st)) {
+               status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               goto out;
        }
+
        /* Ensure attributes match. */
-       fattr = dos_mode(conn,oldname,&sbuf1);
+       fattr = dos_mode(conn, smb_fname_src);
        if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
-               return NT_STATUS_NO_SUCH_FILE;
-       }
-
-       status = unix_convert(ctx, conn, newname_in, False, &newname,
-                       &last_component_newname, &sbuf2);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       status = check_name(conn, newname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               status = NT_STATUS_NO_SUCH_FILE;
+               goto out;
        }
 
-       /* Disallow if newname already exists. */
-       if (VALID_STAT(sbuf2)) {
-               return NT_STATUS_OBJECT_NAME_COLLISION;
+       /* Disallow if dst file already exists. */
+       if (VALID_STAT(smb_fname_dst->st)) {
+               status = NT_STATUS_OBJECT_NAME_COLLISION;
+               goto out;
        }
 
        /* No links from a directory. */
-       if (S_ISDIR(sbuf1.st_mode)) {
-               return NT_STATUS_FILE_IS_A_DIRECTORY;
-       }
-
-       /* Ensure this is within the share. */
-       status = check_reduced_name(conn, oldname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
+               status = NT_STATUS_FILE_IS_A_DIRECTORY;
+               goto out;
        }
 
        DEBUG(10,("copy_internals: doing file copy %s to %s\n",
-                               oldname, newname));
+                 smb_fname_str_dbg(smb_fname_src),
+                 smb_fname_str_dbg(smb_fname_dst)));
 
         status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               oldname,                                /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname_src,                          /* fname */
                FILE_READ_DATA,                         /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
@@ -1242,19 +1389,17 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp1,                                  /* result */
-               &info,                                  /* pinfo */
-               &sbuf1);                                /* psbuf */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
        }
 
         status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               newname,                                /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname_dst,                          /* fname */
                FILE_WRITE_DATA,                        /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
@@ -1266,16 +1411,15 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp2,                                  /* result */
-               &info,                                  /* pinfo */
-               &sbuf2);                                /* psbuf */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                close_file(NULL, fsp1, ERROR_CLOSE);
-               return status;
+               goto out;
        }
 
-       if (sbuf1.st_size) {
-               ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
+       if (smb_fname_src->st.st_ex_size) {
+               ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
        }
 
        /*
@@ -1287,27 +1431,32 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        close_file(NULL, fsp1, NORMAL_CLOSE);
 
        /* Ensure the modtime is set correctly on the destination file. */
-       set_close_write_time(fsp2, get_mtimespec(&sbuf1));
+       set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
 
        status = close_file(NULL, fsp2, NORMAL_CLOSE);
 
        /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
           creates the file. This isn't the correct thing to do in the copy
           case. JRA */
-       if (!parent_dirname(talloc_tos(), newname, &parent, NULL)) {
-               return NT_STATUS_NO_MEMORY;
+       if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
+                           NULL)) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
        }
-       file_set_dosmode(conn, newname, fattr, &sbuf2, parent, false);
+       file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
        TALLOC_FREE(parent);
 
-       if (ret < (SMB_OFF_T)sbuf1.st_size) {
-               return NT_STATUS_DISK_FULL;
+       if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
+               status = NT_STATUS_DISK_FULL;
+               goto out;
        }
-
+ out:
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
-                       nt_errstr(status), oldname, newname));
+                       nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
+                       smb_fname_str_dbg(smb_fname_dst)));
        }
+
        return status;
 }
 
@@ -1318,6 +1467,8 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
 void reply_ntrename(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smb_filename *smb_fname_old = NULL;
+       struct smb_filename *smb_fname_new = NULL;
        char *oldname = NULL;
        char *newname = NULL;
        const char *p;
@@ -1325,6 +1476,8 @@ void reply_ntrename(struct smb_request *req)
        bool src_has_wcard = False;
        bool dest_has_wcard = False;
        uint32 attrs;
+       uint32_t ucf_flags_src = 0;
+       uint32_t ucf_flags_dst = 0;
        uint16 rename_type;
        TALLOC_CTX *ctx = talloc_tos();
 
@@ -1332,8 +1485,7 @@ void reply_ntrename(struct smb_request *req)
 
        if (req->wct < 4) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
        attrs = SVAL(req->vwv+0, 0);
@@ -1344,14 +1496,12 @@ void reply_ntrename(struct smb_request *req)
                                       &status, &src_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
        if (ms_has_wild(oldname)) {
                reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
        p++;
@@ -1359,66 +1509,81 @@ void reply_ntrename(struct smb_request *req)
                                       &status, &dest_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
-                               oldname,
-                               &oldname);
+       /* The newname must begin with a ':' if the oldname contains a ':'. */
+       if (strchr_m(oldname, ':') && (newname[0] != ':')) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               goto out;
+       }
+
+       /*
+        * If this is a rename operation, allow wildcards and save the
+        * destination's last component.
+        */
+       if (rename_type == RENAME_FLAG_RENAME) {
+               ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
+               ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
+       }
+
+       /* rename_internals() calls unix_convert(), so don't call it here. */
+       status = filename_convert(ctx, conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 oldname,
+                                 ucf_flags_src,
+                                 NULL,
+                                 &smb_fname_old);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
-                       reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
+               if (NT_STATUS_EQUAL(status,
+                                   NT_STATUS_PATH_NOT_COVERED)) {
+                       reply_botherror(req,
+                                       NT_STATUS_PATH_NOT_COVERED,
                                        ERRSRV, ERRbadpath);
-                       END_PROFILE(SMBntrename);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
-                               newname,
-                               &newname);
+       status = filename_convert(ctx, conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 newname,
+                                 ucf_flags_dst,
+                                 &dest_has_wcard,
+                                 &smb_fname_new);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
-                       reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
+               if (NT_STATUS_EQUAL(status,
+                                   NT_STATUS_PATH_NOT_COVERED)) {
+                       reply_botherror(req,
+                                       NT_STATUS_PATH_NOT_COVERED,
                                        ERRSRV, ERRbadpath);
-                       END_PROFILE(SMBntrename);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
-       /* The new name must begin with a ':' if the old name is a stream. */
-       if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
-               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               END_PROFILE(SMBntrename);
-               return;
-       }
-
-       DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
+       DEBUG(3,("reply_ntrename: %s -> %s\n",
+                smb_fname_str_dbg(smb_fname_old),
+                smb_fname_str_dbg(smb_fname_new)));
 
        switch(rename_type) {
                case RENAME_FLAG_RENAME:
-                       status = rename_internals(ctx, conn, req, oldname,
-                                       newname, attrs, False, src_has_wcard,
-                                       dest_has_wcard, DELETE_ACCESS);
+                       status = rename_internals(ctx, conn, req,
+                                                 smb_fname_old, smb_fname_new,
+                                                 attrs, False, src_has_wcard,
+                                                 dest_has_wcard,
+                                                 DELETE_ACCESS);
                        break;
                case RENAME_FLAG_HARD_LINK:
                        if (src_has_wcard || dest_has_wcard) {
                                /* No wildcards. */
                                status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
                        } else {
-                               status = hardlink_internals(ctx,
-                                               conn,
-                                               oldname,
-                                               newname);
+                               status = hardlink_internals(ctx, conn,
+                                                           smb_fname_old,
+                                                           smb_fname_new);
                        }
                        break;
                case RENAME_FLAG_COPY:
@@ -1426,8 +1591,10 @@ void reply_ntrename(struct smb_request *req)
                                /* No wildcards. */
                                status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
                        } else {
-                               status = copy_internals(ctx, conn, req, oldname,
-                                                       newname, attrs);
+                               status = copy_internals(ctx, conn, req,
+                                                       smb_fname_old,
+                                                       smb_fname_new,
+                                                       attrs);
                        }
                        break;
                case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
@@ -1441,17 +1608,15 @@ void reply_ntrename(struct smb_request *req)
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call. */
-                       END_PROFILE(SMBntrename);
-                       return;
+                       goto out;
                }
 
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
        reply_outbuf(req, 0, 0);
-
+ out:
        END_PROFILE(SMBntrename);
        return;
 }
@@ -1461,6 +1626,13 @@ void reply_ntrename(struct smb_request *req)
  don't allow a directory to be opened.
 ****************************************************************************/
 
+static void smbd_smb1_notify_reply(struct smb_request *req,
+                                  NTSTATUS error_code,
+                                  uint8_t *buf, size_t len)
+{
+       send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
+}
+
 static void call_nt_transact_notify_change(connection_struct *conn,
                                           struct smb_request *req,
                                           uint16 **ppsetup,
@@ -1478,7 +1650,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
        bool recursive;
 
        if(setup_count < 6) {
-               reply_doserror(req, ERRDOS, ERRbadfunc);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -1489,7 +1661,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
        DEBUG(3,("call_nt_transact_notify_change\n"));
 
        if(!fsp) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
 
@@ -1503,7 +1675,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
 
                DEBUG(3,("call_nt_transact_notify_change: notify change "
                         "called on %s, filter = %s, recursive = %d\n",
-                        fsp->fsp_name, filter_string, recursive));
+                        fsp_str_dbg(fsp), filter_string, recursive));
 
                TALLOC_FREE(filter_string);
        }
@@ -1536,8 +1708,11 @@ static void call_nt_transact_notify_change(connection_struct *conn,
                 * here.
                 */
 
-               change_notify_reply(fsp->conn, req, max_param_count,
-                                   fsp->notify);
+               change_notify_reply(fsp->conn, req,
+                                   NT_STATUS_OK,
+                                   max_param_count,
+                                   fsp->notify,
+                                   smbd_smb1_notify_reply);
 
                /*
                 * change_notify_reply() above has independently sent its
@@ -1553,7 +1728,8 @@ static void call_nt_transact_notify_change(connection_struct *conn,
        status = change_notify_add_request(req,
                        max_param_count,
                        filter,
-                       recursive, fsp);
+                       recursive, fsp,
+                       smbd_smb1_notify_reply);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
        }
@@ -1579,7 +1755,7 @@ static void call_nt_transact_rename(connection_struct *conn,
        TALLOC_CTX *ctx = talloc_tos();
 
         if(parameter_count < 5) {
-               reply_doserror(req, ERRDOS, ERRbadfunc);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -1602,7 +1778,7 @@ static void call_nt_transact_rename(connection_struct *conn,
        send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
 
        DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
-                fsp->fsp_name, new_name));
+                fsp_str_dbg(fsp), new_name));
 
        return;
 }
@@ -1648,24 +1824,25 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
        DATA_BLOB blob;
 
         if(parameter_count < 8) {
-               reply_doserror(req, ERRDOS, ERRbadfunc);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
        fsp = file_fsp(req, SVAL(params,0));
        if(!fsp) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
 
        security_info_wanted = IVAL(params,4);
 
-       DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
-                       (unsigned int)security_info_wanted ));
+       DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
+                "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
+                (unsigned int)security_info_wanted));
 
        params = nttrans_realloc(ppparams, 4);
        if(params == NULL) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
@@ -1673,8 +1850,30 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
         * Get the permissions to return.
         */
 
+       if ((security_info_wanted & SECINFO_SACL) &&
+                       !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
+               reply_nterror(req,  NT_STATUS_ACCESS_DENIED);
+               return;
+       }
+
+       if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
+                       !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+               return;
+       }
+
+       if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
+                       SECINFO_GROUP|SECINFO_SACL)) {
+               /* Don't return SECINFO_LABEL if anything else was
+                  requested. See bug #8458. */
+               security_info_wanted &= ~SECINFO_LABEL;
+       }
+
        if (!lp_nt_acl_support(SNUM(conn))) {
                status = get_null_nt_acl(talloc_tos(), &psd);
+       } else if (security_info_wanted & SECINFO_LABEL) {
+               /* Like W2K3 return a null object. */
+               status = get_null_nt_acl(talloc_tos(), &psd);
        } else {
                status = SMB_VFS_FGET_NT_ACL(
                        fsp, security_info_wanted, &psd);
@@ -1684,6 +1883,21 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
                return;
        }
 
+       if (!(security_info_wanted & SECINFO_OWNER)) {
+               psd->owner_sid = NULL;
+       }
+       if (!(security_info_wanted & SECINFO_GROUP)) {
+               psd->group_sid = NULL;
+       }
+       if (!(security_info_wanted & SECINFO_DACL)) {
+               psd->type &= ~SEC_DESC_DACL_PRESENT;
+               psd->dacl = NULL;
+       }
+       if (!(security_info_wanted & SECINFO_SACL)) {
+               psd->type &= ~SEC_DESC_SACL_PRESENT;
+               psd->sacl = NULL;
+       }
+
        /* If the SACL/DACL is NULL, but was requested, we mark that it is
         * present in the reply to match Windows behavior */
        if (psd->sacl == NULL &&
@@ -1693,12 +1907,22 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
            security_info_wanted & DACL_SECURITY_INFORMATION)
                psd->type |= SEC_DESC_DACL_PRESENT;
 
+       if (security_info_wanted & SECINFO_LABEL) {
+               /* Like W2K3 return a null object. */
+               psd->owner_sid = NULL;
+               psd->group_sid = NULL;
+               psd->dacl = NULL;
+               psd->sacl = NULL;
+               psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
+       }
+
        sd_size = ndr_size_security_descriptor(psd, NULL, 0);
 
        DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
 
        if (DEBUGLEVEL >= 10) {
-               DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
+               DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
+                         fsp_str_dbg(fsp)));
                NDR_PRINT_DEBUG(security_descriptor, psd);
        }
 
@@ -1716,7 +1940,7 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
 
        data = nttrans_realloc(ppdata, sd_size);
        if(data == NULL) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
@@ -1757,12 +1981,12 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
        NTSTATUS status;
 
        if(parameter_count < 8) {
-               reply_doserror(req, ERRDOS, ERRbadfunc);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
        if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
 
@@ -1772,11 +1996,11 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
 
        security_info_sent = IVAL(params,4);
 
-       DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
-               (unsigned int)security_info_sent ));
+       DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
+                fsp_str_dbg(fsp), (unsigned int)security_info_sent));
 
        if (data_count == 0) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -1992,12 +2216,12 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                }
 
                /* needed_data_count 4 bytes */
-               SIVAL(pdata,8,labels_data_count);
+               SIVAL(pdata, 8, labels_data_count+4);
 
                cur_pdata+=12;
 
                DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
-                       shadow_data->num_volumes,fsp->fsp_name));
+                         shadow_data->num_volumes, fsp_str_dbg(fsp)));
                if (labels && shadow_data->labels) {
                        for (i=0;i<shadow_data->num_volumes;i++) {
                                srvstr_push(pdata, req->flags2,
@@ -2038,7 +2262,11 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                /* unknown 4 bytes: this is not the length of the sid :-(  */
                /*unknown = IVAL(pdata,0);*/
 
-               sid_parse(pdata+4,sid_len,&sid);
+               if (!sid_parse(pdata+4,sid_len,&sid)) {
+                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+                       return;
+               }
+
                DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
 
                if (!sid_to_uid(&sid, &uid)) {
@@ -2116,11 +2344,11 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
        ZERO_STRUCT(qt);
 
        /* access check */
-       if (conn->server_info->utok.uid != 0) {
+       if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
                DEBUG(1,("get_user_quota: access_denied service [%s] user "
                         "[%s]\n", lp_servicename(SNUM(conn)),
                         conn->server_info->unix_name));
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -2130,7 +2358,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
 
        if (parameter_count < 4) {
                DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
-               reply_doserror(req, ERRDOS, ERRinvalidparam);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -2165,7 +2393,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                                param_len = 4;
                                params = nttrans_realloc(ppparams, param_len);
                                if(params == NULL) {
-                                       reply_doserror(req, ERRDOS, ERRnomem);
+                                       reply_nterror(req, NT_STATUS_NO_MEMORY);
                                        return;
                                }
 
@@ -2185,7 +2413,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        }
 
                        if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
-                               reply_doserror(req, ERRSRV, ERRerror);
+                               reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
                                return;
                        }
 
@@ -2193,7 +2421,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        param_len = 4;
                        params = nttrans_realloc(ppparams, param_len);
                        if(params == NULL) {
-                               reply_doserror(req, ERRDOS, ERRnomem);
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
                                return;
                        }
 
@@ -2202,7 +2430,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
 
                        pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
                        if(pdata == NULL) {
-                               reply_doserror(req, ERRDOS, ERRnomem);
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
                                return;
                        }
 
@@ -2265,20 +2493,20 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
 
                        if (data_count < 8) {
                                DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
-                               reply_doserror(req, ERRDOS, ERRunknownlevel);
+                               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
                        }
 
                        sid_len = IVAL(pdata,4);
                        /* Ensure this is less than 1mb. */
                        if (sid_len > (1024*1024)) {
-                               reply_doserror(req, ERRDOS, ERRnomem);
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
                                return;
                        }
 
                        if (data_count < 8+sid_len) {
                                DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
-                               reply_doserror(req, ERRDOS, ERRunknownlevel);
+                               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
                        }
 
@@ -2294,7 +2522,10 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                                break;
                        }
 
-                       sid_parse(pdata+8,sid_len,&sid);
+                       if (!sid_parse(pdata+8,sid_len,&sid)) {
+                               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+                               return;
+                       }
 
                        if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
                                ZERO_STRUCT(qt);
@@ -2309,13 +2540,13 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        param_len = 4;
                        params = nttrans_realloc(ppparams, param_len);
                        if(params == NULL) {
-                               reply_doserror(req, ERRDOS, ERRnomem);
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
                                return;
                        }
 
                        pdata = nttrans_realloc(ppdata, data_len);
                        if(pdata == NULL) {
-                               reply_doserror(req, ERRDOS, ERRnomem);
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
                                return;
                        }
 
@@ -2349,7 +2580,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
 
                default:
                        DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
-                       reply_doserror(req, ERRSRV, ERRerror);
+                       reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                        return;
                        break;
        }
@@ -2383,11 +2614,11 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        ZERO_STRUCT(qt);
 
        /* access check */
-       if (conn->server_info->utok.uid != 0) {
+       if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
                DEBUG(1,("set_user_quota: access_denied service [%s] user "
                         "[%s]\n", lp_servicename(SNUM(conn)),
                         conn->server_info->unix_name));
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -2397,7 +2628,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
 
        if (parameter_count < 2) {
                DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
-               reply_doserror(req, ERRDOS, ERRinvalidparam);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -2411,7 +2642,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
 
        if (data_count < 40) {
                DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
+               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                return;
        }
 
@@ -2425,7 +2656,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
 
        if (data_count < 40+sid_len) {
                DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
+               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                return;
        }
 
@@ -2442,7 +2673,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
                ((qt.usedspace != 0xFFFFFFFF)||
                (IVAL(pdata,20)!=0xFFFFFFFF))) {
                /* more than 32 bits? */
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
+               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                return;
        }
 #endif /* LARGE_SMB_OFF_T */
@@ -2456,7 +2687,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
                ((qt.softlim != 0xFFFFFFFF)||
                (IVAL(pdata,28)!=0xFFFFFFFF))) {
                /* more than 32 bits? */
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
+               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                return;
        }
 #endif /* LARGE_SMB_OFF_T */
@@ -2470,18 +2701,22 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
                ((qt.hardlim != 0xFFFFFFFF)||
                (IVAL(pdata,36)!=0xFFFFFFFF))) {
                /* more than 32 bits? */
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
+               reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                return;
        }
 #endif /* LARGE_SMB_OFF_T */
 
-       sid_parse(pdata+40,sid_len,&sid);
+       if (!sid_parse(pdata+40,sid_len,&sid)) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               return;
+       }
+
        DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
 
        /* 44 unknown bytes left... */
 
        if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
-               reply_doserror(req, ERRSRV, ERRerror);
+               reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
                return;
        }
 
@@ -2494,7 +2729,7 @@ static void handle_nttrans(connection_struct *conn,
                           struct trans_state *state,
                           struct smb_request *req)
 {
-       if (Protocol >= PROTOCOL_NT1) {
+       if (get_Protocol() >= PROTOCOL_NT1) {
                req->flags2 |= 0x40; /* IS_LONG_NAME */
                SSVAL(req->inbuf,smb_flg2,req->flags2);
        }
@@ -2615,7 +2850,7 @@ static void handle_nttrans(connection_struct *conn,
                        /* Error in request */
                        DEBUG(0,("handle_nttrans: Unknown request %d in "
                                 "nttrans call\n", state->call));
-                       reply_doserror(req, ERRSRV, ERRerror);
+                       reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                        return;
        }
        return;
@@ -2651,7 +2886,7 @@ void reply_nttrans(struct smb_request *req)
        function_code = SVAL(req->vwv+18, 0);
 
        if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBnttrans);
                return;
        }
@@ -2665,7 +2900,7 @@ void reply_nttrans(struct smb_request *req)
        }
 
        if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                END_PROFILE(SMBnttrans);
                return;
        }
@@ -2711,7 +2946,7 @@ void reply_nttrans(struct smb_request *req)
        /* Don't allow more than 128mb for each value. */
        if ((state->total_data > (1024*1024*128)) ||
            (state->total_param > (1024*1024*128))) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                END_PROFILE(SMBnttrans);
                return;
        }
@@ -2732,7 +2967,7 @@ void reply_nttrans(struct smb_request *req)
                        DEBUG(0,("reply_nttrans: data malloc fail for %u "
                                 "bytes !\n", (unsigned int)state->total_data));
                        TALLOC_FREE(state);
-                       reply_doserror(req, ERRDOS, ERRnomem);
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
                        END_PROFILE(SMBnttrans);
                        return;
                }
@@ -2754,7 +2989,7 @@ void reply_nttrans(struct smb_request *req)
                                 "bytes !\n", (unsigned int)state->total_param));
                        SAFE_FREE(state->data);
                        TALLOC_FREE(state);
-                       reply_doserror(req, ERRDOS, ERRnomem);
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
                        END_PROFILE(SMBnttrans);
                        return;
                }
@@ -2787,7 +3022,7 @@ void reply_nttrans(struct smb_request *req)
                        SAFE_FREE(state->data);
                        SAFE_FREE(state->param);
                        TALLOC_FREE(state);
-                       reply_doserror(req, ERRDOS, ERRnomem);
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
                        END_PROFILE(SMBnttrans);
                        return;
                }
@@ -2840,6 +3075,12 @@ void reply_nttranss(struct smb_request *req)
 
        show_msg((char *)req->inbuf);
 
+       /* Windows clients expect all replies to
+          an NT transact secondary (SMBnttranss 0xA1)
+          to have a command code of NT transact
+          (SMBnttrans 0xA0). See bug #8989 for details. */
+       req->cmd = SMBnttrans;
+
        if (req->wct < 18) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                END_PROFILE(SMBnttranss);