configure: we require autoconf 2.54
[Samba/bb.git] / source3 / torture / masktest.c
blobc5612fefaa93fcb71b65f98eec0a7e7d5d6357d4
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"
22 static fstring password;
23 static fstring username;
24 static int got_pass;
25 static int max_protocol = PROTOCOL_NT1;
26 static bool showall = False;
27 static bool old_list = False;
28 static const char *maskchars = "<>\"?*abc.";
29 static const char *filechars = "abcdefghijklm.";
30 static int verbose;
31 static int die_on_error;
32 static int NumLoops = 0;
33 static int ignore_dot_errors = 0;
35 extern char *optarg;
36 extern int optind;
37 extern bool AllowDebugChange;
39 /* a test fn for LANMAN mask support */
40 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
42 const char *p = pattern, *n = string;
43 char c;
45 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
47 while ((c = *p++)) {
48 switch (c) {
49 case '.':
50 /* if (! *n && ! *p) goto match; */
51 if (*n != '.') goto nomatch;
52 n++;
53 break;
55 case '?':
56 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
57 n++;
58 break;
60 case '>':
61 if (n[0] == '.') {
62 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
63 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
64 goto nomatch;
66 if (! *n) goto next;
67 n++;
68 break;
70 case '*':
71 if (! *p) goto match;
72 for (; *n; n++) {
73 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
75 break;
77 case '<':
78 for (; *n; n++) {
79 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
80 if (*n == '.' && !strchr_m(n+1,'.')) {
81 n++;
82 break;
85 break;
87 case '"':
88 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
89 if (*n != '.') goto nomatch;
90 n++;
91 break;
93 default:
94 if (c != *n) goto nomatch;
95 n++;
99 if (! *n) goto match;
101 nomatch:
102 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
103 return -1;
105 next:
106 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
107 goto nomatch;
109 match:
110 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
111 return 0;
114 static int ms_fnmatch_lanman(const char *pattern, const char *string)
116 if (!strpbrk(pattern, "?*<>\"")) {
117 if (strcmp(string,"..") == 0)
118 string = ".";
120 return strcmp(pattern, string);
123 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
124 return ms_fnmatch_lanman_core(pattern, "..") &&
125 ms_fnmatch_lanman_core(pattern, ".");
128 return ms_fnmatch_lanman_core(pattern, string);
131 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
133 /* oh what a weird world this is */
134 if (old_list && strcmp(pattern, "*.*") == 0) return True;
136 if (strcmp(pattern,".") == 0) return False;
138 if (max_protocol <= PROTOCOL_LANMAN2) {
139 return ms_fnmatch_lanman(pattern, file)==0;
142 if (strcmp(file,"..") == 0) file = ".";
144 return ms_fnmatch(pattern, file, cli->protocol, False) == 0;
147 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
149 static fstring ret;
150 const char *new_pattern = 1+strrchr_m(pattern,'\\');
152 fstrcpy(ret, "---");
153 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
154 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
155 if (reg_match_one(cli, new_pattern, long_name) ||
156 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
157 return ret;
161 /*****************************************************
162 return a connection to a server
163 *******************************************************/
164 static struct cli_state *connect_one(char *share)
166 struct cli_state *c;
167 struct nmb_name called, calling;
168 char *server_n;
169 char *server;
170 struct sockaddr_storage ss;
171 NTSTATUS status;
173 server = share+2;
174 share = strchr_m(server,'\\');
175 if (!share) return NULL;
176 *share = 0;
177 share++;
179 server_n = server;
181 zero_addr(&ss);
183 make_nmb_name(&calling, "masktest", 0x0);
184 make_nmb_name(&called , server, 0x20);
186 again:
187 zero_addr(&ss);
189 /* have to open a new connection */
190 if (!(c=cli_initialise())) {
191 DEBUG(0,("Connection to %s failed\n", server_n));
192 return NULL;
195 status = cli_connect(c, server_n, &ss);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
198 return NULL;
201 c->protocol = max_protocol;
203 if (!cli_session_request(c, &calling, &called)) {
204 DEBUG(0,("session request to %s failed\n", called.name));
205 cli_shutdown(c);
206 if (strcmp(called.name, "*SMBSERVER")) {
207 make_nmb_name(&called , "*SMBSERVER", 0x20);
208 goto again;
210 return NULL;
213 DEBUG(4,(" session request ok\n"));
215 if (!cli_negprot(c)) {
216 DEBUG(0,("protocol negotiation failed\n"));
217 cli_shutdown(c);
218 return NULL;
221 if (!got_pass) {
222 char *pass = getpass("Password: ");
223 if (pass) {
224 fstrcpy(password, pass);
228 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
229 password, strlen(password),
230 password, strlen(password),
231 lp_workgroup()))) {
232 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
233 return NULL;
237 * These next two lines are needed to emulate
238 * old client behaviour for people who have
239 * scripts based on client output.
240 * QUESTION ? Do we want to have a 'client compatibility
241 * mode to turn these on/off ? JRA.
244 if (*c->server_domain || *c->server_os || *c->server_type)
245 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
246 c->server_domain,c->server_os,c->server_type));
248 DEBUG(4,(" session setup ok\n"));
250 if (!cli_send_tconX(c, share, "?????",
251 password, strlen(password)+1)) {
252 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
253 cli_shutdown(c);
254 return NULL;
257 DEBUG(4,(" tconx ok\n"));
259 return c;
262 static char *resultp;
263 static file_info *f_info;
265 static void listfn(const char *mnt, file_info *f, const char *s, void *state)
267 if (strcmp(f->name,".") == 0) {
268 resultp[0] = '+';
269 } else if (strcmp(f->name,"..") == 0) {
270 resultp[1] = '+';
271 } else {
272 resultp[2] = '+';
274 f_info = f;
277 static void get_real_name(struct cli_state *cli,
278 char **pp_long_name, fstring short_name)
280 *pp_long_name = NULL;
281 /* nasty hack to force level 260 listings - tridge */
282 cli->capabilities |= CAP_NT_SMBS;
283 if (max_protocol <= PROTOCOL_LANMAN1) {
284 cli_list_new(cli, "\\masktest\\*.*", aHIDDEN | aDIR, listfn, NULL);
285 } else {
286 cli_list_new(cli, "\\masktest\\*", aHIDDEN | aDIR, listfn, NULL);
288 if (f_info) {
289 fstrcpy(short_name, f_info->short_name);
290 strlower_m(short_name);
291 *pp_long_name = SMB_STRDUP(f_info->name);
292 if (!*pp_long_name) {
293 return;
295 strlower_m(*pp_long_name);
298 if (*short_name == 0) {
299 fstrcpy(short_name, *pp_long_name);
302 #if 0
303 if (!strchr_m(short_name,'.')) {
304 fstrcat(short_name,".");
306 #endif
309 static void testpair(struct cli_state *cli, const char *mask, const char *file)
311 int fnum;
312 fstring res1;
313 char *res2;
314 static int count;
315 fstring short_name;
316 char *long_name = NULL;
318 count++;
320 fstrcpy(res1, "---");
322 fnum = cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0);
323 if (fnum == -1) {
324 DEBUG(0,("Can't create %s\n", file));
325 return;
327 cli_close(cli, fnum);
329 resultp = res1;
330 fstrcpy(short_name, "");
331 f_info = NULL;
332 get_real_name(cli, &long_name, short_name);
333 if (!long_name) {
334 return;
336 f_info = NULL;
337 fstrcpy(res1, "---");
338 cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
340 res2 = reg_test(cli, mask, long_name, short_name);
342 if (showall ||
343 ((strcmp(res1, res2) && !ignore_dot_errors) ||
344 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
345 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
346 res1, res2, count, mask, file, long_name, short_name));
347 if (die_on_error) exit(1);
350 cli_unlink(cli, file);
352 if (count % 100 == 0) DEBUG(0,("%d\n", count));
353 SAFE_FREE(long_name);
356 static void test_mask(int argc, char *argv[],
357 struct cli_state *cli)
359 char *mask, *file;
360 int l1, l2, i, l;
361 int mc_len = strlen(maskchars);
362 int fc_len = strlen(filechars);
363 TALLOC_CTX *ctx = talloc_tos();
365 cli_mkdir(cli, "\\masktest");
367 cli_unlink(cli, "\\masktest\\*");
369 if (argc >= 2) {
370 while (argc >= 2) {
371 mask = talloc_asprintf(ctx,
372 "\\masktest\\%s",
373 argv[0]);
374 file = talloc_asprintf(ctx,
375 "\\masktest\\%s",
376 argv[1]);
377 if (!mask || !file) {
378 goto finished;
380 testpair(cli, mask, file);
381 argv += 2;
382 argc -= 2;
384 goto finished;
387 while (1) {
388 l1 = 1 + random() % 20;
389 l2 = 1 + random() % 20;
390 mask = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
391 file = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
392 if (!mask || !file) {
393 goto finished;
395 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
396 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
397 l = strlen(mask);
398 for (i=0;i<l1;i++) {
399 mask[i+l] = maskchars[random() % mc_len];
401 mask[l+l1] = 0;
403 for (i=0;i<l2;i++) {
404 file[i+l] = filechars[random() % fc_len];
406 file[l+l2] = 0;
408 if (strcmp(file+l,".") == 0 ||
409 strcmp(file+l,"..") == 0 ||
410 strcmp(mask+l,"..") == 0) continue;
412 if (strspn(file+l, ".") == strlen(file+l)) continue;
414 testpair(cli, mask, file);
415 if (NumLoops && (--NumLoops == 0))
416 break;
417 TALLOC_FREE(mask);
418 TALLOC_FREE(file);
421 finished:
422 cli_rmdir(cli, "\\masktest");
426 static void usage(void)
428 printf(
429 "Usage:\n\
430 masktest //server/share [options..]\n\
431 options:\n\
432 -d debuglevel\n\
433 -n numloops\n\
434 -W workgroup\n\
435 -U user%%pass\n\
436 -s seed\n\
437 -M max protocol\n\
438 -f filechars (default %s)\n\
439 -m maskchars (default %s)\n\
440 -v verbose mode\n\
441 -E die on error\n\
442 -a show all tests\n\
443 -i ignore . and .. errors\n\
445 This program tests wildcard matching between two servers. It generates\n\
446 random pairs of filenames/masks and tests that they match in the same\n\
447 way on the servers and internally\n\
449 filechars, maskchars);
452 /****************************************************************************
453 main program
454 ****************************************************************************/
455 int main(int argc,char *argv[])
457 char *share;
458 struct cli_state *cli;
459 int opt;
460 char *p;
461 int seed;
462 TALLOC_CTX *frame = talloc_stackframe();
464 setlinebuf(stdout);
466 dbf = x_stderr;
468 DEBUGLEVEL = 0;
469 AllowDebugChange = False;
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],True);
482 argc -= 1;
483 argv += 1;
485 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
486 load_interfaces();
488 if (getenv("USER")) {
489 fstrcpy(username,getenv("USER"));
492 seed = time(NULL);
494 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
495 switch (opt) {
496 case 'n':
497 NumLoops = atoi(optarg);
498 break;
499 case 'd':
500 DEBUGLEVEL = atoi(optarg);
501 break;
502 case 'E':
503 die_on_error = 1;
504 break;
505 case 'i':
506 ignore_dot_errors = 1;
507 break;
508 case 'v':
509 verbose++;
510 break;
511 case 'M':
512 max_protocol = interpret_protocol(optarg, max_protocol);
513 break;
514 case 'U':
515 fstrcpy(username,optarg);
516 p = strchr_m(username,'%');
517 if (p) {
518 *p = 0;
519 fstrcpy(password, p+1);
520 got_pass = 1;
522 break;
523 case 's':
524 seed = atoi(optarg);
525 break;
526 case 'h':
527 usage();
528 exit(1);
529 case 'm':
530 maskchars = optarg;
531 break;
532 case 'f':
533 filechars = optarg;
534 break;
535 case 'a':
536 showall = 1;
537 break;
538 case 'o':
539 old_list = True;
540 break;
541 default:
542 printf("Unknown option %c (%d)\n", (char)opt, opt);
543 exit(1);
547 argc -= optind;
548 argv += optind;
551 cli = connect_one(share);
552 if (!cli) {
553 DEBUG(0,("Failed to connect to %s\n", share));
554 exit(1);
557 /* need to init seed after connect as clientgen uses random numbers */
558 DEBUG(0,("seed=%d\n", seed));
559 srandom(seed);
561 test_mask(argc, argv, cli);
563 TALLOC_FREE(frame);
564 return(0);