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.
26 #include "../utils/ntlm_auth.h"
29 #define DBGC_CLASS DBGC_WINBIND
31 #define SQUID_BUFFER_SIZE 2010
33 enum stdio_helper_mode {
44 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
45 char *buf, int length);
47 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
48 char *buf, int length);
50 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
51 char *buf, int length);
53 static void manage_client_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
54 char *buf, int length);
56 static void manage_gss_spnego_request (enum stdio_helper_mode stdio_helper_mode,
57 char *buf, int length);
59 static void manage_gss_spnego_client_request (enum stdio_helper_mode stdio_helper_mode,
60 char *buf, int length);
62 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
63 char *buf, int length);
66 enum stdio_helper_mode mode;
68 stdio_helper_function fn;
69 } stdio_helper_protocols[] = {
70 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
71 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
72 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
73 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
74 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
75 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
77 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
79 { NUM_HELPER_MODES, NULL, NULL}
82 extern int winbindd_fd;
84 const char *opt_username;
85 const char *opt_domain;
86 const char *opt_workstation;
87 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_user_session_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");
113 return *lp_winbind_separator();
116 sep = response.data.info.winbind_separator;
120 d_printf("winbind separator was NULL!\n");
121 return *lp_winbind_separator();
127 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"));
143 return lp_workgroup();
146 fstrcpy(winbind_domain, response.data.domain_name);
148 return winbind_domain;
152 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"));
169 return global_myname();
172 fstrcpy(winbind_netbios_name, response.data.netbios_name);
174 return winbind_netbios_name;
178 DATA_BLOB get_challenge(void)
180 static DATA_BLOB chal;
181 if (opt_challenge.length)
182 return opt_challenge;
184 chal = data_blob(NULL, 8);
186 generate_random_buffer(chal.data, chal.length, False);
190 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
191 form DOMAIN/user into a domain and a user */
193 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
197 char *p = strchr(domuser,winbind_separator());
204 fstrcpy(domain, domuser);
205 domain[PTR_DIFF(p, domuser)] = 0;
211 static BOOL get_require_membership_sid(void) {
212 struct winbindd_request request;
213 struct winbindd_response response;
215 if (!require_membership_of) {
219 if (require_membership_sid) {
223 /* Otherwise, ask winbindd for the name->sid request */
225 ZERO_STRUCT(request);
226 ZERO_STRUCT(response);
228 if (!parse_ntlm_auth_domain_user(require_membership_of,
229 request.data.name.dom_name,
230 request.data.name.name)) {
231 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
232 require_membership_of));
236 if (winbindd_request(WINBINDD_LOOKUPNAME, &request, &response) !=
237 NSS_STATUS_SUCCESS) {
238 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
239 require_membership_of));
243 require_membership_sid = strdup(response.data.sid.sid);
245 if (require_membership_sid)
250 /* Authenticate a user with a plaintext password */
252 static BOOL check_plaintext_auth(const char *user, const char *pass,
253 BOOL stdout_diagnostics)
255 struct winbindd_request request;
256 struct winbindd_response response;
259 if (!get_require_membership_sid()) {
263 /* Send off request */
265 ZERO_STRUCT(request);
266 ZERO_STRUCT(response);
268 fstrcpy(request.data.auth.user, user);
269 fstrcpy(request.data.auth.pass, pass);
270 if (require_membership_sid)
271 fstrcpy(request.data.auth.required_membership_sid, require_membership_sid);
273 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
275 /* Display response */
277 if (stdout_diagnostics) {
278 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
279 d_printf("Reading winbind reply failed! (0x01)\n");
282 d_printf("%s: %s (0x%x)\n",
283 response.data.auth.nt_status_string,
284 response.data.auth.error_string,
285 response.data.auth.nt_status);
287 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
288 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
291 DEBUG(3, ("%s: %s (0x%x)\n",
292 response.data.auth.nt_status_string,
293 response.data.auth.error_string,
294 response.data.auth.nt_status));
297 return (result == NSS_STATUS_SUCCESS);
300 /* authenticate a user with an encrypted username/password */
302 NTSTATUS contact_winbind_auth_crap(const char *username,
304 const char *workstation,
305 const DATA_BLOB *challenge,
306 const DATA_BLOB *lm_response,
307 const DATA_BLOB *nt_response,
310 uint8 user_session_key[16],
316 struct winbindd_request request;
317 struct winbindd_response response;
319 if (!get_require_membership_sid()) {
320 return NT_STATUS_INVALID_PARAMETER;
323 ZERO_STRUCT(request);
324 ZERO_STRUCT(response);
326 request.flags = flags;
328 if (require_membership_sid)
329 fstrcpy(request.data.auth_crap.required_membership_sid, require_membership_sid);
331 if (push_utf8_fstring(request.data.auth_crap.user, username) == -1) {
332 *error_string = smb_xstrdup(
333 "unable to create utf8 string for username");
334 return NT_STATUS_UNSUCCESSFUL;
337 if (push_utf8_fstring(request.data.auth_crap.domain, domain) == -1) {
338 *error_string = smb_xstrdup(
339 "unable to create utf8 string for domain");
340 return NT_STATUS_UNSUCCESSFUL;
343 if (push_utf8_fstring(request.data.auth_crap.workstation,
344 workstation) == -1) {
345 *error_string = smb_xstrdup(
346 "unable to create utf8 string for workstation");
347 return NT_STATUS_UNSUCCESSFUL;
350 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
352 if (lm_response && lm_response->length) {
353 memcpy(request.data.auth_crap.lm_resp,
355 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
356 request.data.auth_crap.lm_resp_len = lm_response->length;
359 if (nt_response && nt_response->length) {
360 memcpy(request.data.auth_crap.nt_resp,
362 MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
363 request.data.auth_crap.nt_resp_len = nt_response->length;
366 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
368 /* Display response */
370 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
371 nt_status = NT_STATUS_UNSUCCESSFUL;
373 *error_string = smb_xstrdup("Reading winbind reply failed!");
377 nt_status = (NT_STATUS(response.data.auth.nt_status));
378 if (!NT_STATUS_IS_OK(nt_status)) {
380 *error_string = smb_xstrdup(response.data.auth.error_string);
384 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
385 memcpy(lm_key, response.data.auth.first_8_lm_hash,
386 sizeof(response.data.auth.first_8_lm_hash));
388 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
389 memcpy(user_session_key, response.data.auth.user_session_key,
390 sizeof(response.data.auth.user_session_key));
393 if (flags & WBFLAG_PAM_UNIX_NAME) {
394 if (pull_utf8_allocate(unix_name, (char *)response.extra_data) == -1) {
395 return NT_STATUS_NO_MEMORY;
402 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
404 static const char zeros[16];
408 uint8 user_sess_key[16];
411 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
412 ntlmssp_state->workstation,
413 &ntlmssp_state->chal,
414 &ntlmssp_state->lm_resp,
415 &ntlmssp_state->nt_resp,
416 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
417 lm_key, user_sess_key,
418 &error_string, &unix_name);
420 if (NT_STATUS_IS_OK(nt_status)) {
421 if (memcmp(lm_key, zeros, 8) != 0) {
422 *lm_session_key = data_blob(NULL, 16);
423 memcpy(lm_session_key->data, lm_key, 8);
424 memset(lm_session_key->data+8, '\0', 8);
427 if (memcmp(user_sess_key, zeros, 16) != 0) {
428 *user_session_key = data_blob(user_sess_key, 16);
430 ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state->mem_ctx, unix_name);
431 SAFE_FREE(unix_name);
433 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
434 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
435 ntlmssp_state->domain, ntlmssp_state->user,
436 ntlmssp_state->workstation,
437 error_string ? error_string : "unknown error (NULL)"));
438 ntlmssp_state->auth_context = NULL;
443 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
446 uint8 lm_pw[16], nt_pw[16];
448 nt_lm_owf_gen (opt_password, nt_pw, lm_pw);
450 nt_status = ntlm_password_check(ntlmssp_state->mem_ctx,
451 &ntlmssp_state->chal,
452 &ntlmssp_state->lm_resp,
453 &ntlmssp_state->nt_resp,
457 ntlmssp_state->domain,
458 lm_pw, nt_pw, user_session_key, lm_session_key);
460 if (NT_STATUS_IS_OK(nt_status)) {
461 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state->mem_ctx,
462 "%s%c%s", ntlmssp_state->domain,
463 *lp_winbind_separator(),
464 ntlmssp_state->user);
466 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
467 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
468 nt_errstr(nt_status)));
469 ntlmssp_state->auth_context = NULL;
474 static NTSTATUS ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE **client_ntlmssp_state)
477 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
478 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
482 status = ntlmssp_client_start(client_ntlmssp_state);
484 if (!NT_STATUS_IS_OK(status)) {
485 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
487 ntlmssp_end(client_ntlmssp_state);
491 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
493 if (!NT_STATUS_IS_OK(status)) {
494 DEBUG(1, ("Could not set username: %s\n",
496 ntlmssp_end(client_ntlmssp_state);
500 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
502 if (!NT_STATUS_IS_OK(status)) {
503 DEBUG(1, ("Could not set domain: %s\n",
505 ntlmssp_end(client_ntlmssp_state);
509 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
511 if (!NT_STATUS_IS_OK(status)) {
512 DEBUG(1, ("Could not set password: %s\n",
514 ntlmssp_end(client_ntlmssp_state);
520 static NTSTATUS ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE **ntlmssp_state)
522 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
524 if (!NT_STATUS_IS_OK(status)) {
525 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
530 /* Have we been given a local password, or should we ask winbind? */
532 (*ntlmssp_state)->check_password = local_pw_check;
533 (*ntlmssp_state)->get_domain = lp_workgroup;
534 (*ntlmssp_state)->get_global_myname = global_myname;
536 (*ntlmssp_state)->check_password = winbind_pw_check;
537 (*ntlmssp_state)->get_domain = get_winbind_domain;
538 (*ntlmssp_state)->get_global_myname = get_winbind_netbios_name;
543 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
544 char *buf, int length)
546 static NTLMSSP_STATE *ntlmssp_state = NULL;
547 DATA_BLOB request, reply;
550 if (strlen(buf) < 2) {
551 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
552 x_fprintf(x_stdout, "BH\n");
556 if (strlen(buf) > 3) {
557 request = base64_decode_data_blob(buf + 3);
559 request = data_blob(NULL, 0);
562 if ((strncmp(buf, "PW ", 3) == 0)) {
563 /* The calling application wants us to use a local password (rather than winbindd) */
565 opt_password = strndup((const char *)request.data, request.length);
567 if (opt_password == NULL) {
568 DEBUG(1, ("Out of memory\n"));
569 x_fprintf(x_stdout, "BH\n");
570 data_blob_free(&request);
574 x_fprintf(x_stdout, "OK\n");
575 data_blob_free(&request);
579 if (strncmp(buf, "YR", 2) == 0) {
581 ntlmssp_end(&ntlmssp_state);
582 } else if (strncmp(buf, "KK", 2) == 0) {
585 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
586 x_fprintf(x_stdout, "BH\n");
590 if (!ntlmssp_state) {
591 if (!NT_STATUS_IS_OK(nt_status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
592 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
597 DEBUG(10, ("got NTLMSSP packet:\n"));
598 dump_data(10, (const char *)request.data, request.length);
600 nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
602 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
603 char *reply_base64 = base64_encode_data_blob(reply);
604 x_fprintf(x_stdout, "TT %s\n", reply_base64);
605 SAFE_FREE(reply_base64);
606 data_blob_free(&reply);
607 DEBUG(10, ("NTLMSSP challenge\n"));
608 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
609 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
610 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
612 ntlmssp_end(&ntlmssp_state);
613 } else if (!NT_STATUS_IS_OK(nt_status)) {
614 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
615 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
617 x_fprintf(x_stdout, "AF %s\n", (char *)ntlmssp_state->auth_context);
618 DEBUG(10, ("NTLMSSP OK!\n"));
621 data_blob_free(&request);
624 static void manage_client_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
625 char *buf, int length)
627 static NTLMSSP_STATE *ntlmssp_state = NULL;
628 DATA_BLOB request, reply;
632 if (strlen(buf) < 2) {
633 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
634 x_fprintf(x_stdout, "BH\n");
638 if (strlen(buf) > 3) {
639 request = base64_decode_data_blob(buf + 3);
641 request = data_blob(NULL, 0);
644 if (strncmp(buf, "PW ", 3) == 0) {
645 /* We asked for a password and obviously got it :-) */
647 opt_password = strndup((const char *)request.data, request.length);
649 if (opt_password == NULL) {
650 DEBUG(1, ("Out of memory\n"));
651 x_fprintf(x_stdout, "BH\n");
652 data_blob_free(&request);
656 x_fprintf(x_stdout, "OK\n");
657 data_blob_free(&request);
661 if (opt_password == NULL) {
663 /* Request a password from the calling process. After
664 sending it, the calling process should retry asking for the negotiate. */
666 DEBUG(10, ("Requesting password\n"));
667 x_fprintf(x_stdout, "PW\n");
671 if (strncmp(buf, "YR", 2) == 0) {
673 ntlmssp_end(&ntlmssp_state);
674 } else if (strncmp(buf, "TT", 2) == 0) {
677 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
678 x_fprintf(x_stdout, "BH\n");
682 if (!ntlmssp_state) {
683 if (!NT_STATUS_IS_OK(nt_status = ntlm_auth_start_ntlmssp_client(&ntlmssp_state))) {
684 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
690 DEBUG(10, ("got NTLMSSP packet:\n"));
691 dump_data(10, (const char *)request.data, request.length);
693 nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
695 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
696 char *reply_base64 = base64_encode_data_blob(reply);
698 x_fprintf(x_stdout, "YR %s\n", reply_base64);
700 x_fprintf(x_stdout, "KK %s\n", reply_base64);
702 SAFE_FREE(reply_base64);
703 data_blob_free(&reply);
704 DEBUG(10, ("NTLMSSP challenge\n"));
705 } else if (NT_STATUS_IS_OK(nt_status)) {
706 x_fprintf(x_stdout, "AF\n");
707 DEBUG(10, ("NTLMSSP OK!\n"));
709 ntlmssp_end(&ntlmssp_state);
711 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
712 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
714 ntlmssp_end(&ntlmssp_state);
717 data_blob_free(&request);
720 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
721 char *buf, int length)
726 pass=memchr(buf,' ',length);
728 DEBUG(2, ("Password not found. Denying access\n"));
729 x_fprintf(x_stdout, "ERR\n");
735 if (stdio_helper_mode == SQUID_2_5_BASIC) {
736 rfc1738_unescape(user);
737 rfc1738_unescape(pass);
740 if (check_plaintext_auth(user, pass, False)) {
741 x_fprintf(x_stdout, "OK\n");
743 x_fprintf(x_stdout, "ERR\n");
747 static void offer_gss_spnego_mechs(void) {
755 pstring myname_lower;
759 pstrcpy(myname_lower, global_myname());
760 strlower_m(myname_lower);
762 pstr_sprintf(principal, "%s$@%s", myname_lower, lp_realm());
764 /* Server negTokenInit (mech offerings) */
765 spnego.type = SPNEGO_NEG_TOKEN_INIT;
766 spnego.negTokenInit.mechTypes = smb_xmalloc(sizeof(char *) * 3);
768 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
769 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
770 spnego.negTokenInit.mechTypes[2] = NULL;
772 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
773 spnego.negTokenInit.mechTypes[1] = NULL;
777 spnego.negTokenInit.mechListMIC = data_blob(principal,
780 len = write_spnego_data(&token, &spnego);
781 free_spnego_data(&spnego);
784 DEBUG(1, ("Could not write SPNEGO data blob\n"));
785 x_fprintf(x_stdout, "BH\n");
789 reply_base64 = base64_encode_data_blob(token);
790 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
792 SAFE_FREE(reply_base64);
793 data_blob_free(&token);
794 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
798 static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode,
799 char *buf, int length)
801 static NTLMSSP_STATE *ntlmssp_state = NULL;
802 SPNEGO_DATA request, response;
810 const char *reply_code;
812 pstring reply_argument;
814 if (strlen(buf) < 2) {
816 if (ntlmssp_state != NULL) {
817 DEBUG(1, ("Request for initial SPNEGO request where "
818 "we already have a state\n"));
819 x_fprintf(x_stdout, "BH\n");
823 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
824 x_fprintf(x_stdout, "BH\n");
828 if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
830 /* Initial request, get the negTokenInit offering
833 offer_gss_spnego_mechs();
837 /* All subsequent requests are "KK" (Knock, Knock ;)) and have
838 a blob. This might be negTokenInit or negTokenTarg */
840 if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
841 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
842 x_fprintf(x_stdout, "BH\n");
846 token = base64_decode_data_blob(buf + 3);
847 len = read_spnego_data(token, &request);
848 data_blob_free(&token);
851 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
852 x_fprintf(x_stdout, "BH\n");
856 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
858 /* Second request from Client. This is where the
859 client offers its mechanism to use. */
861 if ( (request.negTokenInit.mechTypes == NULL) ||
862 (request.negTokenInit.mechTypes[0] == NULL) ) {
863 DEBUG(1, ("Client did not offer any mechanism"));
864 x_fprintf(x_stdout, "BH\n");
868 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
870 if ( request.negTokenInit.mechToken.data == NULL ) {
871 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
872 x_fprintf(x_stdout, "BH\n");
876 if ( ntlmssp_state != NULL ) {
877 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
878 "already got one\n"));
879 x_fprintf(x_stdout, "BH\n");
880 ntlmssp_end(&ntlmssp_state);
884 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
885 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
889 DEBUG(10, ("got NTLMSSP packet:\n"));
890 dump_data(10, (const char *)request.negTokenInit.mechToken.data,
891 request.negTokenInit.mechToken.length);
893 response.type = SPNEGO_NEG_TOKEN_TARG;
894 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
895 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
897 status = ntlmssp_update(ntlmssp_state,
898 request.negTokenInit.mechToken,
899 &response.negTokenTarg.responseToken);
903 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
908 DATA_BLOB session_key;
910 if ( request.negTokenInit.mechToken.data == NULL ) {
911 DEBUG(1, ("Client did not provide Kerberos data\n"));
912 x_fprintf(x_stdout, "BH\n");
916 response.type = SPNEGO_NEG_TOKEN_TARG;
917 response.negTokenTarg.supportedMech = strdup(OID_KERBEROS5_OLD);
918 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
919 response.negTokenTarg.responseToken = data_blob(NULL, 0);
921 status = ads_verify_ticket(lp_realm(),
922 &request.negTokenInit.mechToken,
923 &principal, &auth_data, &ap_rep,
926 /* Now in "principal" we have the name we are
929 if (NT_STATUS_IS_OK(status)) {
931 domain = strchr(principal, '@');
933 if (domain == NULL) {
934 DEBUG(1, ("Did not get a valid principal "
935 "from ads_verify_ticket\n"));
936 x_fprintf(x_stdout, "BH\n");
941 domain = strdup(domain);
942 user = strdup(principal);
944 data_blob_free(&ap_rep);
945 data_blob_free(&auth_data);
947 SAFE_FREE(principal);
954 if ( (request.negTokenTarg.supportedMech == NULL) ||
955 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
956 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
957 is the only one we support that sends this stuff */
958 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
959 request.negTokenTarg.supportedMech));
960 x_fprintf(x_stdout, "BH\n");
964 if (request.negTokenTarg.responseToken.data == NULL) {
965 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
966 x_fprintf(x_stdout, "BH\n");
970 status = ntlmssp_update(ntlmssp_state,
971 request.negTokenTarg.responseToken,
972 &response.negTokenTarg.responseToken);
974 response.type = SPNEGO_NEG_TOKEN_TARG;
975 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
976 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
978 if (NT_STATUS_IS_OK(status)) {
979 user = strdup(ntlmssp_state->user);
980 domain = strdup(ntlmssp_state->domain);
981 ntlmssp_end(&ntlmssp_state);
985 free_spnego_data(&request);
987 if (NT_STATUS_IS_OK(status)) {
988 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
990 pstr_sprintf(reply_argument, "%s\\%s", domain, user);
991 } else if (NT_STATUS_EQUAL(status,
992 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
993 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
995 pstr_sprintf(reply_argument, "*");
997 response.negTokenTarg.negResult = SPNEGO_REJECT;
999 pstrcpy(reply_argument, nt_errstr(status));
1005 len = write_spnego_data(&token, &response);
1006 free_spnego_data(&response);
1009 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1010 x_fprintf(x_stdout, "BH\n");
1014 reply_base64 = base64_encode_data_blob(token);
1016 x_fprintf(x_stdout, "%s %s %s\n",
1017 reply_code, reply_base64, reply_argument);
1019 SAFE_FREE(reply_base64);
1020 data_blob_free(&token);
1025 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
1027 static BOOL manage_client_ntlmssp_init(SPNEGO_DATA spnego)
1030 DATA_BLOB null_blob = data_blob(NULL, 0);
1031 DATA_BLOB to_server;
1032 char *to_server_base64;
1033 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1035 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1037 if (client_ntlmssp_state != NULL) {
1038 DEBUG(1, ("Request for initial SPNEGO request where "
1039 "we already have a state\n"));
1043 if (!client_ntlmssp_state) {
1044 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1045 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1051 if (opt_password == NULL) {
1053 /* Request a password from the calling process. After
1054 sending it, the calling process should retry with
1055 the negTokenInit. */
1057 DEBUG(10, ("Requesting password\n"));
1058 x_fprintf(x_stdout, "PW\n");
1062 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1063 spnego.negTokenInit.mechTypes = my_mechs;
1064 spnego.negTokenInit.reqFlags = 0;
1065 spnego.negTokenInit.mechListMIC = null_blob;
1067 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1068 &spnego.negTokenInit.mechToken);
1070 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1071 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
1072 nt_errstr(status)));
1073 ntlmssp_end(&client_ntlmssp_state);
1077 write_spnego_data(&to_server, &spnego);
1078 data_blob_free(&spnego.negTokenInit.mechToken);
1080 to_server_base64 = base64_encode_data_blob(to_server);
1081 data_blob_free(&to_server);
1082 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1083 SAFE_FREE(to_server_base64);
1087 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
1090 DATA_BLOB null_blob = data_blob(NULL, 0);
1092 DATA_BLOB to_server;
1093 char *to_server_base64;
1095 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1097 if (client_ntlmssp_state == NULL) {
1098 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1099 x_fprintf(x_stdout, "BH\n");
1100 ntlmssp_end(&client_ntlmssp_state);
1104 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1105 x_fprintf(x_stdout, "NA\n");
1106 ntlmssp_end(&client_ntlmssp_state);
1110 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1111 x_fprintf(x_stdout, "AF\n");
1112 ntlmssp_end(&client_ntlmssp_state);
1116 status = ntlmssp_update(client_ntlmssp_state,
1117 spnego.negTokenTarg.responseToken,
1120 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1121 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1122 "ntlmssp_client_update, got: %s\n",
1123 nt_errstr(status)));
1124 x_fprintf(x_stdout, "BH\n");
1125 data_blob_free(&request);
1126 ntlmssp_end(&client_ntlmssp_state);
1130 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1131 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1132 spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
1133 spnego.negTokenTarg.responseToken = request;
1134 spnego.negTokenTarg.mechListMIC = null_blob;
1136 write_spnego_data(&to_server, &spnego);
1137 data_blob_free(&request);
1139 to_server_base64 = base64_encode_data_blob(to_server);
1140 data_blob_free(&to_server);
1141 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1142 SAFE_FREE(to_server_base64);
1148 static BOOL manage_client_krb5_init(SPNEGO_DATA spnego)
1151 DATA_BLOB tkt, to_server;
1152 DATA_BLOB session_key_krb5;
1157 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1160 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1161 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1162 DEBUG(1, ("Did not get a principal for krb5\n"));
1166 principal = malloc(spnego.negTokenInit.mechListMIC.length+1);
1168 if (principal == NULL) {
1169 DEBUG(1, ("Could not malloc principal\n"));
1173 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1174 spnego.negTokenInit.mechListMIC.length);
1175 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1177 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5);
1183 /* Let's try to first get the TGT, for that we need a
1186 if (opt_password == NULL) {
1187 DEBUG(10, ("Requesting password\n"));
1188 x_fprintf(x_stdout, "PW\n");
1192 pstr_sprintf(user, "%s@%s", opt_username, opt_domain);
1194 if ((retval = kerberos_kinit_password(user, opt_password,
1196 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1197 x_fprintf(x_stdout, "NA\n");
1201 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5);
1204 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1208 data_blob_free(&session_key_krb5);
1212 reply.type = SPNEGO_NEG_TOKEN_INIT;
1213 reply.negTokenInit.mechTypes = my_mechs;
1214 reply.negTokenInit.reqFlags = 0;
1215 reply.negTokenInit.mechToken = tkt;
1216 reply.negTokenInit.mechListMIC = data_blob(NULL, 0);
1218 len = write_spnego_data(&to_server, &reply);
1219 data_blob_free(&tkt);
1222 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1226 reply_base64 = base64_encode_data_blob(to_server);
1227 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1229 SAFE_FREE(reply_base64);
1230 data_blob_free(&to_server);
1231 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1235 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
1237 switch (spnego.negTokenTarg.negResult) {
1238 case SPNEGO_ACCEPT_INCOMPLETE:
1239 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1240 x_fprintf(x_stdout, "BH\n");
1242 case SPNEGO_ACCEPT_COMPLETED:
1243 DEBUG(10, ("Accept completed\n"));
1244 x_fprintf(x_stdout, "AF\n");
1247 DEBUG(10, ("Rejected\n"));
1248 x_fprintf(x_stdout, "NA\n");
1251 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1252 x_fprintf(x_stdout, "AF\n");
1258 static void manage_gss_spnego_client_request(enum stdio_helper_mode stdio_helper_mode,
1259 char *buf, int length)
1265 if (strlen(buf) <= 3) {
1266 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1267 x_fprintf(x_stdout, "BH\n");
1271 request = base64_decode_data_blob(buf+3);
1273 if (strncmp(buf, "PW ", 3) == 0) {
1275 /* We asked for a password and obviously got it :-) */
1277 opt_password = strndup((const char *)request.data, request.length);
1279 if (opt_password == NULL) {
1280 DEBUG(1, ("Out of memory\n"));
1281 x_fprintf(x_stdout, "BH\n");
1282 data_blob_free(&request);
1286 x_fprintf(x_stdout, "OK\n");
1287 data_blob_free(&request);
1291 if ( (strncmp(buf, "TT ", 3) != 0) &&
1292 (strncmp(buf, "AF ", 3) != 0) &&
1293 (strncmp(buf, "NA ", 3) != 0) ) {
1294 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1295 x_fprintf(x_stdout, "BH\n");
1296 data_blob_free(&request);
1300 /* So we got a server challenge to generate a SPNEGO
1301 client-to-server request... */
1303 len = read_spnego_data(request, &spnego);
1304 data_blob_free(&request);
1307 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1308 x_fprintf(x_stdout, "BH\n");
1312 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1314 /* The server offers a list of mechanisms */
1316 const char **mechType = spnego.negTokenInit.mechTypes;
1318 while (*mechType != NULL) {
1321 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1322 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1323 if (manage_client_krb5_init(spnego))
1328 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1329 if (manage_client_ntlmssp_init(spnego))
1336 DEBUG(1, ("Server offered no compatible mechanism\n"));
1337 x_fprintf(x_stdout, "BH\n");
1341 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1343 if (spnego.negTokenTarg.supportedMech == NULL) {
1344 /* On accept/reject Windows does not send the
1345 mechanism anymore. Handle that here and
1346 shut down the mechanisms. */
1348 switch (spnego.negTokenTarg.negResult) {
1349 case SPNEGO_ACCEPT_COMPLETED:
1350 x_fprintf(x_stdout, "AF\n");
1353 x_fprintf(x_stdout, "NA\n");
1356 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1357 "unknown negResult: %d\n",
1358 spnego.negTokenTarg.negResult));
1359 x_fprintf(x_stdout, "BH\n");
1362 ntlmssp_end(&client_ntlmssp_state);
1366 if (strcmp(spnego.negTokenTarg.supportedMech,
1367 OID_NTLMSSP) == 0) {
1368 manage_client_ntlmssp_targ(spnego);
1373 if (strcmp(spnego.negTokenTarg.supportedMech,
1374 OID_KERBEROS5_OLD) == 0) {
1375 manage_client_krb5_targ(spnego);
1382 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1383 x_fprintf(x_stdout, "BH\n");
1387 free_spnego_data(&spnego);
1391 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
1392 char *buf, int length)
1394 char *request, *parameter;
1395 static DATA_BLOB challenge;
1396 static DATA_BLOB lm_response;
1397 static DATA_BLOB nt_response;
1398 static char *full_username;
1399 static char *username;
1400 static char *domain;
1401 static char *plaintext_password;
1402 static BOOL ntlm_server_1_user_session_key;
1403 static BOOL ntlm_server_1_lm_session_key;
1405 if (strequal(buf, ".")) {
1406 if (!full_username && !username) {
1407 x_fprintf(x_stdout, "Error: No username supplied!\n");
1408 } else if (plaintext_password) {
1409 /* handle this request as plaintext */
1410 if (!full_username) {
1411 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1412 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1416 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1417 x_fprintf(x_stdout, "Authenticated: Yes\n");
1419 x_fprintf(x_stdout, "Authenticated: No\n");
1421 } else if (!lm_response.data && !nt_response.data) {
1422 x_fprintf(x_stdout, "Error: No password supplied!\n");
1423 } else if (!challenge.data) {
1424 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1426 char *error_string = NULL;
1428 uchar user_session_key[16];
1431 if (full_username && !username) {
1433 fstring fstr_domain;
1435 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1436 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1437 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1439 SAFE_FREE(username);
1441 username = smb_xstrdup(fstr_user);
1442 domain = smb_xstrdup(fstr_domain);
1446 domain = smb_xstrdup(get_winbind_domain());
1449 if (ntlm_server_1_lm_session_key)
1450 flags |= WBFLAG_PAM_LMKEY;
1452 if (ntlm_server_1_user_session_key)
1453 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1455 if (!NT_STATUS_IS_OK(
1456 contact_winbind_auth_crap(username,
1468 x_fprintf(x_stdout, "Authenticated: No\n");
1469 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1470 SAFE_FREE(error_string);
1472 static char zeros[16];
1474 char *hex_user_session_key;
1476 x_fprintf(x_stdout, "Authenticated: Yes\n");
1478 if (ntlm_server_1_lm_session_key
1479 && (memcmp(zeros, lm_key,
1480 sizeof(lm_key)) != 0)) {
1481 hex_encode((const unsigned char *)lm_key,
1484 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1485 SAFE_FREE(hex_lm_key);
1488 if (ntlm_server_1_user_session_key
1489 && (memcmp(zeros, user_session_key,
1490 sizeof(user_session_key)) != 0)) {
1491 hex_encode((const unsigned char *)user_session_key,
1492 sizeof(user_session_key),
1493 &hex_user_session_key);
1494 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1495 SAFE_FREE(hex_user_session_key);
1499 /* clear out the state */
1500 challenge = data_blob(NULL, 0);
1501 nt_response = data_blob(NULL, 0);
1502 lm_response = data_blob(NULL, 0);
1503 SAFE_FREE(full_username);
1504 SAFE_FREE(username);
1506 SAFE_FREE(plaintext_password);
1507 ntlm_server_1_user_session_key = False;
1508 ntlm_server_1_lm_session_key = False;
1509 x_fprintf(x_stdout, ".\n");
1516 /* Indicates a base64 encoded structure */
1517 parameter = strstr_m(request, ":: ");
1519 parameter = strstr_m(request, ": ");
1522 DEBUG(0, ("Parameter not found!\n"));
1523 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1540 base64_decode_inplace(parameter);
1543 if (strequal(request, "LANMAN-Challenge")) {
1544 challenge = strhex_to_data_blob(parameter);
1545 if (challenge.length != 8) {
1546 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1548 (int)challenge.length);
1549 challenge = data_blob(NULL, 0);
1551 } else if (strequal(request, "NT-Response")) {
1552 nt_response = strhex_to_data_blob(parameter);
1553 if (nt_response.length < 24) {
1554 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1556 (int)opt_nt_response.length);
1557 nt_response = data_blob(NULL, 0);
1559 } else if (strequal(request, "LANMAN-Response")) {
1560 lm_response = strhex_to_data_blob(parameter);
1561 if (lm_response.length != 24) {
1562 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
1564 (int)lm_response.length);
1565 lm_response = data_blob(NULL, 0);
1567 } else if (strequal(request, "Password")) {
1568 plaintext_password = smb_xstrdup(parameter);
1569 } else if (strequal(request, "NT-Domain")) {
1570 domain = smb_xstrdup(parameter);
1571 } else if (strequal(request, "Username")) {
1572 username = smb_xstrdup(parameter);
1573 } else if (strequal(request, "Full-Username")) {
1574 full_username = smb_xstrdup(parameter);
1575 } else if (strequal(request, "Request-User-Session-Key")) {
1576 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
1577 } else if (strequal(request, "Request-LanMan-Session-Key")) {
1578 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
1580 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
1584 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn)
1586 char buf[SQUID_BUFFER_SIZE+1];
1591 /* this is not a typo - x_fgets doesn't work too well under squid */
1592 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1593 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
1594 strerror(ferror(stdin))));
1595 exit(1); /* BIIG buffer */
1598 c=memchr(buf,'\n',sizeof(buf)-1);
1607 DEBUG(2, ("Oversized message\n"));
1608 x_fprintf(x_stderr, "ERR\n");
1613 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
1615 if (buf[0] == '\0') {
1616 DEBUG(2, ("Invalid Request\n"));
1617 x_fprintf(x_stderr, "ERR\n");
1621 fn(helper_mode, buf, length);
1625 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
1626 /* initialize FDescs */
1627 x_setbuf(x_stdout, NULL);
1628 x_setbuf(x_stderr, NULL);
1630 manage_squid_request(stdio_mode, fn);
1635 /* Authenticate a user with a challenge/response */
1637 static BOOL check_auth_crap(void)
1642 char user_session_key[16];
1644 char *hex_user_session_key;
1646 static uint8 zeros[16];
1648 x_setbuf(x_stdout, NULL);
1651 flags |= WBFLAG_PAM_LMKEY;
1653 if (request_user_session_key)
1654 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1656 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
1658 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1664 (unsigned char *)lm_key,
1665 (unsigned char *)user_session_key,
1666 &error_string, NULL);
1668 if (!NT_STATUS_IS_OK(nt_status)) {
1669 x_fprintf(x_stdout, "%s (0x%x)\n",
1671 NT_STATUS_V(nt_status));
1672 SAFE_FREE(error_string);
1677 && (memcmp(zeros, lm_key,
1678 sizeof(lm_key)) != 0)) {
1679 hex_encode((const unsigned char *)lm_key,
1682 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
1683 SAFE_FREE(hex_lm_key);
1685 if (request_user_session_key
1686 && (memcmp(zeros, user_session_key,
1687 sizeof(user_session_key)) != 0)) {
1688 hex_encode((const unsigned char *)user_session_key,
1689 sizeof(user_session_key),
1690 &hex_user_session_key);
1691 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
1692 SAFE_FREE(hex_user_session_key);
1701 OPT_USERNAME = 1000,
1710 OPT_USER_SESSION_KEY,
1712 OPT_REQUIRE_MEMBERSHIP
1715 int main(int argc, const char **argv)
1718 static const char *helper_protocol;
1719 static int diagnostics;
1721 static const char *hex_challenge;
1722 static const char *hex_lm_response;
1723 static const char *hex_nt_response;
1727 /* NOTE: DO NOT change this interface without considering the implications!
1728 This is an external interface, which other programs will use to interact
1732 /* We do not use single-letter command abbreviations, because they harm future
1733 interface stability. */
1735 struct poptOption long_options[] = {
1737 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1738 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1739 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1740 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1741 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1742 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1743 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1744 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1745 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1746 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retreive User (NT) session key"},
1747 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
1748 { "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" },
1753 /* Samba client initialisation */
1757 /* Samba client initialisation */
1759 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1760 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1761 dyn_CONFIGFILE, strerror(errno));
1767 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1769 /* Parse command line options */
1772 poptPrintHelp(pc, stderr, 0);
1776 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1777 POPT_CONTEXT_KEEP_FIRST);
1779 while((opt = poptGetNextOpt(pc)) != -1) {
1782 opt_challenge = strhex_to_data_blob(hex_challenge);
1783 if (opt_challenge.length != 8) {
1784 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
1786 (int)opt_challenge.length);
1791 opt_lm_response = strhex_to_data_blob(hex_lm_response);
1792 if (opt_lm_response.length != 24) {
1793 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
1795 (int)opt_lm_response.length);
1801 opt_nt_response = strhex_to_data_blob(hex_nt_response);
1802 if (opt_nt_response.length < 24) {
1803 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
1805 (int)opt_nt_response.length);
1810 case OPT_REQUIRE_MEMBERSHIP:
1811 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
1812 require_membership_sid = require_membership_of;
1818 if (helper_protocol) {
1820 for (i=0; i<NUM_HELPER_MODES; i++) {
1821 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1822 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1826 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1828 for (i=0; i<NUM_HELPER_MODES; i++) {
1829 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1835 if (!opt_username) {
1836 x_fprintf(x_stderr, "username must be specified!\n\n");
1837 poptPrintHelp(pc, stderr, 0);
1841 if (opt_domain == NULL) {
1842 opt_domain = get_winbind_domain();
1845 if (opt_workstation == NULL) {
1846 opt_workstation = "";
1849 if (opt_challenge.length) {
1850 if (!check_auth_crap()) {
1856 if (!opt_password) {
1857 opt_password = getpass("password: ");
1861 if (!diagnose_ntlm_auth()) {
1867 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
1868 if (!check_plaintext_auth(user, opt_password, True)) {
1875 poptFreeContext(pc);