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