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