WHATSNEW: Start release notes for 3.6.0pre3.
[Samba.git] / source3 / torture / masktest.c
blobc8f346435af8312c9f44d9b0660178567b1a2ebb
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/nmblib.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 extern char *optarg;
39 extern int optind;
41 /* a test fn for LANMAN mask support */
42 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
44 const char *p = pattern, *n = string;
45 char c;
47 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
49 while ((c = *p++)) {
50 switch (c) {
51 case '.':
52 /* if (! *n && ! *p) goto match; */
53 if (*n != '.') goto nomatch;
54 n++;
55 break;
57 case '?':
58 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
59 n++;
60 break;
62 case '>':
63 if (n[0] == '.') {
64 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
65 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
66 goto nomatch;
68 if (! *n) goto next;
69 n++;
70 break;
72 case '*':
73 if (! *p) goto match;
74 for (; *n; n++) {
75 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
77 break;
79 case '<':
80 for (; *n; n++) {
81 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
82 if (*n == '.' && !strchr_m(n+1,'.')) {
83 n++;
84 break;
87 break;
89 case '"':
90 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
91 if (*n != '.') goto nomatch;
92 n++;
93 break;
95 default:
96 if (c != *n) goto nomatch;
97 n++;
101 if (! *n) goto match;
103 nomatch:
104 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
105 return -1;
107 next:
108 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
109 goto nomatch;
111 match:
112 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
113 return 0;
116 static int ms_fnmatch_lanman(const char *pattern, const char *string)
118 if (!strpbrk(pattern, "?*<>\"")) {
119 if (strcmp(string,"..") == 0)
120 string = ".";
122 return strcmp(pattern, string);
125 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
126 return ms_fnmatch_lanman_core(pattern, "..") &&
127 ms_fnmatch_lanman_core(pattern, ".");
130 return ms_fnmatch_lanman_core(pattern, string);
133 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
135 /* oh what a weird world this is */
136 if (old_list && strcmp(pattern, "*.*") == 0) return True;
138 if (strcmp(pattern,".") == 0) return False;
140 if (max_protocol <= PROTOCOL_LANMAN2) {
141 return ms_fnmatch_lanman(pattern, file)==0;
144 if (strcmp(file,"..") == 0) file = ".";
146 return ms_fnmatch(pattern, file, cli->protocol, False) == 0;
149 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
151 static fstring ret;
152 const char *new_pattern = 1+strrchr_m(pattern,'\\');
154 fstrcpy(ret, "---");
155 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
156 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
157 if (reg_match_one(cli, new_pattern, long_name) ||
158 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
159 return ret;
163 /*****************************************************
164 return a connection to a server
165 *******************************************************/
166 static struct cli_state *connect_one(char *share)
168 struct cli_state *c;
169 struct nmb_name called, calling;
170 char *server_n;
171 char *server;
172 struct sockaddr_storage ss;
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 zero_sockaddr(&ss);
185 make_nmb_name(&calling, "masktest", 0x0);
186 make_nmb_name(&called , server, 0x20);
188 again:
189 zero_sockaddr(&ss);
191 /* have to open a new connection */
192 if (!(c=cli_initialise())) {
193 DEBUG(0,("Connection to %s failed\n", server_n));
194 return NULL;
197 status = cli_connect(c, server_n, &ss);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
200 return NULL;
203 c->protocol = max_protocol;
205 if (!cli_session_request(c, &calling, &called)) {
206 DEBUG(0,("session request to %s failed\n", called.name));
207 cli_shutdown(c);
208 if (strcmp(called.name, "*SMBSERVER")) {
209 make_nmb_name(&called , "*SMBSERVER", 0x20);
210 goto again;
212 return NULL;
215 DEBUG(4,(" session request ok\n"));
217 status = cli_negprot(c);
218 if (!NT_STATUS_IS_OK(status)) {
219 DEBUG(0, ("protocol negotiation failed: %s\n",
220 nt_errstr(status)));
221 cli_shutdown(c);
222 return NULL;
225 if (!got_pass) {
226 char *pass = getpass("Password: ");
227 if (pass) {
228 fstrcpy(password, pass);
232 status = cli_session_setup(c, username,
233 password, strlen(password),
234 password, strlen(password),
235 lp_workgroup());
236 if (!NT_STATUS_IS_OK(status)) {
237 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
238 return NULL;
242 * These next two lines are needed to emulate
243 * old client behaviour for people who have
244 * scripts based on client output.
245 * QUESTION ? Do we want to have a 'client compatibility
246 * mode to turn these on/off ? JRA.
249 if (*c->server_domain || *c->server_os || *c->server_type)
250 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
251 c->server_domain,c->server_os,c->server_type));
253 DEBUG(4,(" session setup ok\n"));
255 status = cli_tcon_andx(c, share, "?????", password,
256 strlen(password)+1);
257 if (!NT_STATUS_IS_OK(status)) {
258 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
259 cli_shutdown(c);
260 return NULL;
263 DEBUG(4,(" tconx ok\n"));
265 return c;
268 static char *resultp;
270 struct rn_state {
271 char **pp_long_name;
272 char *short_name;
275 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
276 void *private_data)
278 struct rn_state *state = (struct rn_state *)private_data;
279 if (strcmp(f->name,".") == 0) {
280 resultp[0] = '+';
281 } else if (strcmp(f->name,"..") == 0) {
282 resultp[1] = '+';
283 } else {
284 resultp[2] = '+';
287 if (state == NULL) {
288 return NT_STATUS_INTERNAL_ERROR;
291 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
292 return NT_STATUS_OK;
295 fstrcpy(state->short_name, f->short_name);
296 strlower_m(state->short_name);
297 *state->pp_long_name = SMB_STRDUP(f->name);
298 if (!*state->pp_long_name) {
299 return NT_STATUS_NO_MEMORY;
301 strlower_m(*state->pp_long_name);
302 return NT_STATUS_OK;
305 static void get_real_name(struct cli_state *cli,
306 char **pp_long_name, fstring short_name)
308 struct rn_state state;
310 state.pp_long_name = pp_long_name;
311 state.short_name = short_name;
313 *pp_long_name = NULL;
314 /* nasty hack to force level 260 listings - tridge */
315 if (max_protocol <= PROTOCOL_LANMAN1) {
316 cli_list_trans(cli, "\\masktest\\*.*", aHIDDEN | aDIR,
317 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
318 &state);
319 } else {
320 cli_list_trans(cli, "\\masktest\\*", aHIDDEN | aDIR,
321 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
322 listfn, &state);
325 if (*short_name == 0) {
326 fstrcpy(short_name, *pp_long_name);
329 #if 0
330 if (!strchr_m(short_name,'.')) {
331 fstrcat(short_name,".");
333 #endif
336 static void testpair(struct cli_state *cli, const char *mask, const char *file)
338 uint16_t fnum;
339 fstring res1;
340 char *res2;
341 static int count;
342 fstring short_name;
343 char *long_name = NULL;
345 count++;
347 fstrcpy(res1, "---");
349 if (!NT_STATUS_IS_OK(cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
350 DEBUG(0,("Can't create %s\n", file));
351 return;
353 cli_close(cli, fnum);
355 resultp = res1;
356 fstrcpy(short_name, "");
357 get_real_name(cli, &long_name, short_name);
358 if (!long_name) {
359 return;
361 fstrcpy(res1, "---");
362 cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
364 res2 = reg_test(cli, mask, long_name, short_name);
366 if (showall ||
367 ((strcmp(res1, res2) && !ignore_dot_errors) ||
368 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
369 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
370 res1, res2, count, mask, file, long_name, short_name));
371 if (die_on_error) exit(1);
374 cli_unlink(cli, file, aSYSTEM | aHIDDEN);
376 if (count % 100 == 0) DEBUG(0,("%d\n", count));
377 SAFE_FREE(long_name);
380 static void test_mask(int argc, char *argv[],
381 struct cli_state *cli)
383 char *mask, *file;
384 int l1, l2, i, l;
385 int mc_len = strlen(maskchars);
386 int fc_len = strlen(filechars);
387 TALLOC_CTX *ctx = talloc_tos();
389 cli_mkdir(cli, "\\masktest");
391 cli_unlink(cli, "\\masktest\\*", aSYSTEM | aHIDDEN);
393 if (argc >= 2) {
394 while (argc >= 2) {
395 mask = talloc_asprintf(ctx,
396 "\\masktest\\%s",
397 argv[0]);
398 file = talloc_asprintf(ctx,
399 "\\masktest\\%s",
400 argv[1]);
401 if (!mask || !file) {
402 goto finished;
404 testpair(cli, mask, file);
405 argv += 2;
406 argc -= 2;
408 goto finished;
411 while (1) {
412 l1 = 1 + random() % 20;
413 l2 = 1 + random() % 20;
414 mask = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
415 file = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
416 if (!mask || !file) {
417 goto finished;
419 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
420 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
421 l = strlen(mask);
422 for (i=0;i<l1;i++) {
423 mask[i+l] = maskchars[random() % mc_len];
425 mask[l+l1] = 0;
427 for (i=0;i<l2;i++) {
428 file[i+l] = filechars[random() % fc_len];
430 file[l+l2] = 0;
432 if (strcmp(file+l,".") == 0 ||
433 strcmp(file+l,"..") == 0 ||
434 strcmp(mask+l,"..") == 0) continue;
436 if (strspn(file+l, ".") == strlen(file+l)) continue;
438 testpair(cli, mask, file);
439 if (NumLoops && (--NumLoops == 0))
440 break;
441 TALLOC_FREE(mask);
442 TALLOC_FREE(file);
445 finished:
446 cli_rmdir(cli, "\\masktest");
450 static void usage(void)
452 printf(
453 "Usage:\n\
454 masktest //server/share [options..]\n\
455 options:\n\
456 -d debuglevel\n\
457 -n numloops\n\
458 -W workgroup\n\
459 -U user%%pass\n\
460 -s seed\n\
461 -M max protocol\n\
462 -f filechars (default %s)\n\
463 -m maskchars (default %s)\n\
464 -v verbose mode\n\
465 -E die on error\n\
466 -a show all tests\n\
467 -i ignore . and .. errors\n\
469 This program tests wildcard matching between two servers. It generates\n\
470 random pairs of filenames/masks and tests that they match in the same\n\
471 way on the servers and internally\n\
473 filechars, maskchars);
476 /****************************************************************************
477 main program
478 ****************************************************************************/
479 int main(int argc,char *argv[])
481 char *share;
482 struct cli_state *cli;
483 int opt;
484 char *p;
485 int seed;
486 TALLOC_CTX *frame = talloc_stackframe();
488 setlinebuf(stdout);
490 lp_set_cmdline("log level", "0");
492 if (argc < 2 || argv[1][0] == '-') {
493 usage();
494 exit(1);
497 share = argv[1];
499 all_string_sub(share,"/","\\",0);
501 setup_logging(argv[0], DEBUG_STDERR);
503 argc -= 1;
504 argv += 1;
506 load_case_tables();
507 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
508 load_interfaces();
510 if (getenv("USER")) {
511 fstrcpy(username,getenv("USER"));
514 seed = time(NULL);
516 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
517 switch (opt) {
518 case 'n':
519 NumLoops = atoi(optarg);
520 break;
521 case 'd':
522 lp_set_cmdline("log level", optarg);
523 break;
524 case 'E':
525 die_on_error = 1;
526 break;
527 case 'i':
528 ignore_dot_errors = 1;
529 break;
530 case 'v':
531 verbose++;
532 break;
533 case 'M':
534 max_protocol = interpret_protocol(optarg, max_protocol);
535 break;
536 case 'U':
537 fstrcpy(username,optarg);
538 p = strchr_m(username,'%');
539 if (p) {
540 *p = 0;
541 fstrcpy(password, p+1);
542 got_pass = 1;
544 break;
545 case 's':
546 seed = atoi(optarg);
547 break;
548 case 'h':
549 usage();
550 exit(1);
551 case 'm':
552 maskchars = optarg;
553 break;
554 case 'f':
555 filechars = optarg;
556 break;
557 case 'a':
558 showall = 1;
559 break;
560 case 'o':
561 old_list = True;
562 break;
563 default:
564 printf("Unknown option %c (%d)\n", (char)opt, opt);
565 exit(1);
569 argc -= optind;
570 argv += optind;
573 cli = connect_one(share);
574 if (!cli) {
575 DEBUG(0,("Failed to connect to %s\n", share));
576 exit(1);
579 /* need to init seed after connect as clientgen uses random numbers */
580 DEBUG(0,("seed=%d\n", seed));
581 srandom(seed);
583 test_mask(argc, argv, cli);
585 TALLOC_FREE(frame);
586 return(0);