s3:msdfs: removed unused variables
[Samba/vl.git] / source3 / torture / masktest.c
blobeaac0d8edccc4e688a89597665d64468c4b11dba
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"
26 static fstring password;
27 static fstring username;
28 static int got_pass;
29 static int max_protocol = PROTOCOL_NT1;
30 static bool showall = False;
31 static bool old_list = False;
32 static const char *maskchars = "<>\"?*abc.";
33 static const char *filechars = "abcdefghijklm.";
34 static int verbose;
35 static int die_on_error;
36 static int NumLoops = 0;
37 static int ignore_dot_errors = 0;
39 extern char *optarg;
40 extern int optind;
42 /* a test fn for LANMAN mask support */
43 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
45 const char *p = pattern, *n = string;
46 char c;
48 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
50 while ((c = *p++)) {
51 switch (c) {
52 case '.':
53 /* if (! *n && ! *p) goto match; */
54 if (*n != '.') goto nomatch;
55 n++;
56 break;
58 case '?':
59 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
60 n++;
61 break;
63 case '>':
64 if (n[0] == '.') {
65 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
66 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
67 goto nomatch;
69 if (! *n) goto next;
70 n++;
71 break;
73 case '*':
74 if (! *p) goto match;
75 for (; *n; n++) {
76 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
78 break;
80 case '<':
81 for (; *n; n++) {
82 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
83 if (*n == '.' && !strchr_m(n+1,'.')) {
84 n++;
85 break;
88 break;
90 case '"':
91 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
92 if (*n != '.') goto nomatch;
93 n++;
94 break;
96 default:
97 if (c != *n) goto nomatch;
98 n++;
102 if (! *n) goto match;
104 nomatch:
105 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
106 return -1;
108 next:
109 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
110 goto nomatch;
112 match:
113 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
114 return 0;
117 static int ms_fnmatch_lanman(const char *pattern, const char *string)
119 if (!strpbrk(pattern, "?*<>\"")) {
120 if (strcmp(string,"..") == 0)
121 string = ".";
123 return strcmp(pattern, string);
126 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
127 return ms_fnmatch_lanman_core(pattern, "..") &&
128 ms_fnmatch_lanman_core(pattern, ".");
131 return ms_fnmatch_lanman_core(pattern, string);
134 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
136 /* oh what a weird world this is */
137 if (old_list && strcmp(pattern, "*.*") == 0) return True;
139 if (strcmp(pattern,".") == 0) return False;
141 if (max_protocol <= PROTOCOL_LANMAN2) {
142 return ms_fnmatch_lanman(pattern, file)==0;
145 if (strcmp(file,"..") == 0) file = ".";
147 return ms_fnmatch(pattern, file, cli_state_protocol(cli), False) == 0;
150 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
152 static fstring ret;
153 const char *new_pattern = 1+strrchr_m(pattern,'\\');
155 fstrcpy(ret, "---");
156 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
157 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
158 if (reg_match_one(cli, new_pattern, long_name) ||
159 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
160 return ret;
164 /*****************************************************
165 return a connection to a server
166 *******************************************************/
167 static struct cli_state *connect_one(char *share)
169 struct cli_state *c;
170 char *server_n;
171 char *server;
172 NTSTATUS status;
174 server = share+2;
175 share = strchr_m(server,'\\');
176 if (!share) return NULL;
177 *share = 0;
178 share++;
180 server_n = server;
182 status = cli_connect_nb(server, NULL, 0, 0x20, "masktest", Undefined, 0,
183 &c);
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(0,("Connection to %s failed. Error %s\n", server_n,
186 nt_errstr(status)));
187 return NULL;
190 status = cli_negprot(c, max_protocol);
191 if (!NT_STATUS_IS_OK(status)) {
192 DEBUG(0, ("protocol negotiation failed: %s\n",
193 nt_errstr(status)));
194 cli_shutdown(c);
195 return NULL;
198 if (!got_pass) {
199 char *pass = getpass("Password: ");
200 if (pass) {
201 fstrcpy(password, pass);
205 status = cli_session_setup(c, username,
206 password, strlen(password),
207 password, strlen(password),
208 lp_workgroup());
209 if (!NT_STATUS_IS_OK(status)) {
210 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
211 return NULL;
215 * These next two lines are needed to emulate
216 * old client behaviour for people who have
217 * scripts based on client output.
218 * QUESTION ? Do we want to have a 'client compatibility
219 * mode to turn these on/off ? JRA.
222 if (*c->server_domain || *c->server_os || *c->server_type)
223 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
224 c->server_domain,c->server_os,c->server_type));
226 DEBUG(4,(" session setup ok\n"));
228 status = cli_tcon_andx(c, share, "?????", password,
229 strlen(password)+1);
230 if (!NT_STATUS_IS_OK(status)) {
231 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
232 cli_shutdown(c);
233 return NULL;
236 DEBUG(4,(" tconx ok\n"));
238 return c;
241 static char *resultp;
243 struct rn_state {
244 char **pp_long_name;
245 char *short_name;
248 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
249 void *private_data)
251 struct rn_state *state = (struct rn_state *)private_data;
252 if (strcmp(f->name,".") == 0) {
253 resultp[0] = '+';
254 } else if (strcmp(f->name,"..") == 0) {
255 resultp[1] = '+';
256 } else {
257 resultp[2] = '+';
260 if (state == NULL) {
261 return NT_STATUS_INTERNAL_ERROR;
264 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
265 return NT_STATUS_OK;
269 fstrcpy(state->short_name, f->short_name ? f->short_name : "");
270 strlower_m(state->short_name);
271 *state->pp_long_name = SMB_STRDUP(f->name);
272 if (!*state->pp_long_name) {
273 return NT_STATUS_NO_MEMORY;
275 strlower_m(*state->pp_long_name);
276 return NT_STATUS_OK;
279 static void get_real_name(struct cli_state *cli,
280 char **pp_long_name, fstring short_name)
282 struct rn_state state;
284 state.pp_long_name = pp_long_name;
285 state.short_name = short_name;
287 *pp_long_name = NULL;
288 /* nasty hack to force level 260 listings - tridge */
289 if (max_protocol <= PROTOCOL_LANMAN1) {
290 cli_list_trans(cli, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
291 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
292 &state);
293 } else {
294 cli_list_trans(cli, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
295 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
296 listfn, &state);
299 if (*short_name == 0) {
300 fstrcpy(short_name, *pp_long_name);
303 #if 0
304 if (!strchr_m(short_name,'.')) {
305 fstrcat(short_name,".");
307 #endif
310 static void testpair(struct cli_state *cli, const char *mask, const char *file)
312 uint16_t fnum;
313 fstring res1;
314 char *res2;
315 static int count;
316 fstring short_name;
317 char *long_name = NULL;
319 count++;
321 fstrcpy(res1, "---");
323 if (!NT_STATUS_IS_OK(cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
324 DEBUG(0,("Can't create %s\n", file));
325 return;
327 cli_close(cli, fnum);
329 resultp = res1;
330 fstrcpy(short_name, "");
331 get_real_name(cli, &long_name, short_name);
332 if (!long_name) {
333 return;
335 fstrcpy(res1, "---");
336 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, listfn, NULL);
338 res2 = reg_test(cli, mask, long_name, short_name);
340 if (showall ||
341 ((strcmp(res1, res2) && !ignore_dot_errors) ||
342 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
343 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
344 res1, res2, count, mask, file, long_name, short_name));
345 if (die_on_error) exit(1);
348 cli_unlink(cli, file, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
350 if (count % 100 == 0) DEBUG(0,("%d\n", count));
351 SAFE_FREE(long_name);
354 static void test_mask(int argc, char *argv[],
355 struct cli_state *cli)
357 char *mask, *file;
358 int l1, l2, i, l;
359 int mc_len = strlen(maskchars);
360 int fc_len = strlen(filechars);
361 TALLOC_CTX *ctx = talloc_tos();
363 cli_mkdir(cli, "\\masktest");
365 cli_unlink(cli, "\\masktest\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
367 if (argc >= 2) {
368 while (argc >= 2) {
369 mask = talloc_asprintf(ctx,
370 "\\masktest\\%s",
371 argv[0]);
372 file = talloc_asprintf(ctx,
373 "\\masktest\\%s",
374 argv[1]);
375 if (!mask || !file) {
376 goto finished;
378 testpair(cli, mask, file);
379 argv += 2;
380 argc -= 2;
382 goto finished;
385 while (1) {
386 l1 = 1 + random() % 20;
387 l2 = 1 + random() % 20;
388 mask = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
389 file = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
390 if (!mask || !file) {
391 goto finished;
393 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
394 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
395 l = strlen(mask);
396 for (i=0;i<l1;i++) {
397 mask[i+l] = maskchars[random() % mc_len];
399 mask[l+l1] = 0;
401 for (i=0;i<l2;i++) {
402 file[i+l] = filechars[random() % fc_len];
404 file[l+l2] = 0;
406 if (strcmp(file+l,".") == 0 ||
407 strcmp(file+l,"..") == 0 ||
408 strcmp(mask+l,"..") == 0) continue;
410 if (strspn(file+l, ".") == strlen(file+l)) continue;
412 testpair(cli, mask, file);
413 if (NumLoops && (--NumLoops == 0))
414 break;
415 TALLOC_FREE(mask);
416 TALLOC_FREE(file);
419 finished:
420 cli_rmdir(cli, "\\masktest");
424 static void usage(void)
426 printf(
427 "Usage:\n\
428 masktest //server/share [options..]\n\
429 options:\n\
430 -d debuglevel\n\
431 -n numloops\n\
432 -W workgroup\n\
433 -U user%%pass\n\
434 -s seed\n\
435 -M max protocol\n\
436 -f filechars (default %s)\n\
437 -m maskchars (default %s)\n\
438 -v verbose mode\n\
439 -E die on error\n\
440 -a show all tests\n\
441 -i ignore . and .. errors\n\
443 This program tests wildcard matching between two servers. It generates\n\
444 random pairs of filenames/masks and tests that they match in the same\n\
445 way on the servers and internally\n\
447 filechars, maskchars);
450 /****************************************************************************
451 main program
452 ****************************************************************************/
453 int main(int argc,char *argv[])
455 char *share;
456 struct cli_state *cli;
457 int opt;
458 char *p;
459 int seed;
460 TALLOC_CTX *frame = talloc_stackframe();
462 setlinebuf(stdout);
464 lp_set_cmdline("log level", "0");
466 if (argc < 2 || argv[1][0] == '-') {
467 usage();
468 exit(1);
471 share = argv[1];
473 all_string_sub(share,"/","\\",0);
475 setup_logging(argv[0], DEBUG_STDERR);
477 argc -= 1;
478 argv += 1;
480 load_case_tables();
481 lp_load_global(get_dyn_CONFIGFILE());
482 load_interfaces();
484 if (getenv("USER")) {
485 fstrcpy(username,getenv("USER"));
488 seed = time(NULL);
490 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
491 switch (opt) {
492 case 'n':
493 NumLoops = atoi(optarg);
494 break;
495 case 'd':
496 lp_set_cmdline("log level", optarg);
497 break;
498 case 'E':
499 die_on_error = 1;
500 break;
501 case 'i':
502 ignore_dot_errors = 1;
503 break;
504 case 'v':
505 verbose++;
506 break;
507 case 'M':
508 max_protocol = interpret_protocol(optarg, max_protocol);
509 break;
510 case 'U':
511 fstrcpy(username,optarg);
512 p = strchr_m(username,'%');
513 if (p) {
514 *p = 0;
515 fstrcpy(password, p+1);
516 got_pass = 1;
518 break;
519 case 's':
520 seed = atoi(optarg);
521 break;
522 case 'h':
523 usage();
524 exit(1);
525 case 'm':
526 maskchars = optarg;
527 break;
528 case 'f':
529 filechars = optarg;
530 break;
531 case 'a':
532 showall = 1;
533 break;
534 case 'o':
535 old_list = True;
536 break;
537 default:
538 printf("Unknown option %c (%d)\n", (char)opt, opt);
539 exit(1);
543 argc -= optind;
544 argv += optind;
547 cli = connect_one(share);
548 if (!cli) {
549 DEBUG(0,("Failed to connect to %s\n", share));
550 exit(1);
553 /* need to init seed after connect as clientgen uses random numbers */
554 DEBUG(0,("seed=%d\n", seed));
555 srandom(seed);
557 test_mask(argc, argv, cli);
559 TALLOC_FREE(frame);
560 return(0);