Merge branch 'master' of ssh://git.samba.org/data/git/samba
[samba.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/dynconfig.h"
32 #include "libcli/resolve/resolve.h"
33 #include "lib/events/events.h"
34
35 static bool showall = false;
36 static bool old_list = false;
37 static const char *maskchars = "<>\"?*abc.";
38 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 {
43         TALLOC_CTX *mem_ctx;
44 };
45
46 static bool reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
47 {
48         /* oh what a weird world this is */
49         if (old_list && strcmp(pattern, "*.*") == 0) return true;
50
51         if (ISDOT(pattern)) return false;
52
53         if (ISDOTDOT(file)) file = ".";
54
55         return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
56 }
57
58 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
59 {
60         static fstring ret;
61         fstrcpy(ret, "---");
62
63         pattern = 1+strrchr_m(pattern,'\\');
64
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] = '+';
69         return ret;
70 }
71
72
73 /***************************************************** 
74 return a connection to a server
75 *******************************************************/
76 static struct smbcli_state *connect_one(struct resolve_context *resolve_ctx, 
77                                         struct event_context *ev,
78                                         char *share, const char **ports,
79                                         struct smbcli_options *options,
80                                         struct smbcli_session_options *session_options)
81 {
82         struct smbcli_state *c;
83         fstring server;
84         NTSTATUS status;
85
86         fstrcpy(server,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                                         cmdline_credentials, resolve_ctx, ev,
99                                         options, session_options);
100
101         if (!NT_STATUS_IS_OK(status)) {
102                 return NULL;
103         }
104
105         return c;
106 }
107
108 static char *resultp;
109 static struct {
110         char *long_name;
111         char *short_name;
112 } last_hit;
113 static bool f_info_hit;
114
115 static void listfn(struct clilist_file_info *f, const char *s, void *state)
116 {
117         struct masktest_state *m = (struct masktest_state *)state;
118
119         if (ISDOT(f->name)) {
120                 resultp[0] = '+';
121         } else if (ISDOTDOT(f->name)) {
122                 resultp[1] = '+';
123         } else {
124                 resultp[2] = '+';
125         }
126
127         last_hit.long_name = talloc_strdup(m->mem_ctx, f->name);
128         last_hit.short_name = talloc_strdup(m->mem_ctx, f->short_name);
129         f_info_hit = true;
130 }
131
132 static void get_real_name(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
133                           char **long_name, fstring short_name)
134 {
135         const char *mask;
136         struct masktest_state state;
137
138         if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
139                 mask = "\\masktest\\*.*";
140         } else {
141                 mask = "\\masktest\\*";
142         }
143
144         f_info_hit = false;
145
146         state.mem_ctx = mem_ctx;
147
148         smbcli_list_new(cli->tree, mask,
149                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
150                         RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
151                         listfn, &state);
152
153         if (f_info_hit) {
154                 fstrcpy(short_name, last_hit.short_name);
155                 strlower(short_name);
156                 *long_name = talloc_strdup(mem_ctx, last_hit.long_name);
157                 strlower(*long_name);
158         }
159
160         if (*short_name == '\0') {
161                 fstrcpy(short_name, *long_name);
162         }
163 }
164
165 static void testpair(TALLOC_CTX *mem_ctx, struct smbcli_state *cli, char *mask,
166                 char *file)
167 {
168         int fnum;
169         fstring res1;
170         char *res2;
171         static int count;
172         fstring short_name;
173         char *long_name = NULL;
174         struct masktest_state state;
175
176         count++;
177
178         fstrcpy(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         fstrcpy(short_name, "");
191         get_real_name(mem_ctx, cli, &long_name, short_name);
192         fstrcpy(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, 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
213 static void test_mask(int argc, char *argv[],
214                       struct smbcli_state *cli)
215 {
216         TALLOC_CTX *mem_ctx;
217         char *mask, *file;
218         int l1, l2, i, l;
219         int mc_len = strlen(maskchars);
220         int fc_len = strlen(filechars);
221
222         mem_ctx = talloc_init("test_mask");
223
224         smbcli_mkdir(cli->tree, "\\masktest");
225
226         smbcli_unlink(cli->tree, "\\masktest\\*");
227
228         if (argc >= 2) {
229                 while (argc >= 2) {
230                         mask = talloc_strdup(mem_ctx, "\\masktest\\");
231                         file = talloc_strdup(mem_ctx, "\\masktest\\");
232                         mask = talloc_strdup_append(mask, argv[0]);
233                         file = talloc_strdup_append(file, argv[1]);
234                         testpair(mem_ctx, cli, mask, file);
235                         argv += 2;
236                         argc -= 2;
237                 }
238                 goto finished;
239         }
240
241         while (1) {
242                 l1 = 1 + random() % max_length;
243                 l2 = 1 + random() % max_length;
244                 mask = talloc_strdup(mem_ctx, "\\masktest\\");
245                 file = talloc_strdup(mem_ctx, "\\masktest\\");
246                 mask = talloc_realloc_size(mem_ctx, mask, strlen(mask)+l1+1);
247                 file = talloc_realloc_size(mem_ctx, file, strlen(file)+l2+1);
248                 l = strlen(mask);
249                 for (i=0;i<l1;i++) {
250                         mask[i+l] = maskchars[random() % mc_len];
251                 }
252                 mask[l+l1] = 0;
253
254                 for (i=0;i<l2;i++) {
255                         file[i+l] = filechars[random() % fc_len];
256                 }
257                 file[l+l2] = 0;
258
259                 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
260                         continue;
261                 }
262
263                 if (strspn(file+l, ".") == strlen(file+l)) continue;
264
265                 testpair(mem_ctx, cli, mask, file);
266                 if (NumLoops && (--NumLoops == 0))
267                         break;
268         }
269
270  finished:
271         smbcli_rmdir(cli->tree, "\\masktest");
272         talloc_free(mem_ctx);
273 }
274
275
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 ****************************************************************************/
291  int main(int argc,char *argv[])
292 {
293         char *share;
294         struct smbcli_state *cli;       
295         int opt;
296         int seed;
297         struct event_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         enum {OPT_UNCLIST=1000};
305         struct poptOption long_options[] = {
306                 POPT_AUTOHELP
307                 {"seed",          0, POPT_ARG_INT,  &seed,      0,      "Seed to use for randomizer",   NULL},
308                 {"num-ops",       0, POPT_ARG_INT,  &NumLoops,  0,      "num ops",      NULL},
309                 {"maxlength",     0, POPT_ARG_INT,  &max_length,0,      "maximum length",       NULL},
310                 {"dieonerror",    0, POPT_ARG_NONE, &die_on_error, 0,   "die on errors", NULL},
311                 {"showall",       0, POPT_ARG_NONE, &showall,    0,      "display all operations", NULL},
312                 {"oldlist",       0, POPT_ARG_NONE, &old_list,    0,     "use old list call", NULL},
313                 {"maskchars",     0, POPT_ARG_STRING,   &maskchars,    0,"mask characters",     NULL},
314                 {"filechars",     0, POPT_ARG_STRING,   &filechars,    0,"file characters",     NULL},
315                 POPT_COMMON_SAMBA
316                 POPT_COMMON_CONNECTION
317                 POPT_COMMON_CREDENTIALS
318                 POPT_COMMON_VERSION
319                 { NULL }
320         };
321
322         setlinebuf(stdout);
323         seed = time(NULL);
324
325         pc = poptGetContext("locktest", argc, (const char **) argv, long_options, 
326                             POPT_CONTEXT_KEEP_FIRST);
327
328         poptSetOtherOptionHelp(pc, "<unc>");
329
330         while((opt = poptGetNextOpt(pc)) != -1) {
331                 switch (opt) {
332                 case OPT_UNCLIST:
333                         lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
334                         break;
335                 }
336         }
337
338         argv_new = discard_const_p(char *, poptGetArgs(pc));
339         argc_new = argc;
340         for (i=0; i<argc; i++) {
341                 if (argv_new[i] == NULL) {
342                         argc_new = i;
343                         break;
344                 }
345         }
346
347         if (!(argc_new >= 2)) {
348                 usage(pc);
349                 exit(1);
350         }
351
352         setup_logging("masktest", DEBUG_STDOUT);
353
354         share = argv_new[1];
355
356         all_string_sub(share,"/","\\",0);
357
358         lp_ctx = cmdline_lp_ctx;
359
360         ev = s4_event_context_init(talloc_autofree_context());
361
362         gensec_init(lp_ctx);
363
364         lp_smbcli_options(lp_ctx, &options);
365         lp_smbcli_session_options(lp_ctx, &session_options);
366
367         cli = connect_one(lp_resolve_context(lp_ctx), ev, share, 
368                           lp_smb_ports(lp_ctx), &options, &session_options);
369         if (!cli) {
370                 DEBUG(0,("Failed to connect to %s\n", share));
371                 exit(1);
372         }
373
374         /* need to init seed after connect as clientgen uses random numbers */
375         DEBUG(0,("seed=%d     format --- --- (server, correct)\n", seed));
376         srandom(seed);
377
378         test_mask(argc_new-1, argv_new+1, cli);
379
380         return(0);
381 }