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