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"
32 #define MAX_STRING_LEN 1024
34 static struct cli_credentials *credentials;
35 static bool showall = false;
36 static bool old_list = false;
37 static const char *maskchars = "<>\"?*abc.";
38 static const char *filechars = "abcdefghijklm.";
40 static int die_on_error;
41 static int NumLoops = 0;
42 static int max_length = 20;
44 static bool reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
46 /* oh what a weird world this is */
47 if (old_list && strcmp(pattern, "*.*") == 0) return true;
49 if (ISDOT(pattern)) return false;
51 if (ISDOTDOT(file)) file = ".";
53 return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
56 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
61 pattern = 1+strrchr_m(pattern,'\\');
63 if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
64 if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
65 if (reg_match_one(cli, pattern, long_name) ||
66 (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
71 /*****************************************************
72 return a connection to a server
73 *******************************************************/
74 static struct smbcli_state *connect_one(char *share, const char **ports)
76 struct smbcli_state *c;
80 fstrcpy(server,share+2);
81 share = strchr_m(server,'\\');
82 if (!share) return NULL;
86 cli_credentials_set_workstation(credentials, "masktest", CRED_SPECIFIED);
88 status = smbcli_full_connection(NULL, &c,
94 if (!NT_STATUS_IS_OK(status)) {
101 static char *resultp;
106 static bool f_info_hit;
108 static void listfn(struct clilist_file_info *f, const char *s, void *state)
110 if (ISDOT(f->name)) {
112 } else if (ISDOTDOT(f->name)) {
117 safe_strcpy(last_hit.long_name, f->name, MAX_STRING_LEN);
118 safe_strcpy(last_hit.short_name, f->short_name, MAX_STRING_LEN);
122 static void get_real_name(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
123 char *long_name, fstring short_name)
126 if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
127 mask = "\\masktest\\*.*";
129 mask = "\\masktest\\*";
133 last_hit.short_name = talloc_array(mem_ctx, char, MAX_STRING_LEN);
134 last_hit.long_name = talloc_array(mem_ctx, char, MAX_STRING_LEN);
136 smbcli_list_new(cli->tree, mask,
137 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
138 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
142 fstrcpy(short_name, last_hit.short_name);
143 strlower(short_name);
144 safe_strcpy(long_name, last_hit.long_name, MAX_STRING_LEN);
148 if (*short_name == 0) {
149 fstrcpy(short_name, long_name);
153 static void testpair(TALLOC_CTX *mem_ctx, struct smbcli_state *cli, char *mask,
163 long_name = talloc_array(mem_ctx, char, MAX_STRING_LEN);
166 fstrcpy(res1, "---");
168 fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
170 DEBUG(0,("Can't create %s\n", file));
173 smbcli_close(cli->tree, fnum);
176 fstrcpy(short_name, "");
177 get_real_name(mem_ctx, cli, long_name, short_name);
178 fstrcpy(res1, "---");
179 smbcli_list_new(cli->tree, mask,
180 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
181 RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
184 res2 = reg_test(cli, mask, long_name, short_name);
186 if (showall || strcmp(res1, res2)) {
187 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
188 res1, res2, count, mask, file, long_name, short_name);
189 if (die_on_error) exit(1);
192 smbcli_unlink(cli->tree, file);
194 if (count % 100 == 0) DEBUG(0,("%d\n", count));
199 static void test_mask(int argc, char *argv[],
200 struct smbcli_state *cli)
205 int mc_len = strlen(maskchars);
206 int fc_len = strlen(filechars);
208 mem_ctx = talloc_init("test_mask");
209 mask = talloc_array(mem_ctx, char, MAX_STRING_LEN);
210 file = talloc_array(mem_ctx, char, MAX_STRING_LEN);
212 smbcli_mkdir(cli->tree, "\\masktest");
214 smbcli_unlink(cli->tree, "\\masktest\\*");
218 safe_strcpy(mask,"\\masktest\\", MAX_STRING_LEN);
219 safe_strcpy(file,"\\masktest\\", MAX_STRING_LEN);
220 safe_strcat(mask, argv[0], MAX_STRING_LEN);
221 safe_strcat(file, argv[1], MAX_STRING_LEN);
222 testpair(mem_ctx, cli, mask, file);
230 l1 = 1 + random() % max_length;
231 l2 = 1 + random() % max_length;
232 safe_strcpy(mask,"\\masktest\\", MAX_STRING_LEN);
233 safe_strcpy(file,"\\masktest\\", MAX_STRING_LEN);
236 mask[i+l] = maskchars[random() % mc_len];
241 file[i+l] = filechars[random() % fc_len];
245 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
249 if (strspn(file+l, ".") == strlen(file+l)) continue;
251 testpair(mem_ctx, cli, mask, file);
252 if (NumLoops && (--NumLoops == 0))
257 smbcli_rmdir(cli->tree, "\\masktest");
258 talloc_free(mem_ctx);
262 static void usage(void)
266 masktest //server/share [options..]\n\
273 -l max test length\n\
275 -f filechars (default %s)\n\
276 -m maskchars (default %s)\n\
281 This program tests wildcard matching between two servers. It generates\n\
282 random pairs of filenames/masks and tests that they match in the same\n\
283 way on the servers and internally\n\
285 filechars, maskchars);
288 /****************************************************************************
290 ****************************************************************************/
291 int main(int argc,char *argv[])
294 struct smbcli_state *cli;
297 struct loadparm_context *lp_ctx;
301 setup_logging("masktest", DEBUG_STDOUT);
303 if (argc < 2 || argv[1][0] == '-') {
310 all_string_sub(share,"/","\\",0);
312 setup_logging(argv[0], DEBUG_STDOUT);
317 lp_ctx = loadparm_init(talloc_autofree_context());
318 lp_load(lp_ctx, dyn_CONFIGFILE);
320 credentials = cli_credentials_init(talloc_autofree_context());
321 cli_credentials_guess(credentials, lp_ctx);
325 while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
328 NumLoops = atoi(optarg);
331 DEBUGLEVEL = atoi(optarg);
340 lp_set_cmdline(lp_ctx, "max protocol", optarg);
343 cli_credentials_parse_string(credentials, optarg, CRED_SPECIFIED);
355 max_length = atoi(optarg);
367 printf("Unknown option %c (%d)\n", (char)opt, opt);
377 cli = connect_one(share, lp_smb_ports(lp_ctx));
379 DEBUG(0,("Failed to connect to %s\n", share));
383 /* need to init seed after connect as clientgen uses random numbers */
384 DEBUG(0,("seed=%d format --- --- (server, correct)\n", seed));
387 test_mask(argc, argv, cli);