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_CreateTrustedDomain(struct dcerpc_pipe *p,
636 struct policy_handle *handle)
639 struct lsa_CreateTrustedDomain r;
640 struct lsa_TrustInformation trustinfo;
641 struct dom_sid *domsid;
642 struct policy_handle trustdom_handle;
644 printf("Testing CreateTrustedDomain\n");
646 domsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-21-97398-379795-12345");
648 trustinfo.sid = domsid;
649 init_lsa_String(&trustinfo.name, "torturedomain");
651 r.in.handle = handle;
652 r.in.info = &trustinfo;
653 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
654 r.out.trustdom_handle = &trustdom_handle;
656 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
657 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
658 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
659 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
661 if (!NT_STATUS_IS_OK(status)) {
662 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
666 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
673 static BOOL test_CreateSecret(struct dcerpc_pipe *p,
675 struct policy_handle *handle)
678 struct lsa_CreateSecret r;
679 struct lsa_OpenSecret r2;
680 struct lsa_SetSecret r3;
681 struct lsa_QuerySecret r4;
682 struct lsa_SetSecret r5;
683 struct lsa_QuerySecret r6;
684 struct lsa_SetSecret r7;
685 struct lsa_QuerySecret r8;
686 struct policy_handle sec_handle, sec_handle2, sec_handle3;
688 struct lsa_DATA_BUF buf1;
689 struct lsa_DATA_BUF_PTR bufp1;
690 struct lsa_DATA_BUF_PTR bufp2;
693 DATA_BLOB session_key;
694 NTTIME old_mtime, new_mtime;
695 DATA_BLOB blob1, blob2;
696 const char *secret1 = "abcdef12345699qwerty";
698 const char *secret3 = "ABCDEF12345699QWERTY";
700 const char *secret5 = "NEW-SAMBA4-SECRET";
705 const int GLOBAL = 1;
707 secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
708 secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
710 for (i=0; i< 2; i++) {
711 printf("Testing CreateSecret of %s\n", secname[i]);
713 init_lsa_String(&r.in.name, secname[i]);
715 r.in.handle = handle;
716 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
717 r.out.sec_handle = &sec_handle;
719 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
720 if (!NT_STATUS_IS_OK(status)) {
721 printf("CreateSecret failed - %s\n", nt_errstr(status));
725 r.in.handle = handle;
726 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
727 r.out.sec_handle = &sec_handle3;
729 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
730 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
731 printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
735 r2.in.handle = handle;
736 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
737 r2.in.name = r.in.name;
738 r2.out.sec_handle = &sec_handle2;
740 printf("Testing OpenSecret\n");
742 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
743 if (!NT_STATUS_IS_OK(status)) {
744 printf("OpenSecret failed - %s\n", nt_errstr(status));
748 status = dcerpc_fetch_session_key(p, &session_key);
749 if (!NT_STATUS_IS_OK(status)) {
750 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
754 enc_key = sess_encrypt_string(secret1, &session_key);
756 r3.in.sec_handle = &sec_handle;
757 r3.in.new_val = &buf1;
758 r3.in.old_val = NULL;
759 r3.in.new_val->data = enc_key.data;
760 r3.in.new_val->length = enc_key.length;
761 r3.in.new_val->size = enc_key.length;
763 printf("Testing SetSecret\n");
765 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
766 if (!NT_STATUS_IS_OK(status)) {
767 printf("SetSecret failed - %s\n", nt_errstr(status));
771 r3.in.sec_handle = &sec_handle;
772 r3.in.new_val = &buf1;
773 r3.in.old_val = NULL;
774 r3.in.new_val->data = enc_key.data;
775 r3.in.new_val->length = enc_key.length;
776 r3.in.new_val->size = enc_key.length;
778 /* break the encrypted data */
781 printf("Testing SetSecret with broken key\n");
783 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
784 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
785 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
789 data_blob_free(&enc_key);
791 ZERO_STRUCT(new_mtime);
792 ZERO_STRUCT(old_mtime);
794 /* fetch the secret back again */
795 r4.in.sec_handle = &sec_handle;
796 r4.in.new_val = &bufp1;
797 r4.in.new_mtime = &new_mtime;
798 r4.in.old_val = NULL;
799 r4.in.old_mtime = NULL;
803 printf("Testing QuerySecret\n");
804 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
805 if (!NT_STATUS_IS_OK(status)) {
806 printf("QuerySecret failed - %s\n", nt_errstr(status));
809 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
810 printf("No secret buffer returned\n");
813 blob1.data = r4.out.new_val->buf->data;
814 blob1.length = r4.out.new_val->buf->size;
816 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
818 secret2 = sess_decrypt_string(&blob1, &session_key);
820 if (strcmp(secret1, secret2) != 0) {
821 printf("Returned secret '%s' doesn't match '%s'\n",
828 enc_key = sess_encrypt_string(secret3, &session_key);
830 r5.in.sec_handle = &sec_handle;
831 r5.in.new_val = &buf1;
832 r5.in.old_val = NULL;
833 r5.in.new_val->data = enc_key.data;
834 r5.in.new_val->length = enc_key.length;
835 r5.in.new_val->size = enc_key.length;
837 printf("Testing SetSecret (existing value should move to old)\n");
839 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
840 if (!NT_STATUS_IS_OK(status)) {
841 printf("SetSecret failed - %s\n", nt_errstr(status));
845 data_blob_free(&enc_key);
847 ZERO_STRUCT(new_mtime);
848 ZERO_STRUCT(old_mtime);
850 /* fetch the secret back again */
851 r6.in.sec_handle = &sec_handle;
852 r6.in.new_val = &bufp1;
853 r6.in.new_mtime = &new_mtime;
854 r6.in.old_val = &bufp2;
855 r6.in.old_mtime = &old_mtime;
860 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
861 if (!NT_STATUS_IS_OK(status)) {
862 printf("QuerySecret failed - %s\n", nt_errstr(status));
866 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
867 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
868 printf("Both secret buffers and both times not returned\n");
871 blob1.data = r6.out.new_val->buf->data;
872 blob1.length = r6.out.new_val->buf->size;
874 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
876 secret4 = sess_decrypt_string(&blob1, &session_key);
878 if (strcmp(secret3, secret4) != 0) {
879 printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
883 blob1.data = r6.out.old_val->buf->data;
884 blob1.length = r6.out.old_val->buf->length;
886 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
888 secret2 = sess_decrypt_string(&blob1, &session_key);
890 if (strcmp(secret1, secret2) != 0) {
891 printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
895 if (*r6.out.new_mtime == *r6.out.old_mtime) {
896 printf("Returned secret %s had same mtime for both secrets: %s\n",
898 nt_time_string(mem_ctx, *r6.out.new_mtime));
904 enc_key = sess_encrypt_string(secret5, &session_key);
906 r7.in.sec_handle = &sec_handle;
907 r7.in.old_val = &buf1;
908 r7.in.old_val->data = enc_key.data;
909 r7.in.old_val->length = enc_key.length;
910 r7.in.old_val->size = enc_key.length;
911 r7.in.new_val = NULL;
913 printf("Testing SetSecret of old Secret only\n");
915 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
916 if (!NT_STATUS_IS_OK(status)) {
917 printf("SetSecret failed - %s\n", nt_errstr(status));
921 data_blob_free(&enc_key);
923 /* fetch the secret back again */
924 r8.in.sec_handle = &sec_handle;
925 r8.in.new_val = &bufp1;
926 r8.in.new_mtime = &new_mtime;
927 r8.in.old_val = &bufp2;
928 r8.in.old_mtime = &old_mtime;
933 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
934 if (!NT_STATUS_IS_OK(status)) {
935 printf("QuerySecret failed - %s\n", nt_errstr(status));
938 if (!r8.out.new_val || !r8.out.old_val) {
939 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
941 } else if (r8.out.new_val->buf == NULL) {
943 printf("NEW secret buffer not returned after GLOBAL OLD set\n");
946 } else if (r8.out.old_val->buf == NULL) {
947 printf("OLD secret buffer not returned after OLD set\n");
949 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
950 printf("Both times not returned after OLD set\n");
954 printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
957 blob1.data = r8.out.new_val->buf->data;
958 blob1.length = r8.out.new_val->buf->length;
960 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
962 secret6 = sess_decrypt_string(&blob1, &session_key);
964 if (strcmp(secret3, secret4) != 0) {
965 printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
969 blob1.data = r8.out.old_val->buf->data;
970 blob1.length = r8.out.old_val->buf->size;
972 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
974 secret6 = sess_decrypt_string(&blob1, &session_key);
976 if (strcmp(secret5, secret6) != 0) {
977 printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
981 if (*r8.out.new_mtime == *r8.out.old_mtime) {
983 printf("Returned secret %s had same mtime for both secrets: %s\n",
985 nt_time_string(mem_ctx, *r8.out.new_mtime));
989 printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n",
991 nt_time_string(mem_ctx, *r8.out.old_mtime),
992 nt_time_string(mem_ctx, *r8.out.new_mtime));
998 if (!test_Delete(p, mem_ctx, &sec_handle)) {
1002 d.in.handle = &sec_handle2;
1003 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
1004 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1005 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1009 printf("Testing OpenSecret of just-deleted secret\n");
1011 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1012 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1013 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1024 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p,
1025 TALLOC_CTX *mem_ctx,
1026 struct policy_handle *acct_handle,
1027 struct dom_sid *sid)
1030 struct lsa_EnumAccountRights r;
1031 struct lsa_RightSet rights;
1033 printf("Testing EnumAccountRights\n");
1035 r.in.handle = acct_handle;
1037 r.out.rights = &rights;
1039 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
1049 static BOOL test_QuerySecurity(struct dcerpc_pipe *p,
1050 TALLOC_CTX *mem_ctx,
1051 struct policy_handle *handle,
1052 struct policy_handle *acct_handle)
1055 struct lsa_QuerySecurity r;
1057 printf("Testing QuerySecurity\n");
1059 r.in.handle = acct_handle;
1062 status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1071 static BOOL test_OpenAccount(struct dcerpc_pipe *p,
1072 TALLOC_CTX *mem_ctx,
1073 struct policy_handle *handle,
1074 struct dom_sid *sid)
1077 struct lsa_OpenAccount r;
1078 struct policy_handle acct_handle;
1080 printf("Testing OpenAccount\n");
1082 r.in.handle = handle;
1084 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1085 r.out.acct_handle = &acct_handle;
1087 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1088 if (!NT_STATUS_IS_OK(status)) {
1089 printf("OpenAccount failed - %s\n", nt_errstr(status));
1093 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1097 if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1104 static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
1105 TALLOC_CTX *mem_ctx,
1106 struct policy_handle *handle)
1109 struct lsa_EnumAccounts r;
1110 struct lsa_SidArray sids1, sids2;
1111 uint32_t resume_handle = 0;
1114 printf("\ntesting EnumAccounts\n");
1116 r.in.handle = handle;
1117 r.in.resume_handle = &resume_handle;
1118 r.in.num_entries = 100;
1119 r.out.resume_handle = &resume_handle;
1120 r.out.sids = &sids1;
1124 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1125 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1128 if (!NT_STATUS_IS_OK(status)) {
1129 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1133 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1137 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1141 if (!test_LookupSids3(p, mem_ctx, handle, &sids1)) {
1145 printf("testing all accounts\n");
1146 for (i=0;i<sids1.num_sids;i++) {
1147 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1148 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1153 if (sids1.num_sids < 3) {
1157 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1159 r.in.num_entries = 1;
1160 r.out.sids = &sids2;
1162 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1163 if (!NT_STATUS_IS_OK(status)) {
1164 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1168 if (sids2.num_sids != 1) {
1169 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1176 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1177 TALLOC_CTX *mem_ctx,
1178 struct policy_handle *handle,
1179 struct lsa_String *priv_name)
1181 struct lsa_LookupPrivDisplayName r;
1183 /* produce a reasonable range of language output without screwing up
1185 uint16 language_id = (random() % 4) + 0x409;
1187 printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1189 r.in.handle = handle;
1190 r.in.name = priv_name;
1191 r.in.language_id = &language_id;
1192 r.out.language_id = &language_id;
1195 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1196 if (!NT_STATUS_IS_OK(status)) {
1197 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1200 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
1201 priv_name->string, r.out.disp_name->string,
1202 *r.in.language_id, *r.out.language_id);
1207 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1208 TALLOC_CTX *mem_ctx,
1209 struct policy_handle *handle,
1210 struct lsa_String *priv_name)
1212 struct lsa_EnumAccountsWithUserRight r;
1213 struct lsa_SidArray sids;
1218 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1220 r.in.handle = handle;
1221 r.in.name = priv_name;
1224 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1226 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1227 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1231 if (!NT_STATUS_IS_OK(status)) {
1232 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1240 static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
1241 TALLOC_CTX *mem_ctx,
1242 struct policy_handle *handle)
1245 struct lsa_EnumPrivs r;
1246 struct lsa_PrivArray privs1;
1247 uint32_t resume_handle = 0;
1251 printf("\ntesting EnumPrivs\n");
1253 r.in.handle = handle;
1254 r.in.resume_handle = &resume_handle;
1255 r.in.max_count = 100;
1256 r.out.resume_handle = &resume_handle;
1257 r.out.privs = &privs1;
1260 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1261 if (!NT_STATUS_IS_OK(status)) {
1262 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1266 for (i = 0; i< privs1.count; i++) {
1267 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
1268 test_LookupPrivValue(p, mem_ctx, handle, &privs1.privs[i].name);
1269 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
1278 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
1279 TALLOC_CTX *mem_ctx,
1280 struct policy_handle *handle)
1282 struct lsa_EnumTrustDom r;
1284 uint32_t resume_handle = 0;
1285 struct lsa_DomainList domains;
1289 printf("\nTesting EnumTrustDom\n");
1291 r.in.handle = handle;
1292 r.in.resume_handle = &resume_handle;
1293 r.in.num_entries = 100;
1294 r.out.domains = &domains;
1295 r.out.resume_handle = &resume_handle;
1297 status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1299 /* NO_MORE_ENTRIES is allowed */
1300 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1304 if (!NT_STATUS_IS_OK(status)) {
1305 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
1309 printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1311 for (i=0; i< domains.count; i++) {
1312 struct lsa_OpenTrustedDomain trust;
1313 struct lsa_OpenTrustedDomainByName trust_by_name;
1314 struct policy_handle trustdom_handle;
1315 struct policy_handle handle2;
1317 int levels [] = {1, 3, 6, 8, 12};
1319 trust.in.handle = handle;
1320 trust.in.sid = domains.domains[i].sid;
1321 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1322 trust.out.trustdom_handle = &trustdom_handle;
1324 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1326 if (!NT_STATUS_IS_OK(status)) {
1327 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1331 c.in.handle = &trustdom_handle;
1332 c.out.handle = &handle2;
1334 for (j=0; j < ARRAY_SIZE(levels); j++) {
1335 struct lsa_QueryTrustedDomainInfo q;
1336 union lsa_TrustedDomainInfo info;
1337 q.in.trustdom_handle = &trustdom_handle;
1338 q.in.level = levels[j];
1340 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1341 if (!NT_STATUS_IS_OK(status)) {
1342 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1343 levels[j], nt_errstr(status));
1348 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1349 if (!NT_STATUS_IS_OK(status)) {
1350 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1354 trust_by_name.in.handle = handle;
1355 trust_by_name.in.name = domains.domains[i].name;
1356 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1357 trust_by_name.out.trustdom_handle = &trustdom_handle;
1359 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1361 if (!NT_STATUS_IS_OK(status)) {
1362 printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1366 for (j=0; j < ARRAY_SIZE(levels); j++) {
1367 struct lsa_QueryTrustedDomainInfo q;
1368 union lsa_TrustedDomainInfo info;
1369 q.in.trustdom_handle = &trustdom_handle;
1370 q.in.level = levels[j];
1372 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1373 if (!NT_STATUS_IS_OK(status)) {
1374 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1375 levels[j], nt_errstr(status));
1380 c.in.handle = &trustdom_handle;
1381 c.out.handle = &handle2;
1383 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1384 if (!NT_STATUS_IS_OK(status)) {
1385 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1389 for (j=0; j < ARRAY_SIZE(levels); j++) {
1390 struct lsa_QueryTrustedDomainInfoBySid q;
1391 union lsa_TrustedDomainInfo info;
1392 q.in.handle = handle;
1393 q.in.dom_sid = domains.domains[i].sid;
1394 q.in.level = levels[j];
1396 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1397 if (!NT_STATUS_IS_OK(status)) {
1398 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n",
1399 levels[j], nt_errstr(status));
1404 for (j=0; j < ARRAY_SIZE(levels); j++) {
1405 struct lsa_QueryTrustedDomainInfoByName q;
1406 union lsa_TrustedDomainInfo info;
1407 q.in.handle = handle;
1408 q.in.trusted_domain = domains.domains[i].name;
1409 q.in.level = levels[j];
1411 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 printf("QueryTrustedDomainInfoByName level %d failed - %s\n",
1414 levels[j], nt_errstr(status));
1426 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p,
1427 TALLOC_CTX *mem_ctx,
1428 struct policy_handle *handle)
1430 struct lsa_QueryInfoPolicy r;
1434 printf("\nTesting QueryInfoPolicy\n");
1436 for (i=1;i<13;i++) {
1437 r.in.handle = handle;
1440 printf("\ntrying QueryInfoPolicy level %d\n", i);
1442 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
1444 if ((i == 9 || i == 10 || i == 11) &&
1445 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1446 printf("server failed level %u (OK)\n", i);
1450 if (!NT_STATUS_IS_OK(status)) {
1451 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1460 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p,
1461 TALLOC_CTX *mem_ctx,
1462 struct policy_handle *handle)
1464 struct lsa_QueryInfoPolicy2 r;
1468 printf("\nTesting QueryInfoPolicy2\n");
1470 for (i=1;i<13;i++) {
1471 r.in.handle = handle;
1474 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
1476 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
1478 if ((i == 9 || i == 10 || i == 11) &&
1479 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1480 printf("server failed level %u (OK)\n", i);
1484 if (!NT_STATUS_IS_OK(status)) {
1485 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
1494 static BOOL test_GetUserName(struct dcerpc_pipe *p,
1495 TALLOC_CTX *mem_ctx,
1496 struct policy_handle *handle)
1498 struct lsa_GetUserName r;
1501 struct lsa_StringPointer authority_name_p;
1503 printf("\nTesting GetUserName\n");
1505 r.in.system_name = "\\";
1506 r.in.account_name = NULL;
1507 r.in.authority_name = &authority_name_p;
1508 authority_name_p.string = NULL;
1510 status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
1512 if (!NT_STATUS_IS_OK(status)) {
1513 printf("GetUserName failed - %s\n", nt_errstr(status));
1520 BOOL test_lsa_Close(struct dcerpc_pipe *p,
1521 TALLOC_CTX *mem_ctx,
1522 struct policy_handle *handle)
1526 struct policy_handle handle2;
1528 printf("\ntesting Close\n");
1530 r.in.handle = handle;
1531 r.out.handle = &handle2;
1533 status = dcerpc_lsa_Close(p, mem_ctx, &r);
1534 if (!NT_STATUS_IS_OK(status)) {
1535 printf("Close failed - %s\n", nt_errstr(status));
1539 status = dcerpc_lsa_Close(p, mem_ctx, &r);
1540 /* its really a fault - we need a status code for rpc fault */
1541 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
1542 printf("Close failed - %s\n", nt_errstr(status));
1551 BOOL torture_rpc_lsa(void)
1554 struct dcerpc_pipe *p;
1555 TALLOC_CTX *mem_ctx;
1557 struct policy_handle handle;
1559 mem_ctx = talloc_init("torture_rpc_lsa");
1561 status = torture_rpc_connection(&p,
1564 DCERPC_LSARPC_VERSION);
1565 if (!NT_STATUS_IS_OK(status)) {
1569 if (!test_OpenPolicy(p, mem_ctx)) {
1573 if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
1577 if (!test_many_LookupSids(p, mem_ctx, &handle)) {
1581 if (!test_CreateAccount(p, mem_ctx, &handle)) {
1585 if (!test_CreateSecret(p, mem_ctx, &handle)) {
1589 if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
1593 if (!test_EnumAccounts(p, mem_ctx, &handle)) {
1597 if (!test_EnumPrivs(p, mem_ctx, &handle)) {
1601 if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
1605 if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
1609 if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
1613 if (!test_GetUserName(p, mem_ctx, &handle)) {
1618 if (!test_Delete(p, mem_ctx, &handle)) {
1623 if (!test_lsa_Close(p, mem_ctx, &handle)) {
1627 talloc_destroy(mem_ctx);
1629 torture_rpc_close(p);