r3917: A few more LSA RPCs found in my wanderings (for trusted domains, these
[samba.git] / source / 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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_lsa.h"
24
25 static void init_lsa_String(struct lsa_String *name, const char *s)
26 {
27         name->string = s;
28 }
29
30 static BOOL test_OpenPolicy(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
31 {
32         struct lsa_ObjectAttribute attr;
33         struct policy_handle handle;
34         struct lsa_QosInfo qos;
35         struct lsa_OpenPolicy r;
36         NTSTATUS status;
37         uint16_t system_name = '\\';
38
39         printf("\ntesting OpenPolicy\n");
40
41         qos.len = 0;
42         qos.impersonation_level = 2;
43         qos.context_mode = 1;
44         qos.effective_only = 0;
45
46         attr.len = 0;
47         attr.root_dir = NULL;
48         attr.object_name = NULL;
49         attr.attributes = 0;
50         attr.sec_desc = NULL;
51         attr.sec_qos = &qos;
52
53         r.in.system_name = &system_name;
54         r.in.attr = &attr;
55         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
56         r.out.handle = &handle;
57
58         status = dcerpc_lsa_OpenPolicy(p, mem_ctx, &r);
59         if (!NT_STATUS_IS_OK(status)) {
60                 printf("OpenPolicy failed - %s\n", nt_errstr(status));
61                 return False;
62         }
63
64         return True;
65 }
66
67
68 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
69                              struct policy_handle *handle)
70 {
71         struct lsa_ObjectAttribute attr;
72         struct lsa_QosInfo qos;
73         struct lsa_OpenPolicy2 r;
74         NTSTATUS status;
75
76         printf("\ntesting OpenPolicy2\n");
77
78         qos.len = 0;
79         qos.impersonation_level = 2;
80         qos.context_mode = 1;
81         qos.effective_only = 0;
82
83         attr.len = 0;
84         attr.root_dir = NULL;
85         attr.object_name = NULL;
86         attr.attributes = 0;
87         attr.sec_desc = NULL;
88         attr.sec_qos = &qos;
89
90         r.in.system_name = "\\";
91         r.in.attr = &attr;
92         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
93         r.out.handle = handle;
94
95         status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
96         if (!NT_STATUS_IS_OK(status)) {
97                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
98                 return False;
99         }
100
101         return True;
102 }
103
104 static BOOL test_LookupNames(struct dcerpc_pipe *p, 
105                             TALLOC_CTX *mem_ctx, 
106                             struct policy_handle *handle,
107                             struct lsa_TransNameArray *tnames)
108 {
109         struct lsa_LookupNames r;
110         struct lsa_TransSidArray sids;
111         struct lsa_String *names;
112         uint32_t count = 0;
113         NTSTATUS status;
114         int i;
115
116         printf("\nTesting LookupNames with %d names\n", tnames->count);
117
118         sids.count = 0;
119         sids.sids = NULL;
120
121         names = talloc(mem_ctx, tnames->count * sizeof(names[0]));
122         for (i=0;i<tnames->count;i++) {
123                 init_lsa_String(&names[i], tnames->names[i].name.string);
124         }
125
126         r.in.handle = handle;
127         r.in.num_names = tnames->count;
128         r.in.names = names;
129         r.in.sids = &sids;
130         r.in.level = 1;
131         r.in.count = &count;
132         r.out.count = &count;
133         r.out.sids = &sids;
134
135         status = dcerpc_lsa_LookupNames(p, mem_ctx, &r);
136         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
137                 printf("LookupNames failed - %s\n", nt_errstr(status));
138                 return False;
139         }
140
141         printf("\n");
142
143         return True;
144 }
145
146
147 static BOOL test_LookupSids(struct dcerpc_pipe *p, 
148                             TALLOC_CTX *mem_ctx, 
149                             struct policy_handle *handle,
150                             struct lsa_SidArray *sids)
151 {
152         struct lsa_LookupSids r;
153         struct lsa_TransNameArray names;
154         uint32_t count = sids->num_sids;
155         NTSTATUS status;
156
157         printf("\nTesting LookupSids\n");
158
159         names.count = 0;
160         names.names = NULL;
161
162         r.in.handle = handle;
163         r.in.sids = sids;
164         r.in.names = &names;
165         r.in.level = 1;
166         r.in.count = &count;
167         r.out.count = &count;
168         r.out.names = &names;
169
170         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
171         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
172                 printf("LookupSids failed - %s\n", nt_errstr(status));
173                 return False;
174         }
175
176         printf("\n");
177
178         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
179                 return False;
180         }
181
182         return True;
183 }
184
185 static BOOL test_many_LookupSids(struct dcerpc_pipe *p, 
186                                  TALLOC_CTX *mem_ctx, 
187                                  struct policy_handle *handle)
188 {
189         struct lsa_LookupSids r;
190         struct lsa_TransNameArray names;
191         uint32_t count;
192         NTSTATUS status;
193         struct lsa_SidArray sids;
194         int i;
195
196         printf("\nTesting LookupSids with lots of SIDs\n");
197
198         names.count = 0;
199         names.names = NULL;
200
201         sids.num_sids = 1000;
202
203         sids.sids = talloc_array_p(mem_ctx, struct lsa_SidPtr, sids.num_sids);
204
205         for (i=0; i<sids.num_sids; i++) {
206                 const char *sidstr = "S-1-5-32-545";
207                 sids.sids[i].sid = dom_sid_parse_talloc(mem_ctx, sidstr);
208         }
209
210         count = sids.num_sids;
211
212         r.in.handle = handle;
213         r.in.sids = &sids;
214         r.in.names = &names;
215         r.in.level = 1;
216         r.in.count = &names.count;
217         r.out.count = &count;
218         r.out.names = &names;
219
220         status = dcerpc_lsa_LookupSids(p, mem_ctx, &r);
221         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
222                 printf("LookupSids failed - %s\n", nt_errstr(status));
223                 return False;
224         }
225
226         printf("\n");
227
228         if (!test_LookupNames(p, mem_ctx, handle, &names)) {
229                 return False;
230         }
231
232         return True;
233 }
234
235 static BOOL test_LookupPrivName(struct dcerpc_pipe *p, 
236                                 TALLOC_CTX *mem_ctx, 
237                                 struct policy_handle *handle,
238                                 struct lsa_LUID *luid)
239 {
240         NTSTATUS status;
241         struct lsa_LookupPrivName r;
242
243         r.in.handle = handle;
244         r.in.luid = luid;
245
246         status = dcerpc_lsa_LookupPrivName(p, mem_ctx, &r);
247         if (!NT_STATUS_IS_OK(status)) {
248                 printf("\nLookupPrivName failed - %s\n", nt_errstr(status));
249                 return False;
250         }
251
252         return True;
253 }
254
255 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p, 
256                                   TALLOC_CTX *mem_ctx,                            
257                                   struct policy_handle *handle,
258                                   struct policy_handle *acct_handle)
259 {
260         NTSTATUS status;
261         struct lsa_EnumPrivsAccount r;
262
263         printf("Testing EnumPrivsAccount\n");
264
265         r.in.handle = acct_handle;
266
267         status = dcerpc_lsa_EnumPrivsAccount(p, mem_ctx, &r);
268         if (!NT_STATUS_IS_OK(status)) {
269                 printf("EnumPrivsAccount failed - %s\n", nt_errstr(status));
270                 return False;
271         }
272
273         if (r.out.privs) {
274                 int i;
275                 for (i=0;i<r.out.privs->count;i++) {
276                         test_LookupPrivName(p, mem_ctx, handle, 
277                                             &r.out.privs->set[i].luid);
278                 }
279         }
280
281         return True;
282 }
283
284 static BOOL test_Delete(struct dcerpc_pipe *p, 
285                        TALLOC_CTX *mem_ctx, 
286                        struct policy_handle *handle)
287 {
288         NTSTATUS status;
289         struct lsa_Delete r;
290
291         printf("\ntesting Delete\n");
292
293         r.in.handle = handle;
294         status = dcerpc_lsa_Delete(p, mem_ctx, &r);
295         if (!NT_STATUS_IS_OK(status)) {
296                 printf("Delete failed - %s\n", nt_errstr(status));
297                 return False;
298         }
299
300         printf("\n");
301
302         return True;
303 }
304
305
306 static BOOL test_CreateAccount(struct dcerpc_pipe *p, 
307                                TALLOC_CTX *mem_ctx, 
308                                struct policy_handle *handle)
309 {
310         NTSTATUS status;
311         struct lsa_CreateAccount r;
312         struct dom_sid2 *newsid;
313         struct policy_handle acct_handle;
314
315         newsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-12349876-4321-2854");
316
317         printf("Testing CreateAccount\n");
318
319         r.in.handle = handle;
320         r.in.sid = newsid;
321         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
322         r.out.acct_handle = &acct_handle;
323
324         status = dcerpc_lsa_CreateAccount(p, mem_ctx, &r);
325         if (!NT_STATUS_IS_OK(status)) {
326                 printf("CreateAccount failed - %s\n", nt_errstr(status));
327                 return False;
328         }
329
330         if (!test_Delete(p, mem_ctx, &acct_handle)) {
331                 return False;
332         }
333
334         return True;
335 }
336
337
338 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p, 
339                                      TALLOC_CTX *mem_ctx, 
340                                      struct policy_handle *handle)
341 {
342         NTSTATUS status;
343         struct lsa_CreateTrustedDomain r;
344         struct lsa_TrustInformation trustinfo;
345         struct dom_sid *domsid;
346         struct policy_handle dom_handle;
347
348         printf("Testing CreateTrustedDomain\n");
349
350         domsid = dom_sid_parse_talloc(mem_ctx, "S-1-5-21-97398-379795-12345");
351
352         trustinfo.sid = domsid;
353         init_lsa_String(&trustinfo.name, "torturedomain");
354
355         r.in.handle = handle;
356         r.in.info = &trustinfo;
357         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
358         r.out.dom_handle = &dom_handle;
359
360         status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
361         if (!NT_STATUS_IS_OK(status)) {
362                 printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
363                 return False;
364         }
365
366         if (!test_Delete(p, mem_ctx, &dom_handle)) {
367                 return False;
368         }
369
370         return True;
371 }
372
373 static BOOL test_CreateSecret(struct dcerpc_pipe *p, 
374                               TALLOC_CTX *mem_ctx, 
375                               struct policy_handle *handle)
376 {
377         NTSTATUS status;
378         struct lsa_CreateSecret r;
379         struct lsa_OpenSecret r2;
380         struct lsa_SetSecret r3;
381         struct lsa_QuerySecret r4;
382         struct policy_handle sec_handle, sec_handle2;
383         struct lsa_Delete d;
384         struct lsa_DATA_BUF buf1;
385         struct lsa_DATA_BUF_PTR bufp1;
386         DATA_BLOB enc_key;
387         BOOL ret = True;
388         DATA_BLOB session_key;
389         NTTIME old_mtime, new_mtime;
390         DATA_BLOB blob1, blob2;
391         const char *secret1 = "abcdef12345699qwerty";
392         char *secret2;
393         char *secname;
394
395         printf("Testing CreateSecret\n");
396
397         asprintf(&secname, "torturesecret-%u", (uint_t)random());
398
399         init_lsa_String(&r.in.name, secname);
400
401         r.in.handle = handle;
402         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
403         r.out.sec_handle = &sec_handle;
404
405         status = dcerpc_lsa_CreateSecret(p, mem_ctx, &r);
406         if (!NT_STATUS_IS_OK(status)) {
407                 printf("CreateSecret failed - %s\n", nt_errstr(status));
408                 return False;
409         }
410
411         r2.in.handle = handle;
412         r2.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
413         r2.in.name = r.in.name;
414         r2.out.sec_handle = &sec_handle2;
415
416         printf("Testing OpenSecret\n");
417
418         status = dcerpc_lsa_OpenSecret(p, mem_ctx, &r2);
419         if (!NT_STATUS_IS_OK(status)) {
420                 printf("OpenSecret failed - %s\n", nt_errstr(status));
421                 ret = False;
422         }
423
424         status = dcerpc_fetch_session_key(p, &session_key);
425         if (!NT_STATUS_IS_OK(status)) {
426                 printf("dcerpc_fetch_session_key failed - %s\n", nt_errstr(status));
427                 ret = False;
428         }
429
430         enc_key = sess_encrypt_string(secret1, &session_key);
431
432         r3.in.handle = &sec_handle;
433         r3.in.new_val = &buf1;
434         r3.in.old_val = NULL;
435         r3.in.new_val->data = enc_key.data;
436         r3.in.new_val->length = enc_key.length;
437         r3.in.new_val->size = enc_key.length;
438
439         printf("Testing SetSecret\n");
440
441         status = dcerpc_lsa_SetSecret(p, mem_ctx, &r3);
442         if (!NT_STATUS_IS_OK(status)) {
443                 printf("SetSecret failed - %s\n", nt_errstr(status));
444                 ret = False;
445         }
446
447         data_blob_free(&enc_key);
448
449         ZERO_STRUCT(new_mtime);
450         ZERO_STRUCT(old_mtime);
451
452         /* fetch the secret back again */
453         r4.in.handle = &sec_handle;
454         r4.in.new_val = &bufp1;
455         r4.in.new_mtime = &new_mtime;
456         r4.in.old_val = NULL;
457         r4.in.old_mtime = NULL;
458
459         bufp1.buf = NULL;
460
461         status = dcerpc_lsa_QuerySecret(p, mem_ctx, &r4);
462         if (!NT_STATUS_IS_OK(status)) {
463                 printf("QuerySecret failed - %s\n", nt_errstr(status));
464                 ret = False;
465         }
466
467         if (r4.out.new_val->buf == NULL) {
468                 printf("No secret buffer returned\n");
469                 ret = False;
470         } else {
471                 blob1.data = r4.out.new_val->buf->data;
472                 blob1.length = r4.out.new_val->buf->length;
473
474                 blob2 = data_blob(NULL, blob1.length);
475
476                 secret2 = sess_decrypt_string(&blob1, &session_key);
477
478                 printf("returned secret '%s'\n", secret2);
479
480                 if (strcmp(secret1, secret2) != 0) {
481                         printf("Returned secret doesn't match\n");
482                         ret = False;
483                 }
484         }
485
486         if (!test_Delete(p, mem_ctx, &sec_handle)) {
487                 ret = False;
488         }
489
490         d.in.handle = &sec_handle2;
491         status = dcerpc_lsa_Delete(p, mem_ctx, &d);
492         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
493                 printf("Second delete expected INVALID_HANDLE - %s\n", nt_errstr(status));
494                 ret = False;
495         }
496
497         return ret;
498 }
499
500
501 static BOOL test_EnumAccountRights(struct dcerpc_pipe *p, 
502                                    TALLOC_CTX *mem_ctx, 
503                                    struct policy_handle *acct_handle,
504                                    struct dom_sid *sid)
505 {
506         NTSTATUS status;
507         struct lsa_EnumAccountRights r;
508         struct lsa_RightSet rights;
509
510         printf("Testing EnumAccountRights\n");
511
512         r.in.handle = acct_handle;
513         r.in.sid = sid;
514         r.out.rights = &rights;
515
516         status = dcerpc_lsa_EnumAccountRights(p, mem_ctx, &r);
517         if (!NT_STATUS_IS_OK(status)) {
518                 printf("EnumAccountRights failed - %s\n", nt_errstr(status));
519                 return False;
520         }
521
522         return True;
523 }
524
525
526 static BOOL test_QuerySecurity(struct dcerpc_pipe *p, 
527                              TALLOC_CTX *mem_ctx, 
528                              struct policy_handle *handle,
529                              struct policy_handle *acct_handle)
530 {
531         NTSTATUS status;
532         struct lsa_QuerySecurity r;
533
534         printf("Testing QuerySecuriy\n");
535
536         r.in.handle = acct_handle;
537         r.in.sec_info = 7;
538
539         status = dcerpc_lsa_QuerySecurity(p, mem_ctx, &r);
540         if (!NT_STATUS_IS_OK(status)) {
541                 printf("QuerySecurity failed - %s\n", nt_errstr(status));
542                 return False;
543         }
544
545         return True;
546 }
547
548 static BOOL test_OpenAccount(struct dcerpc_pipe *p, 
549                              TALLOC_CTX *mem_ctx, 
550                              struct policy_handle *handle,
551                              struct dom_sid *sid)
552 {
553         NTSTATUS status;
554         struct lsa_OpenAccount r;
555         struct policy_handle acct_handle;
556
557         printf("Testing OpenAccount\n");
558
559         r.in.handle = handle;
560         r.in.sid = sid;
561         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
562         r.out.acct_handle = &acct_handle;
563
564         status = dcerpc_lsa_OpenAccount(p, mem_ctx, &r);
565         if (!NT_STATUS_IS_OK(status)) {
566                 printf("OpenAccount failed - %s\n", nt_errstr(status));
567                 return False;
568         }
569
570         if (!test_EnumPrivsAccount(p, mem_ctx, handle, &acct_handle)) {
571                 return False;
572         }
573
574         if (!test_QuerySecurity(p, mem_ctx, handle, &acct_handle)) {
575                 return False;
576         }
577
578         return True;
579 }
580
581 static BOOL test_EnumAccounts(struct dcerpc_pipe *p, 
582                           TALLOC_CTX *mem_ctx, 
583                           struct policy_handle *handle)
584 {
585         NTSTATUS status;
586         struct lsa_EnumAccounts r;
587         struct lsa_SidArray sids1, sids2;
588         uint32_t resume_handle = 0;
589         int i;
590
591         printf("\ntesting EnumAccounts\n");
592
593         r.in.handle = handle;
594         r.in.resume_handle = &resume_handle;
595         r.in.num_entries = 100;
596         r.out.resume_handle = &resume_handle;
597         r.out.sids = &sids1;
598
599         resume_handle = 0;
600         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
601         if (!NT_STATUS_IS_OK(status)) {
602                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
603                 return False;
604         }
605
606         if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
607                 return False;
608         }
609
610         printf("testing all accounts\n");
611         for (i=0;i<sids1.num_sids;i++) {
612                 test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
613                 test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
614         }
615         printf("\n");
616
617         if (sids1.num_sids < 3) {
618                 return True;
619         }
620         
621         printf("trying EnumAccounts partial listing (asking for 1 at 2)\n");
622         resume_handle = 2;
623         r.in.num_entries = 1;
624         r.out.sids = &sids2;
625
626         status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
627         if (!NT_STATUS_IS_OK(status)) {
628                 printf("EnumAccounts failed - %s\n", nt_errstr(status));
629                 return False;
630         }
631
632         if (sids2.num_sids != 1) {
633                 printf("Returned wrong number of entries (%d)\n", sids2.num_sids);
634                 return False;
635         }
636
637         return True;
638 }
639
640 static BOOL test_LookupPrivDisplayName(struct dcerpc_pipe *p,
641                                 TALLOC_CTX *mem_ctx,
642                                 struct policy_handle *handle,
643                                 struct lsa_String *priv_name)
644 {
645         struct lsa_LookupPrivDisplayName r;
646         NTSTATUS status;
647         /* produce a reasonable range of language output without screwing up
648            terminals */
649         uint16 language_id = (random() % 4) + 0x409;
650
651         printf("testing LookupPrivDisplayName(%s)\n", priv_name->string);
652         
653         r.in.handle = handle;
654         r.in.name = priv_name;
655         r.in.language_id = &language_id;
656         r.out.language_id = &language_id;
657         r.in.unknown = 0;
658
659         status = dcerpc_lsa_LookupPrivDisplayName(p, mem_ctx, &r);
660         if (!NT_STATUS_IS_OK(status)) {
661                 printf("LookupPrivDisplayName failed - %s\n", nt_errstr(status));
662                 return False;
663         }
664         printf("%s -> \"%s\"  (language 0x%x/0x%x)\n", 
665                priv_name->string, r.out.disp_name->string, 
666                *r.in.language_id, *r.out.language_id);
667
668         return True;
669 }
670
671 static BOOL test_EnumAccountsWithUserRight(struct dcerpc_pipe *p, 
672                                 TALLOC_CTX *mem_ctx,
673                                 struct policy_handle *handle,
674                                 struct lsa_String *priv_name)
675 {
676         struct lsa_EnumAccountsWithUserRight r;
677         struct lsa_SidArray sids;
678         NTSTATUS status;
679
680         ZERO_STRUCT(sids);
681         
682         printf("testing EnumAccountsWithUserRight(%s)\n", priv_name->string);
683         
684         r.in.handle = handle;
685         r.in.name = priv_name;
686         r.out.sids = &sids;
687
688         status = dcerpc_lsa_EnumAccountsWithUserRight(p, mem_ctx, &r);
689
690         /* NT_STATUS_NO_MORE_ENTRIES means noone has this privilege */
691         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
692                 return True;
693         }
694
695         if (!NT_STATUS_IS_OK(status)) {
696                 printf("EnumAccountsWithUserRight failed - %s\n", nt_errstr(status));
697                 return False;
698         }
699         
700         return True;
701 }
702
703
704 static BOOL test_EnumPrivs(struct dcerpc_pipe *p, 
705                            TALLOC_CTX *mem_ctx, 
706                            struct policy_handle *handle)
707 {
708         NTSTATUS status;
709         struct lsa_EnumPrivs r;
710         struct lsa_PrivArray privs1;
711         uint32_t resume_handle = 0;
712         int i;
713         BOOL ret = True;
714
715         printf("\ntesting EnumPrivs\n");
716
717         r.in.handle = handle;
718         r.in.resume_handle = &resume_handle;
719         r.in.max_count = 1000;
720         r.out.resume_handle = &resume_handle;
721         r.out.privs = &privs1;
722
723         resume_handle = 0;
724         status = dcerpc_lsa_EnumPrivs(p, mem_ctx, &r);
725         if (!NT_STATUS_IS_OK(status)) {
726                 printf("EnumPrivs failed - %s\n", nt_errstr(status));
727                 return False;
728         }
729
730         for (i = 0; i< privs1.count; i++) {
731                 test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
732                 if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
733                         ret = False;
734                 }
735         }
736
737         return ret;
738 }
739
740
741 static BOOL test_EnumTrustDom(struct dcerpc_pipe *p, 
742                               TALLOC_CTX *mem_ctx, 
743                               struct policy_handle *handle)
744 {
745         struct lsa_EnumTrustDom r;
746         NTSTATUS status;
747         uint32_t resume_handle = 0;
748         struct lsa_DomainList domains;
749         int i,j;
750         BOOL ret = True;
751
752         printf("\nTesting EnumTrustDom\n");
753
754         r.in.handle = handle;
755         r.in.resume_handle = &resume_handle;
756         r.in.num_entries = 1000;
757         r.out.domains = &domains;
758         r.out.resume_handle = &resume_handle;
759
760         status = dcerpc_lsa_EnumTrustDom(p, mem_ctx, &r);
761
762         /* NO_MORE_ENTRIES is allowed */
763         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
764                 return True;
765         }
766
767         if (!NT_STATUS_IS_OK(status)) {
768                 printf("EnumTrustDom failed - %s\n", nt_errstr(status));
769                 return False;
770         }
771
772         printf("\nTesting OpenTrustedDomain, OpenTrustedDomainByName and QueryInfoTrustedDomain\n");
773
774         for (i=0; i< domains.count; i++) {
775                 struct lsa_OpenTrustedDomain trust;
776                 struct lsa_OpenTrustedDomainByName trust_by_name;
777                 struct policy_handle trustdom_handle;
778                 struct policy_handle handle2;
779                 struct lsa_Close c;
780                 int levels [] = {1, 3, 6};
781                 
782                 trust.in.handle = handle;
783                 trust.in.sid = domains.domains[i].sid;
784                 trust.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
785                 trust.out.trustdom_handle = &trustdom_handle;
786
787                 status = dcerpc_lsa_OpenTrustedDomain(p, mem_ctx, &trust);
788
789                 if (!NT_STATUS_IS_OK(status)) {
790                         printf("OpenTrustedDomain failed - %s\n", nt_errstr(status));
791                         return False;
792                 }
793
794                 c.in.handle = &trustdom_handle;
795                 c.out.handle = &handle2;
796                 
797                 for (j=0; j < ARRAY_SIZE(levels); j++) {
798                         struct lsa_QueryTrustedDomainInfo q;
799                         union lsa_TrustedDomainInfo info;
800                         q.in.trustdom_handle = &trustdom_handle;
801                         q.in.level = levels[j];
802                         q.out.info = &info;
803                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
804                         if (!NT_STATUS_IS_OK(status)) {
805                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
806                                        levels[j], nt_errstr(status));
807                                 ret = False;
808                         }
809                 }
810                 
811                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
812                 if (!NT_STATUS_IS_OK(status)) {
813                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
814                         return False;
815                 }
816
817                 trust_by_name.in.handle = handle;
818                 trust_by_name.in.name = domains.domains[i].name;
819                 trust_by_name.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
820                 trust_by_name.out.trustdom_handle = &trustdom_handle;
821                 
822                 status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &trust_by_name);
823
824                 if (!NT_STATUS_IS_OK(status)) {
825                         printf("OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
826                         return False;
827                 }
828
829                 for (j=0; j < ARRAY_SIZE(levels); j++) {
830                         struct lsa_QueryTrustedDomainInfo q;
831                         union lsa_TrustedDomainInfo info;
832                         q.in.trustdom_handle = &trustdom_handle;
833                         q.in.level = levels[j];
834                         q.out.info = &info;
835                         status = dcerpc_lsa_QueryTrustedDomainInfo(p, mem_ctx, &q);
836                         if (!NT_STATUS_IS_OK(status)) {
837                                 printf("QueryTrustedDomainInfo level %d failed - %s\n", 
838                                        levels[j], nt_errstr(status));
839                                 ret = False;
840                         }
841                 }
842                 
843                 c.in.handle = &trustdom_handle;
844                 c.out.handle = &handle2;
845
846                 status = dcerpc_lsa_Close(p, mem_ctx, &c);
847                 if (!NT_STATUS_IS_OK(status)) {
848                         printf("Close of trusted domain failed - %s\n", nt_errstr(status));
849                         return False;
850                 }
851
852                 for (j=0; j < ARRAY_SIZE(levels); j++) {
853                         struct lsa_QueryTrustedDomainInfoBySid q;
854                         union lsa_TrustedDomainInfo info;
855                         q.in.handle  = handle;
856                         q.in.dom_sid = domains.domains[i].sid;
857                         q.in.level   = levels[j];
858                         q.out.info   = &info;
859                         status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, mem_ctx, &q);
860                         if (!NT_STATUS_IS_OK(status)) {
861                                 printf("QueryTrustedDomainInfoBySid level %d failed - %s\n", 
862                                        levels[j], nt_errstr(status));
863                                 ret = False;
864                         }
865                 }
866                 
867                 for (j=0; j < ARRAY_SIZE(levels); j++) {
868                         struct lsa_QueryTrustedDomainInfoByName q;
869                         union lsa_TrustedDomainInfo info;
870                         q.in.handle         = handle;
871                         q.in.trusted_domain = domains.domains[i].name;
872                         q.in.level          = levels[j];
873                         q.out.info          = &info;
874                         status = dcerpc_lsa_QueryTrustedDomainInfoByName(p, mem_ctx, &q);
875                         if (!NT_STATUS_IS_OK(status)) {
876                                 printf("QueryTrustedDomainInfoByName level %d failed - %s\n", 
877                                        levels[j], nt_errstr(status));
878                                 ret = False;
879                         }
880                 }
881                 
882                 
883
884         }
885
886         return ret;
887 }
888
889 static BOOL test_QueryInfoPolicy(struct dcerpc_pipe *p, 
890                                  TALLOC_CTX *mem_ctx, 
891                                  struct policy_handle *handle)
892 {
893         struct lsa_QueryInfoPolicy r;
894         NTSTATUS status;
895         int i;
896         BOOL ret = True;
897         printf("\nTesting QueryInfoPolicy\n");
898
899         for (i=1;i<13;i++) {
900                 r.in.handle = handle;
901                 r.in.level = i;
902
903                 printf("\ntrying QueryInfoPolicy level %d\n", i);
904
905                 status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
906
907                 if ((i == 9 || i == 10 || i == 11) &&
908                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
909                         printf("server failed level %u (OK)\n", i);
910                         continue;
911                 }
912
913                 if (!NT_STATUS_IS_OK(status)) {
914                         printf("QueryInfoPolicy failed - %s\n", nt_errstr(status));
915                         ret = False;
916                         continue;
917                 }
918         }
919
920         return ret;
921 }
922
923 static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p, 
924                                   TALLOC_CTX *mem_ctx, 
925                                   struct policy_handle *handle)
926 {
927         struct lsa_QueryInfoPolicy2 r;
928         NTSTATUS status;
929         int i;
930         BOOL ret = True;
931         printf("\nTesting QueryInfoPolicy2\n");
932
933         for (i=1;i<13;i++) {
934                 r.in.handle = handle;
935                 r.in.level = i;
936
937                 printf("\ntrying QueryInfoPolicy2 level %d\n", i);
938
939                 status = dcerpc_lsa_QueryInfoPolicy2(p, mem_ctx, &r);
940
941                 if ((i == 9 || i == 10 || i == 11) &&
942                     NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
943                         printf("server failed level %u (OK)\n", i);
944                         continue;
945                 }
946
947                 if (!NT_STATUS_IS_OK(status)) {
948                         printf("QueryInfoPolicy2 failed - %s\n", nt_errstr(status));
949                         ret = False;
950                         continue;
951                 }
952         }
953
954         return ret;
955 }
956
957 static BOOL test_Close(struct dcerpc_pipe *p, 
958                        TALLOC_CTX *mem_ctx, 
959                        struct policy_handle *handle)
960 {
961         NTSTATUS status;
962         struct lsa_Close r;
963         struct policy_handle handle2;
964
965         printf("\ntesting Close\n");
966
967         r.in.handle = handle;
968         r.out.handle = &handle2;
969
970         status = dcerpc_lsa_Close(p, mem_ctx, &r);
971         if (!NT_STATUS_IS_OK(status)) {
972                 printf("Close failed - %s\n", nt_errstr(status));
973                 return False;
974         }
975
976         status = dcerpc_lsa_Close(p, mem_ctx, &r);
977         /* its really a fault - we need a status code for rpc fault */
978         if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
979                 printf("Close failed - %s\n", nt_errstr(status));
980                 return False;
981         }
982
983         printf("\n");
984
985         return True;
986 }
987
988 BOOL torture_rpc_lsa(void)
989 {
990         NTSTATUS status;
991         struct dcerpc_pipe *p;
992         TALLOC_CTX *mem_ctx;
993         BOOL ret = True;
994         struct policy_handle handle;
995
996         mem_ctx = talloc_init("torture_rpc_lsa");
997
998         status = torture_rpc_connection(&p, 
999                                         DCERPC_LSARPC_NAME, 
1000                                         DCERPC_LSARPC_UUID, 
1001                                         DCERPC_LSARPC_VERSION);
1002         if (!NT_STATUS_IS_OK(status)) {
1003                 return False;
1004         }
1005
1006         if (!test_OpenPolicy(p, mem_ctx)) {
1007                 ret = False;
1008         }
1009
1010         if (!test_OpenPolicy2(p, mem_ctx, &handle)) {
1011                 ret = False;
1012         }
1013
1014         if (!test_many_LookupSids(p, mem_ctx, &handle)) {
1015                 ret = False;
1016         }
1017
1018         if (!test_CreateAccount(p, mem_ctx, &handle)) {
1019                 ret = False;
1020         }
1021
1022         if (!test_CreateSecret(p, mem_ctx, &handle)) {
1023                 ret = False;
1024         }
1025
1026         if (!test_CreateTrustedDomain(p, mem_ctx, &handle)) {
1027                 ret = False;
1028         }
1029
1030         if (!test_EnumAccounts(p, mem_ctx, &handle)) {
1031                 ret = False;
1032         }
1033
1034         if (!test_EnumPrivs(p, mem_ctx, &handle)) {
1035                 ret = False;
1036         }
1037
1038         if (!test_EnumTrustDom(p, mem_ctx, &handle)) {
1039                 ret = False;
1040         }
1041
1042         if (!test_QueryInfoPolicy(p, mem_ctx, &handle)) {
1043                 ret = False;
1044         }
1045
1046         if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
1047                 ret = False;
1048         }
1049         
1050 #if 0
1051         if (!test_Delete(p, mem_ctx, &handle)) {
1052                 ret = False;
1053         }
1054 #endif
1055         
1056         if (!test_Close(p, mem_ctx, &handle)) {
1057                 ret = False;
1058         }
1059
1060         talloc_destroy(mem_ctx);
1061
1062         torture_rpc_close(p);
1063
1064         return ret;
1065 }