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