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