Convert all uses of uint32/16/8 to _t in source3/torture.
[mat/samba.git] / source3 / torture / torture.c
index 530e935a5c22b5932416d863511ba63f07921e9b..d5ec4c6fed1aa78333d4b05d5343eec95c034cc4 100644 (file)
@@ -26,7 +26,7 @@
 #include "tldap.h"
 #include "tldap_util.h"
 #include "../librpc/gen_ndr/svcctl.h"
-#include "memcache.h"
+#include "../lib/util/memcache.h"
 #include "nsswitch/winbind_client.h"
 #include "dbwrap/dbwrap.h"
 #include "dbwrap/dbwrap_open.h"
 #include "../lib/util/tevent_ntstatus.h"
 #include "util_tdb.h"
 #include "../libcli/smb/read_smb.h"
+#include "../libcli/smb/smbXcli_base.h"
+#include "lib/sys_rw_data.h"
 
 extern char *optarg;
 extern int optind;
 
 fstring host, workgroup, share, password, username, myname;
-static int max_protocol = PROTOCOL_NT1;
 static const char *sockops="TCP_NODELAY";
 int torture_nprocs=1;
 static int port_to_use=0;
@@ -64,68 +65,13 @@ 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 = SMB_SIGNING_DEFAULT;
+static enum smb_signing_setting signing_state = SMB_SIGNING_DEFAULT;
 char *test_filename;
 
 bool torture_showall = False;
 
 static double create_procs(bool (*fn)(int), bool *result);
 
-
-/* return a pointer to a anonymous shared memory segment of size "size"
-   which will persist across fork() but will disappear when all processes
-   exit 
-
-   The memory is not zeroed 
-
-   This function uses system5 shared memory. It takes advantage of a property
-   that the memory is not destroyed if it is attached when the id is removed
-   */
-void *shm_setup(int size)
-{
-       int shmid;
-       void *ret;
-
-#ifdef __QNXNTO__
-       shmid = shm_open("private", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
-       if (shmid == -1) {
-               printf("can't get shared memory\n");
-               exit(1);
-       }
-       shm_unlink("private");
-       if (ftruncate(shmid, size) == -1) {
-               printf("can't set shared memory size\n");
-               exit(1);
-       }
-       ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
-       if (ret == MAP_FAILED) {
-               printf("can't map shared memory\n");
-               exit(1);
-       }
-#else
-       shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
-       if (shmid == -1) {
-               printf("can't get shared memory\n");
-               exit(1);
-       }
-       ret = (void *)shmat(shmid, 0, 0);
-       if (!ret || ret == (void *)-1) {
-               printf("can't attach to shared memory\n");
-               return NULL;
-       }
-       /* the following releases the ipc, but note that this process
-          and all its children will still have access to the memory, its
-          just that the shmid is no longer valid for other shm calls. This
-          means we don't leave behind lots of shm segments after we exit 
-
-          See Stevens "advanced programming in unix env" for details
-          */
-       shmctl(shmid, IPC_RMID, 0);
-#endif
-
-       return ret;
-}
-
 /********************************************************************
  Ensure a connection is encrypted.
 ********************************************************************/
@@ -133,8 +79,8 @@ void *shm_setup(int size)
 static bool force_cli_encryption(struct cli_state *c,
                        const char *sharename)
 {
-       uint16 major, minor;
-       uint32 caplow, caphigh;
+       uint16_t major, minor;
+       uint32_t caplow, caphigh;
        NTSTATUS status;
 
        if (!SERVER_HAS_UNIX_CIFS(c)) {
@@ -234,7 +180,7 @@ static bool cli_bad_session_request(int fd,
        bool ret = false;
        uint8_t message_type;
        uint8_t error;
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
 
        frame = talloc_stackframe();
@@ -282,7 +228,7 @@ static bool cli_bad_session_request(int fd,
                goto fail;
        }
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -463,26 +409,21 @@ bool torture_init_connection(struct cli_state **pcli)
        return true;
 }
 
-bool torture_cli_session_setup2(struct cli_state *cli, uint16 *new_vuid)
+bool torture_cli_session_setup2(struct cli_state *cli, uint16_t *new_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_state_set_uid(cli, 0);
-       ret = NT_STATUS_IS_OK(cli_session_setup(cli, username,
-                                               password, passlen,
-                                               password, passlen,
-                                               workgroup));
+       status = cli_session_setup(cli, username,
+                                  password, passlen,
+                                  password, passlen,
+                                  workgroup);
+       ret = NT_STATUS_IS_OK(status);
        *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;
-       }
        return ret;
 }
 
@@ -506,11 +447,11 @@ bool torture_close_connection(struct cli_state *c)
 
 /* check if the server produced the expected dos or nt error code */
 static bool check_both_error(int line, NTSTATUS status,
-                            uint8 eclass, uint32 ecode, NTSTATUS nterr)
+                            uint8_t eclass, uint32_t ecode, NTSTATUS nterr)
 {
        if (NT_STATUS_IS_DOS(status)) {
-               uint8 cclass;
-               uint32 num;
+               uint8_t cclass;
+               uint32_t num;
 
                /* Check DOS error */
                cclass = NT_STATUS_DOS_CLASS(status);
@@ -540,11 +481,11 @@ static bool check_both_error(int line, NTSTATUS status,
 
 /* check if the server produced the expected error code */
 static bool check_error(int line, NTSTATUS status,
-                       uint8 eclass, uint32 ecode, NTSTATUS nterr)
+                       uint8_t eclass, uint32_t ecode, NTSTATUS nterr)
 {
        if (NT_STATUS_IS_DOS(status)) {
-                uint8 cclass;
-                uint32 num;
+                uint8_t cclass;
+                uint32_t num;
 
                 /* Check DOS error */
 
@@ -576,7 +517,7 @@ static bool check_error(int line, NTSTATUS status,
 }
 
 
-static bool wait_lock(struct cli_state *c, int fnum, uint32 offset, uint32 len)
+static bool wait_lock(struct cli_state *c, int fnum, uint32_t offset, uint32_t len)
 {
        NTSTATUS status;
 
@@ -712,7 +653,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);
 
@@ -736,7 +677,7 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
        NTSTATUS status = NT_STATUS_OK;
 
        srandom(1);
-       for (i = 0; i < sizeof(buf); i += sizeof(uint32))
+       for (i = 0; i < sizeof(buf); i += sizeof(uint32_t))
        {
                SIVAL(buf, i, sys_random());
        }
@@ -765,7 +706,7 @@ static bool rw_torture3(struct cli_state *c, char *lockfname)
                {
                        status = cli_openx(c, lockfname, O_RDONLY, 
                                         DENY_NONE, &fnum);
-                       if (!NT_STATUS_IS_OK(status)) {
+                       if (NT_STATUS_IS_OK(status)) {
                                break;
                        }
                        smb_msleep(10);
@@ -936,8 +877,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");
 
@@ -967,7 +908,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);
@@ -992,7 +933,7 @@ static bool run_readwritelarge_internal(void)
        if (!torture_open_connection(&cli1, 0)) {
                return False;
        }
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
        memset(buf,'\0',sizeof(buf));
 
        printf("starting readwritelarge_internal\n");
@@ -1122,7 +1063,7 @@ static bool run_netbench(int client)
 
        nbio_id = client;
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        nb_setup(cli);
 
@@ -1245,8 +1186,8 @@ 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");
 
@@ -1366,9 +1307,9 @@ static bool run_tcon_test(int dummy)
 {
        static struct cli_state *cli;
        const char *fname = "\\tcontest.tmp";
-       uint16 fnum1;
-       uint16 cnum1, cnum2, cnum3;
-       uint16 vuid1, vuid2;
+       uint16_t fnum1;
+       uint16_t cnum1, cnum2, cnum3;
+       uint16_t vuid1, vuid2;
        char buf[4];
        bool ret = True;
        NTSTATUS status;
@@ -1378,7 +1319,7 @@ 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");
 
@@ -1483,14 +1424,14 @@ static bool run_tcon_test(int dummy)
 static bool run_tcon2_test(int dummy)
 {
        static struct cli_state *cli;
-       uint16 cnum, max_xmit;
+       uint16_t cnum, max_xmit;
        char *service;
        NTSTATUS status;
 
        if (!torture_open_connection(&cli, 0)) {
                return False;
        }
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting tcon2 test\n");
 
@@ -1647,7 +1588,7 @@ static bool run_locktest2(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting locktest2\n");
 
@@ -1800,17 +1741,17 @@ static bool run_locktest3(int dummy)
        const char *fname = "\\lockt3.lck";
        uint16_t fnum1, fnum2;
        int i;
-       uint32 offset;
+       uint32_t offset;
        bool correct = True;
        NTSTATUS status;
 
-#define NEXT_OFFSET offset += (~(uint32)0) / torture_numops
+#define NEXT_OFFSET offset += (~(uint32_t)0) / torture_numops
 
        if (!torture_open_connection(&cli1, 0) || !torture_open_connection(&cli2, 1)) {
                return False;
        }
-       cli_sockopt(cli1, sockops);
-       cli_sockopt(cli2, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+       smbXcli_conn_set_sockopt(cli2->conn, sockops);
 
        printf("starting locktest3\n");
 
@@ -1971,8 +1912,8 @@ 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");
 
@@ -2160,8 +2101,8 @@ 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");
 
@@ -2289,7 +2230,7 @@ static bool run_locktest6(int dummy)
                return False;
        }
 
-       cli_sockopt(cli, sockops);
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
 
        printf("starting locktest6\n");
 
@@ -2331,7 +2272,7 @@ static bool run_locktest7(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        printf("starting locktest7\n");
 
@@ -2508,7 +2449,7 @@ static bool run_locktest8(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        printf("starting locktest8\n");
 
@@ -2579,7 +2520,7 @@ static void alarm_handler(int dummy)
 
 static void alarm_handler_parent(int dummy)
 {
-       cli_state_disconnect(alarm_cli);
+       smbXcli_conn_disconnect(alarm_cli->conn, NT_STATUS_OK);
 }
 
 static void do_local_lock(int read_fd, int write_fd)
@@ -2703,7 +2644,7 @@ static bool run_locktest9(int dummy)
                return false;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        status = cli_openx(cli1, fname, O_RDWR, DENY_NONE,
                          &fnum);
@@ -2782,8 +2723,8 @@ 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");
 
@@ -2825,10 +2766,10 @@ static bool run_fdpasstest(int dummy)
 static bool run_fdsesstest(int dummy)
 {
        struct cli_state *cli;
-       uint16 new_vuid;
-       uint16 saved_vuid;
-       uint16 new_cnum;
-       uint16 saved_cnum;
+       uint16_t new_vuid;
+       uint16_t saved_vuid;
+       uint16_t new_cnum;
+       uint16_t saved_cnum;
        const char *fname = "\\fdsess.tst";
        const char *fname1 = "\\fdsess1.tst";
        uint16_t fnum1;
@@ -2839,7 +2780,7 @@ 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;
@@ -2924,7 +2865,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");
 
@@ -2981,7 +2922,7 @@ 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());
@@ -3039,7 +2980,7 @@ static bool run_negprot_nowait(int dummy)
 
        printf("starting negprot nowait test\n");
 
-       ev = tevent_context_init(talloc_tos());
+       ev = samba_tevent_context_init(talloc_tos());
        if (ev == NULL) {
                return false;
        }
@@ -3052,7 +2993,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, PROTOCOL_NT1);
+               req = smbXcli_negprot_send(ev, ev, cli->conn, cli->timeout,
+                                          PROTOCOL_CORE, PROTOCOL_NT1);
                if (req == NULL) {
                        TALLOC_FREE(ev);
                        return false;
@@ -3141,7 +3083,7 @@ static bool run_randomipc(int dummy)
 
                cli_api(cli, 
                        param, param_len, 8,  
-                       NULL, 0, BUFFER_SIZE, 
+                       NULL, 0, CLI_BUFFER_SIZE,
                        &rparam, &rprcnt,     
                        &rdata, &rdrcnt);
                if (i % 100 == 0) {
@@ -3154,6 +3096,9 @@ static bool run_randomipc(int dummy)
                correct = False;
        }
 
+       SAFE_FREE(rparam);
+       SAFE_FREE(rdata);
+
        printf("finished random ipc test\n");
 
        return correct;
@@ -3161,7 +3106,7 @@ static bool run_randomipc(int dummy)
 
 
 
-static void browse_callback(const char *sname, uint32 stype, 
+static void browse_callback(const char *sname, uint32_t stype,
                            const char *comment, void *state)
 {
        printf("\t%20.20s %08x %s\n", sname, stype, comment);
@@ -3435,7 +3380,7 @@ static bool run_trans2test(int dummy)
 static NTSTATUS new_trans(struct cli_state *pcli, int fnum, int level)
 {
        uint8_t *buf = NULL;
-       uint32 len;
+       uint32_t len;
        NTSTATUS status;
 
        status = cli_qfileinfo(talloc_tos(), pcli, fnum, level, 0,
@@ -3445,7 +3390,7 @@ static NTSTATUS new_trans(struct cli_state *pcli, int fnum, int level)
                       nt_errstr(status));
        } else {
                printf("qfileinfo: level %d, len = %u\n", level, len);
-               dump_data(0, (uint8 *)buf, len);
+               dump_data(0, (uint8_t *)buf, len);
                printf("\n");
        }
        TALLOC_FREE(buf);
@@ -3504,7 +3449,7 @@ 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;
 
@@ -3553,7 +3498,7 @@ static bool run_oplock2(int dummy)
        size_t nread;
        NTSTATUS status;
 
-       shared_correct = (volatile bool *)shm_setup(sizeof(bool));
+       shared_correct = (volatile bool *)anonymous_shared_allocate(sizeof(bool));
        *shared_correct = True;
 
        use_level_II_oplocks = True;
@@ -3575,8 +3520,8 @@ static bool run_oplock2(int dummy)
 
        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_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
                          &fnum1);
@@ -3717,8 +3662,8 @@ 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_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE,
@@ -3776,7 +3721,7 @@ static bool run_oplock4(int dummy)
                return false;
        }
 
-       ev = tevent_context_init(talloc_tos());
+       ev = samba_tevent_context_init(talloc_tos());
        if (ev == NULL) {
                printf("tevent_context_init failed\n");
                return false;
@@ -3906,7 +3851,7 @@ static bool run_deletetest(int dummy)
        const char *fname = "\\delete.file";
        uint16_t fnum1 = (uint16_t)-1;
        uint16_t fnum2 = (uint16_t)-1;
-       bool correct = True;
+       bool correct = false;
        NTSTATUS status;
 
        printf("starting delete test\n");
@@ -3915,7 +3860,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. */
 
@@ -3924,23 +3869,21 @@ static bool run_deletetest(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
-                             FILE_DELETE_ON_CLOSE, 0, &fnum1);
+                             FILE_DELETE_ON_CLOSE, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[1] open of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[1] close failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
-       if (NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1))) {
+       status = cli_openx(cli1, fname, O_RDWR, DENY_NONE, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[1] open of %s succeeded (should fail)\n", fname);
-               correct = False;
                goto fail;
        }
 
@@ -3953,38 +3896,36 @@ static bool run_deletetest(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[2] open of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_nt_delete_on_close(cli1, fnum1, true);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[2] setting delete_on_close failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[2] close failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
-       if (NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                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)) {
                        printf("[2] close failed (%s)\n", nt_errstr(status));
-                       correct = False;
-                       goto fail;
                }
                cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
-       } else
-               printf("second delete on close test succeeded.\n");
+               goto fail;
+       }
+
+       printf("second delete on close test succeeded.\n");
 
        /* Test 3 - ... */
        cli_setatr(cli1, fname, 0, 0);
@@ -3993,20 +3934,21 @@ static bool run_deletetest(int dummy)
        status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
                              FILE_ATTRIBUTE_NORMAL,
                              FILE_SHARE_READ|FILE_SHARE_WRITE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[3] open - 1 of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        /* This should fail with a sharing violation - open for delete is only compatible
           with SHARE_DELETE. */
 
-       if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                       FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0, 0, &fnum2))) {
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE,
+                             FILE_OPEN, 0, 0, &fnum2, NULL);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[3] open  - 2 of %s succeeded - should have failed.\n", fname);
-               correct = False;
                goto fail;
        }
 
@@ -4014,47 +3956,44 @@ static bool run_deletetest(int dummy)
        status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
                             FILE_ATTRIBUTE_NORMAL,
                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                            FILE_OPEN, 0, 0, &fnum2);
+                            FILE_OPEN, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("[3] open  - 2 of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
+               printf("[3] open  - 3 of %s failed (%s)\n", fname, nt_errstr(status));
                goto fail;
        }
 
        status = cli_nt_delete_on_close(cli1, fnum1, true);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[3] setting delete_on_close failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[3] close 1 failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum2);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[3] close 2 failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        /* This should fail - file should no longer be there. */
 
-       if (NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[3] open of %s succeeded should have been deleted on close !\n", fname);
                status = cli_close(cli1, fnum1);
                if (!NT_STATUS_IS_OK(status)) {
                        printf("[3] close failed (%s)\n", nt_errstr(status));
                }
                cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
-               correct = False;
                goto fail;
-       } else
-               printf("third delete on close test succeeded.\n");
+       }
+
+       printf("third delete on close test succeeded.\n");
 
        /* Test 4 ... */
        cli_setatr(cli1, fname, 0, 0);
@@ -4064,10 +4003,9 @@ static bool run_deletetest(int dummy)
                              FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
                              FILE_ATTRIBUTE_NORMAL,
                              FILE_SHARE_READ|FILE_SHARE_WRITE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[4] open of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
@@ -4075,44 +4013,42 @@ static bool run_deletetest(int dummy)
        status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
                             FILE_ATTRIBUTE_NORMAL,
                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                            FILE_OPEN, 0, 0, &fnum2);
+                            FILE_OPEN, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[4] open  - 2 of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum2);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[4] close - 1 failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_nt_delete_on_close(cli1, fnum1, true);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[4] setting delete_on_close failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        /* This should fail - no more opens once delete on close set. */
-       if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
-                                  FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                                  FILE_OPEN, 0, 0, &fnum2))) {
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                             FILE_OPEN, 0, 0, &fnum2, NULL);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[4] open  - 3 of %s succeeded ! Should have failed.\n", fname );
-               correct = False;
                goto fail;
-       } else
-               printf("fourth delete on close test succeeded.\n");
+       }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[4] close - 2 failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
+       printf("fourth delete on close test succeeded.\n");
+
        /* Test 5 ... */
        cli_setatr(cli1, fname, 0, 0);
        cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
@@ -4120,22 +4056,20 @@ static bool run_deletetest(int dummy)
        status = cli_openx(cli1, fname, O_RDWR|O_CREAT, DENY_NONE, &fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[5] open of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        /* This should fail - only allowed on NT opens with DELETE access. */
 
-       if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[5] setting delete_on_close on OpenX file succeeded - should fail !\n");
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("[5] close - 2 failed (%s)\n", nt_errstr(status));
-               correct = False;
+               printf("[5] close failed (%s)\n", nt_errstr(status));
                goto fail;
        }
 
@@ -4148,26 +4082,24 @@ static bool run_deletetest(int dummy)
        status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
                             FILE_ATTRIBUTE_NORMAL,
                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                            FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                            FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[6] open of %s failed (%s)\n", fname,
                       nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        /* This should fail - only allowed on NT opens with DELETE access. */
 
-       if (NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[6] setting delete_on_close on file with no delete access succeeded - should fail !\n");
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("[6] close - 2 failed (%s)\n", nt_errstr(status));
-               correct = False;
+               printf("[6] close failed (%s)\n", nt_errstr(status));
                goto fail;
        }
 
@@ -4180,69 +4112,63 @@ static bool run_deletetest(int dummy)
        status = cli_ntcreate(cli1, fname, 0,
                              FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
-                             0, 0, &fnum1);
+                             0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[7] open of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("[7] setting delete_on_close on file failed !\n");
-               correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, false))) {
+       status = cli_nt_delete_on_close(cli1, fnum1, false);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("[7] unsetting delete_on_close on file failed !\n");
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
-               correct = False;
+               printf("[7] close - 1 failed (%s)\n", nt_errstr(status));
                goto fail;
        }
 
        /* This next open should succeed - we reset the flag. */
        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;
+               printf("[7] open of %s failed (%s)\n", fname, nt_errstr(status));
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[7] close - 2 failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        printf("seventh delete on close test succeeded.\n");
 
-       /* Test 7 ... */
+       /* Test 8 ... */
        cli_setatr(cli1, fname, 0, 0);
        cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
        if (!torture_open_connection(&cli2, 1)) {
                printf("[8] failed to open second connection.\n");
-               correct = False;
                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,
                             FILE_ATTRIBUTE_NORMAL,
                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                            FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                            FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[8] open 1 of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
@@ -4250,30 +4176,27 @@ static bool run_deletetest(int dummy)
                             FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
                             FILE_ATTRIBUTE_NORMAL,
                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                            FILE_OPEN, 0, 0, &fnum2);
+                            FILE_OPEN, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[8] open 2 of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
-       if (!NT_STATUS_IS_OK(cli_nt_delete_on_close(cli1, fnum1, true))) {
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (!NT_STATUS_IS_OK(status)) {
                printf("[8] setting delete_on_close on file failed !\n");
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[8] close - 1 failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli2, fnum2);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[8] close - 2 failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
@@ -4282,28 +4205,34 @@ static bool run_deletetest(int dummy)
        if (NT_STATUS_IS_OK(status)) {
                printf("[8] open of %s succeeded should have been deleted on close !\n", fname);
                goto fail;
-               correct = False;
-       } else
-               printf("eighth delete on close test succeeded.\n");
+       }
+
+       printf("eighth delete on close test succeeded.\n");
+
+       /* Test 9 ... */
 
        /* This should fail - we need to set DELETE_ACCESS. */
-       if (NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0,FILE_READ_DATA|FILE_WRITE_DATA,
-                                  FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE, FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE, 0, &fnum1))) {
+       status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_NONE,
+                             FILE_OVERWRITE_IF,
+                             FILE_DELETE_ON_CLOSE, 0, &fnum1, NULL);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[9] open of %s succeeded should have failed!\n", fname);
-               correct = False;
                goto fail;
        }
 
        printf("ninth delete on close test succeeded.\n");
 
+       /* Test 10 ... */
+
        status = cli_ntcreate(cli1, fname, 0,
                             FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
                             FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
                             FILE_OVERWRITE_IF, FILE_DELETE_ON_CLOSE,
-                            0, &fnum1);
+                            0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[10] open of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
@@ -4311,38 +4240,37 @@ static bool run_deletetest(int dummy)
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[10] close failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        /* This should fail.. */
-       if (NT_STATUS_IS_OK(cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1))) {
+       status = cli_openx(cli1, fname, O_RDONLY, DENY_NONE, &fnum1);
+       if (NT_STATUS_IS_OK(status)) {
                printf("[10] open of %s succeeded should have been deleted on close !\n", fname);
                goto fail;
-               correct = False;
-       } else
-               printf("tenth delete on close test succeeded.\n");
+       }
+
+       printf("tenth delete on close test succeeded.\n");
+
+       /* Test 11 ... */
 
        cli_setatr(cli1, fname, 0, 0);
        cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
-       /* What error do we get when attempting to open a read-only file with
-          delete access ? */
+       /* Can we open a read-only file with delete access? */
 
        /* Create a readonly file. */
        status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
                              FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[11] open of %s failed (%s)\n", fname, nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
        status = cli_close(cli1, fnum1);
        if (!NT_STATUS_IS_OK(status)) {
                printf("[11] close failed (%s)\n", nt_errstr(status));
-               correct = False;
                goto fail;
        }
 
@@ -4351,24 +4279,118 @@ static bool run_deletetest(int dummy)
                             FILE_READ_ATTRIBUTES|DELETE_ACCESS,
                             0,
                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                            FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                            FILE_OPEN, 0, 0, &fnum1, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[11] open of %s failed: %s\n", fname, nt_errstr(status));
+               goto fail;
+       }
+
+       cli_close(cli1, fnum1);
+
+       printf("eleventh delete on close test succeeded.\n");
+
+       /*
+        * Test 12
+        * like test 4 but with initial delete on close
+        */
+
+       cli_setatr(cli1, fname, 0, 0);
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+
+       status = cli_ntcreate(cli1, fname, 0,
+                             FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE,
+                             FILE_OVERWRITE_IF,
+                             FILE_DELETE_ON_CLOSE, 0, &fnum1, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] open 1 of %s failed (%s)\n", fname, nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                             FILE_OPEN, 0, 0, &fnum2, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] open 2 of %s failed(%s).\n", fname, nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_close(cli1, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] close 1 failed (%s)\n", nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] setting delete_on_close failed (%s)\n", nt_errstr(status));
+               goto fail;
+       }
+
+       /* This should fail - no more opens once delete on close set. */
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                             FILE_OPEN, 0, 0, &fnum2, NULL);
        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);
+               printf("[12] open 3 of %s succeeded - should fail).\n", fname);
+               goto fail;
+       }
+
+       status = cli_nt_delete_on_close(cli1, fnum1, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] unsetting delete_on_close failed (%s)\n", nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                             FILE_OPEN, 0, 0, &fnum2, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] open 4 of %s failed (%s)\n", fname, nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_close(cli1, fnum2);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] close 2 failed (%s)\n", nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("[12] close 3 failed (%s)\n", nt_errstr(status));
                goto fail;
-               correct = False;
-       } else {
-               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 {
-                       printf("eleventh delete on close test succeeded.\n");
-               }
        }
 
+       /*
+        * setting delete on close on the handle does
+        * not unset the initial delete on close...
+        */
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL,
+                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                             FILE_OPEN, 0, 0, &fnum2, NULL);
+       if (NT_STATUS_IS_OK(status)) {
+               printf("[12] open 5 of %s succeeded - should fail).\n", fname);
+               goto fail;
+       } else if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
+               printf("ntcreate returned %s, expected "
+                      "NT_STATUS_OBJECT_NAME_NOT_FOUND\n",
+                      nt_errstr(status));
+               goto fail;
+       }
+
+       printf("twelfth delete on close test succeeded.\n");
+
+
        printf("finished delete test\n");
 
+       correct = true;
+
   fail:
        /* FIXME: This will crash if we aborted before cli2 got
         * intialized, because these functions don't handle
@@ -4408,7 +4430,7 @@ 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_openx(cli, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
@@ -4434,7 +4456,7 @@ static bool run_deletetest_ln(int dummy)
        status = cli_ntcreate(cli, fname, 0, FILE_READ_DATA,
                        FILE_ATTRIBUTE_NORMAL,
                        FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                       FILE_OPEN_IF, 0, 0, &fnum);
+                       FILE_OPEN_IF, 0, 0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("ntcreate of %s failed (%s)\n", fname, nt_errstr(status));
                return false;
@@ -4444,7 +4466,7 @@ static bool run_deletetest_ln(int dummy)
        status = cli_ntcreate(cli, fname_ln, 0, DELETE_ACCESS,
                        FILE_ATTRIBUTE_NORMAL,
                        FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                       FILE_OPEN_IF, 0, 0, &fnum1);
+                       FILE_OPEN_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("ntcreate of %s failed (%s)\n", fname_ln, nt_errstr(status));
                return false;
@@ -4518,9 +4540,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_state_capabilities(cli));
+       d_printf("Capabilities 0x%08x\n", smb1cli_conn_capabilities(cli->conn));
 
        if (!torture_close_connection(cli)) {
                correct = False;
@@ -4569,7 +4591,7 @@ static bool run_xcopy(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, FIRST_DESIRED_ACCESS,
                              FILE_ATTRIBUTE_ARCHIVE, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0x4044, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0x4044, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("First open failed - %s\n", nt_errstr(status));
                return False;
@@ -4577,7 +4599,7 @@ static bool run_xcopy(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, SECOND_DESIRED_ACCESS, 0,
                             FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                            FILE_OPEN, 0x200000, 0, &fnum2);
+                            FILE_OPEN, 0x200000, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("second open failed - %s\n", nt_errstr(status));
                return False;
@@ -4614,7 +4636,7 @@ static bool run_rename(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("First open failed - %s\n", nt_errstr(status));
                return False;
@@ -4642,7 +4664,7 @@ static bool run_rename(int dummy)
 #else
                              FILE_SHARE_DELETE|FILE_SHARE_READ,
 #endif
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Second open failed - %s\n", nt_errstr(status));
                return False;
@@ -4667,7 +4689,7 @@ static bool run_rename(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, READ_CONTROL_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Third open failed - %s\n", nt_errstr(status));
                return False;
@@ -4679,7 +4701,7 @@ static bool run_rename(int dummy)
        uint16_t fnum2;
 
        if (!NT_STATUS_IS_OK(cli_ntcreate(cli1, fname, 0, DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
+                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0, &fnum2, NULL))) {
                printf("Fourth open failed - %s\n", cli_errstr(cli1));
                return False;
        }
@@ -4717,7 +4739,7 @@ static bool run_rename(int dummy)
        status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
                              FILE_ATTRIBUTE_NORMAL,
                              FILE_SHARE_READ | FILE_SHARE_WRITE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Fourth open failed - %s\n", nt_errstr(status));
                return False;
@@ -4745,7 +4767,7 @@ static bool run_rename(int dummy)
        status = cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS,
                         FILE_ATTRIBUTE_NORMAL,
                         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
-                        FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                        FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("Fifth open failed - %s\n", nt_errstr(status));
                return False;
@@ -4764,7 +4786,8 @@ static bool run_rename(int dummy)
          */
 
         /* if (!NT_STATUS_OP(cli_ntcreate(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0, &fnum2))) {
+                                  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+                                  FILE_OVERWRITE_IF, 0, 0, &fnum2, NULL))) {
           printf("Opening original file after rename of open file fails: %s\n",
               cli_errstr(cli1));
         }
@@ -4822,12 +4845,12 @@ 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,
                                      FILE_SHARE_READ|FILE_SHARE_WRITE,
-                                     FILE_OPEN_IF, 0, 0, &fnum);
+                                     FILE_OPEN_IF, 0, 0, &fnum, NULL);
                if (!NT_STATUS_IS_OK(status)) {
                        printf("Open of pipe %s failed with error (%s)\n", pipe_name, nt_errstr(status));
                        break;
@@ -4865,7 +4888,7 @@ 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_openx(cli1, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum1);
        if (!NT_STATUS_IS_OK(status)) {
@@ -5021,12 +5044,12 @@ 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,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #1 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5034,7 +5057,7 @@ static bool run_opentest(int dummy)
 
        status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OPEN_IF, 0, 0, &fnum2);
+                             FILE_OPEN_IF, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #1 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5061,7 +5084,7 @@ static bool run_opentest(int dummy)
        status = cli_ntcreate(cli1, fname, 0,
                              DELETE_ACCESS|FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #2 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5069,7 +5092,7 @@ static bool run_opentest(int dummy)
 
        status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OPEN_IF, 0, 0, &fnum2);
+                             FILE_OPEN_IF, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #2 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5095,7 +5118,7 @@ static bool run_opentest(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #3 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5104,7 +5127,7 @@ static bool run_opentest(int dummy)
        status = cli_ntcreate(cli2, fname, 0,
                              DELETE_ACCESS|FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OPEN_IF, 0, 0, &fnum2);
+                             FILE_OPEN_IF, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #3 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5131,7 +5154,7 @@ static bool run_opentest(int dummy)
        status = cli_ntcreate(cli1, fname, 0,
                               DELETE_ACCESS|FILE_READ_ATTRIBUTES,
                               FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                              FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                              FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #4 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5140,7 +5163,7 @@ static bool run_opentest(int dummy)
        status = cli_ntcreate(cli2, fname, 0,
                              DELETE_ACCESS|FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OPEN_IF, 0, 0, &fnum2);
+                             FILE_OPEN_IF, 0, 0, &fnum2, NULL);
        if (NT_STATUS_IS_OK(status)) {
                printf("TEST #4 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5163,7 +5186,7 @@ static bool run_opentest(int dummy)
        status = cli_ntcreate(cli1, fname, 0,
                              DELETE_ACCESS|FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #5 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5172,7 +5195,7 @@ static bool run_opentest(int dummy)
        status = cli_ntcreate(cli2, fname, 0,
                              DELETE_ACCESS|FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_DELETE,
-                             FILE_OPEN_IF, 0, 0, &fnum2);
+                             FILE_OPEN_IF, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #5 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5198,7 +5221,7 @@ static bool run_opentest(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #6 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5206,7 +5229,7 @@ static bool run_opentest(int dummy)
 
        status = cli_ntcreate(cli2, fname, 0, FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
-                             FILE_OPEN_IF, 0, 0, &fnum2);
+                             FILE_OPEN_IF, 0, 0, &fnum2, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #6 open 2 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5232,7 +5255,7 @@ static bool run_opentest(int dummy)
 
        status = cli_ntcreate(cli1, fname, 0, FILE_READ_DATA,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_NONE,
-                             FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                             FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #7 open 1 of %s failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5242,7 +5265,7 @@ static bool run_opentest(int dummy)
                              DELETE_ACCESS|FILE_READ_ATTRIBUTES,
                              FILE_ATTRIBUTE_NORMAL,
                              FILE_SHARE_READ|FILE_SHARE_DELETE,
-                             FILE_OPEN_IF, 0, 0, &fnum2);
+                             FILE_OPEN_IF, 0, 0, &fnum2, NULL);
        if (NT_STATUS_IS_OK(status)) {
                printf("TEST #7 open 2 of %s SUCCEEDED - should have failed (%s)\n", fname, nt_errstr(status));
                return False;
@@ -5263,7 +5286,7 @@ static bool run_opentest(int dummy)
        printf("TEST #8 testing open without WRITE_ATTRIBUTES, updating close write time.\n");
        status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
                                FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                               FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                               FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("TEST #8 open of %s failed (%s)\n", fname, nt_errstr(status));
                correct = false;
@@ -5299,8 +5322,8 @@ static bool run_opentest(int dummy)
 
 NTSTATUS torture_setup_unix_extensions(struct cli_state *cli)
 {
-       uint16 major, minor;
-       uint32 caplow, caphigh;
+       uint16_t major, minor;
+       uint32_t caplow, caphigh;
        NTSTATUS status;
 
        if (!SERVER_HAS_UNIX_CIFS(cli)) {
@@ -5344,6 +5367,8 @@ static bool run_simple_posix_open_test(int dummy)
        bool correct = false;
        NTSTATUS status;
        size_t nread;
+       const char *fname_windows = "windows_file";
+       uint16_t fnum2 = (uint16_t)-1;
 
        printf("Starting simple POSIX open test\n");
 
@@ -5351,7 +5376,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)) {
@@ -5366,6 +5391,8 @@ static bool run_simple_posix_open_test(int dummy)
        cli_posix_unlink(cli1, hname);
        cli_setatr(cli1, sname, 0, 0);
        cli_posix_unlink(cli1, sname);
+       cli_setatr(cli1, fname_windows, 0, 0);
+       cli_posix_unlink(cli1, fname_windows);
 
        /* Create a directory. */
        status = cli_posix_mkdir(cli1, dname, 0777);
@@ -5400,6 +5427,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)) {
@@ -5630,6 +5664,59 @@ 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;
+       }
+
+       /*
+        * Now create a Windows file, and attempt a POSIX unlink.
+        * This should fail with a sharing violation but due to:
+        *
+        * [Bug 9571] Unlink after open causes smbd to panic
+        *
+        * ensure we've fixed the lock ordering violation.
+        */
+
+       status = cli_ntcreate(cli1, fname_windows, 0,
+                       FILE_READ_DATA|FILE_WRITE_DATA, 0,
+                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                       FILE_CREATE,
+                       0x0, 0x0, &fnum2, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("Windows create of %s failed (%s)\n", fname_windows,
+                       nt_errstr(status));
+               goto out;
+       }
+
+       /* Now try posix_unlink. */
+       status = cli_posix_unlink(cli1, fname_windows);
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
+               printf("POSIX unlink of %s should fail "
+                       "with NT_STATUS_SHARING_VIOLATION "
+                       "got %s instead !\n",
+                       fname_windows,
+                       nt_errstr(status));
+               goto out;
+       }
+
+       cli_close(cli1, fnum2);
+
        printf("Simple POSIX open test passed\n");
        correct = true;
 
@@ -5640,6 +5727,11 @@ static bool run_simple_posix_open_test(int dummy)
                fnum1 = (uint16_t)-1;
        }
 
+       if (fnum2 != (uint16_t)-1) {
+               cli_close(cli1, fnum2);
+               fnum2 = (uint16_t)-1;
+       }
+
        cli_setatr(cli1, sname, 0, 0);
        cli_posix_unlink(cli1, sname);
        cli_setatr(cli1, hname, 0, 0);
@@ -5648,6 +5740,8 @@ static bool run_simple_posix_open_test(int dummy)
        cli_posix_unlink(cli1, fname);
        cli_setatr(cli1, dname, 0, 0);
        cli_posix_rmdir(cli1, dname);
+       cli_setatr(cli1, fname_windows, 0, 0);
+       cli_posix_unlink(cli1, fname_windows);
 
        if (!torture_close_connection(cli1)) {
                correct = false;
@@ -5657,7 +5751,7 @@ static bool run_simple_posix_open_test(int dummy)
 }
 
 
-static uint32 open_attrs_table[] = {
+static uint32_t open_attrs_table[] = {
                FILE_ATTRIBUTE_NORMAL,
                FILE_ATTRIBUTE_ARCHIVE,
                FILE_ATTRIBUTE_READONLY,
@@ -5679,9 +5773,9 @@ static uint32 open_attrs_table[] = {
 
 struct trunc_open_results {
        unsigned int num;
-       uint32 init_attr;
-       uint32 trunc_attr;
-       uint32 result_attr;
+       uint32_t init_attr;
+       uint32_t trunc_attr;
+       uint32_t result_attr;
 };
 
 static struct trunc_open_results attr_results[] = {
@@ -5719,7 +5813,7 @@ static bool run_openattrtest(int dummy)
        const char *fname = "\\openattr.file";
        uint16_t fnum1;
        bool correct = True;
-       uint16 attr;
+       uint16_t attr;
        unsigned int i, j, k, l;
        NTSTATUS status;
 
@@ -5729,15 +5823,15 @@ 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++) {
+       for (k = 0, i = 0; i < sizeof(open_attrs_table)/sizeof(uint32_t); i++) {
                cli_setatr(cli1, fname, 0, 0);
                cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
 
                status = cli_ntcreate(cli1, fname, 0, FILE_WRITE_DATA,
                                       open_attrs_table[i], FILE_SHARE_NONE,
-                                      FILE_OVERWRITE_IF, 0, 0, &fnum1);
+                                      FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
                if (!NT_STATUS_IS_OK(status)) {
                        printf("open %d (1) of %s failed (%s)\n", i, fname, nt_errstr(status));
                        return False;
@@ -5749,12 +5843,12 @@ static bool run_openattrtest(int dummy)
                        return False;
                }
 
-               for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32); j++) {
+               for (j = 0; j < sizeof(open_attrs_table)/sizeof(uint32_t); j++) {
                        status = cli_ntcreate(cli1, fname, 0,
                                              FILE_READ_DATA|FILE_WRITE_DATA,
                                              open_attrs_table[j],
                                              FILE_SHARE_NONE, FILE_OVERWRITE,
-                                             0, 0, &fnum1);
+                                             0, 0, &fnum1, NULL);
                        if (!NT_STATUS_IS_OK(status)) {
                                for (l = 0; l < sizeof(attr_results)/sizeof(struct trunc_open_results); l++) {
                                        if (attr_results[l].num == k) {
@@ -5854,7 +5948,7 @@ 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++) {
@@ -5955,7 +6049,7 @@ bool torture_ioctl_test(int dummy)
        for (device=0;device<0x100;device++) {
                printf("ioctl test with device = 0x%x\n", device);
                for (function=0;function<0x100;function++) {
-                       uint32 code = (device<<16) | function;
+                       uint32_t code = (device<<16) | function;
 
                        status = cli_raw_ioctl(cli, fnum, code, &blob);
 
@@ -6042,7 +6136,7 @@ bool torture_chkpath_test(int dummy)
                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;
        }
 
@@ -6090,7 +6184,7 @@ static bool run_eatest(int dummy)
        status = cli_ntcreate(cli, fname, 0,
                               FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
                               FILE_SHARE_NONE, FILE_OVERWRITE_IF,
-                              0x4044, 0, &fnum);
+                              0x4044, 0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                printf("open failed - %s\n", nt_errstr(status));
                talloc_destroy(mem_ctx);
@@ -6182,10 +6276,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;
        }
@@ -6212,7 +6306,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);
@@ -6224,7 +6318,8 @@ static bool run_dirtest1(int dummy)
                fstring fname;
                slprintf(fname, sizeof(fname), "\\LISTDIR\\f%d", i);
                if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
-                                  FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum))) {
+                                  FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
+                                  0, 0, &fnum, NULL))) {
                        fprintf(stderr,"Failed to open %s\n", fname);
                        return False;
                }
@@ -6288,10 +6383,10 @@ static bool run_error_map_extract(int dummy) {
        static struct cli_state *c_nt;
        NTSTATUS status;
 
-       uint32 error;
+       uint32_t error;
 
-       uint32 errnum;
-        uint8 errclass;
+       uint32_t errnum;
+        uint8_t errclass;
 
        NTSTATUS nt_status;
 
@@ -6306,7 +6401,8 @@ static bool run_error_map_extract(int dummy) {
        }
        disable_spnego = false;
 
-       status = cli_negprot(c_nt, PROTOCOL_NT1);
+       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,
@@ -6333,7 +6429,8 @@ static bool run_error_map_extract(int dummy) {
        disable_spnego = false;
        force_dos_errors = false;
 
-       status = cli_negprot(c_dos, PROTOCOL_NT1);
+       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));
@@ -6417,7 +6514,7 @@ static bool run_sesssetup_bench(int dummy)
 
        status = cli_ntcreate(c, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
-                             FILE_DELETE_ON_CLOSE, 0, &fnum);
+                             FILE_DELETE_ON_CLOSE, 0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("open %s failed: %s\n", fname, nt_errstr(status));
                return false;
@@ -6454,7 +6551,7 @@ static bool subst_test(const char *str, const char *user, const char *domain,
        char *subst;
        bool result = true;
 
-       subst = talloc_sub_specified(talloc_tos(), str, user, domain, uid, gid);
+       subst = talloc_sub_specified(talloc_tos(), str, user, NULL, domain, uid, gid);
 
        if (strcmp(subst, expected) != 0) {
                printf("sub_specified(%s, %s, %s, %d, %d) returned [%s], expected "
@@ -6507,7 +6604,7 @@ static void chain1_close_completion(struct tevent_req *req)
 static bool run_chain1(int dummy)
 {
        struct cli_state *cli1;
-       struct event_context *evt = event_context_init(NULL);
+       struct tevent_context *evt = samba_tevent_context_init(NULL);
        struct tevent_req *reqs[3], *smbreqs[3];
        bool done = false;
        const char *str = "foobar";
@@ -6518,7 +6615,7 @@ static bool run_chain1(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        reqs[0] = cli_openx_create(talloc_tos(), evt, cli1, "\\test",
                                  O_CREAT|O_RDWR, 0, &smbreqs[0]);
@@ -6536,7 +6633,7 @@ 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;
        }
@@ -6568,7 +6665,7 @@ static void chain2_tcon_completion(struct tevent_req *req)
 static bool run_chain2(int dummy)
 {
        struct cli_state *cli1;
-       struct event_context *evt = event_context_init(NULL);
+       struct tevent_context *evt = samba_tevent_context_init(NULL);
        struct tevent_req *reqs[2], *smbreqs[2];
        bool done = false;
        NTSTATUS status;
@@ -6580,7 +6677,7 @@ static bool run_chain2(int dummy)
                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]);
@@ -6592,7 +6689,7 @@ 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;
        }
@@ -6653,12 +6750,11 @@ static void torture_createdel_created(struct tevent_req *subreq)
        NTSTATUS status;
        uint16_t fnum;
 
-       status = cli_ntcreate_recv(subreq, &fnum);
+       status = cli_ntcreate_recv(subreq, &fnum, NULL);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (tevent_req_nterror(req, status)) {
                DEBUG(10, ("cli_ntcreate_recv returned %s\n",
                           nt_errstr(status)));
-               tevent_req_nterror(req, status);
                return;
        }
 
@@ -6676,9 +6772,8 @@ static void torture_createdel_closed(struct tevent_req *subreq)
        NTSTATUS status;
 
        status = cli_close_recv(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (tevent_req_nterror(req, status)) {
                DEBUG(10, ("cli_close_recv returned %s\n", nt_errstr(status)));
-               tevent_req_nterror(req, status);
                return;
        }
        tevent_req_done(req);
@@ -6933,7 +7028,7 @@ static bool run_notify_bench(int dummy)
                num_unc_names = 1;
        }
 
-       ev = tevent_context_init(talloc_tos());
+       ev = samba_tevent_context_init(talloc_tos());
        if (ev == NULL) {
                d_printf("tevent_context_init failed\n");
                return false;
@@ -6958,7 +7053,7 @@ static bool run_notify_bench(int dummy)
                                      0, FILE_SHARE_READ|FILE_SHARE_WRITE|
                                      FILE_SHARE_DELETE,
                                      FILE_OPEN_IF, FILE_DIRECTORY_FILE, 0,
-                                     &dnum);
+                                     &dnum, NULL);
 
                if (!NT_STATUS_IS_OK(status)) {
                        d_printf("Could not create %s: %s\n", dname,
@@ -7024,11 +7119,11 @@ 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,
-                             0, 0, &fnum);
+                             0, 0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("open %s failed: %s\n", fname, nt_errstr(status));
                return false;
@@ -7100,7 +7195,7 @@ static bool run_windows_write(int dummy)
                return False;
        }
 
-       cli_sockopt(cli1, sockops);
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
        start_time = timeval_current();
 
@@ -7139,20 +7234,356 @@ static bool run_windows_write(int dummy)
        return ret;
 }
 
-static bool run_cli_echo(int dummy)
+static size_t calc_expected_return(struct cli_state *cli, size_t len_requested)
 {
-       struct cli_state *cli;
-       NTSTATUS status;
+       size_t max_pdu = 0x1FFFF;
 
-       printf("starting cli_echo test\n");
-       if (!torture_open_connection(&cli, 0)) {
-               return false;
+       if (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP) {
+               max_pdu = 0xFFFFFF;
        }
-       cli_sockopt(cli, sockops);
 
-       status = cli_echo(cli, 5, data_blob_const("hello", 5));
+       if (smb1cli_conn_signing_is_active(cli->conn)) {
+               max_pdu = 0x1FFFF;
+       }
 
-       d_printf("cli_echo returned %s\n", nt_errstr(status));
+       if (smb1cli_conn_encryption_on(cli->conn)) {
+               max_pdu = CLI_BUFFER_SIZE;
+       }
+
+       if ((len_requested & 0xFFFF0000) == 0xFFFF0000) {
+               len_requested &= 0xFFFF;
+       }
+
+       return MIN(len_requested,
+                  max_pdu - (MIN_SMB_SIZE + VWV(12) + 1 /* padding byte */));
+}
+
+static bool check_read_call(struct cli_state *cli,
+                           uint16_t fnum,
+                           uint8_t *buf,
+                           size_t len_requested)
+{
+       NTSTATUS status;
+       struct tevent_req *subreq = NULL;
+       ssize_t len_read = 0;
+       size_t len_expected = 0;
+       struct tevent_context *ev = NULL;
+
+       ev = samba_tevent_context_init(talloc_tos());
+       if (ev == NULL) {
+               return false;
+       }
+
+       subreq = cli_read_andx_send(talloc_tos(),
+                                   ev,
+                                   cli,
+                                   fnum,
+                                   0,
+                                   len_requested);
+
+       if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
+               return false;
+       }
+
+       status = cli_read_andx_recv(subreq, &len_read, &buf);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_read_andx_recv failed: %s\n", nt_errstr(status));
+               return false;
+       }
+
+       TALLOC_FREE(subreq);
+       TALLOC_FREE(ev);
+
+       len_expected = calc_expected_return(cli, len_requested);
+
+       if (len_expected > 0x10000 && len_read == 0x10000) {
+               /* Windows servers only return a max of 0x10000,
+                  doesn't matter if you set CAP_LARGE_READX in
+                  the client sessionsetupX call or not. */
+               d_printf("Windows server - returned 0x10000 on a read of 0x%x\n",
+                       (unsigned int)len_requested);
+       } else if (len_read != len_expected) {
+               d_printf("read of 0x%x failed: got 0x%x, expected 0x%x\n",
+                       (unsigned int)len_requested,
+                       (unsigned int)len_read,
+                       (unsigned int)len_expected);
+               return false;
+       } else {
+               d_printf("Correct read reply.\n");
+       }
+
+       return true;
+}
+
+/* Test large readX variants. */
+static bool large_readx_tests(struct cli_state *cli,
+                               uint16_t fnum,
+                               uint8_t *buf)
+{
+       /* A read of 0xFFFF0001 should *always* return 1 byte. */
+       if (check_read_call(cli, fnum, buf, 0xFFFF0001) == false) {
+               return false;
+       }
+       /* A read of 0x10000 should return 0x10000 bytes. */
+       if (check_read_call(cli, fnum, buf,    0x10000) == false) {
+               return false;
+       }
+       /* A read of 0x10000 should return 0x10001 bytes. */
+       if (check_read_call(cli, fnum, buf,    0x10001) == false) {
+               return false;
+       }
+       /* A read of 0x1FFFF - (MIN_SMB_SIZE + VWV(12) should return
+          the requested number of bytes. */
+       if (check_read_call(cli, fnum, buf, 0x1FFFF - (MIN_SMB_SIZE + VWV(12))) == false) {
+               return false;
+       }
+       /* A read of 1MB should return 1MB bytes (on Samba). */
+       if (check_read_call(cli, fnum, buf,   0x100000) == false) {
+               return false;
+       }
+
+       if (check_read_call(cli, fnum, buf,    0x20001) == false) {
+               return false;
+       }
+       if (check_read_call(cli, fnum, buf, 0x22000001) == false) {
+               return false;
+       }
+       if (check_read_call(cli, fnum, buf, 0xFFFE0001) == false) {
+               return false;
+       }
+       return true;
+}
+
+static bool run_large_readx(int dummy)
+{
+       uint8_t *buf = NULL;
+       struct cli_state *cli1 = NULL;
+       struct cli_state *cli2 = NULL;
+       bool correct = false;
+       const char *fname = "\\large_readx.dat";
+       NTSTATUS status;
+       uint16_t fnum1 = UINT16_MAX;
+       uint32_t normal_caps = 0;
+       size_t file_size = 20*1024*1024;
+       TALLOC_CTX *frame = talloc_stackframe();
+       size_t i;
+       struct {
+               const char *name;
+               enum smb_signing_setting signing_setting;
+               enum protocol_types protocol;
+       } runs[] = {
+               {
+                       .name = "NT1",
+                       .signing_setting = SMB_SIGNING_IF_REQUIRED,
+                       .protocol = PROTOCOL_NT1,
+               },{
+                       .name = "NT1 - SIGNING_REQUIRED",
+                       .signing_setting = SMB_SIGNING_REQUIRED,
+                       .protocol = PROTOCOL_NT1,
+               },
+       };
+
+       printf("starting large_readx test\n");
+
+       if (!torture_open_connection(&cli1, 0)) {
+               goto out;
+       }
+
+       normal_caps = smb1cli_conn_capabilities(cli1->conn);
+
+       if (!(normal_caps & CAP_LARGE_READX)) {
+               d_printf("Server doesn't have CAP_LARGE_READX 0x%x\n",
+                       (unsigned int)normal_caps);
+               goto out;
+       }
+
+       /* Create a file of size 4MB. */
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS,
+                       FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                       0, 0, &fnum1, NULL);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("open %s failed: %s\n", fname, nt_errstr(status));
+               goto out;
+       }
+
+       /* Write file_size bytes. */
+       buf = talloc_zero_array(frame, uint8_t, file_size);
+       if (buf == NULL) {
+               goto out;
+       }
+
+       status = cli_writeall(cli1,
+                             fnum1,
+                             0,
+                             buf,
+                             0,
+                             file_size,
+                             NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_writeall failed: %s\n", nt_errstr(status));
+               goto out;
+       }
+
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_close failed: %s\n", nt_errstr(status));
+               goto out;
+       }
+
+       fnum1 = UINT16_MAX;
+
+       for (i=0; i < ARRAY_SIZE(runs); i++) {
+               enum smb_signing_setting saved_signing_setting = signing_state;
+               uint16_t fnum2 = -1;
+
+               if (do_encrypt &&
+                   (runs[i].signing_setting == SMB_SIGNING_REQUIRED))
+               {
+                       d_printf("skip[%u] - %s\n", (unsigned)i, runs[i].name);
+                       continue;
+               }
+
+               d_printf("run[%u] - %s\n", (unsigned)i, runs[i].name);
+
+               signing_state = runs[i].signing_setting;
+               cli2 = open_nbt_connection();
+               signing_state = saved_signing_setting;
+               if (cli2 == NULL) {
+                       goto out;
+               }
+
+               status = smbXcli_negprot(cli2->conn,
+                                        cli2->timeout,
+                                        runs[i].protocol,
+                                        runs[i].protocol);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto out;
+               }
+
+               status = cli_session_setup(cli2,
+                                       username,
+                                       password,
+                                       strlen(password)+1,
+                                       password,
+                                       strlen(password)+1,
+                                       workgroup);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto out;
+               }
+
+               status = cli_tree_connect(cli2,
+                                       share,
+                                       "?????",
+                                       password,
+                                       strlen(password)+1);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto out;
+               }
+
+               cli_set_timeout(cli2, 120000); /* set a really long timeout (2 minutes) */
+
+               normal_caps = smb1cli_conn_capabilities(cli2->conn);
+
+               if (!(normal_caps & CAP_LARGE_READX)) {
+                       d_printf("Server doesn't have CAP_LARGE_READX 0x%x\n",
+                               (unsigned int)normal_caps);
+                       goto out;
+               }
+
+               if (do_encrypt) {
+                       if (force_cli_encryption(cli2, share) == false) {
+                               goto out;
+                       }
+               } else if (SERVER_HAS_UNIX_CIFS(cli2)) {
+                       uint16_t major, minor;
+                       uint32_t caplow, caphigh;
+
+                       status = cli_unix_extensions_version(cli2,
+                                                            &major, &minor,
+                                                            &caplow, &caphigh);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               goto out;
+                       }
+               }
+
+               status = cli_ntcreate(cli2, fname, 0, FILE_READ_DATA,
+                               FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
+                               0, 0, &fnum2, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("Second open %s failed: %s\n", fname, nt_errstr(status));
+                       goto out;
+               }
+
+               /* All reads must return less than file_size bytes. */
+               if (!large_readx_tests(cli2, fnum2, buf)) {
+                       goto out;
+               }
+
+               status = cli_close(cli2, fnum2);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("cli_close failed: %s\n", nt_errstr(status));
+                       goto out;
+               }
+               fnum2 = -1;
+
+               if (!torture_close_connection(cli2)) {
+                       goto out;
+               }
+               cli2 = NULL;
+       }
+
+       correct = true;
+       printf("Success on large_readx test\n");
+
+  out:
+
+       if (cli2) {
+               if (!torture_close_connection(cli2)) {
+                       correct = false;
+               }
+       }
+
+       if (cli1) {
+               if (fnum1 != UINT16_MAX) {
+                       status = cli_close(cli1, fnum1);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               d_printf("cli_close failed: %s\n", nt_errstr(status));
+                       }
+                       fnum1 = UINT16_MAX;
+               }
+
+               status = cli_unlink(cli1, fname,
+                                   FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("unlink failed (%s)\n", nt_errstr(status));
+               }
+
+               if (!torture_close_connection(cli1)) {
+                       correct = false;
+               }
+       }
+
+       TALLOC_FREE(frame);
+
+       printf("finished large_readx test\n");
+       return correct;
+}
+
+static bool run_cli_echo(int dummy)
+{
+       struct cli_state *cli;
+       NTSTATUS status;
+
+       printf("starting cli_echo test\n");
+       if (!torture_open_connection(&cli, 0)) {
+               return false;
+       }
+       smbXcli_conn_set_sockopt(cli->conn, sockops);
+
+       status = cli_echo(cli, 5, data_blob_const("hello", 5));
+
+       d_printf("cli_echo returned %s\n", nt_errstr(status));
 
        torture_close_connection(cli);
        return NT_STATUS_IS_OK(status);
@@ -7172,7 +7603,7 @@ 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_state_get_uid(cli);
@@ -7324,7 +7755,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);
@@ -7360,7 +7791,8 @@ static bool run_shortname_test(int dummy)
                fname[15] = i;
 
                status = cli_ntcreate(cli, fname, 0, GENERIC_ALL_ACCESS, FILE_ATTRIBUTE_NORMAL,
-                                   FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0, &fnum);
+                                   FILE_SHARE_READ|FILE_SHARE_WRITE,
+                                  FILE_OVERWRITE_IF, 0, 0, &fnum, NULL);
                if (!NT_STATUS_IS_OK(status)) {
                        d_printf("(%s) cli_nt_create of %s failed: %s\n",
                                __location__, fname, nt_errstr(status));
@@ -7469,7 +7901,7 @@ static bool run_tldap(int dummy)
        }
        d_printf("defaultNamingContext: %s\n", basedn);
 
-       ev = tevent_context_init(talloc_tos());
+       ev = samba_tevent_context_init(talloc_tos());
        if (ev == NULL) {
                d_printf("tevent_context_init failed\n");
                return false;
@@ -7616,7 +8048,7 @@ static bool run_streamerror(int dummy)
                              FILE_READ_DATA|FILE_READ_EA|
                              FILE_READ_ATTRIBUTES|READ_CONTROL_ACCESS,
                              FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
-                             FILE_OPEN, 0, 0, &fnum);
+                             FILE_OPEN, 0, 0, &fnum, NULL);
 
        if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
                printf("ntcreate returned %s, expected "
@@ -7684,23 +8116,48 @@ static bool run_local_base64(int dummy)
        return ret;
 }
 
+static void parse_fn(time_t timeout, DATA_BLOB blob, void *private_data)
+{
+       return;
+}
+
 static bool run_local_gencache(int dummy)
 {
        char *val;
        time_t tm;
        DATA_BLOB blob;
+       char v;
+       struct memcache *mem;
+       int i;
+
+       mem = memcache_init(NULL, 0);
+       if (mem == NULL) {
+               d_printf("%s: memcache_init failed\n", __location__);
+               return false;
+       }
+       memcache_set_global(mem);
 
        if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
                d_printf("%s: gencache_set() failed\n", __location__);
                return False;
        }
 
-       if (!gencache_get("foo", NULL, NULL)) {
+       if (!gencache_get("foo", NULL, NULL, NULL)) {
                d_printf("%s: gencache_get() failed\n", __location__);
                return False;
        }
 
-       if (!gencache_get("foo", &val, &tm)) {
+       for (i=0; i<1000000; i++) {
+               gencache_parse("foo", parse_fn, NULL);
+       }
+
+       if (!gencache_get("foo", talloc_tos(), &val, &tm)) {
+               d_printf("%s: gencache_get() failed\n", __location__);
+               return False;
+       }
+       TALLOC_FREE(val);
+
+       if (!gencache_get("foo", talloc_tos(), &val, &tm)) {
                d_printf("%s: gencache_get() failed\n", __location__);
                return False;
        }
@@ -7708,11 +8165,11 @@ static bool run_local_gencache(int dummy)
        if (strcmp(val, "bar") != 0) {
                d_printf("%s: gencache_get() returned %s, expected %s\n",
                         __location__, val, "bar");
-               SAFE_FREE(val);
+               TALLOC_FREE(val);
                return False;
        }
 
-       SAFE_FREE(val);
+       TALLOC_FREE(val);
 
        if (!gencache_del("foo")) {
                d_printf("%s: gencache_del() failed\n", __location__);
@@ -7724,7 +8181,7 @@ static bool run_local_gencache(int dummy)
                return False;
        }
 
-       if (gencache_get("foo", &val, &tm)) {
+       if (gencache_get("foo", talloc_tos(), &val, &tm)) {
                d_printf("%s: gencache_get() on deleted entry "
                         "succeeded\n", __location__);
                return False;
@@ -7738,7 +8195,7 @@ static bool run_local_gencache(int dummy)
                return False;
        }
 
-       if (!gencache_get_data_blob("foo", &blob, NULL, NULL)) {
+       if (!gencache_get_data_blob("foo", talloc_tos(), &blob, NULL, NULL)) {
                d_printf("%s: gencache_get_data_blob() failed\n", __location__);
                return False;
        }
@@ -7762,12 +8219,26 @@ static bool run_local_gencache(int dummy)
                return False;
        }
 
-       if (gencache_get_data_blob("foo", &blob, NULL, NULL)) {
+       if (gencache_get_data_blob("foo", talloc_tos(), &blob, NULL, NULL)) {
                d_printf("%s: gencache_get_data_blob() on deleted entry "
                         "succeeded\n", __location__);
                return False;
        }
 
+       v = 1;
+       blob.data = (uint8_t *)&v;
+       blob.length = sizeof(v);
+
+       if (!gencache_set_data_blob("blob", &blob, tm)) {
+               d_printf("%s: gencache_set_data_blob() failed\n",
+                        __location__);
+               return false;
+       }
+       if (gencache_get("blob", talloc_tos(), &val, &tm)) {
+               d_printf("%s: gencache_get succeeded\n", __location__);
+               return false;
+       }
+
        return True;
 }
 
@@ -8034,6 +8505,22 @@ static bool run_local_string_to_sid(int dummy) {
                printf("allowing S-1-5-32-545-abc\n");
                return false;
        }
+       if (string_to_sid(&sid, "S-300-5-32-545")) {
+               printf("allowing S-300-5-32-545\n");
+               return false;
+       }
+       if (string_to_sid(&sid, "S-1-0xfffffffffffffe-32-545")) {
+               printf("allowing S-1-0xfffffffffffffe-32-545\n");
+               return false;
+       }
+       if (string_to_sid(&sid, "S-1-0xffffffffffff-5294967297-545")) {
+               printf("allowing S-1-0xffffffffffff-5294967297-545\n");
+               return false;
+       }
+       if (!string_to_sid(&sid, "S-1-0xfffffffffffe-32-545")) {
+               printf("could not parse S-1-0xfffffffffffe-32-545\n");
+               return false;
+       }
        if (!string_to_sid(&sid, "S-1-5-32-545")) {
                printf("could not parse S-1-5-32-545\n");
                return false;
@@ -8046,6 +8533,35 @@ static bool run_local_string_to_sid(int dummy) {
        return true;
 }
 
+static bool sid_to_string_test(const char *expected) {
+       char *str;
+       bool res = true;
+       struct dom_sid sid;
+
+       if (!string_to_sid(&sid, expected)) {
+               printf("could not parse %s\n", expected);
+               return false;
+       }
+
+       str = dom_sid_string(NULL, &sid);
+       if (strcmp(str, expected)) {
+               printf("Comparison failed (%s != %s)\n", str, expected);
+               res = false;
+       }
+       TALLOC_FREE(str);
+       return res;
+}
+
+static bool run_local_sid_to_string(int dummy) {
+       if (!sid_to_string_test("S-1-0xffffffffffff-1-1-1-1-1-1-1-1-1-1-1-1"))
+               return false;
+       if (!sid_to_string_test("S-1-545"))
+               return false;
+       if (!sid_to_string_test("S-255-3840-1-1-1-1"))
+               return false;
+       return true;
+}
+
 static bool run_local_binary_to_sid(int dummy) {
        struct dom_sid *sid = talloc(NULL, struct dom_sid);
        static const char good_binary_sid[] = {
@@ -8220,7 +8736,7 @@ static NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
                /*
                 * upper-case the type field
                 */
-               strupper_m(strchr_m(stream, ':')+1);
+               (void)strupper_m(strchr_m(stream, ':')+1);
        }
 
  done:
@@ -8419,9 +8935,9 @@ static void wbclient_done(struct tevent_req *req)
        d_printf("wb_trans_recv %d returned %s\n", *i, wbcErrorString(wbc_err));
 }
 
-static bool run_local_wbclient(int dummy)
+static bool run_wbclient_multi_ping(int dummy)
 {
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct wb_context **wb_ctx;
        struct winbindd_request wb_req;
        bool result = false;
@@ -8429,7 +8945,7 @@ static bool run_local_wbclient(int dummy)
 
        BlockSignals(True, SIGPIPE);
 
-       ev = tevent_context_init_byname(talloc_tos(), "epoll");
+       ev = tevent_context_init(talloc_tos());
        if (ev == NULL) {
                goto fail;
        }
@@ -8498,7 +9014,7 @@ static bool run_getaddrinfo_send(int dummy)
        struct tevent_req *reqs[4];
        int i;
 
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -8574,7 +9090,8 @@ static bool run_local_dbtrans(int dummy)
        TDB_DATA value;
 
        db = db_open(talloc_tos(), "transtest.tdb", 0, TDB_DEFAULT,
-                    O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_1);
+                    O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_1,
+                    DBWRAP_FLAG_NONE);
        if (db == NULL) {
                printf("Could not open transtest.db\n");
                return false;
@@ -8625,7 +9142,7 @@ static bool run_local_dbtrans(int dummy)
                        break;
                }
 
-               status = dbwrap_fetch_uint32(db, "transtest", &val);
+               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));
@@ -8638,7 +9155,7 @@ static bool run_local_dbtrans(int dummy)
                        }
                }
 
-               status = dbwrap_fetch_uint32(db, "transtest", &val2);
+               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));
@@ -8726,6 +9243,163 @@ static bool run_local_hex_encode_buf(int dummy)
        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 bool run_local_tdb_opener(int dummy)
+{
+       TDB_CONTEXT *t;
+       unsigned v = 0;
+
+       while (1) {
+               t = tdb_open("test.tdb", 1000, TDB_CLEAR_IF_FIRST,
+                            O_RDWR|O_CREAT, 0755);
+               if (t == NULL) {
+                       perror("tdb_open failed");
+                       return false;
+               }
+               tdb_close(t);
+
+               v += 1;
+               printf("\r%u", v);
+       }
+       return true;
+}
+
+static bool run_local_tdb_writer(int dummy)
+{
+       TDB_CONTEXT *t;
+       unsigned v = 0;
+       TDB_DATA val;
+
+       t = tdb_open("test.tdb", 1000, 0, O_RDWR|O_CREAT, 0755);
+       if (t == 0) {
+               perror("tdb_open failed");
+               return 1;
+       }
+
+       val.dptr = (uint8_t *)&v;
+       val.dsize = sizeof(v);
+
+       while (1) {
+               TDB_DATA data;
+               int ret;
+
+               ret = tdb_store(t, val, val, 0);
+               if (ret != 0) {
+                       printf("%s\n", tdb_errorstr(t));
+               }
+               v += 1;
+               printf("\r%u", v);
+
+               data = tdb_fetch(t, val);
+               if (data.dptr != NULL) {
+                       SAFE_FREE(data.dptr);
+               }
+       }
+       return true;
+}
+
 static double create_procs(bool (*fn)(int), bool *result)
 {
        int i, status;
@@ -8737,13 +9411,13 @@ static double create_procs(bool (*fn)(int), bool *result)
 
        synccount = 0;
 
-       child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
+       child_status = (volatile pid_t *)anonymous_shared_allocate(sizeof(pid_t)*torture_nprocs);
        if (!child_status) {
                printf("Failed to setup shared memory\n");
                return -1;
        }
 
-       child_status_out = (volatile bool *)shm_setup(sizeof(bool)*torture_nprocs);
+       child_status_out = (volatile bool *)anonymous_shared_allocate(sizeof(bool)*torture_nprocs);
        if (!child_status_out) {
                printf("Failed to setup result status shared memory\n");
                return -1;
@@ -8896,6 +9570,7 @@ static struct {
        { "CHAIN2", run_chain2, 0},
        { "CHAIN3", run_chain3, 0},
        { "WINDOWS-WRITE", run_windows_write, 0},
+       { "LARGE_READX", run_large_readx, 0},
        { "NTTRANS-CREATE", run_nttrans_create, 0},
        { "NTTRANS-FSCTL", run_nttrans_fsctl, 0},
        { "CLI_ECHO", run_cli_echo, 0},
@@ -8916,18 +9591,29 @@ static struct {
        { "SMB2-SESSION-REAUTH", run_smb2_session_reauth },
        { "CLEANUP1", run_cleanup1 },
        { "CLEANUP2", run_cleanup2 },
+       { "CLEANUP3", run_cleanup3 },
+       { "CLEANUP4", run_cleanup4 },
+       { "OPLOCK-CANCEL", run_oplock_cancel },
        { "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-MESSAGING-READ1", run_messaging_read1, 0 },
+       { "LOCAL-MESSAGING-READ2", run_messaging_read2, 0 },
+       { "LOCAL-MESSAGING-READ3", run_messaging_read3, 0 },
+       { "LOCAL-MESSAGING-READ4", run_messaging_read4, 0 },
+       { "LOCAL-MESSAGING-FDPASS1", run_messaging_fdpass1, 0 },
+       { "LOCAL-MESSAGING-FDPASS2", run_messaging_fdpass2, 0 },
+       { "LOCAL-MESSAGING-FDPASS2a", run_messaging_fdpass2a, 0 },
+       { "LOCAL-MESSAGING-FDPASS2b", run_messaging_fdpass2b, 0 },
        { "LOCAL-BASE64", run_local_base64, 0},
        { "LOCAL-RBTREE", run_local_rbtree, 0},
        { "LOCAL-MEMCACHE", run_local_memcache, 0},
        { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
-       { "LOCAL-WBCLIENT", run_local_wbclient, 0},
+       { "WBCLIENT-MULTI-PING", run_wbclient_multi_ping, 0},
        { "LOCAL-string_to_sid", run_local_string_to_sid, 0},
+       { "LOCAL-sid_to_string", run_local_sid_to_string, 0},
        { "LOCAL-binary_to_sid", run_local_binary_to_sid, 0},
        { "LOCAL-DBTRANS", run_local_dbtrans, 0},
        { "LOCAL-TEVENT-SELECT", run_local_tevent_select, 0},
@@ -8935,9 +9621,23 @@ static struct {
        { "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},
+       { "local-tdb-opener", run_local_tdb_opener, 0 },
+       { "local-tdb-writer", run_local_tdb_writer, 0 },
+       { "LOCAL-DBWRAP-CTDB", run_local_dbwrap_ctdb, 0 },
+       { "LOCAL-BENCH-PTHREADPOOL", run_bench_pthreadpool, 0 },
+       { "qpathinfo-bufsize", run_qpathinfo_bufsize, 0 },
        {NULL, NULL, 0}};
 
-
+/*
+ * dummy function to satisfy linker dependency
+ */
+struct tevent_context *winbind_event_context(void);
+struct tevent_context *winbind_event_context(void)
+{
+       return NULL;
+}
 
 /****************************************************************************
 run a specified test or "ALL"
@@ -9002,7 +9702,7 @@ static void usage(void)
 
        printf("\t-d debuglevel\n");
        printf("\t-U user%%pass\n");
-       printf("\t-k               use kerberos\n");
+       printf("\t-k                    use kerberos\n");
        printf("\t-N numprocs\n");
        printf("\t-n my_netbios_name\n");
        printf("\t-W workgroup\n");
@@ -9010,12 +9710,13 @@ static void usage(void)
        printf("\t-O socket_options\n");
        printf("\t-m maximum protocol\n");
        printf("\t-L use oplocks\n");
-       printf("\t-c CLIENT.TXT   specify client load file for NBENCH\n");
+       printf("\t-c CLIENT.TXT         specify client load file for NBENCH\n");
        printf("\t-A showall\n");
        printf("\t-p port\n");
        printf("\t-s seed\n");
        printf("\t-b unclist_filename   specify multiple shares for multiple connections\n");
-       printf("\t-f filename   filename to test\n");
+       printf("\t-f filename           filename to test\n");
+       printf("\t-e                    encrypt\n");
        printf("\n\n");
 
        printf("tests are:");
@@ -9048,7 +9749,8 @@ static void usage(void)
 
        setup_logging("smbtorture", DEBUG_STDOUT);
 
-       load_case_tables();
+       smb_init_locale();
+       fault_setup();
 
        if (is_default_dyn_CONFIGFILE()) {
                if(getenv("SMB_CONF_PATH")) {
@@ -9106,7 +9808,7 @@ static void usage(void)
                        fstrcpy(workgroup,optarg);
                        break;
                case 'm':
-                       max_protocol = interpret_protocol(optarg, max_protocol);
+                       lp_set_cmdline("client max protocol", optarg);
                        break;
                case 'N':
                        torture_nprocs = atoi(optarg);
@@ -9179,9 +9881,12 @@ static void usage(void)
        if(use_kerberos && !gotuser) gotpass = True;
 
        while (!gotpass) {
-               p = getpass("Password:");
-               if (p) {
-                       fstrcpy(password, p);
+               char pwd[256] = {0};
+               int rc;
+
+               rc = samba_getpass("Password:", pwd, sizeof(pwd), false, false);
+               if (rc == 0) {
+                       fstrcpy(password, pwd);
                        gotpass = 1;
                }
        }