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/rpc/torture_rpc.h"
28 #include "torture/libnet/usertest.h"
29 #include "torture/libnet/proto.h"
30 #include "param/param.h"
31 #include "lib/ldb_wrap.h"
35 * Find out user's samAccountName for given
36 * user RDN. We need samAccountName value
37 * when deleting users.
39 static bool _get_account_name_for_user_rdn(struct torture_context *tctx,
40 struct dcerpc_binding_handle *b,
43 const char **_account_name)
46 struct ldb_context *ldb;
49 struct dcerpc_pipe *p = talloc_get_type_abort(b->private_data, struct dcerpc_pipe);
51 struct ldb_result *ldb_res;
52 const char *account_name = NULL;
53 static const char *attrs[] = {
58 tmp_ctx = talloc_new(tctx);
59 torture_assert(tctx, tmp_ctx != NULL, "Failed to create temporary mem context");
61 url = talloc_asprintf(tmp_ctx, "ldap://%s/", p->binding->target_hostname);
62 torture_assert_goto(tctx, url != NULL, test_res, done, "Failed to allocate URL for ldb");
64 ldb = ldb_wrap_connect(tmp_ctx,
65 tctx->ev, tctx->lp_ctx,
66 url, NULL, cmdline_credentials, 0);
67 torture_assert_goto(tctx, ldb != NULL, test_res, done, "Failed to make LDB connection");
69 ldb_ret = ldb_search(ldb, tmp_ctx, &ldb_res,
70 ldb_get_default_basedn(ldb), LDB_SCOPE_SUBTREE,
72 "(&(objectClass=user)(name=%s))", user_rdn);
73 if (LDB_SUCCESS == ldb_ret && 1 == ldb_res->count) {
74 account_name = ldb_msg_find_attr_as_string(ldb_res->msgs[0], "samAccountName", NULL);
77 /* return user_rdn by default */
79 account_name = user_rdn;
82 /* duplicate memory in parent context */
83 *_account_name = talloc_strdup(mem_ctx, account_name);
91 * Deletes a user account when given user RDN name
93 * @param username RDN for the user to be deleted
95 static bool test_cleanup(struct torture_context *tctx,
96 struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
97 struct policy_handle *domain_handle, const char *username)
99 struct samr_LookupNames r1;
100 struct samr_OpenUser r2;
101 struct samr_DeleteUser r3;
102 struct lsa_String names[2];
104 struct policy_handle user_handle;
105 struct samr_Ids rids, types;
106 const char *account_name;
108 if (!_get_account_name_for_user_rdn(tctx, b, username, mem_ctx, &account_name)) {
109 torture_result(tctx, TORTURE_FAIL,
110 __location__": Failed to find samAccountName for %s", username);
114 names[0].string = account_name;
116 r1.in.domain_handle = domain_handle;
120 r1.out.types = &types;
122 torture_comment(tctx, "user account lookup '%s'\n", account_name);
124 torture_assert_ntstatus_ok(tctx,
125 dcerpc_samr_LookupNames_r(b, mem_ctx, &r1),
126 "LookupNames failed");
127 torture_assert_ntstatus_ok(tctx, r1.out.result,
128 "LookupNames failed");
130 rid = r1.out.rids->ids[0];
132 r2.in.domain_handle = domain_handle;
133 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
135 r2.out.user_handle = &user_handle;
137 torture_comment(tctx, "opening user account\n");
139 torture_assert_ntstatus_ok(tctx,
140 dcerpc_samr_OpenUser_r(b, mem_ctx, &r2),
142 torture_assert_ntstatus_ok(tctx, r2.out.result,
145 r3.in.user_handle = &user_handle;
146 r3.out.user_handle = &user_handle;
148 torture_comment(tctx, "deleting user account\n");
150 torture_assert_ntstatus_ok(tctx,
151 dcerpc_samr_DeleteUser_r(b, mem_ctx, &r3),
152 "DeleteUser failed");
153 torture_assert_ntstatus_ok(tctx, r3.out.result,
154 "DeleteUser failed");
160 static bool test_samr_close(struct torture_context *tctx,
161 struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
162 struct policy_handle *domain_handle)
166 r.in.handle = domain_handle;
167 r.out.handle = domain_handle;
169 torture_assert_ntstatus_ok(tctx,
170 dcerpc_samr_Close_r(b, mem_ctx, &r),
171 "Close samr domain failed");
172 torture_assert_ntstatus_ok(tctx, r.out.result,
173 "Close samr domain failed");
179 static bool test_lsa_close(struct torture_context *tctx,
180 struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx,
181 struct policy_handle *domain_handle)
185 r.in.handle = domain_handle;
186 r.out.handle = domain_handle;
188 torture_assert_ntstatus_ok(tctx,
189 dcerpc_lsa_Close_r(b, mem_ctx, &r),
190 "Close lsa domain failed");
191 torture_assert_ntstatus_ok(tctx, r.out.result,
192 "Close lsa domain failed");
198 bool torture_createuser(struct torture_context *torture)
202 struct libnet_context *ctx;
203 struct libnet_CreateUser req;
206 mem_ctx = talloc_init("test_createuser");
208 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
209 ctx->cred = cmdline_credentials;
211 req.in.user_name = TEST_USERNAME;
212 req.in.domain_name = lp_workgroup(torture->lp_ctx);
213 req.out.error_string = NULL;
215 status = libnet_CreateUser(ctx, mem_ctx, &req);
216 if (!NT_STATUS_IS_OK(status)) {
217 torture_comment(torture, "libnet_CreateUser call failed: %s\n", nt_errstr(status));
222 if (!test_cleanup(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
223 torture_comment(torture, "cleanup failed\n");
228 if (!test_samr_close(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
229 torture_comment(torture, "domain close failed\n");
235 talloc_free(mem_ctx);
240 bool torture_deleteuser(struct torture_context *torture)
243 struct dcerpc_pipe *p;
244 TALLOC_CTX *prep_mem_ctx, *mem_ctx;
245 struct policy_handle h;
246 struct lsa_String domain_name;
247 const char *name = TEST_USERNAME;
248 struct libnet_context *ctx;
249 struct libnet_DeleteUser req;
252 prep_mem_ctx = talloc_init("prepare test_deleteuser");
254 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
255 ctx->cred = cmdline_credentials;
257 req.in.user_name = TEST_USERNAME;
258 req.in.domain_name = lp_workgroup(torture->lp_ctx);
260 status = torture_rpc_connection(torture,
263 if (!NT_STATUS_IS_OK(status)) {
268 domain_name.string = lp_workgroup(torture->lp_ctx);
269 if (!test_domain_open(torture, p->binding_handle, &domain_name, prep_mem_ctx, &h, NULL)) {
274 if (!test_user_create(torture, p->binding_handle, prep_mem_ctx, &h, name, NULL)) {
279 mem_ctx = talloc_init("test_deleteuser");
281 status = libnet_DeleteUser(ctx, mem_ctx, &req);
282 if (!NT_STATUS_IS_OK(status)) {
283 torture_comment(torture, "libnet_DeleteUser call failed: %s\n", nt_errstr(status));
287 talloc_free(mem_ctx);
291 talloc_free(prep_mem_ctx);
297 Generate testing set of random changes
300 static void set_test_changes(struct torture_context *tctx,
301 TALLOC_CTX *mem_ctx, struct libnet_ModifyUser *r,
302 int num_changes, char **user_name, enum test_fields req_change)
304 const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
305 const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
306 const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
307 const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL | ACB_PW_EXPIRED),
308 (ACB_NORMAL | ACB_PWNOEXP),
309 (ACB_NORMAL | ACB_PW_EXPIRED) };
310 const char *homedir, *homedrive, *logonscript;
314 torture_comment(tctx, "Fields to change: [");
316 for (i = 0; i < num_changes && i <= USER_FIELD_LAST; i++) {
319 testfld = (req_change == none) ? (random() % USER_FIELD_LAST) + 1 : req_change;
321 /* get one in case we hit time field this time */
322 gettimeofday(&now, NULL);
326 continue_if_field_set(r->in.account_name);
327 r->in.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
328 (int)(random() % 100));
329 fldname = "account_name";
331 /* update the test's user name in case it's about to change */
332 *user_name = talloc_strdup(mem_ctx, r->in.account_name);
336 continue_if_field_set(r->in.full_name);
337 r->in.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
338 (unsigned int)random(), (unsigned int)random());
339 fldname = "full_name";
342 case acct_description:
343 continue_if_field_set(r->in.description);
344 r->in.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
346 fldname = "description";
349 case acct_home_directory:
350 continue_if_field_set(r->in.home_directory);
351 homedir = home_dirs[random() % ARRAY_SIZE(home_dirs)];
352 r->in.home_directory = talloc_strdup(mem_ctx, homedir);
353 fldname = "home_dir";
356 case acct_home_drive:
357 continue_if_field_set(r->in.home_drive);
358 homedrive = home_drives[random() % ARRAY_SIZE(home_drives)];
359 r->in.home_drive = talloc_strdup(mem_ctx, homedrive);
360 fldname = "home_drive";
364 continue_if_field_set(r->in.comment);
365 r->in.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
366 (unsigned long)random(), (unsigned long)random());
370 case acct_logon_script:
371 continue_if_field_set(r->in.logon_script);
372 logonscript = logon_scripts[random() % ARRAY_SIZE(logon_scripts)];
373 r->in.logon_script = talloc_strdup(mem_ctx, logonscript);
374 fldname = "logon_script";
377 case acct_profile_path:
378 continue_if_field_set(r->in.profile_path);
379 r->in.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
380 (unsigned long)random(), (unsigned int)random());
381 fldname = "profile_path";
385 continue_if_field_set(r->in.acct_expiry);
386 now = timeval_add(&now, (random() % (31*24*60*60)), 0);
387 r->in.acct_expiry = (struct timeval *)talloc_memdup(mem_ctx, &now, sizeof(now));
388 fldname = "acct_expiry";
392 continue_if_field_set(r->in.acct_flags);
393 r->in.acct_flags = flags[random() % ARRAY_SIZE(flags)];
394 fldname = "acct_flags";
398 fldname = "unknown_field";
401 torture_comment(tctx, ((i < num_changes - 1) ? "%s," : "%s"), fldname);
403 /* disable requested field (it's supposed to be the only one used) */
404 if (req_change != none) req_change = none;
407 torture_comment(tctx, "]\n");
411 #define TEST_STR_FLD(fld) \
412 if (!strequal(req.in.fld, user_req.out.fld)) { \
413 torture_comment(torture, "failed to change '%s'\n", #fld); \
418 #define TEST_TIME_FLD(fld) \
419 if (timeval_compare(req.in.fld, user_req.out.fld)) { \
420 torture_comment(torture, "failed to change '%s'\n", #fld); \
425 #define TEST_NUM_FLD(fld) \
426 if (req.in.fld != user_req.out.fld) { \
427 torture_comment(torture, "failed to change '%s'\n", #fld); \
433 static bool _libnet_context_init_pipes(struct torture_context *tctx,
434 struct libnet_context *libnet_ctx)
438 /* connect SAMR pipe */
439 status = torture_rpc_connection(tctx,
440 &libnet_ctx->samr.pipe,
442 torture_assert_ntstatus_ok(tctx, status, "Failed to open SAMR pipe");
444 libnet_ctx->samr.samr_handle = libnet_ctx->samr.pipe->binding_handle;
447 /* connect LSARPC pipe */
448 status = torture_rpc_connection(tctx,
449 &libnet_ctx->lsa.pipe,
451 torture_assert_ntstatus_ok(tctx, status, "Failed to open LSA pipe");
453 libnet_ctx->lsa.lsa_handle = libnet_ctx->lsa.pipe->binding_handle;
458 bool torture_modifyuser(struct torture_context *torture)
461 struct dcerpc_pipe *p;
462 TALLOC_CTX *prep_mem_ctx;
463 struct policy_handle h;
464 struct lsa_String domain_name;
466 struct libnet_context *ctx;
467 struct libnet_ModifyUser req;
468 struct libnet_UserInfo user_req;
471 struct dcerpc_binding_handle *b;
473 prep_mem_ctx = talloc_init("prepare test_deleteuser");
475 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
476 ctx->cred = cmdline_credentials;
478 status = torture_rpc_connection(torture,
481 if (!NT_STATUS_IS_OK(status)) {
485 b = p->binding_handle;
487 name = talloc_strdup(prep_mem_ctx, TEST_USERNAME);
489 domain_name.string = lp_workgroup(torture->lp_ctx);
490 if (!test_domain_open(torture, b, &domain_name, prep_mem_ctx, &h, NULL)) {
495 if (!test_user_create(torture, b, prep_mem_ctx, &h, name, NULL)) {
500 torture_comment(torture, "Testing change of all fields - each single one in turn\n");
502 if (!_libnet_context_init_pipes(torture, ctx)) {
506 for (fld = USER_FIELD_FIRST; fld <= USER_FIELD_LAST; fld++) {
508 req.in.domain_name = lp_workgroup(torture->lp_ctx);
509 req.in.user_name = name;
511 set_test_changes(torture, torture, &req, 1, &name, fld);
513 status = libnet_ModifyUser(ctx, torture, &req);
514 if (!NT_STATUS_IS_OK(status)) {
515 torture_comment(torture, "libnet_ModifyUser call failed: %s\n", nt_errstr(status));
520 ZERO_STRUCT(user_req);
521 user_req.in.domain_name = lp_workgroup(torture->lp_ctx);
522 user_req.in.data.user_name = name;
523 user_req.in.level = USER_INFO_BY_NAME;
525 status = libnet_UserInfo(ctx, torture, &user_req);
526 if (!NT_STATUS_IS_OK(status)) {
527 torture_comment(torture, "libnet_UserInfo call failed: %s\n", nt_errstr(status));
533 case acct_name: TEST_STR_FLD(account_name);
535 case acct_full_name: TEST_STR_FLD(full_name);
537 case acct_comment: TEST_STR_FLD(comment);
539 case acct_description: TEST_STR_FLD(description);
541 case acct_home_directory: TEST_STR_FLD(home_directory);
543 case acct_home_drive: TEST_STR_FLD(home_drive);
545 case acct_logon_script: TEST_STR_FLD(logon_script);
547 case acct_profile_path: TEST_STR_FLD(profile_path);
549 case acct_expiry: TEST_TIME_FLD(acct_expiry);
551 case acct_flags: TEST_NUM_FLD(acct_flags);
559 if (!test_cleanup(torture, ctx->samr.pipe->binding_handle,
560 torture, &ctx->samr.handle, TEST_USERNAME)) {
561 torture_comment(torture, "cleanup failed\n");
566 if (!test_samr_close(torture, ctx->samr.pipe->binding_handle, torture, &ctx->samr.handle)) {
567 torture_comment(torture, "domain close failed\n");
573 talloc_free(prep_mem_ctx);
578 bool torture_userinfo_api(struct torture_context *torture)
580 const char *name = TEST_USERNAME;
583 TALLOC_CTX *mem_ctx = NULL, *prep_mem_ctx;
584 struct libnet_context *ctx;
585 struct dcerpc_pipe *p;
586 struct policy_handle h;
587 struct lsa_String domain_name;
588 struct libnet_UserInfo req;
589 struct dcerpc_binding_handle *b;
591 prep_mem_ctx = talloc_init("prepare torture user info");
593 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
594 ctx->cred = cmdline_credentials;
596 status = torture_rpc_connection(torture,
599 if (!NT_STATUS_IS_OK(status)) {
602 b = p->binding_handle;
604 domain_name.string = lp_workgroup(torture->lp_ctx);
605 if (!test_domain_open(torture, b, &domain_name, prep_mem_ctx, &h, NULL)) {
610 if (!test_user_create(torture, b, prep_mem_ctx, &h, name, NULL)) {
615 mem_ctx = talloc_init("torture user info");
619 req.in.domain_name = domain_name.string;
620 req.in.data.user_name = name;
621 req.in.level = USER_INFO_BY_NAME;
623 status = libnet_UserInfo(ctx, mem_ctx, &req);
624 if (!NT_STATUS_IS_OK(status)) {
625 torture_comment(torture, "libnet_UserInfo call failed: %s\n", nt_errstr(status));
630 if (!test_cleanup(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle, TEST_USERNAME)) {
631 torture_comment(torture, "cleanup failed\n");
636 if (!test_samr_close(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
637 torture_comment(torture, "domain close failed\n");
644 talloc_free(mem_ctx);
649 bool torture_userlist(struct torture_context *torture)
653 TALLOC_CTX *mem_ctx = NULL;
654 struct libnet_context *ctx;
655 struct lsa_String domain_name;
656 struct libnet_UserList req;
659 ctx = libnet_context_init(torture->ev, torture->lp_ctx);
660 ctx->cred = cmdline_credentials;
662 domain_name.string = lp_workgroup(torture->lp_ctx);
663 mem_ctx = talloc_init("torture user list");
667 torture_comment(torture, "listing user accounts:\n");
671 req.in.domain_name = domain_name.string;
672 req.in.page_size = 128;
673 req.in.resume_index = req.out.resume_index;
675 status = libnet_UserList(ctx, mem_ctx, &req);
676 if (!NT_STATUS_IS_OK(status) &&
677 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) break;
679 for (i = 0; i < req.out.count; i++) {
680 torture_comment(torture, "\tuser: %s, sid=%s\n",
681 req.out.users[i].username, req.out.users[i].sid);
684 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
686 if (!(NT_STATUS_IS_OK(status) ||
687 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))) {
688 torture_comment(torture, "libnet_UserList call failed: %s\n", nt_errstr(status));
693 if (!test_samr_close(torture, ctx->samr.pipe->binding_handle, mem_ctx, &ctx->samr.handle)) {
694 torture_comment(torture, "samr domain close failed\n");
699 if (!test_lsa_close(torture, ctx->lsa.pipe->binding_handle, mem_ctx, &ctx->lsa.handle)) {
700 torture_comment(torture, "lsa domain close failed\n");
707 talloc_free(mem_ctx);