s3 file_access: Convert some more functions over to use smb_filneame
[ira/wip.git] / source3 / smbd / nttrans.c
index 24a14a8c1b8f1f363fcc3804fdbba90915e05fe6..04767bf5590d8c5ca574b053a06812beb39cf284 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
@@ -230,7 +232,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.");
                }
 
@@ -259,14 +263,70 @@ void send_nt_replies(connection_struct *conn,
  An NTFS file name is <path>.<extention>:<stream name>:<stream type>
  $DATA can be used as both a stream name and a stream type. A missing stream
  name or type implies $DATA.
+
+ Both Windows stream names and POSIX files can contain the ':' character.
+ This function first checks for the existence of a colon in the last component
+ of the given name.  If the name contains a colon we differentiate between a
+ stream and POSIX file by checking if the latter exists through a POSIX stat.
+
+ Function assumes we've already chdir() to the "root" directory of fname.
 ****************************************************************************/
 
 bool is_ntfs_stream_name(const char *fname)
 {
+       const char *lastcomp;
+       SMB_STRUCT_STAT sbuf;
+
+       /* If all pathnames are treated as POSIX we ignore streams. */
        if (lp_posix_pathnames()) {
-               return False;
+               return false;
        }
-       return (strchr_m(fname, ':') != NULL) ? True : False;
+
+       /* Find the last component of the name. */
+       if ((lastcomp = strrchr_m(fname, '/')) != NULL)
+               ++lastcomp;
+       else
+               lastcomp = fname;
+
+       /* If there is no colon in the last component, it's not a stream. */
+       if (strchr_m(lastcomp, ':') == NULL)
+               return false;
+
+       /*
+        * If file already exists on disk, it's not a stream. The stat must
+        * bypass the vfs layer so streams modules don't intefere.
+        */
+       if (sys_stat(fname, &sbuf) == 0) {
+               DEBUG(5, ("is_ntfs_stream_name: file %s contains a ':' but is "
+                       "not a stream\n", fname));
+               return false;
+       }
+
+       return true;
+}
+
+/****************************************************************************
+ Simple check to determine if the filename is a stream.
+ ***************************************************************************/
+bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
+{
+       if (lp_posix_pathnames()) {
+               return false;
+       }
+
+       return smb_fname->stream_name;
+}
+
+/****************************************************************************
+ Returns true if the filename's stream == "::$DATA"
+ ***************************************************************************/
+bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
+{
+       if (!is_ntfs_stream_smb_fname(smb_fname)) {
+               return false;
+       }
+
+       return StrCaseCmp(smb_fname->stream_name, "::$DATA") == 0;
 }
 
 /****************************************************************************
@@ -284,7 +344,7 @@ static void nt_open_pipe(char *fname, connection_struct *conn,
        /* Strip \\ off the name. */
        fname++;
 
-       status = np_open(req, 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,
@@ -380,6 +440,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;
@@ -393,7 +454,6 @@ 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;
@@ -430,8 +490,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
 
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBntcreateX);
-               return;
+               goto out;
        }
 
        DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
@@ -460,12 +519,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;
@@ -474,12 +531,29 @@ void reply_ntcreate_and_X(struct smb_request *req)
                        ? BATCH_OPLOCK : 0;
        }
 
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               &smb_fname,
+                               &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 */
-               fname,                                  /* fname */
-               CFF_DOS_PATH,                           /* create_file_flags */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_access,                           /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -490,14 +564,12 @@ void reply_ntcreate_and_X(struct smb_request *req)
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               &info,                                  /* pinfo */
-               &sbuf);                                 /* psbuf */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call, no error. */
-                       END_PROFILE(SMBntcreateX);
-                       return;
+                       goto out;
                }
                if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
                        reply_botherror(req, status, ERRDOS, ERRfilexists);
@@ -505,8 +577,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
                else {
                        reply_nterror(req, status);
                }
-               END_PROFILE(SMBntcreateX);
-               return;
+               goto out;
        }
 
        /*
@@ -534,8 +605,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,fsp->fsp_name,&smb_fname->st);
        if (fattr == 0) {
                fattr = FILE_ATTRIBUTE_NORMAL;
        }
@@ -568,10 +639,9 @@ void reply_ntcreate_and_X(struct smb_request *req)
        p += 4;
 
        /* 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);
+       c_timespec = smb_fname->st.st_ex_btime;
+       a_timespec = smb_fname->st.st_ex_atime;
+       m_timespec = smb_fname->st.st_ex_mtime;
 
        if (lp_dos_filetime_resolution(SNUM(conn))) {
                dos_filetime_timespec(&c_timespec);
@@ -589,7 +659,7 @@ void reply_ntcreate_and_X(struct smb_request *req)
        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;
@@ -602,8 +672,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;
@@ -615,6 +685,8 @@ void reply_ntcreate_and_X(struct smb_request *req)
                 fsp->fnum, fsp->fsp_name));
 
        chain_reply(req);
+ out:
+       TALLOC_FREE(smb_fname);
        END_PROFILE(SMBntcreateX);
        return;
 }
@@ -800,13 +872,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;
@@ -844,10 +916,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;
        }
 
        /*
@@ -857,7 +929,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);
@@ -888,7 +960,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) {
@@ -902,7 +974,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;
                }
        }
 
@@ -912,7 +984,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) {
@@ -920,7 +992,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. */
@@ -928,7 +1000,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;
                }
        }
 
@@ -937,7 +1009,25 @@ 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,
+                               &smb_fname,
+                               &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;
@@ -950,8 +1040,7 @@ static void call_nt_transact_create(connection_struct *conn,
                conn,                                   /* conn */
                req,                                    /* req */
                root_dir_fid,                           /* root_dir_fid */
-               fname,                                  /* fname */
-               CFF_DOS_PATH,                           /* create_file_flags */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_access,                           /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -962,8 +1051,7 @@ static void call_nt_transact_create(connection_struct *conn,
                sd,                                     /* sd */
                ea_list,                                /* ea_list */
                &fsp,                                   /* result */
-               &info,                                  /* pinfo */
-               &sbuf);                                 /* psbuf */
+               &info);                                 /* pinfo */
 
        if(!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -971,7 +1059,7 @@ static void call_nt_transact_create(connection_struct *conn,
                        return;
                }
                reply_openerror(req, status);
-               return;
+               goto out;
        }
 
        /*
@@ -999,8 +1087,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, fsp->fsp_name, &smb_fname->st);
        if (fattr == 0) {
                fattr = FILE_ATTRIBUTE_NORMAL;
        }
@@ -1015,7 +1103,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;
@@ -1033,10 +1121,9 @@ static void call_nt_transact_create(connection_struct *conn,
        p += 8;
 
        /* 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);
+       c_timespec = smb_fname->st.st_ex_btime;
+       a_timespec = smb_fname->st.st_ex_atime;
+       m_timespec = smb_fname->st.st_ex_mtime;
 
        if (lp_dos_filetime_resolution(SNUM(conn))) {
                dos_filetime_timespec(&c_timespec);
@@ -1054,7 +1141,7 @@ static void call_nt_transact_create(connection_struct *conn,
        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;
@@ -1067,8 +1154,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;
@@ -1080,7 +1167,8 @@ static void call_nt_transact_create(connection_struct *conn,
 
        /* Send the required number of replies */
        send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
-
+ out:
+       TALLOC_FREE(smb_fname);
        return;
 }
 
@@ -1096,9 +1184,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));
 
@@ -1113,85 +1201,63 @@ 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 = NT_STATUS_MEDIA_WRITE_PROTECTED;
+               goto out;
        }
 
-       status = unix_convert(ctx, conn, oldname_in, False, &oldname,
-                       &last_component_oldname, &sbuf1);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+        /* Source must already exist. */
+       if (!VALID_STAT(smb_fname_src->st)) {
+               status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               goto out;
        }
 
-       status = check_name(conn, oldname);
+       status = get_full_smb_filename(ctx, smb_fname_src, &oldname);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
        }
 
-        /* Source must already exist. */
-       if (!VALID_STAT(sbuf1)) {
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
-       }
        /* Ensure attributes match. */
-       fattr = dos_mode(conn,oldname,&sbuf1);
+       fattr = dos_mode(conn, oldname, &smb_fname_src->st);
        if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
-               return NT_STATUS_NO_SUCH_FILE;
-       }
-
-       status = unix_convert(ctx, conn, newname_in, False, &newname,
-                       &last_component_newname, &sbuf2);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       status = check_name(conn, newname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               status = NT_STATUS_NO_SUCH_FILE;
+               goto out;
        }
 
-       /* Disallow if newname already exists. */
-       if (VALID_STAT(sbuf2)) {
-               return NT_STATUS_OBJECT_NAME_COLLISION;
+       /* Disallow if dst file already exists. */
+       if (VALID_STAT(smb_fname_dst->st)) {
+               status = NT_STATUS_OBJECT_NAME_COLLISION;
+               goto out;
        }
 
        /* No links from a directory. */
-       if (S_ISDIR(sbuf1.st_mode)) {
-               return NT_STATUS_FILE_IS_A_DIRECTORY;
-       }
-
-       /* Ensure this is within the share. */
-       status = check_reduced_name(conn, oldname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
+               status = NT_STATUS_FILE_IS_A_DIRECTORY;
+               goto out;
        }
 
        DEBUG(10,("copy_internals: doing file copy %s to %s\n",
-                               oldname, newname));
+                 smb_fname_str_dbg(smb_fname_src),
+                 smb_fname_str_dbg(smb_fname_dst)));
 
         status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               oldname,                                /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname_src,                          /* fname */
                FILE_READ_DATA,                         /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
@@ -1203,19 +1269,17 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp1,                                  /* result */
-               &info,                                  /* pinfo */
-               &sbuf1);                                /* psbuf */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
        }
 
         status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               newname,                                /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname_dst,                          /* fname */
                FILE_WRITE_DATA,                        /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
@@ -1227,16 +1291,15 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp2,                                  /* result */
-               &info,                                  /* pinfo */
-               &sbuf2);                                /* psbuf */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                close_file(NULL, fsp1, ERROR_CLOSE);
-               return status;
+               goto out;
        }
 
-       if (sbuf1.st_size) {
-               ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
+       if (smb_fname_src->st.st_ex_size) {
+               ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
        }
 
        /*
@@ -1248,24 +1311,40 @@ 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);
 
+       status = get_full_smb_filename(ctx, smb_fname_dst, &newname);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
        /* 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(), newname, &parent, NULL)) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
        }
+       file_set_dosmode(conn, newname, fattr, &smb_fname_dst->st, 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)));
        }
+
+       TALLOC_FREE(oldname);
+       TALLOC_FREE(newname);
+
        return status;
 }
 
@@ -1276,6 +1355,8 @@ static NTSTATUS copy_internals(TALLOC_CTX *ctx,
 void reply_ntrename(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smb_filename *smb_fname_old = NULL;
+       struct smb_filename *smb_fname_new = NULL;
        char *oldname = NULL;
        char *newname = NULL;
        const char *p;
@@ -1306,13 +1387,6 @@ void reply_ntrename(struct smb_request *req)
                return;
        }
 
-       if( is_ntfs_stream_name(oldname)) {
-               /* Can't rename a stream. */
-               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
-               END_PROFILE(SMBntrename);
-               return;
-       }
-
        if (ms_has_wild(oldname)) {
                reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
                END_PROFILE(SMBntrename);
@@ -1328,9 +1402,10 @@ void reply_ntrename(struct smb_request *req)
                return;
        }
 
-       status = resolve_dfspath(ctx, conn,
+       status = filename_convert(ctx, conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                oldname,
+                               &smb_fname_old,
                                &oldname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
@@ -1344,9 +1419,10 @@ void reply_ntrename(struct smb_request *req)
                return;
        }
 
-       status = resolve_dfspath(ctx, conn,
+       status = filename_convert(ctx, conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                newname,
+                               &smb_fname_new,
                                &newname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
@@ -1360,6 +1436,13 @@ void reply_ntrename(struct smb_request *req)
                return;
        }
 
+       /* The new name must begin with a ':' if the old name is a stream. */
+       if (is_ntfs_stream_name(oldname) && (newname[0] != ':')) {
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               END_PROFILE(SMBntrename);
+               return;
+       }
+
        DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
 
        switch(rename_type) {
@@ -1375,8 +1458,8 @@ void reply_ntrename(struct smb_request *req)
                        } else {
                                status = hardlink_internals(ctx,
                                                conn,
-                                               oldname,
-                                               newname);
+                                               smb_fname_old,
+                                               smb_fname_new);
                        }
                        break;
                case RENAME_FLAG_COPY:
@@ -1384,8 +1467,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:
@@ -1651,7 +1736,7 @@ static void call_nt_transact_query_security_desc(connection_struct *conn,
            security_info_wanted & DACL_SECURITY_INFORMATION)
                psd->type |= SEC_DESC_DACL_PRESENT;
 
-       sd_size = ndr_size_security_descriptor(psd, 0);
+       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));
 
@@ -1766,7 +1851,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) {
@@ -1788,6 +1872,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
@@ -1818,6 +1904,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);
@@ -2026,8 +2114,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));
                }
@@ -2178,7 +2266,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 */
@@ -2454,6 +2542,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: