s3:libsmb: get rid of cli_state_protocol
[Samba/gebeck_regimport.git] / source3 / torture / masktest.c
blob66699496350848c973bd2210980561195d7cb204
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 = cli_negprot(c, max_protocol);
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(0, ("protocol negotiation failed: %s\n",
194 nt_errstr(status)));
195 cli_shutdown(c);
196 return NULL;
199 if (!got_pass) {
200 char *pass = getpass("Password: ");
201 if (pass) {
202 fstrcpy(password, pass);
206 status = cli_session_setup(c, username,
207 password, strlen(password),
208 password, strlen(password),
209 lp_workgroup());
210 if (!NT_STATUS_IS_OK(status)) {
211 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
212 return NULL;
216 * These next two lines are needed to emulate
217 * old client behaviour for people who have
218 * scripts based on client output.
219 * QUESTION ? Do we want to have a 'client compatibility
220 * mode to turn these on/off ? JRA.
223 if (*c->server_domain || *c->server_os || *c->server_type)
224 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
225 c->server_domain,c->server_os,c->server_type));
227 DEBUG(4,(" session setup ok\n"));
229 status = cli_tree_connect(c, share, "?????", password,
230 strlen(password)+1);
231 if (!NT_STATUS_IS_OK(status)) {
232 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
233 cli_shutdown(c);
234 return NULL;
237 DEBUG(4,(" tconx ok\n"));
239 return c;
242 static char *resultp;
244 struct rn_state {
245 char **pp_long_name;
246 char *short_name;
249 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
250 void *private_data)
252 struct rn_state *state = (struct rn_state *)private_data;
253 if (strcmp(f->name,".") == 0) {
254 resultp[0] = '+';
255 } else if (strcmp(f->name,"..") == 0) {
256 resultp[1] = '+';
257 } else {
258 resultp[2] = '+';
261 if (state == NULL) {
262 return NT_STATUS_INTERNAL_ERROR;
265 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
266 return NT_STATUS_OK;
270 fstrcpy(state->short_name, f->short_name ? f->short_name : "");
271 strlower_m(state->short_name);
272 *state->pp_long_name = SMB_STRDUP(f->name);
273 if (!*state->pp_long_name) {
274 return NT_STATUS_NO_MEMORY;
276 strlower_m(*state->pp_long_name);
277 return NT_STATUS_OK;
280 static void get_real_name(struct cli_state *cli,
281 char **pp_long_name, fstring short_name)
283 struct rn_state state;
285 state.pp_long_name = pp_long_name;
286 state.short_name = short_name;
288 *pp_long_name = NULL;
289 /* nasty hack to force level 260 listings - tridge */
290 if (max_protocol <= PROTOCOL_LANMAN1) {
291 cli_list_trans(cli, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
292 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
293 &state);
294 } else {
295 cli_list_trans(cli, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
296 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
297 listfn, &state);
300 if (*short_name == 0) {
301 fstrcpy(short_name, *pp_long_name);
304 #if 0
305 if (!strchr_m(short_name,'.')) {
306 fstrcat(short_name,".");
308 #endif
311 static void testpair(struct cli_state *cli, const char *mask, const char *file)
313 uint16_t fnum;
314 fstring res1;
315 char *res2;
316 static int count;
317 fstring short_name;
318 char *long_name = NULL;
320 count++;
322 fstrcpy(res1, "---");
324 if (!NT_STATUS_IS_OK(cli_openx(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
325 DEBUG(0,("Can't create %s\n", file));
326 return;
328 cli_close(cli, fnum);
330 resultp = res1;
331 fstrcpy(short_name, "");
332 get_real_name(cli, &long_name, short_name);
333 if (!long_name) {
334 return;
336 fstrcpy(res1, "---");
337 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, listfn, NULL);
339 res2 = reg_test(cli, mask, long_name, short_name);
341 if (showall ||
342 ((strcmp(res1, res2) && !ignore_dot_errors) ||
343 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
344 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
345 res1, res2, count, mask, file, long_name, short_name));
346 if (die_on_error) exit(1);
349 cli_unlink(cli, file, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
351 if (count % 100 == 0) DEBUG(0,("%d\n", count));
352 SAFE_FREE(long_name);
355 static void test_mask(int argc, char *argv[],
356 struct cli_state *cli)
358 char *mask, *file;
359 int l1, l2, i, l;
360 int mc_len = strlen(maskchars);
361 int fc_len = strlen(filechars);
362 TALLOC_CTX *ctx = talloc_tos();
364 cli_mkdir(cli, "\\masktest");
366 cli_unlink(cli, "\\masktest\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
368 if (argc >= 2) {
369 while (argc >= 2) {
370 mask = talloc_asprintf(ctx,
371 "\\masktest\\%s",
372 argv[0]);
373 file = talloc_asprintf(ctx,
374 "\\masktest\\%s",
375 argv[1]);
376 if (!mask || !file) {
377 goto finished;
379 testpair(cli, mask, file);
380 argv += 2;
381 argc -= 2;
383 goto finished;
386 while (1) {
387 l1 = 1 + random() % 20;
388 l2 = 1 + random() % 20;
389 mask = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
390 file = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
391 if (!mask || !file) {
392 goto finished;
394 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
395 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
396 l = strlen(mask);
397 for (i=0;i<l1;i++) {
398 mask[i+l] = maskchars[random() % mc_len];
400 mask[l+l1] = 0;
402 for (i=0;i<l2;i++) {
403 file[i+l] = filechars[random() % fc_len];
405 file[l+l2] = 0;
407 if (strcmp(file+l,".") == 0 ||
408 strcmp(file+l,"..") == 0 ||
409 strcmp(mask+l,"..") == 0) continue;
411 if (strspn(file+l, ".") == strlen(file+l)) continue;
413 testpair(cli, mask, file);
414 if (NumLoops && (--NumLoops == 0))
415 break;
416 TALLOC_FREE(mask);
417 TALLOC_FREE(file);
420 finished:
421 cli_rmdir(cli, "\\masktest");
425 static void usage(void)
427 printf(
428 "Usage:\n\
429 masktest //server/share [options..]\n\
430 options:\n\
431 -d debuglevel\n\
432 -n numloops\n\
433 -W workgroup\n\
434 -U user%%pass\n\
435 -s seed\n\
436 -M max protocol\n\
437 -f filechars (default %s)\n\
438 -m maskchars (default %s)\n\
439 -v verbose mode\n\
440 -E die on error\n\
441 -a show all tests\n\
442 -i ignore . and .. errors\n\
444 This program tests wildcard matching between two servers. It generates\n\
445 random pairs of filenames/masks and tests that they match in the same\n\
446 way on the servers and internally\n\
448 filechars, maskchars);
451 /****************************************************************************
452 main program
453 ****************************************************************************/
454 int main(int argc,char *argv[])
456 char *share;
457 struct cli_state *cli;
458 int opt;
459 char *p;
460 int seed;
461 TALLOC_CTX *frame = talloc_stackframe();
463 setlinebuf(stdout);
465 lp_set_cmdline("log level", "0");
467 if (argc < 2 || argv[1][0] == '-') {
468 usage();
469 exit(1);
472 share = argv[1];
474 all_string_sub(share,"/","\\",0);
476 setup_logging(argv[0], DEBUG_STDERR);
478 argc -= 1;
479 argv += 1;
481 load_case_tables();
482 lp_load_global(get_dyn_CONFIGFILE());
483 load_interfaces();
485 if (getenv("USER")) {
486 fstrcpy(username,getenv("USER"));
489 seed = time(NULL);
491 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
492 switch (opt) {
493 case 'n':
494 NumLoops = atoi(optarg);
495 break;
496 case 'd':
497 lp_set_cmdline("log level", optarg);
498 break;
499 case 'E':
500 die_on_error = 1;
501 break;
502 case 'i':
503 ignore_dot_errors = 1;
504 break;
505 case 'v':
506 verbose++;
507 break;
508 case 'M':
509 max_protocol = interpret_protocol(optarg, max_protocol);
510 break;
511 case 'U':
512 fstrcpy(username,optarg);
513 p = strchr_m(username,'%');
514 if (p) {
515 *p = 0;
516 fstrcpy(password, p+1);
517 got_pass = 1;
519 break;
520 case 's':
521 seed = atoi(optarg);
522 break;
523 case 'h':
524 usage();
525 exit(1);
526 case 'm':
527 maskchars = optarg;
528 break;
529 case 'f':
530 filechars = optarg;
531 break;
532 case 'a':
533 showall = 1;
534 break;
535 case 'o':
536 old_list = True;
537 break;
538 default:
539 printf("Unknown option %c (%d)\n", (char)opt, opt);
540 exit(1);
544 argc -= optind;
545 argv += optind;
548 cli = connect_one(share);
549 if (!cli) {
550 DEBUG(0,("Failed to connect to %s\n", share));
551 exit(1);
554 /* need to init seed after connect as clientgen uses random numbers */
555 DEBUG(0,("seed=%d\n", seed));
556 srandom(seed);
558 test_mask(argc, argv, cli);
560 TALLOC_FREE(frame);
561 return(0);