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