s3: remove dependency on automake for "make everything"
[Samba.git] / source3 / torture / masktest.c
blob37cb43e5409adee7b582d388cf9ea869c2531de5
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->protocol, 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 struct nmb_name called, calling;
171 char *server_n;
172 char *server;
173 struct sockaddr_storage ss;
174 NTSTATUS status;
176 server = share+2;
177 share = strchr_m(server,'\\');
178 if (!share) return NULL;
179 *share = 0;
180 share++;
182 server_n = server;
184 zero_sockaddr(&ss);
186 make_nmb_name(&calling, "masktest", 0x0);
187 make_nmb_name(&called , server, 0x20);
189 again:
190 zero_sockaddr(&ss);
192 /* have to open a new connection */
193 if (!(c=cli_initialise())) {
194 DEBUG(0,("Connection to %s failed\n", server_n));
195 return NULL;
198 status = cli_connect(c, server_n, &ss);
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
201 return NULL;
204 c->protocol = max_protocol;
206 if (!cli_session_request(c, &calling, &called)) {
207 DEBUG(0,("session request to %s failed\n", called.name));
208 cli_shutdown(c);
209 if (strcmp(called.name, "*SMBSERVER")) {
210 make_nmb_name(&called , "*SMBSERVER", 0x20);
211 goto again;
213 return NULL;
216 DEBUG(4,(" session request ok\n"));
218 status = cli_negprot(c);
219 if (!NT_STATUS_IS_OK(status)) {
220 DEBUG(0, ("protocol negotiation failed: %s\n",
221 nt_errstr(status)));
222 cli_shutdown(c);
223 return NULL;
226 if (!got_pass) {
227 char *pass = getpass("Password: ");
228 if (pass) {
229 fstrcpy(password, pass);
233 status = cli_session_setup(c, username,
234 password, strlen(password),
235 password, strlen(password),
236 lp_workgroup());
237 if (!NT_STATUS_IS_OK(status)) {
238 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
239 return NULL;
243 * These next two lines are needed to emulate
244 * old client behaviour for people who have
245 * scripts based on client output.
246 * QUESTION ? Do we want to have a 'client compatibility
247 * mode to turn these on/off ? JRA.
250 if (*c->server_domain || *c->server_os || *c->server_type)
251 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
252 c->server_domain,c->server_os,c->server_type));
254 DEBUG(4,(" session setup ok\n"));
256 status = cli_tcon_andx(c, share, "?????", password,
257 strlen(password)+1);
258 if (!NT_STATUS_IS_OK(status)) {
259 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
260 cli_shutdown(c);
261 return NULL;
264 DEBUG(4,(" tconx ok\n"));
266 return c;
269 static char *resultp;
271 struct rn_state {
272 char **pp_long_name;
273 char *short_name;
276 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
277 void *private_data)
279 struct rn_state *state = (struct rn_state *)private_data;
280 if (strcmp(f->name,".") == 0) {
281 resultp[0] = '+';
282 } else if (strcmp(f->name,"..") == 0) {
283 resultp[1] = '+';
284 } else {
285 resultp[2] = '+';
288 if (state == NULL) {
289 return NT_STATUS_INTERNAL_ERROR;
292 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
293 return NT_STATUS_OK;
296 fstrcpy(state->short_name, f->short_name);
297 strlower_m(state->short_name);
298 *state->pp_long_name = SMB_STRDUP(f->name);
299 if (!*state->pp_long_name) {
300 return NT_STATUS_NO_MEMORY;
302 strlower_m(*state->pp_long_name);
303 return NT_STATUS_OK;
306 static void get_real_name(struct cli_state *cli,
307 char **pp_long_name, fstring short_name)
309 struct rn_state state;
311 state.pp_long_name = pp_long_name;
312 state.short_name = short_name;
314 *pp_long_name = NULL;
315 /* nasty hack to force level 260 listings - tridge */
316 if (max_protocol <= PROTOCOL_LANMAN1) {
317 cli_list_trans(cli, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
318 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
319 &state);
320 } else {
321 cli_list_trans(cli, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
322 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
323 listfn, &state);
326 if (*short_name == 0) {
327 fstrcpy(short_name, *pp_long_name);
330 #if 0
331 if (!strchr_m(short_name,'.')) {
332 fstrcat(short_name,".");
334 #endif
337 static void testpair(struct cli_state *cli, const char *mask, const char *file)
339 uint16_t fnum;
340 fstring res1;
341 char *res2;
342 static int count;
343 fstring short_name;
344 char *long_name = NULL;
346 count++;
348 fstrcpy(res1, "---");
350 if (!NT_STATUS_IS_OK(cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
351 DEBUG(0,("Can't create %s\n", file));
352 return;
354 cli_close(cli, fnum);
356 resultp = res1;
357 fstrcpy(short_name, "");
358 get_real_name(cli, &long_name, short_name);
359 if (!long_name) {
360 return;
362 fstrcpy(res1, "---");
363 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, listfn, NULL);
365 res2 = reg_test(cli, mask, long_name, short_name);
367 if (showall ||
368 ((strcmp(res1, res2) && !ignore_dot_errors) ||
369 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
370 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
371 res1, res2, count, mask, file, long_name, short_name));
372 if (die_on_error) exit(1);
375 cli_unlink(cli, file, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
377 if (count % 100 == 0) DEBUG(0,("%d\n", count));
378 SAFE_FREE(long_name);
381 static void test_mask(int argc, char *argv[],
382 struct cli_state *cli)
384 char *mask, *file;
385 int l1, l2, i, l;
386 int mc_len = strlen(maskchars);
387 int fc_len = strlen(filechars);
388 TALLOC_CTX *ctx = talloc_tos();
390 cli_mkdir(cli, "\\masktest");
392 cli_unlink(cli, "\\masktest\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
394 if (argc >= 2) {
395 while (argc >= 2) {
396 mask = talloc_asprintf(ctx,
397 "\\masktest\\%s",
398 argv[0]);
399 file = talloc_asprintf(ctx,
400 "\\masktest\\%s",
401 argv[1]);
402 if (!mask || !file) {
403 goto finished;
405 testpair(cli, mask, file);
406 argv += 2;
407 argc -= 2;
409 goto finished;
412 while (1) {
413 l1 = 1 + random() % 20;
414 l2 = 1 + random() % 20;
415 mask = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
416 file = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
417 if (!mask || !file) {
418 goto finished;
420 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
421 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
422 l = strlen(mask);
423 for (i=0;i<l1;i++) {
424 mask[i+l] = maskchars[random() % mc_len];
426 mask[l+l1] = 0;
428 for (i=0;i<l2;i++) {
429 file[i+l] = filechars[random() % fc_len];
431 file[l+l2] = 0;
433 if (strcmp(file+l,".") == 0 ||
434 strcmp(file+l,"..") == 0 ||
435 strcmp(mask+l,"..") == 0) continue;
437 if (strspn(file+l, ".") == strlen(file+l)) continue;
439 testpair(cli, mask, file);
440 if (NumLoops && (--NumLoops == 0))
441 break;
442 TALLOC_FREE(mask);
443 TALLOC_FREE(file);
446 finished:
447 cli_rmdir(cli, "\\masktest");
451 static void usage(void)
453 printf(
454 "Usage:\n\
455 masktest //server/share [options..]\n\
456 options:\n\
457 -d debuglevel\n\
458 -n numloops\n\
459 -W workgroup\n\
460 -U user%%pass\n\
461 -s seed\n\
462 -M max protocol\n\
463 -f filechars (default %s)\n\
464 -m maskchars (default %s)\n\
465 -v verbose mode\n\
466 -E die on error\n\
467 -a show all tests\n\
468 -i ignore . and .. errors\n\
470 This program tests wildcard matching between two servers. It generates\n\
471 random pairs of filenames/masks and tests that they match in the same\n\
472 way on the servers and internally\n\
474 filechars, maskchars);
477 /****************************************************************************
478 main program
479 ****************************************************************************/
480 int main(int argc,char *argv[])
482 char *share;
483 struct cli_state *cli;
484 int opt;
485 char *p;
486 int seed;
487 TALLOC_CTX *frame = talloc_stackframe();
489 setlinebuf(stdout);
491 lp_set_cmdline("log level", "0");
493 if (argc < 2 || argv[1][0] == '-') {
494 usage();
495 exit(1);
498 share = argv[1];
500 all_string_sub(share,"/","\\",0);
502 setup_logging(argv[0], DEBUG_STDERR);
504 argc -= 1;
505 argv += 1;
507 load_case_tables();
508 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
509 load_interfaces();
511 if (getenv("USER")) {
512 fstrcpy(username,getenv("USER"));
515 seed = time(NULL);
517 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
518 switch (opt) {
519 case 'n':
520 NumLoops = atoi(optarg);
521 break;
522 case 'd':
523 lp_set_cmdline("log level", optarg);
524 break;
525 case 'E':
526 die_on_error = 1;
527 break;
528 case 'i':
529 ignore_dot_errors = 1;
530 break;
531 case 'v':
532 verbose++;
533 break;
534 case 'M':
535 max_protocol = interpret_protocol(optarg, max_protocol);
536 break;
537 case 'U':
538 fstrcpy(username,optarg);
539 p = strchr_m(username,'%');
540 if (p) {
541 *p = 0;
542 fstrcpy(password, p+1);
543 got_pass = 1;
545 break;
546 case 's':
547 seed = atoi(optarg);
548 break;
549 case 'h':
550 usage();
551 exit(1);
552 case 'm':
553 maskchars = optarg;
554 break;
555 case 'f':
556 filechars = optarg;
557 break;
558 case 'a':
559 showall = 1;
560 break;
561 case 'o':
562 old_list = True;
563 break;
564 default:
565 printf("Unknown option %c (%d)\n", (char)opt, opt);
566 exit(1);
570 argc -= optind;
571 argv += optind;
574 cli = connect_one(share);
575 if (!cli) {
576 DEBUG(0,("Failed to connect to %s\n", share));
577 exit(1);
580 /* need to init seed after connect as clientgen uses random numbers */
581 DEBUG(0,("seed=%d\n", seed));
582 srandom(seed);
584 test_mask(argc, argv, cli);
586 TALLOC_FREE(frame);
587 return(0);