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