2 Unix SMB/CIFS implementation.
3 test suite for lsa rpc operations
5 Copyright (C) Andrew Tridgell 2003
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "librpc/gen_ndr/ndr_lsa.h"
26 static void init_lsa_String(struct lsa_String *name, const char *s)
31 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
33 struct lsa_ObjectAttribute attr;
34 struct policy_handle handle;
35 struct lsa_QosInfo qos;
36 struct lsa_OpenPolicy r;
38 uint16_t system_name = '\\';
40 printf("\ntesting OpenPolicy\n");
43 qos.impersonation_level = 2;
45 qos.effective_only = 0;
49 attr.object_name = NULL;
54 r.in.system_name = &system_name;
56 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
57 r.out.handle = &handle;
59 status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
60 if (!NT_STATUS_IS_OK(status)) {
61 printf("OpenPolicy failed - %s\n", nt_errstr(status));
69 BOOL test_lsa_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
70 struct policy_handle *handle)
72 struct lsa_ObjectAttribute attr;
73 struct lsa_QosInfo qos;
74 struct lsa_OpenPolicy2 r;
77 printf("\ntesting OpenPolicy2\n");
80 qos.impersonation_level = 2;
82 qos.effective_only = 0;
86 attr.object_name = NULL;
91 r.in.system_name = "\\";
93 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
94 r.out.handle = handle;
96 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
97 if (!NT_STATUS_IS_OK(status)) {
98 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
105 static BOOL test_LookupNames(struct dcerpc_pipe *p,
107 struct policy_handle *handle,
108 struct lsa_TransNameArray *tnames)
110 struct lsa_LookupNames r;
111 struct lsa_TransSidArray sids;
112 struct lsa_String *names;
117 printf("\nTesting LookupNames with %d names\n", tnames->count);
122 names = talloc_array_p(mem_ctx, struct lsa_String, tnames->count);
123 for (i=0;i<tnames->count;i++) {
124 init_lsa_String(&names[i], tnames->names[i].name.string);
127 r.in.handle = handle;
128 r.in.num_names = tnames->count;
133 r.out.count = &count;
136 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
137 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
138 printf("LookupNames failed - %s\n", nt_errstr(status));
147 static BOOL test_LookupNames2(struct dcerpc_pipe *p,
149 struct policy_handle *handle,
150 struct lsa_TransNameArray2 *tnames)
152 struct lsa_LookupNames2 r;
153 struct lsa_TransSidArray2 sids;
154 struct lsa_String *names;
159 printf("\nTesting LookupNames2 with %d names\n", tnames->count);
164 names = talloc_array_p(mem_ctx, struct lsa_String, tnames->count);
165 for (i=0;i<tnames->count;i++) {
166 init_lsa_String(&names[i], tnames->names[i].name.string);
169 r.in.handle = handle;
170 r.in.num_names = tnames->count;
177 r.out.count = &count;
180 status = dcerpc_lsa_LookupNames2(p, mem_ctx, &r);
181 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
182 printf("LookupNames2 failed - %s\n", nt_errstr(status));
192 static BOOL test_LookupNames3(struct dcerpc_pipe *p,
194 struct policy_handle *handle,
195 struct lsa_TransNameArray2 *tnames)
197 struct lsa_LookupNames3 r;
198 struct lsa_TransSidArray3 sids;
199 struct lsa_String *names;
204 printf("\nTesting LookupNames3 with %d names\n", tnames->count);
209 names = talloc_array_p(mem_ctx, struct lsa_String, tnames->count);
210 for (i=0;i<tnames->count;i++) {
211 init_lsa_String(&names[i], tnames->names[i].name.string);
214 r.in.handle = handle;
215 r.in.num_names = tnames->count;
222 r.out.count = &count;
225 status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
226 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
227 printf("LookupNames3 failed - %s\n", nt_errstr(status));
237 static BOOL test_LookupSids(struct dcerpc_pipe *p,
239 struct policy_handle *handle,
240 struct lsa_SidArray *sids)
242 struct lsa_LookupSids r;
243 struct lsa_TransNameArray names;
244 uint32_t count = sids->num_sids;
247 printf("\nTesting LookupSids\n");
252 r.in.handle = handle;
257 r.out.count = &count;
258 r.out.names = &names;
260 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
261 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
262 printf("LookupSids failed - %s\n", nt_errstr(status));
268 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
276 static BOOL test_LookupSids2(struct dcerpc_pipe *p,
278 struct policy_handle *handle,
279 struct lsa_SidArray *sids)
281 struct lsa_LookupSids2 r;
282 struct lsa_TransNameArray2 names;
283 uint32_t count = sids->num_sids;
286 printf("\nTesting LookupSids2\n");
291 r.in.handle = handle;
298 r.out.count = &count;
299 r.out.names = &names;
301 status = dcerpc_lsa_LookupSids2(p, mem_ctx, &r);
302 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
303 printf("LookupSids2 failed - %s\n", nt_errstr(status));
309 if (!test_LookupNames2(p, mem_ctx, handle, &names)) {
313 if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
320 static BOOL test_LookupSids3(struct dcerpc_pipe *p,
322 struct policy_handle *handle,
323 struct lsa_SidArray *sids)
325 struct lsa_LookupSids3 r;
326 struct lsa_TransNameArray2 names;
327 uint32_t count = sids->num_sids;
330 printf("\nTesting LookupSids3\n");
341 r.out.count = &count;
342 r.out.names = &names;
344 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
345 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
346 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
347 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
348 printf("not considering %s to be an error\n", nt_errstr(status));
351 printf("LookupSids3 failed - %s - not considered an error\n",
358 if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
365 static BOOL test_many_LookupSids(struct dcerpc_pipe *p,
367 struct policy_handle *handle)
369 struct lsa_LookupSids r;
370 struct lsa_TransNameArray names;
373 struct lsa_SidArray sids;
376 printf("\nTesting LookupSids with lots of SIDs\n");
383 sids.sids = talloc_array_p(mem_ctx, struct lsa_SidPtr, sids.num_sids);
385 for (i=0; i<sids.num_sids; i++) {
386 const char *sidstr = "S-1-5-32-545";
387 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
390 count = sids.num_sids;
392 r.in.handle = handle;
396 r.in.count = &names.count;
397 r.out.count = &count;
398 r.out.names = &names;
400 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
401 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
402 printf("LookupSids failed - %s\n", nt_errstr(status));
408 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
415 static BOOL test_LookupPrivValue(struct dcerpc_pipe *p,
417 struct policy_handle *handle,
418 struct lsa_String *name)
421 struct lsa_LookupPrivValue r;
422 struct lsa_LUID luid;
424 r.in.handle = handle;
428 status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
429 if (!NT_STATUS_IS_OK(status)) {
430 printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
437 static BOOL test_LookupPrivName(struct dcerpc_pipe *p,
439 struct policy_handle *handle,
440 struct lsa_LUID *luid)
443 struct lsa_LookupPrivName r;
445 r.in.handle = handle;
448 status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
449 if (!NT_STATUS_IS_OK(status)) {
450 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
457 static BOOL test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p,
459 struct policy_handle *acct_handle,
460 struct lsa_LUID *luid)
463 struct lsa_RemovePrivilegesFromAccount r;
464 struct lsa_PrivilegeSet privs;
467 printf("Testing RemovePrivilegesFromAccount\n");
469 r.in.handle = acct_handle;
475 privs.set = talloc_array_p(mem_ctx, struct lsa_LUIDAttribute, 1);
476 privs.set[0].luid = *luid;
477 privs.set[0].attribute = 0;
479 status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
480 if (!NT_STATUS_IS_OK(status)) {
481 printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
488 static BOOL test_AddPrivilegesToAccount(struct dcerpc_pipe *p,
490 struct policy_handle *acct_handle,
491 struct lsa_LUID *luid)
494 struct lsa_AddPrivilegesToAccount r;
495 struct lsa_PrivilegeSet privs;
498 printf("Testing AddPrivilegesToAccount\n");
500 r.in.handle = acct_handle;
505 privs.set = talloc_array_p(mem_ctx, struct lsa_LUIDAttribute, 1);
506 privs.set[0].luid = *luid;
507 privs.set[0].attribute = 0;
509 status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
510 if (!NT_STATUS_IS_OK(status)) {
511 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
518 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p,
520 struct policy_handle *handle,
521 struct policy_handle *acct_handle)
524 struct lsa_EnumPrivsAccount r;
527 printf("Testing EnumPrivsAccount\n");
529 r.in.handle = acct_handle;
531 status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
532 if (!NT_STATUS_IS_OK(status)) {
533 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
537 if (r.out.privs && r.out.privs->count > 0) {
539 for (i=0;i<r.out.privs->count;i++) {
540 test_LookupPrivName(p, mem_ctx, handle,
541 &r.out.privs->set[i].luid);
544 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle,
545 &r.out.privs->set[0].luid);
546 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle,
547 &r.out.privs->set[0].luid);
553 static BOOL test_Delete(struct dcerpc_pipe *p,
555 struct policy_handle *handle)
560 printf("\ntesting Delete\n");
562 r.in.handle = handle;
563 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
564 if (!NT_STATUS_IS_OK(status)) {
565 printf("Delete failed - %s\n", nt_errstr(status));
575 static BOOL test_CreateAccount(struct dcerpc_pipe *p,
577 struct policy_handle *handle)
580 struct lsa_CreateAccount r;
581 struct dom_sid2 *newsid;
582 struct policy_handle acct_handle;
584 newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
586 printf("Testing CreateAccount\n");
588 r.in.handle = handle;
590 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
591 r.out.acct_handle = &acct_handle;
593 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
594 if (!NT_STATUS_IS_OK(status)) {
595 printf("CreateAccount failed - %s\n", nt_errstr(status));
599 if (!test_Delete(p, mem_ctx, &acct_handle)) {
606 static BOOL test_DeleteTrustedDomain(struct dcerpc_pipe *p,
608 struct policy_handle *handle,
609 struct lsa_String name)
612 struct lsa_OpenTrustedDomainByName r;
613 struct policy_handle trustdom_handle;
615 r.in.handle = handle;
617 r.in.access_mask = SEC_STD_DELETE;
618 r.out.trustdom_handle = &trustdom_handle;
620 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
621 if (!NT_STATUS_IS_OK(status)) {
622 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
626 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
634 static BOOL test_CreateSecret(struct dcerpc_pipe *p,
636 struct policy_handle *handle)
639 struct lsa_CreateSecret r;
640 struct lsa_OpenSecret r2;
641 struct lsa_SetSecret r3;
642 struct lsa_QuerySecret r4;
643 struct lsa_SetSecret r5;
644 struct lsa_QuerySecret r6;
645 struct lsa_SetSecret r7;
646 struct lsa_QuerySecret r8;
647 struct policy_handle sec_handle, sec_handle2, sec_handle3;
649 struct lsa_DATA_BUF buf1;
650 struct lsa_DATA_BUF_PTR bufp1;
651 struct lsa_DATA_BUF_PTR bufp2;
654 DATA_BLOB session_key;
655 NTTIME old_mtime, new_mtime;
656 DATA_BLOB blob1, blob2;
657 const char *secret1 = "abcdef12345699qwerty";
659 const char *secret3 = "ABCDEF12345699QWERTY";
661 const char *secret5 = "NEW-SAMBA4-SECRET";
666 const int GLOBAL = 1;
668 secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
669 secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
671 for (i=0; i< 2; i++) {
672 printf("Testing CreateSecret of %s\n", secname[i]);
674 init_lsa_String(&r.in.name, secname[i]);
676 r.in.handle = handle;
677 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
678 r.out.sec_handle = &sec_handle;
680 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
681 if (!NT_STATUS_IS_OK(status)) {
682 printf("CreateSecret failed - %s\n", nt_errstr(status));
686 r.in.handle = handle;
687 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
688 r.out.sec_handle = &sec_handle3;
690 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
691 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
692 printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
696 r2.in.handle = handle;
697 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
698 r2.in.name = r.in.name;
699 r2.out.sec_handle = &sec_handle2;
701 printf("Testing OpenSecret\n");
703 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
704 if (!NT_STATUS_IS_OK(status)) {
705 printf("OpenSecret failed - %s\n", nt_errstr(status));
709 status = dcerpc_fetch_session_key(p, &session_key);
710 if (!NT_STATUS_IS_OK(status)) {
711 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
715 enc_key = sess_encrypt_string(secret1, &session_key);
717 r3.in.sec_handle = &sec_handle;
718 r3.in.new_val = &buf1;
719 r3.in.old_val = NULL;
720 r3.in.new_val->data = enc_key.data;
721 r3.in.new_val->length = enc_key.length;
722 r3.in.new_val->size = enc_key.length;
724 printf("Testing SetSecret\n");
726 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
727 if (!NT_STATUS_IS_OK(status)) {
728 printf("SetSecret failed - %s\n", nt_errstr(status));
732 r3.in.sec_handle = &sec_handle;
733 r3.in.new_val = &buf1;
734 r3.in.old_val = NULL;
735 r3.in.new_val->data = enc_key.data;
736 r3.in.new_val->length = enc_key.length;
737 r3.in.new_val->size = enc_key.length;
739 /* break the encrypted data */
742 printf("Testing SetSecret with broken key\n");
744 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
745 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
746 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
750 data_blob_free(&enc_key);
752 ZERO_STRUCT(new_mtime);
753 ZERO_STRUCT(old_mtime);
755 /* fetch the secret back again */
756 r4.in.sec_handle = &sec_handle;
757 r4.in.new_val = &bufp1;
758 r4.in.new_mtime = &new_mtime;
759 r4.in.old_val = NULL;
760 r4.in.old_mtime = NULL;
764 printf("Testing QuerySecret\n");
765 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
766 if (!NT_STATUS_IS_OK(status)) {
767 printf("QuerySecret failed - %s\n", nt_errstr(status));
770 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
771 printf("No secret buffer returned\n");
774 blob1.data = r4.out.new_val->buf->data;
775 blob1.length = r4.out.new_val->buf->size;
777 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
779 secret2 = sess_decrypt_string(&blob1, &session_key);
781 if (strcmp(secret1, secret2) != 0) {
782 printf("Returned secret '%s' doesn't match '%s'\n",
789 enc_key = sess_encrypt_string(secret3, &session_key);
791 r5.in.sec_handle = &sec_handle;
792 r5.in.new_val = &buf1;
793 r5.in.old_val = NULL;
794 r5.in.new_val->data = enc_key.data;
795 r5.in.new_val->length = enc_key.length;
796 r5.in.new_val->size = enc_key.length;
798 printf("Testing SetSecret (existing value should move to old)\n");
800 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
801 if (!NT_STATUS_IS_OK(status)) {
802 printf("SetSecret failed - %s\n", nt_errstr(status));
806 data_blob_free(&enc_key);
808 ZERO_STRUCT(new_mtime);
809 ZERO_STRUCT(old_mtime);
811 /* fetch the secret back again */
812 r6.in.sec_handle = &sec_handle;
813 r6.in.new_val = &bufp1;
814 r6.in.new_mtime = &new_mtime;
815 r6.in.old_val = &bufp2;
816 r6.in.old_mtime = &old_mtime;
821 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
822 if (!NT_STATUS_IS_OK(status)) {
823 printf("QuerySecret failed - %s\n", nt_errstr(status));
827 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
828 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
829 printf("Both secret buffers and both times not returned\n");
832 blob1.data = r6.out.new_val->buf->data;
833 blob1.length = r6.out.new_val->buf->size;
835 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
837 secret4 = sess_decrypt_string(&blob1, &session_key);
839 if (strcmp(secret3, secret4) != 0) {
840 printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
844 blob1.data = r6.out.old_val->buf->data;
845 blob1.length = r6.out.old_val->buf->length;
847 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
849 secret2 = sess_decrypt_string(&blob1, &session_key);
851 if (strcmp(secret1, secret2) != 0) {
852 printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
856 if (*r6.out.new_mtime == *r6.out.old_mtime) {
857 printf("Returned secret %s had same mtime for both secrets: %s\n",
859 nt_time_string(mem_ctx, *r6.out.new_mtime));
865 enc_key = sess_encrypt_string(secret5, &session_key);
867 r7.in.sec_handle = &sec_handle;
868 r7.in.old_val = &buf1;
869 r7.in.old_val->data = enc_key.data;
870 r7.in.old_val->length = enc_key.length;
871 r7.in.old_val->size = enc_key.length;
872 r7.in.new_val = NULL;
874 printf("Testing SetSecret of old Secret only\n");
876 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
877 if (!NT_STATUS_IS_OK(status)) {
878 printf("SetSecret failed - %s\n", nt_errstr(status));
882 data_blob_free(&enc_key);
884 /* fetch the secret back again */
885 r8.in.sec_handle = &sec_handle;
886 r8.in.new_val = &bufp1;
887 r8.in.new_mtime = &new_mtime;
888 r8.in.old_val = &bufp2;
889 r8.in.old_mtime = &old_mtime;
894 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
895 if (!NT_STATUS_IS_OK(status)) {
896 printf("QuerySecret failed - %s\n", nt_errstr(status));
899 if (!r8.out.new_val || !r8.out.old_val) {
900 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
902 } else if (r8.out.new_val->buf == NULL) {
904 printf("NEW secret buffer not returned after GLOBAL OLD set\n");
907 } else if (r8.out.old_val->buf == NULL) {
908 printf("OLD secret buffer not returned after OLD set\n");
910 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
911 printf("Both times not returned after OLD set\n");
915 printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
918 blob1.data = r8.out.new_val->buf->data;
919 blob1.length = r8.out.new_val->buf->length;
921 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
923 secret6 = sess_decrypt_string(&blob1, &session_key);
925 if (strcmp(secret3, secret4) != 0) {
926 printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
930 blob1.data = r8.out.old_val->buf->data;
931 blob1.length = r8.out.old_val->buf->size;
933 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
935 secret6 = sess_decrypt_string(&blob1, &session_key);
937 if (strcmp(secret5, secret6) != 0) {
938 printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
942 if (*r8.out.new_mtime == *r8.out.old_mtime) {
944 printf("Returned secret %s had same mtime for both secrets: %s\n",
946 nt_time_string(mem_ctx, *r8.out.new_mtime));
950 printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n",
952 nt_time_string(mem_ctx, *r8.out.old_mtime),
953 nt_time_string(mem_ctx, *r8.out.new_mtime));
959 if (!test_Delete(p, mem_ctx, &sec_handle)) {
963 d.in.handle = &sec_handle2;
964 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
965 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
966 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
970 printf("Testing OpenSecret of just-deleted secret\n");
972 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
973 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
974 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
985 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p,
987 struct policy_handle *acct_handle,
991 struct lsa_EnumAccountRights r;
992 struct lsa_RightSet rights;
994 printf("Testing EnumAccountRights\n");
996 r.in.handle = acct_handle;
998 r.out.rights = &rights;
1000 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1001 if (!NT_STATUS_IS_OK(status)) {
1002 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
1010 static BOOL test_QuerySecurity(struct dcerpc_pipe *p,
1011 TALLOC_CTX *mem_ctx,
1012 struct policy_handle *handle,
1013 struct policy_handle *acct_handle)
1016 struct lsa_QuerySecurity r;
1018 printf("Testing QuerySecurity\n");
1020 r.in.handle = acct_handle;
1023 status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1032 static BOOL test_OpenAccount(struct dcerpc_pipe *p,
1033 TALLOC_CTX *mem_ctx,
1034 struct policy_handle *handle,
1035 struct dom_sid *sid)
1038 struct lsa_OpenAccount r;
1039 struct policy_handle acct_handle;
1041 printf("Testing OpenAccount\n");
1043 r.in.handle = handle;
1045 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1046 r.out.acct_handle = &acct_handle;
1048 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1049 if (!NT_STATUS_IS_OK(status)) {
1050 printf("OpenAccount failed - %s\n", nt_errstr(status));
1054 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1058 if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1065 static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
1066 TALLOC_CTX *mem_ctx,
1067 struct policy_handle *handle)
1070 struct lsa_EnumAccounts r;
1071 struct lsa_SidArray sids1, sids2;
1072 uint32_t resume_handle = 0;
1075 printf("\ntesting EnumAccounts\n");
1077 r.in.handle = handle;
1078 r.in.resume_handle = &resume_handle;
1079 r.in.num_entries = 100;
1080 r.out.resume_handle = &resume_handle;
1081 r.out.sids = &sids1;
1085 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1086 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1089 if (!NT_STATUS_IS_OK(status)) {
1090 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1094 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1098 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1102 if (!test_LookupSids3(p, mem_ctx, handle, &sids1)) {
1106 printf("testing all accounts\n");
1107 for (i=0;i<sids1.num_sids;i++) {
1108 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1109 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1114 if (sids1.num_sids < 3) {
1118 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1120 r.in.num_entries = 1;
1121 r.out.sids = &sids2;
1123 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1124 if (!NT_STATUS_IS_OK(status)) {
1125 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1129 if (sids2.num_sids != 1) {
1130 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1137 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1138 TALLOC_CTX *mem_ctx,
1139 struct policy_handle *handle,
1140 struct lsa_String *priv_name)
1142 struct lsa_LookupPrivDisplayName r;
1144 /* produce a reasonable range of language output without screwing up
1146 uint16 language_id = (random() % 4) + 0x409;
1148 printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1150 r.in.handle = handle;
1151 r.in.name = priv_name;
1152 r.in.language_id = &language_id;
1153 r.out.language_id = &language_id;
1156 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1157 if (!NT_STATUS_IS_OK(status)) {
1158 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1161 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
1162 priv_name->string, r.out.disp_name->string,
1163 *r.in.language_id, *r.out.language_id);
1168 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1169 TALLOC_CTX *mem_ctx,
1170 struct policy_handle *handle,
1171 struct lsa_String *priv_name)
1173 struct lsa_EnumAccountsWithUserRight r;
1174 struct lsa_SidArray sids;
1179 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1181 r.in.handle = handle;
1182 r.in.name = priv_name;
1185 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1187 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1188 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1192 if (!NT_STATUS_IS_OK(status)) {
1193 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1201 static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
1202 TALLOC_CTX *mem_ctx,
1203 struct policy_handle *handle)
1206 struct lsa_EnumPrivs r;
1207 struct lsa_PrivArray privs1;
1208 uint32_t resume_handle = 0;
1212 printf("\ntesting EnumPrivs\n");
1214 r.in.handle = handle;
1215 r.in.resume_handle = &resume_handle;
1216 r.in.max_count = 100;
1217 r.out.resume_handle = &resume_handle;
1218 r.out.privs = &privs1;
1221 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1227 for (i = 0; i< privs1.count; i++) {
1228 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
1229 test_LookupPrivValue(p, mem_ctx, handle, &privs1.privs[i].name);
1230 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
1239 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
1240 TALLOC_CTX *mem_ctx,
1241 struct policy_handle *handle)
1243 struct lsa_EnumTrustDom r;
1245 uint32_t resume_handle = 0;
1246 struct lsa_DomainList domains;
1250 printf("\nTesting EnumTrustDom\n");
1252 r.in.handle = handle;
1253 r.in.resume_handle = &resume_handle;
1254 r.in.num_entries = 100;
1255 r.out.domains = &domains;
1256 r.out.resume_handle = &resume_handle;
1258 status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1260 /* NO_MORE_ENTRIES is allowed */
1261 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1265 if (!NT_STATUS_IS_OK(status)) {
1266 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
1270 printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1272 for (i=0; i< domains.count; i++) {
1273 struct lsa_OpenTrustedDomain trust;
1274 struct lsa_OpenTrustedDomainByName trust_by_name;
1275 struct policy_handle trustdom_handle;
1276 struct policy_handle handle2;
1278 int levels [] = {1, 3, 6, 8, 12};
1280 trust.in.handle = handle;
1281 trust.in.sid = domains.domains[i].sid;
1282 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1283 trust.out.trustdom_handle = &trustdom_handle;
1285 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1287 if (!NT_STATUS_IS_OK(status)) {
1288 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1292 c.in.handle = &trustdom_handle;
1293 c.out.handle = &handle2;
1295 for (j=0; j < ARRAY_SIZE(levels); j++) {
1296 struct lsa_QueryTrustedDomainInfo q;
1297 union lsa_TrustedDomainInfo info;
1298 q.in.trustdom_handle = &trustdom_handle;
1299 q.in.level = levels[j];
1301 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1302 if (!NT_STATUS_IS_OK(status)) {
1303 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1304 levels[j], nt_errstr(status));
1309 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1310 if (!NT_STATUS_IS_OK(status)) {
1311 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1315 trust_by_name.in.handle = handle;
1316 trust_by_name.in.name = domains.domains[i].name;
1317 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1318 trust_by_name.out.trustdom_handle = &trustdom_handle;
1320 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1322 if (!NT_STATUS_IS_OK(status)) {
1323 printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1327 for (j=0; j < ARRAY_SIZE(levels); j++) {
1328 struct lsa_QueryTrustedDomainInfo q;
1329 union lsa_TrustedDomainInfo info;
1330 q.in.trustdom_handle = &trustdom_handle;
1331 q.in.level = levels[j];
1333 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1334 if (!NT_STATUS_IS_OK(status)) {
1335 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1336 levels[j], nt_errstr(status));
1341 c.in.handle = &trustdom_handle;
1342 c.out.handle = &handle2;
1344 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1345 if (!NT_STATUS_IS_OK(status)) {
1346 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1350 for (j=0; j < ARRAY_SIZE(levels); j++) {
1351 struct lsa_QueryTrustedDomainInfoBySid q;
1352 union lsa_TrustedDomainInfo info;
1353 q.in.handle = handle;
1354 q.in.dom_sid = domains.domains[i].sid;
1355 q.in.level = levels[j];
1357 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n",
1360 levels[j], nt_errstr(status));
1365 for (j=0; j < ARRAY_SIZE(levels); j++) {
1366 struct lsa_QueryTrustedDomainInfoByName q;
1367 union lsa_TrustedDomainInfo info;
1368 q.in.handle = handle;
1369 q.in.trusted_domain = domains.domains[i].name;
1370 q.in.level = levels[j];
1372 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1373 if (!NT_STATUS_IS_OK(status)) {
1374 printf("QueryTrustedDomainInfoByName level %d failed - %s\n",
1375 levels[j], nt_errstr(status));
1387 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p,
1388 TALLOC_CTX *mem_ctx,
1389 struct policy_handle *handle)
1393 struct lsa_CreateTrustedDomain r;
1394 struct lsa_TrustInformation trustinfo;
1395 struct dom_sid *domsid;
1396 struct policy_handle trustdom_handle;
1397 struct lsa_QueryTrustedDomainInfo q;
1399 printf("Testing CreateTrustedDomain\n");
1401 domsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-21-97398-379795-12345");
1403 trustinfo.sid = domsid;
1404 init_lsa_String(&trustinfo.name, "torturedomain");
1406 r.in.handle = handle;
1407 r.in.info = &trustinfo;
1408 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1409 r.out.trustdom_handle = &trustdom_handle;
1411 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1412 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1413 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1414 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1416 if (!NT_STATUS_IS_OK(status)) {
1417 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1421 q.in.trustdom_handle = &trustdom_handle;
1422 q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
1423 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1424 if (!NT_STATUS_IS_OK(status)) {
1425 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
1431 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
1432 printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
1433 q.out.info->name.netbios_name.string, trustinfo.name.string);
1438 /* now that we have some domains to look over, we can test the enum calls */
1439 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1443 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
1450 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p,
1451 TALLOC_CTX *mem_ctx,
1452 struct policy_handle *handle)
1454 struct lsa_QueryInfoPolicy r;
1458 printf("\nTesting QueryInfoPolicy\n");
1460 for (i=1;i<13;i++) {
1461 r.in.handle = handle;
1464 printf("\ntrying QueryInfoPolicy level %d\n", i);
1466 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
1468 if ((i == 9 || i == 10 || i == 11) &&
1469 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1470 printf("server failed level %u (OK)\n", i);
1474 if (!NT_STATUS_IS_OK(status)) {
1475 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1484 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p,
1485 TALLOC_CTX *mem_ctx,
1486 struct policy_handle *handle)
1488 struct lsa_QueryInfoPolicy2 r;
1492 printf("\nTesting QueryInfoPolicy2\n");
1494 for (i=1;i<13;i++) {
1495 r.in.handle = handle;
1498 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
1500 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
1502 if ((i == 9 || i == 10 || i == 11) &&
1503 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1504 printf("server failed level %u (OK)\n", i);
1508 if (!NT_STATUS_IS_OK(status)) {
1509 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
1518 static BOOL test_GetUserName(struct dcerpc_pipe *p,
1519 TALLOC_CTX *mem_ctx,
1520 struct policy_handle *handle)
1522 struct lsa_GetUserName r;
1525 struct lsa_StringPointer authority_name_p;
1527 printf("\nTesting GetUserName\n");
1529 r.in.system_name = "\\";
1530 r.in.account_name = NULL;
1531 r.in.authority_name = &authority_name_p;
1532 authority_name_p.string = NULL;
1534 status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
1536 if (!NT_STATUS_IS_OK(status)) {
1537 printf("GetUserName failed - %s\n", nt_errstr(status));
1544 BOOL test_lsa_Close(struct dcerpc_pipe *p,
1545 TALLOC_CTX *mem_ctx,
1546 struct policy_handle *handle)
1550 struct policy_handle handle2;
1552 printf("\ntesting Close\n");
1554 r.in.handle = handle;
1555 r.out.handle = &handle2;
1557 status = dcerpc_lsa_Close(p, mem_ctx, &r);
1558 if (!NT_STATUS_IS_OK(status)) {
1559 printf("Close failed - %s\n", nt_errstr(status));
1563 status = dcerpc_lsa_Close(p, mem_ctx, &r);
1564 /* its really a fault - we need a status code for rpc fault */
1565 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
1566 printf("Close failed - %s\n", nt_errstr(status));
1575 BOOL torture_rpc_lsa(void)
1578 struct dcerpc_pipe *p;
1579 TALLOC_CTX *mem_ctx;
1581 struct policy_handle handle;
1583 mem_ctx = talloc_init("torture_rpc_lsa");
1585 status = torture_rpc_connection(&p,
1588 DCERPC_LSARPC_VERSION);
1589 if (!NT_STATUS_IS_OK(status)) {
1593 if (!test_OpenPolicy(p, mem_ctx)) {
1597 if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
1601 if (!test_many_LookupSids(p, mem_ctx, &handle)) {
1605 if (!test_CreateAccount(p, mem_ctx, &handle)) {
1609 if (!test_CreateSecret(p, mem_ctx, &handle)) {
1613 if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
1617 if (!test_EnumAccounts(p, mem_ctx, &handle)) {
1621 if (!test_EnumPrivs(p, mem_ctx, &handle)) {
1625 if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
1629 if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
1633 if (!test_GetUserName(p, mem_ctx, &handle)) {
1638 if (!test_Delete(p, mem_ctx, &handle)) {
1643 if (!test_lsa_Close(p, mem_ctx, &handle)) {
1647 talloc_destroy(mem_ctx);
1649 torture_rpc_close(p);