2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
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 {
50 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
51 char *buf, int length);
53 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
54 char *buf, int length);
56 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
57 char *buf, int length);
59 static void manage_client_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
60 char *buf, int length);
62 static void manage_gss_spnego_request (enum stdio_helper_mode stdio_helper_mode,
63 char *buf, int length);
65 static void manage_gss_spnego_client_request (enum stdio_helper_mode stdio_helper_mode,
66 char *buf, int length);
69 enum stdio_helper_mode mode;
71 stdio_helper_function fn;
72 } stdio_helper_protocols[] = {
73 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
74 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
75 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
76 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
77 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
78 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
79 { NUM_HELPER_MODES, NULL, NULL}
82 extern int winbindd_fd;
84 static const char *opt_username;
85 static const char *opt_domain;
86 static const char *opt_workstation;
87 static const char *opt_password;
88 static DATA_BLOB opt_challenge;
89 static DATA_BLOB opt_lm_response;
90 static DATA_BLOB opt_nt_response;
91 static int request_lm_key;
92 static int request_nt_key;
94 static const char *require_membership_of;
95 static const char *require_membership_sid;
97 static char winbind_separator(void)
99 struct winbindd_response response;
106 ZERO_STRUCT(response);
108 /* Send off request */
110 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
111 NSS_STATUS_SUCCESS) {
112 d_printf("could not obtain winbind separator!\n");
116 sep = response.data.info.winbind_separator;
120 d_printf("winbind separator was NULL!\n");
127 static const char *get_winbind_domain(void)
129 struct winbindd_response response;
131 static fstring winbind_domain;
132 if (*winbind_domain) {
133 return winbind_domain;
136 ZERO_STRUCT(response);
138 /* Send off request */
140 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
141 NSS_STATUS_SUCCESS) {
142 DEBUG(0, ("could not obtain winbind domain name!\n"));
146 fstrcpy(winbind_domain, response.data.domain_name);
148 return winbind_domain;
152 static const char *get_winbind_netbios_name(void)
154 struct winbindd_response response;
156 static fstring winbind_netbios_name;
158 if (*winbind_netbios_name) {
159 return winbind_netbios_name;
162 ZERO_STRUCT(response);
164 /* Send off request */
166 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
167 NSS_STATUS_SUCCESS) {
168 DEBUG(0, ("could not obtain winbind netbios name!\n"));
172 fstrcpy(winbind_netbios_name, response.data.netbios_name);
174 return winbind_netbios_name;
178 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
179 form DOMAIN/user into a domain and a user */
181 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
185 char *p = strchr(domuser,winbind_separator());
192 fstrcpy(domain, domuser);
193 domain[PTR_DIFF(p, domuser)] = 0;
199 static BOOL get_require_membership_sid(void) {
200 struct winbindd_request request;
201 struct winbindd_response response;
203 if (!require_membership_of) {
207 if (require_membership_sid) {
211 /* Otherwise, ask winbindd for the name->sid request */
213 ZERO_STRUCT(request);
214 ZERO_STRUCT(response);
216 if (!parse_ntlm_auth_domain_user(require_membership_of,
217 request.data.name.dom_name,
218 request.data.name.name)) {
219 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
220 require_membership_of));
224 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
225 NSS_STATUS_SUCCESS) {
226 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
227 require_membership_of));
231 require_membership_sid = strdup(response.data.sid.sid);
233 if (require_membership_sid)
238 /* Authenticate a user with a plaintext password */
240 static BOOL check_plaintext_auth(const char *user, const char *pass,
241 BOOL stdout_diagnostics)
243 struct winbindd_request request;
244 struct winbindd_response response;
247 if (!get_require_membership_sid()) {
251 /* Send off request */
253 ZERO_STRUCT(request);
254 ZERO_STRUCT(response);
256 fstrcpy(request.data.auth.user, user);
257 fstrcpy(request.data.auth.pass, pass);
258 if (require_membership_sid)
259 fstrcpy(request.data.auth.required_membership_sid, require_membership_sid);
261 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
263 /* Display response */
265 if (stdout_diagnostics) {
266 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
267 d_printf("Reading winbind reply failed! (0x01)\n");
270 d_printf("%s: %s (0x%x)\n",
271 response.data.auth.nt_status_string,
272 response.data.auth.error_string,
273 response.data.auth.nt_status);
275 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
276 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
279 DEBUG(3, ("%s: %s (0x%x)\n",
280 response.data.auth.nt_status_string,
281 response.data.auth.error_string,
282 response.data.auth.nt_status));
285 return (result == NSS_STATUS_SUCCESS);
288 /* authenticate a user with an encrypted username/password */
290 static NTSTATUS contact_winbind_auth_crap(const char *username,
292 const char *workstation,
293 const DATA_BLOB *challenge,
294 const DATA_BLOB *lm_response,
295 const DATA_BLOB *nt_response,
304 struct winbindd_request request;
305 struct winbindd_response response;
307 static uint8 zeros[16];
309 if (!get_require_membership_sid()) {
310 return NT_STATUS_INVALID_PARAMETER;
313 ZERO_STRUCT(request);
314 ZERO_STRUCT(response);
316 request.flags = flags;
318 if (require_membership_sid)
319 fstrcpy(request.data.auth_crap.required_membership_sid, require_membership_sid);
321 if (push_utf8_fstring(request.data.auth_crap.user, username) == -1) {
322 *error_string = smb_xstrdup(
323 "unable to create utf8 string for username");
324 return NT_STATUS_UNSUCCESSFUL;
327 if (push_utf8_fstring(request.data.auth_crap.domain, domain) == -1) {
328 *error_string = smb_xstrdup(
329 "unable to create utf8 string for domain");
330 return NT_STATUS_UNSUCCESSFUL;
333 if (push_utf8_fstring(request.data.auth_crap.workstation,
334 workstation) == -1) {
335 *error_string = smb_xstrdup(
336 "unable to create utf8 string for workstation");
337 return NT_STATUS_UNSUCCESSFUL;
340 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
342 if (lm_response && lm_response->length) {
343 memcpy(request.data.auth_crap.lm_resp,
345 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
346 request.data.auth_crap.lm_resp_len = lm_response->length;
349 if (nt_response && nt_response->length) {
350 memcpy(request.data.auth_crap.nt_resp,
352 MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
353 request.data.auth_crap.nt_resp_len = nt_response->length;
356 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
358 /* Display response */
360 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
361 nt_status = NT_STATUS_UNSUCCESSFUL;
363 *error_string = smb_xstrdup("Reading winbind reply failed!");
367 nt_status = (NT_STATUS(response.data.auth.nt_status));
368 if (!NT_STATUS_IS_OK(nt_status)) {
370 *error_string = smb_xstrdup(response.data.auth.error_string);
374 if ((flags & WBFLAG_PAM_LMKEY) && lm_key
375 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
376 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
377 memcpy(lm_key, response.data.auth.first_8_lm_hash,
378 sizeof(response.data.auth.first_8_lm_hash));
380 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && nt_key
381 && (memcmp(zeros, response.data.auth.user_session_key,
382 sizeof(response.data.auth.user_session_key)) != 0)) {
383 memcpy(nt_key, response.data.auth.user_session_key,
384 sizeof(response.data.auth.user_session_key));
387 if (flags & WBFLAG_PAM_UNIX_NAME) {
388 if (pull_utf8_allocate(unix_name, (char *)response.extra_data) == -1) {
389 return NT_STATUS_NO_MEMORY;
396 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
398 static const char zeros[16];
405 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
406 ntlmssp_state->workstation,
407 &ntlmssp_state->chal,
408 &ntlmssp_state->lm_resp,
409 &ntlmssp_state->nt_resp,
410 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
412 &error_string, &unix_name);
414 if (NT_STATUS_IS_OK(nt_status)) {
415 if (memcmp(lm_key, zeros, 8) != 0) {
416 *lm_session_key = data_blob(NULL, 16);
417 memcpy(lm_session_key->data, lm_key, 8);
418 memset(lm_session_key->data+8, '\0', 8);
421 if (memcmp(nt_key, zeros, 16) != 0) {
422 *user_session_key = data_blob(nt_key, 16);
424 ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state->mem_ctx, unix_name);
425 SAFE_FREE(unix_name);
427 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
428 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
429 ntlmssp_state->domain, ntlmssp_state->user,
430 ntlmssp_state->workstation,
431 error_string ? error_string : "unknown error (NULL)"));
432 ntlmssp_state->auth_context = NULL;
437 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
439 static const char zeros[16];
443 uint8 lm_pw[16], nt_pw[16];
445 nt_lm_owf_gen (opt_password, nt_pw, lm_pw);
447 nt_status = ntlm_password_check(ntlmssp_state->mem_ctx,
448 &ntlmssp_state->chal,
449 &ntlmssp_state->lm_resp,
450 &ntlmssp_state->nt_resp,
454 ntlmssp_state->domain,
455 lm_pw, nt_pw, user_session_key, lm_session_key);
457 if (NT_STATUS_IS_OK(nt_status)) {
458 if (memcmp(lm_key, zeros, 8) != 0) {
459 *lm_session_key = data_blob(NULL, 16);
460 memcpy(lm_session_key->data, lm_key, 8);
461 memset(lm_session_key->data+8, '\0', 8);
464 if (memcmp(nt_key, zeros, 16) != 0) {
465 *user_session_key = data_blob(nt_key, 16);
467 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state->mem_ctx,
468 "%s%c%s", ntlmssp_state->domain,
469 *lp_winbind_separator(),
470 ntlmssp_state->user);
472 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
473 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
474 nt_errstr(nt_status)));
475 ntlmssp_state->auth_context = NULL;
480 static NTSTATUS ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE **client_ntlmssp_state)
483 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
484 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
488 status = ntlmssp_client_start(client_ntlmssp_state);
490 if (!NT_STATUS_IS_OK(status)) {
491 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
493 ntlmssp_end(client_ntlmssp_state);
497 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
499 if (!NT_STATUS_IS_OK(status)) {
500 DEBUG(1, ("Could not set username: %s\n",
502 ntlmssp_end(client_ntlmssp_state);
506 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(1, ("Could not set domain: %s\n",
511 ntlmssp_end(client_ntlmssp_state);
515 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
517 if (!NT_STATUS_IS_OK(status)) {
518 DEBUG(1, ("Could not set password: %s\n",
520 ntlmssp_end(client_ntlmssp_state);
526 static NTSTATUS ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE **ntlmssp_state)
528 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
530 if (!NT_STATUS_IS_OK(status)) {
531 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
536 /* Have we been given a local password, or should we ask winbind? */
538 (*ntlmssp_state)->check_password = local_pw_check;
539 (*ntlmssp_state)->get_domain = lp_workgroup;
540 (*ntlmssp_state)->get_global_myname = global_myname;
542 (*ntlmssp_state)->check_password = winbind_pw_check;
543 (*ntlmssp_state)->get_domain = get_winbind_domain;
544 (*ntlmssp_state)->get_global_myname = get_winbind_netbios_name;
549 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
550 char *buf, int length)
552 static NTLMSSP_STATE *ntlmssp_state = NULL;
553 DATA_BLOB request, reply;
556 if (strlen(buf) < 2) {
557 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
558 x_fprintf(x_stdout, "BH\n");
562 if (strlen(buf) > 3) {
563 request = base64_decode_data_blob(buf + 3);
565 request = data_blob(NULL, 0);
568 if ((strncmp(buf, "PW ", 3) == 0)) {
569 /* The calling application wants us to use a local password (rather than winbindd) */
571 opt_password = strndup((const char *)request.data, request.length);
573 if (opt_password == NULL) {
574 DEBUG(1, ("Out of memory\n"));
575 x_fprintf(x_stdout, "BH\n");
576 data_blob_free(&request);
580 x_fprintf(x_stdout, "OK\n");
581 data_blob_free(&request);
585 if (strncmp(buf, "YR", 2) == 0) {
587 ntlmssp_end(&ntlmssp_state);
588 } else if (strncmp(buf, "KK", 2) == 0) {
591 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
592 x_fprintf(x_stdout, "BH\n");
596 if (!ntlmssp_state) {
597 if (!NT_STATUS_IS_OK(nt_status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
598 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
603 DEBUG(10, ("got NTLMSSP packet:\n"));
604 dump_data(10, (const char *)request.data, request.length);
606 nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
608 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
609 char *reply_base64 = base64_encode_data_blob(reply);
610 x_fprintf(x_stdout, "TT %s\n", reply_base64);
611 SAFE_FREE(reply_base64);
612 data_blob_free(&reply);
613 DEBUG(10, ("NTLMSSP challenge\n"));
614 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
615 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
616 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
618 ntlmssp_end(&ntlmssp_state);
619 } else if (!NT_STATUS_IS_OK(nt_status)) {
620 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
621 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
623 x_fprintf(x_stdout, "AF %s\n", (char *)ntlmssp_state->auth_context);
624 DEBUG(10, ("NTLMSSP OK!\n"));
627 data_blob_free(&request);
630 static void manage_client_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
631 char *buf, int length)
633 static NTLMSSP_STATE *ntlmssp_state = NULL;
634 DATA_BLOB request, reply;
638 if (strlen(buf) < 2) {
639 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
640 x_fprintf(x_stdout, "BH\n");
644 if (strlen(buf) > 3) {
645 request = base64_decode_data_blob(buf + 3);
647 request = data_blob(NULL, 0);
650 if (strncmp(buf, "PW ", 3) == 0) {
651 /* We asked for a password and obviously got it :-) */
653 opt_password = strndup((const char *)request.data, request.length);
655 if (opt_password == NULL) {
656 DEBUG(1, ("Out of memory\n"));
657 x_fprintf(x_stdout, "BH\n");
658 data_blob_free(&request);
662 x_fprintf(x_stdout, "OK\n");
663 data_blob_free(&request);
667 if (opt_password == NULL) {
669 /* Request a password from the calling process. After
670 sending it, the calling process should retry asking for the negotiate. */
672 DEBUG(10, ("Requesting password\n"));
673 x_fprintf(x_stdout, "PW\n");
677 if (strncmp(buf, "YR", 2) == 0) {
679 ntlmssp_end(&ntlmssp_state);
680 } else if (strncmp(buf, "TT", 2) == 0) {
683 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
684 x_fprintf(x_stdout, "BH\n");
688 if (!ntlmssp_state) {
689 if (!NT_STATUS_IS_OK(nt_status = ntlm_auth_start_ntlmssp_client(&ntlmssp_state))) {
690 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
696 DEBUG(10, ("got NTLMSSP packet:\n"));
697 dump_data(10, (const char *)request.data, request.length);
699 nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
701 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
702 char *reply_base64 = base64_encode_data_blob(reply);
704 x_fprintf(x_stdout, "YR %s\n", reply_base64);
706 x_fprintf(x_stdout, "KK %s\n", reply_base64);
708 SAFE_FREE(reply_base64);
709 data_blob_free(&reply);
710 DEBUG(10, ("NTLMSSP challenge\n"));
711 } else if (NT_STATUS_IS_OK(nt_status)) {
712 x_fprintf(x_stdout, "AF\n");
713 DEBUG(10, ("NTLMSSP OK!\n"));
715 ntlmssp_end(&ntlmssp_state);
717 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
718 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
720 ntlmssp_end(&ntlmssp_state);
723 data_blob_free(&request);
726 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
727 char *buf, int length)
732 pass=memchr(buf,' ',length);
734 DEBUG(2, ("Password not found. Denying access\n"));
735 x_fprintf(x_stderr, "ERR\n");
741 if (stdio_helper_mode == SQUID_2_5_BASIC) {
742 rfc1738_unescape(user);
743 rfc1738_unescape(pass);
746 if (check_plaintext_auth(user, pass, False)) {
747 x_fprintf(x_stdout, "OK\n");
749 x_fprintf(x_stdout, "ERR\n");
753 static void offer_gss_spnego_mechs(void) {
761 pstring myname_lower;
765 pstrcpy(myname_lower, global_myname());
766 strlower_m(myname_lower);
768 pstr_sprintf(principal, "%s$@%s", myname_lower, lp_realm());
770 /* Server negTokenInit (mech offerings) */
771 spnego.type = SPNEGO_NEG_TOKEN_INIT;
772 spnego.negTokenInit.mechTypes = smb_xmalloc(sizeof(char *) * 3);
774 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
775 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
776 spnego.negTokenInit.mechTypes[2] = NULL;
778 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
779 spnego.negTokenInit.mechTypes[1] = NULL;
783 spnego.negTokenInit.mechListMIC = data_blob(principal,
786 len = write_spnego_data(&token, &spnego);
787 free_spnego_data(&spnego);
790 DEBUG(1, ("Could not write SPNEGO data blob\n"));
791 x_fprintf(x_stdout, "BH\n");
795 reply_base64 = base64_encode_data_blob(token);
796 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
798 SAFE_FREE(reply_base64);
799 data_blob_free(&token);
800 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
804 static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode,
805 char *buf, int length)
807 static NTLMSSP_STATE *ntlmssp_state = NULL;
808 SPNEGO_DATA request, response;
816 const char *reply_code;
818 pstring reply_argument;
820 if (strlen(buf) < 2) {
822 if (ntlmssp_state != NULL) {
823 DEBUG(1, ("Request for initial SPNEGO request where "
824 "we already have a state\n"));
825 x_fprintf(x_stdout, "BH\n");
829 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
830 x_fprintf(x_stdout, "BH\n");
834 if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
836 /* Initial request, get the negTokenInit offering
839 offer_gss_spnego_mechs();
843 /* All subsequent requests are "KK" (Knock, Knock ;)) and have
844 a blob. This might be negTokenInit or negTokenTarg */
846 if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
847 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
848 x_fprintf(x_stdout, "BH\n");
852 token = base64_decode_data_blob(buf + 3);
853 len = read_spnego_data(token, &request);
854 data_blob_free(&token);
857 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
858 x_fprintf(x_stdout, "BH\n");
862 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
864 /* Second request from Client. This is where the
865 client offers its mechanism to use. */
867 if ( (request.negTokenInit.mechTypes == NULL) ||
868 (request.negTokenInit.mechTypes[0] == NULL) ) {
869 DEBUG(1, ("Client did not offer any mechanism"));
870 x_fprintf(x_stdout, "BH\n");
874 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
876 if ( request.negTokenInit.mechToken.data == NULL ) {
877 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
878 x_fprintf(x_stdout, "BH\n");
882 if ( ntlmssp_state != NULL ) {
883 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
884 "already got one\n"));
885 x_fprintf(x_stdout, "BH\n");
886 ntlmssp_end(&ntlmssp_state);
890 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
891 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
895 DEBUG(10, ("got NTLMSSP packet:\n"));
896 dump_data(10, (const char *)request.negTokenInit.mechToken.data,
897 request.negTokenInit.mechToken.length);
899 response.type = SPNEGO_NEG_TOKEN_TARG;
900 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
901 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
903 status = ntlmssp_update(ntlmssp_state,
904 request.negTokenInit.mechToken,
905 &response.negTokenTarg.responseToken);
909 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
914 DATA_BLOB session_key;
916 if ( request.negTokenInit.mechToken.data == NULL ) {
917 DEBUG(1, ("Client did not provide Kerberos data\n"));
918 x_fprintf(x_stdout, "BH\n");
922 response.type = SPNEGO_NEG_TOKEN_TARG;
923 response.negTokenTarg.supportedMech = strdup(OID_KERBEROS5_OLD);
924 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
925 response.negTokenTarg.responseToken = data_blob(NULL, 0);
927 status = ads_verify_ticket(lp_realm(),
928 &request.negTokenInit.mechToken,
929 &principal, &auth_data, &ap_rep,
932 /* Now in "principal" we have the name we are
935 if (NT_STATUS_IS_OK(status)) {
937 domain = strchr(principal, '@');
939 if (domain == NULL) {
940 DEBUG(1, ("Did not get a valid principal "
941 "from ads_verify_ticket\n"));
942 x_fprintf(x_stdout, "BH\n");
947 domain = strdup(domain);
948 user = strdup(principal);
950 data_blob_free(&ap_rep);
951 data_blob_free(&auth_data);
953 SAFE_FREE(principal);
960 if ( (request.negTokenTarg.supportedMech == NULL) ||
961 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
962 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
963 is the only one we support that sends this stuff */
964 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
965 request.negTokenTarg.supportedMech));
966 x_fprintf(x_stdout, "BH\n");
970 if (request.negTokenTarg.responseToken.data == NULL) {
971 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
972 x_fprintf(x_stdout, "BH\n");
976 status = ntlmssp_update(ntlmssp_state,
977 request.negTokenTarg.responseToken,
978 &response.negTokenTarg.responseToken);
980 response.type = SPNEGO_NEG_TOKEN_TARG;
981 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
982 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
984 if (NT_STATUS_IS_OK(status)) {
985 user = strdup(ntlmssp_state->user);
986 domain = strdup(ntlmssp_state->domain);
987 ntlmssp_end(&ntlmssp_state);
991 free_spnego_data(&request);
993 if (NT_STATUS_IS_OK(status)) {
994 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
996 pstr_sprintf(reply_argument, "%s\\%s", domain, user);
997 } else if (NT_STATUS_EQUAL(status,
998 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
999 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1001 pstr_sprintf(reply_argument, "*");
1003 response.negTokenTarg.negResult = SPNEGO_REJECT;
1005 pstrcpy(reply_argument, nt_errstr(status));
1011 len = write_spnego_data(&token, &response);
1012 free_spnego_data(&response);
1015 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1016 x_fprintf(x_stdout, "BH\n");
1020 reply_base64 = base64_encode_data_blob(token);
1022 x_fprintf(x_stdout, "%s %s %s\n",
1023 reply_code, reply_base64, reply_argument);
1025 SAFE_FREE(reply_base64);
1026 data_blob_free(&token);
1031 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
1033 static BOOL manage_client_ntlmssp_init(SPNEGO_DATA spnego)
1036 DATA_BLOB null_blob = data_blob(NULL, 0);
1037 DATA_BLOB to_server;
1038 char *to_server_base64;
1039 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1041 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1043 if (client_ntlmssp_state != NULL) {
1044 DEBUG(1, ("Request for initial SPNEGO request where "
1045 "we already have a state\n"));
1049 if (!client_ntlmssp_state) {
1050 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1051 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1057 if (opt_password == NULL) {
1059 /* Request a password from the calling process. After
1060 sending it, the calling process should retry with
1061 the negTokenInit. */
1063 DEBUG(10, ("Requesting password\n"));
1064 x_fprintf(x_stdout, "PW\n");
1068 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1069 spnego.negTokenInit.mechTypes = my_mechs;
1070 spnego.negTokenInit.reqFlags = 0;
1071 spnego.negTokenInit.mechListMIC = null_blob;
1073 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1074 &spnego.negTokenInit.mechToken);
1076 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1077 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
1078 nt_errstr(status)));
1079 ntlmssp_end(&client_ntlmssp_state);
1083 write_spnego_data(&to_server, &spnego);
1084 data_blob_free(&spnego.negTokenInit.mechToken);
1086 to_server_base64 = base64_encode_data_blob(to_server);
1087 data_blob_free(&to_server);
1088 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1089 SAFE_FREE(to_server_base64);
1093 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
1096 DATA_BLOB null_blob = data_blob(NULL, 0);
1098 DATA_BLOB to_server;
1099 char *to_server_base64;
1101 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1103 if (client_ntlmssp_state == NULL) {
1104 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1105 x_fprintf(x_stdout, "BH\n");
1106 ntlmssp_end(&client_ntlmssp_state);
1110 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1111 x_fprintf(x_stdout, "NA\n");
1112 ntlmssp_end(&client_ntlmssp_state);
1116 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1117 x_fprintf(x_stdout, "AF\n");
1118 ntlmssp_end(&client_ntlmssp_state);
1122 status = ntlmssp_update(client_ntlmssp_state,
1123 spnego.negTokenTarg.responseToken,
1126 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1127 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1128 "ntlmssp_client_update, got: %s\n",
1129 nt_errstr(status)));
1130 x_fprintf(x_stdout, "BH\n");
1131 data_blob_free(&request);
1132 ntlmssp_end(&client_ntlmssp_state);
1136 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1137 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1138 spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
1139 spnego.negTokenTarg.responseToken = request;
1140 spnego.negTokenTarg.mechListMIC = null_blob;
1142 write_spnego_data(&to_server, &spnego);
1143 data_blob_free(&request);
1145 to_server_base64 = base64_encode_data_blob(to_server);
1146 data_blob_free(&to_server);
1147 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1148 SAFE_FREE(to_server_base64);
1154 static BOOL manage_client_krb5_init(SPNEGO_DATA spnego)
1157 DATA_BLOB tkt, to_server;
1158 DATA_BLOB session_key_krb5;
1163 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1166 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1167 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1168 DEBUG(1, ("Did not get a principal for krb5\n"));
1172 principal = malloc(spnego.negTokenInit.mechListMIC.length+1);
1174 if (principal == NULL) {
1175 DEBUG(1, ("Could not malloc principal\n"));
1179 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1180 spnego.negTokenInit.mechListMIC.length);
1181 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1183 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5);
1189 /* Let's try to first get the TGT, for that we need a
1192 if (opt_password == NULL) {
1193 DEBUG(10, ("Requesting password\n"));
1194 x_fprintf(x_stdout, "PW\n");
1198 pstr_sprintf(user, "%s@%s", opt_username, opt_domain);
1200 if ((retval = kerberos_kinit_password(user, opt_password,
1202 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1203 x_fprintf(x_stdout, "NA\n");
1207 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5);
1210 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1214 data_blob_free(&session_key_krb5);
1218 reply.type = SPNEGO_NEG_TOKEN_INIT;
1219 reply.negTokenInit.mechTypes = my_mechs;
1220 reply.negTokenInit.reqFlags = 0;
1221 reply.negTokenInit.mechToken = tkt;
1222 reply.negTokenInit.mechListMIC = data_blob(NULL, 0);
1224 len = write_spnego_data(&to_server, &reply);
1225 data_blob_free(&tkt);
1228 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1232 reply_base64 = base64_encode_data_blob(to_server);
1233 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1235 SAFE_FREE(reply_base64);
1236 data_blob_free(&to_server);
1237 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1241 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
1243 switch (spnego.negTokenTarg.negResult) {
1244 case SPNEGO_ACCEPT_INCOMPLETE:
1245 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1246 x_fprintf(x_stdout, "BH\n");
1248 case SPNEGO_ACCEPT_COMPLETED:
1249 DEBUG(10, ("Accept completed\n"));
1250 x_fprintf(x_stdout, "AF\n");
1253 DEBUG(10, ("Rejected\n"));
1254 x_fprintf(x_stdout, "NA\n");
1257 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1258 x_fprintf(x_stdout, "AF\n");
1264 static void manage_gss_spnego_client_request(enum stdio_helper_mode stdio_helper_mode,
1265 char *buf, int length)
1271 if (strlen(buf) <= 3) {
1272 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1273 x_fprintf(x_stdout, "BH\n");
1277 request = base64_decode_data_blob(buf+3);
1279 if (strncmp(buf, "PW ", 3) == 0) {
1281 /* We asked for a password and obviously got it :-) */
1283 opt_password = strndup((const char *)request.data, request.length);
1285 if (opt_password == NULL) {
1286 DEBUG(1, ("Out of memory\n"));
1287 x_fprintf(x_stdout, "BH\n");
1288 data_blob_free(&request);
1292 x_fprintf(x_stdout, "OK\n");
1293 data_blob_free(&request);
1297 if ( (strncmp(buf, "TT ", 3) != 0) &&
1298 (strncmp(buf, "AF ", 3) != 0) &&
1299 (strncmp(buf, "NA ", 3) != 0) ) {
1300 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1301 x_fprintf(x_stdout, "BH\n");
1302 data_blob_free(&request);
1306 /* So we got a server challenge to generate a SPNEGO
1307 client-to-server request... */
1309 len = read_spnego_data(request, &spnego);
1310 data_blob_free(&request);
1313 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1314 x_fprintf(x_stdout, "BH\n");
1318 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1320 /* The server offers a list of mechanisms */
1322 const char **mechType = spnego.negTokenInit.mechTypes;
1324 while (*mechType != NULL) {
1327 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1328 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1329 if (manage_client_krb5_init(spnego))
1334 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1335 if (manage_client_ntlmssp_init(spnego))
1342 DEBUG(1, ("Server offered no compatible mechanism\n"));
1343 x_fprintf(x_stdout, "BH\n");
1347 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1349 if (spnego.negTokenTarg.supportedMech == NULL) {
1350 /* On accept/reject Windows does not send the
1351 mechanism anymore. Handle that here and
1352 shut down the mechanisms. */
1354 switch (spnego.negTokenTarg.negResult) {
1355 case SPNEGO_ACCEPT_COMPLETED:
1356 x_fprintf(x_stdout, "AF\n");
1359 x_fprintf(x_stdout, "NA\n");
1362 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1363 "unknown negResult: %d\n",
1364 spnego.negTokenTarg.negResult));
1365 x_fprintf(x_stdout, "BH\n");
1368 ntlmssp_end(&client_ntlmssp_state);
1372 if (strcmp(spnego.negTokenTarg.supportedMech,
1373 OID_NTLMSSP) == 0) {
1374 manage_client_ntlmssp_targ(spnego);
1379 if (strcmp(spnego.negTokenTarg.supportedMech,
1380 OID_KERBEROS5_OLD) == 0) {
1381 manage_client_krb5_targ(spnego);
1388 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1389 x_fprintf(x_stdout, "BH\n");
1393 free_spnego_data(&spnego);
1397 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn)
1399 char buf[SQUID_BUFFER_SIZE+1];
1404 /* this is not a typo - x_fgets doesn't work too well under squid */
1405 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1406 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
1407 strerror(ferror(stdin))));
1408 exit(1); /* BIIG buffer */
1411 c=memchr(buf,'\n',sizeof(buf)-1);
1420 DEBUG(2, ("Oversized message\n"));
1421 x_fprintf(x_stderr, "ERR\n");
1426 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
1428 if (buf[0] == '\0') {
1429 DEBUG(2, ("Invalid Request\n"));
1430 x_fprintf(x_stderr, "ERR\n");
1434 fn(helper_mode, buf, length);
1438 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
1439 /* initialize FDescs */
1440 x_setbuf(x_stdout, NULL);
1441 x_setbuf(x_stderr, NULL);
1443 manage_squid_request(stdio_mode, fn);
1448 /* Authenticate a user with a challenge/response */
1450 static BOOL check_auth_crap(void)
1459 static uint8 zeros[16];
1461 x_setbuf(x_stdout, NULL);
1464 flags |= WBFLAG_PAM_LMKEY;
1467 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1469 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
1471 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1477 (unsigned char *)lm_key,
1478 (unsigned char *)nt_key,
1479 &error_string, NULL);
1481 if (!NT_STATUS_IS_OK(nt_status)) {
1482 x_fprintf(x_stdout, "%s (0x%x)\n",
1484 NT_STATUS_V(nt_status));
1485 SAFE_FREE(error_string);
1490 && (memcmp(zeros, lm_key,
1491 sizeof(lm_key)) != 0)) {
1492 hex_encode((const unsigned char *)lm_key,
1495 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
1496 SAFE_FREE(hex_lm_key);
1499 && (memcmp(zeros, nt_key,
1500 sizeof(nt_key)) != 0)) {
1501 hex_encode((const unsigned char *)nt_key,
1504 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_nt_key);
1505 SAFE_FREE(hex_nt_key);
1512 Authenticate a user with a challenge/response, checking session key
1513 and valid authentication types
1516 static DATA_BLOB get_challenge(void)
1518 static DATA_BLOB chal;
1519 if (opt_challenge.length)
1520 return opt_challenge;
1522 chal = data_blob(NULL, 8);
1524 generate_random_buffer(chal.data, chal.length, False);
1529 * Test the normal 'LM and NTLM' combination
1532 static BOOL test_lm_ntlm_broken(enum ntlm_break break_which)
1537 DATA_BLOB lm_response = data_blob(NULL, 24);
1538 DATA_BLOB nt_response = data_blob(NULL, 24);
1539 DATA_BLOB session_key = data_blob(NULL, 16);
1545 DATA_BLOB chall = get_challenge();
1548 ZERO_STRUCT(lm_key);
1549 ZERO_STRUCT(nt_key);
1551 flags |= WBFLAG_PAM_LMKEY;
1552 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1554 SMBencrypt(opt_password,chall.data,lm_response.data);
1555 E_deshash(opt_password, lm_hash);
1557 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1559 E_md4hash(opt_password, nt_hash);
1560 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1562 switch (break_which) {
1566 lm_response.data[0]++;
1569 nt_response.data[0]++;
1572 data_blob_free(&lm_response);
1575 data_blob_free(&nt_response);
1579 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1587 &error_string, NULL);
1589 data_blob_free(&lm_response);
1591 if (!NT_STATUS_IS_OK(nt_status)) {
1592 d_printf("%s (0x%x)\n",
1594 NT_STATUS_V(nt_status));
1595 SAFE_FREE(error_string);
1596 return break_which == BREAK_NT;
1599 if (memcmp(lm_hash, lm_key,
1600 sizeof(lm_key)) != 0) {
1601 DEBUG(1, ("LM Key does not match expectations!\n"));
1602 DEBUG(1, ("lm_key:\n"));
1603 dump_data(1, (const char *)lm_key, 8);
1604 DEBUG(1, ("expected:\n"));
1605 dump_data(1, (const char *)lm_hash, 8);
1609 if (break_which == NO_NT) {
1610 if (memcmp(lm_hash, nt_key,
1612 DEBUG(1, ("NT Session Key does not match expectations (should be LM hash)!\n"));
1613 DEBUG(1, ("nt_key:\n"));
1614 dump_data(1, (const char *)nt_key, sizeof(nt_key));
1615 DEBUG(1, ("expected:\n"));
1616 dump_data(1, (const char *)lm_hash, sizeof(lm_hash));
1620 if (memcmp(session_key.data, nt_key,
1621 sizeof(nt_key)) != 0) {
1622 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1623 DEBUG(1, ("nt_key:\n"));
1624 dump_data(1, (const char *)nt_key, 16);
1625 DEBUG(1, ("expected:\n"));
1626 dump_data(1, (const char *)session_key.data, session_key.length);
1634 * Test LM authentication, no NT response supplied
1637 static BOOL test_lm(void)
1640 return test_lm_ntlm_broken(NO_NT);
1644 * Test the NTLM response only, no LM.
1647 static BOOL test_ntlm(void)
1649 return test_lm_ntlm_broken(NO_LM);
1653 * Test the NTLM response only, but in the LM field.
1656 static BOOL test_ntlm_in_lm(void)
1661 DATA_BLOB nt_response = data_blob(NULL, 24);
1666 DATA_BLOB chall = get_challenge();
1669 ZERO_STRUCT(nt_key);
1671 flags |= WBFLAG_PAM_LMKEY;
1672 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1674 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1676 E_deshash(opt_password, lm_hash);
1678 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1686 &error_string, NULL);
1688 data_blob_free(&nt_response);
1690 if (!NT_STATUS_IS_OK(nt_status)) {
1691 d_printf("%s (0x%x)\n",
1693 NT_STATUS_V(nt_status));
1694 SAFE_FREE(error_string);
1698 if (memcmp(lm_hash, lm_key,
1699 sizeof(lm_key)) != 0) {
1700 DEBUG(1, ("LM Key does not match expectations!\n"));
1701 DEBUG(1, ("lm_key:\n"));
1702 dump_data(1, (const char *)lm_key, 8);
1703 DEBUG(1, ("expected:\n"));
1704 dump_data(1, (const char *)lm_hash, 8);
1707 if (memcmp(lm_hash, nt_key, 8) != 0) {
1708 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
1709 DEBUG(1, ("nt_key:\n"));
1710 dump_data(1, (const char *)nt_key, 16);
1711 DEBUG(1, ("expected:\n"));
1712 dump_data(1, (const char *)lm_hash, 8);
1719 * Test the NTLM response only, but in the both the NT and LM fields.
1722 static BOOL test_ntlm_in_both(void)
1727 DATA_BLOB nt_response = data_blob(NULL, 24);
1728 DATA_BLOB session_key = data_blob(NULL, 16);
1734 DATA_BLOB chall = get_challenge();
1737 ZERO_STRUCT(lm_key);
1738 ZERO_STRUCT(nt_key);
1740 flags |= WBFLAG_PAM_LMKEY;
1741 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1743 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1744 E_md4hash(opt_password, (unsigned char *)nt_hash);
1745 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1747 E_deshash(opt_password, (unsigned char *)lm_hash);
1749 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1755 (unsigned char *)lm_key,
1756 (unsigned char *)nt_key,
1757 &error_string, NULL);
1759 data_blob_free(&nt_response);
1761 if (!NT_STATUS_IS_OK(nt_status)) {
1762 d_printf("%s (0x%x)\n",
1764 NT_STATUS_V(nt_status));
1765 SAFE_FREE(error_string);
1769 if (memcmp(lm_hash, lm_key,
1770 sizeof(lm_key)) != 0) {
1771 DEBUG(1, ("LM Key does not match expectations!\n"));
1772 DEBUG(1, ("lm_key:\n"));
1773 dump_data(1, lm_key, 8);
1774 DEBUG(1, ("expected:\n"));
1775 dump_data(1, lm_hash, 8);
1778 if (memcmp(session_key.data, nt_key,
1779 sizeof(nt_key)) != 0) {
1780 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1781 DEBUG(1, ("nt_key:\n"));
1782 dump_data(1, nt_key, 16);
1783 DEBUG(1, ("expected:\n"));
1784 dump_data(1, (const char *)session_key.data, session_key.length);
1793 * Test the NTLMv2 and LMv2 responses
1796 static BOOL test_lmv2_ntlmv2_broken(enum ntlm_break break_which)
1801 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1802 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1803 DATA_BLOB user_session_key = data_blob(NULL, 0);
1804 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1807 DATA_BLOB chall = get_challenge();
1810 ZERO_STRUCT(nt_key);
1812 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1814 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1816 &lmv2_response, &ntlmv2_response,
1817 &user_session_key)) {
1818 data_blob_free(&names_blob);
1821 data_blob_free(&names_blob);
1823 switch (break_which) {
1827 lmv2_response.data[0]++;
1830 ntlmv2_response.data[0]++;
1833 data_blob_free(&lmv2_response);
1836 data_blob_free(&ntlmv2_response);
1840 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1848 &error_string, NULL);
1850 data_blob_free(&lmv2_response);
1851 data_blob_free(&ntlmv2_response);
1853 if (!NT_STATUS_IS_OK(nt_status)) {
1854 d_printf("%s (0x%x)\n",
1856 NT_STATUS_V(nt_status));
1857 SAFE_FREE(error_string);
1858 return break_which == BREAK_NT;
1861 if (break_which != NO_NT && break_which != BREAK_NT && memcmp(user_session_key.data, nt_key,
1862 sizeof(nt_key)) != 0) {
1863 DEBUG(1, ("USER (NT) Session Key does not match expectations!\n"));
1864 DEBUG(1, ("nt_key:\n"));
1865 dump_data(1, (const char *)nt_key, 16);
1866 DEBUG(1, ("expected:\n"));
1867 dump_data(1, (const char *)user_session_key.data, user_session_key.length);
1874 * Test the NTLMv2 and LMv2 responses
1877 static BOOL test_lmv2_ntlmv2(void)
1879 return test_lmv2_ntlmv2_broken(BREAK_NONE);
1883 * Test the LMv2 response only
1886 static BOOL test_lmv2(void)
1888 return test_lmv2_ntlmv2_broken(NO_NT);
1892 * Test the NTLMv2 response only
1895 static BOOL test_ntlmv2(void)
1897 return test_lmv2_ntlmv2_broken(NO_LM);
1900 static BOOL test_lm_ntlm(void)
1902 return test_lm_ntlm_broken(BREAK_NONE);
1905 static BOOL test_ntlm_lm_broken(void)
1907 return test_lm_ntlm_broken(BREAK_LM);
1910 static BOOL test_ntlm_ntlm_broken(void)
1912 return test_lm_ntlm_broken(BREAK_NT);
1915 static BOOL test_ntlmv2_lmv2_broken(void)
1917 return test_lmv2_ntlmv2_broken(BREAK_LM);
1920 static BOOL test_ntlmv2_ntlmv2_broken(void)
1922 return test_lmv2_ntlmv2_broken(BREAK_NT);
1925 static BOOL test_plaintext(enum ntlm_break break_which)
1929 DATA_BLOB nt_response = data_blob(NULL, 0);
1930 DATA_BLOB lm_response = data_blob(NULL, 0);
1935 static const uchar zeros[8];
1936 DATA_BLOB chall = data_blob(zeros, sizeof(zeros));
1939 ZERO_STRUCT(nt_key);
1941 flags |= WBFLAG_PAM_LMKEY;
1942 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1944 if ((push_ucs2_allocate((smb_ucs2_t **)&nt_response.data, opt_password)) == -1) {
1945 DEBUG(0, ("push_ucs2_allocate failed!\n"));
1949 nt_response.length = strlen_w(((void *)nt_response.data))*sizeof(smb_ucs2_t);
1951 password = strdup_upper(opt_password);
1953 if ((convert_string_allocate(NULL, CH_UNIX,
1956 (void**)&lm_response.data,True)) == -1) {
1957 DEBUG(0, ("push_ascii_allocate failed!\n"));
1961 SAFE_FREE(password);
1963 lm_response.length = strlen(lm_response.data);
1965 switch (break_which) {
1969 lm_response.data[0]++;
1972 nt_response.data[0]++;
1975 SAFE_FREE(lm_response.data);
1976 lm_response.length = 0;
1979 SAFE_FREE(nt_response.data);
1980 nt_response.length = 0;
1984 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1992 &error_string, NULL);
1994 SAFE_FREE(nt_response.data);
1995 SAFE_FREE(lm_response.data);
1996 data_blob_free(&chall);
1998 if (!NT_STATUS_IS_OK(nt_status)) {
1999 d_printf("%s (0x%x)\n",
2001 NT_STATUS_V(nt_status));
2002 SAFE_FREE(error_string);
2003 return break_which == BREAK_NT;
2006 return break_which != BREAK_NT;
2009 static BOOL test_plaintext_none_broken(void) {
2010 return test_plaintext(BREAK_NONE);
2013 static BOOL test_plaintext_lm_broken(void) {
2014 return test_plaintext(BREAK_LM);
2017 static BOOL test_plaintext_nt_broken(void) {
2018 return test_plaintext(BREAK_NT);
2021 static BOOL test_plaintext_nt_only(void) {
2022 return test_plaintext(NO_LM);
2025 static BOOL test_plaintext_lm_only(void) {
2026 return test_plaintext(NO_NT);
2040 - plaintext tests (in challenge-response feilds)
2042 check we get the correct session key in each case
2043 check what values we get for the LM session key
2047 static const struct ntlm_tests {
2052 {test_lm_ntlm, "LM and NTLM"},
2053 {test_ntlm, "NTLM"},
2054 {test_ntlm_in_lm, "NTLM in LM"},
2055 {test_ntlm_in_both, "NTLM in both"},
2056 {test_ntlmv2, "NTLMv2"},
2057 {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
2058 {test_lmv2, "LMv2"},
2059 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
2060 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
2061 {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
2062 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"},
2063 {test_plaintext_none_broken, "Plaintext"},
2064 {test_plaintext_lm_broken, "Plaintext LM broken"},
2065 {test_plaintext_nt_broken, "Plaintext NT broken"},
2066 {test_plaintext_nt_only, "Plaintext NT only"},
2067 {test_plaintext_lm_only, "Plaintext LM only"}
2070 static BOOL diagnose_ntlm_auth(void)
2075 for (i=0; test_table[i].fn; i++) {
2076 if (!test_table[i].fn()) {
2077 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
2088 OPT_USERNAME = 1000,
2099 OPT_REQUIRE_MEMBERSHIP
2102 int main(int argc, const char **argv)
2105 static const char *helper_protocol;
2106 static int diagnostics;
2108 static const char *hex_challenge;
2109 static const char *hex_lm_response;
2110 static const char *hex_nt_response;
2114 /* NOTE: DO NOT change this interface without considering the implications!
2115 This is an external interface, which other programs will use to interact
2119 /* We do not use single-letter command abbreviations, because they harm future
2120 interface stability. */
2122 struct poptOption long_options[] = {
2124 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2125 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2126 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2127 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2128 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2129 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2130 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2131 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2132 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
2133 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
2134 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2135 { "require-membership-of", 0, POPT_ARG_STRING, &require_membership_of, OPT_REQUIRE_MEMBERSHIP, "Require that a user be a member of this group (either name or SID) for authentication to succeed" },
2140 /* Samba client initialisation */
2144 /* Samba client initialisation */
2146 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
2147 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
2148 dyn_CONFIGFILE, strerror(errno));
2154 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2156 /* Parse command line options */
2159 poptPrintHelp(pc, stderr, 0);
2163 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2164 POPT_CONTEXT_KEEP_FIRST);
2166 while((opt = poptGetNextOpt(pc)) != -1) {
2169 opt_challenge = strhex_to_data_blob(hex_challenge);
2170 if (opt_challenge.length != 8) {
2171 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_challenge);
2176 opt_lm_response = strhex_to_data_blob(hex_lm_response);
2177 if (opt_lm_response.length != 24) {
2178 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
2184 opt_nt_response = strhex_to_data_blob(hex_nt_response);
2185 if (opt_nt_response.length < 24) {
2186 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
2191 case OPT_REQUIRE_MEMBERSHIP:
2192 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2193 require_membership_sid = require_membership_of;
2199 if (helper_protocol) {
2201 for (i=0; i<NUM_HELPER_MODES; i++) {
2202 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2203 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2207 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2209 for (i=0; i<NUM_HELPER_MODES; i++) {
2210 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2216 if (!opt_username) {
2217 x_fprintf(x_stderr, "username must be specified!\n\n");
2218 poptPrintHelp(pc, stderr, 0);
2222 if (opt_domain == NULL) {
2223 opt_domain = get_winbind_domain();
2226 if (opt_workstation == NULL) {
2227 opt_workstation = "";
2230 if (opt_challenge.length) {
2231 if (!check_auth_crap()) {
2237 if (!opt_password) {
2238 opt_password = getpass("password: ");
2242 if (!diagnose_ntlm_auth()) {
2248 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2249 if (!check_plaintext_auth(user, opt_password, True)) {
2256 poptFreeContext(pc);