r9166: This checks more of auth subsystem in the PAC test.
[ira/wip.git] / source4 / torture / rpc / samlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for netlogon SamLogon 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 "librpc/gen_ndr/ndr_netlogon.h"
27 #include "auth/auth.h"
28 #include "lib/crypto/crypto.h"
29 #include "lib/cmdline/popt_common.h"
30
31 #define TEST_MACHINE_NAME "samlogontest"
32 #define TEST_USER_NAME "samlogontestuser"
33
34 enum ntlm_break {
35         BREAK_BOTH,
36         BREAK_NONE,
37         BREAK_LM,
38         BREAK_NT,
39         NO_LM,
40         NO_NT
41 };
42
43 struct samlogon_state {
44         TALLOC_CTX *mem_ctx;
45         const char *account_name;
46         const char *account_domain;
47         const char *password;
48         struct dcerpc_pipe *p;
49         int function_level;
50         struct netr_LogonSamLogon r;
51         struct netr_LogonSamLogonEx r_ex;
52         struct netr_LogonSamLogonWithFlags r_flags;
53         struct netr_Authenticator auth, auth2;
54         struct creds_CredentialState *creds;
55
56         DATA_BLOB chall;
57 };
58
59 /* 
60    Authenticate a user with a challenge/response, checking session key
61    and valid authentication types
62 */
63 static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state, 
64                                enum ntlm_break break_which,
65                                DATA_BLOB *chall, 
66                                DATA_BLOB *lm_response, 
67                                DATA_BLOB *nt_response, 
68                                uint8_t lm_key[8], 
69                                uint8_t user_session_key[16], 
70                                char **error_string)
71 {
72         NTSTATUS status;
73         struct netr_LogonSamLogon *r = &samlogon_state->r;
74         struct netr_LogonSamLogonEx *r_ex = &samlogon_state->r_ex;
75         struct netr_LogonSamLogonWithFlags *r_flags = &samlogon_state->r_flags;
76         struct netr_NetworkInfo ninfo;
77         struct netr_SamBaseInfo *base = NULL;
78         uint16_t validation_level = 0;
79         
80         samlogon_state->r.in.logon.network = &ninfo;
81         samlogon_state->r_ex.in.logon.network = &ninfo;
82         samlogon_state->r_flags.in.logon.network = &ninfo;
83         
84         ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
85         ninfo.identity_info.parameter_control = 0;
86         ninfo.identity_info.logon_id_low = 0;
87         ninfo.identity_info.logon_id_high = 0;
88         ninfo.identity_info.account_name.string = samlogon_state->account_name;
89         ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
90                 
91         memcpy(ninfo.challenge, chall->data, 8);
92                 
93         switch (break_which) {
94         case BREAK_NONE:
95                 break;
96         case BREAK_LM:
97                 if (lm_response && lm_response->data) {
98                         lm_response->data[0]++;
99                 }
100                 break;
101         case BREAK_NT:
102                 if (nt_response && nt_response->data) {
103                         nt_response->data[0]++;
104                 }
105                 break;
106         case BREAK_BOTH:
107                 if (lm_response && lm_response->data) {
108                         lm_response->data[0]++;
109                 }
110                 if (nt_response && nt_response->data) {
111                         nt_response->data[0]++;
112                 }
113                 break;
114         case NO_LM:
115                 data_blob_free(lm_response);
116                 break;
117         case NO_NT:
118                 data_blob_free(nt_response);
119                 break;
120         }
121                 
122         if (nt_response) {
123                 ninfo.nt.data = nt_response->data;
124                 ninfo.nt.length = nt_response->length;
125         } else {
126                 ninfo.nt.data = NULL;
127                 ninfo.nt.length = 0;
128         }
129                 
130         if (lm_response) {
131                 ninfo.lm.data = lm_response->data;
132                 ninfo.lm.length = lm_response->length;
133         } else {
134                 ninfo.lm.data = NULL;
135                 ninfo.lm.length = 0;
136         }
137         
138         switch (samlogon_state->function_level) {
139         case DCERPC_NETR_LOGONSAMLOGON: 
140                 ZERO_STRUCT(samlogon_state->auth2);
141                 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
142
143                 r->out.return_authenticator = NULL;
144                 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
145                 if (!r->out.return_authenticator || 
146                     !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
147                         printf("Credential chaining failed\n");
148                 }
149                 if (!NT_STATUS_IS_OK(status)) {
150                         if (error_string) {
151                                 *error_string = strdup(nt_errstr(status));
152                         }
153                 }
154
155                 validation_level = r->in.validation_level;
156                 switch (validation_level) {
157                 case 2:
158                         base = &r->out.validation.sam2->base;
159                         break;
160                 case 3:
161                         base = &r->out.validation.sam3->base;
162                         break;
163                 case 6:
164                         base = &r->out.validation.sam6->base;
165                         break;
166                 }
167                 break;
168         case DCERPC_NETR_LOGONSAMLOGONEX: 
169                 status = dcerpc_netr_LogonSamLogonEx(samlogon_state->p, samlogon_state->mem_ctx, r_ex);
170                 if (!NT_STATUS_IS_OK(status)) {
171                         if (error_string) {
172                                 *error_string = strdup(nt_errstr(status));
173                         }
174                 }
175
176                 validation_level = r_ex->in.validation_level;
177                 switch (validation_level) {
178                 case 2:
179                         base = &r_ex->out.validation.sam2->base;
180                         break;
181                 case 3:
182                         base = &r_ex->out.validation.sam3->base;
183                         break;
184                 case 6:
185                         base = &r_ex->out.validation.sam6->base;
186                         break;
187                 }
188                 break;
189         case DCERPC_NETR_LOGONSAMLOGONWITHFLAGS: 
190                 ZERO_STRUCT(samlogon_state->auth2);
191                 creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
192
193                 r_flags->out.return_authenticator = NULL;
194                 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
195                 if (!r_flags->out.return_authenticator || 
196                     !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
197                         printf("Credential chaining failed\n");
198                 }
199                 if (!NT_STATUS_IS_OK(status)) {
200                         if (error_string) {
201                                 *error_string = strdup(nt_errstr(status));
202                         }
203                 }
204
205                 validation_level = r_flags->in.validation_level;
206                 switch (validation_level) {
207                 case 2:
208                         base = &r_flags->out.validation.sam2->base;
209                         break;
210                 case 3:
211                         base = &r_flags->out.validation.sam3->base;
212                         break;
213                 case 6:
214                         base = &r_flags->out.validation.sam6->base;
215                         break;
216                 }
217                 break;
218         }
219                 
220
221         if (!NT_STATUS_IS_OK(status)) {
222                 /* we cannot check the session key, if the logon failed... */
223                 return status;
224         }
225
226         if (!base) {
227                 printf("No user info returned from 'successful' SamLogon*() call!\n");
228                 return NT_STATUS_INVALID_PARAMETER;
229         }
230
231         /* find and decyrpt the session keys, return in parameters above */
232         if (validation_level == 6) {
233                 /* they aren't encrypted! */
234                 if (user_session_key) {
235                         memcpy(user_session_key, base->key.key, 16);
236                 }
237                 if (lm_key) {
238                         memcpy(lm_key, base->LMSessKey.key, 8);
239                 }
240         } else if (samlogon_state->creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
241                 static const char zeros[16];
242                         
243                 if (memcmp(base->key.key, zeros,  
244                            sizeof(base->key.key)) != 0) {
245                         creds_arcfour_crypt(samlogon_state->creds, 
246                                             base->key.key, 
247                                             sizeof(base->key.key));
248                 }
249                         
250                 if (user_session_key) {
251                         memcpy(user_session_key, base->key.key, 16);
252                 }
253                         
254                 if (memcmp(base->LMSessKey.key, zeros,  
255                            sizeof(base->LMSessKey.key)) != 0) {
256                         creds_arcfour_crypt(samlogon_state->creds, 
257                                             base->LMSessKey.key, 
258                                             sizeof(base->LMSessKey.key));
259                 }
260                         
261                 if (lm_key) {
262                         memcpy(lm_key, base->LMSessKey.key, 8);
263                 }
264         } else {
265                 static const char zeros[16];
266                         
267                 if (user_session_key) {
268                         memcpy(user_session_key, base->key.key, 16);
269                 }
270
271                 if (memcmp(base->LMSessKey.key, zeros,  
272                            sizeof(base->LMSessKey.key)) != 0) {
273                         creds_des_decrypt_LMKey(samlogon_state->creds, 
274                                                 &base->LMSessKey);
275                 }
276                         
277                 if (lm_key) {
278                         memcpy(lm_key, base->LMSessKey.key, 8);
279                 }
280         }
281         
282         return status;
283
284
285
286 /* 
287  * Test the normal 'LM and NTLM' combination
288  */
289
290 static BOOL test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string) 
291 {
292         BOOL pass = True;
293         BOOL lm_good;
294         NTSTATUS nt_status;
295         DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
296         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
297         DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
298
299         uint8_t lm_key[8];
300         uint8_t user_session_key[16];
301         uint8_t lm_hash[16];
302         uint8_t nt_hash[16];
303         
304         ZERO_STRUCT(lm_key);
305         ZERO_STRUCT(user_session_key);
306
307         lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
308         if (!lm_good) {
309                 ZERO_STRUCT(lm_hash);
310         } else {
311                 E_deshash(samlogon_state->password, lm_hash); 
312         }
313                 
314         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
315
316         E_md4hash(samlogon_state->password, nt_hash);
317         SMBsesskeygen_ntv1(nt_hash, session_key.data);
318
319         nt_status = check_samlogon(samlogon_state,
320                                    break_which,
321                                    &samlogon_state->chall,
322                                    &lm_response,
323                                    &nt_response,
324                                    lm_key, 
325                                    user_session_key,
326                                    error_string);
327         
328         data_blob_free(&lm_response);
329
330         if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
331                 /* for 'long' passwords, the LM password is invalid */
332                 if (break_which == NO_NT && !lm_good) {
333                         return True;
334                 }
335                 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
336         }
337
338         if (!NT_STATUS_IS_OK(nt_status)) {
339                 return False;
340         }
341
342         if (break_which == NO_NT && !lm_good) {
343                 printf("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
344                 return False;
345         }
346
347         if (memcmp(lm_hash, lm_key, 
348                    sizeof(lm_key)) != 0) {
349                 printf("LM Key does not match expectations!\n");
350                 printf("lm_key:\n");
351                 dump_data(1, lm_key, 8);
352                 printf("expected:\n");
353                 dump_data(1, lm_hash, 8);
354                 pass = False;
355         }
356
357         switch (break_which) {
358         case NO_NT:
359         {
360                 uint8_t lm_key_expected[16];
361                 memcpy(lm_key_expected, lm_hash, 8);
362                 memset(lm_key_expected+8, '\0', 8);
363                 if (memcmp(lm_key_expected, user_session_key, 
364                            16) != 0) {
365                         printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
366                         printf("user_session_key:\n");
367                         dump_data(1, user_session_key, sizeof(user_session_key));
368                         printf("expected:\n");
369                         dump_data(1, lm_key_expected, sizeof(lm_key_expected));
370                         pass = False;
371                 }
372                 break;
373         }
374         default:
375                 if (memcmp(session_key.data, user_session_key, 
376                            sizeof(user_session_key)) != 0) {
377                         printf("NT Session Key does not match expectations!\n");
378                         printf("user_session_key:\n");
379                         dump_data(1, user_session_key, 16);
380                         printf("expected:\n");
381                         dump_data(1, session_key.data, session_key.length);
382                         pass = False;
383                 }
384         }
385         return pass;
386 }
387
388 /* 
389  * Test LM authentication, no NT response supplied
390  */
391
392 static BOOL test_lm(struct samlogon_state *samlogon_state, char **error_string) 
393 {
394
395         return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
396 }
397
398 /* 
399  * Test the NTLM response only, no LM.
400  */
401
402 static BOOL test_ntlm(struct samlogon_state *samlogon_state, char **error_string) 
403 {
404         return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
405 }
406
407 /* 
408  * Test the NTLM response only, but in the LM field.
409  */
410
411 static BOOL test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string) 
412 {
413         BOOL pass = True;
414         NTSTATUS nt_status;
415         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
416
417         uint8_t lm_key[8];
418         uint8_t lm_hash[16];
419         uint8_t user_session_key[16];
420         
421         ZERO_STRUCT(user_session_key);
422
423         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
424
425         E_deshash(samlogon_state->password, lm_hash); 
426
427         nt_status = check_samlogon(samlogon_state,
428                                    BREAK_NONE,
429                                    &samlogon_state->chall,
430                                    &nt_response,
431                                    NULL,
432                                    lm_key, 
433                                    user_session_key,
434                                    error_string);
435         
436         if (!NT_STATUS_IS_OK(nt_status)) {
437                 return False;
438         }
439
440         if (memcmp(lm_hash, lm_key, 
441                    sizeof(lm_key)) != 0) {
442                 printf("LM Key does not match expectations!\n");
443                 printf("lm_key:\n");
444                 dump_data(1, lm_key, 8);
445                 printf("expected:\n");
446                 dump_data(1, lm_hash, 8);
447                 pass = False;
448         }
449         if (memcmp(lm_hash, user_session_key, 8) != 0) {
450                 uint8_t lm_key_expected[16];
451                 memcpy(lm_key_expected, lm_hash, 8);
452                 memset(lm_key_expected+8, '\0', 8);
453                 if (memcmp(lm_key_expected, user_session_key, 
454                            16) != 0) {
455                         printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
456                         printf("user_session_key:\n");
457                         dump_data(1, user_session_key, sizeof(user_session_key));
458                         printf("expected:\n");
459                         dump_data(1, lm_key_expected, sizeof(lm_key_expected));
460                         pass = False;
461                 }
462         }
463         return pass;
464 }
465
466 /* 
467  * Test the NTLM response only, but in the both the NT and LM fields.
468  */
469
470 static BOOL test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string) 
471 {
472         BOOL pass = True;
473         BOOL lm_good;
474         NTSTATUS nt_status;
475         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
476         DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
477
478         uint8_t lm_key[8];
479         uint8_t lm_hash[16];
480         uint8_t user_session_key[16];
481         uint8_t nt_hash[16];
482         
483         ZERO_STRUCT(lm_key);
484         ZERO_STRUCT(user_session_key);
485
486         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, 
487                      nt_response.data);
488         E_md4hash(samlogon_state->password, nt_hash);
489         SMBsesskeygen_ntv1(nt_hash, 
490                            session_key.data);
491
492         lm_good = E_deshash(samlogon_state->password, lm_hash); 
493         if (!lm_good) {
494                 ZERO_STRUCT(lm_hash);
495         }
496
497         nt_status = check_samlogon(samlogon_state,
498                                    BREAK_NONE,
499                                    &samlogon_state->chall,
500                                    NULL, 
501                                    &nt_response,
502                                    lm_key, 
503                                    user_session_key,
504                                    error_string);
505         
506         if (!NT_STATUS_IS_OK(nt_status)) {
507                 return False;
508         }
509
510         if (memcmp(lm_hash, lm_key, 
511                    sizeof(lm_key)) != 0) {
512                 printf("LM Key does not match expectations!\n");
513                 printf("lm_key:\n");
514                 dump_data(1, lm_key, 8);
515                 printf("expected:\n");
516                 dump_data(1, lm_hash, 8);
517                 pass = False;
518         }
519         if (memcmp(session_key.data, user_session_key, 
520                    sizeof(user_session_key)) != 0) {
521                 printf("NT Session Key does not match expectations!\n");
522                 printf("user_session_key:\n");
523                 dump_data(1, user_session_key, 16);
524                 printf("expected:\n");
525                 dump_data(1, session_key.data, session_key.length);
526                 pass = False;
527         }
528
529
530         return pass;
531 }
532
533 /* 
534  * Test the NTLMv2 and LMv2 responses
535  */
536
537 enum ntlmv2_domain {
538         UPPER_DOMAIN,
539         NO_DOMAIN
540 };
541
542 static BOOL test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, 
543                                     enum ntlm_break break_which, 
544                                     enum ntlmv2_domain ntlmv2_domain, 
545                                     char **error_string) 
546 {
547         BOOL pass = True;
548         NTSTATUS nt_status;
549         DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
550         DATA_BLOB lmv2_response = data_blob(NULL, 0);
551         DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
552         DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
553         DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
554
555         uint8_t lm_session_key[8];
556         uint8_t user_session_key[16];
557
558         ZERO_STRUCT(lm_session_key);
559         ZERO_STRUCT(user_session_key);
560         
561         switch (ntlmv2_domain) {
562         case UPPER_DOMAIN:
563                 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx, 
564                                       samlogon_state->account_name, samlogon_state->account_domain, 
565                                       samlogon_state->password, &samlogon_state->chall,
566                                       &names_blob,
567                                       &lmv2_response, &ntlmv2_response, 
568                                       &lmv2_session_key, &ntlmv2_session_key)) {
569                         data_blob_free(&names_blob);
570                         return False;
571                 }
572                 break;
573         case NO_DOMAIN:
574                 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx, 
575                                       samlogon_state->account_name, "",
576                                       samlogon_state->password, &samlogon_state->chall,
577                                       &names_blob,
578                                       &lmv2_response, &ntlmv2_response, 
579                                       &lmv2_session_key, &ntlmv2_session_key)) {
580                         data_blob_free(&names_blob);
581                         return False;
582                 }
583                 break;
584         }
585         data_blob_free(&names_blob);
586
587         nt_status = check_samlogon(samlogon_state,
588                                    break_which,
589                                    &samlogon_state->chall,
590                                    &lmv2_response,
591                                    &ntlmv2_response,
592                                    lm_session_key, 
593                                    user_session_key,
594                                    error_string);
595         
596         data_blob_free(&lmv2_response);
597         data_blob_free(&ntlmv2_response);
598
599
600         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
601                 return break_which == BREAK_BOTH;
602         }
603
604         if (!NT_STATUS_IS_OK(nt_status)) {
605                 return False;
606         }
607
608         switch (break_which) {
609         case NO_NT:
610                 if (memcmp(lmv2_session_key.data, user_session_key,
611                            sizeof(user_session_key)) != 0) {
612                         printf("USER (LMv2) Session Key does not match expectations!\n");
613                         printf("user_session_key:\n");
614                         dump_data(1, user_session_key, 16);
615                         printf("expected:\n");
616                         dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
617                         pass = False;
618                 }
619                 if (memcmp(lmv2_session_key.data, lm_session_key, 
620                            sizeof(lm_session_key)) != 0) {
621                         printf("LM (LMv2) Session Key does not match expectations!\n");
622                         printf("lm_session_key:\n");
623                         dump_data(1, lm_session_key, 8);
624                         printf("expected:\n");
625                         dump_data(1, lmv2_session_key.data, 8);
626                         pass = False;
627                 }
628                 break;
629         default:
630                 if (memcmp(ntlmv2_session_key.data, user_session_key, 
631                            sizeof(user_session_key)) != 0) {
632                         if (memcmp(lmv2_session_key.data, user_session_key,
633                                    sizeof(user_session_key)) == 0) {
634                                 printf("USER (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
635                                 printf("user_session_key:\n");
636                                 dump_data(1, user_session_key, 16);
637                                 printf("expected:\n");
638                                 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
639                                 pass = False;
640                                 
641                         } else {
642                                 printf("USER (NTLMv2) Session Key does not match expectations!\n");
643                                 printf("user_session_key:\n");
644                                 dump_data(1, user_session_key, 16);
645                                 printf("expected:\n");
646                                 dump_data(1, ntlmv2_session_key.data, ntlmv2_session_key.length);
647                                 pass = False;
648                         }
649                 }
650                 if (memcmp(ntlmv2_session_key.data, lm_session_key, 
651                            sizeof(lm_session_key)) != 0) {
652                         if (memcmp(lmv2_session_key.data, lm_session_key,
653                                    sizeof(lm_session_key)) == 0) {
654                                 printf("LM (NTLMv2) Session Key expected, got LMv2 sessesion key instead:\n");
655                                 printf("user_session_key:\n");
656                                 dump_data(1, lm_session_key, 8);
657                                 printf("expected:\n");
658                                 dump_data(1, ntlmv2_session_key.data, 8);
659                                 pass = False;
660                         } else {
661                                 printf("LM (NTLMv2) Session Key does not match expectations!\n");
662                                 printf("lm_session_key:\n");
663                                 dump_data(1, lm_session_key, 8);
664                                 printf("expected:\n");
665                                 dump_data(1, ntlmv2_session_key.data, 8);
666                                 pass = False;
667                         }
668                 }
669         }
670
671         return pass;
672 }
673
674 /* 
675  * Test the NTLM and LMv2 responses
676  */
677
678 static BOOL test_lmv2_ntlm_broken(struct samlogon_state *samlogon_state, 
679                                   enum ntlm_break break_which, 
680                                   enum ntlmv2_domain ntlmv2_domain, 
681                                   char **error_string) 
682 {
683         BOOL pass = True;
684         NTSTATUS nt_status;
685         DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
686         DATA_BLOB lmv2_response = data_blob(NULL, 0);
687         DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
688         DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
689         DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
690
691         DATA_BLOB ntlm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
692         DATA_BLOB ntlm_session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
693
694         uint8_t lm_hash[16];
695         uint8_t lm_session_key[8];
696         uint8_t user_session_key[16];
697         uint8_t nt_hash[16];
698
699         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, 
700                      ntlm_response.data);
701         E_md4hash(samlogon_state->password, nt_hash);
702         SMBsesskeygen_ntv1(nt_hash, 
703                            ntlm_session_key.data);
704         E_deshash(samlogon_state->password, lm_hash); 
705
706         ZERO_STRUCT(lm_session_key);
707         ZERO_STRUCT(user_session_key);
708
709         switch (ntlmv2_domain) {
710         case UPPER_DOMAIN:
711                 /* TODO - test with various domain cases, and without domain */
712                 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx, 
713                                       samlogon_state->account_name, samlogon_state->account_domain, 
714                                       samlogon_state->password, &samlogon_state->chall,
715                                       &names_blob,
716                                       &lmv2_response, &ntlmv2_response, 
717                                       &lmv2_session_key, &ntlmv2_session_key)) {
718                         data_blob_free(&names_blob);
719                         return False;
720                 }
721                 break;
722         case NO_DOMAIN:
723                 /* TODO - test with various domain cases, and without domain */
724                 if (!SMBNTLMv2encrypt(samlogon_state->mem_ctx, 
725                                       samlogon_state->account_name, "",
726                                       samlogon_state->password, &samlogon_state->chall,
727                                       &names_blob,
728                                       &lmv2_response, &ntlmv2_response, 
729                                       &lmv2_session_key, &ntlmv2_session_key)) {
730                         data_blob_free(&names_blob);
731                         return False;
732                 }
733                 break;
734         }
735
736         data_blob_free(&names_blob);
737
738         nt_status = check_samlogon(samlogon_state,
739                                    break_which,
740                                    &samlogon_state->chall,
741                                    &lmv2_response,
742                                    &ntlm_response,
743                                    lm_session_key, 
744                                    user_session_key,
745                                    error_string);
746         
747         data_blob_free(&lmv2_response);
748         data_blob_free(&ntlmv2_response);
749
750
751         if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
752                 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
753         }
754
755         if (!NT_STATUS_IS_OK(nt_status)) {
756                 return False;
757         }
758
759         switch (break_which) {
760         case NO_NT:
761                 if (memcmp(lmv2_session_key.data, user_session_key, 
762                            sizeof(user_session_key)) != 0) {
763                         printf("USER (LMv2) Session Key does not match expectations!\n");
764                         printf("user_session_key:\n");
765                         dump_data(1, user_session_key, 16);
766                         printf("expected:\n");
767                         dump_data(1, lmv2_session_key.data, ntlmv2_session_key.length);
768                         pass = False;
769                 }
770                 if (memcmp(lmv2_session_key.data, lm_session_key, 
771                            sizeof(lm_session_key)) != 0) {
772                         printf("LM (LMv2) Session Key does not match expectations!\n");
773                         printf("lm_session_key:\n");
774                         dump_data(1, lm_session_key, 8);
775                         printf("expected:\n");
776                         dump_data(1, lmv2_session_key.data, 8);
777                         pass = False;
778                 }
779                 break;
780         case BREAK_LM:
781                 if (memcmp(ntlm_session_key.data, user_session_key, 
782                            sizeof(user_session_key)) != 0) {
783                         printf("USER (NTLMv2) Session Key does not match expectations!\n");
784                         printf("user_session_key:\n");
785                         dump_data(1, user_session_key, 16);
786                         printf("expected:\n");
787                         dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
788                         pass = False;
789                 }
790                 if (memcmp(lm_hash, lm_session_key, 
791                            sizeof(lm_session_key)) != 0) {
792                         printf("LM Session Key does not match expectations!\n");
793                         printf("lm_session_key:\n");
794                         dump_data(1, lm_session_key, 8);
795                         printf("expected:\n");
796                         dump_data(1, lm_hash, 8);
797                         pass = False;
798                 }
799                 break;
800         default:
801                 if (memcmp(ntlm_session_key.data, user_session_key, 
802                            sizeof(user_session_key)) != 0) {
803                         printf("USER (NTLMv2) Session Key does not match expectations!\n");
804                         printf("user_session_key:\n");
805                         dump_data(1, user_session_key, 16);
806                         printf("expected:\n");
807                         dump_data(1, ntlm_session_key.data, ntlm_session_key.length);
808                         pass = False;
809                 }
810                 if (memcmp(ntlm_session_key.data, lm_session_key, 
811                            sizeof(lm_session_key)) != 0) {
812                         printf("LM (NTLMv2) Session Key does not match expectations!\n");
813                         printf("lm_session_key:\n");
814                         dump_data(1, lm_session_key, 8);
815                         printf("expected:\n");
816                         dump_data(1, ntlm_session_key.data, 8);
817                         pass = False;
818                 }
819         }
820
821         return pass;
822 }
823
824 /* 
825  * Test the NTLMv2 and LMv2 responses
826  */
827
828 static BOOL test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string) 
829 {
830         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, UPPER_DOMAIN, error_string);
831 }
832
833 #if 0
834 static BOOL test_lmv2_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
835 {
836         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, NO_DOMAIN, error_string);
837 }
838 #endif
839
840 /* 
841  * Test the LMv2 response only
842  */
843
844 static BOOL test_lmv2(struct samlogon_state *samlogon_state, char **error_string) 
845 {
846         return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, UPPER_DOMAIN, error_string);
847 }
848
849 static BOOL test_lmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
850 {
851         return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, NO_DOMAIN, error_string);
852 }
853
854 /* 
855  * Test the NTLMv2 response only
856  */
857
858 static BOOL test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string) 
859 {
860         return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, UPPER_DOMAIN, error_string);
861 }
862
863 static BOOL test_ntlmv2_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
864 {
865         return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, NO_DOMAIN, error_string);
866 }
867
868 static BOOL test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string) 
869 {
870         return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
871 }
872
873 static BOOL test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string) 
874 {
875         return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
876 }
877
878 static BOOL test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string) 
879 {
880         return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
881 }
882
883 static BOOL test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string) 
884 {
885         return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
886 }
887 static BOOL test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string) 
888 {
889         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
890 }
891
892 static BOOL test_ntlmv2_lmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
893 {
894         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
895 }
896
897 static BOOL test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string) 
898 {
899         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
900 }
901
902 #if 0
903 static BOOL test_ntlmv2_ntlmv2_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
904 {
905         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
906 }
907 #endif
908
909 static BOOL test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string) 
910 {
911         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
912 }
913
914 static BOOL test_ntlmv2_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
915 {
916         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
917 }
918
919 static BOOL test_lmv2_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string) 
920 {
921         return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, UPPER_DOMAIN, error_string);
922 }
923
924 static BOOL test_lmv2_ntlm_both_broken_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
925 {
926         return test_lmv2_ntlm_broken(samlogon_state, BREAK_BOTH, NO_DOMAIN, error_string);
927 }
928
929 static BOOL test_lmv2_ntlm_break_ntlm(struct samlogon_state *samlogon_state, char **error_string) 
930 {
931         return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, UPPER_DOMAIN, error_string);
932 }
933
934 static BOOL test_lmv2_ntlm_break_ntlm_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
935 {
936         return test_lmv2_ntlm_broken(samlogon_state, BREAK_NT, NO_DOMAIN, error_string);
937 }
938
939 static BOOL test_lmv2_ntlm_break_lm(struct samlogon_state *samlogon_state, char **error_string) 
940 {
941         return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, UPPER_DOMAIN, error_string);
942 }
943
944 static BOOL test_lmv2_ntlm_break_lm_no_dom(struct samlogon_state *samlogon_state, char **error_string) 
945 {
946         return test_lmv2_ntlm_broken(samlogon_state, BREAK_LM, NO_DOMAIN, error_string);
947 }
948
949 /* 
950  * Test the NTLM2 response (extra challenge in LM feild)
951  *
952  * This test is the same as the 'break LM' test, but checks that the
953  * server implements NTLM2 session security in the right place
954  * (NETLOGON is the wrong place).
955  */
956
957 static BOOL test_ntlm2(struct samlogon_state *samlogon_state, char **error_string) 
958 {
959         BOOL pass = True;
960         NTSTATUS nt_status;
961         DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
962         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
963
964         uint8_t lm_key[8];
965         uint8_t nt_hash[16];
966         uint8_t lm_hash[16];
967         uint8_t nt_key[16];
968         uint8_t user_session_key[16];
969         uint8_t expected_user_session_key[16];
970         uint8_t session_nonce_hash[16];
971         uint8_t client_chall[8];
972         
973         struct MD5Context md5_session_nonce_ctx;
974         HMACMD5Context hmac_ctx;
975                         
976         ZERO_STRUCT(user_session_key);
977         ZERO_STRUCT(lm_key);
978         generate_random_buffer(client_chall, 8);
979         
980         MD5Init(&md5_session_nonce_ctx);
981         MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
982         MD5Update(&md5_session_nonce_ctx, client_chall, 8);
983         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
984         
985         E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
986         E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
987         SMBsesskeygen_ntv1((const uint8_t *)nt_hash, 
988                            nt_key);
989
990         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
991
992         memcpy(lm_response.data, session_nonce_hash, 8);
993         memset(lm_response.data + 8, 0, 16);
994
995         hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
996         hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
997         hmac_md5_update(client_chall, 8, &hmac_ctx);
998         hmac_md5_final(expected_user_session_key, &hmac_ctx);
999
1000         nt_status = check_samlogon(samlogon_state,
1001                                    BREAK_NONE,
1002                                    &samlogon_state->chall,
1003                                    &lm_response,
1004                                    &nt_response,
1005                                    lm_key, 
1006                                    user_session_key,
1007                                    error_string);
1008         
1009         if (!NT_STATUS_IS_OK(nt_status)) {
1010                 return False;
1011         }
1012
1013         if (memcmp(lm_hash, lm_key, 
1014                    sizeof(lm_key)) != 0) {
1015                 printf("LM Key does not match expectations!\n");
1016                 printf("lm_key:\n");
1017                 dump_data(1, lm_key, 8);
1018                 printf("expected:\n");
1019                 dump_data(1, lm_hash, 8);
1020                 pass = False;
1021         }
1022         if (memcmp(nt_key, user_session_key, 16) != 0) {
1023                 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
1024                 printf("user_session_key:\n");
1025                 dump_data(1, user_session_key, sizeof(user_session_key));
1026                 printf("expected:\n");
1027                 dump_data(1, nt_key, sizeof(nt_key));
1028                 pass = False;
1029         }
1030         return pass;
1031 }
1032
1033 static BOOL test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
1034 {
1035         NTSTATUS nt_status;
1036         DATA_BLOB nt_response = data_blob(NULL, 0);
1037         DATA_BLOB lm_response = data_blob(NULL, 0);
1038         char *password;
1039         char *dospw;
1040         void *unicodepw;
1041
1042         uint8_t user_session_key[16];
1043         uint8_t lm_key[16];
1044         static const uint8_t zeros[8];
1045         DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
1046
1047         ZERO_STRUCT(user_session_key);
1048         
1049         if ((push_ucs2_talloc(samlogon_state->mem_ctx, &unicodepw, 
1050                               samlogon_state->password)) == -1) {
1051                 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1052                 exit(1);
1053         }
1054
1055         nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
1056
1057         password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
1058
1059         if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX, 
1060                                    CH_DOS, password,
1061                                    strlen(password)+1, 
1062                                    (void**)&dospw)) == -1) {
1063                 DEBUG(0, ("convert_string_talloc failed!\n"));
1064                 exit(1);
1065         }
1066
1067         lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
1068
1069         nt_status = check_samlogon(samlogon_state,
1070                                    break_which,
1071                                    &chall,
1072                                    &lm_response,
1073                                    &nt_response,
1074                                    lm_key, 
1075                                    user_session_key,
1076                                    error_string);
1077         
1078         if (!NT_STATUS_IS_OK(nt_status)) {
1079                 return break_which == BREAK_NT;
1080         }
1081
1082         return True;
1083 }
1084
1085 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state, 
1086                                        char **error_string) {
1087         return test_plaintext(samlogon_state, BREAK_NONE, error_string);
1088 }
1089
1090 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state, 
1091                                      char **error_string) {
1092         return test_plaintext(samlogon_state, BREAK_LM, error_string);
1093 }
1094
1095 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state, 
1096                                      char **error_string) {
1097         return test_plaintext(samlogon_state, BREAK_NT, error_string);
1098 }
1099
1100 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state, 
1101                                    char **error_string) {
1102         return test_plaintext(samlogon_state, NO_LM, error_string);
1103 }
1104
1105 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state, 
1106                                    char **error_string) {
1107         return test_plaintext(samlogon_state, NO_NT, error_string);
1108 }
1109
1110 /* 
1111    Tests:
1112    
1113    - LM only
1114    - NT and LM             
1115    - NT
1116    - NT in LM field
1117    - NT in both fields
1118    - NTLMv2
1119    - NTLMv2 and LMv2
1120    - LMv2
1121    - plaintext tests (in challenge-response fields)
1122   
1123    check we get the correct session key in each case
1124    check what values we get for the LM session key
1125    
1126 */
1127
1128 static const struct ntlm_tests {
1129         BOOL (*fn)(struct samlogon_state *, char **);
1130         const char *name;
1131         BOOL expect_fail;
1132 } test_table[] = {
1133         {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
1134 #if 0
1135         {test_lmv2_ntlmv2_no_dom, "NTLMv2 and LMv2 (no domain)", False},
1136 #endif
1137         {test_lm, "LM", False},
1138         {test_lm_ntlm, "LM and NTLM", False},
1139         {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
1140         {test_ntlm, "NTLM", False},
1141         {test_ntlm_in_lm, "NTLM in LM", False},
1142         {test_ntlm_in_both, "NTLM in both", False},
1143         {test_ntlmv2, "NTLMv2", False},
1144         {test_ntlmv2_no_dom, "NTLMv2 (no domain)", False},
1145         {test_lmv2, "LMv2", False},
1146         {test_lmv2_no_dom, "LMv2 (no domain)", False},
1147         {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
1148         {test_ntlmv2_lmv2_broken_no_dom, "NTLMv2 and LMv2, LMv2 broken (no domain)", False},
1149         {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
1150 #if 0
1151         {test_ntlmv2_ntlmv2_broken_no_dom, "NTLMv2 and LMv2, NTLMv2 broken (no domain)", False},
1152 #endif
1153         {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
1154         {test_ntlmv2_both_broken_no_dom, "NTLMv2 and LMv2, both broken (no domain)", False},
1155         {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
1156         {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
1157         {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
1158         {test_lmv2_ntlm_both_broken, "LMv2 and NTLM, both broken", False},
1159         {test_lmv2_ntlm_both_broken_no_dom, "LMv2 and NTLM, both broken (no domain)", False},
1160         {test_lmv2_ntlm_break_ntlm, "LMv2 and NTLM, NTLM broken", False},
1161         {test_lmv2_ntlm_break_ntlm_no_dom, "LMv2 and NTLM, NTLM broken (no domain)", False},
1162         {test_lmv2_ntlm_break_lm, "LMv2 and NTLM, LMv2 broken", False},
1163         {test_lmv2_ntlm_break_lm_no_dom, "LMv2 and NTLM, LMv2 broken (no domain)", False},
1164         {test_plaintext_none_broken, "Plaintext", True},
1165         {test_plaintext_lm_broken, "Plaintext LM broken", True},
1166         {test_plaintext_nt_broken, "Plaintext NT broken", True},
1167         {test_plaintext_nt_only, "Plaintext NT only", True},
1168         {test_plaintext_lm_only, "Plaintext LM only", True},
1169         {NULL, NULL}
1170 };
1171
1172 /*
1173   try a netlogon SamLogon
1174 */
1175 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
1176                           struct creds_CredentialState *creds, 
1177                           const char *account_domain, const char *account_name, 
1178                           const char *plain_pass,
1179                           int n_subtests)
1180 {
1181         TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_SamLogon function-level context");
1182         int i, v, l, f;
1183         BOOL ret = True;
1184         int validation_levels[] = {2,3,6};
1185         int logon_levels[] = { 2, 6 };
1186         int function_levels[] = { 
1187                 DCERPC_NETR_LOGONSAMLOGON,
1188                 DCERPC_NETR_LOGONSAMLOGONEX,
1189                 DCERPC_NETR_LOGONSAMLOGONWITHFLAGS };
1190         struct samlogon_state samlogon_state;
1191         
1192         printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
1193         
1194         samlogon_state.account_name = account_name;
1195         samlogon_state.account_domain = account_domain;
1196         samlogon_state.password = plain_pass;
1197         samlogon_state.p = p;
1198         samlogon_state.creds = creds;
1199
1200         samlogon_state.chall = data_blob_talloc(fn_ctx, NULL, 8);
1201
1202         generate_random_buffer(samlogon_state.chall.data, 8);
1203         samlogon_state.r_flags.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1204         samlogon_state.r_flags.in.workstation = TEST_MACHINE_NAME;
1205         samlogon_state.r_flags.in.credential = &samlogon_state.auth;
1206         samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
1207         samlogon_state.r_flags.in.flags = 0;
1208
1209         samlogon_state.r_ex.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1210         samlogon_state.r_ex.in.workstation = TEST_MACHINE_NAME;
1211         samlogon_state.r_ex.in.flags = 0;
1212
1213         samlogon_state.r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1214         samlogon_state.r.in.workstation = TEST_MACHINE_NAME;
1215         samlogon_state.r.in.credential = &samlogon_state.auth;
1216         samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
1217
1218         for (f=0;f<ARRAY_SIZE(function_levels);f++) {
1219                 for (i=0; test_table[i].fn; i++) {
1220                         if (n_subtests && (i > n_subtests)) {
1221                                 continue;
1222                         }
1223                         for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
1224                                 for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
1225                                         char *error_string = NULL;
1226                                         TALLOC_CTX *tmp_ctx = talloc_named(fn_ctx, 0, "test_SamLogon inner loop");
1227                                         samlogon_state.mem_ctx = tmp_ctx;
1228                                         samlogon_state.function_level = function_levels[f];
1229                                         samlogon_state.r.in.validation_level = validation_levels[v];
1230                                         samlogon_state.r.in.logon_level = logon_levels[l];
1231                                         samlogon_state.r_ex.in.validation_level = validation_levels[v];
1232                                         samlogon_state.r_ex.in.logon_level = logon_levels[l];
1233                                         samlogon_state.r_flags.in.validation_level = validation_levels[v];
1234                                         samlogon_state.r_flags.in.logon_level = logon_levels[l];
1235                                         if (!test_table[i].fn(&samlogon_state, &error_string)) {
1236                                                 printf("Testing [%s]\\[%s] '%s' at validation level %d, logon level %d, function %d: \n", 
1237                                                        samlogon_state.account_domain,
1238                                                        samlogon_state.account_name,
1239                                                        test_table[i].name, validation_levels[v], 
1240                                                        logon_levels[l], function_levels[f]);
1241                                                 
1242                                                 if (test_table[i].expect_fail) {
1243                                                         printf(" failed (expected, test incomplete): %s\n", error_string);
1244                                                 } else {
1245                                                         printf(" failed: %s\n", error_string);
1246                                                         ret = False;
1247                                                 }
1248                                                 SAFE_FREE(error_string);
1249                                         }
1250                                         talloc_free(tmp_ctx);
1251                                 }
1252                         }
1253                 }
1254         }
1255         talloc_free(fn_ctx);
1256         return ret;
1257 }
1258
1259 /*
1260   test an ADS style interactive domain logon
1261 */
1262 BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
1263                            struct creds_CredentialState *creds, 
1264                            const char *workstation_name,
1265                            const char *account_domain, const char *account_name,
1266                            const char *plain_pass)
1267 {
1268         NTSTATUS status;
1269         TALLOC_CTX *fn_ctx = talloc_named(mem_ctx, 0, "test_InteractiveLogon function-level context");
1270         struct netr_LogonSamLogonWithFlags r;
1271         struct netr_Authenticator a, ra;
1272         struct netr_PasswordInfo pinfo;
1273
1274         ZERO_STRUCT(a);
1275         ZERO_STRUCT(r);
1276         ZERO_STRUCT(ra);
1277
1278         creds_client_authenticator(creds, &a);
1279
1280         r.in.server_name = talloc_asprintf(fn_ctx, "\\\\%s", dcerpc_server_name(p));
1281         r.in.workstation = TEST_MACHINE_NAME;
1282         r.in.credential = &a;
1283         r.in.return_authenticator = &ra;
1284         r.in.logon_level = 5;
1285         r.in.logon.password = &pinfo;
1286         r.in.validation_level = 6;
1287         r.in.flags = 0;
1288
1289         pinfo.identity_info.domain_name.string = account_domain;
1290         pinfo.identity_info.parameter_control = 0;
1291         pinfo.identity_info.logon_id_low = 0;
1292         pinfo.identity_info.logon_id_high = 0;
1293         pinfo.identity_info.account_name.string = account_name;
1294         pinfo.identity_info.workstation.string = workstation_name;
1295
1296         if (!E_deshash(plain_pass, pinfo.lmpassword.hash)) {
1297                 ZERO_STRUCT(pinfo.lmpassword.hash);
1298         }
1299         E_md4hash(plain_pass, pinfo.ntpassword.hash);
1300
1301         if (creds->negotiate_flags & NETLOGON_NEG_ARCFOUR) {
1302                 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
1303                 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
1304         } else {
1305                 creds_des_encrypt(creds, &pinfo.lmpassword);
1306                 creds_des_encrypt(creds, &pinfo.ntpassword);
1307         }
1308
1309         printf("Testing netr_LogonSamLogonWithFlags (Interactive Logon)\n");
1310
1311         status = dcerpc_netr_LogonSamLogonWithFlags(p, fn_ctx, &r);
1312         if (!r.out.return_authenticator 
1313             || !creds_client_check(creds, &r.out.return_authenticator->cred)) {
1314                 printf("Credential chaining failed\n");
1315                 talloc_free(fn_ctx);
1316                 return False;
1317         }
1318
1319         talloc_free(fn_ctx);
1320
1321         if (!NT_STATUS_IS_OK(status)) {
1322                 printf("[%s]\\[%s] netr_LogonSamLogonWithFlags - %s\n", 
1323                        account_name, account_domain, nt_errstr(status));
1324                 return False;
1325         }
1326
1327         return True;
1328 }
1329
1330
1331
1332 BOOL torture_rpc_samlogon(void)
1333 {
1334         NTSTATUS status;
1335         struct dcerpc_pipe *p;
1336         struct dcerpc_binding *b;
1337         struct cli_credentials *credentials;
1338         TALLOC_CTX *mem_ctx = talloc_init("torture_rpc_netlogon");
1339         BOOL ret = True;
1340         struct test_join *join_ctx;
1341 #if 0
1342         struct test_join *user_ctx;
1343         const char *user_password;
1344 #endif
1345         char *test_machine_account;
1346         const char *machine_password;
1347         const char *binding = lp_parm_string(-1, "torture", "binding");
1348         int i;
1349         int ci;
1350
1351         unsigned int credential_flags[] = {
1352                 NETLOGON_NEG_AUTH2_FLAGS,
1353                 NETLOGON_NEG_ARCFOUR,
1354                 NETLOGON_NEG_ARCFOUR | NETLOGON_NEG_128BIT,
1355                 NETLOGON_NEG_AUTH2_ADS_FLAGS, 
1356                 0 /* yes, this is a valid flag, causes the use of DES */ 
1357         };
1358
1359         struct creds_CredentialState *creds;
1360
1361         struct {
1362                 const char *domain;
1363                 const char *username;
1364                 const char *password;
1365                 BOOL network_login;
1366         } usercreds[] = {
1367                 {
1368                         cli_credentials_get_domain(cmdline_credentials),
1369                         cli_credentials_get_username(cmdline_credentials),
1370                         cli_credentials_get_password(cmdline_credentials),
1371                         True
1372                 },
1373                 {
1374                         cli_credentials_get_realm(cmdline_credentials),
1375                         cli_credentials_get_username(cmdline_credentials),
1376                         cli_credentials_get_password(cmdline_credentials),
1377                         True
1378                 },
1379                 {
1380                         NULL,
1381                         talloc_asprintf(mem_ctx, 
1382                                         "%s@%s", 
1383                                         cli_credentials_get_username(cmdline_credentials),
1384                                         cli_credentials_get_domain(cmdline_credentials)
1385                                 ),
1386                         cli_credentials_get_password(cmdline_credentials),
1387                         False
1388                 },
1389                 {
1390                         NULL,
1391                         talloc_asprintf(mem_ctx, 
1392                                         "%s@%s", 
1393                                         cli_credentials_get_username(cmdline_credentials),
1394                                         cli_credentials_get_realm(cmdline_credentials)
1395                                 ),
1396                         cli_credentials_get_password(cmdline_credentials),
1397                         True
1398                 },
1399 #if 0
1400                 {       
1401                         lp_parm_string(-1, "torture", "userdomain"),
1402                         TEST_USER_NAME,
1403                         NULL,
1404                         True
1405                 },
1406                 {
1407                         NULL,
1408                         talloc_asprintf(mem_ctx, 
1409                                         "%s@%s", 
1410                                         TEST_USER_NAME,
1411                                         lp_realm()),
1412                         NULL,
1413                         True
1414                 },
1415                 {
1416                         NULL,
1417                         talloc_asprintf(mem_ctx, 
1418                                         "%s@%s", 
1419                                         TEST_USER_NAME,
1420                                         lp_parm_string(-1, "torture", "userdomain")),
1421                         NULL,
1422                         False
1423                 }
1424 #endif
1425         };
1426                 
1427         credentials = cli_credentials_init(mem_ctx);
1428
1429         test_machine_account = talloc_asprintf(mem_ctx, "%s$", TEST_MACHINE_NAME);
1430         /* We only need to join as a workstation here, and in future,
1431          * if we wish to test against trusted domains, we must be a
1432          * workstation here */
1433         join_ctx = torture_create_testuser(test_machine_account, lp_workgroup(), ACB_WSTRUST, 
1434                                            &machine_password);
1435         if (!join_ctx) {
1436                 printf("Failed to join as Workstation\n");
1437                 return False;
1438         }
1439 #if 0
1440         user_ctx = torture_create_testuser(TEST_USER_NAME,
1441                                            lp_parm_string(-1, "torture", "userdomain"),
1442                                            ACB_NORMAL, 
1443                                            &user_password);
1444         if (!user_ctx) {
1445                 printf("Failed to join as Workstation\n");
1446                 return False;
1447         }
1448
1449         usercreds[3].password = user_password;
1450         usercreds[4].password = user_password;
1451         usercreds[5].password = user_password;
1452 #endif
1453
1454         status = dcerpc_parse_binding(mem_ctx, binding, &b);
1455         if (!NT_STATUS_IS_OK(status)) {
1456                 printf("Bad binding string %s\n", binding);
1457                 ret = False;
1458                 goto failed;
1459         }
1460
1461         /* We have to use schannel, otherwise the SamLogonEx fails
1462          * with INTERNAL_ERROR */
1463
1464         b->flags &= ~DCERPC_AUTH_OPTIONS;
1465         b->flags |= DCERPC_SCHANNEL | DCERPC_SIGN | DCERPC_SCHANNEL_128;
1466
1467         cli_credentials_set_workstation(credentials, TEST_MACHINE_NAME, CRED_SPECIFIED);
1468         cli_credentials_set_domain(credentials, lp_workgroup(), CRED_SPECIFIED);
1469         cli_credentials_set_username(credentials, test_machine_account, CRED_SPECIFIED);
1470         cli_credentials_set_password(credentials, machine_password, CRED_SPECIFIED);
1471         cli_credentials_set_secure_channel_type(credentials,
1472                                                 SEC_CHAN_WKSTA);
1473
1474         status = dcerpc_pipe_connect_b(mem_ctx, &p, b, 
1475                                        DCERPC_NETLOGON_UUID,
1476                                        DCERPC_NETLOGON_VERSION,
1477                                        credentials, NULL);
1478
1479         if (!NT_STATUS_IS_OK(status)) {
1480                 printf("RPC pipe connect as domain member failed: %s\n", nt_errstr(status));
1481                 ret = False;
1482                 goto failed;
1483         }
1484
1485         status = dcerpc_schannel_creds(p->conn->security_state.generic_state, mem_ctx, &creds);
1486         if (!NT_STATUS_IS_OK(status)) {
1487                 ret = False;
1488                 goto failed;
1489         }
1490
1491         /* Try all the tests for different username forms */
1492         for (ci = 0; ci < ARRAY_SIZE(usercreds); ci++) {
1493                 
1494                 if (!test_InteractiveLogon(p, mem_ctx, creds,
1495                                            TEST_MACHINE_NAME,
1496                                            usercreds[ci].domain,
1497                                            usercreds[ci].username,
1498                                            usercreds[ci].password)) {
1499                         ret = False;
1500                 }
1501                 
1502                 if (usercreds[ci].network_login) {
1503                         if (!test_SamLogon(p, mem_ctx, creds, 
1504                                            usercreds[ci].domain,
1505                                            usercreds[ci].username,
1506                                            usercreds[ci].password,
1507                                            0)) {
1508                                 ret = False;
1509                         }
1510                 }
1511         }
1512
1513         /* Using the first username form, try the different
1514          * credentials flag setups, on only one of the tests (checks
1515          * session key encryption) */
1516
1517         for (i=0; i < ARRAY_SIZE(credential_flags); i++) {
1518                 if (!test_InteractiveLogon(p, mem_ctx, creds,
1519                                            TEST_MACHINE_NAME,
1520                                            usercreds[0].domain,
1521                                            usercreds[0].username,
1522                                            usercreds[0].password)) {
1523                         ret = False;
1524                 }
1525                 
1526                 if (usercreds[ci].network_login) {
1527                         if (!test_SamLogon(p, mem_ctx, creds, 
1528                                            usercreds[0].domain,
1529                                            usercreds[0].username,
1530                                            usercreds[0].password,
1531                                            1)) {
1532                                 ret = False;
1533                         }
1534                 }
1535         }
1536
1537 failed:
1538         talloc_free(mem_ctx);
1539
1540         torture_leave_domain(join_ctx);
1541 #if 0
1542         torture_leave_domain(user_ctx);
1543 #endif
1544         return ret;
1545 }