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.
25 these really shouldn't be here ....
27 static char *lsa_sid_string_talloc(TALLOC_CTX *mem_ctx, struct dom_sid *sid)
34 return talloc_asprintf(mem_ctx, "(NULL SID)");
37 maxlen = sid->num_auths * 11 + 25;
38 ret = talloc(mem_ctx, maxlen);
39 if (!ret) return NULL;
41 ia = (sid->id_auth[5]) +
42 (sid->id_auth[4] << 8 ) +
43 (sid->id_auth[3] << 16) +
44 (sid->id_auth[2] << 24);
46 ofs = snprintf(ret, maxlen, "S-%u-%lu",
47 (unsigned int)sid->sid_rev_num, (unsigned long)ia);
49 for (i = 0; i < sid->num_auths; i++) {
50 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu", (unsigned long)sid->sub_auths[i]);
56 static int dom_sid_compare(struct dom_sid *sid1, struct dom_sid *sid2)
60 if (sid1 == sid2) return 0;
64 /* Compare most likely different rids, first: i.e start at end */
65 if (sid1->num_auths != sid2->num_auths)
66 return sid1->num_auths - sid2->num_auths;
68 for (i = sid1->num_auths-1; i >= 0; --i)
69 if (sid1->sub_auths[i] != sid2->sub_auths[i])
70 return sid1->sub_auths[i] - sid2->sub_auths[i];
72 if (sid1->sid_rev_num != sid2->sid_rev_num)
73 return sid1->sid_rev_num - sid2->sid_rev_num;
75 for (i = 0; i < 6; i++)
76 if (sid1->id_auth[i] != sid2->id_auth[i])
77 return sid1->id_auth[i] - sid2->id_auth[i];
82 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
84 struct lsa_ObjectAttribute attr;
85 struct policy_handle handle;
86 struct lsa_QosInfo qos;
87 struct lsa_OpenPolicy r;
89 uint16 system_name = '\\';
91 printf("\ntesting OpenPolicy\n");
93 qos.impersonation_level = 2;
95 qos.effective_only = 0;
98 attr.object_name = NULL;
100 attr.sec_desc = NULL;
103 r.in.system_name = &system_name;
105 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
106 r.out.handle = &handle;
108 status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
109 if (!NT_STATUS_IS_OK(status)) {
110 printf("OpenPolicy failed - %s\n", nt_errstr(status));
118 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
119 struct policy_handle *handle)
121 struct lsa_ObjectAttribute attr;
122 struct lsa_QosInfo qos;
123 struct lsa_OpenPolicy2 r;
126 printf("\ntesting OpenPolicy2\n");
128 qos.impersonation_level = 2;
129 qos.context_mode = 1;
130 qos.effective_only = 0;
132 attr.root_dir = NULL;
133 attr.object_name = NULL;
135 attr.sec_desc = NULL;
138 r.in.system_name = "\\";
140 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
141 r.out.handle = handle;
143 status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
144 if (!NT_STATUS_IS_OK(status)) {
145 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
152 static BOOL test_LookupNames(struct dcerpc_pipe *p,
154 struct policy_handle *handle,
155 struct lsa_TransNameArray *tnames)
157 struct lsa_LookupNames r;
158 struct lsa_TransSidArray sids;
159 struct lsa_Name *names;
164 printf("\nTesting LookupNames\n");
169 names = talloc(mem_ctx, tnames->count * sizeof(names[0]));
170 for (i=0;i<tnames->count;i++) {
171 names[i].name_len = 2*strlen(tnames->names[i].name.name);
172 names[i].name_size = 2*strlen(tnames->names[i].name.name);
173 names[i].name = tnames->names[i].name.name;
176 r.in.handle = handle;
177 r.in.num_names = tnames->count;
182 r.out.count = &count;
185 status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
186 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
187 printf("LookupNames failed - %s\n", nt_errstr(status));
192 printf("lookup gave %d domains (max_count=%d)\n",
193 r.out.domains->count,
194 r.out.domains->max_count);
195 for (i=0;i<r.out.domains->count;i++) {
196 printf("name='%s' sid=%s\n",
197 r.out.domains->domains[i].name.name,
198 lsa_sid_string_talloc(mem_ctx, r.out.domains->domains[i].sid));
202 printf("lookup gave %d sids (sids.count=%d)\n", count, sids.count);
203 for (i=0;i<sids.count;i++) {
204 printf("sid_type=%d rid=%d sid_index=%d\n",
205 sids.sids[i].sid_type,
207 sids.sids[i].sid_index);
216 static BOOL test_LookupSids(struct dcerpc_pipe *p,
218 struct policy_handle *handle,
219 struct lsa_SidArray *sids)
221 struct lsa_LookupSids r;
222 struct lsa_TransNameArray names;
223 uint32 count = sids->num_sids;
227 printf("\nTesting LookupSids\n");
232 r.in.handle = handle;
237 r.out.count = &count;
238 r.out.names = &names;
240 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
241 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
242 printf("LookupSids failed - %s\n", nt_errstr(status));
247 printf("lookup gave %d domains (max_count=%d)\n",
248 r.out.domains->count,
249 r.out.domains->max_count);
250 for (i=0;i<r.out.domains->count;i++) {
251 printf("name='%s' sid=%s\n",
252 r.out.domains->domains[i].name.name,
253 lsa_sid_string_talloc(mem_ctx, r.out.domains->domains[i].sid));
257 printf("lookup gave %d names (names.count=%d)\n", count, names.count);
258 for (i=0;i<names.count;i++) {
259 printf("type=%d sid_index=%d name='%s'\n",
260 names.names[i].sid_type,
261 names.names[i].sid_index,
262 names.names[i].name.name);
267 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
274 static BOOL test_OpenAccount(struct dcerpc_pipe *p,
276 struct policy_handle *handle,
280 struct lsa_OpenAccount r;
281 struct policy_handle acct_handle;
283 printf("Testing account %s\n", lsa_sid_string_talloc(mem_ctx, sid));
285 r.in.handle = handle;
287 r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
288 r.out.acct_handle = &acct_handle;
290 status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
291 if (!NT_STATUS_IS_OK(status)) {
292 printf("OpenAccount failed - %s\n", nt_errstr(status));
299 static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
301 struct policy_handle *handle)
304 struct lsa_EnumAccounts r;
305 struct lsa_SidArray sids1, sids2;
306 uint32 resume_handle = 0;
309 printf("\ntesting EnumAccounts\n");
311 r.in.handle = handle;
312 r.in.resume_handle = &resume_handle;
313 r.in.num_entries = 100;
314 r.out.resume_handle = &resume_handle;
318 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
319 if (!NT_STATUS_IS_OK(status)) {
320 printf("EnumAccounts failed - %s\n", nt_errstr(status));
324 printf("Got %d sids resume_handle=%u\n", sids1.num_sids, resume_handle);
326 for (i=0;i<sids1.num_sids;i++) {
327 printf("%s\n", lsa_sid_string_talloc(mem_ctx, sids1.sids[i].sid));
330 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
334 printf("testing all accounts\n");
335 for (i=0;i<sids1.num_sids;i++) {
336 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
340 if (sids1.num_sids < 3) {
344 printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
346 r.in.num_entries = 1;
349 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
350 if (!NT_STATUS_IS_OK(status)) {
351 printf("EnumAccounts failed - %s\n", nt_errstr(status));
355 if (sids2.num_sids != 1) {
356 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
364 static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
366 struct policy_handle *handle)
369 struct lsa_EnumPrivs r;
370 struct lsa_PrivArray privs1;
371 uint32 resume_handle = 0;
374 printf("\ntesting EnumPrivs\n");
376 r.in.handle = handle;
377 r.in.resume_handle = &resume_handle;
378 r.in.max_count = 1000;
379 r.out.resume_handle = &resume_handle;
380 r.out.privs = &privs1;
383 status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
384 if (!NT_STATUS_IS_OK(status)) {
385 printf("EnumPrivs failed - %s\n", nt_errstr(status));
389 printf("Got %d privs resume_handle=%u\n", privs1.count, resume_handle);
391 for (i=0;i<privs1.count;i++) {
392 printf("luid=%08x-%08x '%s'\n",
393 privs1.privs[i].luid_low,
394 privs1.privs[i].luid_high,
395 privs1.privs[i].name.name);
402 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
404 struct policy_handle *handle)
406 struct lsa_EnumTrustDom r;
409 uint32 resume_handle = 0;
410 struct lsa_RefDomainList domains;
412 printf("\nTesting EnumTrustDom\n");
414 r.in.handle = handle;
415 r.in.resume_handle = &resume_handle;
416 r.in.num_entries = 1000;
417 r.out.domains = &domains;
418 r.out.resume_handle = &resume_handle;
420 status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
421 if (!NT_STATUS_IS_OK(status)) {
422 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
426 printf("lookup gave %d domains (max_count=%d)\n",
429 for (i=0;i<r.out.domains->count;i++) {
430 printf("name='%s' sid=%s\n",
431 domains.domains[i].name.name,
432 lsa_sid_string_talloc(mem_ctx, domains.domains[i].sid));
438 static BOOL test_Delete(struct dcerpc_pipe *p,
440 struct policy_handle *handle)
445 printf("\ntesting Delete - but what does it do?\n");
447 r.in.handle = handle;
448 status = dcerpc_lsa_Delete(p, mem_ctx, &r);
449 if (!NT_STATUS_IS_OK(status)) {
450 printf("Delete failed - %s\n", nt_errstr(status));
459 static BOOL test_Close(struct dcerpc_pipe *p,
461 struct policy_handle *handle)
465 struct policy_handle handle2;
467 printf("\ntesting Close\n");
469 r.in.handle = handle;
470 r.out.handle = &handle2;
472 status = dcerpc_lsa_Close(p, mem_ctx, &r);
473 if (!NT_STATUS_IS_OK(status)) {
474 printf("Close failed - %s\n", nt_errstr(status));
478 status = dcerpc_lsa_Close(p, mem_ctx, &r);
479 /* its really a fault - we need a status code for rpc fault */
480 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_LEVEL)) {
481 printf("Close failed - %s\n", nt_errstr(status));
490 BOOL torture_rpc_lsa(int dummy)
493 struct dcerpc_pipe *p;
496 struct policy_handle handle;
498 mem_ctx = talloc_init("torture_rpc_lsa");
500 status = torture_rpc_connection(&p, "lsarpc");
501 if (!NT_STATUS_IS_OK(status)) {
505 if (!test_OpenPolicy(p, mem_ctx)) {
509 if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
513 if (!test_EnumAccounts(p, mem_ctx, &handle)) {
517 if (!test_EnumPrivs(p, mem_ctx, &handle)) {
521 if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
526 if (!test_Delete(p, mem_ctx, &handle)) {
531 if (!test_Close(p, mem_ctx, &handle)) {
535 torture_rpc_close(p);