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