Add in secure channel type used by AD trusts
[kai/samba.git] / source4 / torture / locktest.c
index 08050fc40a069bab9666ae83f852e7fa1d83ce73..a511e261cb9606dd74718ef9bf4179b6cf96351e 100644 (file)
@@ -18,6 +18,8 @@
 */
 
 #include "includes.h"
+#include "lib/cmdline/popt_common.h"
+#include "lib/events/events.h"
 #include "system/filesys.h"
 #include "system/time.h"
 #include "pstring.h"
 #include "auth/gensec/gensec.h"
 #include "libcli/libcli.h"
 #include "param/param.h"
-#include "dynconfig.h"
+#include "dynconfig/dynconfig.h"
+#include "libcli/resolve/resolve.h"
 
 static int numops = 1000;
-static bool showall;
-static bool analyze;
-static bool hide_unlock_fails;
-static bool use_oplocks;
+static int showall;
+static int analyze;
+static int hide_unlock_fails;
+static int use_oplocks;
 static uint_t lock_range = 100;
 static uint_t lock_base = 0;
 static uint_t min_length = 0;
-static bool exact_error_codes;
-static bool zero_zero;
+static int exact_error_codes;
+static int zero_zero;
 
 #define FILENAME "\\locktest.dat"
 
@@ -105,13 +108,19 @@ static struct record *recorded;
 /***************************************************** 
 return a connection to a server
 *******************************************************/
-static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
+static struct smbcli_state *connect_one(struct event_context *ev,
+                                       struct loadparm_context *lp_ctx,
                                        char *share, int snum, int conn)
 {
        struct smbcli_state *c;
        fstring server, myname;
        NTSTATUS status;
        int retries = 10;
+       struct smbcli_options options;
+       struct smbcli_session_options session_options;
+
+       lp_smbcli_options(lp_ctx, &options);
+       lp_smbcli_session_options(lp_ctx, &session_options);
 
        printf("connect_one(%s, %d, %d)\n", share, snum, conn);
 
@@ -153,8 +162,11 @@ static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
                printf("\\\\%s\\%s\n", server, share);
                status = smbcli_full_connection(NULL, &c, 
                                                server, 
+                                               lp_smb_ports(lp_ctx),
                                                share, NULL,
-                                               servers[snum], NULL);
+                                               servers[snum], 
+                                               lp_resolve_context(lp_ctx),
+                                               ev, &options, &session_options);
                if (!NT_STATUS_IS_OK(status)) {
                        sleep(2);
                }
@@ -168,7 +180,8 @@ static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
 }
 
 
-static void reconnect(struct loadparm_context *lp_ctx,
+static void reconnect(struct event_context *ev,
+                     struct loadparm_context *lp_ctx,
                      struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
                      char *share[NSERVERS])
 {
@@ -185,7 +198,7 @@ static void reconnect(struct loadparm_context *lp_ctx,
                        }
                        talloc_free(cli[server][conn]);
                }
-               cli[server][conn] = connect_one(lp_ctx, share[server], 
+               cli[server][conn] = connect_one(ev, lp_ctx, share[server], 
                                                server, conn);
                if (!cli[server][conn]) {
                        DEBUG(0,("Failed to connect to %s\n", share[server]));
@@ -388,7 +401,9 @@ static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
    we then do random locking ops in tamdem on the 4 fnums from each
    server and ensure that the results match
  */
-static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
+static int test_locks(struct event_context *ev,
+                     struct loadparm_context *lp_ctx,
+                     char *share[NSERVERS])
 {
        struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
        int fnum[NSERVERS][NCONNECTIONS][NFILES];
@@ -439,11 +454,16 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
 #endif
        }
 
-       reconnect(lp_ctx, cli, fnum, share);
+       reconnect(ev, lp_ctx, cli, fnum, share);
        open_files(cli, fnum);
        n = retest(cli, fnum, numops);
 
-       if (n == numops || !analyze) return;
+       if (n == numops || !analyze) {
+               if (n != numops) {
+                       return 1;
+               }
+               return 0;
+       }
        n++;
 
        skip = n/2;
@@ -452,7 +472,7 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
                n1 = n;
 
                close_files(cli, fnum);
-               reconnect(lp_ctx, cli, fnum, share);
+               reconnect(ev, lp_ctx, cli, fnum, share);
                open_files(cli, fnum);
 
                for (i=0;i<n-skip;i+=skip) {
@@ -490,7 +510,7 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
        }
 
        close_files(cli, fnum);
-       reconnect(lp_ctx, cli, fnum, share);
+       reconnect(ev, lp_ctx, cli, fnum, share);
        open_files(cli, fnum);
        showall = true;
        n1 = retest(cli, fnum, n);
@@ -509,30 +529,16 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
                       (double)recorded[i].len,
                       recorded[i].needed);
        }       
+
+       return 1;
 }
 
 
 
-static void usage(void)
+static void usage(poptContext pc)
 {
-       printf(
-"Usage:\n\
-  locktest //server1/share1 //server2/share2 [options..]\n\
-  options:\n\
-        -U user%%pass        (may be specified twice)\n\
-        -s seed\n\
-        -o numops\n\
-        -u          hide unlock fails\n\
-        -a          (show all ops)\n\
-        -A          analyse for minimal ops\n\
-        -O          use oplocks\n\
-        -E          enable exact error code checking\n\
-        -Z          enable the zero/zero lock\n\
-        -R range    set lock range\n\
-        -B base     set lock base\n\
-        -M min      set min lock length\n\
-        -l filename unclist file\n\
-");
+       printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
+       poptPrintUsage(pc, stdout, 0);
 }
 
 /****************************************************************************
@@ -544,114 +550,103 @@ static void usage(void)
        int opt;
        int seed, server;
        int username_count=0;
+       struct event_context *ev;
        struct loadparm_context *lp_ctx;
+       poptContext pc;
+       int argc_new, i;
+       char **argv_new;
+       enum {OPT_UNCLIST=1000};
+       struct poptOption long_options[] = {
+               POPT_AUTOHELP
+               {"seed",          0, POPT_ARG_INT,  &seed,      0,      "Seed to use for randomizer",   NULL},
+               {"num-ops",       0, POPT_ARG_INT,  &numops,    0,      "num ops",      NULL},
+               {"lockrange",     0, POPT_ARG_INT,  &lock_range,0,      "locking range", NULL},
+               {"lockbase",      0, POPT_ARG_INT,  &lock_base, 0,      "locking base", NULL},
+               {"minlength",     0, POPT_ARG_INT,  &min_length,0,      "min lock length", NULL},
+               {"hidefails",     0, POPT_ARG_NONE, &hide_unlock_fails,0,"hide unlock fails", NULL},
+               {"oplocks",       0, POPT_ARG_NONE, &use_oplocks,0,      "use oplocks", NULL},
+               {"showall",       0, POPT_ARG_NONE, &showall,    0,      "display all operations", NULL},
+               {"analyse",       0, POPT_ARG_NONE, &analyze,    0,      "do backtrack analysis", NULL},
+               {"zerozero",      0, POPT_ARG_NONE, &zero_zero,    0,      "do zero/zero lock", NULL},
+               {"exacterrors",   0, POPT_ARG_NONE, &exact_error_codes,0,"use exact error codes", NULL},
+               {"unclist",       0, POPT_ARG_STRING,   NULL,   OPT_UNCLIST,    "unclist",      NULL},
+               { "user", 'U',       POPT_ARG_STRING, NULL, 'U', "Set the network username", "[DOMAIN/]USERNAME[%PASSWORD]" },
+               POPT_COMMON_SAMBA
+               POPT_COMMON_CONNECTION
+               POPT_COMMON_CREDENTIALS
+               POPT_COMMON_VERSION
+               { NULL }
+       };
 
        setlinebuf(stdout);
+       seed = time(NULL);
 
-       setup_logging("locktest", DEBUG_STDOUT);
-
-       if (argc < 3 || argv[1][0] == '-') {
-               usage();
-               exit(1);
-       }
-
-       setup_logging(argv[0], DEBUG_STDOUT);
-
-       for (server=0;server<NSERVERS;server++) {
-               share[server] = argv[1+server];
-               all_string_sub(share[server],"/","\\",0);
-       }
-
-       argc -= NSERVERS;
-       argv += NSERVERS;
+       pc = poptGetContext("locktest", argc, (const char **) argv, long_options, 
+                           POPT_CONTEXT_KEEP_FIRST);
 
-       lp_load(dyn_CONFIGFILE, &lp_ctx);
+       poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
 
+       lp_ctx = cmdline_lp_ctx;
        servers[0] = cli_credentials_init(talloc_autofree_context());
        servers[1] = cli_credentials_init(talloc_autofree_context());
        cli_credentials_guess(servers[0], lp_ctx);
        cli_credentials_guess(servers[1], lp_ctx);
 
-       seed = time(NULL);
-
-       while ((opt = getopt(argc, argv, "U:s:ho:aAW:OR:B:M:EZW:l:")) != EOF) {
+       while((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
+               case OPT_UNCLIST:
+                       lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
+                       break;
                case 'U':
                        if (username_count == 2) {
-                               usage();
+                               usage(pc);
                                exit(1);
                        }
-                       cli_credentials_parse_string(servers[username_count], 
-                                                    optarg, CRED_SPECIFIED);
+                       cli_credentials_parse_string(servers[username_count], poptGetOptArg(pc), CRED_SPECIFIED);
                        username_count++;
                        break;
-               case 'R':
-                       lock_range = strtol(optarg, NULL, 0);
-                       break;
-               case 'B':
-                       lock_base = strtol(optarg, NULL, 0);
-                       break;
-               case 'M':
-                       min_length = strtol(optarg, NULL, 0);
-                       break;
-               case 's':
-                       seed = atoi(optarg);
-                       break;
-               case 'u':
-                       hide_unlock_fails = true;
-                       break;
-               case 'o':
-                       numops = atoi(optarg);
-                       break;
-               case 'O':
-                       use_oplocks = true;
-                       break;
-               case 'a':
-                       showall = true;
-                       break;
-               case 'A':
-                       analyze = true;
-                       break;
-               case 'Z':
-                       zero_zero = true;
-                       break;
-               case 'E':
-                       exact_error_codes = true;
-                       break;
-               case 'l':
-                       lp_set_cmdline(lp_ctx, "torture:unclist", optarg);
-                       break;
-               case 'W':
-                       lp_set_cmdline(lp_ctx, "workgroup", optarg);
+               }
+       }
+
+       argv_new = discard_const_p(char *, poptGetArgs(pc));
+       argc_new = argc;
+       for (i=0; i<argc; i++) {
+               if (argv_new[i] == NULL) {
+                       argc_new = i;
                        break;
-               case 'h':
-                       usage();
-                       exit(1);
-               default:
-                       printf("Unknown option %c (%d)\n", (char)opt, opt);
-                       exit(1);
                }
        }
 
+       if (!(argc_new >= 3)) {
+               usage(pc);
+               exit(1);
+       }
+
+       setup_logging("locktest", DEBUG_STDOUT);
+
+       for (server=0;server<NSERVERS;server++) {
+               share[server] = argv_new[1+server];
+               all_string_sub(share[server],"/","\\",0);
+       }
+
+       lp_ctx = cmdline_lp_ctx;
+
        if (username_count == 0) {
-               usage();
+               usage(pc);
                return -1;
        }
        if (username_count == 1) {
                servers[1] = servers[0];
        }
 
-       gensec_init(lp_ctx);
+       ev = s4_event_context_init(talloc_autofree_context());
 
-       argc -= optind;
-       argv += optind;
+       gensec_init(lp_ctx);
 
        DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
                 seed, lock_base, lock_range, min_length));
        srandom(seed);
 
-       test_locks(lp_ctx, share);
-
-       return(0);
+       return test_locks(ev, lp_ctx, share);
 }