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