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"
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, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
566 static const char zeros[16] = { 0, };
568 char *error_string = NULL;
570 uint8 user_sess_key[16];
571 char *unix_name = NULL;
573 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
574 ntlmssp_state->workstation,
575 &ntlmssp_state->chal,
576 &ntlmssp_state->lm_resp,
577 &ntlmssp_state->nt_resp,
578 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
579 lm_key, user_sess_key,
580 &error_string, &unix_name);
582 if (NT_STATUS_IS_OK(nt_status)) {
583 if (memcmp(lm_key, zeros, 8) != 0) {
584 *lm_session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
585 memcpy(lm_session_key->data, lm_key, 8);
586 memset(lm_session_key->data+8, '\0', 8);
589 if (memcmp(user_sess_key, zeros, 16) != 0) {
590 *user_session_key = data_blob_talloc(ntlmssp_state, user_sess_key, 16);
592 ntlmssp_state->callback_private = talloc_strdup(ntlmssp_state,
595 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
596 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
597 ntlmssp_state->domain, ntlmssp_state->user,
598 ntlmssp_state->workstation,
599 error_string ? error_string : "unknown error (NULL)"));
600 ntlmssp_state->callback_private = NULL;
603 SAFE_FREE(error_string);
604 SAFE_FREE(unix_name);
608 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
611 struct samr_Password lm_pw, nt_pw;
613 nt_lm_owf_gen (opt_password, nt_pw.hash, lm_pw.hash);
615 nt_status = ntlm_password_check(ntlmssp_state,
617 &ntlmssp_state->chal,
618 &ntlmssp_state->lm_resp,
619 &ntlmssp_state->nt_resp,
622 ntlmssp_state->domain,
623 &lm_pw, &nt_pw, user_session_key, lm_session_key);
625 if (NT_STATUS_IS_OK(nt_status)) {
626 ntlmssp_state->callback_private = talloc_asprintf(ntlmssp_state,
627 "%s%c%s", ntlmssp_state->domain,
628 *lp_winbind_separator(),
629 ntlmssp_state->user);
631 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
632 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
633 nt_errstr(nt_status)));
634 ntlmssp_state->callback_private = NULL;
639 static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntlmssp_state)
642 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
643 status = NT_STATUS_UNSUCCESSFUL;
644 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
645 return NT_STATUS_INVALID_PARAMETER;
648 status = ntlmssp_client_start(NULL,
651 lp_client_ntlmv2_auth(),
652 client_ntlmssp_state);
654 if (!NT_STATUS_IS_OK(status)) {
655 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
657 ntlmssp_end(client_ntlmssp_state);
661 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
663 if (!NT_STATUS_IS_OK(status)) {
664 DEBUG(1, ("Could not set username: %s\n",
666 ntlmssp_end(client_ntlmssp_state);
670 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
672 if (!NT_STATUS_IS_OK(status)) {
673 DEBUG(1, ("Could not set domain: %s\n",
675 ntlmssp_end(client_ntlmssp_state);
680 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
682 if (!NT_STATUS_IS_OK(status)) {
683 DEBUG(1, ("Could not set password: %s\n",
685 ntlmssp_end(client_ntlmssp_state);
693 static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_state)
696 const char *netbios_name;
697 const char *netbios_domain;
698 const char *dns_name;
700 bool is_standalone = false;
703 netbios_name = global_myname();
704 netbios_domain = lp_workgroup();
706 netbios_name = get_winbind_netbios_name();
707 netbios_domain = get_winbind_domain();
709 /* This should be a 'netbios domain -> DNS domain' mapping */
710 dns_domain = get_mydnsdomname(talloc_tos());
712 strlower_m(dns_domain);
714 dns_name = get_mydnsfullname();
716 status = ntlmssp_server_start(NULL,
723 if (!NT_STATUS_IS_OK(status)) {
724 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
729 /* Have we been given a local password, or should we ask winbind? */
731 (*ntlmssp_state)->check_password = local_pw_check;
733 (*ntlmssp_state)->check_password = winbind_pw_check;
738 /*******************************************************************
739 Used by firefox to drive NTLM auth to IIS servers.
740 *******************************************************************/
742 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
745 struct winbindd_request wb_request;
746 struct winbindd_response wb_response;
750 /* get winbindd to do the ntlmssp step on our behalf */
751 ZERO_STRUCT(wb_request);
752 ZERO_STRUCT(wb_response);
755 * This is tricky here. If we set krb5_auth in pam_winbind.conf
756 * creds for users in trusted domain will be stored the winbindd
757 * child of the trusted domain. If we ask the primary domain for
758 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
759 * domain's child for ccache_ntlm_auth. that is to say, we have to
760 * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
762 ctrl = get_pam_winbind_config();
764 if (ctrl | WINBIND_KRB5_AUTH) {
765 wb_request.flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
768 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
769 "%s%c%s", opt_domain, winbind_separator(), opt_username);
770 wb_request.data.ccache_ntlm_auth.uid = geteuid();
771 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
772 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
773 wb_request.extra_len = initial_msg.length + challenge_msg.length;
775 if (wb_request.extra_len > 0) {
776 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
777 if (wb_request.extra_data.data == NULL) {
778 return NT_STATUS_NO_MEMORY;
781 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
782 memcpy(wb_request.extra_data.data + initial_msg.length,
783 challenge_msg.data, challenge_msg.length);
786 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
787 SAFE_FREE(wb_request.extra_data.data);
789 if (result != NSS_STATUS_SUCCESS) {
790 winbindd_free_response(&wb_response);
791 return NT_STATUS_UNSUCCESSFUL;
795 *reply = data_blob(wb_response.extra_data.data,
796 wb_response.data.ccache_ntlm_auth.auth_blob_len);
797 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
798 reply->data == NULL) {
799 winbindd_free_response(&wb_response);
800 return NT_STATUS_NO_MEMORY;
804 winbindd_free_response(&wb_response);
805 return NT_STATUS_MORE_PROCESSING_REQUIRED;
808 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
809 char *buf, int length)
811 DATA_BLOB request, reply;
814 if (strlen(buf) < 2) {
815 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
816 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
820 if (strlen(buf) > 3) {
821 if(strncmp(buf, "SF ", 3) == 0){
822 DEBUG(10, ("Setting flags to negotioate\n"));
823 TALLOC_FREE(state->want_feature_list);
824 state->want_feature_list = talloc_strdup(state->mem_ctx,
826 x_fprintf(x_stdout, "OK\n");
829 request = base64_decode_data_blob(buf + 3);
831 request = data_blob_null;
834 if ((strncmp(buf, "PW ", 3) == 0)) {
835 /* The calling application wants us to use a local password
836 * (rather than winbindd) */
838 opt_password = SMB_STRNDUP((const char *)request.data,
841 if (opt_password == NULL) {
842 DEBUG(1, ("Out of memory\n"));
843 x_fprintf(x_stdout, "BH Out of memory\n");
844 data_blob_free(&request);
848 x_fprintf(x_stdout, "OK\n");
849 data_blob_free(&request);
853 if (strncmp(buf, "YR", 2) == 0) {
854 if (state->ntlmssp_state)
855 ntlmssp_end(&state->ntlmssp_state);
856 state->svr_state = SERVER_INITIAL;
857 } else if (strncmp(buf, "KK", 2) == 0) {
858 /* No special preprocessing required */
859 } else if (strncmp(buf, "GF", 2) == 0) {
860 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
862 if (state->svr_state == SERVER_FINISHED) {
863 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
866 x_fprintf(x_stdout, "BH\n");
868 data_blob_free(&request);
870 } else if (strncmp(buf, "GK", 2) == 0) {
871 DEBUG(10, ("Requested NTLMSSP session key\n"));
872 if(state->have_session_key) {
873 char *key64 = base64_encode_data_blob(state->mem_ctx,
875 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
878 x_fprintf(x_stdout, "BH\n");
881 data_blob_free(&request);
884 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
885 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
889 if (!state->ntlmssp_state) {
890 nt_status = ntlm_auth_start_ntlmssp_server(
891 &state->ntlmssp_state);
892 if (!NT_STATUS_IS_OK(nt_status)) {
893 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
896 ntlmssp_want_feature_list(state->ntlmssp_state,
897 state->want_feature_list);
900 DEBUG(10, ("got NTLMSSP packet:\n"));
901 dump_data(10, request.data, request.length);
903 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
905 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
906 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
908 x_fprintf(x_stdout, "TT %s\n", reply_base64);
909 TALLOC_FREE(reply_base64);
910 data_blob_free(&reply);
911 state->svr_state = SERVER_CHALLENGE;
912 DEBUG(10, ("NTLMSSP challenge\n"));
913 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
914 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
915 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
917 ntlmssp_end(&state->ntlmssp_state);
918 } else if (!NT_STATUS_IS_OK(nt_status)) {
919 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
920 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
922 x_fprintf(x_stdout, "AF %s\n",
923 (char *)state->ntlmssp_state->callback_private);
924 DEBUG(10, ("NTLMSSP OK!\n"));
926 if(state->have_session_key)
927 data_blob_free(&state->session_key);
928 state->session_key = data_blob(
929 state->ntlmssp_state->session_key.data,
930 state->ntlmssp_state->session_key.length);
931 state->neg_flags = state->ntlmssp_state->neg_flags;
932 state->have_session_key = true;
933 state->svr_state = SERVER_FINISHED;
936 data_blob_free(&request);
939 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
940 char *buf, int length)
942 DATA_BLOB request, reply;
945 if (!opt_username || !*opt_username) {
946 x_fprintf(x_stderr, "username must be specified!\n\n");
950 if (strlen(buf) < 2) {
951 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
952 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
956 if (strlen(buf) > 3) {
957 if(strncmp(buf, "SF ", 3) == 0) {
958 DEBUG(10, ("Looking for flags to negotiate\n"));
959 talloc_free(state->want_feature_list);
960 state->want_feature_list = talloc_strdup(state->mem_ctx,
962 x_fprintf(x_stdout, "OK\n");
965 request = base64_decode_data_blob(buf + 3);
967 request = data_blob_null;
970 if (strncmp(buf, "PW ", 3) == 0) {
971 /* We asked for a password and obviously got it :-) */
973 opt_password = SMB_STRNDUP((const char *)request.data,
976 if (opt_password == NULL) {
977 DEBUG(1, ("Out of memory\n"));
978 x_fprintf(x_stdout, "BH Out of memory\n");
979 data_blob_free(&request);
983 x_fprintf(x_stdout, "OK\n");
984 data_blob_free(&request);
988 if (!state->ntlmssp_state && use_cached_creds) {
989 /* check whether cached credentials are usable. */
990 DATA_BLOB empty_blob = data_blob_null;
992 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
993 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
994 /* failed to use cached creds */
995 use_cached_creds = False;
999 if (opt_password == NULL && !use_cached_creds) {
1000 /* Request a password from the calling process. After
1001 sending it, the calling process should retry asking for the
1004 DEBUG(10, ("Requesting password\n"));
1005 x_fprintf(x_stdout, "PW\n");
1009 if (strncmp(buf, "YR", 2) == 0) {
1010 if (state->ntlmssp_state)
1011 ntlmssp_end(&state->ntlmssp_state);
1012 state->cli_state = CLIENT_INITIAL;
1013 } else if (strncmp(buf, "TT", 2) == 0) {
1014 /* No special preprocessing required */
1015 } else if (strncmp(buf, "GF", 2) == 0) {
1016 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1018 if(state->cli_state == CLIENT_FINISHED) {
1019 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
1022 x_fprintf(x_stdout, "BH\n");
1025 data_blob_free(&request);
1027 } else if (strncmp(buf, "GK", 2) == 0 ) {
1028 DEBUG(10, ("Requested session key\n"));
1030 if(state->cli_state == CLIENT_FINISHED) {
1031 char *key64 = base64_encode_data_blob(state->mem_ctx,
1032 state->session_key);
1033 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
1037 x_fprintf(x_stdout, "BH\n");
1040 data_blob_free(&request);
1043 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
1044 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
1048 if (!state->ntlmssp_state) {
1049 nt_status = ntlm_auth_start_ntlmssp_client(
1050 &state->ntlmssp_state);
1051 if (!NT_STATUS_IS_OK(nt_status)) {
1052 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1055 ntlmssp_want_feature_list(state->ntlmssp_state,
1056 state->want_feature_list);
1057 state->initial_message = data_blob_null;
1060 DEBUG(10, ("got NTLMSSP packet:\n"));
1061 dump_data(10, request.data, request.length);
1063 if (use_cached_creds && !opt_password &&
1064 (state->cli_state == CLIENT_RESPONSE)) {
1065 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
1068 nt_status = ntlmssp_update(state->ntlmssp_state, request,
1072 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1073 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
1075 if (state->cli_state == CLIENT_INITIAL) {
1076 x_fprintf(x_stdout, "YR %s\n", reply_base64);
1077 state->initial_message = reply;
1078 state->cli_state = CLIENT_RESPONSE;
1080 x_fprintf(x_stdout, "KK %s\n", reply_base64);
1081 data_blob_free(&reply);
1083 TALLOC_FREE(reply_base64);
1084 DEBUG(10, ("NTLMSSP challenge\n"));
1085 } else if (NT_STATUS_IS_OK(nt_status)) {
1086 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1088 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1089 TALLOC_FREE(reply_base64);
1091 if(state->have_session_key)
1092 data_blob_free(&state->session_key);
1094 state->session_key = data_blob(
1095 state->ntlmssp_state->session_key.data,
1096 state->ntlmssp_state->session_key.length);
1097 state->neg_flags = state->ntlmssp_state->neg_flags;
1098 state->have_session_key = true;
1100 DEBUG(10, ("NTLMSSP OK!\n"));
1101 state->cli_state = CLIENT_FINISHED;
1102 if (state->ntlmssp_state)
1103 ntlmssp_end(&state->ntlmssp_state);
1105 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1106 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1107 state->cli_state = CLIENT_ERROR;
1108 if (state->ntlmssp_state)
1109 ntlmssp_end(&state->ntlmssp_state);
1112 data_blob_free(&request);
1115 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1116 char *buf, int length)
1121 pass=(char *)memchr(buf,' ',length);
1123 DEBUG(2, ("Password not found. Denying access\n"));
1124 x_fprintf(x_stdout, "ERR\n");
1130 if (state->helper_mode == SQUID_2_5_BASIC) {
1131 rfc1738_unescape(user);
1132 rfc1738_unescape(pass);
1135 if (check_plaintext_auth(user, pass, False)) {
1136 x_fprintf(x_stdout, "OK\n");
1138 x_fprintf(x_stdout, "ERR\n");
1142 static void offer_gss_spnego_mechs(void) {
1145 struct spnego_data spnego;
1148 TALLOC_CTX *ctx = talloc_tos();
1152 ZERO_STRUCT(spnego);
1154 myname_lower = talloc_strdup(ctx, global_myname());
1155 if (!myname_lower) {
1158 strlower_m(myname_lower);
1160 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1165 /* Server negTokenInit (mech offerings) */
1166 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1167 spnego.negTokenInit.mechTypes = SMB_XMALLOC_ARRAY(const char *, 2);
1169 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
1170 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
1171 spnego.negTokenInit.mechTypes[2] = NULL;
1173 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
1174 spnego.negTokenInit.mechTypes[1] = NULL;
1178 spnego.negTokenInit.mechListMIC = data_blob(principal,
1181 len = spnego_write_data(ctx, &token, &spnego);
1182 spnego_free_data(&spnego);
1185 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1186 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1190 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1191 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1193 TALLOC_FREE(reply_base64);
1194 data_blob_free(&token);
1195 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1199 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1200 char *buf, int length)
1202 static struct ntlmssp_state *ntlmssp_state = NULL;
1203 struct spnego_data request, response;
1207 TALLOC_CTX *ctx = talloc_tos();
1210 char *domain = NULL;
1212 const char *reply_code;
1214 char *reply_argument = NULL;
1216 if (strlen(buf) < 2) {
1217 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1218 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1222 if (strncmp(buf, "YR", 2) == 0) {
1224 ntlmssp_end(&ntlmssp_state);
1225 } else if (strncmp(buf, "KK", 2) == 0) {
1228 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1229 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1233 if ( (strlen(buf) == 2)) {
1235 /* no client data, get the negTokenInit offering
1238 offer_gss_spnego_mechs();
1242 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1244 if (strlen(buf) <= 3) {
1245 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1246 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1250 token = base64_decode_data_blob(buf + 3);
1251 len = spnego_read_data(ctx, token, &request);
1252 data_blob_free(&token);
1255 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
1256 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1260 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1262 /* Second request from Client. This is where the
1263 client offers its mechanism to use. */
1265 if ( (request.negTokenInit.mechTypes == NULL) ||
1266 (request.negTokenInit.mechTypes[0] == NULL) ) {
1267 DEBUG(1, ("Client did not offer any mechanism"));
1268 x_fprintf(x_stdout, "BH Client did not offer any "
1273 status = NT_STATUS_UNSUCCESSFUL;
1274 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1276 if ( request.negTokenInit.mechToken.data == NULL ) {
1277 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1278 x_fprintf(x_stdout, "BH Client did not provide "
1283 if ( ntlmssp_state != NULL ) {
1284 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1285 "already got one\n"));
1286 x_fprintf(x_stdout, "BH Client wants a new "
1287 "NTLMSSP challenge, but "
1288 "already got one\n");
1289 ntlmssp_end(&ntlmssp_state);
1293 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1294 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1298 DEBUG(10, ("got NTLMSSP packet:\n"));
1299 dump_data(10, request.negTokenInit.mechToken.data,
1300 request.negTokenInit.mechToken.length);
1302 response.type = SPNEGO_NEG_TOKEN_TARG;
1303 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1304 response.negTokenTarg.mechListMIC = data_blob_null;
1306 status = ntlmssp_update(ntlmssp_state,
1307 request.negTokenInit.mechToken,
1308 &response.negTokenTarg.responseToken);
1312 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1314 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1317 DATA_BLOB session_key;
1318 struct PAC_DATA *pac_data = NULL;
1320 if ( request.negTokenInit.mechToken.data == NULL ) {
1321 DEBUG(1, ("Client did not provide Kerberos data\n"));
1322 x_fprintf(x_stdout, "BH Client did not provide "
1327 response.type = SPNEGO_NEG_TOKEN_TARG;
1328 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_KERBEROS5_OLD);
1329 response.negTokenTarg.mechListMIC = data_blob_null;
1330 response.negTokenTarg.responseToken = data_blob_null;
1332 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1333 &request.negTokenInit.mechToken,
1334 &principal, &pac_data, &ap_rep,
1335 &session_key, True);
1337 /* Now in "principal" we have the name we are
1338 authenticated as. */
1340 if (NT_STATUS_IS_OK(status)) {
1342 domain = strchr_m(principal, '@');
1344 if (domain == NULL) {
1345 DEBUG(1, ("Did not get a valid principal "
1346 "from ads_verify_ticket\n"));
1347 x_fprintf(x_stdout, "BH Did not get a "
1348 "valid principal from "
1349 "ads_verify_ticket\n");
1354 domain = SMB_STRDUP(domain);
1355 user = SMB_STRDUP(principal);
1357 data_blob_free(&ap_rep);
1360 TALLOC_FREE(mem_ctx);
1366 if ( (request.negTokenTarg.supportedMech == NULL) ||
1367 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1368 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1369 is the only one we support that sends this stuff */
1370 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1371 request.negTokenTarg.supportedMech));
1372 x_fprintf(x_stdout, "BH Got a negTokenTarg for "
1373 "something non-NTLMSSP\n");
1377 if (request.negTokenTarg.responseToken.data == NULL) {
1378 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1379 x_fprintf(x_stdout, "BH Got a negTokenTarg without a "
1380 "responseToken!\n");
1384 status = ntlmssp_update(ntlmssp_state,
1385 request.negTokenTarg.responseToken,
1386 &response.negTokenTarg.responseToken);
1388 response.type = SPNEGO_NEG_TOKEN_TARG;
1389 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1390 response.negTokenTarg.mechListMIC = data_blob_null;
1392 if (NT_STATUS_IS_OK(status)) {
1393 user = SMB_STRDUP(ntlmssp_state->user);
1394 domain = SMB_STRDUP(ntlmssp_state->domain);
1395 ntlmssp_end(&ntlmssp_state);
1399 spnego_free_data(&request);
1401 if (NT_STATUS_IS_OK(status)) {
1402 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1404 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1405 } else if (NT_STATUS_EQUAL(status,
1406 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1407 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1409 reply_argument = talloc_strdup(ctx, "*");
1411 response.negTokenTarg.negResult = SPNEGO_REJECT;
1413 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1416 if (!reply_argument) {
1417 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1418 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1425 len = spnego_write_data(ctx, &token, &response);
1426 spnego_free_data(&response);
1429 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1430 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1434 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1436 x_fprintf(x_stdout, "%s %s %s\n",
1437 reply_code, reply_base64, reply_argument);
1439 TALLOC_FREE(reply_base64);
1440 data_blob_free(&token);
1445 static struct ntlmssp_state *client_ntlmssp_state = NULL;
1447 static bool manage_client_ntlmssp_init(struct spnego_data spnego)
1450 DATA_BLOB null_blob = data_blob_null;
1451 DATA_BLOB to_server;
1452 char *to_server_base64;
1453 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1454 TALLOC_CTX *ctx = talloc_tos();
1456 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1458 if (client_ntlmssp_state != NULL) {
1459 DEBUG(1, ("Request for initial SPNEGO request where "
1460 "we already have a state\n"));
1464 if (!client_ntlmssp_state) {
1465 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1466 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1472 if (opt_password == NULL) {
1474 /* Request a password from the calling process. After
1475 sending it, the calling process should retry with
1476 the negTokenInit. */
1478 DEBUG(10, ("Requesting password\n"));
1479 x_fprintf(x_stdout, "PW\n");
1483 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1484 spnego.negTokenInit.mechTypes = my_mechs;
1485 spnego.negTokenInit.reqFlags = data_blob_null;
1486 spnego.negTokenInit.reqFlagsPadding = 0;
1487 spnego.negTokenInit.mechListMIC = null_blob;
1489 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1490 &spnego.negTokenInit.mechToken);
1492 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1493 NT_STATUS_IS_OK(status)) ) {
1494 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1495 nt_errstr(status)));
1496 ntlmssp_end(&client_ntlmssp_state);
1500 spnego_write_data(ctx, &to_server, &spnego);
1501 data_blob_free(&spnego.negTokenInit.mechToken);
1503 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1504 data_blob_free(&to_server);
1505 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1506 TALLOC_FREE(to_server_base64);
1510 static void manage_client_ntlmssp_targ(struct spnego_data spnego)
1513 DATA_BLOB null_blob = data_blob_null;
1515 DATA_BLOB to_server;
1516 char *to_server_base64;
1517 TALLOC_CTX *ctx = talloc_tos();
1519 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1521 if (client_ntlmssp_state == NULL) {
1522 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1523 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1527 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1528 x_fprintf(x_stdout, "NA\n");
1529 ntlmssp_end(&client_ntlmssp_state);
1533 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1534 x_fprintf(x_stdout, "AF\n");
1535 ntlmssp_end(&client_ntlmssp_state);
1539 status = ntlmssp_update(client_ntlmssp_state,
1540 spnego.negTokenTarg.responseToken,
1543 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1544 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1545 "ntlmssp_client_update, got: %s\n",
1546 nt_errstr(status)));
1547 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1548 "ntlmssp_client_update\n");
1549 data_blob_free(&request);
1550 ntlmssp_end(&client_ntlmssp_state);
1554 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1555 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1556 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1557 spnego.negTokenTarg.responseToken = request;
1558 spnego.negTokenTarg.mechListMIC = null_blob;
1560 spnego_write_data(ctx, &to_server, &spnego);
1561 data_blob_free(&request);
1563 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1564 data_blob_free(&to_server);
1565 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1566 TALLOC_FREE(to_server_base64);
1572 static bool manage_client_krb5_init(struct spnego_data spnego)
1575 DATA_BLOB tkt, to_server;
1576 DATA_BLOB session_key_krb5 = data_blob_null;
1577 struct spnego_data reply;
1581 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1583 TALLOC_CTX *ctx = talloc_tos();
1585 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1586 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1587 DEBUG(1, ("Did not get a principal for krb5\n"));
1591 principal = (char *)SMB_MALLOC(
1592 spnego.negTokenInit.mechListMIC.length+1);
1594 if (principal == NULL) {
1595 DEBUG(1, ("Could not malloc principal\n"));
1599 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1600 spnego.negTokenInit.mechListMIC.length);
1601 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1603 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1608 /* Let's try to first get the TGT, for that we need a
1611 if (opt_password == NULL) {
1612 DEBUG(10, ("Requesting password\n"));
1613 x_fprintf(x_stdout, "PW\n");
1617 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1622 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1623 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1627 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1630 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1635 data_blob_free(&session_key_krb5);
1639 reply.type = SPNEGO_NEG_TOKEN_INIT;
1640 reply.negTokenInit.mechTypes = my_mechs;
1641 reply.negTokenInit.reqFlags = data_blob_null;
1642 reply.negTokenInit.reqFlagsPadding = 0;
1643 reply.negTokenInit.mechToken = tkt;
1644 reply.negTokenInit.mechListMIC = data_blob_null;
1646 len = spnego_write_data(ctx, &to_server, &reply);
1647 data_blob_free(&tkt);
1650 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1654 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1655 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1657 TALLOC_FREE(reply_base64);
1658 data_blob_free(&to_server);
1659 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1663 static void manage_client_krb5_targ(struct spnego_data spnego)
1665 switch (spnego.negTokenTarg.negResult) {
1666 case SPNEGO_ACCEPT_INCOMPLETE:
1667 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1668 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1669 "ACCEPT_INCOMPLETE\n");
1671 case SPNEGO_ACCEPT_COMPLETED:
1672 DEBUG(10, ("Accept completed\n"));
1673 x_fprintf(x_stdout, "AF\n");
1676 DEBUG(10, ("Rejected\n"));
1677 x_fprintf(x_stdout, "NA\n");
1680 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1681 x_fprintf(x_stdout, "AF\n");
1687 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1688 char *buf, int length)
1691 struct spnego_data spnego;
1693 TALLOC_CTX *ctx = talloc_tos();
1695 if (!opt_username || !*opt_username) {
1696 x_fprintf(x_stderr, "username must be specified!\n\n");
1700 if (strlen(buf) <= 3) {
1701 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1702 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1706 request = base64_decode_data_blob(buf+3);
1708 if (strncmp(buf, "PW ", 3) == 0) {
1710 /* We asked for a password and obviously got it :-) */
1712 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1714 if (opt_password == NULL) {
1715 DEBUG(1, ("Out of memory\n"));
1716 x_fprintf(x_stdout, "BH Out of memory\n");
1717 data_blob_free(&request);
1721 x_fprintf(x_stdout, "OK\n");
1722 data_blob_free(&request);
1726 if ( (strncmp(buf, "TT ", 3) != 0) &&
1727 (strncmp(buf, "AF ", 3) != 0) &&
1728 (strncmp(buf, "NA ", 3) != 0) ) {
1729 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1730 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1731 data_blob_free(&request);
1735 /* So we got a server challenge to generate a SPNEGO
1736 client-to-server request... */
1738 len = spnego_read_data(ctx, request, &spnego);
1739 data_blob_free(&request);
1742 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1743 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1747 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1749 /* The server offers a list of mechanisms */
1751 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1753 while (*mechType != NULL) {
1756 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1757 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1758 if (manage_client_krb5_init(spnego))
1763 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1764 if (manage_client_ntlmssp_init(spnego))
1771 DEBUG(1, ("Server offered no compatible mechanism\n"));
1772 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1776 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1778 if (spnego.negTokenTarg.supportedMech == NULL) {
1779 /* On accept/reject Windows does not send the
1780 mechanism anymore. Handle that here and
1781 shut down the mechanisms. */
1783 switch (spnego.negTokenTarg.negResult) {
1784 case SPNEGO_ACCEPT_COMPLETED:
1785 x_fprintf(x_stdout, "AF\n");
1788 x_fprintf(x_stdout, "NA\n");
1791 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1792 "unknown negResult: %d\n",
1793 spnego.negTokenTarg.negResult));
1794 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1795 " no mech and an unknown "
1799 ntlmssp_end(&client_ntlmssp_state);
1803 if (strcmp(spnego.negTokenTarg.supportedMech,
1804 OID_NTLMSSP) == 0) {
1805 manage_client_ntlmssp_targ(spnego);
1810 if (strcmp(spnego.negTokenTarg.supportedMech,
1811 OID_KERBEROS5_OLD) == 0) {
1812 manage_client_krb5_targ(spnego);
1819 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1820 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1824 spnego_free_data(&spnego);
1828 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1829 char *buf, int length)
1831 char *request, *parameter;
1832 static DATA_BLOB challenge;
1833 static DATA_BLOB lm_response;
1834 static DATA_BLOB nt_response;
1835 static char *full_username;
1836 static char *username;
1837 static char *domain;
1838 static char *plaintext_password;
1839 static bool ntlm_server_1_user_session_key;
1840 static bool ntlm_server_1_lm_session_key;
1842 if (strequal(buf, ".")) {
1843 if (!full_username && !username) {
1844 x_fprintf(x_stdout, "Error: No username supplied!\n");
1845 } else if (plaintext_password) {
1846 /* handle this request as plaintext */
1847 if (!full_username) {
1848 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1849 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1853 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1854 x_fprintf(x_stdout, "Authenticated: Yes\n");
1856 x_fprintf(x_stdout, "Authenticated: No\n");
1858 } else if (!lm_response.data && !nt_response.data) {
1859 x_fprintf(x_stdout, "Error: No password supplied!\n");
1860 } else if (!challenge.data) {
1861 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1863 char *error_string = NULL;
1865 uchar user_session_key[16];
1868 if (full_username && !username) {
1870 fstring fstr_domain;
1872 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1873 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1874 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1876 SAFE_FREE(username);
1878 username = smb_xstrdup(fstr_user);
1879 domain = smb_xstrdup(fstr_domain);
1883 domain = smb_xstrdup(get_winbind_domain());
1886 if (ntlm_server_1_lm_session_key)
1887 flags |= WBFLAG_PAM_LMKEY;
1889 if (ntlm_server_1_user_session_key)
1890 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1892 if (!NT_STATUS_IS_OK(
1893 contact_winbind_auth_crap(username,
1905 x_fprintf(x_stdout, "Authenticated: No\n");
1906 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1908 static char zeros[16];
1910 char *hex_user_session_key;
1912 x_fprintf(x_stdout, "Authenticated: Yes\n");
1914 if (ntlm_server_1_lm_session_key
1915 && (memcmp(zeros, lm_key,
1916 sizeof(lm_key)) != 0)) {
1917 hex_lm_key = hex_encode_talloc(NULL,
1918 (const unsigned char *)lm_key,
1920 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1921 TALLOC_FREE(hex_lm_key);
1924 if (ntlm_server_1_user_session_key
1925 && (memcmp(zeros, user_session_key,
1926 sizeof(user_session_key)) != 0)) {
1927 hex_user_session_key = hex_encode_talloc(NULL,
1928 (const unsigned char *)user_session_key,
1929 sizeof(user_session_key));
1930 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1931 TALLOC_FREE(hex_user_session_key);
1934 SAFE_FREE(error_string);
1936 /* clear out the state */
1937 challenge = data_blob_null;
1938 nt_response = data_blob_null;
1939 lm_response = data_blob_null;
1940 SAFE_FREE(full_username);
1941 SAFE_FREE(username);
1943 SAFE_FREE(plaintext_password);
1944 ntlm_server_1_user_session_key = False;
1945 ntlm_server_1_lm_session_key = False;
1946 x_fprintf(x_stdout, ".\n");
1953 /* Indicates a base64 encoded structure */
1954 parameter = strstr_m(request, ":: ");
1956 parameter = strstr_m(request, ": ");
1959 DEBUG(0, ("Parameter not found!\n"));
1960 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1977 base64_decode_inplace(parameter);
1980 if (strequal(request, "LANMAN-Challenge")) {
1981 challenge = strhex_to_data_blob(NULL, parameter);
1982 if (challenge.length != 8) {
1983 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1985 (int)challenge.length);
1986 challenge = data_blob_null;
1988 } else if (strequal(request, "NT-Response")) {
1989 nt_response = strhex_to_data_blob(NULL, parameter);
1990 if (nt_response.length < 24) {
1991 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1993 (int)nt_response.length);
1994 nt_response = data_blob_null;
1996 } else if (strequal(request, "LANMAN-Response")) {
1997 lm_response = strhex_to_data_blob(NULL, parameter);
1998 if (lm_response.length != 24) {
1999 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2001 (int)lm_response.length);
2002 lm_response = data_blob_null;
2004 } else if (strequal(request, "Password")) {
2005 plaintext_password = smb_xstrdup(parameter);
2006 } else if (strequal(request, "NT-Domain")) {
2007 domain = smb_xstrdup(parameter);
2008 } else if (strequal(request, "Username")) {
2009 username = smb_xstrdup(parameter);
2010 } else if (strequal(request, "Full-Username")) {
2011 full_username = smb_xstrdup(parameter);
2012 } else if (strequal(request, "Request-User-Session-Key")) {
2013 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
2014 } else if (strequal(request, "Request-LanMan-Session-Key")) {
2015 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
2017 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2021 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
2022 char *buf, int length)
2024 char *request, *parameter;
2025 static DATA_BLOB new_nt_pswd;
2026 static DATA_BLOB old_nt_hash_enc;
2027 static DATA_BLOB new_lm_pswd;
2028 static DATA_BLOB old_lm_hash_enc;
2029 static char *full_username = NULL;
2030 static char *username = NULL;
2031 static char *domain = NULL;
2032 static char *newpswd = NULL;
2033 static char *oldpswd = NULL;
2035 if (strequal(buf, ".")) {
2036 if(newpswd && oldpswd) {
2037 uchar old_nt_hash[16];
2038 uchar old_lm_hash[16];
2039 uchar new_nt_hash[16];
2040 uchar new_lm_hash[16];
2042 new_nt_pswd = data_blob(NULL, 516);
2043 old_nt_hash_enc = data_blob(NULL, 16);
2045 /* Calculate the MD4 hash (NT compatible) of the
2047 E_md4hash(oldpswd, old_nt_hash);
2048 E_md4hash(newpswd, new_nt_hash);
2050 /* E_deshash returns false for 'long'
2051 passwords (> 14 DOS chars).
2053 Therefore, don't send a buffer
2054 encrypted with the truncated hash
2055 (it could allow an even easier
2056 attack on the password)
2058 Likewise, obey the admin's restriction
2061 if (lp_client_lanman_auth() &&
2062 E_deshash(newpswd, new_lm_hash) &&
2063 E_deshash(oldpswd, old_lm_hash)) {
2064 new_lm_pswd = data_blob(NULL, 516);
2065 old_lm_hash_enc = data_blob(NULL, 16);
2066 encode_pw_buffer(new_lm_pswd.data, newpswd,
2069 arcfour_crypt(new_lm_pswd.data, old_nt_hash, 516);
2070 E_old_pw_hash(new_nt_hash, old_lm_hash,
2071 old_lm_hash_enc.data);
2073 new_lm_pswd.data = NULL;
2074 new_lm_pswd.length = 0;
2075 old_lm_hash_enc.data = NULL;
2076 old_lm_hash_enc.length = 0;
2079 encode_pw_buffer(new_nt_pswd.data, newpswd,
2082 arcfour_crypt(new_nt_pswd.data, old_nt_hash, 516);
2083 E_old_pw_hash(new_nt_hash, old_nt_hash,
2084 old_nt_hash_enc.data);
2087 if (!full_username && !username) {
2088 x_fprintf(x_stdout, "Error: No username supplied!\n");
2089 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2090 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2091 x_fprintf(x_stdout, "Error: No NT or LM password "
2092 "blobs supplied!\n");
2094 char *error_string = NULL;
2096 if (full_username && !username) {
2098 fstring fstr_domain;
2100 if (!parse_ntlm_auth_domain_user(full_username,
2103 /* username might be 'tainted', don't
2104 * print into our new-line
2105 * deleimianted stream */
2106 x_fprintf(x_stdout, "Error: Could not "
2107 "parse into domain and "
2109 SAFE_FREE(username);
2110 username = smb_xstrdup(full_username);
2112 SAFE_FREE(username);
2114 username = smb_xstrdup(fstr_user);
2115 domain = smb_xstrdup(fstr_domain);
2120 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2127 x_fprintf(x_stdout, "Password-Change: No\n");
2128 x_fprintf(x_stdout, "Password-Change-Error: "
2129 "%s\n.\n", error_string);
2131 x_fprintf(x_stdout, "Password-Change: Yes\n");
2134 SAFE_FREE(error_string);
2136 /* clear out the state */
2137 new_nt_pswd = data_blob_null;
2138 old_nt_hash_enc = data_blob_null;
2139 new_lm_pswd = data_blob_null;
2140 old_nt_hash_enc = data_blob_null;
2141 SAFE_FREE(full_username);
2142 SAFE_FREE(username);
2146 x_fprintf(x_stdout, ".\n");
2153 /* Indicates a base64 encoded structure */
2154 parameter = strstr_m(request, ":: ");
2156 parameter = strstr_m(request, ": ");
2159 DEBUG(0, ("Parameter not found!\n"));
2160 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2176 base64_decode_inplace(parameter);
2179 if (strequal(request, "new-nt-password-blob")) {
2180 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2181 if (new_nt_pswd.length != 516) {
2182 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2183 "(got %d bytes, expected 516)\n.\n",
2185 (int)new_nt_pswd.length);
2186 new_nt_pswd = data_blob_null;
2188 } else if (strequal(request, "old-nt-hash-blob")) {
2189 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2190 if (old_nt_hash_enc.length != 16) {
2191 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2192 "(got %d bytes, expected 16)\n.\n",
2194 (int)old_nt_hash_enc.length);
2195 old_nt_hash_enc = data_blob_null;
2197 } else if (strequal(request, "new-lm-password-blob")) {
2198 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2199 if (new_lm_pswd.length != 516) {
2200 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2201 "(got %d bytes, expected 516)\n.\n",
2203 (int)new_lm_pswd.length);
2204 new_lm_pswd = data_blob_null;
2207 else if (strequal(request, "old-lm-hash-blob")) {
2208 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2209 if (old_lm_hash_enc.length != 16)
2211 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2212 "(got %d bytes, expected 16)\n.\n",
2214 (int)old_lm_hash_enc.length);
2215 old_lm_hash_enc = data_blob_null;
2217 } else if (strequal(request, "nt-domain")) {
2218 domain = smb_xstrdup(parameter);
2219 } else if(strequal(request, "username")) {
2220 username = smb_xstrdup(parameter);
2221 } else if(strequal(request, "full-username")) {
2222 username = smb_xstrdup(parameter);
2223 } else if(strequal(request, "new-password")) {
2224 newpswd = smb_xstrdup(parameter);
2225 } else if (strequal(request, "old-password")) {
2226 oldpswd = smb_xstrdup(parameter);
2228 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2232 static void manage_squid_request(struct ntlm_auth_state *state,
2233 stdio_helper_function fn)
2236 char tmp[INITIAL_BUFFER_SIZE+1];
2237 int length, buf_size = 0;
2240 buf = talloc_strdup(state->mem_ctx, "");
2242 DEBUG(0, ("Failed to allocate input buffer.\n"));
2243 x_fprintf(x_stderr, "ERR\n");
2249 /* this is not a typo - x_fgets doesn't work too well under
2251 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2252 if (ferror(stdin)) {
2253 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2254 "(%s)\n", ferror(stdin),
2255 strerror(ferror(stdin))));
2262 buf = talloc_strdup_append_buffer(buf, tmp);
2263 buf_size += INITIAL_BUFFER_SIZE;
2265 if (buf_size > MAX_BUFFER_SIZE) {
2266 DEBUG(2, ("Oversized message\n"));
2267 x_fprintf(x_stderr, "ERR\n");
2272 c = strchr(buf, '\n');
2273 } while (c == NULL);
2278 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2280 if (buf[0] == '\0') {
2281 DEBUG(2, ("Invalid Request\n"));
2282 x_fprintf(x_stderr, "ERR\n");
2287 fn(state, buf, length);
2292 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2293 TALLOC_CTX *mem_ctx;
2294 struct ntlm_auth_state *state;
2296 /* initialize FDescs */
2297 x_setbuf(x_stdout, NULL);
2298 x_setbuf(x_stderr, NULL);
2300 mem_ctx = talloc_init("ntlm_auth");
2302 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2303 x_fprintf(x_stderr, "ERR\n");
2307 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2309 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2310 x_fprintf(x_stderr, "ERR\n");
2314 state->mem_ctx = mem_ctx;
2315 state->helper_mode = stdio_mode;
2318 manage_squid_request(state, fn);
2323 /* Authenticate a user with a challenge/response */
2325 static bool check_auth_crap(void)
2330 char user_session_key[16];
2332 char *hex_user_session_key;
2334 static uint8 zeros[16];
2336 x_setbuf(x_stdout, NULL);
2339 flags |= WBFLAG_PAM_LMKEY;
2341 if (request_user_session_key)
2342 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2344 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2346 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2352 (unsigned char *)lm_key,
2353 (unsigned char *)user_session_key,
2354 &error_string, NULL);
2356 if (!NT_STATUS_IS_OK(nt_status)) {
2357 x_fprintf(x_stdout, "%s (0x%x)\n",
2359 NT_STATUS_V(nt_status));
2360 SAFE_FREE(error_string);
2365 && (memcmp(zeros, lm_key,
2366 sizeof(lm_key)) != 0)) {
2367 hex_lm_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key,
2369 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2370 TALLOC_FREE(hex_lm_key);
2372 if (request_user_session_key
2373 && (memcmp(zeros, user_session_key,
2374 sizeof(user_session_key)) != 0)) {
2375 hex_user_session_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key,
2376 sizeof(user_session_key));
2377 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2378 TALLOC_FREE(hex_user_session_key);
2387 OPT_USERNAME = 1000,
2396 OPT_USER_SESSION_KEY,
2398 OPT_REQUIRE_MEMBERSHIP,
2399 OPT_USE_CACHED_CREDS,
2400 OPT_PAM_WINBIND_CONF
2403 int main(int argc, const char **argv)
2405 TALLOC_CTX *frame = talloc_stackframe();
2407 static const char *helper_protocol;
2408 static int diagnostics;
2410 static const char *hex_challenge;
2411 static const char *hex_lm_response;
2412 static const char *hex_nt_response;
2416 /* NOTE: DO NOT change this interface without considering the implications!
2417 This is an external interface, which other programs will use to interact
2421 /* We do not use single-letter command abbreviations, because they harm future
2422 interface stability. */
2424 struct poptOption long_options[] = {
2426 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2427 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2428 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2429 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2430 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2431 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2432 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2433 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2434 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2435 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2436 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2437 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2438 { "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" },
2439 { "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" },
2440 POPT_COMMON_CONFIGFILE
2445 /* Samba client initialisation */
2452 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2454 /* Parse command line options */
2457 poptPrintHelp(pc, stderr, 0);
2461 while((opt = poptGetNextOpt(pc)) != -1) {
2462 /* Get generic config options like --configfile */
2465 poptFreeContext(pc);
2467 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2468 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2469 get_dyn_CONFIGFILE(), strerror(errno));
2473 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2474 POPT_CONTEXT_KEEP_FIRST);
2476 while((opt = poptGetNextOpt(pc)) != -1) {
2479 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2480 if (opt_challenge.length != 8) {
2481 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2483 (int)opt_challenge.length);
2488 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2489 if (opt_lm_response.length != 24) {
2490 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2492 (int)opt_lm_response.length);
2498 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2499 if (opt_nt_response.length < 24) {
2500 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2502 (int)opt_nt_response.length);
2507 case OPT_REQUIRE_MEMBERSHIP:
2508 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2509 require_membership_of_sid = require_membership_of;
2516 char *domain = SMB_STRDUP(opt_username);
2517 char *p = strchr_m(domain, *lp_winbind_separator());
2521 if (opt_domain && !strequal(opt_domain, domain)) {
2522 x_fprintf(x_stderr, "Domain specified in username (%s) "
2523 "doesn't match specified domain (%s)!\n\n",
2524 domain, opt_domain);
2525 poptPrintHelp(pc, stderr, 0);
2528 opt_domain = domain;
2534 /* Note: if opt_domain is "" then send no domain */
2535 if (opt_domain == NULL) {
2536 opt_domain = get_winbind_domain();
2539 if (opt_workstation == NULL) {
2540 opt_workstation = "";
2543 if (helper_protocol) {
2545 for (i=0; i<NUM_HELPER_MODES; i++) {
2546 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2547 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2551 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2553 for (i=0; i<NUM_HELPER_MODES; i++) {
2554 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2560 if (!opt_username || !*opt_username) {
2561 x_fprintf(x_stderr, "username must be specified!\n\n");
2562 poptPrintHelp(pc, stderr, 0);
2566 if (opt_challenge.length) {
2567 if (!check_auth_crap()) {
2573 if (!opt_password) {
2574 opt_password = getpass("password: ");
2578 if (!diagnose_ntlm_auth()) {
2584 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2585 if (!check_plaintext_auth(user, opt_password, True)) {
2592 poptFreeContext(pc);