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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_lsa_c.h"
25 #include "librpc/gen_ndr/netlogon.h"
26 #include "lib/events/events.h"
27 #include "libcli/security/security.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "torture/rpc/rpc.h"
30 #include "param/param.h"
32 static void init_lsa_String(struct lsa_String *name, const char *s)
37 static bool test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
39 struct lsa_ObjectAttribute attr;
40 struct policy_handle handle;
41 struct lsa_QosInfo qos;
42 struct lsa_OpenPolicy r;
44 uint16_t system_name = '\\';
46 printf("\ntesting OpenPolicy\n");
49 qos.impersonation_level = 2;
51 qos.effective_only = 0;
55 attr.object_name = NULL;
60 r.in.system_name = &system_name;
62 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
63 r.out.handle = &handle;
65 status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
66 if (!NT_STATUS_IS_OK(status)) {
67 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
68 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
69 printf("not considering %s to be an error\n", nt_errstr(status));
72 printf("OpenPolicy failed - %s\n", nt_errstr(status));
80 bool test_lsa_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
81 struct policy_handle **handle)
83 struct lsa_ObjectAttribute attr;
84 struct lsa_QosInfo qos;
85 struct lsa_OpenPolicy2 r;
88 printf("\ntesting OpenPolicy2\n");
90 *handle = talloc(mem_ctx, struct policy_handle);
96 qos.impersonation_level = 2;
98 qos.effective_only = 0;
101 attr.root_dir = NULL;
102 attr.object_name = NULL;
104 attr.sec_desc = NULL;
107 r.in.system_name = "\\";
109 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
110 r.out.handle = *handle;
112 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
113 if (!NT_STATUS_IS_OK(status)) {
114 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
115 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
116 printf("not considering %s to be an error\n", nt_errstr(status));
117 talloc_free(*handle);
121 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
128 static bool test_LookupNames(struct dcerpc_pipe *p,
130 struct policy_handle *handle,
131 struct lsa_TransNameArray *tnames)
133 struct lsa_LookupNames r;
134 struct lsa_TransSidArray sids;
135 struct lsa_String *names;
140 printf("\nTesting LookupNames with %d names\n", tnames->count);
145 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
146 for (i=0;i<tnames->count;i++) {
147 init_lsa_String(&names[i], tnames->names[i].name.string);
150 r.in.handle = handle;
151 r.in.num_names = tnames->count;
156 r.out.count = &count;
159 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
160 if (!NT_STATUS_IS_OK(status)) {
161 printf("LookupNames failed - %s\n", nt_errstr(status));
170 static bool test_LookupNames_bogus(struct dcerpc_pipe *p,
172 struct policy_handle *handle)
174 struct lsa_LookupNames r;
175 struct lsa_TransSidArray sids;
176 struct lsa_String *names;
181 struct lsa_TranslatedName name;
182 struct lsa_TransNameArray tnames;
184 tnames.names = &name;
186 name.name.string = "NT AUTHORITY\\BOGUS";
188 printf("\nTesting LookupNames with bogus names\n");
193 names = talloc_array(mem_ctx, struct lsa_String, tnames.count);
194 for (i=0;i<tnames.count;i++) {
195 init_lsa_String(&names[i], tnames.names[i].name.string);
198 r.in.handle = handle;
199 r.in.num_names = tnames.count;
204 r.out.count = &count;
207 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
208 if (!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
209 printf("LookupNames failed - %s\n", nt_errstr(status));
218 static bool test_LookupNames_wellknown(struct dcerpc_pipe *p,
220 struct policy_handle *handle)
222 struct lsa_TranslatedName name;
223 struct lsa_TransNameArray tnames;
226 printf("Testing LookupNames with well known names\n");
228 tnames.names = &name;
230 name.name.string = "NT AUTHORITY\\SYSTEM";
231 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
233 name.name.string = "NT AUTHORITY\\ANONYMOUS LOGON";
234 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
236 name.name.string = "NT AUTHORITY\\Authenticated Users";
237 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
239 name.name.string = "NT AUTHORITY";
240 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
242 name.name.string = "NT AUTHORITY\\";
243 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
245 name.name.string = "BUILTIN\\";
246 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
248 name.name.string = "BUILTIN\\Administrators";
249 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
251 name.name.string = "SYSTEM";
252 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
254 name.name.string = "Everyone";
255 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
259 static bool test_LookupNames2(struct dcerpc_pipe *p,
261 struct policy_handle *handle,
262 struct lsa_TransNameArray2 *tnames)
264 struct lsa_LookupNames2 r;
265 struct lsa_TransSidArray2 sids;
266 struct lsa_String *names;
271 printf("\nTesting LookupNames2 with %d names\n", tnames->count);
276 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
277 for (i=0;i<tnames->count;i++) {
278 init_lsa_String(&names[i], tnames->names[i].name.string);
281 r.in.handle = handle;
282 r.in.num_names = tnames->count;
289 r.out.count = &count;
292 status = dcerpc_lsa_LookupNames2(p, mem_ctx, &r);
293 if (!NT_STATUS_IS_OK(status)) {
294 printf("LookupNames2 failed - %s\n", nt_errstr(status));
304 static bool test_LookupNames3(struct dcerpc_pipe *p,
306 struct policy_handle *handle,
307 struct lsa_TransNameArray2 *tnames)
309 struct lsa_LookupNames3 r;
310 struct lsa_TransSidArray3 sids;
311 struct lsa_String *names;
316 printf("\nTesting LookupNames3 with %d names\n", tnames->count);
321 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
322 for (i=0;i<tnames->count;i++) {
323 init_lsa_String(&names[i], tnames->names[i].name.string);
326 r.in.handle = handle;
327 r.in.num_names = tnames->count;
334 r.out.count = &count;
337 status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
338 if (!NT_STATUS_IS_OK(status)) {
339 printf("LookupNames3 failed - %s\n", nt_errstr(status));
348 static bool test_LookupNames4(struct dcerpc_pipe *p,
350 struct lsa_TransNameArray2 *tnames)
352 struct lsa_LookupNames4 r;
353 struct lsa_TransSidArray3 sids;
354 struct lsa_String *names;
359 printf("\nTesting LookupNames4 with %d names\n", tnames->count);
364 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
365 for (i=0;i<tnames->count;i++) {
366 init_lsa_String(&names[i], tnames->names[i].name.string);
369 r.in.num_names = tnames->count;
376 r.out.count = &count;
379 status = dcerpc_lsa_LookupNames4(p, mem_ctx, &r);
380 if (!NT_STATUS_IS_OK(status)) {
381 printf("LookupNames4 failed - %s\n", nt_errstr(status));
391 static bool test_LookupSids(struct dcerpc_pipe *p,
393 struct policy_handle *handle,
394 struct lsa_SidArray *sids)
396 struct lsa_LookupSids r;
397 struct lsa_TransNameArray names;
398 uint32_t count = sids->num_sids;
401 printf("\nTesting LookupSids\n");
406 r.in.handle = handle;
411 r.out.count = &count;
412 r.out.names = &names;
414 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
415 if (!NT_STATUS_IS_OK(status)) {
416 printf("LookupSids failed - %s\n", nt_errstr(status));
422 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
430 static bool test_LookupSids2(struct dcerpc_pipe *p,
432 struct policy_handle *handle,
433 struct lsa_SidArray *sids)
435 struct lsa_LookupSids2 r;
436 struct lsa_TransNameArray2 names;
437 uint32_t count = sids->num_sids;
440 printf("\nTesting LookupSids2\n");
445 r.in.handle = handle;
452 r.out.count = &count;
453 r.out.names = &names;
455 status = dcerpc_lsa_LookupSids2(p, mem_ctx, &r);
456 if (!NT_STATUS_IS_OK(status)) {
457 printf("LookupSids2 failed - %s\n", nt_errstr(status));
463 if (!test_LookupNames2(p, mem_ctx, handle, &names)) {
467 if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
474 static bool test_LookupSids3(struct dcerpc_pipe *p,
476 struct lsa_SidArray *sids)
478 struct lsa_LookupSids3 r;
479 struct lsa_TransNameArray2 names;
480 uint32_t count = sids->num_sids;
483 printf("\nTesting LookupSids3\n");
494 r.out.count = &count;
495 r.out.names = &names;
497 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
498 if (!NT_STATUS_IS_OK(status)) {
499 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
500 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
501 printf("not considering %s to be an error\n", nt_errstr(status));
504 printf("LookupSids3 failed - %s - not considered an error\n",
511 if (!test_LookupNames4(p, mem_ctx, &names)) {
518 bool test_many_LookupSids(struct dcerpc_pipe *p,
520 struct policy_handle *handle)
524 struct lsa_SidArray sids;
527 printf("\nTesting LookupSids with lots of SIDs\n");
531 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
533 for (i=0; i<sids.num_sids; i++) {
534 const char *sidstr = "S-1-5-32-545";
535 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
538 count = sids.num_sids;
541 struct lsa_LookupSids r;
542 struct lsa_TransNameArray names;
546 r.in.handle = handle;
550 r.in.count = &names.count;
551 r.out.count = &count;
552 r.out.names = &names;
554 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
555 if (!NT_STATUS_IS_OK(status)) {
556 printf("LookupSids failed - %s\n", nt_errstr(status));
562 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
566 struct lsa_LookupSids3 r;
567 struct lsa_TransNameArray2 names;
572 printf("\nTesting LookupSids3\n");
580 r.out.count = &count;
581 r.out.names = &names;
583 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
584 if (!NT_STATUS_IS_OK(status)) {
585 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
586 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
587 printf("not considering %s to be an error\n", nt_errstr(status));
590 printf("LookupSids3 failed - %s\n",
594 if (!test_LookupNames4(p, mem_ctx, &names)) {
606 static void lookupsids_cb(struct rpc_request *req)
608 int *replies = (int *)req->async.private_data;
611 status = dcerpc_ndr_request_recv(req);
612 if (!NT_STATUS_IS_OK(status)) {
613 printf("lookupsids returned %s\n", nt_errstr(status));
622 static bool test_LookupSids_async(struct dcerpc_pipe *p,
624 struct policy_handle *handle)
626 struct lsa_SidArray sids;
627 struct lsa_SidPtr sidptr;
629 struct lsa_TransNameArray *names;
630 struct lsa_LookupSids *r;
631 struct rpc_request **req;
634 const int num_async_requests = 50;
636 count = talloc_array(mem_ctx, uint32_t, num_async_requests);
637 names = talloc_array(mem_ctx, struct lsa_TransNameArray, num_async_requests);
638 r = talloc_array(mem_ctx, struct lsa_LookupSids, num_async_requests);
640 printf("\nTesting %d async lookupsids request\n", num_async_requests);
642 req = talloc_array(mem_ctx, struct rpc_request *, num_async_requests);
646 sidptr.sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-545");
650 for (i=0; i<num_async_requests; i++) {
653 names[i].names = NULL;
655 r[i].in.handle = handle;
656 r[i].in.sids = &sids;
657 r[i].in.names = &names[i];
659 r[i].in.count = &names[i].count;
660 r[i].out.count = &count[i];
661 r[i].out.names = &names[i];
663 req[i] = dcerpc_lsa_LookupSids_send(p, req, &r[i]);
664 if (req[i] == NULL) {
669 req[i]->async.callback = lookupsids_cb;
670 req[i]->async.private_data = &replies;
673 while (replies >= 0 && replies < num_async_requests) {
674 event_loop_once(p->conn->event_ctx);
686 static bool test_LookupPrivValue(struct dcerpc_pipe *p,
688 struct policy_handle *handle,
689 struct lsa_String *name)
692 struct lsa_LookupPrivValue r;
693 struct lsa_LUID luid;
695 r.in.handle = handle;
699 status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
700 if (!NT_STATUS_IS_OK(status)) {
701 printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
708 static bool test_LookupPrivName(struct dcerpc_pipe *p,
710 struct policy_handle *handle,
711 struct lsa_LUID *luid)
714 struct lsa_LookupPrivName r;
716 r.in.handle = handle;
719 status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
720 if (!NT_STATUS_IS_OK(status)) {
721 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
728 static bool test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p,
730 struct policy_handle *acct_handle,
731 struct lsa_LUID *luid)
734 struct lsa_RemovePrivilegesFromAccount r;
735 struct lsa_PrivilegeSet privs;
738 printf("Testing RemovePrivilegesFromAccount\n");
740 r.in.handle = acct_handle;
746 privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
747 privs.set[0].luid = *luid;
748 privs.set[0].attribute = 0;
750 status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
751 if (!NT_STATUS_IS_OK(status)) {
752 printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
759 static bool test_AddPrivilegesToAccount(struct dcerpc_pipe *p,
761 struct policy_handle *acct_handle,
762 struct lsa_LUID *luid)
765 struct lsa_AddPrivilegesToAccount r;
766 struct lsa_PrivilegeSet privs;
769 printf("Testing AddPrivilegesToAccount\n");
771 r.in.handle = acct_handle;
776 privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
777 privs.set[0].luid = *luid;
778 privs.set[0].attribute = 0;
780 status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
781 if (!NT_STATUS_IS_OK(status)) {
782 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
789 static bool test_EnumPrivsAccount(struct dcerpc_pipe *p,
791 struct policy_handle *handle,
792 struct policy_handle *acct_handle)
795 struct lsa_EnumPrivsAccount r;
798 printf("Testing EnumPrivsAccount\n");
800 r.in.handle = acct_handle;
802 status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
803 if (!NT_STATUS_IS_OK(status)) {
804 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
808 if (r.out.privs && r.out.privs->count > 0) {
810 for (i=0;i<r.out.privs->count;i++) {
811 test_LookupPrivName(p, mem_ctx, handle,
812 &r.out.privs->set[i].luid);
815 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle,
816 &r.out.privs->set[0].luid);
817 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle,
818 &r.out.privs->set[0].luid);
824 static bool test_Delete(struct dcerpc_pipe *p,
826 struct policy_handle *handle)
831 printf("testing Delete\n");
833 r.in.handle = handle;
834 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
835 if (!NT_STATUS_IS_OK(status)) {
836 printf("Delete failed - %s\n", nt_errstr(status));
844 static bool test_CreateAccount(struct dcerpc_pipe *p,
846 struct policy_handle *handle)
849 struct lsa_CreateAccount r;
850 struct dom_sid2 *newsid;
851 struct policy_handle acct_handle;
853 newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
855 printf("Testing CreateAccount\n");
857 r.in.handle = handle;
859 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
860 r.out.acct_handle = &acct_handle;
862 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
863 if (!NT_STATUS_IS_OK(status)) {
864 printf("CreateAccount failed - %s\n", nt_errstr(status));
868 if (!test_Delete(p, mem_ctx, &acct_handle)) {
875 static bool test_DeleteTrustedDomain(struct dcerpc_pipe *p,
877 struct policy_handle *handle,
878 struct lsa_StringLarge name)
881 struct lsa_OpenTrustedDomainByName r;
882 struct policy_handle trustdom_handle;
884 r.in.handle = handle;
885 r.in.name.string = name.string;
886 r.in.access_mask = SEC_STD_DELETE;
887 r.out.trustdom_handle = &trustdom_handle;
889 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
890 if (!NT_STATUS_IS_OK(status)) {
891 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
895 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
902 static bool test_DeleteTrustedDomainBySid(struct dcerpc_pipe *p,
904 struct policy_handle *handle,
908 struct lsa_DeleteTrustedDomain r;
910 r.in.handle = handle;
913 status = dcerpc_lsa_DeleteTrustedDomain(p, mem_ctx, &r);
914 if (!NT_STATUS_IS_OK(status)) {
915 printf("lsa_DeleteTrustedDomain failed - %s\n", nt_errstr(status));
923 static bool test_CreateSecret(struct dcerpc_pipe *p,
925 struct policy_handle *handle)
928 struct lsa_CreateSecret r;
929 struct lsa_OpenSecret r2;
930 struct lsa_SetSecret r3;
931 struct lsa_QuerySecret r4;
932 struct lsa_SetSecret r5;
933 struct lsa_QuerySecret r6;
934 struct lsa_SetSecret r7;
935 struct lsa_QuerySecret r8;
936 struct policy_handle sec_handle, sec_handle2, sec_handle3;
938 struct lsa_DATA_BUF buf1;
939 struct lsa_DATA_BUF_PTR bufp1;
940 struct lsa_DATA_BUF_PTR bufp2;
943 DATA_BLOB session_key;
944 NTTIME old_mtime, new_mtime;
945 DATA_BLOB blob1, blob2;
946 const char *secret1 = "abcdef12345699qwerty";
948 const char *secret3 = "ABCDEF12345699QWERTY";
950 const char *secret5 = "NEW-SAMBA4-SECRET";
955 const int GLOBAL = 1;
957 secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
958 secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
960 for (i=0; i< 2; i++) {
961 printf("Testing CreateSecret of %s\n", secname[i]);
963 init_lsa_String(&r.in.name, secname[i]);
965 r.in.handle = handle;
966 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
967 r.out.sec_handle = &sec_handle;
969 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
970 if (!NT_STATUS_IS_OK(status)) {
971 printf("CreateSecret failed - %s\n", nt_errstr(status));
975 r.in.handle = handle;
976 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
977 r.out.sec_handle = &sec_handle3;
979 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
980 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
981 printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
985 r2.in.handle = handle;
986 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
987 r2.in.name = r.in.name;
988 r2.out.sec_handle = &sec_handle2;
990 printf("Testing OpenSecret\n");
992 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
993 if (!NT_STATUS_IS_OK(status)) {
994 printf("OpenSecret failed - %s\n", nt_errstr(status));
998 status = dcerpc_fetch_session_key(p, &session_key);
999 if (!NT_STATUS_IS_OK(status)) {
1000 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
1004 enc_key = sess_encrypt_string(secret1, &session_key);
1006 r3.in.sec_handle = &sec_handle;
1007 r3.in.new_val = &buf1;
1008 r3.in.old_val = NULL;
1009 r3.in.new_val->data = enc_key.data;
1010 r3.in.new_val->length = enc_key.length;
1011 r3.in.new_val->size = enc_key.length;
1013 printf("Testing SetSecret\n");
1015 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1016 if (!NT_STATUS_IS_OK(status)) {
1017 printf("SetSecret failed - %s\n", nt_errstr(status));
1021 r3.in.sec_handle = &sec_handle;
1022 r3.in.new_val = &buf1;
1023 r3.in.old_val = NULL;
1024 r3.in.new_val->data = enc_key.data;
1025 r3.in.new_val->length = enc_key.length;
1026 r3.in.new_val->size = enc_key.length;
1028 /* break the encrypted data */
1031 printf("Testing SetSecret with broken key\n");
1033 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1034 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
1035 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
1039 data_blob_free(&enc_key);
1041 ZERO_STRUCT(new_mtime);
1042 ZERO_STRUCT(old_mtime);
1044 /* fetch the secret back again */
1045 r4.in.sec_handle = &sec_handle;
1046 r4.in.new_val = &bufp1;
1047 r4.in.new_mtime = &new_mtime;
1048 r4.in.old_val = NULL;
1049 r4.in.old_mtime = NULL;
1053 printf("Testing QuerySecret\n");
1054 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
1055 if (!NT_STATUS_IS_OK(status)) {
1056 printf("QuerySecret failed - %s\n", nt_errstr(status));
1059 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
1060 printf("No secret buffer returned\n");
1063 blob1.data = r4.out.new_val->buf->data;
1064 blob1.length = r4.out.new_val->buf->size;
1066 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1068 secret2 = sess_decrypt_string(mem_ctx,
1069 &blob1, &session_key);
1071 if (strcmp(secret1, secret2) != 0) {
1072 printf("Returned secret '%s' doesn't match '%s'\n",
1079 enc_key = sess_encrypt_string(secret3, &session_key);
1081 r5.in.sec_handle = &sec_handle;
1082 r5.in.new_val = &buf1;
1083 r5.in.old_val = NULL;
1084 r5.in.new_val->data = enc_key.data;
1085 r5.in.new_val->length = enc_key.length;
1086 r5.in.new_val->size = enc_key.length;
1088 printf("Testing SetSecret (existing value should move to old)\n");
1090 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
1091 if (!NT_STATUS_IS_OK(status)) {
1092 printf("SetSecret failed - %s\n", nt_errstr(status));
1096 data_blob_free(&enc_key);
1098 ZERO_STRUCT(new_mtime);
1099 ZERO_STRUCT(old_mtime);
1101 /* fetch the secret back again */
1102 r6.in.sec_handle = &sec_handle;
1103 r6.in.new_val = &bufp1;
1104 r6.in.new_mtime = &new_mtime;
1105 r6.in.old_val = &bufp2;
1106 r6.in.old_mtime = &old_mtime;
1111 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
1112 if (!NT_STATUS_IS_OK(status)) {
1113 printf("QuerySecret failed - %s\n", nt_errstr(status));
1118 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
1119 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1120 printf("Both secret buffers and both times not returned\n");
1124 blob1.data = r6.out.new_val->buf->data;
1125 blob1.length = r6.out.new_val->buf->size;
1127 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1129 secret4 = sess_decrypt_string(mem_ctx,
1130 &blob1, &session_key);
1132 if (strcmp(secret3, secret4) != 0) {
1133 printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1137 blob1.data = r6.out.old_val->buf->data;
1138 blob1.length = r6.out.old_val->buf->length;
1140 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1142 secret2 = sess_decrypt_string(mem_ctx,
1143 &blob1, &session_key);
1145 if (strcmp(secret1, secret2) != 0) {
1146 printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1150 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1151 printf("Returned secret %s had same mtime for both secrets: %s\n",
1153 nt_time_string(mem_ctx, *r6.out.new_mtime));
1159 enc_key = sess_encrypt_string(secret5, &session_key);
1161 r7.in.sec_handle = &sec_handle;
1162 r7.in.old_val = &buf1;
1163 r7.in.old_val->data = enc_key.data;
1164 r7.in.old_val->length = enc_key.length;
1165 r7.in.old_val->size = enc_key.length;
1166 r7.in.new_val = NULL;
1168 printf("Testing SetSecret of old Secret only\n");
1170 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
1171 if (!NT_STATUS_IS_OK(status)) {
1172 printf("SetSecret failed - %s\n", nt_errstr(status));
1176 data_blob_free(&enc_key);
1178 /* fetch the secret back again */
1179 r8.in.sec_handle = &sec_handle;
1180 r8.in.new_val = &bufp1;
1181 r8.in.new_mtime = &new_mtime;
1182 r8.in.old_val = &bufp2;
1183 r8.in.old_mtime = &old_mtime;
1188 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
1189 if (!NT_STATUS_IS_OK(status)) {
1190 printf("QuerySecret failed - %s\n", nt_errstr(status));
1193 if (!r8.out.new_val || !r8.out.old_val) {
1194 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
1196 } else if (r8.out.new_val->buf == NULL) {
1198 printf("NEW secret buffer not returned after GLOBAL OLD set\n");
1201 } else if (r8.out.old_val->buf == NULL) {
1202 printf("OLD secret buffer not returned after OLD set\n");
1204 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1205 printf("Both times not returned after OLD set\n");
1209 printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
1212 blob1.data = r8.out.new_val->buf->data;
1213 blob1.length = r8.out.new_val->buf->length;
1215 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1217 secret6 = sess_decrypt_string(mem_ctx,
1218 &blob1, &session_key);
1220 if (strcmp(secret3, secret4) != 0) {
1221 printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
1225 blob1.data = r8.out.old_val->buf->data;
1226 blob1.length = r8.out.old_val->buf->size;
1228 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1230 secret6 = sess_decrypt_string(mem_ctx,
1231 &blob1, &session_key);
1233 if (strcmp(secret5, secret6) != 0) {
1234 printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1238 if (*r8.out.new_mtime == *r8.out.old_mtime) {
1240 printf("Returned secret %s had same mtime for both secrets: %s\n",
1242 nt_time_string(mem_ctx, *r8.out.new_mtime));
1246 printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n",
1248 nt_time_string(mem_ctx, *r8.out.old_mtime),
1249 nt_time_string(mem_ctx, *r8.out.new_mtime));
1255 if (!test_Delete(p, mem_ctx, &sec_handle)) {
1259 d.in.handle = &sec_handle2;
1260 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
1261 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1262 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1266 printf("Testing OpenSecret of just-deleted secret\n");
1268 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1269 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1270 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1281 static bool test_EnumAccountRights(struct dcerpc_pipe *p,
1282 TALLOC_CTX *mem_ctx,
1283 struct policy_handle *acct_handle,
1284 struct dom_sid *sid)
1287 struct lsa_EnumAccountRights r;
1288 struct lsa_RightSet rights;
1290 printf("Testing EnumAccountRights\n");
1292 r.in.handle = acct_handle;
1294 r.out.rights = &rights;
1296 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1297 if (!NT_STATUS_IS_OK(status)) {
1298 printf("EnumAccountRights of %s failed - %s\n",
1299 dom_sid_string(mem_ctx, sid), nt_errstr(status));
1307 static bool test_QuerySecurity(struct dcerpc_pipe *p,
1308 TALLOC_CTX *mem_ctx,
1309 struct policy_handle *handle,
1310 struct policy_handle *acct_handle)
1313 struct lsa_QuerySecurity r;
1315 if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
1316 printf("skipping QuerySecurity test against Samba4\n");
1320 printf("Testing QuerySecurity\n");
1322 r.in.handle = acct_handle;
1325 status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
1326 if (!NT_STATUS_IS_OK(status)) {
1327 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1334 static bool test_OpenAccount(struct dcerpc_pipe *p,
1335 TALLOC_CTX *mem_ctx,
1336 struct policy_handle *handle,
1337 struct dom_sid *sid)
1340 struct lsa_OpenAccount r;
1341 struct policy_handle acct_handle;
1343 printf("Testing OpenAccount\n");
1345 r.in.handle = handle;
1347 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1348 r.out.acct_handle = &acct_handle;
1350 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1351 if (!NT_STATUS_IS_OK(status)) {
1352 printf("OpenAccount failed - %s\n", nt_errstr(status));
1356 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1360 if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1367 static bool test_EnumAccounts(struct dcerpc_pipe *p,
1368 TALLOC_CTX *mem_ctx,
1369 struct policy_handle *handle)
1372 struct lsa_EnumAccounts r;
1373 struct lsa_SidArray sids1, sids2;
1374 uint32_t resume_handle = 0;
1378 printf("\ntesting EnumAccounts\n");
1380 r.in.handle = handle;
1381 r.in.resume_handle = &resume_handle;
1382 r.in.num_entries = 100;
1383 r.out.resume_handle = &resume_handle;
1384 r.out.sids = &sids1;
1388 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1389 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1392 if (!NT_STATUS_IS_OK(status)) {
1393 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1397 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1401 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1405 if (!test_LookupSids3(p, mem_ctx, &sids1)) {
1409 printf("testing all accounts\n");
1410 for (i=0;i<sids1.num_sids;i++) {
1411 ret &= test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1412 ret &= test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1417 if (sids1.num_sids < 3) {
1421 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1423 r.in.num_entries = 1;
1424 r.out.sids = &sids2;
1426 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1427 if (!NT_STATUS_IS_OK(status)) {
1428 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1432 if (sids2.num_sids != 1) {
1433 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1440 static bool test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1441 TALLOC_CTX *mem_ctx,
1442 struct policy_handle *handle,
1443 struct lsa_String *priv_name)
1445 struct lsa_LookupPrivDisplayName r;
1447 /* produce a reasonable range of language output without screwing up
1449 uint16_t language_id = (random() % 4) + 0x409;
1451 printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1453 r.in.handle = handle;
1454 r.in.name = priv_name;
1455 r.in.language_id = &language_id;
1456 r.out.language_id = &language_id;
1459 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1460 if (!NT_STATUS_IS_OK(status)) {
1461 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1464 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
1465 priv_name->string, r.out.disp_name->string,
1466 *r.in.language_id, *r.out.language_id);
1471 static bool test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1472 TALLOC_CTX *mem_ctx,
1473 struct policy_handle *handle,
1474 struct lsa_String *priv_name)
1476 struct lsa_EnumAccountsWithUserRight r;
1477 struct lsa_SidArray sids;
1482 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1484 r.in.handle = handle;
1485 r.in.name = priv_name;
1488 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1490 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1491 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1495 if (!NT_STATUS_IS_OK(status)) {
1496 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1504 static bool test_EnumPrivs(struct dcerpc_pipe *p,
1505 TALLOC_CTX *mem_ctx,
1506 struct policy_handle *handle)
1509 struct lsa_EnumPrivs r;
1510 struct lsa_PrivArray privs1;
1511 uint32_t resume_handle = 0;
1515 printf("\ntesting EnumPrivs\n");
1517 r.in.handle = handle;
1518 r.in.resume_handle = &resume_handle;
1519 r.in.max_count = 100;
1520 r.out.resume_handle = &resume_handle;
1521 r.out.privs = &privs1;
1524 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1525 if (!NT_STATUS_IS_OK(status)) {
1526 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1530 for (i = 0; i< privs1.count; i++) {
1531 test_LookupPrivDisplayName(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1532 test_LookupPrivValue(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1533 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
1541 static bool test_QueryForestTrustInformation(struct dcerpc_pipe *p,
1542 TALLOC_CTX *mem_ctx,
1543 struct policy_handle *handle,
1544 const char *trusted_domain_name)
1547 struct lsa_lsaRQueryForestTrustInformation r;
1549 struct lsa_String string;
1550 struct lsa_ForestTrustInformation info, *info_ptr;
1552 printf("\nTesting lsaRQueryForestTrustInformation\n");
1554 if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
1555 printf("skipping QueryForestTrustInformation against Samba4\n");
1559 ZERO_STRUCT(string);
1561 if (trusted_domain_name) {
1562 init_lsa_String(&string, trusted_domain_name);
1567 r.in.handle = handle;
1568 r.in.trusted_domain_name = &string;
1570 r.out.forest_trust_info = &info_ptr;
1572 status = dcerpc_lsa_lsaRQueryForestTrustInformation(p, mem_ctx, &r);
1574 if (!NT_STATUS_IS_OK(status)) {
1575 printf("lsaRQueryForestTrustInformation failed - %s\n", nt_errstr(status));
1582 static bool test_query_each_TrustDomEx(struct dcerpc_pipe *p,
1583 TALLOC_CTX *mem_ctx,
1584 struct policy_handle *handle,
1585 struct lsa_DomainListEx *domains)
1590 for (i=0; i< domains->count; i++) {
1592 if (domains->domains[i].trust_attributes & NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1593 ret &= test_QueryForestTrustInformation(p, mem_ctx, handle,
1594 domains->domains[i].domain_name.string);
1601 static bool test_query_each_TrustDom(struct dcerpc_pipe *p,
1602 TALLOC_CTX *mem_ctx,
1603 struct policy_handle *handle,
1604 struct lsa_DomainList *domains)
1610 printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1611 for (i=0; i< domains->count; i++) {
1612 struct lsa_OpenTrustedDomain trust;
1613 struct lsa_OpenTrustedDomainByName trust_by_name;
1614 struct policy_handle trustdom_handle;
1615 struct policy_handle handle2;
1617 struct lsa_CloseTrustedDomainEx c_trust;
1618 int levels [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
1619 int ok[] = {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1};
1621 if (domains->domains[i].sid) {
1622 trust.in.handle = handle;
1623 trust.in.sid = domains->domains[i].sid;
1624 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1625 trust.out.trustdom_handle = &trustdom_handle;
1627 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1629 if (!NT_STATUS_IS_OK(status)) {
1630 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1634 c.in.handle = &trustdom_handle;
1635 c.out.handle = &handle2;
1637 c_trust.in.handle = &trustdom_handle;
1638 c_trust.out.handle = &handle2;
1640 for (j=0; j < ARRAY_SIZE(levels); j++) {
1641 struct lsa_QueryTrustedDomainInfo q;
1642 union lsa_TrustedDomainInfo info;
1643 q.in.trustdom_handle = &trustdom_handle;
1644 q.in.level = levels[j];
1646 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1647 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1648 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1649 levels[j], nt_errstr(status));
1651 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1652 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1653 levels[j], nt_errstr(status));
1658 status = dcerpc_lsa_CloseTrustedDomainEx(p, mem_ctx, &c_trust);
1659 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1660 printf("Expected CloseTrustedDomainEx to return NT_STATUS_NOT_IMPLEMENTED, instead - %s\n", nt_errstr(status));
1664 c.in.handle = &trustdom_handle;
1665 c.out.handle = &handle2;
1667 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1668 if (!NT_STATUS_IS_OK(status)) {
1669 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1673 for (j=0; j < ARRAY_SIZE(levels); j++) {
1674 struct lsa_QueryTrustedDomainInfoBySid q;
1675 union lsa_TrustedDomainInfo info;
1677 if (!domains->domains[i].sid) {
1681 q.in.handle = handle;
1682 q.in.dom_sid = domains->domains[i].sid;
1683 q.in.level = levels[j];
1685 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1686 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1687 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n",
1688 levels[j], nt_errstr(status));
1690 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1691 printf("QueryTrustedDomainInfoBySid level %d unexpectedly succeeded - %s\n",
1692 levels[j], nt_errstr(status));
1698 trust_by_name.in.handle = handle;
1699 trust_by_name.in.name.string = domains->domains[i].name.string;
1700 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1701 trust_by_name.out.trustdom_handle = &trustdom_handle;
1703 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1705 if (!NT_STATUS_IS_OK(status)) {
1706 printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1710 for (j=0; j < ARRAY_SIZE(levels); j++) {
1711 struct lsa_QueryTrustedDomainInfo q;
1712 union lsa_TrustedDomainInfo info;
1713 q.in.trustdom_handle = &trustdom_handle;
1714 q.in.level = levels[j];
1716 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1717 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1718 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1719 levels[j], nt_errstr(status));
1721 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1722 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1723 levels[j], nt_errstr(status));
1728 c.in.handle = &trustdom_handle;
1729 c.out.handle = &handle2;
1731 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1737 for (j=0; j < ARRAY_SIZE(levels); j++) {
1738 struct lsa_QueryTrustedDomainInfoByName q;
1739 union lsa_TrustedDomainInfo info;
1740 q.in.handle = handle;
1741 q.in.trusted_domain.string = domains->domains[i].name.string;
1742 q.in.level = levels[j];
1744 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1745 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1746 printf("QueryTrustedDomainInfoByName level %d failed - %s\n",
1747 levels[j], nt_errstr(status));
1749 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1750 printf("QueryTrustedDomainInfoByName level %d unexpectedly succeeded - %s\n",
1751 levels[j], nt_errstr(status));
1759 static bool test_EnumTrustDom(struct dcerpc_pipe *p,
1760 TALLOC_CTX *mem_ctx,
1761 struct policy_handle *handle)
1763 struct lsa_EnumTrustDom r;
1764 struct lsa_EnumTrustedDomainsEx r_ex;
1765 NTSTATUS enum_status;
1766 uint32_t resume_handle = 0;
1767 struct lsa_DomainList domains;
1768 struct lsa_DomainListEx domains_ex;
1771 printf("\nTesting EnumTrustDom\n");
1774 r.in.handle = handle;
1775 r.in.resume_handle = &resume_handle;
1776 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
1777 r.out.domains = &domains;
1778 r.out.resume_handle = &resume_handle;
1780 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1782 /* NO_MORE_ENTRIES is allowed */
1783 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1785 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1786 /* Windows 2003 gets this off by one on the first run */
1787 if (r.out.domains->count < 3 || r.out.domains->count > 4) {
1788 printf("EnumTrustDom didn't fill the buffer we "
1789 "asked it to (got %d, expected %d / %d == %d entries)\n",
1790 r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3,
1791 LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
1794 } else if (!NT_STATUS_IS_OK(enum_status)) {
1795 printf("EnumTrustDom failed - %s\n", nt_errstr(enum_status));
1799 ret &= test_query_each_TrustDom(p, mem_ctx, handle, &domains);
1801 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1803 printf("\nTesting EnumTrustedDomainsEx\n");
1807 r_ex.in.handle = handle;
1808 r_ex.in.resume_handle = &resume_handle;
1809 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1810 r_ex.out.domains = &domains_ex;
1811 r_ex.out.resume_handle = &resume_handle;
1813 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1815 /* NO_MORE_ENTRIES is allowed */
1816 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1818 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1819 /* Windows 2003 gets this off by one on the first run */
1820 if (r_ex.out.domains->count < 3 || r_ex.out.domains->count > 4) {
1821 printf("EnumTrustDom didn't fill the buffer we "
1822 "asked it to (got %d, expected %d / %d == %d entries)\n",
1823 r_ex.out.domains->count,
1825 LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER,
1826 r_ex.in.max_size / LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER);
1830 } else if (!NT_STATUS_IS_OK(enum_status)) {
1831 printf("EnumTrustedDomainEx failed - %s\n", nt_errstr(enum_status));
1835 ret &= test_query_each_TrustDomEx(p, mem_ctx, handle, &domains_ex);
1837 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1842 static bool test_CreateTrustedDomain(struct dcerpc_pipe *p,
1843 TALLOC_CTX *mem_ctx,
1844 struct policy_handle *handle)
1848 struct lsa_CreateTrustedDomain r;
1849 struct lsa_DomainInfo trustinfo;
1850 struct dom_sid *domsid[12];
1851 struct policy_handle trustdom_handle[12];
1852 struct lsa_QueryTrustedDomainInfo q;
1855 printf("Testing CreateTrustedDomain for 12 domains\n");
1857 for (i=0; i< 12; i++) {
1858 char *trust_name = talloc_asprintf(mem_ctx, "torturedom%02d", i);
1859 char *trust_sid = talloc_asprintf(mem_ctx, "S-1-5-21-97398-379795-100%02d", i);
1861 domsid[i] = dom_sid_parse_talloc(mem_ctx, trust_sid);
1863 trustinfo.sid = domsid[i];
1864 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
1866 r.in.handle = handle;
1867 r.in.info = &trustinfo;
1868 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1869 r.out.trustdom_handle = &trustdom_handle[i];
1871 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1872 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1873 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1874 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1876 if (!NT_STATUS_IS_OK(status)) {
1877 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1881 q.in.trustdom_handle = &trustdom_handle[i];
1882 q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
1883 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1884 if (!NT_STATUS_IS_OK(status)) {
1885 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
1887 } else if (!q.out.info) {
1890 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
1891 printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
1892 q.out.info->name.netbios_name.string, trustinfo.name.string);
1899 /* now that we have some domains to look over, we can test the enum calls */
1900 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1904 for (i=0; i<12; i++) {
1905 if (!test_DeleteTrustedDomainBySid(p, mem_ctx, handle, domsid[i])) {
1913 static bool test_QueryDomainInfoPolicy(struct dcerpc_pipe *p,
1914 TALLOC_CTX *mem_ctx,
1915 struct policy_handle *handle)
1917 struct lsa_QueryDomainInformationPolicy r;
1921 if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
1922 printf("skipping QueryDomainInformationPolicy test against Samba4\n");
1926 printf("\nTesting QueryDomainInformationPolicy\n");
1929 r.in.handle = handle;
1932 printf("\ntrying QueryDomainInformationPolicy level %d\n", i);
1934 status = dcerpc_lsa_QueryDomainInformationPolicy(p, mem_ctx, &r);
1936 if (!NT_STATUS_IS_OK(status)) {
1937 printf("QueryDomainInformationPolicy failed - %s\n", nt_errstr(status));
1947 static bool test_QueryInfoPolicy(struct dcerpc_pipe *p,
1948 TALLOC_CTX *mem_ctx,
1949 struct policy_handle *handle)
1951 struct lsa_QueryInfoPolicy r;
1955 printf("\nTesting QueryInfoPolicy\n");
1957 if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
1958 printf("skipping QueryInfoPolicy against Samba4\n");
1962 for (i=1;i<13;i++) {
1963 r.in.handle = handle;
1966 printf("\ntrying QueryInfoPolicy level %d\n", i);
1968 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
1971 case LSA_POLICY_INFO_DB:
1972 case LSA_POLICY_INFO_AUDIT_FULL_SET:
1973 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
1974 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1975 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
1979 case LSA_POLICY_INFO_DOMAIN:
1980 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
1981 case LSA_POLICY_INFO_DNS:
1982 if (!NT_STATUS_IS_OK(status)) {
1983 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1988 if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
1989 /* Other levels not implemented yet */
1990 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
1991 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1994 } else if (!NT_STATUS_IS_OK(status)) {
1995 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2001 if (NT_STATUS_IS_OK(status) && i == LSA_POLICY_INFO_DNS) {
2002 /* Let's look up some of these names */
2004 struct lsa_TransNameArray tnames;
2006 tnames.names = talloc_array(mem_ctx, struct lsa_TranslatedName, tnames.count);
2007 tnames.names[0].name.string = r.out.info->dns.name.string;
2008 tnames.names[1].name.string = r.out.info->dns.dns_domain.string;
2009 tnames.names[2].name.string = talloc_asprintf(mem_ctx, "%s\\", r.out.info->dns.name.string);
2010 tnames.names[3].name.string = talloc_asprintf(mem_ctx, "%s\\", r.out.info->dns.dns_domain.string);
2011 tnames.names[4].name.string = talloc_asprintf(mem_ctx, "%s\\guest", r.out.info->dns.name.string);
2012 tnames.names[5].name.string = talloc_asprintf(mem_ctx, "%s\\krbtgt", r.out.info->dns.name.string);
2013 tnames.names[6].name.string = talloc_asprintf(mem_ctx, "%s\\guest", r.out.info->dns.dns_domain.string);
2014 tnames.names[7].name.string = talloc_asprintf(mem_ctx, "%s\\krbtgt", r.out.info->dns.dns_domain.string);
2015 tnames.names[8].name.string = talloc_asprintf(mem_ctx, "krbtgt@%s", r.out.info->dns.name.string);
2016 tnames.names[9].name.string = talloc_asprintf(mem_ctx, "krbtgt@%s", r.out.info->dns.dns_domain.string);
2017 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
2024 static bool test_QueryInfoPolicy2(struct dcerpc_pipe *p,
2025 TALLOC_CTX *mem_ctx,
2026 struct policy_handle *handle)
2028 struct lsa_QueryInfoPolicy2 r;
2032 printf("\nTesting QueryInfoPolicy2\n");
2033 for (i=1;i<13;i++) {
2034 r.in.handle = handle;
2037 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
2039 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
2042 case LSA_POLICY_INFO_DB:
2043 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2044 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2045 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2046 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2050 case LSA_POLICY_INFO_DOMAIN:
2051 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2052 case LSA_POLICY_INFO_DNS:
2053 if (!NT_STATUS_IS_OK(status)) {
2054 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2059 if (lp_parm_bool(global_loadparm, NULL, "torture", "samba4", false)) {
2060 /* Other levels not implemented yet */
2061 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2062 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2065 } else if (!NT_STATUS_IS_OK(status)) {
2066 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2076 static bool test_GetUserName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
2078 struct lsa_GetUserName r;
2081 struct lsa_StringPointer authority_name_p;
2083 printf("\nTesting GetUserName\n");
2085 r.in.system_name = "\\";
2086 r.in.account_name = NULL;
2087 r.in.authority_name = &authority_name_p;
2088 authority_name_p.string = NULL;
2090 status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
2092 if (!NT_STATUS_IS_OK(status)) {
2093 printf("GetUserName failed - %s\n", nt_errstr(status));
2100 bool test_lsa_Close(struct dcerpc_pipe *p,
2101 TALLOC_CTX *mem_ctx,
2102 struct policy_handle *handle)
2106 struct policy_handle handle2;
2108 printf("\ntesting Close\n");
2110 r.in.handle = handle;
2111 r.out.handle = &handle2;
2113 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2114 if (!NT_STATUS_IS_OK(status)) {
2115 printf("Close failed - %s\n", nt_errstr(status));
2119 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2120 /* its really a fault - we need a status code for rpc fault */
2121 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
2122 printf("Close failed - %s\n", nt_errstr(status));
2131 bool torture_rpc_lsa(struct torture_context *torture)
2134 struct dcerpc_pipe *p;
2135 TALLOC_CTX *mem_ctx;
2137 struct policy_handle *handle;
2139 mem_ctx = talloc_init("torture_rpc_lsa");
2141 status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
2142 if (!NT_STATUS_IS_OK(status)) {
2143 talloc_free(mem_ctx);
2147 if (!test_OpenPolicy(p, mem_ctx)) {
2151 if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
2156 if (!test_LookupNames_wellknown(p, mem_ctx, handle)) {
2160 if (!test_LookupNames_bogus(p, mem_ctx, handle)) {
2164 if (!test_LookupSids_async(p, mem_ctx, handle)) {
2168 if (!test_QueryDomainInfoPolicy(p, mem_ctx, handle)) {
2172 if (!test_CreateAccount(p, mem_ctx, handle)) {
2176 if (!test_CreateSecret(p, mem_ctx, handle)) {
2180 if (!test_CreateTrustedDomain(p, mem_ctx, handle)) {
2184 if (!test_EnumAccounts(p, mem_ctx, handle)) {
2188 if (!test_EnumPrivs(p, mem_ctx, handle)) {
2192 if (!test_QueryInfoPolicy(p, mem_ctx, handle)) {
2196 if (!test_QueryInfoPolicy2(p, mem_ctx, handle)) {
2201 if (!test_Delete(p, mem_ctx, handle)) {
2206 if (!test_many_LookupSids(p, mem_ctx, handle)) {
2210 if (!test_lsa_Close(p, mem_ctx, handle)) {
2214 if (!test_many_LookupSids(p, mem_ctx, handle)) {
2219 if (!test_GetUserName(p, mem_ctx)) {
2223 talloc_free(mem_ctx);
2228 bool torture_rpc_lsa_get_user(struct torture_context *torture)
2231 struct dcerpc_pipe *p;
2232 TALLOC_CTX *mem_ctx;
2235 mem_ctx = talloc_init("torture_rpc_lsa_get_user");
2237 status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
2238 if (!NT_STATUS_IS_OK(status)) {
2239 talloc_free(mem_ctx);
2243 if (!test_GetUserName(p, mem_ctx)) {
2247 talloc_free(mem_ctx);