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