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