source: vendor/3.5.0/source3/torture/masktest.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 12.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 mask_match tester
4 Copyright (C) Andrew Tridgell 1999
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21
22static fstring password;
23static fstring username;
24static int got_pass;
25static int max_protocol = PROTOCOL_NT1;
26static bool showall = False;
27static bool old_list = False;
28static const char *maskchars = "<>\"?*abc.";
29static const char *filechars = "abcdefghijklm.";
30static int verbose;
31static int die_on_error;
32static int NumLoops = 0;
33static int ignore_dot_errors = 0;
34
35extern char *optarg;
36extern int optind;
37extern bool AllowDebugChange;
38
39/* a test fn for LANMAN mask support */
40static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
41{
42 const char *p = pattern, *n = string;
43 char c;
44
45 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
46
47 while ((c = *p++)) {
48 switch (c) {
49 case '.':
50 /* if (! *n && ! *p) goto match; */
51 if (*n != '.') goto nomatch;
52 n++;
53 break;
54
55 case '?':
56 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
57 n++;
58 break;
59
60 case '>':
61 if (n[0] == '.') {
62 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
63 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
64 goto nomatch;
65 }
66 if (! *n) goto next;
67 n++;
68 break;
69
70 case '*':
71 if (! *p) goto match;
72 for (; *n; n++) {
73 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
74 }
75 break;
76
77 case '<':
78 for (; *n; n++) {
79 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
80 if (*n == '.' && !strchr_m(n+1,'.')) {
81 n++;
82 break;
83 }
84 }
85 break;
86
87 case '"':
88 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
89 if (*n != '.') goto nomatch;
90 n++;
91 break;
92
93 default:
94 if (c != *n) goto nomatch;
95 n++;
96 }
97 }
98
99 if (! *n) goto match;
100
101 nomatch:
102 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
103 return -1;
104
105next:
106 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
107 goto nomatch;
108
109 match:
110 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
111 return 0;
112}
113
114static int ms_fnmatch_lanman(const char *pattern, const char *string)
115{
116 if (!strpbrk(pattern, "?*<>\"")) {
117 if (strcmp(string,"..") == 0)
118 string = ".";
119
120 return strcmp(pattern, string);
121 }
122
123 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
124 return ms_fnmatch_lanman_core(pattern, "..") &&
125 ms_fnmatch_lanman_core(pattern, ".");
126 }
127
128 return ms_fnmatch_lanman_core(pattern, string);
129}
130
131static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
132{
133 /* oh what a weird world this is */
134 if (old_list && strcmp(pattern, "*.*") == 0) return True;
135
136 if (strcmp(pattern,".") == 0) return False;
137
138 if (max_protocol <= PROTOCOL_LANMAN2) {
139 return ms_fnmatch_lanman(pattern, file)==0;
140 }
141
142 if (strcmp(file,"..") == 0) file = ".";
143
144 return ms_fnmatch(pattern, file, cli->protocol, False) == 0;
145}
146
147static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
148{
149 static fstring ret;
150 const char *new_pattern = 1+strrchr_m(pattern,'\\');
151
152 fstrcpy(ret, "---");
153 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
154 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
155 if (reg_match_one(cli, new_pattern, long_name) ||
156 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
157 return ret;
158}
159
160
161/*****************************************************
162return a connection to a server
163*******************************************************/
164static struct cli_state *connect_one(char *share)
165{
166 struct cli_state *c;
167 struct nmb_name called, calling;
168 char *server_n;
169 char *server;
170 struct sockaddr_storage ss;
171 NTSTATUS status;
172
173 server = share+2;
174 share = strchr_m(server,'\\');
175 if (!share) return NULL;
176 *share = 0;
177 share++;
178
179 server_n = server;
180
181 zero_sockaddr(&ss);
182
183 make_nmb_name(&calling, "masktest", 0x0);
184 make_nmb_name(&called , server, 0x20);
185
186 again:
187 zero_sockaddr(&ss);
188
189 /* have to open a new connection */
190 if (!(c=cli_initialise())) {
191 DEBUG(0,("Connection to %s failed\n", server_n));
192 return NULL;
193 }
194
195 status = cli_connect(c, server_n, &ss);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
198 return NULL;
199 }
200
201 c->protocol = max_protocol;
202
203 if (!cli_session_request(c, &calling, &called)) {
204 DEBUG(0,("session request to %s failed\n", called.name));
205 cli_shutdown(c);
206 if (strcmp(called.name, "*SMBSERVER")) {
207 make_nmb_name(&called , "*SMBSERVER", 0x20);
208 goto again;
209 }
210 return NULL;
211 }
212
213 DEBUG(4,(" session request ok\n"));
214
215 status = cli_negprot(c);
216 if (!NT_STATUS_IS_OK(status)) {
217 DEBUG(0, ("protocol negotiation failed: %s\n",
218 nt_errstr(status)));
219 cli_shutdown(c);
220 return NULL;
221 }
222
223 if (!got_pass) {
224 char *pass = getpass("Password: ");
225 if (pass) {
226 fstrcpy(password, pass);
227 }
228 }
229
230 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
231 password, strlen(password),
232 password, strlen(password),
233 lp_workgroup()))) {
234 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
235 return NULL;
236 }
237
238 /*
239 * These next two lines are needed to emulate
240 * old client behaviour for people who have
241 * scripts based on client output.
242 * QUESTION ? Do we want to have a 'client compatibility
243 * mode to turn these on/off ? JRA.
244 */
245
246 if (*c->server_domain || *c->server_os || *c->server_type)
247 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
248 c->server_domain,c->server_os,c->server_type));
249
250 DEBUG(4,(" session setup ok\n"));
251
252 status = cli_tcon_andx(c, share, "?????", password,
253 strlen(password)+1);
254 if (!NT_STATUS_IS_OK(status)) {
255 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
256 cli_shutdown(c);
257 return NULL;
258 }
259
260 DEBUG(4,(" tconx ok\n"));
261
262 return c;
263}
264
265static char *resultp;
266static file_info *f_info;
267
268static void listfn(const char *mnt, file_info *f, const char *s, void *state)
269{
270 if (strcmp(f->name,".") == 0) {
271 resultp[0] = '+';
272 } else if (strcmp(f->name,"..") == 0) {
273 resultp[1] = '+';
274 } else {
275 resultp[2] = '+';
276 }
277 f_info = f;
278}
279
280static void get_real_name(struct cli_state *cli,
281 char **pp_long_name, fstring short_name)
282{
283 *pp_long_name = NULL;
284 /* nasty hack to force level 260 listings - tridge */
285 cli->capabilities |= CAP_NT_SMBS;
286 if (max_protocol <= PROTOCOL_LANMAN1) {
287 cli_list_new(cli, "\\masktest\\*.*", aHIDDEN | aDIR, listfn, NULL);
288 } else {
289 cli_list_new(cli, "\\masktest\\*", aHIDDEN | aDIR, listfn, NULL);
290 }
291 if (f_info) {
292 fstrcpy(short_name, f_info->short_name);
293 strlower_m(short_name);
294 *pp_long_name = SMB_STRDUP(f_info->name);
295 if (!*pp_long_name) {
296 return;
297 }
298 strlower_m(*pp_long_name);
299 }
300
301 if (*short_name == 0) {
302 fstrcpy(short_name, *pp_long_name);
303 }
304
305#if 0
306 if (!strchr_m(short_name,'.')) {
307 fstrcat(short_name,".");
308 }
309#endif
310}
311
312static void testpair(struct cli_state *cli, const char *mask, const char *file)
313{
314 uint16_t fnum;
315 fstring res1;
316 char *res2;
317 static int count;
318 fstring short_name;
319 char *long_name = NULL;
320
321 count++;
322
323 fstrcpy(res1, "---");
324
325 if (!NT_STATUS_IS_OK(cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
326 DEBUG(0,("Can't create %s\n", file));
327 return;
328 }
329 cli_close(cli, fnum);
330
331 resultp = res1;
332 fstrcpy(short_name, "");
333 f_info = NULL;
334 get_real_name(cli, &long_name, short_name);
335 if (!long_name) {
336 return;
337 }
338 f_info = NULL;
339 fstrcpy(res1, "---");
340 cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
341
342 res2 = reg_test(cli, mask, long_name, short_name);
343
344 if (showall ||
345 ((strcmp(res1, res2) && !ignore_dot_errors) ||
346 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
347 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
348 res1, res2, count, mask, file, long_name, short_name));
349 if (die_on_error) exit(1);
350 }
351
352 cli_unlink(cli, file, aSYSTEM | aHIDDEN);
353
354 if (count % 100 == 0) DEBUG(0,("%d\n", count));
355 SAFE_FREE(long_name);
356}
357
358static void test_mask(int argc, char *argv[],
359 struct cli_state *cli)
360{
361 char *mask, *file;
362 int l1, l2, i, l;
363 int mc_len = strlen(maskchars);
364 int fc_len = strlen(filechars);
365 TALLOC_CTX *ctx = talloc_tos();
366
367 cli_mkdir(cli, "\\masktest");
368
369 cli_unlink(cli, "\\masktest\\*", aSYSTEM | aHIDDEN);
370
371 if (argc >= 2) {
372 while (argc >= 2) {
373 mask = talloc_asprintf(ctx,
374 "\\masktest\\%s",
375 argv[0]);
376 file = talloc_asprintf(ctx,
377 "\\masktest\\%s",
378 argv[1]);
379 if (!mask || !file) {
380 goto finished;
381 }
382 testpair(cli, mask, file);
383 argv += 2;
384 argc -= 2;
385 }
386 goto finished;
387 }
388
389 while (1) {
390 l1 = 1 + random() % 20;
391 l2 = 1 + random() % 20;
392 mask = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
393 file = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
394 if (!mask || !file) {
395 goto finished;
396 }
397 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
398 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
399 l = strlen(mask);
400 for (i=0;i<l1;i++) {
401 mask[i+l] = maskchars[random() % mc_len];
402 }
403 mask[l+l1] = 0;
404
405 for (i=0;i<l2;i++) {
406 file[i+l] = filechars[random() % fc_len];
407 }
408 file[l+l2] = 0;
409
410 if (strcmp(file+l,".") == 0 ||
411 strcmp(file+l,"..") == 0 ||
412 strcmp(mask+l,"..") == 0) continue;
413
414 if (strspn(file+l, ".") == strlen(file+l)) continue;
415
416 testpair(cli, mask, file);
417 if (NumLoops && (--NumLoops == 0))
418 break;
419 TALLOC_FREE(mask);
420 TALLOC_FREE(file);
421 }
422
423 finished:
424 cli_rmdir(cli, "\\masktest");
425}
426
427
428static void usage(void)
429{
430 printf(
431"Usage:\n\
432 masktest //server/share [options..]\n\
433 options:\n\
434 -d debuglevel\n\
435 -n numloops\n\
436 -W workgroup\n\
437 -U user%%pass\n\
438 -s seed\n\
439 -M max protocol\n\
440 -f filechars (default %s)\n\
441 -m maskchars (default %s)\n\
442 -v verbose mode\n\
443 -E die on error\n\
444 -a show all tests\n\
445 -i ignore . and .. errors\n\
446\n\
447 This program tests wildcard matching between two servers. It generates\n\
448 random pairs of filenames/masks and tests that they match in the same\n\
449 way on the servers and internally\n\
450",
451 filechars, maskchars);
452}
453
454/****************************************************************************
455 main program
456****************************************************************************/
457 int main(int argc,char *argv[])
458{
459 char *share;
460 struct cli_state *cli;
461 int opt;
462 char *p;
463 int seed;
464 TALLOC_CTX *frame = talloc_stackframe();
465
466 setlinebuf(stdout);
467
468 dbf = x_stderr;
469
470 DEBUGLEVEL = 0;
471 AllowDebugChange = False;
472
473 if (argc < 2 || argv[1][0] == '-') {
474 usage();
475 exit(1);
476 }
477
478 share = argv[1];
479
480 all_string_sub(share,"/","\\",0);
481
482 setup_logging(argv[0],True);
483
484 argc -= 1;
485 argv += 1;
486
487 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
488 load_interfaces();
489
490 if (getenv("USER")) {
491 fstrcpy(username,getenv("USER"));
492 }
493
494 seed = time(NULL);
495
496 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
497 switch (opt) {
498 case 'n':
499 NumLoops = atoi(optarg);
500 break;
501 case 'd':
502 DEBUGLEVEL = atoi(optarg);
503 break;
504 case 'E':
505 die_on_error = 1;
506 break;
507 case 'i':
508 ignore_dot_errors = 1;
509 break;
510 case 'v':
511 verbose++;
512 break;
513 case 'M':
514 max_protocol = interpret_protocol(optarg, max_protocol);
515 break;
516 case 'U':
517 fstrcpy(username,optarg);
518 p = strchr_m(username,'%');
519 if (p) {
520 *p = 0;
521 fstrcpy(password, p+1);
522 got_pass = 1;
523 }
524 break;
525 case 's':
526 seed = atoi(optarg);
527 break;
528 case 'h':
529 usage();
530 exit(1);
531 case 'm':
532 maskchars = optarg;
533 break;
534 case 'f':
535 filechars = optarg;
536 break;
537 case 'a':
538 showall = 1;
539 break;
540 case 'o':
541 old_list = True;
542 break;
543 default:
544 printf("Unknown option %c (%d)\n", (char)opt, opt);
545 exit(1);
546 }
547 }
548
549 argc -= optind;
550 argv += optind;
551
552
553 cli = connect_one(share);
554 if (!cli) {
555 DEBUG(0,("Failed to connect to %s\n", share));
556 exit(1);
557 }
558
559 /* need to init seed after connect as clientgen uses random numbers */
560 DEBUG(0,("seed=%d\n", seed));
561 srandom(seed);
562
563 test_mask(argc, argv, cli);
564
565 TALLOC_FREE(frame);
566 return(0);
567}
Note: See TracBrowser for help on using the repository browser.