Revert "fix LSA-PRIVILEGES"
[Samba/gbeck.git] / source4 / torture / rpc / lsa.c
blobb45b56545251214ffcb2f069a0df60af031a1ed7
1 /*
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/>.
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_lsa_c.h"
25 #include "librpc/gen_ndr/netlogon.h"
26 #include "librpc/gen_ndr/ndr_drsblobs.h"
27 #include "lib/events/events.h"
28 #include "libcli/security/security.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "torture/rpc/rpc.h"
31 #include "param/param.h"
32 #include "../lib/crypto/crypto.h"
33 #define TEST_MACHINENAME "lsatestmach"
35 static void init_lsa_String(struct lsa_String *name, const char *s)
37 name->string = s;
40 static bool test_OpenPolicy(struct dcerpc_pipe *p,
41 struct torture_context *tctx)
43 struct lsa_ObjectAttribute attr;
44 struct policy_handle handle;
45 struct lsa_QosInfo qos;
46 struct lsa_OpenPolicy r;
47 NTSTATUS status;
48 uint16_t system_name = '\\';
50 torture_comment(tctx, "\nTesting OpenPolicy\n");
52 qos.len = 0;
53 qos.impersonation_level = 2;
54 qos.context_mode = 1;
55 qos.effective_only = 0;
57 attr.len = 0;
58 attr.root_dir = NULL;
59 attr.object_name = NULL;
60 attr.attributes = 0;
61 attr.sec_desc = NULL;
62 attr.sec_qos = &qos;
64 r.in.system_name = &system_name;
65 r.in.attr = &attr;
66 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
67 r.out.handle = &handle;
69 status = dcerpc_lsa_OpenPolicy(p, tctx, &r);
70 if (!NT_STATUS_IS_OK(status)) {
71 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
72 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
73 torture_comment(tctx, "not considering %s to be an error\n", nt_errstr(status));
74 return true;
76 torture_comment(tctx, "OpenPolicy failed - %s\n", nt_errstr(status));
77 return false;
80 return true;
84 bool test_lsa_OpenPolicy2(struct dcerpc_pipe *p,
85 struct torture_context *tctx,
86 struct policy_handle **handle)
88 struct lsa_ObjectAttribute attr;
89 struct lsa_QosInfo qos;
90 struct lsa_OpenPolicy2 r;
91 NTSTATUS status;
93 torture_comment(tctx, "\nTesting OpenPolicy2\n");
95 *handle = talloc(tctx, struct policy_handle);
96 if (!*handle) {
97 return false;
100 qos.len = 0;
101 qos.impersonation_level = 2;
102 qos.context_mode = 1;
103 qos.effective_only = 0;
105 attr.len = 0;
106 attr.root_dir = NULL;
107 attr.object_name = NULL;
108 attr.attributes = 0;
109 attr.sec_desc = NULL;
110 attr.sec_qos = &qos;
112 r.in.system_name = "\\";
113 r.in.attr = &attr;
114 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
115 r.out.handle = *handle;
117 status = dcerpc_lsa_OpenPolicy2(p, tctx, &r);
118 if (!NT_STATUS_IS_OK(status)) {
119 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
120 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
121 torture_comment(tctx, "not considering %s to be an error\n", nt_errstr(status));
122 talloc_free(*handle);
123 *handle = NULL;
124 return true;
126 torture_comment(tctx, "OpenPolicy2 failed - %s\n", nt_errstr(status));
127 return false;
130 return true;
134 static const char *sid_type_lookup(enum lsa_SidType r)
136 switch (r) {
137 case SID_NAME_USE_NONE: return "SID_NAME_USE_NONE"; break;
138 case SID_NAME_USER: return "SID_NAME_USER"; break;
139 case SID_NAME_DOM_GRP: return "SID_NAME_DOM_GRP"; break;
140 case SID_NAME_DOMAIN: return "SID_NAME_DOMAIN"; break;
141 case SID_NAME_ALIAS: return "SID_NAME_ALIAS"; break;
142 case SID_NAME_WKN_GRP: return "SID_NAME_WKN_GRP"; break;
143 case SID_NAME_DELETED: return "SID_NAME_DELETED"; break;
144 case SID_NAME_INVALID: return "SID_NAME_INVALID"; break;
145 case SID_NAME_UNKNOWN: return "SID_NAME_UNKNOWN"; break;
146 case SID_NAME_COMPUTER: return "SID_NAME_COMPUTER"; break;
148 return "Invalid sid type\n";
151 static bool test_LookupNames(struct dcerpc_pipe *p,
152 struct torture_context *tctx,
153 struct policy_handle *handle,
154 struct lsa_TransNameArray *tnames)
156 struct lsa_LookupNames r;
157 struct lsa_TransSidArray sids;
158 struct lsa_RefDomainList *domains = NULL;
159 struct lsa_String *names;
160 uint32_t count = 0;
161 NTSTATUS status;
162 int i;
164 torture_comment(tctx, "\nTesting LookupNames with %d names\n", tnames->count);
166 sids.count = 0;
167 sids.sids = NULL;
169 names = talloc_array(tctx, struct lsa_String, tnames->count);
170 for (i=0;i<tnames->count;i++) {
171 init_lsa_String(&names[i], tnames->names[i].name.string);
174 r.in.handle = handle;
175 r.in.num_names = tnames->count;
176 r.in.names = names;
177 r.in.sids = &sids;
178 r.in.level = 1;
179 r.in.count = &count;
180 r.out.count = &count;
181 r.out.sids = &sids;
182 r.out.domains = &domains;
184 status = dcerpc_lsa_LookupNames(p, tctx, &r);
186 if (NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED) ||
187 NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
188 for (i=0;i< tnames->count;i++) {
189 if (i < count && sids.sids[i].sid_type == SID_NAME_UNKNOWN) {
190 torture_comment(tctx, "LookupName of %s was unmapped\n",
191 tnames->names[i].name.string);
192 } else if (i >=count) {
193 torture_comment(tctx, "LookupName of %s failed to return a result\n",
194 tnames->names[i].name.string);
197 torture_comment(tctx, "LookupNames failed - %s\n", nt_errstr(status));
198 return false;
199 } else if (!NT_STATUS_IS_OK(status)) {
200 torture_comment(tctx, "LookupNames failed - %s\n", nt_errstr(status));
201 return false;
204 for (i=0;i< tnames->count;i++) {
205 if (i < count && sids.sids[i].sid_type != tnames->names[i].sid_type) {
206 torture_comment(tctx, "LookupName of %s got unexpected name type: %s\n",
207 tnames->names[i].name.string, sid_type_lookup(sids.sids[i].sid_type));
208 } else if (i >=count) {
209 torture_comment(tctx, "LookupName of %s failed to return a result\n",
210 tnames->names[i].name.string);
213 torture_comment(tctx, "\n");
215 return true;
218 static bool test_LookupNames_bogus(struct dcerpc_pipe *p,
219 struct torture_context *tctx,
220 struct policy_handle *handle)
222 struct lsa_LookupNames r;
223 struct lsa_TransSidArray sids;
224 struct lsa_RefDomainList *domains = NULL;
225 struct lsa_String *names;
226 uint32_t count = 0;
227 NTSTATUS status;
228 int i;
230 struct lsa_TranslatedName name[2];
231 struct lsa_TransNameArray tnames;
233 tnames.names = name;
234 tnames.count = 2;
235 name[0].name.string = "NT AUTHORITY\\BOGUS";
236 name[1].name.string = NULL;
238 torture_comment(tctx, "\nTesting LookupNames with bogus names\n");
240 sids.count = 0;
241 sids.sids = NULL;
243 names = talloc_array(tctx, struct lsa_String, tnames.count);
244 for (i=0;i<tnames.count;i++) {
245 init_lsa_String(&names[i], tnames.names[i].name.string);
248 r.in.handle = handle;
249 r.in.num_names = tnames.count;
250 r.in.names = names;
251 r.in.sids = &sids;
252 r.in.level = 1;
253 r.in.count = &count;
254 r.out.count = &count;
255 r.out.sids = &sids;
256 r.out.domains = &domains;
258 status = dcerpc_lsa_LookupNames(p, tctx, &r);
259 if (!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
260 torture_comment(tctx, "LookupNames failed - %s\n", nt_errstr(status));
261 return false;
264 torture_comment(tctx, "\n");
266 return true;
269 static bool test_LookupNames_wellknown(struct dcerpc_pipe *p,
270 struct torture_context *tctx,
271 struct policy_handle *handle)
273 struct lsa_TranslatedName name;
274 struct lsa_TransNameArray tnames;
275 bool ret = true;
277 torture_comment(tctx, "Testing LookupNames with well known names\n");
279 tnames.names = &name;
280 tnames.count = 1;
281 name.name.string = "NT AUTHORITY\\SYSTEM";
282 name.sid_type = SID_NAME_WKN_GRP;
283 ret &= test_LookupNames(p, tctx, handle, &tnames);
285 name.name.string = "NT AUTHORITY\\ANONYMOUS LOGON";
286 name.sid_type = SID_NAME_WKN_GRP;
287 ret &= test_LookupNames(p, tctx, handle, &tnames);
289 name.name.string = "NT AUTHORITY\\Authenticated Users";
290 name.sid_type = SID_NAME_WKN_GRP;
291 ret &= test_LookupNames(p, tctx, handle, &tnames);
293 #if 0
294 name.name.string = "NT AUTHORITY";
295 ret &= test_LookupNames(p, tctx, handle, &tnames);
297 name.name.string = "NT AUTHORITY\\";
298 ret &= test_LookupNames(p, tctx, handle, &tnames);
299 #endif
301 name.name.string = "BUILTIN\\";
302 name.sid_type = SID_NAME_DOMAIN;
303 ret &= test_LookupNames(p, tctx, handle, &tnames);
305 name.name.string = "BUILTIN\\Administrators";
306 name.sid_type = SID_NAME_ALIAS;
307 ret &= test_LookupNames(p, tctx, handle, &tnames);
309 name.name.string = "SYSTEM";
310 name.sid_type = SID_NAME_WKN_GRP;
311 ret &= test_LookupNames(p, tctx, handle, &tnames);
313 name.name.string = "Everyone";
314 name.sid_type = SID_NAME_WKN_GRP;
315 ret &= test_LookupNames(p, tctx, handle, &tnames);
316 return ret;
319 static bool test_LookupNames2(struct dcerpc_pipe *p,
320 struct torture_context *tctx,
321 struct policy_handle *handle,
322 struct lsa_TransNameArray2 *tnames,
323 bool check_result)
325 struct lsa_LookupNames2 r;
326 struct lsa_TransSidArray2 sids;
327 struct lsa_RefDomainList *domains = NULL;
328 struct lsa_String *names;
329 uint32_t count = 0;
330 NTSTATUS status;
331 int i;
333 torture_comment(tctx, "\nTesting LookupNames2 with %d names\n", tnames->count);
335 sids.count = 0;
336 sids.sids = NULL;
338 names = talloc_array(tctx, struct lsa_String, tnames->count);
339 for (i=0;i<tnames->count;i++) {
340 init_lsa_String(&names[i], tnames->names[i].name.string);
343 r.in.handle = handle;
344 r.in.num_names = tnames->count;
345 r.in.names = names;
346 r.in.sids = &sids;
347 r.in.level = 1;
348 r.in.count = &count;
349 r.in.lookup_options = 0;
350 r.in.client_revision = 0;
351 r.out.count = &count;
352 r.out.sids = &sids;
353 r.out.domains = &domains;
355 status = dcerpc_lsa_LookupNames2(p, tctx, &r);
356 if (!NT_STATUS_IS_OK(status)) {
357 torture_comment(tctx, "LookupNames2 failed - %s\n", nt_errstr(status));
358 return false;
361 if (check_result) {
362 torture_assert_int_equal(tctx, count, sids.count,
363 "unexpected number of results returned");
364 if (sids.count > 0) {
365 torture_assert(tctx, sids.sids, "invalid sid buffer");
369 torture_comment(tctx, "\n");
371 return true;
375 static bool test_LookupNames3(struct dcerpc_pipe *p,
376 struct torture_context *tctx,
377 struct policy_handle *handle,
378 struct lsa_TransNameArray2 *tnames,
379 bool check_result)
381 struct lsa_LookupNames3 r;
382 struct lsa_TransSidArray3 sids;
383 struct lsa_RefDomainList *domains = NULL;
384 struct lsa_String *names;
385 uint32_t count = 0;
386 NTSTATUS status;
387 int i;
389 torture_comment(tctx, "\nTesting LookupNames3 with %d names\n", tnames->count);
391 sids.count = 0;
392 sids.sids = NULL;
394 names = talloc_array(tctx, struct lsa_String, tnames->count);
395 for (i=0;i<tnames->count;i++) {
396 init_lsa_String(&names[i], tnames->names[i].name.string);
399 r.in.handle = handle;
400 r.in.num_names = tnames->count;
401 r.in.names = names;
402 r.in.sids = &sids;
403 r.in.level = 1;
404 r.in.count = &count;
405 r.in.lookup_options = 0;
406 r.in.client_revision = 0;
407 r.out.count = &count;
408 r.out.sids = &sids;
409 r.out.domains = &domains;
411 status = dcerpc_lsa_LookupNames3(p, tctx, &r);
412 if (!NT_STATUS_IS_OK(status)) {
413 torture_comment(tctx, "LookupNames3 failed - %s\n", nt_errstr(status));
414 return false;
417 if (check_result) {
418 torture_assert_int_equal(tctx, count, sids.count,
419 "unexpected number of results returned");
420 if (sids.count > 0) {
421 torture_assert(tctx, sids.sids, "invalid sid buffer");
425 torture_comment(tctx, "\n");
427 return true;
430 static bool test_LookupNames4(struct dcerpc_pipe *p,
431 struct torture_context *tctx,
432 struct lsa_TransNameArray2 *tnames,
433 bool check_result)
435 struct lsa_LookupNames4 r;
436 struct lsa_TransSidArray3 sids;
437 struct lsa_RefDomainList *domains = NULL;
438 struct lsa_String *names;
439 uint32_t count = 0;
440 NTSTATUS status;
441 int i;
443 torture_comment(tctx, "\nTesting LookupNames4 with %d names\n", tnames->count);
445 sids.count = 0;
446 sids.sids = NULL;
448 names = talloc_array(tctx, struct lsa_String, tnames->count);
449 for (i=0;i<tnames->count;i++) {
450 init_lsa_String(&names[i], tnames->names[i].name.string);
453 r.in.num_names = tnames->count;
454 r.in.names = names;
455 r.in.sids = &sids;
456 r.in.level = 1;
457 r.in.count = &count;
458 r.in.lookup_options = 0;
459 r.in.client_revision = 0;
460 r.out.count = &count;
461 r.out.sids = &sids;
462 r.out.domains = &domains;
464 status = dcerpc_lsa_LookupNames4(p, tctx, &r);
465 if (!NT_STATUS_IS_OK(status)) {
466 torture_comment(tctx, "LookupNames4 failed - %s\n", nt_errstr(status));
467 return false;
470 if (check_result) {
471 torture_assert_int_equal(tctx, count, sids.count,
472 "unexpected number of results returned");
473 if (sids.count > 0) {
474 torture_assert(tctx, sids.sids, "invalid sid buffer");
478 torture_comment(tctx, "\n");
480 return true;
484 static bool test_LookupSids(struct dcerpc_pipe *p,
485 struct torture_context *tctx,
486 struct policy_handle *handle,
487 struct lsa_SidArray *sids)
489 struct lsa_LookupSids r;
490 struct lsa_TransNameArray names;
491 struct lsa_RefDomainList *domains = NULL;
492 uint32_t count = sids->num_sids;
493 NTSTATUS status;
495 torture_comment(tctx, "\nTesting LookupSids\n");
497 names.count = 0;
498 names.names = NULL;
500 r.in.handle = handle;
501 r.in.sids = sids;
502 r.in.names = &names;
503 r.in.level = 1;
504 r.in.count = &count;
505 r.out.count = &count;
506 r.out.names = &names;
507 r.out.domains = &domains;
509 status = dcerpc_lsa_LookupSids(p, tctx, &r);
510 if (!NT_STATUS_IS_OK(status)) {
511 torture_comment(tctx, "LookupSids failed - %s\n", nt_errstr(status));
512 return false;
515 torture_comment(tctx, "\n");
517 if (!test_LookupNames(p, tctx, handle, &names)) {
518 return false;
521 return true;
525 static bool test_LookupSids2(struct dcerpc_pipe *p,
526 struct torture_context *tctx,
527 struct policy_handle *handle,
528 struct lsa_SidArray *sids)
530 struct lsa_LookupSids2 r;
531 struct lsa_TransNameArray2 names;
532 struct lsa_RefDomainList *domains = NULL;
533 uint32_t count = sids->num_sids;
534 NTSTATUS status;
536 torture_comment(tctx, "\nTesting LookupSids2\n");
538 names.count = 0;
539 names.names = NULL;
541 r.in.handle = handle;
542 r.in.sids = sids;
543 r.in.names = &names;
544 r.in.level = 1;
545 r.in.count = &count;
546 r.in.unknown1 = 0;
547 r.in.unknown2 = 0;
548 r.out.count = &count;
549 r.out.names = &names;
550 r.out.domains = &domains;
552 status = dcerpc_lsa_LookupSids2(p, tctx, &r);
553 if (!NT_STATUS_IS_OK(status)) {
554 torture_comment(tctx, "LookupSids2 failed - %s\n", nt_errstr(status));
555 return false;
558 torture_comment(tctx, "\n");
560 if (!test_LookupNames2(p, tctx, handle, &names, false)) {
561 return false;
564 if (!test_LookupNames3(p, tctx, handle, &names, false)) {
565 return false;
568 return true;
571 static bool test_LookupSids3(struct dcerpc_pipe *p,
572 struct torture_context *tctx,
573 struct lsa_SidArray *sids)
575 struct lsa_LookupSids3 r;
576 struct lsa_TransNameArray2 names;
577 struct lsa_RefDomainList *domains = NULL;
578 uint32_t count = sids->num_sids;
579 NTSTATUS status;
581 torture_comment(tctx, "\nTesting LookupSids3\n");
583 names.count = 0;
584 names.names = NULL;
586 r.in.sids = sids;
587 r.in.names = &names;
588 r.in.level = 1;
589 r.in.count = &count;
590 r.in.unknown1 = 0;
591 r.in.unknown2 = 0;
592 r.out.domains = &domains;
593 r.out.count = &count;
594 r.out.names = &names;
596 status = dcerpc_lsa_LookupSids3(p, tctx, &r);
597 if (!NT_STATUS_IS_OK(status)) {
598 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
599 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
600 torture_comment(tctx, "not considering %s to be an error\n", nt_errstr(status));
601 return true;
603 torture_comment(tctx, "LookupSids3 failed - %s - not considered an error\n",
604 nt_errstr(status));
605 return false;
608 torture_comment(tctx, "\n");
610 if (!test_LookupNames4(p, tctx, &names, false)) {
611 return false;
614 return true;
617 bool test_many_LookupSids(struct dcerpc_pipe *p,
618 struct torture_context *tctx,
619 struct policy_handle *handle)
621 uint32_t count;
622 NTSTATUS status;
623 struct lsa_SidArray sids;
624 int i;
626 torture_comment(tctx, "\nTesting LookupSids with lots of SIDs\n");
628 sids.num_sids = 100;
630 sids.sids = talloc_array(tctx, struct lsa_SidPtr, sids.num_sids);
632 for (i=0; i<sids.num_sids; i++) {
633 const char *sidstr = "S-1-5-32-545";
634 sids.sids[i].sid = dom_sid_parse_talloc(tctx, sidstr);
637 count = sids.num_sids;
639 if (handle) {
640 struct lsa_LookupSids r;
641 struct lsa_TransNameArray names;
642 struct lsa_RefDomainList *domains = NULL;
643 names.count = 0;
644 names.names = NULL;
646 r.in.handle = handle;
647 r.in.sids = &sids;
648 r.in.names = &names;
649 r.in.level = 1;
650 r.in.count = &names.count;
651 r.out.count = &count;
652 r.out.names = &names;
653 r.out.domains = &domains;
655 status = dcerpc_lsa_LookupSids(p, tctx, &r);
656 if (!NT_STATUS_IS_OK(status)) {
657 torture_comment(tctx, "LookupSids failed - %s\n", nt_errstr(status));
658 return false;
661 torture_comment(tctx, "\n");
663 if (!test_LookupNames(p, tctx, handle, &names)) {
664 return false;
666 } else if (p->conn->security_state.auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL &&
667 p->conn->security_state.auth_info->auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
668 struct lsa_LookupSids3 r;
669 struct lsa_RefDomainList *domains = NULL;
670 struct lsa_TransNameArray2 names;
672 names.count = 0;
673 names.names = NULL;
675 torture_comment(tctx, "\nTesting LookupSids3\n");
677 r.in.sids = &sids;
678 r.in.names = &names;
679 r.in.level = 1;
680 r.in.count = &count;
681 r.in.unknown1 = 0;
682 r.in.unknown2 = 0;
683 r.out.count = &count;
684 r.out.names = &names;
685 r.out.domains = &domains;
687 status = dcerpc_lsa_LookupSids3(p, tctx, &r);
688 if (!NT_STATUS_IS_OK(status)) {
689 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
690 NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
691 torture_comment(tctx, "not considering %s to be an error\n", nt_errstr(status));
692 return true;
694 torture_comment(tctx, "LookupSids3 failed - %s\n",
695 nt_errstr(status));
696 return false;
698 if (!test_LookupNames4(p, tctx, &names, false)) {
699 return false;
703 torture_comment(tctx, "\n");
707 return true;
710 static void lookupsids_cb(struct rpc_request *req)
712 int *replies = (int *)req->async.private_data;
713 NTSTATUS status;
715 status = dcerpc_ndr_request_recv(req);
716 if (!NT_STATUS_IS_OK(status)) {
717 printf("lookupsids returned %s\n", nt_errstr(status));
718 *replies = -1;
721 if (*replies >= 0) {
722 *replies += 1;
726 static bool test_LookupSids_async(struct dcerpc_pipe *p,
727 struct torture_context *tctx,
728 struct policy_handle *handle)
730 struct lsa_SidArray sids;
731 struct lsa_SidPtr sidptr;
732 uint32_t *count;
733 struct lsa_TransNameArray *names;
734 struct lsa_LookupSids *r;
735 struct lsa_RefDomainList *domains = NULL;
736 struct rpc_request **req;
737 int i, replies;
738 bool ret = true;
739 const int num_async_requests = 50;
741 count = talloc_array(tctx, uint32_t, num_async_requests);
742 names = talloc_array(tctx, struct lsa_TransNameArray, num_async_requests);
743 r = talloc_array(tctx, struct lsa_LookupSids, num_async_requests);
745 torture_comment(tctx, "\nTesting %d async lookupsids request\n", num_async_requests);
747 req = talloc_array(tctx, struct rpc_request *, num_async_requests);
749 sids.num_sids = 1;
750 sids.sids = &sidptr;
751 sidptr.sid = dom_sid_parse_talloc(tctx, "S-1-5-32-545");
753 replies = 0;
755 for (i=0; i<num_async_requests; i++) {
756 count[i] = 0;
757 names[i].count = 0;
758 names[i].names = NULL;
760 r[i].in.handle = handle;
761 r[i].in.sids = &sids;
762 r[i].in.names = &names[i];
763 r[i].in.level = 1;
764 r[i].in.count = &names[i].count;
765 r[i].out.count = &count[i];
766 r[i].out.names = &names[i];
767 r[i].out.domains = &domains;
769 req[i] = dcerpc_lsa_LookupSids_send(p, req, &r[i]);
770 if (req[i] == NULL) {
771 ret = false;
772 break;
775 req[i]->async.callback = lookupsids_cb;
776 req[i]->async.private_data = &replies;
779 while (replies >= 0 && replies < num_async_requests) {
780 event_loop_once(p->conn->event_ctx);
783 talloc_free(req);
785 if (replies < 0) {
786 ret = false;
789 return ret;
792 static bool test_LookupPrivValue(struct dcerpc_pipe *p,
793 struct torture_context *tctx,
794 struct policy_handle *handle,
795 struct lsa_String *name)
797 NTSTATUS status;
798 struct lsa_LookupPrivValue r;
799 struct lsa_LUID luid;
801 r.in.handle = handle;
802 r.in.name = name;
803 r.out.luid = &luid;
805 status = dcerpc_lsa_LookupPrivValue(p, tctx, &r);
806 if (!NT_STATUS_IS_OK(status)) {
807 torture_comment(tctx, "\nLookupPrivValue failed - %s\n", nt_errstr(status));
808 return false;
811 return true;
814 static bool test_LookupPrivName(struct dcerpc_pipe *p,
815 struct torture_context *tctx,
816 struct policy_handle *handle,
817 struct lsa_LUID *luid)
819 NTSTATUS status;
820 struct lsa_LookupPrivName r;
821 struct lsa_StringLarge *name = NULL;
823 r.in.handle = handle;
824 r.in.luid = luid;
825 r.out.name = &name;
827 status = dcerpc_lsa_LookupPrivName(p, tctx, &r);
828 if (!NT_STATUS_IS_OK(status)) {
829 torture_comment(tctx, "\nLookupPrivName failed - %s\n", nt_errstr(status));
830 return false;
833 return true;
836 static bool test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p,
837 struct torture_context *tctx,
838 struct policy_handle *handle,
839 struct policy_handle *acct_handle,
840 struct lsa_LUID *luid)
842 NTSTATUS status;
843 struct lsa_RemovePrivilegesFromAccount r;
844 struct lsa_PrivilegeSet privs;
845 bool ret = true;
847 torture_comment(tctx, "\nTesting RemovePrivilegesFromAccount\n");
849 r.in.handle = acct_handle;
850 r.in.remove_all = 0;
851 r.in.privs = &privs;
853 privs.count = 1;
854 privs.unknown = 0;
855 privs.set = talloc_array(tctx, struct lsa_LUIDAttribute, 1);
856 privs.set[0].luid = *luid;
857 privs.set[0].attribute = 0;
859 status = dcerpc_lsa_RemovePrivilegesFromAccount(p, tctx, &r);
860 if (!NT_STATUS_IS_OK(status)) {
862 struct lsa_LookupPrivName r_name;
863 struct lsa_StringLarge *name = NULL;
865 r_name.in.handle = handle;
866 r_name.in.luid = luid;
867 r_name.out.name = &name;
869 status = dcerpc_lsa_LookupPrivName(p, tctx, &r_name);
870 if (!NT_STATUS_IS_OK(status)) {
871 torture_comment(tctx, "\nLookupPrivName failed - %s\n", nt_errstr(status));
872 return false;
874 /* Windows 2008 does not allow this to be removed */
875 if (strcmp("SeAuditPrivilege", name->string) == 0) {
876 return ret;
879 torture_comment(tctx, "RemovePrivilegesFromAccount failed to remove %s - %s\n",
880 name->string,
881 nt_errstr(status));
882 return false;
885 return ret;
888 static bool test_AddPrivilegesToAccount(struct dcerpc_pipe *p,
889 struct torture_context *tctx,
890 struct policy_handle *acct_handle,
891 struct lsa_LUID *luid)
893 NTSTATUS status;
894 struct lsa_AddPrivilegesToAccount r;
895 struct lsa_PrivilegeSet privs;
896 bool ret = true;
898 torture_comment(tctx, "\nTesting AddPrivilegesToAccount\n");
900 r.in.handle = acct_handle;
901 r.in.privs = &privs;
903 privs.count = 1;
904 privs.unknown = 0;
905 privs.set = talloc_array(tctx, struct lsa_LUIDAttribute, 1);
906 privs.set[0].luid = *luid;
907 privs.set[0].attribute = 0;
909 status = dcerpc_lsa_AddPrivilegesToAccount(p, tctx, &r);
910 if (!NT_STATUS_IS_OK(status)) {
911 torture_comment(tctx, "AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
912 return false;
915 return ret;
918 static bool test_EnumPrivsAccount(struct dcerpc_pipe *p,
919 struct torture_context *tctx,
920 struct policy_handle *handle,
921 struct policy_handle *acct_handle)
923 NTSTATUS status;
924 struct lsa_EnumPrivsAccount r;
925 struct lsa_PrivilegeSet *privs = NULL;
926 bool ret = true;
928 torture_comment(tctx, "\nTesting EnumPrivsAccount\n");
930 r.in.handle = acct_handle;
931 r.out.privs = &privs;
933 status = dcerpc_lsa_EnumPrivsAccount(p, tctx, &r);
934 if (!NT_STATUS_IS_OK(status)) {
935 torture_comment(tctx, "EnumPrivsAccount failed - %s\n", nt_errstr(status));
936 return false;
939 if (privs && privs->count > 0) {
940 int i;
941 for (i=0;i<privs->count;i++) {
942 test_LookupPrivName(p, tctx, handle,
943 &privs->set[i].luid);
946 ret &= test_RemovePrivilegesFromAccount(p, tctx, handle, acct_handle,
947 &privs->set[0].luid);
948 ret &= test_AddPrivilegesToAccount(p, tctx, acct_handle,
949 &privs->set[0].luid);
952 return ret;
955 static bool test_GetSystemAccessAccount(struct dcerpc_pipe *p,
956 struct torture_context *tctx,
957 struct policy_handle *handle,
958 struct policy_handle *acct_handle)
960 NTSTATUS status;
961 uint32_t access_mask;
962 struct lsa_GetSystemAccessAccount r;
964 torture_comment(tctx, "\nTesting GetSystemAccessAccount\n");
966 r.in.handle = acct_handle;
967 r.out.access_mask = &access_mask;
969 status = dcerpc_lsa_GetSystemAccessAccount(p, tctx, &r);
970 if (!NT_STATUS_IS_OK(status)) {
971 torture_comment(tctx, "GetSystemAccessAccount failed - %s\n", nt_errstr(status));
972 return false;
975 if (r.out.access_mask != NULL) {
976 torture_comment(tctx, "Rights:");
977 if (*(r.out.access_mask) & LSA_POLICY_MODE_INTERACTIVE)
978 torture_comment(tctx, " LSA_POLICY_MODE_INTERACTIVE");
979 if (*(r.out.access_mask) & LSA_POLICY_MODE_NETWORK)
980 torture_comment(tctx, " LSA_POLICY_MODE_NETWORK");
981 if (*(r.out.access_mask) & LSA_POLICY_MODE_BATCH)
982 torture_comment(tctx, " LSA_POLICY_MODE_BATCH");
983 if (*(r.out.access_mask) & LSA_POLICY_MODE_SERVICE)
984 torture_comment(tctx, " LSA_POLICY_MODE_SERVICE");
985 if (*(r.out.access_mask) & LSA_POLICY_MODE_PROXY)
986 torture_comment(tctx, " LSA_POLICY_MODE_PROXY");
987 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_INTERACTIVE)
988 torture_comment(tctx, " LSA_POLICY_MODE_DENY_INTERACTIVE");
989 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_NETWORK)
990 torture_comment(tctx, " LSA_POLICY_MODE_DENY_NETWORK");
991 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_BATCH)
992 torture_comment(tctx, " LSA_POLICY_MODE_DENY_BATCH");
993 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_SERVICE)
994 torture_comment(tctx, " LSA_POLICY_MODE_DENY_SERVICE");
995 if (*(r.out.access_mask) & LSA_POLICY_MODE_REMOTE_INTERACTIVE)
996 torture_comment(tctx, " LSA_POLICY_MODE_REMOTE_INTERACTIVE");
997 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_REMOTE_INTERACTIVE)
998 torture_comment(tctx, " LSA_POLICY_MODE_DENY_REMOTE_INTERACTIVE");
999 if (*(r.out.access_mask) & LSA_POLICY_MODE_ALL)
1000 torture_comment(tctx, " LSA_POLICY_MODE_ALL");
1001 if (*(r.out.access_mask) & LSA_POLICY_MODE_ALL_NT4)
1002 torture_comment(tctx, " LSA_POLICY_MODE_ALL_NT4");
1003 torture_comment(tctx, "\n");
1006 return true;
1009 static bool test_Delete(struct dcerpc_pipe *p,
1010 struct torture_context *tctx,
1011 struct policy_handle *handle)
1013 NTSTATUS status;
1014 struct lsa_Delete r;
1016 torture_comment(tctx, "\nTesting Delete\n");
1018 r.in.handle = handle;
1019 status = dcerpc_lsa_Delete(p, tctx, &r);
1020 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
1021 torture_comment(tctx, "Delete should have failed NT_STATUS_NOT_SUPPORTED - %s\n", nt_errstr(status));
1022 return false;
1025 return true;
1028 static bool test_DeleteObject(struct dcerpc_pipe *p,
1029 struct torture_context *tctx,
1030 struct policy_handle *handle)
1032 NTSTATUS status;
1033 struct lsa_DeleteObject r;
1035 torture_comment(tctx, "\nTesting DeleteObject\n");
1037 r.in.handle = handle;
1038 r.out.handle = handle;
1039 status = dcerpc_lsa_DeleteObject(p, tctx, &r);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 torture_comment(tctx, "DeleteObject failed - %s\n", nt_errstr(status));
1042 return false;
1045 return true;
1049 static bool test_CreateAccount(struct dcerpc_pipe *p,
1050 struct torture_context *tctx,
1051 struct policy_handle *handle)
1053 NTSTATUS status;
1054 struct lsa_CreateAccount r;
1055 struct dom_sid2 *newsid;
1056 struct policy_handle acct_handle;
1058 newsid = dom_sid_parse_talloc(tctx, "S-1-5-12349876-4321-2854");
1060 torture_comment(tctx, "\nTesting CreateAccount\n");
1062 r.in.handle = handle;
1063 r.in.sid = newsid;
1064 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1065 r.out.acct_handle = &acct_handle;
1067 status = dcerpc_lsa_CreateAccount(p, tctx, &r);
1068 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1069 struct lsa_OpenAccount r_o;
1070 r_o.in.handle = handle;
1071 r_o.in.sid = newsid;
1072 r_o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1073 r_o.out.acct_handle = &acct_handle;
1075 status = dcerpc_lsa_OpenAccount(p, tctx, &r_o);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 torture_comment(tctx, "OpenAccount failed - %s\n", nt_errstr(status));
1078 return false;
1080 } else if (!NT_STATUS_IS_OK(status)) {
1081 torture_comment(tctx, "CreateAccount failed - %s\n", nt_errstr(status));
1082 return false;
1085 if (!test_Delete(p, tctx, &acct_handle)) {
1086 return false;
1089 if (!test_DeleteObject(p, tctx, &acct_handle)) {
1090 return false;
1093 return true;
1096 static bool test_DeleteTrustedDomain(struct dcerpc_pipe *p,
1097 struct torture_context *tctx,
1098 struct policy_handle *handle,
1099 struct lsa_StringLarge name)
1101 NTSTATUS status;
1102 struct lsa_OpenTrustedDomainByName r;
1103 struct policy_handle trustdom_handle;
1105 r.in.handle = handle;
1106 r.in.name.string = name.string;
1107 r.in.access_mask = SEC_STD_DELETE;
1108 r.out.trustdom_handle = &trustdom_handle;
1110 status = dcerpc_lsa_OpenTrustedDomainByName(p, tctx, &r);
1111 if (!NT_STATUS_IS_OK(status)) {
1112 torture_comment(tctx, "OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1113 return false;
1116 if (!test_Delete(p, tctx, &trustdom_handle)) {
1117 return false;
1120 if (!test_DeleteObject(p, tctx, &trustdom_handle)) {
1121 return false;
1124 return true;
1127 static bool test_DeleteTrustedDomainBySid(struct dcerpc_pipe *p,
1128 struct torture_context *tctx,
1129 struct policy_handle *handle,
1130 struct dom_sid *sid)
1132 NTSTATUS status;
1133 struct lsa_DeleteTrustedDomain r;
1135 r.in.handle = handle;
1136 r.in.dom_sid = sid;
1138 status = dcerpc_lsa_DeleteTrustedDomain(p, tctx, &r);
1139 if (!NT_STATUS_IS_OK(status)) {
1140 torture_comment(tctx, "DeleteTrustedDomain failed - %s\n", nt_errstr(status));
1141 return false;
1144 return true;
1148 static bool test_CreateSecret(struct dcerpc_pipe *p,
1149 struct torture_context *tctx,
1150 struct policy_handle *handle)
1152 NTSTATUS status;
1153 struct lsa_CreateSecret r;
1154 struct lsa_OpenSecret r2;
1155 struct lsa_SetSecret r3;
1156 struct lsa_QuerySecret r4;
1157 struct lsa_SetSecret r5;
1158 struct lsa_QuerySecret r6;
1159 struct lsa_SetSecret r7;
1160 struct lsa_QuerySecret r8;
1161 struct policy_handle sec_handle, sec_handle2, sec_handle3;
1162 struct lsa_DeleteObject d_o;
1163 struct lsa_DATA_BUF buf1;
1164 struct lsa_DATA_BUF_PTR bufp1;
1165 struct lsa_DATA_BUF_PTR bufp2;
1166 DATA_BLOB enc_key;
1167 bool ret = true;
1168 DATA_BLOB session_key;
1169 NTTIME old_mtime, new_mtime;
1170 DATA_BLOB blob1, blob2;
1171 const char *secret1 = "abcdef12345699qwerty";
1172 char *secret2;
1173 const char *secret3 = "ABCDEF12345699QWERTY";
1174 char *secret4;
1175 const char *secret5 = "NEW-SAMBA4-SECRET";
1176 char *secret6;
1177 char *secname[2];
1178 int i;
1179 const int LOCAL = 0;
1180 const int GLOBAL = 1;
1182 secname[LOCAL] = talloc_asprintf(tctx, "torturesecret-%u", (uint_t)random());
1183 secname[GLOBAL] = talloc_asprintf(tctx, "G$torturesecret-%u", (uint_t)random());
1185 for (i=0; i< 2; i++) {
1186 torture_comment(tctx, "\nTesting CreateSecret of %s\n", secname[i]);
1188 init_lsa_String(&r.in.name, secname[i]);
1190 r.in.handle = handle;
1191 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1192 r.out.sec_handle = &sec_handle;
1194 status = dcerpc_lsa_CreateSecret(p, tctx, &r);
1195 if (!NT_STATUS_IS_OK(status)) {
1196 torture_comment(tctx, "CreateSecret failed - %s\n", nt_errstr(status));
1197 return false;
1200 r.in.handle = handle;
1201 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1202 r.out.sec_handle = &sec_handle3;
1204 status = dcerpc_lsa_CreateSecret(p, tctx, &r);
1205 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1206 torture_comment(tctx, "CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
1207 return false;
1210 r2.in.handle = handle;
1211 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1212 r2.in.name = r.in.name;
1213 r2.out.sec_handle = &sec_handle2;
1215 torture_comment(tctx, "Testing OpenSecret\n");
1217 status = dcerpc_lsa_OpenSecret(p, tctx, &r2);
1218 if (!NT_STATUS_IS_OK(status)) {
1219 torture_comment(tctx, "OpenSecret failed - %s\n", nt_errstr(status));
1220 return false;
1223 status = dcerpc_fetch_session_key(p, &session_key);
1224 if (!NT_STATUS_IS_OK(status)) {
1225 torture_comment(tctx, "dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
1226 return false;
1229 enc_key = sess_encrypt_string(secret1, &session_key);
1231 r3.in.sec_handle = &sec_handle;
1232 r3.in.new_val = &buf1;
1233 r3.in.old_val = NULL;
1234 r3.in.new_val->data = enc_key.data;
1235 r3.in.new_val->length = enc_key.length;
1236 r3.in.new_val->size = enc_key.length;
1238 torture_comment(tctx, "Testing SetSecret\n");
1240 status = dcerpc_lsa_SetSecret(p, tctx, &r3);
1241 if (!NT_STATUS_IS_OK(status)) {
1242 torture_comment(tctx, "SetSecret failed - %s\n", nt_errstr(status));
1243 return false;
1246 r3.in.sec_handle = &sec_handle;
1247 r3.in.new_val = &buf1;
1248 r3.in.old_val = NULL;
1249 r3.in.new_val->data = enc_key.data;
1250 r3.in.new_val->length = enc_key.length;
1251 r3.in.new_val->size = enc_key.length;
1253 /* break the encrypted data */
1254 enc_key.data[0]++;
1256 torture_comment(tctx, "Testing SetSecret with broken key\n");
1258 status = dcerpc_lsa_SetSecret(p, tctx, &r3);
1259 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
1260 torture_comment(tctx, "SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
1261 ret = false;
1264 data_blob_free(&enc_key);
1266 ZERO_STRUCT(new_mtime);
1267 ZERO_STRUCT(old_mtime);
1269 /* fetch the secret back again */
1270 r4.in.sec_handle = &sec_handle;
1271 r4.in.new_val = &bufp1;
1272 r4.in.new_mtime = &new_mtime;
1273 r4.in.old_val = NULL;
1274 r4.in.old_mtime = NULL;
1276 bufp1.buf = NULL;
1278 torture_comment(tctx, "Testing QuerySecret\n");
1279 status = dcerpc_lsa_QuerySecret(p, tctx, &r4);
1280 if (!NT_STATUS_IS_OK(status)) {
1281 torture_comment(tctx, "QuerySecret failed - %s\n", nt_errstr(status));
1282 ret = false;
1283 } else {
1284 if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
1285 torture_comment(tctx, "No secret buffer returned\n");
1286 ret = false;
1287 } else {
1288 blob1.data = r4.out.new_val->buf->data;
1289 blob1.length = r4.out.new_val->buf->size;
1291 blob2 = data_blob_talloc(tctx, NULL, blob1.length);
1293 secret2 = sess_decrypt_string(tctx,
1294 &blob1, &session_key);
1296 if (strcmp(secret1, secret2) != 0) {
1297 torture_comment(tctx, "Returned secret (r4) '%s' doesn't match '%s'\n",
1298 secret2, secret1);
1299 ret = false;
1304 enc_key = sess_encrypt_string(secret3, &session_key);
1306 r5.in.sec_handle = &sec_handle;
1307 r5.in.new_val = &buf1;
1308 r5.in.old_val = NULL;
1309 r5.in.new_val->data = enc_key.data;
1310 r5.in.new_val->length = enc_key.length;
1311 r5.in.new_val->size = enc_key.length;
1314 msleep(200);
1315 torture_comment(tctx, "Testing SetSecret (existing value should move to old)\n");
1317 status = dcerpc_lsa_SetSecret(p, tctx, &r5);
1318 if (!NT_STATUS_IS_OK(status)) {
1319 torture_comment(tctx, "SetSecret failed - %s\n", nt_errstr(status));
1320 ret = false;
1323 data_blob_free(&enc_key);
1325 ZERO_STRUCT(new_mtime);
1326 ZERO_STRUCT(old_mtime);
1328 /* fetch the secret back again */
1329 r6.in.sec_handle = &sec_handle;
1330 r6.in.new_val = &bufp1;
1331 r6.in.new_mtime = &new_mtime;
1332 r6.in.old_val = &bufp2;
1333 r6.in.old_mtime = &old_mtime;
1335 bufp1.buf = NULL;
1336 bufp2.buf = NULL;
1338 status = dcerpc_lsa_QuerySecret(p, tctx, &r6);
1339 if (!NT_STATUS_IS_OK(status)) {
1340 torture_comment(tctx, "QuerySecret failed - %s\n", nt_errstr(status));
1341 ret = false;
1342 secret4 = NULL;
1343 } else {
1345 if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
1346 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1347 torture_comment(tctx, "Both secret buffers and both times not returned\n");
1348 ret = false;
1349 secret4 = NULL;
1350 } else {
1351 blob1.data = r6.out.new_val->buf->data;
1352 blob1.length = r6.out.new_val->buf->size;
1354 blob2 = data_blob_talloc(tctx, NULL, blob1.length);
1356 secret4 = sess_decrypt_string(tctx,
1357 &blob1, &session_key);
1359 if (strcmp(secret3, secret4) != 0) {
1360 torture_comment(tctx, "Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1361 ret = false;
1364 blob1.data = r6.out.old_val->buf->data;
1365 blob1.length = r6.out.old_val->buf->length;
1367 blob2 = data_blob_talloc(tctx, NULL, blob1.length);
1369 secret2 = sess_decrypt_string(tctx,
1370 &blob1, &session_key);
1372 if (strcmp(secret1, secret2) != 0) {
1373 torture_comment(tctx, "Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1374 ret = false;
1377 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1378 torture_comment(tctx, "Returned secret (r6-%d) %s must not have same mtime for both secrets: %s != %s\n",
1380 secname[i],
1381 nt_time_string(tctx, *r6.out.old_mtime),
1382 nt_time_string(tctx, *r6.out.new_mtime));
1383 ret = false;
1388 enc_key = sess_encrypt_string(secret5, &session_key);
1390 r7.in.sec_handle = &sec_handle;
1391 r7.in.old_val = &buf1;
1392 r7.in.old_val->data = enc_key.data;
1393 r7.in.old_val->length = enc_key.length;
1394 r7.in.old_val->size = enc_key.length;
1395 r7.in.new_val = NULL;
1397 torture_comment(tctx, "Testing SetSecret of old Secret only\n");
1399 status = dcerpc_lsa_SetSecret(p, tctx, &r7);
1400 if (!NT_STATUS_IS_OK(status)) {
1401 torture_comment(tctx, "SetSecret failed - %s\n", nt_errstr(status));
1402 ret = false;
1405 data_blob_free(&enc_key);
1407 /* fetch the secret back again */
1408 r8.in.sec_handle = &sec_handle;
1409 r8.in.new_val = &bufp1;
1410 r8.in.new_mtime = &new_mtime;
1411 r8.in.old_val = &bufp2;
1412 r8.in.old_mtime = &old_mtime;
1414 bufp1.buf = NULL;
1415 bufp2.buf = NULL;
1417 status = dcerpc_lsa_QuerySecret(p, tctx, &r8);
1418 if (!NT_STATUS_IS_OK(status)) {
1419 torture_comment(tctx, "QuerySecret failed - %s\n", nt_errstr(status));
1420 ret = false;
1421 } else {
1422 if (!r8.out.new_val || !r8.out.old_val) {
1423 torture_comment(tctx, "in/out pointers not returned, despite being set on in for QuerySecret\n");
1424 ret = false;
1425 } else if (r8.out.new_val->buf != NULL) {
1426 torture_comment(tctx, "NEW secret buffer must not be returned after OLD set\n");
1427 ret = false;
1428 } else if (r8.out.old_val->buf == NULL) {
1429 torture_comment(tctx, "OLD secret buffer was not returned after OLD set\n");
1430 ret = false;
1431 } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1432 torture_comment(tctx, "Both times not returned after OLD set\n");
1433 ret = false;
1434 } else {
1435 blob1.data = r8.out.old_val->buf->data;
1436 blob1.length = r8.out.old_val->buf->size;
1438 blob2 = data_blob_talloc(tctx, NULL, blob1.length);
1440 secret6 = sess_decrypt_string(tctx,
1441 &blob1, &session_key);
1443 if (strcmp(secret5, secret6) != 0) {
1444 torture_comment(tctx, "Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1445 ret = false;
1448 if (*r8.out.new_mtime != *r8.out.old_mtime) {
1449 torture_comment(tctx, "Returned secret (r8) %s did not had same mtime for both secrets: %s != %s\n",
1450 secname[i],
1451 nt_time_string(tctx, *r8.out.old_mtime),
1452 nt_time_string(tctx, *r8.out.new_mtime));
1453 ret = false;
1458 if (!test_Delete(p, tctx, &sec_handle)) {
1459 ret = false;
1462 if (!test_DeleteObject(p, tctx, &sec_handle)) {
1463 return false;
1466 d_o.in.handle = &sec_handle2;
1467 d_o.out.handle = &sec_handle2;
1468 status = dcerpc_lsa_DeleteObject(p, tctx, &d_o);
1469 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1470 torture_comment(tctx, "Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1471 ret = false;
1472 } else {
1474 torture_comment(tctx, "Testing OpenSecret of just-deleted secret\n");
1476 status = dcerpc_lsa_OpenSecret(p, tctx, &r2);
1477 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1478 torture_comment(tctx, "OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1479 ret = false;
1485 return ret;
1489 static bool test_EnumAccountRights(struct dcerpc_pipe *p,
1490 struct torture_context *tctx,
1491 struct policy_handle *acct_handle,
1492 struct dom_sid *sid)
1494 NTSTATUS status;
1495 struct lsa_EnumAccountRights r;
1496 struct lsa_RightSet rights;
1498 torture_comment(tctx, "\nTesting EnumAccountRights\n");
1500 r.in.handle = acct_handle;
1501 r.in.sid = sid;
1502 r.out.rights = &rights;
1504 status = dcerpc_lsa_EnumAccountRights(p, tctx, &r);
1505 if (!NT_STATUS_IS_OK(status)) {
1506 torture_comment(tctx, "EnumAccountRights of %s failed - %s\n",
1507 dom_sid_string(tctx, sid), nt_errstr(status));
1508 return false;
1511 return true;
1515 static bool test_QuerySecurity(struct dcerpc_pipe *p,
1516 struct torture_context *tctx,
1517 struct policy_handle *handle,
1518 struct policy_handle *acct_handle)
1520 NTSTATUS status;
1521 struct lsa_QuerySecurity r;
1522 struct sec_desc_buf *sdbuf = NULL;
1524 if (torture_setting_bool(tctx, "samba4", false)) {
1525 torture_comment(tctx, "\nskipping QuerySecurity test against Samba4\n");
1526 return true;
1529 torture_comment(tctx, "\nTesting QuerySecurity\n");
1531 r.in.handle = acct_handle;
1532 r.in.sec_info = SECINFO_OWNER |
1533 SECINFO_GROUP |
1534 SECINFO_DACL;
1535 r.out.sdbuf = &sdbuf;
1537 status = dcerpc_lsa_QuerySecurity(p, tctx, &r);
1538 if (!NT_STATUS_IS_OK(status)) {
1539 torture_comment(tctx, "QuerySecurity failed - %s\n", nt_errstr(status));
1540 return false;
1543 return true;
1546 static bool test_OpenAccount(struct dcerpc_pipe *p,
1547 struct torture_context *tctx,
1548 struct policy_handle *handle,
1549 struct dom_sid *sid)
1551 NTSTATUS status;
1552 struct lsa_OpenAccount r;
1553 struct policy_handle acct_handle;
1555 torture_comment(tctx, "\nTesting OpenAccount\n");
1557 r.in.handle = handle;
1558 r.in.sid = sid;
1559 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1560 r.out.acct_handle = &acct_handle;
1562 status = dcerpc_lsa_OpenAccount(p, tctx, &r);
1563 if (!NT_STATUS_IS_OK(status)) {
1564 torture_comment(tctx, "OpenAccount failed - %s\n", nt_errstr(status));
1565 return false;
1568 if (!test_EnumPrivsAccount(p, tctx, handle, &acct_handle)) {
1569 return false;
1572 if (!test_GetSystemAccessAccount(p, tctx, handle, &acct_handle)) {
1573 return false;
1576 if (!test_QuerySecurity(p, tctx, handle, &acct_handle)) {
1577 return false;
1580 return true;
1583 static bool test_EnumAccounts(struct dcerpc_pipe *p,
1584 struct torture_context *tctx,
1585 struct policy_handle *handle)
1587 NTSTATUS status;
1588 struct lsa_EnumAccounts r;
1589 struct lsa_SidArray sids1, sids2;
1590 uint32_t resume_handle = 0;
1591 int i;
1592 bool ret = true;
1594 torture_comment(tctx, "\nTesting EnumAccounts\n");
1596 r.in.handle = handle;
1597 r.in.resume_handle = &resume_handle;
1598 r.in.num_entries = 100;
1599 r.out.resume_handle = &resume_handle;
1600 r.out.sids = &sids1;
1602 resume_handle = 0;
1603 while (true) {
1604 status = dcerpc_lsa_EnumAccounts(p, tctx, &r);
1605 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1606 break;
1608 if (!NT_STATUS_IS_OK(status)) {
1609 torture_comment(tctx, "EnumAccounts failed - %s\n", nt_errstr(status));
1610 return false;
1613 if (!test_LookupSids(p, tctx, handle, &sids1)) {
1614 return false;
1617 if (!test_LookupSids2(p, tctx, handle, &sids1)) {
1618 return false;
1621 /* Can't test lookupSids3 here, as clearly we must not
1622 * be on schannel, or we would not be able to do the
1623 * rest */
1625 torture_comment(tctx, "Testing all accounts\n");
1626 for (i=0;i<sids1.num_sids;i++) {
1627 ret &= test_OpenAccount(p, tctx, handle, sids1.sids[i].sid);
1628 ret &= test_EnumAccountRights(p, tctx, handle, sids1.sids[i].sid);
1630 torture_comment(tctx, "\n");
1633 if (sids1.num_sids < 3) {
1634 return ret;
1637 torture_comment(tctx, "Trying EnumAccounts partial listing (asking for 1 at 2)\n");
1638 resume_handle = 2;
1639 r.in.num_entries = 1;
1640 r.out.sids = &sids2;
1642 status = dcerpc_lsa_EnumAccounts(p, tctx, &r);
1643 if (!NT_STATUS_IS_OK(status)) {
1644 torture_comment(tctx, "EnumAccounts failed - %s\n", nt_errstr(status));
1645 return false;
1648 if (sids2.num_sids != 1) {
1649 torture_comment(tctx, "Returned wrong number of entries (%d)\n", sids2.num_sids);
1650 return false;
1653 return true;
1656 static bool test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1657 struct torture_context *tctx,
1658 struct policy_handle *handle,
1659 struct lsa_String *priv_name)
1661 struct lsa_LookupPrivDisplayName r;
1662 NTSTATUS status;
1663 /* produce a reasonable range of language output without screwing up
1664 terminals */
1665 uint16_t language_id = (random() % 4) + 0x409;
1666 uint16_t returned_language_id = 0;
1667 struct lsa_StringLarge *disp_name = NULL;
1669 torture_comment(tctx, "\nTesting LookupPrivDisplayName(%s)\n", priv_name->string);
1671 r.in.handle = handle;
1672 r.in.name = priv_name;
1673 r.in.language_id = language_id;
1674 r.in.language_id_sys = 0;
1675 r.out.returned_language_id = &returned_language_id;
1676 r.out.disp_name = &disp_name;
1678 status = dcerpc_lsa_LookupPrivDisplayName(p, tctx, &r);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 torture_comment(tctx, "LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1681 return false;
1683 torture_comment(tctx, "%s -> \"%s\" (language 0x%x/0x%x)\n",
1684 priv_name->string, disp_name->string,
1685 r.in.language_id, *r.out.returned_language_id);
1687 return true;
1690 static bool test_EnumAccountsWithUserRight(struct dcerpc_pipe *p,
1691 struct torture_context *tctx,
1692 struct policy_handle *handle,
1693 struct lsa_String *priv_name)
1695 struct lsa_EnumAccountsWithUserRight r;
1696 struct lsa_SidArray sids;
1697 NTSTATUS status;
1699 ZERO_STRUCT(sids);
1701 torture_comment(tctx, "\nTesting EnumAccountsWithUserRight(%s)\n", priv_name->string);
1703 r.in.handle = handle;
1704 r.in.name = priv_name;
1705 r.out.sids = &sids;
1707 status = dcerpc_lsa_EnumAccountsWithUserRight(p, tctx, &r);
1709 /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1710 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1711 return true;
1714 if (!NT_STATUS_IS_OK(status)) {
1715 torture_comment(tctx, "EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1716 return false;
1719 return true;
1723 static bool test_EnumPrivs(struct dcerpc_pipe *p,
1724 struct torture_context *tctx,
1725 struct policy_handle *handle)
1727 NTSTATUS status;
1728 struct lsa_EnumPrivs r;
1729 struct lsa_PrivArray privs1;
1730 uint32_t resume_handle = 0;
1731 int i;
1732 bool ret = true;
1734 torture_comment(tctx, "\nTesting EnumPrivs\n");
1736 r.in.handle = handle;
1737 r.in.resume_handle = &resume_handle;
1738 r.in.max_count = 100;
1739 r.out.resume_handle = &resume_handle;
1740 r.out.privs = &privs1;
1742 resume_handle = 0;
1743 status = dcerpc_lsa_EnumPrivs(p, tctx, &r);
1744 if (!NT_STATUS_IS_OK(status)) {
1745 torture_comment(tctx, "EnumPrivs failed - %s\n", nt_errstr(status));
1746 return false;
1749 for (i = 0; i< privs1.count; i++) {
1750 test_LookupPrivDisplayName(p, tctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1751 test_LookupPrivValue(p, tctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1752 if (!test_EnumAccountsWithUserRight(p, tctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
1753 ret = false;
1757 return ret;
1760 static bool test_QueryForestTrustInformation(struct dcerpc_pipe *p,
1761 struct torture_context *tctx,
1762 struct policy_handle *handle,
1763 const char *trusted_domain_name)
1765 bool ret = true;
1766 struct lsa_lsaRQueryForestTrustInformation r;
1767 NTSTATUS status;
1768 struct lsa_String string;
1769 struct lsa_ForestTrustInformation info, *info_ptr;
1771 torture_comment(tctx, "\nTesting lsaRQueryForestTrustInformation\n");
1773 if (torture_setting_bool(tctx, "samba4", false)) {
1774 torture_comment(tctx, "skipping QueryForestTrustInformation against Samba4\n");
1775 return true;
1778 ZERO_STRUCT(string);
1780 if (trusted_domain_name) {
1781 init_lsa_String(&string, trusted_domain_name);
1784 info_ptr = &info;
1786 r.in.handle = handle;
1787 r.in.trusted_domain_name = &string;
1788 r.in.unknown = 0;
1789 r.out.forest_trust_info = &info_ptr;
1791 status = dcerpc_lsa_lsaRQueryForestTrustInformation(p, tctx, &r);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 torture_comment(tctx, "lsaRQueryForestTrustInformation of %s failed - %s\n", trusted_domain_name, nt_errstr(status));
1795 ret = false;
1798 return ret;
1801 static bool test_query_each_TrustDomEx(struct dcerpc_pipe *p,
1802 struct torture_context *tctx,
1803 struct policy_handle *handle,
1804 struct lsa_DomainListEx *domains)
1806 int i;
1807 bool ret = true;
1809 for (i=0; i< domains->count; i++) {
1811 if (domains->domains[i].trust_attributes & NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1812 ret &= test_QueryForestTrustInformation(p, tctx, handle,
1813 domains->domains[i].domain_name.string);
1817 return ret;
1820 static bool test_query_each_TrustDom(struct dcerpc_pipe *p,
1821 struct torture_context *tctx,
1822 struct policy_handle *handle,
1823 struct lsa_DomainList *domains)
1825 NTSTATUS status;
1826 int i,j;
1827 bool ret = true;
1829 torture_comment(tctx, "\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1830 for (i=0; i< domains->count; i++) {
1831 struct lsa_OpenTrustedDomain trust;
1832 struct lsa_OpenTrustedDomainByName trust_by_name;
1833 struct policy_handle trustdom_handle;
1834 struct policy_handle handle2;
1835 struct lsa_Close c;
1836 struct lsa_CloseTrustedDomainEx c_trust;
1837 int levels [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
1838 int ok[] = {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1};
1840 if (domains->domains[i].sid) {
1841 trust.in.handle = handle;
1842 trust.in.sid = domains->domains[i].sid;
1843 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1844 trust.out.trustdom_handle = &trustdom_handle;
1846 status = dcerpc_lsa_OpenTrustedDomain(p, tctx, &trust);
1848 if (!NT_STATUS_IS_OK(status)) {
1849 torture_comment(tctx, "OpenTrustedDomain failed - %s\n", nt_errstr(status));
1850 return false;
1853 c.in.handle = &trustdom_handle;
1854 c.out.handle = &handle2;
1856 c_trust.in.handle = &trustdom_handle;
1857 c_trust.out.handle = &handle2;
1859 for (j=0; j < ARRAY_SIZE(levels); j++) {
1860 struct lsa_QueryTrustedDomainInfo q;
1861 union lsa_TrustedDomainInfo *info = NULL;
1862 q.in.trustdom_handle = &trustdom_handle;
1863 q.in.level = levels[j];
1864 q.out.info = &info;
1865 status = dcerpc_lsa_QueryTrustedDomainInfo(p, tctx, &q);
1866 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1867 torture_comment(tctx, "QueryTrustedDomainInfo level %d failed - %s\n",
1868 levels[j], nt_errstr(status));
1869 ret = false;
1870 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1871 torture_comment(tctx, "QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1872 levels[j], nt_errstr(status));
1873 ret = false;
1877 status = dcerpc_lsa_CloseTrustedDomainEx(p, tctx, &c_trust);
1878 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1879 torture_comment(tctx, "Expected CloseTrustedDomainEx to return NT_STATUS_NOT_IMPLEMENTED, instead - %s\n", nt_errstr(status));
1880 return false;
1883 c.in.handle = &trustdom_handle;
1884 c.out.handle = &handle2;
1886 status = dcerpc_lsa_Close(p, tctx, &c);
1887 if (!NT_STATUS_IS_OK(status)) {
1888 torture_comment(tctx, "Close of trusted domain failed - %s\n", nt_errstr(status));
1889 return false;
1892 for (j=0; j < ARRAY_SIZE(levels); j++) {
1893 struct lsa_QueryTrustedDomainInfoBySid q;
1894 union lsa_TrustedDomainInfo *info = NULL;
1896 if (!domains->domains[i].sid) {
1897 continue;
1900 q.in.handle = handle;
1901 q.in.dom_sid = domains->domains[i].sid;
1902 q.in.level = levels[j];
1903 q.out.info = &info;
1905 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, tctx, &q);
1906 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1907 torture_comment(tctx, "QueryTrustedDomainInfoBySid level %d failed - %s\n",
1908 levels[j], nt_errstr(status));
1909 ret = false;
1910 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1911 torture_comment(tctx, "QueryTrustedDomainInfoBySid level %d unexpectedly succeeded - %s\n",
1912 levels[j], nt_errstr(status));
1913 ret = false;
1918 trust_by_name.in.handle = handle;
1919 trust_by_name.in.name.string = domains->domains[i].name.string;
1920 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1921 trust_by_name.out.trustdom_handle = &trustdom_handle;
1923 status = dcerpc_lsa_OpenTrustedDomainByName(p, tctx, &trust_by_name);
1925 if (!NT_STATUS_IS_OK(status)) {
1926 torture_comment(tctx, "OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1927 return false;
1930 for (j=0; j < ARRAY_SIZE(levels); j++) {
1931 struct lsa_QueryTrustedDomainInfo q;
1932 union lsa_TrustedDomainInfo *info = NULL;
1933 q.in.trustdom_handle = &trustdom_handle;
1934 q.in.level = levels[j];
1935 q.out.info = &info;
1936 status = dcerpc_lsa_QueryTrustedDomainInfo(p, tctx, &q);
1937 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1938 torture_comment(tctx, "QueryTrustedDomainInfo level %d failed - %s\n",
1939 levels[j], nt_errstr(status));
1940 ret = false;
1941 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1942 torture_comment(tctx, "QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
1943 levels[j], nt_errstr(status));
1944 ret = false;
1948 c.in.handle = &trustdom_handle;
1949 c.out.handle = &handle2;
1951 status = dcerpc_lsa_Close(p, tctx, &c);
1952 if (!NT_STATUS_IS_OK(status)) {
1953 torture_comment(tctx, "Close of trusted domain failed - %s\n", nt_errstr(status));
1954 return false;
1957 for (j=0; j < ARRAY_SIZE(levels); j++) {
1958 struct lsa_QueryTrustedDomainInfoByName q;
1959 union lsa_TrustedDomainInfo *info = NULL;
1960 struct lsa_String name;
1962 name.string = domains->domains[i].name.string;
1964 q.in.handle = handle;
1965 q.in.trusted_domain = &name;
1966 q.in.level = levels[j];
1967 q.out.info = &info;
1968 status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, tctx, &q);
1969 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1970 torture_comment(tctx, "QueryTrustedDomainInfoByName level %d failed - %s\n",
1971 levels[j], nt_errstr(status));
1972 ret = false;
1973 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1974 torture_comment(tctx, "QueryTrustedDomainInfoByName level %d unexpectedly succeeded - %s\n",
1975 levels[j], nt_errstr(status));
1976 ret = false;
1980 return ret;
1983 static bool test_EnumTrustDom(struct dcerpc_pipe *p,
1984 struct torture_context *tctx,
1985 struct policy_handle *handle)
1987 struct lsa_EnumTrustDom r;
1988 struct lsa_EnumTrustedDomainsEx r_ex;
1989 NTSTATUS enum_status;
1990 uint32_t resume_handle = 0;
1991 struct lsa_DomainList domains;
1992 struct lsa_DomainListEx domains_ex;
1993 bool ret = true;
1995 torture_comment(tctx, "\nTesting EnumTrustDom\n");
1997 r.in.handle = handle;
1998 r.in.resume_handle = &resume_handle;
1999 r.in.max_size = 0;
2000 r.out.domains = &domains;
2001 r.out.resume_handle = &resume_handle;
2003 enum_status = dcerpc_lsa_EnumTrustDom(p, tctx, &r);
2005 if (NT_STATUS_IS_OK(enum_status)) {
2006 if (domains.count == 0) {
2007 torture_comment(tctx, "EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
2008 return false;
2010 } else if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
2011 torture_comment(tctx, "EnumTrustDom of zero size failed - %s\n", nt_errstr(enum_status));
2012 return false;
2015 /* Start from the bottom again */
2016 resume_handle = 0;
2018 do {
2019 r.in.handle = handle;
2020 r.in.resume_handle = &resume_handle;
2021 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
2022 r.out.domains = &domains;
2023 r.out.resume_handle = &resume_handle;
2025 enum_status = dcerpc_lsa_EnumTrustDom(p, tctx, &r);
2027 /* NO_MORE_ENTRIES is allowed */
2028 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
2029 if (domains.count == 0) {
2030 return true;
2032 torture_comment(tctx, "EnumTrustDom failed - should have returned 0 trusted domains with 'NT_STATUS_NO_MORE_ENTRIES'\n");
2033 return false;
2034 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
2035 /* Windows 2003 gets this off by one on the first run */
2036 if (r.out.domains->count < 3 || r.out.domains->count > 4) {
2037 torture_comment(tctx, "EnumTrustDom didn't fill the buffer we "
2038 "asked it to (got %d, expected %d / %d == %d entries)\n",
2039 r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3,
2040 LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
2041 ret = false;
2043 } else if (!NT_STATUS_IS_OK(enum_status)) {
2044 torture_comment(tctx, "EnumTrustDom failed - %s\n", nt_errstr(enum_status));
2045 return false;
2048 if (domains.count == 0) {
2049 torture_comment(tctx, "EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
2050 return false;
2053 ret &= test_query_each_TrustDom(p, tctx, handle, &domains);
2055 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
2057 torture_comment(tctx, "\nTesting EnumTrustedDomainsEx\n");
2059 r_ex.in.handle = handle;
2060 r_ex.in.resume_handle = &resume_handle;
2061 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
2062 r_ex.out.domains = &domains_ex;
2063 r_ex.out.resume_handle = &resume_handle;
2065 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, tctx, &r_ex);
2067 if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
2068 torture_comment(tctx, "EnumTrustedDomainEx of zero size failed - %s\n", nt_errstr(enum_status));
2069 return false;
2072 resume_handle = 0;
2073 do {
2074 r_ex.in.handle = handle;
2075 r_ex.in.resume_handle = &resume_handle;
2076 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
2077 r_ex.out.domains = &domains_ex;
2078 r_ex.out.resume_handle = &resume_handle;
2080 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, tctx, &r_ex);
2082 /* NO_MORE_ENTRIES is allowed */
2083 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
2084 if (domains_ex.count == 0) {
2085 return true;
2087 torture_comment(tctx, "EnumTrustDomainsEx failed - should have returned 0 trusted domains with 'NT_STATUS_NO_MORE_ENTRIES'\n");
2088 return false;
2089 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
2090 /* Windows 2003 gets this off by one on the first run */
2091 if (r_ex.out.domains->count < 3 || r_ex.out.domains->count > 4) {
2092 torture_comment(tctx, "EnumTrustDom didn't fill the buffer we "
2093 "asked it to (got %d, expected %d / %d == %d entries)\n",
2094 r_ex.out.domains->count,
2095 r_ex.in.max_size,
2096 LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER,
2097 r_ex.in.max_size / LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER);
2099 } else if (!NT_STATUS_IS_OK(enum_status)) {
2100 torture_comment(tctx, "EnumTrustedDomainEx failed - %s\n", nt_errstr(enum_status));
2101 return false;
2104 if (domains_ex.count == 0) {
2105 torture_comment(tctx, "EnumTrustDomainEx failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
2106 return false;
2109 ret &= test_query_each_TrustDomEx(p, tctx, handle, &domains_ex);
2111 } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
2113 return ret;
2116 static bool test_CreateTrustedDomain(struct dcerpc_pipe *p,
2117 struct torture_context *tctx,
2118 struct policy_handle *handle)
2120 NTSTATUS status;
2121 bool ret = true;
2122 struct lsa_CreateTrustedDomain r;
2123 struct lsa_DomainInfo trustinfo;
2124 struct dom_sid *domsid[12];
2125 struct policy_handle trustdom_handle[12];
2126 struct lsa_QueryTrustedDomainInfo q;
2127 union lsa_TrustedDomainInfo *info = NULL;
2128 int i;
2130 torture_comment(tctx, "\nTesting CreateTrustedDomain for 12 domains\n");
2132 if (!test_EnumTrustDom(p, tctx, handle)) {
2133 ret = false;
2136 for (i=0; i< 12; i++) {
2137 char *trust_name = talloc_asprintf(tctx, "torturedom%02d", i);
2138 char *trust_sid = talloc_asprintf(tctx, "S-1-5-21-97398-379795-100%02d", i);
2140 domsid[i] = dom_sid_parse_talloc(tctx, trust_sid);
2142 trustinfo.sid = domsid[i];
2143 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
2145 r.in.policy_handle = handle;
2146 r.in.info = &trustinfo;
2147 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2148 r.out.trustdom_handle = &trustdom_handle[i];
2150 status = dcerpc_lsa_CreateTrustedDomain(p, tctx, &r);
2151 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
2152 test_DeleteTrustedDomain(p, tctx, handle, trustinfo.name);
2153 status = dcerpc_lsa_CreateTrustedDomain(p, tctx, &r);
2155 if (!NT_STATUS_IS_OK(status)) {
2156 torture_comment(tctx, "CreateTrustedDomain failed - %s\n", nt_errstr(status));
2157 ret = false;
2158 } else {
2160 q.in.trustdom_handle = &trustdom_handle[i];
2161 q.in.level = LSA_TRUSTED_DOMAIN_INFO_INFO_EX;
2162 q.out.info = &info;
2163 status = dcerpc_lsa_QueryTrustedDomainInfo(p, tctx, &q);
2164 if (!NT_STATUS_IS_OK(status)) {
2165 torture_comment(tctx, "QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
2166 ret = false;
2167 } else if (!q.out.info) {
2168 ret = false;
2169 } else {
2170 if (strcmp(info->info_ex.netbios_name.string, trustinfo.name.string) != 0) {
2171 torture_comment(tctx, "QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
2172 info->info_ex.netbios_name.string, trustinfo.name.string);
2173 ret = false;
2175 if (info->info_ex.trust_type != LSA_TRUST_TYPE_DOWNLEVEL) {
2176 torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust type %d != %d\n",
2177 trust_name, info->info_ex.trust_type, LSA_TRUST_TYPE_DOWNLEVEL);
2178 ret = false;
2180 if (info->info_ex.trust_attributes != 0) {
2181 torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust attributes %d != %d\n",
2182 trust_name, info->info_ex.trust_attributes, 0);
2183 ret = false;
2185 if (info->info_ex.trust_direction != LSA_TRUST_DIRECTION_OUTBOUND) {
2186 torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust direction %d != %d\n",
2187 trust_name, info->info_ex.trust_direction, LSA_TRUST_DIRECTION_OUTBOUND);
2188 ret = false;
2194 /* now that we have some domains to look over, we can test the enum calls */
2195 if (!test_EnumTrustDom(p, tctx, handle)) {
2196 ret = false;
2199 for (i=0; i<12; i++) {
2200 if (!test_DeleteTrustedDomainBySid(p, tctx, handle, domsid[i])) {
2201 ret = false;
2205 return ret;
2208 static bool test_CreateTrustedDomainEx2(struct dcerpc_pipe *p,
2209 struct torture_context *tctx,
2210 struct policy_handle *handle)
2212 NTSTATUS status;
2213 bool ret = true;
2214 struct lsa_CreateTrustedDomainEx2 r;
2215 struct lsa_TrustDomainInfoInfoEx trustinfo;
2216 struct lsa_TrustDomainInfoAuthInfoInternal authinfo;
2217 struct trustDomainPasswords auth_struct;
2218 DATA_BLOB auth_blob;
2219 struct dom_sid *domsid[12];
2220 struct policy_handle trustdom_handle[12];
2221 struct lsa_QueryTrustedDomainInfo q;
2222 union lsa_TrustedDomainInfo *info = NULL;
2223 DATA_BLOB session_key;
2224 enum ndr_err_code ndr_err;
2225 int i;
2227 torture_comment(tctx, "\nTesting CreateTrustedDomainEx2 for 12 domains\n");
2229 status = dcerpc_fetch_session_key(p, &session_key);
2230 if (!NT_STATUS_IS_OK(status)) {
2231 torture_comment(tctx, "dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
2232 return false;
2235 for (i=0; i< 12; i++) {
2236 char *trust_name = talloc_asprintf(tctx, "torturedom%02d", i);
2237 char *trust_name_dns = talloc_asprintf(tctx, "torturedom%02d.samba.example.com", i);
2238 char *trust_sid = talloc_asprintf(tctx, "S-1-5-21-97398-379795-100%02d", i);
2240 domsid[i] = dom_sid_parse_talloc(tctx, trust_sid);
2242 trustinfo.sid = domsid[i];
2243 trustinfo.netbios_name.string = trust_name;
2244 trustinfo.domain_name.string = trust_name_dns;
2246 /* Create inbound, some outbound, and some
2247 * bi-directional trusts in a repeating pattern based
2248 * on i */
2250 /* 1 == inbound, 2 == outbound, 3 == both */
2251 trustinfo.trust_direction = (i % 3) + 1;
2253 /* Try different trust types too */
2255 /* 1 == downlevel (NT4), 2 == uplevel (ADS), 3 == MIT (kerberos but not AD) */
2256 trustinfo.trust_type = (((i / 3) + 1) % 3) + 1;
2258 trustinfo.trust_attributes = LSA_TRUST_ATTRIBUTE_USES_RC4_ENCRYPTION;
2260 generate_random_buffer(auth_struct.confounder, sizeof(auth_struct.confounder));
2262 auth_struct.outgoing.count = 0;
2263 auth_struct.incoming.count = 0;
2265 ndr_err = ndr_push_struct_blob(&auth_blob, tctx, lp_iconv_convenience(tctx->lp_ctx), &auth_struct,
2266 (ndr_push_flags_fn_t)ndr_push_trustDomainPasswords);
2267 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2268 torture_comment(tctx, "ndr_push_struct_blob of trustDomainPasswords structure failed");
2269 ret = false;
2272 arcfour_crypt_blob(auth_blob.data, auth_blob.length, &session_key);
2274 authinfo.auth_blob.size = auth_blob.length;
2275 authinfo.auth_blob.data = auth_blob.data;
2277 r.in.policy_handle = handle;
2278 r.in.info = &trustinfo;
2279 r.in.auth_info = &authinfo;
2280 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2281 r.out.trustdom_handle = &trustdom_handle[i];
2283 status = dcerpc_lsa_CreateTrustedDomainEx2(p, tctx, &r);
2284 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
2285 test_DeleteTrustedDomain(p, tctx, handle, trustinfo.netbios_name);
2286 status = dcerpc_lsa_CreateTrustedDomainEx2(p, tctx, &r);
2288 if (!NT_STATUS_IS_OK(status)) {
2289 torture_comment(tctx, "CreateTrustedDomainEx failed2 - %s\n", nt_errstr(status));
2290 ret = false;
2291 } else {
2293 q.in.trustdom_handle = &trustdom_handle[i];
2294 q.in.level = LSA_TRUSTED_DOMAIN_INFO_INFO_EX;
2295 q.out.info = &info;
2296 status = dcerpc_lsa_QueryTrustedDomainInfo(p, tctx, &q);
2297 if (!NT_STATUS_IS_OK(status)) {
2298 torture_comment(tctx, "QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
2299 ret = false;
2300 } else if (!q.out.info) {
2301 torture_comment(tctx, "QueryTrustedDomainInfo level 1 failed to return an info pointer\n");
2302 ret = false;
2303 } else {
2304 if (strcmp(info->info_ex.netbios_name.string, trustinfo.netbios_name.string) != 0) {
2305 torture_comment(tctx, "QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
2306 info->info_ex.netbios_name.string, trustinfo.netbios_name.string);
2307 ret = false;
2309 if (info->info_ex.trust_type != trustinfo.trust_type) {
2310 torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust type %d != %d\n",
2311 trust_name, info->info_ex.trust_type, trustinfo.trust_type);
2312 ret = false;
2314 if (info->info_ex.trust_attributes != LSA_TRUST_ATTRIBUTE_USES_RC4_ENCRYPTION) {
2315 torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust attributes %d != %d\n",
2316 trust_name, info->info_ex.trust_attributes, LSA_TRUST_ATTRIBUTE_USES_RC4_ENCRYPTION);
2317 ret = false;
2319 if (info->info_ex.trust_direction != trustinfo.trust_direction) {
2320 torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust direction %d != %d\n",
2321 trust_name, info->info_ex.trust_direction, trustinfo.trust_direction);
2322 ret = false;
2328 /* now that we have some domains to look over, we can test the enum calls */
2329 if (!test_EnumTrustDom(p, tctx, handle)) {
2330 torture_comment(tctx, "test_EnumTrustDom failed\n");
2331 ret = false;
2334 for (i=0; i<12; i++) {
2335 if (!test_DeleteTrustedDomainBySid(p, tctx, handle, domsid[i])) {
2336 torture_comment(tctx, "test_DeleteTrustedDomainBySid failed\n");
2337 ret = false;
2341 return ret;
2344 static bool test_QueryDomainInfoPolicy(struct dcerpc_pipe *p,
2345 struct torture_context *tctx,
2346 struct policy_handle *handle)
2348 struct lsa_QueryDomainInformationPolicy r;
2349 union lsa_DomainInformationPolicy *info = NULL;
2350 NTSTATUS status;
2351 int i;
2352 bool ret = true;
2354 torture_comment(tctx, "\nTesting QueryDomainInformationPolicy\n");
2356 for (i=2;i<4;i++) {
2357 r.in.handle = handle;
2358 r.in.level = i;
2359 r.out.info = &info;
2361 torture_comment(tctx, "\nTrying QueryDomainInformationPolicy level %d\n", i);
2363 status = dcerpc_lsa_QueryDomainInformationPolicy(p, tctx, &r);
2365 /* If the server does not support EFS, then this is the correct return */
2366 if (i == LSA_DOMAIN_INFO_POLICY_EFS && NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2367 continue;
2368 } else if (!NT_STATUS_IS_OK(status)) {
2369 torture_comment(tctx, "QueryDomainInformationPolicy failed - %s\n", nt_errstr(status));
2370 ret = false;
2371 continue;
2375 return ret;
2379 static bool test_QueryInfoPolicyCalls( bool version2,
2380 struct dcerpc_pipe *p,
2381 struct torture_context *tctx,
2382 struct policy_handle *handle)
2384 struct lsa_QueryInfoPolicy r;
2385 union lsa_PolicyInformation *info = NULL;
2386 NTSTATUS status;
2387 int i;
2388 bool ret = true;
2390 if (version2)
2391 torture_comment(tctx, "\nTesting QueryInfoPolicy2\n");
2392 else
2393 torture_comment(tctx, "\nTesting QueryInfoPolicy\n");
2395 for (i=1;i<=14;i++) {
2396 r.in.handle = handle;
2397 r.in.level = i;
2398 r.out.info = &info;
2400 if (version2)
2401 torture_comment(tctx, "\nTrying QueryInfoPolicy2 level %d\n", i);
2402 else
2403 torture_comment(tctx, "\nTrying QueryInfoPolicy level %d\n", i);
2405 if (version2)
2406 /* We can perform the cast, because both types are
2407 structurally equal */
2408 status = dcerpc_lsa_QueryInfoPolicy2(p, tctx,
2409 (struct lsa_QueryInfoPolicy2*) &r);
2410 else
2411 status = dcerpc_lsa_QueryInfoPolicy(p, tctx, &r);
2413 switch (i) {
2414 case LSA_POLICY_INFO_MOD:
2415 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2416 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2417 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2418 torture_comment(tctx, "Server should have failed level %u: %s\n", i, nt_errstr(status));
2419 ret = false;
2421 break;
2422 case LSA_POLICY_INFO_DOMAIN:
2423 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2424 case LSA_POLICY_INFO_L_ACCOUNT_DOMAIN:
2425 case LSA_POLICY_INFO_DNS_INT:
2426 case LSA_POLICY_INFO_DNS:
2427 case LSA_POLICY_INFO_REPLICA:
2428 case LSA_POLICY_INFO_QUOTA:
2429 case LSA_POLICY_INFO_ROLE:
2430 case LSA_POLICY_INFO_AUDIT_LOG:
2431 case LSA_POLICY_INFO_AUDIT_EVENTS:
2432 case LSA_POLICY_INFO_PD:
2433 if (!NT_STATUS_IS_OK(status)) {
2434 if (version2)
2435 torture_comment(tctx, "QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2436 else
2437 torture_comment(tctx, "QueryInfoPolicy failed - %s\n", nt_errstr(status));
2438 ret = false;
2440 break;
2441 default:
2442 if (torture_setting_bool(tctx, "samba4", false)) {
2443 /* Other levels not implemented yet */
2444 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2445 if (version2)
2446 torture_comment(tctx, "QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2447 else
2448 torture_comment(tctx, "QueryInfoPolicy failed - %s\n", nt_errstr(status));
2449 ret = false;
2451 } else if (!NT_STATUS_IS_OK(status)) {
2452 if (version2)
2453 torture_comment(tctx, "QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2454 else
2455 torture_comment(tctx, "QueryInfoPolicy failed - %s\n", nt_errstr(status));
2456 ret = false;
2458 break;
2461 if (NT_STATUS_IS_OK(status) && (i == LSA_POLICY_INFO_DNS
2462 || i == LSA_POLICY_INFO_DNS_INT)) {
2463 /* Let's look up some of these names */
2465 struct lsa_TransNameArray tnames;
2466 tnames.count = 14;
2467 tnames.names = talloc_zero_array(tctx, struct lsa_TranslatedName, tnames.count);
2468 tnames.names[0].name.string = info->dns.name.string;
2469 tnames.names[0].sid_type = SID_NAME_DOMAIN;
2470 tnames.names[1].name.string = info->dns.dns_domain.string;
2471 tnames.names[1].sid_type = SID_NAME_DOMAIN;
2472 tnames.names[2].name.string = talloc_asprintf(tctx, "%s\\", info->dns.name.string);
2473 tnames.names[2].sid_type = SID_NAME_DOMAIN;
2474 tnames.names[3].name.string = talloc_asprintf(tctx, "%s\\", info->dns.dns_domain.string);
2475 tnames.names[3].sid_type = SID_NAME_DOMAIN;
2476 tnames.names[4].name.string = talloc_asprintf(tctx, "%s\\guest", info->dns.name.string);
2477 tnames.names[4].sid_type = SID_NAME_USER;
2478 tnames.names[5].name.string = talloc_asprintf(tctx, "%s\\krbtgt", info->dns.name.string);
2479 tnames.names[5].sid_type = SID_NAME_USER;
2480 tnames.names[6].name.string = talloc_asprintf(tctx, "%s\\guest", info->dns.dns_domain.string);
2481 tnames.names[6].sid_type = SID_NAME_USER;
2482 tnames.names[7].name.string = talloc_asprintf(tctx, "%s\\krbtgt", info->dns.dns_domain.string);
2483 tnames.names[7].sid_type = SID_NAME_USER;
2484 tnames.names[8].name.string = talloc_asprintf(tctx, "krbtgt@%s", info->dns.name.string);
2485 tnames.names[8].sid_type = SID_NAME_USER;
2486 tnames.names[9].name.string = talloc_asprintf(tctx, "krbtgt@%s", info->dns.dns_domain.string);
2487 tnames.names[9].sid_type = SID_NAME_USER;
2488 tnames.names[10].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", info->dns.name.string);
2489 tnames.names[10].sid_type = SID_NAME_USER;
2490 tnames.names[11].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", info->dns.dns_domain.string);
2491 tnames.names[11].sid_type = SID_NAME_USER;
2492 tnames.names[12].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", info->dns.name.string);
2493 tnames.names[12].sid_type = SID_NAME_USER;
2494 tnames.names[13].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", info->dns.dns_domain.string);
2495 tnames.names[13].sid_type = SID_NAME_USER;
2496 ret &= test_LookupNames(p, tctx, handle, &tnames);
2501 return ret;
2504 static bool test_QueryInfoPolicy(struct dcerpc_pipe *p,
2505 struct torture_context *tctx,
2506 struct policy_handle *handle)
2508 return test_QueryInfoPolicyCalls(false, p, tctx, handle);
2511 static bool test_QueryInfoPolicy2(struct dcerpc_pipe *p,
2512 struct torture_context *tctx,
2513 struct policy_handle *handle)
2515 return test_QueryInfoPolicyCalls(true, p, tctx, handle);
2518 static bool test_GetUserName(struct dcerpc_pipe *p,
2519 struct torture_context *tctx)
2521 struct lsa_GetUserName r;
2522 NTSTATUS status;
2523 bool ret = true;
2524 struct lsa_String *authority_name_p = NULL;
2525 struct lsa_String *account_name_p = NULL;
2527 torture_comment(tctx, "\nTesting GetUserName\n");
2529 r.in.system_name = "\\";
2530 r.in.account_name = &account_name_p;
2531 r.in.authority_name = NULL;
2532 r.out.account_name = &account_name_p;
2534 status = dcerpc_lsa_GetUserName(p, tctx, &r);
2536 if (!NT_STATUS_IS_OK(status)) {
2537 torture_comment(tctx, "GetUserName failed - %s\n", nt_errstr(status));
2538 ret = false;
2541 account_name_p = NULL;
2542 r.in.account_name = &account_name_p;
2543 r.in.authority_name = &authority_name_p;
2544 r.out.account_name = &account_name_p;
2546 status = dcerpc_lsa_GetUserName(p, tctx, &r);
2548 if (!NT_STATUS_IS_OK(status)) {
2549 torture_comment(tctx, "GetUserName failed - %s\n", nt_errstr(status));
2550 ret = false;
2553 return ret;
2556 bool test_lsa_Close(struct dcerpc_pipe *p,
2557 struct torture_context *tctx,
2558 struct policy_handle *handle)
2560 NTSTATUS status;
2561 struct lsa_Close r;
2562 struct policy_handle handle2;
2564 torture_comment(tctx, "\nTesting Close\n");
2566 r.in.handle = handle;
2567 r.out.handle = &handle2;
2569 status = dcerpc_lsa_Close(p, tctx, &r);
2570 if (!NT_STATUS_IS_OK(status)) {
2571 torture_comment(tctx, "Close failed - %s\n", nt_errstr(status));
2572 return false;
2575 status = dcerpc_lsa_Close(p, tctx, &r);
2576 /* its really a fault - we need a status code for rpc fault */
2577 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
2578 torture_comment(tctx, "Close failed - %s\n", nt_errstr(status));
2579 return false;
2582 torture_comment(tctx, "\n");
2584 return true;
2587 bool torture_rpc_lsa(struct torture_context *tctx)
2589 NTSTATUS status;
2590 struct dcerpc_pipe *p;
2591 bool ret = true;
2592 struct policy_handle *handle;
2593 struct test_join *join = NULL;
2594 struct cli_credentials *machine_creds;
2596 status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
2597 if (!NT_STATUS_IS_OK(status)) {
2598 return false;
2601 if (!test_OpenPolicy(p, tctx)) {
2602 ret = false;
2605 if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2606 ret = false;
2609 if (handle) {
2610 join = torture_join_domain(tctx, TEST_MACHINENAME, ACB_WSTRUST, &machine_creds);
2611 if (!join) {
2612 ret = false;
2614 if (!test_LookupNames_wellknown(p, tctx, handle)) {
2615 ret = false;
2618 if (!test_LookupNames_bogus(p, tctx, handle)) {
2619 ret = false;
2622 if (!test_LookupSids_async(p, tctx, handle)) {
2623 ret = false;
2626 if (!test_QueryDomainInfoPolicy(p, tctx, handle)) {
2627 ret = false;
2630 if (!test_CreateSecret(p, tctx, handle)) {
2631 ret = false;
2634 if (!test_QueryInfoPolicy(p, tctx, handle)) {
2635 ret = false;
2638 if (!test_QueryInfoPolicy2(p, tctx, handle)) {
2639 ret = false;
2642 if (!test_Delete(p, tctx, handle)) {
2643 ret = false;
2646 if (!test_many_LookupSids(p, tctx, handle)) {
2647 ret = false;
2650 if (!test_lsa_Close(p, tctx, handle)) {
2651 ret = false;
2654 torture_leave_domain(tctx, join);
2656 } else {
2657 if (!test_many_LookupSids(p, tctx, handle)) {
2658 ret = false;
2662 if (!test_GetUserName(p, tctx)) {
2663 ret = false;
2666 return ret;
2669 bool torture_rpc_lsa_get_user(struct torture_context *tctx)
2671 NTSTATUS status;
2672 struct dcerpc_pipe *p;
2673 bool ret = true;
2675 status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
2676 if (!NT_STATUS_IS_OK(status)) {
2677 return false;
2680 if (!test_GetUserName(p, tctx)) {
2681 ret = false;
2684 return ret;
2687 static bool testcase_LookupNames(struct torture_context *tctx,
2688 struct dcerpc_pipe *p)
2690 bool ret = true;
2691 struct policy_handle *handle;
2692 struct lsa_TransNameArray tnames;
2693 struct lsa_TransNameArray2 tnames2;
2695 if (!test_OpenPolicy(p, tctx)) {
2696 ret = false;
2699 if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2700 ret = false;
2703 if (!handle) {
2704 ret = false;
2707 tnames.count = 1;
2708 tnames.names = talloc_array(tctx, struct lsa_TranslatedName, tnames.count);
2709 ZERO_STRUCT(tnames.names[0]);
2710 tnames.names[0].name.string = "BUILTIN";
2711 tnames.names[0].sid_type = SID_NAME_DOMAIN;
2713 if (!test_LookupNames(p, tctx, handle, &tnames)) {
2714 ret = false;
2717 tnames2.count = 1;
2718 tnames2.names = talloc_array(tctx, struct lsa_TranslatedName2, tnames2.count);
2719 ZERO_STRUCT(tnames2.names[0]);
2720 tnames2.names[0].name.string = "BUILTIN";
2721 tnames2.names[0].sid_type = SID_NAME_DOMAIN;
2723 if (!test_LookupNames2(p, tctx, handle, &tnames2, true)) {
2724 ret = false;
2727 if (!test_LookupNames3(p, tctx, handle, &tnames2, true)) {
2728 ret = false;
2731 if (!test_lsa_Close(p, tctx, handle)) {
2732 ret = false;
2735 return ret;
2738 struct torture_suite *torture_rpc_lsa_lookup_names(TALLOC_CTX *mem_ctx)
2740 struct torture_suite *suite;
2741 struct torture_rpc_tcase *tcase;
2743 suite = torture_suite_create(mem_ctx, "LSA-LOOKUPNAMES");
2745 tcase = torture_suite_add_rpc_iface_tcase(suite, "lsa",
2746 &ndr_table_lsarpc);
2747 torture_rpc_tcase_add_test(tcase, "LookupNames",
2748 testcase_LookupNames);
2750 return suite;
2753 static bool testcase_TrustedDomains(struct torture_context *tctx,
2754 struct dcerpc_pipe *p)
2756 bool ret = true;
2757 struct policy_handle *handle;
2759 if (!test_OpenPolicy(p, tctx)) {
2760 ret = false;
2763 if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2764 ret = false;
2767 if (!handle) {
2768 ret = false;
2771 if (!test_CreateTrustedDomain(p, tctx, handle)) {
2772 ret = false;
2775 if (!test_CreateTrustedDomainEx2(p, tctx, handle)) {
2776 ret = false;
2779 if (!test_lsa_Close(p, tctx, handle)) {
2780 ret = false;
2783 return ret;
2786 struct torture_suite *torture_rpc_lsa_trusted_domains(TALLOC_CTX *mem_ctx)
2788 struct torture_suite *suite;
2789 struct torture_rpc_tcase *tcase;
2791 suite = torture_suite_create(mem_ctx, "LSA-TRUSTED-DOMAINS");
2793 tcase = torture_suite_add_rpc_iface_tcase(suite, "lsa",
2794 &ndr_table_lsarpc);
2795 torture_rpc_tcase_add_test(tcase, "TrustedDomains",
2796 testcase_TrustedDomains);
2798 return suite;
2801 static bool testcase_Privileges(struct torture_context *tctx,
2802 struct dcerpc_pipe *p)
2804 bool ret = true;
2805 struct policy_handle *handle;
2807 if (!test_OpenPolicy(p, tctx)) {
2808 ret = false;
2811 if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2812 ret = false;
2815 if (!handle) {
2816 ret = false;
2819 if (!test_CreateAccount(p, tctx, handle)) {
2820 ret = false;
2823 if (!test_EnumAccounts(p, tctx, handle)) {
2824 ret = false;
2827 if (!test_EnumPrivs(p, tctx, handle)) {
2828 ret = false;
2831 if (!test_lsa_Close(p, tctx, handle)) {
2832 ret = false;
2835 return ret;
2839 struct torture_suite *torture_rpc_lsa_privileges(TALLOC_CTX *mem_ctx)
2841 struct torture_suite *suite;
2842 struct torture_rpc_tcase *tcase;
2844 suite = torture_suite_create(mem_ctx, "LSA-PRIVILEGES");
2846 tcase = torture_suite_add_rpc_iface_tcase(suite, "lsa",
2847 &ndr_table_lsarpc);
2848 torture_rpc_tcase_add_test(tcase, "Privileges",
2849 testcase_Privileges);
2851 return suite;