Fix bug 6494 - Incorrect FileStatus returned in NT_CREATE_ANDX.
[ira/wip.git] / source3 / smbd / nttrans.c
index b78c946388eff77de8e12ef11b802108eb5fd240..1b981578e321e563752e19118ce0dee619c7d795 100644 (file)
@@ -19,8 +19,8 @@
 */
 
 #include "includes.h"
+#include "smbd/globals.h"
 
-extern int max_send;
 extern enum protocol_types Protocol;
 extern const struct generic_mapping file_generic_mapping;
 
@@ -58,6 +58,8 @@ void send_nt_replies(connection_struct *conn,
        int params_sent_thistime, data_sent_thistime, total_sent_thistime;
        int alignment_offset = 3;
        int data_alignment_offset = 0;
+       struct smbd_server_connection *sconn = smbd_server_conn;
+       int max_send = sconn->smb1.sessions.max_send;
 
        /*
         * If there genuinely are no parameters or data to send just send
@@ -66,7 +68,20 @@ void send_nt_replies(connection_struct *conn,
 
        if(params_to_send == 0 && data_to_send == 0) {
                reply_outbuf(req, 18, 0);
+               if (NT_STATUS_V(nt_error)) {
+                       error_packet_set((char *)req->outbuf,
+                                        0, 0, nt_error,
+                                        __LINE__,__FILE__);
+               }
                show_msg((char *)req->outbuf);
+               if (!srv_send_smb(smbd_server_fd(),
+                               (char *)req->outbuf,
+                               true, req->seqnum+1,
+                               IS_CONN_ENCRYPTED(conn),
+                               &req->pcd)) {
+                       exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
+               }
+               TALLOC_FREE(req->outbuf);
                return;
        }
 
@@ -119,6 +134,11 @@ 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.
                 */
@@ -225,7 +245,9 @@ void send_nt_replies(connection_struct *conn,
                show_msg((char *)req->outbuf);
                if (!srv_send_smb(smbd_server_fd(),
                                (char *)req->outbuf,
-                               IS_CONN_ENCRYPTED(conn))) {
+                               true, req->seqnum+1,
+                               IS_CONN_ENCRYPTED(conn),
+                               &req->pcd)) {
                        exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
                }
 
@@ -249,21 +271,6 @@ void send_nt_replies(connection_struct *conn,
        }
 }
 
-/****************************************************************************
- Is it an NTFS stream name ?
- An NTFS file name is <path>.<extention>:<stream name>:<stream type>
- $DATA can be used as both a stream name and a stream type. A missing stream
- name or type implies $DATA.
-****************************************************************************/
-
-bool is_ntfs_stream_name(const char *fname)
-{
-       if (lp_posix_pathnames()) {
-               return False;
-       }
-       return (strchr_m(fname, ':') != NULL) ? True : False;
-}
-
 /****************************************************************************
  Reply to an NT create and X call on a pipe
 ****************************************************************************/
@@ -276,21 +283,16 @@ static void nt_open_pipe(char *fname, connection_struct *conn,
 
        DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
 
-       /* See if it is one we want to handle. */
-
-       if (!is_known_pipename(fname)) {
-               reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
-                               ERRDOS, ERRbadpipe);
-               return;
-       }
-
        /* Strip \\ off the name. */
        fname++;
 
-       DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
-
-       status = np_open(req, conn, fname, &fsp);
+       status = open_np_file(req, fname, &fsp);
        if (!NT_STATUS_IS_OK(status)) {
+               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+                       reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
+                                       ERRDOS, ERRbadpipe);
+                       return;
+               }
                reply_nterror(req, status);
                return;
        }
@@ -309,11 +311,10 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
        char *fname = NULL;
        int pnum = -1;
        char *p = NULL;
-       uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
+       uint32 flags = IVAL(req->vwv+3, 1);
        TALLOC_CTX *ctx = talloc_tos();
 
-       srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
-                       smb_buf(req->inbuf), STR_TERMINATE);
+       srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
 
        if (!fname) {
                reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
@@ -381,6 +382,7 @@ static void do_ntcreate_pipe_open(connection_struct *conn,
 void reply_ntcreate_and_X(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
        uint32 flags;
        uint32 access_mask;
@@ -394,13 +396,14 @@ void reply_ntcreate_and_X(struct smb_request *req)
           reply bits separately. */
        uint32 fattr=0;
        SMB_OFF_T file_len = 0;
-       SMB_STRUCT_STAT sbuf;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
+       struct timespec create_timespec;
        struct timespec c_timespec;
        struct timespec a_timespec;
        struct timespec m_timespec;
+       struct timespec write_time_ts;
        NTSTATUS status;
        int oplock_request;
        uint8_t oplock_granted = NO_OPLOCK_RETURN;
@@ -413,29 +416,25 @@ void reply_ntcreate_and_X(struct smb_request *req)
                return;
        }
 
-       flags = IVAL(req->inbuf,smb_ntcreate_Flags);
-       access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
-       file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
-       share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
-       create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
-       create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
-       root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
+       flags = IVAL(req->vwv+3, 1);
+       access_mask = IVAL(req->vwv+7, 1);
+       file_attributes = IVAL(req->vwv+13, 1);
+       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->inbuf,
-                                            smb_ntcreate_AllocationSize);
+       allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
 #ifdef LARGE_SMB_OFF_T
-       allocation_size |= (((uint64_t)IVAL(
-                                    req->inbuf,
-                                    smb_ntcreate_AllocationSize + 4)) << 32);
+       allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
 #endif
 
-       srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
-                       smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
+       srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
+                           STR_TERMINATE, &status);
 
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBntcreateX);
-               return;
+               goto out;
        }
 
        DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
@@ -464,12 +463,10 @@ void reply_ntcreate_and_X(struct smb_request *req)
        if (IS_IPC(conn)) {
                if (lp_nt_pipe_support()) {
                        do_ntcreate_pipe_open(conn, req);
-                       END_PROFILE(SMBntcreateX);
-                       return;
+                       goto out;
                }
                reply_doserror(req, ERRDOS, ERRnoaccess);
-               END_PROFILE(SMBntcreateX);
-               return;
+               goto out;
        }
 
        oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
@@ -478,16 +475,46 @@ void reply_ntcreate_and_X(struct smb_request *req)
                        ? BATCH_OPLOCK : 0;
        }
 
-       status = create_file(conn, req, root_dir_fid, fname,
-                            access_mask, share_access, create_disposition,
-                            create_options, file_attributes, oplock_request,
-                            allocation_size, NULL, NULL, &fsp, &info, &sbuf);
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               0,
+                               NULL,
+                               &smb_fname);
+
+       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;
+       }
+
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               root_dir_fid,                           /* root_dir_fid */
+               smb_fname,                              /* fname */
+               access_mask,                            /* access_mask */
+               share_access,                           /* share_access */
+               create_disposition,                     /* create_disposition*/
+               create_options,                         /* create_options */
+               file_attributes,                        /* file_attributes */
+               oplock_request,                         /* oplock_request */
+               allocation_size,                        /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call, no error. */
-                       END_PROFILE(SMBntcreateX);
-                       return;
+                       goto out;
                }
                if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
                        reply_botherror(req, status, ERRDOS, ERRfilexists);
@@ -495,10 +522,13 @@ void reply_ntcreate_and_X(struct smb_request *req)
                else {
                        reply_nterror(req, status);
                }
-               END_PROFILE(SMBntcreateX);
-               return;
+               goto out;
        }
 
+       /* Ensure we're pointing at the correct stat struct. */
+       TALLOC_FREE(smb_fname);
+       smb_fname = fsp->fsp_name;
+
        /*
         * If the caller set the extended oplock request bit
         * and we granted one (by whatever means) - set the
@@ -524,8 +554,8 @@ void reply_ntcreate_and_X(struct smb_request *req)
                oplock_granted = NO_OPLOCK_RETURN;
        }
 
-       file_len = sbuf.st_size;
-       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
+       file_len = smb_fname->st.st_ex_size;
+       fattr = dos_mode(conn, smb_fname);
        if (fattr == 0) {
                fattr = FILE_ATTRIBUTE_NORMAL;
        }
@@ -557,34 +587,61 @@ void reply_ntcreate_and_X(struct smb_request *req)
        }
        p += 4;
 
+       /* Deal with other possible opens having a modified
+          write time. JRA. */
+       ZERO_STRUCT(write_time_ts);
+       get_file_infos(fsp->file_id, NULL, &write_time_ts);
+       if (!null_timespec(write_time_ts)) {
+               update_stat_ex_mtime(&smb_fname->st, write_time_ts);
+       }
+
        /* Create time. */
-       c_timespec = get_create_timespec(
-               &sbuf,lp_fake_dir_create_times(SNUM(conn)));
-       a_timespec = get_atimespec(&sbuf);
-       m_timespec = get_mtimespec(&sbuf);
+       create_timespec = get_create_timespec(conn, fsp, smb_fname);
+       a_timespec = smb_fname->st.st_ex_atime;
+       m_timespec = smb_fname->st.st_ex_mtime;
+       c_timespec = get_change_timespec(conn, fsp, smb_fname);
 
        if (lp_dos_filetime_resolution(SNUM(conn))) {
-               dos_filetime_timespec(&c_timespec);
+               dos_filetime_timespec(&create_timespec);
                dos_filetime_timespec(&a_timespec);
                dos_filetime_timespec(&m_timespec);
+               dos_filetime_timespec(&c_timespec);
        }
 
-       put_long_date_timespec(p, c_timespec); /* create time. */
+       put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
        p += 8;
-       put_long_date_timespec(p, a_timespec); /* access time */
+       put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* write time */
+       put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* change time */
+       put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
        p += 8;
        SIVAL(p,0,fattr); /* File Attributes. */
        p += 4;
-       SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
+       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
        p += 8;
        SOFF_T(p,0,file_len);
        p += 8;
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
-               SSVAL(p,2,0x7);
+               uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
+               size_t num_names = 0;
+               unsigned int num_streams;
+               struct stream_struct *streams = NULL;
+
+               /* Do we have any EA's ? */
+               status = get_ea_names_from_file(ctx, conn, fsp,
+                               smb_fname->base_name, NULL, &num_names);
+               if (NT_STATUS_IS_OK(status) && num_names) {
+                       file_status &= ~NO_EAS;
+               }
+               status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
+                       &num_streams, &streams);
+               /* There is always one stream, ::$DATA. */
+               if (NT_STATUS_IS_OK(status) && num_streams > 1) {
+                       file_status &= ~NO_SUBSTREAMS;
+               }
+               TALLOC_FREE(streams);
+               SSVAL(p,2,file_status);
        }
        p += 4;
        SCVAL(p,0,fsp->is_directory ? 1 : 0);
@@ -592,8 +649,8 @@ void reply_ntcreate_and_X(struct smb_request *req)
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                uint32 perms = 0;
                p += 25;
-               if (fsp->is_directory
-                   || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
+               if (fsp->is_directory ||
+                   can_write_to_file(conn, smb_fname)) {
                        perms = FILE_GENERIC_ALL;
                } else {
                        perms = FILE_GENERIC_READ|FILE_EXECUTE;
@@ -602,9 +659,10 @@ void reply_ntcreate_and_X(struct smb_request *req)
        }
 
        DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
-                fsp->fnum, fsp->fsp_name));
+               fsp->fnum, smb_fname_str_dbg(smb_fname)));
 
        chain_reply(req);
+ out:
        END_PROFILE(SMBntcreateX);
        return;
 }
@@ -725,23 +783,22 @@ static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
                return status;
        }
 
-       if (psd->owner_sid==0) {
+       if (psd->owner_sid == NULL) {
                security_info_sent &= ~OWNER_SECURITY_INFORMATION;
        }
-       if (psd->group_sid==0) {
+       if (psd->group_sid == NULL) {
                security_info_sent &= ~GROUP_SECURITY_INFORMATION;
        }
-       if (psd->sacl==0) {
-               security_info_sent &= ~SACL_SECURITY_INFORMATION;
-       }
-       if (psd->dacl==0) {
-               security_info_sent &= ~DACL_SECURITY_INFORMATION;
-       }
 
        /* Convert all the generic bits. */
        security_acl_map_generic(psd->dacl, &file_generic_mapping);
        security_acl_map_generic(psd->sacl, &file_generic_mapping);
 
+       if (DEBUGLEVEL >= 10) {
+               DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
+               NDR_PRINT_DEBUG(security_descriptor, psd);
+       }
+
        status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
 
        TALLOC_FREE(psd);
@@ -791,13 +848,13 @@ static void call_nt_transact_create(connection_struct *conn,
                                    char **ppdata, uint32 data_count,
                                    uint32 max_data_count)
 {
+       struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
        char *params = *ppparams;
        char *data = *ppdata;
        /* Breakout the oplock request bits so we can set the reply bits separately. */
        uint32 fattr=0;
        SMB_OFF_T file_len = 0;
-       SMB_STRUCT_STAT sbuf;
        int info = 0;
        files_struct *fsp = NULL;
        char *p = NULL;
@@ -811,9 +868,11 @@ static void call_nt_transact_create(connection_struct *conn,
        struct security_descriptor *sd = NULL;
        uint32 ea_len;
        uint16 root_dir_fid;
+       struct timespec create_timespec;
        struct timespec c_timespec;
        struct timespec a_timespec;
        struct timespec m_timespec;
+       struct timespec write_time_ts;
        struct ea_list *ea_list = NULL;
        NTSTATUS status;
        size_t param_len;
@@ -835,10 +894,10 @@ static void call_nt_transact_create(connection_struct *conn,
                                ppsetup, setup_count,
                                ppparams, parameter_count,
                                ppdata, data_count);
-                       return;
+                       goto out;
                }
                reply_doserror(req, ERRDOS, ERRnoaccess);
-               return;
+               goto out;
        }
 
        /*
@@ -848,7 +907,7 @@ static void call_nt_transact_create(connection_struct *conn,
        if(parameter_count < 54) {
                DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
+               goto out;
        }
 
        flags = IVAL(params,0);
@@ -879,7 +938,7 @@ static void call_nt_transact_create(connection_struct *conn,
                           "%u, data_count = %u\n", (unsigned int)ea_len,
                           (unsigned int)sd_len, (unsigned int)data_count));
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
+               goto out;
        }
 
        if (sd_len) {
@@ -893,7 +952,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                   "unmarshall_sec_desc failed: %s\n",
                                   nt_errstr(status)));
                        reply_nterror(req, status);
-                       return;
+                       goto out;
                }
        }
 
@@ -903,7 +962,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                   "EA's not supported.\n",
                                   (unsigned int)ea_len));
                        reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
-                       return;
+                       goto out;
                }
 
                if (ea_len < 10) {
@@ -911,7 +970,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                  "too small (should be more than 10)\n",
                                  (unsigned int)ea_len ));
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
+                       goto out;
                }
 
                /* We have already checked that ea_len <= data_count here. */
@@ -919,7 +978,7 @@ static void call_nt_transact_create(connection_struct *conn,
                                               ea_len);
                if (ea_list == NULL) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-                       return;
+                       goto out;
                }
        }
 
@@ -928,7 +987,26 @@ static void call_nt_transact_create(connection_struct *conn,
                        STR_TERMINATE, &status);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               return;
+               goto out;
+       }
+
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               0,
+                               NULL,
+                               &smb_fname);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
+                       reply_botherror(req,
+                               NT_STATUS_PATH_NOT_COVERED,
+                               ERRSRV, ERRbadpath);
+                       goto out;
+               }
+               reply_nterror(req, status);
+               goto out;
        }
 
        oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
@@ -937,10 +1015,22 @@ static void call_nt_transact_create(connection_struct *conn,
                        ? BATCH_OPLOCK : 0;
        }
 
-       status = create_file(conn, req, root_dir_fid, fname,
-                            access_mask, share_access, create_disposition,
-                            create_options, file_attributes, oplock_request,
-                            allocation_size, sd, ea_list, &fsp, &info, &sbuf);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               root_dir_fid,                           /* root_dir_fid */
+               smb_fname,                              /* fname */
+               access_mask,                            /* access_mask */
+               share_access,                           /* share_access */
+               create_disposition,                     /* create_disposition*/
+               create_options,                         /* create_options */
+               file_attributes,                        /* file_attributes */
+               oplock_request,                         /* oplock_request */
+               allocation_size,                        /* allocation_size */
+               sd,                                     /* sd */
+               ea_list,                                /* ea_list */
+               &fsp,                                   /* result */
+               &info);                                 /* pinfo */
 
        if(!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -948,9 +1038,13 @@ static void call_nt_transact_create(connection_struct *conn,
                        return;
                }
                reply_openerror(req, status);
-               return;
+               goto out;
        }
 
+       /* Ensure we're pointing at the correct stat struct. */
+       TALLOC_FREE(smb_fname);
+       smb_fname = fsp->fsp_name;
+
        /*
         * If the caller set the extended oplock request bit
         * and we granted one (by whatever means) - set the
@@ -976,8 +1070,8 @@ static void call_nt_transact_create(connection_struct *conn,
                oplock_granted = NO_OPLOCK_RETURN;
        }
 
-       file_len = sbuf.st_size;
-       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
+       file_len = smb_fname->st.st_ex_size;
+       fattr = dos_mode(conn, smb_fname);
        if (fattr == 0) {
                fattr = FILE_ATTRIBUTE_NORMAL;
        }
@@ -992,7 +1086,7 @@ static void call_nt_transact_create(connection_struct *conn,
        params = nttrans_realloc(ppparams, param_len);
        if(params == NULL) {
                reply_doserror(req, ERRDOS, ERRnomem);
-               return;
+               goto out;
        }
 
        p = params;
@@ -1009,29 +1103,38 @@ static void call_nt_transact_create(connection_struct *conn,
        }
        p += 8;
 
+       /* Deal with other possible opens having a modified
+          write time. JRA. */
+       ZERO_STRUCT(write_time_ts);
+       get_file_infos(fsp->file_id, NULL, &write_time_ts);
+       if (!null_timespec(write_time_ts)) {
+               update_stat_ex_mtime(&smb_fname->st, write_time_ts);
+       }
+
        /* Create time. */
-       c_timespec = get_create_timespec(
-               &sbuf,lp_fake_dir_create_times(SNUM(conn)));
-       a_timespec = get_atimespec(&sbuf);
-       m_timespec = get_mtimespec(&sbuf);
+       create_timespec = get_create_timespec(conn, fsp, smb_fname);
+       a_timespec = smb_fname->st.st_ex_atime;
+       m_timespec = smb_fname->st.st_ex_mtime;
+       c_timespec = get_change_timespec(conn, fsp, smb_fname);
 
        if (lp_dos_filetime_resolution(SNUM(conn))) {
-               dos_filetime_timespec(&c_timespec);
+               dos_filetime_timespec(&create_timespec);
                dos_filetime_timespec(&a_timespec);
                dos_filetime_timespec(&m_timespec);
+               dos_filetime_timespec(&c_timespec);
        }
 
-       put_long_date_timespec(p, c_timespec); /* create time. */
+       put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
        p += 8;
-       put_long_date_timespec(p, a_timespec); /* access time */
+       put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* write time */
+       put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
        p += 8;
-       put_long_date_timespec(p, m_timespec); /* change time */
+       put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
        p += 8;
        SIVAL(p,0,fattr); /* File Attributes. */
        p += 4;
-       SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
+       SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
        p += 8;
        SOFF_T(p,0,file_len);
        p += 8;
@@ -1044,8 +1147,8 @@ static void call_nt_transact_create(connection_struct *conn,
        if (flags & EXTENDED_RESPONSE_REQUIRED) {
                uint32 perms = 0;
                p += 25;
-               if (fsp->is_directory
-                   || can_write_to_file(conn, fsp->fsp_name, &sbuf)) {
+               if (fsp->is_directory ||
+                   can_write_to_file(conn, smb_fname)) {
                        perms = FILE_GENERIC_ALL;
                } else {
                        perms = FILE_GENERIC_READ|FILE_EXECUTE;
@@ -1053,11 +1156,12 @@ static void call_nt_transact_create(connection_struct *conn,
                SIVAL(p,0,perms);
        }
 
-       DEBUG(5,("call_nt_transact_create: open name = %s\n", fsp->fsp_name));
+       DEBUG(5,("call_nt_transact_create: open name = %s\n",
+                smb_fname_str_dbg(smb_fname)));
 
        /* Send the required number of replies */
        send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
-
+ out:
        return;
 }
 
@@ -1073,9 +1177,9 @@ 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->mid);
 
        DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
 
@@ -1090,108 +1194,98 @@ void reply_ntcancel(struct smb_request *req)
 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                                connection_struct *conn,
                                struct smb_request *req,
-                               const char *oldname_in,
-                               const char *newname_in,
+                               struct smb_filename *smb_fname_src,
+                               struct smb_filename *smb_fname_dst,
                                uint32 attrs)
 {
-       SMB_STRUCT_STAT sbuf1, sbuf2;
-       char *oldname = NULL;
-       char *newname = NULL;
-       char *last_component_oldname = NULL;
-       char *last_component_newname = NULL;
        files_struct *fsp1,*fsp2;
        uint32 fattr;
        int info;
        SMB_OFF_T ret=-1;
        NTSTATUS status = NT_STATUS_OK;
-
-       ZERO_STRUCT(sbuf1);
-       ZERO_STRUCT(sbuf2);
+       char *parent;
 
        if (!CAN_WRITE(conn)) {
-               return NT_STATUS_MEDIA_WRITE_PROTECTED;
-       }
-
-       status = unix_convert(ctx, conn, oldname_in, False, &oldname,
-                       &last_component_oldname, &sbuf1);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       status = check_name(conn, oldname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               status = NT_STATUS_MEDIA_WRITE_PROTECTED;
+               goto out;
        }
 
         /* Source must already exist. */
-       if (!VALID_STAT(sbuf1)) {
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       if (!VALID_STAT(smb_fname_src->st)) {
+               status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               goto out;
        }
+
        /* Ensure attributes match. */
-       fattr = dos_mode(conn,oldname,&sbuf1);
+       fattr = dos_mode(conn, smb_fname_src);
        if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
-               return NT_STATUS_NO_SUCH_FILE;
-       }
-
-       status = unix_convert(ctx, conn, newname_in, False, &newname,
-                       &last_component_newname, &sbuf2);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       status = check_name(conn, newname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               status = NT_STATUS_NO_SUCH_FILE;
+               goto out;
        }
 
-       /* Disallow if newname already exists. */
-       if (VALID_STAT(sbuf2)) {
-               return NT_STATUS_OBJECT_NAME_COLLISION;
+       /* Disallow if dst file already exists. */
+       if (VALID_STAT(smb_fname_dst->st)) {
+               status = NT_STATUS_OBJECT_NAME_COLLISION;
+               goto out;
        }
 
        /* No links from a directory. */
-       if (S_ISDIR(sbuf1.st_mode)) {
-               return NT_STATUS_FILE_IS_A_DIRECTORY;
-       }
-
-       /* Ensure this is within the share. */
-       status = check_reduced_name(conn, oldname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
+               status = NT_STATUS_FILE_IS_A_DIRECTORY;
+               goto out;
        }
 
        DEBUG(10,("copy_internals: doing file copy %s to %s\n",
-                               oldname, newname));
-
-        status = open_file_ntcreate(conn, req, oldname, &sbuf1,
-                       FILE_READ_DATA, /* Read-only. */
-                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                       FILE_OPEN,
-                       0, /* No create options. */
-                       FILE_ATTRIBUTE_NORMAL,
-                       NO_OPLOCK,
-                       &info, &fsp1);
+                 smb_fname_str_dbg(smb_fname_src),
+                 smb_fname_str_dbg(smb_fname_dst)));
+
+        status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               smb_fname_src,                          /* fname */
+               FILE_READ_DATA,                         /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
+               NO_OPLOCK,                              /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp1,                                  /* result */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-        status = open_file_ntcreate(conn, req, newname, &sbuf2,
-                       FILE_WRITE_DATA, /* Read-only. */
-                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                       FILE_CREATE,
-                       0, /* No create options. */
-                       fattr,
-                       NO_OPLOCK,
-                       &info, &fsp2);
+               goto out;
+       }
+
+        status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               smb_fname_dst,                          /* fname */
+               FILE_WRITE_DATA,                        /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               FILE_CREATE,                            /* create_disposition*/
+               0,                                      /* create_options */
+               fattr,                                  /* file_attributes */
+               NO_OPLOCK,                              /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp2,                                  /* result */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                close_file(NULL, fsp1, ERROR_CLOSE);
-               return status;
+               goto out;
        }
 
-       if (sbuf1.st_size) {
-               ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
+       if (smb_fname_src->st.st_ex_size) {
+               ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
        }
 
        /*
@@ -1203,24 +1297,32 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
        close_file(NULL, fsp1, NORMAL_CLOSE);
 
        /* Ensure the modtime is set correctly on the destination file. */
-       set_close_write_time(fsp2, get_mtimespec(&sbuf1));
+       set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
 
        status = close_file(NULL, fsp2, NORMAL_CLOSE);
 
        /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
           creates the file. This isn't the correct thing to do in the copy
           case. JRA */
-       file_set_dosmode(conn, newname, fattr, &sbuf2,
-                        parent_dirname(newname),false);
-
-       if (ret < (SMB_OFF_T)sbuf1.st_size) {
-               return NT_STATUS_DISK_FULL;
+       if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
+                           NULL)) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
        }
+       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) {
+               status = NT_STATUS_DISK_FULL;
+               goto out;
+       }
+ out:
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
-                       nt_errstr(status), oldname, newname));
+                       nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
+                       smb_fname_str_dbg(smb_fname_dst)));
        }
+
        return status;
 }
 
@@ -1231,13 +1333,17 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
 void reply_ntrename(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smb_filename *smb_fname_old = NULL;
+       struct smb_filename *smb_fname_new = NULL;
        char *oldname = NULL;
        char *newname = NULL;
-       char *p;
+       const char *p;
        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;
        TALLOC_CTX *ctx = talloc_tos();
 
@@ -1245,95 +1351,105 @@ void reply_ntrename(struct smb_request *req)
 
        if (req->wct < 4) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
-       attrs = SVAL(req->inbuf,smb_vwv0);
-       rename_type = SVAL(req->inbuf,smb_vwv1);
+       attrs = SVAL(req->vwv+0, 0);
+       rename_type = SVAL(req->vwv+1, 0);
 
-       p = smb_buf(req->inbuf) + 1;
-       p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
-                                  0, STR_TERMINATE, &status,
-                                  &src_has_wcard);
+       p = (const char *)req->buf + 1;
+       p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
+                                      &status, &src_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
-       }
-
-       if( is_ntfs_stream_name(oldname)) {
-               /* Can't rename a stream. */
-               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
        if (ms_has_wild(oldname)) {
                reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
        p++;
-       p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
-                                  0, STR_TERMINATE, &status,
-                                  &dest_has_wcard);
+       p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
+                                      &status, &dest_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
-                               oldname,
-                               &oldname);
+       /* The newname must begin with a ':' if the oldname contains a ':'. */
+       if (strchr_m(oldname, ':') && (newname[0] != ':')) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               goto out;
+       }
+
+       /*
+        * If this is a rename operation, allow wildcards and save the
+        * destination's last component.
+        */
+       if (rename_type == RENAME_FLAG_RENAME) {
+               ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
+               ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
+       }
+
+       /* rename_internals() calls unix_convert(), so don't call it here. */
+       status = filename_convert(ctx, conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 oldname,
+                                 ucf_flags_src,
+                                 NULL,
+                                 &smb_fname_old);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
-                       reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
+               if (NT_STATUS_EQUAL(status,
+                                   NT_STATUS_PATH_NOT_COVERED)) {
+                       reply_botherror(req,
+                                       NT_STATUS_PATH_NOT_COVERED,
                                        ERRSRV, ERRbadpath);
-                       END_PROFILE(SMBntrename);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
-                               newname,
-                               &newname);
+       status = filename_convert(ctx, conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 newname,
+                                 ucf_flags_dst,
+                                 &dest_has_wcard,
+                                 &smb_fname_new);
        if (!NT_STATUS_IS_OK(status)) {
-               if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
-                       reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
+               if (NT_STATUS_EQUAL(status,
+                                   NT_STATUS_PATH_NOT_COVERED)) {
+                       reply_botherror(req,
+                                       NT_STATUS_PATH_NOT_COVERED,
                                        ERRSRV, ERRbadpath);
-                       END_PROFILE(SMBntrename);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
-       DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
+       DEBUG(3,("reply_ntrename: %s -> %s\n",
+                smb_fname_str_dbg(smb_fname_old),
+                smb_fname_str_dbg(smb_fname_new)));
 
        switch(rename_type) {
                case RENAME_FLAG_RENAME:
-                       status = rename_internals(ctx, conn, req, oldname,
-                                       newname, attrs, False, src_has_wcard,
-                                       dest_has_wcard, DELETE_ACCESS);
+                       status = rename_internals(ctx, conn, req,
+                                                 smb_fname_old, smb_fname_new,
+                                                 attrs, False, src_has_wcard,
+                                                 dest_has_wcard,
+                                                 DELETE_ACCESS);
                        break;
                case RENAME_FLAG_HARD_LINK:
                        if (src_has_wcard || dest_has_wcard) {
                                /* No wildcards. */
                                status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
                        } else {
-                               status = hardlink_internals(ctx,
-                                               conn,
-                                               oldname,
-                                               newname);
+                               status = hardlink_internals(ctx, conn,
+                                                           smb_fname_old,
+                                                           smb_fname_new);
                        }
                        break;
                case RENAME_FLAG_COPY:
@@ -1341,8 +1457,10 @@ void reply_ntrename(struct smb_request *req)
                                /* No wildcards. */
                                status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
                        } else {
-                               status = copy_internals(ctx, conn, req, oldname,
-                                                       newname, attrs);
+                               status = copy_internals(ctx, conn, req,
+                                                       smb_fname_old,
+                                                       smb_fname_new,
+                                                       attrs);
                        }
                        break;
                case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
@@ -1356,17 +1474,15 @@ void reply_ntrename(struct smb_request *req)
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call. */
-                       END_PROFILE(SMBntrename);
-                       return;
+                       goto out;
                }
 
                reply_nterror(req, status);
-               END_PROFILE(SMBntrename);
-               return;
+               goto out;
        }
 
        reply_outbuf(req, 0, 0);
-
+ out:
        END_PROFILE(SMBntrename);
        return;
 }
@@ -1376,6 +1492,13 @@ void reply_ntrename(struct smb_request *req)
  don't allow a directory to be opened.
 ****************************************************************************/
 
+static void smbd_smb1_notify_reply(struct smb_request *req,
+                                  NTSTATUS error_code,
+                                  uint8_t *buf, size_t len)
+{
+       send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
+}
+
 static void call_nt_transact_notify_change(connection_struct *conn,
                                           struct smb_request *req,
                                           uint16 **ppsetup,
@@ -1418,7 +1541,7 @@ static void call_nt_transact_notify_change(connection_struct *conn,
 
                DEBUG(3,("call_nt_transact_notify_change: notify change "
                         "called on %s, filter = %s, recursive = %d\n",
-                        fsp->fsp_name, filter_string, recursive));
+                        fsp_str_dbg(fsp), filter_string, recursive));
 
                TALLOC_FREE(filter_string);
        }
@@ -1451,7 +1574,11 @@ static void call_nt_transact_notify_change(connection_struct *conn,
                 * here.
                 */
 
-               change_notify_reply(fsp->conn, req->inbuf, max_param_count, fsp->notify);
+               change_notify_reply(fsp->conn, req,
+                                   NT_STATUS_OK,
+                                   max_param_count,
+                                   fsp->notify,
+                                   smbd_smb1_notify_reply);
 
                /*
                 * change_notify_reply() above has independently sent its
@@ -1467,7 +1594,8 @@ static void call_nt_transact_notify_change(connection_struct *conn,
        status = change_notify_add_request(req,
                        max_param_count,
                        filter,
-                       recursive, fsp);
+                       recursive, fsp,
+                       smbd_smb1_notify_reply);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
        }
@@ -1516,7 +1644,7 @@ static void call_nt_transact_rename(connection_struct *conn,
        send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
 
        DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
-                fsp->fsp_name, new_name));
+                fsp_str_dbg(fsp), new_name));
 
        return;
 }
@@ -1574,8 +1702,9 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
 
        security_info_wanted = IVAL(params,4);
 
-       DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
-                       (unsigned int)security_info_wanted ));
+       DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
+                "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
+                (unsigned int)security_info_wanted));
 
        params = nttrans_realloc(ppparams, 4);
        if(params == NULL) {
@@ -1593,16 +1722,30 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
                status = SMB_VFS_FGET_NT_ACL(
                        fsp, security_info_wanted, &psd);
        }
-
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
        }
 
-       sd_size = ndr_size_security_descriptor(psd, 0);
+       /* 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));
 
+       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);
 
        if (max_data_count < sd_size) {
@@ -1673,8 +1816,8 @@ static void call_nt_transact_set_security_desc(connection_struct *conn,
 
        security_info_sent = IVAL(params,4);
 
-       DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
-               (unsigned int)security_info_sent ));
+       DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
+                fsp_str_dbg(fsp), (unsigned int)security_info_sent));
 
        if (data_count == 0) {
                reply_doserror(req, ERRDOS, ERRnoaccess);
@@ -1709,7 +1852,6 @@ static void call_nt_transact_ioctl(connection_struct *conn,
        files_struct *fsp;
        uint8 isFSctl;
        uint8 compfilter;
-       static bool logged_message;
        char *pdata = *ppdata;
 
        if (setup_count != 8) {
@@ -1731,6 +1873,8 @@ static void call_nt_transact_ioctl(connection_struct *conn,
           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
@@ -1761,6 +1905,8 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                        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);
@@ -1895,7 +2041,7 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                cur_pdata+=12;
 
                DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
-                       shadow_data->num_volumes,fsp->fsp_name));
+                         shadow_data->num_volumes, fsp_str_dbg(fsp)));
                if (labels && shadow_data->labels) {
                        for (i=0;i<shadow_data->num_volumes;i++) {
                                srvstr_push(pdata, req->flags2,
@@ -1969,8 +2115,8 @@ static void call_nt_transact_ioctl(connection_struct *conn,
                return;
        }
        default:
-               if (!logged_message) {
-                       logged_message = True; /* Only print this once... */
+               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));
                }
@@ -2121,7 +2267,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, 0);
+                                       &tmp_list->quotas->sid, NULL, 0);
                                entry_len = 40 + sid_len;
 
                                /* nextoffset entry 4 bytes */
@@ -2397,6 +2543,9 @@ static void handle_nttrans(connection_struct *conn,
                SSVAL(req->inbuf,smb_flg2,req->flags2);
        }
 
+
+       SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
+
        /* Now we must call the relevant NT_TRANS function */
        switch(state->call) {
                case NT_TRANSACT_CREATE:
@@ -2530,8 +2679,6 @@ void reply_nttrans(struct smb_request *req)
        uint16 function_code;
        NTSTATUS result;
        struct trans_state *state;
-       uint32_t size;
-       uint32_t av_size;
 
        START_PROFILE(SMBnttrans);
 
@@ -2541,13 +2688,11 @@ void reply_nttrans(struct smb_request *req)
                return;
        }
 
-       size = smb_len(req->inbuf) + 4;
-       av_size = smb_len(req->inbuf);
-       pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
-       psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
-       dscnt = IVAL(req->inbuf,smb_nt_DataCount);
-       dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
-       function_code = SVAL(req->inbuf, smb_nt_Function);
+       pscnt = IVAL(req->vwv+9, 1);
+       psoff = IVAL(req->vwv+11, 1);
+       dscnt = IVAL(req->vwv+13, 1);
+       dsoff = IVAL(req->vwv+15, 1);
+       function_code = SVAL(req->vwv+18, 0);
 
        if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
                reply_doserror(req, ERRSRV, ERRaccess);
@@ -2573,15 +2718,15 @@ void reply_nttrans(struct smb_request *req)
 
        state->mid = req->mid;
        state->vuid = req->vuid;
-       state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
+       state->total_data = IVAL(req->vwv+3, 1);
        state->data = NULL;
-       state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
+       state->total_param = IVAL(req->vwv+1, 1);
        state->param = NULL;
-       state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
-       state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
+       state->max_data_return = IVAL(req->vwv+7, 1);
+       state->max_param_return = IVAL(req->vwv+5, 1);
 
        /* setup count is in *words* */
-       state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
+       state->setup_count = 2*CVAL(req->vwv+17, 1);
        state->setup = NULL;
        state->call = function_code;
 
@@ -2619,6 +2764,12 @@ void reply_nttrans(struct smb_request *req)
                goto bad_param;
 
        if (state->total_data)  {
+
+               if (trans_oob(state->total_data, 0, dscnt)
+                   || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
+                       goto bad_param;
+               }
+
                /* Can't use talloc here, the core routines do realloc on the
                 * params and data. */
                if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
@@ -2630,21 +2781,16 @@ void reply_nttrans(struct smb_request *req)
                        return;
                }
 
-               if (dscnt > state->total_data ||
-                               dsoff+dscnt < dsoff) {
-                       goto bad_param;
-               }
-
-               if (dsoff > av_size ||
-                               dscnt > av_size ||
-                               dsoff+dscnt > av_size) {
-                       goto bad_param;
-               }
-
                memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
        }
 
        if (state->total_param) {
+
+               if (trans_oob(state->total_param, 0, pscnt)
+                   || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
+                       goto bad_param;
+               }
+
                /* Can't use talloc here, the core routines do realloc on the
                 * params and data. */
                if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
@@ -2657,17 +2803,6 @@ void reply_nttrans(struct smb_request *req)
                        return;
                }
 
-               if (pscnt > state->total_param ||
-                               psoff+pscnt < psoff) {
-                       goto bad_param;
-               }
-
-               if (psoff > av_size ||
-                               pscnt > av_size ||
-                               psoff+pscnt > av_size) {
-                       goto bad_param;
-               }
-
                memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
        }
 
@@ -2677,6 +2812,19 @@ void reply_nttrans(struct smb_request *req)
        if(state->setup_count > 0) {
                DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
                          state->setup_count));
+
+               /*
+                * No overflow possible here, state->setup_count is an
+                * unsigned int, being filled by a single byte from
+                * CVAL(req->vwv+13, 0) above. The cast in the comparison
+                * below is not necessary, it's here to clarify things. The
+                * validity of req->vwv and req->wct has been checked in
+                * init_smb_request already.
+                */
+               if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
+                       goto bad_param;
+               }
+
                state->setup = (uint16 *)TALLOC(state, state->setup_count);
                if (state->setup == NULL) {
                        DEBUG(0,("reply_nttrans : Out of memory\n"));
@@ -2688,16 +2836,7 @@ void reply_nttrans(struct smb_request *req)
                        return;
                }
 
-               if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
-                   (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
-                       goto bad_param;
-               }
-               if (smb_nt_SetupStart + state->setup_count > size) {
-                       goto bad_param;
-               }
-
-               memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
-                       state->setup_count);
+               memcpy(state->setup, req->vwv+19, state->setup_count);
                dump_data(10, (uint8 *)state->setup, state->setup_count);
        }
 
@@ -2740,8 +2879,6 @@ void reply_nttranss(struct smb_request *req)
        connection_struct *conn = req->conn;
        uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
        struct trans_state *state;
-       uint32_t av_size;
-       uint32_t size;
 
        START_PROFILE(SMBnttranss);
 
@@ -2768,25 +2905,20 @@ void reply_nttranss(struct smb_request *req)
 
        /* Revise state->total_param and state->total_data in case they have
           changed downwards */
-       if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
-           < state->total_param) {
-               state->total_param = IVAL(req->inbuf,
-                                         smb_nts_TotalParameterCount);
+       if (IVAL(req->vwv+1, 1) < state->total_param) {
+               state->total_param = IVAL(req->vwv+1, 1);
        }
-       if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
-               state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
+       if (IVAL(req->vwv+3, 1) < state->total_data) {
+               state->total_data = IVAL(req->vwv+3, 1);
        }
 
-       size = smb_len(req->inbuf) + 4;
-       av_size = smb_len(req->inbuf);
-
-       pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
-       poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
-       pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
+       pcnt = IVAL(req->vwv+5, 1);
+       poff = IVAL(req->vwv+7, 1);
+       pdisp = IVAL(req->vwv+9, 1);
 
-       dcnt = IVAL(req->inbuf, smb_nts_DataCount);
-       ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
-       doff = IVAL(req->inbuf, smb_nts_DataOffset);
+       dcnt = IVAL(req->vwv+11, 1);
+       doff = IVAL(req->vwv+13, 1);
+       ddisp = IVAL(req->vwv+15, 1);
 
        state->received_param += pcnt;
        state->received_data += dcnt;
@@ -2796,41 +2928,19 @@ void reply_nttranss(struct smb_request *req)
                goto bad_param;
 
        if (pcnt) {
-               if (pdisp > state->total_param ||
-                               pcnt > state->total_param ||
-                               pdisp+pcnt > state->total_param ||
-                               pdisp+pcnt < pdisp) {
+               if (trans_oob(state->total_param, pdisp, pcnt)
+                   || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
                        goto bad_param;
                }
-
-               if (poff > av_size ||
-                               pcnt > av_size ||
-                               poff+pcnt > av_size ||
-                               poff+pcnt < poff) {
-                       goto bad_param;
-               }
-
-               memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
-                      pcnt);
+               memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
        }
 
        if (dcnt) {
-               if (ddisp > state->total_data ||
-                               dcnt > state->total_data ||
-                               ddisp+dcnt > state->total_data ||
-                               ddisp+dcnt < ddisp) {
+               if (trans_oob(state->total_data, ddisp, dcnt)
+                   || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
                        goto bad_param;
                }
-
-               if (ddisp > av_size ||
-                               dcnt > av_size ||
-                               ddisp+dcnt > av_size ||
-                               ddisp+dcnt < ddisp) {
-                       goto bad_param;
-               }
-
-               memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
-                      dcnt);
+               memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
        }
 
        if ((state->received_param < state->total_param) ||
@@ -2839,12 +2949,6 @@ void reply_nttranss(struct smb_request *req)
                return;
        }
 
-       /*
-        * construct_reply_common will copy smb_com from inbuf to
-        * outbuf. SMBnttranss is wrong here.
-        */
-       SCVAL(req->inbuf,smb_com,SMBnttrans);
-
        handle_nttrans(conn, state, req);
 
        DLIST_REMOVE(conn->pending_trans, state);