summaryrefslogtreecommitdiff
path: root/source3/torture/test_nttrans_fsctl.c
blob: 963b20ef4b6a1c604b0a990686ab4c0140709f2a (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
/*
   Unix SMB/CIFS implementation.
   Basic test for NTTRANS FSCTL requests (copied from NTTRANS CREATE)
   Copyright (C) Richard Sharpe 2011
   Copyright (C) Volker Lendecke 2011

   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 "torture/proto.h"
#include "libsmb/libsmb.h"
#include "libcli/security/security.h"

bool run_nttrans_fsctl(int dummy)
{
	struct cli_state *cli = NULL;
	NTSTATUS status;
	bool ret = false;
	const char *fname = "fsctltest";
	uint16_t fnum;
	uint16_t setup[4];
	uint8_t *object_data = NULL;
	uint8_t *ranges = NULL;
	uint8_t range_data[16];
	uint8_t *param_data = NULL;
	uint8_t data[1] = { 0x1 };
	uint32_t rdata_size;
	uint32_t rparam_size;

	printf("Starting NTTRANS_FSCTL\n");

	if (!torture_open_connection(&cli, 0)) {
		printf("torture_open_connection failed\n");
		goto fail;
	}

	status = cli_nttrans_create(
		cli, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA|DELETE_ACCESS|
		READ_CONTROL_ACCESS,
		FILE_ATTRIBUTE_NORMAL,
		FILE_SHARE_READ|FILE_SHARE_WRITE| FILE_SHARE_DELETE,
		FILE_CREATE, 0, 0, NULL, NULL, 0, &fnum);
	if (!NT_STATUS_IS_OK(status)) {
		d_fprintf(stderr, "cli_nttrans_create returned %s\n",
			  nt_errstr(status));
		goto fail;
	}

	status = cli_nt_delete_on_close(cli, fnum, true);
	if (!NT_STATUS_IS_OK(status)) {
		d_fprintf(stderr, "cli_nt_delete_on_close returned %s\n",
			  nt_errstr(status));
		goto fail;
	}

	/* Fill in for FSCTL_SET_SPARSE and call cli_trans ... */
	SIVAL(setup, 0, FSCTL_SET_SPARSE); /* returns value */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   data, 1, 1,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   NULL, 0, NULL, /* rparam, min_rparam, num_rparam */
			   NULL, 0, NULL); /* rdata, ... */
	if (!NT_STATUS_IS_OK(status)) {
		d_fprintf(stderr, "cli_trans of FSCTL_SET_SPARSE returned %s instead of NT_STATUS_OK\n",
			nt_errstr(status));
		goto fail;
	}

	printf("FSCTL_SET_SPARSE returned correct status \n");

	/* Fill in for FSCTL_CREATE_OR_GET_OBJECT_ID and call cli_trans ... */
	SIVAL(setup, 0, FSCTL_CREATE_OR_GET_OBJECT_ID); /* returns value */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   NULL, 0, 64,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   &param_data, 0, &rparam_size, /* rparam, min_rparam, num_rparam */
			   &object_data, 0, &rdata_size); /* rdata, ... */
	if (!NT_STATUS_IS_OK(status)) {
		d_fprintf(stderr, "cli_trans of FSCTL_CREATE_OR_GET_OBJECT_ID returned %s instead of NT_STATUS_OK\n",
			nt_errstr(status));
		goto fail;
	}

	TALLOC_FREE(object_data);
	TALLOC_FREE(param_data);

	printf("FSCTL_CREATE_OR_GET_OBJECT_ID returned correct status \n");

	/* Fill in for FSCTL_GET_REPARSE_POINT and call cli_trans ... */
	SIVAL(setup, 0, FSCTL_GET_REPARSE_POINT); /* returns NOT A REPARSE POINT */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   NULL, 0, 0,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   NULL, 0, NULL, /* rparam, min_rparam, num_rparam */
			   NULL, 0, NULL); /* rdata, ... */
	if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_REPARSE_POINT)) {
		d_fprintf(stderr, "cli_trans of FSCTL_GET_REPARSE_POINT returned %s instead of NT_STATUS_NOT_A_REPARSE_POINT\n",
			nt_errstr(status));
		goto fail;
	}

	printf("FSCTL_GET_REPARSE_POINT returned correct status \n");

	/* Fill in for FSCTL_SET_REPARSE_POINT and call cli_trans ... */
	SIVAL(setup, 0, FSCTL_SET_REPARSE_POINT); /* returns NOT A REPARSE POINT */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   NULL, 0, 0,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   NULL, 0, NULL, /* rparam, min_rparam, num_rparam */
			   NULL, 0, NULL); /* rdata, ... */
	if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_REPARSE_POINT)) {
		d_fprintf(stderr, "cli_trans of FSCTL_SET_REPARSE_POINT returned %s instead of NT_STATUS_NOT_A_REPARSE_POINT\n",
			nt_errstr(status));
		goto fail;
	}

	printf("FSCTL_SET_REPARSE_POINT returned correct status \n");

	/* 
 	 * Fill in for FSCTL_GET_SHADOW_COPY_DATA and call cli_trans ... what
 	 * we do is send an invalid data length to provoke an INVALID PARAMETER
 	 * response.
 	 */
	SIVAL(setup, 0, FSCTL_GET_SHADOW_COPY_DATA); /* Should return IVN VAL */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	memset(range_data, 0, sizeof(range_data));  /* 0 and 0 */

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   NULL, 0, 8,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   NULL, 0, NULL, /* rparam, min_rparam, num_rparam */
			   &ranges, 0, &rdata_size); /* rdata, ... */
	if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
		d_fprintf(stderr, "cli_trans of FSCTL_QUERY_ALLOCATED_RANGES returned %s instead of NT_STATUS_INVALID_PARAMETER\n",
			nt_errstr(status));
		goto fail;
	}

	TALLOC_FREE(ranges);

	printf("FSCTL_GET_SHADOW_COPY_DATA returned correct status \n");
	/* 
	 * Fill in for FSCTL_FIND_FILES_BY and call cli_trans ... here we are
	 * only probing for its existence by provoking an INVALID PARAM
	 * response with a short and invalid SID in range_data
	 */
	SIVAL(setup, 0, FSCTL_FIND_FILES_BY_SID); /* Should return 16 bytes */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	memset(range_data, 0, sizeof(range_data));  /* 0 and 0 */

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   range_data, 4, 16,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   NULL, 0, NULL, /* rparam, min_rparam, num_rparam */
			   &ranges, 0, &rdata_size); /* rdata, ... */
	if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
		d_fprintf(stderr, "cli_trans of FSCTL_QUERY_ALLOCATED_RANGES returned %s instead of NT_STATUS_INVALID_PARAMETER\n",
			nt_errstr(status));
		goto fail;
	}

	printf("FSCTL_FIND_FILES_BY_SID returned correct status \n");

	/* Fill in for FSCTL_QUERY_ALLOCATED_RANGES and call cli_trans ... */
	SIVAL(setup, 0, FSCTL_QUERY_ALLOCATED_RANGES); /* Should return 16 bytes */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	memset(range_data, 0, sizeof(range_data));  /* 0 and 0 */

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   range_data, 16, 16,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   NULL, 0, NULL, /* rparam, min_rparam, num_rparam */
			   &ranges, 0, &rdata_size); /* rdata, ... */
	if (!NT_STATUS_IS_OK(status)) {
		d_fprintf(stderr, "cli_trans of FSCTL_QUERY_ALLOCATED_RANGES returned %s instead of NT_STATUS_OK\n",
			nt_errstr(status));
		goto fail;
	}

	TALLOC_FREE(ranges);

	printf("FSCTL_QUERY_ALLOCATED_RANGES returned correct status \n");

	/* Fill in for FSCTL_IS_VOLUME_DIRTY and call cli_trans ... */
	SIVAL(setup, 0, FSCTL_IS_VOLUME_DIRTY); /* Should return INVAL PARAM */
	SSVAL(setup, 4, fnum);
	SCVAL(setup, 6, 0x1);   /* It is an fsctl */
	SCVAL(setup, 7, 0x0);

	status = cli_trans(talloc_tos(), cli, SMBnttrans,
			   NULL, fnum,
			   NT_TRANSACT_IOCTL, 0,
			   setup, 4, 4,
			   NULL, 0, 0,    /* param, param_num, max_param */
			   NULL, 0, 0,    /* data, data_len, max_data */
			   NULL,          /* recv_flags2 */
			   NULL, 0, NULL, /* rsetup, min_rsetup, num_rsetup */
			   NULL, 0, NULL, /* rparam, min_rparam, num_rparam */
			   NULL, 0, NULL); /* rdata, ... */
	if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
		d_fprintf(stderr, "cli_trans of FSCTL_IS_VOLUME_DIRTY returned %s instead of NT_STATUS_INVALID_PARAMETER\n",
			nt_errstr(status));
		goto fail;
	}

	printf("FSCTL_IS_VOLUME_DIRTY returned correct status \n");

	ret = true;
fail:
	if (cli != NULL) {
		torture_close_connection(cli);
	}
	return ret;
}