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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static fstring password;
24 static fstring username;
25 static BOOL showall = False;
26 static BOOL old_list = False;
27 static const char *maskchars = "<>\"?*abc.";
28 static const char *filechars = "abcdefghijklm.";
30 static int die_on_error;
31 static int NumLoops = 0;
33 /* a test fn for LANMAN mask support */
34 static int ms_fnmatch_lanman_core(const char *pattern, const char *string)
36 const char *p = pattern, *n = string;
39 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
44 /* if (! *n && ! *p) goto match; */
45 if (*n != '.') goto nomatch;
50 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
56 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
57 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
67 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
73 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
74 if (*n == '.' && !strchr_m(n+1,'.')) {
82 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
83 if (*n != '.') goto nomatch;
88 if (c != *n) goto nomatch;
96 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
100 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
104 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
108 static int ms_fnmatch_lanman(const char *pattern, const char *string)
110 if (!strpbrk(pattern, "?*<>\"")) {
111 if (strcmp(string,"..") == 0)
114 return strcmp(pattern, string);
117 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
118 return ms_fnmatch_lanman_core(pattern, "..") &&
119 ms_fnmatch_lanman_core(pattern, ".");
122 return ms_fnmatch_lanman_core(pattern, string);
125 static BOOL reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
127 /* oh what a weird world this is */
128 if (old_list && strcmp(pattern, "*.*") == 0) return True;
130 if (strcmp(pattern,".") == 0) return False;
132 if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
133 return ms_fnmatch_lanman(pattern, file)==0;
136 if (strcmp(file,"..") == 0) file = ".";
138 return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
141 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
146 pattern = 1+strrchr_m(pattern,'\\');
148 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
149 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
150 if (reg_match_one(cli, pattern, long_name) ||
151 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
156 /*****************************************************
157 return a connection to a server
158 *******************************************************/
159 static struct smbcli_state *connect_one(char *share)
161 struct smbcli_state *c;
166 fstrcpy(server,share+2);
167 share = strchr_m(server,'\\');
168 if (!share) return NULL;
172 status = smbcli_full_connection(NULL, &c, "masktest",
175 username, lp_workgroup(),
176 password, flags, NULL);
178 if (!NT_STATUS_IS_OK(status)) {
185 static char *resultp;
190 static BOOL f_info_hit;
192 static void listfn(file_info *f, const char *s, void *state)
194 if (strcmp(f->name,".") == 0) {
196 } else if (strcmp(f->name,"..") == 0) {
201 pstrcpy(last_hit.long_name, f->name);
202 pstrcpy(last_hit.short_name, f->short_name);
206 static void get_real_name(struct smbcli_state *cli,
207 pstring long_name, fstring short_name)
210 if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
211 mask = "\\masktest\\*.*";
213 mask = "\\masktest\\*";
218 smbcli_list_new(cli->tree, mask,
219 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
223 fstrcpy(short_name, last_hit.short_name);
224 strlower(short_name);
225 pstrcpy(long_name, last_hit.long_name);
229 if (*short_name == 0) {
230 fstrcpy(short_name, long_name);
234 static void testpair(struct smbcli_state *cli, char *mask, char *file)
245 fstrcpy(res1, "---");
247 fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
249 DEBUG(0,("Can't create %s\n", file));
252 smbcli_close(cli->tree, fnum);
255 fstrcpy(short_name, "");
256 get_real_name(cli, long_name, short_name);
257 fstrcpy(res1, "---");
258 smbcli_list(cli->tree, mask,
259 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
262 res2 = reg_test(cli, mask, long_name, short_name);
264 if (showall || strcmp(res1, res2)) {
265 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
266 res1, res2, count, mask, file, long_name, short_name);
267 if (die_on_error) exit(1);
270 smbcli_unlink(cli->tree, file);
272 if (count % 100 == 0) DEBUG(0,("%d\n", count));
275 static void test_mask(int argc, char *argv[],
276 struct smbcli_state *cli)
280 int mc_len = strlen(maskchars);
281 int fc_len = strlen(filechars);
283 smbcli_mkdir(cli->tree, "\\masktest");
285 smbcli_unlink(cli->tree, "\\masktest\\*");
289 pstrcpy(mask,"\\masktest\\");
290 pstrcpy(file,"\\masktest\\");
291 pstrcat(mask, argv[0]);
292 pstrcat(file, argv[1]);
293 testpair(cli, mask, file);
301 l1 = 1 + random() % 20;
302 l2 = 1 + random() % 20;
303 pstrcpy(mask,"\\masktest\\");
304 pstrcpy(file,"\\masktest\\");
307 mask[i+l] = maskchars[random() % mc_len];
312 file[i+l] = filechars[random() % fc_len];
316 if (strcmp(file+l,".") == 0 ||
317 strcmp(file+l,"..") == 0 ||
318 strcmp(mask+l,"..") == 0) continue;
320 if (strspn(file+l, ".") == strlen(file+l)) continue;
322 testpair(cli, mask, file);
323 if (NumLoops && (--NumLoops == 0))
328 smbcli_rmdir(cli->tree, "\\masktest");
332 static void usage(void)
336 masktest //server/share [options..]\n\
344 -f filechars (default %s)\n\
345 -m maskchars (default %s)\n\
350 This program tests wildcard matching between two servers. It generates\n\
351 random pairs of filenames/masks and tests that they match in the same\n\
352 way on the servers and internally\n\
354 filechars, maskchars);
357 /****************************************************************************
359 ****************************************************************************/
360 int main(int argc,char *argv[])
363 struct smbcli_state *cli;
370 setup_logging("masktest", DEBUG_STDOUT);
372 lp_set_cmdline("log level", "0");
374 if (argc < 2 || argv[1][0] == '-') {
381 all_string_sub(share,"/","\\",0);
383 setup_logging(argv[0], DEBUG_STDOUT);
388 lp_load(dyn_CONFIGFILE,True,False,False);
391 if (getenv("USER")) {
392 fstrcpy(username,getenv("USER"));
397 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vE")) != EOF) {
400 NumLoops = atoi(optarg);
403 DEBUGLEVEL = atoi(optarg);
412 lp_set_cmdline("max protocol", optarg);
415 fstrcpy(username,optarg);
416 p = strchr_m(username,'%');
419 fstrcpy(password, p+1);
441 printf("Unknown option %c (%d)\n", (char)opt, opt);
450 cli = connect_one(share);
452 DEBUG(0,("Failed to connect to %s\n", share));
456 /* need to init seed after connect as clientgen uses random numbers */
457 DEBUG(0,("seed=%d format --- --- (server, correct)\n", seed));
460 test_mask(argc, argv, cli);