r2199: the unknown 16 bit number in lsa_LookupPrivDisplayName() is a language
[kai/samba.git] / source / 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 void init_lsa_Name(struct lsa_Name *name, const char *s)
25 {
26         name->name = s;
27 }
28
29 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
30 {
31         struct lsa_ObjectAttribute attr;
32         struct policy_handle handle;
33         struct lsa_QosInfo qos;
34         struct lsa_OpenPolicy r;
35         NTSTATUS status;
36         uint16_t system_name = '\\';
37
38         printf("\ntesting OpenPolicy\n");
39
40         qos.len = 0;
41         qos.impersonation_level = 2;
42         qos.context_mode = 1;
43         qos.effective_only = 0;
44
45         attr.len = 0;
46         attr.root_dir = NULL;
47         attr.object_name = NULL;
48         attr.attributes = 0;
49         attr.sec_desc = NULL;
50         attr.sec_qos = &qos;
51
52         r.in.system_name = &system_name;
53         r.in.attr = &attr;
54         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
55         r.out.handle = &handle;
56
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));
60                 return False;
61         }
62
63         return True;
64 }
65
66
67 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
68                              struct policy_handle *handle)
69 {
70         struct lsa_ObjectAttribute attr;
71         struct lsa_QosInfo qos;
72         struct lsa_OpenPolicy2 r;
73         NTSTATUS status;
74
75         printf("\ntesting OpenPolicy2\n");
76
77         qos.len = 0;
78         qos.impersonation_level = 2;
79         qos.context_mode = 1;
80         qos.effective_only = 0;
81
82         attr.len = 0;
83         attr.root_dir = NULL;
84         attr.object_name = NULL;
85         attr.attributes = 0;
86         attr.sec_desc = NULL;
87         attr.sec_qos = &qos;
88
89         r.in.system_name = "\\";
90         r.in.attr = &attr;
91         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
92         r.out.handle = handle;
93
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));
97                 return False;
98         }
99
100         return True;
101 }
102
103 static BOOL test_LookupNames(struct dcerpc_pipe *p, 
104                             TALLOC_CTX *mem_ctx, 
105                             struct policy_handle *handle,
106                             struct lsa_TransNameArray *tnames)
107 {
108         struct lsa_LookupNames r;
109         struct lsa_TransSidArray sids;
110         struct lsa_Name *names;
111         uint32_t count = 0;
112         NTSTATUS status;
113         int i;
114
115         printf("\nTesting LookupNames with %d names\n", tnames->count);
116
117         sids.count = 0;
118         sids.sids = NULL;
119
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);
123         }
124
125         r.in.handle = handle;
126         r.in.num_names = tnames->count;
127         r.in.names = names;
128         r.in.sids = &sids;
129         r.in.level = 1;
130         r.in.count = &count;
131         r.out.count = &count;
132         r.out.sids = &sids;
133
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));
137                 return False;
138         }
139
140         printf("\n");
141
142         return True;
143 }
144
145
146 static BOOL test_LookupSids(struct dcerpc_pipe *p, 
147                             TALLOC_CTX *mem_ctx, 
148                             struct policy_handle *handle,
149                             struct lsa_SidArray *sids)
150 {
151         struct lsa_LookupSids r;
152         struct lsa_TransNameArray names;
153         uint32_t count = sids->num_sids;
154         NTSTATUS status;
155
156         printf("\nTesting LookupSids\n");
157
158         names.count = 0;
159         names.names = NULL;
160
161         r.in.handle = handle;
162         r.in.sids = sids;
163         r.in.names = &names;
164         r.in.level = 1;
165         r.in.count = &count;
166         r.out.count = &count;
167         r.out.names = &names;
168
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));
172                 return False;
173         }
174
175         printf("\n");
176
177         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
178                 return False;
179         }
180
181         return True;
182 }
183
184 static BOOL test_many_LookupSids(struct dcerpc_pipe *p, 
185                                  TALLOC_CTX *mem_ctx, 
186                                  struct policy_handle *handle)
187 {
188         struct lsa_LookupSids r;
189         struct lsa_TransNameArray names;
190         uint32_t count;
191         NTSTATUS status;
192         struct lsa_SidArray sids;
193         int i;
194
195         printf("\nTesting LookupSids with lots of SIDs\n");
196
197         names.count = 0;
198         names.names = NULL;
199
200         sids.num_sids = 1000;
201
202         sids.sids = talloc_array_p(mem_ctx, struct lsa_SidPtr, sids.num_sids);
203
204         for (i=0; i<sids.num_sids; i++) {
205                 const char *sidstr = "S-1-5-32-545";
206                 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
207         }
208
209         count = sids.num_sids;
210
211         r.in.handle = handle;
212         r.in.sids = &sids;
213         r.in.names = &names;
214         r.in.level = 1;
215         r.in.count = &names.count;
216         r.out.count = &count;
217         r.out.names = &names;
218
219         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
220         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
221                 printf("LookupSids failed - %s\n", nt_errstr(status));
222                 return False;
223         }
224
225         printf("\n");
226
227         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
228                 return False;
229         }
230
231         return True;
232 }
233
234 static BOOL test_LookupPrivName(struct dcerpc_pipe *p, 
235                                 TALLOC_CTX *mem_ctx, 
236                                 struct policy_handle *handle,
237                                 struct lsa_LUID *luid)
238 {
239         NTSTATUS status;
240         struct lsa_LookupPrivName r;
241
242         r.in.handle = handle;
243         r.in.luid = luid;
244
245         status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
246         if (!NT_STATUS_IS_OK(status)) {
247                 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
248                 return False;
249         }
250
251         return True;
252 }
253
254 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p, 
255                                   TALLOC_CTX *mem_ctx,                            
256                                   struct policy_handle *handle,
257                                   struct policy_handle *acct_handle)
258 {
259         NTSTATUS status;
260         struct lsa_EnumPrivsAccount r;
261
262         printf("Testing EnumPrivsAccount\n");
263
264         r.in.handle = acct_handle;
265
266         status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
267         if (!NT_STATUS_IS_OK(status)) {
268                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
269                 return False;
270         }
271
272         if (r.out.privs) {
273                 int i;
274                 for (i=0;i<r.out.privs->count;i++) {
275                         test_LookupPrivName(p, mem_ctx, handle, 
276                                             &r.out.privs->set[i].luid);
277                 }
278         }
279
280         return True;
281 }
282
283 static BOOL test_Delete(struct dcerpc_pipe *p, 
284                        TALLOC_CTX *mem_ctx, 
285                        struct policy_handle *handle)
286 {
287         NTSTATUS status;
288         struct lsa_Delete r;
289
290         printf("\ntesting Delete\n");
291
292         r.in.handle = handle;
293         status = dcerpc_lsa_Delete(p, mem_ctx, &r);
294         if (!NT_STATUS_IS_OK(status)) {
295                 printf("Delete failed - %s\n", nt_errstr(status));
296                 return False;
297         }
298
299         printf("\n");
300
301         return True;
302 }
303
304
305 static BOOL test_CreateAccount(struct dcerpc_pipe *p, 
306                                TALLOC_CTX *mem_ctx, 
307                                struct policy_handle *handle)
308 {
309         NTSTATUS status;
310         struct lsa_CreateAccount r;
311         struct dom_sid2 *newsid;
312         struct policy_handle acct_handle;
313
314         newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
315
316         printf("Testing CreateAccount\n");
317
318         r.in.handle = handle;
319         r.in.sid = newsid;
320         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
321         r.out.acct_handle = &acct_handle;
322
323         status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
324         if (!NT_STATUS_IS_OK(status)) {
325                 printf("CreateAccount failed - %s\n", nt_errstr(status));
326                 return False;
327         }
328
329         if (!test_Delete(p, mem_ctx, &acct_handle)) {
330                 return False;
331         }
332
333         return True;
334 }
335
336
337 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p, 
338                                      TALLOC_CTX *mem_ctx, 
339                                      struct policy_handle *handle)
340 {
341         NTSTATUS status;
342         struct lsa_CreateTrustedDomain r;
343         struct lsa_TrustInformation trustinfo;
344         struct dom_sid *domsid;
345         struct policy_handle dom_handle;
346
347         printf("Testing CreateTrustedDomain\n");
348
349         domsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-21-97398-379795-12345");
350
351         trustinfo.sid = domsid;
352         init_lsa_Name(&trustinfo.name, "torturedomain");
353
354         r.in.handle = handle;
355         r.in.info = &trustinfo;
356         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
357         r.out.dom_handle = &dom_handle;
358
359         status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
360         if (!NT_STATUS_IS_OK(status)) {
361                 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
362                 return False;
363         }
364
365         if (!test_Delete(p, mem_ctx, &dom_handle)) {
366                 return False;
367         }
368
369         return True;
370 }
371
372 static BOOL test_CreateSecret(struct dcerpc_pipe *p, 
373                               TALLOC_CTX *mem_ctx, 
374                               struct policy_handle *handle)
375 {
376         NTSTATUS status;
377         struct lsa_CreateSecret r;
378         struct lsa_OpenSecret r2;
379         struct lsa_SetSecret r3;
380         struct lsa_QuerySecret r4;
381         struct policy_handle sec_handle, sec_handle2;
382         struct lsa_Delete d;
383         struct lsa_DATA_BUF buf1;
384         struct lsa_DATA_BUF_PTR bufp1;
385         DATA_BLOB enc_key;
386         BOOL ret = True;
387         DATA_BLOB session_key;
388         NTTIME old_mtime, new_mtime;
389         DATA_BLOB blob1, blob2;
390         const char *secret1 = "abcdef12345699qwerty";
391         char *secret2;
392         char *secname;
393
394         printf("Testing CreateSecret\n");
395
396         asprintf(&secname, "torturesecret-%u", (uint_t)random());
397
398         init_lsa_Name(&r.in.name, secname);
399
400         r.in.handle = handle;
401         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
402         r.out.sec_handle = &sec_handle;
403
404         status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
405         if (!NT_STATUS_IS_OK(status)) {
406                 printf("CreateSecret failed - %s\n", nt_errstr(status));
407                 return False;
408         }
409
410         r2.in.handle = handle;
411         r2.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
412         r2.in.name = r.in.name;
413         r2.out.sec_handle = &sec_handle2;
414
415         printf("Testing OpenSecret\n");
416
417         status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
418         if (!NT_STATUS_IS_OK(status)) {
419                 printf("OpenSecret failed - %s\n", nt_errstr(status));
420                 ret = False;
421         }
422
423         status = dcerpc_fetch_session_key(p, &session_key);
424         if (!NT_STATUS_IS_OK(status)) {
425                 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
426                 ret = False;
427         }
428
429         enc_key = sess_encrypt_string(secret1, &session_key);
430
431         r3.in.handle = &sec_handle;
432         r3.in.new_val = &buf1;
433         r3.in.old_val = NULL;
434         r3.in.new_val->data = enc_key.data;
435         r3.in.new_val->length = enc_key.length;
436         r3.in.new_val->size = enc_key.length;
437
438         printf("Testing SetSecret\n");
439
440         status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
441         if (!NT_STATUS_IS_OK(status)) {
442                 printf("SetSecret failed - %s\n", nt_errstr(status));
443                 ret = False;
444         }
445
446         data_blob_free(&enc_key);
447
448         ZERO_STRUCT(new_mtime);
449         ZERO_STRUCT(old_mtime);
450
451         /* fetch the secret back again */
452         r4.in.handle = &sec_handle;
453         r4.in.new_val = &bufp1;
454         r4.in.new_mtime = &new_mtime;
455         r4.in.old_val = NULL;
456         r4.in.old_mtime = NULL;
457
458         bufp1.buf = NULL;
459
460         status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
461         if (!NT_STATUS_IS_OK(status)) {
462                 printf("QuerySecret failed - %s\n", nt_errstr(status));
463                 ret = False;
464         }
465
466         if (r4.out.new_val->buf == NULL) {
467                 printf("No secret buffer returned\n");
468                 ret = False;
469         } else {
470                 blob1.data = r4.out.new_val->buf->data;
471                 blob1.length = r4.out.new_val->buf->length;
472
473                 blob2 = data_blob(NULL, blob1.length);
474
475                 secret2 = sess_decrypt_string(&blob1, &session_key);
476
477                 printf("returned secret '%s'\n", secret2);
478
479                 if (strcmp(secret1, secret2) != 0) {
480                         printf("Returned secret doesn't match\n");
481                         ret = False;
482                 }
483         }
484
485         if (!test_Delete(p, mem_ctx, &sec_handle)) {
486                 ret = False;
487         }
488
489         d.in.handle = &sec_handle2;
490         status = dcerpc_lsa_Delete(p, mem_ctx, &d);
491         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
492                 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
493                 ret = False;
494         }
495
496         return ret;
497 }
498
499 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p, 
500                                    TALLOC_CTX *mem_ctx, 
501                                    struct policy_handle *acct_handle,
502                                    struct dom_sid *sid)
503 {
504         NTSTATUS status;
505         struct lsa_EnumAccountRights r;
506         struct lsa_RightSet rights;
507
508         printf("Testing EnumAccountRights\n");
509
510         r.in.handle = acct_handle;
511         r.in.sid = sid;
512         r.out.rights = &rights;
513
514         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
515         if (!NT_STATUS_IS_OK(status)) {
516                 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
517                 return False;
518         }
519
520         return True;
521 }
522
523
524 static BOOL test_QuerySecObj(struct dcerpc_pipe *p, 
525                              TALLOC_CTX *mem_ctx, 
526                              struct policy_handle *handle,
527                              struct policy_handle *acct_handle)
528 {
529         NTSTATUS status;
530         struct lsa_QuerySecObj r;
531
532         printf("Testing QuerySecObj\n");
533
534         r.in.handle = acct_handle;
535         r.in.sec_info = 7;
536
537         status = dcerpc_lsa_QuerySecObj(p, mem_ctx, &r);
538         if (!NT_STATUS_IS_OK(status)) {
539                 printf("QuerySecObj failed - %s\n", nt_errstr(status));
540                 return False;
541         }
542
543         return True;
544 }
545
546 static BOOL test_OpenAccount(struct dcerpc_pipe *p, 
547                              TALLOC_CTX *mem_ctx, 
548                              struct policy_handle *handle,
549                              struct dom_sid *sid)
550 {
551         NTSTATUS status;
552         struct lsa_OpenAccount r;
553         struct policy_handle acct_handle;
554
555         printf("Testing OpenAccount\n");
556
557         r.in.handle = handle;
558         r.in.sid = sid;
559         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
560         r.out.acct_handle = &acct_handle;
561
562         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
563         if (!NT_STATUS_IS_OK(status)) {
564                 printf("OpenAccount failed - %s\n", nt_errstr(status));
565                 return False;
566         }
567
568         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
569                 return False;
570         }
571
572         if (!test_QuerySecObj(p, mem_ctx, handle, &acct_handle)) {
573                 return False;
574         }
575
576         return True;
577 }
578
579 static BOOL test_EnumAccounts(struct dcerpc_pipe *p, 
580                           TALLOC_CTX *mem_ctx, 
581                           struct policy_handle *handle)
582 {
583         NTSTATUS status;
584         struct lsa_EnumAccounts r;
585         struct lsa_SidArray sids1, sids2;
586         uint32_t resume_handle = 0;
587         int i;
588
589         printf("\ntesting EnumAccounts\n");
590
591         r.in.handle = handle;
592         r.in.resume_handle = &resume_handle;
593         r.in.num_entries = 100;
594         r.out.resume_handle = &resume_handle;
595         r.out.sids = &sids1;
596
597         resume_handle = 0;
598         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
599         if (!NT_STATUS_IS_OK(status)) {
600                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
601                 return False;
602         }
603
604         if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
605                 return False;
606         }
607
608         printf("testing all accounts\n");
609         for (i=0;i<sids1.num_sids;i++) {
610                 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
611                 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
612         }
613         printf("\n");
614
615         if (sids1.num_sids < 3) {
616                 return True;
617         }
618         
619         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
620         resume_handle = 2;
621         r.in.num_entries = 1;
622         r.out.sids = &sids2;
623
624         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
625         if (!NT_STATUS_IS_OK(status)) {
626                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
627                 return False;
628         }
629
630         if (sids2.num_sids != 1) {
631                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
632                 return False;
633         }
634
635         return True;
636 }
637
638 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
639                                 TALLOC_CTX *mem_ctx,
640                                 struct policy_handle *handle,
641                                 struct lsa_Name *priv_name)
642 {
643         struct lsa_LookupPrivDisplayName r;
644         NTSTATUS status;
645         /* produce a reasonable range of language output without screwing up
646            terminals */
647         uint16 language_id = (random() % 4) + 0x409;
648
649         printf("testing LookupPrivDisplayName(%s)\n", priv_name->name);
650         
651         r.in.handle = handle;
652         r.in.name = priv_name;
653         r.in.language_id = &language_id;
654         r.out.language_id = &language_id;
655         r.in.unknown = 0;
656
657         status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
658         if (!NT_STATUS_IS_OK(status)) {
659                 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
660                 return False;
661         }
662         printf("%s -> \"%s\"  (language 0x%x/0x%x)\n", 
663                priv_name->name, r.out.disp_name->name, 
664                *r.in.language_id, *r.out.language_id);
665
666         return True;
667 }
668
669 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p, 
670                                 TALLOC_CTX *mem_ctx,
671                                 struct policy_handle *handle,
672                                 struct lsa_Name *priv_name)
673 {
674         struct lsa_EnumAccountsWithUserRight r;
675         struct lsa_SidArray sids;
676         NTSTATUS status;
677
678         ZERO_STRUCT(sids);
679         
680         printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->name);
681         
682         r.in.handle = handle;
683         r.in.name = priv_name;
684         r.out.sids = &sids;
685
686         status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
687
688         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
689         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
690                 return True;
691         }
692
693         if (!NT_STATUS_IS_OK(status)) {
694                 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
695                 return False;
696         }
697         
698         return True;
699 }
700
701
702 static BOOL test_EnumPrivs(struct dcerpc_pipe *p, 
703                            TALLOC_CTX *mem_ctx, 
704                            struct policy_handle *handle)
705 {
706         NTSTATUS status;
707         struct lsa_EnumPrivs r;
708         struct lsa_PrivArray privs1;
709         uint32_t resume_handle = 0;
710         int i;
711         BOOL ret = True;
712
713         printf("\ntesting EnumPrivs\n");
714
715         r.in.handle = handle;
716         r.in.resume_handle = &resume_handle;
717         r.in.max_count = 1000;
718         r.out.resume_handle = &resume_handle;
719         r.out.privs = &privs1;
720
721         resume_handle = 0;
722         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
723         if (!NT_STATUS_IS_OK(status)) {
724                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
725                 return False;
726         }
727
728         for (i = 0; i< privs1.count; i++) {
729                 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
730                 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
731                         ret = False;
732                 }
733         }
734
735         return ret;
736 }
737
738
739 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p, 
740                               TALLOC_CTX *mem_ctx, 
741                               struct policy_handle *handle)
742 {
743         struct lsa_EnumTrustDom r;
744         NTSTATUS status;
745         uint32_t resume_handle = 0;
746         struct lsa_DomainList domains;
747
748         printf("\nTesting EnumTrustDom\n");
749
750         r.in.handle = handle;
751         r.in.resume_handle = &resume_handle;
752         r.in.num_entries = 1000;
753         r.out.domains = &domains;
754         r.out.resume_handle = &resume_handle;
755
756         status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
757
758         /* NO_MORE_ENTRIES is allowed */
759         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
760                 return True;
761         }
762
763         if (!NT_STATUS_IS_OK(status)) {
764                 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
765                 return False;
766         }
767
768         return True;
769 }
770
771 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p, 
772                                  TALLOC_CTX *mem_ctx, 
773                                  struct policy_handle *handle)
774 {
775         struct lsa_QueryInfoPolicy r;
776         NTSTATUS status;
777         int i;
778         BOOL ret = True;
779         printf("\nTesting QueryInfoPolicy\n");
780
781         for (i=1;i<13;i++) {
782                 r.in.handle = handle;
783                 r.in.level = i;
784
785                 printf("\ntrying QueryInfoPolicy level %d\n", i);
786
787                 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
788
789                 if ((i == 9 || i == 10 || i == 11) &&
790                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
791                         printf("server failed level %u (OK)\n", i);
792                         continue;
793                 }
794
795                 if (!NT_STATUS_IS_OK(status)) {
796                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
797                         ret = False;
798                         continue;
799                 }
800         }
801
802         return ret;
803 }
804
805 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p, 
806                                   TALLOC_CTX *mem_ctx, 
807                                   struct policy_handle *handle)
808 {
809         struct lsa_QueryInfoPolicy2 r;
810         NTSTATUS status;
811         int i;
812         BOOL ret = True;
813         printf("\nTesting QueryInfoPolicy2\n");
814
815         for (i=1;i<13;i++) {
816                 r.in.handle = handle;
817                 r.in.level = i;
818
819                 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
820
821                 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
822
823                 if ((i == 9 || i == 10 || i == 11) &&
824                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
825                         printf("server failed level %u (OK)\n", i);
826                         continue;
827                 }
828
829                 if (!NT_STATUS_IS_OK(status)) {
830                         printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
831                         ret = False;
832                         continue;
833                 }
834         }
835
836         return ret;
837 }
838
839 static BOOL test_Close(struct dcerpc_pipe *p, 
840                        TALLOC_CTX *mem_ctx, 
841                        struct policy_handle *handle)
842 {
843         NTSTATUS status;
844         struct lsa_Close r;
845         struct policy_handle handle2;
846
847         printf("\ntesting Close\n");
848
849         r.in.handle = handle;
850         r.out.handle = &handle2;
851
852         status = dcerpc_lsa_Close(p, mem_ctx, &r);
853         if (!NT_STATUS_IS_OK(status)) {
854                 printf("Close failed - %s\n", nt_errstr(status));
855                 return False;
856         }
857
858         status = dcerpc_lsa_Close(p, mem_ctx, &r);
859         /* its really a fault - we need a status code for rpc fault */
860         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
861                 printf("Close failed - %s\n", nt_errstr(status));
862                 return False;
863         }
864
865         printf("\n");
866
867         return True;
868 }
869
870 BOOL torture_rpc_lsa(int dummy)
871 {
872         NTSTATUS status;
873         struct dcerpc_pipe *p;
874         TALLOC_CTX *mem_ctx;
875         BOOL ret = True;
876         struct policy_handle handle;
877
878         mem_ctx = talloc_init("torture_rpc_lsa");
879
880         status = torture_rpc_connection(&p, 
881                                         DCERPC_LSARPC_NAME, 
882                                         DCERPC_LSARPC_UUID, 
883                                         DCERPC_LSARPC_VERSION);
884         if (!NT_STATUS_IS_OK(status)) {
885                 return False;
886         }
887
888         if (!test_OpenPolicy(p, mem_ctx)) {
889                 ret = False;
890         }
891
892         if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
893                 ret = False;
894         }
895
896         if (!test_many_LookupSids(p, mem_ctx, &handle)) {
897                 ret = False;
898         }
899
900         if (!test_CreateAccount(p, mem_ctx, &handle)) {
901                 ret = False;
902         }
903
904         if (!test_CreateSecret(p, mem_ctx, &handle)) {
905                 ret = False;
906         }
907
908         if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
909                 ret = False;
910         }
911
912         if (!test_EnumAccounts(p, mem_ctx, &handle)) {
913                 ret = False;
914         }
915
916         if (!test_EnumPrivs(p, mem_ctx, &handle)) {
917                 ret = False;
918         }
919
920         if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
921                 ret = False;
922         }
923
924         if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
925                 ret = False;
926         }
927
928         if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
929                 ret = False;
930         }
931         
932 #if 0
933         if (!test_Delete(p, mem_ctx, &handle)) {
934                 ret = False;
935         }
936 #endif
937         
938         if (!test_Close(p, mem_ctx, &handle)) {
939                 ret = False;
940         }
941
942         talloc_destroy(mem_ctx);
943
944         torture_rpc_close(p);
945
946         return ret;
947 }