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