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