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 "popt_common.h"
28 #include "utils/ntlm_auth.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "../libcli/auth/spnego.h"
31 #include "../libcli/auth/ntlmssp.h"
33 #include <iniparser.h>
34 #include "../lib/crypto/arcfour.h"
35 #include "libads/kerberos_proto.h"
36 #include "nsswitch/winbind_client.h"
38 #ifndef PAM_WINBIND_CONFIG_FILE
39 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
42 #define WINBIND_KRB5_AUTH 0x00000080
45 #define DBGC_CLASS DBGC_WINBIND
47 #define INITIAL_BUFFER_SIZE 300
48 #define MAX_BUFFER_SIZE 630000
50 enum stdio_helper_mode {
58 NTLM_CHANGE_PASSWORD_1,
62 enum ntlm_auth_cli_state {
69 enum ntlm_auth_svr_state {
76 struct ntlm_auth_state {
78 enum stdio_helper_mode helper_mode;
79 enum ntlm_auth_cli_state cli_state;
80 enum ntlm_auth_svr_state svr_state;
81 struct ntlmssp_state *ntlmssp_state;
83 char *want_feature_list;
84 bool have_session_key;
85 DATA_BLOB session_key;
86 DATA_BLOB initial_message;
89 typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
92 static void manage_squid_basic_request (struct ntlm_auth_state *state,
93 char *buf, int length);
95 static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
96 char *buf, int length);
98 static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
99 char *buf, int length);
101 static void manage_gss_spnego_request (struct ntlm_auth_state *state,
102 char *buf, int length);
104 static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
105 char *buf, int length);
107 static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
108 char *buf, int length);
110 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
111 char *buf, int length);
113 static const struct {
114 enum stdio_helper_mode mode;
116 stdio_helper_function fn;
117 } stdio_helper_protocols[] = {
118 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
119 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
120 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
121 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
122 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
123 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
124 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
125 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
126 { NUM_HELPER_MODES, NULL, NULL}
129 const char *opt_username;
130 const char *opt_domain;
131 const char *opt_workstation;
132 const char *opt_password;
133 static DATA_BLOB opt_challenge;
134 static DATA_BLOB opt_lm_response;
135 static DATA_BLOB opt_nt_response;
136 static int request_lm_key;
137 static int request_user_session_key;
138 static int use_cached_creds;
140 static const char *require_membership_of;
141 static const char *require_membership_of_sid;
142 static const char *opt_pam_winbind_conf;
144 static char winbind_separator(void)
146 struct winbindd_response response;
153 ZERO_STRUCT(response);
155 /* Send off request */
157 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
158 NSS_STATUS_SUCCESS) {
159 d_printf("could not obtain winbind separator!\n");
160 return *lp_winbind_separator();
163 sep = response.data.info.winbind_separator;
167 d_printf("winbind separator was NULL!\n");
168 return *lp_winbind_separator();
174 const char *get_winbind_domain(void)
176 struct winbindd_response response;
178 static fstring winbind_domain;
179 if (*winbind_domain) {
180 return winbind_domain;
183 ZERO_STRUCT(response);
185 /* Send off request */
187 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
188 NSS_STATUS_SUCCESS) {
189 DEBUG(0, ("could not obtain winbind domain name!\n"));
190 return lp_workgroup();
193 fstrcpy(winbind_domain, response.data.domain_name);
195 return winbind_domain;
199 const char *get_winbind_netbios_name(void)
201 struct winbindd_response response;
203 static fstring winbind_netbios_name;
205 if (*winbind_netbios_name) {
206 return winbind_netbios_name;
209 ZERO_STRUCT(response);
211 /* Send off request */
213 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
214 NSS_STATUS_SUCCESS) {
215 DEBUG(0, ("could not obtain winbind netbios name!\n"));
216 return global_myname();
219 fstrcpy(winbind_netbios_name, response.data.netbios_name);
221 return winbind_netbios_name;
225 DATA_BLOB get_challenge(void)
227 static DATA_BLOB chal;
228 if (opt_challenge.length)
229 return opt_challenge;
231 chal = data_blob(NULL, 8);
233 generate_random_buffer(chal.data, chal.length);
237 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
238 form DOMAIN/user into a domain and a user */
240 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
244 char *p = strchr(domuser,winbind_separator());
251 fstrcpy(domain, domuser);
252 domain[PTR_DIFF(p, domuser)] = 0;
258 static bool get_require_membership_sid(void) {
259 struct winbindd_request request;
260 struct winbindd_response response;
262 if (!require_membership_of) {
266 if (require_membership_of_sid) {
270 /* Otherwise, ask winbindd for the name->sid request */
272 ZERO_STRUCT(request);
273 ZERO_STRUCT(response);
275 if (!parse_ntlm_auth_domain_user(require_membership_of,
276 request.data.name.dom_name,
277 request.data.name.name)) {
278 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
279 require_membership_of));
283 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
284 NSS_STATUS_SUCCESS) {
285 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
286 require_membership_of));
290 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
292 if (require_membership_of_sid)
299 * Get some configuration from pam_winbind.conf to see if we
300 * need to contact trusted domain
303 int get_pam_winbind_config()
306 dictionary *d = NULL;
308 if (!opt_pam_winbind_conf || !*opt_pam_winbind_conf) {
309 opt_pam_winbind_conf = PAM_WINBIND_CONFIG_FILE;
312 d = iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf));
318 if (iniparser_getboolean(d, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
319 ctrl |= WINBIND_KRB5_AUTH;
322 iniparser_freedict(d);
327 /* Authenticate a user with a plaintext password */
329 static bool check_plaintext_auth(const char *user, const char *pass,
330 bool stdout_diagnostics)
332 struct winbindd_request request;
333 struct winbindd_response response;
336 if (!get_require_membership_sid()) {
340 /* Send off request */
342 ZERO_STRUCT(request);
343 ZERO_STRUCT(response);
345 fstrcpy(request.data.auth.user, user);
346 fstrcpy(request.data.auth.pass, pass);
347 if (require_membership_of_sid) {
348 strlcpy(request.data.auth.require_membership_of_sid,
349 require_membership_of_sid,
350 sizeof(request.data.auth.require_membership_of_sid));
353 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
355 /* Display response */
357 if (stdout_diagnostics) {
358 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
359 d_printf("Reading winbind reply failed! (0x01)\n");
362 d_printf("%s: %s (0x%x)\n",
363 response.data.auth.nt_status_string,
364 response.data.auth.error_string,
365 response.data.auth.nt_status);
367 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
368 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
371 DEBUG(3, ("%s: %s (0x%x)\n",
372 response.data.auth.nt_status_string,
373 response.data.auth.error_string,
374 response.data.auth.nt_status));
377 return (result == NSS_STATUS_SUCCESS);
380 /* authenticate a user with an encrypted username/password */
382 NTSTATUS contact_winbind_auth_crap(const char *username,
384 const char *workstation,
385 const DATA_BLOB *challenge,
386 const DATA_BLOB *lm_response,
387 const DATA_BLOB *nt_response,
390 uint8 user_session_key[16],
396 struct winbindd_request request;
397 struct winbindd_response response;
399 if (!get_require_membership_sid()) {
400 return NT_STATUS_INVALID_PARAMETER;
403 ZERO_STRUCT(request);
404 ZERO_STRUCT(response);
406 request.flags = flags;
408 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
410 if (require_membership_of_sid)
411 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
413 fstrcpy(request.data.auth_crap.user, username);
414 fstrcpy(request.data.auth_crap.domain, domain);
416 fstrcpy(request.data.auth_crap.workstation,
419 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
421 if (lm_response && lm_response->length) {
422 memcpy(request.data.auth_crap.lm_resp,
424 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
425 request.data.auth_crap.lm_resp_len = lm_response->length;
428 if (nt_response && nt_response->length) {
429 if (nt_response->length > sizeof(request.data.auth_crap.nt_resp)) {
430 request.flags = request.flags | WBFLAG_BIG_NTLMV2_BLOB;
431 request.extra_len = nt_response->length;
432 request.extra_data.data = SMB_MALLOC_ARRAY(char, request.extra_len);
433 if (request.extra_data.data == NULL) {
434 return NT_STATUS_NO_MEMORY;
436 memcpy(request.extra_data.data, nt_response->data,
437 nt_response->length);
440 memcpy(request.data.auth_crap.nt_resp,
441 nt_response->data, nt_response->length);
443 request.data.auth_crap.nt_resp_len = nt_response->length;
446 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
447 SAFE_FREE(request.extra_data.data);
449 /* Display response */
451 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
452 nt_status = NT_STATUS_UNSUCCESSFUL;
454 *error_string = smb_xstrdup("Reading winbind reply failed!");
455 winbindd_free_response(&response);
459 nt_status = (NT_STATUS(response.data.auth.nt_status));
460 if (!NT_STATUS_IS_OK(nt_status)) {
462 *error_string = smb_xstrdup(response.data.auth.error_string);
463 winbindd_free_response(&response);
467 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
468 memcpy(lm_key, response.data.auth.first_8_lm_hash,
469 sizeof(response.data.auth.first_8_lm_hash));
471 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
472 memcpy(user_session_key, response.data.auth.user_session_key,
473 sizeof(response.data.auth.user_session_key));
476 if (flags & WBFLAG_PAM_UNIX_NAME) {
477 *unix_name = SMB_STRDUP(response.data.auth.unix_username);
479 winbindd_free_response(&response);
480 return NT_STATUS_NO_MEMORY;
484 winbindd_free_response(&response);
488 /* contact server to change user password using auth crap */
489 static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
491 const DATA_BLOB new_nt_pswd,
492 const DATA_BLOB old_nt_hash_enc,
493 const DATA_BLOB new_lm_pswd,
494 const DATA_BLOB old_lm_hash_enc,
499 struct winbindd_request request;
500 struct winbindd_response response;
502 if (!get_require_membership_sid())
505 *error_string = smb_xstrdup("Can't get membership sid.");
506 return NT_STATUS_INVALID_PARAMETER;
509 ZERO_STRUCT(request);
510 ZERO_STRUCT(response);
513 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
515 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
517 if(new_nt_pswd.length)
519 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
520 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
523 if(old_nt_hash_enc.length)
525 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));
526 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
529 if(new_lm_pswd.length)
531 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
532 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
535 if(old_lm_hash_enc.length)
537 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));
538 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
541 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
543 /* Display response */
545 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
547 nt_status = NT_STATUS_UNSUCCESSFUL;
549 *error_string = smb_xstrdup("Reading winbind reply failed!");
550 winbindd_free_response(&response);
554 nt_status = (NT_STATUS(response.data.auth.nt_status));
555 if (!NT_STATUS_IS_OK(nt_status))
558 *error_string = smb_xstrdup(response.data.auth.error_string);
559 winbindd_free_response(&response);
563 winbindd_free_response(&response);
568 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx,
569 DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
571 static const char zeros[16] = { 0, };
573 char *error_string = NULL;
575 uint8 user_sess_key[16];
576 char *unix_name = NULL;
578 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
579 ntlmssp_state->client.netbios_name,
580 &ntlmssp_state->chal,
581 &ntlmssp_state->lm_resp,
582 &ntlmssp_state->nt_resp,
583 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
584 lm_key, user_sess_key,
585 &error_string, &unix_name);
587 if (NT_STATUS_IS_OK(nt_status)) {
588 if (memcmp(lm_key, zeros, 8) != 0) {
589 *lm_session_key = data_blob_talloc(mem_ctx, NULL, 16);
590 memcpy(lm_session_key->data, lm_key, 8);
591 memset(lm_session_key->data+8, '\0', 8);
594 if (memcmp(user_sess_key, zeros, 16) != 0) {
595 *user_session_key = data_blob_talloc(mem_ctx, user_sess_key, 16);
597 ntlmssp_state->callback_private = talloc_strdup(ntlmssp_state,
600 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
601 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
602 ntlmssp_state->domain, ntlmssp_state->user,
603 ntlmssp_state->client.netbios_name,
604 error_string ? error_string : "unknown error (NULL)"));
605 ntlmssp_state->callback_private = NULL;
608 SAFE_FREE(error_string);
609 SAFE_FREE(unix_name);
613 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx,
614 DATA_BLOB *user_session_key, 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(mem_ctx,
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->callback_private = talloc_asprintf(ntlmssp_state,
633 "%s%c%s", ntlmssp_state->domain,
634 *lp_winbind_separator(),
635 ntlmssp_state->user);
637 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
638 ntlmssp_state->domain, ntlmssp_state->user,
639 ntlmssp_state->client.netbios_name,
640 nt_errstr(nt_status)));
641 ntlmssp_state->callback_private = NULL;
646 static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntlmssp_state)
649 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
650 status = NT_STATUS_UNSUCCESSFUL;
651 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
652 return NT_STATUS_INVALID_PARAMETER;
655 status = ntlmssp_client_start(NULL,
658 lp_client_ntlmv2_auth(),
659 client_ntlmssp_state);
661 if (!NT_STATUS_IS_OK(status)) {
662 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
664 TALLOC_FREE(*client_ntlmssp_state);
668 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
670 if (!NT_STATUS_IS_OK(status)) {
671 DEBUG(1, ("Could not set username: %s\n",
673 TALLOC_FREE(*client_ntlmssp_state);
677 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
679 if (!NT_STATUS_IS_OK(status)) {
680 DEBUG(1, ("Could not set domain: %s\n",
682 TALLOC_FREE(*client_ntlmssp_state);
687 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
689 if (!NT_STATUS_IS_OK(status)) {
690 DEBUG(1, ("Could not set password: %s\n",
692 TALLOC_FREE(*client_ntlmssp_state);
700 static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_state)
703 const char *netbios_name;
704 const char *netbios_domain;
705 const char *dns_name;
707 bool is_standalone = false;
710 netbios_name = global_myname();
711 netbios_domain = lp_workgroup();
713 netbios_name = get_winbind_netbios_name();
714 netbios_domain = get_winbind_domain();
716 /* This should be a 'netbios domain -> DNS domain' mapping */
717 dns_domain = get_mydnsdomname(talloc_tos());
719 strlower_m(dns_domain);
721 dns_name = get_mydnsfullname();
723 status = ntlmssp_server_start(NULL,
730 if (!NT_STATUS_IS_OK(status)) {
731 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
736 /* Have we been given a local password, or should we ask winbind? */
738 (*ntlmssp_state)->check_password = local_pw_check;
740 (*ntlmssp_state)->check_password = winbind_pw_check;
745 /*******************************************************************
746 Used by firefox to drive NTLM auth to IIS servers.
747 *******************************************************************/
749 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
752 struct winbindd_request wb_request;
753 struct winbindd_response wb_response;
757 /* get winbindd to do the ntlmssp step on our behalf */
758 ZERO_STRUCT(wb_request);
759 ZERO_STRUCT(wb_response);
762 * This is tricky here. If we set krb5_auth in pam_winbind.conf
763 * creds for users in trusted domain will be stored the winbindd
764 * child of the trusted domain. If we ask the primary domain for
765 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
766 * domain's child for ccache_ntlm_auth. that is to say, we have to
767 * set WBFLAG_PAM_CONTACT_TRUSTDOM in request.flags.
769 ctrl = get_pam_winbind_config();
771 if (ctrl | WINBIND_KRB5_AUTH) {
772 wb_request.flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
775 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
776 "%s%c%s", opt_domain, winbind_separator(), opt_username);
777 wb_request.data.ccache_ntlm_auth.uid = geteuid();
778 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
779 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
780 wb_request.extra_len = initial_msg.length + challenge_msg.length;
782 if (wb_request.extra_len > 0) {
783 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
784 if (wb_request.extra_data.data == NULL) {
785 return NT_STATUS_NO_MEMORY;
788 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
789 memcpy(wb_request.extra_data.data + initial_msg.length,
790 challenge_msg.data, challenge_msg.length);
793 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
794 SAFE_FREE(wb_request.extra_data.data);
796 if (result != NSS_STATUS_SUCCESS) {
797 winbindd_free_response(&wb_response);
798 return NT_STATUS_UNSUCCESSFUL;
802 *reply = data_blob(wb_response.extra_data.data,
803 wb_response.data.ccache_ntlm_auth.auth_blob_len);
804 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
805 reply->data == NULL) {
806 winbindd_free_response(&wb_response);
807 return NT_STATUS_NO_MEMORY;
811 winbindd_free_response(&wb_response);
812 return NT_STATUS_MORE_PROCESSING_REQUIRED;
815 static void manage_squid_ntlmssp_request_int(struct ntlm_auth_state *state,
816 char *buf, int length,
820 DATA_BLOB request, reply;
823 if (strlen(buf) < 2) {
824 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
825 *response = talloc_strdup(mem_ctx, "BH NTLMSSP query invalid");
829 if (strlen(buf) > 3) {
830 if(strncmp(buf, "SF ", 3) == 0){
831 DEBUG(10, ("Setting flags to negotioate\n"));
832 TALLOC_FREE(state->want_feature_list);
833 state->want_feature_list = talloc_strdup(state->mem_ctx,
835 *response = talloc_strdup(mem_ctx, "OK");
838 request = base64_decode_data_blob(buf + 3);
840 request = data_blob_null;
843 if ((strncmp(buf, "PW ", 3) == 0)) {
844 /* The calling application wants us to use a local password
845 * (rather than winbindd) */
847 opt_password = SMB_STRNDUP((const char *)request.data,
850 if (opt_password == NULL) {
851 DEBUG(1, ("Out of memory\n"));
852 *response = talloc_strdup(mem_ctx, "BH Out of memory");
853 data_blob_free(&request);
857 *response = talloc_strdup(mem_ctx, "OK");
858 data_blob_free(&request);
862 if (strncmp(buf, "YR", 2) == 0) {
863 TALLOC_FREE(state->ntlmssp_state);
864 state->svr_state = SERVER_INITIAL;
865 } else if (strncmp(buf, "KK", 2) == 0) {
866 /* No special preprocessing required */
867 } else if (strncmp(buf, "GF", 2) == 0) {
868 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
870 if (state->svr_state == SERVER_FINISHED) {
871 *response = talloc_asprintf(mem_ctx, "GF 0x%08x",
875 *response = talloc_strdup(mem_ctx, "BH\n");
877 data_blob_free(&request);
879 } else if (strncmp(buf, "GK", 2) == 0) {
880 DEBUG(10, ("Requested NTLMSSP session key\n"));
881 if(state->have_session_key) {
882 char *key64 = base64_encode_data_blob(state->mem_ctx,
884 *response = talloc_asprintf(mem_ctx, "GK %s",
885 key64 ? key64 : "<NULL>");
888 *response = talloc_strdup(mem_ctx, "BH");
891 data_blob_free(&request);
894 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
895 *response = talloc_strdup(mem_ctx, "BH NTLMSSP query invalid");
899 if (!state->ntlmssp_state) {
900 nt_status = ntlm_auth_start_ntlmssp_server(
901 &state->ntlmssp_state);
902 if (!NT_STATUS_IS_OK(nt_status)) {
903 *response = talloc_asprintf(
904 mem_ctx, "BH %s", nt_errstr(nt_status));
907 ntlmssp_want_feature_list(state->ntlmssp_state,
908 state->want_feature_list);
911 DEBUG(10, ("got NTLMSSP packet:\n"));
912 dump_data(10, request.data, request.length);
914 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
916 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
917 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
919 *response = talloc_asprintf(mem_ctx, "TT %s", reply_base64);
920 TALLOC_FREE(reply_base64);
921 data_blob_free(&reply);
922 state->svr_state = SERVER_CHALLENGE;
923 DEBUG(10, ("NTLMSSP challenge\n"));
924 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
925 *response = talloc_asprintf(mem_ctx, "BH %s",
926 nt_errstr(nt_status));
927 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
929 TALLOC_FREE(state->ntlmssp_state);
930 } else if (!NT_STATUS_IS_OK(nt_status)) {
931 *response = talloc_asprintf(mem_ctx, "NA %s",
932 nt_errstr(nt_status));
933 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
935 *response = talloc_asprintf(
937 (char *)state->ntlmssp_state->callback_private);
938 DEBUG(10, ("NTLMSSP OK!\n"));
940 if(state->have_session_key)
941 data_blob_free(&state->session_key);
942 state->session_key = data_blob(
943 state->ntlmssp_state->session_key.data,
944 state->ntlmssp_state->session_key.length);
945 state->neg_flags = state->ntlmssp_state->neg_flags;
946 state->have_session_key = true;
947 state->svr_state = SERVER_FINISHED;
950 data_blob_free(&request);
953 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
954 char *buf, int length)
958 manage_squid_ntlmssp_request_int(state, buf, length,
959 talloc_tos(), &response);
961 if (response == NULL) {
962 x_fprintf(x_stdout, "BH Out of memory\n");
965 x_fprintf(x_stdout, "%s\n", response);
966 TALLOC_FREE(response);
969 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
970 char *buf, int length)
972 DATA_BLOB request, reply;
975 if (!opt_username || !*opt_username) {
976 x_fprintf(x_stderr, "username must be specified!\n\n");
980 if (strlen(buf) < 2) {
981 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
982 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
986 if (strlen(buf) > 3) {
987 if(strncmp(buf, "SF ", 3) == 0) {
988 DEBUG(10, ("Looking for flags to negotiate\n"));
989 talloc_free(state->want_feature_list);
990 state->want_feature_list = talloc_strdup(state->mem_ctx,
992 x_fprintf(x_stdout, "OK\n");
995 request = base64_decode_data_blob(buf + 3);
997 request = data_blob_null;
1000 if (strncmp(buf, "PW ", 3) == 0) {
1001 /* We asked for a password and obviously got it :-) */
1003 opt_password = SMB_STRNDUP((const char *)request.data,
1006 if (opt_password == NULL) {
1007 DEBUG(1, ("Out of memory\n"));
1008 x_fprintf(x_stdout, "BH Out of memory\n");
1009 data_blob_free(&request);
1013 x_fprintf(x_stdout, "OK\n");
1014 data_blob_free(&request);
1018 if (!state->ntlmssp_state && use_cached_creds) {
1019 /* check whether cached credentials are usable. */
1020 DATA_BLOB empty_blob = data_blob_null;
1022 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
1023 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1024 /* failed to use cached creds */
1025 use_cached_creds = False;
1029 if (opt_password == NULL && !use_cached_creds) {
1030 /* Request a password from the calling process. After
1031 sending it, the calling process should retry asking for the
1034 DEBUG(10, ("Requesting password\n"));
1035 x_fprintf(x_stdout, "PW\n");
1039 if (strncmp(buf, "YR", 2) == 0) {
1040 TALLOC_FREE(state->ntlmssp_state);
1041 state->cli_state = CLIENT_INITIAL;
1042 } else if (strncmp(buf, "TT", 2) == 0) {
1043 /* No special preprocessing required */
1044 } else if (strncmp(buf, "GF", 2) == 0) {
1045 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1047 if(state->cli_state == CLIENT_FINISHED) {
1048 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
1051 x_fprintf(x_stdout, "BH\n");
1054 data_blob_free(&request);
1056 } else if (strncmp(buf, "GK", 2) == 0 ) {
1057 DEBUG(10, ("Requested session key\n"));
1059 if(state->cli_state == CLIENT_FINISHED) {
1060 char *key64 = base64_encode_data_blob(state->mem_ctx,
1061 state->session_key);
1062 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
1066 x_fprintf(x_stdout, "BH\n");
1069 data_blob_free(&request);
1072 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
1073 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
1077 if (!state->ntlmssp_state) {
1078 nt_status = ntlm_auth_start_ntlmssp_client(
1079 &state->ntlmssp_state);
1080 if (!NT_STATUS_IS_OK(nt_status)) {
1081 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1084 ntlmssp_want_feature_list(state->ntlmssp_state,
1085 state->want_feature_list);
1086 state->initial_message = data_blob_null;
1089 DEBUG(10, ("got NTLMSSP packet:\n"));
1090 dump_data(10, request.data, request.length);
1092 if (use_cached_creds && !opt_password &&
1093 (state->cli_state == CLIENT_RESPONSE)) {
1094 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
1097 nt_status = ntlmssp_update(state->ntlmssp_state, request,
1101 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1102 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
1104 if (state->cli_state == CLIENT_INITIAL) {
1105 x_fprintf(x_stdout, "YR %s\n", reply_base64);
1106 state->initial_message = reply;
1107 state->cli_state = CLIENT_RESPONSE;
1109 x_fprintf(x_stdout, "KK %s\n", reply_base64);
1110 data_blob_free(&reply);
1112 TALLOC_FREE(reply_base64);
1113 DEBUG(10, ("NTLMSSP challenge\n"));
1114 } else if (NT_STATUS_IS_OK(nt_status)) {
1115 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1117 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1118 TALLOC_FREE(reply_base64);
1120 if(state->have_session_key)
1121 data_blob_free(&state->session_key);
1123 state->session_key = data_blob(
1124 state->ntlmssp_state->session_key.data,
1125 state->ntlmssp_state->session_key.length);
1126 state->neg_flags = state->ntlmssp_state->neg_flags;
1127 state->have_session_key = true;
1129 DEBUG(10, ("NTLMSSP OK!\n"));
1130 state->cli_state = CLIENT_FINISHED;
1131 TALLOC_FREE(state->ntlmssp_state);
1133 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1134 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1135 state->cli_state = CLIENT_ERROR;
1136 TALLOC_FREE(state->ntlmssp_state);
1139 data_blob_free(&request);
1142 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1143 char *buf, int length)
1148 pass=(char *)memchr(buf,' ',length);
1150 DEBUG(2, ("Password not found. Denying access\n"));
1151 x_fprintf(x_stdout, "ERR\n");
1157 if (state->helper_mode == SQUID_2_5_BASIC) {
1158 rfc1738_unescape(user);
1159 rfc1738_unescape(pass);
1162 if (check_plaintext_auth(user, pass, False)) {
1163 x_fprintf(x_stdout, "OK\n");
1165 x_fprintf(x_stdout, "ERR\n");
1169 static void offer_gss_spnego_mechs(void) {
1172 struct spnego_data spnego;
1175 TALLOC_CTX *ctx = talloc_tos();
1179 ZERO_STRUCT(spnego);
1181 myname_lower = talloc_strdup(ctx, global_myname());
1182 if (!myname_lower) {
1185 strlower_m(myname_lower);
1187 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1192 /* Server negTokenInit (mech offerings) */
1193 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1194 spnego.negTokenInit.mechTypes = talloc_array(ctx, const char *, 3);
1196 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_KERBEROS5_OLD);
1197 spnego.negTokenInit.mechTypes[1] = talloc_strdup(ctx, OID_NTLMSSP);
1198 spnego.negTokenInit.mechTypes[2] = NULL;
1200 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_NTLMSSP);
1201 spnego.negTokenInit.mechTypes[1] = NULL;
1205 spnego.negTokenInit.mechListMIC = data_blob_talloc(ctx, principal,
1208 len = spnego_write_data(ctx, &token, &spnego);
1209 spnego_free_data(&spnego);
1212 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1213 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1217 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1218 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1220 TALLOC_FREE(reply_base64);
1221 data_blob_free(&token);
1222 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1226 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1227 char *buf, int length)
1229 static struct ntlmssp_state *ntlmssp_state = NULL;
1230 struct spnego_data request, response;
1234 TALLOC_CTX *ctx = talloc_tos();
1237 char *domain = NULL;
1239 const char *reply_code;
1241 char *reply_argument = NULL;
1243 if (strlen(buf) < 2) {
1244 DEBUG(1, ("SPENGO query [%s] invalid\n", buf));
1245 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1249 if (strncmp(buf, "YR", 2) == 0) {
1250 TALLOC_FREE(ntlmssp_state);
1251 } else if (strncmp(buf, "KK", 2) == 0) {
1254 DEBUG(1, ("SPENGO query [%s] invalid\n", buf));
1255 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1259 if ( (strlen(buf) == 2)) {
1261 /* no client data, get the negTokenInit offering
1264 offer_gss_spnego_mechs();
1268 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1270 if (strlen(buf) <= 3) {
1271 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1272 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1276 token = base64_decode_data_blob(buf + 3);
1278 if ((token.length >= 7)
1279 && (strncmp((char *)token.data, "NTLMSSP", 7) == 0)) {
1282 DEBUG(10, ("Could not parse GSS-SPNEGO, trying raw "
1285 manage_squid_ntlmssp_request_int(state, buf, length,
1286 talloc_tos(), &reply);
1287 if (reply == NULL) {
1288 x_fprintf(x_stdout, "BH Out of memory\n");
1292 if (strncmp(reply, "AF ", 3) == 0) {
1293 x_fprintf(x_stdout, "AF * %s\n", reply+3);
1295 x_fprintf(x_stdout, "%s *\n", reply);
1302 len = spnego_read_data(ctx, token, &request);
1303 data_blob_free(&token);
1306 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1307 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1311 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1313 /* Second request from Client. This is where the
1314 client offers its mechanism to use. */
1316 if ( (request.negTokenInit.mechTypes == NULL) ||
1317 (request.negTokenInit.mechTypes[0] == NULL) ) {
1318 DEBUG(1, ("Client did not offer any mechanism\n"));
1319 x_fprintf(x_stdout, "BH Client did not offer any "
1324 status = NT_STATUS_UNSUCCESSFUL;
1325 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1327 if ( request.negTokenInit.mechToken.data == NULL ) {
1328 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1329 x_fprintf(x_stdout, "BH Client did not provide "
1334 if ( ntlmssp_state != NULL ) {
1335 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1336 "already got one\n"));
1337 x_fprintf(x_stdout, "BH Client wants a new "
1338 "NTLMSSP challenge, but "
1339 "already got one\n");
1340 TALLOC_FREE(ntlmssp_state);
1344 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1345 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1349 DEBUG(10, ("got NTLMSSP packet:\n"));
1350 dump_data(10, request.negTokenInit.mechToken.data,
1351 request.negTokenInit.mechToken.length);
1353 response.type = SPNEGO_NEG_TOKEN_TARG;
1354 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
1355 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1357 status = ntlmssp_update(ntlmssp_state,
1358 request.negTokenInit.mechToken,
1359 &response.negTokenTarg.responseToken);
1363 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1365 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1368 DATA_BLOB session_key;
1369 struct PAC_LOGON_INFO *logon_info = NULL;
1371 if ( request.negTokenInit.mechToken.data == NULL ) {
1372 DEBUG(1, ("Client did not provide Kerberos data\n"));
1373 x_fprintf(x_stdout, "BH Client did not provide "
1378 response.type = SPNEGO_NEG_TOKEN_TARG;
1379 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_KERBEROS5_OLD);
1380 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1381 response.negTokenTarg.responseToken = data_blob_talloc(ctx, NULL, 0);
1383 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1384 &request.negTokenInit.mechToken,
1385 &principal, &logon_info, &ap_rep,
1386 &session_key, True);
1388 /* Now in "principal" we have the name we are
1389 authenticated as. */
1391 if (NT_STATUS_IS_OK(status)) {
1393 domain = strchr_m(principal, '@');
1395 if (domain == NULL) {
1396 DEBUG(1, ("Did not get a valid principal "
1397 "from ads_verify_ticket\n"));
1398 x_fprintf(x_stdout, "BH Did not get a "
1399 "valid principal from "
1400 "ads_verify_ticket\n");
1405 domain = SMB_STRDUP(domain);
1406 user = SMB_STRDUP(principal);
1408 data_blob_free(&ap_rep);
1411 TALLOC_FREE(mem_ctx);
1417 if ( (request.negTokenTarg.supportedMech == NULL) ||
1418 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1419 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1420 is the only one we support that sends this stuff */
1421 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1422 request.negTokenTarg.supportedMech));
1423 x_fprintf(x_stdout, "BH Got a negTokenTarg for "
1424 "something non-NTLMSSP\n");
1428 if (request.negTokenTarg.responseToken.data == NULL) {
1429 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1430 x_fprintf(x_stdout, "BH Got a negTokenTarg without a "
1431 "responseToken!\n");
1435 status = ntlmssp_update(ntlmssp_state,
1436 request.negTokenTarg.responseToken,
1437 &response.negTokenTarg.responseToken);
1439 response.type = SPNEGO_NEG_TOKEN_TARG;
1440 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
1441 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1443 if (NT_STATUS_IS_OK(status)) {
1444 user = SMB_STRDUP(ntlmssp_state->user);
1445 domain = SMB_STRDUP(ntlmssp_state->domain);
1446 TALLOC_FREE(ntlmssp_state);
1450 spnego_free_data(&request);
1452 if (NT_STATUS_IS_OK(status)) {
1453 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1455 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1456 } else if (NT_STATUS_EQUAL(status,
1457 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1458 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1460 reply_argument = talloc_strdup(ctx, "*");
1462 response.negTokenTarg.negResult = SPNEGO_REJECT;
1464 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1467 if (!reply_argument) {
1468 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1469 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1476 len = spnego_write_data(ctx, &token, &response);
1477 spnego_free_data(&response);
1480 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1481 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1485 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1487 x_fprintf(x_stdout, "%s %s %s\n",
1488 reply_code, reply_base64, reply_argument);
1490 TALLOC_FREE(reply_base64);
1491 data_blob_free(&token);
1496 static struct ntlmssp_state *client_ntlmssp_state = NULL;
1498 static bool manage_client_ntlmssp_init(struct spnego_data spnego)
1501 DATA_BLOB null_blob = data_blob_null;
1502 DATA_BLOB to_server;
1503 char *to_server_base64;
1504 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1505 TALLOC_CTX *ctx = talloc_tos();
1507 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1509 if (client_ntlmssp_state != NULL) {
1510 DEBUG(1, ("Request for initial SPNEGO request where "
1511 "we already have a state\n"));
1515 if (!client_ntlmssp_state) {
1516 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1517 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1523 if (opt_password == NULL) {
1525 /* Request a password from the calling process. After
1526 sending it, the calling process should retry with
1527 the negTokenInit. */
1529 DEBUG(10, ("Requesting password\n"));
1530 x_fprintf(x_stdout, "PW\n");
1534 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1535 spnego.negTokenInit.mechTypes = my_mechs;
1536 spnego.negTokenInit.reqFlags = data_blob_null;
1537 spnego.negTokenInit.reqFlagsPadding = 0;
1538 spnego.negTokenInit.mechListMIC = null_blob;
1540 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1541 &spnego.negTokenInit.mechToken);
1543 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1544 NT_STATUS_IS_OK(status)) ) {
1545 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1546 nt_errstr(status)));
1547 TALLOC_FREE(client_ntlmssp_state);
1551 spnego_write_data(ctx, &to_server, &spnego);
1552 data_blob_free(&spnego.negTokenInit.mechToken);
1554 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1555 data_blob_free(&to_server);
1556 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1557 TALLOC_FREE(to_server_base64);
1561 static void manage_client_ntlmssp_targ(struct spnego_data spnego)
1564 DATA_BLOB null_blob = data_blob_null;
1566 DATA_BLOB to_server;
1567 char *to_server_base64;
1568 TALLOC_CTX *ctx = talloc_tos();
1570 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1572 if (client_ntlmssp_state == NULL) {
1573 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1574 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1578 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1579 x_fprintf(x_stdout, "NA\n");
1580 TALLOC_FREE(client_ntlmssp_state);
1584 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1585 x_fprintf(x_stdout, "AF\n");
1586 TALLOC_FREE(client_ntlmssp_state);
1590 status = ntlmssp_update(client_ntlmssp_state,
1591 spnego.negTokenTarg.responseToken,
1594 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1595 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1596 "ntlmssp_client_update, got: %s\n",
1597 nt_errstr(status)));
1598 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1599 "ntlmssp_client_update\n");
1600 data_blob_free(&request);
1601 TALLOC_FREE(client_ntlmssp_state);
1605 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1606 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1607 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1608 spnego.negTokenTarg.responseToken = request;
1609 spnego.negTokenTarg.mechListMIC = null_blob;
1611 spnego_write_data(ctx, &to_server, &spnego);
1612 data_blob_free(&request);
1614 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1615 data_blob_free(&to_server);
1616 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1617 TALLOC_FREE(to_server_base64);
1623 static bool manage_client_krb5_init(struct spnego_data spnego)
1626 DATA_BLOB tkt, to_server;
1627 DATA_BLOB session_key_krb5 = data_blob_null;
1628 struct spnego_data reply;
1632 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1634 TALLOC_CTX *ctx = talloc_tos();
1636 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1637 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1638 DEBUG(1, ("Did not get a principal for krb5\n"));
1642 principal = (char *)SMB_MALLOC(
1643 spnego.negTokenInit.mechListMIC.length+1);
1645 if (principal == NULL) {
1646 DEBUG(1, ("Could not malloc principal\n"));
1650 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1651 spnego.negTokenInit.mechListMIC.length);
1652 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1654 retval = cli_krb5_get_ticket(ctx, principal, 0,
1655 &tkt, &session_key_krb5,
1656 0, NULL, NULL, NULL);
1660 /* Let's try to first get the TGT, for that we need a
1663 if (opt_password == NULL) {
1664 DEBUG(10, ("Requesting password\n"));
1665 x_fprintf(x_stdout, "PW\n");
1669 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1674 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1675 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1679 retval = cli_krb5_get_ticket(ctx, principal, 0,
1680 &tkt, &session_key_krb5,
1681 0, NULL, NULL, NULL);
1683 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1688 data_blob_free(&session_key_krb5);
1692 reply.type = SPNEGO_NEG_TOKEN_INIT;
1693 reply.negTokenInit.mechTypes = my_mechs;
1694 reply.negTokenInit.reqFlags = data_blob_null;
1695 reply.negTokenInit.reqFlagsPadding = 0;
1696 reply.negTokenInit.mechToken = tkt;
1697 reply.negTokenInit.mechListMIC = data_blob_null;
1699 len = spnego_write_data(ctx, &to_server, &reply);
1700 data_blob_free(&tkt);
1703 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1707 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1708 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1710 TALLOC_FREE(reply_base64);
1711 data_blob_free(&to_server);
1712 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1716 static void manage_client_krb5_targ(struct spnego_data spnego)
1718 switch (spnego.negTokenTarg.negResult) {
1719 case SPNEGO_ACCEPT_INCOMPLETE:
1720 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1721 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1722 "ACCEPT_INCOMPLETE\n");
1724 case SPNEGO_ACCEPT_COMPLETED:
1725 DEBUG(10, ("Accept completed\n"));
1726 x_fprintf(x_stdout, "AF\n");
1729 DEBUG(10, ("Rejected\n"));
1730 x_fprintf(x_stdout, "NA\n");
1733 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1734 x_fprintf(x_stdout, "AF\n");
1740 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1741 char *buf, int length)
1744 struct spnego_data spnego;
1746 TALLOC_CTX *ctx = talloc_tos();
1748 if (!opt_username || !*opt_username) {
1749 x_fprintf(x_stderr, "username must be specified!\n\n");
1753 if (strlen(buf) <= 3) {
1754 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1755 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1759 request = base64_decode_data_blob(buf+3);
1761 if (strncmp(buf, "PW ", 3) == 0) {
1763 /* We asked for a password and obviously got it :-) */
1765 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1767 if (opt_password == NULL) {
1768 DEBUG(1, ("Out of memory\n"));
1769 x_fprintf(x_stdout, "BH Out of memory\n");
1770 data_blob_free(&request);
1774 x_fprintf(x_stdout, "OK\n");
1775 data_blob_free(&request);
1779 if ( (strncmp(buf, "TT ", 3) != 0) &&
1780 (strncmp(buf, "AF ", 3) != 0) &&
1781 (strncmp(buf, "NA ", 3) != 0) ) {
1782 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1783 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1784 data_blob_free(&request);
1788 /* So we got a server challenge to generate a SPNEGO
1789 client-to-server request... */
1791 len = spnego_read_data(ctx, request, &spnego);
1792 data_blob_free(&request);
1795 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1796 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1800 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1802 /* The server offers a list of mechanisms */
1804 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1806 while (*mechType != NULL) {
1809 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1810 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1811 if (manage_client_krb5_init(spnego))
1816 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1817 if (manage_client_ntlmssp_init(spnego))
1824 DEBUG(1, ("Server offered no compatible mechanism\n"));
1825 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1829 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1831 if (spnego.negTokenTarg.supportedMech == NULL) {
1832 /* On accept/reject Windows does not send the
1833 mechanism anymore. Handle that here and
1834 shut down the mechanisms. */
1836 switch (spnego.negTokenTarg.negResult) {
1837 case SPNEGO_ACCEPT_COMPLETED:
1838 x_fprintf(x_stdout, "AF\n");
1841 x_fprintf(x_stdout, "NA\n");
1844 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1845 "unknown negResult: %d\n",
1846 spnego.negTokenTarg.negResult));
1847 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1848 " no mech and an unknown "
1852 TALLOC_FREE(client_ntlmssp_state);
1856 if (strcmp(spnego.negTokenTarg.supportedMech,
1857 OID_NTLMSSP) == 0) {
1858 manage_client_ntlmssp_targ(spnego);
1863 if (strcmp(spnego.negTokenTarg.supportedMech,
1864 OID_KERBEROS5_OLD) == 0) {
1865 manage_client_krb5_targ(spnego);
1872 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1873 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1877 spnego_free_data(&spnego);
1881 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1882 char *buf, int length)
1884 char *request, *parameter;
1885 static DATA_BLOB challenge;
1886 static DATA_BLOB lm_response;
1887 static DATA_BLOB nt_response;
1888 static char *full_username;
1889 static char *username;
1890 static char *domain;
1891 static char *plaintext_password;
1892 static bool ntlm_server_1_user_session_key;
1893 static bool ntlm_server_1_lm_session_key;
1895 if (strequal(buf, ".")) {
1896 if (!full_username && !username) {
1897 x_fprintf(x_stdout, "Error: No username supplied!\n");
1898 } else if (plaintext_password) {
1899 /* handle this request as plaintext */
1900 if (!full_username) {
1901 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1902 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1906 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1907 x_fprintf(x_stdout, "Authenticated: Yes\n");
1909 x_fprintf(x_stdout, "Authenticated: No\n");
1911 } else if (!lm_response.data && !nt_response.data) {
1912 x_fprintf(x_stdout, "Error: No password supplied!\n");
1913 } else if (!challenge.data) {
1914 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1916 char *error_string = NULL;
1918 uchar user_session_key[16];
1921 if (full_username && !username) {
1923 fstring fstr_domain;
1925 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1926 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1927 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1929 SAFE_FREE(username);
1931 username = smb_xstrdup(fstr_user);
1932 domain = smb_xstrdup(fstr_domain);
1936 domain = smb_xstrdup(get_winbind_domain());
1939 if (ntlm_server_1_lm_session_key)
1940 flags |= WBFLAG_PAM_LMKEY;
1942 if (ntlm_server_1_user_session_key)
1943 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1945 if (!NT_STATUS_IS_OK(
1946 contact_winbind_auth_crap(username,
1958 x_fprintf(x_stdout, "Authenticated: No\n");
1959 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1961 static char zeros[16];
1963 char *hex_user_session_key;
1965 x_fprintf(x_stdout, "Authenticated: Yes\n");
1967 if (ntlm_server_1_lm_session_key
1968 && (memcmp(zeros, lm_key,
1969 sizeof(lm_key)) != 0)) {
1970 hex_lm_key = hex_encode_talloc(NULL,
1971 (const unsigned char *)lm_key,
1973 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1974 TALLOC_FREE(hex_lm_key);
1977 if (ntlm_server_1_user_session_key
1978 && (memcmp(zeros, user_session_key,
1979 sizeof(user_session_key)) != 0)) {
1980 hex_user_session_key = hex_encode_talloc(NULL,
1981 (const unsigned char *)user_session_key,
1982 sizeof(user_session_key));
1983 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1984 TALLOC_FREE(hex_user_session_key);
1987 SAFE_FREE(error_string);
1989 /* clear out the state */
1990 challenge = data_blob_null;
1991 nt_response = data_blob_null;
1992 lm_response = data_blob_null;
1993 SAFE_FREE(full_username);
1994 SAFE_FREE(username);
1996 SAFE_FREE(plaintext_password);
1997 ntlm_server_1_user_session_key = False;
1998 ntlm_server_1_lm_session_key = False;
1999 x_fprintf(x_stdout, ".\n");
2006 /* Indicates a base64 encoded structure */
2007 parameter = strstr_m(request, ":: ");
2009 parameter = strstr_m(request, ": ");
2012 DEBUG(0, ("Parameter not found!\n"));
2013 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2030 base64_decode_inplace(parameter);
2033 if (strequal(request, "LANMAN-Challenge")) {
2034 challenge = strhex_to_data_blob(NULL, parameter);
2035 if (challenge.length != 8) {
2036 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
2038 (int)challenge.length);
2039 challenge = data_blob_null;
2041 } else if (strequal(request, "NT-Response")) {
2042 nt_response = strhex_to_data_blob(NULL, parameter);
2043 if (nt_response.length < 24) {
2044 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
2046 (int)nt_response.length);
2047 nt_response = data_blob_null;
2049 } else if (strequal(request, "LANMAN-Response")) {
2050 lm_response = strhex_to_data_blob(NULL, parameter);
2051 if (lm_response.length != 24) {
2052 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2054 (int)lm_response.length);
2055 lm_response = data_blob_null;
2057 } else if (strequal(request, "Password")) {
2058 plaintext_password = smb_xstrdup(parameter);
2059 } else if (strequal(request, "NT-Domain")) {
2060 domain = smb_xstrdup(parameter);
2061 } else if (strequal(request, "Username")) {
2062 username = smb_xstrdup(parameter);
2063 } else if (strequal(request, "Full-Username")) {
2064 full_username = smb_xstrdup(parameter);
2065 } else if (strequal(request, "Request-User-Session-Key")) {
2066 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
2067 } else if (strequal(request, "Request-LanMan-Session-Key")) {
2068 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
2070 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2074 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
2075 char *buf, int length)
2077 char *request, *parameter;
2078 static DATA_BLOB new_nt_pswd;
2079 static DATA_BLOB old_nt_hash_enc;
2080 static DATA_BLOB new_lm_pswd;
2081 static DATA_BLOB old_lm_hash_enc;
2082 static char *full_username = NULL;
2083 static char *username = NULL;
2084 static char *domain = NULL;
2085 static char *newpswd = NULL;
2086 static char *oldpswd = NULL;
2088 if (strequal(buf, ".")) {
2089 if(newpswd && oldpswd) {
2090 uchar old_nt_hash[16];
2091 uchar old_lm_hash[16];
2092 uchar new_nt_hash[16];
2093 uchar new_lm_hash[16];
2095 new_nt_pswd = data_blob(NULL, 516);
2096 old_nt_hash_enc = data_blob(NULL, 16);
2098 /* Calculate the MD4 hash (NT compatible) of the
2100 E_md4hash(oldpswd, old_nt_hash);
2101 E_md4hash(newpswd, new_nt_hash);
2103 /* E_deshash returns false for 'long'
2104 passwords (> 14 DOS chars).
2106 Therefore, don't send a buffer
2107 encrypted with the truncated hash
2108 (it could allow an even easier
2109 attack on the password)
2111 Likewise, obey the admin's restriction
2114 if (lp_client_lanman_auth() &&
2115 E_deshash(newpswd, new_lm_hash) &&
2116 E_deshash(oldpswd, old_lm_hash)) {
2117 new_lm_pswd = data_blob(NULL, 516);
2118 old_lm_hash_enc = data_blob(NULL, 16);
2119 encode_pw_buffer(new_lm_pswd.data, newpswd,
2122 arcfour_crypt(new_lm_pswd.data, old_nt_hash, 516);
2123 E_old_pw_hash(new_nt_hash, old_lm_hash,
2124 old_lm_hash_enc.data);
2126 new_lm_pswd.data = NULL;
2127 new_lm_pswd.length = 0;
2128 old_lm_hash_enc.data = NULL;
2129 old_lm_hash_enc.length = 0;
2132 encode_pw_buffer(new_nt_pswd.data, newpswd,
2135 arcfour_crypt(new_nt_pswd.data, old_nt_hash, 516);
2136 E_old_pw_hash(new_nt_hash, old_nt_hash,
2137 old_nt_hash_enc.data);
2140 if (!full_username && !username) {
2141 x_fprintf(x_stdout, "Error: No username supplied!\n");
2142 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2143 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2144 x_fprintf(x_stdout, "Error: No NT or LM password "
2145 "blobs supplied!\n");
2147 char *error_string = NULL;
2149 if (full_username && !username) {
2151 fstring fstr_domain;
2153 if (!parse_ntlm_auth_domain_user(full_username,
2156 /* username might be 'tainted', don't
2157 * print into our new-line
2158 * deleimianted stream */
2159 x_fprintf(x_stdout, "Error: Could not "
2160 "parse into domain and "
2162 SAFE_FREE(username);
2163 username = smb_xstrdup(full_username);
2165 SAFE_FREE(username);
2167 username = smb_xstrdup(fstr_user);
2168 domain = smb_xstrdup(fstr_domain);
2173 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2180 x_fprintf(x_stdout, "Password-Change: No\n");
2181 x_fprintf(x_stdout, "Password-Change-Error: "
2182 "%s\n.\n", error_string);
2184 x_fprintf(x_stdout, "Password-Change: Yes\n");
2187 SAFE_FREE(error_string);
2189 /* clear out the state */
2190 new_nt_pswd = data_blob_null;
2191 old_nt_hash_enc = data_blob_null;
2192 new_lm_pswd = data_blob_null;
2193 old_nt_hash_enc = data_blob_null;
2194 SAFE_FREE(full_username);
2195 SAFE_FREE(username);
2199 x_fprintf(x_stdout, ".\n");
2206 /* Indicates a base64 encoded structure */
2207 parameter = strstr_m(request, ":: ");
2209 parameter = strstr_m(request, ": ");
2212 DEBUG(0, ("Parameter not found!\n"));
2213 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2229 base64_decode_inplace(parameter);
2232 if (strequal(request, "new-nt-password-blob")) {
2233 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2234 if (new_nt_pswd.length != 516) {
2235 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2236 "(got %d bytes, expected 516)\n.\n",
2238 (int)new_nt_pswd.length);
2239 new_nt_pswd = data_blob_null;
2241 } else if (strequal(request, "old-nt-hash-blob")) {
2242 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2243 if (old_nt_hash_enc.length != 16) {
2244 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2245 "(got %d bytes, expected 16)\n.\n",
2247 (int)old_nt_hash_enc.length);
2248 old_nt_hash_enc = data_blob_null;
2250 } else if (strequal(request, "new-lm-password-blob")) {
2251 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2252 if (new_lm_pswd.length != 516) {
2253 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2254 "(got %d bytes, expected 516)\n.\n",
2256 (int)new_lm_pswd.length);
2257 new_lm_pswd = data_blob_null;
2260 else if (strequal(request, "old-lm-hash-blob")) {
2261 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2262 if (old_lm_hash_enc.length != 16)
2264 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2265 "(got %d bytes, expected 16)\n.\n",
2267 (int)old_lm_hash_enc.length);
2268 old_lm_hash_enc = data_blob_null;
2270 } else if (strequal(request, "nt-domain")) {
2271 domain = smb_xstrdup(parameter);
2272 } else if(strequal(request, "username")) {
2273 username = smb_xstrdup(parameter);
2274 } else if(strequal(request, "full-username")) {
2275 username = smb_xstrdup(parameter);
2276 } else if(strequal(request, "new-password")) {
2277 newpswd = smb_xstrdup(parameter);
2278 } else if (strequal(request, "old-password")) {
2279 oldpswd = smb_xstrdup(parameter);
2281 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2285 static void manage_squid_request(struct ntlm_auth_state *state,
2286 stdio_helper_function fn)
2289 char tmp[INITIAL_BUFFER_SIZE+1];
2290 int length, buf_size = 0;
2293 buf = talloc_strdup(state->mem_ctx, "");
2295 DEBUG(0, ("Failed to allocate input buffer.\n"));
2296 x_fprintf(x_stderr, "ERR\n");
2302 /* this is not a typo - x_fgets doesn't work too well under
2304 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2305 if (ferror(stdin)) {
2306 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2307 "(%s)\n", ferror(stdin),
2308 strerror(ferror(stdin))));
2315 buf = talloc_strdup_append_buffer(buf, tmp);
2316 buf_size += INITIAL_BUFFER_SIZE;
2318 if (buf_size > MAX_BUFFER_SIZE) {
2319 DEBUG(2, ("Oversized message\n"));
2320 x_fprintf(x_stderr, "ERR\n");
2325 c = strchr(buf, '\n');
2326 } while (c == NULL);
2331 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2333 if (buf[0] == '\0') {
2334 DEBUG(2, ("Invalid Request\n"));
2335 x_fprintf(x_stderr, "ERR\n");
2340 fn(state, buf, length);
2345 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2346 TALLOC_CTX *mem_ctx;
2347 struct ntlm_auth_state *state;
2349 /* initialize FDescs */
2350 x_setbuf(x_stdout, NULL);
2351 x_setbuf(x_stderr, NULL);
2353 mem_ctx = talloc_init("ntlm_auth");
2355 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2356 x_fprintf(x_stderr, "ERR\n");
2360 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2362 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2363 x_fprintf(x_stderr, "ERR\n");
2367 state->mem_ctx = mem_ctx;
2368 state->helper_mode = stdio_mode;
2371 TALLOC_CTX *frame = talloc_stackframe();
2372 manage_squid_request(state, fn);
2378 /* Authenticate a user with a challenge/response */
2380 static bool check_auth_crap(void)
2385 char user_session_key[16];
2387 char *hex_user_session_key;
2389 static uint8 zeros[16];
2391 x_setbuf(x_stdout, NULL);
2394 flags |= WBFLAG_PAM_LMKEY;
2396 if (request_user_session_key)
2397 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2399 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2401 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2407 (unsigned char *)lm_key,
2408 (unsigned char *)user_session_key,
2409 &error_string, NULL);
2411 if (!NT_STATUS_IS_OK(nt_status)) {
2412 x_fprintf(x_stdout, "%s (0x%x)\n",
2414 NT_STATUS_V(nt_status));
2415 SAFE_FREE(error_string);
2420 && (memcmp(zeros, lm_key,
2421 sizeof(lm_key)) != 0)) {
2422 hex_lm_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key,
2424 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2425 TALLOC_FREE(hex_lm_key);
2427 if (request_user_session_key
2428 && (memcmp(zeros, user_session_key,
2429 sizeof(user_session_key)) != 0)) {
2430 hex_user_session_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key,
2431 sizeof(user_session_key));
2432 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2433 TALLOC_FREE(hex_user_session_key);
2442 OPT_USERNAME = 1000,
2451 OPT_USER_SESSION_KEY,
2453 OPT_REQUIRE_MEMBERSHIP,
2454 OPT_USE_CACHED_CREDS,
2455 OPT_PAM_WINBIND_CONF
2458 int main(int argc, const char **argv)
2460 TALLOC_CTX *frame = talloc_stackframe();
2462 static const char *helper_protocol;
2463 static int diagnostics;
2465 static const char *hex_challenge;
2466 static const char *hex_lm_response;
2467 static const char *hex_nt_response;
2471 /* NOTE: DO NOT change this interface without considering the implications!
2472 This is an external interface, which other programs will use to interact
2476 /* We do not use single-letter command abbreviations, because they harm future
2477 interface stability. */
2479 struct poptOption long_options[] = {
2481 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2482 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2483 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2484 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2485 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2486 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2487 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2488 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2489 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2490 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2491 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2492 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics,
2494 "Perform diagnostics on the authentication chain"},
2495 { "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" },
2496 { "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" },
2497 POPT_COMMON_CONFIGFILE
2502 /* Samba client initialisation */
2509 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2511 /* Parse command line options */
2514 poptPrintHelp(pc, stderr, 0);
2518 while((opt = poptGetNextOpt(pc)) != -1) {
2519 /* Get generic config options like --configfile */
2522 poptFreeContext(pc);
2524 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2525 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2526 get_dyn_CONFIGFILE(), strerror(errno));
2530 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2531 POPT_CONTEXT_KEEP_FIRST);
2533 while((opt = poptGetNextOpt(pc)) != -1) {
2536 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2537 if (opt_challenge.length != 8) {
2538 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2540 (int)opt_challenge.length);
2545 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2546 if (opt_lm_response.length != 24) {
2547 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2549 (int)opt_lm_response.length);
2555 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2556 if (opt_nt_response.length < 24) {
2557 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2559 (int)opt_nt_response.length);
2564 case OPT_REQUIRE_MEMBERSHIP:
2565 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2566 require_membership_of_sid = require_membership_of;
2573 char *domain = SMB_STRDUP(opt_username);
2574 char *p = strchr_m(domain, *lp_winbind_separator());
2578 if (opt_domain && !strequal(opt_domain, domain)) {
2579 x_fprintf(x_stderr, "Domain specified in username (%s) "
2580 "doesn't match specified domain (%s)!\n\n",
2581 domain, opt_domain);
2582 poptPrintHelp(pc, stderr, 0);
2585 opt_domain = domain;
2591 /* Note: if opt_domain is "" then send no domain */
2592 if (opt_domain == NULL) {
2593 opt_domain = get_winbind_domain();
2596 if (opt_workstation == NULL) {
2597 opt_workstation = "";
2600 if (helper_protocol) {
2602 for (i=0; i<NUM_HELPER_MODES; i++) {
2603 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2604 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2608 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2610 for (i=0; i<NUM_HELPER_MODES; i++) {
2611 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2617 if (!opt_username || !*opt_username) {
2618 x_fprintf(x_stderr, "username must be specified!\n\n");
2619 poptPrintHelp(pc, stderr, 0);
2623 if (opt_challenge.length) {
2624 if (!check_auth_crap()) {
2630 if (!opt_password) {
2631 opt_password = getpass("password: ");
2635 if (!diagnose_ntlm_auth()) {
2641 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2642 if (!check_plaintext_auth(user, opt_password, True)) {
2649 poptFreeContext(pc);