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