2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
9 Copyright (C) Robert O'Callahan 2006 (added cached credential code).
10 Copyright (C) Kai Blin <kai@samba.org> 2008
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "utils/ntlm_auth.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../libcli/auth/spnego.h"
31 #include <iniparser.h>
33 #ifndef PAM_WINBIND_CONFIG_FILE
34 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
37 #define WINBIND_KRB5_AUTH 0x00000080
40 #define DBGC_CLASS DBGC_WINBIND
42 #define INITIAL_BUFFER_SIZE 300
43 #define MAX_BUFFER_SIZE 630000
45 enum stdio_helper_mode
{
53 NTLM_CHANGE_PASSWORD_1
,
57 enum ntlm_auth_cli_state
{
64 enum ntlm_auth_svr_state
{
71 struct ntlm_auth_state
{
73 enum stdio_helper_mode helper_mode
;
74 enum ntlm_auth_cli_state cli_state
;
75 enum ntlm_auth_svr_state svr_state
;
76 struct ntlmssp_state
*ntlmssp_state
;
78 char *want_feature_list
;
80 char *spnego_mech_oid
;
81 bool have_session_key
;
82 DATA_BLOB session_key
;
83 DATA_BLOB initial_message
;
86 typedef void (*stdio_helper_function
)(struct ntlm_auth_state
*state
, char *buf
,
89 static void manage_squid_basic_request (struct ntlm_auth_state
*state
,
90 char *buf
, int length
);
92 static void manage_squid_ntlmssp_request (struct ntlm_auth_state
*state
,
93 char *buf
, int length
);
95 static void manage_client_ntlmssp_request (struct ntlm_auth_state
*state
,
96 char *buf
, int length
);
98 static void manage_gss_spnego_request (struct ntlm_auth_state
*state
,
99 char *buf
, int length
);
101 static void manage_gss_spnego_client_request (struct ntlm_auth_state
*state
,
102 char *buf
, int length
);
104 static void manage_ntlm_server_1_request (struct ntlm_auth_state
*state
,
105 char *buf
, int length
);
107 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
108 char *buf
, int length
);
110 static const struct {
111 enum stdio_helper_mode mode
;
113 stdio_helper_function fn
;
114 } stdio_helper_protocols
[] = {
115 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
116 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
117 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request
},
118 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_client_ntlmssp_request
},
119 { GSS_SPNEGO
, "gss-spnego", manage_gss_spnego_request
},
120 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gss_spnego_client_request
},
121 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
122 { NTLM_CHANGE_PASSWORD_1
, "ntlm-change-password-1", manage_ntlm_change_password_1_request
},
123 { NUM_HELPER_MODES
, NULL
, NULL
}
126 const char *opt_username
;
127 const char *opt_domain
;
128 const char *opt_workstation
;
129 const char *opt_password
;
130 static DATA_BLOB opt_challenge
;
131 static DATA_BLOB opt_lm_response
;
132 static DATA_BLOB opt_nt_response
;
133 static int request_lm_key
;
134 static int request_user_session_key
;
135 static int use_cached_creds
;
137 static const char *require_membership_of
;
138 static const char *require_membership_of_sid
;
139 static const char *opt_pam_winbind_conf
;
141 static char winbind_separator(void)
143 struct winbindd_response response
;
150 ZERO_STRUCT(response
);
152 /* Send off request */
154 if (winbindd_request_response(WINBINDD_INFO
, NULL
, &response
) !=
155 NSS_STATUS_SUCCESS
) {
156 d_printf("could not obtain winbind separator!\n");
157 return *lp_winbind_separator();
160 sep
= response
.data
.info
.winbind_separator
;
164 d_printf("winbind separator was NULL!\n");
165 return *lp_winbind_separator();
171 const char *get_winbind_domain(void)
173 struct winbindd_response response
;
175 static fstring winbind_domain
;
176 if (*winbind_domain
) {
177 return winbind_domain
;
180 ZERO_STRUCT(response
);
182 /* Send off request */
184 if (winbindd_request_response(WINBINDD_DOMAIN_NAME
, NULL
, &response
) !=
185 NSS_STATUS_SUCCESS
) {
186 DEBUG(0, ("could not obtain winbind domain name!\n"));
187 return lp_workgroup();
190 fstrcpy(winbind_domain
, response
.data
.domain_name
);
192 return winbind_domain
;
196 const char *get_winbind_netbios_name(void)
198 struct winbindd_response response
;
200 static fstring winbind_netbios_name
;
202 if (*winbind_netbios_name
) {
203 return winbind_netbios_name
;
206 ZERO_STRUCT(response
);
208 /* Send off request */
210 if (winbindd_request_response(WINBINDD_NETBIOS_NAME
, NULL
, &response
) !=
211 NSS_STATUS_SUCCESS
) {
212 DEBUG(0, ("could not obtain winbind netbios name!\n"));
213 return global_myname();
216 fstrcpy(winbind_netbios_name
, response
.data
.netbios_name
);
218 return winbind_netbios_name
;
222 DATA_BLOB
get_challenge(void)
224 static DATA_BLOB chal
;
225 if (opt_challenge
.length
)
226 return opt_challenge
;
228 chal
= data_blob(NULL
, 8);
230 generate_random_buffer(chal
.data
, chal
.length
);
234 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
235 form DOMAIN/user into a domain and a user */
237 static bool parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
241 char *p
= strchr(domuser
,winbind_separator());
248 fstrcpy(domain
, domuser
);
249 domain
[PTR_DIFF(p
, domuser
)] = 0;
255 static bool get_require_membership_sid(void) {
256 struct winbindd_request request
;
257 struct winbindd_response response
;
259 if (!require_membership_of
) {
263 if (require_membership_of_sid
) {
267 /* Otherwise, ask winbindd for the name->sid request */
269 ZERO_STRUCT(request
);
270 ZERO_STRUCT(response
);
272 if (!parse_ntlm_auth_domain_user(require_membership_of
,
273 request
.data
.name
.dom_name
,
274 request
.data
.name
.name
)) {
275 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
276 require_membership_of
));
280 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
281 NSS_STATUS_SUCCESS
) {
282 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
283 require_membership_of
));
287 require_membership_of_sid
= SMB_STRDUP(response
.data
.sid
.sid
);
289 if (require_membership_of_sid
)
296 * Get some configuration from pam_winbind.conf to see if we
297 * need to contact trusted domain
300 int get_pam_winbind_config()
303 dictionary
*d
= NULL
;
305 if (!opt_pam_winbind_conf
|| !*opt_pam_winbind_conf
) {
306 opt_pam_winbind_conf
= PAM_WINBIND_CONFIG_FILE
;
309 d
= iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf
));
315 if (iniparser_getboolean(d
, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
316 ctrl
|= WINBIND_KRB5_AUTH
;
319 iniparser_freedict(d
);
324 /* Authenticate a user with a plaintext password */
326 static bool check_plaintext_auth(const char *user
, const char *pass
,
327 bool stdout_diagnostics
)
329 struct winbindd_request request
;
330 struct winbindd_response response
;
333 if (!get_require_membership_sid()) {
337 /* Send off request */
339 ZERO_STRUCT(request
);
340 ZERO_STRUCT(response
);
342 fstrcpy(request
.data
.auth
.user
, user
);
343 fstrcpy(request
.data
.auth
.pass
, pass
);
344 if (require_membership_of_sid
) {
345 strlcpy(request
.data
.auth
.require_membership_of_sid
,
346 require_membership_of_sid
,
347 sizeof(request
.data
.auth
.require_membership_of_sid
));
350 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
352 /* Display response */
354 if (stdout_diagnostics
) {
355 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
356 d_printf("Reading winbind reply failed! (0x01)\n");
359 d_printf("%s: %s (0x%x)\n",
360 response
.data
.auth
.nt_status_string
,
361 response
.data
.auth
.error_string
,
362 response
.data
.auth
.nt_status
);
364 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
365 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
368 DEBUG(3, ("%s: %s (0x%x)\n",
369 response
.data
.auth
.nt_status_string
,
370 response
.data
.auth
.error_string
,
371 response
.data
.auth
.nt_status
));
374 return (result
== NSS_STATUS_SUCCESS
);
377 /* authenticate a user with an encrypted username/password */
379 NTSTATUS
contact_winbind_auth_crap(const char *username
,
381 const char *workstation
,
382 const DATA_BLOB
*challenge
,
383 const DATA_BLOB
*lm_response
,
384 const DATA_BLOB
*nt_response
,
387 uint8 user_session_key
[16],
393 struct winbindd_request request
;
394 struct winbindd_response response
;
396 if (!get_require_membership_sid()) {
397 return NT_STATUS_INVALID_PARAMETER
;
400 ZERO_STRUCT(request
);
401 ZERO_STRUCT(response
);
403 request
.flags
= flags
;
405 request
.data
.auth_crap
.logon_parameters
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
| MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
407 if (require_membership_of_sid
)
408 fstrcpy(request
.data
.auth_crap
.require_membership_of_sid
, require_membership_of_sid
);
410 fstrcpy(request
.data
.auth_crap
.user
, username
);
411 fstrcpy(request
.data
.auth_crap
.domain
, domain
);
413 fstrcpy(request
.data
.auth_crap
.workstation
,
416 memcpy(request
.data
.auth_crap
.chal
, challenge
->data
, MIN(challenge
->length
, 8));
418 if (lm_response
&& lm_response
->length
) {
419 memcpy(request
.data
.auth_crap
.lm_resp
,
421 MIN(lm_response
->length
, sizeof(request
.data
.auth_crap
.lm_resp
)));
422 request
.data
.auth_crap
.lm_resp_len
= lm_response
->length
;
425 if (nt_response
&& nt_response
->length
) {
426 if (nt_response
->length
> sizeof(request
.data
.auth_crap
.nt_resp
)) {
427 request
.flags
= request
.flags
| WBFLAG_BIG_NTLMV2_BLOB
;
428 request
.extra_len
= nt_response
->length
;
429 request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, request
.extra_len
);
430 if (request
.extra_data
.data
== NULL
) {
431 return NT_STATUS_NO_MEMORY
;
433 memcpy(request
.extra_data
.data
, nt_response
->data
,
434 nt_response
->length
);
437 memcpy(request
.data
.auth_crap
.nt_resp
,
438 nt_response
->data
, nt_response
->length
);
440 request
.data
.auth_crap
.nt_resp_len
= nt_response
->length
;
443 result
= winbindd_request_response(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
444 SAFE_FREE(request
.extra_data
.data
);
446 /* Display response */
448 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
449 nt_status
= NT_STATUS_UNSUCCESSFUL
;
451 *error_string
= smb_xstrdup("Reading winbind reply failed!");
452 winbindd_free_response(&response
);
456 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
457 if (!NT_STATUS_IS_OK(nt_status
)) {
459 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
460 winbindd_free_response(&response
);
464 if ((flags
& WBFLAG_PAM_LMKEY
) && lm_key
) {
465 memcpy(lm_key
, response
.data
.auth
.first_8_lm_hash
,
466 sizeof(response
.data
.auth
.first_8_lm_hash
));
468 if ((flags
& WBFLAG_PAM_USER_SESSION_KEY
) && user_session_key
) {
469 memcpy(user_session_key
, response
.data
.auth
.user_session_key
,
470 sizeof(response
.data
.auth
.user_session_key
));
473 if (flags
& WBFLAG_PAM_UNIX_NAME
) {
474 *unix_name
= SMB_STRDUP(response
.data
.auth
.unix_username
);
476 winbindd_free_response(&response
);
477 return NT_STATUS_NO_MEMORY
;
481 winbindd_free_response(&response
);
485 /* contact server to change user password using auth crap */
486 static NTSTATUS
contact_winbind_change_pswd_auth_crap(const char *username
,
488 const DATA_BLOB new_nt_pswd
,
489 const DATA_BLOB old_nt_hash_enc
,
490 const DATA_BLOB new_lm_pswd
,
491 const DATA_BLOB old_lm_hash_enc
,
496 struct winbindd_request request
;
497 struct winbindd_response response
;
499 if (!get_require_membership_sid())
502 *error_string
= smb_xstrdup("Can't get membership sid.");
503 return NT_STATUS_INVALID_PARAMETER
;
506 ZERO_STRUCT(request
);
507 ZERO_STRUCT(response
);
510 fstrcpy(request
.data
.chng_pswd_auth_crap
.user
, username
);
512 fstrcpy(request
.data
.chng_pswd_auth_crap
.domain
,domain
);
514 if(new_nt_pswd
.length
)
516 memcpy(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
, new_nt_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
));
517 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
= new_nt_pswd
.length
;
520 if(old_nt_hash_enc
.length
)
522 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
));
523 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
= old_nt_hash_enc
.length
;
526 if(new_lm_pswd
.length
)
528 memcpy(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
, new_lm_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
));
529 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
= new_lm_pswd
.length
;
532 if(old_lm_hash_enc
.length
)
534 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
));
535 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
= old_lm_hash_enc
.length
;
538 result
= winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, &request
, &response
);
540 /* Display response */
542 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0))
544 nt_status
= NT_STATUS_UNSUCCESSFUL
;
546 *error_string
= smb_xstrdup("Reading winbind reply failed!");
547 winbindd_free_response(&response
);
551 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
552 if (!NT_STATUS_IS_OK(nt_status
))
555 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
556 winbindd_free_response(&response
);
560 winbindd_free_response(&response
);
565 static NTSTATUS
winbind_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
567 static const char zeros
[16] = { 0, };
569 char *error_string
= NULL
;
571 uint8 user_sess_key
[16];
572 char *unix_name
= NULL
;
574 nt_status
= contact_winbind_auth_crap(ntlmssp_state
->user
, ntlmssp_state
->domain
,
575 ntlmssp_state
->workstation
,
576 &ntlmssp_state
->chal
,
577 &ntlmssp_state
->lm_resp
,
578 &ntlmssp_state
->nt_resp
,
579 WBFLAG_PAM_LMKEY
| WBFLAG_PAM_USER_SESSION_KEY
| WBFLAG_PAM_UNIX_NAME
,
580 lm_key
, user_sess_key
,
581 &error_string
, &unix_name
);
583 if (NT_STATUS_IS_OK(nt_status
)) {
584 if (memcmp(lm_key
, zeros
, 8) != 0) {
585 *lm_session_key
= data_blob_talloc(ntlmssp_state
, NULL
, 16);
586 memcpy(lm_session_key
->data
, lm_key
, 8);
587 memset(lm_session_key
->data
+8, '\0', 8);
590 if (memcmp(user_sess_key
, zeros
, 16) != 0) {
591 *user_session_key
= data_blob_talloc(ntlmssp_state
, user_sess_key
, 16);
593 ntlmssp_state
->auth_context
= talloc_strdup(ntlmssp_state
,
596 DEBUG(NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
) ? 0 : 3,
597 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
598 ntlmssp_state
->domain
, ntlmssp_state
->user
,
599 ntlmssp_state
->workstation
,
600 error_string
? error_string
: "unknown error (NULL)"));
601 ntlmssp_state
->auth_context
= NULL
;
604 SAFE_FREE(error_string
);
605 SAFE_FREE(unix_name
);
609 static NTSTATUS
local_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
612 struct samr_Password lm_pw
, nt_pw
;
614 nt_lm_owf_gen (opt_password
, nt_pw
.hash
, lm_pw
.hash
);
616 nt_status
= ntlm_password_check(ntlmssp_state
,
618 &ntlmssp_state
->chal
,
619 &ntlmssp_state
->lm_resp
,
620 &ntlmssp_state
->nt_resp
,
623 ntlmssp_state
->domain
,
624 &lm_pw
, &nt_pw
, user_session_key
, lm_session_key
);
626 if (NT_STATUS_IS_OK(nt_status
)) {
627 ntlmssp_state
->auth_context
= talloc_asprintf(ntlmssp_state
,
628 "%s%c%s", ntlmssp_state
->domain
,
629 *lp_winbind_separator(),
630 ntlmssp_state
->user
);
632 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
633 ntlmssp_state
->domain
, ntlmssp_state
->user
, ntlmssp_state
->workstation
,
634 nt_errstr(nt_status
)));
635 ntlmssp_state
->auth_context
= NULL
;
640 static NTSTATUS
ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE
**client_ntlmssp_state
)
643 if ( (opt_username
== NULL
) || (opt_domain
== NULL
) ) {
644 status
= NT_STATUS_UNSUCCESSFUL
;
645 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
646 return NT_STATUS_INVALID_PARAMETER
;
649 status
= ntlmssp_client_start(client_ntlmssp_state
);
651 if (!NT_STATUS_IS_OK(status
)) {
652 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
654 ntlmssp_end(client_ntlmssp_state
);
658 status
= ntlmssp_set_username(*client_ntlmssp_state
, opt_username
);
660 if (!NT_STATUS_IS_OK(status
)) {
661 DEBUG(1, ("Could not set username: %s\n",
663 ntlmssp_end(client_ntlmssp_state
);
667 status
= ntlmssp_set_domain(*client_ntlmssp_state
, opt_domain
);
669 if (!NT_STATUS_IS_OK(status
)) {
670 DEBUG(1, ("Could not set domain: %s\n",
672 ntlmssp_end(client_ntlmssp_state
);
677 status
= ntlmssp_set_password(*client_ntlmssp_state
, opt_password
);
679 if (!NT_STATUS_IS_OK(status
)) {
680 DEBUG(1, ("Could not set password: %s\n",
682 ntlmssp_end(client_ntlmssp_state
);
690 static NTSTATUS
ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE
**ntlmssp_state
)
692 NTSTATUS status
= ntlmssp_server_start(ntlmssp_state
);
694 if (!NT_STATUS_IS_OK(status
)) {
695 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
700 /* Have we been given a local password, or should we ask winbind? */
702 (*ntlmssp_state
)->check_password
= local_pw_check
;
703 (*ntlmssp_state
)->get_domain
= lp_workgroup
;
704 (*ntlmssp_state
)->get_global_myname
= global_myname
;
706 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
707 (*ntlmssp_state
)->get_domain
= get_winbind_domain
;
708 (*ntlmssp_state
)->get_global_myname
= get_winbind_netbios_name
;
713 /*******************************************************************
714 Used by firefox to drive NTLM auth to IIS servers.
715 *******************************************************************/
717 static NTSTATUS
do_ccache_ntlm_auth(DATA_BLOB initial_msg
, DATA_BLOB challenge_msg
,
720 struct winbindd_request wb_request
;
721 struct winbindd_response wb_response
;
725 /* get winbindd to do the ntlmssp step on our behalf */
726 ZERO_STRUCT(wb_request
);
727 ZERO_STRUCT(wb_response
);
730 * This is tricky here. If we set krb5_auth in pam_winbind.conf
731 * creds for users in trusted domain will be stored the winbindd
732 * child of the trusted domain. If we ask the primary domain for
733 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
734 * domain's child for ccache_ntlm_auth. that is to say, we have to
735 * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
737 ctrl
= get_pam_winbind_config();
739 if (ctrl
& WINBIND_KRB5_AUTH
) {
740 wb_request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
743 fstr_sprintf(wb_request
.data
.ccache_ntlm_auth
.user
,
744 "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
745 wb_request
.data
.ccache_ntlm_auth
.uid
= geteuid();
746 wb_request
.data
.ccache_ntlm_auth
.initial_blob_len
= initial_msg
.length
;
747 wb_request
.data
.ccache_ntlm_auth
.challenge_blob_len
= challenge_msg
.length
;
748 wb_request
.extra_len
= initial_msg
.length
+ challenge_msg
.length
;
750 if (wb_request
.extra_len
> 0) {
751 wb_request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, wb_request
.extra_len
);
752 if (wb_request
.extra_data
.data
== NULL
) {
753 return NT_STATUS_NO_MEMORY
;
756 memcpy(wb_request
.extra_data
.data
, initial_msg
.data
, initial_msg
.length
);
757 memcpy(wb_request
.extra_data
.data
+ initial_msg
.length
,
758 challenge_msg
.data
, challenge_msg
.length
);
761 result
= winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH
, &wb_request
, &wb_response
);
762 SAFE_FREE(wb_request
.extra_data
.data
);
764 if (result
!= NSS_STATUS_SUCCESS
) {
765 winbindd_free_response(&wb_response
);
766 return NT_STATUS_UNSUCCESSFUL
;
770 *reply
= data_blob(wb_response
.extra_data
.data
,
771 wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
);
772 if (wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
> 0 &&
773 reply
->data
== NULL
) {
774 winbindd_free_response(&wb_response
);
775 return NT_STATUS_NO_MEMORY
;
779 winbindd_free_response(&wb_response
);
780 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
783 static void manage_squid_ntlmssp_request_int(struct ntlm_auth_state
*state
,
784 char *buf
, int length
,
788 DATA_BLOB request
, reply
;
791 if (strlen(buf
) < 2) {
792 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
793 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
797 if (strlen(buf
) > 3) {
798 if(strncmp(buf
, "SF ", 3) == 0){
799 DEBUG(10, ("Setting flags to negotioate\n"));
800 TALLOC_FREE(state
->want_feature_list
);
801 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
803 *response
= talloc_strdup(mem_ctx
, "OK");
806 request
= base64_decode_data_blob(buf
+ 3);
808 request
= data_blob_null
;
811 if ((strncmp(buf
, "PW ", 3) == 0)) {
812 /* The calling application wants us to use a local password
813 * (rather than winbindd) */
815 opt_password
= SMB_STRNDUP((const char *)request
.data
,
818 if (opt_password
== NULL
) {
819 DEBUG(1, ("Out of memory\n"));
820 *response
= talloc_strdup(mem_ctx
, "BH Out of memory");
821 data_blob_free(&request
);
825 *response
= talloc_strdup(mem_ctx
, "OK");
826 data_blob_free(&request
);
830 if (strncmp(buf
, "YR", 2) == 0) {
831 if (state
->ntlmssp_state
)
832 ntlmssp_end(&state
->ntlmssp_state
);
833 state
->svr_state
= SERVER_INITIAL
;
834 } else if (strncmp(buf
, "KK", 2) == 0) {
835 /* No special preprocessing required */
836 } else if (strncmp(buf
, "GF", 2) == 0) {
837 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
839 if (state
->svr_state
== SERVER_FINISHED
) {
840 *response
= talloc_asprintf(mem_ctx
, "GF 0x%08x",
844 *response
= talloc_strdup(mem_ctx
, "BH\n");
846 data_blob_free(&request
);
848 } else if (strncmp(buf
, "GK", 2) == 0) {
849 DEBUG(10, ("Requested NTLMSSP session key\n"));
850 if(state
->have_session_key
) {
851 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
853 *response
= talloc_asprintf(mem_ctx
, "GK %s",
854 key64
? key64
: "<NULL>");
857 *response
= talloc_strdup(mem_ctx
, "BH");
860 data_blob_free(&request
);
863 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
864 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
868 if (!state
->ntlmssp_state
) {
869 nt_status
= ntlm_auth_start_ntlmssp_server(
870 &state
->ntlmssp_state
);
871 if (!NT_STATUS_IS_OK(nt_status
)) {
872 *response
= talloc_asprintf(
873 mem_ctx
, "BH %s", nt_errstr(nt_status
));
876 ntlmssp_want_feature_list(state
->ntlmssp_state
,
877 state
->want_feature_list
);
880 DEBUG(10, ("got NTLMSSP packet:\n"));
881 dump_data(10, request
.data
, request
.length
);
883 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
, &reply
);
885 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
886 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
888 *response
= talloc_asprintf(mem_ctx
, "TT %s", reply_base64
);
889 TALLOC_FREE(reply_base64
);
890 data_blob_free(&reply
);
891 state
->svr_state
= SERVER_CHALLENGE
;
892 DEBUG(10, ("NTLMSSP challenge\n"));
893 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
894 *response
= talloc_asprintf(mem_ctx
, "BH %s",
895 nt_errstr(nt_status
));
896 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
898 ntlmssp_end(&state
->ntlmssp_state
);
899 } else if (!NT_STATUS_IS_OK(nt_status
)) {
900 *response
= talloc_asprintf(mem_ctx
, "NA %s",
901 nt_errstr(nt_status
));
902 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
904 *response
= talloc_asprintf(
906 (char *)state
->ntlmssp_state
->auth_context
);
907 DEBUG(10, ("NTLMSSP OK!\n"));
909 if(state
->have_session_key
)
910 data_blob_free(&state
->session_key
);
911 state
->session_key
= data_blob(
912 state
->ntlmssp_state
->session_key
.data
,
913 state
->ntlmssp_state
->session_key
.length
);
914 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
915 state
->have_session_key
= true;
916 state
->svr_state
= SERVER_FINISHED
;
919 data_blob_free(&request
);
922 static void manage_squid_ntlmssp_request(struct ntlm_auth_state
*state
,
923 char *buf
, int length
)
927 manage_squid_ntlmssp_request_int(state
, buf
, length
,
928 talloc_tos(), &response
);
930 if (response
== NULL
) {
931 x_fprintf(x_stdout
, "BH Out of memory\n");
934 x_fprintf(x_stdout
, "%s\n", response
);
935 TALLOC_FREE(response
);
938 static void manage_client_ntlmssp_request(struct ntlm_auth_state
*state
,
939 char *buf
, int length
)
941 DATA_BLOB request
, reply
;
944 if (!opt_username
|| !*opt_username
) {
945 x_fprintf(x_stderr
, "username must be specified!\n\n");
949 if (strlen(buf
) < 2) {
950 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
951 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
955 if (strlen(buf
) > 3) {
956 if(strncmp(buf
, "SF ", 3) == 0) {
957 DEBUG(10, ("Looking for flags to negotiate\n"));
958 talloc_free(state
->want_feature_list
);
959 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
961 x_fprintf(x_stdout
, "OK\n");
964 request
= base64_decode_data_blob(buf
+ 3);
966 request
= data_blob_null
;
969 if (strncmp(buf
, "PW ", 3) == 0) {
970 /* We asked for a password and obviously got it :-) */
972 opt_password
= SMB_STRNDUP((const char *)request
.data
,
975 if (opt_password
== NULL
) {
976 DEBUG(1, ("Out of memory\n"));
977 x_fprintf(x_stdout
, "BH Out of memory\n");
978 data_blob_free(&request
);
982 x_fprintf(x_stdout
, "OK\n");
983 data_blob_free(&request
);
987 if (!state
->ntlmssp_state
&& use_cached_creds
) {
988 /* check whether cached credentials are usable. */
989 DATA_BLOB empty_blob
= data_blob_null
;
991 nt_status
= do_ccache_ntlm_auth(empty_blob
, empty_blob
, NULL
);
992 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
993 /* failed to use cached creds */
994 use_cached_creds
= False
;
998 if (opt_password
== NULL
&& !use_cached_creds
) {
999 /* Request a password from the calling process. After
1000 sending it, the calling process should retry asking for the
1003 DEBUG(10, ("Requesting password\n"));
1004 x_fprintf(x_stdout
, "PW\n");
1008 if (strncmp(buf
, "YR", 2) == 0) {
1009 if (state
->ntlmssp_state
)
1010 ntlmssp_end(&state
->ntlmssp_state
);
1011 state
->cli_state
= CLIENT_INITIAL
;
1012 } else if (strncmp(buf
, "TT", 2) == 0) {
1013 /* No special preprocessing required */
1014 } else if (strncmp(buf
, "GF", 2) == 0) {
1015 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1017 if(state
->cli_state
== CLIENT_FINISHED
) {
1018 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
1021 x_fprintf(x_stdout
, "BH\n");
1024 data_blob_free(&request
);
1026 } else if (strncmp(buf
, "GK", 2) == 0 ) {
1027 DEBUG(10, ("Requested session key\n"));
1029 if(state
->cli_state
== CLIENT_FINISHED
) {
1030 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
1031 state
->session_key
);
1032 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
1036 x_fprintf(x_stdout
, "BH\n");
1039 data_blob_free(&request
);
1042 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
1043 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
1047 if (!state
->ntlmssp_state
) {
1048 nt_status
= ntlm_auth_start_ntlmssp_client(
1049 &state
->ntlmssp_state
);
1050 if (!NT_STATUS_IS_OK(nt_status
)) {
1051 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1054 ntlmssp_want_feature_list(state
->ntlmssp_state
,
1055 state
->want_feature_list
);
1056 state
->initial_message
= data_blob_null
;
1059 DEBUG(10, ("got NTLMSSP packet:\n"));
1060 dump_data(10, request
.data
, request
.length
);
1062 if (use_cached_creds
&& !opt_password
&&
1063 (state
->cli_state
== CLIENT_RESPONSE
)) {
1064 nt_status
= do_ccache_ntlm_auth(state
->initial_message
, request
,
1067 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
,
1071 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1072 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
1074 if (state
->cli_state
== CLIENT_INITIAL
) {
1075 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
1076 state
->initial_message
= reply
;
1077 state
->cli_state
= CLIENT_RESPONSE
;
1079 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
1080 data_blob_free(&reply
);
1082 TALLOC_FREE(reply_base64
);
1083 DEBUG(10, ("NTLMSSP challenge\n"));
1084 } else if (NT_STATUS_IS_OK(nt_status
)) {
1085 char *reply_base64
= base64_encode_data_blob(talloc_tos(),
1087 x_fprintf(x_stdout
, "AF %s\n", reply_base64
);
1088 TALLOC_FREE(reply_base64
);
1090 if(state
->have_session_key
)
1091 data_blob_free(&state
->session_key
);
1093 state
->session_key
= data_blob(
1094 state
->ntlmssp_state
->session_key
.data
,
1095 state
->ntlmssp_state
->session_key
.length
);
1096 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
1097 state
->have_session_key
= true;
1099 DEBUG(10, ("NTLMSSP OK!\n"));
1100 state
->cli_state
= CLIENT_FINISHED
;
1101 if (state
->ntlmssp_state
)
1102 ntlmssp_end(&state
->ntlmssp_state
);
1104 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1105 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
1106 state
->cli_state
= CLIENT_ERROR
;
1107 if (state
->ntlmssp_state
)
1108 ntlmssp_end(&state
->ntlmssp_state
);
1111 data_blob_free(&request
);
1114 static void manage_squid_basic_request(struct ntlm_auth_state
*state
,
1115 char *buf
, int length
)
1120 pass
=(char *)memchr(buf
,' ',length
);
1122 DEBUG(2, ("Password not found. Denying access\n"));
1123 x_fprintf(x_stdout
, "ERR\n");
1129 if (state
->helper_mode
== SQUID_2_5_BASIC
) {
1130 rfc1738_unescape(user
);
1131 rfc1738_unescape(pass
);
1134 if (check_plaintext_auth(user
, pass
, False
)) {
1135 x_fprintf(x_stdout
, "OK\n");
1137 x_fprintf(x_stdout
, "ERR\n");
1141 static void offer_gss_spnego_mechs(void) {
1144 struct spnego_data spnego
;
1147 TALLOC_CTX
*ctx
= talloc_tos();
1151 ZERO_STRUCT(spnego
);
1153 myname_lower
= talloc_strdup(ctx
, global_myname());
1154 if (!myname_lower
) {
1157 strlower_m(myname_lower
);
1159 principal
= talloc_asprintf(ctx
, "%s$@%s", myname_lower
, lp_realm());
1164 /* Server negTokenInit (mech offerings) */
1165 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1166 spnego
.negTokenInit
.mechTypes
= talloc_array(ctx
, const char *, 4);
1168 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_KERBEROS5_OLD
);
1169 spnego
.negTokenInit
.mechTypes
[1] = talloc_strdup(ctx
, OID_KERBEROS5
);
1170 spnego
.negTokenInit
.mechTypes
[2] = talloc_strdup(ctx
, OID_NTLMSSP
);
1171 spnego
.negTokenInit
.mechTypes
[3] = NULL
;
1173 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_NTLMSSP
);
1174 spnego
.negTokenInit
.mechTypes
[1] = NULL
;
1178 spnego
.negTokenInit
.mechListMIC
= data_blob_talloc(ctx
, principal
,
1181 len
= spnego_write_data(ctx
, &token
, &spnego
);
1182 spnego_free_data(&spnego
);
1185 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1186 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1190 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1191 x_fprintf(x_stdout
, "TT %s *\n", reply_base64
);
1193 TALLOC_FREE(reply_base64
);
1194 data_blob_free(&token
);
1195 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1199 static bool _spnego_parse_krb5_wrap(TALLOC_CTX
*ctx
, DATA_BLOB blob
, DATA_BLOB
*ticket
, uint8 tok_id
[2])
1205 data
= asn1_init(talloc_tos());
1210 asn1_load(data
, blob
);
1211 asn1_start_tag(data
, ASN1_APPLICATION(0));
1212 asn1_check_OID(data
, OID_KERBEROS5
);
1214 data_remaining
= asn1_tag_remaining(data
);
1216 if (data_remaining
< 3) {
1217 data
->has_error
= True
;
1219 asn1_read(data
, tok_id
, 2);
1220 data_remaining
-= 2;
1221 *ticket
= data_blob_talloc(ctx
, NULL
, data_remaining
);
1222 asn1_read(data
, ticket
->data
, ticket
->length
);
1227 ret
= !data
->has_error
;
1229 if (data
->has_error
) {
1230 data_blob_free(ticket
);
1238 static void manage_gss_spnego_request(struct ntlm_auth_state
*state
,
1239 char *buf
, int length
)
1241 struct spnego_data request
, response
;
1243 DATA_BLOB raw_in_token
= data_blob_null
;
1244 DATA_BLOB raw_out_token
= data_blob_null
;
1247 TALLOC_CTX
*ctx
= talloc_tos();
1250 char *domain
= NULL
;
1252 const char *reply_code
;
1254 char *reply_argument
= NULL
;
1255 char *supportedMech
= NULL
;
1257 if (strlen(buf
) < 2) {
1258 DEBUG(1, ("SPENGO query [%s] invalid\n", buf
));
1259 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1263 if (strncmp(buf
, "YR", 2) == 0) {
1264 if (state
->ntlmssp_state
)
1265 ntlmssp_end(&state
->ntlmssp_state
);
1266 TALLOC_FREE(state
->spnego_mech
);
1267 TALLOC_FREE(state
->spnego_mech_oid
);
1268 } else if (strncmp(buf
, "KK", 2) == 0) {
1271 DEBUG(1, ("SPENGO query [%s] invalid\n", buf
));
1272 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1276 if ( (strlen(buf
) == 2)) {
1278 /* no client data, get the negTokenInit offering
1281 offer_gss_spnego_mechs();
1285 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1287 if (strlen(buf
) <= 3) {
1288 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1289 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1293 token
= base64_decode_data_blob(buf
+ 3);
1295 if ((token
.length
>= 7)
1296 && (strncmp((char *)token
.data
, "NTLMSSP", 7) == 0)) {
1299 data_blob_free(&token
);
1301 DEBUG(10, ("Could not parse GSS-SPNEGO, trying raw "
1304 manage_squid_ntlmssp_request_int(state
, buf
, length
,
1305 talloc_tos(), &reply
);
1306 if (reply
== NULL
) {
1307 x_fprintf(x_stdout
, "BH Out of memory\n");
1311 if (strncmp(reply
, "AF ", 3) == 0) {
1312 x_fprintf(x_stdout
, "AF * %s\n", reply
+3);
1314 x_fprintf(x_stdout
, "%s *\n", reply
);
1321 ZERO_STRUCT(request
);
1322 len
= spnego_read_data(ctx
, token
, &request
);
1323 data_blob_free(&token
);
1326 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1327 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1331 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1339 if (state
->spnego_mech
) {
1340 DEBUG(1, ("Client restarted SPNEGO with NegTokenInit "
1341 "while mech[%s] was already negotiated\n",
1342 state
->spnego_mech
));
1343 x_fprintf(x_stdout
, "BH Client send NegTokenInit twice\n");
1347 /* Second request from Client. This is where the
1348 client offers its mechanism to use. */
1350 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
1351 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
1352 DEBUG(1, ("Client did not offer any mechanism\n"));
1353 x_fprintf(x_stdout
, "BH Client did not offer any "
1358 status
= NT_STATUS_UNSUCCESSFUL
;
1359 for (i
= 0; request
.negTokenInit
.mechTypes
[i
] != NULL
; i
++) {
1360 DEBUG(10,("got mech[%d][%s]\n",
1361 i
, request
.negTokenInit
.mechTypes
[i
]));
1363 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5_OLD
) == 0) {
1367 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5
) == 0) {
1372 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_NTLMSSP
) == 0) {
1378 used_idx
= ntlm_idx
;
1380 if (krb5_idx
!= -1) {
1382 used_idx
= krb5_idx
;
1385 if (ntlm_idx
> -1) {
1386 state
->spnego_mech
= talloc_strdup(state
, "ntlmssp");
1387 if (state
->spnego_mech
== NULL
) {
1388 x_fprintf(x_stdout
, "BH Out of memory\n");
1392 if (state
->ntlmssp_state
) {
1393 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1394 "already got one\n"));
1395 x_fprintf(x_stdout
, "BH Client wants a new "
1396 "NTLMSSP challenge, but "
1397 "already got one\n");
1398 ntlmssp_end(&state
->ntlmssp_state
);
1402 status
= ntlm_auth_start_ntlmssp_server(&state
->ntlmssp_state
);
1403 if (!NT_STATUS_IS_OK(status
)) {
1404 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1410 if (krb5_idx
> -1) {
1411 state
->spnego_mech
= talloc_strdup(state
, "krb5");
1412 if (state
->spnego_mech
== NULL
) {
1413 x_fprintf(x_stdout
, "BH Out of memory\n");
1418 if (used_idx
> -1) {
1419 state
->spnego_mech_oid
= talloc_strdup(state
,
1420 request
.negTokenInit
.mechTypes
[used_idx
]);
1421 if (state
->spnego_mech_oid
== NULL
) {
1422 x_fprintf(x_stdout
, "BH Out of memory\n");
1425 supportedMech
= talloc_strdup(ctx
, state
->spnego_mech_oid
);
1426 if (supportedMech
== NULL
) {
1427 x_fprintf(x_stdout
, "BH Out of memory\n");
1431 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1433 status
= NT_STATUS_NOT_SUPPORTED
;
1435 if (used_idx
== 0) {
1436 status
= NT_STATUS_OK
;
1437 raw_in_token
= request
.negTokenInit
.mechToken
;
1440 if (state
->spnego_mech
== NULL
) {
1441 DEBUG(1,("Got netTokenTarg without negTokenInit\n"));
1442 x_fprintf(x_stdout
, "BH Got a negTokenTarg without "
1447 if ((request
.negTokenTarg
.supportedMech
!= NULL
) &&
1448 (strcmp(request
.negTokenTarg
.supportedMech
, state
->spnego_mech_oid
) != 0 ) ) {
1449 DEBUG(1, ("Got a negTokenTarg with mech[%s] while [%s] was already negotiated\n",
1450 request
.negTokenTarg
.supportedMech
,
1451 state
->spnego_mech_oid
));
1452 x_fprintf(x_stdout
, "BH Got a negTokenTarg with speficied mech\n");
1456 status
= NT_STATUS_OK
;
1457 raw_in_token
= request
.negTokenTarg
.responseToken
;
1460 if (!NT_STATUS_IS_OK(status
)) {
1461 /* error or more processing */
1462 } else if (strcmp(state
->spnego_mech
, "ntlmssp") == 0) {
1464 DEBUG(10, ("got NTLMSSP packet:\n"));
1465 dump_data(10, raw_in_token
.data
, raw_in_token
.length
);
1467 status
= ntlmssp_update(state
->ntlmssp_state
,
1470 if (NT_STATUS_IS_OK(status
)) {
1471 user
= talloc_strdup(ctx
, state
->ntlmssp_state
->user
);
1472 domain
= talloc_strdup(ctx
, state
->ntlmssp_state
->domain
);
1474 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1475 ntlmssp_end(&state
->ntlmssp_state
);
1478 } else if (strcmp(state
->spnego_mech
, "krb5") == 0) {
1481 DATA_BLOB session_key
;
1482 struct PAC_DATA
*pac_data
= NULL
;
1486 if (!_spnego_parse_krb5_wrap(ctx
, raw_in_token
,
1488 DEBUG(1, ("spnego_parse_krb5_wrap failed\n"));
1489 x_fprintf(x_stdout
, "BH spnego_parse_krb5_wrap failed\n");
1493 status
= ads_verify_ticket(ctx
, lp_realm(), 0,
1495 &principal
, &pac_data
, &ap_rep
,
1496 &session_key
, True
);
1498 /* Now in "principal" we have the name we are authenticated as. */
1500 if (NT_STATUS_IS_OK(status
)) {
1502 domain
= strchr_m(principal
, '@');
1504 if (domain
== NULL
) {
1505 DEBUG(1, ("Did not get a valid principal "
1506 "from ads_verify_ticket\n"));
1507 x_fprintf(x_stdout
, "BH Did not get a "
1508 "valid principal from "
1509 "ads_verify_ticket\n");
1514 domain
= talloc_strdup(ctx
, domain
);
1515 user
= talloc_strdup(ctx
, principal
);
1518 struct PAC_LOGON_INFO
*logon_info
;
1519 logon_info
= get_logon_info_from_pac(
1522 netsamlogon_cache_store(
1524 &logon_info
->info3
);
1528 data_blob_free(&ap_rep
);
1529 data_blob_free(&session_key
);
1531 data_blob_free(&ticket
);
1535 spnego_free_data(&request
);
1536 ZERO_STRUCT(response
);
1537 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1539 if (NT_STATUS_IS_OK(status
)) {
1540 TALLOC_FREE(state
->spnego_mech
);
1541 TALLOC_FREE(state
->spnego_mech_oid
);
1542 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
1543 response
.negTokenTarg
.responseToken
= raw_out_token
;
1545 reply_argument
= talloc_asprintf(ctx
, "%s\\%s", domain
, user
);
1546 } else if (NT_STATUS_EQUAL(status
,
1547 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1548 response
.negTokenTarg
.supportedMech
= supportedMech
;
1549 response
.negTokenTarg
.responseToken
= raw_out_token
;
1550 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1552 reply_argument
= talloc_strdup(ctx
, "*");
1554 TALLOC_FREE(state
->spnego_mech
);
1555 TALLOC_FREE(state
->spnego_mech_oid
);
1556 data_blob_free(&raw_out_token
);
1557 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1559 reply_argument
= talloc_strdup(ctx
, nt_errstr(status
));
1562 if (!reply_argument
) {
1563 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1564 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1565 spnego_free_data(&response
);
1569 len
= spnego_write_data(ctx
, &token
, &response
);
1570 spnego_free_data(&response
);
1573 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1574 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1578 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1580 x_fprintf(x_stdout
, "%s %s %s\n",
1581 reply_code
, reply_base64
, reply_argument
);
1583 TALLOC_FREE(reply_base64
);
1584 data_blob_free(&token
);
1589 static NTLMSSP_STATE
*client_ntlmssp_state
= NULL
;
1591 static bool manage_client_ntlmssp_init(struct spnego_data spnego
)
1594 DATA_BLOB null_blob
= data_blob_null
;
1595 DATA_BLOB to_server
;
1596 char *to_server_base64
;
1597 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1598 TALLOC_CTX
*ctx
= talloc_tos();
1600 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1602 if (client_ntlmssp_state
!= NULL
) {
1603 DEBUG(1, ("Request for initial SPNEGO request where "
1604 "we already have a state\n"));
1608 if (!client_ntlmssp_state
) {
1609 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1610 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1616 if (opt_password
== NULL
) {
1618 /* Request a password from the calling process. After
1619 sending it, the calling process should retry with
1620 the negTokenInit. */
1622 DEBUG(10, ("Requesting password\n"));
1623 x_fprintf(x_stdout
, "PW\n");
1627 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1628 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1629 spnego
.negTokenInit
.reqFlags
= data_blob_null
;
1630 spnego
.negTokenInit
.reqFlagsPadding
= 0;
1631 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1633 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1634 &spnego
.negTokenInit
.mechToken
);
1636 if ( !(NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) ||
1637 NT_STATUS_IS_OK(status
)) ) {
1638 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1639 nt_errstr(status
)));
1640 ntlmssp_end(&client_ntlmssp_state
);
1644 spnego_write_data(ctx
, &to_server
, &spnego
);
1645 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1647 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1648 data_blob_free(&to_server
);
1649 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1650 TALLOC_FREE(to_server_base64
);
1654 static void manage_client_ntlmssp_targ(struct spnego_data spnego
)
1657 DATA_BLOB null_blob
= data_blob_null
;
1659 DATA_BLOB to_server
;
1660 char *to_server_base64
;
1661 TALLOC_CTX
*ctx
= talloc_tos();
1663 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1665 if (client_ntlmssp_state
== NULL
) {
1666 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1667 x_fprintf(x_stdout
, "BH Got NTLMSSP tArg without a client state\n");
1671 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1672 x_fprintf(x_stdout
, "NA\n");
1673 ntlmssp_end(&client_ntlmssp_state
);
1677 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1678 x_fprintf(x_stdout
, "AF\n");
1679 ntlmssp_end(&client_ntlmssp_state
);
1683 status
= ntlmssp_update(client_ntlmssp_state
,
1684 spnego
.negTokenTarg
.responseToken
,
1687 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1688 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1689 "ntlmssp_client_update, got: %s\n",
1690 nt_errstr(status
)));
1691 x_fprintf(x_stdout
, "BH Expected MORE_PROCESSING_REQUIRED from "
1692 "ntlmssp_client_update\n");
1693 data_blob_free(&request
);
1694 ntlmssp_end(&client_ntlmssp_state
);
1698 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1699 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1700 spnego
.negTokenTarg
.supportedMech
= (char *)OID_NTLMSSP
;
1701 spnego
.negTokenTarg
.responseToken
= request
;
1702 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1704 spnego_write_data(ctx
, &to_server
, &spnego
);
1705 data_blob_free(&request
);
1707 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1708 data_blob_free(&to_server
);
1709 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1710 TALLOC_FREE(to_server_base64
);
1716 static bool manage_client_krb5_init(struct spnego_data spnego
)
1719 DATA_BLOB tkt
, to_server
;
1720 DATA_BLOB session_key_krb5
= data_blob_null
;
1721 struct spnego_data reply
;
1725 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1727 TALLOC_CTX
*ctx
= talloc_tos();
1729 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1730 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1731 DEBUG(1, ("Did not get a principal for krb5\n"));
1735 principal
= (char *)SMB_MALLOC(
1736 spnego
.negTokenInit
.mechListMIC
.length
+1);
1738 if (principal
== NULL
) {
1739 DEBUG(1, ("Could not malloc principal\n"));
1743 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1744 spnego
.negTokenInit
.mechListMIC
.length
);
1745 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1747 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
, 0, NULL
, NULL
, NULL
);
1752 /* Let's try to first get the TGT, for that we need a
1755 if (opt_password
== NULL
) {
1756 DEBUG(10, ("Requesting password\n"));
1757 x_fprintf(x_stdout
, "PW\n");
1761 user
= talloc_asprintf(talloc_tos(), "%s@%s", opt_username
, opt_domain
);
1766 if ((retval
= kerberos_kinit_password(user
, opt_password
, 0, NULL
))) {
1767 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval
)));
1771 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
, 0, NULL
, NULL
, NULL
);
1774 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1779 data_blob_free(&session_key_krb5
);
1783 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1784 reply
.negTokenInit
.mechTypes
= my_mechs
;
1785 reply
.negTokenInit
.reqFlags
= data_blob_null
;
1786 reply
.negTokenInit
.reqFlagsPadding
= 0;
1787 reply
.negTokenInit
.mechToken
= tkt
;
1788 reply
.negTokenInit
.mechListMIC
= data_blob_null
;
1790 len
= spnego_write_data(ctx
, &to_server
, &reply
);
1791 data_blob_free(&tkt
);
1794 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1798 reply_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1799 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1801 TALLOC_FREE(reply_base64
);
1802 data_blob_free(&to_server
);
1803 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1807 static void manage_client_krb5_targ(struct spnego_data spnego
)
1809 switch (spnego
.negTokenTarg
.negResult
) {
1810 case SPNEGO_ACCEPT_INCOMPLETE
:
1811 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1812 x_fprintf(x_stdout
, "BH Got a Kerberos negTokenTarg with "
1813 "ACCEPT_INCOMPLETE\n");
1815 case SPNEGO_ACCEPT_COMPLETED
:
1816 DEBUG(10, ("Accept completed\n"));
1817 x_fprintf(x_stdout
, "AF\n");
1820 DEBUG(10, ("Rejected\n"));
1821 x_fprintf(x_stdout
, "NA\n");
1824 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1825 x_fprintf(x_stdout
, "AF\n");
1831 static void manage_gss_spnego_client_request(struct ntlm_auth_state
*state
,
1832 char *buf
, int length
)
1835 struct spnego_data spnego
;
1837 TALLOC_CTX
*ctx
= talloc_tos();
1839 if (!opt_username
|| !*opt_username
) {
1840 x_fprintf(x_stderr
, "username must be specified!\n\n");
1844 if (strlen(buf
) <= 3) {
1845 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1846 x_fprintf(x_stdout
, "BH SPNEGO query too short\n");
1850 request
= base64_decode_data_blob(buf
+3);
1852 if (strncmp(buf
, "PW ", 3) == 0) {
1854 /* We asked for a password and obviously got it :-) */
1856 opt_password
= SMB_STRNDUP((const char *)request
.data
, request
.length
);
1858 if (opt_password
== NULL
) {
1859 DEBUG(1, ("Out of memory\n"));
1860 x_fprintf(x_stdout
, "BH Out of memory\n");
1861 data_blob_free(&request
);
1865 x_fprintf(x_stdout
, "OK\n");
1866 data_blob_free(&request
);
1870 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1871 (strncmp(buf
, "AF ", 3) != 0) &&
1872 (strncmp(buf
, "NA ", 3) != 0) ) {
1873 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1874 x_fprintf(x_stdout
, "BH SPNEGO request invalid\n");
1875 data_blob_free(&request
);
1879 /* So we got a server challenge to generate a SPNEGO
1880 client-to-server request... */
1882 len
= spnego_read_data(ctx
, request
, &spnego
);
1883 data_blob_free(&request
);
1886 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1887 x_fprintf(x_stdout
, "BH Could not read SPNEGO data\n");
1891 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1893 /* The server offers a list of mechanisms */
1895 const char **mechType
= (const char **)spnego
.negTokenInit
.mechTypes
;
1897 while (*mechType
!= NULL
) {
1900 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1901 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1902 if (manage_client_krb5_init(spnego
))
1907 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1908 if (manage_client_ntlmssp_init(spnego
))
1915 DEBUG(1, ("Server offered no compatible mechanism\n"));
1916 x_fprintf(x_stdout
, "BH Server offered no compatible mechanism\n");
1920 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1922 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1923 /* On accept/reject Windows does not send the
1924 mechanism anymore. Handle that here and
1925 shut down the mechanisms. */
1927 switch (spnego
.negTokenTarg
.negResult
) {
1928 case SPNEGO_ACCEPT_COMPLETED
:
1929 x_fprintf(x_stdout
, "AF\n");
1932 x_fprintf(x_stdout
, "NA\n");
1935 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1936 "unknown negResult: %d\n",
1937 spnego
.negTokenTarg
.negResult
));
1938 x_fprintf(x_stdout
, "BH Got a negTokenTarg with"
1939 " no mech and an unknown "
1943 ntlmssp_end(&client_ntlmssp_state
);
1947 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1948 OID_NTLMSSP
) == 0) {
1949 manage_client_ntlmssp_targ(spnego
);
1954 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1955 OID_KERBEROS5_OLD
) == 0) {
1956 manage_client_krb5_targ(spnego
);
1963 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1964 x_fprintf(x_stdout
, "BH Got an SPNEGO token I could not handle\n");
1968 spnego_free_data(&spnego
);
1972 static void manage_ntlm_server_1_request(struct ntlm_auth_state
*state
,
1973 char *buf
, int length
)
1975 char *request
, *parameter
;
1976 static DATA_BLOB challenge
;
1977 static DATA_BLOB lm_response
;
1978 static DATA_BLOB nt_response
;
1979 static char *full_username
;
1980 static char *username
;
1981 static char *domain
;
1982 static char *plaintext_password
;
1983 static bool ntlm_server_1_user_session_key
;
1984 static bool ntlm_server_1_lm_session_key
;
1986 if (strequal(buf
, ".")) {
1987 if (!full_username
&& !username
) {
1988 x_fprintf(x_stdout
, "Error: No username supplied!\n");
1989 } else if (plaintext_password
) {
1990 /* handle this request as plaintext */
1991 if (!full_username
) {
1992 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
1993 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
1997 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
1998 x_fprintf(x_stdout
, "Authenticated: Yes\n");
2000 x_fprintf(x_stdout
, "Authenticated: No\n");
2002 } else if (!lm_response
.data
&& !nt_response
.data
) {
2003 x_fprintf(x_stdout
, "Error: No password supplied!\n");
2004 } else if (!challenge
.data
) {
2005 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
2007 char *error_string
= NULL
;
2009 uchar user_session_key
[16];
2012 if (full_username
&& !username
) {
2014 fstring fstr_domain
;
2016 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
2017 /* username might be 'tainted', don't print into our new-line deleimianted stream */
2018 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
2020 SAFE_FREE(username
);
2022 username
= smb_xstrdup(fstr_user
);
2023 domain
= smb_xstrdup(fstr_domain
);
2027 domain
= smb_xstrdup(get_winbind_domain());
2030 if (ntlm_server_1_lm_session_key
)
2031 flags
|= WBFLAG_PAM_LMKEY
;
2033 if (ntlm_server_1_user_session_key
)
2034 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2036 if (!NT_STATUS_IS_OK(
2037 contact_winbind_auth_crap(username
,
2049 x_fprintf(x_stdout
, "Authenticated: No\n");
2050 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
2052 static char zeros
[16];
2054 char *hex_user_session_key
;
2056 x_fprintf(x_stdout
, "Authenticated: Yes\n");
2058 if (ntlm_server_1_lm_session_key
2059 && (memcmp(zeros
, lm_key
,
2060 sizeof(lm_key
)) != 0)) {
2061 hex_lm_key
= hex_encode_talloc(NULL
,
2062 (const unsigned char *)lm_key
,
2064 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
2065 TALLOC_FREE(hex_lm_key
);
2068 if (ntlm_server_1_user_session_key
2069 && (memcmp(zeros
, user_session_key
,
2070 sizeof(user_session_key
)) != 0)) {
2071 hex_user_session_key
= hex_encode_talloc(NULL
,
2072 (const unsigned char *)user_session_key
,
2073 sizeof(user_session_key
));
2074 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
2075 TALLOC_FREE(hex_user_session_key
);
2078 SAFE_FREE(error_string
);
2080 /* clear out the state */
2081 challenge
= data_blob_null
;
2082 nt_response
= data_blob_null
;
2083 lm_response
= data_blob_null
;
2084 SAFE_FREE(full_username
);
2085 SAFE_FREE(username
);
2087 SAFE_FREE(plaintext_password
);
2088 ntlm_server_1_user_session_key
= False
;
2089 ntlm_server_1_lm_session_key
= False
;
2090 x_fprintf(x_stdout
, ".\n");
2097 /* Indicates a base64 encoded structure */
2098 parameter
= strstr_m(request
, ":: ");
2100 parameter
= strstr_m(request
, ": ");
2103 DEBUG(0, ("Parameter not found!\n"));
2104 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2121 base64_decode_inplace(parameter
);
2124 if (strequal(request
, "LANMAN-Challenge")) {
2125 challenge
= strhex_to_data_blob(NULL
, parameter
);
2126 if (challenge
.length
!= 8) {
2127 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
2129 (int)challenge
.length
);
2130 challenge
= data_blob_null
;
2132 } else if (strequal(request
, "NT-Response")) {
2133 nt_response
= strhex_to_data_blob(NULL
, parameter
);
2134 if (nt_response
.length
< 24) {
2135 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
2137 (int)nt_response
.length
);
2138 nt_response
= data_blob_null
;
2140 } else if (strequal(request
, "LANMAN-Response")) {
2141 lm_response
= strhex_to_data_blob(NULL
, parameter
);
2142 if (lm_response
.length
!= 24) {
2143 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2145 (int)lm_response
.length
);
2146 lm_response
= data_blob_null
;
2148 } else if (strequal(request
, "Password")) {
2149 plaintext_password
= smb_xstrdup(parameter
);
2150 } else if (strequal(request
, "NT-Domain")) {
2151 domain
= smb_xstrdup(parameter
);
2152 } else if (strequal(request
, "Username")) {
2153 username
= smb_xstrdup(parameter
);
2154 } else if (strequal(request
, "Full-Username")) {
2155 full_username
= smb_xstrdup(parameter
);
2156 } else if (strequal(request
, "Request-User-Session-Key")) {
2157 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
2158 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
2159 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
2161 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2165 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
2166 char *buf
, int length
)
2168 char *request
, *parameter
;
2169 static DATA_BLOB new_nt_pswd
;
2170 static DATA_BLOB old_nt_hash_enc
;
2171 static DATA_BLOB new_lm_pswd
;
2172 static DATA_BLOB old_lm_hash_enc
;
2173 static char *full_username
= NULL
;
2174 static char *username
= NULL
;
2175 static char *domain
= NULL
;
2176 static char *newpswd
= NULL
;
2177 static char *oldpswd
= NULL
;
2179 if (strequal(buf
, ".")) {
2180 if(newpswd
&& oldpswd
) {
2181 uchar old_nt_hash
[16];
2182 uchar old_lm_hash
[16];
2183 uchar new_nt_hash
[16];
2184 uchar new_lm_hash
[16];
2186 new_nt_pswd
= data_blob(NULL
, 516);
2187 old_nt_hash_enc
= data_blob(NULL
, 16);
2189 /* Calculate the MD4 hash (NT compatible) of the
2191 E_md4hash(oldpswd
, old_nt_hash
);
2192 E_md4hash(newpswd
, new_nt_hash
);
2194 /* E_deshash returns false for 'long'
2195 passwords (> 14 DOS chars).
2197 Therefore, don't send a buffer
2198 encrypted with the truncated hash
2199 (it could allow an even easier
2200 attack on the password)
2202 Likewise, obey the admin's restriction
2205 if (lp_client_lanman_auth() &&
2206 E_deshash(newpswd
, new_lm_hash
) &&
2207 E_deshash(oldpswd
, old_lm_hash
)) {
2208 new_lm_pswd
= data_blob(NULL
, 516);
2209 old_lm_hash_enc
= data_blob(NULL
, 16);
2210 encode_pw_buffer(new_lm_pswd
.data
, newpswd
,
2213 arcfour_crypt(new_lm_pswd
.data
, old_nt_hash
, 516);
2214 E_old_pw_hash(new_nt_hash
, old_lm_hash
,
2215 old_lm_hash_enc
.data
);
2217 new_lm_pswd
.data
= NULL
;
2218 new_lm_pswd
.length
= 0;
2219 old_lm_hash_enc
.data
= NULL
;
2220 old_lm_hash_enc
.length
= 0;
2223 encode_pw_buffer(new_nt_pswd
.data
, newpswd
,
2226 arcfour_crypt(new_nt_pswd
.data
, old_nt_hash
, 516);
2227 E_old_pw_hash(new_nt_hash
, old_nt_hash
,
2228 old_nt_hash_enc
.data
);
2231 if (!full_username
&& !username
) {
2232 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2233 } else if ((!new_nt_pswd
.data
|| !old_nt_hash_enc
.data
) &&
2234 (!new_lm_pswd
.data
|| old_lm_hash_enc
.data
) ) {
2235 x_fprintf(x_stdout
, "Error: No NT or LM password "
2236 "blobs supplied!\n");
2238 char *error_string
= NULL
;
2240 if (full_username
&& !username
) {
2242 fstring fstr_domain
;
2244 if (!parse_ntlm_auth_domain_user(full_username
,
2247 /* username might be 'tainted', don't
2248 * print into our new-line
2249 * deleimianted stream */
2250 x_fprintf(x_stdout
, "Error: Could not "
2251 "parse into domain and "
2253 SAFE_FREE(username
);
2254 username
= smb_xstrdup(full_username
);
2256 SAFE_FREE(username
);
2258 username
= smb_xstrdup(fstr_user
);
2259 domain
= smb_xstrdup(fstr_domain
);
2264 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2271 x_fprintf(x_stdout
, "Password-Change: No\n");
2272 x_fprintf(x_stdout
, "Password-Change-Error: "
2273 "%s\n.\n", error_string
);
2275 x_fprintf(x_stdout
, "Password-Change: Yes\n");
2278 SAFE_FREE(error_string
);
2280 /* clear out the state */
2281 new_nt_pswd
= data_blob_null
;
2282 old_nt_hash_enc
= data_blob_null
;
2283 new_lm_pswd
= data_blob_null
;
2284 old_nt_hash_enc
= data_blob_null
;
2285 SAFE_FREE(full_username
);
2286 SAFE_FREE(username
);
2290 x_fprintf(x_stdout
, ".\n");
2297 /* Indicates a base64 encoded structure */
2298 parameter
= strstr_m(request
, ":: ");
2300 parameter
= strstr_m(request
, ": ");
2303 DEBUG(0, ("Parameter not found!\n"));
2304 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2320 base64_decode_inplace(parameter
);
2323 if (strequal(request
, "new-nt-password-blob")) {
2324 new_nt_pswd
= strhex_to_data_blob(NULL
, parameter
);
2325 if (new_nt_pswd
.length
!= 516) {
2326 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2327 "(got %d bytes, expected 516)\n.\n",
2329 (int)new_nt_pswd
.length
);
2330 new_nt_pswd
= data_blob_null
;
2332 } else if (strequal(request
, "old-nt-hash-blob")) {
2333 old_nt_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2334 if (old_nt_hash_enc
.length
!= 16) {
2335 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2336 "(got %d bytes, expected 16)\n.\n",
2338 (int)old_nt_hash_enc
.length
);
2339 old_nt_hash_enc
= data_blob_null
;
2341 } else if (strequal(request
, "new-lm-password-blob")) {
2342 new_lm_pswd
= strhex_to_data_blob(NULL
, parameter
);
2343 if (new_lm_pswd
.length
!= 516) {
2344 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2345 "(got %d bytes, expected 516)\n.\n",
2347 (int)new_lm_pswd
.length
);
2348 new_lm_pswd
= data_blob_null
;
2351 else if (strequal(request
, "old-lm-hash-blob")) {
2352 old_lm_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2353 if (old_lm_hash_enc
.length
!= 16)
2355 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2356 "(got %d bytes, expected 16)\n.\n",
2358 (int)old_lm_hash_enc
.length
);
2359 old_lm_hash_enc
= data_blob_null
;
2361 } else if (strequal(request
, "nt-domain")) {
2362 domain
= smb_xstrdup(parameter
);
2363 } else if(strequal(request
, "username")) {
2364 username
= smb_xstrdup(parameter
);
2365 } else if(strequal(request
, "full-username")) {
2366 username
= smb_xstrdup(parameter
);
2367 } else if(strequal(request
, "new-password")) {
2368 newpswd
= smb_xstrdup(parameter
);
2369 } else if (strequal(request
, "old-password")) {
2370 oldpswd
= smb_xstrdup(parameter
);
2372 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2376 static void manage_squid_request(struct ntlm_auth_state
*state
,
2377 stdio_helper_function fn
)
2380 char tmp
[INITIAL_BUFFER_SIZE
+1];
2381 int length
, buf_size
= 0;
2384 buf
= talloc_strdup(state
->mem_ctx
, "");
2386 DEBUG(0, ("Failed to allocate input buffer.\n"));
2387 x_fprintf(x_stderr
, "ERR\n");
2393 /* this is not a typo - x_fgets doesn't work too well under
2395 if (fgets(tmp
, sizeof(tmp
)-1, stdin
) == NULL
) {
2396 if (ferror(stdin
)) {
2397 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2398 "(%s)\n", ferror(stdin
),
2399 strerror(ferror(stdin
))));
2406 buf
= talloc_strdup_append_buffer(buf
, tmp
);
2407 buf_size
+= INITIAL_BUFFER_SIZE
;
2409 if (buf_size
> MAX_BUFFER_SIZE
) {
2410 DEBUG(2, ("Oversized message\n"));
2411 x_fprintf(x_stderr
, "ERR\n");
2416 c
= strchr(buf
, '\n');
2417 } while (c
== NULL
);
2422 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
2424 if (buf
[0] == '\0') {
2425 DEBUG(2, ("Invalid Request\n"));
2426 x_fprintf(x_stderr
, "ERR\n");
2431 fn(state
, buf
, length
);
2436 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
2437 TALLOC_CTX
*mem_ctx
;
2438 struct ntlm_auth_state
*state
;
2440 /* initialize FDescs */
2441 x_setbuf(x_stdout
, NULL
);
2442 x_setbuf(x_stderr
, NULL
);
2444 mem_ctx
= talloc_init("ntlm_auth");
2446 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2447 x_fprintf(x_stderr
, "ERR\n");
2451 state
= talloc_zero(mem_ctx
, struct ntlm_auth_state
);
2453 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2454 x_fprintf(x_stderr
, "ERR\n");
2458 state
->mem_ctx
= mem_ctx
;
2459 state
->helper_mode
= stdio_mode
;
2462 TALLOC_CTX
*frame
= talloc_stackframe();
2463 manage_squid_request(state
, fn
);
2469 /* Authenticate a user with a challenge/response */
2471 static bool check_auth_crap(void)
2476 char user_session_key
[16];
2478 char *hex_user_session_key
;
2480 static uint8 zeros
[16];
2482 x_setbuf(x_stdout
, NULL
);
2485 flags
|= WBFLAG_PAM_LMKEY
;
2487 if (request_user_session_key
)
2488 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2490 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
2492 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
2498 (unsigned char *)lm_key
,
2499 (unsigned char *)user_session_key
,
2500 &error_string
, NULL
);
2502 if (!NT_STATUS_IS_OK(nt_status
)) {
2503 x_fprintf(x_stdout
, "%s (0x%x)\n",
2505 NT_STATUS_V(nt_status
));
2506 SAFE_FREE(error_string
);
2511 && (memcmp(zeros
, lm_key
,
2512 sizeof(lm_key
)) != 0)) {
2513 hex_lm_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key
,
2515 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
2516 TALLOC_FREE(hex_lm_key
);
2518 if (request_user_session_key
2519 && (memcmp(zeros
, user_session_key
,
2520 sizeof(user_session_key
)) != 0)) {
2521 hex_user_session_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key
,
2522 sizeof(user_session_key
));
2523 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
2524 TALLOC_FREE(hex_user_session_key
);
2533 OPT_USERNAME
= 1000,
2542 OPT_USER_SESSION_KEY
,
2544 OPT_REQUIRE_MEMBERSHIP
,
2545 OPT_USE_CACHED_CREDS
,
2546 OPT_PAM_WINBIND_CONF
2549 int main(int argc
, const char **argv
)
2551 TALLOC_CTX
*frame
= talloc_stackframe();
2553 static const char *helper_protocol
;
2554 static int diagnostics
;
2556 static const char *hex_challenge
;
2557 static const char *hex_lm_response
;
2558 static const char *hex_nt_response
;
2562 /* NOTE: DO NOT change this interface without considering the implications!
2563 This is an external interface, which other programs will use to interact
2567 /* We do not use single-letter command abbreviations, because they harm future
2568 interface stability. */
2570 struct poptOption long_options
[] = {
2572 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
2573 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
2574 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
2575 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
2576 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
2577 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
2578 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2579 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
2580 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retrieve LM session key"},
2581 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retrieve User (NT) session key"},
2582 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "Use cached credentials if no password is given"},
2583 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
, OPT_DIAGNOSTICS
, "Perform diagnostics on the authentictaion chain"},
2584 { "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" },
2585 { "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" },
2586 POPT_COMMON_CONFIGFILE
2591 /* Samba client initialisation */
2598 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
2600 /* Parse command line options */
2603 poptPrintHelp(pc
, stderr
, 0);
2607 while((opt
= poptGetNextOpt(pc
)) != -1) {
2608 /* Get generic config options like --configfile */
2611 poptFreeContext(pc
);
2613 if (!lp_load(get_dyn_CONFIGFILE(), True
, False
, False
, True
)) {
2614 d_fprintf(stderr
, "ntlm_auth: error opening config file %s. Error was %s\n",
2615 get_dyn_CONFIGFILE(), strerror(errno
));
2619 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2620 POPT_CONTEXT_KEEP_FIRST
);
2622 while((opt
= poptGetNextOpt(pc
)) != -1) {
2625 opt_challenge
= strhex_to_data_blob(NULL
, hex_challenge
);
2626 if (opt_challenge
.length
!= 8) {
2627 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2629 (int)opt_challenge
.length
);
2634 opt_lm_response
= strhex_to_data_blob(NULL
, hex_lm_response
);
2635 if (opt_lm_response
.length
!= 24) {
2636 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2638 (int)opt_lm_response
.length
);
2644 opt_nt_response
= strhex_to_data_blob(NULL
, hex_nt_response
);
2645 if (opt_nt_response
.length
< 24) {
2646 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2648 (int)opt_nt_response
.length
);
2653 case OPT_REQUIRE_MEMBERSHIP
:
2654 if (StrnCaseCmp("S-", require_membership_of
, 2) == 0) {
2655 require_membership_of_sid
= require_membership_of
;
2662 char *domain
= SMB_STRDUP(opt_username
);
2663 char *p
= strchr_m(domain
, *lp_winbind_separator());
2667 if (opt_domain
&& !strequal(opt_domain
, domain
)) {
2668 x_fprintf(x_stderr
, "Domain specified in username (%s) "
2669 "doesn't match specified domain (%s)!\n\n",
2670 domain
, opt_domain
);
2671 poptPrintHelp(pc
, stderr
, 0);
2674 opt_domain
= domain
;
2680 /* Note: if opt_domain is "" then send no domain */
2681 if (opt_domain
== NULL
) {
2682 opt_domain
= get_winbind_domain();
2685 if (opt_workstation
== NULL
) {
2686 opt_workstation
= "";
2689 if (helper_protocol
) {
2691 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2692 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
2693 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
2697 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
2699 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2700 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
2706 if (!opt_username
|| !*opt_username
) {
2707 x_fprintf(x_stderr
, "username must be specified!\n\n");
2708 poptPrintHelp(pc
, stderr
, 0);
2712 if (opt_challenge
.length
) {
2713 if (!check_auth_crap()) {
2719 if (!opt_password
) {
2720 opt_password
= getpass("password: ");
2724 if (!diagnose_ntlm_auth()) {
2730 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
2731 if (!check_plaintext_auth(user
, opt_password
, True
)) {
2738 poptFreeContext(pc
);