Thanks to Serassio Guido for noticing issues in our Squid NTLMSSP
[samba.git] / source3 / utils / ntlm_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind status program.
5
6    Copyright (C) Tim Potter      2000-2002
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
8    Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000 
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 #define SQUID_BUFFER_SIZE 2010
31
32 enum stdio_helper_mode {
33         SQUID_2_4_BASIC,
34         SQUID_2_5_BASIC,
35         SQUID_2_5_NTLMSSP,
36         GSS_SPNEGO,
37         GSS_SPNEGO_CLIENT,
38         NUM_HELPER_MODES
39 };
40
41 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode, 
42                                      char *buf, int length);
43
44 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode, 
45                                         char *buf, int length);
46
47 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode, 
48                                           char *buf, int length);
49
50 static void manage_gss_spnego_request (enum stdio_helper_mode stdio_helper_mode, 
51                                        char *buf, int length);
52
53 static void manage_gss_spnego_client_request (enum stdio_helper_mode stdio_helper_mode, 
54                                               char *buf, int length);
55
56 static const struct {
57         enum stdio_helper_mode mode;
58         const char *name;
59         stdio_helper_function fn;
60 } stdio_helper_protocols[] = {
61         { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
62         { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
63         { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
64         { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
65         { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
66         { NUM_HELPER_MODES, NULL, NULL}
67 };
68
69 extern int winbindd_fd;
70
71 static const char *opt_username;
72 static const char *opt_domain;
73 static const char *opt_workstation;
74 static const char *opt_password;
75 static DATA_BLOB opt_challenge;
76 static DATA_BLOB opt_lm_response;
77 static DATA_BLOB opt_nt_response;
78 static int request_lm_key;
79 static int request_nt_key;
80
81
82 static char winbind_separator(void)
83 {
84         struct winbindd_response response;
85         static BOOL got_sep;
86         static char sep;
87
88         if (got_sep)
89                 return sep;
90
91         ZERO_STRUCT(response);
92
93         /* Send off request */
94
95         if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
96             NSS_STATUS_SUCCESS) {
97                 d_printf("could not obtain winbind separator!\n");
98                 return '\\';
99         }
100
101         sep = response.data.info.winbind_separator;
102         got_sep = True;
103
104         if (!sep) {
105                 d_printf("winbind separator was NULL!\n");
106                 return '\\';
107         }
108         
109         return sep;
110 }
111
112 static const char *get_winbind_domain(void)
113 {
114         struct winbindd_response response;
115
116         static fstring winbind_domain;
117         if (*winbind_domain) {
118                 return winbind_domain;
119         }
120
121         ZERO_STRUCT(response);
122
123         /* Send off request */
124
125         if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
126             NSS_STATUS_SUCCESS) {
127                 DEBUG(0, ("could not obtain winbind domain name!\n"));
128                 return NULL;
129         }
130
131         fstrcpy(winbind_domain, response.data.domain_name);
132
133         return winbind_domain;
134
135 }
136
137 static const char *get_winbind_netbios_name(void)
138 {
139         struct winbindd_response response;
140
141         static fstring winbind_netbios_name;
142
143         if (*winbind_netbios_name) {
144                 return winbind_netbios_name;
145         }
146
147         ZERO_STRUCT(response);
148
149         /* Send off request */
150
151         if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
152             NSS_STATUS_SUCCESS) {
153                 DEBUG(0, ("could not obtain winbind netbios name!\n"));
154                 return NULL;
155         }
156
157         fstrcpy(winbind_netbios_name, response.data.netbios_name);
158
159         return winbind_netbios_name;
160
161 }
162
163 /* Authenticate a user with a plaintext password */
164
165 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
166 {
167         struct winbindd_request request;
168         struct winbindd_response response;
169         NSS_STATUS result;
170
171         /* Send off request */
172
173         ZERO_STRUCT(request);
174         ZERO_STRUCT(response);
175
176         fstrcpy(request.data.auth.user, user);
177         fstrcpy(request.data.auth.pass, pass);
178
179         result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
180
181         /* Display response */
182         
183         if (stdout_diagnostics) {
184                 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
185                         d_printf("Reading winbind reply failed! (0x01)\n");
186                 }
187                 
188                 d_printf("%s: %s (0x%x)\n", 
189                          response.data.auth.nt_status_string, 
190                          response.data.auth.error_string, 
191                          response.data.auth.nt_status);
192         } else {
193                 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
194                         DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
195                 }
196                 
197                 DEBUG(3, ("%s: %s (0x%x)\n", 
198                           response.data.auth.nt_status_string, 
199                           response.data.auth.error_string,
200                           response.data.auth.nt_status));               
201         }
202                 
203         return (result == NSS_STATUS_SUCCESS);
204 }
205
206 /* authenticate a user with an encrypted username/password */
207
208 static NTSTATUS contact_winbind_auth_crap(const char *username, 
209                                           const char *domain, 
210                                           const char *workstation,
211                                           const DATA_BLOB *challenge, 
212                                           const DATA_BLOB *lm_response, 
213                                           const DATA_BLOB *nt_response, 
214                                           uint32 flags, 
215                                           uint8 lm_key[8], 
216                                           uint8 nt_key[16], 
217                                           char **error_string) 
218 {
219         NTSTATUS nt_status;
220         NSS_STATUS result;
221         struct winbindd_request request;
222         struct winbindd_response response;
223
224         static uint8 zeros[16];
225
226         ZERO_STRUCT(request);
227         ZERO_STRUCT(response);
228
229         request.flags = flags;
230
231         if (push_utf8_fstring(request.data.auth_crap.user, username) == -1) {
232                 *error_string = smb_xstrdup(
233                         "unable to create utf8 string for username");
234                 return NT_STATUS_UNSUCCESSFUL;
235         }
236
237         if (push_utf8_fstring(request.data.auth_crap.domain, domain) == -1) {
238                 *error_string = smb_xstrdup(
239                         "unable to create utf8 string for domain");
240                 return NT_STATUS_UNSUCCESSFUL;
241         }
242
243         if (push_utf8_fstring(request.data.auth_crap.workstation, 
244                               workstation) == -1) {
245                 *error_string = smb_xstrdup(
246                         "unable to create utf8 string for workstation");
247                 return NT_STATUS_UNSUCCESSFUL;
248         }
249
250         memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
251
252         if (lm_response && lm_response->length) {
253                 memcpy(request.data.auth_crap.lm_resp, lm_response->data, MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
254                 request.data.auth_crap.lm_resp_len = lm_response->length;
255         }
256
257         if (nt_response && nt_response->length) {
258                 memcpy(request.data.auth_crap.nt_resp, nt_response->data, MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
259                 request.data.auth_crap.nt_resp_len = nt_response->length;
260         }
261         
262         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
263
264         /* Display response */
265
266         if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
267                 nt_status = NT_STATUS_UNSUCCESSFUL;
268                 if (error_string)
269                         *error_string = smb_xstrdup("Reading winbind reply failed!");
270                 return nt_status;
271         }
272         
273         nt_status = (NT_STATUS(response.data.auth.nt_status));
274         if (!NT_STATUS_IS_OK(nt_status)) {
275                 if (error_string) 
276                         *error_string = smb_xstrdup(response.data.auth.error_string);
277                 return nt_status;
278         }
279
280         if ((flags & WBFLAG_PAM_LMKEY) && lm_key 
281             && (memcmp(zeros, response.data.auth.first_8_lm_hash, 
282                        sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
283                 memcpy(lm_key, response.data.auth.first_8_lm_hash, 
284                         sizeof(response.data.auth.first_8_lm_hash));
285         }
286         if ((flags & WBFLAG_PAM_NTKEY) && nt_key
287                     && (memcmp(zeros, response.data.auth.nt_session_key, 
288                                sizeof(response.data.auth.nt_session_key)) != 0)) {
289                 memcpy(nt_key, response.data.auth.nt_session_key, 
290                         sizeof(response.data.auth.nt_session_key));
291         }
292         return nt_status;
293 }
294                                    
295 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *nt_session_key, DATA_BLOB *lm_session_key) 
296 {
297         static const char zeros[16];
298         NTSTATUS nt_status;
299         char *error_string;
300         uint8 lm_key[8]; 
301         uint8 nt_key[16]; 
302         
303         nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
304                                               ntlmssp_state->workstation,
305                                               &ntlmssp_state->chal,
306                                               &ntlmssp_state->lm_resp,
307                                               &ntlmssp_state->nt_resp, 
308                                               WBFLAG_PAM_LMKEY | WBFLAG_PAM_NTKEY,
309                                               lm_key, nt_key, 
310                                               &error_string);
311
312         if (NT_STATUS_IS_OK(nt_status)) {
313                 if (memcmp(lm_key, zeros, 8) != 0) {
314                         *lm_session_key = data_blob(NULL, 16);
315                         memcpy(lm_session_key->data, lm_key, 8);
316                         memset(lm_session_key->data+8, '\0', 8);
317                 }
318                 
319                 if (memcmp(nt_key, zeros, 16) != 0) {
320                         *nt_session_key = data_blob(nt_key, 16);
321                 }
322         } else {
323                 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3, 
324                       ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n", 
325                        ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation, error_string ? error_string : "unknown error (NULL)"));
326         }
327         return nt_status;
328 }
329
330 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode, 
331                                          char *buf, int length) 
332 {
333         static NTLMSSP_STATE *ntlmssp_state = NULL;
334         DATA_BLOB request, reply;
335         NTSTATUS nt_status;
336
337         if (strlen(buf) < 2) {
338                 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
339                 x_fprintf(x_stdout, "BH\n");
340                 return;
341         }
342
343         if (strncmp(buf, "YR", 2) == 0) {
344                 if (ntlmssp_state)
345                         ntlmssp_end(&ntlmssp_state);
346         } else if (strncmp(buf, "KK", 2) == 0) {
347                 
348         } else {
349                 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
350                 x_fprintf(x_stdout, "BH\n");
351                 return;
352         }
353
354         if (strlen(buf) > 3) {
355                 request = base64_decode_data_blob(buf + 3);
356         } else {
357                 request = data_blob(NULL, 0);
358         }
359
360         if (!ntlmssp_state) {
361                 ntlmssp_server_start(&ntlmssp_state);
362                 ntlmssp_state->check_password = winbind_pw_check;
363                 ntlmssp_state->get_domain = get_winbind_domain;
364                 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
365         }
366
367         DEBUG(10, ("got NTLMSSP packet:\n"));
368         dump_data(10, (const char *)request.data, request.length);
369
370         nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
371         
372         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
373                 char *reply_base64 = base64_encode_data_blob(reply);
374                 x_fprintf(x_stdout, "TT %s\n", reply_base64);
375                 SAFE_FREE(reply_base64);
376                 data_blob_free(&reply);
377                 DEBUG(10, ("NTLMSSP challenge\n"));
378         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
379                 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
380                 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
381         } else if (!NT_STATUS_IS_OK(nt_status)) {
382                 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
383                 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
384         } else {
385                 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
386                 DEBUG(10, ("NTLMSSP OK!\n"));
387         }
388
389         data_blob_free(&request);
390 }
391
392 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode, 
393                                        char *buf, int length) 
394 {
395         char *user, *pass;      
396         user=buf;
397         
398         pass=memchr(buf,' ',length);
399         if (!pass) {
400                 DEBUG(2, ("Password not found. Denying access\n"));
401                 x_fprintf(x_stderr, "ERR\n");
402                 return;
403         }
404         *pass='\0';
405         pass++;
406         
407         if (stdio_helper_mode == SQUID_2_5_BASIC) {
408                 rfc1738_unescape(user);
409                 rfc1738_unescape(pass);
410         }
411         
412         if (check_plaintext_auth(user, pass, False)) {
413                 x_fprintf(x_stdout, "OK\n");
414         } else {
415                 x_fprintf(x_stdout, "ERR\n");
416         }
417 }
418
419 static void offer_gss_spnego_mechs(void) {
420
421         DATA_BLOB token;
422         SPNEGO_DATA spnego;
423         ssize_t len;
424         char *reply_base64;
425
426         pstring principal;
427         pstring myname_lower;
428
429         ZERO_STRUCT(spnego);
430
431         pstrcpy(myname_lower, global_myname());
432         strlower_m(myname_lower);
433
434         pstr_sprintf(principal, "%s$@%s", myname_lower, lp_realm());
435
436         /* Server negTokenInit (mech offerings) */
437         spnego.type = SPNEGO_NEG_TOKEN_INIT;
438         spnego.negTokenInit.mechTypes = smb_xmalloc(sizeof(char *) * 3);
439 #ifdef HAVE_KRB5
440         spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
441         spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
442         spnego.negTokenInit.mechTypes[2] = NULL;
443 #else
444         spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
445         spnego.negTokenInit.mechTypes[1] = NULL;
446 #endif
447
448
449         spnego.negTokenInit.mechListMIC = data_blob(principal,
450                                                     strlen(principal));
451
452         len = write_spnego_data(&token, &spnego);
453         free_spnego_data(&spnego);
454
455         if (len == -1) {
456                 DEBUG(1, ("Could not write SPNEGO data blob\n"));
457                 x_fprintf(x_stdout, "BH\n");
458                 return;
459         }
460
461         reply_base64 = base64_encode_data_blob(token);
462         x_fprintf(x_stdout, "TT %s *\n", reply_base64);
463
464         SAFE_FREE(reply_base64);
465         data_blob_free(&token);
466         DEBUG(10, ("sent SPNEGO negTokenInit\n"));
467         return;
468 }
469
470 static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode, 
471                                       char *buf, int length) 
472 {
473         static NTLMSSP_STATE *ntlmssp_state = NULL;
474         SPNEGO_DATA request, response;
475         DATA_BLOB token;
476         NTSTATUS status;
477         ssize_t len;
478
479         char *user = NULL;
480         char *domain = NULL;
481
482         const char *reply_code;
483         char       *reply_base64;
484         pstring     reply_argument;
485
486         if (strlen(buf) < 2) {
487
488                 if (ntlmssp_state != NULL) {
489                         DEBUG(1, ("Request for initial SPNEGO request where "
490                                   "we already have a state\n"));
491                         x_fprintf(x_stdout, "BH\n");
492                         return;
493                 }
494
495                 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
496                 x_fprintf(x_stdout, "BH\n");
497                 return;
498         }
499
500         if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
501
502                 /* Initial request, get the negTokenInit offering
503                    mechanisms */
504
505                 offer_gss_spnego_mechs();
506                 return;
507         }
508
509         /* All subsequent requests are "KK" (Knock, Knock ;)) and have
510            a blob. This might be negTokenInit or negTokenTarg */
511
512         if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
513                 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
514                 x_fprintf(x_stdout, "BH\n");
515                 return;
516         }
517
518         token = base64_decode_data_blob(buf + 3);
519         len = read_spnego_data(token, &request);
520         data_blob_free(&token);
521
522         if (len == -1) {
523                 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
524                 x_fprintf(x_stdout, "BH\n");
525                 return;
526         }
527
528         if (request.type == SPNEGO_NEG_TOKEN_INIT) {
529
530                 /* Second request from Client. This is where the
531                    client offers its mechanism to use. */
532
533                 if ( (request.negTokenInit.mechTypes == NULL) ||
534                      (request.negTokenInit.mechTypes[0] == NULL) ) {
535                         DEBUG(1, ("Client did not offer any mechanism"));
536                         x_fprintf(x_stdout, "BH\n");
537                         return;
538                 }
539
540                 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
541
542                         if ( request.negTokenInit.mechToken.data == NULL ) {
543                                 DEBUG(1, ("Client did not provide  NTLMSSP data\n"));
544                                 x_fprintf(x_stdout, "BH\n");
545                                 return;
546                         }
547
548                         if ( ntlmssp_state != NULL ) {
549                                 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
550                                           "already got one\n"));
551                                 x_fprintf(x_stdout, "BH\n");
552                                 ntlmssp_end(&ntlmssp_state);
553                                 return;
554                         }
555
556                         ntlmssp_server_start(&ntlmssp_state);
557                         ntlmssp_state->check_password = winbind_pw_check;
558                         ntlmssp_state->get_domain = get_winbind_domain;
559                         ntlmssp_state->get_global_myname = get_winbind_netbios_name;
560
561                         DEBUG(10, ("got NTLMSSP packet:\n"));
562                         dump_data(10, (const char *)request.negTokenInit.mechToken.data,
563                                   request.negTokenInit.mechToken.length);
564
565                         response.type = SPNEGO_NEG_TOKEN_TARG;
566                         response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
567                         response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
568
569                         status = ntlmssp_update(ntlmssp_state,
570                                                        request.negTokenInit.mechToken,
571                                                        &response.negTokenTarg.responseToken);
572                 }
573
574 #ifdef HAVE_KRB5
575                 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
576
577                         char *principal;
578                         DATA_BLOB auth_data;
579                         DATA_BLOB ap_rep;
580                         DATA_BLOB session_key;
581
582                         if ( request.negTokenInit.mechToken.data == NULL ) {
583                                 DEBUG(1, ("Client did not provide Kerberos data\n"));
584                                 x_fprintf(x_stdout, "BH\n");
585                                 return;
586                         }
587
588                         response.type = SPNEGO_NEG_TOKEN_TARG;
589                         response.negTokenTarg.supportedMech = strdup(OID_KERBEROS5_OLD);
590                         response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
591                         response.negTokenTarg.responseToken = data_blob(NULL, 0);
592
593                         status = ads_verify_ticket(lp_realm(),
594                                                    &request.negTokenInit.mechToken,
595                                                    &principal, &auth_data, &ap_rep,
596                                                    &session_key);
597
598                         /* Now in "principal" we have the name we are
599                            authenticated as. */
600
601                         if (NT_STATUS_IS_OK(status)) {
602
603                                 domain = strchr(principal, '@');
604
605                                 if (domain == NULL) {
606                                         DEBUG(1, ("Did not get a valid principal "
607                                                   "from ads_verify_ticket\n"));
608                                         x_fprintf(x_stdout, "BH\n");
609                                         return;
610                                 }
611
612                                 *domain++ = '\0';
613                                 domain = strdup(domain);
614                                 user = strdup(principal);
615
616                                 data_blob_free(&ap_rep);
617                                 data_blob_free(&auth_data);
618
619                                 SAFE_FREE(principal);
620                         }
621                 }
622 #endif
623
624         } else {
625
626                 if ( (request.negTokenTarg.supportedMech == NULL) ||
627                      ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
628                         /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
629                            is the only one we support that sends this stuff */
630                         DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
631                                   request.negTokenTarg.supportedMech));
632                         x_fprintf(x_stdout, "BH\n");
633                         return;
634                 }
635
636                 if (request.negTokenTarg.responseToken.data == NULL) {
637                         DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
638                         x_fprintf(x_stdout, "BH\n");
639                         return;
640                 }
641
642                 status = ntlmssp_update(ntlmssp_state,
643                                                request.negTokenTarg.responseToken,
644                                                &response.negTokenTarg.responseToken);
645
646                 response.type = SPNEGO_NEG_TOKEN_TARG;
647                 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
648                 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
649
650                 if (NT_STATUS_IS_OK(status)) {
651                         user = strdup(ntlmssp_state->user);
652                         domain = strdup(ntlmssp_state->domain);
653                         ntlmssp_end(&ntlmssp_state);
654                 }
655         }
656
657         free_spnego_data(&request);
658
659         if (NT_STATUS_IS_OK(status)) {
660                 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
661                 reply_code = "AF";
662                 pstr_sprintf(reply_argument, "%s\\%s", domain, user);
663         } else if (NT_STATUS_EQUAL(status,
664                                    NT_STATUS_MORE_PROCESSING_REQUIRED)) {
665                 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
666                 reply_code = "TT";
667                 pstr_sprintf(reply_argument, "*");
668         } else {
669                 response.negTokenTarg.negResult = SPNEGO_REJECT;
670                 reply_code = "NA";
671                 pstrcpy(reply_argument, nt_errstr(status));
672         }
673
674         SAFE_FREE(user);
675         SAFE_FREE(domain);
676
677         len = write_spnego_data(&token, &response);
678         free_spnego_data(&response);
679
680         if (len == -1) {
681                 DEBUG(1, ("Could not write SPNEGO data blob\n"));
682                 x_fprintf(x_stdout, "BH\n");
683                 return;
684         }
685
686         reply_base64 = base64_encode_data_blob(token);
687
688         x_fprintf(x_stdout, "%s %s %s\n",
689                   reply_code, reply_base64, reply_argument);
690
691         SAFE_FREE(reply_base64);
692         data_blob_free(&token);
693
694         return;
695 }
696
697 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
698
699 static BOOL manage_client_ntlmssp_init(SPNEGO_DATA spnego)
700 {
701         NTSTATUS status;
702         DATA_BLOB null_blob = data_blob(NULL, 0);
703         DATA_BLOB to_server;
704         char *to_server_base64;
705         const char *my_mechs[] = {OID_NTLMSSP, NULL};
706
707         DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
708
709         if (client_ntlmssp_state != NULL) {
710                 DEBUG(1, ("Request for initial SPNEGO request where "
711                           "we already have a state\n"));
712                 return False;
713         }
714
715         if ( (opt_username == NULL) || (opt_domain == NULL) ) {
716                 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
717                 return False;
718         }
719
720         if (opt_password == NULL) {
721
722                 /* Request a password from the calling process.  After
723                    sending it, the calling process should retry with
724                    the negTokenInit. */
725
726                 DEBUG(10, ("Requesting password\n"));
727                 x_fprintf(x_stdout, "PW\n");
728                 return True;
729         }
730
731         status = ntlmssp_client_start(&client_ntlmssp_state);
732
733         if (!NT_STATUS_IS_OK(status)) {
734                 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
735                           nt_errstr(status)));
736                 ntlmssp_end(&client_ntlmssp_state);
737                 return False;
738         }
739
740         status = ntlmssp_set_username(client_ntlmssp_state, opt_username);
741
742         if (!NT_STATUS_IS_OK(status)) {
743                 DEBUG(1, ("Could not set username: %s\n",
744                           nt_errstr(status)));
745                 ntlmssp_end(&client_ntlmssp_state);
746                 return False;
747         }
748
749         status = ntlmssp_set_domain(client_ntlmssp_state, opt_domain);
750
751         if (!NT_STATUS_IS_OK(status)) {
752                 DEBUG(1, ("Could not set domain: %s\n",
753                           nt_errstr(status)));
754                 ntlmssp_end(&client_ntlmssp_state);
755                 return False;
756         }
757
758         status = ntlmssp_set_password(client_ntlmssp_state, opt_password);
759         
760         if (!NT_STATUS_IS_OK(status)) {
761                 DEBUG(1, ("Could not set password: %s\n",
762                           nt_errstr(status)));
763                 ntlmssp_end(&client_ntlmssp_state);
764                 return False;
765         }
766
767         spnego.type = SPNEGO_NEG_TOKEN_INIT;
768         spnego.negTokenInit.mechTypes = my_mechs;
769         spnego.negTokenInit.reqFlags = 0;
770         spnego.negTokenInit.mechListMIC = null_blob;
771
772         status = ntlmssp_update(client_ntlmssp_state, null_blob,
773                                        &spnego.negTokenInit.mechToken);
774
775         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
776                 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
777                           nt_errstr(status)));
778                 ntlmssp_end(&client_ntlmssp_state);
779                 return False;
780         }
781
782         write_spnego_data(&to_server, &spnego);
783         data_blob_free(&spnego.negTokenInit.mechToken);
784
785         to_server_base64 = base64_encode_data_blob(to_server);
786         data_blob_free(&to_server);
787         x_fprintf(x_stdout, "KK %s\n", to_server_base64);
788         SAFE_FREE(to_server_base64);
789         return True;
790 }
791
792 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
793 {
794         NTSTATUS status;
795         DATA_BLOB null_blob = data_blob(NULL, 0);
796         DATA_BLOB request;
797         DATA_BLOB to_server;
798         char *to_server_base64;
799
800         DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
801
802         if (client_ntlmssp_state == NULL) {
803                 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
804                 x_fprintf(x_stdout, "BH\n");
805                 ntlmssp_end(&client_ntlmssp_state);
806                 return;
807         }
808
809         if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
810                 x_fprintf(x_stdout, "NA\n");
811                 ntlmssp_end(&client_ntlmssp_state);
812                 return;
813         }
814
815         if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
816                 x_fprintf(x_stdout, "AF\n");
817                 ntlmssp_end(&client_ntlmssp_state);
818                 return;
819         }
820
821         status = ntlmssp_update(client_ntlmssp_state,
822                                        spnego.negTokenTarg.responseToken,
823                                        &request);
824                 
825         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
826                 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
827                           "ntlmssp_client_update, got: %s\n",
828                           nt_errstr(status)));
829                 x_fprintf(x_stdout, "BH\n");
830                 data_blob_free(&request);
831                 ntlmssp_end(&client_ntlmssp_state);
832                 return;
833         }
834
835         spnego.type = SPNEGO_NEG_TOKEN_TARG;
836         spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
837         spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
838         spnego.negTokenTarg.responseToken = request;
839         spnego.negTokenTarg.mechListMIC = null_blob;
840         
841         write_spnego_data(&to_server, &spnego);
842         data_blob_free(&request);
843
844         to_server_base64 = base64_encode_data_blob(to_server);
845         data_blob_free(&to_server);
846         x_fprintf(x_stdout, "KK %s\n", to_server_base64);
847         SAFE_FREE(to_server_base64);
848         return;
849 }
850
851 #ifdef HAVE_KRB5
852
853 static BOOL manage_client_krb5_init(SPNEGO_DATA spnego)
854 {
855         char *principal;
856         DATA_BLOB tkt, to_server;
857         DATA_BLOB session_key_krb5;
858         SPNEGO_DATA reply;
859         char *reply_base64;
860         
861         const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
862         ssize_t len;
863
864         if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
865              (spnego.negTokenInit.mechListMIC.length == 0) ) {
866                 DEBUG(1, ("Did not get a principal for krb5\n"));
867                 return False;
868         }
869
870         principal = malloc(spnego.negTokenInit.mechListMIC.length+1);
871
872         if (principal == NULL) {
873                 DEBUG(1, ("Could not malloc principal\n"));
874                 return False;
875         }
876
877         memcpy(principal, spnego.negTokenInit.mechListMIC.data,
878                spnego.negTokenInit.mechListMIC.length);
879         principal[spnego.negTokenInit.mechListMIC.length] = '\0';
880
881         tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
882
883         if (tkt.data == NULL) {
884
885                 pstring user;
886
887                 /* Let's try to first get the TGT, for that we need a
888                    password. */
889
890                 if (opt_password == NULL) {
891                         DEBUG(10, ("Requesting password\n"));
892                         x_fprintf(x_stdout, "PW\n");
893                         return True;
894                 }
895
896                 pstr_sprintf(user, "%s@%s", opt_username, opt_domain);
897
898                 if (kerberos_kinit_password(user, opt_password, 0) != 0) {
899                         DEBUG(10, ("Requesting TGT failed\n"));
900                         x_fprintf(x_stdout, "NA\n");
901                         return True;
902                 }
903
904                 tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
905         }
906
907         data_blob_free(&session_key_krb5);
908
909         ZERO_STRUCT(reply);
910
911         reply.type = SPNEGO_NEG_TOKEN_INIT;
912         reply.negTokenInit.mechTypes = my_mechs;
913         reply.negTokenInit.reqFlags = 0;
914         reply.negTokenInit.mechToken = tkt;
915         reply.negTokenInit.mechListMIC = data_blob(NULL, 0);
916
917         len = write_spnego_data(&to_server, &reply);
918         data_blob_free(&tkt);
919
920         if (len == -1) {
921                 DEBUG(1, ("Could not write SPNEGO data blob\n"));
922                 return False;
923         }
924
925         reply_base64 = base64_encode_data_blob(to_server);
926         x_fprintf(x_stdout, "KK %s *\n", reply_base64);
927
928         SAFE_FREE(reply_base64);
929         data_blob_free(&to_server);
930         DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
931         return True;
932 }
933
934 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
935 {
936         switch (spnego.negTokenTarg.negResult) {
937         case SPNEGO_ACCEPT_INCOMPLETE:
938                 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
939                 x_fprintf(x_stdout, "BH\n");
940                 break;
941         case SPNEGO_ACCEPT_COMPLETED:
942                 DEBUG(10, ("Accept completed\n"));
943                 x_fprintf(x_stdout, "AF\n");
944                 break;
945         case SPNEGO_REJECT:
946                 DEBUG(10, ("Rejected\n"));
947                 x_fprintf(x_stdout, "NA\n");
948                 break;
949         default:
950                 DEBUG(1, ("Got an invalid negTokenTarg\n"));
951                 x_fprintf(x_stdout, "AF\n");
952         }
953 }
954
955 #endif
956
957 static void manage_gss_spnego_client_request(enum stdio_helper_mode stdio_helper_mode, 
958                                              char *buf, int length) 
959 {
960         DATA_BLOB request;
961         SPNEGO_DATA spnego;
962         ssize_t len;
963
964         if (strlen(buf) <= 3) {
965                 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
966                 x_fprintf(x_stdout, "BH\n");
967                 return;
968         }
969
970         request = base64_decode_data_blob(buf+3);
971
972         if (strncmp(buf, "PW ", 3) == 0) {
973
974                 /* We asked for a password and obviously got it :-) */
975
976                 opt_password = strndup((const char *)request.data, request.length);
977
978                 if (opt_password == NULL) {
979                         DEBUG(1, ("Out of memory\n"));
980                         x_fprintf(x_stdout, "BH\n");
981                         data_blob_free(&request);
982                         return;
983                 }
984
985                 x_fprintf(x_stdout, "OK\n");
986                 data_blob_free(&request);
987                 return;
988         }
989
990         if ( (strncmp(buf, "TT ", 3) != 0) &&
991              (strncmp(buf, "AF ", 3) != 0) &&
992              (strncmp(buf, "NA ", 3) != 0) ) {
993                 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
994                 x_fprintf(x_stdout, "BH\n");
995                 data_blob_free(&request);
996                 return;
997         }
998
999         /* So we got a server challenge to generate a SPNEGO
1000            client-to-server request... */
1001
1002         len = read_spnego_data(request, &spnego);
1003         data_blob_free(&request);
1004
1005         if (len == -1) {
1006                 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1007                 x_fprintf(x_stdout, "BH\n");
1008                 return;
1009         }
1010
1011         if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1012
1013                 /* The server offers a list of mechanisms */
1014
1015                 const char **mechType = spnego.negTokenInit.mechTypes;
1016
1017                 while (*mechType != NULL) {
1018
1019 #ifdef HAVE_KRB5
1020                         if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1021                              (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1022                                 if (manage_client_krb5_init(spnego))
1023                                         goto out;
1024                         }
1025 #endif
1026
1027                         if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1028                                 if (manage_client_ntlmssp_init(spnego))
1029                                         goto out;
1030                         }
1031
1032                         mechType++;
1033                 }
1034
1035                 DEBUG(1, ("Server offered no compatible mechanism\n"));
1036                 x_fprintf(x_stdout, "BH\n");
1037                 return;
1038         }
1039
1040         if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1041
1042                 if (spnego.negTokenTarg.supportedMech == NULL) {
1043                         /* On accept/reject Windows does not send the
1044                            mechanism anymore. Handle that here and
1045                            shut down the mechanisms. */
1046
1047                         switch (spnego.negTokenTarg.negResult) {
1048                         case SPNEGO_ACCEPT_COMPLETED:
1049                                 x_fprintf(x_stdout, "AF\n");
1050                                 break;
1051                         case SPNEGO_REJECT:
1052                                 x_fprintf(x_stdout, "NA\n");
1053                                 break;
1054                         default:
1055                                 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1056                                           "unknown negResult: %d\n",
1057                                           spnego.negTokenTarg.negResult));
1058                                 x_fprintf(x_stdout, "BH\n");
1059                         }
1060
1061                         ntlmssp_end(&client_ntlmssp_state);
1062                         goto out;
1063                 }
1064
1065                 if (strcmp(spnego.negTokenTarg.supportedMech,
1066                            OID_NTLMSSP) == 0) {
1067                         manage_client_ntlmssp_targ(spnego);
1068                         goto out;
1069                 }
1070
1071 #if HAVE_KRB5
1072                 if (strcmp(spnego.negTokenTarg.supportedMech,
1073                            OID_KERBEROS5_OLD) == 0) {
1074                         manage_client_krb5_targ(spnego);
1075                         goto out;
1076                 }
1077 #endif
1078
1079         }
1080
1081         DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1082         x_fprintf(x_stdout, "BH\n");
1083         return;
1084
1085  out:
1086         free_spnego_data(&spnego);
1087         return;
1088 }
1089
1090 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn) 
1091 {
1092         char buf[SQUID_BUFFER_SIZE+1];
1093         int length;
1094         char *c;
1095         static BOOL err;
1096
1097         /* this is not a typo - x_fgets doesn't work too well under squid */
1098         if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1099                 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
1100                           strerror(ferror(stdin))));
1101                 exit(1);    /* BIIG buffer */
1102         }
1103     
1104         c=memchr(buf,'\n',sizeof(buf)-1);
1105         if (c) {
1106                 *c = '\0';
1107                 length = c-buf;
1108         } else {
1109                 err = 1;
1110                 return;
1111         }
1112         if (err) {
1113                 DEBUG(2, ("Oversized message\n"));
1114                 x_fprintf(x_stderr, "ERR\n");
1115                 err = 0;
1116                 return;
1117         }
1118
1119         DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
1120
1121         if (buf[0] == '\0') {
1122                 DEBUG(2, ("Invalid Request\n"));
1123                 x_fprintf(x_stderr, "ERR\n");
1124                 return;
1125         }
1126         
1127         fn(helper_mode, buf, length);
1128 }
1129
1130
1131 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
1132         /* initialize FDescs */
1133         x_setbuf(x_stdout, NULL);
1134         x_setbuf(x_stderr, NULL);
1135         while(1) {
1136                 manage_squid_request(stdio_mode, fn);
1137         }
1138 }
1139
1140
1141 /* Authenticate a user with a challenge/response */
1142
1143 static BOOL check_auth_crap(void)
1144 {
1145         NTSTATUS nt_status;
1146         uint32 flags = 0;
1147         char lm_key[8];
1148         char nt_key[16];
1149         char *hex_lm_key;
1150         char *hex_nt_key;
1151         char *error_string;
1152         static uint8 zeros[16];
1153
1154         x_setbuf(x_stdout, NULL);
1155
1156         if (request_lm_key) 
1157                 flags |= WBFLAG_PAM_LMKEY;
1158
1159         if (request_nt_key) 
1160                 flags |= WBFLAG_PAM_NTKEY;
1161
1162         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1163                                               opt_workstation,
1164                                               &opt_challenge, 
1165                                               &opt_lm_response, 
1166                                               &opt_nt_response, 
1167                                               flags,
1168                                               (unsigned char *)lm_key, 
1169                                               (unsigned char *)nt_key, 
1170                                               &error_string);
1171
1172         if (!NT_STATUS_IS_OK(nt_status)) {
1173                 x_fprintf(x_stdout, "%s (0x%x)\n", 
1174                           error_string,
1175                           NT_STATUS_V(nt_status));
1176                 SAFE_FREE(error_string);
1177                 return False;
1178         }
1179
1180         if (request_lm_key 
1181             && (memcmp(zeros, lm_key, 
1182                        sizeof(lm_key)) != 0)) {
1183                 hex_encode((const unsigned char *)lm_key,
1184                            sizeof(lm_key),
1185                            &hex_lm_key);
1186                 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
1187                 SAFE_FREE(hex_lm_key);
1188         }
1189         if (request_nt_key 
1190             && (memcmp(zeros, nt_key, 
1191                        sizeof(nt_key)) != 0)) {
1192                 hex_encode((const unsigned char *)nt_key, 
1193                            sizeof(nt_key), 
1194                            &hex_nt_key);
1195                 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_nt_key);
1196                 SAFE_FREE(hex_nt_key);
1197         }
1198
1199         return True;
1200 }
1201
1202 /* 
1203    Authenticate a user with a challenge/response, checking session key
1204    and valid authentication types
1205 */
1206
1207 static DATA_BLOB get_challenge(void) 
1208 {
1209         static DATA_BLOB chal;
1210         if (opt_challenge.length)
1211                 return opt_challenge;
1212         
1213         chal = data_blob(NULL, 8);
1214
1215         generate_random_buffer(chal.data, chal.length, False);
1216         return chal;
1217 }
1218
1219 /* 
1220  * Test LM authentication, no NT response supplied
1221  */
1222
1223 static BOOL test_lm(void) 
1224 {
1225         NTSTATUS nt_status;
1226         uint32 flags = 0;
1227         DATA_BLOB lm_response = data_blob(NULL, 24);
1228
1229         uchar lm_key[8];
1230         uchar nt_key[16];
1231         uchar lm_hash[16];
1232         DATA_BLOB chall = get_challenge();
1233         char *error_string;
1234         
1235         ZERO_STRUCT(lm_key);
1236         ZERO_STRUCT(nt_key);
1237
1238         flags |= WBFLAG_PAM_LMKEY;
1239         flags |= WBFLAG_PAM_NTKEY;
1240
1241         SMBencrypt(opt_password, chall.data, lm_response.data);
1242         E_deshash(opt_password, lm_hash); 
1243
1244         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, opt_workstation,
1245                                               &chall,
1246                                               &lm_response,
1247                                               NULL,
1248                                               flags,
1249                                               lm_key, 
1250                                               nt_key,
1251                                               &error_string);
1252         
1253         data_blob_free(&lm_response);
1254
1255         if (!NT_STATUS_IS_OK(nt_status)) {
1256                 d_printf("%s (0x%x)\n", 
1257                          error_string,
1258                          NT_STATUS_V(nt_status));
1259                 return False;
1260         }
1261
1262         if (memcmp(lm_hash, lm_key, 
1263                    sizeof(lm_key)) != 0) {
1264                 DEBUG(1, ("LM Key does not match expectations!\n"));
1265                 DEBUG(1, ("lm_key:\n"));
1266                 dump_data(1, (const char *)lm_key, 8);
1267                 DEBUG(1, ("expected:\n"));
1268                 dump_data(1, (const char *)lm_hash, 8);
1269         }
1270         if (memcmp(lm_hash, nt_key, 8) != 0) {
1271                 DEBUG(1, ("Session Key (first 8, lm hash) does not match expectations!\n"));
1272                 DEBUG(1, ("nt_key:\n"));
1273                 dump_data(1, (const char *)nt_key, 8);
1274                 DEBUG(1, ("expected:\n"));
1275                 dump_data(1, (const char *)lm_hash, 8);
1276         }
1277         return True;
1278 }
1279
1280 /* 
1281  * Test the normal 'LM and NTLM' combination
1282  */
1283
1284 static BOOL test_lm_ntlm(void) 
1285 {
1286         BOOL pass = True;
1287         NTSTATUS nt_status;
1288         uint32 flags = 0;
1289         DATA_BLOB lm_response = data_blob(NULL, 24);
1290         DATA_BLOB nt_response = data_blob(NULL, 24);
1291         DATA_BLOB session_key = data_blob(NULL, 16);
1292
1293         uchar lm_key[8];
1294         uchar nt_key[16];
1295         uchar lm_hash[16];
1296         uchar nt_hash[16];
1297         DATA_BLOB chall = get_challenge();
1298         char *error_string;
1299         
1300         ZERO_STRUCT(lm_key);
1301         ZERO_STRUCT(nt_key);
1302
1303         flags |= WBFLAG_PAM_LMKEY;
1304         flags |= WBFLAG_PAM_NTKEY;
1305
1306         SMBencrypt(opt_password,chall.data,lm_response.data);
1307         E_deshash(opt_password, lm_hash); 
1308
1309         SMBNTencrypt(opt_password,chall.data,nt_response.data);
1310
1311         E_md4hash(opt_password, nt_hash);
1312         SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1313
1314         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1315                                               opt_workstation,
1316                                               &chall,
1317                                               &lm_response,
1318                                               &nt_response,
1319                                               flags,
1320                                               lm_key, 
1321                                               nt_key,
1322                                               &error_string);
1323         
1324         data_blob_free(&lm_response);
1325
1326         if (!NT_STATUS_IS_OK(nt_status)) {
1327                 d_printf("%s (0x%x)\n", 
1328                          error_string,
1329                          NT_STATUS_V(nt_status));
1330                 SAFE_FREE(error_string);
1331                 return False;
1332         }
1333
1334         if (memcmp(lm_hash, lm_key, 
1335                    sizeof(lm_key)) != 0) {
1336                 DEBUG(1, ("LM Key does not match expectations!\n"));
1337                 DEBUG(1, ("lm_key:\n"));
1338                 dump_data(1, (const char *)lm_key, 8);
1339                 DEBUG(1, ("expected:\n"));
1340                 dump_data(1, (const char *)lm_hash, 8);
1341                 pass = False;
1342         }
1343         if (memcmp(session_key.data, nt_key, 
1344                    sizeof(nt_key)) != 0) {
1345                 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1346                 DEBUG(1, ("nt_key:\n"));
1347                 dump_data(1, (const char *)nt_key, 16);
1348                 DEBUG(1, ("expected:\n"));
1349                 dump_data(1, (const char *)session_key.data, session_key.length);
1350                 pass = False;
1351         }
1352         return pass;
1353 }
1354
1355 /* 
1356  * Test the NTLM response only, no LM.
1357  */
1358
1359 static BOOL test_ntlm(void) 
1360 {
1361         BOOL pass = True;
1362         NTSTATUS nt_status;
1363         uint32 flags = 0;
1364         DATA_BLOB nt_response = data_blob(NULL, 24);
1365         DATA_BLOB session_key = data_blob(NULL, 16);
1366
1367         char lm_key[8];
1368         char nt_key[16];
1369         char lm_hash[16];
1370         char nt_hash[16];
1371         DATA_BLOB chall = get_challenge();
1372         char *error_string;
1373         
1374         ZERO_STRUCT(lm_key);
1375         ZERO_STRUCT(nt_key);
1376
1377         flags |= WBFLAG_PAM_LMKEY;
1378         flags |= WBFLAG_PAM_NTKEY;
1379
1380         SMBNTencrypt(opt_password,chall.data,nt_response.data);
1381         E_md4hash(opt_password, (unsigned char *)nt_hash);
1382         SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1383
1384         E_deshash(opt_password, (unsigned char *)lm_hash); 
1385
1386         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1387                                               opt_workstation,
1388                                               &chall,
1389                                               NULL,
1390                                               &nt_response,
1391                                               flags,
1392                                               (unsigned char *)lm_key,
1393                                               (unsigned char *)nt_key,
1394                                               &error_string);
1395         
1396         data_blob_free(&nt_response);
1397
1398         if (!NT_STATUS_IS_OK(nt_status)) {
1399                 d_printf("%s (0x%x)\n", 
1400                          error_string,
1401                          NT_STATUS_V(nt_status));
1402                 SAFE_FREE(error_string);
1403                 return False;
1404         }
1405
1406         if (memcmp(lm_hash, lm_key, 
1407                    sizeof(lm_key)) != 0) {
1408                 DEBUG(1, ("LM Key does not match expectations!\n"));
1409                 DEBUG(1, ("lm_key:\n"));
1410                 dump_data(1, lm_key, 8);
1411                 DEBUG(1, ("expected:\n"));
1412                 dump_data(1, lm_hash, 8);
1413                 pass = False;
1414         }
1415         if (memcmp(session_key.data, nt_key, 
1416                    sizeof(nt_key)) != 0) {
1417                 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1418                 DEBUG(1, ("nt_key:\n"));
1419                 dump_data(1, nt_key, 16);
1420                 DEBUG(1, ("expected:\n"));
1421                 dump_data(1, (const char *)session_key.data, session_key.length);
1422                 pass = False;
1423         }
1424         return pass;
1425 }
1426
1427 /* 
1428  * Test the NTLM response only, but in the LM field.
1429  */
1430
1431 static BOOL test_ntlm_in_lm(void) 
1432 {
1433         BOOL pass = True;
1434         NTSTATUS nt_status;
1435         uint32 flags = 0;
1436         DATA_BLOB nt_response = data_blob(NULL, 24);
1437
1438         uchar lm_key[8];
1439         uchar lm_hash[16];
1440         uchar nt_key[16];
1441         DATA_BLOB chall = get_challenge();
1442         char *error_string;
1443         
1444         ZERO_STRUCT(nt_key);
1445
1446         flags |= WBFLAG_PAM_LMKEY;
1447         flags |= WBFLAG_PAM_NTKEY;
1448
1449         SMBNTencrypt(opt_password,chall.data,nt_response.data);
1450
1451         E_deshash(opt_password, lm_hash); 
1452
1453         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1454                                               opt_workstation,
1455                                               &chall,
1456                                               &nt_response,
1457                                               NULL,
1458                                               flags,
1459                                               lm_key,
1460                                               nt_key,
1461                                               &error_string);
1462         
1463         data_blob_free(&nt_response);
1464
1465         if (!NT_STATUS_IS_OK(nt_status)) {
1466                 d_printf("%s (0x%x)\n", 
1467                          error_string,
1468                          NT_STATUS_V(nt_status));
1469                 SAFE_FREE(error_string);
1470                 return False;
1471         }
1472
1473         if (memcmp(lm_hash, lm_key, 
1474                    sizeof(lm_key)) != 0) {
1475                 DEBUG(1, ("LM Key does not match expectations!\n"));
1476                 DEBUG(1, ("lm_key:\n"));
1477                 dump_data(1, (const char *)lm_key, 8);
1478                 DEBUG(1, ("expected:\n"));
1479                 dump_data(1, (const char *)lm_hash, 8);
1480                 pass = False;
1481         }
1482         if (memcmp(lm_hash, nt_key, 8) != 0) {
1483                 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
1484                 DEBUG(1, ("nt_key:\n"));
1485                 dump_data(1, (const char *)nt_key, 16);
1486                 DEBUG(1, ("expected:\n"));
1487                 dump_data(1, (const char *)lm_hash, 8);
1488                 pass = False;
1489         }
1490         return pass;
1491 }
1492
1493 /* 
1494  * Test the NTLM response only, but in the both the NT and LM fields.
1495  */
1496
1497 static BOOL test_ntlm_in_both(void) 
1498 {
1499         BOOL pass = True;
1500         NTSTATUS nt_status;
1501         uint32 flags = 0;
1502         DATA_BLOB nt_response = data_blob(NULL, 24);
1503         DATA_BLOB session_key = data_blob(NULL, 16);
1504
1505         char lm_key[8];
1506         char lm_hash[16];
1507         char nt_key[16];
1508         char nt_hash[16];
1509         DATA_BLOB chall = get_challenge();
1510         char *error_string;
1511         
1512         ZERO_STRUCT(lm_key);
1513         ZERO_STRUCT(nt_key);
1514
1515         flags |= WBFLAG_PAM_LMKEY;
1516         flags |= WBFLAG_PAM_NTKEY;
1517
1518         SMBNTencrypt(opt_password,chall.data,nt_response.data);
1519         E_md4hash(opt_password, (unsigned char *)nt_hash);
1520         SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1521
1522         E_deshash(opt_password, (unsigned char *)lm_hash); 
1523
1524         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1525                                               opt_workstation,
1526                                               &chall,
1527                                               &nt_response,
1528                                               &nt_response,
1529                                               flags,
1530                                               (unsigned char *)lm_key,
1531                                               (unsigned char *)nt_key,
1532                                               &error_string);
1533         
1534         data_blob_free(&nt_response);
1535
1536         if (!NT_STATUS_IS_OK(nt_status)) {
1537                 d_printf("%s (0x%x)\n", 
1538                          error_string,
1539                          NT_STATUS_V(nt_status));
1540                 SAFE_FREE(error_string);
1541                 return False;
1542         }
1543
1544         if (memcmp(lm_hash, lm_key, 
1545                    sizeof(lm_key)) != 0) {
1546                 DEBUG(1, ("LM Key does not match expectations!\n"));
1547                 DEBUG(1, ("lm_key:\n"));
1548                 dump_data(1, lm_key, 8);
1549                 DEBUG(1, ("expected:\n"));
1550                 dump_data(1, lm_hash, 8);
1551                 pass = False;
1552         }
1553         if (memcmp(session_key.data, nt_key, 
1554                    sizeof(nt_key)) != 0) {
1555                 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1556                 DEBUG(1, ("nt_key:\n"));
1557                 dump_data(1, nt_key, 16);
1558                 DEBUG(1, ("expected:\n"));
1559                 dump_data(1, (const char *)session_key.data, session_key.length);
1560                 pass = False;
1561         }
1562
1563
1564         return pass;
1565 }
1566
1567 /* 
1568  * Test the NTLMv2 response only
1569  */
1570
1571 static BOOL test_ntlmv2(void) 
1572 {
1573         BOOL pass = True;
1574         NTSTATUS nt_status;
1575         uint32 flags = 0;
1576         DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1577         DATA_BLOB nt_session_key = data_blob(NULL, 0);
1578         DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1579
1580         uchar nt_key[16];
1581         DATA_BLOB chall = get_challenge();
1582         char *error_string;
1583
1584         ZERO_STRUCT(nt_key);
1585         
1586         flags |= WBFLAG_PAM_NTKEY;
1587
1588         if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1589                               &names_blob,
1590                               NULL, &ntlmv2_response, 
1591                               &nt_session_key)) {
1592                 data_blob_free(&names_blob);
1593                 return False;
1594         }
1595         data_blob_free(&names_blob);
1596
1597         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1598                                               opt_workstation,
1599                                               &chall,
1600                                               NULL, 
1601                                               &ntlmv2_response,
1602                                               flags,
1603                                               NULL, 
1604                                               nt_key,
1605                                               &error_string);
1606         
1607         data_blob_free(&ntlmv2_response);
1608
1609         if (!NT_STATUS_IS_OK(nt_status)) {
1610                 d_printf("%s (0x%x)\n", 
1611                          error_string,
1612                          NT_STATUS_V(nt_status));
1613                 SAFE_FREE(error_string);
1614                 return False;
1615         }
1616
1617         if (memcmp(nt_session_key.data, nt_key, 
1618                    sizeof(nt_key)) != 0) {
1619                 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1620                 DEBUG(1, ("nt_key:\n"));
1621                 dump_data(1, (const char *)nt_key, 16);
1622                 DEBUG(1, ("expected:\n"));
1623                 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1624                 pass = False;
1625         }
1626         return pass;
1627 }
1628
1629 /* 
1630  * Test the NTLMv2 and LMv2 responses
1631  */
1632
1633 static BOOL test_lmv2_ntlmv2(void) 
1634 {
1635         BOOL pass = True;
1636         NTSTATUS nt_status;
1637         uint32 flags = 0;
1638         DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1639         DATA_BLOB lmv2_response = data_blob(NULL, 0);
1640         DATA_BLOB nt_session_key = data_blob(NULL, 0);
1641         DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1642
1643         uchar nt_key[16];
1644         DATA_BLOB chall = get_challenge();
1645         char *error_string;
1646
1647         ZERO_STRUCT(nt_key);
1648         
1649         flags |= WBFLAG_PAM_NTKEY;
1650
1651         if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1652                               &names_blob,
1653                               &lmv2_response, &ntlmv2_response, 
1654                               &nt_session_key)) {
1655                 data_blob_free(&names_blob);
1656                 return False;
1657         }
1658         data_blob_free(&names_blob);
1659
1660         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1661                                               opt_workstation,
1662                                               &chall,
1663                                               &lmv2_response,
1664                                               &ntlmv2_response,
1665                                               flags,
1666                                               NULL, 
1667                                               nt_key,
1668                                               &error_string);
1669         
1670         data_blob_free(&lmv2_response);
1671         data_blob_free(&ntlmv2_response);
1672
1673         if (!NT_STATUS_IS_OK(nt_status)) {
1674                 d_printf("%s (0x%x)\n", 
1675                          error_string,
1676                          NT_STATUS_V(nt_status));
1677                 SAFE_FREE(error_string);
1678                 return False;
1679         }
1680
1681         if (memcmp(nt_session_key.data, nt_key, 
1682                    sizeof(nt_key)) != 0) {
1683                 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1684                 DEBUG(1, ("nt_key:\n"));
1685                 dump_data(1, (const char *)nt_key, 16);
1686                 DEBUG(1, ("expected:\n"));
1687                 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1688                 pass = False;
1689         }
1690         return pass;
1691 }
1692
1693 /* 
1694  * Test the LMv2 response only
1695  */
1696
1697 static BOOL test_lmv2(void) 
1698 {
1699         BOOL pass = True;
1700         NTSTATUS nt_status;
1701         uint32 flags = 0;
1702         DATA_BLOB lmv2_response = data_blob(NULL, 0);
1703
1704         DATA_BLOB chall = get_challenge();
1705         char *error_string;
1706
1707         if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1708                               NULL, 
1709                               &lmv2_response, NULL,
1710                               NULL)) {
1711                 return False;
1712         }
1713
1714         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1715                                               opt_workstation,
1716                                               &chall,
1717                                               &lmv2_response,
1718                                               NULL, 
1719                                               flags,
1720                                               NULL, 
1721                                               NULL,
1722                                               &error_string);
1723         
1724         data_blob_free(&lmv2_response);
1725
1726         if (!NT_STATUS_IS_OK(nt_status)) {
1727                 d_printf("%s (0x%x)\n", 
1728                          error_string,
1729                          NT_STATUS_V(nt_status));
1730                 SAFE_FREE(error_string);
1731                 return False;
1732         }
1733
1734         return pass;
1735 }
1736
1737 /* 
1738  * Test the normal 'LM and NTLM' combination but deliberately break one
1739  */
1740
1741 static BOOL test_ntlm_broken(BOOL break_lm) 
1742 {
1743         BOOL pass = True;
1744         NTSTATUS nt_status;
1745         uint32 flags = 0;
1746         DATA_BLOB lm_response = data_blob(NULL, 24);
1747         DATA_BLOB nt_response = data_blob(NULL, 24);
1748         DATA_BLOB session_key = data_blob(NULL, 16);
1749
1750         uchar lm_key[8];
1751         uchar nt_key[16];
1752         uchar lm_hash[16];
1753         uchar nt_hash[16];
1754         DATA_BLOB chall = get_challenge();
1755         char *error_string;
1756         
1757         ZERO_STRUCT(lm_key);
1758         ZERO_STRUCT(nt_key);
1759
1760         flags |= WBFLAG_PAM_LMKEY;
1761         flags |= WBFLAG_PAM_NTKEY;
1762
1763         SMBencrypt(opt_password,chall.data,lm_response.data);
1764         E_deshash(opt_password, lm_hash); 
1765
1766         SMBNTencrypt(opt_password,chall.data,nt_response.data);
1767
1768         E_md4hash(opt_password, nt_hash);
1769         SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1770
1771         if (break_lm)
1772                 lm_response.data[0]++;
1773         else
1774                 nt_response.data[0]++;
1775
1776         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1777                                               opt_workstation,
1778                                               &chall,
1779                                               &lm_response,
1780                                               &nt_response,
1781                                               flags,
1782                                               lm_key, 
1783                                               nt_key,
1784                                               &error_string);
1785         
1786         data_blob_free(&lm_response);
1787
1788         if (!NT_STATUS_IS_OK(nt_status)) {
1789                 d_printf("%s (0x%x)\n", 
1790                          error_string,
1791                          NT_STATUS_V(nt_status));
1792                 SAFE_FREE(error_string);
1793                 return False;
1794         }
1795
1796         if (memcmp(lm_hash, lm_key, 
1797                    sizeof(lm_key)) != 0) {
1798                 DEBUG(1, ("LM Key does not match expectations!\n"));
1799                 DEBUG(1, ("lm_key:\n"));
1800                 dump_data(1, (const char *)lm_key, 8);
1801                 DEBUG(1, ("expected:\n"));
1802                 dump_data(1, (const char *)lm_hash, 8);
1803                 pass = False;
1804         }
1805         if (memcmp(session_key.data, nt_key, 
1806                    sizeof(nt_key)) != 0) {
1807                 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1808                 DEBUG(1, ("nt_key:\n"));
1809                 dump_data(1, (const char *)nt_key, 16);
1810                 DEBUG(1, ("expected:\n"));
1811                 dump_data(1, (const char *)session_key.data, session_key.length);
1812                 pass = False;
1813         }
1814         return pass;
1815 }
1816
1817 static BOOL test_ntlm_lm_broken(void) 
1818 {
1819         return test_ntlm_broken(True);
1820 }
1821
1822 static BOOL test_ntlm_ntlm_broken(void) 
1823 {
1824         return test_ntlm_broken(False);
1825 }
1826
1827 static BOOL test_ntlmv2_broken(BOOL break_lmv2)
1828 {
1829         BOOL pass = True;
1830         NTSTATUS nt_status;
1831         uint32 flags = 0;
1832         DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1833         DATA_BLOB lmv2_response = data_blob(NULL, 0);
1834         DATA_BLOB nt_session_key = data_blob(NULL, 0);
1835         DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1836
1837         uchar nt_key[16];
1838         DATA_BLOB chall = get_challenge();
1839         char *error_string;
1840
1841         ZERO_STRUCT(nt_key);
1842         
1843         flags |= WBFLAG_PAM_NTKEY;
1844          
1845         if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1846                               &names_blob,
1847                               &lmv2_response, &ntlmv2_response, 
1848                               &nt_session_key)) {
1849                 data_blob_free(&names_blob);
1850                 return False;
1851         }
1852         data_blob_free(&names_blob);
1853
1854         /* Heh - this should break the appropriate password hash nicely! */
1855
1856         if (break_lmv2)
1857                 lmv2_response.data[0]++;
1858         else
1859                 ntlmv2_response.data[0]++;
1860
1861         nt_status = contact_winbind_auth_crap(opt_username, opt_domain, 
1862                                               opt_workstation,
1863                                               &chall,
1864                                               &lmv2_response,
1865                                               &ntlmv2_response,
1866                                               flags,
1867                                               NULL,
1868                                               nt_key,
1869                                               &error_string);
1870         
1871         data_blob_free(&lmv2_response);
1872         data_blob_free(&ntlmv2_response);
1873
1874         if (!NT_STATUS_IS_OK(nt_status)) {
1875                 d_printf("%s (0x%x)\n", 
1876                          error_string,
1877                          NT_STATUS_V(nt_status));
1878                 SAFE_FREE(error_string);
1879                 return False;
1880         }
1881
1882         return pass;
1883 }
1884
1885 static BOOL test_ntlmv2_lmv2_broken(void) 
1886 {
1887         return test_ntlmv2_broken(True);
1888 }
1889
1890 static BOOL test_ntlmv2_ntlmv2_broken(void) 
1891 {
1892         return test_ntlmv2_broken(False);
1893 }
1894
1895 /* 
1896    Tests:
1897    
1898    - LM only
1899    - NT and LM             
1900    - NT
1901    - NT in LM field
1902    - NT in both fields
1903    - NTLMv2
1904    - NTLMv2 and LMv2
1905    - LMv2
1906    
1907    check we get the correct session key in each case
1908    check what values we get for the LM session key
1909    
1910 */
1911
1912 struct ntlm_tests {
1913         BOOL (*fn)(void);
1914         const char *name;
1915 } test_table[] = {
1916         {test_lm, "LM"},
1917         {test_lm_ntlm, "LM and NTLM"},
1918         {test_ntlm, "NTLM"},
1919         {test_ntlm_in_lm, "NTLM in LM"},
1920         {test_ntlm_in_both, "NTLM in both"},
1921         {test_ntlmv2, "NTLMv2"},
1922         {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
1923         {test_lmv2, "LMv2"},
1924         {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
1925         {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
1926         {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
1927         {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"}
1928 };
1929
1930 static BOOL diagnose_ntlm_auth(void)
1931 {
1932         unsigned int i;
1933         BOOL pass = True;
1934
1935         for (i=0; test_table[i].fn; i++) {
1936                 if (!test_table[i].fn()) {
1937                         DEBUG(1, ("Test %s failed!\n", test_table[i].name));
1938                         pass = False;
1939                 }
1940         }
1941
1942         return pass;
1943 }
1944
1945 /* Main program */
1946
1947 enum {
1948         OPT_USERNAME = 1000,
1949         OPT_DOMAIN,
1950         OPT_WORKSTATION,
1951         OPT_CHALLENGE,
1952         OPT_RESPONSE,
1953         OPT_LM,
1954         OPT_NT,
1955         OPT_PASSWORD,
1956         OPT_LM_KEY,
1957         OPT_NT_KEY,
1958         OPT_DIAGNOSTICS
1959 };
1960
1961  int main(int argc, const char **argv)
1962 {
1963         int opt;
1964         static const char *helper_protocol;
1965         static int diagnostics;
1966
1967         static const char *hex_challenge;
1968         static const char *hex_lm_response;
1969         static const char *hex_nt_response;
1970         char *challenge;
1971         char *lm_response;
1972         char *nt_response;
1973         size_t challenge_len;
1974         size_t lm_response_len;
1975         size_t nt_response_len;
1976
1977         poptContext pc;
1978
1979         /* NOTE: DO NOT change this interface without considering the implications!
1980            This is an external interface, which other programs will use to interact 
1981            with this helper.
1982         */
1983
1984         /* We do not use single-letter command abbreviations, because they harm future 
1985            interface stability. */
1986
1987         struct poptOption long_options[] = {
1988                 POPT_AUTOHELP
1989                 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1990                 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1991                 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1992                 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1993                 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1994                 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1995                 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1996                 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},            
1997                 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1998                 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
1999                 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2000                 POPT_COMMON_SAMBA
2001                 POPT_TABLEEND
2002         };
2003
2004         /* Samba client initialisation */
2005
2006         dbf = x_stderr;
2007         
2008         /* Samba client initialisation */
2009
2010         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
2011                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
2012                         dyn_CONFIGFILE, strerror(errno));
2013                 exit(1);
2014         }
2015
2016         /* Parse options */
2017
2018         pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2019
2020         /* Parse command line options */
2021
2022         if (argc == 1) {
2023                 poptPrintHelp(pc, stderr, 0);
2024                 return 1;
2025         }
2026
2027         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
2028                             POPT_CONTEXT_KEEP_FIRST);
2029
2030         while((opt = poptGetNextOpt(pc)) != -1) {
2031                 switch (opt) {
2032                 case OPT_CHALLENGE:
2033                         challenge = smb_xmalloc((strlen(hex_challenge))/2+1);
2034                         if ((challenge_len = strhex_to_str(challenge, 
2035                                                            strlen(hex_challenge), 
2036                                                            hex_challenge)) != 8) {
2037                                 x_fprintf(x_stderr, "hex decode of %s failed (only got %lu bytes)!\n", 
2038                                         hex_challenge, (unsigned long)challenge_len);
2039                                 exit(1);
2040                         }
2041                         opt_challenge = data_blob(challenge, challenge_len);
2042                         SAFE_FREE(challenge);
2043                         break;
2044                 case OPT_LM: 
2045                         lm_response = smb_xmalloc((strlen(hex_lm_response))/2+1);
2046                         lm_response_len = strhex_to_str(lm_response,    
2047                                                         strlen(hex_lm_response), 
2048                                                         hex_lm_response);
2049                         if (lm_response_len != 24) {
2050                                 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
2051                                 exit(1);
2052                         }
2053                         opt_lm_response = data_blob(lm_response, lm_response_len);
2054                         SAFE_FREE(lm_response);
2055                         break;
2056                 case OPT_NT: 
2057                         nt_response = smb_xmalloc((strlen(hex_nt_response)+2)/2+1);
2058                         nt_response_len = strhex_to_str(nt_response, 
2059                                                         strlen(hex_nt_response), 
2060                                                         hex_nt_response);
2061                         if (nt_response_len < 24) {
2062                                 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
2063                                 exit(1);
2064                         }
2065                         opt_nt_response = data_blob(nt_response, nt_response_len);
2066                         SAFE_FREE(nt_response);
2067                         break;
2068                 }
2069         }
2070
2071         if (helper_protocol) {
2072                 int i;
2073                 for (i=0; i<NUM_HELPER_MODES; i++) {
2074                         if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2075                                 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2076                                 exit(0);
2077                         }
2078                 }
2079                 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
2080                 exit(1);
2081         }
2082
2083         if (!opt_username) {
2084                 x_fprintf(x_stderr, "username must be specified!\n\n");
2085                 poptPrintHelp(pc, stderr, 0);
2086                 exit(1);
2087         }
2088
2089         if (opt_domain == NULL) {
2090                 opt_domain = get_winbind_domain();
2091         }
2092
2093         if (opt_workstation == NULL) {
2094                 opt_workstation = "";
2095         }
2096
2097         if (opt_challenge.length) {
2098                 if (!check_auth_crap()) {
2099                         exit(1);
2100                 }
2101                 exit(0);
2102         } 
2103
2104         if (!opt_password) {
2105                 opt_password = getpass("password: ");
2106         }
2107
2108         if (diagnostics) {
2109                 if (!diagnose_ntlm_auth()) {
2110                         exit(1);
2111                 }
2112         } else {
2113                 fstring user;
2114
2115                 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2116                 if (!check_plaintext_auth(user, opt_password, True)) {
2117                         exit(1);
2118                 }
2119         }
2120
2121         /* Exit code */
2122
2123         poptFreeContext(pc);
2124         return 0;
2125 }