Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into manpage
[bbaumbach/samba-autobuild/.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    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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_lsa_c.h"
25 #include "librpc/gen_ndr/netlogon.h"
26 #include "lib/events/events.h"
27 #include "libcli/security/security.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "torture/rpc/rpc.h"
30 #include "param/param.h"
31
32 #define TEST_MACHINENAME "lsatestmach"
33
34 static void init_lsa_String(struct lsa_String *name, const char *s)
35 {
36         name->string = s;
37 }
38
39 static bool test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
40 {
41         struct lsa_ObjectAttribute attr;
42         struct policy_handle handle;
43         struct lsa_QosInfo qos;
44         struct lsa_OpenPolicy r;
45         NTSTATUS status;
46         uint16_t system_name = '\\';
47
48         printf("\ntesting OpenPolicy\n");
49
50         qos.len = 0;
51         qos.impersonation_level = 2;
52         qos.context_mode = 1;
53         qos.effective_only = 0;
54
55         attr.len = 0;
56         attr.root_dir = NULL;
57         attr.object_name = NULL;
58         attr.attributes = 0;
59         attr.sec_desc = NULL;
60         attr.sec_qos = &qos;
61
62         r.in.system_name = &system_name;
63         r.in.attr = &attr;
64         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65         r.out.handle = &handle;
66
67         status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
68         if (!NT_STATUS_IS_OK(status)) {
69                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
70                     NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
71                         printf("not considering %s to be an error\n", nt_errstr(status));
72                         return true;
73                 }
74                 printf("OpenPolicy failed - %s\n", nt_errstr(status));
75                 return false;
76         }
77
78         return true;
79 }
80
81
82 bool test_lsa_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
83                           struct policy_handle **handle)
84 {
85         struct lsa_ObjectAttribute attr;
86         struct lsa_QosInfo qos;
87         struct lsa_OpenPolicy2 r;
88         NTSTATUS status;
89
90         printf("\ntesting OpenPolicy2\n");
91
92         *handle = talloc(mem_ctx, struct policy_handle);
93         if (!*handle) {
94                 return false;
95         }
96
97         qos.len = 0;
98         qos.impersonation_level = 2;
99         qos.context_mode = 1;
100         qos.effective_only = 0;
101
102         attr.len = 0;
103         attr.root_dir = NULL;
104         attr.object_name = NULL;
105         attr.attributes = 0;
106         attr.sec_desc = NULL;
107         attr.sec_qos = &qos;
108
109         r.in.system_name = "\\";
110         r.in.attr = &attr;
111         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
112         r.out.handle = *handle;
113
114         status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
115         if (!NT_STATUS_IS_OK(status)) {
116                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
117                     NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
118                         printf("not considering %s to be an error\n", nt_errstr(status));
119                         talloc_free(*handle);
120                         *handle = NULL;
121                         return true;
122                 }
123                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
124                 return false;
125         }
126
127         return true;
128 }
129
130
131 static const char *sid_type_lookup(enum lsa_SidType r)
132 {
133         switch (r) {
134                 case SID_NAME_USE_NONE: return "SID_NAME_USE_NONE"; break;
135                 case SID_NAME_USER: return "SID_NAME_USER"; break;
136                 case SID_NAME_DOM_GRP: return "SID_NAME_DOM_GRP"; break;
137                 case SID_NAME_DOMAIN: return "SID_NAME_DOMAIN"; break;
138                 case SID_NAME_ALIAS: return "SID_NAME_ALIAS"; break;
139                 case SID_NAME_WKN_GRP: return "SID_NAME_WKN_GRP"; break;
140                 case SID_NAME_DELETED: return "SID_NAME_DELETED"; break;
141                 case SID_NAME_INVALID: return "SID_NAME_INVALID"; break;
142                 case SID_NAME_UNKNOWN: return "SID_NAME_UNKNOWN"; break;
143                 case SID_NAME_COMPUTER: return "SID_NAME_COMPUTER"; break;
144         }
145         return "Invalid sid type\n";
146 }
147
148 static bool test_LookupNames(struct dcerpc_pipe *p, 
149                              TALLOC_CTX *mem_ctx, 
150                              struct policy_handle *handle,
151                              struct lsa_TransNameArray *tnames)
152 {
153         struct lsa_LookupNames r;
154         struct lsa_TransSidArray sids;
155         struct lsa_String *names;
156         uint32_t count = 0;
157         NTSTATUS status;
158         int i;
159
160         printf("\nTesting LookupNames with %d names\n", tnames->count);
161
162         sids.count = 0;
163         sids.sids = NULL;
164
165         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
166         for (i=0;i<tnames->count;i++) {
167                 init_lsa_String(&names[i], tnames->names[i].name.string);
168         }
169
170         r.in.handle = handle;
171         r.in.num_names = tnames->count;
172         r.in.names = names;
173         r.in.sids = &sids;
174         r.in.level = 1;
175         r.in.count = &count;
176         r.out.count = &count;
177         r.out.sids = &sids;
178
179         status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
180
181         if (NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED) || 
182             NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
183                 for (i=0;i< tnames->count;i++) {
184                         if (i < count && sids.sids[i].sid_type == SID_NAME_UNKNOWN) {
185                                 printf("LookupName of %s was unmapped\n", 
186                                        tnames->names[i].name.string);   
187                         } else if (i >=count) {
188                                 printf("LookupName of %s failed to return a result\n",
189                                        tnames->names[i].name.string);
190                         }
191                 }
192                 printf("LookupNames failed - %s\n", nt_errstr(status));
193                 return false;
194         } else if (!NT_STATUS_IS_OK(status)) {
195                 printf("LookupNames failed - %s\n", nt_errstr(status));
196                 return false;
197         }
198         
199         for (i=0;i< tnames->count;i++) {
200                 if (i < count && sids.sids[i].sid_type != tnames->names[i].sid_type) {
201                         printf("LookupName of %s got unexpected name type: %s\n", 
202                                tnames->names[i].name.string, sid_type_lookup(sids.sids[i].sid_type));
203                 } else if (i >=count) {
204                         printf("LookupName of %s failed to return a result\n",
205                                tnames->names[i].name.string);
206                 }
207         }
208         printf("\n");
209
210         return true;
211 }
212
213 static bool test_LookupNames_bogus(struct dcerpc_pipe *p, 
214                             TALLOC_CTX *mem_ctx, 
215                             struct policy_handle *handle)
216 {
217         struct lsa_LookupNames r;
218         struct lsa_TransSidArray sids;
219         struct lsa_String *names;
220         uint32_t count = 0;
221         NTSTATUS status;
222         int i;
223
224         struct lsa_TranslatedName name;
225         struct lsa_TransNameArray tnames;
226
227         tnames.names = &name;
228         tnames.count = 1;
229         name.name.string = "NT AUTHORITY\\BOGUS";
230
231         printf("\nTesting LookupNames with bogus names\n");
232
233         sids.count = 0;
234         sids.sids = NULL;
235
236         names = talloc_array(mem_ctx, struct lsa_String, tnames.count);
237         for (i=0;i<tnames.count;i++) {
238                 init_lsa_String(&names[i], tnames.names[i].name.string);
239         }
240
241         r.in.handle = handle;
242         r.in.num_names = tnames.count;
243         r.in.names = names;
244         r.in.sids = &sids;
245         r.in.level = 1;
246         r.in.count = &count;
247         r.out.count = &count;
248         r.out.sids = &sids;
249
250         status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
251         if (!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
252                 printf("LookupNames failed - %s\n", nt_errstr(status));
253                 return false;
254         }
255
256         printf("\n");
257
258         return true;
259 }
260
261 static bool test_LookupNames_wellknown(struct dcerpc_pipe *p, 
262                                        TALLOC_CTX *mem_ctx, 
263                                        struct policy_handle *handle)
264 {
265         struct lsa_TranslatedName name;
266         struct lsa_TransNameArray tnames;
267         bool ret = true;
268
269         printf("Testing LookupNames with well known names\n");
270
271         tnames.names = &name;
272         tnames.count = 1;
273         name.name.string = "NT AUTHORITY\\SYSTEM";
274         name.sid_type = SID_NAME_WKN_GRP;
275         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
276
277         name.name.string = "NT AUTHORITY\\ANONYMOUS LOGON";
278         name.sid_type = SID_NAME_WKN_GRP;
279         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
280
281         name.name.string = "NT AUTHORITY\\Authenticated Users";
282         name.sid_type = SID_NAME_WKN_GRP;
283         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
284
285 #if 0
286         name.name.string = "NT AUTHORITY";
287         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
288
289         name.name.string = "NT AUTHORITY\\";
290         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
291 #endif
292
293         name.name.string = "BUILTIN\\";
294         name.sid_type = SID_NAME_DOMAIN;
295         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
296
297         name.name.string = "BUILTIN\\Administrators";
298         name.sid_type = SID_NAME_ALIAS;
299         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
300
301         name.name.string = "SYSTEM";
302         name.sid_type = SID_NAME_WKN_GRP;
303         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
304
305         name.name.string = "Everyone";
306         name.sid_type = SID_NAME_WKN_GRP;
307         ret &= test_LookupNames(p, mem_ctx, handle, &tnames);
308         return ret;
309 }
310
311 static bool test_LookupNames2(struct dcerpc_pipe *p, 
312                               TALLOC_CTX *mem_ctx, 
313                               struct policy_handle *handle,
314                               struct lsa_TransNameArray2 *tnames)
315 {
316         struct lsa_LookupNames2 r;
317         struct lsa_TransSidArray2 sids;
318         struct lsa_String *names;
319         uint32_t count = 0;
320         NTSTATUS status;
321         int i;
322
323         printf("\nTesting LookupNames2 with %d names\n", tnames->count);
324
325         sids.count = 0;
326         sids.sids = NULL;
327
328         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
329         for (i=0;i<tnames->count;i++) {
330                 init_lsa_String(&names[i], tnames->names[i].name.string);
331         }
332
333         r.in.handle = handle;
334         r.in.num_names = tnames->count;
335         r.in.names = names;
336         r.in.sids = &sids;
337         r.in.level = 1;
338         r.in.count = &count;
339         r.in.unknown1 = 0;
340         r.in.unknown2 = 0;
341         r.out.count = &count;
342         r.out.sids = &sids;
343
344         status = dcerpc_lsa_LookupNames2(p, mem_ctx, &r);
345         if (!NT_STATUS_IS_OK(status)) {
346                 printf("LookupNames2 failed - %s\n", nt_errstr(status));
347                 return false;
348         }
349
350         printf("\n");
351
352         return true;
353 }
354
355
356 static bool test_LookupNames3(struct dcerpc_pipe *p, 
357                               TALLOC_CTX *mem_ctx, 
358                               struct policy_handle *handle,
359                               struct lsa_TransNameArray2 *tnames)
360 {
361         struct lsa_LookupNames3 r;
362         struct lsa_TransSidArray3 sids;
363         struct lsa_String *names;
364         uint32_t count = 0;
365         NTSTATUS status;
366         int i;
367
368         printf("\nTesting LookupNames3 with %d names\n", tnames->count);
369
370         sids.count = 0;
371         sids.sids = NULL;
372
373         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
374         for (i=0;i<tnames->count;i++) {
375                 init_lsa_String(&names[i], tnames->names[i].name.string);
376         }
377
378         r.in.handle = handle;
379         r.in.num_names = tnames->count;
380         r.in.names = names;
381         r.in.sids = &sids;
382         r.in.level = 1;
383         r.in.count = &count;
384         r.in.unknown1 = 0;
385         r.in.unknown2 = 0;
386         r.out.count = &count;
387         r.out.sids = &sids;
388
389         status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
390         if (!NT_STATUS_IS_OK(status)) {
391                 printf("LookupNames3 failed - %s\n", nt_errstr(status));
392                 return false;
393         }
394
395         printf("\n");
396
397         return true;
398 }
399
400 static bool test_LookupNames4(struct dcerpc_pipe *p, 
401                               TALLOC_CTX *mem_ctx, 
402                               struct lsa_TransNameArray2 *tnames)
403 {
404         struct lsa_LookupNames4 r;
405         struct lsa_TransSidArray3 sids;
406         struct lsa_String *names;
407         uint32_t count = 0;
408         NTSTATUS status;
409         int i;
410
411         printf("\nTesting LookupNames4 with %d names\n", tnames->count);
412
413         sids.count = 0;
414         sids.sids = NULL;
415
416         names = talloc_array(mem_ctx, struct lsa_String, tnames->count);
417         for (i=0;i<tnames->count;i++) {
418                 init_lsa_String(&names[i], tnames->names[i].name.string);
419         }
420
421         r.in.num_names = tnames->count;
422         r.in.names = names;
423         r.in.sids = &sids;
424         r.in.level = 1;
425         r.in.count = &count;
426         r.in.unknown1 = 0;
427         r.in.unknown2 = 0;
428         r.out.count = &count;
429         r.out.sids = &sids;
430
431         status = dcerpc_lsa_LookupNames4(p, mem_ctx, &r);
432         if (!NT_STATUS_IS_OK(status)) {
433                 printf("LookupNames4 failed - %s\n", nt_errstr(status));
434                 return false;
435         }
436
437         printf("\n");
438
439         return true;
440 }
441
442
443 static bool test_LookupSids(struct dcerpc_pipe *p, 
444                             TALLOC_CTX *mem_ctx, 
445                             struct policy_handle *handle,
446                             struct lsa_SidArray *sids)
447 {
448         struct lsa_LookupSids r;
449         struct lsa_TransNameArray names;
450         uint32_t count = sids->num_sids;
451         NTSTATUS status;
452
453         printf("\nTesting LookupSids\n");
454
455         names.count = 0;
456         names.names = NULL;
457
458         r.in.handle = handle;
459         r.in.sids = sids;
460         r.in.names = &names;
461         r.in.level = 1;
462         r.in.count = &count;
463         r.out.count = &count;
464         r.out.names = &names;
465
466         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
467         if (!NT_STATUS_IS_OK(status)) {
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
478         return true;
479 }
480
481
482 static bool test_LookupSids2(struct dcerpc_pipe *p, 
483                             TALLOC_CTX *mem_ctx, 
484                             struct policy_handle *handle,
485                             struct lsa_SidArray *sids)
486 {
487         struct lsa_LookupSids2 r;
488         struct lsa_TransNameArray2 names;
489         uint32_t count = sids->num_sids;
490         NTSTATUS status;
491
492         printf("\nTesting LookupSids2\n");
493
494         names.count = 0;
495         names.names = NULL;
496
497         r.in.handle = handle;
498         r.in.sids = sids;
499         r.in.names = &names;
500         r.in.level = 1;
501         r.in.count = &count;
502         r.in.unknown1 = 0;
503         r.in.unknown2 = 0;
504         r.out.count = &count;
505         r.out.names = &names;
506
507         status = dcerpc_lsa_LookupSids2(p, mem_ctx, &r);
508         if (!NT_STATUS_IS_OK(status)) {
509                 printf("LookupSids2 failed - %s\n", nt_errstr(status));
510                 return false;
511         }
512
513         printf("\n");
514
515         if (!test_LookupNames2(p, mem_ctx, handle, &names)) {
516                 return false;
517         }
518
519         if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
520                 return false;
521         }
522
523         return true;
524 }
525
526 static bool test_LookupSids3(struct dcerpc_pipe *p, 
527                             TALLOC_CTX *mem_ctx, 
528                             struct lsa_SidArray *sids)
529 {
530         struct lsa_LookupSids3 r;
531         struct lsa_TransNameArray2 names;
532         uint32_t count = sids->num_sids;
533         NTSTATUS status;
534
535         printf("\nTesting LookupSids3\n");
536
537         names.count = 0;
538         names.names = NULL;
539
540         r.in.sids = sids;
541         r.in.names = &names;
542         r.in.level = 1;
543         r.in.count = &count;
544         r.in.unknown1 = 0;
545         r.in.unknown2 = 0;
546         r.out.count = &count;
547         r.out.names = &names;
548
549         status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
550         if (!NT_STATUS_IS_OK(status)) {
551                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
552                     NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
553                         printf("not considering %s to be an error\n", nt_errstr(status));
554                         return true;
555                 }
556                 printf("LookupSids3 failed - %s - not considered an error\n", 
557                        nt_errstr(status));
558                 return false;
559         }
560
561         printf("\n");
562
563         if (!test_LookupNames4(p, mem_ctx, &names)) {
564                 return false;
565         }
566
567         return true;
568 }
569
570 bool test_many_LookupSids(struct dcerpc_pipe *p, 
571                           TALLOC_CTX *mem_ctx, 
572                           struct policy_handle *handle)
573 {
574         uint32_t count;
575         NTSTATUS status;
576         struct lsa_SidArray sids;
577         int i;
578
579         printf("\nTesting LookupSids with lots of SIDs\n");
580
581         sids.num_sids = 100;
582
583         sids.sids = talloc_array(mem_ctx, struct lsa_SidPtr, sids.num_sids);
584
585         for (i=0; i<sids.num_sids; i++) {
586                 const char *sidstr = "S-1-5-32-545";
587                 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
588         }
589
590         count = sids.num_sids;
591
592         if (handle) {
593                 struct lsa_LookupSids r;
594                 struct lsa_TransNameArray names;
595                 names.count = 0;
596                 names.names = NULL;
597
598                 r.in.handle = handle;
599                 r.in.sids = &sids;
600                 r.in.names = &names;
601                 r.in.level = 1;
602                 r.in.count = &names.count;
603                 r.out.count = &count;
604                 r.out.names = &names;
605                 
606                 status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
607                 if (!NT_STATUS_IS_OK(status)) {
608                         printf("LookupSids failed - %s\n", nt_errstr(status));
609                         return false;
610                 }
611                 
612                 printf("\n");
613                 
614                 if (!test_LookupNames(p, mem_ctx, handle, &names)) {
615                         return false;
616                 }
617         } else {
618                 struct lsa_LookupSids3 r;
619                 struct lsa_TransNameArray2 names;
620
621                 names.count = 0;
622                 names.names = NULL;
623
624                 printf("\nTesting LookupSids3\n");
625                 
626                 r.in.sids = &sids;
627                 r.in.names = &names;
628                 r.in.level = 1;
629                 r.in.count = &count;
630                 r.in.unknown1 = 0;
631                 r.in.unknown2 = 0;
632                 r.out.count = &count;
633                 r.out.names = &names;
634                 
635                 status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
636                 if (!NT_STATUS_IS_OK(status)) {
637                         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
638                             NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
639                                 printf("not considering %s to be an error\n", nt_errstr(status));
640                                 return true;
641                         }
642                         printf("LookupSids3 failed - %s\n", 
643                                nt_errstr(status));
644                         return false;
645                 }
646                 if (!test_LookupNames4(p, mem_ctx, &names)) {
647                         return false;
648                 }
649         }
650
651         printf("\n");
652
653
654
655         return true;
656 }
657
658 static void lookupsids_cb(struct rpc_request *req)
659 {
660         int *replies = (int *)req->async.private_data;
661         NTSTATUS status;
662
663         status = dcerpc_ndr_request_recv(req);
664         if (!NT_STATUS_IS_OK(status)) {
665                 printf("lookupsids returned %s\n", nt_errstr(status));
666                 *replies = -1;
667         }
668
669         if (*replies >= 0) {
670                 *replies += 1;
671         }
672 }
673
674 static bool test_LookupSids_async(struct dcerpc_pipe *p, 
675                                   TALLOC_CTX *mem_ctx, 
676                                   struct policy_handle *handle)
677 {
678         struct lsa_SidArray sids;
679         struct lsa_SidPtr sidptr;
680         uint32_t *count;
681         struct lsa_TransNameArray *names;
682         struct lsa_LookupSids *r;
683         struct rpc_request **req;
684         int i, replies;
685         bool ret = true;
686         const int num_async_requests = 50;
687
688         count = talloc_array(mem_ctx, uint32_t, num_async_requests);
689         names = talloc_array(mem_ctx, struct lsa_TransNameArray, num_async_requests);
690         r = talloc_array(mem_ctx, struct lsa_LookupSids, num_async_requests);
691
692         printf("\nTesting %d async lookupsids request\n", num_async_requests);
693
694         req = talloc_array(mem_ctx, struct rpc_request *, num_async_requests);
695
696         sids.num_sids = 1;
697         sids.sids = &sidptr;
698         sidptr.sid = dom_sid_parse_talloc(mem_ctx, "S-1-5-32-545");
699
700         replies = 0;
701
702         for (i=0; i<num_async_requests; i++) {
703                 count[i] = 0;
704                 names[i].count = 0;
705                 names[i].names = NULL;
706
707                 r[i].in.handle = handle;
708                 r[i].in.sids = &sids;
709                 r[i].in.names = &names[i];
710                 r[i].in.level = 1;
711                 r[i].in.count = &names[i].count;
712                 r[i].out.count = &count[i];
713                 r[i].out.names = &names[i];
714                 
715                 req[i] = dcerpc_lsa_LookupSids_send(p, req, &r[i]);
716                 if (req[i] == NULL) {
717                         ret = false;
718                         break;
719                 }
720
721                 req[i]->async.callback = lookupsids_cb;
722                 req[i]->async.private_data = &replies;
723         }
724
725         while (replies >= 0 && replies < num_async_requests) {
726                 event_loop_once(p->conn->event_ctx);
727         }
728
729         talloc_free(req);
730
731         if (replies < 0) {
732                 ret = false;
733         }
734
735         return ret;
736 }
737
738 static bool test_LookupPrivValue(struct dcerpc_pipe *p, 
739                                  TALLOC_CTX *mem_ctx, 
740                                  struct policy_handle *handle,
741                                  struct lsa_String *name)
742 {
743         NTSTATUS status;
744         struct lsa_LookupPrivValue r;
745         struct lsa_LUID luid;
746
747         r.in.handle = handle;
748         r.in.name = name;
749         r.out.luid = &luid;
750
751         status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
752         if (!NT_STATUS_IS_OK(status)) {
753                 printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
754                 return false;
755         }
756
757         return true;
758 }
759
760 static bool test_LookupPrivName(struct dcerpc_pipe *p, 
761                                 TALLOC_CTX *mem_ctx, 
762                                 struct policy_handle *handle,
763                                 struct lsa_LUID *luid)
764 {
765         NTSTATUS status;
766         struct lsa_LookupPrivName r;
767
768         r.in.handle = handle;
769         r.in.luid = luid;
770
771         status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
772         if (!NT_STATUS_IS_OK(status)) {
773                 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
774                 return false;
775         }
776
777         return true;
778 }
779
780 static bool test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p, 
781                                              TALLOC_CTX *mem_ctx,                                 
782                                              struct policy_handle *acct_handle,
783                                              struct lsa_LUID *luid)
784 {
785         NTSTATUS status;
786         struct lsa_RemovePrivilegesFromAccount r;
787         struct lsa_PrivilegeSet privs;
788         bool ret = true;
789
790         printf("Testing RemovePrivilegesFromAccount\n");
791
792         r.in.handle = acct_handle;
793         r.in.remove_all = 0;
794         r.in.privs = &privs;
795
796         privs.count = 1;
797         privs.unknown = 0;
798         privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
799         privs.set[0].luid = *luid;
800         privs.set[0].attribute = 0;
801
802         status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
803         if (!NT_STATUS_IS_OK(status)) {
804                 printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
805                 return false;
806         }
807
808         return ret;
809 }
810
811 static bool test_AddPrivilegesToAccount(struct dcerpc_pipe *p, 
812                                         TALLOC_CTX *mem_ctx,                              
813                                         struct policy_handle *acct_handle,
814                                         struct lsa_LUID *luid)
815 {
816         NTSTATUS status;
817         struct lsa_AddPrivilegesToAccount r;
818         struct lsa_PrivilegeSet privs;
819         bool ret = true;
820
821         printf("Testing AddPrivilegesToAccount\n");
822
823         r.in.handle = acct_handle;
824         r.in.privs = &privs;
825
826         privs.count = 1;
827         privs.unknown = 0;
828         privs.set = talloc_array(mem_ctx, struct lsa_LUIDAttribute, 1);
829         privs.set[0].luid = *luid;
830         privs.set[0].attribute = 0;
831
832         status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
833         if (!NT_STATUS_IS_OK(status)) {
834                 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
835                 return false;
836         }
837
838         return ret;
839 }
840
841 static bool test_EnumPrivsAccount(struct dcerpc_pipe *p, 
842                                   TALLOC_CTX *mem_ctx,                            
843                                   struct policy_handle *handle,
844                                   struct policy_handle *acct_handle)
845 {
846         NTSTATUS status;
847         struct lsa_EnumPrivsAccount r;
848         bool ret = true;
849
850         printf("Testing EnumPrivsAccount\n");
851
852         r.in.handle = acct_handle;
853
854         status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
855         if (!NT_STATUS_IS_OK(status)) {
856                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
857                 return false;
858         }
859
860         if (r.out.privs && r.out.privs->count > 0) {
861                 int i;
862                 for (i=0;i<r.out.privs->count;i++) {
863                         test_LookupPrivName(p, mem_ctx, handle, 
864                                             &r.out.privs->set[i].luid);
865                 }
866
867                 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle, 
868                                                         &r.out.privs->set[0].luid);
869                 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle, 
870                                                    &r.out.privs->set[0].luid);
871         }
872
873         return ret;
874 }
875
876 static bool test_Delete(struct dcerpc_pipe *p, 
877                        TALLOC_CTX *mem_ctx, 
878                        struct policy_handle *handle)
879 {
880         NTSTATUS status;
881         struct lsa_Delete r;
882
883         printf("testing Delete\n");
884
885         r.in.handle = handle;
886         status = dcerpc_lsa_Delete(p, mem_ctx, &r);
887         if (!NT_STATUS_IS_OK(status)) {
888                 printf("Delete failed - %s\n", nt_errstr(status));
889                 return false;
890         }
891
892         return true;
893 }
894
895
896 static bool test_CreateAccount(struct dcerpc_pipe *p, 
897                                TALLOC_CTX *mem_ctx, 
898                                struct policy_handle *handle)
899 {
900         NTSTATUS status;
901         struct lsa_CreateAccount r;
902         struct dom_sid2 *newsid;
903         struct policy_handle acct_handle;
904
905         newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
906
907         printf("Testing CreateAccount\n");
908
909         r.in.handle = handle;
910         r.in.sid = newsid;
911         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
912         r.out.acct_handle = &acct_handle;
913
914         status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
915         if (!NT_STATUS_IS_OK(status)) {
916                 printf("CreateAccount failed - %s\n", nt_errstr(status));
917                 return false;
918         }
919
920         if (!test_Delete(p, mem_ctx, &acct_handle)) {
921                 return false;
922         }
923
924         return true;
925 }
926
927 static bool test_DeleteTrustedDomain(struct dcerpc_pipe *p, 
928                                      TALLOC_CTX *mem_ctx, 
929                                      struct policy_handle *handle,
930                                      struct lsa_StringLarge name)
931 {
932         NTSTATUS status;
933         struct lsa_OpenTrustedDomainByName r;
934         struct policy_handle trustdom_handle;
935
936         r.in.handle = handle;
937         r.in.name.string = name.string;
938         r.in.access_mask = SEC_STD_DELETE;
939         r.out.trustdom_handle = &trustdom_handle;
940
941         status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
942         if (!NT_STATUS_IS_OK(status)) {
943                 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
944                 return false;
945         }
946
947         if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
948                 return false;
949         }
950
951         return true;
952 }
953
954 static bool test_DeleteTrustedDomainBySid(struct dcerpc_pipe *p, 
955                                           TALLOC_CTX *mem_ctx, 
956                                           struct policy_handle *handle,
957                                           struct dom_sid *sid)
958 {
959         NTSTATUS status;
960         struct lsa_DeleteTrustedDomain r;
961
962         r.in.handle = handle;
963         r.in.dom_sid = sid;
964
965         status = dcerpc_lsa_DeleteTrustedDomain(p, mem_ctx, &r);
966         if (!NT_STATUS_IS_OK(status)) {
967                 printf("lsa_DeleteTrustedDomain failed - %s\n", nt_errstr(status));
968                 return false;
969         }
970
971         return true;
972 }
973
974
975 static bool test_CreateSecret(struct dcerpc_pipe *p, 
976                               TALLOC_CTX *mem_ctx, 
977                               struct policy_handle *handle)
978 {
979         NTSTATUS status;
980         struct lsa_CreateSecret r;
981         struct lsa_OpenSecret r2;
982         struct lsa_SetSecret r3;
983         struct lsa_QuerySecret r4;
984         struct lsa_SetSecret r5;
985         struct lsa_QuerySecret r6;
986         struct lsa_SetSecret r7;
987         struct lsa_QuerySecret r8;
988         struct policy_handle sec_handle, sec_handle2, sec_handle3;
989         struct lsa_Delete d;
990         struct lsa_DATA_BUF buf1;
991         struct lsa_DATA_BUF_PTR bufp1;
992         struct lsa_DATA_BUF_PTR bufp2;
993         DATA_BLOB enc_key;
994         bool ret = true;
995         DATA_BLOB session_key;
996         NTTIME old_mtime, new_mtime;
997         DATA_BLOB blob1, blob2;
998         const char *secret1 = "abcdef12345699qwerty";
999         char *secret2;
1000         const char *secret3 = "ABCDEF12345699QWERTY";
1001         char *secret4;
1002         const char *secret5 = "NEW-SAMBA4-SECRET";
1003         char *secret6;
1004         char *secname[2];
1005         int i;
1006         const int LOCAL = 0;
1007         const int GLOBAL = 1;
1008
1009         secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
1010         secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
1011
1012         for (i=0; i< 2; i++) {
1013                 printf("Testing CreateSecret of %s\n", secname[i]);
1014                 
1015                 init_lsa_String(&r.in.name, secname[i]);
1016                 
1017                 r.in.handle = handle;
1018                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1019                 r.out.sec_handle = &sec_handle;
1020                 
1021                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1022                 if (!NT_STATUS_IS_OK(status)) {
1023                         printf("CreateSecret failed - %s\n", nt_errstr(status));
1024                         return false;
1025                 }
1026                 
1027                 r.in.handle = handle;
1028                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1029                 r.out.sec_handle = &sec_handle3;
1030                 
1031                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
1032                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1033                         printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
1034                         return false;
1035                 }
1036                 
1037                 r2.in.handle = handle;
1038                 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1039                 r2.in.name = r.in.name;
1040                 r2.out.sec_handle = &sec_handle2;
1041                 
1042                 printf("Testing OpenSecret\n");
1043                 
1044                 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1045                 if (!NT_STATUS_IS_OK(status)) {
1046                         printf("OpenSecret failed - %s\n", nt_errstr(status));
1047                         return false;
1048                 }
1049                 
1050                 status = dcerpc_fetch_session_key(p, &session_key);
1051                 if (!NT_STATUS_IS_OK(status)) {
1052                         printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
1053                         return false;
1054                 }
1055                 
1056                 enc_key = sess_encrypt_string(secret1, &session_key);
1057                 
1058                 r3.in.sec_handle = &sec_handle;
1059                 r3.in.new_val = &buf1;
1060                 r3.in.old_val = NULL;
1061                 r3.in.new_val->data = enc_key.data;
1062                 r3.in.new_val->length = enc_key.length;
1063                 r3.in.new_val->size = enc_key.length;
1064                 
1065                 printf("Testing SetSecret\n");
1066                 
1067                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1068                 if (!NT_STATUS_IS_OK(status)) {
1069                         printf("SetSecret failed - %s\n", nt_errstr(status));
1070                         return false;
1071                 }
1072                 
1073                 r3.in.sec_handle = &sec_handle;
1074                 r3.in.new_val = &buf1;
1075                 r3.in.old_val = NULL;
1076                 r3.in.new_val->data = enc_key.data;
1077                 r3.in.new_val->length = enc_key.length;
1078                 r3.in.new_val->size = enc_key.length;
1079                 
1080                 /* break the encrypted data */
1081                 enc_key.data[0]++;
1082
1083                 printf("Testing SetSecret with broken key\n");
1084                 
1085                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
1086                 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
1087                         printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
1088                         ret = false;
1089                 }
1090                 
1091                 data_blob_free(&enc_key);
1092                 
1093                 ZERO_STRUCT(new_mtime);
1094                 ZERO_STRUCT(old_mtime);
1095                 
1096                 /* fetch the secret back again */
1097                 r4.in.sec_handle = &sec_handle;
1098                 r4.in.new_val = &bufp1;
1099                 r4.in.new_mtime = &new_mtime;
1100                 r4.in.old_val = NULL;
1101                 r4.in.old_mtime = NULL;
1102                 
1103                 bufp1.buf = NULL;
1104                 
1105                 printf("Testing QuerySecret\n");
1106                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
1107                 if (!NT_STATUS_IS_OK(status)) {
1108                         printf("QuerySecret failed - %s\n", nt_errstr(status));
1109                         ret = false;
1110                 } else {
1111                         if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
1112                                 printf("No secret buffer returned\n");
1113                                 ret = false;
1114                         } else {
1115                                 blob1.data = r4.out.new_val->buf->data;
1116                                 blob1.length = r4.out.new_val->buf->size;
1117                                 
1118                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1119                                 
1120                                 secret2 = sess_decrypt_string(mem_ctx, 
1121                                                               &blob1, &session_key);
1122                                 
1123                                 if (strcmp(secret1, secret2) != 0) {
1124                                         printf("Returned secret '%s' doesn't match '%s'\n", 
1125                                                secret2, secret1);
1126                                         ret = false;
1127                                 }
1128                         }
1129                 }
1130                 
1131                 enc_key = sess_encrypt_string(secret3, &session_key);
1132                 
1133                 r5.in.sec_handle = &sec_handle;
1134                 r5.in.new_val = &buf1;
1135                 r5.in.old_val = NULL;
1136                 r5.in.new_val->data = enc_key.data;
1137                 r5.in.new_val->length = enc_key.length;
1138                 r5.in.new_val->size = enc_key.length;
1139                 
1140                 printf("Testing SetSecret (existing value should move to old)\n");
1141                 
1142                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
1143                 if (!NT_STATUS_IS_OK(status)) {
1144                         printf("SetSecret failed - %s\n", nt_errstr(status));
1145                         ret = false;
1146                 }
1147                 
1148                 data_blob_free(&enc_key);
1149                 
1150                 ZERO_STRUCT(new_mtime);
1151                 ZERO_STRUCT(old_mtime);
1152                 
1153                 /* fetch the secret back again */
1154                 r6.in.sec_handle = &sec_handle;
1155                 r6.in.new_val = &bufp1;
1156                 r6.in.new_mtime = &new_mtime;
1157                 r6.in.old_val = &bufp2;
1158                 r6.in.old_mtime = &old_mtime;
1159                 
1160                 bufp1.buf = NULL;
1161                 bufp2.buf = NULL;
1162                 
1163                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
1164                 if (!NT_STATUS_IS_OK(status)) {
1165                         printf("QuerySecret failed - %s\n", nt_errstr(status));
1166                         ret = false;
1167                         secret4 = NULL;
1168                 } else {
1169
1170                         if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL 
1171                                 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1172                                 printf("Both secret buffers and both times not returned\n");
1173                                 ret = false;
1174                                 secret4 = NULL;
1175                         } else {
1176                                 blob1.data = r6.out.new_val->buf->data;
1177                                 blob1.length = r6.out.new_val->buf->size;
1178                                 
1179                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1180                                 
1181                                 secret4 = sess_decrypt_string(mem_ctx, 
1182                                                               &blob1, &session_key);
1183                                 
1184                                 if (strcmp(secret3, secret4) != 0) {
1185                                         printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1186                                         ret = false;
1187                                 }
1188
1189                                 blob1.data = r6.out.old_val->buf->data;
1190                                 blob1.length = r6.out.old_val->buf->length;
1191                                 
1192                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1193                                 
1194                                 secret2 = sess_decrypt_string(mem_ctx, 
1195                                                               &blob1, &session_key);
1196                                 
1197                                 if (strcmp(secret1, secret2) != 0) {
1198                                         printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1199                                         ret = false;
1200                                 }
1201                                 
1202                                 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1203                                         printf("Returned secret %s had same mtime for both secrets: %s\n", 
1204                                                secname[i],
1205                                                nt_time_string(mem_ctx, *r6.out.new_mtime));
1206                                         ret = false;
1207                                 }
1208                         }
1209                 }
1210
1211                 enc_key = sess_encrypt_string(secret5, &session_key);
1212                 
1213                 r7.in.sec_handle = &sec_handle;
1214                 r7.in.old_val = &buf1;
1215                 r7.in.old_val->data = enc_key.data;
1216                 r7.in.old_val->length = enc_key.length;
1217                 r7.in.old_val->size = enc_key.length;
1218                 r7.in.new_val = NULL;
1219                 
1220                 printf("Testing SetSecret of old Secret only\n");
1221                 
1222                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
1223                 if (!NT_STATUS_IS_OK(status)) {
1224                         printf("SetSecret failed - %s\n", nt_errstr(status));
1225                         ret = false;
1226                 }
1227                 
1228                 data_blob_free(&enc_key);
1229                 
1230                 /* fetch the secret back again */
1231                 r8.in.sec_handle = &sec_handle;
1232                 r8.in.new_val = &bufp1;
1233                 r8.in.new_mtime = &new_mtime;
1234                 r8.in.old_val = &bufp2;
1235                 r8.in.old_mtime = &old_mtime;
1236                 
1237                 bufp1.buf = NULL;
1238                 bufp2.buf = NULL;
1239                 
1240                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
1241                 if (!NT_STATUS_IS_OK(status)) {
1242                         printf("QuerySecret failed - %s\n", nt_errstr(status));
1243                         ret = false;
1244                 } else {
1245                         if (!r8.out.new_val || !r8.out.old_val) {
1246                                 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
1247                                 ret = false;
1248                         } else if (r8.out.new_val->buf == NULL) {
1249                                 if (i != LOCAL) { 
1250                                         printf("NEW secret buffer not returned after GLOBAL OLD set\n");
1251                                         ret = false;
1252                                 }
1253                         } else if (r8.out.old_val->buf == NULL) {
1254                                 printf("OLD secret buffer not returned after OLD set\n");
1255                                 ret = false;
1256                         } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1257                                 printf("Both times not returned after OLD set\n");
1258                                 ret = false;
1259                         } else {
1260                                 if (i == LOCAL) { 
1261                                         printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
1262                                         ret = false;
1263                                 }
1264                                 blob1.data = r8.out.new_val->buf->data;
1265                                 blob1.length = r8.out.new_val->buf->length;
1266                                 
1267                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1268                                 
1269                                 secret6 = sess_decrypt_string(mem_ctx,
1270                                                               &blob1, &session_key);
1271                                 
1272                                 if (strcmp(secret3, secret4) != 0) {
1273                                         printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
1274                                         ret = false;
1275                                 }
1276
1277                                 blob1.data = r8.out.old_val->buf->data;
1278                                 blob1.length = r8.out.old_val->buf->size;
1279                                 
1280                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
1281                                 
1282                                 secret6 = sess_decrypt_string(mem_ctx,
1283                                                               &blob1, &session_key);
1284                                 
1285                                 if (strcmp(secret5, secret6) != 0) {
1286                                         printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1287                                         ret = false;
1288                                 }
1289                                 
1290                                 if (*r8.out.new_mtime == *r8.out.old_mtime) {
1291                                         if (i != GLOBAL) { 
1292                                                 printf("Returned secret %s had same mtime for both secrets: %s\n", 
1293                                                        secname[i],
1294                                                        nt_time_string(mem_ctx, *r8.out.new_mtime));
1295                                                 ret = false;
1296                                         }
1297                                 } else {
1298                                         printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n", 
1299                                                secname[i],
1300                                                nt_time_string(mem_ctx, *r8.out.old_mtime),
1301                                                nt_time_string(mem_ctx, *r8.out.new_mtime));
1302                                         ret = false;
1303                                 }
1304                         }
1305                 }
1306
1307                 if (!test_Delete(p, mem_ctx, &sec_handle)) {
1308                         ret = false;
1309                 }
1310                 
1311                 d.in.handle = &sec_handle2;
1312                 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
1313                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1314                         printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1315                         ret = false;
1316                 } else {
1317
1318                         printf("Testing OpenSecret of just-deleted secret\n");
1319                         
1320                         status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1321                         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1322                                 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1323                                 ret = false;
1324                         }
1325                 }
1326                 
1327         }
1328
1329         return ret;
1330 }
1331
1332
1333 static bool test_EnumAccountRights(struct dcerpc_pipe *p, 
1334                                    TALLOC_CTX *mem_ctx, 
1335                                    struct policy_handle *acct_handle,
1336                                    struct dom_sid *sid)
1337 {
1338         NTSTATUS status;
1339         struct lsa_EnumAccountRights r;
1340         struct lsa_RightSet rights;
1341
1342         printf("Testing EnumAccountRights\n");
1343
1344         r.in.handle = acct_handle;
1345         r.in.sid = sid;
1346         r.out.rights = &rights;
1347
1348         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1349         if (!NT_STATUS_IS_OK(status)) {
1350                 printf("EnumAccountRights of %s failed - %s\n", 
1351                        dom_sid_string(mem_ctx, sid), nt_errstr(status));
1352                 return false;
1353         }
1354
1355         return true;
1356 }
1357
1358
1359 static bool test_QuerySecurity(struct dcerpc_pipe *p, 
1360                              struct torture_context *tctx, 
1361                              struct policy_handle *handle,
1362                              struct policy_handle *acct_handle)
1363 {
1364         NTSTATUS status;
1365         struct lsa_QuerySecurity r;
1366
1367         if (torture_setting_bool(tctx, "samba4", false)) {
1368                 printf("skipping QuerySecurity test against Samba4\n");
1369                 return true;
1370         }
1371
1372         printf("Testing QuerySecurity\n");
1373
1374         r.in.handle = acct_handle;
1375         r.in.sec_info = 7;
1376
1377         status = dcerpc_lsa_QuerySecurity(p, tctx, &r);
1378         if (!NT_STATUS_IS_OK(status)) {
1379                 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1380                 return false;
1381         }
1382
1383         return true;
1384 }
1385
1386 static bool test_OpenAccount(struct dcerpc_pipe *p, 
1387                              TALLOC_CTX *mem_ctx, 
1388                              struct policy_handle *handle,
1389                              struct dom_sid *sid)
1390 {
1391         NTSTATUS status;
1392         struct lsa_OpenAccount r;
1393         struct policy_handle acct_handle;
1394
1395         printf("Testing OpenAccount\n");
1396
1397         r.in.handle = handle;
1398         r.in.sid = sid;
1399         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1400         r.out.acct_handle = &acct_handle;
1401
1402         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1403         if (!NT_STATUS_IS_OK(status)) {
1404                 printf("OpenAccount failed - %s\n", nt_errstr(status));
1405                 return false;
1406         }
1407
1408         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1409                 return false;
1410         }
1411
1412         if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1413                 return false;
1414         }
1415
1416         return true;
1417 }
1418
1419 static bool test_EnumAccounts(struct dcerpc_pipe *p, 
1420                           TALLOC_CTX *mem_ctx, 
1421                           struct policy_handle *handle)
1422 {
1423         NTSTATUS status;
1424         struct lsa_EnumAccounts r;
1425         struct lsa_SidArray sids1, sids2;
1426         uint32_t resume_handle = 0;
1427         int i;
1428         bool ret = true;
1429
1430         printf("\ntesting EnumAccounts\n");
1431
1432         r.in.handle = handle;
1433         r.in.resume_handle = &resume_handle;
1434         r.in.num_entries = 100;
1435         r.out.resume_handle = &resume_handle;
1436         r.out.sids = &sids1;
1437
1438         resume_handle = 0;
1439         while (true) {
1440                 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1441                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1442                         break;
1443                 }
1444                 if (!NT_STATUS_IS_OK(status)) {
1445                         printf("EnumAccounts failed - %s\n", nt_errstr(status));
1446                         return false;
1447                 }
1448
1449                 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1450                         return false;
1451                 }
1452
1453                 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1454                         return false;
1455                 }
1456
1457                 if (!test_LookupSids3(p, mem_ctx, &sids1)) {
1458                         return false;
1459                 }
1460
1461                 printf("testing all accounts\n");
1462                 for (i=0;i<sids1.num_sids;i++) {
1463                         ret &= test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1464                         ret &= test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1465                 }
1466                 printf("\n");
1467         }
1468
1469         if (sids1.num_sids < 3) {
1470                 return ret;
1471         }
1472         
1473         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1474         resume_handle = 2;
1475         r.in.num_entries = 1;
1476         r.out.sids = &sids2;
1477
1478         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1479         if (!NT_STATUS_IS_OK(status)) {
1480                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1481                 return false;
1482         }
1483
1484         if (sids2.num_sids != 1) {
1485                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1486                 return false;
1487         }
1488
1489         return true;
1490 }
1491
1492 static bool test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1493                                 TALLOC_CTX *mem_ctx,
1494                                 struct policy_handle *handle,
1495                                 struct lsa_String *priv_name)
1496 {
1497         struct lsa_LookupPrivDisplayName r;
1498         NTSTATUS status;
1499         /* produce a reasonable range of language output without screwing up
1500            terminals */
1501         uint16_t language_id = (random() % 4) + 0x409;
1502
1503         printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1504         
1505         r.in.handle = handle;
1506         r.in.name = priv_name;
1507         r.in.language_id = &language_id;
1508         r.out.language_id = &language_id;
1509         r.in.unknown = 0;
1510
1511         status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1512         if (!NT_STATUS_IS_OK(status)) {
1513                 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1514                 return false;
1515         }
1516         printf("%s -> \"%s\"  (language 0x%x/0x%x)\n", 
1517                priv_name->string, r.out.disp_name->string, 
1518                *r.in.language_id, *r.out.language_id);
1519
1520         return true;
1521 }
1522
1523 static bool test_EnumAccountsWithUserRight(struct dcerpc_pipe *p, 
1524                                 TALLOC_CTX *mem_ctx,
1525                                 struct policy_handle *handle,
1526                                 struct lsa_String *priv_name)
1527 {
1528         struct lsa_EnumAccountsWithUserRight r;
1529         struct lsa_SidArray sids;
1530         NTSTATUS status;
1531
1532         ZERO_STRUCT(sids);
1533         
1534         printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1535         
1536         r.in.handle = handle;
1537         r.in.name = priv_name;
1538         r.out.sids = &sids;
1539
1540         status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1541
1542         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1543         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1544                 return true;
1545         }
1546
1547         if (!NT_STATUS_IS_OK(status)) {
1548                 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1549                 return false;
1550         }
1551         
1552         return true;
1553 }
1554
1555
1556 static bool test_EnumPrivs(struct dcerpc_pipe *p, 
1557                            TALLOC_CTX *mem_ctx, 
1558                            struct policy_handle *handle)
1559 {
1560         NTSTATUS status;
1561         struct lsa_EnumPrivs r;
1562         struct lsa_PrivArray privs1;
1563         uint32_t resume_handle = 0;
1564         int i;
1565         bool ret = true;
1566
1567         printf("\ntesting EnumPrivs\n");
1568
1569         r.in.handle = handle;
1570         r.in.resume_handle = &resume_handle;
1571         r.in.max_count = 100;
1572         r.out.resume_handle = &resume_handle;
1573         r.out.privs = &privs1;
1574
1575         resume_handle = 0;
1576         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1577         if (!NT_STATUS_IS_OK(status)) {
1578                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1579                 return false;
1580         }
1581
1582         for (i = 0; i< privs1.count; i++) {
1583                 test_LookupPrivDisplayName(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1584                 test_LookupPrivValue(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name);
1585                 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
1586                         ret = false;
1587                 }
1588         }
1589
1590         return ret;
1591 }
1592
1593 static bool test_QueryForestTrustInformation(struct dcerpc_pipe *p, 
1594                                              struct torture_context *tctx, 
1595                                              struct policy_handle *handle,
1596                                              const char *trusted_domain_name)
1597 {
1598         bool ret = true;
1599         struct lsa_lsaRQueryForestTrustInformation r;
1600         NTSTATUS status;
1601         struct lsa_String string;
1602         struct lsa_ForestTrustInformation info, *info_ptr;
1603
1604         printf("\nTesting lsaRQueryForestTrustInformation\n");
1605
1606         if (torture_setting_bool(tctx, "samba4", false)) {
1607                 printf("skipping QueryForestTrustInformation against Samba4\n");
1608                 return true;
1609         }
1610
1611         ZERO_STRUCT(string);
1612
1613         if (trusted_domain_name) {
1614                 init_lsa_String(&string, trusted_domain_name);
1615         }
1616
1617         info_ptr = &info;
1618
1619         r.in.handle = handle;
1620         r.in.trusted_domain_name = &string;
1621         r.in.unknown = 0;
1622         r.out.forest_trust_info = &info_ptr;
1623
1624         status = dcerpc_lsa_lsaRQueryForestTrustInformation(p, tctx, &r);
1625
1626         if (!NT_STATUS_IS_OK(status)) {
1627                 printf("lsaRQueryForestTrustInformation failed - %s\n", nt_errstr(status));
1628                 ret = false;
1629         }
1630
1631         return ret;
1632 }
1633
1634 static bool test_query_each_TrustDomEx(struct dcerpc_pipe *p, 
1635                                        TALLOC_CTX *mem_ctx, 
1636                                        struct policy_handle *handle, 
1637                                        struct lsa_DomainListEx *domains) 
1638 {
1639         int i;
1640         bool ret = true;
1641
1642         for (i=0; i< domains->count; i++) {
1643
1644                 if (domains->domains[i].trust_attributes & NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1645                         ret &= test_QueryForestTrustInformation(p, mem_ctx, handle, 
1646                                                                 domains->domains[i].domain_name.string);
1647                 }
1648         }
1649
1650         return ret;
1651 }
1652
1653 static bool test_query_each_TrustDom(struct dcerpc_pipe *p, 
1654                                      TALLOC_CTX *mem_ctx, 
1655                                      struct policy_handle *handle, 
1656                                      struct lsa_DomainList *domains) 
1657 {
1658         NTSTATUS status;
1659         int i,j;
1660         bool ret = true;
1661                 
1662         printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1663         for (i=0; i< domains->count; i++) {
1664                 struct lsa_OpenTrustedDomain trust;
1665                 struct lsa_OpenTrustedDomainByName trust_by_name;
1666                 struct policy_handle trustdom_handle;
1667                 struct policy_handle handle2;
1668                 struct lsa_Close c;
1669                 struct lsa_CloseTrustedDomainEx c_trust;
1670                 int levels [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
1671                 int ok[]      = {1, 0, 1, 0, 0, 1, 0, 1, 0,  0,  0,  1};
1672
1673                 if (domains->domains[i].sid) {
1674                         trust.in.handle = handle;
1675                         trust.in.sid = domains->domains[i].sid;
1676                         trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1677                         trust.out.trustdom_handle = &trustdom_handle;
1678                         
1679                         status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1680                         
1681                         if (!NT_STATUS_IS_OK(status)) {
1682                                 printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1683                                 return false;
1684                         }
1685                         
1686                         c.in.handle = &trustdom_handle;
1687                         c.out.handle = &handle2;
1688                         
1689                         c_trust.in.handle = &trustdom_handle;
1690                         c_trust.out.handle = &handle2;
1691                         
1692                         for (j=0; j < ARRAY_SIZE(levels); j++) {
1693                                 struct lsa_QueryTrustedDomainInfo q;
1694                                 union lsa_TrustedDomainInfo info;
1695                                 q.in.trustdom_handle = &trustdom_handle;
1696                                 q.in.level = levels[j];
1697                                 q.out.info = &info;
1698                                 status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1699                                 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1700                                         printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1701                                                levels[j], nt_errstr(status));
1702                                         ret = false;
1703                                 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1704                                         printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n", 
1705                                                levels[j], nt_errstr(status));
1706                                         ret = false;
1707                                 }
1708                         }
1709                         
1710                         status = dcerpc_lsa_CloseTrustedDomainEx(p, mem_ctx, &c_trust);
1711                         if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1712                                 printf("Expected CloseTrustedDomainEx to return NT_STATUS_NOT_IMPLEMENTED, instead - %s\n", nt_errstr(status));
1713                                 return false;
1714                         }
1715                         
1716                         c.in.handle = &trustdom_handle;
1717                         c.out.handle = &handle2;
1718                         
1719                         status = dcerpc_lsa_Close(p, mem_ctx, &c);
1720                         if (!NT_STATUS_IS_OK(status)) {
1721                                 printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1722                                 return false;
1723                         }
1724
1725                         for (j=0; j < ARRAY_SIZE(levels); j++) {
1726                                 struct lsa_QueryTrustedDomainInfoBySid q;
1727                                 union lsa_TrustedDomainInfo info;
1728                                 
1729                                 if (!domains->domains[i].sid) {
1730                                         continue;
1731                                 }
1732                                 
1733                                 q.in.handle  = handle;
1734                                 q.in.dom_sid = domains->domains[i].sid;
1735                                 q.in.level   = levels[j];
1736                                 q.out.info   = &info;
1737                                 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1738                                 if (!NT_STATUS_IS_OK(status) && ok[j]) {
1739                                         printf("QueryTrustedDomainInfoBySid level %d failed - %s\n", 
1740                                                levels[j], nt_errstr(status));
1741                                         ret = false;
1742                                 } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1743                                         printf("QueryTrustedDomainInfoBySid level %d unexpectedly succeeded - %s\n", 
1744                                                levels[j], nt_errstr(status));
1745                                         ret = false;
1746                                 }
1747                         }
1748                 }
1749
1750                 trust_by_name.in.handle = handle;
1751                 trust_by_name.in.name.string = domains->domains[i].name.string;
1752                 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1753                 trust_by_name.out.trustdom_handle = &trustdom_handle;
1754                         
1755                 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1756                         
1757                 if (!NT_STATUS_IS_OK(status)) {
1758                         printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1759                         return false;
1760                 }
1761
1762                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1763                         struct lsa_QueryTrustedDomainInfo q;
1764                         union lsa_TrustedDomainInfo info;
1765                         q.in.trustdom_handle = &trustdom_handle;
1766                         q.in.level = levels[j];
1767                         q.out.info = &info;
1768                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1769                         if (!NT_STATUS_IS_OK(status) && ok[j]) {
1770                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1771                                        levels[j], nt_errstr(status));
1772                                 ret = false;
1773                         } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1774                                 printf("QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n", 
1775                                        levels[j], nt_errstr(status));
1776                                 ret = false;
1777                         }
1778                 }
1779                 
1780                 c.in.handle = &trustdom_handle;
1781                 c.out.handle = &handle2;
1782
1783                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1784                 if (!NT_STATUS_IS_OK(status)) {
1785                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1786                         return false;
1787                 }
1788
1789                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1790                         struct lsa_QueryTrustedDomainInfoByName q;
1791                         union lsa_TrustedDomainInfo info;
1792                         q.in.handle         = handle;
1793                         q.in.trusted_domain.string = domains->domains[i].name.string;
1794                         q.in.level          = levels[j];
1795                         q.out.info          = &info;
1796                         status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1797                         if (!NT_STATUS_IS_OK(status) && ok[j]) {
1798                                 printf("QueryTrustedDomainInfoByName level %d failed - %s\n", 
1799                                        levels[j], nt_errstr(status));
1800                                 ret = false;
1801                         } else if (NT_STATUS_IS_OK(status) && !ok[j]) {
1802                                 printf("QueryTrustedDomainInfoByName level %d unexpectedly succeeded - %s\n", 
1803                                        levels[j], nt_errstr(status));
1804                                 ret = false;
1805                         }
1806                 }
1807         }
1808         return ret;
1809 }
1810
1811 static bool test_EnumTrustDom(struct dcerpc_pipe *p, 
1812                               TALLOC_CTX *mem_ctx, 
1813                               struct policy_handle *handle)
1814 {
1815         struct lsa_EnumTrustDom r;
1816         struct lsa_EnumTrustedDomainsEx r_ex;
1817         NTSTATUS enum_status;
1818         uint32_t resume_handle = 0;
1819         struct lsa_DomainList domains;
1820         struct lsa_DomainListEx domains_ex;
1821         bool ret = true;
1822
1823         printf("\nTesting EnumTrustDom\n");
1824
1825         r.in.handle = handle;
1826         r.in.resume_handle = &resume_handle;
1827         r.in.max_size = 0;
1828         r.out.domains = &domains;
1829         r.out.resume_handle = &resume_handle;
1830         
1831         enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1832         
1833         if (NT_STATUS_IS_OK(enum_status)) {
1834                 if (domains.count == 0) {
1835                         printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1836                         return false;
1837                 }
1838         } else if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1839                 printf("EnumTrustDom of zero size failed - %s\n", nt_errstr(enum_status));
1840                 return false;
1841         }
1842                 
1843         do {
1844                 r.in.handle = handle;
1845                 r.in.resume_handle = &resume_handle;
1846                 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
1847                 r.out.domains = &domains;
1848                 r.out.resume_handle = &resume_handle;
1849                 
1850                 enum_status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1851                 
1852                 /* NO_MORE_ENTRIES is allowed */
1853                 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1854                         return true;
1855                 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1856                         /* Windows 2003 gets this off by one on the first run */
1857                         if (r.out.domains->count < 3 || r.out.domains->count > 4) {
1858                                 printf("EnumTrustDom didn't fill the buffer we "
1859                                        "asked it to (got %d, expected %d / %d == %d entries)\n",
1860                                        r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3, 
1861                                        LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
1862                                 ret = false;
1863                         }
1864                 } else if (!NT_STATUS_IS_OK(enum_status)) {
1865                         printf("EnumTrustDom failed - %s\n", nt_errstr(enum_status));
1866                         return false;
1867                 }
1868                 
1869                 if (domains.count == 0) {
1870                         printf("EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1871                         return false;
1872                 }
1873
1874                 ret &= test_query_each_TrustDom(p, mem_ctx, handle, &domains);
1875                 
1876         } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1877
1878         printf("\nTesting EnumTrustedDomainsEx\n");
1879
1880         r_ex.in.handle = handle;
1881         r_ex.in.resume_handle = &resume_handle;
1882         r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1883         r_ex.out.domains = &domains_ex;
1884         r_ex.out.resume_handle = &resume_handle;
1885         
1886         enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1887         
1888         if (!(NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES))) {
1889                 printf("EnumTrustedDomainEx of zero size failed - %s\n", nt_errstr(enum_status));
1890                 return false;
1891         }
1892                 
1893         resume_handle = 0;
1894         do {
1895                 r_ex.in.handle = handle;
1896                 r_ex.in.resume_handle = &resume_handle;
1897                 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
1898                 r_ex.out.domains = &domains_ex;
1899                 r_ex.out.resume_handle = &resume_handle;
1900                 
1901                 enum_status = dcerpc_lsa_EnumTrustedDomainsEx(p, mem_ctx, &r_ex);
1902                 
1903                 /* NO_MORE_ENTRIES is allowed */
1904                 if (NT_STATUS_EQUAL(enum_status, NT_STATUS_NO_MORE_ENTRIES)) {
1905                         return true;
1906                 } else if (NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)) {
1907                         /* Windows 2003 gets this off by one on the first run */
1908                         if (r_ex.out.domains->count < 3 || r_ex.out.domains->count > 4) {
1909                                 printf("EnumTrustDom didn't fill the buffer we "
1910                                        "asked it to (got %d, expected %d / %d == %d entries)\n",
1911                                        r_ex.out.domains->count, 
1912                                        r_ex.in.max_size,
1913                                        LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER, 
1914                                        r_ex.in.max_size / LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER);
1915                                 ret = false;
1916                                 exit(1);
1917                         }
1918                 } else if (!NT_STATUS_IS_OK(enum_status)) {
1919                         printf("EnumTrustedDomainEx failed - %s\n", nt_errstr(enum_status));
1920                         return false;
1921                 }
1922
1923                 if (domains_ex.count == 0) {
1924                         printf("EnumTrustDomainEx failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
1925                         return false;
1926                 }
1927
1928                 ret &= test_query_each_TrustDomEx(p, mem_ctx, handle, &domains_ex);
1929                 
1930         } while ((NT_STATUS_EQUAL(enum_status, STATUS_MORE_ENTRIES)));
1931
1932         return ret;
1933 }
1934
1935 static bool test_CreateTrustedDomain(struct dcerpc_pipe *p, 
1936                                      TALLOC_CTX *mem_ctx, 
1937                                      struct policy_handle *handle)
1938 {
1939         NTSTATUS status;
1940         bool ret = true;
1941         struct lsa_CreateTrustedDomain r;
1942         struct lsa_DomainInfo trustinfo;
1943         struct dom_sid *domsid[12];
1944         struct policy_handle trustdom_handle[12];
1945         struct lsa_QueryTrustedDomainInfo q;
1946         int i;
1947
1948         printf("Testing CreateTrustedDomain for 12 domains\n");
1949
1950         if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1951                 ret = false;
1952         }
1953         
1954         for (i=0; i< 12; i++) {
1955                 char *trust_name = talloc_asprintf(mem_ctx, "torturedom%02d", i);
1956                 char *trust_sid = talloc_asprintf(mem_ctx, "S-1-5-21-97398-379795-100%02d", i);
1957                 
1958                 domsid[i] = dom_sid_parse_talloc(mem_ctx, trust_sid);
1959
1960                 trustinfo.sid = domsid[i];
1961                 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
1962
1963                 r.in.handle = handle;
1964                 r.in.info = &trustinfo;
1965                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1966                 r.out.trustdom_handle = &trustdom_handle[i];
1967                 
1968                 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1969                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1970                         test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1971                         status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1972                 }
1973                 if (!NT_STATUS_IS_OK(status)) {
1974                         printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1975                         ret = false;
1976                 } else {
1977                 
1978                         q.in.trustdom_handle = &trustdom_handle[i];
1979                         q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
1980                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1981                         if (!NT_STATUS_IS_OK(status)) {
1982                                 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
1983                                 ret = false;
1984                         } else if (!q.out.info) {
1985                                 ret = false;
1986                         } else {
1987                                 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
1988                                         printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
1989                                                q.out.info->name.netbios_name.string, trustinfo.name.string);
1990                                         ret = false;
1991                                 }
1992                         }
1993                 }
1994         }
1995
1996         /* now that we have some domains to look over, we can test the enum calls */
1997         if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1998                 ret = false;
1999         }
2000         
2001         for (i=0; i<12; i++) {
2002                 if (!test_DeleteTrustedDomainBySid(p, mem_ctx, handle, domsid[i])) {
2003                         ret = false;
2004                 }
2005         }
2006
2007         return ret;
2008 }
2009
2010 static bool test_QueryDomainInfoPolicy(struct dcerpc_pipe *p, 
2011                                  struct torture_context *tctx, 
2012                                  struct policy_handle *handle)
2013 {
2014         struct lsa_QueryDomainInformationPolicy r;
2015         NTSTATUS status;
2016         int i;
2017         bool ret = true;
2018         if (torture_setting_bool(tctx, "samba4", false)) {
2019                 printf("skipping QueryDomainInformationPolicy test against Samba4\n");
2020                 return true;
2021         }
2022
2023         printf("\nTesting QueryDomainInformationPolicy\n");
2024
2025         for (i=2;i<4;i++) {
2026                 r.in.handle = handle;
2027                 r.in.level = i;
2028
2029                 printf("\ntrying QueryDomainInformationPolicy level %d\n", i);
2030
2031                 status = dcerpc_lsa_QueryDomainInformationPolicy(p, tctx, &r);
2032
2033                 if (!NT_STATUS_IS_OK(status)) {
2034                         printf("QueryDomainInformationPolicy failed - %s\n", nt_errstr(status));
2035                         ret = false;
2036                         continue;
2037                 }
2038         }
2039
2040         return ret;
2041 }
2042
2043
2044 static bool test_QueryInfoPolicy(struct dcerpc_pipe *p, 
2045                                  struct torture_context *tctx, 
2046                                  struct policy_handle *handle)
2047 {
2048         struct lsa_QueryInfoPolicy r;
2049         NTSTATUS status;
2050         int i;
2051         bool ret = true;
2052         printf("\nTesting QueryInfoPolicy\n");
2053
2054         for (i=1;i<13;i++) {
2055                 r.in.handle = handle;
2056                 r.in.level = i;
2057
2058                 printf("\ntrying QueryInfoPolicy level %d\n", i);
2059
2060                 status = dcerpc_lsa_QueryInfoPolicy(p, tctx, &r);
2061
2062                 switch (i) {
2063                 case LSA_POLICY_INFO_DB:
2064                 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2065                 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2066                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2067                                 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2068                                 ret = false;
2069                         }
2070                         break;
2071                 case LSA_POLICY_INFO_DOMAIN:
2072                 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2073                 case LSA_POLICY_INFO_DNS:
2074                         if (!NT_STATUS_IS_OK(status)) {
2075                                 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2076                                 ret = false;
2077                         }
2078                         break;
2079                 default:
2080                         if (torture_setting_bool(tctx, "samba4", false)) {
2081                                 /* Other levels not implemented yet */
2082                                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2083                                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2084                                         ret = false;
2085                                 }
2086                         } else if (!NT_STATUS_IS_OK(status)) {
2087                                 printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
2088                                 ret = false;
2089                         }
2090                         break;
2091                 }
2092
2093                 if (NT_STATUS_IS_OK(status) && i == LSA_POLICY_INFO_DNS) {
2094                         /* Let's look up some of these names */
2095
2096                         struct lsa_TransNameArray tnames;
2097                         tnames.count = 14;
2098                         tnames.names = talloc_zero_array(tctx, struct lsa_TranslatedName, tnames.count);
2099                         tnames.names[0].name.string = r.out.info->dns.name.string;
2100                         tnames.names[0].sid_type = SID_NAME_DOMAIN;
2101                         tnames.names[1].name.string = r.out.info->dns.dns_domain.string;
2102                         tnames.names[1].sid_type = SID_NAME_DOMAIN;
2103                         tnames.names[2].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.name.string);
2104                         tnames.names[2].sid_type = SID_NAME_DOMAIN;
2105                         tnames.names[3].name.string = talloc_asprintf(tctx, "%s\\", r.out.info->dns.dns_domain.string);
2106                         tnames.names[3].sid_type = SID_NAME_DOMAIN;
2107                         tnames.names[4].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.name.string);
2108                         tnames.names[4].sid_type = SID_NAME_USER;
2109                         tnames.names[5].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.name.string);
2110                         tnames.names[5].sid_type = SID_NAME_USER;
2111                         tnames.names[6].name.string = talloc_asprintf(tctx, "%s\\guest", r.out.info->dns.dns_domain.string);
2112                         tnames.names[6].sid_type = SID_NAME_USER;
2113                         tnames.names[7].name.string = talloc_asprintf(tctx, "%s\\krbtgt", r.out.info->dns.dns_domain.string);
2114                         tnames.names[7].sid_type = SID_NAME_USER;
2115                         tnames.names[8].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.name.string);
2116                         tnames.names[8].sid_type = SID_NAME_USER;
2117                         tnames.names[9].name.string = talloc_asprintf(tctx, "krbtgt@%s", r.out.info->dns.dns_domain.string);
2118                         tnames.names[9].sid_type = SID_NAME_USER;
2119                         tnames.names[10].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.name.string);
2120                         tnames.names[10].sid_type = SID_NAME_USER;
2121                         tnames.names[11].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", r.out.info->dns.dns_domain.string);
2122                         tnames.names[11].sid_type = SID_NAME_USER;
2123                         tnames.names[12].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.name.string);
2124                         tnames.names[12].sid_type = SID_NAME_USER;
2125                         tnames.names[13].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", r.out.info->dns.dns_domain.string);
2126                         tnames.names[13].sid_type = SID_NAME_USER;
2127                         ret &= test_LookupNames(p, tctx, handle, &tnames);
2128
2129                 }
2130         }
2131
2132         return ret;
2133 }
2134
2135 static bool test_QueryInfoPolicy2(struct dcerpc_pipe *p, 
2136                                   struct torture_context *tctx, 
2137                                   struct policy_handle *handle)
2138 {
2139         struct lsa_QueryInfoPolicy2 r;
2140         NTSTATUS status;
2141         int i;
2142         bool ret = true;
2143         printf("\nTesting QueryInfoPolicy2\n");
2144         for (i=1;i<13;i++) {
2145                 r.in.handle = handle;
2146                 r.in.level = i;
2147
2148                 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
2149
2150                 status = dcerpc_lsa_QueryInfoPolicy2(p, tctx, &r);
2151                 
2152                 switch (i) {
2153                 case LSA_POLICY_INFO_DB:
2154                 case LSA_POLICY_INFO_AUDIT_FULL_SET:
2155                 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
2156                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2157                                 printf("server should have failed level %u: %s\n", i, nt_errstr(status));
2158                                 ret = false;
2159                         }
2160                         break;
2161                 case LSA_POLICY_INFO_DOMAIN:
2162                 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
2163                 case LSA_POLICY_INFO_DNS:
2164                         if (!NT_STATUS_IS_OK(status)) {
2165                                 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2166                                 ret = false;
2167                         }
2168                         break;
2169                 default:
2170                         if (torture_setting_bool(tctx, "samba4", false)) {
2171                                 /* Other levels not implemented yet */
2172                                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
2173                                         printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2174                                         ret = false;
2175                                 }
2176                         } else if (!NT_STATUS_IS_OK(status)) {
2177                                 printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
2178                                 ret = false;
2179                         }
2180                         break;
2181                 }
2182         }
2183
2184         return ret;
2185 }
2186
2187 static bool test_GetUserName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
2188 {
2189         struct lsa_GetUserName r;
2190         NTSTATUS status;
2191         bool ret = true;
2192         struct lsa_StringPointer authority_name_p;
2193
2194         printf("\nTesting GetUserName\n");
2195
2196         r.in.system_name = "\\";
2197         r.in.account_name = NULL;
2198         r.in.authority_name = &authority_name_p;
2199         authority_name_p.string = NULL;
2200
2201         status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
2202
2203         if (!NT_STATUS_IS_OK(status)) {
2204                 printf("GetUserName failed - %s\n", nt_errstr(status));
2205                 ret = false;
2206         }
2207
2208         return ret;
2209 }
2210
2211 bool test_lsa_Close(struct dcerpc_pipe *p, 
2212                     TALLOC_CTX *mem_ctx, 
2213                     struct policy_handle *handle)
2214 {
2215         NTSTATUS status;
2216         struct lsa_Close r;
2217         struct policy_handle handle2;
2218
2219         printf("\ntesting Close\n");
2220
2221         r.in.handle = handle;
2222         r.out.handle = &handle2;
2223
2224         status = dcerpc_lsa_Close(p, mem_ctx, &r);
2225         if (!NT_STATUS_IS_OK(status)) {
2226                 printf("Close failed - %s\n", nt_errstr(status));
2227                 return false;
2228         }
2229
2230         status = dcerpc_lsa_Close(p, mem_ctx, &r);
2231         /* its really a fault - we need a status code for rpc fault */
2232         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
2233                 printf("Close failed - %s\n", nt_errstr(status));
2234                 return false;
2235         }
2236
2237         printf("\n");
2238
2239         return true;
2240 }
2241
2242 bool torture_rpc_lsa(struct torture_context *tctx)
2243 {
2244         NTSTATUS status;
2245         struct dcerpc_pipe *p;
2246         bool ret = true;
2247         struct policy_handle *handle;
2248         struct test_join *join = NULL;
2249         struct cli_credentials *machine_creds;
2250
2251         status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
2252         if (!NT_STATUS_IS_OK(status)) {
2253                 return false;
2254         }
2255
2256         if (!test_OpenPolicy(p, tctx)) {
2257                 ret = false;
2258         }
2259
2260         if (!test_lsa_OpenPolicy2(p, tctx, &handle)) {
2261                 ret = false;
2262         }
2263
2264         if (handle) {
2265                 join = torture_join_domain(tctx, TEST_MACHINENAME, ACB_WSTRUST, &machine_creds);
2266                 if (!join) {
2267                         ret = false;
2268                 }
2269
2270                 if (!test_LookupNames_wellknown(p, tctx, handle)) {
2271                         ret = false;
2272                 }               
2273
2274                 if (!test_LookupNames_bogus(p, tctx, handle)) {
2275                         ret = false;
2276                 }               
2277
2278                 if (!test_LookupSids_async(p, tctx, handle)) {
2279                         ret = false;
2280                 }
2281
2282                 if (!test_QueryDomainInfoPolicy(p, tctx, handle)) {
2283                         ret = false;
2284                 }
2285                 
2286                 if (!test_CreateAccount(p, tctx, handle)) {
2287                         ret = false;
2288                 }
2289                 
2290                 if (!test_CreateSecret(p, tctx, handle)) {
2291                         ret = false;
2292                 }
2293                 
2294                 if (!test_CreateTrustedDomain(p, tctx, handle)) {
2295                         ret = false;
2296                 }
2297                 
2298                 if (!test_EnumAccounts(p, tctx, handle)) {
2299                         ret = false;
2300                 }
2301                 
2302                 if (!test_EnumPrivs(p, tctx, handle)) {
2303                         ret = false;
2304                 }
2305                 
2306                 if (!test_QueryInfoPolicy(p, tctx, handle)) {
2307                         ret = false;
2308                 }
2309                 
2310                 if (!test_QueryInfoPolicy2(p, tctx, handle)) {
2311                         ret = false;
2312                 }
2313                 
2314 #if 0
2315                 if (!test_Delete(p, tctx, handle)) {
2316                         ret = false;
2317                 }
2318 #endif
2319                 
2320                 if (!test_many_LookupSids(p, tctx, handle)) {
2321                         ret = false;
2322                 }
2323                 
2324                 if (!test_lsa_Close(p, tctx, handle)) {
2325                         ret = false;
2326                 }
2327                 
2328                 torture_leave_domain(tctx, join);
2329
2330         } else {
2331                 if (!test_many_LookupSids(p, tctx, handle)) {
2332                         ret = false;
2333                 }
2334         }
2335
2336         if (!test_GetUserName(p, tctx)) {
2337                 ret = false;
2338         }
2339
2340         return ret;
2341 }
2342
2343 bool torture_rpc_lsa_get_user(struct torture_context *torture)
2344 {
2345         NTSTATUS status;
2346         struct dcerpc_pipe *p;
2347         TALLOC_CTX *mem_ctx;
2348         bool ret = true;
2349
2350         mem_ctx = talloc_init("torture_rpc_lsa_get_user");
2351
2352         status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
2353         if (!NT_STATUS_IS_OK(status)) {
2354                 talloc_free(mem_ctx);
2355                 return false;
2356         }
2357
2358         if (!test_GetUserName(p, mem_ctx)) {
2359                 ret = false;
2360         }
2361                 
2362         talloc_free(mem_ctx);
2363
2364         return ret;
2365 }