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