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