s4:kdc: split out a sdb_keys_free() helper function
[Samba.git] / source3 / torture / masktest.c
blobd7485cfb5eb801bf5dbe1e9899a3db91747e2708
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"
26 #include "lib/util/string_wrappers.h"
28 static fstring password;
29 static fstring username;
30 static int got_pass;
31 static struct cli_credentials *test_creds;
32 static int max_protocol = -1;
33 static bool showall = False;
34 static bool old_list = False;
35 static const char *maskchars = "<>\"?*abc.";
36 static const char *filechars = "abcdefghijklm.";
37 static int verbose;
38 static int die_on_error;
39 static int NumLoops = 0;
40 static int ignore_dot_errors = 0;
42 extern char *optarg;
43 extern int optind;
45 /* a test fn for LANMAN mask support */
46 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
48 const char *p = pattern, *n = string;
49 char c;
51 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
53 while ((c = *p++)) {
54 switch (c) {
55 case '.':
56 /* if (! *n && ! *p) goto match; */
57 if (*n != '.') goto nomatch;
58 n++;
59 break;
61 case '?':
62 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
63 n++;
64 break;
66 case '>':
67 if (n[0] == '.') {
68 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
69 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
70 goto nomatch;
72 if (! *n) goto next;
73 n++;
74 break;
76 case '*':
77 if (! *p) goto match;
78 for (; *n; n++) {
79 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
81 break;
83 case '<':
84 for (; *n; n++) {
85 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
86 if (*n == '.' && !strchr_m(n+1,'.')) {
87 n++;
88 break;
91 break;
93 case '"':
94 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
95 if (*n != '.') goto nomatch;
96 n++;
97 break;
99 default:
100 if (c != *n) goto nomatch;
101 n++;
105 if (! *n) goto match;
107 nomatch:
108 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
109 return -1;
111 next:
112 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
113 goto nomatch;
115 match:
116 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
117 return 0;
120 static int ms_fnmatch_lanman(const char *pattern, const char *string)
122 if (!strpbrk(pattern, "?*<>\"")) {
123 if (strcmp(string,"..") == 0)
124 string = ".";
126 return strcmp(pattern, string);
129 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
130 return ms_fnmatch_lanman_core(pattern, "..") &&
131 ms_fnmatch_lanman_core(pattern, ".");
134 return ms_fnmatch_lanman_core(pattern, string);
137 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
139 /* oh what a weird world this is */
140 if (old_list && strcmp(pattern, "*.*") == 0) return True;
142 if (strcmp(pattern,".") == 0) return False;
144 if (max_protocol <= PROTOCOL_LANMAN2) {
145 return ms_fnmatch_lanman(pattern, file)==0;
148 if (strcmp(file,"..") == 0) file = ".";
150 return ms_fnmatch(pattern, file, smbXcli_conn_protocol(cli->conn), False) == 0;
153 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
155 static fstring ret;
156 const char *new_pattern = 1+strrchr_m(pattern,'\\');
158 fstrcpy(ret, "---");
159 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
160 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
161 if (reg_match_one(cli, new_pattern, long_name) ||
162 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
163 return ret;
167 /*****************************************************
168 return a connection to a server
169 *******************************************************/
170 static struct cli_state *connect_one(char *share)
172 struct cli_state *c;
173 char *server_n;
174 char *server;
175 NTSTATUS status;
177 server = share+2;
178 share = strchr_m(server,'\\');
179 if (!share) return NULL;
180 *share = 0;
181 share++;
183 server_n = server;
185 status = cli_connect_nb(server, NULL, 0, 0x20, "masktest",
186 SMB_SIGNING_DEFAULT, 0, &c);
187 if (!NT_STATUS_IS_OK(status)) {
188 DEBUG(0,("Connection to %s failed. Error %s\n", server_n,
189 nt_errstr(status)));
190 return NULL;
193 status = smbXcli_negprot(c->conn, c->timeout, PROTOCOL_CORE,
194 max_protocol);
195 if (!NT_STATUS_IS_OK(status)) {
196 DEBUG(0, ("protocol negotiation failed: %s\n",
197 nt_errstr(status)));
198 cli_shutdown(c);
199 return NULL;
202 status = cli_session_setup_creds(c, test_creds);
203 if (!NT_STATUS_IS_OK(status)) {
204 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
205 return NULL;
209 * These next two lines are needed to emulate
210 * old client behaviour for people who have
211 * scripts based on client output.
212 * QUESTION ? Do we want to have a 'client compatibility
213 * mode to turn these on/off ? JRA.
216 if (*c->server_domain || *c->server_os || *c->server_type)
217 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
218 c->server_domain,c->server_os,c->server_type));
220 DEBUG(4,(" session setup ok\n"));
222 status = cli_tree_connect_creds(c, share, "?????", test_creds);
223 if (!NT_STATUS_IS_OK(status)) {
224 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
225 cli_shutdown(c);
226 return NULL;
229 DEBUG(4,(" tconx ok\n"));
231 return c;
234 static char *resultp;
236 struct rn_state {
237 char **pp_long_name;
238 char *short_name;
241 static NTSTATUS listfn(
242 struct file_info *f,
243 const char *s,
244 void *private_data)
246 struct rn_state *state = (struct rn_state *)private_data;
247 if (strcmp(f->name,".") == 0) {
248 resultp[0] = '+';
249 } else if (strcmp(f->name,"..") == 0) {
250 resultp[1] = '+';
251 } else {
252 resultp[2] = '+';
255 if (state == NULL) {
256 return NT_STATUS_INTERNAL_ERROR;
259 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
260 return NT_STATUS_OK;
264 fstrcpy(state->short_name, f->short_name ? f->short_name : "");
265 (void)strlower_m(state->short_name);
266 *state->pp_long_name = SMB_STRDUP(f->name);
267 if (!*state->pp_long_name) {
268 return NT_STATUS_NO_MEMORY;
270 (void)strlower_m(*state->pp_long_name);
271 return NT_STATUS_OK;
274 static void get_real_name(struct cli_state *cli,
275 char **pp_long_name, fstring short_name)
277 struct rn_state state;
279 state.pp_long_name = pp_long_name;
280 state.short_name = short_name;
282 *pp_long_name = NULL;
283 /* nasty hack to force level 260 listings - tridge */
284 if (max_protocol <= PROTOCOL_LANMAN1) {
285 cli_list_trans(cli, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
286 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
287 &state);
288 } else {
289 cli_list_trans(cli, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
290 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
291 listfn, &state);
294 if (*short_name == 0) {
295 fstrcpy(short_name, *pp_long_name);
298 #if 0
299 if (!strchr_m(short_name,'.')) {
300 fstrcat(short_name,".");
302 #endif
305 static void testpair(struct cli_state *cli, const char *mask, const char *file)
307 uint16_t fnum;
308 fstring res1;
309 char *res2;
310 static int count;
311 fstring short_name;
312 char *long_name = NULL;
314 count++;
316 fstrcpy(res1, "---");
318 if (!NT_STATUS_IS_OK(cli_openx(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
319 DEBUG(0,("Can't create %s\n", file));
320 return;
322 cli_close(cli, fnum);
324 resultp = res1;
325 fstrcpy(short_name, "");
326 get_real_name(cli, &long_name, short_name);
327 if (!long_name) {
328 return;
330 fstrcpy(res1, "---");
331 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, listfn, NULL);
333 res2 = reg_test(cli, mask, long_name, short_name);
335 if (showall ||
336 ((strcmp(res1, res2) && !ignore_dot_errors) ||
337 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
338 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
339 res1, res2, count, mask, file, long_name, short_name));
340 if (die_on_error) exit(1);
343 cli_unlink(cli, file, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
345 if (count % 100 == 0) DEBUG(0,("%d\n", count));
346 SAFE_FREE(long_name);
349 static void test_mask(int argc, char *argv[],
350 struct cli_state *cli)
352 char *mask, *file;
353 int l1, l2, i, l;
354 int mc_len = strlen(maskchars);
355 int fc_len = strlen(filechars);
356 TALLOC_CTX *ctx = talloc_tos();
358 torture_deltree(cli, "\\masktest");
359 cli_mkdir(cli, "\\masktest");
361 if (argc >= 2) {
362 while (argc >= 2) {
363 mask = talloc_asprintf(ctx,
364 "\\masktest\\%s",
365 argv[0]);
366 file = talloc_asprintf(ctx,
367 "\\masktest\\%s",
368 argv[1]);
369 if (!mask || !file) {
370 goto finished;
372 testpair(cli, mask, file);
373 argv += 2;
374 argc -= 2;
376 goto finished;
379 while (1) {
380 l1 = 1 + random() % 20;
381 l2 = 1 + random() % 20;
382 mask = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
383 file = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
384 if (!mask || !file) {
385 goto finished;
387 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
388 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
389 l = strlen(mask);
390 for (i=0;i<l1;i++) {
391 mask[i+l] = maskchars[random() % mc_len];
393 mask[l+l1] = 0;
395 for (i=0;i<l2;i++) {
396 file[i+l] = filechars[random() % fc_len];
398 file[l+l2] = 0;
400 if (strcmp(file+l,".") == 0 ||
401 strcmp(file+l,"..") == 0 ||
402 strcmp(mask+l,"..") == 0) continue;
404 if (strspn(file+l, ".") == strlen(file+l)) continue;
406 testpair(cli, mask, file);
407 if (NumLoops && (--NumLoops == 0))
408 break;
409 TALLOC_FREE(mask);
410 TALLOC_FREE(file);
413 finished:
414 cli_rmdir(cli, "\\masktest");
418 static void usage(void)
420 printf(
421 "Usage:\n\
422 masktest //server/share [options..]\n\
423 options:\n\
424 -d debuglevel\n\
425 -n numloops\n\
426 -W workgroup\n\
427 -U user%%pass\n\
428 -s seed\n\
429 -M max protocol\n\
430 -f filechars (default %s)\n\
431 -m maskchars (default %s)\n\
432 -v verbose mode\n\
433 -E die on error\n\
434 -a show all tests\n\
435 -i ignore . and .. errors\n\
437 This program tests wildcard matching between two servers. It generates\n\
438 random pairs of filenames/masks and tests that they match in the same\n\
439 way on the servers and internally\n\
441 filechars, maskchars);
444 /****************************************************************************
445 main program
446 ****************************************************************************/
447 int main(int argc,char *argv[])
449 char *share;
450 struct cli_state *cli;
451 int opt;
452 char *p;
453 int seed;
454 TALLOC_CTX *frame = talloc_stackframe();
456 setlinebuf(stdout);
458 lp_set_cmdline("log level", "0");
460 if (argc < 2 || argv[1][0] == '-') {
461 usage();
462 exit(1);
465 share = argv[1];
467 all_string_sub(share,"/","\\",0);
469 setup_logging(argv[0], DEBUG_STDERR);
471 argc -= 1;
472 argv += 1;
474 smb_init_locale();
475 lp_load_global(get_dyn_CONFIGFILE());
476 load_interfaces();
478 if (getenv("USER")) {
479 fstrcpy(username,getenv("USER"));
482 seed = time(NULL);
484 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
485 switch (opt) {
486 case 'n':
487 NumLoops = atoi(optarg);
488 break;
489 case 'd':
490 lp_set_cmdline("log level", optarg);
491 break;
492 case 'E':
493 die_on_error = 1;
494 break;
495 case 'i':
496 ignore_dot_errors = 1;
497 break;
498 case 'v':
499 verbose++;
500 break;
501 case 'M':
502 lp_set_cmdline("client max protocol", optarg);
503 break;
504 case 'U':
505 fstrcpy(username,optarg);
506 p = strchr_m(username,'%');
507 if (p) {
508 *p = 0;
509 fstrcpy(password, p+1);
510 got_pass = 1;
512 break;
513 case 's':
514 seed = atoi(optarg);
515 break;
516 case 'h':
517 usage();
518 exit(1);
519 case 'm':
520 maskchars = optarg;
521 break;
522 case 'f':
523 filechars = optarg;
524 break;
525 case 'a':
526 showall = 1;
527 break;
528 case 'o':
529 old_list = True;
530 break;
531 default:
532 printf("Unknown option %c (%d)\n", (char)opt, opt);
533 exit(1);
537 argc -= optind;
538 argv += optind;
540 max_protocol = lp_client_max_protocol();
541 max_protocol = MIN(max_protocol, PROTOCOL_NT1);
543 if (!got_pass) {
544 char pwd[256] = {0};
545 int rc;
547 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
548 if (rc == 0) {
549 fstrcpy(password, pass);
553 test_creds = cli_session_creds_init(frame,
554 username,
555 lp_workgroup(),
556 NULL, /* realm */
557 password,
558 false, /* use_kerberos */
559 false, /* fallback_after_kerberos */
560 false, /* use_ccache */
561 false); /* password_is_nt_hash */
562 if (test_creds == NULL) {
563 d_printf("cli_session_creds_init() failed.\n");
564 exit(1);
567 cli = connect_one(share);
568 if (!cli) {
569 DEBUG(0,("Failed to connect to %s\n", share));
570 exit(1);
573 /* need to init seed after connect as clientgen uses random numbers */
574 DEBUG(0,("seed=%d\n", seed));
575 srandom(seed);
577 test_mask(argc, argv, cli);
579 TALLOC_FREE(frame);
580 return(0);