r5870: syncing up for 3.0.12
[Samba.git] / source / torture / masktest.c
blobf3c87e0c72ce133b36c1a36d3d99bcdda5a3ce64
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define NO_SYSLOG
23 #include "includes.h"
25 static fstring password;
26 static fstring username;
27 static int got_pass;
28 static int max_protocol = PROTOCOL_NT1;
29 static BOOL showall = False;
30 static BOOL old_list = False;
31 static const char *maskchars = "<>\"?*abc.";
32 static const char *filechars = "abcdefghijklm.";
33 static int verbose;
34 static int die_on_error;
35 static int NumLoops = 0;
36 static int ignore_dot_errors = 0;
38 /* a test fn for LANMAN mask support */
39 int ms_fnmatch_lanman_core(const char *pattern, const char *string)
41 const char *p = pattern, *n = string;
42 char c;
44 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
46 while ((c = *p++)) {
47 switch (c) {
48 case '.':
49 /* if (! *n && ! *p) goto match; */
50 if (*n != '.') goto nomatch;
51 n++;
52 break;
54 case '?':
55 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
56 n++;
57 break;
59 case '>':
60 if (n[0] == '.') {
61 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
62 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
63 goto nomatch;
65 if (! *n) goto next;
66 n++;
67 break;
69 case '*':
70 if (! *p) goto match;
71 for (; *n; n++) {
72 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
74 break;
76 case '<':
77 for (; *n; n++) {
78 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
79 if (*n == '.' && !strchr_m(n+1,'.')) {
80 n++;
81 break;
84 break;
86 case '"':
87 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
88 if (*n != '.') goto nomatch;
89 n++;
90 break;
92 default:
93 if (c != *n) goto nomatch;
94 n++;
98 if (! *n) goto match;
100 nomatch:
101 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
102 return -1;
104 next:
105 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
106 goto nomatch;
108 match:
109 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
110 return 0;
113 int ms_fnmatch_lanman(const char *pattern, const char *string)
115 if (!strpbrk(pattern, "?*<>\"")) {
116 if (strcmp(string,"..") == 0)
117 string = ".";
119 return strcmp(pattern, string);
122 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
123 return ms_fnmatch_lanman_core(pattern, "..") &&
124 ms_fnmatch_lanman_core(pattern, ".");
127 return ms_fnmatch_lanman_core(pattern, string);
130 static BOOL reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
132 /* oh what a weird world this is */
133 if (old_list && strcmp(pattern, "*.*") == 0) return True;
135 if (strcmp(pattern,".") == 0) return False;
137 if (max_protocol <= PROTOCOL_LANMAN2) {
138 return ms_fnmatch_lanman(pattern, file)==0;
141 if (strcmp(file,"..") == 0) file = ".";
143 return ms_fnmatch(pattern, file, cli->protocol, False) == 0;
146 static char *reg_test(struct cli_state *cli, char *pattern, char *long_name, char *short_name)
148 static fstring ret;
149 fstrcpy(ret, "---");
151 pattern = 1+strrchr_m(pattern,'\\');
153 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
154 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
155 if (reg_match_one(cli, pattern, long_name) ||
156 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
157 return ret;
161 /*****************************************************
162 return a connection to a server
163 *******************************************************/
164 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 in_addr ip;
172 server = share+2;
173 share = strchr_m(server,'\\');
174 if (!share) return NULL;
175 *share = 0;
176 share++;
178 server_n = server;
180 zero_ip(&ip);
182 make_nmb_name(&calling, "masktest", 0x0);
183 make_nmb_name(&called , server, 0x20);
185 again:
186 zero_ip(&ip);
188 /* have to open a new connection */
189 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
190 DEBUG(0,("Connection to %s failed\n", server_n));
191 return NULL;
194 c->protocol = max_protocol;
196 if (!cli_session_request(c, &calling, &called)) {
197 DEBUG(0,("session request to %s failed\n", called.name));
198 cli_shutdown(c);
199 if (strcmp(called.name, "*SMBSERVER")) {
200 make_nmb_name(&called , "*SMBSERVER", 0x20);
201 goto again;
203 return NULL;
206 DEBUG(4,(" session request ok\n"));
208 if (!cli_negprot(c)) {
209 DEBUG(0,("protocol negotiation failed\n"));
210 cli_shutdown(c);
211 return NULL;
214 if (!got_pass) {
215 char *pass = getpass("Password: ");
216 if (pass) {
217 fstrcpy(password, pass);
221 if (!cli_session_setup(c, username,
222 password, strlen(password),
223 password, strlen(password),
224 lp_workgroup())) {
225 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
226 return NULL;
230 * These next two lines are needed to emulate
231 * old client behaviour for people who have
232 * scripts based on client output.
233 * QUESTION ? Do we want to have a 'client compatibility
234 * mode to turn these on/off ? JRA.
237 if (*c->server_domain || *c->server_os || *c->server_type)
238 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
239 c->server_domain,c->server_os,c->server_type));
241 DEBUG(4,(" session setup ok\n"));
243 if (!cli_send_tconX(c, share, "?????",
244 password, strlen(password)+1)) {
245 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
246 cli_shutdown(c);
247 return NULL;
250 DEBUG(4,(" tconx ok\n"));
252 return c;
255 static char *resultp;
256 static file_info *f_info;
258 static void listfn(const char *mnt, file_info *f, const char *s, void *state)
260 if (strcmp(f->name,".") == 0) {
261 resultp[0] = '+';
262 } else if (strcmp(f->name,"..") == 0) {
263 resultp[1] = '+';
264 } else {
265 resultp[2] = '+';
267 f_info = f;
270 static void get_real_name(struct cli_state *cli,
271 pstring long_name, fstring short_name)
273 /* nasty hack to force level 260 listings - tridge */
274 cli->capabilities |= CAP_NT_SMBS;
275 if (max_protocol <= PROTOCOL_LANMAN1) {
276 cli_list_new(cli, "\\masktest\\*.*", aHIDDEN | aDIR, listfn, NULL);
277 } else {
278 cli_list_new(cli, "\\masktest\\*", aHIDDEN | aDIR, listfn, NULL);
280 if (f_info) {
281 fstrcpy(short_name, f_info->short_name);
282 strlower_m(short_name);
283 pstrcpy(long_name, f_info->name);
284 strlower_m(long_name);
287 if (*short_name == 0) {
288 fstrcpy(short_name, long_name);
291 #if 0
292 if (!strchr_m(short_name,'.')) {
293 fstrcat(short_name,".");
295 #endif
298 static void testpair(struct cli_state *cli, char *mask, char *file)
300 int fnum;
301 fstring res1;
302 char *res2;
303 static int count;
304 fstring short_name;
305 pstring long_name;
307 count++;
309 fstrcpy(res1, "---");
311 fnum = cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0);
312 if (fnum == -1) {
313 DEBUG(0,("Can't create %s\n", file));
314 return;
316 cli_close(cli, fnum);
318 resultp = res1;
319 fstrcpy(short_name, "");
320 f_info = NULL;
321 get_real_name(cli, long_name, short_name);
322 f_info = NULL;
323 fstrcpy(res1, "---");
324 cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
326 res2 = reg_test(cli, mask, long_name, short_name);
328 if (showall ||
329 ((strcmp(res1, res2) && !ignore_dot_errors) ||
330 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
331 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
332 res1, res2, count, mask, file, long_name, short_name));
333 if (die_on_error) exit(1);
336 cli_unlink(cli, file);
338 if (count % 100 == 0) DEBUG(0,("%d\n", count));
341 static void test_mask(int argc, char *argv[],
342 struct cli_state *cli)
344 pstring mask, file;
345 int l1, l2, i, l;
346 int mc_len = strlen(maskchars);
347 int fc_len = strlen(filechars);
349 cli_mkdir(cli, "\\masktest");
351 cli_unlink(cli, "\\masktest\\*");
353 if (argc >= 2) {
354 while (argc >= 2) {
355 pstrcpy(mask,"\\masktest\\");
356 pstrcpy(file,"\\masktest\\");
357 pstrcat(mask, argv[0]);
358 pstrcat(file, argv[1]);
359 testpair(cli, mask, file);
360 argv += 2;
361 argc -= 2;
363 goto finished;
366 while (1) {
367 l1 = 1 + random() % 20;
368 l2 = 1 + random() % 20;
369 pstrcpy(mask,"\\masktest\\");
370 pstrcpy(file,"\\masktest\\");
371 l = strlen(mask);
372 for (i=0;i<l1;i++) {
373 mask[i+l] = maskchars[random() % mc_len];
375 mask[l+l1] = 0;
377 for (i=0;i<l2;i++) {
378 file[i+l] = filechars[random() % fc_len];
380 file[l+l2] = 0;
382 if (strcmp(file+l,".") == 0 ||
383 strcmp(file+l,"..") == 0 ||
384 strcmp(mask+l,"..") == 0) continue;
386 if (strspn(file+l, ".") == strlen(file+l)) continue;
388 testpair(cli, mask, file);
389 if (NumLoops && (--NumLoops == 0))
390 break;
393 finished:
394 cli_rmdir(cli, "\\masktest");
398 static void usage(void)
400 printf(
401 "Usage:\n\
402 masktest //server/share [options..]\n\
403 options:\n\
404 -d debuglevel\n\
405 -n numloops\n\
406 -W workgroup\n\
407 -U user%%pass\n\
408 -s seed\n\
409 -M max protocol\n\
410 -f filechars (default %s)\n\
411 -m maskchars (default %s)\n\
412 -v verbose mode\n\
413 -E die on error\n\
414 -a show all tests\n\
415 -i ignore . and .. errors\n\
417 This program tests wildcard matching between two servers. It generates\n\
418 random pairs of filenames/masks and tests that they match in the same\n\
419 way on the servers and internally\n\
421 filechars, maskchars);
424 /****************************************************************************
425 main program
426 ****************************************************************************/
427 int main(int argc,char *argv[])
429 char *share;
430 struct cli_state *cli;
431 extern char *optarg;
432 extern int optind;
433 extern BOOL AllowDebugChange;
434 int opt;
435 char *p;
436 int seed;
438 setlinebuf(stdout);
440 dbf = x_stderr;
442 DEBUGLEVEL = 0;
443 AllowDebugChange = False;
445 if (argc < 2 || argv[1][0] == '-') {
446 usage();
447 exit(1);
450 share = argv[1];
452 all_string_sub(share,"/","\\",0);
454 setup_logging(argv[0],True);
456 argc -= 1;
457 argv += 1;
459 lp_load(dyn_CONFIGFILE,True,False,False);
460 load_interfaces();
462 if (getenv("USER")) {
463 fstrcpy(username,getenv("USER"));
466 seed = time(NULL);
468 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
469 switch (opt) {
470 case 'n':
471 NumLoops = atoi(optarg);
472 break;
473 case 'd':
474 DEBUGLEVEL = atoi(optarg);
475 break;
476 case 'E':
477 die_on_error = 1;
478 break;
479 case 'i':
480 ignore_dot_errors = 1;
481 break;
482 case 'v':
483 verbose++;
484 break;
485 case 'M':
486 max_protocol = interpret_protocol(optarg, max_protocol);
487 break;
488 case 'U':
489 fstrcpy(username,optarg);
490 p = strchr_m(username,'%');
491 if (p) {
492 *p = 0;
493 fstrcpy(password, p+1);
494 got_pass = 1;
496 break;
497 case 's':
498 seed = atoi(optarg);
499 break;
500 case 'h':
501 usage();
502 exit(1);
503 case 'm':
504 maskchars = optarg;
505 break;
506 case 'f':
507 filechars = optarg;
508 break;
509 case 'a':
510 showall = 1;
511 break;
512 case 'o':
513 old_list = True;
514 break;
515 default:
516 printf("Unknown option %c (%d)\n", (char)opt, opt);
517 exit(1);
521 argc -= optind;
522 argv += optind;
525 cli = connect_one(share);
526 if (!cli) {
527 DEBUG(0,("Failed to connect to %s\n", share));
528 exit(1);
531 /* need to init seed after connect as clientgen uses random numbers */
532 DEBUG(0,("seed=%d\n", seed));
533 srandom(seed);
535 test_mask(argc, argv, cli);
537 return(0);