lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[metze/samba-autobuild/.git] / source3 / libsmb / clifile.c
index ee37808a225d166272b0884c671298855e893e57..cc1d1e416652baa98b1afc24e8ad50263b608d24 100644 (file)
 #include "trans2.h"
 #include "ntioctl.h"
 #include "libcli/security/secdesc.h"
-
-/***********************************************************
- Common function for pushing stings, used by smb_bytes_push_str()
- and trans_bytes_push_str(). Only difference is the align_odd
- parameter setting.
-***********************************************************/
-
-static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
-                               const char *str, size_t str_len,
-                               bool align_odd,
-                               size_t *pconverted_size)
-{
-       size_t buflen;
-       char *converted;
-       size_t converted_size;
-
-       if (buf == NULL) {
-               return NULL;
-       }
-
-       buflen = talloc_get_size(buf);
-
-       if (ucs2 &&
-           ((align_odd && (buflen % 2 == 0)) ||
-            (!align_odd && (buflen % 2 == 1)))) {
-               /*
-                * We're pushing into an SMB buffer, align odd
-                */
-               buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
-               if (buf == NULL) {
-                       return NULL;
-               }
-               buf[buflen] = '\0';
-               buflen += 1;
-       }
-
-       if (!convert_string_talloc(talloc_tos(), CH_UNIX,
-                                  ucs2 ? CH_UTF16LE : CH_DOS,
-                                  str, str_len, &converted,
-                                  &converted_size)) {
-               return NULL;
-       }
-
-       buf = talloc_realloc(NULL, buf, uint8_t,
-                                  buflen + converted_size);
-       if (buf == NULL) {
-               TALLOC_FREE(converted);
-               return NULL;
-       }
-
-       memcpy(buf + buflen, converted, converted_size);
-
-       TALLOC_FREE(converted);
-
-       if (pconverted_size) {
-               *pconverted_size = converted_size;
-       }
-
-       return buf;
-}
-
-/***********************************************************
- Push a string into an SMB buffer, with odd byte alignment
- if it's a UCS2 string.
-***********************************************************/
-
-uint8_t *smb_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,
-                       true, pconverted_size);
-}
-
-uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
-                             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 + 1 + num_bytes);
-       if (buf == NULL) {
-               return NULL;
-       }
-       buf[buflen] = prefix;
-       memcpy(&buf[buflen+1], bytes, num_bytes);
-       return buf;
-}
-
-/***********************************************************
- Same as smb_bytes_push_str(), but without the odd byte
- align for ucs2 (we're pushing into a param or data block).
- static for now, although this will probably change when
- other modules use async trans calls.
-***********************************************************/
-
-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;
-}
+#include "../libcli/smb/smbXcli_base.h"
 
 struct cli_setpathinfo_state {
        uint16_t setup;
@@ -172,6 +46,7 @@ struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req, *subreq;
        struct cli_setpathinfo_state *state;
+       uint16_t additional_flags2 = 0;
 
        req = tevent_req_create(mem_ctx, &state,
                                struct cli_setpathinfo_state);
@@ -190,15 +65,21 @@ struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
        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);
        }
 
+       if (clistr_is_previous_version_path(path, NULL, NULL, NULL) &&
+                       !INFO_LEVEL_IS_UNIX(level)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_trans_send(
                state,                  /* mem ctx. */
                ev,                     /* event ctx. */
                cli,                    /* cli_state. */
+               additional_flags2,      /* additional_flags2 */
                SMBtrans2,              /* cmd. */
                NULL,                   /* pipe name. */
                -1,                     /* fid. */
@@ -244,14 +125,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;
        }
@@ -280,7 +161,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,
@@ -301,7 +182,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,
@@ -324,7 +205,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)
@@ -343,11 +224,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
                 */
@@ -355,7 +236,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;
@@ -371,8 +252,7 @@ NTSTATUS cli_posix_symlink(struct cli_state *cli,
                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;
        }
 
@@ -395,14 +275,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) {
@@ -463,7 +343,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,
@@ -484,11 +364,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
                 */
@@ -496,7 +376,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;
@@ -512,8 +392,7 @@ NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
                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;
        }
 
@@ -529,7 +408,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)
@@ -548,11 +427,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
                 */
@@ -560,7 +439,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;
@@ -576,8 +455,7 @@ NTSTATUS cli_posix_hardlink(struct cli_state *cli,
                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;
        }
 
@@ -589,25 +467,25 @@ NTSTATUS cli_posix_hardlink(struct cli_state *cli,
 }
 
 /****************************************************************************
- Do a POSIX getfacl (UNIX extensions).
+ Do a POSIX getacl - pathname based ACL get (UNIX extensions).
 ****************************************************************************/
 
-struct getfacl_state {
+struct getacl_state {
        uint32_t num_data;
        uint8_t *data;
 };
 
-static void cli_posix_getfacl_done(struct tevent_req *subreq);
+static void cli_posix_getacl_done(struct tevent_req *subreq);
 
-struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
+struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
                                        struct cli_state *cli,
                                        const char *fname)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
-       struct getfacl_state *state = NULL;
+       struct getacl_state *state = NULL;
 
-       req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
+       req = tevent_req_create(mem_ctx, &state, struct getacl_state);
        if (req == NULL) {
                return NULL;
        }
@@ -616,16 +494,16 @@ struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
+       tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
        return req;
 }
 
-static void cli_posix_getfacl_done(struct tevent_req *subreq)
+static void cli_posix_getacl_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
                subreq, struct tevent_req);
-       struct getfacl_state *state = tevent_req_data(
-               req, struct getfacl_state);
+       struct getacl_state *state = tevent_req_data(
+               req, struct getacl_state);
        NTSTATUS status;
 
        status = cli_qpathinfo_recv(subreq, state, &state->data,
@@ -637,12 +515,12 @@ static void cli_posix_getfacl_done(struct tevent_req *subreq)
        tevent_req_done(req);
 }
 
-NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
+NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
                                TALLOC_CTX *mem_ctx,
                                size_t *prb_size,
                                char **retbuf)
 {
-       struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
+       struct getacl_state *state = tevent_req_data(req, struct getacl_state);
        NTSTATUS status;
 
        if (tevent_req_is_nterror(req, &status)) {
@@ -653,18 +531,18 @@ NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
        return NT_STATUS_OK;
 }
 
-NTSTATUS cli_posix_getfacl(struct cli_state *cli,
+NTSTATUS cli_posix_getacl(struct cli_state *cli,
                        const char *fname,
                        TALLOC_CTX *mem_ctx,
                        size_t *prb_size,
                        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
                 */
@@ -672,13 +550,13 @@ 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;
        }
 
-       req = cli_posix_getfacl_send(frame,
+       req = cli_posix_getacl_send(frame,
                                ev,
                                cli,
                                fname);
@@ -687,12 +565,111 @@ NTSTATUS cli_posix_getfacl(struct cli_state *cli,
                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_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
+       status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
+
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
+/****************************************************************************
+ Do a POSIX setacl - pathname based ACL set (UNIX extensions).
+****************************************************************************/
+
+struct setacl_state {
+       uint8_t *data;
+};
+
+static void cli_posix_setacl_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname,
+                                       const void *data,
+                                       size_t num_data)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct setacl_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct setacl_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->data = talloc_memdup(state, data, num_data);
+       if (tevent_req_nomem(state->data, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       subreq = cli_setpathinfo_send(state,
+                               ev,
+                               cli,
+                               SMB_SET_POSIX_ACL,
+                               fname,
+                               state->data,
+                               num_data);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
+       return req;
+}
+
+static void cli_posix_setacl_done(struct tevent_req *subreq)
+{
+       NTSTATUS status = cli_setpathinfo_recv(subreq);
+       tevent_req_simple_finish_ntstatus(subreq, status);
+}
+
+NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS cli_posix_setacl(struct cli_state *cli,
+                       const char *fname,
+                       const void *acl_buf,
+                       size_t acl_buf_size)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       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) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_posix_setacl_send(frame,
+                               ev,
+                               cli,
+                               fname,
+                               acl_buf,
+                               acl_buf_size);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+
+       status = cli_posix_setacl_recv(req);
 
  fail:
        TALLOC_FREE(frame);
@@ -711,7 +688,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)
 {
@@ -791,11 +768,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
                 */
@@ -803,7 +780,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;
@@ -818,8 +795,7 @@ NTSTATUS cli_posix_stat(struct cli_state *cli,
                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;
        }
 
@@ -841,7 +817,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,
@@ -884,7 +860,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)
@@ -904,11 +880,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
                 */
@@ -916,7 +892,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;
@@ -932,8 +908,7 @@ NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
                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;
        }
 
@@ -949,7 +924,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,
@@ -973,11 +948,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
                 */
@@ -985,7 +960,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;
@@ -1002,8 +977,7 @@ NTSTATUS cli_posix_chown(struct cli_state *cli,
                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;
        }
 
@@ -1018,28 +992,146 @@ NTSTATUS cli_posix_chown(struct cli_state *cli,
  Rename a file.
 ****************************************************************************/
 
-static void cli_rename_done(struct tevent_req *subreq);
+static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
+                                              struct tevent_context *ev,
+                                              struct cli_state *cli,
+                                              const char *fname_src,
+                                              const char *fname_dst,
+                                              bool replace);
+
+static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
+                                              struct tevent_context *ev,
+                                              struct cli_state *cli,
+                                              const char *fname_src,
+                                              const char *fname_dst,
+                                              bool replace);
+
+struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
+                                  struct tevent_context *ev,
+                                  struct cli_state *cli,
+                                  const char *fname_src,
+                                  const char *fname_dst,
+                                  bool replace)
+{
+       if (replace && smbXcli_conn_support_passthrough(cli->conn)) {
+               return cli_smb1_rename_send(mem_ctx, ev, cli, fname_src,
+                                           fname_dst, replace);
+       } else {
+               return cli_cifs_rename_send(mem_ctx, ev, cli, fname_src,
+                                           fname_dst, replace);
+       }
+}
+
+struct cli_smb1_rename_state {
+       uint8_t *data;
+};
+
+static void cli_smb1_rename_done(struct tevent_req *subreq);
+
+static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
+                                              struct tevent_context *ev,
+                                              struct cli_state *cli,
+                                              const char *fname_src,
+                                              const char *fname_dst,
+                                              bool replace)
+{
+       NTSTATUS status;
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_smb1_rename_state *state = NULL;
+       smb_ucs2_t *converted_str = NULL;
+       size_t converted_size_bytes = 0;
+
+       req = tevent_req_create(mem_ctx, &state, struct cli_smb1_rename_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       if (!push_ucs2_talloc(talloc_tos(), &converted_str, fname_dst,
+                             &converted_size_bytes)) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       /* W2K8 insists the dest name is not null
+          terminated. Remove the last 2 zero bytes
+          and reduce the name length. */
+
+       if (converted_size_bytes < 2) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+       converted_size_bytes -= 2;
+
+       state->data =
+           talloc_zero_array(state, uint8_t, 12 + converted_size_bytes);
+       if (state->data == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (replace) {
+               SCVAL(state->data, 0, 1);
+       }
+
+       SIVAL(state->data, 8, converted_size_bytes);
+       memcpy(state->data + 12, converted_str, converted_size_bytes);
+
+       TALLOC_FREE(converted_str);
+
+       subreq = cli_setpathinfo_send(
+           state, ev, cli, SMB_FILE_RENAME_INFORMATION, fname_src, state->data,
+           talloc_get_size(state->data));
+       if (tevent_req_nomem(subreq, req)) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+       tevent_req_set_callback(subreq, cli_smb1_rename_done, req);
+       return req;
+
+fail:
+       TALLOC_FREE(converted_str);
+       tevent_req_nterror(req, status);
+       return tevent_req_post(req, ev);
+}
 
-struct cli_rename_state {
+static void cli_smb1_rename_done(struct tevent_req *subreq)
+{
+       NTSTATUS status = cli_setpathinfo_recv(subreq);
+       tevent_req_simple_finish_ntstatus(subreq, status);
+}
+
+static void cli_cifs_rename_done(struct tevent_req *subreq);
+
+struct cli_cifs_rename_state {
        uint16_t vwv[1];
 };
 
-struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
-                               struct cli_state *cli,
-                               const char *fname_src,
-                               const char *fname_dst)
+static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
+                                              struct tevent_context *ev,
+                                              struct cli_state *cli,
+                                              const char *fname_src,
+                                              const char *fname_dst,
+                                              bool replace)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
-       struct cli_rename_state *state = NULL;
+       struct cli_cifs_rename_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
-       req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
+       req = tevent_req_create(mem_ctx, &state, struct cli_cifs_rename_state);
        if (req == NULL) {
                return NULL;
        }
 
+       if (replace) {
+               /*
+                * CIFS doesn't support replace
+                */
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               return tevent_req_post(req, ev);
+       }
+
        SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
 
        bytes = talloc_array(state, uint8_t, 1);
@@ -1047,12 +1139,16 @@ 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);
        }
 
+       if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
@@ -1060,22 +1156,23 @@ struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
        }
 
        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);
        }
 
        subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
-                             1, state->vwv, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       1, state->vwv, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, cli_rename_done, req);
+       tevent_req_set_callback(subreq, cli_cifs_rename_done, req);
        return req;
 }
 
-static void cli_rename_done(struct tevent_req *subreq)
+static void cli_cifs_rename_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
                                subreq, struct tevent_req);
@@ -1094,14 +1191,23 @@ NTSTATUS cli_rename_recv(struct tevent_req *req)
        return tevent_req_simple_recv_ntstatus(req);
 }
 
-NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
+NTSTATUS cli_rename(struct cli_state *cli,
+                   const char *fname_src,
+                   const char *fname_dst,
+                   bool replace)
 {
-       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, replace);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -1109,20 +1215,19 @@ 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;
        }
 
-       req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
+       req = cli_rename_send(frame, ev, cli, fname_src, fname_dst, replace);
        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;
        }
 
@@ -1144,7 +1249,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,
@@ -1153,6 +1258,7 @@ static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_ntrename_internal_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state,
@@ -1169,12 +1275,16 @@ 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);
        }
 
+       if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
@@ -1182,14 +1292,15 @@ static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
        }
 
        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);
        }
 
        subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
-                             4, state->vwv, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       4, state->vwv, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -1217,7 +1328,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)
@@ -1238,11 +1349,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
                 */
@@ -1250,7 +1361,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;
@@ -1262,8 +1373,7 @@ NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *
                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;
        }
 
@@ -1279,7 +1389,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)
@@ -1300,11 +1410,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
                 */
@@ -1312,7 +1422,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;
@@ -1324,8 +1434,7 @@ NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const cha
                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;
        }
 
@@ -1347,7 +1456,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)
@@ -1355,6 +1464,7 @@ struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_unlink_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
@@ -1369,14 +1479,19 @@ 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)) {
                return tevent_req_post(req, ev);
        }
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
+                               additional_flags2,
                                1, state->vwv, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -1406,12 +1521,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
                 */
@@ -1419,7 +1540,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;
@@ -1431,8 +1552,7 @@ NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_a
                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;
        }
 
@@ -1454,13 +1574,14 @@ 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)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_mkdir_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
@@ -1473,15 +1594,20 @@ 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)) {
                return tevent_req_post(req, ev);
        }
 
+       if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
-                             0, NULL, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       0, NULL, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -1510,12 +1636,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
                 */
@@ -1523,7 +1655,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;
@@ -1535,8 +1667,7 @@ NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
                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;
        }
 
@@ -1558,13 +1689,14 @@ 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)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_rmdir_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
@@ -1577,15 +1709,20 @@ 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)) {
                return tevent_req_post(req, ev);
        }
 
+       if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
-                             0, NULL, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       0, NULL, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -1614,12 +1751,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
                 */
@@ -1627,7 +1770,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;
@@ -1639,8 +1782,7 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
                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;
        }
 
@@ -1669,7 +1811,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)
@@ -1695,6 +1837,7 @@ struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
        subreq = cli_trans_send(state,                  /* mem ctx. */
                                ev,                     /* event ctx. */
                                cli,                    /* cli_state. */
+                               0,                      /* additional_flags2 */
                                SMBtrans2,              /* cmd. */
                                NULL,                   /* pipe name. */
                                -1,                     /* fid. */
@@ -1725,11 +1868,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
                 */
@@ -1737,7 +1880,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;
@@ -1753,8 +1896,7 @@ NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
                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;
        }
 
@@ -1765,32 +1907,36 @@ 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;
+       uint16_t additional_flags2 = 0;
 
-       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;
        }
@@ -1819,12 +1965,16 @@ struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
        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);
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        /* 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);
@@ -1832,39 +1982,162 @@ struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
 
        SSVAL(vwv+2, 1, converted_len);
 
-       subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
-                             talloc_get_size(bytes), bytes);
+       subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0,
+                       additional_flags2, 24, vwv,
+                       talloc_get_size(bytes), bytes);
        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);
@@ -1873,7 +2146,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;
 }
 
@@ -1886,14 +2164,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
                 */
@@ -1901,9 +2180,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;
        }
 
@@ -1912,16 +2190,14 @@ 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;
@@ -1929,12 +2205,13 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
 
 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 event_context *ev,
+                                          struct tevent_context *ev,
                                           struct cli_state *cli,
                                           const char *fname,
                                           uint32_t CreatFlags,
@@ -1955,6 +2232,7 @@ struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
        size_t secdesc_len;
        NTSTATUS status;
        size_t converted_len;
+       uint16_t additional_flags2 = 0;
 
        req = tevent_req_create(mem_ctx,
                                &state, struct cli_nttrans_create_state);
@@ -1988,13 +2266,17 @@ struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       param = trans2_bytes_push_str(param, cli_ucs2(cli),
+       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);
        }
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        SIVAL(param, 0, CreatFlags);
        SIVAL(param, 4, 0x0);   /* RootDirectoryFid */
        SIVAL(param, 8, DesiredAccess);
@@ -2011,7 +2293,9 @@ struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
        SIVAL(param, 48, 0x02); /* ImpersonationLevel */
        SCVAL(param, 52, SecurityFlags);
 
-       subreq = cli_trans_send(state, ev, cli, SMBnttrans,
+       subreq = cli_trans_send(state, ev, cli,
+                               additional_flags2, /* additional_flags2 */
+                               SMBnttrans,
                                NULL, -1, /* name, fid */
                                NT_TRANSACT_CREATE, 0,
                                NULL, 0, 0, /* setup */
@@ -2041,12 +2325,24 @@ static void cli_nttrans_create_done(struct tevent_req *subreq)
        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)
+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);
@@ -2056,6 +2352,9 @@ NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
                return status;
        }
        *fnum = state->fnum;
+       if (cr != NULL) {
+               *cr = state->cr;
+       }
        return NT_STATUS_OK;
 }
 
@@ -2071,21 +2370,22 @@ NTSTATUS cli_nttrans_create(struct cli_state *cli,
                            struct security_descriptor *secdesc,
                            struct ea_struct *eas,
                            int num_eas,
-                           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_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;
        }
@@ -2100,7 +2400,7 @@ NTSTATUS cli_nttrans_create(struct cli_state *cli,
        if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
-       status = cli_nttrans_create_recv(req, pfid);
+       status = cli_nttrans_create_recv(req, pfid, cr);
  fail:
        TALLOC_FREE(frame);
        return status;
@@ -2112,101 +2412,102 @@ NTSTATUS cli_nttrans_create(struct cli_state *cli,
 ****************************************************************************/
 
 struct cli_openx_state {
-       struct tevent_context *ev;
-       struct cli_state *cli;
        const char *fname;
        uint16_t vwv[15];
        uint16_t fnum;
-       unsigned openfn;
-       unsigned dos_deny;
-       uint8_t additional_flags;
        struct iovec bytes;
 };
 
 static void cli_openx_done(struct tevent_req *subreq);
-static void cli_open_ntcreate_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
-                                  struct event_context *ev,
+                                  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_openx_state *state;
+       unsigned openfn;
+       unsigned accessmode;
+       uint8_t additional_flags;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
        if (req == NULL) {
                return NULL;
        }
-       state->ev = ev;
-       state->cli = cli;
-       state->fname = fname;
 
+       openfn = 0;
        if (flags & O_CREAT) {
-               state->openfn |= (1<<4);
+               openfn |= (1<<4);
        }
        if (!(flags & O_EXCL)) {
                if (flags & O_TRUNC)
-                       state->openfn |= (1<<1);
+                       openfn |= (1<<1);
                else
-                       state->openfn |= (1<<0);
+                       openfn |= (1<<0);
        }
 
-       state->dos_deny = (share_mode<<4);
+       accessmode = (share_mode<<4);
 
        if ((flags & O_ACCMODE) == O_RDWR) {
-               state->dos_deny |= 2;
+               accessmode |= 2;
        } else if ((flags & O_ACCMODE) == O_WRONLY) {
-               state->dos_deny |= 1;
+               accessmode |= 1;
        }
 
 #if defined(O_SYNC)
        if ((flags & O_SYNC) == O_SYNC) {
-               state->dos_deny |= (1<<14);
+               accessmode |= (1<<14);
        }
 #endif /* O_SYNC */
 
        if (share_mode == DENY_FCB) {
-               state->dos_deny = 0xFF;
+               accessmode = 0xFF;
        }
 
        SCVAL(state->vwv + 0, 0, 0xFF);
        SCVAL(state->vwv + 0, 1, 0);
        SSVAL(state->vwv + 1, 0, 0);
        SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
-       SSVAL(state->vwv + 3, 0, state->dos_deny);
+       SSVAL(state->vwv + 3, 0, accessmode);
        SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        SSVAL(state->vwv + 5, 0, 0);
        SIVAL(state->vwv + 6, 0, 0);
-       SSVAL(state->vwv + 8, 0, state->openfn);
+       SSVAL(state->vwv + 8, 0, openfn);
        SIVAL(state->vwv + 9, 0, 0);
        SIVAL(state->vwv + 11, 0, 0);
        SIVAL(state->vwv + 13, 0, 0);
 
+       additional_flags = 0;
+
        if (cli->use_oplocks) {
                /* if using oplocks then ask for a batch oplock via
                    core and extended methods */
-               state->additional_flags =
+               additional_flags =
                        FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
                SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
        }
 
        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)) {
                return tevent_req_post(req, ev);
        }
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        state->bytes.iov_base = (void *)bytes;
        state->bytes.iov_len = talloc_get_size(bytes);
 
-       subreq = cli_smb_req_create(state, ev, cli, SMBopenX,
-                                   state->additional_flags,
-                                   15, state->vwv, 1, &state->bytes);
+       subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
+                       additional_flags2, 15, state->vwv, 1, &state->bytes);
        if (subreq == NULL) {
                TALLOC_FREE(req);
                return NULL;
@@ -2216,7 +2517,7 @@ struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
        return req;
 }
 
-struct tevent_req *cli_openx_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)
 {
@@ -2229,7 +2530,7 @@ struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
                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);
        }
@@ -2244,61 +2545,15 @@ static void cli_openx_done(struct tevent_req *subreq)
                req, struct cli_openx_state);
        uint8_t wct;
        uint16_t *vwv;
-       uint8_t *inbuf;
        NTSTATUS status;
-       uint32_t access_mask, share_mode, create_disposition, create_options;
 
-       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 (NT_STATUS_IS_OK(status)) {
-               state->fnum = SVAL(vwv+2, 0);
-               tevent_req_done(req);
-               return;
-       }
-
-       if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
-               tevent_req_nterror(req, status);
-               return;
-       }
-
-       /*
-        * For the new shiny OS/X Lion SMB server, try a ntcreate
-        * fallback.
-        */
-
-       if (!map_open_params_to_ntcreate(state->fname, state->dos_deny,
-                                        state->openfn, &access_mask,
-                                        &share_mode, &create_disposition,
-                                        &create_options, NULL)) {
-               tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
-               return;
-       }
-
-       subreq = cli_ntcreate_send(state, state->ev, state->cli,
-                                  state->fname, 0, access_mask,
-                                  0, share_mode, create_disposition,
-                                  create_options, 0);
-       if (tevent_req_nomem(subreq, req)) {
-               return;
-       }
-       tevent_req_set_callback(subreq, cli_open_ntcreate_done, req);
-}
-
-static void cli_open_ntcreate_done(struct tevent_req *subreq)
-{
-       struct tevent_req *req = tevent_req_callback_data(
-               subreq, struct tevent_req);
-       struct cli_openx_state *state = tevent_req_data(
-               req, struct cli_openx_state);
-       NTSTATUS status;
-
-       status = cli_ntcreate_recv(subreq, &state->fnum);
-       TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
                return;
        }
+       state->fnum = SVAL(vwv+2, 0);
        tevent_req_done(req);
 }
 
@@ -2319,11 +2574,11 @@ 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
                 */
@@ -2331,20 +2586,17 @@ NTSTATUS cli_openx(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_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;
        }
 
@@ -2365,6 +2617,7 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
        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) {
@@ -2445,7 +2698,8 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
                                create_disposition,
                                create_options,
                                0,
-                               pfnum);
+                               pfnum,
+                               &cr);
 
        /* Try and cope will all varients of "we don't do this call"
           and fall back to openX. */
@@ -2462,6 +2716,25 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
                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:
@@ -2480,7 +2753,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)
@@ -2496,8 +2769,8 @@ struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
        SSVAL(state->vwv+0, 0, fnum);
        SIVALS(state->vwv+1, 0, -1);
 
-       subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
-                                   0, NULL);
+       subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
+                               3, state->vwv, 0, NULL);
        if (subreq == NULL) {
                TALLOC_FREE(req);
                return NULL;
@@ -2508,7 +2781,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)
 {
@@ -2520,7 +2793,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);
        }
@@ -2548,12 +2821,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
                 */
@@ -2561,7 +2840,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;
@@ -2573,8 +2852,7 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
                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;
        }
 
@@ -2602,7 +2880,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)
@@ -2629,6 +2907,7 @@ struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
        subreq = cli_trans_send(state,                  /* mem ctx. */
                                ev,                     /* event ctx. */
                                cli,                    /* cli_state. */
+                               0,                      /* additional_flags2 */
                                SMBtrans2,              /* cmd. */
                                NULL,                   /* pipe name. */
                                -1,                     /* fid. */
@@ -2658,12 +2937,18 @@ 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;
+       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_ftruncate(cli, fnum, size);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
@@ -2671,7 +2956,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;
@@ -2687,8 +2972,7 @@ NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
                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;
        }
 
@@ -2775,7 +3059,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,
@@ -2802,7 +3086,7 @@ struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
        SIVAL(state->data, 2, offset);
        SIVAL(state->data, 6, len);
 
-       subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
+       subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
                                8, state->vwv, 10, state->data);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -2836,11 +3120,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
                 */
@@ -2848,7 +3132,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;
@@ -2861,8 +3145,7 @@ NTSTATUS cli_unlock(struct cli_state *cli,
                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;
        }
 
@@ -2888,7 +3171,7 @@ NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
        int ltype;
        NTSTATUS status;
 
-       if (! (cli_state_capabilities(cli) & CAP_LARGE_FILES)) {
+       if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
                return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
        }
 
@@ -2940,7 +3223,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,
@@ -2967,7 +3250,7 @@ struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
        SOFF_T_R(state->data, 4, offset);
        SOFF_T_R(state->data, 12, len);
 
-       subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
+       subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
                                8, state->vwv, 20, state->data);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -3001,15 +3284,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_state_capabilities(cli) & 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
                 */
@@ -3017,7 +3300,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;
@@ -3030,8 +3313,7 @@ NTSTATUS cli_unlock64(struct cli_state *cli,
                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;
        }
 
@@ -3060,7 +3342,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,
@@ -3116,6 +3398,7 @@ static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
        subreq = cli_trans_send(state,                  /* mem ctx. */
                                ev,                     /* event ctx. */
                                cli,                    /* cli_state. */
+                               0,                      /* additional_flags2 */
                                SMBtrans2,              /* cmd. */
                                NULL,                   /* pipe name. */
                                -1,                     /* fid. */
@@ -3143,7 +3426,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,
@@ -3165,11 +3448,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
                 */
@@ -3182,7 +3465,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;
@@ -3201,8 +3484,7 @@ NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
                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;
        }
 
@@ -3218,7 +3500,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,
@@ -3236,11 +3518,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
                 */
@@ -3248,7 +3530,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;
@@ -3265,8 +3547,7 @@ NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset,
                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;
        }
 
@@ -3287,14 +3568,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)
 {
@@ -3307,10 +3588,10 @@ struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       state->zone_offset = cli_state_server_time_zone(cli);
+       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,
+       subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
                              1, state->vwv, 0, NULL);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -3327,17 +3608,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);
@@ -3348,7 +3628,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)
@@ -3381,17 +3661,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
                 */
@@ -3399,7 +3691,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;
@@ -3411,8 +3703,7 @@ NTSTATUS cli_getattrE(struct cli_state *cli,
                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;
        }
 
@@ -3437,18 +3728,19 @@ 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)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_getatr_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
@@ -3456,22 +3748,27 @@ struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       state->zone_offset = cli_state_server_time_zone(cli);
+       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)) {
                return tevent_req_post(req, ev);
        }
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
-                             0, NULL, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       0, NULL, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -3487,10 +3784,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)) {
@@ -3498,7 +3794,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);
@@ -3506,7 +3802,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(
@@ -3531,15 +3827,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
                 */
@@ -3547,7 +3853,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;
@@ -3559,8 +3865,7 @@ NTSTATUS cli_getatr(struct cli_state *cli,
                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;
        }
 
@@ -3585,7 +3890,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,
@@ -3603,13 +3908,13 @@ 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_state_server_time_zone(cli));
+                      smb1cli_conn_server_time_zone(cli->conn));
        push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
-                      cli_state_server_time_zone(cli));
+                      smb1cli_conn_server_time_zone(cli->conn));
        push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
-                      cli_state_server_time_zone(cli));
+                      smb1cli_conn_server_time_zone(cli->conn));
 
-       subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
+       subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
                              7, state->vwv, 0, NULL);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -3643,12 +3948,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
                 */
@@ -3656,7 +3971,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;
@@ -3674,8 +3989,7 @@ NTSTATUS cli_setattrE(struct cli_state *cli,
                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;
        }
 
@@ -3697,7 +4011,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,
@@ -3706,6 +4020,7 @@ struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_setatr_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
@@ -3714,14 +4029,14 @@ 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_state_server_time_zone(cli));
+       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);
@@ -3733,14 +4048,19 @@ struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
        }
 
        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);
        }
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
-                             8, state->vwv, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       8, state->vwv, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -3772,12 +4092,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
                 */
@@ -3785,7 +4114,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;
@@ -3797,8 +4126,7 @@ NTSTATUS cli_setatr(struct cli_state *cli,
                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;
        }
 
@@ -3810,7 +4138,7 @@ NTSTATUS cli_setatr(struct cli_state *cli,
 }
 
 /****************************************************************************
- Check for existance of a dir.
+ Check for existence of a dir.
 ****************************************************************************/
 
 static void cli_chkpath_done(struct tevent_req *subreq);
@@ -3820,13 +4148,14 @@ 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)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_chkpath_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
@@ -3839,15 +4168,20 @@ 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)) {
                return tevent_req_post(req, ev);
        }
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
-                             0, NULL, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       0, NULL, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -3877,12 +4211,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
                 */
@@ -3904,7 +4238,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;
@@ -3916,8 +4250,7 @@ NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
                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;
        }
 
@@ -3941,7 +4274,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;
@@ -3953,7 +4286,7 @@ struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
+       subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
                              0, NULL, 0, NULL);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -3970,10 +4303,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)) {
@@ -4002,12 +4334,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
                 */
@@ -4015,7 +4349,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;
@@ -4027,8 +4361,7 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
                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;
        }
 
@@ -4039,6 +4372,78 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
        return status;
 }
 
+NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, 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, path, 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.
 ****************************************************************************/
@@ -4052,13 +4457,14 @@ 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)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
        struct ctemp_state *state = NULL;
        uint8_t additional_flags = 0;
+       uint16_t additional_flags2 = 0;
        uint8_t *bytes = NULL;
 
        req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
@@ -4074,14 +4480,19 @@ 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);
        }
 
+       if (clistr_is_previous_version_path(path, NULL, NULL, NULL)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
-                             3, state->vwv, talloc_get_size(bytes), bytes);
+                       additional_flags2,
+                       3, state->vwv, talloc_get_size(bytes), bytes);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -4100,9 +4511,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)) {
@@ -4157,11 +4567,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
                 */
@@ -4169,7 +4579,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;
@@ -4181,8 +4591,7 @@ NTSTATUS cli_ctemp(struct cli_state *cli,
                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;
        }
 
@@ -4234,7 +4643,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;
                }
@@ -4242,7 +4653,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;
                }
@@ -4256,15 +4669,24 @@ static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
                memcpy(p+4+ea_namelen+1, ea_val, ea_len);
        }
 
+       /*
+        * FIXME - if we want to do previous version path
+        * processing on an EA set call we need to turn this
+        * into calls to cli_trans_send()/cli_trans_recv()
+        * with a temporary event context, as cli_trans_send()
+        * have access to the additional_flags2 needed to
+        * send @GMT- paths. JRA.
+        */
+
        status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
                           setup, 1, 0,
                           param, param_len, 2,
-                          data,  data_len, CLI_BUFFER_SIZE,
+                          data,  data_len, 0,
                           NULL,
                           NULL, 0, NULL, /* rsetup */
                           NULL, 0, NULL, /* rparam */
                           NULL, 0, NULL); /* rdata */
-       SAFE_FREE(data);
+       talloc_free(data);
        return status;
 }
 
@@ -4279,24 +4701,38 @@ NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
        unsigned int param_len = 0;
        uint8_t *param;
        NTSTATUS status;
-       TALLOC_CTX *frame = talloc_stackframe();
+       TALLOC_CTX *frame = NULL;
 
-       param = talloc_array(talloc_tos(), uint8_t, 6);
+       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;
        }
        SSVAL(param,0,SMB_INFO_SET_EA);
        SSVAL(param,2,0);
        SSVAL(param,4,0);
 
-       param = trans2_bytes_push_str(param, cli_ucs2(cli),
+       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(frame);
+
+  fail:
+
+       TALLOC_FREE(frame);
        return status;
 }
 
@@ -4310,6 +4746,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);
@@ -4491,19 +4935,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;
        }
@@ -4603,7 +5057,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,
@@ -4630,7 +5084,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)) {
@@ -4651,6 +5105,7 @@ static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
        subreq = cli_trans_send(state,                  /* mem ctx. */
                                ev,                     /* event ctx. */
                                cli,                    /* cli_state. */
+                               0,                      /* additional_flags2 */
                                SMBtrans2,              /* cmd. */
                                NULL,                   /* pipe name. */
                                -1,                     /* fid. */
@@ -4674,7 +5129,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,
@@ -4705,11 +5160,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
                 */
@@ -4717,7 +5172,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;
@@ -4734,8 +5189,7 @@ NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
                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;
        }
 
@@ -4747,7 +5201,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)
@@ -4764,11 +5218,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
                 */
@@ -4776,7 +5230,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;
@@ -4792,8 +5246,7 @@ NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
                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;
        }
 
@@ -4815,7 +5268,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)
@@ -4850,7 +5303,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)
 {
@@ -4870,11 +5323,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
                 */
@@ -4882,7 +5335,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;
@@ -4897,8 +5350,7 @@ NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
                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;
        }
 
@@ -4914,7 +5366,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)
 {
@@ -4931,11 +5383,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
                 */
@@ -4943,7 +5395,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;
@@ -4958,8 +5410,7 @@ NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
                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;
        }
 
@@ -4990,6 +5441,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) {
@@ -5000,10 +5452,16 @@ 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. */
                cli,                    /* cli_state. */
+               0,                      /* additional_flags2 */
                SMBnttrans,             /* cmd. */
                NULL,                   /* pipe name. */
                -1,                     /* fid. */
@@ -5019,6 +5477,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);
        }
@@ -5049,13 +5509,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,
@@ -5073,7 +5533,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);
@@ -5081,7 +5541,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) {
@@ -5114,6 +5574,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;
@@ -5133,6 +5628,7 @@ struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req, *subreq;
        struct cli_qpathinfo_state *state;
+       uint16_t additional_flags2 = 0;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
        if (req == NULL) {
@@ -5147,15 +5643,21 @@ 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);
        }
 
+       if (clistr_is_previous_version_path(fname, NULL, NULL, NULL) &&
+                       !INFO_LEVEL_IS_UNIX(level)) {
+               additional_flags2 = FLAGS2_REPARSE_PATH;
+       }
+
        subreq = cli_trans_send(
                state,                  /* mem ctx. */
                ev,                     /* event ctx. */
                cli,                    /* cli_state. */
+               additional_flags2,      /* additional_flags2 */
                SMBtrans2,              /* cmd. */
                NULL,                   /* pipe name. */
                -1,                     /* fid. */
@@ -5223,18 +5725,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;
        }
@@ -5286,6 +5788,7 @@ struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
                state,                  /* mem ctx. */
                ev,                     /* event ctx. */
                cli,                    /* cli_state. */
+               0,                      /* additional_flags2 */
                SMBtrans2,              /* cmd. */
                NULL,                   /* pipe name. */
                -1,                     /* fid. */
@@ -5360,18 +5863,18 @@ NTSTATUS cli_qfileinfo(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;
        }
@@ -5396,7 +5899,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)
 {
@@ -5409,7 +5912,7 @@ struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
        }
        SSVAL(state->vwv + 0, 0, fnum);
 
-       subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
+       subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
                              0, NULL);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -5440,18 +5943,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;
        }
@@ -5497,12 +6000,13 @@ struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
 
        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(
-               state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
-               state->setup, ARRAY_SIZE(state->setup), 0,
+               state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
+               state->setup, ARRAY_SIZE(state->setup),
+               ARRAY_SIZE(state->setup),
                NULL, 0, 0,
                NULL, 0, ret_size);
        if (tevent_req_nomem(subreq, req)) {
@@ -5536,36 +6040,61 @@ NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 {
        struct cli_shadow_copy_data_state *state = tevent_req_data(
                req, struct cli_shadow_copy_data_state);
-       char **names;
-       int i, num_names;
+       char **names = NULL;
+       uint32_t i, num_names;
        uint32_t dlength;
+       uint8_t *endp = NULL;
        NTSTATUS status;
 
        if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
+
+       if (state->num_data < 16) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
        num_names = IVAL(state->data, 4);
        dlength = IVAL(state->data, 8);
 
+       if (num_names > 0x7FFFFFFF) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
        if (!state->get_names) {
-               *pnum_names = num_names;
+               *pnum_names = (int)num_names;
                return NT_STATUS_OK;
        }
 
-       if (dlength+12 > state->num_data) {
+       if (dlength + 12 < 12) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+       if (dlength + 12 > state->num_data) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+       if (state->num_data + (2 * sizeof(SHADOW_COPY_LABEL)) <
+                       state->num_data) {
                return NT_STATUS_INVALID_NETWORK_RESPONSE;
        }
+
        names = talloc_array(mem_ctx, char *, num_names);
        if (names == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
+       endp = state->data + state->num_data;
+
        for (i=0; i<num_names; i++) {
                bool ret;
                uint8_t *src;
                size_t converted_size;
 
                src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
+
+               if (src + (2 * sizeof(SHADOW_COPY_LABEL)) > endp) {
+                       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+               }
+
                ret = convert_string_talloc(
                        names, CH_UTF16LE, CH_UNIX,
                        src, 2 * sizeof(SHADOW_COPY_LABEL),
@@ -5575,7 +6104,7 @@ NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                        return NT_STATUS_INVALID_NETWORK_RESPONSE;
                }
        }
-       *pnum_names = num_names;
+       *pnum_names = (int)num_names;
        *pnames = names;
        return NT_STATUS_OK;
 }
@@ -5584,19 +6113,30 @@ NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
                              uint16_t fnum, bool get_names,
                              char ***pnames, int *pnum_names)
 {
-       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_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+        if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_shadow_copy_data(mem_ctx,
+                                       cli,
+                                       fnum,
+                                       get_names,
+                                       pnames,
+                                       pnum_names);
+       }
+
+       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;
        }