2 Unix SMB/CIFS implementation.
3 test suite for lsa rpc operations
5 Copyright (C) Andrew Tridgell 2003
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static void init_lsa_Name(struct lsa_Name *name, const char *s)
29 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
31 struct lsa_ObjectAttribute attr;
32 struct policy_handle handle;
33 struct lsa_QosInfo qos;
34 struct lsa_OpenPolicy r;
36 uint16_t system_name = '\\';
38 printf("\ntesting OpenPolicy\n");
41 qos.impersonation_level = 2;
43 qos.effective_only = 0;
47 attr.object_name = NULL;
52 r.in.system_name = &system_name;
54 r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
55 r.out.handle = &handle;
57 status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
58 if (!NT_STATUS_IS_OK(status)) {
59 printf("OpenPolicy failed - %s\n", nt_errstr(status));
67 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
68 struct policy_handle *handle)
70 struct lsa_ObjectAttribute attr;
71 struct lsa_QosInfo qos;
72 struct lsa_OpenPolicy2 r;
75 printf("\ntesting OpenPolicy2\n");
78 qos.impersonation_level = 2;
80 qos.effective_only = 0;
84 attr.object_name = NULL;
89 r.in.system_name = "\\";
91 r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
92 r.out.handle = handle;
94 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
95 if (!NT_STATUS_IS_OK(status)) {
96 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
103 static BOOL test_LookupNames(struct dcerpc_pipe *p,
105 struct policy_handle *handle,
106 struct lsa_TransNameArray *tnames)
108 struct lsa_LookupNames r;
109 struct lsa_TransSidArray sids;
110 struct lsa_Name *names;
115 printf("\nTesting LookupNames with %d names\n", tnames->count);
120 names = talloc(mem_ctx, tnames->count * sizeof(names[0]));
121 for (i=0;i<tnames->count;i++) {
122 init_lsa_Name(&names[i], tnames->names[i].name.name);
125 r.in.handle = handle;
126 r.in.num_names = tnames->count;
131 r.out.count = &count;
134 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
135 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
136 printf("LookupNames failed - %s\n", nt_errstr(status));
146 static BOOL test_LookupSids(struct dcerpc_pipe *p,
148 struct policy_handle *handle,
149 struct lsa_SidArray *sids)
151 struct lsa_LookupSids r;
152 struct lsa_TransNameArray names;
153 uint32_t count = sids->num_sids;
156 printf("\nTesting LookupSids\n");
161 r.in.handle = handle;
166 r.out.count = &count;
167 r.out.names = &names;
169 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
170 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
171 printf("LookupSids failed - %s\n", nt_errstr(status));
177 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
184 static BOOL test_many_LookupSids(struct dcerpc_pipe *p,
186 struct policy_handle *handle)
188 struct lsa_LookupSids r;
189 struct lsa_TransNameArray names;
192 struct lsa_SidArray sids;
195 printf("\nTesting LookupSids with lots of SIDs\n");
200 sids.num_sids = 1000;
202 sids.sids = talloc_array_p(mem_ctx, struct lsa_SidPtr, sids.num_sids);
204 for (i=0; i<sids.num_sids; i++) {
205 const char *sidstr = "S-1-5-32-545";
206 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
209 count = sids.num_sids;
211 r.in.handle = handle;
215 r.in.count = &names.count;
216 r.out.count = &count;
217 r.out.names = &names;
219 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
220 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
221 printf("LookupSids failed - %s\n", nt_errstr(status));
227 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
234 static BOOL test_LookupPrivName(struct dcerpc_pipe *p,
236 struct policy_handle *handle,
237 struct lsa_LUID *luid)
240 struct lsa_LookupPrivName r;
242 r.in.handle = handle;
245 status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
246 if (!NT_STATUS_IS_OK(status)) {
247 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
254 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p,
256 struct policy_handle *handle,
257 struct policy_handle *acct_handle)
260 struct lsa_EnumPrivsAccount r;
262 printf("Testing EnumPrivsAccount\n");
264 r.in.handle = acct_handle;
266 status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
267 if (!NT_STATUS_IS_OK(status)) {
268 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
274 for (i=0;i<r.out.privs->count;i++) {
275 test_LookupPrivName(p, mem_ctx, handle,
276 &r.out.privs->set[i].luid);
283 static BOOL test_Delete(struct dcerpc_pipe *p,
285 struct policy_handle *handle)
290 printf("\ntesting Delete\n");
292 r.in.handle = handle;
293 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
294 if (!NT_STATUS_IS_OK(status)) {
295 printf("Delete failed - %s\n", nt_errstr(status));
305 static BOOL test_CreateAccount(struct dcerpc_pipe *p,
307 struct policy_handle *handle)
310 struct lsa_CreateAccount r;
311 struct dom_sid2 *newsid;
312 struct policy_handle acct_handle;
314 newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
316 printf("Testing CreateAccount\n");
318 r.in.handle = handle;
320 r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
321 r.out.acct_handle = &acct_handle;
323 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
324 if (!NT_STATUS_IS_OK(status)) {
325 printf("CreateAccount failed - %s\n", nt_errstr(status));
329 if (!test_Delete(p, mem_ctx, &acct_handle)) {
337 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p,
339 struct policy_handle *handle)
342 struct lsa_CreateTrustedDomain r;
343 struct lsa_TrustInformation trustinfo;
344 struct dom_sid *domsid;
345 struct policy_handle dom_handle;
347 printf("Testing CreateTrustedDomain\n");
349 domsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-21-97398-379795-12345");
351 trustinfo.sid = domsid;
352 init_lsa_Name(&trustinfo.name, "torturedomain");
354 r.in.handle = handle;
355 r.in.info = &trustinfo;
356 r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
357 r.out.dom_handle = &dom_handle;
359 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
360 if (!NT_STATUS_IS_OK(status)) {
361 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
365 if (!test_Delete(p, mem_ctx, &dom_handle)) {
372 static BOOL test_CreateSecret(struct dcerpc_pipe *p,
374 struct policy_handle *handle)
377 struct lsa_CreateSecret r;
378 struct lsa_OpenSecret r2;
379 struct lsa_SetSecret r3;
380 struct lsa_QuerySecret r4;
381 struct policy_handle sec_handle, sec_handle2;
383 struct lsa_DATA_BUF buf1;
384 struct lsa_DATA_BUF_PTR bufp1;
387 DATA_BLOB session_key;
388 NTTIME old_mtime, new_mtime;
389 DATA_BLOB blob1, blob2;
390 const char *secret1 = "abcdef12345699qwerty";
394 printf("Testing CreateSecret\n");
396 asprintf(&secname, "torturesecret-%u", (uint_t)random());
398 init_lsa_Name(&r.in.name, secname);
400 r.in.handle = handle;
401 r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
402 r.out.sec_handle = &sec_handle;
404 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
405 if (!NT_STATUS_IS_OK(status)) {
406 printf("CreateSecret failed - %s\n", nt_errstr(status));
410 r2.in.handle = handle;
411 r2.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
412 r2.in.name = r.in.name;
413 r2.out.sec_handle = &sec_handle2;
415 printf("Testing OpenSecret\n");
417 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
418 if (!NT_STATUS_IS_OK(status)) {
419 printf("OpenSecret failed - %s\n", nt_errstr(status));
423 status = dcerpc_fetch_session_key(p, &session_key);
424 if (!NT_STATUS_IS_OK(status)) {
425 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
429 enc_key = sess_encrypt_string(secret1, &session_key);
431 r3.in.handle = &sec_handle;
432 r3.in.new_val = &buf1;
433 r3.in.old_val = NULL;
434 r3.in.new_val->data = enc_key.data;
435 r3.in.new_val->length = enc_key.length;
436 r3.in.new_val->size = enc_key.length;
438 printf("Testing SetSecret\n");
440 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
441 if (!NT_STATUS_IS_OK(status)) {
442 printf("SetSecret failed - %s\n", nt_errstr(status));
446 data_blob_free(&enc_key);
448 ZERO_STRUCT(new_mtime);
449 ZERO_STRUCT(old_mtime);
451 /* fetch the secret back again */
452 r4.in.handle = &sec_handle;
453 r4.in.new_val = &bufp1;
454 r4.in.new_mtime = &new_mtime;
455 r4.in.old_val = NULL;
456 r4.in.old_mtime = NULL;
460 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
461 if (!NT_STATUS_IS_OK(status)) {
462 printf("QuerySecret failed - %s\n", nt_errstr(status));
466 if (r4.out.new_val->buf == NULL) {
467 printf("No secret buffer returned\n");
470 blob1.data = r4.out.new_val->buf->data;
471 blob1.length = r4.out.new_val->buf->length;
473 blob2 = data_blob(NULL, blob1.length);
475 secret2 = sess_decrypt_string(&blob1, &session_key);
477 printf("returned secret '%s'\n", secret2);
479 if (strcmp(secret1, secret2) != 0) {
480 printf("Returned secret doesn't match\n");
485 if (!test_Delete(p, mem_ctx, &sec_handle)) {
489 d.in.handle = &sec_handle2;
490 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
491 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
492 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
499 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p,
501 struct policy_handle *acct_handle,
505 struct lsa_EnumAccountRights r;
506 struct lsa_RightSet rights;
508 printf("Testing EnumAccountRights\n");
510 r.in.handle = acct_handle;
512 r.out.rights = &rights;
514 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
515 if (!NT_STATUS_IS_OK(status)) {
516 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
524 static BOOL test_QuerySecObj(struct dcerpc_pipe *p,
526 struct policy_handle *handle,
527 struct policy_handle *acct_handle)
530 struct lsa_QuerySecObj r;
532 printf("Testing QuerySecObj\n");
534 r.in.handle = acct_handle;
537 status = dcerpc_lsa_QuerySecObj(p, mem_ctx, &r);
538 if (!NT_STATUS_IS_OK(status)) {
539 printf("QuerySecObj failed - %s\n", nt_errstr(status));
546 static BOOL test_OpenAccount(struct dcerpc_pipe *p,
548 struct policy_handle *handle,
552 struct lsa_OpenAccount r;
553 struct policy_handle acct_handle;
555 printf("Testing OpenAccount\n");
557 r.in.handle = handle;
559 r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
560 r.out.acct_handle = &acct_handle;
562 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
563 if (!NT_STATUS_IS_OK(status)) {
564 printf("OpenAccount failed - %s\n", nt_errstr(status));
568 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
572 if (!test_QuerySecObj(p, mem_ctx, handle, &acct_handle)) {
579 static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
581 struct policy_handle *handle)
584 struct lsa_EnumAccounts r;
585 struct lsa_SidArray sids1, sids2;
586 uint32_t resume_handle = 0;
589 printf("\ntesting EnumAccounts\n");
591 r.in.handle = handle;
592 r.in.resume_handle = &resume_handle;
593 r.in.num_entries = 100;
594 r.out.resume_handle = &resume_handle;
598 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
599 if (!NT_STATUS_IS_OK(status)) {
600 printf("EnumAccounts failed - %s\n", nt_errstr(status));
604 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
608 printf("testing all accounts\n");
609 for (i=0;i<sids1.num_sids;i++) {
610 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
611 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
615 if (sids1.num_sids < 3) {
619 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
621 r.in.num_entries = 1;
624 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
625 if (!NT_STATUS_IS_OK(status)) {
626 printf("EnumAccounts failed - %s\n", nt_errstr(status));
630 if (sids2.num_sids != 1) {
631 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
639 static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
641 struct policy_handle *handle)
644 struct lsa_EnumPrivs r;
645 struct lsa_PrivArray privs1;
646 uint32_t resume_handle = 0;
648 printf("\ntesting EnumPrivs\n");
650 r.in.handle = handle;
651 r.in.resume_handle = &resume_handle;
652 r.in.max_count = 1000;
653 r.out.resume_handle = &resume_handle;
654 r.out.privs = &privs1;
657 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
658 if (!NT_STATUS_IS_OK(status)) {
659 printf("EnumPrivs failed - %s\n", nt_errstr(status));
667 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
669 struct policy_handle *handle)
671 struct lsa_EnumTrustDom r;
673 uint32_t resume_handle = 0;
674 struct lsa_DomainList domains;
676 printf("\nTesting EnumTrustDom\n");
678 r.in.handle = handle;
679 r.in.resume_handle = &resume_handle;
680 r.in.num_entries = 1000;
681 r.out.domains = &domains;
682 r.out.resume_handle = &resume_handle;
684 status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
686 /* NO_MORE_ENTRIES is allowed */
687 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
691 if (!NT_STATUS_IS_OK(status)) {
692 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
699 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p,
701 struct policy_handle *handle)
703 struct lsa_QueryInfoPolicy r;
707 printf("\nTesting QueryInfoPolicy\n");
710 r.in.handle = handle;
713 printf("\ntrying QueryInfoPolicy level %d\n", i);
715 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
717 if ((i == 9 || i == 10 || i == 11) &&
718 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
719 printf("server failed level %u (OK)\n", i);
723 if (!NT_STATUS_IS_OK(status)) {
724 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
733 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p,
735 struct policy_handle *handle)
737 struct lsa_QueryInfoPolicy2 r;
741 printf("\nTesting QueryInfoPolicy2\n");
744 r.in.handle = handle;
747 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
749 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
751 if ((i == 9 || i == 10 || i == 11) &&
752 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
753 printf("server failed level %u (OK)\n", i);
757 if (!NT_STATUS_IS_OK(status)) {
758 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
767 static BOOL test_Close(struct dcerpc_pipe *p,
769 struct policy_handle *handle)
773 struct policy_handle handle2;
775 printf("\ntesting Close\n");
777 r.in.handle = handle;
778 r.out.handle = &handle2;
780 status = dcerpc_lsa_Close(p, mem_ctx, &r);
781 if (!NT_STATUS_IS_OK(status)) {
782 printf("Close failed - %s\n", nt_errstr(status));
786 status = dcerpc_lsa_Close(p, mem_ctx, &r);
787 /* its really a fault - we need a status code for rpc fault */
788 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
789 printf("Close failed - %s\n", nt_errstr(status));
798 BOOL torture_rpc_lsa(int dummy)
801 struct dcerpc_pipe *p;
804 struct policy_handle handle;
806 mem_ctx = talloc_init("torture_rpc_lsa");
808 status = torture_rpc_connection(&p,
811 DCERPC_LSARPC_VERSION);
812 if (!NT_STATUS_IS_OK(status)) {
816 if (!test_OpenPolicy(p, mem_ctx)) {
820 if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
824 if (!test_many_LookupSids(p, mem_ctx, &handle)) {
828 if (!test_CreateAccount(p, mem_ctx, &handle)) {
832 if (!test_CreateSecret(p, mem_ctx, &handle)) {
836 if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
840 if (!test_EnumAccounts(p, mem_ctx, &handle)) {
844 if (!test_EnumPrivs(p, mem_ctx, &handle)) {
848 if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
852 if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
856 if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
861 if (!test_Delete(p, mem_ctx, &handle)) {
866 if (!test_Close(p, mem_ctx, &handle)) {
870 talloc_destroy(mem_ctx);
872 torture_rpc_close(p);