source: vendor/current/source4/torture/masktest.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

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