Add SMB_QFS_PROXY_INFO to identify WAFS proxy capabilities
[Samba/vfs_proxy.git] / source4 / torture / libnet / libnet_user.c
blob18007dccadd0f2e6804e7c98bf2b47a994fa34a9
1 /*
2 Unix SMB/CIFS implementation.
3 Test suite for libnet calls.
5 Copyright (C) Rafal Szczesniak 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/time.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
27 #include "torture/torture.h"
28 #include "torture/rpc/rpc.h"
29 #include "torture/libnet/usertest.h"
30 #include "param/param.h"
33 static bool test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
34 struct policy_handle *domain_handle, const char *username)
36 NTSTATUS status;
37 struct samr_LookupNames r1;
38 struct samr_OpenUser r2;
39 struct samr_DeleteUser r3;
40 struct lsa_String names[2];
41 uint32_t rid;
42 struct policy_handle user_handle;
43 struct samr_Ids rids, types;
45 names[0].string = username;
47 r1.in.domain_handle = domain_handle;
48 r1.in.num_names = 1;
49 r1.in.names = names;
50 r1.out.rids = &rids;
51 r1.out.types = &types;
53 printf("user account lookup '%s'\n", username);
55 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
56 if (!NT_STATUS_IS_OK(status)) {
57 printf("LookupNames failed - %s\n", nt_errstr(status));
58 return false;
61 rid = r1.out.rids->ids[0];
63 r2.in.domain_handle = domain_handle;
64 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65 r2.in.rid = rid;
66 r2.out.user_handle = &user_handle;
68 printf("opening user account\n");
70 status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
71 if (!NT_STATUS_IS_OK(status)) {
72 printf("OpenUser failed - %s\n", nt_errstr(status));
73 return false;
76 r3.in.user_handle = &user_handle;
77 r3.out.user_handle = &user_handle;
79 printf("deleting user account\n");
81 status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
82 if (!NT_STATUS_IS_OK(status)) {
83 printf("DeleteUser failed - %s\n", nt_errstr(status));
84 return false;
87 return true;
91 static bool test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
92 struct policy_handle *handle, struct lsa_String *domname)
94 NTSTATUS status;
95 struct policy_handle h, domain_handle;
96 struct samr_Connect r1;
97 struct samr_LookupDomain r2;
98 struct dom_sid2 *sid = NULL;
99 struct samr_OpenDomain r3;
101 printf("connecting\n");
103 r1.in.system_name = 0;
104 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
105 r1.out.connect_handle = &h;
107 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
108 if (!NT_STATUS_IS_OK(status)) {
109 printf("Connect failed - %s\n", nt_errstr(status));
110 return false;
113 r2.in.connect_handle = &h;
114 r2.in.domain_name = domname;
115 r2.out.sid = &sid;
117 printf("domain lookup on %s\n", domname->string);
119 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
120 if (!NT_STATUS_IS_OK(status)) {
121 printf("LookupDomain failed - %s\n", nt_errstr(status));
122 return false;
125 r3.in.connect_handle = &h;
126 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
127 r3.in.sid = *r2.out.sid;
128 r3.out.domain_handle = &domain_handle;
130 printf("opening domain\n");
132 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
133 if (!NT_STATUS_IS_OK(status)) {
134 printf("OpenDomain failed - %s\n", nt_errstr(status));
135 return false;
136 } else {
137 *handle = domain_handle;
140 return true;
144 static bool test_samr_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
145 struct policy_handle *domain_handle)
147 NTSTATUS status;
148 struct samr_Close r;
150 r.in.handle = domain_handle;
151 r.out.handle = domain_handle;
153 status = dcerpc_samr_Close(p, mem_ctx, &r);
154 if (!NT_STATUS_IS_OK(status)) {
155 printf("Close samr domain failed - %s\n", nt_errstr(status));
156 return false;
159 return true;
163 static bool test_lsa_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
164 struct policy_handle *domain_handle)
166 NTSTATUS status;
167 struct lsa_Close r;
169 r.in.handle = domain_handle;
170 r.out.handle = domain_handle;
172 status = dcerpc_lsa_Close(p, mem_ctx, &r);
173 if (!NT_STATUS_IS_OK(status)) {
174 printf("Close lsa domain failed - %s\n", nt_errstr(status));
175 return false;
178 return true;
182 static bool test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
183 struct policy_handle *handle, const char* user)
185 NTSTATUS status;
186 struct policy_handle user_handle;
187 struct lsa_String username;
188 struct samr_CreateUser r1;
189 struct samr_Close r2;
190 uint32_t user_rid;
192 username.string = user;
194 r1.in.domain_handle = handle;
195 r1.in.account_name = &username;
196 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
197 r1.out.user_handle = &user_handle;
198 r1.out.rid = &user_rid;
200 printf("creating user '%s'\n", username.string);
202 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
203 if (!NT_STATUS_IS_OK(status)) {
204 printf("CreateUser failed - %s\n", nt_errstr(status));
206 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
207 printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
208 if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
209 return false;
212 printf("creating user account\n");
214 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
215 if (!NT_STATUS_IS_OK(status)) {
216 printf("CreateUser failed - %s\n", nt_errstr(status));
217 return false;
219 return true;
221 return false;
224 r2.in.handle = &user_handle;
225 r2.out.handle = &user_handle;
227 printf("closing user '%s'\n", username.string);
229 status = dcerpc_samr_Close(p, mem_ctx, &r2);
230 if (!NT_STATUS_IS_OK(status)) {
231 printf("Close failed - %s\n", nt_errstr(status));
232 return false;
235 return true;
239 bool torture_createuser(struct torture_context *torture)
241 NTSTATUS status;
242 TALLOC_CTX *mem_ctx;
243 struct libnet_context *ctx;
244 struct libnet_CreateUser req;
245 bool ret = true;
247 mem_ctx = talloc_init("test_createuser");
249 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
250 ctx->cred = cmdline_credentials;
252 req.in.user_name = TEST_USERNAME;
253 req.in.domain_name = lp_workgroup(torture->lp_ctx);
254 req.out.error_string = NULL;
256 status = libnet_CreateUser(ctx, mem_ctx, &req);
257 if (!NT_STATUS_IS_OK(status)) {
258 printf("libnet_CreateUser call failed: %s\n", nt_errstr(status));
259 ret = false;
260 goto done;
263 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
264 printf("cleanup failed\n");
265 ret = false;
266 goto done;
269 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
270 printf("domain close failed\n");
271 ret = false;
274 done:
275 talloc_free(ctx);
276 talloc_free(mem_ctx);
277 return ret;
281 bool torture_deleteuser(struct torture_context *torture)
283 NTSTATUS status;
284 struct dcerpc_pipe *p;
285 TALLOC_CTX *prep_mem_ctx, *mem_ctx;
286 struct policy_handle h;
287 struct lsa_String domain_name;
288 const char *name = TEST_USERNAME;
289 struct libnet_context *ctx;
290 struct libnet_DeleteUser req;
291 bool ret = true;
293 prep_mem_ctx = talloc_init("prepare test_deleteuser");
295 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
296 ctx->cred = cmdline_credentials;
298 req.in.user_name = TEST_USERNAME;
299 req.in.domain_name = lp_workgroup(torture->lp_ctx);
301 status = torture_rpc_connection(torture,
303 &ndr_table_samr);
304 if (!NT_STATUS_IS_OK(status)) {
305 ret = false;
306 goto done;
309 domain_name.string = lp_workgroup(torture->lp_ctx);
310 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
311 ret = false;
312 goto done;
315 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
316 ret = false;
317 goto done;
320 mem_ctx = talloc_init("test_deleteuser");
322 status = libnet_DeleteUser(ctx, mem_ctx, &req);
323 if (!NT_STATUS_IS_OK(status)) {
324 printf("libnet_DeleteUser call failed: %s\n", nt_errstr(status));
325 ret = false;
328 talloc_free(mem_ctx);
330 done:
331 talloc_free(ctx);
332 talloc_free(prep_mem_ctx);
333 return ret;
338 Generate testing set of random changes
341 static void set_test_changes(TALLOC_CTX *mem_ctx, struct libnet_ModifyUser *r,
342 int num_changes, char **user_name, enum test_fields req_change)
344 const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
345 const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
346 const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
347 const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL | ACB_PW_EXPIRED),
348 (ACB_NORMAL | ACB_PWNOEXP),
349 (ACB_NORMAL | ACB_PW_EXPIRED) };
350 const char *homedir, *homedrive, *logonscript;
351 struct timeval now;
352 int i, testfld;
354 printf("Fields to change: [");
356 for (i = 0; i < num_changes && i < FIELDS_NUM; i++) {
357 const char *fldname;
359 testfld = (req_change == none) ? (random() % FIELDS_NUM) : req_change;
361 /* get one in case we hit time field this time */
362 gettimeofday(&now, NULL);
364 switch (testfld) {
365 case account_name:
366 continue_if_field_set(r->in.account_name);
367 r->in.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
368 (int)(random() % 100));
369 fldname = "account_name";
371 /* update the test's user name in case it's about to change */
372 *user_name = talloc_strdup(mem_ctx, r->in.account_name);
373 break;
375 case full_name:
376 continue_if_field_set(r->in.full_name);
377 r->in.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
378 (unsigned int)random(), (unsigned int)random());
379 fldname = "full_name";
380 break;
382 case description:
383 continue_if_field_set(r->in.description);
384 r->in.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
385 (long)random());
386 fldname = "description";
387 break;
389 case home_directory:
390 continue_if_field_set(r->in.home_directory);
391 homedir = home_dirs[random() % ARRAY_SIZE(home_dirs)];
392 r->in.home_directory = talloc_strdup(mem_ctx, homedir);
393 fldname = "home_dir";
394 break;
396 case home_drive:
397 continue_if_field_set(r->in.home_drive);
398 homedrive = home_drives[random() % ARRAY_SIZE(home_drives)];
399 r->in.home_drive = talloc_strdup(mem_ctx, homedrive);
400 fldname = "home_drive";
401 break;
403 case comment:
404 continue_if_field_set(r->in.comment);
405 r->in.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
406 (unsigned long)random(), (unsigned long)random());
407 fldname = "comment";
408 break;
410 case logon_script:
411 continue_if_field_set(r->in.logon_script);
412 logonscript = logon_scripts[random() % ARRAY_SIZE(logon_scripts)];
413 r->in.logon_script = talloc_strdup(mem_ctx, logonscript);
414 fldname = "logon_script";
415 break;
417 case profile_path:
418 continue_if_field_set(r->in.profile_path);
419 r->in.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
420 (unsigned long)random(), (unsigned int)random());
421 fldname = "profile_path";
422 break;
424 case acct_expiry:
425 continue_if_field_set(r->in.acct_expiry);
426 now = timeval_add(&now, (random() % (31*24*60*60)), 0);
427 r->in.acct_expiry = (struct timeval *)talloc_memdup(mem_ctx, &now, sizeof(now));
428 fldname = "acct_expiry";
429 break;
431 case acct_flags:
432 continue_if_field_set(r->in.acct_flags);
433 r->in.acct_flags = flags[random() % ARRAY_SIZE(flags)];
434 fldname = "acct_flags";
435 break;
437 default:
438 fldname = "unknown_field";
441 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
443 /* disable requested field (it's supposed to be the only one used) */
444 if (req_change != none) req_change = none;
447 printf("]\n");
451 #define TEST_STR_FLD(fld) \
452 if (!strequal(req.in.fld, user_req.out.fld)) { \
453 printf("failed to change '%s'\n", #fld); \
454 ret = false; \
455 goto cleanup; \
458 #define TEST_TIME_FLD(fld) \
459 if (timeval_compare(req.in.fld, user_req.out.fld)) { \
460 printf("failed to change '%s'\n", #fld); \
461 ret = false; \
462 goto cleanup; \
465 #define TEST_NUM_FLD(fld) \
466 if (req.in.fld != user_req.out.fld) { \
467 printf("failed to change '%s'\n", #fld); \
468 ret = false; \
469 goto cleanup; \
473 bool torture_modifyuser(struct torture_context *torture)
475 NTSTATUS status;
476 struct dcerpc_binding *binding;
477 struct dcerpc_pipe *p;
478 TALLOC_CTX *prep_mem_ctx;
479 struct policy_handle h;
480 struct lsa_String domain_name;
481 char *name;
482 struct libnet_context *ctx;
483 struct libnet_ModifyUser req;
484 struct libnet_UserInfo user_req;
485 int fld;
486 bool ret = true;
488 prep_mem_ctx = talloc_init("prepare test_deleteuser");
490 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
491 ctx->cred = cmdline_credentials;
493 status = torture_rpc_connection(torture,
495 &ndr_table_samr);
496 if (!NT_STATUS_IS_OK(status)) {
497 ret = false;
498 goto done;
501 name = talloc_strdup(prep_mem_ctx, TEST_USERNAME);
503 domain_name.string = lp_workgroup(torture->lp_ctx);
504 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
505 ret = false;
506 goto done;
509 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
510 ret = false;
511 goto done;
514 status = torture_rpc_binding(torture, &binding);
515 if (!NT_STATUS_IS_OK(status)) {
516 ret = false;
517 goto done;
520 printf("Testing change of all fields - each single one in turn\n");
522 for (fld = 1; fld < FIELDS_NUM - 1; fld++) {
523 ZERO_STRUCT(req);
524 req.in.domain_name = lp_workgroup(torture->lp_ctx);
525 req.in.user_name = name;
527 set_test_changes(torture, &req, 1, &name, fld);
529 status = libnet_ModifyUser(ctx, torture, &req);
530 if (!NT_STATUS_IS_OK(status)) {
531 printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
532 ret = false;
533 continue;
536 ZERO_STRUCT(user_req);
537 user_req.in.domain_name = lp_workgroup(torture->lp_ctx);
538 user_req.in.data.user_name = name;
539 user_req.in.level = USER_INFO_BY_NAME;
541 status = libnet_UserInfo(ctx, torture, &user_req);
542 if (!NT_STATUS_IS_OK(status)) {
543 printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
544 ret = false;
545 continue;
548 switch (fld) {
549 case account_name: TEST_STR_FLD(account_name);
550 break;
551 case full_name: TEST_STR_FLD(full_name);
552 break;
553 case comment: TEST_STR_FLD(comment);
554 break;
555 case description: TEST_STR_FLD(description);
556 break;
557 case home_directory: TEST_STR_FLD(home_directory);
558 break;
559 case home_drive: TEST_STR_FLD(home_drive);
560 break;
561 case logon_script: TEST_STR_FLD(logon_script);
562 break;
563 case profile_path: TEST_STR_FLD(profile_path);
564 break;
565 case acct_expiry: TEST_TIME_FLD(acct_expiry);
566 break;
567 case acct_flags: TEST_NUM_FLD(acct_flags);
568 break;
569 default:
570 break;
573 if (fld == account_name) {
574 /* restore original testing username - it's useful when test fails
575 because it prevents from problems with recreating account */
576 ZERO_STRUCT(req);
577 req.in.domain_name = lp_workgroup(torture->lp_ctx);
578 req.in.user_name = name;
579 req.in.account_name = TEST_USERNAME;
581 status = libnet_ModifyUser(ctx, torture, &req);
582 if (!NT_STATUS_IS_OK(status)) {
583 printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
584 ret = false;
585 goto done;
588 name = talloc_strdup(torture, TEST_USERNAME);
592 cleanup:
593 if (!test_cleanup(ctx->samr.pipe, torture, &ctx->samr.handle, name)) {
594 printf("cleanup failed\n");
595 ret = false;
596 goto done;
599 if (!test_samr_close(ctx->samr.pipe, torture, &ctx->samr.handle)) {
600 printf("domain close failed\n");
601 ret = false;
604 done:
605 talloc_free(ctx);
606 talloc_free(prep_mem_ctx);
607 return ret;
611 bool torture_userinfo_api(struct torture_context *torture)
613 const char *name = TEST_USERNAME;
614 bool ret = true;
615 NTSTATUS status;
616 TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
617 struct libnet_context *ctx;
618 struct dcerpc_pipe *p;
619 struct policy_handle h;
620 struct lsa_String domain_name;
621 struct libnet_UserInfo req;
623 prep_mem_ctx = talloc_init("prepare torture user info");
625 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
626 ctx->cred = cmdline_credentials;
628 status = torture_rpc_connection(torture,
630 &ndr_table_samr);
631 if (!NT_STATUS_IS_OK(status)) {
632 return false;
635 domain_name.string = lp_workgroup(torture->lp_ctx);
636 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
637 ret = false;
638 goto done;
641 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
642 ret = false;
643 goto done;
646 mem_ctx = talloc_init("torture user info");
648 ZERO_STRUCT(req);
650 req.in.domain_name = domain_name.string;
651 req.in.data.user_name = name;
652 req.in.level = USER_INFO_BY_NAME;
654 status = libnet_UserInfo(ctx, mem_ctx, &req);
655 if (!NT_STATUS_IS_OK(status)) {
656 printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
657 ret = false;
658 talloc_free(mem_ctx);
659 goto done;
662 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
663 printf("cleanup failed\n");
664 ret = false;
665 goto done;
668 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
669 printf("domain close failed\n");
670 ret = false;
673 talloc_free(ctx);
675 done:
676 talloc_free(mem_ctx);
677 return ret;
681 bool torture_userlist(struct torture_context *torture)
683 bool ret = true;
684 NTSTATUS status;
685 TALLOC_CTX *mem_ctx = NULL;
686 struct libnet_context *ctx;
687 struct lsa_String domain_name;
688 struct libnet_UserList req;
689 int i;
691 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
692 ctx->cred = cmdline_credentials;
694 domain_name.string = lp_workgroup(torture->lp_ctx);
695 mem_ctx = talloc_init("torture user list");
697 ZERO_STRUCT(req);
699 printf("listing user accounts:\n");
701 do {
703 req.in.domain_name = domain_name.string;
704 req.in.page_size = 128;
705 req.in.resume_index = req.out.resume_index;
707 status = libnet_UserList(ctx, mem_ctx, &req);
708 if (!NT_STATUS_IS_OK(status) &&
709 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) break;
711 for (i = 0; i < req.out.count; i++) {
712 printf("\tuser: %s, sid=%s\n",
713 req.out.users[i].username, req.out.users[i].sid);
716 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
718 if (!(NT_STATUS_IS_OK(status) ||
719 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))) {
720 printf("libnet_UserList call failed: %s\n", nt_errstr(status));
721 ret = false;
722 goto done;
725 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
726 printf("samr domain close failed\n");
727 ret = false;
728 goto done;
731 if (!test_lsa_close(ctx->lsa.pipe, mem_ctx, &ctx->lsa.handle)) {
732 printf("lsa domain close failed\n");
733 ret = false;
736 talloc_free(ctx);
738 done:
739 talloc_free(mem_ctx);
740 return ret;