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