smbd: make it possible to disable get_file_infos() on searches
[jra/samba/.git] / source3 / smbd / trans2.c
index a01e79b6243dce064736765f45caa440e88ac7cd..05e8375d05b5be7742e86d3b3112228def255a82 100644 (file)
@@ -1,11 +1,11 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    SMB transaction2 handling
    Copyright (C) Jeremy Allison                        1994-2007
    Copyright (C) Stefan (metze) Metzmacher     2003
    Copyright (C) Volker Lendecke               2005-2007
    Copyright (C) Steve French                  2005
-   Copyright (C) James Peach                   2007
+   Copyright (C) James Peach                   2006-2007
 
    Extensively modified by Andrew Tridgell, 1995
 
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@@ -27,7 +27,6 @@
 
 extern int max_send;
 extern enum protocol_types Protocol;
-extern int smb_read_error;
 extern uint32 global_client_caps;
 extern struct current_user current_user;
 
@@ -94,7 +93,7 @@ SMB_BIG_UINT get_allocation_size(connection_struct *conn, files_struct *fsp, con
  Refuse to allow clients to overwrite our private xattrs.
 ****************************************************************************/
 
-static BOOL samba_private_attr_name(const char *unix_ea_name)
+static bool samba_private_attr_name(const char *unix_ea_name)
 {
        static const char *prohibited_ea_names[] = {
                SAMBA_POSIX_INHERITANCE_EA_NAME,
@@ -106,17 +105,22 @@ static BOOL samba_private_attr_name(const char *unix_ea_name)
 
        for (i = 0; prohibited_ea_names[i]; i++) {
                if (strequal( prohibited_ea_names[i], unix_ea_name))
-                       return True;
+                       return true;
        }
-       return False;
+       if (StrnCaseCmp(unix_ea_name, SAMBA_XATTR_DOSSTREAM_PREFIX,
+                       strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) == 0) {
+               return true;
+       }
+       return false;
 }
 
 /****************************************************************************
  Get one EA value. Fill in a struct ea_struct.
 ****************************************************************************/
 
-static BOOL get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
-                               const char *fname, char *ea_name, struct ea_struct *pea)
+NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
+                     files_struct *fsp, const char *fname,
+                     const char *ea_name, struct ea_struct *pea)
 {
        /* Get the value of this xattr. Max size is 64k. */
        size_t attr_size = 256;
@@ -127,11 +131,11 @@ static BOOL get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn, files_str
 
        val = TALLOC_REALLOC_ARRAY(mem_ctx, val, char, attr_size);
        if (!val) {
-               return False;
+               return NT_STATUS_NO_MEMORY;
        }
 
        if (fsp && fsp->fh->fd != -1) {
-               sizeret = SMB_VFS_FGETXATTR(fsp, fsp->fh->fd, ea_name, val, attr_size);
+               sizeret = SMB_VFS_FGETXATTR(fsp, ea_name, val, attr_size);
        } else {
                sizeret = SMB_VFS_GETXATTR(conn, fname, ea_name, val, attr_size);
        }
@@ -142,101 +146,200 @@ static BOOL get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn, files_str
        }
 
        if (sizeret == -1) {
-               return False;
+               return map_nt_error_from_unix(errno);
        }
 
-       DEBUG(10,("get_ea_value: EA %s is of length %u", ea_name, (unsigned int)sizeret));
+       DEBUG(10,("get_ea_value: EA %s is of length %u\n", ea_name, (unsigned int)sizeret));
        dump_data(10, (uint8 *)val, sizeret);
 
        pea->flags = 0;
        if (strnequal(ea_name, "user.", 5)) {
-               pea->name = &ea_name[5];
+               pea->name = talloc_strdup(mem_ctx, &ea_name[5]);
        } else {
-               pea->name = ea_name;
+               pea->name = talloc_strdup(mem_ctx, ea_name);
+       }
+       if (pea->name == NULL) {
+               TALLOC_FREE(val);
+               return NT_STATUS_NO_MEMORY;
        }
        pea->value.data = (unsigned char *)val;
        pea->value.length = (size_t)sizeret;
-       return True;
+       return NT_STATUS_OK;
 }
 
-/****************************************************************************
- Return a linked list of the total EA's. Plus the total size
-****************************************************************************/
-
-static struct ea_list *get_ea_list_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
-                                       const char *fname, size_t *pea_total_len)
+NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
+                               files_struct *fsp, const char *fname,
+                               char ***pnames, size_t *pnum_names)
 {
        /* Get a list of all xattrs. Max namesize is 64k. */
        size_t ea_namelist_size = 1024;
-       char *ea_namelist;
+       char *ea_namelist = NULL;
+
        char *p;
+       char **names, **tmp;
+       size_t num_names;
        ssize_t sizeret;
-       int i;
-       struct ea_list *ea_list_head = NULL;
-
-       *pea_total_len = 0;
 
        if (!lp_ea_support(SNUM(conn))) {
-               return NULL;
+               *pnames = NULL;
+               *pnum_names = 0;
+               return NT_STATUS_OK;
        }
 
-       for (i = 0, ea_namelist = TALLOC_ARRAY(mem_ctx, char, ea_namelist_size); i < 6;
-            ea_namelist = TALLOC_REALLOC_ARRAY(mem_ctx, ea_namelist, char, ea_namelist_size), i++) {
+       /*
+        * TALLOC the result early to get the talloc hierarchy right.
+        */
 
-               if (!ea_namelist) {
-                       return NULL;
+       names = TALLOC_ARRAY(mem_ctx, char *, 1);
+       if (names == NULL) {
+               DEBUG(0, ("talloc failed\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       while (ea_namelist_size <= 65536) {
+
+               ea_namelist = TALLOC_REALLOC_ARRAY(
+                       names, ea_namelist, char, ea_namelist_size);
+               if (ea_namelist == NULL) {
+                       DEBUG(0, ("talloc failed\n"));
+                       TALLOC_FREE(names);
+                       return NT_STATUS_NO_MEMORY;
                }
 
                if (fsp && fsp->fh->fd != -1) {
-                       sizeret = SMB_VFS_FLISTXATTR(fsp, fsp->fh->fd, ea_namelist, ea_namelist_size);
+                       sizeret = SMB_VFS_FLISTXATTR(fsp, ea_namelist,
+                                                    ea_namelist_size);
                } else {
-                       sizeret = SMB_VFS_LISTXATTR(conn, fname, ea_namelist, ea_namelist_size);
+                       sizeret = SMB_VFS_LISTXATTR(conn, fname, ea_namelist,
+                                                   ea_namelist_size);
                }
 
-               if (sizeret == -1 && errno == ERANGE) {
+               if ((sizeret == -1) && (errno == ERANGE)) {
                        ea_namelist_size *= 2;
-               } else {
+               }
+               else {
                        break;
                }
        }
 
-       if (sizeret == -1)
+       if (sizeret == -1) {
+               TALLOC_FREE(names);
+               return map_nt_error_from_unix(errno);
+       }
+
+       DEBUG(10, ("get_ea_list_from_file: ea_namelist size = %u\n",
+                  (unsigned int)sizeret));
+
+       if (sizeret == 0) {
+               TALLOC_FREE(names);
+               *pnames = NULL;
+               *pnum_names = 0;
+               return NT_STATUS_OK;
+       }
+
+       /*
+        * Ensure the result is 0-terminated
+        */
+
+       if (ea_namelist[sizeret-1] != '\0') {
+               TALLOC_FREE(names);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       /*
+        * count the names
+        */
+       num_names = 0;
+
+       for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
+               num_names += 1;
+       }
+
+       tmp = TALLOC_REALLOC_ARRAY(mem_ctx, names, char *, num_names);
+       if (tmp == NULL) {
+               DEBUG(0, ("talloc failed\n"));
+               TALLOC_FREE(names);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       names = tmp;
+       num_names = 0;
+
+       for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
+               names[num_names++] = p;
+       }
+
+       *pnames = names;
+       *pnum_names = num_names;
+       return NT_STATUS_OK;
+}
+
+/****************************************************************************
+ Return a linked list of the total EA's. Plus the total size
+****************************************************************************/
+
+static struct ea_list *get_ea_list_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn, files_struct *fsp,
+                                       const char *fname, size_t *pea_total_len)
+{
+       /* Get a list of all xattrs. Max namesize is 64k. */
+       size_t i, num_names;
+       char **names;
+       struct ea_list *ea_list_head = NULL;
+       NTSTATUS status;
+
+       *pea_total_len = 0;
+
+       if (!lp_ea_support(SNUM(conn))) {
                return NULL;
+       }
 
-       DEBUG(10,("get_ea_list_from_file: ea_namelist size = %u\n", (unsigned int)sizeret ));
+       status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
+                                       &names, &num_names);
 
-       if (sizeret) {
-               for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p) + 1) {
-                       struct ea_list *listp;
+       if (!NT_STATUS_IS_OK(status) || (num_names == 0)) {
+               return NULL;
+       }
 
-                       if (strnequal(p, "system.", 7) || samba_private_attr_name(p))
-                               continue;
-               
-                       listp = TALLOC_P(mem_ctx, struct ea_list);
-                       if (!listp)
-                               return NULL;
+       for (i=0; i<num_names; i++) {
+               struct ea_list *listp;
+               fstring dos_ea_name;
 
-                       if (!get_ea_value(mem_ctx, conn, fsp, fname, p, &listp->ea)) {
-                               return NULL;
-                       }
+               if (strnequal(names[i], "system.", 7)
+                   || samba_private_attr_name(names[i]))
+                       continue;
 
-                       {
-                               fstring dos_ea_name;
-                               push_ascii_fstring(dos_ea_name, listp->ea.name);
-                               *pea_total_len += 4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
-                               DEBUG(10,("get_ea_list_from_file: total_len = %u, %s, val len = %u\n",
-                                       (unsigned int)*pea_total_len, dos_ea_name,
-                                       (unsigned int)listp->ea.value.length ));
-                       }
-                       DLIST_ADD_END(ea_list_head, listp, struct ea_list *);
+               listp = TALLOC_P(mem_ctx, struct ea_list);
+               if (listp == NULL) {
+                       return NULL;
                }
-               /* Add on 4 for total length. */
-               if (*pea_total_len) {
-                       *pea_total_len += 4;
+
+               if (!NT_STATUS_IS_OK(get_ea_value(mem_ctx, conn, fsp,
+                                                 fname, names[i],
+                                                 &listp->ea))) {
+                       return NULL;
                }
+
+               push_ascii_fstring(dos_ea_name, listp->ea.name);
+
+               *pea_total_len +=
+                       4 + strlen(dos_ea_name) + 1 + listp->ea.value.length;
+
+               DEBUG(10,("get_ea_list_from_file: total_len = %u, %s, val len "
+                         "= %u\n", (unsigned int)*pea_total_len, dos_ea_name,
+                         (unsigned int)listp->ea.value.length));
+
+               DLIST_ADD_END(ea_list_head, listp, struct ea_list *);
+
        }
 
-       DEBUG(10,("get_ea_list_from_file: total_len = %u\n", (unsigned int)*pea_total_len));
+       /* Add on 4 for total length. */
+       if (*pea_total_len) {
+               *pea_total_len += 4;
+       }
+
+       DEBUG(10, ("get_ea_list_from_file: total_len = %u\n",
+                  (unsigned int)*pea_total_len));
+
        return ea_list_head;
 }
 
@@ -298,9 +401,8 @@ static unsigned int estimate_ea_size(connection_struct *conn, files_struct *fsp,
        if (!lp_ea_support(SNUM(conn))) {
                return 0;
        }
-       mem_ctx = talloc_init("estimate_ea_size");
+       mem_ctx = talloc_tos();
        (void)get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
-       talloc_destroy(mem_ctx);
        return total_ea_len;
 }
 
@@ -311,7 +413,7 @@ static unsigned int estimate_ea_size(connection_struct *conn, files_struct *fsp,
 static void canonicalize_ea_name(connection_struct *conn, files_struct *fsp, const char *fname, fstring unix_ea_name)
 {
        size_t total_ea_len;
-       TALLOC_CTX *mem_ctx = talloc_init("canonicalize_ea_name");
+       TALLOC_CTX *mem_ctx = talloc_tos();
        struct ea_list *ea_list = get_ea_list_from_file(mem_ctx, conn, fsp, fname, &total_ea_len);
 
        for (; ea_list; ea_list = ea_list->next) {
@@ -322,7 +424,6 @@ static void canonicalize_ea_name(connection_struct *conn, files_struct *fsp, con
                        break;
                }
        }
-       talloc_destroy(mem_ctx);
 }
 
 /****************************************************************************
@@ -356,7 +457,7 @@ NTSTATUS set_ea(connection_struct *conn, files_struct *fsp, const char *fname, s
                        if (fsp && (fsp->fh->fd != -1)) {
                                DEBUG(10,("set_ea: deleting ea name %s on file %s by file descriptor.\n",
                                        unix_ea_name, fsp->fsp_name));
-                               ret = SMB_VFS_FREMOVEXATTR(fsp, fsp->fh->fd, unix_ea_name);
+                               ret = SMB_VFS_FREMOVEXATTR(fsp, unix_ea_name);
                        } else {
                                DEBUG(10,("set_ea: deleting ea name %s on file %s.\n",
                                        unix_ea_name, fname));
@@ -374,7 +475,7 @@ NTSTATUS set_ea(connection_struct *conn, files_struct *fsp, const char *fname, s
                        if (fsp && (fsp->fh->fd != -1)) {
                                DEBUG(10,("set_ea: setting ea name %s on file %s by file descriptor.\n",
                                        unix_ea_name, fsp->fsp_name));
-                               ret = SMB_VFS_FSETXATTR(fsp, fsp->fh->fd, unix_ea_name,
+                               ret = SMB_VFS_FSETXATTR(fsp, unix_ea_name,
                                                        ea_list->ea.value.data, ea_list->ea.value.length, 0);
                        } else {
                                DEBUG(10,("set_ea: setting ea name %s on file %s.\n",
@@ -576,7 +677,8 @@ static struct ea_list *ea_list_union(struct ea_list *name_list, struct ea_list *
   HACK ! Always assumes smb_setup field is zero.
 ****************************************************************************/
 
-void send_trans2_replies(struct smb_request *req,
+void send_trans2_replies(connection_struct *conn,
+                       struct smb_request *req,
                         const char *params,
                         int paramsize,
                         const char *pdata,
@@ -597,7 +699,7 @@ void send_trans2_replies(struct smb_request *req,
        int params_sent_thistime, data_sent_thistime, total_sent_thistime;
        int alignment_offset = 1; /* JRA. This used to be 3. Set to 1 to make netmon parse ok. */
        int data_alignment_offset = 0;
-       BOOL overflow = False;
+       bool overflow = False;
 
        /* Modify the data_to_send and datasize and set the error if
           we're trying to send more than max_data_bytes. We still send
@@ -738,8 +840,10 @@ void send_trans2_replies(struct smb_request *req,
 
                /* Send the packet */
                show_msg((char *)req->outbuf);
-               if (!send_smb(smbd_server_fd(),(char *)req->outbuf))
-                       exit_server_cleanly("send_trans2_replies: send_smb failed.");
+               if (!srv_send_smb(smbd_server_fd(),
+                               (char *)req->outbuf,
+                               IS_CONN_ENCRYPTED(conn)))
+                       exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
 
                TALLOC_FREE(req->outbuf);
 
@@ -774,16 +878,16 @@ static void call_trans2open(connection_struct *conn,
        char *pdata = *ppdata;
        int deny_mode;
        int32 open_attr;
-       BOOL oplock_request;
+       bool oplock_request;
 #if 0
-       BOOL return_additional_info;
+       bool return_additional_info;
        int16 open_sattr;
        time_t open_time;
 #endif
        int open_ofun;
        uint32 open_size;
        char *pname;
-       pstring fname;
+       char *fname = NULL;
        SMB_OFF_T size=0;
        int fattr=0,mtime=0;
        SMB_INO_T inode = 0;
@@ -797,6 +901,7 @@ static void call_trans2open(connection_struct *conn,
        uint32 share_mode;
        uint32 create_disposition;
        uint32 create_options = 0;
+       TALLOC_CTX *ctx = talloc_tos();
 
        /*
         * Ensure we have enough parameters to perform the operation.
@@ -829,8 +934,8 @@ static void call_trans2open(connection_struct *conn,
                return;
        }
 
-       srvstr_get_path(params, req->flags2, fname, pname,
-                       sizeof(fname), total_params - 28, STR_TERMINATE,
+       srvstr_get_path(ctx, params, req->flags2, &fname, pname,
+                       total_params - 28, STR_TERMINATE,
                        &status);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
@@ -841,20 +946,6 @@ static void call_trans2open(connection_struct *conn,
                fname, (unsigned int)deny_mode, (unsigned int)open_attr,
                (unsigned int)open_ofun, open_size));
 
-       /* XXXX we need to handle passed times, sattr and flags */
-
-       status = unix_convert(conn, fname, False, NULL, &sbuf);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               return;
-       }
-    
-       status = check_name(conn, fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               return;
-       }
-
        if (open_ofun == 0) {
                reply_nterror(req, NT_STATUS_OBJECT_NAME_COLLISION);
                return;
@@ -888,7 +979,7 @@ static void call_trans2open(connection_struct *conn,
                        return;
                }
 
-               ea_list = read_ea_list(tmp_talloc_ctx(), pdata + 4,
+               ea_list = read_ea_list(talloc_tos(), pdata + 4,
                                       total_data - 4);
                if (!ea_list) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -899,37 +990,34 @@ static void call_trans2open(connection_struct *conn,
                return;
        }
 
-       status = open_file_ntcreate(conn, req, fname, &sbuf,
-               access_mask,
-               share_mode,
-               create_disposition,
-               create_options,
-               open_attr,
-               oplock_request,
-               &smb_action, &fsp);
+       status = create_file(conn,                      /* conn */
+                            req,                       /* req */
+                            0,                         /* root_dir_fid */
+                            fname,                     /* fname */
+                            access_mask,               /* access_mask */
+                            share_mode,                /* share_access */
+                            create_disposition,        /* create_disposition*/
+                            create_options,            /* create_options */
+                            open_attr,                 /* file_attributes */
+                            oplock_request,            /* oplock_request */
+                            open_size,                 /* allocation_size */
+                            NULL,                      /* sd */
+                            ea_list,                   /* ea_list */
+                            &fsp,                      /* result */
+                            &smb_action,               /* pinfo */
+                            &sbuf);                    /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call. */
                        return;
                }
-               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
-                       /*
-                        * We hit an existing file, and if we're returning DOS
-                        * error codes OBJECT_NAME_COLLISION would map to
-                        * ERRDOS/183, we need to return ERRDOS/80, see bug
-                        * 4852.
-                        */
-                       reply_botherror(req, NT_STATUS_OBJECT_NAME_COLLISION,
-                               ERRDOS, ERRfilexists);
-                       return;
-               }
-               reply_nterror(req, status);
+               reply_openerror(req, status);
                return;
        }
 
        size = get_file_size(sbuf);
-       fattr = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
        mtime = sbuf.st_mtime;
        inode = sbuf.st_ino;
        if (fattr & aDIR) {
@@ -938,41 +1026,6 @@ static void call_trans2open(connection_struct *conn,
                return;
        }
 
-       /* Save the requested allocation size. */
-       /* Allocate space for the file if a size hint is supplied */
-       if ((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) {
-               SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)open_size;
-               if (allocation_size && (allocation_size > (SMB_BIG_UINT)size)) {
-                        fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
-                        if (fsp->is_directory) {
-                                close_file(fsp,ERROR_CLOSE);
-                                /* Can't set allocation size on a directory. */
-                               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
-                               return;
-                        }
-                        if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
-                                close_file(fsp,ERROR_CLOSE);
-                               reply_nterror(req, NT_STATUS_DISK_FULL);
-                               return;
-                        }
-
-                       /* Adjust size here to return the right size in the reply.
-                          Windows does it this way. */
-                       size = fsp->initial_allocation_size;
-                } else {
-                        fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)size);
-                }
-       }
-
-       if (ea_list && smb_action == FILE_WAS_CREATED) {
-               status = set_ea(conn, fsp, fname, ea_list);
-               if (!NT_STATUS_IS_OK(status)) {
-                       close_file(fsp,ERROR_CLOSE);
-                       reply_nterror(req, status);
-                       return;
-               }
-       }
-
        /* Realloc the size of parameters and data we will return */
        *pparams = (char *)SMB_REALLOC(*pparams, 30);
        if(*pparams == NULL ) {
@@ -1001,14 +1054,14 @@ static void call_trans2open(connection_struct *conn,
        SIVAL(params,20,inode);
        SSVAL(params,24,0); /* Padding. */
        if (flags & 8) {
-               uint32 ea_size = estimate_ea_size(conn, fsp, fname);
+               uint32 ea_size = estimate_ea_size(conn, fsp, fsp->fsp_name);
                SIVAL(params, 26, ea_size);
        } else {
                SIVAL(params, 26, 0);
        }
 
        /* Send the required number of replies */
-       send_trans2_replies(req, params, 30, *ppdata, 0, max_data_bytes);
+       send_trans2_replies(conn, req, params, 30, *ppdata, 0, max_data_bytes);
 }
 
 /*********************************************************
@@ -1018,7 +1071,9 @@ static void call_trans2open(connection_struct *conn,
  Case can be significant or not.
 **********************************************************/
 
-static BOOL exact_match(connection_struct *conn, char *str, char *mask)
+static bool exact_match(connection_struct *conn,
+               const char *str,
+               const char *mask)
 {
        if (mask[0] == '.' && mask[1] == 0)
                return False;
@@ -1144,21 +1199,30 @@ static NTSTATUS unix_perms_from_wire( connection_struct *conn,
  Get a level dependent lanman2 dir entry.
 ****************************************************************************/
 
-static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
-                                char *path_mask,uint32 dirtype,int info_level,
-                                int requires_resume_key,
-                                BOOL dont_descend,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, TALLOC_CTX *ea_ctx)
+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)
 {
        const char *dname;
-       BOOL found = False;
+       bool found = False;
        SMB_STRUCT_STAT sbuf;
-       pstring mask;
-       pstring pathreal;
-       pstring fname;
+       const char *mask = NULL;
+       char *pathreal = NULL;
+       const char *fname = NULL;
        char *p, *q, *pdata = *ppdata;
        uint32 reskey=0;
        long prev_dirpos=0;
@@ -1170,12 +1234,12 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
        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;
+       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);
+       bool needslash = ( conn->dirpath[strlen(conn->dirpath) -1] != '/');
+       bool check_mangled_names = lp_manglednames(conn->params);
+       char mangled_name[13]; /* mangled 8.3 name. */
 
-       *fname = 0;
        *out_of_space = False;
        *got_exact_match = False;
 
@@ -1183,26 +1247,28 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
        ZERO_STRUCT(adate_ts);
        ZERO_STRUCT(create_date_ts);
 
-       if (!conn->dirptr)
+       if (!conn->dirptr) {
                return(False);
+       }
 
        p = strrchr_m(path_mask,'/');
        if(p != NULL) {
-               if(p[1] == '\0')
-                       pstrcpy(mask,"*.*");
-               else
-                       pstrcpy(mask, p+1);
-       } else
-               pstrcpy(mask, path_mask);
-
+               if(p[1] == '\0') {
+                       mask = talloc_strdup(ctx,"*.*");
+               } else {
+                       mask = p+1;
+               }
+       } else {
+               mask = path_mask;
+       }
 
        while (!found) {
-               BOOL got_match;
-               BOOL ms_dfs_link = False;
+               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(conn->dirptr,&curr_dirpos,&sbuf);
+               dname = dptr_ReadDirName(ctx,conn->dirptr,&curr_dirpos,&sbuf);
 
                /*
                 * Due to bugs in NT client redirectors we are not using
@@ -1215,7 +1281,7 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
 
                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);
                }
@@ -1226,10 +1292,16 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                 * pathreal which is composed from dname.
                 */
 
-               pstrcpy(fname,dname);      
+               pathreal = NULL;
+               fname = dname;
 
-               /* This will mangle fname if it's an illegal name. */
-               mangle_map(fname,False,True,conn->params);
+               /* 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)) {
+                               continue; /* Error - couldn't mangle. */
+                       }
+                       fname = mangled_name;
+               }
 
                if(!(got_match = *got_exact_match = exact_match(conn, fname, mask))) {
                        got_match = mask_match(fname, mask, conn->case_sensitive);
@@ -1237,51 +1309,60 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
 
                if(!got_match && check_mangled_names &&
                   !mangle_is_8_3(fname, False, conn->params)) {
-                       pstring mangled_name;
-
                        /*
                         * 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.
                         */
-
-                       pstrcpy(mangled_name, fname);
-
                        /* Force the mangling into 8.3. */
-                       mangle_map( mangled_name, True, False, conn->params);
+                       if (!name_to_8_3( fname, mangled_name, False, conn->params)) {
+                               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 = (strequal(dname,"..") || strequal(dname,"."));
+                       bool isdots = (ISDOT(dname) || ISDOTDOT(dname));
+
                        if (dont_descend && !isdots) {
                                continue;
                        }
-         
-                       pstrcpy(pathreal,conn->dirpath);
-                       if(needslash) {
-                               pstrcat(pathreal,"/");
+
+                       if (needslash) {
+                               pathreal = NULL;
+                               pathreal = talloc_asprintf(ctx,
+                                       "%s/%s",
+                                       conn->dirpath,
+                                       dname);
+                       } else {
+                               pathreal = talloc_asprintf(ctx,
+                                       "%s%s",
+                                       conn->dirpath,
+                                       dname);
+                       }
+
+                       if (!pathreal) {
+                               return False;
                        }
-                       pstrcat(pathreal,dname);
 
                        if (INFO_LEVEL_IS_UNIX(info_level)) {
                                if (SMB_VFS_LSTAT(conn,pathreal,&sbuf) != 0) {
                                        DEBUG(5,("get_lanman2_dir_entry:Couldn't lstat [%s] (%s)\n",
                                                pathreal,strerror(errno)));
+                                       TALLOC_FREE(pathreal);
                                        continue;
                                }
                        } else if (!VALID_STAT(sbuf) && SMB_VFS_STAT(conn,pathreal,&sbuf) != 0) {
-                               pstring link_target;
-
-                               /* Needed to show the msdfs symlinks as 
+                               /* Needed to show the msdfs symlinks as
                                 * directories */
 
-                               if(lp_host_msdfs() && 
+                               if(lp_host_msdfs() &&
                                   lp_msdfs_root(SNUM(conn)) &&
-                                  ((ms_dfs_link = is_msdfs_link(conn, pathreal, link_target, &sbuf)) == True)) {
+                                  ((ms_dfs_link = is_msdfs_link(conn, pathreal, &sbuf)) == True)) {
                                        DEBUG(5,("get_lanman2_dir_entry: Masquerading msdfs link %s "
                                                "as a directory\n",
                                                pathreal));
@@ -1291,6 +1372,7 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
 
                                        DEBUG(5,("get_lanman2_dir_entry:Couldn't stat [%s] (%s)\n",
                                                pathreal,strerror(errno)));
+                                       TALLOC_FREE(pathreal);
                                        continue;
                                }
                        }
@@ -1303,6 +1385,7 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
 
                        if (!dir_check_ftype(conn,mode,dirtype)) {
                                DEBUG(5,("get_lanman2_dir_entry: [%s] attribs didn't match %x\n",fname,dirtype));
+                               TALLOC_FREE(pathreal);
                                continue;
                        }
 
@@ -1315,6 +1398,17 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                        adate_ts = get_atimespec(&sbuf);
                        create_date_ts = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
 
+                       if (ask_sharemode) {
+                               struct timespec write_time_ts;
+                               struct file_id fileid;
+
+                               fileid = vfs_file_id_from_sbuf(conn, &sbuf);
+                               get_file_infos(fileid, NULL, &write_time_ts);
+                               if (!null_timespec(write_time_ts)) {
+                                       mdate_ts = write_time_ts;
+                               }
+                       }
+
                        if (lp_dos_filetime_resolution(SNUM(conn))) {
                                dos_filetime_timespec(&create_date_ts);
                                dos_filetime_timespec(&mdate_ts);
@@ -1324,9 +1418,9 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                        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);
-                       
+
                        DEBUG(5,("get_lanman2_dir_entry: found %s fname=%s\n",pathreal,fname));
-         
+
                        found = True;
 
                        dptr_DirCacheAdd(conn->dirptr, dname, curr_dirpos);
@@ -1353,7 +1447,9 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                        SSVAL(p,20,mode);
                        p += 23;
                        nameptr = p;
-                       p += align_string(pdata, p, 0);
+                       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);
@@ -1433,7 +1529,7 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                        SSVAL(p,20,mode);
                        p += 22; /* p now points to the EA area. */
 
-                       file_list = get_ea_list_from_file(ea_ctx, conn, NULL, pathreal, &ea_len);
+                       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. */
@@ -1447,10 +1543,10 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                        }
 
                        /* Push the ea_data followed by the name. */
-                       p += fill_ea_buffer(ea_ctx, p, space_remaining, conn, name_list);
+                       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),
+                                         p + 1, fname, PTR_DIFF(end_data, p+1),
                                          STR_TERMINATE | STR_NOALIGN);
                        if (flags2 & FLAGS2_UNICODE_STRINGS) {
                                if (len > 2) {
@@ -1494,10 +1590,11 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                         * a Win2k client bug. JRA.
                         */
                        if (!was_8_3 && check_mangled_names) {
-                               pstring mangled_name;
-                               pstrcpy(mangled_name, fname);
-                               mangle_map(mangled_name,True,True,
-                                          conn->params);
+                               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,
@@ -1534,7 +1631,7 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                        SOFF_T(p,0,allocation_size); p += 8;
                        SIVAL(p,0,nt_extmode); p += 4;
                        len = srvstr_push(base_data, flags2,
-                                         p + 4, fname, PTR_DIFF(end_data, p),
+                                         p + 4, fname, PTR_DIFF(end_data, p+4),
                                          STR_TERMINATE_ASCII);
                        SIVAL(p,0,len);
                        p += 4 + len;
@@ -1544,7 +1641,7 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                        SIVAL(pdata,0,len);
                        p = pdata + len;
                        break;
-      
+
                case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
                        DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
                        p += 4;
@@ -1649,10 +1746,11 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
                         * a Win2k client bug. JRA.
                         */
                        if (!was_8_3 && check_mangled_names) {
-                               pstring mangled_name;
-                               pstrcpy(mangled_name, fname);
-                               mangle_map(mangled_name,True,True,
-                                          conn->params);
+                               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,
@@ -1719,7 +1817,7 @@ static BOOL get_lanman2_dir_entry(connection_struct *conn, uint16 flags2,
 
                        break;
 
-               default:      
+               default:
                        return(False);
        }
 
@@ -1761,26 +1859,27 @@ static void call_trans2findfirst(connection_struct *conn,
        uint32 dirtype;
        int maxentries;
        uint16 findfirst_flags;
-       BOOL close_after_first;
-       BOOL close_if_end;
-       BOOL requires_resume_key;
+       bool close_after_first;
+       bool close_if_end;
+       bool requires_resume_key;
        int info_level;
-       pstring directory;
-       pstring mask;
+       char *directory = NULL;
+       const char *mask = NULL;
        char *p;
        int last_entry_off=0;
        int dptr_num = -1;
        int numentries = 0;
        int i;
-       BOOL finished = False;
-       BOOL dont_descend = False;
-       BOOL out_of_space = False;
+       bool finished = False;
+       bool dont_descend = False;
+       bool out_of_space = False;
        int space_remaining;
-       BOOL mask_contains_wcard = False;
+       bool mask_contains_wcard = False;
        SMB_STRUCT_STAT sbuf;
-       TALLOC_CTX *ea_ctx = NULL;
        struct ea_list *ea_list = NULL;
        NTSTATUS ntstatus = NT_STATUS_OK;
+       bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
+       TALLOC_CTX *ctx = talloc_tos();
 
        if (total_params < 13) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -1795,8 +1894,6 @@ static void call_trans2findfirst(connection_struct *conn,
        requires_resume_key = (findfirst_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
        info_level = SVAL(params,6);
 
-       *directory = *mask = 0;
-
        DEBUG(3,("call_trans2findfirst: dirtype = %x, maxentries = %d, close_after_first=%d, \
 close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                (unsigned int)dirtype, maxentries, close_after_first, close_if_end, requires_resume_key,
@@ -1830,15 +1927,19 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                        return;
        }
 
-       srvstr_get_path_wcard(params, req->flags2, directory,
-                             params+12, sizeof(directory), total_params - 12,
+       srvstr_get_path_wcard(ctx, params, req->flags2, &directory,
+                             params+12, total_params - 12,
                              STR_TERMINATE, &ntstatus, &mask_contains_wcard);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                reply_nterror(req, ntstatus);
                return;
        }
 
-       ntstatus = resolve_dfspath_wcard(conn, req->flags2 & FLAGS2_DFS_PATHNAMES, directory, &mask_contains_wcard);
+       ntstatus = resolve_dfspath_wcard(ctx, conn,
+                       req->flags2 & FLAGS2_DFS_PATHNAMES,
+                       directory,
+                       &directory,
+                       &mask_contains_wcard);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                if (NT_STATUS_EQUAL(ntstatus,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -1849,11 +1950,12 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                return;
        }
 
-       ntstatus = unix_convert(conn, directory, True, NULL, &sbuf);
+       ntstatus = unix_convert(ctx, conn, directory, True, &directory, NULL, &sbuf);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                reply_nterror(req, ntstatus);
                return;
        }
+
        ntstatus = check_name(conn, directory);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                reply_nterror(req, ntstatus);
@@ -1864,14 +1966,18 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
        if(p == NULL) {
                /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
                if((directory[0] == '.') && (directory[1] == '\0')) {
-                       pstrcpy(mask,"*");
+                       mask = "*";
                        mask_contains_wcard = True;
                } else {
-                       pstrcpy(mask,directory);
+                       mask = directory;
+               }
+               directory = talloc_strdup(talloc_tos(), "./");
+               if (!directory) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       return;
                }
-               pstrcpy(directory,"./");
        } else {
-               pstrcpy(mask,p+1);
+               mask = p+1;
                *p = 0;
        }
 
@@ -1897,16 +2003,10 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        reply_doserror(req, ERRDOS, ERReasnotsupported);
                        return;
                }
-                                                                                                                                                        
-               if ((ea_ctx = talloc_init("findnext_ea_list")) == NULL) {
-                       reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
 
                /* Pull out the list of names. */
-               ea_list = read_ea_name_list(ea_ctx, pdata + 4, ea_size - 4);
+               ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
                if (!ea_list) {
-                       talloc_destroy(ea_ctx);
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        return;
                }
@@ -1915,7 +2015,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        *ppdata = (char *)SMB_REALLOC(
                *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
        if(*ppdata == NULL ) {
-               talloc_destroy(ea_ctx);
                reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
@@ -1925,7 +2024,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        /* Realloc the params space */
        *pparams = (char *)SMB_REALLOC(*pparams, 10);
        if (*pparams == NULL) {
-               talloc_destroy(ea_ctx);
                reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
@@ -1945,7 +2043,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                &conn->dirptr);
 
        if (!NT_STATUS_IS_OK(ntstatus)) {
-               talloc_destroy(ea_ctx);
                reply_nterror(req, ntstatus);
                return;
        }
@@ -1953,19 +2050,19 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        dptr_num = dptr_dnum(conn->dirptr);
        DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
 
-       /* We don't need to check for VOL here as this is returned by 
+       /* 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))
                dont_descend = True;
-    
+
        p = pdata;
        space_remaining = max_data_bytes;
        out_of_space = False;
 
        for (i=0;(i<maxentries) && !finished && !out_of_space;i++) {
-               BOOL got_exact_match = False;
+               bool got_exact_match = False;
 
                /* this is a heuristic to avoid seeking the dirptr except when 
                        absolutely necessary. It allows for a filename of about 40 chars */
@@ -1973,14 +2070,16 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        out_of_space = True;
                        finished = False;
                } else {
-                       finished = !get_lanman2_dir_entry(conn,
+                       finished = !get_lanman2_dir_entry(ctx,
+                                       conn,
                                        req->flags2,
                                        mask,dirtype,info_level,
                                        requires_resume_key,dont_descend,
+                                       ask_sharemode,
                                        &p,pdata,data_end,
                                        space_remaining, &out_of_space,
                                        &got_exact_match,
-                                       &last_entry_off, ea_list, ea_ctx);
+                                       &last_entry_off, ea_list);
                }
 
                if (finished && out_of_space)
@@ -1999,10 +2098,14 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                if(got_exact_match)
                        finished = True;
 
-               space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
+               /* Ensure space_remaining never goes -ve. */
+               if (PTR_DIFF(p,pdata) > max_data_bytes) {
+                       space_remaining = 0;
+                       out_of_space = true;
+               } else {
+                       space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
+               }
        }
-  
-       talloc_destroy(ea_ctx);
 
        /* Check if we can close the dirptr */
        if(close_after_first || (finished && close_if_end)) {
@@ -2010,8 +2113,8 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                dptr_close(&dptr_num);
        }
 
-       /* 
-        * If there are no matching entries we must return ERRDOS/ERRbadfile - 
+       /*
+        * If there are no matching entries we must return ERRDOS/ERRbadfile -
         * from observation of NT. NB. This changes to ERRDOS,ERRnofiles if
         * the protocol level is less than NT1. Tested with smbclient. JRA.
         * This should fix the OS/2 client bug #2335.
@@ -2038,17 +2141,21 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        SSVAL(params,6,0); /* Never an EA error */
        SSVAL(params,8,last_entry_off);
 
-       send_trans2_replies(req, params, 10, pdata, PTR_DIFF(p,pdata),
+       send_trans2_replies(conn, req, params, 10, pdata, PTR_DIFF(p,pdata),
                            max_data_bytes);
 
-       if ((! *directory) && dptr_path(dptr_num))
-               slprintf(directory,sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
+       if ((! *directory) && dptr_path(dptr_num)) {
+               directory = talloc_strdup(talloc_tos(),dptr_path(dptr_num));
+               if (!directory) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+               }
+       }
 
        DEBUG( 4, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
                smb_fn_name(CVAL(req->inbuf,smb_com)),
                mask, directory, dirtype, numentries ) );
 
-       /* 
+       /*
         * Force a name mangle here to ensure that the
         * mask as an 8.3 name is top of the mangled cache.
         * The reasons for this are subtle. Don't remove
@@ -2056,8 +2163,10 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
         * (see PR#13758). JRA.
         */
 
-       if(!mangle_is_8_3_wildcards( mask, False, conn->params))
-               mangle_map(mask, True, True, conn->params);
+       if(!mangle_is_8_3_wildcards( mask, False, conn->params)) {
+               char mangled_name[13];
+               name_to_8_3(mask, mangled_name, True, conn->params);
+       }
 
        return;
 }
@@ -2085,25 +2194,26 @@ static void call_trans2findnext(connection_struct *conn,
        uint16 info_level;
        uint32 resume_key;
        uint16 findnext_flags;
-       BOOL close_after_request;
-       BOOL close_if_end;
-       BOOL requires_resume_key;
-       BOOL continue_bit;
-       BOOL mask_contains_wcard = False;
-       pstring resume_name;
-       pstring mask;
-       pstring directory;
-       char *p;
+       bool close_after_request;
+       bool close_if_end;
+       bool requires_resume_key;
+       bool continue_bit;
+       bool mask_contains_wcard = False;
+       char *resume_name = NULL;
+       const char *mask = NULL;
+       const char *directory = NULL;
+       char *p = NULL;
        uint16 dirtype;
        int numentries = 0;
        int i, last_entry_off=0;
-       BOOL finished = False;
-       BOOL dont_descend = False;
-       BOOL out_of_space = False;
+       bool finished = False;
+       bool dont_descend = False;
+       bool out_of_space = False;
        int space_remaining;
-       TALLOC_CTX *ea_ctx = NULL;
        struct ea_list *ea_list = NULL;
        NTSTATUS ntstatus = NT_STATUS_OK;
+       bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
+       TALLOC_CTX *ctx = talloc_tos();
 
        if (total_params < 13) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2120,10 +2230,8 @@ 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);
 
-       *mask = *directory = *resume_name = 0;
-
-       srvstr_get_path_wcard(params, req->flags2, resume_name,
-                             params+12, sizeof(resume_name),
+       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)) {
@@ -2131,12 +2239,12 @@ static void call_trans2findnext(connection_struct *conn,
                   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(params, req->flags2,
-                               resume_name, params+12,
-                               sizeof(resume_name), total_params - 12,
+               srvstr_pull_talloc(ctx, params, req->flags2,
+                               &resume_name, params+12,
+                               total_params - 12,
                                STR_TERMINATE);
 
-               if (!(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
+               if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
                        reply_nterror(req, ntstatus);
                        return;
                }
@@ -2191,21 +2299,15 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        return;
                }
-                                                                                                                                                     
+
                if (!lp_ea_support(SNUM(conn))) {
                        reply_doserror(req, ERRDOS, ERReasnotsupported);
                        return;
                }
-                                                                                                                                                     
-               if ((ea_ctx = talloc_init("findnext_ea_list")) == NULL) {
-                       reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       return;
-               }
 
                /* Pull out the list of names. */
-               ea_list = read_ea_name_list(ea_ctx, pdata + 4, ea_size - 4);
+               ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
                if (!ea_list) {
-                       talloc_destroy(ea_ctx);
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        return;
                }
@@ -2214,7 +2316,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        *ppdata = (char *)SMB_REALLOC(
                *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
        if(*ppdata == NULL) {
-               talloc_destroy(ea_ctx);
                reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
@@ -2225,7 +2326,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        /* Realloc the params space */
        *pparams = (char *)SMB_REALLOC(*pparams, 6*SIZEOFWORD);
        if(*pparams == NULL ) {
-               talloc_destroy(ea_ctx);
                reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
@@ -2234,7 +2334,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
        /* Check that the dptr is valid */
        if(!(conn->dirptr = dptr_fetch_lanman2(dptr_num))) {
-               talloc_destroy(ea_ctx);
                reply_doserror(req, ERRDOS, ERRnofiles);
                return;
        }
@@ -2244,34 +2343,33 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        /* Get the wildcard mask from the dptr */
        if((p = dptr_wcard(dptr_num))== NULL) {
                DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
-               talloc_destroy(ea_ctx);
                reply_doserror(req, ERRDOS, ERRnofiles);
                return;
        }
 
-       pstrcpy(mask, p);
-       pstrcpy(directory,conn->dirpath);
+       mask = p;
+       directory = conn->dirpath;
 
        /* Get the attr mask from the dptr */
        dirtype = dptr_attr(dptr_num);
 
        DEBUG(3,("dptr_num is %d, mask = %s, attr = %x, dirptr=(0x%lX,%ld)\n",
-               dptr_num, mask, dirtype, 
+               dptr_num, mask, dirtype,
                (long)conn->dirptr,
                dptr_TellDir(conn->dirptr)));
 
-       /* We don't need to check for VOL here as this is returned by 
+       /* 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))
                dont_descend = True;
-    
+
        p = pdata;
        space_remaining = max_data_bytes;
        out_of_space = False;
 
-       /* 
+       /*
         * Seek to the correct position. We no longer use the resume key but
         * depend on the last file name instead.
         */
@@ -2281,14 +2379,20 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                long current_pos = 0;
                /*
-                * Remember, mangle_map is called by
+                * Remember, name_to_8_3 is called by
                 * get_lanman2_dir_entry(), so the resume name
                 * could be mangled. Ensure we check the unmangled name.
                 */
 
                if (mangle_is_mangled(resume_name, conn->params)) {
-                       mangle_check_cache(resume_name, sizeof(resume_name)-1,
-                                          conn->params);
+                       char *new_resume_name = NULL;
+                       mangle_lookup_name_from_8_3(ctx,
+                                               resume_name,
+                                               &new_resume_name,
+                                               conn->params);
+                       if (new_resume_name) {
+                               resume_name = new_resume_name;
+                       }
                }
 
                /*
@@ -2304,7 +2408,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        } /* end if resume_name && !continue_bit */
 
        for (i=0;(i<(int)maxentries) && !finished && !out_of_space ;i++) {
-               BOOL got_exact_match = False;
+               bool got_exact_match = False;
 
                /* this is a heuristic to avoid seeking the dirptr except when 
                        absolutely necessary. It allows for a filename of about 40 chars */
@@ -2312,14 +2416,16 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        out_of_space = True;
                        finished = False;
                } else {
-                       finished = !get_lanman2_dir_entry(conn,
+                       finished = !get_lanman2_dir_entry(ctx,
+                                               conn,
                                                req->flags2,
                                                mask,dirtype,info_level,
                                                requires_resume_key,dont_descend,
+                                               ask_sharemode,
                                                &p,pdata,data_end,
                                                space_remaining, &out_of_space,
                                                &got_exact_match,
-                                               &last_entry_off, ea_list, ea_ctx);
+                                               &last_entry_off, ea_list);
                }
 
                if (finished && out_of_space)
@@ -2340,8 +2446,10 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                space_remaining = max_data_bytes - PTR_DIFF(p,pdata);
        }
-  
-       talloc_destroy(ea_ctx);
+
+       DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
+               smb_fn_name(CVAL(req->inbuf,smb_com)),
+               mask, directory, dirtype, numentries ) );
 
        /* Check if we can close the dirptr */
        if(close_after_request || (finished && close_if_end)) {
@@ -2355,16 +2463,9 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        SSVAL(params,4,0); /* Never an EA error */
        SSVAL(params,6,last_entry_off);
 
-       send_trans2_replies(req, params, 8, pdata, PTR_DIFF(p,pdata),
+       send_trans2_replies(conn, req, params, 8, pdata, PTR_DIFF(p,pdata),
                            max_data_bytes);
 
-       if ((! *directory) && dptr_path(dptr_num))
-               slprintf(directory,sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
-
-       DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
-               smb_fn_name(CVAL(req->inbuf,smb_com)),
-               mask, directory, dirtype, numentries ) );
-
        return;
 }
 
@@ -2374,6 +2475,41 @@ unsigned char *create_volume_objectid(connection_struct *conn, unsigned char obj
        return objid;
 }
 
+static void samba_extended_info_version(struct smb_extended_info *extended_info)
+{
+       SMB_ASSERT(extended_info != NULL);
+
+       extended_info->samba_magic = SAMBA_EXTENDED_INFO_MAGIC;
+       extended_info->samba_version = ((SAMBA_VERSION_MAJOR & 0xff) << 24)
+                                      | ((SAMBA_VERSION_MINOR & 0xff) << 16)
+                                      | ((SAMBA_VERSION_RELEASE & 0xff) << 8);
+#ifdef SAMBA_VERSION_REVISION
+       extended_info->samba_version |= (tolower(*SAMBA_VERSION_REVISION) - 'a' + 1) & 0xff;
+#endif
+       extended_info->samba_subversion = 0;
+#ifdef SAMBA_VERSION_RC_RELEASE
+       extended_info->samba_subversion |= (SAMBA_VERSION_RC_RELEASE & 0xff) << 24;
+#else
+#ifdef SAMBA_VERSION_PRE_RELEASE
+       extended_info->samba_subversion |= (SAMBA_VERSION_PRE_RELEASE & 0xff) << 16;
+#endif
+#endif
+#ifdef SAMBA_VERSION_VENDOR_PATCH
+       extended_info->samba_subversion |= (SAMBA_VERSION_VENDOR_PATCH & 0xffff);
+#endif
+       extended_info->samba_gitcommitdate = 0;
+#ifdef SAMBA_VERSION_GIT_COMMIT_TIME
+       unix_to_nt_time(&extended_info->samba_gitcommitdate, SAMBA_VERSION_GIT_COMMIT_TIME);
+#endif
+
+       memset(extended_info->samba_version_string, 0,
+              sizeof(extended_info->samba_version_string));
+
+       snprintf (extended_info->samba_version_string,
+                 sizeof(extended_info->samba_version_string),
+                 "%s", samba_version_string());
+}
+
 /****************************************************************************
  Reply to a TRANS2_QFSINFO (query filesystem info).
 ****************************************************************************/
@@ -2392,8 +2528,8 @@ static void call_trans2qfsinfo(connection_struct *conn,
        const char *vname = volume_label(SNUM(conn));
        int snum = SNUM(conn);
        char *fstype = lp_fstype(SNUM(conn));
-       int quota_flag = 0;
-
+       uint32 additional_flags = 0;
+       
        if (total_params < 2) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
@@ -2401,6 +2537,27 @@ static void call_trans2qfsinfo(connection_struct *conn,
 
        info_level = SVAL(params,0);
 
+       if (IS_IPC(conn)) {
+               if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
+                       DEBUG(0,("call_trans2qfsinfo: not an allowed "
+                               "info level (0x%x) on IPC$.\n",
+                               (unsigned int)info_level));
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+                       return;
+               }
+       }
+
+       if (ENCRYPTION_REQUIRED(conn) && !req->encrypted) {
+               if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
+                       DEBUG(0,("call_trans2qfsinfo: encryption required "
+                               "and info level 0x%x sent.\n",
+                               (unsigned int)info_level));
+                       exit_server_cleanly("encryption required "
+                               "on connection");
+                       return;
+               }
+       }
+
        DEBUG(3,("call_trans2qfsinfo: level = %d\n", info_level));
 
        if(SMB_VFS_STAT(conn,".",&st)!=0) {
@@ -2485,16 +2642,21 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_dev, (unsi
                case SMB_QUERY_FS_ATTRIBUTE_INFO:
                case SMB_FS_ATTRIBUTE_INFORMATION:
 
-
+                       additional_flags = 0;
 #if defined(HAVE_SYS_QUOTAS)
-                       quota_flag = FILE_VOLUME_QUOTAS;
+                       additional_flags |= FILE_VOLUME_QUOTAS;
 #endif
 
+                       if(lp_nt_acl_support(SNUM(conn))) {
+                               additional_flags |= FILE_PERSISTENT_ACLS;
+                       }
+
+                       /* Capabilities are filled in at connection time through STATVFS call */
+                       additional_flags |= conn->fs_capabilities;
+
                        SIVAL(pdata,0,FILE_CASE_PRESERVED_NAMES|FILE_CASE_SENSITIVE_SEARCH|
-                               (lp_nt_acl_support(SNUM(conn)) ? FILE_PERSISTENT_ACLS : 0)|
-                               FILE_SUPPORTS_OBJECT_IDS|
-                               FILE_UNICODE_ON_DISK|
-                               quota_flag); /* FS ATTRIBUTES */
+                               FILE_SUPPORTS_OBJECT_IDS|FILE_UNICODE_ON_DISK|
+                               additional_flags); /* FS ATTRIBUTES */
 
                        SIVAL(pdata,4,255); /* Max filename component length */
                        /* NOTE! the fstype must *not* be null terminated or win98 won't recognise it
@@ -2686,7 +2848,14 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                case SMB_FS_OBJECTID_INFORMATION:
                {
                        unsigned char objid[16];
+                       struct smb_extended_info extended_info;
                        memcpy(pdata,create_volume_objectid(conn, objid),16);
+                       samba_extended_info_version (&extended_info);
+                       SIVAL(pdata,16,extended_info.samba_magic);
+                       SIVAL(pdata,20,extended_info.samba_version);
+                       SIVAL(pdata,24,extended_info.samba_subversion);
+                       SBIG_UINT(pdata,28,extended_info.samba_gitcommitdate);
+                       memcpy(pdata+36,extended_info.samba_version_string,28);
                        data_len = 64;
                        break;
                }
@@ -2697,24 +2866,52 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                 */
 
                case SMB_QUERY_CIFS_UNIX_INFO:
+               {
+                       bool large_write = lp_min_receive_file_size() &&
+                                               !srv_is_signing_active();
+                       bool large_read = !srv_is_signing_active();
+                       int encrypt_caps = 0;
+
                        if (!lp_unix_extensions()) {
                                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
                        }
+
+                       switch (conn->encrypt_level) {
+                       case 0:
+                               encrypt_caps = 0;
+                               break;
+                       case 1:
+                       case Auto:
+                               encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP;
+                               break;
+                       case Required:
+                               encrypt_caps = CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP|
+                                               CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP;
+                               large_write = false;
+                               large_read = false;
+                               break;
+                       }
+
                        data_len = 12;
                        SSVAL(pdata,0,CIFS_UNIX_MAJOR_VERSION);
                        SSVAL(pdata,2,CIFS_UNIX_MINOR_VERSION);
-                       /* We have POSIX ACLs, pathname and locking capability. */
+
+                       /* We have POSIX ACLs, pathname, encryption, 
+                        * large read/write, and locking capability. */
+
                        SBIG_UINT(pdata,4,((SMB_BIG_UINT)(
                                        CIFS_UNIX_POSIX_ACLS_CAP|
                                        CIFS_UNIX_POSIX_PATHNAMES_CAP|
                                        CIFS_UNIX_FCNTL_LOCKS_CAP|
                                        CIFS_UNIX_EXTATTR_CAP|
                                        CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP|
-                                       /* Ensure we don't do this on signed or sealed data. */
-                                       (srv_is_signing_active() ? 0 : CIFS_UNIX_LARGE_READ_CAP)
-                                       )));
+                                       encrypt_caps|
+                                       (large_read ? CIFS_UNIX_LARGE_READ_CAP : 0) |
+                                       (large_write ?
+                                       CIFS_UNIX_LARGE_WRITE_CAP : 0))));
                        break;
+               }
 
                case SMB_QUERY_POSIX_FS_INFO:
                {
@@ -2830,8 +3027,8 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                         */
                        for (i = 0, sid_bytes = 0;
                            i < current_user.nt_user_token->num_sids; ++i) {
-                               sid_bytes +=
-                                   sid_size(&current_user.nt_user_token->user_sids[i]);
+                               sid_bytes += ndr_size_dom_sid(
+                                       &current_user.nt_user_token->user_sids[i], 0);
                        }
 
                        /* SID list byte count */
@@ -2851,8 +3048,8 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        /* SID list */
                        for (i = 0;
                            i < current_user.nt_user_token->num_sids; ++i) {
-                               int sid_len =
-                                   sid_size(&current_user.nt_user_token->user_sids[i]);
+                               int sid_len = ndr_size_dom_sid(
+                                       &current_user.nt_user_token->user_sids[i], 0);
 
                                sid_linearize(pdata + data_len, sid_len,
                                    &current_user.nt_user_token->user_sids[i]);
@@ -2879,7 +3076,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
        }
 
 
-       send_trans2_replies(req, params, 0, pdata, data_len,
+       send_trans2_replies(conn, req, params, 0, pdata, data_len,
                            max_data_bytes);
 
        DEBUG( 4, ( "%s info_level = %d\n",
@@ -2914,6 +3111,28 @@ static void call_trans2setfsinfo(connection_struct *conn,
 
        info_level = SVAL(params,2);
 
+       if (IS_IPC(conn)) {
+               if (info_level != SMB_REQUEST_TRANSPORT_ENCRYPTION &&
+                               info_level != SMB_SET_CIFS_UNIX_INFO) {
+                       DEBUG(0,("call_trans2setfsinfo: not an allowed "
+                               "info level (0x%x) on IPC$.\n",
+                               (unsigned int)info_level));
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+                       return;
+               }
+       }
+
+       if (ENCRYPTION_REQUIRED(conn) && !req->encrypted) {
+               if (info_level != SMB_REQUEST_TRANSPORT_ENCRYPTION) {
+                       DEBUG(0,("call_trans2setfsinfo: encryption required "
+                               "and info level 0x%x sent.\n",
+                               (unsigned int)info_level));
+                       exit_server_cleanly("encryption required "
+                               "on connection");
+                       return;
+               }
+       }
+
        switch(info_level) {
                case SMB_SET_CIFS_UNIX_INFO:
                        {
@@ -2964,6 +3183,7 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                }
                                break;
                        }
+
                case SMB_REQUEST_TRANSPORT_ENCRYPTION:
                        {
                                NTSTATUS status;
@@ -2972,43 +3192,53 @@ cap_low = 0x%x, cap_high = 0x%x\n",
 
                                if (!lp_unix_extensions()) {
                                        reply_nterror(
-                                               req, NT_STATUS_INVALID_LEVEL);
+                                               req,
+                                               NT_STATUS_INVALID_LEVEL);
+                                       return;
+                               }
+
+                               if (lp_smb_encrypt(SNUM(conn)) == false) {
+                                       reply_nterror(
+                                               req,
+                                               NT_STATUS_NOT_SUPPORTED);
                                        return;
                                }
 
-                               DEBUG( 4,("call_trans2setfsinfo: request transport encrption.\n"));
+                               DEBUG( 4,("call_trans2setfsinfo: "
+                                       "request transport encryption.\n"));
 
                                status = srv_request_encryption_setup(conn,
-                                                                       (unsigned char **)ppdata,
-                                                                       &data_len,
-                                                                       (unsigned char **)pparams,
-                                                                       &param_len
-                                                                       );
-
-                               if (!NT_STATUS_IS_OK(status)) {
-                                       /*
-                                        * TODO: Check
-                                        * MORE_PROCESSING_REQUIRED, this used
-                                        * to have special handling here.
-                                        */
+                                                               (unsigned char **)ppdata,
+                                                               &data_len,
+                                                               (unsigned char **)pparams,
+                                                               &param_len);
+
+                               if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
+                                               !NT_STATUS_IS_OK(status)) {
                                        reply_nterror(req, status);
                                        return;
                                }
 
-                               send_trans2_replies(req,
-                                                   *pparams, param_len,
-                                                   *ppdata, data_len,
-                                                   max_data_bytes);
+                               send_trans2_replies(conn, req,
+                                               *pparams,
+                                               param_len,
+                                               *ppdata,
+                                               data_len,
+                                               max_data_bytes);
 
                                if (NT_STATUS_IS_OK(status)) {
-                                       /* Server-side transport encryption is now *on*. */
+                                       /* Server-side transport
+                                        * encryption is now *on*. */
                                        status = srv_encryption_start(conn);
                                        if (!NT_STATUS_IS_OK(status)) {
-                                               exit_server_cleanly("Failure in setting up encrypted transport");
+                                               exit_server_cleanly(
+                                                       "Failure in setting "
+                                                       "up encrypted transport");
                                        }
                                }
                                return;
                        }
+
                case SMB_FS_QUOTA_INFORMATION:
                        {
                                files_struct *fsp = NULL;
@@ -3135,7 +3365,7 @@ static unsigned int count_acl_entries(connection_struct *conn, SMB_ACL_T posix_a
  Utility function to marshall a POSIX acl into wire format.
 ****************************************************************************/
 
-static BOOL marshall_posix_acl(connection_struct *conn, char *pdata, SMB_STRUCT_STAT *pst, SMB_ACL_T posix_acl)
+static bool marshall_posix_acl(connection_struct *conn, char *pdata, SMB_STRUCT_STAT *pst, SMB_ACL_T posix_acl)
 {
        int entry_id = SMB_ACL_FIRST_ENTRY;
        SMB_ACL_ENTRY_T entry;
@@ -3290,7 +3520,7 @@ static char *store_file_unix_basic(connection_struct *conn,
  *
  * XXX: this really should be behind the VFS interface. To do this, we would
  * need to alter SMB_STRUCT_STAT so that it included a flags and a mask field.
- * Each VFS module could then implement it's own mapping as appropriate for the
+ * Each VFS module could then implement its own mapping as appropriate for the
  * platform. We would then pass the SMB flags into SMB_VFS_CHFLAGS.
  */
 static const struct {unsigned stat_fflag; unsigned smb_fflag;}
@@ -3334,7 +3564,7 @@ static void map_info2_flags_from_sbuf(const SMB_STRUCT_STAT *psbuf,
 #endif /* HAVE_STAT_ST_FLAGS */
 }
 
-static BOOL map_info2_flags_to_sbuf(const SMB_STRUCT_STAT *psbuf,
+static bool map_info2_flags_to_sbuf(const SMB_STRUCT_STAT *psbuf,
                                const uint32 smb_fflags,
                                const uint32 smb_fmask,
                                int *stat_fflags)
@@ -3400,6 +3630,72 @@ static char *store_file_unix_basic_info2(connection_struct *conn,
        return pdata;
 }
 
+static NTSTATUS marshall_stream_info(unsigned int num_streams,
+                                    const struct stream_struct *streams,
+                                    char *data,
+                                    unsigned int max_data_bytes,
+                                    unsigned int *data_size)
+{
+       unsigned int i;
+       unsigned int ofs = 0;
+
+       for (i=0; i<num_streams; i++) {
+               unsigned int next_offset;
+               size_t namelen;
+               smb_ucs2_t *namebuf;
+
+               namelen = push_ucs2_talloc(talloc_tos(), &namebuf,
+                                           streams[i].name);
+
+               if ((namelen == (size_t)-1) || (namelen <= 2)) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               /*
+                * name_buf is now null-terminated, we need to marshall as not
+                * terminated
+                */
+
+               namelen -= 2;
+
+               if (ofs + 24 + namelen > max_data_bytes) {
+                       TALLOC_FREE(namebuf);
+                       return NT_STATUS_BUFFER_TOO_SMALL;
+               }
+
+               SIVAL(data, ofs+4, namelen);
+               SOFF_T(data, ofs+8, streams[i].size);
+               SOFF_T(data, ofs+16, streams[i].alloc_size);
+               memcpy(data+ofs+24, namebuf, namelen);
+               TALLOC_FREE(namebuf);
+
+               next_offset = ofs + 24 + namelen;
+
+               if (i == num_streams-1) {
+                       SIVAL(data, ofs, 0);
+               }
+               else {
+                       unsigned int align = ndr_align_size(next_offset, 8);
+
+                       if (next_offset + align > max_data_bytes) {
+                               return NT_STATUS_BUFFER_TOO_SMALL;
+                       }
+
+                       memset(data+next_offset, 0, align);
+                       next_offset += align;
+
+                       SIVAL(data, ofs, next_offset - ofs);
+                       ofs = next_offset;
+               }
+
+               ofs = next_offset;
+       }
+
+       *data_size = ofs;
+
+       return NT_STATUS_OK;
+}
+
 /****************************************************************************
  Reply to a TRANSACT2_QFILEINFO on a PIPE !
 ****************************************************************************/
@@ -3453,7 +3749,7 @@ static void call_trans2qpipeinfo(connection_struct *conn,
 
        switch (info_level) {
                case SMB_FILE_STANDARD_INFORMATION:
-                       memset(pdata,24,0);
+                       memset(pdata,0,24);
                        SOFF_T(pdata,0,4096LL);
                        SIVAL(pdata,16,1);
                        SIVAL(pdata,20,1);
@@ -3465,7 +3761,7 @@ static void call_trans2qpipeinfo(connection_struct *conn,
                        return;
        }
 
-       send_trans2_replies(req, params, param_size, *ppdata, data_size,
+       send_trans2_replies(conn, req, params, param_size, *ppdata, data_size,
                            max_data_bytes);
 
        return;
@@ -3494,21 +3790,23 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        unsigned int data_size = 0;
        unsigned int param_size = 2;
        SMB_STRUCT_STAT sbuf;
-       pstring fname, dos_fname;
+       char *dos_fname = NULL;
+       char *fname = NULL;
        char *fullpathname;
        char *base_name;
        char *p;
        SMB_OFF_T pos = 0;
-       BOOL delete_pending = False;
+       bool delete_pending = False;
        int len;
        time_t create_time, mtime, atime;
        struct timespec create_time_ts, mtime_ts, atime_ts;
+       struct timespec write_time_ts;
        files_struct *fsp = NULL;
        struct file_id fileid;
-       TALLOC_CTX *data_ctx = NULL;
        struct ea_list *ea_list = NULL;
        uint32 access_mask = 0x12019F; /* Default - GENERIC_EXECUTE mapping from Windows */
        char *lock_data = NULL;
+       TALLOC_CTX *ctx = talloc_tos();
 
        if (!params) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -3516,6 +3814,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        }
 
        ZERO_STRUCT(sbuf);
+       ZERO_STRUCT(write_time_ts);
 
        if (tran_call == TRANSACT2_QFILEINFO) {
                if (total_params < 4) {
@@ -3541,23 +3840,31 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               if(fsp && (fsp->fake_file_handle)) {
+               /* Initial check for valid fsp ptr. */
+               if (!check_fsp_open(conn, req, fsp, &current_user)) {
+                       return;
+               }
+
+               fname = talloc_strdup(talloc_tos(),fsp->fsp_name);
+               if (!fname) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       return;
+               }
+
+               if(fsp->fake_file_handle) {
                        /*
                         * This is actually for the QUOTA_FAKE_FILE --metze
                         */
-                                               
-                       pstrcpy(fname, fsp->fsp_name);
+
                        /* We know this name is ok, it's already passed the checks. */
-                       
+
                } else if(fsp && (fsp->is_directory || fsp->fh->fd == -1)) {
                        /*
                         * This is actually a QFILEINFO on a directory
                         * handle (returned from an NT SMB). NT5.0 seems
                         * to do this call. JRA.
                         */
-                       /* We know this name is ok, it's already passed the checks. */
-                       pstrcpy(fname, fsp->fsp_name);
-                 
+
                        if (INFO_LEVEL_IS_UNIX(info_level)) {
                                /* Always do lstat for UNIX calls. */
                                if (SMB_VFS_LSTAT(conn,fname,&sbuf)) {
@@ -3572,7 +3879,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        }
 
                        fileid = vfs_file_id_from_sbuf(conn, &sbuf);
-                       delete_pending = get_delete_on_close_flag(fileid);
+                       get_file_infos(fileid, &delete_pending, &write_time_ts);
                } else {
                        /*
                         * Original code - this is an open file.
@@ -3581,17 +3888,17 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                return;
                        }
 
-                       pstrcpy(fname, fsp->fsp_name);
-                       if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&sbuf) != 0) {
+                       if (SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
                                DEBUG(3,("fstat of fnum %d failed (%s)\n", fsp->fnum, strerror(errno)));
                                reply_unixerror(req, ERRDOS, ERRbadfid);
                                return;
                        }
                        pos = fsp->fh->position_information;
                        fileid = vfs_file_id_from_sbuf(conn, &sbuf);
-                       delete_pending = get_delete_on_close_flag(fileid);
+                       get_file_infos(fileid, &delete_pending, &write_time_ts);
                        access_mask = fsp->access_mask;
                }
+
        } else {
                NTSTATUS status = NT_STATUS_OK;
 
@@ -3610,17 +3917,19 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               srvstr_get_path(params, req->flags2, fname, &params[6],
-                               sizeof(fname), total_params - 6,
+               srvstr_get_path(ctx, params, req->flags2, &fname, &params[6],
+                               total_params - 6,
                                STR_TERMINATE, &status);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        return;
                }
 
-               status = resolve_dfspath(conn,
-                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                        fname);
+               status = resolve_dfspath(ctx,
+                                       conn,
+                                       req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                       fname,
+                                       &fname);
                if (!NT_STATUS_IS_OK(status)) {
                        if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                                reply_botherror(req,
@@ -3631,7 +3940,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               status = unix_convert(conn, fname, False, NULL, &sbuf);
+               status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        return;
@@ -3657,24 +3966,13 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                }
 
                fileid = vfs_file_id_from_sbuf(conn, &sbuf);
-               delete_pending = get_delete_on_close_flag(fileid);
+               get_file_infos(fileid, &delete_pending, &write_time_ts);
                if (delete_pending) {
                        reply_nterror(req, NT_STATUS_DELETE_PENDING);
                        return;
                }
        }
 
-       nlink = sbuf.st_nlink;
-
-       if ((nlink > 0) && S_ISDIR(sbuf.st_mode)) {
-               /* NTFS does not seem to count ".." */
-               nlink -= 1;
-       }
-
-       if ((nlink > 0) && delete_pending) {
-               nlink -= 1;
-       }
-
        if (INFO_LEVEL_IS_UNIX(info_level) && !lp_unix_extensions()) {
                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                return;
@@ -3683,7 +3981,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        DEBUG(3,("call_trans2qfilepathinfo %s (fnum = %d) level=%d call=%d total_data=%d\n",
                fname,fsp ? fsp->fnum : -1, info_level,tran_call,total_data));
 
-       p = strrchr_m(fname,'/'); 
+       p = strrchr_m(fname,'/');
        if (!p)
                base_name = fname;
        else
@@ -3693,6 +3991,16 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        if (!mode)
                mode = FILE_ATTRIBUTE_NORMAL;
 
+       nlink = sbuf.st_nlink;
+
+       if (nlink && (mode&aDIR)) {
+               nlink = 1;
+       }
+
+       if ((nlink > 0) && delete_pending) {
+               nlink -= 1;
+       }
+
        fullpathname = fname;
        if (!(mode & aDIR))
                file_size = get_file_size(sbuf);
@@ -3725,15 +4033,9 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                return;
                        }
 
-                       if ((data_ctx = talloc_init("ea_list")) == NULL) {
-                               reply_nterror(req, NT_STATUS_NO_MEMORY);
-                               return;
-                       }
-
                        /* Pull out the list of names. */
-                       ea_list = read_ea_name_list(data_ctx, pdata + 4, ea_size - 4);
+                       ea_list = read_ea_name_list(ctx, pdata + 4, ea_size - 4);
                        if (!ea_list) {
-                               talloc_destroy(data_ctx);
                                reply_nterror(
                                        req, NT_STATUS_INVALID_PARAMETER);
                                return;
@@ -3754,16 +4056,10 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                return;
                        }
 
-                       if ((data_ctx = talloc_init("lock_request")) == NULL) {
-                               reply_nterror(req, NT_STATUS_NO_MEMORY);
-                               return;
-                       }
-
                        /* Copy the lock range data. */
                        lock_data = (char *)TALLOC_MEMDUP(
-                               data_ctx, pdata, total_data);
+                               ctx, pdata, total_data);
                        if (!lock_data) {
-                               talloc_destroy(data_ctx);
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
                                return;
                        }
@@ -3774,7 +4070,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
        *pparams = (char *)SMB_REALLOC(*pparams,2);
        if (*pparams == NULL) {
-               talloc_destroy(data_ctx);
                reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
@@ -3783,7 +4078,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        data_size = max_data_bytes + DIR_ENTRY_SAFETY_MARGIN;
        *ppdata = (char *)SMB_REALLOC(*ppdata, data_size); 
        if (*ppdata == NULL ) {
-               talloc_destroy(data_ctx);
                reply_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
@@ -3797,25 +4091,20 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
        allocation_size = get_allocation_size(conn,fsp,&sbuf);
 
-       if (fsp) {
-               if (!null_timespec(fsp->pending_modtime)) {
-                       /* the pending modtime overrides the current modtime */
-                       mtime_ts = fsp->pending_modtime;
-               }
-       } else {
-               files_struct *fsp1;
+       if (!fsp) {
                /* Do we have this path open ? */
+               files_struct *fsp1;
                fileid = vfs_file_id_from_sbuf(conn, &sbuf);
                fsp1 = file_find_di_first(fileid);
-               if (fsp1 && !null_timespec(fsp1->pending_modtime)) {
-                       /* the pending modtime overrides the current modtime */
-                       mtime_ts = fsp1->pending_modtime;
-               }
                if (fsp1 && fsp1->initial_allocation_size) {
                        allocation_size = get_allocation_size(conn, fsp1, &sbuf);
                }
        }
 
+       if (!null_timespec(write_time_ts)) {
+               mtime_ts = write_time_ts;
+       }
+
        if (lp_dos_filetime_resolution(SNUM(conn))) {
                dos_filetime_timespec(&create_time_ts);
                dos_filetime_timespec(&mtime_ts);
@@ -3828,10 +4117,20 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
        /* NT expects the name to be in an exact form of the *full*
           filename. See the trans2 torture test */
-       if (strequal(base_name,".")) {
-               pstrcpy(dos_fname, "\\");
+       if (ISDOT(base_name)) {
+               dos_fname = talloc_strdup(ctx, "\\");
+               if (!dos_fname) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       return;
+               }
        } else {
-               pstr_sprintf(dos_fname, "\\%s", fname);
+               dos_fname = talloc_asprintf(ctx,
+                               "\\%s",
+                               fname);
+               if (!dos_fname) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       return;
+               }
                string_replace(dos_fname, '/', '\\');
        }
 
@@ -3865,14 +4164,14 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                case SMB_INFO_IS_NAME_VALID:
                        DEBUG(10,("call_trans2qfilepathinfo: SMB_INFO_IS_NAME_VALID\n"));
                        if (tran_call == TRANSACT2_QFILEINFO) {
-                               /* os/2 needs this ? really ?*/      
+                               /* os/2 needs this ? really ?*/
                                reply_doserror(req, ERRDOS, ERRbadfunc);
                                return;
                        }
                        data_size = 0;
                        param_size = 0;
                        break;
-                       
+
                case SMB_INFO_QUERY_EAS_FROM_LIST:
                {
                        size_t total_ea_len = 0;
@@ -3880,18 +4179,16 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                        DEBUG(10,("call_trans2qfilepathinfo: SMB_INFO_QUERY_EAS_FROM_LIST\n"));
 
-                       ea_file_list = get_ea_list_from_file(data_ctx, conn, fsp, fname, &total_ea_len);
+                       ea_file_list = get_ea_list_from_file(ctx, conn, fsp, fname, &total_ea_len);
                        ea_list = ea_list_union(ea_list, ea_file_list, &total_ea_len);
 
                        if (!ea_list || (total_ea_len > data_size)) {
-                               talloc_destroy(data_ctx);
                                data_size = 4;
                                SIVAL(pdata,0,4);   /* EA List Length must be set to 4 if no EA's. */
                                break;
                        }
 
-                       data_size = fill_ea_buffer(data_ctx, pdata, data_size, conn, ea_list);
-                       talloc_destroy(data_ctx);
+                       data_size = fill_ea_buffer(ctx, pdata, data_size, conn, ea_list);
                        break;
                }
 
@@ -3902,22 +4199,14 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                        DEBUG(10,("call_trans2qfilepathinfo: SMB_INFO_QUERY_ALL_EAS\n"));
 
-                       data_ctx = talloc_init("ea_ctx");
-                       if (!data_ctx) {
-                               reply_nterror(req, NT_STATUS_NO_MEMORY);
-                               return;
-                       }
-
-                       ea_list = get_ea_list_from_file(data_ctx, conn, fsp, fname, &total_ea_len);
+                       ea_list = get_ea_list_from_file(ctx, conn, fsp, fname, &total_ea_len);
                        if (!ea_list || (total_ea_len > data_size)) {
-                               talloc_destroy(data_ctx);
                                data_size = 4;
                                SIVAL(pdata,0,4);   /* EA List Length must be set to 4 if no EA's. */
                                break;
                        }
 
-                       data_size = fill_ea_buffer(data_ctx, pdata, data_size, conn, ea_list);
-                       talloc_destroy(data_ctx);
+                       data_size = fill_ea_buffer(ctx, pdata, data_size, conn, ea_list);
                        break;
                }
 
@@ -3973,16 +4262,16 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                case SMB_QUERY_FILE_ALT_NAME_INFO:
                case SMB_FILE_ALTERNATE_NAME_INFORMATION:
                {
-                       pstring short_name;
-
+                       char mangled_name[13];
                        DEBUG(10,("call_trans2qfilepathinfo: SMB_FILE_ALTERNATE_NAME_INFORMATION\n"));
-                       pstrcpy(short_name,base_name);
-                       /* Mangle if not already 8.3 */
-                       if(!mangle_is_8_3(short_name, True, conn->params)) {
-                               mangle_map(short_name,True,True,conn->params);
+                       if (!name_to_8_3(base_name,mangled_name,
+                                               True,conn->params)) {
+                               reply_nterror(
+                                       req,
+                                       NT_STATUS_NO_MEMORY);
                        }
                        len = srvstr_push(dstart, req->flags2,
-                                         pdata+4, short_name,
+                                         pdata+4, mangled_name,
                                          PTR_DIFF(dend, pdata+4),
                                          STR_UNICODE);
                        data_size = 4 + len;
@@ -4101,28 +4390,49 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        data_size = 4;
                        break;
 
-#if 0
                /*
-                * NT4 server just returns "invalid query" to this - if we try to answer
-                * it then NTws gets a BSOD! (tridge).
-                * W2K seems to want this. JRA.
+                * NT4 server just returns "invalid query" to this - if we try
+                * to answer it then NTws gets a BSOD! (tridge).  W2K seems to
+                * want this. JRA.
+                */
+               /* The first statement above is false - verified using Thursby
+                * client against NT4 -- gcolley.
                 */
                case SMB_QUERY_FILE_STREAM_INFO:
-#endif
-               case SMB_FILE_STREAM_INFORMATION:
-                       DEBUG(10,("call_trans2qfilepathinfo: SMB_FILE_STREAM_INFORMATION\n"));
-                       if (mode & aDIR) {
-                               data_size = 0;
-                       } else {
-                               size_t byte_len = dos_PutUniCode(pdata+24,"::$DATA", (size_t)0xE, False);
-                               SIVAL(pdata,0,0); /* ??? */
-                               SIVAL(pdata,4,byte_len); /* Byte length of unicode string ::$DATA */
-                               SOFF_T(pdata,8,file_size);
-                               SOFF_T(pdata,16,allocation_size);
-                               data_size = 24 + byte_len;
+               case SMB_FILE_STREAM_INFORMATION: {
+                       unsigned int num_streams;
+                       struct stream_struct *streams;
+                       NTSTATUS status;
+
+                       DEBUG(10,("call_trans2qfilepathinfo: "
+                                 "SMB_FILE_STREAM_INFORMATION\n"));
+
+                       status = SMB_VFS_STREAMINFO(
+                               conn, fsp, fname, talloc_tos(),
+                               &num_streams, &streams);
+
+                       if (!NT_STATUS_IS_OK(status)) {
+                               DEBUG(10, ("could not get stream info: %s\n",
+                                          nt_errstr(status)));
+                               reply_nterror(req, status);
+                               return;
+                       }
+
+                       status = marshall_stream_info(num_streams, streams,
+                                                     pdata, max_data_bytes,
+                                                     &data_size);
+
+                       if (!NT_STATUS_IS_OK(status)) {
+                               DEBUG(10, ("marshall_stream_info failed: %s\n",
+                                          nt_errstr(status)));
+                               reply_nterror(req, status);
+                               return;
                        }
-                       break;
 
+                       TALLOC_FREE(streams);
+
+                       break;
+               }
                case SMB_QUERY_COMPRESSION_INFO:
                case SMB_FILE_COMPRESSION_INFORMATION:
                        DEBUG(10,("call_trans2qfilepathinfo: SMB_FILE_COMPRESSION_INFORMATION\n"));
@@ -4190,7 +4500,12 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                case SMB_QUERY_FILE_UNIX_LINK:
                        {
-                               pstring buffer;
+                               char *buffer = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
+
+                               if (!buffer) {
+                                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                                       return;
+                               }
 
                                DEBUG(10,("call_trans2qfilepathinfo: SMB_QUERY_FILE_UNIX_LINK\n"));
 #ifdef S_ISLNK
@@ -4203,7 +4518,8 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                reply_unixerror(req, ERRDOS, ERRbadlink);
                                return;
 #endif
-                               len = SMB_VFS_READLINK(conn,fullpathname, buffer, sizeof(pstring)-1);     /* read link */
+                               len = SMB_VFS_READLINK(conn,fullpathname,
+                                               buffer, PATH_MAX);
                                if (len == -1) {
                                        reply_unixerror(req, ERRDOS,
                                                        ERRnoaccess);
@@ -4229,7 +4545,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                uint16 num_def_acls = 0;
 
                                if (fsp && !fsp->is_directory && (fsp->fh->fd != -1)) {
-                                       file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp, fsp->fh->fd);
+                                       file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp);
                                } else {
                                        file_acl = SMB_VFS_SYS_ACL_GET_FILE(conn, fname, SMB_ACL_TYPE_ACCESS);
                                }
@@ -4335,7 +4651,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                case POSIX_LOCK_TYPE_UNLOCK:
                                default:
                                        /* There's no point in asking for an unlock... */
-                                       talloc_destroy(data_ctx);
                                        reply_nterror(
                                                req,
                                                NT_STATUS_INVALID_PARAMETER);
@@ -4395,7 +4710,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        return;
        }
 
-       send_trans2_replies(req, params, param_size, *ppdata, data_size,
+       send_trans2_replies(conn, req, params, param_size, *ppdata, data_size,
                            max_data_bytes);
 
        return;
@@ -4406,17 +4721,23 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
  code.
 ****************************************************************************/
 
-NTSTATUS hardlink_internals(connection_struct *conn, pstring oldname, pstring newname)
+NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
+               connection_struct *conn,
+               const char *oldname_in,
+               const char *newname_in)
 {
        SMB_STRUCT_STAT sbuf1, sbuf2;
-       pstring last_component_oldname;
-       pstring last_component_newname;
+       char *last_component_oldname = NULL;
+       char *last_component_newname = NULL;
+       char *oldname = NULL;
+       char *newname = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
        ZERO_STRUCT(sbuf1);
        ZERO_STRUCT(sbuf2);
 
-       status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
+       status = unix_convert(ctx, conn, oldname_in, False, &oldname,
+                       &last_component_oldname, &sbuf1);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -4431,7 +4752,8 @@ NTSTATUS hardlink_internals(connection_struct *conn, pstring oldname, pstring ne
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
-       status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
+       status = unix_convert(ctx, conn, newname_in, False, &newname,
+                       &last_component_newname, &sbuf2);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -4472,17 +4794,17 @@ NTSTATUS hardlink_internals(connection_struct *conn, pstring oldname, pstring ne
  Deal with setting the time from any of the setfilepathinfo functions.
 ****************************************************************************/
 
-static NTSTATUS smb_set_file_time(connection_struct *conn,
-                               files_struct *fsp,
-                               const char *fname,
-                               const SMB_STRUCT_STAT *psbuf,
-                               struct timespec ts[2])
+NTSTATUS smb_set_file_time(connection_struct *conn,
+                          files_struct *fsp,
+                          const char *fname,
+                          const SMB_STRUCT_STAT *psbuf,
+                          struct timespec ts[2],
+                          bool setting_write_time)
 {
        uint32 action =
                FILE_NOTIFY_CHANGE_LAST_ACCESS
                |FILE_NOTIFY_CHANGE_LAST_WRITE;
 
-       
        if (!VALID_STAT(*psbuf)) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
@@ -4498,6 +4820,11 @@ static NTSTATUS smb_set_file_time(connection_struct *conn,
                action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
        }
 
+       if (!setting_write_time) {
+               /* ts[1] comes from change time, not write time. */
+               action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
+       }
+
        DEBUG(6,("smb_set_file_time: actime: %s " , time_to_asc(convert_timespec_to_time_t(ts[0])) ));
        DEBUG(6,("smb_set_file_time: modtime: %s ", time_to_asc(convert_timespec_to_time_t(ts[1])) ));
 
@@ -4514,7 +4841,7 @@ static NTSTATUS smb_set_file_time(connection_struct *conn,
                }
        }
 
-       if(fsp != NULL) {
+       if (setting_write_time) {
                /*
                 * This was a setfileinfo on an open file.
                 * NT does this a lot. We also need to 
@@ -4525,21 +4852,25 @@ static NTSTATUS smb_set_file_time(connection_struct *conn,
                 * away and will set it on file close and after a write. JRA.
                 */
 
-               if (!null_timespec(ts[1])) {
-                       DEBUG(10,("smb_set_file_time: setting pending modtime to %s\n",
-                               time_to_asc(convert_timespec_to_time_t(ts[1])) ));
-                       fsp_set_pending_modtime(fsp, ts[1]);
-               }
+               DEBUG(10,("smb_set_file_time: setting pending modtime to %s\n",
+                         time_to_asc(convert_timespec_to_time_t(ts[1])) ));
 
+               if (fsp != NULL) {
+                       set_write_time_fsp(fsp, ts[1], true);
+               } else {
+                       set_write_time_path(conn, fname,
+                                           vfs_file_id_from_sbuf(conn, psbuf),
+                                           ts[1], true);
+               }
        }
+
        DEBUG(10,("smb_set_file_time: setting utimes to modified values.\n"));
 
        if(file_ntimes(conn, fname, ts)!=0) {
                return map_nt_error_from_unix(errno);
        }
-       if (action != 0) {
-               notify_fname(conn, NOTIFY_ACTION_MODIFIED, action, fname);
-       }
+       notify_fname(conn, NOTIFY_ACTION_MODIFIED, action, fname);
+
        return NT_STATUS_OK;
 }
 
@@ -4572,7 +4903,7 @@ static NTSTATUS smb_set_file_dosmode(connection_struct *conn,
                DEBUG(10,("smb_set_file_dosmode: file %s : setting dos mode 0x%x\n",
                                        fname, (unsigned int)dosmode ));
 
-               if(file_set_dosmode(conn, fname, dosmode, psbuf, False)) {
+               if(file_set_dosmode(conn, fname, dosmode, psbuf, NULL, false)) {
                        DEBUG(2,("smb_set_file_dosmode: file_set_dosmode of %s failed (%s)\n",
                                                fname, strerror(errno)));
                        return map_nt_error_from_unix(errno);
@@ -4617,7 +4948,7 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
        }
 
        status = open_file_ntcreate(conn, req, fname, psbuf,
-                               FILE_WRITE_DATA,
+                               FILE_WRITE_ATTRIBUTES,
                                FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
                                FILE_OPEN,
                                0,
@@ -4673,17 +5004,12 @@ static NTSTATUS smb_info_set_ea(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       ctx = talloc_init("SMB_INFO_SET_EA");
-       if (!ctx) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       ctx = talloc_tos();
        ea_list = read_ea_list(ctx, pdata + 4, total_data - 4);
        if (!ea_list) {
-               talloc_destroy(ctx);
                return NT_STATUS_INVALID_PARAMETER;
        }
        status = set_ea(conn, fsp, fname, ea_list);
-       talloc_destroy(ctx);
 
        return status;
 }
@@ -4700,7 +5026,7 @@ static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
                                SMB_STRUCT_STAT *psbuf)
 {
        NTSTATUS status = NT_STATUS_OK;
-       BOOL delete_on_close;
+       bool delete_on_close;
        uint32 dosmode = 0;
 
        if (total_data < 1) {
@@ -4799,9 +5125,10 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
                                       int total_data,
                                       const char *fname)
 {
-       pstring link_target;
+       char *link_target = NULL;
        const char *newname = fname;
        NTSTATUS status = NT_STATUS_OK;
+       TALLOC_CTX *ctx = talloc_tos();
 
        /* Set a symbolic link. */
        /* Don't allow this if follow links is false. */
@@ -4814,27 +5141,42 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
                return NT_STATUS_ACCESS_DENIED;
        }
 
-       srvstr_pull(pdata, req->flags2, link_target, pdata,
-                   sizeof(link_target), total_data, STR_TERMINATE);
+       srvstr_pull_talloc(ctx, pdata, req->flags2, &link_target, pdata,
+                   total_data, STR_TERMINATE);
+
+       if (!link_target) {
+               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))) {
-               pstring rel_name;
+               char *rel_name = NULL;
                char *last_dirp = NULL;
 
                if (*link_target == '/') {
                        /* No absolute paths allowed. */
                        return NT_STATUS_ACCESS_DENIED;
                }
-               pstrcpy(rel_name, newname);
+               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 {
-                       pstrcpy(rel_name, "./");
+                       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;
                }
-               pstrcat(rel_name, link_target);
 
                status = check_name(conn, rel_name);
                if (!NT_STATUS_IS_OK(status)) {
@@ -4859,9 +5201,10 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
 static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
                                        struct smb_request *req,
                                        const char *pdata, int total_data,
-                                       pstring fname)
+                                       const char *fname)
 {
-       pstring oldname;
+       char *oldname = NULL;
+       TALLOC_CTX *ctx = talloc_tos();
        NTSTATUS status = NT_STATUS_OK;
 
        /* Set a hard link. */
@@ -4869,14 +5212,16 @@ static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       srvstr_get_path(pdata, req->flags2, oldname, pdata,
-                       sizeof(oldname), total_data, STR_TERMINATE, &status);
+       srvstr_get_path(ctx, pdata, req->flags2, &oldname, pdata,
+                       total_data, STR_TERMINATE, &status);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       status = resolve_dfspath(conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                oldname);
+       status = resolve_dfspath(ctx, conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               oldname,
+                               &oldname);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -4884,7 +5229,7 @@ static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
        DEBUG(10,("smb_set_file_unix_hlink: SMB_SET_FILE_UNIX_LINK doing hard link %s -> %s\n",
                fname, oldname));
 
-       return hardlink_internals(conn, oldname, fname);
+       return hardlink_internals(ctx, conn, oldname, fname);
 }
 
 /****************************************************************************
@@ -4893,17 +5238,20 @@ static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
 
 static NTSTATUS smb_file_rename_information(connection_struct *conn,
                                            struct smb_request *req,
-                                           const char *pdata, int total_data,
-                                           files_struct *fsp, pstring fname)
+                                           const char *pdata,
+                                           int total_data,
+                                           files_struct *fsp,
+                                           const char *fname)
 {
-       BOOL overwrite;
+       bool overwrite;
        uint32 root_fid;
        uint32 len;
-       pstring newname;
-       pstring base_name;
-       BOOL dest_has_wcard = False;
+       char *newname = NULL;
+       char *base_name = NULL;
+       bool dest_has_wcard = False;
        NTSTATUS status = NT_STATUS_OK;
        char *p;
+       TALLOC_CTX *ctx = talloc_tos();
 
        if (total_data < 13) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -4917,16 +5265,21 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       srvstr_get_path_wcard(pdata, req->flags2, newname, &pdata[12],
-                             sizeof(newname), len, 0, &status,
+       srvstr_get_path_wcard(ctx, pdata, req->flags2, &newname, &pdata[12],
+                             len, 0, &status,
                              &dest_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       status = resolve_dfspath_wcard(conn,
+       DEBUG(10,("smb_file_rename_information: got name |%s|\n",
+                               newname));
+
+       status = resolve_dfspath_wcard(ctx, conn,
                                       req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                      newname, &dest_has_wcard);
+                                      newname,
+                                      &newname,
+                                      &dest_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -4937,25 +5290,58 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
        }
 
        /* Create the base directory. */
-       pstrcpy(base_name, fname);
+       base_name = talloc_strdup(ctx, fname);
+       if (!base_name) {
+               return NT_STATUS_NO_MEMORY;
+       }
        p = strrchr_m(base_name, '/');
        if (p) {
                p[1] = '\0';
        } else {
-               pstrcpy(base_name, "./");
+               base_name = talloc_strdup(ctx, "./");
+               if (!base_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
        }
        /* Append the new name. */
-       pstrcat(base_name, newname);
+       base_name = talloc_asprintf_append(base_name,
+                       "%s",
+                       newname);
+       if (!base_name) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        if (fsp) {
+               SMB_STRUCT_STAT sbuf;
+               char *newname_last_component = NULL;
+
+               ZERO_STRUCT(sbuf);
+
+               status = unix_convert(ctx, conn, newname, False,
+                                       &newname,
+                                       &newname_last_component,
+                                       &sbuf);
+
+               /* If an error we expect this to be
+                * NT_STATUS_OBJECT_PATH_NOT_FOUND */
+
+               if (!NT_STATUS_IS_OK(status)
+                   && !NT_STATUS_EQUAL(NT_STATUS_OBJECT_PATH_NOT_FOUND,
+                                       status)) {
+                       return status;
+               }
+
                DEBUG(10,("smb_file_rename_information: SMB_FILE_RENAME_INFORMATION (fnum %d) %s -> %s\n",
                        fsp->fnum, fsp->fsp_name, base_name ));
-               status = rename_internals_fsp(conn, fsp, base_name, 0, overwrite);
+               status = rename_internals_fsp(conn, fsp, base_name,
+                                             newname_last_component, 0,
+                                             overwrite);
        } else {
                DEBUG(10,("smb_file_rename_information: SMB_FILE_RENAME_INFORMATION %s -> %s\n",
-                       fname, newname ));
-               status = rename_internals(conn, req, fname, base_name, 0,
-                                         overwrite, False, dest_has_wcard);
+                       fname, base_name ));
+               status = rename_internals(ctx, conn, req, fname, base_name, 0,
+                                       overwrite, False, dest_has_wcard,
+                                       FILE_WRITE_ATTRIBUTES);
        }
 
        return status;
@@ -4976,8 +5362,8 @@ static NTSTATUS smb_set_posix_acl(connection_struct *conn,
        uint16 posix_acl_version;
        uint16 num_file_acls;
        uint16 num_def_acls;
-       BOOL valid_file_acls = True;
-       BOOL valid_def_acls = True;
+       bool valid_file_acls = True;
+       bool valid_def_acls = True;
 
        if (total_data < SMB_POSIX_ACL_HEADER_SIZE) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -5029,8 +5415,7 @@ static NTSTATUS smb_set_posix_acl(connection_struct *conn,
 ****************************************************************************/
 
 static NTSTATUS smb_set_posix_lock(connection_struct *conn,
-                               const uint8 *inbuf,
-                               int length,
+                               const struct smb_request *req,
                                const char *pdata,
                                int total_data,
                                files_struct *fsp)
@@ -5038,8 +5423,9 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
        SMB_BIG_UINT count;
        SMB_BIG_UINT offset;
        uint32 lock_pid;
-       BOOL blocking_lock = False;
+       bool blocking_lock = False;
        enum brl_type lock_type;
+
        NTSTATUS status = NT_STATUS_OK;
 
        if (fsp == NULL || fsp->fh->fd == -1) {
@@ -5127,7 +5513,7 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
                         * onto the blocking lock queue.
                         */
                        if(push_blocking_lock_request(br_lck,
-                                               (char *)inbuf, length,
+                                               req,
                                                fsp,
                                                -1, /* infinite timeout. */
                                                0,
@@ -5176,7 +5562,8 @@ static NTSTATUS smb_set_info_standard(connection_struct *conn,
                                fsp,
                                fname,
                                psbuf,
-                               ts);
+                               ts,
+                               true);
 }
 
 /****************************************************************************
@@ -5196,6 +5583,7 @@ static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
        uint32 dosmode = 0;
        struct timespec ts[2];
        NTSTATUS status = NT_STATUS_OK;
+       bool setting_write_time = true;
 
        if (total_data < 36) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -5228,7 +5616,12 @@ static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
 
        /* Prefer a defined time to an undefined one. */
        if (null_timespec(ts[1])) {
-               ts[1] = null_timespec(write_time) ? changed_time : write_time;
+               if (null_timespec(write_time)) {
+                       ts[1] = changed_time;
+                       setting_write_time = false;
+               } else {
+                       ts[1] = write_time;
+               }
        }
 
        DEBUG(10,("smb_set_file_basic_info: file %s\n",
@@ -5238,7 +5631,8 @@ static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
                                fsp,
                                fname,
                                psbuf,
-                               ts);
+                               ts,
+                               setting_write_time);
 }
 
 /****************************************************************************
@@ -5294,13 +5688,11 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
                        }
                }
                /* But always update the time. */
-               if (null_timespec(fsp->pending_modtime)) {
-                       /*
-                        * This is equivalent to a write. Ensure it's seen immediately
-                        * if there are no pending writes.
-                        */
-                       set_filetime(fsp->conn, fsp->fsp_name, timespec_current());
-               }
+               /*
+                * This is equivalent to a write. Ensure it's seen immediately
+                * if there are no pending writes.
+                */
+               trigger_write_time_update(fsp);
                return NT_STATUS_OK;
        }
 
@@ -5330,10 +5722,11 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
        }
 
        /* Changing the allocation size should set the last mod time. */
-       /* Don't need to call set_filetime as this will be flushed on
-        * close. */
-
-       fsp_set_pending_modtime(new_fsp, timespec_current());
+       /*
+        * This is equivalent to a write. Ensure it's seen immediately
+        * if there are no pending writes.
+        */
+       trigger_write_time_update(new_fsp);
 
        close_file(new_fsp,NORMAL_CLOSE);
        return NT_STATUS_OK;
@@ -5479,7 +5872,7 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        uid_t set_owner = (uid_t)SMB_UID_NO_CHANGE;
        gid_t set_grp = (uid_t)SMB_GID_NO_CHANGE;
        NTSTATUS status = NT_STATUS_OK;
-       BOOL delete_on_fail = False;
+       bool delete_on_fail = False;
        enum perm_type ptype;
 
        if (total_data < 100) {
@@ -5632,7 +6025,8 @@ size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
                                fsp,
                                fname,
                                psbuf,
-                               ts);
+                               ts,
+                               true);
 }
 
 /****************************************************************************
@@ -5804,7 +6198,7 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
                                SMB_STRUCT_STAT *psbuf,
                                int *pdata_return_size)
 {
-       BOOL extended_oplock_granted = False;
+       bool extended_oplock_granted = False;
        char *pdata = *ppdata;
        uint32 flags = 0;
        uint32 wire_open_mode = 0;
@@ -6053,7 +6447,8 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
         * non-POSIX opens return SHARING_VIOLATION.
         */
 
-       lck = get_share_mode_lock(NULL, fsp->file_id, NULL, NULL);
+       lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
+                                 NULL);
        if (lck == NULL) {
                DEBUG(0, ("smb_posix_unlink: Could not get share mode "
                        "lock for file %s\n", fsp->fsp_name));
@@ -6113,10 +6508,11 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
        char *pdata = *ppdata;
        uint16 info_level;
        SMB_STRUCT_STAT sbuf;
-       pstring fname;
+       char *fname = NULL;
        files_struct *fsp = NULL;
        NTSTATUS status = NT_STATUS_OK;
        int data_return_size = 0;
+       TALLOC_CTX *ctx = talloc_tos();
 
        if (!params) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -6132,15 +6528,24 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                }
 
                fsp = file_fsp(SVAL(params,0));
-               info_level = SVAL(params,2);    
+               /* Basic check for non-null fsp. */
+               if (!check_fsp_open(conn, req, fsp, &current_user)) {
+                       return;
+               }
+               info_level = SVAL(params,2);
 
-               if(fsp && (fsp->is_directory || fsp->fh->fd == -1)) {
+               fname = talloc_strdup(talloc_tos(),fsp->fsp_name);
+               if (!fname) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       return;
+               }
+
+               if(fsp->is_directory || fsp->fh->fd == -1) {
                        /*
                         * This is actually a SETFILEINFO on a directory
                         * handle (returned from an NT SMB). NT5.0 seems
                         * to do this call. JRA.
                         */
-                       pstrcpy(fname, fsp->fsp_name);
                        if (INFO_LEVEL_IS_UNIX(info_level)) {
                                /* Always do lstat for UNIX calls. */
                                if (SMB_VFS_LSTAT(conn,fname,&sbuf)) {
@@ -6155,7 +6560,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                        return;
                                }
                        }
-               } else if (fsp && fsp->print_file) {
+               } else if (fsp->print_file) {
                        /*
                         * Doing a DELETE_ON_CLOSE should cancel a print job.
                         */
@@ -6163,18 +6568,17 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                fsp->fh->private_options |= FILE_DELETE_ON_CLOSE;
 
                                DEBUG(3,("call_trans2setfilepathinfo: Cancelling print job (%s)\n", fsp->fsp_name ));
-       
+
                                SSVAL(params,0,0);
-                               send_trans2_replies(req, params, 2,
+                               send_trans2_replies(conn, req, params, 2,
                                                    *ppdata, 0,
                                                    max_data_bytes);
                                return;
-                       }
-                       else {
+                       } else {
                                reply_unixerror(req, ERRDOS, ERRbadpath);
                                return;
                        }
-           } else {
+               } else {
                        /*
                         * Original code - this is an open file.
                         */
@@ -6182,9 +6586,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                return;
                        }
 
-                       pstrcpy(fname, fsp->fsp_name);
-
-                       if (SMB_VFS_FSTAT(fsp, fsp->fh->fd, &sbuf) != 0) {
+                       if (SMB_VFS_FSTAT(fsp, &sbuf) != 0) {
                                DEBUG(3,("call_trans2setfilepathinfo: fstat of fnum %d failed (%s)\n",fsp->fnum, strerror(errno)));
                                reply_unixerror(req, ERRDOS, ERRbadfid);
                                return;
@@ -6197,18 +6599,19 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               info_level = SVAL(params,0);    
-               srvstr_get_path(params, req->flags2, fname, &params[6],
-                               sizeof(fname), total_params - 6, STR_TERMINATE,
+               info_level = SVAL(params,0);
+               srvstr_get_path(ctx, params, req->flags2, &fname, &params[6],
+                               total_params - 6, STR_TERMINATE,
                                &status);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        return;
                }
 
-               status = resolve_dfspath(conn,
+               status = resolve_dfspath(ctx, conn,
                                         req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                        fname);
+                                        fname,
+                                        &fname);
                if (!NT_STATUS_IS_OK(status)) {
                        if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                                reply_botherror(req,
@@ -6220,7 +6623,8 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               status = unix_convert(conn, fname, False, NULL, &sbuf);
+               status = unix_convert(ctx, conn, fname, False,
+                               &fname, NULL, &sbuf);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        return;
@@ -6270,11 +6674,6 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
 
        SSVAL(params,0,0);
 
-       if (fsp && !null_timespec(fsp->pending_modtime)) {
-               /* the pending modtime overrides the current modtime */
-               set_mtimespec(&sbuf, fsp->pending_modtime);
-       }
-
        switch (info_level) {
 
                case SMB_INFO_STANDARD:
@@ -6456,8 +6855,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
                        }
-                       status = smb_set_posix_lock(conn, req->inbuf,
-                                                   smb_len(req->inbuf) + 4,
+                       status = smb_set_posix_lock(conn, req,
                                                    pdata, total_data, fsp);
                        break;
                }
@@ -6515,16 +6913,9 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                        ERRSRV, ERRbadpath);
                        return;
                }
-               if (info_level == SMB_POSIX_PATH_OPEN &&
-                       NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
-                               /*
-                                * We hit an existing file, and if we're returning DOS
-                                * error codes OBJECT_NAME_COLLISION would map to
-                                * ERRDOS/183, we need to return ERRDOS/80, see bug
-                                * 4852.
-                                */
-                               return ERROR_BOTH(NT_STATUS_OBJECT_NAME_COLLISION,
-                                       ERRDOS, ERRfilexists);
+               if (info_level == SMB_POSIX_PATH_OPEN) {
+                       reply_openerror(req, status);
+                       return;
                }
 
                reply_nterror(req, status);
@@ -6532,7 +6923,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
        }
 
        SSVAL(params,0,0);
-       send_trans2_replies(req, params, 2, *ppdata, data_return_size,
+       send_trans2_replies(conn, req, params, 2, *ppdata, data_return_size,
                            max_data_bytes);
   
        return;
@@ -6549,10 +6940,11 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
 {
        char *params = *pparams;
        char *pdata = *ppdata;
-       pstring directory;
+       char *directory = NULL;
        SMB_STRUCT_STAT sbuf;
        NTSTATUS status = NT_STATUS_OK;
        struct ea_list *ea_list = NULL;
+       TALLOC_CTX *ctx = talloc_tos();
 
        if (!CAN_WRITE(conn)) {
                reply_doserror(req, ERRSRV, ERRaccess);
@@ -6564,8 +6956,8 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                return;
        }
 
-       srvstr_get_path(params, req->flags2, directory, &params[4],
-                       sizeof(directory), total_params - 4, STR_TERMINATE,
+       srvstr_get_path(ctx, params, req->flags2, &directory, &params[4],
+                       total_params - 4, STR_TERMINATE,
                        &status);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
@@ -6574,7 +6966,7 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
 
        DEBUG(3,("call_trans2mkdir : name = %s\n", directory));
 
-       status = unix_convert(conn, directory, False, NULL, &sbuf);
+       status = unix_convert(ctx, conn, directory, False, &directory, NULL, &sbuf);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
@@ -6612,7 +7004,7 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                        return;
                }
 
-               ea_list = read_ea_list(tmp_talloc_ctx(), pdata + 4,
+               ea_list = read_ea_list(talloc_tos(), pdata + 4,
                                       total_data - 4);
                if (!ea_list) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -6623,7 +7015,7 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                return;
        }
 
-       status = create_directory(conn, directory);
+       status = create_directory(conn, req, directory);
 
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
@@ -6649,7 +7041,7 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
 
        SSVAL(params,0,0);
 
-       send_trans2_replies(req, params, 2, *ppdata, 0, max_data_bytes);
+       send_trans2_replies(conn, req, params, 2, *ppdata, 0, max_data_bytes);
   
        return;
 }
@@ -6703,7 +7095,7 @@ static void call_trans2findnotifyfirst(connection_struct *conn,
        if(fnf_handle == 0)
                fnf_handle = 257;
 
-       send_trans2_replies(req, params, 6, *ppdata, 0, max_data_bytes);
+       send_trans2_replies(conn, req, params, 6, *ppdata, 0, max_data_bytes);
   
        return;
 }
@@ -6734,7 +7126,7 @@ static void call_trans2findnotifynext(connection_struct *conn,
        SSVAL(params,0,0); /* No changes */
        SSVAL(params,2,0); /* No EA errors */
 
-       send_trans2_replies(req, params, 4, *ppdata, 0, max_data_bytes);
+       send_trans2_replies(conn, req, params, 4, *ppdata, 0, max_data_bytes);
   
        return;
 }
@@ -6750,10 +7142,11 @@ static void call_trans2getdfsreferral(connection_struct *conn,
                                      unsigned int max_data_bytes)
 {
        char *params = *pparams;
-       pstring pathname;
+       char *pathname = NULL;
        int reply_size = 0;
        int max_referral_level;
        NTSTATUS status = NT_STATUS_OK;
+       TALLOC_CTX *ctx = talloc_tos();
 
        DEBUG(10,("call_trans2getdfsreferral\n"));
 
@@ -6769,17 +7162,21 @@ static void call_trans2getdfsreferral(connection_struct *conn,
                return;
        }
 
-       srvstr_pull(params, req->flags2, pathname, &params[2],
-                   sizeof(pathname), total_params - 2, STR_TERMINATE);
+       srvstr_pull_talloc(ctx, params, req->flags2, &pathname, &params[2],
+                   total_params - 2, STR_TERMINATE);
+       if (!pathname) {
+               reply_nterror(req, NT_STATUS_NOT_FOUND);
+               return;
+       }
        if((reply_size = setup_dfs_referral(conn, pathname, max_referral_level,
                                            ppdata,&status)) < 0) {
                reply_nterror(req, status);
                return;
        }
-    
+
        SSVAL(req->inbuf, smb_flg2,
              SVAL(req->inbuf,smb_flg2) | FLAGS2_DFS_PATHNAMES);
-       send_trans2_replies(req,0,0,*ppdata,reply_size, max_data_bytes);
+       send_trans2_replies(conn, req,0,0,*ppdata,reply_size, max_data_bytes);
 
        return;
 }
@@ -6801,7 +7198,7 @@ static void call_trans2ioctl(connection_struct *conn,
        files_struct *fsp = file_fsp(SVAL(req->inbuf,smb_vwv15));
 
        /* check for an invalid fid before proceeding */
-       
+
        if (!fsp) {
                reply_doserror(req, ERRDOS, ERRbadfid);
                return;
@@ -6826,7 +7223,7 @@ static void call_trans2ioctl(connection_struct *conn,
                srvstr_push(pdata, req->flags2, pdata+18,
                            lp_servicename(SNUM(conn)), 13,
                            STR_ASCII|STR_TERMINATE); /* Service name */
-               send_trans2_replies(req, *pparams, 0, *ppdata, 32,
+               send_trans2_replies(conn, req, *pparams, 0, *ppdata, 32,
                                    max_data_bytes);
                return;
        }
@@ -6839,7 +7236,7 @@ static void call_trans2ioctl(connection_struct *conn,
  Reply to a SMBfindclose (stop trans2 directory search).
 ****************************************************************************/
 
-void reply_findclose(connection_struct *conn, struct smb_request *req)
+void reply_findclose(struct smb_request *req)
 {
        int dptr_num;
 
@@ -6869,7 +7266,7 @@ void reply_findclose(connection_struct *conn, struct smb_request *req)
  Reply to a SMBfindnclose (stop FINDNOTIFYFIRST directory search).
 ****************************************************************************/
 
-void reply_findnclose(connection_struct *conn, struct smb_request *req)
+void reply_findnclose(struct smb_request *req)
 {
        int dptr_num;
 
@@ -6905,6 +7302,17 @@ static void handle_trans2(connection_struct *conn, struct smb_request *req,
                SSVAL(req->inbuf,smb_flg2,req->flags2);
        }
 
+       if (conn->encrypt_level == Required && !req->encrypted) {
+               if (state->call != TRANSACT2_QFSINFO &&
+                               state->call != TRANSACT2_SETFSINFO) {
+                       DEBUG(0,("handle_trans2: encryption required "
+                               "with call 0x%x\n",
+                               (unsigned int)state->call));
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+                       return;
+               }
+       }
+
        /* Now we must call the relevant TRANS2 function */
        switch(state->call)  {
        case TRANSACT2_OPEN:
@@ -7052,8 +7460,9 @@ static void handle_trans2(connection_struct *conn, struct smb_request *req,
  Reply to a SMBtrans2.
  ****************************************************************************/
 
-void reply_trans2(connection_struct *conn, struct smb_request *req)
+void reply_trans2(struct smb_request *req)
 {
+       connection_struct *conn = req->conn;
        unsigned int dsoff;
        unsigned int dscnt;
        unsigned int psoff;
@@ -7087,12 +7496,20 @@ void reply_trans2(connection_struct *conn, struct smb_request *req)
                return;
        }
 
-       if (IS_IPC(conn) && (tran_call != TRANSACT2_OPEN)
-            && (tran_call != TRANSACT2_GET_DFS_REFERRAL)
-            && (tran_call != TRANSACT2_QFILEINFO)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
-               END_PROFILE(SMBtrans2);
-               return;
+       if (IS_IPC(conn)) {
+               switch (tran_call) {
+               /* List the allowed trans2 calls on IPC$ */
+               case TRANSACT2_OPEN:
+               case TRANSACT2_GET_DFS_REFERRAL:
+               case TRANSACT2_QFILEINFO:
+               case TRANSACT2_QFSINFO:
+               case TRANSACT2_SETFSINFO:
+                       break;
+               default:
+                       reply_doserror(req, ERRSRV, ERRaccess);
+                       END_PROFILE(SMBtrans2);
+                       return;
+               }
        }
 
        if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
@@ -7233,8 +7650,9 @@ void reply_trans2(connection_struct *conn, struct smb_request *req)
  Reply to a SMBtranss2
  ****************************************************************************/
 
-void reply_transs2(connection_struct *conn, struct smb_request *req)
+void reply_transs2(struct smb_request *req)
 {
+       connection_struct *conn = req->conn;
        unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
        struct trans_state *state;
        int size;