edd3a0c6e5ff3e00b42faf96bae1a7aaacf39bf5
[metze/samba/wip.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 "librpc/gen_ndr/ndr_netlogon_c.h"
28 #include "lib/events/events.h"
29 #include "libcli/security/security.h"
30 #include "libcli/auth/libcli_auth.h"
31 #include "torture/rpc/torture_rpc.h"
32 #include "param/param.h"
33 #include "../lib/crypto/crypto.h"
34 #define TEST_MACHINENAME "lsatestmach"
35 #define TRUSTPW "12345678"
36
37 static void init_lsa_String(struct lsa_String *name, const char *s)
38 {
39         name->string = s;
40 }
41
42 static bool test_OpenPolicy(struct dcerpc_binding_handle *b,
43                             struct torture_context *tctx)
44 {
45         struct lsa_ObjectAttribute attr;
46         struct policy_handle handle;
47         struct lsa_QosInfo qos;
48         struct lsa_OpenPolicy r;
49         uint16_t system_name = '\\';
50
51         torture_comment(tctx, "\nTesting OpenPolicy\n");
52
53         qos.len = 0;
54         qos.impersonation_level = 2;
55         qos.context_mode = 1;
56         qos.effective_only = 0;
57
58         attr.len = 0;
59         attr.root_dir = NULL;
60         attr.object_name = NULL;
61         attr.attributes = 0;
62         attr.sec_desc = NULL;
63         attr.sec_qos = &qos;
64
65         r.in.system_name = &system_name;
66         r.in.attr = &attr;
67         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
68         r.out.handle = &handle;
69
70         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenPolicy_r(b, tctx, &r),
71                                    "OpenPolicy failed");
72
73         torture_assert_ntstatus_ok(tctx,
74                                    r.out.result,
75                                    "OpenPolicy failed");
76
77         return true;
78 }
79
80 static bool test_OpenPolicy_fail(struct dcerpc_binding_handle *b,
81                                  struct torture_context *tctx)
82 {
83         struct lsa_ObjectAttribute attr;
84         struct policy_handle handle;
85         struct lsa_QosInfo qos;
86         struct lsa_OpenPolicy r;
87         uint16_t system_name = '\\';
88         NTSTATUS status;
89
90         torture_comment(tctx, "\nTesting OpenPolicy_fail\n");
91
92         qos.len = 0;
93         qos.impersonation_level = 2;
94         qos.context_mode = 1;
95         qos.effective_only = 0;
96
97         attr.len = 0;
98         attr.root_dir = NULL;
99         attr.object_name = NULL;
100         attr.attributes = 0;
101         attr.sec_desc = NULL;
102         attr.sec_qos = &qos;
103
104         r.in.system_name = &system_name;
105         r.in.attr = &attr;
106         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
107         r.out.handle = &handle;
108
109         status = dcerpc_lsa_OpenPolicy_r(b, tctx, &r);
110         if (!NT_STATUS_IS_OK(status)) {
111                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
112                         torture_comment(tctx,
113                                         "OpenPolicy correctly returned with "
114                                         "status: %s\n",
115                                         nt_errstr(status));
116                         return true;
117                 }
118
119                 torture_assert_ntstatus_equal(tctx,
120                                               status,
121                                               NT_STATUS_ACCESS_DENIED,
122                                               "OpenPolicy return value should "
123                                               "be ACCESS_DENIED");
124                 return true;
125         }
126
127         if (!NT_STATUS_IS_OK(r.out.result)) {
128                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED) ||
129                     NT_STATUS_EQUAL(r.out.result, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
130                         torture_comment(tctx,
131                                         "OpenPolicy correctly returned with "
132                                         "result: %s\n",
133                                         nt_errstr(r.out.result));
134                         return true;
135                 }
136         }
137
138         torture_assert_ntstatus_equal(tctx,
139                                       r.out.result,
140                                       NT_STATUS_OK,
141                                       "OpenPolicy return value should be "
142                                       "ACCESS_DENIED");
143
144         return false;
145 }
146
147
148 bool test_lsa_OpenPolicy2_ex(struct dcerpc_binding_handle *b,
149                              struct torture_context *tctx,
150                              struct policy_handle **handle,
151                              NTSTATUS expected_status)
152 {
153         struct lsa_ObjectAttribute attr;
154         struct lsa_QosInfo qos;
155         struct lsa_OpenPolicy2 r;
156         NTSTATUS status;
157
158         torture_comment(tctx, "\nTesting OpenPolicy2\n");
159
160         *handle = talloc(tctx, struct policy_handle);
161         if (!*handle) {
162                 return false;
163         }
164
165         qos.len = 0;
166         qos.impersonation_level = 2;
167         qos.context_mode = 1;
168         qos.effective_only = 0;
169
170         attr.len = 0;
171         attr.root_dir = NULL;
172         attr.object_name = NULL;
173         attr.attributes = 0;
174         attr.sec_desc = NULL;
175         attr.sec_qos = &qos;
176
177         r.in.system_name = "\\";
178         r.in.attr = &attr;
179         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
180         r.out.handle = *handle;
181
182         status = dcerpc_lsa_OpenPolicy2_r(b, tctx, &r);
183         torture_assert_ntstatus_equal(tctx, status, expected_status,
184                                    "OpenPolicy2 failed");
185         if (!NT_STATUS_IS_OK(expected_status)) {
186                 return true;
187         }
188
189         torture_assert_ntstatus_ok(tctx,
190                                    r.out.result,
191                                    "OpenPolicy2 failed");
192
193         return true;
194 }
195
196
197 bool test_lsa_OpenPolicy2(struct dcerpc_binding_handle *b,
198                           struct torture_context *tctx,
199                           struct policy_handle **handle)
200 {
201         return test_lsa_OpenPolicy2_ex(b, tctx, handle, NT_STATUS_OK);
202 }
203
204 static bool test_OpenPolicy2_fail(struct dcerpc_binding_handle *b,
205                                   struct torture_context *tctx)
206 {
207         struct lsa_ObjectAttribute attr;
208         struct policy_handle handle;
209         struct lsa_QosInfo qos;
210         struct lsa_OpenPolicy2 r;
211         NTSTATUS status;
212
213         torture_comment(tctx, "\nTesting OpenPolicy2_fail\n");
214
215         qos.len = 0;
216         qos.impersonation_level = 2;
217         qos.context_mode = 1;
218         qos.effective_only = 0;
219
220         attr.len = 0;
221         attr.root_dir = NULL;
222         attr.object_name = NULL;
223         attr.attributes = 0;
224         attr.sec_desc = NULL;
225         attr.sec_qos = &qos;
226
227         r.in.system_name = "\\";
228         r.in.attr = &attr;
229         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
230         r.out.handle = &handle;
231
232         status = dcerpc_lsa_OpenPolicy2_r(b, tctx, &r);
233         if (!NT_STATUS_IS_OK(status)) {
234                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
235                         torture_comment(tctx,
236                                         "OpenPolicy2 correctly returned with "
237                                         "status: %s\n",
238                                         nt_errstr(status));
239                         return true;
240                 }
241
242                 torture_assert_ntstatus_equal(tctx,
243                                               status,
244                                               NT_STATUS_ACCESS_DENIED,
245                                               "OpenPolicy2 return value should "
246                                               "be ACCESS_DENIED");
247                 return true;
248         }
249
250         if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED) ||
251             NT_STATUS_EQUAL(r.out.result, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
252                 torture_comment(tctx,
253                                 "OpenPolicy2 correctly returned with "
254                                 "result: %s\n",
255                                 nt_errstr(r.out.result));
256                 return true;
257         }
258
259         torture_fail(tctx,
260                      "OpenPolicy2 return value should be "
261                      "ACCESS_DENIED or RPC_PROTSEQ_NOT_SUPPORTED");
262
263         return false;
264 }
265
266 static bool test_LookupNames(struct dcerpc_binding_handle *b,
267                              struct torture_context *tctx,
268                              struct policy_handle *handle,
269                              struct lsa_TransNameArray *tnames)
270 {
271         struct lsa_LookupNames r;
272         struct lsa_TransSidArray sids;
273         struct lsa_RefDomainList *domains = NULL;
274         struct lsa_String *names;
275         uint32_t count = 0;
276         int i;
277         uint32_t *input_idx;
278
279         torture_comment(tctx, "\nTesting LookupNames with %d names\n", tnames->count);
280
281         sids.count = 0;
282         sids.sids = NULL;
283
284
285         r.in.num_names = 0;
286
287         input_idx = talloc_array(tctx, uint32_t, tnames->count);
288         names = talloc_array(tctx, struct lsa_String, tnames->count);
289
290         for (i=0;i<tnames->count;i++) {
291                 if (tnames->names[i].sid_type != SID_NAME_UNKNOWN) {
292                         init_lsa_String(&names[r.in.num_names], tnames->names[i].name.string);
293                         input_idx[r.in.num_names] = i;
294                         r.in.num_names++;
295                 }
296         }
297
298         r.in.handle = handle;
299         r.in.names = names;
300         r.in.sids = &sids;
301         r.in.level = 1;
302         r.in.count = &count;
303         r.out.count = &count;
304         r.out.sids = &sids;
305         r.out.domains = &domains;
306
307         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupNames_r(b, tctx, &r),
308                                    "LookupNames failed");
309         if (NT_STATUS_EQUAL(r.out.result, STATUS_SOME_UNMAPPED) ||
310             NT_STATUS_EQUAL(r.out.result, NT_STATUS_NONE_MAPPED)) {
311                 for (i=0;i< r.in.num_names;i++) {
312                         if (i < count && sids.sids[i].sid_type == SID_NAME_UNKNOWN) {
313                                 torture_comment(tctx, "LookupName of %s was unmapped\n",
314                                        tnames->names[i].name.string);
315                         } else if (i >=count) {
316                                 torture_comment(tctx, "LookupName of %s failed to return a result\n",
317                                        tnames->names[i].name.string);
318                         }
319                 }
320                 torture_comment(tctx, "LookupNames failed - %s\n",
321                                 nt_errstr(r.out.result));
322                 return false;
323         } else if (!NT_STATUS_IS_OK(r.out.result)) {
324                 torture_comment(tctx, "LookupNames failed - %s\n",
325                                 nt_errstr(r.out.result));
326                 return false;
327         }
328
329         for (i=0;i< r.in.num_names;i++) {
330                 if (i < count) {
331                         if (sids.sids[i].sid_type != tnames->names[input_idx[i]].sid_type) {
332                                 torture_comment(tctx, "LookupName of %s got unexpected name type: %s\n",
333                                                 tnames->names[input_idx[i]].name.string,
334                                                 sid_type_lookup(sids.sids[i].sid_type));
335                                 return false;
336                         }
337                         if ((sids.sids[i].sid_type == SID_NAME_DOMAIN) &&
338                             (sids.sids[i].rid != (uint32_t)-1)) {
339                                 torture_comment(tctx, "LookupName of %s got unexpected rid: %d\n",
340                                         tnames->names[input_idx[i]].name.string, sids.sids[i].rid);
341                                 return false;
342                         }
343                 } else if (i >=count) {
344                         torture_comment(tctx, "LookupName of %s failed to return a result\n",
345                                tnames->names[input_idx[i]].name.string);
346                         return false;
347                 }
348         }
349         torture_comment(tctx, "\n");
350
351         return true;
352 }
353
354 static bool test_LookupNames_bogus(struct dcerpc_binding_handle *b,
355                                    struct torture_context *tctx,
356                                    struct policy_handle *handle)
357 {
358         struct lsa_LookupNames r;
359         struct lsa_TransSidArray sids;
360         struct lsa_RefDomainList *domains = NULL;
361         struct lsa_String names[1];
362         uint32_t count = 0;
363
364         torture_comment(tctx, "\nTesting LookupNames with bogus name\n");
365
366         sids.count = 0;
367         sids.sids = NULL;
368
369         init_lsa_String(&names[0], "NT AUTHORITY\\BOGUS");
370
371         r.in.handle = handle;
372         r.in.num_names = 1;
373         r.in.names = names;
374         r.in.sids = &sids;
375         r.in.level = 1;
376         r.in.count = &count;
377         r.out.count = &count;
378         r.out.sids = &sids;
379         r.out.domains = &domains;
380
381         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupNames_r(b, tctx, &r),
382                                    "LookupNames bogus failed");
383         if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_NONE_MAPPED)) {
384                 torture_comment(tctx, "LookupNames failed - %s\n",
385                                 nt_errstr(r.out.result));
386                 return false;
387         }
388
389         torture_comment(tctx, "\n");
390
391         return true;
392 }
393
394 static bool test_LookupNames_NULL(struct dcerpc_binding_handle *b,
395                                   struct torture_context *tctx,
396                                   struct policy_handle *handle)
397 {
398         struct lsa_LookupNames r;
399         struct lsa_TransSidArray sids;
400         struct lsa_RefDomainList *domains = NULL;
401         struct lsa_String names[1];
402         uint32_t count = 0;
403
404         torture_comment(tctx, "\nTesting LookupNames with NULL name\n");
405
406         sids.count = 0;
407         sids.sids = NULL;
408
409         names[0].string = NULL;
410
411         r.in.handle = handle;
412         r.in.num_names = 1;
413         r.in.names = names;
414         r.in.sids = &sids;
415         r.in.level = 1;
416         r.in.count = &count;
417         r.out.count = &count;
418         r.out.sids = &sids;
419         r.out.domains = &domains;
420
421         /* nt4 returns NT_STATUS_NONE_MAPPED with sid_type
422          * SID_NAME_UNKNOWN, rid 0, and sid_index -1;
423          *
424          * w2k3/w2k8 return NT_STATUS_OK with sid_type
425          * SID_NAME_DOMAIN, rid -1 and sid_index 0 and BUILTIN domain
426          */
427
428         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupNames_r(b, tctx, &r),
429                 "LookupNames with NULL name failed");
430         torture_assert_ntstatus_ok(tctx, r.out.result,
431                 "LookupNames with NULL name failed");
432
433         torture_comment(tctx, "\n");
434
435         return true;
436 }
437
438 static bool test_LookupNames_wellknown(struct dcerpc_binding_handle *b,
439                                        struct torture_context *tctx,
440                                        struct policy_handle *handle)
441 {
442         struct lsa_TranslatedName name;
443         struct lsa_TransNameArray tnames;
444         bool ret = true;
445
446         torture_comment(tctx, "Testing LookupNames with well known names\n");
447
448         tnames.names = &name;
449         tnames.count = 1;
450         name.name.string = "NT AUTHORITY\\SYSTEM";
451         name.sid_type = SID_NAME_WKN_GRP;
452         ret &= test_LookupNames(b, tctx, handle, &tnames);
453
454         name.name.string = "NT AUTHORITY\\ANONYMOUS LOGON";
455         name.sid_type = SID_NAME_WKN_GRP;
456         ret &= test_LookupNames(b, tctx, handle, &tnames);
457
458         name.name.string = "NT AUTHORITY\\Authenticated Users";
459         name.sid_type = SID_NAME_WKN_GRP;
460         ret &= test_LookupNames(b, tctx, handle, &tnames);
461
462 #if 0
463         name.name.string = "NT AUTHORITY";
464         ret &= test_LookupNames(b, tctx, handle, &tnames);
465
466         name.name.string = "NT AUTHORITY\\";
467         ret &= test_LookupNames(b, tctx, handle, &tnames);
468 #endif
469
470         name.name.string = "BUILTIN\\";
471         name.sid_type = SID_NAME_DOMAIN;
472         ret &= test_LookupNames(b, tctx, handle, &tnames);
473
474         name.name.string = "BUILTIN\\Administrators";
475         name.sid_type = SID_NAME_ALIAS;
476         ret &= test_LookupNames(b, tctx, handle, &tnames);
477
478         name.name.string = "SYSTEM";
479         name.sid_type = SID_NAME_WKN_GRP;
480         ret &= test_LookupNames(b, tctx, handle, &tnames);
481
482         name.name.string = "Everyone";
483         name.sid_type = SID_NAME_WKN_GRP;
484         ret &= test_LookupNames(b, tctx, handle, &tnames);
485         return ret;
486 }
487
488 static bool test_LookupNames2(struct dcerpc_binding_handle *b,
489                               struct torture_context *tctx,
490                               struct policy_handle *handle,
491                               struct lsa_TransNameArray2 *tnames,
492                               bool check_result)
493 {
494         struct lsa_LookupNames2 r;
495         struct lsa_TransSidArray2 sids;
496         struct lsa_RefDomainList *domains = NULL;
497         struct lsa_String *names;
498         uint32_t *input_idx;
499         uint32_t count = 0;
500         int i;
501
502         torture_comment(tctx, "\nTesting LookupNames2 with %d names\n", tnames->count);
503
504         sids.count = 0;
505         sids.sids = NULL;
506
507         r.in.num_names = 0;
508
509         input_idx = talloc_array(tctx, uint32_t, tnames->count);
510         names = talloc_array(tctx, struct lsa_String, tnames->count);
511
512         for (i=0;i<tnames->count;i++) {
513                 if (tnames->names[i].sid_type != SID_NAME_UNKNOWN) {
514                         init_lsa_String(&names[r.in.num_names], tnames->names[i].name.string);
515                         input_idx[r.in.num_names] = i;
516                         r.in.num_names++;
517                 }
518         }
519
520         r.in.handle = handle;
521         r.in.names = names;
522         r.in.sids = &sids;
523         r.in.level = 1;
524         r.in.count = &count;
525         r.in.lookup_options = 0;
526         r.in.client_revision = 0;
527         r.out.count = &count;
528         r.out.sids = &sids;
529         r.out.domains = &domains;
530
531         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupNames2_r(b, tctx, &r),
532                 "LookupNames2 failed");
533         torture_assert_ntstatus_ok(tctx, r.out.result, "LookupNames2 failed");
534
535         if (check_result) {
536                 torture_assert_int_equal(tctx, count, sids.count,
537                         "unexpected number of results returned");
538                 if (sids.count > 0) {
539                         torture_assert(tctx, sids.sids, "invalid sid buffer");
540                 }
541         }
542
543         torture_comment(tctx, "\n");
544
545         return true;
546 }
547
548
549 static bool test_LookupNames3(struct dcerpc_binding_handle *b,
550                               struct torture_context *tctx,
551                               struct policy_handle *handle,
552                               struct lsa_TransNameArray2 *tnames,
553                               bool check_result)
554 {
555         struct lsa_LookupNames3 r;
556         struct lsa_TransSidArray3 sids;
557         struct lsa_RefDomainList *domains = NULL;
558         struct lsa_String *names;
559         uint32_t count = 0;
560         int i;
561         uint32_t *input_idx;
562
563         torture_comment(tctx, "\nTesting LookupNames3 with %d names\n", tnames->count);
564
565         sids.count = 0;
566         sids.sids = NULL;
567
568         r.in.num_names = 0;
569
570         input_idx = talloc_array(tctx, uint32_t, tnames->count);
571         names = talloc_array(tctx, struct lsa_String, tnames->count);
572         for (i=0;i<tnames->count;i++) {
573                 if (tnames->names[i].sid_type != SID_NAME_UNKNOWN) {
574                         init_lsa_String(&names[r.in.num_names], tnames->names[i].name.string);
575                         input_idx[r.in.num_names] = i;
576                         r.in.num_names++;
577                 }
578         }
579
580         r.in.handle = handle;
581         r.in.names = names;
582         r.in.sids = &sids;
583         r.in.level = 1;
584         r.in.count = &count;
585         r.in.lookup_options = 0;
586         r.in.client_revision = 0;
587         r.out.count = &count;
588         r.out.sids = &sids;
589         r.out.domains = &domains;
590
591         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupNames3_r(b, tctx, &r),
592                 "LookupNames3 failed");
593         torture_assert_ntstatus_ok(tctx, r.out.result,
594                 "LookupNames3 failed");
595
596         if (check_result) {
597                 torture_assert_int_equal(tctx, count, sids.count,
598                         "unexpected number of results returned");
599                 if (sids.count > 0) {
600                         torture_assert(tctx, sids.sids, "invalid sid buffer");
601                 }
602         }
603
604         torture_comment(tctx, "\n");
605
606         return true;
607 }
608
609 static bool test_LookupNames4(struct dcerpc_binding_handle *b,
610                               struct torture_context *tctx,
611                               struct lsa_TransNameArray2 *tnames,
612                               bool check_result)
613 {
614         struct lsa_LookupNames4 r;
615         struct lsa_TransSidArray3 sids;
616         struct lsa_RefDomainList *domains = NULL;
617         struct lsa_String *names;
618         uint32_t count = 0;
619         int i;
620         uint32_t *input_idx;
621
622         torture_comment(tctx, "\nTesting LookupNames4 with %d names\n", tnames->count);
623
624         sids.count = 0;
625         sids.sids = NULL;
626
627         r.in.num_names = 0;
628
629         input_idx = talloc_array(tctx, uint32_t, tnames->count);
630         names = talloc_array(tctx, struct lsa_String, tnames->count);
631         for (i=0;i<tnames->count;i++) {
632                 if (tnames->names[i].sid_type != SID_NAME_UNKNOWN) {
633                         init_lsa_String(&names[r.in.num_names], tnames->names[i].name.string);
634                         input_idx[r.in.num_names] = i;
635                         r.in.num_names++;
636                 }
637         }
638
639         r.in.num_names = tnames->count;
640         r.in.names = names;
641         r.in.sids = &sids;
642         r.in.level = 1;
643         r.in.count = &count;
644         r.in.lookup_options = 0;
645         r.in.client_revision = 0;
646         r.out.count = &count;
647         r.out.sids = &sids;
648         r.out.domains = &domains;
649
650         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupNames4_r(b, tctx, &r),
651                 "LookupNames4 failed");
652
653         if (!NT_STATUS_IS_OK(r.out.result)) {
654                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_NONE_MAPPED)) {
655                         torture_comment(tctx,
656                                         "LookupNames4 failed: %s - not considered as an error",
657                                         nt_errstr(r.out.result));
658
659                         return true;
660                 }
661         }
662         torture_assert_ntstatus_ok(tctx,
663                                    r.out.result,
664                                    "LookupNames4 failed");
665
666         if (check_result) {
667                 torture_assert_int_equal(tctx, count, sids.count,
668                         "unexpected number of results returned");
669                 if (sids.count > 0) {
670                         torture_assert(tctx, sids.sids, "invalid sid buffer");
671                 }
672         }
673
674         torture_comment(tctx, "\n");
675
676         return true;
677 }
678
679 static bool test_LookupNames4_fail(struct dcerpc_binding_handle *b,
680                                    struct torture_context *tctx)
681 {
682         struct lsa_LookupNames4 r;
683         struct lsa_TransSidArray3 sids;
684         struct lsa_RefDomainList *domains = NULL;
685         struct lsa_String *names = NULL;
686         uint32_t count = 0;
687         NTSTATUS status;
688
689         torture_comment(tctx, "\nTesting LookupNames4_fail");
690
691         sids.count = 0;
692         sids.sids = NULL;
693
694         r.in.num_names = 0;
695
696         r.in.num_names = count;
697         r.in.names = names;
698         r.in.sids = &sids;
699         r.in.level = 1;
700         r.in.count = &count;
701         r.in.lookup_options = 0;
702         r.in.client_revision = 0;
703         r.out.count = &count;
704         r.out.sids = &sids;
705         r.out.domains = &domains;
706
707         status = dcerpc_lsa_LookupNames4_r(b, tctx, &r);
708         if (!NT_STATUS_IS_OK(status)) {
709                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
710                         torture_comment(tctx,
711                                         "LookupNames4 correctly returned with "
712                                         "status: %s\n",
713                                         nt_errstr(status));
714                         return true;
715                 }
716
717                 torture_assert_ntstatus_equal(tctx,
718                                               status,
719                                               NT_STATUS_ACCESS_DENIED,
720                                               "LookupNames4 return value should "
721                                               "be ACCESS_DENIED");
722                 return true;
723         }
724
725         if (!NT_STATUS_IS_OK(r.out.result)) {
726                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED) ||
727                     NT_STATUS_EQUAL(r.out.result, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
728                         torture_comment(tctx,
729                                         "LookupSids3 correctly returned with "
730                                         "result: %s\n",
731                                         nt_errstr(r.out.result));
732                         return true;
733                 }
734         }
735
736         torture_fail(tctx,
737                      "LookupNames4 return value should be "
738                      "ACCESS_DENIED or RPC_PROTSEQ_NOT_SUPPORTED");
739
740         return false;
741 }
742
743
744 static bool test_LookupSids(struct dcerpc_binding_handle *b,
745                             struct torture_context *tctx,
746                             struct policy_handle *handle,
747                             struct lsa_SidArray *sids)
748 {
749         struct lsa_LookupSids r;
750         struct lsa_TransNameArray names;
751         struct lsa_RefDomainList *domains = NULL;
752         uint32_t count = sids->num_sids;
753
754         torture_comment(tctx, "\nTesting LookupSids\n");
755
756         names.count = 0;
757         names.names = NULL;
758
759         r.in.handle = handle;
760         r.in.sids = sids;
761         r.in.names = &names;
762         r.in.level = 1;
763         r.in.count = &count;
764         r.out.count = &count;
765         r.out.names = &names;
766         r.out.domains = &domains;
767
768         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupSids_r(b, tctx, &r),
769                 "LookupSids failed");
770         if (!NT_STATUS_IS_OK(r.out.result) &&
771             !NT_STATUS_EQUAL(r.out.result, STATUS_SOME_UNMAPPED)) {
772                 torture_comment(tctx, "LookupSids failed - %s\n",
773                                 nt_errstr(r.out.result));
774                 return false;
775         }
776
777         torture_comment(tctx, "\n");
778
779         if (!test_LookupNames(b, tctx, handle, &names)) {
780                 return false;
781         }
782
783         return true;
784 }
785
786
787 static bool test_LookupSids2(struct dcerpc_binding_handle *b,
788                             struct torture_context *tctx,
789                             struct policy_handle *handle,
790                             struct lsa_SidArray *sids)
791 {
792         struct lsa_LookupSids2 r;
793         struct lsa_TransNameArray2 names;
794         struct lsa_RefDomainList *domains = NULL;
795         uint32_t count = sids->num_sids;
796
797         torture_comment(tctx, "\nTesting LookupSids2\n");
798
799         names.count = 0;
800         names.names = NULL;
801
802         r.in.handle = handle;
803         r.in.sids = sids;
804         r.in.names = &names;
805         r.in.level = 1;
806         r.in.count = &count;
807         r.in.lookup_options = 0;
808         r.in.client_revision = 0;
809         r.out.count = &count;
810         r.out.names = &names;
811         r.out.domains = &domains;
812
813         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupSids2_r(b, tctx, &r),
814                 "LookupSids2 failed");
815         if (!NT_STATUS_IS_OK(r.out.result) &&
816             !NT_STATUS_EQUAL(r.out.result, STATUS_SOME_UNMAPPED)) {
817                 torture_comment(tctx, "LookupSids2 failed - %s\n",
818                                 nt_errstr(r.out.result));
819                 return false;
820         }
821
822         torture_comment(tctx, "\n");
823
824         if (!test_LookupNames2(b, tctx, handle, &names, false)) {
825                 return false;
826         }
827
828         if (!test_LookupNames3(b, tctx, handle, &names, false)) {
829                 return false;
830         }
831
832         return true;
833 }
834
835 static bool test_LookupSids3(struct dcerpc_binding_handle *b,
836                             struct torture_context *tctx,
837                             struct lsa_SidArray *sids)
838 {
839         struct lsa_LookupSids3 r;
840         struct lsa_TransNameArray2 names;
841         struct lsa_RefDomainList *domains = NULL;
842         uint32_t count = sids->num_sids;
843
844         torture_comment(tctx, "\nTesting LookupSids3\n");
845
846         names.count = 0;
847         names.names = NULL;
848
849         r.in.sids = sids;
850         r.in.names = &names;
851         r.in.level = 1;
852         r.in.count = &count;
853         r.in.lookup_options = 0;
854         r.in.client_revision = 0;
855         r.out.domains = &domains;
856         r.out.count = &count;
857         r.out.names = &names;
858
859         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupSids3_r(b, tctx, &r),
860                 "LookupSids3 failed");
861
862         if (!NT_STATUS_IS_OK(r.out.result)) {
863                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_NONE_MAPPED)) {
864                         torture_comment(tctx,
865                                         "LookupSids3 failed: %s - not considered as an error",
866                                         nt_errstr(r.out.result));
867
868                         return true;
869                 }
870
871                 torture_assert_ntstatus_ok(tctx,
872                                            r.out.result,
873                                            "LookupSids3 failed");
874
875                 return false;
876         }
877
878         torture_comment(tctx, "\n");
879
880         if (!test_LookupNames4(b, tctx, &names, true)) {
881                 return false;
882         }
883
884         return true;
885 }
886
887 static bool test_LookupSids3_fail(struct dcerpc_binding_handle *b,
888                                   struct torture_context *tctx,
889                                   struct lsa_SidArray *sids)
890 {
891         struct lsa_LookupSids3 r;
892         struct lsa_TransNameArray2 names;
893         struct lsa_RefDomainList *domains = NULL;
894         uint32_t count = sids->num_sids;
895         NTSTATUS status;
896
897         torture_comment(tctx, "\nTesting LookupSids3\n");
898
899         names.count = 0;
900         names.names = NULL;
901
902         r.in.sids = sids;
903         r.in.names = &names;
904         r.in.level = 1;
905         r.in.count = &count;
906         r.in.lookup_options = 0;
907         r.in.client_revision = 0;
908         r.out.domains = &domains;
909         r.out.count = &count;
910         r.out.names = &names;
911
912         status = dcerpc_lsa_LookupSids3_r(b, tctx, &r);
913         if (!NT_STATUS_IS_OK(status)) {
914                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
915                         torture_comment(tctx,
916                                         "LookupSids3 correctly returned with "
917                                         "status: %s\n",
918                                         nt_errstr(status));
919                         return true;
920                 }
921
922                 torture_assert_ntstatus_equal(tctx,
923                                               status,
924                                               NT_STATUS_ACCESS_DENIED,
925                                               "LookupSids3 return value should "
926                                               "be ACCESS_DENIED");
927                 return true;
928         }
929
930         if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED) ||
931             NT_STATUS_EQUAL(r.out.result, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
932                 torture_comment(tctx,
933                                 "LookupNames4 correctly returned with "
934                                 "result: %s\n",
935                                 nt_errstr(r.out.result));
936                 return true;
937         }
938
939         torture_fail(tctx,
940                      "LookupSids3 return value should be "
941                      "ACCESS_DENIED or RPC_PROTSEQ_NOT_SUPPORTED");
942
943         return false;
944 }
945
946 bool test_many_LookupSids(struct dcerpc_pipe *p,
947                           struct torture_context *tctx,
948                           struct policy_handle *handle)
949 {
950         uint32_t count;
951         struct lsa_SidArray sids;
952         int i;
953         struct dcerpc_binding_handle *b = p->binding_handle;
954
955         torture_comment(tctx, "\nTesting LookupSids with lots of SIDs\n");
956
957         sids.num_sids = 100;
958
959         sids.sids = talloc_array(tctx, struct lsa_SidPtr, sids.num_sids);
960
961         for (i=0; i<sids.num_sids; i++) {
962                 const char *sidstr = "S-1-5-32-545";
963                 sids.sids[i].sid = dom_sid_parse_talloc(tctx, sidstr);
964         }
965
966         count = sids.num_sids;
967
968         if (handle) {
969                 struct lsa_LookupSids r;
970                 struct lsa_TransNameArray names;
971                 struct lsa_RefDomainList *domains = NULL;
972                 names.count = 0;
973                 names.names = NULL;
974
975                 r.in.handle = handle;
976                 r.in.sids = &sids;
977                 r.in.names = &names;
978                 r.in.level = 1;
979                 r.in.count = &names.count;
980                 r.out.count = &count;
981                 r.out.names = &names;
982                 r.out.domains = &domains;
983
984                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupSids_r(b, tctx, &r),
985                         "LookupSids failed");
986                 if (!NT_STATUS_IS_OK(r.out.result)) {
987                         torture_comment(tctx, "LookupSids failed - %s\n",
988                                         nt_errstr(r.out.result));
989                         return false;
990                 }
991
992                 torture_comment(tctx, "\n");
993
994                 if (!test_LookupNames(b, tctx, handle, &names)) {
995                         return false;
996                 }
997         }
998
999         if (p->binding->transport == NCACN_NP) {
1000                 if (!test_LookupSids3_fail(b, tctx, &sids)) {
1001                         return false;
1002                 }
1003                 if (!test_LookupNames4_fail(b, tctx)) {
1004                         return false;
1005                 }
1006         } else if (p->binding->transport == NCACN_IP_TCP) {
1007                 struct lsa_TransNameArray2 names;
1008
1009                 names.count = 0;
1010                 names.names = NULL;
1011
1012                 if (p->conn->security_state.auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL &&
1013                    p->conn->security_state.auth_info->auth_level >= DCERPC_AUTH_LEVEL_INTEGRITY) {
1014                         if (!test_LookupSids3(b, tctx, &sids)) {
1015                                 return false;
1016                         }
1017                         if (!test_LookupNames4(b, tctx, &names, true)) {
1018                                 return false;
1019                         }
1020                 } else {
1021                         /*
1022                          * If we don't have a secure channel these tests must
1023                          * fail with ACCESS_DENIED.
1024                          */
1025                         if (!test_LookupSids3_fail(b, tctx, &sids)) {
1026                                 return false;
1027                         }
1028                         if (!test_LookupNames4_fail(b, tctx)) {
1029                                 return false;
1030                         }
1031                 }
1032         }
1033
1034         torture_comment(tctx, "\n");
1035
1036
1037
1038         return true;
1039 }
1040
1041 static void lookupsids_cb(struct tevent_req *subreq)
1042 {
1043         int *replies = (int *)tevent_req_callback_data_void(subreq);
1044         NTSTATUS status;
1045
1046         status = dcerpc_lsa_LookupSids_r_recv(subreq, subreq);
1047         TALLOC_FREE(subreq);
1048         if (!NT_STATUS_IS_OK(status)) {
1049                 printf("lookupsids returned %s\n", nt_errstr(status));
1050                 *replies = -1;
1051         }
1052
1053         if (*replies >= 0) {
1054                 *replies += 1;
1055         }
1056 }
1057
1058 static bool test_LookupSids_async(struct dcerpc_binding_handle *b,
1059                                   struct torture_context *tctx,
1060                                   struct policy_handle *handle)
1061 {
1062         struct lsa_SidArray sids;
1063         struct lsa_SidPtr sidptr;
1064         uint32_t *count;
1065         struct lsa_TransNameArray *names;
1066         struct lsa_LookupSids *r;
1067         struct lsa_RefDomainList *domains = NULL;
1068         struct tevent_req **req;
1069         int i, replies;
1070         bool ret = true;
1071         const int num_async_requests = 50;
1072
1073         count = talloc_array(tctx, uint32_t, num_async_requests);
1074         names = talloc_array(tctx, struct lsa_TransNameArray, num_async_requests);
1075         r = talloc_array(tctx, struct lsa_LookupSids, num_async_requests);
1076
1077         torture_comment(tctx, "\nTesting %d async lookupsids request\n", num_async_requests);
1078
1079         req = talloc_array(tctx, struct tevent_req *, num_async_requests);
1080
1081         sids.num_sids = 1;
1082         sids.sids = &sidptr;
1083         sidptr.sid = dom_sid_parse_talloc(tctx, "S-1-5-32-545");
1084
1085         replies = 0;
1086
1087         for (i=0; i<num_async_requests; i++) {
1088                 count[i] = 0;
1089                 names[i].count = 0;
1090                 names[i].names = NULL;
1091
1092                 r[i].in.handle = handle;
1093                 r[i].in.sids = &sids;
1094                 r[i].in.names = &names[i];
1095                 r[i].in.level = 1;
1096                 r[i].in.count = &names[i].count;
1097                 r[i].out.count = &count[i];
1098                 r[i].out.names = &names[i];
1099                 r[i].out.domains = &domains;
1100
1101                 req[i] = dcerpc_lsa_LookupSids_r_send(tctx, tctx->ev, b, &r[i]);
1102                 if (req[i] == NULL) {
1103                         ret = false;
1104                         break;
1105                 }
1106
1107                 tevent_req_set_callback(req[i], lookupsids_cb, &replies);
1108         }
1109
1110         while (replies >= 0 && replies < num_async_requests) {
1111                 tevent_loop_once(tctx->ev);
1112         }
1113
1114         talloc_free(req);
1115
1116         if (replies < 0) {
1117                 ret = false;
1118         }
1119
1120         return ret;
1121 }
1122
1123 static bool test_LookupPrivValue(struct dcerpc_binding_handle *b,
1124                                  struct torture_context *tctx,
1125                                  struct policy_handle *handle,
1126                                  struct lsa_String *name)
1127 {
1128         struct lsa_LookupPrivValue r;
1129         struct lsa_LUID luid;
1130
1131         r.in.handle = handle;
1132         r.in.name = name;
1133         r.out.luid = &luid;
1134
1135         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupPrivValue_r(b, tctx, &r),
1136                 "LookupPrivValue failed");
1137         torture_assert_ntstatus_ok(tctx, r.out.result,
1138                 "LookupPrivValue failed");
1139
1140         return true;
1141 }
1142
1143 static bool test_LookupPrivName(struct dcerpc_binding_handle *b,
1144                                 struct torture_context *tctx,
1145                                 struct policy_handle *handle,
1146                                 struct lsa_LUID *luid)
1147 {
1148         struct lsa_LookupPrivName r;
1149         struct lsa_StringLarge *name = NULL;
1150
1151         r.in.handle = handle;
1152         r.in.luid = luid;
1153         r.out.name = &name;
1154
1155         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupPrivName_r(b, tctx, &r),
1156                 "LookupPrivName failed");
1157         torture_assert_ntstatus_ok(tctx, r.out.result, "LookupPrivName failed");
1158
1159         return true;
1160 }
1161
1162 static bool test_RemovePrivilegesFromAccount(struct dcerpc_binding_handle *b,
1163                                              struct torture_context *tctx,
1164                                              struct policy_handle *handle,
1165                                              struct policy_handle *acct_handle,
1166                                              struct lsa_LUID *luid)
1167 {
1168         struct lsa_RemovePrivilegesFromAccount r;
1169         struct lsa_PrivilegeSet privs;
1170         bool ret = true;
1171
1172         torture_comment(tctx, "\nTesting RemovePrivilegesFromAccount\n");
1173
1174         r.in.handle = acct_handle;
1175         r.in.remove_all = 0;
1176         r.in.privs = &privs;
1177
1178         privs.count = 1;
1179         privs.unknown = 0;
1180         privs.set = talloc_array(tctx, struct lsa_LUIDAttribute, 1);
1181         privs.set[0].luid = *luid;
1182         privs.set[0].attribute = 0;
1183
1184         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_RemovePrivilegesFromAccount_r(b, tctx, &r),
1185                 "RemovePrivilegesFromAccount failed");
1186         if (!NT_STATUS_IS_OK(r.out.result)) {
1187
1188                 struct lsa_LookupPrivName r_name;
1189                 struct lsa_StringLarge *name = NULL;
1190
1191                 r_name.in.handle = handle;
1192                 r_name.in.luid = luid;
1193                 r_name.out.name = &name;
1194
1195                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupPrivName_r(b, tctx, &r_name),
1196                         "LookupPrivName failed");
1197                 if (!NT_STATUS_IS_OK(r_name.out.result)) {
1198                         torture_comment(tctx, "\nLookupPrivName failed - %s\n",
1199                                         nt_errstr(r_name.out.result));
1200                         return false;
1201                 }
1202                 /* Windows 2008 does not allow this to be removed */
1203                 if (strcmp("SeAuditPrivilege", name->string) == 0) {
1204                         return ret;
1205                 }
1206
1207                 torture_comment(tctx, "RemovePrivilegesFromAccount failed to remove %s - %s\n",
1208                        name->string,
1209                        nt_errstr(r.out.result));
1210                 return false;
1211         }
1212
1213         return ret;
1214 }
1215
1216 static bool test_AddPrivilegesToAccount(struct dcerpc_binding_handle *b,
1217                                         struct torture_context *tctx,
1218                                         struct policy_handle *acct_handle,
1219                                         struct lsa_LUID *luid)
1220 {
1221         struct lsa_AddPrivilegesToAccount r;
1222         struct lsa_PrivilegeSet privs;
1223         bool ret = true;
1224
1225         torture_comment(tctx, "\nTesting AddPrivilegesToAccount\n");
1226
1227         r.in.handle = acct_handle;
1228         r.in.privs = &privs;
1229
1230         privs.count = 1;
1231         privs.unknown = 0;
1232         privs.set = talloc_array(tctx, struct lsa_LUIDAttribute, 1);
1233         privs.set[0].luid = *luid;
1234         privs.set[0].attribute = 0;
1235
1236         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_AddPrivilegesToAccount_r(b, tctx, &r),
1237                 "AddPrivilegesToAccount failed");
1238         torture_assert_ntstatus_ok(tctx, r.out.result,
1239                 "AddPrivilegesToAccount failed");
1240         return ret;
1241 }
1242
1243 static bool test_EnumPrivsAccount(struct dcerpc_binding_handle *b,
1244                                   struct torture_context *tctx,
1245                                   struct policy_handle *handle,
1246                                   struct policy_handle *acct_handle)
1247 {
1248         struct lsa_EnumPrivsAccount r;
1249         struct lsa_PrivilegeSet *privs = NULL;
1250         bool ret = true;
1251
1252         torture_comment(tctx, "\nTesting EnumPrivsAccount\n");
1253
1254         r.in.handle = acct_handle;
1255         r.out.privs = &privs;
1256
1257         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumPrivsAccount_r(b, tctx, &r),
1258                 "EnumPrivsAccount failed");
1259         torture_assert_ntstatus_ok(tctx, r.out.result,
1260                 "EnumPrivsAccount failed");
1261
1262         if (privs && privs->count > 0) {
1263                 int i;
1264                 for (i=0;i<privs->count;i++) {
1265                         test_LookupPrivName(b, tctx, handle,
1266                                             &privs->set[i].luid);
1267                 }
1268
1269                 ret &= test_RemovePrivilegesFromAccount(b, tctx, handle, acct_handle,
1270                                                         &privs->set[0].luid);
1271                 ret &= test_AddPrivilegesToAccount(b, tctx, acct_handle,
1272                                                    &privs->set[0].luid);
1273         }
1274
1275         return ret;
1276 }
1277
1278 static bool test_GetSystemAccessAccount(struct dcerpc_binding_handle *b,
1279                                         struct torture_context *tctx,
1280                                         struct policy_handle *handle,
1281                                         struct policy_handle *acct_handle)
1282 {
1283         uint32_t access_mask;
1284         struct lsa_GetSystemAccessAccount r;
1285
1286         torture_comment(tctx, "\nTesting GetSystemAccessAccount\n");
1287
1288         r.in.handle = acct_handle;
1289         r.out.access_mask = &access_mask;
1290
1291         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_GetSystemAccessAccount_r(b, tctx, &r),
1292                 "GetSystemAccessAccount failed");
1293         torture_assert_ntstatus_ok(tctx, r.out.result,
1294                 "GetSystemAccessAccount failed");
1295
1296         if (r.out.access_mask != NULL) {
1297                 torture_comment(tctx, "Rights:");
1298                 if (*(r.out.access_mask) & LSA_POLICY_MODE_INTERACTIVE)
1299                         torture_comment(tctx, " LSA_POLICY_MODE_INTERACTIVE");
1300                 if (*(r.out.access_mask) & LSA_POLICY_MODE_NETWORK)
1301                         torture_comment(tctx, " LSA_POLICY_MODE_NETWORK");
1302                 if (*(r.out.access_mask) & LSA_POLICY_MODE_BATCH)
1303                         torture_comment(tctx, " LSA_POLICY_MODE_BATCH");
1304                 if (*(r.out.access_mask) & LSA_POLICY_MODE_SERVICE)
1305                         torture_comment(tctx, " LSA_POLICY_MODE_SERVICE");
1306                 if (*(r.out.access_mask) & LSA_POLICY_MODE_PROXY)
1307                         torture_comment(tctx, " LSA_POLICY_MODE_PROXY");
1308                 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_INTERACTIVE)
1309                         torture_comment(tctx, " LSA_POLICY_MODE_DENY_INTERACTIVE");
1310                 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_NETWORK)
1311                         torture_comment(tctx, " LSA_POLICY_MODE_DENY_NETWORK");
1312                 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_BATCH)
1313                         torture_comment(tctx, " LSA_POLICY_MODE_DENY_BATCH");
1314                 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_SERVICE)
1315                         torture_comment(tctx, " LSA_POLICY_MODE_DENY_SERVICE");
1316                 if (*(r.out.access_mask) & LSA_POLICY_MODE_REMOTE_INTERACTIVE)
1317                         torture_comment(tctx, " LSA_POLICY_MODE_REMOTE_INTERACTIVE");
1318                 if (*(r.out.access_mask) & LSA_POLICY_MODE_DENY_REMOTE_INTERACTIVE)
1319                         torture_comment(tctx, " LSA_POLICY_MODE_DENY_REMOTE_INTERACTIVE");
1320                 if (*(r.out.access_mask) & LSA_POLICY_MODE_ALL)
1321                         torture_comment(tctx, " LSA_POLICY_MODE_ALL");
1322                 if (*(r.out.access_mask) & LSA_POLICY_MODE_ALL_NT4)
1323                         torture_comment(tctx, " LSA_POLICY_MODE_ALL_NT4");
1324                 torture_comment(tctx, "\n");
1325         }
1326
1327         return true;
1328 }
1329
1330 static bool test_Delete(struct dcerpc_binding_handle *b,
1331                         struct torture_context *tctx,
1332                         struct policy_handle *handle)
1333 {
1334         struct lsa_Delete r;
1335
1336         torture_comment(tctx, "\nTesting Delete\n");
1337
1338         r.in.handle = handle;
1339         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_Delete_r(b, tctx, &r),
1340                 "Delete failed");
1341         if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_NOT_SUPPORTED)) {
1342                 torture_comment(tctx, "Delete should have failed NT_STATUS_NOT_SUPPORTED - %s\n", nt_errstr(r.out.result));
1343                 return false;
1344         }
1345
1346         return true;
1347 }
1348
1349 static bool test_DeleteObject(struct dcerpc_binding_handle *b,
1350                               struct torture_context *tctx,
1351                               struct policy_handle *handle)
1352 {
1353         struct lsa_DeleteObject r;
1354
1355         torture_comment(tctx, "\nTesting DeleteObject\n");
1356
1357         r.in.handle = handle;
1358         r.out.handle = handle;
1359         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_DeleteObject_r(b, tctx, &r),
1360                 "DeleteObject failed");
1361         torture_assert_ntstatus_ok(tctx, r.out.result,
1362                 "DeleteObject failed");
1363
1364         return true;
1365 }
1366
1367
1368 static bool test_CreateAccount(struct dcerpc_binding_handle *b,
1369                                struct torture_context *tctx,
1370                                struct policy_handle *handle)
1371 {
1372         struct lsa_CreateAccount r;
1373         struct dom_sid2 *newsid;
1374         struct policy_handle acct_handle;
1375
1376         newsid = dom_sid_parse_talloc(tctx, "S-1-5-12349876-4321-2854");
1377
1378         torture_comment(tctx, "\nTesting CreateAccount\n");
1379
1380         r.in.handle = handle;
1381         r.in.sid = newsid;
1382         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1383         r.out.acct_handle = &acct_handle;
1384
1385         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_CreateAccount_r(b, tctx, &r),
1386                 "CreateAccount failed");
1387         if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_OBJECT_NAME_COLLISION)) {
1388                 struct lsa_OpenAccount r_o;
1389                 r_o.in.handle = handle;
1390                 r_o.in.sid = newsid;
1391                 r_o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1392                 r_o.out.acct_handle = &acct_handle;
1393
1394                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenAccount_r(b, tctx, &r_o),
1395                         "OpenAccount failed");
1396                 torture_assert_ntstatus_ok(tctx, r_o.out.result,
1397                         "OpenAccount failed");
1398         } else {
1399                 torture_assert_ntstatus_ok(tctx, r.out.result,
1400                                            "CreateAccount failed");
1401         }
1402
1403         if (!test_Delete(b, tctx, &acct_handle)) {
1404                 return false;
1405         }
1406
1407         if (!test_DeleteObject(b, tctx, &acct_handle)) {
1408                 return false;
1409         }
1410
1411         return true;
1412 }
1413
1414 static bool test_DeleteTrustedDomain(struct dcerpc_binding_handle *b,
1415                                      struct torture_context *tctx,
1416                                      struct policy_handle *handle,
1417                                      struct lsa_StringLarge name)
1418 {
1419         struct lsa_OpenTrustedDomainByName r;
1420         struct policy_handle trustdom_handle;
1421
1422         r.in.handle = handle;
1423         r.in.name.string = name.string;
1424         r.in.access_mask = SEC_STD_DELETE;
1425         r.out.trustdom_handle = &trustdom_handle;
1426
1427         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenTrustedDomainByName_r(b, tctx, &r),
1428                 "OpenTrustedDomainByName failed");
1429         torture_assert_ntstatus_ok(tctx, r.out.result,
1430                 "OpenTrustedDomainByName failed");
1431
1432         if (!test_Delete(b, tctx, &trustdom_handle)) {
1433                 return false;
1434         }
1435
1436         if (!test_DeleteObject(b, tctx, &trustdom_handle)) {
1437                 return false;
1438         }
1439
1440         return true;
1441 }
1442
1443 static bool test_DeleteTrustedDomainBySid(struct dcerpc_binding_handle *b,
1444                                           struct torture_context *tctx,
1445                                           struct policy_handle *handle,
1446                                           struct dom_sid *sid)
1447 {
1448         struct lsa_DeleteTrustedDomain r;
1449
1450         r.in.handle = handle;
1451         r.in.dom_sid = sid;
1452
1453         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_DeleteTrustedDomain_r(b, tctx, &r),
1454                 "DeleteTrustedDomain failed");
1455         torture_assert_ntstatus_ok(tctx, r.out.result,
1456                 "DeleteTrustedDomain failed");
1457
1458         return true;
1459 }
1460
1461
1462 static bool test_CreateSecret(struct dcerpc_pipe *p,
1463                               struct torture_context *tctx,
1464                               struct policy_handle *handle)
1465 {
1466         struct lsa_CreateSecret r;
1467         struct lsa_OpenSecret r2;
1468         struct lsa_SetSecret r3;
1469         struct lsa_QuerySecret r4;
1470         struct lsa_SetSecret r5;
1471         struct lsa_QuerySecret r6;
1472         struct lsa_SetSecret r7;
1473         struct lsa_QuerySecret r8;
1474         struct policy_handle sec_handle, sec_handle2, sec_handle3;
1475         struct lsa_DeleteObject d_o;
1476         struct lsa_DATA_BUF buf1;
1477         struct lsa_DATA_BUF_PTR bufp1;
1478         struct lsa_DATA_BUF_PTR bufp2;
1479         DATA_BLOB enc_key;
1480         bool ret = true;
1481         DATA_BLOB session_key;
1482         NTTIME old_mtime, new_mtime;
1483         DATA_BLOB blob1;
1484         const char *secret1 = "abcdef12345699qwerty";
1485         char *secret2;
1486         const char *secret3 = "ABCDEF12345699QWERTY";
1487         char *secret4;
1488         const char *secret5 = "NEW-SAMBA4-SECRET";
1489         char *secret6;
1490         char *secname[2];
1491         int i;
1492         const int LOCAL = 0;
1493         const int GLOBAL = 1;
1494         struct dcerpc_binding_handle *b = p->binding_handle;
1495
1496         secname[LOCAL] = talloc_asprintf(tctx, "torturesecret-%u", (unsigned int)random());
1497         secname[GLOBAL] = talloc_asprintf(tctx, "G$torturesecret-%u", (unsigned int)random());
1498
1499         for (i=0; i< 2; i++) {
1500                 torture_comment(tctx, "\nTesting CreateSecret of %s\n", secname[i]);
1501
1502                 init_lsa_String(&r.in.name, secname[i]);
1503
1504                 r.in.handle = handle;
1505                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1506                 r.out.sec_handle = &sec_handle;
1507
1508                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_CreateSecret_r(b, tctx, &r),
1509                         "CreateSecret failed");
1510                 torture_assert_ntstatus_ok(tctx, r.out.result,
1511                         "CreateSecret failed");
1512
1513                 r.in.handle = handle;
1514                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1515                 r.out.sec_handle = &sec_handle3;
1516
1517                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_CreateSecret_r(b, tctx, &r),
1518                         "CreateSecret failed");
1519                 torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_OBJECT_NAME_COLLISION,
1520                                               "CreateSecret should have failed OBJECT_NAME_COLLISION");
1521
1522                 r2.in.handle = handle;
1523                 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1524                 r2.in.name = r.in.name;
1525                 r2.out.sec_handle = &sec_handle2;
1526
1527                 torture_comment(tctx, "Testing OpenSecret\n");
1528
1529                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenSecret_r(b, tctx, &r2),
1530                         "OpenSecret failed");
1531                 torture_assert_ntstatus_ok(tctx, r2.out.result,
1532                                            "OpenSecret failed");
1533
1534                 torture_assert_ntstatus_ok(tctx, dcerpc_fetch_session_key(p, &session_key),
1535                                            "dcerpc_fetch_session_key failed");
1536
1537                 enc_key = sess_encrypt_string(secret1, &session_key);
1538
1539                 r3.in.sec_handle = &sec_handle;
1540                 r3.in.new_val = &buf1;
1541                 r3.in.old_val = NULL;
1542                 r3.in.new_val->data = enc_key.data;
1543                 r3.in.new_val->length = enc_key.length;
1544                 r3.in.new_val->size = enc_key.length;
1545
1546                 torture_comment(tctx, "Testing SetSecret\n");
1547
1548                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_SetSecret_r(b, tctx, &r3),
1549                         "SetSecret failed");
1550                 torture_assert_ntstatus_ok(tctx, r3.out.result,
1551                         "SetSecret failed");
1552
1553                 r3.in.sec_handle = &sec_handle;
1554                 r3.in.new_val = &buf1;
1555                 r3.in.old_val = NULL;
1556                 r3.in.new_val->data = enc_key.data;
1557                 r3.in.new_val->length = enc_key.length;
1558                 r3.in.new_val->size = enc_key.length;
1559
1560                 /* break the encrypted data */
1561                 enc_key.data[0]++;
1562
1563                 torture_comment(tctx, "Testing SetSecret with broken key\n");
1564
1565                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_SetSecret_r(b, tctx, &r3),
1566                                            "SetSecret failed");
1567                 torture_assert_ntstatus_equal(tctx, r3.out.result, NT_STATUS_UNKNOWN_REVISION,
1568                                               "SetSecret should have failed UNKNOWN_REVISION");
1569
1570                 data_blob_free(&enc_key);
1571
1572                 ZERO_STRUCT(new_mtime);
1573                 ZERO_STRUCT(old_mtime);
1574
1575                 /* fetch the secret back again */
1576                 r4.in.sec_handle = &sec_handle;
1577                 r4.in.new_val = &bufp1;
1578                 r4.in.new_mtime = &new_mtime;
1579                 r4.in.old_val = NULL;
1580                 r4.in.old_mtime = NULL;
1581
1582                 bufp1.buf = NULL;
1583
1584                 torture_comment(tctx, "Testing QuerySecret\n");
1585                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QuerySecret_r(b, tctx, &r4),
1586                         "QuerySecret failed");
1587                 if (!NT_STATUS_IS_OK(r4.out.result)) {
1588                         torture_comment(tctx, "QuerySecret failed - %s\n", nt_errstr(r4.out.result));
1589                         ret = false;
1590                 } else {
1591                         if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
1592                                 torture_comment(tctx, "No secret buffer returned\n");
1593                                 ret = false;
1594                         } else {
1595                                 blob1.data = r4.out.new_val->buf->data;
1596                                 blob1.length = r4.out.new_val->buf->size;
1597
1598                                 secret2 = sess_decrypt_string(tctx,
1599                                                               &blob1, &session_key);
1600
1601                                 if (strcmp(secret1, secret2) != 0) {
1602                                         torture_comment(tctx, "Returned secret (r4) '%s' doesn't match '%s'\n",
1603                                                secret2, secret1);
1604                                         ret = false;
1605                                 }
1606                         }
1607                 }
1608
1609                 enc_key = sess_encrypt_string(secret3, &session_key);
1610
1611                 r5.in.sec_handle = &sec_handle;
1612                 r5.in.new_val = &buf1;
1613                 r5.in.old_val = NULL;
1614                 r5.in.new_val->data = enc_key.data;
1615                 r5.in.new_val->length = enc_key.length;
1616                 r5.in.new_val->size = enc_key.length;
1617
1618
1619                 smb_msleep(200);
1620                 torture_comment(tctx, "Testing SetSecret (existing value should move to old)\n");
1621
1622                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_SetSecret_r(b, tctx, &r5),
1623                         "SetSecret failed");
1624                 if (!NT_STATUS_IS_OK(r5.out.result)) {
1625                         torture_comment(tctx, "SetSecret failed - %s\n", nt_errstr(r5.out.result));
1626                         ret = false;
1627                 }
1628
1629                 data_blob_free(&enc_key);
1630
1631                 ZERO_STRUCT(new_mtime);
1632                 ZERO_STRUCT(old_mtime);
1633
1634                 /* fetch the secret back again */
1635                 r6.in.sec_handle = &sec_handle;
1636                 r6.in.new_val = &bufp1;
1637                 r6.in.new_mtime = &new_mtime;
1638                 r6.in.old_val = &bufp2;
1639                 r6.in.old_mtime = &old_mtime;
1640
1641                 bufp1.buf = NULL;
1642                 bufp2.buf = NULL;
1643
1644                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QuerySecret_r(b, tctx, &r6),
1645                         "QuerySecret failed");
1646                 if (!NT_STATUS_IS_OK(r6.out.result)) {
1647                         torture_comment(tctx, "QuerySecret failed - %s\n", nt_errstr(r6.out.result));
1648                         ret = false;
1649                         secret4 = NULL;
1650                 } else {
1651
1652                         if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL
1653                                 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
1654                                 torture_comment(tctx, "Both secret buffers and both times not returned\n");
1655                                 ret = false;
1656                                 secret4 = NULL;
1657                         } else {
1658                                 blob1.data = r6.out.new_val->buf->data;
1659                                 blob1.length = r6.out.new_val->buf->size;
1660
1661                                 secret4 = sess_decrypt_string(tctx,
1662                                                               &blob1, &session_key);
1663
1664                                 if (strcmp(secret3, secret4) != 0) {
1665                                         torture_comment(tctx, "Returned NEW secret %s doesn't match %s\n", secret4, secret3);
1666                                         ret = false;
1667                                 }
1668
1669                                 blob1.data = r6.out.old_val->buf->data;
1670                                 blob1.length = r6.out.old_val->buf->length;
1671
1672                                 secret2 = sess_decrypt_string(tctx,
1673                                                               &blob1, &session_key);
1674
1675                                 if (strcmp(secret1, secret2) != 0) {
1676                                         torture_comment(tctx, "Returned OLD secret %s doesn't match %s\n", secret2, secret1);
1677                                         ret = false;
1678                                 }
1679
1680                                 if (*r6.out.new_mtime == *r6.out.old_mtime) {
1681                                         torture_comment(tctx, "Returned secret (r6-%d) %s must not have same mtime for both secrets: %s != %s\n",
1682                                                i,
1683                                                secname[i],
1684                                                nt_time_string(tctx, *r6.out.old_mtime),
1685                                                nt_time_string(tctx, *r6.out.new_mtime));
1686                                         ret = false;
1687                                 }
1688                         }
1689                 }
1690
1691                 enc_key = sess_encrypt_string(secret5, &session_key);
1692
1693                 r7.in.sec_handle = &sec_handle;
1694                 r7.in.old_val = &buf1;
1695                 r7.in.old_val->data = enc_key.data;
1696                 r7.in.old_val->length = enc_key.length;
1697                 r7.in.old_val->size = enc_key.length;
1698                 r7.in.new_val = NULL;
1699
1700                 torture_comment(tctx, "Testing SetSecret of old Secret only\n");
1701
1702                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_SetSecret_r(b, tctx, &r7),
1703                         "SetSecret failed");
1704                 if (!NT_STATUS_IS_OK(r7.out.result)) {
1705                         torture_comment(tctx, "SetSecret failed - %s\n", nt_errstr(r7.out.result));
1706                         ret = false;
1707                 }
1708
1709                 data_blob_free(&enc_key);
1710
1711                 /* fetch the secret back again */
1712                 r8.in.sec_handle = &sec_handle;
1713                 r8.in.new_val = &bufp1;
1714                 r8.in.new_mtime = &new_mtime;
1715                 r8.in.old_val = &bufp2;
1716                 r8.in.old_mtime = &old_mtime;
1717
1718                 bufp1.buf = NULL;
1719                 bufp2.buf = NULL;
1720
1721                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QuerySecret_r(b, tctx, &r8),
1722                         "QuerySecret failed");
1723                 if (!NT_STATUS_IS_OK(r8.out.result)) {
1724                         torture_comment(tctx, "QuerySecret failed - %s\n", nt_errstr(r8.out.result));
1725                         ret = false;
1726                 } else {
1727                         if (!r8.out.new_val || !r8.out.old_val) {
1728                                 torture_comment(tctx, "in/out pointers not returned, despite being set on in for QuerySecret\n");
1729                                 ret = false;
1730                         } else if (r8.out.new_val->buf != NULL) {
1731                                 torture_comment(tctx, "NEW secret buffer must not be returned after OLD set\n");
1732                                 ret = false;
1733                         } else if (r8.out.old_val->buf == NULL) {
1734                                 torture_comment(tctx, "OLD secret buffer was not returned after OLD set\n");
1735                                 ret = false;
1736                         } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
1737                                 torture_comment(tctx, "Both times not returned after OLD set\n");
1738                                 ret = false;
1739                         } else {
1740                                 blob1.data = r8.out.old_val->buf->data;
1741                                 blob1.length = r8.out.old_val->buf->size;
1742
1743                                 secret6 = sess_decrypt_string(tctx,
1744                                                               &blob1, &session_key);
1745
1746                                 if (strcmp(secret5, secret6) != 0) {
1747                                         torture_comment(tctx, "Returned OLD secret %s doesn't match %s\n", secret5, secret6);
1748                                         ret = false;
1749                                 }
1750
1751                                 if (*r8.out.new_mtime != *r8.out.old_mtime) {
1752                                         torture_comment(tctx, "Returned secret (r8) %s did not had same mtime for both secrets: %s != %s\n",
1753                                                secname[i],
1754                                                nt_time_string(tctx, *r8.out.old_mtime),
1755                                                nt_time_string(tctx, *r8.out.new_mtime));
1756                                         ret = false;
1757                                 }
1758                         }
1759                 }
1760
1761                 if (!test_Delete(b, tctx, &sec_handle)) {
1762                         ret = false;
1763                 }
1764
1765                 if (!test_DeleteObject(b, tctx, &sec_handle)) {
1766                         return false;
1767                 }
1768
1769                 d_o.in.handle = &sec_handle2;
1770                 d_o.out.handle = &sec_handle2;
1771                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_DeleteObject_r(b, tctx, &d_o),
1772                         "DeleteObject failed");
1773                 torture_assert_ntstatus_equal(tctx, d_o.out.result, NT_STATUS_INVALID_HANDLE,
1774                                               "OpenSecret expected INVALID_HANDLE");
1775
1776                 torture_comment(tctx, "Testing OpenSecret of just-deleted secret\n");
1777
1778                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenSecret_r(b, tctx, &r2),
1779                                            "OpenSecret failed");
1780                 torture_assert_ntstatus_equal(tctx, r2.out.result, NT_STATUS_OBJECT_NAME_NOT_FOUND,
1781                                               "OpenSecret expected OBJECT_NAME_NOT_FOUND");
1782         }
1783         return ret;
1784 }
1785
1786
1787 static bool test_EnumAccountRights(struct dcerpc_binding_handle *b,
1788                                    struct torture_context *tctx,
1789                                    struct policy_handle *acct_handle,
1790                                    struct dom_sid *sid)
1791 {
1792         struct lsa_EnumAccountRights r;
1793         struct lsa_RightSet rights;
1794
1795         torture_comment(tctx, "\nTesting EnumAccountRights\n");
1796
1797         r.in.handle = acct_handle;
1798         r.in.sid = sid;
1799         r.out.rights = &rights;
1800
1801         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumAccountRights_r(b, tctx, &r),
1802                 "EnumAccountRights failed");
1803         if (!NT_STATUS_IS_OK(r.out.result)) {
1804                 torture_comment(tctx, "EnumAccountRights of %s failed - %s\n",
1805                        dom_sid_string(tctx, sid), nt_errstr(r.out.result));
1806         }
1807         torture_assert_ntstatus_ok(tctx, r.out.result,
1808                 "EnumAccountRights failed");
1809
1810         return true;
1811 }
1812
1813
1814 static bool test_QuerySecurity(struct dcerpc_binding_handle *b,
1815                              struct torture_context *tctx,
1816                              struct policy_handle *handle,
1817                              struct policy_handle *acct_handle)
1818 {
1819         struct lsa_QuerySecurity r;
1820         struct sec_desc_buf *sdbuf = NULL;
1821
1822         if (torture_setting_bool(tctx, "samba4", false)) {
1823                 torture_comment(tctx, "\nskipping QuerySecurity test against Samba4\n");
1824                 return true;
1825         }
1826
1827         torture_comment(tctx, "\nTesting QuerySecurity\n");
1828
1829         r.in.handle = acct_handle;
1830         r.in.sec_info = SECINFO_OWNER |
1831                         SECINFO_GROUP |
1832                         SECINFO_DACL;
1833         r.out.sdbuf = &sdbuf;
1834
1835         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QuerySecurity_r(b, tctx, &r),
1836                 "QuerySecurity failed");
1837         if (!NT_STATUS_IS_OK(r.out.result)) {
1838                 torture_comment(tctx, "QuerySecurity failed - %s\n", nt_errstr(r.out.result));
1839                 return false;
1840         }
1841
1842         return true;
1843 }
1844
1845 static bool test_OpenAccount(struct dcerpc_binding_handle *b,
1846                              struct torture_context *tctx,
1847                              struct policy_handle *handle,
1848                              struct dom_sid *sid)
1849 {
1850         struct lsa_OpenAccount r;
1851         struct policy_handle acct_handle;
1852
1853         torture_comment(tctx, "\nTesting OpenAccount\n");
1854
1855         r.in.handle = handle;
1856         r.in.sid = sid;
1857         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1858         r.out.acct_handle = &acct_handle;
1859
1860         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenAccount_r(b, tctx, &r),
1861                 "OpenAccount failed");
1862         torture_assert_ntstatus_ok(tctx, r.out.result,
1863                 "OpenAccount failed");
1864
1865         if (!test_EnumPrivsAccount(b, tctx, handle, &acct_handle)) {
1866                 return false;
1867         }
1868
1869         if (!test_GetSystemAccessAccount(b, tctx, handle, &acct_handle)) {
1870                 return false;
1871         }
1872
1873         if (!test_QuerySecurity(b, tctx, handle, &acct_handle)) {
1874                 return false;
1875         }
1876
1877         return true;
1878 }
1879
1880 static bool test_EnumAccounts(struct dcerpc_binding_handle *b,
1881                               struct torture_context *tctx,
1882                               struct policy_handle *handle)
1883 {
1884         struct lsa_EnumAccounts r;
1885         struct lsa_SidArray sids1, sids2;
1886         uint32_t resume_handle = 0;
1887         int i;
1888         bool ret = true;
1889
1890         torture_comment(tctx, "\nTesting EnumAccounts\n");
1891
1892         r.in.handle = handle;
1893         r.in.resume_handle = &resume_handle;
1894         r.in.num_entries = 100;
1895         r.out.resume_handle = &resume_handle;
1896         r.out.sids = &sids1;
1897
1898         resume_handle = 0;
1899         while (true) {
1900                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumAccounts_r(b, tctx, &r),
1901                         "EnumAccounts failed");
1902                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_NO_MORE_ENTRIES)) {
1903                         break;
1904                 }
1905                 torture_assert_ntstatus_ok(tctx, r.out.result,
1906                         "EnumAccounts failed");
1907
1908                 if (!test_LookupSids(b, tctx, handle, &sids1)) {
1909                         return false;
1910                 }
1911
1912                 if (!test_LookupSids2(b, tctx, handle, &sids1)) {
1913                         return false;
1914                 }
1915
1916                 /* Can't test lookupSids3 here, as clearly we must not
1917                  * be on schannel, or we would not be able to do the
1918                  * rest */
1919
1920                 torture_comment(tctx, "Testing all accounts\n");
1921                 for (i=0;i<sids1.num_sids;i++) {
1922                         ret &= test_OpenAccount(b, tctx, handle, sids1.sids[i].sid);
1923                         ret &= test_EnumAccountRights(b, tctx, handle, sids1.sids[i].sid);
1924                 }
1925                 torture_comment(tctx, "\n");
1926         }
1927
1928         if (sids1.num_sids < 3) {
1929                 return ret;
1930         }
1931
1932         torture_comment(tctx, "Trying EnumAccounts partial listing (asking for 1 at 2)\n");
1933         resume_handle = 2;
1934         r.in.num_entries = 1;
1935         r.out.sids = &sids2;
1936
1937         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumAccounts_r(b, tctx, &r),
1938                 "EnumAccounts failed");
1939         torture_assert_ntstatus_ok(tctx, r.out.result,
1940                 "EnumAccounts failed");
1941
1942         if (sids2.num_sids != 1) {
1943                 torture_comment(tctx, "Returned wrong number of entries (%d)\n", sids2.num_sids);
1944                 return false;
1945         }
1946
1947         return true;
1948 }
1949
1950 static bool test_LookupPrivDisplayName(struct dcerpc_binding_handle *b,
1951                                        struct torture_context *tctx,
1952                                        struct policy_handle *handle,
1953                                        struct lsa_String *priv_name)
1954 {
1955         struct lsa_LookupPrivDisplayName r;
1956         /* produce a reasonable range of language output without screwing up
1957            terminals */
1958         uint16_t language_id = (random() % 4) + 0x409;
1959         uint16_t returned_language_id = 0;
1960         struct lsa_StringLarge *disp_name = NULL;
1961
1962         torture_comment(tctx, "\nTesting LookupPrivDisplayName(%s)\n", priv_name->string);
1963
1964         r.in.handle = handle;
1965         r.in.name = priv_name;
1966         r.in.language_id = language_id;
1967         r.in.language_id_sys = 0;
1968         r.out.returned_language_id = &returned_language_id;
1969         r.out.disp_name = &disp_name;
1970
1971         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupPrivDisplayName_r(b, tctx, &r),
1972                 "LookupPrivDisplayName failed");
1973         if (!NT_STATUS_IS_OK(r.out.result)) {
1974                 torture_comment(tctx, "LookupPrivDisplayName failed - %s\n", nt_errstr(r.out.result));
1975                 return false;
1976         }
1977         torture_comment(tctx, "%s -> \"%s\"  (language 0x%x/0x%x)\n",
1978                priv_name->string, disp_name->string,
1979                r.in.language_id, *r.out.returned_language_id);
1980
1981         return true;
1982 }
1983
1984 static bool test_EnumAccountsWithUserRight(struct dcerpc_binding_handle *b,
1985                                            struct torture_context *tctx,
1986                                            struct policy_handle *handle,
1987                                            struct lsa_String *priv_name)
1988 {
1989         struct lsa_EnumAccountsWithUserRight r;
1990         struct lsa_SidArray sids;
1991
1992         ZERO_STRUCT(sids);
1993
1994         torture_comment(tctx, "\nTesting EnumAccountsWithUserRight(%s)\n", priv_name->string);
1995
1996         r.in.handle = handle;
1997         r.in.name = priv_name;
1998         r.out.sids = &sids;
1999
2000         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumAccountsWithUserRight_r(b, tctx, &r),
2001                 "EnumAccountsWithUserRight failed");
2002
2003         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
2004         if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_NO_MORE_ENTRIES)) {
2005                 return true;
2006         }
2007
2008         if (!NT_STATUS_IS_OK(r.out.result)) {
2009                 torture_comment(tctx, "EnumAccountsWithUserRight failed - %s\n", nt_errstr(r.out.result));
2010                 return false;
2011         }
2012
2013         return true;
2014 }
2015
2016
2017 static bool test_EnumPrivs(struct dcerpc_binding_handle *b,
2018                            struct torture_context *tctx,
2019                            struct policy_handle *handle)
2020 {
2021         struct lsa_EnumPrivs r;
2022         struct lsa_PrivArray privs1;
2023         uint32_t resume_handle = 0;
2024         int i;
2025         bool ret = true;
2026
2027         torture_comment(tctx, "\nTesting EnumPrivs\n");
2028
2029         r.in.handle = handle;
2030         r.in.resume_handle = &resume_handle;
2031         r.in.max_count = 100;
2032         r.out.resume_handle = &resume_handle;
2033         r.out.privs = &privs1;
2034
2035         resume_handle = 0;
2036         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumPrivs_r(b, tctx, &r),
2037                 "EnumPrivs failed");
2038         torture_assert_ntstatus_ok(tctx, r.out.result,
2039                 "EnumPrivs failed");
2040
2041         for (i = 0; i< privs1.count; i++) {
2042                 test_LookupPrivDisplayName(b, tctx, handle, (struct lsa_String *)&privs1.privs[i].name);
2043                 test_LookupPrivValue(b, tctx, handle, (struct lsa_String *)&privs1.privs[i].name);
2044                 if (!test_EnumAccountsWithUserRight(b, tctx, handle, (struct lsa_String *)&privs1.privs[i].name)) {
2045                         ret = false;
2046                 }
2047         }
2048
2049         return ret;
2050 }
2051
2052 static bool test_QueryForestTrustInformation(struct dcerpc_binding_handle *b,
2053                                              struct torture_context *tctx,
2054                                              struct policy_handle *handle,
2055                                              const char *trusted_domain_name)
2056 {
2057         bool ret = true;
2058         struct lsa_lsaRQueryForestTrustInformation r;
2059         struct lsa_String string;
2060         struct lsa_ForestTrustInformation info, *info_ptr;
2061
2062         torture_comment(tctx, "\nTesting lsaRQueryForestTrustInformation\n");
2063
2064         if (torture_setting_bool(tctx, "samba4", false)) {
2065                 torture_comment(tctx, "skipping QueryForestTrustInformation against Samba4\n");
2066                 return true;
2067         }
2068
2069         ZERO_STRUCT(string);
2070
2071         if (trusted_domain_name) {
2072                 init_lsa_String(&string, trusted_domain_name);
2073         }
2074
2075         info_ptr = &info;
2076
2077         r.in.handle = handle;
2078         r.in.trusted_domain_name = &string;
2079         r.in.unknown = 0;
2080         r.out.forest_trust_info = &info_ptr;
2081
2082         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_lsaRQueryForestTrustInformation_r(b, tctx, &r),
2083                 "lsaRQueryForestTrustInformation failed");
2084
2085         if (!NT_STATUS_IS_OK(r.out.result)) {
2086                 torture_comment(tctx, "lsaRQueryForestTrustInformation of %s failed - %s\n", trusted_domain_name, nt_errstr(r.out.result));
2087                 ret = false;
2088         }
2089
2090         return ret;
2091 }
2092
2093 static bool test_query_each_TrustDomEx(struct dcerpc_binding_handle *b,
2094                                        struct torture_context *tctx,
2095                                        struct policy_handle *handle,
2096                                        struct lsa_DomainListEx *domains)
2097 {
2098         int i;
2099         bool ret = true;
2100
2101         for (i=0; i< domains->count; i++) {
2102
2103                 if (domains->domains[i].trust_attributes & NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
2104                         ret &= test_QueryForestTrustInformation(b, tctx, handle,
2105                                                                 domains->domains[i].domain_name.string);
2106                 }
2107         }
2108
2109         return ret;
2110 }
2111
2112 static bool test_query_each_TrustDom(struct dcerpc_binding_handle *b,
2113                                      struct torture_context *tctx,
2114                                      struct policy_handle *handle,
2115                                      struct lsa_DomainList *domains)
2116 {
2117         int i,j;
2118         bool ret = true;
2119
2120         torture_comment(tctx, "\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
2121         for (i=0; i< domains->count; i++) {
2122                 struct lsa_OpenTrustedDomain trust;
2123                 struct lsa_OpenTrustedDomainByName trust_by_name;
2124                 struct policy_handle trustdom_handle;
2125                 struct policy_handle handle2;
2126                 struct lsa_Close c;
2127                 struct lsa_CloseTrustedDomainEx c_trust;
2128                 int levels [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
2129                 int ok[]      = {1, 0, 1, 0, 0, 1, 0, 1, 0,  0,  0,  1, 1};
2130
2131                 if (domains->domains[i].sid) {
2132                         trust.in.handle = handle;
2133                         trust.in.sid = domains->domains[i].sid;
2134                         trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2135                         trust.out.trustdom_handle = &trustdom_handle;
2136
2137                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenTrustedDomain_r(b, tctx, &trust),
2138                                 "OpenTrustedDomain failed");
2139
2140                         if (!NT_STATUS_IS_OK(trust.out.result)) {
2141                                 torture_comment(tctx, "OpenTrustedDomain failed - %s\n", nt_errstr(trust.out.result));
2142                                 return false;
2143                         }
2144
2145                         c.in.handle = &trustdom_handle;
2146                         c.out.handle = &handle2;
2147
2148                         c_trust.in.handle = &trustdom_handle;
2149                         c_trust.out.handle = &handle2;
2150
2151                         for (j=0; j < ARRAY_SIZE(levels); j++) {
2152                                 struct lsa_QueryTrustedDomainInfo q;
2153                                 union lsa_TrustedDomainInfo *info = NULL;
2154                                 q.in.trustdom_handle = &trustdom_handle;
2155                                 q.in.level = levels[j];
2156                                 q.out.info = &info;
2157                                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryTrustedDomainInfo_r(b, tctx, &q),
2158                                         "QueryTrustedDomainInfo failed");
2159                                 if (!NT_STATUS_IS_OK(q.out.result) && ok[j]) {
2160                                         torture_comment(tctx, "QueryTrustedDomainInfo level %d failed - %s\n",
2161                                                levels[j], nt_errstr(q.out.result));
2162                                         ret = false;
2163                                 } else if (NT_STATUS_IS_OK(q.out.result) && !ok[j]) {
2164                                         torture_comment(tctx, "QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
2165                                                levels[j], nt_errstr(q.out.result));
2166                                         ret = false;
2167                                 }
2168                         }
2169
2170                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_CloseTrustedDomainEx_r(b, tctx, &c_trust),
2171                                 "CloseTrustedDomainEx failed");
2172                         if (!NT_STATUS_EQUAL(c_trust.out.result, NT_STATUS_NOT_IMPLEMENTED)) {
2173                                 torture_comment(tctx, "Expected CloseTrustedDomainEx to return NT_STATUS_NOT_IMPLEMENTED, instead - %s\n", nt_errstr(c_trust.out.result));
2174                                 return false;
2175                         }
2176
2177                         c.in.handle = &trustdom_handle;
2178                         c.out.handle = &handle2;
2179
2180                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_Close_r(b, tctx, &c),
2181                                 "Close failed");
2182                         if (!NT_STATUS_IS_OK(c.out.result)) {
2183                                 torture_comment(tctx, "Close of trusted domain failed - %s\n", nt_errstr(c.out.result));
2184                                 return false;
2185                         }
2186
2187                         for (j=0; j < ARRAY_SIZE(levels); j++) {
2188                                 struct lsa_QueryTrustedDomainInfoBySid q;
2189                                 union lsa_TrustedDomainInfo *info = NULL;
2190
2191                                 if (!domains->domains[i].sid) {
2192                                         continue;
2193                                 }
2194
2195                                 q.in.handle  = handle;
2196                                 q.in.dom_sid = domains->domains[i].sid;
2197                                 q.in.level   = levels[j];
2198                                 q.out.info   = &info;
2199
2200                                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryTrustedDomainInfoBySid_r(b, tctx, &q),
2201                                         "lsa_QueryTrustedDomainInfoBySid failed");
2202                                 if (!NT_STATUS_IS_OK(q.out.result) && ok[j]) {
2203                                         torture_comment(tctx, "QueryTrustedDomainInfoBySid level %d failed - %s\n",
2204                                                levels[j], nt_errstr(q.out.result));
2205                                         ret = false;
2206                                 } else if (NT_STATUS_IS_OK(q.out.result) && !ok[j]) {
2207                                         torture_comment(tctx, "QueryTrustedDomainInfoBySid level %d unexpectedly succeeded - %s\n",
2208                                                levels[j], nt_errstr(q.out.result));
2209                                         ret = false;
2210                                 }
2211                         }
2212                 }
2213
2214                 trust_by_name.in.handle = handle;
2215                 trust_by_name.in.name.string = domains->domains[i].name.string;
2216                 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2217                 trust_by_name.out.trustdom_handle = &trustdom_handle;
2218
2219                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenTrustedDomainByName_r(b, tctx, &trust_by_name),
2220                         "OpenTrustedDomainByName failed");
2221
2222                 if (!NT_STATUS_IS_OK(trust_by_name.out.result)) {
2223                         torture_comment(tctx, "OpenTrustedDomainByName failed - %s\n", nt_errstr(trust_by_name.out.result));
2224                         return false;
2225                 }
2226
2227                 for (j=0; j < ARRAY_SIZE(levels); j++) {
2228                         struct lsa_QueryTrustedDomainInfo q;
2229                         union lsa_TrustedDomainInfo *info = NULL;
2230                         q.in.trustdom_handle = &trustdom_handle;
2231                         q.in.level = levels[j];
2232                         q.out.info = &info;
2233                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryTrustedDomainInfo_r(b, tctx, &q),
2234                                 "QueryTrustedDomainInfo failed");
2235                         if (!NT_STATUS_IS_OK(q.out.result) && ok[j]) {
2236                                 torture_comment(tctx, "QueryTrustedDomainInfo level %d failed - %s\n",
2237                                        levels[j], nt_errstr(q.out.result));
2238                                 ret = false;
2239                         } else if (NT_STATUS_IS_OK(q.out.result) && !ok[j]) {
2240                                 torture_comment(tctx, "QueryTrustedDomainInfo level %d unexpectedly succeeded - %s\n",
2241                                        levels[j], nt_errstr(q.out.result));
2242                                 ret = false;
2243                         }
2244                 }
2245
2246                 c.in.handle = &trustdom_handle;
2247                 c.out.handle = &handle2;
2248
2249                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_Close_r(b, tctx, &c),
2250                         "Close failed");
2251                 if (!NT_STATUS_IS_OK(c.out.result)) {
2252                         torture_comment(tctx, "Close of trusted domain failed - %s\n", nt_errstr(c.out.result));
2253                         return false;
2254                 }
2255
2256                 for (j=0; j < ARRAY_SIZE(levels); j++) {
2257                         struct lsa_QueryTrustedDomainInfoByName q;
2258                         union lsa_TrustedDomainInfo *info = NULL;
2259                         struct lsa_String name;
2260
2261                         name.string = domains->domains[i].name.string;
2262
2263                         q.in.handle         = handle;
2264                         q.in.trusted_domain = &name;
2265                         q.in.level          = levels[j];
2266                         q.out.info          = &info;
2267                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryTrustedDomainInfoByName_r(b, tctx, &q),
2268                                 "QueryTrustedDomainInfoByName failed");
2269                         if (!NT_STATUS_IS_OK(q.out.result) && ok[j]) {
2270                                 torture_comment(tctx, "QueryTrustedDomainInfoByName level %d failed - %s\n",
2271                                        levels[j], nt_errstr(q.out.result));
2272                                 ret = false;
2273                         } else if (NT_STATUS_IS_OK(q.out.result) && !ok[j]) {
2274                                 torture_comment(tctx, "QueryTrustedDomainInfoByName level %d unexpectedly succeeded - %s\n",
2275                                        levels[j], nt_errstr(q.out.result));
2276                                 ret = false;
2277                         }
2278                 }
2279         }
2280         return ret;
2281 }
2282
2283 static bool test_EnumTrustDom(struct dcerpc_binding_handle *b,
2284                               struct torture_context *tctx,
2285                               struct policy_handle *handle)
2286 {
2287         struct lsa_EnumTrustDom r;
2288         uint32_t in_resume_handle = 0;
2289         uint32_t out_resume_handle;
2290         struct lsa_DomainList domains;
2291         bool ret = true;
2292
2293         torture_comment(tctx, "\nTesting EnumTrustDom\n");
2294
2295         r.in.handle = handle;
2296         r.in.resume_handle = &in_resume_handle;
2297         r.in.max_size = 0;
2298         r.out.domains = &domains;
2299         r.out.resume_handle = &out_resume_handle;
2300
2301         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumTrustDom_r(b, tctx, &r),
2302                 "lsa_EnumTrustDom failed");
2303
2304         /* according to MS-LSAD 3.1.4.7.8 output resume handle MUST
2305          * always be larger than the previous input resume handle, in
2306          * particular when hitting the last query it is vital to set the
2307          * resume handle correctly to avoid infinite client loops, as
2308          * seen e.g.  with Windows XP SP3 when resume handle is 0 and
2309          * status is NT_STATUS_OK - gd */
2310
2311         if (NT_STATUS_IS_OK(r.out.result) ||
2312             NT_STATUS_EQUAL(r.out.result, NT_STATUS_NO_MORE_ENTRIES) ||
2313             NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES))
2314         {
2315                 if (out_resume_handle <= in_resume_handle) {
2316                         torture_comment(tctx, "EnumTrustDom failed - should have returned output resume_handle (0x%08x) larger than input resume handle (0x%08x)\n",
2317                                 out_resume_handle, in_resume_handle);
2318                         return false;
2319                 }
2320         }
2321
2322         if (NT_STATUS_IS_OK(r.out.result)) {
2323                 if (domains.count == 0) {
2324                         torture_comment(tctx, "EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
2325                         return false;
2326                 }
2327         } else if (!(NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(r.out.result, NT_STATUS_NO_MORE_ENTRIES))) {
2328                 torture_comment(tctx, "EnumTrustDom of zero size failed - %s\n", nt_errstr(r.out.result));
2329                 return false;
2330         }
2331
2332         /* Start from the bottom again */
2333         in_resume_handle = 0;
2334
2335         do {
2336                 r.in.handle = handle;
2337                 r.in.resume_handle = &in_resume_handle;
2338                 r.in.max_size = LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3;
2339                 r.out.domains = &domains;
2340                 r.out.resume_handle = &out_resume_handle;
2341
2342                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumTrustDom_r(b, tctx, &r),
2343                         "EnumTrustDom failed");
2344
2345                 /* according to MS-LSAD 3.1.4.7.8 output resume handle MUST
2346                  * always be larger than the previous input resume handle, in
2347                  * particular when hitting the last query it is vital to set the
2348                  * resume handle correctly to avoid infinite client loops, as
2349                  * seen e.g.  with Windows XP SP3 when resume handle is 0 and
2350                  * status is NT_STATUS_OK - gd */
2351
2352                 if (NT_STATUS_IS_OK(r.out.result) ||
2353                     NT_STATUS_EQUAL(r.out.result, NT_STATUS_NO_MORE_ENTRIES) ||
2354                     NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES))
2355                 {
2356                         if (out_resume_handle <= in_resume_handle) {
2357                                 torture_comment(tctx, "EnumTrustDom failed - should have returned output resume_handle (0x%08x) larger than input resume handle (0x%08x)\n",
2358                                         out_resume_handle, in_resume_handle);
2359                                 return false;
2360                         }
2361                 }
2362
2363                 /* NO_MORE_ENTRIES is allowed */
2364                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_NO_MORE_ENTRIES)) {
2365                         if (domains.count == 0) {
2366                                 return true;
2367                         }
2368                         torture_comment(tctx, "EnumTrustDom failed - should have returned 0 trusted domains with 'NT_STATUS_NO_MORE_ENTRIES'\n");
2369                         return false;
2370                 } else if (NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES)) {
2371                         /* Windows 2003 gets this off by one on the first run */
2372                         if (r.out.domains->count < 3 || r.out.domains->count > 4) {
2373                                 torture_comment(tctx, "EnumTrustDom didn't fill the buffer we "
2374                                        "asked it to (got %d, expected %d / %d == %d entries)\n",
2375                                        r.out.domains->count, LSA_ENUM_TRUST_DOMAIN_MULTIPLIER * 3,
2376                                        LSA_ENUM_TRUST_DOMAIN_MULTIPLIER, r.in.max_size);
2377                                 ret = false;
2378                         }
2379                 } else if (!NT_STATUS_IS_OK(r.out.result)) {
2380                         torture_comment(tctx, "EnumTrustDom failed - %s\n", nt_errstr(r.out.result));
2381                         return false;
2382                 }
2383
2384                 if (domains.count == 0) {
2385                         torture_comment(tctx, "EnumTrustDom failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
2386                         return false;
2387                 }
2388
2389                 ret &= test_query_each_TrustDom(b, tctx, handle, &domains);
2390
2391                 in_resume_handle = out_resume_handle;
2392
2393         } while ((NT_STATUS_EQUAL(r.out.result, STATUS_MORE_ENTRIES)));
2394
2395         return ret;
2396 }
2397
2398 static bool test_EnumTrustDomEx(struct dcerpc_binding_handle *b,
2399                                 struct torture_context *tctx,
2400                                 struct policy_handle *handle)
2401 {
2402         struct lsa_EnumTrustedDomainsEx r_ex;
2403         uint32_t resume_handle = 0;
2404         struct lsa_DomainListEx domains_ex;
2405         bool ret = true;
2406
2407         torture_comment(tctx, "\nTesting EnumTrustedDomainsEx\n");
2408
2409         r_ex.in.handle = handle;
2410         r_ex.in.resume_handle = &resume_handle;
2411         r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
2412         r_ex.out.domains = &domains_ex;
2413         r_ex.out.resume_handle = &resume_handle;
2414
2415         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumTrustedDomainsEx_r(b, tctx, &r_ex),
2416                 "EnumTrustedDomainsEx failed");
2417
2418         if (!(NT_STATUS_EQUAL(r_ex.out.result, STATUS_MORE_ENTRIES) || NT_STATUS_EQUAL(r_ex.out.result, NT_STATUS_NO_MORE_ENTRIES))) {
2419                 torture_comment(tctx, "EnumTrustedDomainEx of zero size failed - %s\n", nt_errstr(r_ex.out.result));
2420                 return false;
2421         }
2422
2423         resume_handle = 0;
2424         do {
2425                 r_ex.in.handle = handle;
2426                 r_ex.in.resume_handle = &resume_handle;
2427                 r_ex.in.max_size = LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER * 3;
2428                 r_ex.out.domains = &domains_ex;
2429                 r_ex.out.resume_handle = &resume_handle;
2430
2431                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumTrustedDomainsEx_r(b, tctx, &r_ex),
2432                         "EnumTrustedDomainsEx failed");
2433
2434                 /* NO_MORE_ENTRIES is allowed */
2435                 if (NT_STATUS_EQUAL(r_ex.out.result, NT_STATUS_NO_MORE_ENTRIES)) {
2436                         if (domains_ex.count == 0) {
2437                                 return true;
2438                         }
2439                         torture_comment(tctx, "EnumTrustDomainsEx failed - should have returned 0 trusted domains with 'NT_STATUS_NO_MORE_ENTRIES'\n");
2440                         return false;
2441                 } else if (NT_STATUS_EQUAL(r_ex.out.result, STATUS_MORE_ENTRIES)) {
2442                         /* Windows 2003 gets this off by one on the first run */
2443                         if (r_ex.out.domains->count < 3 || r_ex.out.domains->count > 4) {
2444                                 torture_comment(tctx, "EnumTrustDom didn't fill the buffer we "
2445                                        "asked it to (got %d, expected %d / %d == %d entries)\n",
2446                                        r_ex.out.domains->count,
2447                                        r_ex.in.max_size,
2448                                        LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER,
2449                                        r_ex.in.max_size / LSA_ENUM_TRUST_DOMAIN_EX_MULTIPLIER);
2450                         }
2451                 } else if (!NT_STATUS_IS_OK(r_ex.out.result)) {
2452                         torture_comment(tctx, "EnumTrustedDomainEx failed - %s\n", nt_errstr(r_ex.out.result));
2453                         return false;
2454                 }
2455
2456                 if (domains_ex.count == 0) {
2457                         torture_comment(tctx, "EnumTrustDomainEx failed - should have returned 'NT_STATUS_NO_MORE_ENTRIES' for 0 trusted domains\n");
2458                         return false;
2459                 }
2460
2461                 ret &= test_query_each_TrustDomEx(b, tctx, handle, &domains_ex);
2462
2463         } while ((NT_STATUS_EQUAL(r_ex.out.result, STATUS_MORE_ENTRIES)));
2464
2465         return ret;
2466 }
2467
2468
2469 static bool test_CreateTrustedDomain(struct dcerpc_binding_handle *b,
2470                                      struct torture_context *tctx,
2471                                      struct policy_handle *handle,
2472                                      uint32_t num_trusts)
2473 {
2474         bool ret = true;
2475         struct lsa_CreateTrustedDomain r;
2476         struct lsa_DomainInfo trustinfo;
2477         struct dom_sid **domsid;
2478         struct policy_handle *trustdom_handle;
2479         struct lsa_QueryTrustedDomainInfo q;
2480         union lsa_TrustedDomainInfo *info = NULL;
2481         int i;
2482
2483         torture_comment(tctx, "\nTesting CreateTrustedDomain for %d domains\n", num_trusts);
2484
2485         if (!test_EnumTrustDom(b, tctx, handle)) {
2486                 ret = false;
2487         }
2488
2489         if (!test_EnumTrustDomEx(b, tctx, handle)) {
2490                 ret = false;
2491         }
2492
2493         domsid = talloc_array(tctx, struct dom_sid *, num_trusts);
2494         trustdom_handle = talloc_array(tctx, struct policy_handle, num_trusts);
2495
2496         for (i=0; i< num_trusts; i++) {
2497                 char *trust_name = talloc_asprintf(tctx, "torturedom%02d", i);
2498                 char *trust_sid = talloc_asprintf(tctx, "S-1-5-21-97398-379795-100%02d", i);
2499
2500                 domsid[i] = dom_sid_parse_talloc(tctx, trust_sid);
2501
2502                 trustinfo.sid = domsid[i];
2503                 init_lsa_String((struct lsa_String *)&trustinfo.name, trust_name);
2504
2505                 r.in.policy_handle = handle;
2506                 r.in.info = &trustinfo;
2507                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2508                 r.out.trustdom_handle = &trustdom_handle[i];
2509
2510                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_CreateTrustedDomain_r(b, tctx, &r),
2511                         "CreateTrustedDomain failed");
2512                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_OBJECT_NAME_COLLISION)) {
2513                         test_DeleteTrustedDomain(b, tctx, handle, trustinfo.name);
2514                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_CreateTrustedDomain_r(b, tctx, &r),
2515                                 "CreateTrustedDomain failed");
2516                 }
2517                 if (!NT_STATUS_IS_OK(r.out.result)) {
2518                         torture_comment(tctx, "CreateTrustedDomain failed - %s\n", nt_errstr(r.out.result));
2519                         ret = false;
2520                 } else {
2521
2522                         q.in.trustdom_handle = &trustdom_handle[i];
2523                         q.in.level = LSA_TRUSTED_DOMAIN_INFO_INFO_EX;
2524                         q.out.info = &info;
2525                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryTrustedDomainInfo_r(b, tctx, &q),
2526                                 "QueryTrustedDomainInfo failed");
2527                         if (!NT_STATUS_IS_OK(q.out.result)) {
2528                                 torture_comment(tctx, "QueryTrustedDomainInfo level %d failed - %s\n", q.in.level, nt_errstr(q.out.result));
2529                                 ret = false;
2530                         } else if (!q.out.info) {
2531                                 ret = false;
2532                         } else {
2533                                 if (strcmp(info->info_ex.netbios_name.string, trustinfo.name.string) != 0) {
2534                                         torture_comment(tctx, "QueryTrustedDomainInfo returned inconsistent short name: %s != %s\n",
2535                                                info->info_ex.netbios_name.string, trustinfo.name.string);
2536                                         ret = false;
2537                                 }
2538                                 if (info->info_ex.trust_type != LSA_TRUST_TYPE_DOWNLEVEL) {
2539                                         torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust type %d != %d\n",
2540                                                trust_name, info->info_ex.trust_type, LSA_TRUST_TYPE_DOWNLEVEL);
2541                                         ret = false;
2542                                 }
2543                                 if (info->info_ex.trust_attributes != 0) {
2544                                         torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust attributes %d != %d\n",
2545                                                trust_name, info->info_ex.trust_attributes, 0);
2546                                         ret = false;
2547                                 }
2548                                 if (info->info_ex.trust_direction != LSA_TRUST_DIRECTION_OUTBOUND) {
2549                                         torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust direction %d != %d\n",
2550                                                trust_name, info->info_ex.trust_direction, LSA_TRUST_DIRECTION_OUTBOUND);
2551                                         ret = false;
2552                                 }
2553                         }
2554                 }
2555         }
2556
2557         /* now that we have some domains to look over, we can test the enum calls */
2558         if (!test_EnumTrustDom(b, tctx, handle)) {
2559                 ret = false;
2560         }
2561
2562         if (!test_EnumTrustDomEx(b, tctx, handle)) {
2563                 ret = false;
2564         }
2565
2566         for (i=0; i<num_trusts; i++) {
2567                 if (!test_DeleteTrustedDomainBySid(b, tctx, handle, domsid[i])) {
2568                         ret = false;
2569                 }
2570         }
2571
2572         return ret;
2573 }
2574
2575 static bool gen_authinfo_internal(TALLOC_CTX *mem_ctx, const char *password,
2576                                   DATA_BLOB session_key,
2577                                   struct lsa_TrustDomainInfoAuthInfoInternal **_authinfo_internal)
2578 {
2579         struct lsa_TrustDomainInfoAuthInfoInternal *authinfo_internal;
2580         struct trustDomainPasswords auth_struct;
2581         struct AuthenticationInformation *auth_info_array;
2582         size_t converted_size;
2583         DATA_BLOB auth_blob;
2584         enum ndr_err_code ndr_err;
2585
2586         authinfo_internal = talloc_zero(mem_ctx, struct lsa_TrustDomainInfoAuthInfoInternal);
2587         if (authinfo_internal == NULL) {
2588                 return false;
2589         }
2590
2591         auth_info_array = talloc_array(mem_ctx,
2592                                        struct AuthenticationInformation, 1);
2593         if (auth_info_array == NULL) {
2594                 return false;
2595         }
2596
2597         generate_random_buffer(auth_struct.confounder, sizeof(auth_struct.confounder));
2598
2599         auth_info_array[0].AuthType = TRUST_AUTH_TYPE_CLEAR;
2600
2601         if (!convert_string_talloc(mem_ctx, CH_UNIX, CH_UTF16, password,
2602                                   strlen(password),
2603                                   &auth_info_array[0].AuthInfo.clear.password,
2604                                   &converted_size)) {
2605                 return false;
2606         }
2607
2608         auth_info_array[0].AuthInfo.clear.size = converted_size;
2609
2610         auth_struct.outgoing.count = 1;
2611         auth_struct.outgoing.current.count = 1;
2612         auth_struct.outgoing.current.array = auth_info_array;
2613         auth_struct.outgoing.previous.count = 0;
2614         auth_struct.outgoing.previous.array = NULL;
2615
2616         auth_struct.incoming.count = 1;
2617         auth_struct.incoming.current.count = 1;
2618         auth_struct.incoming.current.array = auth_info_array;
2619         auth_struct.incoming.previous.count = 0;
2620         auth_struct.incoming.previous.array = NULL;
2621
2622
2623         ndr_err = ndr_push_struct_blob(&auth_blob, mem_ctx, &auth_struct,
2624                                        (ndr_push_flags_fn_t)ndr_push_trustDomainPasswords);
2625         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2626                 return false;
2627         }
2628
2629         arcfour_crypt_blob(auth_blob.data, auth_blob.length, &session_key);
2630
2631         authinfo_internal->auth_blob.size = auth_blob.length;
2632         authinfo_internal->auth_blob.data = auth_blob.data;
2633
2634         *_authinfo_internal = authinfo_internal;
2635
2636         return true;
2637 }
2638
2639 static bool gen_authinfo(TALLOC_CTX *mem_ctx, const char *password,
2640                          struct lsa_TrustDomainInfoAuthInfo **_authinfo)
2641 {
2642         struct lsa_TrustDomainInfoAuthInfo *authinfo;
2643         struct lsa_TrustDomainInfoBuffer *info_buffer;
2644         size_t converted_size;
2645
2646         authinfo = talloc_zero(mem_ctx, struct lsa_TrustDomainInfoAuthInfo);
2647         if (authinfo == NULL) {
2648                 return false;
2649         }
2650
2651         info_buffer = talloc_zero(mem_ctx, struct lsa_TrustDomainInfoBuffer);
2652         if (info_buffer == NULL) {
2653                 return false;
2654         }
2655
2656         info_buffer->AuthType = TRUST_AUTH_TYPE_CLEAR;
2657
2658         if (!convert_string_talloc(mem_ctx, CH_UNIX, CH_UTF16, password,
2659                                   strlen(password),
2660                                   &info_buffer->data.data,
2661                                   &converted_size)) {
2662                 return false;
2663         }
2664
2665         info_buffer->data.size = converted_size;
2666
2667         authinfo->incoming_count = 1;
2668         authinfo->incoming_current_auth_info = info_buffer;
2669         authinfo->incoming_previous_auth_info = NULL;
2670         authinfo->outgoing_count = 1;
2671         authinfo->outgoing_current_auth_info = info_buffer;
2672         authinfo->outgoing_previous_auth_info = NULL;
2673
2674         *_authinfo = authinfo;
2675
2676         return true;
2677 }
2678
2679 static bool check_pw_with_ServerAuthenticate3(struct dcerpc_pipe *p,
2680                                              struct torture_context *tctx,
2681                                              uint32_t negotiate_flags,
2682                                              struct cli_credentials *machine_credentials,
2683                                              struct netlogon_creds_CredentialState **creds_out)
2684 {
2685         struct netr_ServerReqChallenge r;
2686         struct netr_ServerAuthenticate3 a;
2687         struct netr_Credential credentials1, credentials2, credentials3;
2688         struct netlogon_creds_CredentialState *creds;
2689         struct samr_Password mach_password;
2690         uint32_t rid;
2691         const char *machine_name;
2692         const char *plain_pass;
2693         struct dcerpc_binding_handle *b = p->binding_handle;
2694
2695         machine_name = cli_credentials_get_workstation(machine_credentials);
2696         plain_pass = cli_credentials_get_password(machine_credentials);
2697
2698         r.in.server_name = NULL;
2699         r.in.computer_name = machine_name;
2700         r.in.credentials = &credentials1;
2701         r.out.return_credentials = &credentials2;
2702
2703         generate_random_buffer(credentials1.data, sizeof(credentials1.data));
2704
2705         torture_assert_ntstatus_ok(tctx, dcerpc_netr_ServerReqChallenge_r(b, tctx, &r),
2706                 "ServerReqChallenge failed");
2707         torture_assert_ntstatus_ok(tctx, r.out.result, "ServerReqChallenge failed");
2708
2709         E_md4hash(plain_pass, mach_password.hash);
2710
2711         a.in.server_name = NULL;
2712         a.in.account_name = talloc_asprintf(tctx, "%s$", machine_name);
2713         a.in.secure_channel_type = cli_credentials_get_secure_channel_type(machine_credentials);
2714         a.in.computer_name = machine_name;
2715         a.in.negotiate_flags = &negotiate_flags;
2716         a.in.credentials = &credentials3;
2717         a.out.return_credentials = &credentials3;
2718         a.out.negotiate_flags = &negotiate_flags;
2719         a.out.rid = &rid;
2720
2721         creds = netlogon_creds_client_init(tctx, a.in.account_name,
2722                                            a.in.computer_name,
2723                                            &credentials1, &credentials2,
2724                                            &mach_password, &credentials3,
2725                                            negotiate_flags);
2726
2727         torture_assert(tctx, creds != NULL, "memory allocation");
2728
2729         torture_assert_ntstatus_ok(tctx, dcerpc_netr_ServerAuthenticate3_r(b, tctx, &a),
2730                 "ServerAuthenticate3 failed");
2731         if (!NT_STATUS_IS_OK(a.out.result)) {
2732                 if (!NT_STATUS_EQUAL(a.out.result, NT_STATUS_ACCESS_DENIED)) {
2733                         torture_assert_ntstatus_ok(tctx, a.out.result,
2734                                                    "ServerAuthenticate3 failed");
2735                 }
2736                 return false;
2737         }
2738         torture_assert(tctx, netlogon_creds_client_check(creds, &credentials3), "Credential chaining failed");
2739
2740         /* Prove that requesting a challenge again won't break it */
2741         torture_assert_ntstatus_ok(tctx, dcerpc_netr_ServerReqChallenge_r(b, tctx, &r),
2742                 "ServerReqChallenge failed");
2743         torture_assert_ntstatus_ok(tctx, r.out.result, "ServerReqChallenge failed");
2744
2745         *creds_out = creds;
2746         return true;
2747 }
2748
2749 static bool check_dom_trust_pw(struct dcerpc_pipe *p,
2750                                struct torture_context *tctx,
2751                                const char *trusted_dom_name,
2752                                const char *password)
2753 {
2754         struct cli_credentials *credentials;
2755         char *dummy;
2756         struct netlogon_creds_CredentialState *creds;
2757         struct dcerpc_pipe *pipe;
2758         NTSTATUS status;
2759         bool ok;
2760
2761         credentials = cli_credentials_init(tctx);
2762         if (credentials == NULL) {
2763                 return false;
2764         }
2765
2766         dummy = talloc_asprintf(tctx, "%s$", trusted_dom_name);
2767         if (dummy == NULL) {
2768                 return false;
2769         }
2770
2771         cli_credentials_set_username(credentials, dummy, CRED_SPECIFIED);
2772         cli_credentials_set_password(credentials, password, CRED_SPECIFIED);
2773         cli_credentials_set_workstation(credentials,
2774                                         trusted_dom_name, CRED_SPECIFIED);
2775         cli_credentials_set_secure_channel_type(credentials, SEC_CHAN_DOMAIN);
2776
2777         status = dcerpc_pipe_connect_b(tctx, &pipe, p->binding,
2778                                        &ndr_table_netlogon,
2779                                        cli_credentials_init_anon(tctx),
2780                                        tctx->ev, tctx->lp_ctx);
2781         if (!NT_STATUS_IS_OK(status)) {
2782                 torture_comment(tctx, "dcerpc_pipe_connect_b failed.\n");
2783                 return false;
2784         }
2785
2786         ok = check_pw_with_ServerAuthenticate3(pipe, tctx,
2787                                                NETLOGON_NEG_AUTH2_ADS_FLAGS,
2788                                                credentials, &creds);
2789         talloc_free(pipe);
2790
2791         return ok;
2792 }
2793
2794 static bool test_CreateTrustedDomainEx_common(struct dcerpc_pipe *p,
2795                                               struct torture_context *tctx,
2796                                               struct policy_handle *handle,
2797                                               uint32_t num_trusts,
2798                                               bool ex2_call)
2799 {
2800         NTSTATUS status;
2801         bool ret = true;
2802         struct lsa_CreateTrustedDomainEx r;
2803         struct lsa_CreateTrustedDomainEx2 r2;
2804         struct lsa_TrustDomainInfoInfoEx trustinfo;
2805         struct lsa_TrustDomainInfoAuthInfoInternal *authinfo_internal;
2806         struct lsa_TrustDomainInfoAuthInfo *authinfo;
2807         struct dom_sid **domsid;
2808         struct policy_handle *trustdom_handle;
2809         struct lsa_QueryTrustedDomainInfo q;
2810         union lsa_TrustedDomainInfo *info = NULL;
2811         DATA_BLOB session_key;
2812         int i;
2813         struct dcerpc_binding_handle *b = p->binding_handle;
2814
2815         if (ex2_call) {
2816                 torture_comment(tctx, "\nTesting CreateTrustedDomainEx2 for %d domains\n", num_trusts);
2817         } else {
2818                 torture_comment(tctx, "\nTesting CreateTrustedDomainEx for %d domains\n", num_trusts);
2819         }
2820
2821         domsid = talloc_array(tctx, struct dom_sid *, num_trusts);
2822         trustdom_handle = talloc_array(tctx, struct policy_handle, num_trusts);
2823
2824         status = dcerpc_fetch_session_key(p, &session_key);
2825         if (!NT_STATUS_IS_OK(status)) {
2826                 torture_comment(tctx, "dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
2827                 return false;
2828         }
2829
2830         for (i=0; i< num_trusts; i++) {
2831                 char *trust_name = talloc_asprintf(tctx, "torturedom%02d", i);
2832                 char *trust_name_dns = talloc_asprintf(tctx, "torturedom%02d.samba.example.com", i);
2833                 char *trust_sid = talloc_asprintf(tctx, "S-1-5-21-97398-379795-100%02d", i);
2834
2835                 domsid[i] = dom_sid_parse_talloc(tctx, trust_sid);
2836
2837                 trustinfo.sid = domsid[i];
2838                 trustinfo.netbios_name.string = trust_name;
2839                 trustinfo.domain_name.string = trust_name_dns;
2840
2841                 /* Create inbound, some outbound, and some
2842                  * bi-directional trusts in a repeating pattern based
2843                  * on i */
2844
2845                 /* 1 == inbound, 2 == outbound, 3 == both */
2846                 trustinfo.trust_direction = (i % 3) + 1;
2847
2848                 /* Try different trust types too */
2849
2850                 /* 1 == downlevel (NT4), 2 == uplevel (ADS), 3 == MIT (kerberos but not AD) */
2851                 trustinfo.trust_type = (((i / 3) + 1) % 3) + 1;
2852
2853                 trustinfo.trust_attributes = LSA_TRUST_ATTRIBUTE_USES_RC4_ENCRYPTION;
2854
2855                 if (!gen_authinfo_internal(tctx, TRUSTPW, session_key, &authinfo_internal)) {
2856                         torture_comment(tctx, "gen_authinfo_internal failed");
2857                         ret = false;
2858                 }
2859
2860                 if (!gen_authinfo(tctx, TRUSTPW, &authinfo)) {
2861                         torture_comment(tctx, "gen_authinfonfo failed");
2862                         ret = false;
2863                 }
2864
2865                 if (ex2_call) {
2866
2867                         r2.in.policy_handle = handle;
2868                         r2.in.info = &trustinfo;
2869                         r2.in.auth_info_internal = authinfo_internal;
2870                         r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2871                         r2.out.trustdom_handle = &trustdom_handle[i];
2872
2873                         torture_assert_ntstatus_ok(tctx,
2874                                 dcerpc_lsa_CreateTrustedDomainEx2_r(b, tctx, &r2),
2875                                 "CreateTrustedDomainEx2 failed");
2876
2877                         status = r2.out.result;
2878                 } else {
2879
2880                         r.in.policy_handle = handle;
2881                         r.in.info = &trustinfo;
2882                         r.in.auth_info = authinfo;
2883                         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
2884                         r.out.trustdom_handle = &trustdom_handle[i];
2885
2886                         torture_assert_ntstatus_ok(tctx,
2887                                 dcerpc_lsa_CreateTrustedDomainEx_r(b, tctx, &r),
2888                                 "CreateTrustedDomainEx failed");
2889
2890                         status = r.out.result;
2891                 }
2892
2893                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
2894                         test_DeleteTrustedDomain(b, tctx, handle, trustinfo.netbios_name);
2895                         if (ex2_call) {
2896                                 torture_assert_ntstatus_ok(tctx,
2897                                         dcerpc_lsa_CreateTrustedDomainEx2_r(b, tctx, &r2),
2898                                         "CreateTrustedDomainEx2 failed");
2899                                 status = r2.out.result;
2900                         } else {
2901                                 torture_assert_ntstatus_ok(tctx,
2902                                         dcerpc_lsa_CreateTrustedDomainEx_r(b, tctx, &r),
2903                                         "CreateTrustedDomainEx2 failed");
2904                                 status = r.out.result;
2905                         }
2906                 }
2907                 if (!NT_STATUS_IS_OK(status)) {
2908                         torture_comment(tctx, "CreateTrustedDomainEx failed2 - %s\n", nt_errstr(status));
2909                         ret = false;
2910                 } else {
2911                         /* For outbound and MIT trusts there is no trust account */
2912                         if (trustinfo.trust_direction != 2 &&
2913                             trustinfo.trust_type != 3) {
2914
2915                                 if (torture_setting_bool(tctx, "samba3", false) ||
2916                                     torture_setting_bool(tctx, "samba4", false)) {
2917                                         torture_comment(tctx, "skipping trusted domain auth tests against samba");
2918                                 } else {
2919                                         if (check_dom_trust_pw(p, tctx, trust_name,
2920                                                                 "x" TRUSTPW "x")) {
2921                                                 torture_comment(tctx, "Password check passed unexpectedly\n");
2922                                                 ret = false;
2923                                         }
2924                                         if (!check_dom_trust_pw(p, tctx, trust_name,
2925                                                                 TRUSTPW)) {
2926                                                 torture_comment(tctx, "Password check failed\n");
2927                                                 ret = false;
2928                                         }
2929                                 }
2930                         }
2931
2932                         q.in.trustdom_handle = &trustdom_handle[i];
2933                         q.in.level = LSA_TRUSTED_DOMAIN_INFO_INFO_EX;
2934                         q.out.info = &info;
2935                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryTrustedDomainInfo_r(b, tctx, &q),
2936                                 "QueryTrustedDomainInfo failed");
2937                         if (!NT_STATUS_IS_OK(q.out.result)) {
2938                                 torture_comment(tctx, "QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(q.out.result));
2939                                 ret = false;
2940                         } else if (!q.out.info) {
2941                                 torture_comment(tctx, "QueryTrustedDomainInfo level 1 failed to return an info pointer\n");
2942                                 ret = false;
2943                         } else {
2944                                 if (strcmp(info->info_ex.netbios_name.string, trustinfo.netbios_name.string) != 0) {
2945                                         torture_comment(tctx, "QueryTrustedDomainInfo returned inconsistent short name: %s != %s\n",
2946                                                info->info_ex.netbios_name.string, trustinfo.netbios_name.string);
2947                                         ret = false;
2948                                 }
2949                                 if (info->info_ex.trust_type != trustinfo.trust_type) {
2950                                         torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust type %d != %d\n",
2951                                                trust_name, info->info_ex.trust_type, trustinfo.trust_type);
2952                                         ret = false;
2953                                 }
2954                                 if (info->info_ex.trust_attributes != LSA_TRUST_ATTRIBUTE_USES_RC4_ENCRYPTION) {
2955                                         torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust attributes %d != %d\n",
2956                                                trust_name, info->info_ex.trust_attributes, LSA_TRUST_ATTRIBUTE_USES_RC4_ENCRYPTION);
2957                                         ret = false;
2958                                 }
2959                                 if (info->info_ex.trust_direction != trustinfo.trust_direction) {
2960                                         torture_comment(tctx, "QueryTrustedDomainInfo of %s returned incorrect trust direction %d != %d\n",
2961                                                trust_name, info->info_ex.trust_direction, trustinfo.trust_direction);
2962                                         ret = false;
2963                                 }
2964                         }
2965                 }
2966         }
2967
2968         /* now that we have some domains to look over, we can test the enum calls */
2969         if (!test_EnumTrustDom(b, tctx, handle)) {
2970                 torture_comment(tctx, "test_EnumTrustDom failed\n");
2971                 ret = false;
2972         }
2973
2974         if (!test_EnumTrustDomEx(b, tctx, handle)) {
2975                 torture_comment(tctx, "test_EnumTrustDomEx failed\n");
2976                 ret = false;
2977         }
2978
2979         for (i=0; i<num_trusts; i++) {
2980                 if (!test_DeleteTrustedDomainBySid(b, tctx, handle, domsid[i])) {
2981                         torture_comment(tctx, "test_DeleteTrustedDomainBySid failed\n");
2982                         ret = false;
2983                 }
2984         }
2985
2986         return ret;
2987 }
2988
2989 static bool test_CreateTrustedDomainEx2(struct dcerpc_pipe *p,
2990                                         struct torture_context *tctx,
2991                                         struct policy_handle *handle,
2992                                         uint32_t num_trusts)
2993 {
2994         return test_CreateTrustedDomainEx_common(p, tctx, handle, num_trusts, true);
2995 }
2996
2997 static bool test_CreateTrustedDomainEx(struct dcerpc_pipe *p,
2998                                        struct torture_context *tctx,
2999                                        struct policy_handle *handle,
3000                                        uint32_t num_trusts)
3001 {
3002         return test_CreateTrustedDomainEx_common(p, tctx, handle, num_trusts, false);
3003 }
3004
3005 static bool test_QueryDomainInfoPolicy(struct dcerpc_binding_handle *b,
3006                                  struct torture_context *tctx,
3007                                  struct policy_handle *handle)
3008 {
3009         struct lsa_QueryDomainInformationPolicy r;
3010         union lsa_DomainInformationPolicy *info = NULL;
3011         int i;
3012         bool ret = true;
3013
3014         if (torture_setting_bool(tctx, "samba3", false)) {
3015                 torture_skip(tctx, "skipping QueryDomainInformationPolicy test\n");
3016         }
3017
3018         torture_comment(tctx, "\nTesting QueryDomainInformationPolicy\n");
3019
3020         for (i=2;i<4;i++) {
3021                 r.in.handle = handle;
3022                 r.in.level = i;
3023                 r.out.info = &info;
3024
3025                 torture_comment(tctx, "\nTrying QueryDomainInformationPolicy level %d\n", i);
3026
3027                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryDomainInformationPolicy_r(b, tctx, &r),
3028                         "QueryDomainInformationPolicy failed");
3029
3030                 /* If the server does not support EFS, then this is the correct return */
3031                 if (i == LSA_DOMAIN_INFO_POLICY_EFS && NT_STATUS_EQUAL(r.out.result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
3032                         continue;
3033                 } else if (!NT_STATUS_IS_OK(r.out.result)) {
3034                         torture_comment(tctx, "QueryDomainInformationPolicy failed - %s\n", nt_errstr(r.out.result));
3035                         ret = false;
3036                         continue;
3037                 }
3038         }
3039
3040         return ret;
3041 }
3042
3043
3044 static bool test_QueryInfoPolicyCalls(  bool version2,
3045                                         struct dcerpc_binding_handle *b,
3046                                         struct torture_context *tctx,
3047                                         struct policy_handle *handle)
3048 {
3049         struct lsa_QueryInfoPolicy r;
3050         union lsa_PolicyInformation *info = NULL;
3051         int i;
3052         bool ret = true;
3053         const char *call = talloc_asprintf(tctx, "QueryInfoPolicy%s", version2 ? "2":"");
3054
3055         torture_comment(tctx, "\nTesting %s\n", call);
3056
3057         if (version2 && torture_setting_bool(tctx, "samba3", false)) {
3058                 torture_skip(tctx, "skipping QueryInfoPolicy2 tests\n");
3059         }
3060
3061         for (i=1;i<=14;i++) {
3062                 r.in.handle = handle;
3063                 r.in.level = i;
3064                 r.out.info = &info;
3065
3066                 torture_comment(tctx, "\nTrying %s level %d\n", call, i);
3067
3068                 if (version2)
3069                         /* We can perform the cast, because both types are
3070                            structurally equal */
3071                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryInfoPolicy2_r(b, tctx,
3072                                  (struct lsa_QueryInfoPolicy2*) &r),
3073                                  "QueryInfoPolicy2 failed");
3074                 else
3075                         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryInfoPolicy_r(b, tctx, &r),
3076                                 "QueryInfoPolicy2 failed");
3077
3078                 switch (i) {
3079                 case LSA_POLICY_INFO_MOD:
3080                 case LSA_POLICY_INFO_AUDIT_FULL_SET:
3081                 case LSA_POLICY_INFO_AUDIT_FULL_QUERY:
3082                         if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_INVALID_PARAMETER)) {
3083                                 torture_comment(tctx, "Server should have failed level %u: %s\n", i, nt_errstr(r.out.result));
3084                                 ret = false;
3085                         }
3086                         break;
3087                 case LSA_POLICY_INFO_DOMAIN:
3088                 case LSA_POLICY_INFO_ACCOUNT_DOMAIN:
3089                 case LSA_POLICY_INFO_REPLICA:
3090                 case LSA_POLICY_INFO_QUOTA:
3091                 case LSA_POLICY_INFO_ROLE:
3092                 case LSA_POLICY_INFO_AUDIT_LOG:
3093                 case LSA_POLICY_INFO_AUDIT_EVENTS:
3094                 case LSA_POLICY_INFO_PD:
3095                         if (!NT_STATUS_IS_OK(r.out.result)) {
3096                                 torture_comment(tctx, "%s failed - %s\n", call, nt_errstr(r.out.result));
3097                                 ret = false;
3098                         }
3099                         break;
3100                 case LSA_POLICY_INFO_L_ACCOUNT_DOMAIN:
3101                 case LSA_POLICY_INFO_DNS_INT:
3102                 case LSA_POLICY_INFO_DNS:
3103                         if (torture_setting_bool(tctx, "samba3", false)) {
3104                                 /* Other levels not implemented yet */
3105                                 if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_INVALID_INFO_CLASS)) {
3106                                         torture_comment(tctx, "%s failed - %s\n", call, nt_errstr(r.out.result));
3107                                         ret = false;
3108                                 }
3109                         } else if (!NT_STATUS_IS_OK(r.out.result)) {
3110                                 torture_comment(tctx, "%s failed - %s\n", call, nt_errstr(r.out.result));
3111                                 ret = false;
3112                         }
3113                         break;
3114                 default:
3115                         if (torture_setting_bool(tctx, "samba4", false)) {
3116                                 /* Other levels not implemented yet */
3117                                 if (!NT_STATUS_EQUAL(r.out.result, NT_STATUS_INVALID_INFO_CLASS)) {
3118                                         torture_comment(tctx, "%s failed - %s\n", call, nt_errstr(r.out.result));
3119                                         ret = false;
3120                                 }
3121                         } else if (!NT_STATUS_IS_OK(r.out.result)) {
3122                                 torture_comment(tctx, "%s failed - %s\n", call, nt_errstr(r.out.result));
3123                                 ret = false;
3124                         }
3125                         break;
3126                 }
3127
3128                 if (NT_STATUS_IS_OK(r.out.result) && (i == LSA_POLICY_INFO_DNS
3129                         || i == LSA_POLICY_INFO_DNS_INT)) {
3130                         /* Let's look up some of these names */
3131
3132                         struct lsa_TransNameArray tnames;
3133                         tnames.count = 14;
3134                         tnames.names = talloc_zero_array(tctx, struct lsa_TranslatedName, tnames.count);
3135                         tnames.names[0].name.string = info->dns.name.string;
3136                         tnames.names[0].sid_type = SID_NAME_DOMAIN;
3137                         tnames.names[1].name.string = info->dns.dns_domain.string;
3138                         tnames.names[1].sid_type = SID_NAME_DOMAIN;
3139                         tnames.names[2].name.string = talloc_asprintf(tctx, "%s\\", info->dns.name.string);
3140                         tnames.names[2].sid_type = SID_NAME_DOMAIN;
3141                         tnames.names[3].name.string = talloc_asprintf(tctx, "%s\\", info->dns.dns_domain.string);
3142                         tnames.names[3].sid_type = SID_NAME_DOMAIN;
3143                         tnames.names[4].name.string = talloc_asprintf(tctx, "%s\\guest", info->dns.name.string);
3144                         tnames.names[4].sid_type = SID_NAME_USER;
3145                         tnames.names[5].name.string = talloc_asprintf(tctx, "%s\\krbtgt", info->dns.name.string);
3146                         tnames.names[5].sid_type = SID_NAME_USER;
3147                         tnames.names[6].name.string = talloc_asprintf(tctx, "%s\\guest", info->dns.dns_domain.string);
3148                         tnames.names[6].sid_type = SID_NAME_USER;
3149                         tnames.names[7].name.string = talloc_asprintf(tctx, "%s\\krbtgt", info->dns.dns_domain.string);
3150                         tnames.names[7].sid_type = SID_NAME_USER;
3151                         tnames.names[8].name.string = talloc_asprintf(tctx, "krbtgt@%s", info->dns.name.string);
3152                         tnames.names[8].sid_type = SID_NAME_USER;
3153                         tnames.names[9].name.string = talloc_asprintf(tctx, "krbtgt@%s", info->dns.dns_domain.string);
3154                         tnames.names[9].sid_type = SID_NAME_USER;
3155                         tnames.names[10].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", info->dns.name.string);
3156                         tnames.names[10].sid_type = SID_NAME_USER;
3157                         tnames.names[11].name.string = talloc_asprintf(tctx, "%s\\"TEST_MACHINENAME "$", info->dns.dns_domain.string);
3158                         tnames.names[11].sid_type = SID_NAME_USER;
3159                         tnames.names[12].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", info->dns.name.string);
3160                         tnames.names[12].sid_type = SID_NAME_USER;
3161                         tnames.names[13].name.string = talloc_asprintf(tctx, TEST_MACHINENAME "$@%s", info->dns.dns_domain.string);
3162                         tnames.names[13].sid_type = SID_NAME_USER;
3163                         ret &= test_LookupNames(b, tctx, handle, &tnames);
3164
3165                 }
3166         }
3167
3168         return ret;
3169 }
3170
3171 static bool test_QueryInfoPolicy(struct dcerpc_binding_handle *b,
3172                                  struct torture_context *tctx,
3173                                  struct policy_handle *handle)
3174 {
3175         return test_QueryInfoPolicyCalls(false, b, tctx, handle);
3176 }
3177
3178 static bool test_QueryInfoPolicy2(struct dcerpc_binding_handle *b,
3179                                   struct torture_context *tctx,
3180                                   struct policy_handle *handle)
3181 {
3182         return test_QueryInfoPolicyCalls(true, b, tctx, handle);
3183 }
3184
3185 static bool test_GetUserName(struct dcerpc_binding_handle *b,
3186                              struct torture_context *tctx)
3187 {
3188         struct lsa_GetUserName r;
3189         bool ret = true;
3190         struct lsa_String *authority_name_p = NULL;
3191         struct lsa_String *account_name_p = NULL;
3192
3193         torture_comment(tctx, "\nTesting GetUserName\n");
3194
3195         r.in.system_name        = "\\";
3196         r.in.account_name       = &account_name_p;
3197         r.in.authority_name     = NULL;
3198         r.out.account_name      = &account_name_p;
3199
3200         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_GetUserName_r(b, tctx, &r),
3201                 "GetUserName failed");
3202
3203         if (!NT_STATUS_IS_OK(r.out.result)) {
3204                 torture_comment(tctx, "GetUserName failed - %s\n", nt_errstr(r.out.result));
3205                 ret = false;
3206         }
3207
3208         account_name_p = NULL;
3209         r.in.account_name       = &account_name_p;
3210         r.in.authority_name     = &authority_name_p;
3211         r.out.account_name      = &account_name_p;
3212
3213         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_GetUserName_r(b, tctx, &r),
3214                 "GetUserName failed");
3215
3216         if (!NT_STATUS_IS_OK(r.out.result)) {
3217                 torture_comment(tctx, "GetUserName failed - %s\n", nt_errstr(r.out.result));
3218                 ret = false;
3219         }
3220
3221         return ret;
3222 }
3223
3224 static bool test_GetUserName_fail(struct dcerpc_binding_handle *b,
3225                                   struct torture_context *tctx)
3226 {
3227         struct lsa_GetUserName r;
3228         struct lsa_String *account_name_p = NULL;
3229         NTSTATUS status;
3230
3231         torture_comment(tctx, "\nTesting GetUserName_fail\n");
3232
3233         r.in.system_name        = "\\";
3234         r.in.account_name       = &account_name_p;
3235         r.in.authority_name     = NULL;
3236         r.out.account_name      = &account_name_p;
3237
3238         status = dcerpc_lsa_GetUserName_r(b, tctx, &r);
3239         if (!NT_STATUS_IS_OK(status)) {
3240                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3241                         torture_comment(tctx,
3242                                         "GetUserName correctly returned with "
3243                                         "status: %s\n",
3244                                         nt_errstr(status));
3245                         return true;
3246                 }
3247
3248                 torture_assert_ntstatus_equal(tctx,
3249                                               status,
3250                                               NT_STATUS_ACCESS_DENIED,
3251                                               "GetUserName return value should "
3252                                               "be ACCESS_DENIED");
3253                 return true;
3254         }
3255
3256         if (!NT_STATUS_IS_OK(r.out.result)) {
3257                 if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_ACCESS_DENIED) ||
3258                     NT_STATUS_EQUAL(r.out.result, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
3259                         torture_comment(tctx,
3260                                         "GetUserName correctly returned with "
3261                                         "result: %s\n",
3262                                         nt_errstr(r.out.result));
3263                         return true;
3264                 }
3265         }
3266
3267         torture_assert_ntstatus_equal(tctx,
3268                                       r.out.result,
3269                                       NT_STATUS_OK,
3270                                       "GetUserName return value should be "
3271                                       "ACCESS_DENIED");
3272
3273         return false;
3274 }
3275
3276 bool test_lsa_Close(struct dcerpc_binding_handle *b,
3277                     struct torture_context *tctx,
3278                     struct policy_handle *handle)
3279 {
3280         struct lsa_Close r;
3281         struct policy_handle handle2;
3282
3283         torture_comment(tctx, "\nTesting Close\n");
3284
3285         r.in.handle = handle;
3286         r.out.handle = &handle2;
3287
3288         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_Close_r(b, tctx, &r),
3289                 "Close failed");
3290         torture_assert_ntstatus_ok(tctx, r.out.result,
3291                 "Close failed");
3292
3293         torture_assert_ntstatus_equal(tctx, dcerpc_lsa_Close_r(b, tctx, &r),
3294                 NT_STATUS_RPC_SS_CONTEXT_MISMATCH, "Close should failed");
3295
3296         torture_comment(tctx, "\n");
3297
3298         return true;
3299 }
3300
3301 bool torture_rpc_lsa(struct torture_context *tctx)
3302 {
3303         NTSTATUS status;
3304         struct dcerpc_pipe *p;
3305         bool ret = true;
3306         struct policy_handle *handle = NULL;
3307         struct test_join *join = NULL;
3308         struct cli_credentials *machine_creds;
3309         struct dcerpc_binding_handle *b;
3310
3311         status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
3312         if (!NT_STATUS_IS_OK(status)) {
3313                 return false;
3314         }
3315         b = p->binding_handle;
3316
3317         /* Test lsaLookupSids3 and lsaLookupNames4 over tcpip */
3318         if (p->binding->transport == NCACN_IP_TCP) {
3319                 if (!test_OpenPolicy_fail(b, tctx)) {
3320                         ret = false;
3321                 }
3322
3323                 if (!test_OpenPolicy2_fail(b, tctx)) {
3324                         ret = false;
3325                 }
3326
3327                 if (!test_many_LookupSids(p, tctx, handle)) {
3328                         ret = false;
3329                 }
3330
3331                 return ret;
3332         }
3333
3334         if (!test_OpenPolicy(b, tctx)) {
3335                 ret = false;
3336         }
3337
3338         if (!test_lsa_OpenPolicy2(b, tctx, &handle)) {
3339                 ret = false;
3340         }
3341
3342         if (handle) {
3343                 join = torture_join_domain(tctx, TEST_MACHINENAME, ACB_WSTRUST, &machine_creds);
3344                 if (!join) {
3345                         ret = false;
3346                 }
3347
3348                 if (!test_LookupSids_async(b, tctx, handle)) {
3349                         ret = false;
3350                 }
3351
3352                 if (!test_QueryDomainInfoPolicy(b, tctx, handle)) {
3353                         ret = false;
3354                 }
3355
3356                 if (!test_CreateSecret(p, tctx, handle)) {
3357                         ret = false;
3358                 }
3359
3360                 if (!test_QueryInfoPolicy(b, tctx, handle)) {
3361                         ret = false;
3362                 }
3363
3364                 if (!test_QueryInfoPolicy2(b, tctx, handle)) {
3365                         ret = false;
3366                 }
3367
3368                 if (!test_Delete(b, tctx, handle)) {
3369                         ret = false;
3370                 }
3371
3372                 if (!test_many_LookupSids(p, tctx, handle)) {
3373                         ret = false;
3374                 }
3375
3376                 if (!test_lsa_Close(b, tctx, handle)) {
3377                         ret = false;
3378                 }
3379
3380                 torture_leave_domain(tctx, join);
3381
3382         } else {
3383                 if (!test_many_LookupSids(p, tctx, handle)) {
3384                         ret = false;
3385                 }
3386         }
3387
3388         if (!test_GetUserName(b, tctx)) {
3389                 ret = false;
3390         }
3391
3392         return ret;
3393 }
3394
3395 bool torture_rpc_lsa_get_user(struct torture_context *tctx)
3396 {
3397         NTSTATUS status;
3398         struct dcerpc_pipe *p;
3399         bool ret = true;
3400         struct dcerpc_binding_handle *b;
3401
3402         status = torture_rpc_connection(tctx, &p, &ndr_table_lsarpc);
3403         if (!NT_STATUS_IS_OK(status)) {
3404                 return false;
3405         }
3406         b = p->binding_handle;
3407
3408         if (p->binding->transport == NCACN_IP_TCP) {
3409                 if (!test_GetUserName_fail(b, tctx)) {
3410                         ret = false;
3411                 }
3412                 return ret;
3413         }
3414
3415         if (!test_GetUserName(b, tctx)) {
3416                 ret = false;
3417         }
3418
3419         return ret;
3420 }
3421
3422 static bool testcase_LookupNames(struct torture_context *tctx,
3423                                  struct dcerpc_pipe *p)
3424 {
3425         bool ret = true;
3426         struct policy_handle *handle;
3427         struct lsa_TransNameArray tnames;
3428         struct lsa_TransNameArray2 tnames2;
3429         struct dcerpc_binding_handle *b = p->binding_handle;
3430
3431         if (p->binding->transport != NCACN_NP &&
3432             p->binding->transport != NCALRPC) {
3433                 torture_comment(tctx, "testcase_LookupNames is only available "
3434                                 "over NCACN_NP or NCALRPC");
3435                 return true;
3436         }
3437
3438         if (!test_OpenPolicy(b, tctx)) {
3439                 ret = false;
3440         }
3441
3442         if (!test_lsa_OpenPolicy2(b, tctx, &handle)) {
3443                 ret = false;
3444         }
3445
3446         if (!handle) {
3447                 ret = false;
3448         }
3449
3450         tnames.count = 1;
3451         tnames.names = talloc_array(tctx, struct lsa_TranslatedName, tnames.count);
3452         ZERO_STRUCT(tnames.names[0]);
3453         tnames.names[0].name.string = "BUILTIN";
3454         tnames.names[0].sid_type = SID_NAME_DOMAIN;
3455
3456         if (!test_LookupNames(b, tctx, handle, &tnames)) {
3457                 ret = false;
3458         }
3459
3460         tnames2.count = 1;
3461         tnames2.names = talloc_array(tctx, struct lsa_TranslatedName2, tnames2.count);
3462         ZERO_STRUCT(tnames2.names[0]);
3463         tnames2.names[0].name.string = "BUILTIN";
3464         tnames2.names[0].sid_type = SID_NAME_DOMAIN;
3465
3466         if (!test_LookupNames2(b, tctx, handle, &tnames2, true)) {
3467                 ret = false;
3468         }
3469
3470         if (!test_LookupNames3(b, tctx, handle, &tnames2, true)) {
3471                 ret = false;
3472         }
3473
3474         if (!test_LookupNames_wellknown(b, tctx, handle)) {
3475                 ret = false;
3476         }
3477
3478         if (!test_LookupNames_NULL(b, tctx, handle)) {
3479                 ret = false;
3480         }
3481
3482         if (!test_LookupNames_bogus(b, tctx, handle)) {
3483                 ret = false;
3484         }
3485
3486         if (!test_lsa_Close(b, tctx, handle)) {
3487                 ret = false;
3488         }
3489
3490         return ret;
3491 }
3492
3493 struct torture_suite *torture_rpc_lsa_lookup_names(TALLOC_CTX *mem_ctx)
3494 {
3495         struct torture_suite *suite;
3496         struct torture_rpc_tcase *tcase;
3497
3498         suite = torture_suite_create(mem_ctx, "lsa.lookupnames");
3499
3500         tcase = torture_suite_add_rpc_iface_tcase(suite, "lsa",
3501                                                   &ndr_table_lsarpc);
3502         torture_rpc_tcase_add_test(tcase, "LookupNames",
3503                                    testcase_LookupNames);
3504
3505         return suite;
3506 }
3507
3508 struct lsa_trustdom_state {
3509         uint32_t num_trusts;
3510 };
3511
3512 static bool testcase_TrustedDomains(struct torture_context *tctx,
3513                                     struct dcerpc_pipe *p,
3514                                     void *data)
3515 {
3516         bool ret = true;
3517         struct policy_handle *handle;
3518         struct lsa_trustdom_state *state =
3519                 talloc_get_type_abort(data, struct lsa_trustdom_state);
3520         struct dcerpc_binding_handle *b = p->binding_handle;
3521
3522         if (p->binding->transport != NCACN_NP &&
3523             p->binding->transport != NCALRPC) {
3524                 torture_comment(tctx, "testcase_TrustedDomains is only available "
3525                                 "over NCACN_NP or NCALRPC");
3526                 return true;
3527         }
3528
3529         torture_comment(tctx, "Testing %d domains\n", state->num_trusts);
3530
3531         if (!test_OpenPolicy(b, tctx)) {
3532                 ret = false;
3533         }
3534
3535         if (!test_lsa_OpenPolicy2(b, tctx, &handle)) {
3536                 ret = false;
3537         }
3538
3539         if (!handle) {
3540                 ret = false;
3541         }
3542
3543         if (!test_CreateTrustedDomain(b, tctx, handle, state->num_trusts)) {
3544                 ret = false;
3545         }
3546
3547         if (!test_CreateTrustedDomainEx(p, tctx, handle, state->num_trusts)) {
3548                 ret = false;
3549         }
3550
3551         if (!test_CreateTrustedDomainEx2(p, tctx, handle, state->num_trusts)) {
3552                 ret = false;
3553         }
3554
3555         if (!test_lsa_Close(b, tctx, handle)) {
3556                 ret = false;
3557         }
3558
3559         return ret;
3560 }
3561
3562 struct torture_suite *torture_rpc_lsa_trusted_domains(TALLOC_CTX *mem_ctx)
3563 {
3564         struct torture_suite *suite;
3565         struct torture_rpc_tcase *tcase;
3566         struct lsa_trustdom_state *state;
3567
3568         state = talloc(mem_ctx, struct lsa_trustdom_state);
3569
3570         state->num_trusts = 12;
3571
3572         suite = torture_suite_create(mem_ctx, "lsa.trusted.domains");
3573
3574         tcase = torture_suite_add_rpc_iface_tcase(suite, "lsa",
3575                                                   &ndr_table_lsarpc);
3576         torture_rpc_tcase_add_test_ex(tcase, "TrustedDomains",
3577                                       testcase_TrustedDomains,
3578                                       state);
3579
3580         return suite;
3581 }
3582
3583 static bool testcase_Privileges(struct torture_context *tctx,
3584                                 struct dcerpc_pipe *p)
3585 {
3586         bool ret = true;
3587         struct policy_handle *handle;
3588         struct dcerpc_binding_handle *b = p->binding_handle;
3589
3590         if (p->binding->transport != NCACN_NP &&
3591             p->binding->transport != NCALRPC) {
3592                 torture_comment(tctx, "testcase_Privileges is only available "
3593                                 "over NCACN_NP or NCALRPC");
3594                 return true;
3595         }
3596
3597         if (!test_OpenPolicy(b, tctx)) {
3598                 ret = false;
3599         }
3600
3601         if (!test_lsa_OpenPolicy2(b, tctx, &handle)) {
3602                 ret = false;
3603         }
3604
3605         if (!handle) {
3606                 ret = false;
3607         }
3608
3609         if (!test_CreateAccount(b, tctx, handle)) {
3610                 ret = false;
3611         }
3612
3613         if (!test_EnumAccounts(b, tctx, handle)) {
3614                 ret = false;
3615         }
3616
3617         if (!test_EnumPrivs(b, tctx, handle)) {
3618                 ret = false;
3619         }
3620
3621         if (!test_lsa_Close(b, tctx, handle)) {
3622                 ret = false;
3623         }
3624
3625         return ret;
3626 }
3627
3628
3629 struct torture_suite *torture_rpc_lsa_privileges(TALLOC_CTX *mem_ctx)
3630 {
3631         struct torture_suite *suite;
3632         struct torture_rpc_tcase *tcase;
3633
3634         suite = torture_suite_create(mem_ctx, "lsa.privileges");
3635
3636         tcase = torture_suite_add_rpc_iface_tcase(suite, "lsa",
3637                                                   &ndr_table_lsarpc);
3638         torture_rpc_tcase_add_test(tcase, "Privileges",
3639                                    testcase_Privileges);
3640
3641         return suite;
3642 }