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