s3-talloc Change TALLOC_ARRAY() to talloc_array()
[samba.git] / source3 / smbd / trans2.c
index f2c025b6c103e168027f3c41cbac898be1814a32..b9a4d4624b858e137b356f5c10a314294f736662 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
 #include "version.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "../libcli/auth/libcli_auth.h"
-
-extern enum protocol_types Protocol;
+#include "../librpc/gen_ndr/xattr.h"
+#include "../librpc/gen_ndr/ndr_security.h"
+#include "libcli/security/security.h"
+#include "trans2.h"
+#include "auth.h"
+#include "smbprofile.h"
+#include "rpc_server/srv_pipe_hnd.h"
 
 #define DIR_ENTRY_SAFETY_MARGIN 4096
 
@@ -59,6 +66,23 @@ uint64_t smb_roundup(connection_struct *conn, uint64_t val)
        return val;
 }
 
+/********************************************************************
+ Create a 64 bit FileIndex. If the file is on the same device as
+ the root of the share, just return the 64-bit inode. If it isn't,
+ mangle as we used to do.
+********************************************************************/
+
+uint64_t get_FileIndex(connection_struct *conn, const SMB_STRUCT_STAT *psbuf)
+{
+       uint64_t file_index;
+       if (conn->base_share_dev == psbuf->st_ex_dev) {
+               return (uint64_t)psbuf->st_ex_ino;
+       }
+       file_index = ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
+       file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
+       return file_index;
+}
+
 /****************************************************************************
  Utility functions for dealing with extended attributes.
 ****************************************************************************/
@@ -72,7 +96,8 @@ static bool samba_private_attr_name(const char *unix_ea_name)
        static const char * const prohibited_ea_names[] = {
                SAMBA_POSIX_INHERITANCE_EA_NAME,
                SAMBA_XATTR_DOS_ATTRIB,
-               SAMBA_XATTR_DOSTIMESTAMPS,
+               SAMBA_XATTR_MARKER,
+               XATTR_NTACL_NAME,
                NULL
        };
 
@@ -82,7 +107,7 @@ static bool samba_private_attr_name(const char *unix_ea_name)
                if (strequal( prohibited_ea_names[i], unix_ea_name))
                        return true;
        }
-       if (StrnCaseCmp(unix_ea_name, SAMBA_XATTR_DOSSTREAM_PREFIX,
+       if (strncasecmp_m(unix_ea_name, SAMBA_XATTR_DOSSTREAM_PREFIX,
                        strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) == 0) {
                return true;
        }
@@ -104,7 +129,7 @@ NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
 
  again:
 
-       val = TALLOC_REALLOC_ARRAY(mem_ctx, val, char, attr_size);
+       val = talloc_realloc(mem_ctx, val, char, attr_size);
        if (!val) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -156,7 +181,9 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
        ssize_t sizeret = -1;
 
        if (!lp_ea_support(SNUM(conn))) {
-               *pnames = NULL;
+               if (pnames) {
+                       *pnames = NULL;
+               }
                *pnum_names = 0;
                return NT_STATUS_OK;
        }
@@ -165,7 +192,7 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
         * TALLOC the result early to get the talloc hierarchy right.
         */
 
-       names = TALLOC_ARRAY(mem_ctx, char *, 1);
+       names = talloc_array(mem_ctx, char *, 1);
        if (names == NULL) {
                DEBUG(0, ("talloc failed\n"));
                return NT_STATUS_NO_MEMORY;
@@ -173,7 +200,7 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
 
        while (ea_namelist_size <= 65536) {
 
-               ea_namelist = TALLOC_REALLOC_ARRAY(
+               ea_namelist = talloc_realloc(
                        names, ea_namelist, char, ea_namelist_size);
                if (ea_namelist == NULL) {
                        DEBUG(0, ("talloc failed\n"));
@@ -232,7 +259,7 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
                num_names += 1;
        }
 
-       tmp = TALLOC_REALLOC_ARRAY(mem_ctx, names, char *, num_names);
+       tmp = talloc_realloc(mem_ctx, names, char *, num_names);
        if (tmp == NULL) {
                DEBUG(0, ("talloc failed\n"));
                TALLOC_FREE(names);
@@ -361,7 +388,7 @@ static unsigned int fill_ea_buffer(TALLOC_CTX *mem_ctx, char *pdata, unsigned in
                SCVAL(p,0,ea_list->ea.flags);
                SCVAL(p,1,dos_namelen);
                SSVAL(p,2,ea_list->ea.value.length);
-               fstrcpy(p+4, dos_ea_name);
+               strlcpy(p+4, dos_ea_name, dos_namelen+1);
                memcpy( p + 4 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
 
                total_data_size -= 4 + dos_namelen + 1 + ea_list->ea.value.length;
@@ -425,7 +452,7 @@ static NTSTATUS fill_ea_chained_buffer(TALLOC_CTX *mem_ctx,
                SCVAL(p, 0x04, ea_list->ea.flags);
                SCVAL(p, 0x05, dos_namelen);
                SSVAL(p, 0x06, ea_list->ea.value.length);
-               fstrcpy((char *)(p+0x08), dos_ea_name);
+               strlcpy((char *)(p+0x08), dos_ea_name, dos_namelen+1);
                memcpy(p + 0x08 + dos_namelen + 1, ea_list->ea.value.data, ea_list->ea.value.length);
 
                total_data_size -= this_size;
@@ -464,7 +491,7 @@ static void canonicalize_ea_name(connection_struct *conn, files_struct *fsp, con
                if (strequal(&unix_ea_name[5], ea_list->ea.name)) {
                        DEBUG(10,("canonicalize_ea_name: %s -> %s\n",
                                &unix_ea_name[5], ea_list->ea.name));
-                       safe_strcpy(&unix_ea_name[5], ea_list->ea.name, sizeof(fstring)-6);
+                       strlcpy(&unix_ea_name[5], ea_list->ea.name, sizeof(fstring)-5);
                        break;
                }
        }
@@ -483,6 +510,10 @@ NTSTATUS set_ea(connection_struct *conn, files_struct *fsp,
                return NT_STATUS_EAS_NOT_SUPPORTED;
        }
 
+       if (fsp && !(fsp->access_mask & FILE_WRITE_EA)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        /* For now setting EAs on streams isn't supported. */
        fname = smb_fname->base_name;
 
@@ -760,7 +791,7 @@ void send_trans2_replies(connection_struct *conn,
        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;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
        int max_send = sconn->smb1.sessions.max_send;
 
        /* Modify the data_to_send and datasize and set the error if
@@ -780,7 +811,7 @@ void send_trans2_replies(connection_struct *conn,
        if(params_to_send == 0 && data_to_send == 0) {
                reply_outbuf(req, 10, 0);
                show_msg((char *)req->outbuf);
-               if (!srv_send_smb(smbd_server_fd(),
+               if (!srv_send_smb(sconn,
                                (char *)req->outbuf,
                                true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn),
@@ -919,7 +950,7 @@ void send_trans2_replies(connection_struct *conn,
 
                /* Send the packet */
                show_msg((char *)req->outbuf);
-               if (!srv_send_smb(smbd_server_fd(),
+               if (!srv_send_smb(sconn,
                                (char *)req->outbuf,
                                true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn),
@@ -982,6 +1013,7 @@ static void call_trans2open(connection_struct *conn,
        uint32 share_mode;
        uint32 create_disposition;
        uint32 create_options = 0;
+       uint32_t private_flags = 0;
        TALLOC_CTX *ctx = talloc_tos();
 
        /*
@@ -1011,7 +1043,7 @@ static void call_trans2open(connection_struct *conn,
        pname = &params[28];
 
        if (IS_IPC(conn)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
                goto out;
        }
 
@@ -1053,18 +1085,14 @@ static void call_trans2open(connection_struct *conn,
        if (!map_open_params_to_ntcreate(smb_fname, deny_mode, open_ofun,
                                         &access_mask, &share_mode,
                                         &create_disposition,
-                                        &create_options)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+                                        &create_options,
+                                        &private_flags)) {
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
        /* Any data in this call is an EA list. */
-       if (total_data && (total_data != 4) && !lp_ea_support(SNUM(conn))) {
-               reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
-               goto out;
-       }
-
-       if (total_data != 4) {
+       if (total_data && (total_data != 4)) {
                if (total_data < 10) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
@@ -1083,9 +1111,11 @@ static void call_trans2open(connection_struct *conn,
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
                }
-       } else if (IVAL(pdata,0) != 4) {
-               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               goto out;
+
+               if (!lp_ea_support(SNUM(conn))) {
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
+                       goto out;
+               }
        }
 
        status = SMB_VFS_CREATE_FILE(
@@ -1100,6 +1130,7 @@ static void call_trans2open(connection_struct *conn,
                open_attr,                              /* file_attributes */
                oplock_request,                         /* oplock_request */
                open_size,                              /* allocation_size */
+               private_flags,
                NULL,                                   /* sd */
                ea_list,                                /* ea_list */
                &fsp,                                   /* result */
@@ -1118,9 +1149,9 @@ static void call_trans2open(connection_struct *conn,
        fattr = dos_mode(conn, smb_fname);
        mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
        inode = smb_fname->st.st_ex_ino;
-       if (fattr & aDIR) {
+       if (fattr & FILE_ATTRIBUTE_DIRECTORY) {
                close_file(req, fsp, ERROR_CLOSE);
-               reply_doserror(req, ERRDOS,ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -1188,7 +1219,7 @@ static bool exact_match(bool has_wild,
        if (case_sensitive) {
                return strcmp(str,mask)==0;
        } else {
-               return StrCaseCmp(str,mask) == 0;
+               return strcasecmp_m(str,mask) == 0;
        }
 }
 
@@ -1463,7 +1494,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                                    uint32_t mode,
                                    const char *fname,
                                    const struct smb_filename *smb_fname,
-                                   uint64_t space_remaining,
+                                   int space_remaining,
                                    uint8_t align,
                                    bool do_pad,
                                    char *base_data,
@@ -1476,6 +1507,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        uint32_t reskey=0;
        uint64_t file_size = 0;
        uint64_t allocation_size = 0;
+       uint64_t file_index = 0;
        uint32_t len;
        struct timespec mdate_ts, adate_ts, cdate_ts, create_date_ts;
        time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
@@ -1483,9 +1515,8 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        char *nameptr;
        char *last_entry_ptr;
        bool was_8_3;
-       uint32_t nt_extmode; /* Used for NT connections instead of mode */
-       off_t off;
-       off_t pad = 0;
+       int off;
+       int pad = 0;
 
        *out_of_space = false;
 
@@ -1494,11 +1525,13 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        ZERO_STRUCT(create_date_ts);
        ZERO_STRUCT(cdate_ts);
 
-       if (!(mode & aDIR)) {
+       if (!(mode & FILE_ATTRIBUTE_DIRECTORY)) {
                file_size = get_file_size_stat(&smb_fname->st);
        }
        allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
 
+       file_index = get_FileIndex(conn, &smb_fname->st);
+
        mdate_ts = smb_fname->st.st_ex_mtime;
        adate_ts = smb_fname->st.st_ex_atime;
        create_date_ts = get_create_timespec(conn, NULL, smb_fname);
@@ -1517,14 +1550,31 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        c_date = convert_timespec_to_time_t(cdate_ts);
 
        /* align the record */
-       off = PTR_DIFF(pdata, base_data);
+       SMB_ASSERT(align >= 1);
+
+       off = (int)PTR_DIFF(pdata, base_data);
        pad = (off + (align-1)) & ~(align-1);
        pad -= off;
+
+       if (pad && pad > space_remaining) {
+               *out_of_space = true;
+               DEBUG(9,("smbd_marshall_dir_entry: out of space "
+                       "for padding (wanted %u, had %d)\n",
+                       (unsigned int)pad,
+                       space_remaining ));
+               return false; /* Not finished - just out of space */
+       }
+
        off += pad;
        /* initialize padding to 0 */
-       memset(pdata, 0, pad);
+       if (pad) {
+               memset(pdata, 0, pad);
+       }
        space_remaining -= pad;
 
+       DEBUG(10,("smbd_marshall_dir_entry: space_remaining = %d\n",
+               space_remaining ));
+
        pdata += pad;
        p = pdata;
        last_entry_ptr = p;
@@ -1532,11 +1582,9 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        pad = 0;
        off = 0;
 
-       nt_extmode = mode ? mode : FILE_ATTRIBUTE_NORMAL;
-
        switch (info_level) {
        case SMB_FIND_INFO_STANDARD:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_INFO_STANDARD\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_INFO_STANDARD\n"));
                if(requires_resume_key) {
                        SIVAL(p,0,reskey);
                        p += 4;
@@ -1572,7 +1620,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                break;
 
        case SMB_FIND_EA_SIZE:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_SIZE\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_SIZE\n"));
                if (requires_resume_key) {
                        SIVAL(p,0,reskey);
                        p += 4;
@@ -1616,7 +1664,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                struct ea_list *file_list = NULL;
                size_t ea_len = 0;
 
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_EA_LIST\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_EA_LIST\n"));
                if (!name_list) {
                        return false;
                }
@@ -1641,7 +1689,10 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                /* Max string size is 255 bytes. */
                if (PTR_DIFF(p + 255 + ea_len,pdata) > space_remaining) {
                        *out_of_space = true;
-                       DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
+                       DEBUG(9,("smbd_marshall_dir_entry: out of space "
+                               "(wanted %u, had %d)\n",
+                               (unsigned int)PTR_DIFF(p + 255 + ea_len,pdata),
+                               space_remaining ));
                        return False; /* Not finished - just out of space */
                }
 
@@ -1671,7 +1722,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
        }
 
        case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_BOTH_DIRECTORY_INFO\n"));
                was_8_3 = mangle_is_8_3(fname, True, conn->params);
                p += 4;
                SIVAL(p,0,reskey); p += 4;
@@ -1681,7 +1732,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length. */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1738,7 +1789,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                break;
 
        case SMB_FIND_FILE_DIRECTORY_INFO:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_DIRECTORY_INFO\n"));
                p += 4;
                SIVAL(p,0,reskey); p += 4;
                put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
@@ -1747,7 +1798,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                len = srvstr_push(base_data, flags2,
                                  p + 4, fname, PTR_DIFF(end_data, p+4),
                                  STR_TERMINATE_ASCII);
@@ -1774,7 +1825,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                break;
 
        case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_FULL_DIRECTORY_INFO\n"));
                p += 4;
                SIVAL(p,0,reskey); p += 4;
                put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
@@ -1783,7 +1834,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length. */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1817,7 +1868,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                break;
 
        case SMB_FIND_FILE_NAMES_INFO:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_NAMES_INFO\n"));
                p += 4;
                SIVAL(p,0,reskey); p += 4;
                p += 4;
@@ -1849,7 +1900,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                break;
 
        case SMB_FIND_ID_FULL_DIRECTORY_INFO:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_FULL_DIRECTORY_INFO\n"));
                p += 4;
                SIVAL(p,0,reskey); p += 4;
                put_long_date_timespec(conn->ts_res,p,create_date_ts); p += 8;
@@ -1858,7 +1909,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length. */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1867,8 +1918,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                        p +=4;
                }
                SIVAL(p,0,0); p += 4; /* Unknown - reserved ? */
-               SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
-               SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
+               SBVAL(p,0,file_index); p += 8;
                len = srvstr_push(base_data, flags2, p,
                                  fname, PTR_DIFF(end_data, p),
                                  STR_TERMINATE_ASCII);
@@ -1895,7 +1945,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                break;
 
        case SMB_FIND_ID_BOTH_DIRECTORY_INFO:
-               DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
+               DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_ID_BOTH_DIRECTORY_INFO\n"));
                was_8_3 = mangle_is_8_3(fname, True, conn->params);
                p += 4;
                SIVAL(p,0,reskey); p += 4;
@@ -1905,7 +1955,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                put_long_date_timespec(conn->ts_res,p,cdate_ts); p += 8;
                SOFF_T(p,0,file_size); p += 8;
                SOFF_T(p,0,allocation_size); p += 8;
-               SIVAL(p,0,nt_extmode); p += 4;
+               SIVAL(p,0,mode); p += 4;
                q = p; p += 4; /* q is placeholder for name length */
                {
                        unsigned int ea_size = estimate_ea_size(conn, NULL,
@@ -1938,8 +1988,7 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                }
                p += 26;
                SSVAL(p,0,0); p += 2; /* Reserved ? */
-               SIVAL(p,0,smb_fname->st.st_ex_ino); p += 4; /* FileIndexLow */
-               SIVAL(p,0,smb_fname->st.st_ex_dev); p += 4; /* FileIndexHigh */
+               SBVAL(p,0,file_index); p += 8;
                len = srvstr_push(base_data, flags2, p,
                                  fname, PTR_DIFF(end_data, p),
                                  STR_TERMINATE_ASCII);
@@ -1975,14 +2024,14 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
                /* Begin of SMB_QUERY_FILE_UNIX_BASIC */
 
                if (info_level == SMB_FIND_FILE_UNIX) {
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX\n"));
+                       DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX\n"));
                        p = store_file_unix_basic(conn, p,
                                                NULL, &smb_fname->st);
                        len = srvstr_push(base_data, flags2, p,
                                          fname, PTR_DIFF(end_data, p),
                                          STR_TERMINATE);
                } else {
-                       DEBUG(10,("get_lanman2_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
+                       DEBUG(10,("smbd_marshall_dir_entry: SMB_FIND_FILE_UNIX_INFO2\n"));
                        p = store_file_unix_basic_info2(conn, p,
                                                NULL, &smb_fname->st);
                        nameptr = p;
@@ -2021,7 +2070,10 @@ static bool smbd_marshall_dir_entry(TALLOC_CTX *ctx,
 
        if (PTR_DIFF(p,pdata) > space_remaining) {
                *out_of_space = true;
-               DEBUG(9,("get_lanman2_dir_entry: out of space\n"));
+               DEBUG(9,("smbd_marshall_dir_entry: out of space "
+                       "(wanted %u, had %d)\n",
+                       (unsigned int)PTR_DIFF(p,pdata),
+                       space_remaining ));
                return false; /* Not finished - just out of space */
        }
 
@@ -2143,7 +2195,7 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                                const char *path_mask,
                                uint32 dirtype,
                                int info_level,
-                               int requires_resume_key,
+                               bool requires_resume_key,
                                bool dont_descend,
                                bool ask_sharemode,
                                char **ppdata,
@@ -2155,17 +2207,17 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                                int *last_entry_off,
                                struct ea_list *name_list)
 {
-       bool resume_key = false;
-       const uint8_t align = 4;
+       uint8_t align = 4;
        const bool do_pad = true;
 
-       if (requires_resume_key) {
-               resume_key = true;
+       if (info_level >= 1 && info_level <= 3) {
+               /* No alignment on earlier info levels. */
+               align = 1;
        }
 
        return smbd_dirptr_lanman2_entry(ctx, conn, dirptr, flags2,
                                         path_mask, dirtype, info_level,
-                                        resume_key, dont_descend, ask_sharemode,
+                                        requires_resume_key, dont_descend, ask_sharemode,
                                         align, do_pad,
                                         ppdata, base_data, end_data,
                                         space_remaining,
@@ -2216,7 +2268,7 @@ static void call_trans2findfirst(connection_struct *conn,
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        TALLOC_CTX *ctx = talloc_tos();
        struct dptr_struct *dirptr = NULL;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        if (total_params < 13) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2306,13 +2358,17 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                        }
                        mask_contains_wcard = True;
                }
-               directory = talloc_strdup(talloc_tos(), "./");
+       } else {
+               *p = 0;
+       }
+
+       if (p == NULL || p == directory) {
+               /* Ensure we don't have a directory name of "". */
+               directory = talloc_strdup(talloc_tos(), ".");
                if (!directory) {
                        reply_nterror(req, NT_STATUS_NO_MEMORY);
                        goto out;
                }
-       } else {
-               *p = 0;
        }
 
        DEBUG(5,("dir=%s, mask = %s\n",directory, mask));
@@ -2334,7 +2390,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                }
 
                if (!lp_ea_support(SNUM(conn))) {
-                       reply_doserror(req, ERRDOS, ERReasnotsupported);
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                        goto out;
                }
 
@@ -2367,6 +2423,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                needed as lanman2 assumes these are being saved between calls */
 
        ntstatus = dptr_create(conn,
+                               NULL, /* fsp */
                                directory,
                                False,
                                True,
@@ -2461,8 +2518,8 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
        if(numentries == 0) {
                dptr_close(sconn, &dptr_num);
-               if (Protocol < PROTOCOL_NT1) {
-                       reply_doserror(req, ERRDOS, ERRnofiles);
+               if (get_Protocol() < PROTOCOL_NT1) {
+                       reply_force_doserror(req, ERRDOS, ERRnofiles);
                        goto out;
                } else {
                        reply_botherror(req, NT_STATUS_NO_SUCH_FILE,
@@ -2555,7 +2612,7 @@ static void call_trans2findnext(connection_struct *conn,
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        TALLOC_CTX *ctx = talloc_tos();
        struct dptr_struct *dirptr;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        if (total_params < 13) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -2572,23 +2629,26 @@ static void call_trans2findnext(connection_struct *conn,
        requires_resume_key = (findnext_flags & FLAG_TRANS2_FIND_REQUIRE_RESUME);
        continue_bit = (findnext_flags & FLAG_TRANS2_FIND_CONTINUE);
 
-       srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
+       if (!continue_bit) {
+               /* We only need resume_name if continue_bit is zero. */
+               srvstr_get_path_wcard(ctx, params, req->flags2, &resume_name,
                              params+12,
                              total_params - 12, STR_TERMINATE, &ntstatus,
                              &mask_contains_wcard);
-       if (!NT_STATUS_IS_OK(ntstatus)) {
-               /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
-                  complain (it thinks we're asking for the directory above the shared
-                  path or an invalid name). Catch this as the resume name is only compared, never used in
-                  a file access. JRA. */
-               srvstr_pull_talloc(ctx, params, req->flags2,
+               if (!NT_STATUS_IS_OK(ntstatus)) {
+                       /* Win9x or OS/2 can send a resume name of ".." or ".". This will cause the parser to
+                          complain (it thinks we're asking for the directory above the shared
+                          path or an invalid name). Catch this as the resume name is only compared, never used in
+                          a file access. JRA. */
+                       srvstr_pull_talloc(ctx, params, req->flags2,
                                &resume_name, params+12,
                                total_params - 12,
                                STR_TERMINATE);
 
-               if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
-                       reply_nterror(req, ntstatus);
-                       return;
+                       if (!resume_name || !(ISDOT(resume_name) || ISDOTDOT(resume_name))) {
+                               reply_nterror(req, ntstatus);
+                               return;
+                       }
                }
        }
 
@@ -2596,7 +2656,8 @@ static void call_trans2findnext(connection_struct *conn,
 close_after_request=%d, close_if_end = %d requires_resume_key = %d \
 resume_key = %d resume name = %s continue=%d level = %d\n",
                dptr_num, max_data_bytes, maxentries, close_after_request, close_if_end, 
-               requires_resume_key, resume_key, resume_name, continue_bit, info_level));
+               requires_resume_key, resume_key,
+               resume_name ? resume_name : "(NULL)", continue_bit, info_level));
 
        if (!maxentries) {
                /* W2K3 seems to treat zero as 1. */
@@ -2645,7 +2706,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                }
 
                if (!lp_ea_support(SNUM(conn))) {
-                       reply_doserror(req, ERRDOS, ERReasnotsupported);
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                        return;
                }
 
@@ -2678,7 +2739,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
        /* Check that the dptr is valid */
        if(!(dirptr = dptr_fetch_lanman2(sconn, dptr_num))) {
-               reply_doserror(req, ERRDOS, ERRnofiles);
+               reply_nterror(req, STATUS_NO_MORE_FILES);
                return;
        }
 
@@ -2687,7 +2748,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        /* Get the wildcard mask from the dptr */
        if((p = dptr_wcard(sconn, dptr_num))== NULL) {
                DEBUG(2,("dptr_num %d has no wildcard\n", dptr_num));
-               reply_doserror(req, ERRDOS, ERRnofiles);
+               reply_nterror(req, STATUS_NO_MORE_FILES);
                return;
        }
 
@@ -2721,7 +2782,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
         * depend on the last file name instead.
         */
 
-       if(*resume_name && !continue_bit) {
+       if(!continue_bit && resume_name && *resume_name) {
                SMB_STRUCT_STAT st;
 
                long current_pos = 0;
@@ -2846,8 +2907,8 @@ static void samba_extended_info_version(struct smb_extended_info *extended_info)
        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);
+#ifdef SAMBA_VERSION_COMMIT_TIME
+       unix_to_nt_time(&extended_info->samba_gitcommitdate, SAMBA_VERSION_COMMIT_TIME);
 #endif
 
        memset(extended_info->samba_version_string, 0,
@@ -2872,9 +2933,8 @@ NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
        int snum = SNUM(conn);
        char *fstype = lp_fstype(SNUM(conn));
        uint32 additional_flags = 0;
-       struct smb_filename *smb_fname_dot = NULL;
+       struct smb_filename smb_fname_dot;
        SMB_STRUCT_STAT st;
-       NTSTATUS status;
 
        if (IS_IPC(conn)) {
                if (info_level != SMB_QUERY_CIFS_UNIX_INFO) {
@@ -2887,20 +2947,15 @@ NTSTATUS smbd_do_qfsinfo(connection_struct *conn,
 
        DEBUG(3,("smbd_do_qfsinfo: level = %d\n", info_level));
 
-       status = create_synthetic_smb_fname(talloc_tos(), ".", NULL, NULL,
-                                           &smb_fname_dot);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
+       ZERO_STRUCT(smb_fname_dot);
+       smb_fname_dot.base_name = discard_const_p(char, ".");
 
-       if(SMB_VFS_STAT(conn, smb_fname_dot) != 0) {
+       if(SMB_VFS_STAT(conn, &smb_fname_dot) != 0) {
                DEBUG(2,("stat of . failed (%s)\n", strerror(errno)));
-               TALLOC_FREE(smb_fname_dot);
                return map_nt_error_from_unix(errno);
        }
 
-       st = smb_fname_dot->st;
-       TALLOC_FREE(smb_fname_dot);
+       st = smb_fname_dot.st;
 
        *ppdata = (char *)SMB_REALLOC(
                *ppdata, max_data_bytes + DIR_ENTRY_SAFETY_MARGIN);
@@ -3103,10 +3158,17 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
 
                case SMB_QUERY_FS_DEVICE_INFO:
                case SMB_FS_DEVICE_INFORMATION:
+               {
+                       uint32_t characteristics = FILE_DEVICE_IS_MOUNTED;
+
+                       if (!CAN_WRITE(conn)) {
+                               characteristics |= FILE_READ_ONLY_DEVICE;
+                       }
                        data_len = 8;
-                       SIVAL(pdata,0,0); /* dev type */
-                       SIVAL(pdata,4,0); /* characteristics */
+                       SIVAL(pdata,0,FILE_DEVICE_DISK); /* dev type */
+                       SIVAL(pdata,4,characteristics);
                        break;
+               }
 
 #ifdef HAVE_SYS_QUOTAS
                case SMB_FS_QUOTA_INFORMATION:
@@ -3143,11 +3205,11 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        fsp.fnum = -1;
 
                        /* access check */
-                       if (conn->server_info->utok.uid != sec_initial_uid()) {
+                       if (get_current_uid(conn) != 0) {
                                DEBUG(0,("set_user_quota: access_denied "
                                         "service [%s] user [%s]\n",
                                         lp_servicename(SNUM(conn)),
-                                        conn->server_info->unix_name));
+                                        conn->session_info->unix_name));
                                return NT_STATUS_ACCESS_DENIED;
                        }
 
@@ -3205,8 +3267,8 @@ 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(smbd_server_conn);
-                       bool large_read = !srv_is_signing_active(smbd_server_conn);
+                                       !srv_is_signing_active(conn->sconn);
+                       bool large_read = !srv_is_signing_active(conn->sconn);
                        int encrypt_caps = 0;
 
                        if (!lp_unix_extensions()) {
@@ -3300,7 +3362,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                         * in our list of SIDs.
                         */
                        if (nt_token_check_sid(&global_sid_Builtin_Guests,
-                                              conn->server_info->ptok)) {
+                                              conn->session_info->security_token)) {
                                flags |= SMB_WHOAMI_GUEST;
                        }
 
@@ -3308,7 +3370,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                         * is in our list of SIDs.
                         */
                        if (nt_token_check_sid(&global_sid_Authenticated_Users,
-                                              conn->server_info->ptok)) {
+                                              conn->session_info->security_token)) {
                                flags &= ~SMB_WHOAMI_GUEST;
                        }
 
@@ -3324,18 +3386,18 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                            + 4 /* num_sids */
                            + 4 /* SID bytes */
                            + 4 /* pad/reserved */
-                           + (conn->server_info->utok.ngroups * 8)
+                           + (conn->session_info->utok.ngroups * 8)
                                /* groups list */
-                           + (conn->server_info->ptok->num_sids *
+                           + (conn->session_info->security_token->num_sids *
                                    SID_MAX_SIZE)
                                /* SID list */;
 
                        SIVAL(pdata, 0, flags);
                        SIVAL(pdata, 4, SMB_WHOAMI_MASK);
                        SBIG_UINT(pdata, 8,
-                                 (uint64_t)conn->server_info->utok.uid);
+                                 (uint64_t)conn->session_info->utok.uid);
                        SBIG_UINT(pdata, 16,
-                                 (uint64_t)conn->server_info->utok.gid);
+                                 (uint64_t)conn->session_info->utok.gid);
 
 
                        if (data_len >= max_data_bytes) {
@@ -3350,18 +3412,17 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                                break;
                        }
 
-                       SIVAL(pdata, 24, conn->server_info->utok.ngroups);
-                       SIVAL(pdata, 28, conn->server_info->num_sids);
+                       SIVAL(pdata, 24, conn->session_info->utok.ngroups);
+                       SIVAL(pdata, 28, conn->session_info->security_token->num_sids);
 
                        /* We walk the SID list twice, but this call is fairly
                         * infrequent, and I don't expect that it's performance
                         * sensitive -- jpeach
                         */
                        for (i = 0, sid_bytes = 0;
-                            i < conn->server_info->ptok->num_sids; ++i) {
+                            i < conn->session_info->security_token->num_sids; ++i) {
                                sid_bytes += ndr_size_dom_sid(
-                                       &conn->server_info->ptok->user_sids[i],
-                                       NULL, 
+                                       &conn->session_info->security_token->sids[i],
                                        0);
                        }
 
@@ -3373,22 +3434,21 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        data_len = 40;
 
                        /* GID list */
-                       for (i = 0; i < conn->server_info->utok.ngroups; ++i) {
+                       for (i = 0; i < conn->session_info->utok.ngroups; ++i) {
                                SBIG_UINT(pdata, data_len,
-                                         (uint64_t)conn->server_info->utok.groups[i]);
+                                         (uint64_t)conn->session_info->utok.groups[i]);
                                data_len += 8;
                        }
 
                        /* SID list */
                        for (i = 0;
-                           i < conn->server_info->ptok->num_sids; ++i) {
+                           i < conn->session_info->security_token->num_sids; ++i) {
                                int sid_len = ndr_size_dom_sid(
-                                       &conn->server_info->ptok->user_sids[i],
-                                       NULL,
+                                       &conn->session_info->security_token->sids[i],
                                        0);
 
                                sid_linearize(pdata + data_len, sid_len,
-                                   &conn->server_info->ptok->user_sids[i]);
+                                   &conn->session_info->security_token->sids[i]);
                                data_len += sid_len;
                        }
 
@@ -3587,6 +3647,16 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                        return;
                                }
 
+                               if (req->sconn->smb1.echo_handler.trusted_fde) {
+                                       DEBUG( 2,("call_trans2setfsinfo: "
+                                               "request transport encryption disabled"
+                                               "with 'fork echo handler = yes'\n"));
+                                       reply_nterror(
+                                               req,
+                                               NT_STATUS_NOT_SUPPORTED);
+                                       return;
+                               }
+
                                DEBUG( 4,("call_trans2setfsinfo: "
                                        "request transport encryption.\n"));
 
@@ -3630,11 +3700,10 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                ZERO_STRUCT(quotas);
 
                                /* access check */
-                               if ((conn->server_info->utok.uid != sec_initial_uid())
-                                   ||!CAN_WRITE(conn)) {
+                               if ((get_current_uid(conn) != 0) || !CAN_WRITE(conn)) {
                                        DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
                                                 lp_servicename(SNUM(conn)),
-                                                conn->server_info->unix_name));
+                                                conn->session_info->unix_name));
                                        reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                                        return;
                                }
@@ -3665,36 +3734,10 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                /* unknown_1 24 NULL bytes in pdata*/
 
                                /* the soft quotas 8 bytes (uint64_t)*/
-                               quotas.softlim = (uint64_t)IVAL(pdata,24);
-#ifdef LARGE_SMB_OFF_T
-                               quotas.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
-#else /* LARGE_SMB_OFF_T */
-                               if ((IVAL(pdata,28) != 0)&&
-                                       ((quotas.softlim != 0xFFFFFFFF)||
-                                       (IVAL(pdata,28)!=0xFFFFFFFF))) {
-                                       /* more than 32 bits? */
-                                       reply_nterror(
-                                               req,
-                                               NT_STATUS_INVALID_PARAMETER);
-                                       return;
-                               }
-#endif /* LARGE_SMB_OFF_T */
+                               quotas.softlim = BVAL(pdata,24);
 
                                /* the hard quotas 8 bytes (uint64_t)*/
-                               quotas.hardlim = (uint64_t)IVAL(pdata,32);
-#ifdef LARGE_SMB_OFF_T
-                               quotas.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
-#else /* LARGE_SMB_OFF_T */
-                               if ((IVAL(pdata,36) != 0)&&
-                                       ((quotas.hardlim != 0xFFFFFFFF)||
-                                       (IVAL(pdata,36)!=0xFFFFFFFF))) {
-                                       /* more than 32 bits? */
-                                       reply_nterror(
-                                               req,
-                                               NT_STATUS_INVALID_PARAMETER);
-                                       return;
-                               }
-#endif /* LARGE_SMB_OFF_T */
+                               quotas.hardlim = BVAL(pdata,32);
 
                                /* quota_flags 2 bytes **/
                                quotas.qflags = SVAL(pdata,40);
@@ -3855,6 +3898,8 @@ static char *store_file_unix_basic(connection_struct *conn,
                                files_struct *fsp,
                                const SMB_STRUCT_STAT *psbuf)
 {
+       uint64_t file_index = get_FileIndex(conn, psbuf);
+
        DEBUG(10,("store_file_unix_basic: SMB_QUERY_FILE_UNIX_BASIC\n"));
        DEBUG(4,("store_file_unix_basic: st_mode=%o\n",(int)psbuf->st_ex_mode));
 
@@ -3888,7 +3933,7 @@ static char *store_file_unix_basic(connection_struct *conn,
        SIVAL(pdata,4,0);
        pdata += 8;
 
-       SINO_T_VAL(pdata,0,(SMB_INO_T)psbuf->st_ex_ino);   /* inode number */
+       SINO_T_VAL(pdata,0,(SMB_INO_T)file_index);   /* inode number */
        pdata += 8;
 
        SIVAL(pdata,0, unix_perms_to_wire(psbuf->st_ex_mode));     /* Standard UNIX file permissions */
@@ -4146,7 +4191,6 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                               struct smb_filename *smb_fname,
                               bool delete_pending,
                               struct timespec write_time_ts,
-                              bool ms_dfs_link,
                               struct ea_list *ea_list,
                               int lock_data_count,
                               char *lock_data,
@@ -4181,17 +4225,10 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                 smb_fname_str_dbg(smb_fname), fsp ? fsp->fnum : -1,
                 info_level, max_data_bytes));
 
-       if (ms_dfs_link) {
-               mode = dos_mode_msdfs(conn, smb_fname);
-       } else {
-               mode = dos_mode(conn, smb_fname);
-       }
-       if (!mode)
-               mode = FILE_ATTRIBUTE_NORMAL;
-
+       mode = dos_mode(conn, smb_fname);
        nlink = psbuf->st_ex_nlink;
 
-       if (nlink && (mode&aDIR)) {
+       if (nlink && (mode&FILE_ATTRIBUTE_DIRECTORY)) {
                nlink = 1;
        }
 
@@ -4266,13 +4303,13 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                /* Do we have this path open ? */
                files_struct *fsp1;
                struct file_id fileid = vfs_file_id_from_sbuf(conn, psbuf);
-               fsp1 = file_find_di_first(fileid);
+               fsp1 = file_find_di_first(conn->sconn, fileid);
                if (fsp1 && fsp1->initial_allocation_size) {
                        allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp1, psbuf);
                }
        }
 
-       if (!(mode & aDIR)) {
+       if (!(mode & FILE_ATTRIBUTE_DIRECTORY)) {
                file_size = get_file_size_stat(psbuf);
        }
 
@@ -4292,8 +4329,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
           I think this causes us to fail the IFSKIT
           BasicFileInformationTest. -tpot */
-       file_index =  ((psbuf->st_ex_ino) & UINT32_MAX); /* FileIndexLow */
-       file_index |= ((uint64_t)((psbuf->st_ex_dev) & UINT32_MAX)) << 32; /* FileIndexHigh */
+       file_index = get_FileIndex(conn, psbuf);
 
        switch (info_level) {
                case SMB_INFO_STANDARD:
@@ -4379,6 +4415,9 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                case 0xFF0F:/*SMB2_INFO_QUERY_ALL_EAS*/
                {
+                       /* This is FileFullEaInformation - 0xF which maps to
+                        * 1015 (decimal) in smbd_do_setfilepathinfo. */
+
                        /* We have data_size bytes to put EA's into. */
                        size_t total_ea_len = 0;
                        struct ea_list *ea_file_list = NULL;
@@ -4440,7 +4479,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                        SOFF_T(pdata,8,file_size);
                        SIVAL(pdata,16,nlink);
                        SCVAL(pdata,20,delete_pending?1:0);
-                       SCVAL(pdata,21,(mode&aDIR)?1:0);
+                       SCVAL(pdata,21,(mode&FILE_ATTRIBUTE_DIRECTORY)?1:0);
                        SSVAL(pdata,22,0); /* Padding. */
                        break;
 
@@ -4523,7 +4562,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                        SOFF_T(pdata,8,file_size);
                        SIVAL(pdata,16,nlink);
                        SCVAL(pdata,20,delete_pending);
-                       SCVAL(pdata,21,(mode&aDIR)?1:0);
+                       SCVAL(pdata,21,(mode&FILE_ATTRIBUTE_DIRECTORY)?1:0);
                        SSVAL(pdata,22,0);
                        pdata += 24;
                        SIVAL(pdata,0,ea_size);
@@ -4554,7 +4593,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                        SBVAL(pdata,    0x30, file_size);
                        SIVAL(pdata,    0x38, nlink);
                        SCVAL(pdata,    0x3C, delete_pending);
-                       SCVAL(pdata,    0x3D, (mode&aDIR)?1:0);
+                       SCVAL(pdata,    0x3D, (mode&FILE_ATTRIBUTE_DIRECTORY)?1:0);
                        SSVAL(pdata,    0x3E, 0); /* padding */
                        SBVAL(pdata,    0x40, file_index);
                        SIVAL(pdata,    0x48, ea_size);
@@ -4704,14 +4743,9 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                        pdata = store_file_unix_basic(conn, pdata, fsp, psbuf);
                        data_size = PTR_DIFF(pdata,(*ppdata));
 
-                       {
-                               int i;
-                               DEBUG(4,("smbd_do_qfilepathinfo: SMB_QUERY_FILE_UNIX_BASIC "));
-
-                               for (i=0; i<100; i++)
-                                       DEBUG(4,("%d=%x, ",i, (*ppdata)[i]));
-                               DEBUG(4,("\n"));
-                       }
+                       DEBUG(4,("smbd_do_qfilepathinfo: "
+                                "SMB_QUERY_FILE_UNIX_BASIC\n"));
+                       dump_data(4, (uint8_t *)(*ppdata), data_size);
 
                        break;
 
@@ -4734,7 +4768,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                case SMB_QUERY_FILE_UNIX_LINK:
                        {
                                int len;
-                               char *buffer = TALLOC_ARRAY(mem_ctx, char, PATH_MAX+1);
+                               char *buffer = talloc_array(mem_ctx, char, PATH_MAX+1);
 
                                if (!buffer) {
                                        return NT_STATUS_NO_MEMORY;
@@ -4773,7 +4807,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                uint16 num_file_acls = 0;
                                uint16 num_def_acls = 0;
 
-                               if (fsp && !fsp->is_directory && (fsp->fh->fd != -1)) {
+                               if (fsp && fsp->fh->fd != -1) {
                                        file_acl = SMB_VFS_SYS_ACL_GET_FD(fsp);
                                } else {
                                        file_acl =
@@ -4862,11 +4896,11 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                {
                        uint64_t count;
                        uint64_t offset;
-                       uint32 lock_pid;
+                       uint64_t smblctx;
                        enum brl_type lock_type;
 
                        /* We need an open file with a real fd for this. */
-                       if (!fsp || fsp->is_directory || fsp->fh->fd == -1) {
+                       if (!fsp || fsp->fh->fd == -1) {
                                return NT_STATUS_INVALID_LEVEL;
                        }
 
@@ -4887,7 +4921,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                        return NT_STATUS_INVALID_PARAMETER;
                        }
 
-                       lock_pid = IVAL(pdata, POSIX_LOCK_PID_OFFSET);
+                       smblctx = (uint64_t)IVAL(pdata, POSIX_LOCK_PID_OFFSET);
 #if defined(HAVE_LONGLONG)
                        offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
                                        ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
@@ -4899,7 +4933,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 #endif /* HAVE_LONGLONG */
 
                        status = query_lock(fsp,
-                                       &lock_pid,
+                                       &smblctx,
                                        &count,
                                        &offset,
                                        &lock_type,
@@ -4911,7 +4945,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
 
                                SSVAL(pdata, POSIX_LOCK_TYPE_OFFSET, lock_type);
                                SSVAL(pdata, POSIX_LOCK_FLAGS_OFFSET, 0);
-                               SIVAL(pdata, POSIX_LOCK_PID_OFFSET, lock_pid);
+                               SIVAL(pdata, POSIX_LOCK_PID_OFFSET, (uint32_t)smblctx);
 #if defined(HAVE_LONGLONG)
                                SIVAL(pdata, POSIX_LOCK_START_OFFSET, (uint32)(offset & 0xFFFFFFFF));
                                SIVAL(pdata, POSIX_LOCK_START_OFFSET + 4, (uint32)((offset >> 32) & 0xFFFFFFFF));
@@ -4967,7 +5001,6 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        struct ea_list *ea_list = NULL;
        int lock_data_count = 0;
        char *lock_data = NULL;
-       bool ms_dfs_link = false;
        NTSTATUS status = NT_STATUS_OK;
 
        if (!params) {
@@ -5020,7 +5053,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
 
                        /* We know this name is ok, it's already passed the checks. */
 
-               } else if(fsp->is_directory || fsp->fh->fd == -1) {
+               } else if(fsp->fh->fd == -1) {
                        /*
                         * This is actually a QFILEINFO on a directory
                         * handle (returned from an NT SMB). NT5.0 seems
@@ -5050,15 +5083,11 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        }
 
                        fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
-                       get_file_infos(fileid, &delete_pending, &write_time_ts);
+                       get_file_infos(fileid, fsp->name_hash, &delete_pending, &write_time_ts);
                } else {
                        /*
                         * Original code - this is an open file.
                         */
-                       if (!check_fsp(conn, req, fsp)) {
-                               return;
-                       }
-
                        if (SMB_VFS_FSTAT(fsp, &smb_fname->st) != 0) {
                                DEBUG(3, ("fstat of fnum %d failed (%s)\n",
                                          fsp->fnum, strerror(errno)));
@@ -5067,10 +5096,11 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                return;
                        }
                        fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
-                       get_file_infos(fileid, &delete_pending, &write_time_ts);
+                       get_file_infos(fileid, fsp->name_hash, &delete_pending, &write_time_ts);
                }
 
        } else {
+               uint32_t name_hash;
                char *fname = NULL;
 
                /* qpathinfo */
@@ -5157,10 +5187,19 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                }
                        }
 
+                       status = file_name_hash(conn,
+                                       smb_fname_str_dbg(smb_fname_base),
+                                       &name_hash);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               TALLOC_FREE(smb_fname_base);
+                               reply_nterror(req, status);
+                               return;
+                       }
+
                        fileid = vfs_file_id_from_sbuf(conn,
                                                       &smb_fname_base->st);
                        TALLOC_FREE(smb_fname_base);
-                       get_file_infos(fileid, &delete_pending, NULL);
+                       get_file_infos(fileid, name_hash, &delete_pending, NULL);
                        if (delete_pending) {
                                reply_nterror(req, NT_STATUS_DELETE_PENDING);
                                return;
@@ -5179,14 +5218,8 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                                return;
                        }
 
-               } else if (!VALID_STAT(smb_fname->st) &&
-                          SMB_VFS_STAT(conn, smb_fname) &&
-                          (info_level != SMB_INFO_IS_NAME_VALID)) {
-                       ms_dfs_link = check_msdfs_link(conn,
-                                                      smb_fname->base_name,
-                                                      &smb_fname->st);
-
-                       if (!ms_dfs_link) {
+               } else {
+                       if (SMB_VFS_STAT(conn, smb_fname) != 0) {
                                DEBUG(3,("call_trans2qfilepathinfo: "
                                         "SMB_VFS_STAT of %s failed (%s)\n",
                                         smb_fname_str_dbg(smb_fname),
@@ -5197,8 +5230,16 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        }
                }
 
+               status = file_name_hash(conn,
+                               smb_fname_str_dbg(smb_fname),
+                               &name_hash);
+               if (!NT_STATUS_IS_OK(status)) {
+                       reply_nterror(req, status);
+                       return;
+               }
+
                fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
-               get_file_infos(fileid, &delete_pending, &write_time_ts);
+               get_file_infos(fileid, name_hash, &delete_pending, &write_time_ts);
                if (delete_pending) {
                        reply_nterror(req, NT_STATUS_DELETE_PENDING);
                        return;
@@ -5232,8 +5273,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                        }
 
                        if (!lp_ea_support(SNUM(conn))) {
-                               reply_doserror(req, ERRDOS,
-                                              ERReasnotsupported);
+                               reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
                                return;
                        }
 
@@ -5311,7 +5351,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        status = smbd_do_qfilepathinfo(conn, req, info_level,
                                       fsp, smb_fname,
                                       delete_pending, write_time_ts,
-                                      ms_dfs_link, ea_list,
+                                      ea_list,
                                       lock_data_count, lock_data,
                                       req->flags2, max_data_bytes,
                                       ppdata, &data_size);
@@ -5333,8 +5373,10 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
 NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
                connection_struct *conn,
+               struct smb_request *req,
+               bool overwrite_if_exists,
                const struct smb_filename *smb_fname_old,
-               const struct smb_filename *smb_fname_new)
+               struct smb_filename *smb_fname_new)
 {
        NTSTATUS status = NT_STATUS_OK;
 
@@ -5343,9 +5385,23 @@ NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
-       /* Disallow if newname already exists. */
        if (VALID_STAT(smb_fname_new->st)) {
-               return NT_STATUS_OBJECT_NAME_COLLISION;
+               if (overwrite_if_exists) {
+                       if (S_ISDIR(smb_fname_new->st.st_ex_mode)) {
+                               return NT_STATUS_FILE_IS_A_DIRECTORY;
+                       }
+                       status = unlink_internals(conn,
+                                               req,
+                                               FILE_ATTRIBUTE_NORMAL,
+                                               smb_fname_new,
+                                               false);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               return status;
+                       }
+               } else {
+                       /* Disallow if newname already exists. */
+                       return NT_STATUS_OBJECT_NAME_COLLISION;
+               }
        }
 
        /* No links from a directory. */
@@ -5382,17 +5438,20 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
                           struct smb_file_time *ft,
                           bool setting_write_time)
 {
-       struct smb_filename *smb_fname_base = NULL;
+       struct smb_filename smb_fname_base;
        uint32 action =
                FILE_NOTIFY_CHANGE_LAST_ACCESS
                |FILE_NOTIFY_CHANGE_LAST_WRITE
                |FILE_NOTIFY_CHANGE_CREATION;
-       NTSTATUS status;
 
        if (!VALID_STAT(smb_fname->st)) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
+       if (fsp && !(fsp->access_mask & FILE_WRITE_ATTRIBUTES)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        /* get some defaults (no modifications) if any info is zero or -1. */
        if (null_timespec(ft->create_time)) {
                action &= ~FILE_NOTIFY_CHANGE_CREATION;
@@ -5459,18 +5518,12 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
        DEBUG(10,("smb_set_file_time: setting utimes to modified values.\n"));
 
        /* Always call ntimes on the base, even if a stream was passed in. */
-       status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
-                                           NULL, &smb_fname->st,
-                                           &smb_fname_base);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
+       smb_fname_base = *smb_fname;
+       smb_fname_base.stream_name = NULL;
 
-       if(file_ntimes(conn, smb_fname_base, ft)!=0) {
-               TALLOC_FREE(smb_fname_base);
+       if(file_ntimes(conn, &smb_fname_base, ft)!=0) {
                return map_nt_error_from_unix(errno);
        }
-       TALLOC_FREE(smb_fname_base);
 
        notify_fname(conn, NOTIFY_ACTION_MODIFIED, action,
                     smb_fname->base_name);
@@ -5502,9 +5555,9 @@ static NTSTATUS smb_set_file_dosmode(connection_struct *conn,
 
        if (dosmode) {
                if (S_ISDIR(smb_fname_base->st.st_ex_mode)) {
-                       dosmode |= aDIR;
+                       dosmode |= FILE_ATTRIBUTE_DIRECTORY;
                } else {
-                       dosmode &= ~aDIR;
+                       dosmode &= ~FILE_ATTRIBUTE_DIRECTORY;
                }
        }
 
@@ -5541,7 +5594,8 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                                  files_struct *fsp,
                                  const struct smb_filename *smb_fname,
                                  const SMB_STRUCT_STAT *psbuf,
-                                 SMB_OFF_T size)
+                                 SMB_OFF_T size,
+                                 bool fail_after_createfile)
 {
        NTSTATUS status = NT_STATUS_OK;
        struct smb_filename *smb_fname_tmp = NULL;
@@ -5551,6 +5605,10 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
+       if (fsp && !(fsp->access_mask & FILE_WRITE_DATA)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        DEBUG(6,("smb_set_file_size: size: %.0f ", (double)size));
 
        if (size == get_file_size_stat(psbuf)) {
@@ -5581,7 +5639,7 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                req,                                    /* req */
                0,                                      /* root_dir_fid */
                smb_fname_tmp,                          /* fname */
-               FILE_WRITE_ATTRIBUTES,                  /* access_mask */
+               FILE_WRITE_DATA,                        /* access_mask */
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
                FILE_OPEN,                              /* create_disposition*/
@@ -5589,6 +5647,7 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
                FORCE_OPLOCK_BREAK_TO_NONE,             /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &new_fsp,                               /* result */
@@ -5601,6 +5660,12 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                return status;
        }
 
+       /* See RAW-SFILEINFO-END-OF-FILE */
+       if (fail_after_createfile) {
+               close_file(req, new_fsp,NORMAL_CLOSE);
+               return NT_STATUS_INVALID_LEVEL;
+       }
+
        if (vfs_set_filelen(new_fsp, size) == -1) {
                status = map_nt_error_from_unix(errno);
                close_file(req, new_fsp,NORMAL_CLOSE);
@@ -5650,11 +5715,68 @@ static NTSTATUS smb_info_set_ea(connection_struct *conn,
        if (!ea_list) {
                return NT_STATUS_INVALID_PARAMETER;
        }
+
+       if (fsp && !(fsp->access_mask & FILE_WRITE_EA)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        status = set_ea(conn, fsp, smb_fname, ea_list);
 
        return status;
 }
 
+/****************************************************************************
+ Deal with SMB_FILE_FULL_EA_INFORMATION set.
+****************************************************************************/
+
+static NTSTATUS smb_set_file_full_ea_info(connection_struct *conn,
+                               const char *pdata,
+                               int total_data,
+                               files_struct *fsp)
+{
+       struct ea_list *ea_list = NULL;
+       NTSTATUS status;
+
+       if (!fsp) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (!lp_ea_support(SNUM(conn))) {
+               DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u but "
+                       "EA's not supported.\n",
+                       (unsigned int)total_data));
+               return NT_STATUS_EAS_NOT_SUPPORTED;
+       }
+
+       if (total_data < 10) {
+               DEBUG(10, ("smb_set_file_full_ea_info - ea_len = %u "
+                       "too small.\n",
+                       (unsigned int)total_data));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       ea_list = read_nttrans_ea_list(talloc_tos(),
+                               pdata,
+                               total_data);
+
+       if (!ea_list) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (fsp && !(fsp->access_mask & FILE_WRITE_EA)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       status = set_ea(conn, fsp, fsp->fsp_name, ea_list);
+
+       DEBUG(10, ("smb_set_file_full_ea_info on file %s returned %s\n",
+               smb_fname_str_dbg(fsp->fsp_name),
+               nt_errstr(status) ));
+
+       return status;
+}
+
+
 /****************************************************************************
  Deal with SMB_SET_FILE_DISPOSITION_INFO.
 ****************************************************************************/
@@ -5663,7 +5785,7 @@ static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
                                const char *pdata,
                                int total_data,
                                files_struct *fsp,
-                               const struct smb_filename *smb_fname)
+                               struct smb_filename *smb_fname)
 {
        NTSTATUS status = NT_STATUS_OK;
        bool delete_on_close;
@@ -5686,15 +5808,16 @@ static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
                (unsigned int)dosmode,
                (unsigned int)delete_on_close ));
 
-       status = can_set_delete_on_close(fsp, delete_on_close, dosmode);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (delete_on_close) {
+               status = can_set_delete_on_close(fsp, dosmode);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
        }
 
        /* The set is across all open files on this dev/inode pair. */
        if (!set_delete_on_close(fsp, delete_on_close,
-                                &conn->server_info->utok)) {
+                                &conn->session_info->utok)) {
                return NT_STATUS_ACCESS_DENIED;
        }
        return NT_STATUS_OK;
@@ -5769,7 +5892,6 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
 {
        char *link_target = NULL;
        const char *newname = smb_fname->base_name;
-       NTSTATUS status = NT_STATUS_OK;
        TALLOC_CTX *ctx = talloc_tos();
 
        /* Set a symbolic link. */
@@ -5790,42 +5912,6 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* !widelinks forces the target path to be within the share. */
-       /* This means we can interpret the target as a pathname. */
-       if (!lp_widelinks(SNUM(conn))) {
-               char *rel_name = NULL;
-               char *last_dirp = NULL;
-
-               if (*link_target == '/') {
-                       /* No absolute paths allowed. */
-                       return NT_STATUS_ACCESS_DENIED;
-               }
-               rel_name = talloc_strdup(ctx,newname);
-               if (!rel_name) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-               last_dirp = strrchr_m(rel_name, '/');
-               if (last_dirp) {
-                       last_dirp[1] = '\0';
-               } else {
-                       rel_name = talloc_strdup(ctx,"./");
-                       if (!rel_name) {
-                               return NT_STATUS_NO_MEMORY;
-                       }
-               }
-               rel_name = talloc_asprintf_append(rel_name,
-                               "%s",
-                               link_target);
-               if (!rel_name) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               status = check_name(conn, rel_name);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
-       }
-
        DEBUG(10,("smb_set_file_unix_link: SMB_SET_FILE_UNIX_LINK doing symlink %s -> %s\n",
                        newname, link_target ));
 
@@ -5843,7 +5929,7 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
 static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
                                        struct smb_request *req,
                                        const char *pdata, int total_data,
-                                       const struct smb_filename *smb_fname_new)
+                                       struct smb_filename *smb_fname_new)
 {
        char *oldname = NULL;
        struct smb_filename *smb_fname_old = NULL;
@@ -5875,7 +5961,172 @@ static NTSTATUS smb_set_file_unix_hlink(connection_struct *conn,
                return status;
        }
 
-       return hardlink_internals(ctx, conn, smb_fname_old, smb_fname_new);
+       return hardlink_internals(ctx, conn, req, false,
+                       smb_fname_old, smb_fname_new);
+}
+
+/****************************************************************************
+ Deal with SMB2_FILE_RENAME_INFORMATION_INTERNAL
+****************************************************************************/
+
+static NTSTATUS smb2_file_rename_information(connection_struct *conn,
+                                           struct smb_request *req,
+                                           const char *pdata,
+                                           int total_data,
+                                           files_struct *fsp,
+                                           struct smb_filename *smb_fname_src)
+{
+       bool overwrite;
+       uint32_t len;
+       char *newname = NULL;
+       struct smb_filename *smb_fname_dst = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+       TALLOC_CTX *ctx = talloc_tos();
+
+       if (!fsp) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (total_data < 20) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       overwrite = (CVAL(pdata,0) ? True : False);
+       len = IVAL(pdata,16);
+
+       if (len > (total_data - 20) || (len == 0)) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       srvstr_get_path(ctx, pdata, req->flags2, &newname,
+                               &pdata[20], len, STR_TERMINATE,
+                               &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       DEBUG(10,("smb2_file_rename_information: got name |%s|\n",
+                               newname));
+
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               newname,
+                               UCF_SAVE_LCOMP,
+                               NULL,
+                               &smb_fname_dst);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (fsp->base_fsp) {
+               /* newname must be a stream name. */
+               if (newname[0] != ':') {
+                       return NT_STATUS_NOT_SUPPORTED;
+               }
+
+               /* Create an smb_fname to call rename_internals_fsp() with. */
+               status = create_synthetic_smb_fname(talloc_tos(),
+                   fsp->base_fsp->fsp_name->base_name, newname, NULL,
+                   &smb_fname_dst);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto out;
+               }
+
+               /*
+                * Set the original last component, since
+                * rename_internals_fsp() requires it.
+                */
+               smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
+                                                             newname);
+               if (smb_fname_dst->original_lcomp == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
+
+       }
+
+       DEBUG(10,("smb2_file_rename_information: "
+                 "SMB_FILE_RENAME_INFORMATION (fnum %d) %s -> %s\n",
+                 fsp->fnum, fsp_str_dbg(fsp),
+                 smb_fname_str_dbg(smb_fname_dst)));
+       status = rename_internals_fsp(conn, fsp, smb_fname_dst,
+                               (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM),
+                               overwrite);
+
+ out:
+       TALLOC_FREE(smb_fname_dst);
+       return status;
+}
+
+static NTSTATUS smb_file_link_information(connection_struct *conn,
+                                           struct smb_request *req,
+                                           const char *pdata,
+                                           int total_data,
+                                           files_struct *fsp,
+                                           struct smb_filename *smb_fname_src)
+{
+       bool overwrite;
+       uint32_t len;
+       char *newname = NULL;
+       struct smb_filename *smb_fname_dst = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+       TALLOC_CTX *ctx = talloc_tos();
+
+       if (!fsp) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (total_data < 20) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       overwrite = (CVAL(pdata,0) ? true : false);
+       len = IVAL(pdata,16);
+
+       if (len > (total_data - 20) || (len == 0)) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       srvstr_get_path(ctx, pdata, req->flags2, &newname,
+                               &pdata[20], len, STR_TERMINATE,
+                               &status);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       DEBUG(10,("smb_file_link_information: got name |%s|\n",
+                               newname));
+
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               newname,
+                               UCF_SAVE_LCOMP,
+                               NULL,
+                               &smb_fname_dst);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (fsp->base_fsp) {
+               /* No stream names. */
+               return NT_STATUS_NOT_SUPPORTED;
+       }
+
+       DEBUG(10,("smb_file_link_information: "
+                 "SMB_FILE_LINK_INFORMATION (fnum %d) %s -> %s\n",
+                 fsp->fnum, fsp_str_dbg(fsp),
+                 smb_fname_str_dbg(smb_fname_dst)));
+       status = hardlink_internals(ctx,
+                               conn,
+                               req,
+                               overwrite,
+                               fsp->fsp_name,
+                               smb_fname_dst);
+
+       TALLOC_FREE(smb_fname_dst);
+       return status;
 }
 
 /****************************************************************************
@@ -5924,6 +6175,7 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
        status = resolve_dfspath_wcard(ctx, conn,
                                       req->flags2 & FLAGS2_DFS_PATHNAMES,
                                       newname,
+                                      true,
                                       &newname,
                                       &dest_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
@@ -5985,7 +6237,7 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                if (p) {
                        p[1] = '\0';
                } else {
-                       base_name = talloc_strdup(ctx, "./");
+                       base_name = talloc_strdup(ctx, "");
                        if (!base_name) {
                                return NT_STATUS_NO_MEMORY;
                        }
@@ -6121,7 +6373,7 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
 {
        uint64_t count;
        uint64_t offset;
-       uint32 lock_pid;
+       uint64_t smblctx;
        bool blocking_lock = False;
        enum brl_type lock_type;
 
@@ -6165,7 +6417,7 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
                blocking_lock = False;
        }
 
-       lock_pid = IVAL(pdata, POSIX_LOCK_PID_OFFSET);
+       smblctx = (uint64_t)IVAL(pdata, POSIX_LOCK_PID_OFFSET);
 #if defined(HAVE_LONGLONG)
        offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
                        ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
@@ -6177,33 +6429,33 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
 #endif /* HAVE_LONGLONG */
 
        DEBUG(10,("smb_set_posix_lock: file %s, lock_type = %u,"
-                       "lock_pid = %u, count = %.0f, offset = %.0f\n",
+                       "smblctx = %llu, count = %.0f, offset = %.0f\n",
                fsp_str_dbg(fsp),
                (unsigned int)lock_type,
-               (unsigned int)lock_pid,
+               (unsigned long long)smblctx,
                (double)count,
                (double)offset ));
 
        if (lock_type == UNLOCK_LOCK) {
-               status = do_unlock(smbd_messaging_context(),
+               status = do_unlock(req->sconn->msg_ctx,
                                fsp,
-                               lock_pid,
+                               smblctx,
                                count,
                                offset,
                                POSIX_LOCK);
        } else {
-               uint32 block_smbpid;
+               uint64_t block_smblctx;
 
-               struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
+               struct byte_range_lock *br_lck = do_lock(req->sconn->msg_ctx,
                                                        fsp,
-                                                       lock_pid,
+                                                       smblctx,
                                                        count,
                                                        offset,
                                                        lock_type,
                                                        POSIX_LOCK,
                                                        blocking_lock,
                                                        &status,
-                                                       &block_smbpid,
+                                                       &block_smblctx,
                                                        NULL);
 
                if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
@@ -6217,12 +6469,12 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
                                                fsp,
                                                -1, /* infinite timeout. */
                                                0,
-                                               lock_pid,
+                                               smblctx,
                                                lock_type,
                                                POSIX_LOCK,
                                                offset,
                                                count,
-                                               block_smbpid)) {
+                                               block_smblctx)) {
                                TALLOC_FREE(br_lck);
                                return status;
                        }
@@ -6254,6 +6506,10 @@ static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       if (fsp && !(fsp->access_mask & FILE_WRITE_ATTRIBUTES)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        /* Set the attributes */
        dosmode = IVAL(pdata,32);
        status = smb_set_file_dosmode(conn, smb_fname, dosmode);
@@ -6298,6 +6554,10 @@ static NTSTATUS smb_set_info_standard(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       if (fsp && !(fsp->access_mask & FILE_WRITE_ATTRIBUTES)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        /* create time */
        ft.create_time = convert_time_t_to_timespec(srv_make_unix_date2(pdata));
        /* access time */
@@ -6356,6 +6616,10 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
                allocation_size = smb_roundup(conn, allocation_size);
        }
 
+       if (fsp && !(fsp->access_mask & FILE_WRITE_DATA)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        DEBUG(10,("smb_set_file_allocation_info: file %s : setting new "
                  "allocation size to %.0f\n", smb_fname_str_dbg(smb_fname),
                  (double)allocation_size));
@@ -6391,6 +6655,7 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
                FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
                FORCE_OPLOCK_BREAK_TO_NONE,             /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &new_fsp,                               /* result */
@@ -6430,7 +6695,8 @@ static NTSTATUS smb_set_file_end_of_file_info(connection_struct *conn,
                                        const char *pdata,
                                        int total_data,
                                        files_struct *fsp,
-                                       const struct smb_filename *smb_fname)
+                                       const struct smb_filename *smb_fname,
+                                       bool fail_after_createfile)
 {
        SMB_OFF_T size;
 
@@ -6451,11 +6717,16 @@ static NTSTATUS smb_set_file_end_of_file_info(connection_struct *conn,
                  "file %s to %.0f\n", smb_fname_str_dbg(smb_fname),
                  (double)size));
 
+       if (fsp && !(fsp->access_mask & FILE_WRITE_DATA)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        return smb_set_file_size(conn, req,
                                fsp,
                                smb_fname,
                                &smb_fname->st,
-                               size);
+                               size,
+                               fail_after_createfile);
 }
 
 /****************************************************************************
@@ -6566,6 +6837,7 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        files_struct *all_fsps = NULL;
        bool modify_mtime = true;
        struct file_id id;
+       struct smb_filename *smb_fname_tmp = NULL;
        SMB_STRUCT_STAT sbuf;
 
        ZERO_STRUCT(ft);
@@ -6618,7 +6890,6 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        sbuf = smb_fname->st;
 
        if (!VALID_STAT(sbuf)) {
-               struct smb_filename *smb_fname_tmp = NULL;
                /*
                 * The only valid use of this is to create character and block
                 * devices, and named pipes. This is deprecated (IMHO) and 
@@ -6647,7 +6918,7 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
                }
 
                sbuf = smb_fname_tmp->st;
-               TALLOC_FREE(smb_fname_tmp);
+               smb_fname = smb_fname_tmp;
 
                /* Ensure we don't try and change anything else. */
                raw_unixmode = SMB_MODE_NO_CHANGE;
@@ -6741,7 +7012,8 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
                                   fsp,
                                   smb_fname,
                                   &sbuf,
-                                  size);
+                                  size,
+                                  false);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -6753,7 +7025,7 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        }
 
        id = vfs_file_id_from_sbuf(conn, &sbuf);
-       for(all_fsps = file_find_di_first(id); all_fsps;
+       for(all_fsps = file_find_di_first(conn->sconn, id); all_fsps;
                        all_fsps = file_find_di_next(all_fsps)) {
                /*
                 * We're setting the time explicitly for UNIX.
@@ -6897,6 +7169,7 @@ static NTSTATUS smb_posix_mkdir(connection_struct *conn,
                mod_unixmode,                           /* file_attributes */
                0,                                      /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
@@ -6954,6 +7227,9 @@ static NTSTATUS smb_posix_mkdir(connection_struct *conn,
  Open/Create a file with POSIX semantics.
 ****************************************************************************/
 
+#define SMB_O_RDONLY_MAPPING (FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA)
+#define SMB_O_WRONLY_MAPPING (FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA)
+
 static NTSTATUS smb_posix_open(connection_struct *conn,
                               struct smb_request *req,
                                char **ppdata,
@@ -6969,7 +7245,7 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
        uint32 mod_unixmode = 0;
        uint32 create_disp = 0;
        uint32 access_mask = 0;
-       uint32 create_options = 0;
+       uint32 create_options = FILE_NON_DIRECTORY_FILE;
        NTSTATUS status = NT_STATUS_OK;
        mode_t unixmode = (mode_t)0;
        files_struct *fsp = NULL;
@@ -6999,13 +7275,14 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
 
        switch (wire_open_mode & SMB_ACCMODE) {
                case SMB_O_RDONLY:
-                       access_mask = FILE_READ_DATA;
+                       access_mask = SMB_O_RDONLY_MAPPING;
                        break;
                case SMB_O_WRONLY:
-                       access_mask = FILE_WRITE_DATA;
+                       access_mask = SMB_O_WRONLY_MAPPING;
                        break;
                case SMB_O_RDWR:
-                       access_mask = FILE_READ_DATA|FILE_WRITE_DATA;
+                       access_mask = (SMB_O_RDONLY_MAPPING|
+                                       SMB_O_WRONLY_MAPPING);
                        break;
                default:
                        DEBUG(5,("smb_posix_open: invalid open mode 0x%x\n",
@@ -7015,18 +7292,55 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
 
        wire_open_mode &= ~SMB_ACCMODE;
 
-       if((wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL)) == (SMB_O_CREAT | SMB_O_EXCL)) {
-               create_disp = FILE_CREATE;
-       } else if((wire_open_mode & (SMB_O_CREAT | SMB_O_TRUNC)) == (SMB_O_CREAT | SMB_O_TRUNC)) {
-               create_disp = FILE_OVERWRITE_IF;
-       } else if((wire_open_mode & SMB_O_CREAT) == SMB_O_CREAT) {
-               create_disp = FILE_OPEN_IF;
-       } else if ((wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL | SMB_O_TRUNC)) == 0) {
-               create_disp = FILE_OPEN;
-       } else {
-               DEBUG(5,("smb_posix_open: invalid create mode 0x%x\n",
-                       (unsigned int)wire_open_mode ));
-               return NT_STATUS_INVALID_PARAMETER;
+       /* First take care of O_CREAT|O_EXCL interactions. */
+       switch (wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL)) {
+               case (SMB_O_CREAT | SMB_O_EXCL):
+                       /* File exists fail. File not exist create. */
+                       create_disp = FILE_CREATE;
+                       break;
+               case SMB_O_CREAT:
+                       /* File exists open. File not exist create. */
+                       create_disp = FILE_OPEN_IF;
+                       break;
+               case 0:
+                       /* File exists open. File not exist fail. */
+                       create_disp = FILE_OPEN;
+                       break;
+               case SMB_O_EXCL:
+                       /* O_EXCL on its own without O_CREAT is undefined. */
+               default:
+                       DEBUG(5,("smb_posix_open: invalid create mode 0x%x\n",
+                               (unsigned int)wire_open_mode ));
+                       return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       /* Next factor in the effects of O_TRUNC. */
+       wire_open_mode &= ~(SMB_O_CREAT | SMB_O_EXCL);
+
+       if (wire_open_mode & SMB_O_TRUNC) {
+               switch (create_disp) {
+                       case FILE_CREATE:
+                               /* (SMB_O_CREAT | SMB_O_EXCL | O_TRUNC) */
+                               /* Leave create_disp alone as
+                                  (O_CREAT|O_EXCL|O_TRUNC) == (O_CREAT|O_EXCL)
+                               */
+                               /* File exists fail. File not exist create. */
+                               break;
+                       case FILE_OPEN_IF:
+                               /* SMB_O_CREAT | SMB_O_TRUNC */
+                               /* File exists overwrite. File not exist create. */
+                               create_disp = FILE_OVERWRITE_IF;
+                               break;
+                       case FILE_OPEN:
+                               /* SMB_O_TRUNC */
+                               /* File exists overwrite. File not exist fail. */
+                               create_disp = FILE_OVERWRITE;
+                               break;
+                       default:
+                               /* Cannot get here. */
+                               smb_panic("smb_posix_open: logic error");
+                               return NT_STATUS_INVALID_PARAMETER;
+               }
        }
 
        raw_unixmode = IVAL(pdata,8);
@@ -7053,6 +7367,15 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
                mod_unixmode |= FILE_FLAG_NO_BUFFERING;
        }
 
+       if ((wire_open_mode & SMB_O_DIRECTORY) ||
+                       VALID_STAT_OF_DIR(smb_fname->st)) {
+               if (access_mask != SMB_O_RDONLY_MAPPING) {
+                       return NT_STATUS_FILE_IS_A_DIRECTORY;
+               }
+               create_options &= ~FILE_NON_DIRECTORY_FILE;
+               create_options |= FILE_DIRECTORY_FILE;
+       }
+
        DEBUG(10,("smb_posix_open: file %s, smb_posix_flags = %u, mode 0%o\n",
                smb_fname_str_dbg(smb_fname),
                (unsigned int)wire_open_mode,
@@ -7067,10 +7390,11 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
                (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
                    FILE_SHARE_DELETE),
                create_disp,                            /* create_disposition*/
-               FILE_NON_DIRECTORY_FILE,                /* create_options */
+               create_options,                         /* create_options */
                mod_unixmode,                           /* file_attributes */
                oplock_request,                         /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
@@ -7200,6 +7524,7 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
                FILE_FLAG_POSIX_SEMANTICS|0777,         /* file_attributes */
                0,                                      /* oplock_request */
                0,                                      /* allocation_size */
+               0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
@@ -7337,11 +7662,22 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                case SMB_FILE_END_OF_FILE_INFORMATION:
                case SMB_SET_FILE_END_OF_FILE_INFO:
                {
+                       /*
+                        * XP/Win7 both fail after the createfile with
+                        * SMB_SET_FILE_END_OF_FILE_INFO but not
+                        * SMB_FILE_END_OF_FILE_INFORMATION (pass-through).
+                        * The level is known here, so pass it down
+                        * appropriately.
+                        */
+                       bool should_fail =
+                           (info_level == SMB_SET_FILE_END_OF_FILE_INFO);
+
                        status = smb_set_file_end_of_file_info(conn, req,
                                                                pdata,
                                                                total_data,
                                                                fsp,
-                                                               smb_fname);
+                                                               smb_fname,
+                                                               should_fail);
                        break;
                }
 
@@ -7374,6 +7710,15 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                        break;
                }
 
+               case SMB_FILE_FULL_EA_INFORMATION:
+               {
+                       status = smb_set_file_full_ea_info(conn,
+                                               pdata,
+                                               total_data,
+                                               fsp);
+                       break;
+               }
+
                /* From tridge Samba4 : 
                 * MODE_INFORMATION in setfileinfo (I have no
                 * idea what "mode information" on a file is - it takes a value of 0,
@@ -7443,6 +7788,23 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                        break;
                }
 
+               case SMB2_FILE_RENAME_INFORMATION_INTERNAL:
+               {
+                       /* SMB2 rename information. */
+                       status = smb2_file_rename_information(conn, req,
+                                                            pdata, total_data,
+                                                            fsp, smb_fname);
+                       break;
+               }
+
+               case SMB_FILE_LINK_INFORMATION:
+               {
+                       status = smb_file_link_information(conn, req,
+                                                       pdata, total_data,
+                                                       fsp, smb_fname);
+                       break;
+               }
+
 #if defined(HAVE_POSIX_ACLS)
                case SMB_SET_POSIX_ACL:
                {
@@ -7550,7 +7912,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               if(fsp->is_directory || fsp->fh->fd == -1) {
+               if(fsp->fh->fd == -1) {
                        /*
                         * This is actually a SETFILEINFO on a directory
                         * handle (returned from an NT SMB). NT5.0 seems
@@ -7582,7 +7944,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                         * Doing a DELETE_ON_CLOSE should cancel a print job.
                         */
                        if ((info_level == SMB_SET_FILE_DISPOSITION_INFO) && CVAL(pdata,0)) {
-                               fsp->fh->private_options |= FILE_DELETE_ON_CLOSE;
+                               fsp->fh->private_options |= NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE;
 
                                DEBUG(3,("call_trans2setfilepathinfo: "
                                         "Cancelling print job (%s)\n",
@@ -7594,17 +7956,14 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                                                    max_data_bytes);
                                return;
                        } else {
-                               reply_doserror(req, ERRDOS, ERRbadpath);
+                               reply_nterror(req,
+                                       NT_STATUS_OBJECT_PATH_NOT_FOUND);
                                return;
                        }
                } else {
                        /*
                         * Original code - this is an open file.
                         */
-                       if (!check_fsp(conn, req, fsp)) {
-                               return;
-                       }
-
                        if (SMB_VFS_FSTAT(fsp, &smb_fname->st) != 0) {
                                DEBUG(3,("call_trans2setfilepathinfo: fstat "
                                         "of fnum %d failed (%s)\n", fsp->fnum,
@@ -7692,7 +8051,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        /* We have re-scheduled this call. */
                        return;
                }
-               if (blocking_lock_was_deferred(req->mid)) {
+               if (blocking_lock_was_deferred_smb1(req->sconn, req->mid)) {
                        /* We have re-scheduled this call. */
                        return;
                }
@@ -7734,7 +8093,7 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
        TALLOC_CTX *ctx = talloc_tos();
 
        if (!CAN_WRITE(conn)) {
-               reply_doserror(req, ERRSRV, ERRaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return;
        }
 
@@ -7772,19 +8131,14 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                return;
         }
 
-       /* Any data in this call is an EA list. */
-       if (total_data && (total_data != 4) && !lp_ea_support(SNUM(conn))) {
-               reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
-               goto out;
-       }
-
        /*
         * OS/2 workplace shell seems to send SET_EA requests of "null"
         * length (4 bytes containing IVAL 4).
         * They seem to have no effect. Bug #3212. JRA.
         */
 
-       if (total_data != 4) {
+       if (total_data && (total_data != 4)) {
+               /* Any data in this call is an EA list. */
                if (total_data < 10) {
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
@@ -7803,6 +8157,11 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        goto out;
                }
+
+               if (!lp_ea_support(SNUM(conn))) {
+                       reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
+                       goto out;
+               }
        }
        /* If total_data == 4 Windows doesn't care what values
         * are placed in that field, it just ignores them.
@@ -7953,7 +8312,7 @@ static void call_trans2getdfsreferral(connection_struct *conn,
        max_referral_level = SVAL(params,0);
 
        if(!lp_host_msdfs()) {
-               reply_doserror(req, ERRDOS, ERRbadfunc);
+               reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
                return;
        }
 
@@ -7969,7 +8328,7 @@ static void call_trans2getdfsreferral(connection_struct *conn,
                return;
        }
 
-       SSVAL(req->inbuf, smb_flg2,
+       SSVAL((discard_const_p(uint8_t, req->inbuf)), smb_flg2,
              SVAL(req->inbuf,smb_flg2) | FLAGS2_DFS_PATHNAMES);
        send_trans2_replies(conn, req,0,0,*ppdata,reply_size, max_data_bytes);
 
@@ -7995,7 +8354,7 @@ static void call_trans2ioctl(connection_struct *conn,
        /* check for an invalid fid before proceeding */
 
        if (!fsp) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
 
@@ -8011,7 +8370,12 @@ static void call_trans2ioctl(connection_struct *conn,
                /* NOTE - THIS IS ASCII ONLY AT THE MOMENT - NOT SURE IF OS/2
                        CAN ACCEPT THIS IN UNICODE. JRA. */
 
-               SSVAL(pdata,0,fsp->rap_print_jobid);                     /* Job number */
+               /* Job number */
+               if (fsp->print_file) {
+                       SSVAL(pdata, 0, fsp->print_file->rap_jobid);
+               } else {
+                       SSVAL(pdata, 0, 0);
+               }
                srvstr_push(pdata, req->flags2, pdata + 2,
                            global_myname(), 15,
                            STR_ASCII|STR_TERMINATE); /* Our NetBIOS name */
@@ -8024,7 +8388,7 @@ static void call_trans2ioctl(connection_struct *conn,
        }
 
        DEBUG(2,("Unknown TRANS2_IOCTL\n"));
-       reply_doserror(req, ERRSRV, ERRerror);
+       reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
 }
 
 /****************************************************************************
@@ -8034,7 +8398,7 @@ static void call_trans2ioctl(connection_struct *conn,
 void reply_findclose(struct smb_request *req)
 {
        int dptr_num;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBfindclose);
 
@@ -8093,9 +8457,9 @@ void reply_findnclose(struct smb_request *req)
 static void handle_trans2(connection_struct *conn, struct smb_request *req,
                          struct trans_state *state)
 {
-       if (Protocol >= PROTOCOL_NT1) {
+       if (get_Protocol() >= PROTOCOL_NT1) {
                req->flags2 |= 0x40; /* IS_LONG_NAME */
-               SSVAL(req->inbuf,smb_flg2,req->flags2);
+               SSVAL((discard_const_p(uint8_t, req->inbuf)),smb_flg2,req->flags2);
        }
 
        if (conn->encrypt_level == Required && !req->encrypted) {
@@ -8250,7 +8614,7 @@ static void handle_trans2(connection_struct *conn, struct smb_request *req,
        default:
                /* Error in request */
                DEBUG(2,("Unknown request %d in trans2 call\n", state->call));
-               reply_doserror(req, ERRSRV,ERRerror);
+               reply_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
        }
 }
 
@@ -8302,7 +8666,7 @@ void reply_trans2(struct smb_request *req)
                case TRANSACT2_SETFSINFO:
                        break;
                default:
-                       reply_doserror(req, ERRSRV, ERRaccess);
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBtrans2);
                        return;
                }
@@ -8454,7 +8818,7 @@ void reply_transs2(struct smb_request *req)
 
        START_PROFILE(SMBtranss2);
 
-       show_msg((char *)req->inbuf);
+       show_msg((const char *)req->inbuf);
 
        if (req->wct < 8) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);