Add cli_posix_readlink() and a torture test for it.
[ira/wip.git] / source3 / libsmb / clifile.c
index a4e70c53cde9f4a4621b9f4bf21f36f8279075b7..187fcdf62519b55200cb0dca10b9ebded250b061 100644 (file)
 
 #include "includes.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 (align_odd && ucs2 && (buflen % 2 == 0)) {
+               /*
+                * We're pushing into an SMB buffer, align odd
+                */
+               buf = TALLOC_REALLOC_ARRAY(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, true)) {
+               return NULL;
+       }
+
+       buf = TALLOC_REALLOC_ARRAY(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);
+}
+
+/***********************************************************
+ 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.
+***********************************************************/
+
+static 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);
+}
+
 /****************************************************************************
  Hard/Symlink a file (UNIX extensions).
  Creates new name (sym)linked to oldname.
 ****************************************************************************/
 
-static bool cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, bool hard_link)
+struct link_state {
+       uint16_t setup;
+       uint8_t *param;
+       uint8_t *data;
+};
+
+static void cli_posix_link_internal_done(struct tevent_req *subreq)
 {
-       unsigned int data_len = 0;
-       unsigned int param_len = 0;
-       uint16_t setup = TRANSACT2_SETPATHINFO;
-       char *param;
-       char *data;
-       char *rparam=NULL, *rdata=NULL;
-       char *p;
-       size_t oldlen = 2*(strlen(oldname)+1);
-       size_t newlen = 2*(strlen(newname)+1);
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       struct link_state *state = tevent_req_data(req, struct link_state);
+       NTSTATUS status;
+
+       status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       tevent_req_done(req);
+}
 
-       param = SMB_MALLOC_ARRAY(char, 6+newlen+2);
+static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *oldname,
+                                       const char *newname,
+                                       bool hardlink)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct link_state *state = NULL;
 
-       if (!param) {
-               return false;
+       req = tevent_req_create(mem_ctx, &state, struct link_state);
+       if (req == NULL) {
+               return NULL;
        }
 
-       data = SMB_MALLOC_ARRAY(char, oldlen+2);
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
 
-       if (!data) {
-               SAFE_FREE(param);
-               return false;
+       /* Setup param array. */
+       state->param = talloc_array(state, uint8_t, 6);
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
        }
+       memset(state->param, '\0', 6);
+       SSVAL(state->param,0,hardlink ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
 
-       SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
-       SIVAL(param,2,0);
-       p = &param[6];
+       state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), newname,
+                                  strlen(newname)+1, NULL);
 
-       p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
+       }
 
-       p = data;
-       p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
-       data_len = PTR_DIFF(p, data);
+       /* Setup data array. */
+       state->data = talloc_array(state, uint8_t, 0);
+       if (tevent_req_nomem(state->data, req)) {
+               return tevent_req_post(req, ev);
+       }
+       state->data = trans2_bytes_push_str(state->data, cli_ucs2(cli), oldname,
+                                  strlen(oldname)+1, NULL);
 
-       if (!cli_send_trans(cli, SMBtrans2,
-                       NULL,                        /* name */
-                       -1, 0,                          /* fid, flags */
-                       &setup, 1, 0,                   /* setup, length, max */
-                       param, param_len, 2,            /* param, length, max */
-                       data,  data_len, cli->max_xmit /* data, length, max */
-                       )) {
-               SAFE_FREE(data);
-               SAFE_FREE(param);
-               return false;
+       subreq = cli_trans_send(state,                  /* mem ctx. */
+                               ev,                     /* event ctx. */
+                               cli,                    /* cli_state. */
+                               SMBtrans2,              /* cmd. */
+                               NULL,                   /* pipe name. */
+                               -1,                     /* fid. */
+                               0,                      /* function. */
+                               0,                      /* flags. */
+                               &state->setup,          /* setup. */
+                               1,                      /* num setup uint16_t words. */
+                               0,                      /* max returned setup. */
+                               state->param,           /* param. */
+                               talloc_get_size(state->param),  /* num param. */
+                               2,                      /* max returned param. */
+                               state->data,            /* data. */
+                               talloc_get_size(state->data),   /* num data. */
+                               0);                     /* max returned data. */
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
+       tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
+       return req;
+}
 
-       SAFE_FREE(data);
-       SAFE_FREE(param);
+/****************************************************************************
+ Symlink a file (UNIX extensions).
+****************************************************************************/
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-                       &rparam, &param_len,
-                       &rdata, &data_len)) {
-                       return false;
+struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *oldname,
+                                       const char *newname)
+{
+       return cli_posix_link_internal_send(mem_ctx, ev, cli,
+                       oldname, newname, false);
+}
+
+NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
        }
+       return NT_STATUS_OK;
+}
 
-       SAFE_FREE(data);
-       SAFE_FREE(param);
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
+NTSTATUS cli_posix_symlink(struct cli_state *cli,
+                       const char *oldname,
+                       const char *newname)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
 
-       return true;
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_posix_symlink_send(frame,
+                               ev,
+                               cli,
+                               oldname,
+                               newname);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+
+       status = cli_posix_symlink_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+/****************************************************************************
+ Read a POSIX symlink.
+****************************************************************************/
+
+struct readlink_state {
+       uint16_t setup;
+       uint8_t *param;
+       uint8_t *data;
+       uint32_t num_data;
+};
+
+static void cli_posix_readlink_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       struct readlink_state *state = tevent_req_data(req, struct readlink_state);
+       NTSTATUS status;
+
+       status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
+                       &state->data, &state->num_data);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       if (state->num_data == 0) {
+               tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
+               return;
+       }
+       if (state->data[state->num_data-1] != '\0') {
+               tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
+               return;
+       }
+       tevent_req_done(req);
+}
+
+struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
+                                       struct event_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);
+
+       if (maxbytelen < len) {
+               return NULL;
+       }
+
+       req = tevent_req_create(mem_ctx, &state, struct readlink_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_QPATHINFO);
+
+       /* Setup param array. */
+       state->param = talloc_array(state, uint8_t, 6);
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
+       }
+       memset(state->param, '\0', 6);
+       SSVAL(state->param,0,SMB_QUERY_FILE_UNIX_LINK);
+
+       state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
+                                  strlen(fname)+1, NULL);
+
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       subreq = cli_trans_send(state,                  /* mem ctx. */
+                               ev,                     /* event ctx. */
+                               cli,                    /* cli_state. */
+                               SMBtrans2,              /* cmd. */
+                               NULL,                   /* pipe name. */
+                               -1,                     /* fid. */
+                               0,                      /* function. */
+                               0,                      /* flags. */
+                               &state->setup,          /* setup. */
+                               1,                      /* num setup uint16_t words. */
+                               0,                      /* max returned setup. */
+                               state->param,           /* param. */
+                               talloc_get_size(state->param),  /* num param. */
+                               2,                      /* max returned param. */
+                               NULL,                   /* data. */
+                               0,                      /* num data. */
+                               maxbytelen);            /* max returned data. */
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
+       return req;
+}
+
+NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
+                               char *retpath, size_t len)
+{
+       NTSTATUS status;
+       char *converted = NULL;
+       size_t converted_size = 0;
+       struct readlink_state *state = tevent_req_data(req, struct readlink_state);
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       /* The returned data is a pushed string, not raw data. */
+       if (!convert_string_talloc(state,
+                               cli_ucs2(cli) ? CH_UTF16LE : CH_DOS, 
+                               CH_UNIX,
+                               state->data,
+                               state->num_data,
+                               &converted,
+                               &converted_size,
+                               true)) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       len = MIN(len,converted_size);
+       if (len == 0) {
+               return NT_STATUS_DATA_ERROR;
+       }
+       memcpy(retpath, converted, len);
+       return NT_STATUS_OK;
+}
+
+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_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       /* Len is in bytes, we need it in UCS2 units. */
+       if (2*len < len) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       req = cli_posix_readlink_send(frame,
+                               ev,
+                               cli,
+                               fname,
+                               len);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+
+       status = cli_posix_readlink_recv(req, cli, linkpath, len);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+
+/****************************************************************************
+ Hard link a file (UNIX extensions).
+****************************************************************************/
+
+struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *oldname,
+                                       const char *newname)
+{
+       return cli_posix_link_internal_send(mem_ctx, ev, cli,
+                       oldname, newname, true);
+}
+
+NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_posix_hardlink(struct cli_state *cli,
+                       const char *oldname,
+                       const char *newname)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_posix_hardlink_send(frame,
+                               ev,
+                               cli,
+                               oldname,
+                               newname);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+
+       status = cli_posix_hardlink_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
@@ -294,56 +730,37 @@ bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbu
                return false;
        }
 
-       sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
-       sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
+       sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
+       sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
-       sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
+       sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
 #else
        /* assume 512 byte blocks */
-       sbuf->st_blocks /= 512;
+       sbuf->st_ex_blocks /= 512;
 #endif
-       set_ctimespec(sbuf, interpret_long_date(rdata + 16));    /* time of last change */
-       set_atimespec(sbuf, interpret_long_date(rdata + 24));    /* time of last access */
-       set_mtimespec(sbuf, interpret_long_date(rdata + 32));    /* time of last modification */
+       sbuf->st_ex_ctime = interpret_long_date(rdata + 16);    /* time of last change */
+       sbuf->st_ex_atime = interpret_long_date(rdata + 24);    /* time of last access */
+       sbuf->st_ex_mtime = interpret_long_date(rdata + 32);    /* time of last modification */
 
-       sbuf->st_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
-       sbuf->st_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
-       sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
+       sbuf->st_ex_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
+       sbuf->st_ex_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
+       sbuf->st_ex_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
 #if defined(HAVE_MAKEDEV)
        {
                uint32_t dev_major = IVAL(rdata,60);
                uint32_t dev_minor = IVAL(rdata,68);
-               sbuf->st_rdev = makedev(dev_major, dev_minor);
+               sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
        }
 #endif
-       sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
-       sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
-       sbuf->st_nlink = IVAL(rdata,92);    /* number of hard links */
+       sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
+       sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
+       sbuf->st_ex_nlink = IVAL(rdata,92);    /* number of hard links */
 
        SAFE_FREE(rdata);
        SAFE_FREE(rparam);
 
        return true;
 }
-
-/****************************************************************************
- Symlink a file (UNIX extensions).
-****************************************************************************/
-
-bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
-{
-       return cli_link_internal(cli, oldname, newname, False);
-}
-
-/****************************************************************************
- Hard a file (UNIX extensions).
-****************************************************************************/
-
-bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
-{
-       return cli_link_internal(cli, oldname, newname, True);
-}
-
 /****************************************************************************
  Chmod or chown a file internal (UNIX extensions).
 ****************************************************************************/
@@ -552,13 +969,13 @@ NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fn
  NT Rename a file.
 ****************************************************************************/
 
-static void cli_ntrename_done(struct tevent_req *subreq);
+static void cli_ntrename_internal_done(struct tevent_req *subreq);
 
-struct cli_ntrename_state {
+struct cli_ntrename_internal_state {
        uint16_t vwv[4];
 };
 
-static struct tevent_req *cli_ntrename_send_internal(TALLOC_CTX *mem_ctx,
+static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
                                struct event_context *ev,
                                struct cli_state *cli,
                                const char *fname_src,
@@ -566,11 +983,12 @@ static struct tevent_req *cli_ntrename_send_internal(TALLOC_CTX *mem_ctx,
                                uint16_t rename_flag)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
-       struct cli_ntrename_state *state = NULL;
+       struct cli_ntrename_internal_state *state = NULL;
        uint8_t additional_flags = 0;
        uint8_t *bytes = NULL;
 
-       req = tevent_req_create(mem_ctx, &state, struct cli_ntrename_state);
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_ntrename_internal_state);
        if (req == NULL) {
                return NULL;
        }
@@ -607,25 +1025,11 @@ static struct tevent_req *cli_ntrename_send_internal(TALLOC_CTX *mem_ctx,
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, cli_ntrename_done, req);
+       tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
        return req;
 }
 
-struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
-                               struct cli_state *cli,
-                               const char *fname_src,
-                               const char *fname_dst)
-{
-       return cli_ntrename_send_internal(mem_ctx,
-                                       ev,
-                                       cli,
-                                       fname_src,
-                                       fname_dst,
-                                       RENAME_FLAG_RENAME);
-}
-
-static void cli_ntrename_done(struct tevent_req *subreq)
+static void cli_ntrename_internal_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
                                subreq, struct tevent_req);
@@ -640,11 +1044,30 @@ static void cli_ntrename_done(struct tevent_req *subreq)
        tevent_req_done(req);
 }
 
-NTSTATUS cli_ntrename_recv(struct tevent_req *req)
+static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
 {
        return tevent_req_simple_recv_ntstatus(req);
 }
 
+struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname_src,
+                               const char *fname_dst)
+{
+       return cli_ntrename_internal_send(mem_ctx,
+                                         ev,
+                                         cli,
+                                         fname_src,
+                                         fname_dst,
+                                         RENAME_FLAG_RENAME);
+}
+
+NTSTATUS cli_ntrename_recv(struct tevent_req *req)
+{
+       return cli_ntrename_internal_recv(req);
+}
+
 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -697,17 +1120,17 @@ struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
                                const char *fname_src,
                                const char *fname_dst)
 {
-       return cli_ntrename_send_internal(mem_ctx,
-                                       ev,
-                                       cli,
-                                       fname_src,
-                                       fname_dst,
-                                       RENAME_FLAG_HARD_LINK);
+       return cli_ntrename_internal_send(mem_ctx,
+                                         ev,
+                                         cli,
+                                         fname_src,
+                                         fname_dst,
+                                         RENAME_FLAG_HARD_LINK);
 }
 
 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
 {
-       return tevent_req_simple_recv_ntstatus(req);
+       return cli_ntrename_internal_recv(req);
 }
 
 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
@@ -1282,92 +1705,6 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
        return status;
 }
 
-/***********************************************************
- 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 (align_odd && ucs2 && (buflen % 2 == 0)) {
-               /*
-                * We're pushing into an SMB buffer, align odd
-                */
-               buf = TALLOC_REALLOC_ARRAY(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, true)) {
-               return NULL;
-       }
-
-       buf = TALLOC_REALLOC_ARRAY(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);
-}
-
-/***********************************************************
- 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.
-***********************************************************/
-
-static 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);
-}
-
 /****************************************************************************
  Open a file
  WARNING: if you open with O_WRONLY then getattrE won't work!
@@ -1459,7 +1796,7 @@ struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       state->bytes.iov_base = bytes;
+       state->bytes.iov_base = (void *)bytes;
        state->bytes.iov_len = talloc_get_size(bytes);
 
        subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
@@ -1478,13 +1815,19 @@ struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
                                 int flags, int share_mode)
 {
        struct tevent_req *req, *subreq;
+       NTSTATUS status;
 
        req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
                              &subreq);
-       if ((req == NULL) || !cli_smb_req_send(subreq)) {
-               TALLOC_FREE(req);
+       if (req == NULL) {
                return NULL;
        }
+
+       status = cli_smb_req_send(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
        return req;
 }
 
@@ -1606,12 +1949,18 @@ struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
                                uint16_t fnum)
 {
        struct tevent_req *req, *subreq;
+       NTSTATUS status;
 
        req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
-       if ((req == NULL) || !cli_smb_req_send(subreq)) {
-               TALLOC_FREE(req);
+       if (req == NULL) {
                return NULL;
        }
+
+       status = cli_smb_req_send(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
        return req;
 }
 
@@ -2055,220 +2404,579 @@ static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
 }
 
 /****************************************************************************
- POSIX Lock a file.
+ POSIX Lock a file.
+****************************************************************************/
+
+bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
+                       uint64_t offset, uint64_t len,
+                       bool wait_lock, enum brl_type lock_type)
+{
+       if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
+               return False;
+       }
+       return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
+}
+
+/****************************************************************************
+ POSIX Unlock a file.
+****************************************************************************/
+
+bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
+{
+       return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
+}
+
+/****************************************************************************
+ POSIX Get any lock covering a file.
+****************************************************************************/
+
+bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
+{
+       return True;
+}
+
+/****************************************************************************
+ Do a SMBgetattrE call.
+****************************************************************************/
+
+static void cli_getattrE_done(struct tevent_req *subreq);
+
+struct cli_getattrE_state {
+       uint16_t vwv[1];
+       int zone_offset;
+       uint16_t attr;
+       SMB_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 cli_state *cli,
+                               uint16_t fnum)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_getattrE_state *state = NULL;
+       uint8_t additional_flags = 0;
+
+       req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->zone_offset = cli->serverzone;
+       SSVAL(state->vwv+0,0,fnum);
+
+       subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
+                             1, state->vwv, 0, NULL);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_getattrE_done, req);
+       return req;
+}
+
+static void cli_getattrE_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_getattrE_state *state = tevent_req_data(
+               req, struct cli_getattrE_state);
+       uint8_t wct;
+       uint16_t *vwv = NULL;
+       NTSTATUS status;
+
+       status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+
+       state->size = (SMB_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);
+       state->write_time = make_unix_date2(vwv+4, state->zone_offset);
+
+       TALLOC_FREE(subreq);
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_getattrE_recv(struct tevent_req *req,
+                       uint16_t *attr,
+                       SMB_OFF_T *size,
+                       time_t *change_time,
+                       time_t *access_time,
+                       time_t *write_time)
+{
+       struct cli_getattrE_state *state = tevent_req_data(
+                               req, struct cli_getattrE_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       if (attr) {
+               *attr = state->attr;
+       }
+       if (size) {
+               *size = state->size;
+       }
+       if (change_time) {
+               *change_time = state->change_time;
+       }
+       if (access_time) {
+               *access_time = state->access_time;
+       }
+       if (write_time) {
+               *write_time = state->write_time;
+       }
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_getattrE(struct cli_state *cli,
+                       uint16_t fnum,
+                       uint16_t *attr,
+                       SMB_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;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_getattrE_send(frame, ev, cli, fnum);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+
+       status = cli_getattrE_recv(req,
+                                       attr,
+                                       size,
+                                       change_time,
+                                       access_time,
+                                       write_time);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+/****************************************************************************
+ Do a SMBgetatr call
 ****************************************************************************/
 
-bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
-                       uint64_t offset, uint64_t len,
-                       bool wait_lock, enum brl_type lock_type)
+static void cli_getatr_done(struct tevent_req *subreq);
+
+struct cli_getatr_state {
+       int zone_offset;
+       uint16_t attr;
+       SMB_OFF_T size;
+       time_t write_time;
+};
+
+struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname)
 {
-       if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
-               return False;
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_getatr_state *state = NULL;
+       uint8_t additional_flags = 0;
+       uint8_t *bytes = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
+       if (req == NULL) {
+               return NULL;
        }
-       return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
-}
 
-/****************************************************************************
- POSIX Unlock a file.
-****************************************************************************/
+       state->zone_offset = cli->serverzone;
 
-bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
-{
-       return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
-}
+       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,
+                                  strlen(fname)+1, NULL);
 
-/****************************************************************************
- POSIX Get any lock covering a file.
-****************************************************************************/
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
+       }
 
-bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
-{
-       return True;
+       subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
+                             0, NULL, talloc_get_size(bytes), bytes);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_getatr_done, req);
+       return req;
 }
 
-/****************************************************************************
- Do a SMBgetattrE call.
-****************************************************************************/
-
-bool cli_getattrE(struct cli_state *cli, int fd,
-                 uint16_t *attr, SMB_OFF_T *size,
-                 time_t *change_time,
-                  time_t *access_time,
-                  time_t *write_time)
+static void cli_getatr_done(struct tevent_req *subreq)
 {
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_getatr_state *state = tevent_req_data(
+               req, struct cli_getatr_state);
+       uint8_t wct;
+       uint16_t *vwv = NULL;
+       NTSTATUS status;
+
+       status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
 
-       cli_set_message(cli->outbuf,1,0,True);
+       state->attr = SVAL(vwv+0,0);
+       state->size = (SMB_OFF_T)IVAL(vwv+3,0);
+       state->write_time = make_unix_date3(vwv+1, state->zone_offset);
 
-       SCVAL(cli->outbuf,smb_com,SMBgetattrE);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       TALLOC_FREE(subreq);
+       tevent_req_done(req);
+}
 
-       SSVAL(cli->outbuf,smb_vwv0,fd);
+NTSTATUS cli_getatr_recv(struct tevent_req *req,
+                       uint16_t *attr,
+                       SMB_OFF_T *size,
+                       time_t *write_time)
+{
+       struct cli_getatr_state *state = tevent_req_data(
+                               req, struct cli_getatr_state);
+       NTSTATUS status;
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
        }
-
-       if (cli_is_error(cli)) {
-               return False;
+       if (attr) {
+               *attr = state->attr;
        }
-
        if (size) {
-               *size = IVAL(cli->inbuf, smb_vwv6);
+               *size = state->size;
+       }
+       if (write_time) {
+               *write_time = state->write_time;
        }
+       return NT_STATUS_OK;
+}
 
-       if (attr) {
-               *attr = SVAL(cli->inbuf,smb_vwv10);
+NTSTATUS cli_getatr(struct cli_state *cli,
+                       const char *fname,
+                       uint16_t *attr,
+                       SMB_OFF_T *size,
+                       time_t *write_time)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
        }
 
-       if (change_time) {
-               *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (access_time) {
-               *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
+       req = cli_getatr_send(frame, ev, cli, fname);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (write_time) {
-               *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
-       return True;
+       status = cli_getatr_recv(req,
+                               attr,
+                               size,
+                               write_time);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
- Do a SMBgetatr call
+ Do a SMBsetattrE call.
 ****************************************************************************/
 
-bool cli_getatr(struct cli_state *cli, const char *fname,
-               uint16_t *attr, SMB_OFF_T *size, time_t *write_time)
-{
-       char *p;
+static void cli_setattrE_done(struct tevent_req *subreq);
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+struct cli_setattrE_state {
+       int dummy;
+};
 
-       cli_set_message(cli->outbuf,0,0,True);
+struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               uint16_t fnum,
+                               time_t change_time,
+                               time_t access_time,
+                               time_t write_time)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_setattrE_state *state = NULL;
+       uint8_t additional_flags = 0;
+       uint16_t vwv[7];
 
-       SCVAL(cli->outbuf,smb_com,SMBgetatr);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       p = smb_buf(cli->outbuf);
-       *p++ = 4;
-       p += clistr_push(cli, p, fname,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
+       memset(vwv, '\0', sizeof(vwv));
+       SSVAL(vwv+0, 0, fnum);
+       cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
+       cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
+       cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
 
-       cli_setup_bcc(cli, p);
+       subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
+                             7, vwv, 0, NULL);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_setattrE_done, req);
+       return req;
+}
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+static void cli_setattrE_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       NTSTATUS status;
+
+       status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
        }
+       tevent_req_done(req);
+}
 
-       if (cli_is_error(cli)) {
-               return False;
+NTSTATUS cli_setattrE_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS cli_setattrE(struct cli_state *cli,
+                       uint16_t fnum,
+                       time_t change_time,
+                       time_t access_time,
+                       time_t write_time)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
        }
 
-       if (size) {
-               *size = IVAL(cli->inbuf, smb_vwv3);
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (write_time) {
-               *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
+       req = cli_setattrE_send(frame, ev,
+                       cli,
+                       fnum,
+                       change_time,
+                       access_time,
+                       write_time);
+
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (attr) {
-               *attr = SVAL(cli->inbuf,smb_vwv0);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
-       return True;
+       status = cli_setattrE_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
- Do a SMBsetattrE call.
+ Do a SMBsetatr call.
 ****************************************************************************/
 
-bool cli_setattrE(struct cli_state *cli, int fd,
-                 time_t change_time,
-                  time_t access_time,
-                  time_t write_time)
-
-{
-       char *p;
-
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
-
-       cli_set_message(cli->outbuf,7,0,True);
+static void cli_setatr_done(struct tevent_req *subreq);
 
-       SCVAL(cli->outbuf,smb_com,SMBsetattrE);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+struct cli_setatr_state {
+       uint16_t vwv[8];
+};
 
-       SSVAL(cli->outbuf,smb_vwv0, fd);
-       cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
-       cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
-       cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
+struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname,
+                               uint16_t attr,
+                               time_t mtime)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_setatr_state *state = NULL;
+       uint8_t additional_flags = 0;
+       uint8_t *bytes = NULL;
 
-       p = smb_buf(cli->outbuf);
-       *p++ = 4;
+       req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       cli_setup_bcc(cli, p);
+       memset(state->vwv, '\0', sizeof(state->vwv));
+       SSVAL(state->vwv+0, 0, attr);
+       cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+       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,
+                                  strlen(fname)+1, NULL);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
+       }
+       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+                       talloc_get_size(bytes)+1);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
        }
 
-       if (cli_is_error(cli)) {
-               return False;
+       bytes[talloc_get_size(bytes)-1] = 4;
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
+                                  1, NULL);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
        }
 
-       return True;
+       subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
+                             8, 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_setatr_done, req);
+       return req;
 }
 
-/****************************************************************************
- Do a SMBsetatr call.
-****************************************************************************/
-
-bool cli_setatr(struct cli_state *cli, const char *fname, uint16_t attr, time_t t)
+static void cli_setatr_done(struct tevent_req *subreq)
 {
-       char *p;
-
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       NTSTATUS status;
 
-       cli_set_message(cli->outbuf,8,0,True);
+       status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       tevent_req_done(req);
+}
 
-       SCVAL(cli->outbuf,smb_com,SMBsetatr);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+NTSTATUS cli_setatr_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
 
-       SSVAL(cli->outbuf,smb_vwv0, attr);
-       cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
+NTSTATUS cli_setatr(struct cli_state *cli,
+               const char *fname,
+               uint16_t attr,
+               time_t mtime)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
 
-       p = smb_buf(cli->outbuf);
-       *p++ = 4;
-       p += clistr_push(cli, p, fname,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
-       *p++ = 4;
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
 
-       cli_setup_bcc(cli, p);
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+       req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (cli_is_error(cli)) {
-               return False;
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
-       return True;
+       status = cli_setatr_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
@@ -2934,87 +3642,248 @@ static uint32_t open_flags_to_wire(int flags)
  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
 ****************************************************************************/
 
-static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
+struct posix_open_state {
+       uint16_t setup;
+       uint8_t *param;
+       uint8_t data[18];
+       uint16_t fnum; /* Out */
+};
+
+static void cli_posix_open_internal_done(struct tevent_req *subreq)
 {
-       unsigned int data_len = 0;
-       unsigned int param_len = 0;
-       uint16_t setup = TRANSACT2_SETPATHINFO;
-       char *param;
-       char data[18];
-       char *rparam=NULL, *rdata=NULL;
-       char *p;
-       uint16_t fnum = (uint16_t)-1;
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
+       NTSTATUS status;
+       uint8_t *data;
+       uint32_t num_data;
+
+       status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, &data, &num_data);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       if (num_data < 12) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       state->fnum = SVAL(data,2);
+       tevent_req_done(req);
+}
+
+static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname,
+                                       int flags,
+                                       mode_t mode,
+                                       bool is_dir)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct posix_open_state *state = NULL;
        uint32_t wire_flags = open_flags_to_wire(flags);
-       size_t srclen = 2*(strlen(fname)+1);
 
-       param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
-       if (!param) {
-               return false;
+       req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
+       if (req == NULL) {
+               return NULL;
        }
-       memset(param, '\0', 6);
-       SSVAL(param,0, SMB_POSIX_PATH_OPEN);
-       p = &param[6];
 
-       p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
 
-       if (is_dir) {
-               wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
-               wire_flags |= SMB_O_DIRECTORY;
+       /* Setup param array. */
+       state->param = talloc_array(state, uint8_t, 6);
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
        }
+       memset(state->param, '\0', 6);
+       SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
 
-       p = data;
-       SIVAL(p,0,0); /* No oplock. */
-       SIVAL(p,4,wire_flags);
-       SIVAL(p,8,unix_perms_to_wire(mode));
-       SIVAL(p,12,0); /* Top bits of perms currently undefined. */
-       SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
+       state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
+                                  strlen(fname)+1, NULL);
 
-       data_len = 18;
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
+       }
 
-       if (!cli_send_trans(cli, SMBtrans2,
-                       NULL,                        /* name */
-                       -1, 0,                          /* fid, flags */
-                       &setup, 1, 0,                   /* setup, length, max */
-                       param, param_len, 0,            /* param, length, max */
-                       (char *)&data,  data_len, cli->max_xmit /* data, length, max */
-                       )) {
-               SAFE_FREE(param);
-               return -1;
+       /* Setup data words. */
+       if (is_dir) {
+               wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
+               wire_flags |= SMB_O_DIRECTORY;
        }
 
-       SAFE_FREE(param);
+       SIVAL(state->data,0,0); /* No oplock. */
+       SIVAL(state->data,4,wire_flags);
+       SIVAL(state->data,8,unix_perms_to_wire(mode));
+       SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
+       SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-               &rparam, &param_len,
-               &rdata, &data_len)) {
-                       return -1;
+       subreq = cli_trans_send(state,                  /* mem ctx. */
+                               ev,                     /* event ctx. */
+                               cli,                    /* cli_state. */
+                               SMBtrans2,              /* cmd. */
+                               NULL,                   /* pipe name. */
+                               -1,                     /* fid. */
+                               0,                      /* function. */
+                               0,                      /* flags. */
+                               &state->setup,          /* setup. */
+                               1,                      /* num setup uint16_t words. */
+                               0,                      /* max returned setup. */
+                               state->param,           /* param. */
+                               talloc_get_size(state->param),/* num param. */
+                               2,                      /* max returned param. */
+                               state->data,            /* data. */
+                               18,                     /* num data. */
+                               12);                    /* max returned data. */
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
+       tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
+       return req;
+}
 
-       fnum = SVAL(rdata,2);
+struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname,
+                                       int flags,
+                                       mode_t mode)
+{
+       return cli_posix_open_internal_send(mem_ctx, ev,
+                               cli, fname, flags, mode, false);
+}
 
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
+NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
+{
+       struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
+       NTSTATUS status;
 
-       return fnum;
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *pfnum = state->fnum;
+       return NT_STATUS_OK;
 }
 
 /****************************************************************************
open - POSIX semantics.
Open - POSIX semantics. Doesn't request oplock.
 ****************************************************************************/
 
-int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
+NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
+                       int flags, mode_t mode, uint16_t *pfnum)
+{
+
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_posix_open_send(frame,
+                               ev,
+                               cli,
+                               fname,
+                               flags,
+                               mode);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+
+       status = cli_posix_open_recv(req, pfnum);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname,
+                                       mode_t mode)
 {
-       return cli_posix_open_internal(cli, fname, flags, mode, False);
+       return cli_posix_open_internal_send(mem_ctx, ev,
+                               cli, fname, O_CREAT, mode, true);
 }
 
-/****************************************************************************
- mkdir - POSIX semantics.
-****************************************************************************/
+NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
 
-int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
+NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
 {
-       return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_posix_mkdir_send(frame,
+                               ev,
+                               cli,
+                               fname,
+                               mode);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+
+       status = cli_posix_mkdir_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
@@ -3022,7 +3891,8 @@ int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
 ****************************************************************************/
 
 struct unlink_state {
-       int dummy;
+       uint16_t setup;
+       uint8_t data[2];
 };
 
 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
@@ -3049,9 +3919,7 @@ static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req = NULL, *subreq = NULL;
        struct unlink_state *state = NULL;
-       uint16_t setup;
        uint8_t *param = NULL;
-       uint8_t data[2];
 
        req = tevent_req_create(mem_ctx, &state, struct unlink_state);
        if (req == NULL) {
@@ -3059,11 +3927,11 @@ static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
        }
 
        /* Setup setup word. */
-       SSVAL(&setup+0, 0, TRANSACT2_SETPATHINFO);
+       SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
 
        /* Setup param array. */
        param = talloc_array(state, uint8_t, 6);
-       if (tevent_req_nomem(data, req)) {
+       if (tevent_req_nomem(param, req)) {
                return tevent_req_post(req, ev);
        }
        memset(param, '\0', 6);
@@ -3077,7 +3945,7 @@ static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
        }
 
        /* Setup data word. */
-       SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
+       SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
                        SMB_POSIX_UNLINK_FILE_TARGET);
 
        subreq = cli_trans_send(state,                  /* mem ctx. */
@@ -3088,13 +3956,13 @@ static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
                                -1,                     /* fid. */
                                0,                      /* function. */
                                0,                      /* flags. */
-                               &setup,                 /* setup. */
+                               &state->setup,          /* setup. */
                                1,                      /* num setup uint16_t words. */
                                0,                      /* max returned setup. */
                                param,                  /* param. */
                                talloc_get_size(param), /* num param. */
                                2,                      /* max returned param. */
-                               data,                   /* data. */
+                               state->data,            /* data. */
                                2,                      /* num data. */
                                0);                     /* max returned data. */
 
@@ -3113,7 +3981,7 @@ struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
        return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
 }
 
-NTSTATUS cli_posix_unlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
+NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
 {
        NTSTATUS status;
 
@@ -3162,7 +4030,7 @@ NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
                goto fail;
        }
 
-       status = cli_posix_unlink_recv(req, frame);
+       status = cli_posix_unlink_recv(req);
 
  fail:
        TALLOC_FREE(frame);