examples: fix build on AIX6
[Samba/gebeck_regimport.git] / source3 / torture / masktest.c
blob58e09822a148fbf3e4bf594699b6201d121b2e06
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"
25 #include "../libcli/smb/smbXcli_base.h"
27 static fstring password;
28 static fstring username;
29 static int got_pass;
30 static int max_protocol = PROTOCOL_NT1;
31 static bool showall = False;
32 static bool old_list = False;
33 static const char *maskchars = "<>\"?*abc.";
34 static const char *filechars = "abcdefghijklm.";
35 static int verbose;
36 static int die_on_error;
37 static int NumLoops = 0;
38 static int ignore_dot_errors = 0;
40 extern char *optarg;
41 extern int optind;
43 /* a test fn for LANMAN mask support */
44 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
46 const char *p = pattern, *n = string;
47 char c;
49 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
51 while ((c = *p++)) {
52 switch (c) {
53 case '.':
54 /* if (! *n && ! *p) goto match; */
55 if (*n != '.') goto nomatch;
56 n++;
57 break;
59 case '?':
60 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
61 n++;
62 break;
64 case '>':
65 if (n[0] == '.') {
66 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
67 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
68 goto nomatch;
70 if (! *n) goto next;
71 n++;
72 break;
74 case '*':
75 if (! *p) goto match;
76 for (; *n; n++) {
77 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
79 break;
81 case '<':
82 for (; *n; n++) {
83 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
84 if (*n == '.' && !strchr_m(n+1,'.')) {
85 n++;
86 break;
89 break;
91 case '"':
92 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
93 if (*n != '.') goto nomatch;
94 n++;
95 break;
97 default:
98 if (c != *n) goto nomatch;
99 n++;
103 if (! *n) goto match;
105 nomatch:
106 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
107 return -1;
109 next:
110 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
111 goto nomatch;
113 match:
114 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
115 return 0;
118 static int ms_fnmatch_lanman(const char *pattern, const char *string)
120 if (!strpbrk(pattern, "?*<>\"")) {
121 if (strcmp(string,"..") == 0)
122 string = ".";
124 return strcmp(pattern, string);
127 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
128 return ms_fnmatch_lanman_core(pattern, "..") &&
129 ms_fnmatch_lanman_core(pattern, ".");
132 return ms_fnmatch_lanman_core(pattern, string);
135 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
137 /* oh what a weird world this is */
138 if (old_list && strcmp(pattern, "*.*") == 0) return True;
140 if (strcmp(pattern,".") == 0) return False;
142 if (max_protocol <= PROTOCOL_LANMAN2) {
143 return ms_fnmatch_lanman(pattern, file)==0;
146 if (strcmp(file,"..") == 0) file = ".";
148 return ms_fnmatch(pattern, file, smbXcli_conn_protocol(cli->conn), False) == 0;
151 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
153 static fstring ret;
154 const char *new_pattern = 1+strrchr_m(pattern,'\\');
156 fstrcpy(ret, "---");
157 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
158 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
159 if (reg_match_one(cli, new_pattern, long_name) ||
160 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
161 return ret;
165 /*****************************************************
166 return a connection to a server
167 *******************************************************/
168 static struct cli_state *connect_one(char *share)
170 struct cli_state *c;
171 char *server_n;
172 char *server;
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 status = cli_connect_nb(server, NULL, 0, 0x20, "masktest",
184 SMB_SIGNING_DEFAULT, 0, &c);
185 if (!NT_STATUS_IS_OK(status)) {
186 DEBUG(0,("Connection to %s failed. Error %s\n", server_n,
187 nt_errstr(status)));
188 return NULL;
191 status = smbXcli_negprot(c->conn, c->timeout, PROTOCOL_CORE,
192 max_protocol);
193 if (!NT_STATUS_IS_OK(status)) {
194 DEBUG(0, ("protocol negotiation failed: %s\n",
195 nt_errstr(status)));
196 cli_shutdown(c);
197 return NULL;
200 if (!got_pass) {
201 char *pass = getpass("Password: ");
202 if (pass) {
203 fstrcpy(password, pass);
207 status = cli_session_setup(c, username,
208 password, strlen(password),
209 password, strlen(password),
210 lp_workgroup());
211 if (!NT_STATUS_IS_OK(status)) {
212 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
213 return NULL;
217 * These next two lines are needed to emulate
218 * old client behaviour for people who have
219 * scripts based on client output.
220 * QUESTION ? Do we want to have a 'client compatibility
221 * mode to turn these on/off ? JRA.
224 if (*c->server_domain || *c->server_os || *c->server_type)
225 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
226 c->server_domain,c->server_os,c->server_type));
228 DEBUG(4,(" session setup ok\n"));
230 status = cli_tree_connect(c, share, "?????", password,
231 strlen(password)+1);
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
234 cli_shutdown(c);
235 return NULL;
238 DEBUG(4,(" tconx ok\n"));
240 return c;
243 static char *resultp;
245 struct rn_state {
246 char **pp_long_name;
247 char *short_name;
250 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
251 void *private_data)
253 struct rn_state *state = (struct rn_state *)private_data;
254 if (strcmp(f->name,".") == 0) {
255 resultp[0] = '+';
256 } else if (strcmp(f->name,"..") == 0) {
257 resultp[1] = '+';
258 } else {
259 resultp[2] = '+';
262 if (state == NULL) {
263 return NT_STATUS_INTERNAL_ERROR;
266 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
267 return NT_STATUS_OK;
271 fstrcpy(state->short_name, f->short_name ? f->short_name : "");
272 (void)strlower_m(state->short_name);
273 *state->pp_long_name = SMB_STRDUP(f->name);
274 if (!*state->pp_long_name) {
275 return NT_STATUS_NO_MEMORY;
277 (void)strlower_m(*state->pp_long_name);
278 return NT_STATUS_OK;
281 static void get_real_name(struct cli_state *cli,
282 char **pp_long_name, fstring short_name)
284 struct rn_state state;
286 state.pp_long_name = pp_long_name;
287 state.short_name = short_name;
289 *pp_long_name = NULL;
290 /* nasty hack to force level 260 listings - tridge */
291 if (max_protocol <= PROTOCOL_LANMAN1) {
292 cli_list_trans(cli, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
293 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
294 &state);
295 } else {
296 cli_list_trans(cli, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
297 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
298 listfn, &state);
301 if (*short_name == 0) {
302 fstrcpy(short_name, *pp_long_name);
305 #if 0
306 if (!strchr_m(short_name,'.')) {
307 fstrcat(short_name,".");
309 #endif
312 static void testpair(struct cli_state *cli, const char *mask, const char *file)
314 uint16_t fnum;
315 fstring res1;
316 char *res2;
317 static int count;
318 fstring short_name;
319 char *long_name = NULL;
321 count++;
323 fstrcpy(res1, "---");
325 if (!NT_STATUS_IS_OK(cli_openx(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
326 DEBUG(0,("Can't create %s\n", file));
327 return;
329 cli_close(cli, fnum);
331 resultp = res1;
332 fstrcpy(short_name, "");
333 get_real_name(cli, &long_name, short_name);
334 if (!long_name) {
335 return;
337 fstrcpy(res1, "---");
338 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 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, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
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\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
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 lp_set_cmdline("log level", "0");
468 if (argc < 2 || argv[1][0] == '-') {
469 usage();
470 exit(1);
473 share = argv[1];
475 all_string_sub(share,"/","\\",0);
477 setup_logging(argv[0], DEBUG_STDERR);
479 argc -= 1;
480 argv += 1;
482 load_case_tables();
483 lp_load_global(get_dyn_CONFIGFILE());
484 load_interfaces();
486 if (getenv("USER")) {
487 fstrcpy(username,getenv("USER"));
490 seed = time(NULL);
492 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
493 switch (opt) {
494 case 'n':
495 NumLoops = atoi(optarg);
496 break;
497 case 'd':
498 lp_set_cmdline("log level", optarg);
499 break;
500 case 'E':
501 die_on_error = 1;
502 break;
503 case 'i':
504 ignore_dot_errors = 1;
505 break;
506 case 'v':
507 verbose++;
508 break;
509 case 'M':
510 max_protocol = interpret_protocol(optarg, max_protocol);
511 break;
512 case 'U':
513 fstrcpy(username,optarg);
514 p = strchr_m(username,'%');
515 if (p) {
516 *p = 0;
517 fstrcpy(password, p+1);
518 got_pass = 1;
520 break;
521 case 's':
522 seed = atoi(optarg);
523 break;
524 case 'h':
525 usage();
526 exit(1);
527 case 'm':
528 maskchars = optarg;
529 break;
530 case 'f':
531 filechars = optarg;
532 break;
533 case 'a':
534 showall = 1;
535 break;
536 case 'o':
537 old_list = True;
538 break;
539 default:
540 printf("Unknown option %c (%d)\n", (char)opt, opt);
541 exit(1);
545 argc -= optind;
546 argv += optind;
549 cli = connect_one(share);
550 if (!cli) {
551 DEBUG(0,("Failed to connect to %s\n", share));
552 exit(1);
555 /* need to init seed after connect as clientgen uses random numbers */
556 DEBUG(0,("seed=%d\n", seed));
557 srandom(seed);
559 test_mask(argc, argv, cli);
561 TALLOC_FREE(frame);
562 return(0);