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>
34 #ifndef PAM_WINBIND_CONFIG_FILE
35 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
38 #define WINBIND_KRB5_AUTH 0x00000080
41 #define DBGC_CLASS DBGC_WINBIND
43 #define INITIAL_BUFFER_SIZE 300
44 #define MAX_BUFFER_SIZE 630000
46 enum stdio_helper_mode {
54 NTLM_CHANGE_PASSWORD_1,
58 enum ntlm_auth_cli_state {
65 enum ntlm_auth_svr_state {
72 struct ntlm_auth_state {
74 enum stdio_helper_mode helper_mode;
75 enum ntlm_auth_cli_state cli_state;
76 enum ntlm_auth_svr_state svr_state;
77 struct ntlmssp_state *ntlmssp_state;
79 char *want_feature_list;
80 bool have_session_key;
81 DATA_BLOB session_key;
82 DATA_BLOB initial_message;
85 typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
88 static void manage_squid_basic_request (struct ntlm_auth_state *state,
89 char *buf, int length);
91 static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
92 char *buf, int length);
94 static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
95 char *buf, int length);
97 static void manage_gss_spnego_request (struct ntlm_auth_state *state,
98 char *buf, int length);
100 static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
101 char *buf, int length);
103 static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
104 char *buf, int length);
106 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
107 char *buf, int length);
109 static const struct {
110 enum stdio_helper_mode mode;
112 stdio_helper_function fn;
113 } stdio_helper_protocols[] = {
114 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
115 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
116 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
117 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
118 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
119 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
120 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
121 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
122 { NUM_HELPER_MODES, NULL, NULL}
125 const char *opt_username;
126 const char *opt_domain;
127 const char *opt_workstation;
128 const char *opt_password;
129 static DATA_BLOB opt_challenge;
130 static DATA_BLOB opt_lm_response;
131 static DATA_BLOB opt_nt_response;
132 static int request_lm_key;
133 static int request_user_session_key;
134 static int use_cached_creds;
136 static const char *require_membership_of;
137 static const char *require_membership_of_sid;
138 static const char *opt_pam_winbind_conf;
140 static char winbind_separator(void)
142 struct winbindd_response response;
149 ZERO_STRUCT(response);
151 /* Send off request */
153 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
154 NSS_STATUS_SUCCESS) {
155 d_printf("could not obtain winbind separator!\n");
156 return *lp_winbind_separator();
159 sep = response.data.info.winbind_separator;
163 d_printf("winbind separator was NULL!\n");
164 return *lp_winbind_separator();
170 const char *get_winbind_domain(void)
172 struct winbindd_response response;
174 static fstring winbind_domain;
175 if (*winbind_domain) {
176 return winbind_domain;
179 ZERO_STRUCT(response);
181 /* Send off request */
183 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
184 NSS_STATUS_SUCCESS) {
185 DEBUG(0, ("could not obtain winbind domain name!\n"));
186 return lp_workgroup();
189 fstrcpy(winbind_domain, response.data.domain_name);
191 return winbind_domain;
195 const char *get_winbind_netbios_name(void)
197 struct winbindd_response response;
199 static fstring winbind_netbios_name;
201 if (*winbind_netbios_name) {
202 return winbind_netbios_name;
205 ZERO_STRUCT(response);
207 /* Send off request */
209 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
210 NSS_STATUS_SUCCESS) {
211 DEBUG(0, ("could not obtain winbind netbios name!\n"));
212 return global_myname();
215 fstrcpy(winbind_netbios_name, response.data.netbios_name);
217 return winbind_netbios_name;
221 DATA_BLOB get_challenge(void)
223 static DATA_BLOB chal;
224 if (opt_challenge.length)
225 return opt_challenge;
227 chal = data_blob(NULL, 8);
229 generate_random_buffer(chal.data, chal.length);
233 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
234 form DOMAIN/user into a domain and a user */
236 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
240 char *p = strchr(domuser,winbind_separator());
247 fstrcpy(domain, domuser);
248 domain[PTR_DIFF(p, domuser)] = 0;
254 static bool get_require_membership_sid(void) {
255 struct winbindd_request request;
256 struct winbindd_response response;
258 if (!require_membership_of) {
262 if (require_membership_of_sid) {
266 /* Otherwise, ask winbindd for the name->sid request */
268 ZERO_STRUCT(request);
269 ZERO_STRUCT(response);
271 if (!parse_ntlm_auth_domain_user(require_membership_of,
272 request.data.name.dom_name,
273 request.data.name.name)) {
274 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
275 require_membership_of));
279 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
280 NSS_STATUS_SUCCESS) {
281 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
282 require_membership_of));
286 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
288 if (require_membership_of_sid)
295 * Get some configuration from pam_winbind.conf to see if we
296 * need to contact trusted domain
299 int get_pam_winbind_config()
302 dictionary *d = NULL;
304 if (!opt_pam_winbind_conf || !*opt_pam_winbind_conf) {
305 opt_pam_winbind_conf = PAM_WINBIND_CONFIG_FILE;
308 d = iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf));
314 if (iniparser_getboolean(d, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
315 ctrl |= WINBIND_KRB5_AUTH;
318 iniparser_freedict(d);
323 /* Authenticate a user with a plaintext password */
325 static bool check_plaintext_auth(const char *user, const char *pass,
326 bool stdout_diagnostics)
328 struct winbindd_request request;
329 struct winbindd_response response;
332 if (!get_require_membership_sid()) {
336 /* Send off request */
338 ZERO_STRUCT(request);
339 ZERO_STRUCT(response);
341 fstrcpy(request.data.auth.user, user);
342 fstrcpy(request.data.auth.pass, pass);
343 if (require_membership_of_sid) {
344 strlcpy(request.data.auth.require_membership_of_sid,
345 require_membership_of_sid,
346 sizeof(request.data.auth.require_membership_of_sid));
349 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
351 /* Display response */
353 if (stdout_diagnostics) {
354 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
355 d_printf("Reading winbind reply failed! (0x01)\n");
358 d_printf("%s: %s (0x%x)\n",
359 response.data.auth.nt_status_string,
360 response.data.auth.error_string,
361 response.data.auth.nt_status);
363 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
364 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
367 DEBUG(3, ("%s: %s (0x%x)\n",
368 response.data.auth.nt_status_string,
369 response.data.auth.error_string,
370 response.data.auth.nt_status));
373 return (result == NSS_STATUS_SUCCESS);
376 /* authenticate a user with an encrypted username/password */
378 NTSTATUS contact_winbind_auth_crap(const char *username,
380 const char *workstation,
381 const DATA_BLOB *challenge,
382 const DATA_BLOB *lm_response,
383 const DATA_BLOB *nt_response,
386 uint8 user_session_key[16],
392 struct winbindd_request request;
393 struct winbindd_response response;
395 if (!get_require_membership_sid()) {
396 return NT_STATUS_INVALID_PARAMETER;
399 ZERO_STRUCT(request);
400 ZERO_STRUCT(response);
402 request.flags = flags;
404 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
406 if (require_membership_of_sid)
407 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
409 fstrcpy(request.data.auth_crap.user, username);
410 fstrcpy(request.data.auth_crap.domain, domain);
412 fstrcpy(request.data.auth_crap.workstation,
415 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
417 if (lm_response && lm_response->length) {
418 memcpy(request.data.auth_crap.lm_resp,
420 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
421 request.data.auth_crap.lm_resp_len = lm_response->length;
424 if (nt_response && nt_response->length) {
425 if (nt_response->length > sizeof(request.data.auth_crap.nt_resp)) {
426 request.flags = request.flags | WBFLAG_BIG_NTLMV2_BLOB;
427 request.extra_len = nt_response->length;
428 request.extra_data.data = SMB_MALLOC_ARRAY(char, request.extra_len);
429 if (request.extra_data.data == NULL) {
430 return NT_STATUS_NO_MEMORY;
432 memcpy(request.extra_data.data, nt_response->data,
433 nt_response->length);
436 memcpy(request.data.auth_crap.nt_resp,
437 nt_response->data, nt_response->length);
439 request.data.auth_crap.nt_resp_len = nt_response->length;
442 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
443 SAFE_FREE(request.extra_data.data);
445 /* Display response */
447 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
448 nt_status = NT_STATUS_UNSUCCESSFUL;
450 *error_string = smb_xstrdup("Reading winbind reply failed!");
451 winbindd_free_response(&response);
455 nt_status = (NT_STATUS(response.data.auth.nt_status));
456 if (!NT_STATUS_IS_OK(nt_status)) {
458 *error_string = smb_xstrdup(response.data.auth.error_string);
459 winbindd_free_response(&response);
463 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
464 memcpy(lm_key, response.data.auth.first_8_lm_hash,
465 sizeof(response.data.auth.first_8_lm_hash));
467 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
468 memcpy(user_session_key, response.data.auth.user_session_key,
469 sizeof(response.data.auth.user_session_key));
472 if (flags & WBFLAG_PAM_UNIX_NAME) {
473 *unix_name = SMB_STRDUP(response.data.auth.unix_username);
475 winbindd_free_response(&response);
476 return NT_STATUS_NO_MEMORY;
480 winbindd_free_response(&response);
484 /* contact server to change user password using auth crap */
485 static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
487 const DATA_BLOB new_nt_pswd,
488 const DATA_BLOB old_nt_hash_enc,
489 const DATA_BLOB new_lm_pswd,
490 const DATA_BLOB old_lm_hash_enc,
495 struct winbindd_request request;
496 struct winbindd_response response;
498 if (!get_require_membership_sid())
501 *error_string = smb_xstrdup("Can't get membership sid.");
502 return NT_STATUS_INVALID_PARAMETER;
505 ZERO_STRUCT(request);
506 ZERO_STRUCT(response);
509 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
511 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
513 if(new_nt_pswd.length)
515 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
516 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
519 if(old_nt_hash_enc.length)
521 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));
522 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
525 if(new_lm_pswd.length)
527 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
528 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
531 if(old_lm_hash_enc.length)
533 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));
534 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
537 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
539 /* Display response */
541 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
543 nt_status = NT_STATUS_UNSUCCESSFUL;
545 *error_string = smb_xstrdup("Reading winbind reply failed!");
546 winbindd_free_response(&response);
550 nt_status = (NT_STATUS(response.data.auth.nt_status));
551 if (!NT_STATUS_IS_OK(nt_status))
554 *error_string = smb_xstrdup(response.data.auth.error_string);
555 winbindd_free_response(&response);
559 winbindd_free_response(&response);
564 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state,
566 DATA_BLOB *user_session_key,
567 DATA_BLOB *lm_session_key)
569 static const char zeros[16] = { 0, };
571 char *error_string = NULL;
573 uint8 user_sess_key[16];
574 char *unix_name = NULL;
576 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
577 ntlmssp_state->workstation,
578 &ntlmssp_state->chal,
579 &ntlmssp_state->lm_resp,
580 &ntlmssp_state->nt_resp,
581 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
582 lm_key, user_sess_key,
583 &error_string, &unix_name);
585 if (NT_STATUS_IS_OK(nt_status)) {
586 if (memcmp(lm_key, zeros, 8) != 0) {
587 *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
588 memcpy(lm_session_key->data, lm_key, 8);
589 memset(lm_session_key->data+8, '\0', 8);
592 if (memcmp(user_sess_key, zeros, 16) != 0) {
593 *user_session_key = data_blob_talloc(mem_ctx, user_sess_key, 16);
595 ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state,
598 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
599 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
600 ntlmssp_state->domain, ntlmssp_state->user,
601 ntlmssp_state->workstation,
602 error_string ? error_string : "unknown error (NULL)"));
603 ntlmssp_state->auth_context = NULL;
606 SAFE_FREE(error_string);
607 SAFE_FREE(unix_name);
611 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state,
613 DATA_BLOB *user_session_key,
614 DATA_BLOB *lm_session_key)
617 struct samr_Password lm_pw, nt_pw;
619 nt_lm_owf_gen (opt_password, nt_pw.hash, lm_pw.hash);
621 nt_status = ntlm_password_check(ntlmssp_state,
623 &ntlmssp_state->chal,
624 &ntlmssp_state->lm_resp,
625 &ntlmssp_state->nt_resp,
628 ntlmssp_state->domain,
629 &lm_pw, &nt_pw, user_session_key, lm_session_key);
631 if (NT_STATUS_IS_OK(nt_status)) {
632 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state,
633 "%s%c%s", ntlmssp_state->domain,
634 *lp_winbind_separator(),
635 ntlmssp_state->user);
636 if (user_session_key) {
637 talloc_steal(mem_ctx, user_session_key->data);
639 if (lm_session_key) {
640 talloc_steal(mem_ctx, lm_session_key->data);
643 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
644 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
645 nt_errstr(nt_status)));
646 ntlmssp_state->auth_context = NULL;
653 static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntlmssp_state)
656 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
657 status = NT_STATUS_UNSUCCESSFUL;
658 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
659 return NT_STATUS_INVALID_PARAMETER;
662 status = ntlmssp_client_start(client_ntlmssp_state);
664 if (!NT_STATUS_IS_OK(status)) {
665 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
667 ntlmssp_end(client_ntlmssp_state);
671 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
673 if (!NT_STATUS_IS_OK(status)) {
674 DEBUG(1, ("Could not set username: %s\n",
676 ntlmssp_end(client_ntlmssp_state);
680 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
682 if (!NT_STATUS_IS_OK(status)) {
683 DEBUG(1, ("Could not set domain: %s\n",
685 ntlmssp_end(client_ntlmssp_state);
690 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
692 if (!NT_STATUS_IS_OK(status)) {
693 DEBUG(1, ("Could not set password: %s\n",
695 ntlmssp_end(client_ntlmssp_state);
703 static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_state)
705 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
707 if (!NT_STATUS_IS_OK(status)) {
708 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
713 /* Have we been given a local password, or should we ask winbind? */
715 (*ntlmssp_state)->check_password = local_pw_check;
716 (*ntlmssp_state)->client_workgroup = lp_workgroup();
717 (*ntlmssp_state)->my_netbios_name = global_myname();
719 (*ntlmssp_state)->check_password = winbind_pw_check;
720 (*ntlmssp_state)->client_workgroup = get_winbind_domain();
721 (*ntlmssp_state)->my_netbios_name = get_winbind_netbios_name();
726 /*******************************************************************
727 Used by firefox to drive NTLM auth to IIS servers.
728 *******************************************************************/
730 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
733 struct winbindd_request wb_request;
734 struct winbindd_response wb_response;
738 /* get winbindd to do the ntlmssp step on our behalf */
739 ZERO_STRUCT(wb_request);
740 ZERO_STRUCT(wb_response);
743 * This is tricky here. If we set krb5_auth in pam_winbind.conf
744 * creds for users in trusted domain will be stored the winbindd
745 * child of the trusted domain. If we ask the primary domain for
746 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
747 * domain's child for ccache_ntlm_auth. that is to say, we have to
748 * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
750 ctrl = get_pam_winbind_config();
752 if (ctrl | WINBIND_KRB5_AUTH) {
753 wb_request.flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
756 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
757 "%s%c%s", opt_domain, winbind_separator(), opt_username);
758 wb_request.data.ccache_ntlm_auth.uid = geteuid();
759 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
760 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
761 wb_request.extra_len = initial_msg.length + challenge_msg.length;
763 if (wb_request.extra_len > 0) {
764 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
765 if (wb_request.extra_data.data == NULL) {
766 return NT_STATUS_NO_MEMORY;
769 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
770 memcpy(wb_request.extra_data.data + initial_msg.length,
771 challenge_msg.data, challenge_msg.length);
774 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
775 SAFE_FREE(wb_request.extra_data.data);
777 if (result != NSS_STATUS_SUCCESS) {
778 winbindd_free_response(&wb_response);
779 return NT_STATUS_UNSUCCESSFUL;
783 *reply = data_blob(wb_response.extra_data.data,
784 wb_response.data.ccache_ntlm_auth.auth_blob_len);
785 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
786 reply->data == NULL) {
787 winbindd_free_response(&wb_response);
788 return NT_STATUS_NO_MEMORY;
792 winbindd_free_response(&wb_response);
793 return NT_STATUS_MORE_PROCESSING_REQUIRED;
796 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
797 char *buf, int length)
799 DATA_BLOB request, reply;
802 if (strlen(buf) < 2) {
803 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
804 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
808 if (strlen(buf) > 3) {
809 if(strncmp(buf, "SF ", 3) == 0){
810 DEBUG(10, ("Setting flags to negotioate\n"));
811 TALLOC_FREE(state->want_feature_list);
812 state->want_feature_list = talloc_strdup(state->mem_ctx,
814 x_fprintf(x_stdout, "OK\n");
817 request = base64_decode_data_blob(buf + 3);
819 request = data_blob_null;
822 if ((strncmp(buf, "PW ", 3) == 0)) {
823 /* The calling application wants us to use a local password
824 * (rather than winbindd) */
826 opt_password = SMB_STRNDUP((const char *)request.data,
829 if (opt_password == NULL) {
830 DEBUG(1, ("Out of memory\n"));
831 x_fprintf(x_stdout, "BH Out of memory\n");
832 data_blob_free(&request);
836 x_fprintf(x_stdout, "OK\n");
837 data_blob_free(&request);
841 if (strncmp(buf, "YR", 2) == 0) {
842 if (state->ntlmssp_state)
843 ntlmssp_end(&state->ntlmssp_state);
844 state->svr_state = SERVER_INITIAL;
845 } else if (strncmp(buf, "KK", 2) == 0) {
846 /* No special preprocessing required */
847 } else if (strncmp(buf, "GF", 2) == 0) {
848 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
850 if (state->svr_state == SERVER_FINISHED) {
851 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
854 x_fprintf(x_stdout, "BH\n");
856 data_blob_free(&request);
858 } else if (strncmp(buf, "GK", 2) == 0) {
859 DEBUG(10, ("Requested NTLMSSP session key\n"));
860 if(state->have_session_key) {
861 char *key64 = base64_encode_data_blob(state->mem_ctx,
863 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
866 x_fprintf(x_stdout, "BH\n");
869 data_blob_free(&request);
872 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
873 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
877 if (!state->ntlmssp_state) {
878 nt_status = ntlm_auth_start_ntlmssp_server(
879 &state->ntlmssp_state);
880 if (!NT_STATUS_IS_OK(nt_status)) {
881 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
884 ntlmssp_want_feature_list(state->ntlmssp_state,
885 state->want_feature_list);
888 DEBUG(10, ("got NTLMSSP packet:\n"));
889 dump_data(10, request.data, request.length);
891 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
893 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
894 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
896 x_fprintf(x_stdout, "TT %s\n", reply_base64);
897 TALLOC_FREE(reply_base64);
898 data_blob_free(&reply);
899 state->svr_state = SERVER_CHALLENGE;
900 DEBUG(10, ("NTLMSSP challenge\n"));
901 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
902 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
903 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
905 ntlmssp_end(&state->ntlmssp_state);
906 } else if (!NT_STATUS_IS_OK(nt_status)) {
907 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
908 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
910 x_fprintf(x_stdout, "AF %s\n",
911 (char *)state->ntlmssp_state->auth_context);
912 DEBUG(10, ("NTLMSSP OK!\n"));
914 if(state->have_session_key)
915 data_blob_free(&state->session_key);
916 state->session_key = data_blob(
917 state->ntlmssp_state->session_key.data,
918 state->ntlmssp_state->session_key.length);
919 state->neg_flags = state->ntlmssp_state->neg_flags;
920 state->have_session_key = true;
921 state->svr_state = SERVER_FINISHED;
924 data_blob_free(&request);
927 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
928 char *buf, int length)
930 DATA_BLOB request, reply;
933 if (!opt_username || !*opt_username) {
934 x_fprintf(x_stderr, "username must be specified!\n\n");
938 if (strlen(buf) < 2) {
939 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
940 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
944 if (strlen(buf) > 3) {
945 if(strncmp(buf, "SF ", 3) == 0) {
946 DEBUG(10, ("Looking for flags to negotiate\n"));
947 talloc_free(state->want_feature_list);
948 state->want_feature_list = talloc_strdup(state->mem_ctx,
950 x_fprintf(x_stdout, "OK\n");
953 request = base64_decode_data_blob(buf + 3);
955 request = data_blob_null;
958 if (strncmp(buf, "PW ", 3) == 0) {
959 /* We asked for a password and obviously got it :-) */
961 opt_password = SMB_STRNDUP((const char *)request.data,
964 if (opt_password == NULL) {
965 DEBUG(1, ("Out of memory\n"));
966 x_fprintf(x_stdout, "BH Out of memory\n");
967 data_blob_free(&request);
971 x_fprintf(x_stdout, "OK\n");
972 data_blob_free(&request);
976 if (!state->ntlmssp_state && use_cached_creds) {
977 /* check whether cached credentials are usable. */
978 DATA_BLOB empty_blob = data_blob_null;
980 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
981 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
982 /* failed to use cached creds */
983 use_cached_creds = False;
987 if (opt_password == NULL && !use_cached_creds) {
988 /* Request a password from the calling process. After
989 sending it, the calling process should retry asking for the
992 DEBUG(10, ("Requesting password\n"));
993 x_fprintf(x_stdout, "PW\n");
997 if (strncmp(buf, "YR", 2) == 0) {
998 if (state->ntlmssp_state)
999 ntlmssp_end(&state->ntlmssp_state);
1000 state->cli_state = CLIENT_INITIAL;
1001 } else if (strncmp(buf, "TT", 2) == 0) {
1002 /* No special preprocessing required */
1003 } else if (strncmp(buf, "GF", 2) == 0) {
1004 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1006 if(state->cli_state == CLIENT_FINISHED) {
1007 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
1010 x_fprintf(x_stdout, "BH\n");
1013 data_blob_free(&request);
1015 } else if (strncmp(buf, "GK", 2) == 0 ) {
1016 DEBUG(10, ("Requested session key\n"));
1018 if(state->cli_state == CLIENT_FINISHED) {
1019 char *key64 = base64_encode_data_blob(state->mem_ctx,
1020 state->session_key);
1021 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
1025 x_fprintf(x_stdout, "BH\n");
1028 data_blob_free(&request);
1031 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
1032 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
1036 if (!state->ntlmssp_state) {
1037 nt_status = ntlm_auth_start_ntlmssp_client(
1038 &state->ntlmssp_state);
1039 if (!NT_STATUS_IS_OK(nt_status)) {
1040 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1043 ntlmssp_want_feature_list(state->ntlmssp_state,
1044 state->want_feature_list);
1045 state->initial_message = data_blob_null;
1048 DEBUG(10, ("got NTLMSSP packet:\n"));
1049 dump_data(10, request.data, request.length);
1051 if (use_cached_creds && !opt_password &&
1052 (state->cli_state == CLIENT_RESPONSE)) {
1053 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
1056 nt_status = ntlmssp_update(state->ntlmssp_state, request,
1060 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1061 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
1063 if (state->cli_state == CLIENT_INITIAL) {
1064 x_fprintf(x_stdout, "YR %s\n", reply_base64);
1065 state->initial_message = reply;
1066 state->cli_state = CLIENT_RESPONSE;
1068 x_fprintf(x_stdout, "KK %s\n", reply_base64);
1069 data_blob_free(&reply);
1071 TALLOC_FREE(reply_base64);
1072 DEBUG(10, ("NTLMSSP challenge\n"));
1073 } else if (NT_STATUS_IS_OK(nt_status)) {
1074 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1076 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1077 TALLOC_FREE(reply_base64);
1079 if(state->have_session_key)
1080 data_blob_free(&state->session_key);
1082 state->session_key = data_blob(
1083 state->ntlmssp_state->session_key.data,
1084 state->ntlmssp_state->session_key.length);
1085 state->neg_flags = state->ntlmssp_state->neg_flags;
1086 state->have_session_key = true;
1088 DEBUG(10, ("NTLMSSP OK!\n"));
1089 state->cli_state = CLIENT_FINISHED;
1090 if (state->ntlmssp_state)
1091 ntlmssp_end(&state->ntlmssp_state);
1093 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1094 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1095 state->cli_state = CLIENT_ERROR;
1096 if (state->ntlmssp_state)
1097 ntlmssp_end(&state->ntlmssp_state);
1100 data_blob_free(&request);
1103 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1104 char *buf, int length)
1109 pass=(char *)memchr(buf,' ',length);
1111 DEBUG(2, ("Password not found. Denying access\n"));
1112 x_fprintf(x_stdout, "ERR\n");
1118 if (state->helper_mode == SQUID_2_5_BASIC) {
1119 rfc1738_unescape(user);
1120 rfc1738_unescape(pass);
1123 if (check_plaintext_auth(user, pass, False)) {
1124 x_fprintf(x_stdout, "OK\n");
1126 x_fprintf(x_stdout, "ERR\n");
1130 static void offer_gss_spnego_mechs(void) {
1133 struct spnego_data spnego;
1136 TALLOC_CTX *ctx = talloc_tos();
1140 ZERO_STRUCT(spnego);
1142 myname_lower = talloc_strdup(ctx, global_myname());
1143 if (!myname_lower) {
1146 strlower_m(myname_lower);
1148 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1153 /* Server negTokenInit (mech offerings) */
1154 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1155 spnego.negTokenInit.mechTypes = SMB_XMALLOC_ARRAY(const char *, 2);
1157 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
1158 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
1159 spnego.negTokenInit.mechTypes[2] = NULL;
1161 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
1162 spnego.negTokenInit.mechTypes[1] = NULL;
1166 spnego.negTokenInit.mechListMIC = data_blob(principal,
1169 len = spnego_write_data(ctx, &token, &spnego);
1170 spnego_free_data(&spnego);
1173 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1174 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1178 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1179 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1181 TALLOC_FREE(reply_base64);
1182 data_blob_free(&token);
1183 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1187 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1188 char *buf, int length)
1190 static struct ntlmssp_state *ntlmssp_state = NULL;
1191 struct spnego_data request, response;
1195 TALLOC_CTX *ctx = talloc_tos();
1198 char *domain = NULL;
1200 const char *reply_code;
1202 char *reply_argument = NULL;
1204 if (strlen(buf) < 2) {
1205 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1206 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1210 if (strncmp(buf, "YR", 2) == 0) {
1212 ntlmssp_end(&ntlmssp_state);
1213 } else if (strncmp(buf, "KK", 2) == 0) {
1216 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1217 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1221 if ( (strlen(buf) == 2)) {
1223 /* no client data, get the negTokenInit offering
1226 offer_gss_spnego_mechs();
1230 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1232 if (strlen(buf) <= 3) {
1233 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1234 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1238 token = base64_decode_data_blob(buf + 3);
1239 len = spnego_read_data(ctx, token, &request);
1240 data_blob_free(&token);
1243 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
1244 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1248 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1250 /* Second request from Client. This is where the
1251 client offers its mechanism to use. */
1253 if ( (request.negTokenInit.mechTypes == NULL) ||
1254 (request.negTokenInit.mechTypes[0] == NULL) ) {
1255 DEBUG(1, ("Client did not offer any mechanism"));
1256 x_fprintf(x_stdout, "BH Client did not offer any "
1261 status = NT_STATUS_UNSUCCESSFUL;
1262 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1264 if ( request.negTokenInit.mechToken.data == NULL ) {
1265 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1266 x_fprintf(x_stdout, "BH Client did not provide "
1271 if ( ntlmssp_state != NULL ) {
1272 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1273 "already got one\n"));
1274 x_fprintf(x_stdout, "BH Client wants a new "
1275 "NTLMSSP challenge, but "
1276 "already got one\n");
1277 ntlmssp_end(&ntlmssp_state);
1281 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1282 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1286 DEBUG(10, ("got NTLMSSP packet:\n"));
1287 dump_data(10, request.negTokenInit.mechToken.data,
1288 request.negTokenInit.mechToken.length);
1290 response.type = SPNEGO_NEG_TOKEN_TARG;
1291 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1292 response.negTokenTarg.mechListMIC = data_blob_null;
1294 status = ntlmssp_update(ntlmssp_state,
1295 request.negTokenInit.mechToken,
1296 &response.negTokenTarg.responseToken);
1300 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1302 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1305 DATA_BLOB session_key;
1306 struct PAC_DATA *pac_data = NULL;
1308 if ( request.negTokenInit.mechToken.data == NULL ) {
1309 DEBUG(1, ("Client did not provide Kerberos data\n"));
1310 x_fprintf(x_stdout, "BH Client did not provide "
1315 response.type = SPNEGO_NEG_TOKEN_TARG;
1316 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_KERBEROS5_OLD);
1317 response.negTokenTarg.mechListMIC = data_blob_null;
1318 response.negTokenTarg.responseToken = data_blob_null;
1320 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1321 &request.negTokenInit.mechToken,
1322 &principal, &pac_data, &ap_rep,
1323 &session_key, True);
1325 /* Now in "principal" we have the name we are
1326 authenticated as. */
1328 if (NT_STATUS_IS_OK(status)) {
1330 domain = strchr_m(principal, '@');
1332 if (domain == NULL) {
1333 DEBUG(1, ("Did not get a valid principal "
1334 "from ads_verify_ticket\n"));
1335 x_fprintf(x_stdout, "BH Did not get a "
1336 "valid principal from "
1337 "ads_verify_ticket\n");
1342 domain = SMB_STRDUP(domain);
1343 user = SMB_STRDUP(principal);
1345 data_blob_free(&ap_rep);
1348 TALLOC_FREE(mem_ctx);
1354 if ( (request.negTokenTarg.supportedMech == NULL) ||
1355 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1356 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1357 is the only one we support that sends this stuff */
1358 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1359 request.negTokenTarg.supportedMech));
1360 x_fprintf(x_stdout, "BH Got a negTokenTarg for "
1361 "something non-NTLMSSP\n");
1365 if (request.negTokenTarg.responseToken.data == NULL) {
1366 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1367 x_fprintf(x_stdout, "BH Got a negTokenTarg without a "
1368 "responseToken!\n");
1372 status = ntlmssp_update(ntlmssp_state,
1373 request.negTokenTarg.responseToken,
1374 &response.negTokenTarg.responseToken);
1376 response.type = SPNEGO_NEG_TOKEN_TARG;
1377 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1378 response.negTokenTarg.mechListMIC = data_blob_null;
1380 if (NT_STATUS_IS_OK(status)) {
1381 user = SMB_STRDUP(ntlmssp_state->user);
1382 domain = SMB_STRDUP(ntlmssp_state->domain);
1383 ntlmssp_end(&ntlmssp_state);
1387 spnego_free_data(&request);
1389 if (NT_STATUS_IS_OK(status)) {
1390 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1392 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1393 } else if (NT_STATUS_EQUAL(status,
1394 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1395 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1397 reply_argument = talloc_strdup(ctx, "*");
1399 response.negTokenTarg.negResult = SPNEGO_REJECT;
1401 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1404 if (!reply_argument) {
1405 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1406 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1413 len = spnego_write_data(ctx, &token, &response);
1414 spnego_free_data(&response);
1417 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1418 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1422 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1424 x_fprintf(x_stdout, "%s %s %s\n",
1425 reply_code, reply_base64, reply_argument);
1427 TALLOC_FREE(reply_base64);
1428 data_blob_free(&token);
1433 static struct ntlmssp_state *client_ntlmssp_state = NULL;
1435 static bool manage_client_ntlmssp_init(struct spnego_data spnego)
1438 DATA_BLOB null_blob = data_blob_null;
1439 DATA_BLOB to_server;
1440 char *to_server_base64;
1441 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1442 TALLOC_CTX *ctx = talloc_tos();
1444 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1446 if (client_ntlmssp_state != NULL) {
1447 DEBUG(1, ("Request for initial SPNEGO request where "
1448 "we already have a state\n"));
1452 if (!client_ntlmssp_state) {
1453 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1454 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1460 if (opt_password == NULL) {
1462 /* Request a password from the calling process. After
1463 sending it, the calling process should retry with
1464 the negTokenInit. */
1466 DEBUG(10, ("Requesting password\n"));
1467 x_fprintf(x_stdout, "PW\n");
1471 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1472 spnego.negTokenInit.mechTypes = my_mechs;
1473 spnego.negTokenInit.reqFlags = data_blob_null;
1474 spnego.negTokenInit.reqFlagsPadding = 0;
1475 spnego.negTokenInit.mechListMIC = null_blob;
1477 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1478 &spnego.negTokenInit.mechToken);
1480 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1481 NT_STATUS_IS_OK(status)) ) {
1482 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1483 nt_errstr(status)));
1484 ntlmssp_end(&client_ntlmssp_state);
1488 spnego_write_data(ctx, &to_server, &spnego);
1489 data_blob_free(&spnego.negTokenInit.mechToken);
1491 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1492 data_blob_free(&to_server);
1493 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1494 TALLOC_FREE(to_server_base64);
1498 static void manage_client_ntlmssp_targ(struct spnego_data spnego)
1501 DATA_BLOB null_blob = data_blob_null;
1503 DATA_BLOB to_server;
1504 char *to_server_base64;
1505 TALLOC_CTX *ctx = talloc_tos();
1507 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1509 if (client_ntlmssp_state == NULL) {
1510 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1511 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1515 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1516 x_fprintf(x_stdout, "NA\n");
1517 ntlmssp_end(&client_ntlmssp_state);
1521 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1522 x_fprintf(x_stdout, "AF\n");
1523 ntlmssp_end(&client_ntlmssp_state);
1527 status = ntlmssp_update(client_ntlmssp_state,
1528 spnego.negTokenTarg.responseToken,
1531 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1532 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1533 "ntlmssp_client_update, got: %s\n",
1534 nt_errstr(status)));
1535 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1536 "ntlmssp_client_update\n");
1537 data_blob_free(&request);
1538 ntlmssp_end(&client_ntlmssp_state);
1542 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1543 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1544 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1545 spnego.negTokenTarg.responseToken = request;
1546 spnego.negTokenTarg.mechListMIC = null_blob;
1548 spnego_write_data(ctx, &to_server, &spnego);
1549 data_blob_free(&request);
1551 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1552 data_blob_free(&to_server);
1553 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1554 TALLOC_FREE(to_server_base64);
1560 static bool manage_client_krb5_init(struct spnego_data spnego)
1563 DATA_BLOB tkt, to_server;
1564 DATA_BLOB session_key_krb5 = data_blob_null;
1565 struct spnego_data reply;
1569 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1571 TALLOC_CTX *ctx = talloc_tos();
1573 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1574 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1575 DEBUG(1, ("Did not get a principal for krb5\n"));
1579 principal = (char *)SMB_MALLOC(
1580 spnego.negTokenInit.mechListMIC.length+1);
1582 if (principal == NULL) {
1583 DEBUG(1, ("Could not malloc principal\n"));
1587 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1588 spnego.negTokenInit.mechListMIC.length);
1589 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1591 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1596 /* Let's try to first get the TGT, for that we need a
1599 if (opt_password == NULL) {
1600 DEBUG(10, ("Requesting password\n"));
1601 x_fprintf(x_stdout, "PW\n");
1605 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1610 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1611 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1615 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1618 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1623 data_blob_free(&session_key_krb5);
1627 reply.type = SPNEGO_NEG_TOKEN_INIT;
1628 reply.negTokenInit.mechTypes = my_mechs;
1629 reply.negTokenInit.reqFlags = data_blob_null;
1630 reply.negTokenInit.reqFlagsPadding = 0;
1631 reply.negTokenInit.mechToken = tkt;
1632 reply.negTokenInit.mechListMIC = data_blob_null;
1634 len = spnego_write_data(ctx, &to_server, &reply);
1635 data_blob_free(&tkt);
1638 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1642 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1643 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1645 TALLOC_FREE(reply_base64);
1646 data_blob_free(&to_server);
1647 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1651 static void manage_client_krb5_targ(struct spnego_data spnego)
1653 switch (spnego.negTokenTarg.negResult) {
1654 case SPNEGO_ACCEPT_INCOMPLETE:
1655 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1656 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1657 "ACCEPT_INCOMPLETE\n");
1659 case SPNEGO_ACCEPT_COMPLETED:
1660 DEBUG(10, ("Accept completed\n"));
1661 x_fprintf(x_stdout, "AF\n");
1664 DEBUG(10, ("Rejected\n"));
1665 x_fprintf(x_stdout, "NA\n");
1668 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1669 x_fprintf(x_stdout, "AF\n");
1675 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1676 char *buf, int length)
1679 struct spnego_data spnego;
1681 TALLOC_CTX *ctx = talloc_tos();
1683 if (!opt_username || !*opt_username) {
1684 x_fprintf(x_stderr, "username must be specified!\n\n");
1688 if (strlen(buf) <= 3) {
1689 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1690 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1694 request = base64_decode_data_blob(buf+3);
1696 if (strncmp(buf, "PW ", 3) == 0) {
1698 /* We asked for a password and obviously got it :-) */
1700 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1702 if (opt_password == NULL) {
1703 DEBUG(1, ("Out of memory\n"));
1704 x_fprintf(x_stdout, "BH Out of memory\n");
1705 data_blob_free(&request);
1709 x_fprintf(x_stdout, "OK\n");
1710 data_blob_free(&request);
1714 if ( (strncmp(buf, "TT ", 3) != 0) &&
1715 (strncmp(buf, "AF ", 3) != 0) &&
1716 (strncmp(buf, "NA ", 3) != 0) ) {
1717 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1718 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1719 data_blob_free(&request);
1723 /* So we got a server challenge to generate a SPNEGO
1724 client-to-server request... */
1726 len = spnego_read_data(ctx, request, &spnego);
1727 data_blob_free(&request);
1730 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1731 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1735 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1737 /* The server offers a list of mechanisms */
1739 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1741 while (*mechType != NULL) {
1744 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1745 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1746 if (manage_client_krb5_init(spnego))
1751 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1752 if (manage_client_ntlmssp_init(spnego))
1759 DEBUG(1, ("Server offered no compatible mechanism\n"));
1760 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1764 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1766 if (spnego.negTokenTarg.supportedMech == NULL) {
1767 /* On accept/reject Windows does not send the
1768 mechanism anymore. Handle that here and
1769 shut down the mechanisms. */
1771 switch (spnego.negTokenTarg.negResult) {
1772 case SPNEGO_ACCEPT_COMPLETED:
1773 x_fprintf(x_stdout, "AF\n");
1776 x_fprintf(x_stdout, "NA\n");
1779 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1780 "unknown negResult: %d\n",
1781 spnego.negTokenTarg.negResult));
1782 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1783 " no mech and an unknown "
1787 ntlmssp_end(&client_ntlmssp_state);
1791 if (strcmp(spnego.negTokenTarg.supportedMech,
1792 OID_NTLMSSP) == 0) {
1793 manage_client_ntlmssp_targ(spnego);
1798 if (strcmp(spnego.negTokenTarg.supportedMech,
1799 OID_KERBEROS5_OLD) == 0) {
1800 manage_client_krb5_targ(spnego);
1807 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1808 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1812 spnego_free_data(&spnego);
1816 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1817 char *buf, int length)
1819 char *request, *parameter;
1820 static DATA_BLOB challenge;
1821 static DATA_BLOB lm_response;
1822 static DATA_BLOB nt_response;
1823 static char *full_username;
1824 static char *username;
1825 static char *domain;
1826 static char *plaintext_password;
1827 static bool ntlm_server_1_user_session_key;
1828 static bool ntlm_server_1_lm_session_key;
1830 if (strequal(buf, ".")) {
1831 if (!full_username && !username) {
1832 x_fprintf(x_stdout, "Error: No username supplied!\n");
1833 } else if (plaintext_password) {
1834 /* handle this request as plaintext */
1835 if (!full_username) {
1836 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1837 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1841 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1842 x_fprintf(x_stdout, "Authenticated: Yes\n");
1844 x_fprintf(x_stdout, "Authenticated: No\n");
1846 } else if (!lm_response.data && !nt_response.data) {
1847 x_fprintf(x_stdout, "Error: No password supplied!\n");
1848 } else if (!challenge.data) {
1849 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1851 char *error_string = NULL;
1853 uchar user_session_key[16];
1856 if (full_username && !username) {
1858 fstring fstr_domain;
1860 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1861 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1862 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1864 SAFE_FREE(username);
1866 username = smb_xstrdup(fstr_user);
1867 domain = smb_xstrdup(fstr_domain);
1871 domain = smb_xstrdup(get_winbind_domain());
1874 if (ntlm_server_1_lm_session_key)
1875 flags |= WBFLAG_PAM_LMKEY;
1877 if (ntlm_server_1_user_session_key)
1878 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1880 if (!NT_STATUS_IS_OK(
1881 contact_winbind_auth_crap(username,
1893 x_fprintf(x_stdout, "Authenticated: No\n");
1894 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1896 static char zeros[16];
1898 char *hex_user_session_key;
1900 x_fprintf(x_stdout, "Authenticated: Yes\n");
1902 if (ntlm_server_1_lm_session_key
1903 && (memcmp(zeros, lm_key,
1904 sizeof(lm_key)) != 0)) {
1905 hex_lm_key = hex_encode_talloc(NULL,
1906 (const unsigned char *)lm_key,
1908 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1909 TALLOC_FREE(hex_lm_key);
1912 if (ntlm_server_1_user_session_key
1913 && (memcmp(zeros, user_session_key,
1914 sizeof(user_session_key)) != 0)) {
1915 hex_user_session_key = hex_encode_talloc(NULL,
1916 (const unsigned char *)user_session_key,
1917 sizeof(user_session_key));
1918 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1919 TALLOC_FREE(hex_user_session_key);
1922 SAFE_FREE(error_string);
1924 /* clear out the state */
1925 challenge = data_blob_null;
1926 nt_response = data_blob_null;
1927 lm_response = data_blob_null;
1928 SAFE_FREE(full_username);
1929 SAFE_FREE(username);
1931 SAFE_FREE(plaintext_password);
1932 ntlm_server_1_user_session_key = False;
1933 ntlm_server_1_lm_session_key = False;
1934 x_fprintf(x_stdout, ".\n");
1941 /* Indicates a base64 encoded structure */
1942 parameter = strstr_m(request, ":: ");
1944 parameter = strstr_m(request, ": ");
1947 DEBUG(0, ("Parameter not found!\n"));
1948 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1965 base64_decode_inplace(parameter);
1968 if (strequal(request, "LANMAN-Challenge")) {
1969 challenge = strhex_to_data_blob(NULL, parameter);
1970 if (challenge.length != 8) {
1971 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1973 (int)challenge.length);
1974 challenge = data_blob_null;
1976 } else if (strequal(request, "NT-Response")) {
1977 nt_response = strhex_to_data_blob(NULL, parameter);
1978 if (nt_response.length < 24) {
1979 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1981 (int)nt_response.length);
1982 nt_response = data_blob_null;
1984 } else if (strequal(request, "LANMAN-Response")) {
1985 lm_response = strhex_to_data_blob(NULL, parameter);
1986 if (lm_response.length != 24) {
1987 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
1989 (int)lm_response.length);
1990 lm_response = data_blob_null;
1992 } else if (strequal(request, "Password")) {
1993 plaintext_password = smb_xstrdup(parameter);
1994 } else if (strequal(request, "NT-Domain")) {
1995 domain = smb_xstrdup(parameter);
1996 } else if (strequal(request, "Username")) {
1997 username = smb_xstrdup(parameter);
1998 } else if (strequal(request, "Full-Username")) {
1999 full_username = smb_xstrdup(parameter);
2000 } else if (strequal(request, "Request-User-Session-Key")) {
2001 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
2002 } else if (strequal(request, "Request-LanMan-Session-Key")) {
2003 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
2005 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2009 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
2010 char *buf, int length)
2012 char *request, *parameter;
2013 static DATA_BLOB new_nt_pswd;
2014 static DATA_BLOB old_nt_hash_enc;
2015 static DATA_BLOB new_lm_pswd;
2016 static DATA_BLOB old_lm_hash_enc;
2017 static char *full_username = NULL;
2018 static char *username = NULL;
2019 static char *domain = NULL;
2020 static char *newpswd = NULL;
2021 static char *oldpswd = NULL;
2023 if (strequal(buf, ".")) {
2024 if(newpswd && oldpswd) {
2025 uchar old_nt_hash[16];
2026 uchar old_lm_hash[16];
2027 uchar new_nt_hash[16];
2028 uchar new_lm_hash[16];
2030 new_nt_pswd = data_blob(NULL, 516);
2031 old_nt_hash_enc = data_blob(NULL, 16);
2033 /* Calculate the MD4 hash (NT compatible) of the
2035 E_md4hash(oldpswd, old_nt_hash);
2036 E_md4hash(newpswd, new_nt_hash);
2038 /* E_deshash returns false for 'long'
2039 passwords (> 14 DOS chars).
2041 Therefore, don't send a buffer
2042 encrypted with the truncated hash
2043 (it could allow an even easier
2044 attack on the password)
2046 Likewise, obey the admin's restriction
2049 if (lp_client_lanman_auth() &&
2050 E_deshash(newpswd, new_lm_hash) &&
2051 E_deshash(oldpswd, old_lm_hash)) {
2052 new_lm_pswd = data_blob(NULL, 516);
2053 old_lm_hash_enc = data_blob(NULL, 16);
2054 encode_pw_buffer(new_lm_pswd.data, newpswd,
2057 arcfour_crypt(new_lm_pswd.data, old_nt_hash, 516);
2058 E_old_pw_hash(new_nt_hash, old_lm_hash,
2059 old_lm_hash_enc.data);
2061 new_lm_pswd.data = NULL;
2062 new_lm_pswd.length = 0;
2063 old_lm_hash_enc.data = NULL;
2064 old_lm_hash_enc.length = 0;
2067 encode_pw_buffer(new_nt_pswd.data, newpswd,
2070 arcfour_crypt(new_nt_pswd.data, old_nt_hash, 516);
2071 E_old_pw_hash(new_nt_hash, old_nt_hash,
2072 old_nt_hash_enc.data);
2075 if (!full_username && !username) {
2076 x_fprintf(x_stdout, "Error: No username supplied!\n");
2077 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2078 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2079 x_fprintf(x_stdout, "Error: No NT or LM password "
2080 "blobs supplied!\n");
2082 char *error_string = NULL;
2084 if (full_username && !username) {
2086 fstring fstr_domain;
2088 if (!parse_ntlm_auth_domain_user(full_username,
2091 /* username might be 'tainted', don't
2092 * print into our new-line
2093 * deleimianted stream */
2094 x_fprintf(x_stdout, "Error: Could not "
2095 "parse into domain and "
2097 SAFE_FREE(username);
2098 username = smb_xstrdup(full_username);
2100 SAFE_FREE(username);
2102 username = smb_xstrdup(fstr_user);
2103 domain = smb_xstrdup(fstr_domain);
2108 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2115 x_fprintf(x_stdout, "Password-Change: No\n");
2116 x_fprintf(x_stdout, "Password-Change-Error: "
2117 "%s\n.\n", error_string);
2119 x_fprintf(x_stdout, "Password-Change: Yes\n");
2122 SAFE_FREE(error_string);
2124 /* clear out the state */
2125 new_nt_pswd = data_blob_null;
2126 old_nt_hash_enc = data_blob_null;
2127 new_lm_pswd = data_blob_null;
2128 old_nt_hash_enc = data_blob_null;
2129 SAFE_FREE(full_username);
2130 SAFE_FREE(username);
2134 x_fprintf(x_stdout, ".\n");
2141 /* Indicates a base64 encoded structure */
2142 parameter = strstr_m(request, ":: ");
2144 parameter = strstr_m(request, ": ");
2147 DEBUG(0, ("Parameter not found!\n"));
2148 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2164 base64_decode_inplace(parameter);
2167 if (strequal(request, "new-nt-password-blob")) {
2168 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2169 if (new_nt_pswd.length != 516) {
2170 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2171 "(got %d bytes, expected 516)\n.\n",
2173 (int)new_nt_pswd.length);
2174 new_nt_pswd = data_blob_null;
2176 } else if (strequal(request, "old-nt-hash-blob")) {
2177 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2178 if (old_nt_hash_enc.length != 16) {
2179 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2180 "(got %d bytes, expected 16)\n.\n",
2182 (int)old_nt_hash_enc.length);
2183 old_nt_hash_enc = data_blob_null;
2185 } else if (strequal(request, "new-lm-password-blob")) {
2186 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2187 if (new_lm_pswd.length != 516) {
2188 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2189 "(got %d bytes, expected 516)\n.\n",
2191 (int)new_lm_pswd.length);
2192 new_lm_pswd = data_blob_null;
2195 else if (strequal(request, "old-lm-hash-blob")) {
2196 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2197 if (old_lm_hash_enc.length != 16)
2199 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2200 "(got %d bytes, expected 16)\n.\n",
2202 (int)old_lm_hash_enc.length);
2203 old_lm_hash_enc = data_blob_null;
2205 } else if (strequal(request, "nt-domain")) {
2206 domain = smb_xstrdup(parameter);
2207 } else if(strequal(request, "username")) {
2208 username = smb_xstrdup(parameter);
2209 } else if(strequal(request, "full-username")) {
2210 username = smb_xstrdup(parameter);
2211 } else if(strequal(request, "new-password")) {
2212 newpswd = smb_xstrdup(parameter);
2213 } else if (strequal(request, "old-password")) {
2214 oldpswd = smb_xstrdup(parameter);
2216 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2220 static void manage_squid_request(struct ntlm_auth_state *state,
2221 stdio_helper_function fn)
2224 char tmp[INITIAL_BUFFER_SIZE+1];
2225 int length, buf_size = 0;
2228 buf = talloc_strdup(state->mem_ctx, "");
2230 DEBUG(0, ("Failed to allocate input buffer.\n"));
2231 x_fprintf(x_stderr, "ERR\n");
2237 /* this is not a typo - x_fgets doesn't work too well under
2239 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2240 if (ferror(stdin)) {
2241 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2242 "(%s)\n", ferror(stdin),
2243 strerror(ferror(stdin))));
2250 buf = talloc_strdup_append_buffer(buf, tmp);
2251 buf_size += INITIAL_BUFFER_SIZE;
2253 if (buf_size > MAX_BUFFER_SIZE) {
2254 DEBUG(2, ("Oversized message\n"));
2255 x_fprintf(x_stderr, "ERR\n");
2260 c = strchr(buf, '\n');
2261 } while (c == NULL);
2266 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2268 if (buf[0] == '\0') {
2269 DEBUG(2, ("Invalid Request\n"));
2270 x_fprintf(x_stderr, "ERR\n");
2275 fn(state, buf, length);
2280 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2281 TALLOC_CTX *mem_ctx;
2282 struct ntlm_auth_state *state;
2284 /* initialize FDescs */
2285 x_setbuf(x_stdout, NULL);
2286 x_setbuf(x_stderr, NULL);
2288 mem_ctx = talloc_init("ntlm_auth");
2290 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2291 x_fprintf(x_stderr, "ERR\n");
2295 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2297 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2298 x_fprintf(x_stderr, "ERR\n");
2302 state->mem_ctx = mem_ctx;
2303 state->helper_mode = stdio_mode;
2306 manage_squid_request(state, fn);
2311 /* Authenticate a user with a challenge/response */
2313 static bool check_auth_crap(void)
2318 char user_session_key[16];
2320 char *hex_user_session_key;
2322 static uint8 zeros[16];
2324 x_setbuf(x_stdout, NULL);
2327 flags |= WBFLAG_PAM_LMKEY;
2329 if (request_user_session_key)
2330 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2332 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2334 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2340 (unsigned char *)lm_key,
2341 (unsigned char *)user_session_key,
2342 &error_string, NULL);
2344 if (!NT_STATUS_IS_OK(nt_status)) {
2345 x_fprintf(x_stdout, "%s (0x%x)\n",
2347 NT_STATUS_V(nt_status));
2348 SAFE_FREE(error_string);
2353 && (memcmp(zeros, lm_key,
2354 sizeof(lm_key)) != 0)) {
2355 hex_lm_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key,
2357 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2358 TALLOC_FREE(hex_lm_key);
2360 if (request_user_session_key
2361 && (memcmp(zeros, user_session_key,
2362 sizeof(user_session_key)) != 0)) {
2363 hex_user_session_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key,
2364 sizeof(user_session_key));
2365 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2366 TALLOC_FREE(hex_user_session_key);
2375 OPT_USERNAME = 1000,
2384 OPT_USER_SESSION_KEY,
2386 OPT_REQUIRE_MEMBERSHIP,
2387 OPT_USE_CACHED_CREDS,
2388 OPT_PAM_WINBIND_CONF
2391 int main(int argc, const char **argv)
2393 TALLOC_CTX *frame = talloc_stackframe();
2395 static const char *helper_protocol;
2396 static int diagnostics;
2398 static const char *hex_challenge;
2399 static const char *hex_lm_response;
2400 static const char *hex_nt_response;
2404 /* NOTE: DO NOT change this interface without considering the implications!
2405 This is an external interface, which other programs will use to interact
2409 /* We do not use single-letter command abbreviations, because they harm future
2410 interface stability. */
2412 struct poptOption long_options[] = {
2414 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2415 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2416 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2417 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2418 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2419 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2420 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2421 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2422 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2423 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2424 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2425 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2426 { "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" },
2427 { "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" },
2428 POPT_COMMON_CONFIGFILE
2433 /* Samba client initialisation */
2440 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2442 /* Parse command line options */
2445 poptPrintHelp(pc, stderr, 0);
2449 while((opt = poptGetNextOpt(pc)) != -1) {
2450 /* Get generic config options like --configfile */
2453 poptFreeContext(pc);
2455 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2456 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2457 get_dyn_CONFIGFILE(), strerror(errno));
2461 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2462 POPT_CONTEXT_KEEP_FIRST);
2464 while((opt = poptGetNextOpt(pc)) != -1) {
2467 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2468 if (opt_challenge.length != 8) {
2469 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2471 (int)opt_challenge.length);
2476 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2477 if (opt_lm_response.length != 24) {
2478 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2480 (int)opt_lm_response.length);
2486 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2487 if (opt_nt_response.length < 24) {
2488 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2490 (int)opt_nt_response.length);
2495 case OPT_REQUIRE_MEMBERSHIP:
2496 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2497 require_membership_of_sid = require_membership_of;
2504 char *domain = SMB_STRDUP(opt_username);
2505 char *p = strchr_m(domain, *lp_winbind_separator());
2509 if (opt_domain && !strequal(opt_domain, domain)) {
2510 x_fprintf(x_stderr, "Domain specified in username (%s) "
2511 "doesn't match specified domain (%s)!\n\n",
2512 domain, opt_domain);
2513 poptPrintHelp(pc, stderr, 0);
2516 opt_domain = domain;
2522 /* Note: if opt_domain is "" then send no domain */
2523 if (opt_domain == NULL) {
2524 opt_domain = get_winbind_domain();
2527 if (opt_workstation == NULL) {
2528 opt_workstation = "";
2531 if (helper_protocol) {
2533 for (i=0; i<NUM_HELPER_MODES; i++) {
2534 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2535 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2539 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2541 for (i=0; i<NUM_HELPER_MODES; i++) {
2542 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2548 if (!opt_username || !*opt_username) {
2549 x_fprintf(x_stderr, "username must be specified!\n\n");
2550 poptPrintHelp(pc, stderr, 0);
2554 if (opt_challenge.length) {
2555 if (!check_auth_crap()) {
2561 if (!opt_password) {
2562 opt_password = getpass("password: ");
2566 if (!diagnose_ntlm_auth()) {
2572 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2573 if (!check_plaintext_auth(user, opt_password, True)) {
2580 poptFreeContext(pc);