Merge commit 'origin/v3-2-test' into my_branch
[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, char *pattern, char *long_name, char *short_name)
148 {
149         static fstring ret;
150         fstrcpy(ret, "---");
151
152         pattern = 1+strrchr_m(pattern,'\\');
153
154         if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
155         if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
156         if (reg_match_one(cli, pattern, long_name) || 
157             (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
158         return ret;
159 }
160
161
162 /***************************************************** 
163 return a connection to a server
164 *******************************************************/
165 static struct cli_state *connect_one(char *share)
166 {
167         struct cli_state *c;
168         struct nmb_name called, calling;
169         char *server_n;
170         char *server;
171         struct in_addr ip;
172         NTSTATUS status;
173
174         server = share+2;
175         share = strchr_m(server,'\\');
176         if (!share) return NULL;
177         *share = 0;
178         share++;
179
180         server_n = server;
181         
182         zero_ip_v4(&ip);
183
184         make_nmb_name(&calling, "masktest", 0x0);
185         make_nmb_name(&called , server, 0x20);
186
187  again:
188         zero_ip_v4(&ip);
189
190         /* have to open a new connection */
191         if (!(c=cli_initialise())) {
192                 DEBUG(0,("Connection to %s failed\n", server_n));
193                 return NULL;
194         }
195
196         status = cli_connect(c, server_n, &ip);
197         if (!NT_STATUS_IS_OK(status)) {
198                 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
199                 return NULL;
200         }
201
202         c->protocol = max_protocol;
203
204         if (!cli_session_request(c, &calling, &called)) {
205                 DEBUG(0,("session request to %s failed\n", called.name));
206                 cli_shutdown(c);
207                 if (strcmp(called.name, "*SMBSERVER")) {
208                         make_nmb_name(&called , "*SMBSERVER", 0x20);
209                         goto again;
210                 }
211                 return NULL;
212         }
213
214         DEBUG(4,(" session request ok\n"));
215
216         if (!cli_negprot(c)) {
217                 DEBUG(0,("protocol negotiation failed\n"));
218                 cli_shutdown(c);
219                 return NULL;
220         }
221
222         if (!got_pass) {
223                 char *pass = getpass("Password: ");
224                 if (pass) {
225                         fstrcpy(password, pass);
226                 }
227         }
228
229         if (!NT_STATUS_IS_OK(cli_session_setup(c, username, 
230                                                password, strlen(password),
231                                                password, strlen(password),
232                                                lp_workgroup()))) {
233                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
234                 return NULL;
235         }
236
237         /*
238          * These next two lines are needed to emulate
239          * old client behaviour for people who have
240          * scripts based on client output.
241          * QUESTION ? Do we want to have a 'client compatibility
242          * mode to turn these on/off ? JRA.
243          */
244
245         if (*c->server_domain || *c->server_os || *c->server_type)
246                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
247                         c->server_domain,c->server_os,c->server_type));
248         
249         DEBUG(4,(" session setup ok\n"));
250
251         if (!cli_send_tconX(c, share, "?????",
252                             password, strlen(password)+1)) {
253                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
254                 cli_shutdown(c);
255                 return NULL;
256         }
257
258         DEBUG(4,(" tconx ok\n"));
259
260         return c;
261 }
262
263 static char *resultp;
264 static file_info *f_info;
265
266 static void listfn(const char *mnt, file_info *f, const char *s, void *state)
267 {
268         if (strcmp(f->name,".") == 0) {
269                 resultp[0] = '+';
270         } else if (strcmp(f->name,"..") == 0) {
271                 resultp[1] = '+';               
272         } else {
273                 resultp[2] = '+';
274         }
275         f_info = f;
276 }
277
278 static void get_real_name(struct cli_state *cli, 
279                           pstring long_name, fstring short_name)
280 {
281         /* nasty hack to force level 260 listings - tridge */
282         cli->capabilities |= CAP_NT_SMBS;
283         if (max_protocol <= PROTOCOL_LANMAN1) {
284                 cli_list_new(cli, "\\masktest\\*.*", aHIDDEN | aDIR, listfn, NULL);
285         } else {
286                 cli_list_new(cli, "\\masktest\\*", aHIDDEN | aDIR, listfn, NULL);
287         }
288         if (f_info) {
289                 fstrcpy(short_name, f_info->short_name);
290                 strlower_m(short_name);
291                 pstrcpy(long_name, f_info->name);
292                 strlower_m(long_name);
293         }
294
295         if (*short_name == 0) {
296                 fstrcpy(short_name, long_name);
297         }
298
299 #if 0
300         if (!strchr_m(short_name,'.')) {
301                 fstrcat(short_name,".");
302         }
303 #endif
304 }
305
306 static void testpair(struct cli_state *cli, char *mask, char *file)
307 {
308         int fnum;
309         fstring res1;
310         char *res2;
311         static int count;
312         fstring short_name;
313         pstring long_name;
314
315         count++;
316
317         fstrcpy(res1, "---");
318
319         fnum = cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0);
320         if (fnum == -1) {
321                 DEBUG(0,("Can't create %s\n", file));
322                 return;
323         }
324         cli_close(cli, fnum);
325
326         resultp = res1;
327         fstrcpy(short_name, "");
328         f_info = NULL;
329         get_real_name(cli, long_name, short_name);
330         f_info = NULL;
331         fstrcpy(res1, "---");
332         cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
333
334         res2 = reg_test(cli, mask, long_name, short_name);
335
336         if (showall || 
337             ((strcmp(res1, res2) && !ignore_dot_errors) ||
338              (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
339                 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
340                          res1, res2, count, mask, file, long_name, short_name));
341                 if (die_on_error) exit(1);
342         }
343
344         cli_unlink(cli, file);
345
346         if (count % 100 == 0) DEBUG(0,("%d\n", count));
347 }
348
349 static void test_mask(int argc, char *argv[], 
350                       struct cli_state *cli)
351 {
352         pstring mask, file;
353         int l1, l2, i, l;
354         int mc_len = strlen(maskchars);
355         int fc_len = strlen(filechars);
356
357         cli_mkdir(cli, "\\masktest");
358
359         cli_unlink(cli, "\\masktest\\*");
360
361         if (argc >= 2) {
362                 while (argc >= 2) {
363                         pstrcpy(mask,"\\masktest\\");
364                         pstrcpy(file,"\\masktest\\");
365                         pstrcat(mask, argv[0]);
366                         pstrcat(file, argv[1]);
367                         testpair(cli, mask, file);
368                         argv += 2;
369                         argc -= 2;
370                 }
371                 goto finished;
372         }
373
374         while (1) {
375                 l1 = 1 + random() % 20;
376                 l2 = 1 + random() % 20;
377                 pstrcpy(mask,"\\masktest\\");
378                 pstrcpy(file,"\\masktest\\");
379                 l = strlen(mask);
380                 for (i=0;i<l1;i++) {
381                         mask[i+l] = maskchars[random() % mc_len];
382                 }
383                 mask[l+l1] = 0;
384
385                 for (i=0;i<l2;i++) {
386                         file[i+l] = filechars[random() % fc_len];
387                 }
388                 file[l+l2] = 0;
389
390                 if (strcmp(file+l,".") == 0 || 
391                     strcmp(file+l,"..") == 0 ||
392                     strcmp(mask+l,"..") == 0) continue;
393
394                 if (strspn(file+l, ".") == strlen(file+l)) continue;
395
396                 testpair(cli, mask, file);
397                 if (NumLoops && (--NumLoops == 0))
398                         break;
399         }
400
401  finished:
402         cli_rmdir(cli, "\\masktest");
403 }
404
405
406 static void usage(void)
407 {
408         printf(
409 "Usage:\n\
410   masktest //server/share [options..]\n\
411   options:\n\
412         -d debuglevel\n\
413         -n numloops\n\
414         -W workgroup\n\
415         -U user%%pass\n\
416         -s seed\n\
417         -M max protocol\n\
418         -f filechars (default %s)\n\
419         -m maskchars (default %s)\n\
420         -v                             verbose mode\n\
421         -E                             die on error\n\
422         -a                             show all tests\n\
423         -i                             ignore . and .. errors\n\
424 \n\
425   This program tests wildcard matching between two servers. It generates\n\
426   random pairs of filenames/masks and tests that they match in the same\n\
427   way on the servers and internally\n\
428 ", 
429   filechars, maskchars);
430 }
431
432 /****************************************************************************
433   main program
434 ****************************************************************************/
435  int main(int argc,char *argv[])
436 {
437         char *share;
438         struct cli_state *cli;  
439         int opt;
440         char *p;
441         int seed;
442
443         setlinebuf(stdout);
444
445         dbf = x_stderr;
446
447         DEBUGLEVEL = 0;
448         AllowDebugChange = False;
449
450         if (argc < 2 || argv[1][0] == '-') {
451                 usage();
452                 exit(1);
453         }
454
455         share = argv[1];
456
457         all_string_sub(share,"/","\\",0);
458
459         setup_logging(argv[0],True);
460
461         argc -= 1;
462         argv += 1;
463
464         lp_load(dyn_CONFIGFILE,True,False,False,True);
465         load_interfaces();
466
467         if (getenv("USER")) {
468                 fstrcpy(username,getenv("USER"));
469         }
470
471         seed = time(NULL);
472
473         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
474                 switch (opt) {
475                 case 'n':
476                         NumLoops = atoi(optarg);
477                         break;
478                 case 'd':
479                         DEBUGLEVEL = atoi(optarg);
480                         break;
481                 case 'E':
482                         die_on_error = 1;
483                         break;
484                 case 'i':
485                         ignore_dot_errors = 1;
486                         break;
487                 case 'v':
488                         verbose++;
489                         break;
490                 case 'M':
491                         max_protocol = interpret_protocol(optarg, max_protocol);
492                         break;
493                 case 'U':
494                         fstrcpy(username,optarg);
495                         p = strchr_m(username,'%');
496                         if (p) {
497                                 *p = 0;
498                                 fstrcpy(password, p+1);
499                                 got_pass = 1;
500                         }
501                         break;
502                 case 's':
503                         seed = atoi(optarg);
504                         break;
505                 case 'h':
506                         usage();
507                         exit(1);
508                 case 'm':
509                         maskchars = optarg;
510                         break;
511                 case 'f':
512                         filechars = optarg;
513                         break;
514                 case 'a':
515                         showall = 1;
516                         break;
517                 case 'o':
518                         old_list = True;
519                         break;
520                 default:
521                         printf("Unknown option %c (%d)\n", (char)opt, opt);
522                         exit(1);
523                 }
524         }
525
526         argc -= optind;
527         argv += optind;
528
529
530         cli = connect_one(share);
531         if (!cli) {
532                 DEBUG(0,("Failed to connect to %s\n", share));
533                 exit(1);
534         }
535
536         /* need to init seed after connect as clientgen uses random numbers */
537         DEBUG(0,("seed=%d\n", seed));
538         srandom(seed);
539
540         test_mask(argc, argv, cli);
541
542         return(0);
543 }