r25398: Parse loadparm context to all lp_*() functions.
[garming/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 "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
32 static struct cli_credentials *credentials;
33 static BOOL showall = False;
34 static BOOL old_list = False;
35 static const char *maskchars = "<>\"?*abc.";
36 static const char *filechars = "abcdefghijklm.";
37 static int verbose;
38 static int die_on_error;
39 static int NumLoops = 0;
40 static int max_length = 20;
41
42 static BOOL reg_match_one(struct smbcli_state *cli, const char *pattern, const char *file)
43 {
44         /* oh what a weird world this is */
45         if (old_list && strcmp(pattern, "*.*") == 0) return True;
46
47         if (ISDOT(pattern)) return False;
48
49         if (ISDOTDOT(file)) file = ".";
50
51         return ms_fnmatch(pattern, file, cli->transport->negotiate.protocol)==0;
52 }
53
54 static char *reg_test(struct smbcli_state *cli, char *pattern, char *long_name, char *short_name)
55 {
56         static fstring ret;
57         fstrcpy(ret, "---");
58
59         pattern = 1+strrchr_m(pattern,'\\');
60
61         if (reg_match_one(cli, pattern, ".")) ret[0] = '+';
62         if (reg_match_one(cli, pattern, "..")) ret[1] = '+';
63         if (reg_match_one(cli, pattern, long_name) || 
64             (*short_name && reg_match_one(cli, pattern, short_name))) ret[2] = '+';
65         return ret;
66 }
67
68
69 /***************************************************** 
70 return a connection to a server
71 *******************************************************/
72 static struct smbcli_state *connect_one(char *share)
73 {
74         struct smbcli_state *c;
75         fstring server;
76         NTSTATUS status;
77
78         fstrcpy(server,share+2);
79         share = strchr_m(server,'\\');
80         if (!share) return NULL;
81         *share = 0;
82         share++;
83
84         cli_credentials_set_workstation(credentials, "masktest", CRED_SPECIFIED);
85
86         status = smbcli_full_connection(NULL, &c,
87                                         server, 
88                                         share, NULL,
89                                         credentials, NULL);
90
91         if (!NT_STATUS_IS_OK(status)) {
92                 return NULL;
93         }
94
95         return c;
96 }
97
98 static char *resultp;
99 static struct {
100         pstring long_name;
101         pstring short_name;
102 } last_hit;
103 static BOOL f_info_hit;
104
105 static void listfn(struct clilist_file_info *f, const char *s, void *state)
106 {
107         if (ISDOT(f->name)) {
108                 resultp[0] = '+';
109         } else if (ISDOTDOT(f->name)) {
110                 resultp[1] = '+';               
111         } else {
112                 resultp[2] = '+';
113         }
114         pstrcpy(last_hit.long_name, f->name);
115         pstrcpy(last_hit.short_name, f->short_name);
116         f_info_hit = True;
117 }
118
119 static void get_real_name(struct smbcli_state *cli, 
120                           pstring long_name, fstring short_name)
121 {
122         const char *mask;
123         if (cli->transport->negotiate.protocol <= PROTOCOL_LANMAN1) {
124                 mask = "\\masktest\\*.*";
125         } else {
126                 mask = "\\masktest\\*";
127         }
128
129         f_info_hit = False;
130
131         smbcli_list_new(cli->tree, mask, 
132                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
133                         RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
134                         listfn, NULL);
135
136         if (f_info_hit) {
137                 fstrcpy(short_name, last_hit.short_name);
138                 strlower(short_name);
139                 pstrcpy(long_name, last_hit.long_name);
140                 strlower(long_name);
141         }
142
143         if (*short_name == 0) {
144                 fstrcpy(short_name, long_name);
145         }
146 }
147
148 static void testpair(struct smbcli_state *cli, char *mask, char *file)
149 {
150         int fnum;
151         fstring res1;
152         char *res2;
153         static int count;
154         fstring short_name;
155         pstring long_name;
156
157         count++;
158
159         fstrcpy(res1, "---");
160
161         fnum = smbcli_open(cli->tree, file, O_CREAT|O_TRUNC|O_RDWR, 0);
162         if (fnum == -1) {
163                 DEBUG(0,("Can't create %s\n", file));
164                 return;
165         }
166         smbcli_close(cli->tree, fnum);
167
168         resultp = res1;
169         fstrcpy(short_name, "");
170         get_real_name(cli, long_name, short_name);
171         fstrcpy(res1, "---");
172         smbcli_list_new(cli->tree, mask, 
173                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY, 
174                         RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO,
175                         listfn, NULL);
176
177         res2 = reg_test(cli, mask, long_name, short_name);
178
179         if (showall || strcmp(res1, res2)) {
180                 d_printf("%s %s %d mask=[%s] file=[%s] rfile=[%s/%s]\n",
181                          res1, res2, count, mask, file, long_name, short_name);
182                 if (die_on_error) exit(1);
183         }
184
185         smbcli_unlink(cli->tree, file);
186
187         if (count % 100 == 0) DEBUG(0,("%d\n", count));
188
189         resultp = NULL;
190 }
191
192 static void test_mask(int argc, char *argv[], 
193                       struct smbcli_state *cli)
194 {
195         pstring mask, file;
196         int l1, l2, i, l;
197         int mc_len = strlen(maskchars);
198         int fc_len = strlen(filechars);
199
200         smbcli_mkdir(cli->tree, "\\masktest");
201
202         smbcli_unlink(cli->tree, "\\masktest\\*");
203
204         if (argc >= 2) {
205                 while (argc >= 2) {
206                         pstrcpy(mask,"\\masktest\\");
207                         pstrcpy(file,"\\masktest\\");
208                         pstrcat(mask, argv[0]);
209                         pstrcat(file, argv[1]);
210                         testpair(cli, mask, file);
211                         argv += 2;
212                         argc -= 2;
213                 }
214                 goto finished;
215         }
216
217         while (1) {
218                 l1 = 1 + random() % max_length;
219                 l2 = 1 + random() % max_length;
220                 pstrcpy(mask,"\\masktest\\");
221                 pstrcpy(file,"\\masktest\\");
222                 l = strlen(mask);
223                 for (i=0;i<l1;i++) {
224                         mask[i+l] = maskchars[random() % mc_len];
225                 }
226                 mask[l+l1] = 0;
227
228                 for (i=0;i<l2;i++) {
229                         file[i+l] = filechars[random() % fc_len];
230                 }
231                 file[l+l2] = 0;
232
233                 if (ISDOT(file+l) || ISDOTDOT(file+l) || ISDOTDOT(mask+l)) {
234                         continue;
235                 }
236
237                 if (strspn(file+l, ".") == strlen(file+l)) continue;
238
239                 testpair(cli, mask, file);
240                 if (NumLoops && (--NumLoops == 0))
241                         break;
242         }
243
244  finished:
245         smbcli_rmdir(cli->tree, "\\masktest");
246 }
247
248
249 static void usage(void)
250 {
251         printf(
252 "Usage:\n\
253   masktest //server/share [options..]\n\
254   options:\n\
255         -d debuglevel\n\
256         -n numloops\n\
257         -W workgroup\n\
258         -U user%%pass\n\
259         -s seed\n\
260         -l max test length\n\
261         -M max protocol\n\
262         -f filechars (default %s)\n\
263         -m maskchars (default %s)\n\
264         -v                             verbose mode\n\
265         -E                             die on error\n\
266         -a                             show all tests\n\
267 \n\
268   This program tests wildcard matching between two servers. It generates\n\
269   random pairs of filenames/masks and tests that they match in the same\n\
270   way on the servers and internally\n\
271 ", 
272   filechars, maskchars);
273 }
274
275 /****************************************************************************
276   main program
277 ****************************************************************************/
278  int main(int argc,char *argv[])
279 {
280         char *share;
281         struct smbcli_state *cli;       
282         int opt;
283         int seed;
284
285         setlinebuf(stdout);
286
287         setup_logging("masktest", DEBUG_STDOUT);
288
289         lp_set_cmdline(global_loadparm, "log level", "0");
290
291         if (argc < 2 || argv[1][0] == '-') {
292                 usage();
293                 exit(1);
294         }
295
296         share = argv[1];
297
298         all_string_sub(share,"/","\\",0);
299
300         setup_logging(argv[0], DEBUG_STDOUT);
301
302         argc -= 1;
303         argv += 1;
304
305         lp_load(dyn_CONFIGFILE);
306
307         credentials = cli_credentials_init(talloc_autofree_context());
308         cli_credentials_guess(credentials);
309
310         seed = time(NULL);
311
312         init_iconv();
313
314         while ((opt = getopt(argc, argv, "n:d:U:s:hm:f:aoW:M:vEl:")) != EOF) {
315                 switch (opt) {
316                 case 'n':
317                         NumLoops = atoi(optarg);
318                         break;
319                 case 'd':
320                         DEBUGLEVEL = atoi(optarg);
321                         break;
322                 case 'E':
323                         die_on_error = 1;
324                         break;
325                 case 'v':
326                         verbose++;
327                         break;
328                 case 'M':
329                         lp_set_cmdline(global_loadparm, "max protocol", optarg);
330                         break;
331                 case 'U':
332                         cli_credentials_parse_string(credentials, optarg, CRED_SPECIFIED);
333                         break;
334                 case 's':
335                         seed = atoi(optarg);
336                         break;
337                 case 'h':
338                         usage();
339                         exit(1);
340                 case 'm':
341                         maskchars = optarg;
342                         break;
343                 case 'l':
344                         max_length = atoi(optarg);
345                         break;
346                 case 'f':
347                         filechars = optarg;
348                         break;
349                 case 'a':
350                         showall = 1;
351                         break;
352                 case 'o':
353                         old_list = True;
354                         break;
355                 default:
356                         printf("Unknown option %c (%d)\n", (char)opt, opt);
357                         exit(1);
358                 }
359         }
360
361         gensec_init();
362
363         argc -= optind;
364         argv += optind;
365
366         cli = connect_one(share);
367         if (!cli) {
368                 DEBUG(0,("Failed to connect to %s\n", share));
369                 exit(1);
370         }
371
372         /* need to init seed after connect as clientgen uses random numbers */
373         DEBUG(0,("seed=%d     format --- --- (server, correct)\n", seed));
374         srandom(seed);
375
376         test_mask(argc, argv, cli);
377
378         return(0);
379 }