1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
Unix SMB/CIFS implementation.
SMB torture tester
Copyright (C) Andrew Tridgell 1997-2003
Copyright (C) Jelmer Vernooij 2006-2008
Copyright (C) James Peach 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "system/readline.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;
typedef void (*shell_function)(const struct shell_command *,
struct torture_context *, int, const char **);
static void shell_quit(const struct shell_command *,
struct torture_context *, int, const char **);
static void shell_help(const struct shell_command *,
struct torture_context *, int, const char **);
static void shell_set(const struct shell_command *,
struct torture_context *, int, const char **);
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 *);
struct shell_command
{
shell_function handler;
const char * name;
const char * usage;
const char * help;
} 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_run, "run", "[TESTNAME]",
"run the specified test"
},
{
shell_set, "set", "[NAME VALUE]",
"print or set test configuration parameters"
},
{
shell_target, "target", "[TARGET]",
"print or set the test target"
}
};
void torture_shell(struct torture_context *tctx)
{
char *cline;
int argc;
const char **argv;
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.
*/
if (cmdline_credentials->password_obtained != CRED_SPECIFIED) {
cli_credentials_set_password(cmdline_credentials, "",
CRED_SPECIFIED);
}
while (1) {
cline = smb_readline("torture> ", NULL, NULL);
if (cline == NULL)
return;
#if HAVE_ADD_HISTORY
add_history(cline);
#endif
ret = poptParseArgvString(cline, &argc, &argv);
if (ret != 0) {
fprintf(stderr, "Error parsing line\n");
continue;
}
for (i = 0; i < ARRAY_SIZE(commands); i++) {
if (match_command(argv[0], &commands[i])) {
argc--;
argv++;
commands[i].handler(&commands[i],
tctx, argc, argv);
break;
}
}
free(cline);
}
}
static void shell_quit(const struct shell_command * command,
struct torture_context *tctx, int argc, const char **argv)
{
exit(0);
}
static void shell_help(const struct shell_command * command,
struct torture_context *tctx, int argc, const char **argv)
{
int i;
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)
{
switch (argc) {
case 0:
lpcfg_dump(tctx->lp_ctx, stdout,
false /* show_defaults */,
0 /* skip services */);
break;
case 2:
/* 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:
shell_usage(command);
}
}
static void shell_run(const struct shell_command * command,
struct torture_context *tctx, int argc, const char **argv)
{
if (argc != 1) {
shell_usage(command);
return;
}
torture_run_named_tests(tctx, argv[0], NULL /* restricted */);
}
static void shell_list(const struct shell_command * command,
struct torture_context *tctx, int argc, const char **argv)
{
if (argc != 0) {
shell_usage(command);
return;
}
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(cmdline_credentials);
principal = cli_credentials_get_principal(cmdline_credentials, tctx);
domain = cli_credentials_get_domain(cmdline_credentials);
realm = cli_credentials_get_realm(cmdline_credentials);
password = cli_credentials_get_password(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(
cmdline_credentials, argv[1], CRED_SPECIFIED);
} else if (!strcmp(argv[0], "principal")) {
result = cli_credentials_set_principal(
cmdline_credentials, argv[1], CRED_SPECIFIED);
} else if (!strcmp(argv[0], "domain")) {
result = cli_credentials_set_domain(
cmdline_credentials, argv[1], CRED_SPECIFIED);
} else if (!strcmp(argv[0], "realm")) {
result = cli_credentials_set_realm(
cmdline_credentials, argv[1], CRED_SPECIFIED);
} else if (!strcmp(argv[0], "password")) {
result = cli_credentials_set_password(
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->lp_ctx, argv[0]);
} else {
shell_usage(command);
}
}
static void shell_usage(const struct shell_command * command)
{
if (command->usage) {
fprintf(stderr, "Usage: %s %s\n",
command->name, command->usage);
} else {
fprintf(stderr, "Usage: %s\n",
command->name);
}
}
static bool match_command(const char * name,
const struct shell_command * command)
{
if (!strcmp(name, command->name)) {
return true;
}
if (name[0] == command->name[0] && name[1] == '\0') {
return true;
}
return false;
}
|