s4: popt: Global replace of cmdline_credentials -> popt_get_cmdline_credentials().
[gd/samba-autobuild/.git] / source4 / torture / shell.c
index 31efb28447cdc208edbd87aa900a19219fb6472c..5a4eb5c1d6a1b60482ffaf8704e5bab66ee94ef4 100644 (file)
 
 #include "includes.h"
 #include "system/readline.h"
-#include "lib/smbreadline/smbreadline.h"
+#include "../libcli/smbreadline/smbreadline.h"
+#include "lib/cmdline/popt_common.h"
+#include "auth/credentials/credentials.h"
 #include "torture/smbtorture.h"
+#include "param/param.h"
 
 struct shell_command;
 
@@ -39,6 +42,10 @@ static void shell_run(const struct shell_command *,
        struct torture_context *, int, const char **);
 static void shell_list(const struct shell_command *,
        struct torture_context *, int, const char **);
+static void shell_auth(const struct shell_command *,
+       struct torture_context *, int, const char **);
+static void shell_target(const struct shell_command *,
+       struct torture_context *, int, const char **);
 
 static void shell_usage(const struct shell_command *);
 static bool match_command(const char *, const struct shell_command *);
@@ -53,19 +60,30 @@ struct shell_command
 
 static const struct shell_command commands[] =
 {
+    {
+       shell_auth, "auth",
+       "[[username | principal | domain | realm | password] STRING]",
+       "set authentication parameters"
+    },
+
     {
        shell_help, "help", NULL,
        "print this help message"
     },
 
+    {
+       shell_list, "list", NULL,
+       "list the available tests"
+    },
+
     {
        shell_quit, "quit", NULL,
        "exit smbtorture"
     },
 
     {
-       shell_list, "list", NULL,
-       "list the available tests"
+       shell_run, "run", "[TESTNAME]",
+       "run the specified test"
     },
 
     {
@@ -74,9 +92,10 @@ static const struct shell_command commands[] =
     },
 
     {
-       shell_run, "run", "[TESTNAME]",
-       "run the specified test"
+       shell_target, "target", "[TARGET]",
+       "print or set the test target"
     }
+
 };
 
 void torture_shell(struct torture_context *tctx)
@@ -87,6 +106,13 @@ void torture_shell(struct torture_context *tctx)
        int ret;
        int i;
 
+       /* If we don't have a specified password, specify it as empty. This
+        * stops the credentials system prompting when we use the "auth"
+        * command to display the current auth parameters.
+        */
+       cli_credentials_set_password(popt_get_cmdline_credentials(),
+                       "", CRED_GUESS_ENV);
+
        while (1) {
                cline = smb_readline("torture> ", NULL, NULL);
 
@@ -128,29 +154,39 @@ static void shell_help(const struct shell_command * command,
 {
     int i;
 
-    fprintf(stdout, "Available commands:\n");
-    for (i = 0; i < ARRAY_SIZE(commands); i++) {
-           fprintf(stdout, "\t%s - %s\n",
-                   commands[i].name, commands[i].help);
+    if (argc == 1) {
+           for (i = 0; i < ARRAY_SIZE(commands); i++) {
+                   if (match_command(argv[0], &commands[i])) {
+                           shell_usage(&commands[i]);
+                           return;
+                   }
+           }
+    } else {
+           fprintf(stdout, "Available commands:\n");
+           for (i = 0; i < ARRAY_SIZE(commands); i++) {
+                   fprintf(stdout, "\t%s - %s\n",
+                           commands[i].name, commands[i].help);
+           }
     }
 }
 
 static void shell_set(const struct shell_command *command,
        struct torture_context *tctx, int argc, const char **argv)
 {
-       char * name;
-
        switch (argc) {
        case 0:
-           lp_dump(tctx->lp_ctx, stdout,
+           lpcfg_dump(tctx->lp_ctx, stdout,
                    false /* show_defaults */,
                    0 /* skip services */);
            break;
 
        case 2:
-           name = talloc_asprintf(NULL, "torture:%s", argv[0]);
-           lp_set_cmdline(tctx->lp_ctx, name, argv[1]);
-           talloc_free(name);
+           /* We want to allow users to set any config option. Top level
+            * options will get checked against their static definition, but
+            * parametric options can't be checked and will just get stashed
+            * as they are provided.
+            */
+           lpcfg_set_cmdline(tctx->lp_ctx, argv[0], argv[1]);
            break;
 
        default:
@@ -177,7 +213,91 @@ static void shell_list(const struct shell_command * command,
        return;
     }
 
-    torture_print_tests(true);
+    torture_print_testsuites(true);
+}
+
+static void shell_auth(const struct shell_command * command,
+       struct torture_context *tctx, int argc, const char **argv)
+{
+
+    if (argc == 0) {
+           const char * username;
+           const char * domain;
+           const char * realm;
+           const char * password;
+           const char * principal;
+
+           username = cli_credentials_get_username(
+                       popt_get_cmdline_credentials());
+           principal = cli_credentials_get_principal(
+                               popt_get_cmdline_credentials(), tctx);
+           domain = cli_credentials_get_domain(popt_get_cmdline_credentials());
+           realm = cli_credentials_get_realm(popt_get_cmdline_credentials());
+           password = cli_credentials_get_password(
+                               popt_get_cmdline_credentials());
+
+           printf("Username: %s\n", username ? username : "");
+           printf("User Principal: %s\n", principal ? principal : "");
+           printf("Domain: %s\n", domain ? domain : "");
+           printf("Realm: %s\n", realm ? realm : "");
+           printf("Password: %s\n", password ? password : "");
+    } else if (argc == 2) {
+           bool result;
+
+           if (!strcmp(argv[0], "username")) {
+                   result = cli_credentials_set_username(
+                       popt_get_cmdline_credentials(),
+                       argv[1], CRED_SPECIFIED);
+           } else if (!strcmp(argv[0], "principal")) {
+                   result = cli_credentials_set_principal(
+                       popt_get_cmdline_credentials(),
+                       argv[1], CRED_SPECIFIED);
+           } else if (!strcmp(argv[0], "domain")) {
+                   result = cli_credentials_set_domain(
+                       popt_get_cmdline_credentials(),
+                       argv[1], CRED_SPECIFIED);
+           } else if (!strcmp(argv[0], "realm")) {
+                   result = cli_credentials_set_realm(
+                       popt_get_cmdline_credentials(),
+                       argv[1], CRED_SPECIFIED);
+           } else if (!strcmp(argv[0], "password")) {
+                   result = cli_credentials_set_password(
+                       popt_get_cmdline_credentials(),
+                       argv[1], CRED_SPECIFIED);
+           } else {
+                   shell_usage(command);
+                   return;
+           }
+
+           if (!result) {
+                   printf("failed to set %s\n", argv[0]);
+           }
+    } else {
+           shell_usage(command);
+    }
+
+}
+
+static void shell_target(const struct shell_command *command,
+       struct torture_context *tctx, int argc, const char **argv)
+{
+       if (argc == 0) {
+               const char * host;
+               const char * share;
+               const char * binding;
+
+               host = torture_setting_string(tctx, "host", NULL);
+               share = torture_setting_string(tctx, "share", NULL);
+               binding = torture_setting_string(tctx, "binding", NULL);
+
+               printf("Target host: %s\n", host ? host : "");
+               printf("Target share: %s\n", share ? share : "");
+               printf("Target binding: %s\n", binding ? binding : "");
+       } else if (argc == 1) {
+               torture_parse_target(tctx, tctx->lp_ctx, argv[0]);
+       } else {
+               shell_usage(command);
+       }
 }
 
 static void shell_usage(const struct shell_command * command)
@@ -204,4 +324,3 @@ static bool match_command(const char * name,
 
        return false;
 }
-