Fix bug #10010 - Missing integer wrap protection in EA list reading can cause server...
[samba.git] / source3 / smbd / nttrans.c
index 5c6d78089d71077f2cea4d44c32130f7500e3138..f70fb36964e0d971f9a45ce635a380e1ba115c18 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
+#include "fake_file.h"
+#include "../libcli/security/security.h"
+#include "../librpc/gen_ndr/ndr_security.h"
+#include "passdb/lookup_sid.h"
+#include "auth.h"
+#include "smbprofile.h"
+#include "libsmb/libsmb.h"
 
 extern const struct generic_mapping file_generic_mapping;
 
@@ -44,10 +53,10 @@ static char *nttrans_realloc(char **ptr, size_t size)
  HACK ! Always assumes smb_setup field is zero.
 ****************************************************************************/
 
-void send_nt_replies(connection_struct *conn,
-                       struct smb_request *req, NTSTATUS nt_error,
-                    char *params, int paramsize,
-                    char *pdata, int datasize)
+static void send_nt_replies(connection_struct *conn,
+                           struct smb_request *req, NTSTATUS nt_error,
+                           char *params, int paramsize,
+                           char *pdata, int datasize)
 {
        int data_to_send = datasize;
        int params_to_send = paramsize;
@@ -55,9 +64,9 @@ void send_nt_replies(connection_struct *conn,
        char *pp = params;
        char *pd = pdata;
        int params_sent_thistime, data_sent_thistime, total_sent_thistime;
-       int alignment_offset = 3;
+       int alignment_offset = 1;
        int data_alignment_offset = 0;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
        int max_send = sconn->smb1.sessions.max_send;
 
        /*
@@ -73,7 +82,7 @@ void send_nt_replies(connection_struct *conn,
                                         __LINE__,__FILE__);
                }
                show_msg((char *)req->outbuf);
-               if (!srv_send_smb(smbd_server_fd(),
+               if (!srv_send_smb(sconn,
                                (char *)req->outbuf,
                                true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn),
@@ -133,11 +142,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.
                 */
@@ -242,7 +246,7 @@ void send_nt_replies(connection_struct *conn,
 
                /* Send the packet */
                show_msg((char *)req->outbuf);
-               if (!srv_send_smb(smbd_server_fd(),
+               if (!srv_send_smb(sconn,
                                (char *)req->outbuf,
                                true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn),
@@ -275,15 +279,17 @@ void send_nt_replies(connection_struct *conn,
 ****************************************************************************/
 
 static void nt_open_pipe(char *fname, connection_struct *conn,
-                        struct smb_request *req, int *ppnum)
+                        struct smb_request *req, uint16_t *ppnum)
 {
        files_struct *fsp;
        NTSTATUS status;
 
        DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
 
-       /* Strip \\ off the name. */
-       fname++;
+       /* Strip \\ off the name if present. */
+       while (fname[0] == '\\') {
+               fname++;
+       }
 
        status = open_np_file(req, fname, &fsp);
        if (!NT_STATUS_IS_OK(status)) {
@@ -308,7 +314,7 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
                                  struct smb_request *req)
 {
        char *fname = NULL;
-       int pnum = -1;
+       uint16_t pnum = FNUM_FIELD_INVALID;
        char *p = NULL;
        uint32 flags = IVAL(req->vwv+3, 1);
        TALLOC_CTX *ctx = talloc_tos();
@@ -334,7 +340,7 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                /* This is very strange. We
                 * return 50 words, but only set
-                * the wcnt to 42 ? It's definately
+                * the wcnt to 42 ? It's definitely
                 * what happens on the wire....
                 */
                reply_outbuf(req, 50, 0);
@@ -343,6 +349,9 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
                reply_outbuf(req, 34, 0);
        }
 
+       SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
+       SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
+
        p = (char *)req->outbuf + smb_vwv2;
        p++;
        SSVAL(p,0,pnum);
@@ -370,8 +379,53 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
        }
 
        DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
+}
 
-       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;
 }
 
 /****************************************************************************
@@ -394,7 +448,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        /* Breakout the oplock request bits so we can set the
           reply bits separately. */
        uint32 fattr=0;
-       SMB_OFF_T file_len = 0;
+       off_t file_len = 0;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
@@ -406,32 +460,25 @@ void reply_ntcreate_and_X(struct smb_request *req)
        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);
 
        if (req->wct < 24) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
+               goto out;
        }
 
        flags = IVAL(req->vwv+3, 1);
        access_mask = IVAL(req->vwv+7, 1);
        file_attributes = IVAL(req->vwv+13, 1);
-       /*
-        * Bug #6898 - clients using Windows opens should
-        * never be able to set this attribute.
-        */
-       file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
        share_access = IVAL(req->vwv+15, 1);
        create_disposition = IVAL(req->vwv+17, 1);
        create_options = IVAL(req->vwv+19, 1);
        root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
 
-       allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
-#ifdef LARGE_SMB_OFF_T
-       allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
-#endif
+       allocation_size = BVAL(req->vwv+9, 1);
 
        srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
                            STR_TERMINATE, &status);
@@ -469,7 +516,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
                        do_ntcreate_pipe_open(conn, req);
                        goto out;
                }
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -479,6 +526,14 @@ 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,
@@ -487,6 +542,8 @@ void reply_ntcreate_and_X(struct smb_request *req)
                                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,
@@ -498,6 +555,13 @@ void reply_ntcreate_and_X(struct smb_request *req)
                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 */
@@ -510,22 +574,18 @@ void reply_ntcreate_and_X(struct smb_request *req)
                file_attributes,                        /* file_attributes */
                oplock_request,                         /* oplock_request */
                allocation_size,                        /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
                &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call, no error. */
                        goto out;
                }
-               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
-                       reply_botherror(req, status, ERRDOS, ERRfilexists);
-               }
-               else {
-                       reply_nterror(req, status);
-               }
+               reply_openerror(req, status);
                goto out;
        }
 
@@ -563,7 +623,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                /* This is very strange. We
                 * return 50 words, but only set
-                * the wcnt to 42 ? It's definately
+                * the wcnt to 42 ? It's definitely
                 * what happens on the wire....
                 */
                reply_outbuf(req, 50, 0);
@@ -572,6 +632,9 @@ void reply_ntcreate_and_X(struct smb_request *req)
                reply_outbuf(req, 34, 0);
        }
 
+       SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
+       SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
+
        p = (char *)req->outbuf + smb_vwv2;
 
        SCVAL(p, 0, oplock_granted);
@@ -595,7 +658,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        /* 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);
+       get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
        if (!null_timespec(write_time_ts)) {
                update_stat_ex_mtime(&smb_fname->st, write_time_ts);
        }
@@ -630,7 +693,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
                size_t num_names = 0;
-               unsigned int num_streams;
+               unsigned int num_streams = 0;
                struct stream_struct *streams = NULL;
 
                /* Do we have any EA's ? */
@@ -639,7 +702,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
                if (NT_STATUS_IS_OK(status) && num_names) {
                        file_status &= ~NO_EAS;
                }
-               status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
+               status = 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) {
@@ -663,10 +726,9 @@ void reply_ntcreate_and_X(struct smb_request *req)
                SIVAL(p,0,perms);
        }
 
-       DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
-               fsp->fnum, smb_fname_str_dbg(smb_fname)));
+       DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
+               fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
 
-       chain_reply(req);
  out:
        END_PROFILE(SMBntcreateX);
        return;
@@ -684,7 +746,7 @@ static void do_nt_transact_create_pipe(connection_struct *conn,
 {
        char *fname = NULL;
        char *params = *ppparams;
-       int pnum = -1;
+       uint16_t pnum = FNUM_FIELD_INVALID;
        char *p = NULL;
        NTSTATUS status;
        size_t param_len;
@@ -697,7 +759,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;
        }
 
@@ -727,7 +789,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;
        }
 
@@ -768,36 +830,108 @@ static void do_nt_transact_create_pipe(connection_struct *conn,
        return;
 }
 
+/*********************************************************************
+ Windows seems to do canonicalization of inheritance bits. Do the
+ same.
+*********************************************************************/
+
+static void canonicalize_inheritance_bits(struct security_descriptor *psd)
+{
+       bool set_auto_inherited = false;
+
+       /*
+        * We need to filter out the
+        * SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ
+        * bits. If both are set we store SEC_DESC_DACL_AUTO_INHERITED
+        * as this alters whether SEC_ACE_FLAG_INHERITED_ACE is set
+        * when an ACE is inherited. Otherwise we zero these bits out.
+        * See:
+        *
+        * http://social.msdn.microsoft.com/Forums/eu/os_fileservices/thread/11f77b68-731e-407d-b1b3-064750716531
+        *
+        * for details.
+        */
+
+       if ((psd->type & (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ))
+                       == (SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ)) {
+               set_auto_inherited = true;
+       }
+
+       psd->type &= ~(SEC_DESC_DACL_AUTO_INHERITED|SEC_DESC_DACL_AUTO_INHERIT_REQ);
+       if (set_auto_inherited) {
+               psd->type |= SEC_DESC_DACL_AUTO_INHERITED;
+       }
+}
+
 /****************************************************************************
  Internal fn to set security descriptors.
 ****************************************************************************/
 
-static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
-                      uint32 security_info_sent)
+NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
+                      uint32_t security_info_sent)
 {
-       SEC_DESC *psd = NULL;
        NTSTATUS status;
 
-       if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
-               return NT_STATUS_OK;
+       if (!CAN_WRITE(fsp->conn)) {
+               return NT_STATUS_ACCESS_DENIED;
        }
 
-       status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (!lp_nt_acl_support(SNUM(fsp->conn))) {
+               return NT_STATUS_OK;
        }
 
        if (psd->owner_sid == NULL) {
-               security_info_sent &= ~OWNER_SECURITY_INFORMATION;
+               security_info_sent &= ~SECINFO_OWNER;
        }
        if (psd->group_sid == NULL) {
-               security_info_sent &= ~GROUP_SECURITY_INFORMATION;
+               security_info_sent &= ~SECINFO_GROUP;
        }
 
-       /* 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);
+               }
+       }
+
+       canonicalize_inheritance_bits(psd);
 
        if (DEBUGLEVEL >= 10) {
                DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
@@ -811,6 +945,29 @@ static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
        return status;
 }
 
+/****************************************************************************
+ Internal fn to set security descriptors from a data blob.
+****************************************************************************/
+
+NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
+                      uint32_t security_info_sent)
+{
+       struct security_descriptor *psd = NULL;
+       NTSTATUS status;
+
+       if (sd_len == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return set_sd(fsp, psd, security_info_sent);
+}
+
 /****************************************************************************
  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
 ****************************************************************************/
@@ -836,7 +993,19 @@ struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t
                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;
@@ -859,7 +1028,7 @@ static void call_nt_transact_create(connection_struct *conn,
        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;
+       off_t file_len = 0;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
@@ -884,6 +1053,7 @@ static void call_nt_transact_create(connection_struct *conn,
        uint64_t allocation_size;
        int oplock_request;
        uint8_t oplock_granted;
+       struct case_semantics_state *case_state = NULL;
        TALLOC_CTX *ctx = talloc_tos();
 
        DEBUG(5,("call_nt_transact_create\n"));
@@ -901,7 +1071,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                ppdata, data_count);
                        goto out;
                }
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -918,21 +1088,13 @@ static void call_nt_transact_create(connection_struct *conn,
        flags = IVAL(params,0);
        access_mask = IVAL(params,8);
        file_attributes = IVAL(params,20);
-       /*
-        * Bug #6898 - clients using Windows opens should
-        * never be able to set this attribute.
-        */
-       file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
        share_access = IVAL(params,24);
        create_disposition = IVAL(params,28);
        create_options = IVAL(params,32);
        sd_len = IVAL(params,36);
        ea_len = IVAL(params,40);
        root_dir_fid = (uint16)IVAL(params,4);
-       allocation_size = (uint64_t)IVAL(params,12);
-#ifdef LARGE_SMB_OFF_T
-       allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
-#endif
+       allocation_size = BVAL(params,12);
 
        /*
         * we need to remove ignored bits when they come directly from the client
@@ -1000,6 +1162,14 @@ static void call_nt_transact_create(connection_struct *conn,
                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,
@@ -1008,6 +1178,8 @@ static void call_nt_transact_create(connection_struct *conn,
                                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,
@@ -1025,6 +1197,13 @@ 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 */
@@ -1037,13 +1216,14 @@ static void call_nt_transact_create(connection_struct *conn,
                file_attributes,                        /* file_attributes */
                oplock_request,                         /* oplock_request */
                allocation_size,                        /* allocation_size */
+               0,                                      /* private_flags */
                sd,                                     /* sd */
                ea_list,                                /* ea_list */
                &fsp,                                   /* result */
                &info);                                 /* pinfo */
 
        if(!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call, no error. */
                        return;
                }
@@ -1091,7 +1271,7 @@ static void call_nt_transact_create(connection_struct *conn,
        }
        params = nttrans_realloc(ppparams, param_len);
        if(params == NULL) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                goto out;
        }
 
@@ -1117,7 +1297,7 @@ static void call_nt_transact_create(connection_struct *conn,
        /* 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);
+       get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
        if (!null_timespec(write_time_ts)) {
                update_stat_ex_mtime(&smb_fname->st, write_time_ts);
        }
@@ -1150,7 +1330,25 @@ static void call_nt_transact_create(connection_struct *conn,
        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 = 0;
+               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 = 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);
@@ -1188,11 +1386,12 @@ void reply_ntcancel(struct smb_request *req)
         */
 
        START_PROFILE(SMBntcancel);
-       srv_cancel_sign_response(smbd_server_conn);
-       remove_pending_change_notify_requests_by_mid(req->mid);
-       remove_pending_lock_requests_by_mid(req->mid);
+       srv_cancel_sign_response(req->sconn);
+       remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
+       remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
 
-       DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
+       DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
+               (unsigned long long)req->mid));
 
        END_PROFILE(SMBntcancel);
        return;
@@ -1212,7 +1411,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        files_struct *fsp1,*fsp2;
        uint32 fattr;
        int info;
-       SMB_OFF_T ret=-1;
+       off_t ret=-1;
        NTSTATUS status = NT_STATUS_OK;
        char *parent;
 
@@ -1229,7 +1428,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
 
        /* Ensure attributes match. */
        fattr = dos_mode(conn, smb_fname_src);
-       if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
+       if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
                status = NT_STATUS_NO_SUCH_FILE;
                goto out;
        }
@@ -1255,7 +1454,8 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                req,                                    /* req */
                0,                                      /* root_dir_fid */
                smb_fname_src,                          /* fname */
-               FILE_READ_DATA,                         /* access_mask */
+               FILE_READ_DATA|FILE_READ_ATTRIBUTES|
+                       FILE_READ_EA,                   /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
                FILE_OPEN,                              /* create_disposition*/
@@ -1263,6 +1463,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
                NO_OPLOCK,                              /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp1,                                  /* result */
@@ -1277,7 +1478,8 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                req,                                    /* req */
                0,                                      /* root_dir_fid */
                smb_fname_dst,                          /* fname */
-               FILE_WRITE_DATA,                        /* access_mask */
+               FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
+                       FILE_WRITE_EA,                  /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
                FILE_CREATE,                            /* create_disposition*/
@@ -1285,6 +1487,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                fattr,                                  /* file_attributes */
                NO_OPLOCK,                              /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp2,                                  /* result */
@@ -1312,7 +1515,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
 
        status = close_file(NULL, fsp2, NORMAL_CLOSE);
 
-       /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
+       /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
           creates the file. This isn't the correct thing to do in the copy
           case. JRA */
        if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
@@ -1323,7 +1526,7 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
        TALLOC_FREE(parent);
 
-       if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
+       if (ret < (off_t)smb_fname_src->st.st_ex_size) {
                status = NT_STATUS_DISK_FULL;
                goto out;
        }
@@ -1357,6 +1560,7 @@ void reply_ntrename(struct smb_request *req)
        uint32_t ucf_flags_dst = 0;
        uint16 rename_type;
        TALLOC_CTX *ctx = talloc_tos();
+       bool stream_rename = false;
 
        START_PROFILE(SMBntrename);
 
@@ -1389,10 +1593,16 @@ void reply_ntrename(struct smb_request *req)
                goto out;
        }
 
-       /* 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 (!lp_posix_pathnames()) {
+               /* The newname must begin with a ':' if the
+                  oldname contains a ':'. */
+               if (strchr_m(oldname, ':')) {
+                       if (newname[0] != ':') {
+                               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+                               goto out;
+                       }
+                       stream_rename = true;
+               }
        }
 
        /*
@@ -1441,6 +1651,17 @@ void reply_ntrename(struct smb_request *req)
                goto out;
        }
 
+       if (stream_rename) {
+               /* smb_fname_new must be the same as smb_fname_old. */
+               TALLOC_FREE(smb_fname_new->base_name);
+               smb_fname_new->base_name = talloc_strdup(smb_fname_new,
+                                               smb_fname_old->base_name);
+               if (!smb_fname_new->base_name) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       goto out;
+               }
+       }
+
        DEBUG(3,("reply_ntrename: %s -> %s\n",
                 smb_fname_str_dbg(smb_fname_old),
                 smb_fname_str_dbg(smb_fname_new)));
@@ -1459,6 +1680,8 @@ void reply_ntrename(struct smb_request *req)
                                status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
                        } else {
                                status = hardlink_internals(ctx, conn,
+                                                           req,
+                                                           false,
                                                            smb_fname_old,
                                                            smb_fname_new);
                        }
@@ -1483,7 +1706,7 @@ void reply_ntrename(struct smb_request *req)
        }
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -1527,7 +1750,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;
        }
 
@@ -1538,7 +1761,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;
        }
 
@@ -1574,7 +1797,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
                }
        }
 
-       if (fsp->notify->num_changes != 0) {
+       if (change_notify_fsp_has_changes(fsp)) {
 
                /*
                 * We've got changes pending, respond immediately
@@ -1585,7 +1808,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
                 * here.
                 */
 
-               change_notify_reply(fsp->conn, req,
+               change_notify_reply(req,
                                    NT_STATUS_OK,
                                    max_param_count,
                                    fsp->notify,
@@ -1632,7 +1855,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;
        }
 
@@ -1664,7 +1887,7 @@ static void call_nt_transact_rename(connection_struct *conn,
  Fake up a completely empty SD.
 *******************************************************************************/
 
-static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
+static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
 {
        size_t sd_size;
 
@@ -1679,6 +1902,125 @@ static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
 
 /****************************************************************************
  Reply to query a security descriptor.
+ Callable from SMB2 and SMB2.
+ If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
+ the required size.
+****************************************************************************/
+
+NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
+                                       TALLOC_CTX *mem_ctx,
+                                       files_struct *fsp,
+                                       uint32_t security_info_wanted,
+                                       uint32_t max_data_count,
+                                       uint8_t **ppmarshalled_sd,
+                                       size_t *psd_size)
+{
+       NTSTATUS status;
+       struct security_descriptor *psd = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       /*
+        * Get the permissions to return.
+        */
+
+       if ((security_info_wanted & SECINFO_SACL) &&
+                       !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
+               DEBUG(10, ("Access to SACL denied.\n"));
+               TALLOC_FREE(frame);
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
+                       !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
+               DEBUG(10, ("Access to DACL, OWNER, or GROUP denied.\n"));
+               TALLOC_FREE(frame);
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       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(frame, &psd);
+       } else if (security_info_wanted & SECINFO_LABEL) {
+               /* Like W2K3 return a null object. */
+               status = get_null_nt_acl(frame, &psd);
+       } else {
+               status = SMB_VFS_FGET_NT_ACL(
+                       fsp, security_info_wanted, frame, &psd);
+       }
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               return status;
+       }
+
+       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 &&
+           security_info_wanted & SECINFO_SACL)
+               psd->type |= SEC_DESC_SACL_PRESENT;
+       if (psd->dacl == NULL &&
+           security_info_wanted & SECINFO_DACL)
+               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);
+       }
+
+       *psd_size = ndr_size_security_descriptor(psd, 0);
+
+       DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
+               (unsigned long)*psd_size));
+
+       if (DEBUGLEVEL >= 10) {
+               DEBUG(10,("smbd_do_query_security_desc for file %s\n",
+                         fsp_str_dbg(fsp)));
+               NDR_PRINT_DEBUG(security_descriptor, psd);
+       }
+
+       if (max_data_count < *psd_size) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_BUFFER_TOO_SMALL;
+       }
+
+       status = marshall_sec_desc(mem_ctx, psd,
+                                  ppmarshalled_sd, psd_size);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               return status;
+       }
+
+       TALLOC_FREE(frame);
+       return NT_STATUS_OK;
+}
+
+/****************************************************************************
+ SMB1 reply to query a security descriptor.
 ****************************************************************************/
 
 static void call_nt_transact_query_security_desc(connection_struct *conn,
@@ -1693,21 +2035,20 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
 {
        char *params = *ppparams;
        char *data = *ppdata;
-       SEC_DESC *psd = NULL;
-       size_t sd_size;
+       size_t sd_size = 0;
        uint32 security_info_wanted;
        files_struct *fsp = NULL;
        NTSTATUS status;
-       DATA_BLOB blob;
+       uint8_t *marshalled_sd = NULL;
 
         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;
        }
 
@@ -1719,7 +2060,7 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
 
        params = nttrans_realloc(ppparams, 4);
        if(params == NULL) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
@@ -1727,64 +2068,47 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
         * Get the permissions to return.
         */
 
-       if (!lp_nt_acl_support(SNUM(conn))) {
-               status = get_null_nt_acl(talloc_tos(), &psd);
-       } else {
-               status = SMB_VFS_FGET_NT_ACL(
-                       fsp, security_info_wanted, &psd);
-       }
+       status = smbd_do_query_security_desc(conn,
+                                       talloc_tos(),
+                                       fsp,
+                                       security_info_wanted,
+                                       max_data_count,
+                                       &marshalled_sd,
+                                       &sd_size);
+
+       if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
+               SIVAL(params,0,(uint32_t)sd_size);
+               send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
+                       params, 4, NULL, 0);
+               return;
+        }
+
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
        }
 
-       /* 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 &&
-           security_info_wanted & SACL_SECURITY_INFORMATION)
-               psd->type |= SEC_DESC_SACL_PRESENT;
-       if (psd->dacl == NULL &&
-           security_info_wanted & DACL_SECURITY_INFORMATION)
-               psd->type |= SEC_DESC_DACL_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));
+       SMB_ASSERT(sd_size > 0);
 
-       if (DEBUGLEVEL >= 10) {
-               DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
-                         fsp_str_dbg(fsp)));
-               NDR_PRINT_DEBUG(security_descriptor, psd);
-       }
-
-       SIVAL(params,0,(uint32)sd_size);
+       SIVAL(params,0,(uint32_t)sd_size);
 
        if (max_data_count < sd_size) {
                send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
-                               params, 4, *ppdata, 0);
+                               params, 4, NULL, 0);
                return;
        }
 
        /*
-        * Allocate the data we will point this at.
+        * Allocate the data we will return.
         */
 
        data = nttrans_realloc(ppdata, sd_size);
        if(data == NULL) {
-               reply_doserror(req, ERRDOS, ERRnomem);
-               return;
-       }
-
-       status = marshall_sec_desc(talloc_tos(), psd,
-                                  &blob.data, &blob.length);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
-       SMB_ASSERT(sd_size == blob.length);
-       memcpy(data, blob.data, sd_size);
+       memcpy(data, marshalled_sd, sd_size);
 
        send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
 
@@ -1812,12 +2136,17 @@ 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;
+       }
+
+       if (!CAN_WRITE(fsp->conn)) {
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -1831,11 +2160,11 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
                 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;
        }
 
-       status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
+       status = set_sd_blob(fsp, (uint8 *)data, data_count, security_info_sent);
 
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
@@ -1858,12 +2187,16 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                                   char **ppdata, uint32 data_count,
                                   uint32 max_data_count)
 {
+       NTSTATUS status;
        uint32 function;
        uint16 fidnum;
        files_struct *fsp;
        uint8 isFSctl;
        uint8 compfilter;
+       char *out_data = NULL;
+       uint32 out_data_len = 0;
        char *pdata = *ppdata;
+       TALLOC_CTX *ctx = talloc_tos();
 
        if (setup_count != 8) {
                DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
@@ -1876,264 +2209,46 @@ static void call_nt_transact_ioctl(connection_struct *conn,
        isFSctl = CVAL(*ppsetup, 6);
        compfilter = CVAL(*ppsetup, 7);
 
-       DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
+       DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
                 function, fidnum, isFSctl, compfilter));
 
        fsp=file_fsp(req, fidnum);
-       /* this check is done in each implemented function case for now
-          because I don't want to break anything... --metze
-       FSP_BELONGS_CONN(fsp,conn);*/
-
-       SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
 
-       switch (function) {
-       case FSCTL_SET_SPARSE:
-               /* pretend this succeeded - tho strictly we should
-                  mark the file sparse (if the local fs supports it)
-                  so we can know if we need to pre-allocate or not */
-
-               DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
-               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
-               return;
-
-       case FSCTL_CREATE_OR_GET_OBJECT_ID:
-       {
-               unsigned char objid[16];
-
-               /* This should return the object-id on this file.
-                * I think I'll make this be the inode+dev. JRA.
-                */
-
-               DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
-
-               if (!fsp_belongs_conn(conn, req, fsp)) {
-                       return;
-               }
-
-               data_count = 64;
-               pdata = nttrans_realloc(ppdata, data_count);
-               if (pdata == NULL) {
-                       reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
-
-               /* For backwards compatibility only store the dev/inode. */
-               push_file_id_16(pdata, &fsp->file_id);
-               memcpy(pdata+16,create_volume_objectid(conn,objid),16);
-               push_file_id_16(pdata+32, &fsp->file_id);
-               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
-                               pdata, data_count);
+       /*
+        * We don't really implement IOCTLs, especially on files.
+        */
+       if (!isFSctl) {
+               DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
+                       isFSctl));
+               reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
                return;
        }
 
-       case FSCTL_GET_REPARSE_POINT:
-               /* pretend this fail - my winXP does it like this
-                * --metze
-                */
-
-               DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
-               reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
-               return;
-
-       case FSCTL_SET_REPARSE_POINT:
-               /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
-                * --metze
-                */
-
-               DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
-               reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
+       /* Has to be for an open file! */
+       if (!check_fsp_open(conn, req, fsp)) {
                return;
+       }
 
-       case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
-       {
-               /*
-                * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
-                * and return their volume names.  If max_data_count is 16, then it is just
-                * asking for the number of volumes and length of the combined names.
-                *
-                * pdata is the data allocated by our caller, but that uses
-                * total_data_count (which is 0 in our case) rather than max_data_count.
-                * Allocate the correct amount and return the pointer to let
-                * it be deallocated when we return.
-                */
-               SHADOW_COPY_DATA *shadow_data = NULL;
-               TALLOC_CTX *shadow_mem_ctx = NULL;
-               bool labels = False;
-               uint32 labels_data_count = 0;
-               uint32 i;
-               char *cur_pdata;
-
-               if (!fsp_belongs_conn(conn, req, fsp)) {
-                       return;
-               }
-
-               if (max_data_count < 16) {
-                       DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
-                               max_data_count));
-                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
-               }
-
-               if (max_data_count > 16) {
-                       labels = True;
-               }
-
-               shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
-               if (shadow_mem_ctx == NULL) {
-                       DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
-                       reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
-
-               shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
-               if (shadow_data == NULL) {
-                       DEBUG(0,("TALLOC_ZERO() failed!\n"));
-                       talloc_destroy(shadow_mem_ctx);
-                       reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
-
-               shadow_data->mem_ctx = shadow_mem_ctx;
-
-               /*
-                * Call the VFS routine to actually do the work.
-                */
-               if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
-                       talloc_destroy(shadow_data->mem_ctx);
-                       if (errno == ENOSYS) {
-                               DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
-                                       conn->connectpath));
-                               reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
-                               return;
-                       } else {
-                               DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
-                                       conn->connectpath));
-                               reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
-                               return;
-                       }
-               }
-
-               labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
-
-               if (!labels) {
-                       data_count = 16;
-               } else {
-                       data_count = 12+labels_data_count+4;
-               }
-
-               if (max_data_count<data_count) {
-                       DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
-                               max_data_count,data_count));
-                       talloc_destroy(shadow_data->mem_ctx);
-                       reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
-                       return;
-               }
-
-               pdata = nttrans_realloc(ppdata, data_count);
-               if (pdata == NULL) {
-                       talloc_destroy(shadow_data->mem_ctx);
-                       reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
-
-               cur_pdata = pdata;
-
-               /* num_volumes 4 bytes */
-               SIVAL(pdata,0,shadow_data->num_volumes);
-
-               if (labels) {
-                       /* num_labels 4 bytes */
-                       SIVAL(pdata,4,shadow_data->num_volumes);
-               }
-
-               /* needed_data_count 4 bytes */
-               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_str_dbg(fsp)));
-               if (labels && shadow_data->labels) {
-                       for (i=0;i<shadow_data->num_volumes;i++) {
-                               srvstr_push(pdata, req->flags2,
-                                           cur_pdata, shadow_data->labels[i],
-                                           2*sizeof(SHADOW_COPY_LABEL),
-                                           STR_UNICODE|STR_TERMINATE);
-                               cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
-                               DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
-                       }
-               }
-
-               talloc_destroy(shadow_data->mem_ctx);
-
-               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
-                               pdata, data_count);
-
-               return;
-        }
-
-       case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
-       {
-               /* pretend this succeeded -
-                *
-                * we have to send back a list with all files owned by this SID
-                *
-                * but I have to check that --metze
-                */
-               DOM_SID sid;
-               uid_t uid;
-               size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
-
-               DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
-
-               if (!fsp_belongs_conn(conn, req, fsp)) {
-                       return;
-               }
-
-               /* unknown 4 bytes: this is not the length of the sid :-(  */
-               /*unknown = IVAL(pdata,0);*/
-
-               sid_parse(pdata+4,sid_len,&sid);
-               DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
-
-               if (!sid_to_uid(&sid, &uid)) {
-                       DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
-                                sid_string_dbg(&sid),
-                                (unsigned long)sid_len));
-                       uid = (-1);
-               }
-
-               /* we can take a look at the find source :-)
-                *
-                * find ./ -uid $uid  -name '*'   is what we need here
-                *
-                *
-                * and send 4bytes len and then NULL terminated unicode strings
-                * for each file
-                *
-                * but I don't know how to deal with the paged results
-                * (maybe we can hang the result anywhere in the fsp struct)
-                *
-                * we don't send all files at once
-                * and at the next we should *not* start from the beginning,
-                * so we have to cache the result
-                *
-                * --metze
-                */
+       SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
 
-               /* this works for now... */
-               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
-               return;
-       }
-       default:
-               if (!logged_ioctl_message) {
-                       logged_ioctl_message = true; /* Only print this once... */
-                       DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
-                                function));
-               }
+       /*
+        * out_data might be allocated by the VFS module, but talloc should be
+        * used, and should be cleaned up when the request ends.
+        */
+       status = SMB_VFS_FSCTL(fsp, 
+                              ctx,
+                              function, 
+                              req->flags2,
+                              (uint8_t *)pdata, 
+                              data_count, 
+                              (uint8_t **)&out_data,
+                              max_data_count,
+                              &out_data_len);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+       } else {
+               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
        }
-
-       reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
 }
 
 
@@ -2162,7 +2277,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
        files_struct *fsp = NULL;
        uint16 level = 0;
        size_t sid_len;
-       DOM_SID sid;
+       struct dom_sid sid;
        bool start_enum = True;
        SMB_NTQUOTA_STRUCT qt;
        SMB_NTQUOTA_LIST *tmp_list;
@@ -2171,11 +2286,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 (get_current_uid(conn) != 0) {
                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);
+                        "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
+                        conn->session_info->unix_info->unix_name));
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -2185,7 +2300,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;
        }
 
@@ -2220,7 +2335,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;
                                }
 
@@ -2240,7 +2355,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;
                        }
 
@@ -2248,7 +2363,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;
                        }
 
@@ -2257,7 +2372,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;
                        }
 
@@ -2278,7 +2393,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                                tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
 
                                sid_len = ndr_size_dom_sid(
-                                       &tmp_list->quotas->sid, NULL, 0);
+                                       &tmp_list->quotas->sid, 0);
                                entry_len = 40 + sid_len;
 
                                /* nextoffset entry 4 bytes */
@@ -2320,20 +2435,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;
                        }
 
@@ -2349,7 +2464,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);
@@ -2364,13 +2482,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;
                        }
 
@@ -2403,8 +2521,10 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        break;
 
                default:
-                       DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
-                       reply_doserror(req, ERRSRV, ERRerror);
+                       DEBUG(0, ("do_nt_transact_get_user_quota: %s: unknown "
+                                 "level 0x%04hX\n",
+                                 fsp_fnum_dbg(fsp), level));
+                       reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                        return;
                        break;
        }
@@ -2432,17 +2552,17 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        int data_len=0,param_len=0;
        SMB_NTQUOTA_STRUCT qt;
        size_t sid_len;
-       DOM_SID sid;
+       struct dom_sid sid;
        files_struct *fsp = NULL;
 
        ZERO_STRUCT(qt);
 
        /* access check */
-       if (conn->server_info->utok.uid != 0) {
+       if (get_current_uid(conn) != 0) {
                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);
+                        "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
+                        conn->session_info->unix_info->unix_name));
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -2452,7 +2572,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;
        }
 
@@ -2466,7 +2586,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_PARAMETER);
                return;
        }
 
@@ -2478,9 +2598,9 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        /* sid len */
        sid_len = IVAL(pdata,4);
 
-       if (data_count < 40+sid_len) {
+       if (data_count < 40+sid_len || (40+sid_len < 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_PARAMETER);
                return;
        }
 
@@ -2489,54 +2609,25 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
         */
 
        /* the used space 8 bytes (uint64_t)*/
-       qt.usedspace = (uint64_t)IVAL(pdata,16);
-#ifdef LARGE_SMB_OFF_T
-       qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(pdata,20) != 0)&&
-               ((qt.usedspace != 0xFFFFFFFF)||
-               (IVAL(pdata,20)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
-               return;
-       }
-#endif /* LARGE_SMB_OFF_T */
+       qt.usedspace = BVAL(pdata,16);
 
        /* the soft quotas 8 bytes (uint64_t)*/
-       qt.softlim = (uint64_t)IVAL(pdata,24);
-#ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(pdata,28) != 0)&&
-               ((qt.softlim != 0xFFFFFFFF)||
-               (IVAL(pdata,28)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
-               return;
-       }
-#endif /* LARGE_SMB_OFF_T */
+       qt.softlim = BVAL(pdata,24);
 
        /* the hard quotas 8 bytes (uint64_t)*/
-       qt.hardlim = (uint64_t)IVAL(pdata,32);
-#ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(pdata,36) != 0)&&
-               ((qt.hardlim != 0xFFFFFFFF)||
-               (IVAL(pdata,36)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               reply_doserror(req, ERRDOS, ERRunknownlevel);
+       qt.hardlim = BVAL(pdata,32);
+
+       if (!sid_parse(pdata+40,sid_len,&sid)) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
-#endif /* LARGE_SMB_OFF_T */
 
-       sid_parse(pdata+40,sid_len,&sid);
        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;
        }
 
@@ -2551,7 +2642,7 @@ static void handle_nttrans(connection_struct *conn,
 {
        if (get_Protocol() >= PROTOCOL_NT1) {
                req->flags2 |= 0x40; /* IS_LONG_NAME */
-               SSVAL(req->inbuf,smb_flg2,req->flags2);
+               SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
        }
 
 
@@ -2670,7 +2761,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;
@@ -2706,7 +2797,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;
        }
@@ -2719,8 +2810,8 @@ void reply_nttrans(struct smb_request *req)
                return;
        }
 
-       if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+       if ((state = talloc(conn, struct trans_state)) == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                END_PROFILE(SMBnttrans);
                return;
        }
@@ -2766,7 +2857,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;
        }
@@ -2787,7 +2878,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;
                }
@@ -2809,7 +2900,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;
                }
@@ -2842,7 +2933,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;
                }
@@ -2893,7 +2984,13 @@ void reply_nttranss(struct smb_request *req)
 
        START_PROFILE(SMBnttranss);
 
-       show_msg((char *)req->inbuf);
+       show_msg((const 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);