s3:libsmb/cli*: use CLI_BUFFER_SIZE instead of cli->max_xmit
[metze/samba/wip.git] / source3 / libsmb / clifile.c
index e735a6e3db5510ef1759a4ce0c60f1c99f60e446..2a30d2cda9c92ab8978215feb8d541dd618b2e4a 100644 (file)
@@ -3,23 +3,30 @@
    client file operations
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Jeremy Allison 2001-2009
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "libsmb/libsmb.h"
+#include "../lib/util/tevent_ntstatus.h"
 #include "async_smb.h"
+#include "libsmb/clirap.h"
+#include "trans2.h"
+#include "ntioctl.h"
+#include "libcli/security/secdesc.h"
 
 /***********************************************************
  Common function for pushing stings, used by smb_bytes_push_str()
@@ -42,11 +49,13 @@ static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
 
        buflen = talloc_get_size(buf);
 
-       if (align_odd && ucs2 && (buflen % 2 == 0)) {
+       if (ucs2 &&
+           ((align_odd && (buflen % 2 == 0)) ||
+            (!align_odd && (buflen % 2 == 1)))) {
                /*
                 * We're pushing into an SMB buffer, align odd
                 */
-               buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
+               buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
                if (buf == NULL) {
                        return NULL;
                }
@@ -57,11 +66,11 @@ static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
        if (!convert_string_talloc(talloc_tos(), CH_UNIX,
                                   ucs2 ? CH_UTF16LE : CH_DOS,
                                   str, str_len, &converted,
-                                  &converted_size, true)) {
+                                  &converted_size)) {
                return NULL;
        }
 
-       buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
+       buf = talloc_realloc(NULL, buf, uint8_t,
                                   buflen + converted_size);
        if (buf == NULL) {
                TALLOC_FREE(converted);
@@ -102,7 +111,7 @@ uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
        }
        buflen = talloc_get_size(buf);
 
-       buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
+       buf = talloc_realloc(NULL, buf, uint8_t,
                                   buflen + 1 + num_bytes);
        if (buf == NULL) {
                return NULL;
@@ -119,14 +128,33 @@ uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
  other modules use async trans calls.
 ***********************************************************/
 
-static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
-                           const char *str, size_t str_len,
-                           size_t *pconverted_size)
+uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
+                              const char *str, size_t str_len,
+                              size_t *pconverted_size)
 {
        return internal_bytes_push_str(buf, ucs2, str, str_len,
                        false, pconverted_size);
 }
 
+uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
+                                const uint8_t *bytes, size_t num_bytes)
+{
+       size_t buflen;
+
+       if (buf == NULL) {
+               return NULL;
+       }
+       buflen = talloc_get_size(buf);
+
+       buf = talloc_realloc(NULL, buf, uint8_t,
+                            buflen + num_bytes);
+       if (buf == NULL) {
+               return NULL;
+       }
+       memcpy(&buf[buflen], bytes, num_bytes);
+       return buf;
+}
+
 struct cli_setpathinfo_state {
        uint16_t setup;
        uint8_t *param;
@@ -155,7 +183,7 @@ struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
        SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
 
        /* Setup param array. */
-       state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
+       state->param = talloc_zero_array(state, uint8_t, 6);
        if (tevent_req_nomem(state->param, req)) {
                return tevent_req_post(req, ev);
        }
@@ -205,83 +233,79 @@ NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
        return tevent_req_simple_recv_ntstatus(req);
 }
 
+NTSTATUS cli_setpathinfo(struct cli_state *cli,
+                        uint16_t level,
+                        const char *path,
+                        uint8_t *data,
+                        size_t data_len)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       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 = tevent_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+       status = cli_setpathinfo_recv(req);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
 /****************************************************************************
  Hard/Symlink a file (UNIX extensions).
  Creates new name (sym)linked to oldname.
 ****************************************************************************/
 
-struct link_state {
-       uint16_t setup;
-       uint8_t *param;
+struct cli_posix_link_internal_state {
        uint8_t *data;
 };
 
-static void cli_posix_link_internal_done(struct tevent_req *subreq)
-{
-       NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
-                                        NULL, 0, NULL, NULL, 0, NULL);
-       tevent_req_simple_finish_ntstatus(subreq, status);
-}
+static void cli_posix_link_internal_done(struct tevent_req *subreq);
 
 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
                                        struct event_context *ev,
                                        struct cli_state *cli,
+                                       uint16_t level,
                                        const char *oldname,
-                                       const char *newname,
-                                       bool hardlink)
+                                       const char *newname)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
-       struct link_state *state = NULL;
+       struct cli_posix_link_internal_state *state = NULL;
 
-       req = tevent_req_create(mem_ctx, &state, struct link_state);
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_posix_link_internal_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,hardlink ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
-
-       state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), newname,
-                                  strlen(newname)+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, 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);
-
-       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. */
+       state->data = trans2_bytes_push_str(
+               state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
 
+       subreq = cli_setpathinfo_send(
+               state, ev, cli, level, newname,
+               state->data, talloc_get_size(state->data));
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -289,6 +313,12 @@ static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
        return req;
 }
 
+static void cli_posix_link_internal_done(struct tevent_req *subreq)
+{
+       NTSTATUS status = cli_setpathinfo_recv(subreq);
+       tevent_req_simple_finish_ntstatus(subreq, status);
+}
+
 /****************************************************************************
  Symlink a file (UNIX extensions).
 ****************************************************************************/
@@ -299,18 +329,13 @@ struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
                                        const char *oldname,
                                        const char *newname)
 {
-       return cli_posix_link_internal_send(mem_ctx, ev, cli,
-                       oldname, newname, false);
+       return cli_posix_link_internal_send(
+               mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
 }
 
 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_symlink(struct cli_state *cli,
@@ -355,9 +380,6 @@ NTSTATUS cli_posix_symlink(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -415,8 +437,7 @@ static void cli_posix_readlink_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        /*
@@ -447,8 +468,7 @@ NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
                                state->data,
                                state->num_data,
                                &converted,
-                               &converted_size,
-                               true)) {
+                               &converted_size)) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -501,9 +521,6 @@ NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -517,18 +534,13 @@ struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
                                        const char *oldname,
                                        const char *newname)
 {
-       return cli_posix_link_internal_send(mem_ctx, ev, cli,
-                       oldname, newname, true);
+       return cli_posix_link_internal_send(
+               mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
 }
 
 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
@@ -573,106 +585,9 @@ NTSTATUS cli_posix_hardlink(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
-/****************************************************************************
- Map standard UNIX permissions onto wire representations.
-****************************************************************************/
-
-uint32_t unix_perms_to_wire(mode_t perms)
-{
-        unsigned int ret = 0;
-
-        ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
-        ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
-        ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
-        ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
-        ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
-        ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
-        ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
-        ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
-        ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
-#ifdef S_ISVTX
-        ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
-#endif
-#ifdef S_ISGID
-        ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
-#endif
-#ifdef S_ISUID
-        ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
-#endif
-        return ret;
-}
-
-/****************************************************************************
- Map wire permissions to standard UNIX.
-****************************************************************************/
-
-mode_t wire_perms_to_unix(uint32_t perms)
-{
-        mode_t ret = (mode_t)0;
-
-        ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
-        ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
-        ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
-        ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
-        ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
-        ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
-        ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
-        ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
-        ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
-#ifdef S_ISVTX
-        ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
-#endif
-#ifdef S_ISGID
-        ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
-#endif
-#ifdef S_ISUID
-        ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
-#endif
-        return ret;
-}
-
-/****************************************************************************
- Return the file type from the wire filetype for UNIX extensions.
-****************************************************************************/
-
-static mode_t unix_filetype_from_wire(uint32_t wire_type)
-{
-       switch (wire_type) {
-               case UNIX_TYPE_FILE:
-                       return S_IFREG;
-               case UNIX_TYPE_DIR:
-                       return S_IFDIR;
-#ifdef S_IFLNK
-               case UNIX_TYPE_SYMLINK:
-                       return S_IFLNK;
-#endif
-#ifdef S_IFCHR
-               case UNIX_TYPE_CHARDEV:
-                       return S_IFCHR;
-#endif
-#ifdef S_IFBLK
-               case UNIX_TYPE_BLKDEV:
-                       return S_IFBLK;
-#endif
-#ifdef S_IFIFO
-               case UNIX_TYPE_FIFO:
-                       return S_IFIFO;
-#endif
-#ifdef S_IFSOCK
-               case UNIX_TYPE_SOCKET:
-                       return S_IFSOCK;
-#endif
-               default:
-                       return (mode_t)0;
-       }
-}
-
 /****************************************************************************
  Do a POSIX getfacl (UNIX extensions).
 ****************************************************************************/
@@ -697,7 +612,7 @@ struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
-                                   0, cli->max_xmit);
+                                   0, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -716,8 +631,7 @@ static void cli_posix_getfacl_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -782,9 +696,6 @@ NTSTATUS cli_posix_getfacl(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -830,8 +741,7 @@ static void cli_posix_stat_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -917,9 +827,6 @@ NTSTATUS cli_posix_stat(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -927,18 +834,11 @@ NTSTATUS cli_posix_stat(struct cli_state *cli,
  Chmod or chown a file internal (UNIX extensions).
 ****************************************************************************/
 
-struct ch_state {
-       uint16_t setup;
-       uint8_t *param;
-       uint8_t *data;
+struct cli_posix_chown_chmod_internal_state {
+       uint8_t data[100];
 };
 
-static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
-{
-       NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
-                                        NULL, 0, NULL, NULL, 0, NULL);
-       tevent_req_simple_finish_ntstatus(subreq, status);
-}
+static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
 
 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
                                        struct event_context *ev,
@@ -949,67 +849,36 @@ static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ct
                                        uint32_t gid)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
-       struct ch_state *state = NULL;
+       struct cli_posix_chown_chmod_internal_state *state = NULL;
 
-       req = tevent_req_create(mem_ctx, &state, struct ch_state);
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_posix_chown_chmod_internal_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);
-       }
        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);
 
-       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. */
-
+       subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
+                                     fname, state->data, sizeof(state->data));
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done, req);
+       tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
+                               req);
        return req;
 }
 
+static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
+{
+       NTSTATUS status = cli_setpathinfo_recv(subreq);
+       tevent_req_simple_finish_ntstatus(subreq, status);
+}
+
 /****************************************************************************
  chmod a file (UNIX extensions).
 ****************************************************************************/
@@ -1029,12 +898,7 @@ struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
@@ -1077,9 +941,6 @@ NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1103,12 +964,7 @@ struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_chown(struct cli_state *cli,
@@ -1155,9 +1011,6 @@ NTSTATUS cli_posix_chown(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1187,7 +1040,7 @@ struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
+       SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
 
        bytes = talloc_array(state, uint8_t, 1);
        if (tevent_req_nomem(bytes, req)) {
@@ -1200,7 +1053,7 @@ struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+       bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -1230,8 +1083,7 @@ static void cli_rename_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1278,9 +1130,6 @@ NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fn
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1312,7 +1161,7 @@ static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
+       SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
        SSVAL(state->vwv+1, 0, rename_flag);
 
        bytes = talloc_array(state, uint8_t, 1);
@@ -1326,7 +1175,7 @@ static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+       bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -1356,8 +1205,7 @@ static void cli_ntrename_internal_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1423,9 +1271,6 @@ NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1488,9 +1333,6 @@ NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const cha
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1551,8 +1393,7 @@ static void cli_unlink_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1599,9 +1440,6 @@ NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_a
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1659,8 +1497,7 @@ static void cli_mkdir_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1707,9 +1544,6 @@ NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1767,8 +1601,7 @@ static void cli_rmdir_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -1815,9 +1648,6 @@ NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1889,12 +1719,7 @@ struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
 
 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;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
@@ -1937,9 +1762,6 @@ NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2034,8 +1856,7 @@ static void cli_ntcreate_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
                              &num_bytes, &bytes);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        state->fnum = SVAL(vwv+2, 1);
@@ -2102,9 +1923,184 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
        status = cli_ntcreate_recv(req, pfid);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
+       return status;
+}
+
+struct cli_nttrans_create_state {
+       uint16_t fnum;
+};
+
+static void cli_nttrans_create_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
+                                          struct event_context *ev,
+                                          struct cli_state *cli,
+                                          const char *fname,
+                                          uint32_t CreatFlags,
+                                          uint32_t DesiredAccess,
+                                          uint32_t FileAttributes,
+                                          uint32_t ShareAccess,
+                                          uint32_t CreateDisposition,
+                                          uint32_t CreateOptions,
+                                          uint8_t SecurityFlags,
+                                          struct security_descriptor *secdesc,
+                                          struct ea_struct *eas,
+                                          int num_eas)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_nttrans_create_state *state;
+       uint8_t *param;
+       uint8_t *secdesc_buf;
+       size_t secdesc_len;
+       NTSTATUS status;
+       size_t converted_len;
+
+       req = tevent_req_create(mem_ctx,
+                               &state, struct cli_nttrans_create_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       if (secdesc != NULL) {
+               status = marshall_sec_desc(talloc_tos(), secdesc,
+                                          &secdesc_buf, &secdesc_len);
+               if (tevent_req_nterror(req, status)) {
+                       DEBUG(10, ("marshall_sec_desc failed: %s\n",
+                                  nt_errstr(status)));
+                       return tevent_req_post(req, ev);
+               }
+       } else {
+               secdesc_buf = NULL;
+               secdesc_len = 0;
+       }
+
+       if (num_eas != 0) {
+               /*
+                * TODO ;-)
+                */
+               tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
+               return tevent_req_post(req, ev);
+       }
+
+       param = talloc_array(state, uint8_t, 53);
+       if (tevent_req_nomem(param, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       param = trans2_bytes_push_str(param, cli_ucs2(cli),
+                                     fname, strlen(fname),
+                                     &converted_len);
+       if (tevent_req_nomem(param, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       SIVAL(param, 0, CreatFlags);
+       SIVAL(param, 4, 0x0);   /* RootDirectoryFid */
+       SIVAL(param, 8, DesiredAccess);
+       SIVAL(param, 12, 0x0);  /* AllocationSize */
+       SIVAL(param, 16, 0x0);  /* AllocationSize */
+       SIVAL(param, 20, FileAttributes);
+       SIVAL(param, 24, ShareAccess);
+       SIVAL(param, 28, CreateDisposition);
+       SIVAL(param, 32, CreateOptions);
+       SIVAL(param, 36, secdesc_len);
+       SIVAL(param, 40, 0);     /* EA length*/
+       SIVAL(param, 44, converted_len);
+       SIVAL(param, 48, 0x02); /* ImpersonationLevel */
+       SCVAL(param, 52, SecurityFlags);
+
+       subreq = cli_trans_send(state, ev, cli, SMBnttrans,
+                               NULL, -1, /* name, fid */
+                               NT_TRANSACT_CREATE, 0,
+                               NULL, 0, 0, /* setup */
+                               param, talloc_get_size(param), 128, /* param */
+                               secdesc_buf, secdesc_len, 0); /* data */
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
+       return req;
+}
+
+static void cli_nttrans_create_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_nttrans_create_state *state = tevent_req_data(
+               req, struct cli_nttrans_create_state);
+       uint8_t *param;
+       uint32_t num_param;
+       NTSTATUS status;
+
+       status = cli_trans_recv(subreq, talloc_tos(), NULL,
+                               NULL, 0, NULL, /* rsetup */
+                               &param, 69, &num_param,
+                               NULL, 0, NULL);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+       state->fnum = SVAL(param, 2);
+       TALLOC_FREE(param);
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
+{
+       struct cli_nttrans_create_state *state = tevent_req_data(
+               req, struct cli_nttrans_create_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       *fnum = state->fnum;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_nttrans_create(struct cli_state *cli,
+                           const char *fname,
+                           uint32_t CreatFlags,
+                           uint32_t DesiredAccess,
+                           uint32_t FileAttributes,
+                           uint32_t ShareAccess,
+                           uint32_t CreateDisposition,
+                           uint32_t CreateOptions,
+                           uint8_t SecurityFlags,
+                           struct security_descriptor *secdesc,
+                           struct ea_struct *eas,
+                           int num_eas,
+                           uint16_t *pfid)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       if (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) {
+               goto fail;
        }
+       req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
+                                     DesiredAccess, FileAttributes,
+                                     ShareAccess, CreateDisposition,
+                                     CreateOptions, SecurityFlags,
+                                     secdesc, eas, num_eas);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+       status = cli_nttrans_create_recv(req, pfid);
+ fail:
+       TALLOC_FREE(frame);
        return status;
 }
 
@@ -2114,12 +2110,19 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
 ****************************************************************************/
 
 struct cli_open_state {
+       struct tevent_context *ev;
+       struct cli_state *cli;
+       const char *fname;
        uint16_t vwv[15];
        uint16_t fnum;
+       unsigned openfn;
+       unsigned dos_deny;
+       uint8_t additional_flags;
        struct iovec bytes;
 };
 
 static void cli_open_done(struct tevent_req *subreq);
+static void cli_open_ntcreate_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
                                   struct event_context *ev,
@@ -2129,64 +2132,61 @@ struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req, *subreq;
        struct cli_open_state *state;
-       unsigned openfn;
-       unsigned accessmode;
-       uint8_t additional_flags;
        uint8_t *bytes;
 
        req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
        if (req == NULL) {
                return NULL;
        }
+       state->ev = ev;
+       state->cli = cli;
+       state->fname = fname;
 
-       openfn = 0;
        if (flags & O_CREAT) {
-               openfn |= (1<<4);
+               state->openfn |= (1<<4);
        }
        if (!(flags & O_EXCL)) {
                if (flags & O_TRUNC)
-                       openfn |= (1<<1);
+                       state->openfn |= (1<<1);
                else
-                       openfn |= (1<<0);
+                       state->openfn |= (1<<0);
        }
 
-       accessmode = (share_mode<<4);
+       state->dos_deny = (share_mode<<4);
 
        if ((flags & O_ACCMODE) == O_RDWR) {
-               accessmode |= 2;
+               state->dos_deny |= 2;
        } else if ((flags & O_ACCMODE) == O_WRONLY) {
-               accessmode |= 1;
+               state->dos_deny |= 1;
        }
 
 #if defined(O_SYNC)
        if ((flags & O_SYNC) == O_SYNC) {
-               accessmode |= (1<<14);
+               state->dos_deny |= (1<<14);
        }
 #endif /* O_SYNC */
 
        if (share_mode == DENY_FCB) {
-               accessmode = 0xFF;
+               state->dos_deny = 0xFF;
        }
 
        SCVAL(state->vwv + 0, 0, 0xFF);
        SCVAL(state->vwv + 0, 1, 0);
        SSVAL(state->vwv + 1, 0, 0);
        SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
-       SSVAL(state->vwv + 3, 0, accessmode);
-       SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
+       SSVAL(state->vwv + 3, 0, state->dos_deny);
+       SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        SSVAL(state->vwv + 5, 0, 0);
        SIVAL(state->vwv + 6, 0, 0);
-       SSVAL(state->vwv + 8, 0, openfn);
+       SSVAL(state->vwv + 8, 0, state->openfn);
        SIVAL(state->vwv + 9, 0, 0);
        SIVAL(state->vwv + 11, 0, 0);
        SIVAL(state->vwv + 13, 0, 0);
 
-       additional_flags = 0;
-
        if (cli->use_oplocks) {
                /* if using oplocks then ask for a batch oplock via
                    core and extended methods */
-               additional_flags =
+               state->additional_flags =
                        FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
                SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
        }
@@ -2202,7 +2202,8 @@ struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
        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,
+       subreq = cli_smb_req_create(state, ev, cli, SMBopenX,
+                                   state->additional_flags,
                                    15, state->vwv, 1, &state->bytes);
        if (subreq == NULL) {
                TALLOC_FREE(req);
@@ -2227,8 +2228,7 @@ struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
        }
 
        status = cli_smb_req_send(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return tevent_req_post(req, ev);
        }
        return req;
@@ -2244,15 +2244,59 @@ static void cli_open_done(struct tevent_req *subreq)
        uint16_t *vwv;
        uint8_t *inbuf;
        NTSTATUS status;
+       uint32_t access_mask, share_mode, create_disposition, create_options;
 
        status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+
+       if (NT_STATUS_IS_OK(status)) {
+               state->fnum = SVAL(vwv+2, 0);
+               tevent_req_done(req);
+               return;
+       }
+
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
                tevent_req_nterror(req, status);
                return;
        }
-       state->fnum = SVAL(vwv+2, 0);
+
+       /*
+        * For the new shiny OS/X Lion SMB server, try a ntcreate
+        * fallback.
+        */
+
+       if (!map_open_params_to_ntcreate(state->fname, state->dos_deny,
+                                        state->openfn, &access_mask,
+                                        &share_mode, &create_disposition,
+                                        &create_options, NULL)) {
+               tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
+               return;
+       }
+
+       subreq = cli_ntcreate_send(state, state->ev, state->cli,
+                                  state->fname, 0, access_mask,
+                                  0, share_mode, create_disposition,
+                                  create_options, 0);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, cli_open_ntcreate_done, req);
+}
+
+static void cli_open_ntcreate_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_open_state *state = tevent_req_data(
+               req, struct cli_open_state);
+       NTSTATUS status;
+
+       status = cli_ntcreate_recv(subreq, &state->fnum);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
        tevent_req_done(req);
 }
 
@@ -2305,9 +2349,6 @@ NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
        status = cli_open_recv(req, pfnum);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2363,8 +2404,7 @@ struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
        }
 
        status = cli_smb_req_send(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return tevent_req_post(req, ev);
        }
        return req;
@@ -2378,8 +2418,7 @@ static void cli_close_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -2425,9 +2464,6 @@ NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
        status = cli_close_recv(req);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2500,12 +2536,7 @@ struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
@@ -2548,9 +2579,6 @@ NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2563,48 +2591,43 @@ NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
                      uint32_t offset, uint32_t len,
                      int timeout, unsigned char locktype)
 {
-       char *p;
-       int saved_timeout = cli->timeout;
-
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0', smb_size);
-
-       cli_set_message(cli->outbuf,8,0,True);
-
-       SCVAL(cli->outbuf,smb_com,SMBlockingX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
-
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-       SCVAL(cli->outbuf,smb_vwv3,locktype);
-       SIVALS(cli->outbuf, smb_vwv4, timeout);
-       SSVAL(cli->outbuf,smb_vwv6,0);
-       SSVAL(cli->outbuf,smb_vwv7,1);
-
-       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);
+       uint16_t vwv[8];
+       uint8_t bytes[10];
+       NTSTATUS status;
+       unsigned int set_timeout = 0;
+       unsigned int saved_timeout = 0;
+
+       SCVAL(vwv + 0, 0, 0xff);
+       SCVAL(vwv + 0, 1, 0);
+       SSVAL(vwv + 1, 0, 0);
+       SSVAL(vwv + 2, 0, fnum);
+       SCVAL(vwv + 3, 0, locktype);
+       SCVAL(vwv + 3, 1, 0);
+       SIVALS(vwv + 4, 0, timeout);
+       SSVAL(vwv + 6, 0, 0);
+       SSVAL(vwv + 7, 0, 1);
+
+       SSVAL(bytes, 0, cli_getpid(cli));
+       SIVAL(bytes, 2, offset);
+       SIVAL(bytes, 6, len);
 
        if (timeout != 0) {
-               cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
+               if (timeout == -1) {
+                       set_timeout = 0x7FFFFFFF;
+               } else {
+                       set_timeout = timeout + 2*1000;
+               }
+               saved_timeout = cli_set_timeout(cli, set_timeout);
        }
 
-       if (!cli_receive_smb(cli)) {
-               cli->timeout = saved_timeout;
-               return NT_STATUS_UNSUCCESSFUL;
-       }
+       status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
+                        10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
 
-       cli->timeout = saved_timeout;
+       if (saved_timeout != 0) {
+               cli_set_timeout(cli, saved_timeout);
+       }
 
-       return cli_nt_error(cli);
+       return status;
 }
 
 /****************************************************************************
@@ -2612,55 +2635,15 @@ NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
  note that timeout is in units of 2 milliseconds
 ****************************************************************************/
 
-bool cli_lock(struct cli_state *cli, uint16_t fnum,
-             uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
+NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
+                 uint32_t offset, uint32_t len, int timeout,
+                 enum brl_type lock_type)
 {
-       char *p;
-       int saved_timeout = cli->timeout;
-
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0', smb_size);
-
-       cli_set_message(cli->outbuf,8,0,True);
-
-       SCVAL(cli->outbuf,smb_com,SMBlockingX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
-
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-       SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
-       SIVALS(cli->outbuf, smb_vwv4, timeout);
-       SSVAL(cli->outbuf,smb_vwv6,0);
-       SSVAL(cli->outbuf,smb_vwv7,1);
-
-       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 (timeout != 0) {
-               cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
-       }
-
-       if (!cli_receive_smb(cli)) {
-               cli->timeout = saved_timeout;
-               return False;
-       }
-
-       cli->timeout = saved_timeout;
-
-       if (cli_is_error(cli)) {
-               return False;
-       }
+       NTSTATUS status;
 
-       return True;
+       status = cli_locktype(cli, fnum, offset, len, timeout,
+                             (lock_type == READ_LOCK? 1 : 0));
+       return status;
 }
 
 /****************************************************************************
@@ -2698,7 +2681,7 @@ struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->vwv+6, 0, 1);
        SSVAL(state->vwv+7, 0, 0);
 
-       SSVAL(state->data, 0, cli->pid);
+       SSVAL(state->data, 0, cli_getpid(cli));
        SIVAL(state->data, 2, offset);
        SIVAL(state->data, 6, len);
 
@@ -2719,8 +2702,7 @@ static void cli_unlock_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -2771,9 +2753,6 @@ NTSTATUS cli_unlock(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -2781,61 +2760,55 @@ NTSTATUS cli_unlock(struct cli_state *cli,
  Lock a file with 64 bit offsets.
 ****************************************************************************/
 
-bool cli_lock64(struct cli_state *cli, uint16_t fnum,
-               uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
+NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
+                   uint64_t offset, uint64_t len, int timeout,
+                   enum brl_type lock_type)
 {
-       char *p;
-        int saved_timeout = cli->timeout;
+       uint16_t vwv[8];
+       uint8_t bytes[20];
+       unsigned int set_timeout = 0;
+       unsigned int saved_timeout = 0;
        int ltype;
+       NTSTATUS status;
 
-       if (! (cli->capabilities & CAP_LARGE_FILES)) {
-               return cli_lock(cli, fnum, offset, len, timeout, lock_type);
+       if (! (cli_state_capabilities(cli) & CAP_LARGE_FILES)) {
+               return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
        }
 
        ltype = (lock_type == READ_LOCK? 1 : 0);
        ltype |= LOCKING_ANDX_LARGE_FILES;
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0', smb_size);
-
-       cli_set_message(cli->outbuf,8,0,True);
-
-       SCVAL(cli->outbuf,smb_com,SMBlockingX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       SCVAL(vwv + 0, 0, 0xff);
+       SCVAL(vwv + 0, 1, 0);
+       SSVAL(vwv + 1, 0, 0);
+       SSVAL(vwv + 2, 0, fnum);
+       SCVAL(vwv + 3, 0, ltype);
+       SCVAL(vwv + 3, 1, 0);
+       SIVALS(vwv + 4, 0, timeout);
+       SSVAL(vwv + 6, 0, 0);
+       SSVAL(vwv + 7, 0, 1);
 
-       SCVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,fnum);
-       SCVAL(cli->outbuf,smb_vwv3,ltype);
-       SIVALS(cli->outbuf, smb_vwv4, timeout);
-       SSVAL(cli->outbuf,smb_vwv6,0);
-       SSVAL(cli->outbuf,smb_vwv7,1);
-
-       p = smb_buf(cli->outbuf);
-       SIVAL(p, 0, cli->pid);
-       SOFF_T_R(p, 4, offset);
-       SOFF_T_R(p, 12, len);
-       p += 20;
-
-       cli_setup_bcc(cli, p);
-       cli_send_smb(cli);
+       SIVAL(bytes, 0, cli_getpid(cli));
+       SOFF_T_R(bytes, 4, offset);
+       SOFF_T_R(bytes, 12, len);
 
        if (timeout != 0) {
-               cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
-       }
-
-       if (!cli_receive_smb(cli)) {
-                cli->timeout = saved_timeout;
-               return False;
+               if (timeout == -1) {
+                       set_timeout = 0x7FFFFFFF;
+               } else {
+                       set_timeout = timeout + 2*1000;
+               }
+               saved_timeout = cli_set_timeout(cli, set_timeout);
        }
 
-       cli->timeout = saved_timeout;
+       status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
+                        20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
 
-       if (cli_is_error(cli)) {
-               return False;
+       if (saved_timeout != 0) {
+               cli_set_timeout(cli, saved_timeout);
        }
 
-       return True;
+       return status;
 }
 
 /****************************************************************************
@@ -2873,7 +2846,7 @@ struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->vwv+6, 0, 1);
        SSVAL(state->vwv+7, 0, 0);
 
-       SIVAL(state->data, 0, cli->pid);
+       SIVAL(state->data, 0, cli_getpid(cli));
        SOFF_T_R(state->data, 4, offset);
        SOFF_T_R(state->data, 12, len);
 
@@ -2894,8 +2867,7 @@ static void cli_unlock64_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -2916,7 +2888,7 @@ NTSTATUS cli_unlock64(struct cli_state *cli,
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
-       if (! (cli->capabilities & CAP_LARGE_FILES)) {
+       if (! (cli_state_capabilities(cli) & CAP_LARGE_FILES)) {
                return cli_unlock(cli, fnum, offset, len);
        }
 
@@ -2950,9 +2922,6 @@ NTSTATUS cli_unlock64(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3023,7 +2992,7 @@ static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
                                POSIX_LOCK_FLAG_NOWAIT);
        }
 
-       SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
+       SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
        SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
        SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
 
@@ -3071,12 +3040,7 @@ struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
@@ -3129,9 +3093,6 @@ NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3152,12 +3113,7 @@ struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
@@ -3201,9 +3157,6 @@ NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3263,8 +3216,7 @@ static void cli_getattrE_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
                              NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
@@ -3356,9 +3308,6 @@ NTSTATUS cli_getattrE(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3427,8 +3376,7 @@ static void cli_getatr_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
@@ -3506,9 +3454,6 @@ NTSTATUS cli_getatr(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3564,8 +3509,7 @@ static void cli_setattrE_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -3622,9 +3566,6 @@ NTSTATUS cli_setattrE(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3668,7 +3609,7 @@ struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
-       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+       bytes = talloc_realloc(state, bytes, uint8_t,
                        talloc_get_size(bytes)+1);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
@@ -3698,8 +3639,7 @@ static void cli_setatr_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -3749,9 +3689,6 @@ NTSTATUS cli_setatr(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3809,8 +3746,7 @@ static void cli_chkpath_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -3872,9 +3808,6 @@ NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -3926,8 +3859,7 @@ static void cli_dskattr_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
                              NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
@@ -3987,9 +3919,6 @@ NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4059,8 +3988,7 @@ static void cli_ctemp_done(struct tevent_req *subreq)
        status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
                              &num_bytes, &bytes);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
 
@@ -4145,9 +4073,6 @@ NTSTATUS cli_ctemp(struct cli_state *cli,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4158,14 +4083,13 @@ NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA
 {
        uint16_t vwv[3];
        NTSTATUS status;
-       struct tevent_req *result_parent;
 
        SSVAL(vwv+0, 0, fnum);
        SSVAL(vwv+1, 0, code>>16);
        SSVAL(vwv+2, 0, (code&0xFFFF));
 
        status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
-                        &result_parent, 0, NULL, NULL, NULL, NULL);
+                        NULL, 0, NULL, NULL, NULL, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -4177,30 +4101,35 @@ NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA
  Set an extended attribute utility fn.
 *********************************************************/
 
-static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
-                       const char *ea_name, const char *ea_val, size_t ea_len)
+static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
+                          uint8_t *param, unsigned int param_len,
+                          const char *ea_name,
+                          const char *ea_val, size_t ea_len)
 {
+       uint16_t setup[1];
        unsigned int data_len = 0;
-       char *data = NULL;
-       char *rparam=NULL, *rdata=NULL;
+       uint8_t *data = NULL;
        char *p;
        size_t ea_namelen = strlen(ea_name);
+       NTSTATUS status;
+
+       SSVAL(setup, 0, setup_val);
 
        if (ea_namelen == 0 && ea_len == 0) {
                data_len = 4;
-               data = (char *)SMB_MALLOC(data_len);
+               data = (uint8_t *)SMB_MALLOC(data_len);
                if (!data) {
-                       return False;
+                       return NT_STATUS_NO_MEMORY;
                }
-               p = data;
+               p = (char *)data;
                SIVAL(p,0,data_len);
        } else {
                data_len = 4 + 4 + ea_namelen + 1 + ea_len;
-               data = (char *)SMB_MALLOC(data_len);
+               data = (uint8_t *)SMB_MALLOC(data_len);
                if (!data) {
-                       return False;
+                       return NT_STATUS_NO_MEMORY;
                }
-               p = data;
+               p = (char *)data;
                SIVAL(p,0,data_len);
                p += 4;
                SCVAL(p, 0, 0); /* EA flags. */
@@ -4210,74 +4139,66 @@ static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsig
                memcpy(p+4+ea_namelen+1, ea_val, ea_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 */
-                       data,  data_len, cli->max_xmit /* data, length, max */
-                       )) {
-               SAFE_FREE(data);
-               return False;
-       }
-
-       if (!cli_receive_trans(cli, SMBtrans2,
-                       &rparam, &param_len,
-                       &rdata, &data_len)) {
-                       SAFE_FREE(data);
-               return false;
-       }
-
+       status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
+                          setup, 1, 0,
+                          param, param_len, 2,
+                          data,  data_len, CLI_BUFFER_SIZE,
+                          NULL,
+                          NULL, 0, NULL, /* rsetup */
+                          NULL, 0, NULL, /* rparam */
+                          NULL, 0, NULL); /* rdata */
        SAFE_FREE(data);
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
-
-       return True;
+       return status;
 }
 
 /*********************************************************
  Set an extended attribute on a pathname.
 *********************************************************/
 
-bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
+NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
+                        const char *ea_name, const char *ea_val,
+                        size_t ea_len)
 {
-       uint16_t setup = TRANSACT2_SETPATHINFO;
        unsigned int param_len = 0;
-       char *param;
-       size_t srclen = 2*(strlen(path)+1);
-       char *p;
-       bool ret;
+       uint8_t *param;
+       NTSTATUS status;
+       TALLOC_CTX *frame = talloc_stackframe();
 
-       param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
+       param = talloc_array(talloc_tos(), uint8_t, 6);
        if (!param) {
-               return false;
+               return NT_STATUS_NO_MEMORY;
        }
-       memset(param, '\0', 6);
        SSVAL(param,0,SMB_INFO_SET_EA);
-       p = &param[6];
+       SSVAL(param,2,0);
+       SSVAL(param,4,0);
 
-       p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
+       param = trans2_bytes_push_str(param, cli_ucs2(cli),
+                                     path, strlen(path)+1,
+                                     NULL);
+       param_len = talloc_get_size(param);
 
-       ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
-       SAFE_FREE(param);
-       return ret;
+       status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
+                           ea_name, ea_val, ea_len);
+       SAFE_FREE(frame);
+       return status;
 }
 
 /*********************************************************
  Set an extended attribute on an fnum.
 *********************************************************/
 
-bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
+NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
+                        const char *ea_name, const char *ea_val,
+                        size_t ea_len)
 {
-       char param[6];
-       uint16_t setup = TRANSACT2_SETFILEINFO;
+       uint8_t param[6];
 
        memset(param, 0, 6);
        SSVAL(param,0,fnum);
        SSVAL(param,2,SMB_INFO_SET_EA);
 
-       return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
+       return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
+                         ea_name, ea_val, ea_len);
 }
 
 /*********************************************************
@@ -4338,7 +4259,7 @@ static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
                return true;
        }
 
-       ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
+       ea_list = talloc_array(ctx, struct ea_struct, num_eas);
        if (!ea_list) {
                return false;
        }
@@ -4354,7 +4275,7 @@ static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
 
                ea->flags = CVAL(p,0);
                unix_ea_name[0] = '\0';
-               pull_ascii_fstring(unix_ea_name, p + 4);
+               pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
                ea->name = talloc_strdup(ea_list, unix_ea_name);
                if (!ea->name) {
                        goto fail;
@@ -4406,7 +4327,7 @@ struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
        }
        subreq = cli_qpathinfo_send(state, ev, cli, fname,
                                    SMB_INFO_QUERY_ALL_EAS, 4,
-                                   cli->max_xmit);
+                                   CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -4425,8 +4346,7 @@ static void cli_get_ea_list_path_done(struct tevent_req *subreq)
        status = cli_qpathinfo_recv(subreq, state, &state->data,
                                    &state->num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -4480,9 +4400,6 @@ NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
        status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4532,7 +4449,6 @@ static uint32_t open_flags_to_wire(int flags)
 #endif
 #if defined(O_DIRECTORY)
        if (flags & O_DIRECTORY) {
-               ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
                ret |= SMB_O_DIRECTORY;
        }
 #endif
@@ -4562,8 +4478,7 @@ static void cli_posix_open_internal_done(struct tevent_req *subreq)
        status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
                                NULL, 0, NULL, &data, 12, &num_data);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        state->fnum = SVAL(data,2);
@@ -4607,7 +4522,6 @@ static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
 
        /* Setup data words. */
        if (is_dir) {
-               wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
                wire_flags |= SMB_O_DIRECTORY;
        }
 
@@ -4712,9 +4626,6 @@ NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4730,12 +4641,7 @@ struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
@@ -4778,9 +4684,6 @@ NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4788,73 +4691,34 @@ NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
  unlink or rmdir - POSIX semantics.
 ****************************************************************************/
 
-struct unlink_state {
-       uint16_t setup;
+struct cli_posix_unlink_internal_state {
        uint8_t data[2];
 };
 
-static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
-{
-       NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
-                                        NULL, 0, NULL, NULL, 0, NULL);
-       tevent_req_simple_finish_ntstatus(subreq, status);
-}
+static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
 
 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
                                        struct event_context *ev,
                                        struct cli_state *cli,
                                        const char *fname,
-                                       bool is_dir)
+                                       uint16_t level)
 {
        struct tevent_req *req = NULL, *subreq = NULL;
-       struct unlink_state *state = NULL;
-       uint8_t *param = NULL;
+       struct cli_posix_unlink_internal_state *state = NULL;
 
-       req = tevent_req_create(mem_ctx, &state, struct unlink_state);
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_posix_unlink_internal_state);
        if (req == NULL) {
                return NULL;
        }
 
-       /* Setup setup word. */
-       SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
-
-       /* Setup param array. */
-       param = talloc_array(state, uint8_t, 6);
-       if (tevent_req_nomem(param, req)) {
-               return tevent_req_post(req, ev);
-       }
-       memset(param, '\0', 6);
-       SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
-
-       param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
-                                  strlen(fname)+1, NULL);
-
-       if (tevent_req_nomem(param, req)) {
-               return tevent_req_post(req, ev);
-       }
-
        /* Setup data word. */
-       SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
-                       SMB_POSIX_UNLINK_FILE_TARGET);
-
-       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. */
-                               param,                  /* param. */
-                               talloc_get_size(param), /* num param. */
-                               2,                      /* max returned param. */
-                               state->data,            /* data. */
-                               2,                      /* num data. */
-                               0);                     /* max returned data. */
+       SSVAL(state->data, 0, level);
 
+       subreq = cli_setpathinfo_send(state, ev, cli,
+                                     SMB_POSIX_PATH_UNLINK,
+                                     fname,
+                                     state->data, sizeof(state->data));
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -4862,22 +4726,24 @@ static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
        return req;
 }
 
+static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
+{
+       NTSTATUS status = cli_setpathinfo_recv(subreq);
+       tevent_req_simple_finish_ntstatus(subreq, status);
+}
+
 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
                                        struct event_context *ev,
                                        struct cli_state *cli,
                                        const char *fname)
 {
-       return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
+       return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
+                                             SMB_POSIX_UNLINK_FILE_TARGET);
 }
 
 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 /****************************************************************************
@@ -4923,9 +4789,6 @@ NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -4938,17 +4801,14 @@ struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
                                        struct cli_state *cli,
                                        const char *fname)
 {
-       return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
+       return cli_posix_unlink_internal_send(
+               mem_ctx, ev, cli, fname,
+               SMB_POSIX_UNLINK_DIRECTORY_TARGET);
 }
 
 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
 {
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
@@ -4990,9 +4850,6 @@ NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
 
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -5066,9 +4923,8 @@ static void cli_notify_done(struct tevent_req *subreq)
        status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
                                &params, 0, &num_params, NULL, 0, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (tevent_req_nterror(req, status)) {
                DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
-               tevent_req_nterror(req, status);
                return;
        }
 
@@ -5217,8 +5073,7 @@ static void cli_qpathinfo_done(struct tevent_req *subreq)
                                NULL, 0, NULL,
                                &state->rdata, state->min_rdata,
                                &state->num_rdata);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -5277,9 +5132,6 @@ NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -5287,6 +5139,7 @@ struct cli_qfileinfo_state {
        uint16_t setup[1];
        uint8_t param[4];
        uint8_t *data;
+       uint16_t recv_flags2;
        uint32_t min_rdata;
        uint8_t *rdata;
        uint32_t num_rdata;
@@ -5346,18 +5199,20 @@ static void cli_qfileinfo_done(struct tevent_req *subreq)
                req, struct cli_qfileinfo_state);
        NTSTATUS status;
 
-       status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
+       status = cli_trans_recv(subreq, state,
+                               &state->recv_flags2,
+                               NULL, 0, NULL,
                                NULL, 0, NULL,
                                &state->rdata, state->min_rdata,
                                &state->num_rdata);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
 }
 
 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+                           uint16_t *recv_flags2,
                            uint8_t **rdata, uint32_t *num_rdata)
 {
        struct cli_qfileinfo_state *state = tevent_req_data(
@@ -5367,6 +5222,10 @@ NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
+
+       if (recv_flags2 != NULL) {
+               *recv_flags2 = state->recv_flags2;
+       }
        if (rdata != NULL) {
                *rdata = talloc_move(mem_ctx, &state->rdata);
        } else {
@@ -5380,7 +5239,7 @@ NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
                       uint16_t fnum, uint16_t level, uint32_t min_rdata,
-                      uint32_t max_rdata,
+                      uint32_t max_rdata, uint16_t *recv_flags2,
                       uint8_t **rdata, uint32_t *num_rdata)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -5407,12 +5266,9 @@ NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
        if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
-       status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
+       status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -5453,8 +5309,7 @@ static void cli_flush_done(struct tevent_req *subreq)
 
        status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
+       if (tevent_req_nterror(req, status)) {
                return;
        }
        tevent_req_done(req);
@@ -5493,8 +5348,150 @@ NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
        status = cli_flush_recv(req);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
+       return status;
+}
+
+struct cli_shadow_copy_data_state {
+       uint16_t setup[4];
+       uint8_t *data;
+       uint32_t num_data;
+       bool get_names;
+};
+
+static void cli_shadow_copy_data_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
+                                            struct tevent_context *ev,
+                                            struct cli_state *cli,
+                                            uint16_t fnum,
+                                            bool get_names)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_shadow_copy_data_state *state;
+       uint32_t ret_size;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_shadow_copy_data_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->get_names = get_names;
+       ret_size = get_names ? CLI_BUFFER_SIZE : 16;
+
+       SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
+       SSVAL(state->setup + 2, 0, fnum);
+       SCVAL(state->setup + 3, 0, 0); /* isFsctl */
+       SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
+
+       subreq = cli_trans_send(
+               state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
+               state->setup, ARRAY_SIZE(state->setup), 0,
+               NULL, 0, 0,
+               NULL, 0, ret_size);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
+       return req;
+}
+
+static void cli_shadow_copy_data_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_shadow_copy_data_state *state = tevent_req_data(
+               req, struct cli_shadow_copy_data_state);
+       NTSTATUS status;
+
+       status = cli_trans_recv(subreq, state, NULL,
+                               NULL, 0, NULL, /* setup */
+                               NULL, 0, NULL, /* param */
+                               &state->data, 12, &state->num_data);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+                                  char ***pnames, int *pnum_names)
+{
+       struct cli_shadow_copy_data_state *state = tevent_req_data(
+               req, struct cli_shadow_copy_data_state);
+       char **names;
+       int i, num_names;
+       uint32_t dlength;
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       num_names = IVAL(state->data, 4);
+       dlength = IVAL(state->data, 8);
+
+       if (!state->get_names) {
+               *pnum_names = num_names;
+               return NT_STATUS_OK;
+       }
+
+       if (dlength+12 > state->num_data) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+       names = talloc_array(mem_ctx, char *, num_names);
+       if (names == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
+
+       for (i=0; i<num_names; i++) {
+               bool ret;
+               uint8_t *src;
+               size_t converted_size;
+
+               src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
+               ret = convert_string_talloc(
+                       names, CH_UTF16LE, CH_UNIX,
+                       src, 2 * sizeof(SHADOW_COPY_LABEL),
+                       &names[i], &converted_size);
+               if (!ret) {
+                       TALLOC_FREE(names);
+                       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+               }
+       }
+       *pnum_names = num_names;
+       *pnames = names;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
+                             uint16_t fnum, bool get_names,
+                             char ***pnames, int *pnum_names)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       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) {
+               goto fail;
+       }
+       req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+       status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
+ fail:
+       TALLOC_FREE(frame);
        return status;
 }