1 /*
2    Unix SMB/CIFS implementation.
3    mask_match tester
4    Copyright (C) Andrew Tridgell 1999
5 
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.
10 
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.
15 
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/>.
18 */
19 
20 #include "includes.h"
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"
31 
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 {
40 	TALLOC_CTX *mem_ctx;
41 };
42 
reg_match_one(struct smbcli_state * cli,const char * pattern,const char * file)43 static bool reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
44 {
45 	/* oh what a weird world this is */
46 	if (old_list && strcmp(pattern, "*.*") == 0) return true;
47 
48 	if (ISDOT(pattern)) return false;
49 
50 	if (ISDOTDOT(file)) file = ".";
51 
52 	return ms_fnmatch_protocol(
53 		pattern, file, cli->transport->negotiate.protocol, false)==0;
54 }
55 
reg_test(struct smbcli_state * cli,TALLOC_CTX * mem_ctx,const char * pattern,const char * long_name,const char * short_name)56 static char *reg_test(struct smbcli_state *cli, TALLOC_CTX *mem_ctx, const char *pattern, const char *long_name, const char *short_name)
57 {
58 	char *ret;
59 	ret = talloc_strdup(mem_ctx, "---");
60 
61 	pattern = 1+strrchr_m(pattern,'\\');
62 
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] = '+';
67 	return ret;
68 }
69 
70 
71 /*****************************************************
72 return a connection to a server
73 *******************************************************/
connect_one(struct resolve_context * resolve_ctx,struct tevent_context * ev,TALLOC_CTX * mem_ctx,char * share,const char ** ports,const char * socket_options,struct smbcli_options * options,struct smbcli_session_options * session_options,struct gensec_settings * gensec_settings)74 static struct smbcli_state *connect_one(struct resolve_context *resolve_ctx,
75 					struct tevent_context *ev,
76 					TALLOC_CTX *mem_ctx,
77 					char *share, const char **ports,
78 					const char *socket_options,
79 					struct smbcli_options *options,
80 					struct smbcli_session_options *session_options,
81 					struct gensec_settings *gensec_settings)
82 {
83 	struct smbcli_state *c;
84 	char *server;
85 	NTSTATUS status;
86 
87 	server = talloc_strdup(mem_ctx, share+2);
88 	share = strchr_m(server,'\\');
89 	if (!share) return NULL;
90 	*share = 0;
91 	share++;
92 
93 	cli_credentials_set_workstation(popt_get_cmdline_credentials(),
94 			"masktest", CRED_SPECIFIED);
95 
96 	status = smbcli_full_connection(NULL, &c,
97 					server,
98 					ports,
99 					share, NULL,
100 					socket_options,
101 					popt_get_cmdline_credentials(),
102 					resolve_ctx, ev,
103 					options, session_options,
104 					gensec_settings);
105 
106 	if (!NT_STATUS_IS_OK(status)) {
107 		return NULL;
108 	}
109 
110 	return c;
111 }
112 
113 static char *resultp;
114 static struct {
115 	char *long_name;
116 	char *short_name;
117 } last_hit;
118 static bool f_info_hit;
119 
listfn(struct clilist_file_info * f,const char * s,void * state)120 static void listfn(struct clilist_file_info *f, const char *s, void *state)
121 {
122 	struct masktest_state *m = (struct masktest_state *)state;
123 
124 	if (ISDOT(f->name)) {
125 		resultp[0] = '+';
126 	} else if (ISDOTDOT(f->name)) {
127 		resultp[1] = '+';
128 	} else {
129 		resultp[2] = '+';
130 	}
131 
132 	last_hit.long_name = talloc_strdup(m->mem_ctx, f->name);
133 	last_hit.short_name = talloc_strdup(m->mem_ctx, f->short_name);
134 	f_info_hit = true;
135 }
136 
get_real_name(TALLOC_CTX * mem_ctx,struct smbcli_state * cli,char ** long_name,char ** short_name)137 static void get_real_name(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
138 			  char **long_name, char **short_name)
139 {
140 	const char *mask;
141 	struct masktest_state state;
142 
143 	if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
144 		mask = "\\masktest\\*.*";
145 	} else {
146 		mask = "\\masktest\\*";
147 	}
148 
149 	f_info_hit = false;
150 
151 	state.mem_ctx = mem_ctx;
152 
153 	smbcli_list_new(cli->tree, mask,
154 			FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
155 			RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
156 			listfn, &state);
157 
158 	if (f_info_hit) {
159 		*short_name = strlower_talloc(mem_ctx, last_hit.short_name);
160 		*long_name = strlower_talloc(mem_ctx, last_hit.long_name);
161 	}
162 
163 	if (*short_name[0] == '\0') {
164 		*short_name = talloc_strdup(mem_ctx, *long_name);
165 	}
166 }
167 
testpair(TALLOC_CTX * mem_ctx,struct smbcli_state * cli,char * mask,char * file)168 static void testpair(TALLOC_CTX *mem_ctx, struct smbcli_state *cli, char *mask,
169 		char *file)
170 {
171 	int fnum;
172 	char res1[256];
173 	char *res2;
174 	static int count;
175 	char *short_name = NULL;
176 	char *long_name = NULL;
177 	struct masktest_state state;
178 
179 	count++;
180 
181 	strlcpy(res1, "---", sizeof(res1));
182 
183 	state.mem_ctx = mem_ctx;
184 
185 	fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
186 	if (fnum == -1) {
187 		DEBUG(0,("Can't create %s\n", file));
188 		return;
189 	}
190 	smbcli_close(cli->tree, fnum);
191 
192 	resultp = res1;
193 	short_name = talloc_strdup(mem_ctx, "");
194 	get_real_name(mem_ctx, cli, &long_name, &short_name);
195 	strlcpy(res1, "---", sizeof(res1));
196 	smbcli_list_new(cli->tree, mask,
197 			FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
198 			RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
199 			listfn, &state);
200 
201 	res2 = reg_test(cli, mem_ctx, mask, long_name, short_name);
202 
203 	if (showall || strcmp(res1, res2)) {
204 		d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
205 			 res1, res2, count, mask, file, long_name, short_name);
206 		if (die_on_error) exit(1);
207 	}
208 
209 	smbcli_unlink(cli->tree, file);
210 
211 	if (count % 100 == 0) DEBUG(0,("%d\n", count));
212 
213 	resultp = NULL;
214 }
215 
test_mask(int argc,char * argv[],TALLOC_CTX * mem_ctx,struct smbcli_state * cli)216 static void test_mask(int argc, char *argv[],
217 					  TALLOC_CTX *mem_ctx,
218 		      struct smbcli_state *cli)
219 {
220 	char *mask, *file;
221 	int l1, l2, i, l;
222 	int mc_len = strlen(maskchars);
223 	int fc_len = strlen(filechars);
224 
225 	smbcli_mkdir(cli->tree, "\\masktest");
226 
227 	smbcli_unlink(cli->tree, "\\masktest\\*");
228 
229 	if (argc >= 2) {
230 		while (argc >= 2) {
231 			mask = talloc_strdup(mem_ctx, "\\masktest\\");
232 			file = talloc_strdup(mem_ctx, "\\masktest\\");
233 			mask = talloc_strdup_append(mask, argv[0]);
234 			file = talloc_strdup_append(file, argv[1]);
235 			testpair(mem_ctx, cli, mask, file);
236 			argv += 2;
237 			argc -= 2;
238 		}
239 		goto finished;
240 	}
241 
242 	while (1) {
243 		l1 = 1 + random() % max_length;
244 		l2 = 1 + random() % max_length;
245 		mask = talloc_strdup(mem_ctx, "\\masktest\\");
246 		file = talloc_strdup(mem_ctx, "\\masktest\\");
247 		mask = talloc_realloc_size(mem_ctx, mask, strlen(mask)+l1+1);
248 		file = talloc_realloc_size(mem_ctx, file, strlen(file)+l2+1);
249 		l = strlen(mask);
250 		for (i=0;i<l1;i++) {
251 			mask[i+l] = maskchars[random() % mc_len];
252 		}
253 		mask[l+l1] = 0;
254 
255 		for (i=0;i<l2;i++) {
256 			file[i+l] = filechars[random() % fc_len];
257 		}
258 		file[l+l2] = 0;
259 
260 		if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
261 			continue;
262 		}
263 
264 		if (strspn(file+l, ".") == strlen(file+l)) continue;
265 
266 		testpair(mem_ctx, cli, mask, file);
267 		if (NumLoops && (--NumLoops == 0))
268 			break;
269 	}
270 
271  finished:
272 	smbcli_rmdir(cli->tree, "\\masktest");
273 }
274 
275 
usage(poptContext pc)276 static void usage(poptContext pc)
277 {
278 	printf(
279 "Usage:\n\
280   masktest //server/share [options..]\n\
281 \n\
282   This program tests wildcard matching between two servers. It generates\n\
283   random pairs of filenames/masks and tests that they match in the same\n\
284   way on the servers and internally\n");
285 	poptPrintUsage(pc, stdout, 0);
286 }
287 
288 /****************************************************************************
289   main program
290 ****************************************************************************/
main(int argc,const char * argv[])291 int main(int argc, const char *argv[])
292 {
293 	char *share;
294 	struct smbcli_state *cli;
295 	int opt;
296 	int seed;
297 	struct tevent_context *ev;
298 	struct loadparm_context *lp_ctx;
299 	struct smbcli_options options;
300 	struct smbcli_session_options session_options;
301 	poptContext pc;
302 	int argc_new, i;
303 	char **argv_new;
304 	TALLOC_CTX *mem_ctx = NULL;
305 	enum {OPT_UNCLIST=1000};
306 	struct poptOption long_options[] = {
307 		POPT_AUTOHELP
308 		{"seed",	  0, POPT_ARG_INT,  &seed, 	0,	"Seed to use for randomizer", 	NULL},
309 		{"num-ops",	  0, POPT_ARG_INT,  &NumLoops, 	0, 	"num ops",	NULL},
310 		{"maxlength",	  0, POPT_ARG_INT,  &max_length,0, 	"maximum length",	NULL},
311 		{"dieonerror",    0, POPT_ARG_NONE, &die_on_error, 0,   "die on errors", NULL},
312 		{"showall",       0, POPT_ARG_NONE, &showall,    0,      "display all operations", NULL},
313 		{"oldlist",       0, POPT_ARG_NONE, &old_list,    0,     "use old list call", NULL},
314 		{"maskchars",	  0, POPT_ARG_STRING,	&maskchars,    0,"mask characters", 	NULL},
315 		{"filechars",	  0, POPT_ARG_STRING,	&filechars,    0,"file characters", 	NULL},
316 		POPT_COMMON_SAMBA
317 		POPT_COMMON_CONNECTION
318 		POPT_COMMON_CREDENTIALS
319 		POPT_COMMON_VERSION
320 		{ NULL }
321 	};
322 
323 	setlinebuf(stdout);
324 	seed = time(NULL);
325 
326 	mem_ctx = talloc_named_const(NULL, 0, "masktest_ctx");
327 	if (mem_ctx == NULL) {
328 		exit(1);
329 	}
330 
331 	pc = poptGetContext("locktest", argc, argv, long_options,
332 			    POPT_CONTEXT_KEEP_FIRST);
333 
334 	poptSetOtherOptionHelp(pc, "<unc>");
335 
336 	while((opt = poptGetNextOpt(pc)) != -1) {
337 		switch (opt) {
338 		case OPT_UNCLIST:
339 			lpcfg_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
340 			break;
341 		}
342 	}
343 
344 	argv_new = discard_const_p(char *, poptGetArgs(pc));
345 	argc_new = argc;
346 	for (i=0; i<argc; i++) {
347 		if (argv_new[i] == NULL) {
348 			argc_new = i;
349 			break;
350 		}
351 	}
352 
353 	if (!(argc_new >= 2)) {
354 		usage(pc);
355 		talloc_free(mem_ctx);
356 		exit(1);
357 	}
358 
359 	setup_logging("masktest", DEBUG_STDOUT);
360 
361 	share = argv_new[1];
362 
363 	all_string_sub(share,"/","\\",0);
364 
365 	lp_ctx = cmdline_lp_ctx;
366 
367 	ev = s4_event_context_init(mem_ctx);
368 
369 	gensec_init();
370 
371 	lpcfg_smbcli_options(lp_ctx, &options);
372 	lpcfg_smbcli_session_options(lp_ctx, &session_options);
373 
374 	cli = connect_one(lpcfg_resolve_context(lp_ctx), ev, mem_ctx, share,
375 			  lpcfg_smb_ports(lp_ctx), lpcfg_socket_options(lp_ctx),
376 			  &options, &session_options,
377 			  lpcfg_gensec_settings(mem_ctx, lp_ctx));
378 	if (!cli) {
379 		DEBUG(0,("Failed to connect to %s\n", share));
380 		talloc_free(mem_ctx);
381 		exit(1);
382 	}
383 
384 	/* need to init seed after connect as clientgen uses random numbers */
385 	DEBUG(0,("seed=%d     format --- --- (server, correct)\n", seed));
386 	srandom(seed);
387 
388 	test_mask(argc_new-1, argv_new+1, mem_ctx, cli);
389 
390 	poptFreeContext(pc);
391 	talloc_free(mem_ctx);
392 	return(0);
393 }
394