s3-winbind: Implemented samr backend function sam_trusted_domains.
[Samba/gebeck_regimport.git] / source4 / torture / shell.c
blob3f48075dba06d492a1a14ff94bb6321129dc1b3c
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Andrew Tridgell 1997-2003
5 Copyright (C) Jelmer Vernooij 2006-2008
6 Copyright (C) James Peach 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/readline.h"
24 #include "lib/smbreadline/smbreadline.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "auth/credentials/credentials.h"
27 #include "torture/smbtorture.h"
28 #include "param/param.h"
30 struct shell_command;
32 typedef void (*shell_function)(const struct shell_command *,
33 struct torture_context *, int, const char **);
35 static void shell_quit(const struct shell_command *,
36 struct torture_context *, int, const char **);
37 static void shell_help(const struct shell_command *,
38 struct torture_context *, int, const char **);
39 static void shell_set(const struct shell_command *,
40 struct torture_context *, int, const char **);
41 static void shell_run(const struct shell_command *,
42 struct torture_context *, int, const char **);
43 static void shell_list(const struct shell_command *,
44 struct torture_context *, int, const char **);
45 static void shell_auth(const struct shell_command *,
46 struct torture_context *, int, const char **);
47 static void shell_target(const struct shell_command *,
48 struct torture_context *, int, const char **);
50 static void shell_usage(const struct shell_command *);
51 static bool match_command(const char *, const struct shell_command *);
53 struct shell_command
55 shell_function handler;
56 const char * name;
57 const char * usage;
58 const char * help;
59 } shell_command;
61 static const struct shell_command commands[] =
64 shell_auth, "auth",
65 "[[username | principal | domain | realm | password] STRING]",
66 "set authentication parameters"
70 shell_help, "help", NULL,
71 "print this help message"
75 shell_list, "list", NULL,
76 "list the available tests"
80 shell_quit, "quit", NULL,
81 "exit smbtorture"
85 shell_run, "run", "[TESTNAME]",
86 "run the specified test"
90 shell_set, "set", "[NAME VALUE]",
91 "print or set test configuration parameters"
95 shell_target, "target", "[TARGET]",
96 "print or set the test target"
101 void torture_shell(struct torture_context *tctx)
103 char *cline;
104 int argc;
105 const char **argv;
106 int ret;
107 int i;
109 /* If we don't have a specified password, specify it as empty. This
110 * stops the credentials system prompting when we use the "auth"
111 * command to display the current auth parameters.
113 if (cmdline_credentials->password_obtained != CRED_SPECIFIED) {
114 cli_credentials_set_password(cmdline_credentials, "",
115 CRED_SPECIFIED);
118 while (1) {
119 cline = smb_readline("torture> ", NULL, NULL);
121 if (cline == NULL)
122 return;
124 #if HAVE_ADD_HISTORY
125 add_history(cline);
126 #endif
128 ret = poptParseArgvString(cline, &argc, &argv);
129 if (ret != 0) {
130 fprintf(stderr, "Error parsing line\n");
131 continue;
134 for (i = 0; i < ARRAY_SIZE(commands); i++) {
135 if (match_command(argv[0], &commands[i])) {
136 argc--;
137 argv++;
138 commands[i].handler(&commands[i],
139 tctx, argc, argv);
140 break;
144 free(cline);
148 static void shell_quit(const struct shell_command * command,
149 struct torture_context *tctx, int argc, const char **argv)
151 exit(0);
154 static void shell_help(const struct shell_command * command,
155 struct torture_context *tctx, int argc, const char **argv)
157 int i;
159 if (argc == 1) {
160 for (i = 0; i < ARRAY_SIZE(commands); i++) {
161 if (match_command(argv[0], &commands[i])) {
162 shell_usage(&commands[i]);
163 return;
166 } else {
167 fprintf(stdout, "Available commands:\n");
168 for (i = 0; i < ARRAY_SIZE(commands); i++) {
169 fprintf(stdout, "\t%s - %s\n",
170 commands[i].name, commands[i].help);
175 static void shell_set(const struct shell_command *command,
176 struct torture_context *tctx, int argc, const char **argv)
178 char * name;
180 switch (argc) {
181 case 0:
182 lp_dump(tctx->lp_ctx, stdout,
183 false /* show_defaults */,
184 0 /* skip services */);
185 break;
187 case 2:
188 name = talloc_asprintf(NULL, "torture:%s", argv[0]);
189 lp_set_cmdline(tctx->lp_ctx, name, argv[1]);
190 talloc_free(name);
191 break;
193 default:
194 shell_usage(command);
198 static void shell_run(const struct shell_command * command,
199 struct torture_context *tctx, int argc, const char **argv)
201 if (argc != 1) {
202 shell_usage(command);
203 return;
206 torture_run_named_tests(tctx, argv[0], NULL /* restricted */);
209 static void shell_list(const struct shell_command * command,
210 struct torture_context *tctx, int argc, const char **argv)
212 if (argc != 0) {
213 shell_usage(command);
214 return;
217 torture_print_tests(true);
220 static void shell_auth(const struct shell_command * command,
221 struct torture_context *tctx, int argc, const char **argv)
224 if (argc == 0) {
225 const char * username;
226 const char * domain;
227 const char * realm;
228 const char * password;
229 const char * principal;
231 username = cli_credentials_get_username(cmdline_credentials);
232 principal = cli_credentials_get_principal(cmdline_credentials, tctx);
233 domain = cli_credentials_get_domain(cmdline_credentials);
234 realm = cli_credentials_get_realm(cmdline_credentials);
235 password = cli_credentials_get_password(cmdline_credentials);
237 printf("Username: %s\n", username ? username : "");
238 printf("User Principal: %s\n", principal ? principal : "");
239 printf("Domain: %s\n", domain ? domain : "");
240 printf("Realm: %s\n", realm ? realm : "");
241 printf("Password: %s\n", password ? password : "");
242 } else if (argc == 2) {
243 bool result;
245 if (!strcmp(argv[0], "username")) {
246 result = cli_credentials_set_username(
247 cmdline_credentials, argv[1], CRED_SPECIFIED);
248 } else if (!strcmp(argv[0], "principal")) {
249 result = cli_credentials_set_principal(
250 cmdline_credentials, argv[1], CRED_SPECIFIED);
251 } else if (!strcmp(argv[0], "domain")) {
252 result = cli_credentials_set_domain(
253 cmdline_credentials, argv[1], CRED_SPECIFIED);
254 } else if (!strcmp(argv[0], "realm")) {
255 result = cli_credentials_set_realm(
256 cmdline_credentials, argv[1], CRED_SPECIFIED);
257 } else if (!strcmp(argv[0], "password")) {
258 result = cli_credentials_set_password(
259 cmdline_credentials, argv[1], CRED_SPECIFIED);
260 } else {
261 shell_usage(command);
262 return;
265 if (!result) {
266 printf("failed to set %s\n", argv[0]);
268 } else {
269 shell_usage(command);
274 static void shell_target(const struct shell_command *command,
275 struct torture_context *tctx, int argc, const char **argv)
277 if (argc == 0) {
278 const char * host;
279 const char * share;
280 const char * binding;
282 host = torture_setting_string(tctx, "host", NULL);
283 share = torture_setting_string(tctx, "share", NULL);
284 binding = torture_setting_string(tctx, "binding", NULL);
286 printf("Target host: %s\n", host ? host : "");
287 printf("Target share: %s\n", share ? share : "");
288 printf("Target binding: %s\n", binding ? binding : "");
289 } else if (argc == 1) {
290 torture_parse_target(tctx->lp_ctx, argv[0]);
291 } else {
292 shell_usage(command);
296 static void shell_usage(const struct shell_command * command)
298 if (command->usage) {
299 fprintf(stderr, "Usage: %s %s\n",
300 command->name, command->usage);
301 } else {
302 fprintf(stderr, "Usage: %s\n",
303 command->name);
307 static bool match_command(const char * name,
308 const struct shell_command * command)
310 if (!strcmp(name, command->name)) {
311 return true;
314 if (name[0] == command->name[0] && name[1] == '\0') {
315 return true;
318 return false;