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