Repel pstring to nsswitch/.
[kai/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 "auth/credentials/credentials.h"
28 #include "auth/gensec/gensec.h"
29 #include "param/param.h"
30 #include "dynconfig/dynconfig.h"
31 #include "libcli/resolve/resolve.h"
32 #include "lib/events/events.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, TALLOC_CTX *mem_ctx, const char *pattern, const char *long_name, const char *short_name)
58 {
59         char *ret;
60         ret = talloc_strdup(mem_ctx, "---");
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                                         struct event_context *ev,
77                                         char *share, const char **ports,
78                                         struct smbcli_options *options,
79                                         struct smbcli_session_options *session_options)
80 {
81         struct smbcli_state *c;
82         char *server;
83         NTSTATUS status;
84
85         server = smb_xstrdup(share+2);
86         share = strchr_m(server,'\\');
87         if (!share) {
88                 SAFE_FREE(server);
89                 return NULL;
90         }
91         *share = 0;
92         share++;
93
94         cli_credentials_set_workstation(cmdline_credentials, "masktest", CRED_SPECIFIED);
95
96         status = smbcli_full_connection(NULL, &c,
97                                         server, 
98                                         ports,
99                                         share, NULL,
100                                         cmdline_credentials, resolve_ctx, ev,
101                                         options, session_options);
102
103         SAFE_FREE(server);
104
105         if (!NT_STATUS_IS_OK(status)) {
106                 return NULL;
107         }
108
109         return c;
110 }
111
112 static char *resultp;
113 static struct {
114         char *long_name;
115         char *short_name;
116 } last_hit;
117 static bool f_info_hit;
118
119 static void listfn(struct clilist_file_info *f, const char *s, void *state)
120 {
121         struct masktest_state *m = (struct masktest_state *)state;
122
123         if (ISDOT(f->name)) {
124                 resultp[0] = '+';
125         } else if (ISDOTDOT(f->name)) {
126                 resultp[1] = '+';
127         } else {
128                 resultp[2] = '+';
129         }
130
131         last_hit.long_name = talloc_strdup(m->mem_ctx, f->name);
132         last_hit.short_name = talloc_strdup(m->mem_ctx, f->short_name);
133         f_info_hit = true;
134 }
135
136 static void get_real_name(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
137                           char **long_name, char **short_name)
138 {
139         const char *mask;
140         struct masktest_state state;
141
142         if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
143                 mask = "\\masktest\\*.*";
144         } else {
145                 mask = "\\masktest\\*";
146         }
147
148         f_info_hit = false;
149
150         state.mem_ctx = mem_ctx;
151
152         smbcli_list_new(cli->tree, mask,
153                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
154                         RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
155                         listfn, &state);
156
157         if (f_info_hit) {
158                 *short_name = talloc_strdup(mem_ctx, last_hit.short_name);
159                 strlower(*short_name);
160                 *long_name = talloc_strdup(mem_ctx, last_hit.long_name);
161                 strlower(*long_name);
162         }
163
164         if (*short_name == '\0') {
165                 *short_name = talloc_strdup(mem_ctx, *long_name);
166         }
167 }
168
169 static void testpair(TALLOC_CTX *mem_ctx, struct smbcli_state *cli, char *mask,
170                 char *file)
171 {
172         int fnum;
173         char *res1;
174         char *res2;
175         static int count;
176         char *short_name = NULL;
177         char *long_name = NULL;
178         struct masktest_state state;
179
180         count++;
181
182         res1 = talloc_strdup(mem_ctx, "---");
183
184         state.mem_ctx = mem_ctx;
185
186         fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
187         if (fnum == -1) {
188                 DEBUG(0,("Can't create %s\n", file));
189                 return;
190         }
191         smbcli_close(cli->tree, fnum);
192
193         resultp = res1;
194         get_real_name(mem_ctx, cli, &long_name, &short_name);
195         res1 = talloc_strdup(mem_ctx, "---");
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
216 static void test_mask(int argc, char *argv[],
217                       struct smbcli_state *cli)
218 {
219         TALLOC_CTX *mem_ctx;
220         char *mask, *file;
221         int l1, l2, i, l;
222         int mc_len = strlen(maskchars);
223         int fc_len = strlen(filechars);
224
225         mem_ctx = talloc_init("test_mask");
226
227         smbcli_mkdir(cli->tree, "\\masktest");
228
229         smbcli_unlink(cli->tree, "\\masktest\\*");
230
231         if (argc >= 2) {
232                 while (argc >= 2) {
233                         mask = talloc_strdup(mem_ctx, "\\masktest\\");
234                         file = talloc_strdup(mem_ctx, "\\masktest\\");
235                         mask = talloc_strdup_append(mask, argv[0]);
236                         file = talloc_strdup_append(file, argv[1]);
237                         testpair(mem_ctx, cli, mask, file);
238                         argv += 2;
239                         argc -= 2;
240                 }
241                 goto finished;
242         }
243
244         while (1) {
245                 l1 = 1 + random() % max_length;
246                 l2 = 1 + random() % max_length;
247                 mask = talloc_strdup(mem_ctx, "\\masktest\\");
248                 file = talloc_strdup(mem_ctx, "\\masktest\\");
249                 mask = talloc_realloc_size(mem_ctx, mask, strlen(mask)+l1+1);
250                 file = talloc_realloc_size(mem_ctx, file, strlen(file)+l2+1);
251                 l = strlen(mask);
252                 for (i=0;i<l1;i++) {
253                         mask[i+l] = maskchars[random() % mc_len];
254                 }
255                 mask[l+l1] = 0;
256
257                 for (i=0;i<l2;i++) {
258                         file[i+l] = filechars[random() % fc_len];
259                 }
260                 file[l+l2] = 0;
261
262                 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
263                         continue;
264                 }
265
266                 if (strspn(file+l, ".") == strlen(file+l)) continue;
267
268                 testpair(mem_ctx, cli, mask, file);
269                 if (NumLoops && (--NumLoops == 0))
270                         break;
271         }
272
273  finished:
274         smbcli_rmdir(cli->tree, "\\masktest");
275         talloc_free(mem_ctx);
276 }
277
278
279 static void usage(poptContext pc)
280 {
281         printf(
282 "Usage:\n\
283   masktest //server/share [options..]\n\
284 \n\
285   This program tests wildcard matching between two servers. It generates\n\
286   random pairs of filenames/masks and tests that they match in the same\n\
287   way on the servers and internally\n");
288         poptPrintUsage(pc, stdout, 0);
289 }
290
291 /****************************************************************************
292   main program
293 ****************************************************************************/
294  int main(int argc,char *argv[])
295 {
296         char *share;
297         struct smbcli_state *cli;       
298         int opt;
299         int seed;
300         struct event_context *ev;
301         struct loadparm_context *lp_ctx;
302         struct smbcli_options options;
303         struct smbcli_session_options session_options;
304         poptContext pc;
305         int argc_new, i;
306         char **argv_new;
307         enum {OPT_UNCLIST=1000};
308         struct poptOption long_options[] = {
309                 POPT_AUTOHELP
310                 {"seed",          0, POPT_ARG_INT,  &seed,      0,      "Seed to use for randomizer",   NULL},
311                 {"num-ops",       0, POPT_ARG_INT,  &NumLoops,  0,      "num ops",      NULL},
312                 {"maxlength",     0, POPT_ARG_INT,  &max_length,0,      "maximum length",       NULL},
313                 {"dieonerror",    0, POPT_ARG_NONE, &die_on_error, 0,   "die on errors", NULL},
314                 {"showall",       0, POPT_ARG_NONE, &showall,    0,      "display all operations", NULL},
315                 {"oldlist",       0, POPT_ARG_NONE, &old_list,    0,     "use old list call", NULL},
316                 {"maskchars",     0, POPT_ARG_STRING,   &maskchars,    0,"mask characters",     NULL},
317                 {"filechars",     0, POPT_ARG_STRING,   &filechars,    0,"file characters",     NULL},
318                 POPT_COMMON_SAMBA
319                 POPT_COMMON_CONNECTION
320                 POPT_COMMON_CREDENTIALS
321                 POPT_COMMON_VERSION
322                 { NULL }
323         };
324
325         setlinebuf(stdout);
326         seed = time(NULL);
327
328         pc = poptGetContext("locktest", argc, (const char **) argv, long_options, 
329                             POPT_CONTEXT_KEEP_FIRST);
330
331         poptSetOtherOptionHelp(pc, "<unc>");
332
333         while((opt = poptGetNextOpt(pc)) != -1) {
334                 switch (opt) {
335                 case OPT_UNCLIST:
336                         lp_set_cmdline(cmdline_lp_ctx, "torture:unclist", poptGetOptArg(pc));
337                         break;
338                 }
339         }
340
341         argv_new = discard_const_p(char *, poptGetArgs(pc));
342         argc_new = argc;
343         for (i=0; i<argc; i++) {
344                 if (argv_new[i] == NULL) {
345                         argc_new = i;
346                         break;
347                 }
348         }
349
350         if (!(argc_new >= 2)) {
351                 usage(pc);
352                 exit(1);
353         }
354
355         setup_logging("masktest", DEBUG_STDOUT);
356
357         share = argv_new[1];
358
359         all_string_sub(share,"/","\\",0);
360
361         lp_ctx = cmdline_lp_ctx;
362
363         ev = s4_event_context_init(talloc_autofree_context());
364
365         gensec_init(lp_ctx);
366
367         lp_smbcli_options(lp_ctx, &options);
368         lp_smbcli_session_options(lp_ctx, &session_options);
369
370         cli = connect_one(lp_resolve_context(lp_ctx), ev, share, 
371                           lp_smb_ports(lp_ctx), &options, &session_options);
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, cli);
382
383         return(0);
384 }