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 *handle,
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)) {
806 struct lsa_LookupPrivName r_name;
808 r_name.in.handle = handle;
809 r_name.in.luid = luid;
811 status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r_name);
812 if (!NT_STATUS_IS_OK(status)) {
813 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
816 /* Windows 2008 does not allow this to be removed */
817 if (strcmp("SeAuditPrivilege", r_name.out.name->string) == 0) {
821 printf("RemovePrivilegesFromAccount failed to remove %s - %s\n",
822 r_name.out.name->string,
830 static bool test_AddPrivilegesToAccount(struct dcerpc_pipe *p,
832 struct policy_handle *acct_handle,
833 struct lsa_LUID *luid)
836 struct lsa_AddPrivilegesToAccount r;
837 struct lsa_PrivilegeSet privs;
840 printf("Testing AddPrivilegesToAccount\n");
842 r.in.handle = acct_handle;
847 privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
848 privs.set[0].luid = *luid;
849 privs.set[0].attribute = 0;
851 status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
852 if (!NT_STATUS_IS_OK(status)) {
853 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
860 static bool test_EnumPrivsAccount(struct dcerpc_pipe *p,
862 struct policy_handle *handle,
863 struct policy_handle *acct_handle)
866 struct lsa_EnumPrivsAccount r;
869 printf("Testing EnumPrivsAccount\n");
871 r.in.handle = acct_handle;
873 status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
874 if (!NT_STATUS_IS_OK(status)) {
875 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
879 if (r.out.privs && r.out.privs->count > 0) {
881 for (i=0;i<r.out.privs->count;i++) {
882 test_LookupPrivName(p, mem_ctx, handle,
883 &r.out.privs->set[i].luid);
886 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, handle, acct_handle,
887 &r.out.privs->set[0].luid);
888 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle,
889 &r.out.privs->set[0].luid);
895 static bool test_Delete(struct dcerpc_pipe *p,
897 struct policy_handle *handle)
902 printf("testing Delete\n");
904 r.in.handle = handle;
905 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
906 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
907 printf("Delete should have failed NT_STATUS_NOT_SUPPORTED - %s\n", nt_errstr(status));
914 static bool test_DeleteObject(struct dcerpc_pipe *p,
916 struct policy_handle *handle)
919 struct lsa_DeleteObject r;
921 printf("testing DeleteObject\n");
923 r.in.handle = handle;
924 r.out.handle = handle;
925 status = dcerpc_lsa_DeleteObject(p, mem_ctx, &r);
926 if (!NT_STATUS_IS_OK(status)) {
927 printf("Delete failed - %s\n", nt_errstr(status));
935 static bool test_CreateAccount(struct dcerpc_pipe *p,
937 struct policy_handle *handle)
940 struct lsa_CreateAccount r;
941 struct dom_sid2 *newsid;
942 struct policy_handle acct_handle;
944 newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
946 printf("Testing CreateAccount\n");
948 r.in.handle = handle;
950 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
951 r.out.acct_handle = &acct_handle;
953 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
954 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
955 struct lsa_OpenAccount r_o;
956 r_o.in.handle = handle;
958 r_o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
959 r_o.out.acct_handle = &acct_handle;
961 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r_o);
962 if (!NT_STATUS_IS_OK(status)) {
963 printf("OpenAccount failed - %s\n", nt_errstr(status));
966 } else if (!NT_STATUS_IS_OK(status)) {
967 printf("CreateAccount failed - %s\n", nt_errstr(status));
971 if (!test_Delete(p, mem_ctx, &acct_handle)) {
975 if (!test_DeleteObject(p, mem_ctx, &acct_handle)) {
982 static bool test_DeleteTrustedDomain(struct dcerpc_pipe *p,
984 struct policy_handle *handle,
985 struct lsa_StringLarge name)
988 struct lsa_OpenTrustedDomainByName r;
989 struct policy_handle trustdom_handle;
991 r.in.handle = handle;
992 r.in.name.string = name.string;
993 r.in.access_mask = SEC_STD_DELETE;
994 r.out.trustdom_handle = &trustdom_handle;
996 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
997 if (!NT_STATUS_IS_OK(status)) {
998 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1002 if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
1006 if (!test_DeleteObject(p, mem_ctx, &trustdom_handle)) {
1013 static bool test_DeleteTrustedDomainBySid(struct dcerpc_pipe *p,
1014 TALLOC_CTX *mem_ctx,
1015 struct policy_handle *handle,
1016 struct dom_sid *sid)
1019 struct lsa_DeleteTrustedDomain r;
1021 r.in.handle = handle;
1024 status = dcerpc_lsa_DeleteTrustedDomain(p, mem_ctx, &r);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 printf("lsa_DeleteTrustedDomain failed - %s\n", nt_errstr(status));
1034 static bool test_CreateSecret(struct dcerpc_pipe *p,
1035 TALLOC_CTX *mem_ctx,
1036 struct policy_handle *handle)
1039 struct lsa_CreateSecret r;
1040 struct lsa_OpenSecret r2;
1041 struct lsa_SetSecret r3;
1042 struct lsa_QuerySecret r4;
1043 struct lsa_SetSecret r5;
1044 struct lsa_QuerySecret r6;
1045 struct lsa_SetSecret r7;
1046 struct lsa_QuerySecret r8;
1047 struct policy_handle sec_handle, sec_handle2, sec_handle3;
1048 struct lsa_DeleteObject d_o;
1049 struct lsa_DATA_BUF buf1;
1050 struct lsa_DATA_BUF_PTR bufp1;
1051 struct lsa_DATA_BUF_PTR bufp2;
1054 DATA_BLOB session_key;
1055 NTTIME old_mtime, new_mtime;
1056 DATA_BLOB blob1, blob2;
1057 const char *secret1 = "abcdef12345699qwerty";
1059 const char *secret3 = "ABCDEF12345699QWERTY";
1061 const char *secret5 = "NEW-SAMBA4-SECRET";
1065 const int LOCAL = 0;
1066 const int GLOBAL = 1;
1068 secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
1069 secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
1071 for (i=0; i< 2; i++) {
1072 printf("Testing CreateSecret of %s\n", secname[i]);
1074 init_lsa_String(&r.in.name, secname[i]);
1076 r.in.handle = handle;
1077 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1078 r.out.sec_handle = &sec_handle;
1080 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 printf("CreateSecret failed - %s\n", nt_errstr(status));
1086 r.in.handle = handle;
1087 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1088 r.out.sec_handle = &sec_handle3;
1090 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1091 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1092 printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
1096 r2.in.handle = handle;
1097 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1098 r2.in.name = r.in.name;
1099 r2.out.sec_handle = &sec_handle2;
1101 printf("Testing OpenSecret\n");
1103 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 printf("OpenSecret failed - %s\n", nt_errstr(status));
1109 status = dcerpc_fetch_session_key(p, &session_key);
1110 if (!NT_STATUS_IS_OK(status)) {
1111 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
1115 enc_key = sess_encrypt_string(secret1, &session_key);
1117 r3.in.sec_handle = &sec_handle;
1118 r3.in.new_val = &buf1;
1119 r3.in.old_val = NULL;
1120 r3.in.new_val->data = enc_key.data;
1121 r3.in.new_val->length = enc_key.length;
1122 r3.in.new_val->size = enc_key.length;
1124 printf("Testing SetSecret\n");
1126 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 printf("SetSecret failed - %s\n", nt_errstr(status));
1132 r3.in.sec_handle = &sec_handle;
1133 r3.in.new_val = &buf1;
1134 r3.in.old_val = NULL;
1135 r3.in.new_val->data = enc_key.data;
1136 r3.in.new_val->length = enc_key.length;
1137 r3.in.new_val->size = enc_key.length;
1139 /* break the encrypted data */
1142 printf("Testing SetSecret with broken key\n");
1144 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1145 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
1146 printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
1150 data_blob_free(&enc_key);
1152 ZERO_STRUCT(new_mtime);
1153 ZERO_STRUCT(old_mtime);
1155 /* fetch the secret back again */
1156 r4.in.sec_handle = &sec_handle;
1157 r4.in.new_val = &bufp1;
1158 r4.in.new_mtime = &new_mtime;
1159 r4.in.old_val = NULL;
1160 r4.in.old_mtime = NULL;
1164 printf("Testing QuerySecret\n");
1165 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
1166 if (!NT_STATUS_IS_OK(status)) {
1167 printf("QuerySecret failed - %s\n", nt_errstr(status));
1170 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
1171 printf("No secret buffer returned\n");
1174 blob1.data = r4.out.new_val->buf->data;
1175 blob1.length = r4.out.new_val->buf->size;
1177 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1179 secret2 = sess_decrypt_string(mem_ctx,
1180 &blob1, &session_key);
1182 if (strcmp(secret1, secret2) != 0) {
1183 printf("Returned secret (r4) '%s' doesn't match '%s'\n",
1190 enc_key = sess_encrypt_string(secret3, &session_key);
1192 r5.in.sec_handle = &sec_handle;
1193 r5.in.new_val = &buf1;
1194 r5.in.old_val = NULL;
1195 r5.in.new_val->data = enc_key.data;
1196 r5.in.new_val->length = enc_key.length;
1197 r5.in.new_val->size = enc_key.length;
1201 printf("Testing SetSecret (existing value should move to old)\n");
1203 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 printf("SetSecret failed - %s\n", nt_errstr(status));
1209 data_blob_free(&enc_key);
1211 ZERO_STRUCT(new_mtime);
1212 ZERO_STRUCT(old_mtime);
1214 /* fetch the secret back again */
1215 r6.in.sec_handle = &sec_handle;
1216 r6.in.new_val = &bufp1;
1217 r6.in.new_mtime = &new_mtime;
1218 r6.in.old_val = &bufp2;
1219 r6.in.old_mtime = &old_mtime;
1224 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
1225 if (!NT_STATUS_IS_OK(status)) {
1226 printf("QuerySecret failed - %s\n", nt_errstr(status));
1231 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
1232 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1233 printf("Both secret buffers and both times not returned\n");
1237 blob1.data = r6.out.new_val->buf->data;
1238 blob1.length = r6.out.new_val->buf->size;
1240 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1242 secret4 = sess_decrypt_string(mem_ctx,
1243 &blob1, &session_key);
1245 if (strcmp(secret3, secret4) != 0) {
1246 printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1250 blob1.data = r6.out.old_val->buf->data;
1251 blob1.length = r6.out.old_val->buf->length;
1253 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1255 secret2 = sess_decrypt_string(mem_ctx,
1256 &blob1, &session_key);
1258 if (strcmp(secret1, secret2) != 0) {
1259 printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1263 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1264 printf("Returned secret (r6-%d) %s must not have same mtime for both secrets: %s != %s\n",
1267 nt_time_string(mem_ctx, *r6.out.old_mtime),
1268 nt_time_string(mem_ctx, *r6.out.new_mtime));
1274 enc_key = sess_encrypt_string(secret5, &session_key);
1276 r7.in.sec_handle = &sec_handle;
1277 r7.in.old_val = &buf1;
1278 r7.in.old_val->data = enc_key.data;
1279 r7.in.old_val->length = enc_key.length;
1280 r7.in.old_val->size = enc_key.length;
1281 r7.in.new_val = NULL;
1283 printf("Testing SetSecret of old Secret only\n");
1285 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
1286 if (!NT_STATUS_IS_OK(status)) {
1287 printf("SetSecret failed - %s\n", nt_errstr(status));
1291 data_blob_free(&enc_key);
1293 /* fetch the secret back again */
1294 r8.in.sec_handle = &sec_handle;
1295 r8.in.new_val = &bufp1;
1296 r8.in.new_mtime = &new_mtime;
1297 r8.in.old_val = &bufp2;
1298 r8.in.old_mtime = &old_mtime;
1303 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 printf("QuerySecret failed - %s\n", nt_errstr(status));
1308 if (!r8.out.new_val || !r8.out.old_val) {
1309 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
1311 } else if (r8.out.new_val->buf != NULL) {
1312 printf("NEW secret buffer must not be returned after OLD set\n");
1314 } else if (r8.out.old_val->buf == NULL) {
1315 printf("OLD secret buffer was not returned after OLD set\n");
1317 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1318 printf("Both times not returned after OLD set\n");
1321 blob1.data = r8.out.old_val->buf->data;
1322 blob1.length = r8.out.old_val->buf->size;
1324 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1326 secret6 = sess_decrypt_string(mem_ctx,
1327 &blob1, &session_key);
1329 if (strcmp(secret5, secret6) != 0) {
1330 printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1334 if (*r8.out.new_mtime != *r8.out.old_mtime) {
1335 printf("Returned secret (r8) %s did not had same mtime for both secrets: %s != %s\n",
1337 nt_time_string(mem_ctx, *r8.out.old_mtime),
1338 nt_time_string(mem_ctx, *r8.out.new_mtime));
1344 if (!test_Delete(p, mem_ctx, &sec_handle)) {
1348 if (!test_DeleteObject(p, mem_ctx, &sec_handle)) {
1352 d_o.in.handle = &sec_handle2;
1353 d_o.out.handle = &sec_handle2;
1354 status = dcerpc_lsa_DeleteObject(p, mem_ctx, &d_o);
1355 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1356 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1360 printf("Testing OpenSecret of just-deleted secret\n");
1362 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1363 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1364 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1375 static bool test_EnumAccountRights(struct dcerpc_pipe *p,
1376 TALLOC_CTX *mem_ctx,
1377 struct policy_handle *acct_handle,
1378 struct dom_sid *sid)
1381 struct lsa_EnumAccountRights r;
1382 struct lsa_RightSet rights;
1384 printf("Testing EnumAccountRights\n");
1386 r.in.handle = acct_handle;
1388 r.out.rights = &rights;
1390 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1391 if (!NT_STATUS_IS_OK(status)) {
1392 printf("EnumAccountRights of %s failed - %s\n",
1393 dom_sid_string(mem_ctx, sid), nt_errstr(status));
1401 static bool test_QuerySecurity(struct dcerpc_pipe *p,
1402 struct torture_context *tctx,
1403 struct policy_handle *handle,
1404 struct policy_handle *acct_handle)
1407 struct lsa_QuerySecurity r;
1409 if (torture_setting_bool(tctx, "samba4", false)) {
1410 printf("skipping QuerySecurity test against Samba4\n");
1414 printf("Testing QuerySecurity\n");
1416 r.in.handle = acct_handle;
1419 status = dcerpc_lsa_QuerySecurity(p, tctx, &r);
1420 if (!NT_STATUS_IS_OK(status)) {
1421 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1428 static bool test_OpenAccount(struct dcerpc_pipe *p,
1429 TALLOC_CTX *mem_ctx,
1430 struct policy_handle *handle,
1431 struct dom_sid *sid)
1434 struct lsa_OpenAccount r;
1435 struct policy_handle acct_handle;
1437 printf("Testing OpenAccount\n");
1439 r.in.handle = handle;
1441 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1442 r.out.acct_handle = &acct_handle;
1444 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1445 if (!NT_STATUS_IS_OK(status)) {
1446 printf("OpenAccount failed - %s\n", nt_errstr(status));
1450 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1454 if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1461 static bool test_EnumAccounts(struct dcerpc_pipe *p,
1462 TALLOC_CTX *mem_ctx,
1463 struct policy_handle *handle)
1466 struct lsa_EnumAccounts r;
1467 struct lsa_SidArray sids1, sids2;
1468 uint32_t resume_handle = 0;
1472 printf("\ntesting EnumAccounts\n");
1474 r.in.handle = handle;
1475 r.in.resume_handle = &resume_handle;
1476 r.in.num_entries = 100;
1477 r.out.resume_handle = &resume_handle;
1478 r.out.sids = &sids1;
1482 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1483 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1486 if (!NT_STATUS_IS_OK(status)) {
1487 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1491 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1495 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1499 /* Can't test lookupSids3 here, as clearly we must not
1500 * be on schannel, or we would not be able to do the
1503 printf("testing all accounts\n");
1504 for (i=0;i<sids1.num_sids;i++) {
1505 ret &= test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1506 ret &= test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1511 if (sids1.num_sids < 3) {
1515 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1517 r.in.num_entries = 1;
1518 r.out.sids = &sids2;
1520 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1521 if (!NT_STATUS_IS_OK(status)) {
1522 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1526 if (sids2.num_sids != 1) {
1527 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1534 static bool test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1535 TALLOC_CTX *mem_ctx,
1536 struct policy_handle *handle,
1537 struct lsa_String *priv_name)
1539 struct lsa_LookupPrivDisplayName r;
1541 /* produce a reasonable range of language output without screwing up
1543 uint16_t language_id = (random() % 4) + 0x409;
1545 printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1547 r.in.handle = handle;
1548 r.in.name = priv_name;
1549 r.in.language_id = &language_id;
1550 r.out.language_id = &language_id;
1553 status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1554 if (!NT_STATUS_IS_OK(status)) {
1555 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1558 printf("%s -> \"%s\" (language 0x%x/0x%x)\n",
1559 priv_name->string, r.out.disp_name->string,
1560 *r.in.language_id, *r.out.language_id);
1565 static bool test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1566 TALLOC_CTX *mem_ctx,
1567 struct policy_handle *handle,
1568 struct lsa_String *priv_name)
1570 struct lsa_EnumAccountsWithUserRight r;
1571 struct lsa_SidArray sids;
1576 printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1578 r.in.handle = handle;
1579 r.in.name = priv_name;
1582 status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1584 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1585 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1589 if (!NT_STATUS_IS_OK(status)) {
1590 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1598 static bool test_EnumPrivs(struct dcerpc_pipe *p,
1599 TALLOC_CTX *mem_ctx,
1600 struct policy_handle *handle)
1603 struct lsa_EnumPrivs r;
1604 struct lsa_PrivArray privs1;
1605 uint32_t resume_handle = 0;
1609 printf("\ntesting EnumPrivs\n");
1611 r.in.handle = handle;
1612 r.in.resume_handle = &resume_handle;
1613 r.in.max_count = 100;
1614 r.out.resume_handle = &resume_handle;
1615 r.out.privs = &privs1;
1618 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1619 if (!NT_STATUS_IS_OK(status)) {
1620 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1624 for (i = 0; i< privs1.count; i++) {
1625 test_LookupPrivDisplayName(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1626 test_LookupPrivValue(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1627 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
1635 static bool test_QueryForestTrustInformation(struct dcerpc_pipe *p,
1636 struct torture_context *tctx,
1637 struct policy_handle *handle,
1638 const char *trusted_domain_name)
1641 struct lsa_lsaRQueryForestTrustInformation r;
1643 struct lsa_String string;
1644 struct lsa_ForestTrustInformation info, *info_ptr;
1646 printf("\nTesting lsaRQueryForestTrustInformation\n");
1648 if (torture_setting_bool(tctx, "samba4", false)) {
1649 printf("skipping QueryForestTrustInformation against Samba4\n");
1653 ZERO_STRUCT(string);
1655 if (trusted_domain_name) {
1656 init_lsa_String(&string, trusted_domain_name);
1661 r.in.handle = handle;
1662 r.in.trusted_domain_name = &string;
1664 r.out.forest_trust_info = &info_ptr;
1666 status = dcerpc_lsa_lsaRQueryForestTrustInformation(p, tctx, &r);
1668 if (!NT_STATUS_IS_OK(status)) {
1669 printf("lsaRQueryForestTrustInformation failed - %s\n", nt_errstr(status));
1676 static bool test_query_each_TrustDomEx(struct dcerpc_pipe *p,
1677 TALLOC_CTX *mem_ctx,
1678 struct policy_handle *handle,
1679 struct lsa_DomainListEx *domains)
1684 for (i=0; i< domains->count; i++) {
1686 if (domains->domains[i].trust_attributes & NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1687 ret &= test_QueryForestTrustInformation(p, mem_ctx, handle,
1688 domains->domains[i].domain_name.string);
1695 static bool test_query_each_TrustDom(struct dcerpc_pipe *p,
1696 TALLOC_CTX *mem_ctx,
1697 struct policy_handle *handle,
1698 struct lsa_DomainList *domains)
1704 printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1705 for (i=0; i< domains->count; i++) {
1706 struct lsa_OpenTrustedDomain trust;
1707 struct lsa_OpenTrustedDomainByName trust_by_name;
1708 struct policy_handle trustdom_handle;
1709 struct policy_handle handle2;
1711 struct lsa_CloseTrustedDomainEx c_trust;
1712 int levels [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
1713 int ok[] = {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1};
1715 if (domains->domains[i].sid) {
1716 trust.in.handle = handle;
1717 trust.in.sid = domains->domains[i].sid;
1718 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1719 trust.out.trustdom_handle = &trustdom_handle;
1721 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1723 if (!NT_STATUS_IS_OK(status)) {
1724 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1728 c.in.handle = &trustdom_handle;
1729 c.out.handle = &handle2;
1731 c_trust.in.handle = &trustdom_handle;
1732 c_trust.out.handle = &handle2;
1734 for (j=0; j < ARRAY_SIZE(levels); j++) {
1735 struct lsa_QueryTrustedDomainInfo q;
1736 union lsa_TrustedDomainInfo info;
1737 q.in.trustdom_handle = &trustdom_handle;
1738 q.in.level = levels[j];
1740 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1741 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1742 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1743 levels[j], nt_errstr(status));
1745 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1746 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1747 levels[j], nt_errstr(status));
1752 status = dcerpc_lsa_CloseTrustedDomainEx(p, mem_ctx, &c_trust);
1753 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1754 printf("Expected CloseTrustedDomainEx to return NT_STATUS_NOT_IMPLEMENTED, instead - %s\n", nt_errstr(status));
1758 c.in.handle = &trustdom_handle;
1759 c.out.handle = &handle2;
1761 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1762 if (!NT_STATUS_IS_OK(status)) {
1763 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1767 for (j=0; j < ARRAY_SIZE(levels); j++) {
1768 struct lsa_QueryTrustedDomainInfoBySid q;
1769 union lsa_TrustedDomainInfo info;
1771 if (!domains->domains[i].sid) {
1775 q.in.handle = handle;
1776 q.in.dom_sid = domains->domains[i].sid;
1777 q.in.level = levels[j];
1779 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1780 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1781 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n",
1782 levels[j], nt_errstr(status));
1784 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1785 printf("QueryTrustedDomainInfoBySid level %d unexpectedly succeeded - %s\n",
1786 levels[j], nt_errstr(status));
1792 trust_by_name.in.handle = handle;
1793 trust_by_name.in.name.string = domains->domains[i].name.string;
1794 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1795 trust_by_name.out.trustdom_handle = &trustdom_handle;
1797 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1799 if (!NT_STATUS_IS_OK(status)) {
1800 printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1804 for (j=0; j < ARRAY_SIZE(levels); j++) {
1805 struct lsa_QueryTrustedDomainInfo q;
1806 union lsa_TrustedDomainInfo info;
1807 q.in.trustdom_handle = &trustdom_handle;
1808 q.in.level = levels[j];
1810 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1811 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1812 printf("QueryTrustedDomainInfo level %d failed - %s\n",
1813 levels[j], nt_errstr(status));
1815 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1816 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1817 levels[j], nt_errstr(status));
1822 c.in.handle = &trustdom_handle;
1823 c.out.handle = &handle2;
1825 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1826 if (!NT_STATUS_IS_OK(status)) {
1827 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1831 for (j=0; j < ARRAY_SIZE(levels); j++) {
1832 struct lsa_QueryTrustedDomainInfoByName q;
1833 union lsa_TrustedDomainInfo info;
1834 q.in.handle = handle;
1835 q.in.trusted_domain.string = domains->domains[i].name.string;
1836 q.in.level = levels[j];
1838 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1839 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1840 printf("QueryTrustedDomainInfoByName level %d failed - %s\n",
1841 levels[j], nt_errstr(status));
1843 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1844 printf("QueryTrustedDomainInfoByName level %d unexpectedly succeeded - %s\n",
1845 levels[j], nt_errstr(status));
1853 static bool test_EnumTrustDom(struct dcerpc_pipe *p,
1854 TALLOC_CTX *mem_ctx,
1855 struct policy_handle *handle)
1857 struct lsa_EnumTrustDom r;
1858 struct lsa_EnumTrustedDomainsEx r_ex;
1859 NTSTATUS enum_status;
1860 uint32_t resume_handle = 0;
1861 struct lsa_DomainList domains;
1862 struct lsa_DomainListEx domains_ex;
1865 printf("\nTesting EnumTrustDom\n");
1867 r.in.handle = handle;
1868 r.in.resume_handle = &resume_handle;
1870 r.out.domains = &domains;
1871 r.out.resume_handle = &resume_handle;
1873 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1875 if (NT_STATUS_IS_OK(enum_status)) {
1876 if (domains.count == 0) {
1877 printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1880 } else if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1881 printf("EnumTrustDom of zero size failed - %s\n", nt_errstr(enum_status));
1886 r.in.handle = handle;
1887 r.in.resume_handle = &resume_handle;
1888 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
1889 r.out.domains = &domains;
1890 r.out.resume_handle = &resume_handle;
1892 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1894 /* NO_MORE_ENTRIES is allowed */
1895 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1897 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1898 /* Windows 2003 gets this off by one on the first run */
1899 if (r.out.domains->count < 3 || r.out.domains->count > 4) {
1900 printf("EnumTrustDom didn't fill the buffer we "
1901 "asked it to (got %d, expected %d / %d == %d entries)\n",
1902 r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3,
1903 LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
1906 } else if (!NT_STATUS_IS_OK(enum_status)) {
1907 printf("EnumTrustDom failed - %s\n", nt_errstr(enum_status));
1911 if (domains.count == 0) {
1912 printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1916 ret &= test_query_each_TrustDom(p, mem_ctx, handle, &domains);
1918 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1920 printf("\nTesting EnumTrustedDomainsEx\n");
1922 r_ex.in.handle = handle;
1923 r_ex.in.resume_handle = &resume_handle;
1924 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1925 r_ex.out.domains = &domains_ex;
1926 r_ex.out.resume_handle = &resume_handle;
1928 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1930 if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1931 printf("EnumTrustedDomainEx of zero size failed - %s\n", nt_errstr(enum_status));
1937 r_ex.in.handle = handle;
1938 r_ex.in.resume_handle = &resume_handle;
1939 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1940 r_ex.out.domains = &domains_ex;
1941 r_ex.out.resume_handle = &resume_handle;
1943 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1945 /* NO_MORE_ENTRIES is allowed */
1946 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1948 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1949 /* Windows 2003 gets this off by one on the first run */
1950 if (r_ex.out.domains->count < 3 || r_ex.out.domains->count > 4) {
1951 printf("EnumTrustDom didn't fill the buffer we "
1952 "asked it to (got %d, expected %d / %d == %d entries)\n",
1953 r_ex.out.domains->count,
1955 LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER,
1956 r_ex.in.max_size / LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER);
1958 } else if (!NT_STATUS_IS_OK(enum_status)) {
1959 printf("EnumTrustedDomainEx failed - %s\n", nt_errstr(enum_status));
1963 if (domains_ex.count == 0) {
1964 printf("EnumTrustDomainEx failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1968 ret &= test_query_each_TrustDomEx(p, mem_ctx, handle, &domains_ex);
1970 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1975 static bool test_CreateTrustedDomain(struct dcerpc_pipe *p,
1976 TALLOC_CTX *mem_ctx,
1977 struct policy_handle *handle)
1981 struct lsa_CreateTrustedDomain r;
1982 struct lsa_DomainInfo trustinfo;
1983 struct dom_sid *domsid[12];
1984 struct policy_handle trustdom_handle[12];
1985 struct lsa_QueryTrustedDomainInfo q;
1988 printf("Testing CreateTrustedDomain for 12 domains\n");
1990 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1994 for (i=0; i< 12; i++) {
1995 char *trust_name = talloc_asprintf(mem_ctx, "torturedom%02d", i);
1996 char *trust_sid = talloc_asprintf(mem_ctx, "S-1-5-21-97398-379795-100%02d", i);
1998 domsid[i] = dom_sid_parse_talloc(mem_ctx, trust_sid);
2000 trustinfo.sid = domsid[i];
2001 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
2003 r.in.handle = handle;
2004 r.in.info = &trustinfo;
2005 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2006 r.out.trustdom_handle = &trustdom_handle[i];
2008 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
2009 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
2010 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
2011 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
2013 if (!NT_STATUS_IS_OK(status)) {
2014 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
2018 q.in.trustdom_handle = &trustdom_handle[i];
2019 q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
2020 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
2021 if (!NT_STATUS_IS_OK(status)) {
2022 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
2024 } else if (!q.out.info) {
2027 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
2028 printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
2029 q.out.info->name.netbios_name.string, trustinfo.name.string);
2036 /* now that we have some domains to look over, we can test the enum calls */
2037 if (!test_EnumTrustDom(p, mem_ctx, handle)) {
2041 for (i=0; i<12; i++) {
2042 if (!test_DeleteTrustedDomainBySid(p, mem_ctx, handle, domsid[i])) {
2050 static bool test_QueryDomainInfoPolicy(struct dcerpc_pipe *p,
2051 struct torture_context *tctx,
2052 struct policy_handle *handle)
2054 struct lsa_QueryDomainInformationPolicy r;
2059 printf("\nTesting QueryDomainInformationPolicy\n");
2062 r.in.handle = handle;
2065 printf("\ntrying QueryDomainInformationPolicy level %d\n", i);
2067 status = dcerpc_lsa_QueryDomainInformationPolicy(p, tctx, &r);
2069 /* If the server does not support EFS, then this is the correct return */
2070 if (i == LSA_DOMAIN_INFO_POLICY_EFS && NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2072 } else if (!NT_STATUS_IS_OK(status)) {
2073 printf("QueryDomainInformationPolicy failed - %s\n", nt_errstr(status));
2083 static bool test_QueryInfoPolicy(struct dcerpc_pipe *p,
2084 struct torture_context *tctx,
2085 struct policy_handle *handle)
2087 struct lsa_QueryInfoPolicy r;
2091 printf("\nTesting QueryInfoPolicy\n");
2093 for (i=1;i<13;i++) {
2094 r.in.handle = handle;
2097 printf("\ntrying QueryInfoPolicy level %d\n", i);
2099 status = dcerpc_lsa_QueryInfoPolicy(p, tctx, &r);
2102 case LSA_POLICY_INFO_DB:
2103 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2104 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2105 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2106 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2110 case LSA_POLICY_INFO_DOMAIN:
2111 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2112 case LSA_POLICY_INFO_DNS:
2113 if (!NT_STATUS_IS_OK(status)) {
2114 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2119 if (torture_setting_bool(tctx, "samba4", false)) {
2120 /* Other levels not implemented yet */
2121 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2122 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2125 } else if (!NT_STATUS_IS_OK(status)) {
2126 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2132 if (NT_STATUS_IS_OK(status) && i == LSA_POLICY_INFO_DNS) {
2133 /* Let's look up some of these names */
2135 struct lsa_TransNameArray tnames;
2137 tnames.names = talloc_zero_array(tctx, struct lsa_TranslatedName, tnames.count);
2138 tnames.names[0].name.string = r.out.info->dns.name.string;
2139 tnames.names[0].sid_type = SID_NAME_DOMAIN;
2140 tnames.names[1].name.string = r.out.info->dns.dns_domain.string;
2141 tnames.names[1].sid_type = SID_NAME_DOMAIN;
2142 tnames.names[2].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.name.string);
2143 tnames.names[2].sid_type = SID_NAME_DOMAIN;
2144 tnames.names[3].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.dns_domain.string);
2145 tnames.names[3].sid_type = SID_NAME_DOMAIN;
2146 tnames.names[4].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.name.string);
2147 tnames.names[4].sid_type = SID_NAME_USER;
2148 tnames.names[5].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.name.string);
2149 tnames.names[5].sid_type = SID_NAME_USER;
2150 tnames.names[6].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.dns_domain.string);
2151 tnames.names[6].sid_type = SID_NAME_USER;
2152 tnames.names[7].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.dns_domain.string);
2153 tnames.names[7].sid_type = SID_NAME_USER;
2154 tnames.names[8].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.name.string);
2155 tnames.names[8].sid_type = SID_NAME_USER;
2156 tnames.names[9].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.dns_domain.string);
2157 tnames.names[9].sid_type = SID_NAME_USER;
2158 tnames.names[10].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.name.string);
2159 tnames.names[10].sid_type = SID_NAME_USER;
2160 tnames.names[11].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.dns_domain.string);
2161 tnames.names[11].sid_type = SID_NAME_USER;
2162 tnames.names[12].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.name.string);
2163 tnames.names[12].sid_type = SID_NAME_USER;
2164 tnames.names[13].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.dns_domain.string);
2165 tnames.names[13].sid_type = SID_NAME_USER;
2166 ret &= test_LookupNames(p, tctx, handle, &tnames);
2174 static bool test_QueryInfoPolicy2(struct dcerpc_pipe *p,
2175 struct torture_context *tctx,
2176 struct policy_handle *handle)
2178 struct lsa_QueryInfoPolicy2 r;
2182 printf("\nTesting QueryInfoPolicy2\n");
2183 for (i=1;i<13;i++) {
2184 r.in.handle = handle;
2187 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
2189 status = dcerpc_lsa_QueryInfoPolicy2(p, tctx, &r);
2192 case LSA_POLICY_INFO_DB:
2193 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2194 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2195 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2196 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2200 case LSA_POLICY_INFO_DOMAIN:
2201 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2202 case LSA_POLICY_INFO_DNS:
2203 if (!NT_STATUS_IS_OK(status)) {
2204 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2209 if (torture_setting_bool(tctx, "samba4", false)) {
2210 /* Other levels not implemented yet */
2211 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2212 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2215 } else if (!NT_STATUS_IS_OK(status)) {
2216 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2226 static bool test_GetUserName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
2228 struct lsa_GetUserName r;
2231 struct lsa_StringPointer authority_name_p;
2233 printf("\nTesting GetUserName\n");
2235 r.in.system_name = "\\";
2236 r.in.account_name = NULL;
2237 r.in.authority_name = &authority_name_p;
2238 authority_name_p.string = NULL;
2240 status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
2242 if (!NT_STATUS_IS_OK(status)) {
2243 printf("GetUserName failed - %s\n", nt_errstr(status));
2250 bool test_lsa_Close(struct dcerpc_pipe *p,
2251 TALLOC_CTX *mem_ctx,
2252 struct policy_handle *handle)
2256 struct policy_handle handle2;
2258 printf("\ntesting Close\n");
2260 r.in.handle = handle;
2261 r.out.handle = &handle2;
2263 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2264 if (!NT_STATUS_IS_OK(status)) {
2265 printf("Close failed - %s\n", nt_errstr(status));
2269 status = dcerpc_lsa_Close(p, mem_ctx, &r);
2270 /* its really a fault - we need a status code for rpc fault */
2271 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
2272 printf("Close failed - %s\n", nt_errstr(status));
2281 bool torture_rpc_lsa(struct torture_context *tctx)
2284 struct dcerpc_pipe *p;
2286 struct policy_handle *handle;
2287 struct test_join *join = NULL;
2288 struct cli_credentials *machine_creds;
2290 status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
2291 if (!NT_STATUS_IS_OK(status)) {
2295 if (!test_OpenPolicy(p, tctx)) {
2299 if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2304 join = torture_join_domain(tctx, TEST_MACHINENAME, ACB_WSTRUST, &machine_creds);
2309 if (!test_LookupNames_wellknown(p, tctx, handle)) {
2313 if (!test_LookupNames_bogus(p, tctx, handle)) {
2317 if (!test_LookupSids_async(p, tctx, handle)) {
2321 if (!test_QueryDomainInfoPolicy(p, tctx, handle)) {
2325 if (!test_CreateAccount(p, tctx, handle)) {
2329 if (!test_CreateSecret(p, tctx, handle)) {
2333 if (!test_CreateTrustedDomain(p, tctx, handle)) {
2337 if (!test_EnumAccounts(p, tctx, handle)) {
2341 if (!test_EnumPrivs(p, tctx, handle)) {
2345 if (!test_QueryInfoPolicy(p, tctx, handle)) {
2349 if (!test_QueryInfoPolicy2(p, tctx, handle)) {
2353 if (!test_Delete(p, tctx, handle)) {
2357 if (!test_many_LookupSids(p, tctx, handle)) {
2361 if (!test_lsa_Close(p, tctx, handle)) {
2365 torture_leave_domain(tctx, join);
2368 if (!test_many_LookupSids(p, tctx, handle)) {
2373 if (!test_GetUserName(p, tctx)) {
2380 bool torture_rpc_lsa_get_user(struct torture_context *torture)
2383 struct dcerpc_pipe *p;
2384 TALLOC_CTX *mem_ctx;
2387 mem_ctx = talloc_init("torture_rpc_lsa_get_user");
2389 status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
2390 if (!NT_STATUS_IS_OK(status)) {
2391 talloc_free(mem_ctx);
2395 if (!test_GetUserName(p, mem_ctx)) {
2399 talloc_free(mem_ctx);