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