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