s3:smbd: return DELETE_PENDING on path based operations on streams, when the main...
[metze/samba/wip.git] / source / smbd / trans2.c
index ce038e719acf5e5f88f0862def23c0a8a0d2aa7f..80c3694f4beb87aa9074ba3984852f6f8ddbaf92 100644 (file)
@@ -5,7 +5,7 @@
    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
 
@@ -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)
-               return NULL;
+       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));
 
-       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;
+       }
 
-       if (sizeret) {
-               for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p) + 1) {
-                       struct ea_list *listp;
+       /*
+        * Ensure the result is 0-terminated
+        */
 
-                       if (strnequal(p, "system.", 7) || samba_private_attr_name(p))
-                               continue;
+       if (ea_namelist[sizeret-1] != '\0') {
+               TALLOC_FREE(names);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
 
-                       listp = TALLOC_P(mem_ctx, struct ea_list);
-                       if (!listp)
-                               return NULL;
+       /*
+        * count the names
+        */
+       num_names = 0;
 
-                       if (!get_ea_value(mem_ctx, conn, fsp, fname, p, &listp->ea)) {
-                               return NULL;
-                       }
+       for (p = ea_namelist; p - ea_namelist < sizeret; p += strlen(p)+1) {
+               num_names += 1;
+       }
 
-                       {
-                               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 *);
+       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;
+       }
+
+       status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
+                                       &names, &num_names);
+
+       if (!NT_STATUS_IS_OK(status) || (num_names == 0)) {
+               return NULL;
+       }
+
+       for (i=0; i<num_names; i++) {
+               struct ea_list *listp;
+               fstring dos_ea_name;
+
+               if (strnequal(names[i], "system.", 7)
+                   || samba_private_attr_name(names[i]))
+                       continue;
+
+               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 *);
+
+       }
+
+       /* 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));
+       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
@@ -635,14 +737,16 @@ void send_trans2_replies(struct smb_request *req,
                                    + alignment_offset
                                    + data_alignment_offset);
 
-       /* useable_space can never be more than max_send minus the alignment offset. */
-
-       useable_space = MIN(useable_space, max_send - (alignment_offset+data_alignment_offset));
+       if (useable_space < 0) {
+               DEBUG(0, ("send_trans2_replies failed sanity useable_space "
+                         "= %d!!!", useable_space));
+               exit_server_cleanly("send_trans2_replies: Not enough space");
+       }
 
        while (params_to_send || data_to_send) {
                /* Calculate whether we will totally or partially fill this packet */
 
-               total_sent_thistime = params_to_send + data_to_send + alignment_offset + data_alignment_offset;
+               total_sent_thistime = params_to_send + data_to_send;
 
                /* We can never send more than useable_space */
                /*
@@ -652,9 +756,10 @@ void send_trans2_replies(struct smb_request *req,
                 * are sent here. Fix from Marc_Jacobsen@hp.com.
                 */
 
-               total_sent_thistime = MIN(total_sent_thistime, useable_space+ alignment_offset + data_alignment_offset);
+               total_sent_thistime = MIN(total_sent_thistime, useable_space);
 
-               reply_outbuf(req, 10, total_sent_thistime);
+               reply_outbuf(req, 10, total_sent_thistime + alignment_offset
+                            + data_alignment_offset);
 
                /* Set total params and data to be sent */
                SSVAL(req->outbuf,smb_tprcnt,paramsize);
@@ -738,8 +843,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,9 +881,9 @@ 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
@@ -842,20 +949,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(ctx, conn, fname, False, &fname, 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;
@@ -900,14 +993,22 @@ 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)) {
@@ -919,7 +1020,7 @@ static void call_trans2open(connection_struct *conn,
        }
 
        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) {
@@ -928,41 +1029,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 ) {
@@ -991,14 +1057,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);
 }
 
 /*********************************************************
@@ -1008,7 +1074,7 @@ static void call_trans2open(connection_struct *conn,
  Case can be significant or not.
 **********************************************************/
 
-static BOOL exact_match(connection_struct *conn,
+static bool exact_match(connection_struct *conn,
                const char *str,
                const char *mask)
 {
@@ -1132,29 +1198,56 @@ static NTSTATUS unix_perms_from_wire( connection_struct *conn,
        return NT_STATUS_OK;
 }
 
+/****************************************************************************
+ Needed to show the msdfs symlinks as directories. Modifies psbuf
+ to be a directory if it's a msdfs link.
+****************************************************************************/
+
+static bool check_msdfs_link(connection_struct *conn,
+                               const char *pathname,
+                               SMB_STRUCT_STAT *psbuf)
+{
+       int saved_errno = errno;
+       if(lp_host_msdfs() &&
+               lp_msdfs_root(SNUM(conn)) &&
+               is_msdfs_link(conn, pathname, psbuf)) {
+
+               DEBUG(5,("check_msdfs_link: Masquerading msdfs link %s "
+                       "as a directory\n",
+                       pathname));
+               psbuf->st_mode = (psbuf->st_mode & 0xFFF) | S_IFDIR;
+               errno = saved_errno;
+               return true;
+       }
+       errno = saved_errno;
+       return false;
+}
+
+
 /****************************************************************************
  Get a level dependent lanman2 dir entry.
 ****************************************************************************/
 
-static BOOL get_lanman2_dir_entry(TALLOC_CTX *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 dont_descend,
+                               bool ask_sharemode,
                                char **ppdata,
                                char *base_data,
                                char *end_data,
                                int space_remaining,
-                               BOOL *out_of_space,
-                               BOOL *got_exact_match,
+                               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;
        const char *mask = NULL;
        char *pathreal = NULL;
@@ -1170,10 +1263,10 @@ static BOOL get_lanman2_dir_entry(TALLOC_CTX *ctx,
        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. */
 
        *out_of_space = False;
@@ -1199,8 +1292,8 @@ static BOOL get_lanman2_dir_entry(TALLOC_CTX *ctx,
        }
 
        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);
@@ -1262,7 +1355,7 @@ static BOOL get_lanman2_dir_entry(TALLOC_CTX *ctx,
                }
 
                if (got_match) {
-                       BOOL isdots = (ISDOT(dname) || ISDOTDOT(dname));
+                       bool isdots = (ISDOT(dname) || ISDOTDOT(dname));
 
                        if (dont_descend && !isdots) {
                                continue;
@@ -1296,16 +1389,8 @@ static BOOL get_lanman2_dir_entry(TALLOC_CTX *ctx,
                                /* Needed to show the msdfs symlinks as
                                 * directories */
 
-                               if(lp_host_msdfs() &&
-                                  lp_msdfs_root(SNUM(conn)) &&
-                                  ((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));
-                                       sbuf.st_mode = (sbuf.st_mode & 0xFFF) | S_IFDIR;
-
-                               } else {
-
+                               ms_dfs_link = check_msdfs_link(conn, pathreal, &sbuf);
+                               if (!ms_dfs_link) {
                                        DEBUG(5,("get_lanman2_dir_entry:Couldn't stat [%s] (%s)\n",
                                                pathreal,strerror(errno)));
                                        TALLOC_FREE(pathreal);
@@ -1334,6 +1419,17 @@ static BOOL get_lanman2_dir_entry(TALLOC_CTX *ctx,
                        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);
@@ -1372,7 +1468,9 @@ static BOOL get_lanman2_dir_entry(TALLOC_CTX *ctx,
                        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);
@@ -1782,25 +1880,26 @@ 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;
        char *directory = NULL;
-       const char *mask = NULL;
+       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;
        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) {
@@ -1839,6 +1938,8 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                        break;
                case SMB_FIND_FILE_UNIX:
                case SMB_FIND_FILE_UNIX_INFO2:
+                       /* Always use filesystem for UNIX mtime query. */
+                       ask_sharemode = false;
                        if (!lp_unix_extensions()) {
                                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
@@ -1872,7 +1973,7 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                return;
        }
 
-       ntstatus = unix_convert(ctx, conn, directory, True, &directory, NULL, &sbuf);
+       ntstatus = unix_convert(ctx, conn, directory, True, &directory, &mask, &sbuf);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                reply_nterror(req, ntstatus);
                return;
@@ -1888,10 +1989,12 @@ 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')) {
-                       mask = "*";
+                       mask = talloc_strdup(ctx,"*");
+                       if (!mask) {
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
+                               return;
+                       }
                        mask_contains_wcard = True;
-               } else {
-                       mask = directory;
                }
                directory = talloc_strdup(talloc_tos(), "./");
                if (!directory) {
@@ -1899,7 +2002,6 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                        return;
                }
        } else {
-               mask = p+1;
                *p = 0;
        }
 
@@ -1984,7 +2086,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        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 */
@@ -1992,19 +2094,16 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        out_of_space = True;
                        finished = False;
                } else {
-                       TALLOC_CTX *sub_ctx = talloc_stackframe();
-
-                       finished = !get_lanman2_dir_entry(sub_ctx,
+                       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);
-
-                       TALLOC_FREE(sub_ctx);
                }
 
                if (finished && out_of_space)
@@ -2023,7 +2122,13 @@ 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);
+               }
        }
 
        /* Check if we can close the dirptr */
@@ -2060,7 +2165,7 @@ 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)) {
@@ -2113,11 +2218,11 @@ 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;
+       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;
@@ -2125,12 +2230,13 @@ static void call_trans2findnext(connection_struct *conn,
        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;
        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) {
@@ -2192,6 +2298,8 @@ resume_key = %d resume name = %s continue=%d level = %d\n",
                        break;
                case SMB_FIND_FILE_UNIX:
                case SMB_FIND_FILE_UNIX_INFO2:
+                       /* Always use filesystem for UNIX mtime query. */
+                       ask_sharemode = false;
                        if (!lp_unix_extensions()) {
                                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
@@ -2326,7 +2434,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 */
@@ -2334,19 +2442,16 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        out_of_space = True;
                        finished = False;
                } else {
-                       TALLOC_CTX *sub_ctx = talloc_stackframe();
-
-                       finished = !get_lanman2_dir_entry(sub_ctx,
+                       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);
-
-                       TALLOC_FREE(sub_ctx);
                }
 
                if (finished && out_of_space)
@@ -2384,7 +2489,7 @@ 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);
 
        return;
@@ -2396,6 +2501,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).
 ****************************************************************************/
@@ -2414,8 +2554,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;
@@ -2423,6 +2563,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) {
@@ -2507,16 +2668,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
@@ -2708,7 +2874,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;
                }
@@ -2719,22 +2892,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|
-                                       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:
                {
@@ -2850,8 +3053,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 */
@@ -2871,8 +3074,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]);
@@ -2899,7 +3102,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",
@@ -2934,6 +3137,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:
                        {
@@ -2984,6 +3209,62 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                }
                                break;
                        }
+
+               case SMB_REQUEST_TRANSPORT_ENCRYPTION:
+                       {
+                               NTSTATUS status;
+                               size_t param_len = 0;
+                               size_t data_len = total_data;
+
+                               if (!lp_unix_extensions()) {
+                                       reply_nterror(
+                                               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 encryption.\n"));
+
+                               status = srv_request_encryption_setup(conn,
+                                                               (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(conn, req,
+                                               *pparams,
+                                               param_len,
+                                               *ppdata,
+                                               data_len,
+                                               max_data_bytes);
+
+                               if (NT_STATUS_IS_OK(status)) {
+                                       /* 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");
+                                       }
+                               }
+                               return;
+                       }
+
                case SMB_FS_QUOTA_INFORMATION:
                        {
                                files_struct *fsp = NULL;
@@ -3110,7 +3391,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;
@@ -3265,7 +3546,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;}
@@ -3309,7 +3590,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)
@@ -3375,6 +3656,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 !
 ****************************************************************************/
@@ -3440,7 +3787,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;
@@ -3475,15 +3822,17 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        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;
        struct ea_list *ea_list = NULL;
        uint32 access_mask = 0x12019F; /* Default - GENERIC_EXECUTE mapping from Windows */
        char *lock_data = NULL;
+       bool ms_dfs_link = false;
        TALLOC_CTX *ctx = talloc_tos();
 
        if (!params) {
@@ -3492,6 +3841,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) {
@@ -3556,7 +3906,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.
@@ -3565,14 +3915,14 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                return;
                        }
 
-                       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;
                }
 
@@ -3629,6 +3979,46 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        return;
                }
 
+               if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
+                   && is_ntfs_stream_name(fname)) {
+                       char *base;
+                       SMB_STRUCT_STAT bsbuf;
+
+                       status = split_ntfs_stream_name(talloc_tos(), fname,
+                                                       &base, NULL);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               DEBUG(10, ("create_file_unixpath: "
+                                       "split_ntfs_stream_name failed: %s\n",
+                                       nt_errstr(status)));
+                               reply_nterror(req, status);
+                               return;
+                       }
+
+                       SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
+
+                       if (INFO_LEVEL_IS_UNIX(info_level)) {
+                               /* Always do lstat for UNIX calls. */
+                               if (SMB_VFS_LSTAT(conn,base,&bsbuf)) {
+                                       DEBUG(3,("call_trans2qfilepathinfo: SMB_VFS_LSTAT of %s failed (%s)\n",base,strerror(errno)));
+                                       reply_unixerror(req,ERRDOS,ERRbadpath);
+                                       return;
+                               }
+                       } else {
+                               if (SMB_VFS_STAT(conn,base,&bsbuf) != 0) {
+                                       DEBUG(3,("call_trans2qfilepathinfo: fileinfo of %s failed (%s)\n",base,strerror(errno)));
+                                       reply_unixerror(req,ERRDOS,ERRbadpath);
+                                       return;
+                               }
+                       }
+
+                       fileid = vfs_file_id_from_sbuf(conn, &bsbuf);
+                       get_file_infos(fileid, &delete_pending, NULL);
+                       if (delete_pending) {
+                               reply_nterror(req, NT_STATUS_DELETE_PENDING);
+                               return;
+                       }
+               }
+
                if (INFO_LEVEL_IS_UNIX(info_level)) {
                        /* Always do lstat for UNIX calls. */
                        if (SMB_VFS_LSTAT(conn,fname,&sbuf)) {
@@ -3636,31 +4026,25 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                reply_unixerror(req, ERRDOS, ERRbadpath);
                                return;
                        }
+
                } else if (!VALID_STAT(sbuf) && SMB_VFS_STAT(conn,fname,&sbuf) && (info_level != SMB_INFO_IS_NAME_VALID)) {
-                       DEBUG(3,("call_trans2qfilepathinfo: SMB_VFS_STAT of %s failed (%s)\n",fname,strerror(errno)));
-                       reply_unixerror(req, ERRDOS, ERRbadpath);
-                       return;
+                       ms_dfs_link = check_msdfs_link(conn,fname,&sbuf);
+
+                       if (!ms_dfs_link) {
+                               DEBUG(3,("call_trans2qfilepathinfo: SMB_VFS_STAT of %s failed (%s)\n",fname,strerror(errno)));
+                               reply_unixerror(req, ERRDOS, ERRbadpath);
+                               return;
+                       }
                }
 
                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;
@@ -3675,10 +4059,24 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        else
                base_name = p+1;
 
-       mode = dos_mode(conn,fname,&sbuf);
+       if (ms_dfs_link) {
+               mode = dos_mode_msdfs(conn,fname,&sbuf);
+       } else {
+               mode = dos_mode(conn,fname,&sbuf);
+       }
        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);
@@ -3769,25 +4167,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) && !INFO_LEVEL_IS_UNIX(info_level)) {
+               mtime_ts = write_time_ts;
+       }
+
        if (lp_dos_filetime_resolution(SNUM(conn))) {
                dos_filetime_timespec(&create_time_ts);
                dos_filetime_timespec(&mtime_ts);
@@ -4073,28 +4466,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;
                        }
-                       break;
 
+                       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;
+                       }
+
+                       TALLOC_FREE(streams);
+
+                       break;
+               }
                case SMB_QUERY_COMPRESSION_INFO:
                case SMB_FILE_COMPRESSION_INFORMATION:
                        DEBUG(10,("call_trans2qfilepathinfo: SMB_FILE_COMPRESSION_INFORMATION\n"));
@@ -4162,7 +4576,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                case SMB_QUERY_FILE_UNIX_LINK:
                        {
-                               char *buffer = TALLOC_ARRAY(ctx, char, 1024);
+                               char *buffer = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
 
                                if (!buffer) {
                                        reply_nterror(req, NT_STATUS_NO_MEMORY);
@@ -4181,7 +4595,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                                return;
 #endif
                                len = SMB_VFS_READLINK(conn,fullpathname,
-                                               buffer, 1023);
+                                               buffer, PATH_MAX);
                                if (len == -1) {
                                        reply_unixerror(req, ERRDOS,
                                                        ERRnoaccess);
@@ -4207,7 +4621,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);
                                }
@@ -4372,7 +4786,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;
@@ -4456,17 +4870,17 @@ NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
  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;
        }
@@ -4482,6 +4896,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])) ));
 
@@ -4498,7 +4917,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 
@@ -4509,21 +4928,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_sticky_write_time_fsp(fsp, ts[1]);
+               } else {
+                       set_sticky_write_time_path(conn, fname,
+                                           vfs_file_id_from_sbuf(conn, psbuf),
+                                           ts[1]);
+               }
        }
+
        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;
 }
 
@@ -4556,7 +4979,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);
@@ -4597,11 +5020,12 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                if (vfs_set_filelen(fsp, size) == -1) {
                        return map_nt_error_from_unix(errno);
                }
+               trigger_write_time_update_immediate(fsp);
                return NT_STATUS_OK;
        }
 
        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,
@@ -4620,6 +5044,7 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                return status;
        }
 
+       trigger_write_time_update_immediate(new_fsp);
        close_file(new_fsp,NORMAL_CLOSE);
        return NT_STATUS_OK;
 }
@@ -4657,17 +5082,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;
 }
@@ -4684,7 +5104,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) {
@@ -4901,12 +5321,12 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                                            files_struct *fsp,
                                            const char *fname)
 {
-       BOOL overwrite;
+       bool overwrite;
        uint32 root_fid;
        uint32 len;
        char *newname = NULL;
        char *base_name = NULL;
-       BOOL dest_has_wcard = False;
+       bool dest_has_wcard = False;
        NTSTATUS status = NT_STATUS_OK;
        char *p;
        TALLOC_CTX *ctx = talloc_tos();
@@ -4947,26 +5367,42 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                return NT_STATUS_NOT_SUPPORTED;
        }
 
-       /* Create the base directory. */
-       base_name = talloc_strdup(ctx, fname);
-       if (!base_name) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       p = strrchr_m(base_name, '/');
-       if (p) {
-               p[1] = '\0';
+       if (fsp && fsp->base_fsp) {
+               if (newname[0] != ':') {
+                       return NT_STATUS_NOT_SUPPORTED;
+               }
+               base_name = talloc_asprintf(ctx, "%s%s",
+                                          fsp->base_fsp->fsp_name,
+                                          newname);
+               if (!base_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
        } else {
-               base_name = talloc_strdup(ctx, "./");
+               if (is_ntfs_stream_name(newname)) {
+                       return NT_STATUS_NOT_SUPPORTED;
+               }
+
+               /* Create the base directory. */
+               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 {
+                       base_name = talloc_strdup(ctx, "./");
+                       if (!base_name) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               }
+               /* Append the new name. */
+               base_name = talloc_asprintf_append(base_name,
+                               "%s",
+                               newname);
                if (!base_name) {
                        return NT_STATUS_NO_MEMORY;
                }
-       }
-       /* Append the new name. */
-       base_name = talloc_asprintf_append(base_name,
-                       "%s",
-                       newname);
-       if (!base_name) {
-               return NT_STATUS_NO_MEMORY;
        }
 
        if (fsp) {
@@ -4998,7 +5434,8 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                DEBUG(10,("smb_file_rename_information: SMB_FILE_RENAME_INFORMATION %s -> %s\n",
                        fname, base_name ));
                status = rename_internals(ctx, conn, req, fname, base_name, 0,
-                                         overwrite, False, dest_has_wcard);
+                                       overwrite, False, dest_has_wcard,
+                                       FILE_WRITE_ATTRIBUTES);
        }
 
        return status;
@@ -5019,8 +5456,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;
@@ -5072,8 +5509,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)
@@ -5081,8 +5517,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) {
@@ -5170,7 +5607,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,
@@ -5219,7 +5656,8 @@ static NTSTATUS smb_set_info_standard(connection_struct *conn,
                                fsp,
                                fname,
                                psbuf,
-                               ts);
+                               ts,
+                               true);
 }
 
 /****************************************************************************
@@ -5239,6 +5677,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;
@@ -5271,7 +5710,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",
@@ -5281,7 +5725,8 @@ static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
                                fsp,
                                fname,
                                psbuf,
-                               ts);
+                               ts,
+                               setting_write_time);
 }
 
 /****************************************************************************
@@ -5337,13 +5782,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_immediate(fsp);
                return NT_STATUS_OK;
        }
 
@@ -5373,10 +5816,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_immediate(new_fsp);
 
        close_file(new_fsp,NORMAL_CLOSE);
        return NT_STATUS_OK;
@@ -5522,7 +5966,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) {
@@ -5675,7 +6119,8 @@ size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
                                fsp,
                                fname,
                                psbuf,
-                               ts);
+                               ts,
+                               true);
 }
 
 /****************************************************************************
@@ -5847,7 +6292,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;
@@ -6096,7 +6541,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));
@@ -6218,7 +6664,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                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;
@@ -6234,7 +6680,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                return;
                        }
 
-                       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;
@@ -6322,11 +6768,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:
@@ -6508,8 +6949,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;
                }
@@ -6577,7 +7017,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;
@@ -6664,12 +7104,13 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        return;
                }
-       } else if (IVAL(pdata,0) != 4) {
-               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
        }
+       /* If total_data == 4 Windows doesn't care what values
+        * are placed in that field, it just ignores them.
+        * The System i QNTC IBM SMB client puts bad values here,
+        * so ignore them. */
 
-       status = create_directory(conn, directory);
+       status = create_directory(conn, req, directory);
 
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
@@ -6695,7 +7136,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;
 }
@@ -6749,7 +7190,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;
 }
@@ -6780,7 +7221,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;
 }
@@ -6830,7 +7271,7 @@ static void call_trans2getdfsreferral(connection_struct *conn,
 
        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;
 }
@@ -6877,7 +7318,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;
        }
@@ -6890,7 +7331,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;
 
@@ -6920,7 +7361,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;
 
@@ -6956,6 +7397,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:
@@ -7103,14 +7555,16 @@ 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;
        unsigned int pscnt;
        unsigned int tran_call;
-       int size;
+       unsigned int size;
+       unsigned int av_size;
        struct trans_state *state;
        NTSTATUS result;
 
@@ -7128,6 +7582,7 @@ void reply_trans2(connection_struct *conn, struct smb_request *req)
        pscnt = SVAL(req->inbuf, smb_pscnt);
        tran_call = SVAL(req->inbuf, smb_setup0);
        size = smb_len(req->inbuf) + 4;
+       av_size = smb_len(req->inbuf);
 
        result = allow_new_trans(conn->pending_trans, req->mid);
        if (!NT_STATUS_IS_OK(result)) {
@@ -7138,12 +7593,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) {
@@ -7212,12 +7675,17 @@ void reply_trans2(connection_struct *conn, struct smb_request *req)
                        END_PROFILE(SMBtrans2);
                        return;
                }
-               if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
+
+               if (dscnt > state->total_data ||
+                               dsoff+dscnt < dsoff) {
                        goto bad_param;
-               if ((smb_base(req->inbuf)+dsoff+dscnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
+               }
+
+               if (dsoff > av_size ||
+                               dscnt > av_size ||
+                               dsoff+dscnt > av_size) {
                        goto bad_param;
+               }
 
                memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
        }
@@ -7235,12 +7703,17 @@ void reply_trans2(connection_struct *conn, struct smb_request *req)
                        END_PROFILE(SMBtrans2);
                        return;
                } 
-               if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
+
+               if (pscnt > state->total_param ||
+                               psoff+pscnt < psoff) {
                        goto bad_param;
-               if ((smb_base(req->inbuf)+psoff+pscnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
+               }
+
+               if (psoff > av_size ||
+                               pscnt > av_size ||
+                               psoff+pscnt > av_size) {
                        goto bad_param;
+               }
 
                memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
        }
@@ -7284,11 +7757,13 @@ 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;
+       unsigned int size;
+       unsigned int av_size;
 
        START_PROFILE(SMBtranss2);
 
@@ -7301,6 +7776,7 @@ void reply_transs2(connection_struct *conn, struct smb_request *req)
        }
 
        size = smb_len(req->inbuf)+4;
+       av_size = smb_len(req->inbuf);
 
        for (state = conn->pending_trans; state != NULL;
             state = state->next) {
@@ -7339,36 +7815,38 @@ void reply_transs2(connection_struct *conn, struct smb_request *req)
                goto bad_param;
 
        if (pcnt) {
-               if (pdisp+pcnt > state->total_param)
+               if (pdisp > state->total_param ||
+                               pcnt > state->total_param ||
+                               pdisp+pcnt > state->total_param ||
+                               pdisp+pcnt < pdisp) {
                        goto bad_param;
-               if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
-                       goto bad_param;
-               if (pdisp > state->total_param)
-                       goto bad_param;
-               if ((smb_base(req->inbuf) + poff + pcnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf) + poff + pcnt < smb_base(req->inbuf)))
-                       goto bad_param;
-               if (state->param + pdisp < state->param)
+               }
+
+               if (poff > av_size ||
+                               pcnt > av_size ||
+                               poff+pcnt > av_size ||
+                               poff+pcnt < poff) {
                        goto bad_param;
+               }
 
                memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,
                       pcnt);
        }
 
        if (dcnt) {
-               if (ddisp+dcnt > state->total_data)
+               if (ddisp > state->total_data ||
+                               dcnt > state->total_data ||
+                               ddisp+dcnt > state->total_data ||
+                               ddisp+dcnt < ddisp) {
                        goto bad_param;
-               if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
-                       goto bad_param;
-               if (ddisp > state->total_data)
-                       goto bad_param;
-               if ((smb_base(req->inbuf) + doff + dcnt
-                    > (char *)req->inbuf + size) ||
-                   (smb_base(req->inbuf) + doff + dcnt < smb_base(req->inbuf)))
-                       goto bad_param;
-               if (state->data + ddisp < state->data)
+               }
+
+               if (ddisp > av_size ||
+                               dcnt > av_size ||
+                               ddisp+dcnt > av_size ||
+                               ddisp+dcnt < ddisp) {
                        goto bad_param;
+               }
 
                memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
                       dcnt);