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 "lib/cmdline/popt_common.h"
22 #include "system/filesys.h"
23 #include "system/dir.h"
24 #include "libcli/libcli.h"
25 #include "system/time.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/gensec/gensec.h"
28 #include "param/param.h"
29 #include "libcli/resolve/resolve.h"
30 #include "lib/events/events.h"
32 static bool showall
= false;
33 static bool old_list
= false;
34 static const char *maskchars
= "<>\"?*abc.";
35 static const char *filechars
= "abcdefghijklm.";
36 static int die_on_error
;
37 static int NumLoops
= 0;
38 static int max_length
= 20;
39 struct masktest_state
{
43 static bool reg_match_one(struct smbcli_state
*cli
, const char *pattern
, const char *file
)
45 /* oh what a weird world this is */
46 if (old_list
&& strcmp(pattern
, "*.*") == 0) return true;
48 if (ISDOT(pattern
)) return false;
50 if (ISDOTDOT(file
)) file
= ".";
52 return ms_fnmatch(pattern
, file
, cli
->transport
->negotiate
.protocol
)==0;
55 static char *reg_test(struct smbcli_state
*cli
, TALLOC_CTX
*mem_ctx
, const char *pattern
, const char *long_name
, const char *short_name
)
58 ret
= talloc_strdup(mem_ctx
, "---");
60 pattern
= 1+strrchr_m(pattern
,'\\');
62 if (reg_match_one(cli
, pattern
, ".")) ret
[0] = '+';
63 if (reg_match_one(cli
, pattern
, "..")) ret
[1] = '+';
64 if (reg_match_one(cli
, pattern
, long_name
) ||
65 (*short_name
&& reg_match_one(cli
, pattern
, short_name
))) ret
[2] = '+';
70 /*****************************************************
71 return a connection to a server
72 *******************************************************/
73 static struct smbcli_state
*connect_one(struct resolve_context
*resolve_ctx
,
74 struct tevent_context
*ev
,
76 char *share
, const char **ports
,
77 const char *socket_options
,
78 struct smbcli_options
*options
,
79 struct smbcli_session_options
*session_options
,
80 struct smb_iconv_convenience
*iconv_convenience
,
81 struct gensec_settings
*gensec_settings
)
83 struct smbcli_state
*c
;
87 server
= talloc_strdup(mem_ctx
, share
+2);
88 share
= strchr_m(server
,'\\');
89 if (!share
) return NULL
;
93 cli_credentials_set_workstation(cmdline_credentials
, "masktest", CRED_SPECIFIED
);
95 status
= smbcli_full_connection(NULL
, &c
,
100 cmdline_credentials
, resolve_ctx
, ev
,
101 options
, session_options
,
105 if (!NT_STATUS_IS_OK(status
)) {
112 static char *resultp
;
117 static bool f_info_hit
;
119 static void listfn(struct clilist_file_info
*f
, const char *s
, void *state
)
121 struct masktest_state
*m
= (struct masktest_state
*)state
;
123 if (ISDOT(f
->name
)) {
125 } else if (ISDOTDOT(f
->name
)) {
131 last_hit
.long_name
= talloc_strdup(m
->mem_ctx
, f
->name
);
132 last_hit
.short_name
= talloc_strdup(m
->mem_ctx
, f
->short_name
);
136 static void get_real_name(TALLOC_CTX
*mem_ctx
, struct smbcli_state
*cli
,
137 char **long_name
, char **short_name
)
140 struct masktest_state state
;
142 if (cli
->transport
->negotiate
.protocol
<= PROTOCOL_LANMAN1
) {
143 mask
= "\\masktest\\*.*";
145 mask
= "\\masktest\\*";
150 state
.mem_ctx
= mem_ctx
;
152 smbcli_list_new(cli
->tree
, mask
,
153 FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
,
154 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO
,
158 *short_name
= talloc_strdup(mem_ctx
, last_hit
.short_name
);
159 strlower(*short_name
);
160 *long_name
= talloc_strdup(mem_ctx
, last_hit
.long_name
);
161 strlower(*long_name
);
164 if (*short_name
== '\0') {
165 *short_name
= talloc_strdup(mem_ctx
, *long_name
);
169 static void testpair(TALLOC_CTX
*mem_ctx
, struct smbcli_state
*cli
, char *mask
,
176 char *short_name
= NULL
;
177 char *long_name
= NULL
;
178 struct masktest_state state
;
182 safe_strcpy(res1
, "---", sizeof(res1
));
184 state
.mem_ctx
= mem_ctx
;
186 fnum
= smbcli_open(cli
->tree
, file
, O_CREAT
|O_TRUNC
|O_RDWR
, 0);
188 DEBUG(0,("Can't create %s\n", file
));
191 smbcli_close(cli
->tree
, fnum
);
194 short_name
= talloc_strdup(mem_ctx
, "");
195 get_real_name(mem_ctx
, cli
, &long_name
, &short_name
);
196 safe_strcpy(res1
, "---", sizeof(res1
));
197 smbcli_list_new(cli
->tree
, mask
,
198 FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
,
199 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO
,
202 res2
= reg_test(cli
, mem_ctx
, mask
, long_name
, short_name
);
204 if (showall
|| strcmp(res1
, res2
)) {
205 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
206 res1
, res2
, count
, mask
, file
, long_name
, short_name
);
207 if (die_on_error
) exit(1);
210 smbcli_unlink(cli
->tree
, file
);
212 if (count
% 100 == 0) DEBUG(0,("%d\n", count
));
217 static void test_mask(int argc
, char *argv
[],
219 struct smbcli_state
*cli
)
223 int mc_len
= strlen(maskchars
);
224 int fc_len
= strlen(filechars
);
226 smbcli_mkdir(cli
->tree
, "\\masktest");
228 smbcli_unlink(cli
->tree
, "\\masktest\\*");
232 mask
= talloc_strdup(mem_ctx
, "\\masktest\\");
233 file
= talloc_strdup(mem_ctx
, "\\masktest\\");
234 mask
= talloc_strdup_append(mask
, argv
[0]);
235 file
= talloc_strdup_append(file
, argv
[1]);
236 testpair(mem_ctx
, cli
, mask
, file
);
244 l1
= 1 + random() % max_length
;
245 l2
= 1 + random() % max_length
;
246 mask
= talloc_strdup(mem_ctx
, "\\masktest\\");
247 file
= talloc_strdup(mem_ctx
, "\\masktest\\");
248 mask
= talloc_realloc_size(mem_ctx
, mask
, strlen(mask
)+l1
+1);
249 file
= talloc_realloc_size(mem_ctx
, file
, strlen(file
)+l2
+1);
252 mask
[i
+l
] = maskchars
[random() % mc_len
];
257 file
[i
+l
] = filechars
[random() % fc_len
];
261 if (ISDOT(file
+l
) || ISDOTDOT(file
+l
) || ISDOTDOT(mask
+l
)) {
265 if (strspn(file
+l
, ".") == strlen(file
+l
)) continue;
267 testpair(mem_ctx
, cli
, mask
, file
);
268 if (NumLoops
&& (--NumLoops
== 0))
273 smbcli_rmdir(cli
->tree
, "\\masktest");
274 talloc_free(mem_ctx
);
278 static void usage(poptContext pc
)
282 masktest //server/share [options..]\n\
284 This program tests wildcard matching between two servers. It generates\n\
285 random pairs of filenames/masks and tests that they match in the same\n\
286 way on the servers and internally\n");
287 poptPrintUsage(pc
, stdout
, 0);
290 /****************************************************************************
292 ****************************************************************************/
293 int main(int argc
,char *argv
[])
296 struct smbcli_state
*cli
;
299 struct tevent_context
*ev
;
300 struct loadparm_context
*lp_ctx
;
301 struct smbcli_options options
;
302 struct smbcli_session_options session_options
;
307 enum {OPT_UNCLIST
=1000};
308 struct poptOption long_options
[] = {
310 {"seed", 0, POPT_ARG_INT
, &seed
, 0, "Seed to use for randomizer", NULL
},
311 {"num-ops", 0, POPT_ARG_INT
, &NumLoops
, 0, "num ops", NULL
},
312 {"maxlength", 0, POPT_ARG_INT
, &max_length
,0, "maximum length", NULL
},
313 {"dieonerror", 0, POPT_ARG_NONE
, &die_on_error
, 0, "die on errors", NULL
},
314 {"showall", 0, POPT_ARG_NONE
, &showall
, 0, "display all operations", NULL
},
315 {"oldlist", 0, POPT_ARG_NONE
, &old_list
, 0, "use old list call", NULL
},
316 {"maskchars", 0, POPT_ARG_STRING
, &maskchars
, 0,"mask characters", NULL
},
317 {"filechars", 0, POPT_ARG_STRING
, &filechars
, 0,"file characters", NULL
},
319 POPT_COMMON_CONNECTION
320 POPT_COMMON_CREDENTIALS
328 pc
= poptGetContext("locktest", argc
, (const char **) argv
, long_options
,
329 POPT_CONTEXT_KEEP_FIRST
);
331 poptSetOtherOptionHelp(pc
, "<unc>");
333 while((opt
= poptGetNextOpt(pc
)) != -1) {
336 lp_set_cmdline(cmdline_lp_ctx
, "torture:unclist", poptGetOptArg(pc
));
341 argv_new
= discard_const_p(char *, poptGetArgs(pc
));
343 for (i
=0; i
<argc
; i
++) {
344 if (argv_new
[i
] == NULL
) {
350 if (!(argc_new
>= 2)) {
355 setup_logging("masktest", DEBUG_STDOUT
);
359 all_string_sub(share
,"/","\\",0);
361 lp_ctx
= cmdline_lp_ctx
;
363 mem_ctx
= talloc_autofree_context();
365 ev
= s4_event_context_init(mem_ctx
);
369 lp_smbcli_options(lp_ctx
, &options
);
370 lp_smbcli_session_options(lp_ctx
, &session_options
);
372 cli
= connect_one(lp_resolve_context(lp_ctx
), ev
, mem_ctx
, share
,
373 lp_smb_ports(lp_ctx
), lp_socket_options(lp_ctx
),
374 &options
, &session_options
,
375 lp_iconv_convenience(lp_ctx
),
376 lp_gensec_settings(mem_ctx
, lp_ctx
));
378 DEBUG(0,("Failed to connect to %s\n", share
));
382 /* need to init seed after connect as clientgen uses random numbers */
383 DEBUG(0,("seed=%d format --- --- (server, correct)\n", seed
));
386 test_mask(argc_new
-1, argv_new
+1, mem_ctx
, cli
);