r3677: Seperate the SamLogon tests from the main RPC-NETLOGON test into a
[kai/samba.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
30 #define TEST_MACHINE_NAME "samlogontest"
31
32 enum ntlm_break {
33         BREAK_BOTH,
34         BREAK_NONE,
35         BREAK_LM,
36         BREAK_NT,
37         NO_LM,
38         NO_NT
39 };
40
41 struct samlogon_state {
42         TALLOC_CTX *mem_ctx;
43         const char *account_name;
44         const char *account_domain;
45         const char *password;
46         struct dcerpc_pipe *p;
47         int function_level;
48         struct netr_LogonSamLogon r;
49         struct netr_LogonSamLogonWithFlags r_flags;
50         struct netr_Authenticator auth, auth2;
51         struct creds_CredentialState *creds;
52
53         DATA_BLOB chall;
54 };
55
56 /* 
57    Authenticate a user with a challenge/response, checking session key
58    and valid authentication types
59 */
60 static NTSTATUS check_samlogon(struct samlogon_state *samlogon_state, 
61                                enum ntlm_break break_which,
62                                DATA_BLOB *chall, 
63                                DATA_BLOB *lm_response, 
64                                DATA_BLOB *nt_response, 
65                                uint8_t lm_key[8], 
66                                uint8_t user_session_key[16], 
67                                char **error_string)
68 {
69         NTSTATUS status;
70         struct netr_LogonSamLogon *r = &samlogon_state->r;
71         struct netr_LogonSamLogonWithFlags *r_flags = &samlogon_state->r_flags;
72         struct netr_NetworkInfo ninfo;
73         
74         struct netr_SamBaseInfo *base;
75
76         uint16 validation_level;
77         
78         samlogon_state->r.in.logon.network = &ninfo;
79         samlogon_state->r_flags.in.logon.network = &ninfo;
80         
81         ninfo.identity_info.domain_name.string = samlogon_state->account_domain;
82         ninfo.identity_info.parameter_control = 0;
83         ninfo.identity_info.logon_id_low = 0;
84         ninfo.identity_info.logon_id_high = 0;
85         ninfo.identity_info.account_name.string = samlogon_state->account_name;
86         ninfo.identity_info.workstation.string = TEST_MACHINE_NAME;
87                 
88         memcpy(ninfo.challenge, chall->data, 8);
89                 
90         switch (break_which) {
91         case BREAK_NONE:
92                 break;
93         case BREAK_LM:
94                 if (lm_response && lm_response->data) {
95                         lm_response->data[0]++;
96                 }
97                 break;
98         case BREAK_NT:
99                 if (nt_response && nt_response->data) {
100                         nt_response->data[0]++;
101                 }
102                 break;
103         case BREAK_BOTH:
104                 if (lm_response && lm_response->data) {
105                         lm_response->data[0]++;
106                 }
107                 if (nt_response && nt_response->data) {
108                         nt_response->data[0]++;
109                 }
110                 break;
111         case NO_LM:
112                 data_blob_free(lm_response);
113                 break;
114         case NO_NT:
115                 data_blob_free(nt_response);
116                 break;
117         }
118                 
119         if (nt_response) {
120                 ninfo.nt.data = nt_response->data;
121                 ninfo.nt.length = nt_response->length;
122         } else {
123                 ninfo.nt.data = NULL;
124                 ninfo.nt.length = 0;
125         }
126                 
127         if (lm_response) {
128                 ninfo.lm.data = lm_response->data;
129                 ninfo.lm.length = lm_response->length;
130         } else {
131                 ninfo.lm.data = NULL;
132                 ninfo.lm.length = 0;
133         }
134         
135         ZERO_STRUCT(samlogon_state->auth2);
136         creds_client_authenticator(samlogon_state->creds, &samlogon_state->auth);
137
138         switch (samlogon_state->function_level) {
139         case DCERPC_NETR_LOGONSAMLOGON: 
140                 r->out.return_authenticator = NULL;
141                 status = dcerpc_netr_LogonSamLogon(samlogon_state->p, samlogon_state->mem_ctx, r);
142                 if (!r->out.return_authenticator || 
143                     !creds_client_check(samlogon_state->creds, &r->out.return_authenticator->cred)) {
144                         printf("Credential chaining failed\n");
145                 }
146                 if (!NT_STATUS_IS_OK(status)) {
147                         if (error_string) {
148                                 *error_string = strdup(nt_errstr(status));
149                         }
150                 }
151
152                 validation_level = r->in.validation_level;
153                 switch (validation_level) {
154                 case 2:
155                         base = &r->out.validation.sam2->base;
156                         break;
157                 case 3:
158                         base = &r->out.validation.sam3->base;
159                         break;
160                 case 6:
161                         base = &r->out.validation.sam6->base;
162                         break;
163                 }
164                 break;
165         case DCERPC_NETR_LOGONSAMLOGONWITHFLAGS: 
166                 r_flags->out.return_authenticator = NULL;
167                 status = dcerpc_netr_LogonSamLogonWithFlags(samlogon_state->p, samlogon_state->mem_ctx, r_flags);
168                 if (!r_flags->out.return_authenticator || 
169                     !creds_client_check(samlogon_state->creds, &r_flags->out.return_authenticator->cred)) {
170                         printf("Credential chaining failed\n");
171                 }
172                 if (!NT_STATUS_IS_OK(status)) {
173                         if (error_string) {
174                                 *error_string = strdup(nt_errstr(status));
175                         }
176                 }
177
178                 validation_level = r_flags->in.validation_level;
179                 switch (validation_level) {
180                 case 2:
181                         base = &r_flags->out.validation.sam2->base;
182                         break;
183                 case 3:
184                         base = &r_flags->out.validation.sam3->base;
185                         break;
186                 case 6:
187                         base = &r_flags->out.validation.sam6->base;
188                         break;
189                 }
190                 break;
191         }
192                 
193
194         if (!NT_STATUS_IS_OK(status)) {
195                 /* we cannot check the session key, if the logon failed... */
196                 return status;
197         }
198                 
199
200         /* find and decyrpt the session keys, return in parameters above */
201         if (validation_level == 6) {
202                 /* they aren't encrypted! */
203                 if (user_session_key) {
204                         memcpy(user_session_key, base->key.key, 16);
205                 }
206                 if (lm_key) {
207                         memcpy(lm_key, base->LMSessKey.key, 8);
208                 }
209         } else if (samlogon_state->creds->negotiate_flags) {
210                 static const char zeros[16];
211                         
212                 if (memcmp(base->key.key, zeros,  
213                            sizeof(base->key.key)) != 0) {
214                         creds_arcfour_crypt(samlogon_state->creds, 
215                                             base->key.key, 
216                                             sizeof(base->key.key));
217                 }
218                         
219                 if (user_session_key) {
220                         memcpy(user_session_key, base->key.key, 16);
221                 }
222                         
223                 if (memcmp(base->LMSessKey.key, zeros,  
224                            sizeof(base->LMSessKey.key)) != 0) {
225                         creds_arcfour_crypt(samlogon_state->creds, 
226                                             base->LMSessKey.key, 
227                                             sizeof(base->LMSessKey.key));
228                 }
229                         
230                 if (lm_key) {
231                         memcpy(lm_key, base->LMSessKey.key, 8);
232                 }
233         } else {
234                 static const char zeros[16];
235                         
236                 if (user_session_key) {
237                         memcpy(user_session_key, base->key.key, 16);
238                 }
239
240                 if (memcmp(base->LMSessKey.key, zeros,  
241                            sizeof(base->LMSessKey.key)) != 0) {
242                         creds_des_decrypt_LMKey(samlogon_state->creds, 
243                                                 &base->LMSessKey);
244                 }
245                         
246                 if (lm_key) {
247                         memcpy(lm_key, base->LMSessKey.key, 8);
248                 }
249         }
250         
251         return status;
252
253
254
255 /* 
256  * Test the normal 'LM and NTLM' combination
257  */
258
259 static BOOL test_lm_ntlm_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string) 
260 {
261         BOOL pass = True;
262         BOOL lm_good;
263         NTSTATUS nt_status;
264         DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
265         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
266         DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
267
268         uint8_t lm_key[8];
269         uint8_t user_session_key[16];
270         uint8_t lm_hash[16];
271         uint8_t nt_hash[16];
272         
273         ZERO_STRUCT(lm_key);
274         ZERO_STRUCT(user_session_key);
275
276         lm_good = SMBencrypt(samlogon_state->password, samlogon_state->chall.data, lm_response.data);
277         if (!lm_good) {
278                 ZERO_STRUCT(lm_hash);
279         } else {
280                 E_deshash(samlogon_state->password, lm_hash); 
281         }
282                 
283         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
284
285         E_md4hash(samlogon_state->password, nt_hash);
286         SMBsesskeygen_ntv1(nt_hash, session_key.data);
287
288         nt_status = check_samlogon(samlogon_state,
289                                    break_which,
290                                    &samlogon_state->chall,
291                                    &lm_response,
292                                    &nt_response,
293                                    lm_key, 
294                                    user_session_key,
295                                    error_string);
296         
297         data_blob_free(&lm_response);
298
299         if (NT_STATUS_EQUAL(NT_STATUS_WRONG_PASSWORD, nt_status)) {
300                 /* for 'long' passwords, the LM password is invalid */
301                 if (break_which == NO_NT && !lm_good) {
302                         return True;
303                 }
304                 return ((break_which == BREAK_NT) || (break_which == BREAK_BOTH));
305         }
306
307         if (!NT_STATUS_IS_OK(nt_status)) {
308                 return False;
309         }
310
311         if (break_which == NO_NT && !lm_good) {
312                 printf("LM password is 'long' (> 14 chars and therefore invalid) but login did not fail!");
313                 return False;
314         }
315
316         if (memcmp(lm_hash, lm_key, 
317                    sizeof(lm_key)) != 0) {
318                 printf("LM Key does not match expectations!\n");
319                 printf("lm_key:\n");
320                 dump_data(1, (const char *)lm_key, 8);
321                 printf("expected:\n");
322                 dump_data(1, (const char *)lm_hash, 8);
323                 pass = False;
324         }
325
326         switch (break_which) {
327         case NO_NT:
328         {
329                 char lm_key_expected[16];
330                 memcpy(lm_key_expected, lm_hash, 8);
331                 memset(lm_key_expected+8, '\0', 8);
332                 if (memcmp(lm_key_expected, user_session_key, 
333                            16) != 0) {
334                         printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
335                         printf("user_session_key:\n");
336                         dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
337                         printf("expected:\n");
338                         dump_data(1, (const char *)lm_key_expected, sizeof(lm_key_expected));
339                         pass = False;
340                 }
341                 break;
342         }
343         default:
344                 if (memcmp(session_key.data, user_session_key, 
345                            sizeof(user_session_key)) != 0) {
346                         printf("NT Session Key does not match expectations!\n");
347                         printf("user_session_key:\n");
348                         dump_data(1, (const char *)user_session_key, 16);
349                         printf("expected:\n");
350                         dump_data(1, (const char *)session_key.data, session_key.length);
351                         pass = False;
352                 }
353         }
354         return pass;
355 }
356
357 /* 
358  * Test LM authentication, no NT response supplied
359  */
360
361 static BOOL test_lm(struct samlogon_state *samlogon_state, char **error_string) 
362 {
363
364         return test_lm_ntlm_broken(samlogon_state, NO_NT, error_string);
365 }
366
367 /* 
368  * Test the NTLM response only, no LM.
369  */
370
371 static BOOL test_ntlm(struct samlogon_state *samlogon_state, char **error_string) 
372 {
373         return test_lm_ntlm_broken(samlogon_state, NO_LM, error_string);
374 }
375
376 /* 
377  * Test the NTLM response only, but in the LM field.
378  */
379
380 static BOOL test_ntlm_in_lm(struct samlogon_state *samlogon_state, char **error_string) 
381 {
382         BOOL pass = True;
383         NTSTATUS nt_status;
384         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
385
386         uint8_t lm_key[8];
387         uint8_t lm_hash[16];
388         uint8_t user_session_key[16];
389         
390         ZERO_STRUCT(user_session_key);
391
392         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
393
394         E_deshash(samlogon_state->password, lm_hash); 
395
396         nt_status = check_samlogon(samlogon_state,
397                                    BREAK_NONE,
398                                    &samlogon_state->chall,
399                                    &nt_response,
400                                    NULL,
401                                    lm_key, 
402                                    user_session_key,
403                                    error_string);
404         
405         if (!NT_STATUS_IS_OK(nt_status)) {
406                 return False;
407         }
408
409         if (memcmp(lm_hash, lm_key, 
410                    sizeof(lm_key)) != 0) {
411                 printf("LM Key does not match expectations!\n");
412                 printf("lm_key:\n");
413                 dump_data(1, (const char *)lm_key, 8);
414                 printf("expected:\n");
415                 dump_data(1, (const char *)lm_hash, 8);
416                 pass = False;
417         }
418         if (memcmp(lm_hash, user_session_key, 8) != 0) {
419                 char lm_key_expected[16];
420                 memcpy(lm_key_expected, lm_hash, 8);
421                 memset(lm_key_expected+8, '\0', 8);
422                 if (memcmp(lm_key_expected, user_session_key, 
423                            16) != 0) {
424                         printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
425                         printf("user_session_key:\n");
426                         dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
427                         printf("expected:\n");
428                         dump_data(1, (const char *)lm_key_expected, sizeof(lm_key_expected));
429                         pass = False;
430                 }
431         }
432         return pass;
433 }
434
435 /* 
436  * Test the NTLM response only, but in the both the NT and LM fields.
437  */
438
439 static BOOL test_ntlm_in_both(struct samlogon_state *samlogon_state, char **error_string) 
440 {
441         BOOL pass = True;
442         BOOL lm_good;
443         NTSTATUS nt_status;
444         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
445         DATA_BLOB session_key = data_blob_talloc(samlogon_state->mem_ctx, NULL, 16);
446
447         char lm_key[8];
448         char lm_hash[16];
449         char user_session_key[16];
450         char nt_hash[16];
451         
452         ZERO_STRUCT(lm_key);
453         ZERO_STRUCT(user_session_key);
454
455         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, 
456                      nt_response.data);
457         E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
458         SMBsesskeygen_ntv1((const uint8_t *)nt_hash, 
459                            session_key.data);
460
461         lm_good = E_deshash(samlogon_state->password, (uint8_t *)lm_hash); 
462         if (!lm_good) {
463                 ZERO_STRUCT(lm_hash);
464         }
465
466         nt_status = check_samlogon(samlogon_state,
467                                    BREAK_NONE,
468                                    &samlogon_state->chall,
469                                    NULL, 
470                                    &nt_response,
471                                    lm_key, 
472                                    user_session_key,
473                                    error_string);
474         
475         if (!NT_STATUS_IS_OK(nt_status)) {
476                 return False;
477         }
478
479         if (memcmp(lm_hash, lm_key, 
480                    sizeof(lm_key)) != 0) {
481                 printf("LM Key does not match expectations!\n");
482                 printf("lm_key:\n");
483                 dump_data(1, lm_key, 8);
484                 printf("expected:\n");
485                 dump_data(1, lm_hash, 8);
486                 pass = False;
487         }
488         if (memcmp(session_key.data, user_session_key, 
489                    sizeof(user_session_key)) != 0) {
490                 printf("NT Session Key does not match expectations!\n");
491                 printf("user_session_key:\n");
492                 dump_data(1, user_session_key, 16);
493                 printf("expected:\n");
494                 dump_data(1, (const char *)session_key.data, session_key.length);
495                 pass = False;
496         }
497
498
499         return pass;
500 }
501
502 /* 
503  * Test the NTLMv2 and LMv2 responses
504  */
505
506 static BOOL test_lmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string) 
507 {
508         BOOL pass = True;
509         NTSTATUS nt_status;
510         DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
511         DATA_BLOB lmv2_response = data_blob(NULL, 0);
512         DATA_BLOB lmv2_session_key = data_blob(NULL, 0);
513         DATA_BLOB ntlmv2_session_key = data_blob(NULL, 0);
514         DATA_BLOB names_blob = NTLMv2_generate_names_blob(samlogon_state->mem_ctx, lp_netbios_name(), lp_workgroup());
515
516         uint8_t lm_session_key[8];
517         uint8_t user_session_key[16];
518
519         ZERO_STRUCT(lm_session_key);
520         ZERO_STRUCT(user_session_key);
521         
522         /* TODO - test with various domain cases, and without domain */
523         if (!SMBNTLMv2encrypt(samlogon_state->account_name, samlogon_state->account_domain, 
524                               samlogon_state->password, &samlogon_state->chall,
525                               &names_blob,
526                               &lmv2_response, &ntlmv2_response, 
527                               &lmv2_session_key, &ntlmv2_session_key)) {
528                 data_blob_free(&names_blob);
529                 return False;
530         }
531         data_blob_free(&names_blob);
532
533         nt_status = check_samlogon(samlogon_state,
534                                    break_which,
535                                    &samlogon_state->chall,
536                                    &lmv2_response,
537                                    &ntlmv2_response,
538                                    lm_session_key, 
539                                    user_session_key,
540                                    error_string);
541         
542         data_blob_free(&lmv2_response);
543         data_blob_free(&ntlmv2_response);
544
545
546         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
547                 return break_which == BREAK_BOTH;
548         }
549
550         switch (break_which) {
551         case NO_NT:
552                 if (memcmp(lmv2_session_key.data, user_session_key, 
553                            sizeof(user_session_key)) != 0) {
554                         printf("USER (LMv2) Session Key does not match expectations!\n");
555                         printf("user_session_key:\n");
556                         dump_data(1, (const char *)user_session_key, 16);
557                         printf("expected:\n");
558                         dump_data(1, (const char *)lmv2_session_key.data, ntlmv2_session_key.length);
559                         pass = False;
560                 }
561                 if (memcmp(lmv2_session_key.data, lm_session_key, 
562                            sizeof(lm_session_key)) != 0) {
563                         printf("LM (LMv2) Session Key does not match expectations!\n");
564                         printf("lm_session_key:\n");
565                         dump_data(1, (const char *)lm_session_key, 8);
566                         printf("expected:\n");
567                         dump_data(1, (const char *)lmv2_session_key.data, 8);
568                         pass = False;
569                 }
570                 break;
571         default:
572                 if (memcmp(ntlmv2_session_key.data, user_session_key, 
573                            sizeof(user_session_key)) != 0) {
574                         printf("USER (NTLMv2) Session Key does not match expectations!\n");
575                         printf("user_session_key:\n");
576                         dump_data(1, (const char *)user_session_key, 16);
577                         printf("expected:\n");
578                         dump_data(1, (const char *)ntlmv2_session_key.data, ntlmv2_session_key.length);
579                         pass = False;
580                 }
581                 if (memcmp(ntlmv2_session_key.data, lm_session_key, 
582                            sizeof(lm_session_key)) != 0) {
583                         printf("LM (NTLMv2) Session Key does not match expectations!\n");
584                         printf("lm_session_key:\n");
585                         dump_data(1, (const char *)lm_session_key, 8);
586                         printf("expected:\n");
587                         dump_data(1, (const char *)ntlmv2_session_key.data, 8);
588                         pass = False;
589                 }
590         }
591
592         return pass;
593 }
594
595 /* 
596  * Test the NTLMv2 and LMv2 responses
597  */
598
599 static BOOL test_lmv2_ntlmv2(struct samlogon_state *samlogon_state, char **error_string) 
600 {
601         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NONE, error_string);
602 }
603
604 /* 
605  * Test the LMv2 response only
606  */
607
608 static BOOL test_lmv2(struct samlogon_state *samlogon_state, char **error_string) 
609 {
610         return test_lmv2_ntlmv2_broken(samlogon_state, NO_NT, error_string);
611 }
612
613 /* 
614  * Test the NTLMv2 response only
615  */
616
617 static BOOL test_ntlmv2(struct samlogon_state *samlogon_state, char **error_string) 
618 {
619         return test_lmv2_ntlmv2_broken(samlogon_state, NO_LM, error_string);
620 }
621
622 static BOOL test_lm_ntlm(struct samlogon_state *samlogon_state, char **error_string) 
623 {
624         return test_lm_ntlm_broken(samlogon_state, BREAK_NONE, error_string);
625 }
626
627 static BOOL test_ntlm_lm_broken(struct samlogon_state *samlogon_state, char **error_string) 
628 {
629         return test_lm_ntlm_broken(samlogon_state, BREAK_LM, error_string);
630 }
631
632 static BOOL test_ntlm_ntlm_broken(struct samlogon_state *samlogon_state, char **error_string) 
633 {
634         return test_lm_ntlm_broken(samlogon_state, BREAK_NT, error_string);
635 }
636
637 static BOOL test_lm_ntlm_both_broken(struct samlogon_state *samlogon_state, char **error_string) 
638 {
639         return test_lm_ntlm_broken(samlogon_state, BREAK_BOTH, error_string);
640 }
641 static BOOL test_ntlmv2_lmv2_broken(struct samlogon_state *samlogon_state, char **error_string) 
642 {
643         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_LM, error_string);
644 }
645
646 static BOOL test_ntlmv2_ntlmv2_broken(struct samlogon_state *samlogon_state, char **error_string) 
647 {
648         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_NT, error_string);
649 }
650
651 static BOOL test_ntlmv2_both_broken(struct samlogon_state *samlogon_state, char **error_string) 
652 {
653         return test_lmv2_ntlmv2_broken(samlogon_state, BREAK_BOTH, error_string);
654 }
655
656 /* 
657  * Test the NTLM2 response (extra challenge in LM feild)
658  *
659  * This test is the same as the 'break LM' test, but checks that the
660  * server implements NTLM2 session security in the right place
661  * (NETLOGON is the wrong place).
662  */
663
664 static BOOL test_ntlm2(struct samlogon_state *samlogon_state, char **error_string) 
665 {
666         BOOL pass = True;
667         NTSTATUS nt_status;
668         DATA_BLOB lm_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
669         DATA_BLOB nt_response = data_blob_talloc(samlogon_state->mem_ctx, NULL, 24);
670
671         uint8_t lm_key[8];
672         uint8_t nt_hash[16];
673         uint8_t lm_hash[16];
674         uint8_t nt_key[16];
675         uint8_t user_session_key[16];
676         uint8_t expected_user_session_key[16];
677         uint8_t session_nonce_hash[16];
678         uint8_t client_chall[8];
679         
680         struct MD5Context md5_session_nonce_ctx;
681         HMACMD5Context hmac_ctx;
682                         
683         ZERO_STRUCT(user_session_key);
684         ZERO_STRUCT(lm_key);
685         generate_random_buffer(client_chall, 8);
686         
687         MD5Init(&md5_session_nonce_ctx);
688         MD5Update(&md5_session_nonce_ctx, samlogon_state->chall.data, 8);
689         MD5Update(&md5_session_nonce_ctx, client_chall, 8);
690         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
691         
692         E_md4hash(samlogon_state->password, (uint8_t *)nt_hash);
693         E_deshash(samlogon_state->password, (uint8_t *)lm_hash);
694         SMBsesskeygen_ntv1((const uint8_t *)nt_hash, 
695                            nt_key);
696
697         SMBNTencrypt(samlogon_state->password, samlogon_state->chall.data, nt_response.data);
698
699         memcpy(lm_response.data, session_nonce_hash, 8);
700         memset(lm_response.data + 8, 0, 16);
701
702         hmac_md5_init_rfc2104(nt_key, 16, &hmac_ctx);
703         hmac_md5_update(samlogon_state->chall.data, 8, &hmac_ctx);
704         hmac_md5_update(client_chall, 8, &hmac_ctx);
705         hmac_md5_final(expected_user_session_key, &hmac_ctx);
706
707         nt_status = check_samlogon(samlogon_state,
708                                    BREAK_NONE,
709                                    &samlogon_state->chall,
710                                    &lm_response,
711                                    &nt_response,
712                                    lm_key, 
713                                    user_session_key,
714                                    error_string);
715         
716         if (!NT_STATUS_IS_OK(nt_status)) {
717                 return False;
718         }
719
720         if (memcmp(lm_hash, lm_key, 
721                    sizeof(lm_key)) != 0) {
722                 printf("LM Key does not match expectations!\n");
723                 printf("lm_key:\n");
724                 dump_data(1, (const char *)lm_key, 8);
725                 printf("expected:\n");
726                 dump_data(1, (const char *)lm_hash, 8);
727                 pass = False;
728         }
729         if (memcmp(nt_key, user_session_key, 16) != 0) {
730                 printf("NT Session Key does not match expectations (should be first-8 LM hash)!\n");
731                 printf("user_session_key:\n");
732                 dump_data(1, (const char *)user_session_key, sizeof(user_session_key));
733                 printf("expected:\n");
734                 dump_data(1, (const char *)nt_key, sizeof(nt_key));
735                 pass = False;
736         }
737         return pass;
738 }
739
740 static BOOL test_plaintext(struct samlogon_state *samlogon_state, enum ntlm_break break_which, char **error_string)
741 {
742         NTSTATUS nt_status;
743         DATA_BLOB nt_response = data_blob(NULL, 0);
744         DATA_BLOB lm_response = data_blob(NULL, 0);
745         char *password;
746         char *dospw;
747         void *unicodepw;
748
749         uint8_t user_session_key[16];
750         uint8_t lm_key[16];
751         static const uint8_t zeros[8];
752         DATA_BLOB chall = data_blob_talloc(samlogon_state->mem_ctx, zeros, sizeof(zeros));
753
754         ZERO_STRUCT(user_session_key);
755         
756         if ((push_ucs2_talloc(samlogon_state->mem_ctx, &unicodepw, 
757                               samlogon_state->password)) == -1) {
758                 DEBUG(0, ("push_ucs2_allocate failed!\n"));
759                 exit(1);
760         }
761
762         nt_response = data_blob_talloc(samlogon_state->mem_ctx, unicodepw, strlen_m(samlogon_state->password)*2);
763
764         password = strupper_talloc(samlogon_state->mem_ctx, samlogon_state->password);
765
766         if ((convert_string_talloc(samlogon_state->mem_ctx, CH_UNIX, 
767                                    CH_DOS, password,
768                                    strlen(password)+1, 
769                                    (void**)&dospw)) == -1) {
770                 DEBUG(0, ("convert_string_talloc failed!\n"));
771                 exit(1);
772         }
773
774         lm_response = data_blob_talloc(samlogon_state->mem_ctx, dospw, strlen(dospw));
775
776         nt_status = check_samlogon(samlogon_state,
777                                    break_which,
778                                    &chall,
779                                    &lm_response,
780                                    &nt_response,
781                                    lm_key, 
782                                    user_session_key,
783                                    error_string);
784         
785         if (!NT_STATUS_IS_OK(nt_status)) {
786                 return break_which == BREAK_NT;
787         }
788
789         return True;
790 }
791
792 static BOOL test_plaintext_none_broken(struct samlogon_state *samlogon_state, 
793                                        char **error_string) {
794         return test_plaintext(samlogon_state, BREAK_NONE, error_string);
795 }
796
797 static BOOL test_plaintext_lm_broken(struct samlogon_state *samlogon_state, 
798                                      char **error_string) {
799         return test_plaintext(samlogon_state, BREAK_LM, error_string);
800 }
801
802 static BOOL test_plaintext_nt_broken(struct samlogon_state *samlogon_state, 
803                                      char **error_string) {
804         return test_plaintext(samlogon_state, BREAK_NT, error_string);
805 }
806
807 static BOOL test_plaintext_nt_only(struct samlogon_state *samlogon_state, 
808                                    char **error_string) {
809         return test_plaintext(samlogon_state, NO_LM, error_string);
810 }
811
812 static BOOL test_plaintext_lm_only(struct samlogon_state *samlogon_state, 
813                                    char **error_string) {
814         return test_plaintext(samlogon_state, NO_NT, error_string);
815 }
816
817 /* 
818    Tests:
819    
820    - LM only
821    - NT and LM             
822    - NT
823    - NT in LM field
824    - NT in both fields
825    - NTLMv2
826    - NTLMv2 and LMv2
827    - LMv2
828    - plaintext tests (in challenge-response fields)
829   
830    check we get the correct session key in each case
831    check what values we get for the LM session key
832    
833 */
834
835 static const struct ntlm_tests {
836         BOOL (*fn)(struct samlogon_state *, char **);
837         const char *name;
838         BOOL expect_fail;
839 } test_table[] = {
840         {test_lm, "LM", False},
841         {test_lm_ntlm, "LM and NTLM", False},
842         {test_lm_ntlm_both_broken, "LM and NTLM, both broken", False},
843         {test_ntlm, "NTLM", False},
844         {test_ntlm_in_lm, "NTLM in LM", False},
845         {test_ntlm_in_both, "NTLM in both", False},
846         {test_ntlmv2, "NTLMv2", False},
847         {test_lmv2_ntlmv2, "NTLMv2 and LMv2", False},
848         {test_lmv2, "LMv2", False},
849         {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken", False},
850         {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken", False},
851         {test_ntlmv2_both_broken, "NTLMv2 and LMv2, both broken", False},
852         {test_ntlm_lm_broken, "NTLM and LM, LM broken", False},
853         {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken", False},
854         {test_ntlm2, "NTLM2 (NTLMv2 session security)", False},
855         {test_plaintext_none_broken, "Plaintext", True},
856         {test_plaintext_lm_broken, "Plaintext LM broken", True},
857         {test_plaintext_nt_broken, "Plaintext NT broken", True},
858         {test_plaintext_nt_only, "Plaintext NT only", True},
859         {test_plaintext_lm_only, "Plaintext LM only", True},
860         {NULL, NULL}
861 };
862
863 /*
864   try a netlogon SamLogon
865 */
866 static BOOL test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
867                           struct creds_CredentialState *creds)
868 {
869         int i, v, l, f;
870         BOOL ret = True;
871         int validation_levels[] = {2,3,6};
872         int logon_levels[] = { 2, 6 };
873         int function_levels[] = { 
874                 DCERPC_NETR_LOGONSAMLOGON,
875                 DCERPC_NETR_LOGONSAMLOGONWITHFLAGS };
876         struct samlogon_state samlogon_state;
877         
878         printf("testing netr_LogonSamLogon and netr_LogonSamLogonWithFlags\n");
879         
880         samlogon_state.mem_ctx = mem_ctx;
881         samlogon_state.account_name = lp_parm_string(-1, "torture", "username");
882         samlogon_state.account_domain = lp_parm_string(-1, "torture", "userdomain");
883         samlogon_state.password = lp_parm_string(-1, "torture", "password");
884         samlogon_state.p = p;
885         samlogon_state.creds = creds;
886
887         samlogon_state.chall = data_blob_talloc(mem_ctx, NULL, 8);
888
889         generate_random_buffer(samlogon_state.chall.data, 8);
890         samlogon_state.r_flags.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
891         samlogon_state.r_flags.in.workstation = TEST_MACHINE_NAME;
892         samlogon_state.r_flags.in.credential = &samlogon_state.auth;
893         samlogon_state.r_flags.in.return_authenticator = &samlogon_state.auth2;
894         samlogon_state.r_flags.in.flags = 0;
895
896         samlogon_state.r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
897         samlogon_state.r.in.workstation = TEST_MACHINE_NAME;
898         samlogon_state.r.in.credential = &samlogon_state.auth;
899         samlogon_state.r.in.return_authenticator = &samlogon_state.auth2;
900
901         for (i=0; test_table[i].fn; i++) {
902                 for (v=0;v<ARRAY_SIZE(validation_levels);v++) {
903                         for (l=0;l<ARRAY_SIZE(logon_levels);l++) {
904                                 for (f=0;f<ARRAY_SIZE(function_levels);f++) {
905                                         char *error_string = NULL;
906                                         samlogon_state.function_level = function_levels[f];
907                                         samlogon_state.r.in.validation_level = validation_levels[v];
908                                         samlogon_state.r.in.logon_level = logon_levels[l];
909                                         samlogon_state.r_flags.in.validation_level = validation_levels[v];
910                                         samlogon_state.r_flags.in.logon_level = logon_levels[l];
911                                         if (!test_table[i].fn(&samlogon_state, &error_string)) {
912                                                 printf("Testing '%s' at validation level %d, logon level %d, function %d: \n", 
913                                                        test_table[i].name, validation_levels[v], 
914                                                        logon_levels[l], function_levels[f]);
915                                                 
916                                                 if (test_table[i].expect_fail) {
917                                                         printf(" failed (expected, test incomplete): %s\n", error_string);
918                                                 } else {
919                                                         printf(" failed: %s\n", error_string);
920                                                         ret = False;
921                                                 }
922                                                 SAFE_FREE(error_string);
923                                         }
924                                 }
925                         }
926                 }
927         }
928
929         return ret;
930 }
931
932 /*
933   test an ADS style interactive domain logon
934 */
935 static BOOL test_InteractiveLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
936                                   struct creds_CredentialState *creds)
937 {
938         NTSTATUS status;
939         struct netr_LogonSamLogonWithFlags r;
940         struct netr_Authenticator a, ra;
941         struct netr_PasswordInfo pinfo;
942         const char *plain_pass;
943
944         ZERO_STRUCT(a);
945         ZERO_STRUCT(r);
946         ZERO_STRUCT(ra);
947
948         creds_client_authenticator(creds, &a);
949
950         r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
951         r.in.workstation = TEST_MACHINE_NAME;
952         r.in.credential = &a;
953         r.in.return_authenticator = &ra;
954         r.in.logon_level = 5;
955         r.in.logon.password = &pinfo;
956         r.in.validation_level = 6;
957         r.in.flags = 0;
958
959         pinfo.identity_info.domain_name.string = lp_parm_string(-1, "torture", "userdomain");
960         pinfo.identity_info.parameter_control = 0;
961         pinfo.identity_info.logon_id_low = 0;
962         pinfo.identity_info.logon_id_high = 0;
963         pinfo.identity_info.account_name.string = lp_parm_string(-1, "torture", "username");
964         pinfo.identity_info.workstation.string = TEST_MACHINE_NAME;
965
966         plain_pass = lp_parm_string(-1, "torture", "password");
967
968         E_deshash(plain_pass, pinfo.lmpassword.hash);
969         E_md4hash(plain_pass, pinfo.ntpassword.hash);
970
971         if (creds->negotiate_flags) {
972                 creds_arcfour_crypt(creds, pinfo.lmpassword.hash, 16);
973                 creds_arcfour_crypt(creds, pinfo.ntpassword.hash, 16);
974         } else {
975                 creds_des_encrypt(creds, &pinfo.lmpassword);
976                 creds_des_encrypt(creds, &pinfo.ntpassword);
977         }
978
979         printf("Testing netr_LogonSamLogonWithFlags\n");
980
981         status = dcerpc_netr_LogonSamLogonWithFlags(p, mem_ctx, &r);
982         if (!NT_STATUS_IS_OK(status)) {
983                 printf("netr_LogonSamLogonWithFlags - %s\n", nt_errstr(status));
984                 return False;
985         }
986
987         if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
988                 printf("Credential chaining failed\n");
989                 return False;
990         }
991
992         return True;
993 }
994
995
996
997 BOOL torture_rpc_samlogon(void)
998 {
999         NTSTATUS status;
1000         struct dcerpc_pipe *p;
1001         TALLOC_CTX *mem_ctx;
1002         BOOL ret = True;
1003         void *join_ctx;
1004         const char *machine_pass;
1005
1006         struct creds_CredentialState creds;
1007
1008         mem_ctx = talloc_init("torture_rpc_netlogon");
1009
1010         join_ctx = torture_join_domain(TEST_MACHINE_NAME, lp_workgroup(), ACB_SVRTRUST, 
1011                                        &machine_pass);
1012         if (!join_ctx) {
1013                 printf("Failed to join as BDC\n");
1014                 return False;
1015         }
1016
1017         status = torture_rpc_connection(&p, 
1018                                         DCERPC_NETLOGON_NAME,
1019                                         DCERPC_NETLOGON_UUID,
1020                                         DCERPC_NETLOGON_VERSION);
1021         if (!NT_STATUS_IS_OK(status)) {
1022                 return False;
1023         }
1024
1025
1026         if (!test_SetupCredentials(p, mem_ctx, 
1027                                     TEST_MACHINE_NAME, machine_pass, &creds)) {
1028                 ret = False;
1029         }
1030
1031         if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1032                 ret = False;
1033         }
1034
1035         if (!test_SamLogon(p, mem_ctx, &creds)) {
1036                 ret = False;
1037         }
1038
1039         if (!test_SetupCredentials2(p, mem_ctx, NETLOGON_NEG_AUTH2_FLAGS,
1040                                     TEST_MACHINE_NAME, machine_pass, &creds)) {
1041                 return False;
1042         }
1043
1044         if (!test_SamLogon(p, mem_ctx, &creds)) {
1045                 ret = False;
1046         }
1047
1048         if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1049                 ret = False;
1050         }
1051
1052         if (!test_SetupCredentials3(p, mem_ctx, NETLOGON_NEG_AUTH2_FLAGS,
1053                                     TEST_MACHINE_NAME, machine_pass, &creds)) {
1054                 return False;
1055         }
1056
1057         if (!test_SamLogon(p, mem_ctx, &creds)) {
1058                 ret = False;
1059         }
1060
1061         if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1062                 ret = False;
1063         }
1064
1065         if (!test_SetupCredentials3(p, mem_ctx, NETLOGON_NEG_AUTH2_ADS_FLAGS,
1066                                     TEST_MACHINE_NAME, machine_pass, &creds)) {
1067                 return False;
1068         }
1069
1070         if (!test_SamLogon(p, mem_ctx, &creds)) {
1071                 ret = False;
1072         }
1073
1074         if (!test_InteractiveLogon(p, mem_ctx, &creds)) {
1075                 ret = False;
1076         }
1077
1078         talloc_destroy(mem_ctx);
1079
1080         torture_rpc_close(p);
1081
1082         torture_leave_domain(join_ctx);
1083
1084         return ret;
1085 }