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 {
41 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
42 char *buf, int length);
44 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
45 char *buf, int length);
47 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
48 char *buf, int length);
50 static void manage_gss_spnego_request (enum stdio_helper_mode stdio_helper_mode,
51 char *buf, int length);
53 static void manage_gss_spnego_client_request (enum stdio_helper_mode stdio_helper_mode,
54 char *buf, int length);
57 enum stdio_helper_mode mode;
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}
69 extern int winbindd_fd;
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;
82 static char winbind_separator(void)
84 struct winbindd_response response;
91 ZERO_STRUCT(response);
93 /* Send off request */
95 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
97 d_printf("could not obtain winbind separator!\n");
101 sep = response.data.info.winbind_separator;
105 d_printf("winbind separator was NULL!\n");
112 static const char *get_winbind_domain(void)
114 struct winbindd_response response;
116 static fstring winbind_domain;
117 if (*winbind_domain) {
118 return winbind_domain;
121 ZERO_STRUCT(response);
123 /* Send off request */
125 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
126 NSS_STATUS_SUCCESS) {
127 DEBUG(0, ("could not obtain winbind domain name!\n"));
131 fstrcpy(winbind_domain, response.data.domain_name);
133 return winbind_domain;
137 static const char *get_winbind_netbios_name(void)
139 struct winbindd_response response;
141 static fstring winbind_netbios_name;
143 if (*winbind_netbios_name) {
144 return winbind_netbios_name;
147 ZERO_STRUCT(response);
149 /* Send off request */
151 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
152 NSS_STATUS_SUCCESS) {
153 DEBUG(0, ("could not obtain winbind netbios name!\n"));
157 fstrcpy(winbind_netbios_name, response.data.netbios_name);
159 return winbind_netbios_name;
163 /* Authenticate a user with a plaintext password */
165 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
167 struct winbindd_request request;
168 struct winbindd_response response;
171 /* Send off request */
173 ZERO_STRUCT(request);
174 ZERO_STRUCT(response);
176 fstrcpy(request.data.auth.user, user);
177 fstrcpy(request.data.auth.pass, pass);
179 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
181 /* Display response */
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");
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);
193 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
194 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
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));
203 return (result == NSS_STATUS_SUCCESS);
206 /* authenticate a user with an encrypted username/password */
208 static NTSTATUS contact_winbind_auth_crap(const char *username,
210 const char *workstation,
211 const DATA_BLOB *challenge,
212 const DATA_BLOB *lm_response,
213 const DATA_BLOB *nt_response,
221 struct winbindd_request request;
222 struct winbindd_response response;
224 static uint8 zeros[16];
226 ZERO_STRUCT(request);
227 ZERO_STRUCT(response);
229 request.flags = flags;
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;
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;
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;
250 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
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;
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;
262 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
264 /* Display response */
266 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
267 nt_status = NT_STATUS_UNSUCCESSFUL;
269 *error_string = smb_xstrdup("Reading winbind reply failed!");
273 nt_status = (NT_STATUS(response.data.auth.nt_status));
274 if (!NT_STATUS_IS_OK(nt_status)) {
276 *error_string = smb_xstrdup(response.data.auth.error_string);
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));
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));
295 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *nt_session_key, DATA_BLOB *lm_session_key)
297 static const char zeros[16];
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,
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);
319 if (memcmp(nt_key, zeros, 16) != 0) {
320 *nt_session_key = data_blob(nt_key, 16);
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)"));
330 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
331 char *buf, int length)
333 static NTLMSSP_STATE *ntlmssp_state = NULL;
334 DATA_BLOB request, reply;
337 if (strlen(buf) < 2) {
338 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
339 x_fprintf(x_stdout, "BH\n");
343 if (strlen(buf) > 3) {
344 request = base64_decode_data_blob(buf + 3);
345 } else if (strcmp(buf, "YR") == 0) {
346 request = data_blob(NULL, 0);
348 ntlmssp_end(&ntlmssp_state);
350 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
351 x_fprintf(x_stdout, "BH\n");
355 if (!ntlmssp_state) {
356 ntlmssp_server_start(&ntlmssp_state);
357 ntlmssp_state->check_password = winbind_pw_check;
358 ntlmssp_state->get_domain = get_winbind_domain;
359 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
362 DEBUG(10, ("got NTLMSSP packet:\n"));
363 dump_data(10, (const char *)request.data, request.length);
365 nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
367 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
368 char *reply_base64 = base64_encode_data_blob(reply);
369 x_fprintf(x_stdout, "TT %s\n", reply_base64);
370 SAFE_FREE(reply_base64);
371 data_blob_free(&reply);
372 DEBUG(10, ("NTLMSSP challenge\n"));
373 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
374 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
375 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
376 } else if (!NT_STATUS_IS_OK(nt_status)) {
377 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
378 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
380 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
381 DEBUG(10, ("NTLMSSP OK!\n"));
384 data_blob_free(&request);
387 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
388 char *buf, int length)
393 pass=memchr(buf,' ',length);
395 DEBUG(2, ("Password not found. Denying access\n"));
396 x_fprintf(x_stderr, "ERR\n");
402 if (stdio_helper_mode == SQUID_2_5_BASIC) {
403 rfc1738_unescape(user);
404 rfc1738_unescape(pass);
407 if (check_plaintext_auth(user, pass, False)) {
408 x_fprintf(x_stdout, "OK\n");
410 x_fprintf(x_stdout, "ERR\n");
414 static void offer_gss_spnego_mechs(void) {
422 pstring myname_lower;
426 pstrcpy(myname_lower, global_myname());
427 strlower_m(myname_lower);
429 pstr_sprintf(principal, "%s$@%s", myname_lower, lp_realm());
431 /* Server negTokenInit (mech offerings) */
432 spnego.type = SPNEGO_NEG_TOKEN_INIT;
433 spnego.negTokenInit.mechTypes = smb_xmalloc(sizeof(char *) * 3);
435 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
436 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
437 spnego.negTokenInit.mechTypes[2] = NULL;
439 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
440 spnego.negTokenInit.mechTypes[1] = NULL;
444 spnego.negTokenInit.mechListMIC = data_blob(principal,
447 len = write_spnego_data(&token, &spnego);
448 free_spnego_data(&spnego);
451 DEBUG(1, ("Could not write SPNEGO data blob\n"));
452 x_fprintf(x_stdout, "BH\n");
456 reply_base64 = base64_encode_data_blob(token);
457 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
459 SAFE_FREE(reply_base64);
460 data_blob_free(&token);
461 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
465 static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode,
466 char *buf, int length)
468 static NTLMSSP_STATE *ntlmssp_state = NULL;
469 SPNEGO_DATA request, response;
477 const char *reply_code;
479 pstring reply_argument;
481 if (strlen(buf) < 2) {
483 if (ntlmssp_state != NULL) {
484 DEBUG(1, ("Request for initial SPNEGO request where "
485 "we already have a state\n"));
486 x_fprintf(x_stdout, "BH\n");
490 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
491 x_fprintf(x_stdout, "BH\n");
495 if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
497 /* Initial request, get the negTokenInit offering
500 offer_gss_spnego_mechs();
504 /* All subsequent requests are "KK" (Knock, Knock ;)) and have
505 a blob. This might be negTokenInit or negTokenTarg */
507 if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
508 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
509 x_fprintf(x_stdout, "BH\n");
513 token = base64_decode_data_blob(buf + 3);
514 len = read_spnego_data(token, &request);
515 data_blob_free(&token);
518 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
519 x_fprintf(x_stdout, "BH\n");
523 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
525 /* Second request from Client. This is where the
526 client offers its mechanism to use. */
528 if ( (request.negTokenInit.mechTypes == NULL) ||
529 (request.negTokenInit.mechTypes[0] == NULL) ) {
530 DEBUG(1, ("Client did not offer any mechanism"));
531 x_fprintf(x_stdout, "BH\n");
535 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
537 if ( request.negTokenInit.mechToken.data == NULL ) {
538 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
539 x_fprintf(x_stdout, "BH\n");
543 if ( ntlmssp_state != NULL ) {
544 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
545 "already got one\n"));
546 x_fprintf(x_stdout, "BH\n");
547 ntlmssp_end(&ntlmssp_state);
551 ntlmssp_server_start(&ntlmssp_state);
552 ntlmssp_state->check_password = winbind_pw_check;
553 ntlmssp_state->get_domain = get_winbind_domain;
554 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
556 DEBUG(10, ("got NTLMSSP packet:\n"));
557 dump_data(10, (const char *)request.negTokenInit.mechToken.data,
558 request.negTokenInit.mechToken.length);
560 response.type = SPNEGO_NEG_TOKEN_TARG;
561 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
562 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
564 status = ntlmssp_update(ntlmssp_state,
565 request.negTokenInit.mechToken,
566 &response.negTokenTarg.responseToken);
570 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
575 DATA_BLOB session_key;
577 if ( request.negTokenInit.mechToken.data == NULL ) {
578 DEBUG(1, ("Client did not provide Kerberos data\n"));
579 x_fprintf(x_stdout, "BH\n");
583 response.type = SPNEGO_NEG_TOKEN_TARG;
584 response.negTokenTarg.supportedMech = strdup(OID_KERBEROS5_OLD);
585 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
586 response.negTokenTarg.responseToken = data_blob(NULL, 0);
588 status = ads_verify_ticket(lp_realm(),
589 &request.negTokenInit.mechToken,
590 &principal, &auth_data, &ap_rep,
593 /* Now in "principal" we have the name we are
596 if (NT_STATUS_IS_OK(status)) {
598 domain = strchr(principal, '@');
600 if (domain == NULL) {
601 DEBUG(1, ("Did not get a valid principal "
602 "from ads_verify_ticket\n"));
603 x_fprintf(x_stdout, "BH\n");
608 domain = strdup(domain);
609 user = strdup(principal);
611 data_blob_free(&ap_rep);
612 data_blob_free(&auth_data);
614 SAFE_FREE(principal);
621 if ( (request.negTokenTarg.supportedMech == NULL) ||
622 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
623 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
624 is the only one we support that sends this stuff */
625 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
626 request.negTokenTarg.supportedMech));
627 x_fprintf(x_stdout, "BH\n");
631 if (request.negTokenTarg.responseToken.data == NULL) {
632 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
633 x_fprintf(x_stdout, "BH\n");
637 status = ntlmssp_update(ntlmssp_state,
638 request.negTokenTarg.responseToken,
639 &response.negTokenTarg.responseToken);
641 response.type = SPNEGO_NEG_TOKEN_TARG;
642 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
643 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
645 if (NT_STATUS_IS_OK(status)) {
646 user = strdup(ntlmssp_state->user);
647 domain = strdup(ntlmssp_state->domain);
648 ntlmssp_end(&ntlmssp_state);
652 free_spnego_data(&request);
654 if (NT_STATUS_IS_OK(status)) {
655 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
657 pstr_sprintf(reply_argument, "%s\\%s", domain, user);
658 } else if (NT_STATUS_EQUAL(status,
659 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
660 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
662 pstr_sprintf(reply_argument, "*");
664 response.negTokenTarg.negResult = SPNEGO_REJECT;
666 pstrcpy(reply_argument, nt_errstr(status));
672 len = write_spnego_data(&token, &response);
673 free_spnego_data(&response);
676 DEBUG(1, ("Could not write SPNEGO data blob\n"));
677 x_fprintf(x_stdout, "BH\n");
681 reply_base64 = base64_encode_data_blob(token);
683 x_fprintf(x_stdout, "%s %s %s\n",
684 reply_code, reply_base64, reply_argument);
686 SAFE_FREE(reply_base64);
687 data_blob_free(&token);
692 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
694 static BOOL manage_client_ntlmssp_init(SPNEGO_DATA spnego)
697 DATA_BLOB null_blob = data_blob(NULL, 0);
699 char *to_server_base64;
700 const char *my_mechs[] = {OID_NTLMSSP, NULL};
702 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
704 if (client_ntlmssp_state != NULL) {
705 DEBUG(1, ("Request for initial SPNEGO request where "
706 "we already have a state\n"));
710 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
711 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
715 if (opt_password == NULL) {
717 /* Request a password from the calling process. After
718 sending it, the calling process should retry with
721 DEBUG(10, ("Requesting password\n"));
722 x_fprintf(x_stdout, "PW\n");
726 status = ntlmssp_client_start(&client_ntlmssp_state);
728 if (!NT_STATUS_IS_OK(status)) {
729 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
731 ntlmssp_end(&client_ntlmssp_state);
735 status = ntlmssp_set_username(client_ntlmssp_state, opt_username);
737 if (!NT_STATUS_IS_OK(status)) {
738 DEBUG(1, ("Could not set username: %s\n",
740 ntlmssp_end(&client_ntlmssp_state);
744 status = ntlmssp_set_domain(client_ntlmssp_state, opt_domain);
746 if (!NT_STATUS_IS_OK(status)) {
747 DEBUG(1, ("Could not set domain: %s\n",
749 ntlmssp_end(&client_ntlmssp_state);
753 status = ntlmssp_set_password(client_ntlmssp_state, opt_password);
755 if (!NT_STATUS_IS_OK(status)) {
756 DEBUG(1, ("Could not set password: %s\n",
758 ntlmssp_end(&client_ntlmssp_state);
762 spnego.type = SPNEGO_NEG_TOKEN_INIT;
763 spnego.negTokenInit.mechTypes = my_mechs;
764 spnego.negTokenInit.reqFlags = 0;
765 spnego.negTokenInit.mechListMIC = null_blob;
767 status = ntlmssp_update(client_ntlmssp_state, null_blob,
768 &spnego.negTokenInit.mechToken);
770 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
771 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
773 ntlmssp_end(&client_ntlmssp_state);
777 write_spnego_data(&to_server, &spnego);
778 data_blob_free(&spnego.negTokenInit.mechToken);
780 to_server_base64 = base64_encode_data_blob(to_server);
781 data_blob_free(&to_server);
782 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
783 SAFE_FREE(to_server_base64);
787 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
790 DATA_BLOB null_blob = data_blob(NULL, 0);
793 char *to_server_base64;
795 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
797 if (client_ntlmssp_state == NULL) {
798 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
799 x_fprintf(x_stdout, "BH\n");
800 ntlmssp_end(&client_ntlmssp_state);
804 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
805 x_fprintf(x_stdout, "NA\n");
806 ntlmssp_end(&client_ntlmssp_state);
810 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
811 x_fprintf(x_stdout, "AF\n");
812 ntlmssp_end(&client_ntlmssp_state);
816 status = ntlmssp_update(client_ntlmssp_state,
817 spnego.negTokenTarg.responseToken,
820 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
821 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
822 "ntlmssp_client_update, got: %s\n",
824 x_fprintf(x_stdout, "BH\n");
825 data_blob_free(&request);
826 ntlmssp_end(&client_ntlmssp_state);
830 spnego.type = SPNEGO_NEG_TOKEN_TARG;
831 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
832 spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
833 spnego.negTokenTarg.responseToken = request;
834 spnego.negTokenTarg.mechListMIC = null_blob;
836 write_spnego_data(&to_server, &spnego);
837 data_blob_free(&request);
839 to_server_base64 = base64_encode_data_blob(to_server);
840 data_blob_free(&to_server);
841 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
842 SAFE_FREE(to_server_base64);
848 static BOOL manage_client_krb5_init(SPNEGO_DATA spnego)
851 DATA_BLOB tkt, to_server;
852 DATA_BLOB session_key_krb5;
856 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
859 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
860 (spnego.negTokenInit.mechListMIC.length == 0) ) {
861 DEBUG(1, ("Did not get a principal for krb5\n"));
865 principal = malloc(spnego.negTokenInit.mechListMIC.length+1);
867 if (principal == NULL) {
868 DEBUG(1, ("Could not malloc principal\n"));
872 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
873 spnego.negTokenInit.mechListMIC.length);
874 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
876 tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
878 if (tkt.data == NULL) {
882 /* Let's try to first get the TGT, for that we need a
885 if (opt_password == NULL) {
886 DEBUG(10, ("Requesting password\n"));
887 x_fprintf(x_stdout, "PW\n");
891 pstr_sprintf(user, "%s@%s", opt_username, opt_domain);
893 if (kerberos_kinit_password(user, opt_password, 0) != 0) {
894 DEBUG(10, ("Requesting TGT failed\n"));
895 x_fprintf(x_stdout, "NA\n");
899 tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
902 data_blob_free(&session_key_krb5);
906 reply.type = SPNEGO_NEG_TOKEN_INIT;
907 reply.negTokenInit.mechTypes = my_mechs;
908 reply.negTokenInit.reqFlags = 0;
909 reply.negTokenInit.mechToken = tkt;
910 reply.negTokenInit.mechListMIC = data_blob(NULL, 0);
912 len = write_spnego_data(&to_server, &reply);
913 data_blob_free(&tkt);
916 DEBUG(1, ("Could not write SPNEGO data blob\n"));
920 reply_base64 = base64_encode_data_blob(to_server);
921 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
923 SAFE_FREE(reply_base64);
924 data_blob_free(&to_server);
925 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
929 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
931 switch (spnego.negTokenTarg.negResult) {
932 case SPNEGO_ACCEPT_INCOMPLETE:
933 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
934 x_fprintf(x_stdout, "BH\n");
936 case SPNEGO_ACCEPT_COMPLETED:
937 DEBUG(10, ("Accept completed\n"));
938 x_fprintf(x_stdout, "AF\n");
941 DEBUG(10, ("Rejected\n"));
942 x_fprintf(x_stdout, "NA\n");
945 DEBUG(1, ("Got an invalid negTokenTarg\n"));
946 x_fprintf(x_stdout, "AF\n");
952 static void manage_gss_spnego_client_request(enum stdio_helper_mode stdio_helper_mode,
953 char *buf, int length)
959 if (strlen(buf) <= 3) {
960 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
961 x_fprintf(x_stdout, "BH\n");
965 request = base64_decode_data_blob(buf+3);
967 if (strncmp(buf, "PW ", 3) == 0) {
969 /* We asked for a password and obviously got it :-) */
971 opt_password = strndup((const char *)request.data, request.length);
973 if (opt_password == NULL) {
974 DEBUG(1, ("Out of memory\n"));
975 x_fprintf(x_stdout, "BH\n");
976 data_blob_free(&request);
980 x_fprintf(x_stdout, "OK\n");
981 data_blob_free(&request);
985 if ( (strncmp(buf, "TT ", 3) != 0) &&
986 (strncmp(buf, "AF ", 3) != 0) &&
987 (strncmp(buf, "NA ", 3) != 0) ) {
988 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
989 x_fprintf(x_stdout, "BH\n");
990 data_blob_free(&request);
994 /* So we got a server challenge to generate a SPNEGO
995 client-to-server request... */
997 len = read_spnego_data(request, &spnego);
998 data_blob_free(&request);
1001 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1002 x_fprintf(x_stdout, "BH\n");
1006 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1008 /* The server offers a list of mechanisms */
1010 const char **mechType = spnego.negTokenInit.mechTypes;
1012 while (*mechType != NULL) {
1015 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1016 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1017 if (manage_client_krb5_init(spnego))
1022 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1023 if (manage_client_ntlmssp_init(spnego))
1030 DEBUG(1, ("Server offered no compatible mechanism\n"));
1031 x_fprintf(x_stdout, "BH\n");
1035 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1037 if (spnego.negTokenTarg.supportedMech == NULL) {
1038 /* On accept/reject Windows does not send the
1039 mechanism anymore. Handle that here and
1040 shut down the mechanisms. */
1042 switch (spnego.negTokenTarg.negResult) {
1043 case SPNEGO_ACCEPT_COMPLETED:
1044 x_fprintf(x_stdout, "AF\n");
1047 x_fprintf(x_stdout, "NA\n");
1050 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1051 "unknown negResult: %d\n",
1052 spnego.negTokenTarg.negResult));
1053 x_fprintf(x_stdout, "BH\n");
1056 ntlmssp_end(&client_ntlmssp_state);
1060 if (strcmp(spnego.negTokenTarg.supportedMech,
1061 OID_NTLMSSP) == 0) {
1062 manage_client_ntlmssp_targ(spnego);
1067 if (strcmp(spnego.negTokenTarg.supportedMech,
1068 OID_KERBEROS5_OLD) == 0) {
1069 manage_client_krb5_targ(spnego);
1076 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1077 x_fprintf(x_stdout, "BH\n");
1081 free_spnego_data(&spnego);
1085 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn)
1087 char buf[SQUID_BUFFER_SIZE+1];
1092 /* this is not a typo - x_fgets doesn't work too well under squid */
1093 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1094 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
1095 strerror(ferror(stdin))));
1096 exit(1); /* BIIG buffer */
1099 c=memchr(buf,'\n',sizeof(buf)-1);
1108 DEBUG(2, ("Oversized message\n"));
1109 x_fprintf(x_stderr, "ERR\n");
1114 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
1116 if (buf[0] == '\0') {
1117 DEBUG(2, ("Invalid Request\n"));
1118 x_fprintf(x_stderr, "ERR\n");
1122 fn(helper_mode, buf, length);
1126 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
1127 /* initialize FDescs */
1128 x_setbuf(x_stdout, NULL);
1129 x_setbuf(x_stderr, NULL);
1131 manage_squid_request(stdio_mode, fn);
1136 /* Authenticate a user with a challenge/response */
1138 static BOOL check_auth_crap(void)
1147 static uint8 zeros[16];
1149 x_setbuf(x_stdout, NULL);
1152 flags |= WBFLAG_PAM_LMKEY;
1155 flags |= WBFLAG_PAM_NTKEY;
1157 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1163 (unsigned char *)lm_key,
1164 (unsigned char *)nt_key,
1167 if (!NT_STATUS_IS_OK(nt_status)) {
1168 x_fprintf(x_stdout, "%s (0x%x)\n",
1170 NT_STATUS_V(nt_status));
1171 SAFE_FREE(error_string);
1176 && (memcmp(zeros, lm_key,
1177 sizeof(lm_key)) != 0)) {
1178 hex_encode((const unsigned char *)lm_key,
1181 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
1182 SAFE_FREE(hex_lm_key);
1185 && (memcmp(zeros, nt_key,
1186 sizeof(nt_key)) != 0)) {
1187 hex_encode((const unsigned char *)nt_key,
1190 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_nt_key);
1191 SAFE_FREE(hex_nt_key);
1198 Authenticate a user with a challenge/response, checking session key
1199 and valid authentication types
1202 static DATA_BLOB get_challenge(void)
1204 static DATA_BLOB chal;
1205 if (opt_challenge.length)
1206 return opt_challenge;
1208 chal = data_blob(NULL, 8);
1210 generate_random_buffer(chal.data, chal.length, False);
1215 * Test LM authentication, no NT response supplied
1218 static BOOL test_lm(void)
1222 DATA_BLOB lm_response = data_blob(NULL, 24);
1227 DATA_BLOB chall = get_challenge();
1230 ZERO_STRUCT(lm_key);
1231 ZERO_STRUCT(nt_key);
1233 flags |= WBFLAG_PAM_LMKEY;
1234 flags |= WBFLAG_PAM_NTKEY;
1236 SMBencrypt(opt_password, chall.data, lm_response.data);
1237 E_deshash(opt_password, lm_hash);
1239 nt_status = contact_winbind_auth_crap(opt_username, opt_domain, opt_workstation,
1248 data_blob_free(&lm_response);
1250 if (!NT_STATUS_IS_OK(nt_status)) {
1251 d_printf("%s (0x%x)\n",
1253 NT_STATUS_V(nt_status));
1257 if (memcmp(lm_hash, lm_key,
1258 sizeof(lm_key)) != 0) {
1259 DEBUG(1, ("LM Key does not match expectations!\n"));
1260 DEBUG(1, ("lm_key:\n"));
1261 dump_data(1, (const char *)lm_key, 8);
1262 DEBUG(1, ("expected:\n"));
1263 dump_data(1, (const char *)lm_hash, 8);
1265 if (memcmp(lm_hash, nt_key, 8) != 0) {
1266 DEBUG(1, ("Session Key (first 8, lm hash) does not match expectations!\n"));
1267 DEBUG(1, ("nt_key:\n"));
1268 dump_data(1, (const char *)nt_key, 8);
1269 DEBUG(1, ("expected:\n"));
1270 dump_data(1, (const char *)lm_hash, 8);
1276 * Test the normal 'LM and NTLM' combination
1279 static BOOL test_lm_ntlm(void)
1284 DATA_BLOB lm_response = data_blob(NULL, 24);
1285 DATA_BLOB nt_response = data_blob(NULL, 24);
1286 DATA_BLOB session_key = data_blob(NULL, 16);
1292 DATA_BLOB chall = get_challenge();
1295 ZERO_STRUCT(lm_key);
1296 ZERO_STRUCT(nt_key);
1298 flags |= WBFLAG_PAM_LMKEY;
1299 flags |= WBFLAG_PAM_NTKEY;
1301 SMBencrypt(opt_password,chall.data,lm_response.data);
1302 E_deshash(opt_password, lm_hash);
1304 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1306 E_md4hash(opt_password, nt_hash);
1307 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1309 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1319 data_blob_free(&lm_response);
1321 if (!NT_STATUS_IS_OK(nt_status)) {
1322 d_printf("%s (0x%x)\n",
1324 NT_STATUS_V(nt_status));
1325 SAFE_FREE(error_string);
1329 if (memcmp(lm_hash, lm_key,
1330 sizeof(lm_key)) != 0) {
1331 DEBUG(1, ("LM Key does not match expectations!\n"));
1332 DEBUG(1, ("lm_key:\n"));
1333 dump_data(1, (const char *)lm_key, 8);
1334 DEBUG(1, ("expected:\n"));
1335 dump_data(1, (const char *)lm_hash, 8);
1338 if (memcmp(session_key.data, nt_key,
1339 sizeof(nt_key)) != 0) {
1340 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1341 DEBUG(1, ("nt_key:\n"));
1342 dump_data(1, (const char *)nt_key, 16);
1343 DEBUG(1, ("expected:\n"));
1344 dump_data(1, (const char *)session_key.data, session_key.length);
1351 * Test the NTLM response only, no LM.
1354 static BOOL test_ntlm(void)
1359 DATA_BLOB nt_response = data_blob(NULL, 24);
1360 DATA_BLOB session_key = data_blob(NULL, 16);
1366 DATA_BLOB chall = get_challenge();
1369 ZERO_STRUCT(lm_key);
1370 ZERO_STRUCT(nt_key);
1372 flags |= WBFLAG_PAM_LMKEY;
1373 flags |= WBFLAG_PAM_NTKEY;
1375 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1376 E_md4hash(opt_password, (unsigned char *)nt_hash);
1377 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1379 E_deshash(opt_password, (unsigned char *)lm_hash);
1381 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1387 (unsigned char *)lm_key,
1388 (unsigned char *)nt_key,
1391 data_blob_free(&nt_response);
1393 if (!NT_STATUS_IS_OK(nt_status)) {
1394 d_printf("%s (0x%x)\n",
1396 NT_STATUS_V(nt_status));
1397 SAFE_FREE(error_string);
1401 if (memcmp(lm_hash, lm_key,
1402 sizeof(lm_key)) != 0) {
1403 DEBUG(1, ("LM Key does not match expectations!\n"));
1404 DEBUG(1, ("lm_key:\n"));
1405 dump_data(1, lm_key, 8);
1406 DEBUG(1, ("expected:\n"));
1407 dump_data(1, lm_hash, 8);
1410 if (memcmp(session_key.data, nt_key,
1411 sizeof(nt_key)) != 0) {
1412 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1413 DEBUG(1, ("nt_key:\n"));
1414 dump_data(1, nt_key, 16);
1415 DEBUG(1, ("expected:\n"));
1416 dump_data(1, (const char *)session_key.data, session_key.length);
1423 * Test the NTLM response only, but in the LM field.
1426 static BOOL test_ntlm_in_lm(void)
1431 DATA_BLOB nt_response = data_blob(NULL, 24);
1436 DATA_BLOB chall = get_challenge();
1439 ZERO_STRUCT(nt_key);
1441 flags |= WBFLAG_PAM_LMKEY;
1442 flags |= WBFLAG_PAM_NTKEY;
1444 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1446 E_deshash(opt_password, lm_hash);
1448 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
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, (const char *)lm_key, 8);
1473 DEBUG(1, ("expected:\n"));
1474 dump_data(1, (const char *)lm_hash, 8);
1477 if (memcmp(lm_hash, nt_key, 8) != 0) {
1478 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
1479 DEBUG(1, ("nt_key:\n"));
1480 dump_data(1, (const char *)nt_key, 16);
1481 DEBUG(1, ("expected:\n"));
1482 dump_data(1, (const char *)lm_hash, 8);
1489 * Test the NTLM response only, but in the both the NT and LM fields.
1492 static BOOL test_ntlm_in_both(void)
1497 DATA_BLOB nt_response = data_blob(NULL, 24);
1498 DATA_BLOB session_key = data_blob(NULL, 16);
1504 DATA_BLOB chall = get_challenge();
1507 ZERO_STRUCT(lm_key);
1508 ZERO_STRUCT(nt_key);
1510 flags |= WBFLAG_PAM_LMKEY;
1511 flags |= WBFLAG_PAM_NTKEY;
1513 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1514 E_md4hash(opt_password, (unsigned char *)nt_hash);
1515 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1517 E_deshash(opt_password, (unsigned char *)lm_hash);
1519 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1525 (unsigned char *)lm_key,
1526 (unsigned char *)nt_key,
1529 data_blob_free(&nt_response);
1531 if (!NT_STATUS_IS_OK(nt_status)) {
1532 d_printf("%s (0x%x)\n",
1534 NT_STATUS_V(nt_status));
1535 SAFE_FREE(error_string);
1539 if (memcmp(lm_hash, lm_key,
1540 sizeof(lm_key)) != 0) {
1541 DEBUG(1, ("LM Key does not match expectations!\n"));
1542 DEBUG(1, ("lm_key:\n"));
1543 dump_data(1, lm_key, 8);
1544 DEBUG(1, ("expected:\n"));
1545 dump_data(1, lm_hash, 8);
1548 if (memcmp(session_key.data, nt_key,
1549 sizeof(nt_key)) != 0) {
1550 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1551 DEBUG(1, ("nt_key:\n"));
1552 dump_data(1, nt_key, 16);
1553 DEBUG(1, ("expected:\n"));
1554 dump_data(1, (const char *)session_key.data, session_key.length);
1563 * Test the NTLMv2 response only
1566 static BOOL test_ntlmv2(void)
1571 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1572 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1573 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1576 DATA_BLOB chall = get_challenge();
1579 ZERO_STRUCT(nt_key);
1581 flags |= WBFLAG_PAM_NTKEY;
1583 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1585 NULL, &ntlmv2_response,
1587 data_blob_free(&names_blob);
1590 data_blob_free(&names_blob);
1592 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1602 data_blob_free(&ntlmv2_response);
1604 if (!NT_STATUS_IS_OK(nt_status)) {
1605 d_printf("%s (0x%x)\n",
1607 NT_STATUS_V(nt_status));
1608 SAFE_FREE(error_string);
1612 if (memcmp(nt_session_key.data, nt_key,
1613 sizeof(nt_key)) != 0) {
1614 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1615 DEBUG(1, ("nt_key:\n"));
1616 dump_data(1, (const char *)nt_key, 16);
1617 DEBUG(1, ("expected:\n"));
1618 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1625 * Test the NTLMv2 and LMv2 responses
1628 static BOOL test_lmv2_ntlmv2(void)
1633 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1634 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1635 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1636 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1639 DATA_BLOB chall = get_challenge();
1642 ZERO_STRUCT(nt_key);
1644 flags |= WBFLAG_PAM_NTKEY;
1646 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1648 &lmv2_response, &ntlmv2_response,
1650 data_blob_free(&names_blob);
1653 data_blob_free(&names_blob);
1655 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1665 data_blob_free(&lmv2_response);
1666 data_blob_free(&ntlmv2_response);
1668 if (!NT_STATUS_IS_OK(nt_status)) {
1669 d_printf("%s (0x%x)\n",
1671 NT_STATUS_V(nt_status));
1672 SAFE_FREE(error_string);
1676 if (memcmp(nt_session_key.data, nt_key,
1677 sizeof(nt_key)) != 0) {
1678 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1679 DEBUG(1, ("nt_key:\n"));
1680 dump_data(1, (const char *)nt_key, 16);
1681 DEBUG(1, ("expected:\n"));
1682 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1689 * Test the LMv2 response only
1692 static BOOL test_lmv2(void)
1697 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1699 DATA_BLOB chall = get_challenge();
1702 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1704 &lmv2_response, NULL,
1709 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1719 data_blob_free(&lmv2_response);
1721 if (!NT_STATUS_IS_OK(nt_status)) {
1722 d_printf("%s (0x%x)\n",
1724 NT_STATUS_V(nt_status));
1725 SAFE_FREE(error_string);
1733 * Test the normal 'LM and NTLM' combination but deliberately break one
1736 static BOOL test_ntlm_broken(BOOL break_lm)
1741 DATA_BLOB lm_response = data_blob(NULL, 24);
1742 DATA_BLOB nt_response = data_blob(NULL, 24);
1743 DATA_BLOB session_key = data_blob(NULL, 16);
1749 DATA_BLOB chall = get_challenge();
1752 ZERO_STRUCT(lm_key);
1753 ZERO_STRUCT(nt_key);
1755 flags |= WBFLAG_PAM_LMKEY;
1756 flags |= WBFLAG_PAM_NTKEY;
1758 SMBencrypt(opt_password,chall.data,lm_response.data);
1759 E_deshash(opt_password, lm_hash);
1761 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1763 E_md4hash(opt_password, nt_hash);
1764 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1767 lm_response.data[0]++;
1769 nt_response.data[0]++;
1771 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1781 data_blob_free(&lm_response);
1783 if (!NT_STATUS_IS_OK(nt_status)) {
1784 d_printf("%s (0x%x)\n",
1786 NT_STATUS_V(nt_status));
1787 SAFE_FREE(error_string);
1791 if (memcmp(lm_hash, lm_key,
1792 sizeof(lm_key)) != 0) {
1793 DEBUG(1, ("LM Key does not match expectations!\n"));
1794 DEBUG(1, ("lm_key:\n"));
1795 dump_data(1, (const char *)lm_key, 8);
1796 DEBUG(1, ("expected:\n"));
1797 dump_data(1, (const char *)lm_hash, 8);
1800 if (memcmp(session_key.data, nt_key,
1801 sizeof(nt_key)) != 0) {
1802 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1803 DEBUG(1, ("nt_key:\n"));
1804 dump_data(1, (const char *)nt_key, 16);
1805 DEBUG(1, ("expected:\n"));
1806 dump_data(1, (const char *)session_key.data, session_key.length);
1812 static BOOL test_ntlm_lm_broken(void)
1814 return test_ntlm_broken(True);
1817 static BOOL test_ntlm_ntlm_broken(void)
1819 return test_ntlm_broken(False);
1822 static BOOL test_ntlmv2_broken(BOOL break_lmv2)
1827 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1828 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1829 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1830 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1833 DATA_BLOB chall = get_challenge();
1836 ZERO_STRUCT(nt_key);
1838 flags |= WBFLAG_PAM_NTKEY;
1840 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1842 &lmv2_response, &ntlmv2_response,
1844 data_blob_free(&names_blob);
1847 data_blob_free(&names_blob);
1849 /* Heh - this should break the appropriate password hash nicely! */
1852 lmv2_response.data[0]++;
1854 ntlmv2_response.data[0]++;
1856 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1866 data_blob_free(&lmv2_response);
1867 data_blob_free(&ntlmv2_response);
1869 if (!NT_STATUS_IS_OK(nt_status)) {
1870 d_printf("%s (0x%x)\n",
1872 NT_STATUS_V(nt_status));
1873 SAFE_FREE(error_string);
1880 static BOOL test_ntlmv2_lmv2_broken(void)
1882 return test_ntlmv2_broken(True);
1885 static BOOL test_ntlmv2_ntlmv2_broken(void)
1887 return test_ntlmv2_broken(False);
1902 check we get the correct session key in each case
1903 check what values we get for the LM session key
1912 {test_lm_ntlm, "LM and NTLM"},
1913 {test_ntlm, "NTLM"},
1914 {test_ntlm_in_lm, "NTLM in LM"},
1915 {test_ntlm_in_both, "NTLM in both"},
1916 {test_ntlmv2, "NTLMv2"},
1917 {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
1918 {test_lmv2, "LMv2"},
1919 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
1920 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
1921 {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
1922 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"}
1925 static BOOL diagnose_ntlm_auth(void)
1930 for (i=0; test_table[i].fn; i++) {
1931 if (!test_table[i].fn()) {
1932 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
1943 OPT_USERNAME = 1000,
1956 int main(int argc, const char **argv)
1959 static const char *helper_protocol;
1960 static int diagnostics;
1962 static const char *hex_challenge;
1963 static const char *hex_lm_response;
1964 static const char *hex_nt_response;
1968 size_t challenge_len;
1969 size_t lm_response_len;
1970 size_t nt_response_len;
1974 /* NOTE: DO NOT change this interface without considering the implications!
1975 This is an external interface, which other programs will use to interact
1979 /* We do not use single-letter command abbreviations, because they harm future
1980 interface stability. */
1982 struct poptOption long_options[] = {
1984 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1985 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1986 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1987 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1988 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1989 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1990 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1991 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1992 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1993 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
1994 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
1999 /* Samba client initialisation */
2003 /* Samba client initialisation */
2005 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
2006 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
2007 dyn_CONFIGFILE, strerror(errno));
2013 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2015 /* Parse command line options */
2018 poptPrintHelp(pc, stderr, 0);
2022 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2023 POPT_CONTEXT_KEEP_FIRST);
2025 while((opt = poptGetNextOpt(pc)) != -1) {
2028 challenge = smb_xmalloc((strlen(hex_challenge))/2+1);
2029 if ((challenge_len = strhex_to_str(challenge,
2030 strlen(hex_challenge),
2031 hex_challenge)) != 8) {
2032 x_fprintf(x_stderr, "hex decode of %s failed (only got %u bytes)!\n",
2033 hex_challenge, challenge_len);
2036 opt_challenge = data_blob(challenge, challenge_len);
2037 SAFE_FREE(challenge);
2040 lm_response = smb_xmalloc((strlen(hex_lm_response))/2+1);
2041 lm_response_len = strhex_to_str(lm_response,
2042 strlen(hex_lm_response),
2044 if (lm_response_len != 24) {
2045 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
2048 opt_lm_response = data_blob(lm_response, lm_response_len);
2049 SAFE_FREE(lm_response);
2052 nt_response = smb_xmalloc((strlen(hex_nt_response)+2)/2+1);
2053 nt_response_len = strhex_to_str(nt_response,
2054 strlen(hex_nt_response),
2056 if (nt_response_len < 24) {
2057 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
2060 opt_nt_response = data_blob(nt_response, nt_response_len);
2061 SAFE_FREE(nt_response);
2066 if (helper_protocol) {
2068 for (i=0; i<NUM_HELPER_MODES; i++) {
2069 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2070 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2074 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
2078 if (!opt_username) {
2079 x_fprintf(x_stderr, "username must be specified!\n\n");
2080 poptPrintHelp(pc, stderr, 0);
2084 if (opt_domain == NULL) {
2085 opt_domain = get_winbind_domain();
2088 if (opt_workstation == NULL) {
2089 opt_workstation = "";
2092 if (opt_challenge.length) {
2093 if (!check_auth_crap()) {
2099 if (!opt_password) {
2100 opt_password = getpass("password: ");
2104 if (!diagnose_ntlm_auth()) {
2110 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2111 if (!check_plaintext_auth(user, opt_password, True)) {
2118 poptFreeContext(pc);