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"
31 #define TEST_MACHINENAME "lsatestmach"
33 static void init_lsa_String(struct lsa_String *name, const char *s)
38 static bool test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
40 struct lsa_ObjectAttribute attr;
41 struct policy_handle handle;
42 struct lsa_QosInfo qos;
43 struct lsa_OpenPolicy r;
45 uint16_t system_name = '\\';
47 printf("\ntesting OpenPolicy\n");
50 qos.impersonation_level = 2;
52 qos.effective_only = 0;
56 attr.object_name = NULL;
61 r.in.system_name = &system_name;
63 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
64 r.out.handle = &handle;
66 status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
67 if (!NT_STATUS_IS_OK(status)) {
68 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
69 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
70 printf("not considering %s to be an error\n", nt_errstr(status));
73 printf("OpenPolicy failed - %s\n", nt_errstr(status));
81 bool test_lsa_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
82 struct policy_handle **handle)
84 struct lsa_ObjectAttribute attr;
85 struct lsa_QosInfo qos;
86 struct lsa_OpenPolicy2 r;
89 printf("\ntesting OpenPolicy2\n");
91 *handle = talloc(mem_ctx, struct policy_handle);
97 qos.impersonation_level = 2;
99 qos.effective_only = 0;
102 attr.root_dir = NULL;
103 attr.object_name = NULL;
105 attr.sec_desc = NULL;
108 r.in.system_name = "\\";
110 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
111 r.out.handle = *handle;
113 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
114 if (!NT_STATUS_IS_OK(status)) {
115 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
116 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
117 printf("not considering %s to be an error\n", nt_errstr(status));
118 talloc_free(*handle);
122 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
130 static const char *sid_type_lookup(enum lsa_SidType r)
133 case SID_NAME_USE_NONE: return "SID_NAME_USE_NONE"; break;
134 case SID_NAME_USER: return "SID_NAME_USER"; break;
135 case SID_NAME_DOM_GRP: return "SID_NAME_DOM_GRP"; break;
136 case SID_NAME_DOMAIN: return "SID_NAME_DOMAIN"; break;
137 case SID_NAME_ALIAS: return "SID_NAME_ALIAS"; break;
138 case SID_NAME_WKN_GRP: return "SID_NAME_WKN_GRP"; break;
139 case SID_NAME_DELETED: return "SID_NAME_DELETED"; break;
140 case SID_NAME_INVALID: return "SID_NAME_INVALID"; break;
141 case SID_NAME_UNKNOWN: return "SID_NAME_UNKNOWN"; break;
142 case SID_NAME_COMPUTER: return "SID_NAME_COMPUTER"; break;
144 return "Invalid sid type\n";
147 static bool test_LookupNames(struct dcerpc_pipe *p,
149 struct policy_handle *handle,
150 struct lsa_TransNameArray *tnames)
152 struct lsa_LookupNames r;
153 struct lsa_TransSidArray sids;
154 struct lsa_String *names;
159 printf("\nTesting LookupNames with %d names\n", tnames->count);
164 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
165 for (i=0;i<tnames->count;i++) {
166 init_lsa_String(&names[i], tnames->names[i].name.string);
169 r.in.handle = handle;
170 r.in.num_names = tnames->count;
175 r.out.count = &count;
178 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
180 if (NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED) ||
181 NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
182 for (i=0;i< tnames->count;i++) {
183 if (i < count && sids.sids[i].sid_type == SID_NAME_UNKNOWN) {
184 printf("LookupName of %s was unmapped\n",
185 tnames->names[i].name.string);
186 } else if (i >=count) {
187 printf("LookupName of %s failed to return a result\n",
188 tnames->names[i].name.string);
191 printf("LookupNames failed - %s\n", nt_errstr(status));
193 } else if (!NT_STATUS_IS_OK(status)) {
194 printf("LookupNames failed - %s\n", nt_errstr(status));
198 for (i=0;i< tnames->count;i++) {
199 if (i < count && sids.sids[i].sid_type != tnames->names[i].sid_type) {
200 printf("LookupName of %s got unexpected name type: %s\n",
201 tnames->names[i].name.string, sid_type_lookup(sids.sids[i].sid_type));
202 } else if (i >=count) {
203 printf("LookupName of %s failed to return a result\n",
204 tnames->names[i].name.string);
212 static bool test_LookupNames_bogus(struct dcerpc_pipe *p,
214 struct policy_handle *handle)
216 struct lsa_LookupNames r;
217 struct lsa_TransSidArray sids;
218 struct lsa_String *names;
223 struct lsa_TranslatedName name;
224 struct lsa_TransNameArray tnames;
226 tnames.names = &name;
228 name.name.string = "NT AUTHORITY\\BOGUS";
230 printf("\nTesting LookupNames with bogus names\n");
235 names = talloc_array(mem_ctx, struct lsa_String, tnames.count);
236 for (i=0;i<tnames.count;i++) {
237 init_lsa_String(&names[i], tnames.names[i].name.string);
240 r.in.handle = handle;
241 r.in.num_names = tnames.count;
246 r.out.count = &count;
249 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
250 if (!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
251 printf("LookupNames failed - %s\n", nt_errstr(status));
260 static bool test_LookupNames_wellknown(struct dcerpc_pipe *p,
262 struct policy_handle *handle)
264 struct lsa_TranslatedName name;
265 struct lsa_TransNameArray tnames;
268 printf("Testing LookupNames with well known names\n");
270 tnames.names = &name;
272 name.name.string = "NT AUTHORITY\\SYSTEM";
273 name.sid_type = SID_NAME_WKN_GRP;
274 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
276 name.name.string = "NT AUTHORITY\\ANONYMOUS LOGON";
277 name.sid_type = SID_NAME_WKN_GRP;
278 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
280 name.name.string = "NT AUTHORITY\\Authenticated Users";
281 name.sid_type = SID_NAME_WKN_GRP;
282 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
285 name.name.string = "NT AUTHORITY";
286 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
288 name.name.string = "NT AUTHORITY\\";
289 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
292 name.name.string = "BUILTIN\\";
293 name.sid_type = SID_NAME_DOMAIN;
294 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
296 name.name.string = "BUILTIN\\Administrators";
297 name.sid_type = SID_NAME_ALIAS;
298 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
300 name.name.string = "SYSTEM";
301 name.sid_type = SID_NAME_WKN_GRP;
302 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
304 name.name.string = "Everyone";
305 name.sid_type = SID_NAME_WKN_GRP;
306 ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
310 static bool test_LookupNames2(struct dcerpc_pipe *p,
312 struct policy_handle *handle,
313 struct lsa_TransNameArray2 *tnames)
315 struct lsa_LookupNames2 r;
316 struct lsa_TransSidArray2 sids;
317 struct lsa_String *names;
322 printf("\nTesting LookupNames2 with %d names\n", tnames->count);
327 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
328 for (i=0;i<tnames->count;i++) {
329 init_lsa_String(&names[i], tnames->names[i].name.string);
332 r.in.handle = handle;
333 r.in.num_names = tnames->count;
340 r.out.count = &count;
343 status = dcerpc_lsa_LookupNames2(p, mem_ctx, &r);
344 if (!NT_STATUS_IS_OK(status)) {
345 printf("LookupNames2 failed - %s\n", nt_errstr(status));
355 static bool test_LookupNames3(struct dcerpc_pipe *p,
357 struct policy_handle *handle,
358 struct lsa_TransNameArray2 *tnames)
360 struct lsa_LookupNames3 r;
361 struct lsa_TransSidArray3 sids;
362 struct lsa_String *names;
367 printf("\nTesting LookupNames3 with %d names\n", tnames->count);
372 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
373 for (i=0;i<tnames->count;i++) {
374 init_lsa_String(&names[i], tnames->names[i].name.string);
377 r.in.handle = handle;
378 r.in.num_names = tnames->count;
385 r.out.count = &count;
388 status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
389 if (!NT_STATUS_IS_OK(status)) {
390 printf("LookupNames3 failed - %s\n", nt_errstr(status));
399 static bool test_LookupNames4(struct dcerpc_pipe *p,
401 struct lsa_TransNameArray2 *tnames)
403 struct lsa_LookupNames4 r;
404 struct lsa_TransSidArray3 sids;
405 struct lsa_String *names;
410 printf("\nTesting LookupNames4 with %d names\n", tnames->count);
415 names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
416 for (i=0;i<tnames->count;i++) {
417 init_lsa_String(&names[i], tnames->names[i].name.string);
420 r.in.num_names = tnames->count;
427 r.out.count = &count;
430 status = dcerpc_lsa_LookupNames4(p, mem_ctx, &r);
431 if (!NT_STATUS_IS_OK(status)) {
432 printf("LookupNames4 failed - %s\n", nt_errstr(status));
442 static bool test_LookupSids(struct dcerpc_pipe *p,
444 struct policy_handle *handle,
445 struct lsa_SidArray *sids)
447 struct lsa_LookupSids r;
448 struct lsa_TransNameArray names;
449 uint32_t count = sids->num_sids;
452 printf("\nTesting LookupSids\n");
457 r.in.handle = handle;
462 r.out.count = &count;
463 r.out.names = &names;
465 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
466 if (!NT_STATUS_IS_OK(status)) {
467 printf("LookupSids failed - %s\n", nt_errstr(status));
473 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
481 static bool test_LookupSids2(struct dcerpc_pipe *p,
483 struct policy_handle *handle,
484 struct lsa_SidArray *sids)
486 struct lsa_LookupSids2 r;
487 struct lsa_TransNameArray2 names;
488 uint32_t count = sids->num_sids;
491 printf("\nTesting LookupSids2\n");
496 r.in.handle = handle;
503 r.out.count = &count;
504 r.out.names = &names;
506 status = dcerpc_lsa_LookupSids2(p, mem_ctx, &r);
507 if (!NT_STATUS_IS_OK(status)) {
508 printf("LookupSids2 failed - %s\n", nt_errstr(status));
514 if (!test_LookupNames2(p, mem_ctx, handle, &names)) {
518 if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
525 static bool test_LookupSids3(struct dcerpc_pipe *p,
527 struct lsa_SidArray *sids)
529 struct lsa_LookupSids3 r;
530 struct lsa_TransNameArray2 names;
531 uint32_t count = sids->num_sids;
534 printf("\nTesting LookupSids3\n");
545 r.out.count = &count;
546 r.out.names = &names;
548 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
549 if (!NT_STATUS_IS_OK(status)) {
550 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
551 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
552 printf("not considering %s to be an error\n", nt_errstr(status));
555 printf("LookupSids3 failed - %s - not considered an error\n",
562 if (!test_LookupNames4(p, mem_ctx, &names)) {
569 bool test_many_LookupSids(struct dcerpc_pipe *p,
571 struct policy_handle *handle)
575 struct lsa_SidArray sids;
578 printf("\nTesting LookupSids with lots of SIDs\n");
582 sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
584 for (i=0; i<sids.num_sids; i++) {
585 const char *sidstr = "S-1-5-32-545";
586 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
589 count = sids.num_sids;
592 struct lsa_LookupSids r;
593 struct lsa_TransNameArray names;
597 r.in.handle = handle;
601 r.in.count = &names.count;
602 r.out.count = &count;
603 r.out.names = &names;
605 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
606 if (!NT_STATUS_IS_OK(status)) {
607 printf("LookupSids failed - %s\n", nt_errstr(status));
613 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
616 } else if (p->conn->security_state.auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL &&
617 p->conn->security_state.auth_info->auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
618 struct lsa_LookupSids3 r;
619 struct lsa_TransNameArray2 names;
624 printf("\nTesting LookupSids3\n");
632 r.out.count = &count;
633 r.out.names = &names;
635 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
636 if (!NT_STATUS_IS_OK(status)) {
637 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
638 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
639 printf("not considering %s to be an error\n", nt_errstr(status));
642 printf("LookupSids3 failed - %s\n",
646 if (!test_LookupNames4(p, mem_ctx, &names)) {
658 static void lookupsids_cb(struct rpc_request *req)
660 int *replies = (int *)req->async.private_data;
663 status = dcerpc_ndr_request_recv(req);
664 if (!NT_STATUS_IS_OK(status)) {
665 printf("lookupsids returned %s\n", nt_errstr(status));
674 static bool test_LookupSids_async(struct dcerpc_pipe *p,
676 struct policy_handle *handle)
678 struct lsa_SidArray sids;
679 struct lsa_SidPtr sidptr;
681 struct lsa_TransNameArray *names;
682 struct lsa_LookupSids *r;
683 struct rpc_request **req;
686 const int num_async_requests = 50;
688 count = talloc_array(mem_ctx, uint32_t, num_async_requests);
689 names = talloc_array(mem_ctx, struct lsa_TransNameArray, num_async_requests);
690 r = talloc_array(mem_ctx, struct lsa_LookupSids, num_async_requests);
692 printf("\nTesting %d async lookupsids request\n", num_async_requests);
694 req = talloc_array(mem_ctx, struct rpc_request *, num_async_requests);
698 sidptr.sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-545");
702 for (i=0; i<num_async_requests; i++) {
705 names[i].names = NULL;
707 r[i].in.handle = handle;
708 r[i].in.sids = &sids;
709 r[i].in.names = &names[i];
711 r[i].in.count = &names[i].count;
712 r[i].out.count = &count[i];
713 r[i].out.names = &names[i];
715 req[i] = dcerpc_lsa_LookupSids_send(p, req, &r[i]);
716 if (req[i] == NULL) {
721 req[i]->async.callback = lookupsids_cb;
722 req[i]->async.private_data = &replies;
725 while (replies >= 0 && replies < num_async_requests) {
726 event_loop_once(p->conn->event_ctx);
738 static bool test_LookupPrivValue(struct dcerpc_pipe *p,
740 struct policy_handle *handle,
741 struct lsa_String *name)
744 struct lsa_LookupPrivValue r;
745 struct lsa_LUID luid;
747 r.in.handle = handle;
751 status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
752 if (!NT_STATUS_IS_OK(status)) {
753 printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
760 static bool test_LookupPrivName(struct dcerpc_pipe *p,
762 struct policy_handle *handle,
763 struct lsa_LUID *luid)
766 struct lsa_LookupPrivName r;
768 r.in.handle = handle;
771 status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
772 if (!NT_STATUS_IS_OK(status)) {
773 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
780 static bool test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p,
782 struct policy_handle *acct_handle,
783 struct lsa_LUID *luid)
786 struct lsa_RemovePrivilegesFromAccount r;
787 struct lsa_PrivilegeSet privs;
790 printf("Testing RemovePrivilegesFromAccount\n");
792 r.in.handle = acct_handle;
798 privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
799 privs.set[0].luid = *luid;
800 privs.set[0].attribute = 0;
802 status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
803 if (!NT_STATUS_IS_OK(status)) {
804 printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
811 static bool test_AddPrivilegesToAccount(struct dcerpc_pipe *p,
813 struct policy_handle *acct_handle,
814 struct lsa_LUID *luid)
817 struct lsa_AddPrivilegesToAccount r;
818 struct lsa_PrivilegeSet privs;
821 printf("Testing AddPrivilegesToAccount\n");
823 r.in.handle = acct_handle;
828 privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
829 privs.set[0].luid = *luid;
830 privs.set[0].attribute = 0;
832 status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
833 if (!NT_STATUS_IS_OK(status)) {
834 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
841 static bool test_EnumPrivsAccount(struct dcerpc_pipe *p,
843 struct policy_handle *handle,
844 struct policy_handle *acct_handle)
847 struct lsa_EnumPrivsAccount r;
850 printf("Testing EnumPrivsAccount\n");
852 r.in.handle = acct_handle;
854 status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
855 if (!NT_STATUS_IS_OK(status)) {
856 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
860 if (r.out.privs && r.out.privs->count > 0) {
862 for (i=0;i<r.out.privs->count;i++) {
863 test_LookupPrivName(p, mem_ctx, handle,
864 &r.out.privs->set[i].luid);
867 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle,
868 &r.out.privs->set[0].luid);
869 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle,
870 &r.out.privs->set[0].luid);
876 static bool test_Delete(struct dcerpc_pipe *p,
878 struct policy_handle *handle)
883 printf("testing Delete\n");
885 r.in.handle = handle;
886 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
887 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
888 printf("Delete should have failed NT_STATUS_NOT_SUPPORTED - %s\n", nt_errstr(status));
895 static bool test_DeleteObject(struct dcerpc_pipe *p,
897 struct policy_handle *handle)
900 struct lsa_DeleteObject r;
902 printf("testing DeleteObject\n");
904 r.in.handle = handle;
905 r.out.handle = handle;
906 status = dcerpc_lsa_DeleteObject(p, mem_ctx, &r);
907 if (!NT_STATUS_IS_OK(status)) {
908 printf("Delete failed - %s\n", nt_errstr(status));
916 static bool test_CreateAccount(struct dcerpc_pipe *p,
918 struct policy_handle *handle)
921 struct lsa_CreateAccount r;
922 struct dom_sid2 *newsid;
923 struct policy_handle acct_handle;
925 newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
927 printf("Testing CreateAccount\n");
929 r.in.handle = handle;
931 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
932 r.out.acct_handle = &acct_handle;
934 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
935 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
936 struct lsa_OpenAccount r_o;
937 r_o.in.handle = handle;
939 r_o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
940 r_o.out.acct_handle = &acct_handle;
942 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r_o);
943 if (!NT_STATUS_IS_OK(status)) {
944 printf("OpenAccount failed - %s\n", nt_errstr(status));
947 } else if (!NT_STATUS_IS_OK(status)) {
948 printf("CreateAccount failed - %s\n", nt_errstr(status));
952 if (!test_Delete(p, mem_ctx, &acct_handle)) {
956 if (!test_DeleteObject(p, mem_ctx, &acct_handle)) {
963 static bool test_DeleteTrustedDomain(struct dcerpc_pipe *p,
965 struct policy_handle *handle,
966 struct lsa_StringLarge name)
969 struct lsa_OpenTrustedDomainByName r;
970 struct policy_handle trustdom_handle;
972 r.in.handle = handle;
973 r.in.name.string = name.string;
974 r.in.access_mask = SEC_STD_DELETE;
975 r.out.trustdom_handle = &trustdom_handle;
977 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
978 if (!NT_STATUS_IS_OK(status)) {
979 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
983 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
987 if (!test_DeleteObject(p, mem_ctx, &trustdom_handle)) {
994 static bool test_DeleteTrustedDomainBySid(struct dcerpc_pipe *p,
996 struct policy_handle *handle,
1000 struct lsa_DeleteTrustedDomain r;
1002 r.in.handle = handle;
1005 status = dcerpc_lsa_DeleteTrustedDomain(p, mem_ctx, &r);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 printf("lsa_DeleteTrustedDomain failed - %s\n", nt_errstr(status));
1015 static bool test_CreateSecret(struct dcerpc_pipe *p,
1016 TALLOC_CTX *mem_ctx,
1017 struct policy_handle *handle)
1020 struct lsa_CreateSecret r;
1021 struct lsa_OpenSecret r2;
1022 struct lsa_SetSecret r3;
1023 struct lsa_QuerySecret r4;
1024 struct lsa_SetSecret r5;
1025 struct lsa_QuerySecret r6;
1026 struct lsa_SetSecret r7;
1027 struct lsa_QuerySecret r8;
1028 struct policy_handle sec_handle, sec_handle2, sec_handle3;
1029 struct lsa_DeleteObject d_o;
1030 struct lsa_DATA_BUF buf1;
1031 struct lsa_DATA_BUF_PTR bufp1;
1032 struct lsa_DATA_BUF_PTR bufp2;
1035 DATA_BLOB session_key;
1036 NTTIME old_mtime, new_mtime;
1037 DATA_BLOB blob1, blob2;
1038 const char *secret1 = "abcdef12345699qwerty";
1040 const char *secret3 = "ABCDEF12345699QWERTY";
1042 const char *secret5 = "NEW-SAMBA4-SECRET";
1046 const int LOCAL = 0;
1047 const int GLOBAL = 1;
1049 secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
1050 secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
1052 for (i=0; i< 2; i++) {
1053 printf("Testing CreateSecret of %s\n", secname[i]);
1055 init_lsa_String(&r.in.name, secname[i]);
1057 r.in.handle = handle;
1058 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1059 r.out.sec_handle = &sec_handle;
1061 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1062 if (!NT_STATUS_IS_OK(status)) {
1063 printf("CreateSecret failed - %s\n", nt_errstr(status));
1067 r.in.handle = handle;
1068 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1069 r.out.sec_handle = &sec_handle3;
1071 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1072 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1073 printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
1077 r2.in.handle = handle;
1078 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1079 r2.in.name = r.in.name;
1080 r2.out.sec_handle = &sec_handle2;
1082 printf("Testing OpenSecret\n");
1084 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1085 if (!NT_STATUS_IS_OK(status)) {
1086 printf("OpenSecret failed - %s\n", nt_errstr(status));
1090 status = dcerpc_fetch_session_key(p, &session_key);
1091 if (!NT_STATUS_IS_OK(status)) {
1092 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
1096 enc_key = sess_encrypt_string(secret1, &session_key);
1098 r3.in.sec_handle = &sec_handle;
1099 r3.in.new_val = &buf1;
1100 r3.in.old_val = NULL;
1101 r3.in.new_val->data = enc_key.data;
1102 r3.in.new_val->length = enc_key.length;
1103 r3.in.new_val->size = enc_key.length;
1105 printf("Testing SetSecret\n");
1107 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 printf("SetSecret failed - %s\n", nt_errstr(status));
1113 r3.in.sec_handle = &sec_handle;
1114 r3.in.new_val = &buf1;
1115 r3.in.old_val = NULL;
1116 r3.in.new_val->data = enc_key.data;
1117 r3.in.new_val->length = enc_key.length;
1118 r3.in.new_val->size = enc_key.length;
1120 /* break the encrypted data */
1123 printf("Testing SetSecret with broken key\n");
1125 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1126 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
1127 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
1131 data_blob_free(&enc_key);
1133 ZERO_STRUCT(new_mtime);
1134 ZERO_STRUCT(old_mtime);
1136 /* fetch the secret back again */
1137 r4.in.sec_handle = &sec_handle;
1138 r4.in.new_val = &bufp1;
1139 r4.in.new_mtime = &new_mtime;
1140 r4.in.old_val = NULL;
1141 r4.in.old_mtime = NULL;
1145 printf("Testing QuerySecret\n");
1146 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
1147 if (!NT_STATUS_IS_OK(status)) {
1148 printf("QuerySecret failed - %s\n", nt_errstr(status));
1151 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
1152 printf("No secret buffer returned\n");
1155 blob1.data = r4.out.new_val->buf->data;
1156 blob1.length = r4.out.new_val->buf->size;
1158 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1160 secret2 = sess_decrypt_string(mem_ctx,
1161 &blob1, &session_key);
1163 if (strcmp(secret1, secret2) != 0) {
1164 printf("Returned secret (r4) '%s' doesn't match '%s'\n",
1171 enc_key = sess_encrypt_string(secret3, &session_key);
1173 r5.in.sec_handle = &sec_handle;
1174 r5.in.new_val = &buf1;
1175 r5.in.old_val = NULL;
1176 r5.in.new_val->data = enc_key.data;
1177 r5.in.new_val->length = enc_key.length;
1178 r5.in.new_val->size = enc_key.length;
1182 printf("Testing SetSecret (existing value should move to old)\n");
1184 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
1185 if (!NT_STATUS_IS_OK(status)) {
1186 printf("SetSecret failed - %s\n", nt_errstr(status));
1190 data_blob_free(&enc_key);
1192 ZERO_STRUCT(new_mtime);
1193 ZERO_STRUCT(old_mtime);
1195 /* fetch the secret back again */
1196 r6.in.sec_handle = &sec_handle;
1197 r6.in.new_val = &bufp1;
1198 r6.in.new_mtime = &new_mtime;
1199 r6.in.old_val = &bufp2;
1200 r6.in.old_mtime = &old_mtime;
1205 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
1206 if (!NT_STATUS_IS_OK(status)) {
1207 printf("QuerySecret failed - %s\n", nt_errstr(status));
1212 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
1213 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1214 printf("Both secret buffers and both times not returned\n");
1218 blob1.data = r6.out.new_val->buf->data;
1219 blob1.length = r6.out.new_val->buf->size;
1221 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1223 secret4 = sess_decrypt_string(mem_ctx,
1224 &blob1, &session_key);
1226 if (strcmp(secret3, secret4) != 0) {
1227 printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1231 blob1.data = r6.out.old_val->buf->data;
1232 blob1.length = r6.out.old_val->buf->length;
1234 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1236 secret2 = sess_decrypt_string(mem_ctx,
1237 &blob1, &session_key);
1239 if (strcmp(secret1, secret2) != 0) {
1240 printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1244 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1245 printf("Returned secret (r6-%d) %s must not have same mtime for both secrets: %s != %s\n",
1248 nt_time_string(mem_ctx, *r6.out.old_mtime),
1249 nt_time_string(mem_ctx, *r6.out.new_mtime));
1255 enc_key = sess_encrypt_string(secret5, &session_key);
1257 r7.in.sec_handle = &sec_handle;
1258 r7.in.old_val = &buf1;
1259 r7.in.old_val->data = enc_key.data;
1260 r7.in.old_val->length = enc_key.length;
1261 r7.in.old_val->size = enc_key.length;
1262 r7.in.new_val = NULL;
1264 printf("Testing SetSecret of old Secret only\n");
1266 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
1267 if (!NT_STATUS_IS_OK(status)) {
1268 printf("SetSecret failed - %s\n", nt_errstr(status));
1272 data_blob_free(&enc_key);
1274 /* fetch the secret back again */
1275 r8.in.sec_handle = &sec_handle;
1276 r8.in.new_val = &bufp1;
1277 r8.in.new_mtime = &new_mtime;
1278 r8.in.old_val = &bufp2;
1279 r8.in.old_mtime = &old_mtime;
1284 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
1285 if (!NT_STATUS_IS_OK(status)) {
1286 printf("QuerySecret failed - %s\n", nt_errstr(status));
1289 if (!r8.out.new_val || !r8.out.old_val) {
1290 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
1292 } else if (r8.out.new_val->buf != NULL) {
1293 printf("NEW secret buffer must not be returned after OLD set\n");
1295 } else if (r8.out.old_val->buf == NULL) {
1296 printf("OLD secret buffer was not returned after OLD set\n");
1298 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1299 printf("Both times not returned after OLD set\n");
1302 blob1.data = r8.out.old_val->buf->data;
1303 blob1.length = r8.out.old_val->buf->size;
1305 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1307 secret6 = sess_decrypt_string(mem_ctx,
1308 &blob1, &session_key);
1310 if (strcmp(secret5, secret6) != 0) {
1311 printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1315 if (*r8.out.new_mtime != *r8.out.old_mtime) {
1316 printf("Returned secret (r8) %s did not had same mtime for both secrets: %s != %s\n",
1318 nt_time_string(mem_ctx, *r8.out.old_mtime),
1319 nt_time_string(mem_ctx, *r8.out.new_mtime));
1325 if (!test_Delete(p, mem_ctx, &sec_handle)) {
1329 if (!test_DeleteObject(p, mem_ctx, &sec_handle)) {
1333 d_o.in.handle = &sec_handle2;
1334 d_o.out.handle = &sec_handle2;
1335 status = dcerpc_lsa_DeleteObject(p, mem_ctx, &d_o);
1336 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1337 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1341 printf("Testing OpenSecret of just-deleted secret\n");
1343 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1344 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1345 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1356 static bool test_EnumAccountRights(struct dcerpc_pipe *p,
1357 TALLOC_CTX *mem_ctx,
1358 struct policy_handle *acct_handle,
1359 struct dom_sid *sid)
1362 struct lsa_EnumAccountRights r;
1363 struct lsa_RightSet rights;
1365 printf("Testing EnumAccountRights\n");
1367 r.in.handle = acct_handle;
1369 r.out.rights = &rights;
1371 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1372 if (!NT_STATUS_IS_OK(status)) {
1373 printf("EnumAccountRights of %s failed - %s\n",
1374 dom_sid_string(mem_ctx, sid), nt_errstr(status));
1382 static bool test_QuerySecurity(struct dcerpc_pipe *p,
1383 struct torture_context *tctx,
1384 struct policy_handle *handle,
1385 struct policy_handle *acct_handle)
1388 struct lsa_QuerySecurity r;
1390 if (torture_setting_bool(tctx, "samba4", false)) {
1391 printf("skipping QuerySecurity test against Samba4\n");
1395 printf("Testing QuerySecurity\n");
1397 r.in.handle = acct_handle;
1400 status = dcerpc_lsa_QuerySecurity(p, tctx, &r);
1401 if (!NT_STATUS_IS_OK(status)) {
1402 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1409 static bool test_OpenAccount(struct dcerpc_pipe *p,
1410 TALLOC_CTX *mem_ctx,
1411 struct policy_handle *handle,
1412 struct dom_sid *sid)
1415 struct lsa_OpenAccount r;
1416 struct policy_handle acct_handle;
1418 printf("Testing OpenAccount\n");
1420 r.in.handle = handle;
1422 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1423 r.out.acct_handle = &acct_handle;
1425 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1426 if (!NT_STATUS_IS_OK(status)) {
1427 printf("OpenAccount failed - %s\n", nt_errstr(status));
1431 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1435 if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1442 static bool test_EnumAccounts(struct dcerpc_pipe *p,
1443 TALLOC_CTX *mem_ctx,
1444 struct policy_handle *handle)
1447 struct lsa_EnumAccounts r;
1448 struct lsa_SidArray sids1, sids2;
1449 uint32_t resume_handle = 0;
1453 printf("\ntesting EnumAccounts\n");
1455 r.in.handle = handle;
1456 r.in.resume_handle = &resume_handle;
1457 r.in.num_entries = 100;
1458 r.out.resume_handle = &resume_handle;
1459 r.out.sids = &sids1;
1463 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1464 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1467 if (!NT_STATUS_IS_OK(status)) {
1468 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1472 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1476 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1480 /* Can't test lookupSids3 here, as clearly we must not
1481 * be on schannel, or we would not be able to do the
1484 printf("testing all accounts\n");
1485 for (i=0;i<sids1.num_sids;i++) {
1486 ret &= test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1487 ret &= test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1492 if (sids1.num_sids < 3) {
1496 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1498 r.in.num_entries = 1;
1499 r.out.sids = &sids2;
1501 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1507 if (sids2.num_sids != 1) {
1508 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1515 static bool test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1516 TALLOC_CTX *mem_ctx,
1517 struct policy_handle *handle,
1518 struct lsa_String *priv_name)
1520 struct lsa_LookupPrivDisplayName r;
1522 /* produce a reasonable range of language output without screwing up
1524 uint16_t language_id = (random() % 4) + 0x409;
1526 printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1528 r.in.handle = handle;
1529 r.in.name = priv_name;
1530 r.in.language_id = &language_id;
1531 r.out.language_id = &language_id;
1534 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1535 if (!NT_STATUS_IS_OK(status)) {
1536 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1539 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
1540 priv_name->string, r.out.disp_name->string,
1541 *r.in.language_id, *r.out.language_id);
1546 static bool test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1547 TALLOC_CTX *mem_ctx,
1548 struct policy_handle *handle,
1549 struct lsa_String *priv_name)
1551 struct lsa_EnumAccountsWithUserRight r;
1552 struct lsa_SidArray sids;
1557 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1559 r.in.handle = handle;
1560 r.in.name = priv_name;
1563 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1565 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1566 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1570 if (!NT_STATUS_IS_OK(status)) {
1571 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1579 static bool test_EnumPrivs(struct dcerpc_pipe *p,
1580 TALLOC_CTX *mem_ctx,
1581 struct policy_handle *handle)
1584 struct lsa_EnumPrivs r;
1585 struct lsa_PrivArray privs1;
1586 uint32_t resume_handle = 0;
1590 printf("\ntesting EnumPrivs\n");
1592 r.in.handle = handle;
1593 r.in.resume_handle = &resume_handle;
1594 r.in.max_count = 100;
1595 r.out.resume_handle = &resume_handle;
1596 r.out.privs = &privs1;
1599 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1600 if (!NT_STATUS_IS_OK(status)) {
1601 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1605 for (i = 0; i< privs1.count; i++) {
1606 test_LookupPrivDisplayName(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1607 test_LookupPrivValue(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1608 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
1616 static bool test_QueryForestTrustInformation(struct dcerpc_pipe *p,
1617 struct torture_context *tctx,
1618 struct policy_handle *handle,
1619 const char *trusted_domain_name)
1622 struct lsa_lsaRQueryForestTrustInformation r;
1624 struct lsa_String string;
1625 struct lsa_ForestTrustInformation info, *info_ptr;
1627 printf("\nTesting lsaRQueryForestTrustInformation\n");
1629 if (torture_setting_bool(tctx, "samba4", false)) {
1630 printf("skipping QueryForestTrustInformation against Samba4\n");
1634 ZERO_STRUCT(string);
1636 if (trusted_domain_name) {
1637 init_lsa_String(&string, trusted_domain_name);
1642 r.in.handle = handle;
1643 r.in.trusted_domain_name = &string;
1645 r.out.forest_trust_info = &info_ptr;
1647 status = dcerpc_lsa_lsaRQueryForestTrustInformation(p, tctx, &r);
1649 if (!NT_STATUS_IS_OK(status)) {
1650 printf("lsaRQueryForestTrustInformation failed - %s\n", nt_errstr(status));
1657 static bool test_query_each_TrustDomEx(struct dcerpc_pipe *p,
1658 TALLOC_CTX *mem_ctx,
1659 struct policy_handle *handle,
1660 struct lsa_DomainListEx *domains)
1665 for (i=0; i< domains->count; i++) {
1667 if (domains->domains[i].trust_attributes & NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1668 ret &= test_QueryForestTrustInformation(p, mem_ctx, handle,
1669 domains->domains[i].domain_name.string);
1676 static bool test_query_each_TrustDom(struct dcerpc_pipe *p,
1677 TALLOC_CTX *mem_ctx,
1678 struct policy_handle *handle,
1679 struct lsa_DomainList *domains)
1685 printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1686 for (i=0; i< domains->count; i++) {
1687 struct lsa_OpenTrustedDomain trust;
1688 struct lsa_OpenTrustedDomainByName trust_by_name;
1689 struct policy_handle trustdom_handle;
1690 struct policy_handle handle2;
1692 struct lsa_CloseTrustedDomainEx c_trust;
1693 int levels [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
1694 int ok[] = {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1};
1696 if (domains->domains[i].sid) {
1697 trust.in.handle = handle;
1698 trust.in.sid = domains->domains[i].sid;
1699 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1700 trust.out.trustdom_handle = &trustdom_handle;
1702 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1704 if (!NT_STATUS_IS_OK(status)) {
1705 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1709 c.in.handle = &trustdom_handle;
1710 c.out.handle = &handle2;
1712 c_trust.in.handle = &trustdom_handle;
1713 c_trust.out.handle = &handle2;
1715 for (j=0; j < ARRAY_SIZE(levels); j++) {
1716 struct lsa_QueryTrustedDomainInfo q;
1717 union lsa_TrustedDomainInfo info;
1718 q.in.trustdom_handle = &trustdom_handle;
1719 q.in.level = levels[j];
1721 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1722 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1723 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1724 levels[j], nt_errstr(status));
1726 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1727 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1728 levels[j], nt_errstr(status));
1733 status = dcerpc_lsa_CloseTrustedDomainEx(p, mem_ctx, &c_trust);
1734 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1735 printf("Expected CloseTrustedDomainEx to return NT_STATUS_NOT_IMPLEMENTED, instead - %s\n", nt_errstr(status));
1739 c.in.handle = &trustdom_handle;
1740 c.out.handle = &handle2;
1742 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1743 if (!NT_STATUS_IS_OK(status)) {
1744 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1748 for (j=0; j < ARRAY_SIZE(levels); j++) {
1749 struct lsa_QueryTrustedDomainInfoBySid q;
1750 union lsa_TrustedDomainInfo info;
1752 if (!domains->domains[i].sid) {
1756 q.in.handle = handle;
1757 q.in.dom_sid = domains->domains[i].sid;
1758 q.in.level = levels[j];
1760 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1761 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1762 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n",
1763 levels[j], nt_errstr(status));
1765 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1766 printf("QueryTrustedDomainInfoBySid level %d unexpectedly succeeded - %s\n",
1767 levels[j], nt_errstr(status));
1773 trust_by_name.in.handle = handle;
1774 trust_by_name.in.name.string = domains->domains[i].name.string;
1775 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1776 trust_by_name.out.trustdom_handle = &trustdom_handle;
1778 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1780 if (!NT_STATUS_IS_OK(status)) {
1781 printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1785 for (j=0; j < ARRAY_SIZE(levels); j++) {
1786 struct lsa_QueryTrustedDomainInfo q;
1787 union lsa_TrustedDomainInfo info;
1788 q.in.trustdom_handle = &trustdom_handle;
1789 q.in.level = levels[j];
1791 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1792 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1793 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1794 levels[j], nt_errstr(status));
1796 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1797 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1798 levels[j], nt_errstr(status));
1803 c.in.handle = &trustdom_handle;
1804 c.out.handle = &handle2;
1806 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1807 if (!NT_STATUS_IS_OK(status)) {
1808 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1812 for (j=0; j < ARRAY_SIZE(levels); j++) {
1813 struct lsa_QueryTrustedDomainInfoByName q;
1814 union lsa_TrustedDomainInfo info;
1815 q.in.handle = handle;
1816 q.in.trusted_domain.string = domains->domains[i].name.string;
1817 q.in.level = levels[j];
1819 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1820 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1821 printf("QueryTrustedDomainInfoByName level %d failed - %s\n",
1822 levels[j], nt_errstr(status));
1824 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1825 printf("QueryTrustedDomainInfoByName level %d unexpectedly succeeded - %s\n",
1826 levels[j], nt_errstr(status));
1834 static bool test_EnumTrustDom(struct dcerpc_pipe *p,
1835 TALLOC_CTX *mem_ctx,
1836 struct policy_handle *handle)
1838 struct lsa_EnumTrustDom r;
1839 struct lsa_EnumTrustedDomainsEx r_ex;
1840 NTSTATUS enum_status;
1841 uint32_t resume_handle = 0;
1842 struct lsa_DomainList domains;
1843 struct lsa_DomainListEx domains_ex;
1846 printf("\nTesting EnumTrustDom\n");
1848 r.in.handle = handle;
1849 r.in.resume_handle = &resume_handle;
1851 r.out.domains = &domains;
1852 r.out.resume_handle = &resume_handle;
1854 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1856 if (NT_STATUS_IS_OK(enum_status)) {
1857 if (domains.count == 0) {
1858 printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1861 } else if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1862 printf("EnumTrustDom of zero size failed - %s\n", nt_errstr(enum_status));
1867 r.in.handle = handle;
1868 r.in.resume_handle = &resume_handle;
1869 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
1870 r.out.domains = &domains;
1871 r.out.resume_handle = &resume_handle;
1873 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1875 /* NO_MORE_ENTRIES is allowed */
1876 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1878 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1879 /* Windows 2003 gets this off by one on the first run */
1880 if (r.out.domains->count < 3 || r.out.domains->count > 4) {
1881 printf("EnumTrustDom didn't fill the buffer we "
1882 "asked it to (got %d, expected %d / %d == %d entries)\n",
1883 r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3,
1884 LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
1887 } else if (!NT_STATUS_IS_OK(enum_status)) {
1888 printf("EnumTrustDom failed - %s\n", nt_errstr(enum_status));
1892 if (domains.count == 0) {
1893 printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1897 ret &= test_query_each_TrustDom(p, mem_ctx, handle, &domains);
1899 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1901 printf("\nTesting EnumTrustedDomainsEx\n");
1903 r_ex.in.handle = handle;
1904 r_ex.in.resume_handle = &resume_handle;
1905 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1906 r_ex.out.domains = &domains_ex;
1907 r_ex.out.resume_handle = &resume_handle;
1909 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1911 if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1912 printf("EnumTrustedDomainEx of zero size failed - %s\n", nt_errstr(enum_status));
1918 r_ex.in.handle = handle;
1919 r_ex.in.resume_handle = &resume_handle;
1920 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1921 r_ex.out.domains = &domains_ex;
1922 r_ex.out.resume_handle = &resume_handle;
1924 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1926 /* NO_MORE_ENTRIES is allowed */
1927 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1929 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1930 /* Windows 2003 gets this off by one on the first run */
1931 if (r_ex.out.domains->count < 3 || r_ex.out.domains->count > 4) {
1932 printf("EnumTrustDom didn't fill the buffer we "
1933 "asked it to (got %d, expected %d / %d == %d entries)\n",
1934 r_ex.out.domains->count,
1936 LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER,
1937 r_ex.in.max_size / LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER);
1939 } else if (!NT_STATUS_IS_OK(enum_status)) {
1940 printf("EnumTrustedDomainEx failed - %s\n", nt_errstr(enum_status));
1944 if (domains_ex.count == 0) {
1945 printf("EnumTrustDomainEx failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1949 ret &= test_query_each_TrustDomEx(p, mem_ctx, handle, &domains_ex);
1951 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1956 static bool test_CreateTrustedDomain(struct dcerpc_pipe *p,
1957 TALLOC_CTX *mem_ctx,
1958 struct policy_handle *handle)
1962 struct lsa_CreateTrustedDomain r;
1963 struct lsa_DomainInfo trustinfo;
1964 struct dom_sid *domsid[12];
1965 struct policy_handle trustdom_handle[12];
1966 struct lsa_QueryTrustedDomainInfo q;
1969 printf("Testing CreateTrustedDomain for 12 domains\n");
1971 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1975 for (i=0; i< 12; i++) {
1976 char *trust_name = talloc_asprintf(mem_ctx, "torturedom%02d", i);
1977 char *trust_sid = talloc_asprintf(mem_ctx, "S-1-5-21-97398-379795-100%02d", i);
1979 domsid[i] = dom_sid_parse_talloc(mem_ctx, trust_sid);
1981 trustinfo.sid = domsid[i];
1982 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
1984 r.in.handle = handle;
1985 r.in.info = &trustinfo;
1986 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1987 r.out.trustdom_handle = &trustdom_handle[i];
1989 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1990 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1991 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1992 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1994 if (!NT_STATUS_IS_OK(status)) {
1995 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1999 q.in.trustdom_handle = &trustdom_handle[i];
2000 q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
2001 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
2002 if (!NT_STATUS_IS_OK(status)) {
2003 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
2005 } else if (!q.out.info) {
2008 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
2009 printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
2010 q.out.info->name.netbios_name.string, trustinfo.name.string);
2017 /* now that we have some domains to look over, we can test the enum calls */
2018 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
2022 for (i=0; i<12; i++) {
2023 if (!test_DeleteTrustedDomainBySid(p, mem_ctx, handle, domsid[i])) {
2031 static bool test_QueryDomainInfoPolicy(struct dcerpc_pipe *p,
2032 struct torture_context *tctx,
2033 struct policy_handle *handle)
2035 struct lsa_QueryDomainInformationPolicy r;
2039 if (torture_setting_bool(tctx, "samba4", false)) {
2040 printf("skipping QueryDomainInformationPolicy test against Samba4\n");
2044 printf("\nTesting QueryDomainInformationPolicy\n");
2047 r.in.handle = handle;
2050 printf("\ntrying QueryDomainInformationPolicy level %d\n", i);
2052 status = dcerpc_lsa_QueryDomainInformationPolicy(p, tctx, &r);
2054 if (!NT_STATUS_IS_OK(status)) {
2055 printf("QueryDomainInformationPolicy failed - %s\n", nt_errstr(status));
2065 static bool test_QueryInfoPolicy(struct dcerpc_pipe *p,
2066 struct torture_context *tctx,
2067 struct policy_handle *handle)
2069 struct lsa_QueryInfoPolicy r;
2073 printf("\nTesting QueryInfoPolicy\n");
2075 for (i=1;i<13;i++) {
2076 r.in.handle = handle;
2079 printf("\ntrying QueryInfoPolicy level %d\n", i);
2081 status = dcerpc_lsa_QueryInfoPolicy(p, tctx, &r);
2084 case LSA_POLICY_INFO_DB:
2085 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2086 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2087 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2088 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2092 case LSA_POLICY_INFO_DOMAIN:
2093 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2094 case LSA_POLICY_INFO_DNS:
2095 if (!NT_STATUS_IS_OK(status)) {
2096 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2101 if (torture_setting_bool(tctx, "samba4", false)) {
2102 /* Other levels not implemented yet */
2103 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2104 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2107 } else if (!NT_STATUS_IS_OK(status)) {
2108 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2114 if (NT_STATUS_IS_OK(status) && i == LSA_POLICY_INFO_DNS) {
2115 /* Let's look up some of these names */
2117 struct lsa_TransNameArray tnames;
2119 tnames.names = talloc_zero_array(tctx, struct lsa_TranslatedName, tnames.count);
2120 tnames.names[0].name.string = r.out.info->dns.name.string;
2121 tnames.names[0].sid_type = SID_NAME_DOMAIN;
2122 tnames.names[1].name.string = r.out.info->dns.dns_domain.string;
2123 tnames.names[1].sid_type = SID_NAME_DOMAIN;
2124 tnames.names[2].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.name.string);
2125 tnames.names[2].sid_type = SID_NAME_DOMAIN;
2126 tnames.names[3].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.dns_domain.string);
2127 tnames.names[3].sid_type = SID_NAME_DOMAIN;
2128 tnames.names[4].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.name.string);
2129 tnames.names[4].sid_type = SID_NAME_USER;
2130 tnames.names[5].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.name.string);
2131 tnames.names[5].sid_type = SID_NAME_USER;
2132 tnames.names[6].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.dns_domain.string);
2133 tnames.names[6].sid_type = SID_NAME_USER;
2134 tnames.names[7].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.dns_domain.string);
2135 tnames.names[7].sid_type = SID_NAME_USER;
2136 tnames.names[8].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.name.string);
2137 tnames.names[8].sid_type = SID_NAME_USER;
2138 tnames.names[9].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.dns_domain.string);
2139 tnames.names[9].sid_type = SID_NAME_USER;
2140 tnames.names[10].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.name.string);
2141 tnames.names[10].sid_type = SID_NAME_USER;
2142 tnames.names[11].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.dns_domain.string);
2143 tnames.names[11].sid_type = SID_NAME_USER;
2144 tnames.names[12].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.name.string);
2145 tnames.names[12].sid_type = SID_NAME_USER;
2146 tnames.names[13].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.dns_domain.string);
2147 tnames.names[13].sid_type = SID_NAME_USER;
2148 ret &= test_LookupNames(p, tctx, handle, &tnames);
2156 static bool test_QueryInfoPolicy2(struct dcerpc_pipe *p,
2157 struct torture_context *tctx,
2158 struct policy_handle *handle)
2160 struct lsa_QueryInfoPolicy2 r;
2164 printf("\nTesting QueryInfoPolicy2\n");
2165 for (i=1;i<13;i++) {
2166 r.in.handle = handle;
2169 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
2171 status = dcerpc_lsa_QueryInfoPolicy2(p, tctx, &r);
2174 case LSA_POLICY_INFO_DB:
2175 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2176 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2177 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2178 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2182 case LSA_POLICY_INFO_DOMAIN:
2183 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2184 case LSA_POLICY_INFO_DNS:
2185 if (!NT_STATUS_IS_OK(status)) {
2186 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2191 if (torture_setting_bool(tctx, "samba4", false)) {
2192 /* Other levels not implemented yet */
2193 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2194 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2197 } else if (!NT_STATUS_IS_OK(status)) {
2198 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2208 static bool test_GetUserName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
2210 struct lsa_GetUserName r;
2213 struct lsa_StringPointer authority_name_p;
2215 printf("\nTesting GetUserName\n");
2217 r.in.system_name = "\\";
2218 r.in.account_name = NULL;
2219 r.in.authority_name = &authority_name_p;
2220 authority_name_p.string = NULL;
2222 status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
2224 if (!NT_STATUS_IS_OK(status)) {
2225 printf("GetUserName failed - %s\n", nt_errstr(status));
2232 bool test_lsa_Close(struct dcerpc_pipe *p,
2233 TALLOC_CTX *mem_ctx,
2234 struct policy_handle *handle)
2238 struct policy_handle handle2;
2240 printf("\ntesting Close\n");
2242 r.in.handle = handle;
2243 r.out.handle = &handle2;
2245 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2246 if (!NT_STATUS_IS_OK(status)) {
2247 printf("Close failed - %s\n", nt_errstr(status));
2251 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2252 /* its really a fault - we need a status code for rpc fault */
2253 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
2254 printf("Close failed - %s\n", nt_errstr(status));
2263 bool torture_rpc_lsa(struct torture_context *tctx)
2266 struct dcerpc_pipe *p;
2268 struct policy_handle *handle;
2269 struct test_join *join = NULL;
2270 struct cli_credentials *machine_creds;
2272 status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
2273 if (!NT_STATUS_IS_OK(status)) {
2277 if (!test_OpenPolicy(p, tctx)) {
2281 if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2286 join = torture_join_domain(tctx, TEST_MACHINENAME, ACB_WSTRUST, &machine_creds);
2291 if (!test_LookupNames_wellknown(p, tctx, handle)) {
2295 if (!test_LookupNames_bogus(p, tctx, handle)) {
2299 if (!test_LookupSids_async(p, tctx, handle)) {
2303 if (!test_QueryDomainInfoPolicy(p, tctx, handle)) {
2307 if (!test_CreateAccount(p, tctx, handle)) {
2311 if (!test_CreateSecret(p, tctx, handle)) {
2315 if (!test_CreateTrustedDomain(p, tctx, handle)) {
2319 if (!test_EnumAccounts(p, tctx, handle)) {
2323 if (!test_EnumPrivs(p, tctx, handle)) {
2327 if (!test_QueryInfoPolicy(p, tctx, handle)) {
2331 if (!test_QueryInfoPolicy2(p, tctx, handle)) {
2335 if (!test_Delete(p, tctx, handle)) {
2339 if (!test_many_LookupSids(p, tctx, handle)) {
2343 if (!test_lsa_Close(p, tctx, handle)) {
2347 torture_leave_domain(tctx, join);
2350 if (!test_many_LookupSids(p, tctx, handle)) {
2355 if (!test_GetUserName(p, tctx)) {
2362 bool torture_rpc_lsa_get_user(struct torture_context *torture)
2365 struct dcerpc_pipe *p;
2366 TALLOC_CTX *mem_ctx;
2369 mem_ctx = talloc_init("torture_rpc_lsa_get_user");
2371 status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
2372 if (!NT_STATUS_IS_OK(status)) {
2373 talloc_free(mem_ctx);
2377 if (!test_GetUserName(p, mem_ctx)) {
2381 talloc_free(mem_ctx);