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