summaryrefslogtreecommitdiffstats
path: root/source4/torture/basic/aliases.c
blob: 660009704b9f44515a885847e8cd80f7f443422a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* 
   Unix SMB/CIFS implementation.
   SMB trans2 alias scanner
   Copyright (C) Andrew Tridgell 2003
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "lib/util/dlinklist.h"
#include "libcli/raw/libcliraw.h"
#include "torture/torture.h"
#include "libcli/libcli.h"
#include "torture/util.h"

int create_complex_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx, const char *fname);

struct trans2_blobs {
	struct trans2_blobs *next, *prev;
	uint16_t level;
	DATA_BLOB params, data;
};

/* look for aliases for a query */
static bool gen_aliases(struct torture_context *tctx, 
						struct smbcli_state *cli, struct smb_trans2 *t2, 
						int level_offset)
{
	uint16_t level;
	struct trans2_blobs *alias_blobs = NULL;
	struct trans2_blobs *t2b, *t2b2;
	int count=0, alias_count=0;

	for (level=0;level<2000;level++) {
		NTSTATUS status;

		SSVAL(t2->in.params.data, level_offset, level);
		
		status = smb_raw_trans2(cli->tree, tctx, t2);
		if (!NT_STATUS_IS_OK(status)) continue;

		t2b = talloc(tctx, struct trans2_blobs);
		t2b->level = level;
		t2b->params = t2->out.params;
		t2b->data = t2->out.data;
		DLIST_ADD(alias_blobs, t2b);
		torture_comment(tctx, 
						"\tFound level %4u (0x%03x) of size %3d (0x%02x)\n", 
			 level, level,
			 (int)t2b->data.length, (int)t2b->data.length);
		count++;
	}

	torture_comment(tctx, "Found %d levels with success status\n", count);

	for (t2b=alias_blobs; t2b; t2b=t2b->next) {
		for (t2b2=alias_blobs; t2b2; t2b2=t2b2->next) {
			if (t2b->level >= t2b2->level) continue;
			if (data_blob_cmp(&t2b->params, &t2b2->params) == 0 &&
			    data_blob_cmp(&t2b->data, &t2b2->data) == 0) {
				torture_comment(tctx, 
				"\tLevel %u (0x%x) and level %u (0x%x) are possible aliases\n", 
				       t2b->level, t2b->level, t2b2->level, t2b2->level);
				alias_count++;
			}
		}
	}

	torture_comment(tctx, "Found %d aliased levels\n", alias_count);

	return true;
}

/* look for qfsinfo aliases */
static bool qfsinfo_aliases(struct torture_context *tctx, struct smbcli_state *cli)
{
	struct smb_trans2 t2;
	uint16_t setup = TRANSACT2_QFSINFO;

	t2.in.max_param = 0;
	t2.in.max_data = smb_raw_max_trans_data(cli->tree, 0);
	t2.in.max_setup = 0;
	t2.in.flags = 0;
	t2.in.timeout = 0;
	t2.in.setup_count = 1;
	t2.in.setup = &setup;
	t2.in.params = data_blob_talloc_zero(tctx, 2);
	t2.in.data = data_blob(NULL, 0);
	ZERO_STRUCT(t2.out);

	return gen_aliases(tctx, cli, &t2, 0);
}

/* look for qfileinfo aliases */
static bool qfileinfo_aliases(struct torture_context *tctx, struct smbcli_state *cli)
{
	struct smb_trans2 t2;
	uint16_t setup = TRANSACT2_QFILEINFO;
	const char *fname = "\\qfileinfo_aliases.txt";
	int fnum;

	t2.in.max_param = 2;
	t2.in.max_data = smb_raw_max_trans_data(cli->tree, 2);
	t2.in.max_setup = 0;
	t2.in.flags = 0;
	t2.in.timeout = 0;
	t2.in.setup_count = 1;
	t2.in.setup = &setup;
	t2.in.params = data_blob_talloc_zero(tctx, 4);
	t2.in.data = data_blob(NULL, 0);
	ZERO_STRUCT(t2.out);

	smbcli_unlink(cli->tree, fname);
	fnum = create_complex_file(cli, cli, fname);
	torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, 
					"open of %s failed (%s)", fname, 
				   smbcli_errstr(cli->tree)));

	smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));

	SSVAL(t2.in.params.data, 0, fnum);

	if (!gen_aliases(tctx, cli, &t2, 2))
		return false;

	smbcli_close(cli->tree, fnum);
	smbcli_unlink(cli->tree, fname);

	return true;
}


/* look for qpathinfo aliases */
static bool qpathinfo_aliases(struct torture_context *tctx, struct smbcli_state *cli)
{
	struct smb_trans2 t2;
	uint16_t setup = TRANSACT2_QPATHINFO;
	const char *fname = "\\qpathinfo_aliases.txt";
	int fnum;

	t2.in.max_param = 2;
	t2.in.max_data = smb_raw_max_trans_data(cli->tree, 2);
	t2.in.max_setup = 0;
	t2.in.flags = 0;
	t2.in.timeout = 0;
	t2.in.setup_count = 1;
	t2.in.setup = &setup;
	t2.in.params = data_blob_talloc_zero(tctx, 6);
	t2.in.data = data_blob(NULL, 0);
	ZERO_STRUCT(t2.out);

	smbcli_unlink(cli->tree, fname);
	fnum = create_complex_file(cli, cli, fname);
	torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, 
					"open of %s failed (%s)", fname, 
				   smbcli_errstr(cli->tree)));

	smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
	smbcli_close(cli->tree, fnum);

	SIVAL(t2.in.params.data, 2, 0);

	smbcli_blob_append_string(cli->session, tctx, &t2.in.params, 
			       fname, STR_TERMINATE);

	if (!gen_aliases(tctx, cli, &t2, 0))
		return false;

	smbcli_unlink(cli->tree, fname);

	return true;
}


/* look for trans2 findfirst aliases */
static bool findfirst_aliases(struct torture_context *tctx, struct smbcli_state *cli)
{
	struct smb_trans2 t2;
	uint16_t setup = TRANSACT2_FINDFIRST;
	const char *fname = "\\findfirst_aliases.txt";
	int fnum;

	t2.in.max_param = 16;
	t2.in.max_data = smb_raw_max_trans_data(cli->tree, 16);
	t2.in.max_setup = 0;
	t2.in.flags = 0;
	t2.in.timeout = 0;
	t2.in.setup_count = 1;
	t2.in.setup = &setup;
	t2.in.params = data_blob_talloc_zero(tctx, 12);
	t2.in.data = data_blob(NULL, 0);
	ZERO_STRUCT(t2.out);

	smbcli_unlink(cli->tree, fname);
	fnum = create_complex_file(cli, cli, fname);
	torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, 
					"open of %s failed (%s)", fname, 
				   smbcli_errstr(cli->tree)));

	smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
	smbcli_close(cli->tree, fnum);

	SSVAL(t2.in.params.data, 0, 0);
	SSVAL(t2.in.params.data, 2, 1);
	SSVAL(t2.in.params.data, 4, FLAG_TRANS2_FIND_CLOSE);
	SSVAL(t2.in.params.data, 6, 0);
	SIVAL(t2.in.params.data, 8, 0);

	smbcli_blob_append_string(cli->session, tctx, &t2.in.params, 
			       fname, STR_TERMINATE);

	if (!gen_aliases(tctx, cli, &t2, 6))
		return false;

	smbcli_unlink(cli->tree, fname);

	return true;
}



/* look for aliases for a set function */
static bool gen_set_aliases(struct torture_context *tctx, 
							struct smbcli_state *cli, 
							struct smb_trans2 *t2, int level_offset)
{
	uint16_t level;
	struct trans2_blobs *alias_blobs = NULL;
	struct trans2_blobs *t2b;
	int count=0, dsize;

	for (level=1;level<1100;level++) {
		NTSTATUS status, status1;
		SSVAL(t2->in.params.data, level_offset, level);

		status1 = NT_STATUS_OK;

		for (dsize=2; dsize<1024; dsize += 2) {
			data_blob_free(&t2->in.data);
			t2->in.data = data_blob(NULL, dsize);
			data_blob_clear(&t2->in.data);
			status = smb_raw_trans2(cli->tree, tctx, t2);
			/* some error codes mean that this whole level doesn't exist */
			if (NT_STATUS_EQUAL(NT_STATUS_INVALID_LEVEL, status) ||
			    NT_STATUS_EQUAL(NT_STATUS_INVALID_INFO_CLASS, status) ||
			    NT_STATUS_EQUAL(NT_STATUS_NOT_SUPPORTED, status)) {
				break;
			}
			if (NT_STATUS_IS_OK(status)) break;

			/* invalid parameter means that the level exists at this 
			   size, but the contents are wrong (not surprising with
			   all zeros!) */
			if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) break;

			/* this is the usual code for 'wrong size' */
			if (NT_STATUS_EQUAL(status, NT_STATUS_INFO_LENGTH_MISMATCH)) {
				continue;
			}

			if (!NT_STATUS_EQUAL(status, status1)) {
				torture_comment(tctx, "level=%d size=%d %s\n", level, dsize, nt_errstr(status));
			}
			status1 = status;
		}

		if (!NT_STATUS_IS_OK(status) &&
		    !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) continue;

		t2b = talloc(tctx, struct trans2_blobs);
		t2b->level = level;
		t2b->params = t2->out.params;
		t2b->data = t2->out.data;
		DLIST_ADD(alias_blobs, t2b);
		torture_comment(tctx, 
					"\tFound level %4u (0x%03x) of size %3d (0x%02x)\n", 
			 level, level,
			 (int)t2->in.data.length, (int)t2->in.data.length);
		count++;
	}

	torture_comment(tctx, "Found %d valid levels\n", count);

	return true;
}



/* look for setfileinfo aliases */
static bool setfileinfo_aliases(struct torture_context *tctx, struct smbcli_state *cli)
{
	struct smb_trans2 t2;
	uint16_t setup = TRANSACT2_SETFILEINFO;
	const char *fname = "\\setfileinfo_aliases.txt";
	int fnum;

	t2.in.max_param = 2;
	t2.in.max_data = 0;
	t2.in.max_setup = 0;
	t2.in.flags = 0;
	t2.in.timeout = 0;
	t2.in.setup_count = 1;
	t2.in.setup = &setup;
	t2.in.params = data_blob_talloc_zero(tctx, 6);
	t2.in.data = data_blob(NULL, 0);
	ZERO_STRUCT(t2.out);

	smbcli_unlink(cli->tree, fname);
	fnum = create_complex_file(cli, cli, fname);
	torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, 
				   "open of %s failed (%s)", fname, 
				   smbcli_errstr(cli->tree)));

	smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));

	SSVAL(t2.in.params.data, 0, fnum);
	SSVAL(t2.in.params.data, 4, 0);

	gen_set_aliases(tctx, cli, &t2, 2);

	smbcli_close(cli->tree, fnum);
	smbcli_unlink(cli->tree, fname);

	return true;
}

/* look for setpathinfo aliases */
static bool setpathinfo_aliases(struct torture_context *tctx, 
								struct smbcli_state *cli)
{
	struct smb_trans2 t2;
	uint16_t setup = TRANSACT2_SETPATHINFO;
	const char *fname = "\\setpathinfo_aliases.txt";
	int fnum;

	t2.in.max_param = 32;
	t2.in.max_data = smb_raw_max_trans_data(cli->tree, 32);
	t2.in.max_setup = 0;
	t2.in.flags = 0;
	t2.in.timeout = 0;
	t2.in.setup_count = 1;
	t2.in.setup = &setup;
	t2.in.params = data_blob_talloc_zero(tctx, 4);
	t2.in.data = data_blob(NULL, 0);
	ZERO_STRUCT(t2.out);

	smbcli_unlink(cli->tree, fname);

	fnum = create_complex_file(cli, cli, fname);
	torture_assert(tctx, fnum != -1, talloc_asprintf(tctx, 
					"open of %s failed (%s)", fname, 
				   smbcli_errstr(cli->tree)));

	smbcli_write(cli->tree, fnum, 0, &t2, 0, sizeof(t2));
	smbcli_close(cli->tree, fnum);

	SSVAL(t2.in.params.data, 2, 0);

	smbcli_blob_append_string(cli->session, tctx, &t2.in.params, 
			       fname, STR_TERMINATE);

	if (!gen_set_aliases(tctx, cli, &t2, 0))
		return false;

	torture_assert_ntstatus_ok(tctx, smbcli_unlink(cli->tree, fname),
		talloc_asprintf(tctx, "unlink: %s", smbcli_errstr(cli->tree)));

	return true;
}


/* look for aliased info levels in trans2 calls */
struct torture_suite *torture_trans2_aliases(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx, "ALIASES");

	torture_suite_add_1smb_test(suite, "QFILEINFO aliases", qfsinfo_aliases);
	torture_suite_add_1smb_test(suite, "QFSINFO aliases", qfileinfo_aliases);
	torture_suite_add_1smb_test(suite, "QPATHINFO aliases", qpathinfo_aliases);
	torture_suite_add_1smb_test(suite, "FINDFIRST aliases", findfirst_aliases);
	torture_suite_add_1smb_test(suite, "setfileinfo_aliases", setfileinfo_aliases);
	torture_suite_add_1smb_test(suite, "setpathinfo_aliases", setpathinfo_aliases);

	return suite;
}