r4698: - Initial implementation of trusted domains in LSA.
[kai/samba.git] / source4 / torture / rpc / lsa.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for lsa rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_lsa.h"
25
26 static void init_lsa_String(struct lsa_String *name, const char *s)
27 {
28         name->string = s;
29 }
30
31 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
32 {
33         struct lsa_ObjectAttribute attr;
34         struct policy_handle handle;
35         struct lsa_QosInfo qos;
36         struct lsa_OpenPolicy r;
37         NTSTATUS status;
38         uint16_t system_name = '\\';
39
40         printf("\ntesting OpenPolicy\n");
41
42         qos.len = 0;
43         qos.impersonation_level = 2;
44         qos.context_mode = 1;
45         qos.effective_only = 0;
46
47         attr.len = 0;
48         attr.root_dir = NULL;
49         attr.object_name = NULL;
50         attr.attributes = 0;
51         attr.sec_desc = NULL;
52         attr.sec_qos = &qos;
53
54         r.in.system_name = &system_name;
55         r.in.attr = &attr;
56         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
57         r.out.handle = &handle;
58
59         status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
60         if (!NT_STATUS_IS_OK(status)) {
61                 printf("OpenPolicy failed - %s\n", nt_errstr(status));
62                 return False;
63         }
64
65         return True;
66 }
67
68
69 BOOL test_lsa_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
70                           struct policy_handle *handle)
71 {
72         struct lsa_ObjectAttribute attr;
73         struct lsa_QosInfo qos;
74         struct lsa_OpenPolicy2 r;
75         NTSTATUS status;
76
77         printf("\ntesting OpenPolicy2\n");
78
79         qos.len = 0;
80         qos.impersonation_level = 2;
81         qos.context_mode = 1;
82         qos.effective_only = 0;
83
84         attr.len = 0;
85         attr.root_dir = NULL;
86         attr.object_name = NULL;
87         attr.attributes = 0;
88         attr.sec_desc = NULL;
89         attr.sec_qos = &qos;
90
91         r.in.system_name = "\\";
92         r.in.attr = &attr;
93         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
94         r.out.handle = handle;
95
96         status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
97         if (!NT_STATUS_IS_OK(status)) {
98                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
99                 return False;
100         }
101
102         return True;
103 }
104
105 static BOOL test_LookupNames(struct dcerpc_pipe *p, 
106                             TALLOC_CTX *mem_ctx, 
107                             struct policy_handle *handle,
108                             struct lsa_TransNameArray *tnames)
109 {
110         struct lsa_LookupNames r;
111         struct lsa_TransSidArray sids;
112         struct lsa_String *names;
113         uint32_t count = 0;
114         NTSTATUS status;
115         int i;
116
117         printf("\nTesting LookupNames with %d names\n", tnames->count);
118
119         sids.count = 0;
120         sids.sids = NULL;
121
122         names = talloc_array_p(mem_ctx, struct lsa_String, tnames->count);
123         for (i=0;i<tnames->count;i++) {
124                 init_lsa_String(&names[i], tnames->names[i].name.string);
125         }
126
127         r.in.handle = handle;
128         r.in.num_names = tnames->count;
129         r.in.names = names;
130         r.in.sids = &sids;
131         r.in.level = 1;
132         r.in.count = &count;
133         r.out.count = &count;
134         r.out.sids = &sids;
135
136         status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
137         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
138                 printf("LookupNames failed - %s\n", nt_errstr(status));
139                 return False;
140         }
141
142         printf("\n");
143
144         return True;
145 }
146
147 static BOOL test_LookupNames2(struct dcerpc_pipe *p, 
148                               TALLOC_CTX *mem_ctx, 
149                               struct policy_handle *handle,
150                               struct lsa_TransNameArray2 *tnames)
151 {
152         struct lsa_LookupNames2 r;
153         struct lsa_TransSidArray2 sids;
154         struct lsa_String *names;
155         uint32_t count = 0;
156         NTSTATUS status;
157         int i;
158
159         printf("\nTesting LookupNames2 with %d names\n", tnames->count);
160
161         sids.count = 0;
162         sids.sids = NULL;
163
164         names = talloc_array_p(mem_ctx, struct lsa_String, tnames->count);
165         for (i=0;i<tnames->count;i++) {
166                 init_lsa_String(&names[i], tnames->names[i].name.string);
167         }
168
169         r.in.handle = handle;
170         r.in.num_names = tnames->count;
171         r.in.names = names;
172         r.in.sids = &sids;
173         r.in.level = 1;
174         r.in.count = &count;
175         r.in.unknown1 = 0;
176         r.in.unknown2 = 0;
177         r.out.count = &count;
178         r.out.sids = &sids;
179
180         status = dcerpc_lsa_LookupNames2(p, mem_ctx, &r);
181         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
182                 printf("LookupNames2 failed - %s\n", nt_errstr(status));
183                 return False;
184         }
185
186         printf("\n");
187
188         return True;
189 }
190
191
192 static BOOL test_LookupNames3(struct dcerpc_pipe *p, 
193                               TALLOC_CTX *mem_ctx, 
194                               struct policy_handle *handle,
195                               struct lsa_TransNameArray2 *tnames)
196 {
197         struct lsa_LookupNames3 r;
198         struct lsa_TransSidArray3 sids;
199         struct lsa_String *names;
200         uint32_t count = 0;
201         NTSTATUS status;
202         int i;
203
204         printf("\nTesting LookupNames3 with %d names\n", tnames->count);
205
206         sids.count = 0;
207         sids.sids = NULL;
208
209         names = talloc_array_p(mem_ctx, struct lsa_String, tnames->count);
210         for (i=0;i<tnames->count;i++) {
211                 init_lsa_String(&names[i], tnames->names[i].name.string);
212         }
213
214         r.in.handle = handle;
215         r.in.num_names = tnames->count;
216         r.in.names = names;
217         r.in.sids = &sids;
218         r.in.level = 1;
219         r.in.count = &count;
220         r.in.unknown1 = 0;
221         r.in.unknown2 = 0;
222         r.out.count = &count;
223         r.out.sids = &sids;
224
225         status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
226         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
227                 printf("LookupNames3 failed - %s\n", nt_errstr(status));
228                 return False;
229         }
230
231         printf("\n");
232
233         return True;
234 }
235
236
237 static BOOL test_LookupSids(struct dcerpc_pipe *p, 
238                             TALLOC_CTX *mem_ctx, 
239                             struct policy_handle *handle,
240                             struct lsa_SidArray *sids)
241 {
242         struct lsa_LookupSids r;
243         struct lsa_TransNameArray names;
244         uint32_t count = sids->num_sids;
245         NTSTATUS status;
246
247         printf("\nTesting LookupSids\n");
248
249         names.count = 0;
250         names.names = NULL;
251
252         r.in.handle = handle;
253         r.in.sids = sids;
254         r.in.names = &names;
255         r.in.level = 1;
256         r.in.count = &count;
257         r.out.count = &count;
258         r.out.names = &names;
259
260         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
261         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
262                 printf("LookupSids failed - %s\n", nt_errstr(status));
263                 return False;
264         }
265
266         printf("\n");
267
268         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
269                 return False;
270         }
271
272         return True;
273 }
274
275
276 static BOOL test_LookupSids2(struct dcerpc_pipe *p, 
277                             TALLOC_CTX *mem_ctx, 
278                             struct policy_handle *handle,
279                             struct lsa_SidArray *sids)
280 {
281         struct lsa_LookupSids2 r;
282         struct lsa_TransNameArray2 names;
283         uint32_t count = sids->num_sids;
284         NTSTATUS status;
285
286         printf("\nTesting LookupSids2\n");
287
288         names.count = 0;
289         names.names = NULL;
290
291         r.in.handle = handle;
292         r.in.sids = sids;
293         r.in.names = &names;
294         r.in.level = 1;
295         r.in.count = &count;
296         r.in.unknown1 = 0;
297         r.in.unknown2 = 0;
298         r.out.count = &count;
299         r.out.names = &names;
300
301         status = dcerpc_lsa_LookupSids2(p, mem_ctx, &r);
302         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
303                 printf("LookupSids2 failed - %s\n", nt_errstr(status));
304                 return False;
305         }
306
307         printf("\n");
308
309         if (!test_LookupNames2(p, mem_ctx, handle, &names)) {
310                 return False;
311         }
312
313         if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
314                 return False;
315         }
316
317         return True;
318 }
319
320 static BOOL test_LookupSids3(struct dcerpc_pipe *p, 
321                             TALLOC_CTX *mem_ctx, 
322                             struct policy_handle *handle,
323                             struct lsa_SidArray *sids)
324 {
325         struct lsa_LookupSids3 r;
326         struct lsa_TransNameArray2 names;
327         uint32_t count = sids->num_sids;
328         NTSTATUS status;
329
330         printf("\nTesting LookupSids3\n");
331
332         names.count = 0;
333         names.names = NULL;
334
335         r.in.sids = sids;
336         r.in.names = &names;
337         r.in.level = 1;
338         r.in.count = &count;
339         r.in.unknown1 = 0;
340         r.in.unknown2 = 0;
341         r.out.count = &count;
342         r.out.names = &names;
343
344         status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
345         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
346                 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
347                     NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
348                         printf("not considering %s to be an error\n", nt_errstr(status));
349                         return True;
350                 }
351                 printf("LookupSids3 failed - %s - not considered an error\n", 
352                        nt_errstr(status));
353                 return False;
354         }
355
356         printf("\n");
357
358         if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
359                 return False;
360         }
361
362         return True;
363 }
364
365 static BOOL test_many_LookupSids(struct dcerpc_pipe *p, 
366                                  TALLOC_CTX *mem_ctx, 
367                                  struct policy_handle *handle)
368 {
369         struct lsa_LookupSids r;
370         struct lsa_TransNameArray names;
371         uint32_t count;
372         NTSTATUS status;
373         struct lsa_SidArray sids;
374         int i;
375
376         printf("\nTesting LookupSids with lots of SIDs\n");
377
378         names.count = 0;
379         names.names = NULL;
380
381         sids.num_sids = 100;
382
383         sids.sids = talloc_array_p(mem_ctx, struct lsa_SidPtr, sids.num_sids);
384
385         for (i=0; i<sids.num_sids; i++) {
386                 const char *sidstr = "S-1-5-32-545";
387                 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
388         }
389
390         count = sids.num_sids;
391
392         r.in.handle = handle;
393         r.in.sids = &sids;
394         r.in.names = &names;
395         r.in.level = 1;
396         r.in.count = &names.count;
397         r.out.count = &count;
398         r.out.names = &names;
399
400         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
401         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
402                 printf("LookupSids failed - %s\n", nt_errstr(status));
403                 return False;
404         }
405
406         printf("\n");
407
408         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
409                 return False;
410         }
411
412         return True;
413 }
414
415 static BOOL test_LookupPrivValue(struct dcerpc_pipe *p, 
416                                  TALLOC_CTX *mem_ctx, 
417                                  struct policy_handle *handle,
418                                  struct lsa_String *name)
419 {
420         NTSTATUS status;
421         struct lsa_LookupPrivValue r;
422         struct lsa_LUID luid;
423
424         r.in.handle = handle;
425         r.in.name = name;
426         r.out.luid = &luid;
427
428         status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
429         if (!NT_STATUS_IS_OK(status)) {
430                 printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
431                 return False;
432         }
433
434         return True;
435 }
436
437 static BOOL test_LookupPrivName(struct dcerpc_pipe *p, 
438                                 TALLOC_CTX *mem_ctx, 
439                                 struct policy_handle *handle,
440                                 struct lsa_LUID *luid)
441 {
442         NTSTATUS status;
443         struct lsa_LookupPrivName r;
444
445         r.in.handle = handle;
446         r.in.luid = luid;
447
448         status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
449         if (!NT_STATUS_IS_OK(status)) {
450                 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
451                 return False;
452         }
453
454         return True;
455 }
456
457 static BOOL test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p, 
458                                              TALLOC_CTX *mem_ctx,                                 
459                                              struct policy_handle *acct_handle,
460                                              struct lsa_LUID *luid)
461 {
462         NTSTATUS status;
463         struct lsa_RemovePrivilegesFromAccount r;
464         struct lsa_PrivilegeSet privs;
465         BOOL ret = True;
466
467         printf("Testing RemovePrivilegesFromAccount\n");
468
469         r.in.handle = acct_handle;
470         r.in.remove_all = 0;
471         r.in.privs = &privs;
472
473         privs.count = 1;
474         privs.unknown = 0;
475         privs.set = talloc_array_p(mem_ctx, struct lsa_LUIDAttribute, 1);
476         privs.set[0].luid = *luid;
477         privs.set[0].attribute = 0;
478
479         status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
480         if (!NT_STATUS_IS_OK(status)) {
481                 printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
482                 return False;
483         }
484
485         return ret;
486 }
487
488 static BOOL test_AddPrivilegesToAccount(struct dcerpc_pipe *p, 
489                                         TALLOC_CTX *mem_ctx,                              
490                                         struct policy_handle *acct_handle,
491                                         struct lsa_LUID *luid)
492 {
493         NTSTATUS status;
494         struct lsa_AddPrivilegesToAccount r;
495         struct lsa_PrivilegeSet privs;
496         BOOL ret = True;
497
498         printf("Testing AddPrivilegesToAccount\n");
499
500         r.in.handle = acct_handle;
501         r.in.privs = &privs;
502
503         privs.count = 1;
504         privs.unknown = 0;
505         privs.set = talloc_array_p(mem_ctx, struct lsa_LUIDAttribute, 1);
506         privs.set[0].luid = *luid;
507         privs.set[0].attribute = 0;
508
509         status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
510         if (!NT_STATUS_IS_OK(status)) {
511                 printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
512                 return False;
513         }
514
515         return ret;
516 }
517
518 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p, 
519                                   TALLOC_CTX *mem_ctx,                            
520                                   struct policy_handle *handle,
521                                   struct policy_handle *acct_handle)
522 {
523         NTSTATUS status;
524         struct lsa_EnumPrivsAccount r;
525         BOOL ret = True;
526
527         printf("Testing EnumPrivsAccount\n");
528
529         r.in.handle = acct_handle;
530
531         status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
532         if (!NT_STATUS_IS_OK(status)) {
533                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
534                 return False;
535         }
536
537         if (r.out.privs && r.out.privs->count > 0) {
538                 int i;
539                 for (i=0;i<r.out.privs->count;i++) {
540                         test_LookupPrivName(p, mem_ctx, handle, 
541                                             &r.out.privs->set[i].luid);
542                 }
543
544                 ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle, 
545                                                         &r.out.privs->set[0].luid);
546                 ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle, 
547                                                    &r.out.privs->set[0].luid);
548         }
549
550         return ret;
551 }
552
553 static BOOL test_Delete(struct dcerpc_pipe *p, 
554                        TALLOC_CTX *mem_ctx, 
555                        struct policy_handle *handle)
556 {
557         NTSTATUS status;
558         struct lsa_Delete r;
559
560         printf("\ntesting Delete\n");
561
562         r.in.handle = handle;
563         status = dcerpc_lsa_Delete(p, mem_ctx, &r);
564         if (!NT_STATUS_IS_OK(status)) {
565                 printf("Delete failed - %s\n", nt_errstr(status));
566                 return False;
567         }
568
569         printf("\n");
570
571         return True;
572 }
573
574
575 static BOOL test_CreateAccount(struct dcerpc_pipe *p, 
576                                TALLOC_CTX *mem_ctx, 
577                                struct policy_handle *handle)
578 {
579         NTSTATUS status;
580         struct lsa_CreateAccount r;
581         struct dom_sid2 *newsid;
582         struct policy_handle acct_handle;
583
584         newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
585
586         printf("Testing CreateAccount\n");
587
588         r.in.handle = handle;
589         r.in.sid = newsid;
590         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
591         r.out.acct_handle = &acct_handle;
592
593         status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
594         if (!NT_STATUS_IS_OK(status)) {
595                 printf("CreateAccount failed - %s\n", nt_errstr(status));
596                 return False;
597         }
598
599         if (!test_Delete(p, mem_ctx, &acct_handle)) {
600                 return False;
601         }
602
603         return True;
604 }
605
606 static BOOL test_DeleteTrustedDomain(struct dcerpc_pipe *p, 
607                                      TALLOC_CTX *mem_ctx, 
608                                      struct policy_handle *handle,
609                                      struct lsa_String name)
610 {
611         NTSTATUS status;
612         struct lsa_OpenTrustedDomainByName r;
613         struct policy_handle trustdom_handle;
614
615         r.in.handle = handle;
616         r.in.name = name;
617         r.in.access_mask = SEC_STD_DELETE;
618         r.out.trustdom_handle = &trustdom_handle;
619
620         status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
621         if (!NT_STATUS_IS_OK(status)) {
622                 printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
623                 return False;
624         }
625
626         if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
627                 return False;
628         }
629
630         return True;
631 }
632
633
634 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p, 
635                                      TALLOC_CTX *mem_ctx, 
636                                      struct policy_handle *handle)
637 {
638         NTSTATUS status;
639         struct lsa_CreateTrustedDomain r;
640         struct lsa_TrustInformation trustinfo;
641         struct dom_sid *domsid;
642         struct policy_handle trustdom_handle;
643
644         printf("Testing CreateTrustedDomain\n");
645
646         domsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-21-97398-379795-12345");
647
648         trustinfo.sid = domsid;
649         init_lsa_String(&trustinfo.name, "torturedomain");
650
651         r.in.handle = handle;
652         r.in.info = &trustinfo;
653         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
654         r.out.trustdom_handle = &trustdom_handle;
655
656         status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
657         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
658                 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
659                 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
660         }
661         if (!NT_STATUS_IS_OK(status)) {
662                 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
663                 return False;
664         }
665
666         if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
667                 return False;
668         }
669
670         return True;
671 }
672
673 static BOOL test_CreateSecret(struct dcerpc_pipe *p, 
674                               TALLOC_CTX *mem_ctx, 
675                               struct policy_handle *handle)
676 {
677         NTSTATUS status;
678         struct lsa_CreateSecret r;
679         struct lsa_OpenSecret r2;
680         struct lsa_SetSecret r3;
681         struct lsa_QuerySecret r4;
682         struct lsa_SetSecret r5;
683         struct lsa_QuerySecret r6;
684         struct lsa_SetSecret r7;
685         struct lsa_QuerySecret r8;
686         struct policy_handle sec_handle, sec_handle2, sec_handle3;
687         struct lsa_Delete d;
688         struct lsa_DATA_BUF buf1;
689         struct lsa_DATA_BUF_PTR bufp1;
690         struct lsa_DATA_BUF_PTR bufp2;
691         DATA_BLOB enc_key;
692         BOOL ret = True;
693         DATA_BLOB session_key;
694         NTTIME old_mtime, new_mtime;
695         DATA_BLOB blob1, blob2;
696         const char *secret1 = "abcdef12345699qwerty";
697         char *secret2;
698         const char *secret3 = "ABCDEF12345699QWERTY";
699         char *secret4;
700         const char *secret5 = "NEW-SAMBA4-SECRET";
701         char *secret6;
702         char *secname[2];
703         int i;
704         const int LOCAL = 0;
705         const int GLOBAL = 1;
706
707         secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
708         secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
709
710         for (i=0; i< 2; i++) {
711                 printf("Testing CreateSecret of %s\n", secname[i]);
712                 
713                 init_lsa_String(&r.in.name, secname[i]);
714                 
715                 r.in.handle = handle;
716                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
717                 r.out.sec_handle = &sec_handle;
718                 
719                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
720                 if (!NT_STATUS_IS_OK(status)) {
721                         printf("CreateSecret failed - %s\n", nt_errstr(status));
722                         return False;
723                 }
724                 
725                 r.in.handle = handle;
726                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
727                 r.out.sec_handle = &sec_handle3;
728                 
729                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
730                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
731                         printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
732                         return False;
733                 }
734                 
735                 r2.in.handle = handle;
736                 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
737                 r2.in.name = r.in.name;
738                 r2.out.sec_handle = &sec_handle2;
739                 
740                 printf("Testing OpenSecret\n");
741                 
742                 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
743                 if (!NT_STATUS_IS_OK(status)) {
744                         printf("OpenSecret failed - %s\n", nt_errstr(status));
745                         ret = False;
746                 }
747                 
748                 status = dcerpc_fetch_session_key(p, &session_key);
749                 if (!NT_STATUS_IS_OK(status)) {
750                         printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
751                         ret = False;
752                 }
753                 
754                 enc_key = sess_encrypt_string(secret1, &session_key);
755                 
756                 r3.in.sec_handle = &sec_handle;
757                 r3.in.new_val = &buf1;
758                 r3.in.old_val = NULL;
759                 r3.in.new_val->data = enc_key.data;
760                 r3.in.new_val->length = enc_key.length;
761                 r3.in.new_val->size = enc_key.length;
762                 
763                 printf("Testing SetSecret\n");
764                 
765                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
766                 if (!NT_STATUS_IS_OK(status)) {
767                         printf("SetSecret failed - %s\n", nt_errstr(status));
768                         ret = False;
769                 }
770                 
771                 r3.in.sec_handle = &sec_handle;
772                 r3.in.new_val = &buf1;
773                 r3.in.old_val = NULL;
774                 r3.in.new_val->data = enc_key.data;
775                 r3.in.new_val->length = enc_key.length;
776                 r3.in.new_val->size = enc_key.length;
777                 
778                 /* break the encrypted data */
779                 enc_key.data[0]++;
780
781                 printf("Testing SetSecret with broken key\n");
782                 
783                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
784                 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
785                         printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
786                         ret = False;
787                 }
788                 
789                 data_blob_free(&enc_key);
790                 
791                 ZERO_STRUCT(new_mtime);
792                 ZERO_STRUCT(old_mtime);
793                 
794                 /* fetch the secret back again */
795                 r4.in.sec_handle = &sec_handle;
796                 r4.in.new_val = &bufp1;
797                 r4.in.new_mtime = &new_mtime;
798                 r4.in.old_val = NULL;
799                 r4.in.old_mtime = NULL;
800                 
801                 bufp1.buf = NULL;
802                 
803                 printf("Testing QuerySecret\n");
804                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
805                 if (!NT_STATUS_IS_OK(status)) {
806                         printf("QuerySecret failed - %s\n", nt_errstr(status));
807                         ret = False;
808                 } else {
809                         if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
810                                 printf("No secret buffer returned\n");
811                                 ret = False;
812                         } else {
813                                 blob1.data = r4.out.new_val->buf->data;
814                                 blob1.length = r4.out.new_val->buf->size;
815                                 
816                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
817                                 
818                                 secret2 = sess_decrypt_string(&blob1, &session_key);
819                                 
820                                 if (strcmp(secret1, secret2) != 0) {
821                                         printf("Returned secret '%s' doesn't match '%s'\n", 
822                                                secret2, secret1);
823                                         ret = False;
824                                 }
825                         }
826                 }
827                 
828                 enc_key = sess_encrypt_string(secret3, &session_key);
829                 
830                 r5.in.sec_handle = &sec_handle;
831                 r5.in.new_val = &buf1;
832                 r5.in.old_val = NULL;
833                 r5.in.new_val->data = enc_key.data;
834                 r5.in.new_val->length = enc_key.length;
835                 r5.in.new_val->size = enc_key.length;
836                 
837                 printf("Testing SetSecret (existing value should move to old)\n");
838                 
839                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
840                 if (!NT_STATUS_IS_OK(status)) {
841                         printf("SetSecret failed - %s\n", nt_errstr(status));
842                         ret = False;
843                 }
844                 
845                 data_blob_free(&enc_key);
846                 
847                 ZERO_STRUCT(new_mtime);
848                 ZERO_STRUCT(old_mtime);
849                 
850                 /* fetch the secret back again */
851                 r6.in.sec_handle = &sec_handle;
852                 r6.in.new_val = &bufp1;
853                 r6.in.new_mtime = &new_mtime;
854                 r6.in.old_val = &bufp2;
855                 r6.in.old_mtime = &old_mtime;
856                 
857                 bufp1.buf = NULL;
858                 bufp2.buf = NULL;
859                 
860                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
861                 if (!NT_STATUS_IS_OK(status)) {
862                         printf("QuerySecret failed - %s\n", nt_errstr(status));
863                         ret = False;
864                 } else {
865
866                         if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL 
867                                 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
868                                 printf("Both secret buffers and both times not returned\n");
869                                 ret = False;
870                         } else {
871                                 blob1.data = r6.out.new_val->buf->data;
872                                 blob1.length = r6.out.new_val->buf->size;
873                                 
874                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
875                                 
876                                 secret4 = sess_decrypt_string(&blob1, &session_key);
877                                 
878                                 if (strcmp(secret3, secret4) != 0) {
879                                         printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
880                                         ret = False;
881                                 }
882
883                                 blob1.data = r6.out.old_val->buf->data;
884                                 blob1.length = r6.out.old_val->buf->length;
885                                 
886                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
887                                 
888                                 secret2 = sess_decrypt_string(&blob1, &session_key);
889                                 
890                                 if (strcmp(secret1, secret2) != 0) {
891                                         printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
892                                         ret = False;
893                                 }
894                                 
895                                 if (*r6.out.new_mtime == *r6.out.old_mtime) {
896                                         printf("Returned secret %s had same mtime for both secrets: %s\n", 
897                                                secname[i],
898                                                nt_time_string(mem_ctx, *r6.out.new_mtime));
899                                         ret = False;
900                                 }
901                         }
902                 }
903
904                 enc_key = sess_encrypt_string(secret5, &session_key);
905                 
906                 r7.in.sec_handle = &sec_handle;
907                 r7.in.old_val = &buf1;
908                 r7.in.old_val->data = enc_key.data;
909                 r7.in.old_val->length = enc_key.length;
910                 r7.in.old_val->size = enc_key.length;
911                 r7.in.new_val = NULL;
912                 
913                 printf("Testing SetSecret of old Secret only\n");
914                 
915                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
916                 if (!NT_STATUS_IS_OK(status)) {
917                         printf("SetSecret failed - %s\n", nt_errstr(status));
918                         ret = False;
919                 }
920                 
921                 data_blob_free(&enc_key);
922                 
923                 /* fetch the secret back again */
924                 r8.in.sec_handle = &sec_handle;
925                 r8.in.new_val = &bufp1;
926                 r8.in.new_mtime = &new_mtime;
927                 r8.in.old_val = &bufp2;
928                 r8.in.old_mtime = &old_mtime;
929                 
930                 bufp1.buf = NULL;
931                 bufp2.buf = NULL;
932                 
933                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
934                 if (!NT_STATUS_IS_OK(status)) {
935                         printf("QuerySecret failed - %s\n", nt_errstr(status));
936                         ret = False;
937                 } else {
938                         if (!r8.out.new_val || !r8.out.old_val) {
939                                 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
940                                 ret = False;
941                         } else if (r8.out.new_val->buf == NULL) {
942                                 if (i != LOCAL) { 
943                                         printf("NEW secret buffer not returned after GLOBAL OLD set\n");
944                                         ret = False;
945                                 }
946                         } else if (r8.out.old_val->buf == NULL) {
947                                 printf("OLD secret buffer not returned after OLD set\n");
948                                 ret = False;
949                         } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
950                                 printf("Both times not returned after OLD set\n");
951                                 ret = False;
952                         } else {
953                                 if (i == LOCAL) { 
954                                         printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
955                                         ret = False;
956                                 }
957                                 blob1.data = r8.out.new_val->buf->data;
958                                 blob1.length = r8.out.new_val->buf->length;
959                                 
960                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
961                                 
962                                 secret6 = sess_decrypt_string(&blob1, &session_key);
963                                 
964                                 if (strcmp(secret3, secret4) != 0) {
965                                         printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
966                                         ret = False;
967                                 }
968
969                                 blob1.data = r8.out.old_val->buf->data;
970                                 blob1.length = r8.out.old_val->buf->size;
971                                 
972                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
973                                 
974                                 secret6 = sess_decrypt_string(&blob1, &session_key);
975                                 
976                                 if (strcmp(secret5, secret6) != 0) {
977                                         printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
978                                         ret = False;
979                                 }
980                                 
981                                 if (*r8.out.new_mtime == *r8.out.old_mtime) {
982                                         if (i != GLOBAL) { 
983                                                 printf("Returned secret %s had same mtime for both secrets: %s\n", 
984                                                        secname[i],
985                                                        nt_time_string(mem_ctx, *r8.out.new_mtime));
986                                                 ret = False;
987                                         }
988                                 } else {
989                                         printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n", 
990                                                secname[i],
991                                                nt_time_string(mem_ctx, *r8.out.old_mtime),
992                                                nt_time_string(mem_ctx, *r8.out.new_mtime));
993                                         ret = False;
994                                 }
995                         }
996                 }
997
998                 if (!test_Delete(p, mem_ctx, &sec_handle)) {
999                         ret = False;
1000                 }
1001                 
1002                 d.in.handle = &sec_handle2;
1003                 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
1004                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
1005                         printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
1006                         ret = False;
1007                 } else {
1008
1009                         printf("Testing OpenSecret of just-deleted secret\n");
1010                         
1011                         status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
1012                         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1013                                 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
1014                                 ret = False;
1015                         }
1016                 }
1017                 
1018         }
1019
1020         return ret;
1021 }
1022
1023
1024 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p, 
1025                                    TALLOC_CTX *mem_ctx, 
1026                                    struct policy_handle *acct_handle,
1027                                    struct dom_sid *sid)
1028 {
1029         NTSTATUS status;
1030         struct lsa_EnumAccountRights r;
1031         struct lsa_RightSet rights;
1032
1033         printf("Testing EnumAccountRights\n");
1034
1035         r.in.handle = acct_handle;
1036         r.in.sid = sid;
1037         r.out.rights = &rights;
1038
1039         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1040         if (!NT_STATUS_IS_OK(status)) {
1041                 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
1042                 return False;
1043         }
1044
1045         return True;
1046 }
1047
1048
1049 static BOOL test_QuerySecurity(struct dcerpc_pipe *p, 
1050                              TALLOC_CTX *mem_ctx, 
1051                              struct policy_handle *handle,
1052                              struct policy_handle *acct_handle)
1053 {
1054         NTSTATUS status;
1055         struct lsa_QuerySecurity r;
1056
1057         printf("Testing QuerySecurity\n");
1058
1059         r.in.handle = acct_handle;
1060         r.in.sec_info = 7;
1061
1062         status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
1063         if (!NT_STATUS_IS_OK(status)) {
1064                 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1065                 return False;
1066         }
1067
1068         return True;
1069 }
1070
1071 static BOOL test_OpenAccount(struct dcerpc_pipe *p, 
1072                              TALLOC_CTX *mem_ctx, 
1073                              struct policy_handle *handle,
1074                              struct dom_sid *sid)
1075 {
1076         NTSTATUS status;
1077         struct lsa_OpenAccount r;
1078         struct policy_handle acct_handle;
1079
1080         printf("Testing OpenAccount\n");
1081
1082         r.in.handle = handle;
1083         r.in.sid = sid;
1084         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1085         r.out.acct_handle = &acct_handle;
1086
1087         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1088         if (!NT_STATUS_IS_OK(status)) {
1089                 printf("OpenAccount failed - %s\n", nt_errstr(status));
1090                 return False;
1091         }
1092
1093         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1094                 return False;
1095         }
1096
1097         if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1098                 return False;
1099         }
1100
1101         return True;
1102 }
1103
1104 static BOOL test_EnumAccounts(struct dcerpc_pipe *p, 
1105                           TALLOC_CTX *mem_ctx, 
1106                           struct policy_handle *handle)
1107 {
1108         NTSTATUS status;
1109         struct lsa_EnumAccounts r;
1110         struct lsa_SidArray sids1, sids2;
1111         uint32_t resume_handle = 0;
1112         int i;
1113
1114         printf("\ntesting EnumAccounts\n");
1115
1116         r.in.handle = handle;
1117         r.in.resume_handle = &resume_handle;
1118         r.in.num_entries = 100;
1119         r.out.resume_handle = &resume_handle;
1120         r.out.sids = &sids1;
1121
1122         resume_handle = 0;
1123         while (True) {
1124                 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1125                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1126                         break;
1127                 }
1128                 if (!NT_STATUS_IS_OK(status)) {
1129                         printf("EnumAccounts failed - %s\n", nt_errstr(status));
1130                         return False;
1131                 }
1132
1133                 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1134                         return False;
1135                 }
1136
1137                 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1138                         return False;
1139                 }
1140
1141                 if (!test_LookupSids3(p, mem_ctx, handle, &sids1)) {
1142                         return False;
1143                 }
1144
1145                 printf("testing all accounts\n");
1146                 for (i=0;i<sids1.num_sids;i++) {
1147                         test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1148                         test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1149                 }
1150                 printf("\n");
1151         }
1152
1153         if (sids1.num_sids < 3) {
1154                 return True;
1155         }
1156         
1157         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1158         resume_handle = 2;
1159         r.in.num_entries = 1;
1160         r.out.sids = &sids2;
1161
1162         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1163         if (!NT_STATUS_IS_OK(status)) {
1164                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1165                 return False;
1166         }
1167
1168         if (sids2.num_sids != 1) {
1169                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1170                 return False;
1171         }
1172
1173         return True;
1174 }
1175
1176 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1177                                 TALLOC_CTX *mem_ctx,
1178                                 struct policy_handle *handle,
1179                                 struct lsa_String *priv_name)
1180 {
1181         struct lsa_LookupPrivDisplayName r;
1182         NTSTATUS status;
1183         /* produce a reasonable range of language output without screwing up
1184            terminals */
1185         uint16 language_id = (random() % 4) + 0x409;
1186
1187         printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1188         
1189         r.in.handle = handle;
1190         r.in.name = priv_name;
1191         r.in.language_id = &language_id;
1192         r.out.language_id = &language_id;
1193         r.in.unknown = 0;
1194
1195         status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1196         if (!NT_STATUS_IS_OK(status)) {
1197                 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1198                 return False;
1199         }
1200         printf("%s -> \"%s\"  (language 0x%x/0x%x)\n", 
1201                priv_name->string, r.out.disp_name->string, 
1202                *r.in.language_id, *r.out.language_id);
1203
1204         return True;
1205 }
1206
1207 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p, 
1208                                 TALLOC_CTX *mem_ctx,
1209                                 struct policy_handle *handle,
1210                                 struct lsa_String *priv_name)
1211 {
1212         struct lsa_EnumAccountsWithUserRight r;
1213         struct lsa_SidArray sids;
1214         NTSTATUS status;
1215
1216         ZERO_STRUCT(sids);
1217         
1218         printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1219         
1220         r.in.handle = handle;
1221         r.in.name = priv_name;
1222         r.out.sids = &sids;
1223
1224         status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1225
1226         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1227         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1228                 return True;
1229         }
1230
1231         if (!NT_STATUS_IS_OK(status)) {
1232                 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1233                 return False;
1234         }
1235         
1236         return True;
1237 }
1238
1239
1240 static BOOL test_EnumPrivs(struct dcerpc_pipe *p, 
1241                            TALLOC_CTX *mem_ctx, 
1242                            struct policy_handle *handle)
1243 {
1244         NTSTATUS status;
1245         struct lsa_EnumPrivs r;
1246         struct lsa_PrivArray privs1;
1247         uint32_t resume_handle = 0;
1248         int i;
1249         BOOL ret = True;
1250
1251         printf("\ntesting EnumPrivs\n");
1252
1253         r.in.handle = handle;
1254         r.in.resume_handle = &resume_handle;
1255         r.in.max_count = 100;
1256         r.out.resume_handle = &resume_handle;
1257         r.out.privs = &privs1;
1258
1259         resume_handle = 0;
1260         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1261         if (!NT_STATUS_IS_OK(status)) {
1262                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1263                 return False;
1264         }
1265
1266         for (i = 0; i< privs1.count; i++) {
1267                 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
1268                 test_LookupPrivValue(p, mem_ctx, handle, &privs1.privs[i].name);
1269                 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
1270                         ret = False;
1271                 }
1272         }
1273
1274         return ret;
1275 }
1276
1277
1278 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p, 
1279                               TALLOC_CTX *mem_ctx, 
1280                               struct policy_handle *handle)
1281 {
1282         struct lsa_EnumTrustDom r;
1283         NTSTATUS status;
1284         uint32_t resume_handle = 0;
1285         struct lsa_DomainList domains;
1286         int i,j;
1287         BOOL ret = True;
1288
1289         printf("\nTesting EnumTrustDom\n");
1290
1291         r.in.handle = handle;
1292         r.in.resume_handle = &resume_handle;
1293         r.in.num_entries = 100;
1294         r.out.domains = &domains;
1295         r.out.resume_handle = &resume_handle;
1296
1297         status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1298
1299         /* NO_MORE_ENTRIES is allowed */
1300         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1301                 return True;
1302         }
1303
1304         if (!NT_STATUS_IS_OK(status)) {
1305                 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
1306                 return False;
1307         }
1308
1309         printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1310
1311         for (i=0; i< domains.count; i++) {
1312                 struct lsa_OpenTrustedDomain trust;
1313                 struct lsa_OpenTrustedDomainByName trust_by_name;
1314                 struct policy_handle trustdom_handle;
1315                 struct policy_handle handle2;
1316                 struct lsa_Close c;
1317                 int levels [] = {1, 3, 6, 8, 12};
1318                 
1319                 trust.in.handle = handle;
1320                 trust.in.sid = domains.domains[i].sid;
1321                 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1322                 trust.out.trustdom_handle = &trustdom_handle;
1323
1324                 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1325
1326                 if (!NT_STATUS_IS_OK(status)) {
1327                         printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1328                         return False;
1329                 }
1330
1331                 c.in.handle = &trustdom_handle;
1332                 c.out.handle = &handle2;
1333                 
1334                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1335                         struct lsa_QueryTrustedDomainInfo q;
1336                         union lsa_TrustedDomainInfo info;
1337                         q.in.trustdom_handle = &trustdom_handle;
1338                         q.in.level = levels[j];
1339                         q.out.info = &info;
1340                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1341                         if (!NT_STATUS_IS_OK(status)) {
1342                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1343                                        levels[j], nt_errstr(status));
1344                                 ret = False;
1345                         }
1346                 }
1347                 
1348                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1349                 if (!NT_STATUS_IS_OK(status)) {
1350                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1351                         return False;
1352                 }
1353
1354                 trust_by_name.in.handle = handle;
1355                 trust_by_name.in.name = domains.domains[i].name;
1356                 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1357                 trust_by_name.out.trustdom_handle = &trustdom_handle;
1358                 
1359                 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1360
1361                 if (!NT_STATUS_IS_OK(status)) {
1362                         printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1363                         return False;
1364                 }
1365
1366                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1367                         struct lsa_QueryTrustedDomainInfo q;
1368                         union lsa_TrustedDomainInfo info;
1369                         q.in.trustdom_handle = &trustdom_handle;
1370                         q.in.level = levels[j];
1371                         q.out.info = &info;
1372                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1373                         if (!NT_STATUS_IS_OK(status)) {
1374                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1375                                        levels[j], nt_errstr(status));
1376                                 ret = False;
1377                         }
1378                 }
1379                 
1380                 c.in.handle = &trustdom_handle;
1381                 c.out.handle = &handle2;
1382
1383                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1384                 if (!NT_STATUS_IS_OK(status)) {
1385                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1386                         return False;
1387                 }
1388
1389                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1390                         struct lsa_QueryTrustedDomainInfoBySid q;
1391                         union lsa_TrustedDomainInfo info;
1392                         q.in.handle  = handle;
1393                         q.in.dom_sid = domains.domains[i].sid;
1394                         q.in.level   = levels[j];
1395                         q.out.info   = &info;
1396                         status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1397                         if (!NT_STATUS_IS_OK(status)) {
1398                                 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n", 
1399                                        levels[j], nt_errstr(status));
1400                                 ret = False;
1401                         }
1402                 }
1403                 
1404                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1405                         struct lsa_QueryTrustedDomainInfoByName q;
1406                         union lsa_TrustedDomainInfo info;
1407                         q.in.handle         = handle;
1408                         q.in.trusted_domain = domains.domains[i].name;
1409                         q.in.level          = levels[j];
1410                         q.out.info          = &info;
1411                         status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1412                         if (!NT_STATUS_IS_OK(status)) {
1413                                 printf("QueryTrustedDomainInfoByName level %d failed - %s\n", 
1414                                        levels[j], nt_errstr(status));
1415                                 ret = False;
1416                         }
1417                 }
1418                 
1419                 
1420
1421         }
1422
1423         return ret;
1424 }
1425
1426 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p, 
1427                                  TALLOC_CTX *mem_ctx, 
1428                                  struct policy_handle *handle)
1429 {
1430         struct lsa_QueryInfoPolicy r;
1431         NTSTATUS status;
1432         int i;
1433         BOOL ret = True;
1434         printf("\nTesting QueryInfoPolicy\n");
1435
1436         for (i=1;i<13;i++) {
1437                 r.in.handle = handle;
1438                 r.in.level = i;
1439
1440                 printf("\ntrying QueryInfoPolicy level %d\n", i);
1441
1442                 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
1443
1444                 if ((i == 9 || i == 10 || i == 11) &&
1445                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1446                         printf("server failed level %u (OK)\n", i);
1447                         continue;
1448                 }
1449
1450                 if (!NT_STATUS_IS_OK(status)) {
1451                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1452                         ret = False;
1453                         continue;
1454                 }
1455         }
1456
1457         return ret;
1458 }
1459
1460 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p, 
1461                                   TALLOC_CTX *mem_ctx, 
1462                                   struct policy_handle *handle)
1463 {
1464         struct lsa_QueryInfoPolicy2 r;
1465         NTSTATUS status;
1466         int i;
1467         BOOL ret = True;
1468         printf("\nTesting QueryInfoPolicy2\n");
1469
1470         for (i=1;i<13;i++) {
1471                 r.in.handle = handle;
1472                 r.in.level = i;
1473
1474                 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
1475
1476                 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
1477
1478                 if ((i == 9 || i == 10 || i == 11) &&
1479                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1480                         printf("server failed level %u (OK)\n", i);
1481                         continue;
1482                 }
1483
1484                 if (!NT_STATUS_IS_OK(status)) {
1485                         printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
1486                         ret = False;
1487                         continue;
1488                 }
1489         }
1490
1491         return ret;
1492 }
1493
1494 static BOOL test_GetUserName(struct dcerpc_pipe *p, 
1495                                   TALLOC_CTX *mem_ctx, 
1496                                   struct policy_handle *handle)
1497 {
1498         struct lsa_GetUserName r;
1499         NTSTATUS status;
1500         BOOL ret = True;
1501         struct lsa_StringPointer authority_name_p;
1502
1503         printf("\nTesting GetUserName\n");
1504
1505         r.in.system_name = "\\";        
1506         r.in.account_name = NULL;       
1507         r.in.authority_name = &authority_name_p;
1508         authority_name_p.string = NULL;
1509
1510         status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
1511
1512         if (!NT_STATUS_IS_OK(status)) {
1513                 printf("GetUserName failed - %s\n", nt_errstr(status));
1514                 ret = False;
1515         }
1516
1517         return ret;
1518 }
1519
1520 BOOL test_lsa_Close(struct dcerpc_pipe *p, 
1521                     TALLOC_CTX *mem_ctx, 
1522                     struct policy_handle *handle)
1523 {
1524         NTSTATUS status;
1525         struct lsa_Close r;
1526         struct policy_handle handle2;
1527
1528         printf("\ntesting Close\n");
1529
1530         r.in.handle = handle;
1531         r.out.handle = &handle2;
1532
1533         status = dcerpc_lsa_Close(p, mem_ctx, &r);
1534         if (!NT_STATUS_IS_OK(status)) {
1535                 printf("Close failed - %s\n", nt_errstr(status));
1536                 return False;
1537         }
1538
1539         status = dcerpc_lsa_Close(p, mem_ctx, &r);
1540         /* its really a fault - we need a status code for rpc fault */
1541         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
1542                 printf("Close failed - %s\n", nt_errstr(status));
1543                 return False;
1544         }
1545
1546         printf("\n");
1547
1548         return True;
1549 }
1550
1551 BOOL torture_rpc_lsa(void)
1552 {
1553         NTSTATUS status;
1554         struct dcerpc_pipe *p;
1555         TALLOC_CTX *mem_ctx;
1556         BOOL ret = True;
1557         struct policy_handle handle;
1558
1559         mem_ctx = talloc_init("torture_rpc_lsa");
1560
1561         status = torture_rpc_connection(&p, 
1562                                         DCERPC_LSARPC_NAME, 
1563                                         DCERPC_LSARPC_UUID, 
1564                                         DCERPC_LSARPC_VERSION);
1565         if (!NT_STATUS_IS_OK(status)) {
1566                 return False;
1567         }
1568
1569         if (!test_OpenPolicy(p, mem_ctx)) {
1570                 ret = False;
1571         }
1572
1573         if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
1574                 ret = False;
1575         }
1576
1577         if (!test_many_LookupSids(p, mem_ctx, &handle)) {
1578                 ret = False;
1579         }
1580
1581         if (!test_CreateAccount(p, mem_ctx, &handle)) {
1582                 ret = False;
1583         }
1584
1585         if (!test_CreateSecret(p, mem_ctx, &handle)) {
1586                 ret = False;
1587         }
1588
1589         if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
1590                 ret = False;
1591         }
1592
1593         if (!test_EnumAccounts(p, mem_ctx, &handle)) {
1594                 ret = False;
1595         }
1596
1597         if (!test_EnumPrivs(p, mem_ctx, &handle)) {
1598                 ret = False;
1599         }
1600
1601         if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
1602                 ret = False;
1603         }
1604
1605         if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
1606                 ret = False;
1607         }
1608
1609         if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
1610                 ret = False;
1611         }
1612
1613         if (!test_GetUserName(p, mem_ctx, &handle)) {
1614                 ret = False;
1615         }
1616
1617 #if 0
1618         if (!test_Delete(p, mem_ctx, &handle)) {
1619                 ret = False;
1620         }
1621 #endif
1622         
1623         if (!test_lsa_Close(p, mem_ctx, &handle)) {
1624                 ret = False;
1625         }
1626
1627         talloc_destroy(mem_ctx);
1628
1629         torture_rpc_close(p);
1630
1631         return ret;
1632 }