s3: async cli_list
[kai/samba.git] / source3 / torture / masktest.c
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
22 static fstring password;
23 static fstring username;
24 static int got_pass;
25 static int max_protocol = PROTOCOL_NT1;
26 static bool showall = False;
27 static bool old_list = False;
28 static const char *maskchars = "<>\"?*abc.";
29 static const char *filechars = "abcdefghijklm.";
30 static int verbose;
31 static int die_on_error;
32 static int NumLoops = 0;
33 static int ignore_dot_errors = 0;
34
35 extern char *optarg;
36 extern int optind;
37 extern bool AllowDebugChange;
38
39 /* a test fn for LANMAN mask support */
40 static 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
105 next:
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
114 static 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
131 static 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
147 static 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 /***************************************************** 
162 return a connection to a server
163 *******************************************************/
164 static 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
265 static char *resultp;
266
267 struct rn_state {
268         char **pp_long_name;
269         char *short_name;
270 };
271
272 static void listfn(const char *mnt, struct file_info *f, const char *s,
273                    void *private_data)
274 {
275         struct rn_state *state = (struct rn_state *)private_data;
276         if (strcmp(f->name,".") == 0) {
277                 resultp[0] = '+';
278         } else if (strcmp(f->name,"..") == 0) {
279                 resultp[1] = '+';
280         } else {
281                 resultp[2] = '+';
282         }
283
284         if ((state == NULL) || ISDOT(f->name) || ISDOTDOT(f->name))  {
285                 return;
286         }
287
288         fstrcpy(state->short_name, f->short_name);
289         strlower_m(state->short_name);
290         *state->pp_long_name = SMB_STRDUP(f->name);
291         if (!*state->pp_long_name) {
292                 return;
293         }
294         strlower_m(*state->pp_long_name);
295 }
296
297 static void get_real_name(struct cli_state *cli,
298                           char **pp_long_name, fstring short_name)
299 {
300         struct rn_state state;
301
302         state.pp_long_name = pp_long_name;
303         state.short_name = short_name;
304
305         *pp_long_name = NULL;
306         /* nasty hack to force level 260 listings - tridge */
307         if (max_protocol <= PROTOCOL_LANMAN1) {
308                 cli_list_trans(cli, "\\masktest\\*.*", aHIDDEN | aDIR,
309                                SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
310                                &state);
311         } else {
312                 cli_list_trans(cli, "\\masktest\\*", aHIDDEN | aDIR,
313                                SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
314                                listfn, &state);
315         }
316
317         if (*short_name == 0) {
318                 fstrcpy(short_name, *pp_long_name);
319         }
320
321 #if 0
322         if (!strchr_m(short_name,'.')) {
323                 fstrcat(short_name,".");
324         }
325 #endif
326 }
327
328 static void testpair(struct cli_state *cli, const char *mask, const char *file)
329 {
330         uint16_t fnum;
331         fstring res1;
332         char *res2;
333         static int count;
334         fstring short_name;
335         char *long_name = NULL;
336
337         count++;
338
339         fstrcpy(res1, "---");
340
341         if (!NT_STATUS_IS_OK(cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
342                 DEBUG(0,("Can't create %s\n", file));
343                 return;
344         }
345         cli_close(cli, fnum);
346
347         resultp = res1;
348         fstrcpy(short_name, "");
349         get_real_name(cli, &long_name, short_name);
350         if (!long_name) {
351                 return;
352         }
353         fstrcpy(res1, "---");
354         cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
355
356         res2 = reg_test(cli, mask, long_name, short_name);
357
358         if (showall ||
359             ((strcmp(res1, res2) && !ignore_dot_errors) ||
360              (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
361                 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
362                          res1, res2, count, mask, file, long_name, short_name));
363                 if (die_on_error) exit(1);
364         }
365
366         cli_unlink(cli, file, aSYSTEM | aHIDDEN);
367
368         if (count % 100 == 0) DEBUG(0,("%d\n", count));
369         SAFE_FREE(long_name);
370 }
371
372 static void test_mask(int argc, char *argv[],
373                       struct cli_state *cli)
374 {
375         char *mask, *file;
376         int l1, l2, i, l;
377         int mc_len = strlen(maskchars);
378         int fc_len = strlen(filechars);
379         TALLOC_CTX *ctx = talloc_tos();
380
381         cli_mkdir(cli, "\\masktest");
382
383         cli_unlink(cli, "\\masktest\\*", aSYSTEM | aHIDDEN);
384
385         if (argc >= 2) {
386                 while (argc >= 2) {
387                         mask = talloc_asprintf(ctx,
388                                         "\\masktest\\%s",
389                                         argv[0]);
390                         file = talloc_asprintf(ctx,
391                                         "\\masktest\\%s",
392                                         argv[1]);
393                         if (!mask || !file) {
394                                 goto finished;
395                         }
396                         testpair(cli, mask, file);
397                         argv += 2;
398                         argc -= 2;
399                 }
400                 goto finished;
401         }
402
403         while (1) {
404                 l1 = 1 + random() % 20;
405                 l2 = 1 + random() % 20;
406                 mask = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
407                 file = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
408                 if (!mask || !file) {
409                         goto finished;
410                 }
411                 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
412                 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
413                 l = strlen(mask);
414                 for (i=0;i<l1;i++) {
415                         mask[i+l] = maskchars[random() % mc_len];
416                 }
417                 mask[l+l1] = 0;
418
419                 for (i=0;i<l2;i++) {
420                         file[i+l] = filechars[random() % fc_len];
421                 }
422                 file[l+l2] = 0;
423
424                 if (strcmp(file+l,".") == 0 || 
425                     strcmp(file+l,"..") == 0 ||
426                     strcmp(mask+l,"..") == 0) continue;
427
428                 if (strspn(file+l, ".") == strlen(file+l)) continue;
429
430                 testpair(cli, mask, file);
431                 if (NumLoops && (--NumLoops == 0))
432                         break;
433                 TALLOC_FREE(mask);
434                 TALLOC_FREE(file);
435         }
436
437  finished:
438         cli_rmdir(cli, "\\masktest");
439 }
440
441
442 static void usage(void)
443 {
444         printf(
445 "Usage:\n\
446   masktest //server/share [options..]\n\
447   options:\n\
448         -d debuglevel\n\
449         -n numloops\n\
450         -W workgroup\n\
451         -U user%%pass\n\
452         -s seed\n\
453         -M max protocol\n\
454         -f filechars (default %s)\n\
455         -m maskchars (default %s)\n\
456         -v                             verbose mode\n\
457         -E                             die on error\n\
458         -a                             show all tests\n\
459         -i                             ignore . and .. errors\n\
460 \n\
461   This program tests wildcard matching between two servers. It generates\n\
462   random pairs of filenames/masks and tests that they match in the same\n\
463   way on the servers and internally\n\
464 ", 
465   filechars, maskchars);
466 }
467
468 /****************************************************************************
469   main program
470 ****************************************************************************/
471  int main(int argc,char *argv[])
472 {
473         char *share;
474         struct cli_state *cli;
475         int opt;
476         char *p;
477         int seed;
478         TALLOC_CTX *frame = talloc_stackframe();
479
480         setlinebuf(stdout);
481
482         dbf = x_stderr;
483
484         DEBUGLEVEL = 0;
485         AllowDebugChange = False;
486
487         if (argc < 2 || argv[1][0] == '-') {
488                 usage();
489                 exit(1);
490         }
491
492         share = argv[1];
493
494         all_string_sub(share,"/","\\",0);
495
496         setup_logging(argv[0],True);
497
498         argc -= 1;
499         argv += 1;
500
501         load_case_tables();
502         lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
503         load_interfaces();
504
505         if (getenv("USER")) {
506                 fstrcpy(username,getenv("USER"));
507         }
508
509         seed = time(NULL);
510
511         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
512                 switch (opt) {
513                 case 'n':
514                         NumLoops = atoi(optarg);
515                         break;
516                 case 'd':
517                         DEBUGLEVEL = atoi(optarg);
518                         break;
519                 case 'E':
520                         die_on_error = 1;
521                         break;
522                 case 'i':
523                         ignore_dot_errors = 1;
524                         break;
525                 case 'v':
526                         verbose++;
527                         break;
528                 case 'M':
529                         max_protocol = interpret_protocol(optarg, max_protocol);
530                         break;
531                 case 'U':
532                         fstrcpy(username,optarg);
533                         p = strchr_m(username,'%');
534                         if (p) {
535                                 *p = 0;
536                                 fstrcpy(password, p+1);
537                                 got_pass = 1;
538                         }
539                         break;
540                 case 's':
541                         seed = atoi(optarg);
542                         break;
543                 case 'h':
544                         usage();
545                         exit(1);
546                 case 'm':
547                         maskchars = optarg;
548                         break;
549                 case 'f':
550                         filechars = optarg;
551                         break;
552                 case 'a':
553                         showall = 1;
554                         break;
555                 case 'o':
556                         old_list = True;
557                         break;
558                 default:
559                         printf("Unknown option %c (%d)\n", (char)opt, opt);
560                         exit(1);
561                 }
562         }
563
564         argc -= optind;
565         argv += optind;
566
567
568         cli = connect_one(share);
569         if (!cli) {
570                 DEBUG(0,("Failed to connect to %s\n", share));
571                 exit(1);
572         }
573
574         /* need to init seed after connect as clientgen uses random numbers */
575         DEBUG(0,("seed=%d\n", seed));
576         srandom(seed);
577
578         test_mask(argc, argv, cli);
579
580         TALLOC_FREE(frame);
581         return(0);
582 }