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 #define TEST_MACHINENAME "lsatestmach"
34 static void init_lsa_String(struct lsa_String *name, const char *s)
39 static bool test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
41 struct lsa_ObjectAttribute attr;
42 struct policy_handle handle;
43 struct lsa_QosInfo qos;
44 struct lsa_OpenPolicy r;
46 uint16_t system_name = '\\';
48 printf("\ntesting OpenPolicy\n");
51 qos.impersonation_level = 2;
53 qos.effective_only = 0;
57 attr.object_name = NULL;
62 r.in.system_name = &system_name;
64 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65 r.out.handle = &handle;
67 status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
68 if (!NT_STATUS_IS_OK(status)) {
69 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
70 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
71 printf("not considering %s to be an error\n", nt_errstr(status));
74 printf("OpenPolicy failed - %s\n", nt_errstr(status));
82 bool test_lsa_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
83 struct policy_handle **handle)
85 struct lsa_ObjectAttribute attr;
86 struct lsa_QosInfo qos;
87 struct lsa_OpenPolicy2 r;
90 printf("\ntesting OpenPolicy2\n");
92 *handle = talloc(mem_ctx, struct policy_handle);
98 qos.impersonation_level = 2;
100 qos.effective_only = 0;
103 attr.root_dir = NULL;
104 attr.object_name = NULL;
106 attr.sec_desc = NULL;
109 r.in.system_name = "\\";
111 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
112 r.out.handle = *handle;
114 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
115 if (!NT_STATUS_IS_OK(status)) {
116 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
117 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
118 printf("not considering %s to be an error\n", nt_errstr(status));
119 talloc_free(*handle);
123 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
131 static const char *sid_type_lookup(enum lsa_SidType r)
134 case SID_NAME_USE_NONE: return "SID_NAME_USE_NONE"; break;
135 case SID_NAME_USER: return "SID_NAME_USER"; break;
136 case SID_NAME_DOM_GRP: return "SID_NAME_DOM_GRP"; break;
137 case SID_NAME_DOMAIN: return "SID_NAME_DOMAIN"; break;
138 case SID_NAME_ALIAS: return "SID_NAME_ALIAS"; break;
139 case SID_NAME_WKN_GRP: return "SID_NAME_WKN_GRP"; break;
140 case SID_NAME_DELETED: return "SID_NAME_DELETED"; break;
141 case SID_NAME_INVALID: return "SID_NAME_INVALID"; break;
142 case SID_NAME_UNKNOWN: return "SID_NAME_UNKNOWN"; break;
143 case SID_NAME_COMPUTER: return "SID_NAME_COMPUTER"; break;
145 return "Invalid sid type\n";
148 static bool test_LookupNames(struct dcerpc_pipe *p,
150 struct policy_handle *handle,
151 struct lsa_TransNameArray *tnames)
153 struct lsa_LookupNames r;
154 struct lsa_TransSidArray sids;
155 struct lsa_String *names;
160 printf("\nTesting LookupNames with %d names\n", tnames->count);
165 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
166 for (i=0;i<tnames->count;i++) {
167 init_lsa_String(&names[i], tnames->names[i].name.string);
170 r.in.handle = handle;
171 r.in.num_names = tnames->count;
176 r.out.count = &count;
179 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
181 if (NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED) ||
182 NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
183 for (i=0;i< tnames->count;i++) {
184 if (i < count && sids.sids[i].sid_type == SID_NAME_UNKNOWN) {
185 printf("LookupName of %s was unmapped\n",
186 tnames->names[i].name.string);
187 } else if (i >=count) {
188 printf("LookupName of %s failed to return a result\n",
189 tnames->names[i].name.string);
192 printf("LookupNames failed - %s\n", nt_errstr(status));
194 } else if (!NT_STATUS_IS_OK(status)) {
195 printf("LookupNames failed - %s\n", nt_errstr(status));
199 for (i=0;i< tnames->count;i++) {
200 if (i < count && sids.sids[i].sid_type != tnames->names[i].sid_type) {
201 printf("LookupName of %s got unexpected name type: %s\n",
202 tnames->names[i].name.string, sid_type_lookup(sids.sids[i].sid_type));
203 } else if (i >=count) {
204 printf("LookupName of %s failed to return a result\n",
205 tnames->names[i].name.string);
213 static bool test_LookupNames_bogus(struct dcerpc_pipe *p,
215 struct policy_handle *handle)
217 struct lsa_LookupNames r;
218 struct lsa_TransSidArray sids;
219 struct lsa_String *names;
224 struct lsa_TranslatedName name;
225 struct lsa_TransNameArray tnames;
227 tnames.names = &name;
229 name.name.string = "NT AUTHORITY\\BOGUS";
231 printf("\nTesting LookupNames with bogus names\n");
236 names = talloc_array(mem_ctx, struct lsa_String, tnames.count);
237 for (i=0;i<tnames.count;i++) {
238 init_lsa_String(&names[i], tnames.names[i].name.string);
241 r.in.handle = handle;
242 r.in.num_names = tnames.count;
247 r.out.count = &count;
250 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
251 if (!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
252 printf("LookupNames failed - %s\n", nt_errstr(status));
261 static bool test_LookupNames_wellknown(struct dcerpc_pipe *p,
263 struct policy_handle *handle)
265 struct lsa_TranslatedName name;
266 struct lsa_TransNameArray tnames;
269 printf("Testing LookupNames with well known names\n");
271 tnames.names = &name;
273 name.name.string = "NT AUTHORITY\\SYSTEM";
274 name.sid_type = SID_NAME_WKN_GRP;
275 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
277 name.name.string = "NT AUTHORITY\\ANONYMOUS LOGON";
278 name.sid_type = SID_NAME_WKN_GRP;
279 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
281 name.name.string = "NT AUTHORITY\\Authenticated Users";
282 name.sid_type = SID_NAME_WKN_GRP;
283 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
286 name.name.string = "NT AUTHORITY";
287 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
289 name.name.string = "NT AUTHORITY\\";
290 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
293 name.name.string = "BUILTIN\\";
294 name.sid_type = SID_NAME_DOMAIN;
295 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
297 name.name.string = "BUILTIN\\Administrators";
298 name.sid_type = SID_NAME_ALIAS;
299 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
301 name.name.string = "SYSTEM";
302 name.sid_type = SID_NAME_WKN_GRP;
303 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
305 name.name.string = "Everyone";
306 name.sid_type = SID_NAME_WKN_GRP;
307 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
311 static bool test_LookupNames2(struct dcerpc_pipe *p,
313 struct policy_handle *handle,
314 struct lsa_TransNameArray2 *tnames)
316 struct lsa_LookupNames2 r;
317 struct lsa_TransSidArray2 sids;
318 struct lsa_String *names;
323 printf("\nTesting LookupNames2 with %d names\n", tnames->count);
328 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
329 for (i=0;i<tnames->count;i++) {
330 init_lsa_String(&names[i], tnames->names[i].name.string);
333 r.in.handle = handle;
334 r.in.num_names = tnames->count;
341 r.out.count = &count;
344 status = dcerpc_lsa_LookupNames2(p, mem_ctx, &r);
345 if (!NT_STATUS_IS_OK(status)) {
346 printf("LookupNames2 failed - %s\n", nt_errstr(status));
356 static bool test_LookupNames3(struct dcerpc_pipe *p,
358 struct policy_handle *handle,
359 struct lsa_TransNameArray2 *tnames)
361 struct lsa_LookupNames3 r;
362 struct lsa_TransSidArray3 sids;
363 struct lsa_String *names;
368 printf("\nTesting LookupNames3 with %d names\n", tnames->count);
373 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
374 for (i=0;i<tnames->count;i++) {
375 init_lsa_String(&names[i], tnames->names[i].name.string);
378 r.in.handle = handle;
379 r.in.num_names = tnames->count;
386 r.out.count = &count;
389 status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
390 if (!NT_STATUS_IS_OK(status)) {
391 printf("LookupNames3 failed - %s\n", nt_errstr(status));
400 static bool test_LookupNames4(struct dcerpc_pipe *p,
402 struct lsa_TransNameArray2 *tnames)
404 struct lsa_LookupNames4 r;
405 struct lsa_TransSidArray3 sids;
406 struct lsa_String *names;
411 printf("\nTesting LookupNames4 with %d names\n", tnames->count);
416 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
417 for (i=0;i<tnames->count;i++) {
418 init_lsa_String(&names[i], tnames->names[i].name.string);
421 r.in.num_names = tnames->count;
428 r.out.count = &count;
431 status = dcerpc_lsa_LookupNames4(p, mem_ctx, &r);
432 if (!NT_STATUS_IS_OK(status)) {
433 printf("LookupNames4 failed - %s\n", nt_errstr(status));
443 static bool test_LookupSids(struct dcerpc_pipe *p,
445 struct policy_handle *handle,
446 struct lsa_SidArray *sids)
448 struct lsa_LookupSids r;
449 struct lsa_TransNameArray names;
450 uint32_t count = sids->num_sids;
453 printf("\nTesting LookupSids\n");
458 r.in.handle = handle;
463 r.out.count = &count;
464 r.out.names = &names;
466 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
467 if (!NT_STATUS_IS_OK(status)) {
468 printf("LookupSids failed - %s\n", nt_errstr(status));
474 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
482 static bool test_LookupSids2(struct dcerpc_pipe *p,
484 struct policy_handle *handle,
485 struct lsa_SidArray *sids)
487 struct lsa_LookupSids2 r;
488 struct lsa_TransNameArray2 names;
489 uint32_t count = sids->num_sids;
492 printf("\nTesting LookupSids2\n");
497 r.in.handle = handle;
504 r.out.count = &count;
505 r.out.names = &names;
507 status = dcerpc_lsa_LookupSids2(p, mem_ctx, &r);
508 if (!NT_STATUS_IS_OK(status)) {
509 printf("LookupSids2 failed - %s\n", nt_errstr(status));
515 if (!test_LookupNames2(p, mem_ctx, handle, &names)) {
519 if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
526 static bool test_LookupSids3(struct dcerpc_pipe *p,
528 struct lsa_SidArray *sids)
530 struct lsa_LookupSids3 r;
531 struct lsa_TransNameArray2 names;
532 uint32_t count = sids->num_sids;
535 printf("\nTesting LookupSids3\n");
546 r.out.count = &count;
547 r.out.names = &names;
549 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
550 if (!NT_STATUS_IS_OK(status)) {
551 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
552 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
553 printf("not considering %s to be an error\n", nt_errstr(status));
556 printf("LookupSids3 failed - %s - not considered an error\n",
563 if (!test_LookupNames4(p, mem_ctx, &names)) {
570 bool test_many_LookupSids(struct dcerpc_pipe *p,
572 struct policy_handle *handle)
576 struct lsa_SidArray sids;
579 printf("\nTesting LookupSids with lots of SIDs\n");
583 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
585 for (i=0; i<sids.num_sids; i++) {
586 const char *sidstr = "S-1-5-32-545";
587 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
590 count = sids.num_sids;
593 struct lsa_LookupSids r;
594 struct lsa_TransNameArray names;
598 r.in.handle = handle;
602 r.in.count = &names.count;
603 r.out.count = &count;
604 r.out.names = &names;
606 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
607 if (!NT_STATUS_IS_OK(status)) {
608 printf("LookupSids failed - %s\n", nt_errstr(status));
614 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
617 } else if (p->conn->security_state.auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL &&
618 p->conn->security_state.auth_info->auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
619 struct lsa_LookupSids3 r;
620 struct lsa_TransNameArray2 names;
625 printf("\nTesting LookupSids3\n");
633 r.out.count = &count;
634 r.out.names = &names;
636 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
637 if (!NT_STATUS_IS_OK(status)) {
638 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
639 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
640 printf("not considering %s to be an error\n", nt_errstr(status));
643 printf("LookupSids3 failed - %s\n",
647 if (!test_LookupNames4(p, mem_ctx, &names)) {
659 static void lookupsids_cb(struct rpc_request *req)
661 int *replies = (int *)req->async.private_data;
664 status = dcerpc_ndr_request_recv(req);
665 if (!NT_STATUS_IS_OK(status)) {
666 printf("lookupsids returned %s\n", nt_errstr(status));
675 static bool test_LookupSids_async(struct dcerpc_pipe *p,
677 struct policy_handle *handle)
679 struct lsa_SidArray sids;
680 struct lsa_SidPtr sidptr;
682 struct lsa_TransNameArray *names;
683 struct lsa_LookupSids *r;
684 struct rpc_request **req;
687 const int num_async_requests = 50;
689 count = talloc_array(mem_ctx, uint32_t, num_async_requests);
690 names = talloc_array(mem_ctx, struct lsa_TransNameArray, num_async_requests);
691 r = talloc_array(mem_ctx, struct lsa_LookupSids, num_async_requests);
693 printf("\nTesting %d async lookupsids request\n", num_async_requests);
695 req = talloc_array(mem_ctx, struct rpc_request *, num_async_requests);
699 sidptr.sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-545");
703 for (i=0; i<num_async_requests; i++) {
706 names[i].names = NULL;
708 r[i].in.handle = handle;
709 r[i].in.sids = &sids;
710 r[i].in.names = &names[i];
712 r[i].in.count = &names[i].count;
713 r[i].out.count = &count[i];
714 r[i].out.names = &names[i];
716 req[i] = dcerpc_lsa_LookupSids_send(p, req, &r[i]);
717 if (req[i] == NULL) {
722 req[i]->async.callback = lookupsids_cb;
723 req[i]->async.private_data = &replies;
726 while (replies >= 0 && replies < num_async_requests) {
727 event_loop_once(p->conn->event_ctx);
739 static bool test_LookupPrivValue(struct dcerpc_pipe *p,
741 struct policy_handle *handle,
742 struct lsa_String *name)
745 struct lsa_LookupPrivValue r;
746 struct lsa_LUID luid;
748 r.in.handle = handle;
752 status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
753 if (!NT_STATUS_IS_OK(status)) {
754 printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
761 static bool test_LookupPrivName(struct dcerpc_pipe *p,
763 struct policy_handle *handle,
764 struct lsa_LUID *luid)
767 struct lsa_LookupPrivName r;
769 r.in.handle = handle;
772 status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
773 if (!NT_STATUS_IS_OK(status)) {
774 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
781 static bool test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p,
783 struct policy_handle *acct_handle,
784 struct lsa_LUID *luid)
787 struct lsa_RemovePrivilegesFromAccount r;
788 struct lsa_PrivilegeSet privs;
791 printf("Testing RemovePrivilegesFromAccount\n");
793 r.in.handle = acct_handle;
799 privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
800 privs.set[0].luid = *luid;
801 privs.set[0].attribute = 0;
803 status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
804 if (!NT_STATUS_IS_OK(status)) {
805 printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
812 static bool test_AddPrivilegesToAccount(struct dcerpc_pipe *p,
814 struct policy_handle *acct_handle,
815 struct lsa_LUID *luid)
818 struct lsa_AddPrivilegesToAccount r;
819 struct lsa_PrivilegeSet privs;
822 printf("Testing AddPrivilegesToAccount\n");
824 r.in.handle = acct_handle;
829 privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
830 privs.set[0].luid = *luid;
831 privs.set[0].attribute = 0;
833 status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
834 if (!NT_STATUS_IS_OK(status)) {
835 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
842 static bool test_EnumPrivsAccount(struct dcerpc_pipe *p,
844 struct policy_handle *handle,
845 struct policy_handle *acct_handle)
848 struct lsa_EnumPrivsAccount r;
851 printf("Testing EnumPrivsAccount\n");
853 r.in.handle = acct_handle;
855 status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
856 if (!NT_STATUS_IS_OK(status)) {
857 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
861 if (r.out.privs && r.out.privs->count > 0) {
863 for (i=0;i<r.out.privs->count;i++) {
864 test_LookupPrivName(p, mem_ctx, handle,
865 &r.out.privs->set[i].luid);
868 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle,
869 &r.out.privs->set[0].luid);
870 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle,
871 &r.out.privs->set[0].luid);
877 static bool test_Delete(struct dcerpc_pipe *p,
879 struct policy_handle *handle)
884 printf("testing Delete\n");
886 r.in.handle = handle;
887 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
888 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
889 printf("Delete should have failed NT_STATUS_NOT_SUPPORTED - %s\n", nt_errstr(status));
896 static bool test_DeleteObject(struct dcerpc_pipe *p,
898 struct policy_handle *handle)
901 struct lsa_DeleteObject r;
903 printf("testing DeleteObject\n");
905 r.in.handle = handle;
906 r.out.handle = handle;
907 status = dcerpc_lsa_DeleteObject(p, mem_ctx, &r);
908 if (!NT_STATUS_IS_OK(status)) {
909 printf("Delete failed - %s\n", nt_errstr(status));
917 static bool test_CreateAccount(struct dcerpc_pipe *p,
919 struct policy_handle *handle)
922 struct lsa_CreateAccount r;
923 struct dom_sid2 *newsid;
924 struct policy_handle acct_handle;
926 newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
928 printf("Testing CreateAccount\n");
930 r.in.handle = handle;
932 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
933 r.out.acct_handle = &acct_handle;
935 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
936 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
937 struct lsa_OpenAccount r_o;
938 r_o.in.handle = handle;
940 r_o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
941 r_o.out.acct_handle = &acct_handle;
943 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r_o);
944 if (!NT_STATUS_IS_OK(status)) {
945 printf("OpenAccount failed - %s\n", nt_errstr(status));
948 } else if (!NT_STATUS_IS_OK(status)) {
949 printf("CreateAccount failed - %s\n", nt_errstr(status));
953 if (!test_Delete(p, mem_ctx, &acct_handle)) {
957 if (!test_DeleteObject(p, mem_ctx, &acct_handle)) {
964 static bool test_DeleteTrustedDomain(struct dcerpc_pipe *p,
966 struct policy_handle *handle,
967 struct lsa_StringLarge name)
970 struct lsa_OpenTrustedDomainByName r;
971 struct policy_handle trustdom_handle;
973 r.in.handle = handle;
974 r.in.name.string = name.string;
975 r.in.access_mask = SEC_STD_DELETE;
976 r.out.trustdom_handle = &trustdom_handle;
978 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
979 if (!NT_STATUS_IS_OK(status)) {
980 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
984 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
988 if (!test_DeleteObject(p, mem_ctx, &trustdom_handle)) {
995 static bool test_DeleteTrustedDomainBySid(struct dcerpc_pipe *p,
997 struct policy_handle *handle,
1001 struct lsa_DeleteTrustedDomain r;
1003 r.in.handle = handle;
1006 status = dcerpc_lsa_DeleteTrustedDomain(p, mem_ctx, &r);
1007 if (!NT_STATUS_IS_OK(status)) {
1008 printf("lsa_DeleteTrustedDomain failed - %s\n", nt_errstr(status));
1016 static bool test_CreateSecret(struct dcerpc_pipe *p,
1017 TALLOC_CTX *mem_ctx,
1018 struct policy_handle *handle)
1021 struct lsa_CreateSecret r;
1022 struct lsa_OpenSecret r2;
1023 struct lsa_SetSecret r3;
1024 struct lsa_QuerySecret r4;
1025 struct lsa_SetSecret r5;
1026 struct lsa_QuerySecret r6;
1027 struct lsa_SetSecret r7;
1028 struct lsa_QuerySecret r8;
1029 struct policy_handle sec_handle, sec_handle2, sec_handle3;
1030 struct lsa_DeleteObject d_o;
1031 struct lsa_DATA_BUF buf1;
1032 struct lsa_DATA_BUF_PTR bufp1;
1033 struct lsa_DATA_BUF_PTR bufp2;
1036 DATA_BLOB session_key;
1037 NTTIME old_mtime, new_mtime;
1038 DATA_BLOB blob1, blob2;
1039 const char *secret1 = "abcdef12345699qwerty";
1041 const char *secret3 = "ABCDEF12345699QWERTY";
1043 const char *secret5 = "NEW-SAMBA4-SECRET";
1047 const int LOCAL = 0;
1048 const int GLOBAL = 1;
1050 secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
1051 secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
1053 for (i=0; i< 2; i++) {
1054 printf("Testing CreateSecret of %s\n", secname[i]);
1056 init_lsa_String(&r.in.name, secname[i]);
1058 r.in.handle = handle;
1059 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1060 r.out.sec_handle = &sec_handle;
1062 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 printf("CreateSecret failed - %s\n", nt_errstr(status));
1068 r.in.handle = handle;
1069 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1070 r.out.sec_handle = &sec_handle3;
1072 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1073 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1074 printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
1078 r2.in.handle = handle;
1079 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1080 r2.in.name = r.in.name;
1081 r2.out.sec_handle = &sec_handle2;
1083 printf("Testing OpenSecret\n");
1085 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1086 if (!NT_STATUS_IS_OK(status)) {
1087 printf("OpenSecret failed - %s\n", nt_errstr(status));
1091 status = dcerpc_fetch_session_key(p, &session_key);
1092 if (!NT_STATUS_IS_OK(status)) {
1093 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
1097 enc_key = sess_encrypt_string(secret1, &session_key);
1099 r3.in.sec_handle = &sec_handle;
1100 r3.in.new_val = &buf1;
1101 r3.in.old_val = NULL;
1102 r3.in.new_val->data = enc_key.data;
1103 r3.in.new_val->length = enc_key.length;
1104 r3.in.new_val->size = enc_key.length;
1106 printf("Testing SetSecret\n");
1108 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1109 if (!NT_STATUS_IS_OK(status)) {
1110 printf("SetSecret failed - %s\n", nt_errstr(status));
1114 r3.in.sec_handle = &sec_handle;
1115 r3.in.new_val = &buf1;
1116 r3.in.old_val = NULL;
1117 r3.in.new_val->data = enc_key.data;
1118 r3.in.new_val->length = enc_key.length;
1119 r3.in.new_val->size = enc_key.length;
1121 /* break the encrypted data */
1124 printf("Testing SetSecret with broken key\n");
1126 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1127 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
1128 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
1132 data_blob_free(&enc_key);
1134 ZERO_STRUCT(new_mtime);
1135 ZERO_STRUCT(old_mtime);
1137 /* fetch the secret back again */
1138 r4.in.sec_handle = &sec_handle;
1139 r4.in.new_val = &bufp1;
1140 r4.in.new_mtime = &new_mtime;
1141 r4.in.old_val = NULL;
1142 r4.in.old_mtime = NULL;
1146 printf("Testing QuerySecret\n");
1147 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 printf("QuerySecret failed - %s\n", nt_errstr(status));
1152 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
1153 printf("No secret buffer returned\n");
1156 blob1.data = r4.out.new_val->buf->data;
1157 blob1.length = r4.out.new_val->buf->size;
1159 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1161 secret2 = sess_decrypt_string(mem_ctx,
1162 &blob1, &session_key);
1164 if (strcmp(secret1, secret2) != 0) {
1165 printf("Returned secret '%s' doesn't match '%s'\n",
1172 enc_key = sess_encrypt_string(secret3, &session_key);
1174 r5.in.sec_handle = &sec_handle;
1175 r5.in.new_val = &buf1;
1176 r5.in.old_val = NULL;
1177 r5.in.new_val->data = enc_key.data;
1178 r5.in.new_val->length = enc_key.length;
1179 r5.in.new_val->size = enc_key.length;
1181 printf("Testing SetSecret (existing value should move to old)\n");
1183 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
1184 if (!NT_STATUS_IS_OK(status)) {
1185 printf("SetSecret failed - %s\n", nt_errstr(status));
1189 data_blob_free(&enc_key);
1191 ZERO_STRUCT(new_mtime);
1192 ZERO_STRUCT(old_mtime);
1194 /* fetch the secret back again */
1195 r6.in.sec_handle = &sec_handle;
1196 r6.in.new_val = &bufp1;
1197 r6.in.new_mtime = &new_mtime;
1198 r6.in.old_val = &bufp2;
1199 r6.in.old_mtime = &old_mtime;
1204 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 printf("QuerySecret failed - %s\n", nt_errstr(status));
1211 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
1212 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1213 printf("Both secret buffers and both times not returned\n");
1217 blob1.data = r6.out.new_val->buf->data;
1218 blob1.length = r6.out.new_val->buf->size;
1220 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1222 secret4 = sess_decrypt_string(mem_ctx,
1223 &blob1, &session_key);
1225 if (strcmp(secret3, secret4) != 0) {
1226 printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1230 blob1.data = r6.out.old_val->buf->data;
1231 blob1.length = r6.out.old_val->buf->length;
1233 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1235 secret2 = sess_decrypt_string(mem_ctx,
1236 &blob1, &session_key);
1238 if (strcmp(secret1, secret2) != 0) {
1239 printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1243 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1244 printf("Returned secret %s had same mtime for both secrets: %s\n",
1246 nt_time_string(mem_ctx, *r6.out.new_mtime));
1252 enc_key = sess_encrypt_string(secret5, &session_key);
1254 r7.in.sec_handle = &sec_handle;
1255 r7.in.old_val = &buf1;
1256 r7.in.old_val->data = enc_key.data;
1257 r7.in.old_val->length = enc_key.length;
1258 r7.in.old_val->size = enc_key.length;
1259 r7.in.new_val = NULL;
1261 printf("Testing SetSecret of old Secret only\n");
1263 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
1264 if (!NT_STATUS_IS_OK(status)) {
1265 printf("SetSecret failed - %s\n", nt_errstr(status));
1269 data_blob_free(&enc_key);
1271 /* fetch the secret back again */
1272 r8.in.sec_handle = &sec_handle;
1273 r8.in.new_val = &bufp1;
1274 r8.in.new_mtime = &new_mtime;
1275 r8.in.old_val = &bufp2;
1276 r8.in.old_mtime = &old_mtime;
1281 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
1282 if (!NT_STATUS_IS_OK(status)) {
1283 printf("QuerySecret failed - %s\n", nt_errstr(status));
1286 if (!r8.out.new_val || !r8.out.old_val) {
1287 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
1289 } else if (r8.out.new_val->buf != NULL) {
1290 printf("NEW secret buffer must not be returned after OLD set\n");
1292 } else if (r8.out.old_val->buf == NULL) {
1293 printf("OLD secret buffer was not returned after OLD set\n");
1295 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1296 printf("Both times not returned after OLD set\n");
1299 blob1.data = r8.out.old_val->buf->data;
1300 blob1.length = r8.out.old_val->buf->size;
1302 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1304 secret6 = sess_decrypt_string(mem_ctx,
1305 &blob1, &session_key);
1307 if (strcmp(secret5, secret6) != 0) {
1308 printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1312 if (*r8.out.new_mtime != *r8.out.old_mtime) {
1313 printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n",
1315 nt_time_string(mem_ctx, *r8.out.old_mtime),
1316 nt_time_string(mem_ctx, *r8.out.new_mtime));
1322 if (!test_Delete(p, mem_ctx, &sec_handle)) {
1326 if (!test_DeleteObject(p, mem_ctx, &sec_handle)) {
1330 d_o.in.handle = &sec_handle2;
1331 d_o.out.handle = &sec_handle2;
1332 status = dcerpc_lsa_DeleteObject(p, mem_ctx, &d_o);
1333 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1334 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1338 printf("Testing OpenSecret of just-deleted secret\n");
1340 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1341 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1342 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1353 static bool test_EnumAccountRights(struct dcerpc_pipe *p,
1354 TALLOC_CTX *mem_ctx,
1355 struct policy_handle *acct_handle,
1356 struct dom_sid *sid)
1359 struct lsa_EnumAccountRights r;
1360 struct lsa_RightSet rights;
1362 printf("Testing EnumAccountRights\n");
1364 r.in.handle = acct_handle;
1366 r.out.rights = &rights;
1368 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1369 if (!NT_STATUS_IS_OK(status)) {
1370 printf("EnumAccountRights of %s failed - %s\n",
1371 dom_sid_string(mem_ctx, sid), nt_errstr(status));
1379 static bool test_QuerySecurity(struct dcerpc_pipe *p,
1380 struct torture_context *tctx,
1381 struct policy_handle *handle,
1382 struct policy_handle *acct_handle)
1385 struct lsa_QuerySecurity r;
1387 if (torture_setting_bool(tctx, "samba4", false)) {
1388 printf("skipping QuerySecurity test against Samba4\n");
1392 printf("Testing QuerySecurity\n");
1394 r.in.handle = acct_handle;
1397 status = dcerpc_lsa_QuerySecurity(p, tctx, &r);
1398 if (!NT_STATUS_IS_OK(status)) {
1399 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1406 static bool test_OpenAccount(struct dcerpc_pipe *p,
1407 TALLOC_CTX *mem_ctx,
1408 struct policy_handle *handle,
1409 struct dom_sid *sid)
1412 struct lsa_OpenAccount r;
1413 struct policy_handle acct_handle;
1415 printf("Testing OpenAccount\n");
1417 r.in.handle = handle;
1419 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1420 r.out.acct_handle = &acct_handle;
1422 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1423 if (!NT_STATUS_IS_OK(status)) {
1424 printf("OpenAccount failed - %s\n", nt_errstr(status));
1428 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1432 if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1439 static bool test_EnumAccounts(struct dcerpc_pipe *p,
1440 TALLOC_CTX *mem_ctx,
1441 struct policy_handle *handle)
1444 struct lsa_EnumAccounts r;
1445 struct lsa_SidArray sids1, sids2;
1446 uint32_t resume_handle = 0;
1450 printf("\ntesting EnumAccounts\n");
1452 r.in.handle = handle;
1453 r.in.resume_handle = &resume_handle;
1454 r.in.num_entries = 100;
1455 r.out.resume_handle = &resume_handle;
1456 r.out.sids = &sids1;
1460 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1461 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1464 if (!NT_STATUS_IS_OK(status)) {
1465 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1469 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1473 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1477 /* Can't test lookupSids3 here, as clearly we must not
1478 * be on schannel, or we would not be able to do the
1481 printf("testing all accounts\n");
1482 for (i=0;i<sids1.num_sids;i++) {
1483 ret &= test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1484 ret &= test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1489 if (sids1.num_sids < 3) {
1493 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1495 r.in.num_entries = 1;
1496 r.out.sids = &sids2;
1498 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1499 if (!NT_STATUS_IS_OK(status)) {
1500 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1504 if (sids2.num_sids != 1) {
1505 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1512 static bool test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1513 TALLOC_CTX *mem_ctx,
1514 struct policy_handle *handle,
1515 struct lsa_String *priv_name)
1517 struct lsa_LookupPrivDisplayName r;
1519 /* produce a reasonable range of language output without screwing up
1521 uint16_t language_id = (random() % 4) + 0x409;
1523 printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1525 r.in.handle = handle;
1526 r.in.name = priv_name;
1527 r.in.language_id = &language_id;
1528 r.out.language_id = &language_id;
1531 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1536 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
1537 priv_name->string, r.out.disp_name->string,
1538 *r.in.language_id, *r.out.language_id);
1543 static bool test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1544 TALLOC_CTX *mem_ctx,
1545 struct policy_handle *handle,
1546 struct lsa_String *priv_name)
1548 struct lsa_EnumAccountsWithUserRight r;
1549 struct lsa_SidArray sids;
1554 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1556 r.in.handle = handle;
1557 r.in.name = priv_name;
1560 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1562 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1563 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1567 if (!NT_STATUS_IS_OK(status)) {
1568 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1576 static bool test_EnumPrivs(struct dcerpc_pipe *p,
1577 TALLOC_CTX *mem_ctx,
1578 struct policy_handle *handle)
1581 struct lsa_EnumPrivs r;
1582 struct lsa_PrivArray privs1;
1583 uint32_t resume_handle = 0;
1587 printf("\ntesting EnumPrivs\n");
1589 r.in.handle = handle;
1590 r.in.resume_handle = &resume_handle;
1591 r.in.max_count = 100;
1592 r.out.resume_handle = &resume_handle;
1593 r.out.privs = &privs1;
1596 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1597 if (!NT_STATUS_IS_OK(status)) {
1598 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1602 for (i = 0; i< privs1.count; i++) {
1603 test_LookupPrivDisplayName(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1604 test_LookupPrivValue(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1605 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
1613 static bool test_QueryForestTrustInformation(struct dcerpc_pipe *p,
1614 struct torture_context *tctx,
1615 struct policy_handle *handle,
1616 const char *trusted_domain_name)
1619 struct lsa_lsaRQueryForestTrustInformation r;
1621 struct lsa_String string;
1622 struct lsa_ForestTrustInformation info, *info_ptr;
1624 printf("\nTesting lsaRQueryForestTrustInformation\n");
1626 if (torture_setting_bool(tctx, "samba4", false)) {
1627 printf("skipping QueryForestTrustInformation against Samba4\n");
1631 ZERO_STRUCT(string);
1633 if (trusted_domain_name) {
1634 init_lsa_String(&string, trusted_domain_name);
1639 r.in.handle = handle;
1640 r.in.trusted_domain_name = &string;
1642 r.out.forest_trust_info = &info_ptr;
1644 status = dcerpc_lsa_lsaRQueryForestTrustInformation(p, tctx, &r);
1646 if (!NT_STATUS_IS_OK(status)) {
1647 printf("lsaRQueryForestTrustInformation failed - %s\n", nt_errstr(status));
1654 static bool test_query_each_TrustDomEx(struct dcerpc_pipe *p,
1655 TALLOC_CTX *mem_ctx,
1656 struct policy_handle *handle,
1657 struct lsa_DomainListEx *domains)
1662 for (i=0; i< domains->count; i++) {
1664 if (domains->domains[i].trust_attributes & NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1665 ret &= test_QueryForestTrustInformation(p, mem_ctx, handle,
1666 domains->domains[i].domain_name.string);
1673 static bool test_query_each_TrustDom(struct dcerpc_pipe *p,
1674 TALLOC_CTX *mem_ctx,
1675 struct policy_handle *handle,
1676 struct lsa_DomainList *domains)
1682 printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1683 for (i=0; i< domains->count; i++) {
1684 struct lsa_OpenTrustedDomain trust;
1685 struct lsa_OpenTrustedDomainByName trust_by_name;
1686 struct policy_handle trustdom_handle;
1687 struct policy_handle handle2;
1689 struct lsa_CloseTrustedDomainEx c_trust;
1690 int levels [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
1691 int ok[] = {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1};
1693 if (domains->domains[i].sid) {
1694 trust.in.handle = handle;
1695 trust.in.sid = domains->domains[i].sid;
1696 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1697 trust.out.trustdom_handle = &trustdom_handle;
1699 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1701 if (!NT_STATUS_IS_OK(status)) {
1702 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1706 c.in.handle = &trustdom_handle;
1707 c.out.handle = &handle2;
1709 c_trust.in.handle = &trustdom_handle;
1710 c_trust.out.handle = &handle2;
1712 for (j=0; j < ARRAY_SIZE(levels); j++) {
1713 struct lsa_QueryTrustedDomainInfo q;
1714 union lsa_TrustedDomainInfo info;
1715 q.in.trustdom_handle = &trustdom_handle;
1716 q.in.level = levels[j];
1718 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1719 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1720 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1721 levels[j], nt_errstr(status));
1723 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1724 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1725 levels[j], nt_errstr(status));
1730 status = dcerpc_lsa_CloseTrustedDomainEx(p, mem_ctx, &c_trust);
1731 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1732 printf("Expected CloseTrustedDomainEx to return NT_STATUS_NOT_IMPLEMENTED, instead - %s\n", nt_errstr(status));
1736 c.in.handle = &trustdom_handle;
1737 c.out.handle = &handle2;
1739 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1740 if (!NT_STATUS_IS_OK(status)) {
1741 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1745 for (j=0; j < ARRAY_SIZE(levels); j++) {
1746 struct lsa_QueryTrustedDomainInfoBySid q;
1747 union lsa_TrustedDomainInfo info;
1749 if (!domains->domains[i].sid) {
1753 q.in.handle = handle;
1754 q.in.dom_sid = domains->domains[i].sid;
1755 q.in.level = levels[j];
1757 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1758 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1759 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n",
1760 levels[j], nt_errstr(status));
1762 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1763 printf("QueryTrustedDomainInfoBySid level %d unexpectedly succeeded - %s\n",
1764 levels[j], nt_errstr(status));
1770 trust_by_name.in.handle = handle;
1771 trust_by_name.in.name.string = domains->domains[i].name.string;
1772 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1773 trust_by_name.out.trustdom_handle = &trustdom_handle;
1775 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1777 if (!NT_STATUS_IS_OK(status)) {
1778 printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1782 for (j=0; j < ARRAY_SIZE(levels); j++) {
1783 struct lsa_QueryTrustedDomainInfo q;
1784 union lsa_TrustedDomainInfo info;
1785 q.in.trustdom_handle = &trustdom_handle;
1786 q.in.level = levels[j];
1788 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1789 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1790 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1791 levels[j], nt_errstr(status));
1793 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1794 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1795 levels[j], nt_errstr(status));
1800 c.in.handle = &trustdom_handle;
1801 c.out.handle = &handle2;
1803 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1804 if (!NT_STATUS_IS_OK(status)) {
1805 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1809 for (j=0; j < ARRAY_SIZE(levels); j++) {
1810 struct lsa_QueryTrustedDomainInfoByName q;
1811 union lsa_TrustedDomainInfo info;
1812 q.in.handle = handle;
1813 q.in.trusted_domain.string = domains->domains[i].name.string;
1814 q.in.level = levels[j];
1816 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1817 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1818 printf("QueryTrustedDomainInfoByName level %d failed - %s\n",
1819 levels[j], nt_errstr(status));
1821 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1822 printf("QueryTrustedDomainInfoByName level %d unexpectedly succeeded - %s\n",
1823 levels[j], nt_errstr(status));
1831 static bool test_EnumTrustDom(struct dcerpc_pipe *p,
1832 TALLOC_CTX *mem_ctx,
1833 struct policy_handle *handle)
1835 struct lsa_EnumTrustDom r;
1836 struct lsa_EnumTrustedDomainsEx r_ex;
1837 NTSTATUS enum_status;
1838 uint32_t resume_handle = 0;
1839 struct lsa_DomainList domains;
1840 struct lsa_DomainListEx domains_ex;
1843 printf("\nTesting EnumTrustDom\n");
1845 r.in.handle = handle;
1846 r.in.resume_handle = &resume_handle;
1848 r.out.domains = &domains;
1849 r.out.resume_handle = &resume_handle;
1851 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1853 if (NT_STATUS_IS_OK(enum_status)) {
1854 if (domains.count == 0) {
1855 printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1858 } else if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1859 printf("EnumTrustDom of zero size failed - %s\n", nt_errstr(enum_status));
1864 r.in.handle = handle;
1865 r.in.resume_handle = &resume_handle;
1866 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
1867 r.out.domains = &domains;
1868 r.out.resume_handle = &resume_handle;
1870 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1872 /* NO_MORE_ENTRIES is allowed */
1873 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1875 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1876 /* Windows 2003 gets this off by one on the first run */
1877 if (r.out.domains->count < 3 || r.out.domains->count > 4) {
1878 printf("EnumTrustDom didn't fill the buffer we "
1879 "asked it to (got %d, expected %d / %d == %d entries)\n",
1880 r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3,
1881 LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
1884 } else if (!NT_STATUS_IS_OK(enum_status)) {
1885 printf("EnumTrustDom failed - %s\n", nt_errstr(enum_status));
1889 if (domains.count == 0) {
1890 printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1894 ret &= test_query_each_TrustDom(p, mem_ctx, handle, &domains);
1896 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1898 printf("\nTesting EnumTrustedDomainsEx\n");
1900 r_ex.in.handle = handle;
1901 r_ex.in.resume_handle = &resume_handle;
1902 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1903 r_ex.out.domains = &domains_ex;
1904 r_ex.out.resume_handle = &resume_handle;
1906 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1908 if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1909 printf("EnumTrustedDomainEx of zero size failed - %s\n", nt_errstr(enum_status));
1915 r_ex.in.handle = handle;
1916 r_ex.in.resume_handle = &resume_handle;
1917 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1918 r_ex.out.domains = &domains_ex;
1919 r_ex.out.resume_handle = &resume_handle;
1921 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1923 /* NO_MORE_ENTRIES is allowed */
1924 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1926 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1927 /* Windows 2003 gets this off by one on the first run */
1928 if (r_ex.out.domains->count < 3 || r_ex.out.domains->count > 4) {
1929 printf("EnumTrustDom didn't fill the buffer we "
1930 "asked it to (got %d, expected %d / %d == %d entries)\n",
1931 r_ex.out.domains->count,
1933 LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER,
1934 r_ex.in.max_size / LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER);
1936 } else if (!NT_STATUS_IS_OK(enum_status)) {
1937 printf("EnumTrustedDomainEx failed - %s\n", nt_errstr(enum_status));
1941 if (domains_ex.count == 0) {
1942 printf("EnumTrustDomainEx failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1946 ret &= test_query_each_TrustDomEx(p, mem_ctx, handle, &domains_ex);
1948 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1953 static bool test_CreateTrustedDomain(struct dcerpc_pipe *p,
1954 TALLOC_CTX *mem_ctx,
1955 struct policy_handle *handle)
1959 struct lsa_CreateTrustedDomain r;
1960 struct lsa_DomainInfo trustinfo;
1961 struct dom_sid *domsid[12];
1962 struct policy_handle trustdom_handle[12];
1963 struct lsa_QueryTrustedDomainInfo q;
1966 printf("Testing CreateTrustedDomain for 12 domains\n");
1968 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1972 for (i=0; i< 12; i++) {
1973 char *trust_name = talloc_asprintf(mem_ctx, "torturedom%02d", i);
1974 char *trust_sid = talloc_asprintf(mem_ctx, "S-1-5-21-97398-379795-100%02d", i);
1976 domsid[i] = dom_sid_parse_talloc(mem_ctx, trust_sid);
1978 trustinfo.sid = domsid[i];
1979 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
1981 r.in.handle = handle;
1982 r.in.info = &trustinfo;
1983 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1984 r.out.trustdom_handle = &trustdom_handle[i];
1986 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1987 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1988 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1989 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1991 if (!NT_STATUS_IS_OK(status)) {
1992 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1996 q.in.trustdom_handle = &trustdom_handle[i];
1997 q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
1998 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1999 if (!NT_STATUS_IS_OK(status)) {
2000 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
2002 } else if (!q.out.info) {
2005 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
2006 printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
2007 q.out.info->name.netbios_name.string, trustinfo.name.string);
2014 /* now that we have some domains to look over, we can test the enum calls */
2015 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
2019 for (i=0; i<12; i++) {
2020 if (!test_DeleteTrustedDomainBySid(p, mem_ctx, handle, domsid[i])) {
2028 static bool test_QueryDomainInfoPolicy(struct dcerpc_pipe *p,
2029 struct torture_context *tctx,
2030 struct policy_handle *handle)
2032 struct lsa_QueryDomainInformationPolicy r;
2036 if (torture_setting_bool(tctx, "samba4", false)) {
2037 printf("skipping QueryDomainInformationPolicy test against Samba4\n");
2041 printf("\nTesting QueryDomainInformationPolicy\n");
2044 r.in.handle = handle;
2047 printf("\ntrying QueryDomainInformationPolicy level %d\n", i);
2049 status = dcerpc_lsa_QueryDomainInformationPolicy(p, tctx, &r);
2051 if (!NT_STATUS_IS_OK(status)) {
2052 printf("QueryDomainInformationPolicy failed - %s\n", nt_errstr(status));
2062 static bool test_QueryInfoPolicy(struct dcerpc_pipe *p,
2063 struct torture_context *tctx,
2064 struct policy_handle *handle)
2066 struct lsa_QueryInfoPolicy r;
2070 printf("\nTesting QueryInfoPolicy\n");
2072 for (i=1;i<13;i++) {
2073 r.in.handle = handle;
2076 printf("\ntrying QueryInfoPolicy level %d\n", i);
2078 status = dcerpc_lsa_QueryInfoPolicy(p, tctx, &r);
2081 case LSA_POLICY_INFO_DB:
2082 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2083 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2084 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2085 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2089 case LSA_POLICY_INFO_DOMAIN:
2090 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2091 case LSA_POLICY_INFO_DNS:
2092 if (!NT_STATUS_IS_OK(status)) {
2093 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2098 if (torture_setting_bool(tctx, "samba4", false)) {
2099 /* Other levels not implemented yet */
2100 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2101 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2104 } else if (!NT_STATUS_IS_OK(status)) {
2105 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2111 if (NT_STATUS_IS_OK(status) && i == LSA_POLICY_INFO_DNS) {
2112 /* Let's look up some of these names */
2114 struct lsa_TransNameArray tnames;
2116 tnames.names = talloc_zero_array(tctx, struct lsa_TranslatedName, tnames.count);
2117 tnames.names[0].name.string = r.out.info->dns.name.string;
2118 tnames.names[0].sid_type = SID_NAME_DOMAIN;
2119 tnames.names[1].name.string = r.out.info->dns.dns_domain.string;
2120 tnames.names[1].sid_type = SID_NAME_DOMAIN;
2121 tnames.names[2].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.name.string);
2122 tnames.names[2].sid_type = SID_NAME_DOMAIN;
2123 tnames.names[3].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.dns_domain.string);
2124 tnames.names[3].sid_type = SID_NAME_DOMAIN;
2125 tnames.names[4].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.name.string);
2126 tnames.names[4].sid_type = SID_NAME_USER;
2127 tnames.names[5].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.name.string);
2128 tnames.names[5].sid_type = SID_NAME_USER;
2129 tnames.names[6].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.dns_domain.string);
2130 tnames.names[6].sid_type = SID_NAME_USER;
2131 tnames.names[7].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.dns_domain.string);
2132 tnames.names[7].sid_type = SID_NAME_USER;
2133 tnames.names[8].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.name.string);
2134 tnames.names[8].sid_type = SID_NAME_USER;
2135 tnames.names[9].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.dns_domain.string);
2136 tnames.names[9].sid_type = SID_NAME_USER;
2137 tnames.names[10].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.name.string);
2138 tnames.names[10].sid_type = SID_NAME_USER;
2139 tnames.names[11].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.dns_domain.string);
2140 tnames.names[11].sid_type = SID_NAME_USER;
2141 tnames.names[12].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.name.string);
2142 tnames.names[12].sid_type = SID_NAME_USER;
2143 tnames.names[13].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.dns_domain.string);
2144 tnames.names[13].sid_type = SID_NAME_USER;
2145 ret &= test_LookupNames(p, tctx, handle, &tnames);
2153 static bool test_QueryInfoPolicy2(struct dcerpc_pipe *p,
2154 struct torture_context *tctx,
2155 struct policy_handle *handle)
2157 struct lsa_QueryInfoPolicy2 r;
2161 printf("\nTesting QueryInfoPolicy2\n");
2162 for (i=1;i<13;i++) {
2163 r.in.handle = handle;
2166 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
2168 status = dcerpc_lsa_QueryInfoPolicy2(p, tctx, &r);
2171 case LSA_POLICY_INFO_DB:
2172 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2173 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2174 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2175 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2179 case LSA_POLICY_INFO_DOMAIN:
2180 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2181 case LSA_POLICY_INFO_DNS:
2182 if (!NT_STATUS_IS_OK(status)) {
2183 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2188 if (torture_setting_bool(tctx, "samba4", false)) {
2189 /* Other levels not implemented yet */
2190 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2191 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2194 } else if (!NT_STATUS_IS_OK(status)) {
2195 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2205 static bool test_GetUserName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
2207 struct lsa_GetUserName r;
2210 struct lsa_StringPointer authority_name_p;
2212 printf("\nTesting GetUserName\n");
2214 r.in.system_name = "\\";
2215 r.in.account_name = NULL;
2216 r.in.authority_name = &authority_name_p;
2217 authority_name_p.string = NULL;
2219 status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
2221 if (!NT_STATUS_IS_OK(status)) {
2222 printf("GetUserName failed - %s\n", nt_errstr(status));
2229 bool test_lsa_Close(struct dcerpc_pipe *p,
2230 TALLOC_CTX *mem_ctx,
2231 struct policy_handle *handle)
2235 struct policy_handle handle2;
2237 printf("\ntesting Close\n");
2239 r.in.handle = handle;
2240 r.out.handle = &handle2;
2242 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2243 if (!NT_STATUS_IS_OK(status)) {
2244 printf("Close failed - %s\n", nt_errstr(status));
2248 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2249 /* its really a fault - we need a status code for rpc fault */
2250 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
2251 printf("Close failed - %s\n", nt_errstr(status));
2260 bool torture_rpc_lsa(struct torture_context *tctx)
2263 struct dcerpc_pipe *p;
2265 struct policy_handle *handle;
2266 struct test_join *join = NULL;
2267 struct cli_credentials *machine_creds;
2269 status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
2270 if (!NT_STATUS_IS_OK(status)) {
2274 if (!test_OpenPolicy(p, tctx)) {
2278 if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2283 join = torture_join_domain(tctx, TEST_MACHINENAME, ACB_WSTRUST, &machine_creds);
2288 if (!test_LookupNames_wellknown(p, tctx, handle)) {
2292 if (!test_LookupNames_bogus(p, tctx, handle)) {
2296 if (!test_LookupSids_async(p, tctx, handle)) {
2300 if (!test_QueryDomainInfoPolicy(p, tctx, handle)) {
2304 if (!test_CreateAccount(p, tctx, handle)) {
2308 if (!test_CreateSecret(p, tctx, handle)) {
2312 if (!test_CreateTrustedDomain(p, tctx, handle)) {
2316 if (!test_EnumAccounts(p, tctx, handle)) {
2320 if (!test_EnumPrivs(p, tctx, handle)) {
2324 if (!test_QueryInfoPolicy(p, tctx, handle)) {
2328 if (!test_QueryInfoPolicy2(p, tctx, handle)) {
2332 if (!test_Delete(p, tctx, handle)) {
2336 if (!test_many_LookupSids(p, tctx, handle)) {
2340 if (!test_lsa_Close(p, tctx, handle)) {
2344 torture_leave_domain(tctx, join);
2347 if (!test_many_LookupSids(p, tctx, handle)) {
2352 if (!test_GetUserName(p, tctx)) {
2359 bool torture_rpc_lsa_get_user(struct torture_context *torture)
2362 struct dcerpc_pipe *p;
2363 TALLOC_CTX *mem_ctx;
2366 mem_ctx = talloc_init("torture_rpc_lsa_get_user");
2368 status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
2369 if (!NT_STATUS_IS_OK(status)) {
2370 talloc_free(mem_ctx);
2374 if (!test_GetUserName(p, mem_ctx)) {
2378 talloc_free(mem_ctx);