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"
22 #include "system/dir.h"
23 #include "libcli/libcli.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
27 #include "auth/credentials/credentials.h"
28 #include "auth/gensec/gensec.h"
29 #include "param/param.h"
30 #include "dynconfig.h"
31 #include "libcli/resolve/resolve.h"
33 static struct cli_credentials *credentials;
34 static bool showall = false;
35 static bool old_list = false;
36 static const char *maskchars = "<>\"?*abc.";
37 static const char *filechars = "abcdefghijklm.";
39 static int die_on_error;
40 static int NumLoops = 0;
41 static int max_length = 20;
42 struct masktest_state {
46 static bool reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
48 /* oh what a weird world this is */
49 if (old_list && strcmp(pattern, "*.*") == 0) return true;
51 if (ISDOT(pattern)) return false;
53 if (ISDOTDOT(file)) file = ".";
55 return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
58 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
63 pattern = 1+strrchr_m(pattern,'\\');
65 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
66 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
67 if (reg_match_one(cli, pattern, long_name) ||
68 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
73 /*****************************************************
74 return a connection to a server
75 *******************************************************/
76 static struct smbcli_state *connect_one(struct resolve_context *resolve_ctx,
77 char *share, const char **ports,
78 struct smbcli_options *options)
80 struct smbcli_state *c;
84 fstrcpy(server,share+2);
85 share = strchr_m(server,'\\');
86 if (!share) return NULL;
90 cli_credentials_set_workstation(credentials, "masktest", CRED_SPECIFIED);
92 status = smbcli_full_connection(NULL, &c,
96 credentials, resolve_ctx, NULL,
99 if (!NT_STATUS_IS_OK(status)) {
106 static char *resultp;
111 static bool f_info_hit;
113 static void listfn(struct clilist_file_info *f, const char *s, void *state)
115 struct masktest_state *m = talloc_get_type(state,struct masktest_state);
117 if (ISDOT(f->name)) {
119 } else if (ISDOTDOT(f->name)) {
125 last_hit.long_name = talloc_strdup(m->mem_ctx, f->name);
126 last_hit.short_name = talloc_strdup(m->mem_ctx, f->short_name);
130 static void get_real_name(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
131 char *long_name, fstring short_name)
134 struct masktest_state state;
136 if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
137 mask = "\\masktest\\*.*";
139 mask = "\\masktest\\*";
144 state.mem_ctx = mem_ctx;
146 smbcli_list_new(cli->tree, mask,
147 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
148 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
152 fstrcpy(short_name, last_hit.short_name);
153 strlower(short_name);
154 long_name = talloc_strdup(mem_ctx, last_hit.long_name);
158 if (*short_name == '\0') {
159 fstrcpy(short_name, long_name);
163 static void testpair(TALLOC_CTX *mem_ctx, struct smbcli_state *cli, char *mask,
172 struct masktest_state state;
176 fstrcpy(res1, "---");
178 state.mem_ctx = mem_ctx;
180 fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
182 DEBUG(0,("Can't create %s\n", file));
185 smbcli_close(cli->tree, fnum);
188 fstrcpy(short_name, "");
189 get_real_name(mem_ctx, cli, long_name, short_name);
190 fstrcpy(res1, "---");
191 smbcli_list_new(cli->tree, mask,
192 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
193 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
196 res2 = reg_test(cli, mask, long_name, short_name);
198 if (showall || strcmp(res1, res2)) {
199 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
200 res1, res2, count, mask, file, long_name, short_name);
201 if (die_on_error) exit(1);
204 smbcli_unlink(cli->tree, file);
206 if (count % 100 == 0) DEBUG(0,("%d\n", count));
211 static void test_mask(int argc, char *argv[],
212 struct smbcli_state *cli)
217 int mc_len = strlen(maskchars);
218 int fc_len = strlen(filechars);
220 mem_ctx = talloc_init("test_mask");
222 smbcli_mkdir(cli->tree, "\\masktest");
224 smbcli_unlink(cli->tree, "\\masktest\\*");
228 mask = talloc_strdup(mem_ctx, "\\masktest\\");
229 file = talloc_strdup(mem_ctx, "\\masktest\\");
230 mask = talloc_strdup_append(mask, argv[0]);
231 file = talloc_strdup_append(file, argv[1]);
232 testpair(mem_ctx, cli, mask, file);
240 l1 = 1 + random() % max_length;
241 l2 = 1 + random() % max_length;
242 mask = talloc_strdup(mem_ctx, "\\masktest\\");
243 file = talloc_strdup(mem_ctx, "\\masktest\\");
246 mask[i+l] = maskchars[random() % mc_len];
251 file[i+l] = filechars[random() % fc_len];
255 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
259 if (strspn(file+l, ".") == strlen(file+l)) continue;
261 testpair(mem_ctx, cli, mask, file);
262 if (NumLoops && (--NumLoops == 0))
267 smbcli_rmdir(cli->tree, "\\masktest");
268 talloc_free(mem_ctx);
272 static void usage(void)
276 masktest //server/share [options..]\n\
283 -l max test length\n\
285 -f filechars (default %s)\n\
286 -m maskchars (default %s)\n\
291 This program tests wildcard matching between two servers. It generates\n\
292 random pairs of filenames/masks and tests that they match in the same\n\
293 way on the servers and internally\n\
295 filechars, maskchars);
298 /****************************************************************************
300 ****************************************************************************/
301 int main(int argc,char *argv[])
304 struct smbcli_state *cli;
307 struct loadparm_context *lp_ctx;
308 struct smbcli_options options;
312 setup_logging("masktest", DEBUG_STDOUT);
314 if (argc < 2 || argv[1][0] == '-') {
321 all_string_sub(share,"/","\\",0);
323 setup_logging(argv[0], DEBUG_STDOUT);
328 lp_ctx = loadparm_init(talloc_autofree_context());
329 lp_load(lp_ctx, dyn_CONFIGFILE);
331 credentials = cli_credentials_init(talloc_autofree_context());
332 cli_credentials_guess(credentials, lp_ctx);
336 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
339 NumLoops = atoi(optarg);
342 DEBUGLEVEL = atoi(optarg);
351 lp_set_cmdline(lp_ctx, "max protocol", optarg);
354 cli_credentials_parse_string(credentials, optarg, CRED_SPECIFIED);
366 max_length = atoi(optarg);
378 printf("Unknown option %c (%d)\n", (char)opt, opt);
388 lp_smbcli_options(lp_ctx, &options);
390 cli = connect_one(lp_resolve_context(lp_ctx), share,
391 lp_smb_ports(lp_ctx), &options);
393 DEBUG(0,("Failed to connect to %s\n", share));
397 /* need to init seed after connect as clientgen uses random numbers */
398 DEBUG(0,("seed=%d format --- --- (server, correct)\n", seed));
401 test_mask(argc, argv, cli);