2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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/>.
22 static fstring password;
23 static fstring username;
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.";
31 static int die_on_error;
32 static int NumLoops = 0;
33 static int ignore_dot_errors = 0;
37 extern bool AllowDebugChange;
39 /* a test fn for LANMAN mask support */
40 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
42 const char *p = pattern, *n = string;
45 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
50 /* if (! *n && ! *p) goto match; */
51 if (*n != '.') goto nomatch;
56 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
62 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
63 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
73 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
79 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
80 if (*n == '.' && !strchr_m(n+1,'.')) {
88 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
89 if (*n != '.') goto nomatch;
94 if (c != *n) goto nomatch;
102 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
106 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
110 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
114 static int ms_fnmatch_lanman(const char *pattern, const char *string)
116 if (!strpbrk(pattern, "?*<>\"")) {
117 if (strcmp(string,"..") == 0)
120 return strcmp(pattern, string);
123 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
124 return ms_fnmatch_lanman_core(pattern, "..") &&
125 ms_fnmatch_lanman_core(pattern, ".");
128 return ms_fnmatch_lanman_core(pattern, string);
131 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
133 /* oh what a weird world this is */
134 if (old_list && strcmp(pattern, "*.*") == 0) return True;
136 if (strcmp(pattern,".") == 0) return False;
138 if (max_protocol <= PROTOCOL_LANMAN2) {
139 return ms_fnmatch_lanman(pattern, file)==0;
142 if (strcmp(file,"..") == 0) file = ".";
144 return ms_fnmatch(pattern, file, cli->protocol, False) == 0;
147 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
150 const char *new_pattern = 1+strrchr_m(pattern,'\\');
153 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
154 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
155 if (reg_match_one(cli, new_pattern, long_name) ||
156 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
161 /*****************************************************
162 return a connection to a server
163 *******************************************************/
164 static struct cli_state *connect_one(char *share)
167 struct nmb_name called, calling;
170 struct sockaddr_storage ss;
174 share = strchr_m(server,'\\');
175 if (!share) return NULL;
183 make_nmb_name(&calling, "masktest", 0x0);
184 make_nmb_name(&called , server, 0x20);
189 /* have to open a new connection */
190 if (!(c=cli_initialise())) {
191 DEBUG(0,("Connection to %s failed\n", server_n));
195 status = cli_connect(c, server_n, &ss);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(0,("Connection to %s failed. Error %s\n", server_n, nt_errstr(status) ));
201 c->protocol = max_protocol;
203 if (!cli_session_request(c, &calling, &called)) {
204 DEBUG(0,("session request to %s failed\n", called.name));
206 if (strcmp(called.name, "*SMBSERVER")) {
207 make_nmb_name(&called , "*SMBSERVER", 0x20);
213 DEBUG(4,(" session request ok\n"));
215 status = cli_negprot(c);
216 if (!NT_STATUS_IS_OK(status)) {
217 DEBUG(0, ("protocol negotiation failed: %s\n",
224 char *pass = getpass("Password: ");
226 fstrcpy(password, pass);
230 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
231 password, strlen(password),
232 password, strlen(password),
234 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
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.
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));
250 DEBUG(4,(" session setup ok\n"));
252 status = cli_tcon_andx(c, share, "?????", password,
254 if (!NT_STATUS_IS_OK(status)) {
255 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
260 DEBUG(4,(" tconx ok\n"));
265 static char *resultp;
272 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
275 struct rn_state *state = (struct rn_state *)private_data;
276 if (strcmp(f->name,".") == 0) {
278 } else if (strcmp(f->name,"..") == 0) {
285 return NT_STATUS_INTERNAL_ERROR;
288 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
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;
298 strlower_m(*state->pp_long_name);
302 static void get_real_name(struct cli_state *cli,
303 char **pp_long_name, fstring short_name)
305 struct rn_state state;
307 state.pp_long_name = pp_long_name;
308 state.short_name = short_name;
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,
317 cli_list_trans(cli, "\\masktest\\*", aHIDDEN | aDIR,
318 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
322 if (*short_name == 0) {
323 fstrcpy(short_name, *pp_long_name);
327 if (!strchr_m(short_name,'.')) {
328 fstrcat(short_name,".");
333 static void testpair(struct cli_state *cli, const char *mask, const char *file)
340 char *long_name = NULL;
344 fstrcpy(res1, "---");
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));
350 cli_close(cli, fnum);
353 fstrcpy(short_name, "");
354 get_real_name(cli, &long_name, short_name);
358 fstrcpy(res1, "---");
359 cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
361 res2 = reg_test(cli, mask, long_name, short_name);
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);
371 cli_unlink(cli, file, aSYSTEM | aHIDDEN);
373 if (count % 100 == 0) DEBUG(0,("%d\n", count));
374 SAFE_FREE(long_name);
377 static void test_mask(int argc, char *argv[],
378 struct cli_state *cli)
382 int mc_len = strlen(maskchars);
383 int fc_len = strlen(filechars);
384 TALLOC_CTX *ctx = talloc_tos();
386 cli_mkdir(cli, "\\masktest");
388 cli_unlink(cli, "\\masktest\\*", aSYSTEM | aHIDDEN);
392 mask = talloc_asprintf(ctx,
395 file = talloc_asprintf(ctx,
398 if (!mask || !file) {
401 testpair(cli, mask, file);
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) {
416 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
417 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
420 mask[i+l] = maskchars[random() % mc_len];
425 file[i+l] = filechars[random() % fc_len];
429 if (strcmp(file+l,".") == 0 ||
430 strcmp(file+l,"..") == 0 ||
431 strcmp(mask+l,"..") == 0) continue;
433 if (strspn(file+l, ".") == strlen(file+l)) continue;
435 testpair(cli, mask, file);
436 if (NumLoops && (--NumLoops == 0))
443 cli_rmdir(cli, "\\masktest");
447 static void usage(void)
451 masktest //server/share [options..]\n\
459 -f filechars (default %s)\n\
460 -m maskchars (default %s)\n\
464 -i ignore . and .. errors\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\
470 filechars, maskchars);
473 /****************************************************************************
475 ****************************************************************************/
476 int main(int argc,char *argv[])
479 struct cli_state *cli;
483 TALLOC_CTX *frame = talloc_stackframe();
488 AllowDebugChange = False;
490 if (argc < 2 || argv[1][0] == '-') {
497 all_string_sub(share,"/","\\",0);
499 setup_logging(argv[0], DEBUG_STDERR);
505 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
508 if (getenv("USER")) {
509 fstrcpy(username,getenv("USER"));
514 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
517 NumLoops = atoi(optarg);
520 DEBUGLEVEL = atoi(optarg);
526 ignore_dot_errors = 1;
532 max_protocol = interpret_protocol(optarg, max_protocol);
535 fstrcpy(username,optarg);
536 p = strchr_m(username,'%');
539 fstrcpy(password, p+1);
562 printf("Unknown option %c (%d)\n", (char)opt, opt);
571 cli = connect_one(share);
573 DEBUG(0,("Failed to connect to %s\n", share));
577 /* need to init seed after connect as clientgen uses random numbers */
578 DEBUG(0,("seed=%d\n", seed));
581 test_mask(argc, argv, cli);