s3-net: display full value of "msDS-SupportedEncryptionTypes".
[sfrench/samba-autobuild/.git] / source3 / libsmb / clifile.c
index 1bf72aaf8fb8e556e79eb86c4ccd0c19d5115476..61cb8b598930152d9c5fdd8e92c7a5cf94ef6652 100644 (file)
@@ -26,6 +26,8 @@
 #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()
@@ -48,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;
                }
@@ -67,7 +71,7 @@ static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
                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);
@@ -108,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;
@@ -125,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;
@@ -161,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);
        }
@@ -222,14 +245,14 @@ 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
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = tevent_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -258,7 +281,7 @@ struct cli_posix_link_internal_state {
 static void cli_posix_link_internal_done(struct tevent_req *subreq);
 
 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        uint16_t level,
                                        const char *oldname,
@@ -279,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,
@@ -302,7 +325,7 @@ static void cli_posix_link_internal_done(struct tevent_req *subreq)
 ****************************************************************************/
 
 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *oldname,
                                        const char *newname)
@@ -321,11 +344,11 @@ NTSTATUS cli_posix_symlink(struct cli_state *cli,
                        const char *newname)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -333,7 +356,7 @@ NTSTATUS cli_posix_symlink(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -373,14 +396,14 @@ struct readlink_state {
 static void cli_posix_readlink_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        size_t len)
 {
        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) {
@@ -441,7 +464,7 @@ 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,
@@ -462,11 +485,11 @@ NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
                                char *linkpath, size_t len)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -474,7 +497,7 @@ NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -507,7 +530,7 @@ NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
 ****************************************************************************/
 
 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *oldname,
                                        const char *newname)
@@ -526,11 +549,11 @@ NTSTATUS cli_posix_hardlink(struct cli_state *cli,
                        const char *newname)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -538,7 +561,7 @@ NTSTATUS cli_posix_hardlink(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -578,7 +601,7 @@ struct getfacl_state {
 static void cli_posix_getfacl_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname)
 {
@@ -590,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);
        }
@@ -638,11 +661,11 @@ NTSTATUS cli_posix_getfacl(struct cli_state *cli,
                        char **retbuf)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -650,7 +673,7 @@ NTSTATUS cli_posix_getfacl(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -689,7 +712,7 @@ struct stat_state {
 static void cli_posix_stat_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname)
 {
@@ -769,11 +792,11 @@ NTSTATUS cli_posix_stat(struct cli_state *cli,
                        SMB_STRUCT_STAT *sbuf)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -781,7 +804,7 @@ NTSTATUS cli_posix_stat(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -819,7 +842,7 @@ struct cli_posix_chown_chmod_internal_state {
 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
 
 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        uint32_t mode,
@@ -862,7 +885,7 @@ static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
 ****************************************************************************/
 
 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        mode_t mode)
@@ -882,11 +905,11 @@ NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -894,7 +917,7 @@ NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -927,7 +950,7 @@ NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
 ****************************************************************************/
 
 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        uid_t uid,
@@ -951,11 +974,11 @@ NTSTATUS cli_posix_chown(struct cli_state *cli,
                        gid_t gid)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -963,7 +986,7 @@ NTSTATUS cli_posix_chown(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1003,7 +1026,7 @@ struct cli_rename_state {
 };
 
 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *fname_src,
                                const char *fname_dst)
@@ -1025,20 +1048,20 @@ struct tevent_req *cli_rename_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);
@@ -1074,12 +1097,20 @@ NTSTATUS cli_rename_recv(struct tevent_req *req)
 
 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_rename(cli,
+                                       fname_src,
+                                       fname_dst);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1087,7 +1118,7 @@ NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fn
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1122,7 +1153,7 @@ struct cli_ntrename_internal_state {
 };
 
 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *fname_src,
                                const char *fname_dst,
@@ -1147,20 +1178,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);
@@ -1195,7 +1226,7 @@ static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
 }
 
 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *fname_src,
                                const char *fname_dst)
@@ -1216,11 +1247,11 @@ NTSTATUS cli_ntrename_recv(struct tevent_req *req)
 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
@@ -1228,7 +1259,7 @@ NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1257,7 +1288,7 @@ NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *
 ****************************************************************************/
 
 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *fname_src,
                                const char *fname_dst)
@@ -1278,11 +1309,11 @@ NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
@@ -1290,7 +1321,7 @@ NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const cha
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1325,7 +1356,7 @@ struct cli_unlink_state {
 };
 
 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *fname,
                                uint16_t mayhave_attrs)
@@ -1347,7 +1378,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)) {
@@ -1384,12 +1415,18 @@ NTSTATUS cli_unlink_recv(struct tevent_req *req)
 
 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_unlink(cli, fname);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1397,7 +1434,7 @@ NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_a
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1432,7 +1469,7 @@ struct cli_mkdir_state {
 };
 
 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
-                                 struct event_context *ev,
+                                 struct tevent_context *ev,
                                  struct cli_state *cli,
                                  const char *dname)
 {
@@ -1451,7 +1488,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)) {
@@ -1488,12 +1525,18 @@ NTSTATUS cli_mkdir_recv(struct tevent_req *req)
 
 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_mkdir(cli, dname);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1501,7 +1544,7 @@ NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1536,7 +1579,7 @@ struct cli_rmdir_state {
 };
 
 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
-                                 struct event_context *ev,
+                                 struct tevent_context *ev,
                                  struct cli_state *cli,
                                  const char *dname)
 {
@@ -1555,7 +1598,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)) {
@@ -1592,12 +1635,18 @@ NTSTATUS cli_rmdir_recv(struct tevent_req *req)
 
 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_rmdir(cli, dname);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1605,7 +1654,7 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1647,7 +1696,7 @@ static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
 }
 
 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        uint16_t fnum,
                                        bool flag)
@@ -1703,11 +1752,11 @@ NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -1715,7 +1764,7 @@ NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1743,32 +1792,35 @@ NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
        return status;
 }
 
-struct cli_ntcreate_state {
+struct cli_ntcreate1_state {
        uint16_t vwv[24];
        uint16_t fnum;
+       struct smb_create_returns cr;
+       struct tevent_req *subreq;
 };
 
-static void cli_ntcreate_done(struct tevent_req *subreq);
+static void cli_ntcreate1_done(struct tevent_req *subreq);
+static bool cli_ntcreate1_cancel(struct tevent_req *req);
 
-struct tevent_req *cli_ntcreate_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)
+static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
+                                            struct tevent_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 tevent_req *req, *subreq;
-       struct cli_ntcreate_state *state;
+       struct cli_ntcreate1_state *state;
        uint16_t *vwv;
        uint8_t *bytes;
        size_t converted_len;
 
-       req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
+       req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
        if (req == NULL) {
                return NULL;
        }
@@ -1791,17 +1843,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);
@@ -1814,34 +1867,156 @@ struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, cli_ntcreate_done, req);
+       tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
+
+       state->subreq = subreq;
+       tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
+
        return req;
 }
 
-static void cli_ntcreate_done(struct tevent_req *subreq)
+static void cli_ntcreate1_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
                subreq, struct tevent_req);
-       struct cli_ntcreate_state *state = tevent_req_data(
-               req, struct cli_ntcreate_state);
+       struct cli_ntcreate1_state *state = tevent_req_data(
+               req, struct cli_ntcreate1_state);
        uint8_t wct;
        uint16_t *vwv;
        uint32_t num_bytes;
        uint8_t *bytes;
-       uint8_t *inbuf;
        NTSTATUS status;
 
-       status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
+       status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
                              &num_bytes, &bytes);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
                return;
        }
+       state->cr.oplock_level = CVAL(vwv+2, 0);
        state->fnum = SVAL(vwv+2, 1);
+       state->cr.create_action = IVAL(vwv+3, 1);
+       state->cr.creation_time = BVAL(vwv+5, 1);
+       state->cr.last_access_time = BVAL(vwv+9, 1);
+       state->cr.last_write_time = BVAL(vwv+13, 1);
+       state->cr.change_time   = BVAL(vwv+17, 1);
+       state->cr.file_attributes = IVAL(vwv+21, 1);
+       state->cr.allocation_size = BVAL(vwv+23, 1);
+       state->cr.end_of_file   = BVAL(vwv+27, 1);
+
        tevent_req_done(req);
 }
 
-NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
+static bool cli_ntcreate1_cancel(struct tevent_req *req)
+{
+       struct cli_ntcreate1_state *state = tevent_req_data(
+               req, struct cli_ntcreate1_state);
+       return tevent_req_cancel(state->subreq);
+}
+
+static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
+                                  uint16_t *pfnum,
+                                  struct smb_create_returns *cr)
+{
+       struct cli_ntcreate1_state *state = tevent_req_data(
+               req, struct cli_ntcreate1_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *pfnum = state->fnum;
+       if (cr != NULL) {
+               *cr = state->cr;
+       }
+       return NT_STATUS_OK;
+}
+
+struct cli_ntcreate_state {
+       NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
+                        struct smb_create_returns *cr);
+       struct smb_create_returns cr;
+       uint16_t fnum;
+       struct tevent_req *subreq;
+};
+
+static void cli_ntcreate_done(struct tevent_req *subreq);
+static bool cli_ntcreate_cancel(struct tevent_req *req);
+
+struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
+                                    struct tevent_context *ev,
+                                    struct cli_state *cli,
+                                    const char *fname,
+                                    uint32_t create_flags,
+                                    uint32_t desired_access,
+                                    uint32_t file_attributes,
+                                    uint32_t share_access,
+                                    uint32_t create_disposition,
+                                    uint32_t create_options,
+                                    uint8_t security_flags)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_ntcreate_state *state;
+
+       req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               state->recv = cli_smb2_create_fnum_recv;
+
+               if (cli->use_oplocks) {
+                       create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
+               }
+
+               subreq = cli_smb2_create_fnum_send(
+                       state, ev, cli, fname, create_flags, desired_access,
+                       file_attributes, share_access, create_disposition,
+                       create_options);
+       } else {
+               state->recv = cli_ntcreate1_recv;
+               subreq = cli_ntcreate1_send(
+                       state, ev, cli, fname, create_flags, desired_access,
+                       file_attributes, share_access, create_disposition,
+                       create_options, security_flags);
+       }
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_ntcreate_done, req);
+
+       state->subreq = subreq;
+       tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
+
+       return req;
+}
+
+static void cli_ntcreate_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_ntcreate_state *state = tevent_req_data(
+               req, struct cli_ntcreate_state);
+       NTSTATUS status;
+
+       status = state->recv(subreq, &state->fnum, &state->cr);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+       tevent_req_done(req);
+}
+
+static bool cli_ntcreate_cancel(struct tevent_req *req)
+{
+       struct cli_ntcreate_state *state = tevent_req_data(
+               req, struct cli_ntcreate_state);
+       return tevent_req_cancel(state->subreq);
+}
+
+NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
+                          struct smb_create_returns *cr)
 {
        struct cli_ntcreate_state *state = tevent_req_data(
                req, struct cli_ntcreate_state);
@@ -1850,7 +2025,12 @@ NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
        if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
-       *pfnum = state->fnum;
+       if (fnum != NULL) {
+               *fnum = state->fnum;
+       }
+       if (cr != NULL) {
+               *cr = state->cr;
+       }
        return NT_STATUS_OK;
 }
 
@@ -1863,14 +2043,15 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
                      uint32_t CreateDisposition,
                      uint32_t CreateOptions,
                      uint8_t SecurityFlags,
-                     uint16_t *pfid)
+                     uint16_t *pfid,
+                     struct smb_create_returns *cr)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
-       NTSTATUS status = NT_STATUS_OK;
+       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
                 */
@@ -1878,9 +2059,8 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
-               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
@@ -1889,16 +2069,210 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
                                CreateDisposition, CreateOptions,
                                SecurityFlags);
        if (req == NULL) {
-               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
-       if (!tevent_req_poll(req, ev)) {
-               status = map_nt_error_from_unix(errno);
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
 
-       status = cli_ntcreate_recv(req, pfid);
+       status = cli_ntcreate_recv(req, pfid, cr);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
+struct cli_nttrans_create_state {
+       uint16_t fnum;
+       struct smb_create_returns cr;
+};
+
+static void cli_nttrans_create_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
+                                          struct tevent_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->cr.oplock_level = CVAL(param, 0);
+       state->fnum = SVAL(param, 2);
+       state->cr.create_action = IVAL(param, 4);
+       state->cr.creation_time = BVAL(param, 12);
+       state->cr.last_access_time = BVAL(param, 20);
+       state->cr.last_write_time = BVAL(param, 28);
+       state->cr.change_time   = BVAL(param, 36);
+       state->cr.file_attributes = IVAL(param, 44);
+       state->cr.allocation_size = BVAL(param, 48);
+       state->cr.end_of_file   = BVAL(param, 56);
+
+       TALLOC_FREE(param);
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
+                       uint16_t *fnum,
+                       struct smb_create_returns *cr)
+{
+       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;
+       if (cr != NULL) {
+               *cr = state->cr;
+       }
+       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,
+                           struct smb_create_returns *cr)
+{
+       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 = samba_tevent_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, cr);
  fail:
        TALLOC_FREE(frame);
        return status;
@@ -1909,28 +2283,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 event_context *ev,
+struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
+                                  struct tevent_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;
        }
@@ -1988,7 +2363,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)) {
@@ -2004,43 +2379,42 @@ 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 tevent_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);
+       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;
        NTSTATUS status;
 
-       status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
+       status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
@@ -2050,10 +2424,10 @@ static void cli_open_done(struct tevent_req *subreq)
        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)) {
@@ -2063,15 +2437,15 @@ 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();
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
-       NTSTATUS status = NT_STATUS_OK;
+       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
                 */
@@ -2079,28 +2453,161 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
-               status = NT_STATUS_NO_MEMORY;
                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;
        }
 
-       if (!tevent_req_poll(req, ev)) {
-               status = map_nt_error_from_unix(errno);
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
 
-       status = cli_open_recv(req, pfnum);
+       status = cli_openx_recv(req, pfnum);
  fail:
        TALLOC_FREE(frame);
        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;
+       struct smb_create_returns cr;
+
+       /* 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,
+                               &cr);
+
+       /* 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;
+       }
+
+       if (NT_STATUS_IS_OK(status) &&
+           (create_options & FILE_NON_DIRECTORY_FILE) &&
+           (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
+       {
+               /*
+                * Some (broken) servers return a valid handle
+                * for directories even if FILE_NON_DIRECTORY_FILE
+                * is set. Just close the handle and set the
+                * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
+                */
+               status = cli_close(cli, *pfnum);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               status = NT_STATUS_FILE_IS_A_DIRECTORY;
+               /* Set this so libsmbclient can retrieve it. */
+               cli->raw_status = status;
+       }
+
+       return status;
+
+  try_openx:
+
+       return cli_openx(cli, fname, flags, share_mode_in, pfnum);
+}
 
 /****************************************************************************
  Close a file.
@@ -2113,7 +2620,7 @@ struct cli_close_state {
 static void cli_close_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                uint16_t fnum,
                                struct tevent_req **psubreq)
@@ -2141,7 +2648,7 @@ struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
 }
 
 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                uint16_t fnum)
 {
@@ -2153,7 +2660,7 @@ struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       status = cli_smb_req_send(subreq);
+       status = smb1cli_req_chain_submit(&subreq, 1);
        if (tevent_req_nterror(req, status)) {
                return tevent_req_post(req, ev);
        }
@@ -2181,12 +2688,18 @@ NTSTATUS cli_close_recv(struct tevent_req *req)
 
 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_close_fnum(cli, fnum);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2194,7 +2707,7 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2235,7 +2748,7 @@ static void cli_ftruncate_done(struct tevent_req *subreq)
 }
 
 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        uint16_t fnum,
                                        uint64_t size)
@@ -2292,11 +2805,11 @@ NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -2304,7 +2817,7 @@ NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2344,7 +2857,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);
@@ -2356,21 +2870,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;
 }
@@ -2380,7 +2898,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)
 {
@@ -2388,7 +2906,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));
-       return NT_STATUS_IS_OK(status);
+       return status;
 }
 
 /****************************************************************************
@@ -2403,7 +2921,7 @@ struct cli_unlock_state {
 static void cli_unlock_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                uint16_t fnum,
                                uint64_t offset,
@@ -2426,7 +2944,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);
 
@@ -2464,11 +2982,11 @@ NTSTATUS cli_unlock(struct cli_state *cli,
                        uint32_t len)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
@@ -2476,7 +2994,7 @@ NTSTATUS cli_unlock(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2505,18 +3023,19 @@ 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)
 {
        uint16_t vwv[8];
        uint8_t bytes[20];
-        int saved_timeout = cli->timeout;
+       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);
@@ -2532,23 +3051,27 @@ bool cli_lock64(struct cli_state *cli, uint16_t fnum,
        SSVAL(vwv + 6, 0, 0);
        SSVAL(vwv + 7, 0, 1);
 
-       SIVAL(bytes, 0, cli->pid);
+       SIVAL(bytes, 0, cli_getpid(cli));
        SOFF_T_R(bytes, 4, offset);
        SOFF_T_R(bytes, 12, 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,
                         20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
 
-       cli->timeout = saved_timeout;
+       if (saved_timeout != 0) {
+               cli_set_timeout(cli, saved_timeout);
+       }
 
-       return NT_STATUS_IS_OK(status);
+       return status;
 }
 
 /****************************************************************************
@@ -2563,7 +3086,7 @@ struct cli_unlock64_state {
 static void cli_unlock64_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                uint16_t fnum,
                                uint64_t offset,
@@ -2586,7 +3109,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);
 
@@ -2624,15 +3147,15 @@ NTSTATUS cli_unlock64(struct cli_state *cli,
                                uint64_t len)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
@@ -2640,7 +3163,7 @@ NTSTATUS cli_unlock64(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2683,7 +3206,7 @@ static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
 }
 
 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        uint16_t fnum,
                                        uint64_t offset,
@@ -2732,7 +3255,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);
 
@@ -2766,7 +3289,7 @@ static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
 ****************************************************************************/
 
 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        uint16_t fnum,
                                        uint64_t offset,
@@ -2788,11 +3311,11 @@ NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
                        bool wait_lock, enum brl_type lock_type)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -2805,7 +3328,7 @@ NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2841,7 +3364,7 @@ NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
 ****************************************************************************/
 
 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        uint16_t fnum,
                                        uint64_t offset,
@@ -2859,11 +3382,11 @@ NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -2871,7 +3394,7 @@ NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2910,14 +3433,14 @@ 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;
 };
 
 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                uint16_t fnum)
 {
@@ -2930,7 +3453,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,
@@ -2950,17 +3473,16 @@ static void cli_getattrE_done(struct tevent_req *subreq)
                req, struct cli_getattrE_state);
        uint8_t wct;
        uint16_t *vwv = NULL;
-       uint8_t *inbuf;
        NTSTATUS status;
 
-       status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
+       status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
                              NULL, NULL);
        TALLOC_FREE(subreq);
        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);
@@ -2971,7 +3493,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)
@@ -3004,17 +3526,29 @@ 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)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev = NULL;
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_getattrE(cli,
+                                       fnum,
+                                       attr,
+                                       size,
+                                       change_time,
+                                       access_time,
+                                       write_time);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3022,7 +3556,7 @@ NTSTATUS cli_getattrE(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -3060,12 +3594,12 @@ 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;
 };
 
 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *fname)
 {
@@ -3079,14 +3613,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)) {
@@ -3110,10 +3644,9 @@ static void cli_getatr_done(struct tevent_req *subreq)
                req, struct cli_getatr_state);
        uint8_t wct;
        uint16_t *vwv = NULL;
-       uint8_t *inbuf;
        NTSTATUS status;
 
-       status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
+       status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
@@ -3121,7 +3654,7 @@ static void cli_getatr_done(struct tevent_req *subreq)
        }
 
        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);
@@ -3129,7 +3662,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(
@@ -3154,15 +3687,25 @@ 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();
-       struct event_context *ev = NULL;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev = NULL;
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_getatr(cli,
+                                       fname,
+                                       attr,
+                                       size,
+                                       write_time);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3170,7 +3713,7 @@ NTSTATUS cli_getatr(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -3208,7 +3751,7 @@ struct cli_setattrE_state {
 };
 
 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                uint16_t fnum,
                                time_t change_time,
@@ -3226,11 +3769,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);
@@ -3266,12 +3809,22 @@ NTSTATUS cli_setattrE(struct cli_state *cli,
                        time_t access_time,
                        time_t write_time)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev = NULL;
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_setattrE(cli,
+                                       fnum,
+                                       change_time,
+                                       access_time,
+                                       write_time);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3279,7 +3832,7 @@ NTSTATUS cli_setattrE(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -3320,7 +3873,7 @@ struct cli_setatr_state {
 };
 
 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *fname,
                                uint16_t attr,
@@ -3337,26 +3890,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);
@@ -3395,12 +3948,21 @@ NTSTATUS cli_setatr(struct cli_state *cli,
                uint16_t attr,
                time_t mtime)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev = NULL;
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_setatr(cli,
+                                       fname,
+                                       attr,
+                                       mtime);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3408,7 +3970,7 @@ NTSTATUS cli_setatr(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -3443,7 +4005,7 @@ struct cli_chkpath_state {
 };
 
 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
-                                 struct event_context *ev,
+                                 struct tevent_context *ev,
                                  struct cli_state *cli,
                                  const char *fname)
 {
@@ -3462,7 +4024,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)) {
@@ -3500,12 +4062,12 @@ NTSTATUS cli_chkpath_recv(struct tevent_req *req)
 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        struct tevent_req *req = NULL;
        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
                 */
@@ -3527,7 +4089,7 @@ NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
                }
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -3564,7 +4126,7 @@ struct cli_dskattr_state {
 };
 
 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
-                                 struct event_context *ev,
+                                 struct tevent_context *ev,
                                  struct cli_state *cli)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
@@ -3593,10 +4155,9 @@ static void cli_dskattr_done(struct tevent_req *subreq)
                req, struct cli_dskattr_state);
        uint8_t wct;
        uint16_t *vwv = NULL;
-       uint8_t *inbuf;
        NTSTATUS status;
 
-       status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
+       status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
@@ -3625,12 +4186,14 @@ NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *a
 
 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev = NULL;
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (cli_has_async_calls(cli)) {
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -3638,7 +4201,7 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -3662,6 +4225,77 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
        return status;
 }
 
+NTSTATUS cli_disk_size(struct cli_state *cli, uint64_t *bsize, uint64_t *total, uint64_t *avail)
+{
+       uint64_t sectors_per_block;
+       uint64_t bytes_per_sector;
+       int old_bsize, old_total, old_avail;
+       NTSTATUS status;
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_dskattr(cli, bsize, total, avail);
+       }
+
+       /*
+        * Try the trans2 disk full size info call first.
+        * We already use this in SMBC_fstatvfs_ctx().
+        * Ignore 'actual_available_units' as we only
+        * care about the quota for the caller.
+        */
+
+       status = cli_get_fs_full_size_info(cli,
+                       total,
+                       avail,
+                       NULL,
+                       &sectors_per_block,
+                       &bytes_per_sector);
+
+        /* Try and cope will all varients of "we don't do this call"
+           and fall back to cli_dskattr. */
+
+       if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
+                       NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
+                       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_dskattr;
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (bsize) {
+               *bsize = sectors_per_block *
+                        bytes_per_sector;
+       }
+
+       return NT_STATUS_OK;
+
+  try_dskattr:
+
+       /* Old SMB1 core protocol fallback. */
+       status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (bsize) {
+               *bsize = (uint64_t)old_bsize;
+       }
+       if (total) {
+               *total = (uint64_t)old_total;
+       }
+       if (avail) {
+               *avail = (uint64_t)old_avail;
+       }
+       return NT_STATUS_OK;
+}
+
 /****************************************************************************
  Create and open a temporary file.
 ****************************************************************************/
@@ -3675,7 +4309,7 @@ struct ctemp_state {
 };
 
 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
+                               struct tevent_context *ev,
                                struct cli_state *cli,
                                const char *path)
 {
@@ -3697,7 +4331,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);
@@ -3723,9 +4357,8 @@ static void cli_ctemp_done(struct tevent_req *subreq)
        uint16_t *vwv;
        uint32_t num_bytes = 0;
        uint8_t *bytes = NULL;
-       uint8_t *inbuf;
 
-       status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
+       status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
                              &num_bytes, &bytes);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
@@ -3780,11 +4413,11 @@ NTSTATUS cli_ctemp(struct cli_state *cli,
                        char **out_path)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
@@ -3792,7 +4425,7 @@ NTSTATUS cli_ctemp(struct cli_state *cli,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -3857,7 +4490,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;
                }
@@ -3865,7 +4500,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;
                }
@@ -3882,12 +4519,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;
 }
 
@@ -3901,24 +4538,39 @@ 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 = NULL;
 
-       param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_set_ea_path(cli,
+                                       path,
+                                       ea_name,
+                                       ea_val,
+                                       ea_len);
+       }
+
+       frame = talloc_stackframe();
+
+       param = talloc_array(frame, uint8_t, 6);
        if (!param) {
-               return NT_STATUS_NO_MEMORY;
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
-       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);
+
+  fail:
+
+       TALLOC_FREE(frame);
        return status;
 }
 
@@ -3932,6 +4584,14 @@ NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
 {
        uint8_t param[6];
 
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_set_ea_fnum(cli,
+                                       fnum,
+                                       ea_name,
+                                       ea_val,
+                                       ea_len);
+       }
+
        memset(param, 0, 6);
        SSVAL(param,0,fnum);
        SSVAL(param,2,SMB_INFO_SET_EA);
@@ -3998,7 +4658,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;
        }
@@ -4066,7 +4726,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);
        }
@@ -4113,19 +4773,29 @@ NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
                size_t *pnum_eas,
                struct ea_struct **pea_list)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev = NULL;
        struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_get_ea_list_path(cli,
+                                       path,
+                                       ctx,
+                                       pnum_eas,
+                                       pea_list);
+       }
+
+       frame = talloc_stackframe();
+
+       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);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -4225,7 +4895,7 @@ static void cli_posix_open_internal_done(struct tevent_req *subreq)
 }
 
 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        int flags,
@@ -4252,7 +4922,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)) {
@@ -4296,7 +4966,7 @@ static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
 }
 
 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        int flags,
@@ -4327,11 +4997,11 @@ NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
 {
 
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -4339,7 +5009,7 @@ NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -4369,7 +5039,7 @@ NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
 }
 
 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        mode_t mode)
@@ -4386,11 +5056,11 @@ NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -4398,7 +5068,7 @@ NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -4437,7 +5107,7 @@ struct cli_posix_unlink_internal_state {
 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
 
 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
                                        uint16_t level)
@@ -4472,7 +5142,7 @@ static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
 }
 
 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname)
 {
@@ -4492,11 +5162,11 @@ NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -4504,7 +5174,7 @@ NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -4536,7 +5206,7 @@ NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
 ****************************************************************************/
 
 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname)
 {
@@ -4553,11 +5223,11 @@ NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
+       struct tevent_context *ev = NULL;
        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
                 */
@@ -4565,7 +5235,7 @@ NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -4612,6 +5282,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) {
@@ -4622,6 +5293,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. */
@@ -4641,6 +5317,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);
        }
@@ -4671,13 +5349,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,
@@ -4695,7 +5373,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);
@@ -4703,7 +5381,7 @@ static void cli_notify_done(struct tevent_req *subreq)
                }
 
                state->changes[i].action = IVAL(params, ofs+4);
-               ret = clistr_pull_talloc(params, (char *)params, flags2,
+               ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
                                         &name, params+ofs+12, len,
                                         STR_TERMINATE|STR_UNICODE);
                if (ret == -1) {
@@ -4736,6 +5414,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 = samba_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;
@@ -4769,7 +5482,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);
        }
@@ -4845,18 +5558,18 @@ NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
                       uint8_t **rdata, uint32_t *num_rdata)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -4878,6 +5591,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;
@@ -4937,7 +5651,9 @@ 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);
@@ -4948,6 +5664,7 @@ static void cli_qfileinfo_done(struct tevent_req *subreq)
 }
 
 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(
@@ -4957,6 +5674,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 {
@@ -4970,22 +5691,22 @@ 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();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -4997,7 +5718,7 @@ 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);
        return status;
@@ -5010,7 +5731,7 @@ struct cli_flush_state {
 static void cli_flush_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
-                                 struct event_context *ev,
+                                 struct tevent_context *ev,
                                  struct cli_state *cli,
                                  uint16_t fnum)
 {
@@ -5054,18 +5775,18 @@ NTSTATUS cli_flush_recv(struct tevent_req *req)
 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -5107,11 +5828,11 @@ 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);
-       SCVAL(state->setup + 3, 0, 0); /* isFsctl */
+       SCVAL(state->setup + 3, 0, 1); /* isFsctl */
        SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
 
        subreq = cli_trans_send(
@@ -5199,18 +5920,18 @@ NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
                              char ***pnames, int *pnum_names)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        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
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }