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