Implement rename/move in SMB2 from Windows7.
[ira/wip.git] / source3 / smbd / trans2.c
index 856fd9432d072cc924b6eddffe6815a9e9b13692..eff5fba6760649b73e9fb3ac14876aa9561ed8d7 100644 (file)
@@ -28,8 +28,6 @@
 #include "smbd/globals.h"
 #include "../libcli/auth/libcli_auth.h"
 
-extern enum protocol_types Protocol;
-
 #define DIR_ENTRY_SAFETY_MARGIN 4096
 
 static char *store_file_unix_basic(connection_struct *conn,
@@ -72,6 +70,8 @@ static bool samba_private_attr_name(const char *unix_ea_name)
        static const char * const prohibited_ea_names[] = {
                SAMBA_POSIX_INHERITANCE_EA_NAME,
                SAMBA_XATTR_DOS_ATTRIB,
+               SAMBA_XATTR_MARKER,
+               XATTR_NTACL_NAME,
                NULL
        };
 
@@ -155,7 +155,9 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
        ssize_t sizeret = -1;
 
        if (!lp_ea_support(SNUM(conn))) {
-               *pnames = NULL;
+               if (pnames) {
+                       *pnames = NULL;
+               }
                *pnum_names = 0;
                return NT_STATUS_OK;
        }
@@ -206,7 +208,9 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
 
        if (sizeret == 0) {
                TALLOC_FREE(names);
-               *pnames = NULL;
+               if (pnames) {
+                       *pnames = NULL;
+               }
                *pnum_names = 0;
                return NT_STATUS_OK;
        }
@@ -243,7 +247,11 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
                names[num_names++] = p;
        }
 
-       *pnames = names;
+       if (pnames) {
+               *pnames = names;
+       } else {
+               TALLOC_FREE(names);
+       }
        *pnum_names = num_names;
        return NT_STATUS_OK;
 }
@@ -1004,7 +1012,7 @@ static void call_trans2open(connection_struct *conn,
        pname = &params[28];
 
        if (IS_IPC(conn)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
                goto out;
        }
 
@@ -1024,6 +1032,8 @@ static void call_trans2open(connection_struct *conn,
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
+                               0,
+                               NULL,
                                &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
@@ -1045,7 +1055,7 @@ static void call_trans2open(connection_struct *conn,
                                         &access_mask, &share_mode,
                                         &create_disposition,
                                         &create_options)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -1111,7 +1121,7 @@ static void call_trans2open(connection_struct *conn,
        inode = smb_fname->st.st_ex_ino;
        if (fattr & aDIR) {
                close_file(req, fsp, ERROR_CLOSE);
-               reply_doserror(req, ERRDOS,ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -1163,19 +1173,24 @@ static void call_trans2open(connection_struct *conn,
  Case can be significant or not.
 **********************************************************/
 
-static bool exact_match(connection_struct *conn,
-               const char *str,
-               const char *mask)
+static bool exact_match(bool has_wild,
+                       bool case_sensitive,
+                       const char *str,
+                       const char *mask)
 {
-       if (mask[0] == '.' && mask[1] == 0)
-               return False;
-       if (dptr_has_wild(conn->dirptr)) {
-               return False;
+       if (mask[0] == '.' && mask[1] == 0) {
+               return false;
+       }
+
+       if (has_wild) {
+               return false;
        }
-       if (conn->case_sensitive)
+
+       if (case_sensitive) {
                return strcmp(str,mask)==0;
-       else
+       } else {
                return StrCaseCmp(str,mask) == 0;
+       }
 }
 
 /****************************************************************************
@@ -1315,671 +1330,699 @@ static bool check_msdfs_link(connection_struct *conn,
  Get a level dependent lanman2 dir entry.
 ****************************************************************************/
 
-static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
-                               connection_struct *conn,
-                               uint16 flags2,
-                               const char *path_mask,
-                               uint32 dirtype,
-                               int info_level,
-                               int requires_resume_key,
-                               bool dont_descend,
-                               bool ask_sharemode,
-                               char **ppdata,
-                               char *base_data,
-                               char *end_data,
-                               int space_remaining,
-                               bool *out_of_space,
-                               bool *got_exact_match,
-                               int *last_entry_off,
-                               struct ea_list *name_list)
+struct smbd_dirptr_lanman2_state {
+       connection_struct *conn;
+       uint32_t info_level;
+       bool check_mangled_names;
+       bool has_wild;
+       bool got_exact_match;
+};
+
+static bool smbd_dirptr_lanman2_match_fn(TALLOC_CTX *ctx,
+                                        void *private_data,
+                                        const char *dname,
+                                        const char *mask,
+                                        char **_fname)
 {
-       char *dname;
-       bool found = False;
-       SMB_STRUCT_STAT sbuf;
-       const char *mask = NULL;
-       char *pathreal = NULL;
-       char *fname = NULL;
-       char *p, *q, *pdata = *ppdata;
-       uint32 reskey=0;
-       long prev_dirpos=0;
-       uint32 mode=0;
-       SMB_OFF_T file_size = 0;
-       uint64_t allocation_size = 0;
-       uint32 len;
-       struct timespec mdate_ts, adate_ts, create_date_ts;
-       time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
-       char *nameptr;
-       char *last_entry_ptr;
-       bool was_8_3;
-       uint32 nt_extmode; /* Used for NT connections instead of mode */
-       bool needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
-       bool check_mangled_names = lp_manglednames(conn->params);
+       struct smbd_dirptr_lanman2_state *state =
+               (struct smbd_dirptr_lanman2_state *)private_data;
+       bool ok;
        char mangled_name[13]; /* mangled 8.3 name. */
+       bool got_match;
+       const char *fname;
 
-       *out_of_space = False;
-       *got_exact_match = False;
-
-       ZERO_STRUCT(mdate_ts);
-       ZERO_STRUCT(adate_ts);
-       ZERO_STRUCT(create_date_ts);
-
-       if (!conn->dirptr) {
-               return(False);
-       }
-
-       p = strrchr_m(path_mask,'/');
-       if(p != NULL) {
-               if(p[1] == '\0') {
-                       mask = talloc_strdup(ctx,"*.*");
-               } else {
-                       mask = p+1;
+       /* Mangle fname if it's an illegal name. */
+       if (mangle_must_mangle(dname, state->conn->params)) {
+               ok = name_to_8_3(dname, mangled_name,
+                                true, state->conn->params);
+               if (!ok) {
+                       return false;
                }
+               fname = mangled_name;
        } else {
-               mask = path_mask;
+               fname = dname;
        }
 
-       while (!found) {
-               bool got_match;
-               bool ms_dfs_link = False;
-
-               /* Needed if we run out of space */
-               long curr_dirpos = prev_dirpos = dptr_TellDir(conn->dirptr);
-               dname = dptr_ReadDirName(ctx,conn->dirptr,&curr_dirpos,&sbuf);
-
-               /*
-                * Due to bugs in NT client redirectors we are not using
-                * resume keys any more - set them to zero.
-                * Check out the related comments in findfirst/findnext.
-                * JRA.
-                */
-
-               reskey = 0;
-
-               DEBUG(8,("get_lanman2_dir_entry:readdir on dirptr 0x%lx now at offset %ld\n",
-                       (long)conn->dirptr,curr_dirpos));
-
-               if (!dname) {
-                       return(False);
-               }
+       got_match = exact_match(state->has_wild,
+                               state->conn->case_sensitive,
+                               fname, mask);
+       state->got_exact_match = got_match;
+       if (!got_match) {
+               got_match = mask_match(fname, mask,
+                                      state->conn->case_sensitive);
+       }
 
+       if(!got_match && state->check_mangled_names &&
+          !mangle_is_8_3(fname, false, state->conn->params)) {
                /*
-                * fname may get mangled, dname is never mangled.
-                * Whenever we're accessing the filesystem we use
-                * pathreal which is composed from dname.
+                * It turns out that NT matches wildcards against
+                * both long *and* short names. This may explain some
+                * of the wildcard wierdness from old DOS clients
+                * that some people have been seeing.... JRA.
                 */
-
-               pathreal = NULL;
-               fname = dname;
-
-               /* Mangle fname if it's an illegal name. */
-               if (mangle_must_mangle(dname,conn->params)) {
-                       if (!name_to_8_3(dname,mangled_name,True,conn->params)) {
-                               TALLOC_FREE(fname);
-                               continue; /* Error - couldn't mangle. */
-                       }
-                       fname = talloc_strdup(ctx, mangled_name);
-                       if (!fname) {
-                               return False;
-                       }
+               /* Force the mangling into 8.3. */
+               ok = name_to_8_3(fname, mangled_name,
+                                false, state->conn->params);
+               if (!ok) {
+                       return false;
                }
 
-               if(!(got_match = *got_exact_match = exact_match(conn, fname, mask))) {
-                       got_match = mask_match(fname, mask, conn->case_sensitive);
+               got_match = exact_match(state->has_wild,
+                                       state->conn->case_sensitive,
+                                       mangled_name, mask);
+               state->got_exact_match = got_match;
+               if (!got_match) {
+                       got_match = mask_match(mangled_name, mask,
+                                              state->conn->case_sensitive);
                }
+       }
 
-               if(!got_match && check_mangled_names &&
-                  !mangle_is_8_3(fname, False, conn->params)) {
-                       /*
-                        * It turns out that NT matches wildcards against
-                        * both long *and* short names. This may explain some
-                        * of the wildcard wierdness from old DOS clients
-                        * that some people have been seeing.... JRA.
-                        */
-                       /* Force the mangling into 8.3. */
-                       if (!name_to_8_3( fname, mangled_name, False, conn->params)) {
-                               TALLOC_FREE(fname);
-                               continue; /* Error - couldn't mangle. */
-                       }
-
-                       if(!(got_match = *got_exact_match = exact_match(conn, mangled_name, mask))) {
-                               got_match = mask_match(mangled_name, mask, conn->case_sensitive);
-                       }
-               }
-
-               if (got_match) {
-                       bool isdots = (ISDOT(dname) || ISDOTDOT(dname));
-                       struct smb_filename *smb_fname = NULL;
-                       NTSTATUS status;
-
-                       if (dont_descend && !isdots) {
-                               TALLOC_FREE(fname);
-                               continue;
-                       }
-
-                       if (needslash) {
-                               pathreal = NULL;
-                               pathreal = talloc_asprintf(ctx,
-                                       "%s/%s",
-                                       conn->dirpath,
-                                       dname);
-                       } else {
-                               pathreal = talloc_asprintf(ctx,
-                                       "%s%s",
-                                       conn->dirpath,
-                                       dname);
-                       }
+       if (!got_match) {
+               return false;
+       }
 
-                       if (!pathreal) {
-                               TALLOC_FREE(fname);
-                               return False;
-                       }
+       *_fname = talloc_strdup(ctx, fname);
+       if (*_fname == NULL) {
+               return false;
+       }
 
-                       /* A dirent from dptr_ReadDirName isn't a stream. */
-                       status = create_synthetic_smb_fname(ctx, pathreal,
-                                                           NULL, &sbuf,
-                                                           &smb_fname);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               TALLOC_FREE(fname);
-                               return false;
-                       }
+       return true;
+}
 
-                       if (INFO_LEVEL_IS_UNIX(info_level)) {
-                               if (SMB_VFS_LSTAT(conn, smb_fname) != 0) {
-                                       DEBUG(5,("get_lanman2_dir_entry: "
-                                                "Couldn't lstat [%s] (%s)\n",
-                                                smb_fname_str_dbg(smb_fname),
-                                                strerror(errno)));
-                                       TALLOC_FREE(smb_fname);
-                                       TALLOC_FREE(pathreal);
-                                       TALLOC_FREE(fname);
-                                       continue;
-                               }
-                       } else if (!VALID_STAT(smb_fname->st) &&
-                                  SMB_VFS_STAT(conn, smb_fname) != 0) {
-                               /* Needed to show the msdfs symlinks as
-                                * directories */
-
-                               ms_dfs_link =
-                                   check_msdfs_link(conn,
-                                                    smb_fname->base_name,
-                                                    &smb_fname->st);
-                               if (!ms_dfs_link) {
-                                       DEBUG(5,("get_lanman2_dir_entry: "
-                                                "Couldn't stat [%s] (%s)\n",
-                                                smb_fname_str_dbg(smb_fname),
-                                                strerror(errno)));
-                                       TALLOC_FREE(smb_fname);
-                                       TALLOC_FREE(pathreal);
-                                       TALLOC_FREE(fname);
-                                       continue;
-                               }
-                       }
+static bool smbd_dirptr_lanman2_mode_fn(TALLOC_CTX *ctx,
+                                       void *private_data,
+                                       struct smb_filename *smb_fname,
+                                       uint32_t *_mode)
+{
+       struct smbd_dirptr_lanman2_state *state =
+               (struct smbd_dirptr_lanman2_state *)private_data;
+       bool ms_dfs_link = false;
+       uint32_t mode = 0;
 
-                       if (ms_dfs_link) {
-                               mode = dos_mode_msdfs(conn, smb_fname);
-                       } else {
-                               mode = dos_mode(conn, smb_fname);
-                       }
+       if (INFO_LEVEL_IS_UNIX(state->info_level)) {
+               if (SMB_VFS_LSTAT(state->conn, smb_fname) != 0) {
+                       DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
+                                "Couldn't lstat [%s] (%s)\n",
+                                smb_fname_str_dbg(smb_fname),
+                                strerror(errno)));
+                       return false;
+               }
+       } else if (!VALID_STAT(smb_fname->st) &&
+                  SMB_VFS_STAT(state->conn, smb_fname) != 0) {
+               /* Needed to show the msdfs symlinks as
+                * directories */
+
+               ms_dfs_link = check_msdfs_link(state->conn,
+                                              smb_fname->base_name,
+                                              &smb_fname->st);
+               if (!ms_dfs_link) {
+                       DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
+                                "Couldn't stat [%s] (%s)\n",
+                                smb_fname_str_dbg(smb_fname),
+                                strerror(errno)));
+                       return false;
+               }
+       }
 
-                       if (!dir_check_ftype(conn,mode,dirtype)) {
-                               DEBUG(5,("get_lanman2_dir_entry: [%s] attribs didn't match %x\n",fname,dirtype));
-                               TALLOC_FREE(smb_fname);
-                               TALLOC_FREE(pathreal);
-                               TALLOC_FREE(fname);
-                               continue;
-                       }
+       if (ms_dfs_link) {
+               mode = dos_mode_msdfs(state->conn, smb_fname);
+       } else {
+               mode = dos_mode(state->conn, smb_fname);
+       }
 
-                       if (!(mode & aDIR)) {
-                               file_size = get_file_size_stat(&smb_fname->st);
-                       }
-                       allocation_size =
-                           SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
-
-                       if (ask_sharemode) {
-                               struct timespec write_time_ts;
-                               struct file_id fileid;
-
-                               ZERO_STRUCT(write_time_ts);
-                               fileid = vfs_file_id_from_sbuf(conn,
-                                                              &smb_fname->st);
-                               get_file_infos(fileid, NULL, &write_time_ts);
-                               if (!null_timespec(write_time_ts)) {
-                                       update_stat_ex_mtime(&smb_fname->st,
-                                                            write_time_ts);
-                               }
-                       }
+       *_mode = mode;
+       return true;
+}
 
-                       mdate_ts = smb_fname->st.st_ex_mtime;
-                       adate_ts = smb_fname->st.st_ex_atime;
-                       create_date_ts = smb_fname->st.st_ex_btime;
+static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
+                                   connection_struct *conn,
+                                   uint16_t flags2,
+                                   uint32_t info_level,
+                                   struct ea_list *name_list,
+                                   bool check_mangled_names,
+                                   bool requires_resume_key,
+                                   uint32_t mode,
+                                   const char *fname,
+                                   const struct smb_filename *smb_fname,
+                                   uint64_t space_remaining,
+                                   uint8_t align,
+                                   bool do_pad,
+                                   char *base_data,
+                                   char **ppdata,
+                                   char *end_data,
+                                   bool *out_of_space,
+                                   uint64_t *last_entry_off)
+{
+       char *p, *q, *pdata = *ppdata;
+       uint32_t reskey=0;
+       uint64_t file_size = 0;
+       uint64_t allocation_size = 0;
+       uint32_t len;
+       struct timespec mdate_ts, adate_ts, cdate_ts, create_date_ts;
+       time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
+       time_t c_date = (time_t)0;
+       char *nameptr;
+       char *last_entry_ptr;
+       bool was_8_3;
+       off_t off;
+       off_t pad = 0;
 
-                       if (lp_dos_filetime_resolution(SNUM(conn))) {
-                               dos_filetime_timespec(&create_date_ts);
-                               dos_filetime_timespec(&mdate_ts);
-                               dos_filetime_timespec(&adate_ts);
-                       }
+       *out_of_space = false;
 
-                       create_date = convert_timespec_to_time_t(create_date_ts);
-                       mdate = convert_timespec_to_time_t(mdate_ts);
-                       adate = convert_timespec_to_time_t(adate_ts);
+       ZERO_STRUCT(mdate_ts);
+       ZERO_STRUCT(adate_ts);
+       ZERO_STRUCT(create_date_ts);
+       ZERO_STRUCT(cdate_ts);
 
-                       DEBUG(5,("get_lanman2_dir_entry: found %s fname=%s\n",
-                                smb_fname_str_dbg(smb_fname), fname));
+       if (!(mode & aDIR)) {
+               file_size = get_file_size_stat(&smb_fname->st);
+       }
+       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
 
-                       found = True;
+       mdate_ts = smb_fname->st.st_ex_mtime;
+       adate_ts = smb_fname->st.st_ex_atime;
+       create_date_ts = get_create_timespec(conn, NULL, smb_fname);
+       cdate_ts = get_change_timespec(conn, NULL, smb_fname);
 
-                       dptr_DirCacheAdd(conn->dirptr, dname, curr_dirpos);
-                       sbuf = smb_fname->st;
+       if (lp_dos_filetime_resolution(SNUM(conn))) {
+               dos_filetime_timespec(&create_date_ts);
+               dos_filetime_timespec(&mdate_ts);
+               dos_filetime_timespec(&adate_ts);
+               dos_filetime_timespec(&cdate_ts);
+       }
 
-                       TALLOC_FREE(smb_fname);
-               }
+       create_date = convert_timespec_to_time_t(create_date_ts);
+       mdate = convert_timespec_to_time_t(mdate_ts);
+       adate = convert_timespec_to_time_t(adate_ts);
+       c_date = convert_timespec_to_time_t(cdate_ts);
 
-               if (!found)
-                       TALLOC_FREE(fname);
+       /* align the record */
+       off = PTR_DIFF(pdata, base_data);
+       pad = (off + (align-1)) & ~(align-1);
+       pad -= off;
+       off += pad;
+       /* initialize padding to 0 */
+       if (pad) {
+               memset(pdata, 0, pad);
        }
+       space_remaining -= pad;
 
+       pdata += pad;
        p = pdata;
        last_entry_ptr = p;
 
-       nt_extmode = mode ? mode : FILE_ATTRIBUTE_NORMAL;
+       pad = 0;
+       off = 0;
 
        switch (info_level) {
-               case SMB_FIND_INFO_STANDARD:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_INFO_STANDARD\n"));
-                       if(requires_resume_key) {
-                               SIVAL(p,0,reskey);
-                               p += 4;
-                       }
-                       srv_put_dos_date2(p,0,create_date);
-                       srv_put_dos_date2(p,4,adate);
-                       srv_put_dos_date2(p,8,mdate);
-                       SIVAL(p,12,(uint32)file_size);
-                       SIVAL(p,16,(uint32)allocation_size);
-                       SSVAL(p,20,mode);
-                       p += 23;
-                       nameptr = p;
-                       if (flags2 & FLAGS2_UNICODE_STRINGS) {
-                               p += ucs2_align(base_data, p, 0);
-                       }
-                       len = srvstr_push(base_data, flags2, p,
-                                         fname, PTR_DIFF(end_data, p),
-                                         STR_TERMINATE);
-                       if (flags2 & FLAGS2_UNICODE_STRINGS) {
-                               if (len > 2) {
-                                       SCVAL(nameptr, -1, len - 2);
-                               } else {
-                                       SCVAL(nameptr, -1, 0);
-                               }
+       case SMB_FIND_INFO_STANDARD:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_INFO_STANDARD\n"));
+               if(requires_resume_key) {
+                       SIVAL(p,0,reskey);
+                       p += 4;
+               }
+               srv_put_dos_date2(p,0,create_date);
+               srv_put_dos_date2(p,4,adate);
+               srv_put_dos_date2(p,8,mdate);
+               SIVAL(p,12,(uint32)file_size);
+               SIVAL(p,16,(uint32)allocation_size);
+               SSVAL(p,20,mode);
+               p += 23;
+               nameptr = p;
+               if (flags2 & FLAGS2_UNICODE_STRINGS) {
+                       p += ucs2_align(base_data, p, 0);
+               }
+               len = srvstr_push(base_data, flags2, p,
+                                 fname, PTR_DIFF(end_data, p),
+                                 STR_TERMINATE);
+               if (flags2 & FLAGS2_UNICODE_STRINGS) {
+                       if (len > 2) {
+                               SCVAL(nameptr, -1, len - 2);
                        } else {
-                               if (len > 1) {
-                                       SCVAL(nameptr, -1, len - 1);
-                               } else {
-                                       SCVAL(nameptr, -1, 0);
-                               }
-                       }
-                       p += len;
-                       break;
-
-               case SMB_FIND_EA_SIZE:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_SIZE\n"));
-                       if(requires_resume_key) {
-                               SIVAL(p,0,reskey);
-                               p += 4;
+                               SCVAL(nameptr, -1, 0);
                        }
-                       srv_put_dos_date2(p,0,create_date);
-                       srv_put_dos_date2(p,4,adate);
-                       srv_put_dos_date2(p,8,mdate);
-                       SIVAL(p,12,(uint32)file_size);
-                       SIVAL(p,16,(uint32)allocation_size);
-                       SSVAL(p,20,mode);
-                       {
-                               unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
-                               SIVAL(p,22,ea_size); /* Extended attributes */
-                       }
-                       p += 27;
-                       nameptr = p - 1;
-                       len = srvstr_push(base_data, flags2,
-                                         p, fname, PTR_DIFF(end_data, p),
-                                         STR_TERMINATE | STR_NOALIGN);
-                       if (flags2 & FLAGS2_UNICODE_STRINGS) {
-                               if (len > 2) {
-                                       len -= 2;
-                               } else {
-                                       len = 0;
-                               }
+               } else {
+                       if (len > 1) {
+                               SCVAL(nameptr, -1, len - 1);
                        } else {
-                               if (len > 1) {
-                                       len -= 1;
-                               } else {
-                                       len = 0;
-                               }
+                               SCVAL(nameptr, -1, 0);
                        }
-                       SCVAL(nameptr,0,len);
-                       p += len;
-                       SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
-                       break;
+               }
+               p += len;
+               break;
 
-               case SMB_FIND_EA_LIST:
+       case SMB_FIND_EA_SIZE:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_SIZE\n"));
+               if (requires_resume_key) {
+                       SIVAL(p,0,reskey);
+                       p += 4;
+               }
+               srv_put_dos_date2(p,0,create_date);
+               srv_put_dos_date2(p,4,adate);
+               srv_put_dos_date2(p,8,mdate);
+               SIVAL(p,12,(uint32)file_size);
+               SIVAL(p,16,(uint32)allocation_size);
+               SSVAL(p,20,mode);
                {
-                       struct ea_list *file_list = NULL;
-                       size_t ea_len = 0;
-
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_LIST\n"));
-                       if (!name_list) {
-                               return False;
-                       }
-                       if(requires_resume_key) {
-                               SIVAL(p,0,reskey);
-                               p += 4;
-                       }
-                       srv_put_dos_date2(p,0,create_date);
-                       srv_put_dos_date2(p,4,adate);
-                       srv_put_dos_date2(p,8,mdate);
-                       SIVAL(p,12,(uint32)file_size);
-                       SIVAL(p,16,(uint32)allocation_size);
-                       SSVAL(p,20,mode);
-                       p += 22; /* p now points to the EA area. */
-
-                       file_list = get_ea_list_from_file(ctx, conn, NULL, pathreal, &ea_len);
-                       name_list = ea_list_union(name_list, file_list, &ea_len);
-
-                       /* We need to determine if this entry will fit in the space available. */
-                       /* Max string size is 255 bytes. */
-                       if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
-                               /* Move the dirptr back to prev_dirpos */
-                               dptr_SeekDir(conn->dirptr, prev_dirpos);
-                               *out_of_space = True;
-                               DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
-                               return False; /* Not finished - just out of space */
+                       unsigned int ea_size = estimate_ea_size(conn, NULL,
+                                                               smb_fname->base_name);
+                       SIVAL(p,22,ea_size); /* Extended attributes */
+               }
+               p += 27;
+               nameptr = p - 1;
+               len = srvstr_push(base_data, flags2,
+                                 p, fname, PTR_DIFF(end_data, p),
+                                 STR_TERMINATE | STR_NOALIGN);
+               if (flags2 & FLAGS2_UNICODE_STRINGS) {
+                       if (len > 2) {
+                               len -= 2;
+                       } else {
+                               len = 0;
                        }
-
-                       /* Push the ea_data followed by the name. */
-                       p += fill_ea_buffer(ctx, p, space_remaining, conn, name_list);
-                       nameptr = p;
-                       len = srvstr_push(base_data, flags2,
-                                         p + 1, fname, PTR_DIFF(end_data, p+1),
-                                         STR_TERMINATE | STR_NOALIGN);
-                       if (flags2 & FLAGS2_UNICODE_STRINGS) {
-                               if (len > 2) {
-                                       len -= 2;
-                               } else {
-                                       len = 0;
-                               }
+               } else {
+                       if (len > 1) {
+                               len -= 1;
                        } else {
-                               if (len > 1) {
-                                       len -= 1;
-                               } else {
-                                       len = 0;
-                               }
+                               len = 0;
                        }
-                       SCVAL(nameptr,0,len);
-                       p += len + 1;
-                       SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
-                       break;
                }
+               SCVAL(nameptr,0,len);
+               p += len;
+               SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
+               break;
 
-               case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
-                       was_8_3 = mangle_is_8_3(fname, True, conn->params);
+       case SMB_FIND_EA_LIST:
+       {
+               struct ea_list *file_list = NULL;
+               size_t ea_len = 0;
+
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_LIST\n"));
+               if (!name_list) {
+                       return false;
+               }
+               if (requires_resume_key) {
+                       SIVAL(p,0,reskey);
                        p += 4;
-                       SIVAL(p,0,reskey); p += 4;
-                       put_long_date_timespec(p,create_date_ts); p += 8;
-                       put_long_date_timespec(p,adate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       SOFF_T(p,0,file_size); p += 8;
-                       SOFF_T(p,0,allocation_size); p += 8;
-                       SIVAL(p,0,nt_extmode); p += 4;
-                       q = p; p += 4; /* q is placeholder for name length. */
-                       {
-                               unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
-                               SIVAL(p,0,ea_size); /* Extended attributes */
-                               p += 4;
+               }
+               srv_put_dos_date2(p,0,create_date);
+               srv_put_dos_date2(p,4,adate);
+               srv_put_dos_date2(p,8,mdate);
+               SIVAL(p,12,(uint32)file_size);
+               SIVAL(p,16,(uint32)allocation_size);
+               SSVAL(p,20,mode);
+               p += 22; /* p now points to the EA area. */
+
+               file_list = get_ea_list_from_file(ctx, conn, NULL,
+                                                 smb_fname->base_name,
+                                                 &ea_len);
+               name_list = ea_list_union(name_list, file_list, &ea_len);
+
+               /* We need to determine if this entry will fit in the space available. */
+               /* Max string size is 255 bytes. */
+               if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
+                       *out_of_space = true;
+                       DEBUG(9,("smbd_marshall_dir_entry: out of space\n"));
+                       return False; /* Not finished - just out of space */
+               }
+
+               /* Push the ea_data followed by the name. */
+               p += fill_ea_buffer(ctx, p, space_remaining, conn, name_list);
+               nameptr = p;
+               len = srvstr_push(base_data, flags2,
+                                 p + 1, fname, PTR_DIFF(end_data, p+1),
+                                 STR_TERMINATE | STR_NOALIGN);
+               if (flags2 & FLAGS2_UNICODE_STRINGS) {
+                       if (len > 2) {
+                               len -= 2;
+                       } else {
+                               len = 0;
                        }
-                       /* Clear the short name buffer. This is
-                        * IMPORTANT as not doing so will trigger
-                        * a Win2k client bug. JRA.
-                        */
-                       if (!was_8_3 && check_mangled_names) {
-                               if (!name_to_8_3(fname,mangled_name,True,
-                                                  conn->params)) {
-                                       /* Error - mangle failed ! */
-                                       memset(mangled_name,'\0',12);
-                               }
-                               mangled_name[12] = 0;
-                               len = srvstr_push(base_data, flags2,
-                                                 p+2, mangled_name, 24,
-                                                 STR_UPPER|STR_UNICODE);
-                               if (len < 24) {
-                                       memset(p + 2 + len,'\0',24 - len);
-                               }
-                               SSVAL(p, 0, len);
+               } else {
+                       if (len > 1) {
+                               len -= 1;
                        } else {
-                               memset(p,'\0',26);
+                               len = 0;
                        }
-                       p += 2 + 24;
-                       len = srvstr_push(base_data, flags2, p,
-                                         fname, PTR_DIFF(end_data, p),
-                                         STR_TERMINATE_ASCII);
-                       SIVAL(q,0,len);
-                       p += len;
-                       SIVAL(p,0,0); /* Ensure any padding is null. */
-                       len = PTR_DIFF(p, pdata);
-                       len = (len + 3) & ~3;
-                       SIVAL(pdata,0,len);
-                       p = pdata + len;
-                       break;
+               }
+               SCVAL(nameptr,0,len);
+               p += len + 1;
+               SCVAL(p,0,0); p += 1; /* Extra zero byte ? - why.. */
+               break;
+       }
 
-               case SMB_FIND_FILE_DIRECTORY_INFO:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
+       case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
+               was_8_3 = mangle_is_8_3(fname, True, conn->params);
+               p += 4;
+               SIVAL(p,0,reskey); p += 4;
+               put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
+               SOFF_T(p,0,file_size); p += 8;
+               SOFF_T(p,0,allocation_size); p += 8;
+               SIVAL(p,0,mode); p += 4;
+               q = p; p += 4; /* q is placeholder for name length. */
+               {
+                       unsigned int ea_size = estimate_ea_size(conn, NULL,
+                                                               smb_fname->base_name);
+                       SIVAL(p,0,ea_size); /* Extended attributes */
                        p += 4;
-                       SIVAL(p,0,reskey); p += 4;
-                       put_long_date_timespec(p,create_date_ts); p += 8;
-                       put_long_date_timespec(p,adate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       SOFF_T(p,0,file_size); p += 8;
-                       SOFF_T(p,0,allocation_size); p += 8;
-                       SIVAL(p,0,nt_extmode); p += 4;
+               }
+               /* Clear the short name buffer. This is
+                * IMPORTANT as not doing so will trigger
+                * a Win2k client bug. JRA.
+                */
+               if (!was_8_3 && check_mangled_names) {
+                       char mangled_name[13]; /* mangled 8.3 name. */
+                       if (!name_to_8_3(fname,mangled_name,True,
+                                          conn->params)) {
+                               /* Error - mangle failed ! */
+                               memset(mangled_name,'\0',12);
+                       }
+                       mangled_name[12] = 0;
                        len = srvstr_push(base_data, flags2,
-                                         p + 4, fname, PTR_DIFF(end_data, p+4),
-                                         STR_TERMINATE_ASCII);
-                       SIVAL(p,0,len);
-                       p += 4 + len;
-                       SIVAL(p,0,0); /* Ensure any padding is null. */
-                       len = PTR_DIFF(p, pdata);
-                       len = (len + 3) & ~3;
-                       SIVAL(pdata,0,len);
+                                         p+2, mangled_name, 24,
+                                         STR_UPPER|STR_UNICODE);
+                       if (len < 24) {
+                               memset(p + 2 + len,'\0',24 - len);
+                       }
+                       SSVAL(p, 0, len);
+               } else {
+                       memset(p,'\0',26);
+               }
+               p += 2 + 24;
+               len = srvstr_push(base_data, flags2, p,
+                                 fname, PTR_DIFF(end_data, p),
+                                 STR_TERMINATE_ASCII);
+               SIVAL(q,0,len);
+               p += len;
+
+               len = PTR_DIFF(p, pdata);
+               pad = (len + (align-1)) & ~(align-1);
+               /*
+                * offset to the next entry, the caller
+                * will overwrite it for the last entry
+                * that's why we always include the padding
+                */
+               SIVAL(pdata,0,pad);
+               /*
+                * set padding to zero
+                */
+               if (do_pad) {
+                       memset(p, 0, pad - len);
+                       p = pdata + pad;
+               } else {
                        p = pdata + len;
-                       break;
+               }
+               break;
 
-               case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
-                       p += 4;
-                       SIVAL(p,0,reskey); p += 4;
-                       put_long_date_timespec(p,create_date_ts); p += 8;
-                       put_long_date_timespec(p,adate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       SOFF_T(p,0,file_size); p += 8;
-                       SOFF_T(p,0,allocation_size); p += 8;
-                       SIVAL(p,0,nt_extmode); p += 4;
-                       q = p; p += 4; /* q is placeholder for name length. */
-                       {
-                               unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
-                               SIVAL(p,0,ea_size); /* Extended attributes */
-                               p +=4;
-                       }
-                       len = srvstr_push(base_data, flags2, p,
-                                         fname, PTR_DIFF(end_data, p),
-                                         STR_TERMINATE_ASCII);
-                       SIVAL(q, 0, len);
-                       p += len;
+       case SMB_FIND_FILE_DIRECTORY_INFO:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
+               p += 4;
+               SIVAL(p,0,reskey); p += 4;
+               put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
+               SOFF_T(p,0,file_size); p += 8;
+               SOFF_T(p,0,allocation_size); p += 8;
+               SIVAL(p,0,mode); p += 4;
+               len = srvstr_push(base_data, flags2,
+                                 p + 4, fname, PTR_DIFF(end_data, p+4),
+                                 STR_TERMINATE_ASCII);
+               SIVAL(p,0,len);
+               p += 4 + len;
+
+               len = PTR_DIFF(p, pdata);
+               pad = (len + (align-1)) & ~(align-1);
+               /*
+                * offset to the next entry, the caller
+                * will overwrite it for the last entry
+                * that's why we always include the padding
+                */
+               SIVAL(pdata,0,pad);
+               /*
+                * set padding to zero
+                */
+               if (do_pad) {
+                       memset(p, 0, pad - len);
+                       p = pdata + pad;
+               } else {
+                       p = pdata + len;
+               }
+               break;
 
-                       SIVAL(p,0,0); /* Ensure any padding is null. */
-                       len = PTR_DIFF(p, pdata);
-                       len = (len + 3) & ~3;
-                       SIVAL(pdata,0,len);
+       case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
+               p += 4;
+               SIVAL(p,0,reskey); p += 4;
+               put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
+               SOFF_T(p,0,file_size); p += 8;
+               SOFF_T(p,0,allocation_size); p += 8;
+               SIVAL(p,0,mode); p += 4;
+               q = p; p += 4; /* q is placeholder for name length. */
+               {
+                       unsigned int ea_size = estimate_ea_size(conn, NULL,
+                                                               smb_fname->base_name);
+                       SIVAL(p,0,ea_size); /* Extended attributes */
+                       p +=4;
+               }
+               len = srvstr_push(base_data, flags2, p,
+                                 fname, PTR_DIFF(end_data, p),
+                                 STR_TERMINATE_ASCII);
+               SIVAL(q, 0, len);
+               p += len;
+
+               len = PTR_DIFF(p, pdata);
+               pad = (len + (align-1)) & ~(align-1);
+               /*
+                * offset to the next entry, the caller
+                * will overwrite it for the last entry
+                * that's why we always include the padding
+                */
+               SIVAL(pdata,0,pad);
+               /*
+                * set padding to zero
+                */
+               if (do_pad) {
+                       memset(p, 0, pad - len);
+                       p = pdata + pad;
+               } else {
                        p = pdata + len;
-                       break;
+               }
+               break;
 
-               case SMB_FIND_FILE_NAMES_INFO:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
-                       p += 4;
-                       SIVAL(p,0,reskey); p += 4;
-                       p += 4;
-                       /* this must *not* be null terminated or w2k gets in a loop trying to set an
-                          acl on a dir (tridge) */
-                       len = srvstr_push(base_data, flags2, p,
-                                         fname, PTR_DIFF(end_data, p),
-                                         STR_TERMINATE_ASCII);
-                       SIVAL(p, -4, len);
-                       p += len;
-                       SIVAL(p,0,0); /* Ensure any padding is null. */
-                       len = PTR_DIFF(p, pdata);
-                       len = (len + 3) & ~3;
-                       SIVAL(pdata,0,len);
+       case SMB_FIND_FILE_NAMES_INFO:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
+               p += 4;
+               SIVAL(p,0,reskey); p += 4;
+               p += 4;
+               /* this must *not* be null terminated or w2k gets in a loop trying to set an
+                  acl on a dir (tridge) */
+               len = srvstr_push(base_data, flags2, p,
+                                 fname, PTR_DIFF(end_data, p),
+                                 STR_TERMINATE_ASCII);
+               SIVAL(p, -4, len);
+               p += len;
+
+               len = PTR_DIFF(p, pdata);
+               pad = (len + (align-1)) & ~(align-1);
+               /*
+                * offset to the next entry, the caller
+                * will overwrite it for the last entry
+                * that's why we always include the padding
+                */
+               SIVAL(pdata,0,pad);
+               /*
+                * set padding to zero
+                */
+               if (do_pad) {
+                       memset(p, 0, pad - len);
+                       p = pdata + pad;
+               } else {
                        p = pdata + len;
-                       break;
+               }
+               break;
 
-               case SMB_FIND_ID_FULL_DIRECTORY_INFO:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
-                       p += 4;
-                       SIVAL(p,0,reskey); p += 4;
-                       put_long_date_timespec(p,create_date_ts); p += 8;
-                       put_long_date_timespec(p,adate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       SOFF_T(p,0,file_size); p += 8;
-                       SOFF_T(p,0,allocation_size); p += 8;
-                       SIVAL(p,0,nt_extmode); p += 4;
-                       q = p; p += 4; /* q is placeholder for name length. */
-                       {
-                               unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
-                               SIVAL(p,0,ea_size); /* Extended attributes */
-                               p +=4;
-                       }
-                       SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
-                       SIVAL(p,0,sbuf.st_ex_ino); p += 4; /* FileIndexLow */
-                       SIVAL(p,0,sbuf.st_ex_dev); p += 4; /* FileIndexHigh */
-                       len = srvstr_push(base_data, flags2, p,
-                                         fname, PTR_DIFF(end_data, p),
-                                         STR_TERMINATE_ASCII);
-                       SIVAL(q, 0, len);
-                       p += len; 
-                       SIVAL(p,0,0); /* Ensure any padding is null. */
-                       len = PTR_DIFF(p, pdata);
-                       len = (len + 3) & ~3;
-                       SIVAL(pdata,0,len);
+       case SMB_FIND_ID_FULL_DIRECTORY_INFO:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
+               p += 4;
+               SIVAL(p,0,reskey); p += 4;
+               put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
+               SOFF_T(p,0,file_size); p += 8;
+               SOFF_T(p,0,allocation_size); p += 8;
+               SIVAL(p,0,mode); p += 4;
+               q = p; p += 4; /* q is placeholder for name length. */
+               {
+                       unsigned int ea_size = estimate_ea_size(conn, NULL,
+                                                               smb_fname->base_name);
+                       SIVAL(p,0,ea_size); /* Extended attributes */
+                       p +=4;
+               }
+               SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
+               SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
+               SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
+               len = srvstr_push(base_data, flags2, p,
+                                 fname, PTR_DIFF(end_data, p),
+                                 STR_TERMINATE_ASCII);
+               SIVAL(q, 0, len);
+               p += len;
+
+               len = PTR_DIFF(p, pdata);
+               pad = (len + (align-1)) & ~(align-1);
+               /*
+                * offset to the next entry, the caller
+                * will overwrite it for the last entry
+                * that's why we always include the padding
+                */
+               SIVAL(pdata,0,pad);
+               /*
+                * set padding to zero
+                */
+               if (do_pad) {
+                       memset(p, 0, pad - len);
+                       p = pdata + pad;
+               } else {
                        p = pdata + len;
-                       break;
+               }
+               break;
 
-               case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
-                       was_8_3 = mangle_is_8_3(fname, True, conn->params);
-                       p += 4;
-                       SIVAL(p,0,reskey); p += 4;
-                       put_long_date_timespec(p,create_date_ts); p += 8;
-                       put_long_date_timespec(p,adate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       put_long_date_timespec(p,mdate_ts); p += 8;
-                       SOFF_T(p,0,file_size); p += 8;
-                       SOFF_T(p,0,allocation_size); p += 8;
-                       SIVAL(p,0,nt_extmode); p += 4;
-                       q = p; p += 4; /* q is placeholder for name length */
-                       {
-                               unsigned int ea_size = estimate_ea_size(conn, NULL, pathreal);
-                               SIVAL(p,0,ea_size); /* Extended attributes */
-                               p +=4;
+       case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
+               was_8_3 = mangle_is_8_3(fname, True, conn->params);
+               p += 4;
+               SIVAL(p,0,reskey); p += 4;
+               put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,adate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,mdate_ts); p += 8;
+               put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
+               SOFF_T(p,0,file_size); p += 8;
+               SOFF_T(p,0,allocation_size); p += 8;
+               SIVAL(p,0,mode); p += 4;
+               q = p; p += 4; /* q is placeholder for name length */
+               {
+                       unsigned int ea_size = estimate_ea_size(conn, NULL,
+                                                               smb_fname->base_name);
+                       SIVAL(p,0,ea_size); /* Extended attributes */
+                       p +=4;
+               }
+               /* Clear the short name buffer. This is
+                * IMPORTANT as not doing so will trigger
+                * a Win2k client bug. JRA.
+                */
+               if (!was_8_3 && check_mangled_names) {
+                       char mangled_name[13]; /* mangled 8.3 name. */
+                       if (!name_to_8_3(fname,mangled_name,True,
+                                       conn->params)) {
+                               /* Error - mangle failed ! */
+                               memset(mangled_name,'\0',12);
                        }
-                       /* Clear the short name buffer. This is
-                        * IMPORTANT as not doing so will trigger
-                        * a Win2k client bug. JRA.
-                        */
-                       if (!was_8_3 && check_mangled_names) {
-                               if (!name_to_8_3(fname,mangled_name,True,
-                                               conn->params)) {
-                                       /* Error - mangle failed ! */
-                                       memset(mangled_name,'\0',12);
-                               }
-                               mangled_name[12] = 0;
-                               len = srvstr_push(base_data, flags2,
-                                                 p+2, mangled_name, 24,
-                                                 STR_UPPER|STR_UNICODE);
-                               SSVAL(p, 0, len);
-                               if (len < 24) {
-                                       memset(p + 2 + len,'\0',24 - len);
-                               }
-                               SSVAL(p, 0, len);
-                       } else {
-                               memset(p,'\0',26);
+                       mangled_name[12] = 0;
+                       len = srvstr_push(base_data, flags2,
+                                         p+2, mangled_name, 24,
+                                         STR_UPPER|STR_UNICODE);
+                       SSVAL(p, 0, len);
+                       if (len < 24) {
+                               memset(p + 2 + len,'\0',24 - len);
                        }
-                       p += 26;
-                       SSVAL(p,0,0); p += 2; /* Reserved ? */
-                       SIVAL(p,0,sbuf.st_ex_ino); p += 4; /* FileIndexLow */
-                       SIVAL(p,0,sbuf.st_ex_dev); p += 4; /* FileIndexHigh */
-                       len = srvstr_push(base_data, flags2, p,
-                                         fname, PTR_DIFF(end_data, p),
-                                         STR_TERMINATE_ASCII);
-                       SIVAL(q,0,len);
-                       p += len;
-                       SIVAL(p,0,0); /* Ensure any padding is null. */
-                       len = PTR_DIFF(p, pdata);
-                       len = (len + 3) & ~3;
-                       SIVAL(pdata,0,len);
+                       SSVAL(p, 0, len);
+               } else {
+                       memset(p,'\0',26);
+               }
+               p += 26;
+               SSVAL(p,0,0); p += 2; /* Reserved ? */
+               SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
+               SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
+               len = srvstr_push(base_data, flags2, p,
+                                 fname, PTR_DIFF(end_data, p),
+                                 STR_TERMINATE_ASCII);
+               SIVAL(q,0,len);
+               p += len;
+
+               len = PTR_DIFF(p, pdata);
+               pad = (len + (align-1)) & ~(align-1);
+               /*
+                * offset to the next entry, the caller
+                * will overwrite it for the last entry
+                * that's why we always include the padding
+                */
+               SIVAL(pdata,0,pad);
+               /*
+                * set padding to zero
+                */
+               if (do_pad) {
+                       memset(p, 0, pad - len);
+                       p = pdata + pad;
+               } else {
                        p = pdata + len;
-                       break;
+               }
+               break;
 
-               /* CIFS UNIX Extension. */
+       /* CIFS UNIX Extension. */
 
-               case SMB_FIND_FILE_UNIX:
-               case SMB_FIND_FILE_UNIX_INFO2:
-                       p+= 4;
-                       SIVAL(p,0,reskey); p+= 4;    /* Used for continuing search. */
+       case SMB_FIND_FILE_UNIX:
+       case SMB_FIND_FILE_UNIX_INFO2:
+               p+= 4;
+               SIVAL(p,0,reskey); p+= 4;    /* Used for continuing search. */
 
-                       /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
+               /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
 
-                       if (info_level == SMB_FIND_FILE_UNIX) {
-                               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX\n"));
-                               p = store_file_unix_basic(conn, p,
-                                                       NULL, &sbuf);
-                               len = srvstr_push(base_data, flags2, p,
-                                                 fname, PTR_DIFF(end_data, p),
-                                                 STR_TERMINATE);
-                       } else {
-                               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
-                               p = store_file_unix_basic_info2(conn, p,
-                                                       NULL, &sbuf);
-                               nameptr = p;
-                               p += 4;
-                               len = srvstr_push(base_data, flags2, p, fname,
-                                                 PTR_DIFF(end_data, p), 0);
-                               SIVAL(nameptr, 0, len);
-                       }
+               if (info_level == SMB_FIND_FILE_UNIX) {
+                       DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX\n"));
+                       p = store_file_unix_basic(conn, p,
+                                               NULL, &smb_fname->st);
+                       len = srvstr_push(base_data, flags2, p,
+                                         fname, PTR_DIFF(end_data, p),
+                                         STR_TERMINATE);
+               } else {
+                       DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
+                       p = store_file_unix_basic_info2(conn, p,
+                                               NULL, &smb_fname->st);
+                       nameptr = p;
+                       p += 4;
+                       len = srvstr_push(base_data, flags2, p, fname,
+                                         PTR_DIFF(end_data, p), 0);
+                       SIVAL(nameptr, 0, len);
+               }
 
-                       p += len;
-                       SIVAL(p,0,0); /* Ensure any padding is null. */
+               p += len;
 
-                       len = PTR_DIFF(p, pdata);
-                       len = (len + 3) & ~3;
-                       SIVAL(pdata,0,len);     /* Offset from this structure to the beginning of the next one */
+               len = PTR_DIFF(p, pdata);
+               pad = (len + (align-1)) & ~(align-1);
+               /*
+                * offset to the next entry, the caller
+                * will overwrite it for the last entry
+                * that's why we always include the padding
+                */
+               SIVAL(pdata,0,pad);
+               /*
+                * set padding to zero
+                */
+               if (do_pad) {
+                       memset(p, 0, pad - len);
+                       p = pdata + pad;
+               } else {
                        p = pdata + len;
-                       /* End of SMB_QUERY_FILE_UNIX_BASIC */
+               }
+               /* End of SMB_QUERY_FILE_UNIX_BASIC */
 
-                       break;
+               break;
 
-               default:
-                       TALLOC_FREE(fname);
-                       return(False);
+       default:
+               return false;
        }
 
-       TALLOC_FREE(fname);
        if (PTR_DIFF(p,pdata) > space_remaining) {
-               /* Move the dirptr back to prev_dirpos */
-               dptr_SeekDir(conn->dirptr, prev_dirpos);
-               *out_of_space = True;
-               DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
-               return False; /* Not finished - just out of space */
+               *out_of_space = true;
+               DEBUG(9,("smbd_marshall_dir_entry: out of space\n"));
+               return false; /* Not finished - just out of space */
        }
 
        /* Setup the last entry pointer, as an offset from base_data */
@@ -1987,7 +2030,147 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
        /* Advance the data pointer to the next slot */
        *ppdata = p;
 
-       return(found);
+       return true;
+}
+
+bool smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
+                              connection_struct *conn,
+                              struct dptr_struct *dirptr,
+                              uint16 flags2,
+                              const char *path_mask,
+                              uint32 dirtype,
+                              int info_level,
+                              int requires_resume_key,
+                              bool dont_descend,
+                              bool ask_sharemode,
+                              uint8_t align,
+                              bool do_pad,
+                              char **ppdata,
+                              char *base_data,
+                              char *end_data,
+                              int space_remaining,
+                              bool *out_of_space,
+                              bool *got_exact_match,
+                              int *_last_entry_off,
+                              struct ea_list *name_list)
+{
+       const char *p;
+       const char *mask = NULL;
+       long prev_dirpos = 0;
+       uint32_t mode = 0;
+       char *fname = NULL;
+       struct smb_filename *smb_fname = NULL;
+       struct smbd_dirptr_lanman2_state state;
+       bool ok;
+       uint64_t last_entry_off = 0;
+
+       ZERO_STRUCT(state);
+       state.conn = conn;
+       state.info_level = info_level;
+       state.check_mangled_names = lp_manglednames(conn->params);
+       state.has_wild = dptr_has_wild(dirptr);
+       state.got_exact_match = false;
+
+       *out_of_space = false;
+       *got_exact_match = false;
+
+       p = strrchr_m(path_mask,'/');
+       if(p != NULL) {
+               if(p[1] == '\0') {
+                       mask = "*.*";
+               } else {
+                       mask = p+1;
+               }
+       } else {
+               mask = path_mask;
+       }
+
+       ok = smbd_dirptr_get_entry(ctx,
+                                  dirptr,
+                                  mask,
+                                  dirtype,
+                                  dont_descend,
+                                  ask_sharemode,
+                                  smbd_dirptr_lanman2_match_fn,
+                                  smbd_dirptr_lanman2_mode_fn,
+                                  &state,
+                                  &fname,
+                                  &smb_fname,
+                                  &mode,
+                                  &prev_dirpos);
+       if (!ok) {
+               return false;
+       }
+
+       *got_exact_match = state.got_exact_match;
+
+       ok = smbd_marshall_dir_entry(ctx,
+                                    conn,
+                                    flags2,
+                                    info_level,
+                                    name_list,
+                                    state.check_mangled_names,
+                                    requires_resume_key,
+                                    mode,
+                                    fname,
+                                    smb_fname,
+                                    space_remaining,
+                                    align,
+                                    do_pad,
+                                    base_data,
+                                    ppdata,
+                                    end_data,
+                                    out_of_space,
+                                    &last_entry_off);
+       TALLOC_FREE(fname);
+       TALLOC_FREE(smb_fname);
+       if (*out_of_space) {
+               dptr_SeekDir(dirptr, prev_dirpos);
+               return false;
+       }
+       if (!ok) {
+               return false;
+       }
+
+       *_last_entry_off = last_entry_off;
+       return true;
+}
+
+static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
+                               connection_struct *conn,
+                               struct dptr_struct *dirptr,
+                               uint16 flags2,
+                               const char *path_mask,
+                               uint32 dirtype,
+                               int info_level,
+                               bool requires_resume_key,
+                               bool dont_descend,
+                               bool ask_sharemode,
+                               char **ppdata,
+                               char *base_data,
+                               char *end_data,
+                               int space_remaining,
+                               bool *out_of_space,
+                               bool *got_exact_match,
+                               int *last_entry_off,
+                               struct ea_list *name_list)
+{
+       uint8_t align = 4;
+       const bool do_pad = true;
+
+       if (info_level >= 1 && info_level <= 3) {
+               /* No alignment on earlier info levels. */
+               align = 1;
+       }
+
+       return smbd_dirptr_lanman2_entry(ctx, conn, dirptr, flags2,
+                                        path_mask, dirtype, info_level,
+                                        requires_resume_key, dont_descend, ask_sharemode,
+                                        align, do_pad,
+                                        ppdata, base_data, end_data,
+                                        space_remaining,
+                                        out_of_space, got_exact_match,
+                                        last_entry_off, name_list);
 }
 
 /****************************************************************************
@@ -2032,6 +2215,8 @@ static void call_trans2findfirst(connection_struct *conn,
        NTSTATUS ntstatus = NT_STATUS_OK;
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        TALLOC_CTX *ctx = talloc_tos();
+       struct dptr_struct *dirptr = NULL;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        if (total_params < 13) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2089,11 +2274,13 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                goto out;
        }
 
-       ntstatus = resolve_dfspath_wcard(ctx, conn,
-                       req->flags2 & FLAGS2_DFS_PATHNAMES,
-                       directory,
-                       &directory,
-                       &mask_contains_wcard);
+       ntstatus = filename_convert(ctx, conn,
+                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                   directory,
+                                   (UCF_SAVE_LCOMP |
+                                       UCF_ALWAYS_ALLOW_WCARD_LCOMP),
+                                   &mask_contains_wcard,
+                                   &smb_dname);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -2104,23 +2291,10 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                goto out;
        }
 
-       ntstatus = unix_convert(ctx, conn, directory, &smb_dname,
-                               (UCF_SAVE_LCOMP | UCF_ALLOW_WCARD_LCOMP));
-       if (!NT_STATUS_IS_OK(ntstatus)) {
-               reply_nterror(req, ntstatus);
-               goto out;
-       }
-
        mask = smb_dname->original_lcomp;
 
        directory = smb_dname->base_name;
 
-       ntstatus = check_name(conn, directory);
-       if (!NT_STATUS_IS_OK(ntstatus)) {
-               reply_nterror(req, ntstatus);
-               goto out;
-       }
-
        p = strrchr_m(directory,'/');
        if(p == NULL) {
                /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
@@ -2160,7 +2334,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                }
 
                if (!lp_ea_support(SNUM(conn))) {
-                       reply_doserror(req, ERRDOS, ERReasnotsupported);
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                        goto out;
                }
 
@@ -2200,24 +2374,25 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                mask,
                                mask_contains_wcard,
                                dirtype,
-                               &conn->dirptr);
+                               &dirptr);
 
        if (!NT_STATUS_IS_OK(ntstatus)) {
                reply_nterror(req, ntstatus);
                goto out;
        }
 
-       dptr_num = dptr_dnum(conn->dirptr);
+       dptr_num = dptr_dnum(dirptr);
        DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
 
        /* Initialize per TRANS2_FIND_FIRST operation data */
-       dptr_init_search_op(conn->dirptr);
+       dptr_init_search_op(dirptr);
 
        /* We don't need to check for VOL here as this is returned by
                a different TRANS2 call. */
 
-       DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n", conn->dirpath,lp_dontdescend(SNUM(conn))));
-       if (in_list(conn->dirpath,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
+       DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
+               directory,lp_dontdescend(SNUM(conn))));
+       if (in_list(directory,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
                dont_descend = True;
 
        p = pdata;
@@ -2235,6 +2410,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                } else {
                        finished = !get_lanman2_dir_entry(ctx,
                                        conn,
+                                       dirptr,
                                        req->flags2,
                                        mask,dirtype,info_level,
                                        requires_resume_key,dont_descend,
@@ -2273,7 +2449,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        /* Check if we can close the dirptr */
        if(close_after_first || (finished && close_if_end)) {
                DEBUG(5,("call_trans2findfirst - (2) closing dptr_num %d\n", dptr_num));
-               dptr_close(&dptr_num);
+               dptr_close(sconn, &dptr_num);
        }
 
        /*
@@ -2284,9 +2460,9 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
         */
 
        if(numentries == 0) {
-               dptr_close(&dptr_num);
-               if (Protocol < PROTOCOL_NT1) {
-                       reply_doserror(req, ERRDOS, ERRnofiles);
+               dptr_close(sconn, &dptr_num);
+               if (get_Protocol() < PROTOCOL_NT1) {
+                       reply_force_doserror(req, ERRDOS, ERRnofiles);
                        goto out;
                } else {
                        reply_botherror(req, NT_STATUS_NO_SUCH_FILE,
@@ -2307,8 +2483,8 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        send_trans2_replies(conn, req, params, 10, pdata, PTR_DIFF(p,pdata),
                            max_data_bytes);
 
-       if ((! *directory) && dptr_path(dptr_num)) {
-               directory = talloc_strdup(talloc_tos(),dptr_path(dptr_num));
+       if ((! *directory) && dptr_path(sconn, dptr_num)) {
+               directory = talloc_strdup(talloc_tos(),dptr_path(sconn, dptr_num));
                if (!directory) {
                        reply_nterror(req, NT_STATUS_NO_MEMORY);
                }
@@ -2378,6 +2554,8 @@ static void call_trans2findnext(connection_struct *conn,
        NTSTATUS ntstatus = NT_STATUS_OK;
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        TALLOC_CTX *ctx = talloc_tos();
+       struct dptr_struct *dirptr;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        if (total_params < 13) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2394,23 +2572,26 @@ static void call_trans2findnext(connection_struct *conn,
        requires_resume_key = (findnext_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
        continue_bit = (findnext_flags & FLAG_TRANS2_FIND_CONTINUE);
 
-       srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
+       if (!continue_bit) {
+               /* We only need resume_name if continue_bit is zero. */
+               srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
                              params+12,
                              total_params - 12, STR_TERMINATE, &ntstatus,
                              &mask_contains_wcard);
-       if (!NT_STATUS_IS_OK(ntstatus)) {
-               /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
-                  complain (it thinks we're asking for the directory above the shared
-                  path or an invalid name). Catch this as the resume name is only compared, never used in
-                  a file access. JRA. */
-               srvstr_pull_talloc(ctx, params, req->flags2,
+               if (!NT_STATUS_IS_OK(ntstatus)) {
+                       /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
+                          complain (it thinks we're asking for the directory above the shared
+                          path or an invalid name). Catch this as the resume name is only compared, never used in
+                          a file access. JRA. */
+                       srvstr_pull_talloc(ctx, params, req->flags2,
                                &resume_name, params+12,
                                total_params - 12,
                                STR_TERMINATE);
 
-               if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
-                       reply_nterror(req, ntstatus);
-                       return;
+                       if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
+                               reply_nterror(req, ntstatus);
+                               return;
+                       }
                }
        }
 
@@ -2418,7 +2599,8 @@ static void call_trans2findnext(connection_struct *conn,
 close_after_request=%d, close_if_end = %d requires_resume_key = %d \
 resume_key = %d resume name = %s continue=%d level = %d\n",
                dptr_num, max_data_bytes, maxentries, close_after_request, close_if_end, 
-               requires_resume_key, resume_key, resume_name, continue_bit, info_level));
+               requires_resume_key, resume_key,
+               resume_name ? resume_name : "(NULL)", continue_bit, info_level));
 
        if (!maxentries) {
                /* W2K3 seems to treat zero as 1. */
@@ -2467,7 +2649,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                }
 
                if (!lp_ea_support(SNUM(conn))) {
-                       reply_doserror(req, ERRDOS, ERReasnotsupported);
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                        return;
                }
 
@@ -2499,39 +2681,39 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        params = *pparams;
 
        /* Check that the dptr is valid */
-       if(!(conn->dirptr = dptr_fetch_lanman2(dptr_num))) {
-               reply_doserror(req, ERRDOS, ERRnofiles);
+       if(!(dirptr = dptr_fetch_lanman2(sconn, dptr_num))) {
+               reply_nterror(req, STATUS_NO_MORE_FILES);
                return;
        }
 
-       string_set(&conn->dirpath,dptr_path(dptr_num));
+       directory = dptr_path(sconn, dptr_num);
 
        /* Get the wildcard mask from the dptr */
-       if((p = dptr_wcard(dptr_num))== NULL) {
+       if((p = dptr_wcard(sconn, dptr_num))== NULL) {
                DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
-               reply_doserror(req, ERRDOS, ERRnofiles);
+               reply_nterror(req, STATUS_NO_MORE_FILES);
                return;
        }
 
        mask = p;
-       directory = conn->dirpath;
 
        /* Get the attr mask from the dptr */
-       dirtype = dptr_attr(dptr_num);
+       dirtype = dptr_attr(sconn, dptr_num);
 
        DEBUG(3,("dptr_num is %d, mask = %s, attr = %x, dirptr=(0x%lX,%ld)\n",
                dptr_num, mask, dirtype,
-               (long)conn->dirptr,
-               dptr_TellDir(conn->dirptr)));
+               (long)dirptr,
+               dptr_TellDir(dirptr)));
 
        /* Initialize per TRANS2_FIND_NEXT operation data */
-       dptr_init_search_op(conn->dirptr);
+       dptr_init_search_op(dirptr);
 
        /* We don't need to check for VOL here as this is returned by
                a different TRANS2 call. */
 
-       DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",conn->dirpath,lp_dontdescend(SNUM(conn))));
-       if (in_list(conn->dirpath,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
+       DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
+                directory,lp_dontdescend(SNUM(conn))));
+       if (in_list(directory,lp_dontdescend(SNUM(conn)),conn->case_sensitive))
                dont_descend = True;
 
        p = pdata;
@@ -2543,7 +2725,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
         * depend on the last file name instead.
         */
 
-       if(*resume_name && !continue_bit) {
+       if(!continue_bit && resume_name && *resume_name) {
                SMB_STRUCT_STAT st;
 
                long current_pos = 0;
@@ -2573,7 +2755,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                 * should already be at the correct place.
                 */
 
-               finished = !dptr_SearchDir(conn->dirptr, resume_name, &current_pos, &st);
+               finished = !dptr_SearchDir(dirptr, resume_name, &current_pos, &st);
        } /* end if resume_name && !continue_bit */
 
        for (i=0;(i<(int)maxentries) && !finished && !out_of_space ;i++) {
@@ -2587,6 +2769,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                } else {
                        finished = !get_lanman2_dir_entry(ctx,
                                                conn,
+                                               dirptr,
                                                req->flags2,
                                                mask,dirtype,info_level,
                                                requires_resume_key,dont_descend,
@@ -2623,7 +2806,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        /* Check if we can close the dirptr */
        if(close_after_request || (finished && close_if_end)) {
                DEBUG(5,("call_trans2findnext: closing dptr_num = %d\n", dptr_num));
-               dptr_close(&dptr_num); /* This frees up the saved mask */
+               dptr_close(sconn, &dptr_num); /* This frees up the saved mask */
        }
 
        /* Set up the return parameter block */
@@ -2693,9 +2876,8 @@ NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
        int snum = SNUM(conn);
        char *fstype = lp_fstype(SNUM(conn));
        uint32 additional_flags = 0;
-       struct smb_filename *smb_fname_dot = NULL;
+       struct smb_filename smb_fname_dot;
        SMB_STRUCT_STAT st;
-       NTSTATUS status;
 
        if (IS_IPC(conn)) {
                if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
@@ -2708,20 +2890,15 @@ NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
 
        DEBUG(3,("smbd_do_qfsinfo: level = %d\n", info_level));
 
-       status = create_synthetic_smb_fname(talloc_tos(), ".", NULL, NULL,
-                                           &smb_fname_dot);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
+       ZERO_STRUCT(smb_fname_dot);
+       smb_fname_dot.base_name = discard_const_p(char, ".");
 
-       if(SMB_VFS_STAT(conn, smb_fname_dot) != 0) {
+       if(SMB_VFS_STAT(conn, &smb_fname_dot) != 0) {
                DEBUG(2,("stat of . failed (%s)\n", strerror(errno)));
-               TALLOC_FREE(smb_fname_dot);
                return map_nt_error_from_unix(errno);
        }
 
-       st = smb_fname_dot->st;
-       TALLOC_FREE(smb_fname_dot);
+       st = smb_fname_dot.st;
 
        *ppdata = (char *)SMB_REALLOC(
                *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
@@ -2809,6 +2986,9 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_ex_dev, (u
 
                        /* Capabilities are filled in at connection time through STATVFS call */
                        additional_flags |= conn->fs_capabilities;
+                       additional_flags |= lp_parm_int(conn->params->service,
+                                                       "share", "fake_fscaps",
+                                                       0);
 
                        SIVAL(pdata,0,FILE_CASE_PRESERVED_NAMES|FILE_CASE_SENSITIVE_SEARCH|
                                FILE_SUPPORTS_OBJECT_IDS|FILE_UNICODE_ON_DISK|
@@ -3682,9 +3862,9 @@ static char *store_file_unix_basic(connection_struct *conn,
        SOFF_T(pdata,0,SMB_VFS_GET_ALLOC_SIZE(conn,fsp,psbuf)); /* Number of bytes used on disk - 64 Bit */
        pdata += 8;
 
-       put_long_date_timespec(pdata, psbuf->st_ex_ctime);       /* Change Time 64 Bit */
-       put_long_date_timespec(pdata+8, psbuf->st_ex_atime);     /* Last access time 64 Bit */
-       put_long_date_timespec(pdata+16, psbuf->st_ex_mtime);    /* Last modification time 64 Bit */
+       put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER, pdata, psbuf->st_ex_ctime);       /* Change Time 64 Bit */
+       put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER ,pdata+8, psbuf->st_ex_atime);     /* Last access time 64 Bit */
+       put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER, pdata+16, psbuf->st_ex_mtime);    /* Last modification time 64 Bit */
        pdata += 24;
 
        SIVAL(pdata,0,psbuf->st_ex_uid);               /* user id for the owner */
@@ -3818,7 +3998,7 @@ static char *store_file_unix_basic_info2(connection_struct *conn,
        pdata = store_file_unix_basic(conn, pdata, fsp, psbuf);
 
        /* Create (birth) time 64 bit */
-       put_long_date_timespec(pdata, psbuf->st_ex_btime);
+       put_long_date_timespec(TIMESTAMP_SET_NT_OR_BETTER,pdata, psbuf->st_ex_btime);
        pdata += 8;
 
        map_info2_flags_from_sbuf(psbuf, &file_flags, &flags_mask);
@@ -3961,7 +4141,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                               TALLOC_CTX *mem_ctx,
                               uint16_t info_level,
                               files_struct *fsp,
-                              const struct smb_filename *smb_fname,
+                              struct smb_filename *smb_fname,
                               bool delete_pending,
                               struct timespec write_time_ts,
                               bool ms_dfs_link,
@@ -3976,11 +4156,10 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
        char *pdata = *ppdata;
        char *dstart, *dend;
        unsigned int data_size;
-       struct timespec create_time_ts, mtime_ts, atime_ts;
-       time_t create_time, mtime, atime;
-       SMB_STRUCT_STAT sbuf;
+       struct timespec create_time_ts, mtime_ts, atime_ts, ctime_ts;
+       time_t create_time, mtime, atime, c_time;
+       SMB_STRUCT_STAT *psbuf = &smb_fname->st;
        char *p;
-       char *fname;
        char *base_name;
        char *dos_fname;
        int mode;
@@ -3992,17 +4171,10 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
        uint64_t file_index = 0;
        uint32_t access_mask = 0;
 
-       sbuf = smb_fname->st;
-
        if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
                return NT_STATUS_INVALID_LEVEL;
        }
 
-       status = get_full_smb_filename(mem_ctx, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
        DEBUG(5,("smbd_do_qfilepathinfo: %s (fnum = %d) level=%d max_data=%u\n",
                 smb_fname_str_dbg(smb_fname), fsp ? fsp->fnum : -1,
                 info_level, max_data_bytes));
@@ -4012,10 +4184,8 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
        } else {
                mode = dos_mode(conn, smb_fname);
        }
-       if (!mode)
-               mode = FILE_ATTRIBUTE_NORMAL;
 
-       nlink = sbuf.st_ex_nlink;
+       nlink = psbuf->st_ex_nlink;
 
        if (nlink && (mode&aDIR)) {
                nlink = 1;
@@ -4035,22 +4205,25 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
        dend = dstart + data_size - 1;
 
        if (!null_timespec(write_time_ts) && !INFO_LEVEL_IS_UNIX(info_level)) {
-               update_stat_ex_mtime(&sbuf, write_time_ts);
+               update_stat_ex_mtime(psbuf, write_time_ts);
        }
 
-       create_time_ts = sbuf.st_ex_btime;
-       mtime_ts = sbuf.st_ex_mtime;
-       atime_ts = sbuf.st_ex_atime;
+       create_time_ts = get_create_timespec(conn, fsp, smb_fname);
+       mtime_ts = psbuf->st_ex_mtime;
+       atime_ts = psbuf->st_ex_atime;
+       ctime_ts = get_change_timespec(conn, fsp, smb_fname);
 
        if (lp_dos_filetime_resolution(SNUM(conn))) {
                dos_filetime_timespec(&create_time_ts);
                dos_filetime_timespec(&mtime_ts);
                dos_filetime_timespec(&atime_ts);
+               dos_filetime_timespec(&ctime_ts);
        }
 
        create_time = convert_timespec_to_time_t(create_time_ts);
        mtime = convert_timespec_to_time_t(mtime_ts);
        atime = convert_timespec_to_time_t(atime_ts);
+       c_time = convert_timespec_to_time_t(ctime_ts);
 
        p = strrchr_m(smb_fname->base_name,'/');
        if (!p)
@@ -4068,27 +4241,35 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
        } else {
                dos_fname = talloc_asprintf(mem_ctx,
                                "\\%s",
-                               fname);
+                               smb_fname->base_name);
                if (!dos_fname) {
                        return NT_STATUS_NO_MEMORY;
                }
+               if (is_ntfs_stream_smb_fname(smb_fname)) {
+                       dos_fname = talloc_asprintf(dos_fname, "%s",
+                                                   smb_fname->stream_name);
+                       if (!dos_fname) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               }
+
                string_replace(dos_fname, '/', '\\');
        }
 
-       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &sbuf);
+       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp, psbuf);
 
        if (!fsp) {
                /* Do we have this path open ? */
                files_struct *fsp1;
-               struct file_id fileid = vfs_file_id_from_sbuf(conn, &sbuf);
+               struct file_id fileid = vfs_file_id_from_sbuf(conn, psbuf);
                fsp1 = file_find_di_first(fileid);
                if (fsp1 && fsp1->initial_allocation_size) {
-                       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp1, &sbuf);
+                       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp1, psbuf);
                }
        }
 
        if (!(mode & aDIR)) {
-               file_size = get_file_size_stat(&sbuf);
+               file_size = get_file_size_stat(psbuf);
        }
 
        if (fsp) {
@@ -4107,8 +4288,8 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
           I think this causes us to fail the IFSKIT
           BasicFileInformationTest. -tpot */
-       file_index =  ((sbuf.st_ex_ino) & UINT32_MAX); /* FileIndexLow */
-       file_index |= ((uint64_t)((sbuf.st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
+       file_index =  ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
+       file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
 
        switch (info_level) {
                case SMB_INFO_STANDARD:
@@ -4124,7 +4305,9 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                case SMB_INFO_QUERY_EA_SIZE:
                {
-                       unsigned int ea_size = estimate_ea_size(conn, fsp, fname);
+                       unsigned int ea_size =
+                           estimate_ea_size(conn, fsp,
+                                            smb_fname->base_name);
                        DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_EA_SIZE\n"));
                        data_size = 26;
                        srv_put_dos_date2(pdata,0,create_time);
@@ -4154,7 +4337,10 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                        DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_EAS_FROM_LIST\n"));
 
-                       ea_file_list = get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
+                       ea_file_list =
+                           get_ea_list_from_file(mem_ctx, conn, fsp,
+                                                 smb_fname->base_name,
+                                                 &total_ea_len);
                        ea_list = ea_list_union(ea_list, ea_file_list, &total_ea_len);
 
                        if (!ea_list || (total_ea_len > data_size)) {
@@ -4174,7 +4360,9 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                        DEBUG(10,("smbd_do_qfilepathinfo: SMB_INFO_QUERY_ALL_EAS\n"));
 
-                       ea_list = get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
+                       ea_list = get_ea_list_from_file(mem_ctx, conn, fsp,
+                                                       smb_fname->base_name,
+                                                       &total_ea_len);
                        if (!ea_list || (total_ea_len > data_size)) {
                                data_size = 4;
                                SIVAL(pdata,0,4);   /* EA List Length must be set to 4 if no EA's. */
@@ -4187,6 +4375,9 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                case 0xFF0F:/*SMB2_INFO_QUERY_ALL_EAS*/
                {
+                       /* This is FileFullEaInformation - 0xF which maps to
+                        * 1015 (decimal) in smbd_do_setfilepathinfo. */
+
                        /* We have data_size bytes to put EA's into. */
                        size_t total_ea_len = 0;
                        struct ea_list *ea_file_list = NULL;
@@ -4195,10 +4386,10 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                        /*TODO: add filtering and index handling */
 
-                       ea_file_list = get_ea_list_from_file(mem_ctx,
-                                                            conn, fsp,
-                                                            fname,
-                                                            &total_ea_len);
+                       ea_file_list =
+                           get_ea_list_from_file(mem_ctx, conn, fsp,
+                                                 smb_fname->base_name,
+                                                 &total_ea_len);
                        if (!ea_file_list) {
                                return NT_STATUS_NO_EAS_ON_FILE;
                        }
@@ -4225,17 +4416,17 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                data_size = 40;
                                SIVAL(pdata,36,0);
                        }
-                       put_long_date_timespec(pdata,create_time_ts);
-                       put_long_date_timespec(pdata+8,atime_ts);
-                       put_long_date_timespec(pdata+16,mtime_ts); /* write time */
-                       put_long_date_timespec(pdata+24,mtime_ts); /* change time */
+                       put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
+                       put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
                        SIVAL(pdata,32,mode);
 
                        DEBUG(5,("SMB_QFBI - "));
                        DEBUG(5,("create: %s ", ctime(&create_time)));
                        DEBUG(5,("access: %s ", ctime(&atime)));
                        DEBUG(5,("write: %s ", ctime(&mtime)));
-                       DEBUG(5,("change: %s ", ctime(&mtime)));
+                       DEBUG(5,("change: %s ", ctime(&c_time)));
                        DEBUG(5,("mode: %x\n", mode));
                        break;
 
@@ -4255,7 +4446,8 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                case SMB_FILE_EA_INFORMATION:
                case SMB_QUERY_FILE_EA_INFO:
                {
-                       unsigned int ea_size = estimate_ea_size(conn, fsp, fname);
+                       unsigned int ea_size =
+                           estimate_ea_size(conn, fsp, smb_fname->base_name);
                        DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_EA_INFORMATION\n"));
                        data_size = 4;
                        SIVAL(pdata,0,ea_size);
@@ -4316,12 +4508,13 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                case SMB_FILE_ALL_INFORMATION:
                {
                        int len;
-                       unsigned int ea_size = estimate_ea_size(conn, fsp, fname);
+                       unsigned int ea_size =
+                           estimate_ea_size(conn, fsp, smb_fname->base_name);
                        DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_ALL_INFORMATION\n"));
-                       put_long_date_timespec(pdata,create_time_ts);
-                       put_long_date_timespec(pdata+8,atime_ts);
-                       put_long_date_timespec(pdata+16,mtime_ts); /* write time */
-                       put_long_date_timespec(pdata+24,mtime_ts); /* change time */
+                       put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
+                       put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
                        SIVAL(pdata,32,mode);
                        SIVAL(pdata,36,0); /* padding. */
                        pdata += 40;
@@ -4347,12 +4540,13 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                case 0xFF12:/*SMB2_FILE_ALL_INFORMATION*/
                {
                        int len;
-                       unsigned int ea_size = estimate_ea_size(conn, fsp, fname);
+                       unsigned int ea_size =
+                           estimate_ea_size(conn, fsp, smb_fname->base_name);
                        DEBUG(10,("smbd_do_qfilepathinfo: SMB2_FILE_ALL_INFORMATION\n"));
-                       put_long_date_timespec(pdata+0x00,create_time_ts);
-                       put_long_date_timespec(pdata+0x08,atime_ts);
-                       put_long_date_timespec(pdata+0x10,mtime_ts); /* write time */
-                       put_long_date_timespec(pdata+0x18,mtime_ts); /* change time */
+                       put_long_date_timespec(conn->ts_res,pdata+0x00,create_time_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+0x08,atime_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+0x10,mtime_ts); /* write time */
+                       put_long_date_timespec(conn->ts_res,pdata+0x18,ctime_ts); /* change time */
                        SIVAL(pdata,    0x20, mode);
                        SIVAL(pdata,    0x24, 0); /* padding. */
                        SBVAL(pdata,    0x28, allocation_size);
@@ -4443,8 +4637,12 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                        DEBUG(10,("smbd_do_qfilepathinfo: "
                                  "SMB_FILE_STREAM_INFORMATION\n"));
 
+                       if (is_ntfs_stream_smb_fname(smb_fname)) {
+                               return NT_STATUS_INVALID_PARAMETER;
+                       }
+
                        status = SMB_VFS_STREAMINFO(
-                               conn, fsp, fname, talloc_tos(),
+                               conn, fsp, smb_fname->base_name, talloc_tos(),
                                &num_streams, &streams);
 
                        if (!NT_STATUS_IS_OK(status)) {
@@ -4478,10 +4676,10 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                case SMB_FILE_NETWORK_OPEN_INFORMATION:
                        DEBUG(10,("smbd_do_qfilepathinfo: SMB_FILE_NETWORK_OPEN_INFORMATION\n"));
-                       put_long_date_timespec(pdata,create_time_ts);
-                       put_long_date_timespec(pdata+8,atime_ts);
-                       put_long_date_timespec(pdata+16,mtime_ts); /* write time */
-                       put_long_date_timespec(pdata+24,mtime_ts); /* change time */
+                       put_long_date_timespec(conn->ts_res,pdata,create_time_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+8,atime_ts);
+                       put_long_date_timespec(conn->ts_res,pdata+16,mtime_ts); /* write time */
+                       put_long_date_timespec(conn->ts_res,pdata+24,ctime_ts); /* change time */
                        SOFF_T(pdata,32,allocation_size);
                        SOFF_T(pdata,40,file_size);
                        SIVAL(pdata,48,mode);
@@ -4502,7 +4700,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                case SMB_QUERY_FILE_UNIX_BASIC:
 
-                       pdata = store_file_unix_basic(conn, pdata, fsp, &sbuf);
+                       pdata = store_file_unix_basic(conn, pdata, fsp, psbuf);
                        data_size = PTR_DIFF(pdata,(*ppdata));
 
                        {
@@ -4518,7 +4716,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                case SMB_QUERY_FILE_UNIX_INFO2:
 
-                       pdata = store_file_unix_basic_info2(conn, pdata, fsp, &sbuf);
+                       pdata = store_file_unix_basic_info2(conn, pdata, fsp, psbuf);
                        data_size = PTR_DIFF(pdata,(*ppdata));
 
                        {
@@ -4543,14 +4741,15 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                                DEBUG(10,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_UNIX_LINK\n"));
 #ifdef S_ISLNK
-                               if(!S_ISLNK(sbuf.st_ex_mode)) {
+                               if(!S_ISLNK(psbuf->st_ex_mode)) {
                                        return NT_STATUS_DOS(ERRSRV, ERRbadlink);
                                }
 #else
                                return NT_STATUS_DOS(ERRDOS, ERRbadlink);
 #endif
-                               len = SMB_VFS_READLINK(conn,fname,
-                                               buffer, PATH_MAX);
+                               len = SMB_VFS_READLINK(conn,
+                                                      smb_fname->base_name,
+                                                      buffer, PATH_MAX);
                                if (len == -1) {
                                        return map_nt_error_from_unix(errno);
                                }
@@ -4576,16 +4775,21 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                if (fsp && !fsp->is_directory && (fsp->fh->fd != -1)) {
                                        file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp);
                                } else {
-                                       file_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS);
+                                       file_acl =
+                                           SMB_VFS_SYS_ACL_GET_FILE(conn,
+                                               smb_fname->base_name,
+                                               SMB_ACL_TYPE_ACCESS);
                                }
 
                                if (file_acl == NULL && no_acl_syscall_error(errno)) {
-                                       DEBUG(5,("smbd_do_qfilepathinfo: ACLs not implemented on filesystem containing %s\n",
-                                               fname ));
+                                       DEBUG(5,("smbd_do_qfilepathinfo: ACLs "
+                                                "not implemented on "
+                                                "filesystem containing %s\n",
+                                                smb_fname->base_name));
                                        return NT_STATUS_NOT_IMPLEMENTED;
                                }
 
-                               if (S_ISDIR(sbuf.st_ex_mode)) {
+                               if (S_ISDIR(psbuf->st_ex_mode)) {
                                        if (fsp && fsp->is_directory) {
                                                def_acl =
                                                    SMB_VFS_SYS_ACL_GET_FILE(
@@ -4593,7 +4797,11 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                                            fsp->fsp_name->base_name,
                                                            SMB_ACL_TYPE_DEFAULT);
                                        } else {
-                                               def_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname, SMB_ACL_TYPE_DEFAULT);
+                                               def_acl =
+                                                   SMB_VFS_SYS_ACL_GET_FILE(
+                                                           conn,
+                                                           smb_fname->base_name,
+                                                           SMB_ACL_TYPE_DEFAULT);
                                        }
                                        def_acl = free_empty_sys_acl(conn, def_acl);
                                }
@@ -4618,7 +4826,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                SSVAL(pdata,0,SMB_POSIX_ACL_VERSION);
                                SSVAL(pdata,2,num_file_acls);
                                SSVAL(pdata,4,num_def_acls);
-                               if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE, &sbuf, file_acl)) {
+                               if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE, psbuf, file_acl)) {
                                        if (file_acl) {
                                                SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
                                        }
@@ -4627,7 +4835,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                        }
                                        return NT_STATUS_INTERNAL_ERROR;
                                }
-                               if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE + (num_file_acls*SMB_POSIX_ACL_ENTRY_SIZE), &sbuf, def_acl)) {
+                               if (!marshall_posix_acl(conn, pdata + SMB_POSIX_ACL_HEADER_SIZE + (num_file_acls*SMB_POSIX_ACL_ENTRY_SIZE), psbuf, def_acl)) {
                                        if (file_acl) {
                                                SMB_VFS_SYS_ACL_FREE_ACL(conn, file_acl);
                                        }
@@ -4891,6 +5099,8 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                        conn,
                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
                                        fname,
+                                       0,
+                                       NULL,
                                        &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
                        if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
@@ -5021,8 +5231,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        }
 
                        if (!lp_ea_support(SNUM(conn))) {
-                               reply_doserror(req, ERRDOS,
-                                              ERReasnotsupported);
+                               reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                                return;
                        }
 
@@ -5171,24 +5380,26 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
                           struct smb_file_time *ft,
                           bool setting_write_time)
 {
-       struct smb_filename *smb_fname_base = NULL;
+       struct smb_filename smb_fname_base;
        uint32 action =
                FILE_NOTIFY_CHANGE_LAST_ACCESS
-               |FILE_NOTIFY_CHANGE_LAST_WRITE;
-       NTSTATUS status;
+               |FILE_NOTIFY_CHANGE_LAST_WRITE
+               |FILE_NOTIFY_CHANGE_CREATION;
 
        if (!VALID_STAT(smb_fname->st)) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
        /* get some defaults (no modifications) if any info is zero or -1. */
+       if (null_timespec(ft->create_time)) {
+               action &= ~FILE_NOTIFY_CHANGE_CREATION;
+       }
+
        if (null_timespec(ft->atime)) {
-               ft->atime= smb_fname->st.st_ex_atime;
                action &= ~FILE_NOTIFY_CHANGE_LAST_ACCESS;
        }
 
        if (null_timespec(ft->mtime)) {
-               ft->mtime = smb_fname->st.st_ex_mtime;
                action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
        }
 
@@ -5197,28 +5408,22 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
                action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
        }
 
+       /* Ensure the resolution is the correct for
+        * what we can store on this filesystem. */
+
+       round_timespec(conn->ts_res, &ft->create_time);
+       round_timespec(conn->ts_res, &ft->ctime);
+       round_timespec(conn->ts_res, &ft->atime);
+       round_timespec(conn->ts_res, &ft->mtime);
+
        DEBUG(5,("smb_set_filetime: actime: %s\n ",
                time_to_asc(convert_timespec_to_time_t(ft->atime))));
        DEBUG(5,("smb_set_filetime: modtime: %s\n ",
                time_to_asc(convert_timespec_to_time_t(ft->mtime))));
-       if (!null_timespec(ft->create_time)) {
-               DEBUG(5,("smb_set_file_time: createtime: %s\n ",
-                  time_to_asc(convert_timespec_to_time_t(ft->create_time))));
-       }
-
-       /*
-        * Try and set the times of this file if
-        * they are different from the current values.
-        */
-
-       {
-               struct timespec mts = smb_fname->st.st_ex_mtime;
-               struct timespec ats = smb_fname->st.st_ex_atime;
-               if ((timespec_compare(&ft->atime, &ats) == 0) &&
-                   (timespec_compare(&ft->mtime, &mts) == 0)) {
-                       return NT_STATUS_OK;
-               }
-       }
+       DEBUG(5,("smb_set_filetime: ctime: %s\n ",
+               time_to_asc(convert_timespec_to_time_t(ft->ctime))));
+       DEBUG(5,("smb_set_file_time: createtime: %s\n ",
+               time_to_asc(convert_timespec_to_time_t(ft->create_time))));
 
        if (setting_write_time) {
                /*
@@ -5251,18 +5456,12 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
        DEBUG(10,("smb_set_file_time: setting utimes to modified values.\n"));
 
        /* Always call ntimes on the base, even if a stream was passed in. */
-       status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
-                                           NULL, &smb_fname->st,
-                                           &smb_fname_base);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
+       smb_fname_base = *smb_fname;
+       smb_fname_base.stream_name = NULL;
 
-       if(file_ntimes(conn, smb_fname_base, ft)!=0) {
-               TALLOC_FREE(smb_fname_base);
+       if(file_ntimes(conn, &smb_fname_base, ft)!=0) {
                return map_nt_error_from_unix(errno);
        }
-       TALLOC_FREE(smb_fname_base);
 
        notify_fname(conn, NOTIFY_ACTION_MODIFIED, action,
                     smb_fname->base_name);
@@ -5333,7 +5532,8 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                                  files_struct *fsp,
                                  const struct smb_filename *smb_fname,
                                  const SMB_STRUCT_STAT *psbuf,
-                                 SMB_OFF_T size)
+                                 SMB_OFF_T size,
+                                 bool fail_after_createfile)
 {
        NTSTATUS status = NT_STATUS_OK;
        struct smb_filename *smb_fname_tmp = NULL;
@@ -5373,7 +5573,7 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                req,                                    /* req */
                0,                                      /* root_dir_fid */
                smb_fname_tmp,                          /* fname */
-               FILE_WRITE_ATTRIBUTES,                  /* access_mask */
+               FILE_WRITE_DATA,                        /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
                FILE_OPEN,                              /* create_disposition*/
@@ -5393,6 +5593,12 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                return status;
        }
 
+       /* See RAW-SFILEINFO-END-OF-FILE */
+       if (fail_after_createfile) {
+               close_file(req, new_fsp,NORMAL_CLOSE);
+               return NT_STATUS_INVALID_LEVEL;
+       }
+
        if (vfs_set_filelen(new_fsp, size) == -1) {
                status = map_nt_error_from_unix(errno);
                close_file(req, new_fsp,NORMAL_CLOSE);
@@ -5447,6 +5653,53 @@ static NTSTATUS smb_info_set_ea(connection_struct *conn,
        return status;
 }
 
+/****************************************************************************
+ Deal with SMB_FILE_FULL_EA_INFORMATION set.
+****************************************************************************/
+
+static NTSTATUS smb_set_file_full_ea_info(connection_struct *conn,
+                               const char *pdata,
+                               int total_data,
+                               files_struct *fsp)
+{
+       struct ea_list *ea_list = NULL;
+       NTSTATUS status;
+
+       if (!fsp) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (!lp_ea_support(SNUM(conn))) {
+               DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u but "
+                       "EA's not supported.\n",
+                       (unsigned int)total_data));
+               return NT_STATUS_EAS_NOT_SUPPORTED;
+       }
+
+       if (total_data < 10) {
+               DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u "
+                       "too small.\n",
+                       (unsigned int)total_data));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       ea_list = read_nttrans_ea_list(talloc_tos(),
+                               pdata,
+                               total_data);
+
+       if (!ea_list) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+       status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
+
+       DEBUG(10, ("smb_set_file_full_ea_info on file %s returned %s\n",
+               smb_fname_str_dbg(fsp->fsp_name),
+               nt_errstr(status) ));
+
+       return status;
+}
+
+
 /****************************************************************************
  Deal with SMB_SET_FILE_DISPOSITION_INFO.
 ****************************************************************************/
@@ -5455,7 +5708,7 @@ static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
                                const char *pdata,
                                int total_data,
                                files_struct *fsp,
-                               const struct smb_filename *smb_fname)
+                               struct smb_filename *smb_fname)
 {
        NTSTATUS status = NT_STATUS_OK;
        bool delete_on_close;
@@ -5478,10 +5731,11 @@ static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
                (unsigned int)dosmode,
                (unsigned int)delete_on_close ));
 
-       status = can_set_delete_on_close(fsp, delete_on_close, dosmode);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (delete_on_close) {
+               status = can_set_delete_on_close(fsp, dosmode);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
        }
 
        /* The set is across all open files on this dev/inode pair. */
@@ -5561,7 +5815,6 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
 {
        char *link_target = NULL;
        const char *newname = smb_fname->base_name;
-       NTSTATUS status = NT_STATUS_OK;
        TALLOC_CTX *ctx = talloc_tos();
 
        /* Set a symbolic link. */
@@ -5582,42 +5835,6 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* !widelinks forces the target path to be within the share. */
-       /* This means we can interpret the target as a pathname. */
-       if (!lp_widelinks(SNUM(conn))) {
-               char *rel_name = NULL;
-               char *last_dirp = NULL;
-
-               if (*link_target == '/') {
-                       /* No absolute paths allowed. */
-                       return NT_STATUS_ACCESS_DENIED;
-               }
-               rel_name = talloc_strdup(ctx,newname);
-               if (!rel_name) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-               last_dirp = strrchr_m(rel_name, '/');
-               if (last_dirp) {
-                       last_dirp[1] = '\0';
-               } else {
-                       rel_name = talloc_strdup(ctx,"./");
-                       if (!rel_name) {
-                               return NT_STATUS_NO_MEMORY;
-                       }
-               }
-               rel_name = talloc_asprintf_append(rel_name,
-                               "%s",
-                               link_target);
-               if (!rel_name) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               status = check_name(conn, rel_name);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
-       }
-
        DEBUG(10,("smb_set_file_unix_link: SMB_SET_FILE_UNIX_LINK doing symlink %s -> %s\n",
                        newname, link_target ));
 
@@ -5660,6 +5877,8 @@ static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                oldname,
+                               0,
+                               NULL,
                                &smb_fname_old);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -5668,6 +5887,107 @@ static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
        return hardlink_internals(ctx, conn, smb_fname_old, smb_fname_new);
 }
 
+/****************************************************************************
+ Deal with SMB2_FILE_RENAME_INFORMATION_INTERNAL
+****************************************************************************/
+
+static NTSTATUS smb2_file_rename_information(connection_struct *conn,
+                                           struct smb_request *req,
+                                           const char *pdata,
+                                           int total_data,
+                                           files_struct *fsp,
+                                           struct smb_filename *smb_fname_src)
+{
+       bool overwrite;
+       uint32_t len;
+       char *newname = NULL;
+       struct smb_filename *smb_fname_dst = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+       TALLOC_CTX *ctx = talloc_tos();
+
+       if (!fsp) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (total_data < 20) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       overwrite = (CVAL(pdata,0) ? True : False);
+       len = IVAL(pdata,16);
+
+       if (len > (total_data - 20) || (len == 0)) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       srvstr_get_path(ctx, pdata, req->flags2, &newname,
+                               &pdata[20], len, STR_TERMINATE,
+                               &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       DEBUG(10,("smb_file_rename_information: got name |%s|\n",
+                               newname));
+
+#if 0
+       /* Check the new name has no '/' characters. */
+       if (strchr_m(newname, '/')) {
+               return NT_STATUS_NOT_SUPPORTED;
+       }
+#endif
+
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               newname,
+                               0,
+                               NULL,
+                               &smb_fname_dst);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (fsp && fsp->base_fsp) {
+               /* newname must be a stream name. */
+               if (newname[0] != ':') {
+                       return NT_STATUS_NOT_SUPPORTED;
+               }
+
+               /* Create an smb_fname to call rename_internals_fsp() with. */
+               status = create_synthetic_smb_fname(talloc_tos(),
+                   fsp->base_fsp->fsp_name->base_name, newname, NULL,
+                   &smb_fname_dst);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto out;
+               }
+
+               /*
+                * Set the original last component, since
+                * rename_internals_fsp() requires it.
+                */
+               smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
+                                                             newname);
+               if (smb_fname_dst->original_lcomp == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
+
+       }
+
+       DEBUG(10,("smb_file_rename_information: "
+                 "SMB_FILE_RENAME_INFORMATION (fnum %d) %s -> %s\n",
+                 fsp->fnum, fsp_str_dbg(fsp),
+                 smb_fname_str_dbg(smb_fname_dst)));
+       status = rename_internals_fsp(conn, fsp, smb_fname_dst, 0,
+                                     overwrite);
+
+ out:
+       TALLOC_FREE(smb_fname_dst);
+       return status;
+}
+
+
 /****************************************************************************
  Deal with SMB_FILE_RENAME_INFORMATION.
 ****************************************************************************/
@@ -5677,14 +5997,13 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                                            const char *pdata,
                                            int total_data,
                                            files_struct *fsp,
-                                           const char *fname)
+                                           struct smb_filename *smb_fname_src)
 {
        bool overwrite;
        uint32 root_fid;
        uint32 len;
        char *newname = NULL;
-       char *base_name = NULL;
-       struct smb_filename *smb_fname = NULL;
+       struct smb_filename *smb_fname_dst = NULL;
        bool dest_has_wcard = False;
        NTSTATUS status = NT_STATUS_OK;
        char *p;
@@ -5735,7 +6054,7 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                /* Create an smb_fname to call rename_internals_fsp() with. */
                status = create_synthetic_smb_fname(talloc_tos(),
                    fsp->base_fsp->fsp_name->base_name, newname, NULL,
-                   &smb_fname);
+                   &smb_fname_dst);
                if (!NT_STATUS_IS_OK(status)) {
                        goto out;
                }
@@ -5744,28 +6063,31 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                 * Set the original last component, since
                 * rename_internals_fsp() requires it.
                 */
-               smb_fname->original_lcomp = talloc_strdup(smb_fname, newname);
-               if (smb_fname->original_lcomp == NULL) {
+               smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
+                                                             newname);
+               if (smb_fname_dst->original_lcomp == NULL) {
                        status = NT_STATUS_NO_MEMORY;
                        goto out;
                }
 
-               /* Create a char * to call rename_internals() with. */
-               base_name = talloc_asprintf(ctx, "%s%s",
-                                          fsp->base_fsp->fsp_name->base_name,
-                                          newname);
-               if (!base_name) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto out;
-               }
        } else {
+               /*
+                * Build up an smb_fname_dst based on the filename passed in.
+                * We basically just strip off the last component, and put on
+                * the newname instead.
+                */
+               char *base_name = NULL;
+
                /* newname must *not* be a stream name. */
                if (newname[0] == ':') {
                        return NT_STATUS_NOT_SUPPORTED;
                }
 
-               /* Create the base directory. */
-               base_name = talloc_strdup(ctx, fname);
+               /*
+                * Strip off the last component (filename) of the path passed
+                * in.
+                */
+               base_name = talloc_strdup(ctx, smb_fname_src->base_name);
                if (!base_name) {
                        return NT_STATUS_NO_MEMORY;
                }
@@ -5786,8 +6108,11 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                        return NT_STATUS_NO_MEMORY;
                }
 
-               status = unix_convert(ctx, conn, base_name, &smb_fname,
-                                     UCF_SAVE_LCOMP);
+               status = unix_convert(ctx, conn, base_name, &smb_fname_dst,
+                                     (UCF_SAVE_LCOMP |
+                                         (dest_has_wcard ?
+                                             UCF_ALWAYS_ALLOW_WCARD_LCOMP :
+                                             0)));
 
                /* If an error we expect this to be
                 * NT_STATUS_OBJECT_PATH_NOT_FOUND */
@@ -5798,32 +6123,35 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                                goto out;
                        }
                        /* Create an smb_fname to call rename_internals_fsp() */
-                       status = create_synthetic_smb_fname(talloc_tos(),
+                       status = create_synthetic_smb_fname(ctx,
                                                            base_name, NULL,
-                                                           NULL, &smb_fname);
+                                                           NULL,
+                                                           &smb_fname_dst);
                        if (!NT_STATUS_IS_OK(status)) {
                                goto out;
                        }
                }
-
        }
 
        if (fsp) {
                DEBUG(10,("smb_file_rename_information: "
                          "SMB_FILE_RENAME_INFORMATION (fnum %d) %s -> %s\n",
-                         fsp->fnum, fsp_str_dbg(fsp), base_name));
-               status = rename_internals_fsp(conn, fsp, smb_fname, 0,
+                         fsp->fnum, fsp_str_dbg(fsp),
+                         smb_fname_str_dbg(smb_fname_dst)));
+               status = rename_internals_fsp(conn, fsp, smb_fname_dst, 0,
                                              overwrite);
        } else {
                DEBUG(10,("smb_file_rename_information: "
                          "SMB_FILE_RENAME_INFORMATION %s -> %s\n",
-                         fname, base_name));
-               status = rename_internals(ctx, conn, req, fname, base_name, 0,
-                                       overwrite, False, dest_has_wcard,
-                                       FILE_WRITE_ATTRIBUTES);
+                         smb_fname_str_dbg(smb_fname_src),
+                         smb_fname_str_dbg(smb_fname_dst)));
+               status = rename_internals(ctx, conn, req, smb_fname_src,
+                                         smb_fname_dst, 0, overwrite, false,
+                                         dest_has_wcard,
+                                         FILE_WRITE_ATTRIBUTES);
        }
  out:
-       TALLOC_FREE(smb_fname);
+       TALLOC_FREE(smb_fname_dst);
        return status;
 }
 
@@ -6016,100 +6344,85 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
 }
 
 /****************************************************************************
- Deal with SMB_INFO_STANDARD.
+ Deal with SMB_SET_FILE_BASIC_INFO.
 ****************************************************************************/
 
-static NTSTATUS smb_set_info_standard(connection_struct *conn,
+static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
                                        const char *pdata,
                                        int total_data,
                                        files_struct *fsp,
                                        const struct smb_filename *smb_fname)
 {
+       /* Patch to do this correctly from Paul Eggert <eggert@twinsun.com>. */
        struct smb_file_time ft;
+       uint32 dosmode = 0;
+       NTSTATUS status = NT_STATUS_OK;
+
        ZERO_STRUCT(ft);
 
-       if (total_data < 12) {
+       if (total_data < 36) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       /* Set the attributes */
+       dosmode = IVAL(pdata,32);
+       status = smb_set_file_dosmode(conn, smb_fname, dosmode);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
        /* create time */
        ft.create_time = interpret_long_date(pdata);
 
        /* access time */
-       ft.atime = interpret_long_date(pdata + 8);
+       ft.atime = interpret_long_date(pdata+8);
 
-       /* write time */
-       ft.mtime = interpret_long_date(pdata + 16);
+       /* write time. */
+       ft.mtime = interpret_long_date(pdata+16);
 
-       DEBUG(10,("smb_set_info_standard: file %s\n",
-                 smb_fname_str_dbg(smb_fname)));
+       /* change time. */
+       ft.ctime = interpret_long_date(pdata+24);
+
+       DEBUG(10, ("smb_set_file_basic_info: file %s\n",
+                  smb_fname_str_dbg(smb_fname)));
 
-       return smb_set_file_time(conn, fsp, smb_fname, &ft, true);
+       return smb_set_file_time(conn, fsp, smb_fname, &ft,
+                                true);
 }
 
 /****************************************************************************
- Deal with SMB_SET_FILE_BASIC_INFO.
+ Deal with SMB_INFO_STANDARD.
 ****************************************************************************/
 
-static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
+static NTSTATUS smb_set_info_standard(connection_struct *conn,
                                        const char *pdata,
                                        int total_data,
                                        files_struct *fsp,
                                        const struct smb_filename *smb_fname)
 {
-       /* Patch to do this correctly from Paul Eggert <eggert@twinsun.com>. */
-       struct timespec write_time;
-       struct timespec changed_time;
        struct smb_file_time ft;
-       uint32 dosmode = 0;
-       NTSTATUS status = NT_STATUS_OK;
-       bool setting_write_time = true;
 
        ZERO_STRUCT(ft);
 
-       if (total_data < 36) {
+       if (total_data < 12) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* Set the attributes */
-       dosmode = IVAL(pdata,32);
-       status = smb_set_file_dosmode(conn, smb_fname, dosmode);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       /* access time */
-       ft.atime = interpret_long_date(pdata+8);
-
-       write_time = interpret_long_date(pdata+16);
-       changed_time = interpret_long_date(pdata+24);
-
-       /* mtime */
-       ft.mtime = timespec_min(&write_time, &changed_time);
-
        /* create time */
-       ft.create_time = interpret_long_date(pdata);
-
-       if ((timespec_compare(&write_time, &ft.mtime) == 1) &&
-           !null_timespec(write_time)) {
-               ft.mtime = write_time;
-       }
-
-       /* Prefer a defined time to an undefined one. */
-       if (null_timespec(ft.mtime)) {
-               if (null_timespec(write_time)) {
-                       ft.mtime = changed_time;
-                       setting_write_time = false;
-               } else {
-                       ft.mtime = write_time;
-               }
-       }
+       ft.create_time = convert_time_t_to_timespec(srv_make_unix_date2(pdata));
+       /* access time */
+       ft.atime = convert_time_t_to_timespec(srv_make_unix_date2(pdata+4));
+       /* write time */
+       ft.mtime = convert_time_t_to_timespec(srv_make_unix_date2(pdata+8));
 
-       DEBUG(10, ("smb_set_file_basic_info: file %s\n",
-                  smb_fname_str_dbg(smb_fname)));
+       DEBUG(10,("smb_set_info_standard: file %s\n",
+               smb_fname_str_dbg(smb_fname)));
 
-       return smb_set_file_time(conn, fsp, smb_fname, &ft,
-                                setting_write_time);
+        return smb_set_file_time(conn,
+                                fsp,
+                               smb_fname,
+                               &ft,
+                                true);
 }
 
 /****************************************************************************
@@ -6227,7 +6540,8 @@ static NTSTATUS smb_set_file_end_of_file_info(connection_struct *conn,
                                        const char *pdata,
                                        int total_data,
                                        files_struct *fsp,
-                                       const struct smb_filename *smb_fname)
+                                       const struct smb_filename *smb_fname,
+                                       bool fail_after_createfile)
 {
        SMB_OFF_T size;
 
@@ -6252,7 +6566,8 @@ static NTSTATUS smb_set_file_end_of_file_info(connection_struct *conn,
                                fsp,
                                smb_fname,
                                &smb_fname->st,
-                               size);
+                               size,
+                               fail_after_createfile);
 }
 
 /****************************************************************************
@@ -6538,7 +6853,8 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
                                   fsp,
                                   smb_fname,
                                   &sbuf,
-                                  size);
+                                  size,
+                                  false);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -7066,7 +7382,6 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                                int *ret_data_size)
 {
        char *pdata = *ppdata;
-       char *fname = NULL;
        NTSTATUS status = NT_STATUS_OK;
        int data_return_size = 0;
 
@@ -7084,11 +7399,6 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                }
        }
 
-       status = get_full_smb_filename(mem_ctx, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
        DEBUG(3,("smbd_do_setfilepathinfo: %s (fnum %d) info_level=%d "
                 "totdata=%d\n", smb_fname_str_dbg(smb_fname),
                 fsp ? fsp->fnum : -1, info_level, total_data));
@@ -7140,11 +7450,22 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                case SMB_FILE_END_OF_FILE_INFORMATION:
                case SMB_SET_FILE_END_OF_FILE_INFO:
                {
+                       /*
+                        * XP/Win7 both fail after the createfile with
+                        * SMB_SET_FILE_END_OF_FILE_INFO but not
+                        * SMB_FILE_END_OF_FILE_INFORMATION (pass-through).
+                        * The level is known here, so pass it down
+                        * appropriately.
+                        */
+                       bool should_fail =
+                           (info_level == SMB_SET_FILE_END_OF_FILE_INFO);
+
                        status = smb_set_file_end_of_file_info(conn, req,
                                                                pdata,
                                                                total_data,
                                                                fsp,
-                                                               smb_fname);
+                                                               smb_fname,
+                                                               should_fail);
                        break;
                }
 
@@ -7177,6 +7498,15 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                        break;
                }
 
+               case SMB_FILE_FULL_EA_INFORMATION:
+               {
+                       status = smb_set_file_full_ea_info(conn,
+                                               pdata,
+                                               total_data,
+                                               fsp);
+                       break;
+               }
+
                /* From tridge Samba4 : 
                 * MODE_INFORMATION in setfileinfo (I have no
                 * idea what "mode information" on a file is - it takes a value of 0,
@@ -7242,10 +7572,18 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                {
                        status = smb_file_rename_information(conn, req,
                                                             pdata, total_data,
-                                                            fsp, fname);
+                                                            fsp, smb_fname);
                        break;
                }
 
+               case SMB2_FILE_RENAME_INFORMATION_INTERNAL:
+               {
+                       /* SMB2 rename information. */
+                       status = smb2_file_rename_information(conn, req,
+                                                            pdata, total_data,
+                                                            fsp, smb_fname);
+                       break;
+               }
 #if defined(HAVE_POSIX_ACLS)
                case SMB_SET_POSIX_ACL:
                {
@@ -7397,7 +7735,8 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                                    max_data_bytes);
                                return;
                        } else {
-                               reply_doserror(req, ERRDOS, ERRbadpath);
+                               reply_nterror(req,
+                                       NT_STATUS_OBJECT_PATH_NOT_FOUND);
                                return;
                        }
                } else {
@@ -7437,6 +7776,8 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                status = filename_convert(req, conn,
                                         req->flags2 & FLAGS2_DFS_PATHNAMES,
                                         fname,
+                                        0,
+                                        NULL,
                                         &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
                        if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
@@ -7535,7 +7876,7 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
        TALLOC_CTX *ctx = talloc_tos();
 
        if (!CAN_WRITE(conn)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -7558,6 +7899,8 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                directory,
+                               0,
+                               NULL,
                                &smb_dname);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -7752,7 +8095,7 @@ static void call_trans2getdfsreferral(connection_struct *conn,
        max_referral_level = SVAL(params,0);
 
        if(!lp_host_msdfs()) {
-               reply_doserror(req, ERRDOS, ERRbadfunc);
+               reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
                return;
        }
 
@@ -7794,7 +8137,7 @@ static void call_trans2ioctl(connection_struct *conn,
        /* check for an invalid fid before proceeding */
 
        if (!fsp) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
 
@@ -7823,7 +8166,7 @@ static void call_trans2ioctl(connection_struct *conn,
        }
 
        DEBUG(2,("Unknown TRANS2_IOCTL\n"));
-       reply_doserror(req, ERRSRV, ERRerror);
+       reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
 }
 
 /****************************************************************************
@@ -7833,6 +8176,7 @@ static void call_trans2ioctl(connection_struct *conn,
 void reply_findclose(struct smb_request *req)
 {
        int dptr_num;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        START_PROFILE(SMBfindclose);
 
@@ -7846,7 +8190,7 @@ void reply_findclose(struct smb_request *req)
 
        DEBUG(3,("reply_findclose, dptr_num = %d\n", dptr_num));
 
-       dptr_close(&dptr_num);
+       dptr_close(sconn, &dptr_num);
 
        reply_outbuf(req, 0, 0);
 
@@ -7891,7 +8235,7 @@ void reply_findnclose(struct smb_request *req)
 static void handle_trans2(connection_struct *conn, struct smb_request *req,
                          struct trans_state *state)
 {
-       if (Protocol >= PROTOCOL_NT1) {
+       if (get_Protocol() >= PROTOCOL_NT1) {
                req->flags2 |= 0x40; /* IS_LONG_NAME */
                SSVAL(req->inbuf,smb_flg2,req->flags2);
        }
@@ -8048,7 +8392,7 @@ static void handle_trans2(connection_struct *conn, struct smb_request *req,
        default:
                /* Error in request */
                DEBUG(2,("Unknown request %d in trans2 call\n", state->call));
-               reply_doserror(req, ERRSRV,ERRerror);
+               reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
        }
 }
 
@@ -8100,7 +8444,7 @@ void reply_trans2(struct smb_request *req)
                case TRANSACT2_SETFSINFO:
                        break;
                default:
-                       reply_doserror(req, ERRSRV, ERRaccess);
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBtrans2);
                        return;
                }