r12542: Move some more prototypes out to seperate headers
[abartlet/samba.git/.git] / source4 / torture / basic / aliases.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB trans2 alias scanner
4    Copyright (C) Andrew Tridgell 2003
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 "dlinklist.h"
23 #include "libcli/raw/libcliraw.h"
24
25 int create_complex_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx, const char *fname);
26
27 struct trans2_blobs {
28         struct trans2_blobs *next, *prev;
29         uint16_t level;
30         DATA_BLOB params, data;
31 };
32
33 /* look for aliases for a query */
34 static void gen_aliases(struct smbcli_state *cli, struct smb_trans2 *t2, int level_offset)
35 {
36         TALLOC_CTX *mem_ctx;
37         uint16_t level;
38         struct trans2_blobs *alias_blobs = NULL;
39         struct trans2_blobs *t2b, *t2b2;
40         int count=0, alias_count=0;
41
42         mem_ctx = talloc_init("aliases");
43
44         for (level=0;level<2000;level++) {
45                 NTSTATUS status;
46
47                 SSVAL(t2->in.params.data, level_offset, level);
48                 
49                 status = smb_raw_trans2(cli->tree, mem_ctx, t2);
50                 if (!NT_STATUS_IS_OK(status)) continue;
51
52                 t2b = talloc(mem_ctx, struct trans2_blobs);
53                 t2b->level = level;
54                 t2b->params = t2->out.params;
55                 t2b->data = t2->out.data;
56                 DLIST_ADD(alias_blobs, t2b);
57                 d_printf("\tFound level %4u (0x%03x) of size %3d (0x%02x)\n", 
58                          level, level,
59                          (int)t2b->data.length, (int)t2b->data.length);
60                 count++;
61         }
62
63         d_printf("Found %d levels with success status\n", count);
64
65         for (t2b=alias_blobs; t2b; t2b=t2b->next) {
66                 for (t2b2=alias_blobs; t2b2; t2b2=t2b2->next) {
67                         if (t2b->level >= t2b2->level) continue;
68                         if (data_blob_equal(&t2b->params, &t2b2->params) &&
69                             data_blob_equal(&t2b->data, &t2b2->data)) {
70                                 printf("\tLevel %u (0x%x) and level %u (0x%x) are possible aliases\n", 
71                                        t2b->level, t2b->level, t2b2->level, t2b2->level);
72                                 alias_count++;
73                         }
74                 }
75         }
76
77         d_printf("Found %d aliased levels\n", alias_count);
78         
79         talloc_free(mem_ctx);
80 }
81
82 /* look for qfsinfo aliases */
83 static void qfsinfo_aliases(struct smbcli_state *cli)
84 {
85         struct smb_trans2 t2;
86         uint16_t setup = TRANSACT2_QFSINFO;
87
88         d_printf("\nChecking for QFSINFO aliases\n");
89
90         t2.in.max_param = 0;
91         t2.in.max_data = smb_raw_max_trans_data(cli->tree, 0);
92         t2.in.max_setup = 0;
93         t2.in.flags = 0;
94         t2.in.timeout = 0;
95         t2.in.setup_count = 1;
96         t2.in.setup = &setup;
97         t2.in.params = data_blob(NULL, 2);
98         t2.in.data = data_blob(NULL, 0);
99
100         gen_aliases(cli, &t2, 0);
101 }
102
103 /* look for qfileinfo aliases */
104 static void qfileinfo_aliases(struct smbcli_state *cli)
105 {
106         struct smb_trans2 t2;
107         uint16_t setup = TRANSACT2_QFILEINFO;
108         const char *fname = "\\qfileinfo_aliases.txt";
109         int fnum;
110
111         d_printf("\nChecking for QFILEINFO aliases\n");
112
113         t2.in.max_param = 2;
114         t2.in.max_data = smb_raw_max_trans_data(cli->tree, 2);
115         t2.in.max_setup = 0;
116         t2.in.flags = 0;
117         t2.in.timeout = 0;
118         t2.in.setup_count = 1;
119         t2.in.setup = &setup;
120         t2.in.params = data_blob(NULL, 4);
121         t2.in.data = data_blob(NULL, 0);
122
123         smbcli_unlink(cli->tree, fname);
124         fnum = create_complex_file(cli, cli, fname);
125         if (fnum == -1) {
126                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
127         }
128
129         smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
130
131         SSVAL(t2.in.params.data, 0, fnum);
132
133         gen_aliases(cli, &t2, 2);
134
135         smbcli_close(cli->tree, fnum);
136         smbcli_unlink(cli->tree, fname);
137 }
138
139
140 /* look for qpathinfo aliases */
141 static void qpathinfo_aliases(struct smbcli_state *cli)
142 {
143         struct smb_trans2 t2;
144         uint16_t setup = TRANSACT2_QPATHINFO;
145         const char *fname = "\\qpathinfo_aliases.txt";
146         int fnum;
147         TALLOC_CTX *mem_ctx;
148
149         mem_ctx = talloc_init("qpathinfo");
150
151         d_printf("\nChecking for QPATHINFO aliases\n");
152
153         t2.in.max_param = 2;
154         t2.in.max_data = smb_raw_max_trans_data(cli->tree, 2);
155         t2.in.max_setup = 0;
156         t2.in.flags = 0;
157         t2.in.timeout = 0;
158         t2.in.setup_count = 1;
159         t2.in.setup = &setup;
160         t2.in.params = data_blob_talloc(mem_ctx, NULL, 6);
161         t2.in.data = data_blob(NULL, 0);
162
163         smbcli_unlink(cli->tree, fname);
164         fnum = create_complex_file(cli, cli, fname);
165         if (fnum == -1) {
166                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
167         }
168
169         smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
170         smbcli_close(cli->tree, fnum);
171
172         SIVAL(t2.in.params.data, 2, 0);
173
174         smbcli_blob_append_string(cli->session, mem_ctx, &t2.in.params, 
175                                fname, STR_TERMINATE);
176
177         gen_aliases(cli, &t2, 0);
178
179         smbcli_unlink(cli->tree, fname);
180         talloc_free(mem_ctx);
181 }
182
183
184 /* look for trans2 findfirst aliases */
185 static void findfirst_aliases(struct smbcli_state *cli)
186 {
187         struct smb_trans2 t2;
188         uint16_t setup = TRANSACT2_FINDFIRST;
189         const char *fname = "\\findfirst_aliases.txt";
190         int fnum;
191         TALLOC_CTX *mem_ctx;
192
193         mem_ctx = talloc_init("findfirst");
194
195         d_printf("\nChecking for FINDFIRST aliases\n");
196
197         t2.in.max_param = 16;
198         t2.in.max_data = smb_raw_max_trans_data(cli->tree, 16);
199         t2.in.max_setup = 0;
200         t2.in.flags = 0;
201         t2.in.timeout = 0;
202         t2.in.setup_count = 1;
203         t2.in.setup = &setup;
204         t2.in.params = data_blob_talloc(mem_ctx, NULL, 12);
205         t2.in.data = data_blob(NULL, 0);
206
207         smbcli_unlink(cli->tree, fname);
208         fnum = create_complex_file(cli, cli, fname);
209         if (fnum == -1) {
210                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
211         }
212
213         smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
214         smbcli_close(cli->tree, fnum);
215
216         SSVAL(t2.in.params.data, 0, 0);
217         SSVAL(t2.in.params.data, 2, 1);
218         SSVAL(t2.in.params.data, 4, FLAG_TRANS2_FIND_CLOSE);
219         SSVAL(t2.in.params.data, 6, 0);
220         SIVAL(t2.in.params.data, 8, 0);
221
222         smbcli_blob_append_string(cli->session, mem_ctx, &t2.in.params, 
223                                fname, STR_TERMINATE);
224
225         gen_aliases(cli, &t2, 6);
226
227         smbcli_unlink(cli->tree, fname);
228         talloc_free(mem_ctx);
229 }
230
231
232
233 /* look for aliases for a set function */
234 static void gen_set_aliases(struct smbcli_state *cli, struct smb_trans2 *t2, int level_offset)
235 {
236         TALLOC_CTX *mem_ctx;
237         uint16_t level;
238         struct trans2_blobs *alias_blobs = NULL;
239         struct trans2_blobs *t2b;
240         int count=0, dsize;
241
242         mem_ctx = talloc_init("aliases");
243
244         for (level=1;level<1100;level++) {
245                 NTSTATUS status, status1;
246                 SSVAL(t2->in.params.data, level_offset, level);
247
248                 status1 = NT_STATUS_OK;
249
250                 for (dsize=2; dsize<1024; dsize += 2) {
251                         data_blob_free(&t2->in.data);
252                         t2->in.data = data_blob(NULL, dsize);
253                         data_blob_clear(&t2->in.data);
254                         status = smb_raw_trans2(cli->tree, mem_ctx, t2);
255                         /* some error codes mean that this whole level doesn't exist */
256                         if (NT_STATUS_EQUAL(NT_STATUS_INVALID_LEVEL, status) ||
257                             NT_STATUS_EQUAL(NT_STATUS_INVALID_INFO_CLASS, status) ||
258                             NT_STATUS_EQUAL(NT_STATUS_NOT_SUPPORTED, status)) {
259                                 break;
260                         }
261                         if (NT_STATUS_IS_OK(status)) break;
262
263                         /* invalid parameter means that the level exists at this 
264                            size, but the contents are wrong (not surprising with
265                            all zeros!) */
266                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) break;
267
268                         /* this is the usual code for 'wrong size' */
269                         if (NT_STATUS_EQUAL(status, NT_STATUS_INFO_LENGTH_MISMATCH)) {
270                                 continue;
271                         }
272
273                         if (!NT_STATUS_EQUAL(status, status1)) {
274                                 printf("level=%d size=%d %s\n", level, dsize, nt_errstr(status));
275                         }
276                         status1 = status;
277                 }
278
279                 if (!NT_STATUS_IS_OK(status) &&
280                     !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) continue;
281
282                 t2b = talloc(mem_ctx, struct trans2_blobs);
283                 t2b->level = level;
284                 t2b->params = t2->out.params;
285                 t2b->data = t2->out.data;
286                 DLIST_ADD(alias_blobs, t2b);
287                 d_printf("\tFound level %4u (0x%03x) of size %3d (0x%02x)\n", 
288                          level, level,
289                          (int)t2->in.data.length, (int)t2->in.data.length);
290                 count++;
291         }
292
293         d_printf("Found %d valid levels\n", count);
294         talloc_free(mem_ctx);
295 }
296
297
298
299 /* look for setfileinfo aliases */
300 static void setfileinfo_aliases(struct smbcli_state *cli)
301 {
302         struct smb_trans2 t2;
303         uint16_t setup = TRANSACT2_SETFILEINFO;
304         const char *fname = "\\setfileinfo_aliases.txt";
305         int fnum;
306
307         d_printf("\nChecking for SETFILEINFO aliases\n");
308
309         t2.in.max_param = 2;
310         t2.in.max_data = 0;
311         t2.in.max_setup = 0;
312         t2.in.flags = 0;
313         t2.in.timeout = 0;
314         t2.in.setup_count = 1;
315         t2.in.setup = &setup;
316         t2.in.params = data_blob(NULL, 6);
317         t2.in.data = data_blob(NULL, 0);
318
319         smbcli_unlink(cli->tree, fname);
320         fnum = create_complex_file(cli, cli, fname);
321         if (fnum == -1) {
322                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
323         }
324
325         smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
326
327         SSVAL(t2.in.params.data, 0, fnum);
328         SSVAL(t2.in.params.data, 4, 0);
329
330         gen_set_aliases(cli, &t2, 2);
331
332         smbcli_close(cli->tree, fnum);
333         smbcli_unlink(cli->tree, fname);
334 }
335
336 /* look for setpathinfo aliases */
337 static void setpathinfo_aliases(struct smbcli_state *cli)
338 {
339         struct smb_trans2 t2;
340         uint16_t setup = TRANSACT2_SETPATHINFO;
341         const char *fname = "\\setpathinfo_aliases.txt";
342         int fnum;
343         TALLOC_CTX *mem_ctx;
344
345         mem_ctx = talloc_init("findfirst");
346
347         d_printf("\nChecking for SETPATHINFO aliases\n");
348
349         t2.in.max_param = 32;
350         t2.in.max_data = smb_raw_max_trans_data(cli->tree, 32);
351         t2.in.max_setup = 0;
352         t2.in.flags = 0;
353         t2.in.timeout = 0;
354         t2.in.setup_count = 1;
355         t2.in.setup = &setup;
356         t2.in.params = data_blob_talloc(mem_ctx, NULL, 4);
357         t2.in.data = data_blob(NULL, 0);
358
359         smbcli_unlink(cli->tree, fname);
360
361         fnum = create_complex_file(cli, cli, fname);
362         if (fnum == -1) {
363                 printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
364         }
365
366         smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
367         smbcli_close(cli->tree, fnum);
368
369         SSVAL(t2.in.params.data, 2, 0);
370
371         smbcli_blob_append_string(cli->session, mem_ctx, &t2.in.params, 
372                                fname, STR_TERMINATE);
373
374         gen_set_aliases(cli, &t2, 0);
375
376         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
377                 printf("unlink: %s\n", smbcli_errstr(cli->tree));
378         }
379         talloc_free(mem_ctx);
380 }
381
382
383 /* look for aliased info levels in trans2 calls */
384 BOOL torture_trans2_aliases(void)
385 {
386         struct smbcli_state *cli;
387
388         if (!torture_open_connection(&cli)) {
389                 return False;
390         }
391
392
393         qfsinfo_aliases(cli);
394         qfileinfo_aliases(cli);
395         qpathinfo_aliases(cli);
396         findfirst_aliases(cli);
397         setfileinfo_aliases(cli);
398         setpathinfo_aliases(cli);
399
400         if (!torture_close_connection(cli)) {
401                 return False;
402         }
403
404         return True;
405 }