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