2 Unix SMB/CIFS implementation.
4 Winbind status program.
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
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.
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.
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.
28 #define DBGC_CLASS DBGC_WINBIND
30 #define SQUID_BUFFER_SIZE 2010
32 enum stdio_helper_mode {
49 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
50 char *buf, int length);
52 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
53 char *buf, int length);
55 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
56 char *buf, int length);
58 static void manage_gss_spnego_request (enum stdio_helper_mode stdio_helper_mode,
59 char *buf, int length);
61 static void manage_gss_spnego_client_request (enum stdio_helper_mode stdio_helper_mode,
62 char *buf, int length);
65 enum stdio_helper_mode mode;
67 stdio_helper_function fn;
68 } stdio_helper_protocols[] = {
69 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
70 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
71 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
72 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
73 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
74 { NUM_HELPER_MODES, NULL, NULL}
77 extern int winbindd_fd;
79 static const char *opt_username;
80 static const char *opt_domain;
81 static const char *opt_workstation;
82 static const char *opt_password;
83 static DATA_BLOB opt_challenge;
84 static DATA_BLOB opt_lm_response;
85 static DATA_BLOB opt_nt_response;
86 static int request_lm_key;
87 static int request_nt_key;
90 static char winbind_separator(void)
92 struct winbindd_response response;
99 ZERO_STRUCT(response);
101 /* Send off request */
103 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
104 NSS_STATUS_SUCCESS) {
105 d_printf("could not obtain winbind separator!\n");
109 sep = response.data.info.winbind_separator;
113 d_printf("winbind separator was NULL!\n");
120 static const char *get_winbind_domain(void)
122 struct winbindd_response response;
124 static fstring winbind_domain;
125 if (*winbind_domain) {
126 return winbind_domain;
129 ZERO_STRUCT(response);
131 /* Send off request */
133 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
134 NSS_STATUS_SUCCESS) {
135 DEBUG(0, ("could not obtain winbind domain name!\n"));
139 fstrcpy(winbind_domain, response.data.domain_name);
141 return winbind_domain;
145 static const char *get_winbind_netbios_name(void)
147 struct winbindd_response response;
149 static fstring winbind_netbios_name;
151 if (*winbind_netbios_name) {
152 return winbind_netbios_name;
155 ZERO_STRUCT(response);
157 /* Send off request */
159 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
160 NSS_STATUS_SUCCESS) {
161 DEBUG(0, ("could not obtain winbind netbios name!\n"));
165 fstrcpy(winbind_netbios_name, response.data.netbios_name);
167 return winbind_netbios_name;
171 /* Authenticate a user with a plaintext password */
173 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
175 struct winbindd_request request;
176 struct winbindd_response response;
179 /* Send off request */
181 ZERO_STRUCT(request);
182 ZERO_STRUCT(response);
184 fstrcpy(request.data.auth.user, user);
185 fstrcpy(request.data.auth.pass, pass);
187 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
189 /* Display response */
191 if (stdout_diagnostics) {
192 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
193 d_printf("Reading winbind reply failed! (0x01)\n");
196 d_printf("%s: %s (0x%x)\n",
197 response.data.auth.nt_status_string,
198 response.data.auth.error_string,
199 response.data.auth.nt_status);
201 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
202 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
205 DEBUG(3, ("%s: %s (0x%x)\n",
206 response.data.auth.nt_status_string,
207 response.data.auth.error_string,
208 response.data.auth.nt_status));
211 return (result == NSS_STATUS_SUCCESS);
214 /* authenticate a user with an encrypted username/password */
216 static NTSTATUS contact_winbind_auth_crap(const char *username,
218 const char *workstation,
219 const DATA_BLOB *challenge,
220 const DATA_BLOB *lm_response,
221 const DATA_BLOB *nt_response,
229 struct winbindd_request request;
230 struct winbindd_response response;
232 static uint8 zeros[16];
234 ZERO_STRUCT(request);
235 ZERO_STRUCT(response);
237 request.flags = flags;
239 if (push_utf8_fstring(request.data.auth_crap.user, username) == -1) {
240 *error_string = smb_xstrdup(
241 "unable to create utf8 string for username");
242 return NT_STATUS_UNSUCCESSFUL;
245 if (push_utf8_fstring(request.data.auth_crap.domain, domain) == -1) {
246 *error_string = smb_xstrdup(
247 "unable to create utf8 string for domain");
248 return NT_STATUS_UNSUCCESSFUL;
251 if (push_utf8_fstring(request.data.auth_crap.workstation,
252 workstation) == -1) {
253 *error_string = smb_xstrdup(
254 "unable to create utf8 string for workstation");
255 return NT_STATUS_UNSUCCESSFUL;
258 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
260 if (lm_response && lm_response->length) {
261 memcpy(request.data.auth_crap.lm_resp, lm_response->data, MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
262 request.data.auth_crap.lm_resp_len = lm_response->length;
265 if (nt_response && nt_response->length) {
266 memcpy(request.data.auth_crap.nt_resp, nt_response->data, MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
267 request.data.auth_crap.nt_resp_len = nt_response->length;
270 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
272 /* Display response */
274 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
275 nt_status = NT_STATUS_UNSUCCESSFUL;
277 *error_string = smb_xstrdup("Reading winbind reply failed!");
281 nt_status = (NT_STATUS(response.data.auth.nt_status));
282 if (!NT_STATUS_IS_OK(nt_status)) {
284 *error_string = smb_xstrdup(response.data.auth.error_string);
288 if ((flags & WBFLAG_PAM_LMKEY) && lm_key
289 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
290 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
291 memcpy(lm_key, response.data.auth.first_8_lm_hash,
292 sizeof(response.data.auth.first_8_lm_hash));
294 if ((flags & WBFLAG_PAM_NTKEY) && nt_key
295 && (memcmp(zeros, response.data.auth.nt_session_key,
296 sizeof(response.data.auth.nt_session_key)) != 0)) {
297 memcpy(nt_key, response.data.auth.nt_session_key,
298 sizeof(response.data.auth.nt_session_key));
303 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *nt_session_key, DATA_BLOB *lm_session_key)
305 static const char zeros[16];
311 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
312 ntlmssp_state->workstation,
313 &ntlmssp_state->chal,
314 &ntlmssp_state->lm_resp,
315 &ntlmssp_state->nt_resp,
316 WBFLAG_PAM_LMKEY | WBFLAG_PAM_NTKEY,
320 if (NT_STATUS_IS_OK(nt_status)) {
321 if (memcmp(lm_key, zeros, 8) != 0) {
322 *lm_session_key = data_blob(NULL, 16);
323 memcpy(lm_session_key->data, lm_key, 8);
324 memset(lm_session_key->data+8, '\0', 8);
327 if (memcmp(nt_key, zeros, 16) != 0) {
328 *nt_session_key = data_blob(nt_key, 16);
331 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
332 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
333 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation, error_string ? error_string : "unknown error (NULL)"));
338 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
339 char *buf, int length)
341 static NTLMSSP_STATE *ntlmssp_state = NULL;
342 DATA_BLOB request, reply;
345 if (strlen(buf) < 2) {
346 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
347 x_fprintf(x_stdout, "BH\n");
351 if (strncmp(buf, "YR", 2) == 0) {
353 ntlmssp_end(&ntlmssp_state);
354 } else if (strncmp(buf, "KK", 2) == 0) {
357 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
358 x_fprintf(x_stdout, "BH\n");
362 if (strlen(buf) > 3) {
363 request = base64_decode_data_blob(buf + 3);
365 request = data_blob(NULL, 0);
368 if (!ntlmssp_state) {
369 ntlmssp_server_start(&ntlmssp_state);
370 ntlmssp_state->check_password = winbind_pw_check;
371 ntlmssp_state->get_domain = get_winbind_domain;
372 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
375 DEBUG(10, ("got NTLMSSP packet:\n"));
376 dump_data(10, (const char *)request.data, request.length);
378 nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
380 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
381 char *reply_base64 = base64_encode_data_blob(reply);
382 x_fprintf(x_stdout, "TT %s\n", reply_base64);
383 SAFE_FREE(reply_base64);
384 data_blob_free(&reply);
385 DEBUG(10, ("NTLMSSP challenge\n"));
386 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
387 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
388 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
389 } else if (!NT_STATUS_IS_OK(nt_status)) {
390 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
391 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
393 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
394 DEBUG(10, ("NTLMSSP OK!\n"));
397 data_blob_free(&request);
400 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
401 char *buf, int length)
406 pass=memchr(buf,' ',length);
408 DEBUG(2, ("Password not found. Denying access\n"));
409 x_fprintf(x_stderr, "ERR\n");
415 if (stdio_helper_mode == SQUID_2_5_BASIC) {
416 rfc1738_unescape(user);
417 rfc1738_unescape(pass);
420 if (check_plaintext_auth(user, pass, False)) {
421 x_fprintf(x_stdout, "OK\n");
423 x_fprintf(x_stdout, "ERR\n");
427 static void offer_gss_spnego_mechs(void) {
435 pstring myname_lower;
439 pstrcpy(myname_lower, global_myname());
440 strlower_m(myname_lower);
442 pstr_sprintf(principal, "%s$@%s", myname_lower, lp_realm());
444 /* Server negTokenInit (mech offerings) */
445 spnego.type = SPNEGO_NEG_TOKEN_INIT;
446 spnego.negTokenInit.mechTypes = smb_xmalloc(sizeof(char *) * 3);
448 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
449 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
450 spnego.negTokenInit.mechTypes[2] = NULL;
452 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
453 spnego.negTokenInit.mechTypes[1] = NULL;
457 spnego.negTokenInit.mechListMIC = data_blob(principal,
460 len = write_spnego_data(&token, &spnego);
461 free_spnego_data(&spnego);
464 DEBUG(1, ("Could not write SPNEGO data blob\n"));
465 x_fprintf(x_stdout, "BH\n");
469 reply_base64 = base64_encode_data_blob(token);
470 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
472 SAFE_FREE(reply_base64);
473 data_blob_free(&token);
474 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
478 static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode,
479 char *buf, int length)
481 static NTLMSSP_STATE *ntlmssp_state = NULL;
482 SPNEGO_DATA request, response;
490 const char *reply_code;
492 pstring reply_argument;
494 if (strlen(buf) < 2) {
496 if (ntlmssp_state != NULL) {
497 DEBUG(1, ("Request for initial SPNEGO request where "
498 "we already have a state\n"));
499 x_fprintf(x_stdout, "BH\n");
503 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
504 x_fprintf(x_stdout, "BH\n");
508 if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
510 /* Initial request, get the negTokenInit offering
513 offer_gss_spnego_mechs();
517 /* All subsequent requests are "KK" (Knock, Knock ;)) and have
518 a blob. This might be negTokenInit or negTokenTarg */
520 if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
521 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
522 x_fprintf(x_stdout, "BH\n");
526 token = base64_decode_data_blob(buf + 3);
527 len = read_spnego_data(token, &request);
528 data_blob_free(&token);
531 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
532 x_fprintf(x_stdout, "BH\n");
536 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
538 /* Second request from Client. This is where the
539 client offers its mechanism to use. */
541 if ( (request.negTokenInit.mechTypes == NULL) ||
542 (request.negTokenInit.mechTypes[0] == NULL) ) {
543 DEBUG(1, ("Client did not offer any mechanism"));
544 x_fprintf(x_stdout, "BH\n");
548 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
550 if ( request.negTokenInit.mechToken.data == NULL ) {
551 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
552 x_fprintf(x_stdout, "BH\n");
556 if ( ntlmssp_state != NULL ) {
557 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
558 "already got one\n"));
559 x_fprintf(x_stdout, "BH\n");
560 ntlmssp_end(&ntlmssp_state);
564 ntlmssp_server_start(&ntlmssp_state);
565 ntlmssp_state->check_password = winbind_pw_check;
566 ntlmssp_state->get_domain = get_winbind_domain;
567 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
569 DEBUG(10, ("got NTLMSSP packet:\n"));
570 dump_data(10, (const char *)request.negTokenInit.mechToken.data,
571 request.negTokenInit.mechToken.length);
573 response.type = SPNEGO_NEG_TOKEN_TARG;
574 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
575 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
577 status = ntlmssp_update(ntlmssp_state,
578 request.negTokenInit.mechToken,
579 &response.negTokenTarg.responseToken);
583 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
588 DATA_BLOB session_key;
590 if ( request.negTokenInit.mechToken.data == NULL ) {
591 DEBUG(1, ("Client did not provide Kerberos data\n"));
592 x_fprintf(x_stdout, "BH\n");
596 response.type = SPNEGO_NEG_TOKEN_TARG;
597 response.negTokenTarg.supportedMech = strdup(OID_KERBEROS5_OLD);
598 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
599 response.negTokenTarg.responseToken = data_blob(NULL, 0);
601 status = ads_verify_ticket(lp_realm(),
602 &request.negTokenInit.mechToken,
603 &principal, &auth_data, &ap_rep,
606 /* Now in "principal" we have the name we are
609 if (NT_STATUS_IS_OK(status)) {
611 domain = strchr(principal, '@');
613 if (domain == NULL) {
614 DEBUG(1, ("Did not get a valid principal "
615 "from ads_verify_ticket\n"));
616 x_fprintf(x_stdout, "BH\n");
621 domain = strdup(domain);
622 user = strdup(principal);
624 data_blob_free(&ap_rep);
625 data_blob_free(&auth_data);
627 SAFE_FREE(principal);
634 if ( (request.negTokenTarg.supportedMech == NULL) ||
635 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
636 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
637 is the only one we support that sends this stuff */
638 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
639 request.negTokenTarg.supportedMech));
640 x_fprintf(x_stdout, "BH\n");
644 if (request.negTokenTarg.responseToken.data == NULL) {
645 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
646 x_fprintf(x_stdout, "BH\n");
650 status = ntlmssp_update(ntlmssp_state,
651 request.negTokenTarg.responseToken,
652 &response.negTokenTarg.responseToken);
654 response.type = SPNEGO_NEG_TOKEN_TARG;
655 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
656 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
658 if (NT_STATUS_IS_OK(status)) {
659 user = strdup(ntlmssp_state->user);
660 domain = strdup(ntlmssp_state->domain);
661 ntlmssp_end(&ntlmssp_state);
665 free_spnego_data(&request);
667 if (NT_STATUS_IS_OK(status)) {
668 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
670 pstr_sprintf(reply_argument, "%s\\%s", domain, user);
671 } else if (NT_STATUS_EQUAL(status,
672 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
673 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
675 pstr_sprintf(reply_argument, "*");
677 response.negTokenTarg.negResult = SPNEGO_REJECT;
679 pstrcpy(reply_argument, nt_errstr(status));
685 len = write_spnego_data(&token, &response);
686 free_spnego_data(&response);
689 DEBUG(1, ("Could not write SPNEGO data blob\n"));
690 x_fprintf(x_stdout, "BH\n");
694 reply_base64 = base64_encode_data_blob(token);
696 x_fprintf(x_stdout, "%s %s %s\n",
697 reply_code, reply_base64, reply_argument);
699 SAFE_FREE(reply_base64);
700 data_blob_free(&token);
705 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
707 static BOOL manage_client_ntlmssp_init(SPNEGO_DATA spnego)
710 DATA_BLOB null_blob = data_blob(NULL, 0);
712 char *to_server_base64;
713 const char *my_mechs[] = {OID_NTLMSSP, NULL};
715 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
717 if (client_ntlmssp_state != NULL) {
718 DEBUG(1, ("Request for initial SPNEGO request where "
719 "we already have a state\n"));
723 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
724 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
728 if (opt_password == NULL) {
730 /* Request a password from the calling process. After
731 sending it, the calling process should retry with
734 DEBUG(10, ("Requesting password\n"));
735 x_fprintf(x_stdout, "PW\n");
739 status = ntlmssp_client_start(&client_ntlmssp_state);
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
744 ntlmssp_end(&client_ntlmssp_state);
748 status = ntlmssp_set_username(client_ntlmssp_state, opt_username);
750 if (!NT_STATUS_IS_OK(status)) {
751 DEBUG(1, ("Could not set username: %s\n",
753 ntlmssp_end(&client_ntlmssp_state);
757 status = ntlmssp_set_domain(client_ntlmssp_state, opt_domain);
759 if (!NT_STATUS_IS_OK(status)) {
760 DEBUG(1, ("Could not set domain: %s\n",
762 ntlmssp_end(&client_ntlmssp_state);
766 status = ntlmssp_set_password(client_ntlmssp_state, opt_password);
768 if (!NT_STATUS_IS_OK(status)) {
769 DEBUG(1, ("Could not set password: %s\n",
771 ntlmssp_end(&client_ntlmssp_state);
775 spnego.type = SPNEGO_NEG_TOKEN_INIT;
776 spnego.negTokenInit.mechTypes = my_mechs;
777 spnego.negTokenInit.reqFlags = 0;
778 spnego.negTokenInit.mechListMIC = null_blob;
780 status = ntlmssp_update(client_ntlmssp_state, null_blob,
781 &spnego.negTokenInit.mechToken);
783 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
784 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
786 ntlmssp_end(&client_ntlmssp_state);
790 write_spnego_data(&to_server, &spnego);
791 data_blob_free(&spnego.negTokenInit.mechToken);
793 to_server_base64 = base64_encode_data_blob(to_server);
794 data_blob_free(&to_server);
795 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
796 SAFE_FREE(to_server_base64);
800 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
803 DATA_BLOB null_blob = data_blob(NULL, 0);
806 char *to_server_base64;
808 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
810 if (client_ntlmssp_state == NULL) {
811 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
812 x_fprintf(x_stdout, "BH\n");
813 ntlmssp_end(&client_ntlmssp_state);
817 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
818 x_fprintf(x_stdout, "NA\n");
819 ntlmssp_end(&client_ntlmssp_state);
823 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
824 x_fprintf(x_stdout, "AF\n");
825 ntlmssp_end(&client_ntlmssp_state);
829 status = ntlmssp_update(client_ntlmssp_state,
830 spnego.negTokenTarg.responseToken,
833 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
834 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
835 "ntlmssp_client_update, got: %s\n",
837 x_fprintf(x_stdout, "BH\n");
838 data_blob_free(&request);
839 ntlmssp_end(&client_ntlmssp_state);
843 spnego.type = SPNEGO_NEG_TOKEN_TARG;
844 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
845 spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
846 spnego.negTokenTarg.responseToken = request;
847 spnego.negTokenTarg.mechListMIC = null_blob;
849 write_spnego_data(&to_server, &spnego);
850 data_blob_free(&request);
852 to_server_base64 = base64_encode_data_blob(to_server);
853 data_blob_free(&to_server);
854 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
855 SAFE_FREE(to_server_base64);
861 static BOOL manage_client_krb5_init(SPNEGO_DATA spnego)
864 DATA_BLOB tkt, to_server;
865 DATA_BLOB session_key_krb5;
869 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
872 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
873 (spnego.negTokenInit.mechListMIC.length == 0) ) {
874 DEBUG(1, ("Did not get a principal for krb5\n"));
878 principal = malloc(spnego.negTokenInit.mechListMIC.length+1);
880 if (principal == NULL) {
881 DEBUG(1, ("Could not malloc principal\n"));
885 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
886 spnego.negTokenInit.mechListMIC.length);
887 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
889 tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
891 if (tkt.data == NULL) {
895 /* Let's try to first get the TGT, for that we need a
898 if (opt_password == NULL) {
899 DEBUG(10, ("Requesting password\n"));
900 x_fprintf(x_stdout, "PW\n");
904 pstr_sprintf(user, "%s@%s", opt_username, opt_domain);
906 if (kerberos_kinit_password(user, opt_password, 0) != 0) {
907 DEBUG(10, ("Requesting TGT failed\n"));
908 x_fprintf(x_stdout, "NA\n");
912 tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
915 data_blob_free(&session_key_krb5);
919 reply.type = SPNEGO_NEG_TOKEN_INIT;
920 reply.negTokenInit.mechTypes = my_mechs;
921 reply.negTokenInit.reqFlags = 0;
922 reply.negTokenInit.mechToken = tkt;
923 reply.negTokenInit.mechListMIC = data_blob(NULL, 0);
925 len = write_spnego_data(&to_server, &reply);
926 data_blob_free(&tkt);
929 DEBUG(1, ("Could not write SPNEGO data blob\n"));
933 reply_base64 = base64_encode_data_blob(to_server);
934 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
936 SAFE_FREE(reply_base64);
937 data_blob_free(&to_server);
938 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
942 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
944 switch (spnego.negTokenTarg.negResult) {
945 case SPNEGO_ACCEPT_INCOMPLETE:
946 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
947 x_fprintf(x_stdout, "BH\n");
949 case SPNEGO_ACCEPT_COMPLETED:
950 DEBUG(10, ("Accept completed\n"));
951 x_fprintf(x_stdout, "AF\n");
954 DEBUG(10, ("Rejected\n"));
955 x_fprintf(x_stdout, "NA\n");
958 DEBUG(1, ("Got an invalid negTokenTarg\n"));
959 x_fprintf(x_stdout, "AF\n");
965 static void manage_gss_spnego_client_request(enum stdio_helper_mode stdio_helper_mode,
966 char *buf, int length)
972 if (strlen(buf) <= 3) {
973 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
974 x_fprintf(x_stdout, "BH\n");
978 request = base64_decode_data_blob(buf+3);
980 if (strncmp(buf, "PW ", 3) == 0) {
982 /* We asked for a password and obviously got it :-) */
984 opt_password = strndup((const char *)request.data, request.length);
986 if (opt_password == NULL) {
987 DEBUG(1, ("Out of memory\n"));
988 x_fprintf(x_stdout, "BH\n");
989 data_blob_free(&request);
993 x_fprintf(x_stdout, "OK\n");
994 data_blob_free(&request);
998 if ( (strncmp(buf, "TT ", 3) != 0) &&
999 (strncmp(buf, "AF ", 3) != 0) &&
1000 (strncmp(buf, "NA ", 3) != 0) ) {
1001 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1002 x_fprintf(x_stdout, "BH\n");
1003 data_blob_free(&request);
1007 /* So we got a server challenge to generate a SPNEGO
1008 client-to-server request... */
1010 len = read_spnego_data(request, &spnego);
1011 data_blob_free(&request);
1014 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1015 x_fprintf(x_stdout, "BH\n");
1019 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1021 /* The server offers a list of mechanisms */
1023 const char **mechType = spnego.negTokenInit.mechTypes;
1025 while (*mechType != NULL) {
1028 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1029 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1030 if (manage_client_krb5_init(spnego))
1035 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1036 if (manage_client_ntlmssp_init(spnego))
1043 DEBUG(1, ("Server offered no compatible mechanism\n"));
1044 x_fprintf(x_stdout, "BH\n");
1048 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1050 if (spnego.negTokenTarg.supportedMech == NULL) {
1051 /* On accept/reject Windows does not send the
1052 mechanism anymore. Handle that here and
1053 shut down the mechanisms. */
1055 switch (spnego.negTokenTarg.negResult) {
1056 case SPNEGO_ACCEPT_COMPLETED:
1057 x_fprintf(x_stdout, "AF\n");
1060 x_fprintf(x_stdout, "NA\n");
1063 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1064 "unknown negResult: %d\n",
1065 spnego.negTokenTarg.negResult));
1066 x_fprintf(x_stdout, "BH\n");
1069 ntlmssp_end(&client_ntlmssp_state);
1073 if (strcmp(spnego.negTokenTarg.supportedMech,
1074 OID_NTLMSSP) == 0) {
1075 manage_client_ntlmssp_targ(spnego);
1080 if (strcmp(spnego.negTokenTarg.supportedMech,
1081 OID_KERBEROS5_OLD) == 0) {
1082 manage_client_krb5_targ(spnego);
1089 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1090 x_fprintf(x_stdout, "BH\n");
1094 free_spnego_data(&spnego);
1098 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn)
1100 char buf[SQUID_BUFFER_SIZE+1];
1105 /* this is not a typo - x_fgets doesn't work too well under squid */
1106 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1107 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
1108 strerror(ferror(stdin))));
1109 exit(1); /* BIIG buffer */
1112 c=memchr(buf,'\n',sizeof(buf)-1);
1121 DEBUG(2, ("Oversized message\n"));
1122 x_fprintf(x_stderr, "ERR\n");
1127 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
1129 if (buf[0] == '\0') {
1130 DEBUG(2, ("Invalid Request\n"));
1131 x_fprintf(x_stderr, "ERR\n");
1135 fn(helper_mode, buf, length);
1139 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
1140 /* initialize FDescs */
1141 x_setbuf(x_stdout, NULL);
1142 x_setbuf(x_stderr, NULL);
1144 manage_squid_request(stdio_mode, fn);
1149 /* Authenticate a user with a challenge/response */
1151 static BOOL check_auth_crap(void)
1160 static uint8 zeros[16];
1162 x_setbuf(x_stdout, NULL);
1165 flags |= WBFLAG_PAM_LMKEY;
1168 flags |= WBFLAG_PAM_NTKEY;
1170 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1176 (unsigned char *)lm_key,
1177 (unsigned char *)nt_key,
1180 if (!NT_STATUS_IS_OK(nt_status)) {
1181 x_fprintf(x_stdout, "%s (0x%x)\n",
1183 NT_STATUS_V(nt_status));
1184 SAFE_FREE(error_string);
1189 && (memcmp(zeros, lm_key,
1190 sizeof(lm_key)) != 0)) {
1191 hex_encode((const unsigned char *)lm_key,
1194 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
1195 SAFE_FREE(hex_lm_key);
1198 && (memcmp(zeros, nt_key,
1199 sizeof(nt_key)) != 0)) {
1200 hex_encode((const unsigned char *)nt_key,
1203 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_nt_key);
1204 SAFE_FREE(hex_nt_key);
1211 Authenticate a user with a challenge/response, checking session key
1212 and valid authentication types
1215 static DATA_BLOB get_challenge(void)
1217 static DATA_BLOB chal;
1218 if (opt_challenge.length)
1219 return opt_challenge;
1221 chal = data_blob(NULL, 8);
1223 generate_random_buffer(chal.data, chal.length, False);
1228 * Test the normal 'LM and NTLM' combination
1231 static BOOL test_lm_ntlm_broken(enum ntlm_break break_which)
1236 DATA_BLOB lm_response = data_blob(NULL, 24);
1237 DATA_BLOB nt_response = data_blob(NULL, 24);
1238 DATA_BLOB session_key = data_blob(NULL, 16);
1244 DATA_BLOB chall = get_challenge();
1247 ZERO_STRUCT(lm_key);
1248 ZERO_STRUCT(nt_key);
1250 flags |= WBFLAG_PAM_LMKEY;
1251 flags |= WBFLAG_PAM_NTKEY;
1253 SMBencrypt(opt_password,chall.data,lm_response.data);
1254 E_deshash(opt_password, lm_hash);
1256 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1258 E_md4hash(opt_password, nt_hash);
1259 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1261 switch (break_which) {
1265 lm_response.data[0]++;
1268 nt_response.data[0]++;
1271 data_blob_free(&lm_response);
1274 data_blob_free(&nt_response);
1278 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1288 data_blob_free(&lm_response);
1290 if (!NT_STATUS_IS_OK(nt_status)) {
1291 d_printf("%s (0x%x)\n",
1293 NT_STATUS_V(nt_status));
1294 SAFE_FREE(error_string);
1295 return break_which == BREAK_NT;
1298 if (memcmp(lm_hash, lm_key,
1299 sizeof(lm_key)) != 0) {
1300 DEBUG(1, ("LM Key does not match expectations!\n"));
1301 DEBUG(1, ("lm_key:\n"));
1302 dump_data(1, (const char *)lm_key, 8);
1303 DEBUG(1, ("expected:\n"));
1304 dump_data(1, (const char *)lm_hash, 8);
1308 if (break_which == NO_NT) {
1309 if (memcmp(lm_hash, nt_key,
1311 DEBUG(1, ("NT Session Key does not match expectations (should be LM hash)!\n"));
1312 DEBUG(1, ("nt_key:\n"));
1313 dump_data(1, (const char *)nt_key, sizeof(nt_key));
1314 DEBUG(1, ("expected:\n"));
1315 dump_data(1, (const char *)lm_hash, sizeof(lm_hash));
1319 if (memcmp(session_key.data, nt_key,
1320 sizeof(nt_key)) != 0) {
1321 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1322 DEBUG(1, ("nt_key:\n"));
1323 dump_data(1, (const char *)nt_key, 16);
1324 DEBUG(1, ("expected:\n"));
1325 dump_data(1, (const char *)session_key.data, session_key.length);
1333 * Test LM authentication, no NT response supplied
1336 static BOOL test_lm(void)
1339 return test_lm_ntlm_broken(NO_NT);
1343 * Test the NTLM response only, no LM.
1346 static BOOL test_ntlm(void)
1348 return test_lm_ntlm_broken(NO_LM);
1352 * Test the NTLM response only, but in the LM field.
1355 static BOOL test_ntlm_in_lm(void)
1360 DATA_BLOB nt_response = data_blob(NULL, 24);
1365 DATA_BLOB chall = get_challenge();
1368 ZERO_STRUCT(nt_key);
1370 flags |= WBFLAG_PAM_LMKEY;
1371 flags |= WBFLAG_PAM_NTKEY;
1373 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1375 E_deshash(opt_password, lm_hash);
1377 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1387 data_blob_free(&nt_response);
1389 if (!NT_STATUS_IS_OK(nt_status)) {
1390 d_printf("%s (0x%x)\n",
1392 NT_STATUS_V(nt_status));
1393 SAFE_FREE(error_string);
1397 if (memcmp(lm_hash, lm_key,
1398 sizeof(lm_key)) != 0) {
1399 DEBUG(1, ("LM Key does not match expectations!\n"));
1400 DEBUG(1, ("lm_key:\n"));
1401 dump_data(1, (const char *)lm_key, 8);
1402 DEBUG(1, ("expected:\n"));
1403 dump_data(1, (const char *)lm_hash, 8);
1406 if (memcmp(lm_hash, nt_key, 8) != 0) {
1407 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
1408 DEBUG(1, ("nt_key:\n"));
1409 dump_data(1, (const char *)nt_key, 16);
1410 DEBUG(1, ("expected:\n"));
1411 dump_data(1, (const char *)lm_hash, 8);
1418 * Test the NTLM response only, but in the both the NT and LM fields.
1421 static BOOL test_ntlm_in_both(void)
1426 DATA_BLOB nt_response = data_blob(NULL, 24);
1427 DATA_BLOB session_key = data_blob(NULL, 16);
1433 DATA_BLOB chall = get_challenge();
1436 ZERO_STRUCT(lm_key);
1437 ZERO_STRUCT(nt_key);
1439 flags |= WBFLAG_PAM_LMKEY;
1440 flags |= WBFLAG_PAM_NTKEY;
1442 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1443 E_md4hash(opt_password, (unsigned char *)nt_hash);
1444 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1446 E_deshash(opt_password, (unsigned char *)lm_hash);
1448 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1454 (unsigned char *)lm_key,
1455 (unsigned char *)nt_key,
1458 data_blob_free(&nt_response);
1460 if (!NT_STATUS_IS_OK(nt_status)) {
1461 d_printf("%s (0x%x)\n",
1463 NT_STATUS_V(nt_status));
1464 SAFE_FREE(error_string);
1468 if (memcmp(lm_hash, lm_key,
1469 sizeof(lm_key)) != 0) {
1470 DEBUG(1, ("LM Key does not match expectations!\n"));
1471 DEBUG(1, ("lm_key:\n"));
1472 dump_data(1, lm_key, 8);
1473 DEBUG(1, ("expected:\n"));
1474 dump_data(1, lm_hash, 8);
1477 if (memcmp(session_key.data, nt_key,
1478 sizeof(nt_key)) != 0) {
1479 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1480 DEBUG(1, ("nt_key:\n"));
1481 dump_data(1, nt_key, 16);
1482 DEBUG(1, ("expected:\n"));
1483 dump_data(1, (const char *)session_key.data, session_key.length);
1492 * Test the NTLMv2 and LMv2 responses
1495 static BOOL test_lmv2_ntlmv2_broken(enum ntlm_break break_which)
1500 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1501 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1502 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1503 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1506 DATA_BLOB chall = get_challenge();
1509 ZERO_STRUCT(nt_key);
1511 flags |= WBFLAG_PAM_NTKEY;
1513 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1515 &lmv2_response, &ntlmv2_response,
1517 data_blob_free(&names_blob);
1520 data_blob_free(&names_blob);
1522 switch (break_which) {
1526 lmv2_response.data[0]++;
1529 ntlmv2_response.data[0]++;
1532 data_blob_free(&lmv2_response);
1535 data_blob_free(&ntlmv2_response);
1539 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1549 data_blob_free(&lmv2_response);
1550 data_blob_free(&ntlmv2_response);
1552 if (!NT_STATUS_IS_OK(nt_status)) {
1553 d_printf("%s (0x%x)\n",
1555 NT_STATUS_V(nt_status));
1556 SAFE_FREE(error_string);
1557 return break_which == BREAK_NT;
1560 if (break_which != NO_NT && break_which != BREAK_NT && memcmp(nt_session_key.data, nt_key,
1561 sizeof(nt_key)) != 0) {
1562 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1563 DEBUG(1, ("nt_key:\n"));
1564 dump_data(1, (const char *)nt_key, 16);
1565 DEBUG(1, ("expected:\n"));
1566 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1573 * Test the NTLMv2 and LMv2 responses
1576 static BOOL test_lmv2_ntlmv2(void)
1578 return test_lmv2_ntlmv2_broken(BREAK_NONE);
1582 * Test the LMv2 response only
1585 static BOOL test_lmv2(void)
1587 return test_lmv2_ntlmv2_broken(NO_NT);
1591 * Test the NTLMv2 response only
1594 static BOOL test_ntlmv2(void)
1596 return test_lmv2_ntlmv2_broken(NO_LM);
1599 static BOOL test_lm_ntlm(void)
1601 return test_lm_ntlm_broken(BREAK_NONE);
1604 static BOOL test_ntlm_lm_broken(void)
1606 return test_lm_ntlm_broken(BREAK_LM);
1609 static BOOL test_ntlm_ntlm_broken(void)
1611 return test_lm_ntlm_broken(BREAK_NT);
1614 static BOOL test_ntlmv2_lmv2_broken(void)
1616 return test_lmv2_ntlmv2_broken(BREAK_LM);
1619 static BOOL test_ntlmv2_ntlmv2_broken(void)
1621 return test_lmv2_ntlmv2_broken(BREAK_NT);
1624 static BOOL test_plaintext(enum ntlm_break break_which)
1628 DATA_BLOB nt_response = data_blob(NULL, 0);
1629 DATA_BLOB lm_response = data_blob(NULL, 0);
1634 static const uchar zeros[8];
1635 DATA_BLOB chall = data_blob(zeros, sizeof(zeros));
1638 ZERO_STRUCT(nt_key);
1640 flags |= WBFLAG_PAM_NTKEY;
1641 flags |= WBFLAG_PAM_LMKEY;
1643 if ((push_ucs2_allocate((smb_ucs2_t **)&nt_response.data, opt_password)) == -1) {
1644 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1648 nt_response.length = strlen_w(((void *)nt_response.data))*sizeof(smb_ucs2_t);
1650 password = strdup_upper(opt_password);
1652 if ((convert_string_allocate(NULL, CH_UNIX,
1655 (void**)&lm_response.data)) == -1) {
1656 DEBUG(0, ("push_ascii_allocate failed!\n"));
1660 SAFE_FREE(password);
1662 lm_response.length = strlen(lm_response.data);
1664 switch (break_which) {
1668 lm_response.data[0]++;
1671 nt_response.data[0]++;
1674 SAFE_FREE(lm_response.data);
1675 lm_response.length = 0;
1678 SAFE_FREE(nt_response.data);
1679 nt_response.length = 0;
1683 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1693 SAFE_FREE(nt_response.data);
1694 SAFE_FREE(lm_response.data);
1695 data_blob_free(&chall);
1697 if (!NT_STATUS_IS_OK(nt_status)) {
1698 d_printf("%s (0x%x)\n",
1700 NT_STATUS_V(nt_status));
1701 SAFE_FREE(error_string);
1702 return break_which == BREAK_NT;
1705 return break_which != BREAK_NT;
1708 static BOOL test_plaintext_none_broken(void) {
1709 return test_plaintext(BREAK_NONE);
1712 static BOOL test_plaintext_lm_broken(void) {
1713 return test_plaintext(BREAK_LM);
1716 static BOOL test_plaintext_nt_broken(void) {
1717 return test_plaintext(BREAK_NT);
1720 static BOOL test_plaintext_nt_only(void) {
1721 return test_plaintext(NO_LM);
1724 static BOOL test_plaintext_lm_only(void) {
1725 return test_plaintext(NO_NT);
1739 - plaintext tests (in challenge-response feilds)
1741 check we get the correct session key in each case
1742 check what values we get for the LM session key
1751 {test_lm_ntlm, "LM and NTLM"},
1752 {test_ntlm, "NTLM"},
1753 {test_ntlm_in_lm, "NTLM in LM"},
1754 {test_ntlm_in_both, "NTLM in both"},
1755 {test_ntlmv2, "NTLMv2"},
1756 {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
1757 {test_lmv2, "LMv2"},
1758 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
1759 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
1760 {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
1761 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"},
1762 {test_plaintext_none_broken, "Plaintext"},
1763 {test_plaintext_lm_broken, "Plaintext LM broken"},
1764 {test_plaintext_nt_broken, "Plaintext NT broken"},
1765 {test_plaintext_nt_only, "Plaintext NT only"},
1766 {test_plaintext_lm_only, "Plaintext LM only"}
1769 static BOOL diagnose_ntlm_auth(void)
1774 for (i=0; test_table[i].fn; i++) {
1775 if (!test_table[i].fn()) {
1776 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
1787 OPT_USERNAME = 1000,
1800 int main(int argc, const char **argv)
1803 static const char *helper_protocol;
1804 static int diagnostics;
1806 static const char *hex_challenge;
1807 static const char *hex_lm_response;
1808 static const char *hex_nt_response;
1812 size_t challenge_len;
1813 size_t lm_response_len;
1814 size_t nt_response_len;
1818 /* NOTE: DO NOT change this interface without considering the implications!
1819 This is an external interface, which other programs will use to interact
1823 /* We do not use single-letter command abbreviations, because they harm future
1824 interface stability. */
1826 struct poptOption long_options[] = {
1828 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1829 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1830 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1831 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1832 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1833 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1834 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1835 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1836 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1837 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
1838 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
1843 /* Samba client initialisation */
1847 /* Samba client initialisation */
1849 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1850 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1851 dyn_CONFIGFILE, strerror(errno));
1857 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1859 /* Parse command line options */
1862 poptPrintHelp(pc, stderr, 0);
1866 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1867 POPT_CONTEXT_KEEP_FIRST);
1869 while((opt = poptGetNextOpt(pc)) != -1) {
1872 challenge = smb_xmalloc((strlen(hex_challenge))/2+1);
1873 if ((challenge_len = strhex_to_str(challenge,
1874 strlen(hex_challenge),
1875 hex_challenge)) != 8) {
1876 x_fprintf(x_stderr, "hex decode of %s failed (only got %lu bytes)!\n",
1877 hex_challenge, (unsigned long)challenge_len);
1880 opt_challenge = data_blob(challenge, challenge_len);
1881 SAFE_FREE(challenge);
1884 lm_response = smb_xmalloc((strlen(hex_lm_response))/2+1);
1885 lm_response_len = strhex_to_str(lm_response,
1886 strlen(hex_lm_response),
1888 if (lm_response_len != 24) {
1889 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
1892 opt_lm_response = data_blob(lm_response, lm_response_len);
1893 SAFE_FREE(lm_response);
1896 nt_response = smb_xmalloc((strlen(hex_nt_response)+2)/2+1);
1897 nt_response_len = strhex_to_str(nt_response,
1898 strlen(hex_nt_response),
1900 if (nt_response_len < 24) {
1901 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
1904 opt_nt_response = data_blob(nt_response, nt_response_len);
1905 SAFE_FREE(nt_response);
1910 if (helper_protocol) {
1912 for (i=0; i<NUM_HELPER_MODES; i++) {
1913 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1914 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1918 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
1922 if (!opt_username) {
1923 x_fprintf(x_stderr, "username must be specified!\n\n");
1924 poptPrintHelp(pc, stderr, 0);
1928 if (opt_domain == NULL) {
1929 opt_domain = get_winbind_domain();
1932 if (opt_workstation == NULL) {
1933 opt_workstation = "";
1936 if (opt_challenge.length) {
1937 if (!check_auth_crap()) {
1943 if (!opt_password) {
1944 opt_password = getpass("password: ");
1948 if (!diagnose_ntlm_auth()) {
1954 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
1955 if (!check_plaintext_auth(user, opt_password, True)) {
1962 poptFreeContext(pc);