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