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"
25 #include "../libcli/smb/smbXcli_base.h"
26 #include "lib/util/string_wrappers.h"
28 static fstring password
;
29 static fstring username
;
31 static struct cli_credentials
*test_creds
;
32 static int max_protocol
= -1;
33 static bool showall
= False
;
34 static bool old_list
= False
;
35 static const char *maskchars
= "<>\"?*abc.";
36 static const char *filechars
= "abcdefghijklm.";
38 static int die_on_error
;
39 static int NumLoops
= 0;
40 static int ignore_dot_errors
= 0;
45 /* a test fn for LANMAN mask support */
46 static int ms_fnmatch_lanman_core(const char *pattern
, const char *string
)
48 const char *p
= pattern
, *n
= string
;
51 if (strcmp(p
,"?")==0 && strcmp(n
,".")==0) goto match
;
56 /* if (! *n && ! *p) goto match; */
57 if (*n
!= '.') goto nomatch
;
62 if ((*n
== '.' && n
[1] != '.') || ! *n
) goto next
;
68 if (! n
[1] && ms_fnmatch_lanman_core(p
, n
+1) == 0) goto match
;
69 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
79 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
85 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
86 if (*n
== '.' && !strchr_m(n
+1,'.')) {
94 if (*n
== 0 && ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
95 if (*n
!= '.') goto nomatch
;
100 if (c
!= *n
) goto nomatch
;
105 if (! *n
) goto match
;
108 if (verbose
) printf("NOMATCH pattern=[%s] string=[%s]\n", pattern
, string
);
112 if (ms_fnmatch_lanman_core(p
, n
) == 0) goto match
;
116 if (verbose
) printf("MATCH pattern=[%s] string=[%s]\n", pattern
, string
);
120 static int ms_fnmatch_lanman(const char *pattern
, const char *string
)
122 if (!strpbrk(pattern
, "?*<>\"")) {
123 if (strcmp(string
,"..") == 0)
126 return strcmp(pattern
, string
);
129 if (strcmp(string
,"..") == 0 || strcmp(string
,".") == 0) {
130 return ms_fnmatch_lanman_core(pattern
, "..") &&
131 ms_fnmatch_lanman_core(pattern
, ".");
134 return ms_fnmatch_lanman_core(pattern
, string
);
137 static bool reg_match_one(struct cli_state
*cli
, const char *pattern
, const char *file
)
139 /* oh what a weird world this is */
140 if (old_list
&& strcmp(pattern
, "*.*") == 0) return True
;
142 if (strcmp(pattern
,".") == 0) return False
;
144 if (max_protocol
<= PROTOCOL_LANMAN2
) {
145 return ms_fnmatch_lanman(pattern
, file
)==0;
148 if (strcmp(file
,"..") == 0) file
= ".";
150 return ms_fnmatch(pattern
, file
, smbXcli_conn_protocol(cli
->conn
), False
) == 0;
153 static char *reg_test(struct cli_state
*cli
, const char *pattern
, const char *long_name
, const char *short_name
)
156 const char *new_pattern
= 1+strrchr_m(pattern
,'\\');
159 if (reg_match_one(cli
, new_pattern
, ".")) ret
[0] = '+';
160 if (reg_match_one(cli
, new_pattern
, "..")) ret
[1] = '+';
161 if (reg_match_one(cli
, new_pattern
, long_name
) ||
162 (*short_name
&& reg_match_one(cli
, new_pattern
, short_name
))) ret
[2] = '+';
167 /*****************************************************
168 return a connection to a server
169 *******************************************************/
170 static struct cli_state
*connect_one(char *share
)
178 share
= strchr_m(server
,'\\');
179 if (!share
) return NULL
;
185 status
= cli_connect_nb(server
, NULL
, 0, 0x20, "masktest",
186 SMB_SIGNING_DEFAULT
, 0, &c
);
187 if (!NT_STATUS_IS_OK(status
)) {
188 DEBUG(0,("Connection to %s failed. Error %s\n", server_n
,
193 status
= smbXcli_negprot(c
->conn
, c
->timeout
, PROTOCOL_CORE
,
195 if (!NT_STATUS_IS_OK(status
)) {
196 DEBUG(0, ("protocol negotiation failed: %s\n",
202 status
= cli_session_setup_creds(c
, test_creds
);
203 if (!NT_STATUS_IS_OK(status
)) {
204 DEBUG(0, ("session setup failed: %s\n", nt_errstr(status
)));
209 * These next two lines are needed to emulate
210 * old client behaviour for people who have
211 * scripts based on client output.
212 * QUESTION ? Do we want to have a 'client compatibility
213 * mode to turn these on/off ? JRA.
216 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
217 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
218 c
->server_domain
,c
->server_os
,c
->server_type
));
220 DEBUG(4,(" session setup ok\n"));
222 status
= cli_tree_connect_creds(c
, share
, "?????", test_creds
);
223 if (!NT_STATUS_IS_OK(status
)) {
224 DEBUG(0,("tree connect failed: %s\n", nt_errstr(status
)));
229 DEBUG(4,(" tconx ok\n"));
234 static char *resultp
;
241 static NTSTATUS
listfn(
246 struct rn_state
*state
= (struct rn_state
*)private_data
;
247 if (strcmp(f
->name
,".") == 0) {
249 } else if (strcmp(f
->name
,"..") == 0) {
256 return NT_STATUS_INTERNAL_ERROR
;
259 if (ISDOT(f
->name
) || ISDOTDOT(f
->name
)) {
264 fstrcpy(state
->short_name
, f
->short_name
? f
->short_name
: "");
265 (void)strlower_m(state
->short_name
);
266 *state
->pp_long_name
= SMB_STRDUP(f
->name
);
267 if (!*state
->pp_long_name
) {
268 return NT_STATUS_NO_MEMORY
;
270 (void)strlower_m(*state
->pp_long_name
);
274 static void get_real_name(struct cli_state
*cli
,
275 char **pp_long_name
, fstring short_name
)
277 struct rn_state state
;
279 state
.pp_long_name
= pp_long_name
;
280 state
.short_name
= short_name
;
282 *pp_long_name
= NULL
;
283 /* nasty hack to force level 260 listings - tridge */
284 if (max_protocol
<= PROTOCOL_LANMAN1
) {
285 cli_list_trans(cli
, "\\masktest\\*.*", FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
,
286 SMB_FIND_FILE_BOTH_DIRECTORY_INFO
, listfn
,
289 cli_list_trans(cli
, "\\masktest\\*", FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
,
290 SMB_FIND_FILE_BOTH_DIRECTORY_INFO
,
294 if (*short_name
== 0) {
295 fstrcpy(short_name
, *pp_long_name
);
299 if (!strchr_m(short_name
,'.')) {
300 fstrcat(short_name
,".");
305 static void testpair(struct cli_state
*cli
, const char *mask
, const char *file
)
312 char *long_name
= NULL
;
316 fstrcpy(res1
, "---");
318 if (!NT_STATUS_IS_OK(cli_openx(cli
, file
, O_CREAT
|O_TRUNC
|O_RDWR
, 0, &fnum
))) {
319 DEBUG(0,("Can't create %s\n", file
));
322 cli_close(cli
, fnum
);
325 fstrcpy(short_name
, "");
326 get_real_name(cli
, &long_name
, short_name
);
330 fstrcpy(res1
, "---");
331 cli_list(cli
, mask
, FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
, listfn
, NULL
);
333 res2
= reg_test(cli
, mask
, long_name
, short_name
);
336 ((strcmp(res1
, res2
) && !ignore_dot_errors
) ||
337 (strcmp(res1
+2, res2
+2) && ignore_dot_errors
))) {
338 DEBUG(0,("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
339 res1
, res2
, count
, mask
, file
, long_name
, short_name
));
340 if (die_on_error
) exit(1);
343 cli_unlink(cli
, file
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
345 if (count
% 100 == 0) DEBUG(0,("%d\n", count
));
346 SAFE_FREE(long_name
);
349 static void test_mask(int argc
, char *argv
[],
350 struct cli_state
*cli
)
354 int mc_len
= strlen(maskchars
);
355 int fc_len
= strlen(filechars
);
356 TALLOC_CTX
*ctx
= talloc_tos();
358 torture_deltree(cli
, "\\masktest");
359 cli_mkdir(cli
, "\\masktest");
363 mask
= talloc_asprintf(ctx
,
366 file
= talloc_asprintf(ctx
,
369 if (!mask
|| !file
) {
372 testpair(cli
, mask
, file
);
380 l1
= 1 + random() % 20;
381 l2
= 1 + random() % 20;
382 mask
= talloc_array(ctx
, char, strlen("\\masktest\\")+1+22);
383 file
= talloc_array(ctx
, char, strlen("\\masktest\\")+1+22);
384 if (!mask
|| !file
) {
387 memcpy(mask
,"\\masktest\\",strlen("\\masktest\\")+1);
388 memcpy(file
,"\\masktest\\",strlen("\\masktest\\")+1);
391 mask
[i
+l
] = maskchars
[random() % mc_len
];
396 file
[i
+l
] = filechars
[random() % fc_len
];
400 if (strcmp(file
+l
,".") == 0 ||
401 strcmp(file
+l
,"..") == 0 ||
402 strcmp(mask
+l
,"..") == 0) continue;
404 if (strspn(file
+l
, ".") == strlen(file
+l
)) continue;
406 testpair(cli
, mask
, file
);
407 if (NumLoops
&& (--NumLoops
== 0))
414 cli_rmdir(cli
, "\\masktest");
418 static void usage(void)
422 masktest //server/share [options..]\n\
430 -f filechars (default %s)\n\
431 -m maskchars (default %s)\n\
435 -i ignore . and .. errors\n\
437 This program tests wildcard matching between two servers. It generates\n\
438 random pairs of filenames/masks and tests that they match in the same\n\
439 way on the servers and internally\n\
441 filechars
, maskchars
);
444 /****************************************************************************
446 ****************************************************************************/
447 int main(int argc
,char *argv
[])
450 struct cli_state
*cli
;
454 TALLOC_CTX
*frame
= talloc_stackframe();
458 lp_set_cmdline("log level", "0");
460 if (argc
< 2 || argv
[1][0] == '-') {
467 all_string_sub(share
,"/","\\",0);
469 setup_logging(argv
[0], DEBUG_STDERR
);
475 lp_load_global(get_dyn_CONFIGFILE());
478 if (getenv("USER")) {
479 fstrcpy(username
,getenv("USER"));
484 while ((opt
= getopt(argc
, argv
, "n:d:U:s:hm:f:aoW:M:vEi")) != EOF
) {
487 NumLoops
= atoi(optarg
);
490 lp_set_cmdline("log level", optarg
);
496 ignore_dot_errors
= 1;
502 lp_set_cmdline("client max protocol", optarg
);
505 fstrcpy(username
,optarg
);
506 p
= strchr_m(username
,'%');
509 fstrcpy(password
, p
+1);
532 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
540 max_protocol
= lp_client_max_protocol();
541 max_protocol
= MIN(max_protocol
, PROTOCOL_NT1
);
547 rc
= samba_getpass("Password: ", pwd
, sizeof(pwd
), false, false);
549 fstrcpy(password
, pass
);
553 test_creds
= cli_session_creds_init(frame
,
558 false, /* use_kerberos */
559 false, /* fallback_after_kerberos */
560 false, /* use_ccache */
561 false); /* password_is_nt_hash */
562 if (test_creds
== NULL
) {
563 d_printf("cli_session_creds_init() failed.\n");
567 cli
= connect_one(share
);
569 DEBUG(0,("Failed to connect to %s\n", share
));
573 /* need to init seed after connect as clientgen uses random numbers */
574 DEBUG(0,("seed=%d\n", seed
));
577 test_mask(argc
, argv
, cli
);