s3/vfs: rename SMB_VFS_STRICT_LOCK to SMB_VFS_STRICT_LOCK_CHECK
[samba.git] / source3 / smbd / reply.c
index 0b6c1024481fc31f7a173cfc338489eb678ca69c..317143f912cd0878deef31a9f9147fe1efc6bfd5 100644 (file)
@@ -43,7 +43,7 @@
 #include "../lib/tsocket/tsocket.h"
 #include "lib/tevent_wait.h"
 #include "libcli/smb/smb_signing.h"
-#include "lib/sys_rw_data.h"
+#include "lib/util/sys_rw_data.h"
 
 /****************************************************************************
  Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
@@ -253,15 +253,17 @@ NTSTATUS check_path_syntax_posix(char *path)
 
 /****************************************************************************
  Pull a string and check the path allowing a wilcard - provide for error return.
+ Passes in posix flag.
 ****************************************************************************/
 
-size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
+static size_t srvstr_get_path_wcard_internal(TALLOC_CTX *ctx,
                        const char *base_ptr,
-                       uint16 smb_flags2,
+                       uint16_t smb_flags2,
                        char **pp_dest,
                        const char *src,
                        size_t src_len,
                        int flags,
+                       bool posix_pathnames,
                        NTSTATUS *err,
                        bool *contains_wcard)
 {
@@ -288,7 +290,7 @@ size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
                return ret;
        }
 
-       if (lp_posix_pathnames()) {
+       if (posix_pathnames) {
                *err = check_path_syntax_posix(*pp_dest);
        } else {
                *err = check_path_syntax_wcard(*pp_dest, contains_wcard);
@@ -297,13 +299,66 @@ size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
        return ret;
 }
 
+/****************************************************************************
+ Pull a string and check the path allowing a wilcard - provide for error return.
+****************************************************************************/
+
+size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
+                       const char *base_ptr,
+                       uint16_t smb_flags2,
+                       char **pp_dest,
+                       const char *src,
+                       size_t src_len,
+                       int flags,
+                       NTSTATUS *err,
+                       bool *contains_wcard)
+{
+       return srvstr_get_path_wcard_internal(ctx,
+                       base_ptr,
+                       smb_flags2,
+                       pp_dest,
+                       src,
+                       src_len,
+                       flags,
+                       false,
+                       err,
+                       contains_wcard);
+}
+
+/****************************************************************************
+ Pull a string and check the path allowing a wilcard - provide for error return.
+ posix_pathnames version.
+****************************************************************************/
+
+size_t srvstr_get_path_wcard_posix(TALLOC_CTX *ctx,
+                       const char *base_ptr,
+                       uint16_t smb_flags2,
+                       char **pp_dest,
+                       const char *src,
+                       size_t src_len,
+                       int flags,
+                       NTSTATUS *err,
+                       bool *contains_wcard)
+{
+       return srvstr_get_path_wcard_internal(ctx,
+                       base_ptr,
+                       smb_flags2,
+                       pp_dest,
+                       src,
+                       src_len,
+                       flags,
+                       true,
+                       err,
+                       contains_wcard);
+}
+
 /****************************************************************************
  Pull a string and check the path - provide for error return.
 ****************************************************************************/
 
 size_t srvstr_get_path(TALLOC_CTX *ctx,
                        const char *base_ptr,
-                       uint16 smb_flags2,
+                       uint16_t smb_flags2,
                        char **pp_dest,
                        const char *src,
                        size_t src_len,
@@ -311,10 +366,46 @@ size_t srvstr_get_path(TALLOC_CTX *ctx,
                        NTSTATUS *err)
 {
        bool ignore;
-       return srvstr_get_path_wcard(ctx, base_ptr, smb_flags2, pp_dest, src,
-                                    src_len, flags, err, &ignore);
+       return srvstr_get_path_wcard_internal(ctx,
+                       base_ptr,
+                       smb_flags2,
+                       pp_dest,
+                       src,
+                       src_len,
+                       flags,
+                       false,
+                       err,
+                       &ignore);
+}
+
+/****************************************************************************
+ Pull a string and check the path - provide for error return.
+ posix_pathnames version.
+****************************************************************************/
+
+size_t srvstr_get_path_posix(TALLOC_CTX *ctx,
+                       const char *base_ptr,
+                       uint16_t smb_flags2,
+                       char **pp_dest,
+                       const char *src,
+                       size_t src_len,
+                       int flags,
+                       NTSTATUS *err)
+{
+       bool ignore;
+       return srvstr_get_path_wcard_internal(ctx,
+                       base_ptr,
+                       smb_flags2,
+                       pp_dest,
+                       src,
+                       src_len,
+                       flags,
+                       true,
+                       err,
+                       &ignore);
 }
 
+
 size_t srvstr_get_path_req_wcard(TALLOC_CTX *mem_ctx, struct smb_request *req,
                                 char **pp_dest, const char *src, int flags,
                                 NTSTATUS *err, bool *contains_wcard)
@@ -326,9 +417,29 @@ size_t srvstr_get_path_req_wcard(TALLOC_CTX *mem_ctx, struct smb_request *req,
                return 0;
        }
 
-       return srvstr_get_path_wcard(mem_ctx, (const char *)req->inbuf,
-                                    req->flags2, pp_dest, src, bufrem, flags,
-                                    err, contains_wcard);
+       if (req->posix_pathnames) {
+               return srvstr_get_path_wcard_internal(mem_ctx,
+                               (const char *)req->inbuf,
+                               req->flags2,
+                               pp_dest,
+                               src,
+                               bufrem,
+                               flags,
+                               true,
+                               err,
+                               contains_wcard);
+       } else {
+               return srvstr_get_path_wcard_internal(mem_ctx,
+                               (const char *)req->inbuf,
+                               req->flags2,
+                               pp_dest,
+                               src,
+                               bufrem,
+                               flags,
+                               false,
+                               err,
+                               contains_wcard);
+       }
 }
 
 size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
@@ -668,7 +779,9 @@ void reply_special(struct smbXsrv_connection *xconn, char *inbuf, size_t inbuf_s
        DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
                    msg_type, msg_flags));
 
-       srv_send_smb(xconn, outbuf, false, 0, false, NULL);
+       if (!srv_send_smb(xconn, outbuf, false, 0, false, NULL)) {
+               exit_server_cleanly("reply_special: srv_send_smb failed.");
+       }
 
        if (CVAL(outbuf, 0) != 0x82) {
                exit_server_cleanly("invalid netbios session");
@@ -756,7 +869,7 @@ void reply_tcon_and_X(struct smb_request *req)
        connection_struct *conn = req->conn;
        const char *service = NULL;
        TALLOC_CTX *ctx = talloc_tos();
-       /* what the cleint thinks the device is */
+       /* what the client thinks the device is */
        char *client_devicetype = NULL;
        /* what the server tells the client the share represents */
        const char *server_devicetype;
@@ -979,8 +1092,8 @@ void reply_tcon_and_X(struct smb_request *req)
 
                if (tcon_flags & TCONX_FLAG_EXTENDED_RESPONSE) {
                        /* Return permissions. */
-                       uint32 perm1 = 0;
-                       uint32 perm2 = 0;
+                       uint32_t perm1 = 0;
+                       uint32_t perm2 = 0;
 
                        reply_outbuf(req, 7, 0);
 
@@ -1040,7 +1153,7 @@ void reply_tcon_and_X(struct smb_request *req)
  Reply to an unknown type.
 ****************************************************************************/
 
-void reply_unknown_new(struct smb_request *req, uint8 type)
+void reply_unknown_new(struct smb_request *req, uint8_t type)
 {
        DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
                  smb_fn_name(type), type, type));
@@ -1056,9 +1169,9 @@ void reply_unknown_new(struct smb_request *req, uint8 type)
 void reply_ioctl(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       uint16 device;
-       uint16 function;
-       uint32 ioctl_code;
+       uint16_t device;
+       uint16_t function;
+       uint32_t ioctl_code;
        int replysize;
        char *p;
 
@@ -1165,6 +1278,7 @@ void reply_checkpath(struct smb_request *req)
        struct smb_filename *smb_fname = NULL;
        char *name = NULL;
        NTSTATUS status;
+       uint32_t ucf_flags = ucf_flags_from_smb_request(req);
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBcheckpath);
@@ -1183,9 +1297,8 @@ void reply_checkpath(struct smb_request *req)
 
        status = filename_convert(ctx,
                                conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
                                name,
-                               0,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
 
@@ -1279,11 +1392,11 @@ void reply_getatr(struct smb_request *req)
                size = 0;
                mtime = 0;
        } else {
+               uint32_t ucf_flags = ucf_flags_from_smb_request(req);
                status = filename_convert(ctx,
                                conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               0,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1333,7 +1446,7 @@ void reply_getatr(struct smb_request *req)
        } else {
                srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime);
        }
-       SIVAL(req->outbuf,smb_vwv3,(uint32)size);
+       SIVAL(req->outbuf,smb_vwv3,(uint32_t)size);
 
        if (get_Protocol() >= PROTOCOL_NT1) {
                SSVAL(req->outbuf, smb_flg2,
@@ -1364,6 +1477,7 @@ void reply_setatr(struct smb_request *req)
        time_t mtime;
        const char *p;
        NTSTATUS status;
+       uint32_t ucf_flags = ucf_flags_from_smb_request(req);
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBsetatr);
@@ -1384,9 +1498,8 @@ void reply_setatr(struct smb_request *req)
 
        status = filename_convert(ctx,
                                conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               0,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1456,15 +1569,45 @@ void reply_setatr(struct smb_request *req)
 void reply_dskattr(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       uint64_t ret;
        uint64_t dfree,dsize,bsize;
+       struct smb_filename smb_fname;
        START_PROFILE(SMBdskattr);
 
-       if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (uint64_t)-1) {
+       ZERO_STRUCT(smb_fname);
+       smb_fname.base_name = discard_const_p(char, ".");
+
+       if (SMB_VFS_STAT(conn, &smb_fname) != 0) {
+               reply_nterror(req, map_nt_error_from_unix(errno));
+               DBG_WARNING("stat of . failed (%s)\n", strerror(errno));
+               END_PROFILE(SMBdskattr);
+               return;
+       }
+
+       ret = get_dfree_info(conn, &smb_fname, &bsize, &dfree, &dsize);
+       if (ret == (uint64_t)-1) {
                reply_nterror(req, map_nt_error_from_unix(errno));
                END_PROFILE(SMBdskattr);
                return;
        }
 
+       /*
+        * Force max to fit in 16 bit fields.
+        */
+       while (dfree > WORDMAX || dsize > WORDMAX || bsize < 512) {
+               dfree /= 2;
+               dsize /= 2;
+               bsize *= 2;
+               if (bsize > (WORDMAX*512)) {
+                       bsize = (WORDMAX*512);
+                       if (dsize > WORDMAX)
+                               dsize = WORDMAX;
+                       if (dfree >  WORDMAX)
+                               dfree = WORDMAX;
+                       break;
+               }
+       }
+
        reply_outbuf(req, 5, 0);
 
        if (get_Protocol() <= PROTOCOL_LANMAN2) {
@@ -1542,7 +1685,7 @@ static bool make_dir_struct(TALLOC_CTX *ctx,
                            const char *mask,
                            const char *fname,
                            off_t size,
-                           uint32 mode,
+                           uint32_t mode,
                            time_t date,
                            bool uc)
 {
@@ -1588,14 +1731,14 @@ void reply_search(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
        char *path = NULL;
-       const char *mask = NULL;
+       char *mask = NULL;
        char *directory = NULL;
        struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
        off_t size;
-       uint32 mode;
+       uint32_t mode;
        struct timespec date;
-       uint32 dirtype;
+       uint32_t dirtype;
        unsigned int numentries = 0;
        unsigned int maxentries = 0;
        bool finished = False;
@@ -1621,7 +1764,7 @@ void reply_search(struct smb_request *req)
                goto out;
        }
 
-       if (lp_posix_pathnames()) {
+       if (req->posix_pathnames) {
                reply_unknown_new(req, req->cmd);
                goto out;
        }
@@ -1649,10 +1792,12 @@ void reply_search(struct smb_request *req)
        /* dirtype &= ~FILE_ATTRIBUTE_DIRECTORY; */
 
        if (status_len == 0) {
+               struct smb_filename *smb_dname = NULL;
+               uint32_t ucf_flags = UCF_ALWAYS_ALLOW_WCARD_LCOMP |
+                       ucf_flags_from_smb_request(req);
                nt_status = filename_convert(ctx, conn,
-                                            req->flags2 & FLAGS2_DFS_PATHNAMES,
                                             path,
-                                            UCF_ALWAYS_ALLOW_WCARD_LCOMP,
+                                            ucf_flags,
                                             &mask_contains_wcard,
                                             &smb_fname);
                if (!NT_STATUS_IS_OK(nt_status)) {
@@ -1669,11 +1814,11 @@ void reply_search(struct smb_request *req)
 
                p = strrchr_m(directory,'/');
                if ((p != NULL) && (*directory != '/')) {
-                       mask = p + 1;
+                       mask = talloc_strdup(ctx, p + 1);
                        directory = talloc_strndup(ctx, directory,
                                                   PTR_DIFF(p, directory));
                } else {
-                       mask = directory;
+                       mask = talloc_strdup(ctx, directory);
                        directory = talloc_strdup(ctx,".");
                }
 
@@ -1685,10 +1830,20 @@ void reply_search(struct smb_request *req)
                memset((char *)status,'\0',21);
                SCVAL(status,0,(dirtype & 0x1F));
 
+               smb_dname = synthetic_smb_fname(talloc_tos(),
+                                       directory,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+               if (smb_dname == NULL) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       goto out;
+               }
+
                nt_status = dptr_create(conn,
                                        NULL, /* req */
                                        NULL, /* fsp */
-                                       directory,
+                                       smb_dname,
                                        True,
                                        expect_close,
                                        req->smbpid,
@@ -1696,6 +1851,9 @@ void reply_search(struct smb_request *req)
                                        mask_contains_wcard,
                                        dirtype,
                                        &dirptr);
+
+               TALLOC_FREE(smb_dname);
+
                if (!NT_STATUS_IS_OK(nt_status)) {
                        reply_nterror(req, nt_status);
                        goto out;
@@ -1722,7 +1880,7 @@ void reply_search(struct smb_request *req)
                        goto out;
                }
 
-               mask = dptr_wcard(sconn, dptr_num);
+               mask = talloc_strdup(ctx, dptr_wcard(sconn, dptr_num));
                if (!mask) {
                        goto SearchEmpty;
                }
@@ -1730,7 +1888,9 @@ void reply_search(struct smb_request *req)
                 * For a 'continue' search we have no string. So
                 * check from the initial saved string.
                 */
-               mask_contains_wcard = ms_has_wild(mask);
+               if (!req->posix_pathnames) {
+                       mask_contains_wcard = ms_has_wild(mask);
+               }
                dirtype = dptr_attr(sconn, dptr_num);
        }
 
@@ -1861,6 +2021,7 @@ void reply_search(struct smb_request *req)
                maxentries ));
  out:
        TALLOC_FREE(directory);
+       TALLOC_FREE(mask);
        TALLOC_FREE(smb_fname);
        END_PROFILE(SMBsearch);
        return;
@@ -1884,7 +2045,7 @@ void reply_fclose(struct smb_request *req)
 
        START_PROFILE(SMBfclose);
 
-       if (lp_posix_pathnames()) {
+       if (req->posix_pathnames) {
                reply_unknown_new(req, req->cmd);
                END_PROFILE(SMBfclose);
                return;
@@ -1933,20 +2094,21 @@ void reply_open(struct smb_request *req)
        connection_struct *conn = req->conn;
        struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
-       uint32 fattr=0;
+       uint32_t fattr=0;
        off_t size = 0;
        time_t mtime=0;
        int info;
        files_struct *fsp;
        int oplock_request;
        int deny_mode;
-       uint32 dos_attr;
-       uint32 access_mask;
-       uint32 share_mode;
-       uint32 create_disposition;
-       uint32 create_options = 0;
+       uint32_t dos_attr;
+       uint32_t access_mask;
+       uint32_t share_mode;
+       uint32_t create_disposition;
+       uint32_t create_options = 0;
        uint32_t private_flags = 0;
        NTSTATUS status;
+       uint32_t ucf_flags;
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBopen);
@@ -1975,11 +2137,12 @@ void reply_open(struct smb_request *req)
                goto out;
        }
 
+       ucf_flags = filename_create_ucf_flags(req, create_disposition);
+
        status = filename_convert(ctx,
                                conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               UCF_PREP_CREATEFILE,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2048,7 +2211,7 @@ void reply_open(struct smb_request *req)
        } else {
                srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime);
        }
-       SIVAL(req->outbuf,smb_vwv4,(uint32)size);
+       SIVAL(req->outbuf,smb_vwv4,(uint32_t)size);
        SSVAL(req->outbuf,smb_vwv6,deny_mode);
 
        if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
@@ -2074,9 +2237,9 @@ void reply_open_and_X(struct smb_request *req)
        connection_struct *conn = req->conn;
        struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
-       uint16 open_flags;
+       uint16_t open_flags;
        int deny_mode;
-       uint32 smb_attr;
+       uint32_t smb_attr;
        /* Breakout the oplock request bits so we can set the
                reply bits separately. */
        int ex_oplock_request;
@@ -2084,21 +2247,22 @@ void reply_open_and_X(struct smb_request *req)
        int oplock_request;
 #if 0
        int smb_sattr = SVAL(req->vwv+4, 0);
-       uint32 smb_time = make_unix_date3(req->vwv+6);
+       uint32_t smb_time = make_unix_date3(req->vwv+6);
 #endif
        int smb_ofun;
-       uint32 fattr=0;
+       uint32_t fattr=0;
        int mtime=0;
        int smb_action = 0;
        files_struct *fsp;
        NTSTATUS status;
        uint64_t allocation_size;
        ssize_t retval = -1;
-       uint32 access_mask;
-       uint32 share_mode;
-       uint32 create_disposition;
-       uint32 create_options = 0;
+       uint32_t access_mask;
+       uint32_t share_mode;
+       uint32_t create_disposition;
+       uint32_t create_options = 0;
        uint32_t private_flags = 0;
+       uint32_t ucf_flags;
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBopenX);
@@ -2145,11 +2309,12 @@ void reply_open_and_X(struct smb_request *req)
                goto out;
        }
 
+       ucf_flags = filename_create_ucf_flags(req, create_disposition);
+
        status = filename_convert(ctx,
                                conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               UCF_PREP_CREATEFILE,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2267,7 +2432,7 @@ void reply_open_and_X(struct smb_request *req)
        } else {
                srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
        }
-       SIVAL(req->outbuf,smb_vwv6,(uint32)fsp->fsp_name->st.st_ex_size);
+       SIVAL(req->outbuf,smb_vwv6,(uint32_t)fsp->fsp_name->st.st_ex_size);
        SSVAL(req->outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
        SSVAL(req->outbuf,smb_vwv11,smb_action);
 
@@ -2348,15 +2513,16 @@ void reply_mknew(struct smb_request *req)
        connection_struct *conn = req->conn;
        struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
-       uint32 fattr = 0;
+       uint32_t fattr = 0;
        struct smb_file_time ft;
        files_struct *fsp;
        int oplock_request = 0;
        NTSTATUS status;
-       uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
-       uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
-       uint32 create_disposition;
-       uint32 create_options = 0;
+       uint32_t access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
+       uint32_t share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
+       uint32_t create_disposition;
+       uint32_t create_options = 0;
+       uint32_t ucf_flags;
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBcreate);
@@ -2370,6 +2536,14 @@ void reply_mknew(struct smb_request *req)
        fattr = SVAL(req->vwv+0, 0);
        oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
 
+       if (req->cmd == SMBmknew) {
+               /* We should fail if file exists. */
+               create_disposition = FILE_CREATE;
+       } else {
+               /* Create if file doesn't exist, truncate if it does. */
+               create_disposition = FILE_OVERWRITE_IF;
+       }
+
        /* mtime. */
        ft.mtime = convert_time_t_to_timespec(srv_make_unix_date3(req->vwv+1));
 
@@ -2380,11 +2554,11 @@ void reply_mknew(struct smb_request *req)
                goto out;
        }
 
+       ucf_flags = filename_create_ucf_flags(req, create_disposition);
        status = filename_convert(ctx,
                                conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               UCF_PREP_CREATEFILE,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2404,14 +2578,6 @@ void reply_mknew(struct smb_request *req)
                         smb_fname_str_dbg(smb_fname)));
        }
 
-       if(req->cmd == SMBmknew) {
-               /* We should fail if file exists. */
-               create_disposition = FILE_CREATE;
-       } else {
-               /* Create if file doesn't exist, truncate if it does. */
-               create_disposition = FILE_OVERWRITE_IF;
-       }
-
        status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
@@ -2482,12 +2648,13 @@ void reply_ctemp(struct smb_request *req)
        struct smb_filename *smb_fname = NULL;
        char *wire_name = NULL;
        char *fname = NULL;
-       uint32 fattr;
+       uint32_t fattr;
        files_struct *fsp;
        int oplock_request;
        char *s;
        NTSTATUS status;
        int i;
+       uint32_t ucf_flags;
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBctemp);
@@ -2524,10 +2691,10 @@ void reply_ctemp(struct smb_request *req)
                        goto out;
                }
 
+               ucf_flags = filename_create_ucf_flags(req, FILE_CREATE);
                status = filename_convert(ctx, conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               UCF_PREP_CREATEFILE,
+                               ucf_flags,
                                NULL,
                                &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
@@ -2632,7 +2799,7 @@ void reply_ctemp(struct smb_request *req)
 ********************************************************************/
 
 static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
-                       uint16 dirtype)
+                       uint16_t dirtype)
 {
        if (!CAN_WRITE(conn)) {
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
@@ -2641,7 +2808,7 @@ static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
        if ((dirtype & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) !=
                        (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
                /* Only bother to read the DOS attribute if we might deny the
-                  rename on the grounds of attribute missmatch. */
+                  rename on the grounds of attribute mismatch. */
                uint32_t fmode = dos_mode(conn, fsp->fsp_name);
                if ((fmode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
                        return NT_STATUS_NO_SUCH_FILE;
@@ -2649,14 +2816,24 @@ static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
        }
 
        if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
-               if (fsp->posix_open) {
+               if (fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) {
                        return NT_STATUS_OK;
                }
 
                /* If no pathnames are open below this
                   directory, allow the rename. */
 
-               if (file_find_subpath(fsp)) {
+               if (lp_strict_rename(SNUM(conn))) {
+                       /*
+                        * Strict rename, check open file db.
+                        */
+                       if (have_file_open_below(fsp->conn, fsp->fsp_name)) {
+                               return NT_STATUS_ACCESS_DENIED;
+                       }
+               } else if (file_find_subpath(fsp)) {
+                       /*
+                        * No strict rename, just look in local process.
+                        */
                        return NT_STATUS_ACCESS_DENIED;
                }
                return NT_STATUS_OK;
@@ -2676,14 +2853,14 @@ static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
 static NTSTATUS do_unlink(connection_struct *conn,
                        struct smb_request *req,
                        struct smb_filename *smb_fname,
-                       uint32 dirtype)
+                       uint32_t dirtype)
 {
-       uint32 fattr;
+       uint32_t fattr;
        files_struct *fsp;
-       uint32 dirtype_orig = dirtype;
+       uint32_t dirtype_orig = dirtype;
        NTSTATUS status;
        int ret;
-       bool posix_paths = lp_posix_pathnames();
+       bool posix_paths = (req != NULL && req->posix_pathnames);
 
        DEBUG(10,("do_unlink: %s, dirtype = %d\n",
                  smb_fname_str_dbg(smb_fname),
@@ -2813,13 +2990,14 @@ static NTSTATUS do_unlink(connection_struct *conn,
 ****************************************************************************/
 
 NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
-                         uint32 dirtype, struct smb_filename *smb_fname,
+                         uint32_t dirtype, struct smb_filename *smb_fname,
                          bool has_wild)
 {
        char *fname_dir = NULL;
        char *fname_mask = NULL;
        int count=0;
        NTSTATUS status = NT_STATUS_OK;
+       struct smb_filename *smb_fname_dir = NULL;
        TALLOC_CTX *ctx = talloc_tos();
 
        /* Split up the directory from the filename/mask. */
@@ -2875,7 +3053,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                        dirtype = FILE_ATTRIBUTE_NORMAL;
                }
 
-               status = check_name(conn, smb_fname->base_name);
+               status = check_name(conn, smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
                        goto out;
                }
@@ -2896,6 +3074,9 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                        status = NT_STATUS_OBJECT_NAME_INVALID;
                        goto out;
                }
+               if (dirtype == 0) {
+                       dirtype = FILE_ATTRIBUTE_NORMAL;
+               }
 
                if (strequal(fname_mask,"????????.???")) {
                        TALLOC_FREE(fname_mask);
@@ -2906,12 +3087,22 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                        }
                }
 
-               status = check_name(conn, fname_dir);
+               smb_fname_dir = synthetic_smb_fname(talloc_tos(),
+                                       fname_dir,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+               if (smb_fname_dir == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
+
+               status = check_name(conn, smb_fname_dir);
                if (!NT_STATUS_IS_OK(status)) {
                        goto out;
                }
 
-               dir_hnd = OpenDir(talloc_tos(), conn, fname_dir, fname_mask,
+               dir_hnd = OpenDir(talloc_tos(), conn, smb_fname_dir, fname_mask,
                                  dirtype);
                if (dir_hnd == NULL) {
                        status = map_nt_error_from_unix(errno);
@@ -2970,7 +3161,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                                goto out;
                        }
 
-                       status = check_name(conn, smb_fname->base_name);
+                       status = check_name(conn, smb_fname);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
                                TALLOC_FREE(frame);
@@ -3001,6 +3192,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
        }
 
  out:
+       TALLOC_FREE(smb_fname_dir);
        TALLOC_FREE(fname_dir);
        TALLOC_FREE(fname_mask);
        return status;
@@ -3015,9 +3207,11 @@ void reply_unlink(struct smb_request *req)
        connection_struct *conn = req->conn;
        char *name = NULL;
        struct smb_filename *smb_fname = NULL;
-       uint32 dirtype;
+       uint32_t dirtype;
        NTSTATUS status;
        bool path_contains_wcard = False;
+       uint32_t ucf_flags = UCF_COND_ALLOW_WCARD_LCOMP |
+                       ucf_flags_from_smb_request(req);
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBunlink);
@@ -3038,9 +3232,8 @@ void reply_unlink(struct smb_request *req)
        }
 
        status = filename_convert(ctx, conn,
-                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                  name,
-                                 UCF_COND_ALLOW_WCARD_LCOMP,
+                                 ucf_flags,
                                  &path_contains_wcard,
                                  &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3486,7 +3679,7 @@ void reply_readbraw(struct smb_request *req)
            (uint64_t)startpos, (uint64_t)maxcount, READ_LOCK,
            &lock);
 
-       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+       if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                reply_readbraw_error(xconn);
                END_PROFILE(SMBreadbraw);
                return;
@@ -3518,8 +3711,6 @@ void reply_readbraw(struct smb_request *req)
 
        DEBUG(5,("reply_readbraw finished\n"));
 
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-
        END_PROFILE(SMBreadbraw);
        return;
 }
@@ -3699,7 +3890,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
            (uint64_t)startpos, (uint64_t)numtoread, READ_LOCK,
            &lock);
 
-       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+       if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                END_PROFILE(SMBread);
                return;
@@ -3710,7 +3901,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
 
        if (nread < 0) {
                reply_nterror(req, map_nt_error_from_unix(errno));
-               goto strict_unlock;
+               goto out;
        }
 
        srv_set_message((char *)req->outbuf, 5, nread+3, False);
@@ -3723,9 +3914,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
        DEBUG(3, ("read %s num=%d nread=%d\n",
                  fsp_fnum_dbg(fsp), (int)numtoread, (int)nread));
 
-strict_unlock:
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-
+out:
        END_PROFILE(SMBread);
        return;
 }
@@ -3734,8 +3923,7 @@ strict_unlock:
  Setup readX header.
 ****************************************************************************/
 
-static int setup_readX_header(struct smb_request *req, char *outbuf,
-                             size_t smb_maxcnt)
+int setup_readX_header(char *outbuf, size_t smb_maxcnt)
 {
        int outsize;
 
@@ -3754,7 +3942,6 @@ static int setup_readX_header(struct smb_request *req, char *outbuf,
              + 2               /* the buflen field */
              + 1);             /* padding byte */
        SSVAL(outbuf,smb_vwv7,(smb_maxcnt >> 16));
-       SSVAL(outbuf,smb_vwv11,smb_maxcnt);
        SCVAL(smb_buf(outbuf), 0, 0); /* padding byte */
        /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
        _smb_setlen_large(outbuf,
@@ -3779,7 +3966,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
            (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
            &lock);
 
-       if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+       if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                return;
        }
@@ -3795,12 +3982,12 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
            (fsp->base_fsp == NULL) &&
            (fsp->wcp == NULL) &&
            lp_use_sendfile(SNUM(conn), xconn->smb1.signing_state) ) {
-               uint8 headerbuf[smb_size + 12 * 2 + 1 /* padding byte */];
+               uint8_t headerbuf[smb_size + 12 * 2 + 1 /* padding byte */];
                DATA_BLOB header;
 
                if(fsp_stat(fsp) == -1) {
                        reply_nterror(req, map_nt_error_from_unix(errno));
-                       goto strict_unlock;
+                       goto out;
                }
 
                if (!S_ISREG(fsp->fsp_name->st.st_ex_mode) ||
@@ -3822,7 +4009,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                header = data_blob_const(headerbuf, sizeof(headerbuf));
 
                construct_reply_common_req(req, (char *)headerbuf);
-               setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
+               setup_readX_header((char *)headerbuf, smb_maxcnt);
 
                nread = SMB_VFS_SENDFILE(xconn->transport.sock, fsp, &header,
                                         startpos, smb_maxcnt);
@@ -3862,7 +4049,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                                DEBUG(3, ("send_file_readX: fake_sendfile %s max=%d nread=%d\n",
                                          fsp_fnum_dbg(fsp), (int)smb_maxcnt, (int)nread));
                                /* No outbuf here means successful sendfile. */
-                               goto strict_unlock;
+                               goto out;
                        }
 
                        DEBUG(0,("send_file_readX: sendfile failed for file "
@@ -3903,17 +4090,27 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                /* No outbuf here means successful sendfile. */
                SMB_PERFCOUNT_SET_MSGLEN_OUT(&req->pcd, nread);
                SMB_PERFCOUNT_END(&req->pcd);
-               goto strict_unlock;
+               goto out;
        }
 
 normal_read:
 
        if ((smb_maxcnt & 0xFF0000) > 0x10000) {
-               uint8 headerbuf[smb_size + 2*12 + 1 /* padding byte */];
+               uint8_t headerbuf[smb_size + 2*12 + 1 /* padding byte */];
                ssize_t ret;
 
+               if (!S_ISREG(fsp->fsp_name->st.st_ex_mode) ||
+                   (startpos > fsp->fsp_name->st.st_ex_size) ||
+                   (smb_maxcnt > (fsp->fsp_name->st.st_ex_size - startpos))) {
+                       /*
+                        * We already know that we would do a short
+                        * read, so don't try the sendfile() path.
+                        */
+                       goto nosendfile_read;
+               }
+
                construct_reply_common_req(req, (char *)headerbuf);
-               setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
+               setup_readX_header((char *)headerbuf, smb_maxcnt);
 
                /* Send out the header. */
                ret = write_data(xconn->transport.sock, (char *)headerbuf,
@@ -3943,7 +4140,7 @@ normal_read:
                        errno = saved_errno;
                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
                }
-               goto strict_unlock;
+               goto out;
        }
 
 nosendfile_read:
@@ -3956,21 +4153,18 @@ nosendfile_read:
                          startpos, smb_maxcnt);
        saved_errno = errno;
 
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-
        if (nread < 0) {
                reply_nterror(req, map_nt_error_from_unix(saved_errno));
                return;
        }
 
-       setup_readX_header(req, (char *)req->outbuf, nread);
+       setup_readX_header((char *)req->outbuf, nread);
 
        DEBUG(3, ("send_file_readX %s max=%d nread=%d\n",
                  fsp_fnum_dbg(fsp), (int)smb_maxcnt, (int)nread));
        return;
 
- strict_unlock:
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
+out:
        TALLOC_FREE(req->outbuf);
        return;
 }
@@ -4154,7 +4348,7 @@ void reply_read_and_X(struct smb_request *req)
 
 void error_to_writebrawerr(struct smb_request *req)
 {
-       uint8 *old_outbuf = req->outbuf;
+       uint8_t *old_outbuf = req->outbuf;
 
        reply_outbuf(req, 1, 0);
 
@@ -4292,7 +4486,7 @@ void reply_writebraw(struct smb_request *req)
                    (uint64_t)startpos, (uint64_t)tcount, WRITE_LOCK,
                    &lock);
 
-               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+               if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                        reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        error_to_writebrawerr(req);
                        END_PROFILE(SMBwritebraw);
@@ -4312,7 +4506,7 @@ void reply_writebraw(struct smb_request *req)
        if (nwritten < (ssize_t)numtowrite)  {
                reply_nterror(req, NT_STATUS_DISK_FULL);
                error_to_writebrawerr(req);
-               goto strict_unlock;
+               goto out;
        }
 
        total_written = nwritten;
@@ -4322,7 +4516,7 @@ void reply_writebraw(struct smb_request *req)
        if (!buf) {
                reply_nterror(req, NT_STATUS_NO_MEMORY);
                error_to_writebrawerr(req);
-               goto strict_unlock;
+               goto out;
        }
 
        /* Return a SMBwritebraw message to the redirector to tell
@@ -4385,7 +4579,7 @@ void reply_writebraw(struct smb_request *req)
                        TALLOC_FREE(buf);
                        reply_nterror(req, map_nt_error_from_unix(errno));
                        error_to_writebrawerr(req);
-                       goto strict_unlock;
+                       goto out;
                }
 
                if (nwritten < (ssize_t)numtowrite) {
@@ -4407,7 +4601,7 @@ void reply_writebraw(struct smb_request *req)
                         fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
                error_to_writebrawerr(req);
-               goto strict_unlock;
+               goto out;
        }
 
        DEBUG(3,("reply_writebraw: secondart write %s start=%.0f num=%d "
@@ -4415,10 +4609,6 @@ void reply_writebraw(struct smb_request *req)
                fsp_fnum_dbg(fsp), (double)startpos, (int)numtowrite,
                (int)total_written));
 
-       if (!fsp->print_file) {
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-       }
-
        /* We won't return a status if write through is not selected - this
         * follows what WfWg does */
        END_PROFILE(SMBwritebraw);
@@ -4440,11 +4630,7 @@ void reply_writebraw(struct smb_request *req)
        }
        return;
 
-strict_unlock:
-       if (!fsp->print_file) {
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-       }
-
+out:
        END_PROFILE(SMBwritebraw);
        return;
 }
@@ -4498,7 +4684,7 @@ void reply_writeunlock(struct smb_request *req)
                    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
                    &lock);
 
-               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+               if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                        reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        END_PROFILE(SMBwriteunlock);
                        return;
@@ -4520,17 +4706,17 @@ void reply_writeunlock(struct smb_request *req)
                DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
                         fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
-               goto strict_unlock;
+               goto out;
        }
 
        if(nwritten < 0) {
                reply_nterror(req, map_nt_error_from_unix(saved_errno));
-               goto strict_unlock;
+               goto out;
        }
 
        if((nwritten < numtowrite) && (numtowrite != 0)) {
                reply_nterror(req, NT_STATUS_DISK_FULL);
-               goto strict_unlock;
+               goto out;
        }
 
        if (numtowrite && !fsp->print_file) {
@@ -4543,7 +4729,7 @@ void reply_writeunlock(struct smb_request *req)
 
                if (NT_STATUS_V(status)) {
                        reply_nterror(req, status);
-                       goto strict_unlock;
+                       goto out;
                }
        }
 
@@ -4554,11 +4740,7 @@ void reply_writeunlock(struct smb_request *req)
        DEBUG(3, ("writeunlock %s num=%d wrote=%d\n",
                  fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
 
-strict_unlock:
-       if (numtowrite && !fsp->print_file) {
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-       }
-
+out:
        END_PROFILE(SMBwriteunlock);
        return;
 }
@@ -4619,7 +4801,7 @@ void reply_write(struct smb_request *req)
                        (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
                        &lock);
 
-               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+               if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                        reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        END_PROFILE(SMBwrite);
                        return;
@@ -4639,12 +4821,12 @@ void reply_write(struct smb_request *req)
                nwritten = vfs_allocate_file_space(fsp, (off_t)startpos);
                if (nwritten < 0) {
                        reply_nterror(req, NT_STATUS_DISK_FULL);
-                       goto strict_unlock;
+                       goto out;
                }
                nwritten = vfs_set_filelen(fsp, (off_t)startpos);
                if (nwritten < 0) {
                        reply_nterror(req, NT_STATUS_DISK_FULL);
-                       goto strict_unlock;
+                       goto out;
                }
                trigger_write_time_update_immediate(fsp);
        } else {
@@ -4656,17 +4838,17 @@ void reply_write(struct smb_request *req)
                DEBUG(5,("reply_write: sync_file for %s returned %s\n",
                         fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
-               goto strict_unlock;
+               goto out;
        }
 
        if(nwritten < 0) {
                reply_nterror(req, map_nt_error_from_unix(saved_errno));
-               goto strict_unlock;
+               goto out;
        }
 
        if((nwritten == 0) && (numtowrite != 0)) {
                reply_nterror(req, NT_STATUS_DISK_FULL);
-               goto strict_unlock;
+               goto out;
        }
 
        reply_outbuf(req, 1, 0);
@@ -4680,11 +4862,7 @@ void reply_write(struct smb_request *req)
 
        DEBUG(3, ("write %s num=%d wrote=%d\n", fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
 
-strict_unlock:
-       if (!fsp->print_file) {
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-       }
-
+out:
        END_PROFILE(SMBwrite);
        return;
 }
@@ -4912,15 +5090,13 @@ void reply_write_and_X(struct smb_request *req)
                    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
                    &lock);
 
-               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+               if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                        reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        goto out;
                }
 
                nwritten = write_file(req,fsp,data,startpos,numtowrite);
                saved_errno = errno;
-
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
        }
 
        if(nwritten < 0) {
@@ -5061,7 +5237,7 @@ void reply_lseek(struct smb_request *req)
 void reply_flush(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       uint16 fnum;
+       uint16_t fnum;
        files_struct *fsp;
 
        START_PROFILE(SMBflush);
@@ -5306,7 +5482,7 @@ void reply_writeclose(struct smb_request *req)
                    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
                    &lock);
 
-               if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
+               if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
                        reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        END_PROFILE(SMBwriteclose);
                        return;
@@ -5315,10 +5491,6 @@ void reply_writeclose(struct smb_request *req)
 
        nwritten = write_file(req,fsp,data,startpos,numtowrite);
 
-       if (fsp->print_file == NULL) {
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-       }
-
        set_close_write_time(fsp, mtime);
 
        /*
@@ -5741,10 +5913,11 @@ void reply_printqueue(struct smb_request *req)
 
                ZERO_STRUCT(handle);
 
-               status = rpc_pipe_open_interface(conn,
+               status = rpc_pipe_open_interface(mem_ctx,
                                                 &ndr_table_spoolss,
                                                 conn->session_info,
                                                 conn->sconn->remote_address,
+                                                conn->sconn->local_address,
                                                 conn->sconn->msg_ctx,
                                                 &cli);
                if (!NT_STATUS_IS_OK(status)) {
@@ -5926,6 +6099,7 @@ void reply_mkdir(struct smb_request *req)
        struct smb_filename *smb_dname = NULL;
        char *directory = NULL;
        NTSTATUS status;
+       uint32_t ucf_flags;
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBmkdir);
@@ -5937,10 +6111,10 @@ void reply_mkdir(struct smb_request *req)
                goto out;
        }
 
+       ucf_flags = filename_create_ucf_flags(req, FILE_CREATE);
        status = filename_convert(ctx, conn,
-                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                 directory,
-                                UCF_PREP_CREATEFILE,
+                                ucf_flags,
                                 NULL,
                                 &smb_dname);
        if (!NT_STATUS_IS_OK(status)) {
@@ -5996,6 +6170,7 @@ void reply_rmdir(struct smb_request *req)
        TALLOC_CTX *ctx = talloc_tos();
        files_struct *fsp = NULL;
        int info = 0;
+       uint32_t ucf_flags = ucf_flags_from_smb_request(req);
        struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBrmdir);
@@ -6008,9 +6183,8 @@ void reply_rmdir(struct smb_request *req)
        }
 
        status = filename_convert(ctx, conn,
-                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                 directory,
-                                0,
+                                ucf_flags,
                                 NULL,
                                 &smb_dname);
        if (!NT_STATUS_IS_OK(status)) {
@@ -6309,7 +6483,7 @@ static void notify_rename(connection_struct *conn, bool is_dir,
 {
        char *parent_dir_src = NULL;
        char *parent_dir_dst = NULL;
-       uint32 mask;
+       uint32_t mask;
 
        mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
                : FILE_NOTIFY_CHANGE_FILE_NAME;
@@ -6396,16 +6570,17 @@ static NTSTATUS parent_dirname_compatible_open(connection_struct *conn,
 NTSTATUS rename_internals_fsp(connection_struct *conn,
                        files_struct *fsp,
                        const struct smb_filename *smb_fname_dst_in,
-                       uint32 attrs,
+                       uint32_t attrs,
                        bool replace_if_exists)
 {
        TALLOC_CTX *ctx = talloc_tos();
        struct smb_filename *smb_fname_dst = NULL;
        NTSTATUS status = NT_STATUS_OK;
        struct share_mode_lock *lck = NULL;
+       uint32_t access_mask = SEC_DIR_ADD_FILE;
        bool dst_exists, old_is_stream, new_is_stream;
 
-       status = check_name(conn, smb_fname_dst_in->base_name);
+       status = check_name(conn, smb_fname_dst_in);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -6425,67 +6600,83 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
        /*
         * Check for special case with case preserving and not
-        * case sensitive. If the old last component differs from the original
+        * case sensitive. If the new last component differs from the original
         * last component only by case, then we should allow
         * the rename (user is trying to change the case of the
         * filename).
         */
-       if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
+       if (!conn->case_sensitive && conn->case_preserve &&
            strequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
            strequal(fsp->fsp_name->stream_name, smb_fname_dst->stream_name)) {
-               char *last_slash;
-               char *fname_dst_lcomp_base_mod = NULL;
-               struct smb_filename *smb_fname_orig_lcomp = NULL;
+               char *fname_dst_parent = NULL;
+               const char *fname_dst_lcomp = NULL;
+               char *orig_lcomp_path = NULL;
+               char *orig_lcomp_stream = NULL;
+               bool ok = true;
 
                /*
-                * Get the last component of the destination name.
+                * Split off the last component of the processed
+                * destination name. We will compare this to
+                * the split components of smb_fname_dst->original_lcomp.
                 */
-               last_slash = strrchr_m(smb_fname_dst->base_name, '/');
-               if (last_slash) {
-                       fname_dst_lcomp_base_mod = talloc_strdup(ctx, last_slash + 1);
-               } else {
-                       fname_dst_lcomp_base_mod = talloc_strdup(ctx, smb_fname_dst->base_name);
-               }
-               if (!fname_dst_lcomp_base_mod) {
+               if (!parent_dirname(ctx,
+                               smb_fname_dst->base_name,
+                               &fname_dst_parent,
+                               &fname_dst_lcomp)) {
                        status = NT_STATUS_NO_MEMORY;
                        goto out;
                }
 
                /*
-                * Create an smb_filename struct using the original last
-                * component of the destination.
+                * The original_lcomp component contains
+                * the last_component of the path + stream
+                * name (if a stream exists).
+                *
+                * Split off the stream name so we
+                * can check them separately.
                 */
-               smb_fname_orig_lcomp = synthetic_smb_fname_split(
-                       ctx, smb_fname_dst->original_lcomp, NULL);
-               if (smb_fname_orig_lcomp == NULL) {
+
+               if (fsp->posix_flags & FSP_POSIX_FLAGS_PATHNAMES) {
+                       /* POSIX - no stream component. */
+                       orig_lcomp_path = talloc_strdup(ctx,
+                                               smb_fname_dst->original_lcomp);
+                       if (orig_lcomp_path == NULL) {
+                               ok = false;
+                       }
+               } else {
+                       ok = split_stream_filename(ctx,
+                                       smb_fname_dst->original_lcomp,
+                                       &orig_lcomp_path,
+                                       &orig_lcomp_stream);
+               }
+
+               if (!ok) {
+                       TALLOC_FREE(fname_dst_parent);
                        status = NT_STATUS_NO_MEMORY;
-                       TALLOC_FREE(fname_dst_lcomp_base_mod);
                        goto out;
                }
 
                /* If the base names only differ by case, use original. */
-               if(!strcsequal(fname_dst_lcomp_base_mod,
-                              smb_fname_orig_lcomp->base_name)) {
+               if(!strcsequal(fname_dst_lcomp, orig_lcomp_path)) {
                        char *tmp;
                        /*
                         * Replace the modified last component with the
                         * original.
                         */
-                       if (last_slash) {
-                               *last_slash = '\0'; /* Truncate at the '/' */
+                       if (!ISDOT(fname_dst_parent)) {
                                tmp = talloc_asprintf(smb_fname_dst,
                                        "%s/%s",
-                                       smb_fname_dst->base_name,
-                                       smb_fname_orig_lcomp->base_name);
+                                       fname_dst_parent,
+                                       orig_lcomp_path);
                        } else {
-                               tmp = talloc_asprintf(smb_fname_dst,
-                                       "%s",
-                                       smb_fname_orig_lcomp->base_name);
+                               tmp = talloc_strdup(smb_fname_dst,
+                                       orig_lcomp_path);
                        }
                        if (tmp == NULL) {
                                status = NT_STATUS_NO_MEMORY;
-                               TALLOC_FREE(fname_dst_lcomp_base_mod);
-                               TALLOC_FREE(smb_fname_orig_lcomp);
+                               TALLOC_FREE(fname_dst_parent);
+                               TALLOC_FREE(orig_lcomp_path);
+                               TALLOC_FREE(orig_lcomp_stream);
                                goto out;
                        }
                        TALLOC_FREE(smb_fname_dst->base_name);
@@ -6494,22 +6685,23 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
                /* If the stream_names only differ by case, use original. */
                if(!strcsequal(smb_fname_dst->stream_name,
-                              smb_fname_orig_lcomp->stream_name)) {
-                       char *tmp = NULL;
+                              orig_lcomp_stream)) {
                        /* Use the original stream. */
-                       tmp = talloc_strdup(smb_fname_dst,
-                                           smb_fname_orig_lcomp->stream_name);
+                       char *tmp = talloc_strdup(smb_fname_dst,
+                                           orig_lcomp_stream);
                        if (tmp == NULL) {
                                status = NT_STATUS_NO_MEMORY;
-                               TALLOC_FREE(fname_dst_lcomp_base_mod);
-                               TALLOC_FREE(smb_fname_orig_lcomp);
+                               TALLOC_FREE(fname_dst_parent);
+                               TALLOC_FREE(orig_lcomp_path);
+                               TALLOC_FREE(orig_lcomp_stream);
                                goto out;
                        }
                        TALLOC_FREE(smb_fname_dst->stream_name);
                        smb_fname_dst->stream_name = tmp;
                }
-               TALLOC_FREE(fname_dst_lcomp_base_mod);
-               TALLOC_FREE(smb_fname_orig_lcomp);
+               TALLOC_FREE(fname_dst_parent);
+               TALLOC_FREE(orig_lcomp_path);
+               TALLOC_FREE(orig_lcomp_stream);
        }
 
        /*
@@ -6583,6 +6775,23 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
        if (rename_path_prefix_equal(fsp->fsp_name, smb_fname_dst)) {
                status = NT_STATUS_ACCESS_DENIED;
+               goto out;
+       }
+
+       /* Do we have rights to move into the destination ? */
+       if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
+               /* We're moving a directory. */
+               access_mask = SEC_DIR_ADD_SUBDIR;
+       }
+       status = check_parent_access(conn,
+                               smb_fname_dst,
+                               access_mask);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_INFO("check_parent_access on "
+                       "dst %s returned %s\n",
+                       smb_fname_str_dbg(smb_fname_dst),
+                       nt_errstr(status));
+               goto out;
        }
 
        lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
@@ -6595,14 +6804,14 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
        SMB_ASSERT(lck != NULL);
 
        if(SMB_VFS_RENAME(conn, fsp->fsp_name, smb_fname_dst) == 0) {
-               uint32 create_options = fsp->fh->private_options;
+               uint32_t create_options = fsp->fh->private_options;
 
                DEBUG(3, ("rename_internals_fsp: succeeded doing rename on "
                          "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
 
                if (!fsp->is_directory &&
-                   !lp_posix_pathnames() &&
+                   !(fsp->posix_flags & FSP_POSIX_FLAGS_PATHNAMES) &&
                    (lp_map_archive(SNUM(conn)) ||
                    lp_store_dos_attributes(SNUM(conn)))) {
                        /* We must set the archive bit on the newly
@@ -6675,13 +6884,14 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        struct smb_request *req,
                        struct smb_filename *smb_fname_src,
                        struct smb_filename *smb_fname_dst,
-                       uint32 attrs,
+                       uint32_t attrs,
                        bool replace_if_exists,
                        bool src_has_wild,
                        bool dest_has_wild,
                        uint32_t access_mask)
 {
        char *fname_src_dir = NULL;
+       struct smb_filename *smb_fname_src_dir = NULL;
        char *fname_src_mask = NULL;
        int count=0;
        NTSTATUS status = NT_STATUS_OK;
@@ -6690,7 +6900,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
        char *talloced = NULL;
        long offset = 0;
        int create_options = 0;
-       bool posix_pathnames = lp_posix_pathnames();
+       bool posix_pathnames = (req != NULL && req->posix_pathnames);
        int rc;
 
        /*
@@ -6849,12 +7059,22 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                }
        }
 
-       status = check_name(conn, fname_src_dir);
+       smb_fname_src_dir = synthetic_smb_fname(talloc_tos(),
+                               fname_src_dir,
+                               NULL,
+                               NULL,
+                               smb_fname_src->flags);
+       if (smb_fname_src_dir == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
+       }
+
+       status = check_name(conn, smb_fname_src_dir);
        if (!NT_STATUS_IS_OK(status)) {
                goto out;
        }
 
-       dir_hnd = OpenDir(talloc_tos(), conn, fname_src_dir, fname_src_mask,
+       dir_hnd = OpenDir(talloc_tos(), conn, smb_fname_src_dir, fname_src_mask,
                          attrs);
        if (dir_hnd == NULL) {
                status = map_nt_error_from_unix(errno);
@@ -7010,6 +7230,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
 
  out:
        TALLOC_FREE(talloced);
+       TALLOC_FREE(smb_fname_src_dir);
        TALLOC_FREE(fname_src_dir);
        TALLOC_FREE(fname_src_mask);
        return status;
@@ -7025,15 +7246,22 @@ void reply_mv(struct smb_request *req)
        char *name = NULL;
        char *newname = NULL;
        const char *p;
-       uint32 attrs;
+       uint32_t attrs;
        NTSTATUS status;
        bool src_has_wcard = False;
        bool dest_has_wcard = False;
        TALLOC_CTX *ctx = talloc_tos();
        struct smb_filename *smb_fname_src = NULL;
        struct smb_filename *smb_fname_dst = NULL;
-       uint32_t src_ucf_flags = lp_posix_pathnames() ? UCF_UNIX_NAME_LOOKUP : UCF_COND_ALLOW_WCARD_LCOMP;
-       uint32_t dst_ucf_flags = UCF_SAVE_LCOMP | (lp_posix_pathnames() ? 0 : UCF_COND_ALLOW_WCARD_LCOMP);
+       uint32_t src_ucf_flags = ucf_flags_from_smb_request(req) |
+               (req->posix_pathnames ?
+                       UCF_UNIX_NAME_LOOKUP :
+                       UCF_COND_ALLOW_WCARD_LCOMP);
+       uint32_t dst_ucf_flags = ucf_flags_from_smb_request(req) |
+               UCF_SAVE_LCOMP |
+               (req->posix_pathnames ?
+                       0 :
+                       UCF_COND_ALLOW_WCARD_LCOMP);
        bool stream_rename = false;
 
        START_PROFILE(SMBmv);
@@ -7060,7 +7288,7 @@ void reply_mv(struct smb_request *req)
                goto out;
        }
 
-       if (!lp_posix_pathnames()) {
+       if (!req->posix_pathnames) {
                /* The newname must begin with a ':' if the
                   name contains a ':'. */
                if (strchr_m(name, ':')) {
@@ -7074,7 +7302,6 @@ void reply_mv(struct smb_request *req)
 
        status = filename_convert(ctx,
                                  conn,
-                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                  name,
                                  src_ucf_flags,
                                  &src_has_wcard,
@@ -7092,7 +7319,6 @@ void reply_mv(struct smb_request *req)
 
        status = filename_convert(ctx,
                                  conn,
-                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                  newname,
                                  dst_ucf_flags,
                                  &dest_has_wcard,
@@ -7162,8 +7388,8 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
        struct smb_filename *smb_fname_dst_tmp = NULL;
        off_t ret=-1;
        files_struct *fsp1,*fsp2;
-       uint32 dosattrs;
-       uint32 new_create_disposition;
+       uint32_t dosattrs;
+       uint32_t new_create_disposition;
        NTSTATUS status;
 
 
@@ -7329,6 +7555,7 @@ void reply_copy(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
        struct smb_filename *smb_fname_src = NULL;
+       struct smb_filename *smb_fname_src_dir = NULL;
        struct smb_filename *smb_fname_dst = NULL;
        char *fname_src = NULL;
        char *fname_dst = NULL;
@@ -7344,6 +7571,10 @@ void reply_copy(struct smb_request *req)
        bool source_has_wild = False;
        bool dest_has_wild = False;
        NTSTATUS status;
+       uint32_t ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP |
+               ucf_flags_from_smb_request(req);
+       uint32_t ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP |
+               ucf_flags_from_smb_request(req);
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBcopy);
@@ -7381,9 +7612,8 @@ void reply_copy(struct smb_request *req)
        }
 
        status = filename_convert(ctx, conn,
-                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                  fname_src,
-                                 UCF_COND_ALLOW_WCARD_LCOMP,
+                                 ucf_flags_src,
                                  &source_has_wild,
                                  &smb_fname_src);
        if (!NT_STATUS_IS_OK(status)) {
@@ -7397,9 +7627,8 @@ void reply_copy(struct smb_request *req)
        }
 
        status = filename_convert(ctx, conn,
-                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                  fname_dst,
-                                 UCF_COND_ALLOW_WCARD_LCOMP,
+                                 ucf_flags_dst,
                                  &dest_has_wild,
                                  &smb_fname_dst);
        if (!NT_STATUS_IS_OK(status)) {
@@ -7496,13 +7725,13 @@ void reply_copy(struct smb_request *req)
                        smb_fname_dst->base_name = fname_dst_mod;
                }
 
-               status = check_name(conn, smb_fname_src->base_name);
+               status = check_name(conn, smb_fname_src);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        goto out;
                }
 
-               status = check_name(conn, smb_fname_dst->base_name);
+               status = check_name(conn, smb_fname_dst);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        goto out;
@@ -7544,13 +7773,27 @@ void reply_copy(struct smb_request *req)
                        }
                }
 
-               status = check_name(conn, fname_src_dir);
+               smb_fname_src_dir = synthetic_smb_fname(talloc_tos(),
+                                       fname_src_dir,
+                                       NULL,
+                                       NULL,
+                                       smb_fname_src->flags);
+               if (smb_fname_src_dir == NULL) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       goto out;
+               }
+
+               status = check_name(conn, smb_fname_src_dir);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        goto out;
                }
 
-               dir_hnd = OpenDir(ctx, conn, fname_src_dir, fname_src_mask, 0);
+               dir_hnd = OpenDir(ctx,
+                               conn,
+                               smb_fname_src_dir,
+                               fname_src_mask,
+                               0);
                if (dir_hnd == NULL) {
                        status = map_nt_error_from_unix(errno);
                        reply_nterror(req, status);
@@ -7619,7 +7862,7 @@ void reply_copy(struct smb_request *req)
                        TALLOC_FREE(smb_fname_dst->base_name);
                        smb_fname_dst->base_name = destname;
 
-                       status = check_name(conn, smb_fname_src->base_name);
+                       status = check_name(conn, smb_fname_src);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
                                TALLOC_FREE(talloced);
@@ -7627,7 +7870,7 @@ void reply_copy(struct smb_request *req)
                                goto out;
                        }
 
-                       status = check_name(conn, smb_fname_dst->base_name);
+                       status = check_name(conn, smb_fname_dst);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
                                TALLOC_FREE(talloced);
@@ -7660,6 +7903,7 @@ void reply_copy(struct smb_request *req)
        SSVAL(req->outbuf,smb_vwv0,count);
  out:
        TALLOC_FREE(smb_fname_src);
+       TALLOC_FREE(smb_fname_src_dir);
        TALLOC_FREE(smb_fname_dst);
        TALLOC_FREE(fname_src);
        TALLOC_FREE(fname_dst);
@@ -7978,9 +8222,9 @@ void reply_lockingX(struct smb_request *req)
        files_struct *fsp;
        unsigned char locktype;
        unsigned char oplocklevel;
-       uint16 num_ulocks;
-       uint16 num_locks;
-       int32 lock_timeout;
+       uint16_t num_ulocks;
+       uint16_t num_locks;
+       int32_t lock_timeout;
        int i;
        const uint8_t *data;
        bool large_file_format;
@@ -8372,8 +8616,8 @@ void reply_getattrE(struct smb_request *req)
                SIVAL(req->outbuf, smb_vwv6, 0);
                SIVAL(req->outbuf, smb_vwv8, 0);
        } else {
-               uint32 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &fsp->fsp_name->st);
-               SIVAL(req->outbuf, smb_vwv6, (uint32)fsp->fsp_name->st.st_ex_size);
+               uint32_t allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &fsp->fsp_name->st);
+               SIVAL(req->outbuf, smb_vwv6, (uint32_t)fsp->fsp_name->st.st_ex_size);
                SIVAL(req->outbuf, smb_vwv8, allocation_size);
        }
        SSVAL(req->outbuf,smb_vwv10, mode);