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/>.
21 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "libsmb/nmblib.h"
26 static fstring password;
27 static fstring username;
29 static int max_protocol = PROTOCOL_NT1;
30 static bool showall = False;
31 static bool old_list = False;
32 static const char *maskchars = "<>\"?*abc.";
33 static const char *filechars = "abcdefghijklm.";
35 static int die_on_error;
36 static int NumLoops = 0;
37 static int ignore_dot_errors = 0;
42 /* a test fn for LANMAN mask support */
43 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
45 const char *p = pattern, *n = string;
48 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
53 /* if (! *n && ! *p) goto match; */
54 if (*n != '.') goto nomatch;
59 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
65 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
66 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
76 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
82 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
83 if (*n == '.' && !strchr_m(n+1,'.')) {
91 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
92 if (*n != '.') goto nomatch;
97 if (c != *n) goto nomatch;
102 if (! *n) goto match;
105 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
109 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
113 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
117 static int ms_fnmatch_lanman(const char *pattern, const char *string)
119 if (!strpbrk(pattern, "?*<>\"")) {
120 if (strcmp(string,"..") == 0)
123 return strcmp(pattern, string);
126 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
127 return ms_fnmatch_lanman_core(pattern, "..") &&
128 ms_fnmatch_lanman_core(pattern, ".");
131 return ms_fnmatch_lanman_core(pattern, string);
134 static bool reg_match_one(struct cli_state *cli, const char *pattern, const char *file)
136 /* oh what a weird world this is */
137 if (old_list && strcmp(pattern, "*.*") == 0) return True;
139 if (strcmp(pattern,".") == 0) return False;
141 if (max_protocol <= PROTOCOL_LANMAN2) {
142 return ms_fnmatch_lanman(pattern, file)==0;
145 if (strcmp(file,"..") == 0) file = ".";
147 return ms_fnmatch(pattern, file, cli->protocol, False) == 0;
150 static char *reg_test(struct cli_state *cli, const char *pattern, const char *long_name, const char *short_name)
153 const char *new_pattern = 1+strrchr_m(pattern,'\\');
156 if (reg_match_one(cli, new_pattern, ".")) ret[0] = '+';
157 if (reg_match_one(cli, new_pattern, "..")) ret[1] = '+';
158 if (reg_match_one(cli, new_pattern, long_name) ||
159 (*short_name && reg_match_one(cli, new_pattern, short_name))) ret[2] = '+';
164 /*****************************************************
165 return a connection to a server
166 *******************************************************/
167 static struct cli_state *connect_one(char *share)
175 share = strchr_m(server,'\\');
176 if (!share) return NULL;
182 status = cli_connect_nb(server, NULL, 0, 0x20, "masktest", Undefined,
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(0,("Connection to %s failed. Error %s\n", server_n,
190 c->protocol = max_protocol;
192 status = cli_negprot(c);
193 if (!NT_STATUS_IS_OK(status)) {
194 DEBUG(0, ("protocol negotiation failed: %s\n",
201 char *pass = getpass("Password: ");
203 fstrcpy(password, pass);
207 status = cli_session_setup(c, username,
208 password, strlen(password),
209 password, strlen(password),
211 if (!NT_STATUS_IS_OK(status)) {
212 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status)));
217 * These next two lines are needed to emulate
218 * old client behaviour for people who have
219 * scripts based on client output.
220 * QUESTION ? Do we want to have a 'client compatibility
221 * mode to turn these on/off ? JRA.
224 if (*c->server_domain || *c->server_os || *c->server_type)
225 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
226 c->server_domain,c->server_os,c->server_type));
228 DEBUG(4,(" session setup ok\n"));
230 status = cli_tcon_andx(c, share, "?????", password,
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status)));
238 DEBUG(4,(" tconx ok\n"));
243 static char *resultp;
250 static NTSTATUS listfn(const char *mnt, struct file_info *f, const char *s,
253 struct rn_state *state = (struct rn_state *)private_data;
254 if (strcmp(f->name,".") == 0) {
256 } else if (strcmp(f->name,"..") == 0) {
263 return NT_STATUS_INTERNAL_ERROR;
266 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
270 fstrcpy(state->short_name, f->short_name);
271 strlower_m(state->short_name);
272 *state->pp_long_name = SMB_STRDUP(f->name);
273 if (!*state->pp_long_name) {
274 return NT_STATUS_NO_MEMORY;
276 strlower_m(*state->pp_long_name);
280 static void get_real_name(struct cli_state *cli,
281 char **pp_long_name, fstring short_name)
283 struct rn_state state;
285 state.pp_long_name = pp_long_name;
286 state.short_name = short_name;
288 *pp_long_name = NULL;
289 /* nasty hack to force level 260 listings - tridge */
290 if (max_protocol <= PROTOCOL_LANMAN1) {
291 cli_list_trans(cli, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
292 SMB_FIND_FILE_BOTH_DIRECTORY_INFO, listfn,
295 cli_list_trans(cli, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
296 SMB_FIND_FILE_BOTH_DIRECTORY_INFO,
300 if (*short_name == 0) {
301 fstrcpy(short_name, *pp_long_name);
305 if (!strchr_m(short_name,'.')) {
306 fstrcat(short_name,".");
311 static void testpair(struct cli_state *cli, const char *mask, const char *file)
318 char *long_name = NULL;
322 fstrcpy(res1, "---");
324 if (!NT_STATUS_IS_OK(cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0, &fnum))) {
325 DEBUG(0,("Can't create %s\n", file));
328 cli_close(cli, fnum);
331 fstrcpy(short_name, "");
332 get_real_name(cli, &long_name, short_name);
336 fstrcpy(res1, "---");
337 cli_list(cli, mask, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, listfn, NULL);
339 res2 = reg_test(cli, mask, long_name, short_name);
342 ((strcmp(res1, res2) && !ignore_dot_errors) ||
343 (strcmp(res1+2, res2+2) && ignore_dot_errors))) {
344 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
345 res1, res2, count, mask, file, long_name, short_name));
346 if (die_on_error) exit(1);
349 cli_unlink(cli, file, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
351 if (count % 100 == 0) DEBUG(0,("%d\n", count));
352 SAFE_FREE(long_name);
355 static void test_mask(int argc, char *argv[],
356 struct cli_state *cli)
360 int mc_len = strlen(maskchars);
361 int fc_len = strlen(filechars);
362 TALLOC_CTX *ctx = talloc_tos();
364 cli_mkdir(cli, "\\masktest");
366 cli_unlink(cli, "\\masktest\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
370 mask = talloc_asprintf(ctx,
373 file = talloc_asprintf(ctx,
376 if (!mask || !file) {
379 testpair(cli, mask, file);
387 l1 = 1 + random() % 20;
388 l2 = 1 + random() % 20;
389 mask = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
390 file = talloc_array(ctx, char, strlen("\\masktest\\")+1+22);
391 if (!mask || !file) {
394 memcpy(mask,"\\masktest\\",strlen("\\masktest\\")+1);
395 memcpy(file,"\\masktest\\",strlen("\\masktest\\")+1);
398 mask[i+l] = maskchars[random() % mc_len];
403 file[i+l] = filechars[random() % fc_len];
407 if (strcmp(file+l,".") == 0 ||
408 strcmp(file+l,"..") == 0 ||
409 strcmp(mask+l,"..") == 0) continue;
411 if (strspn(file+l, ".") == strlen(file+l)) continue;
413 testpair(cli, mask, file);
414 if (NumLoops && (--NumLoops == 0))
421 cli_rmdir(cli, "\\masktest");
425 static void usage(void)
429 masktest //server/share [options..]\n\
437 -f filechars (default %s)\n\
438 -m maskchars (default %s)\n\
442 -i ignore . and .. errors\n\
444 This program tests wildcard matching between two servers. It generates\n\
445 random pairs of filenames/masks and tests that they match in the same\n\
446 way on the servers and internally\n\
448 filechars, maskchars);
451 /****************************************************************************
453 ****************************************************************************/
454 int main(int argc,char *argv[])
457 struct cli_state *cli;
461 TALLOC_CTX *frame = talloc_stackframe();
465 lp_set_cmdline("log level", "0");
467 if (argc < 2 || argv[1][0] == '-') {
474 all_string_sub(share,"/","\\",0);
476 setup_logging(argv[0], DEBUG_STDERR);
482 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
485 if (getenv("USER")) {
486 fstrcpy(username,getenv("USER"));
491 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF) {
494 NumLoops = atoi(optarg);
497 lp_set_cmdline("log level", optarg);
503 ignore_dot_errors = 1;
509 max_protocol = interpret_protocol(optarg, max_protocol);
512 fstrcpy(username,optarg);
513 p = strchr_m(username,'%');
516 fstrcpy(password, p+1);
539 printf("Unknown option %c (%d)\n", (char)opt, opt);
548 cli = connect_one(share);
550 DEBUG(0,("Failed to connect to %s\n", share));
554 /* need to init seed after connect as clientgen uses random numbers */
555 DEBUG(0,("seed=%d\n", seed));
558 test_mask(argc, argv, cli);