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.
25 static fstring password;
26 static fstring username;
28 static int max_protocol = PROTOCOL_NT1;
29 static BOOL showall = False;
30 static BOOL old_list = False;
31 static char *maskchars = "<>\"?*abc.";
32 static char *filechars = "abcdefghijklm.";
34 static int die_on_error;
36 /* a test fn for LANMAN mask support */
37 int ms_fnmatch_lanman_core(char *pattern, char *string)
39 char *p = pattern, *n = string;
42 if (strcmp(p,"?")==0 && strcmp(n,".")==0) goto match;
47 /* if (! *n && ! *p) goto match; */
48 if (*n != '.') goto nomatch;
53 if ((*n == '.' && n[1] != '.') || ! *n) goto next;
59 if (! n[1] && ms_fnmatch_lanman_core(p, n+1) == 0) goto match;
60 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
70 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
76 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
77 if (*n == '.' && !strchr_m(n+1,'.')) {
85 if (*n == 0 && ms_fnmatch_lanman_core(p, n) == 0) goto match;
86 if (*n != '.') goto nomatch;
91 if (c != *n) goto nomatch;
99 if (verbose) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern, string);
103 if (ms_fnmatch_lanman_core(p, n) == 0) goto match;
107 if (verbose) printf("MATCH pattern=[%s] string=[%s]\n", pattern, string);
111 int ms_fnmatch_lanman(char *pattern, char *string)
113 if (!strpbrk(pattern, "?*<>\"")) {
114 if (strcmp(string,"..") == 0) string = ".";
115 return strcmp(pattern, string);
118 if (strcmp(string,"..") == 0 || strcmp(string,".") == 0) {
119 return ms_fnmatch_lanman_core(pattern, "..") &&
120 ms_fnmatch_lanman_core(pattern, ".");
123 return ms_fnmatch_lanman_core(pattern, string);
126 static BOOL reg_match_one(struct cli_state *cli, char *pattern, char *file)
128 /* oh what a weird world this is */
129 if (old_list && strcmp(pattern, "*.*") == 0) return True;
131 if (strcmp(pattern,".") == 0) return False;
133 if (max_protocol <= PROTOCOL_LANMAN2) {
134 return ms_fnmatch_lanman(pattern, file)==0;
137 if (strcmp(file,"..") == 0) file = ".";
139 return ms_fnmatch(pattern, file, cli->protocol)==0;
142 static char *reg_test(struct cli_state *cli, char *pattern, char *long_name, char *short_name)
147 pattern = 1+strrchr_m(pattern,'\\');
149 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
150 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
151 if (reg_match_one(cli, pattern, long_name) ||
152 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
157 /*****************************************************
158 return a connection to a server
159 *******************************************************/
160 struct cli_state *connect_one(char *share)
163 struct nmb_name called, calling;
169 share = strchr_m(server,'\\');
170 if (!share) return NULL;
178 make_nmb_name(&calling, "masktest", 0x0);
179 make_nmb_name(&called , server, 0x20);
184 /* have to open a new connection */
185 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
186 DEBUG(0,("Connection to %s failed\n", server_n));
190 c->protocol = max_protocol;
192 if (!cli_session_request(c, &calling, &called)) {
193 DEBUG(0,("session request to %s failed\n", called.name));
195 if (strcmp(called.name, "*SMBSERVER")) {
196 make_nmb_name(&called , "*SMBSERVER", 0x20);
202 DEBUG(4,(" session request ok\n"));
204 if (!cli_negprot(c)) {
205 DEBUG(0,("protocol negotiation failed\n"));
211 char *pass = getpass("Password: ");
213 pstrcpy(password, pass);
217 if (!cli_session_setup(c, username,
218 password, strlen(password),
219 password, strlen(password),
221 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
226 * These next two lines are needed to emulate
227 * old client behaviour for people who have
228 * scripts based on client output.
229 * QUESTION ? Do we want to have a 'client compatibility
230 * mode to turn these on/off ? JRA.
233 if (*c->server_domain || *c->server_os || *c->server_type)
234 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
235 c->server_domain,c->server_os,c->server_type));
237 DEBUG(4,(" session setup ok\n"));
239 if (!cli_send_tconX(c, share, "?????",
240 password, strlen(password)+1)) {
241 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
246 DEBUG(4,(" tconx ok\n"));
251 static char *resultp;
252 static file_info *f_info;
254 void listfn(file_info *f, const char *s, void *state)
256 if (strcmp(f->name,".") == 0) {
258 } else if (strcmp(f->name,"..") == 0) {
266 static void get_real_name(struct cli_state *cli,
267 pstring long_name, fstring short_name)
269 /* nasty hack to force level 260 listings - tridge */
270 cli->capabilities |= CAP_NT_SMBS;
271 if (max_protocol <= PROTOCOL_LANMAN1) {
272 cli_list_new(cli, "\\masktest\\*.*", aHIDDEN | aDIR, listfn, NULL);
274 cli_list_new(cli, "\\masktest\\*", aHIDDEN | aDIR, listfn, NULL);
277 fstrcpy(short_name, f_info->short_name);
278 strlower(short_name);
279 pstrcpy(long_name, f_info->name);
283 if (*short_name == 0) {
284 fstrcpy(short_name, long_name);
288 if (!strchr_m(short_name,'.')) {
289 fstrcat(short_name,".");
294 static void testpair(struct cli_state *cli, char *mask, char *file)
305 fstrcpy(res1, "---");
307 fnum = cli_open(cli, file, O_CREAT|O_TRUNC|O_RDWR, 0);
309 DEBUG(0,("Can't create %s\n", file));
312 cli_close(cli, fnum);
315 fstrcpy(short_name, "");
317 get_real_name(cli, long_name, short_name);
319 fstrcpy(res1, "---");
320 cli_list(cli, mask, aHIDDEN | aDIR, listfn, NULL);
322 res2 = reg_test(cli, mask, long_name, short_name);
324 if (showall || strcmp(res1, res2)) {
325 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
326 res1, res2, count, mask, file, long_name, short_name));
327 if (die_on_error) exit(1);
330 cli_unlink(cli, file);
332 if (count % 100 == 0) DEBUG(0,("%d\n", count));
335 static void test_mask(int argc, char *argv[],
336 struct cli_state *cli)
340 int mc_len = strlen(maskchars);
341 int fc_len = strlen(filechars);
343 cli_mkdir(cli, "\\masktest");
345 cli_unlink(cli, "\\masktest\\*");
349 pstrcpy(mask,"\\masktest\\");
350 pstrcpy(file,"\\masktest\\");
351 pstrcat(mask, argv[0]);
352 pstrcat(file, argv[1]);
353 testpair(cli, mask, file);
361 l1 = 1 + random() % 20;
362 l2 = 1 + random() % 20;
363 pstrcpy(mask,"\\masktest\\");
364 pstrcpy(file,"\\masktest\\");
367 mask[i+l] = maskchars[random() % mc_len];
372 file[i+l] = filechars[random() % fc_len];
376 if (strcmp(file+l,".") == 0 ||
377 strcmp(file+l,"..") == 0 ||
378 strcmp(mask+l,"..") == 0) continue;
380 if (strspn(file+l, ".") == strlen(file+l)) continue;
382 testpair(cli, mask, file);
386 cli_rmdir(cli, "\\masktest");
390 static void usage(void)
394 masktest //server/share [options..]\n\
401 -f filechars (default %s)\n\
402 -m maskchars (default %s)\n\
407 This program tests wildcard matching between two servers. It generates\n\
408 random pairs of filenames/masks and tests that they match in the same\n\
409 way on the servers and internally\n\
411 filechars, maskchars);
414 /****************************************************************************
416 ****************************************************************************/
417 int main(int argc,char *argv[])
420 struct cli_state *cli;
423 extern BOOL AllowDebugChange;
433 AllowDebugChange = False;
435 if (argc < 2 || argv[1][0] == '-') {
442 all_string_sub(share,"/","\\",0);
444 setup_logging(argv[0],True);
449 lp_load(dyn_CONFIGFILE,True,False,False);
452 if (getenv("USER")) {
453 pstrcpy(username,getenv("USER"));
458 while ((opt = getopt(argc, argv, "d:U:s:hm:f:aoW:M:vE")) != EOF) {
461 DEBUGLEVEL = atoi(optarg);
470 max_protocol = interpret_protocol(optarg, max_protocol);
473 pstrcpy(username,optarg);
474 p = strchr_m(username,'%');
477 pstrcpy(password, p+1);
500 printf("Unknown option %c (%d)\n", (char)opt, opt);
509 cli = connect_one(share);
511 DEBUG(0,("Failed to connect to %s\n", share));
515 /* need to init seed after connect as clientgen uses random numbers */
516 DEBUG(0,("seed=%d\n", seed));
519 test_mask(argc, argv, cli);