r5940: fix schannel against w2k, it skips the confounder in the signature (24 bytes...
[nivanova/samba-autobuild/.git] / source4 / torture / rpc / netlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for netlogon rpc operations
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8    Copyright (C) Tim Potter      2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/ndr_netlogon.h"
28 #include "auth/auth.h"
29
30 static const char *machine_password;
31
32 #define TEST_MACHINE_NAME "torturetest"
33
34 static BOOL test_LogonUasLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
35 {
36         NTSTATUS status;
37         struct netr_LogonUasLogon r;
38
39         r.in.server_name = NULL;
40         r.in.account_name = lp_parm_string(-1, "torture", "username");
41         r.in.workstation = TEST_MACHINE_NAME;
42
43         printf("Testing LogonUasLogon\n");
44
45         status = dcerpc_netr_LogonUasLogon(p, mem_ctx, &r);
46         if (!NT_STATUS_IS_OK(status)) {
47                 printf("LogonUasLogon - %s\n", nt_errstr(status));
48                 return False;
49         }
50
51         return True;
52         
53 }
54
55 static BOOL test_LogonUasLogoff(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
56 {
57         NTSTATUS status;
58         struct netr_LogonUasLogoff r;
59
60         r.in.server_name = NULL;
61         r.in.account_name = lp_parm_string(-1, "torture", "username");
62         r.in.workstation = TEST_MACHINE_NAME;
63
64         printf("Testing LogonUasLogoff\n");
65
66         status = dcerpc_netr_LogonUasLogoff(p, mem_ctx, &r);
67         if (!NT_STATUS_IS_OK(status)) {
68                 printf("LogonUasLogoff - %s\n", nt_errstr(status));
69                 return False;
70         }
71
72         return True;
73         
74 }
75
76 BOOL test_SetupCredentials(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
77                            const char *machine_name,
78                            const char *plain_pass,
79                            struct creds_CredentialState **creds_out)
80 {
81         NTSTATUS status;
82         struct netr_ServerReqChallenge r;
83         struct netr_ServerAuthenticate a;
84         struct netr_Credential credentials1, credentials2, credentials3;
85         struct creds_CredentialState *creds;
86         struct samr_Password mach_password;
87
88         printf("Testing ServerReqChallenge\n");
89
90         creds = talloc(mem_ctx, struct creds_CredentialState);
91         if (!creds) {
92                 return False;
93         }
94
95         r.in.server_name = NULL;
96         r.in.computer_name = machine_name;
97         r.in.credentials = &credentials1;
98         r.out.credentials = &credentials2;
99
100         generate_random_buffer(credentials1.data, sizeof(credentials1.data));
101
102         status = dcerpc_netr_ServerReqChallenge(p, mem_ctx, &r);
103         if (!NT_STATUS_IS_OK(status)) {
104                 printf("ServerReqChallenge - %s\n", nt_errstr(status));
105                 return False;
106         }
107
108         E_md4hash(plain_pass, mach_password.hash);
109
110         a.in.server_name = NULL;
111         a.in.account_name = talloc_asprintf(mem_ctx, "%s$", machine_name);
112         a.in.secure_channel_type = SEC_CHAN_BDC;
113         a.in.computer_name = machine_name;
114         a.in.credentials = &credentials3;
115         a.out.credentials = &credentials3;
116
117         creds_client_init(creds, &credentials1, &credentials2, 
118                           machine_name, 
119                           lp_workgroup(),
120                           a.in.account_name,
121                           &mach_password, &credentials3, 
122                           0);
123
124         printf("Testing ServerAuthenticate\n");
125
126         status = dcerpc_netr_ServerAuthenticate(p, mem_ctx, &a);
127         if (!NT_STATUS_IS_OK(status)) {
128                 printf("ServerAuthenticate - %s\n", nt_errstr(status));
129                 return False;
130         }
131
132         if (!creds_client_check(creds, &credentials3)) {
133                 printf("Credential chaining failed\n");
134                 return False;
135         }
136
137         *creds_out = creds;
138         return True;
139 }
140
141 BOOL test_SetupCredentials2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
142                             uint32_t negotiate_flags,
143                             const char *machine_name,
144                             const char *plain_pass,
145                             int sec_chan_type,
146                             struct creds_CredentialState **creds_out)
147 {
148         NTSTATUS status;
149         struct netr_ServerReqChallenge r;
150         struct netr_ServerAuthenticate2 a;
151         struct netr_Credential credentials1, credentials2, credentials3;
152         struct creds_CredentialState *creds;
153         struct samr_Password mach_password;
154
155         printf("Testing ServerReqChallenge\n");
156
157         creds = talloc(mem_ctx, struct creds_CredentialState);
158         if (!creds) {
159                 return False;
160         }
161
162         r.in.server_name = NULL;
163         r.in.computer_name = machine_name;
164         r.in.credentials = &credentials1;
165         r.out.credentials = &credentials2;
166
167         generate_random_buffer(credentials1.data, sizeof(credentials1.data));
168
169         status = dcerpc_netr_ServerReqChallenge(p, mem_ctx, &r);
170         if (!NT_STATUS_IS_OK(status)) {
171                 printf("ServerReqChallenge - %s\n", nt_errstr(status));
172                 return False;
173         }
174
175         E_md4hash(plain_pass, mach_password.hash);
176
177         a.in.server_name = NULL;
178         a.in.account_name = talloc_asprintf(mem_ctx, "%s$", machine_name);
179         a.in.secure_channel_type = sec_chan_type;
180         a.in.computer_name = machine_name;
181         a.in.negotiate_flags = &negotiate_flags;
182         a.out.negotiate_flags = &negotiate_flags;
183         a.in.credentials = &credentials3;
184         a.out.credentials = &credentials3;
185
186         creds_client_init(creds, &credentials1, &credentials2, 
187                           machine_name, 
188                           lp_workgroup(),
189                           a.in.account_name,
190                           &mach_password, &credentials3, 
191                           negotiate_flags);
192
193         printf("Testing ServerAuthenticate2\n");
194
195         status = dcerpc_netr_ServerAuthenticate2(p, mem_ctx, &a);
196         if (!NT_STATUS_IS_OK(status)) {
197                 printf("ServerAuthenticate2 - %s\n", nt_errstr(status));
198                 return False;
199         }
200
201         if (!creds_client_check(creds, &credentials3)) {
202                 printf("Credential chaining failed\n");
203                 return False;
204         }
205
206         printf("negotiate_flags=0x%08x\n", negotiate_flags);
207
208         *creds_out = creds;
209         return True;
210 }
211
212
213 BOOL test_SetupCredentials3(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
214                             uint32_t negotiate_flags,
215                             const char *machine_name,
216                             const char *plain_pass,
217                             struct creds_CredentialState **creds_out)
218 {
219         NTSTATUS status;
220         struct netr_ServerReqChallenge r;
221         struct netr_ServerAuthenticate3 a;
222         struct netr_Credential credentials1, credentials2, credentials3;
223         struct creds_CredentialState *creds;
224         struct samr_Password mach_password;
225         uint32_t rid;
226
227         printf("Testing ServerReqChallenge\n");
228
229         creds = talloc(mem_ctx, struct creds_CredentialState);
230         if (!creds) {
231                 return False;
232         }
233
234         r.in.server_name = NULL;
235         r.in.computer_name = machine_name;
236         r.in.credentials = &credentials1;
237         r.out.credentials = &credentials2;
238
239         generate_random_buffer(credentials1.data, sizeof(credentials1.data));
240
241         status = dcerpc_netr_ServerReqChallenge(p, mem_ctx, &r);
242         if (!NT_STATUS_IS_OK(status)) {
243                 printf("ServerReqChallenge - %s\n", nt_errstr(status));
244                 return False;
245         }
246
247         E_md4hash(plain_pass, mach_password.hash);
248
249         a.in.server_name = NULL;
250         a.in.account_name = talloc_asprintf(mem_ctx, "%s$", machine_name);
251         a.in.secure_channel_type = SEC_CHAN_BDC;
252         a.in.computer_name = machine_name;
253         a.in.negotiate_flags = &negotiate_flags;
254         a.in.credentials = &credentials3;
255         a.out.credentials = &credentials3;
256         a.out.negotiate_flags = &negotiate_flags;
257         a.out.rid = &rid;
258
259         creds_client_init(creds, &credentials1, &credentials2, 
260                           machine_name, 
261                           lp_workgroup(),
262                           a.in.account_name,
263                           &mach_password, &credentials3,
264                           negotiate_flags);
265
266         printf("Testing ServerAuthenticate3\n");
267
268         status = dcerpc_netr_ServerAuthenticate3(p, mem_ctx, &a);
269         if (!NT_STATUS_IS_OK(status)) {
270                 printf("ServerAuthenticate3 - %s\n", nt_errstr(status));
271                 return False;
272         }
273
274         if (!creds_client_check(creds, &credentials3)) {
275                 printf("Credential chaining failed\n");
276                 return False;
277         }
278
279         printf("negotiate_flags=0x%08x\n", negotiate_flags);
280
281         *creds_out = creds;
282         return True;
283 }
284
285 /*
286   try a change password for our machine account
287 */
288 static BOOL test_SetPassword(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
289 {
290         NTSTATUS status;
291         struct netr_ServerPasswordSet r;
292         const char *password;
293         struct creds_CredentialState *creds;
294
295         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, 
296                                    machine_password, &creds)) {
297                 return False;
298         }
299
300         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
301         r.in.account_name = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
302         r.in.secure_channel_type = SEC_CHAN_BDC;
303         r.in.computer_name = TEST_MACHINE_NAME;
304
305         password = "";
306         E_md4hash(password, r.in.new_password.hash);
307
308         creds_des_encrypt(creds, &r.in.new_password);
309         /* by changing the machine password to ""
310          * we check if the server uses password restrictions
311          * for ServerPasswordSet2
312          * (win2k3 accepts "")
313          */
314         printf("Testing a second ServerPasswordSet on machine account\n");
315         printf("Changing machine account password to '%s'\n", password);
316
317         creds_client_authenticator(creds, &r.in.credential);
318
319         status = dcerpc_netr_ServerPasswordSet(p, mem_ctx, &r);
320         if (!NT_STATUS_IS_OK(status)) {
321                 printf("ServerPasswordSet (2) - %s\n", nt_errstr(status));
322                 return False;
323         }
324
325         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
326                 printf("Credential chaining failed\n");
327         }
328
329         machine_password = password;
330
331         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
332                 printf("ServerPasswordSet failed to actually change the password\n");
333                 return False;
334         }
335
336         password = generate_random_str(mem_ctx, 8);
337         E_md4hash(password, r.in.new_password.hash);
338
339         creds_des_encrypt(creds, &r.in.new_password);
340
341         printf("Testing ServerPasswordSet on machine account\n");
342         printf("Changing machine account password to '%s'\n", password);
343
344         creds_client_authenticator(creds, &r.in.credential);
345
346         status = dcerpc_netr_ServerPasswordSet(p, mem_ctx, &r);
347         if (!NT_STATUS_IS_OK(status)) {
348                 printf("ServerPasswordSet - %s\n", nt_errstr(status));
349                 return False;
350         }
351
352         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
353                 printf("Credential chaining failed\n");
354         }
355
356         /* by changing the machine password twice we test the
357            credentials chaining fully, and we verify that the server
358            allows the password to be set to the same value twice in a
359            row (match win2k3) */
360         printf("Testing a second ServerPasswordSet on machine account\n");
361         printf("Changing machine account password to '%s' (same as previous run)\n", password);
362
363         creds_client_authenticator(creds, &r.in.credential);
364
365         status = dcerpc_netr_ServerPasswordSet(p, mem_ctx, &r);
366         if (!NT_STATUS_IS_OK(status)) {
367                 printf("ServerPasswordSet (2) - %s\n", nt_errstr(status));
368                 return False;
369         }
370
371         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
372                 printf("Credential chaining failed\n");
373         }
374
375         machine_password = password;
376
377         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
378                 printf("ServerPasswordSet failed to actually change the password\n");
379                 return False;
380         }
381
382         return True;
383 }
384
385 /*
386   try a change password for our machine account
387 */
388 static BOOL test_SetPassword2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
389 {
390         NTSTATUS status;
391         struct netr_ServerPasswordSet2 r;
392         const char *password;
393         struct creds_CredentialState *creds;
394
395         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME,
396                                    machine_password, &creds)) {
397                 return False;
398         }
399
400         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
401         r.in.account_name = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
402         r.in.secure_channel_type = SEC_CHAN_BDC;
403         r.in.computer_name = TEST_MACHINE_NAME;
404
405         password = "";
406         encode_pw_buffer(r.in.new_password.data, password, STR_UNICODE);
407         creds_arcfour_crypt(creds, r.in.new_password.data, 516);
408
409         /* by changing the machine password to ""
410          * we check if the server uses password restrictions
411          * for ServerPasswordSet2
412          * (win2k3 accepts "")
413          */
414         printf("Testing a second ServerPasswordSet2 on machine account\n");
415         printf("Changing machine account password to '%s'\n", password);
416
417         creds_client_authenticator(creds, &r.in.credential);
418
419         status = dcerpc_netr_ServerPasswordSet2(p, mem_ctx, &r);
420         if (!NT_STATUS_IS_OK(status)) {
421                 printf("ServerPasswordSet (2) - %s\n", nt_errstr(status));
422                 return False;
423         }
424
425         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
426                 printf("Credential chaining failed\n");
427         }
428
429         machine_password = password;
430
431         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
432                 printf("ServerPasswordSet failed to actually change the password\n");
433                 return False;
434         }
435
436         /* now try a random password */
437         password = generate_random_str(mem_ctx, 8);
438         encode_pw_buffer(r.in.new_password.data, password, STR_UNICODE);
439         creds_arcfour_crypt(creds, r.in.new_password.data, 516);
440
441         printf("Testing ServerPasswordSet2 on machine account\n");
442         printf("Changing machine account password to '%s'\n", password);
443
444         creds_client_authenticator(creds, &r.in.credential);
445
446         status = dcerpc_netr_ServerPasswordSet2(p, mem_ctx, &r);
447         if (!NT_STATUS_IS_OK(status)) {
448                 printf("ServerPasswordSet2 - %s\n", nt_errstr(status));
449                 return False;
450         }
451
452         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
453                 printf("Credential chaining failed\n");
454         }
455
456         /* by changing the machine password twice we test the
457            credentials chaining fully, and we verify that the server
458            allows the password to be set to the same value twice in a
459            row (match win2k3) */
460         printf("Testing a second ServerPasswordSet2 on machine account\n");
461         printf("Changing machine account password to '%s' (same as previous run)\n", password);
462
463         creds_client_authenticator(creds, &r.in.credential);
464
465         status = dcerpc_netr_ServerPasswordSet2(p, mem_ctx, &r);
466         if (!NT_STATUS_IS_OK(status)) {
467                 printf("ServerPasswordSet (2) - %s\n", nt_errstr(status));
468                 return False;
469         }
470
471         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
472                 printf("Credential chaining failed\n");
473         }
474
475         machine_password = password;
476
477         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
478                 printf("ServerPasswordSet failed to actually change the password\n");
479                 return False;
480         }
481
482         return True;
483 }
484
485 /*
486   try a netlogon SamLogon
487 */
488 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
489 {
490         NTSTATUS status;
491         struct netr_LogonSamLogon r;
492         struct netr_Authenticator auth, auth2;
493         struct netr_NetworkInfo ninfo;
494         const char *username = lp_parm_string(-1, "torture", "username");
495         const char *password = lp_parm_string(-1, "torture", "password");
496         struct creds_CredentialState *creds;
497
498         int i;
499         BOOL ret = True;
500
501         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, 
502                                    machine_password, &creds)) {
503                 return False;
504         }
505
506         ninfo.identity_info.domain_name.string = lp_workgroup();
507         ninfo.identity_info.parameter_control = 0;
508         ninfo.identity_info.logon_id_low = 0;
509         ninfo.identity_info.logon_id_high = 0;
510         ninfo.identity_info.account_name.string = username;
511         ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
512         generate_random_buffer(ninfo.challenge, 
513                                sizeof(ninfo.challenge));
514         ninfo.nt.length = 24;
515         ninfo.nt.data = talloc_size(mem_ctx, 24);
516         SMBNTencrypt(password, ninfo.challenge, ninfo.nt.data);
517         ninfo.lm.length = 24;
518         ninfo.lm.data = talloc_size(mem_ctx, 24);
519         SMBencrypt(password, ninfo.challenge, ninfo.lm.data);
520
521         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
522         r.in.workstation = TEST_MACHINE_NAME;
523         r.in.credential = &auth;
524         r.in.return_authenticator = &auth2;
525         r.in.logon_level = 2;
526         r.in.logon.network = &ninfo;
527
528         for (i=2;i<=3;i++) {
529                 ZERO_STRUCT(auth2);
530                 creds_client_authenticator(creds, &auth);
531
532                 r.in.validation_level = i;
533
534                 printf("Testing SamLogon with validation level %d\n", i);
535
536                 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
537                 if (!NT_STATUS_IS_OK(status)) {
538                         printf("LogonSamLogon - %s\n", nt_errstr(status));
539                         ret = False;
540                 }
541
542                 if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
543                         printf("Credential chaining failed\n");
544                 }
545         }
546
547         r.in.credential = NULL;
548
549         for (i=2;i<=3;i++) {
550
551                 r.in.validation_level = i;
552
553                 printf("Testing SamLogon with validation level %d\n", i);
554
555                 status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
556                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
557                         printf("LogonSamLogon expected INVALID_PARAMETER, got: %s\n", nt_errstr(status));
558                         ret = False;
559                 }
560
561         }
562
563         return ret;
564 }
565
566 /* we remember the sequence numbers so we can easily do a DatabaseDelta */
567 static uint64_t sequence_nums[3];
568
569 /*
570   try a netlogon DatabaseSync
571 */
572 static BOOL test_DatabaseSync(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
573 {
574         NTSTATUS status;
575         struct netr_DatabaseSync r;
576         struct creds_CredentialState *creds;
577         const uint32_t database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS}; 
578         int i;
579         BOOL ret = True;
580
581         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
582                 return False;
583         }
584
585         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
586         r.in.computername = TEST_MACHINE_NAME;
587         r.in.preferredmaximumlength = (uint32_t)-1;
588         ZERO_STRUCT(r.in.return_authenticator);
589
590         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
591                 r.in.sync_context = 0;
592                 r.in.database_id = database_ids[i];
593
594                 printf("Testing DatabaseSync of id %d\n", r.in.database_id);
595
596                 do {
597                         creds_client_authenticator(creds, &r.in.credential);
598
599                         status = dcerpc_netr_DatabaseSync(p, mem_ctx, &r);
600                         if (!NT_STATUS_IS_OK(status) &&
601                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
602                                 printf("DatabaseSync - %s\n", nt_errstr(status));
603                                 ret = False;
604                                 break;
605                         }
606
607                         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
608                                 printf("Credential chaining failed\n");
609                         }
610
611                         r.in.sync_context = r.out.sync_context;
612
613                         if (r.out.delta_enum_array &&
614                             r.out.delta_enum_array->num_deltas > 0 &&
615                             r.out.delta_enum_array->delta_enum[0].delta_type == NETR_DELTA_DOMAIN &&
616                             r.out.delta_enum_array->delta_enum[0].delta_union.domain) {
617                                 sequence_nums[r.in.database_id] = 
618                                         r.out.delta_enum_array->delta_enum[0].delta_union.domain->sequence_num;
619                                 printf("\tsequence_nums[%d]=%llu\n",
620                                        r.in.database_id, 
621                                        sequence_nums[r.in.database_id]);
622                         }
623                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
624         }
625
626         return ret;
627 }
628
629
630 /*
631   try a netlogon DatabaseDeltas
632 */
633 static BOOL test_DatabaseDeltas(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
634 {
635         NTSTATUS status;
636         struct netr_DatabaseDeltas r;
637         struct creds_CredentialState *creds;
638         const uint32_t database_ids[] = {0, 1, 2}; 
639         int i;
640         BOOL ret = True;
641
642         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
643                 return False;
644         }
645
646         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
647         r.in.computername = TEST_MACHINE_NAME;
648         r.in.preferredmaximumlength = (uint32_t)-1;
649         ZERO_STRUCT(r.in.return_authenticator);
650
651         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
652                 r.in.database_id = database_ids[i];
653                 r.in.sequence_num = sequence_nums[r.in.database_id];
654
655                 if (r.in.sequence_num == 0) continue;
656
657                 r.in.sequence_num -= 1;
658
659
660                 printf("Testing DatabaseDeltas of id %d at %llu\n", 
661                        r.in.database_id, r.in.sequence_num);
662
663                 do {
664                         creds_client_authenticator(creds, &r.in.credential);
665
666                         status = dcerpc_netr_DatabaseDeltas(p, mem_ctx, &r);
667                         if (!NT_STATUS_IS_OK(status) &&
668                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
669                                 printf("DatabaseDeltas - %s\n", nt_errstr(status));
670                                 ret = False;
671                                 break;
672                         }
673
674                         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
675                                 printf("Credential chaining failed\n");
676                         }
677
678                         r.in.sequence_num++;
679                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
680         }
681
682         return ret;
683 }
684
685
686 /*
687   try a netlogon AccountDeltas
688 */
689 static BOOL test_AccountDeltas(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
690 {
691         NTSTATUS status;
692         struct netr_AccountDeltas r;
693         struct creds_CredentialState *creds;
694         BOOL ret = True;
695
696         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
697                 return False;
698         }
699
700         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
701         r.in.computername = TEST_MACHINE_NAME;
702         ZERO_STRUCT(r.in.return_authenticator);
703         creds_client_authenticator(creds, &r.in.credential);
704         ZERO_STRUCT(r.in.uas);
705         r.in.count=10;
706         r.in.level=0;
707         r.in.buffersize=100;
708
709         printf("Testing AccountDeltas\n");
710
711         /* w2k3 returns "NOT IMPLEMENTED" for this call */
712         status = dcerpc_netr_AccountDeltas(p, mem_ctx, &r);
713         if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
714                 printf("AccountDeltas - %s\n", nt_errstr(status));
715                 ret = False;
716         }
717
718         return ret;
719 }
720
721 /*
722   try a netlogon AccountSync
723 */
724 static BOOL test_AccountSync(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
725 {
726         NTSTATUS status;
727         struct netr_AccountSync r;
728         struct creds_CredentialState *creds;
729         BOOL ret = True;
730
731         if (!test_SetupCredentials(p, mem_ctx, TEST_MACHINE_NAME, machine_password, &creds)) {
732                 return False;
733         }
734
735         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
736         r.in.computername = TEST_MACHINE_NAME;
737         ZERO_STRUCT(r.in.return_authenticator);
738         creds_client_authenticator(creds, &r.in.credential);
739         ZERO_STRUCT(r.in.recordid);
740         r.in.reference=0;
741         r.in.level=0;
742         r.in.buffersize=100;
743
744         printf("Testing AccountSync\n");
745
746         /* w2k3 returns "NOT IMPLEMENTED" for this call */
747         status = dcerpc_netr_AccountSync(p, mem_ctx, &r);
748         if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
749                 printf("AccountSync - %s\n", nt_errstr(status));
750                 ret = False;
751         }
752
753         return ret;
754 }
755
756 /*
757   try a netlogon GetDcName
758 */
759 static BOOL test_GetDcName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
760 {
761         NTSTATUS status;
762         struct netr_GetDcName r;
763
764         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
765         r.in.domainname = lp_workgroup();
766
767         printf("Testing GetDcName\n");
768
769         status = dcerpc_netr_GetDcName(p, mem_ctx, &r);
770         if (!NT_STATUS_IS_OK(status)) {
771                 printf("GetDcName - %s\n", nt_errstr(status));
772                 return False;
773         }
774
775         printf("\tDC is at '%s'\n", r.out.dcname);
776
777         return True;
778 }
779
780 /*
781   try a netlogon LogonControl 
782 */
783 static BOOL test_LogonControl(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
784 {
785         NTSTATUS status;
786         struct netr_LogonControl r;
787         BOOL ret = True;
788         int i;
789
790         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
791         r.in.function_code = 1;
792
793         for (i=1;i<4;i++) {
794                 r.in.level = i;
795
796                 printf("Testing LogonControl level %d\n", i);
797
798                 status = dcerpc_netr_LogonControl(p, mem_ctx, &r);
799                 if (!NT_STATUS_IS_OK(status)) {
800                         printf("LogonControl - %s\n", nt_errstr(status));
801                         ret = False;
802                 }
803         }
804
805         return ret;
806 }
807
808
809 /*
810   try a netlogon GetAnyDCName
811 */
812 static BOOL test_GetAnyDCName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
813 {
814         NTSTATUS status;
815         struct netr_GetAnyDCName r;
816
817         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
818         r.in.domainname = lp_workgroup();
819
820         printf("Testing GetAnyDCName\n");
821
822         status = dcerpc_netr_GetAnyDCName(p, mem_ctx, &r);
823         if (!NT_STATUS_IS_OK(status)) {
824                 printf("GetAnyDCName - %s\n", nt_errstr(status));
825                 return False;
826         }
827
828         if (r.out.dcname) {
829                 printf("\tDC is at '%s'\n", r.out.dcname);
830         }
831
832         return True;
833 }
834
835
836 /*
837   try a netlogon LogonControl2
838 */
839 static BOOL test_LogonControl2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
840 {
841         NTSTATUS status;
842         struct netr_LogonControl2 r;
843         BOOL ret = True;
844         int i;
845
846         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
847
848         r.in.function_code = NETLOGON_CONTROL_REDISCOVER;
849         r.in.data.domain = lp_workgroup();
850
851         for (i=1;i<4;i++) {
852                 r.in.level = i;
853
854                 printf("Testing LogonControl2 level %d function %d\n", 
855                        i, r.in.function_code);
856
857                 status = dcerpc_netr_LogonControl2(p, mem_ctx, &r);
858                 if (!NT_STATUS_IS_OK(status)) {
859                         printf("LogonControl - %s\n", nt_errstr(status));
860                         ret = False;
861                 }
862         }
863
864         r.in.function_code = NETLOGON_CONTROL_TC_QUERY;
865         r.in.data.domain = lp_workgroup();
866
867         for (i=1;i<4;i++) {
868                 r.in.level = i;
869
870                 printf("Testing LogonControl2 level %d function %d\n", 
871                        i, r.in.function_code);
872
873                 status = dcerpc_netr_LogonControl2(p, mem_ctx, &r);
874                 if (!NT_STATUS_IS_OK(status)) {
875                         printf("LogonControl - %s\n", nt_errstr(status));
876                         ret = False;
877                 }
878         }
879
880         r.in.function_code = NETLOGON_CONTROL_TRANSPORT_NOTIFY;
881         r.in.data.domain = lp_workgroup();
882
883         for (i=1;i<4;i++) {
884                 r.in.level = i;
885
886                 printf("Testing LogonControl2 level %d function %d\n", 
887                        i, r.in.function_code);
888
889                 status = dcerpc_netr_LogonControl2(p, mem_ctx, &r);
890                 if (!NT_STATUS_IS_OK(status)) {
891                         printf("LogonControl - %s\n", nt_errstr(status));
892                         ret = False;
893                 }
894         }
895
896         r.in.function_code = NETLOGON_CONTROL_SET_DBFLAG;
897         r.in.data.debug_level = ~0;
898
899         for (i=1;i<4;i++) {
900                 r.in.level = i;
901
902                 printf("Testing LogonControl2 level %d function %d\n", 
903                        i, r.in.function_code);
904
905                 status = dcerpc_netr_LogonControl2(p, mem_ctx, &r);
906                 if (!NT_STATUS_IS_OK(status)) {
907                         printf("LogonControl - %s\n", nt_errstr(status));
908                         ret = False;
909                 }
910         }
911
912         return ret;
913 }
914
915 /*
916   try a netlogon DatabaseSync2
917 */
918 static BOOL test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
919 {
920         NTSTATUS status;
921         struct netr_DatabaseSync2 r;
922         struct creds_CredentialState *creds;
923         const uint32_t database_ids[] = {0, 1, 2}; 
924         int i;
925         BOOL ret = True;
926
927         if (!test_SetupCredentials2(p, mem_ctx, NETLOGON_NEG_AUTH2_FLAGS, 
928                                     TEST_MACHINE_NAME, machine_password, 
929                                     SEC_CHAN_BDC, &creds)) {
930                 return False;
931         }
932
933         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
934         r.in.computername = TEST_MACHINE_NAME;
935         r.in.preferredmaximumlength = (uint32_t)-1;
936         ZERO_STRUCT(r.in.return_authenticator);
937
938         for (i=0;i<ARRAY_SIZE(database_ids);i++) {
939                 r.in.sync_context = 0;
940                 r.in.database_id = database_ids[i];
941                 r.in.restart_state = 0;
942
943                 printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
944
945                 do {
946                         creds_client_authenticator(creds, &r.in.credential);
947
948                         status = dcerpc_netr_DatabaseSync2(p, mem_ctx, &r);
949                         if (!NT_STATUS_IS_OK(status) &&
950                             !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
951                                 printf("DatabaseSync2 - %s\n", nt_errstr(status));
952                                 ret = False;
953                                 break;
954                         }
955
956                         if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
957                                 printf("Credential chaining failed\n");
958                         }
959
960                         r.in.sync_context = r.out.sync_context;
961                 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
962         }
963
964         return ret;
965 }
966
967
968 /*
969   try a netlogon LogonControl2Ex
970 */
971 static BOOL test_LogonControl2Ex(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
972 {
973         NTSTATUS status;
974         struct netr_LogonControl2Ex r;
975         BOOL ret = True;
976         int i;
977
978         r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
979
980         r.in.function_code = NETLOGON_CONTROL_REDISCOVER;
981         r.in.data.domain = lp_workgroup();
982
983         for (i=1;i<4;i++) {
984                 r.in.level = i;
985
986                 printf("Testing LogonControl2Ex level %d function %d\n", 
987                        i, r.in.function_code);
988
989                 status = dcerpc_netr_LogonControl2Ex(p, mem_ctx, &r);
990                 if (!NT_STATUS_IS_OK(status)) {
991                         printf("LogonControl - %s\n", nt_errstr(status));
992                         ret = False;
993                 }
994         }
995
996         r.in.function_code = NETLOGON_CONTROL_TC_QUERY;
997         r.in.data.domain = lp_workgroup();
998
999         for (i=1;i<4;i++) {
1000                 r.in.level = i;
1001
1002                 printf("Testing LogonControl2Ex level %d function %d\n", 
1003                        i, r.in.function_code);
1004
1005                 status = dcerpc_netr_LogonControl2Ex(p, mem_ctx, &r);
1006                 if (!NT_STATUS_IS_OK(status)) {
1007                         printf("LogonControl - %s\n", nt_errstr(status));
1008                         ret = False;
1009                 }
1010         }
1011
1012         r.in.function_code = NETLOGON_CONTROL_TRANSPORT_NOTIFY;
1013         r.in.data.domain = lp_workgroup();
1014
1015         for (i=1;i<4;i++) {
1016                 r.in.level = i;
1017
1018                 printf("Testing LogonControl2Ex level %d function %d\n", 
1019                        i, r.in.function_code);
1020
1021                 status = dcerpc_netr_LogonControl2Ex(p, mem_ctx, &r);
1022                 if (!NT_STATUS_IS_OK(status)) {
1023                         printf("LogonControl - %s\n", nt_errstr(status));
1024                         ret = False;
1025                 }
1026         }
1027
1028         r.in.function_code = NETLOGON_CONTROL_SET_DBFLAG;
1029         r.in.data.debug_level = ~0;
1030
1031         for (i=1;i<4;i++) {
1032                 r.in.level = i;
1033
1034                 printf("Testing LogonControl2Ex level %d function %d\n", 
1035                        i, r.in.function_code);
1036
1037                 status = dcerpc_netr_LogonControl2Ex(p, mem_ctx, &r);
1038                 if (!NT_STATUS_IS_OK(status)) {
1039                         printf("LogonControl - %s\n", nt_errstr(status));
1040                         ret = False;
1041                 }
1042         }
1043
1044         return ret;
1045 }
1046
1047
1048 /*
1049   try a netlogon netr_DsrEnumerateDomainTrusts
1050 */
1051 static BOOL test_DsrEnumerateDomainTrusts(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
1052 {
1053         NTSTATUS status;
1054         struct netr_DsrEnumerateDomainTrusts r;
1055
1056         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1057         r.in.trust_flags = 0x3f;
1058
1059         printf("Testing netr_DsrEnumerateDomainTrusts\n");
1060
1061         status = dcerpc_netr_DsrEnumerateDomainTrusts(p, mem_ctx, &r);
1062         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
1063                 printf("netr_DsrEnumerateDomainTrusts - %s/%s\n", 
1064                        nt_errstr(status), win_errstr(r.out.result));
1065                 return False;
1066         }
1067
1068         return True;
1069 }
1070
1071 /*
1072   try a netlogon netr_DrsGetDCNameEx2
1073 */
1074 static BOOL test_netr_DrsGetDCNameEx2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
1075 {
1076         NTSTATUS status;
1077         struct netr_DrsGetDCNameEx2 r;
1078         BOOL ret = True;
1079
1080         r.in.server_unc         = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1081         r.in.client_account     = NULL;
1082         r.in.mask               = 0x00000000;
1083         r.in.domain_name        = talloc_asprintf(mem_ctx, "%s", lp_realm());
1084         r.in.domain_guid        = NULL;
1085         r.in.site_name          = NULL;
1086         r.in.flags              = 0x40000000;
1087
1088         printf("Testing netr_DrsGetDCNameEx2 without client account\n");
1089
1090         status = dcerpc_netr_DrsGetDCNameEx2(p, mem_ctx, &r);
1091         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
1092                 printf("netr_DrsGetDCNameEx2 - %s/%s\n", 
1093                        nt_errstr(status), win_errstr(r.out.result));
1094                 ret = False;
1095         }
1096
1097         printf("Testing netr_DrsGetDCNameEx2 with client acount\n");
1098         r.in.client_account     = TEST_MACHINE_NAME"$";
1099         r.in.mask               = 0x00002000;
1100         r.in.flags              = 0x80000000;
1101
1102         status = dcerpc_netr_DrsGetDCNameEx2(p, mem_ctx, &r);
1103         if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
1104                 printf("netr_DrsGetDCNameEx2 - %s/%s\n", 
1105                        nt_errstr(status), win_errstr(r.out.result));
1106                 ret = False;
1107         }
1108
1109         return ret;
1110 }
1111
1112 static BOOL test_GetDomainInfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
1113 {
1114         NTSTATUS status;
1115         struct netr_LogonGetDomainInfo r;
1116         struct netr_DomainQuery1 q1;
1117         struct netr_Authenticator a;
1118         struct creds_CredentialState *creds;
1119
1120         if (!test_SetupCredentials3(p, mem_ctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
1121                                     TEST_MACHINE_NAME, machine_password, &creds)) {
1122                 return False;
1123         }
1124
1125         ZERO_STRUCT(r);
1126
1127         creds_client_authenticator(creds, &a);
1128
1129         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1130         r.in.computer_name = TEST_MACHINE_NAME;
1131         r.in.level = 1;
1132         r.in.credential = &a;
1133         r.in.return_authenticator = &a;
1134         r.out.return_authenticator = &a;
1135
1136         r.in.query.query1 = &q1;
1137         ZERO_STRUCT(q1);
1138         
1139         /* this should really be the fully qualified name */
1140         q1.workstation_domain = TEST_MACHINE_NAME;
1141         q1.workstation_site = "Default-First-Site-Name";
1142         q1.blob2.length = 0;
1143         q1.blob2.size = 0;
1144         q1.blob2.data = NULL;
1145         q1.product.string = "product string";
1146
1147         printf("Testing netr_LogonGetDomainInfo\n");
1148
1149         status = dcerpc_netr_LogonGetDomainInfo(p, mem_ctx, &r);
1150         if (!NT_STATUS_IS_OK(status)) {
1151                 printf("netr_LogonGetDomainInfo - %s\n", nt_errstr(status));
1152                 return False;
1153         }
1154
1155         if (!creds_client_check(creds, &a.cred)) {
1156                 printf("Credential chaining failed\n");
1157                 return False;
1158         }
1159
1160         return True;
1161 }
1162
1163
1164 static void async_callback(struct rpc_request *req)
1165 {
1166         int *counter = req->async.private;
1167         if (NT_STATUS_IS_OK(req->status)) {
1168                 (*counter)++;
1169         }
1170 }
1171
1172 static BOOL test_GetDomainInfo_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
1173 {
1174         NTSTATUS status;
1175         struct netr_LogonGetDomainInfo r;
1176         struct netr_DomainQuery1 q1;
1177         struct netr_Authenticator a;
1178 #define ASYNC_COUNT 100
1179         struct creds_CredentialState *creds;
1180         struct creds_CredentialState *creds_async[ASYNC_COUNT];
1181         struct rpc_request *req[ASYNC_COUNT];
1182         int i;
1183         int *async_counter = talloc(mem_ctx, int);
1184
1185         if (!lp_parm_bool(-1, "torture", "dangerous", False)) {
1186                 printf("test_GetDomainInfo_async disabled - enable dangerous tests to use\n");
1187                 return True;
1188         }
1189
1190         if (!test_SetupCredentials3(p, mem_ctx, NETLOGON_NEG_AUTH2_ADS_FLAGS, 
1191                                     TEST_MACHINE_NAME, machine_password, &creds)) {
1192                 return False;
1193         }
1194
1195         ZERO_STRUCT(r);
1196         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
1197         r.in.computer_name = TEST_MACHINE_NAME;
1198         r.in.level = 1;
1199         r.in.credential = &a;
1200         r.in.return_authenticator = &a;
1201         r.out.return_authenticator = &a;
1202
1203         r.in.query.query1 = &q1;
1204         ZERO_STRUCT(q1);
1205         
1206         /* this should really be the fully qualified name */
1207         q1.workstation_domain = TEST_MACHINE_NAME;
1208         q1.workstation_site = "Default-First-Site-Name";
1209         q1.blob2.length = 0;
1210         q1.blob2.size = 0;
1211         q1.blob2.data = NULL;
1212         q1.product.string = "product string";
1213
1214         printf("Testing netr_LogonGetDomainInfo - async count %d\n", ASYNC_COUNT);
1215
1216         *async_counter = 0;
1217
1218         for (i=0;i<ASYNC_COUNT;i++) {
1219                 creds_client_authenticator(creds, &a);
1220
1221                 creds_async[i] = talloc_memdup(creds, creds, sizeof(*creds));
1222                 req[i] = dcerpc_netr_LogonGetDomainInfo_send(p, mem_ctx, &r);
1223
1224                 req[i]->async.callback = async_callback;
1225                 req[i]->async.private = async_counter;
1226
1227                 /* even with this flush per request a w2k3 server seems to 
1228                    clag with multiple outstanding requests. bleergh. */
1229                 if (event_loop_once(dcerpc_event_context(p)) != 0) {
1230                         return False;
1231                 }
1232         }
1233
1234         for (i=0;i<ASYNC_COUNT;i++) {
1235                 status = dcerpc_ndr_request_recv(req[i]);
1236                 if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(r.out.result)) {
1237                         printf("netr_LogonGetDomainInfo_async(%d) - %s/%s\n", 
1238                                i, nt_errstr(status), nt_errstr(r.out.result));
1239                         break;
1240                 }
1241
1242                 if (!creds_client_check(creds_async[i], &a.cred)) {
1243                         printf("Credential chaining failed at async %d\n", i);
1244                         break;
1245                 }
1246         }
1247
1248         printf("Testing netr_LogonGetDomainInfo - async count %d OK\n", *async_counter);
1249
1250         return (*async_counter) == ASYNC_COUNT;
1251 }
1252
1253 static BOOL test_ManyGetDCName(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
1254 {
1255         NTSTATUS status;
1256         struct dcerpc_binding *b;
1257         struct dcerpc_pipe *p2;
1258         struct lsa_ObjectAttribute attr;
1259         struct lsa_QosInfo qos;
1260         struct lsa_OpenPolicy2 o;
1261         struct policy_handle lsa_handle;
1262         struct lsa_DomainList domains;
1263
1264         struct lsa_EnumTrustDom t;
1265         uint32_t resume_handle = 0;
1266         struct netr_GetAnyDCName d;
1267
1268         int i;
1269         BOOL ret = True;
1270
1271         if (p->conn->transport.transport != NCACN_NP) {
1272                 return True;
1273         }
1274
1275         printf("Torturing GetDCName\n");
1276
1277         status = dcerpc_parse_binding(mem_ctx, p->conn->binding_string, &b);
1278         if (!NT_STATUS_IS_OK(status)) {
1279                 printf("Failed to parse dcerpc binding '%s'\n", p->conn->binding_string);
1280                 return False;
1281         }
1282
1283         status = dcerpc_secondary_connection(p, &p2, b);
1284         if (!NT_STATUS_IS_OK(status)) {
1285                 printf("Failed to create secondary connection\n");
1286                 return False;
1287         }
1288
1289         status = dcerpc_bind_auth_none(p2, DCERPC_LSARPC_UUID, 
1290                                        DCERPC_LSARPC_VERSION);
1291         if (!NT_STATUS_IS_OK(status)) {
1292                 printf("Failed to create bind on secondary connection\n");
1293                 return False;
1294         }
1295
1296         qos.len = 0;
1297         qos.impersonation_level = 2;
1298         qos.context_mode = 1;
1299         qos.effective_only = 0;
1300
1301         attr.len = 0;
1302         attr.root_dir = NULL;
1303         attr.object_name = NULL;
1304         attr.attributes = 0;
1305         attr.sec_desc = NULL;
1306         attr.sec_qos = &qos;
1307
1308         o.in.system_name = "\\";
1309         o.in.attr = &attr;
1310         o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
1311         o.out.handle = &lsa_handle;
1312
1313         status = dcerpc_lsa_OpenPolicy2(p2, mem_ctx, &o);
1314         if (!NT_STATUS_IS_OK(status)) {
1315                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
1316                 return False;
1317         }
1318
1319         t.in.handle = &lsa_handle;
1320         t.in.resume_handle = &resume_handle;
1321         t.in.max_size = 1000;
1322         t.out.domains = &domains;
1323         t.out.resume_handle = &resume_handle;
1324
1325         status = dcerpc_lsa_EnumTrustDom(p2, mem_ctx, &t);
1326
1327         if ((!NT_STATUS_IS_OK(status) &&
1328              (!NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)))) {
1329                 printf("Could not list domains\n");
1330                 return False;
1331         }
1332
1333         dcerpc_pipe_close(p2);
1334
1335         d.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s",
1336                                             dcerpc_server_name(p));
1337
1338         for (i=0; i<domains.count * 4; i++) {
1339                 struct lsa_DomainInformation *info =
1340                         &domains.domains[rand()%domains.count];
1341
1342                 d.in.domainname = info->name.string;
1343
1344                 status = dcerpc_netr_GetAnyDCName(p, mem_ctx, &d);
1345                 if (!NT_STATUS_IS_OK(status)) {
1346                         printf("GetAnyDCName - %s\n", nt_errstr(status));
1347                         continue;
1348                 }
1349
1350                 printf("\tDC for domain %s is %s\n", info->name.string,
1351                        d.out.dcname ? d.out.dcname : "unknown");
1352         }
1353
1354         return ret;
1355 }
1356
1357
1358 BOOL torture_rpc_netlogon(void)
1359 {
1360         NTSTATUS status;
1361         struct dcerpc_pipe *p;
1362         TALLOC_CTX *mem_ctx;
1363         BOOL ret = True;
1364         struct test_join *join_ctx;
1365
1366         mem_ctx = talloc_init("torture_rpc_netlogon");
1367
1368         join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST, 
1369                                        &machine_password);
1370         if (!join_ctx) {
1371                 printf("Failed to join as BDC\n");
1372                 return False;
1373         }
1374
1375         status = torture_rpc_connection(&p, 
1376                                         DCERPC_NETLOGON_NAME,
1377                                         DCERPC_NETLOGON_UUID,
1378                                         DCERPC_NETLOGON_VERSION);
1379         if (!NT_STATUS_IS_OK(status)) {
1380                 return False;
1381         }
1382
1383         ret &= test_LogonUasLogon(p, mem_ctx);
1384         ret &= test_LogonUasLogoff(p, mem_ctx);
1385         ret &= test_SamLogon(p, mem_ctx);
1386         ret &= test_SetPassword(p, mem_ctx);
1387         ret &= test_SetPassword2(p, mem_ctx);
1388         ret &= test_GetDomainInfo(p, mem_ctx);
1389         ret &= test_DatabaseSync(p, mem_ctx);
1390         ret &= test_DatabaseDeltas(p, mem_ctx);
1391         ret &= test_AccountDeltas(p, mem_ctx);
1392         ret &= test_AccountSync(p, mem_ctx);
1393         ret &= test_GetDcName(p, mem_ctx);
1394         ret &= test_ManyGetDCName(p, mem_ctx);
1395         ret &= test_LogonControl(p, mem_ctx);
1396         ret &= test_GetAnyDCName(p, mem_ctx);
1397         ret &= test_LogonControl2(p, mem_ctx);
1398         ret &= test_DatabaseSync2(p, mem_ctx);
1399         ret &= test_LogonControl2Ex(p, mem_ctx);
1400         ret &= test_DsrEnumerateDomainTrusts(p, mem_ctx);
1401         ret &= test_GetDomainInfo_async(p, mem_ctx);
1402         ret &= test_netr_DrsGetDCNameEx2(p, mem_ctx);
1403
1404         talloc_free(mem_ctx);
1405
1406         torture_rpc_close(p);
1407
1408         torture_leave_domain(join_ctx);
1409
1410         return ret;
1411 }