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