s3: smbd: Change canonicalize_ea_name() to take a const smb_filename * parameter...
[sfrench/samba-autobuild/.git] / source3 / smbd / nttrans.c
index 5fdb07d769a0e46fc1f0f5135d6153ba6b8e260c..b1917fe62c11c857e9a8e04038c32decf340677c 100644 (file)
@@ -27,9 +27,9 @@
 #include "../librpc/gen_ndr/ndr_security.h"
 #include "passdb/lookup_sid.h"
 #include "auth.h"
-#include "ntioctl.h"
 #include "smbprofile.h"
 #include "libsmb/libsmb.h"
+#include "lib/util_ea.h"
 
 extern const struct generic_mapping file_generic_mapping;
 
@@ -67,8 +67,8 @@ static void send_nt_replies(connection_struct *conn,
        int params_sent_thistime, data_sent_thistime, total_sent_thistime;
        int alignment_offset = 1;
        int data_alignment_offset = 0;
-       struct smbd_server_connection *sconn = req->sconn;
-       int max_send = sconn->smb1.sessions.max_send;
+       struct smbXsrv_connection *xconn = req->xconn;
+       int max_send = xconn->smb1.sessions.max_send;
 
        /*
         * If there genuinely are no parameters or data to send just send
@@ -83,7 +83,7 @@ static void send_nt_replies(connection_struct *conn,
                                         __LINE__,__FILE__);
                }
                show_msg((char *)req->outbuf);
-               if (!srv_send_smb(sconn,
+               if (!srv_send_smb(xconn,
                                (char *)req->outbuf,
                                true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn),
@@ -143,11 +143,6 @@ static 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.
                 */
@@ -252,7 +247,7 @@ static void send_nt_replies(connection_struct *conn,
 
                /* Send the packet */
                show_msg((char *)req->outbuf);
-               if (!srv_send_smb(sconn,
+               if (!srv_send_smb(xconn,
                                (char *)req->outbuf,
                                true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn),
@@ -285,15 +280,17 @@ static 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)) {
@@ -318,9 +315,9 @@ 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);
+       uint32_t flags = IVAL(req->vwv+3, 1);
        TALLOC_CTX *ctx = talloc_tos();
 
        srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
@@ -344,7 +341,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);
@@ -353,6 +350,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);
@@ -380,8 +380,6 @@ 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 {
@@ -440,18 +438,18 @@ 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;
-       uint32 file_attributes;
-       uint32 share_access;
-       uint32 create_disposition;
-       uint32 create_options;
-       uint16 root_dir_fid;
+       uint32_t flags;
+       uint32_t access_mask;
+       uint32_t file_attributes;
+       uint32_t share_access;
+       uint32_t create_disposition;
+       uint32_t create_options;
+       uint16_t root_dir_fid;
        uint64_t allocation_size;
        /* Breakout the oplock request bits so we can set the
           reply bits separately. */
-       uint32 fattr=0;
-       SMB_OFF_T file_len = 0;
+       uint32_t fattr=0;
+       off_t file_len = 0;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
@@ -459,11 +457,12 @@ void reply_ntcreate_and_X(struct smb_request *req)
        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;
+       uint32_t ucf_flags = UCF_PREP_CREATEFILE |
+                       (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBntcreateX);
@@ -479,7 +478,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        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);
+       root_dir_fid = (uint16_t)IVAL(req->vwv+5, 1);
 
        allocation_size = BVAL(req->vwv+9, 1);
 
@@ -541,7 +540,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               0,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
 
@@ -576,15 +575,17 @@ void reply_ntcreate_and_X(struct smb_request *req)
                create_options,                         /* create_options */
                file_attributes,                        /* file_attributes */
                oplock_request,                         /* oplock_request */
+               NULL,                                   /* lease */
                allocation_size,                        /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               &info);                                 /* pinfo */
+               &info,                                  /* pinfo */
+               NULL, NULL);                            /* create context */
 
        if (!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->xconn, req->mid)) {
                        /* We have re-scheduled this call, no error. */
                        goto out;
                }
@@ -626,7 +627,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);
@@ -635,6 +636,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);
@@ -655,14 +659,6 @@ void reply_ntcreate_and_X(struct smb_request *req)
                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, 0, NULL, &write_time_ts);
-       if (!null_timespec(write_time_ts)) {
-               update_stat_ex_mtime(&smb_fname->st, write_time_ts);
-       }
-
        /* Create time. */
        create_timespec = get_create_timespec(conn, fsp, smb_fname);
        a_timespec = smb_fname->st.st_ex_atime;
@@ -693,7 +689,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 ? */
@@ -702,7 +698,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, ctx,
                        &num_streams, &streams);
                /* There is always one stream, ::$DATA. */
                if (NT_STATUS_IS_OK(status) && num_streams > 1) {
@@ -715,9 +711,10 @@ void reply_ntcreate_and_X(struct smb_request *req)
        SCVAL(p,0,fsp->is_directory ? 1 : 0);
 
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
-               uint32 perms = 0;
+               uint32_t perms = 0;
                p += 25;
                if (fsp->is_directory ||
+                   fsp->can_write ||
                    can_write_to_file(conn, smb_fname)) {
                        perms = FILE_GENERIC_ALL;
                } else {
@@ -726,10 +723,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;
@@ -741,17 +737,17 @@ void reply_ntcreate_and_X(struct smb_request *req)
 
 static void do_nt_transact_create_pipe(connection_struct *conn,
                                       struct smb_request *req,
-                                      uint16 **ppsetup, uint32 setup_count,
-                                      char **ppparams, uint32 parameter_count,
-                                      char **ppdata, uint32 data_count)
+                                      uint16_t **ppsetup, uint32_t setup_count,
+                                      char **ppparams, uint32_t parameter_count,
+                                      char **ppdata, uint32_t data_count)
 {
        char *fname = NULL;
        char *params = *ppparams;
-       int pnum = -1;
+       uint16_t pnum = FNUM_FIELD_INVALID;
        char *p = NULL;
        NTSTATUS status;
        size_t param_len;
-       uint32 flags;
+       uint32_t flags;
        TALLOC_CTX *ctx = talloc_tos();
 
        /*
@@ -766,9 +762,25 @@ static void do_nt_transact_create_pipe(connection_struct *conn,
 
        flags = IVAL(params,0);
 
-       srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
-                       parameter_count-53, STR_TERMINATE,
+       if (req->posix_pathnames) {
+               srvstr_get_path_posix(ctx,
+                       params,
+                       req->flags2,
+                       &fname,
+                       params+53,
+                       parameter_count-53,
+                       STR_TERMINATE,
                        &status);
+       } else {
+               srvstr_get_path(ctx,
+                       params,
+                       req->flags2,
+                       &fname,
+                       params+53,
+                       parameter_count-53,
+                       STR_TERMINATE,
+                       &status);
+       }
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
@@ -831,20 +843,48 @@ 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.
 ****************************************************************************/
 
-NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
+NTSTATUS set_sd(files_struct *fsp, struct security_descriptor *psd,
                       uint32_t security_info_sent)
 {
-       struct security_descriptor *psd = NULL;
        NTSTATUS status;
 
-       if (sd_len == 0) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
        if (!CAN_WRITE(fsp->conn)) {
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -853,10 +893,10 @@ NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
                return NT_STATUS_OK;
        }
 
-       status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (S_ISLNK(fsp->fsp_name->st.st_ex_mode)) {
+               DEBUG(10, ("ACL set on symlink %s denied.\n",
+                       fsp_str_dbg(fsp)));
+               return NT_STATUS_ACCESS_DENIED;
        }
 
        if (psd->owner_sid == NULL) {
@@ -868,7 +908,8 @@ NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
 
        /* Ensure we have at least one thing set. */
        if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
-               return NT_STATUS_INVALID_PARAMETER;
+               /* Just like W2K3 */
+               return NT_STATUS_OK;
        }
 
        /* Ensure we have the rights to do this. */
@@ -904,6 +945,8 @@ NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
                }
        }
 
+       canonicalize_inheritance_bits(psd);
+
        if (DEBUGLEVEL >= 10) {
                DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
                NDR_PRINT_DEBUG(security_descriptor, psd);
@@ -917,34 +960,26 @@ NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
 }
 
 /****************************************************************************
Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
Internal fn to set security descriptors from a data blob.
 ****************************************************************************/
 
-struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
+NTSTATUS set_sd_blob(files_struct *fsp, uint8_t *data, uint32_t sd_len,
+                      uint32_t security_info_sent)
 {
-       struct ea_list *ea_list_head = NULL;
-       size_t offset = 0;
+       struct security_descriptor *psd = NULL;
+       NTSTATUS status;
 
-       if (data_size < 4) {
-               return NULL;
+       if (sd_len == 0) {
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       while (offset + 4 <= data_size) {
-               size_t next_offset = IVAL(pdata,offset);
-               struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
-
-               if (!eal) {
-                       return NULL;
-               }
+       status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
 
-               DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
-               if (next_offset == 0) {
-                       break;
-               }
-               offset += next_offset;
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-       return ea_list_head;
+       return set_sd(fsp, psd, security_info_sent);
 }
 
 /****************************************************************************
@@ -953,36 +988,35 @@ struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t
 
 static void call_nt_transact_create(connection_struct *conn,
                                    struct smb_request *req,
-                                   uint16 **ppsetup, uint32 setup_count,
-                                   char **ppparams, uint32 parameter_count,
-                                   char **ppdata, uint32 data_count,
-                                   uint32 max_data_count)
+                                   uint16_t **ppsetup, uint32_t setup_count,
+                                   char **ppparams, uint32_t parameter_count,
+                                   char **ppdata, uint32_t data_count,
+                                   uint32_t 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;
+       uint32_t fattr=0;
+       off_t file_len = 0;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
-       uint32 flags;
-       uint32 access_mask;
-       uint32 file_attributes;
-       uint32 share_access;
-       uint32 create_disposition;
-       uint32 create_options;
-       uint32 sd_len;
+       uint32_t flags;
+       uint32_t access_mask;
+       uint32_t file_attributes;
+       uint32_t share_access;
+       uint32_t create_disposition;
+       uint32_t create_options;
+       uint32_t sd_len;
        struct security_descriptor *sd = NULL;
-       uint32 ea_len;
-       uint16 root_dir_fid;
+       uint32_t ea_len;
+       uint16_t 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;
@@ -990,6 +1024,8 @@ static void call_nt_transact_create(connection_struct *conn,
        int oplock_request;
        uint8_t oplock_granted;
        struct case_semantics_state *case_state = NULL;
+       uint32_t ucf_flags = UCF_PREP_CREATEFILE |
+                       (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
        TALLOC_CTX *ctx = talloc_tos();
 
        DEBUG(5,("call_nt_transact_create\n"));
@@ -1029,7 +1065,7 @@ static void call_nt_transact_create(connection_struct *conn,
        create_options = IVAL(params,32);
        sd_len = IVAL(params,36);
        ea_len = IVAL(params,40);
-       root_dir_fid = (uint16)IVAL(params,4);
+       root_dir_fid = (uint16_t)IVAL(params,4);
        allocation_size = BVAL(params,12);
 
        /*
@@ -1038,6 +1074,59 @@ static void call_nt_transact_create(connection_struct *conn,
         */
        create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
 
+       if (req->posix_pathnames) {
+               srvstr_get_path_posix(ctx,
+                       params,
+                       req->flags2,
+                       &fname,
+                       params+53,
+                       parameter_count-53,
+                       STR_TERMINATE,
+                       &status);
+       } else {
+               srvstr_get_path(ctx,
+                       params,
+                       req->flags2,
+                       &fname,
+                       params+53,
+                       parameter_count-53,
+                       STR_TERMINATE,
+                       &status);
+       }
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+               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,
+                               ucf_flags,
+                               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;
+       }
+
        /* Ensure the data_len is correct for the sd and ea values given. */
        if ((ea_len + sd_len > data_count)
            || (ea_len > data_count) || (sd_len > data_count)
@@ -1088,43 +1177,26 @@ static void call_nt_transact_create(connection_struct *conn,
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
                }
-       }
-
-       srvstr_get_path(ctx, params, req->flags2, &fname,
-                       params+53, parameter_count-53,
-                       STR_TERMINATE, &status);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               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 (ea_list_has_invalid_name(ea_list)) {
+                       /* Realloc the size of parameters and data we will return */
+                       if (flags & EXTENDED_RESPONSE_REQUIRED) {
+                               /* Extended response is 32 more byyes. */
+                               param_len = 101;
+                       } else {
+                               param_len = 69;
+                       }
+                       params = nttrans_realloc(ppparams, param_len);
+                       if(params == NULL) {
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
+                               goto out;
+                       }
 
-       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);
+                       memset(params, '\0', param_len);
+                       send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
+                               params, param_len, NULL, 0);
                        goto out;
                }
-               reply_nterror(req, status);
-               goto out;
        }
 
        oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
@@ -1151,15 +1223,17 @@ static void call_nt_transact_create(connection_struct *conn,
                create_options,                         /* create_options */
                file_attributes,                        /* file_attributes */
                oplock_request,                         /* oplock_request */
+               NULL,                                   /* lease */
                allocation_size,                        /* allocation_size */
                0,                                      /* private_flags */
                sd,                                     /* sd */
                ea_list,                                /* ea_list */
                &fsp,                                   /* result */
-               &info);                                 /* pinfo */
+               &info,                                  /* pinfo */
+               NULL, NULL);                            /* create context */
 
        if(!NT_STATUS_IS_OK(status)) {
-               if (open_was_deferred(req->mid)) {
+               if (open_was_deferred(req->xconn, req->mid)) {
                        /* We have re-scheduled this call, no error. */
                        return;
                }
@@ -1230,14 +1304,6 @@ static void call_nt_transact_create(connection_struct *conn,
                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, 0, NULL, &write_time_ts);
-       if (!null_timespec(write_time_ts)) {
-               update_stat_ex_mtime(&smb_fname->st, write_time_ts);
-       }
-
        /* Create time. */
        create_timespec = get_create_timespec(conn, fsp, smb_fname);
        a_timespec = smb_fname->st.st_ex_atime;
@@ -1268,7 +1334,7 @@ static void call_nt_transact_create(connection_struct *conn,
        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 ? */
@@ -1277,7 +1343,7 @@ static void call_nt_transact_create(connection_struct *conn,
                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, ctx,
                        &num_streams, &streams);
                /* There is always one stream, ::$DATA. */
                if (NT_STATUS_IS_OK(status) && num_streams > 1) {
@@ -1290,9 +1356,10 @@ static void call_nt_transact_create(connection_struct *conn,
        SCVAL(p,0,fsp->is_directory ? 1 : 0);
 
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
-               uint32 perms = 0;
+               uint32_t perms = 0;
                p += 25;
                if (fsp->is_directory ||
+                   fsp->can_write ||
                    can_write_to_file(conn, smb_fname)) {
                        perms = FILE_GENERIC_ALL;
                } else {
@@ -1317,14 +1384,17 @@ static void call_nt_transact_create(connection_struct *conn,
 
 void reply_ntcancel(struct smb_request *req)
 {
+       struct smbXsrv_connection *xconn = req->xconn;
+       struct smbd_server_connection *sconn = req->sconn;
+
        /*
         * Go through and cancel any pending change notifies.
         */
 
        START_PROFILE(SMBntcancel);
-       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);
+       srv_cancel_sign_response(xconn);
+       remove_pending_change_notify_requests_by_mid(sconn, req->mid);
+       remove_pending_lock_requests_by_mid_smb1(sconn, req->mid);
 
        DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
                (unsigned long long)req->mid));
@@ -1342,12 +1412,12 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                                struct smb_request *req,
                                struct smb_filename *smb_fname_src,
                                struct smb_filename *smb_fname_dst,
-                               uint32 attrs)
+                               uint32_t attrs)
 {
        files_struct *fsp1,*fsp2;
-       uint32 fattr;
+       uint32_t fattr;
        int info;
-       SMB_OFF_T ret=-1;
+       off_t ret=-1;
        NTSTATUS status = NT_STATUS_OK;
        char *parent;
 
@@ -1398,12 +1468,14 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                0,                                      /* create_options */
                FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
                NO_OPLOCK,                              /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp1,                                  /* result */
-               &info);                                 /* pinfo */
+               &info,                                  /* pinfo */
+               NULL, NULL);                            /* create context */
 
        if (!NT_STATUS_IS_OK(status)) {
                goto out;
@@ -1422,12 +1494,14 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                0,                                      /* create_options */
                fattr,                                  /* file_attributes */
                NO_OPLOCK,                              /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp2,                                  /* result */
-               &info);                                 /* pinfo */
+               &info,                                  /* pinfo */
+               NULL, NULL);                            /* create context */
 
        if (!NT_STATUS_IS_OK(status)) {
                close_file(NULL, fsp1, ERROR_CLOSE);
@@ -1462,7 +1536,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;
        }
@@ -1491,10 +1565,10 @@ void reply_ntrename(struct smb_request *req)
        NTSTATUS status;
        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;
+       uint32_t attrs;
+       uint32_t ucf_flags_src = (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
+       uint32_t ucf_flags_dst = (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
+       uint16_t rename_type;
        TALLOC_CTX *ctx = talloc_tos();
        bool stream_rename = false;
 
@@ -1516,7 +1590,7 @@ void reply_ntrename(struct smb_request *req)
                goto out;
        }
 
-       if (ms_has_wild(oldname)) {
+       if (!req->posix_pathnames && ms_has_wild(oldname)) {
                reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
                goto out;
        }
@@ -1529,7 +1603,7 @@ void reply_ntrename(struct smb_request *req)
                goto out;
        }
 
-       if (!lp_posix_pathnames()) {
+       if (!req->posix_pathnames) {
                /* The newname must begin with a ':' if the
                   oldname contains a ':'. */
                if (strchr_m(oldname, ':')) {
@@ -1642,7 +1716,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->xconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        goto out;
                }
@@ -1671,17 +1745,17 @@ static void smbd_smb1_notify_reply(struct smb_request *req,
 
 static void call_nt_transact_notify_change(connection_struct *conn,
                                           struct smb_request *req,
-                                          uint16 **ppsetup,
-                                          uint32 setup_count,
+                                          uint16_t **ppsetup,
+                                          uint32_t setup_count,
                                           char **ppparams,
-                                          uint32 parameter_count,
-                                          char **ppdata, uint32 data_count,
-                                          uint32 max_data_count,
-                                          uint32 max_param_count)
+                                          uint32_t parameter_count,
+                                          char **ppdata, uint32_t data_count,
+                                          uint32_t max_data_count,
+                                          uint32_t max_param_count)
 {
-       uint16 *setup = *ppsetup;
+       uint16_t *setup = *ppsetup;
        files_struct *fsp;
-       uint32 filter;
+       uint32_t filter;
        NTSTATUS status;
        bool recursive;
 
@@ -1733,7 +1807,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
@@ -1778,10 +1852,10 @@ static void call_nt_transact_notify_change(connection_struct *conn,
 
 static void call_nt_transact_rename(connection_struct *conn,
                                    struct smb_request *req,
-                                   uint16 **ppsetup, uint32 setup_count,
-                                   char **ppparams, uint32 parameter_count,
-                                   char **ppdata, uint32 data_count,
-                                   uint32 max_data_count)
+                                   uint16_t **ppsetup, uint32_t setup_count,
+                                   char **ppparams, uint32_t parameter_count,
+                                   char **ppdata, uint32_t data_count,
+                                   uint32_t max_data_count)
 {
        char *params = *ppparams;
        char *new_name = NULL;
@@ -1799,9 +1873,28 @@ static void call_nt_transact_rename(connection_struct *conn,
        if (!check_fsp(conn, req, fsp)) {
                return;
        }
-       srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
-                             parameter_count - 4,
-                             STR_TERMINATE, &status, &dest_has_wcard);
+       if (req->posix_pathnames) {
+               srvstr_get_path_wcard_posix(ctx,
+                               params,
+                               req->flags2,
+                               &new_name,
+                               params+4,
+                               parameter_count - 4,
+                               STR_TERMINATE,
+                               &status,
+                               &dest_has_wcard);
+       } else {
+               srvstr_get_path_wcard(ctx,
+                               params,
+                               req->flags2,
+                               &new_name,
+                               params+4,
+                               parameter_count - 4,
+                               STR_TERMINATE,
+                               &status,
+                               &dest_has_wcard);
+       }
+
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
@@ -1838,7 +1931,7 @@ static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor
 
 /****************************************************************************
  Reply to query a security descriptor.
- Callable from SMB2 and SMB2.
+ Callable from SMB1 and SMB2.
  If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
  the required size.
 ****************************************************************************/
@@ -1853,6 +1946,7 @@ NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
 {
        NTSTATUS status;
        struct security_descriptor *psd = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
 
        /*
         * Get the permissions to return.
@@ -1860,21 +1954,43 @@ NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
 
        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 (S_ISLNK(fsp->fsp_name->st.st_ex_mode)) {
+               DEBUG(10, ("ACL get on symlink %s denied.\n",
+                       fsp_str_dbg(fsp)));
+               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(mem_ctx, &psd);
+               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, &psd);
+                       fsp, security_info_wanted, frame, &psd);
        }
        if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
                return status;
        }
 
@@ -1885,9 +2001,11 @@ NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
                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;
        }
 
@@ -1900,6 +2018,15 @@ NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
            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",
@@ -1912,7 +2039,7 @@ NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
        }
 
        if (max_data_count < *psd_size) {
-               TALLOC_FREE(psd);
+               TALLOC_FREE(frame);
                return NT_STATUS_BUFFER_TOO_SMALL;
        }
 
@@ -1920,11 +2047,11 @@ NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
                                   ppmarshalled_sd, psd_size);
 
        if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(psd);
+               TALLOC_FREE(frame);
                return status;
        }
 
-       TALLOC_FREE(psd);
+       TALLOC_FREE(frame);
        return NT_STATUS_OK;
 }
 
@@ -1934,18 +2061,18 @@ NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
 
 static void call_nt_transact_query_security_desc(connection_struct *conn,
                                                 struct smb_request *req,
-                                                uint16 **ppsetup,
-                                                uint32 setup_count,
+                                                uint16_t **ppsetup,
+                                                uint32_t setup_count,
                                                 char **ppparams,
-                                                uint32 parameter_count,
+                                                uint32_t parameter_count,
                                                 char **ppdata,
-                                                uint32 data_count,
-                                                uint32 max_data_count)
+                                                uint32_t data_count,
+                                                uint32_t max_data_count)
 {
        char *params = *ppparams;
        char *data = *ppdata;
        size_t sd_size = 0;
-       uint32 security_info_wanted;
+       uint32_t security_info_wanted;
        files_struct *fsp = NULL;
        NTSTATUS status;
        uint8_t *marshalled_sd = NULL;
@@ -1980,7 +2107,8 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
        status = smbd_do_query_security_desc(conn,
                                        talloc_tos(),
                                        fsp,
-                                       security_info_wanted,
+                                       security_info_wanted &
+                                       SMB_SUPPORTED_SECINFO_FLAGS,
                                        max_data_count,
                                        &marshalled_sd,
                                        &sd_size);
@@ -2030,18 +2158,18 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
 
 static void call_nt_transact_set_security_desc(connection_struct *conn,
                                               struct smb_request *req,
-                                              uint16 **ppsetup,
-                                              uint32 setup_count,
+                                              uint16_t **ppsetup,
+                                              uint32_t setup_count,
                                               char **ppparams,
-                                              uint32 parameter_count,
+                                              uint32_t parameter_count,
                                               char **ppdata,
-                                              uint32 data_count,
-                                              uint32 max_data_count)
+                                              uint32_t data_count,
+                                              uint32_t max_data_count)
 {
        char *params= *ppparams;
        char *data = *ppdata;
        files_struct *fsp = NULL;
-       uint32 security_info_sent = 0;
+       uint32_t security_info_sent = 0;
        NTSTATUS status;
 
        if(parameter_count < 8) {
@@ -2073,8 +2201,8 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
                return;
        }
 
-       status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
-
+       status = set_sd_blob(fsp, (uint8_t *)data, data_count,
+                            security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
@@ -2091,17 +2219,21 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
 
 static void call_nt_transact_ioctl(connection_struct *conn,
                                   struct smb_request *req,
-                                  uint16 **ppsetup, uint32 setup_count,
-                                  char **ppparams, uint32 parameter_count,
-                                  char **ppdata, uint32 data_count,
-                                  uint32 max_data_count)
+                                  uint16_t **ppsetup, uint32_t setup_count,
+                                  char **ppparams, uint32_t parameter_count,
+                                  char **ppdata, uint32_t data_count,
+                                  uint32_t max_data_count)
 {
-       uint32 function;
-       uint16 fidnum;
+       NTSTATUS status;
+       uint32_t function;
+       uint16_t fidnum;
        files_struct *fsp;
-       uint8 isFSctl;
-       uint8 compfilter;
+       uint8_t isFSctl;
+       uint8_t compfilter;
+       char *out_data = NULL;
+       uint32_t 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));
@@ -2114,353 +2246,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:
-       {
-               bool set_sparse = true;
-               NTSTATUS status;
-
-               if (data_count >= 1 && pdata[0] == 0) {
-                       set_sparse = false;
-               }
-
-               DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X]set[%u]\n",
-                        fidnum, set_sparse));
-
-               if (!check_fsp_open(conn, req, fsp)) {
-                       return;
-               }
-
-               status = file_set_sparse(conn, fsp, set_sparse);
-               if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(9,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
-                                smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
-                       reply_nterror(req, status);
-                       return;
-               }
-
-               DEBUG(10,("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
-                        smb_fname_str_dbg(fsp->fsp_name), set_sparse, nt_errstr(status)));
-               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
+       /*
+        * 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_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 (!check_fsp_open(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);
+       /* Has to be for an open file! */
+       if (!check_fsp_open(conn, req, fsp)) {
                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);
-               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.
-                */
-               struct shadow_copy_data *shadow_data = NULL;
-               bool labels = False;
-               uint32 labels_data_count = 0;
-               uint32 i;
-               char *cur_pdata;
-
-               if (!check_fsp_open(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_data = talloc_zero(talloc_tos(),
-                                           struct shadow_copy_data);
-               if (shadow_data == NULL) {
-                       DEBUG(0,("TALLOC_ZERO() failed!\n"));
-                       reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
-
-               /*
-                * Call the VFS routine to actually do the work.
-                */
-               if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
-                       TALLOC_FREE(shadow_data);
-                       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_FREE(shadow_data);
-                       reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
-                       return;
-               }
-
-               pdata = nttrans_realloc(ppdata, data_count);
-               if (pdata == NULL) {
-                       TALLOC_FREE(shadow_data);
-                       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_FREE(shadow_data);
-
-               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
-                */
-               struct dom_sid sid;
-               uid_t uid;
-               size_t sid_len;
-
-               DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
-
-               if (!check_fsp_open(conn, req, fsp)) {
-                       return;
-               }
-
-               if (data_count < 8) {
-                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
-               }
-
-               sid_len = MIN(data_count-4,SID_MAX_SIZE);
-
-               /* unknown 4 bytes: this is not the length of the sid :-(  */
-               /*unknown = IVAL(pdata,0);*/
-
-               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)) {
-                       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
-                */
-
-               /* this works for now... */
-               send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
-               return;
-       }
-       case FSCTL_QUERY_ALLOCATED_RANGES:
-       {
-               /* FIXME: This is just a dummy reply, telling that all of the
-                * file is allocated. MKS cp needs that.
-                * Adding the real allocated ranges via FIEMAP on Linux
-                * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
-                * this FSCTL correct for sparse files.
-                */
-               NTSTATUS status;
-               uint64_t offset, length;
-
-               if (!check_fsp_open(conn, req, fsp)) {
-                       return;
-               }
-
-               if (data_count != 16) {
-                       DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
-                               data_count));
-                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
-               }
-
-               if (max_data_count < 16) {
-                       DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_data_count(%u) < 16 is invalid!\n",
-                               max_data_count));
-                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
-               }
-
-               offset = BVAL(pdata,0);
-               length = BVAL(pdata,8);
-
-               if (offset + length < offset) {
-                       /* No 64-bit integer wrap. */
-                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
-               }
-
-               status = vfs_stat_fsp(fsp);
-               if (!NT_STATUS_IS_OK(status)) {
-                       reply_nterror(req, status);
-                       return;
-               }
+       SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
 
-               if (offset > fsp->fsp_name->st.st_ex_size ||
-                               fsp->fsp_name->st.st_ex_size == 0 ||
-                               length == 0) {
-                       send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
-               } else {
-                       uint64_t end = offset + length;
-                       end = MIN(end, fsp->fsp_name->st.st_ex_size);
-                       SBVAL(pdata,0,0);
-                       SBVAL(pdata,8,end);
-                       send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
-                               pdata, 16);
-               }
-               return;
-       }
-       case FSCTL_IS_VOLUME_DIRTY:
-               DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
-                         "(but not implemented)\n", (int)fidnum));
-               /*
-                * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
-                * says we have to respond with NT_STATUS_INVALID_PARAMETER
-                */
-               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
-       default:
-               /* Only print this once... */
-               if (!logged_ioctl_message) {
-                       logged_ioctl_message = true;
-                       DEBUG(2,("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);
 }
 
 
@@ -2471,13 +2296,13 @@ static void call_nt_transact_ioctl(connection_struct *conn,
 
 static void call_nt_transact_get_user_quota(connection_struct *conn,
                                            struct smb_request *req,
-                                           uint16 **ppsetup,
-                                           uint32 setup_count,
+                                           uint16_t **ppsetup,
+                                           uint32_t setup_count,
                                            char **ppparams,
-                                           uint32 parameter_count,
+                                           uint32_t parameter_count,
                                            char **ppdata,
-                                           uint32 data_count,
-                                           uint32 max_data_count)
+                                           uint32_t data_count,
+                                           uint32_t max_data_count)
 {
        NTSTATUS nt_status = NT_STATUS_OK;
        char *params = *ppparams;
@@ -2487,7 +2312,7 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
        int qt_len=0;
        int entry_len = 0;
        files_struct *fsp = NULL;
-       uint16 level = 0;
+       uint16_t level = 0;
        size_t sid_len;
        struct dom_sid sid;
        bool start_enum = True;
@@ -2498,10 +2323,10 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
        ZERO_STRUCT(qt);
 
        /* access check */
-       if (get_current_uid(conn) != 0) {
+       if (get_current_uid(conn) != sec_initial_uid()) {
                DEBUG(1,("get_user_quota: access_denied service [%s] user "
-                        "[%s]\n", lp_servicename(SNUM(conn)),
-                        conn->session_info->unix_name));
+                        "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
+                        conn->session_info->unix_info->unix_name));
                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
@@ -2627,7 +2452,8 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                                SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
 
                                /* and now the SID */
-                               sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
+                               sid_linearize((uint8_t *)(entry+40), sid_len,
+                                             &tmp_list->quotas->sid);
                        }
 
                        qt_handle->tmp_list = tmp_list;
@@ -2676,7 +2502,8 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                                break;
                        }
 
-                       if (!sid_parse(pdata+8,sid_len,&sid)) {
+                       if (!sid_parse((const uint8_t *)(pdata+8), sid_len,
+                                      &sid)) {
                                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                                return;
                        }
@@ -2728,12 +2555,14 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
                        SBIG_UINT(entry,32,qt.hardlim);
 
                        /* and now the SID */
-                       sid_linearize(entry+40, sid_len, &sid);
+                       sid_linearize((uint8_t *)(entry+40), sid_len, &sid);
 
                        break;
 
                default:
-                       DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
+                       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;
@@ -2749,13 +2578,13 @@ static void call_nt_transact_get_user_quota(connection_struct *conn,
 
 static void call_nt_transact_set_user_quota(connection_struct *conn,
                                            struct smb_request *req,
-                                           uint16 **ppsetup,
-                                           uint32 setup_count,
+                                           uint16_t **ppsetup,
+                                           uint32_t setup_count,
                                            char **ppparams,
-                                           uint32 parameter_count,
+                                           uint32_t parameter_count,
                                            char **ppdata,
-                                           uint32 data_count,
-                                           uint32 max_data_count)
+                                           uint32_t data_count,
+                                           uint32_t max_data_count)
 {
        char *params = *ppparams;
        char *pdata = *ppdata;
@@ -2770,8 +2599,8 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        /* access check */
        if (get_current_uid(conn) != 0) {
                DEBUG(1,("set_user_quota: access_denied service [%s] user "
-                        "[%s]\n", lp_servicename(SNUM(conn)),
-                        conn->session_info->unix_name));
+                        "[%s]\n", lp_servicename(talloc_tos(), SNUM(conn)),
+                        conn->session_info->unix_info->unix_name));
                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
@@ -2827,7 +2656,7 @@ static void call_nt_transact_set_user_quota(connection_struct *conn,
        /* the hard quotas 8 bytes (uint64_t)*/
        qt.hardlim = BVAL(pdata,32);
 
-       if (!sid_parse(pdata+40,sid_len,&sid)) {
+       if (!sid_parse((const uint8_t *)(pdata+40), sid_len, &sid)) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
@@ -2988,7 +2817,7 @@ void reply_nttrans(struct smb_request *req)
        uint32_t psoff;
        uint32_t dscnt;
        uint32_t dsoff;
-       uint16 function_code;
+       uint16_t function_code;
        NTSTATUS result;
        struct trans_state *state;
 
@@ -3137,7 +2966,7 @@ void reply_nttrans(struct smb_request *req)
                        goto bad_param;
                }
 
-               state->setup = (uint16 *)TALLOC(state, state->setup_count);
+               state->setup = (uint16_t *)TALLOC(state, state->setup_count);
                if (state->setup == NULL) {
                        DEBUG(0,("reply_nttrans : Out of memory\n"));
                        SAFE_FREE(state->data);
@@ -3149,7 +2978,7 @@ void reply_nttrans(struct smb_request *req)
                }
 
                memcpy(state->setup, req->vwv+19, state->setup_count);
-               dump_data(10, (uint8 *)state->setup, state->setup_count);
+               dump_data(10, (uint8_t *)state->setup, state->setup_count);
        }
 
        if ((state->received_data == state->total_data) &&
@@ -3196,6 +3025,12 @@ void reply_nttranss(struct smb_request *req)
 
        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);
                END_PROFILE(SMBnttranss);