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(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(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(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(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(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(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("testing 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));
573 static BOOL test_CreateAccount(struct dcerpc_pipe *p,
575 struct policy_handle *handle)
578 struct lsa_CreateAccount r;
579 struct dom_sid2 *newsid;
580 struct policy_handle acct_handle;
582 newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
584 printf("Testing CreateAccount\n");
586 r.in.handle = handle;
588 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
589 r.out.acct_handle = &acct_handle;
591 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
592 if (!NT_STATUS_IS_OK(status)) {
593 printf("CreateAccount failed - %s\n", nt_errstr(status));
597 if (!test_Delete(p, mem_ctx, &acct_handle)) {
604 static BOOL test_DeleteTrustedDomain(struct dcerpc_pipe *p,
606 struct policy_handle *handle,
607 struct lsa_String name)
610 struct lsa_OpenTrustedDomainByName r;
611 struct policy_handle trustdom_handle;
613 r.in.handle = handle;
615 r.in.access_mask = SEC_STD_DELETE;
616 r.out.trustdom_handle = &trustdom_handle;
618 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
619 if (!NT_STATUS_IS_OK(status)) {
620 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
624 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
632 static BOOL test_CreateSecret(struct dcerpc_pipe *p,
634 struct policy_handle *handle)
637 struct lsa_CreateSecret r;
638 struct lsa_OpenSecret r2;
639 struct lsa_SetSecret r3;
640 struct lsa_QuerySecret r4;
641 struct lsa_SetSecret r5;
642 struct lsa_QuerySecret r6;
643 struct lsa_SetSecret r7;
644 struct lsa_QuerySecret r8;
645 struct policy_handle sec_handle, sec_handle2, sec_handle3;
647 struct lsa_DATA_BUF buf1;
648 struct lsa_DATA_BUF_PTR bufp1;
649 struct lsa_DATA_BUF_PTR bufp2;
652 DATA_BLOB session_key;
653 NTTIME old_mtime, new_mtime;
654 DATA_BLOB blob1, blob2;
655 const char *secret1 = "abcdef12345699qwerty";
657 const char *secret3 = "ABCDEF12345699QWERTY";
659 const char *secret5 = "NEW-SAMBA4-SECRET";
664 const int GLOBAL = 1;
666 secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
667 secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
669 for (i=0; i< 2; i++) {
670 printf("Testing CreateSecret of %s\n", secname[i]);
672 init_lsa_String(&r.in.name, secname[i]);
674 r.in.handle = handle;
675 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
676 r.out.sec_handle = &sec_handle;
678 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
679 if (!NT_STATUS_IS_OK(status)) {
680 printf("CreateSecret failed - %s\n", nt_errstr(status));
684 r.in.handle = handle;
685 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
686 r.out.sec_handle = &sec_handle3;
688 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
689 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
690 printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
694 r2.in.handle = handle;
695 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
696 r2.in.name = r.in.name;
697 r2.out.sec_handle = &sec_handle2;
699 printf("Testing OpenSecret\n");
701 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
702 if (!NT_STATUS_IS_OK(status)) {
703 printf("OpenSecret failed - %s\n", nt_errstr(status));
707 status = dcerpc_fetch_session_key(p, &session_key);
708 if (!NT_STATUS_IS_OK(status)) {
709 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
713 enc_key = sess_encrypt_string(secret1, &session_key);
715 r3.in.sec_handle = &sec_handle;
716 r3.in.new_val = &buf1;
717 r3.in.old_val = NULL;
718 r3.in.new_val->data = enc_key.data;
719 r3.in.new_val->length = enc_key.length;
720 r3.in.new_val->size = enc_key.length;
722 printf("Testing SetSecret\n");
724 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
725 if (!NT_STATUS_IS_OK(status)) {
726 printf("SetSecret failed - %s\n", nt_errstr(status));
730 r3.in.sec_handle = &sec_handle;
731 r3.in.new_val = &buf1;
732 r3.in.old_val = NULL;
733 r3.in.new_val->data = enc_key.data;
734 r3.in.new_val->length = enc_key.length;
735 r3.in.new_val->size = enc_key.length;
737 /* break the encrypted data */
740 printf("Testing SetSecret with broken key\n");
742 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
743 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
744 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
748 data_blob_free(&enc_key);
750 ZERO_STRUCT(new_mtime);
751 ZERO_STRUCT(old_mtime);
753 /* fetch the secret back again */
754 r4.in.sec_handle = &sec_handle;
755 r4.in.new_val = &bufp1;
756 r4.in.new_mtime = &new_mtime;
757 r4.in.old_val = NULL;
758 r4.in.old_mtime = NULL;
762 printf("Testing QuerySecret\n");
763 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
764 if (!NT_STATUS_IS_OK(status)) {
765 printf("QuerySecret failed - %s\n", nt_errstr(status));
768 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
769 printf("No secret buffer returned\n");
772 blob1.data = r4.out.new_val->buf->data;
773 blob1.length = r4.out.new_val->buf->size;
775 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
777 secret2 = sess_decrypt_string(&blob1, &session_key);
779 if (strcmp(secret1, secret2) != 0) {
780 printf("Returned secret '%s' doesn't match '%s'\n",
787 enc_key = sess_encrypt_string(secret3, &session_key);
789 r5.in.sec_handle = &sec_handle;
790 r5.in.new_val = &buf1;
791 r5.in.old_val = NULL;
792 r5.in.new_val->data = enc_key.data;
793 r5.in.new_val->length = enc_key.length;
794 r5.in.new_val->size = enc_key.length;
796 printf("Testing SetSecret (existing value should move to old)\n");
798 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
799 if (!NT_STATUS_IS_OK(status)) {
800 printf("SetSecret failed - %s\n", nt_errstr(status));
804 data_blob_free(&enc_key);
806 ZERO_STRUCT(new_mtime);
807 ZERO_STRUCT(old_mtime);
809 /* fetch the secret back again */
810 r6.in.sec_handle = &sec_handle;
811 r6.in.new_val = &bufp1;
812 r6.in.new_mtime = &new_mtime;
813 r6.in.old_val = &bufp2;
814 r6.in.old_mtime = &old_mtime;
819 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
820 if (!NT_STATUS_IS_OK(status)) {
821 printf("QuerySecret failed - %s\n", nt_errstr(status));
825 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
826 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
827 printf("Both secret buffers and both times not returned\n");
830 blob1.data = r6.out.new_val->buf->data;
831 blob1.length = r6.out.new_val->buf->size;
833 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
835 secret4 = sess_decrypt_string(&blob1, &session_key);
837 if (strcmp(secret3, secret4) != 0) {
838 printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
842 blob1.data = r6.out.old_val->buf->data;
843 blob1.length = r6.out.old_val->buf->length;
845 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
847 secret2 = sess_decrypt_string(&blob1, &session_key);
849 if (strcmp(secret1, secret2) != 0) {
850 printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
854 if (*r6.out.new_mtime == *r6.out.old_mtime) {
855 printf("Returned secret %s had same mtime for both secrets: %s\n",
857 nt_time_string(mem_ctx, *r6.out.new_mtime));
863 enc_key = sess_encrypt_string(secret5, &session_key);
865 r7.in.sec_handle = &sec_handle;
866 r7.in.old_val = &buf1;
867 r7.in.old_val->data = enc_key.data;
868 r7.in.old_val->length = enc_key.length;
869 r7.in.old_val->size = enc_key.length;
870 r7.in.new_val = NULL;
872 printf("Testing SetSecret of old Secret only\n");
874 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
875 if (!NT_STATUS_IS_OK(status)) {
876 printf("SetSecret failed - %s\n", nt_errstr(status));
880 data_blob_free(&enc_key);
882 /* fetch the secret back again */
883 r8.in.sec_handle = &sec_handle;
884 r8.in.new_val = &bufp1;
885 r8.in.new_mtime = &new_mtime;
886 r8.in.old_val = &bufp2;
887 r8.in.old_mtime = &old_mtime;
892 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
893 if (!NT_STATUS_IS_OK(status)) {
894 printf("QuerySecret failed - %s\n", nt_errstr(status));
897 if (!r8.out.new_val || !r8.out.old_val) {
898 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
900 } else if (r8.out.new_val->buf == NULL) {
902 printf("NEW secret buffer not returned after GLOBAL OLD set\n");
905 } else if (r8.out.old_val->buf == NULL) {
906 printf("OLD secret buffer not returned after OLD set\n");
908 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
909 printf("Both times not returned after OLD set\n");
913 printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
916 blob1.data = r8.out.new_val->buf->data;
917 blob1.length = r8.out.new_val->buf->length;
919 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
921 secret6 = sess_decrypt_string(&blob1, &session_key);
923 if (strcmp(secret3, secret4) != 0) {
924 printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
928 blob1.data = r8.out.old_val->buf->data;
929 blob1.length = r8.out.old_val->buf->size;
931 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
933 secret6 = sess_decrypt_string(&blob1, &session_key);
935 if (strcmp(secret5, secret6) != 0) {
936 printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
940 if (*r8.out.new_mtime == *r8.out.old_mtime) {
942 printf("Returned secret %s had same mtime for both secrets: %s\n",
944 nt_time_string(mem_ctx, *r8.out.new_mtime));
948 printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n",
950 nt_time_string(mem_ctx, *r8.out.old_mtime),
951 nt_time_string(mem_ctx, *r8.out.new_mtime));
957 if (!test_Delete(p, mem_ctx, &sec_handle)) {
961 d.in.handle = &sec_handle2;
962 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
963 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
964 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
968 printf("Testing OpenSecret of just-deleted secret\n");
970 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
971 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
972 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
983 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p,
985 struct policy_handle *acct_handle,
989 struct lsa_EnumAccountRights r;
990 struct lsa_RightSet rights;
992 printf("Testing EnumAccountRights\n");
994 r.in.handle = acct_handle;
996 r.out.rights = &rights;
998 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
999 if (!NT_STATUS_IS_OK(status)) {
1000 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
1008 static BOOL test_QuerySecurity(struct dcerpc_pipe *p,
1009 TALLOC_CTX *mem_ctx,
1010 struct policy_handle *handle,
1011 struct policy_handle *acct_handle)
1014 struct lsa_QuerySecurity r;
1016 printf("Testing QuerySecurity\n");
1018 r.in.handle = acct_handle;
1021 status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1030 static BOOL test_OpenAccount(struct dcerpc_pipe *p,
1031 TALLOC_CTX *mem_ctx,
1032 struct policy_handle *handle,
1033 struct dom_sid *sid)
1036 struct lsa_OpenAccount r;
1037 struct policy_handle acct_handle;
1039 printf("Testing OpenAccount\n");
1041 r.in.handle = handle;
1043 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1044 r.out.acct_handle = &acct_handle;
1046 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1047 if (!NT_STATUS_IS_OK(status)) {
1048 printf("OpenAccount failed - %s\n", nt_errstr(status));
1052 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1056 if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1063 static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
1064 TALLOC_CTX *mem_ctx,
1065 struct policy_handle *handle)
1068 struct lsa_EnumAccounts r;
1069 struct lsa_SidArray sids1, sids2;
1070 uint32_t resume_handle = 0;
1073 printf("\ntesting EnumAccounts\n");
1075 r.in.handle = handle;
1076 r.in.resume_handle = &resume_handle;
1077 r.in.num_entries = 100;
1078 r.out.resume_handle = &resume_handle;
1079 r.out.sids = &sids1;
1083 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1084 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1087 if (!NT_STATUS_IS_OK(status)) {
1088 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1092 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1096 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1100 if (!test_LookupSids3(p, mem_ctx, handle, &sids1)) {
1104 printf("testing all accounts\n");
1105 for (i=0;i<sids1.num_sids;i++) {
1106 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1107 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1112 if (sids1.num_sids < 3) {
1116 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1118 r.in.num_entries = 1;
1119 r.out.sids = &sids2;
1121 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1122 if (!NT_STATUS_IS_OK(status)) {
1123 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1127 if (sids2.num_sids != 1) {
1128 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1135 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1136 TALLOC_CTX *mem_ctx,
1137 struct policy_handle *handle,
1138 struct lsa_String *priv_name)
1140 struct lsa_LookupPrivDisplayName r;
1142 /* produce a reasonable range of language output without screwing up
1144 uint16_t language_id = (random() % 4) + 0x409;
1146 printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1148 r.in.handle = handle;
1149 r.in.name = priv_name;
1150 r.in.language_id = &language_id;
1151 r.out.language_id = &language_id;
1154 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1155 if (!NT_STATUS_IS_OK(status)) {
1156 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1159 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
1160 priv_name->string, r.out.disp_name->string,
1161 *r.in.language_id, *r.out.language_id);
1166 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1167 TALLOC_CTX *mem_ctx,
1168 struct policy_handle *handle,
1169 struct lsa_String *priv_name)
1171 struct lsa_EnumAccountsWithUserRight r;
1172 struct lsa_SidArray sids;
1177 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1179 r.in.handle = handle;
1180 r.in.name = priv_name;
1183 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1185 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1186 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1190 if (!NT_STATUS_IS_OK(status)) {
1191 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1199 static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
1200 TALLOC_CTX *mem_ctx,
1201 struct policy_handle *handle)
1204 struct lsa_EnumPrivs r;
1205 struct lsa_PrivArray privs1;
1206 uint32_t resume_handle = 0;
1210 printf("\ntesting EnumPrivs\n");
1212 r.in.handle = handle;
1213 r.in.resume_handle = &resume_handle;
1214 r.in.max_count = 100;
1215 r.out.resume_handle = &resume_handle;
1216 r.out.privs = &privs1;
1219 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1220 if (!NT_STATUS_IS_OK(status)) {
1221 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1225 for (i = 0; i< privs1.count; i++) {
1226 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
1227 test_LookupPrivValue(p, mem_ctx, handle, &privs1.privs[i].name);
1228 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
1236 static BOOL test_query_each_TrustDom(struct dcerpc_pipe *p,
1237 TALLOC_CTX *mem_ctx,
1238 struct policy_handle *handle,
1239 struct lsa_DomainList *domains)
1245 printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1246 for (i=0; i< domains->count; i++) {
1247 struct lsa_OpenTrustedDomain trust;
1248 struct lsa_OpenTrustedDomainByName trust_by_name;
1249 struct policy_handle trustdom_handle;
1250 struct policy_handle handle2;
1252 int levels [] = {1, 3, 6, 8, 12};
1254 if (domains->domains[i].sid) {
1255 trust.in.handle = handle;
1256 trust.in.sid = domains->domains[i].sid;
1257 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1258 trust.out.trustdom_handle = &trustdom_handle;
1260 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1262 if (!NT_STATUS_IS_OK(status)) {
1263 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1267 c.in.handle = &trustdom_handle;
1268 c.out.handle = &handle2;
1270 for (j=0; j < ARRAY_SIZE(levels); j++) {
1271 struct lsa_QueryTrustedDomainInfo q;
1272 union lsa_TrustedDomainInfo info;
1273 q.in.trustdom_handle = &trustdom_handle;
1274 q.in.level = levels[j];
1276 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1277 if (!NT_STATUS_IS_OK(status)) {
1278 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1279 levels[j], nt_errstr(status));
1284 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1285 if (!NT_STATUS_IS_OK(status)) {
1286 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1291 trust_by_name.in.handle = handle;
1292 trust_by_name.in.name = domains->domains[i].name;
1293 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1294 trust_by_name.out.trustdom_handle = &trustdom_handle;
1296 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1298 if (!NT_STATUS_IS_OK(status)) {
1299 printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1303 for (j=0; j < ARRAY_SIZE(levels); j++) {
1304 struct lsa_QueryTrustedDomainInfo q;
1305 union lsa_TrustedDomainInfo info;
1306 q.in.trustdom_handle = &trustdom_handle;
1307 q.in.level = levels[j];
1309 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1310 if (!NT_STATUS_IS_OK(status)) {
1311 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1312 levels[j], nt_errstr(status));
1317 c.in.handle = &trustdom_handle;
1318 c.out.handle = &handle2;
1320 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1321 if (!NT_STATUS_IS_OK(status)) {
1322 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1326 for (j=0; j < ARRAY_SIZE(levels); j++) {
1327 struct lsa_QueryTrustedDomainInfoBySid q;
1328 union lsa_TrustedDomainInfo info;
1330 if (!domains->domains[i].sid) {
1334 q.in.handle = handle;
1335 q.in.dom_sid = domains->domains[i].sid;
1336 q.in.level = levels[j];
1338 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1339 if (!NT_STATUS_IS_OK(status)) {
1340 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n",
1341 levels[j], nt_errstr(status));
1346 for (j=0; j < ARRAY_SIZE(levels); j++) {
1347 struct lsa_QueryTrustedDomainInfoByName q;
1348 union lsa_TrustedDomainInfo info;
1349 q.in.handle = handle;
1350 q.in.trusted_domain = domains->domains[i].name;
1351 q.in.level = levels[j];
1353 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1354 if (!NT_STATUS_IS_OK(status)) {
1355 printf("QueryTrustedDomainInfoByName level %d failed - %s\n",
1356 levels[j], nt_errstr(status));
1364 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
1365 TALLOC_CTX *mem_ctx,
1366 struct policy_handle *handle)
1368 struct lsa_EnumTrustDom r;
1369 NTSTATUS enum_status;
1370 uint32_t resume_handle = 0;
1371 struct lsa_DomainList domains;
1374 printf("\nTesting EnumTrustDom\n");
1377 r.in.handle = handle;
1378 r.in.resume_handle = &resume_handle;
1379 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
1380 r.out.domains = &domains;
1381 r.out.resume_handle = &resume_handle;
1383 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1385 /* NO_MORE_ENTRIES is allowed */
1386 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1388 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1389 /* Windows 2003 gets this off by one on the first run */
1390 if (r.out.domains->count < 3 || r.out.domains->count > 4) {
1391 printf("EnumTrustDom didn't fill the buffer we "
1392 "asked it to (got %d, expected %d / %d == %d entries)\n",
1393 r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3,
1394 LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
1397 } else if (!NT_STATUS_IS_OK(enum_status)) {
1398 printf("EnumTrustDom failed - %s\n", nt_errstr(enum_status));
1402 ret &= test_query_each_TrustDom(p, mem_ctx, handle, &domains);
1404 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1409 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p,
1410 TALLOC_CTX *mem_ctx,
1411 struct policy_handle *handle)
1415 struct lsa_CreateTrustedDomain r;
1416 struct lsa_TrustInformation trustinfo;
1417 struct dom_sid *domsid[12];
1418 struct policy_handle trustdom_handle[12];
1419 struct lsa_QueryTrustedDomainInfo q;
1422 printf("Testing CreateTrustedDomain for 12 domains\n");
1424 for (i=0; i< 12; i++) {
1425 char *trust_name = talloc_asprintf(mem_ctx, "torturedom%02d", i);
1426 char *trust_sid = talloc_asprintf(mem_ctx, "S-1-5-21-97398-379795-100%02d", i);
1428 domsid[i] = dom_sid_parse_talloc(mem_ctx, trust_sid);
1430 trustinfo.sid = domsid[i];
1431 init_lsa_String(&trustinfo.name, trust_name);
1433 r.in.handle = handle;
1434 r.in.info = &trustinfo;
1435 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1436 r.out.trustdom_handle = &trustdom_handle[i];
1438 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1439 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1440 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1441 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1443 if (!NT_STATUS_IS_OK(status)) {
1444 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1448 q.in.trustdom_handle = &trustdom_handle[i];
1449 q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
1450 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1451 if (!NT_STATUS_IS_OK(status)) {
1452 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
1454 } else if (!q.out.info) {
1457 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
1458 printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
1459 q.out.info->name.netbios_name.string, trustinfo.name.string);
1466 /* now that we have some domains to look over, we can test the enum calls */
1467 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1471 for (i=0; i<12; i++) {
1472 if (!test_Delete(p, mem_ctx, &trustdom_handle[i])) {
1480 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p,
1481 TALLOC_CTX *mem_ctx,
1482 struct policy_handle *handle)
1484 struct lsa_QueryInfoPolicy r;
1488 printf("\nTesting QueryInfoPolicy\n");
1490 for (i=1;i<13;i++) {
1491 r.in.handle = handle;
1494 printf("\ntrying QueryInfoPolicy level %d\n", i);
1496 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
1498 if ((i == 9 || i == 10 || i == 11) &&
1499 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1500 printf("server failed level %u (OK)\n", i);
1504 if (!NT_STATUS_IS_OK(status)) {
1505 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1514 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p,
1515 TALLOC_CTX *mem_ctx,
1516 struct policy_handle *handle)
1518 struct lsa_QueryInfoPolicy2 r;
1522 printf("\nTesting QueryInfoPolicy2\n");
1524 for (i=1;i<13;i++) {
1525 r.in.handle = handle;
1528 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
1530 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
1532 if ((i == 9 || i == 10 || i == 11) &&
1533 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1534 printf("server failed level %u (OK)\n", i);
1538 if (!NT_STATUS_IS_OK(status)) {
1539 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
1548 static BOOL test_GetUserName(struct dcerpc_pipe *p,
1549 TALLOC_CTX *mem_ctx,
1550 struct policy_handle *handle)
1552 struct lsa_GetUserName r;
1555 struct lsa_StringPointer authority_name_p;
1557 printf("\nTesting GetUserName\n");
1559 r.in.system_name = "\\";
1560 r.in.account_name = NULL;
1561 r.in.authority_name = &authority_name_p;
1562 authority_name_p.string = NULL;
1564 status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
1566 if (!NT_STATUS_IS_OK(status)) {
1567 printf("GetUserName failed - %s\n", nt_errstr(status));
1574 BOOL test_lsa_Close(struct dcerpc_pipe *p,
1575 TALLOC_CTX *mem_ctx,
1576 struct policy_handle *handle)
1580 struct policy_handle handle2;
1582 printf("\ntesting Close\n");
1584 r.in.handle = handle;
1585 r.out.handle = &handle2;
1587 status = dcerpc_lsa_Close(p, mem_ctx, &r);
1588 if (!NT_STATUS_IS_OK(status)) {
1589 printf("Close failed - %s\n", nt_errstr(status));
1593 status = dcerpc_lsa_Close(p, mem_ctx, &r);
1594 /* its really a fault - we need a status code for rpc fault */
1595 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
1596 printf("Close failed - %s\n", nt_errstr(status));
1605 BOOL torture_rpc_lsa(void)
1608 struct dcerpc_pipe *p;
1609 TALLOC_CTX *mem_ctx;
1611 struct policy_handle handle;
1613 mem_ctx = talloc_init("torture_rpc_lsa");
1615 status = torture_rpc_connection(mem_ctx,
1619 DCERPC_LSARPC_VERSION);
1620 if (!NT_STATUS_IS_OK(status)) {
1621 talloc_free(mem_ctx);
1625 if (!test_OpenPolicy(p, mem_ctx)) {
1629 if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
1633 if (!test_many_LookupSids(p, mem_ctx, &handle)) {
1637 if (!test_CreateAccount(p, mem_ctx, &handle)) {
1641 if (!test_CreateSecret(p, mem_ctx, &handle)) {
1645 if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
1649 if (!test_EnumAccounts(p, mem_ctx, &handle)) {
1653 if (!test_EnumPrivs(p, mem_ctx, &handle)) {
1657 if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
1661 if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
1665 if (!test_GetUserName(p, mem_ctx, &handle)) {
1670 if (!test_Delete(p, mem_ctx, &handle)) {
1675 if (!test_lsa_Close(p, mem_ctx, &handle)) {
1679 talloc_free(mem_ctx);