more fixes from the IRIX compiler (thanks herb!)
[ira/wip.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 #include "includes.h"
22
23 static fstring password;
24 static fstring username;
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
34 /* a test fn for LANMAN mask support */
35 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
36 {
37         const char *p = pattern, *n = string;
38         char c;
39
40         if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
41
42         while ((c = *p++)) {
43                 switch (c) {
44                 case '.':
45                         /* if (! *n && ! *p) goto match; */
46                         if (*n != '.') goto nomatch;
47                         n++;
48                         break;
49
50                 case '?':
51                         if ((*n == '.' && n[1] != '.') || ! *n) goto next;
52                         n++;
53                         break;
54
55                 case '>':
56                         if (n[0] == '.') {
57                                 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
58                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
59                                 goto nomatch;
60                         }
61                         if (! *n) goto next;
62                         n++;
63                         break;
64
65                 case '*':
66                         if (! *p) goto match;
67                         for (; *n; n++) {
68                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
69                         }
70                         break;
71
72                 case '<':
73                         for (; *n; n++) {
74                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
75                                 if (*n == '.' && !strchr_m(n+1,'.')) {
76                                         n++;
77                                         break;
78                                 }
79                         }
80                         break;
81
82                 case '"':
83                         if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
84                         if (*n != '.') goto nomatch;
85                         n++;
86                         break;
87
88                 default:
89                         if (c != *n) goto nomatch;
90                         n++;
91                 }
92         }
93         
94         if (! *n) goto match;
95         
96  nomatch:
97         if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
98         return -1;
99
100 next:
101         if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
102         goto nomatch;
103
104  match:
105         if (verbose) printf("MATCH   pattern=[%s] string=[%s]\n", pattern, string);
106         return 0;
107 }
108
109 static int ms_fnmatch_lanman(const char *pattern, const char *string)
110 {
111         if (!strpbrk(pattern, "?*<>\"")) {
112                 if (strcmp(string,"..") == 0) 
113                         string = ".";
114
115                 return strcmp(pattern, string);
116         }
117
118         if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
119                 return ms_fnmatch_lanman_core(pattern, "..") &&
120                         ms_fnmatch_lanman_core(pattern, ".");
121         }
122
123         return ms_fnmatch_lanman_core(pattern, string);
124 }
125
126 static BOOL reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
127 {
128         /* oh what a weird world this is */
129         if (old_list && strcmp(pattern, "*.*") == 0) return True;
130
131         if (strcmp(pattern,".") == 0) return False;
132
133         if (max_protocol <= PROTOCOL_LANMAN2) {
134                 return ms_fnmatch_lanman(pattern, file)==0;
135         }
136
137         if (strcmp(file,"..") == 0) file = ".";
138
139         return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
140 }
141
142 static char *reg_test(struct cli_state *cli, char *pattern, char *long_name, char *short_name)
143 {
144         static fstring ret;
145         fstrcpy(ret, "---");
146
147         pattern = 1+strrchr_m(pattern,'\\');
148
149         if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
150         if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
151         if (reg_match_one(cli, pattern, long_name) || 
152             (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
153         return ret;
154 }
155
156
157 /***************************************************** 
158 return a connection to a server
159 *******************************************************/
160 static struct cli_state *connect_one(char *share)
161 {
162         struct cli_state *c;
163         fstring server;
164         uint_t flags = 0;
165         NTSTATUS status;
166
167         fstrcpy(server,share+2);
168         share = strchr_m(server,'\\');
169         if (!share) return NULL;
170         *share = 0;
171         share++;
172
173         status = cli_full_connection(&c, "masktest",
174                                      server, NULL, 
175                                      share, "?????", 
176                                      username, lp_workgroup(), 
177                                      password, flags, NULL);
178
179         if (!NT_STATUS_IS_OK(status)) {
180                 return NULL;
181         }
182
183         return c;
184 }
185
186 static char *resultp;
187 static struct {
188         pstring long_name;
189         pstring short_name;
190 } last_hit;
191 static BOOL f_info_hit;
192
193 static void listfn(file_info *f, const char *s, void *state)
194 {
195         if (strcmp(f->name,".") == 0) {
196                 resultp[0] = '+';
197         } else if (strcmp(f->name,"..") == 0) {
198                 resultp[1] = '+';               
199         } else {
200                 resultp[2] = '+';
201         }
202         pstrcpy(last_hit.long_name, f->name);
203         pstrcpy(last_hit.short_name, f->short_name);
204         f_info_hit = True;
205 }
206
207 static void get_real_name(struct cli_state *cli, 
208                           pstring long_name, fstring short_name)
209 {
210         const char *mask;
211         if (max_protocol <= PROTOCOL_LANMAN1) {
212                 mask = "\\masktest\\*.*";
213         } else {
214                 mask = "\\masktest\\*";
215         }
216
217         f_info_hit = False;
218
219         cli_list_new(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
220                      listfn, NULL);
221
222         if (f_info_hit) {
223                 fstrcpy(short_name, last_hit.short_name);
224                 strlower(short_name);
225                 pstrcpy(long_name, last_hit.long_name);
226                 strlower(long_name);
227         }
228
229         if (*short_name == 0) {
230                 fstrcpy(short_name, long_name);
231         }
232 }
233
234 static void testpair(struct cli_state *cli, char *mask, char *file)
235 {
236         int fnum;
237         fstring res1;
238         char *res2;
239         static int count;
240         fstring short_name;
241         pstring long_name;
242
243         count++;
244
245         fstrcpy(res1, "---");
246
247         fnum = cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0);
248         if (fnum == -1) {
249                 DEBUG(0,("Can't create %s\n", file));
250                 return;
251         }
252         cli_close(cli, fnum);
253
254         resultp = res1;
255         fstrcpy(short_name, "");
256         get_real_name(cli, long_name, short_name);
257         fstrcpy(res1, "---");
258         cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
259                  listfn, NULL);
260
261         res2 = reg_test(cli, mask, long_name, short_name);
262
263         if (showall || strcmp(res1, res2)) {
264                 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
265                          res1, res2, count, mask, file, long_name, short_name);
266                 if (die_on_error) exit(1);
267         }
268
269         cli_unlink(cli, file);
270
271         if (count % 100 == 0) DEBUG(0,("%d\n", count));
272 }
273
274 static void test_mask(int argc, char *argv[], 
275                       struct cli_state *cli)
276 {
277         pstring mask, file;
278         int l1, l2, i, l;
279         int mc_len = strlen(maskchars);
280         int fc_len = strlen(filechars);
281
282         cli_mkdir(cli, "\\masktest");
283
284         cli_unlink(cli, "\\masktest\\*");
285
286         if (argc >= 2) {
287                 while (argc >= 2) {
288                         pstrcpy(mask,"\\masktest\\");
289                         pstrcpy(file,"\\masktest\\");
290                         pstrcat(mask, argv[0]);
291                         pstrcat(file, argv[1]);
292                         testpair(cli, mask, file);
293                         argv += 2;
294                         argc -= 2;
295                 }
296                 goto finished;
297         }
298
299         while (1) {
300                 l1 = 1 + random() % 20;
301                 l2 = 1 + random() % 20;
302                 pstrcpy(mask,"\\masktest\\");
303                 pstrcpy(file,"\\masktest\\");
304                 l = strlen(mask);
305                 for (i=0;i<l1;i++) {
306                         mask[i+l] = maskchars[random() % mc_len];
307                 }
308                 mask[l+l1] = 0;
309
310                 for (i=0;i<l2;i++) {
311                         file[i+l] = filechars[random() % fc_len];
312                 }
313                 file[l+l2] = 0;
314
315                 if (strcmp(file+l,".") == 0 || 
316                     strcmp(file+l,"..") == 0 ||
317                     strcmp(mask+l,"..") == 0) continue;
318
319                 if (strspn(file+l, ".") == strlen(file+l)) continue;
320
321                 testpair(cli, mask, file);
322                 if (NumLoops && (--NumLoops == 0))
323                         break;
324         }
325
326  finished:
327         cli_rmdir(cli, "\\masktest");
328 }
329
330
331 static void usage(void)
332 {
333         printf(
334 "Usage:\n\
335   masktest //server/share [options..]\n\
336   options:\n\
337         -d debuglevel\n\
338         -n numloops\n\
339         -W workgroup\n\
340         -U user%%pass\n\
341         -s seed\n\
342         -M max protocol\n\
343         -f filechars (default %s)\n\
344         -m maskchars (default %s)\n\
345         -v                             verbose mode\n\
346         -E                             die on error\n\
347         -a                             show all tests\n\
348 \n\
349   This program tests wildcard matching between two servers. It generates\n\
350   random pairs of filenames/masks and tests that they match in the same\n\
351   way on the servers and internally\n\
352 ", 
353   filechars, maskchars);
354 }
355
356 /****************************************************************************
357   main program
358 ****************************************************************************/
359  int main(int argc,char *argv[])
360 {
361         char *share;
362         struct cli_state *cli;  
363         int opt;
364         char *p;
365         int seed;
366
367         setlinebuf(stdout);
368
369         setup_logging("masktest", DEBUG_STDOUT);
370
371         lp_set_cmdline("log level", "0");
372
373         if (argc < 2 || argv[1][0] == '-') {
374                 usage();
375                 exit(1);
376         }
377
378         share = argv[1];
379
380         all_string_sub(share,"/","\\",0);
381
382         setup_logging(argv[0], DEBUG_STDOUT);
383
384         argc -= 1;
385         argv += 1;
386
387         lp_load(dyn_CONFIGFILE,True,False,False);
388         load_interfaces();
389
390         if (getenv("USER")) {
391                 fstrcpy(username,getenv("USER"));
392         }
393
394         seed = time(NULL);
395
396         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vE")) != EOF) {
397                 switch (opt) {
398                 case 'n':
399                         NumLoops = atoi(optarg);
400                         break;
401                 case 'd':
402                         DEBUGLEVEL = atoi(optarg);
403                         break;
404                 case 'E':
405                         die_on_error = 1;
406                         break;
407                 case 'v':
408                         verbose++;
409                         break;
410                 case 'M':
411                         max_protocol = interpret_protocol(optarg, max_protocol);
412                         break;
413                 case 'U':
414                         fstrcpy(username,optarg);
415                         p = strchr_m(username,'%');
416                         if (p) {
417                                 *p = 0;
418                                 fstrcpy(password, p+1);
419                         }
420                         break;
421                 case 's':
422                         seed = atoi(optarg);
423                         break;
424                 case 'h':
425                         usage();
426                         exit(1);
427                 case 'm':
428                         maskchars = optarg;
429                         break;
430                 case 'f':
431                         filechars = optarg;
432                         break;
433                 case 'a':
434                         showall = 1;
435                         break;
436                 case 'o':
437                         old_list = True;
438                         break;
439                 default:
440                         printf("Unknown option %c (%d)\n", (char)opt, opt);
441                         exit(1);
442                 }
443         }
444
445         argc -= optind;
446         argv += optind;
447
448
449         cli = connect_one(share);
450         if (!cli) {
451                 DEBUG(0,("Failed to connect to %s\n", share));
452                 exit(1);
453         }
454
455         /* need to init seed after connect as clientgen uses random numbers */
456         DEBUG(0,("seed=%d\n", seed));
457         srandom(seed);
458
459         test_mask(argc, argv, cli);
460
461         return(0);
462 }