source: trunk/server/source4/torture/shell.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 8.6 KB
Line 
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
7
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.
12
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.
17
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/>.
20*/
21
22#include "includes.h"
23#include "system/readline.h"
24#include "../libcli/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"
29
30struct shell_command;
31
32typedef void (*shell_function)(const struct shell_command *,
33 struct torture_context *, int, const char **);
34
35static void shell_quit(const struct shell_command *,
36 struct torture_context *, int, const char **);
37static void shell_help(const struct shell_command *,
38 struct torture_context *, int, const char **);
39static void shell_set(const struct shell_command *,
40 struct torture_context *, int, const char **);
41static void shell_run(const struct shell_command *,
42 struct torture_context *, int, const char **);
43static void shell_list(const struct shell_command *,
44 struct torture_context *, int, const char **);
45static void shell_auth(const struct shell_command *,
46 struct torture_context *, int, const char **);
47static void shell_target(const struct shell_command *,
48 struct torture_context *, int, const char **);
49
50static void shell_usage(const struct shell_command *);
51static bool match_command(const char *, const struct shell_command *);
52
53struct shell_command
54{
55 shell_function handler;
56 const char * name;
57 const char * usage;
58 const char * help;
59} shell_command;
60
61static const struct shell_command commands[] =
62{
63 {
64 shell_auth, "auth",
65 "[[username | principal | domain | realm | password] STRING]",
66 "set authentication parameters"
67 },
68
69 {
70 shell_help, "help", NULL,
71 "print this help message"
72 },
73
74 {
75 shell_list, "list", NULL,
76 "list the available tests"
77 },
78
79 {
80 shell_quit, "quit", NULL,
81 "exit smbtorture"
82 },
83
84 {
85 shell_run, "run", "[TESTNAME]",
86 "run the specified test"
87 },
88
89 {
90 shell_set, "set", "[NAME VALUE]",
91 "print or set test configuration parameters"
92 },
93
94 {
95 shell_target, "target", "[TARGET]",
96 "print or set the test target"
97 }
98
99};
100
101void torture_shell(struct torture_context *tctx)
102{
103 char *cline;
104 int argc;
105 const char **argv;
106 int ret;
107 int i;
108
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.
112 */
113 if (cmdline_credentials->password_obtained != CRED_SPECIFIED) {
114 cli_credentials_set_password(cmdline_credentials, "",
115 CRED_SPECIFIED);
116 }
117
118 while (1) {
119 cline = smb_readline("torture> ", NULL, NULL);
120
121 if (cline == NULL)
122 return;
123
124#if HAVE_ADD_HISTORY
125 add_history(cline);
126#endif
127
128 ret = poptParseArgvString(cline, &argc, &argv);
129 if (ret != 0) {
130 fprintf(stderr, "Error parsing line\n");
131 continue;
132 }
133
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;
141 }
142 }
143
144 free(cline);
145 }
146}
147
148static void shell_quit(const struct shell_command * command,
149 struct torture_context *tctx, int argc, const char **argv)
150{
151 exit(0);
152}
153
154static void shell_help(const struct shell_command * command,
155 struct torture_context *tctx, int argc, const char **argv)
156{
157 int i;
158
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;
164 }
165 }
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);
171 }
172 }
173}
174
175static void shell_set(const struct shell_command *command,
176 struct torture_context *tctx, int argc, const char **argv)
177{
178 switch (argc) {
179 case 0:
180 lpcfg_dump(tctx->lp_ctx, stdout,
181 false /* show_defaults */,
182 0 /* skip services */);
183 break;
184
185 case 2:
186 /* We want to allow users to set any config option. Top level
187 * options will get checked against their static definition, but
188 * parametric options can't be checked and will just get stashed
189 * as they are provided.
190 */
191 lpcfg_set_cmdline(tctx->lp_ctx, argv[0], argv[1]);
192 break;
193
194 default:
195 shell_usage(command);
196 }
197}
198
199static void shell_run(const struct shell_command * command,
200 struct torture_context *tctx, int argc, const char **argv)
201{
202 if (argc != 1) {
203 shell_usage(command);
204 return;
205 }
206
207 torture_run_named_tests(tctx, argv[0], NULL /* restricted */);
208}
209
210static void shell_list(const struct shell_command * command,
211 struct torture_context *tctx, int argc, const char **argv)
212{
213 if (argc != 0) {
214 shell_usage(command);
215 return;
216 }
217
218 torture_print_testsuites(true);
219}
220
221static void shell_auth(const struct shell_command * command,
222 struct torture_context *tctx, int argc, const char **argv)
223{
224
225 if (argc == 0) {
226 const char * username;
227 const char * domain;
228 const char * realm;
229 const char * password;
230 const char * principal;
231
232 username = cli_credentials_get_username(cmdline_credentials);
233 principal = cli_credentials_get_principal(cmdline_credentials, tctx);
234 domain = cli_credentials_get_domain(cmdline_credentials);
235 realm = cli_credentials_get_realm(cmdline_credentials);
236 password = cli_credentials_get_password(cmdline_credentials);
237
238 printf("Username: %s\n", username ? username : "");
239 printf("User Principal: %s\n", principal ? principal : "");
240 printf("Domain: %s\n", domain ? domain : "");
241 printf("Realm: %s\n", realm ? realm : "");
242 printf("Password: %s\n", password ? password : "");
243 } else if (argc == 2) {
244 bool result;
245
246 if (!strcmp(argv[0], "username")) {
247 result = cli_credentials_set_username(
248 cmdline_credentials, argv[1], CRED_SPECIFIED);
249 } else if (!strcmp(argv[0], "principal")) {
250 result = cli_credentials_set_principal(
251 cmdline_credentials, argv[1], CRED_SPECIFIED);
252 } else if (!strcmp(argv[0], "domain")) {
253 result = cli_credentials_set_domain(
254 cmdline_credentials, argv[1], CRED_SPECIFIED);
255 } else if (!strcmp(argv[0], "realm")) {
256 result = cli_credentials_set_realm(
257 cmdline_credentials, argv[1], CRED_SPECIFIED);
258 } else if (!strcmp(argv[0], "password")) {
259 result = cli_credentials_set_password(
260 cmdline_credentials, argv[1], CRED_SPECIFIED);
261 } else {
262 shell_usage(command);
263 return;
264 }
265
266 if (!result) {
267 printf("failed to set %s\n", argv[0]);
268 }
269 } else {
270 shell_usage(command);
271 }
272
273}
274
275static void shell_target(const struct shell_command *command,
276 struct torture_context *tctx, int argc, const char **argv)
277{
278 if (argc == 0) {
279 const char * host;
280 const char * share;
281 const char * binding;
282
283 host = torture_setting_string(tctx, "host", NULL);
284 share = torture_setting_string(tctx, "share", NULL);
285 binding = torture_setting_string(tctx, "binding", NULL);
286
287 printf("Target host: %s\n", host ? host : "");
288 printf("Target share: %s\n", share ? share : "");
289 printf("Target binding: %s\n", binding ? binding : "");
290 } else if (argc == 1) {
291 torture_parse_target(tctx->lp_ctx, argv[0]);
292 } else {
293 shell_usage(command);
294 }
295}
296
297static void shell_usage(const struct shell_command * command)
298{
299 if (command->usage) {
300 fprintf(stderr, "Usage: %s %s\n",
301 command->name, command->usage);
302 } else {
303 fprintf(stderr, "Usage: %s\n",
304 command->name);
305 }
306}
307
308static bool match_command(const char * name,
309 const struct shell_command * command)
310{
311 if (!strcmp(name, command->name)) {
312 return true;
313 }
314
315 if (name[0] == command->name[0] && name[1] == '\0') {
316 return true;
317 }
318
319 return false;
320}
Note: See TracBrowser for help on using the repository browser.