r4699: Move the test_EnumTrustDom() test into the test_CreateTrustedDomain
[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_CreateSecret(struct dcerpc_pipe *p, 
635                               TALLOC_CTX *mem_ctx, 
636                               struct policy_handle *handle)
637 {
638         NTSTATUS status;
639         struct lsa_CreateSecret r;
640         struct lsa_OpenSecret r2;
641         struct lsa_SetSecret r3;
642         struct lsa_QuerySecret r4;
643         struct lsa_SetSecret r5;
644         struct lsa_QuerySecret r6;
645         struct lsa_SetSecret r7;
646         struct lsa_QuerySecret r8;
647         struct policy_handle sec_handle, sec_handle2, sec_handle3;
648         struct lsa_Delete d;
649         struct lsa_DATA_BUF buf1;
650         struct lsa_DATA_BUF_PTR bufp1;
651         struct lsa_DATA_BUF_PTR bufp2;
652         DATA_BLOB enc_key;
653         BOOL ret = True;
654         DATA_BLOB session_key;
655         NTTIME old_mtime, new_mtime;
656         DATA_BLOB blob1, blob2;
657         const char *secret1 = "abcdef12345699qwerty";
658         char *secret2;
659         const char *secret3 = "ABCDEF12345699QWERTY";
660         char *secret4;
661         const char *secret5 = "NEW-SAMBA4-SECRET";
662         char *secret6;
663         char *secname[2];
664         int i;
665         const int LOCAL = 0;
666         const int GLOBAL = 1;
667
668         secname[LOCAL] = talloc_asprintf(mem_ctx, "torturesecret-%u", (uint_t)random());
669         secname[GLOBAL] = talloc_asprintf(mem_ctx, "G$torturesecret-%u", (uint_t)random());
670
671         for (i=0; i< 2; i++) {
672                 printf("Testing CreateSecret of %s\n", secname[i]);
673                 
674                 init_lsa_String(&r.in.name, secname[i]);
675                 
676                 r.in.handle = handle;
677                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
678                 r.out.sec_handle = &sec_handle;
679                 
680                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
681                 if (!NT_STATUS_IS_OK(status)) {
682                         printf("CreateSecret failed - %s\n", nt_errstr(status));
683                         return False;
684                 }
685                 
686                 r.in.handle = handle;
687                 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
688                 r.out.sec_handle = &sec_handle3;
689                 
690                 status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
691                 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
692                         printf("CreateSecret should have failed OBJECT_NAME_COLLISION - %s\n", nt_errstr(status));
693                         return False;
694                 }
695                 
696                 r2.in.handle = handle;
697                 r2.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
698                 r2.in.name = r.in.name;
699                 r2.out.sec_handle = &sec_handle2;
700                 
701                 printf("Testing OpenSecret\n");
702                 
703                 status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
704                 if (!NT_STATUS_IS_OK(status)) {
705                         printf("OpenSecret failed - %s\n", nt_errstr(status));
706                         ret = False;
707                 }
708                 
709                 status = dcerpc_fetch_session_key(p, &session_key);
710                 if (!NT_STATUS_IS_OK(status)) {
711                         printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
712                         ret = False;
713                 }
714                 
715                 enc_key = sess_encrypt_string(secret1, &session_key);
716                 
717                 r3.in.sec_handle = &sec_handle;
718                 r3.in.new_val = &buf1;
719                 r3.in.old_val = NULL;
720                 r3.in.new_val->data = enc_key.data;
721                 r3.in.new_val->length = enc_key.length;
722                 r3.in.new_val->size = enc_key.length;
723                 
724                 printf("Testing SetSecret\n");
725                 
726                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
727                 if (!NT_STATUS_IS_OK(status)) {
728                         printf("SetSecret failed - %s\n", nt_errstr(status));
729                         ret = False;
730                 }
731                 
732                 r3.in.sec_handle = &sec_handle;
733                 r3.in.new_val = &buf1;
734                 r3.in.old_val = NULL;
735                 r3.in.new_val->data = enc_key.data;
736                 r3.in.new_val->length = enc_key.length;
737                 r3.in.new_val->size = enc_key.length;
738                 
739                 /* break the encrypted data */
740                 enc_key.data[0]++;
741
742                 printf("Testing SetSecret with broken key\n");
743                 
744                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
745                 if (!NT_STATUS_EQUAL(status, NT_STATUS_UNKNOWN_REVISION)) {
746                         printf("SetSecret should have failed UNKNOWN_REVISION - %s\n", nt_errstr(status));
747                         ret = False;
748                 }
749                 
750                 data_blob_free(&enc_key);
751                 
752                 ZERO_STRUCT(new_mtime);
753                 ZERO_STRUCT(old_mtime);
754                 
755                 /* fetch the secret back again */
756                 r4.in.sec_handle = &sec_handle;
757                 r4.in.new_val = &bufp1;
758                 r4.in.new_mtime = &new_mtime;
759                 r4.in.old_val = NULL;
760                 r4.in.old_mtime = NULL;
761                 
762                 bufp1.buf = NULL;
763                 
764                 printf("Testing QuerySecret\n");
765                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
766                 if (!NT_STATUS_IS_OK(status)) {
767                         printf("QuerySecret failed - %s\n", nt_errstr(status));
768                         ret = False;
769                 } else {
770                         if (r4.out.new_val == NULL || r4.out.new_val->buf == NULL) {
771                                 printf("No secret buffer returned\n");
772                                 ret = False;
773                         } else {
774                                 blob1.data = r4.out.new_val->buf->data;
775                                 blob1.length = r4.out.new_val->buf->size;
776                                 
777                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
778                                 
779                                 secret2 = sess_decrypt_string(&blob1, &session_key);
780                                 
781                                 if (strcmp(secret1, secret2) != 0) {
782                                         printf("Returned secret '%s' doesn't match '%s'\n", 
783                                                secret2, secret1);
784                                         ret = False;
785                                 }
786                         }
787                 }
788                 
789                 enc_key = sess_encrypt_string(secret3, &session_key);
790                 
791                 r5.in.sec_handle = &sec_handle;
792                 r5.in.new_val = &buf1;
793                 r5.in.old_val = NULL;
794                 r5.in.new_val->data = enc_key.data;
795                 r5.in.new_val->length = enc_key.length;
796                 r5.in.new_val->size = enc_key.length;
797                 
798                 printf("Testing SetSecret (existing value should move to old)\n");
799                 
800                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r5);
801                 if (!NT_STATUS_IS_OK(status)) {
802                         printf("SetSecret failed - %s\n", nt_errstr(status));
803                         ret = False;
804                 }
805                 
806                 data_blob_free(&enc_key);
807                 
808                 ZERO_STRUCT(new_mtime);
809                 ZERO_STRUCT(old_mtime);
810                 
811                 /* fetch the secret back again */
812                 r6.in.sec_handle = &sec_handle;
813                 r6.in.new_val = &bufp1;
814                 r6.in.new_mtime = &new_mtime;
815                 r6.in.old_val = &bufp2;
816                 r6.in.old_mtime = &old_mtime;
817                 
818                 bufp1.buf = NULL;
819                 bufp2.buf = NULL;
820                 
821                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r6);
822                 if (!NT_STATUS_IS_OK(status)) {
823                         printf("QuerySecret failed - %s\n", nt_errstr(status));
824                         ret = False;
825                 } else {
826
827                         if (r6.out.new_val->buf == NULL || r6.out.old_val->buf == NULL 
828                                 || r6.out.new_mtime == NULL || r6.out.old_mtime == NULL) {
829                                 printf("Both secret buffers and both times not returned\n");
830                                 ret = False;
831                         } else {
832                                 blob1.data = r6.out.new_val->buf->data;
833                                 blob1.length = r6.out.new_val->buf->size;
834                                 
835                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
836                                 
837                                 secret4 = sess_decrypt_string(&blob1, &session_key);
838                                 
839                                 if (strcmp(secret3, secret4) != 0) {
840                                         printf("Returned NEW secret %s doesn't match %s\n", secret4, secret3);
841                                         ret = False;
842                                 }
843
844                                 blob1.data = r6.out.old_val->buf->data;
845                                 blob1.length = r6.out.old_val->buf->length;
846                                 
847                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
848                                 
849                                 secret2 = sess_decrypt_string(&blob1, &session_key);
850                                 
851                                 if (strcmp(secret1, secret2) != 0) {
852                                         printf("Returned OLD secret %s doesn't match %s\n", secret2, secret1);
853                                         ret = False;
854                                 }
855                                 
856                                 if (*r6.out.new_mtime == *r6.out.old_mtime) {
857                                         printf("Returned secret %s had same mtime for both secrets: %s\n", 
858                                                secname[i],
859                                                nt_time_string(mem_ctx, *r6.out.new_mtime));
860                                         ret = False;
861                                 }
862                         }
863                 }
864
865                 enc_key = sess_encrypt_string(secret5, &session_key);
866                 
867                 r7.in.sec_handle = &sec_handle;
868                 r7.in.old_val = &buf1;
869                 r7.in.old_val->data = enc_key.data;
870                 r7.in.old_val->length = enc_key.length;
871                 r7.in.old_val->size = enc_key.length;
872                 r7.in.new_val = NULL;
873                 
874                 printf("Testing SetSecret of old Secret only\n");
875                 
876                 status = dcerpc_lsa_SetSecret(p, mem_ctx, &r7);
877                 if (!NT_STATUS_IS_OK(status)) {
878                         printf("SetSecret failed - %s\n", nt_errstr(status));
879                         ret = False;
880                 }
881                 
882                 data_blob_free(&enc_key);
883                 
884                 /* fetch the secret back again */
885                 r8.in.sec_handle = &sec_handle;
886                 r8.in.new_val = &bufp1;
887                 r8.in.new_mtime = &new_mtime;
888                 r8.in.old_val = &bufp2;
889                 r8.in.old_mtime = &old_mtime;
890                 
891                 bufp1.buf = NULL;
892                 bufp2.buf = NULL;
893                 
894                 status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r8);
895                 if (!NT_STATUS_IS_OK(status)) {
896                         printf("QuerySecret failed - %s\n", nt_errstr(status));
897                         ret = False;
898                 } else {
899                         if (!r8.out.new_val || !r8.out.old_val) {
900                                 printf("in/out pointers not returned, despite being set on in for QuerySecret\n");
901                                 ret = False;
902                         } else if (r8.out.new_val->buf == NULL) {
903                                 if (i != LOCAL) { 
904                                         printf("NEW secret buffer not returned after GLOBAL OLD set\n");
905                                         ret = False;
906                                 }
907                         } else if (r8.out.old_val->buf == NULL) {
908                                 printf("OLD secret buffer not returned after OLD set\n");
909                                 ret = False;
910                         } else if (r8.out.new_mtime == NULL || r8.out.old_mtime == NULL) {
911                                 printf("Both times not returned after OLD set\n");
912                                 ret = False;
913                         } else {
914                                 if (i == LOCAL) { 
915                                         printf("NEW secret buffer should not be returned after LOCAL OLD set\n");
916                                         ret = False;
917                                 }
918                                 blob1.data = r8.out.new_val->buf->data;
919                                 blob1.length = r8.out.new_val->buf->length;
920                                 
921                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
922                                 
923                                 secret6 = sess_decrypt_string(&blob1, &session_key);
924                                 
925                                 if (strcmp(secret3, secret4) != 0) {
926                                         printf("Returned NEW secret '%s' doesn't match '%s'\n", secret4, secret3);
927                                         ret = False;
928                                 }
929
930                                 blob1.data = r8.out.old_val->buf->data;
931                                 blob1.length = r8.out.old_val->buf->size;
932                                 
933                                 blob2 = data_blob_talloc(mem_ctx, NULL, blob1.length);
934                                 
935                                 secret6 = sess_decrypt_string(&blob1, &session_key);
936                                 
937                                 if (strcmp(secret5, secret6) != 0) {
938                                         printf("Returned OLD secret %s doesn't match %s\n", secret5, secret6);
939                                         ret = False;
940                                 }
941                                 
942                                 if (*r8.out.new_mtime == *r8.out.old_mtime) {
943                                         if (i != GLOBAL) { 
944                                                 printf("Returned secret %s had same mtime for both secrets: %s\n", 
945                                                        secname[i],
946                                                        nt_time_string(mem_ctx, *r8.out.new_mtime));
947                                                 ret = False;
948                                         }
949                                 } else {
950                                         printf("Returned secret %s should have had same mtime for both secrets: %s != %s\n", 
951                                                secname[i],
952                                                nt_time_string(mem_ctx, *r8.out.old_mtime),
953                                                nt_time_string(mem_ctx, *r8.out.new_mtime));
954                                         ret = False;
955                                 }
956                         }
957                 }
958
959                 if (!test_Delete(p, mem_ctx, &sec_handle)) {
960                         ret = False;
961                 }
962                 
963                 d.in.handle = &sec_handle2;
964                 status = dcerpc_lsa_Delete(p, mem_ctx, &d);
965                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
966                         printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
967                         ret = False;
968                 } else {
969
970                         printf("Testing OpenSecret of just-deleted secret\n");
971                         
972                         status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
973                         if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
974                                 printf("OpenSecret expected OBJECT_NAME_NOT_FOUND - %s\n", nt_errstr(status));
975                                 ret = False;
976                         }
977                 }
978                 
979         }
980
981         return ret;
982 }
983
984
985 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p, 
986                                    TALLOC_CTX *mem_ctx, 
987                                    struct policy_handle *acct_handle,
988                                    struct dom_sid *sid)
989 {
990         NTSTATUS status;
991         struct lsa_EnumAccountRights r;
992         struct lsa_RightSet rights;
993
994         printf("Testing EnumAccountRights\n");
995
996         r.in.handle = acct_handle;
997         r.in.sid = sid;
998         r.out.rights = &rights;
999
1000         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
1001         if (!NT_STATUS_IS_OK(status)) {
1002                 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
1003                 return False;
1004         }
1005
1006         return True;
1007 }
1008
1009
1010 static BOOL test_QuerySecurity(struct dcerpc_pipe *p, 
1011                              TALLOC_CTX *mem_ctx, 
1012                              struct policy_handle *handle,
1013                              struct policy_handle *acct_handle)
1014 {
1015         NTSTATUS status;
1016         struct lsa_QuerySecurity r;
1017
1018         printf("Testing QuerySecurity\n");
1019
1020         r.in.handle = acct_handle;
1021         r.in.sec_info = 7;
1022
1023         status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
1024         if (!NT_STATUS_IS_OK(status)) {
1025                 printf("QuerySecurity failed - %s\n", nt_errstr(status));
1026                 return False;
1027         }
1028
1029         return True;
1030 }
1031
1032 static BOOL test_OpenAccount(struct dcerpc_pipe *p, 
1033                              TALLOC_CTX *mem_ctx, 
1034                              struct policy_handle *handle,
1035                              struct dom_sid *sid)
1036 {
1037         NTSTATUS status;
1038         struct lsa_OpenAccount r;
1039         struct policy_handle acct_handle;
1040
1041         printf("Testing OpenAccount\n");
1042
1043         r.in.handle = handle;
1044         r.in.sid = sid;
1045         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1046         r.out.acct_handle = &acct_handle;
1047
1048         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
1049         if (!NT_STATUS_IS_OK(status)) {
1050                 printf("OpenAccount failed - %s\n", nt_errstr(status));
1051                 return False;
1052         }
1053
1054         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
1055                 return False;
1056         }
1057
1058         if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
1059                 return False;
1060         }
1061
1062         return True;
1063 }
1064
1065 static BOOL test_EnumAccounts(struct dcerpc_pipe *p, 
1066                           TALLOC_CTX *mem_ctx, 
1067                           struct policy_handle *handle)
1068 {
1069         NTSTATUS status;
1070         struct lsa_EnumAccounts r;
1071         struct lsa_SidArray sids1, sids2;
1072         uint32_t resume_handle = 0;
1073         int i;
1074
1075         printf("\ntesting EnumAccounts\n");
1076
1077         r.in.handle = handle;
1078         r.in.resume_handle = &resume_handle;
1079         r.in.num_entries = 100;
1080         r.out.resume_handle = &resume_handle;
1081         r.out.sids = &sids1;
1082
1083         resume_handle = 0;
1084         while (True) {
1085                 status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1086                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1087                         break;
1088                 }
1089                 if (!NT_STATUS_IS_OK(status)) {
1090                         printf("EnumAccounts failed - %s\n", nt_errstr(status));
1091                         return False;
1092                 }
1093
1094                 if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
1095                         return False;
1096                 }
1097
1098                 if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
1099                         return False;
1100                 }
1101
1102                 if (!test_LookupSids3(p, mem_ctx, handle, &sids1)) {
1103                         return False;
1104                 }
1105
1106                 printf("testing all accounts\n");
1107                 for (i=0;i<sids1.num_sids;i++) {
1108                         test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
1109                         test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
1110                 }
1111                 printf("\n");
1112         }
1113
1114         if (sids1.num_sids < 3) {
1115                 return True;
1116         }
1117         
1118         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
1119         resume_handle = 2;
1120         r.in.num_entries = 1;
1121         r.out.sids = &sids2;
1122
1123         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
1124         if (!NT_STATUS_IS_OK(status)) {
1125                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
1126                 return False;
1127         }
1128
1129         if (sids2.num_sids != 1) {
1130                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
1131                 return False;
1132         }
1133
1134         return True;
1135 }
1136
1137 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
1138                                 TALLOC_CTX *mem_ctx,
1139                                 struct policy_handle *handle,
1140                                 struct lsa_String *priv_name)
1141 {
1142         struct lsa_LookupPrivDisplayName r;
1143         NTSTATUS status;
1144         /* produce a reasonable range of language output without screwing up
1145            terminals */
1146         uint16 language_id = (random() % 4) + 0x409;
1147
1148         printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
1149         
1150         r.in.handle = handle;
1151         r.in.name = priv_name;
1152         r.in.language_id = &language_id;
1153         r.out.language_id = &language_id;
1154         r.in.unknown = 0;
1155
1156         status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
1157         if (!NT_STATUS_IS_OK(status)) {
1158                 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
1159                 return False;
1160         }
1161         printf("%s -> \"%s\"  (language 0x%x/0x%x)\n", 
1162                priv_name->string, r.out.disp_name->string, 
1163                *r.in.language_id, *r.out.language_id);
1164
1165         return True;
1166 }
1167
1168 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p, 
1169                                 TALLOC_CTX *mem_ctx,
1170                                 struct policy_handle *handle,
1171                                 struct lsa_String *priv_name)
1172 {
1173         struct lsa_EnumAccountsWithUserRight r;
1174         struct lsa_SidArray sids;
1175         NTSTATUS status;
1176
1177         ZERO_STRUCT(sids);
1178         
1179         printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
1180         
1181         r.in.handle = handle;
1182         r.in.name = priv_name;
1183         r.out.sids = &sids;
1184
1185         status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
1186
1187         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
1188         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1189                 return True;
1190         }
1191
1192         if (!NT_STATUS_IS_OK(status)) {
1193                 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
1194                 return False;
1195         }
1196         
1197         return True;
1198 }
1199
1200
1201 static BOOL test_EnumPrivs(struct dcerpc_pipe *p, 
1202                            TALLOC_CTX *mem_ctx, 
1203                            struct policy_handle *handle)
1204 {
1205         NTSTATUS status;
1206         struct lsa_EnumPrivs r;
1207         struct lsa_PrivArray privs1;
1208         uint32_t resume_handle = 0;
1209         int i;
1210         BOOL ret = True;
1211
1212         printf("\ntesting EnumPrivs\n");
1213
1214         r.in.handle = handle;
1215         r.in.resume_handle = &resume_handle;
1216         r.in.max_count = 100;
1217         r.out.resume_handle = &resume_handle;
1218         r.out.privs = &privs1;
1219
1220         resume_handle = 0;
1221         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
1222         if (!NT_STATUS_IS_OK(status)) {
1223                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
1224                 return False;
1225         }
1226
1227         for (i = 0; i< privs1.count; i++) {
1228                 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
1229                 test_LookupPrivValue(p, mem_ctx, handle, &privs1.privs[i].name);
1230                 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
1231                         ret = False;
1232                 }
1233         }
1234
1235         return ret;
1236 }
1237
1238
1239 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p, 
1240                               TALLOC_CTX *mem_ctx, 
1241                               struct policy_handle *handle)
1242 {
1243         struct lsa_EnumTrustDom r;
1244         NTSTATUS status;
1245         uint32_t resume_handle = 0;
1246         struct lsa_DomainList domains;
1247         int i,j;
1248         BOOL ret = True;
1249
1250         printf("\nTesting EnumTrustDom\n");
1251
1252         r.in.handle = handle;
1253         r.in.resume_handle = &resume_handle;
1254         r.in.num_entries = 100;
1255         r.out.domains = &domains;
1256         r.out.resume_handle = &resume_handle;
1257
1258         status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
1259
1260         /* NO_MORE_ENTRIES is allowed */
1261         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1262                 return True;
1263         }
1264
1265         if (!NT_STATUS_IS_OK(status)) {
1266                 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
1267                 return False;
1268         }
1269
1270         printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
1271
1272         for (i=0; i< domains.count; i++) {
1273                 struct lsa_OpenTrustedDomain trust;
1274                 struct lsa_OpenTrustedDomainByName trust_by_name;
1275                 struct policy_handle trustdom_handle;
1276                 struct policy_handle handle2;
1277                 struct lsa_Close c;
1278                 int levels [] = {1, 3, 6, 8, 12};
1279                 
1280                 trust.in.handle = handle;
1281                 trust.in.sid = domains.domains[i].sid;
1282                 trust.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1283                 trust.out.trustdom_handle = &trustdom_handle;
1284
1285                 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
1286
1287                 if (!NT_STATUS_IS_OK(status)) {
1288                         printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
1289                         return False;
1290                 }
1291
1292                 c.in.handle = &trustdom_handle;
1293                 c.out.handle = &handle2;
1294                 
1295                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1296                         struct lsa_QueryTrustedDomainInfo q;
1297                         union lsa_TrustedDomainInfo info;
1298                         q.in.trustdom_handle = &trustdom_handle;
1299                         q.in.level = levels[j];
1300                         q.out.info = &info;
1301                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1302                         if (!NT_STATUS_IS_OK(status)) {
1303                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1304                                        levels[j], nt_errstr(status));
1305                                 ret = False;
1306                         }
1307                 }
1308                 
1309                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1310                 if (!NT_STATUS_IS_OK(status)) {
1311                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1312                         return False;
1313                 }
1314
1315                 trust_by_name.in.handle = handle;
1316                 trust_by_name.in.name = domains.domains[i].name;
1317                 trust_by_name.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1318                 trust_by_name.out.trustdom_handle = &trustdom_handle;
1319                 
1320                 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
1321
1322                 if (!NT_STATUS_IS_OK(status)) {
1323                         printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
1324                         return False;
1325                 }
1326
1327                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1328                         struct lsa_QueryTrustedDomainInfo q;
1329                         union lsa_TrustedDomainInfo info;
1330                         q.in.trustdom_handle = &trustdom_handle;
1331                         q.in.level = levels[j];
1332                         q.out.info = &info;
1333                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1334                         if (!NT_STATUS_IS_OK(status)) {
1335                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
1336                                        levels[j], nt_errstr(status));
1337                                 ret = False;
1338                         }
1339                 }
1340                 
1341                 c.in.handle = &trustdom_handle;
1342                 c.out.handle = &handle2;
1343
1344                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
1345                 if (!NT_STATUS_IS_OK(status)) {
1346                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
1347                         return False;
1348                 }
1349
1350                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1351                         struct lsa_QueryTrustedDomainInfoBySid q;
1352                         union lsa_TrustedDomainInfo info;
1353                         q.in.handle  = handle;
1354                         q.in.dom_sid = domains.domains[i].sid;
1355                         q.in.level   = levels[j];
1356                         q.out.info   = &info;
1357                         status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
1358                         if (!NT_STATUS_IS_OK(status)) {
1359                                 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n", 
1360                                        levels[j], nt_errstr(status));
1361                                 ret = False;
1362                         }
1363                 }
1364                 
1365                 for (j=0; j < ARRAY_SIZE(levels); j++) {
1366                         struct lsa_QueryTrustedDomainInfoByName q;
1367                         union lsa_TrustedDomainInfo info;
1368                         q.in.handle         = handle;
1369                         q.in.trusted_domain = domains.domains[i].name;
1370                         q.in.level          = levels[j];
1371                         q.out.info          = &info;
1372                         status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
1373                         if (!NT_STATUS_IS_OK(status)) {
1374                                 printf("QueryTrustedDomainInfoByName level %d failed - %s\n", 
1375                                        levels[j], nt_errstr(status));
1376                                 ret = False;
1377                         }
1378                 }
1379                 
1380                 
1381
1382         }
1383
1384         return ret;
1385 }
1386
1387 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p, 
1388                                      TALLOC_CTX *mem_ctx, 
1389                                      struct policy_handle *handle)
1390 {
1391         NTSTATUS status;
1392         BOOL ret = True;
1393         struct lsa_CreateTrustedDomain r;
1394         struct lsa_TrustInformation trustinfo;
1395         struct dom_sid *domsid;
1396         struct policy_handle trustdom_handle;
1397         struct lsa_QueryTrustedDomainInfo q;
1398
1399         printf("Testing CreateTrustedDomain\n");
1400
1401         domsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-21-97398-379795-12345");
1402
1403         trustinfo.sid = domsid;
1404         init_lsa_String(&trustinfo.name, "torturedomain");
1405
1406         r.in.handle = handle;
1407         r.in.info = &trustinfo;
1408         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1409         r.out.trustdom_handle = &trustdom_handle;
1410
1411         status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1412         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1413                 test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
1414                 status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
1415         }
1416         if (!NT_STATUS_IS_OK(status)) {
1417                 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
1418                 return False;
1419         }
1420
1421         q.in.trustdom_handle = &trustdom_handle;
1422         q.in.level = LSA_TRUSTED_DOMAIN_INFO_NAME;
1423         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
1424         if (!NT_STATUS_IS_OK(status)) {
1425                 printf("QueryTrustedDomainInfo level 1 failed - %s\n", nt_errstr(status));
1426                 ret = False;
1427         }
1428         if (!q.out.info) {
1429                 ret = False;
1430         } else {
1431                 if (strcmp(q.out.info->name.netbios_name.string, trustinfo.name.string) != 0) {
1432                         printf("QueryTrustedDomainInfo returned inconsistant short name: %s != %s\n",
1433                                q.out.info->name.netbios_name.string, trustinfo.name.string);
1434                         ret = False;
1435                 }
1436         }
1437
1438         /* now that we have some domains to look over, we can test the enum calls */
1439         if (!test_EnumTrustDom(p, mem_ctx, handle)) {
1440                 ret = False;
1441         }
1442
1443         if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
1444                 ret = False;
1445         }
1446
1447         return ret;
1448 }
1449
1450 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p, 
1451                                  TALLOC_CTX *mem_ctx, 
1452                                  struct policy_handle *handle)
1453 {
1454         struct lsa_QueryInfoPolicy r;
1455         NTSTATUS status;
1456         int i;
1457         BOOL ret = True;
1458         printf("\nTesting QueryInfoPolicy\n");
1459
1460         for (i=1;i<13;i++) {
1461                 r.in.handle = handle;
1462                 r.in.level = i;
1463
1464                 printf("\ntrying QueryInfoPolicy level %d\n", i);
1465
1466                 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
1467
1468                 if ((i == 9 || i == 10 || i == 11) &&
1469                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1470                         printf("server failed level %u (OK)\n", i);
1471                         continue;
1472                 }
1473
1474                 if (!NT_STATUS_IS_OK(status)) {
1475                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
1476                         ret = False;
1477                         continue;
1478                 }
1479         }
1480
1481         return ret;
1482 }
1483
1484 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p, 
1485                                   TALLOC_CTX *mem_ctx, 
1486                                   struct policy_handle *handle)
1487 {
1488         struct lsa_QueryInfoPolicy2 r;
1489         NTSTATUS status;
1490         int i;
1491         BOOL ret = True;
1492         printf("\nTesting QueryInfoPolicy2\n");
1493
1494         for (i=1;i<13;i++) {
1495                 r.in.handle = handle;
1496                 r.in.level = i;
1497
1498                 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
1499
1500                 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
1501
1502                 if ((i == 9 || i == 10 || i == 11) &&
1503                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
1504                         printf("server failed level %u (OK)\n", i);
1505                         continue;
1506                 }
1507
1508                 if (!NT_STATUS_IS_OK(status)) {
1509                         printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
1510                         ret = False;
1511                         continue;
1512                 }
1513         }
1514
1515         return ret;
1516 }
1517
1518 static BOOL test_GetUserName(struct dcerpc_pipe *p, 
1519                                   TALLOC_CTX *mem_ctx, 
1520                                   struct policy_handle *handle)
1521 {
1522         struct lsa_GetUserName r;
1523         NTSTATUS status;
1524         BOOL ret = True;
1525         struct lsa_StringPointer authority_name_p;
1526
1527         printf("\nTesting GetUserName\n");
1528
1529         r.in.system_name = "\\";        
1530         r.in.account_name = NULL;       
1531         r.in.authority_name = &authority_name_p;
1532         authority_name_p.string = NULL;
1533
1534         status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
1535
1536         if (!NT_STATUS_IS_OK(status)) {
1537                 printf("GetUserName failed - %s\n", nt_errstr(status));
1538                 ret = False;
1539         }
1540
1541         return ret;
1542 }
1543
1544 BOOL test_lsa_Close(struct dcerpc_pipe *p, 
1545                     TALLOC_CTX *mem_ctx, 
1546                     struct policy_handle *handle)
1547 {
1548         NTSTATUS status;
1549         struct lsa_Close r;
1550         struct policy_handle handle2;
1551
1552         printf("\ntesting Close\n");
1553
1554         r.in.handle = handle;
1555         r.out.handle = &handle2;
1556
1557         status = dcerpc_lsa_Close(p, mem_ctx, &r);
1558         if (!NT_STATUS_IS_OK(status)) {
1559                 printf("Close failed - %s\n", nt_errstr(status));
1560                 return False;
1561         }
1562
1563         status = dcerpc_lsa_Close(p, mem_ctx, &r);
1564         /* its really a fault - we need a status code for rpc fault */
1565         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
1566                 printf("Close failed - %s\n", nt_errstr(status));
1567                 return False;
1568         }
1569
1570         printf("\n");
1571
1572         return True;
1573 }
1574
1575 BOOL torture_rpc_lsa(void)
1576 {
1577         NTSTATUS status;
1578         struct dcerpc_pipe *p;
1579         TALLOC_CTX *mem_ctx;
1580         BOOL ret = True;
1581         struct policy_handle handle;
1582
1583         mem_ctx = talloc_init("torture_rpc_lsa");
1584
1585         status = torture_rpc_connection(&p, 
1586                                         DCERPC_LSARPC_NAME, 
1587                                         DCERPC_LSARPC_UUID, 
1588                                         DCERPC_LSARPC_VERSION);
1589         if (!NT_STATUS_IS_OK(status)) {
1590                 return False;
1591         }
1592
1593         if (!test_OpenPolicy(p, mem_ctx)) {
1594                 ret = False;
1595         }
1596
1597         if (!test_lsa_OpenPolicy2(p, mem_ctx, &handle)) {
1598                 ret = False;
1599         }
1600
1601         if (!test_many_LookupSids(p, mem_ctx, &handle)) {
1602                 ret = False;
1603         }
1604
1605         if (!test_CreateAccount(p, mem_ctx, &handle)) {
1606                 ret = False;
1607         }
1608
1609         if (!test_CreateSecret(p, mem_ctx, &handle)) {
1610                 ret = False;
1611         }
1612
1613         if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
1614                 ret = False;
1615         }
1616
1617         if (!test_EnumAccounts(p, mem_ctx, &handle)) {
1618                 ret = False;
1619         }
1620
1621         if (!test_EnumPrivs(p, mem_ctx, &handle)) {
1622                 ret = False;
1623         }
1624
1625         if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
1626                 ret = False;
1627         }
1628
1629         if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
1630                 ret = False;
1631         }
1632
1633         if (!test_GetUserName(p, mem_ctx, &handle)) {
1634                 ret = False;
1635         }
1636
1637 #if 0
1638         if (!test_Delete(p, mem_ctx, &handle)) {
1639                 ret = False;
1640         }
1641 #endif
1642         
1643         if (!test_lsa_Close(p, mem_ctx, &handle)) {
1644                 ret = False;
1645         }
1646
1647         talloc_destroy(mem_ctx);
1648
1649         torture_rpc_close(p);
1650
1651         return ret;
1652 }