1 /*
2    Unix SMB/CIFS implementation.
3 
4    libcli composite function testing
5 
6    Copyright (C) Andrew Tridgell 2005
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "lib/events/events.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/libcli.h"
28 #include "libcli/security/security.h"
29 #include "libcli/composite/composite.h"
30 #include "libcli/smb_composite/smb_composite.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "lib/cmdline/popt_common.h"
33 #include "torture/util.h"
34 
35 #define BASEDIR "\\composite"
36 
loadfile_complete(struct composite_context * c)37 static void loadfile_complete(struct composite_context *c)
38 {
39 	int *count = talloc_get_type(c->async.private_data, int);
40 	(*count)++;
41 }
42 
43 /*
44   test a simple savefile/loadfile combination
45 */
test_loadfile(struct smbcli_state * cli,TALLOC_CTX * mem_ctx)46 static BOOL test_loadfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
47 {
48 	const char *fname = BASEDIR "\\test.txt";
49 	NTSTATUS status;
50 	struct smb_composite_savefile io1;
51 	struct smb_composite_loadfile io2;
52 	struct composite_context **c;
53 	uint8_t *data;
54 	size_t len = random() % 100000;
55 	const int num_ops = 50;
56 	int i;
57 	int *count = talloc_zero(mem_ctx, int);
58 
59 	data = talloc_array(mem_ctx, uint8_t, len);
60 
61 	generate_random_buffer(data, len);
62 
63 	io1.in.fname = fname;
64 	io1.in.data  = data;
65 	io1.in.size  = len;
66 
67 	printf("testing savefile\n");
68 
69 	status = smb_composite_savefile(cli->tree, &io1);
70 	if (!NT_STATUS_IS_OK(status)) {
71 		printf("savefile failed: %s\n", nt_errstr(status));
72 		return False;
73 	}
74 
75 	io2.in.fname = fname;
76 
77 	printf("testing parallel loadfile with %d ops\n", num_ops);
78 
79 	c = talloc_array(mem_ctx, struct composite_context *, num_ops);
80 
81 	for (i=0;i<num_ops;i++) {
82 		c[i] = smb_composite_loadfile_send(cli->tree, &io2);
83 		c[i]->async.fn = loadfile_complete;
84 		c[i]->async.private_data = count;
85 	}
86 
87 	printf("waiting for completion\n");
88 	while (*count != num_ops) {
89 		event_loop_once(cli->transport->socket->event.ctx);
90 		printf("count=%d\r", *count);
91 		fflush(stdout);
92 	}
93 	printf("count=%d\n", *count);
94 
95 	for (i=0;i<num_ops;i++) {
96 		status = smb_composite_loadfile_recv(c[i], mem_ctx);
97 		if (!NT_STATUS_IS_OK(status)) {
98 			printf("loadfile[%d] failed - %s\n", i, nt_errstr(status));
99 			return False;
100 		}
101 
102 		if (io2.out.size != len) {
103 			printf("wrong length in returned data - %d should be %d\n",
104 			       io2.out.size, (int)len);
105 			return False;
106 		}
107 
108 		if (memcmp(io2.out.data, data, len) != 0) {
109 			printf("wrong data in loadfile!\n");
110 			return False;
111 		}
112 	}
113 
114 	talloc_free(data);
115 
116 	return True;
117 }
118 
119 /*
120   test a simple savefile/loadfile combination
121 */
test_fetchfile(struct smbcli_state * cli,TALLOC_CTX * mem_ctx)122 static BOOL test_fetchfile(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
123 {
124 	const char *fname = BASEDIR "\\test.txt";
125 	NTSTATUS status;
126 	struct smb_composite_savefile io1;
127 	struct smb_composite_fetchfile io2;
128 	struct composite_context **c;
129 	uint8_t *data;
130 	int i;
131 	size_t len = random() % 10000;
132 	extern int torture_numops;
133 	struct event_context *event_ctx;
134 	int *count = talloc_zero(mem_ctx, int);
135 	BOOL ret = True;
136 
137 	data = talloc_array(mem_ctx, uint8_t, len);
138 
139 	generate_random_buffer(data, len);
140 
141 	io1.in.fname = fname;
142 	io1.in.data  = data;
143 	io1.in.size  = len;
144 
145 	printf("testing savefile\n");
146 
147 	status = smb_composite_savefile(cli->tree, &io1);
148 	if (!NT_STATUS_IS_OK(status)) {
149 		printf("savefile failed: %s\n", nt_errstr(status));
150 		return False;
151 	}
152 
153 	io2.in.dest_host = lp_parm_string(-1, "torture", "host");
154 	io2.in.port = 0;
155 	io2.in.called_name = lp_parm_string(-1, "torture", "host");
156 	io2.in.service = lp_parm_string(-1, "torture", "share");
157 	io2.in.service_type = "A:";
158 
159 	io2.in.credentials = cmdline_credentials;
160 	io2.in.workgroup  = lp_workgroup();
161 	io2.in.filename = fname;
162 
163 	printf("testing parallel fetchfile with %d ops\n", torture_numops);
164 
165 	event_ctx = event_context_init(mem_ctx);
166 	c = talloc_array(mem_ctx, struct composite_context *, torture_numops);
167 
168 	for (i=0; i<torture_numops; i++) {
169 		c[i] = smb_composite_fetchfile_send(&io2, event_ctx);
170 		c[i]->async.fn = loadfile_complete;
171 		c[i]->async.private_data = count;
172 	}
173 
174 	printf("waiting for completion\n");
175 
176 	while (*count != torture_numops) {
177 		event_loop_once(event_ctx);
178 		printf("count=%d\r", *count);
179 		fflush(stdout);
180 	}
181 	printf("count=%d\n", *count);
182 
183 	for (i=0;i<torture_numops;i++) {
184 		status = smb_composite_fetchfile_recv(c[i], mem_ctx);
185 		if (!NT_STATUS_IS_OK(status)) {
186 			printf("loadfile[%d] failed - %s\n", i,
187 			       nt_errstr(status));
188 			ret = False;
189 			continue;
190 		}
191 
192 		if (io2.out.size != len) {
193 			printf("wrong length in returned data - %d "
194 			       "should be %d\n",
195 			       io2.out.size, (int)len);
196 			ret = False;
197 			continue;
198 		}
199 
200 		if (memcmp(io2.out.data, data, len) != 0) {
201 			printf("wrong data in loadfile!\n");
202 			ret = False;
203 			continue;
204 		}
205 	}
206 
207 	return ret;
208 }
209 
210 /*
211   test setfileacl
212 */
test_appendacl(struct smbcli_state * cli,TALLOC_CTX * mem_ctx)213 static BOOL test_appendacl(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
214 {
215 	struct smb_composite_appendacl **io;
216 	struct smb_composite_appendacl **io_orig;
217 	struct composite_context **c;
218 	struct event_context *event_ctx;
219 
220 	struct security_descriptor *test_sd;
221 	struct security_ace *ace;
222 	struct dom_sid *test_sid;
223 
224 	const int num_ops = 50;
225 	int *count = talloc_zero(mem_ctx, int);
226 	struct smb_composite_savefile io1;
227 
228 	NTSTATUS status;
229 	int i;
230 
231 	io_orig = talloc_array(mem_ctx, struct smb_composite_appendacl *, num_ops);
232 
233 	printf ("creating %d empty files and getting their acls with appendacl\n", num_ops);
234 
235 	for (i = 0; i < num_ops; i++) {
236 		io1.in.fname = talloc_asprintf(io_orig, BASEDIR "\\test%d.txt", i);
237 		io1.in.data  = NULL;
238 		io1.in.size  = 0;
239 
240 		status = smb_composite_savefile(cli->tree, &io1);
241 		if (!NT_STATUS_IS_OK(status)) {
242 			printf("savefile failed: %s\n", nt_errstr(status));
243 			return False;
244 		}
245 
246 		io_orig[i] = talloc (io_orig, struct smb_composite_appendacl);
247 		io_orig[i]->in.fname = talloc_steal(io_orig[i], io1.in.fname);
248 		io_orig[i]->in.sd = security_descriptor_initialise(io_orig[i]);
249 		status = smb_composite_appendacl(cli->tree, io_orig[i], io_orig[i]);
250 		if (!NT_STATUS_IS_OK(status)) {
251 			printf("appendacl failed: %s\n", nt_errstr(status));
252 			return False;
253 		}
254 	}
255 
256 
257 	/* fill Security Descriptor with aces to be added */
258 
259 	test_sd = security_descriptor_initialise(mem_ctx);
260 	test_sid = dom_sid_parse_talloc (mem_ctx, "S-1-5-32-1234-5432");
261 
262 	ace = talloc_zero(mem_ctx, struct security_ace);
263 
264 	ace->type = SEC_ACE_TYPE_ACCESS_ALLOWED;
265 	ace->flags = 0;
266 	ace->access_mask = SEC_STD_ALL;
267 	ace->trustee = *test_sid;
268 
269 	status = security_descriptor_dacl_add(test_sd, ace);
270 	if (!NT_STATUS_IS_OK(status)) {
271 		printf("appendacl failed: %s\n", nt_errstr(status));
272 		return False;
273 	}
274 
275 	/* set parameters for appendacl async call */
276 
277 	printf("testing parallel appendacl with %d ops\n", num_ops);
278 
279 	c = talloc_array(mem_ctx, struct composite_context *, num_ops);
280 	io = talloc_array(mem_ctx, struct  smb_composite_appendacl *, num_ops);
281 
282 	for (i=0; i < num_ops; i++) {
283 		io[i] = talloc (io, struct smb_composite_appendacl);
284 		io[i]->in.sd = test_sd;
285 		io[i]->in.fname = talloc_asprintf(io[i], BASEDIR "\\test%d.txt", i);
286 
287 		c[i] = smb_composite_appendacl_send(cli->tree, io[i]);
288 		c[i]->async.fn = loadfile_complete;
289 		c[i]->async.private_data = count;
290 	}
291 
292 	event_ctx = talloc_reference(mem_ctx, cli->tree->session->transport->socket->event.ctx);
293 	printf("waiting for completion\n");
294 	while (*count != num_ops) {
295 		event_loop_once(event_ctx);
296 		printf("count=%d\r", *count);
297 		fflush(stdout);
298 	}
299 	printf("count=%d\n", *count);
300 
301 	for (i=0; i < num_ops; i++) {
302 		status = smb_composite_appendacl_recv(c[i], io[i]);
303 		if (!NT_STATUS_IS_OK(status)) {
304 			printf("appendacl[%d] failed - %s\n", i, nt_errstr(status));
305 			return False;
306 		}
307 
308 		security_descriptor_dacl_add(io_orig[i]->out.sd, ace);
309 		if (!security_acl_equal(io_orig[i]->out.sd->dacl, io[i]->out.sd->dacl)) {
310 			printf("appendacl[%d] failed - needed acl isn't set\n", i);
311 			return False;
312 		}
313 	}
314 
315 
316 	talloc_free (ace);
317 	talloc_free (test_sid);
318 	talloc_free (test_sd);
319 
320 	return True;
321 }
322 
323 /* test a query FS info by asking for share's GUID */
test_fsinfo(struct smbcli_state * cli,TALLOC_CTX * mem_ctx)324 static BOOL test_fsinfo(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
325 {
326 	char *guid = NULL;
327 	NTSTATUS status;
328 	struct smb_composite_fsinfo io1;
329 	struct composite_context **c;
330 
331 	int i;
332 	extern int torture_numops;
333 	struct event_context *event_ctx;
334 	int *count = talloc_zero(mem_ctx, int);
335 	BOOL ret = True;
336 
337 	io1.in.dest_host = lp_parm_string(-1, "torture", "host");
338 	io1.in.port = 0;
339 	io1.in.called_name = lp_parm_string(-1, "torture", "host");
340 	io1.in.service = lp_parm_string(-1, "torture", "share");
341 	io1.in.service_type = "A:";
342 	io1.in.credentials = cmdline_credentials;
343 	io1.in.workgroup = lp_workgroup();
344 	io1.in.level = RAW_QFS_OBJECTID_INFORMATION;
345 
346 	printf("testing parallel queryfsinfo [Object ID] with %d ops\n", torture_numops);
347 
348 	event_ctx = talloc_reference(mem_ctx, cli->tree->session->transport->socket->event.ctx);
349 	c = talloc_array(mem_ctx, struct composite_context *, torture_numops);
350 
351 	for (i=0; i<torture_numops; i++) {
352 		c[i] = smb_composite_fsinfo_send(cli->tree,&io1);
353 		c[i]->async.fn = loadfile_complete;
354 		c[i]->async.private_data = count;
355 	}
356 
357 	printf("waiting for completion\n");
358 
359 	while (*count < torture_numops) {
360 		event_loop_once(event_ctx);
361 		printf("count=%d\r", *count);
362 		fflush(stdout);
363 	}
364 	printf("count=%d\n", *count);
365 
366 	for (i=0;i<torture_numops;i++) {
367 		status = smb_composite_fsinfo_recv(c[i], mem_ctx);
368 		if (!NT_STATUS_IS_OK(status)) {
369 			printf("fsinfo[%d] failed - %s\n", i, nt_errstr(status));
370 			ret = False;
371 			continue;
372 		}
373 
374 		if (io1.out.fsinfo->generic.level != RAW_QFS_OBJECTID_INFORMATION) {
375 			printf("wrong level in returned info - %d "
376 			       "should be %d\n",
377 			       io1.out.fsinfo->generic.level, RAW_QFS_OBJECTID_INFORMATION);
378 			ret = False;
379 			continue;
380 		}
381 
382 		guid=GUID_string(mem_ctx, &io1.out.fsinfo->objectid_information.out.guid);
383 		printf("[%d] GUID: %s\n", i, guid);
384 
385 
386 	}
387 
388 	return ret;
389 }
390 
391 
392 /*
393    basic testing of libcli composite calls
394 */
torture_raw_composite(struct torture_context * torture)395 BOOL torture_raw_composite(struct torture_context *torture)
396 {
397 	struct smbcli_state *cli;
398 	BOOL ret = True;
399 	TALLOC_CTX *mem_ctx;
400 
401 	if (!torture_open_connection(&cli, 0)) {
402 		return False;
403 	}
404 
405 	mem_ctx = talloc_init("torture_raw_composite");
406 
407 	if (!torture_setup_dir(cli, BASEDIR)) {
408 		return False;
409 	}
410 
411 	ret &= test_fetchfile(cli, mem_ctx);
412 	ret &= test_loadfile(cli, mem_ctx);
413  	ret &= test_appendacl(cli, mem_ctx);
414 	ret &= test_fsinfo(cli, mem_ctx);
415 
416 	smb_raw_exit(cli->session);
417 	smbcli_deltree(cli->tree, BASEDIR);
418 
419 	torture_close_connection(cli);
420 	talloc_free(mem_ctx);
421 	return ret;
422 }
423