use the auto-generated UUID, version and name rather than listing them
[ira/wip.git] / source4 / torture / rpc / lsa.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for lsa rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
25 {
26         struct lsa_ObjectAttribute attr;
27         struct policy_handle handle;
28         struct lsa_QosInfo qos;
29         struct lsa_OpenPolicy r;
30         NTSTATUS status;
31         uint16 system_name = '\\';
32
33         printf("\ntesting OpenPolicy\n");
34
35         qos.len = 0;
36         qos.impersonation_level = 2;
37         qos.context_mode = 1;
38         qos.effective_only = 0;
39
40         attr.len = 0;
41         attr.root_dir = NULL;
42         attr.object_name = NULL;
43         attr.attributes = 0;
44         attr.sec_desc = NULL;
45         attr.sec_qos = &qos;
46
47         r.in.system_name = &system_name;
48         r.in.attr = &attr;
49         r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
50         r.out.handle = &handle;
51
52         status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
53         if (!NT_STATUS_IS_OK(status)) {
54                 printf("OpenPolicy failed - %s\n", nt_errstr(status));
55                 return False;
56         }
57
58         return True;
59 }
60
61
62 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
63                              struct policy_handle *handle)
64 {
65         struct lsa_ObjectAttribute attr;
66         struct lsa_QosInfo qos;
67         struct lsa_OpenPolicy2 r;
68         NTSTATUS status;
69
70         printf("\ntesting OpenPolicy2\n");
71
72         qos.len = 0;
73         qos.impersonation_level = 2;
74         qos.context_mode = 1;
75         qos.effective_only = 0;
76
77         attr.len = 0;
78         attr.root_dir = NULL;
79         attr.object_name = NULL;
80         attr.attributes = 0;
81         attr.sec_desc = NULL;
82         attr.sec_qos = &qos;
83
84         r.in.system_name = "\\";
85         r.in.attr = &attr;
86         r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
87         r.out.handle = handle;
88
89         status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
90         if (!NT_STATUS_IS_OK(status)) {
91                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
92                 return False;
93         }
94
95         return True;
96 }
97
98 static BOOL test_LookupNames(struct dcerpc_pipe *p, 
99                             TALLOC_CTX *mem_ctx, 
100                             struct policy_handle *handle,
101                             struct lsa_TransNameArray *tnames)
102 {
103         struct lsa_LookupNames r;
104         struct lsa_TransSidArray sids;
105         struct lsa_Name *names;
106         uint32 count = 0;
107         NTSTATUS status;
108         int i;
109
110         printf("\nTesting LookupNames\n");
111
112         sids.count = 0;
113         sids.sids = NULL;
114
115         names = talloc(mem_ctx, tnames->count * sizeof(names[0]));
116         for (i=0;i<tnames->count;i++) {
117                 names[i].name_len = 2*strlen(tnames->names[i].name.name);
118                 names[i].name_size = 2*strlen(tnames->names[i].name.name);
119                 names[i].name = tnames->names[i].name.name;
120         }
121
122         r.in.handle = handle;
123         r.in.num_names = tnames->count;
124         r.in.names = names;
125         r.in.sids = &sids;
126         r.in.level = 1;
127         r.in.count = &count;
128         r.out.count = &count;
129         r.out.sids = &sids;
130
131         status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
132         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
133                 printf("LookupNames failed - %s\n", nt_errstr(status));
134                 return False;
135         }
136
137         printf("\n");
138
139         return True;
140 }
141
142
143 static BOOL test_LookupSids(struct dcerpc_pipe *p, 
144                             TALLOC_CTX *mem_ctx, 
145                             struct policy_handle *handle,
146                             struct lsa_SidArray *sids)
147 {
148         struct lsa_LookupSids r;
149         struct lsa_TransNameArray names;
150         uint32 count = sids->num_sids;
151         NTSTATUS status;
152
153         printf("\nTesting LookupSids\n");
154
155         names.count = 0;
156         names.names = NULL;
157
158         r.in.handle = handle;
159         r.in.sids = sids;
160         r.in.names = &names;
161         r.in.level = 1;
162         r.in.count = &count;
163         r.out.count = &count;
164         r.out.names = &names;
165
166         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
167         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
168                 printf("LookupSids failed - %s\n", nt_errstr(status));
169                 return False;
170         }
171
172         printf("\n");
173
174         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
175                 return False;
176         }
177
178         return True;
179 }
180
181 static BOOL test_LookupPrivName(struct dcerpc_pipe *p, 
182                                 TALLOC_CTX *mem_ctx, 
183                                 struct policy_handle *handle,
184                                 struct lsa_LUID *luid)
185 {
186         NTSTATUS status;
187         struct lsa_LookupPrivName r;
188
189         r.in.handle = handle;
190         r.in.luid = luid;
191
192         status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
193         if (!NT_STATUS_IS_OK(status)) {
194                 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
195                 return False;
196         }
197
198         return True;
199 }
200
201 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p, 
202                                   TALLOC_CTX *mem_ctx,                            
203                                   struct policy_handle *handle,
204                                   struct policy_handle *acct_handle)
205 {
206         NTSTATUS status;
207         struct lsa_EnumPrivsAccount r;
208
209         printf("Testing EnumPrivsAccount\n");
210
211         r.in.handle = acct_handle;
212
213         status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
214         if (!NT_STATUS_IS_OK(status)) {
215                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
216                 return False;
217         }
218
219         if (r.out.privs) {
220                 int i;
221                 for (i=0;i<r.out.privs->count;i++) {
222                         test_LookupPrivName(p, mem_ctx, handle, 
223                                             &r.out.privs->set[i].luid);
224                 }
225         }
226
227         return True;
228 }
229
230 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p, 
231                                    TALLOC_CTX *mem_ctx, 
232                                    struct policy_handle *acct_handle,
233                                    struct dom_sid *sid)
234 {
235         NTSTATUS status;
236         struct lsa_EnumAccountRights r;
237         struct lsa_RightSet rights;
238
239         printf("Testing EnumAccountRights\n");
240
241         r.in.handle = acct_handle;
242         r.in.sid = sid;
243         r.out.rights = &rights;
244
245         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
246         if (!NT_STATUS_IS_OK(status)) {
247                 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
248                 return False;
249         }
250
251         return True;
252 }
253
254
255 static BOOL test_QuerySecObj(struct dcerpc_pipe *p, 
256                              TALLOC_CTX *mem_ctx, 
257                              struct policy_handle *handle,
258                              struct policy_handle *acct_handle)
259 {
260         NTSTATUS status;
261         struct lsa_QuerySecObj r;
262
263         printf("Testing QuerySecObj\n");
264
265         r.in.handle = acct_handle;
266         r.in.sec_info = 7;
267
268         status = dcerpc_lsa_QuerySecObj(p, mem_ctx, &r);
269         if (!NT_STATUS_IS_OK(status)) {
270                 printf("QuerySecObj failed - %s\n", nt_errstr(status));
271                 return False;
272         }
273
274         return True;
275 }
276
277 static BOOL test_OpenAccount(struct dcerpc_pipe *p, 
278                              TALLOC_CTX *mem_ctx, 
279                              struct policy_handle *handle,
280                              struct dom_sid *sid)
281 {
282         NTSTATUS status;
283         struct lsa_OpenAccount r;
284         struct policy_handle acct_handle;
285
286         printf("Testing OpenAccount\n");
287
288         r.in.handle = handle;
289         r.in.sid = sid;
290         r.in.desired_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
291         r.out.acct_handle = &acct_handle;
292
293         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
294         if (!NT_STATUS_IS_OK(status)) {
295                 printf("OpenAccount failed - %s\n", nt_errstr(status));
296                 return False;
297         }
298
299         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
300                 return False;
301         }
302
303         if (!test_QuerySecObj(p, mem_ctx, handle, &acct_handle)) {
304                 return False;
305         }
306
307         return True;
308 }
309
310 static BOOL test_EnumAccounts(struct dcerpc_pipe *p, 
311                           TALLOC_CTX *mem_ctx, 
312                           struct policy_handle *handle)
313 {
314         NTSTATUS status;
315         struct lsa_EnumAccounts r;
316         struct lsa_SidArray sids1, sids2;
317         uint32 resume_handle = 0;
318         int i;
319
320         printf("\ntesting EnumAccounts\n");
321
322         r.in.handle = handle;
323         r.in.resume_handle = &resume_handle;
324         r.in.num_entries = 100;
325         r.out.resume_handle = &resume_handle;
326         r.out.sids = &sids1;
327
328         resume_handle = 0;
329         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
330         if (!NT_STATUS_IS_OK(status)) {
331                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
332                 return False;
333         }
334
335         if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
336                 return False;
337         }
338
339         printf("testing all accounts\n");
340         for (i=0;i<sids1.num_sids;i++) {
341                 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
342                 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
343         }
344         printf("\n");
345
346         if (sids1.num_sids < 3) {
347                 return True;
348         }
349         
350         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
351         resume_handle = 2;
352         r.in.num_entries = 1;
353         r.out.sids = &sids2;
354
355         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
356         if (!NT_STATUS_IS_OK(status)) {
357                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
358                 return False;
359         }
360
361         if (sids2.num_sids != 1) {
362                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
363                 return False;
364         }
365
366         return True;
367 }
368
369
370 static BOOL test_EnumPrivs(struct dcerpc_pipe *p, 
371                            TALLOC_CTX *mem_ctx, 
372                            struct policy_handle *handle)
373 {
374         NTSTATUS status;
375         struct lsa_EnumPrivs r;
376         struct lsa_PrivArray privs1;
377         uint32 resume_handle = 0;
378
379         printf("\ntesting EnumPrivs\n");
380
381         r.in.handle = handle;
382         r.in.resume_handle = &resume_handle;
383         r.in.max_count = 1000;
384         r.out.resume_handle = &resume_handle;
385         r.out.privs = &privs1;
386
387         resume_handle = 0;
388         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
389         if (!NT_STATUS_IS_OK(status)) {
390                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
391                 return False;
392         }
393
394         return True;
395 }
396
397
398 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p, 
399                               TALLOC_CTX *mem_ctx, 
400                               struct policy_handle *handle)
401 {
402         struct lsa_EnumTrustDom r;
403         NTSTATUS status;
404         uint32 resume_handle = 0;
405         struct lsa_DomainList domains;
406
407         printf("\nTesting EnumTrustDom\n");
408
409         r.in.handle = handle;
410         r.in.resume_handle = &resume_handle;
411         r.in.num_entries = 1000;
412         r.out.domains = &domains;
413         r.out.resume_handle = &resume_handle;
414
415         status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
416         if (!NT_STATUS_IS_OK(status)) {
417                 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
418                 return False;
419         }
420
421         return True;
422 }
423
424 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p, 
425                                  TALLOC_CTX *mem_ctx, 
426                                  struct policy_handle *handle)
427 {
428         struct lsa_QueryInfoPolicy r;
429         NTSTATUS status;
430         int i;
431         BOOL ret = True;
432
433         printf("\nTesting QueryInfoPolicy\n");
434
435         for (i=1;i<13;i++) {
436                 r.in.handle = handle;
437                 r.in.level = i;
438
439                 printf("\ntrying QueryInfoPolicy level %d\n", i);
440
441                 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
442                 if (!NT_STATUS_IS_OK(status)) {
443                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
444                         ret = False;
445                         continue;
446                 }
447         }
448
449         return ret;
450 }
451
452 static BOOL test_Delete(struct dcerpc_pipe *p, 
453                        TALLOC_CTX *mem_ctx, 
454                        struct policy_handle *handle)
455 {
456         NTSTATUS status;
457         struct lsa_Delete r;
458
459         printf("\ntesting Delete - but what does it do?\n");
460
461         r.in.handle = handle;
462         status = dcerpc_lsa_Delete(p, mem_ctx, &r);
463         if (!NT_STATUS_IS_OK(status)) {
464                 printf("Delete failed - %s\n", nt_errstr(status));
465                 return False;
466         }
467
468         printf("\n");
469
470         return True;
471 }
472
473 static BOOL test_Close(struct dcerpc_pipe *p, 
474                        TALLOC_CTX *mem_ctx, 
475                        struct policy_handle *handle)
476 {
477         NTSTATUS status;
478         struct lsa_Close r;
479         struct policy_handle handle2;
480
481         printf("\ntesting Close\n");
482
483         r.in.handle = handle;
484         r.out.handle = &handle2;
485
486         status = dcerpc_lsa_Close(p, mem_ctx, &r);
487         if (!NT_STATUS_IS_OK(status)) {
488                 printf("Close failed - %s\n", nt_errstr(status));
489                 return False;
490         }
491
492         status = dcerpc_lsa_Close(p, mem_ctx, &r);
493         /* its really a fault - we need a status code for rpc fault */
494         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
495                 printf("Close failed - %s\n", nt_errstr(status));
496                 return False;
497         }
498
499         printf("\n");
500
501         return True;
502 }
503
504 BOOL torture_rpc_lsa(int dummy)
505 {
506         NTSTATUS status;
507         struct dcerpc_pipe *p;
508         TALLOC_CTX *mem_ctx;
509         BOOL ret = True;
510         struct policy_handle handle;
511
512         mem_ctx = talloc_init("torture_rpc_lsa");
513
514         status = torture_rpc_connection(&p, 
515                                         DCERPC_LSARPC_NAME, 
516                                         DCERPC_LSARPC_UUID, 
517                                         DCERPC_LSARPC_VERSION);
518         if (!NT_STATUS_IS_OK(status)) {
519                 return False;
520         }
521         
522         p->flags |= DCERPC_DEBUG_PRINT_BOTH;
523
524         if (!test_OpenPolicy(p, mem_ctx)) {
525                 ret = False;
526         }
527
528         if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
529                 ret = False;
530         }
531
532         if (!test_EnumAccounts(p, mem_ctx, &handle)) {
533                 ret = False;
534         }
535
536         if (!test_EnumPrivs(p, mem_ctx, &handle)) {
537                 ret = False;
538         }
539
540         if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
541                 ret = False;
542         }
543
544         if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
545                 ret = False;
546         }
547         
548 #if 0
549         if (!test_Delete(p, mem_ctx, &handle)) {
550                 ret = False;
551         }
552 #endif
553         
554         if (!test_Close(p, mem_ctx, &handle)) {
555                 ret = False;
556         }
557
558         torture_rpc_close(p);
559
560         return ret;
561 }