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