r14402: Generate seperate headers for RPC client functions.
[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    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "torture/torture.h"
25 #include "librpc/gen_ndr/ndr_lsa.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
27 #include "lib/events/events.h"
28 #include "libcli/security/proto.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "torture/rpc/rpc.h"
31
32 static void init_lsa_String(struct lsa_String *name, const char *s)
33 {
34         name->string = s;
35 }
36
37 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
38 {
39         struct lsa_ObjectAttribute attr;
40         struct policy_handle handle;
41         struct lsa_QosInfo qos;
42         struct lsa_OpenPolicy r;
43         NTSTATUS status;
44         uint16_t system_name = '\\';
45
46         printf("\ntesting OpenPolicy\n");
47
48         qos.len = 0;
49         qos.impersonation_level = 2;
50         qos.context_mode = 1;
51         qos.effective_only = 0;
52
53         attr.len = 0;
54         attr.root_dir = NULL;
55         attr.object_name = NULL;
56         attr.attributes = 0;
57         attr.sec_desc = NULL;
58         attr.sec_qos = &qos;
59
60         r.in.system_name = &system_name;
61         r.in.attr = &attr;
62         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
63         r.out.handle = &handle;
64
65         status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
66         if (!NT_STATUS_IS_OK(status)) {
67                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
68                     NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
69                         printf("not considering %s to be an error\n", nt_errstr(status));
70                         return True;
71                 }
72                 printf("OpenPolicy failed - %s\n", nt_errstr(status));
73                 return False;
74         }
75
76         return True;
77 }
78
79
80 BOOL test_lsa_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
81                           struct policy_handle **handle)
82 {
83         struct lsa_ObjectAttribute attr;
84         struct lsa_QosInfo qos;
85         struct lsa_OpenPolicy2 r;
86         NTSTATUS status;
87
88         printf("\ntesting OpenPolicy2\n");
89
90         *handle = talloc(mem_ctx, struct policy_handle);
91         if (!*handle) {
92                 return False;
93         }
94
95         qos.len = 0;
96         qos.impersonation_level = 2;
97         qos.context_mode = 1;
98         qos.effective_only = 0;
99
100         attr.len = 0;
101         attr.root_dir = NULL;
102         attr.object_name = NULL;
103         attr.attributes = 0;
104         attr.sec_desc = NULL;
105         attr.sec_qos = &qos;
106
107         r.in.system_name = "\\";
108         r.in.attr = &attr;
109         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
110         r.out.handle = *handle;
111
112         status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
113         if (!NT_STATUS_IS_OK(status)) {
114                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
115                     NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
116                         printf("not considering %s to be an error\n", nt_errstr(status));
117                         talloc_free(*handle);
118                         *handle = NULL;
119                         return True;
120                 }
121                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
122                 return False;
123         }
124
125         return True;
126 }
127
128 static BOOL test_LookupNames(struct dcerpc_pipe *p, 
129                             TALLOC_CTX *mem_ctx, 
130                             struct policy_handle *handle,
131                             struct lsa_TransNameArray *tnames)
132 {
133         struct lsa_LookupNames r;
134         struct lsa_TransSidArray sids;
135         struct lsa_String *names;
136         uint32_t count = 0;
137         NTSTATUS status;
138         int i;
139
140         printf("\nTesting LookupNames with %d names\n", tnames->count);
141
142         sids.count = 0;
143         sids.sids = NULL;
144
145         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
146         for (i=0;i<tnames->count;i++) {
147                 init_lsa_String(&names[i], tnames->names[i].name.string);
148         }
149
150         r.in.handle = handle;
151         r.in.num_names = tnames->count;
152         r.in.names = names;
153         r.in.sids = &sids;
154         r.in.level = 1;
155         r.in.count = &count;
156         r.out.count = &count;
157         r.out.sids = &sids;
158
159         status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
160         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
161                 printf("LookupNames failed - %s\n", nt_errstr(status));
162                 return False;
163         }
164
165         printf("\n");
166
167         return True;
168 }
169
170 static BOOL test_LookupNames2(struct dcerpc_pipe *p, 
171                               TALLOC_CTX *mem_ctx, 
172                               struct policy_handle *handle,
173                               struct lsa_TransNameArray2 *tnames)
174 {
175         struct lsa_LookupNames2 r;
176         struct lsa_TransSidArray2 sids;
177         struct lsa_String *names;
178         uint32_t count = 0;
179         NTSTATUS status;
180         int i;
181
182         printf("\nTesting LookupNames2 with %d names\n", tnames->count);
183
184         sids.count = 0;
185         sids.sids = NULL;
186
187         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
188         for (i=0;i<tnames->count;i++) {
189                 init_lsa_String(&names[i], tnames->names[i].name.string);
190         }
191
192         r.in.handle = handle;
193         r.in.num_names = tnames->count;
194         r.in.names = names;
195         r.in.sids = &sids;
196         r.in.level = 1;
197         r.in.count = &count;
198         r.in.unknown1 = 0;
199         r.in.unknown2 = 0;
200         r.out.count = &count;
201         r.out.sids = &sids;
202
203         status = dcerpc_lsa_LookupNames2(p, mem_ctx, &r);
204         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
205                 printf("LookupNames2 failed - %s\n", nt_errstr(status));
206                 return False;
207         }
208
209         printf("\n");
210
211         return True;
212 }
213
214
215 static BOOL test_LookupNames3(struct dcerpc_pipe *p, 
216                               TALLOC_CTX *mem_ctx, 
217                               struct policy_handle *handle,
218                               struct lsa_TransNameArray2 *tnames)
219 {
220         struct lsa_LookupNames3 r;
221         struct lsa_TransSidArray3 sids;
222         struct lsa_String *names;
223         uint32_t count = 0;
224         NTSTATUS status;
225         int i;
226
227         printf("\nTesting LookupNames3 with %d names\n", tnames->count);
228
229         sids.count = 0;
230         sids.sids = NULL;
231
232         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
233         for (i=0;i<tnames->count;i++) {
234                 init_lsa_String(&names[i], tnames->names[i].name.string);
235         }
236
237         r.in.handle = handle;
238         r.in.num_names = tnames->count;
239         r.in.names = names;
240         r.in.sids = &sids;
241         r.in.level = 1;
242         r.in.count = &count;
243         r.in.unknown1 = 0;
244         r.in.unknown2 = 0;
245         r.out.count = &count;
246         r.out.sids = &sids;
247
248         status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
249         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
250                 printf("LookupNames3 failed - %s\n", nt_errstr(status));
251                 return False;
252         }
253
254         printf("\n");
255
256         return True;
257 }
258
259 static BOOL test_LookupNames4(struct dcerpc_pipe *p, 
260                               TALLOC_CTX *mem_ctx, 
261                               struct lsa_TransNameArray2 *tnames)
262 {
263         struct lsa_LookupNames4 r;
264         struct lsa_TransSidArray3 sids;
265         struct lsa_String *names;
266         uint32_t count = 0;
267         NTSTATUS status;
268         int i;
269
270         printf("\nTesting LookupNames4 with %d names\n", tnames->count);
271
272         sids.count = 0;
273         sids.sids = NULL;
274
275         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
276         for (i=0;i<tnames->count;i++) {
277                 init_lsa_String(&names[i], tnames->names[i].name.string);
278         }
279
280         r.in.num_names = tnames->count;
281         r.in.names = names;
282         r.in.sids = &sids;
283         r.in.level = 1;
284         r.in.count = &count;
285         r.in.unknown1 = 0;
286         r.in.unknown2 = 0;
287         r.out.count = &count;
288         r.out.sids = &sids;
289
290         status = dcerpc_lsa_LookupNames4(p, mem_ctx, &r);
291         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
292                 printf("LookupNames4 failed - %s\n", nt_errstr(status));
293                 return False;
294         }
295
296         printf("\n");
297
298         return True;
299 }
300
301
302 static BOOL test_LookupSids(struct dcerpc_pipe *p, 
303                             TALLOC_CTX *mem_ctx, 
304                             struct policy_handle *handle,
305                             struct lsa_SidArray *sids)
306 {
307         struct lsa_LookupSids r;
308         struct lsa_TransNameArray names;
309         uint32_t count = sids->num_sids;
310         NTSTATUS status;
311
312         printf("\nTesting LookupSids\n");
313
314         names.count = 0;
315         names.names = NULL;
316
317         r.in.handle = handle;
318         r.in.sids = sids;
319         r.in.names = &names;
320         r.in.level = 1;
321         r.in.count = &count;
322         r.out.count = &count;
323         r.out.names = &names;
324
325         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
326         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
327                 printf("LookupSids failed - %s\n", nt_errstr(status));
328                 return False;
329         }
330
331         printf("\n");
332
333         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
334                 return False;
335         }
336
337         return True;
338 }
339
340
341 static BOOL test_LookupSids2(struct dcerpc_pipe *p, 
342                             TALLOC_CTX *mem_ctx, 
343                             struct policy_handle *handle,
344                             struct lsa_SidArray *sids)
345 {
346         struct lsa_LookupSids2 r;
347         struct lsa_TransNameArray2 names;
348         uint32_t count = sids->num_sids;
349         NTSTATUS status;
350
351         printf("\nTesting LookupSids2\n");
352
353         names.count = 0;
354         names.names = NULL;
355
356         r.in.handle = handle;
357         r.in.sids = sids;
358         r.in.names = &names;
359         r.in.level = 1;
360         r.in.count = &count;
361         r.in.unknown1 = 0;
362         r.in.unknown2 = 0;
363         r.out.count = &count;
364         r.out.names = &names;
365
366         status = dcerpc_lsa_LookupSids2(p, mem_ctx, &r);
367         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
368                 printf("LookupSids2 failed - %s\n", nt_errstr(status));
369                 return False;
370         }
371
372         printf("\n");
373
374         if (!test_LookupNames2(p, mem_ctx, handle, &names)) {
375                 return False;
376         }
377
378         if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
379                 return False;
380         }
381
382         return True;
383 }
384
385 static BOOL test_LookupSids3(struct dcerpc_pipe *p, 
386                             TALLOC_CTX *mem_ctx, 
387                             struct lsa_SidArray *sids)
388 {
389         struct lsa_LookupSids3 r;
390         struct lsa_TransNameArray2 names;
391         uint32_t count = sids->num_sids;
392         NTSTATUS status;
393
394         printf("\nTesting LookupSids3\n");
395
396         names.count = 0;
397         names.names = NULL;
398
399         r.in.sids = sids;
400         r.in.names = &names;
401         r.in.level = 1;
402         r.in.count = &count;
403         r.in.unknown1 = 0;
404         r.in.unknown2 = 0;
405         r.out.count = &count;
406         r.out.names = &names;
407
408         status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
409         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
410                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
411                     NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
412                         printf("not considering %s to be an error\n", nt_errstr(status));
413                         return True;
414                 }
415                 printf("LookupSids3 failed - %s - not considered an error\n", 
416                        nt_errstr(status));
417                 return False;
418         }
419
420         printf("\n");
421
422         if (!test_LookupNames4(p, mem_ctx, &names)) {
423                 return False;
424         }
425
426         return True;
427 }
428
429 BOOL test_many_LookupSids(struct dcerpc_pipe *p, 
430                           TALLOC_CTX *mem_ctx, 
431                           struct policy_handle *handle)
432 {
433         uint32_t count;
434         NTSTATUS status;
435         struct lsa_SidArray sids;
436         int i;
437
438         printf("\nTesting LookupSids with lots of SIDs\n");
439
440         sids.num_sids = 100;
441
442         sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
443
444         for (i=0; i<sids.num_sids; i++) {
445                 const char *sidstr = "S-1-5-32-545";
446                 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
447         }
448
449         count = sids.num_sids;
450
451         if (handle) {
452                 struct lsa_LookupSids r;
453                 struct lsa_TransNameArray names;
454                 names.count = 0;
455                 names.names = NULL;
456
457                 r.in.handle = handle;
458                 r.in.sids = &sids;
459                 r.in.names = &names;
460                 r.in.level = 1;
461                 r.in.count = &names.count;
462                 r.out.count = &count;
463                 r.out.names = &names;
464                 
465                 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
466                 if (!NT_STATUS_IS_OK(status) &&
467                     !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
468                         printf("LookupSids failed - %s\n", nt_errstr(status));
469                         return False;
470                 }
471                 
472                 printf("\n");
473                 
474                 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
475                         return False;
476                 }
477         } else {
478                 struct lsa_LookupSids3 r;
479                 struct lsa_TransNameArray2 names;
480
481                 names.count = 0;
482                 names.names = NULL;
483
484                 printf("\nTesting LookupSids3\n");
485                 
486                 r.in.sids = &sids;
487                 r.in.names = &names;
488                 r.in.level = 1;
489                 r.in.count = &count;
490                 r.in.unknown1 = 0;
491                 r.in.unknown2 = 0;
492                 r.out.count = &count;
493                 r.out.names = &names;
494                 
495                 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
496                 if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
497                         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
498                             NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
499                                 printf("not considering %s to be an error\n", nt_errstr(status));
500                                 return True;
501                         }
502                         printf("LookupSids3 failed - %s\n", 
503                                nt_errstr(status));
504                         return False;
505                 }
506                 if (!test_LookupNames4(p, mem_ctx, &names)) {
507                         return False;
508                 }
509         }
510
511         printf("\n");
512
513
514
515         return True;
516 }
517
518 #define NUM_ASYNC_REQUESTS 1000
519
520 static void lookupsids_cb(struct rpc_request *req)
521 {
522         int *replies = (int *)req->async.private;
523         NTSTATUS status;
524
525         status = dcerpc_ndr_request_recv(req);
526         DEBUG(3, ("lookupsids returned %s\n", nt_errstr(status)));
527         if (!NT_STATUS_IS_OK(status)) {
528                 *replies = -1;
529         }
530
531         *replies += 1;
532 }
533
534 static BOOL test_LookupSids_async(struct dcerpc_pipe *p, 
535                                   TALLOC_CTX *mem_ctx, 
536                                   struct policy_handle *handle)
537 {
538         struct lsa_SidArray sids;
539         struct lsa_SidPtr sidptr;
540
541         uint32_t count[NUM_ASYNC_REQUESTS];
542         struct lsa_TransNameArray names[NUM_ASYNC_REQUESTS];
543         struct lsa_LookupSids r[NUM_ASYNC_REQUESTS];
544         struct rpc_request **req;
545
546         int i, replies;
547         BOOL ret = True;
548
549         printf("\nTesting %d async lookupsids request\n", 100);
550
551         req = talloc_array(mem_ctx, struct rpc_request *, NUM_ASYNC_REQUESTS);
552
553         sids.num_sids = 1;
554         sids.sids = &sidptr;
555         sidptr.sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-545");
556
557         replies = 0;
558
559         for (i=0; i<NUM_ASYNC_REQUESTS; i++) {
560                 count[i] = 0;
561                 names[i].count = 0;
562                 names[i].names = NULL;
563
564                 r[i].in.handle = handle;
565                 r[i].in.sids = &sids;
566                 r[i].in.names = &names[i];
567                 r[i].in.level = 1;
568                 r[i].in.count = &names[i].count;
569                 r[i].out.count = &count[i];
570                 r[i].out.names = &names[i];
571                 
572                 req[i] = dcerpc_lsa_LookupSids_send(p, req, &r[i]);
573                 if (req[i] == NULL) {
574                         ret = False;
575                         break;
576                 }
577
578                 req[i]->async.callback = lookupsids_cb;
579                 req[i]->async.private = &replies;
580         }
581
582         while (replies < NUM_ASYNC_REQUESTS) {
583                 event_loop_once(p->conn->event_ctx);
584                 if (replies < 0) {
585                         ret = False;
586                         break;
587                 }
588         }
589
590         talloc_free(req);
591
592         return ret;
593 }
594
595 static BOOL test_LookupPrivValue(struct dcerpc_pipe *p, 
596                                  TALLOC_CTX *mem_ctx, 
597                                  struct policy_handle *handle,
598                                  struct lsa_String *name)
599 {
600         NTSTATUS status;
601         struct lsa_LookupPrivValue r;
602         struct lsa_LUID luid;
603
604         r.in.handle = handle;
605         r.in.name = name;
606         r.out.luid = &luid;
607
608         status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
609         if (!NT_STATUS_IS_OK(status)) {
610                 printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
611                 return False;
612         }
613
614         return True;
615 }
616
617 static BOOL test_LookupPrivName(struct dcerpc_pipe *p, 
618                                 TALLOC_CTX *mem_ctx, 
619                                 struct policy_handle *handle,
620                                 struct lsa_LUID *luid)
621 {
622         NTSTATUS status;
623         struct lsa_LookupPrivName r;
624
625         r.in.handle = handle;
626         r.in.luid = luid;
627
628         status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
629         if (!NT_STATUS_IS_OK(status)) {
630                 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
631                 return False;
632         }
633
634         return True;
635 }
636
637 static BOOL test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p, 
638                                              TALLOC_CTX *mem_ctx,                                 
639                                              struct policy_handle *acct_handle,
640                                              struct lsa_LUID *luid)
641 {
642         NTSTATUS status;
643         struct lsa_RemovePrivilegesFromAccount r;
644         struct lsa_PrivilegeSet privs;
645         BOOL ret = True;
646
647         printf("Testing RemovePrivilegesFromAccount\n");
648
649         r.in.handle = acct_handle;
650         r.in.remove_all = 0;
651         r.in.privs = &privs;
652
653         privs.count = 1;
654         privs.unknown = 0;
655         privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
656         privs.set[0].luid = *luid;
657         privs.set[0].attribute = 0;
658
659         status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
660         if (!NT_STATUS_IS_OK(status)) {
661                 printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
662                 return False;
663         }
664
665         return ret;
666 }
667
668 static BOOL test_AddPrivilegesToAccount(struct dcerpc_pipe *p, 
669                                         TALLOC_CTX *mem_ctx,                              
670                                         struct policy_handle *acct_handle,
671                                         struct lsa_LUID *luid)
672 {
673         NTSTATUS status;
674         struct lsa_AddPrivilegesToAccount r;
675         struct lsa_PrivilegeSet privs;
676         BOOL ret = True;
677
678         printf("Testing AddPrivilegesToAccount\n");
679
680         r.in.handle = acct_handle;
681         r.in.privs = &privs;
682
683         privs.count = 1;
684         privs.unknown = 0;
685         privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
686         privs.set[0].luid = *luid;
687         privs.set[0].attribute = 0;
688
689         status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
690         if (!NT_STATUS_IS_OK(status)) {
691                 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
692                 return False;
693         }
694
695         return ret;
696 }
697
698 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p, 
699                                   TALLOC_CTX *mem_ctx,                            
700                                   struct policy_handle *handle,
701                                   struct policy_handle *acct_handle)
702 {
703         NTSTATUS status;
704         struct lsa_EnumPrivsAccount r;
705         BOOL ret = True;
706
707         printf("Testing EnumPrivsAccount\n");
708
709         r.in.handle = acct_handle;
710
711         status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
712         if (!NT_STATUS_IS_OK(status)) {
713                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
714                 return False;
715         }
716
717         if (r.out.privs && r.out.privs->count > 0) {
718                 int i;
719                 for (i=0;i<r.out.privs->count;i++) {
720                         test_LookupPrivName(p, mem_ctx, handle, 
721                                             &r.out.privs->set[i].luid);
722                 }
723
724                 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle, 
725                                                         &r.out.privs->set[0].luid);
726                 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle, 
727                                                    &r.out.privs->set[0].luid);
728         }
729
730         return ret;
731 }
732
733 static BOOL test_Delete(struct dcerpc_pipe *p, 
734                        TALLOC_CTX *mem_ctx, 
735                        struct policy_handle *handle)
736 {
737         NTSTATUS status;
738         struct lsa_Delete r;
739
740         printf("testing Delete\n");
741
742         r.in.handle = handle;
743         status = dcerpc_lsa_Delete(p, mem_ctx, &r);
744         if (!NT_STATUS_IS_OK(status)) {
745                 printf("Delete failed - %s\n", nt_errstr(status));
746                 return False;
747         }
748
749         return True;
750 }
751
752
753 static BOOL test_CreateAccount(struct dcerpc_pipe *p, 
754                                TALLOC_CTX *mem_ctx, 
755                                struct policy_handle *handle)
756 {
757         NTSTATUS status;
758         struct lsa_CreateAccount r;
759         struct dom_sid2 *newsid;
760         struct policy_handle acct_handle;
761
762         newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
763
764         printf("Testing CreateAccount\n");
765
766         r.in.handle = handle;
767         r.in.sid = newsid;
768         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
769         r.out.acct_handle = &acct_handle;
770
771         status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
772         if (!NT_STATUS_IS_OK(status)) {
773                 printf("CreateAccount failed - %s\n", nt_errstr(status));
774                 return False;
775         }
776
777         if (!test_Delete(p, mem_ctx, &acct_handle)) {
778                 return False;
779         }
780
781         return True;
782 }
783
784 static BOOL test_DeleteTrustedDomain(struct dcerpc_pipe *p, 
785                                      TALLOC_CTX *mem_ctx, 
786                                      struct policy_handle *handle,
787                                      struct lsa_StringLarge name)
788 {
789         NTSTATUS status;
790         struct lsa_OpenTrustedDomainByName r;
791         struct policy_handle trustdom_handle;
792
793         r.in.handle = handle;
794         r.in.name.string = name.string;
795         r.in.access_mask = SEC_STD_DELETE;
796         r.out.trustdom_handle = &trustdom_handle;
797
798         status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
799         if (!NT_STATUS_IS_OK(status)) {
800                 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
801                 return False;
802         }
803
804         if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
805                 return False;
806         }
807
808         return True;
809 }
810
811
812 static BOOL test_CreateSecret(struct dcerpc_pipe *p, 
813                               TALLOC_CTX *mem_ctx, 
814                               struct policy_handle *handle)
815 {
816         NTSTATUS status;
817         struct lsa_CreateSecret r;
818         struct lsa_OpenSecret r2;
819         struct lsa_SetSecret r3;
820         struct lsa_QuerySecret r4;
821         struct lsa_SetSecret r5;
822         struct lsa_QuerySecret r6;
823         struct lsa_SetSecret r7;
824         struct lsa_QuerySecret r8;
825         struct policy_handle sec_handle, sec_handle2, sec_handle3;
826         struct lsa_Delete d;
827         struct lsa_DATA_BUF buf1;
828         struct lsa_DATA_BUF_PTR bufp1;
829         struct lsa_DATA_BUF_PTR bufp2;
830         DATA_BLOB enc_key;
831         BOOL ret = True;
832         DATA_BLOB session_key;
833         NTTIME old_mtime, new_mtime;
834         DATA_BLOB blob1, blob2;
835         const char *secret1 = "abcdef12345699qwerty";
836         char *secret2;
837         const char *secret3 = "ABCDEF12345699QWERTY";
838         char *secret4;
839         const char *secret5 = "NEW-SAMBA4-SECRET";
840         char *secret6;
841         char *secname[2];
842         int i;
843         const int LOCAL = 0;
844         const int GLOBAL = 1;
845
846         secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
847         secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
848
849         for (i=0; i< 2; i++) {
850                 printf("Testing CreateSecret of %s\n", secname[i]);
851                 
852                 init_lsa_String(&r.in.name, secname[i]);
853                 
854                 r.in.handle = handle;
855                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
856                 r.out.sec_handle = &sec_handle;
857                 
858                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
859                 if (!NT_STATUS_IS_OK(status)) {
860                         printf("CreateSecret failed - %s\n", nt_errstr(status));
861                         return False;
862                 }
863                 
864                 r.in.handle = handle;
865                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
866                 r.out.sec_handle = &sec_handle3;
867                 
868                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
869                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
870                         printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
871                         return False;
872                 }
873                 
874                 r2.in.handle = handle;
875                 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
876                 r2.in.name = r.in.name;
877                 r2.out.sec_handle = &sec_handle2;
878                 
879                 printf("Testing OpenSecret\n");
880                 
881                 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
882                 if (!NT_STATUS_IS_OK(status)) {
883                         printf("OpenSecret failed - %s\n", nt_errstr(status));
884                         return False;
885                 }
886                 
887                 status = dcerpc_fetch_session_key(p, &session_key);
888                 if (!NT_STATUS_IS_OK(status)) {
889                         printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
890                         return False;
891                 }
892                 
893                 enc_key = sess_encrypt_string(secret1, &session_key);
894                 
895                 r3.in.sec_handle = &sec_handle;
896                 r3.in.new_val = &buf1;
897                 r3.in.old_val = NULL;
898                 r3.in.new_val->data = enc_key.data;
899                 r3.in.new_val->length = enc_key.length;
900                 r3.in.new_val->size = enc_key.length;
901                 
902                 printf("Testing SetSecret\n");
903                 
904                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
905                 if (!NT_STATUS_IS_OK(status)) {
906                         printf("SetSecret failed - %s\n", nt_errstr(status));
907                         return False;
908                 }
909                 
910                 r3.in.sec_handle = &sec_handle;
911                 r3.in.new_val = &buf1;
912                 r3.in.old_val = NULL;
913                 r3.in.new_val->data = enc_key.data;
914                 r3.in.new_val->length = enc_key.length;
915                 r3.in.new_val->size = enc_key.length;
916                 
917                 /* break the encrypted data */
918                 enc_key.data[0]++;
919
920                 printf("Testing SetSecret with broken key\n");
921                 
922                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
923                 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
924                         printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
925                         ret = False;
926                 }
927                 
928                 data_blob_free(&enc_key);
929                 
930                 ZERO_STRUCT(new_mtime);
931                 ZERO_STRUCT(old_mtime);
932                 
933                 /* fetch the secret back again */
934                 r4.in.sec_handle = &sec_handle;
935                 r4.in.new_val = &bufp1;
936                 r4.in.new_mtime = &new_mtime;
937                 r4.in.old_val = NULL;
938                 r4.in.old_mtime = NULL;
939                 
940                 bufp1.buf = NULL;
941                 
942                 printf("Testing QuerySecret\n");
943                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
944                 if (!NT_STATUS_IS_OK(status)) {
945                         printf("QuerySecret failed - %s\n", nt_errstr(status));
946                         ret = False;
947                 } else {
948                         if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
949                                 printf("No secret buffer returned\n");
950                                 ret = False;
951                         } else {
952                                 blob1.data = r4.out.new_val->buf->data;
953                                 blob1.length = r4.out.new_val->buf->size;
954                                 
955                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
956                                 
957                                 secret2 = sess_decrypt_string(&blob1, &session_key);
958                                 
959                                 if (strcmp(secret1, secret2) != 0) {
960                                         printf("Returned secret '%s' doesn't match '%s'\n", 
961                                                secret2, secret1);
962                                         ret = False;
963                                 }
964                         }
965                 }
966                 
967                 enc_key = sess_encrypt_string(secret3, &session_key);
968                 
969                 r5.in.sec_handle = &sec_handle;
970                 r5.in.new_val = &buf1;
971                 r5.in.old_val = NULL;
972                 r5.in.new_val->data = enc_key.data;
973                 r5.in.new_val->length = enc_key.length;
974                 r5.in.new_val->size = enc_key.length;
975                 
976                 printf("Testing SetSecret (existing value should move to old)\n");
977                 
978                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
979                 if (!NT_STATUS_IS_OK(status)) {
980                         printf("SetSecret failed - %s\n", nt_errstr(status));
981                         ret = False;
982                 }
983                 
984                 data_blob_free(&enc_key);
985                 
986                 ZERO_STRUCT(new_mtime);
987                 ZERO_STRUCT(old_mtime);
988                 
989                 /* fetch the secret back again */
990                 r6.in.sec_handle = &sec_handle;
991                 r6.in.new_val = &bufp1;
992                 r6.in.new_mtime = &new_mtime;
993                 r6.in.old_val = &bufp2;
994                 r6.in.old_mtime = &old_mtime;
995                 
996                 bufp1.buf = NULL;
997                 bufp2.buf = NULL;
998                 
999                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
1000                 if (!NT_STATUS_IS_OK(status)) {
1001                         printf("QuerySecret failed - %s\n", nt_errstr(status));
1002                         ret = False;
1003                         secret4 = NULL;
1004                 } else {
1005
1006                         if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL 
1007                                 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1008                                 printf("Both secret buffers and both times not returned\n");
1009                                 ret = False;
1010                                 secret4 = NULL;
1011                         } else {
1012                                 blob1.data = r6.out.new_val->buf->data;
1013                                 blob1.length = r6.out.new_val->buf->size;
1014                                 
1015                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1016                                 
1017                                 secret4 = sess_decrypt_string(&blob1, &session_key);
1018                                 
1019                                 if (strcmp(secret3, secret4) != 0) {
1020                                         printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1021                                         ret = False;
1022                                 }
1023
1024                                 blob1.data = r6.out.old_val->buf->data;
1025                                 blob1.length = r6.out.old_val->buf->length;
1026                                 
1027                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1028                                 
1029                                 secret2 = sess_decrypt_string(&blob1, &session_key);
1030                                 
1031                                 if (strcmp(secret1, secret2) != 0) {
1032                                         printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1033                                         ret = False;
1034                                 }
1035                                 
1036                                 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1037                                         printf("Returned secret %s had same mtime for both secrets: %s\n", 
1038                                                secname[i],
1039                                                nt_time_string(mem_ctx, *r6.out.new_mtime));
1040                                         ret = False;
1041                                 }
1042                         }
1043                 }
1044
1045                 enc_key = sess_encrypt_string(secret5, &session_key);
1046                 
1047                 r7.in.sec_handle = &sec_handle;
1048                 r7.in.old_val = &buf1;
1049                 r7.in.old_val->data = enc_key.data;
1050                 r7.in.old_val->length = enc_key.length;
1051                 r7.in.old_val->size = enc_key.length;
1052                 r7.in.new_val = NULL;
1053                 
1054                 printf("Testing SetSecret of old Secret only\n");
1055                 
1056                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
1057                 if (!NT_STATUS_IS_OK(status)) {
1058                         printf("SetSecret failed - %s\n", nt_errstr(status));
1059                         ret = False;
1060                 }
1061                 
1062                 data_blob_free(&enc_key);
1063                 
1064                 /* fetch the secret back again */
1065                 r8.in.sec_handle = &sec_handle;
1066                 r8.in.new_val = &bufp1;
1067                 r8.in.new_mtime = &new_mtime;
1068                 r8.in.old_val = &bufp2;
1069                 r8.in.old_mtime = &old_mtime;
1070                 
1071                 bufp1.buf = NULL;
1072                 bufp2.buf = NULL;
1073                 
1074                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
1075                 if (!NT_STATUS_IS_OK(status)) {
1076                         printf("QuerySecret failed - %s\n", nt_errstr(status));
1077                         ret = False;
1078                 } else {
1079                         if (!r8.out.new_val || !r8.out.old_val) {
1080                                 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
1081                                 ret = False;
1082                         } else if (r8.out.new_val->buf == NULL) {
1083                                 if (i != LOCAL) { 
1084                                         printf("NEW secret buffer not returned after GLOBAL OLD set\n");
1085                                         ret = False;
1086                                 }
1087                         } else if (r8.out.old_val->buf == NULL) {
1088                                 printf("OLD secret buffer not returned after OLD set\n");
1089                                 ret = False;
1090                         } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1091                                 printf("Both times not returned after OLD set\n");
1092                                 ret = False;
1093                         } else {
1094                                 if (i == LOCAL) { 
1095                                         printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
1096                                         ret = False;
1097                                 }
1098                                 blob1.data = r8.out.new_val->buf->data;
1099                                 blob1.length = r8.out.new_val->buf->length;
1100                                 
1101                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1102                                 
1103                                 secret6 = sess_decrypt_string(&blob1, &session_key);
1104                                 
1105                                 if (strcmp(secret3, secret4) != 0) {
1106                                         printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
1107                                         ret = False;
1108                                 }
1109
1110                                 blob1.data = r8.out.old_val->buf->data;
1111                                 blob1.length = r8.out.old_val->buf->size;
1112                                 
1113                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1114                                 
1115                                 secret6 = sess_decrypt_string(&blob1, &session_key);
1116                                 
1117                                 if (strcmp(secret5, secret6) != 0) {
1118                                         printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1119                                         ret = False;
1120                                 }
1121                                 
1122                                 if (*r8.out.new_mtime == *r8.out.old_mtime) {
1123                                         if (i != GLOBAL) { 
1124                                                 printf("Returned secret %s had same mtime for both secrets: %s\n", 
1125                                                        secname[i],
1126                                                        nt_time_string(mem_ctx, *r8.out.new_mtime));
1127                                                 ret = False;
1128                                         }
1129                                 } else {
1130                                         printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n", 
1131                                                secname[i],
1132                                                nt_time_string(mem_ctx, *r8.out.old_mtime),
1133                                                nt_time_string(mem_ctx, *r8.out.new_mtime));
1134                                         ret = False;
1135                                 }
1136                         }
1137                 }
1138
1139                 if (!test_Delete(p, mem_ctx, &sec_handle)) {
1140                         ret = False;
1141                 }
1142                 
1143                 d.in.handle = &sec_handle2;
1144                 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
1145                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1146                         printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1147                         ret = False;
1148                 } else {
1149
1150                         printf("Testing OpenSecret of just-deleted secret\n");
1151                         
1152                         status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1153                         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1154                                 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1155                                 ret = False;
1156                         }
1157                 }
1158                 
1159         }
1160
1161         return ret;
1162 }
1163
1164
1165 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p, 
1166                                    TALLOC_CTX *mem_ctx, 
1167                                    struct policy_handle *acct_handle,
1168                                    struct dom_sid *sid)
1169 {
1170         NTSTATUS status;
1171         struct lsa_EnumAccountRights r;
1172         struct lsa_RightSet rights;
1173
1174         printf("Testing EnumAccountRights\n");
1175
1176         r.in.handle = acct_handle;
1177         r.in.sid = sid;
1178         r.out.rights = &rights;
1179
1180         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1181         if (!NT_STATUS_IS_OK(status)) {
1182                 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
1183                 return False;
1184         }
1185
1186         return True;
1187 }
1188
1189
1190 static BOOL test_QuerySecurity(struct dcerpc_pipe *p, 
1191                              TALLOC_CTX *mem_ctx, 
1192                              struct policy_handle *handle,
1193                              struct policy_handle *acct_handle)
1194 {
1195         NTSTATUS status;
1196         struct lsa_QuerySecurity r;
1197
1198         printf("Testing QuerySecurity\n");
1199
1200         r.in.handle = acct_handle;
1201         r.in.sec_info = 7;
1202
1203         status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
1204         if (!NT_STATUS_IS_OK(status)) {
1205                 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1206                 return False;
1207         }
1208
1209         return True;
1210 }
1211
1212 static BOOL test_OpenAccount(struct dcerpc_pipe *p, 
1213                              TALLOC_CTX *mem_ctx, 
1214                              struct policy_handle *handle,
1215                              struct dom_sid *sid)
1216 {
1217         NTSTATUS status;
1218         struct lsa_OpenAccount r;
1219         struct policy_handle acct_handle;
1220
1221         printf("Testing OpenAccount\n");
1222
1223         r.in.handle = handle;
1224         r.in.sid = sid;
1225         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1226         r.out.acct_handle = &acct_handle;
1227
1228         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1229         if (!NT_STATUS_IS_OK(status)) {
1230                 printf("OpenAccount failed - %s\n", nt_errstr(status));
1231                 return False;
1232         }
1233
1234         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1235                 return False;
1236         }
1237
1238         if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1239                 return False;
1240         }
1241
1242         return True;
1243 }
1244
1245 static BOOL test_EnumAccounts(struct dcerpc_pipe *p, 
1246                           TALLOC_CTX *mem_ctx, 
1247                           struct policy_handle *handle)
1248 {
1249         NTSTATUS status;
1250         struct lsa_EnumAccounts r;
1251         struct lsa_SidArray sids1, sids2;
1252         uint32_t resume_handle = 0;
1253         int i;
1254
1255         printf("\ntesting EnumAccounts\n");
1256
1257         r.in.handle = handle;
1258         r.in.resume_handle = &resume_handle;
1259         r.in.num_entries = 100;
1260         r.out.resume_handle = &resume_handle;
1261         r.out.sids = &sids1;
1262
1263         resume_handle = 0;
1264         while (True) {
1265                 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1266                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1267                         break;
1268                 }
1269                 if (!NT_STATUS_IS_OK(status)) {
1270                         printf("EnumAccounts failed - %s\n", nt_errstr(status));
1271                         return False;
1272                 }
1273
1274                 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1275                         return False;
1276                 }
1277
1278                 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1279                         return False;
1280                 }
1281
1282                 if (!test_LookupSids3(p, mem_ctx, &sids1)) {
1283                         return False;
1284                 }
1285
1286                 printf("testing all accounts\n");
1287                 for (i=0;i<sids1.num_sids;i++) {
1288                         test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1289                         test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1290                 }
1291                 printf("\n");
1292         }
1293
1294         if (sids1.num_sids < 3) {
1295                 return True;
1296         }
1297         
1298         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1299         resume_handle = 2;
1300         r.in.num_entries = 1;
1301         r.out.sids = &sids2;
1302
1303         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1304         if (!NT_STATUS_IS_OK(status)) {
1305                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1306                 return False;
1307         }
1308
1309         if (sids2.num_sids != 1) {
1310                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1311                 return False;
1312         }
1313
1314         return True;
1315 }
1316
1317 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1318                                 TALLOC_CTX *mem_ctx,
1319                                 struct policy_handle *handle,
1320                                 struct lsa_String *priv_name)
1321 {
1322         struct lsa_LookupPrivDisplayName r;
1323         NTSTATUS status;
1324         /* produce a reasonable range of language output without screwing up
1325            terminals */
1326         uint16_t language_id = (random() % 4) + 0x409;
1327
1328         printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1329         
1330         r.in.handle = handle;
1331         r.in.name = priv_name;
1332         r.in.language_id = &language_id;
1333         r.out.language_id = &language_id;
1334         r.in.unknown = 0;
1335
1336         status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1337         if (!NT_STATUS_IS_OK(status)) {
1338                 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1339                 return False;
1340         }
1341         printf("%s -> \"%s\"  (language 0x%x/0x%x)\n", 
1342                priv_name->string, r.out.disp_name->string, 
1343                *r.in.language_id, *r.out.language_id);
1344
1345         return True;
1346 }
1347
1348 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p, 
1349                                 TALLOC_CTX *mem_ctx,
1350                                 struct policy_handle *handle,
1351                                 struct lsa_String *priv_name)
1352 {
1353         struct lsa_EnumAccountsWithUserRight r;
1354         struct lsa_SidArray sids;
1355         NTSTATUS status;
1356
1357         ZERO_STRUCT(sids);
1358         
1359         printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1360         
1361         r.in.handle = handle;
1362         r.in.name = priv_name;
1363         r.out.sids = &sids;
1364
1365         status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1366
1367         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1368         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1369                 return True;
1370         }
1371
1372         if (!NT_STATUS_IS_OK(status)) {
1373                 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1374                 return False;
1375         }
1376         
1377         return True;
1378 }
1379
1380
1381 static BOOL test_EnumPrivs(struct dcerpc_pipe *p, 
1382                            TALLOC_CTX *mem_ctx, 
1383                            struct policy_handle *handle)
1384 {
1385         NTSTATUS status;
1386         struct lsa_EnumPrivs r;
1387         struct lsa_PrivArray privs1;
1388         uint32_t resume_handle = 0;
1389         int i;
1390         BOOL ret = True;
1391
1392         printf("\ntesting EnumPrivs\n");
1393
1394         r.in.handle = handle;
1395         r.in.resume_handle = &resume_handle;
1396         r.in.max_count = 100;
1397         r.out.resume_handle = &resume_handle;
1398         r.out.privs = &privs1;
1399
1400         resume_handle = 0;
1401         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1402         if (!NT_STATUS_IS_OK(status)) {
1403                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1404                 return False;
1405         }
1406
1407         for (i = 0; i< privs1.count; i++) {
1408                 test_LookupPrivDisplayName(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1409                 test_LookupPrivValue(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1410                 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
1411                         ret = False;
1412                 }
1413         }
1414
1415         return ret;
1416 }
1417
1418 static BOOL test_query_each_TrustDom(struct dcerpc_pipe *p, 
1419                                      TALLOC_CTX *mem_ctx, 
1420                                      struct policy_handle *handle, 
1421                                      struct lsa_DomainList *domains) 
1422 {
1423         NTSTATUS status;
1424         int i,j;
1425         BOOL ret = True;
1426                 
1427         printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1428         for (i=0; i< domains->count; i++) {
1429                 struct lsa_OpenTrustedDomain trust;
1430                 struct lsa_OpenTrustedDomainByName trust_by_name;
1431                 struct policy_handle trustdom_handle;
1432                 struct policy_handle handle2;
1433                 struct lsa_Close c;
1434                 int levels [] = {1, 3, 6, 8, 12};
1435                         
1436                 if (domains->domains[i].sid) {
1437                         trust.in.handle = handle;
1438                         trust.in.sid = domains->domains[i].sid;
1439                         trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1440                         trust.out.trustdom_handle = &trustdom_handle;
1441                         
1442                         status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1443                         
1444                         if (!NT_STATUS_IS_OK(status)) {
1445                                 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1446                                 return False;
1447                         }
1448                         
1449                         c.in.handle = &trustdom_handle;
1450                         c.out.handle = &handle2;
1451                         
1452                         for (j=0; j < ARRAY_SIZE(levels); j++) {
1453                                 struct lsa_QueryTrustedDomainInfo q;
1454                                 union lsa_TrustedDomainInfo info;
1455                                 q.in.trustdom_handle = &trustdom_handle;
1456                                 q.in.level = levels[j];
1457                                 q.out.info = &info;
1458                                 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1459                                 if (!NT_STATUS_IS_OK(status)) {
1460                                         printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1461                                                levels[j], nt_errstr(status));
1462                                         ret = False;
1463                                 }
1464                         }
1465                         
1466                         status = dcerpc_lsa_Close(p, mem_ctx, &c);
1467                         if (!NT_STATUS_IS_OK(status)) {
1468                                 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1469                                 return False;
1470                         }
1471                 }
1472
1473                 trust_by_name.in.handle = handle;
1474                 trust_by_name.in.name.string = domains->domains[i].name.string;
1475                 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1476                 trust_by_name.out.trustdom_handle = &trustdom_handle;
1477                         
1478                 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1479                         
1480                 if (!NT_STATUS_IS_OK(status)) {
1481                         printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1482                         return False;
1483                 }
1484
1485                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1486                         struct lsa_QueryTrustedDomainInfo q;
1487                         union lsa_TrustedDomainInfo info;
1488                         q.in.trustdom_handle = &trustdom_handle;
1489                         q.in.level = levels[j];
1490                         q.out.info = &info;
1491                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1492                         if (!NT_STATUS_IS_OK(status)) {
1493                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1494                                        levels[j], nt_errstr(status));
1495                                 ret = False;
1496                         }
1497                 }
1498                 
1499                 c.in.handle = &trustdom_handle;
1500                 c.out.handle = &handle2;
1501
1502                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1503                 if (!NT_STATUS_IS_OK(status)) {
1504                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1505                         return False;
1506                 }
1507
1508                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1509                         struct lsa_QueryTrustedDomainInfoBySid q;
1510                         union lsa_TrustedDomainInfo info;
1511
1512                         if (!domains->domains[i].sid) {
1513                                 continue;
1514                         }
1515
1516                         q.in.handle  = handle;
1517                         q.in.dom_sid = domains->domains[i].sid;
1518                         q.in.level   = levels[j];
1519                         q.out.info   = &info;
1520                         status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1521                         if (!NT_STATUS_IS_OK(status)) {
1522                                 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n", 
1523                                        levels[j], nt_errstr(status));
1524                                 ret = False;
1525                         }
1526                 }
1527                 
1528                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1529                         struct lsa_QueryTrustedDomainInfoByName q;
1530                         union lsa_TrustedDomainInfo info;
1531                         q.in.handle         = handle;
1532                         q.in.trusted_domain.string = domains->domains[i].name.string;
1533                         q.in.level          = levels[j];
1534                         q.out.info          = &info;
1535                         status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1536                         if (!NT_STATUS_IS_OK(status)) {
1537                                 printf("QueryTrustedDomainInfoByName level %d failed - %s\n", 
1538                                        levels[j], nt_errstr(status));
1539                                 ret = False;
1540                         }
1541                 }
1542         }
1543         return ret;
1544 }
1545
1546 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p, 
1547                               TALLOC_CTX *mem_ctx, 
1548                               struct policy_handle *handle)
1549 {
1550         struct lsa_EnumTrustDom r;
1551         NTSTATUS enum_status;
1552         uint32_t resume_handle = 0;
1553         struct lsa_DomainList domains;
1554         BOOL ret = True;
1555
1556         printf("\nTesting EnumTrustDom\n");
1557
1558         do {
1559                 r.in.handle = handle;
1560                 r.in.resume_handle = &resume_handle;
1561                 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
1562                 r.out.domains = &domains;
1563                 r.out.resume_handle = &resume_handle;
1564                 
1565                 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1566                 
1567                 /* NO_MORE_ENTRIES is allowed */
1568                 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1569                         return True;
1570                 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1571                         /* Windows 2003 gets this off by one on the first run */
1572                         if (r.out.domains->count < 3 || r.out.domains->count > 4) {
1573                                 printf("EnumTrustDom didn't fill the buffer we "
1574                                        "asked it to (got %d, expected %d / %d == %d entries)\n",
1575                                        r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3, 
1576                                        LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
1577                                 ret = False;
1578                         }
1579                 } else if (!NT_STATUS_IS_OK(enum_status)) {
1580                         printf("EnumTrustDom failed - %s\n", nt_errstr(enum_status));
1581                         return False;
1582                 }
1583                 
1584                 ret &= test_query_each_TrustDom(p, mem_ctx, handle, &domains);
1585
1586         } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1587
1588         return ret;
1589 }
1590
1591 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p, 
1592                                      TALLOC_CTX *mem_ctx, 
1593                                      struct policy_handle *handle)
1594 {
1595         NTSTATUS status;
1596         BOOL ret = True;
1597         struct lsa_CreateTrustedDomain r;
1598         struct lsa_TrustInformation trustinfo;
1599         struct dom_sid *domsid[12];
1600         struct policy_handle trustdom_handle[12];
1601         struct lsa_QueryTrustedDomainInfo q;
1602         int i;
1603
1604         printf("Testing CreateTrustedDomain for 12 domains\n");
1605
1606         for (i=0; i< 12; i++) {
1607                 char *trust_name = talloc_asprintf(mem_ctx, "torturedom%02d", i);
1608                 char *trust_sid = talloc_asprintf(mem_ctx, "S-1-5-21-97398-379795-100%02d", i);
1609                 
1610                 domsid[i] = dom_sid_parse_talloc(mem_ctx, trust_sid);
1611
1612                 trustinfo.sid = domsid[i];
1613                 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
1614
1615                 r.in.handle = handle;
1616                 r.in.info = &trustinfo;
1617                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1618                 r.out.trustdom_handle = &trustdom_handle[i];
1619                 
1620                 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1621                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1622                         test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1623                         status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1624                 }
1625                 if (!NT_STATUS_IS_OK(status)) {
1626                         printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1627                         ret = False;
1628                 } else {
1629                 
1630                         q.in.trustdom_handle = &trustdom_handle[i];
1631                         q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
1632                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1633                         if (!NT_STATUS_IS_OK(status)) {
1634                                 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
1635                                 ret = False;
1636                         } else if (!q.out.info) {
1637                                 ret = False;
1638                         } else {
1639                                 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
1640                                         printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
1641                                                q.out.info->name.netbios_name.string, trustinfo.name.string);
1642                                         ret = False;
1643                                 }
1644                         }
1645                 }
1646         }
1647
1648         /* now that we have some domains to look over, we can test the enum calls */
1649         if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1650                 ret = False;
1651         }
1652         
1653         for (i=0; i<12; i++) {
1654                 if (!test_Delete(p, mem_ctx, &trustdom_handle[i])) {
1655                         ret = False;
1656                 }
1657         }
1658
1659         return ret;
1660 }
1661
1662 static BOOL test_QueryDomainInfoPolicy(struct dcerpc_pipe *p, 
1663                                  TALLOC_CTX *mem_ctx, 
1664                                  struct policy_handle *handle)
1665 {
1666         struct lsa_QueryDomainInformationPolicy r;
1667         NTSTATUS status;
1668         int i;
1669         BOOL ret = True;
1670         printf("\nTesting QueryDomainInformationPolicy\n");
1671
1672         for (i=2;i<4;i++) {
1673                 r.in.handle = handle;
1674                 r.in.level = i;
1675
1676                 printf("\ntrying QueryDomainInformationPolicy level %d\n", i);
1677
1678                 status = dcerpc_lsa_QueryDomainInformationPolicy(p, mem_ctx, &r);
1679
1680                 if (!NT_STATUS_IS_OK(status)) {
1681                         printf("QueryDomainInformationPolicy failed - %s\n", nt_errstr(status));
1682                         ret = False;
1683                         continue;
1684                 }
1685         }
1686
1687         return ret;
1688 }
1689
1690
1691 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p, 
1692                                  TALLOC_CTX *mem_ctx, 
1693                                  struct policy_handle *handle)
1694 {
1695         struct lsa_QueryInfoPolicy r;
1696         NTSTATUS status;
1697         int i;
1698         BOOL ret = True;
1699         printf("\nTesting QueryInfoPolicy\n");
1700
1701         for (i=1;i<13;i++) {
1702                 r.in.handle = handle;
1703                 r.in.level = i;
1704
1705                 printf("\ntrying QueryInfoPolicy level %d\n", i);
1706
1707                 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
1708
1709                 if ((i == 9 || i == 10 || i == 11) &&
1710                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1711                         printf("server failed level %u (OK)\n", i);
1712                         continue;
1713                 }
1714
1715                 if (!NT_STATUS_IS_OK(status)) {
1716                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1717                         ret = False;
1718                         continue;
1719                 }
1720         }
1721
1722         return ret;
1723 }
1724
1725 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p, 
1726                                   TALLOC_CTX *mem_ctx, 
1727                                   struct policy_handle *handle)
1728 {
1729         struct lsa_QueryInfoPolicy2 r;
1730         NTSTATUS status;
1731         int i;
1732         BOOL ret = True;
1733         printf("\nTesting QueryInfoPolicy2\n");
1734
1735         for (i=1;i<13;i++) {
1736                 r.in.handle = handle;
1737                 r.in.level = i;
1738
1739                 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
1740
1741                 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
1742
1743                 if ((i == 9 || i == 10 || i == 11) &&
1744                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1745                         printf("server failed level %u (OK)\n", i);
1746                         continue;
1747                 }
1748
1749                 if (!NT_STATUS_IS_OK(status)) {
1750                         printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
1751                         ret = False;
1752                         continue;
1753                 }
1754         }
1755
1756         return ret;
1757 }
1758
1759 static BOOL test_GetUserName(struct dcerpc_pipe *p, 
1760                                   TALLOC_CTX *mem_ctx, 
1761                                   struct policy_handle *handle)
1762 {
1763         struct lsa_GetUserName r;
1764         NTSTATUS status;
1765         BOOL ret = True;
1766         struct lsa_StringPointer authority_name_p;
1767
1768         printf("\nTesting GetUserName\n");
1769
1770         r.in.system_name = "\\";        
1771         r.in.account_name = NULL;       
1772         r.in.authority_name = &authority_name_p;
1773         authority_name_p.string = NULL;
1774
1775         status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
1776
1777         if (!NT_STATUS_IS_OK(status)) {
1778                 printf("GetUserName failed - %s\n", nt_errstr(status));
1779                 ret = False;
1780         }
1781
1782         return ret;
1783 }
1784
1785 BOOL test_lsa_Close(struct dcerpc_pipe *p, 
1786                     TALLOC_CTX *mem_ctx, 
1787                     struct policy_handle *handle)
1788 {
1789         NTSTATUS status;
1790         struct lsa_Close r;
1791         struct policy_handle handle2;
1792
1793         printf("\ntesting Close\n");
1794
1795         r.in.handle = handle;
1796         r.out.handle = &handle2;
1797
1798         status = dcerpc_lsa_Close(p, mem_ctx, &r);
1799         if (!NT_STATUS_IS_OK(status)) {
1800                 printf("Close failed - %s\n", nt_errstr(status));
1801                 return False;
1802         }
1803
1804         status = dcerpc_lsa_Close(p, mem_ctx, &r);
1805         /* its really a fault - we need a status code for rpc fault */
1806         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
1807                 printf("Close failed - %s\n", nt_errstr(status));
1808                 return False;
1809         }
1810
1811         printf("\n");
1812
1813         return True;
1814 }
1815
1816 BOOL torture_rpc_lsa(void)
1817 {
1818         NTSTATUS status;
1819         struct dcerpc_pipe *p;
1820         TALLOC_CTX *mem_ctx;
1821         BOOL ret = True;
1822         struct policy_handle *handle;
1823
1824         mem_ctx = talloc_init("torture_rpc_lsa");
1825
1826         status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_lsarpc);
1827         if (!NT_STATUS_IS_OK(status)) {
1828                 talloc_free(mem_ctx);
1829                 return False;
1830         }
1831
1832         if (!test_OpenPolicy(p, mem_ctx)) {
1833                 ret = False;
1834         }
1835
1836         if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
1837                 ret = False;
1838         }
1839
1840         if (handle) {
1841                 if (!test_LookupSids_async(p, mem_ctx, handle)) {
1842                         ret = False;
1843                 }
1844
1845                 if (!test_QueryDomainInfoPolicy(p, mem_ctx, handle)) {
1846                         ret = False;
1847                 }
1848                 
1849                 if (!test_CreateAccount(p, mem_ctx, handle)) {
1850                         ret = False;
1851                 }
1852                 
1853                 if (!test_CreateSecret(p, mem_ctx, handle)) {
1854                         ret = False;
1855                 }
1856                 
1857                 if (!test_CreateTrustedDomain(p, mem_ctx, handle)) {
1858                         ret = False;
1859                 }
1860                 
1861                 if (!test_EnumAccounts(p, mem_ctx, handle)) {
1862                         ret = False;
1863                 }
1864                 
1865                 if (!test_EnumPrivs(p, mem_ctx, handle)) {
1866                         ret = False;
1867                 }
1868                 
1869                 if (!test_QueryInfoPolicy(p, mem_ctx, handle)) {
1870                         ret = False;
1871                 }
1872                 
1873                 if (!test_QueryInfoPolicy2(p, mem_ctx, handle)) {
1874                         ret = False;
1875                 }
1876                 
1877                 if (!test_GetUserName(p, mem_ctx, handle)) {
1878                         ret = False;
1879                 }
1880                 
1881 #if 0
1882                 if (!test_Delete(p, mem_ctx, handle)) {
1883                         ret = False;
1884                 }
1885 #endif
1886                 
1887                 if (!test_many_LookupSids(p, mem_ctx, handle)) {
1888                         ret = False;
1889                 }
1890                 
1891                 if (!test_lsa_Close(p, mem_ctx, handle)) {
1892                         ret = False;
1893                 }
1894         } else {
1895                 if (!test_many_LookupSids(p, mem_ctx, handle)) {
1896                         ret = False;
1897                 }
1898         }
1899                 
1900         talloc_free(mem_ctx);
1901
1902         return ret;
1903 }