s3-includes: only include system/filesys.h when needed.
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "trans2.h"
23
24 static fstring password;
25 static fstring username;
26 static int got_pass;
27 static int max_protocol = PROTOCOL_NT1;
28 static bool showall = False;
29 static bool old_list = False;
30 static const char *maskchars = "<>\"?*abc.";
31 static const char *filechars = "abcdefghijklm.";
32 static int verbose;
33 static int die_on_error;
34 static int NumLoops = 0;
35 static int ignore_dot_errors = 0;
36
37 extern char *optarg;
38 extern int optind;
39
40 /* a test fn for LANMAN mask support */
41 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
42 {
43         const char *p = pattern, *n = string;
44         char c;
45
46         if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
47
48         while ((c = *p++)) {
49                 switch (c) {
50                 case '.':
51                         /* if (! *n && ! *p) goto match; */
52                         if (*n != '.') goto nomatch;
53                         n++;
54                         break;
55
56                 case '?':
57                         if ((*n == '.' && n[1] != '.') || ! *n) goto next;
58                         n++;
59                         break;
60
61                 case '>':
62                         if (n[0] == '.') {
63                                 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
64                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
65                                 goto nomatch;
66                         }
67                         if (! *n) goto next;
68                         n++;
69                         break;
70
71                 case '*':
72                         if (! *p) goto match;
73                         for (; *n; n++) {
74                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
75                         }
76                         break;
77
78                 case '<':
79                         for (; *n; n++) {
80                                 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
81                                 if (*n == '.' && !strchr_m(n+1,'.')) {
82                                         n++;
83                                         break;
84                                 }
85                         }
86                         break;
87
88                 case '"':
89                         if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
90                         if (*n != '.') goto nomatch;
91                         n++;
92                         break;
93
94                 default:
95                         if (c != *n) goto nomatch;
96                         n++;
97                 }
98         }
99
100         if (! *n) goto match;
101
102  nomatch:
103         if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
104         return -1;
105
106 next:
107         if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
108         goto nomatch;
109
110  match:
111         if (verbose) printf("MATCH   pattern=[%s] string=[%s]\n", pattern, string);
112         return 0;
113 }
114
115 static int ms_fnmatch_lanman(const char *pattern, const char *string)
116 {
117         if (!strpbrk(pattern, "?*<>\"")) {
118                 if (strcmp(string,"..") == 0) 
119                         string = ".";
120
121                 return strcmp(pattern, string);
122         }
123
124         if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
125                 return ms_fnmatch_lanman_core(pattern, "..") &&
126                         ms_fnmatch_lanman_core(pattern, ".");
127         }
128
129         return ms_fnmatch_lanman_core(pattern, string);
130 }
131
132 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
133 {
134         /* oh what a weird world this is */
135         if (old_list && strcmp(pattern, "*.*") == 0) return True;
136
137         if (strcmp(pattern,".") == 0) return False;
138
139         if (max_protocol <= PROTOCOL_LANMAN2) {
140                 return ms_fnmatch_lanman(pattern, file)==0;
141         }
142
143         if (strcmp(file,"..") == 0) file = ".";
144
145         return ms_fnmatch(pattern, file, cli->protocol, False) == 0;
146 }
147
148 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
149 {
150         static fstring ret;
151         const char *new_pattern = 1+strrchr_m(pattern,'\\');
152
153         fstrcpy(ret, "---");
154         if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
155         if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
156         if (reg_match_one(cli, new_pattern, long_name) ||
157             (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
158         return ret;
159 }
160
161
162 /***************************************************** 
163 return a connection to a server
164 *******************************************************/
165 static struct cli_state *connect_one(char *share)
166 {
167         struct cli_state *c;
168         struct nmb_name called, calling;
169         char *server_n;
170         char *server;
171         struct sockaddr_storage ss;
172         NTSTATUS status;
173
174         server = share+2;
175         share = strchr_m(server,'\\');
176         if (!share) return NULL;
177         *share = 0;
178         share++;
179
180         server_n = server;
181
182         zero_sockaddr(&ss);
183
184         make_nmb_name(&calling, "masktest", 0x0);
185         make_nmb_name(&called , server, 0x20);
186
187  again:
188         zero_sockaddr(&ss);
189
190         /* have to open a new connection */
191         if (!(c=cli_initialise())) {
192                 DEBUG(0,("Connection to %s failed\n", server_n));
193                 return NULL;
194         }
195
196         status = cli_connect(c, server_n, &ss);
197         if (!NT_STATUS_IS_OK(status)) {
198                 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
199                 return NULL;
200         }
201
202         c->protocol = max_protocol;
203
204         if (!cli_session_request(c, &calling, &called)) {
205                 DEBUG(0,("session request to %s failed\n", called.name));
206                 cli_shutdown(c);
207                 if (strcmp(called.name, "*SMBSERVER")) {
208                         make_nmb_name(&called , "*SMBSERVER", 0x20);
209                         goto again;
210                 }
211                 return NULL;
212         }
213
214         DEBUG(4,(" session request ok\n"));
215
216         status = cli_negprot(c);
217         if (!NT_STATUS_IS_OK(status)) {
218                 DEBUG(0, ("protocol negotiation failed: %s\n",
219                           nt_errstr(status)));
220                 cli_shutdown(c);
221                 return NULL;
222         }
223
224         if (!got_pass) {
225                 char *pass = getpass("Password: ");
226                 if (pass) {
227                         fstrcpy(password, pass);
228                 }
229         }
230
231         status = cli_session_setup(c, username,
232                                    password, strlen(password),
233                                    password, strlen(password),
234                                    lp_workgroup());
235         if (!NT_STATUS_IS_OK(status)) {
236                 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
237                 return NULL;
238         }
239
240         /*
241          * These next two lines are needed to emulate
242          * old client behaviour for people who have
243          * scripts based on client output.
244          * QUESTION ? Do we want to have a 'client compatibility
245          * mode to turn these on/off ? JRA.
246          */
247
248         if (*c->server_domain || *c->server_os || *c->server_type)
249                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
250                         c->server_domain,c->server_os,c->server_type));
251
252         DEBUG(4,(" session setup ok\n"));
253
254         status = cli_tcon_andx(c, share, "?????", password,
255                                strlen(password)+1);
256         if (!NT_STATUS_IS_OK(status)) {
257                 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
258                 cli_shutdown(c);
259                 return NULL;
260         }
261
262         DEBUG(4,(" tconx ok\n"));
263
264         return c;
265 }
266
267 static char *resultp;
268
269 struct rn_state {
270         char **pp_long_name;
271         char *short_name;
272 };
273
274 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
275                    void *private_data)
276 {
277         struct rn_state *state = (struct rn_state *)private_data;
278         if (strcmp(f->name,".") == 0) {
279                 resultp[0] = '+';
280         } else if (strcmp(f->name,"..") == 0) {
281                 resultp[1] = '+';
282         } else {
283                 resultp[2] = '+';
284         }
285
286         if (state == NULL) {
287                 return NT_STATUS_INTERNAL_ERROR;
288         }
289
290         if (ISDOT(f->name) || ISDOTDOT(f->name))  {
291                 return NT_STATUS_OK;
292         }
293
294         fstrcpy(state->short_name, f->short_name);
295         strlower_m(state->short_name);
296         *state->pp_long_name = SMB_STRDUP(f->name);
297         if (!*state->pp_long_name) {
298                 return NT_STATUS_NO_MEMORY;
299         }
300         strlower_m(*state->pp_long_name);
301         return NT_STATUS_OK;
302 }
303
304 static void get_real_name(struct cli_state *cli,
305                           char **pp_long_name, fstring short_name)
306 {
307         struct rn_state state;
308
309         state.pp_long_name = pp_long_name;
310         state.short_name = short_name;
311
312         *pp_long_name = NULL;
313         /* nasty hack to force level 260 listings - tridge */
314         if (max_protocol <= PROTOCOL_LANMAN1) {
315                 cli_list_trans(cli, "\\masktest\\*.*", aHIDDEN | aDIR,
316                                SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
317                                &state);
318         } else {
319                 cli_list_trans(cli, "\\masktest\\*", aHIDDEN | aDIR,
320                                SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
321                                listfn, &state);
322         }
323
324         if (*short_name == 0) {
325                 fstrcpy(short_name, *pp_long_name);
326         }
327
328 #if 0
329         if (!strchr_m(short_name,'.')) {
330                 fstrcat(short_name,".");
331         }
332 #endif
333 }
334
335 static void testpair(struct cli_state *cli, const char *mask, const char *file)
336 {
337         uint16_t fnum;
338         fstring res1;
339         char *res2;
340         static int count;
341         fstring short_name;
342         char *long_name = NULL;
343
344         count++;
345
346         fstrcpy(res1, "---");
347
348         if (!NT_STATUS_IS_OK(cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
349                 DEBUG(0,("Can't create %s\n", file));
350                 return;
351         }
352         cli_close(cli, fnum);
353
354         resultp = res1;
355         fstrcpy(short_name, "");
356         get_real_name(cli, &long_name, short_name);
357         if (!long_name) {
358                 return;
359         }
360         fstrcpy(res1, "---");
361         cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
362
363         res2 = reg_test(cli, mask, long_name, short_name);
364
365         if (showall ||
366             ((strcmp(res1, res2) && !ignore_dot_errors) ||
367              (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
368                 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
369                          res1, res2, count, mask, file, long_name, short_name));
370                 if (die_on_error) exit(1);
371         }
372
373         cli_unlink(cli, file, aSYSTEM | aHIDDEN);
374
375         if (count % 100 == 0) DEBUG(0,("%d\n", count));
376         SAFE_FREE(long_name);
377 }
378
379 static void test_mask(int argc, char *argv[],
380                       struct cli_state *cli)
381 {
382         char *mask, *file;
383         int l1, l2, i, l;
384         int mc_len = strlen(maskchars);
385         int fc_len = strlen(filechars);
386         TALLOC_CTX *ctx = talloc_tos();
387
388         cli_mkdir(cli, "\\masktest");
389
390         cli_unlink(cli, "\\masktest\\*", aSYSTEM | aHIDDEN);
391
392         if (argc >= 2) {
393                 while (argc >= 2) {
394                         mask = talloc_asprintf(ctx,
395                                         "\\masktest\\%s",
396                                         argv[0]);
397                         file = talloc_asprintf(ctx,
398                                         "\\masktest\\%s",
399                                         argv[1]);
400                         if (!mask || !file) {
401                                 goto finished;
402                         }
403                         testpair(cli, mask, file);
404                         argv += 2;
405                         argc -= 2;
406                 }
407                 goto finished;
408         }
409
410         while (1) {
411                 l1 = 1 + random() % 20;
412                 l2 = 1 + random() % 20;
413                 mask = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
414                 file = TALLOC_ARRAY(ctx, char, strlen("\\masktest\\")+1+22);
415                 if (!mask || !file) {
416                         goto finished;
417                 }
418                 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
419                 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
420                 l = strlen(mask);
421                 for (i=0;i<l1;i++) {
422                         mask[i+l] = maskchars[random() % mc_len];
423                 }
424                 mask[l+l1] = 0;
425
426                 for (i=0;i<l2;i++) {
427                         file[i+l] = filechars[random() % fc_len];
428                 }
429                 file[l+l2] = 0;
430
431                 if (strcmp(file+l,".") == 0 || 
432                     strcmp(file+l,"..") == 0 ||
433                     strcmp(mask+l,"..") == 0) continue;
434
435                 if (strspn(file+l, ".") == strlen(file+l)) continue;
436
437                 testpair(cli, mask, file);
438                 if (NumLoops && (--NumLoops == 0))
439                         break;
440                 TALLOC_FREE(mask);
441                 TALLOC_FREE(file);
442         }
443
444  finished:
445         cli_rmdir(cli, "\\masktest");
446 }
447
448
449 static void usage(void)
450 {
451         printf(
452 "Usage:\n\
453   masktest //server/share [options..]\n\
454   options:\n\
455         -d debuglevel\n\
456         -n numloops\n\
457         -W workgroup\n\
458         -U user%%pass\n\
459         -s seed\n\
460         -M max protocol\n\
461         -f filechars (default %s)\n\
462         -m maskchars (default %s)\n\
463         -v                             verbose mode\n\
464         -E                             die on error\n\
465         -a                             show all tests\n\
466         -i                             ignore . and .. errors\n\
467 \n\
468   This program tests wildcard matching between two servers. It generates\n\
469   random pairs of filenames/masks and tests that they match in the same\n\
470   way on the servers and internally\n\
471 ", 
472   filechars, maskchars);
473 }
474
475 /****************************************************************************
476   main program
477 ****************************************************************************/
478  int main(int argc,char *argv[])
479 {
480         char *share;
481         struct cli_state *cli;
482         int opt;
483         char *p;
484         int seed;
485         TALLOC_CTX *frame = talloc_stackframe();
486
487         setlinebuf(stdout);
488
489         lp_set_cmdline("log level", "0");
490
491         if (argc < 2 || argv[1][0] == '-') {
492                 usage();
493                 exit(1);
494         }
495
496         share = argv[1];
497
498         all_string_sub(share,"/","\\",0);
499
500         setup_logging(argv[0], DEBUG_STDERR);
501
502         argc -= 1;
503         argv += 1;
504
505         load_case_tables();
506         lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
507         load_interfaces();
508
509         if (getenv("USER")) {
510                 fstrcpy(username,getenv("USER"));
511         }
512
513         seed = time(NULL);
514
515         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
516                 switch (opt) {
517                 case 'n':
518                         NumLoops = atoi(optarg);
519                         break;
520                 case 'd':
521                         lp_set_cmdline("log level", optarg);
522                         break;
523                 case 'E':
524                         die_on_error = 1;
525                         break;
526                 case 'i':
527                         ignore_dot_errors = 1;
528                         break;
529                 case 'v':
530                         verbose++;
531                         break;
532                 case 'M':
533                         max_protocol = interpret_protocol(optarg, max_protocol);
534                         break;
535                 case 'U':
536                         fstrcpy(username,optarg);
537                         p = strchr_m(username,'%');
538                         if (p) {
539                                 *p = 0;
540                                 fstrcpy(password, p+1);
541                                 got_pass = 1;
542                         }
543                         break;
544                 case 's':
545                         seed = atoi(optarg);
546                         break;
547                 case 'h':
548                         usage();
549                         exit(1);
550                 case 'm':
551                         maskchars = optarg;
552                         break;
553                 case 'f':
554                         filechars = optarg;
555                         break;
556                 case 'a':
557                         showall = 1;
558                         break;
559                 case 'o':
560                         old_list = True;
561                         break;
562                 default:
563                         printf("Unknown option %c (%d)\n", (char)opt, opt);
564                         exit(1);
565                 }
566         }
567
568         argc -= optind;
569         argv += optind;
570
571
572         cli = connect_one(share);
573         if (!cli) {
574                 DEBUG(0,("Failed to connect to %s\n", share));
575                 exit(1);
576         }
577
578         /* need to init seed after connect as clientgen uses random numbers */
579         DEBUG(0,("seed=%d\n", seed));
580         srandom(seed);
581
582         test_mask(argc, argv, cli);
583
584         TALLOC_FREE(frame);
585         return(0);
586 }