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/>.
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)
37 struct samr_LookupNames r1;
38 struct samr_OpenUser r2;
39 struct samr_DeleteUser r3;
40 struct lsa_String names[2];
42 struct policy_handle user_handle;
44 names[0].string = username;
46 r1.in.domain_handle = domain_handle;
50 printf("user account lookup '%s'\n", username);
52 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
53 if (!NT_STATUS_IS_OK(status)) {
54 printf("LookupNames failed - %s\n", nt_errstr(status));
58 rid = r1.out.rids.ids[0];
60 r2.in.domain_handle = domain_handle;
61 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
63 r2.out.user_handle = &user_handle;
65 printf("opening user account\n");
67 status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
68 if (!NT_STATUS_IS_OK(status)) {
69 printf("OpenUser failed - %s\n", nt_errstr(status));
73 r3.in.user_handle = &user_handle;
74 r3.out.user_handle = &user_handle;
76 printf("deleting user account\n");
78 status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
79 if (!NT_STATUS_IS_OK(status)) {
80 printf("DeleteUser failed - %s\n", nt_errstr(status));
88 static bool test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
89 struct policy_handle *handle, struct lsa_String *domname)
92 struct policy_handle h, domain_handle;
93 struct samr_Connect r1;
94 struct samr_LookupDomain r2;
95 struct samr_OpenDomain r3;
97 printf("connecting\n");
99 r1.in.system_name = 0;
100 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
101 r1.out.connect_handle = &h;
103 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
104 if (!NT_STATUS_IS_OK(status)) {
105 printf("Connect failed - %s\n", nt_errstr(status));
109 r2.in.connect_handle = &h;
110 r2.in.domain_name = domname;
112 printf("domain lookup on %s\n", domname->string);
114 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
115 if (!NT_STATUS_IS_OK(status)) {
116 printf("LookupDomain failed - %s\n", nt_errstr(status));
120 r3.in.connect_handle = &h;
121 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
122 r3.in.sid = r2.out.sid;
123 r3.out.domain_handle = &domain_handle;
125 printf("opening domain\n");
127 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
128 if (!NT_STATUS_IS_OK(status)) {
129 printf("OpenDomain failed - %s\n", nt_errstr(status));
132 *handle = domain_handle;
139 static bool test_samr_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
140 struct policy_handle *domain_handle)
145 r.in.handle = domain_handle;
146 r.out.handle = domain_handle;
148 status = dcerpc_samr_Close(p, mem_ctx, &r);
149 if (!NT_STATUS_IS_OK(status)) {
150 printf("Close samr domain failed - %s\n", nt_errstr(status));
158 static bool test_lsa_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
159 struct policy_handle *domain_handle)
164 r.in.handle = domain_handle;
165 r.out.handle = domain_handle;
167 status = dcerpc_lsa_Close(p, mem_ctx, &r);
168 if (!NT_STATUS_IS_OK(status)) {
169 printf("Close lsa domain failed - %s\n", nt_errstr(status));
177 static bool test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
178 struct policy_handle *handle, const char* user)
181 struct policy_handle user_handle;
182 struct lsa_String username;
183 struct samr_CreateUser r1;
184 struct samr_Close r2;
187 username.string = user;
189 r1.in.domain_handle = handle;
190 r1.in.account_name = &username;
191 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
192 r1.out.user_handle = &user_handle;
193 r1.out.rid = &user_rid;
195 printf("creating user '%s'\n", username.string);
197 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
198 if (!NT_STATUS_IS_OK(status)) {
199 printf("CreateUser failed - %s\n", nt_errstr(status));
201 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
202 printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
203 if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
207 printf("creating user account\n");
209 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
210 if (!NT_STATUS_IS_OK(status)) {
211 printf("CreateUser failed - %s\n", nt_errstr(status));
219 r2.in.handle = &user_handle;
220 r2.out.handle = &user_handle;
222 printf("closing user '%s'\n", username.string);
224 status = dcerpc_samr_Close(p, mem_ctx, &r2);
225 if (!NT_STATUS_IS_OK(status)) {
226 printf("Close failed - %s\n", nt_errstr(status));
234 bool torture_createuser(struct torture_context *torture)
238 struct libnet_context *ctx;
239 struct libnet_CreateUser req;
242 mem_ctx = talloc_init("test_createuser");
244 ctx = libnet_context_init(NULL, torture->lp_ctx);
245 ctx->cred = cmdline_credentials;
247 req.in.user_name = TEST_USERNAME;
248 req.in.domain_name = lp_workgroup(torture->lp_ctx);
249 req.out.error_string = NULL;
251 status = libnet_CreateUser(ctx, mem_ctx, &req);
252 if (!NT_STATUS_IS_OK(status)) {
253 printf("libnet_CreateUser call failed: %s\n", nt_errstr(status));
258 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
259 printf("cleanup failed\n");
264 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
265 printf("domain close failed\n");
271 talloc_free(mem_ctx);
276 bool torture_deleteuser(struct torture_context *torture)
279 struct dcerpc_pipe *p;
280 TALLOC_CTX *prep_mem_ctx, *mem_ctx;
281 struct policy_handle h;
282 struct lsa_String domain_name;
283 const char *name = TEST_USERNAME;
284 struct libnet_context *ctx;
285 struct libnet_DeleteUser req;
288 prep_mem_ctx = talloc_init("prepare test_deleteuser");
290 ctx = libnet_context_init(NULL, torture->lp_ctx);
291 ctx->cred = cmdline_credentials;
293 req.in.user_name = TEST_USERNAME;
294 req.in.domain_name = lp_workgroup(torture->lp_ctx);
296 status = torture_rpc_connection(torture,
299 if (!NT_STATUS_IS_OK(status)) {
304 domain_name.string = lp_workgroup(torture->lp_ctx);
305 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
310 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
315 mem_ctx = talloc_init("test_deleteuser");
317 status = libnet_DeleteUser(ctx, mem_ctx, &req);
318 if (!NT_STATUS_IS_OK(status)) {
319 printf("libnet_DeleteUser call failed: %s\n", nt_errstr(status));
323 talloc_free(mem_ctx);
327 talloc_free(prep_mem_ctx);
333 Generate testing set of random changes
336 static void set_test_changes(TALLOC_CTX *mem_ctx, struct libnet_ModifyUser *r,
337 int num_changes, char **user_name, enum test_fields req_change)
339 const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
340 const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
341 const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
342 const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL),
343 (ACB_NORMAL | ACB_PWNOEXP),
345 const char *homedir, *homedrive, *logonscript;
349 printf("Fields to change: [");
351 for (i = 0; i < num_changes && i < FIELDS_NUM; i++) {
354 testfld = (req_change == none) ? (random() % FIELDS_NUM) : req_change;
356 /* get one in case we hit time field this time */
357 gettimeofday(&now, NULL);
361 continue_if_field_set(r->in.account_name);
362 r->in.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
363 (int)(random() % 100));
364 fldname = "account_name";
366 /* update the test's user name in case it's about to change */
367 *user_name = talloc_strdup(mem_ctx, r->in.account_name);
371 continue_if_field_set(r->in.full_name);
372 r->in.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
373 (unsigned int)random(), (unsigned int)random());
374 fldname = "full_name";
378 continue_if_field_set(r->in.description);
379 r->in.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
381 fldname = "description";
385 continue_if_field_set(r->in.home_directory);
386 homedir = home_dirs[random() % ARRAY_SIZE(home_dirs)];
387 r->in.home_directory = talloc_strdup(mem_ctx, homedir);
388 fldname = "home_dir";
392 continue_if_field_set(r->in.home_drive);
393 homedrive = home_drives[random() % ARRAY_SIZE(home_drives)];
394 r->in.home_drive = talloc_strdup(mem_ctx, homedrive);
395 fldname = "home_drive";
399 continue_if_field_set(r->in.comment);
400 r->in.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
401 (unsigned long)random(), (unsigned long)random());
406 continue_if_field_set(r->in.logon_script);
407 logonscript = logon_scripts[random() % ARRAY_SIZE(logon_scripts)];
408 r->in.logon_script = talloc_strdup(mem_ctx, logonscript);
409 fldname = "logon_script";
413 continue_if_field_set(r->in.profile_path);
414 r->in.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
415 (unsigned long)random(), (unsigned int)random());
416 fldname = "profile_path";
420 continue_if_field_set(r->in.acct_expiry);
421 now = timeval_add(&now, (random() % (31*24*60*60)), 0);
422 r->in.acct_expiry = (struct timeval *)talloc_memdup(mem_ctx, &now, sizeof(now));
423 fldname = "acct_expiry";
427 continue_if_field_set(r->in.acct_flags);
428 r->in.acct_flags = flags[random() % ARRAY_SIZE(flags)];
429 fldname = "acct_flags";
433 fldname = "unknown_field";
436 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
438 /* disable requested field (it's supposed to be the only one used) */
439 if (req_change != none) req_change = none;
446 #define TEST_STR_FLD(fld) \
447 if (!strequal(req.in.fld, user_req.out.fld)) { \
448 printf("failed to change '%s'\n", #fld); \
453 #define TEST_TIME_FLD(fld) \
454 if (timeval_compare(req.in.fld, user_req.out.fld)) { \
455 printf("failed to change '%s'\n", #fld); \
460 #define TEST_NUM_FLD(fld) \
461 if (req.in.fld != user_req.out.fld) { \
462 printf("failed to change '%s'\n", #fld); \
468 bool torture_modifyuser(struct torture_context *torture)
471 struct dcerpc_binding *binding;
472 struct dcerpc_pipe *p;
473 TALLOC_CTX *prep_mem_ctx;
474 struct policy_handle h;
475 struct lsa_String domain_name;
477 struct libnet_context *ctx;
478 struct libnet_ModifyUser req;
479 struct libnet_UserInfo user_req;
483 prep_mem_ctx = talloc_init("prepare test_deleteuser");
485 ctx = libnet_context_init(NULL, torture->lp_ctx);
486 ctx->cred = cmdline_credentials;
488 status = torture_rpc_connection(torture,
491 if (!NT_STATUS_IS_OK(status)) {
496 name = talloc_strdup(prep_mem_ctx, TEST_USERNAME);
498 domain_name.string = lp_workgroup(torture->lp_ctx);
499 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
504 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
509 status = torture_rpc_binding(torture, &binding);
510 if (!NT_STATUS_IS_OK(status)) {
515 printf("Testing change of all fields - each single one in turn\n");
517 for (fld = 1; fld < FIELDS_NUM - 1; fld++) {
519 req.in.domain_name = lp_workgroup(torture->lp_ctx);
520 req.in.user_name = name;
522 set_test_changes(torture, &req, 1, &name, fld);
524 status = libnet_ModifyUser(ctx, torture, &req);
525 if (!NT_STATUS_IS_OK(status)) {
526 printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
531 ZERO_STRUCT(user_req);
532 user_req.in.domain_name = lp_workgroup(torture->lp_ctx);
533 user_req.in.user_name = name;
535 status = libnet_UserInfo(ctx, torture, &user_req);
536 if (!NT_STATUS_IS_OK(status)) {
537 printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
543 case account_name: TEST_STR_FLD(account_name);
545 case full_name: TEST_STR_FLD(full_name);
547 case comment: TEST_STR_FLD(comment);
549 case description: TEST_STR_FLD(description);
551 case home_directory: TEST_STR_FLD(home_directory);
553 case home_drive: TEST_STR_FLD(home_drive);
555 case logon_script: TEST_STR_FLD(logon_script);
557 case profile_path: TEST_STR_FLD(profile_path);
559 case acct_expiry: TEST_TIME_FLD(acct_expiry);
561 case acct_flags: TEST_NUM_FLD(acct_flags);
567 if (fld == account_name) {
568 /* restore original testing username - it's useful when test fails
569 because it prevents from problems with recreating account */
571 req.in.domain_name = lp_workgroup(torture->lp_ctx);
572 req.in.user_name = name;
573 req.in.account_name = TEST_USERNAME;
575 status = libnet_ModifyUser(ctx, torture, &req);
576 if (!NT_STATUS_IS_OK(status)) {
577 printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
582 name = talloc_strdup(torture, TEST_USERNAME);
587 if (!test_cleanup(ctx->samr.pipe, torture, &ctx->samr.handle, name)) {
588 printf("cleanup failed\n");
593 if (!test_samr_close(ctx->samr.pipe, torture, &ctx->samr.handle)) {
594 printf("domain close failed\n");
600 talloc_free(prep_mem_ctx);
605 bool torture_userinfo_api(struct torture_context *torture)
607 const char *name = TEST_USERNAME;
610 TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
611 struct libnet_context *ctx;
612 struct dcerpc_pipe *p;
613 struct policy_handle h;
614 struct lsa_String domain_name;
615 struct libnet_UserInfo req;
617 prep_mem_ctx = talloc_init("prepare torture user info");
619 ctx = libnet_context_init(NULL, torture->lp_ctx);
620 ctx->cred = cmdline_credentials;
622 status = torture_rpc_connection(torture,
625 if (!NT_STATUS_IS_OK(status)) {
629 domain_name.string = lp_workgroup(torture->lp_ctx);
630 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
635 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
640 mem_ctx = talloc_init("torture user info");
644 req.in.domain_name = domain_name.string;
645 req.in.user_name = name;
647 status = libnet_UserInfo(ctx, mem_ctx, &req);
648 if (!NT_STATUS_IS_OK(status)) {
649 printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
651 talloc_free(mem_ctx);
655 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
656 printf("cleanup failed\n");
661 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
662 printf("domain close failed\n");
669 talloc_free(mem_ctx);
674 bool torture_userlist(struct torture_context *torture)
678 TALLOC_CTX *mem_ctx = NULL;
679 struct libnet_context *ctx;
680 struct lsa_String domain_name;
681 struct libnet_UserList req;
684 ctx = libnet_context_init(NULL, torture->lp_ctx);
685 ctx->cred = cmdline_credentials;
687 domain_name.string = lp_workgroup(torture->lp_ctx);
688 mem_ctx = talloc_init("torture user list");
692 printf("listing user accounts:\n");
696 req.in.domain_name = domain_name.string;
697 req.in.page_size = 128;
698 req.in.resume_index = req.out.resume_index;
700 status = libnet_UserList(ctx, mem_ctx, &req);
701 if (!NT_STATUS_IS_OK(status) &&
702 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) break;
704 for (i = 0; i < req.out.count; i++) {
705 printf("\tuser: %s, sid=%s\n",
706 req.out.users[i].username, req.out.users[i].sid);
709 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
711 if (!(NT_STATUS_IS_OK(status) ||
712 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))) {
713 printf("libnet_UserList call failed: %s\n", nt_errstr(status));
718 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
719 printf("samr domain close failed\n");
724 if (!test_lsa_close(ctx->lsa.pipe, mem_ctx, &ctx->lsa.handle)) {
725 printf("lsa domain close failed\n");
732 talloc_free(mem_ctx);