s3:libsmb: get rid of cli_smb_req_*,cli_smb_wct_ofs,cli_smb_chain_send
[kai/samba.git] / source3 / libsmb / clifile.c
index 582fe0a12bfdfedf622c768fbcbd7f2c1a76f711..b762a377c180941e472350fee7bc9012a91b834d 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "libsmb/libsmb.h"
+#include "../lib/util/tevent_ntstatus.h"
 #include "async_smb.h"
+#include "libsmb/clirap.h"
+#include "trans2.h"
+#include "ntioctl.h"
+#include "libcli/security/secdesc.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 /***********************************************************
  Common function for pushing stings, used by smb_bytes_push_str()
@@ -42,11 +50,13 @@ static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
 
        buflen = talloc_get_size(buf);
 
-       if (align_odd && ucs2 && (buflen % 2 == 0)) {
+       if (ucs2 &&
+           ((align_odd && (buflen % 2 == 0)) ||
+            (!align_odd && (buflen % 2 == 1)))) {
                /*
                 * We're pushing into an SMB buffer, align odd
                 */
-               buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
+               buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
                if (buf == NULL) {
                        return NULL;
                }
@@ -57,11 +67,11 @@ static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
        if (!convert_string_talloc(talloc_tos(), CH_UNIX,
                                   ucs2 ? CH_UTF16LE : CH_DOS,
                                   str, str_len, &converted,
-                                  &converted_size, true)) {
+                                  &converted_size)) {
                return NULL;
        }
 
-       buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
+       buf = talloc_realloc(NULL, buf, uint8_t,
                                   buflen + converted_size);
        if (buf == NULL) {
                TALLOC_FREE(converted);
@@ -102,7 +112,7 @@ uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
        }
        buflen = talloc_get_size(buf);
 
-       buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
+       buf = talloc_realloc(NULL, buf, uint8_t,
                                   buflen + 1 + num_bytes);
        if (buf == NULL) {
                return NULL;
@@ -119,14 +129,33 @@ uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
  other modules use async trans calls.
 ***********************************************************/
 
-static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
-                           const char *str, size_t str_len,
-                           size_t *pconverted_size)
+uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
+                              const char *str, size_t str_len,
+                              size_t *pconverted_size)
 {
        return internal_bytes_push_str(buf, ucs2, str, str_len,
                        false, pconverted_size);
 }
 
+uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
+                                const uint8_t *bytes, size_t num_bytes)
+{
+       size_t buflen;
+
+       if (buf == NULL) {
+               return NULL;
+       }
+       buflen = talloc_get_size(buf);
+
+       buf = talloc_realloc(NULL, buf, uint8_t,
+                            buflen + num_bytes);
+       if (buf == NULL) {
+               return NULL;
+       }
+       memcpy(&buf[buflen], bytes, num_bytes);
+       return buf;
+}
+
 struct cli_setpathinfo_state {
        uint16_t setup;
        uint8_t *param;
@@ -155,14 +184,14 @@ struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
        SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
 
        /* Setup param array. */
-       state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
+       state->param = talloc_zero_array(state, uint8_t, 6);
        if (tevent_req_nomem(state->param, req)) {
                return tevent_req_post(req, ev);
        }
        SSVAL(state->param, 0, level);
 
        state->param = trans2_bytes_push_str(
-               state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
+               state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
        if (tevent_req_nomem(state->param, req)) {
                return tevent_req_post(req, ev);
        }
@@ -216,7 +245,7 @@ NTSTATUS cli_setpathinfo(struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -273,7 +302,7 @@ static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
        state->data = trans2_bytes_push_str(
-               state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
+               state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
 
        subreq = cli_setpathinfo_send(
                state, ev, cli, level, newname,
@@ -319,7 +348,7 @@ NTSTATUS cli_posix_symlink(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -352,9 +381,6 @@ NTSTATUS cli_posix_symlink(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -377,7 +403,7 @@ struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req = NULL, *subreq = NULL;
        struct readlink_state *state = NULL;
-       uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
+       uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
 
        req = tevent_req_create(mem_ctx, &state, struct readlink_state);
        if (req == NULL) {
@@ -412,8 +438,7 @@ static void cli_posix_readlink_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        /*
@@ -439,13 +464,12 @@ NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
        }
        /* The returned data is a pushed string, not raw data. */
        if (!convert_string_talloc(state,
-                               cli_ucs2(cli) ? CH_UTF16LE : CH_DOS, 
+                               smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS, 
                                CH_UNIX,
                                state->data,
                                state->num_data,
                                &converted,
-                               &converted_size,
-                               true)) {
+                               &converted_size)) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -465,7 +489,7 @@ NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -498,9 +522,6 @@ NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -532,7 +553,7 @@ NTSTATUS cli_posix_hardlink(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -565,106 +586,9 @@ NTSTATUS cli_posix_hardlink(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
-/****************************************************************************
- Map standard UNIX permissions onto wire representations.
-****************************************************************************/
-
-uint32_t unix_perms_to_wire(mode_t perms)
-{
-        unsigned int ret = 0;
-
-        ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
-        ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
-        ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
-        ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
-        ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
-        ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
-        ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
-        ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
-        ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
-#ifdef S_ISVTX
-        ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
-#endif
-#ifdef S_ISGID
-        ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
-#endif
-#ifdef S_ISUID
-        ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
-#endif
-        return ret;
-}
-
-/****************************************************************************
- Map wire permissions to standard UNIX.
-****************************************************************************/
-
-mode_t wire_perms_to_unix(uint32_t perms)
-{
-        mode_t ret = (mode_t)0;
-
-        ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
-        ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
-        ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
-        ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
-        ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
-        ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
-        ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
-        ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
-        ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
-#ifdef S_ISVTX
-        ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
-#endif
-#ifdef S_ISGID
-        ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
-#endif
-#ifdef S_ISUID
-        ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
-#endif
-        return ret;
-}
-
-/****************************************************************************
- Return the file type from the wire filetype for UNIX extensions.
-****************************************************************************/
-
-static mode_t unix_filetype_from_wire(uint32_t wire_type)
-{
-       switch (wire_type) {
-               case UNIX_TYPE_FILE:
-                       return S_IFREG;
-               case UNIX_TYPE_DIR:
-                       return S_IFDIR;
-#ifdef S_IFLNK
-               case UNIX_TYPE_SYMLINK:
-                       return S_IFLNK;
-#endif
-#ifdef S_IFCHR
-               case UNIX_TYPE_CHARDEV:
-                       return S_IFCHR;
-#endif
-#ifdef S_IFBLK
-               case UNIX_TYPE_BLKDEV:
-                       return S_IFBLK;
-#endif
-#ifdef S_IFIFO
-               case UNIX_TYPE_FIFO:
-                       return S_IFIFO;
-#endif
-#ifdef S_IFSOCK
-               case UNIX_TYPE_SOCKET:
-                       return S_IFSOCK;
-#endif
-               default:
-                       return (mode_t)0;
-       }
-}
-
 /****************************************************************************
  Do a POSIX getfacl (UNIX extensions).
 ****************************************************************************/
@@ -689,7 +613,7 @@ struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
-                                   0, cli->max_xmit);
+                                   0, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -708,8 +632,7 @@ static void cli_posix_getfacl_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -742,7 +665,7 @@ NTSTATUS cli_posix_getfacl(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -774,9 +697,6 @@ NTSTATUS cli_posix_getfacl(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -822,8 +742,7 @@ static void cli_posix_stat_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -877,7 +796,7 @@ NTSTATUS cli_posix_stat(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -909,9 +828,6 @@ NTSTATUS cli_posix_stat(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -993,7 +909,7 @@ NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1026,9 +942,6 @@ NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1065,7 +978,7 @@ NTSTATUS cli_posix_chown(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1099,9 +1012,6 @@ NTSTATUS cli_posix_chown(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1131,27 +1041,27 @@ struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
+       SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
 
        bytes = talloc_array(state, uint8_t, 1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
                                   strlen(fname_src)+1, NULL);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
 
-       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+       bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
 
        bytes[talloc_get_size(bytes)-1] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
                                   strlen(fname_dst)+1, NULL);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -1174,8 +1084,7 @@ static void cli_rename_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1193,7 +1102,7 @@ NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fn
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1222,9 +1131,6 @@ NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fn
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1256,7 +1162,7 @@ static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
+       SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
        SSVAL(state->vwv+1, 0, rename_flag);
 
        bytes = talloc_array(state, uint8_t, 1);
@@ -1264,20 +1170,20 @@ static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
                                   strlen(fname_src)+1, NULL);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
 
-       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+       bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
 
        bytes[talloc_get_size(bytes)-1] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
                                   strlen(fname_dst)+1, NULL);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -1300,8 +1206,7 @@ static void cli_ntrename_internal_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1338,7 +1243,7 @@ NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1367,9 +1272,6 @@ NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1403,7 +1305,7 @@ NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const cha
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1432,9 +1334,6 @@ NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const cha
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1471,7 +1370,7 @@ struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
                                   strlen(fname)+1, NULL);
 
        if (tevent_req_nomem(bytes, req)) {
@@ -1495,8 +1394,7 @@ static void cli_unlink_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1514,7 +1412,7 @@ NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_a
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1543,9 +1441,6 @@ NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_a
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1579,7 +1474,7 @@ struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
                                   strlen(dname)+1, NULL);
 
        if (tevent_req_nomem(bytes, req)) {
@@ -1603,8 +1498,7 @@ static void cli_mkdir_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1622,7 +1516,7 @@ NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1651,9 +1545,6 @@ NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1687,7 +1578,7 @@ struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
                                   strlen(dname)+1, NULL);
 
        if (tevent_req_nomem(bytes, req)) {
@@ -1711,8 +1602,7 @@ static void cli_rmdir_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1730,7 +1620,7 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1759,9 +1649,6 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1843,7 +1730,7 @@ NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1876,9 +1763,6 @@ NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1930,17 +1814,18 @@ struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
        SIVAL(vwv+13, 1, FileAttributes);
        SIVAL(vwv+15, 1, ShareAccess);
        SIVAL(vwv+17, 1, CreateDisposition);
-       SIVAL(vwv+19, 1, CreateOptions);
+       SIVAL(vwv+19, 1, CreateOptions |
+               (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
        SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
        SCVAL(vwv+23, 1, SecurityFlags);
 
        bytes = talloc_array(state, uint8_t, 0);
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
                                   fname, strlen(fname)+1,
                                   &converted_len);
 
        /* sigh. this copes with broken netapp filer behaviour */
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
 
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -1973,8 +1858,7 @@ static void cli_ntcreate_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
                              &num_bytes, &bytes);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        state->fnum = SVAL(vwv+2, 1);
@@ -2010,7 +1894,7 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2041,9 +1925,185 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
        status = cli_ntcreate_recv(req, pfid);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
+       return status;
+}
+
+struct cli_nttrans_create_state {
+       uint16_t fnum;
+};
+
+static void cli_nttrans_create_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
+                                          struct event_context *ev,
+                                          struct cli_state *cli,
+                                          const char *fname,
+                                          uint32_t CreatFlags,
+                                          uint32_t DesiredAccess,
+                                          uint32_t FileAttributes,
+                                          uint32_t ShareAccess,
+                                          uint32_t CreateDisposition,
+                                          uint32_t CreateOptions,
+                                          uint8_t SecurityFlags,
+                                          struct security_descriptor *secdesc,
+                                          struct ea_struct *eas,
+                                          int num_eas)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_nttrans_create_state *state;
+       uint8_t *param;
+       uint8_t *secdesc_buf;
+       size_t secdesc_len;
+       NTSTATUS status;
+       size_t converted_len;
+
+       req = tevent_req_create(mem_ctx,
+                               &state, struct cli_nttrans_create_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       if (secdesc != NULL) {
+               status = marshall_sec_desc(talloc_tos(), secdesc,
+                                          &secdesc_buf, &secdesc_len);
+               if (tevent_req_nterror(req, status)) {
+                       DEBUG(10, ("marshall_sec_desc failed: %s\n",
+                                  nt_errstr(status)));
+                       return tevent_req_post(req, ev);
+               }
+       } else {
+               secdesc_buf = NULL;
+               secdesc_len = 0;
+       }
+
+       if (num_eas != 0) {
+               /*
+                * TODO ;-)
+                */
+               tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
+               return tevent_req_post(req, ev);
+       }
+
+       param = talloc_array(state, uint8_t, 53);
+       if (tevent_req_nomem(param, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
+                                     fname, strlen(fname),
+                                     &converted_len);
+       if (tevent_req_nomem(param, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       SIVAL(param, 0, CreatFlags);
+       SIVAL(param, 4, 0x0);   /* RootDirectoryFid */
+       SIVAL(param, 8, DesiredAccess);
+       SIVAL(param, 12, 0x0);  /* AllocationSize */
+       SIVAL(param, 16, 0x0);  /* AllocationSize */
+       SIVAL(param, 20, FileAttributes);
+       SIVAL(param, 24, ShareAccess);
+       SIVAL(param, 28, CreateDisposition);
+       SIVAL(param, 32, CreateOptions |
+               (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
+       SIVAL(param, 36, secdesc_len);
+       SIVAL(param, 40, 0);     /* EA length*/
+       SIVAL(param, 44, converted_len);
+       SIVAL(param, 48, 0x02); /* ImpersonationLevel */
+       SCVAL(param, 52, SecurityFlags);
+
+       subreq = cli_trans_send(state, ev, cli, SMBnttrans,
+                               NULL, -1, /* name, fid */
+                               NT_TRANSACT_CREATE, 0,
+                               NULL, 0, 0, /* setup */
+                               param, talloc_get_size(param), 128, /* param */
+                               secdesc_buf, secdesc_len, 0); /* data */
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
+       return req;
+}
+
+static void cli_nttrans_create_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_nttrans_create_state *state = tevent_req_data(
+               req, struct cli_nttrans_create_state);
+       uint8_t *param;
+       uint32_t num_param;
+       NTSTATUS status;
+
+       status = cli_trans_recv(subreq, talloc_tos(), NULL,
+                               NULL, 0, NULL, /* rsetup */
+                               &param, 69, &num_param,
+                               NULL, 0, NULL);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+       state->fnum = SVAL(param, 2);
+       TALLOC_FREE(param);
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
+{
+       struct cli_nttrans_create_state *state = tevent_req_data(
+               req, struct cli_nttrans_create_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *fnum = state->fnum;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_nttrans_create(struct cli_state *cli,
+                           const char *fname,
+                           uint32_t CreatFlags,
+                           uint32_t DesiredAccess,
+                           uint32_t FileAttributes,
+                           uint32_t ShareAccess,
+                           uint32_t CreateDisposition,
+                           uint32_t CreateOptions,
+                           uint8_t SecurityFlags,
+                           struct security_descriptor *secdesc,
+                           struct ea_struct *eas,
+                           int num_eas,
+                           uint16_t *pfid)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
+                                     DesiredAccess, FileAttributes,
+                                     ShareAccess, CreateDisposition,
+                                     CreateOptions, SecurityFlags,
+                                     secdesc, eas, num_eas);
+       if (req == NULL) {
+               goto fail;
        }
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+       status = cli_nttrans_create_recv(req, pfid);
+ fail:
+       TALLOC_FREE(frame);
        return status;
 }
 
@@ -2052,28 +2112,29 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
  WARNING: if you open with O_WRONLY then getattrE won't work!
 ****************************************************************************/
 
-struct cli_open_state {
+struct cli_openx_state {
+       const char *fname;
        uint16_t vwv[15];
        uint16_t fnum;
        struct iovec bytes;
 };
 
-static void cli_open_done(struct tevent_req *subreq);
+static void cli_openx_done(struct tevent_req *subreq);
 
-struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
+struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
                                   struct event_context *ev,
                                   struct cli_state *cli, const char *fname,
                                   int flags, int share_mode,
                                   struct tevent_req **psmbreq)
 {
        struct tevent_req *req, *subreq;
-       struct cli_open_state *state;
+       struct cli_openx_state *state;
        unsigned openfn;
        unsigned accessmode;
        uint8_t additional_flags;
        uint8_t *bytes;
 
-       req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
+       req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
        if (req == NULL) {
                return NULL;
        }
@@ -2112,7 +2173,7 @@ struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
        SSVAL(state->vwv + 1, 0, 0);
        SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
        SSVAL(state->vwv + 3, 0, accessmode);
-       SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
+       SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        SSVAL(state->vwv + 5, 0, 0);
        SIVAL(state->vwv + 6, 0, 0);
        SSVAL(state->vwv + 8, 0, openfn);
@@ -2131,7 +2192,7 @@ struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
        }
 
        bytes = talloc_array(state, uint8_t, 0);
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
                                   strlen(fname)+1, NULL);
 
        if (tevent_req_nomem(bytes, req)) {
@@ -2147,38 +2208,37 @@ struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
                TALLOC_FREE(req);
                return NULL;
        }
-       tevent_req_set_callback(subreq, cli_open_done, req);
+       tevent_req_set_callback(subreq, cli_openx_done, req);
        *psmbreq = subreq;
        return req;
 }
 
-struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
+struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
                                 struct cli_state *cli, const char *fname,
                                 int flags, int share_mode)
 {
        struct tevent_req *req, *subreq;
        NTSTATUS status;
 
-       req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
+       req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
                              &subreq);
        if (req == NULL) {
                return NULL;
        }
 
-       status = cli_smb_req_send(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       status = smb1cli_req_chain_submit(&subreq, 1);
+       if (tevent_req_nterror(req, status)) {
                return tevent_req_post(req, ev);
        }
        return req;
 }
 
-static void cli_open_done(struct tevent_req *subreq)
+static void cli_openx_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
                subreq, struct tevent_req);
-       struct cli_open_state *state = tevent_req_data(
-               req, struct cli_open_state);
+       struct cli_openx_state *state = tevent_req_data(
+               req, struct cli_openx_state);
        uint8_t wct;
        uint16_t *vwv;
        uint8_t *inbuf;
@@ -2187,18 +2247,17 @@ static void cli_open_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        state->fnum = SVAL(vwv+2, 0);
        tevent_req_done(req);
 }
 
-NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
+NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
 {
-       struct cli_open_state *state = tevent_req_data(
-               req, struct cli_open_state);
+       struct cli_openx_state *state = tevent_req_data(
+               req, struct cli_openx_state);
        NTSTATUS status;
 
        if (tevent_req_is_nterror(req, &status)) {
@@ -2208,7 +2267,7 @@ NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
        return NT_STATUS_OK;
 }
 
-NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
+NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
             int share_mode, uint16_t *pfnum)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -2216,7 +2275,7 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2230,7 +2289,7 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
                goto fail;
        }
 
-       req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
+       req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
        if (req == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2241,13 +2300,125 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
                goto fail;
        }
 
-       status = cli_open_recv(req, pfnum);
+       status = cli_openx_recv(req, pfnum);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
+       return status;
+}
+/****************************************************************************
+ Synchronous wrapper function that does an NtCreateX open by preference
+ and falls back to openX if this fails.
+****************************************************************************/
+
+NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
+                       int share_mode_in, uint16_t *pfnum)
+{
+       NTSTATUS status;
+       unsigned int openfn = 0;
+       unsigned int dos_deny = 0;
+       uint32_t access_mask, share_mode, create_disposition, create_options;
+
+       /* Do the initial mapping into OpenX parameters. */
+       if (flags & O_CREAT) {
+               openfn |= (1<<4);
+       }
+       if (!(flags & O_EXCL)) {
+               if (flags & O_TRUNC)
+                       openfn |= (1<<1);
+               else
+                       openfn |= (1<<0);
+       }
+
+       dos_deny = (share_mode_in<<4);
+
+       if ((flags & O_ACCMODE) == O_RDWR) {
+               dos_deny |= 2;
+       } else if ((flags & O_ACCMODE) == O_WRONLY) {
+               dos_deny |= 1;
+       }
+
+#if defined(O_SYNC)
+       if ((flags & O_SYNC) == O_SYNC) {
+               dos_deny |= (1<<14);
+       }
+#endif /* O_SYNC */
+
+       if (share_mode_in == DENY_FCB) {
+               dos_deny = 0xFF;
+       }
+
+#if 0
+       /* Hmmm. This is what I think the above code
+          should look like if it's using the constants
+          we #define. JRA. */
+
+       if (flags & O_CREAT) {
+               openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
+       }
+       if (!(flags & O_EXCL)) {
+               if (flags & O_TRUNC)
+                       openfn |= OPENX_FILE_EXISTS_TRUNCATE;
+               else
+                       openfn |= OPENX_FILE_EXISTS_OPEN;
+       }
+
+       dos_deny = SET_DENY_MODE(share_mode_in);
+
+       if ((flags & O_ACCMODE) == O_RDWR) {
+               dos_deny |= DOS_OPEN_RDWR;
+       } else if ((flags & O_ACCMODE) == O_WRONLY) {
+               dos_deny |= DOS_OPEN_WRONLY;
+       }
+
+#if defined(O_SYNC)
+       if ((flags & O_SYNC) == O_SYNC) {
+               dos_deny |= FILE_SYNC_OPENMODE;
+       }
+#endif /* O_SYNC */
+
+       if (share_mode_in == DENY_FCB) {
+               dos_deny = 0xFF;
+       }
+#endif
+
+       if (!map_open_params_to_ntcreate(fname, dos_deny,
+                                       openfn, &access_mask,
+                                       &share_mode, &create_disposition,
+                                       &create_options, NULL)) {
+               goto try_openx;
        }
+
+       status = cli_ntcreate(cli,
+                               fname,
+                               0,
+                               access_mask,
+                               0,
+                               share_mode,
+                               create_disposition,
+                               create_options,
+                               0,
+                               pfnum);
+
+       /* Try and cope will all varients of "we don't do this call"
+          and fall back to openX. */
+
+       if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
+               goto try_openx;
+       }
+
        return status;
+
+  try_openx:
+
+       return cli_openx(cli, fname, flags, share_mode_in, pfnum);
 }
 
 /****************************************************************************
@@ -2301,9 +2472,8 @@ struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       status = cli_smb_req_send(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       status = smb1cli_req_chain_submit(&subreq, 1);
+       if (tevent_req_nterror(req, status)) {
                return tevent_req_post(req, ev);
        }
        return req;
@@ -2317,8 +2487,7 @@ static void cli_close_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -2336,7 +2505,7 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2364,9 +2533,6 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
        status = cli_close_recv(req);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2449,7 +2615,7 @@ NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2482,9 +2648,6 @@ NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2500,7 +2663,8 @@ NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
        uint16_t vwv[8];
        uint8_t bytes[10];
        NTSTATUS status;
-       int saved_timeout;
+       unsigned int set_timeout = 0;
+       unsigned int saved_timeout = 0;
 
        SCVAL(vwv + 0, 0, 0xff);
        SCVAL(vwv + 0, 1, 0);
@@ -2512,21 +2676,25 @@ NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
        SSVAL(vwv + 6, 0, 0);
        SSVAL(vwv + 7, 0, 1);
 
-       SSVAL(bytes, 0, cli->pid);
+       SSVAL(bytes, 0, cli_getpid(cli));
        SIVAL(bytes, 2, offset);
        SIVAL(bytes, 6, len);
 
-       saved_timeout = cli->timeout;
-
        if (timeout != 0) {
-               cli->timeout = (timeout == -1)
-                       ? 0x7FFFFFFF : (timeout + 2*1000);
+               if (timeout == -1) {
+                       set_timeout = 0x7FFFFFFF;
+               } else {
+                       set_timeout = timeout + 2*1000;
+               }
+               saved_timeout = cli_set_timeout(cli, set_timeout);
        }
 
        status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
                         10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
 
-       cli->timeout = saved_timeout;
+       if (saved_timeout != 0) {
+               cli_set_timeout(cli, saved_timeout);
+       }
 
        return status;
 }
@@ -2536,7 +2704,7 @@ NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
  note that timeout is in units of 2 milliseconds
 ****************************************************************************/
 
-bool cli_lock(struct cli_state *cli, uint16_t fnum,
+NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
                  uint32_t offset, uint32_t len, int timeout,
                  enum brl_type lock_type)
 {
@@ -2544,8 +2712,7 @@ bool cli_lock(struct cli_state *cli, uint16_t fnum,
 
        status = cli_locktype(cli, fnum, offset, len, timeout,
                              (lock_type == READ_LOCK? 1 : 0));
-       cli_set_error(cli, status);
-       return NT_STATUS_IS_OK(status);
+       return status;
 }
 
 /****************************************************************************
@@ -2583,7 +2750,7 @@ struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->vwv+6, 0, 1);
        SSVAL(state->vwv+7, 0, 0);
 
-       SSVAL(state->data, 0, cli->pid);
+       SSVAL(state->data, 0, cli_getpid(cli));
        SIVAL(state->data, 2, offset);
        SIVAL(state->data, 6, len);
 
@@ -2604,8 +2771,7 @@ static void cli_unlock_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -2626,7 +2792,7 @@ NTSTATUS cli_unlock(struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2656,9 +2822,6 @@ NTSTATUS cli_unlock(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2666,61 +2829,55 @@ NTSTATUS cli_unlock(struct cli_state *cli,
  Lock a file with 64 bit offsets.
 ****************************************************************************/
 
-bool cli_lock64(struct cli_state *cli, uint16_t fnum,
-               uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
+NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
+                   uint64_t offset, uint64_t len, int timeout,
+                   enum brl_type lock_type)
 {
-       char *p;
-        int saved_timeout = cli->timeout;
+       uint16_t vwv[8];
+       uint8_t bytes[20];
+       unsigned int set_timeout = 0;
+       unsigned int saved_timeout = 0;
        int ltype;
+       NTSTATUS status;
 
-       if (! (cli->capabilities & CAP_LARGE_FILES)) {
-               return cli_lock(cli, fnum, offset, len, timeout, lock_type);
+       if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
+               return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
        }
 
        ltype = (lock_type == READ_LOCK? 1 : 0);
        ltype |= LOCKING_ANDX_LARGE_FILES;
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0', smb_size);
-
-       cli_set_message(cli->outbuf,8,0,True);
-
-       SCVAL(cli->outbuf,smb_com,SMBlockingX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
-
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-       SCVAL(cli->outbuf,smb_vwv3,ltype);
-       SIVALS(cli->outbuf, smb_vwv4, timeout);
-       SSVAL(cli->outbuf,smb_vwv6,0);
-       SSVAL(cli->outbuf,smb_vwv7,1);
-
-       p = smb_buf(cli->outbuf);
-       SIVAL(p, 0, cli->pid);
-       SOFF_T_R(p, 4, offset);
-       SOFF_T_R(p, 12, len);
-       p += 20;
+       SCVAL(vwv + 0, 0, 0xff);
+       SCVAL(vwv + 0, 1, 0);
+       SSVAL(vwv + 1, 0, 0);
+       SSVAL(vwv + 2, 0, fnum);
+       SCVAL(vwv + 3, 0, ltype);
+       SCVAL(vwv + 3, 1, 0);
+       SIVALS(vwv + 4, 0, timeout);
+       SSVAL(vwv + 6, 0, 0);
+       SSVAL(vwv + 7, 0, 1);
 
-       cli_setup_bcc(cli, p);
-       cli_send_smb(cli);
+       SIVAL(bytes, 0, cli_getpid(cli));
+       SOFF_T_R(bytes, 4, offset);
+       SOFF_T_R(bytes, 12, len);
 
        if (timeout != 0) {
-               cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
-       }
-
-       if (!cli_receive_smb(cli)) {
-                cli->timeout = saved_timeout;
-               return False;
+               if (timeout == -1) {
+                       set_timeout = 0x7FFFFFFF;
+               } else {
+                       set_timeout = timeout + 2*1000;
+               }
+               saved_timeout = cli_set_timeout(cli, set_timeout);
        }
 
-       cli->timeout = saved_timeout;
+       status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
+                        20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
 
-       if (cli_is_error(cli)) {
-               return False;
+       if (saved_timeout != 0) {
+               cli_set_timeout(cli, saved_timeout);
        }
 
-       return True;
+       return status;
 }
 
 /****************************************************************************
@@ -2758,7 +2915,7 @@ struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->vwv+6, 0, 1);
        SSVAL(state->vwv+7, 0, 0);
 
-       SIVAL(state->data, 0, cli->pid);
+       SIVAL(state->data, 0, cli_getpid(cli));
        SOFF_T_R(state->data, 4, offset);
        SOFF_T_R(state->data, 12, len);
 
@@ -2779,8 +2936,7 @@ static void cli_unlock64_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -2801,11 +2957,11 @@ NTSTATUS cli_unlock64(struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (! (cli->capabilities & CAP_LARGE_FILES)) {
+       if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
                return cli_unlock(cli, fnum, offset, len);
        }
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2835,9 +2991,6 @@ NTSTATUS cli_unlock64(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2908,7 +3061,7 @@ static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
                                POSIX_LOCK_FLAG_NOWAIT);
        }
 
-       SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
+       SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
        SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
        SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
 
@@ -2968,7 +3121,7 @@ NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3009,9 +3162,6 @@ NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3042,7 +3192,7 @@ NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3076,9 +3226,6 @@ NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3092,7 +3239,7 @@ struct cli_getattrE_state {
        uint16_t vwv[1];
        int zone_offset;
        uint16_t attr;
-       SMB_OFF_T size;
+       off_t size;
        time_t change_time;
        time_t access_time;
        time_t write_time;
@@ -3112,7 +3259,7 @@ struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       state->zone_offset = cli->serverzone;
+       state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
        SSVAL(state->vwv+0,0,fnum);
 
        subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
@@ -3138,12 +3285,11 @@ static void cli_getattrE_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
                              NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
-       state->size = (SMB_OFF_T)IVAL(vwv+6,0);
+       state->size = (off_t)IVAL(vwv+6,0);
        state->attr = SVAL(vwv+10,0);
        state->change_time = make_unix_date2(vwv+0, state->zone_offset);
        state->access_time = make_unix_date2(vwv+2, state->zone_offset);
@@ -3154,7 +3300,7 @@ static void cli_getattrE_done(struct tevent_req *subreq)
 
 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
                        uint16_t *attr,
-                       SMB_OFF_T *size,
+                       off_t *size,
                        time_t *change_time,
                        time_t *access_time,
                        time_t *write_time)
@@ -3187,7 +3333,7 @@ NTSTATUS cli_getattrE_recv(struct tevent_req *req,
 NTSTATUS cli_getattrE(struct cli_state *cli,
                        uint16_t fnum,
                        uint16_t *attr,
-                       SMB_OFF_T *size,
+                       off_t *size,
                        time_t *change_time,
                        time_t *access_time,
                        time_t *write_time)
@@ -3197,7 +3343,7 @@ NTSTATUS cli_getattrE(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3231,9 +3377,6 @@ NTSTATUS cli_getattrE(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3246,7 +3389,7 @@ static void cli_getatr_done(struct tevent_req *subreq);
 struct cli_getatr_state {
        int zone_offset;
        uint16_t attr;
-       SMB_OFF_T size;
+       off_t size;
        time_t write_time;
 };
 
@@ -3265,14 +3408,14 @@ struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       state->zone_offset = cli->serverzone;
+       state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
 
        bytes = talloc_array(state, uint8_t, 1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
                                   strlen(fname)+1, NULL);
 
        if (tevent_req_nomem(bytes, req)) {
@@ -3302,13 +3445,12 @@ static void cli_getatr_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
        state->attr = SVAL(vwv+0,0);
-       state->size = (SMB_OFF_T)IVAL(vwv+3,0);
+       state->size = (off_t)IVAL(vwv+3,0);
        state->write_time = make_unix_date3(vwv+1, state->zone_offset);
 
        tevent_req_done(req);
@@ -3316,7 +3458,7 @@ static void cli_getatr_done(struct tevent_req *subreq)
 
 NTSTATUS cli_getatr_recv(struct tevent_req *req,
                        uint16_t *attr,
-                       SMB_OFF_T *size,
+                       off_t *size,
                        time_t *write_time)
 {
        struct cli_getatr_state *state = tevent_req_data(
@@ -3341,7 +3483,7 @@ NTSTATUS cli_getatr_recv(struct tevent_req *req,
 NTSTATUS cli_getatr(struct cli_state *cli,
                        const char *fname,
                        uint16_t *attr,
-                       SMB_OFF_T *size,
+                       off_t *size,
                        time_t *write_time)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -3349,7 +3491,7 @@ NTSTATUS cli_getatr(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3381,9 +3523,6 @@ NTSTATUS cli_getatr(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3416,11 +3555,11 @@ struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
 
        SSVAL(state->vwv+0, 0, fnum);
        push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
-                      cli->serverzone);
+                      smb1cli_conn_server_time_zone(cli->conn));
        push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
-                      cli->serverzone);
+                      smb1cli_conn_server_time_zone(cli->conn));
        push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
-                      cli->serverzone);
+                      smb1cli_conn_server_time_zone(cli->conn));
 
        subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
                              7, state->vwv, 0, NULL);
@@ -3439,8 +3578,7 @@ static void cli_setattrE_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -3462,7 +3600,7 @@ NTSTATUS cli_setattrE(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3497,9 +3635,6 @@ NTSTATUS cli_setattrE(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3531,26 +3666,26 @@ struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
        }
 
        SSVAL(state->vwv+0, 0, attr);
-       push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
+       push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
 
        bytes = talloc_array(state, uint8_t, 1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
                                   strlen(fname)+1, NULL);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
-       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+       bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
 
        bytes[talloc_get_size(bytes)-1] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
                                   1, NULL);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -3573,8 +3708,7 @@ static void cli_setatr_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -3595,7 +3729,7 @@ NTSTATUS cli_setatr(struct cli_state *cli,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3624,9 +3758,6 @@ NTSTATUS cli_setatr(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3660,7 +3791,7 @@ struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
                                   strlen(fname)+1, NULL);
 
        if (tevent_req_nomem(bytes, req)) {
@@ -3684,8 +3815,7 @@ static void cli_chkpath_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -3704,7 +3834,7 @@ NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
        char *path2 = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3747,9 +3877,6 @@ NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3801,8 +3928,7 @@ static void cli_dskattr_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
@@ -3833,7 +3959,7 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3862,9 +3988,6 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3903,7 +4026,7 @@ struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
        bytes[0] = 4;
-       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
+       bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
                                   strlen(path)+1, NULL);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -3934,8 +4057,7 @@ static void cli_ctemp_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
                              &num_bytes, &bytes);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
@@ -3991,7 +4113,7 @@ NTSTATUS cli_ctemp(struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -4020,9 +4142,6 @@ NTSTATUS cli_ctemp(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4067,7 +4186,9 @@ static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
 
        if (ea_namelen == 0 && ea_len == 0) {
                data_len = 4;
-               data = (uint8_t *)SMB_MALLOC(data_len);
+               data = talloc_array(talloc_tos(),
+                               uint8_t,
+                               data_len);
                if (!data) {
                        return NT_STATUS_NO_MEMORY;
                }
@@ -4075,7 +4196,9 @@ static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
                SIVAL(p,0,data_len);
        } else {
                data_len = 4 + 4 + ea_namelen + 1 + ea_len;
-               data = (uint8_t *)SMB_MALLOC(data_len);
+               data = talloc_array(talloc_tos(),
+                               uint8_t,
+                               data_len);
                if (!data) {
                        return NT_STATUS_NO_MEMORY;
                }
@@ -4092,12 +4215,12 @@ static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
        status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
                           setup, 1, 0,
                           param, param_len, 2,
-                          data,  data_len, cli->max_xmit,
+                          data,  data_len, CLI_BUFFER_SIZE,
                           NULL,
                           NULL, 0, NULL, /* rsetup */
                           NULL, 0, NULL, /* rparam */
                           NULL, 0, NULL); /* rdata */
-       SAFE_FREE(data);
+       talloc_free(data);
        return status;
 }
 
@@ -4111,24 +4234,25 @@ NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
 {
        unsigned int param_len = 0;
        uint8_t *param;
-       size_t srclen = 2*(strlen(path)+1);
-       char *p;
        NTSTATUS status;
+       TALLOC_CTX *frame = talloc_stackframe();
 
-       param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
+       param = talloc_array(talloc_tos(), uint8_t, 6);
        if (!param) {
                return NT_STATUS_NO_MEMORY;
        }
-       memset(param, '\0', 6);
        SSVAL(param,0,SMB_INFO_SET_EA);
-       p = (char *)(&param[6]);
+       SSVAL(param,2,0);
+       SSVAL(param,4,0);
 
-       p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
+       param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
+                                     path, strlen(path)+1,
+                                     NULL);
+       param_len = talloc_get_size(param);
 
        status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
                            ea_name, ea_val, ea_len);
-       SAFE_FREE(param);
+       talloc_free(frame);
        return status;
 }
 
@@ -4208,7 +4332,7 @@ static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
                return true;
        }
 
-       ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
+       ea_list = talloc_array(ctx, struct ea_struct, num_eas);
        if (!ea_list) {
                return false;
        }
@@ -4224,7 +4348,7 @@ static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
 
                ea->flags = CVAL(p,0);
                unix_ea_name[0] = '\0';
-               pull_ascii_fstring(unix_ea_name, p + 4);
+               pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
                ea->name = talloc_strdup(ea_list, unix_ea_name);
                if (!ea->name) {
                        goto fail;
@@ -4276,7 +4400,7 @@ struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
        }
        subreq = cli_qpathinfo_send(state, ev, cli, fname,
                                    SMB_INFO_QUERY_ALL_EAS, 4,
-                                   cli->max_xmit);
+                                   CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -4295,8 +4419,7 @@ static void cli_get_ea_list_path_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -4329,7 +4452,7 @@ NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -4350,9 +4473,6 @@ NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
        status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4402,7 +4522,6 @@ static uint32_t open_flags_to_wire(int flags)
 #endif
 #if defined(O_DIRECTORY)
        if (flags & O_DIRECTORY) {
-               ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
                ret |= SMB_O_DIRECTORY;
        }
 #endif
@@ -4432,8 +4551,7 @@ static void cli_posix_open_internal_done(struct tevent_req *subreq)
        status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
                                NULL, 0, NULL, &data, 12, &num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        state->fnum = SVAL(data,2);
@@ -4468,7 +4586,7 @@ static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
        memset(state->param, '\0', 6);
        SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
 
-       state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
+       state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
                                   strlen(fname)+1, NULL);
 
        if (tevent_req_nomem(state->param, req)) {
@@ -4477,7 +4595,6 @@ static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
 
        /* Setup data words. */
        if (is_dir) {
-               wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
                wire_flags |= SMB_O_DIRECTORY;
        }
 
@@ -4548,7 +4665,7 @@ NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -4582,9 +4699,6 @@ NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4610,7 +4724,7 @@ NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -4643,9 +4757,6 @@ NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4719,7 +4830,7 @@ NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -4751,9 +4862,6 @@ NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4783,7 +4891,7 @@ NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -4815,9 +4923,6 @@ NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4841,6 +4946,7 @@ struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req, *subreq;
        struct cli_notify_state *state;
+       unsigned old_timeout;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
        if (req == NULL) {
@@ -4851,6 +4957,11 @@ struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->setup, 4, fnum);
        SSVAL(state->setup, 6, recursive);
 
+       /*
+        * Notifies should not time out
+        */
+       old_timeout = cli_set_timeout(cli, 0);
+
        subreq = cli_trans_send(
                state,                  /* mem ctx. */
                ev,                     /* event ctx. */
@@ -4870,6 +4981,8 @@ struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
                0,                      /* num data. */
                0);                     /* max returned data. */
 
+       cli_set_timeout(cli, old_timeout);
+
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -4891,9 +5004,8 @@ static void cli_notify_done(struct tevent_req *subreq)
        status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
                                &params, 0, &num_params, NULL, 0, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (tevent_req_nterror(req, status)) {
                DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
-               tevent_req_nterror(req, status);
                return;
        }
 
@@ -4901,13 +5013,13 @@ static void cli_notify_done(struct tevent_req *subreq)
        ofs = 0;
 
        while (num_params - ofs > 12) {
-               uint32_t len = IVAL(params, ofs);
+               uint32_t next = IVAL(params, ofs);
                state->num_changes += 1;
 
-               if ((len == 0) || (ofs+len >= num_params)) {
+               if ((next == 0) || (ofs+next >= num_params)) {
                        break;
                }
-               ofs += len;
+               ofs += next;
        }
 
        state->changes = talloc_array(state, struct notify_change,
@@ -4925,7 +5037,7 @@ static void cli_notify_done(struct tevent_req *subreq)
                ssize_t ret;
                char *name;
 
-               if ((next != 0) && (len+12 != next)) {
+               if (trans_oob(num_params, ofs + 12, len)) {
                        TALLOC_FREE(params);
                        tevent_req_nterror(
                                req, NT_STATUS_INVALID_NETWORK_RESPONSE);
@@ -4966,6 +5078,41 @@ NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
+NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
+                   uint32_t completion_filter, bool recursive,
+                   TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
+                   struct notify_change **pchanges)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+       ev = tevent_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
+                             completion_filter, recursive);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+       status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
 struct cli_qpathinfo_state {
        uint8_t *param;
        uint8_t *data;
@@ -4999,7 +5146,7 @@ struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
        }
        SSVAL(state->param, 0, level);
        state->param = trans2_bytes_push_str(
-               state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
+               state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
        if (tevent_req_nomem(state->param, req)) {
                return tevent_req_post(req, ev);
        }
@@ -5042,8 +5189,7 @@ static void cli_qpathinfo_done(struct tevent_req *subreq)
                                NULL, 0, NULL,
                                &state->rdata, state->min_rdata,
                                &state->num_rdata);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -5080,7 +5226,7 @@ NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -5102,9 +5248,6 @@ NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -5112,6 +5255,7 @@ struct cli_qfileinfo_state {
        uint16_t setup[1];
        uint8_t param[4];
        uint8_t *data;
+       uint16_t recv_flags2;
        uint32_t min_rdata;
        uint8_t *rdata;
        uint32_t num_rdata;
@@ -5171,18 +5315,20 @@ static void cli_qfileinfo_done(struct tevent_req *subreq)
                req, struct cli_qfileinfo_state);
        NTSTATUS status;
 
-       status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
+       status = cli_trans_recv(subreq, state,
+                               &state->recv_flags2,
+                               NULL, 0, NULL,
                                NULL, 0, NULL,
                                &state->rdata, state->min_rdata,
                                &state->num_rdata);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
 }
 
 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+                           uint16_t *recv_flags2,
                            uint8_t **rdata, uint32_t *num_rdata)
 {
        struct cli_qfileinfo_state *state = tevent_req_data(
@@ -5192,6 +5338,10 @@ NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
+
+       if (recv_flags2 != NULL) {
+               *recv_flags2 = state->recv_flags2;
+       }
        if (rdata != NULL) {
                *rdata = talloc_move(mem_ctx, &state->rdata);
        } else {
@@ -5205,7 +5355,7 @@ NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
                       uint16_t fnum, uint16_t level, uint32_t min_rdata,
-                      uint32_t max_rdata,
+                      uint32_t max_rdata, uint16_t *recv_flags2,
                       uint8_t **rdata, uint32_t *num_rdata)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -5213,7 +5363,7 @@ NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -5232,12 +5382,9 @@ NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
-       status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
+       status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -5278,8 +5425,7 @@ static void cli_flush_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -5297,7 +5443,7 @@ NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -5318,9 +5464,6 @@ NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
        status = cli_flush_recv(req);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -5349,7 +5492,7 @@ struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        state->get_names = get_names;
-       ret_size = get_names ? cli->max_xmit : 16;
+       ret_size = get_names ? CLI_BUFFER_SIZE : 16;
 
        SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
        SSVAL(state->setup + 2, 0, fnum);
@@ -5381,8 +5524,7 @@ static void cli_shadow_copy_data_done(struct tevent_req *subreq)
                                NULL, 0, NULL, /* param */
                                &state->data, 12, &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -5426,7 +5568,7 @@ NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                ret = convert_string_talloc(
                        names, CH_UTF16LE, CH_UNIX,
                        src, 2 * sizeof(SHADOW_COPY_LABEL),
-                       &names[i], &converted_size, True);
+                       &names[i], &converted_size);
                if (!ret) {
                        TALLOC_FREE(names);
                        return NT_STATUS_INVALID_NETWORK_RESPONSE;
@@ -5446,7 +5588,7 @@ NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -5467,8 +5609,5 @@ NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }