Fix bug 6726 - Filename length overwrites oplock request field in cli_nt_create().
[ira/wip.git] / source3 / libsmb / clifile.c
index fdfa257ec82d1dc49d52ee2bbeb21b9764d1271e..b15aa8d998499a3fbad82134e0f5cf7b9c6edb50 100644 (file)
 
 #include "includes.h"
 
-/****************************************************************************
- Hard/Symlink a file (UNIX extensions).
- Creates new name (sym)linked to oldname.
-****************************************************************************/
+/***********************************************************
+ 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 bool cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, bool hard_link)
+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)
 {
-       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);
-
-       param = SMB_MALLOC_ARRAY(char, 6+newlen+2);
+       size_t buflen;
+       char *converted;
+       size_t converted_size;
 
-       if (!param) {
-               return false;
+       if (buf == NULL) {
+               return NULL;
        }
 
-       data = SMB_MALLOC_ARRAY(char, oldlen+2);
+       buflen = talloc_get_size(buf);
 
-       if (!data) {
-               SAFE_FREE(param);
-               return false;
+       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;
        }
 
-       SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
-       SIVAL(param,2,0);
-       p = &param[6];
-
-       p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
-
-       p = data;
-       p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
-       data_len = PTR_DIFF(p, data);
+       if (!convert_string_talloc(talloc_tos(), CH_UNIX,
+                                  ucs2 ? CH_UTF16LE : CH_DOS,
+                                  str, str_len, &converted,
+                                  &converted_size, true)) {
+               return 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;
+       buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
+                                  buflen + converted_size);
+       if (buf == NULL) {
+               TALLOC_FREE(converted);
+               return NULL;
        }
 
-       SAFE_FREE(data);
-       SAFE_FREE(param);
+       memcpy(buf + buflen, converted, converted_size);
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-                       &rparam, &param_len,
-                       &rdata, &data_len)) {
-                       return false;
-       }
+       TALLOC_FREE(converted);
 
-       SAFE_FREE(data);
-       SAFE_FREE(param);
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
+       if (pconverted_size) {
+               *pconverted_size = converted_size;
+       }
 
-       return true;
+       return buf;
 }
 
-/****************************************************************************
- Map standard UNIX permissions onto wire representations.
-****************************************************************************/
+/***********************************************************
+ Push a string into an SMB buffer, with odd byte alignment
+ if it's a UCS2 string.
+***********************************************************/
 
-uint32_t unix_perms_to_wire(mode_t perms)
+uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
+                           const char *str, size_t str_len,
+                           size_t *pconverted_size)
 {
-        unsigned int ret = 0;
-
-        ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
-        ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
-        ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
-        ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
-        ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
-        ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
-        ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
-        ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
-        ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
-#ifdef S_ISVTX
-        ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
-#endif
-#ifdef S_ISGID
-        ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
-#endif
-#ifdef S_ISUID
-        ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
-#endif
-        return ret;
+       return internal_bytes_push_str(buf, ucs2, str, str_len,
+                       true, pconverted_size);
 }
 
-/****************************************************************************
- Map wire permissions to standard UNIX.
-****************************************************************************/
+/***********************************************************
+ 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.
+***********************************************************/
 
-mode_t wire_perms_to_unix(uint32_t perms)
+static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
+                           const char *str, size_t str_len,
+                           size_t *pconverted_size)
 {
-        mode_t ret = (mode_t)0;
-
-        ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
-        ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
-        ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
-        ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
-        ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
-        ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
-        ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
-        ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
-        ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
-#ifdef S_ISVTX
-        ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
-#endif
-#ifdef S_ISGID
-        ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
-#endif
-#ifdef S_ISUID
-        ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
-#endif
-        return ret;
+       return internal_bytes_push_str(buf, ucs2, str, str_len,
+                       false, pconverted_size);
 }
 
 /****************************************************************************
- Return the file type from the wire filetype for UNIX extensions.
+ Hard/Symlink a file (UNIX extensions).
+ Creates new name (sym)linked to oldname.
 ****************************************************************************/
 
-static mode_t unix_filetype_from_wire(uint32_t wire_type)
+struct link_state {
+       uint16_t setup;
+       uint8_t *param;
+       uint8_t *data;
+};
+
+static void cli_posix_link_internal_done(struct tevent_req *subreq)
 {
-       switch (wire_type) {
-               case UNIX_TYPE_FILE:
-                       return S_IFREG;
-               case UNIX_TYPE_DIR:
-                       return S_IFDIR;
-#ifdef S_IFLNK
-               case UNIX_TYPE_SYMLINK:
-                       return S_IFLNK;
-#endif
-#ifdef S_IFCHR
-               case UNIX_TYPE_CHARDEV:
-                       return S_IFCHR;
-#endif
-#ifdef S_IFBLK
-               case UNIX_TYPE_BLKDEV:
-                       return S_IFBLK;
-#endif
-#ifdef S_IFIFO
-               case UNIX_TYPE_FIFO:
-                       return S_IFIFO;
-#endif
-#ifdef S_IFSOCK
-               case UNIX_TYPE_SOCKET:
-                       return S_IFSOCK;
-#endif
-               default:
-                       return (mode_t)0;
+       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);
 }
 
-/****************************************************************************
- Do a POSIX getfacl (UNIX extensions).
-****************************************************************************/
-
-bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
+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)
 {
-       unsigned int param_len = 0;
-       unsigned int data_len = 0;
-       uint16_t setup = TRANSACT2_QPATHINFO;
-       char *param;
-       size_t nlen = 2*(strlen(name)+1);
-       char *rparam=NULL, *rdata=NULL;
-       char *p;
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct link_state *state = NULL;
 
-       param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
-       if (!param) {
-               return false;
+       req = tevent_req_create(mem_ctx, &state, struct link_state);
+       if (req == NULL) {
+               return NULL;
        }
 
-       p = param;
-       memset(p, '\0', 6);
-       SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
-       p += 6;
-       p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
 
-       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 */
-               NULL,  0, cli->max_xmit      /* data, length, max */
-               )) {
-               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);
 
-       SAFE_FREE(param);
+       state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), newname,
+                                  strlen(newname)+1, NULL);
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-                       &rparam, &param_len,
-                       &rdata, &data_len)) {
-               return false;
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
        }
 
-       if (data_len < 6) {
-               SAFE_FREE(rdata);
-               SAFE_FREE(rparam);
-               return false;
+       /* 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);
 
-       SAFE_FREE(rparam);
-       *retbuf = rdata;
-       *prb_size = (size_t)data_len;
+       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. */
 
-       return true;
+       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;
 }
 
 /****************************************************************************
- Stat a file (UNIX extensions).
+ Symlink a file (UNIX extensions).
 ****************************************************************************/
 
-bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
+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)
 {
-       unsigned int param_len = 0;
-       unsigned int data_len = 0;
-       uint16_t setup = TRANSACT2_QPATHINFO;
-       char *param;
-       size_t nlen = 2*(strlen(name)+1);
-       char *rparam=NULL, *rdata=NULL;
-       char *p;
+       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;
+}
+
+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;
+
+       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;
+}
+
+/****************************************************************************
+ Map standard UNIX permissions onto wire representations.
+****************************************************************************/
+
+uint32_t unix_perms_to_wire(mode_t perms)
+{
+        unsigned int ret = 0;
+
+        ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
+        ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
+        ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
+        ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
+        ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
+        ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
+        ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
+        ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
+        ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
+#ifdef S_ISVTX
+        ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
+#endif
+#ifdef S_ISGID
+        ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
+#endif
+#ifdef S_ISUID
+        ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
+#endif
+        return ret;
+}
+
+/****************************************************************************
+ Map wire permissions to standard UNIX.
+****************************************************************************/
+
+mode_t wire_perms_to_unix(uint32_t perms)
+{
+        mode_t ret = (mode_t)0;
+
+        ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
+        ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
+        ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
+        ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
+        ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
+        ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
+        ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
+        ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
+        ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
+#ifdef S_ISVTX
+        ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
+#endif
+#ifdef S_ISGID
+        ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
+#endif
+#ifdef S_ISUID
+        ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
+#endif
+        return ret;
+}
+
+/****************************************************************************
+ Return the file type from the wire filetype for UNIX extensions.
+****************************************************************************/
+
+static mode_t unix_filetype_from_wire(uint32_t wire_type)
+{
+       switch (wire_type) {
+               case UNIX_TYPE_FILE:
+                       return S_IFREG;
+               case UNIX_TYPE_DIR:
+                       return S_IFDIR;
+#ifdef S_IFLNK
+               case UNIX_TYPE_SYMLINK:
+                       return S_IFLNK;
+#endif
+#ifdef S_IFCHR
+               case UNIX_TYPE_CHARDEV:
+                       return S_IFCHR;
+#endif
+#ifdef S_IFBLK
+               case UNIX_TYPE_BLKDEV:
+                       return S_IFBLK;
+#endif
+#ifdef S_IFIFO
+               case UNIX_TYPE_FIFO:
+                       return S_IFIFO;
+#endif
+#ifdef S_IFSOCK
+               case UNIX_TYPE_SOCKET:
+                       return S_IFSOCK;
+#endif
+               default:
+                       return (mode_t)0;
+       }
+}
+
+/****************************************************************************
+ Do a POSIX getfacl (UNIX extensions).
+****************************************************************************/
+
+struct getfacl_state {
+       uint16_t setup;
+       uint8_t *param;
+       uint32_t num_data;
+       uint8_t *data;
+};
+
+static void cli_posix_getfacl_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);
+       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;
+       }
+       tevent_req_done(req);
+}
+
+struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct link_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       ZERO_STRUCTP(sbuf);
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_QPATHINFO);
 
-       param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
-       if (!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);
        }
-       p = param;
-       memset(p, '\0', 6);
-       SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
-       p += 6;
-       p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
+       memset(state->param, '\0', 6);
+       SSVAL(state->param, 0, SMB_QUERY_POSIX_ACL);
 
-       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 */
-                       NULL,  0, cli->max_xmit      /* data, length, max */
-                       )) {
-               SAFE_FREE(param);
-               return false;
+       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);
        }
 
-       SAFE_FREE(param);
+       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. */
+                               cli->max_xmit);         /* max returned data. */
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-                       &rparam, &param_len,
-                       &rdata, &data_len)) {
-               return false;
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
+       tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
+       return req;
+}
 
-       if (data_len < 96) {
-               SAFE_FREE(rdata);
-               SAFE_FREE(rparam);
-               return false;
+NTSTATUS cli_posix_getfacl_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);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *prb_size = (size_t)state->num_data;
+       *retbuf = (char *)talloc_move(mem_ctx, &state->data);
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_posix_getfacl(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_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_getfacl_send(frame,
+                               ev,
+                               cli,
+                               fname);
+       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_getfacl_recv(req, mem_ctx, prb_size, retbuf);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+/****************************************************************************
+ Stat a file (UNIX extensions).
+****************************************************************************/
+
+struct stat_state {
+       uint16_t setup;
+       uint8_t *param;
+       uint32_t num_data;
+       uint8_t *data;
+};
+
+static void cli_posix_stat_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       struct stat_state *state = tevent_req_data(req, struct stat_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;
+       }
+       tevent_req_done(req);
+}
+
+struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct stat_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct stat_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_BASIC);
+
+       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. */
+                               96);                    /* max returned data. */
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_posix_stat_done, req);
+       return req;
+}
+
+NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
+                               SMB_STRUCT_STAT *sbuf)
+{
+       struct stat_state *state = tevent_req_data(req, struct stat_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+
+       if (state->num_data != 96) {
+               return NT_STATUS_DATA_ERROR;
        }
 
-       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(state->data,0);     /* total size, in bytes */
+       sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,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((char *)(state->data + 16));    /* time of last change */
+       sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24));    /* time of last access */
+       sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 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(state->data,40);      /* user ID of owner */
+       sbuf->st_ex_gid = (gid_t) IVAL(state->data,48);      /* group ID of owner */
+       sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 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);
+               uint32_t dev_major = IVAL(state->data,60);
+               uint32_t dev_minor = IVAL(state->data,68);
+               sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
+       }
+#endif
+       sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76);      /* inode */
+       sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84));     /* protection */
+       sbuf->st_ex_nlink = IVAL(state->data,92);    /* number of hard links */
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_posix_stat(struct cli_state *cli,
+                       const char *fname,
+                       SMB_STRUCT_STAT *sbuf)
+{
+       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_stat_send(frame,
+                               ev,
+                               cli,
+                               fname);
+       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_stat_recv(req, sbuf);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+/****************************************************************************
+ Chmod or chown a file internal (UNIX extensions).
+****************************************************************************/
+
+struct ch_state {
+       uint16_t setup;
+       uint8_t *param;
+       uint8_t *data;
+};
+
+static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       struct ch_state *state = tevent_req_data(req, struct ch_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);
+}
+
+static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname,
+                                       uint32_t mode,
+                                       uint32_t uid,
+                                       uint32_t gid)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct ch_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct ch_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
+
+       /* 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_SET_FILE_UNIX_BASIC);
+
+       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);
+       }
+
+       /* Setup data array. */
+       state->data = talloc_array(state, uint8_t, 100);
+       if (tevent_req_nomem(state->data, req)) {
+               return tevent_req_post(req, ev);
        }
-#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 */
+       memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
+       memset(&state->data[40], '\0', 60);
+       SIVAL(state->data,40,uid);
+       SIVAL(state->data,48,gid);
+       SIVAL(state->data,84,mode);
 
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
+       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. */
 
-       return true;
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done, req);
+       return req;
 }
 
 /****************************************************************************
Symlink a file (UNIX extensions).
chmod a file (UNIX extensions).
 ****************************************************************************/
 
-bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
+struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       const char *fname,
+                                       mode_t mode)
 {
-       return cli_link_internal(cli, oldname, newname, False);
+       return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
+                       fname,
+                       unix_perms_to_wire(mode),
+                       SMB_UID_NO_CHANGE,
+                       SMB_GID_NO_CHANGE);
 }
 
-/****************************************************************************
- Hard a file (UNIX extensions).
-****************************************************************************/
-
-bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
+NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
 {
-       return cli_link_internal(cli, oldname, newname, True);
-}
+       NTSTATUS status;
 
-/****************************************************************************
- Chmod or chown a file internal (UNIX extensions).
-****************************************************************************/
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
 
-static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32_t mode, uint32_t uid, uint32_t gid)
+NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
 {
-       unsigned int data_len = 0;
-       unsigned int param_len = 0;
-       uint16_t setup = TRANSACT2_SETPATHINFO;
-       size_t nlen = 2*(strlen(fname)+1);
-       char *param;
-       char data[100];
-       char *rparam=NULL, *rdata=NULL;
-       char *p;
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       NTSTATUS status = NT_STATUS_OK;
 
-       param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
-       if (!param) {
-               return false;
+       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;
        }
-       memset(param, '\0', 6);
-       memset(data, 0, sizeof(data));
-
-       SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
-       p = &param[6];
-
-       p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
 
-       memset(data, 0xff, 40); /* Set all sizes/times to no change. */
-
-       SIVAL(data,40,uid);
-       SIVAL(data,48,gid);
-       SIVAL(data,84,mode);
-
-       data_len = 100;
-
-       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 */
-                       (char *)&data,  data_len, cli->max_xmit /* data, length, max */
-                       )) {
-               SAFE_FREE(param);
-               return False;
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       SAFE_FREE(param);
+       req = cli_posix_chmod_send(frame,
+                               ev,
+                               cli,
+                               fname,
+                               mode);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-                       &rparam, &param_len,
-                       &rdata, &data_len)) {
-               return false;
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
+       status = cli_posix_chmod_recv(req);
 
-       return true;
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
- chmod a file (UNIX extensions).
+ chown a file (UNIX extensions).
 ****************************************************************************/
 
-bool cli_unix_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 cli_state *cli,
+                                       const char *fname,
+                                       uid_t uid,
+                                       gid_t gid)
 {
-       return cli_unix_chmod_chown_internal(cli, fname,
-               unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
+       return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
+                       fname,
+                       SMB_MODE_NO_CHANGE,
+                       (uint32_t)uid,
+                       (uint32_t)gid);
 }
 
-/****************************************************************************
- chown a file (UNIX extensions).
-****************************************************************************/
+NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
 
-bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
+NTSTATUS cli_posix_chown(struct cli_state *cli,
+                       const char *fname,
+                       uid_t uid,
+                       gid_t gid)
 {
-       return cli_unix_chmod_chown_internal(cli, fname,
-                       SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
+       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_chown_send(frame,
+                               ev,
+                               cli,
+                               fname,
+                               uid,
+                               gid);
+       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_chown_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
@@ -1043,11 +1807,138 @@ NTSTATUS cli_rmdir_recv(struct tevent_req *req)
        return tevent_req_simple_recv_ntstatus(req);
 }
 
-NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
+NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       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_rmdir_send(frame, ev, cli, dname);
+       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_rmdir_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+/****************************************************************************
+ Set or clear the delete on close flag.
+****************************************************************************/
+
+struct doc_state {
+       uint16_t setup;
+       uint8_t param[6];
+       uint8_t data[1];
+};
+
+static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       struct doc_state *state = tevent_req_data(req, struct doc_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);
+}
+
+struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       uint16_t fnum,
+                                       bool flag)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct doc_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct doc_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
+
+       /* Setup param array. */
+       SSVAL(state->param,0,fnum);
+       SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
+
+       /* Setup data array. */
+       SCVAL(&state->data[0], 0, flag ? 1 : 0);
+
+       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. */
+                               6,                      /* num param. */
+                               2,                      /* max returned param. */
+                               state->data,            /* data. */
+                               1,                      /* num data. */
+                               0);                     /* max returned data. */
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
+       return req;
+}
+
+NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
-       struct tevent_req *req;
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
        if (cli_has_async_calls(cli)) {
@@ -1064,7 +1955,11 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
                goto fail;
        }
 
-       req = cli_rmdir_send(frame, ev, cli, dname);
+       req = cli_nt_delete_on_close_send(frame,
+                               ev,
+                               cli,
+                               fnum,
+                               flag);
        if (req == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1075,7 +1970,7 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
                goto fail;
        }
 
-       status = cli_rmdir_recv(req);
+       status = cli_nt_delete_on_close_recv(req);
 
  fail:
        TALLOC_FREE(frame);
@@ -1085,47 +1980,6 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
        return status;
 }
 
-/****************************************************************************
- Set or clear the delete on close flag.
-****************************************************************************/
-
-int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
-{
-       unsigned int data_len = 1;
-       unsigned int param_len = 6;
-       uint16_t setup = TRANSACT2_SETFILEINFO;
-       char param[6];
-       unsigned char data;
-       char *rparam=NULL, *rdata=NULL;
-
-       memset(param, 0, param_len);
-       SSVAL(param,0,fnum);
-       SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
-
-       data = flag ? 1 : 0;
-
-       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 */
-                       (char *)&data,  data_len, cli->max_xmit /* data, length, max */
-                       )) {
-               return false;
-       }
-
-       if (!cli_receive_trans(cli, SMBtrans2,
-                       &rparam, &param_len,
-                       &rdata, &data_len)) {
-               return false;
-       }
-
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
-
-       return true;
-}
-
 struct cli_ntcreate_state {
        uint16_t vwv[24];
        uint16_t fnum;
@@ -1155,6 +2009,7 @@ struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
        if (req == NULL) {
                return NULL;
        }
+
        vwv = state->vwv;
 
        SCVAL(vwv+0, 0, 0xFF);
@@ -1189,7 +2044,7 @@ struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       SIVAL(vwv+2, 1, converted_len);
+       SSVAL(vwv+2, 1, converted_len);
 
        subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
                              talloc_get_size(bytes), bytes);
@@ -1288,92 +2143,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!
@@ -1465,7 +2234,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,
@@ -1484,13 +2253,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;
 }
 
@@ -1592,6 +2367,7 @@ struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
        if (req == NULL) {
                return NULL;
        }
+
        SSVAL(state->vwv+0, 0, fnum);
        SIVALS(state->vwv+1, 0, -1);
 
@@ -1612,12 +2388,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;
 }
 
@@ -1644,8 +2426,135 @@ 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;
-       struct tevent_req *req;
+       struct event_context *ev;
+       struct tevent_req *req;
+       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_close_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_close_recv(req);
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+/****************************************************************************
+ Truncate a file to a specified size
+****************************************************************************/
+
+struct ftrunc_state {
+       uint16_t setup;
+       uint8_t param[6];
+       uint8_t data[8];
+};
+
+static void cli_ftruncate_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       struct ftrunc_state *state = tevent_req_data(req, struct ftrunc_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);
+}
+
+struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       uint16_t fnum,
+                                       uint64_t size)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct ftrunc_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
+
+       /* Setup param array. */
+       SSVAL(state->param,0,fnum);
+       SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
+       SSVAL(state->param,4,0);
+
+       /* Setup data array. */
+        SBVAL(state->data, 0, size);
+
+       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. */
+                               6,                      /* num param. */
+                               2,                      /* max returned param. */
+                               state->data,            /* data. */
+                               8,                      /* num data. */
+                               0);                     /* max returned data. */
+
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_ftruncate_done, req);
+       return req;
+}
+
+NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
        if (cli_has_async_calls(cli)) {
@@ -1662,7 +2571,11 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
                goto fail;
        }
 
-       req = cli_close_send(frame, ev, cli, fnum);
+       req = cli_ftruncate_send(frame,
+                               ev,
+                               cli,
+                               fnum,
+                               size);
        if (req == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -1673,7 +2586,8 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
                goto fail;
        }
 
-       status = cli_close_recv(req);
+       status = cli_ftruncate_recv(req);
+
  fail:
        TALLOC_FREE(frame);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1682,55 +2596,6 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
        return status;
 }
 
-/****************************************************************************
- Truncate a file to a specified size
-****************************************************************************/
-
-bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
-{
-       unsigned int param_len = 6;
-       unsigned int data_len = 8;
-       uint16_t setup = TRANSACT2_SETFILEINFO;
-       char param[6];
-       unsigned char data[8];
-       char *rparam=NULL, *rdata=NULL;
-       int saved_timeout = cli->timeout;
-
-       SSVAL(param,0,fnum);
-       SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
-       SSVAL(param,4,0);
-
-        SBVAL(data, 0, size);
-
-       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 */
-                            (char *)&data,  data_len,/* data, length, ... */
-                            cli->max_xmit)) {        /* ... max */
-               cli->timeout = saved_timeout;
-               return False;
-       }
-
-       if (!cli_receive_trans(cli, SMBtrans2,
-                               &rparam, &param_len,
-                               &rdata, &data_len)) {
-               cli->timeout = saved_timeout;
-               SAFE_FREE(rdata);
-               SAFE_FREE(rparam);
-               return False;
-       }
-
-       cli->timeout = saved_timeout;
-
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
-
-       return True;
-}
-
-
 /****************************************************************************
  send a lock with a specified locktype
  this is used for testing LOCKING_ANDX_CANCEL_LOCK
@@ -1844,42 +2709,114 @@ bool cli_lock(struct cli_state *cli, uint16_t fnum,
  Unlock a file.
 ****************************************************************************/
 
-bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
+struct cli_unlock_state {
+       uint16_t vwv[8];
+       uint8_t data[10];
+};
+
+static void cli_unlock_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               uint16_t fnum,
+                               uint64_t offset,
+                               uint64_t len)
+
 {
-       char *p;
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_unlock_state *state = NULL;
+       uint8_t additional_flags = 0;
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+       req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       cli_set_message(cli->outbuf,8,0,True);
+       SCVAL(state->vwv+0, 0, 0xFF);
+       SSVAL(state->vwv+2, 0, fnum);
+       SCVAL(state->vwv+3, 0, 0);
+       SIVALS(state->vwv+4, 0, 0);
+       SSVAL(state->vwv+6, 0, 1);
+       SSVAL(state->vwv+7, 0, 0);
 
-       SCVAL(cli->outbuf,smb_com,SMBlockingX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       SSVAL(state->data, 0, cli->pid);
+       SIVAL(state->data, 2, offset);
+       SIVAL(state->data, 6, len);
 
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-       SCVAL(cli->outbuf,smb_vwv3,0);
-       SIVALS(cli->outbuf, smb_vwv4, 0);
-       SSVAL(cli->outbuf,smb_vwv6,1);
-       SSVAL(cli->outbuf,smb_vwv7,0);
+       subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
+                               8, state->vwv, 10, state->data);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_unlock_done, req);
+       return req;
+}
 
-       p = smb_buf(cli->outbuf);
-       SSVAL(p, 0, cli->pid);
-       SIVAL(p, 2, offset);
-       SIVAL(p, 6, len);
-       p += 10;
-       cli_setup_bcc(cli, p);
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+static void cli_unlock_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_unlock_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS cli_unlock(struct cli_state *cli,
+                       uint16_t fnum,
+                       uint32_t offset,
+                       uint32_t len)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       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;
        }
 
-       return True;
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_unlock_send(frame, ev, cli,
+                       fnum, offset, 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_unlock_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
@@ -1947,149 +2884,378 @@ bool cli_lock64(struct cli_state *cli, uint16_t fnum,
  Unlock a file with 64 bit offsets.
 ****************************************************************************/
 
-bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
+struct cli_unlock64_state {
+       uint16_t vwv[8];
+       uint8_t data[20];
+};
+
+static void cli_unlock64_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               uint16_t fnum,
+                               uint64_t offset,
+                               uint64_t len)
+
 {
-       char *p;
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_unlock64_state *state = NULL;
+       uint8_t additional_flags = 0;
 
-       if (! (cli->capabilities & CAP_LARGE_FILES)) {
-               return cli_unlock(cli, fnum, offset, len);
+       req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
+       if (req == NULL) {
+               return NULL;
        }
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+        SCVAL(state->vwv+0, 0, 0xff);
+       SSVAL(state->vwv+2, 0, fnum);
+       SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
+       SIVALS(state->vwv+4, 0, 0);
+       SSVAL(state->vwv+6, 0, 1);
+       SSVAL(state->vwv+7, 0, 0);
 
-       cli_set_message(cli->outbuf,8,0,True);
+       SIVAL(state->data, 0, cli->pid);
+       SOFF_T_R(state->data, 4, offset);
+       SOFF_T_R(state->data, 12, len);
 
-       SCVAL(cli->outbuf,smb_com,SMBlockingX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
+                               8, state->vwv, 20, state->data);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_unlock64_done, req);
+       return req;
+}
 
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-       SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
-       SIVALS(cli->outbuf, smb_vwv4, 0);
-       SSVAL(cli->outbuf,smb_vwv6,1);
-       SSVAL(cli->outbuf,smb_vwv7,0);
+static void cli_unlock64_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       NTSTATUS status;
 
-       p = smb_buf(cli->outbuf);
-       SIVAL(p, 0, cli->pid);
-       SOFF_T_R(p, 4, offset);
-       SOFF_T_R(p, 12, len);
-       p += 20;
-       cli_setup_bcc(cli, p);
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+       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_unlock64_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS cli_unlock64(struct cli_state *cli,
+                               uint16_t fnum,
+                               uint64_t offset,
+                               uint64_t len)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (! (cli->capabilities & CAP_LARGE_FILES)) {
+               return cli_unlock(cli, fnum, offset, len);
        }
 
-       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_unlock64_send(frame, ev, cli,
+                       fnum, offset, 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_unlock64_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
  Get/unlock a POSIX lock on a file - internal function.
 ****************************************************************************/
 
-static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
-               uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
+struct posix_lock_state {
+        uint16_t setup;
+       uint8_t param[4];
+        uint8_t data[POSIX_LOCK_DATA_SIZE];
+};
+
+static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
 {
-       unsigned int param_len = 4;
-       unsigned int data_len = POSIX_LOCK_DATA_SIZE;
-       uint16_t setup = TRANSACT2_SETFILEINFO;
-       char param[4];
-       unsigned char data[POSIX_LOCK_DATA_SIZE];
-       char *rparam=NULL, *rdata=NULL;
-       int saved_timeout = cli->timeout;
+       struct tevent_req *req = tevent_req_callback_data(
+                                       subreq, struct tevent_req);
+       struct posix_lock_state *state = tevent_req_data(req, struct posix_lock_state);
+       NTSTATUS status;
 
-       SSVAL(param,0,fnum);
-       SSVAL(param,2,SMB_SET_POSIX_LOCK);
+       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);
+}
+
+static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       uint16_t fnum,
+                                       uint64_t offset,
+                                       uint64_t len,
+                                       bool wait_lock,
+                                       enum brl_type lock_type)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct posix_lock_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       /* Setup setup word. */
+       SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
+
+       /* Setup param array. */
+       SSVAL(&state->param, 0, fnum);
+       SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
 
+       /* Setup data array. */
        switch (lock_type) {
                case READ_LOCK:
-                       SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
+                       SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
+                               POSIX_LOCK_TYPE_READ);
                        break;
                case WRITE_LOCK:
-                       SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
+                       SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
+                               POSIX_LOCK_TYPE_WRITE);
                        break;
                case UNLOCK_LOCK:
-                       SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
+                       SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
+                               POSIX_LOCK_TYPE_UNLOCK);
                        break;
                default:
-                       return False;
+                       return NULL;
        }
 
        if (wait_lock) {
-               SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
-               cli->timeout = 0x7FFFFFFF;
+               SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
+                               POSIX_LOCK_FLAG_WAIT);
        } else {
-               SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
-       }
-
-       SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
-       SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
-       SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
-
-       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 */
-                       (char *)&data,  data_len, cli->max_xmit /* data, length, max */
-                       )) {
-               cli->timeout = saved_timeout;
-               return False;
-       }
+               SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
+                               POSIX_LOCK_FLAG_NOWAIT);
+       }
+
+       SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
+       SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
+       SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
+
+       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. */
+                               4,                      /* num param. */
+                               2,                      /* max returned param. */
+                               state->data,            /* data. */
+                               POSIX_LOCK_DATA_SIZE,   /* num data. */
+                               0);                     /* max returned data. */
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-                               &rparam, &param_len,
-                               &rdata, &data_len)) {
-               cli->timeout = saved_timeout;
-               SAFE_FREE(rdata);
-               SAFE_FREE(rparam);
-               return False;
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
-
-       cli->timeout = saved_timeout;
-
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
-
-       return True;
+       tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
+       return req;
 }
 
 /****************************************************************************
  POSIX Lock a file.
 ****************************************************************************/
 
-bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
+struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct cli_state *cli,
+                                       uint16_t fnum,
+                                       uint64_t offset,
+                                       uint64_t len,
+                                       bool wait_lock,
+                                       enum brl_type lock_type)
+{
+       return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
+                                       wait_lock, lock_type);
+}
+
+NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
                        uint64_t offset, uint64_t len,
                        bool wait_lock, enum brl_type lock_type)
 {
+       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 (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
-               return False;
+               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_lock_send(frame,
+                               ev,
+                               cli,
+                               fnum,
+                               offset,
+                               len,
+                               wait_lock,
+                               lock_type);
+       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_lock_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
        }
-       return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
+       return status;
 }
 
 /****************************************************************************
  POSIX Unlock a file.
 ****************************************************************************/
 
-bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
+struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       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);
+       return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
+                                       false, UNLOCK_LOCK);
 }
 
-/****************************************************************************
- POSIX Get any lock covering a file.
-****************************************************************************/
+NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
+{
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       return NT_STATUS_OK;
+}
 
-bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
+NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
 {
-       return True;
+       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_unlock_send(frame,
+                               ev,
+                               cli,
+                               fnum,
+                               offset,
+                               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_unlock_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
@@ -2400,7 +3566,7 @@ NTSTATUS cli_getatr(struct cli_state *cli,
 static void cli_setattrE_done(struct tevent_req *subreq);
 
 struct cli_setattrE_state {
-       int dummy;
+       uint16_t vwv[7];
 };
 
 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
@@ -2414,21 +3580,19 @@ struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
        struct tevent_req *req = NULL, *subreq = NULL;
        struct cli_setattrE_state *state = NULL;
        uint8_t additional_flags = 0;
-       uint16_t vwv[7];
 
        req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
        if (req == NULL) {
                return NULL;
        }
 
-       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);
+       SSVAL(state->vwv+0, 0, fnum);
+       cli_put_dos_date2(cli, (char *)&state->vwv[1], 0, change_time);
+       cli_put_dos_date2(cli, (char *)&state->vwv[3], 0, access_time);
+       cli_put_dos_date2(cli, (char *)&state->vwv[5], 0, write_time);
 
        subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
-                             7, vwv, 0, NULL);
+                             7, state->vwv, 0, NULL);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -2535,7 +3699,6 @@ struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       memset(state->vwv, '\0', sizeof(state->vwv));
        SSVAL(state->vwv+0, 0, attr);
        cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
 
@@ -2784,57 +3947,215 @@ struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
-                             0, NULL, 0, NULL);
+       subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
+                             0, NULL, 0, NULL);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_dskattr_done, req);
+       return req;
+}
+
+static void cli_dskattr_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_dskattr_state *state = tevent_req_data(
+               req, struct cli_dskattr_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;
+       }
+       state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
+       state->total = SVAL(vwv+0, 0);
+       state->avail = SVAL(vwv+3, 0);
+       TALLOC_FREE(subreq);
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
+{
+       struct cli_dskattr_state *state = tevent_req_data(
+                               req, struct cli_dskattr_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *bsize = state->bsize;
+       *total = state->total;
+       *avail = state->avail;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
+{
+       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_dskattr_send(frame, ev, cli);
+       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_dskattr_recv(req, bsize, total, avail);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}
+
+/****************************************************************************
+ Create and open a temporary file.
+****************************************************************************/
+
+static void cli_ctemp_done(struct tevent_req *subreq);
+
+struct ctemp_state {
+       uint16_t vwv[3];
+       char *ret_path;
+       uint16_t fnum;
+};
+
+struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
+                               struct event_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;
+       uint8_t *bytes = NULL;
+
+       req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       SSVAL(state->vwv,0,0);
+       SIVALS(state->vwv+1,0,-1);
+
+       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), path,
+                                  strlen(path)+1, NULL);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
+                             3, 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_dskattr_done, req);
+       tevent_req_set_callback(subreq, cli_ctemp_done, req);
        return req;
 }
 
-static void cli_dskattr_done(struct tevent_req *subreq)
+static void cli_ctemp_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
-               subreq, struct tevent_req);
-       struct cli_dskattr_state *state = tevent_req_data(
-               req, struct cli_dskattr_state);
-       uint8_t wct;
-       uint16_t *vwv = NULL;
+                               subreq, struct tevent_req);
+       struct ctemp_state *state = tevent_req_data(
+                               req, struct ctemp_state);
        NTSTATUS status;
+       uint8_t wcnt;
+       uint16_t *vwv;
+       uint32_t num_bytes = 0;
+       uint8_t *bytes = NULL;
 
-       status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
+       status = cli_smb_recv(subreq, 1, &wcnt, &vwv, &num_bytes, &bytes);
        if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(subreq);
                tevent_req_nterror(req, status);
                return;
        }
-       state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
-       state->total = SVAL(vwv+0, 0);
-       state->avail = SVAL(vwv+3, 0);
+
+       state->fnum = SVAL(vwv+0, 0);
+
        TALLOC_FREE(subreq);
+
+       /* From W2K3, the result is just the ASCII name */
+       if (num_bytes < 2) {
+               tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
+               return;
+       }
+
+       if (pull_string_talloc(state,
+                       NULL,
+                       0,
+                       &state->ret_path,
+                       bytes,
+                       num_bytes,
+                       STR_ASCII) == 0) {
+               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
+               return;
+       }
        tevent_req_done(req);
 }
 
-NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
+NTSTATUS cli_ctemp_recv(struct tevent_req *req,
+                       TALLOC_CTX *ctx,
+                       uint16_t *pfnum,
+                       char **outfile)
 {
-       struct cli_dskattr_state *state = tevent_req_data(
-                               req, struct cli_dskattr_state);
+       struct ctemp_state *state = tevent_req_data(req,
+                       struct ctemp_state);
        NTSTATUS status;
 
        if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
-       *bsize = state->bsize;
-       *total = state->total;
-       *avail = state->avail;
+       *pfnum = state->fnum;
+       *outfile = talloc_strdup(ctx, state->ret_path);
+       if (!*outfile) {
+               return NT_STATUS_NO_MEMORY;
+       }
        return NT_STATUS_OK;
 }
 
-NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
+NTSTATUS cli_ctemp(struct cli_state *cli,
+                       TALLOC_CTX *ctx,
+                       const char *path,
+                       uint16_t *pfnum,
+                       char **out_path)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev = NULL;
-       struct tevent_req *req = NULL;
+       struct event_context *ev;
+       struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
        if (cli_has_async_calls(cli)) {
@@ -2851,7 +4172,7 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
                goto fail;
        }
 
-       req = cli_dskattr_send(frame, ev, cli);
+       req = cli_ctemp_send(frame, ev, cli, path);
        if (req == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
@@ -2862,7 +4183,7 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
                goto fail;
        }
 
-       status = cli_dskattr_recv(req, bsize, total, avail);
+       status = cli_ctemp_recv(req, ctx, pfnum, out_path);
 
  fail:
        TALLOC_FREE(frame);
@@ -2872,63 +4193,6 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
        return status;
 }
 
-/****************************************************************************
- Create and open a temporary file.
-****************************************************************************/
-
-int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
-{
-       int len;
-       char *p;
-
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
-
-       cli_set_message(cli->outbuf,3,0,True);
-
-       SCVAL(cli->outbuf,smb_com,SMBctemp);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
-
-       SSVAL(cli->outbuf,smb_vwv0,0);
-       SIVALS(cli->outbuf,smb_vwv1,-1);
-
-       p = smb_buf(cli->outbuf);
-       *p++ = 4;
-       p += clistr_push(cli, p, path,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
-
-       cli_setup_bcc(cli, p);
-
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return -1;
-       }
-
-       if (cli_is_error(cli)) {
-               return -1;
-       }
-
-       /* despite the spec, the result has a -1, followed by
-          length, followed by name */
-       p = smb_buf(cli->inbuf);
-       p += 4;
-       len = smb_buflen(cli->inbuf) - 4;
-       if (len <= 0 || len > PATH_MAX) return -1;
-
-       if (tmp_path) {
-               char *path2 = SMB_MALLOC_ARRAY(char, len+1);
-               if (!path2) {
-                       return -1;
-               }
-               clistr_pull(cli->inbuf, path2, p,
-                           len+1, len, STR_ASCII);
-               *tmp_path = path2;
-       }
-
-       return SVAL(cli->inbuf,smb_vwv0);
-}
-
 /*
    send a raw ioctl - used by the torture code
 */
@@ -3299,87 +4563,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;
 }
 
 /****************************************************************************
@@ -3477,7 +4902,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;
 
@@ -3526,7 +4951,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);