r26654: libcli/smb_composite: Rather than specifying each of the gazillion options...
[jelmer/samba4-debian.git] / source / 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 "system/filesys.h"
22 #include "system/dir.h"
23 #include "libcli/libcli.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "pstring.h"
27 #include "auth/credentials/credentials.h"
28 #include "auth/gensec/gensec.h"
29 #include "param/param.h"
30 #include "dynconfig.h"
31 #include "libcli/resolve/resolve.h"
32
33 static struct cli_credentials *credentials;
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 verbose;
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                                         char *share, const char **ports,
78                                         struct smbcli_options *options)
79 {
80         struct smbcli_state *c;
81         fstring server;
82         NTSTATUS status;
83
84         fstrcpy(server,share+2);
85         share = strchr_m(server,'\\');
86         if (!share) return NULL;
87         *share = 0;
88         share++;
89
90         cli_credentials_set_workstation(credentials, "masktest", CRED_SPECIFIED);
91
92         status = smbcli_full_connection(NULL, &c,
93                                         server, 
94                                         ports,
95                                         share, NULL,
96                                         credentials, resolve_ctx, NULL,
97                                         options);
98
99         if (!NT_STATUS_IS_OK(status)) {
100                 return NULL;
101         }
102
103         return c;
104 }
105
106 static char *resultp;
107 static struct {
108         char *long_name;
109         char *short_name;
110 } last_hit;
111 static bool f_info_hit;
112
113 static void listfn(struct clilist_file_info *f, const char *s, void *state)
114 {
115         struct masktest_state *m = talloc_get_type(state,struct masktest_state);
116
117         if (ISDOT(f->name)) {
118                 resultp[0] = '+';
119         } else if (ISDOTDOT(f->name)) {
120                 resultp[1] = '+';
121         } else {
122                 resultp[2] = '+';
123         }
124
125         last_hit.long_name = talloc_strdup(m->mem_ctx, f->name);
126         last_hit.short_name = talloc_strdup(m->mem_ctx, f->short_name);
127         f_info_hit = true;
128 }
129
130 static void get_real_name(TALLOC_CTX *mem_ctx, struct smbcli_state *cli,
131                           char *long_name, fstring short_name)
132 {
133         const char *mask;
134         struct masktest_state state;
135
136         if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
137                 mask = "\\masktest\\*.*";
138         } else {
139                 mask = "\\masktest\\*";
140         }
141
142         f_info_hit = false;
143
144         state.mem_ctx = mem_ctx;
145
146         smbcli_list_new(cli->tree, mask,
147                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
148                         RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
149                         listfn, &state);
150
151         if (f_info_hit) {
152                 fstrcpy(short_name, last_hit.short_name);
153                 strlower(short_name);
154                 long_name = talloc_strdup(mem_ctx, last_hit.long_name);
155                 strlower(long_name);
156         }
157
158         if (*short_name == '\0') {
159                 fstrcpy(short_name, long_name);
160         }
161 }
162
163 static void testpair(TALLOC_CTX *mem_ctx, struct smbcli_state *cli, char *mask,
164                 char *file)
165 {
166         int fnum;
167         fstring res1;
168         char *res2;
169         static int count;
170         fstring short_name;
171         char *long_name;
172         struct masktest_state state;
173
174         count++;
175
176         fstrcpy(res1, "---");
177
178         state.mem_ctx = mem_ctx;
179
180         fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
181         if (fnum == -1) {
182                 DEBUG(0,("Can't create %s\n", file));
183                 return;
184         }
185         smbcli_close(cli->tree, fnum);
186
187         resultp = res1;
188         fstrcpy(short_name, "");
189         get_real_name(mem_ctx, cli, long_name, short_name);
190         fstrcpy(res1, "---");
191         smbcli_list_new(cli->tree, mask,
192                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,
193                         RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
194                         listfn, &state);
195
196         res2 = reg_test(cli, mask, long_name, short_name);
197
198         if (showall || strcmp(res1, res2)) {
199                 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
200                          res1, res2, count, mask, file, long_name, short_name);
201                 if (die_on_error) exit(1);
202         }
203
204         smbcli_unlink(cli->tree, file);
205
206         if (count % 100 == 0) DEBUG(0,("%d\n", count));
207
208         resultp = NULL;
209 }
210
211 static void test_mask(int argc, char *argv[],
212                       struct smbcli_state *cli)
213 {
214         TALLOC_CTX *mem_ctx;
215         char *mask, *file;
216         int l1, l2, i, l;
217         int mc_len = strlen(maskchars);
218         int fc_len = strlen(filechars);
219
220         mem_ctx = talloc_init("test_mask");
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                 l = strlen(mask);
245                 for (i=0;i<l1;i++) {
246                         mask[i+l] = maskchars[random() % mc_len];
247                 }
248                 mask[l+l1] = 0;
249
250                 for (i=0;i<l2;i++) {
251                         file[i+l] = filechars[random() % fc_len];
252                 }
253                 file[l+l2] = 0;
254
255                 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
256                         continue;
257                 }
258
259                 if (strspn(file+l, ".") == strlen(file+l)) continue;
260
261                 testpair(mem_ctx, cli, mask, file);
262                 if (NumLoops && (--NumLoops == 0))
263                         break;
264         }
265
266  finished:
267         smbcli_rmdir(cli->tree, "\\masktest");
268         talloc_free(mem_ctx);
269 }
270
271
272 static void usage(void)
273 {
274         printf(
275 "Usage:\n\
276   masktest //server/share [options..]\n\
277   options:\n\
278         -d debuglevel\n\
279         -n numloops\n\
280         -W workgroup\n\
281         -U user%%pass\n\
282         -s seed\n\
283         -l max test length\n\
284         -M max protocol\n\
285         -f filechars (default %s)\n\
286         -m maskchars (default %s)\n\
287         -v                             verbose mode\n\
288         -E                             die on error\n\
289         -a                             show all tests\n\
290 \n\
291   This program tests wildcard matching between two servers. It generates\n\
292   random pairs of filenames/masks and tests that they match in the same\n\
293   way on the servers and internally\n\
294 ", 
295   filechars, maskchars);
296 }
297
298 /****************************************************************************
299   main program
300 ****************************************************************************/
301  int main(int argc,char *argv[])
302 {
303         char *share;
304         struct smbcli_state *cli;       
305         int opt;
306         int seed;
307         struct loadparm_context *lp_ctx;
308         struct smbcli_options options;
309
310         setlinebuf(stdout);
311
312         setup_logging("masktest", DEBUG_STDOUT);
313
314         if (argc < 2 || argv[1][0] == '-') {
315                 usage();
316                 exit(1);
317         }
318
319         share = argv[1];
320
321         all_string_sub(share,"/","\\",0);
322
323         setup_logging(argv[0], DEBUG_STDOUT);
324
325         argc -= 1;
326         argv += 1;
327
328         lp_ctx = loadparm_init(talloc_autofree_context());
329         lp_load(lp_ctx, dyn_CONFIGFILE);
330
331         credentials = cli_credentials_init(talloc_autofree_context());
332         cli_credentials_guess(credentials, lp_ctx);
333
334         seed = time(NULL);
335
336         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
337                 switch (opt) {
338                 case 'n':
339                         NumLoops = atoi(optarg);
340                         break;
341                 case 'd':
342                         DEBUGLEVEL = atoi(optarg);
343                         break;
344                 case 'E':
345                         die_on_error = 1;
346                         break;
347                 case 'v':
348                         verbose++;
349                         break;
350                 case 'M':
351                         lp_set_cmdline(lp_ctx, "max protocol", optarg);
352                         break;
353                 case 'U':
354                         cli_credentials_parse_string(credentials, optarg, CRED_SPECIFIED);
355                         break;
356                 case 's':
357                         seed = atoi(optarg);
358                         break;
359                 case 'h':
360                         usage();
361                         exit(1);
362                 case 'm':
363                         maskchars = optarg;
364                         break;
365                 case 'l':
366                         max_length = atoi(optarg);
367                         break;
368                 case 'f':
369                         filechars = optarg;
370                         break;
371                 case 'a':
372                         showall = 1;
373                         break;
374                 case 'o':
375                         old_list = true;
376                         break;
377                 default:
378                         printf("Unknown option %c (%d)\n", (char)opt, opt);
379                         exit(1);
380                 }
381         }
382
383         gensec_init(lp_ctx);
384
385         argc -= optind;
386         argv += optind;
387
388         lp_smbcli_options(lp_ctx, &options);
389
390         cli = connect_one(lp_resolve_context(lp_ctx), share, 
391                           lp_smb_ports(lp_ctx), &options);
392         if (!cli) {
393                 DEBUG(0,("Failed to connect to %s\n", share));
394                 exit(1);
395         }
396
397         /* need to init seed after connect as clientgen uses random numbers */
398         DEBUG(0,("seed=%d     format --- --- (server, correct)\n", seed));
399         srandom(seed);
400
401         test_mask(argc, argv, cli);
402
403         return(0);
404 }