r2266: yay! LSA session keys on TCP now work!
[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 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
500 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p, 
501                                    TALLOC_CTX *mem_ctx, 
502                                    struct policy_handle *acct_handle,
503                                    struct dom_sid *sid)
504 {
505         NTSTATUS status;
506         struct lsa_EnumAccountRights r;
507         struct lsa_RightSet rights;
508
509         printf("Testing EnumAccountRights\n");
510
511         r.in.handle = acct_handle;
512         r.in.sid = sid;
513         r.out.rights = &rights;
514
515         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
516         if (!NT_STATUS_IS_OK(status)) {
517                 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
518                 return False;
519         }
520
521         return True;
522 }
523
524
525 static BOOL test_QuerySecObj(struct dcerpc_pipe *p, 
526                              TALLOC_CTX *mem_ctx, 
527                              struct policy_handle *handle,
528                              struct policy_handle *acct_handle)
529 {
530         NTSTATUS status;
531         struct lsa_QuerySecObj r;
532
533         printf("Testing QuerySecObj\n");
534
535         r.in.handle = acct_handle;
536         r.in.sec_info = 7;
537
538         status = dcerpc_lsa_QuerySecObj(p, mem_ctx, &r);
539         if (!NT_STATUS_IS_OK(status)) {
540                 printf("QuerySecObj failed - %s\n", nt_errstr(status));
541                 return False;
542         }
543
544         return True;
545 }
546
547 static BOOL test_OpenAccount(struct dcerpc_pipe *p, 
548                              TALLOC_CTX *mem_ctx, 
549                              struct policy_handle *handle,
550                              struct dom_sid *sid)
551 {
552         NTSTATUS status;
553         struct lsa_OpenAccount r;
554         struct policy_handle acct_handle;
555
556         printf("Testing OpenAccount\n");
557
558         r.in.handle = handle;
559         r.in.sid = sid;
560         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
561         r.out.acct_handle = &acct_handle;
562
563         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
564         if (!NT_STATUS_IS_OK(status)) {
565                 printf("OpenAccount failed - %s\n", nt_errstr(status));
566                 return False;
567         }
568
569         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
570                 return False;
571         }
572
573         if (!test_QuerySecObj(p, mem_ctx, handle, &acct_handle)) {
574                 return False;
575         }
576
577         return True;
578 }
579
580 static BOOL test_EnumAccounts(struct dcerpc_pipe *p, 
581                           TALLOC_CTX *mem_ctx, 
582                           struct policy_handle *handle)
583 {
584         NTSTATUS status;
585         struct lsa_EnumAccounts r;
586         struct lsa_SidArray sids1, sids2;
587         uint32_t resume_handle = 0;
588         int i;
589
590         printf("\ntesting EnumAccounts\n");
591
592         r.in.handle = handle;
593         r.in.resume_handle = &resume_handle;
594         r.in.num_entries = 100;
595         r.out.resume_handle = &resume_handle;
596         r.out.sids = &sids1;
597
598         resume_handle = 0;
599         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
600         if (!NT_STATUS_IS_OK(status)) {
601                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
602                 return False;
603         }
604
605         if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
606                 return False;
607         }
608
609         printf("testing all accounts\n");
610         for (i=0;i<sids1.num_sids;i++) {
611                 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
612                 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
613         }
614         printf("\n");
615
616         if (sids1.num_sids < 3) {
617                 return True;
618         }
619         
620         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
621         resume_handle = 2;
622         r.in.num_entries = 1;
623         r.out.sids = &sids2;
624
625         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
626         if (!NT_STATUS_IS_OK(status)) {
627                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
628                 return False;
629         }
630
631         if (sids2.num_sids != 1) {
632                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
633                 return False;
634         }
635
636         return True;
637 }
638
639 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
640                                 TALLOC_CTX *mem_ctx,
641                                 struct policy_handle *handle,
642                                 struct lsa_Name *priv_name)
643 {
644         struct lsa_LookupPrivDisplayName r;
645         NTSTATUS status;
646         /* produce a reasonable range of language output without screwing up
647            terminals */
648         uint16 language_id = (random() % 4) + 0x409;
649
650         printf("testing LookupPrivDisplayName(%s)\n", priv_name->name);
651         
652         r.in.handle = handle;
653         r.in.name = priv_name;
654         r.in.language_id = &language_id;
655         r.out.language_id = &language_id;
656         r.in.unknown = 0;
657
658         status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
659         if (!NT_STATUS_IS_OK(status)) {
660                 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
661                 return False;
662         }
663         printf("%s -> \"%s\"  (language 0x%x/0x%x)\n", 
664                priv_name->name, r.out.disp_name->name, 
665                *r.in.language_id, *r.out.language_id);
666
667         return True;
668 }
669
670 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p, 
671                                 TALLOC_CTX *mem_ctx,
672                                 struct policy_handle *handle,
673                                 struct lsa_Name *priv_name)
674 {
675         struct lsa_EnumAccountsWithUserRight r;
676         struct lsa_SidArray sids;
677         NTSTATUS status;
678
679         ZERO_STRUCT(sids);
680         
681         printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->name);
682         
683         r.in.handle = handle;
684         r.in.name = priv_name;
685         r.out.sids = &sids;
686
687         status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
688
689         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
690         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
691                 return True;
692         }
693
694         if (!NT_STATUS_IS_OK(status)) {
695                 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
696                 return False;
697         }
698         
699         return True;
700 }
701
702
703 static BOOL test_EnumPrivs(struct dcerpc_pipe *p, 
704                            TALLOC_CTX *mem_ctx, 
705                            struct policy_handle *handle)
706 {
707         NTSTATUS status;
708         struct lsa_EnumPrivs r;
709         struct lsa_PrivArray privs1;
710         uint32_t resume_handle = 0;
711         int i;
712         BOOL ret = True;
713
714         printf("\ntesting EnumPrivs\n");
715
716         r.in.handle = handle;
717         r.in.resume_handle = &resume_handle;
718         r.in.max_count = 1000;
719         r.out.resume_handle = &resume_handle;
720         r.out.privs = &privs1;
721
722         resume_handle = 0;
723         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
724         if (!NT_STATUS_IS_OK(status)) {
725                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
726                 return False;
727         }
728
729         for (i = 0; i< privs1.count; i++) {
730                 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
731                 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
732                         ret = False;
733                 }
734         }
735
736         return ret;
737 }
738
739
740 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p, 
741                               TALLOC_CTX *mem_ctx, 
742                               struct policy_handle *handle)
743 {
744         struct lsa_EnumTrustDom r;
745         NTSTATUS status;
746         uint32_t resume_handle = 0;
747         struct lsa_DomainList domains;
748
749         printf("\nTesting EnumTrustDom\n");
750
751         r.in.handle = handle;
752         r.in.resume_handle = &resume_handle;
753         r.in.num_entries = 1000;
754         r.out.domains = &domains;
755         r.out.resume_handle = &resume_handle;
756
757         status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
758
759         /* NO_MORE_ENTRIES is allowed */
760         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
761                 return True;
762         }
763
764         if (!NT_STATUS_IS_OK(status)) {
765                 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
766                 return False;
767         }
768
769         return True;
770 }
771
772 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p, 
773                                  TALLOC_CTX *mem_ctx, 
774                                  struct policy_handle *handle)
775 {
776         struct lsa_QueryInfoPolicy r;
777         NTSTATUS status;
778         int i;
779         BOOL ret = True;
780         printf("\nTesting QueryInfoPolicy\n");
781
782         for (i=1;i<13;i++) {
783                 r.in.handle = handle;
784                 r.in.level = i;
785
786                 printf("\ntrying QueryInfoPolicy level %d\n", i);
787
788                 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
789
790                 if ((i == 9 || i == 10 || i == 11) &&
791                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
792                         printf("server failed level %u (OK)\n", i);
793                         continue;
794                 }
795
796                 if (!NT_STATUS_IS_OK(status)) {
797                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
798                         ret = False;
799                         continue;
800                 }
801         }
802
803         return ret;
804 }
805
806 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p, 
807                                   TALLOC_CTX *mem_ctx, 
808                                   struct policy_handle *handle)
809 {
810         struct lsa_QueryInfoPolicy2 r;
811         NTSTATUS status;
812         int i;
813         BOOL ret = True;
814         printf("\nTesting QueryInfoPolicy2\n");
815
816         for (i=1;i<13;i++) {
817                 r.in.handle = handle;
818                 r.in.level = i;
819
820                 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
821
822                 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
823
824                 if ((i == 9 || i == 10 || i == 11) &&
825                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
826                         printf("server failed level %u (OK)\n", i);
827                         continue;
828                 }
829
830                 if (!NT_STATUS_IS_OK(status)) {
831                         printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
832                         ret = False;
833                         continue;
834                 }
835         }
836
837         return ret;
838 }
839
840 static BOOL test_Close(struct dcerpc_pipe *p, 
841                        TALLOC_CTX *mem_ctx, 
842                        struct policy_handle *handle)
843 {
844         NTSTATUS status;
845         struct lsa_Close r;
846         struct policy_handle handle2;
847
848         printf("\ntesting Close\n");
849
850         r.in.handle = handle;
851         r.out.handle = &handle2;
852
853         status = dcerpc_lsa_Close(p, mem_ctx, &r);
854         if (!NT_STATUS_IS_OK(status)) {
855                 printf("Close failed - %s\n", nt_errstr(status));
856                 return False;
857         }
858
859         status = dcerpc_lsa_Close(p, mem_ctx, &r);
860         /* its really a fault - we need a status code for rpc fault */
861         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
862                 printf("Close failed - %s\n", nt_errstr(status));
863                 return False;
864         }
865
866         printf("\n");
867
868         return True;
869 }
870
871 BOOL torture_rpc_lsa(int dummy)
872 {
873         NTSTATUS status;
874         struct dcerpc_pipe *p;
875         TALLOC_CTX *mem_ctx;
876         BOOL ret = True;
877         struct policy_handle handle;
878
879         mem_ctx = talloc_init("torture_rpc_lsa");
880
881         status = torture_rpc_connection(&p, 
882                                         DCERPC_LSARPC_NAME, 
883                                         DCERPC_LSARPC_UUID, 
884                                         DCERPC_LSARPC_VERSION);
885         if (!NT_STATUS_IS_OK(status)) {
886                 return False;
887         }
888
889         if (!test_OpenPolicy(p, mem_ctx)) {
890                 ret = False;
891         }
892
893         if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
894                 ret = False;
895         }
896
897         if (!test_many_LookupSids(p, mem_ctx, &handle)) {
898                 ret = False;
899         }
900
901         if (!test_CreateAccount(p, mem_ctx, &handle)) {
902                 ret = False;
903         }
904
905         if (!test_CreateSecret(p, mem_ctx, &handle)) {
906                 ret = False;
907         }
908
909         if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
910                 ret = False;
911         }
912
913         if (!test_EnumAccounts(p, mem_ctx, &handle)) {
914                 ret = False;
915         }
916
917         if (!test_EnumPrivs(p, mem_ctx, &handle)) {
918                 ret = False;
919         }
920
921         if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
922                 ret = False;
923         }
924
925         if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
926                 ret = False;
927         }
928
929         if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
930                 ret = False;
931         }
932         
933 #if 0
934         if (!test_Delete(p, mem_ctx, &handle)) {
935                 ret = False;
936         }
937 #endif
938         
939         if (!test_Close(p, mem_ctx, &handle)) {
940                 ret = False;
941         }
942
943         talloc_destroy(mem_ctx);
944
945         torture_rpc_close(p);
946
947         return ret;
948 }