2 Unix SMB/CIFS implementation.
3 test suite for lsa rpc operations
5 Copyright (C) Andrew Tridgell 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static void init_lsa_Name(struct lsa_Name *name, const char *s)
29 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
31 struct lsa_ObjectAttribute attr;
32 struct policy_handle handle;
33 struct lsa_QosInfo qos;
34 struct lsa_OpenPolicy r;
36 uint16 system_name = '\\';
38 printf("\ntesting OpenPolicy\n");
41 qos.impersonation_level = 2;
43 qos.effective_only = 0;
47 attr.object_name = NULL;
52 r.in.system_name = &system_name;
54 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
55 r.out.handle = &handle;
57 status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
58 if (!NT_STATUS_IS_OK(status)) {
59 printf("OpenPolicy failed - %s\n", nt_errstr(status));
67 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
68 struct policy_handle *handle)
70 struct lsa_ObjectAttribute attr;
71 struct lsa_QosInfo qos;
72 struct lsa_OpenPolicy2 r;
75 printf("\ntesting OpenPolicy2\n");
78 qos.impersonation_level = 2;
80 qos.effective_only = 0;
84 attr.object_name = NULL;
89 r.in.system_name = "\\";
91 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
92 r.out.handle = handle;
94 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
95 if (!NT_STATUS_IS_OK(status)) {
96 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
103 static BOOL test_LookupNames(struct dcerpc_pipe *p,
105 struct policy_handle *handle,
106 struct lsa_TransNameArray *tnames)
108 struct lsa_LookupNames r;
109 struct lsa_TransSidArray sids;
110 struct lsa_Name *names;
115 printf("\nTesting LookupNames\n");
120 names = talloc(mem_ctx, tnames->count * sizeof(names[0]));
121 for (i=0;i<tnames->count;i++) {
122 init_lsa_Name(&names[i], tnames->names[i].name.name);
125 r.in.handle = handle;
126 r.in.num_names = tnames->count;
131 r.out.count = &count;
134 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
135 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
136 printf("LookupNames failed - %s\n", nt_errstr(status));
146 static BOOL test_LookupSids(struct dcerpc_pipe *p,
148 struct policy_handle *handle,
149 struct lsa_SidArray *sids)
151 struct lsa_LookupSids r;
152 struct lsa_TransNameArray names;
153 uint32 count = sids->num_sids;
156 printf("\nTesting LookupSids\n");
161 r.in.handle = handle;
166 r.out.count = &count;
167 r.out.names = &names;
169 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
170 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
171 printf("LookupSids failed - %s\n", nt_errstr(status));
177 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
184 static BOOL test_LookupPrivName(struct dcerpc_pipe *p,
186 struct policy_handle *handle,
187 struct lsa_LUID *luid)
190 struct lsa_LookupPrivName r;
192 r.in.handle = handle;
195 status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
196 if (!NT_STATUS_IS_OK(status)) {
197 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
204 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p,
206 struct policy_handle *handle,
207 struct policy_handle *acct_handle)
210 struct lsa_EnumPrivsAccount r;
212 printf("Testing EnumPrivsAccount\n");
214 r.in.handle = acct_handle;
216 status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
217 if (!NT_STATUS_IS_OK(status)) {
218 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
224 for (i=0;i<r.out.privs->count;i++) {
225 test_LookupPrivName(p, mem_ctx, handle,
226 &r.out.privs->set[i].luid);
233 static BOOL test_Delete(struct dcerpc_pipe *p,
235 struct policy_handle *handle)
240 printf("\ntesting Delete\n");
242 r.in.handle = handle;
243 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
244 if (!NT_STATUS_IS_OK(status)) {
245 printf("Delete failed - %s\n", nt_errstr(status));
255 static BOOL find_domain_sid(struct dcerpc_pipe *p,
257 struct policy_handle *handle,
258 struct dom_sid2 **sid)
260 struct lsa_QueryInfoPolicy r;
263 r.in.handle = handle;
264 r.in.level = LSA_POLICY_INFO_DOMAIN;
266 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
268 if (!NT_STATUS_IS_OK(status)) {
269 printf("LSA_POLICY_INFO_DOMAIN failed - %s\n", nt_errstr(status));
273 *sid = r.out.info->domain.sid;
278 static struct dom_sid *sid_add_auth(TALLOC_CTX *mem_ctx,
279 const struct dom_sid *sid,
284 ret = talloc_p(mem_ctx, struct dom_sid);
291 ret->sub_auths = talloc_array_p(mem_ctx, uint32, ret->num_auths+1);
292 if (!ret->sub_auths) {
296 memcpy(ret->sub_auths, sid->sub_auths,
297 ret->num_auths * sizeof(sid->sub_auths[0]));
298 ret->sub_auths[ret->num_auths] = sub_auth;
304 static BOOL test_CreateAccount(struct dcerpc_pipe *p,
306 struct policy_handle *handle)
309 struct lsa_CreateAccount r;
310 struct dom_sid2 *domsid, *newsid;
311 struct policy_handle acct_handle;
313 if (!find_domain_sid(p, mem_ctx, handle, &domsid)) {
317 newsid = sid_add_auth(mem_ctx, domsid, 0x1234abcd);
319 printf("Failed to create newsid\n");
323 printf("Testing CreateAccount\n");
325 r.in.handle = handle;
327 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
328 r.out.acct_handle = &acct_handle;
330 status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
331 if (!NT_STATUS_IS_OK(status)) {
332 printf("CreateAccount failed - %s\n", nt_errstr(status));
336 if (!test_Delete(p, mem_ctx, &acct_handle)) {
344 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p,
346 struct policy_handle *handle)
349 struct lsa_CreateTrustedDomain r;
350 struct lsa_TrustInformation trustinfo;
351 struct dom_sid *domsid;
352 struct policy_handle dom_handle;
354 printf("Testing CreateTrustedDomain\n");
356 if (!find_domain_sid(p, mem_ctx, handle, &domsid)) {
360 domsid->sub_auths[domsid->num_auths-1] ^= 0xF0F0F0F0;
362 trustinfo.sid = domsid;
363 init_lsa_Name(&trustinfo.name, "torturedomain");
365 r.in.handle = handle;
366 r.in.info = &trustinfo;
367 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
368 r.out.dom_handle = &dom_handle;
370 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
371 if (!NT_STATUS_IS_OK(status)) {
372 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
376 if (!test_Delete(p, mem_ctx, &dom_handle)) {
383 static BOOL test_CreateSecret(struct dcerpc_pipe *p,
385 struct policy_handle *handle)
388 struct lsa_CreateSecret r;
389 struct policy_handle sec_handle;
391 printf("Testing CreateSecret\n");
393 init_lsa_Name(&r.in.name, "torturesecret");
395 r.in.handle = handle;
396 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
397 r.out.sec_handle = &sec_handle;
399 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
400 if (!NT_STATUS_IS_OK(status)) {
401 printf("CreateSecret failed - %s\n", nt_errstr(status));
405 if (!test_Delete(p, mem_ctx, &sec_handle)) {
412 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p,
414 struct policy_handle *acct_handle,
418 struct lsa_EnumAccountRights r;
419 struct lsa_RightSet rights;
421 printf("Testing EnumAccountRights\n");
423 r.in.handle = acct_handle;
425 r.out.rights = &rights;
427 status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
428 if (!NT_STATUS_IS_OK(status)) {
429 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
437 static BOOL test_QuerySecObj(struct dcerpc_pipe *p,
439 struct policy_handle *handle,
440 struct policy_handle *acct_handle)
443 struct lsa_QuerySecObj r;
445 printf("Testing QuerySecObj\n");
447 r.in.handle = acct_handle;
450 status = dcerpc_lsa_QuerySecObj(p, mem_ctx, &r);
451 if (!NT_STATUS_IS_OK(status)) {
452 printf("QuerySecObj failed - %s\n", nt_errstr(status));
459 static BOOL test_OpenAccount(struct dcerpc_pipe *p,
461 struct policy_handle *handle,
465 struct lsa_OpenAccount r;
466 struct policy_handle acct_handle;
468 printf("Testing OpenAccount\n");
470 r.in.handle = handle;
472 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
473 r.out.acct_handle = &acct_handle;
475 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
476 if (!NT_STATUS_IS_OK(status)) {
477 printf("OpenAccount failed - %s\n", nt_errstr(status));
481 if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
485 if (!test_QuerySecObj(p, mem_ctx, handle, &acct_handle)) {
492 static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
494 struct policy_handle *handle)
497 struct lsa_EnumAccounts r;
498 struct lsa_SidArray sids1, sids2;
499 uint32 resume_handle = 0;
502 printf("\ntesting EnumAccounts\n");
504 r.in.handle = handle;
505 r.in.resume_handle = &resume_handle;
506 r.in.num_entries = 100;
507 r.out.resume_handle = &resume_handle;
511 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
512 if (!NT_STATUS_IS_OK(status)) {
513 printf("EnumAccounts failed - %s\n", nt_errstr(status));
517 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
521 printf("testing all accounts\n");
522 for (i=0;i<sids1.num_sids;i++) {
523 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
524 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
528 if (sids1.num_sids < 3) {
532 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
534 r.in.num_entries = 1;
537 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
538 if (!NT_STATUS_IS_OK(status)) {
539 printf("EnumAccounts failed - %s\n", nt_errstr(status));
543 if (sids2.num_sids != 1) {
544 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
552 static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
554 struct policy_handle *handle)
557 struct lsa_EnumPrivs r;
558 struct lsa_PrivArray privs1;
559 uint32 resume_handle = 0;
561 printf("\ntesting EnumPrivs\n");
563 r.in.handle = handle;
564 r.in.resume_handle = &resume_handle;
565 r.in.max_count = 1000;
566 r.out.resume_handle = &resume_handle;
567 r.out.privs = &privs1;
570 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
571 if (!NT_STATUS_IS_OK(status)) {
572 printf("EnumPrivs failed - %s\n", nt_errstr(status));
580 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
582 struct policy_handle *handle)
584 struct lsa_EnumTrustDom r;
586 uint32 resume_handle = 0;
587 struct lsa_DomainList domains;
589 printf("\nTesting EnumTrustDom\n");
591 r.in.handle = handle;
592 r.in.resume_handle = &resume_handle;
593 r.in.num_entries = 1000;
594 r.out.domains = &domains;
595 r.out.resume_handle = &resume_handle;
597 status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
599 /* NO_MORE_ENTRIES is allowed */
600 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
604 if (!NT_STATUS_IS_OK(status)) {
605 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
612 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p,
614 struct policy_handle *handle)
616 struct lsa_QueryInfoPolicy r;
620 printf("\nTesting QueryInfoPolicy\n");
623 r.in.handle = handle;
626 printf("\ntrying QueryInfoPolicy level %d\n", i);
628 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
630 if ((i == 9 || i == 10 || i == 11) &&
631 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
632 printf("server failed level %u (OK)\n", i);
636 if (!NT_STATUS_IS_OK(status)) {
637 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
646 static BOOL test_Close(struct dcerpc_pipe *p,
648 struct policy_handle *handle)
652 struct policy_handle handle2;
654 printf("\ntesting Close\n");
656 r.in.handle = handle;
657 r.out.handle = &handle2;
659 status = dcerpc_lsa_Close(p, mem_ctx, &r);
660 if (!NT_STATUS_IS_OK(status)) {
661 printf("Close failed - %s\n", nt_errstr(status));
665 status = dcerpc_lsa_Close(p, mem_ctx, &r);
666 /* its really a fault - we need a status code for rpc fault */
667 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
668 printf("Close failed - %s\n", nt_errstr(status));
677 BOOL torture_rpc_lsa(int dummy)
680 struct dcerpc_pipe *p;
683 struct policy_handle handle;
685 mem_ctx = talloc_init("torture_rpc_lsa");
687 status = torture_rpc_connection(&p,
690 DCERPC_LSARPC_VERSION);
691 if (!NT_STATUS_IS_OK(status)) {
695 if (!test_OpenPolicy(p, mem_ctx)) {
699 if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
703 if (!test_CreateAccount(p, mem_ctx, &handle)) {
707 if (!test_CreateSecret(p, mem_ctx, &handle)) {
711 if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
715 if (!test_EnumAccounts(p, mem_ctx, &handle)) {
719 if (!test_EnumPrivs(p, mem_ctx, &handle)) {
723 if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
727 if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
732 if (!test_Delete(p, mem_ctx, &handle)) {
737 if (!test_Close(p, mem_ctx, &handle)) {
741 talloc_destroy(mem_ctx);
743 torture_rpc_close(p);