s3:torture:delete: untangle function call from result check
[kai/samba.git] / source3 / torture / torture.c
index 9fcf582724b59ffbaea21b39c9f1f1697a43819c..6ba228460f72e0344399f8e33715895a04dcc5dd 100644 (file)
 #include "../librpc/gen_ndr/svcctl.h"
 #include "memcache.h"
 #include "nsswitch/winbind_client.h"
-#include "dbwrap.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
+#include "dbwrap/dbwrap_rbt.h"
 #include "talloc_dict.h"
 #include "async_smb.h"
+#include "libsmb/libsmb.h"
 #include "libsmb/clirap.h"
 #include "trans2.h"
 #include "libsmb/nmblib.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "util_tdb.h"
+#include "../libcli/smb/read_smb.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 extern char *optarg;
 extern int optind;
 
-static fstring host, workgroup, share, password, username, myname;
+fstring host, workgroup, share, password, username, myname;
 static int max_protocol = PROTOCOL_NT1;
 static const char *sockops="TCP_NODELAY";
-static int nprocs=1;
+int torture_nprocs=1;
 static int port_to_use=0;
 int torture_numops=100;
 int torture_blocksize=1024*1024;
@@ -51,72 +58,20 @@ static fstring randomfname;
 static bool use_oplocks;
 static bool use_level_II_oplocks;
 static const char *client_txt = "client_oplocks.txt";
+static bool disable_spnego;
 static bool use_kerberos;
+static bool force_dos_errors;
 static fstring multishare_conn_fname;
 static bool use_multishare_conn = False;
 static bool do_encrypt;
 static const char *local_path = NULL;
-static int signing_state = Undefined;
+static int signing_state = SMB_SIGNING_DEFAULT;
+char *test_filename;
 
 bool torture_showall = False;
 
 static double create_procs(bool (*fn)(int), bool *result);
 
-
-/* return a pointer to a anonymous shared memory segment of size "size"
-   which will persist across fork() but will disappear when all processes
-   exit 
-
-   The memory is not zeroed 
-
-   This function uses system5 shared memory. It takes advantage of a property
-   that the memory is not destroyed if it is attached when the id is removed
-   */
-void *shm_setup(int size)
-{
-       int shmid;
-       void *ret;
-
-#ifdef __QNXNTO__
-       shmid = shm_open("private", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
-       if (shmid == -1) {
-               printf("can't get shared memory\n");
-               exit(1);
-       }
-       shm_unlink("private");
-       if (ftruncate(shmid, size) == -1) {
-               printf("can't set shared memory size\n");
-               exit(1);
-       }
-       ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
-       if (ret == MAP_FAILED) {
-               printf("can't map shared memory\n");
-               exit(1);
-       }
-#else
-       shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
-       if (shmid == -1) {
-               printf("can't get shared memory\n");
-               exit(1);
-       }
-       ret = (void *)shmat(shmid, 0, 0);
-       if (!ret || ret == (void *)-1) {
-               printf("can't attach to shared memory\n");
-               return NULL;
-       }
-       /* the following releases the ipc, but note that this process
-          and all its children will still have access to the memory, its
-          just that the shmid is no longer valid for other shm calls. This
-          means we don't leave behind lots of shm segments after we exit 
-
-          See Stevens "advanced programming in unix env" for details
-          */
-       shmctl(shmid, IPC_RMID, 0);
-#endif
-
-       return ret;
-}
-
 /********************************************************************
  Ensure a connection is encrypted.
 ********************************************************************/
@@ -173,55 +128,38 @@ static bool force_cli_encryption(struct cli_state *c,
 
 static struct cli_state *open_nbt_connection(void)
 {
-       struct nmb_name called, calling;
-       struct sockaddr_storage ss;
        struct cli_state *c;
        NTSTATUS status;
+       int flags = 0;
 
-       make_nmb_name(&calling, myname, 0x0);
-       make_nmb_name(&called , host, 0x20);
+       if (disable_spnego) {
+               flags |= CLI_FULL_CONNECTION_DONT_SPNEGO;
+       }
 
-        zero_sockaddr(&ss);
+       if (use_oplocks) {
+               flags |= CLI_FULL_CONNECTION_OPLOCKS;
+       }
 
-       if (!(c = cli_initialise_ex(signing_state))) {
-               printf("Failed initialize cli_struct to connect with %s\n", host);
-               return NULL;
+       if (use_level_II_oplocks) {
+               flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
        }
 
-       c->port = port_to_use;
+       if (use_kerberos) {
+               flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
+       }
+
+       if (force_dos_errors) {
+               flags |= CLI_FULL_CONNECTION_FORCE_DOS_ERRORS;
+       }
 
-       status = cli_connect(c, host, &ss);
+       status = cli_connect_nb(host, NULL, port_to_use, 0x20, myname,
+                               signing_state, flags, &c);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
                return NULL;
        }
 
-       c->use_kerberos = use_kerberos;
-
-       c->timeout = 120000; /* set a really long timeout (2 minutes) */
-       if (use_oplocks) c->use_oplocks = True;
-       if (use_level_II_oplocks) c->use_level_II_oplocks = True;
-
-       if (!cli_session_request(c, &calling, &called)) {
-               /*
-                * Well, that failed, try *SMBSERVER ...
-                * However, we must reconnect as well ...
-                */
-               status = cli_connect(c, host, &ss);
-               if (!NT_STATUS_IS_OK(status)) {
-                       printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
-                       return NULL;
-               }
-
-               make_nmb_name(&called, "*SMBSERVER", 0x20);
-               if (!cli_session_request(c, &calling, &called)) {
-                       printf("%s rejected the session\n",host);
-                       printf("We tried with a called name of %s & %s\n",
-                               host, "*SMBSERVER");
-                       cli_shutdown(c);
-                       return NULL;
-               }
-       }
+       cli_set_timeout(c, 120000); /* set a really long timeout (2 minutes) */
 
        return c;
 }
@@ -230,115 +168,109 @@ static struct cli_state *open_nbt_connection(void)
  Send a corrupt session request. See rfc1002.txt 4.3 and 4.3.2.
 ****************************************************************************/
 
-static bool cli_bad_session_request(struct cli_state *cli,
+static bool cli_bad_session_request(int fd,
                          struct nmb_name *calling, struct nmb_name *called)
 {
-        char *p;
-        int len = 4;
-        int namelen = 0;
-        char *tmp;
-
-        memcpy(&(cli->calling), calling, sizeof(*calling));
-        memcpy(&(cli->called ), called , sizeof(*called ));
+       TALLOC_CTX *frame;
+       uint8_t len_buf[4];
+       struct iovec iov[3];
+       ssize_t len;
+       uint8_t *inbuf;
+       int err;
+       bool ret = false;
+       uint8_t message_type;
+       uint8_t error;
+       struct event_context *ev;
+       struct tevent_req *req;
 
-        /* put in the destination name */
+       frame = talloc_stackframe();
 
-        tmp = name_mangle(talloc_tos(), cli->called.name,
-                          cli->called.name_type);
-        if (tmp == NULL) {
-                return false;
-        }
+       iov[0].iov_base = len_buf;
+       iov[0].iov_len  = sizeof(len_buf);
 
-        p = cli->outbuf+len;
-        namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
-        if (namelen > 0) {
-                memcpy(p, tmp, namelen);
-                len += namelen;
-        }
-        TALLOC_FREE(tmp);
+       /* put in the destination name */
 
-       /* Deliberately corrupt the name len (first byte) */
-       *p = 100;
+       iov[1].iov_base = name_mangle(talloc_tos(), called->name,
+                                     called->name_type);
+       if (iov[1].iov_base == NULL) {
+               goto fail;
+       }
+       iov[1].iov_len = name_len((unsigned char *)iov[1].iov_base,
+                                 talloc_get_size(iov[1].iov_base));
 
-        /* and my name */
+       /* and my name */
 
-        tmp = name_mangle(talloc_tos(), cli->calling.name,
-                          cli->calling.name_type);
-        if (tmp == NULL) {
-                return false;
-        }
+       iov[2].iov_base = name_mangle(talloc_tos(), calling->name,
+                                     calling->name_type);
+       if (iov[2].iov_base == NULL) {
+               goto fail;
+       }
+       iov[2].iov_len = name_len((unsigned char *)iov[2].iov_base,
+                                 talloc_get_size(iov[2].iov_base));
 
-        p = cli->outbuf+len;
-        namelen = name_len((unsigned char *)tmp, talloc_get_size(tmp));
-        if (namelen > 0) {
-                memcpy(p, tmp, namelen);
-                len += namelen;
-        }
-        TALLOC_FREE(tmp);
        /* Deliberately corrupt the name len (first byte) */
-       *p = 100;
+       *((uint8_t *)iov[2].iov_base) = 100;
 
-        /* send a session request (RFC 1002) */
-        /* setup the packet length
+       /* send a session request (RFC 1002) */
+       /* setup the packet length
          * Remove four bytes from the length count, since the length
          * field in the NBT Session Service header counts the number
          * of bytes which follow.  The cli_send_smb() function knows
          * about this and accounts for those four bytes.
          * CRH.
          */
-        len -= 4;
-        _smb_setlen(cli->outbuf,len);
-        SCVAL(cli->outbuf,0,0x81);
-
-        cli_send_smb(cli);
-        DEBUG(5,("Sent session request\n"));
-
-        if (!cli_receive_smb(cli))
-                return False;
-
-        if (CVAL(cli->inbuf,0) != 0x82) {
-                /* This is the wrong place to put the error... JRA. */
-                cli->rap_error = CVAL(cli->inbuf,4);
-                return False;
-        }
-        return(True);
-}
-
-static struct cli_state *open_bad_nbt_connection(void)
-{
-       struct nmb_name called, calling;
-       struct sockaddr_storage ss;
-       struct cli_state *c;
-       NTSTATUS status;
-
-       make_nmb_name(&calling, myname, 0x0);
-       make_nmb_name(&called , host, 0x20);
 
-        zero_sockaddr(&ss);
+       _smb_setlen(len_buf, iov[1].iov_len + iov[2].iov_len);
+       SCVAL(len_buf,0,0x81);
 
-       if (!(c = cli_initialise_ex(signing_state))) {
-               printf("Failed initialize cli_struct to connect with %s\n", host);
-               return NULL;
+       len = write_data_iov(fd, iov, 3);
+       if (len == -1) {
+               goto fail;
        }
 
-       c->port = 139;
-
-       status = cli_connect(c, host, &ss);
-       if (!NT_STATUS_IS_OK(status)) {
-               printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
-               return NULL;
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
        }
+       req = read_smb_send(frame, ev, fd);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll(req, ev)) {
+               goto fail;
+       }
+       len = read_smb_recv(req, talloc_tos(), &inbuf, &err);
+       if (len == -1) {
+               errno = err;
+               goto fail;
+       }
+       TALLOC_FREE(ev);
 
-       c->timeout = 4000; /* set a short timeout (4 seconds) */
+       message_type = CVAL(inbuf, 0);
+       if (message_type != 0x83) {
+               d_fprintf(stderr, "Expected msg type 0x83, got 0x%2.2x\n",
+                         message_type);
+               goto fail;
+        }
 
-       if (!cli_bad_session_request(c, &calling, &called)) {
-               printf("Failed to connect with %s. Error %s\n", host, nt_errstr(status) );
-               return NULL;
-       }
+       if (smb_len(inbuf) != 1) {
+               d_fprintf(stderr, "Expected smb_len 1, got %d\n",
+                         (int)smb_len(inbuf));
+               goto fail;
+        }
 
-       return c;
-}
+       error = CVAL(inbuf, 4);
+       if (error !=  0x82) {
+               d_fprintf(stderr, "Expected error 0x82, got %d\n",
+                         (int)error);
+               goto fail;
+        }
 
+       ret = true;
+fail:
+       TALLOC_FREE(frame);
+        return ret;
+}
 
 /* Insert a NULL at the first separator of the given path and return a pointer
  * to the remainder of the string.
@@ -423,7 +355,7 @@ static bool torture_open_connection_share(struct cli_state **c,
                return False;
        }
 
-       (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
+       cli_set_timeout(*c, 120000); /* set a really long timeout (2 minutes) */
 
        if (do_encrypt) {
                return force_cli_encryption(*c,
@@ -464,22 +396,35 @@ bool torture_open_connection(struct cli_state **c, int conn_index)
        return torture_open_connection_share(c, host, share);
 }
 
+bool torture_init_connection(struct cli_state **pcli)
+{
+       struct cli_state *cli;
+
+       cli = open_nbt_connection();
+       if (cli == NULL) {
+               return false;
+       }
+
+       *pcli = cli;
+       return true;
+}
+
 bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
 {
-       uint16 old_vuid = cli->vuid;
+       uint16_t old_vuid = cli_state_get_uid(cli);
        fstring old_user_name;
        size_t passlen = strlen(password);
        NTSTATUS status;
        bool ret;
 
        fstrcpy(old_user_name, cli->user_name);
-       cli->vuid = 0;
+       cli_state_set_uid(cli, 0);
        ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
                                                password, passlen,
                                                password, passlen,
                                                workgroup));
-       *new_vuid = cli->vuid;
-       cli->vuid = old_vuid;
+       *new_vuid = cli_state_get_uid(cli);
+       cli_state_set_uid(cli, old_vuid);
        status = cli_set_username(cli, old_user_name);
        if (!NT_STATUS_IS_OK(status)) {
                return false;
@@ -505,36 +450,70 @@ bool torture_close_connection(struct cli_state *c)
 }
 
 
+/* check if the server produced the expected dos or nt error code */
+static bool check_both_error(int line, NTSTATUS status,
+                            uint8 eclass, uint32 ecode, NTSTATUS nterr)
+{
+       if (NT_STATUS_IS_DOS(status)) {
+               uint8 cclass;
+               uint32 num;
+
+               /* Check DOS error */
+               cclass = NT_STATUS_DOS_CLASS(status);
+               num = NT_STATUS_DOS_CODE(status);
+
+               if (eclass != cclass || ecode != num) {
+                       printf("unexpected error code class=%d code=%d\n",
+                              (int)cclass, (int)num);
+                       printf(" expected %d/%d %s (line=%d)\n",
+                              (int)eclass, (int)ecode, nt_errstr(nterr), line);
+                       return false;
+               }
+       } else {
+               /* Check NT error */
+               if (!NT_STATUS_EQUAL(nterr, status)) {
+                       printf("unexpected error code %s\n",
+                               nt_errstr(status));
+                       printf(" expected %s (line=%d)\n",
+                               nt_errstr(nterr), line);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+
 /* check if the server produced the expected error code */
-static bool check_error(int line, struct cli_state *c, 
+static bool check_error(int line, NTSTATUS status,
                        uint8 eclass, uint32 ecode, NTSTATUS nterr)
 {
-        if (cli_is_dos_error(c)) {
+       if (NT_STATUS_IS_DOS(status)) {
                 uint8 cclass;
                 uint32 num;
 
                 /* Check DOS error */
 
-                cli_dos_error(c, &cclass, &num);
+               cclass = NT_STATUS_DOS_CLASS(status);
+               num = NT_STATUS_DOS_CODE(status);
 
                 if (eclass != cclass || ecode != num) {
                         printf("unexpected error code class=%d code=%d\n", 
                                (int)cclass, (int)num);
                         printf(" expected %d/%d %s (line=%d)\n", 
-                               (int)eclass, (int)ecode, nt_errstr(nterr), line);
+                               (int)eclass, (int)ecode, nt_errstr(nterr),
+                              line);
                         return False;
                 }
 
         } else {
-                NTSTATUS status;
-
                 /* Check NT error */
 
-                status = cli_nt_error(c);
-
                 if (NT_STATUS_V(nterr) != NT_STATUS_V(status)) {
-                        printf("unexpected error code %s\n", nt_errstr(status));
-                        printf(" expected %s (line=%d)\n", nt_errstr(nterr), line);
+                        printf("unexpected error code %s\n",
+                              nt_errstr(status));
+                        printf(" expected %s (line=%d)\n", nt_errstr(nterr),
+                              line);
                         return False;
                 }
         }
@@ -545,10 +524,20 @@ static bool check_error(int line, struct cli_state *c,
 
 static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
 {
-       while (!cli_lock(c, fnum, offset, len, -1, WRITE_LOCK)) {
-               if (!check_error(__LINE__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
+       NTSTATUS status;
+
+       status = cli_lock32(c, fnum, offset, len, -1, WRITE_LOCK);
+
+       while (!NT_STATUS_IS_OK(status)) {
+               if (!check_both_error(__LINE__, status, ERRDOS,
+                                     ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) {
+                       return false;
+               }
+
+               status = cli_lock32(c, fnum, offset, len, -1, WRITE_LOCK);
        }
-       return True;
+
+       return true;
 }
 
 
@@ -562,17 +551,19 @@ static bool rw_torture(struct cli_state *c)
        int i, j;
        char buf[1024];
        bool correct = True;
+       size_t nread = 0;
        NTSTATUS status;
 
        memset(buf, '\0', sizeof(buf));
 
-       status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL, 
+       status = cli_openx(c, lockfname, O_RDWR | O_CREAT | O_EXCL, 
                         DENY_NONE, &fnum2);
        if (!NT_STATUS_IS_OK(status)) {
-               status = cli_open(c, lockfname, O_RDWR, DENY_NONE, &fnum2);
+               status = cli_openx(c, lockfname, O_RDWR, DENY_NONE, &fnum2);
        }
        if (!NT_STATUS_IS_OK(status)) {
-               printf("open of %s failed (%s)\n", lockfname, cli_errstr(c));
+               printf("open of %s failed (%s)\n",
+                      lockfname, nt_errstr(status));
                return False;
        }
 
@@ -588,8 +579,10 @@ static bool rw_torture(struct cli_state *c)
                        return False;
                }
 
-               if (!NT_STATUS_IS_OK(cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL, &fnum))) {
-                       printf("open failed (%s)\n", cli_errstr(c));
+               status = cli_openx(c, fname, O_RDWR | O_CREAT | O_TRUNC,
+                                  DENY_ALL, &fnum);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("open failed (%s)\n", nt_errstr(status));
                        correct = False;
                        break;
                }
@@ -614,9 +607,16 @@ static bool rw_torture(struct cli_state *c)
 
                pid2 = 0;
 
-               if (cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
-                       printf("read failed (%s)\n", cli_errstr(c));
-                       correct = False;
+               status = cli_read(c, fnum, (char *)&pid2, 0, sizeof(pid),
+                                 &nread);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("read failed (%s)\n", nt_errstr(status));
+                       correct = false;
+               } else if (nread != sizeof(pid)) {
+                       printf("read/write compare failed: "
+                              "recv %ld req %ld\n", (unsigned long)nread,
+                              (unsigned long)sizeof(pid));
+                       correct = false;
                }
 
                if (pid2 != pid) {
@@ -624,24 +624,27 @@ static bool rw_torture(struct cli_state *c)
                        correct = False;
                }
 
-               if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
-                       printf("close failed (%s)\n", cli_errstr(c));
+               status = cli_close(c, fnum);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("close failed (%s)\n", nt_errstr(status));
                        correct = False;
                }
 
-               if (!NT_STATUS_IS_OK(cli_unlink(c, fname, aSYSTEM | aHIDDEN))) {
-                       printf("unlink failed (%s)\n", cli_errstr(c));
+               status = cli_unlink(c, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("unlink failed (%s)\n", nt_errstr(status));
                        correct = False;
                }
 
-               if (!NT_STATUS_IS_OK(cli_unlock(c, fnum2, n*sizeof(int), sizeof(int)))) {
-                       printf("unlock failed (%s)\n", cli_errstr(c));
+               status = cli_unlock(c, fnum2, n*sizeof(int), sizeof(int));
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("unlock failed (%s)\n", nt_errstr(status));
                        correct = False;
                }
        }
 
        cli_close(c, fnum2);
-       cli_unlink(c, lockfname, aSYSTEM | aHIDDEN);
+       cli_unlink(c, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("%d\n", i);
 
@@ -655,7 +658,7 @@ static bool run_torture(int dummy)
 
        cli = current_cli;
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        ret = rw_torture(cli);
 
@@ -674,7 +677,7 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
        char buf_rd[131072];
        unsigned count;
        unsigned countprev = 0;
-       ssize_t sent = 0;
+       size_t sent = 0;
        bool correct = True;
        NTSTATUS status = NT_STATUS_OK;
 
@@ -686,14 +689,19 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
 
        if (procnum == 0)
        {
-               if (!NT_STATUS_IS_OK(cli_unlink(c, lockfname, aSYSTEM | aHIDDEN))) {
-                       printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c));
+               status = cli_unlink(
+                       c, lockfname,
+                       FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("unlink failed (%s) (normal, this file should "
+                              "not exist)\n", nt_errstr(status));
                }
 
-               if (!NT_STATUS_IS_OK(cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL, 
-                                DENY_NONE, &fnum))) {
+               status = cli_openx(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
+                                 DENY_NONE, &fnum);
+               if (!NT_STATUS_IS_OK(status)) {
                        printf("first open read/write of %s failed (%s)\n",
-                                       lockfname, cli_errstr(c));
+                                       lockfname, nt_errstr(status));
                        return False;
                }
        }
@@ -701,7 +709,7 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
        {
                for (i = 0; i < 500 && fnum == (uint16_t)-1; i++)
                {
-                       status = cli_open(c, lockfname, O_RDONLY, 
+                       status = cli_openx(c, lockfname, O_RDONLY, 
                                         DENY_NONE, &fnum);
                        if (!NT_STATUS_IS_OK(status)) {
                                break;
@@ -710,7 +718,7 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
                }
                if (!NT_STATUS_IS_OK(status)) {
                        printf("second open read-only of %s failed (%s)\n",
-                                       lockfname, cli_errstr(c));
+                                       lockfname, nt_errstr(status));
                        return False;
                }
        }
@@ -734,7 +742,7 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
                        }
 
                        status = cli_writeall(c, fnum, 0, (uint8_t *)buf+count,
-                                             count, (size_t)sent, NULL);
+                                             count, sent, NULL);
                        if (!NT_STATUS_IS_OK(status)) {
                                printf("write failed (%s)\n",
                                       nt_errstr(status));
@@ -743,18 +751,15 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
                }
                else
                {
-                       sent = cli_read(c, fnum, buf_rd+count, count,
-                                                 sizeof(buf)-count);
-                       if (sent < 0)
-                       {
+                       status = cli_read(c, fnum, buf_rd+count, count,
+                                         sizeof(buf)-count, &sent);
+                       if(!NT_STATUS_IS_OK(status)) {
                                printf("read failed offset:%d size:%ld (%s)\n",
                                       count, (unsigned long)sizeof(buf)-count,
-                                      cli_errstr(c));
+                                      nt_errstr(status));
                                correct = False;
                                sent = 0;
-                       }
-                       if (sent > 0)
-                       {
+                       } else if (sent > 0) {
                                if (memcmp(buf_rd+count, buf+count, sent) != 0)
                                {
                                        printf("read/write compare failed\n");
@@ -767,8 +772,9 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
 
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(c, fnum))) {
-               printf("close failed (%s)\n", cli_errstr(c));
+       status = cli_close(c, fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -784,29 +790,32 @@ static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
        char buf[131072];
        char buf_rd[131072];
        bool correct = True;
-       ssize_t bytes_read;
+       size_t bytes_read;
+       NTSTATUS status;
 
-       if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s) (normal, this file should not exist)\n", cli_errstr(c1));
+       status = cli_unlink(c1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s) (normal, this file should not exist)\n", nt_errstr(status));
        }
 
-       if (!NT_STATUS_IS_OK(cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL, 
-                        DENY_NONE, &fnum1))) {
+       status = cli_openx(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
+                         DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("first open read/write of %s failed (%s)\n",
-                               lockfname, cli_errstr(c1));
+                               lockfname, nt_errstr(status));
                return False;
        }
-       if (!NT_STATUS_IS_OK(cli_open(c2, lockfname, O_RDONLY, 
-                        DENY_NONE, &fnum2))) {
+
+       status = cli_openx(c2, lockfname, O_RDONLY, DENY_NONE, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("second open read-only of %s failed (%s)\n",
-                               lockfname, cli_errstr(c2));
+                               lockfname, nt_errstr(status));
                cli_close(c1, fnum1);
                return False;
        }
 
-       for (i=0;i<torture_numops;i++)
+       for (i = 0; i < torture_numops; i++)
        {
-               NTSTATUS status;
                size_t buf_size = ((unsigned)sys_random()%(sizeof(buf)-1))+ 1;
                if (i % 10 == 0) {
                        printf("%d\r", i); fflush(stdout);
@@ -822,9 +831,15 @@ static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
                        break;
                }
 
-               if ((bytes_read = cli_read(c2, fnum2, buf_rd, 0, buf_size)) != buf_size) {
-                       printf("read failed (%s)\n", cli_errstr(c2));
-                       printf("read %d, expected %ld\n", (int)bytes_read, 
+               status = cli_read(c2, fnum2, buf_rd, 0, buf_size, &bytes_read);
+               if(!NT_STATUS_IS_OK(status)) {
+                       printf("read failed (%s)\n", nt_errstr(status));
+                       correct = false;
+                       break;
+               } else if (bytes_read != buf_size) {
+                       printf("read failed\n");
+                       printf("read %ld, expected %ld\n",
+                              (unsigned long)bytes_read,
                               (unsigned long)buf_size); 
                        correct = False;
                        break;
@@ -838,17 +853,21 @@ static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
                }
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(c2, fnum2))) {
-               printf("close failed (%s)\n", cli_errstr(c2));
+       status = cli_close(c2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
                correct = False;
        }
-       if (!NT_STATUS_IS_OK(cli_close(c1, fnum1))) {
-               printf("close failed (%s)\n", cli_errstr(c1));
+
+       status = cli_close(c1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_unlink(c1, lockfname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(c1));
+       status = cli_unlink(c1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -863,8 +882,8 @@ static bool run_readwritetest(int dummy)
        if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
                return False;
        }
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("starting readwritetest\n");
 
@@ -894,7 +913,7 @@ static bool run_readwritemulti(int dummy)
 
        cli = current_cli;
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("run_readwritemulti: fname %s\n", randomfname);
        test = rw_torture3(cli, randomfname);
@@ -906,46 +925,39 @@ static bool run_readwritemulti(int dummy)
        return test;
 }
 
-static bool run_readwritelarge_internal(int max_xmit_k)
+static bool run_readwritelarge_internal(void)
 {
        static struct cli_state *cli1;
        uint16_t fnum1;
        const char *lockfname = "\\large.dat";
-       SMB_OFF_T fsize;
+       off_t fsize;
        char buf[126*1024];
        bool correct = True;
+       NTSTATUS status;
 
        if (!torture_open_connection(&cli1, 0)) {
                return False;
        }
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
        memset(buf,'\0',sizeof(buf));
 
-       cli1->max_xmit = max_xmit_k*1024;
-
-       if (signing_state == Required) {
-               /* Horrible cheat to force
-                  multiple signed outstanding
-                  packets against a Samba server.
-               */
-               cli1->is_samba = false;
-       }
-
        printf("starting readwritelarge_internal\n");
 
-       cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
+       status = cli_openx(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL,
+                         DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open read/write of %s failed (%s)\n", lockfname, nt_errstr(status));
                return False;
        }
 
        cli_writeall(cli1, fnum1, 0, (uint8_t *)buf, 0, sizeof(buf), NULL);
 
-       if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
-                                    cli1, fnum1, NULL, &fsize, NULL, NULL,
-                                    NULL, NULL, NULL))) {
-               printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
+       status = cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL,
+                                    NULL, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("qfileinfo failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -958,29 +970,31 @@ static bool run_readwritelarge_internal(int max_xmit_k)
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, lockfname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(cli1));
+       status = cli_unlink(cli1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open read/write of %s failed (%s)\n", lockfname, cli_errstr(cli1));
+       status = cli_openx(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL,
+                         DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open read/write of %s failed (%s)\n", lockfname, nt_errstr(status));
                return False;
        }
 
-       cli1->max_xmit = 4*1024;
-
        cli_smbwrite(cli1, fnum1, buf, 0, sizeof(buf), NULL);
 
-       if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
-                                    cli1, fnum1, NULL, &fsize, NULL, NULL,
-                                    NULL, NULL, NULL))) {
-               printf("qfileinfo failed (%s)\n", cli_errstr(cli1));
+       status = cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL,
+                                    NULL, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("qfileinfo failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -1008,8 +1022,9 @@ static bool run_readwritelarge_internal(int max_xmit_k)
                printf("readwritelarge test 3 (truncate test) succeeded (size = %x)\n", fsize);
 #endif
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -1021,15 +1036,15 @@ static bool run_readwritelarge_internal(int max_xmit_k)
 
 static bool run_readwritelarge(int dummy)
 {
-       return run_readwritelarge_internal(128);
+       return run_readwritelarge_internal();
 }
 
 static bool run_readwritelarge_signtest(int dummy)
 {
        bool ret;
-       signing_state = Required;
-       ret = run_readwritelarge_internal(2);
-       signing_state = Undefined;
+       signing_state = SMB_SIGNING_REQUIRED;
+       ret = run_readwritelarge_internal();
+       signing_state = SMB_SIGNING_DEFAULT;
        return ret;
 }
 
@@ -1053,7 +1068,7 @@ static bool run_netbench(int client)
 
        nbio_id = client;
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        nb_setup(cli);
 
@@ -1142,7 +1157,7 @@ static bool run_nbench(int dummy)
        double t;
        bool correct = True;
 
-       nbio_shmem(nprocs);
+       nbio_shmem(torture_nprocs);
 
        nbio_id = -1;
 
@@ -1171,54 +1186,66 @@ static bool run_locktest1(int dummy)
        uint16_t fnum1, fnum2, fnum3;
        time_t t1, t2;
        unsigned lock_timeout;
+       NTSTATUS status;
 
        if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
                return False;
        }
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("starting locktest1\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
-               return False;
-       }
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum2))) {
-               printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+                         &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
-       if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum3))) {
-               printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli2));
+
+       status = cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
-               printf("lock1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_openx(cli2, fname, O_RDWR, DENY_NONE, &fnum3);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open3 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
+       status = cli_lock32(cli1, fnum1, 0, 4, 0, WRITE_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("lock1 failed (%s)\n", nt_errstr(status));
+               return false;
+       }
 
-       if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
+       status = cli_lock32(cli2, fnum3, 0, 4, 0, WRITE_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                printf("lock2 succeeded! This is a locking bug\n");
-               return False;
+               return false;
        } else {
-               if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
-                                NT_STATUS_LOCK_NOT_GRANTED)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_LOCK_NOT_GRANTED)) {
+                       return false;
+               }
        }
 
-
        lock_timeout = (1 + (random() % 20));
        printf("Testing lock timeout with timeout=%u\n", lock_timeout);
        t1 = time(NULL);
-       if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
+       status = cli_lock32(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                printf("lock3 succeeded! This is a locking bug\n");
-               return False;
+               return false;
        } else {
-               if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
-                                NT_STATUS_FILE_LOCK_CONFLICT)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_FILE_LOCK_CONFLICT)) {
+                       return false;
+               }
        }
        t2 = time(NULL);
 
@@ -1229,31 +1256,38 @@ static bool run_locktest1(int dummy)
        printf("server slept for %u seconds for a %u second timeout\n",
               (unsigned int)(t2-t1), lock_timeout);
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (cli_lock(cli2, fnum3, 0, 4, 0, WRITE_LOCK)) {
+       status = cli_lock32(cli2, fnum3, 0, 4, 0, WRITE_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                printf("lock4 succeeded! This is a locking bug\n");
-               return False;
+               return false;
        } else {
-               if (!check_error(__LINE__, cli2, ERRDOS, ERRlock, 
-                                NT_STATUS_FILE_LOCK_CONFLICT)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_FILE_LOCK_CONFLICT)) {
+                       return false;
+               }
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum3))) {
-               printf("close3 failed (%s)\n", cli_errstr(cli2));
+       status = cli_close(cli2, fnum3);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close3 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(cli1));
+       status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                return False;
        }
 
@@ -1290,19 +1324,20 @@ static bool run_tcon_test(int dummy)
        if (!torture_open_connection(&cli, 0)) {
                return False;
        }
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting tcontest\n");
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       cnum1 = cli->cnum;
-       vuid1 = cli->vuid;
+       cnum1 = cli_state_get_tid(cli);
+       vuid1 = cli_state_get_uid(cli);
 
        status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1310,8 +1345,8 @@ static bool run_tcon_test(int dummy)
                return False;
        }
 
-       status = cli_tcon_andx(cli, share, "?????",
-                              password, strlen(password)+1);
+       status = cli_tree_connect(cli, share, "?????",
+                                 password, strlen(password)+1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("%s refused 2nd tree connect (%s)\n", host,
                       nt_errstr(status));
@@ -1319,12 +1354,12 @@ static bool run_tcon_test(int dummy)
                return False;
        }
 
-       cnum2 = cli->cnum;
+       cnum2 = cli_state_get_tid(cli);
        cnum3 = MAX(cnum1, cnum2) + 1; /* any invalid number */
-       vuid2 = cli->vuid + 1;
+       vuid2 = cli_state_get_uid(cli) + 1;
 
        /* try a write with the wrong tid */
-       cli->cnum = cnum2;
+       cli_state_set_tid(cli, cnum2);
 
        status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
        if (NT_STATUS_IS_OK(status)) {
@@ -1337,7 +1372,7 @@ static bool run_tcon_test(int dummy)
 
 
        /* try a write with an invalid tid */
-       cli->cnum = cnum3;
+       cli_state_set_tid(cli, cnum3);
 
        status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
        if (NT_STATUS_IS_OK(status)) {
@@ -1349,8 +1384,8 @@ static bool run_tcon_test(int dummy)
        }
 
        /* try a write with an invalid vuid */
-       cli->vuid = vuid2;
-       cli->cnum = cnum1;
+       cli_state_set_uid(cli, vuid2);
+       cli_state_set_tid(cli, cnum1);
 
        status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
        if (NT_STATUS_IS_OK(status)) {
@@ -1361,15 +1396,16 @@ static bool run_tcon_test(int dummy)
                       nt_errstr(status));
        }
 
-       cli->cnum = cnum1;
-       cli->vuid = vuid1;
+       cli_state_set_tid(cli, cnum1);
+       cli_state_set_uid(cli, vuid1);
 
-       if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
-               printf("close failed (%s)\n", cli_errstr(cli));
+       status = cli_close(cli, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli->cnum = cnum2;
+       cli_state_set_tid(cli, cnum2);
 
        status = cli_tdis(cli);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1377,7 +1413,7 @@ static bool run_tcon_test(int dummy)
                return False;
        }
 
-       cli->cnum = cnum1;
+       cli_state_set_tid(cli, cnum1);
 
        if (!torture_close_connection(cli)) {
                return False;
@@ -1400,7 +1436,7 @@ static bool run_tcon2_test(int dummy)
        if (!torture_open_connection(&cli, 0)) {
                return False;
        }
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting tcon2 test\n");
 
@@ -1435,8 +1471,8 @@ static bool tcon_devtest(struct cli_state *cli,
        NTSTATUS status;
        bool ret;
 
-       status = cli_tcon_andx(cli, myshare, devtype,
-                              password, strlen(password)+1);
+       status = cli_tree_connect(cli, myshare, devtype,
+                                 password, strlen(password)+1);
 
        if (NT_STATUS_IS_OK(expected_error)) {
                if (NT_STATUS_IS_OK(status)) {
@@ -1463,8 +1499,7 @@ static bool tcon_devtest(struct cli_state *cli,
                               myshare, devtype);
                        ret = False;
                } else {
-                       if (NT_STATUS_EQUAL(cli_nt_error(cli),
-                                           expected_error)) {
+                       if (NT_STATUS_EQUAL(status, expected_error)) {
                                ret = True;
                        } else {
                                printf("Returned unexpected error\n");
@@ -1552,69 +1587,84 @@ static bool run_locktest2(int dummy)
        const char *fname = "\\lockt2.lck";
        uint16_t fnum1, fnum2, fnum3;
        bool correct = True;
+       NTSTATUS status;
 
        if (!torture_open_connection(&cli, 0)) {
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting locktest2\n");
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        cli_setpid(cli, 1);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum2))) {
-               printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR, DENY_NONE, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        cli_setpid(cli, 2);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum3))) {
-               printf("open3 of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR, DENY_NONE, &fnum3);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open3 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        cli_setpid(cli, 1);
 
-       if (!cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
-               printf("lock1 failed (%s)\n", cli_errstr(cli));
-               return False;
+       status = cli_lock32(cli, fnum1, 0, 4, 0, WRITE_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("lock1 failed (%s)\n", nt_errstr(status));
+               return false;
        }
 
-       if (cli_lock(cli, fnum1, 0, 4, 0, WRITE_LOCK)) {
+       status = cli_lock32(cli, fnum1, 0, 4, 0, WRITE_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                printf("WRITE lock1 succeeded! This is a locking bug\n");
-               correct = False;
+               correct = false;
        } else {
-               if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
-                                NT_STATUS_LOCK_NOT_GRANTED)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_LOCK_NOT_GRANTED)) {
+                       return false;
+               }
        }
 
-       if (cli_lock(cli, fnum2, 0, 4, 0, WRITE_LOCK)) {
+       status = cli_lock32(cli, fnum2, 0, 4, 0, WRITE_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                printf("WRITE lock2 succeeded! This is a locking bug\n");
-               correct = False;
+               correct = false;
        } else {
-               if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
-                                NT_STATUS_LOCK_NOT_GRANTED)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_LOCK_NOT_GRANTED)) {
+                       return false;
+               }
        }
 
-       if (cli_lock(cli, fnum2, 0, 4, 0, READ_LOCK)) {
+       status = cli_lock32(cli, fnum2, 0, 4, 0, READ_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                printf("READ lock2 succeeded! This is a locking bug\n");
-               correct = False;
+               correct = false;
        } else {
-               if (!check_error(__LINE__, cli, ERRDOS, ERRlock, 
-                                NT_STATUS_FILE_LOCK_CONFLICT)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                NT_STATUS_FILE_LOCK_CONFLICT)) {
+                       return false;
+               }
        }
 
-       if (!cli_lock(cli, fnum1, 100, 4, 0, WRITE_LOCK)) {
-               printf("lock at 100 failed (%s)\n", cli_errstr(cli));
+       status = cli_lock32(cli, fnum1, 100, 4, 0, WRITE_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("lock at 100 failed (%s)\n", nt_errstr(status));
        }
        cli_setpid(cli, 2);
        if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 100, 4))) {
@@ -1622,45 +1672,56 @@ static bool run_locktest2(int dummy)
                correct = False;
        }
 
-       if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 4))) {
+       status = cli_unlock(cli, fnum1, 0, 4);
+       if (NT_STATUS_IS_OK(status)) {
                printf("unlock1 succeeded! This is a locking bug\n");
-               correct = False;
+               correct = false;
        } else {
-               if (!check_error(__LINE__, cli, 
-                                ERRDOS, ERRlock, 
-                                NT_STATUS_RANGE_NOT_LOCKED)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_RANGE_NOT_LOCKED)) {
+                       return false;
+               }
        }
 
-       if (NT_STATUS_IS_OK(cli_unlock(cli, fnum1, 0, 8))) {
+       status = cli_unlock(cli, fnum1, 0, 8);
+       if (NT_STATUS_IS_OK(status)) {
                printf("unlock2 succeeded! This is a locking bug\n");
-               correct = False;
+               correct = false;
        } else {
-               if (!check_error(__LINE__, cli, 
-                                ERRDOS, ERRlock, 
-                                NT_STATUS_RANGE_NOT_LOCKED)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_RANGE_NOT_LOCKED)) {
+                       return false;
+               }
        }
 
-       if (cli_lock(cli, fnum3, 0, 4, 0, WRITE_LOCK)) {
+       status = cli_lock32(cli, fnum3, 0, 4, 0, WRITE_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                printf("lock3 succeeded! This is a locking bug\n");
-               correct = False;
+               correct = false;
        } else {
-               if (!check_error(__LINE__, cli, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRlock,
+                                     NT_STATUS_LOCK_NOT_GRANTED)) {
+                       return false;
+               }
        }
 
        cli_setpid(cli, 1);
 
-       if (!NT_STATUS_IS_OK(cli_close(cli, fnum1))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli));
+       status = cli_close(cli, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli, fnum2))) {
-               printf("close2 failed (%s)\n", cli_errstr(cli));
+       status = cli_close(cli, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli, fnum3))) {
-               printf("close3 failed (%s)\n", cli_errstr(cli));
+       status = cli_close(cli, fnum3);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close3 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
@@ -1687,41 +1748,49 @@ static bool run_locktest3(int dummy)
        int i;
        uint32 offset;
        bool correct = True;
+       NTSTATUS status;
 
 #define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
 
        if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
                return False;
        }
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("starting locktest3\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+                        &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
-       if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
-               printf("open2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+
+       status = cli_openx(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        for (offset=i=0;i<torture_numops;i++) {
                NEXT_OFFSET;
-               if (!cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
+
+               status = cli_lock32(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK);
+               if (!NT_STATUS_IS_OK(status)) {
                        printf("lock1 %d failed (%s)\n", 
                               i,
-                              cli_errstr(cli1));
+                              nt_errstr(status));
                        return False;
                }
 
-               if (!cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
+               status = cli_lock32(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK);
+               if (!NT_STATUS_IS_OK(status)) {
                        printf("lock2 %d failed (%s)\n", 
                               i,
-                              cli_errstr(cli1));
+                              nt_errstr(status));
                        return False;
                }
        }
@@ -1729,22 +1798,26 @@ static bool run_locktest3(int dummy)
        for (offset=i=0;i<torture_numops;i++) {
                NEXT_OFFSET;
 
-               if (cli_lock(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK)) {
+               status = cli_lock32(cli1, fnum1, offset-2, 1, 0, WRITE_LOCK);
+               if (NT_STATUS_IS_OK(status)) {
                        printf("error: lock1 %d succeeded!\n", i);
                        return False;
                }
 
-               if (cli_lock(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK)) {
+               status = cli_lock32(cli2, fnum2, offset-1, 1, 0, WRITE_LOCK);
+               if (NT_STATUS_IS_OK(status)) {
                        printf("error: lock2 %d succeeded!\n", i);
                        return False;
                }
 
-               if (cli_lock(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK)) {
+               status = cli_lock32(cli1, fnum1, offset-1, 1, 0, WRITE_LOCK);
+               if (NT_STATUS_IS_OK(status)) {
                        printf("error: lock3 %d succeeded!\n", i);
                        return False;
                }
 
-               if (cli_lock(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK)) {
+               status = cli_lock32(cli2, fnum2, offset-2, 1, 0, WRITE_LOCK);
+               if (NT_STATUS_IS_OK(status)) {
                        printf("error: lock4 %d succeeded!\n", i);
                        return False;
                }
@@ -1753,33 +1826,38 @@ static bool run_locktest3(int dummy)
        for (offset=i=0;i<torture_numops;i++) {
                NEXT_OFFSET;
 
-               if (!NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, offset-1, 1))) {
+               status = cli_unlock(cli1, fnum1, offset-1, 1);
+               if (!NT_STATUS_IS_OK(status)) {
                        printf("unlock1 %d failed (%s)\n", 
                               i,
-                              cli_errstr(cli1));
+                              nt_errstr(status));
                        return False;
                }
 
-               if (!NT_STATUS_IS_OK(cli_unlock(cli2, fnum2, offset-2, 1))) {
+               status = cli_unlock(cli2, fnum2, offset-2, 1);
+               if (!NT_STATUS_IS_OK(status)) {
                        printf("unlock2 %d failed (%s)\n", 
                               i,
-                              cli_errstr(cli1));
+                              nt_errstr(status));
                        return False;
                }
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-               printf("close2 failed (%s)\n", cli_errstr(cli2));
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(cli1));
+       status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                return False;
        }
 
@@ -1796,6 +1874,28 @@ static bool run_locktest3(int dummy)
        return correct;
 }
 
+static bool test_cli_read(struct cli_state *cli, uint16_t fnum,
+                           char *buf, off_t offset, size_t size,
+                           size_t *nread, size_t expect)
+{
+       NTSTATUS status;
+       size_t l_nread;
+
+       status = cli_read(cli, fnum, buf, offset, size, &l_nread);
+
+       if(!NT_STATUS_IS_OK(status)) {
+               return false;
+       } else if (l_nread != expect) {
+               return false;
+       }
+
+       if (nread) {
+               *nread = l_nread;
+       }
+
+       return true;
+}
+
 #define EXPECTED(ret, v) if ((ret) != (v)) { \
         printf("** "); correct = False; \
         }
@@ -1817,15 +1917,15 @@ static bool run_locktest4(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("starting locktest4\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
-       cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
+       cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       cli_openx(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
 
        memset(buf, 0, sizeof(buf));
 
@@ -1837,74 +1937,81 @@ static bool run_locktest4(int dummy)
                goto fail;
        }
 
-       ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
-             cli_lock(cli1, fnum1, 2, 4, 0, WRITE_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 2, 4, 0, WRITE_LOCK));
        EXPECTED(ret, False);
        printf("the same process %s set overlapping write locks\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 10, 4, 0, READ_LOCK) &&
-             cli_lock(cli1, fnum1, 12, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 10, 4, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 12, 4, 0, READ_LOCK));
        EXPECTED(ret, True);
        printf("the same process %s set overlapping read locks\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 20, 4, 0, WRITE_LOCK) &&
-             cli_lock(cli2, fnum2, 22, 4, 0, WRITE_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 20, 4, 0, WRITE_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli2, fnum2, 22, 4, 0, WRITE_LOCK));
        EXPECTED(ret, False);
        printf("a different connection %s set overlapping write locks\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 30, 4, 0, READ_LOCK) &&
-             cli_lock(cli2, fnum2, 32, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 30, 4, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli2, fnum2, 32, 4, 0, READ_LOCK));
        EXPECTED(ret, True);
        printf("a different connection %s set overlapping read locks\n", ret?"can":"cannot");
 
-       ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 40, 4, 0, WRITE_LOCK)) &&
-             (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 42, 4, 0, WRITE_LOCK));
+       ret = (cli_setpid(cli1, 1),
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 40, 4, 0, WRITE_LOCK))) &&
+             (cli_setpid(cli1, 2),
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 42, 4, 0, WRITE_LOCK)));
        EXPECTED(ret, False);
        printf("a different pid %s set overlapping write locks\n", ret?"can":"cannot");
 
-       ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 50, 4, 0, READ_LOCK)) &&
-             (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 52, 4, 0, READ_LOCK));
+       ret = (cli_setpid(cli1, 1),
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 50, 4, 0, READ_LOCK))) &&
+             (cli_setpid(cli1, 2),
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 52, 4, 0, READ_LOCK)));
        EXPECTED(ret, True);
        printf("a different pid %s set overlapping read locks\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK) &&
-             cli_lock(cli1, fnum1, 60, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 60, 4, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 60, 4, 0, READ_LOCK));
        EXPECTED(ret, True);
        printf("the same process %s set the same read lock twice\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK) &&
-             cli_lock(cli1, fnum1, 70, 4, 0, WRITE_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 70, 4, 0, WRITE_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 70, 4, 0, WRITE_LOCK));
        EXPECTED(ret, False);
        printf("the same process %s set the same write lock twice\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 80, 4, 0, READ_LOCK) &&
-             cli_lock(cli1, fnum1, 80, 4, 0, WRITE_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 80, 4, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 80, 4, 0, WRITE_LOCK));
        EXPECTED(ret, False);
        printf("the same process %s overlay a read lock with a write lock\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 90, 4, 0, WRITE_LOCK) &&
-             cli_lock(cli1, fnum1, 90, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 90, 4, 0, WRITE_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 90, 4, 0, READ_LOCK));
        EXPECTED(ret, True);
        printf("the same process %s overlay a write lock with a read lock\n", ret?"can":"cannot");
 
-       ret = (cli_setpid(cli1, 1), cli_lock(cli1, fnum1, 100, 4, 0, WRITE_LOCK)) &&
-             (cli_setpid(cli1, 2), cli_lock(cli1, fnum1, 100, 4, 0, READ_LOCK));
+       ret = (cli_setpid(cli1, 1),
+            NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 100, 4, 0, WRITE_LOCK))) &&
+            (cli_setpid(cli1, 2),
+            NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 100, 4, 0, READ_LOCK)));
        EXPECTED(ret, False);
        printf("a different pid %s overlay a write lock with a read lock\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 110, 4, 0, READ_LOCK) &&
-             cli_lock(cli1, fnum1, 112, 4, 0, READ_LOCK) &&
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 110, 4, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 112, 4, 0, READ_LOCK)) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 110, 6));
        EXPECTED(ret, False);
        printf("the same process %s coalesce read locks\n", ret?"can":"cannot");
 
 
-       ret = cli_lock(cli1, fnum1, 120, 4, 0, WRITE_LOCK) &&
-             (cli_read(cli2, fnum2, buf, 120, 4) == 4);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 120, 4, 0, WRITE_LOCK)) &&
+             test_cli_read(cli2, fnum2, buf, 120, 4, NULL, 4);
        EXPECTED(ret, False);
        printf("this server %s strict write locking\n", ret?"doesn't do":"does");
 
-       ret = cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK);
+       status = cli_lock32(cli1, fnum1, 130, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(status);
        if (ret) {
                status = cli_writeall(cli2, fnum2, 0, (uint8_t *)buf, 130, 4,
                                      NULL);
@@ -1914,58 +2021,58 @@ static bool run_locktest4(int dummy)
        printf("this server %s strict read locking\n", ret?"doesn't do":"does");
 
 
-       ret = cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
-             cli_lock(cli1, fnum1, 140, 4, 0, READ_LOCK) &&
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 140, 4, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 140, 4, 0, READ_LOCK)) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4)) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 140, 4));
        EXPECTED(ret, True);
        printf("this server %s do recursive read locking\n", ret?"does":"doesn't");
 
 
-       ret = cli_lock(cli1, fnum1, 150, 4, 0, WRITE_LOCK) &&
-             cli_lock(cli1, fnum1, 150, 4, 0, READ_LOCK) &&
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 150, 4, 0, WRITE_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 150, 4, 0, READ_LOCK)) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4)) &&
-             (cli_read(cli2, fnum2, buf, 150, 4) == 4) &&
+             test_cli_read(cli2, fnum2, buf, 150, 4, NULL, 4) &&
              !(NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
                                             150, 4, NULL))) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 150, 4));
        EXPECTED(ret, True);
        printf("this server %s do recursive lock overlays\n", ret?"does":"doesn't");
 
-       ret = cli_lock(cli1, fnum1, 160, 4, 0, READ_LOCK) &&
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 160, 4, 0, READ_LOCK)) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 160, 4)) &&
              NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
                                           160, 4, NULL)) &&
-             (cli_read(cli2, fnum2, buf, 160, 4) == 4);                
+             test_cli_read(cli2, fnum2, buf, 160, 4, NULL, 4);
        EXPECTED(ret, True);
        printf("the same process %s remove a read lock using write locking\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 170, 4, 0, WRITE_LOCK) &&
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 170, 4, 0, WRITE_LOCK)) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 170, 4)) &&
              NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
                                           170, 4, NULL)) &&
-             (cli_read(cli2, fnum2, buf, 170, 4) == 4);                
+             test_cli_read(cli2, fnum2, buf, 170, 4, NULL, 4);
        EXPECTED(ret, True);
        printf("the same process %s remove a write lock using read locking\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli1, fnum1, 190, 4, 0, WRITE_LOCK) &&
-             cli_lock(cli1, fnum1, 190, 4, 0, READ_LOCK) &&
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 190, 4, 0, WRITE_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 190, 4, 0, READ_LOCK)) &&
              NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 190, 4)) &&
              !NT_STATUS_IS_OK(cli_writeall(cli2, fnum2, 0, (uint8_t *)buf,
                                            190, 4, NULL)) &&
-             (cli_read(cli2, fnum2, buf, 190, 4) == 4);                
+             test_cli_read(cli2, fnum2, buf, 190, 4, NULL, 4);
        EXPECTED(ret, True);
        printf("the same process %s remove the first lock first\n", ret?"does":"doesn't");
 
        cli_close(cli1, fnum1);
        cli_close(cli2, fnum2);
-       cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
-       cli_open(cli1, fname, O_RDWR, DENY_NONE, &f);
-       ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
-             cli_lock(cli1, f, 0, 1, 0, READ_LOCK) &&
+       cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
+       cli_openx(cli1, fname, O_RDWR, DENY_NONE, &f);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 0, 8, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, f, 0, 1, 0, READ_LOCK)) &&
              NT_STATUS_IS_OK(cli_close(cli1, fnum1)) &&
-             NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1)) &&
-             cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
+             NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 7, 1, 0, WRITE_LOCK));
         cli_close(cli1, f);
        cli_close(cli1, fnum1);
        EXPECTED(ret, True);
@@ -1974,7 +2081,7 @@ static bool run_locktest4(int dummy)
  fail:
        cli_close(cli1, fnum1);
        cli_close(cli2, fnum2);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        torture_close_connection(cli1);
        torture_close_connection(cli2);
 
@@ -1999,16 +2106,16 @@ static bool run_locktest5(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("starting locktest5\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
-       cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
-       cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum3);
+       cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       cli_openx(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
+       cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum3);
 
        memset(buf, 0, sizeof(buf));
 
@@ -2021,23 +2128,25 @@ static bool run_locktest5(int dummy)
        }
 
        /* Check for NT bug... */
-       ret = cli_lock(cli1, fnum1, 0, 8, 0, READ_LOCK) &&
-                 cli_lock(cli1, fnum3, 0, 1, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 0, 8, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum3, 0, 1, 0, READ_LOCK));
        cli_close(cli1, fnum1);
-       cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
-       ret = cli_lock(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
+       cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
+       status = cli_lock32(cli1, fnum1, 7, 1, 0, WRITE_LOCK);
+       ret = NT_STATUS_IS_OK(status);
        EXPECTED(ret, True);
        printf("this server %s the NT locking bug\n", ret ? "doesn't have" : "has");
        cli_close(cli1, fnum1);
-       cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
+       cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
        cli_unlock(cli1, fnum3, 0, 1);
 
-       ret = cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK) &&
-             cli_lock(cli1, fnum1, 1, 1, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 1, 1, 0, READ_LOCK));
        EXPECTED(ret, True);
        printf("the same process %s overlay a write with a read lock\n", ret?"can":"cannot");
 
-       ret = cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
+       status = cli_lock32(cli2, fnum2, 0, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(status);
        EXPECTED(ret, False);
 
        printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
@@ -2045,7 +2154,8 @@ static bool run_locktest5(int dummy)
        /* Unlock the process 2 lock. */
        cli_unlock(cli2, fnum2, 0, 4);
 
-       ret = cli_lock(cli1, fnum3, 0, 4, 0, READ_LOCK);
+       status = cli_lock32(cli1, fnum3, 0, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(status);
        EXPECTED(ret, False);
 
        printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
@@ -2054,8 +2164,8 @@ static bool run_locktest5(int dummy)
        cli_unlock(cli1, fnum3, 0, 4);
 
        /* Stack 2 more locks here. */
-       ret = cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK) &&
-                 cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK);
+       ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 0, 4, 0, READ_LOCK)) &&
+             NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 0, 4, 0, READ_LOCK));
 
        EXPECTED(ret, True);
        printf("the same process %s stack read locks\n", ret?"can":"cannot");
@@ -2064,7 +2174,7 @@ static bool run_locktest5(int dummy)
                removed. */
 
        ret = NT_STATUS_IS_OK(cli_unlock(cli1, fnum1, 0, 4)) &&
-                       cli_lock(cli2, fnum2, 0, 4, 0, READ_LOCK);
+             NT_STATUS_IS_OK(cli_lock32(cli2, fnum2, 0, 4, 0, READ_LOCK));
 
        EXPECTED(ret, True);
        printf("the first unlock removes the %s lock\n", ret?"WRITE":"READ");
@@ -2087,7 +2197,8 @@ static bool run_locktest5(int dummy)
        printf("the same process %s count the lock stack\n", !ret?"can":"cannot"); 
 
        /* Ensure connection 2 can get a write lock. */
-       ret = cli_lock(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
+       status = cli_lock32(cli2, fnum2, 0, 4, 0, WRITE_LOCK);
+       ret = NT_STATUS_IS_OK(status);
        EXPECTED(ret, True);
 
        printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
@@ -2096,7 +2207,7 @@ static bool run_locktest5(int dummy)
  fail:
        cli_close(cli1, fnum1);
        cli_close(cli2, fnum2);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        if (!torture_close_connection(cli1)) {
                correct = False;
        }
@@ -2124,26 +2235,26 @@ static bool run_locktest6(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting locktest6\n");
 
        for (i=0;i<1;i++) {
                printf("Testing %s\n", fname[i]);
 
-               cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
+               cli_unlink(cli, fname[i], FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-               cli_open(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+               cli_openx(cli, fname[i], O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
                status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CHANGE_LOCKTYPE);
                cli_close(cli, fnum);
                printf("CHANGE_LOCKTYPE gave %s\n", nt_errstr(status));
 
-               cli_open(cli, fname[i], O_RDWR, DENY_NONE, &fnum);
+               cli_openx(cli, fname[i], O_RDWR, DENY_NONE, &fnum);
                status = cli_locktype(cli, fnum, 0, 8, 0, LOCKING_ANDX_CANCEL_LOCK);
                cli_close(cli, fnum);
                printf("CANCEL_LOCK gave %s\n", nt_errstr(status));
 
-               cli_unlink(cli, fname[i], aSYSTEM | aHIDDEN);
+               cli_unlink(cli, fname[i], FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        }
 
        torture_close_connection(cli);
@@ -2159,19 +2270,20 @@ static bool run_locktest7(int dummy)
        uint16_t fnum1;
        char buf[200];
        bool correct = False;
+       size_t nread;
        NTSTATUS status;
 
        if (!torture_open_connection(&cli1, 0)) {
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        printf("starting locktest7\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
 
        memset(buf, 0, sizeof(buf));
 
@@ -2184,15 +2296,23 @@ static bool run_locktest7(int dummy)
 
        cli_setpid(cli1, 1);
 
-       if (!cli_lock(cli1, fnum1, 130, 4, 0, READ_LOCK)) {
-               printf("Unable to apply read lock on range 130:4, error was %s\n", cli_errstr(cli1));
+       status = cli_lock32(cli1, fnum1, 130, 4, 0, READ_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Unable to apply read lock on range 130:4, "
+                      "error was %s\n", nt_errstr(status));
                goto fail;
        } else {
                printf("pid1 successfully locked range 130:4 for READ\n");
        }
 
-       if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
-               printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
+       status = cli_read(cli1, fnum1, buf, 130, 4, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("pid1 unable to read the range 130:4, error was %s\n",
+                     nt_errstr(status));
+               goto fail;
+       } else if (nread != 4) {
+               printf("pid1 unable to read the range 130:4, "
+                      "recv %ld req %d\n", (unsigned long)nread, 4);
                goto fail;
        } else {
                printf("pid1 successfully read the range 130:4\n");
@@ -2213,8 +2333,15 @@ static bool run_locktest7(int dummy)
 
        cli_setpid(cli1, 2);
 
-       if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
-               printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
+       status = cli_read(cli1, fnum1, buf, 130, 4, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("pid2 unable to read the range 130:4, error was %s\n",
+                     nt_errstr(status));
+               goto fail;
+       } else if (nread != 4) {
+               printf("pid2 unable to read the range 130:4, "
+                      "recv %ld req %d\n", (unsigned long)nread, 4);
+               goto fail;
        } else {
                printf("pid2 successfully read the range 130:4\n");
        }
@@ -2235,15 +2362,22 @@ static bool run_locktest7(int dummy)
        cli_setpid(cli1, 1);
        cli_unlock(cli1, fnum1, 130, 4);
 
-       if (!cli_lock(cli1, fnum1, 130, 4, 0, WRITE_LOCK)) {
-               printf("Unable to apply write lock on range 130:4, error was %s\n", cli_errstr(cli1));
+       status = cli_lock32(cli1, fnum1, 130, 4, 0, WRITE_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Unable to apply write lock on range 130:4, error was %s\n", nt_errstr(status));
                goto fail;
        } else {
                printf("pid1 successfully locked range 130:4 for WRITE\n");
        }
 
-       if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
-               printf("pid1 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
+       status = cli_read(cli1, fnum1, buf, 130, 4, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("pid1 unable to read the range 130:4, error was %s\n",
+                     nt_errstr(status));
+               goto fail;
+       } else if (nread != 4) {
+               printf("pid1 unable to read the range 130:4, "
+                      "recv %ld req %d\n", (unsigned long)nread, 4);
                goto fail;
        } else {
                printf("pid1 successfully read the range 130:4\n");
@@ -2260,14 +2394,17 @@ static bool run_locktest7(int dummy)
 
        cli_setpid(cli1, 2);
 
-       if (cli_read(cli1, fnum1, buf, 130, 4) != 4) {
-               printf("pid2 unable to read the range 130:4, error was %s\n", cli_errstr(cli1));
-               if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_FILE_LOCK_CONFLICT)) {
+       status = cli_read(cli1, fnum1, buf, 130, 4, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("pid2 unable to read the range 130:4, error was "
+                      "%s\n", nt_errstr(status));
+               if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
                        printf("Incorrect error (should be NT_STATUS_FILE_LOCK_CONFLICT)\n");
                        goto fail;
                }
        } else {
-               printf("pid2 successfully read the range 130:4 (should be denied)\n");
+               printf("pid2 successfully read the range 130:4 (should be denied) recv %ld\n",
+                      (unsigned long)nread);
                goto fail;
        }
 
@@ -2289,7 +2426,7 @@ static bool run_locktest7(int dummy)
 
 fail:
        cli_close(cli1, fnum1);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        torture_close_connection(cli1);
 
        printf("finished locktest7\n");
@@ -2317,44 +2454,45 @@ static bool run_locktest8(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        printf("starting locktest8\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_WRITE,
                          &fnum1);
        if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
+               d_fprintf(stderr, "cli_openx returned %s\n", nt_errstr(status));
                return false;
        }
 
        memset(buf, 0, sizeof(buf));
 
-       status = cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum2);
        if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "cli_open second time returned %s\n",
-                         cli_errstr(cli1));
+               d_fprintf(stderr, "cli_openx second time returned %s\n",
+                         nt_errstr(status));
                goto fail;
        }
 
-       if (!cli_lock(cli1, fnum2, 1, 1, 0, READ_LOCK)) {
+       status = cli_lock32(cli1, fnum2, 1, 1, 0, READ_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("Unable to apply read lock on range 1:1, error was "
-                      "%s\n", cli_errstr(cli1));
+                      "%s\n", nt_errstr(status));
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
+               d_fprintf(stderr, "cli_close(fnum1) %s\n", nt_errstr(status));
                goto fail;
        }
 
-       status = cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
+       status = cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
        if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "cli_open third time returned %s\n",
-                          cli_errstr(cli1));
+               d_fprintf(stderr, "cli_openx third time returned %s\n",
+                          nt_errstr(status));
                 goto fail;
         }
 
@@ -2363,7 +2501,7 @@ static bool run_locktest8(int dummy)
 fail:
        cli_close(cli1, fnum1);
        cli_close(cli1, fnum2);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        torture_close_connection(cli1);
 
        printf("finished locktest8\n");
@@ -2378,7 +2516,7 @@ fail:
  */
 
 static bool got_alarm;
-static int alarm_fd;
+static struct cli_state *alarm_cli;
 
 static void alarm_handler(int dummy)
 {
@@ -2387,7 +2525,7 @@ static void alarm_handler(int dummy)
 
 static void alarm_handler_parent(int dummy)
 {
-       close(alarm_fd);
+       smbXcli_conn_disconnect(alarm_cli->conn, NT_STATUS_OK);
 }
 
 static void do_local_lock(int read_fd, int write_fd)
@@ -2511,17 +2649,18 @@ static bool run_locktest9(int dummy)
                return false;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
-       status = cli_open(cli1, fname, O_RDWR, DENY_NONE,
+       status = cli_openx(cli1, fname, O_RDWR, DENY_NONE,
                          &fnum);
        if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "cli_open returned %s\n", cli_errstr(cli1));
+               d_fprintf(stderr, "cli_openx returned %s\n", nt_errstr(status));
                return false;
        }
 
        /* Ensure the child has the lock. */
-       if (cli_lock(cli1, fnum, 0, 4, 0, WRITE_LOCK)) {
+       status = cli_lock32(cli1, fnum, 0, 4, 0, WRITE_LOCK);
+       if (NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, "Got the lock on range 0:4 - this should not happen !\n");
                goto fail;
        } else {
@@ -2537,15 +2676,16 @@ static bool run_locktest9(int dummy)
        }
 
        /* Wait 20 seconds for the lock. */
-       alarm_fd = cli1->fd;
+       alarm_cli = cli1;
        CatchSignal(SIGALRM, alarm_handler_parent);
        alarm(20);
 
        start = timeval_current();
 
-       if (!cli_lock(cli1, fnum, 0, 4, -1, WRITE_LOCK)) {
+       status = cli_lock32(cli1, fnum, 0, 4, -1, WRITE_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, "Unable to apply write lock on range 0:4, error was "
-                      "%s\n", cli_errstr(cli1));
+                      "%s\n", nt_errstr(status));
                goto fail_nofd;
        }
        alarm(0);
@@ -2557,7 +2697,7 @@ static bool run_locktest9(int dummy)
 
        status = cli_close(cli1, fnum);
        if (!NT_STATUS_IS_OK(status)) {
-               d_fprintf(stderr, "cli_close(fnum1) %s\n", cli_errstr(cli1));
+               d_fprintf(stderr, "cli_close(fnum1) %s\n", nt_errstr(status));
                goto fail;
        }
 
@@ -2588,37 +2728,38 @@ static bool run_fdpasstest(int dummy)
        if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
                return False;
        }
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("starting fdpasstest\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+                         &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       status = cli_writeall(cli1, fnum1, 0, (uint8_t *)"hello world\n", 0,
+       status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"hello world\n", 0,
                              13, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("write failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli2->vuid = cli1->vuid;
-       cli2->cnum = cli1->cnum;
-       cli2->pid = cli1->pid;
+       cli_state_set_uid(cli2, cli_state_get_uid(cli1));
+       cli_state_set_tid(cli2, cli_state_get_tid(cli1));
+       cli_setpid(cli2, cli_getpid(cli1));
 
-       if (cli_read(cli2, fnum1, buf, 0, 13) == 13) {
-               printf("read succeeded! nasty security hole [%s]\n",
-                      buf);
-               return False;
+       if (test_cli_read(cli2, fnum1, buf, 0, 13, NULL, 13)) {
+               printf("read succeeded! nasty security hole [%s]\n", buf);
+               return false;
        }
 
        cli_close(cli1, fnum1);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        torture_close_connection(cli1);
        torture_close_connection(cli2);
@@ -2644,67 +2785,67 @@ static bool run_fdsesstest(int dummy)
 
        if (!torture_open_connection(&cli, 0))
                return False;
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        if (!torture_cli_session_setup2(cli, &new_vuid))
                return False;
 
-       saved_cnum = cli->cnum;
-       if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
+       saved_cnum = cli_state_get_tid(cli);
+       if (!NT_STATUS_IS_OK(cli_tree_connect(cli, share, "?????", "", 1)))
                return False;
-       new_cnum = cli->cnum;
-       cli->cnum = saved_cnum;
+       new_cnum = cli_state_get_tid(cli);
+       cli_state_set_tid(cli, saved_cnum);
 
        printf("starting fdsesstest\n");
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       status = cli_writeall(cli, fnum1, 0, (uint8_t *)"hello world\n", 0, 13,
+       status = cli_writeall(cli, fnum1, 0, (const uint8_t *)"hello world\n", 0, 13,
                              NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("write failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       saved_vuid = cli->vuid;
-       cli->vuid = new_vuid;
+       saved_vuid = cli_state_get_uid(cli);
+       cli_state_set_uid(cli, new_vuid);
 
-       if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
-               printf("read succeeded with different vuid! nasty security hole [%s]\n",
-                      buf);
-               ret = False;
+       if (test_cli_read(cli, fnum1, buf, 0, 13, NULL, 13)) {
+               printf("read succeeded with different vuid! "
+                      "nasty security hole [%s]\n", buf);
+               ret = false;
        }
        /* Try to open a file with different vuid, samba cnum. */
-       if (NT_STATUS_IS_OK(cli_open(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
+       if (NT_STATUS_IS_OK(cli_openx(cli, fname1, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum2))) {
                printf("create with different vuid, same cnum succeeded.\n");
                cli_close(cli, fnum2);
-               cli_unlink(cli, fname1, aSYSTEM | aHIDDEN);
+               cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        } else {
                printf("create with different vuid, same cnum failed.\n");
                printf("This will cause problems with service clients.\n");
                ret = False;
        }
 
-       cli->vuid = saved_vuid;
+       cli_state_set_uid(cli, saved_vuid);
 
        /* Try with same vuid, different cnum. */
-       cli->cnum = new_cnum;
+       cli_state_set_tid(cli, new_cnum);
 
-       if (cli_read(cli, fnum1, buf, 0, 13) == 13) {
-               printf("read succeeded with different cnum![%s]\n",
-                      buf);
-               ret = False;
+       if (test_cli_read(cli, fnum1, buf, 0, 13, NULL, 13)) {
+               printf("read succeeded with different cnum![%s]\n", buf);
+               ret = false;
        }
 
-       cli->cnum = saved_cnum;
+       cli_state_set_tid(cli, saved_cnum);
        cli_close(cli, fnum1);
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        torture_close_connection(cli);
 
@@ -2723,34 +2864,38 @@ static bool run_unlinktest(int dummy)
        const char *fname = "\\unlink.tst";
        uint16_t fnum;
        bool correct = True;
+       NTSTATUS status;
 
        if (!torture_open_connection(&cli, 0)) {
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting unlink test\n");
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        cli_setpid(cli, 1);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
+       status = cli_unlink(cli, fname,
+                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (NT_STATUS_IS_OK(status)) {
                printf("error: server allowed unlink on an open file\n");
                correct = False;
        } else {
-               correct = check_error(__LINE__, cli, ERRDOS, ERRbadshare, 
+               correct = check_error(__LINE__, status, ERRDOS, ERRbadshare,
                                      NT_STATUS_SHARING_VIOLATION);
        }
 
        cli_close(cli, fnum);
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        if (!torture_close_connection(cli)) {
                correct = False;
@@ -2768,12 +2913,12 @@ test how many open files this server supports on the one socket
 static bool run_maxfidtest(int dummy)
 {
        struct cli_state *cli;
-       const char *ftemplate = "\\maxfid.%d.%d";
        fstring fname;
        uint16_t fnums[0x11000];
        int i;
        int retries=4;
        bool correct = True;
+       NTSTATUS status;
 
        cli = current_cli;
 
@@ -2782,14 +2927,15 @@ static bool run_maxfidtest(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        for (i=0; i<0x11000; i++) {
-               slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
-               if (!NT_STATUS_IS_OK(cli_open(cli, fname, 
-                                       O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnums[i]))) {
+               slprintf(fname,sizeof(fname)-1,"\\maxfid.%d.%d", i,(int)getpid());
+               status = cli_openx(cli, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE,
+                                 &fnums[i]);
+               if (!NT_STATUS_IS_OK(status)) {
                        printf("open of %s failed (%s)\n", 
-                              fname, cli_errstr(cli));
+                              fname, nt_errstr(status));
                        printf("maximum fnum is %d\n", i);
                        break;
                }
@@ -2800,11 +2946,13 @@ static bool run_maxfidtest(int dummy)
 
        printf("cleaning up\n");
        for (;i>=0;i--) {
-               slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
+               slprintf(fname,sizeof(fname)-1,"\\maxfid.%d.%d", i,(int)getpid());
                cli_close(cli, fnums[i]);
-               if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
+
+               status = cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+               if (!NT_STATUS_IS_OK(status)) {
                        printf("unlink of %s failed (%s)\n", 
-                              fname, cli_errstr(cli));
+                              fname, nt_errstr(status));
                        correct = False;
                }
                printf("%6d\r", i);
@@ -2850,7 +2998,8 @@ static bool run_negprot_nowait(int dummy)
        for (i=0;i<50000;i++) {
                struct tevent_req *req;
 
-               req = cli_negprot_send(ev, ev, cli);
+               req = smbXcli_negprot_send(ev, ev, cli->conn, cli->timeout,
+                                          PROTOCOL_CORE, PROTOCOL_NT1);
                if (req == NULL) {
                        TALLOC_FREE(ev);
                        return false;
@@ -2876,15 +3025,37 @@ static bool run_negprot_nowait(int dummy)
 /* send smb negprot commands, not reading the response */
 static bool run_bad_nbt_session(int dummy)
 {
-       static struct cli_state *cli;
+       struct nmb_name called, calling;
+       struct sockaddr_storage ss;
+       NTSTATUS status;
+       int fd;
+       bool ret;
 
        printf("starting bad nbt session test\n");
 
-       if (!(cli = open_bad_nbt_connection())) {
-               return False;
+       make_nmb_name(&calling, myname, 0x0);
+       make_nmb_name(&called , host, 0x20);
+
+       if (!resolve_name(host, &ss, 0x20, true)) {
+               d_fprintf(stderr, "Could not resolve name %s\n", host);
+               return false;
+       }
+
+       status = open_socket_out(&ss, NBT_SMB_PORT, 10000, &fd);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, "open_socket_out failed: %s\n",
+                         nt_errstr(status));
+               return false;
+       }
+
+       ret = cli_bad_session_request(fd, &calling, &called);
+       close(fd);
+       if (!ret) {
+               d_fprintf(stderr, "open_socket_out failed: %s\n",
+                         nt_errstr(status));
+               return false;
        }
 
-       cli_shutdown(cli);
        printf("finished bad nbt session test\n");
        return true;
 }
@@ -2991,6 +3162,7 @@ static bool run_attrtest(int dummy)
        time_t t, t2;
        const char *fname = "\\attrib123456789.tst";
        bool correct = True;
+       NTSTATUS status;
 
        printf("starting attrib test\n");
 
@@ -2998,12 +3170,14 @@ static bool run_attrtest(int dummy)
                return False;
        }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       cli_open(cli, fname, 
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_openx(cli, fname, 
                        O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
        cli_close(cli, fnum);
-       if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
-               printf("getatr failed (%s)\n", cli_errstr(cli));
+
+       status = cli_getatr(cli, fname, NULL, NULL, &t);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("getatr failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -3016,13 +3190,15 @@ static bool run_attrtest(int dummy)
 
        t2 = t-60*60*24; /* 1 day ago */
 
-       if (!NT_STATUS_IS_OK(cli_setatr(cli, fname, 0, t2))) {
-               printf("setatr failed (%s)\n", cli_errstr(cli));
+       status = cli_setatr(cli, fname, 0, t2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("setatr failed (%s)\n", nt_errstr(status));
                correct = True;
        }
 
-       if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
-               printf("getatr failed (%s)\n", cli_errstr(cli));
+       status = cli_getatr(cli, fname, NULL, NULL, &t);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("getatr failed (%s)\n", nt_errstr(status));
                correct = True;
        }
 
@@ -3033,7 +3209,7 @@ static bool run_attrtest(int dummy)
                correct = True;
        }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        if (!torture_close_connection(cli)) {
                correct = False;
@@ -3052,13 +3228,13 @@ static bool run_trans2test(int dummy)
 {
        struct cli_state *cli;
        uint16_t fnum;
-       SMB_OFF_T size;
+       off_t size;
        time_t c_time, a_time, m_time;
        struct timespec c_time_ts, a_time_ts, m_time_ts, w_time_ts, m_time2_ts;
        const char *fname = "\\trans2.tst";
        const char *dname = "\\trans2";
        const char *fname2 = "\\trans2\\trans2.tst";
-       char pname[1024];
+       char *pname;
        bool correct = True;
        NTSTATUS status;
        uint32_t fs_attr;
@@ -3076,19 +3252,18 @@ static bool run_trans2test(int dummy)
                correct = false;
        }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       cli_open(cli, fname, 
-                       O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
-       if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
-                                    cli, fnum, NULL, &size, &c_time_ts,
-                                    &a_time_ts, &w_time_ts,
-                                    &m_time_ts, NULL))) {
-               printf("ERROR: qfileinfo failed (%s)\n", cli_errstr(cli));
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_openx(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
+       status = cli_qfileinfo_basic(cli, fnum, NULL, &size, &c_time_ts,
+                                    &a_time_ts, &w_time_ts, &m_time_ts, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("ERROR: qfileinfo failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_qfilename(cli, fnum, pname, sizeof(pname)))) {
-               printf("ERROR: qfilename failed (%s)\n", cli_errstr(cli));
+       status = cli_qfilename(cli, fnum, talloc_tos(), &pname);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("ERROR: qfilename failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -3102,10 +3277,11 @@ static bool run_trans2test(int dummy)
 
        sleep(2);
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, 
-                       O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       status = cli_openx(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE,
+                         &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
        cli_close(cli, fnum);
@@ -3116,26 +3292,28 @@ static bool run_trans2test(int dummy)
                printf("ERROR: qpathinfo failed (%s)\n", nt_errstr(status));
                correct = False;
        } else {
+               time_t t = time(NULL);
+
                if (c_time != m_time) {
                        printf("create time=%s", ctime(&c_time));
                        printf("modify time=%s", ctime(&m_time));
                        printf("This system appears to have sticky create times\n");
                }
-               if (a_time % (60*60) == 0) {
+               if ((abs(a_time - t) > 60) && (a_time % (60*60) == 0)) {
                        printf("access time=%s", ctime(&a_time));
                        printf("This system appears to set a midnight access time\n");
                        correct = False;
                }
 
-               if (abs(m_time - time(NULL)) > 60*60*24*7) {
+               if (abs(m_time - t) > 60*60*24*7) {
                        printf("ERROR: totally incorrect times - maybe word reversed? mtime=%s", ctime(&m_time));
                        correct = False;
                }
        }
 
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       cli_open(cli, fname, 
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_openx(cli, fname, 
                        O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
        cli_close(cli, fnum);
        status = cli_qpathinfo2(cli, fname, &c_time_ts, &a_time_ts, &w_time_ts,
@@ -3151,13 +3329,14 @@ static bool run_trans2test(int dummy)
                }
        }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
 
        /* check if the server updates the directory modification time
            when creating a new file */
-       if (!NT_STATUS_IS_OK(cli_mkdir(cli, dname))) {
-               printf("ERROR: mkdir failed (%s)\n", cli_errstr(cli));
+       status = cli_mkdir(cli, dname);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("ERROR: mkdir failed (%s)\n", nt_errstr(status));
                correct = False;
        }
        sleep(3);
@@ -3168,7 +3347,7 @@ static bool run_trans2test(int dummy)
                correct = False;
        }
 
-       cli_open(cli, fname2, 
+       cli_openx(cli, fname2, 
                        O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
        cli_writeall(cli, fnum,  0, (uint8_t *)&fnum, 0, sizeof(fnum), NULL);
        cli_close(cli, fnum);
@@ -3184,7 +3363,7 @@ static bool run_trans2test(int dummy)
                        correct = False;
                }
        }
-       cli_unlink(cli, fname2, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_rmdir(cli, dname);
 
        if (!torture_close_connection(cli)) {
@@ -3207,7 +3386,7 @@ static NTSTATUS new_trans(struct cli_state *pcli, int fnum, int level)
        NTSTATUS status;
 
        status = cli_qfileinfo(talloc_tos(), pcli, fnum, level, 0,
-                              pcli->max_xmit, &buf, &len);
+                              CLI_BUFFER_SIZE, NULL, &buf, &len);
        if (!NT_STATUS_IS_OK(status)) {
                printf("ERROR: qfileinfo (%d) failed (%s)\n", level,
                       nt_errstr(status));
@@ -3234,7 +3413,7 @@ static bool run_w2ktest(int dummy)
                return False;
        }
 
-       cli_open(cli, fname, 
+       cli_openx(cli, fname, 
                        O_RDWR | O_CREAT , DENY_NONE, &fnum);
 
        for (level = 1004; level < 1040; level++) {
@@ -3262,6 +3441,7 @@ static bool run_oplock1(int dummy)
        const char *fname = "\\lockt1.lck";
        uint16_t fnum1;
        bool correct = True;
+       NTSTATUS status;
 
        printf("starting oplock test 1\n");
 
@@ -3269,29 +3449,33 @@ static bool run_oplock1(int dummy)
                return False;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        cli1->use_oplocks = True;
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+                         &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        cli1->use_oplocks = False;
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(cli1));
+       status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                return False;
        }
 
@@ -3313,8 +3497,10 @@ static bool run_oplock2(int dummy)
        char buf[4];
        bool correct = True;
        volatile bool *shared_correct;
+       size_t nread;
+       NTSTATUS status;
 
-       shared_correct = (volatile bool *)shm_setup(sizeof(bool));
+       shared_correct = (volatile bool *)anonymous_shared_allocate(sizeof(bool));
        *shared_correct = True;
 
        use_level_II_oplocks = True;
@@ -3328,25 +3514,21 @@ static bool run_oplock2(int dummy)
                return False;
        }
 
-       cli1->use_oplocks = True;
-       cli1->use_level_II_oplocks = True;
-
        if (!torture_open_connection(&cli2, 1)) {
                use_level_II_oplocks = False;
                use_oplocks = saved_use_oplocks;
                return False;
        }
 
-       cli2->use_oplocks = True;
-       cli2->use_level_II_oplocks = True;
-
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+                         &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
@@ -3356,16 +3538,18 @@ static bool run_oplock2(int dummy)
 
        if (fork() == 0) {
                /* Child code */
-               if (!NT_STATUS_IS_OK(cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2))) {
-                       printf("second open of %s failed (%s)\n", fname, cli_errstr(cli1));
+               status = cli_openx(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("second open of %s failed (%s)\n", fname, nt_errstr(status));
                        *shared_correct = False;
                        exit(0);
                }
 
                sleep(2);
 
-               if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-                       printf("close2 failed (%s)\n", cli_errstr(cli1));
+               status = cli_close(cli2, fnum2);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("close2 failed (%s)\n", nt_errstr(status));
                        *shared_correct = False;
                }
 
@@ -3376,17 +3560,21 @@ static bool run_oplock2(int dummy)
 
        /* Ensure cli1 processes the break. Empty file should always return 0
         * bytes.  */
-
-       if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
-               printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
-               correct = False;
+       status = cli_read(cli1, fnum1, buf, 0, 4, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("read on fnum1 failed (%s)\n", nt_errstr(status));
+               correct = false;
+       } else if (nread != 0) {
+               printf("read on empty fnum1 failed. recv %ld expected %d\n",
+                     (unsigned long)nread, 0);
+               correct = false;
        }
 
        /* Should now be at level II. */
        /* Test if sending a write locks causes a break to none. */
-
-       if (!cli_lock(cli1, fnum1, 0, 4, 0, READ_LOCK)) {
-               printf("lock failed (%s)\n", cli_errstr(cli1));
+       status = cli_lock32(cli1, fnum1, 0, 4, 0, READ_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("lock failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -3394,8 +3582,9 @@ static bool run_oplock2(int dummy)
 
        sleep(2);
 
-       if (!cli_lock(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) {
-               printf("lock failed (%s)\n", cli_errstr(cli1));
+       status = cli_lock32(cli1, fnum1, 0, 4, 0, WRITE_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("lock failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -3403,17 +3592,19 @@ static bool run_oplock2(int dummy)
 
        sleep(2);
 
-       cli_read(cli1, fnum1, buf, 0, 4);
+       cli_read(cli1, fnum1, buf, 0, 4, NULL);
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
        sleep(4);
 
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(cli1));
+       status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                correct = False;
        }
 
@@ -3430,78 +3621,21 @@ static bool run_oplock2(int dummy)
        return correct;
 }
 
-/* handler for oplock 3 tests */
-static NTSTATUS oplock3_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
-{
-       printf("got oplock break fnum=%d level=%d\n",
-              fnum, level);
-       return cli_oplock_ack(cli, fnum, level);
-}
-
-static bool run_oplock3(int dummy)
-{
+struct oplock4_state {
+       struct tevent_context *ev;
        struct cli_state *cli;
-       const char *fname = "\\oplockt3.dat";
-       uint16_t fnum;
-       char buf[4] = "abcd";
-       bool correct = True;
-       volatile bool *shared_correct;
-
-       shared_correct = (volatile bool *)shm_setup(sizeof(bool));
-       *shared_correct = True;
-
-       printf("starting oplock test 3\n");
-
-       if (fork() == 0) {
-               /* Child code */
-               use_oplocks = True;
-               use_level_II_oplocks = True;
-               if (!torture_open_connection(&cli, 0)) {
-                       *shared_correct = False;
-                       exit(0);
-               } 
-               sleep(2);
-               /* try to trigger a oplock break in parent */
-               cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
-               cli_writeall(cli, fnum, 0, (uint8_t *)buf, 0, 4, NULL);
-               exit(0);
-       }
-
-       /* parent code */
-       use_oplocks = True;
-       use_level_II_oplocks = True;
-       if (!torture_open_connection(&cli, 1)) { /* other is forked */
-               return False;
-       }
-       cli_oplock_handler(cli, oplock3_handler);
-       cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
-       cli_writeall(cli, fnum, 0, (uint8_t *)buf, 0, 4, NULL);
-       cli_close(cli, fnum);
-       cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum);
-       cli->timeout = 20000;
-       cli_receive_smb(cli);
-       printf("finished oplock test 3\n");
-
-       return (correct && *shared_correct);
-
-/* What are we looking for here?  What's sucess and what's FAILURE? */
-}
-
-/* handler for oplock 4 tests */
-bool *oplock4_shared_correct;
+       bool *got_break;
+       uint16_t *fnum2;
+};
 
-static NTSTATUS oplock4_handler(struct cli_state *cli, uint16_t fnum, unsigned char level)
-{
-       printf("got oplock break fnum=%d level=%d\n",
-              fnum, level);
-       *oplock4_shared_correct = true;
-       cli_oplock_ack(cli, fnum, level);
-       return NT_STATUS_UNSUCCESSFUL; /* Cause cli_receive_smb to return. */
-}
+static void oplock4_got_break(struct tevent_req *req);
+static void oplock4_got_open(struct tevent_req *req);
 
 static bool run_oplock4(int dummy)
 {
+       struct tevent_context *ev;
        struct cli_state *cli1, *cli2;
+       struct tevent_req *oplock_req, *open_req;
        const char *fname = "\\lockt4.lck";
        const char *fname_ln = "\\lockt4_ln.lck";
        uint16_t fnum1, fnum2;
@@ -3509,8 +3643,9 @@ static bool run_oplock4(int dummy)
        NTSTATUS status;
        bool correct = true;
 
-       oplock4_shared_correct = (bool *)shm_setup(sizeof(bool));
-       *oplock4_shared_correct = false;
+       bool got_break;
+
+       struct oplock4_state *state;
 
        printf("starting oplock test 4\n");
 
@@ -3526,36 +3661,41 @@ static bool run_oplock4(int dummy)
                return false;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname_ln, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        /* Create the file. */
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+                         &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return false;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                return false;
        }
 
        /* Now create a hardlink. */
-       if (!NT_STATUS_IS_OK(cli_nt_hardlink(cli1, fname, fname_ln))) {
-               printf("nt hardlink failed (%s)\n", cli_errstr(cli1));
+       status = cli_nt_hardlink(cli1, fname, fname_ln);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("nt hardlink failed (%s)\n", nt_errstr(status));
                return false;
        }
 
        /* Prove that opening hardlinks cause deny modes to conflict. */
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR, DENY_ALL, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return false;
        }
 
-       status = cli_open(cli1, fname_ln, O_RDWR, DENY_NONE, &fnum2);
+       status = cli_openx(cli1, fname_ln, O_RDWR, DENY_NONE, &fnum2);
        if (NT_STATUS_IS_OK(status)) {
                printf("open of %s succeeded - should fail with sharing violation.\n",
                        fname_ln);
@@ -3568,55 +3708,87 @@ static bool run_oplock4(int dummy)
                return false;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                return false;
        }
 
        cli1->use_oplocks = true;
-       cli1->use_level_II_oplocks = true;
-
        cli2->use_oplocks = true;
-       cli2->use_level_II_oplocks = true;
 
-       cli_oplock_handler(cli1, oplock4_handler);
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return false;
        }
 
-       if (fork() == 0) {
-               /* Child code */
-               if (!NT_STATUS_IS_OK(cli_open(cli2, fname_ln, O_RDWR, DENY_NONE, &fnum2))) {
-                       printf("open of %s failed (%s)\n", fname_ln, cli_errstr(cli1));
-                       *oplock4_shared_correct = false;
-                       exit(0);
-               }
+       ev = tevent_context_init(talloc_tos());
+       if (ev == NULL) {
+               printf("tevent_context_init failed\n");
+               return false;
+       }
 
-               if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-                       printf("close2 failed (%s)\n", cli_errstr(cli1));
-                       *oplock4_shared_correct = false;
-               }
+       state = talloc(ev, struct oplock4_state);
+       if (state == NULL) {
+               printf("talloc failed\n");
+               return false;
+       }
+       state->ev = ev;
+       state->cli = cli1;
+       state->got_break = &got_break;
+       state->fnum2 = &fnum2;
+
+       oplock_req = cli_smb_oplock_break_waiter_send(
+               talloc_tos(), ev, cli1);
+       if (oplock_req == NULL) {
+               printf("cli_smb_oplock_break_waiter_send failed\n");
+               return false;
+       }
+       tevent_req_set_callback(oplock_req, oplock4_got_break, state);
 
-               exit(0);
+       open_req = cli_openx_send(
+               talloc_tos(), ev, cli2, fname_ln, O_RDWR, DENY_NONE);
+       if (open_req == NULL) {
+               printf("cli_openx_send failed\n");
+               return false;
        }
+       tevent_req_set_callback(open_req, oplock4_got_open, state);
 
-       sleep(2);
+       got_break = false;
+       fnum2 = 0xffff;
+
+       while (!got_break || fnum2 == 0xffff) {
+               int ret;
+               ret = tevent_loop_once(ev);
+               if (ret == -1) {
+                       printf("tevent_loop_once failed: %s\n",
+                              strerror(errno));
+                       return false;
+               }
+       }
 
-       /* Process the oplock break. */
-       cli_receive_smb(cli1);
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
+               correct = false;
+       }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                correct = false;
        }
 
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(cli1));
+       status = cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                correct = false;
        }
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, fname_ln, aSYSTEM | aHIDDEN))) {
-               printf("unlink failed (%s)\n", cli_errstr(cli1));
+
+       status = cli_unlink(cli1, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink failed (%s)\n", nt_errstr(status));
                correct = false;
        }
 
@@ -3624,7 +3796,7 @@ static bool run_oplock4(int dummy)
                correct = false;
        }
 
-       if (!*oplock4_shared_correct) {
+       if (!got_break) {
                correct = false;
        }
 
@@ -3633,6 +3805,43 @@ static bool run_oplock4(int dummy)
        return correct;
 }
 
+static void oplock4_got_break(struct tevent_req *req)
+{
+       struct oplock4_state *state = tevent_req_callback_data(
+               req, struct oplock4_state);
+       uint16_t fnum;
+       uint8_t level;
+       NTSTATUS status;
+
+       status = cli_smb_oplock_break_waiter_recv(req, &fnum, &level);
+       TALLOC_FREE(req);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("cli_smb_oplock_break_waiter_recv returned %s\n",
+                      nt_errstr(status));
+               return;
+       }
+       *state->got_break = true;
+
+       req = cli_oplock_ack_send(state, state->ev, state->cli, fnum,
+                                 NO_OPLOCK);
+       if (req == NULL) {
+               printf("cli_oplock_ack_send failed\n");
+               return;
+       }
+}
+
+static void oplock4_got_open(struct tevent_req *req)
+{
+       struct oplock4_state *state = tevent_req_callback_data(
+               req, struct oplock4_state);
+       NTSTATUS status;
+
+       status = cli_openx_recv(req, state->fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("cli_openx_recv returned %s\n", nt_errstr(status));
+               *state->fnum2 = 0xffff;
+       }
+}
 
 /*
   Test delete on close semantics.
@@ -3645,6 +3854,7 @@ static bool run_deletetest(int dummy)
        uint16_t fnum1 = (uint16_t)-1;
        uint16_t fnum2 = (uint16_t)-1;
        bool correct = True;
+       NTSTATUS status;
 
        printf("starting delete test\n");
 
@@ -3652,28 +3862,30 @@ static bool run_deletetest(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        /* Test 1 - this should delete the file on close. */
 
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  0, FILE_OVERWRITE_IF, 
-                                  FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
-               printf("[1] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                             FILE_DELETE_ON_CLOSE, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[1] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[1] close failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[1] close failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
+       if (NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
                printf("[1] open of %s succeeded (should fail)\n", fname);
                correct = False;
                goto fail;
@@ -3684,46 +3896,53 @@ static bool run_deletetest(int dummy)
        /* Test 2 - this should delete the file on close. */
 
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
-                                  FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, 
-                                  FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("[2] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[2] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
-               printf("[2] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[2] setting delete_on_close failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[2] close failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[2] close failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
+       if (NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
                printf("[2] open of %s succeeded should have been deleted on close !\n", fname);
-               if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-                       printf("[2] close failed (%s)\n", cli_errstr(cli1));
+               status = cli_close(cli1, fnum1);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("[2] close failed (%s)\n", nt_errstr(status));
                        correct = False;
                        goto fail;
                }
-               cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+               cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        } else
                printf("second delete on close test succeeded.\n");
 
        /* Test 3 - ... */
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("[3] open - 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[3] open - 1 of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3731,48 +3950,58 @@ static bool run_deletetest(int dummy)
        /* This should fail with a sharing violation - open for delete is only compatible
           with SHARE_DELETE. */
 
-       if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                       FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE,
+                             FILE_OPEN, 0, 0, &fnum2);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[3] open  - 2 of %s succeeded - should have failed.\n", fname);
                correct = False;
                goto fail;
        }
 
        /* This should succeed. */
-
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
-               printf("[3] open  - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                            FILE_ATTRIBUTE_NORMAL,
+                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                            FILE_OPEN, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[3] open  - 2 of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
-               printf("[3] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[3] setting delete_on_close failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[3] close 1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[3] close 1 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
-               printf("[3] close 2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[3] close 2 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* This should fail - file should no longer be there. */
 
-       if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
-               if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-                       printf("[3] close failed (%s)\n", cli_errstr(cli1));
+               status = cli_close(cli1, fnum1);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("[3] close failed (%s)\n", nt_errstr(status));
                }
-               cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+               cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
                correct = False;
                goto fail;
        } else
@@ -3780,31 +4009,40 @@ static bool run_deletetest(int dummy)
 
        /* Test 4 ... */
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
-                       FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("[4] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0,
+                             FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[4] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* This should succeed. */
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
-                       FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 0, 0, &fnum2))) {
-               printf("[4] open  - 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                            FILE_ATTRIBUTE_NORMAL,
+                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                            FILE_OPEN, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[4] open  - 2 of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum2))) {
-               printf("[4] close - 1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[4] close - 1 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
-               printf("[4] setting delete_on_close failed (%s)\n", cli_errstr(cli1));
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[4] setting delete_on_close failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3819,18 +4057,20 @@ static bool run_deletetest(int dummy)
        } else
                printf("fourth delete on close test succeeded.\n");
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[4] close - 2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[4] close - 2 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* Test 5 ... */
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1))) {
-               printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3843,8 +4083,9 @@ static bool run_deletetest(int dummy)
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[5] close - 2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[5] close - 2 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3853,12 +4094,15 @@ static bool run_deletetest(int dummy)
 
        /* Test 6 ... */
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
-                                  FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                                  FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("[6] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
+                            FILE_ATTRIBUTE_NORMAL,
+                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                            FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[6] open of %s failed (%s)\n", fname,
+                      nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3871,8 +4115,9 @@ static bool run_deletetest(int dummy)
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[6] close - 2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[6] close - 2 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3881,11 +4126,14 @@ static bool run_deletetest(int dummy)
 
        /* Test 7 ... */
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
-                                  FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("[7] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0,
+                             FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                             0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[7] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3902,22 +4150,24 @@ static bool run_deletetest(int dummy)
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* This next open should succeed - we reset the flag. */
-
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
-               printf("[5] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[7] close - 2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3926,7 +4176,7 @@ static bool run_deletetest(int dummy)
 
        /* Test 7 ... */
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        if (!torture_open_connection(&cli2, 1)) {
                printf("[8] failed to open second connection.\n");
@@ -3934,20 +4184,26 @@ static bool run_deletetest(int dummy)
                goto fail;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
-                                  FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                                  FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("[8] open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0,
+                            FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
+                            FILE_ATTRIBUTE_NORMAL,
+                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                            FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[8] open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
-                                  FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                                  FILE_OPEN, 0, 0, &fnum2))) {
-               printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0,
+                            FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
+                            FILE_ATTRIBUTE_NORMAL,
+                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                            FILE_OPEN, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[8] open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
@@ -3958,20 +4214,23 @@ static bool run_deletetest(int dummy)
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[8] close - 1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[8] close - 1 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-               printf("[8] close - 2 failed (%s)\n", cli_errstr(cli2));
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[8] close - 2 failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* This should fail.. */
-       if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
                goto fail;
                correct = False;
@@ -3988,22 +4247,27 @@ static bool run_deletetest(int dummy)
 
        printf("ninth delete on close test succeeded.\n");
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
-                                  FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
-               printf("[10] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0,
+                            FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
+                            FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                            FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE,
+                            0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[10] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* This should delete the file. */
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[10] close failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[10] close failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* This should fail.. */
-       if (NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
+       if (NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
                printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
                goto fail;
                correct = False;
@@ -4011,37 +4275,42 @@ static bool run_deletetest(int dummy)
                printf("tenth delete on close test succeeded.\n");
 
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        /* What error do we get when attempting to open a read-only file with
           delete access ? */
 
        /* Create a readonly file. */
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
-                                  FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("[11] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
+                             FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[11] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("[11] close failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[11] close failed (%s)\n", nt_errstr(status));
                correct = False;
                goto fail;
        }
 
        /* Now try open for delete access. */
-       if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
-                                  0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                                  FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
+       status = cli_ntcreate(cli1, fname, 0,
+                            FILE_READ_ATTRIBUTES|DELETE_ACCESS,
+                            0,
+                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                            FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
                cli_close(cli1, fnum1);
                goto fail;
                correct = False;
        } else {
-               NTSTATUS nterr = cli_nt_error(cli1);
-               if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
-                       printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
+               if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
+                       printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(status));
                        goto fail;
                        correct = False;
                } else {
@@ -4059,7 +4328,7 @@ static bool run_deletetest(int dummy)
        if (fnum1 != (uint16_t)-1) cli_close(cli1, fnum1);
        if (fnum2 != (uint16_t)-1) cli_close(cli1, fnum2);
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        if (cli1 && !torture_close_connection(cli1)) {
                correct = False;
@@ -4087,25 +4356,28 @@ static bool run_deletetest_ln(int dummy)
                return false;
        }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli, fname_ln, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        /* Create the file. */
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return false;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
-               printf("close1 failed (%s)\n", cli_errstr(cli));
+       status = cli_close(cli, fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close1 failed (%s)\n", nt_errstr(status));
                return false;
        }
 
        /* Now create a hardlink. */
-       if (!NT_STATUS_IS_OK(cli_nt_hardlink(cli, fname, fname_ln))) {
-               printf("nt hardlink failed (%s)\n", cli_errstr(cli));
+       status = cli_nt_hardlink(cli, fname, fname_ln);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("nt hardlink failed (%s)\n", nt_errstr(status));
                return false;
        }
 
@@ -4169,8 +4441,8 @@ static bool run_deletetest_ln(int dummy)
                 correct = False;
         }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli, fname_ln, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli, fname_ln, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        if (!torture_close_connection(cli)) {
                correct = false;
@@ -4197,9 +4469,9 @@ static bool run_properties(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
-       d_printf("Capabilities 0x%08x\n", cli->capabilities);
+       d_printf("Capabilities 0x%08x\n", smb1cli_conn_capabilities(cli->conn));
 
        if (!torture_close_connection(cli)) {
                correct = False;
@@ -4238,6 +4510,7 @@ static bool run_xcopy(int dummy)
        const char *fname = "\\test.txt";
        bool correct = True;
        uint16_t fnum1, fnum2;
+       NTSTATUS status;
 
        printf("starting xcopy test\n");
 
@@ -4245,19 +4518,19 @@ static bool run_xcopy(int dummy)
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
-                                  FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 
-                                  0x4044, 0, &fnum1))) {
-               printf("First open failed - %s\n", cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, FIRST_DESIRED_ACCESS,
+                             FILE_ATTRIBUTE_ARCHIVE, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0x4044, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("First open failed - %s\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,
-                                  SECOND_DESIRED_ACCESS, 0,
-                                  FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OPEN, 
-                                  0x200000, 0, &fnum2))) {
-               printf("second open failed - %s\n", cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, SECOND_DESIRED_ACCESS, 0,
+                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                            FILE_OPEN, 0x200000, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("second open failed - %s\n", nt_errstr(status));
                return False;
        }
 
@@ -4287,28 +4560,33 @@ static bool run_rename(int dummy)
                return False;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("First open failed - %s\n", cli_errstr(cli1));
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("First open failed - %s\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
-               printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
+       status = cli_rename(cli1, fname, fname1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("First rename failed (SHARE_READ) (this is correct) - %s\n", nt_errstr(status));
        } else {
                printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close - 1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close - 1 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
 #if 0
                              FILE_SHARE_DELETE|FILE_SHARE_NONE,
@@ -4317,28 +4595,32 @@ static bool run_rename(int dummy)
 #endif
                              FILE_OVERWRITE_IF, 0, 0, &fnum1);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("Second open failed - %s\n", cli_errstr(cli1));
+               printf("Second open failed - %s\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
-               printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
+       status = cli_rename(cli1, fname, fname1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", nt_errstr(status));
                correct = False;
        } else {
                printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close - 2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close - 2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("Third open failed - %s\n", cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Third open failed - %s\n", nt_errstr(status));
                return False;
        }
 
@@ -4364,58 +4646,68 @@ static bool run_rename(int dummy)
   }
 #endif
 
-       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
-               printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", cli_errstr(cli1));
+       status = cli_rename(cli1, fname, fname1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", nt_errstr(status));
                correct = False;
        } else {
                printf("Third rename succeeded (SHARE_NONE)\n");
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close - 3 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close - 3 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
         /*----*/
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("Fourth open failed - %s\n", cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Fourth open failed - %s\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
-               printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", cli_errstr(cli1));
+       status = cli_rename(cli1, fname, fname1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", nt_errstr(status));
        } else {
                printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close - 4 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close - 4 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
         /*--*/
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("Fifth open failed - %s\n", cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                        FILE_ATTRIBUTE_NORMAL,
+                        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+                        FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Fifth open failed - %s\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
-               printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n",
-                       cli_errstr(cli1));
+       status = cli_rename(cli1, fname, fname1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n", nt_errstr(status));
                correct = False;
        } else {
-               printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", cli_errstr(cli1));
+               printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", nt_errstr(status));
        }
 
         /*
@@ -4433,16 +4725,17 @@ static bool run_rename(int dummy)
           } */
 
         /*--*/
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close - 5 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close - 5 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
        /* Check that the renamed file has FILE_ATTRIBUTE_ARCHIVE. */
-       if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname1, &attr, NULL, NULL))) {
+       status = cli_getatr(cli1, fname1, &attr, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("getatr on file %s failed - %s ! \n",
-                       fname1,
-                       cli_errstr(cli1));
+                       fname1, nt_errstr(status));
                correct = False;
        } else {
                if (attr != FILE_ATTRIBUTE_ARCHIVE) {
@@ -4457,8 +4750,8 @@ static bool run_rename(int dummy)
                }
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-       cli_unlink(cli1, fname1, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        if (!torture_close_connection(cli1)) {
                correct = False;
@@ -4473,17 +4766,21 @@ static bool run_pipe_number(int dummy)
        const char *pipe_name = "\\SPOOLSS";
        uint16_t fnum;
        int num_pipes = 0;
+       NTSTATUS status;
 
        printf("starting pipenumber test\n");
        if (!torture_open_connection(&cli1, 0)) {
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
        while(1) {
-               if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0, &fnum))) {
-                       printf("Open of pipe %s failed with error (%s)\n", pipe_name, cli_errstr(cli1));
+               status = cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA,
+                                     FILE_ATTRIBUTE_NORMAL,
+                                     FILE_SHARE_READ|FILE_SHARE_WRITE,
+                                     FILE_OPEN_IF, 0, 0, &fnum);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("Open of pipe %s failed with error (%s)\n", pipe_name, nt_errstr(status));
                        break;
                }
                num_pipes++;
@@ -4505,7 +4802,7 @@ static bool run_opentest(int dummy)
        const char *fname = "\\readonly.file";
        uint16_t fnum1, fnum2;
        char buf[20];
-       SMB_OFF_T fsize;
+       off_t fsize;
        bool correct = True;
        char *tmp_path;
        NTSTATUS status;
@@ -4517,34 +4814,38 @@ static bool run_opentest(int dummy)
        }
 
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_setatr(cli1, fname, aRONLY, 0))) {
-               printf("cli_setatr failed (%s)\n", cli_errstr(cli1));
+       status = cli_setatr(cli1, fname, FILE_ATTRIBUTE_READONLY, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("cli_setatr failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        /* This will fail - but the error should be ERRnoaccess, not ERRbadshare. */
-       cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
+       status = cli_openx(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
 
-        if (check_error(__LINE__, cli1, ERRDOS, ERRnoaccess, 
+        if (check_error(__LINE__, status, ERRDOS, ERRnoaccess,
                        NT_STATUS_ACCESS_DENIED)) {
                printf("correct error code ERRDOS/ERRnoaccess returned\n");
        }
@@ -4557,32 +4858,34 @@ static bool run_opentest(int dummy)
 
        cli_setatr(cli1, fname, 0, 0);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        /* This will fail - but the error should be ERRshare. */
-       cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
+       status = cli_openx(cli1, fname, O_RDWR, DENY_ALL, &fnum2);
 
-       if (check_error(__LINE__, cli1, ERRDOS, ERRbadshare, 
+       if (check_error(__LINE__, status, ERRDOS, ERRbadshare,
                        NT_STATUS_SHARING_VIOLATION)) {
                printf("correct error code ERRDOS/ERRbadshare returned\n");
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("finished open test 2\n");
 
        /* Test truncate open disposition on file opened for read. */
-
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1))) {
-               printf("(3) open (1) of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("(3) open (1) of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
@@ -4596,14 +4899,16 @@ static bool run_opentest(int dummy)
                correct = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("(3) close1 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("(3) close1 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
        /* Ensure size == 20. */
-       if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
-               printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
+       status = cli_getatr(cli1, fname, NULL, &fsize, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("(3) getatr failed (%s)\n", nt_errstr(status));
                return False;
        }
 
@@ -4613,20 +4918,22 @@ static bool run_opentest(int dummy)
        }
 
        /* Now test if we can truncate a file opened for readonly. */
-
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1))) {
-               printf("(3) open (2) of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("(3) open (2) of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close2 failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close2 failed (%s)\n", nt_errstr(status));
                return False;
        }
 
        /* Ensure size == 0. */
-       if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
-               printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
+       status = cli_getatr(cli1, fname, NULL, &fsize, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("(3) getatr failed (%s)\n", nt_errstr(status));
                return False;
        }
 
@@ -4636,19 +4943,24 @@ static bool run_opentest(int dummy)
        }
        printf("finished open test 3\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("Do ctemp tests\n");
-       if (!NT_STATUS_IS_OK(cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path))) {
-               printf("ctemp failed (%s)\n", cli_errstr(cli1));
+       status = cli_ctemp(cli1, talloc_tos(), "\\", &fnum1, &tmp_path);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("ctemp failed (%s)\n", nt_errstr(status));
                return False;
        }
+
        printf("ctemp gave path %s\n", tmp_path);
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close of temp failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close of temp failed (%s)\n", nt_errstr(status));
        }
-       if (!NT_STATUS_IS_OK(cli_unlink(cli1, tmp_path, aSYSTEM | aHIDDEN))) {
-               printf("unlink of temp failed (%s)\n", cli_errstr(cli1));
+
+       status = cli_unlink(cli1, tmp_path, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("unlink of temp failed (%s)\n", nt_errstr(status));
        }
 
        /* Test the non-io opens... */
@@ -4658,137 +4970,174 @@ static bool run_opentest(int dummy)
        }
 
        cli_setatr(cli2, fname, 0, 0);
-       cli_unlink(cli2, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli2, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("TEST #1 testing 2 non-io opens (no delete)\n");
-
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("TEST #1 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #1 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
-               printf("TEST #1 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OPEN_IF, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #1 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("TEST #1 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #1 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-               printf("TEST #1 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #1 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        printf("non-io open test #1 passed.\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("TEST #2 testing 2 non-io opens (first with delete)\n");
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("TEST #2 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0,
+                             DELETE_ACCESS|FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #2 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
-               printf("TEST #2 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OPEN_IF, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #2 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("TEST #2 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #2 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-               printf("TEST #2 close 2 of %s failed (%s)\n", fname, cli_errstr(cli1));
+
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #2 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        printf("non-io open test #2 passed.\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("TEST #3 testing 2 non-io opens (second with delete)\n");
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("TEST #3 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #3 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
-               printf("TEST #3 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0,
+                             DELETE_ACCESS|FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OPEN_IF, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #3 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("TEST #3 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #3 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-               printf("TEST #3 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #3 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        printf("non-io open test #3 passed.\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("TEST #4 testing 2 non-io opens (both with delete)\n");
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("TEST #4 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0,
+                              DELETE_ACCESS|FILE_READ_ATTRIBUTES,
+                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                              FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #4 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OPEN_IF, 0, 0, &fnum2))) {
-               printf("TEST #4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0,
+                             DELETE_ACCESS|FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OPEN_IF, 0, 0, &fnum2);
+       if (NT_STATUS_IS_OK(status)) {
+               printf("TEST #4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       printf("TEST #4 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
+       printf("TEST #4 open 2 of %s gave %s (correct error should be %s)\n", fname, nt_errstr(status), "sharing violation");
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("TEST #4 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #4 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        printf("non-io open test #4 passed.\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("TEST #5 testing 2 non-io opens (both with delete - both with file share delete)\n");
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("TEST #5 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0,
+                             DELETE_ACCESS|FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #5 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
-               printf("TEST #5 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0,
+                             DELETE_ACCESS|FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
+                             FILE_OPEN_IF, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #5 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("TEST #5 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #5 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-               printf("TEST #5 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #5 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
@@ -4796,27 +5145,33 @@ static bool run_opentest(int dummy)
 
        printf("TEST #6 testing 1 non-io open, one io open\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("TEST #6 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #6 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ, FILE_OPEN_IF, 0, 0, &fnum2))) {
-               printf("TEST #6 open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
+                             FILE_OPEN_IF, 0, 0, &fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #6 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("TEST #6 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #6 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli2, fnum2))) {
-               printf("TEST #6 close 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_close(cli2, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #6 close 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
@@ -4824,30 +5179,37 @@ static bool run_opentest(int dummy)
 
        printf("TEST #7 testing 1 non-io open, one io open with delete\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-               printf("TEST #7 open 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
+                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #7 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       if (NT_STATUS_IS_OK(cli_ntcreate(cli2, fname, 0, DELETE_ACCESS|FILE_READ_ATTRIBUTES, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ|FILE_SHARE_DELETE, FILE_OPEN_IF, 0, 0, &fnum2))) {
-               printf("TEST #7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, cli_errstr(cli2));
+       status = cli_ntcreate(cli2, fname, 0,
+                             DELETE_ACCESS|FILE_READ_ATTRIBUTES,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_DELETE,
+                             FILE_OPEN_IF, 0, 0, &fnum2);
+       if (NT_STATUS_IS_OK(status)) {
+               printf("TEST #7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
-       printf("TEST #7 open 2 of %s gave %s (correct error should be %s)\n", fname, cli_errstr(cli2), "sharing violation");
+       printf("TEST #7 open 2 of %s gave %s (correct error should be %s)\n", fname, nt_errstr(status), "sharing violation");
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("TEST #7 close 1 of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("TEST #7 close 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
        printf("non-io open test #7 passed.\n");
 
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("TEST #8 testing open without WRITE_ATTRIBUTES, updating close write time.\n");
        status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
@@ -4860,7 +5222,7 @@ static bool run_opentest(int dummy)
        }
 
        /* Write to ensure we have to update the file time. */
-       status = cli_writeall(cli1, fnum1, 0, (uint8_t *)"TEST DATA\n", 0, 10,
+       status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"TEST DATA\n", 0, 10,
                              NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #8 cli_write failed: %s\n", nt_errstr(status));
@@ -4932,6 +5294,7 @@ static bool run_simple_posix_open_test(int dummy)
        SMB_STRUCT_STAT sbuf;
        bool correct = false;
        NTSTATUS status;
+       size_t nread;
 
        printf("Starting simple POSIX open test\n");
 
@@ -4939,7 +5302,7 @@ static bool run_simple_posix_open_test(int dummy)
                return false;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        status = torture_setup_unix_extensions(cli1);
        if (!NT_STATUS_IS_OK(status)) {
@@ -4956,25 +5319,30 @@ static bool run_simple_posix_open_test(int dummy)
        cli_posix_unlink(cli1, sname);
 
        /* Create a directory. */
-       if (!NT_STATUS_IS_OK(cli_posix_mkdir(cli1, dname, 0777))) {
-               printf("POSIX mkdir of %s failed (%s)\n", dname, cli_errstr(cli1));
+       status = cli_posix_mkdir(cli1, dname, 0777);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX mkdir of %s failed (%s)\n", dname, nt_errstr(status));
                goto out;
        }
 
-       if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
-               printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL,
+                               0600, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
                goto out;
        }
 
        /* Test ftruncate - set file size. */
-       if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 1000))) {
-               printf("ftruncate failed (%s)\n", cli_errstr(cli1));
+       status = cli_ftruncate(cli1, fnum1, 1000);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("ftruncate failed (%s)\n", nt_errstr(status));
                goto out;
        }
 
        /* Ensure st_size == 1000 */
-       if (!NT_STATUS_IS_OK(cli_posix_stat(cli1, fname, &sbuf))) {
-               printf("stat failed (%s)\n", cli_errstr(cli1));
+       status = cli_posix_stat(cli1, fname, &sbuf);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("stat failed (%s)\n", nt_errstr(status));
                goto out;
        }
 
@@ -4983,59 +5351,147 @@ static bool run_simple_posix_open_test(int dummy)
                goto out;
        }
 
+       /* Ensure st_mode == 0600 */
+       if ((sbuf.st_ex_mode & 07777) != 0600) {
+               printf("posix_open - bad permissions 0%o != 0600\n",
+                               (unsigned int)(sbuf.st_ex_mode & 07777));
+               goto out;
+       }
+
        /* Test ftruncate - set file size back to zero. */
-       if (!NT_STATUS_IS_OK(cli_ftruncate(cli1, fnum1, 0))) {
-               printf("ftruncate failed (%s)\n", cli_errstr(cli1));
+       status = cli_ftruncate(cli1, fnum1, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("ftruncate failed (%s)\n", nt_errstr(status));
                goto out;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
                goto out;
        }
 
        /* Now open the file again for read only. */
-       if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
-               printf("POSIX open of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX open of %s failed (%s)\n", fname, nt_errstr(status));
                goto out;
        }
 
        /* Now unlink while open. */
-       if (!NT_STATUS_IS_OK(cli_posix_unlink(cli1, fname))) {
-               printf("POSIX unlink of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_posix_unlink(cli1, fname);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX unlink of %s failed (%s)\n", fname, nt_errstr(status));
                goto out;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-               printf("close(2) failed (%s)\n", cli_errstr(cli1));
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close(2) failed (%s)\n", nt_errstr(status));
                goto out;
        }
 
        /* Ensure the file has gone. */
-       if (NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1))) {
+       status = cli_posix_open(cli1, fname, O_RDONLY, 0, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("POSIX open of %s succeeded, should have been deleted.\n", fname);
                goto out;
        }
 
-       /* What happens when we try and POSIX open a directory ? */
-       if (NT_STATUS_IS_OK(cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1))) {
+       /* Create again to test open with O_TRUNC. */
+       status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
+               goto out;
+       }
+
+       /* Test ftruncate - set file size. */
+       status = cli_ftruncate(cli1, fnum1, 1000);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("ftruncate failed (%s)\n", nt_errstr(status));
+               goto out;
+       }
+
+       /* Ensure st_size == 1000 */
+       status = cli_posix_stat(cli1, fname, &sbuf);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("stat failed (%s)\n", nt_errstr(status));
+               goto out;
+       }
+
+       if (sbuf.st_ex_size != 1000) {
+               printf("ftruncate - stat size (%u) != 1000\n", (unsigned int)sbuf.st_ex_size);
+               goto out;
+       }
+
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close(2) failed (%s)\n", nt_errstr(status));
+               goto out;
+       }
+
+       /* Re-open with O_TRUNC. */
+       status = cli_posix_open(cli1, fname, O_WRONLY|O_TRUNC, 0600, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
+               goto out;
+       }
+
+       /* Ensure st_size == 0 */
+       status = cli_posix_stat(cli1, fname, &sbuf);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("stat failed (%s)\n", nt_errstr(status));
+               goto out;
+       }
+
+       if (sbuf.st_ex_size != 0) {
+               printf("O_TRUNC - stat size (%u) != 0\n", (unsigned int)sbuf.st_ex_size);
+               goto out;
+       }
+
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
+               goto out;
+       }
+
+       status = cli_posix_unlink(cli1, fname);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX unlink of %s failed (%s)\n", fname, nt_errstr(status));
+               goto out;
+       }
+
+       status = cli_posix_open(cli1, dname, O_RDONLY, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX open directory O_RDONLY of %s failed (%s)\n",
+                       dname, nt_errstr(status));
+               goto out;
+       }
+
+       cli_close(cli1, fnum1);
+
+       /* What happens when we try and POSIX open a directory for write ? */
+       status = cli_posix_open(cli1, dname, O_RDWR, 0, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("POSIX open of directory %s succeeded, should have failed.\n", fname);
                goto out;
        } else {
-               if (!check_error(__LINE__, cli1, ERRDOS, EISDIR,
+               if (!check_both_error(__LINE__, status, ERRDOS, EISDIR,
                                NT_STATUS_FILE_IS_A_DIRECTORY)) {
                        goto out;
                }
        }
 
        /* Create the file. */
-       if (!NT_STATUS_IS_OK(cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, 0600, &fnum1))) {
-               printf("POSIX create of %s failed (%s)\n", fname, cli_errstr(cli1));
+       status = cli_posix_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL,
+                               0600, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX create of %s failed (%s)\n", fname, nt_errstr(status));
                goto out;
        }
 
        /* Write some data into it. */
-       status = cli_writeall(cli1, fnum1, 0, (uint8_t *)"TEST DATA\n", 0, 10,
+       status = cli_writeall(cli1, fnum1, 0, (const uint8_t *)"TEST DATA\n", 0, 10,
                              NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("cli_write failed: %s\n", nt_errstr(status));
@@ -5045,25 +5501,34 @@ static bool run_simple_posix_open_test(int dummy)
        cli_close(cli1, fnum1);
 
        /* Now create a hardlink. */
-       if (!NT_STATUS_IS_OK(cli_posix_hardlink(cli1, fname, hname))) {
-               printf("POSIX hardlink of %s failed (%s)\n", hname, cli_errstr(cli1));
+       status = cli_posix_hardlink(cli1, fname, hname);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX hardlink of %s failed (%s)\n", hname, nt_errstr(status));
                goto out;
        }
 
        /* Now create a symlink. */
-       if (!NT_STATUS_IS_OK(cli_posix_symlink(cli1, fname, sname))) {
-               printf("POSIX symlink of %s failed (%s)\n", sname, cli_errstr(cli1));
+       status = cli_posix_symlink(cli1, fname, sname);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX symlink of %s failed (%s)\n", sname, nt_errstr(status));
                goto out;
        }
 
        /* Open the hardlink for read. */
-       if (!NT_STATUS_IS_OK(cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1))) {
-               printf("POSIX open of %s failed (%s)\n", hname, cli_errstr(cli1));
+       status = cli_posix_open(cli1, hname, O_RDONLY, 0, &fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX open of %s failed (%s)\n", hname, nt_errstr(status));
                goto out;
        }
 
-       if (cli_read(cli1, fnum1, buf, 0, 10) != 10) {
-               printf("POSIX read of %s failed (%s)\n", hname, cli_errstr(cli1));
+       status = cli_read(cli1, fnum1, buf, 0, 10, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX read of %s failed (%s)\n", hname,
+                      nt_errstr(status));
+               goto out;
+       } else if (nread != 10) {
+               printf("POSIX read of %s failed. Received %ld, expected %d\n",
+                      hname, (unsigned long)nread, 10);
                goto out;
        }
 
@@ -5073,14 +5538,16 @@ static bool run_simple_posix_open_test(int dummy)
        }
 
        /* Do a POSIX lock/unlock. */
-       if (!NT_STATUS_IS_OK(cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK))) {
-               printf("POSIX lock failed %s\n", cli_errstr(cli1));
+       status = cli_posix_lock(cli1, fnum1, 0, 100, true, READ_LOCK);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX lock failed %s\n", nt_errstr(status));
                goto out;
        }
 
        /* Punch a hole in the locked area. */
-       if (!NT_STATUS_IS_OK(cli_posix_unlock(cli1, fnum1, 10, 80))) {
-               printf("POSIX unlock failed %s\n", cli_errstr(cli1));
+       status = cli_posix_unlock(cli1, fnum1, 10, 80);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX unlock failed %s\n", nt_errstr(status));
                goto out;
        }
 
@@ -5088,22 +5555,24 @@ static bool run_simple_posix_open_test(int dummy)
 
        /* Open the symlink for read - this should fail. A POSIX
           client should not be doing opens on a symlink. */
-       if (NT_STATUS_IS_OK(cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1))) {
+       status = cli_posix_open(cli1, sname, O_RDONLY, 0, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("POSIX open of %s succeeded (should have failed)\n", sname);
                goto out;
        } else {
-               if (!check_error(__LINE__, cli1, ERRDOS, ERRbadpath,
+               if (!check_both_error(__LINE__, status, ERRDOS, ERRbadpath,
                                NT_STATUS_OBJECT_PATH_NOT_FOUND)) {
                        printf("POSIX open of %s should have failed "
                                "with NT_STATUS_OBJECT_PATH_NOT_FOUND, "
                                "failed with %s instead.\n",
-                               sname, cli_errstr(cli1));
+                               sname, nt_errstr(status));
                        goto out;
                }
        }
 
-       if (!NT_STATUS_IS_OK(cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf)))) {
-               printf("POSIX readlink on %s failed (%s)\n", sname, cli_errstr(cli1));
+       status = cli_posix_readlink(cli1, sname, namebuf, sizeof(namebuf));
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX readlink on %s failed (%s)\n", sname, nt_errstr(status));
                goto out;
        }
 
@@ -5113,8 +5582,29 @@ static bool run_simple_posix_open_test(int dummy)
                goto out;
        }
 
-       if (!NT_STATUS_IS_OK(cli_posix_rmdir(cli1, dname))) {
-               printf("POSIX rmdir failed (%s)\n", cli_errstr(cli1));
+       status = cli_posix_rmdir(cli1, dname);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX rmdir failed (%s)\n", nt_errstr(status));
+               goto out;
+       }
+
+       /* Check directory opens with a specific permission. */
+       status = cli_posix_mkdir(cli1, dname, 0700);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("POSIX mkdir of %s failed (%s)\n", dname, nt_errstr(status));
+               goto out;
+       }
+
+       /* Ensure st_mode == 0700 */
+       status = cli_posix_stat(cli1, dname, &sbuf);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("stat failed (%s)\n", nt_errstr(status));
+               goto out;
+       }
+
+       if ((sbuf.st_ex_mode & 07777) != 0700) {
+               printf("posix_mkdir - bad permissions 0%o != 0700\n",
+                               (unsigned int)(sbuf.st_ex_mode & 07777));
                goto out;
        }
 
@@ -5209,6 +5699,7 @@ static bool run_openattrtest(int dummy)
        bool correct = True;
        uint16 attr;
        unsigned int i, j, k, l;
+       NTSTATUS status;
 
        printf("starting open attr test\n");
 
@@ -5216,38 +5707,47 @@ static bool run_openattrtest(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32); i++) {
                cli_setatr(cli1, fname, 0, 0);
-               cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
-               if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, open_attrs_table[i],
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum1))) {
-                       printf("open %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
+               cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+
+               status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA,
+                                      open_attrs_table[i], FILE_SHARE_NONE,
+                                      FILE_OVERWRITE_IF, 0, 0, &fnum1);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("open %d (1) of %s failed (%s)\n", i, fname, nt_errstr(status));
                        return False;
                }
 
-               if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-                       printf("close %d (1) of %s failed (%s)\n", i, fname, cli_errstr(cli1));
+               status = cli_close(cli1, fnum1);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("close %d (1) of %s failed (%s)\n", i, fname, nt_errstr(status));
                        return False;
                }
 
                for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
-                       if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA, open_attrs_table[j],
-                                          FILE_SHARE_NONE, FILE_OVERWRITE, 0, 0, &fnum1))) {
+                       status = cli_ntcreate(cli1, fname, 0,
+                                             FILE_READ_DATA|FILE_WRITE_DATA,
+                                             open_attrs_table[j],
+                                             FILE_SHARE_NONE, FILE_OVERWRITE,
+                                             0, 0, &fnum1);
+                       if (!NT_STATUS_IS_OK(status)) {
                                for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
                                        if (attr_results[l].num == k) {
                                                printf("[%d] trunc open 0x%x -> 0x%x of %s failed - should have succeeded !(0x%x:%s)\n",
                                                                k, open_attrs_table[i],
                                                                open_attrs_table[j],
-                                                               fname, NT_STATUS_V(cli_nt_error(cli1)), cli_errstr(cli1));
+                                                               fname, NT_STATUS_V(status), nt_errstr(status));
                                                correct = False;
                                        }
                                }
-                               if (NT_STATUS_V(cli_nt_error(cli1)) != NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
+
+                               if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
                                        printf("[%d] trunc open 0x%x -> 0x%x failed with wrong error code %s\n",
                                                        k, open_attrs_table[i], open_attrs_table[j],
-                                                       cli_errstr(cli1));
+                                                       nt_errstr(status));
                                        correct = False;
                                }
 #if 0
@@ -5257,13 +5757,15 @@ static bool run_openattrtest(int dummy)
                                continue;
                        }
 
-                       if (!NT_STATUS_IS_OK(cli_close(cli1, fnum1))) {
-                               printf("close %d (2) of %s failed (%s)\n", j, fname, cli_errstr(cli1));
+                       status = cli_close(cli1, fnum1);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               printf("close %d (2) of %s failed (%s)\n", j, fname, nt_errstr(status));
                                return False;
                        }
 
-                       if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, &attr, NULL, NULL))) {
-                               printf("getatr(2) failed (%s)\n", cli_errstr(cli1));
+                       status = cli_getatr(cli1, fname, &attr, NULL, NULL);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               printf("getatr(2) failed (%s)\n", nt_errstr(status));
                                return False;
                        }
 
@@ -5292,7 +5794,7 @@ static bool run_openattrtest(int dummy)
        }
 
        cli_setatr(cli1, fname, 0, 0);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        printf("open attr test %s.\n", correct ? "passed" : "failed");
 
@@ -5330,13 +5832,13 @@ static bool run_dirtest(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        srandom(0);
        for (i=0;i<torture_numops;i++) {
                fstring fname;
                slprintf(fname, sizeof(fname), "\\%x", (int)random());
-               if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
+               if (!NT_STATUS_IS_OK(cli_openx(cli, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum))) {
                        fprintf(stderr,"Failed to open %s\n", fname);
                        return False;
                }
@@ -5363,7 +5865,7 @@ static bool run_dirtest(int dummy)
        for (i=0;i<torture_numops;i++) {
                fstring fname;
                slprintf(fname, sizeof(fname), "\\%x", (int)random());
-               cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+               cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        }
 
        if (!torture_close_connection(cli)) {
@@ -5385,11 +5887,11 @@ static NTSTATUS del_fn(const char *mnt, struct file_info *finfo, const char *mas
        if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
                return NT_STATUS_OK;
 
-       if (finfo->mode & aDIR) {
+       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
                if (!NT_STATUS_IS_OK(cli_rmdir(pcli, fname)))
                        printf("del_fn: failed to rmdir %s\n,", fname );
        } else {
-               if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, aSYSTEM | aHIDDEN)))
+               if (!NT_STATUS_IS_OK(cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN)))
                        printf("del_fn: failed to unlink %s\n,", fname );
        }
        return NT_STATUS_OK;
@@ -5414,10 +5916,11 @@ bool torture_ioctl_test(int dummy)
 
        printf("starting ioctl test\n");
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
-               printf("open of %s failed (%s)\n", fname, cli_errstr(cli));
+       status = cli_openx(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
        }
 
@@ -5458,6 +5961,7 @@ bool torture_chkpath_test(int dummy)
        static struct cli_state *cli;
        uint16_t fnum;
        bool ret;
+       NTSTATUS status;
 
        if (!torture_open_connection(&cli, 0)) {
                return False;
@@ -5467,53 +5971,62 @@ bool torture_chkpath_test(int dummy)
 
        /* cleanup from an old run */
        cli_rmdir(cli, "\\chkpath.dir\\dir2");
-       cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
+       cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_rmdir(cli, "\\chkpath.dir");
 
-       if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir"))) {
-               printf("mkdir1 failed : %s\n", cli_errstr(cli));
+       status = cli_mkdir(cli, "\\chkpath.dir");
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("mkdir1 failed : %s\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\chkpath.dir\\dir2"))) {
-               printf("mkdir2 failed : %s\n", cli_errstr(cli));
+       status = cli_mkdir(cli, "\\chkpath.dir\\dir2");
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("mkdir2 failed : %s\n", nt_errstr(status));
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
-               printf("open1 failed (%s)\n", cli_errstr(cli));
+       status = cli_openx(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL,
+                         DENY_NONE, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open1 failed (%s)\n", nt_errstr(status));
                return False;
        }
        cli_close(cli, fnum);
 
-       if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir"))) {
-               printf("chkpath1 failed: %s\n", cli_errstr(cli));
+       status = cli_chkpath(cli, "\\chkpath.dir");
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("chkpath1 failed: %s\n", nt_errstr(status));
                ret = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dir2"))) {
-               printf("chkpath2 failed: %s\n", cli_errstr(cli));
+       status = cli_chkpath(cli, "\\chkpath.dir\\dir2");
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("chkpath2 failed: %s\n", nt_errstr(status));
                ret = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\foo.txt"))) {
-               ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath, 
+       status = cli_chkpath(cli, "\\chkpath.dir\\foo.txt");
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = check_error(__LINE__, status, ERRDOS, ERRbadpath,
                                  NT_STATUS_NOT_A_DIRECTORY);
        } else {
                printf("* chkpath on a file should fail\n");
                ret = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\bar.txt"))) {
-               ret = check_error(__LINE__, cli, ERRDOS, ERRbadfile, 
+       status = cli_chkpath(cli, "\\chkpath.dir\\bar.txt");
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = check_error(__LINE__, status, ERRDOS, ERRbadfile,
                                  NT_STATUS_OBJECT_NAME_NOT_FOUND);
        } else {
-               printf("* chkpath on a non existant file should fail\n");
+               printf("* chkpath on a non existent file should fail\n");
                ret = False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt"))) {
-               ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath, 
+       status = cli_chkpath(cli, "\\chkpath.dir\\dirxx\\bar.txt");
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = check_error(__LINE__, status, ERRDOS, ERRbadpath,
                                  NT_STATUS_OBJECT_PATH_NOT_FOUND);
        } else {
                printf("* chkpath on a non existent component should fail\n");
@@ -5521,7 +6034,7 @@ bool torture_chkpath_test(int dummy)
        }
 
        cli_rmdir(cli, "\\chkpath.dir\\dir2");
-       cli_unlink(cli, "\\chkpath.dir\\*", aSYSTEM | aHIDDEN);
+       cli_unlink(cli, "\\chkpath.dir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_rmdir(cli, "\\chkpath.dir");
 
        if (!torture_close_connection(cli)) {
@@ -5550,12 +6063,14 @@ static bool run_eatest(int dummy)
                return False;
        }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0,
-                                  FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 
-                                  0x4044, 0, &fnum))) {
-               printf("open failed - %s\n", cli_errstr(cli));
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+
+       status = cli_ntcreate(cli, fname, 0,
+                              FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
+                              FILE_SHARE_NONE, FILE_OVERWRITE_IF,
+                              0x4044, 0, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open failed - %s\n", nt_errstr(status));
                talloc_destroy(mem_ctx);
                return False;
        }
@@ -5645,10 +6160,10 @@ static bool run_eatest(int dummy)
                correct = False;
        }
 
-       /* Try and delete a non existant EA. */
+       /* Try and delete a non existent EA. */
        status = cli_set_ea_path(cli, fname, "foo", "", 0);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("deleting non-existant EA 'foo' should succeed. %s\n",
+               printf("deleting non-existent EA 'foo' should succeed. %s\n",
                       nt_errstr(status));
                correct = False;
        }
@@ -5675,10 +6190,10 @@ static bool run_dirtest1(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
-       cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
+       cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
        cli_rmdir(cli, "\\LISTDIR");
        cli_mkdir(cli, "\\LISTDIR");
 
@@ -5704,7 +6219,7 @@ static bool run_dirtest1(int dummy)
 
        /* Now ensure that doing an old list sees both files and directories. */
        num_seen = 0;
-       cli_list_old(cli, "\\LISTDIR\\*", aDIR, list_fn, &num_seen);
+       cli_list_old(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
        printf("num_seen = %d\n", num_seen );
        /* We should see 100 files + 1000 directories + . and .. */
        if (num_seen != 2002)
@@ -5714,20 +6229,20 @@ static bool run_dirtest1(int dummy)
         * relevent entries.
         */
        num_seen = 0;
-       cli_list_old(cli, "\\LISTDIR\\*", (aDIR<<8)|aDIR, list_fn, &num_seen);
+       cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_DIRECTORY<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
        printf("num_seen = %d\n", num_seen );
        if (num_seen != 1002)
                correct = False;
 
        num_seen = 0;
-       cli_list_old(cli, "\\LISTDIR\\*", (aARCH<<8)|aDIR, list_fn, &num_seen);
+       cli_list_old(cli, "\\LISTDIR\\*", (FILE_ATTRIBUTE_ARCHIVE<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, &num_seen);
        printf("num_seen = %d\n", num_seen );
        if (num_seen != 1000)
                correct = False;
 
        /* Delete everything. */
        cli_list(cli, "\\LISTDIR\\*", 0, del_fn, cli);
-       cli_list(cli, "\\LISTDIR\\*", aDIR, del_fn, cli);
+       cli_list(cli, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
        cli_rmdir(cli, "\\LISTDIR");
 
 #if 0
@@ -5753,7 +6268,7 @@ static bool run_error_map_extract(int dummy) {
 
        uint32 error;
 
-       uint32 flgs2, errnum;
+       uint32 errnum;
         uint8 errclass;
 
        NTSTATUS nt_status;
@@ -5762,13 +6277,15 @@ static bool run_error_map_extract(int dummy) {
 
        /* NT-Error connection */
 
+       disable_spnego = true;
        if (!(c_nt = open_nbt_connection())) {
+               disable_spnego = false;
                return False;
        }
+       disable_spnego = false;
 
-       c_nt->use_spnego = False;
-
-       status = cli_negprot(c_nt);
+       status = smbXcli_negprot(c_nt->conn, c_nt->timeout, PROTOCOL_CORE,
+                                PROTOCOL_NT1);
 
        if (!NT_STATUS_IS_OK(status)) {
                printf("%s rejected the NT-error negprot (%s)\n", host,
@@ -5777,22 +6294,26 @@ static bool run_error_map_extract(int dummy) {
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_session_setup(c_nt, "", "", 0, "", 0,
-                                              workgroup))) {
-               printf("%s rejected the NT-error initial session setup (%s)\n",host, cli_errstr(c_nt));
+       status = cli_session_setup(c_nt, "", "", 0, "", 0, workgroup);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("%s rejected the NT-error initial session setup (%s)\n",host, nt_errstr(status));
                return False;
        }
 
        /* DOS-Error connection */
 
+       disable_spnego = true;
+       force_dos_errors = true;
        if (!(c_dos = open_nbt_connection())) {
+               disable_spnego = false;
+               force_dos_errors = false;
                return False;
        }
+       disable_spnego = false;
+       force_dos_errors = false;
 
-       c_dos->use_spnego = False;
-       c_dos->force_dos_errors = True;
-
-       status = cli_negprot(c_dos);
+       status = smbXcli_negprot(c_dos->conn, c_dos->timeout, PROTOCOL_CORE,
+                                PROTOCOL_NT1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("%s rejected the DOS-error negprot (%s)\n", host,
                       nt_errstr(status));
@@ -5800,60 +6321,64 @@ static bool run_error_map_extract(int dummy) {
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_session_setup(c_dos, "", "", 0, "", 0,
-                                              workgroup))) {
-               printf("%s rejected the DOS-error initial session setup (%s)\n",host, cli_errstr(c_dos));
+       status = cli_session_setup(c_dos, "", "", 0, "", 0, workgroup);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("%s rejected the DOS-error initial session setup (%s)\n",
+                       host, nt_errstr(status));
                return False;
        }
 
+       c_nt->map_dos_errors = false;
+       c_dos->map_dos_errors = false;
+
        for (error=(0xc0000000 | 0x1); error < (0xc0000000| 0xFFF); error++) {
                fstr_sprintf(user, "%X", error);
 
-               if (NT_STATUS_IS_OK(cli_session_setup(c_nt, user, 
-                                                     password, strlen(password),
-                                                     password, strlen(password),
-                                                     workgroup))) {
+               status = cli_session_setup(c_nt, user,
+                                          password, strlen(password),
+                                          password, strlen(password),
+                                          workgroup);
+               if (NT_STATUS_IS_OK(status)) {
                        printf("/** Session setup succeeded.  This shouldn't happen...*/\n");
                }
 
-               flgs2 = SVAL(c_nt->inbuf,smb_flg2);
-
                /* Case #1: 32-bit NT errors */
-               if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
-                       nt_status = NT_STATUS(IVAL(c_nt->inbuf,smb_rcls));
+               if (!NT_STATUS_IS_DOS(status)) {
+                       nt_status = status;
                } else {
                        printf("/** Dos error on NT connection! (%s) */\n", 
-                              cli_errstr(c_nt));
+                              nt_errstr(status));
                        nt_status = NT_STATUS(0xc0000000);
                }
 
-               if (NT_STATUS_IS_OK(cli_session_setup(c_dos, user, 
-                                                     password, strlen(password),
-                                                     password, strlen(password),
-                                                     workgroup))) {
+               status = cli_session_setup(c_dos, user,
+                                          password, strlen(password),
+                                          password, strlen(password),
+                                          workgroup);
+               if (NT_STATUS_IS_OK(status)) {
                        printf("/** Session setup succeeded.  This shouldn't happen...*/\n");
                }
-               flgs2 = SVAL(c_dos->inbuf,smb_flg2), errnum;
 
                /* Case #1: 32-bit NT errors */
-               if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
+               if (NT_STATUS_IS_DOS(status)) {
                        printf("/** NT error on DOS connection! (%s) */\n", 
-                              cli_errstr(c_nt));
+                              nt_errstr(status));
                        errnum = errclass = 0;
                } else {
-                       cli_dos_error(c_dos, &errclass, &errnum);
+                       errclass = NT_STATUS_DOS_CLASS(status);
+                       errnum = NT_STATUS_DOS_CODE(status);
                }
 
                if (NT_STATUS_V(nt_status) != error) { 
                        printf("/*\t{ This NT error code was 'sqashed'\n\t from %s to %s \n\t during the session setup }\n*/\n", 
-                              get_nt_error_c_code(NT_STATUS(error)), 
-                              get_nt_error_c_code(nt_status));
+                              get_nt_error_c_code(talloc_tos(), NT_STATUS(error)), 
+                              get_nt_error_c_code(talloc_tos(), nt_status));
                }
 
                printf("\t{%s,\t%s,\t%s},\n", 
                       smb_dos_err_class(errclass), 
                       smb_dos_err_name(errclass, errnum), 
-                      get_nt_error_c_code(NT_STATUS(error)));
+                      get_nt_error_c_code(talloc_tos(), NT_STATUS(error)));
        }
        return True;
 }
@@ -5870,11 +6395,11 @@ static bool run_sesssetup_bench(int dummy)
                return false;
        }
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(
-                       c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
-                       FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
-                       FILE_DELETE_ON_CLOSE, 0, &fnum))) {
-               d_printf("open %s failed: %s\n", fname, cli_errstr(c));
+       status = cli_ntcreate(c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                             FILE_DELETE_ON_CLOSE, 0, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("open %s failed: %s\n", fname, nt_errstr(status));
                return false;
        }
 
@@ -5890,7 +6415,7 @@ static bool run_sesssetup_bench(int dummy)
                        return false;
                }
 
-               d_printf("\r%d   ", (int)c->vuid);
+               d_printf("\r%d   ", (int)cli_state_get_uid(c));
 
                status = cli_ulogoff(c);
                if (!NT_STATUS_IS_OK(status)) {
@@ -5898,7 +6423,6 @@ static bool run_sesssetup_bench(int dummy)
                                 __location__, nt_errstr(status));
                        return false;
                }
-               c->vuid = 0;
        }
 
        return true;
@@ -5927,10 +6451,10 @@ static void chain1_open_completion(struct tevent_req *req)
 {
        uint16_t fnum;
        NTSTATUS status;
-       status = cli_open_recv(req, &fnum);
+       status = cli_openx_recv(req, &fnum);
        TALLOC_FREE(req);
 
-       d_printf("cli_open_recv returned %s: %d\n",
+       d_printf("cli_openx_recv returned %s: %d\n",
                 nt_errstr(status),
                 NT_STATUS_IS_OK(status) ? fnum : -1);
 }
@@ -5974,16 +6498,16 @@ static bool run_chain1(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
-       reqs[0] = cli_open_create(talloc_tos(), evt, cli1, "\\test",
+       reqs[0] = cli_openx_create(talloc_tos(), evt, cli1, "\\test",
                                  O_CREAT|O_RDWR, 0, &smbreqs[0]);
        if (reqs[0] == NULL) return false;
        tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
 
 
        reqs[1] = cli_write_andx_create(talloc_tos(), evt, cli1, 0, 0,
-                                       (uint8_t *)str, 0, strlen(str)+1,
+                                       (const uint8_t *)str, 0, strlen(str)+1,
                                        smbreqs, 1, &smbreqs[1]);
        if (reqs[1] == NULL) return false;
        tevent_req_set_callback(reqs[1], chain1_write_completion, NULL);
@@ -5992,13 +6516,13 @@ static bool run_chain1(int dummy)
        if (reqs[2] == NULL) return false;
        tevent_req_set_callback(reqs[2], chain1_close_completion, &done);
 
-       status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
+       status = smb1cli_req_chain_submit(smbreqs, ARRAY_SIZE(smbreqs));
        if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
 
        while (!done) {
-               event_loop_once(evt);
+               tevent_loop_once(evt);
        }
 
        torture_close_connection(cli1);
@@ -6030,13 +6554,13 @@ static bool run_chain2(int dummy)
        NTSTATUS status;
 
        printf("starting chain2 test\n");
-       status = cli_start_connection(&cli1, global_myname(), host, NULL,
-                                     port_to_use, Undefined, 0);
+       status = cli_start_connection(&cli1, lp_netbios_name(), host, NULL,
+                                     port_to_use, SMB_SIGNING_DEFAULT, 0);
        if (!NT_STATUS_IS_OK(status)) {
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        reqs[0] = cli_session_setup_guest_create(talloc_tos(), evt, cli1,
                                                 &smbreqs[0]);
@@ -6048,13 +6572,13 @@ static bool run_chain2(int dummy)
        if (reqs[1] == NULL) return false;
        tevent_req_set_callback(reqs[1], chain2_tcon_completion, &done);
 
-       status = cli_smb_chain_send(smbreqs, ARRAY_SIZE(smbreqs));
+       status = smb1cli_req_chain_submit(smbreqs, ARRAY_SIZE(smbreqs));
        if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
 
        while (!done) {
-               event_loop_once(evt);
+               tevent_loop_once(evt);
        }
 
        torture_close_connection(cli1);
@@ -6472,7 +6996,7 @@ static bool run_mangle1(int dummy)
        fstring alt_name;
        NTSTATUS status;
        time_t change_time, access_time, write_time;
-       SMB_OFF_T size;
+       off_t size;
        uint16_t mode;
 
        printf("starting mangle1 test\n");
@@ -6480,12 +7004,13 @@ static bool run_mangle1(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(
-                       cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
-                       FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
-               d_printf("open %s failed: %s\n", fname, cli_errstr(cli));
+       status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                             0, 0, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("open %s failed: %s\n", fname, nt_errstr(status));
                return false;
        }
        cli_close(cli, fnum);
@@ -6498,9 +7023,10 @@ static bool run_mangle1(int dummy)
        }
        d_printf("alt_name: %s\n", alt_name);
 
-       if (!NT_STATUS_IS_OK(cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum))) {
-               d_printf("cli_open(%s) failed: %s\n", alt_name,
-                        cli_errstr(cli));
+       status = cli_openx(cli, alt_name, O_RDONLY, DENY_NONE, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_openx(%s) failed: %s\n", alt_name,
+                        nt_errstr(status));
                return false;
        }
        cli_close(cli, fnum);
@@ -6541,25 +7067,26 @@ static bool run_windows_write(int dummy)
        struct timeval start_time;
        double seconds;
        double kbytes;
+       NTSTATUS status;
 
        printf("starting windows_write test\n");
        if (!torture_open_connection(&cli1, 0)) {
                return False;
        }
 
-       if (!NT_STATUS_IS_OK(cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
-               printf("open failed (%s)\n", cli_errstr(cli1));
+       status = cli_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open failed (%s)\n", nt_errstr(status));
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        start_time = timeval_current();
 
        for (i=0; i<torture_numops; i++) {
                uint8_t c = 0;
                off_t start = i * torture_blocksize;
-               NTSTATUS status;
                size_t to_pull = torture_blocksize - 1;
 
                status = cli_writeall(cli1, fnum, 0, &c,
@@ -6587,7 +7114,7 @@ static bool run_windows_write(int dummy)
        ret = true;
  fail:
        cli_close(cli1, fnum);
-       cli_unlink(cli1, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        torture_close_connection(cli1);
        return ret;
 }
@@ -6601,7 +7128,7 @@ static bool run_cli_echo(int dummy)
        if (!torture_open_connection(&cli, 0)) {
                return false;
        }
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        status = cli_echo(cli, 5, data_blob_const("hello", 5));
 
@@ -6625,10 +7152,10 @@ static bool run_uid_regression_test(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        /* Ok - now save then logoff our current user. */
-       old_vuid = cli->vuid;
+       old_vuid = cli_state_get_uid(cli);
 
        status = cli_ulogoff(cli);
        if (!NT_STATUS_IS_OK(status)) {
@@ -6638,7 +7165,7 @@ static bool run_uid_regression_test(int dummy)
                goto out;
        }
 
-       cli->vuid = old_vuid;
+       cli_state_set_uid(cli, old_vuid);
 
        /* Try an operation. */
        status = cli_mkdir(cli, "\\uid_reg_test");
@@ -6649,17 +7176,17 @@ static bool run_uid_regression_test(int dummy)
                goto out;
        } else {
                /* Should be bad uid. */
-               if (!check_error(__LINE__, cli, ERRSRV, ERRbaduid,
-                               NT_STATUS_USER_SESSION_DELETED)) {
+               if (!check_error(__LINE__, status, ERRSRV, ERRbaduid,
+                                NT_STATUS_USER_SESSION_DELETED)) {
                        correct = false;
                        goto out;
                }
        }
 
-       old_cnum = cli->cnum;
+       old_cnum = cli_state_get_tid(cli);
 
        /* Now try a SMBtdis with the invald vuid set to zero. */
-       cli->vuid = 0;
+       cli_state_set_uid(cli, 0);
 
        /* This should succeed. */
        status = cli_tdis(cli);
@@ -6672,8 +7199,8 @@ static bool run_uid_regression_test(int dummy)
                goto out;
        }
 
-       cli->vuid = old_vuid;
-       cli->cnum = old_cnum;
+       cli_state_set_uid(cli, old_vuid);
+       cli_state_set_tid(cli, old_cnum);
 
        /* This should fail. */
        status = cli_tdis(cli);
@@ -6683,7 +7210,7 @@ static bool run_uid_regression_test(int dummy)
                goto out;
        } else {
                /* Should be bad tid. */
-               if (!check_error(__LINE__, cli, ERRSRV, ERRinvnid,
+               if (!check_error(__LINE__, status, ERRSRV, ERRinvnid,
                                NT_STATUS_NETWORK_NAME_DELETED)) {
                        correct = false;
                        goto out;
@@ -6714,13 +7241,13 @@ static NTSTATUS shortname_del_fn(const char *mnt, struct file_info *finfo,
        if (strcmp(finfo->name, ".") == 0 || strcmp(finfo->name, "..") == 0)
                return NT_STATUS_OK;
 
-       if (finfo->mode & aDIR) {
+       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
                status = cli_rmdir(pcli, fname);
                if (!NT_STATUS_IS_OK(status)) {
                        printf("del_fn: failed to rmdir %s\n,", fname );
                }
        } else {
-               status = cli_unlink(pcli, fname, aSYSTEM | aHIDDEN);
+               status = cli_unlink(pcli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
                if (!NT_STATUS_IS_OK(status)) {
                        printf("del_fn: failed to unlink %s\n,", fname );
                }
@@ -6746,13 +7273,13 @@ static NTSTATUS shortname_list_fn(const char *mnt, struct file_info *finfo,
 #endif
 
        if (strchr(force_shortname_chars, i)) {
-               if (!finfo->short_name[0]) {
+               if (!finfo->short_name) {
                        /* Shortname not created when it should be. */
                        d_printf("(%s) ERROR: Shortname was not created for file %s containing %d\n",
                                __location__, finfo->name, i);
                        s->val = true;
                }
-       } else if (finfo->short_name[0]){
+       } else if (finfo->short_name){
                /* Shortname created when it should not be. */
                d_printf("(%s) ERROR: Shortname %s was created for file %s\n",
                        __location__, finfo->short_name, finfo->name);
@@ -6768,7 +7295,8 @@ static bool run_shortname_test(int dummy)
        bool correct = True;
        int i;
        struct sn_state s;
-       char fname[20];
+       char fname[40];
+       NTSTATUS status;
 
        printf("starting shortname test\n");
 
@@ -6776,26 +7304,32 @@ static bool run_shortname_test(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
-       cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
+       cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
        cli_rmdir(cli, "\\shortname");
 
-       if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\shortname"))) {
+       status = cli_mkdir(cli, "\\shortname");
+       if (!NT_STATUS_IS_OK(status)) {
                d_printf("(%s) cli_mkdir of \\shortname failed: %s\n",
-                       __location__, cli_errstr(cli));
+                       __location__, nt_errstr(status));
                correct = false;
                goto out;
        }
 
-       strlcpy(fname, "\\shortname\\", sizeof(fname));
-       strlcat(fname, "test .txt", sizeof(fname));
+       if (strlcpy(fname, "\\shortname\\", sizeof(fname)) >= sizeof(fname)) {
+               correct = false;
+               goto out;
+       }
+       if (strlcat(fname, "test .txt", sizeof(fname)) >= sizeof(fname)) {
+               correct = false;
+               goto out;
+       }
 
        s.val = false;
 
        for (i = 32; i < 128; i++) {
-               NTSTATUS status;
                uint16_t fnum = (uint16_t)-1;
 
                s.i = i;
@@ -6809,24 +7343,26 @@ static bool run_shortname_test(int dummy)
                                    FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
                if (!NT_STATUS_IS_OK(status)) {
                        d_printf("(%s) cli_nt_create of %s failed: %s\n",
-                               __location__, fname, cli_errstr(cli));
+                               __location__, fname, nt_errstr(status));
                        correct = false;
                        goto out;
                }
                cli_close(cli, fnum);
 
                s.matched = 0;
-               cli_list(cli, "\\shortname\\test*.*", 0, shortname_list_fn,
-                        &s);
+               status = cli_list(cli, "\\shortname\\test*.*", 0,
+                                 shortname_list_fn, &s);
                if (s.matched != 1) {
                        d_printf("(%s) failed to list %s: %s\n",
-                               __location__, fname, cli_errstr(cli));
+                               __location__, fname, nt_errstr(status));
                        correct = false;
                        goto out;
                }
-               if (!NT_STATUS_IS_OK(cli_unlink(cli, fname, aSYSTEM | aHIDDEN))) {
+
+               status = cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+               if (!NT_STATUS_IS_OK(status)) {
                        d_printf("(%s) failed to delete %s: %s\n",
-                               __location__, fname, cli_errstr(cli));
+                               __location__, fname, nt_errstr(status));
                        correct = false;
                        goto out;
                }
@@ -6840,7 +7376,7 @@ static bool run_shortname_test(int dummy)
   out:
 
        cli_list(cli, "\\shortname\\*", 0, shortname_del_fn, cli);
-       cli_list(cli, "\\shortname\\*", aDIR, shortname_del_fn, cli);
+       cli_list(cli, "\\shortname\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
        cli_rmdir(cli, "\\shortname");
        torture_close_connection(cli);
        return correct;
@@ -6969,7 +7505,7 @@ static bool run_dir_createtime(int dummy)
                return false;
        }
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_rmdir(cli, dname);
 
        status = cli_mkdir(cli, dname);
@@ -6989,10 +7525,10 @@ static bool run_dir_createtime(int dummy)
        /* Sleep 3 seconds, then create a file. */
        sleep(3);
 
-       status = cli_open(cli, fname, O_RDWR | O_CREAT | O_EXCL,
+       status = cli_openx(cli, fname, O_RDWR | O_CREAT | O_EXCL,
                          DENY_NONE, &fnum);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("cli_open failed: %s\n", nt_errstr(status));
+               printf("cli_openx failed: %s\n", nt_errstr(status));
                goto out;
        }
 
@@ -7013,7 +7549,7 @@ static bool run_dir_createtime(int dummy)
 
   out:
 
-       cli_unlink(cli, fname, aSYSTEM | aHIDDEN);
+       cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_rmdir(cli, dname);
        if (!torture_close_connection(cli)) {
                ret = false;
@@ -7030,7 +7566,7 @@ static bool run_streamerror(int dummy)
                "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
        NTSTATUS status;
        time_t change_time, access_time, write_time;
-       SMB_OFF_T size;
+       off_t size;
        uint16_t mode, fnum;
        bool ret = true;
 
@@ -7038,7 +7574,7 @@ static bool run_streamerror(int dummy)
                return false;
        }
 
-       cli_unlink(cli, "\\testdir\\*", aSYSTEM | aHIDDEN);
+       cli_unlink(cli, "\\testdir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_rmdir(cli, dname);
 
        status = cli_mkdir(cli, dname);
@@ -7047,10 +7583,8 @@ static bool run_streamerror(int dummy)
                return false;
        }
 
-       cli_qpathinfo1(cli, streamname, &change_time, &access_time, &write_time,
-                     &size, &mode);
-       status = cli_nt_error(cli);
-
+       status = cli_qpathinfo1(cli, streamname, &change_time, &access_time,
+                               &write_time, &size, &mode);
        if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
                printf("pathinfo returned %s, expected "
                       "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
@@ -7224,26 +7758,29 @@ static bool rbt_testval(struct db_context *db, const char *key,
        TDB_DATA data = string_tdb_data(value);
        bool ret = false;
        NTSTATUS status;
+       TDB_DATA dbvalue;
 
-       rec = db->fetch_locked(db, db, string_tdb_data(key));
+       rec = dbwrap_fetch_locked(db, db, string_tdb_data(key));
        if (rec == NULL) {
                d_fprintf(stderr, "fetch_locked failed\n");
                goto done;
        }
-       status = rec->store(rec, data, 0);
+       status = dbwrap_record_store(rec, data, 0);
        if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, "store failed: %s\n", nt_errstr(status));
                goto done;
        }
        TALLOC_FREE(rec);
 
-       rec = db->fetch_locked(db, db, string_tdb_data(key));
+       rec = dbwrap_fetch_locked(db, db, string_tdb_data(key));
        if (rec == NULL) {
                d_fprintf(stderr, "second fetch_locked failed\n");
                goto done;
        }
-       if ((rec->value.dsize != data.dsize)
-           || (memcmp(rec->value.dptr, data.dptr, data.dsize) != 0)) {
+
+       dbvalue = dbwrap_record_get_value(rec);
+       if ((dbvalue.dsize != data.dsize)
+           || (memcmp(dbvalue.dptr, data.dptr, data.dsize) != 0)) {
                d_fprintf(stderr, "Got wrong data back\n");
                goto done;
        }
@@ -7419,7 +7956,8 @@ static bool run_local_talloc_dict(int dummy)
 {
        struct talloc_dict *dict;
        struct talloc_dict_test *t;
-       int key, count;
+       int key, count, res;
+       bool ok;
 
        dict = talloc_dict_init(talloc_tos());
        if (dict == NULL) {
@@ -7433,12 +7971,14 @@ static bool run_local_talloc_dict(int dummy)
 
        key = 1;
        t->content = 1;
-       if (!talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), t)) {
+       ok = talloc_dict_set(dict, data_blob_const(&key, sizeof(key)), &t);
+       if (!ok) {
                return false;
        }
 
        count = 0;
-       if (talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count) != 0) {
+       res = talloc_dict_traverse(dict, talloc_dict_traverse_fn, &count);
+       if (res == -1) {
                return false;
        }
 
@@ -7446,6 +7986,10 @@ static bool run_local_talloc_dict(int dummy)
                return false;
        }
 
+       if (count != res) {
+               return false;
+       }
+
        TALLOC_FREE(dict);
 
        return true;
@@ -7621,7 +8165,7 @@ static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
                stype = "$DATA";
        }
        else {
-               if (StrCaseCmp(stype, ":$DATA") != 0) {
+               if (strcasecmp_m(stype, ":$DATA") != 0) {
                        /*
                         * If there is an explicit stream type, so far we only
                         * allow $DATA. Is there anything else allowed? -- vl
@@ -7763,7 +8307,7 @@ static bool run_local_memcache(int dummy)
        size_t size1, size2;
        bool ret = false;
 
-       cache = memcache_init(NULL, 100);
+       cache = memcache_init(NULL, sizeof(void *) == 8 ? 200 : 100);
 
        if (cache == NULL) {
                printf("memcache_init failed\n");
@@ -7870,7 +8414,7 @@ static bool run_local_wbclient(int dummy)
                goto fail;
        }
 
-       wb_ctx = TALLOC_ARRAY(ev, struct wb_context *, nprocs);
+       wb_ctx = talloc_array(ev, struct wb_context *, torture_nprocs);
        if (wb_ctx == NULL) {
                goto fail;
        }
@@ -7878,9 +8422,9 @@ static bool run_local_wbclient(int dummy)
        ZERO_STRUCT(wb_req);
        wb_req.cmd = WINBINDD_PING;
 
-       d_printf("nprocs=%d, numops=%d\n", (int)nprocs, (int)torture_numops);
+       d_printf("torture_nprocs=%d, numops=%d\n", (int)torture_nprocs, (int)torture_numops);
 
-       for (i=0; i<nprocs; i++) {
+       for (i=0; i<torture_nprocs; i++) {
                wb_ctx[i] = wb_context_init(ev, NULL);
                if (wb_ctx[i] == NULL) {
                        goto fail;
@@ -7898,8 +8442,8 @@ static bool run_local_wbclient(int dummy)
 
        i = 0;
 
-       while (i < nprocs * torture_numops) {
-               event_loop_once(ev);
+       while (i < torture_nprocs * torture_numops) {
+               tevent_loop_once(ev);
        }
 
        result = true;
@@ -7948,7 +8492,7 @@ static bool run_getaddrinfo_send(int dummy)
                        goto fail;
                }
                tevent_req_set_callback(reqs[i], getaddrinfo_finished,
-                                       (void *)names[i]);
+                                       discard_const_p(void, names[i]));
        }
 
        for (i=0; i<ARRAY_SIZE(reqs); i++) {
@@ -7964,28 +8508,30 @@ fail:
 static bool dbtrans_inc(struct db_context *db)
 {
        struct db_record *rec;
-       uint32_t *val;
+       uint32_t val;
        bool ret = false;
        NTSTATUS status;
+       TDB_DATA value;
 
-       rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
+       rec = dbwrap_fetch_locked(db, db, string_term_tdb_data("transtest"));
        if (rec == NULL) {
                printf(__location__ "fetch_lock failed\n");
                return false;
        }
 
-       if (rec->value.dsize != sizeof(uint32_t)) {
+       value = dbwrap_record_get_value(rec);
+
+       if (value.dsize != sizeof(uint32_t)) {
                printf(__location__ "value.dsize = %d\n",
-                      (int)rec->value.dsize);
+                      (int)value.dsize);
                goto fail;
        }
 
-       val = (uint32_t *)rec->value.dptr;
-       *val += 1;
+       memcpy(&val, value.dptr, sizeof(val));
+       val += 1;
 
-       status = rec->store(rec, make_tdb_data((uint8_t *)val,
-                                              sizeof(uint32_t)),
-                           0);
+       status = dbwrap_record_store(
+               rec, make_tdb_data((uint8_t *)&val, sizeof(val)), 0);
        if (!NT_STATUS_IS_OK(status)) {
                printf(__location__ "store failed: %s\n",
                       nt_errstr(status));
@@ -8005,29 +8551,32 @@ static bool run_local_dbtrans(int dummy)
        NTSTATUS status;
        uint32_t initial;
        int res;
+       TDB_DATA value;
 
        db = db_open(talloc_tos(), "transtest.tdb", 0, TDB_DEFAULT,
-                    O_RDWR|O_CREAT, 0600);
+                    O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_1);
        if (db == NULL) {
                printf("Could not open transtest.db\n");
                return false;
        }
 
-       res = db->transaction_start(db);
-       if (res == -1) {
+       res = dbwrap_transaction_start(db);
+       if (res != 0) {
                printf(__location__ "transaction_start failed\n");
                return false;
        }
 
-       rec = db->fetch_locked(db, db, string_term_tdb_data("transtest"));
+       rec = dbwrap_fetch_locked(db, db, string_term_tdb_data("transtest"));
        if (rec == NULL) {
                printf(__location__ "fetch_lock failed\n");
                return false;
        }
 
-       if (rec->value.dptr == NULL) {
+       value = dbwrap_record_get_value(rec);
+
+       if (value.dptr == NULL) {
                initial = 0;
-               status = rec->store(
+               status = dbwrap_record_store(
                        rec, make_tdb_data((uint8_t *)&initial,
                                           sizeof(initial)),
                        0);
@@ -8040,8 +8589,8 @@ static bool run_local_dbtrans(int dummy)
 
        TALLOC_FREE(rec);
 
-       res = db->transaction_commit(db);
-       if (res == -1) {
+       res = dbwrap_transaction_commit(db);
+       if (res != 0) {
                printf(__location__ "transaction_commit failed\n");
                return false;
        }
@@ -8050,14 +8599,16 @@ static bool run_local_dbtrans(int dummy)
                uint32_t val, val2;
                int i;
 
-               res = db->transaction_start(db);
-               if (res == -1) {
+               res = dbwrap_transaction_start(db);
+               if (res != 0) {
                        printf(__location__ "transaction_start failed\n");
                        break;
                }
 
-               if (!dbwrap_fetch_uint32(db, "transtest", &val)) {
-                       printf(__location__ "dbwrap_fetch_uint32 failed\n");
+               status = dbwrap_fetch_uint32_bystring(db, "transtest", &val);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf(__location__ "dbwrap_fetch_uint32 failed: %s\n",
+                              nt_errstr(status));
                        break;
                }
 
@@ -8067,8 +8618,10 @@ static bool run_local_dbtrans(int dummy)
                        }
                }
 
-               if (!dbwrap_fetch_uint32(db, "transtest", &val2)) {
-                       printf(__location__ "dbwrap_fetch_uint32 failed\n");
+               status = dbwrap_fetch_uint32_bystring(db, "transtest", &val2);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf(__location__ "dbwrap_fetch_uint32 failed: %s\n",
+                              nt_errstr(status));
                        break;
                }
 
@@ -8080,8 +8633,8 @@ static bool run_local_dbtrans(int dummy)
 
                printf("val2=%d\r", val2);
 
-               res = db->transaction_commit(db);
-               if (res == -1) {
+               res = dbwrap_transaction_commit(db);
+               if (res != 0) {
                        printf(__location__ "transaction_commit failed\n");
                        break;
                }
@@ -8133,6 +8686,129 @@ fail:
        return result;
 }
 
+static bool run_local_hex_encode_buf(int dummy)
+{
+       char buf[17];
+       uint8_t src[8];
+       int i;
+
+       for (i=0; i<sizeof(src); i++) {
+               src[i] = i;
+       }
+       hex_encode_buf(buf, src, sizeof(src));
+       if (strcmp(buf, "0001020304050607") != 0) {
+               return false;
+       }
+       hex_encode_buf(buf, NULL, 0);
+       if (buf[0] != '\0') {
+               return false;
+       }
+       return true;
+}
+
+static const char *remove_duplicate_addrs2_test_strings_vector[] = {
+       "0.0.0.0",
+       "::0",
+       "1.2.3.1",
+       "0.0.0.0",
+       "0.0.0.0",
+       "1.2.3.2",
+       "1.2.3.3",
+       "1.2.3.4",
+       "1.2.3.5",
+       "::0",
+       "1.2.3.6",
+       "1.2.3.7",
+       "::0",
+       "::0",
+       "::0",
+       "1.2.3.8",
+       "1.2.3.9",
+       "1.2.3.10",
+       "1.2.3.11",
+       "1.2.3.12",
+       "1.2.3.13",
+       "1001:1111:1111:1000:0:1111:1111:1111",
+       "1.2.3.1",
+       "1.2.3.2",
+       "1.2.3.3",
+       "1.2.3.12",
+       "::0",
+       "::0"
+};
+
+static const char *remove_duplicate_addrs2_test_strings_result[] = {
+       "1.2.3.1",
+       "1.2.3.2",
+       "1.2.3.3",
+       "1.2.3.4",
+       "1.2.3.5",
+       "1.2.3.6",
+       "1.2.3.7",
+       "1.2.3.8",
+       "1.2.3.9",
+       "1.2.3.10",
+       "1.2.3.11",
+       "1.2.3.12",
+       "1.2.3.13",
+       "1001:1111:1111:1000:0:1111:1111:1111"
+};
+
+static bool run_local_remove_duplicate_addrs2(int dummy)
+{
+       struct ip_service test_vector[28];
+       int count, i;
+
+       /* Construct the sockaddr_storage test vector. */
+       for (i = 0; i < 28; i++) {
+               struct addrinfo hints;
+               struct addrinfo *res = NULL;
+               int ret;
+
+               memset(&hints, '\0', sizeof(hints));
+               hints.ai_flags = AI_NUMERICHOST;
+               ret = getaddrinfo(remove_duplicate_addrs2_test_strings_vector[i],
+                               NULL,
+                               &hints,
+                               &res);
+               if (ret) {
+                       fprintf(stderr, "getaddrinfo failed on [%s]\n",
+                               remove_duplicate_addrs2_test_strings_vector[i]);
+                       return false;
+               }
+               memset(&test_vector[i], '\0', sizeof(test_vector[i]));
+               memcpy(&test_vector[i].ss,
+                       res->ai_addr,
+                       res->ai_addrlen);
+               freeaddrinfo(res);
+       }
+
+       count = remove_duplicate_addrs2(test_vector, i);
+
+       if (count != 14) {
+               fprintf(stderr, "count wrong (%d) should be 14\n",
+                       count);
+               return false;
+       }
+
+       for (i = 0; i < count; i++) {
+               char addr[INET6_ADDRSTRLEN];
+
+               print_sockaddr(addr, sizeof(addr), &test_vector[i].ss);
+
+               if (strcmp(addr, remove_duplicate_addrs2_test_strings_result[i]) != 0) {
+                       fprintf(stderr, "mismatch on [%d] [%s] [%s]\n",
+                               i,
+                               addr,
+                               remove_duplicate_addrs2_test_strings_result[i]);
+                       return false;
+               }
+       }
+
+       printf("run_local_remove_duplicate_addrs2: success\n");
+       return true;
+}
+
 static double create_procs(bool (*fn)(int), bool *result)
 {
        int i, status;
@@ -8144,26 +8820,26 @@ static double create_procs(bool (*fn)(int), bool *result)
 
        synccount = 0;
 
-       child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*nprocs);
+       child_status = (volatile pid_t *)anonymous_shared_allocate(sizeof(pid_t)*torture_nprocs);
        if (!child_status) {
                printf("Failed to setup shared memory\n");
                return -1;
        }
 
-       child_status_out = (volatile bool *)shm_setup(sizeof(bool)*nprocs);
+       child_status_out = (volatile bool *)anonymous_shared_allocate(sizeof(bool)*torture_nprocs);
        if (!child_status_out) {
                printf("Failed to setup result status shared memory\n");
                return -1;
        }
 
-       for (i = 0; i < nprocs; i++) {
+       for (i = 0; i < torture_nprocs; i++) {
                child_status[i] = 0;
                child_status_out[i] = True;
        }
 
        start = timeval_current();
 
-       for (i=0;i<nprocs;i++) {
+       for (i=0;i<torture_nprocs;i++) {
                procnum = i;
                if (fork() == 0) {
                        pid_t mypid = getpid();
@@ -8191,15 +8867,15 @@ static double create_procs(bool (*fn)(int), bool *result)
 
        do {
                synccount = 0;
-               for (i=0;i<nprocs;i++) {
+               for (i=0;i<torture_nprocs;i++) {
                        if (child_status[i]) synccount++;
                }
-               if (synccount == nprocs) break;
+               if (synccount == torture_nprocs) break;
                smb_msleep(10);
        } while (timeval_elapsed(&start) < 30);
 
-       if (synccount != nprocs) {
-               printf("FAILED TO START %d CLIENTS (started %d)\n", nprocs, synccount);
+       if (synccount != torture_nprocs) {
+               printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
                *result = False;
                return timeval_elapsed(&start);
        }
@@ -8207,19 +8883,19 @@ static double create_procs(bool (*fn)(int), bool *result)
        /* start the client load */
        start = timeval_current();
 
-       for (i=0;i<nprocs;i++) {
+       for (i=0;i<torture_nprocs;i++) {
                child_status[i] = 0;
        }
 
-       printf("%d clients started\n", nprocs);
+       printf("%d clients started\n", torture_nprocs);
 
-       for (i=0;i<nprocs;i++) {
+       for (i=0;i<torture_nprocs;i++) {
                while (waitpid(0, &status, 0) == -1 && errno == EINTR) /* noop */ ;
        }
 
        printf("\n");
 
-       for (i=0;i<nprocs;i++) {
+       for (i=0;i<torture_nprocs;i++) {
                if (!child_status_out[i]) {
                        *result = False;
                }
@@ -8256,7 +8932,6 @@ static struct {
        {"NBENCH2", run_nbench2, 0},
        {"OPLOCK1",  run_oplock1, 0},
        {"OPLOCK2",  run_oplock2, 0},
-       {"OPLOCK3",  run_oplock3, 0},
        {"OPLOCK4",  run_oplock4, 0},
        {"DIR",  run_dirtest, 0},
        {"DIR1",  run_dirtest1, 0},
@@ -8302,17 +8977,35 @@ static struct {
        { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
        { "CHAIN1", run_chain1, 0},
        { "CHAIN2", run_chain2, 0},
+       { "CHAIN3", run_chain3, 0},
        { "WINDOWS-WRITE", run_windows_write, 0},
+       { "NTTRANS-CREATE", run_nttrans_create, 0},
+       { "NTTRANS-FSCTL", run_nttrans_fsctl, 0},
        { "CLI_ECHO", run_cli_echo, 0},
        { "GETADDRINFO", run_getaddrinfo_send, 0},
        { "TLDAP", run_tldap },
        { "STREAMERROR", run_streamerror },
        { "NOTIFY-BENCH", run_notify_bench },
+       { "NOTIFY-BENCH2", run_notify_bench2 },
+       { "NOTIFY-BENCH3", run_notify_bench3 },
        { "BAD-NBT-SESSION", run_bad_nbt_session },
        { "SMB-ANY-CONNECT", run_smb_any_connect },
+       { "NOTIFY-ONLINE", run_notify_online },
+       { "SMB2-BASIC", run_smb2_basic },
+       { "SMB2-NEGPROT", run_smb2_negprot },
+       { "SMB2-SESSION-RECONNECT", run_smb2_session_reconnect },
+       { "SMB2-TCON-DEPENDENCE", run_smb2_tcon_dependence },
+       { "SMB2-MULTI-CHANNEL", run_smb2_multi_channel },
+       { "SMB2-SESSION-REAUTH", run_smb2_session_reauth },
+       { "CLEANUP1", run_cleanup1 },
+       { "CLEANUP2", run_cleanup2 },
+       { "CLEANUP3", run_cleanup3 },
        { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
        { "LOCAL-GENCACHE", run_local_gencache, 0},
        { "LOCAL-TALLOC-DICT", run_local_talloc_dict, 0},
+       { "LOCAL-CTDB-CONN", run_ctdb_conn, 0},
+       { "LOCAL-MSG", run_msg_test, 0},
+       { "LOCAL-DBWRAP-WATCH1", run_dbwrap_watch1, 0 },
        { "LOCAL-BASE64", run_local_base64, 0},
        { "LOCAL-RBTREE", run_local_rbtree, 0},
        { "LOCAL-MEMCACHE", run_local_memcache, 0},
@@ -8323,6 +9016,11 @@ static struct {
        { "LOCAL-DBTRANS", run_local_dbtrans, 0},
        { "LOCAL-TEVENT-SELECT", run_local_tevent_select, 0},
        { "LOCAL-CONVERT-STRING", run_local_convert_string, 0},
+       { "LOCAL-CONV-AUTH-INFO", run_local_conv_auth_info, 0},
+       { "LOCAL-sprintf_append", run_local_sprintf_append, 0},
+       { "LOCAL-hex_encode_buf", run_local_hex_encode_buf, 0},
+       { "LOCAL-IDMAP-TDB-COMMON", run_idmap_tdb_common_test, 0},
+       { "LOCAL-remove_duplicate_addrs2", run_local_remove_duplicate_addrs2, 0},
        {NULL, NULL, 0}};
 
 
@@ -8403,6 +9101,7 @@ static void usage(void)
        printf("\t-p port\n");
        printf("\t-s seed\n");
        printf("\t-b unclist_filename   specify multiple shares for multiple connections\n");
+       printf("\t-f filename   filename to test\n");
        printf("\n\n");
 
        printf("tests are:");
@@ -8442,7 +9141,7 @@ static void usage(void)
                        set_dyn_CONFIGFILE(getenv("SMB_CONF_PATH"));
                }
        }
-       lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
+       lp_load_global(get_dyn_CONFIGFILE());
        load_interfaces();
 
        if (argc < 2) {
@@ -8480,7 +9179,8 @@ static void usage(void)
 
        fstrcpy(workgroup, lp_workgroup());
 
-       while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:")) != EOF) {
+       while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ll:d:Aec:ks:b:B:f:"))
+              != EOF) {
                switch (opt) {
                case 'p':
                        port_to_use = atoi(optarg);
@@ -8495,7 +9195,7 @@ static void usage(void)
                        max_protocol = interpret_protocol(optarg, max_protocol);
                        break;
                case 'N':
-                       nprocs = atoi(optarg);
+                       torture_nprocs = atoi(optarg);
                        break;
                case 'o':
                        torture_numops = atoi(optarg);
@@ -8549,6 +9249,9 @@ static void usage(void)
                case 'B':
                        torture_blocksize = atoi(optarg);
                        break;
+               case 'f':
+                       test_filename = SMB_STRDUP(optarg);
+                       break;
                default:
                        printf("Unknown option %c (%d)\n", (char)opt, opt);
                        usage();