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 "../auth/ntlmssp/ntlmssp.h"
33 #include <iniparser.h>
34 #include "../lib/crypto/arcfour.h"
35 #include "libads/kerberos_proto.h"
36 #include "nsswitch/winbind_client.h"
37 #include "librpc/gen_ndr/krb5pac.h"
38 #include "../lib/util/asn1.h"
40 #ifndef PAM_WINBIND_CONFIG_FILE
41 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
44 #define WINBIND_KRB5_AUTH 0x00000080
47 #define DBGC_CLASS DBGC_WINBIND
49 #define INITIAL_BUFFER_SIZE 300
50 #define MAX_BUFFER_SIZE 630000
52 enum stdio_helper_mode
{
60 NTLM_CHANGE_PASSWORD_1
,
64 enum ntlm_auth_cli_state
{
71 enum ntlm_auth_svr_state
{
78 struct ntlm_auth_state
{
80 enum stdio_helper_mode helper_mode
;
81 enum ntlm_auth_cli_state cli_state
;
82 enum ntlm_auth_svr_state svr_state
;
83 struct ntlmssp_state
*ntlmssp_state
;
85 char *want_feature_list
;
87 char *spnego_mech_oid
;
88 bool have_session_key
;
89 DATA_BLOB session_key
;
90 DATA_BLOB initial_message
;
93 typedef void (*stdio_helper_function
)(struct ntlm_auth_state
*state
, char *buf
,
96 static void manage_squid_basic_request (struct ntlm_auth_state
*state
,
97 char *buf
, int length
);
99 static void manage_squid_ntlmssp_request (struct ntlm_auth_state
*state
,
100 char *buf
, int length
);
102 static void manage_client_ntlmssp_request (struct ntlm_auth_state
*state
,
103 char *buf
, int length
);
105 static void manage_gss_spnego_request (struct ntlm_auth_state
*state
,
106 char *buf
, int length
);
108 static void manage_gss_spnego_client_request (struct ntlm_auth_state
*state
,
109 char *buf
, int length
);
111 static void manage_ntlm_server_1_request (struct ntlm_auth_state
*state
,
112 char *buf
, int length
);
114 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
115 char *buf
, int length
);
117 static const struct {
118 enum stdio_helper_mode mode
;
120 stdio_helper_function fn
;
121 } stdio_helper_protocols
[] = {
122 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
123 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
124 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request
},
125 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_client_ntlmssp_request
},
126 { GSS_SPNEGO
, "gss-spnego", manage_gss_spnego_request
},
127 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gss_spnego_client_request
},
128 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
129 { NTLM_CHANGE_PASSWORD_1
, "ntlm-change-password-1", manage_ntlm_change_password_1_request
},
130 { NUM_HELPER_MODES
, NULL
, NULL
}
133 const char *opt_username
;
134 const char *opt_domain
;
135 const char *opt_workstation
;
136 const char *opt_password
;
137 static DATA_BLOB opt_challenge
;
138 static DATA_BLOB opt_lm_response
;
139 static DATA_BLOB opt_nt_response
;
140 static int request_lm_key
;
141 static int request_user_session_key
;
142 static int use_cached_creds
;
144 static const char *require_membership_of
;
145 static const char *require_membership_of_sid
;
146 static const char *opt_pam_winbind_conf
;
148 static char winbind_separator(void)
150 struct winbindd_response response
;
157 ZERO_STRUCT(response
);
159 /* Send off request */
161 if (winbindd_request_response(WINBINDD_INFO
, NULL
, &response
) !=
162 NSS_STATUS_SUCCESS
) {
163 d_printf("could not obtain winbind separator!\n");
164 return *lp_winbind_separator();
167 sep
= response
.data
.info
.winbind_separator
;
171 d_printf("winbind separator was NULL!\n");
172 return *lp_winbind_separator();
178 const char *get_winbind_domain(void)
180 struct winbindd_response response
;
182 static fstring winbind_domain
;
183 if (*winbind_domain
) {
184 return winbind_domain
;
187 ZERO_STRUCT(response
);
189 /* Send off request */
191 if (winbindd_request_response(WINBINDD_DOMAIN_NAME
, NULL
, &response
) !=
192 NSS_STATUS_SUCCESS
) {
193 DEBUG(0, ("could not obtain winbind domain name!\n"));
194 return lp_workgroup();
197 fstrcpy(winbind_domain
, response
.data
.domain_name
);
199 return winbind_domain
;
203 const char *get_winbind_netbios_name(void)
205 struct winbindd_response response
;
207 static fstring winbind_netbios_name
;
209 if (*winbind_netbios_name
) {
210 return winbind_netbios_name
;
213 ZERO_STRUCT(response
);
215 /* Send off request */
217 if (winbindd_request_response(WINBINDD_NETBIOS_NAME
, NULL
, &response
) !=
218 NSS_STATUS_SUCCESS
) {
219 DEBUG(0, ("could not obtain winbind netbios name!\n"));
220 return lp_netbios_name();
223 fstrcpy(winbind_netbios_name
, response
.data
.netbios_name
);
225 return winbind_netbios_name
;
229 DATA_BLOB
get_challenge(void)
231 static DATA_BLOB chal
;
232 if (opt_challenge
.length
)
233 return opt_challenge
;
235 chal
= data_blob(NULL
, 8);
237 generate_random_buffer(chal
.data
, chal
.length
);
241 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
242 form DOMAIN/user into a domain and a user */
244 static bool parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
248 char *p
= strchr(domuser
,winbind_separator());
255 fstrcpy(domain
, domuser
);
256 domain
[PTR_DIFF(p
, domuser
)] = 0;
262 static bool get_require_membership_sid(void) {
263 struct winbindd_request request
;
264 struct winbindd_response response
;
266 if (!require_membership_of
) {
270 if (require_membership_of_sid
) {
274 /* Otherwise, ask winbindd for the name->sid request */
276 ZERO_STRUCT(request
);
277 ZERO_STRUCT(response
);
279 if (!parse_ntlm_auth_domain_user(require_membership_of
,
280 request
.data
.name
.dom_name
,
281 request
.data
.name
.name
)) {
282 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
283 require_membership_of
));
287 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
288 NSS_STATUS_SUCCESS
) {
289 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
290 require_membership_of
));
294 require_membership_of_sid
= SMB_STRDUP(response
.data
.sid
.sid
);
296 if (require_membership_of_sid
)
303 * Get some configuration from pam_winbind.conf to see if we
304 * need to contact trusted domain
307 int get_pam_winbind_config()
310 dictionary
*d
= NULL
;
312 if (!opt_pam_winbind_conf
|| !*opt_pam_winbind_conf
) {
313 opt_pam_winbind_conf
= PAM_WINBIND_CONFIG_FILE
;
316 d
= iniparser_load(discard_const_p(char, opt_pam_winbind_conf
));
322 if (iniparser_getboolean(d
, discard_const_p(char, "global:krb5_auth"), false)) {
323 ctrl
|= WINBIND_KRB5_AUTH
;
326 iniparser_freedict(d
);
331 /* Authenticate a user with a plaintext password */
333 static bool check_plaintext_auth(const char *user
, const char *pass
,
334 bool stdout_diagnostics
)
336 struct winbindd_request request
;
337 struct winbindd_response response
;
340 if (!get_require_membership_sid()) {
344 /* Send off request */
346 ZERO_STRUCT(request
);
347 ZERO_STRUCT(response
);
349 fstrcpy(request
.data
.auth
.user
, user
);
350 fstrcpy(request
.data
.auth
.pass
, pass
);
351 if (require_membership_of_sid
) {
352 strlcpy(request
.data
.auth
.require_membership_of_sid
,
353 require_membership_of_sid
,
354 sizeof(request
.data
.auth
.require_membership_of_sid
));
357 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
359 /* Display response */
361 if (stdout_diagnostics
) {
362 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
363 d_printf("Reading winbind reply failed! (0x01)\n");
366 d_printf("%s: %s (0x%x)\n",
367 response
.data
.auth
.nt_status_string
,
368 response
.data
.auth
.error_string
,
369 response
.data
.auth
.nt_status
);
371 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
372 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
375 DEBUG(3, ("%s: %s (0x%x)\n",
376 response
.data
.auth
.nt_status_string
,
377 response
.data
.auth
.error_string
,
378 response
.data
.auth
.nt_status
));
381 return (result
== NSS_STATUS_SUCCESS
);
384 /* authenticate a user with an encrypted username/password */
386 NTSTATUS
contact_winbind_auth_crap(const char *username
,
388 const char *workstation
,
389 const DATA_BLOB
*challenge
,
390 const DATA_BLOB
*lm_response
,
391 const DATA_BLOB
*nt_response
,
394 uint8 user_session_key
[16],
400 struct winbindd_request request
;
401 struct winbindd_response response
;
403 if (!get_require_membership_sid()) {
404 return NT_STATUS_INVALID_PARAMETER
;
407 ZERO_STRUCT(request
);
408 ZERO_STRUCT(response
);
410 request
.flags
= flags
;
412 request
.data
.auth_crap
.logon_parameters
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
| MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
414 if (require_membership_of_sid
)
415 fstrcpy(request
.data
.auth_crap
.require_membership_of_sid
, require_membership_of_sid
);
417 fstrcpy(request
.data
.auth_crap
.user
, username
);
418 fstrcpy(request
.data
.auth_crap
.domain
, domain
);
420 fstrcpy(request
.data
.auth_crap
.workstation
,
423 memcpy(request
.data
.auth_crap
.chal
, challenge
->data
, MIN(challenge
->length
, 8));
425 if (lm_response
&& lm_response
->length
) {
426 memcpy(request
.data
.auth_crap
.lm_resp
,
428 MIN(lm_response
->length
, sizeof(request
.data
.auth_crap
.lm_resp
)));
429 request
.data
.auth_crap
.lm_resp_len
= lm_response
->length
;
432 if (nt_response
&& nt_response
->length
) {
433 if (nt_response
->length
> sizeof(request
.data
.auth_crap
.nt_resp
)) {
434 request
.flags
= request
.flags
| WBFLAG_BIG_NTLMV2_BLOB
;
435 request
.extra_len
= nt_response
->length
;
436 request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, request
.extra_len
);
437 if (request
.extra_data
.data
== NULL
) {
438 return NT_STATUS_NO_MEMORY
;
440 memcpy(request
.extra_data
.data
, nt_response
->data
,
441 nt_response
->length
);
444 memcpy(request
.data
.auth_crap
.nt_resp
,
445 nt_response
->data
, nt_response
->length
);
447 request
.data
.auth_crap
.nt_resp_len
= nt_response
->length
;
450 result
= winbindd_request_response(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
451 SAFE_FREE(request
.extra_data
.data
);
453 /* Display response */
455 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
456 nt_status
= NT_STATUS_UNSUCCESSFUL
;
458 *error_string
= smb_xstrdup("Reading winbind reply failed!");
459 winbindd_free_response(&response
);
463 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
464 if (!NT_STATUS_IS_OK(nt_status
)) {
466 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
467 winbindd_free_response(&response
);
471 if ((flags
& WBFLAG_PAM_LMKEY
) && lm_key
) {
472 memcpy(lm_key
, response
.data
.auth
.first_8_lm_hash
,
473 sizeof(response
.data
.auth
.first_8_lm_hash
));
475 if ((flags
& WBFLAG_PAM_USER_SESSION_KEY
) && user_session_key
) {
476 memcpy(user_session_key
, response
.data
.auth
.user_session_key
,
477 sizeof(response
.data
.auth
.user_session_key
));
480 if (flags
& WBFLAG_PAM_UNIX_NAME
) {
481 *unix_name
= SMB_STRDUP(response
.data
.auth
.unix_username
);
483 winbindd_free_response(&response
);
484 return NT_STATUS_NO_MEMORY
;
488 winbindd_free_response(&response
);
492 /* contact server to change user password using auth crap */
493 static NTSTATUS
contact_winbind_change_pswd_auth_crap(const char *username
,
495 const DATA_BLOB new_nt_pswd
,
496 const DATA_BLOB old_nt_hash_enc
,
497 const DATA_BLOB new_lm_pswd
,
498 const DATA_BLOB old_lm_hash_enc
,
503 struct winbindd_request request
;
504 struct winbindd_response response
;
506 if (!get_require_membership_sid())
509 *error_string
= smb_xstrdup("Can't get membership sid.");
510 return NT_STATUS_INVALID_PARAMETER
;
513 ZERO_STRUCT(request
);
514 ZERO_STRUCT(response
);
517 fstrcpy(request
.data
.chng_pswd_auth_crap
.user
, username
);
519 fstrcpy(request
.data
.chng_pswd_auth_crap
.domain
,domain
);
521 if(new_nt_pswd
.length
)
523 memcpy(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
, new_nt_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
));
524 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
= new_nt_pswd
.length
;
527 if(old_nt_hash_enc
.length
)
529 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
));
530 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
= old_nt_hash_enc
.length
;
533 if(new_lm_pswd
.length
)
535 memcpy(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
, new_lm_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
));
536 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
= new_lm_pswd
.length
;
539 if(old_lm_hash_enc
.length
)
541 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
));
542 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
= old_lm_hash_enc
.length
;
545 result
= winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, &request
, &response
);
547 /* Display response */
549 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0))
551 nt_status
= NT_STATUS_UNSUCCESSFUL
;
553 *error_string
= smb_xstrdup("Reading winbind reply failed!");
554 winbindd_free_response(&response
);
558 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
559 if (!NT_STATUS_IS_OK(nt_status
))
562 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
563 winbindd_free_response(&response
);
567 winbindd_free_response(&response
);
572 static NTSTATUS
winbind_pw_check(struct ntlmssp_state
*ntlmssp_state
, TALLOC_CTX
*mem_ctx
,
573 DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
575 static const char zeros
[16] = { 0, };
577 char *error_string
= NULL
;
579 uint8 user_sess_key
[16];
580 char *unix_name
= NULL
;
582 nt_status
= contact_winbind_auth_crap(ntlmssp_state
->user
, ntlmssp_state
->domain
,
583 ntlmssp_state
->client
.netbios_name
,
584 &ntlmssp_state
->chal
,
585 &ntlmssp_state
->lm_resp
,
586 &ntlmssp_state
->nt_resp
,
587 WBFLAG_PAM_LMKEY
| WBFLAG_PAM_USER_SESSION_KEY
| WBFLAG_PAM_UNIX_NAME
,
588 lm_key
, user_sess_key
,
589 &error_string
, &unix_name
);
591 if (NT_STATUS_IS_OK(nt_status
)) {
592 if (memcmp(lm_key
, zeros
, 8) != 0) {
593 *lm_session_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
594 memcpy(lm_session_key
->data
, lm_key
, 8);
595 memset(lm_session_key
->data
+8, '\0', 8);
598 if (memcmp(user_sess_key
, zeros
, 16) != 0) {
599 *user_session_key
= data_blob_talloc(mem_ctx
, user_sess_key
, 16);
601 ntlmssp_state
->callback_private
= talloc_strdup(ntlmssp_state
,
604 DEBUG(NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
) ? 0 : 3,
605 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
606 ntlmssp_state
->domain
, ntlmssp_state
->user
,
607 ntlmssp_state
->client
.netbios_name
,
608 error_string
? error_string
: "unknown error (NULL)"));
609 ntlmssp_state
->callback_private
= NULL
;
612 SAFE_FREE(error_string
);
613 SAFE_FREE(unix_name
);
617 static NTSTATUS
local_pw_check(struct ntlmssp_state
*ntlmssp_state
, TALLOC_CTX
*mem_ctx
,
618 DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
621 struct samr_Password lm_pw
, nt_pw
;
623 nt_lm_owf_gen (opt_password
, nt_pw
.hash
, lm_pw
.hash
);
625 nt_status
= ntlm_password_check(mem_ctx
,
627 &ntlmssp_state
->chal
,
628 &ntlmssp_state
->lm_resp
,
629 &ntlmssp_state
->nt_resp
,
632 ntlmssp_state
->domain
,
633 &lm_pw
, &nt_pw
, user_session_key
, lm_session_key
);
635 if (NT_STATUS_IS_OK(nt_status
)) {
636 ntlmssp_state
->callback_private
= talloc_asprintf(ntlmssp_state
,
637 "%s%c%s", ntlmssp_state
->domain
,
638 *lp_winbind_separator(),
639 ntlmssp_state
->user
);
641 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
642 ntlmssp_state
->domain
, ntlmssp_state
->user
,
643 ntlmssp_state
->client
.netbios_name
,
644 nt_errstr(nt_status
)));
645 ntlmssp_state
->callback_private
= NULL
;
650 static NTSTATUS
ntlm_auth_start_ntlmssp_client(struct ntlmssp_state
**client_ntlmssp_state
)
653 if ( (opt_username
== NULL
) || (opt_domain
== NULL
) ) {
654 status
= NT_STATUS_UNSUCCESSFUL
;
655 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
656 return NT_STATUS_INVALID_PARAMETER
;
659 status
= ntlmssp_client_start(NULL
,
662 lp_client_ntlmv2_auth(),
663 client_ntlmssp_state
);
665 if (!NT_STATUS_IS_OK(status
)) {
666 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
668 TALLOC_FREE(*client_ntlmssp_state
);
672 status
= ntlmssp_set_username(*client_ntlmssp_state
, opt_username
);
674 if (!NT_STATUS_IS_OK(status
)) {
675 DEBUG(1, ("Could not set username: %s\n",
677 TALLOC_FREE(*client_ntlmssp_state
);
681 status
= ntlmssp_set_domain(*client_ntlmssp_state
, opt_domain
);
683 if (!NT_STATUS_IS_OK(status
)) {
684 DEBUG(1, ("Could not set domain: %s\n",
686 TALLOC_FREE(*client_ntlmssp_state
);
691 status
= ntlmssp_set_password(*client_ntlmssp_state
, opt_password
);
693 if (!NT_STATUS_IS_OK(status
)) {
694 DEBUG(1, ("Could not set password: %s\n",
696 TALLOC_FREE(*client_ntlmssp_state
);
704 static NTSTATUS
ntlm_auth_start_ntlmssp_server(struct ntlmssp_state
**ntlmssp_state
)
707 const char *netbios_name
;
708 const char *netbios_domain
;
709 const char *dns_name
;
711 bool is_standalone
= false;
714 netbios_name
= lp_netbios_name();
715 netbios_domain
= lp_workgroup();
717 netbios_name
= get_winbind_netbios_name();
718 netbios_domain
= get_winbind_domain();
720 /* This should be a 'netbios domain -> DNS domain' mapping */
721 dns_domain
= get_mydnsdomname(talloc_tos());
723 strlower_m(dns_domain
);
725 dns_name
= get_mydnsfullname();
727 status
= ntlmssp_server_start(NULL
,
734 if (!NT_STATUS_IS_OK(status
)) {
735 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
740 (*ntlmssp_state
)->neg_flags
|=
741 (NTLMSSP_NEGOTIATE_SIGN
|
742 NTLMSSP_NEGOTIATE_SEAL
);
744 /* Have we been given a local password, or should we ask winbind? */
746 (*ntlmssp_state
)->check_password
= local_pw_check
;
748 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
753 /*******************************************************************
754 Used by firefox to drive NTLM auth to IIS servers.
755 *******************************************************************/
757 static NTSTATUS
do_ccache_ntlm_auth(DATA_BLOB initial_msg
, DATA_BLOB challenge_msg
,
760 struct winbindd_request wb_request
;
761 struct winbindd_response wb_response
;
765 /* get winbindd to do the ntlmssp step on our behalf */
766 ZERO_STRUCT(wb_request
);
767 ZERO_STRUCT(wb_response
);
770 * This is tricky here. If we set krb5_auth in pam_winbind.conf
771 * creds for users in trusted domain will be stored the winbindd
772 * child of the trusted domain. If we ask the primary domain for
773 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
774 * domain's child for ccache_ntlm_auth. that is to say, we have to
775 * set WBFLAG_PAM_CONTACT_TRUSTDOM in request.flags.
777 ctrl
= get_pam_winbind_config();
779 if (ctrl
& WINBIND_KRB5_AUTH
) {
780 wb_request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
783 fstr_sprintf(wb_request
.data
.ccache_ntlm_auth
.user
,
784 "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
785 wb_request
.data
.ccache_ntlm_auth
.uid
= geteuid();
786 wb_request
.data
.ccache_ntlm_auth
.initial_blob_len
= initial_msg
.length
;
787 wb_request
.data
.ccache_ntlm_auth
.challenge_blob_len
= challenge_msg
.length
;
788 wb_request
.extra_len
= initial_msg
.length
+ challenge_msg
.length
;
790 if (wb_request
.extra_len
> 0) {
791 wb_request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, wb_request
.extra_len
);
792 if (wb_request
.extra_data
.data
== NULL
) {
793 return NT_STATUS_NO_MEMORY
;
796 memcpy(wb_request
.extra_data
.data
, initial_msg
.data
, initial_msg
.length
);
797 memcpy(wb_request
.extra_data
.data
+ initial_msg
.length
,
798 challenge_msg
.data
, challenge_msg
.length
);
801 result
= winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH
, &wb_request
, &wb_response
);
802 SAFE_FREE(wb_request
.extra_data
.data
);
804 if (result
!= NSS_STATUS_SUCCESS
) {
805 winbindd_free_response(&wb_response
);
806 return NT_STATUS_UNSUCCESSFUL
;
810 *reply
= data_blob(wb_response
.extra_data
.data
,
811 wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
);
812 if (wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
> 0 &&
813 reply
->data
== NULL
) {
814 winbindd_free_response(&wb_response
);
815 return NT_STATUS_NO_MEMORY
;
819 winbindd_free_response(&wb_response
);
820 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
823 static void manage_squid_ntlmssp_request_int(struct ntlm_auth_state
*state
,
824 char *buf
, int length
,
828 DATA_BLOB request
, reply
;
831 if (strlen(buf
) < 2) {
832 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
833 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
837 if (strlen(buf
) > 3) {
838 if(strncmp(buf
, "SF ", 3) == 0){
839 DEBUG(10, ("Setting flags to negotioate\n"));
840 TALLOC_FREE(state
->want_feature_list
);
841 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
843 *response
= talloc_strdup(mem_ctx
, "OK");
846 request
= base64_decode_data_blob(buf
+ 3);
848 request
= data_blob_null
;
851 if ((strncmp(buf
, "PW ", 3) == 0)) {
852 /* The calling application wants us to use a local password
853 * (rather than winbindd) */
855 opt_password
= SMB_STRNDUP((const char *)request
.data
,
858 if (opt_password
== NULL
) {
859 DEBUG(1, ("Out of memory\n"));
860 *response
= talloc_strdup(mem_ctx
, "BH Out of memory");
861 data_blob_free(&request
);
865 *response
= talloc_strdup(mem_ctx
, "OK");
866 data_blob_free(&request
);
870 if (strncmp(buf
, "YR", 2) == 0) {
871 TALLOC_FREE(state
->ntlmssp_state
);
872 state
->svr_state
= SERVER_INITIAL
;
873 } else if (strncmp(buf
, "KK", 2) == 0) {
874 /* No special preprocessing required */
875 } else if (strncmp(buf
, "GF", 2) == 0) {
876 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
878 if (state
->svr_state
== SERVER_FINISHED
) {
879 *response
= talloc_asprintf(mem_ctx
, "GF 0x%08x",
883 *response
= talloc_strdup(mem_ctx
, "BH\n");
885 data_blob_free(&request
);
887 } else if (strncmp(buf
, "GK", 2) == 0) {
888 DEBUG(10, ("Requested NTLMSSP session key\n"));
889 if(state
->have_session_key
) {
890 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
892 *response
= talloc_asprintf(mem_ctx
, "GK %s",
893 key64
? key64
: "<NULL>");
896 *response
= talloc_strdup(mem_ctx
, "BH");
899 data_blob_free(&request
);
902 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
903 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
907 if (!state
->ntlmssp_state
) {
908 nt_status
= ntlm_auth_start_ntlmssp_server(
909 &state
->ntlmssp_state
);
910 if (!NT_STATUS_IS_OK(nt_status
)) {
911 *response
= talloc_asprintf(
912 mem_ctx
, "BH %s", nt_errstr(nt_status
));
915 ntlmssp_want_feature_list(state
->ntlmssp_state
,
916 state
->want_feature_list
);
919 DEBUG(10, ("got NTLMSSP packet:\n"));
920 dump_data(10, request
.data
, request
.length
);
922 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
, &reply
);
924 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
925 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
927 *response
= talloc_asprintf(mem_ctx
, "TT %s", reply_base64
);
928 TALLOC_FREE(reply_base64
);
929 data_blob_free(&reply
);
930 state
->svr_state
= SERVER_CHALLENGE
;
931 DEBUG(10, ("NTLMSSP challenge\n"));
932 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
933 *response
= talloc_asprintf(mem_ctx
, "BH %s",
934 nt_errstr(nt_status
));
935 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
937 TALLOC_FREE(state
->ntlmssp_state
);
938 } else if (!NT_STATUS_IS_OK(nt_status
)) {
939 *response
= talloc_asprintf(mem_ctx
, "NA %s",
940 nt_errstr(nt_status
));
941 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
943 *response
= talloc_asprintf(
945 (char *)state
->ntlmssp_state
->callback_private
);
946 DEBUG(10, ("NTLMSSP OK!\n"));
948 if(state
->have_session_key
)
949 data_blob_free(&state
->session_key
);
950 state
->session_key
= data_blob(
951 state
->ntlmssp_state
->session_key
.data
,
952 state
->ntlmssp_state
->session_key
.length
);
953 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
954 state
->have_session_key
= true;
955 state
->svr_state
= SERVER_FINISHED
;
958 data_blob_free(&request
);
961 static void manage_squid_ntlmssp_request(struct ntlm_auth_state
*state
,
962 char *buf
, int length
)
966 manage_squid_ntlmssp_request_int(state
, buf
, length
,
967 talloc_tos(), &response
);
969 if (response
== NULL
) {
970 x_fprintf(x_stdout
, "BH Out of memory\n");
973 x_fprintf(x_stdout
, "%s\n", response
);
974 TALLOC_FREE(response
);
977 static void manage_client_ntlmssp_request(struct ntlm_auth_state
*state
,
978 char *buf
, int length
)
980 DATA_BLOB request
, reply
;
983 if (!opt_username
|| !*opt_username
) {
984 x_fprintf(x_stderr
, "username must be specified!\n\n");
988 if (strlen(buf
) < 2) {
989 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
990 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
994 if (strlen(buf
) > 3) {
995 if(strncmp(buf
, "SF ", 3) == 0) {
996 DEBUG(10, ("Looking for flags to negotiate\n"));
997 talloc_free(state
->want_feature_list
);
998 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
1000 x_fprintf(x_stdout
, "OK\n");
1003 request
= base64_decode_data_blob(buf
+ 3);
1005 request
= data_blob_null
;
1008 if (strncmp(buf
, "PW ", 3) == 0) {
1009 /* We asked for a password and obviously got it :-) */
1011 opt_password
= SMB_STRNDUP((const char *)request
.data
,
1014 if (opt_password
== NULL
) {
1015 DEBUG(1, ("Out of memory\n"));
1016 x_fprintf(x_stdout
, "BH Out of memory\n");
1017 data_blob_free(&request
);
1021 x_fprintf(x_stdout
, "OK\n");
1022 data_blob_free(&request
);
1026 if (!state
->ntlmssp_state
&& use_cached_creds
) {
1027 /* check whether cached credentials are usable. */
1028 DATA_BLOB empty_blob
= data_blob_null
;
1030 nt_status
= do_ccache_ntlm_auth(empty_blob
, empty_blob
, NULL
);
1031 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1032 /* failed to use cached creds */
1033 use_cached_creds
= False
;
1037 if (opt_password
== NULL
&& !use_cached_creds
) {
1038 /* Request a password from the calling process. After
1039 sending it, the calling process should retry asking for the
1042 DEBUG(10, ("Requesting password\n"));
1043 x_fprintf(x_stdout
, "PW\n");
1047 if (strncmp(buf
, "YR", 2) == 0) {
1048 TALLOC_FREE(state
->ntlmssp_state
);
1049 state
->cli_state
= CLIENT_INITIAL
;
1050 } else if (strncmp(buf
, "TT", 2) == 0) {
1051 /* No special preprocessing required */
1052 } else if (strncmp(buf
, "GF", 2) == 0) {
1053 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1055 if(state
->cli_state
== CLIENT_FINISHED
) {
1056 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
1059 x_fprintf(x_stdout
, "BH\n");
1062 data_blob_free(&request
);
1064 } else if (strncmp(buf
, "GK", 2) == 0 ) {
1065 DEBUG(10, ("Requested session key\n"));
1067 if(state
->cli_state
== CLIENT_FINISHED
) {
1068 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
1069 state
->session_key
);
1070 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
1074 x_fprintf(x_stdout
, "BH\n");
1077 data_blob_free(&request
);
1080 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
1081 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
1085 if (!state
->ntlmssp_state
) {
1086 nt_status
= ntlm_auth_start_ntlmssp_client(
1087 &state
->ntlmssp_state
);
1088 if (!NT_STATUS_IS_OK(nt_status
)) {
1089 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1092 ntlmssp_want_feature_list(state
->ntlmssp_state
,
1093 state
->want_feature_list
);
1094 state
->initial_message
= data_blob_null
;
1097 DEBUG(10, ("got NTLMSSP packet:\n"));
1098 dump_data(10, request
.data
, request
.length
);
1100 if (use_cached_creds
&& !opt_password
&&
1101 (state
->cli_state
== CLIENT_RESPONSE
)) {
1102 nt_status
= do_ccache_ntlm_auth(state
->initial_message
, request
,
1105 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
,
1109 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1110 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
1112 if (state
->cli_state
== CLIENT_INITIAL
) {
1113 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
1114 state
->initial_message
= reply
;
1115 state
->cli_state
= CLIENT_RESPONSE
;
1117 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
1118 data_blob_free(&reply
);
1120 TALLOC_FREE(reply_base64
);
1121 DEBUG(10, ("NTLMSSP challenge\n"));
1122 } else if (NT_STATUS_IS_OK(nt_status
)) {
1123 char *reply_base64
= base64_encode_data_blob(talloc_tos(),
1125 x_fprintf(x_stdout
, "AF %s\n", reply_base64
);
1126 TALLOC_FREE(reply_base64
);
1128 if(state
->have_session_key
)
1129 data_blob_free(&state
->session_key
);
1131 state
->session_key
= data_blob(
1132 state
->ntlmssp_state
->session_key
.data
,
1133 state
->ntlmssp_state
->session_key
.length
);
1134 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
1135 state
->have_session_key
= true;
1137 DEBUG(10, ("NTLMSSP OK!\n"));
1138 state
->cli_state
= CLIENT_FINISHED
;
1139 TALLOC_FREE(state
->ntlmssp_state
);
1141 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1142 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
1143 state
->cli_state
= CLIENT_ERROR
;
1144 TALLOC_FREE(state
->ntlmssp_state
);
1147 data_blob_free(&request
);
1150 static void manage_squid_basic_request(struct ntlm_auth_state
*state
,
1151 char *buf
, int length
)
1156 pass
=(char *)memchr(buf
,' ',length
);
1158 DEBUG(2, ("Password not found. Denying access\n"));
1159 x_fprintf(x_stdout
, "ERR\n");
1165 if (state
->helper_mode
== SQUID_2_5_BASIC
) {
1166 rfc1738_unescape(user
);
1167 rfc1738_unescape(pass
);
1170 if (check_plaintext_auth(user
, pass
, False
)) {
1171 x_fprintf(x_stdout
, "OK\n");
1173 x_fprintf(x_stdout
, "ERR\n");
1177 static void offer_gss_spnego_mechs(void) {
1180 struct spnego_data spnego
;
1183 TALLOC_CTX
*ctx
= talloc_tos();
1187 ZERO_STRUCT(spnego
);
1189 myname_lower
= talloc_strdup(ctx
, lp_netbios_name());
1190 if (!myname_lower
) {
1193 strlower_m(myname_lower
);
1195 principal
= talloc_asprintf(ctx
, "%s$@%s", myname_lower
, lp_realm());
1200 /* Server negTokenInit (mech offerings) */
1201 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1202 spnego
.negTokenInit
.mechTypes
= talloc_array(ctx
, const char *, 4);
1204 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_KERBEROS5_OLD
);
1205 spnego
.negTokenInit
.mechTypes
[1] = talloc_strdup(ctx
, OID_KERBEROS5
);
1206 spnego
.negTokenInit
.mechTypes
[2] = talloc_strdup(ctx
, OID_NTLMSSP
);
1207 spnego
.negTokenInit
.mechTypes
[3] = NULL
;
1209 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_NTLMSSP
);
1210 spnego
.negTokenInit
.mechTypes
[1] = NULL
;
1214 spnego
.negTokenInit
.mechListMIC
= data_blob_talloc(ctx
, principal
,
1217 len
= spnego_write_data(ctx
, &token
, &spnego
);
1218 spnego_free_data(&spnego
);
1221 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1222 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1226 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1227 x_fprintf(x_stdout
, "TT %s *\n", reply_base64
);
1229 TALLOC_FREE(reply_base64
);
1230 data_blob_free(&token
);
1231 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1235 static void manage_gss_spnego_request(struct ntlm_auth_state
*state
,
1236 char *buf
, int length
)
1238 struct spnego_data request
, response
;
1240 DATA_BLOB raw_in_token
= data_blob_null
;
1241 DATA_BLOB raw_out_token
= data_blob_null
;
1244 TALLOC_CTX
*ctx
= talloc_tos();
1247 char *domain
= NULL
;
1249 const char *reply_code
;
1251 char *reply_argument
= NULL
;
1252 char *supportedMech
= NULL
;
1254 if (strlen(buf
) < 2) {
1255 DEBUG(1, ("SPENGO query [%s] invalid\n", buf
));
1256 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1260 if (strncmp(buf
, "YR", 2) == 0) {
1261 TALLOC_FREE(state
->ntlmssp_state
);
1262 TALLOC_FREE(state
->spnego_mech
);
1263 TALLOC_FREE(state
->spnego_mech_oid
);
1264 } else if (strncmp(buf
, "KK", 2) == 0) {
1267 DEBUG(1, ("SPENGO query [%s] invalid\n", buf
));
1268 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1272 if ( (strlen(buf
) == 2)) {
1274 /* no client data, get the negTokenInit offering
1277 offer_gss_spnego_mechs();
1281 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1283 if (strlen(buf
) <= 3) {
1284 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1285 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1289 token
= base64_decode_data_blob(buf
+ 3);
1291 if ((token
.length
>= 7)
1292 && (strncmp((char *)token
.data
, "NTLMSSP", 7) == 0)) {
1295 data_blob_free(&token
);
1297 DEBUG(10, ("Could not parse GSS-SPNEGO, trying raw "
1300 manage_squid_ntlmssp_request_int(state
, buf
, length
,
1301 talloc_tos(), &reply
);
1302 if (reply
== NULL
) {
1303 x_fprintf(x_stdout
, "BH Out of memory\n");
1307 if (strncmp(reply
, "AF ", 3) == 0) {
1308 x_fprintf(x_stdout
, "AF * %s\n", reply
+3);
1310 x_fprintf(x_stdout
, "%s *\n", reply
);
1317 ZERO_STRUCT(request
);
1318 len
= spnego_read_data(ctx
, token
, &request
);
1319 data_blob_free(&token
);
1322 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1323 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1327 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1335 if (state
->spnego_mech
) {
1336 DEBUG(1, ("Client restarted SPNEGO with NegTokenInit "
1337 "while mech[%s] was already negotiated\n",
1338 state
->spnego_mech
));
1339 x_fprintf(x_stdout
, "BH Client send NegTokenInit twice\n");
1343 /* Second request from Client. This is where the
1344 client offers its mechanism to use. */
1346 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
1347 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
1348 DEBUG(1, ("Client did not offer any mechanism\n"));
1349 x_fprintf(x_stdout
, "BH Client did not offer any "
1354 status
= NT_STATUS_UNSUCCESSFUL
;
1355 for (i
= 0; request
.negTokenInit
.mechTypes
[i
] != NULL
; i
++) {
1356 DEBUG(10,("got mech[%d][%s]\n",
1357 i
, request
.negTokenInit
.mechTypes
[i
]));
1359 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5_OLD
) == 0) {
1363 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5
) == 0) {
1368 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_NTLMSSP
) == 0) {
1374 used_idx
= ntlm_idx
;
1376 if (krb5_idx
!= -1) {
1378 used_idx
= krb5_idx
;
1381 if (ntlm_idx
> -1) {
1382 state
->spnego_mech
= talloc_strdup(state
, "ntlmssp");
1383 if (state
->spnego_mech
== NULL
) {
1384 x_fprintf(x_stdout
, "BH Out of memory\n");
1388 if (state
->ntlmssp_state
) {
1389 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1390 "already got one\n"));
1391 x_fprintf(x_stdout
, "BH Client wants a new "
1392 "NTLMSSP challenge, but "
1393 "already got one\n");
1394 TALLOC_FREE(state
->ntlmssp_state
);
1398 status
= ntlm_auth_start_ntlmssp_server(&state
->ntlmssp_state
);
1399 if (!NT_STATUS_IS_OK(status
)) {
1400 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1406 if (krb5_idx
> -1) {
1407 state
->spnego_mech
= talloc_strdup(state
, "krb5");
1408 if (state
->spnego_mech
== NULL
) {
1409 x_fprintf(x_stdout
, "BH Out of memory\n");
1414 if (used_idx
> -1) {
1415 state
->spnego_mech_oid
= talloc_strdup(state
,
1416 request
.negTokenInit
.mechTypes
[used_idx
]);
1417 if (state
->spnego_mech_oid
== NULL
) {
1418 x_fprintf(x_stdout
, "BH Out of memory\n");
1421 supportedMech
= talloc_strdup(ctx
, state
->spnego_mech_oid
);
1422 if (supportedMech
== NULL
) {
1423 x_fprintf(x_stdout
, "BH Out of memory\n");
1427 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1429 status
= NT_STATUS_NOT_SUPPORTED
;
1431 if (used_idx
== 0) {
1432 status
= NT_STATUS_OK
;
1433 raw_in_token
= request
.negTokenInit
.mechToken
;
1436 if (state
->spnego_mech
== NULL
) {
1437 DEBUG(1,("Got netTokenTarg without negTokenInit\n"));
1438 x_fprintf(x_stdout
, "BH Got a negTokenTarg without "
1443 if ((request
.negTokenTarg
.supportedMech
!= NULL
) &&
1444 (strcmp(request
.negTokenTarg
.supportedMech
, state
->spnego_mech_oid
) != 0 ) ) {
1445 DEBUG(1, ("Got a negTokenTarg with mech[%s] while [%s] was already negotiated\n",
1446 request
.negTokenTarg
.supportedMech
,
1447 state
->spnego_mech_oid
));
1448 x_fprintf(x_stdout
, "BH Got a negTokenTarg with speficied mech\n");
1452 status
= NT_STATUS_OK
;
1453 raw_in_token
= request
.negTokenTarg
.responseToken
;
1456 if (!NT_STATUS_IS_OK(status
)) {
1457 /* error or more processing */
1458 } else if (strcmp(state
->spnego_mech
, "ntlmssp") == 0) {
1460 DEBUG(10, ("got NTLMSSP packet:\n"));
1461 dump_data(10, raw_in_token
.data
, raw_in_token
.length
);
1463 status
= ntlmssp_update(state
->ntlmssp_state
,
1466 if (NT_STATUS_IS_OK(status
)) {
1467 user
= talloc_strdup(ctx
, state
->ntlmssp_state
->user
);
1468 domain
= talloc_strdup(ctx
, state
->ntlmssp_state
->domain
);
1470 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1471 TALLOC_FREE(state
->ntlmssp_state
);
1474 } else if (strcmp(state
->spnego_mech
, "krb5") == 0) {
1477 DATA_BLOB session_key
;
1478 struct PAC_LOGON_INFO
*logon_info
= NULL
;
1482 if (!spnego_parse_krb5_wrap(ctx
, raw_in_token
,
1484 DEBUG(1, ("spnego_parse_krb5_wrap failed\n"));
1485 x_fprintf(x_stdout
, "BH spnego_parse_krb5_wrap failed\n");
1489 status
= ads_verify_ticket(ctx
, lp_realm(), 0,
1491 &principal
, &logon_info
, &ap_rep
,
1492 &session_key
, True
);
1494 /* Now in "principal" we have the name we are authenticated as. */
1496 if (NT_STATUS_IS_OK(status
)) {
1498 domain
= strchr_m(principal
, '@');
1500 if (domain
== NULL
) {
1501 DEBUG(1, ("Did not get a valid principal "
1502 "from ads_verify_ticket\n"));
1503 x_fprintf(x_stdout
, "BH Did not get a "
1504 "valid principal from "
1505 "ads_verify_ticket\n");
1510 domain
= talloc_strdup(ctx
, domain
);
1511 user
= talloc_strdup(ctx
, principal
);
1514 netsamlogon_cache_store(
1515 user
, &logon_info
->info3
);
1518 data_blob_free(&ap_rep
);
1519 data_blob_free(&session_key
);
1521 data_blob_free(&ticket
);
1525 spnego_free_data(&request
);
1526 ZERO_STRUCT(response
);
1527 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1529 if (NT_STATUS_IS_OK(status
)) {
1530 TALLOC_FREE(state
->spnego_mech
);
1531 TALLOC_FREE(state
->spnego_mech_oid
);
1532 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
1533 response
.negTokenTarg
.responseToken
= raw_out_token
;
1535 reply_argument
= talloc_asprintf(ctx
, "%s\\%s", domain
, user
);
1536 } else if (NT_STATUS_EQUAL(status
,
1537 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1538 response
.negTokenTarg
.supportedMech
= supportedMech
;
1539 response
.negTokenTarg
.responseToken
= raw_out_token
;
1540 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1542 reply_argument
= talloc_strdup(ctx
, "*");
1544 TALLOC_FREE(state
->spnego_mech
);
1545 TALLOC_FREE(state
->spnego_mech_oid
);
1546 data_blob_free(&raw_out_token
);
1547 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1549 reply_argument
= talloc_strdup(ctx
, nt_errstr(status
));
1552 if (!reply_argument
) {
1553 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1554 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1555 spnego_free_data(&response
);
1559 len
= spnego_write_data(ctx
, &token
, &response
);
1560 spnego_free_data(&response
);
1563 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1564 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1568 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1570 x_fprintf(x_stdout
, "%s %s %s\n",
1571 reply_code
, reply_base64
, reply_argument
);
1573 TALLOC_FREE(reply_base64
);
1574 data_blob_free(&token
);
1579 static struct ntlmssp_state
*client_ntlmssp_state
= NULL
;
1581 static bool manage_client_ntlmssp_init(struct spnego_data spnego
)
1584 DATA_BLOB null_blob
= data_blob_null
;
1585 DATA_BLOB to_server
;
1586 char *to_server_base64
;
1587 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1588 TALLOC_CTX
*ctx
= talloc_tos();
1590 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1592 if (client_ntlmssp_state
!= NULL
) {
1593 DEBUG(1, ("Request for initial SPNEGO request where "
1594 "we already have a state\n"));
1598 if (!client_ntlmssp_state
) {
1599 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1600 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1606 if (opt_password
== NULL
) {
1608 /* Request a password from the calling process. After
1609 sending it, the calling process should retry with
1610 the negTokenInit. */
1612 DEBUG(10, ("Requesting password\n"));
1613 x_fprintf(x_stdout
, "PW\n");
1617 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1618 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1619 spnego
.negTokenInit
.reqFlags
= data_blob_null
;
1620 spnego
.negTokenInit
.reqFlagsPadding
= 0;
1621 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1623 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1624 &spnego
.negTokenInit
.mechToken
);
1626 if ( !(NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) ||
1627 NT_STATUS_IS_OK(status
)) ) {
1628 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1629 nt_errstr(status
)));
1630 TALLOC_FREE(client_ntlmssp_state
);
1634 spnego_write_data(ctx
, &to_server
, &spnego
);
1635 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1637 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1638 data_blob_free(&to_server
);
1639 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1640 TALLOC_FREE(to_server_base64
);
1644 static void manage_client_ntlmssp_targ(struct spnego_data spnego
)
1647 DATA_BLOB null_blob
= data_blob_null
;
1649 DATA_BLOB to_server
;
1650 char *to_server_base64
;
1651 TALLOC_CTX
*ctx
= talloc_tos();
1653 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1655 if (client_ntlmssp_state
== NULL
) {
1656 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1657 x_fprintf(x_stdout
, "BH Got NTLMSSP tArg without a client state\n");
1661 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1662 x_fprintf(x_stdout
, "NA\n");
1663 TALLOC_FREE(client_ntlmssp_state
);
1667 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1668 x_fprintf(x_stdout
, "AF\n");
1669 TALLOC_FREE(client_ntlmssp_state
);
1673 status
= ntlmssp_update(client_ntlmssp_state
,
1674 spnego
.negTokenTarg
.responseToken
,
1677 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1678 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1679 "ntlmssp_client_update, got: %s\n",
1680 nt_errstr(status
)));
1681 x_fprintf(x_stdout
, "BH Expected MORE_PROCESSING_REQUIRED from "
1682 "ntlmssp_client_update\n");
1683 data_blob_free(&request
);
1684 TALLOC_FREE(client_ntlmssp_state
);
1688 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1689 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1690 spnego
.negTokenTarg
.supportedMech
= (const char *)OID_NTLMSSP
;
1691 spnego
.negTokenTarg
.responseToken
= request
;
1692 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1694 spnego_write_data(ctx
, &to_server
, &spnego
);
1695 data_blob_free(&request
);
1697 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1698 data_blob_free(&to_server
);
1699 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1700 TALLOC_FREE(to_server_base64
);
1706 static bool manage_client_krb5_init(struct spnego_data spnego
)
1709 DATA_BLOB tkt
, to_server
;
1710 DATA_BLOB session_key_krb5
= data_blob_null
;
1711 struct spnego_data reply
;
1715 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1717 TALLOC_CTX
*ctx
= talloc_tos();
1719 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1720 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1721 DEBUG(1, ("Did not get a principal for krb5\n"));
1725 principal
= (char *)SMB_MALLOC(
1726 spnego
.negTokenInit
.mechListMIC
.length
+1);
1728 if (principal
== NULL
) {
1729 DEBUG(1, ("Could not malloc principal\n"));
1733 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1734 spnego
.negTokenInit
.mechListMIC
.length
);
1735 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1737 retval
= cli_krb5_get_ticket(ctx
, principal
, 0,
1738 &tkt
, &session_key_krb5
,
1739 0, NULL
, NULL
, NULL
);
1743 /* Let's try to first get the TGT, for that we need a
1746 if (opt_password
== NULL
) {
1747 DEBUG(10, ("Requesting password\n"));
1748 x_fprintf(x_stdout
, "PW\n");
1752 user
= talloc_asprintf(talloc_tos(), "%s@%s", opt_username
, opt_domain
);
1757 if ((retval
= kerberos_kinit_password(user
, opt_password
, 0, NULL
))) {
1758 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval
)));
1762 retval
= cli_krb5_get_ticket(ctx
, principal
, 0,
1763 &tkt
, &session_key_krb5
,
1764 0, NULL
, NULL
, NULL
);
1766 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1771 data_blob_free(&session_key_krb5
);
1775 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1776 reply
.negTokenInit
.mechTypes
= my_mechs
;
1777 reply
.negTokenInit
.reqFlags
= data_blob_null
;
1778 reply
.negTokenInit
.reqFlagsPadding
= 0;
1779 reply
.negTokenInit
.mechToken
= tkt
;
1780 reply
.negTokenInit
.mechListMIC
= data_blob_null
;
1782 len
= spnego_write_data(ctx
, &to_server
, &reply
);
1783 data_blob_free(&tkt
);
1786 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1790 reply_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1791 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1793 TALLOC_FREE(reply_base64
);
1794 data_blob_free(&to_server
);
1795 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1799 static void manage_client_krb5_targ(struct spnego_data spnego
)
1801 switch (spnego
.negTokenTarg
.negResult
) {
1802 case SPNEGO_ACCEPT_INCOMPLETE
:
1803 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1804 x_fprintf(x_stdout
, "BH Got a Kerberos negTokenTarg with "
1805 "ACCEPT_INCOMPLETE\n");
1807 case SPNEGO_ACCEPT_COMPLETED
:
1808 DEBUG(10, ("Accept completed\n"));
1809 x_fprintf(x_stdout
, "AF\n");
1812 DEBUG(10, ("Rejected\n"));
1813 x_fprintf(x_stdout
, "NA\n");
1816 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1817 x_fprintf(x_stdout
, "AF\n");
1823 static void manage_gss_spnego_client_request(struct ntlm_auth_state
*state
,
1824 char *buf
, int length
)
1827 struct spnego_data spnego
;
1829 TALLOC_CTX
*ctx
= talloc_tos();
1831 if (!opt_username
|| !*opt_username
) {
1832 x_fprintf(x_stderr
, "username must be specified!\n\n");
1836 if (strlen(buf
) <= 3) {
1837 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1838 x_fprintf(x_stdout
, "BH SPNEGO query too short\n");
1842 request
= base64_decode_data_blob(buf
+3);
1844 if (strncmp(buf
, "PW ", 3) == 0) {
1846 /* We asked for a password and obviously got it :-) */
1848 opt_password
= SMB_STRNDUP((const char *)request
.data
, request
.length
);
1850 if (opt_password
== NULL
) {
1851 DEBUG(1, ("Out of memory\n"));
1852 x_fprintf(x_stdout
, "BH Out of memory\n");
1853 data_blob_free(&request
);
1857 x_fprintf(x_stdout
, "OK\n");
1858 data_blob_free(&request
);
1862 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1863 (strncmp(buf
, "AF ", 3) != 0) &&
1864 (strncmp(buf
, "NA ", 3) != 0) ) {
1865 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1866 x_fprintf(x_stdout
, "BH SPNEGO request invalid\n");
1867 data_blob_free(&request
);
1871 /* So we got a server challenge to generate a SPNEGO
1872 client-to-server request... */
1874 len
= spnego_read_data(ctx
, request
, &spnego
);
1875 data_blob_free(&request
);
1878 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1879 x_fprintf(x_stdout
, "BH Could not read SPNEGO data\n");
1883 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1885 /* The server offers a list of mechanisms */
1887 const char **mechType
= (const char **)spnego
.negTokenInit
.mechTypes
;
1889 while (*mechType
!= NULL
) {
1892 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1893 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1894 if (manage_client_krb5_init(spnego
))
1899 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1900 if (manage_client_ntlmssp_init(spnego
))
1907 DEBUG(1, ("Server offered no compatible mechanism\n"));
1908 x_fprintf(x_stdout
, "BH Server offered no compatible mechanism\n");
1912 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1914 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1915 /* On accept/reject Windows does not send the
1916 mechanism anymore. Handle that here and
1917 shut down the mechanisms. */
1919 switch (spnego
.negTokenTarg
.negResult
) {
1920 case SPNEGO_ACCEPT_COMPLETED
:
1921 x_fprintf(x_stdout
, "AF\n");
1924 x_fprintf(x_stdout
, "NA\n");
1927 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1928 "unknown negResult: %d\n",
1929 spnego
.negTokenTarg
.negResult
));
1930 x_fprintf(x_stdout
, "BH Got a negTokenTarg with"
1931 " no mech and an unknown "
1935 TALLOC_FREE(client_ntlmssp_state
);
1939 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1940 OID_NTLMSSP
) == 0) {
1941 manage_client_ntlmssp_targ(spnego
);
1946 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1947 OID_KERBEROS5_OLD
) == 0) {
1948 manage_client_krb5_targ(spnego
);
1955 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1956 x_fprintf(x_stdout
, "BH Got an SPNEGO token I could not handle\n");
1960 spnego_free_data(&spnego
);
1964 static void manage_ntlm_server_1_request(struct ntlm_auth_state
*state
,
1965 char *buf
, int length
)
1967 char *request
, *parameter
;
1968 static DATA_BLOB challenge
;
1969 static DATA_BLOB lm_response
;
1970 static DATA_BLOB nt_response
;
1971 static char *full_username
;
1972 static char *username
;
1973 static char *domain
;
1974 static char *plaintext_password
;
1975 static bool ntlm_server_1_user_session_key
;
1976 static bool ntlm_server_1_lm_session_key
;
1978 if (strequal(buf
, ".")) {
1979 if (!full_username
&& !username
) {
1980 x_fprintf(x_stdout
, "Error: No username supplied!\n");
1981 } else if (plaintext_password
) {
1982 /* handle this request as plaintext */
1983 if (!full_username
) {
1984 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
1985 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
1989 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
1990 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1992 x_fprintf(x_stdout
, "Authenticated: No\n");
1994 } else if (!lm_response
.data
&& !nt_response
.data
) {
1995 x_fprintf(x_stdout
, "Error: No password supplied!\n");
1996 } else if (!challenge
.data
) {
1997 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
1999 char *error_string
= NULL
;
2001 uchar user_session_key
[16];
2004 if (full_username
&& !username
) {
2006 fstring fstr_domain
;
2008 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
2009 /* username might be 'tainted', don't print into our new-line deleimianted stream */
2010 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
2012 SAFE_FREE(username
);
2014 username
= smb_xstrdup(fstr_user
);
2015 domain
= smb_xstrdup(fstr_domain
);
2019 domain
= smb_xstrdup(get_winbind_domain());
2022 if (ntlm_server_1_lm_session_key
)
2023 flags
|= WBFLAG_PAM_LMKEY
;
2025 if (ntlm_server_1_user_session_key
)
2026 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2028 if (!NT_STATUS_IS_OK(
2029 contact_winbind_auth_crap(username
,
2041 x_fprintf(x_stdout
, "Authenticated: No\n");
2042 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
2044 static char zeros
[16];
2046 char *hex_user_session_key
;
2048 x_fprintf(x_stdout
, "Authenticated: Yes\n");
2050 if (ntlm_server_1_lm_session_key
2051 && (memcmp(zeros
, lm_key
,
2052 sizeof(lm_key
)) != 0)) {
2053 hex_lm_key
= hex_encode_talloc(NULL
,
2054 (const unsigned char *)lm_key
,
2056 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
2057 TALLOC_FREE(hex_lm_key
);
2060 if (ntlm_server_1_user_session_key
2061 && (memcmp(zeros
, user_session_key
,
2062 sizeof(user_session_key
)) != 0)) {
2063 hex_user_session_key
= hex_encode_talloc(NULL
,
2064 (const unsigned char *)user_session_key
,
2065 sizeof(user_session_key
));
2066 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
2067 TALLOC_FREE(hex_user_session_key
);
2070 SAFE_FREE(error_string
);
2072 /* clear out the state */
2073 challenge
= data_blob_null
;
2074 nt_response
= data_blob_null
;
2075 lm_response
= data_blob_null
;
2076 SAFE_FREE(full_username
);
2077 SAFE_FREE(username
);
2079 SAFE_FREE(plaintext_password
);
2080 ntlm_server_1_user_session_key
= False
;
2081 ntlm_server_1_lm_session_key
= False
;
2082 x_fprintf(x_stdout
, ".\n");
2089 /* Indicates a base64 encoded structure */
2090 parameter
= strstr_m(request
, ":: ");
2092 parameter
= strstr_m(request
, ": ");
2095 DEBUG(0, ("Parameter not found!\n"));
2096 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2113 base64_decode_inplace(parameter
);
2116 if (strequal(request
, "LANMAN-Challenge")) {
2117 challenge
= strhex_to_data_blob(NULL
, parameter
);
2118 if (challenge
.length
!= 8) {
2119 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
2121 (int)challenge
.length
);
2122 challenge
= data_blob_null
;
2124 } else if (strequal(request
, "NT-Response")) {
2125 nt_response
= strhex_to_data_blob(NULL
, parameter
);
2126 if (nt_response
.length
< 24) {
2127 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
2129 (int)nt_response
.length
);
2130 nt_response
= data_blob_null
;
2132 } else if (strequal(request
, "LANMAN-Response")) {
2133 lm_response
= strhex_to_data_blob(NULL
, parameter
);
2134 if (lm_response
.length
!= 24) {
2135 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2137 (int)lm_response
.length
);
2138 lm_response
= data_blob_null
;
2140 } else if (strequal(request
, "Password")) {
2141 plaintext_password
= smb_xstrdup(parameter
);
2142 } else if (strequal(request
, "NT-Domain")) {
2143 domain
= smb_xstrdup(parameter
);
2144 } else if (strequal(request
, "Username")) {
2145 username
= smb_xstrdup(parameter
);
2146 } else if (strequal(request
, "Full-Username")) {
2147 full_username
= smb_xstrdup(parameter
);
2148 } else if (strequal(request
, "Request-User-Session-Key")) {
2149 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
2150 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
2151 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
2153 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2157 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
2158 char *buf
, int length
)
2160 char *request
, *parameter
;
2161 static DATA_BLOB new_nt_pswd
;
2162 static DATA_BLOB old_nt_hash_enc
;
2163 static DATA_BLOB new_lm_pswd
;
2164 static DATA_BLOB old_lm_hash_enc
;
2165 static char *full_username
= NULL
;
2166 static char *username
= NULL
;
2167 static char *domain
= NULL
;
2168 static char *newpswd
= NULL
;
2169 static char *oldpswd
= NULL
;
2171 if (strequal(buf
, ".")) {
2172 if(newpswd
&& oldpswd
) {
2173 uchar old_nt_hash
[16];
2174 uchar old_lm_hash
[16];
2175 uchar new_nt_hash
[16];
2176 uchar new_lm_hash
[16];
2178 new_nt_pswd
= data_blob(NULL
, 516);
2179 old_nt_hash_enc
= data_blob(NULL
, 16);
2181 /* Calculate the MD4 hash (NT compatible) of the
2183 E_md4hash(oldpswd
, old_nt_hash
);
2184 E_md4hash(newpswd
, new_nt_hash
);
2186 /* E_deshash returns false for 'long'
2187 passwords (> 14 DOS chars).
2189 Therefore, don't send a buffer
2190 encrypted with the truncated hash
2191 (it could allow an even easier
2192 attack on the password)
2194 Likewise, obey the admin's restriction
2197 if (lp_client_lanman_auth() &&
2198 E_deshash(newpswd
, new_lm_hash
) &&
2199 E_deshash(oldpswd
, old_lm_hash
)) {
2200 new_lm_pswd
= data_blob(NULL
, 516);
2201 old_lm_hash_enc
= data_blob(NULL
, 16);
2202 encode_pw_buffer(new_lm_pswd
.data
, newpswd
,
2205 arcfour_crypt(new_lm_pswd
.data
, old_nt_hash
, 516);
2206 E_old_pw_hash(new_nt_hash
, old_lm_hash
,
2207 old_lm_hash_enc
.data
);
2209 new_lm_pswd
.data
= NULL
;
2210 new_lm_pswd
.length
= 0;
2211 old_lm_hash_enc
.data
= NULL
;
2212 old_lm_hash_enc
.length
= 0;
2215 encode_pw_buffer(new_nt_pswd
.data
, newpswd
,
2218 arcfour_crypt(new_nt_pswd
.data
, old_nt_hash
, 516);
2219 E_old_pw_hash(new_nt_hash
, old_nt_hash
,
2220 old_nt_hash_enc
.data
);
2223 if (!full_username
&& !username
) {
2224 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2225 } else if ((!new_nt_pswd
.data
|| !old_nt_hash_enc
.data
) &&
2226 (!new_lm_pswd
.data
|| old_lm_hash_enc
.data
) ) {
2227 x_fprintf(x_stdout
, "Error: No NT or LM password "
2228 "blobs supplied!\n");
2230 char *error_string
= NULL
;
2232 if (full_username
&& !username
) {
2234 fstring fstr_domain
;
2236 if (!parse_ntlm_auth_domain_user(full_username
,
2239 /* username might be 'tainted', don't
2240 * print into our new-line
2241 * deleimianted stream */
2242 x_fprintf(x_stdout
, "Error: Could not "
2243 "parse into domain and "
2245 SAFE_FREE(username
);
2246 username
= smb_xstrdup(full_username
);
2248 SAFE_FREE(username
);
2250 username
= smb_xstrdup(fstr_user
);
2251 domain
= smb_xstrdup(fstr_domain
);
2256 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2263 x_fprintf(x_stdout
, "Password-Change: No\n");
2264 x_fprintf(x_stdout
, "Password-Change-Error: "
2265 "%s\n.\n", error_string
);
2267 x_fprintf(x_stdout
, "Password-Change: Yes\n");
2270 SAFE_FREE(error_string
);
2272 /* clear out the state */
2273 new_nt_pswd
= data_blob_null
;
2274 old_nt_hash_enc
= data_blob_null
;
2275 new_lm_pswd
= data_blob_null
;
2276 old_nt_hash_enc
= data_blob_null
;
2277 SAFE_FREE(full_username
);
2278 SAFE_FREE(username
);
2282 x_fprintf(x_stdout
, ".\n");
2289 /* Indicates a base64 encoded structure */
2290 parameter
= strstr_m(request
, ":: ");
2292 parameter
= strstr_m(request
, ": ");
2295 DEBUG(0, ("Parameter not found!\n"));
2296 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2312 base64_decode_inplace(parameter
);
2315 if (strequal(request
, "new-nt-password-blob")) {
2316 new_nt_pswd
= strhex_to_data_blob(NULL
, parameter
);
2317 if (new_nt_pswd
.length
!= 516) {
2318 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2319 "(got %d bytes, expected 516)\n.\n",
2321 (int)new_nt_pswd
.length
);
2322 new_nt_pswd
= data_blob_null
;
2324 } else if (strequal(request
, "old-nt-hash-blob")) {
2325 old_nt_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2326 if (old_nt_hash_enc
.length
!= 16) {
2327 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2328 "(got %d bytes, expected 16)\n.\n",
2330 (int)old_nt_hash_enc
.length
);
2331 old_nt_hash_enc
= data_blob_null
;
2333 } else if (strequal(request
, "new-lm-password-blob")) {
2334 new_lm_pswd
= strhex_to_data_blob(NULL
, parameter
);
2335 if (new_lm_pswd
.length
!= 516) {
2336 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2337 "(got %d bytes, expected 516)\n.\n",
2339 (int)new_lm_pswd
.length
);
2340 new_lm_pswd
= data_blob_null
;
2343 else if (strequal(request
, "old-lm-hash-blob")) {
2344 old_lm_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2345 if (old_lm_hash_enc
.length
!= 16)
2347 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2348 "(got %d bytes, expected 16)\n.\n",
2350 (int)old_lm_hash_enc
.length
);
2351 old_lm_hash_enc
= data_blob_null
;
2353 } else if (strequal(request
, "nt-domain")) {
2354 domain
= smb_xstrdup(parameter
);
2355 } else if(strequal(request
, "username")) {
2356 username
= smb_xstrdup(parameter
);
2357 } else if(strequal(request
, "full-username")) {
2358 username
= smb_xstrdup(parameter
);
2359 } else if(strequal(request
, "new-password")) {
2360 newpswd
= smb_xstrdup(parameter
);
2361 } else if (strequal(request
, "old-password")) {
2362 oldpswd
= smb_xstrdup(parameter
);
2364 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2368 static void manage_squid_request(struct ntlm_auth_state
*state
,
2369 stdio_helper_function fn
)
2372 char tmp
[INITIAL_BUFFER_SIZE
+1];
2373 int length
, buf_size
= 0;
2376 buf
= talloc_strdup(state
->mem_ctx
, "");
2378 DEBUG(0, ("Failed to allocate input buffer.\n"));
2379 x_fprintf(x_stderr
, "ERR\n");
2385 /* this is not a typo - x_fgets doesn't work too well under
2387 if (fgets(tmp
, sizeof(tmp
)-1, stdin
) == NULL
) {
2388 if (ferror(stdin
)) {
2389 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2390 "(%s)\n", ferror(stdin
),
2391 strerror(ferror(stdin
))));
2398 buf
= talloc_strdup_append_buffer(buf
, tmp
);
2399 buf_size
+= INITIAL_BUFFER_SIZE
;
2401 if (buf_size
> MAX_BUFFER_SIZE
) {
2402 DEBUG(2, ("Oversized message\n"));
2403 x_fprintf(x_stderr
, "ERR\n");
2408 c
= strchr(buf
, '\n');
2409 } while (c
== NULL
);
2414 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
2416 if (buf
[0] == '\0') {
2417 DEBUG(2, ("Invalid Request\n"));
2418 x_fprintf(x_stderr
, "ERR\n");
2423 fn(state
, buf
, length
);
2428 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
2429 TALLOC_CTX
*mem_ctx
;
2430 struct ntlm_auth_state
*state
;
2432 /* initialize FDescs */
2433 x_setbuf(x_stdout
, NULL
);
2434 x_setbuf(x_stderr
, NULL
);
2436 mem_ctx
= talloc_init("ntlm_auth");
2438 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2439 x_fprintf(x_stderr
, "ERR\n");
2443 state
= talloc_zero(mem_ctx
, struct ntlm_auth_state
);
2445 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2446 x_fprintf(x_stderr
, "ERR\n");
2450 state
->mem_ctx
= mem_ctx
;
2451 state
->helper_mode
= stdio_mode
;
2454 TALLOC_CTX
*frame
= talloc_stackframe();
2455 manage_squid_request(state
, fn
);
2461 /* Authenticate a user with a challenge/response */
2463 static bool check_auth_crap(void)
2468 char user_session_key
[16];
2470 char *hex_user_session_key
;
2472 static uint8 zeros
[16];
2474 x_setbuf(x_stdout
, NULL
);
2477 flags
|= WBFLAG_PAM_LMKEY
;
2479 if (request_user_session_key
)
2480 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2482 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
2484 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
2490 (unsigned char *)lm_key
,
2491 (unsigned char *)user_session_key
,
2492 &error_string
, NULL
);
2494 if (!NT_STATUS_IS_OK(nt_status
)) {
2495 x_fprintf(x_stdout
, "%s (0x%x)\n",
2497 NT_STATUS_V(nt_status
));
2498 SAFE_FREE(error_string
);
2503 && (memcmp(zeros
, lm_key
,
2504 sizeof(lm_key
)) != 0)) {
2505 hex_lm_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key
,
2507 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
2508 TALLOC_FREE(hex_lm_key
);
2510 if (request_user_session_key
2511 && (memcmp(zeros
, user_session_key
,
2512 sizeof(user_session_key
)) != 0)) {
2513 hex_user_session_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key
,
2514 sizeof(user_session_key
));
2515 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
2516 TALLOC_FREE(hex_user_session_key
);
2525 OPT_USERNAME
= 1000,
2534 OPT_USER_SESSION_KEY
,
2536 OPT_REQUIRE_MEMBERSHIP
,
2537 OPT_USE_CACHED_CREDS
,
2538 OPT_PAM_WINBIND_CONF
2541 int main(int argc
, const char **argv
)
2543 TALLOC_CTX
*frame
= talloc_stackframe();
2545 static const char *helper_protocol
;
2546 static int diagnostics
;
2548 static const char *hex_challenge
;
2549 static const char *hex_lm_response
;
2550 static const char *hex_nt_response
;
2554 /* NOTE: DO NOT change this interface without considering the implications!
2555 This is an external interface, which other programs will use to interact
2559 /* We do not use single-letter command abbreviations, because they harm future
2560 interface stability. */
2562 struct poptOption long_options
[] = {
2564 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
2565 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
2566 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
2567 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
2568 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
2569 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
2570 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2571 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
2572 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retrieve LM session key"},
2573 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retrieve User (NT) session key"},
2574 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "Use cached credentials if no password is given"},
2575 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
,
2577 "Perform diagnostics on the authentication chain"},
2578 { "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" },
2579 { "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" },
2580 POPT_COMMON_CONFIGFILE
2585 /* Samba client initialisation */
2588 setup_logging("ntlm_auth", DEBUG_STDERR
);
2592 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
2594 /* Parse command line options */
2597 poptPrintHelp(pc
, stderr
, 0);
2601 while((opt
= poptGetNextOpt(pc
)) != -1) {
2602 /* Get generic config options like --configfile */
2605 poptFreeContext(pc
);
2607 if (!lp_load_global(get_dyn_CONFIGFILE())) {
2608 d_fprintf(stderr
, "ntlm_auth: error opening config file %s. Error was %s\n",
2609 get_dyn_CONFIGFILE(), strerror(errno
));
2613 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2614 POPT_CONTEXT_KEEP_FIRST
);
2616 while((opt
= poptGetNextOpt(pc
)) != -1) {
2619 opt_challenge
= strhex_to_data_blob(NULL
, hex_challenge
);
2620 if (opt_challenge
.length
!= 8) {
2621 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2623 (int)opt_challenge
.length
);
2628 opt_lm_response
= strhex_to_data_blob(NULL
, hex_lm_response
);
2629 if (opt_lm_response
.length
!= 24) {
2630 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2632 (int)opt_lm_response
.length
);
2638 opt_nt_response
= strhex_to_data_blob(NULL
, hex_nt_response
);
2639 if (opt_nt_response
.length
< 24) {
2640 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2642 (int)opt_nt_response
.length
);
2647 case OPT_REQUIRE_MEMBERSHIP
:
2648 if (strncasecmp_m("S-", require_membership_of
, 2) == 0) {
2649 require_membership_of_sid
= require_membership_of
;
2656 char *domain
= SMB_STRDUP(opt_username
);
2657 char *p
= strchr_m(domain
, *lp_winbind_separator());
2661 if (opt_domain
&& !strequal(opt_domain
, domain
)) {
2662 x_fprintf(x_stderr
, "Domain specified in username (%s) "
2663 "doesn't match specified domain (%s)!\n\n",
2664 domain
, opt_domain
);
2665 poptPrintHelp(pc
, stderr
, 0);
2668 opt_domain
= domain
;
2674 /* Note: if opt_domain is "" then send no domain */
2675 if (opt_domain
== NULL
) {
2676 opt_domain
= get_winbind_domain();
2679 if (opt_workstation
== NULL
) {
2680 opt_workstation
= "";
2683 if (helper_protocol
) {
2685 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2686 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
2687 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
2691 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
2693 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2694 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
2700 if (!opt_username
|| !*opt_username
) {
2701 x_fprintf(x_stderr
, "username must be specified!\n\n");
2702 poptPrintHelp(pc
, stderr
, 0);
2706 if (opt_challenge
.length
) {
2707 if (!check_auth_crap()) {
2713 if (!opt_password
) {
2714 opt_password
= getpass("password: ");
2718 if (!diagnose_ntlm_auth()) {
2724 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
2725 if (!check_plaintext_auth(user
, opt_password
, True
)) {
2732 poptFreeContext(pc
);