r1654: rename cli_ -> smbcli_
[jelmer/samba4-debian.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 smbcli_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 smbcli_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 smbcli_state *connect_one(char *share)
161 {
162         struct smbcli_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 = smbcli_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 smbcli_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         smbcli_list_new(cli->tree, mask, 
220                      FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
221                      listfn, NULL);
222
223         if (f_info_hit) {
224                 fstrcpy(short_name, last_hit.short_name);
225                 strlower(short_name);
226                 pstrcpy(long_name, last_hit.long_name);
227                 strlower(long_name);
228         }
229
230         if (*short_name == 0) {
231                 fstrcpy(short_name, long_name);
232         }
233 }
234
235 static void testpair(struct smbcli_state *cli, char *mask, char *file)
236 {
237         int fnum;
238         fstring res1;
239         char *res2;
240         static int count;
241         fstring short_name;
242         pstring long_name;
243
244         count++;
245
246         fstrcpy(res1, "---");
247
248         fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
249         if (fnum == -1) {
250                 DEBUG(0,("Can't create %s\n", file));
251                 return;
252         }
253         smbcli_close(cli->tree, fnum);
254
255         resultp = res1;
256         fstrcpy(short_name, "");
257         get_real_name(cli, long_name, short_name);
258         fstrcpy(res1, "---");
259         smbcli_list(cli->tree, mask, 
260                  FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
261                  listfn, NULL);
262
263         res2 = reg_test(cli, mask, long_name, short_name);
264
265         if (showall || strcmp(res1, res2)) {
266                 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
267                          res1, res2, count, mask, file, long_name, short_name);
268                 if (die_on_error) exit(1);
269         }
270
271         smbcli_unlink(cli->tree, file);
272
273         if (count % 100 == 0) DEBUG(0,("%d\n", count));
274 }
275
276 static void test_mask(int argc, char *argv[], 
277                       struct smbcli_state *cli)
278 {
279         pstring mask, file;
280         int l1, l2, i, l;
281         int mc_len = strlen(maskchars);
282         int fc_len = strlen(filechars);
283
284         smbcli_mkdir(cli->tree, "\\masktest");
285
286         smbcli_unlink(cli->tree, "\\masktest\\*");
287
288         if (argc >= 2) {
289                 while (argc >= 2) {
290                         pstrcpy(mask,"\\masktest\\");
291                         pstrcpy(file,"\\masktest\\");
292                         pstrcat(mask, argv[0]);
293                         pstrcat(file, argv[1]);
294                         testpair(cli, mask, file);
295                         argv += 2;
296                         argc -= 2;
297                 }
298                 goto finished;
299         }
300
301         while (1) {
302                 l1 = 1 + random() % 20;
303                 l2 = 1 + random() % 20;
304                 pstrcpy(mask,"\\masktest\\");
305                 pstrcpy(file,"\\masktest\\");
306                 l = strlen(mask);
307                 for (i=0;i<l1;i++) {
308                         mask[i+l] = maskchars[random() % mc_len];
309                 }
310                 mask[l+l1] = 0;
311
312                 for (i=0;i<l2;i++) {
313                         file[i+l] = filechars[random() % fc_len];
314                 }
315                 file[l+l2] = 0;
316
317                 if (strcmp(file+l,".") == 0 || 
318                     strcmp(file+l,"..") == 0 ||
319                     strcmp(mask+l,"..") == 0) continue;
320
321                 if (strspn(file+l, ".") == strlen(file+l)) continue;
322
323                 testpair(cli, mask, file);
324                 if (NumLoops && (--NumLoops == 0))
325                         break;
326         }
327
328  finished:
329         smbcli_rmdir(cli->tree, "\\masktest");
330 }
331
332
333 static void usage(void)
334 {
335         printf(
336 "Usage:\n\
337   masktest //server/share [options..]\n\
338   options:\n\
339         -d debuglevel\n\
340         -n numloops\n\
341         -W workgroup\n\
342         -U user%%pass\n\
343         -s seed\n\
344         -M max protocol\n\
345         -f filechars (default %s)\n\
346         -m maskchars (default %s)\n\
347         -v                             verbose mode\n\
348         -E                             die on error\n\
349         -a                             show all tests\n\
350 \n\
351   This program tests wildcard matching between two servers. It generates\n\
352   random pairs of filenames/masks and tests that they match in the same\n\
353   way on the servers and internally\n\
354 ", 
355   filechars, maskchars);
356 }
357
358 /****************************************************************************
359   main program
360 ****************************************************************************/
361  int main(int argc,char *argv[])
362 {
363         char *share;
364         struct smbcli_state *cli;       
365         int opt;
366         char *p;
367         int seed;
368
369         setlinebuf(stdout);
370
371         setup_logging("masktest", DEBUG_STDOUT);
372
373         lp_set_cmdline("log level", "0");
374
375         if (argc < 2 || argv[1][0] == '-') {
376                 usage();
377                 exit(1);
378         }
379
380         share = argv[1];
381
382         all_string_sub(share,"/","\\",0);
383
384         setup_logging(argv[0], DEBUG_STDOUT);
385
386         argc -= 1;
387         argv += 1;
388
389         lp_load(dyn_CONFIGFILE,True,False,False);
390         load_interfaces();
391
392         if (getenv("USER")) {
393                 fstrcpy(username,getenv("USER"));
394         }
395
396         seed = time(NULL);
397
398         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vE")) != EOF) {
399                 switch (opt) {
400                 case 'n':
401                         NumLoops = atoi(optarg);
402                         break;
403                 case 'd':
404                         DEBUGLEVEL = atoi(optarg);
405                         break;
406                 case 'E':
407                         die_on_error = 1;
408                         break;
409                 case 'v':
410                         verbose++;
411                         break;
412                 case 'M':
413                         max_protocol = interpret_protocol(optarg, max_protocol);
414                         break;
415                 case 'U':
416                         fstrcpy(username,optarg);
417                         p = strchr_m(username,'%');
418                         if (p) {
419                                 *p = 0;
420                                 fstrcpy(password, p+1);
421                         }
422                         break;
423                 case 's':
424                         seed = atoi(optarg);
425                         break;
426                 case 'h':
427                         usage();
428                         exit(1);
429                 case 'm':
430                         maskchars = optarg;
431                         break;
432                 case 'f':
433                         filechars = optarg;
434                         break;
435                 case 'a':
436                         showall = 1;
437                         break;
438                 case 'o':
439                         old_list = True;
440                         break;
441                 default:
442                         printf("Unknown option %c (%d)\n", (char)opt, opt);
443                         exit(1);
444                 }
445         }
446
447         argc -= optind;
448         argv += optind;
449
450
451         cli = connect_one(share);
452         if (!cli) {
453                 DEBUG(0,("Failed to connect to %s\n", share));
454                 exit(1);
455         }
456
457         /* need to init seed after connect as clientgen uses random numbers */
458         DEBUG(0,("seed=%d\n", seed));
459         srandom(seed);
460
461         test_mask(argc, argv, cli);
462
463         return(0);
464 }