I found lots of places where we assume error will be set when calling
[samba.git] / source3 / smbd / reply.c
index e2316ef12008fce523c00dcadfdbc55aad4ef193..d32d998da83821f213850713fc450ab8b803ed4b 100644 (file)
@@ -32,7 +32,6 @@ extern int max_recv;
 unsigned int smb_echo_count = 0;
 extern uint32 global_client_caps;
 
-extern struct current_user current_user;
 extern bool global_encrypted_passwords_negotiated;
 
 /****************************************************************************
@@ -167,6 +166,7 @@ static NTSTATUS check_path_syntax_internal(char *path,
        }
 
        *d = '\0';
+
        return ret;
 }
 
@@ -326,13 +326,13 @@ size_t srvstr_get_path(TALLOC_CTX *ctx,
 ****************************************************************************/
 
 bool check_fsp_open(connection_struct *conn, struct smb_request *req,
-              files_struct *fsp, struct current_user *user)
+                   files_struct *fsp)
 {
        if (!(fsp) || !(conn)) {
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return False;
        }
-       if (((conn) != (fsp)->conn) || user->vuid != (fsp)->vuid) {
+       if (((conn) != (fsp)->conn) || req->vuid != (fsp)->vuid) {
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return False;
        }
@@ -345,9 +345,9 @@ bool check_fsp_open(connection_struct *conn, struct smb_request *req,
 ****************************************************************************/
 
 bool check_fsp(connection_struct *conn, struct smb_request *req,
-              files_struct *fsp, struct current_user *user)
+              files_struct *fsp)
 {
-       if (!check_fsp_open(conn, req, fsp, user)) {
+       if (!check_fsp_open(conn, req, fsp)) {
                return False;
        }
        if ((fsp)->is_directory) {
@@ -362,15 +362,46 @@ bool check_fsp(connection_struct *conn, struct smb_request *req,
        return True;
 }
 
+/****************************************************************************
+ Check if we have a correct fsp pointing to a quota fake file. Replacement for
+ the CHECK_NTQUOTA_HANDLE_OK macro.
+****************************************************************************/
+
+bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
+                             files_struct *fsp)
+{
+       if (!check_fsp_open(conn, req, fsp)) {
+               return false;
+       }
+
+       if (fsp->is_directory) {
+               return false;
+       }
+
+       if (fsp->fake_file_handle == NULL) {
+               return false;
+       }
+
+       if (fsp->fake_file_handle->type != FAKE_FILE_TYPE_QUOTA) {
+               return false;
+       }
+
+       if (fsp->fake_file_handle->private_data == NULL) {
+               return false;
+       }
+
+       return true;
+}
+
 /****************************************************************************
  Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
 ****************************************************************************/
 
 bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
-                     files_struct *fsp, struct current_user *user)
+                     files_struct *fsp)
 {
        if ((fsp) && (conn) && ((conn)==(fsp)->conn)
-           && (current_user.vuid==(fsp)->vuid)) {
+           && (req->vuid == (fsp)->vuid)) {
                return True;
        }
 
@@ -1041,6 +1072,7 @@ void reply_getatr(struct smb_request *req)
 
 void reply_setatr(struct smb_request *req)
 {
+       struct timespec ts[2];
        connection_struct *conn = req->conn;
        char *fname = NULL;
        int mode;
@@ -1052,6 +1084,8 @@ void reply_setatr(struct smb_request *req)
 
        START_PROFILE(SMBsetatr);
 
+       ZERO_STRUCT(ts);
+
        if (req->wct < 2) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
@@ -1109,6 +1143,15 @@ void reply_setatr(struct smb_request *req)
        mode = SVAL(req->inbuf,smb_vwv0);
        mtime = srv_make_unix_date3(req->inbuf+smb_vwv1);
 
+       ts[1] = convert_time_t_to_timespec(mtime);
+       status = smb_set_file_time(conn, NULL, fname,
+                                  &sbuf, ts, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_unixerror(req, ERRDOS, ERRnoaccess);
+               END_PROFILE(SMBsetatr);
+               return;
+       }
+
        if (mode != FILE_ATTRIBUTE_NORMAL) {
                if (VALID_STAT_OF_DIR(sbuf))
                        mode |= aDIR;
@@ -1122,12 +1165,6 @@ void reply_setatr(struct smb_request *req)
                }
        }
 
-       if (!set_filetime(conn,fname,convert_time_t_to_timespec(mtime))) {
-               reply_unixerror(req, ERRDOS, ERRnoaccess);
-               END_PROFILE(SMBsetatr);
-               return;
-       }
-
        reply_outbuf(req, 0, 0);
  
        DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
@@ -1217,6 +1254,7 @@ void reply_search(struct smb_request *req)
        bool mask_contains_wcard = False;
        bool allow_long_path_components = (req->flags2 & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
        TALLOC_CTX *ctx = talloc_tos();
+       bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
 
        START_PROFILE(SMBsearch);
 
@@ -1402,8 +1440,16 @@ void reply_search(struct smb_request *req)
                }
 
                for (i=numentries;(i<maxentries) && !finished;i++) {
-                       finished = !get_dir_entry(ctx,conn,mask,dirtype,&fname,
-                                       &size,&mode,&date,check_descend);
+                       finished = !get_dir_entry(ctx,
+                                                 conn,
+                                                 mask,
+                                                 dirtype,
+                                                 &fname,
+                                                 &size,
+                                                 &mode,
+                                                 &date,
+                                                 check_descend,
+                                                 ask_sharemode);
                        if (!finished) {
                                char buf[DIR_STRUCT_SIZE];
                                memcpy(buf,status,21);
@@ -1638,11 +1684,11 @@ void reply_open(struct smb_request *req)
        }
 
        size = sbuf.st_size;
-       fattr = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
        mtime = sbuf.st_mtime;
 
        if (fattr & aDIR) {
-               DEBUG(3,("attempt to open a directory %s\n",fname));
+               DEBUG(3,("attempt to open a directory %s\n",fsp->fsp_name));
                close_file(fsp,ERROR_CLOSE);
                reply_doserror(req, ERRDOS,ERRnoaccess);
                END_PROFILE(SMBopen);
@@ -1801,7 +1847,7 @@ void reply_open_and_X(struct smb_request *req)
                sbuf.st_size = get_allocation_size(conn,fsp,&sbuf);
        }
 
-       fattr = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
        mtime = sbuf.st_mtime;
        if (fattr & aDIR) {
                close_file(fsp,ERROR_CLOSE);
@@ -1984,7 +2030,12 @@ void reply_mknew(struct smb_request *req)
        }
 
        ts[0] = get_atimespec(&sbuf); /* atime. */
-       file_ntimes(conn, fname, ts);
+       status = smb_set_file_time(conn, fsp, fsp->fsp_name, &sbuf, ts, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBcreate);
+               reply_openerror(req, status);
+               return;
+       }
 
        reply_outbuf(req, 1, 0);
        SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
@@ -1999,9 +2050,9 @@ void reply_mknew(struct smb_request *req)
                                CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
        }
 
-       DEBUG( 2, ( "reply_mknew: file %s\n", fname ) );
+       DEBUG( 2, ( "reply_mknew: file %s\n", fsp->fsp_name ) );
        DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n",
-                               fname, fsp->fh->fd, (unsigned int)fattr ) );
+                   fsp->fsp_name, fsp->fh->fd, (unsigned int)fattr ) );
 
        END_PROFILE(SMBcreate);
        return;
@@ -2080,7 +2131,7 @@ void reply_ctemp(struct smb_request *req)
                return;
        }
 
-       status = check_name(conn, CONST_DISCARD(char *,fname));
+       status = check_name(conn, fname);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                END_PROFILE(SMBctemp);
@@ -2124,9 +2175,9 @@ void reply_ctemp(struct smb_request *req)
        SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
 
        /* the returned filename is relative to the directory */
-       s = strrchr_m(fname, '/');
+       s = strrchr_m(fsp->fsp_name, '/');
        if (!s) {
-               s = fname;
+               s = fsp->fsp_name;
        } else {
                s++;
        }
@@ -2153,9 +2204,9 @@ void reply_ctemp(struct smb_request *req)
                      CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
        }
 
-       DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname ) );
-       DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname, fsp->fh->fd,
-                       (unsigned int)sbuf.st_mode ) );
+       DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fsp->fsp_name ) );
+       DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fsp->fsp_name,
+                   fsp->fh->fd, (unsigned int)sbuf.st_mode ) );
 
        END_PROFILE(SMBctemp);
        return;
@@ -2183,7 +2234,7 @@ static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
                return NT_STATUS_OK;
        }
 
-       if (fsp->access_mask & DELETE_ACCESS) {
+       if (fsp->access_mask & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES)) {
                return NT_STATUS_OK;
        }
 
@@ -2289,23 +2340,31 @@ static NTSTATUS do_unlink(connection_struct *conn,
        /* On open checks the open itself will check the share mode, so
           don't do it here as we'll get it wrong. */
 
-       status = open_file_ntcreate(conn, req, fname, &sbuf,
-                                   DELETE_ACCESS,
-                                   FILE_SHARE_NONE,
-                                   FILE_OPEN,
-                                   0,
-                                   FILE_ATTRIBUTE_NORMAL,
-                                   req != NULL ? 0 : INTERNAL_OPEN_ONLY,
-                                   NULL, &fsp);
+       status = create_file_unixpath
+               (conn,                  /* conn */
+                req,                   /* req */
+                fname,                 /* fname */
+                DELETE_ACCESS,         /* access_mask */
+                FILE_SHARE_NONE,       /* share_access */
+                FILE_OPEN,             /* create_disposition*/
+                FILE_NON_DIRECTORY_FILE, /* create_options */
+                FILE_ATTRIBUTE_NORMAL, /* file_attributes */
+                0,                     /* oplock_request */
+                0,                     /* allocation_size */
+                NULL,                  /* sd */
+                NULL,                  /* ea_list */
+                &fsp,                  /* result */
+                NULL,                  /* pinfo */
+                &sbuf);                /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10, ("open_file_ntcreate failed: %s\n",
+               DEBUG(10, ("create_file_unixpath failed: %s\n",
                           nt_errstr(status)));
                return status;
        }
 
        /* The set is across all open files on this dev/inode pair. */
-       if (!set_delete_on_close(fsp, True, &current_user.ut)) {
+       if (!set_delete_on_close(fsp, True, &conn->server_info->utok)) {
                close_file(fsp, NORMAL_CLOSE);
                return NT_STATUS_ACCESS_DENIED;
        }
@@ -2460,7 +2519,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                        }
 
                        count++;
-                       DEBUG(3,("unlink_internals: succesful unlink [%s]\n",
+                       DEBUG(3,("unlink_internals: successful unlink [%s]\n",
                                 fname));
 
                        TALLOC_FREE(fname);
@@ -2468,7 +2527,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                TALLOC_FREE(dir_hnd);
        }
 
-       if (count == 0 && NT_STATUS_IS_OK(status)) {
+       if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
                status = map_nt_error_from_unix(errno);
        }
 
@@ -2647,7 +2706,7 @@ void send_file_readbraw(connection_struct *conn,
         * reply_readbraw has already checked the length.
         */
 
-       if ( (chain_size == 0) && (nread > 0) &&
+       if ( (chain_size == 0) && (nread > 0) && (fsp->base_fsp == NULL) &&
            (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
                char header[4];
                DATA_BLOB header_blob;
@@ -2760,7 +2819,7 @@ void reply_readbraw(struct smb_request *req)
         */
 
        if (!fsp || !conn || conn != fsp->conn ||
-                       current_user.vuid != fsp->vuid ||
+                       req->vuid != fsp->vuid ||
                        fsp->is_directory || fsp->fh->fd == -1) {
                /*
                 * fsp could be NULL here so use the value from the packet. JRA.
@@ -2895,7 +2954,7 @@ void reply_lockread(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBlockread);
                return;
        }
@@ -3003,7 +3062,7 @@ void reply_read(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBread);
                return;
        }
@@ -3122,7 +3181,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
         */
 
        if ((chain_size == 0) && (CVAL(req->inbuf,smb_vwv0) == 0xFF) &&
-           !is_encrypted_packet(req->inbuf) &&
+           !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
            lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
                uint8 headerbuf[smb_size + 12 * 2];
                DATA_BLOB header;
@@ -3258,7 +3317,7 @@ void reply_read_and_X(struct smb_request *req)
                return;
        }
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBreadX);
                return;
        }
@@ -3329,8 +3388,8 @@ void reply_read_and_X(struct smb_request *req)
                return;
        }
 
-       if (!big_readX
-           && schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
+       if (!big_readX &&
+           schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
                END_PROFILE(SMBreadX);
                return;
        }
@@ -3362,7 +3421,6 @@ void error_to_writebrawerr(struct smb_request *req)
 void reply_writebraw(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       int outsize = 0;
        char *buf = NULL;
        ssize_t nwritten=0;
        ssize_t total_written=0;
@@ -3397,7 +3455,7 @@ void reply_writebraw(struct smb_request *req)
        }
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                error_to_writebrawerr(req);
                END_PROFILE(SMBwritebraw);
                return;
@@ -3472,8 +3530,7 @@ void reply_writebraw(struct smb_request *req)
         * it to send more bytes */
 
        memcpy(buf, req->inbuf, smb_size);
-       outsize = srv_set_message(buf,
-                       Protocol>PROTOCOL_COREPLUS?1:0,0,True);
+       srv_set_message(buf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
        SCVAL(buf,smb_com,SMBwritebraw);
        SSVALS(buf,smb_vwv0,0xFFFF);
        show_msg(buf);
@@ -3485,18 +3542,12 @@ void reply_writebraw(struct smb_request *req)
        }
 
        /* Now read the raw data into the buffer and write it */
-       if (read_smb_length(smbd_server_fd(),buf,
-                       SMB_SECONDARY_WAIT, get_srv_read_error()) == -1) {
+       status = read_smb_length(smbd_server_fd(), buf, SMB_SECONDARY_WAIT,
+                                &numtowrite);
+       if (!NT_STATUS_IS_OK(status)) {
                exit_server_cleanly("secondary writebraw failed");
        }
 
-       /*
-        * Even though this is not an smb message,
-        * smb_len returns the generic length of a packet.
-        */
-
-       numtowrite = smb_len(buf);
-
        /* Set up outbuf to return the correct size */
        reply_outbuf(req, 1, 0);
 
@@ -3515,11 +3566,12 @@ void reply_writebraw(struct smb_request *req)
                                (int)tcount,(int)nwritten,(int)numtowrite));
                }
 
-               if (read_data(smbd_server_fd(), buf+4, numtowrite,get_srv_read_error())
-                                       != numtowrite ) {
+               status = read_data(smbd_server_fd(), buf+4, numtowrite);
+
+               if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0,("reply_writebraw: Oversize secondary write "
-                               "raw read failed (%s). Terminating\n",
-                               strerror(errno) ));
+                                "raw read failed (%s). Terminating\n",
+                                nt_errstr(status)));
                        exit_server_cleanly("secondary writebraw failed");
                }
 
@@ -3609,7 +3661,7 @@ void reply_writeunlock(struct smb_request *req)
        
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBwriteunlock);
                return;
        }
@@ -3716,7 +3768,7 @@ void reply_write(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBwrite);
                return;
        }
@@ -3946,7 +3998,7 @@ void reply_write_and_X(struct smb_request *req)
        startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv3);
        write_through = BITSETW(req->inbuf+smb_vwv7,0);
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBwriteX);
                return;
        }
@@ -4001,13 +4053,13 @@ void reply_write_and_X(struct smb_request *req)
                nwritten = 0;
        } else {
 
-               if (req->unread_bytes == 0 &&
-                               schedule_aio_write_and_X(conn, req, fsp, data,
-                                                       startpos, numtowrite)) {
+               if ((req->unread_bytes == 0) &&
+                   schedule_aio_write_and_X(conn, req, fsp, data, startpos,
+                                            numtowrite)) {
                        END_PROFILE(SMBwriteX);
                        return;
                }
-
+               
                nwritten = write_file(req,fsp,data,startpos,numtowrite);
        }
 
@@ -4065,7 +4117,7 @@ void reply_lseek(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                return;
        }
 
@@ -4151,7 +4203,7 @@ void reply_flush(struct smb_request *req)
        fnum = SVAL(req->inbuf,smb_vwv0);
        fsp = file_fsp(fnum);
 
-       if ((fnum != 0xFFFF) && !check_fsp(conn, req, fsp, &current_user)) {
+       if ((fnum != 0xFFFF) && !check_fsp(conn, req, fsp)) {
                return;
        }
        
@@ -4224,7 +4276,7 @@ void reply_close(struct smb_request *req)
         * We can only use CHECK_FSP if we know it's not a directory.
         */
 
-       if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
+       if(!fsp || (fsp->conn != conn) || (fsp->vuid != req->vuid)) {
                reply_doserror(req, ERRDOS, ERRbadfid);
                END_PROFILE(SMBclose);
                return;
@@ -4237,6 +4289,7 @@ void reply_close(struct smb_request *req)
                DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
                status = close_file(fsp,NORMAL_CLOSE);
        } else {
+               time_t t;
                /*
                 * Close ordinary file.
                 */
@@ -4249,9 +4302,8 @@ void reply_close(struct smb_request *req)
                 * Take care of any time sent in the close.
                 */
 
-               fsp_set_pending_modtime(fsp, convert_time_t_to_timespec(
-                                               srv_make_unix_date3(
-                                                       req->inbuf+smb_vwv1)));
+               t = srv_make_unix_date3(req->inbuf+smb_vwv1);
+               set_close_write_time(fsp, convert_time_t_to_timespec(t));
 
                /*
                 * close_file() returns the unix errno if an error
@@ -4298,7 +4350,7 @@ void reply_writeclose(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBwriteclose);
                return;
        }
@@ -4324,8 +4376,8 @@ void reply_writeclose(struct smb_request *req)
   
        nwritten = write_file(req,fsp,data,startpos,numtowrite);
 
-       set_filetime(conn, fsp->fsp_name, mtime);
-  
+       set_close_write_time(fsp, mtime);
+
        /*
         * More insanity. W2K only closes the file if writelen > 0.
         * JRA.
@@ -4385,7 +4437,7 @@ void reply_lock(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBlock);
                return;
        }
@@ -4444,7 +4496,7 @@ void reply_unlock(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBunlock);
                return;
        }
@@ -4590,7 +4642,7 @@ void reply_printopen(struct smb_request *req)
        }
 
        /* Open for exclusive use, write only. */
-       status = print_fsp_open(conn, NULL, &fsp);
+       status = print_fsp_open(conn, NULL, req->vuid, &fsp);
 
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
@@ -4620,7 +4672,7 @@ void reply_printclose(struct smb_request *req)
 
        START_PROFILE(SMBsplclose);
 
-       if (req->wct < 3) {
+       if (req->wct < 1) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                END_PROFILE(SMBsplclose);
                return;
@@ -4628,7 +4680,7 @@ void reply_printclose(struct smb_request *req)
 
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBsplclose);
                 return;
         }
@@ -4768,7 +4820,7 @@ void reply_printwrite(struct smb_request *req)
   
        fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBsplwr);
                 return;
         }
@@ -5293,7 +5345,7 @@ static void rename_open_files(connection_struct *conn,
                   sharepaths we need to manipulate relative paths. */
                /* TODO - create the absolute path and manipulate the newname
                   relative to the sharepath. */
-               if (fsp->conn != conn) {
+               if (!strequal(fsp->conn->connectpath, conn->connectpath)) {
                        continue;
                }
                DEBUG(10,("rename_open_files: renaming file fnum %d (file_id %s) from %s -> %s\n",
@@ -5513,7 +5565,8 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                return NT_STATUS_ACCESS_DENIED;
        }
 
-       lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL);
+       lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
+                                 NULL);
 
        /*
         * We have the file open ourselves, so not being able to get the
@@ -5583,7 +5636,8 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        uint32 attrs,
                        bool replace_if_exists,
                        bool src_has_wild,
-                       bool dest_has_wild)
+                       bool dest_has_wild,
+                       uint32_t access_mask)
 {
        char *directory = NULL;
        char *mask = NULL;
@@ -5713,12 +5767,12 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
 
                status = S_ISDIR(sbuf1.st_mode) ?
                        open_directory(conn, req, directory, &sbuf1,
-                                      DELETE_ACCESS,
+                                      access_mask,
                                       FILE_SHARE_READ|FILE_SHARE_WRITE,
                                       FILE_OPEN, 0, 0, NULL,
                                       &fsp)
                        : open_file_ntcreate(conn, req, directory, &sbuf1,
-                                            DELETE_ACCESS,
+                                            access_mask,
                                             FILE_SHARE_READ|FILE_SHARE_WRITE,
                                             FILE_OPEN, 0, 0, 0, NULL,
                                             &fsp);
@@ -5817,12 +5871,12 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
 
                status = S_ISDIR(sbuf1.st_mode) ?
                        open_directory(conn, req, fname, &sbuf1,
-                                      DELETE_ACCESS,
+                                      access_mask,
                                       FILE_SHARE_READ|FILE_SHARE_WRITE,
                                       FILE_OPEN, 0, 0, NULL,
                                       &fsp)
                        : open_file_ntcreate(conn, req, fname, &sbuf1,
-                                            DELETE_ACCESS,
+                                            access_mask,
                                             FILE_SHARE_READ|FILE_SHARE_WRITE,
                                             FILE_OPEN, 0, 0, 0, NULL,
                                             &fsp);
@@ -5856,7 +5910,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
        }
        TALLOC_FREE(dir_hnd);
 
-       if (count == 0 && NT_STATUS_IS_OK(status)) {
+       if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
                status = map_nt_error_from_unix(errno);
        }
 
@@ -5945,7 +5999,7 @@ void reply_mv(struct smb_request *req)
        DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
 
        status = rename_internals(ctx, conn, req, name, newname, attrs, False,
-                                 src_has_wcard, dest_has_wcard);
+                                 src_has_wcard, dest_has_wcard, DELETE_ACCESS);
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call. */
@@ -6074,7 +6128,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
        close_file(fsp1,NORMAL_CLOSE);
 
        /* Ensure the modtime is set correctly on the destination file. */
-       fsp_set_pending_modtime( fsp2, get_mtimespec(&src_sbuf));
+       set_close_write_time(fsp2, get_mtimespec(&src_sbuf));
 
        /*
         * As we are opening fsp1 read-only we only expect
@@ -6600,7 +6654,7 @@ void reply_lockingX(struct smb_request *req)
        lock_timeout = IVAL(req->inbuf,smb_vwv4);
        large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
 
-       if (!check_fsp(conn, req, fsp, &current_user)) {
+       if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBlockingX);
                return;
        }
@@ -6957,6 +7011,8 @@ void reply_setattrE(struct smb_request *req)
        connection_struct *conn = req->conn;
        struct timespec ts[2];
        files_struct *fsp;
+       SMB_STRUCT_STAT sbuf;
+       NTSTATUS status;
 
        START_PROFILE(SMBsetattrE);
 
@@ -6992,22 +7048,26 @@ void reply_setattrE(struct smb_request *req)
         * Sometimes times are sent as zero - ignore them.
         */
 
-       if (null_timespec(ts[0]) && null_timespec(ts[1])) {
-               /* Ignore request */
-               if( DEBUGLVL( 3 ) ) {
-                       dbgtext( "reply_setattrE fnum=%d ", fsp->fnum);
-                       dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
+       /* Ensure we have a valid stat struct for the source. */
+       if (fsp->fh->fd != -1) {
+               if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
+                       status = map_nt_error_from_unix(errno);
+                       reply_nterror(req, status);
+                       END_PROFILE(SMBsetattrE);
+                       return;
+               }
+       } else {
+               if (SMB_VFS_STAT(conn, fsp->fsp_name, &sbuf) == -1) {
+                       status = map_nt_error_from_unix(errno);
+                       reply_nterror(req, status);
+                       END_PROFILE(SMBsetattrE);
+                       return;
                }
-               END_PROFILE(SMBsetattrE);
-               return;
-       } else if (!null_timespec(ts[0]) && null_timespec(ts[1])) {
-               /* set modify time = to access time if modify time was unset */
-               ts[1] = ts[0];
        }
 
-       /* Set the date on this file */
-       /* Should we set pending modtime here ? JRA */
-       if(file_ntimes(conn, fsp->fsp_name, ts)) {
+       status = smb_set_file_time(conn, fsp, fsp->fsp_name,
+                                  &sbuf, ts, true);
+       if (!NT_STATUS_IS_OK(status)) {
                reply_doserror(req, ERRDOS, ERRnoaccess);
                END_PROFILE(SMBsetattrE);
                return;