Change check_path_syntax() to use the new next_mb_char_size() function
[ira/wip.git] / source3 / smbd / reply.c
index 1a1c84efed590996cf17ee82caeb0e5b1a0a9ee0..0fe73cddc28f6e08a795baef25c643799b82ede3 100644 (file)
@@ -3,6 +3,7 @@
    Main SMB reply routines
    Copyright (C) Andrew Tridgell 1992-1998
    Copyright (C) Andrew Bartlett      2001
+   Copyright (C) Jeremy Allison 1992-2004.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -39,7 +40,123 @@ unsigned int smb_echo_count = 0;
 extern BOOL global_encrypted_passwords_negotiated;
 
 /****************************************************************************
- Reply to an special message.
+ Ensure we check the path in *exactly* the same way as W2K.
+ We're assuming here that '/' is not the second byte in any multibyte char
+ set (a safe assumption). '\\' *may* be the second byte in a multibyte char
+ set.
+****************************************************************************/
+
+NTSTATUS check_path_syntax(pstring destname, const pstring srcname)
+{
+       char *d = destname;
+       const char *s = srcname;
+       NTSTATUS ret = NT_STATUS_OK;
+
+       while (*s) {
+               if (IS_DIRECTORY_SEP(*s)) {
+                       /*
+                        * Safe to assume is not the second part of a mb char as this is handled below.
+                        */
+                       /* Eat multiple '/' or '\\' */
+                       while (IS_DIRECTORY_SEP(*s)) {
+                               s++;
+                       }
+                       if ((d != destname) && (*s != '\0')) {
+                               /* We only care about non-leading or trailing '/' or '\\' */
+                               *d++ = '/';
+                       }
+               } else if ((s[0] == '.') && (s[1] == '.') && (IS_DIRECTORY_SEP(s[2]) || s[2] == '\0')) {
+                       /* Uh oh - "../" or "..\\"  or "..\0" ! */
+
+                       /*
+                        * No mb char starts with '.' so we're safe checking the directory separator here.
+                        */
+
+                       /* If we just added a '/', delete it. */
+
+                       if ((d > destname) && (*(d-1) == '/')) {
+                               *(d-1) = '\0';
+                               if (d == (destname + 1)) {
+                                       d--;
+                               } else {
+                                       d -= 2;
+                               }
+                       }
+                       /* Are we at the start ? Can't go back further if so. */
+                       if (d == destname) {
+                               return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
+                       }
+                       /* Go back one level... */
+                       /* We know this is safe as '/' cannot be part of a mb sequence. */
+                       /* NOTE - if this assumption is invalid we are not in good shape... */
+                       while (d > destname) {
+                               if (*d == '/')
+                                       break;
+                               d--;
+                       }
+                       s += 3;
+               } else if ((s[0] == '.') && IS_DIRECTORY_SEP(s[1])) {
+
+                       /*
+                        * No mb char starts with '.' so we're safe checking the directory separator here.
+                        */
+
+                       /* "./" or ".\\" fails with a different error depending on where it is... */
+
+                       if (s == srcname) {
+                               ret = NT_STATUS_OBJECT_NAME_INVALID;
+                       } else {
+                               if (s[2] == '\0') {
+                                       return NT_STATUS_INVALID_PARAMETER;
+                               }
+                               ret = NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                       }
+                       s++;
+               } else {
+                       switch(next_mb_char_size(s)) {
+                               case 4:
+                                       *d++ = *s++;
+                               case 3:
+                                       *d++ = *s++;
+                               case 2:
+                                       *d++ = *s++;
+                               case 1:
+                                       *d++ = *s++;
+                                       break;
+                               default:
+                                       DEBUG(0,("check_path_syntax: character length assumptions invalid !\n"));
+                                       return NT_STATUS_INVALID_PARAMETER;
+                       }
+               }
+       }
+       *d = '\0';
+       return ret;
+}
+
+/****************************************************************************
+ Pull a string and check the path - provide for error return.
+****************************************************************************/
+
+size_t srvstr_get_path(char *inbuf, char *dest, const char *src, size_t dest_len, size_t src_len, int flags, NTSTATUS *err)
+{
+       pstring tmppath;
+       char *tmppath_ptr = tmppath;
+       size_t ret;
+#ifdef DEVELOPER
+       SMB_ASSERT(dest_len == sizeof(pstring));
+#endif
+
+       if (src_len == 0) {
+               ret = srvstr_pull_buf( inbuf, tmppath_ptr, src, dest_len, flags);
+       } else {
+               ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
+       }
+       *err = check_path_syntax(dest, tmppath);
+       return ret;
+}
+
+/****************************************************************************
+ Reply to a special message.
 ****************************************************************************/
 
 int reply_special(char *inbuf,char *outbuf)
@@ -47,9 +164,7 @@ int reply_special(char *inbuf,char *outbuf)
        int outsize = 4;
        int msg_type = CVAL(inbuf,0);
        int msg_flags = CVAL(inbuf,1);
-       pstring name1,name2;
-
-       int len;
+       fstring name1,name2;
        char name_type = 0;
        
        static BOOL already_got_session = False;
@@ -75,23 +190,16 @@ int reply_special(char *inbuf,char *outbuf)
                        return(0);
                }
                name_extract(inbuf,4,name1);
-               name_extract(inbuf,4 + name_len(inbuf + 4),name2);
+               name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
                DEBUG(2,("netbios connect: name1=%s name2=%s\n",
                         name1,name2));      
 
-               name1[15] = 0;
-
-               len = strlen(name2);
-               if (len == 16) {
-                       name_type = name2[15];
-                       name2[15] = 0;
-               }
-
                set_local_machine_name(name1, True);
                set_remote_machine_name(name2, True);
 
-               DEBUG(2,("netbios connect: local=%s remote=%s\n",
-                       get_local_machine_name(), get_remote_machine_name() ));
+               DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
+                        get_local_machine_name(), get_remote_machine_name(),
+                        name_type));
 
                if (name_type == 'R') {
                        /* We are being asked for a pathworks session --- 
@@ -148,7 +256,7 @@ int reply_tcon(connection_struct *conn,
        const char *service;
        pstring service_buf;
        pstring password;
-       fstring dev;
+       pstring dev;
        int outsize = 0;
        uint16 vuid = SVAL(inbuf,smb_uid);
        int pwlen=0;
@@ -204,16 +312,21 @@ int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
 {
        fstring service;
        DATA_BLOB password;
-       fstring devicename;
+
+       /* what the cleint thinks the device is */
+       fstring client_devicetype;
+       /* what the server tells the client the share represents */
+       const char *server_devicetype;
        NTSTATUS nt_status;
        uint16 vuid = SVAL(inbuf,smb_uid);
        int passlen = SVAL(inbuf,smb_vwv3);
        pstring path;
        char *p, *q;
        extern BOOL global_encrypted_passwords_negotiated;
+       
        START_PROFILE(SMBtconX);        
 
-       *service = *devicename = 0;
+       *service = *client_devicetype = 0;
 
        /* we might have to close an old one */
        if ((SVAL(inbuf,smb_vwv2) & 0x1) && conn) {
@@ -250,11 +363,11 @@ int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        else
                fstrcpy(service,path);
                
-       p += srvstr_pull(inbuf, devicename, p, sizeof(devicename), 6, STR_ASCII);
+       p += srvstr_pull(inbuf, client_devicetype, p, sizeof(client_devicetype), 6, STR_ASCII);
 
-       DEBUG(4,("Got device type %s\n",devicename));
+       DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
 
-       conn = make_connection(service,password,devicename,vuid,&nt_status);
+       conn = make_connection(service,password,client_devicetype,vuid,&nt_status);
        
        data_blob_clear_free(&password);
 
@@ -263,35 +376,29 @@ int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                return ERROR_NT(nt_status);
        }
 
+       if ( IS_IPC(conn) )
+               server_devicetype = "IPC";
+       else if ( IS_PRINT(conn) )
+               server_devicetype = "LPT1:";
+       else 
+               server_devicetype = "A:";
+
        if (Protocol < PROTOCOL_NT1) {
                set_message(outbuf,2,0,True);
                p = smb_buf(outbuf);
-               p += srvstr_push(outbuf, p, devicename, -1, 
+               p += srvstr_push(outbuf, p, server_devicetype, -1, 
                                 STR_TERMINATE|STR_ASCII);
                set_message_end(outbuf,p);
        } else {
                /* NT sets the fstype of IPC$ to the null string */
-               const char *fsname = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
-               const char *devicetype;
-
+               const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
+               
                set_message(outbuf,3,0,True);
-               if ( IS_IPC(conn) )
-                       devicetype = "IPC";
-               else if ( IS_PRINT(conn) )
-                       devicetype = "LPT:";
-               else 
-                       devicetype = "A:";
-
-               p = smb_buf(outbuf);
-               p += srvstr_push(outbuf, p, IS_IPC(conn) ? "IPC" : devicetype, -1, 
-                                STR_TERMINATE|STR_ASCII);
-               p += srvstr_push(outbuf, p, fsname, -1, 
-                                STR_TERMINATE);
+
                p = smb_buf(outbuf);
-               p += srvstr_push(outbuf, p, IS_IPC(conn) ? "IPC" : devicename, -1, 
+               p += srvstr_push(outbuf, p, server_devicetype, -1, 
                                 STR_TERMINATE|STR_ASCII);
-               p += srvstr_push(outbuf, p, fsname, -1, 
+               p += srvstr_push(outbuf, p, fstype, -1, 
                                 STR_TERMINATE);
                
                set_message_end(outbuf,p);
@@ -343,7 +450,6 @@ int reply_ioctl(connection_struct *conn,
        uint32 ioctl_code = (device << 16) + function;
        int replysize, outsize;
        char *p;
-       files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        START_PROFILE(SMBioctl);
 
        DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
@@ -366,6 +472,11 @@ int reply_ioctl(connection_struct *conn,
        switch (ioctl_code) {
                case IOCTL_QUERY_JOB_INFO:                  
                {
+                       files_struct *fsp = file_fsp(inbuf,smb_vwv0);
+                       if (!fsp) {
+                               END_PROFILE(SMBioctl);
+                               return(UNIXERROR(ERRDOS,ERRbadfid));
+                       }
                        SSVAL(p,0,fsp->rap_print_jobid);             /* Job number */
                        srvstr_push(outbuf, p+2, global_myname(), 15, STR_TERMINATE|STR_ASCII);
                        srvstr_push(outbuf, p+18, lp_servicename(SNUM(conn)), 13, STR_TERMINATE|STR_ASCII);
@@ -389,9 +500,15 @@ int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        BOOL ok = False;
        BOOL bad_path = False;
        SMB_STRUCT_STAT sbuf;
+       NTSTATUS status;
+
        START_PROFILE(SMBchkpth);
 
-       srvstr_pull_buf(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), STR_TERMINATE);
+       srvstr_get_path(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBchkpth);
+               return ERROR_NT(status);
+       }
 
        RESOLVE_DFSPATH(name, conn, inbuf, outbuf);
 
@@ -400,8 +517,11 @@ int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        mode = SVAL(inbuf,smb_vwv0);
 
        if (check_name(name,conn)) {
-               if (VALID_STAT(sbuf) || vfs_stat(conn,name,&sbuf) == 0)
-                       ok = S_ISDIR(sbuf.st_mode);
+               if (VALID_STAT(sbuf) || SMB_VFS_STAT(conn,name,&sbuf) == 0)
+                       if (!(ok = S_ISDIR(sbuf.st_mode))) {
+                               END_PROFILE(SMBchkpth);
+                               return ERROR_BOTH(NT_STATUS_NOT_A_DIRECTORY,ERRDOS,ERRbadpath);
+                       }
        }
 
        if (!ok) {
@@ -410,9 +530,27 @@ int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                        one at a time - if a component fails it expects
                        ERRbadpath, not ERRbadfile.
                */
-               if(errno == ENOENT)
-                       return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
+               if(errno == ENOENT) {
+                       /*
+                        * Windows returns different error codes if
+                        * the parent directory is valid but not the
+                        * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
+                        * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
+                        * if the path is invalid.
+                        */
+                       if (bad_path) {
+                               END_PROFILE(SMBchkpth);
+                               return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
+                       } else {
+                               END_PROFILE(SMBchkpth);
+                               return ERROR_NT(NT_STATUS_OBJECT_NAME_NOT_FOUND);
+                       }
+               } else if (errno == ENOTDIR) {
+                       END_PROFILE(SMBchkpth);
+                       return ERROR_NT(NT_STATUS_NOT_A_DIRECTORY);
+               }
 
+               END_PROFILE(SMBchkpth);
                return(UNIXERROR(ERRDOS,ERRbadpath));
        }
 
@@ -439,10 +577,16 @@ int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        time_t mtime=0;
        BOOL bad_path = False;
        char *p;
+       NTSTATUS status;
+
        START_PROFILE(SMBgetatr);
 
        p = smb_buf(inbuf) + 1;
-       p += srvstr_pull_buf(inbuf, fname, p, sizeof(fname), STR_TERMINATE);
+       p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBgetatr);
+               return ERROR_NT(status);
+       }
 
        RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
   
@@ -458,7 +602,7 @@ int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        } else {
                unix_convert(fname,conn,0,&bad_path,&sbuf);
                if (check_name(fname,conn)) {
-                       if (VALID_STAT(sbuf) || vfs_stat(conn,fname,&sbuf) == 0) {
+                       if (VALID_STAT(sbuf) || SMB_VFS_STAT(conn,fname,&sbuf) == 0) {
                                mode = dos_mode(conn,fname,&sbuf);
                                size = sbuf.st_size;
                                mtime = sbuf.st_mtime;
@@ -472,9 +616,8 @@ int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        }
   
        if (!ok) {
-               set_bad_path_error(errno, bad_path);
                END_PROFILE(SMBgetatr);
-               return(UNIXERROR(ERRDOS,ERRbadfile));
+               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadfile);
        }
  
        outsize = set_message(outbuf,10,0,True);
@@ -509,30 +652,40 @@ int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        SMB_STRUCT_STAT sbuf;
        BOOL bad_path = False;
        char *p;
+       NTSTATUS status;
 
        START_PROFILE(SMBsetatr);
 
        p = smb_buf(inbuf) + 1;
-       p += srvstr_pull_buf(inbuf, fname, p, sizeof(fname), STR_TERMINATE);
+       p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBsetatr);
+               return ERROR_NT(status);
+       }
+
        unix_convert(fname,conn,0,&bad_path,&sbuf);
 
        mode = SVAL(inbuf,smb_vwv0);
        mtime = make_unix_date3(inbuf+smb_vwv1);
   
-       if (VALID_STAT_OF_DIR(sbuf))
-               mode |= aDIR;
-       else
-               mode &= ~aDIR;
+       if (mode != FILE_ATTRIBUTE_NORMAL) {
+               if (VALID_STAT_OF_DIR(sbuf))
+                       mode |= aDIR;
+               else
+                       mode &= ~aDIR;
+
+               if (check_name(fname,conn))
+                       ok =  (file_chmod(conn,fname,mode,NULL) == 0);
+       } else {
+               ok = True;
+       }
 
-       if (check_name(fname,conn))
-               ok =  (file_chmod(conn,fname,mode,NULL) == 0);
        if (ok)
                ok = set_filetime(conn,fname,mtime);
   
        if (!ok) {
-               set_bad_path_error(errno, bad_path);
                END_PROFILE(SMBsetatr);
-               return(UNIXERROR(ERRDOS,ERRnoaccess));
+               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
        }
  
        outsize = set_message(outbuf,0,0,True);
@@ -553,7 +706,7 @@ int reply_dskattr(connection_struct *conn, char *inbuf,char *outbuf, int dum_siz
        SMB_BIG_UINT dfree,dsize,bsize;
        START_PROFILE(SMBdskattr);
 
-       conn->vfs_ops.disk_free(conn,".",True,&bsize,&dfree,&dsize);
+       SMB_VFS_DISK_FREE(conn,".",True,&bsize,&dfree,&dsize);
   
        outsize = set_message(outbuf,5,0,True);
        
@@ -606,10 +759,9 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        time_t date;
        int dirtype;
        int outsize = 0;
-       int numentries = 0;
+       unsigned int numentries = 0;
+       unsigned int maxentries = 0;
        BOOL finished = False;
-       int maxentries;
-       int i;
        char *p;
        BOOL ok = False;
        int status_len;
@@ -620,6 +772,7 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        BOOL expect_close = False;
        BOOL can_open = True;
        BOOL bad_path = False;
+       NTSTATUS nt_status;
        START_PROFILE(SMBsearch);
 
        *mask = *directory = *fname = 0;
@@ -632,7 +785,11 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        maxentries = SVAL(inbuf,smb_vwv0); 
        dirtype = SVAL(inbuf,smb_vwv1);
        p = smb_buf(inbuf) + 1;
-       p += srvstr_pull_buf(inbuf, path, p, sizeof(path), STR_TERMINATE);
+       p += srvstr_get_path(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &nt_status);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               END_PROFILE(SMBsearch);
+               return ERROR_NT(nt_status);
+       }
        p++;
        status_len = SVAL(p, 0);
        p += 2;
@@ -693,9 +850,8 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                        dptr_num = dptr_create(conn,directory,True,expect_close,SVAL(inbuf,smb_pid));
                        if (dptr_num < 0) {
                                if(dptr_num == -2) {
-                                       set_bad_path_error(errno, bad_path);
                                        END_PROFILE(SMBsearch);
-                                       return (UNIXERROR(ERRDOS,ERRnofids));
+                                       return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnofids);
                                }
                                END_PROFILE(SMBsearch);
                                return ERROR_DOS(ERRDOS,ERRnofids);
@@ -719,6 +875,9 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                                        numentries = 0;
                                p += DIR_STRUCT_SIZE;
                        } else {
+                               unsigned int i;
+                               maxentries = MIN(maxentries, ((BUFFER_SIZE - (p - outbuf))/DIR_STRUCT_SIZE));
+
                                DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
                                conn->dirpath,lp_dontdescend(SNUM(conn))));
                                if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True))
@@ -741,21 +900,23 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
 
   SearchEmpty:
 
-       if (numentries == 0 || !ok) {
-               SCVAL(outbuf,smb_rcls,ERRDOS);
-               SSVAL(outbuf,smb_err,ERRnofiles);
-               dptr_close(&dptr_num);
-       }
-
        /* If we were called as SMBffirst with smb_search_id == NULL
                and no entries were found then return error and close dirptr 
                (X/Open spec) */
 
        if(ok && expect_close && numentries == 0 && status_len == 0) {
-               SCVAL(outbuf,smb_rcls,ERRDOS);
-               SSVAL(outbuf,smb_err,ERRnofiles);
+               if (Protocol < PROTOCOL_NT1) {
+                       SCVAL(outbuf,smb_rcls,ERRDOS);
+                       SSVAL(outbuf,smb_err,ERRnofiles);
+               }
                /* Also close the dptr - we know it's gone */
                dptr_close(&dptr_num);
+       } else if (numentries == 0 || !ok) {
+               if (Protocol < PROTOCOL_NT1) {
+                       SCVAL(outbuf,smb_rcls,ERRDOS);
+                       SSVAL(outbuf,smb_err,ERRnofiles);
+               }
+               dptr_close(&dptr_num);
        }
 
        /* If we were called as SMBfunique, then we can close the dirptr now ! */
@@ -776,7 +937,7 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        if ((! *directory) && dptr_path(dptr_num))
                slprintf(directory, sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
 
-       DEBUG( 4, ( "%s mask=%s path=%s dtype=%d nument=%d of %d\n",
+       DEBUG( 4, ( "%s mask=%s path=%s dtype=%d nument=%u of %u\n",
                smb_fn_name(CVAL(inbuf,smb_com)), 
                mask, directory, dirtype, numentries, maxentries ) );
 
@@ -796,12 +957,17 @@ int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        char status[21];
        int dptr_num= -2;
        char *p;
+       NTSTATUS err;
 
        START_PROFILE(SMBfclose);
 
        outsize = set_message(outbuf,1,0,True);
        p = smb_buf(inbuf) + 1;
-       p += srvstr_pull_buf(inbuf, path, p, sizeof(path), STR_TERMINATE);
+       p += srvstr_get_path(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &err);
+       if (!NT_STATUS_IS_OK(err)) {
+               END_PROFILE(SMBfclose);
+               return ERROR_NT(err);
+       }
        p++;
        status_len = SVAL(p,0);
        p += 2;
@@ -844,11 +1010,16 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        BOOL bad_path = False;
        files_struct *fsp;
        int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
+       NTSTATUS status;
        START_PROFILE(SMBopen);
  
        share_mode = SVAL(inbuf,smb_vwv0);
 
-       srvstr_pull_buf(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), STR_TERMINATE);
+       srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBopen);
+               return ERROR_NT(status);
+       }
 
        RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
 
@@ -860,9 +1031,8 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                        unixmode, oplock_request,&rmode,NULL);
 
        if (!fsp) {
-               set_bad_path_error(errno, bad_path);
                END_PROFILE(SMBopen);
-               return(UNIXERROR(ERRDOS,ERRnoaccess));
+               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
        }
 
        size = sbuf.st_size;
@@ -922,6 +1092,7 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        int smb_action = 0;
        BOOL bad_path = False;
        files_struct *fsp;
+       NTSTATUS status;
        START_PROFILE(SMBopenX);
 
        /* If it's an IPC, pass off the pipe handler. */
@@ -936,7 +1107,11 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        }
 
        /* XXXX we need to handle passed times, sattr and flags */
-       srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
+       srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBopenX);
+               return ERROR_NT(status);
+       }
 
        RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
 
@@ -948,9 +1123,8 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                        oplock_request, &rmode,&smb_action);
       
        if (!fsp) {
-               set_bad_path_error(errno, bad_path);
                END_PROFILE(SMBopenX);
-               return(UNIXERROR(ERRDOS,ERRnoaccess));
+               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
        }
 
        size = sbuf.st_size;
@@ -1043,12 +1217,17 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        files_struct *fsp;
        int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
        SMB_STRUCT_STAT sbuf;
+       NTSTATUS status;
        START_PROFILE(SMBcreate);
  
        com = SVAL(inbuf,smb_com);
 
        createmode = SVAL(inbuf,smb_vwv0);
-       srvstr_pull_buf(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), STR_TERMINATE);
+       srvstr_get_path(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBcreate);
+               return ERROR_NT(status);
+       }
 
        RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
 
@@ -1072,9 +1251,8 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                        ofun, unixmode, oplock_request, NULL, NULL);
   
        if (!fsp) {
-               set_bad_path_error(errno, bad_path);
                END_PROFILE(SMBcreate);
-               return(UNIXERROR(ERRDOS,ERRnoaccess));
+               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
        }
  
        outsize = set_message(outbuf,1,0,True);
@@ -1109,11 +1287,16 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        int tmpfd;
        SMB_STRUCT_STAT sbuf;
        char *p, *s;
+       NTSTATUS status;
 
        START_PROFILE(SMBctemp);
 
        createmode = SVAL(inbuf,smb_vwv0);
-       srvstr_pull_buf(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), STR_TERMINATE);
+       srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBctemp);
+               return ERROR_NT(status);
+       }
        pstrcat(fname,"\\TMXXXXXX");
 
        RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
@@ -1128,7 +1311,7 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                return(UNIXERROR(ERRDOS,ERRnoaccess));
        }
 
-       vfs_stat(conn,fname,&sbuf);
+       SMB_VFS_STAT(conn,fname,&sbuf);
 
        /* Open file in dos compatibility share mode. */
        /* We should fail if file does not exist. */
@@ -1141,9 +1324,8 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        close(tmpfd);
 
        if (!fsp) {
-               set_bad_path_error(errno, bad_path);
                END_PROFILE(SMBctemp);
-               return(UNIXERROR(ERRDOS,ERRnoaccess));
+               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
        }
 
        outsize = set_message(outbuf,1,0,True);
@@ -1206,6 +1388,7 @@ static NTSTATUS can_rename(char *fname,connection_struct *conn, SMB_STRUCT_STAT
                        ret = NT_STATUS_SHARING_VIOLATION;
                unix_ERR_class = 0;
                unix_ERR_code = 0;
+               unix_ERR_ntstatus = NT_STATUS_OK;
                return ret;
        }
        close_file(fsp,False);
@@ -1216,7 +1399,7 @@ static NTSTATUS can_rename(char *fname,connection_struct *conn, SMB_STRUCT_STAT
  Check if a user is allowed to delete a file.
 ********************************************************************/
 
-static NTSTATUS can_delete(char *fname,connection_struct *conn, int dirtype)
+static NTSTATUS can_delete(char *fname,connection_struct *conn, int dirtype, BOOL bad_path)
 {
        SMB_STRUCT_STAT sbuf;
        int fmode;
@@ -1224,21 +1407,38 @@ static NTSTATUS can_delete(char *fname,connection_struct *conn, int dirtype)
        int access_mode;
        files_struct *fsp;
 
+       DEBUG(10,("can_delete: %s, dirtype = %d\n",
+               fname, dirtype ));
+
        if (!CAN_WRITE(conn))
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
 
-       if (conn->vfs_ops.lstat(conn,fname,&sbuf) != 0)
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
+               if(errno == ENOENT) {
+                       if (bad_path)
+                               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                       else
+                               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               }
+               return map_nt_error_from_unix(errno);
+       }
 
        fmode = dos_mode(conn,fname,&sbuf);
+
+       /* Can't delete a directory. */
        if (fmode & aDIR)
                return NT_STATUS_FILE_IS_A_DIRECTORY;
+#if 0 /* JRATEST */
+       else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
+               return NT_STATUS_OBJECT_NAME_INVALID;
+#endif /* JRATEST */
+
        if (!lp_delete_readonly(SNUM(conn))) {
                if (fmode & aRONLY)
                        return NT_STATUS_CANNOT_DELETE;
        }
        if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM))
-               return NT_STATUS_CANNOT_DELETE;
+               return NT_STATUS_NO_SUCH_FILE;
 
        /* We need a better way to return NT status codes from open... */
        unix_ERR_class = 0;
@@ -1281,6 +1481,16 @@ NTSTATUS unlink_internals(connection_struct *conn, int dirtype, char *name)
        
        *directory = *mask = 0;
        
+       /* We must check for wildcards in the name given
+        * directly by the client - before any unmangling.
+        * This prevents an unmangling of a UNIX name containing
+        * a DOS wildcard like '*' or '?' from unmangling into
+        * a wildcard delete which was not intended.
+        * FIX for #226. JRA.
+        */
+
+       has_wild = ms_has_wild(name);
+
        rc = unix_convert(name,conn,0,&bad_path,&sbuf);
        
        p = strrchr_m(name,'/');
@@ -1305,15 +1515,14 @@ NTSTATUS unlink_internals(connection_struct *conn, int dirtype, char *name)
        if (!rc && mangle_is_mangled(mask))
                mangle_check_cache( mask );
        
-       has_wild = ms_has_wild(mask);
-       
        if (!has_wild) {
                pstrcat(directory,"/");
                pstrcat(directory,mask);
-               error = can_delete(directory,conn,dirtype);
-               if (!NT_STATUS_IS_OK(error)) return error;
+               error = can_delete(directory,conn,dirtype,bad_path);
+               if (!NT_STATUS_IS_OK(error))
+                       return error;
 
-               if (vfs_unlink(conn,directory) == 0) {
+               if (SMB_VFS_UNLINK(conn,directory) == 0) {
                        count++;
                }
        } else {
@@ -1329,21 +1538,41 @@ NTSTATUS unlink_internals(connection_struct *conn, int dirtype, char *name)
                */
                
                if (dirptr) {
-                       error = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+                       error = NT_STATUS_NO_SUCH_FILE;
                        
                        if (strequal(mask,"????????.???"))
                                pstrcpy(mask,"*");
 
                        while ((dname = ReadDirName(dirptr))) {
                                pstring fname;
+                               BOOL sys_direntry = False;
                                pstrcpy(fname,dname);
+
+                               /* Quick check for "." and ".." */
+                               if (fname[0] == '.') {
+                                       if (!fname[1] || (fname[1] == '.' && !fname[2])) {
+                                               if ((dirtype & aDIR)) {
+                                                       sys_direntry = True;
+                                               } else {
+                                                       continue;
+                                               }
+                                       }
+                               }
+
+                               if(!mask_match(fname, mask, case_sensitive))
+                                       continue;
                                
-                               if(!mask_match(fname, mask, case_sensitive)) continue;
-                               
+                               if (sys_direntry) {
+                                       error = NT_STATUS_OBJECT_NAME_INVALID;
+                                       continue;
+                               }
+
                                slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
-                               error = can_delete(fname,conn,dirtype);
-                               if (!NT_STATUS_IS_OK(error)) continue;
-                               if (vfs_unlink(conn,fname) == 0) count++;
+                               error = can_delete(fname,conn,dirtype,bad_path);
+                               if (!NT_STATUS_IS_OK(error))
+                                       continue;
+                               if (SMB_VFS_UNLINK(conn,fname) == 0)
+                                       count++;
                                DEBUG(3,("unlink_internals: succesful unlink [%s]\n",fname));
                        }
                        CloseDir(dirptr);
@@ -1372,14 +1601,19 @@ int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        
        dirtype = SVAL(inbuf,smb_vwv0);
        
-       srvstr_pull_buf(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), STR_TERMINATE);
+       srvstr_get_path(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBunlink);
+               return ERROR_NT(status);
+       }
        
        RESOLVE_DFSPATH(name, conn, inbuf, outbuf);
        
        DEBUG(3,("reply_unlink : %s\n",name));
        
        status = unlink_internals(conn, dirtype, name);
-       if (!NT_STATUS_IS_OK(status)) return ERROR_NT(status);
+       if (!NT_STATUS_IS_OK(status))
+               return ERROR_NT(status);
 
        /*
         * Win2k needs a changenotify request response before it will
@@ -1429,7 +1663,7 @@ void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T st
                header.length = 4;
                header.free = NULL;
 
-               if ( conn->vfs_ops.sendfile( smbd_server_fd(), fsp, fsp->fd, &header, startpos, nread) == -1) {
+               if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fd, &header, startpos, nread) == -1) {
                        /*
                         * Special hack for broken Linux with no 64 bit clean sendfile. If we
                         * return ENOSYS then pretend we just got a normal read.
@@ -1449,8 +1683,13 @@ void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T st
 
        if (nread > 0) {
                ret = read_file(fsp,outbuf+4,startpos,nread);
+#if 0 /* mincount appears to be ignored in a W2K server. JRA. */
                if (ret < mincount)
                        ret = 0;
+#else
+               if (ret < nread)
+                       ret = 0;
+#endif
        }
 
        _smb_setlen(outbuf,ret);
@@ -1464,6 +1703,7 @@ void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T st
 
 int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_size, int dum_buffsize)
 {
+       extern struct current_user current_user;
        ssize_t maxcount,mincount;
        size_t nread = 0;
        SMB_OFF_T startpos;
@@ -1471,6 +1711,10 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
        files_struct *fsp;
        START_PROFILE(SMBreadbraw);
 
+       if (srv_is_signing_active()) {
+               exit_server("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
+       }
+
        /*
         * Special check if an oplock break has been issued
         * and the readraw request croses on the wire, we must
@@ -1545,7 +1789,6 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
 
        /* ensure we don't overrun the packet size */
        maxcount = MIN(65535,maxcount);
-       maxcount = MAX(mincount,maxcount);
 
        if (!is_locked(fsp,conn,(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK,False)) {
                SMB_OFF_T size = fsp->size;
@@ -1553,7 +1796,7 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
   
                if (size < sizeneeded) {
                        SMB_STRUCT_STAT st;
-                       if (vfs_fstat(fsp,fsp->fd,&st) == 0)
+                       if (SMB_VFS_FSTAT(fsp,fsp->fd,&st) == 0)
                                size = st.st_size;
                        if (!fsp->can_write) 
                                fsp->size = size;
@@ -1565,8 +1808,10 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
                        nread = MIN(maxcount,(size - startpos));          
        }
 
+#if 0 /* mincount appears to be ignored in a W2K server. JRA. */
        if (nread < mincount)
                nread = 0;
+#endif
   
        DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%d min=%d nread=%d\n", fsp->fnum, (double)startpos,
                                (int)maxcount, (int)mincount, (int)nread ) );
@@ -1591,6 +1836,7 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
        size_t numtoread;
        NTSTATUS status;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
+       BOOL my_lock_ctx = False;
        START_PROFILE(SMBlockread);
 
        CHECK_FSP(fsp,conn);
@@ -1610,13 +1856,21 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
         * protocol request that predates the read/write lock concept. 
         * Thus instead of asking for a read lock here we need to ask
         * for a write lock. JRA.
+        * Note that the requested lock size is unaffected by max_recv.
         */
        
        status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), 
-                        (SMB_BIG_UINT)numtoread, (SMB_BIG_UINT)startpos, WRITE_LOCK);
+                        (SMB_BIG_UINT)numtoread, (SMB_BIG_UINT)startpos, WRITE_LOCK, &my_lock_ctx);
 
        if (NT_STATUS_V(status)) {
-               if (lp_blocking_locks(SNUM(conn)) && ERROR_WAS_LOCK_DENIED(status)) {
+#if 0
+               /*
+                * We used to make lockread a blocking lock. It turns out
+                * that this isn't on W2k. Found by the Samba 4 RAW-READ torture
+                * tester. JRA.
+                */
+
+               if (lp_blocking_locks(SNUM(conn)) && !my_lock_ctx && ERROR_WAS_LOCK_DENIED(status)) {
                        /*
                         * A blocking lock was requested. Package up
                         * this smb into a queued request and push it
@@ -1628,10 +1882,21 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
                                return -1;
                        }
                }
+#endif
                END_PROFILE(SMBlockread);
                return ERROR_NT(status);
        }
 
+       /*
+        * However the requested READ size IS affected by max_recv. Insanity.... JRA.
+        */
+
+       if (numtoread > max_recv) {
+               DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
+Returning short read of maximum allowed for compatibility with Windows 2000.\n",
+                       (unsigned int)numtoread, (unsigned int)max_recv ));
+               numtoread = MIN(numtoread,max_recv);
+       }
        nread = read_file(fsp,data,startpos,numtoread);
 
        if (nread < 0) {
@@ -1673,6 +1938,16 @@ int reply_read(connection_struct *conn, char *inbuf,char *outbuf, int size, int
 
        outsize = set_message(outbuf,5,3,True);
        numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
+       /*
+        * The requested read size cannot be greater than max_recv. JRA.
+        */
+       if (numtoread > max_recv) {
+               DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
+Returning short read of maximum allowed for compatibility with Windows 2000.\n",
+                       (unsigned int)numtoread, (unsigned int)max_recv ));
+               numtoread = MIN(numtoread,max_recv);
+       }
+
        data = smb_buf(outbuf) + 3;
   
        if (is_locked(fsp,conn,(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK,False)) {
@@ -1722,7 +1997,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
                SMB_STRUCT_STAT sbuf;
                DATA_BLOB header;
 
-               if(vfs_fstat(fsp,fsp->fd, &sbuf) == -1)
+               if(SMB_VFS_FSTAT(fsp,fsp->fd, &sbuf) == -1)
                        return(UNIXERROR(ERRDOS,ERRnoaccess));
 
                if (startpos > sbuf.st_size)
@@ -1740,6 +2015,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
                 * correct amount of data).
                 */
 
+               SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
                SSVAL(outbuf,smb_vwv5,smb_maxcnt);
                SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
                SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
@@ -1749,7 +2025,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
                header.length = data - outbuf;
                header.free = NULL;
 
-               if ( conn->vfs_ops.sendfile( smbd_server_fd(), fsp, fsp->fd, &header, startpos, smb_maxcnt) == -1) {
+               if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fd, &header, startpos, smb_maxcnt) == -1) {
                        /*
                         * Special hack for broken Linux with no 64 bit clean sendfile. If we
                         * return ENOSYS then pretend we just got a normal read.
@@ -1778,6 +2054,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
                return(UNIXERROR(ERRDOS,ERRnoaccess));
        }
 
+       SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
        SSVAL(outbuf,smb_vwv5,nread);
        SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
        SSVAL(smb_buf(outbuf),-2,nread);
@@ -1869,6 +2146,10 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        int outsize = 0;
        START_PROFILE(SMBwritebraw);
 
+       if (srv_is_signing_active()) {
+               exit_server("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
+       }
+
        CHECK_FSP(fsp,conn);
        CHECK_WRITE(fsp);
   
@@ -1994,7 +2275,7 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        size_t numtowrite;
        SMB_OFF_T startpos;
        char *data;
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_OK;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        int outsize = 0;
        START_PROFILE(SMBwriteunlock);
@@ -2006,7 +2287,7 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
        data = smb_buf(inbuf) + 3;
   
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, 
+       if (numtowrite && is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, 
                      WRITE_LOCK,False)) {
                END_PROFILE(SMBwriteunlock);
                return ERROR_DOS(ERRDOS,ERRlock);
@@ -2028,11 +2309,13 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
                return(UNIXERROR(ERRHRD,ERRdiskfull));
        }
 
-       status = do_unlock(fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtowrite, 
-                          (SMB_BIG_UINT)startpos);
-       if (NT_STATUS_V(status)) {
-               END_PROFILE(SMBwriteunlock);
-               return ERROR_NT(status);
+       if (numtowrite) {
+               status = do_unlock(fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtowrite, 
+                                  (SMB_BIG_UINT)startpos);
+               if (NT_STATUS_V(status)) {
+                       END_PROFILE(SMBwriteunlock);
+                       return ERROR_NT(status);
+               }
        }
        
        outsize = set_message(outbuf,1,0,True);
@@ -2249,51 +2532,36 @@ int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int
        switch (mode) {
                case 0:
                        umode = SEEK_SET;
+                       res = startpos;
                        break;
                case 1:
                        umode = SEEK_CUR;
+                       res = fsp->pos + startpos;
                        break;
                case 2:
                        umode = SEEK_END;
                        break;
                default:
                        umode = SEEK_SET;
+                       res = startpos;
                        break;
        }
 
-       if((res = conn->vfs_ops.lseek(fsp,fsp->fd,startpos,umode)) == -1) {
-               /*
-                * Check for the special case where a seek before the start
-                * of the file sets the offset to zero. Added in the CIFS spec,
-                * section 4.2.7.
-                */
-
-               if(errno == EINVAL) {
-                       SMB_OFF_T current_pos = startpos;
-
-                       if(umode == SEEK_CUR) {
-
-                               if((current_pos = conn->vfs_ops.lseek(fsp,fsp->fd,0,SEEK_CUR)) == -1) {
-                                       END_PROFILE(SMBlseek);
-                                       return(UNIXERROR(ERRDOS,ERRnoaccess));
-                               }
-
-                               current_pos += startpos;
-
-                       } else if (umode == SEEK_END) {
-
+       if (umode == SEEK_END) {
+               if((res = SMB_VFS_LSEEK(fsp,fsp->fd,startpos,umode)) == -1) {
+                       if(errno == EINVAL) {
+                               SMB_OFF_T current_pos = startpos;
                                SMB_STRUCT_STAT sbuf;
 
-                               if(vfs_fstat(fsp,fsp->fd, &sbuf) == -1) {
+                               if(SMB_VFS_FSTAT(fsp,fsp->fd, &sbuf) == -1) {
                                        END_PROFILE(SMBlseek);
                                        return(UNIXERROR(ERRDOS,ERRnoaccess));
                                }
 
                                current_pos += sbuf.st_size;
+                               if(current_pos < 0)
+                                       res = SMB_VFS_LSEEK(fsp,fsp->fd,0,SEEK_SET);
                        }
-                       if(current_pos < 0)
-                               res = conn->vfs_ops.lseek(fsp,fsp->fd,0,SEEK_SET);
                }
 
                if(res == -1) {
@@ -2321,10 +2589,12 @@ int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int
 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
 {
        int outsize = set_message(outbuf,0,0,True);
+       uint16 fnum = SVAL(inbuf,smb_vwv0);
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        START_PROFILE(SMBflush);
 
-       CHECK_FSP(fsp,conn);
+       if (fnum != 0xFFFF)
+               CHECK_FSP(fsp,conn);
        
        if (!fsp) {
                file_sync_all(conn);
@@ -2346,6 +2616,9 @@ int reply_exit(connection_struct *conn,
 {
        int outsize;
        START_PROFILE(SMBexit);
+
+       file_close_pid(SVAL(inbuf,smb_pid));
+
        outsize = set_message(outbuf,0,0,True);
 
        DEBUG(3,("exit\n"));
@@ -2361,6 +2634,7 @@ int reply_exit(connection_struct *conn,
 int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
                 int dum_buffsize)
 {
+       extern struct current_user current_user;
        int outsize = 0;
        time_t mtime;
        int32 eclass = 0, err = 0;
@@ -2381,7 +2655,7 @@ int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
         * We can only use CHECK_FSP if we know it's not a directory.
         */
 
-       if(!fsp || (fsp->conn != conn)) {
+       if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
                END_PROFILE(SMBclose);
                return ERROR_DOS(ERRDOS,ERRbadfid);
        }
@@ -2464,7 +2738,7 @@ int reply_writeclose(connection_struct *conn,
        mtime = make_unix_date3(inbuf+smb_vwv4);
        data = smb_buf(inbuf) + 1;
   
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK,False)) {
+       if (numtowrite && is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK,False)) {
                END_PROFILE(SMBwriteclose);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -2473,7 +2747,16 @@ int reply_writeclose(connection_struct *conn,
 
        set_filetime(conn, fsp->fsp_name,mtime);
   
-       close_err = close_file(fsp,True);
+       /*
+        * More insanity. W2K only closes the file if writelen > 0.
+        * JRA.
+        */
+
+       if (numtowrite) {
+               DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
+                       fsp->fsp_name ));
+               close_err = close_file(fsp,True);
+       }
 
        DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
                 fsp->fnum, (int)numtowrite, (int)nwritten,
@@ -2508,6 +2791,8 @@ int reply_lock(connection_struct *conn,
        SMB_BIG_UINT count,offset;
        NTSTATUS status;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
+       BOOL my_lock_ctx = False;
+
        START_PROFILE(SMBlock);
 
        CHECK_FSP(fsp,conn);
@@ -2520,9 +2805,11 @@ int reply_lock(connection_struct *conn,
        DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
                 fsp->fd, fsp->fnum, (double)offset, (double)count));
 
-       status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK);
+       status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
        if (NT_STATUS_V(status)) {
-               if (lp_blocking_locks(SNUM(conn)) && ERROR_WAS_LOCK_DENIED(status)) {
+#if 0
+               /* Tests using Samba4 against W2K show this call never creates a blocking lock. */
+               if (lp_blocking_locks(SNUM(conn)) && !my_lock_ctx && ERROR_WAS_LOCK_DENIED(status)) {
                        /*
                         * A blocking lock was requested. Package up
                         * this smb into a queued request and push it
@@ -2533,6 +2820,7 @@ int reply_lock(connection_struct *conn,
                                return -1;
                        }
                }
+#endif
                END_PROFILE(SMBlock);
                return ERROR_NT(status);
        }
@@ -2612,7 +2900,11 @@ int reply_echo(connection_struct *conn,
        int outsize = set_message(outbuf,1,data_len,True);
        START_PROFILE(SMBecho);
 
-       data_len = MIN(data_len, (sizeof(inbuf)-(smb_buf(inbuf)-inbuf)));
+       if (data_len > BUFFER_SIZE) {
+               DEBUG(0,("reply_echo: data_len too large.\n"));
+               END_PROFILE(SMBecho);
+               return -1;
+       }
 
        /* copy any incoming data back out */
        if (data_len > 0)
@@ -2690,7 +2982,7 @@ int reply_printclose(connection_struct *conn,
 
        if (!CAN_PRINT(conn)) {
                END_PROFILE(SMBsplclose);
-               return ERROR_DOS(ERRDOS,ERRnoaccess);
+               return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
        }
   
        DEBUG(3,("printclose fd=%d fnum=%d\n",
@@ -2826,14 +3118,25 @@ NTSTATUS mkdir_internal(connection_struct *conn, pstring directory)
        int ret= -1;
        
        unix_convert(directory,conn,0,&bad_path,&sbuf);
-       
+
+       if( strchr_m(directory, ':')) {
+               return NT_STATUS_NOT_A_DIRECTORY;
+       }
+
+       if (ms_has_wild(directory)) {
+               return NT_STATUS_OBJECT_NAME_INVALID;
+       }
+
        if (check_name(directory, conn))
-               ret = vfs_mkdir(conn,directory,unix_mode(conn,aDIR,directory));
+               ret = vfs_MkDir(conn,directory,unix_mode(conn,aDIR,directory));
        
        if (ret == -1) {
-               NTSTATUS nterr = set_bad_path_error(errno, bad_path);
-               if (!NT_STATUS_IS_OK(nterr))
-                       return nterr;
+               if(errno == ENOENT) {
+                       if (bad_path)
+                               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                       else
+                               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               }
                return map_nt_error_from_unix(errno);
        }
        
@@ -2851,13 +3154,19 @@ int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        NTSTATUS status;
        START_PROFILE(SMBmkdir);
  
-       srvstr_pull_buf(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), STR_TERMINATE);
+       srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBmkdir);
+               return ERROR_NT(status);
+       }
 
        RESOLVE_DFSPATH(directory, conn, inbuf, outbuf);
 
        status = mkdir_internal(conn, directory);
-       if (!NT_STATUS_IS_OK(status))
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBmkdir);
                return ERROR_NT(status);
+       }
 
        outsize = set_message(outbuf,0,0,True);
 
@@ -2899,7 +3208,7 @@ static BOOL recursive_rmdir(connection_struct *conn, char *directory)
                pstrcat(fullname, "/");
                pstrcat(fullname, dname);
 
-               if(conn->vfs_ops.lstat(conn,fullname, &st) != 0) {
+               if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
                        ret = True;
                        break;
                }
@@ -2909,11 +3218,11 @@ static BOOL recursive_rmdir(connection_struct *conn, char *directory)
                                ret = True;
                                break;
                        }
-                       if(vfs_rmdir(conn,fullname) != 0) {
+                       if(SMB_VFS_RMDIR(conn,fullname) != 0) {
                                ret = True;
                                break;
                        }
-               } else if(vfs_unlink(conn,fullname) != 0) {
+               } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
                        ret = True;
                        break;
                }
@@ -2930,7 +3239,7 @@ BOOL rmdir_internals(connection_struct *conn, char *directory)
 {
        BOOL ok;
 
-       ok = (vfs_rmdir(conn,directory) == 0);
+       ok = (SMB_VFS_RMDIR(conn,directory) == 0);
        if(!ok && ((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
                /* 
                 * Check to see if the only thing in this directory are
@@ -2972,21 +3281,21 @@ BOOL rmdir_internals(connection_struct *conn, char *directory)
                                        pstrcat(fullname, "/");
                                        pstrcat(fullname, dname);
                      
-                                       if(conn->vfs_ops.lstat(conn,fullname, &st) != 0)
+                                       if(SMB_VFS_LSTAT(conn,fullname, &st) != 0)
                                                break;
                                        if(st.st_mode & S_IFDIR) {
                                                if(lp_recursive_veto_delete(SNUM(conn))) {
                                                        if(recursive_rmdir(conn, fullname) != 0)
                                                                break;
                                                }
-                                               if(vfs_rmdir(conn,fullname) != 0)
+                                               if(SMB_VFS_RMDIR(conn,fullname) != 0)
                                                        break;
-                                       } else if(vfs_unlink(conn,fullname) != 0)
+                                       } else if(SMB_VFS_UNLINK(conn,fullname) != 0)
                                                break;
                                }
                                CloseDir(dirptr);
                                /* Retry the rmdir */
-                               ok = (vfs_rmdir(conn,directory) == 0);
+                               ok = (SMB_VFS_RMDIR(conn,directory) == 0);
                        } else {
                                CloseDir(dirptr);
                        }
@@ -3012,9 +3321,14 @@ int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        BOOL ok = False;
        BOOL bad_path = False;
        SMB_STRUCT_STAT sbuf;
+       NTSTATUS status;
        START_PROFILE(SMBrmdir);
 
-       srvstr_pull_buf(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), STR_TERMINATE);
+       srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBrmdir);
+               return ERROR_NT(status);
+       }
 
        RESOLVE_DFSPATH(directory, conn, inbuf, outbuf)
 
@@ -3026,9 +3340,8 @@ int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        }
   
        if (!ok) {
-               set_bad_path_error(errno, bad_path);
                END_PROFILE(SMBrmdir);
-               return(UNIXERROR(ERRDOS,ERRbadpath));
+               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRbadpath);
        }
  
        outsize = set_message(outbuf,0,0,True);
@@ -3041,35 +3354,41 @@ int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
 /*******************************************************************
  Resolve wildcards in a filename rename.
+ Note that name is in UNIX charset and thus potentially can be more
+ than fstring buffer (255 bytes) especially in default UTF-8 case.
+ Therefore, we use pstring inside and all calls should ensure that
+ name2 is at least pstring-long (they do already)
 ********************************************************************/
 
-static BOOL resolve_wildcards(char *name1,char *name2)
+static BOOL resolve_wildcards(const char *name1, char *name2)
 {
-       fstring root1,root2;
-       fstring ext1,ext2;
-       char *p,*p2;
+       pstring root1,root2;
+       pstring ext1,ext2;
+       char *p,*p2, *pname1, *pname2;
+       int available_space, actual_space;
+       
 
-       name1 = strrchr_m(name1,'/');
-       name2 = strrchr_m(name2,'/');
+       pname1 = strrchr_m(name1,'/');
+       pname2 = strrchr_m(name2,'/');
 
-       if (!name1 || !name2)
+       if (!pname1 || !pname2)
                return(False);
   
-       fstrcpy(root1,name1);
-       fstrcpy(root2,name2);
+       pstrcpy(root1,pname1);
+       pstrcpy(root2,pname2);
        p = strrchr_m(root1,'.');
        if (p) {
                *p = 0;
-               fstrcpy(ext1,p+1);
+               pstrcpy(ext1,p+1);
        } else {
-               fstrcpy(ext1,"");    
+               pstrcpy(ext1,"");    
        }
        p = strrchr_m(root2,'.');
        if (p) {
                *p = 0;
-               fstrcpy(ext2,p+1);
+               pstrcpy(ext2,p+1);
        } else {
-               fstrcpy(ext2,"");    
+               pstrcpy(ext2,"");    
        }
 
        p = root1;
@@ -3098,38 +3417,204 @@ static BOOL resolve_wildcards(char *name1,char *name2)
                        p++;
        }
 
-       pstrcpy(name2,root2);
+       available_space = sizeof(pstring) - PTR_DIFF(pname2, name2);
+       
        if (ext2[0]) {
-               pstrcat(name2,".");
-               pstrcat(name2,ext2);
+               actual_space = snprintf(pname2, available_space - 1, "%s.%s", root2, ext2);
+               if (actual_space >= available_space - 1) {
+                       DEBUG(1,("resolve_wildcards: can't fit resolved name into specified buffer (overrun by %d bytes)\n",
+                               actual_space - available_space));
+               }
+       } else {
+               pstrcpy_base(pname2, root2, name2);
        }
 
        return(True);
 }
 
+/****************************************************************************
+ Ensure open files have their names updates.
+****************************************************************************/
+
+static void rename_open_files(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T inode, char *newname)
+{
+       files_struct *fsp;
+       BOOL did_rename = False;
+
+       for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
+               DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
+                       fsp->fnum, (unsigned int)fsp->dev, (double)fsp->inode,
+                       fsp->fsp_name, newname ));
+               string_set(&fsp->fsp_name, newname);
+               did_rename = True;
+       }
+
+       if (!did_rename)
+               DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
+                       (unsigned int)dev, (double)inode, newname ));
+}
+
+/****************************************************************************
+ Rename an open file - given an fsp.
+****************************************************************************/
+
+NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *newname, BOOL replace_if_exists)
+{
+       SMB_STRUCT_STAT sbuf;
+       BOOL bad_path = False;
+       pstring newname_last_component;
+       NTSTATUS error = NT_STATUS_OK;
+       BOOL dest_exists;
+       BOOL rcdest = True;
+
+       ZERO_STRUCT(sbuf);
+       rcdest = unix_convert(newname,conn,newname_last_component,&bad_path,&sbuf);
+
+       /* Quick check for "." and ".." */
+       if (!bad_path && newname_last_component[0] == '.') {
+               if (!newname_last_component[1] || (newname_last_component[1] == '.' && !newname_last_component[2])) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+       }
+       if (!rcdest && bad_path) {
+               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
+       }
+
+       /* Ensure newname contains a '/' */
+       if(strrchr_m(newname,'/') == 0) {
+               pstring tmpstr;
+               
+               pstrcpy(tmpstr, "./");
+               pstrcat(tmpstr, newname);
+               pstrcpy(newname, tmpstr);
+       }
+
+       /*
+        * Check for special case with case preserving and not
+        * case sensitive. If the old last component differs from the original
+        * last component only by case, then we should allow
+        * the rename (user is trying to change the case of the
+        * filename).
+        */
+
+       if((case_sensitive == False) && (case_preserve == True) &&
+                       strequal(newname, fsp->fsp_name)) {
+               char *p;
+               pstring newname_modified_last_component;
+
+               /*
+                * Get the last component of the modified name.
+                * Note that we guarantee that newname contains a '/'
+                * character above.
+                */
+               p = strrchr_m(newname,'/');
+               pstrcpy(newname_modified_last_component,p+1);
+                       
+               if(strcsequal(newname_modified_last_component, 
+                             newname_last_component) == False) {
+                       /*
+                        * Replace the modified last component with
+                        * the original.
+                        */
+                       pstrcpy(p+1, newname_last_component);
+               }
+       }
+
+       /*
+        * If the src and dest names are identical - including case,
+        * don't do the rename, just return success.
+        */
+
+       if (strcsequal(fsp->fsp_name, newname)) {
+               DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
+                       newname));
+               return NT_STATUS_OK;
+       }
+
+       dest_exists = vfs_object_exist(conn,newname,NULL);
+
+       if(!replace_if_exists && dest_exists) {
+               DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
+                       fsp->fsp_name,newname));
+               return NT_STATUS_OBJECT_NAME_COLLISION;
+       }
+
+       error = can_rename(newname,conn,&sbuf);
+
+       if (dest_exists && !NT_STATUS_IS_OK(error)) {
+               DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
+                       nt_errstr(error), fsp->fsp_name,newname));
+               if (NT_STATUS_EQUAL(error,NT_STATUS_SHARING_VIOLATION))
+                       error = NT_STATUS_ACCESS_DENIED;
+               return error;
+       }
+
+       if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
+               DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
+                       fsp->fsp_name,newname));
+               rename_open_files(conn, fsp->dev, fsp->inode, newname);
+               return NT_STATUS_OK;    
+       }
+
+       if (errno == ENOTDIR || errno == EISDIR)
+               error = NT_STATUS_OBJECT_NAME_COLLISION;
+       else
+               error = map_nt_error_from_unix(errno);
+               
+       DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
+               nt_errstr(error), fsp->fsp_name,newname));
+
+       return error;
+}
+
 /****************************************************************************
  The guts of the rename command, split out so it may be called by the NT SMB
  code. 
 ****************************************************************************/
 
-NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, BOOL replace_if_exists)
+NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, uint16 attrs, BOOL replace_if_exists)
 {
        pstring directory;
        pstring mask;
-       pstring newname_last_component;
+       pstring last_component_src;
+       pstring last_component_dest;
        char *p;
        BOOL has_wild;
-       BOOL bad_path1 = False;
-       BOOL bad_path2 = False;
+       BOOL bad_path_src = False;
+       BOOL bad_path_dest = False;
        int count=0;
        NTSTATUS error = NT_STATUS_OK;
        BOOL rc = True;
+       BOOL rcdest = True;
        SMB_STRUCT_STAT sbuf1, sbuf2;
 
        *directory = *mask = 0;
 
-       rc = unix_convert(name,conn,0,&bad_path1,&sbuf1);
-       unix_convert(newname,conn,newname_last_component,&bad_path2,&sbuf2);
+       ZERO_STRUCT(sbuf1);
+       ZERO_STRUCT(sbuf2);
+
+       rc = unix_convert(name,conn,last_component_src,&bad_path_src,&sbuf1);
+       if (!rc && bad_path_src) {
+               if (ms_has_wild(last_component_src))
+                       return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
+       }
+
+       /* Quick check for "." and ".." */
+       if (last_component_src[0] == '.') {
+               if (!last_component_src[1] || (last_component_src[1] == '.' && !last_component_src[2])) {
+                       return NT_STATUS_OBJECT_NAME_INVALID;
+               }
+       }
+
+       rcdest = unix_convert(newname,conn,last_component_dest,&bad_path_dest,&sbuf2);
+
+       /* Quick check for "." and ".." */
+       if (last_component_dest[0] == '.') {
+               if (!last_component_dest[1] || (last_component_dest[1] == '.' && !last_component_dest[2])) {
+                       return NT_STATUS_OBJECT_NAME_INVALID;
+               }
+       }
 
        /*
         * Split the old name into directory and last component
@@ -3139,7 +3624,7 @@ NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, BO
         * name and newname contain a / character or neither of them do
         * as this is checked in resolve_wildcards().
         */
-       
+
        p = strrchr_m(name,'/');
        if (!p) {
                pstrcpy(directory,".");
@@ -3185,9 +3670,9 @@ NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, BO
                }
                
                DEBUG(3,("rename_internals: case_sensitive = %d, case_preserve = %d, short case preserve = %d, \
-directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n", 
+directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n", 
                         case_sensitive, case_preserve, short_case_preserve, directory, 
-                        newname, newname_last_component, is_short_name));
+                        newname, last_component_dest, is_short_name));
 
                /*
                 * Check for special case with case preserving and not
@@ -3203,7 +3688,7 @@ directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n",
                    ((short_case_preserve == True) && 
                     (is_short_name == True))) &&
                   strcsequal(directory, newname)) {
-                       pstring newname_modified_last_component;
+                       pstring modified_last_component;
 
                        /*
                         * Get the last component of the modified name.
@@ -3211,15 +3696,15 @@ directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n",
                         * character above.
                         */
                        p = strrchr_m(newname,'/');
-                       pstrcpy(newname_modified_last_component,p+1);
+                       pstrcpy(modified_last_component,p+1);
                        
-                       if(strcsequal(newname_modified_last_component, 
-                                     newname_last_component) == False) {
+                       if(strcsequal(modified_last_component, 
+                                     last_component_dest) == False) {
                                /*
                                 * Replace the modified last component with
                                 * the original.
                                 */
-                               pstrcpy(p+1, newname_last_component);
+                               pstrcpy(p+1, last_component_dest);
                        }
                }
        
@@ -3254,6 +3739,12 @@ directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n",
                        return error;
                }
 
+               if (!rcdest && bad_path_dest) {
+                       if (ms_has_wild(last_component_dest))
+                               return NT_STATUS_OBJECT_NAME_INVALID;
+                       return NT_STATUS_OBJECT_PATH_NOT_FOUND;
+               }
+
                error = can_rename(directory,conn,&sbuf1);
 
                if (!NT_STATUS_IS_OK(error)) {
@@ -3268,6 +3759,7 @@ directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n",
                 */
 
                if (strcsequal(directory, newname)) {
+                       rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
                        DEBUG(3,("rename_internals: identical names in rename %s - returning success\n", directory));
                        return NT_STATUS_OK;
                }
@@ -3278,9 +3770,10 @@ directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n",
                        return NT_STATUS_OBJECT_NAME_COLLISION;
                }
 
-               if(conn->vfs_ops.rename(conn,directory, newname) == 0) {
+               if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
                        DEBUG(3,("rename_internals: succeeded doing rename on %s -> %s\n",
                                directory,newname));
+                       rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
                        return NT_STATUS_OK;    
                }
 
@@ -3305,19 +3798,37 @@ directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n",
                        dirptr = OpenDir(conn, directory, True);
                
                if (dirptr) {
-                       error = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+                       error = NT_STATUS_NO_SUCH_FILE;
+/*                     Was error = NT_STATUS_OBJECT_NAME_NOT_FOUND; - gentest fix. JRA */
                        
                        if (strequal(mask,"????????.???"))
                                pstrcpy(mask,"*");
                        
                        while ((dname = ReadDirName(dirptr))) {
                                pstring fname;
+                               BOOL sysdir_entry = False;
 
                                pstrcpy(fname,dname);
                                
+                               /* Quick check for "." and ".." */
+                               if (fname[0] == '.') {
+                                       if (!fname[1] || (fname[1] == '.' && !fname[2])) {
+                                               if (attrs & aDIR) {
+                                                       sysdir_entry = True;
+                                               } else {
+                                                       continue;
+                                               }
+                                       }
+                               }
+
                                if(!mask_match(fname, mask, case_sensitive))
                                        continue;
                                
+                               if (sysdir_entry) {
+                                       error = NT_STATUS_OBJECT_NAME_INVALID;
+                                       continue;
+                               }
+
                                error = NT_STATUS_ACCESS_DENIED;
                                slprintf(fname,sizeof(fname)-1,"%s/%s",directory,dname);
                                if (!vfs_object_exist(conn, fname, &sbuf1)) {
@@ -3345,12 +3856,23 @@ directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n",
                                        continue;
                                }
                                
-                               if (!conn->vfs_ops.rename(conn,fname,destname))
+                               if (!SMB_VFS_RENAME(conn,fname,destname)) {
+                                       rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
                                        count++;
+                                       error = NT_STATUS_OK;
+                               }
                                DEBUG(3,("rename_internals: doing rename on %s -> %s\n",fname,destname));
                        }
                        CloseDir(dirptr);
                }
+
+               if (!NT_STATUS_EQUAL(error,NT_STATUS_NO_SUCH_FILE)) {
+                       if (!rcdest && bad_path_dest) {
+                               if (ms_has_wild(last_component_dest))
+                                       return NT_STATUS_OBJECT_NAME_INVALID;
+                               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                       }
+               }
        }
        
        if (count == 0 && NT_STATUS_IS_OK(error)) {
@@ -3371,22 +3893,32 @@ int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        pstring name;
        pstring newname;
        char *p;
+       uint16 attrs = SVAL(inbuf,smb_vwv0);
        NTSTATUS status;
 
        START_PROFILE(SMBmv);
 
        p = smb_buf(inbuf) + 1;
-       p += srvstr_pull_buf(inbuf, name, p, sizeof(name), STR_TERMINATE);
+       p += srvstr_get_path(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBmv);
+               return ERROR_NT(status);
+       }
        p++;
-       p += srvstr_pull_buf(inbuf, newname, p, sizeof(newname), STR_TERMINATE);
+       p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBmv);
+               return ERROR_NT(status);
+       }
        
        RESOLVE_DFSPATH(name, conn, inbuf, outbuf);
        RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
        
        DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
        
-       status = rename_internals(conn, name, newname, False);
+       status = rename_internals(conn, name, newname, attrs, False);
        if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBmv);
                return ERROR_NT(status);
        }
 
@@ -3439,7 +3971,7 @@ static BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
        if (!target_is_directory && count)
                ofun = FILE_EXISTS_OPEN;
 
-       if (vfs_stat(conn,dest,&sbuf2) == -1)
+       if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1)
                ZERO_STRUCTP(&sbuf2);
 
        fsp2 = open_file_shared(conn,dest,&sbuf2,SET_DENY_MODE(DENY_NONE)|SET_OPEN_MODE(DOS_OPEN_WRONLY),
@@ -3451,7 +3983,7 @@ static BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
        }
 
        if ((ofun&3) == 1) {
-               if(conn->vfs_ops.lseek(fsp2,fsp2->fd,0,SEEK_END) == -1) {
+               if(SMB_VFS_LSEEK(fsp2,fsp2->fd,0,SEEK_END) == -1) {
                        DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
                        /*
                         * Stop the copy from occurring.
@@ -3504,14 +4036,23 @@ int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        BOOL bad_path2 = False;
        BOOL rc = True;
        SMB_STRUCT_STAT sbuf1, sbuf2;
+       NTSTATUS status;
 
        START_PROFILE(SMBcopy);
 
        *directory = *mask = 0;
 
        p = smb_buf(inbuf);
-       p += srvstr_pull_buf(inbuf, name, p, sizeof(name), STR_TERMINATE);
-       p += srvstr_pull_buf(inbuf, newname, p, sizeof(newname), STR_TERMINATE);
+       p += srvstr_get_path(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBcopy);
+               return ERROR_NT(status);
+       }
+       p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBcopy);
+               return ERROR_NT(status);
+       }
    
        DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
    
@@ -3657,6 +4198,7 @@ int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        int outsize = 0;
        BOOL ok = False;
        pstring newdir;
+       NTSTATUS status;
 
        START_PROFILE(pathworks_setdir);
   
@@ -3666,7 +4208,11 @@ int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                return ERROR_DOS(ERRDOS,ERRnoaccess);
        }
 
-       srvstr_pull_buf(inbuf, newdir, smb_buf(inbuf) + 1, sizeof(newdir), STR_TERMINATE);
+       srvstr_get_path(inbuf, newdir, smb_buf(inbuf) + 1, sizeof(newdir), 0, STR_TERMINATE, &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(pathworks_setdir);
+               return ERROR_NT(status);
+       }
   
        if (strlen(newdir) == 0) {
                ok = True;
@@ -3841,6 +4387,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf,char *outbuf,int length,
        char *data;
        BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
        BOOL err;
+       BOOL my_lock_ctx = False;
        NTSTATUS status;
 
        START_PROFILE(SMBlockingX);
@@ -3961,8 +4508,15 @@ no oplock granted on this file (%s).\n", fsp->fnum, fsp->fsp_name));
                        fsp->fsp_name, (int)lock_timeout ));
                
                status = do_lock_spin(fsp,conn,lock_pid, count,offset, 
-                                ((locktype & 1) ? READ_LOCK : WRITE_LOCK));
+                                ((locktype & 1) ? READ_LOCK : WRITE_LOCK), &my_lock_ctx);
                if (NT_STATUS_V(status)) {
+                       /*
+                        * Interesting fact found by IFSTEST /t LockOverlappedTest...
+                        * Even if it's our own lock context, we need to wait here as
+                        * there may be an unlock on the way.
+                        * So I removed a "&& !my_lock_ctx" from the following
+                        * if statement. JRA.
+                        */
                        if ((lock_timeout != 0) && lp_blocking_locks(SNUM(conn)) && ERROR_WAS_LOCK_DENIED(status)) {
                                /*
                                 * A blocking lock was requested. Package up
@@ -4379,8 +4933,9 @@ int reply_getattrE(connection_struct *conn, char *inbuf,char *outbuf, int size,
                SIVAL(outbuf,smb_vwv6,0);
                SIVAL(outbuf,smb_vwv8,0);
        } else {
+               uint32 allocation_size = get_allocation_size(fsp, &sbuf);
                SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
-               SIVAL(outbuf,smb_vwv8,SMB_ROUNDUP(sbuf.st_size,1024));
+               SIVAL(outbuf,smb_vwv8,allocation_size);
        }
        SSVAL(outbuf,smb_vwv10, mode);