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