2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
9 Copyright (C) Robert O'Callahan 2006 (added cached credential code).
10 Copyright (C) Kai Blin <kai@samba.org> 2008
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "popt_common.h"
28 #include "utils/ntlm_auth.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "../libcli/auth/spnego.h"
31 #include "../libcli/auth/ntlmssp.h"
33 #include <iniparser.h>
34 #include "../lib/crypto/arcfour.h"
35 #include "libads/kerberos_proto.h"
36 #include "nsswitch/winbind_client.h"
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 global_myname();
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(CONST_DISCARD(char *, opt_pam_winbind_conf
));
322 if (iniparser_getboolean(d
, CONST_DISCARD(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
= global_myname();
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 /* Have we been given a local password, or should we ask winbind? */
742 (*ntlmssp_state
)->check_password
= local_pw_check
;
744 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
749 /*******************************************************************
750 Used by firefox to drive NTLM auth to IIS servers.
751 *******************************************************************/
753 static NTSTATUS
do_ccache_ntlm_auth(DATA_BLOB initial_msg
, DATA_BLOB challenge_msg
,
756 struct winbindd_request wb_request
;
757 struct winbindd_response wb_response
;
761 /* get winbindd to do the ntlmssp step on our behalf */
762 ZERO_STRUCT(wb_request
);
763 ZERO_STRUCT(wb_response
);
766 * This is tricky here. If we set krb5_auth in pam_winbind.conf
767 * creds for users in trusted domain will be stored the winbindd
768 * child of the trusted domain. If we ask the primary domain for
769 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
770 * domain's child for ccache_ntlm_auth. that is to say, we have to
771 * set WBFLAG_PAM_CONTACT_TRUSTDOM in request.flags.
773 ctrl
= get_pam_winbind_config();
775 if (ctrl
& WINBIND_KRB5_AUTH
) {
776 wb_request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
779 fstr_sprintf(wb_request
.data
.ccache_ntlm_auth
.user
,
780 "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
781 wb_request
.data
.ccache_ntlm_auth
.uid
= geteuid();
782 wb_request
.data
.ccache_ntlm_auth
.initial_blob_len
= initial_msg
.length
;
783 wb_request
.data
.ccache_ntlm_auth
.challenge_blob_len
= challenge_msg
.length
;
784 wb_request
.extra_len
= initial_msg
.length
+ challenge_msg
.length
;
786 if (wb_request
.extra_len
> 0) {
787 wb_request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, wb_request
.extra_len
);
788 if (wb_request
.extra_data
.data
== NULL
) {
789 return NT_STATUS_NO_MEMORY
;
792 memcpy(wb_request
.extra_data
.data
, initial_msg
.data
, initial_msg
.length
);
793 memcpy(wb_request
.extra_data
.data
+ initial_msg
.length
,
794 challenge_msg
.data
, challenge_msg
.length
);
797 result
= winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH
, &wb_request
, &wb_response
);
798 SAFE_FREE(wb_request
.extra_data
.data
);
800 if (result
!= NSS_STATUS_SUCCESS
) {
801 winbindd_free_response(&wb_response
);
802 return NT_STATUS_UNSUCCESSFUL
;
806 *reply
= data_blob(wb_response
.extra_data
.data
,
807 wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
);
808 if (wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
> 0 &&
809 reply
->data
== NULL
) {
810 winbindd_free_response(&wb_response
);
811 return NT_STATUS_NO_MEMORY
;
815 winbindd_free_response(&wb_response
);
816 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
819 static void manage_squid_ntlmssp_request_int(struct ntlm_auth_state
*state
,
820 char *buf
, int length
,
824 DATA_BLOB request
, reply
;
827 if (strlen(buf
) < 2) {
828 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
829 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
833 if (strlen(buf
) > 3) {
834 if(strncmp(buf
, "SF ", 3) == 0){
835 DEBUG(10, ("Setting flags to negotioate\n"));
836 TALLOC_FREE(state
->want_feature_list
);
837 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
839 *response
= talloc_strdup(mem_ctx
, "OK");
842 request
= base64_decode_data_blob(buf
+ 3);
844 request
= data_blob_null
;
847 if ((strncmp(buf
, "PW ", 3) == 0)) {
848 /* The calling application wants us to use a local password
849 * (rather than winbindd) */
851 opt_password
= SMB_STRNDUP((const char *)request
.data
,
854 if (opt_password
== NULL
) {
855 DEBUG(1, ("Out of memory\n"));
856 *response
= talloc_strdup(mem_ctx
, "BH Out of memory");
857 data_blob_free(&request
);
861 *response
= talloc_strdup(mem_ctx
, "OK");
862 data_blob_free(&request
);
866 if (strncmp(buf
, "YR", 2) == 0) {
867 TALLOC_FREE(state
->ntlmssp_state
);
868 state
->svr_state
= SERVER_INITIAL
;
869 } else if (strncmp(buf
, "KK", 2) == 0) {
870 /* No special preprocessing required */
871 } else if (strncmp(buf
, "GF", 2) == 0) {
872 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
874 if (state
->svr_state
== SERVER_FINISHED
) {
875 *response
= talloc_asprintf(mem_ctx
, "GF 0x%08x",
879 *response
= talloc_strdup(mem_ctx
, "BH\n");
881 data_blob_free(&request
);
883 } else if (strncmp(buf
, "GK", 2) == 0) {
884 DEBUG(10, ("Requested NTLMSSP session key\n"));
885 if(state
->have_session_key
) {
886 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
888 *response
= talloc_asprintf(mem_ctx
, "GK %s",
889 key64
? key64
: "<NULL>");
892 *response
= talloc_strdup(mem_ctx
, "BH");
895 data_blob_free(&request
);
898 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
899 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
903 if (!state
->ntlmssp_state
) {
904 nt_status
= ntlm_auth_start_ntlmssp_server(
905 &state
->ntlmssp_state
);
906 if (!NT_STATUS_IS_OK(nt_status
)) {
907 *response
= talloc_asprintf(
908 mem_ctx
, "BH %s", nt_errstr(nt_status
));
911 ntlmssp_want_feature_list(state
->ntlmssp_state
,
912 state
->want_feature_list
);
915 DEBUG(10, ("got NTLMSSP packet:\n"));
916 dump_data(10, request
.data
, request
.length
);
918 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
, &reply
);
920 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
921 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
923 *response
= talloc_asprintf(mem_ctx
, "TT %s", reply_base64
);
924 TALLOC_FREE(reply_base64
);
925 data_blob_free(&reply
);
926 state
->svr_state
= SERVER_CHALLENGE
;
927 DEBUG(10, ("NTLMSSP challenge\n"));
928 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
929 *response
= talloc_asprintf(mem_ctx
, "BH %s",
930 nt_errstr(nt_status
));
931 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
933 TALLOC_FREE(state
->ntlmssp_state
);
934 } else if (!NT_STATUS_IS_OK(nt_status
)) {
935 *response
= talloc_asprintf(mem_ctx
, "NA %s",
936 nt_errstr(nt_status
));
937 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
939 *response
= talloc_asprintf(
941 (char *)state
->ntlmssp_state
->callback_private
);
942 DEBUG(10, ("NTLMSSP OK!\n"));
944 if(state
->have_session_key
)
945 data_blob_free(&state
->session_key
);
946 state
->session_key
= data_blob(
947 state
->ntlmssp_state
->session_key
.data
,
948 state
->ntlmssp_state
->session_key
.length
);
949 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
950 state
->have_session_key
= true;
951 state
->svr_state
= SERVER_FINISHED
;
954 data_blob_free(&request
);
957 static void manage_squid_ntlmssp_request(struct ntlm_auth_state
*state
,
958 char *buf
, int length
)
962 manage_squid_ntlmssp_request_int(state
, buf
, length
,
963 talloc_tos(), &response
);
965 if (response
== NULL
) {
966 x_fprintf(x_stdout
, "BH Out of memory\n");
969 x_fprintf(x_stdout
, "%s\n", response
);
970 TALLOC_FREE(response
);
973 static void manage_client_ntlmssp_request(struct ntlm_auth_state
*state
,
974 char *buf
, int length
)
976 DATA_BLOB request
, reply
;
979 if (!opt_username
|| !*opt_username
) {
980 x_fprintf(x_stderr
, "username must be specified!\n\n");
984 if (strlen(buf
) < 2) {
985 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
986 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
990 if (strlen(buf
) > 3) {
991 if(strncmp(buf
, "SF ", 3) == 0) {
992 DEBUG(10, ("Looking for flags to negotiate\n"));
993 talloc_free(state
->want_feature_list
);
994 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
996 x_fprintf(x_stdout
, "OK\n");
999 request
= base64_decode_data_blob(buf
+ 3);
1001 request
= data_blob_null
;
1004 if (strncmp(buf
, "PW ", 3) == 0) {
1005 /* We asked for a password and obviously got it :-) */
1007 opt_password
= SMB_STRNDUP((const char *)request
.data
,
1010 if (opt_password
== NULL
) {
1011 DEBUG(1, ("Out of memory\n"));
1012 x_fprintf(x_stdout
, "BH Out of memory\n");
1013 data_blob_free(&request
);
1017 x_fprintf(x_stdout
, "OK\n");
1018 data_blob_free(&request
);
1022 if (!state
->ntlmssp_state
&& use_cached_creds
) {
1023 /* check whether cached credentials are usable. */
1024 DATA_BLOB empty_blob
= data_blob_null
;
1026 nt_status
= do_ccache_ntlm_auth(empty_blob
, empty_blob
, NULL
);
1027 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1028 /* failed to use cached creds */
1029 use_cached_creds
= False
;
1033 if (opt_password
== NULL
&& !use_cached_creds
) {
1034 /* Request a password from the calling process. After
1035 sending it, the calling process should retry asking for the
1038 DEBUG(10, ("Requesting password\n"));
1039 x_fprintf(x_stdout
, "PW\n");
1043 if (strncmp(buf
, "YR", 2) == 0) {
1044 TALLOC_FREE(state
->ntlmssp_state
);
1045 state
->cli_state
= CLIENT_INITIAL
;
1046 } else if (strncmp(buf
, "TT", 2) == 0) {
1047 /* No special preprocessing required */
1048 } else if (strncmp(buf
, "GF", 2) == 0) {
1049 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1051 if(state
->cli_state
== CLIENT_FINISHED
) {
1052 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
1055 x_fprintf(x_stdout
, "BH\n");
1058 data_blob_free(&request
);
1060 } else if (strncmp(buf
, "GK", 2) == 0 ) {
1061 DEBUG(10, ("Requested session key\n"));
1063 if(state
->cli_state
== CLIENT_FINISHED
) {
1064 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
1065 state
->session_key
);
1066 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
1070 x_fprintf(x_stdout
, "BH\n");
1073 data_blob_free(&request
);
1076 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
1077 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
1081 if (!state
->ntlmssp_state
) {
1082 nt_status
= ntlm_auth_start_ntlmssp_client(
1083 &state
->ntlmssp_state
);
1084 if (!NT_STATUS_IS_OK(nt_status
)) {
1085 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1088 ntlmssp_want_feature_list(state
->ntlmssp_state
,
1089 state
->want_feature_list
);
1090 state
->initial_message
= data_blob_null
;
1093 DEBUG(10, ("got NTLMSSP packet:\n"));
1094 dump_data(10, request
.data
, request
.length
);
1096 if (use_cached_creds
&& !opt_password
&&
1097 (state
->cli_state
== CLIENT_RESPONSE
)) {
1098 nt_status
= do_ccache_ntlm_auth(state
->initial_message
, request
,
1101 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
,
1105 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1106 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
1108 if (state
->cli_state
== CLIENT_INITIAL
) {
1109 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
1110 state
->initial_message
= reply
;
1111 state
->cli_state
= CLIENT_RESPONSE
;
1113 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
1114 data_blob_free(&reply
);
1116 TALLOC_FREE(reply_base64
);
1117 DEBUG(10, ("NTLMSSP challenge\n"));
1118 } else if (NT_STATUS_IS_OK(nt_status
)) {
1119 char *reply_base64
= base64_encode_data_blob(talloc_tos(),
1121 x_fprintf(x_stdout
, "AF %s\n", reply_base64
);
1122 TALLOC_FREE(reply_base64
);
1124 if(state
->have_session_key
)
1125 data_blob_free(&state
->session_key
);
1127 state
->session_key
= data_blob(
1128 state
->ntlmssp_state
->session_key
.data
,
1129 state
->ntlmssp_state
->session_key
.length
);
1130 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
1131 state
->have_session_key
= true;
1133 DEBUG(10, ("NTLMSSP OK!\n"));
1134 state
->cli_state
= CLIENT_FINISHED
;
1135 TALLOC_FREE(state
->ntlmssp_state
);
1137 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1138 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
1139 state
->cli_state
= CLIENT_ERROR
;
1140 TALLOC_FREE(state
->ntlmssp_state
);
1143 data_blob_free(&request
);
1146 static void manage_squid_basic_request(struct ntlm_auth_state
*state
,
1147 char *buf
, int length
)
1152 pass
=(char *)memchr(buf
,' ',length
);
1154 DEBUG(2, ("Password not found. Denying access\n"));
1155 x_fprintf(x_stdout
, "ERR\n");
1161 if (state
->helper_mode
== SQUID_2_5_BASIC
) {
1162 rfc1738_unescape(user
);
1163 rfc1738_unescape(pass
);
1166 if (check_plaintext_auth(user
, pass
, False
)) {
1167 x_fprintf(x_stdout
, "OK\n");
1169 x_fprintf(x_stdout
, "ERR\n");
1173 static void offer_gss_spnego_mechs(void) {
1176 struct spnego_data spnego
;
1179 TALLOC_CTX
*ctx
= talloc_tos();
1183 ZERO_STRUCT(spnego
);
1185 myname_lower
= talloc_strdup(ctx
, global_myname());
1186 if (!myname_lower
) {
1189 strlower_m(myname_lower
);
1191 principal
= talloc_asprintf(ctx
, "%s$@%s", myname_lower
, lp_realm());
1196 /* Server negTokenInit (mech offerings) */
1197 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1198 spnego
.negTokenInit
.mechTypes
= talloc_array(ctx
, const char *, 4);
1200 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_KERBEROS5_OLD
);
1201 spnego
.negTokenInit
.mechTypes
[1] = talloc_strdup(ctx
, OID_KERBEROS5
);
1202 spnego
.negTokenInit
.mechTypes
[2] = talloc_strdup(ctx
, OID_NTLMSSP
);
1203 spnego
.negTokenInit
.mechTypes
[3] = NULL
;
1205 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_NTLMSSP
);
1206 spnego
.negTokenInit
.mechTypes
[1] = NULL
;
1210 spnego
.negTokenInit
.mechListMIC
= data_blob_talloc(ctx
, principal
,
1213 len
= spnego_write_data(ctx
, &token
, &spnego
);
1214 spnego_free_data(&spnego
);
1217 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1218 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1222 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1223 x_fprintf(x_stdout
, "TT %s *\n", reply_base64
);
1225 TALLOC_FREE(reply_base64
);
1226 data_blob_free(&token
);
1227 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1231 bool spnego_parse_krb5_wrap(TALLOC_CTX
*ctx
, DATA_BLOB blob
, DATA_BLOB
*ticket
, uint8 tok_id
[2])
1237 data
= asn1_init(talloc_tos());
1242 asn1_load(data
, blob
);
1243 asn1_start_tag(data
, ASN1_APPLICATION(0));
1244 asn1_check_OID(data
, OID_KERBEROS5
);
1246 data_remaining
= asn1_tag_remaining(data
);
1248 if (data_remaining
< 3) {
1249 data
->has_error
= True
;
1251 asn1_read(data
, tok_id
, 2);
1252 data_remaining
-= 2;
1253 *ticket
= data_blob_talloc(ctx
, NULL
, data_remaining
);
1254 asn1_read(data
, ticket
->data
, ticket
->length
);
1259 ret
= !data
->has_error
;
1261 if (data
->has_error
) {
1262 data_blob_free(ticket
);
1270 static void manage_gss_spnego_request(struct ntlm_auth_state
*state
,
1271 char *buf
, int length
)
1273 struct spnego_data request
, response
;
1275 DATA_BLOB raw_in_token
= data_blob_null
;
1276 DATA_BLOB raw_out_token
= data_blob_null
;
1279 TALLOC_CTX
*ctx
= talloc_tos();
1282 char *domain
= NULL
;
1284 const char *reply_code
;
1286 char *reply_argument
= NULL
;
1287 char *supportedMech
= NULL
;
1289 if (strlen(buf
) < 2) {
1290 DEBUG(1, ("SPENGO query [%s] invalid\n", buf
));
1291 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1295 if (strncmp(buf
, "YR", 2) == 0) {
1296 TALLOC_FREE(state
->ntlmssp_state
);
1297 TALLOC_FREE(state
->spnego_mech
);
1298 TALLOC_FREE(state
->spnego_mech_oid
);
1299 } else if (strncmp(buf
, "KK", 2) == 0) {
1302 DEBUG(1, ("SPENGO query [%s] invalid\n", buf
));
1303 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1307 if ( (strlen(buf
) == 2)) {
1309 /* no client data, get the negTokenInit offering
1312 offer_gss_spnego_mechs();
1316 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1318 if (strlen(buf
) <= 3) {
1319 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1320 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1324 token
= base64_decode_data_blob(buf
+ 3);
1326 if ((token
.length
>= 7)
1327 && (strncmp((char *)token
.data
, "NTLMSSP", 7) == 0)) {
1330 data_blob_free(&token
);
1332 DEBUG(10, ("Could not parse GSS-SPNEGO, trying raw "
1335 manage_squid_ntlmssp_request_int(state
, buf
, length
,
1336 talloc_tos(), &reply
);
1337 if (reply
== NULL
) {
1338 x_fprintf(x_stdout
, "BH Out of memory\n");
1342 if (strncmp(reply
, "AF ", 3) == 0) {
1343 x_fprintf(x_stdout
, "AF * %s\n", reply
+3);
1345 x_fprintf(x_stdout
, "%s *\n", reply
);
1352 ZERO_STRUCT(request
);
1353 len
= spnego_read_data(ctx
, token
, &request
);
1354 data_blob_free(&token
);
1357 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1358 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1362 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1370 if (state
->spnego_mech
) {
1371 DEBUG(1, ("Client restarted SPNEGO with NegTokenInit "
1372 "while mech[%s] was already negotiated\n",
1373 state
->spnego_mech
));
1374 x_fprintf(x_stdout
, "BH Client send NegTokenInit twice\n");
1378 /* Second request from Client. This is where the
1379 client offers its mechanism to use. */
1381 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
1382 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
1383 DEBUG(1, ("Client did not offer any mechanism\n"));
1384 x_fprintf(x_stdout
, "BH Client did not offer any "
1389 status
= NT_STATUS_UNSUCCESSFUL
;
1390 for (i
= 0; request
.negTokenInit
.mechTypes
[i
] != NULL
; i
++) {
1391 DEBUG(10,("got mech[%d][%s]\n",
1392 i
, request
.negTokenInit
.mechTypes
[i
]));
1394 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5_OLD
) == 0) {
1398 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5
) == 0) {
1403 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_NTLMSSP
) == 0) {
1409 used_idx
= ntlm_idx
;
1411 if (krb5_idx
!= -1) {
1413 used_idx
= krb5_idx
;
1416 if (ntlm_idx
> -1) {
1417 state
->spnego_mech
= talloc_strdup(state
, "ntlmssp");
1418 if (state
->spnego_mech
== NULL
) {
1419 x_fprintf(x_stdout
, "BH Out of memory\n");
1423 if (state
->ntlmssp_state
) {
1424 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1425 "already got one\n"));
1426 x_fprintf(x_stdout
, "BH Client wants a new "
1427 "NTLMSSP challenge, but "
1428 "already got one\n");
1429 TALLOC_FREE(state
->ntlmssp_state
);
1433 status
= ntlm_auth_start_ntlmssp_server(&state
->ntlmssp_state
);
1434 if (!NT_STATUS_IS_OK(status
)) {
1435 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1441 if (krb5_idx
> -1) {
1442 state
->spnego_mech
= talloc_strdup(state
, "krb5");
1443 if (state
->spnego_mech
== NULL
) {
1444 x_fprintf(x_stdout
, "BH Out of memory\n");
1449 if (used_idx
> -1) {
1450 state
->spnego_mech_oid
= talloc_strdup(state
,
1451 request
.negTokenInit
.mechTypes
[used_idx
]);
1452 if (state
->spnego_mech_oid
== NULL
) {
1453 x_fprintf(x_stdout
, "BH Out of memory\n");
1456 supportedMech
= talloc_strdup(ctx
, state
->spnego_mech_oid
);
1457 if (supportedMech
== NULL
) {
1458 x_fprintf(x_stdout
, "BH Out of memory\n");
1462 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1464 status
= NT_STATUS_NOT_SUPPORTED
;
1466 if (used_idx
== 0) {
1467 status
= NT_STATUS_OK
;
1468 raw_in_token
= request
.negTokenInit
.mechToken
;
1471 if (state
->spnego_mech
== NULL
) {
1472 DEBUG(1,("Got netTokenTarg without negTokenInit\n"));
1473 x_fprintf(x_stdout
, "BH Got a negTokenTarg without "
1478 if ((request
.negTokenTarg
.supportedMech
!= NULL
) &&
1479 (strcmp(request
.negTokenTarg
.supportedMech
, state
->spnego_mech_oid
) != 0 ) ) {
1480 DEBUG(1, ("Got a negTokenTarg with mech[%s] while [%s] was already negotiated\n",
1481 request
.negTokenTarg
.supportedMech
,
1482 state
->spnego_mech_oid
));
1483 x_fprintf(x_stdout
, "BH Got a negTokenTarg with speficied mech\n");
1487 status
= NT_STATUS_OK
;
1488 raw_in_token
= request
.negTokenTarg
.responseToken
;
1491 if (!NT_STATUS_IS_OK(status
)) {
1492 /* error or more processing */
1493 } else if (strcmp(state
->spnego_mech
, "ntlmssp") == 0) {
1495 DEBUG(10, ("got NTLMSSP packet:\n"));
1496 dump_data(10, raw_in_token
.data
, raw_in_token
.length
);
1498 status
= ntlmssp_update(state
->ntlmssp_state
,
1501 if (NT_STATUS_IS_OK(status
)) {
1502 user
= talloc_strdup(ctx
, state
->ntlmssp_state
->user
);
1503 domain
= talloc_strdup(ctx
, state
->ntlmssp_state
->domain
);
1505 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1506 TALLOC_FREE(state
->ntlmssp_state
);
1509 } else if (strcmp(state
->spnego_mech
, "krb5") == 0) {
1512 DATA_BLOB session_key
;
1513 struct PAC_LOGON_INFO
*logon_info
= NULL
;
1517 if (!spnego_parse_krb5_wrap(ctx
, raw_in_token
,
1519 DEBUG(1, ("spnego_parse_krb5_wrap failed\n"));
1520 x_fprintf(x_stdout
, "BH spnego_parse_krb5_wrap failed\n");
1524 status
= ads_verify_ticket(ctx
, lp_realm(), 0,
1526 &principal
, &logon_info
, &ap_rep
,
1527 &session_key
, True
);
1529 /* Now in "principal" we have the name we are authenticated as. */
1531 if (NT_STATUS_IS_OK(status
)) {
1533 domain
= strchr_m(principal
, '@');
1535 if (domain
== NULL
) {
1536 DEBUG(1, ("Did not get a valid principal "
1537 "from ads_verify_ticket\n"));
1538 x_fprintf(x_stdout
, "BH Did not get a "
1539 "valid principal from "
1540 "ads_verify_ticket\n");
1545 domain
= talloc_strdup(ctx
, domain
);
1546 user
= talloc_strdup(ctx
, principal
);
1549 netsamlogon_cache_store(
1550 user
, &logon_info
->info3
);
1553 data_blob_free(&ap_rep
);
1554 data_blob_free(&session_key
);
1556 data_blob_free(&ticket
);
1560 spnego_free_data(&request
);
1561 ZERO_STRUCT(response
);
1562 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1564 if (NT_STATUS_IS_OK(status
)) {
1565 TALLOC_FREE(state
->spnego_mech
);
1566 TALLOC_FREE(state
->spnego_mech_oid
);
1567 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
1568 response
.negTokenTarg
.responseToken
= raw_out_token
;
1570 reply_argument
= talloc_asprintf(ctx
, "%s\\%s", domain
, user
);
1571 } else if (NT_STATUS_EQUAL(status
,
1572 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1573 response
.negTokenTarg
.supportedMech
= supportedMech
;
1574 response
.negTokenTarg
.responseToken
= raw_out_token
;
1575 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1577 reply_argument
= talloc_strdup(ctx
, "*");
1579 TALLOC_FREE(state
->spnego_mech
);
1580 TALLOC_FREE(state
->spnego_mech_oid
);
1581 data_blob_free(&raw_out_token
);
1582 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1584 reply_argument
= talloc_strdup(ctx
, nt_errstr(status
));
1587 if (!reply_argument
) {
1588 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1589 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1590 spnego_free_data(&response
);
1594 len
= spnego_write_data(ctx
, &token
, &response
);
1595 spnego_free_data(&response
);
1598 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1599 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1603 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1605 x_fprintf(x_stdout
, "%s %s %s\n",
1606 reply_code
, reply_base64
, reply_argument
);
1608 TALLOC_FREE(reply_base64
);
1609 data_blob_free(&token
);
1614 static struct ntlmssp_state
*client_ntlmssp_state
= NULL
;
1616 static bool manage_client_ntlmssp_init(struct spnego_data spnego
)
1619 DATA_BLOB null_blob
= data_blob_null
;
1620 DATA_BLOB to_server
;
1621 char *to_server_base64
;
1622 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1623 TALLOC_CTX
*ctx
= talloc_tos();
1625 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1627 if (client_ntlmssp_state
!= NULL
) {
1628 DEBUG(1, ("Request for initial SPNEGO request where "
1629 "we already have a state\n"));
1633 if (!client_ntlmssp_state
) {
1634 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1635 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1641 if (opt_password
== NULL
) {
1643 /* Request a password from the calling process. After
1644 sending it, the calling process should retry with
1645 the negTokenInit. */
1647 DEBUG(10, ("Requesting password\n"));
1648 x_fprintf(x_stdout
, "PW\n");
1652 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1653 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1654 spnego
.negTokenInit
.reqFlags
= data_blob_null
;
1655 spnego
.negTokenInit
.reqFlagsPadding
= 0;
1656 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1658 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1659 &spnego
.negTokenInit
.mechToken
);
1661 if ( !(NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) ||
1662 NT_STATUS_IS_OK(status
)) ) {
1663 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1664 nt_errstr(status
)));
1665 TALLOC_FREE(client_ntlmssp_state
);
1669 spnego_write_data(ctx
, &to_server
, &spnego
);
1670 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1672 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1673 data_blob_free(&to_server
);
1674 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1675 TALLOC_FREE(to_server_base64
);
1679 static void manage_client_ntlmssp_targ(struct spnego_data spnego
)
1682 DATA_BLOB null_blob
= data_blob_null
;
1684 DATA_BLOB to_server
;
1685 char *to_server_base64
;
1686 TALLOC_CTX
*ctx
= talloc_tos();
1688 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1690 if (client_ntlmssp_state
== NULL
) {
1691 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1692 x_fprintf(x_stdout
, "BH Got NTLMSSP tArg without a client state\n");
1696 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1697 x_fprintf(x_stdout
, "NA\n");
1698 TALLOC_FREE(client_ntlmssp_state
);
1702 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1703 x_fprintf(x_stdout
, "AF\n");
1704 TALLOC_FREE(client_ntlmssp_state
);
1708 status
= ntlmssp_update(client_ntlmssp_state
,
1709 spnego
.negTokenTarg
.responseToken
,
1712 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1713 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1714 "ntlmssp_client_update, got: %s\n",
1715 nt_errstr(status
)));
1716 x_fprintf(x_stdout
, "BH Expected MORE_PROCESSING_REQUIRED from "
1717 "ntlmssp_client_update\n");
1718 data_blob_free(&request
);
1719 TALLOC_FREE(client_ntlmssp_state
);
1723 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1724 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1725 spnego
.negTokenTarg
.supportedMech
= (char *)OID_NTLMSSP
;
1726 spnego
.negTokenTarg
.responseToken
= request
;
1727 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1729 spnego_write_data(ctx
, &to_server
, &spnego
);
1730 data_blob_free(&request
);
1732 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1733 data_blob_free(&to_server
);
1734 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1735 TALLOC_FREE(to_server_base64
);
1741 static bool manage_client_krb5_init(struct spnego_data spnego
)
1744 DATA_BLOB tkt
, to_server
;
1745 DATA_BLOB session_key_krb5
= data_blob_null
;
1746 struct spnego_data reply
;
1750 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1752 TALLOC_CTX
*ctx
= talloc_tos();
1754 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1755 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1756 DEBUG(1, ("Did not get a principal for krb5\n"));
1760 principal
= (char *)SMB_MALLOC(
1761 spnego
.negTokenInit
.mechListMIC
.length
+1);
1763 if (principal
== NULL
) {
1764 DEBUG(1, ("Could not malloc principal\n"));
1768 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1769 spnego
.negTokenInit
.mechListMIC
.length
);
1770 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1772 retval
= cli_krb5_get_ticket(ctx
, principal
, 0,
1773 &tkt
, &session_key_krb5
,
1774 0, NULL
, NULL
, NULL
);
1778 /* Let's try to first get the TGT, for that we need a
1781 if (opt_password
== NULL
) {
1782 DEBUG(10, ("Requesting password\n"));
1783 x_fprintf(x_stdout
, "PW\n");
1787 user
= talloc_asprintf(talloc_tos(), "%s@%s", opt_username
, opt_domain
);
1792 if ((retval
= kerberos_kinit_password(user
, opt_password
, 0, NULL
))) {
1793 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval
)));
1797 retval
= cli_krb5_get_ticket(ctx
, principal
, 0,
1798 &tkt
, &session_key_krb5
,
1799 0, NULL
, NULL
, NULL
);
1801 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1806 data_blob_free(&session_key_krb5
);
1810 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1811 reply
.negTokenInit
.mechTypes
= my_mechs
;
1812 reply
.negTokenInit
.reqFlags
= data_blob_null
;
1813 reply
.negTokenInit
.reqFlagsPadding
= 0;
1814 reply
.negTokenInit
.mechToken
= tkt
;
1815 reply
.negTokenInit
.mechListMIC
= data_blob_null
;
1817 len
= spnego_write_data(ctx
, &to_server
, &reply
);
1818 data_blob_free(&tkt
);
1821 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1825 reply_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1826 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1828 TALLOC_FREE(reply_base64
);
1829 data_blob_free(&to_server
);
1830 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1834 static void manage_client_krb5_targ(struct spnego_data spnego
)
1836 switch (spnego
.negTokenTarg
.negResult
) {
1837 case SPNEGO_ACCEPT_INCOMPLETE
:
1838 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1839 x_fprintf(x_stdout
, "BH Got a Kerberos negTokenTarg with "
1840 "ACCEPT_INCOMPLETE\n");
1842 case SPNEGO_ACCEPT_COMPLETED
:
1843 DEBUG(10, ("Accept completed\n"));
1844 x_fprintf(x_stdout
, "AF\n");
1847 DEBUG(10, ("Rejected\n"));
1848 x_fprintf(x_stdout
, "NA\n");
1851 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1852 x_fprintf(x_stdout
, "AF\n");
1858 static void manage_gss_spnego_client_request(struct ntlm_auth_state
*state
,
1859 char *buf
, int length
)
1862 struct spnego_data spnego
;
1864 TALLOC_CTX
*ctx
= talloc_tos();
1866 if (!opt_username
|| !*opt_username
) {
1867 x_fprintf(x_stderr
, "username must be specified!\n\n");
1871 if (strlen(buf
) <= 3) {
1872 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1873 x_fprintf(x_stdout
, "BH SPNEGO query too short\n");
1877 request
= base64_decode_data_blob(buf
+3);
1879 if (strncmp(buf
, "PW ", 3) == 0) {
1881 /* We asked for a password and obviously got it :-) */
1883 opt_password
= SMB_STRNDUP((const char *)request
.data
, request
.length
);
1885 if (opt_password
== NULL
) {
1886 DEBUG(1, ("Out of memory\n"));
1887 x_fprintf(x_stdout
, "BH Out of memory\n");
1888 data_blob_free(&request
);
1892 x_fprintf(x_stdout
, "OK\n");
1893 data_blob_free(&request
);
1897 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1898 (strncmp(buf
, "AF ", 3) != 0) &&
1899 (strncmp(buf
, "NA ", 3) != 0) ) {
1900 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1901 x_fprintf(x_stdout
, "BH SPNEGO request invalid\n");
1902 data_blob_free(&request
);
1906 /* So we got a server challenge to generate a SPNEGO
1907 client-to-server request... */
1909 len
= spnego_read_data(ctx
, request
, &spnego
);
1910 data_blob_free(&request
);
1913 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1914 x_fprintf(x_stdout
, "BH Could not read SPNEGO data\n");
1918 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1920 /* The server offers a list of mechanisms */
1922 const char **mechType
= (const char **)spnego
.negTokenInit
.mechTypes
;
1924 while (*mechType
!= NULL
) {
1927 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1928 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1929 if (manage_client_krb5_init(spnego
))
1934 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1935 if (manage_client_ntlmssp_init(spnego
))
1942 DEBUG(1, ("Server offered no compatible mechanism\n"));
1943 x_fprintf(x_stdout
, "BH Server offered no compatible mechanism\n");
1947 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1949 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1950 /* On accept/reject Windows does not send the
1951 mechanism anymore. Handle that here and
1952 shut down the mechanisms. */
1954 switch (spnego
.negTokenTarg
.negResult
) {
1955 case SPNEGO_ACCEPT_COMPLETED
:
1956 x_fprintf(x_stdout
, "AF\n");
1959 x_fprintf(x_stdout
, "NA\n");
1962 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1963 "unknown negResult: %d\n",
1964 spnego
.negTokenTarg
.negResult
));
1965 x_fprintf(x_stdout
, "BH Got a negTokenTarg with"
1966 " no mech and an unknown "
1970 TALLOC_FREE(client_ntlmssp_state
);
1974 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1975 OID_NTLMSSP
) == 0) {
1976 manage_client_ntlmssp_targ(spnego
);
1981 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1982 OID_KERBEROS5_OLD
) == 0) {
1983 manage_client_krb5_targ(spnego
);
1990 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1991 x_fprintf(x_stdout
, "BH Got an SPNEGO token I could not handle\n");
1995 spnego_free_data(&spnego
);
1999 static void manage_ntlm_server_1_request(struct ntlm_auth_state
*state
,
2000 char *buf
, int length
)
2002 char *request
, *parameter
;
2003 static DATA_BLOB challenge
;
2004 static DATA_BLOB lm_response
;
2005 static DATA_BLOB nt_response
;
2006 static char *full_username
;
2007 static char *username
;
2008 static char *domain
;
2009 static char *plaintext_password
;
2010 static bool ntlm_server_1_user_session_key
;
2011 static bool ntlm_server_1_lm_session_key
;
2013 if (strequal(buf
, ".")) {
2014 if (!full_username
&& !username
) {
2015 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2016 } else if (plaintext_password
) {
2017 /* handle this request as plaintext */
2018 if (!full_username
) {
2019 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
2020 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
2024 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
2025 x_fprintf(x_stdout
, "Authenticated: Yes\n");
2027 x_fprintf(x_stdout
, "Authenticated: No\n");
2029 } else if (!lm_response
.data
&& !nt_response
.data
) {
2030 x_fprintf(x_stdout
, "Error: No password supplied!\n");
2031 } else if (!challenge
.data
) {
2032 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
2034 char *error_string
= NULL
;
2036 uchar user_session_key
[16];
2039 if (full_username
&& !username
) {
2041 fstring fstr_domain
;
2043 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
2044 /* username might be 'tainted', don't print into our new-line deleimianted stream */
2045 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
2047 SAFE_FREE(username
);
2049 username
= smb_xstrdup(fstr_user
);
2050 domain
= smb_xstrdup(fstr_domain
);
2054 domain
= smb_xstrdup(get_winbind_domain());
2057 if (ntlm_server_1_lm_session_key
)
2058 flags
|= WBFLAG_PAM_LMKEY
;
2060 if (ntlm_server_1_user_session_key
)
2061 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2063 if (!NT_STATUS_IS_OK(
2064 contact_winbind_auth_crap(username
,
2076 x_fprintf(x_stdout
, "Authenticated: No\n");
2077 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
2079 static char zeros
[16];
2081 char *hex_user_session_key
;
2083 x_fprintf(x_stdout
, "Authenticated: Yes\n");
2085 if (ntlm_server_1_lm_session_key
2086 && (memcmp(zeros
, lm_key
,
2087 sizeof(lm_key
)) != 0)) {
2088 hex_lm_key
= hex_encode_talloc(NULL
,
2089 (const unsigned char *)lm_key
,
2091 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
2092 TALLOC_FREE(hex_lm_key
);
2095 if (ntlm_server_1_user_session_key
2096 && (memcmp(zeros
, user_session_key
,
2097 sizeof(user_session_key
)) != 0)) {
2098 hex_user_session_key
= hex_encode_talloc(NULL
,
2099 (const unsigned char *)user_session_key
,
2100 sizeof(user_session_key
));
2101 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
2102 TALLOC_FREE(hex_user_session_key
);
2105 SAFE_FREE(error_string
);
2107 /* clear out the state */
2108 challenge
= data_blob_null
;
2109 nt_response
= data_blob_null
;
2110 lm_response
= data_blob_null
;
2111 SAFE_FREE(full_username
);
2112 SAFE_FREE(username
);
2114 SAFE_FREE(plaintext_password
);
2115 ntlm_server_1_user_session_key
= False
;
2116 ntlm_server_1_lm_session_key
= False
;
2117 x_fprintf(x_stdout
, ".\n");
2124 /* Indicates a base64 encoded structure */
2125 parameter
= strstr_m(request
, ":: ");
2127 parameter
= strstr_m(request
, ": ");
2130 DEBUG(0, ("Parameter not found!\n"));
2131 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2148 base64_decode_inplace(parameter
);
2151 if (strequal(request
, "LANMAN-Challenge")) {
2152 challenge
= strhex_to_data_blob(NULL
, parameter
);
2153 if (challenge
.length
!= 8) {
2154 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
2156 (int)challenge
.length
);
2157 challenge
= data_blob_null
;
2159 } else if (strequal(request
, "NT-Response")) {
2160 nt_response
= strhex_to_data_blob(NULL
, parameter
);
2161 if (nt_response
.length
< 24) {
2162 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
2164 (int)nt_response
.length
);
2165 nt_response
= data_blob_null
;
2167 } else if (strequal(request
, "LANMAN-Response")) {
2168 lm_response
= strhex_to_data_blob(NULL
, parameter
);
2169 if (lm_response
.length
!= 24) {
2170 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2172 (int)lm_response
.length
);
2173 lm_response
= data_blob_null
;
2175 } else if (strequal(request
, "Password")) {
2176 plaintext_password
= smb_xstrdup(parameter
);
2177 } else if (strequal(request
, "NT-Domain")) {
2178 domain
= smb_xstrdup(parameter
);
2179 } else if (strequal(request
, "Username")) {
2180 username
= smb_xstrdup(parameter
);
2181 } else if (strequal(request
, "Full-Username")) {
2182 full_username
= smb_xstrdup(parameter
);
2183 } else if (strequal(request
, "Request-User-Session-Key")) {
2184 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
2185 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
2186 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
2188 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2192 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
2193 char *buf
, int length
)
2195 char *request
, *parameter
;
2196 static DATA_BLOB new_nt_pswd
;
2197 static DATA_BLOB old_nt_hash_enc
;
2198 static DATA_BLOB new_lm_pswd
;
2199 static DATA_BLOB old_lm_hash_enc
;
2200 static char *full_username
= NULL
;
2201 static char *username
= NULL
;
2202 static char *domain
= NULL
;
2203 static char *newpswd
= NULL
;
2204 static char *oldpswd
= NULL
;
2206 if (strequal(buf
, ".")) {
2207 if(newpswd
&& oldpswd
) {
2208 uchar old_nt_hash
[16];
2209 uchar old_lm_hash
[16];
2210 uchar new_nt_hash
[16];
2211 uchar new_lm_hash
[16];
2213 new_nt_pswd
= data_blob(NULL
, 516);
2214 old_nt_hash_enc
= data_blob(NULL
, 16);
2216 /* Calculate the MD4 hash (NT compatible) of the
2218 E_md4hash(oldpswd
, old_nt_hash
);
2219 E_md4hash(newpswd
, new_nt_hash
);
2221 /* E_deshash returns false for 'long'
2222 passwords (> 14 DOS chars).
2224 Therefore, don't send a buffer
2225 encrypted with the truncated hash
2226 (it could allow an even easier
2227 attack on the password)
2229 Likewise, obey the admin's restriction
2232 if (lp_client_lanman_auth() &&
2233 E_deshash(newpswd
, new_lm_hash
) &&
2234 E_deshash(oldpswd
, old_lm_hash
)) {
2235 new_lm_pswd
= data_blob(NULL
, 516);
2236 old_lm_hash_enc
= data_blob(NULL
, 16);
2237 encode_pw_buffer(new_lm_pswd
.data
, newpswd
,
2240 arcfour_crypt(new_lm_pswd
.data
, old_nt_hash
, 516);
2241 E_old_pw_hash(new_nt_hash
, old_lm_hash
,
2242 old_lm_hash_enc
.data
);
2244 new_lm_pswd
.data
= NULL
;
2245 new_lm_pswd
.length
= 0;
2246 old_lm_hash_enc
.data
= NULL
;
2247 old_lm_hash_enc
.length
= 0;
2250 encode_pw_buffer(new_nt_pswd
.data
, newpswd
,
2253 arcfour_crypt(new_nt_pswd
.data
, old_nt_hash
, 516);
2254 E_old_pw_hash(new_nt_hash
, old_nt_hash
,
2255 old_nt_hash_enc
.data
);
2258 if (!full_username
&& !username
) {
2259 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2260 } else if ((!new_nt_pswd
.data
|| !old_nt_hash_enc
.data
) &&
2261 (!new_lm_pswd
.data
|| old_lm_hash_enc
.data
) ) {
2262 x_fprintf(x_stdout
, "Error: No NT or LM password "
2263 "blobs supplied!\n");
2265 char *error_string
= NULL
;
2267 if (full_username
&& !username
) {
2269 fstring fstr_domain
;
2271 if (!parse_ntlm_auth_domain_user(full_username
,
2274 /* username might be 'tainted', don't
2275 * print into our new-line
2276 * deleimianted stream */
2277 x_fprintf(x_stdout
, "Error: Could not "
2278 "parse into domain and "
2280 SAFE_FREE(username
);
2281 username
= smb_xstrdup(full_username
);
2283 SAFE_FREE(username
);
2285 username
= smb_xstrdup(fstr_user
);
2286 domain
= smb_xstrdup(fstr_domain
);
2291 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2298 x_fprintf(x_stdout
, "Password-Change: No\n");
2299 x_fprintf(x_stdout
, "Password-Change-Error: "
2300 "%s\n.\n", error_string
);
2302 x_fprintf(x_stdout
, "Password-Change: Yes\n");
2305 SAFE_FREE(error_string
);
2307 /* clear out the state */
2308 new_nt_pswd
= data_blob_null
;
2309 old_nt_hash_enc
= data_blob_null
;
2310 new_lm_pswd
= data_blob_null
;
2311 old_nt_hash_enc
= data_blob_null
;
2312 SAFE_FREE(full_username
);
2313 SAFE_FREE(username
);
2317 x_fprintf(x_stdout
, ".\n");
2324 /* Indicates a base64 encoded structure */
2325 parameter
= strstr_m(request
, ":: ");
2327 parameter
= strstr_m(request
, ": ");
2330 DEBUG(0, ("Parameter not found!\n"));
2331 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2347 base64_decode_inplace(parameter
);
2350 if (strequal(request
, "new-nt-password-blob")) {
2351 new_nt_pswd
= strhex_to_data_blob(NULL
, parameter
);
2352 if (new_nt_pswd
.length
!= 516) {
2353 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2354 "(got %d bytes, expected 516)\n.\n",
2356 (int)new_nt_pswd
.length
);
2357 new_nt_pswd
= data_blob_null
;
2359 } else if (strequal(request
, "old-nt-hash-blob")) {
2360 old_nt_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2361 if (old_nt_hash_enc
.length
!= 16) {
2362 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2363 "(got %d bytes, expected 16)\n.\n",
2365 (int)old_nt_hash_enc
.length
);
2366 old_nt_hash_enc
= data_blob_null
;
2368 } else if (strequal(request
, "new-lm-password-blob")) {
2369 new_lm_pswd
= strhex_to_data_blob(NULL
, parameter
);
2370 if (new_lm_pswd
.length
!= 516) {
2371 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2372 "(got %d bytes, expected 516)\n.\n",
2374 (int)new_lm_pswd
.length
);
2375 new_lm_pswd
= data_blob_null
;
2378 else if (strequal(request
, "old-lm-hash-blob")) {
2379 old_lm_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2380 if (old_lm_hash_enc
.length
!= 16)
2382 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2383 "(got %d bytes, expected 16)\n.\n",
2385 (int)old_lm_hash_enc
.length
);
2386 old_lm_hash_enc
= data_blob_null
;
2388 } else if (strequal(request
, "nt-domain")) {
2389 domain
= smb_xstrdup(parameter
);
2390 } else if(strequal(request
, "username")) {
2391 username
= smb_xstrdup(parameter
);
2392 } else if(strequal(request
, "full-username")) {
2393 username
= smb_xstrdup(parameter
);
2394 } else if(strequal(request
, "new-password")) {
2395 newpswd
= smb_xstrdup(parameter
);
2396 } else if (strequal(request
, "old-password")) {
2397 oldpswd
= smb_xstrdup(parameter
);
2399 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2403 static void manage_squid_request(struct ntlm_auth_state
*state
,
2404 stdio_helper_function fn
)
2407 char tmp
[INITIAL_BUFFER_SIZE
+1];
2408 int length
, buf_size
= 0;
2411 buf
= talloc_strdup(state
->mem_ctx
, "");
2413 DEBUG(0, ("Failed to allocate input buffer.\n"));
2414 x_fprintf(x_stderr
, "ERR\n");
2420 /* this is not a typo - x_fgets doesn't work too well under
2422 if (fgets(tmp
, sizeof(tmp
)-1, stdin
) == NULL
) {
2423 if (ferror(stdin
)) {
2424 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2425 "(%s)\n", ferror(stdin
),
2426 strerror(ferror(stdin
))));
2433 buf
= talloc_strdup_append_buffer(buf
, tmp
);
2434 buf_size
+= INITIAL_BUFFER_SIZE
;
2436 if (buf_size
> MAX_BUFFER_SIZE
) {
2437 DEBUG(2, ("Oversized message\n"));
2438 x_fprintf(x_stderr
, "ERR\n");
2443 c
= strchr(buf
, '\n');
2444 } while (c
== NULL
);
2449 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
2451 if (buf
[0] == '\0') {
2452 DEBUG(2, ("Invalid Request\n"));
2453 x_fprintf(x_stderr
, "ERR\n");
2458 fn(state
, buf
, length
);
2463 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
2464 TALLOC_CTX
*mem_ctx
;
2465 struct ntlm_auth_state
*state
;
2467 /* initialize FDescs */
2468 x_setbuf(x_stdout
, NULL
);
2469 x_setbuf(x_stderr
, NULL
);
2471 mem_ctx
= talloc_init("ntlm_auth");
2473 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2474 x_fprintf(x_stderr
, "ERR\n");
2478 state
= talloc_zero(mem_ctx
, struct ntlm_auth_state
);
2480 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2481 x_fprintf(x_stderr
, "ERR\n");
2485 state
->mem_ctx
= mem_ctx
;
2486 state
->helper_mode
= stdio_mode
;
2489 TALLOC_CTX
*frame
= talloc_stackframe();
2490 manage_squid_request(state
, fn
);
2496 /* Authenticate a user with a challenge/response */
2498 static bool check_auth_crap(void)
2503 char user_session_key
[16];
2505 char *hex_user_session_key
;
2507 static uint8 zeros
[16];
2509 x_setbuf(x_stdout
, NULL
);
2512 flags
|= WBFLAG_PAM_LMKEY
;
2514 if (request_user_session_key
)
2515 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2517 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
2519 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
2525 (unsigned char *)lm_key
,
2526 (unsigned char *)user_session_key
,
2527 &error_string
, NULL
);
2529 if (!NT_STATUS_IS_OK(nt_status
)) {
2530 x_fprintf(x_stdout
, "%s (0x%x)\n",
2532 NT_STATUS_V(nt_status
));
2533 SAFE_FREE(error_string
);
2538 && (memcmp(zeros
, lm_key
,
2539 sizeof(lm_key
)) != 0)) {
2540 hex_lm_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key
,
2542 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
2543 TALLOC_FREE(hex_lm_key
);
2545 if (request_user_session_key
2546 && (memcmp(zeros
, user_session_key
,
2547 sizeof(user_session_key
)) != 0)) {
2548 hex_user_session_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key
,
2549 sizeof(user_session_key
));
2550 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
2551 TALLOC_FREE(hex_user_session_key
);
2560 OPT_USERNAME
= 1000,
2569 OPT_USER_SESSION_KEY
,
2571 OPT_REQUIRE_MEMBERSHIP
,
2572 OPT_USE_CACHED_CREDS
,
2573 OPT_PAM_WINBIND_CONF
2576 int main(int argc
, const char **argv
)
2578 TALLOC_CTX
*frame
= talloc_stackframe();
2580 static const char *helper_protocol
;
2581 static int diagnostics
;
2583 static const char *hex_challenge
;
2584 static const char *hex_lm_response
;
2585 static const char *hex_nt_response
;
2589 /* NOTE: DO NOT change this interface without considering the implications!
2590 This is an external interface, which other programs will use to interact
2594 /* We do not use single-letter command abbreviations, because they harm future
2595 interface stability. */
2597 struct poptOption long_options
[] = {
2599 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
2600 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
2601 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
2602 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
2603 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
2604 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
2605 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2606 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
2607 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retrieve LM session key"},
2608 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retrieve User (NT) session key"},
2609 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "Use cached credentials if no password is given"},
2610 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
,
2612 "Perform diagnostics on the authentication chain"},
2613 { "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" },
2614 { "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" },
2615 POPT_COMMON_CONFIGFILE
2620 /* Samba client initialisation */
2623 setup_logging("ntlm_auth", DEBUG_STDERR
);
2627 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
2629 /* Parse command line options */
2632 poptPrintHelp(pc
, stderr
, 0);
2636 while((opt
= poptGetNextOpt(pc
)) != -1) {
2637 /* Get generic config options like --configfile */
2640 poptFreeContext(pc
);
2642 if (!lp_load(get_dyn_CONFIGFILE(), True
, False
, False
, True
)) {
2643 d_fprintf(stderr
, "ntlm_auth: error opening config file %s. Error was %s\n",
2644 get_dyn_CONFIGFILE(), strerror(errno
));
2648 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2649 POPT_CONTEXT_KEEP_FIRST
);
2651 while((opt
= poptGetNextOpt(pc
)) != -1) {
2654 opt_challenge
= strhex_to_data_blob(NULL
, hex_challenge
);
2655 if (opt_challenge
.length
!= 8) {
2656 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2658 (int)opt_challenge
.length
);
2663 opt_lm_response
= strhex_to_data_blob(NULL
, hex_lm_response
);
2664 if (opt_lm_response
.length
!= 24) {
2665 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2667 (int)opt_lm_response
.length
);
2673 opt_nt_response
= strhex_to_data_blob(NULL
, hex_nt_response
);
2674 if (opt_nt_response
.length
< 24) {
2675 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2677 (int)opt_nt_response
.length
);
2682 case OPT_REQUIRE_MEMBERSHIP
:
2683 if (StrnCaseCmp("S-", require_membership_of
, 2) == 0) {
2684 require_membership_of_sid
= require_membership_of
;
2691 char *domain
= SMB_STRDUP(opt_username
);
2692 char *p
= strchr_m(domain
, *lp_winbind_separator());
2696 if (opt_domain
&& !strequal(opt_domain
, domain
)) {
2697 x_fprintf(x_stderr
, "Domain specified in username (%s) "
2698 "doesn't match specified domain (%s)!\n\n",
2699 domain
, opt_domain
);
2700 poptPrintHelp(pc
, stderr
, 0);
2703 opt_domain
= domain
;
2709 /* Note: if opt_domain is "" then send no domain */
2710 if (opt_domain
== NULL
) {
2711 opt_domain
= get_winbind_domain();
2714 if (opt_workstation
== NULL
) {
2715 opt_workstation
= "";
2718 if (helper_protocol
) {
2720 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2721 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
2722 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
2726 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
2728 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2729 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
2735 if (!opt_username
|| !*opt_username
) {
2736 x_fprintf(x_stderr
, "username must be specified!\n\n");
2737 poptPrintHelp(pc
, stderr
, 0);
2741 if (opt_challenge
.length
) {
2742 if (!check_auth_crap()) {
2748 if (!opt_password
) {
2749 opt_password
= getpass("password: ");
2753 if (!diagnose_ntlm_auth()) {
2759 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
2760 if (!check_plaintext_auth(user
, opt_password
, True
)) {
2767 poptFreeContext(pc
);