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