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"
32 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
33 struct policy_handle *domain_handle, const char *username)
36 struct samr_LookupNames r1;
37 struct samr_OpenUser r2;
38 struct samr_DeleteUser r3;
39 struct lsa_String names[2];
41 struct policy_handle user_handle;
43 names[0].string = username;
45 r1.in.domain_handle = domain_handle;
49 printf("user account lookup '%s'\n", username);
51 status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
52 if (!NT_STATUS_IS_OK(status)) {
53 printf("LookupNames failed - %s\n", nt_errstr(status));
57 rid = r1.out.rids.ids[0];
59 r2.in.domain_handle = domain_handle;
60 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
62 r2.out.user_handle = &user_handle;
64 printf("opening user account\n");
66 status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
67 if (!NT_STATUS_IS_OK(status)) {
68 printf("OpenUser failed - %s\n", nt_errstr(status));
72 r3.in.user_handle = &user_handle;
73 r3.out.user_handle = &user_handle;
75 printf("deleting user account\n");
77 status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
78 if (!NT_STATUS_IS_OK(status)) {
79 printf("DeleteUser failed - %s\n", nt_errstr(status));
87 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
88 struct policy_handle *handle, struct lsa_String *domname)
91 struct policy_handle h, domain_handle;
92 struct samr_Connect r1;
93 struct samr_LookupDomain r2;
94 struct samr_OpenDomain r3;
96 printf("connecting\n");
98 r1.in.system_name = 0;
99 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
100 r1.out.connect_handle = &h;
102 status = dcerpc_samr_Connect(p, mem_ctx, &r1);
103 if (!NT_STATUS_IS_OK(status)) {
104 printf("Connect failed - %s\n", nt_errstr(status));
108 r2.in.connect_handle = &h;
109 r2.in.domain_name = domname;
111 printf("domain lookup on %s\n", domname->string);
113 status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
114 if (!NT_STATUS_IS_OK(status)) {
115 printf("LookupDomain failed - %s\n", nt_errstr(status));
119 r3.in.connect_handle = &h;
120 r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
121 r3.in.sid = r2.out.sid;
122 r3.out.domain_handle = &domain_handle;
124 printf("opening domain\n");
126 status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
127 if (!NT_STATUS_IS_OK(status)) {
128 printf("OpenDomain failed - %s\n", nt_errstr(status));
131 *handle = domain_handle;
138 static BOOL test_samr_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
139 struct policy_handle *domain_handle)
144 r.in.handle = domain_handle;
145 r.out.handle = domain_handle;
147 status = dcerpc_samr_Close(p, mem_ctx, &r);
148 if (!NT_STATUS_IS_OK(status)) {
149 printf("Close samr domain failed - %s\n", nt_errstr(status));
157 static BOOL test_lsa_close(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
158 struct policy_handle *domain_handle)
163 r.in.handle = domain_handle;
164 r.out.handle = domain_handle;
166 status = dcerpc_lsa_Close(p, mem_ctx, &r);
167 if (!NT_STATUS_IS_OK(status)) {
168 printf("Close lsa domain failed - %s\n", nt_errstr(status));
176 static BOOL test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
177 struct policy_handle *handle, const char* user)
180 struct policy_handle user_handle;
181 struct lsa_String username;
182 struct samr_CreateUser r1;
183 struct samr_Close r2;
186 username.string = user;
188 r1.in.domain_handle = handle;
189 r1.in.account_name = &username;
190 r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
191 r1.out.user_handle = &user_handle;
192 r1.out.rid = &user_rid;
194 printf("creating user '%s'\n", username.string);
196 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
197 if (!NT_STATUS_IS_OK(status)) {
198 printf("CreateUser failed - %s\n", nt_errstr(status));
200 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
201 printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
202 if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
206 printf("creating user account\n");
208 status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
209 if (!NT_STATUS_IS_OK(status)) {
210 printf("CreateUser failed - %s\n", nt_errstr(status));
218 r2.in.handle = &user_handle;
219 r2.out.handle = &user_handle;
221 printf("closing user '%s'\n", username.string);
223 status = dcerpc_samr_Close(p, mem_ctx, &r2);
224 if (!NT_STATUS_IS_OK(status)) {
225 printf("Close failed - %s\n", nt_errstr(status));
233 BOOL torture_createuser(struct torture_context *torture)
237 struct libnet_context *ctx;
238 struct libnet_CreateUser req;
241 mem_ctx = talloc_init("test_createuser");
243 ctx = libnet_context_init(NULL);
244 ctx->cred = cmdline_credentials;
246 req.in.user_name = TEST_USERNAME;
247 req.in.domain_name = lp_workgroup();
248 req.out.error_string = NULL;
250 status = libnet_CreateUser(ctx, mem_ctx, &req);
251 if (!NT_STATUS_IS_OK(status)) {
252 printf("libnet_CreateUser call failed: %s\n", nt_errstr(status));
257 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
258 printf("cleanup failed\n");
263 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
264 printf("domain close failed\n");
270 talloc_free(mem_ctx);
275 BOOL torture_deleteuser(struct torture_context *torture)
278 struct dcerpc_pipe *p;
279 TALLOC_CTX *prep_mem_ctx, *mem_ctx;
280 struct policy_handle h;
281 struct lsa_String domain_name;
282 const char *name = TEST_USERNAME;
283 struct libnet_context *ctx;
284 struct libnet_DeleteUser req;
287 prep_mem_ctx = talloc_init("prepare test_deleteuser");
289 ctx = libnet_context_init(NULL);
290 ctx->cred = cmdline_credentials;
292 req.in.user_name = TEST_USERNAME;
293 req.in.domain_name = lp_workgroup();
295 status = torture_rpc_connection(torture,
298 if (!NT_STATUS_IS_OK(status)) {
303 domain_name.string = lp_workgroup();
304 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
309 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
314 mem_ctx = talloc_init("test_deleteuser");
316 status = libnet_DeleteUser(ctx, mem_ctx, &req);
317 if (!NT_STATUS_IS_OK(status)) {
318 printf("libnet_DeleteUser call failed: %s\n", nt_errstr(status));
322 talloc_free(mem_ctx);
326 talloc_free(prep_mem_ctx);
332 Generate testing set of random changes
335 static void set_test_changes(TALLOC_CTX *mem_ctx, struct libnet_ModifyUser *r,
336 int num_changes, char **user_name, enum test_fields req_change)
338 const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
339 const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
340 const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
341 const char *homedir, *homedrive, *logonscript;
345 srandom((unsigned)time(NULL));
347 printf("Fields to change: [");
349 for (i = 0; i < num_changes && i < FIELDS_NUM; i++) {
352 testfld = (req_change == none) ? (random() % FIELDS_NUM) : req_change;
354 /* get one in case we hit time field this time */
355 gettimeofday(&now, NULL);
359 continue_if_field_set(r->in.account_name);
360 r->in.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
361 (int)(random() % 100));
362 fldname = "account_name";
364 /* update the test's user name in case it's about to change */
365 *user_name = talloc_strdup(mem_ctx, r->in.account_name);
369 continue_if_field_set(r->in.full_name);
370 r->in.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
371 (unsigned int)random(), (unsigned int)random());
372 fldname = "full_name";
376 continue_if_field_set(r->in.description);
377 r->in.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
379 fldname = "description";
383 continue_if_field_set(r->in.home_directory);
384 homedir = home_dirs[random() % (sizeof(home_dirs)/sizeof(char*))];
385 r->in.home_directory = talloc_strdup(mem_ctx, homedir);
386 fldname = "home_dir";
390 continue_if_field_set(r->in.home_drive);
391 homedrive = home_drives[random() % (sizeof(home_drives)/sizeof(char*))];
392 r->in.home_drive = talloc_strdup(mem_ctx, homedrive);
393 fldname = "home_drive";
397 continue_if_field_set(r->in.comment);
398 r->in.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
399 (unsigned long)random(), (unsigned long)random());
404 continue_if_field_set(r->in.logon_script);
405 logonscript = logon_scripts[random() % (sizeof(logon_scripts)/sizeof(char*))];
406 r->in.logon_script = talloc_strdup(mem_ctx, logonscript);
407 fldname = "logon_script";
411 continue_if_field_set(r->in.profile_path);
412 r->in.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
413 (unsigned long)random(), (unsigned int)random());
414 fldname = "profile_path";
418 continue_if_field_set(r->in.acct_expiry);
419 now = timeval_add(&now, (random() % (31*24*60*60)), 0);
420 r->in.acct_expiry = talloc_memdup(mem_ctx, &now, sizeof(now));
421 fldname = "acct_expiry";
425 fldname = "unknown_field";
428 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
430 /* disable requested field (it's supposed to be the only one used) */
431 if (req_change != none) req_change = none;
438 #define TEST_STR_FLD(fld) \
439 if (!strequal(req.in.fld, user_req.out.fld)) { \
440 printf("failed to change '%s'\n", #fld); \
445 #define TEST_TIME_FLD(fld) \
446 if (timeval_compare(req.in.fld, user_req.out.fld)) { \
447 printf("failed to change '%s'\n", #fld); \
452 #define TEST_NUM_FLD(fld) \
453 if (req.in.fld != user_req.out.fld) { \
454 printf("failed to change '%s'\n", #fld); \
460 BOOL torture_modifyuser(struct torture_context *torture)
463 struct dcerpc_binding *bind;
464 struct dcerpc_pipe *p;
465 TALLOC_CTX *prep_mem_ctx, *mem_ctx;
466 struct policy_handle h;
467 struct lsa_String domain_name;
469 struct libnet_context *ctx;
470 struct libnet_ModifyUser req;
471 struct libnet_UserInfo user_req;
475 prep_mem_ctx = talloc_init("prepare test_deleteuser");
477 ctx = libnet_context_init(NULL);
478 ctx->cred = cmdline_credentials;
480 status = torture_rpc_connection(torture,
483 if (!NT_STATUS_IS_OK(status)) {
488 name = talloc_strdup(prep_mem_ctx, TEST_USERNAME);
490 domain_name.string = lp_workgroup();
491 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
496 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
501 mem_ctx = talloc_init("test_modifyuser");
503 status = torture_rpc_binding(mem_ctx, &bind);
504 if (!NT_STATUS_IS_OK(status)) {
509 printf("Testing change of all fields - each single one in turn\n");
511 for (fld = 1; fld < FIELDS_NUM - 1; fld++) {
513 req.in.domain_name = lp_workgroup();
514 req.in.user_name = name;
516 set_test_changes(mem_ctx, &req, 1, &name, fld);
518 status = libnet_ModifyUser(ctx, mem_ctx, &req);
519 if (!NT_STATUS_IS_OK(status)) {
520 printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
525 ZERO_STRUCT(user_req);
526 user_req.in.domain_name = lp_workgroup();
527 user_req.in.user_name = name;
529 status = libnet_UserInfo(ctx, mem_ctx, &user_req);
530 if (!NT_STATUS_IS_OK(status)) {
531 printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
537 case account_name: TEST_STR_FLD(account_name);
539 case full_name: TEST_STR_FLD(full_name);
541 case comment: TEST_STR_FLD(comment);
543 case description: TEST_STR_FLD(description);
545 case home_directory: TEST_STR_FLD(home_directory);
547 case home_drive: TEST_STR_FLD(home_drive);
549 case logon_script: TEST_STR_FLD(logon_script);
551 case profile_path: TEST_STR_FLD(profile_path);
553 case acct_expiry: TEST_TIME_FLD(acct_expiry);
555 case acct_flags: TEST_NUM_FLD(acct_flags);
561 if (fld == account_name) {
562 /* restore original testing username - it's useful when test fails
563 because it prevents from problems with recreating account */
565 req.in.domain_name = lp_workgroup();
566 req.in.user_name = name;
567 req.in.account_name = TEST_USERNAME;
569 status = libnet_ModifyUser(ctx, mem_ctx, &req);
570 if (!NT_STATUS_IS_OK(status)) {
571 printf("libnet_ModifyUser call failed: %s\n", nt_errstr(status));
572 talloc_free(mem_ctx);
577 name = talloc_strdup(mem_ctx, TEST_USERNAME);
582 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, name)) {
583 printf("cleanup failed\n");
588 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
589 printf("domain close failed\n");
593 talloc_free(mem_ctx);
597 talloc_free(prep_mem_ctx);
602 BOOL torture_userinfo_api(struct torture_context *torture)
604 const char *name = TEST_USERNAME;
607 TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
608 struct libnet_context *ctx;
609 struct dcerpc_pipe *p;
610 struct policy_handle h;
611 struct lsa_String domain_name;
612 struct libnet_UserInfo req;
614 prep_mem_ctx = talloc_init("prepare torture user info");
616 ctx = libnet_context_init(NULL);
617 ctx->cred = cmdline_credentials;
619 status = torture_rpc_connection(torture,
622 if (!NT_STATUS_IS_OK(status)) {
626 domain_name.string = lp_workgroup();
627 if (!test_opendomain(p, prep_mem_ctx, &h, &domain_name)) {
632 if (!test_createuser(p, prep_mem_ctx, &h, name)) {
637 mem_ctx = talloc_init("torture user info");
641 req.in.domain_name = domain_name.string;
642 req.in.user_name = name;
644 status = libnet_UserInfo(ctx, mem_ctx, &req);
645 if (!NT_STATUS_IS_OK(status)) {
646 printf("libnet_UserInfo call failed: %s\n", nt_errstr(status));
648 talloc_free(mem_ctx);
652 if (!test_cleanup(ctx->samr.pipe, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
653 printf("cleanup failed\n");
658 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
659 printf("domain close failed\n");
666 talloc_free(mem_ctx);
671 BOOL torture_userlist(struct torture_context *torture)
675 TALLOC_CTX *mem_ctx = NULL;
676 struct libnet_context *ctx;
677 struct lsa_String domain_name;
678 struct libnet_UserList req;
681 ctx = libnet_context_init(NULL);
682 ctx->cred = cmdline_credentials;
684 domain_name.string = lp_workgroup();
685 mem_ctx = talloc_init("torture user list");
689 printf("listing user accounts:\n");
693 req.in.domain_name = domain_name.string;
694 req.in.page_size = 128;
695 req.in.resume_index = req.out.resume_index;
697 status = libnet_UserList(ctx, mem_ctx, &req);
699 for (i = 0; i < req.out.count; i++) {
700 printf("\tuser: %s, sid=%s\n",
701 req.out.users[i].username, req.out.users[i].sid);
704 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
706 if (!(NT_STATUS_IS_OK(status) ||
707 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))) {
708 printf("libnet_UserList call failed: %s\n", nt_errstr(status));
713 if (!test_samr_close(ctx->samr.pipe, mem_ctx, &ctx->samr.handle)) {
714 printf("samr domain close failed\n");
719 if (!test_lsa_close(ctx->lsa.pipe, mem_ctx, &ctx->lsa.handle)) {
720 printf("lsa domain close failed\n");
727 talloc_free(mem_ctx);