libcli/smb: pass smbXcli_tcon to smb2cli_flush*()
[kai/samba.git] / source3 / torture / torture.c
index 8757904cd3c7b9c9901af480f1a8fe8934f1a9e0..a9edd6152058bc96bd3c4215d87d05090e2e1c2a 100644 (file)
@@ -28,7 +28,9 @@
 #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"
@@ -37,7 +39,8 @@
 #include "libsmb/nmblib.h"
 #include "../lib/util/tevent_ntstatus.h"
 #include "util_tdb.h"
-#include "libsmb/read_smb.h"
+#include "../libcli/smb/read_smb.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 extern char *optarg;
 extern int optind;
@@ -45,7 +48,7 @@ extern int optind;
 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;
@@ -55,12 +58,14 @@ 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;
@@ -180,19 +185,36 @@ static struct cli_state *open_nbt_connection(void)
 {
        struct cli_state *c;
        NTSTATUS status;
+       int flags = 0;
+
+       if (disable_spnego) {
+               flags |= CLI_FULL_CONNECTION_DONT_SPNEGO;
+       }
+
+       if (use_oplocks) {
+               flags |= CLI_FULL_CONNECTION_OPLOCKS;
+       }
+
+       if (use_level_II_oplocks) {
+               flags |= CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS;
+       }
+
+       if (use_kerberos) {
+               flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
+       }
+
+       if (force_dos_errors) {
+               flags |= CLI_FULL_CONNECTION_FORCE_DOS_ERRORS;
+       }
 
        status = cli_connect_nb(host, NULL, port_to_use, 0x20, myname,
-                               signing_state, &c);
+                               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;
+       cli_set_timeout(c, 120000); /* set a really long timeout (2 minutes) */
 
        return c;
 }
@@ -213,6 +235,8 @@ static bool cli_bad_session_request(int fd,
        bool ret = false;
        uint8_t message_type;
        uint8_t error;
+       struct event_context *ev;
+       struct tevent_req *req;
 
        frame = talloc_stackframe();
 
@@ -258,11 +282,24 @@ static bool cli_bad_session_request(int fd,
        if (len == -1) {
                goto fail;
        }
-       len = read_smb(fd, talloc_tos(), &inbuf, &err);
+
+       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);
 
        message_type = CVAL(inbuf, 0);
        if (message_type != 0x83) {
@@ -373,7 +410,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,
@@ -429,20 +466,20 @@ bool torture_init_connection(struct cli_state **pcli)
 
 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;
@@ -503,35 +540,35 @@ static bool check_both_error(int line, NTSTATUS status,
 
 
 /* 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;
                 }
         }
@@ -574,10 +611,10 @@ static bool rw_torture(struct cli_state *c)
 
        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",
@@ -597,7 +634,7 @@ static bool rw_torture(struct cli_state *c)
                        return False;
                }
 
-               status = cli_open(c, fname, O_RDWR | O_CREAT | O_TRUNC,
+               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));
@@ -676,7 +713,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);
 
@@ -695,7 +732,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;
 
@@ -715,7 +752,7 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
                               "not exist)\n", nt_errstr(status));
                }
 
-               status = cli_open(c, lockfname, O_RDWR | O_CREAT | O_EXCL,
+               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",
@@ -727,7 +764,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;
@@ -760,7 +797,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));
@@ -769,18 +806,15 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
                }
                else
                {
-                       sent = cli_read_old(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");
@@ -811,7 +845,7 @@ 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;
 
        status = cli_unlink(c1, lockfname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
@@ -819,7 +853,7 @@ static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
                printf("unlink failed (%s) (normal, this file should not exist)\n", nt_errstr(status));
        }
 
-       status = cli_open(c1, lockfname, O_RDWR | O_CREAT | O_EXCL,
+       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",
@@ -827,7 +861,7 @@ static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
                return False;
        }
 
-       status = 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, nt_errstr(status));
@@ -852,9 +886,15 @@ static bool rw_torture2(struct cli_state *c1, struct cli_state *c2)
                        break;
                }
 
-               if ((bytes_read = cli_read_old(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;
@@ -897,8 +937,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");
 
@@ -928,7 +968,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);
@@ -940,12 +980,12 @@ 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;
@@ -953,24 +993,14 @@ static bool run_readwritelarge_internal(int max_xmit_k)
        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, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL,
+       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));
@@ -1007,15 +1037,13 @@ static bool run_readwritelarge_internal(int max_xmit_k)
                correct = False;
        }
 
-       status = cli_open(cli1, lockfname, O_RDWR | O_CREAT | O_EXCL,
+       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);
 
        status = cli_qfileinfo_basic(cli1, fnum1, NULL, &fsize, NULL, NULL,
@@ -1063,15 +1091,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;
 }
 
@@ -1095,7 +1123,7 @@ static bool run_netbench(int client)
 
        nbio_id = client;
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        nb_setup(cli);
 
@@ -1184,7 +1212,7 @@ static bool run_nbench(int dummy)
        double t;
        bool correct = True;
 
-       nbio_shmem(nprocs);
+       nbio_shmem(torture_nprocs);
 
        nbio_id = -1;
 
@@ -1218,27 +1246,27 @@ static bool run_locktest1(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 locktest1\n");
 
        cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+       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_open(cli1, fname, O_RDWR, DENY_NONE, &fnum2);
+       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;
        }
 
-       status = cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum3);
+       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;
@@ -1351,20 +1379,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, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       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_state_get_tid(cli);
-       vuid1 = cli->vuid;
+       vuid1 = cli_state_get_uid(cli);
 
        status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1372,8 +1400,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));
@@ -1383,7 +1411,7 @@ static bool run_tcon_test(int dummy)
 
        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_state_set_tid(cli, cnum2);
@@ -1411,7 +1439,7 @@ static bool run_tcon_test(int dummy)
        }
 
        /* try a write with an invalid vuid */
-       cli->vuid = vuid2;
+       cli_state_set_uid(cli, vuid2);
        cli_state_set_tid(cli, cnum1);
 
        status = cli_writeall(cli, fnum1, 0, (uint8_t *)buf, 130, 4, NULL);
@@ -1424,7 +1452,7 @@ static bool run_tcon_test(int dummy)
        }
 
        cli_state_set_tid(cli, cnum1);
-       cli->vuid = vuid1;
+       cli_state_set_uid(cli, vuid1);
 
        status = cli_close(cli, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1463,7 +1491,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");
 
@@ -1498,8 +1526,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)) {
@@ -1526,8 +1554,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");
@@ -1621,7 +1648,7 @@ static bool run_locktest2(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting locktest2\n");
 
@@ -1629,13 +1656,13 @@ static bool run_locktest2(int dummy)
 
        cli_setpid(cli, 1);
 
-       status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       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_open(cli, fname, O_RDWR, DENY_NONE, &fnum2);
+       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;
@@ -1643,7 +1670,7 @@ static bool run_locktest2(int dummy)
 
        cli_setpid(cli, 2);
 
-       status = cli_open(cli, fname, O_RDWR, DENY_NONE, &fnum3);
+       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;
@@ -1783,21 +1810,21 @@ static bool run_locktest3(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 locktest3\n");
 
        cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+       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_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
+       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;
@@ -1902,6 +1929,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; \
         }
@@ -1923,15 +1972,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, 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));
 
@@ -2012,7 +2061,7 @@ static bool run_locktest4(int dummy)
 
 
        ret = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 120, 4, 0, WRITE_LOCK)) &&
-             (cli_read_old(cli2, fnum2, buf, 120, 4) == 4);
+             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");
 
@@ -2038,7 +2087,7 @@ static bool run_locktest4(int dummy)
        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_old(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));
@@ -2049,7 +2098,7 @@ static bool run_locktest4(int dummy)
              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_old(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");
 
@@ -2057,7 +2106,7 @@ static bool run_locktest4(int dummy)
              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_old(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");
 
@@ -2066,18 +2115,18 @@ static bool run_locktest4(int dummy)
              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_old(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);
+       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)) &&
+             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);
@@ -2112,16 +2161,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, 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));
 
@@ -2137,13 +2186,13 @@ static bool run_locktest5(int dummy)
        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);
+       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 = NT_STATUS_IS_OK(cli_lock32(cli1, fnum1, 0, 4, 0, WRITE_LOCK)) &&
@@ -2241,7 +2290,7 @@ static bool run_locktest6(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting locktest6\n");
 
@@ -2250,12 +2299,12 @@ static bool run_locktest6(int dummy)
 
                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));
@@ -2276,19 +2325,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, 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));
 
@@ -2303,14 +2353,21 @@ static bool run_locktest7(int dummy)
 
        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));
+               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_old(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");
@@ -2331,8 +2388,15 @@ static bool run_locktest7(int dummy)
 
        cli_setpid(cli1, 2);
 
-       if (cli_read_old(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");
        }
@@ -2361,8 +2425,14 @@ static bool run_locktest7(int dummy)
                printf("pid1 successfully locked range 130:4 for WRITE\n");
        }
 
-       if (cli_read_old(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");
@@ -2379,14 +2449,17 @@ static bool run_locktest7(int dummy)
 
        cli_setpid(cli1, 2);
 
-       if (cli_read_old(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;
        }
 
@@ -2436,24 +2509,24 @@ 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, 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", nt_errstr(status));
+               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",
+               d_fprintf(stderr, "cli_openx second time returned %s\n",
                          nt_errstr(status));
                goto fail;
        }
@@ -2471,9 +2544,9 @@ static bool run_locktest8(int dummy)
                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",
+               d_fprintf(stderr, "cli_openx third time returned %s\n",
                           nt_errstr(status));
                 goto fail;
         }
@@ -2498,7 +2571,7 @@ fail:
  */
 
 static bool got_alarm;
-static int alarm_fd;
+static struct cli_state *alarm_cli;
 
 static void alarm_handler(int dummy)
 {
@@ -2507,7 +2580,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)
@@ -2631,12 +2704,12 @@ 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", nt_errstr(status));
+               d_fprintf(stderr, "cli_openx returned %s\n", nt_errstr(status));
                return false;
        }
 
@@ -2658,7 +2731,7 @@ 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);
 
@@ -2710,14 +2783,14 @@ 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, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+       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));
@@ -2731,14 +2804,13 @@ static bool run_fdpasstest(int dummy)
                return False;
        }
 
-       cli2->vuid = cli1->vuid;
+       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_old(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);
@@ -2768,13 +2840,13 @@ 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_state_get_tid(cli);
-       if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, share, "?????", "", 1)))
+       if (!NT_STATUS_IS_OK(cli_tree_connect(cli, share, "?????", "", 1)))
                return False;
        new_cnum = cli_state_get_tid(cli);
        cli_state_set_tid(cli, saved_cnum);
@@ -2784,7 +2856,7 @@ static bool run_fdsesstest(int dummy)
        cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_unlink(cli, fname1, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       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;
@@ -2797,16 +2869,16 @@ static bool run_fdsesstest(int dummy)
                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_old(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, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
@@ -2816,15 +2888,14 @@ static bool run_fdsesstest(int dummy)
                ret = False;
        }
 
-       cli->vuid = saved_vuid;
+       cli_state_set_uid(cli, saved_vuid);
 
        /* Try with same vuid, different cnum. */
        cli_state_set_tid(cli, new_cnum);
 
-       if (cli_read_old(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_state_set_tid(cli, saved_cnum);
@@ -2854,7 +2925,7 @@ static bool run_unlinktest(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting unlink test\n");
 
@@ -2862,7 +2933,7 @@ static bool run_unlinktest(int dummy)
 
        cli_setpid(cli, 1);
 
-       status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       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;
@@ -2874,7 +2945,7 @@ static bool run_unlinktest(int dummy)
                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);
        }
 
@@ -2911,11 +2982,11 @@ 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,"\\maxfid.%d.%d", i,(int)getpid());
-               status = cli_open(cli, fname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE,
+               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", 
@@ -2982,7 +3053,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;
@@ -3024,7 +3096,7 @@ static bool run_bad_nbt_session(int dummy)
                return false;
        }
 
-       status = open_socket_out(&ss, 139, 10000, &fd);
+       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));
@@ -3154,7 +3226,7 @@ static bool run_attrtest(int dummy)
        }
 
        cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
-       cli_open(cli, fname, 
+       cli_openx(cli, fname, 
                        O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
        cli_close(cli, fnum);
 
@@ -3211,7 +3283,7 @@ 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";
@@ -3236,7 +3308,7 @@ static bool run_trans2test(int dummy)
        }
 
        cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
-       cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
+       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)) {
@@ -3261,7 +3333,7 @@ static bool run_trans2test(int dummy)
        sleep(2);
 
        cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
-       status = cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE,
+       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));
@@ -3275,18 +3347,20 @@ 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;
                }
@@ -3294,7 +3368,7 @@ static bool run_trans2test(int dummy)
 
 
        cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
-       cli_open(cli, fname, 
+       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,
@@ -3328,7 +3402,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);
@@ -3367,7 +3441,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, NULL, &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));
@@ -3394,7 +3468,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++) {
@@ -3432,11 +3506,11 @@ static bool run_oplock1(int dummy)
 
        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;
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+       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));
@@ -3478,6 +3552,7 @@ 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));
@@ -3494,24 +3569,18 @@ 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, 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);
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+       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));
@@ -3524,7 +3593,7 @@ static bool run_oplock2(int dummy)
 
        if (fork() == 0) {
                /* Child code */
-               status = cli_open(cli2, fname, O_RDWR, DENY_NONE, &fnum2);
+               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;
@@ -3546,10 +3615,14 @@ static bool run_oplock2(int dummy)
 
        /* Ensure cli1 processes the break. Empty file should always return 0
         * bytes.  */
-
-       if (cli_read_old(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. */
@@ -3574,7 +3647,7 @@ static bool run_oplock2(int dummy)
 
        sleep(2);
 
-       cli_read_old(cli1, fnum1, buf, 0, 4);
+       cli_read(cli1, fnum1, buf, 0, 4, NULL);
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3646,11 +3719,11 @@ static bool run_oplock4(int dummy)
        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. */
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
+       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));
@@ -3671,13 +3744,13 @@ static bool run_oplock4(int dummy)
        }
 
        /* Prove that opening hardlinks cause deny modes to conflict. */
-       status = cli_open(cli1, fname, O_RDWR, DENY_ALL, &fnum1);
+       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);
@@ -3697,12 +3770,9 @@ static bool run_oplock4(int dummy)
        }
 
        cli1->use_oplocks = true;
-       cli1->use_level_II_oplocks = true;
-
        cli2->use_oplocks = true;
-       cli2->use_level_II_oplocks = true;
 
-       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)) {
                printf("open of %s failed (%s)\n", fname, nt_errstr(status));
                return false;
@@ -3710,7 +3780,7 @@ static bool run_oplock4(int dummy)
 
        ev = tevent_context_init(talloc_tos());
        if (ev == NULL) {
-               printf("tevent_req_create failed\n");
+               printf("tevent_context_init failed\n");
                return false;
        }
 
@@ -3732,10 +3802,10 @@ static bool run_oplock4(int dummy)
        }
        tevent_req_set_callback(oplock_req, oplock4_got_break, state);
 
-       open_req = cli_open_send(
+       open_req = cli_openx_send(
                talloc_tos(), ev, cli2, fname_ln, O_RDWR, DENY_NONE);
-       if (oplock_req == NULL) {
-               printf("cli_open_send failed\n");
+       if (open_req == NULL) {
+               printf("cli_openx_send failed\n");
                return false;
        }
        tevent_req_set_callback(open_req, oplock4_got_open, state);
@@ -3821,9 +3891,9 @@ static void oplock4_got_open(struct tevent_req *req)
                req, struct oplock4_state);
        NTSTATUS status;
 
-       status = cli_open_recv(req, state->fnum2);
+       status = cli_openx_recv(req, state->fnum2);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("cli_open_recv returned %s\n", nt_errstr(status));
+               printf("cli_openx_recv returned %s\n", nt_errstr(status));
                *state->fnum2 = 0xffff;
        }
 }
@@ -3847,7 +3917,7 @@ 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. */
 
@@ -3870,7 +3940,7 @@ static bool run_deletetest(int dummy)
                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;
@@ -3906,7 +3976,7 @@ static bool run_deletetest(int dummy)
                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);
                status = cli_close(cli1, fnum1);
                if (!NT_STATUS_IS_OK(status)) {
@@ -3976,7 +4046,7 @@ static bool run_deletetest(int dummy)
 
        /* This should fail - file should no longer be there. */
 
-       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("[3] open of %s succeeded should have been deleted on close !\n", fname);
                status = cli_close(cli1, fnum1);
                if (!NT_STATUS_IS_OK(status)) {
@@ -4049,7 +4119,7 @@ static bool run_deletetest(int dummy)
        cli_setatr(cli1, fname, 0, 0);
        cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1);
+       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;
@@ -4139,7 +4209,7 @@ static bool run_deletetest(int dummy)
        }
 
        /* This next open should succeed - we reset the flag. */
-       status = 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("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = False;
@@ -4165,7 +4235,7 @@ static bool run_deletetest(int dummy)
                goto fail;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        status = cli_ntcreate(cli1, fname, 0,
                             FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
@@ -4210,7 +4280,7 @@ static bool run_deletetest(int dummy)
        }
 
        /* This should fail.. */
-       status = 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;
@@ -4248,7 +4318,7 @@ static bool run_deletetest(int dummy)
        }
 
        /* 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;
@@ -4279,17 +4349,19 @@ static bool run_deletetest(int dummy)
        }
 
        /* 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 {
@@ -4338,10 +4410,10 @@ static bool run_deletetest_ln(int dummy)
        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. */
-       status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       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;
@@ -4448,9 +4520,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;
@@ -4752,7 +4824,7 @@ static bool run_pipe_number(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
        while(1) {
                status = cli_ntcreate(cli1, pipe_name, 0, FILE_READ_DATA,
                                      FILE_ATTRIBUTE_NORMAL,
@@ -4781,7 +4853,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;
@@ -4795,9 +4867,9 @@ static bool run_opentest(int dummy)
        cli_setatr(cli1, fname, 0, 0);
        cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       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;
@@ -4815,16 +4887,16 @@ static bool run_opentest(int dummy)
                return False;
        }
 
-       status = cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
+       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");
        }
@@ -4837,16 +4909,16 @@ static bool run_opentest(int dummy)
 
        cli_setatr(cli1, fname, 0, 0);
 
-       status = cli_open(cli1, fname, O_RDONLY, DENY_WRITE, &fnum1);
+       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");
        }
@@ -4862,7 +4934,7 @@ static bool run_opentest(int dummy)
        printf("finished open test 2\n");
 
        /* Test truncate open disposition on file opened for read. */
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
+       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;
@@ -4897,7 +4969,7 @@ static bool run_opentest(int dummy)
        }
 
        /* Now test if we can truncate a file opened for readonly. */
-       status = cli_open(cli1, fname, O_RDONLY|O_TRUNC, DENY_NONE, &fnum1);
+       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;
@@ -4951,7 +5023,7 @@ static bool run_opentest(int dummy)
        cli_setatr(cli2, fname, 0, 0);
        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");
        status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
@@ -5273,6 +5345,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");
 
@@ -5280,7 +5353,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)) {
@@ -5329,6 +5402,13 @@ 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. */
        status = cli_ftruncate(cli1, fnum1, 0);
        if (!NT_STATUS_IS_OK(status)) {
@@ -5492,8 +5572,14 @@ static bool run_simple_posix_open_test(int dummy)
                goto out;
        }
 
-       if (cli_read_old(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;
        }
 
@@ -5553,6 +5639,26 @@ static bool run_simple_posix_open_test(int dummy)
                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;
+       }
+
        printf("Simple POSIX open test passed\n");
        correct = true;
 
@@ -5652,7 +5758,7 @@ 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);
@@ -5777,13 +5883,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;
                }
@@ -5863,7 +5969,7 @@ bool torture_ioctl_test(int dummy)
 
        cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       status = cli_open(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       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;
@@ -5931,7 +6037,7 @@ bool torture_chkpath_test(int dummy)
                return False;
        }
 
-       status = cli_open(cli, "\\chkpath.dir\\foo.txt", O_RDWR|O_CREAT|O_EXCL,
+       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));
@@ -5953,23 +6059,25 @@ bool torture_chkpath_test(int dummy)
 
        status = cli_chkpath(cli, "\\chkpath.dir\\foo.txt");
        if (!NT_STATUS_IS_OK(status)) {
-               ret = check_error(__LINE__, cli, ERRDOS, ERRbadpath, 
+               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");
@@ -6103,10 +6211,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;
        }
@@ -6133,7 +6241,7 @@ 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\\*", FILE_ATTRIBUTE_DIRECTORY, del_fn, cli);
@@ -6220,13 +6328,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,
@@ -6243,14 +6353,18 @@ static bool run_error_map_extract(int dummy) {
 
        /* 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));
@@ -6265,6 +6379,9 @@ static bool run_error_map_extract(int dummy) {
                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);
 
@@ -6277,11 +6394,11 @@ static bool run_error_map_extract(int dummy) {
                }
 
                /* Case #1: 32-bit NT errors */
-               if (cli_is_nt_error(c_nt)) {
-                       nt_status = cli_nt_error(c_nt);
+               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);
                }
 
@@ -6294,12 +6411,13 @@ static bool run_error_map_extract(int dummy) {
                }
 
                /* Case #1: 32-bit NT errors */
-               if (!cli_is_dos_error(c_dos)) {
+               if (NT_STATUS_IS_DOS(status)) {
                        printf("/** NT error on DOS connection! (%s) */\n", 
-                              cli_errstr(c_dos));
+                              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) { 
@@ -6348,7 +6466,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)) {
@@ -6356,7 +6474,6 @@ static bool run_sesssetup_bench(int dummy)
                                 __location__, nt_errstr(status));
                        return false;
                }
-               c->vuid = 0;
        }
 
        return true;
@@ -6385,10 +6502,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);
 }
@@ -6432,9 +6549,9 @@ 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);
@@ -6450,13 +6567,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);
@@ -6489,12 +6606,12 @@ static bool run_chain2(int dummy)
 
        printf("starting chain2 test\n");
        status = cli_start_connection(&cli1, lp_netbios_name(), host, NULL,
-                                     port_to_use, Undefined, 0);
+                                     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]);
@@ -6506,13 +6623,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);
@@ -6930,7 +7047,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");
@@ -6938,7 +7055,7 @@ static bool run_mangle1(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
@@ -6957,9 +7074,9 @@ static bool run_mangle1(int dummy)
        }
        d_printf("alt_name: %s\n", alt_name);
 
-       status = cli_open(cli, alt_name, O_RDONLY, DENY_NONE, &fnum);
+       status = cli_openx(cli, alt_name, O_RDONLY, DENY_NONE, &fnum);
        if (!NT_STATUS_IS_OK(status)) {
-               d_printf("cli_open(%s) failed: %s\n", alt_name,
+               d_printf("cli_openx(%s) failed: %s\n", alt_name,
                         nt_errstr(status));
                return false;
        }
@@ -7008,13 +7125,13 @@ static bool run_windows_write(int dummy)
                return False;
        }
 
-       status = cli_open(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
+       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();
 
@@ -7062,7 +7179,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));
 
@@ -7086,10 +7203,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)) {
@@ -7099,7 +7216,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");
@@ -7110,8 +7227,8 @@ 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;
                }
@@ -7120,7 +7237,7 @@ static bool run_uid_regression_test(int dummy)
        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);
@@ -7133,7 +7250,7 @@ static bool run_uid_regression_test(int dummy)
                goto out;
        }
 
-       cli->vuid = old_vuid;
+       cli_state_set_uid(cli, old_vuid);
        cli_state_set_tid(cli, old_cnum);
 
        /* This should fail. */
@@ -7144,7 +7261,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;
@@ -7229,7 +7346,7 @@ 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");
@@ -7238,7 +7355,7 @@ 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\\*", FILE_ATTRIBUTE_DIRECTORY, shortname_del_fn, cli);
@@ -7252,8 +7369,14 @@ static bool run_shortname_test(int dummy)
                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;
 
@@ -7453,10 +7576,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;
        }
 
@@ -7494,7 +7617,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;
 
@@ -7511,10 +7634,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",
@@ -7688,26 +7809,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;
        }
@@ -7883,7 +8007,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) {
@@ -7897,12 +8022,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;
        }
 
@@ -7910,6 +8037,10 @@ static bool run_local_talloc_dict(int dummy)
                return false;
        }
 
+       if (count != res) {
+               return false;
+       }
+
        TALLOC_FREE(dict);
 
        return true;
@@ -8227,7 +8358,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");
@@ -8334,7 +8465,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;
        }
@@ -8342,9 +8473,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;
@@ -8362,8 +8493,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;
@@ -8428,28 +8559,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));
@@ -8469,29 +8602,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);
+       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);
@@ -8504,7 +8640,7 @@ static bool run_local_dbtrans(int dummy)
 
        TALLOC_FREE(rec);
 
-       res = db->transaction_commit(db);
+       res = dbwrap_transaction_commit(db);
        if (res != 0) {
                printf(__location__ "transaction_commit failed\n");
                return false;
@@ -8514,14 +8650,16 @@ static bool run_local_dbtrans(int dummy)
                uint32_t val, val2;
                int i;
 
-               res = db->transaction_start(db);
+               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;
                }
 
@@ -8531,8 +8669,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;
                }
 
@@ -8544,7 +8684,7 @@ static bool run_local_dbtrans(int dummy)
 
                printf("val2=%d\r", val2);
 
-               res = db->transaction_commit(db);
+               res = dbwrap_transaction_commit(db);
                if (res != 0) {
                        printf(__location__ "transaction_commit failed\n");
                        break;
@@ -8597,6 +8737,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;
@@ -8608,26 +8871,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 *)shm_setup(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 *)shm_setup(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();
@@ -8655,15 +8918,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);
        }
@@ -8671,19 +8934,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;
                }
@@ -8765,20 +9028,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},
@@ -8789,6 +9067,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}};
 
 
@@ -8909,7 +9192,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) {
@@ -8963,7 +9246,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);