2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1999
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 char *maskchars
= "<>\"?*abc.";
33 static char *filechars
= "abcdefghijklm.";
35 static int die_on_error
;
36 static int NumLoops
= 0;
38 /* a test fn for LANMAN mask support */
39 int ms_fnmatch_lanman_core(char *pattern
, char *string
)
41 char *p
= pattern
, *n
= string
;
44 if (strcmp(p
,"?")==0 && strcmp(n
,".")==0) goto match
;
49 /* if (! *n && ! *p) goto match; */
50 if (*n
!= '.') goto nomatch
;
55 if ((*n
== '.' && n
[1] != '.') || ! *n
) goto next
;
61 if (! n
[1] && ms_fnmatch_lanman_core(p
, n
+1) == 0) goto match
;
62 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
72 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
78 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
79 if (*n
== '.' && !strchr(n
+1,'.')) {
87 if (*n
== 0 && ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
88 if (*n
!= '.') goto nomatch
;
93 if (c
!= *n
) goto nomatch
;
101 if (verbose
) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern
, string
);
105 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
109 if (verbose
) printf("MATCH pattern=[%s] string=[%s]\n", pattern
, string
);
113 int ms_fnmatch_lanman(char *pattern
, char *string
)
115 if (!strpbrk(pattern
, "?*<>\"")) {
116 if (strcmp(string
,"..") == 0) string
= ".";
117 return strcmp(pattern
, string
);
120 if (strcmp(string
,"..") == 0 || strcmp(string
,".") == 0) {
121 return ms_fnmatch_lanman_core(pattern
, "..") &&
122 ms_fnmatch_lanman_core(pattern
, ".");
125 return ms_fnmatch_lanman_core(pattern
, string
);
128 static BOOL
reg_match_one(char *pattern
, char *file
)
130 /* oh what a weird world this is */
131 if (old_list
&& strcmp(pattern
, "*.*") == 0) return True
;
133 if (strcmp(pattern
,".") == 0) return False
;
135 if (max_protocol
<= PROTOCOL_LANMAN2
) {
136 return ms_fnmatch_lanman(pattern
, file
)==0;
139 if (strcmp(file
,"..") == 0) file
= ".";
141 return ms_fnmatch(pattern
, file
)==0;
144 static char *reg_test(char *pattern
, char *long_name
, char *short_name
)
149 pattern
= 1+strrchr(pattern
,'\\');
151 if (reg_match_one(pattern
, ".")) ret
[0] = '+';
152 if (reg_match_one(pattern
, "..")) ret
[1] = '+';
153 if (reg_match_one(pattern
, long_name
) ||
154 (*short_name
&& reg_match_one(pattern
, short_name
))) ret
[2] = '+';
159 /*****************************************************
160 return a connection to a server
161 *******************************************************/
162 struct cli_state
*connect_one(char *share
)
165 struct nmb_name called
, calling
;
171 share
= strchr(server
,'\\');
172 if (!share
) return NULL
;
180 make_nmb_name(&calling
, "masktest", 0x0);
181 make_nmb_name(&called
, server
, 0x20);
186 /* have to open a new connection */
187 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, 139) == 0) ||
188 !cli_connect(c
, server_n
, &ip
)) {
189 DEBUG(0,("Connection to %s failed\n", server_n
));
193 c
->protocol
= max_protocol
;
195 if (!cli_session_request(c
, &calling
, &called
)) {
196 DEBUG(0,("session request to %s failed\n", called
.name
));
198 if (strcmp(called
.name
, "*SMBSERVER")) {
199 make_nmb_name(&called
, "*SMBSERVER", 0x20);
205 DEBUG(4,(" session request ok\n"));
207 if (!cli_negprot(c
)) {
208 DEBUG(0,("protocol negotiation failed\n"));
214 char *pass
= getpass("Password: ");
216 pstrcpy(password
, pass
);
220 if (!cli_session_setup(c
, username
,
221 password
, strlen(password
),
222 password
, strlen(password
),
224 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
229 * These next two lines are needed to emulate
230 * old client behaviour for people who have
231 * scripts based on client output.
232 * QUESTION ? Do we want to have a 'client compatibility
233 * mode to turn these on/off ? JRA.
236 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
237 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
238 c
->server_domain
,c
->server_os
,c
->server_type
));
240 DEBUG(4,(" session setup ok\n"));
242 if (!cli_send_tconX(c
, share
, "?????",
243 password
, strlen(password
)+1)) {
244 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
249 DEBUG(4,(" tconx ok\n"));
254 static char *resultp
;
255 static file_info
*finfo
;
257 void listfn(file_info
*f
, const char *s
, void *state
)
259 if (strcmp(f
->name
,".") == 0) {
261 } else if (strcmp(f
->name
,"..") == 0) {
269 static void get_real_name(struct cli_state
*cli
,
270 pstring long_name
, fstring short_name
)
272 /* nasty hack to force level 260 listings - tridge */
273 cli
->capabilities
|= CAP_NT_SMBS
;
274 if (max_protocol
<= PROTOCOL_LANMAN1
) {
275 cli_list_new(cli
, "\\masktest\\*.*", aHIDDEN
| aDIR
, listfn
, NULL
);
277 cli_list_new(cli
, "\\masktest\\*", aHIDDEN
| aDIR
, listfn
, NULL
);
280 fstrcpy(short_name
, finfo
->short_name
);
281 strlower(short_name
);
282 pstrcpy(long_name
, finfo
->name
);
286 if (*short_name
== 0) {
287 fstrcpy(short_name
, long_name
);
291 if (!strchr(short_name
,'.')) {
292 fstrcat(short_name
,".");
297 static void testpair(struct cli_state
*cli
, char *mask
, char *file
)
308 fstrcpy(res1
, "---");
310 fnum
= cli_open(cli
, file
, O_CREAT
|O_TRUNC
|O_RDWR
, 0);
312 DEBUG(0,("Can't create %s\n", file
));
315 cli_close(cli
, fnum
);
318 fstrcpy(short_name
, "");
320 get_real_name(cli
, long_name
, short_name
);
322 fstrcpy(res1
, "---");
323 cli_list(cli
, mask
, aHIDDEN
| aDIR
, listfn
, NULL
);
325 res2
= reg_test(mask
, long_name
, short_name
);
327 if (showall
|| strcmp(res1
, res2
)) {
328 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
329 res1
, res2
, count
, mask
, file
, long_name
, short_name
));
330 if (die_on_error
) exit(1);
333 cli_unlink(cli
, file
);
335 if (count
% 100 == 0) DEBUG(0,("%d\n", count
));
338 static void test_mask(int argc
, char *argv
[],
339 struct cli_state
*cli
)
343 int mc_len
= strlen(maskchars
);
344 int fc_len
= strlen(filechars
);
346 cli_mkdir(cli
, "\\masktest");
348 cli_unlink(cli
, "\\masktest\\*");
352 pstrcpy(mask
,"\\masktest\\");
353 pstrcpy(file
,"\\masktest\\");
354 pstrcat(mask
, argv
[0]);
355 pstrcat(file
, argv
[1]);
356 testpair(cli
, mask
, file
);
364 l1
= 1 + random() % 20;
365 l2
= 1 + random() % 20;
366 pstrcpy(mask
,"\\masktest\\");
367 pstrcpy(file
,"\\masktest\\");
370 mask
[i
+l
] = maskchars
[random() % mc_len
];
375 file
[i
+l
] = filechars
[random() % fc_len
];
379 if (strcmp(file
+l
,".") == 0 ||
380 strcmp(file
+l
,"..") == 0 ||
381 strcmp(mask
+l
,"..") == 0) continue;
383 if (strspn(file
+l
, ".") == strlen(file
+l
)) continue;
385 testpair(cli
, mask
, file
);
386 if (NumLoops
&& (--NumLoops
== 0))
391 cli_rmdir(cli
, "\\masktest");
395 static void usage(void)
399 masktest //server/share [options..]\n\
407 -f filechars (default %s)\n\
408 -m maskchars (default %s)\n\
413 This program tests wildcard matching between two servers. It generates\n\
414 random pairs of filenames/masks and tests that they match in the same\n\
415 way on the servers and internally\n\
417 filechars
, maskchars
);
420 /****************************************************************************
422 ****************************************************************************/
423 int main(int argc
,char *argv
[])
426 struct cli_state
*cli
;
430 extern BOOL AllowDebugChange
;
434 static pstring servicesf
= CONFIGFILE
;
439 AllowDebugChange
= False
;
443 if (argc
< 2 || argv
[1][0] == '-') {
450 all_string_sub(share
,"/","\\",0);
452 setup_logging(argv
[0],True
);
458 charset_initialise();
460 if (getenv("USER")) {
461 pstrcpy(username
,getenv("USER"));
466 while ((opt
= getopt(argc
, argv
, "n:d:U:s:hm:f:aoW:M:vE")) != EOF
) {
469 NumLoops
= atoi(optarg
);
472 DEBUGLEVEL
= atoi(optarg
);
481 max_protocol
= interpret_protocol(optarg
, max_protocol
);
484 pstrcpy(username
,optarg
);
485 p
= strchr(username
,'%');
488 pstrcpy(password
, p
+1);
511 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
519 lp_load(servicesf
,True
,False
,False
);
521 codepage_initialise(lp_client_code_page());
523 cli
= connect_one(share
);
525 DEBUG(0,("Failed to connect to %s\n", share
));
528 Protocol
= cli
->protocol
;
530 /* need to init seed after connect as clientgen uses random numbers */
531 DEBUG(0,("seed=%d\n", seed
));
534 test_mask(argc
, argv
, cli
);