s4-torture: add AES support for netr_ServerPasswordSet2 tests.
[Samba/bb.git] / source3 / torture / masktest.c
blob8332e806db4afe5a45fecc24ff8b2b2a5c9ad9f9
1 /*
2 Unix SMB/CIFS implementation.
3 mask_match tester
4 Copyright (C) Andrew Tridgell 1999
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "trans2.h"
23 #include "libsmb/libsmb.h"
24 #include "libsmb/nmblib.h"
25 #include "../libcli/smb/smbXcli_base.h"
27 static fstring password;
28 static fstring username;
29 static int got_pass;
30 static int max_protocol = PROTOCOL_NT1;
31 static bool showall = False;
32 static bool old_list = False;
33 static const char *maskchars = "<>\"?*abc.";
34 static const char *filechars = "abcdefghijklm.";
35 static int verbose;
36 static int die_on_error;
37 static int NumLoops = 0;
38 static int ignore_dot_errors = 0;
40 extern char *optarg;
41 extern int optind;
43 /* a test fn for LANMAN mask support */
44 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
46 const char *p = pattern, *n = string;
47 char c;
49 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
51 while ((c = *p++)) {
52 switch (c) {
53 case '.':
54 /* if (! *n && ! *p) goto match; */
55 if (*n != '.') goto nomatch;
56 n++;
57 break;
59 case '?':
60 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
61 n++;
62 break;
64 case '>':
65 if (n[0] == '.') {
66 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
67 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
68 goto nomatch;
70 if (! *n) goto next;
71 n++;
72 break;
74 case '*':
75 if (! *p) goto match;
76 for (; *n; n++) {
77 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
79 break;
81 case '<':
82 for (; *n; n++) {
83 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
84 if (*n == '.' && !strchr_m(n+1,'.')) {
85 n++;
86 break;
89 break;
91 case '"':
92 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
93 if (*n != '.') goto nomatch;
94 n++;
95 break;
97 default:
98 if (c != *n) goto nomatch;
99 n++;
103 if (! *n) goto match;
105 nomatch:
106 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
107 return -1;
109 next:
110 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
111 goto nomatch;
113 match:
114 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
115 return 0;
118 static int ms_fnmatch_lanman(const char *pattern, const char *string)
120 if (!strpbrk(pattern, "?*<>\"")) {
121 if (strcmp(string,"..") == 0)
122 string = ".";
124 return strcmp(pattern, string);
127 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
128 return ms_fnmatch_lanman_core(pattern, "..") &&
129 ms_fnmatch_lanman_core(pattern, ".");
132 return ms_fnmatch_lanman_core(pattern, string);
135 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
137 /* oh what a weird world this is */
138 if (old_list && strcmp(pattern, "*.*") == 0) return True;
140 if (strcmp(pattern,".") == 0) return False;
142 if (max_protocol <= PROTOCOL_LANMAN2) {
143 return ms_fnmatch_lanman(pattern, file)==0;
146 if (strcmp(file,"..") == 0) file = ".";
148 return ms_fnmatch(pattern, file, smbXcli_conn_protocol(cli->conn), False) == 0;
151 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
153 static fstring ret;
154 const char *new_pattern = 1+strrchr_m(pattern,'\\');
156 fstrcpy(ret, "---");
157 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
158 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
159 if (reg_match_one(cli, new_pattern, long_name) ||
160 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
161 return ret;
165 /*****************************************************
166 return a connection to a server
167 *******************************************************/
168 static struct cli_state *connect_one(char *share)
170 struct cli_state *c;
171 char *server_n;
172 char *server;
173 NTSTATUS status;
175 server = share+2;
176 share = strchr_m(server,'\\');
177 if (!share) return NULL;
178 *share = 0;
179 share++;
181 server_n = server;
183 status = cli_connect_nb(server, NULL, 0, 0x20, "masktest",
184 SMB_SIGNING_DEFAULT, 0, &c);
185 if (!NT_STATUS_IS_OK(status)) {
186 DEBUG(0,("Connection to %s failed. Error %s\n", server_n,
187 nt_errstr(status)));
188 return NULL;
191 status = smbXcli_negprot(c->conn, c->timeout, PROTOCOL_CORE,
192 max_protocol);
193 if (!NT_STATUS_IS_OK(status)) {
194 DEBUG(0, ("protocol negotiation failed: %s\n",
195 nt_errstr(status)));
196 cli_shutdown(c);
197 return NULL;
200 if (!got_pass) {
201 char pwd[256] = {0};
202 int rc;
204 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
205 if (rc == 0) {
206 fstrcpy(password, pass);
210 status = cli_session_setup(c, username,
211 password, strlen(password),
212 password, strlen(password),
213 lp_workgroup());
214 if (!NT_STATUS_IS_OK(status)) {
215 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
216 return NULL;
220 * These next two lines are needed to emulate
221 * old client behaviour for people who have
222 * scripts based on client output.
223 * QUESTION ? Do we want to have a 'client compatibility
224 * mode to turn these on/off ? JRA.
227 if (*c->server_domain || *c->server_os || *c->server_type)
228 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
229 c->server_domain,c->server_os,c->server_type));
231 DEBUG(4,(" session setup ok\n"));
233 status = cli_tree_connect(c, share, "?????", password,
234 strlen(password)+1);
235 if (!NT_STATUS_IS_OK(status)) {
236 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
237 cli_shutdown(c);
238 return NULL;
241 DEBUG(4,(" tconx ok\n"));
243 return c;
246 static char *resultp;
248 struct rn_state {
249 char **pp_long_name;
250 char *short_name;
253 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
254 void *private_data)
256 struct rn_state *state = (struct rn_state *)private_data;
257 if (strcmp(f->name,".") == 0) {
258 resultp[0] = '+';
259 } else if (strcmp(f->name,"..") == 0) {
260 resultp[1] = '+';
261 } else {
262 resultp[2] = '+';
265 if (state == NULL) {
266 return NT_STATUS_INTERNAL_ERROR;
269 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
270 return NT_STATUS_OK;
274 fstrcpy(state->short_name, f->short_name ? f->short_name : "");
275 (void)strlower_m(state->short_name);
276 *state->pp_long_name = SMB_STRDUP(f->name);
277 if (!*state->pp_long_name) {
278 return NT_STATUS_NO_MEMORY;
280 (void)strlower_m(*state->pp_long_name);
281 return NT_STATUS_OK;
284 static void get_real_name(struct cli_state *cli,
285 char **pp_long_name, fstring short_name)
287 struct rn_state state;
289 state.pp_long_name = pp_long_name;
290 state.short_name = short_name;
292 *pp_long_name = NULL;
293 /* nasty hack to force level 260 listings - tridge */
294 if (max_protocol <= PROTOCOL_LANMAN1) {
295 cli_list_trans(cli, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
296 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
297 &state);
298 } else {
299 cli_list_trans(cli, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
300 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
301 listfn, &state);
304 if (*short_name == 0) {
305 fstrcpy(short_name, *pp_long_name);
308 #if 0
309 if (!strchr_m(short_name,'.')) {
310 fstrcat(short_name,".");
312 #endif
315 static void testpair(struct cli_state *cli, const char *mask, const char *file)
317 uint16_t fnum;
318 fstring res1;
319 char *res2;
320 static int count;
321 fstring short_name;
322 char *long_name = NULL;
324 count++;
326 fstrcpy(res1, "---");
328 if (!NT_STATUS_IS_OK(cli_openx(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
329 DEBUG(0,("Can't create %s\n", file));
330 return;
332 cli_close(cli, fnum);
334 resultp = res1;
335 fstrcpy(short_name, "");
336 get_real_name(cli, &long_name, short_name);
337 if (!long_name) {
338 return;
340 fstrcpy(res1, "---");
341 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, listfn, NULL);
343 res2 = reg_test(cli, mask, long_name, short_name);
345 if (showall ||
346 ((strcmp(res1, res2) && !ignore_dot_errors) ||
347 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
348 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
349 res1, res2, count, mask, file, long_name, short_name));
350 if (die_on_error) exit(1);
353 cli_unlink(cli, file, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
355 if (count % 100 == 0) DEBUG(0,("%d\n", count));
356 SAFE_FREE(long_name);
359 static void test_mask(int argc, char *argv[],
360 struct cli_state *cli)
362 char *mask, *file;
363 int l1, l2, i, l;
364 int mc_len = strlen(maskchars);
365 int fc_len = strlen(filechars);
366 TALLOC_CTX *ctx = talloc_tos();
368 cli_mkdir(cli, "\\masktest");
370 cli_unlink(cli, "\\masktest\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
372 if (argc >= 2) {
373 while (argc >= 2) {
374 mask = talloc_asprintf(ctx,
375 "\\masktest\\%s",
376 argv[0]);
377 file = talloc_asprintf(ctx,
378 "\\masktest\\%s",
379 argv[1]);
380 if (!mask || !file) {
381 goto finished;
383 testpair(cli, mask, file);
384 argv += 2;
385 argc -= 2;
387 goto finished;
390 while (1) {
391 l1 = 1 + random() % 20;
392 l2 = 1 + random() % 20;
393 mask = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
394 file = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
395 if (!mask || !file) {
396 goto finished;
398 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
399 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
400 l = strlen(mask);
401 for (i=0;i<l1;i++) {
402 mask[i+l] = maskchars[random() % mc_len];
404 mask[l+l1] = 0;
406 for (i=0;i<l2;i++) {
407 file[i+l] = filechars[random() % fc_len];
409 file[l+l2] = 0;
411 if (strcmp(file+l,".") == 0 ||
412 strcmp(file+l,"..") == 0 ||
413 strcmp(mask+l,"..") == 0) continue;
415 if (strspn(file+l, ".") == strlen(file+l)) continue;
417 testpair(cli, mask, file);
418 if (NumLoops && (--NumLoops == 0))
419 break;
420 TALLOC_FREE(mask);
421 TALLOC_FREE(file);
424 finished:
425 cli_rmdir(cli, "\\masktest");
429 static void usage(void)
431 printf(
432 "Usage:\n\
433 masktest //server/share [options..]\n\
434 options:\n\
435 -d debuglevel\n\
436 -n numloops\n\
437 -W workgroup\n\
438 -U user%%pass\n\
439 -s seed\n\
440 -M max protocol\n\
441 -f filechars (default %s)\n\
442 -m maskchars (default %s)\n\
443 -v verbose mode\n\
444 -E die on error\n\
445 -a show all tests\n\
446 -i ignore . and .. errors\n\
448 This program tests wildcard matching between two servers. It generates\n\
449 random pairs of filenames/masks and tests that they match in the same\n\
450 way on the servers and internally\n\
452 filechars, maskchars);
455 /****************************************************************************
456 main program
457 ****************************************************************************/
458 int main(int argc,char *argv[])
460 char *share;
461 struct cli_state *cli;
462 int opt;
463 char *p;
464 int seed;
465 TALLOC_CTX *frame = talloc_stackframe();
467 setlinebuf(stdout);
469 lp_set_cmdline("log level", "0");
471 if (argc < 2 || argv[1][0] == '-') {
472 usage();
473 exit(1);
476 share = argv[1];
478 all_string_sub(share,"/","\\",0);
480 setup_logging(argv[0], DEBUG_STDERR);
482 argc -= 1;
483 argv += 1;
485 load_case_tables();
486 lp_load_global(get_dyn_CONFIGFILE());
487 load_interfaces();
489 if (getenv("USER")) {
490 fstrcpy(username,getenv("USER"));
493 seed = time(NULL);
495 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
496 switch (opt) {
497 case 'n':
498 NumLoops = atoi(optarg);
499 break;
500 case 'd':
501 lp_set_cmdline("log level", optarg);
502 break;
503 case 'E':
504 die_on_error = 1;
505 break;
506 case 'i':
507 ignore_dot_errors = 1;
508 break;
509 case 'v':
510 verbose++;
511 break;
512 case 'M':
513 max_protocol = interpret_protocol(optarg, max_protocol);
514 break;
515 case 'U':
516 fstrcpy(username,optarg);
517 p = strchr_m(username,'%');
518 if (p) {
519 *p = 0;
520 fstrcpy(password, p+1);
521 got_pass = 1;
523 break;
524 case 's':
525 seed = atoi(optarg);
526 break;
527 case 'h':
528 usage();
529 exit(1);
530 case 'm':
531 maskchars = optarg;
532 break;
533 case 'f':
534 filechars = optarg;
535 break;
536 case 'a':
537 showall = 1;
538 break;
539 case 'o':
540 old_list = True;
541 break;
542 default:
543 printf("Unknown option %c (%d)\n", (char)opt, opt);
544 exit(1);
548 argc -= optind;
549 argv += optind;
552 cli = connect_one(share);
553 if (!cli) {
554 DEBUG(0,("Failed to connect to %s\n", share));
555 exit(1);
558 /* need to init seed after connect as clientgen uses random numbers */
559 DEBUG(0,("seed=%d\n", seed));
560 srandom(seed);
562 test_mask(argc, argv, cli);
564 TALLOC_FREE(frame);
565 return(0);