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