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
9 Copyright (C) Robert O'Callahan 2006 (added cached credential code).
10 Copyright (C) Kai Blin <kai@samba.org> 2008
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "utils/ntlm_auth.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../libcli/auth/spnego.h"
30 #include "../libcli/auth/ntlmssp.h"
32 #include <iniparser.h>
33 #include "../lib/crypto/arcfour.h"
34 #include "libads/kerberos_proto.h"
36 #ifndef PAM_WINBIND_CONFIG_FILE
37 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
40 #define WINBIND_KRB5_AUTH 0x00000080
43 #define DBGC_CLASS DBGC_WINBIND
45 #define INITIAL_BUFFER_SIZE 300
46 #define MAX_BUFFER_SIZE 630000
48 enum stdio_helper_mode {
56 NTLM_CHANGE_PASSWORD_1,
60 enum ntlm_auth_cli_state {
67 enum ntlm_auth_svr_state {
74 struct ntlm_auth_state {
76 enum stdio_helper_mode helper_mode;
77 enum ntlm_auth_cli_state cli_state;
78 enum ntlm_auth_svr_state svr_state;
79 struct ntlmssp_state *ntlmssp_state;
81 char *want_feature_list;
82 bool have_session_key;
83 DATA_BLOB session_key;
84 DATA_BLOB initial_message;
87 typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
90 static void manage_squid_basic_request (struct ntlm_auth_state *state,
91 char *buf, int length);
93 static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
94 char *buf, int length);
96 static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
97 char *buf, int length);
99 static void manage_gss_spnego_request (struct ntlm_auth_state *state,
100 char *buf, int length);
102 static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
103 char *buf, int length);
105 static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
106 char *buf, int length);
108 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
109 char *buf, int length);
111 static const struct {
112 enum stdio_helper_mode mode;
114 stdio_helper_function fn;
115 } stdio_helper_protocols[] = {
116 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
117 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
118 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
119 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
120 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
121 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
122 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
123 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
124 { NUM_HELPER_MODES, NULL, NULL}
127 const char *opt_username;
128 const char *opt_domain;
129 const char *opt_workstation;
130 const char *opt_password;
131 static DATA_BLOB opt_challenge;
132 static DATA_BLOB opt_lm_response;
133 static DATA_BLOB opt_nt_response;
134 static int request_lm_key;
135 static int request_user_session_key;
136 static int use_cached_creds;
138 static const char *require_membership_of;
139 static const char *require_membership_of_sid;
140 static const char *opt_pam_winbind_conf;
142 static char winbind_separator(void)
144 struct winbindd_response response;
151 ZERO_STRUCT(response);
153 /* Send off request */
155 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
156 NSS_STATUS_SUCCESS) {
157 d_printf("could not obtain winbind separator!\n");
158 return *lp_winbind_separator();
161 sep = response.data.info.winbind_separator;
165 d_printf("winbind separator was NULL!\n");
166 return *lp_winbind_separator();
172 const char *get_winbind_domain(void)
174 struct winbindd_response response;
176 static fstring winbind_domain;
177 if (*winbind_domain) {
178 return winbind_domain;
181 ZERO_STRUCT(response);
183 /* Send off request */
185 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
186 NSS_STATUS_SUCCESS) {
187 DEBUG(0, ("could not obtain winbind domain name!\n"));
188 return lp_workgroup();
191 fstrcpy(winbind_domain, response.data.domain_name);
193 return winbind_domain;
197 const char *get_winbind_netbios_name(void)
199 struct winbindd_response response;
201 static fstring winbind_netbios_name;
203 if (*winbind_netbios_name) {
204 return winbind_netbios_name;
207 ZERO_STRUCT(response);
209 /* Send off request */
211 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
212 NSS_STATUS_SUCCESS) {
213 DEBUG(0, ("could not obtain winbind netbios name!\n"));
214 return global_myname();
217 fstrcpy(winbind_netbios_name, response.data.netbios_name);
219 return winbind_netbios_name;
223 DATA_BLOB get_challenge(void)
225 static DATA_BLOB chal;
226 if (opt_challenge.length)
227 return opt_challenge;
229 chal = data_blob(NULL, 8);
231 generate_random_buffer(chal.data, chal.length);
235 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
236 form DOMAIN/user into a domain and a user */
238 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
242 char *p = strchr(domuser,winbind_separator());
249 fstrcpy(domain, domuser);
250 domain[PTR_DIFF(p, domuser)] = 0;
256 static bool get_require_membership_sid(void) {
257 struct winbindd_request request;
258 struct winbindd_response response;
260 if (!require_membership_of) {
264 if (require_membership_of_sid) {
268 /* Otherwise, ask winbindd for the name->sid request */
270 ZERO_STRUCT(request);
271 ZERO_STRUCT(response);
273 if (!parse_ntlm_auth_domain_user(require_membership_of,
274 request.data.name.dom_name,
275 request.data.name.name)) {
276 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
277 require_membership_of));
281 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
282 NSS_STATUS_SUCCESS) {
283 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
284 require_membership_of));
288 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
290 if (require_membership_of_sid)
297 * Get some configuration from pam_winbind.conf to see if we
298 * need to contact trusted domain
301 int get_pam_winbind_config()
304 dictionary *d = NULL;
306 if (!opt_pam_winbind_conf || !*opt_pam_winbind_conf) {
307 opt_pam_winbind_conf = PAM_WINBIND_CONFIG_FILE;
310 d = iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf));
316 if (iniparser_getboolean(d, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
317 ctrl |= WINBIND_KRB5_AUTH;
320 iniparser_freedict(d);
325 /* Authenticate a user with a plaintext password */
327 static bool check_plaintext_auth(const char *user, const char *pass,
328 bool stdout_diagnostics)
330 struct winbindd_request request;
331 struct winbindd_response response;
334 if (!get_require_membership_sid()) {
338 /* Send off request */
340 ZERO_STRUCT(request);
341 ZERO_STRUCT(response);
343 fstrcpy(request.data.auth.user, user);
344 fstrcpy(request.data.auth.pass, pass);
345 if (require_membership_of_sid) {
346 strlcpy(request.data.auth.require_membership_of_sid,
347 require_membership_of_sid,
348 sizeof(request.data.auth.require_membership_of_sid));
351 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
353 /* Display response */
355 if (stdout_diagnostics) {
356 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
357 d_printf("Reading winbind reply failed! (0x01)\n");
360 d_printf("%s: %s (0x%x)\n",
361 response.data.auth.nt_status_string,
362 response.data.auth.error_string,
363 response.data.auth.nt_status);
365 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
366 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
369 DEBUG(3, ("%s: %s (0x%x)\n",
370 response.data.auth.nt_status_string,
371 response.data.auth.error_string,
372 response.data.auth.nt_status));
375 return (result == NSS_STATUS_SUCCESS);
378 /* authenticate a user with an encrypted username/password */
380 NTSTATUS contact_winbind_auth_crap(const char *username,
382 const char *workstation,
383 const DATA_BLOB *challenge,
384 const DATA_BLOB *lm_response,
385 const DATA_BLOB *nt_response,
388 uint8 user_session_key[16],
394 struct winbindd_request request;
395 struct winbindd_response response;
397 if (!get_require_membership_sid()) {
398 return NT_STATUS_INVALID_PARAMETER;
401 ZERO_STRUCT(request);
402 ZERO_STRUCT(response);
404 request.flags = flags;
406 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
408 if (require_membership_of_sid)
409 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
411 fstrcpy(request.data.auth_crap.user, username);
412 fstrcpy(request.data.auth_crap.domain, domain);
414 fstrcpy(request.data.auth_crap.workstation,
417 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
419 if (lm_response && lm_response->length) {
420 memcpy(request.data.auth_crap.lm_resp,
422 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
423 request.data.auth_crap.lm_resp_len = lm_response->length;
426 if (nt_response && nt_response->length) {
427 if (nt_response->length > sizeof(request.data.auth_crap.nt_resp)) {
428 request.flags = request.flags | WBFLAG_BIG_NTLMV2_BLOB;
429 request.extra_len = nt_response->length;
430 request.extra_data.data = SMB_MALLOC_ARRAY(char, request.extra_len);
431 if (request.extra_data.data == NULL) {
432 return NT_STATUS_NO_MEMORY;
434 memcpy(request.extra_data.data, nt_response->data,
435 nt_response->length);
438 memcpy(request.data.auth_crap.nt_resp,
439 nt_response->data, nt_response->length);
441 request.data.auth_crap.nt_resp_len = nt_response->length;
444 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
445 SAFE_FREE(request.extra_data.data);
447 /* Display response */
449 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
450 nt_status = NT_STATUS_UNSUCCESSFUL;
452 *error_string = smb_xstrdup("Reading winbind reply failed!");
453 winbindd_free_response(&response);
457 nt_status = (NT_STATUS(response.data.auth.nt_status));
458 if (!NT_STATUS_IS_OK(nt_status)) {
460 *error_string = smb_xstrdup(response.data.auth.error_string);
461 winbindd_free_response(&response);
465 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
466 memcpy(lm_key, response.data.auth.first_8_lm_hash,
467 sizeof(response.data.auth.first_8_lm_hash));
469 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
470 memcpy(user_session_key, response.data.auth.user_session_key,
471 sizeof(response.data.auth.user_session_key));
474 if (flags & WBFLAG_PAM_UNIX_NAME) {
475 *unix_name = SMB_STRDUP(response.data.auth.unix_username);
477 winbindd_free_response(&response);
478 return NT_STATUS_NO_MEMORY;
482 winbindd_free_response(&response);
486 /* contact server to change user password using auth crap */
487 static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
489 const DATA_BLOB new_nt_pswd,
490 const DATA_BLOB old_nt_hash_enc,
491 const DATA_BLOB new_lm_pswd,
492 const DATA_BLOB old_lm_hash_enc,
497 struct winbindd_request request;
498 struct winbindd_response response;
500 if (!get_require_membership_sid())
503 *error_string = smb_xstrdup("Can't get membership sid.");
504 return NT_STATUS_INVALID_PARAMETER;
507 ZERO_STRUCT(request);
508 ZERO_STRUCT(response);
511 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
513 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
515 if(new_nt_pswd.length)
517 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
518 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
521 if(old_nt_hash_enc.length)
523 memcpy(request.data.chng_pswd_auth_crap.old_nt_hash_enc, old_nt_hash_enc.data, sizeof(request.data.chng_pswd_auth_crap.old_nt_hash_enc));
524 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
527 if(new_lm_pswd.length)
529 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
530 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
533 if(old_lm_hash_enc.length)
535 memcpy(request.data.chng_pswd_auth_crap.old_lm_hash_enc, old_lm_hash_enc.data, sizeof(request.data.chng_pswd_auth_crap.old_lm_hash_enc));
536 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
539 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
541 /* Display response */
543 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
545 nt_status = NT_STATUS_UNSUCCESSFUL;
547 *error_string = smb_xstrdup("Reading winbind reply failed!");
548 winbindd_free_response(&response);
552 nt_status = (NT_STATUS(response.data.auth.nt_status));
553 if (!NT_STATUS_IS_OK(nt_status))
556 *error_string = smb_xstrdup(response.data.auth.error_string);
557 winbindd_free_response(&response);
561 winbindd_free_response(&response);
566 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
568 static const char zeros[16] = { 0, };
570 char *error_string = NULL;
572 uint8 user_sess_key[16];
573 char *unix_name = NULL;
575 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
576 ntlmssp_state->client.netbios_name,
577 &ntlmssp_state->chal,
578 &ntlmssp_state->lm_resp,
579 &ntlmssp_state->nt_resp,
580 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
581 lm_key, user_sess_key,
582 &error_string, &unix_name);
584 if (NT_STATUS_IS_OK(nt_status)) {
585 if (memcmp(lm_key, zeros, 8) != 0) {
586 *lm_session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
587 memcpy(lm_session_key->data, lm_key, 8);
588 memset(lm_session_key->data+8, '\0', 8);
591 if (memcmp(user_sess_key, zeros, 16) != 0) {
592 *user_session_key = data_blob_talloc(ntlmssp_state, user_sess_key, 16);
594 ntlmssp_state->callback_private = talloc_strdup(ntlmssp_state,
597 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
598 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
599 ntlmssp_state->domain, ntlmssp_state->user,
600 ntlmssp_state->client.netbios_name,
601 error_string ? error_string : "unknown error (NULL)"));
602 ntlmssp_state->callback_private = NULL;
605 SAFE_FREE(error_string);
606 SAFE_FREE(unix_name);
610 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
613 struct samr_Password lm_pw, nt_pw;
615 nt_lm_owf_gen (opt_password, nt_pw.hash, lm_pw.hash);
617 nt_status = ntlm_password_check(ntlmssp_state,
619 &ntlmssp_state->chal,
620 &ntlmssp_state->lm_resp,
621 &ntlmssp_state->nt_resp,
624 ntlmssp_state->domain,
625 &lm_pw, &nt_pw, user_session_key, lm_session_key);
627 if (NT_STATUS_IS_OK(nt_status)) {
628 ntlmssp_state->callback_private = talloc_asprintf(ntlmssp_state,
629 "%s%c%s", ntlmssp_state->domain,
630 *lp_winbind_separator(),
631 ntlmssp_state->user);
633 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
634 ntlmssp_state->domain, ntlmssp_state->user,
635 ntlmssp_state->client.netbios_name,
636 nt_errstr(nt_status)));
637 ntlmssp_state->callback_private = NULL;
642 static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntlmssp_state)
645 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
646 status = NT_STATUS_UNSUCCESSFUL;
647 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
648 return NT_STATUS_INVALID_PARAMETER;
651 status = ntlmssp_client_start(NULL,
654 lp_client_ntlmv2_auth(),
655 client_ntlmssp_state);
657 if (!NT_STATUS_IS_OK(status)) {
658 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
660 TALLOC_FREE(*client_ntlmssp_state);
664 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
666 if (!NT_STATUS_IS_OK(status)) {
667 DEBUG(1, ("Could not set username: %s\n",
669 TALLOC_FREE(*client_ntlmssp_state);
673 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
675 if (!NT_STATUS_IS_OK(status)) {
676 DEBUG(1, ("Could not set domain: %s\n",
678 TALLOC_FREE(*client_ntlmssp_state);
683 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
685 if (!NT_STATUS_IS_OK(status)) {
686 DEBUG(1, ("Could not set password: %s\n",
688 TALLOC_FREE(*client_ntlmssp_state);
696 static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_state)
699 const char *netbios_name;
700 const char *netbios_domain;
701 const char *dns_name;
703 bool is_standalone = false;
706 netbios_name = global_myname();
707 netbios_domain = lp_workgroup();
709 netbios_name = get_winbind_netbios_name();
710 netbios_domain = get_winbind_domain();
712 /* This should be a 'netbios domain -> DNS domain' mapping */
713 dns_domain = get_mydnsdomname(talloc_tos());
715 strlower_m(dns_domain);
717 dns_name = get_mydnsfullname();
719 status = ntlmssp_server_start(NULL,
726 if (!NT_STATUS_IS_OK(status)) {
727 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
732 /* Have we been given a local password, or should we ask winbind? */
734 (*ntlmssp_state)->check_password = local_pw_check;
736 (*ntlmssp_state)->check_password = winbind_pw_check;
741 /*******************************************************************
742 Used by firefox to drive NTLM auth to IIS servers.
743 *******************************************************************/
745 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
748 struct winbindd_request wb_request;
749 struct winbindd_response wb_response;
753 /* get winbindd to do the ntlmssp step on our behalf */
754 ZERO_STRUCT(wb_request);
755 ZERO_STRUCT(wb_response);
758 * This is tricky here. If we set krb5_auth in pam_winbind.conf
759 * creds for users in trusted domain will be stored the winbindd
760 * child of the trusted domain. If we ask the primary domain for
761 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
762 * domain's child for ccache_ntlm_auth. that is to say, we have to
763 * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
765 ctrl = get_pam_winbind_config();
767 if (ctrl | WINBIND_KRB5_AUTH) {
768 wb_request.flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
771 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
772 "%s%c%s", opt_domain, winbind_separator(), opt_username);
773 wb_request.data.ccache_ntlm_auth.uid = geteuid();
774 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
775 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
776 wb_request.extra_len = initial_msg.length + challenge_msg.length;
778 if (wb_request.extra_len > 0) {
779 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
780 if (wb_request.extra_data.data == NULL) {
781 return NT_STATUS_NO_MEMORY;
784 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
785 memcpy(wb_request.extra_data.data + initial_msg.length,
786 challenge_msg.data, challenge_msg.length);
789 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
790 SAFE_FREE(wb_request.extra_data.data);
792 if (result != NSS_STATUS_SUCCESS) {
793 winbindd_free_response(&wb_response);
794 return NT_STATUS_UNSUCCESSFUL;
798 *reply = data_blob(wb_response.extra_data.data,
799 wb_response.data.ccache_ntlm_auth.auth_blob_len);
800 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
801 reply->data == NULL) {
802 winbindd_free_response(&wb_response);
803 return NT_STATUS_NO_MEMORY;
807 winbindd_free_response(&wb_response);
808 return NT_STATUS_MORE_PROCESSING_REQUIRED;
811 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
812 char *buf, int length)
814 DATA_BLOB request, reply;
817 if (strlen(buf) < 2) {
818 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
819 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
823 if (strlen(buf) > 3) {
824 if(strncmp(buf, "SF ", 3) == 0){
825 DEBUG(10, ("Setting flags to negotioate\n"));
826 TALLOC_FREE(state->want_feature_list);
827 state->want_feature_list = talloc_strdup(state->mem_ctx,
829 x_fprintf(x_stdout, "OK\n");
832 request = base64_decode_data_blob(buf + 3);
834 request = data_blob_null;
837 if ((strncmp(buf, "PW ", 3) == 0)) {
838 /* The calling application wants us to use a local password
839 * (rather than winbindd) */
841 opt_password = SMB_STRNDUP((const char *)request.data,
844 if (opt_password == NULL) {
845 DEBUG(1, ("Out of memory\n"));
846 x_fprintf(x_stdout, "BH Out of memory\n");
847 data_blob_free(&request);
851 x_fprintf(x_stdout, "OK\n");
852 data_blob_free(&request);
856 if (strncmp(buf, "YR", 2) == 0) {
857 if (state->ntlmssp_state)
858 TALLOC_FREE(state->ntlmssp_state);
859 state->svr_state = SERVER_INITIAL;
860 } else if (strncmp(buf, "KK", 2) == 0) {
861 /* No special preprocessing required */
862 } else if (strncmp(buf, "GF", 2) == 0) {
863 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
865 if (state->svr_state == SERVER_FINISHED) {
866 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
869 x_fprintf(x_stdout, "BH\n");
871 data_blob_free(&request);
873 } else if (strncmp(buf, "GK", 2) == 0) {
874 DEBUG(10, ("Requested NTLMSSP session key\n"));
875 if(state->have_session_key) {
876 char *key64 = base64_encode_data_blob(state->mem_ctx,
878 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
881 x_fprintf(x_stdout, "BH\n");
884 data_blob_free(&request);
887 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
888 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
892 if (!state->ntlmssp_state) {
893 nt_status = ntlm_auth_start_ntlmssp_server(
894 &state->ntlmssp_state);
895 if (!NT_STATUS_IS_OK(nt_status)) {
896 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
899 ntlmssp_want_feature_list(state->ntlmssp_state,
900 state->want_feature_list);
903 DEBUG(10, ("got NTLMSSP packet:\n"));
904 dump_data(10, request.data, request.length);
906 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
908 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
909 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
911 x_fprintf(x_stdout, "TT %s\n", reply_base64);
912 TALLOC_FREE(reply_base64);
913 data_blob_free(&reply);
914 state->svr_state = SERVER_CHALLENGE;
915 DEBUG(10, ("NTLMSSP challenge\n"));
916 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
917 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
918 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
920 TALLOC_FREE(state->ntlmssp_state);
921 } else if (!NT_STATUS_IS_OK(nt_status)) {
922 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
923 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
925 x_fprintf(x_stdout, "AF %s\n",
926 (char *)state->ntlmssp_state->callback_private);
927 DEBUG(10, ("NTLMSSP OK!\n"));
929 if(state->have_session_key)
930 data_blob_free(&state->session_key);
931 state->session_key = data_blob(
932 state->ntlmssp_state->session_key.data,
933 state->ntlmssp_state->session_key.length);
934 state->neg_flags = state->ntlmssp_state->neg_flags;
935 state->have_session_key = true;
936 state->svr_state = SERVER_FINISHED;
939 data_blob_free(&request);
942 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
943 char *buf, int length)
945 DATA_BLOB request, reply;
948 if (!opt_username || !*opt_username) {
949 x_fprintf(x_stderr, "username must be specified!\n\n");
953 if (strlen(buf) < 2) {
954 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
955 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
959 if (strlen(buf) > 3) {
960 if(strncmp(buf, "SF ", 3) == 0) {
961 DEBUG(10, ("Looking for flags to negotiate\n"));
962 talloc_free(state->want_feature_list);
963 state->want_feature_list = talloc_strdup(state->mem_ctx,
965 x_fprintf(x_stdout, "OK\n");
968 request = base64_decode_data_blob(buf + 3);
970 request = data_blob_null;
973 if (strncmp(buf, "PW ", 3) == 0) {
974 /* We asked for a password and obviously got it :-) */
976 opt_password = SMB_STRNDUP((const char *)request.data,
979 if (opt_password == NULL) {
980 DEBUG(1, ("Out of memory\n"));
981 x_fprintf(x_stdout, "BH Out of memory\n");
982 data_blob_free(&request);
986 x_fprintf(x_stdout, "OK\n");
987 data_blob_free(&request);
991 if (!state->ntlmssp_state && use_cached_creds) {
992 /* check whether cached credentials are usable. */
993 DATA_BLOB empty_blob = data_blob_null;
995 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
996 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
997 /* failed to use cached creds */
998 use_cached_creds = False;
1002 if (opt_password == NULL && !use_cached_creds) {
1003 /* Request a password from the calling process. After
1004 sending it, the calling process should retry asking for the
1007 DEBUG(10, ("Requesting password\n"));
1008 x_fprintf(x_stdout, "PW\n");
1012 if (strncmp(buf, "YR", 2) == 0) {
1013 if (state->ntlmssp_state)
1014 TALLOC_FREE(state->ntlmssp_state);
1015 state->cli_state = CLIENT_INITIAL;
1016 } else if (strncmp(buf, "TT", 2) == 0) {
1017 /* No special preprocessing required */
1018 } else if (strncmp(buf, "GF", 2) == 0) {
1019 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1021 if(state->cli_state == CLIENT_FINISHED) {
1022 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
1025 x_fprintf(x_stdout, "BH\n");
1028 data_blob_free(&request);
1030 } else if (strncmp(buf, "GK", 2) == 0 ) {
1031 DEBUG(10, ("Requested session key\n"));
1033 if(state->cli_state == CLIENT_FINISHED) {
1034 char *key64 = base64_encode_data_blob(state->mem_ctx,
1035 state->session_key);
1036 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
1040 x_fprintf(x_stdout, "BH\n");
1043 data_blob_free(&request);
1046 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
1047 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
1051 if (!state->ntlmssp_state) {
1052 nt_status = ntlm_auth_start_ntlmssp_client(
1053 &state->ntlmssp_state);
1054 if (!NT_STATUS_IS_OK(nt_status)) {
1055 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1058 ntlmssp_want_feature_list(state->ntlmssp_state,
1059 state->want_feature_list);
1060 state->initial_message = data_blob_null;
1063 DEBUG(10, ("got NTLMSSP packet:\n"));
1064 dump_data(10, request.data, request.length);
1066 if (use_cached_creds && !opt_password &&
1067 (state->cli_state == CLIENT_RESPONSE)) {
1068 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
1071 nt_status = ntlmssp_update(state->ntlmssp_state, request,
1075 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1076 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
1078 if (state->cli_state == CLIENT_INITIAL) {
1079 x_fprintf(x_stdout, "YR %s\n", reply_base64);
1080 state->initial_message = reply;
1081 state->cli_state = CLIENT_RESPONSE;
1083 x_fprintf(x_stdout, "KK %s\n", reply_base64);
1084 data_blob_free(&reply);
1086 TALLOC_FREE(reply_base64);
1087 DEBUG(10, ("NTLMSSP challenge\n"));
1088 } else if (NT_STATUS_IS_OK(nt_status)) {
1089 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1091 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1092 TALLOC_FREE(reply_base64);
1094 if(state->have_session_key)
1095 data_blob_free(&state->session_key);
1097 state->session_key = data_blob(
1098 state->ntlmssp_state->session_key.data,
1099 state->ntlmssp_state->session_key.length);
1100 state->neg_flags = state->ntlmssp_state->neg_flags;
1101 state->have_session_key = true;
1103 DEBUG(10, ("NTLMSSP OK!\n"));
1104 state->cli_state = CLIENT_FINISHED;
1105 if (state->ntlmssp_state)
1106 TALLOC_FREE(state->ntlmssp_state);
1108 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1109 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1110 state->cli_state = CLIENT_ERROR;
1111 if (state->ntlmssp_state)
1112 TALLOC_FREE(state->ntlmssp_state);
1115 data_blob_free(&request);
1118 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1119 char *buf, int length)
1124 pass=(char *)memchr(buf,' ',length);
1126 DEBUG(2, ("Password not found. Denying access\n"));
1127 x_fprintf(x_stdout, "ERR\n");
1133 if (state->helper_mode == SQUID_2_5_BASIC) {
1134 rfc1738_unescape(user);
1135 rfc1738_unescape(pass);
1138 if (check_plaintext_auth(user, pass, False)) {
1139 x_fprintf(x_stdout, "OK\n");
1141 x_fprintf(x_stdout, "ERR\n");
1145 static void offer_gss_spnego_mechs(void) {
1148 struct spnego_data spnego;
1151 TALLOC_CTX *ctx = talloc_tos();
1155 ZERO_STRUCT(spnego);
1157 myname_lower = talloc_strdup(ctx, global_myname());
1158 if (!myname_lower) {
1161 strlower_m(myname_lower);
1163 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1168 /* Server negTokenInit (mech offerings) */
1169 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1170 spnego.negTokenInit.mechTypes = talloc_array(ctx, const char *, 2);
1172 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_KERBEROS5_OLD);
1173 spnego.negTokenInit.mechTypes[1] = talloc_strdup(ctx, OID_NTLMSSP);
1174 spnego.negTokenInit.mechTypes[2] = NULL;
1176 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_NTLMSSP);
1177 spnego.negTokenInit.mechTypes[1] = NULL;
1181 spnego.negTokenInit.mechListMIC = data_blob_talloc(ctx, principal,
1184 len = spnego_write_data(ctx, &token, &spnego);
1185 spnego_free_data(&spnego);
1188 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1189 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1193 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1194 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1196 TALLOC_FREE(reply_base64);
1197 data_blob_free(&token);
1198 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1202 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1203 char *buf, int length)
1205 static struct ntlmssp_state *ntlmssp_state = NULL;
1206 struct spnego_data request, response;
1210 TALLOC_CTX *ctx = talloc_tos();
1213 char *domain = NULL;
1215 const char *reply_code;
1217 char *reply_argument = NULL;
1219 if (strlen(buf) < 2) {
1220 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1221 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1225 if (strncmp(buf, "YR", 2) == 0) {
1227 TALLOC_FREE(ntlmssp_state);
1228 } else if (strncmp(buf, "KK", 2) == 0) {
1231 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1232 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1236 if ( (strlen(buf) == 2)) {
1238 /* no client data, get the negTokenInit offering
1241 offer_gss_spnego_mechs();
1245 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1247 if (strlen(buf) <= 3) {
1248 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1249 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1253 token = base64_decode_data_blob(buf + 3);
1254 len = spnego_read_data(ctx, token, &request);
1255 data_blob_free(&token);
1258 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
1259 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1263 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1265 /* Second request from Client. This is where the
1266 client offers its mechanism to use. */
1268 if ( (request.negTokenInit.mechTypes == NULL) ||
1269 (request.negTokenInit.mechTypes[0] == NULL) ) {
1270 DEBUG(1, ("Client did not offer any mechanism"));
1271 x_fprintf(x_stdout, "BH Client did not offer any "
1276 status = NT_STATUS_UNSUCCESSFUL;
1277 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1279 if ( request.negTokenInit.mechToken.data == NULL ) {
1280 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1281 x_fprintf(x_stdout, "BH Client did not provide "
1286 if ( ntlmssp_state != NULL ) {
1287 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1288 "already got one\n"));
1289 x_fprintf(x_stdout, "BH Client wants a new "
1290 "NTLMSSP challenge, but "
1291 "already got one\n");
1292 TALLOC_FREE(ntlmssp_state);
1296 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1297 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1301 DEBUG(10, ("got NTLMSSP packet:\n"));
1302 dump_data(10, request.negTokenInit.mechToken.data,
1303 request.negTokenInit.mechToken.length);
1305 response.type = SPNEGO_NEG_TOKEN_TARG;
1306 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
1307 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1309 status = ntlmssp_update(ntlmssp_state,
1310 request.negTokenInit.mechToken,
1311 &response.negTokenTarg.responseToken);
1315 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1317 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1320 DATA_BLOB session_key;
1321 struct PAC_LOGON_INFO *logon_info = NULL;
1323 if ( request.negTokenInit.mechToken.data == NULL ) {
1324 DEBUG(1, ("Client did not provide Kerberos data\n"));
1325 x_fprintf(x_stdout, "BH Client did not provide "
1330 response.type = SPNEGO_NEG_TOKEN_TARG;
1331 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_KERBEROS5_OLD);
1332 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1333 response.negTokenTarg.responseToken = data_blob_talloc(ctx, NULL, 0);
1335 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1336 &request.negTokenInit.mechToken,
1337 &principal, &logon_info, &ap_rep,
1338 &session_key, True);
1340 /* Now in "principal" we have the name we are
1341 authenticated as. */
1343 if (NT_STATUS_IS_OK(status)) {
1345 domain = strchr_m(principal, '@');
1347 if (domain == NULL) {
1348 DEBUG(1, ("Did not get a valid principal "
1349 "from ads_verify_ticket\n"));
1350 x_fprintf(x_stdout, "BH Did not get a "
1351 "valid principal from "
1352 "ads_verify_ticket\n");
1357 domain = SMB_STRDUP(domain);
1358 user = SMB_STRDUP(principal);
1360 data_blob_free(&ap_rep);
1363 TALLOC_FREE(mem_ctx);
1369 if ( (request.negTokenTarg.supportedMech == NULL) ||
1370 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1371 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1372 is the only one we support that sends this stuff */
1373 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1374 request.negTokenTarg.supportedMech));
1375 x_fprintf(x_stdout, "BH Got a negTokenTarg for "
1376 "something non-NTLMSSP\n");
1380 if (request.negTokenTarg.responseToken.data == NULL) {
1381 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1382 x_fprintf(x_stdout, "BH Got a negTokenTarg without a "
1383 "responseToken!\n");
1387 status = ntlmssp_update(ntlmssp_state,
1388 request.negTokenTarg.responseToken,
1389 &response.negTokenTarg.responseToken);
1391 response.type = SPNEGO_NEG_TOKEN_TARG;
1392 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
1393 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1395 if (NT_STATUS_IS_OK(status)) {
1396 user = SMB_STRDUP(ntlmssp_state->user);
1397 domain = SMB_STRDUP(ntlmssp_state->domain);
1398 TALLOC_FREE(ntlmssp_state);
1402 spnego_free_data(&request);
1404 if (NT_STATUS_IS_OK(status)) {
1405 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1407 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1408 } else if (NT_STATUS_EQUAL(status,
1409 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1410 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1412 reply_argument = talloc_strdup(ctx, "*");
1414 response.negTokenTarg.negResult = SPNEGO_REJECT;
1416 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1419 if (!reply_argument) {
1420 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1421 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1428 len = spnego_write_data(ctx, &token, &response);
1429 spnego_free_data(&response);
1432 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1433 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1437 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1439 x_fprintf(x_stdout, "%s %s %s\n",
1440 reply_code, reply_base64, reply_argument);
1442 TALLOC_FREE(reply_base64);
1443 data_blob_free(&token);
1448 static struct ntlmssp_state *client_ntlmssp_state = NULL;
1450 static bool manage_client_ntlmssp_init(struct spnego_data spnego)
1453 DATA_BLOB null_blob = data_blob_null;
1454 DATA_BLOB to_server;
1455 char *to_server_base64;
1456 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1457 TALLOC_CTX *ctx = talloc_tos();
1459 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1461 if (client_ntlmssp_state != NULL) {
1462 DEBUG(1, ("Request for initial SPNEGO request where "
1463 "we already have a state\n"));
1467 if (!client_ntlmssp_state) {
1468 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1469 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1475 if (opt_password == NULL) {
1477 /* Request a password from the calling process. After
1478 sending it, the calling process should retry with
1479 the negTokenInit. */
1481 DEBUG(10, ("Requesting password\n"));
1482 x_fprintf(x_stdout, "PW\n");
1486 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1487 spnego.negTokenInit.mechTypes = my_mechs;
1488 spnego.negTokenInit.reqFlags = data_blob_null;
1489 spnego.negTokenInit.reqFlagsPadding = 0;
1490 spnego.negTokenInit.mechListMIC = null_blob;
1492 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1493 &spnego.negTokenInit.mechToken);
1495 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1496 NT_STATUS_IS_OK(status)) ) {
1497 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1498 nt_errstr(status)));
1499 TALLOC_FREE(client_ntlmssp_state);
1503 spnego_write_data(ctx, &to_server, &spnego);
1504 data_blob_free(&spnego.negTokenInit.mechToken);
1506 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1507 data_blob_free(&to_server);
1508 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1509 TALLOC_FREE(to_server_base64);
1513 static void manage_client_ntlmssp_targ(struct spnego_data spnego)
1516 DATA_BLOB null_blob = data_blob_null;
1518 DATA_BLOB to_server;
1519 char *to_server_base64;
1520 TALLOC_CTX *ctx = talloc_tos();
1522 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1524 if (client_ntlmssp_state == NULL) {
1525 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1526 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1530 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1531 x_fprintf(x_stdout, "NA\n");
1532 TALLOC_FREE(client_ntlmssp_state);
1536 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1537 x_fprintf(x_stdout, "AF\n");
1538 TALLOC_FREE(client_ntlmssp_state);
1542 status = ntlmssp_update(client_ntlmssp_state,
1543 spnego.negTokenTarg.responseToken,
1546 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1547 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1548 "ntlmssp_client_update, got: %s\n",
1549 nt_errstr(status)));
1550 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1551 "ntlmssp_client_update\n");
1552 data_blob_free(&request);
1553 TALLOC_FREE(client_ntlmssp_state);
1557 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1558 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1559 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1560 spnego.negTokenTarg.responseToken = request;
1561 spnego.negTokenTarg.mechListMIC = null_blob;
1563 spnego_write_data(ctx, &to_server, &spnego);
1564 data_blob_free(&request);
1566 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1567 data_blob_free(&to_server);
1568 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1569 TALLOC_FREE(to_server_base64);
1575 static bool manage_client_krb5_init(struct spnego_data spnego)
1578 DATA_BLOB tkt, to_server;
1579 DATA_BLOB session_key_krb5 = data_blob_null;
1580 struct spnego_data reply;
1584 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1586 TALLOC_CTX *ctx = talloc_tos();
1588 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1589 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1590 DEBUG(1, ("Did not get a principal for krb5\n"));
1594 principal = (char *)SMB_MALLOC(
1595 spnego.negTokenInit.mechListMIC.length+1);
1597 if (principal == NULL) {
1598 DEBUG(1, ("Could not malloc principal\n"));
1602 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1603 spnego.negTokenInit.mechListMIC.length);
1604 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1606 retval = cli_krb5_get_ticket(ctx, principal, 0,
1607 &tkt, &session_key_krb5,
1608 0, NULL, NULL, NULL);
1612 /* Let's try to first get the TGT, for that we need a
1615 if (opt_password == NULL) {
1616 DEBUG(10, ("Requesting password\n"));
1617 x_fprintf(x_stdout, "PW\n");
1621 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1626 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1627 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1631 retval = cli_krb5_get_ticket(ctx, principal, 0,
1632 &tkt, &session_key_krb5,
1633 0, NULL, NULL, NULL);
1635 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1640 data_blob_free(&session_key_krb5);
1644 reply.type = SPNEGO_NEG_TOKEN_INIT;
1645 reply.negTokenInit.mechTypes = my_mechs;
1646 reply.negTokenInit.reqFlags = data_blob_null;
1647 reply.negTokenInit.reqFlagsPadding = 0;
1648 reply.negTokenInit.mechToken = tkt;
1649 reply.negTokenInit.mechListMIC = data_blob_null;
1651 len = spnego_write_data(ctx, &to_server, &reply);
1652 data_blob_free(&tkt);
1655 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1659 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1660 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1662 TALLOC_FREE(reply_base64);
1663 data_blob_free(&to_server);
1664 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1668 static void manage_client_krb5_targ(struct spnego_data spnego)
1670 switch (spnego.negTokenTarg.negResult) {
1671 case SPNEGO_ACCEPT_INCOMPLETE:
1672 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1673 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1674 "ACCEPT_INCOMPLETE\n");
1676 case SPNEGO_ACCEPT_COMPLETED:
1677 DEBUG(10, ("Accept completed\n"));
1678 x_fprintf(x_stdout, "AF\n");
1681 DEBUG(10, ("Rejected\n"));
1682 x_fprintf(x_stdout, "NA\n");
1685 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1686 x_fprintf(x_stdout, "AF\n");
1692 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1693 char *buf, int length)
1696 struct spnego_data spnego;
1698 TALLOC_CTX *ctx = talloc_tos();
1700 if (!opt_username || !*opt_username) {
1701 x_fprintf(x_stderr, "username must be specified!\n\n");
1705 if (strlen(buf) <= 3) {
1706 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1707 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1711 request = base64_decode_data_blob(buf+3);
1713 if (strncmp(buf, "PW ", 3) == 0) {
1715 /* We asked for a password and obviously got it :-) */
1717 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1719 if (opt_password == NULL) {
1720 DEBUG(1, ("Out of memory\n"));
1721 x_fprintf(x_stdout, "BH Out of memory\n");
1722 data_blob_free(&request);
1726 x_fprintf(x_stdout, "OK\n");
1727 data_blob_free(&request);
1731 if ( (strncmp(buf, "TT ", 3) != 0) &&
1732 (strncmp(buf, "AF ", 3) != 0) &&
1733 (strncmp(buf, "NA ", 3) != 0) ) {
1734 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1735 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1736 data_blob_free(&request);
1740 /* So we got a server challenge to generate a SPNEGO
1741 client-to-server request... */
1743 len = spnego_read_data(ctx, request, &spnego);
1744 data_blob_free(&request);
1747 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1748 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1752 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1754 /* The server offers a list of mechanisms */
1756 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1758 while (*mechType != NULL) {
1761 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1762 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1763 if (manage_client_krb5_init(spnego))
1768 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1769 if (manage_client_ntlmssp_init(spnego))
1776 DEBUG(1, ("Server offered no compatible mechanism\n"));
1777 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1781 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1783 if (spnego.negTokenTarg.supportedMech == NULL) {
1784 /* On accept/reject Windows does not send the
1785 mechanism anymore. Handle that here and
1786 shut down the mechanisms. */
1788 switch (spnego.negTokenTarg.negResult) {
1789 case SPNEGO_ACCEPT_COMPLETED:
1790 x_fprintf(x_stdout, "AF\n");
1793 x_fprintf(x_stdout, "NA\n");
1796 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1797 "unknown negResult: %d\n",
1798 spnego.negTokenTarg.negResult));
1799 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1800 " no mech and an unknown "
1804 TALLOC_FREE(client_ntlmssp_state);
1808 if (strcmp(spnego.negTokenTarg.supportedMech,
1809 OID_NTLMSSP) == 0) {
1810 manage_client_ntlmssp_targ(spnego);
1815 if (strcmp(spnego.negTokenTarg.supportedMech,
1816 OID_KERBEROS5_OLD) == 0) {
1817 manage_client_krb5_targ(spnego);
1824 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1825 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1829 spnego_free_data(&spnego);
1833 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1834 char *buf, int length)
1836 char *request, *parameter;
1837 static DATA_BLOB challenge;
1838 static DATA_BLOB lm_response;
1839 static DATA_BLOB nt_response;
1840 static char *full_username;
1841 static char *username;
1842 static char *domain;
1843 static char *plaintext_password;
1844 static bool ntlm_server_1_user_session_key;
1845 static bool ntlm_server_1_lm_session_key;
1847 if (strequal(buf, ".")) {
1848 if (!full_username && !username) {
1849 x_fprintf(x_stdout, "Error: No username supplied!\n");
1850 } else if (plaintext_password) {
1851 /* handle this request as plaintext */
1852 if (!full_username) {
1853 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1854 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1858 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1859 x_fprintf(x_stdout, "Authenticated: Yes\n");
1861 x_fprintf(x_stdout, "Authenticated: No\n");
1863 } else if (!lm_response.data && !nt_response.data) {
1864 x_fprintf(x_stdout, "Error: No password supplied!\n");
1865 } else if (!challenge.data) {
1866 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1868 char *error_string = NULL;
1870 uchar user_session_key[16];
1873 if (full_username && !username) {
1875 fstring fstr_domain;
1877 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1878 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1879 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1881 SAFE_FREE(username);
1883 username = smb_xstrdup(fstr_user);
1884 domain = smb_xstrdup(fstr_domain);
1888 domain = smb_xstrdup(get_winbind_domain());
1891 if (ntlm_server_1_lm_session_key)
1892 flags |= WBFLAG_PAM_LMKEY;
1894 if (ntlm_server_1_user_session_key)
1895 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1897 if (!NT_STATUS_IS_OK(
1898 contact_winbind_auth_crap(username,
1910 x_fprintf(x_stdout, "Authenticated: No\n");
1911 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1913 static char zeros[16];
1915 char *hex_user_session_key;
1917 x_fprintf(x_stdout, "Authenticated: Yes\n");
1919 if (ntlm_server_1_lm_session_key
1920 && (memcmp(zeros, lm_key,
1921 sizeof(lm_key)) != 0)) {
1922 hex_lm_key = hex_encode_talloc(NULL,
1923 (const unsigned char *)lm_key,
1925 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1926 TALLOC_FREE(hex_lm_key);
1929 if (ntlm_server_1_user_session_key
1930 && (memcmp(zeros, user_session_key,
1931 sizeof(user_session_key)) != 0)) {
1932 hex_user_session_key = hex_encode_talloc(NULL,
1933 (const unsigned char *)user_session_key,
1934 sizeof(user_session_key));
1935 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1936 TALLOC_FREE(hex_user_session_key);
1939 SAFE_FREE(error_string);
1941 /* clear out the state */
1942 challenge = data_blob_null;
1943 nt_response = data_blob_null;
1944 lm_response = data_blob_null;
1945 SAFE_FREE(full_username);
1946 SAFE_FREE(username);
1948 SAFE_FREE(plaintext_password);
1949 ntlm_server_1_user_session_key = False;
1950 ntlm_server_1_lm_session_key = False;
1951 x_fprintf(x_stdout, ".\n");
1958 /* Indicates a base64 encoded structure */
1959 parameter = strstr_m(request, ":: ");
1961 parameter = strstr_m(request, ": ");
1964 DEBUG(0, ("Parameter not found!\n"));
1965 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1982 base64_decode_inplace(parameter);
1985 if (strequal(request, "LANMAN-Challenge")) {
1986 challenge = strhex_to_data_blob(NULL, parameter);
1987 if (challenge.length != 8) {
1988 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1990 (int)challenge.length);
1991 challenge = data_blob_null;
1993 } else if (strequal(request, "NT-Response")) {
1994 nt_response = strhex_to_data_blob(NULL, parameter);
1995 if (nt_response.length < 24) {
1996 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1998 (int)nt_response.length);
1999 nt_response = data_blob_null;
2001 } else if (strequal(request, "LANMAN-Response")) {
2002 lm_response = strhex_to_data_blob(NULL, parameter);
2003 if (lm_response.length != 24) {
2004 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2006 (int)lm_response.length);
2007 lm_response = data_blob_null;
2009 } else if (strequal(request, "Password")) {
2010 plaintext_password = smb_xstrdup(parameter);
2011 } else if (strequal(request, "NT-Domain")) {
2012 domain = smb_xstrdup(parameter);
2013 } else if (strequal(request, "Username")) {
2014 username = smb_xstrdup(parameter);
2015 } else if (strequal(request, "Full-Username")) {
2016 full_username = smb_xstrdup(parameter);
2017 } else if (strequal(request, "Request-User-Session-Key")) {
2018 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
2019 } else if (strequal(request, "Request-LanMan-Session-Key")) {
2020 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
2022 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2026 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
2027 char *buf, int length)
2029 char *request, *parameter;
2030 static DATA_BLOB new_nt_pswd;
2031 static DATA_BLOB old_nt_hash_enc;
2032 static DATA_BLOB new_lm_pswd;
2033 static DATA_BLOB old_lm_hash_enc;
2034 static char *full_username = NULL;
2035 static char *username = NULL;
2036 static char *domain = NULL;
2037 static char *newpswd = NULL;
2038 static char *oldpswd = NULL;
2040 if (strequal(buf, ".")) {
2041 if(newpswd && oldpswd) {
2042 uchar old_nt_hash[16];
2043 uchar old_lm_hash[16];
2044 uchar new_nt_hash[16];
2045 uchar new_lm_hash[16];
2047 new_nt_pswd = data_blob(NULL, 516);
2048 old_nt_hash_enc = data_blob(NULL, 16);
2050 /* Calculate the MD4 hash (NT compatible) of the
2052 E_md4hash(oldpswd, old_nt_hash);
2053 E_md4hash(newpswd, new_nt_hash);
2055 /* E_deshash returns false for 'long'
2056 passwords (> 14 DOS chars).
2058 Therefore, don't send a buffer
2059 encrypted with the truncated hash
2060 (it could allow an even easier
2061 attack on the password)
2063 Likewise, obey the admin's restriction
2066 if (lp_client_lanman_auth() &&
2067 E_deshash(newpswd, new_lm_hash) &&
2068 E_deshash(oldpswd, old_lm_hash)) {
2069 new_lm_pswd = data_blob(NULL, 516);
2070 old_lm_hash_enc = data_blob(NULL, 16);
2071 encode_pw_buffer(new_lm_pswd.data, newpswd,
2074 arcfour_crypt(new_lm_pswd.data, old_nt_hash, 516);
2075 E_old_pw_hash(new_nt_hash, old_lm_hash,
2076 old_lm_hash_enc.data);
2078 new_lm_pswd.data = NULL;
2079 new_lm_pswd.length = 0;
2080 old_lm_hash_enc.data = NULL;
2081 old_lm_hash_enc.length = 0;
2084 encode_pw_buffer(new_nt_pswd.data, newpswd,
2087 arcfour_crypt(new_nt_pswd.data, old_nt_hash, 516);
2088 E_old_pw_hash(new_nt_hash, old_nt_hash,
2089 old_nt_hash_enc.data);
2092 if (!full_username && !username) {
2093 x_fprintf(x_stdout, "Error: No username supplied!\n");
2094 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2095 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2096 x_fprintf(x_stdout, "Error: No NT or LM password "
2097 "blobs supplied!\n");
2099 char *error_string = NULL;
2101 if (full_username && !username) {
2103 fstring fstr_domain;
2105 if (!parse_ntlm_auth_domain_user(full_username,
2108 /* username might be 'tainted', don't
2109 * print into our new-line
2110 * deleimianted stream */
2111 x_fprintf(x_stdout, "Error: Could not "
2112 "parse into domain and "
2114 SAFE_FREE(username);
2115 username = smb_xstrdup(full_username);
2117 SAFE_FREE(username);
2119 username = smb_xstrdup(fstr_user);
2120 domain = smb_xstrdup(fstr_domain);
2125 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2132 x_fprintf(x_stdout, "Password-Change: No\n");
2133 x_fprintf(x_stdout, "Password-Change-Error: "
2134 "%s\n.\n", error_string);
2136 x_fprintf(x_stdout, "Password-Change: Yes\n");
2139 SAFE_FREE(error_string);
2141 /* clear out the state */
2142 new_nt_pswd = data_blob_null;
2143 old_nt_hash_enc = data_blob_null;
2144 new_lm_pswd = data_blob_null;
2145 old_nt_hash_enc = data_blob_null;
2146 SAFE_FREE(full_username);
2147 SAFE_FREE(username);
2151 x_fprintf(x_stdout, ".\n");
2158 /* Indicates a base64 encoded structure */
2159 parameter = strstr_m(request, ":: ");
2161 parameter = strstr_m(request, ": ");
2164 DEBUG(0, ("Parameter not found!\n"));
2165 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2181 base64_decode_inplace(parameter);
2184 if (strequal(request, "new-nt-password-blob")) {
2185 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2186 if (new_nt_pswd.length != 516) {
2187 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2188 "(got %d bytes, expected 516)\n.\n",
2190 (int)new_nt_pswd.length);
2191 new_nt_pswd = data_blob_null;
2193 } else if (strequal(request, "old-nt-hash-blob")) {
2194 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2195 if (old_nt_hash_enc.length != 16) {
2196 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2197 "(got %d bytes, expected 16)\n.\n",
2199 (int)old_nt_hash_enc.length);
2200 old_nt_hash_enc = data_blob_null;
2202 } else if (strequal(request, "new-lm-password-blob")) {
2203 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2204 if (new_lm_pswd.length != 516) {
2205 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2206 "(got %d bytes, expected 516)\n.\n",
2208 (int)new_lm_pswd.length);
2209 new_lm_pswd = data_blob_null;
2212 else if (strequal(request, "old-lm-hash-blob")) {
2213 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2214 if (old_lm_hash_enc.length != 16)
2216 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2217 "(got %d bytes, expected 16)\n.\n",
2219 (int)old_lm_hash_enc.length);
2220 old_lm_hash_enc = data_blob_null;
2222 } else if (strequal(request, "nt-domain")) {
2223 domain = smb_xstrdup(parameter);
2224 } else if(strequal(request, "username")) {
2225 username = smb_xstrdup(parameter);
2226 } else if(strequal(request, "full-username")) {
2227 username = smb_xstrdup(parameter);
2228 } else if(strequal(request, "new-password")) {
2229 newpswd = smb_xstrdup(parameter);
2230 } else if (strequal(request, "old-password")) {
2231 oldpswd = smb_xstrdup(parameter);
2233 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2237 static void manage_squid_request(struct ntlm_auth_state *state,
2238 stdio_helper_function fn)
2241 char tmp[INITIAL_BUFFER_SIZE+1];
2242 int length, buf_size = 0;
2245 buf = talloc_strdup(state->mem_ctx, "");
2247 DEBUG(0, ("Failed to allocate input buffer.\n"));
2248 x_fprintf(x_stderr, "ERR\n");
2254 /* this is not a typo - x_fgets doesn't work too well under
2256 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2257 if (ferror(stdin)) {
2258 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2259 "(%s)\n", ferror(stdin),
2260 strerror(ferror(stdin))));
2267 buf = talloc_strdup_append_buffer(buf, tmp);
2268 buf_size += INITIAL_BUFFER_SIZE;
2270 if (buf_size > MAX_BUFFER_SIZE) {
2271 DEBUG(2, ("Oversized message\n"));
2272 x_fprintf(x_stderr, "ERR\n");
2277 c = strchr(buf, '\n');
2278 } while (c == NULL);
2283 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2285 if (buf[0] == '\0') {
2286 DEBUG(2, ("Invalid Request\n"));
2287 x_fprintf(x_stderr, "ERR\n");
2292 fn(state, buf, length);
2297 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2298 TALLOC_CTX *mem_ctx;
2299 struct ntlm_auth_state *state;
2301 /* initialize FDescs */
2302 x_setbuf(x_stdout, NULL);
2303 x_setbuf(x_stderr, NULL);
2305 mem_ctx = talloc_init("ntlm_auth");
2307 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2308 x_fprintf(x_stderr, "ERR\n");
2312 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2314 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2315 x_fprintf(x_stderr, "ERR\n");
2319 state->mem_ctx = mem_ctx;
2320 state->helper_mode = stdio_mode;
2323 manage_squid_request(state, fn);
2328 /* Authenticate a user with a challenge/response */
2330 static bool check_auth_crap(void)
2335 char user_session_key[16];
2337 char *hex_user_session_key;
2339 static uint8 zeros[16];
2341 x_setbuf(x_stdout, NULL);
2344 flags |= WBFLAG_PAM_LMKEY;
2346 if (request_user_session_key)
2347 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2349 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2351 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2357 (unsigned char *)lm_key,
2358 (unsigned char *)user_session_key,
2359 &error_string, NULL);
2361 if (!NT_STATUS_IS_OK(nt_status)) {
2362 x_fprintf(x_stdout, "%s (0x%x)\n",
2364 NT_STATUS_V(nt_status));
2365 SAFE_FREE(error_string);
2370 && (memcmp(zeros, lm_key,
2371 sizeof(lm_key)) != 0)) {
2372 hex_lm_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key,
2374 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2375 TALLOC_FREE(hex_lm_key);
2377 if (request_user_session_key
2378 && (memcmp(zeros, user_session_key,
2379 sizeof(user_session_key)) != 0)) {
2380 hex_user_session_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key,
2381 sizeof(user_session_key));
2382 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2383 TALLOC_FREE(hex_user_session_key);
2392 OPT_USERNAME = 1000,
2401 OPT_USER_SESSION_KEY,
2403 OPT_REQUIRE_MEMBERSHIP,
2404 OPT_USE_CACHED_CREDS,
2405 OPT_PAM_WINBIND_CONF
2408 int main(int argc, const char **argv)
2410 TALLOC_CTX *frame = talloc_stackframe();
2412 static const char *helper_protocol;
2413 static int diagnostics;
2415 static const char *hex_challenge;
2416 static const char *hex_lm_response;
2417 static const char *hex_nt_response;
2421 /* NOTE: DO NOT change this interface without considering the implications!
2422 This is an external interface, which other programs will use to interact
2426 /* We do not use single-letter command abbreviations, because they harm future
2427 interface stability. */
2429 struct poptOption long_options[] = {
2431 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2432 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2433 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2434 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2435 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2436 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2437 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2438 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2439 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2440 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2441 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2442 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2443 { "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" },
2444 { "pam-winbind-conf", 0, POPT_ARG_STRING, &opt_pam_winbind_conf, OPT_PAM_WINBIND_CONF, "Require that request must set WBFLAG_PAM_CONTACT_TRUSTDOM when krb5 auth is required" },
2445 POPT_COMMON_CONFIGFILE
2450 /* Samba client initialisation */
2457 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2459 /* Parse command line options */
2462 poptPrintHelp(pc, stderr, 0);
2466 while((opt = poptGetNextOpt(pc)) != -1) {
2467 /* Get generic config options like --configfile */
2470 poptFreeContext(pc);
2472 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2473 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2474 get_dyn_CONFIGFILE(), strerror(errno));
2478 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2479 POPT_CONTEXT_KEEP_FIRST);
2481 while((opt = poptGetNextOpt(pc)) != -1) {
2484 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2485 if (opt_challenge.length != 8) {
2486 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2488 (int)opt_challenge.length);
2493 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2494 if (opt_lm_response.length != 24) {
2495 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2497 (int)opt_lm_response.length);
2503 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2504 if (opt_nt_response.length < 24) {
2505 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2507 (int)opt_nt_response.length);
2512 case OPT_REQUIRE_MEMBERSHIP:
2513 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2514 require_membership_of_sid = require_membership_of;
2521 char *domain = SMB_STRDUP(opt_username);
2522 char *p = strchr_m(domain, *lp_winbind_separator());
2526 if (opt_domain && !strequal(opt_domain, domain)) {
2527 x_fprintf(x_stderr, "Domain specified in username (%s) "
2528 "doesn't match specified domain (%s)!\n\n",
2529 domain, opt_domain);
2530 poptPrintHelp(pc, stderr, 0);
2533 opt_domain = domain;
2539 /* Note: if opt_domain is "" then send no domain */
2540 if (opt_domain == NULL) {
2541 opt_domain = get_winbind_domain();
2544 if (opt_workstation == NULL) {
2545 opt_workstation = "";
2548 if (helper_protocol) {
2550 for (i=0; i<NUM_HELPER_MODES; i++) {
2551 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2552 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2556 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2558 for (i=0; i<NUM_HELPER_MODES; i++) {
2559 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2565 if (!opt_username || !*opt_username) {
2566 x_fprintf(x_stderr, "username must be specified!\n\n");
2567 poptPrintHelp(pc, stderr, 0);
2571 if (opt_challenge.length) {
2572 if (!check_auth_crap()) {
2578 if (!opt_password) {
2579 opt_password = getpass("password: ");
2583 if (!diagnose_ntlm_auth()) {
2589 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2590 if (!check_plaintext_auth(user, opt_password, True)) {
2597 poptFreeContext(pc);