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));
500 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p,
502 struct policy_handle *acct_handle,
506 struct lsa_EnumAccountRights r;
507 struct lsa_RightSet rights;
509 printf("Testing EnumAccountRights\n");
511 r.in.handle = acct_handle;
513 r.out.rights = &rights;
515 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
516 if (!NT_STATUS_IS_OK(status)) {
517 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
525 static BOOL test_QuerySecObj(struct dcerpc_pipe *p,
527 struct policy_handle *handle,
528 struct policy_handle *acct_handle)
531 struct lsa_QuerySecObj r;
533 printf("Testing QuerySecObj\n");
535 r.in.handle = acct_handle;
538 status = dcerpc_lsa_QuerySecObj(p, mem_ctx, &r);
539 if (!NT_STATUS_IS_OK(status)) {
540 printf("QuerySecObj failed - %s\n", nt_errstr(status));
547 static BOOL test_OpenAccount(struct dcerpc_pipe *p,
549 struct policy_handle *handle,
553 struct lsa_OpenAccount r;
554 struct policy_handle acct_handle;
556 printf("Testing OpenAccount\n");
558 r.in.handle = handle;
560 r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
561 r.out.acct_handle = &acct_handle;
563 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
564 if (!NT_STATUS_IS_OK(status)) {
565 printf("OpenAccount failed - %s\n", nt_errstr(status));
569 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
573 if (!test_QuerySecObj(p, mem_ctx, handle, &acct_handle)) {
580 static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
582 struct policy_handle *handle)
585 struct lsa_EnumAccounts r;
586 struct lsa_SidArray sids1, sids2;
587 uint32_t resume_handle = 0;
590 printf("\ntesting EnumAccounts\n");
592 r.in.handle = handle;
593 r.in.resume_handle = &resume_handle;
594 r.in.num_entries = 100;
595 r.out.resume_handle = &resume_handle;
599 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
600 if (!NT_STATUS_IS_OK(status)) {
601 printf("EnumAccounts failed - %s\n", nt_errstr(status));
605 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
609 printf("testing all accounts\n");
610 for (i=0;i<sids1.num_sids;i++) {
611 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
612 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
616 if (sids1.num_sids < 3) {
620 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
622 r.in.num_entries = 1;
625 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
626 if (!NT_STATUS_IS_OK(status)) {
627 printf("EnumAccounts failed - %s\n", nt_errstr(status));
631 if (sids2.num_sids != 1) {
632 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
639 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
641 struct policy_handle *handle,
642 struct lsa_Name *priv_name)
644 struct lsa_LookupPrivDisplayName r;
646 /* produce a reasonable range of language output without screwing up
648 uint16 language_id = (random() % 4) + 0x409;
650 printf("testing LookupPrivDisplayName(%s)\n", priv_name->name);
652 r.in.handle = handle;
653 r.in.name = priv_name;
654 r.in.language_id = &language_id;
655 r.out.language_id = &language_id;
658 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
659 if (!NT_STATUS_IS_OK(status)) {
660 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
663 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
664 priv_name->name, r.out.disp_name->name,
665 *r.in.language_id, *r.out.language_id);
670 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
672 struct policy_handle *handle,
673 struct lsa_Name *priv_name)
675 struct lsa_EnumAccountsWithUserRight r;
676 struct lsa_SidArray sids;
681 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->name);
683 r.in.handle = handle;
684 r.in.name = priv_name;
687 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
689 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
690 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
694 if (!NT_STATUS_IS_OK(status)) {
695 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
703 static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
705 struct policy_handle *handle)
708 struct lsa_EnumPrivs r;
709 struct lsa_PrivArray privs1;
710 uint32_t resume_handle = 0;
714 printf("\ntesting EnumPrivs\n");
716 r.in.handle = handle;
717 r.in.resume_handle = &resume_handle;
718 r.in.max_count = 1000;
719 r.out.resume_handle = &resume_handle;
720 r.out.privs = &privs1;
723 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
724 if (!NT_STATUS_IS_OK(status)) {
725 printf("EnumPrivs failed - %s\n", nt_errstr(status));
729 for (i = 0; i< privs1.count; i++) {
730 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
731 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
740 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
742 struct policy_handle *handle)
744 struct lsa_EnumTrustDom r;
746 uint32_t resume_handle = 0;
747 struct lsa_DomainList domains;
749 printf("\nTesting EnumTrustDom\n");
751 r.in.handle = handle;
752 r.in.resume_handle = &resume_handle;
753 r.in.num_entries = 1000;
754 r.out.domains = &domains;
755 r.out.resume_handle = &resume_handle;
757 status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
759 /* NO_MORE_ENTRIES is allowed */
760 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
764 if (!NT_STATUS_IS_OK(status)) {
765 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
772 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p,
774 struct policy_handle *handle)
776 struct lsa_QueryInfoPolicy r;
780 printf("\nTesting QueryInfoPolicy\n");
783 r.in.handle = handle;
786 printf("\ntrying QueryInfoPolicy level %d\n", i);
788 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
790 if ((i == 9 || i == 10 || i == 11) &&
791 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
792 printf("server failed level %u (OK)\n", i);
796 if (!NT_STATUS_IS_OK(status)) {
797 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
806 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p,
808 struct policy_handle *handle)
810 struct lsa_QueryInfoPolicy2 r;
814 printf("\nTesting QueryInfoPolicy2\n");
817 r.in.handle = handle;
820 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
822 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
824 if ((i == 9 || i == 10 || i == 11) &&
825 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
826 printf("server failed level %u (OK)\n", i);
830 if (!NT_STATUS_IS_OK(status)) {
831 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
840 static BOOL test_Close(struct dcerpc_pipe *p,
842 struct policy_handle *handle)
846 struct policy_handle handle2;
848 printf("\ntesting Close\n");
850 r.in.handle = handle;
851 r.out.handle = &handle2;
853 status = dcerpc_lsa_Close(p, mem_ctx, &r);
854 if (!NT_STATUS_IS_OK(status)) {
855 printf("Close failed - %s\n", nt_errstr(status));
859 status = dcerpc_lsa_Close(p, mem_ctx, &r);
860 /* its really a fault - we need a status code for rpc fault */
861 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
862 printf("Close failed - %s\n", nt_errstr(status));
871 BOOL torture_rpc_lsa(int dummy)
874 struct dcerpc_pipe *p;
877 struct policy_handle handle;
879 mem_ctx = talloc_init("torture_rpc_lsa");
881 status = torture_rpc_connection(&p,
884 DCERPC_LSARPC_VERSION);
885 if (!NT_STATUS_IS_OK(status)) {
889 if (!test_OpenPolicy(p, mem_ctx)) {
893 if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
897 if (!test_many_LookupSids(p, mem_ctx, &handle)) {
901 if (!test_CreateAccount(p, mem_ctx, &handle)) {
905 if (!test_CreateSecret(p, mem_ctx, &handle)) {
909 if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
913 if (!test_EnumAccounts(p, mem_ctx, &handle)) {
917 if (!test_EnumPrivs(p, mem_ctx, &handle)) {
921 if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
925 if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
929 if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
934 if (!test_Delete(p, mem_ctx, &handle)) {
939 if (!test_Close(p, mem_ctx, &handle)) {
943 talloc_destroy(mem_ctx);
945 torture_rpc_close(p);