2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
9 Copyright (C) Robert O'Callahan 2006 (added cached credential code).
10 Copyright (C) Kai Blin <kai@samba.org> 2008
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "popt_common.h"
28 #include "utils/ntlm_auth.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "../libcli/auth/spnego.h"
31 #include "../auth/ntlmssp/ntlmssp.h"
33 #include <iniparser.h>
34 #include "../lib/crypto/arcfour.h"
35 #include "libads/kerberos_proto.h"
36 #include "nsswitch/winbind_client.h"
37 #include "librpc/gen_ndr/krb5pac.h"
38 #include "../lib/util/asn1.h"
40 #ifndef PAM_WINBIND_CONFIG_FILE
41 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
44 #define WINBIND_KRB5_AUTH 0x00000080
47 #define DBGC_CLASS DBGC_WINBIND
49 #define INITIAL_BUFFER_SIZE 300
50 #define MAX_BUFFER_SIZE 630000
52 enum stdio_helper_mode
{
60 NTLM_CHANGE_PASSWORD_1
,
64 enum ntlm_auth_cli_state
{
71 enum ntlm_auth_svr_state
{
78 struct ntlm_auth_state
{
80 enum stdio_helper_mode helper_mode
;
81 enum ntlm_auth_cli_state cli_state
;
82 enum ntlm_auth_svr_state svr_state
;
83 struct ntlmssp_state
*ntlmssp_state
;
85 char *want_feature_list
;
87 char *spnego_mech_oid
;
88 bool have_session_key
;
89 DATA_BLOB session_key
;
90 DATA_BLOB initial_message
;
93 typedef void (*stdio_helper_function
)(struct ntlm_auth_state
*state
, char *buf
,
96 static void manage_squid_basic_request (struct ntlm_auth_state
*state
,
97 char *buf
, int length
);
99 static void manage_squid_ntlmssp_request (struct ntlm_auth_state
*state
,
100 char *buf
, int length
);
102 static void manage_client_ntlmssp_request (struct ntlm_auth_state
*state
,
103 char *buf
, int length
);
105 static void manage_gss_spnego_request (struct ntlm_auth_state
*state
,
106 char *buf
, int length
);
108 static void manage_gss_spnego_client_request (struct ntlm_auth_state
*state
,
109 char *buf
, int length
);
111 static void manage_ntlm_server_1_request (struct ntlm_auth_state
*state
,
112 char *buf
, int length
);
114 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
115 char *buf
, int length
);
117 static const struct {
118 enum stdio_helper_mode mode
;
120 stdio_helper_function fn
;
121 } stdio_helper_protocols
[] = {
122 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
123 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
124 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request
},
125 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_client_ntlmssp_request
},
126 { GSS_SPNEGO
, "gss-spnego", manage_gss_spnego_request
},
127 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gss_spnego_client_request
},
128 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
129 { NTLM_CHANGE_PASSWORD_1
, "ntlm-change-password-1", manage_ntlm_change_password_1_request
},
130 { NUM_HELPER_MODES
, NULL
, NULL
}
133 const char *opt_username
;
134 const char *opt_domain
;
135 const char *opt_workstation
;
136 const char *opt_password
;
137 static DATA_BLOB opt_challenge
;
138 static DATA_BLOB opt_lm_response
;
139 static DATA_BLOB opt_nt_response
;
140 static int request_lm_key
;
141 static int request_user_session_key
;
142 static int use_cached_creds
;
144 static const char *require_membership_of
;
145 static const char *require_membership_of_sid
;
146 static const char *opt_pam_winbind_conf
;
148 static char winbind_separator(void)
150 struct winbindd_response response
;
157 ZERO_STRUCT(response
);
159 /* Send off request */
161 if (winbindd_request_response(WINBINDD_INFO
, NULL
, &response
) !=
162 NSS_STATUS_SUCCESS
) {
163 d_printf("could not obtain winbind separator!\n");
164 return *lp_winbind_separator();
167 sep
= response
.data
.info
.winbind_separator
;
171 d_printf("winbind separator was NULL!\n");
172 return *lp_winbind_separator();
178 const char *get_winbind_domain(void)
180 struct winbindd_response response
;
182 static fstring winbind_domain
;
183 if (*winbind_domain
) {
184 return winbind_domain
;
187 ZERO_STRUCT(response
);
189 /* Send off request */
191 if (winbindd_request_response(WINBINDD_DOMAIN_NAME
, NULL
, &response
) !=
192 NSS_STATUS_SUCCESS
) {
193 DEBUG(0, ("could not obtain winbind domain name!\n"));
194 return lp_workgroup();
197 fstrcpy(winbind_domain
, response
.data
.domain_name
);
199 return winbind_domain
;
203 const char *get_winbind_netbios_name(void)
205 struct winbindd_response response
;
207 static fstring winbind_netbios_name
;
209 if (*winbind_netbios_name
) {
210 return winbind_netbios_name
;
213 ZERO_STRUCT(response
);
215 /* Send off request */
217 if (winbindd_request_response(WINBINDD_NETBIOS_NAME
, NULL
, &response
) !=
218 NSS_STATUS_SUCCESS
) {
219 DEBUG(0, ("could not obtain winbind netbios name!\n"));
220 return lp_netbios_name();
223 fstrcpy(winbind_netbios_name
, response
.data
.netbios_name
);
225 return winbind_netbios_name
;
229 DATA_BLOB
get_challenge(void)
231 static DATA_BLOB chal
;
232 if (opt_challenge
.length
)
233 return opt_challenge
;
235 chal
= data_blob(NULL
, 8);
237 generate_random_buffer(chal
.data
, chal
.length
);
241 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
242 form DOMAIN/user into a domain and a user */
244 static bool parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
248 char *p
= strchr(domuser
,winbind_separator());
255 fstrcpy(domain
, domuser
);
256 domain
[PTR_DIFF(p
, domuser
)] = 0;
262 static bool get_require_membership_sid(void) {
263 struct winbindd_request request
;
264 struct winbindd_response response
;
266 if (!require_membership_of
) {
270 if (require_membership_of_sid
) {
274 /* Otherwise, ask winbindd for the name->sid request */
276 ZERO_STRUCT(request
);
277 ZERO_STRUCT(response
);
279 if (!parse_ntlm_auth_domain_user(require_membership_of
,
280 request
.data
.name
.dom_name
,
281 request
.data
.name
.name
)) {
282 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
283 require_membership_of
));
287 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
288 NSS_STATUS_SUCCESS
) {
289 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
290 require_membership_of
));
294 require_membership_of_sid
= SMB_STRDUP(response
.data
.sid
.sid
);
296 if (require_membership_of_sid
)
303 * Get some configuration from pam_winbind.conf to see if we
304 * need to contact trusted domain
307 int get_pam_winbind_config()
310 dictionary
*d
= NULL
;
312 if (!opt_pam_winbind_conf
|| !*opt_pam_winbind_conf
) {
313 opt_pam_winbind_conf
= PAM_WINBIND_CONFIG_FILE
;
316 d
= iniparser_load(discard_const_p(char, opt_pam_winbind_conf
));
322 if (iniparser_getboolean(d
, discard_const_p(char, "global:krb5_auth"), false)) {
323 ctrl
|= WINBIND_KRB5_AUTH
;
326 iniparser_freedict(d
);
331 /* Authenticate a user with a plaintext password */
333 static bool check_plaintext_auth(const char *user
, const char *pass
,
334 bool stdout_diagnostics
)
336 struct winbindd_request request
;
337 struct winbindd_response response
;
340 if (!get_require_membership_sid()) {
344 /* Send off request */
346 ZERO_STRUCT(request
);
347 ZERO_STRUCT(response
);
349 fstrcpy(request
.data
.auth
.user
, user
);
350 fstrcpy(request
.data
.auth
.pass
, pass
);
351 if (require_membership_of_sid
) {
352 strlcpy(request
.data
.auth
.require_membership_of_sid
,
353 require_membership_of_sid
,
354 sizeof(request
.data
.auth
.require_membership_of_sid
));
357 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
359 /* Display response */
361 if (stdout_diagnostics
) {
362 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
363 d_printf("Reading winbind reply failed! (0x01)\n");
366 d_printf("%s: %s (0x%x)\n",
367 response
.data
.auth
.nt_status_string
,
368 response
.data
.auth
.error_string
,
369 response
.data
.auth
.nt_status
);
371 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
372 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
375 DEBUG(3, ("%s: %s (0x%x)\n",
376 response
.data
.auth
.nt_status_string
,
377 response
.data
.auth
.error_string
,
378 response
.data
.auth
.nt_status
));
381 return (result
== NSS_STATUS_SUCCESS
);
384 /* authenticate a user with an encrypted username/password */
386 NTSTATUS
contact_winbind_auth_crap(const char *username
,
388 const char *workstation
,
389 const DATA_BLOB
*challenge
,
390 const DATA_BLOB
*lm_response
,
391 const DATA_BLOB
*nt_response
,
393 uint32 extra_logon_parameters
,
395 uint8 user_session_key
[16],
401 struct winbindd_request request
;
402 struct winbindd_response response
;
404 if (!get_require_membership_sid()) {
405 return NT_STATUS_INVALID_PARAMETER
;
408 ZERO_STRUCT(request
);
409 ZERO_STRUCT(response
);
411 request
.flags
= flags
;
413 request
.data
.auth_crap
.logon_parameters
= extra_logon_parameters
414 | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
| MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
416 if (require_membership_of_sid
)
417 fstrcpy(request
.data
.auth_crap
.require_membership_of_sid
, require_membership_of_sid
);
419 fstrcpy(request
.data
.auth_crap
.user
, username
);
420 fstrcpy(request
.data
.auth_crap
.domain
, domain
);
422 fstrcpy(request
.data
.auth_crap
.workstation
,
425 memcpy(request
.data
.auth_crap
.chal
, challenge
->data
, MIN(challenge
->length
, 8));
427 if (lm_response
&& lm_response
->length
) {
428 memcpy(request
.data
.auth_crap
.lm_resp
,
430 MIN(lm_response
->length
, sizeof(request
.data
.auth_crap
.lm_resp
)));
431 request
.data
.auth_crap
.lm_resp_len
= lm_response
->length
;
434 if (nt_response
&& nt_response
->length
) {
435 if (nt_response
->length
> sizeof(request
.data
.auth_crap
.nt_resp
)) {
436 request
.flags
= request
.flags
| WBFLAG_BIG_NTLMV2_BLOB
;
437 request
.extra_len
= nt_response
->length
;
438 request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, request
.extra_len
);
439 if (request
.extra_data
.data
== NULL
) {
440 return NT_STATUS_NO_MEMORY
;
442 memcpy(request
.extra_data
.data
, nt_response
->data
,
443 nt_response
->length
);
446 memcpy(request
.data
.auth_crap
.nt_resp
,
447 nt_response
->data
, nt_response
->length
);
449 request
.data
.auth_crap
.nt_resp_len
= nt_response
->length
;
452 result
= winbindd_request_response(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
453 SAFE_FREE(request
.extra_data
.data
);
455 /* Display response */
457 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
458 nt_status
= NT_STATUS_UNSUCCESSFUL
;
460 *error_string
= smb_xstrdup("Reading winbind reply failed!");
461 winbindd_free_response(&response
);
465 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
466 if (!NT_STATUS_IS_OK(nt_status
)) {
468 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
469 winbindd_free_response(&response
);
473 if ((flags
& WBFLAG_PAM_LMKEY
) && lm_key
) {
474 memcpy(lm_key
, response
.data
.auth
.first_8_lm_hash
,
475 sizeof(response
.data
.auth
.first_8_lm_hash
));
477 if ((flags
& WBFLAG_PAM_USER_SESSION_KEY
) && user_session_key
) {
478 memcpy(user_session_key
, response
.data
.auth
.user_session_key
,
479 sizeof(response
.data
.auth
.user_session_key
));
482 if (flags
& WBFLAG_PAM_UNIX_NAME
) {
483 *unix_name
= SMB_STRDUP(response
.data
.auth
.unix_username
);
485 winbindd_free_response(&response
);
486 return NT_STATUS_NO_MEMORY
;
490 winbindd_free_response(&response
);
494 /* contact server to change user password using auth crap */
495 static NTSTATUS
contact_winbind_change_pswd_auth_crap(const char *username
,
497 const DATA_BLOB new_nt_pswd
,
498 const DATA_BLOB old_nt_hash_enc
,
499 const DATA_BLOB new_lm_pswd
,
500 const DATA_BLOB old_lm_hash_enc
,
505 struct winbindd_request request
;
506 struct winbindd_response response
;
508 if (!get_require_membership_sid())
511 *error_string
= smb_xstrdup("Can't get membership sid.");
512 return NT_STATUS_INVALID_PARAMETER
;
515 ZERO_STRUCT(request
);
516 ZERO_STRUCT(response
);
519 fstrcpy(request
.data
.chng_pswd_auth_crap
.user
, username
);
521 fstrcpy(request
.data
.chng_pswd_auth_crap
.domain
,domain
);
523 if(new_nt_pswd
.length
)
525 memcpy(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
, new_nt_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
));
526 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
= new_nt_pswd
.length
;
529 if(old_nt_hash_enc
.length
)
531 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
));
532 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
= old_nt_hash_enc
.length
;
535 if(new_lm_pswd
.length
)
537 memcpy(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
, new_lm_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
));
538 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
= new_lm_pswd
.length
;
541 if(old_lm_hash_enc
.length
)
543 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
));
544 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
= old_lm_hash_enc
.length
;
547 result
= winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, &request
, &response
);
549 /* Display response */
551 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0))
553 nt_status
= NT_STATUS_UNSUCCESSFUL
;
555 *error_string
= smb_xstrdup("Reading winbind reply failed!");
556 winbindd_free_response(&response
);
560 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
561 if (!NT_STATUS_IS_OK(nt_status
))
564 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
565 winbindd_free_response(&response
);
569 winbindd_free_response(&response
);
574 static NTSTATUS
winbind_pw_check(struct ntlmssp_state
*ntlmssp_state
, TALLOC_CTX
*mem_ctx
,
575 DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
577 static const char zeros
[16] = { 0, };
579 char *error_string
= NULL
;
581 uint8 user_sess_key
[16];
582 char *unix_name
= NULL
;
584 nt_status
= contact_winbind_auth_crap(ntlmssp_state
->user
, ntlmssp_state
->domain
,
585 ntlmssp_state
->client
.netbios_name
,
586 &ntlmssp_state
->chal
,
587 &ntlmssp_state
->lm_resp
,
588 &ntlmssp_state
->nt_resp
,
589 WBFLAG_PAM_LMKEY
| WBFLAG_PAM_USER_SESSION_KEY
| WBFLAG_PAM_UNIX_NAME
,
591 lm_key
, user_sess_key
,
592 &error_string
, &unix_name
);
594 if (NT_STATUS_IS_OK(nt_status
)) {
595 if (memcmp(lm_key
, zeros
, 8) != 0) {
596 *lm_session_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
597 memcpy(lm_session_key
->data
, lm_key
, 8);
598 memset(lm_session_key
->data
+8, '\0', 8);
601 if (memcmp(user_sess_key
, zeros
, 16) != 0) {
602 *user_session_key
= data_blob_talloc(mem_ctx
, user_sess_key
, 16);
604 ntlmssp_state
->callback_private
= talloc_strdup(ntlmssp_state
,
607 DEBUG(NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
) ? 0 : 3,
608 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
609 ntlmssp_state
->domain
, ntlmssp_state
->user
,
610 ntlmssp_state
->client
.netbios_name
,
611 error_string
? error_string
: "unknown error (NULL)"));
612 ntlmssp_state
->callback_private
= NULL
;
615 SAFE_FREE(error_string
);
616 SAFE_FREE(unix_name
);
620 static NTSTATUS
local_pw_check(struct ntlmssp_state
*ntlmssp_state
, TALLOC_CTX
*mem_ctx
,
621 DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
624 struct samr_Password lm_pw
, nt_pw
;
626 nt_lm_owf_gen (opt_password
, nt_pw
.hash
, lm_pw
.hash
);
628 nt_status
= ntlm_password_check(mem_ctx
,
630 &ntlmssp_state
->chal
,
631 &ntlmssp_state
->lm_resp
,
632 &ntlmssp_state
->nt_resp
,
635 ntlmssp_state
->domain
,
636 &lm_pw
, &nt_pw
, user_session_key
, lm_session_key
);
638 if (NT_STATUS_IS_OK(nt_status
)) {
639 ntlmssp_state
->callback_private
= talloc_asprintf(ntlmssp_state
,
640 "%s%c%s", ntlmssp_state
->domain
,
641 *lp_winbind_separator(),
642 ntlmssp_state
->user
);
644 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
645 ntlmssp_state
->domain
, ntlmssp_state
->user
,
646 ntlmssp_state
->client
.netbios_name
,
647 nt_errstr(nt_status
)));
648 ntlmssp_state
->callback_private
= NULL
;
653 static NTSTATUS
ntlm_auth_start_ntlmssp_client(struct ntlmssp_state
**client_ntlmssp_state
)
656 if ( (opt_username
== NULL
) || (opt_domain
== NULL
) ) {
657 status
= NT_STATUS_UNSUCCESSFUL
;
658 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
659 return NT_STATUS_INVALID_PARAMETER
;
662 status
= ntlmssp_client_start(NULL
,
665 lp_client_ntlmv2_auth(),
666 client_ntlmssp_state
);
668 if (!NT_STATUS_IS_OK(status
)) {
669 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
671 TALLOC_FREE(*client_ntlmssp_state
);
675 status
= ntlmssp_set_username(*client_ntlmssp_state
, opt_username
);
677 if (!NT_STATUS_IS_OK(status
)) {
678 DEBUG(1, ("Could not set username: %s\n",
680 TALLOC_FREE(*client_ntlmssp_state
);
684 status
= ntlmssp_set_domain(*client_ntlmssp_state
, opt_domain
);
686 if (!NT_STATUS_IS_OK(status
)) {
687 DEBUG(1, ("Could not set domain: %s\n",
689 TALLOC_FREE(*client_ntlmssp_state
);
694 status
= ntlmssp_set_password(*client_ntlmssp_state
, opt_password
);
696 if (!NT_STATUS_IS_OK(status
)) {
697 DEBUG(1, ("Could not set password: %s\n",
699 TALLOC_FREE(*client_ntlmssp_state
);
707 static NTSTATUS
ntlm_auth_start_ntlmssp_server(struct ntlmssp_state
**ntlmssp_state
)
710 const char *netbios_name
;
711 const char *netbios_domain
;
712 const char *dns_name
;
714 bool is_standalone
= false;
717 netbios_name
= lp_netbios_name();
718 netbios_domain
= lp_workgroup();
720 netbios_name
= get_winbind_netbios_name();
721 netbios_domain
= get_winbind_domain();
723 /* This should be a 'netbios domain -> DNS domain' mapping */
724 dns_domain
= get_mydnsdomname(talloc_tos());
726 strlower_m(dns_domain
);
728 dns_name
= get_mydnsfullname();
730 status
= ntlmssp_server_start(NULL
,
737 if (!NT_STATUS_IS_OK(status
)) {
738 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
743 (*ntlmssp_state
)->neg_flags
|=
744 (NTLMSSP_NEGOTIATE_SIGN
|
745 NTLMSSP_NEGOTIATE_SEAL
);
747 /* Have we been given a local password, or should we ask winbind? */
749 (*ntlmssp_state
)->check_password
= local_pw_check
;
751 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
756 /*******************************************************************
757 Used by firefox to drive NTLM auth to IIS servers.
758 *******************************************************************/
760 static NTSTATUS
do_ccache_ntlm_auth(DATA_BLOB initial_msg
, DATA_BLOB challenge_msg
,
763 struct winbindd_request wb_request
;
764 struct winbindd_response wb_response
;
768 /* get winbindd to do the ntlmssp step on our behalf */
769 ZERO_STRUCT(wb_request
);
770 ZERO_STRUCT(wb_response
);
773 * This is tricky here. If we set krb5_auth in pam_winbind.conf
774 * creds for users in trusted domain will be stored the winbindd
775 * child of the trusted domain. If we ask the primary domain for
776 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
777 * domain's child for ccache_ntlm_auth. that is to say, we have to
778 * set WBFLAG_PAM_CONTACT_TRUSTDOM in request.flags.
780 ctrl
= get_pam_winbind_config();
782 if (ctrl
& WINBIND_KRB5_AUTH
) {
783 wb_request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
786 fstr_sprintf(wb_request
.data
.ccache_ntlm_auth
.user
,
787 "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
788 wb_request
.data
.ccache_ntlm_auth
.uid
= geteuid();
789 wb_request
.data
.ccache_ntlm_auth
.initial_blob_len
= initial_msg
.length
;
790 wb_request
.data
.ccache_ntlm_auth
.challenge_blob_len
= challenge_msg
.length
;
791 wb_request
.extra_len
= initial_msg
.length
+ challenge_msg
.length
;
793 if (wb_request
.extra_len
> 0) {
794 wb_request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, wb_request
.extra_len
);
795 if (wb_request
.extra_data
.data
== NULL
) {
796 return NT_STATUS_NO_MEMORY
;
799 memcpy(wb_request
.extra_data
.data
, initial_msg
.data
, initial_msg
.length
);
800 memcpy(wb_request
.extra_data
.data
+ initial_msg
.length
,
801 challenge_msg
.data
, challenge_msg
.length
);
804 result
= winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH
, &wb_request
, &wb_response
);
805 SAFE_FREE(wb_request
.extra_data
.data
);
807 if (result
!= NSS_STATUS_SUCCESS
) {
808 winbindd_free_response(&wb_response
);
809 return NT_STATUS_UNSUCCESSFUL
;
813 *reply
= data_blob(wb_response
.extra_data
.data
,
814 wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
);
815 if (wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
> 0 &&
816 reply
->data
== NULL
) {
817 winbindd_free_response(&wb_response
);
818 return NT_STATUS_NO_MEMORY
;
822 winbindd_free_response(&wb_response
);
823 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
826 static void manage_squid_ntlmssp_request_int(struct ntlm_auth_state
*state
,
827 char *buf
, int length
,
831 DATA_BLOB request
, reply
;
834 if (strlen(buf
) < 2) {
835 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
836 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
840 if (strlen(buf
) > 3) {
841 if(strncmp(buf
, "SF ", 3) == 0){
842 DEBUG(10, ("Setting flags to negotioate\n"));
843 TALLOC_FREE(state
->want_feature_list
);
844 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
846 *response
= talloc_strdup(mem_ctx
, "OK");
849 request
= base64_decode_data_blob(buf
+ 3);
851 request
= data_blob_null
;
854 if ((strncmp(buf
, "PW ", 3) == 0)) {
855 /* The calling application wants us to use a local password
856 * (rather than winbindd) */
858 opt_password
= SMB_STRNDUP((const char *)request
.data
,
861 if (opt_password
== NULL
) {
862 DEBUG(1, ("Out of memory\n"));
863 *response
= talloc_strdup(mem_ctx
, "BH Out of memory");
864 data_blob_free(&request
);
868 *response
= talloc_strdup(mem_ctx
, "OK");
869 data_blob_free(&request
);
873 if (strncmp(buf
, "YR", 2) == 0) {
874 TALLOC_FREE(state
->ntlmssp_state
);
875 state
->svr_state
= SERVER_INITIAL
;
876 } else if (strncmp(buf
, "KK", 2) == 0) {
877 /* No special preprocessing required */
878 } else if (strncmp(buf
, "GF", 2) == 0) {
879 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
881 if (state
->svr_state
== SERVER_FINISHED
) {
882 *response
= talloc_asprintf(mem_ctx
, "GF 0x%08x",
886 *response
= talloc_strdup(mem_ctx
, "BH\n");
888 data_blob_free(&request
);
890 } else if (strncmp(buf
, "GK", 2) == 0) {
891 DEBUG(10, ("Requested NTLMSSP session key\n"));
892 if(state
->have_session_key
) {
893 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
895 *response
= talloc_asprintf(mem_ctx
, "GK %s",
896 key64
? key64
: "<NULL>");
899 *response
= talloc_strdup(mem_ctx
, "BH");
902 data_blob_free(&request
);
905 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
906 *response
= talloc_strdup(mem_ctx
, "BH NTLMSSP query invalid");
910 if (!state
->ntlmssp_state
) {
911 nt_status
= ntlm_auth_start_ntlmssp_server(
912 &state
->ntlmssp_state
);
913 if (!NT_STATUS_IS_OK(nt_status
)) {
914 *response
= talloc_asprintf(
915 mem_ctx
, "BH %s", nt_errstr(nt_status
));
918 ntlmssp_want_feature_list(state
->ntlmssp_state
,
919 state
->want_feature_list
);
922 DEBUG(10, ("got NTLMSSP packet:\n"));
923 dump_data(10, request
.data
, request
.length
);
925 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
, &reply
);
927 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
928 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
930 *response
= talloc_asprintf(mem_ctx
, "TT %s", reply_base64
);
931 TALLOC_FREE(reply_base64
);
932 data_blob_free(&reply
);
933 state
->svr_state
= SERVER_CHALLENGE
;
934 DEBUG(10, ("NTLMSSP challenge\n"));
935 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
936 *response
= talloc_asprintf(mem_ctx
, "BH %s",
937 nt_errstr(nt_status
));
938 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
940 TALLOC_FREE(state
->ntlmssp_state
);
941 } else if (!NT_STATUS_IS_OK(nt_status
)) {
942 *response
= talloc_asprintf(mem_ctx
, "NA %s",
943 nt_errstr(nt_status
));
944 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
946 *response
= talloc_asprintf(
948 (char *)state
->ntlmssp_state
->callback_private
);
949 DEBUG(10, ("NTLMSSP OK!\n"));
951 if(state
->have_session_key
)
952 data_blob_free(&state
->session_key
);
953 state
->session_key
= data_blob(
954 state
->ntlmssp_state
->session_key
.data
,
955 state
->ntlmssp_state
->session_key
.length
);
956 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
957 state
->have_session_key
= true;
958 state
->svr_state
= SERVER_FINISHED
;
961 data_blob_free(&request
);
964 static void manage_squid_ntlmssp_request(struct ntlm_auth_state
*state
,
965 char *buf
, int length
)
969 manage_squid_ntlmssp_request_int(state
, buf
, length
,
970 talloc_tos(), &response
);
972 if (response
== NULL
) {
973 x_fprintf(x_stdout
, "BH Out of memory\n");
976 x_fprintf(x_stdout
, "%s\n", response
);
977 TALLOC_FREE(response
);
980 static void manage_client_ntlmssp_request(struct ntlm_auth_state
*state
,
981 char *buf
, int length
)
983 DATA_BLOB request
, reply
;
986 if (!opt_username
|| !*opt_username
) {
987 x_fprintf(x_stderr
, "username must be specified!\n\n");
991 if (strlen(buf
) < 2) {
992 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
993 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
997 if (strlen(buf
) > 3) {
998 if(strncmp(buf
, "SF ", 3) == 0) {
999 DEBUG(10, ("Looking for flags to negotiate\n"));
1000 talloc_free(state
->want_feature_list
);
1001 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
1003 x_fprintf(x_stdout
, "OK\n");
1006 request
= base64_decode_data_blob(buf
+ 3);
1008 request
= data_blob_null
;
1011 if (strncmp(buf
, "PW ", 3) == 0) {
1012 /* We asked for a password and obviously got it :-) */
1014 opt_password
= SMB_STRNDUP((const char *)request
.data
,
1017 if (opt_password
== NULL
) {
1018 DEBUG(1, ("Out of memory\n"));
1019 x_fprintf(x_stdout
, "BH Out of memory\n");
1020 data_blob_free(&request
);
1024 x_fprintf(x_stdout
, "OK\n");
1025 data_blob_free(&request
);
1029 if (!state
->ntlmssp_state
&& use_cached_creds
) {
1030 /* check whether cached credentials are usable. */
1031 DATA_BLOB empty_blob
= data_blob_null
;
1033 nt_status
= do_ccache_ntlm_auth(empty_blob
, empty_blob
, NULL
);
1034 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1035 /* failed to use cached creds */
1036 use_cached_creds
= False
;
1040 if (opt_password
== NULL
&& !use_cached_creds
) {
1041 /* Request a password from the calling process. After
1042 sending it, the calling process should retry asking for the
1045 DEBUG(10, ("Requesting password\n"));
1046 x_fprintf(x_stdout
, "PW\n");
1050 if (strncmp(buf
, "YR", 2) == 0) {
1051 TALLOC_FREE(state
->ntlmssp_state
);
1052 state
->cli_state
= CLIENT_INITIAL
;
1053 } else if (strncmp(buf
, "TT", 2) == 0) {
1054 /* No special preprocessing required */
1055 } else if (strncmp(buf
, "GF", 2) == 0) {
1056 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1058 if(state
->cli_state
== CLIENT_FINISHED
) {
1059 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
1062 x_fprintf(x_stdout
, "BH\n");
1065 data_blob_free(&request
);
1067 } else if (strncmp(buf
, "GK", 2) == 0 ) {
1068 DEBUG(10, ("Requested session key\n"));
1070 if(state
->cli_state
== CLIENT_FINISHED
) {
1071 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
1072 state
->session_key
);
1073 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
1077 x_fprintf(x_stdout
, "BH\n");
1080 data_blob_free(&request
);
1083 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf
));
1084 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
1088 if (!state
->ntlmssp_state
) {
1089 nt_status
= ntlm_auth_start_ntlmssp_client(
1090 &state
->ntlmssp_state
);
1091 if (!NT_STATUS_IS_OK(nt_status
)) {
1092 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1095 ntlmssp_want_feature_list(state
->ntlmssp_state
,
1096 state
->want_feature_list
);
1097 state
->initial_message
= data_blob_null
;
1100 DEBUG(10, ("got NTLMSSP packet:\n"));
1101 dump_data(10, request
.data
, request
.length
);
1103 if (use_cached_creds
&& !opt_password
&&
1104 (state
->cli_state
== CLIENT_RESPONSE
)) {
1105 nt_status
= do_ccache_ntlm_auth(state
->initial_message
, request
,
1108 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
,
1112 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1113 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
1115 if (state
->cli_state
== CLIENT_INITIAL
) {
1116 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
1117 state
->initial_message
= reply
;
1118 state
->cli_state
= CLIENT_RESPONSE
;
1120 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
1121 data_blob_free(&reply
);
1123 TALLOC_FREE(reply_base64
);
1124 DEBUG(10, ("NTLMSSP challenge\n"));
1125 } else if (NT_STATUS_IS_OK(nt_status
)) {
1126 char *reply_base64
= base64_encode_data_blob(talloc_tos(),
1128 x_fprintf(x_stdout
, "AF %s\n", reply_base64
);
1129 TALLOC_FREE(reply_base64
);
1131 if(state
->have_session_key
)
1132 data_blob_free(&state
->session_key
);
1134 state
->session_key
= data_blob(
1135 state
->ntlmssp_state
->session_key
.data
,
1136 state
->ntlmssp_state
->session_key
.length
);
1137 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
1138 state
->have_session_key
= true;
1140 DEBUG(10, ("NTLMSSP OK!\n"));
1141 state
->cli_state
= CLIENT_FINISHED
;
1142 TALLOC_FREE(state
->ntlmssp_state
);
1144 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1145 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
1146 state
->cli_state
= CLIENT_ERROR
;
1147 TALLOC_FREE(state
->ntlmssp_state
);
1150 data_blob_free(&request
);
1153 static void manage_squid_basic_request(struct ntlm_auth_state
*state
,
1154 char *buf
, int length
)
1159 pass
=(char *)memchr(buf
,' ',length
);
1161 DEBUG(2, ("Password not found. Denying access\n"));
1162 x_fprintf(x_stdout
, "ERR\n");
1168 if (state
->helper_mode
== SQUID_2_5_BASIC
) {
1169 rfc1738_unescape(user
);
1170 rfc1738_unescape(pass
);
1173 if (check_plaintext_auth(user
, pass
, False
)) {
1174 x_fprintf(x_stdout
, "OK\n");
1176 x_fprintf(x_stdout
, "ERR\n");
1180 static void offer_gss_spnego_mechs(void) {
1183 struct spnego_data spnego
;
1186 TALLOC_CTX
*ctx
= talloc_tos();
1190 ZERO_STRUCT(spnego
);
1192 myname_lower
= talloc_strdup(ctx
, lp_netbios_name());
1193 if (!myname_lower
) {
1196 strlower_m(myname_lower
);
1198 principal
= talloc_asprintf(ctx
, "%s$@%s", myname_lower
, lp_realm());
1203 /* Server negTokenInit (mech offerings) */
1204 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1205 spnego
.negTokenInit
.mechTypes
= talloc_array(ctx
, const char *, 4);
1207 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_KERBEROS5_OLD
);
1208 spnego
.negTokenInit
.mechTypes
[1] = talloc_strdup(ctx
, OID_KERBEROS5
);
1209 spnego
.negTokenInit
.mechTypes
[2] = talloc_strdup(ctx
, OID_NTLMSSP
);
1210 spnego
.negTokenInit
.mechTypes
[3] = NULL
;
1212 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_NTLMSSP
);
1213 spnego
.negTokenInit
.mechTypes
[1] = NULL
;
1217 spnego
.negTokenInit
.mechListMIC
= data_blob_talloc(ctx
, principal
,
1220 len
= spnego_write_data(ctx
, &token
, &spnego
);
1221 spnego_free_data(&spnego
);
1224 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1225 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1229 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1230 x_fprintf(x_stdout
, "TT %s *\n", reply_base64
);
1232 TALLOC_FREE(reply_base64
);
1233 data_blob_free(&token
);
1234 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
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 TALLOC_FREE(state
->ntlmssp_state
);
1265 TALLOC_FREE(state
->spnego_mech
);
1266 TALLOC_FREE(state
->spnego_mech_oid
);
1267 } else if (strncmp(buf
, "KK", 2) == 0) {
1270 DEBUG(1, ("SPENGO query [%s] invalid\n", buf
));
1271 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1275 if ( (strlen(buf
) == 2)) {
1277 /* no client data, get the negTokenInit offering
1280 offer_gss_spnego_mechs();
1284 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1286 if (strlen(buf
) <= 3) {
1287 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1288 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1292 token
= base64_decode_data_blob(buf
+ 3);
1294 if ((token
.length
>= 7)
1295 && (strncmp((char *)token
.data
, "NTLMSSP", 7) == 0)) {
1298 data_blob_free(&token
);
1300 DEBUG(10, ("Could not parse GSS-SPNEGO, trying raw "
1303 manage_squid_ntlmssp_request_int(state
, buf
, length
,
1304 talloc_tos(), &reply
);
1305 if (reply
== NULL
) {
1306 x_fprintf(x_stdout
, "BH Out of memory\n");
1310 if (strncmp(reply
, "AF ", 3) == 0) {
1311 x_fprintf(x_stdout
, "AF * %s\n", reply
+3);
1313 x_fprintf(x_stdout
, "%s *\n", reply
);
1320 ZERO_STRUCT(request
);
1321 len
= spnego_read_data(ctx
, token
, &request
);
1322 data_blob_free(&token
);
1325 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1326 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1330 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1338 if (state
->spnego_mech
) {
1339 DEBUG(1, ("Client restarted SPNEGO with NegTokenInit "
1340 "while mech[%s] was already negotiated\n",
1341 state
->spnego_mech
));
1342 x_fprintf(x_stdout
, "BH Client send NegTokenInit twice\n");
1346 /* Second request from Client. This is where the
1347 client offers its mechanism to use. */
1349 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
1350 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
1351 DEBUG(1, ("Client did not offer any mechanism\n"));
1352 x_fprintf(x_stdout
, "BH Client did not offer any "
1357 status
= NT_STATUS_UNSUCCESSFUL
;
1358 for (i
= 0; request
.negTokenInit
.mechTypes
[i
] != NULL
; i
++) {
1359 DEBUG(10,("got mech[%d][%s]\n",
1360 i
, request
.negTokenInit
.mechTypes
[i
]));
1362 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5_OLD
) == 0) {
1366 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_KERBEROS5
) == 0) {
1371 if (strcmp(request
.negTokenInit
.mechTypes
[i
], OID_NTLMSSP
) == 0) {
1377 used_idx
= ntlm_idx
;
1379 if (krb5_idx
!= -1) {
1381 used_idx
= krb5_idx
;
1384 if (ntlm_idx
> -1) {
1385 state
->spnego_mech
= talloc_strdup(state
, "ntlmssp");
1386 if (state
->spnego_mech
== NULL
) {
1387 x_fprintf(x_stdout
, "BH Out of memory\n");
1391 if (state
->ntlmssp_state
) {
1392 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1393 "already got one\n"));
1394 x_fprintf(x_stdout
, "BH Client wants a new "
1395 "NTLMSSP challenge, but "
1396 "already got one\n");
1397 TALLOC_FREE(state
->ntlmssp_state
);
1401 status
= ntlm_auth_start_ntlmssp_server(&state
->ntlmssp_state
);
1402 if (!NT_STATUS_IS_OK(status
)) {
1403 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1409 if (krb5_idx
> -1) {
1410 state
->spnego_mech
= talloc_strdup(state
, "krb5");
1411 if (state
->spnego_mech
== NULL
) {
1412 x_fprintf(x_stdout
, "BH Out of memory\n");
1417 if (used_idx
> -1) {
1418 state
->spnego_mech_oid
= talloc_strdup(state
,
1419 request
.negTokenInit
.mechTypes
[used_idx
]);
1420 if (state
->spnego_mech_oid
== NULL
) {
1421 x_fprintf(x_stdout
, "BH Out of memory\n");
1424 supportedMech
= talloc_strdup(ctx
, state
->spnego_mech_oid
);
1425 if (supportedMech
== NULL
) {
1426 x_fprintf(x_stdout
, "BH Out of memory\n");
1430 status
= NT_STATUS_MORE_PROCESSING_REQUIRED
;
1432 status
= NT_STATUS_NOT_SUPPORTED
;
1434 if (used_idx
== 0) {
1435 status
= NT_STATUS_OK
;
1436 raw_in_token
= request
.negTokenInit
.mechToken
;
1439 if (state
->spnego_mech
== NULL
) {
1440 DEBUG(1,("Got netTokenTarg without negTokenInit\n"));
1441 x_fprintf(x_stdout
, "BH Got a negTokenTarg without "
1446 if ((request
.negTokenTarg
.supportedMech
!= NULL
) &&
1447 (strcmp(request
.negTokenTarg
.supportedMech
, state
->spnego_mech_oid
) != 0 ) ) {
1448 DEBUG(1, ("Got a negTokenTarg with mech[%s] while [%s] was already negotiated\n",
1449 request
.negTokenTarg
.supportedMech
,
1450 state
->spnego_mech_oid
));
1451 x_fprintf(x_stdout
, "BH Got a negTokenTarg with speficied mech\n");
1455 status
= NT_STATUS_OK
;
1456 raw_in_token
= request
.negTokenTarg
.responseToken
;
1459 if (!NT_STATUS_IS_OK(status
)) {
1460 /* error or more processing */
1461 } else if (strcmp(state
->spnego_mech
, "ntlmssp") == 0) {
1463 DEBUG(10, ("got NTLMSSP packet:\n"));
1464 dump_data(10, raw_in_token
.data
, raw_in_token
.length
);
1466 status
= ntlmssp_update(state
->ntlmssp_state
,
1469 if (NT_STATUS_IS_OK(status
)) {
1470 user
= talloc_strdup(ctx
, state
->ntlmssp_state
->user
);
1471 domain
= talloc_strdup(ctx
, state
->ntlmssp_state
->domain
);
1473 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1474 TALLOC_FREE(state
->ntlmssp_state
);
1477 } else if (strcmp(state
->spnego_mech
, "krb5") == 0) {
1480 DATA_BLOB session_key
;
1481 struct PAC_LOGON_INFO
*logon_info
= NULL
;
1485 if (!spnego_parse_krb5_wrap(ctx
, raw_in_token
,
1487 DEBUG(1, ("spnego_parse_krb5_wrap failed\n"));
1488 x_fprintf(x_stdout
, "BH spnego_parse_krb5_wrap failed\n");
1492 status
= ads_verify_ticket(ctx
, lp_realm(), 0,
1494 &principal
, &logon_info
, &ap_rep
,
1495 &session_key
, True
);
1497 /* Now in "principal" we have the name we are authenticated as. */
1499 if (NT_STATUS_IS_OK(status
)) {
1501 domain
= strchr_m(principal
, '@');
1503 if (domain
== NULL
) {
1504 DEBUG(1, ("Did not get a valid principal "
1505 "from ads_verify_ticket\n"));
1506 x_fprintf(x_stdout
, "BH Did not get a "
1507 "valid principal from "
1508 "ads_verify_ticket\n");
1513 domain
= talloc_strdup(ctx
, domain
);
1514 user
= talloc_strdup(ctx
, principal
);
1517 netsamlogon_cache_store(
1518 user
, &logon_info
->info3
);
1521 data_blob_free(&ap_rep
);
1522 data_blob_free(&session_key
);
1524 data_blob_free(&ticket
);
1528 spnego_free_data(&request
);
1529 ZERO_STRUCT(response
);
1530 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1532 if (NT_STATUS_IS_OK(status
)) {
1533 TALLOC_FREE(state
->spnego_mech
);
1534 TALLOC_FREE(state
->spnego_mech_oid
);
1535 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
1536 response
.negTokenTarg
.responseToken
= raw_out_token
;
1538 reply_argument
= talloc_asprintf(ctx
, "%s\\%s", domain
, user
);
1539 } else if (NT_STATUS_EQUAL(status
,
1540 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1541 response
.negTokenTarg
.supportedMech
= supportedMech
;
1542 response
.negTokenTarg
.responseToken
= raw_out_token
;
1543 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1545 reply_argument
= talloc_strdup(ctx
, "*");
1547 TALLOC_FREE(state
->spnego_mech
);
1548 TALLOC_FREE(state
->spnego_mech_oid
);
1549 data_blob_free(&raw_out_token
);
1550 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1552 reply_argument
= talloc_strdup(ctx
, nt_errstr(status
));
1555 if (!reply_argument
) {
1556 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1557 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1558 spnego_free_data(&response
);
1562 len
= spnego_write_data(ctx
, &token
, &response
);
1563 spnego_free_data(&response
);
1566 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1567 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1571 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1573 x_fprintf(x_stdout
, "%s %s %s\n",
1574 reply_code
, reply_base64
, reply_argument
);
1576 TALLOC_FREE(reply_base64
);
1577 data_blob_free(&token
);
1582 static struct ntlmssp_state
*client_ntlmssp_state
= NULL
;
1584 static bool manage_client_ntlmssp_init(struct spnego_data spnego
)
1587 DATA_BLOB null_blob
= data_blob_null
;
1588 DATA_BLOB to_server
;
1589 char *to_server_base64
;
1590 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1591 TALLOC_CTX
*ctx
= talloc_tos();
1593 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1595 if (client_ntlmssp_state
!= NULL
) {
1596 DEBUG(1, ("Request for initial SPNEGO request where "
1597 "we already have a state\n"));
1601 if (!client_ntlmssp_state
) {
1602 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1603 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1609 if (opt_password
== NULL
) {
1611 /* Request a password from the calling process. After
1612 sending it, the calling process should retry with
1613 the negTokenInit. */
1615 DEBUG(10, ("Requesting password\n"));
1616 x_fprintf(x_stdout
, "PW\n");
1620 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1621 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1622 spnego
.negTokenInit
.reqFlags
= data_blob_null
;
1623 spnego
.negTokenInit
.reqFlagsPadding
= 0;
1624 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1626 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1627 &spnego
.negTokenInit
.mechToken
);
1629 if ( !(NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) ||
1630 NT_STATUS_IS_OK(status
)) ) {
1631 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1632 nt_errstr(status
)));
1633 TALLOC_FREE(client_ntlmssp_state
);
1637 spnego_write_data(ctx
, &to_server
, &spnego
);
1638 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1640 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1641 data_blob_free(&to_server
);
1642 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1643 TALLOC_FREE(to_server_base64
);
1647 static void manage_client_ntlmssp_targ(struct spnego_data spnego
)
1650 DATA_BLOB null_blob
= data_blob_null
;
1652 DATA_BLOB to_server
;
1653 char *to_server_base64
;
1654 TALLOC_CTX
*ctx
= talloc_tos();
1656 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1658 if (client_ntlmssp_state
== NULL
) {
1659 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1660 x_fprintf(x_stdout
, "BH Got NTLMSSP tArg without a client state\n");
1664 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1665 x_fprintf(x_stdout
, "NA\n");
1666 TALLOC_FREE(client_ntlmssp_state
);
1670 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1671 x_fprintf(x_stdout
, "AF\n");
1672 TALLOC_FREE(client_ntlmssp_state
);
1676 status
= ntlmssp_update(client_ntlmssp_state
,
1677 spnego
.negTokenTarg
.responseToken
,
1680 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1681 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1682 "ntlmssp_client_update, got: %s\n",
1683 nt_errstr(status
)));
1684 x_fprintf(x_stdout
, "BH Expected MORE_PROCESSING_REQUIRED from "
1685 "ntlmssp_client_update\n");
1686 data_blob_free(&request
);
1687 TALLOC_FREE(client_ntlmssp_state
);
1691 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1692 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1693 spnego
.negTokenTarg
.supportedMech
= (const char *)OID_NTLMSSP
;
1694 spnego
.negTokenTarg
.responseToken
= request
;
1695 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1697 spnego_write_data(ctx
, &to_server
, &spnego
);
1698 data_blob_free(&request
);
1700 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1701 data_blob_free(&to_server
);
1702 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1703 TALLOC_FREE(to_server_base64
);
1709 static bool manage_client_krb5_init(struct spnego_data spnego
)
1712 DATA_BLOB tkt
, to_server
;
1713 DATA_BLOB session_key_krb5
= data_blob_null
;
1714 struct spnego_data reply
;
1718 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1720 TALLOC_CTX
*ctx
= talloc_tos();
1722 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1723 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1724 DEBUG(1, ("Did not get a principal for krb5\n"));
1728 principal
= (char *)SMB_MALLOC(
1729 spnego
.negTokenInit
.mechListMIC
.length
+1);
1731 if (principal
== NULL
) {
1732 DEBUG(1, ("Could not malloc principal\n"));
1736 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1737 spnego
.negTokenInit
.mechListMIC
.length
);
1738 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1740 retval
= cli_krb5_get_ticket(ctx
, principal
, 0,
1741 &tkt
, &session_key_krb5
,
1742 0, NULL
, NULL
, NULL
);
1746 /* Let's try to first get the TGT, for that we need a
1749 if (opt_password
== NULL
) {
1750 DEBUG(10, ("Requesting password\n"));
1751 x_fprintf(x_stdout
, "PW\n");
1755 user
= talloc_asprintf(talloc_tos(), "%s@%s", opt_username
, opt_domain
);
1760 if ((retval
= kerberos_kinit_password(user
, opt_password
, 0, NULL
))) {
1761 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval
)));
1765 retval
= cli_krb5_get_ticket(ctx
, principal
, 0,
1766 &tkt
, &session_key_krb5
,
1767 0, NULL
, NULL
, NULL
);
1769 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1774 data_blob_free(&session_key_krb5
);
1778 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1779 reply
.negTokenInit
.mechTypes
= my_mechs
;
1780 reply
.negTokenInit
.reqFlags
= data_blob_null
;
1781 reply
.negTokenInit
.reqFlagsPadding
= 0;
1782 reply
.negTokenInit
.mechToken
= tkt
;
1783 reply
.negTokenInit
.mechListMIC
= data_blob_null
;
1785 len
= spnego_write_data(ctx
, &to_server
, &reply
);
1786 data_blob_free(&tkt
);
1789 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1793 reply_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1794 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1796 TALLOC_FREE(reply_base64
);
1797 data_blob_free(&to_server
);
1798 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1802 static void manage_client_krb5_targ(struct spnego_data spnego
)
1804 switch (spnego
.negTokenTarg
.negResult
) {
1805 case SPNEGO_ACCEPT_INCOMPLETE
:
1806 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1807 x_fprintf(x_stdout
, "BH Got a Kerberos negTokenTarg with "
1808 "ACCEPT_INCOMPLETE\n");
1810 case SPNEGO_ACCEPT_COMPLETED
:
1811 DEBUG(10, ("Accept completed\n"));
1812 x_fprintf(x_stdout
, "AF\n");
1815 DEBUG(10, ("Rejected\n"));
1816 x_fprintf(x_stdout
, "NA\n");
1819 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1820 x_fprintf(x_stdout
, "AF\n");
1826 static void manage_gss_spnego_client_request(struct ntlm_auth_state
*state
,
1827 char *buf
, int length
)
1830 struct spnego_data spnego
;
1832 TALLOC_CTX
*ctx
= talloc_tos();
1834 if (!opt_username
|| !*opt_username
) {
1835 x_fprintf(x_stderr
, "username must be specified!\n\n");
1839 if (strlen(buf
) <= 3) {
1840 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1841 x_fprintf(x_stdout
, "BH SPNEGO query too short\n");
1845 request
= base64_decode_data_blob(buf
+3);
1847 if (strncmp(buf
, "PW ", 3) == 0) {
1849 /* We asked for a password and obviously got it :-) */
1851 opt_password
= SMB_STRNDUP((const char *)request
.data
, request
.length
);
1853 if (opt_password
== NULL
) {
1854 DEBUG(1, ("Out of memory\n"));
1855 x_fprintf(x_stdout
, "BH Out of memory\n");
1856 data_blob_free(&request
);
1860 x_fprintf(x_stdout
, "OK\n");
1861 data_blob_free(&request
);
1865 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1866 (strncmp(buf
, "AF ", 3) != 0) &&
1867 (strncmp(buf
, "NA ", 3) != 0) ) {
1868 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1869 x_fprintf(x_stdout
, "BH SPNEGO request invalid\n");
1870 data_blob_free(&request
);
1874 /* So we got a server challenge to generate a SPNEGO
1875 client-to-server request... */
1877 len
= spnego_read_data(ctx
, request
, &spnego
);
1878 data_blob_free(&request
);
1881 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1882 x_fprintf(x_stdout
, "BH Could not read SPNEGO data\n");
1886 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1888 /* The server offers a list of mechanisms */
1890 const char **mechType
= (const char **)spnego
.negTokenInit
.mechTypes
;
1892 while (*mechType
!= NULL
) {
1895 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1896 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1897 if (manage_client_krb5_init(spnego
))
1902 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1903 if (manage_client_ntlmssp_init(spnego
))
1910 DEBUG(1, ("Server offered no compatible mechanism\n"));
1911 x_fprintf(x_stdout
, "BH Server offered no compatible mechanism\n");
1915 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1917 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1918 /* On accept/reject Windows does not send the
1919 mechanism anymore. Handle that here and
1920 shut down the mechanisms. */
1922 switch (spnego
.negTokenTarg
.negResult
) {
1923 case SPNEGO_ACCEPT_COMPLETED
:
1924 x_fprintf(x_stdout
, "AF\n");
1927 x_fprintf(x_stdout
, "NA\n");
1930 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1931 "unknown negResult: %d\n",
1932 spnego
.negTokenTarg
.negResult
));
1933 x_fprintf(x_stdout
, "BH Got a negTokenTarg with"
1934 " no mech and an unknown "
1938 TALLOC_FREE(client_ntlmssp_state
);
1942 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1943 OID_NTLMSSP
) == 0) {
1944 manage_client_ntlmssp_targ(spnego
);
1949 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1950 OID_KERBEROS5_OLD
) == 0) {
1951 manage_client_krb5_targ(spnego
);
1958 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1959 x_fprintf(x_stdout
, "BH Got an SPNEGO token I could not handle\n");
1963 spnego_free_data(&spnego
);
1967 static void manage_ntlm_server_1_request(struct ntlm_auth_state
*state
,
1968 char *buf
, int length
)
1970 char *request
, *parameter
;
1971 static DATA_BLOB challenge
;
1972 static DATA_BLOB lm_response
;
1973 static DATA_BLOB nt_response
;
1974 static char *full_username
;
1975 static char *username
;
1976 static char *domain
;
1977 static char *plaintext_password
;
1978 static bool ntlm_server_1_user_session_key
;
1979 static bool ntlm_server_1_lm_session_key
;
1981 if (strequal(buf
, ".")) {
1982 if (!full_username
&& !username
) {
1983 x_fprintf(x_stdout
, "Error: No username supplied!\n");
1984 } else if (plaintext_password
) {
1985 /* handle this request as plaintext */
1986 if (!full_username
) {
1987 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
1988 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
1992 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
1993 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1995 x_fprintf(x_stdout
, "Authenticated: No\n");
1997 } else if (!lm_response
.data
&& !nt_response
.data
) {
1998 x_fprintf(x_stdout
, "Error: No password supplied!\n");
1999 } else if (!challenge
.data
) {
2000 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
2002 char *error_string
= NULL
;
2004 uchar user_session_key
[16];
2007 if (full_username
&& !username
) {
2009 fstring fstr_domain
;
2011 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
2012 /* username might be 'tainted', don't print into our new-line deleimianted stream */
2013 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
2015 SAFE_FREE(username
);
2017 username
= smb_xstrdup(fstr_user
);
2018 domain
= smb_xstrdup(fstr_domain
);
2022 domain
= smb_xstrdup(get_winbind_domain());
2025 if (ntlm_server_1_lm_session_key
)
2026 flags
|= WBFLAG_PAM_LMKEY
;
2028 if (ntlm_server_1_user_session_key
)
2029 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2031 if (!NT_STATUS_IS_OK(
2032 contact_winbind_auth_crap(username
,
2044 x_fprintf(x_stdout
, "Authenticated: No\n");
2045 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
2047 static char zeros
[16];
2049 char *hex_user_session_key
;
2051 x_fprintf(x_stdout
, "Authenticated: Yes\n");
2053 if (ntlm_server_1_lm_session_key
2054 && (memcmp(zeros
, lm_key
,
2055 sizeof(lm_key
)) != 0)) {
2056 hex_lm_key
= hex_encode_talloc(NULL
,
2057 (const unsigned char *)lm_key
,
2059 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
2060 TALLOC_FREE(hex_lm_key
);
2063 if (ntlm_server_1_user_session_key
2064 && (memcmp(zeros
, user_session_key
,
2065 sizeof(user_session_key
)) != 0)) {
2066 hex_user_session_key
= hex_encode_talloc(NULL
,
2067 (const unsigned char *)user_session_key
,
2068 sizeof(user_session_key
));
2069 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
2070 TALLOC_FREE(hex_user_session_key
);
2073 SAFE_FREE(error_string
);
2075 /* clear out the state */
2076 challenge
= data_blob_null
;
2077 nt_response
= data_blob_null
;
2078 lm_response
= data_blob_null
;
2079 SAFE_FREE(full_username
);
2080 SAFE_FREE(username
);
2082 SAFE_FREE(plaintext_password
);
2083 ntlm_server_1_user_session_key
= False
;
2084 ntlm_server_1_lm_session_key
= False
;
2085 x_fprintf(x_stdout
, ".\n");
2092 /* Indicates a base64 encoded structure */
2093 parameter
= strstr_m(request
, ":: ");
2095 parameter
= strstr_m(request
, ": ");
2098 DEBUG(0, ("Parameter not found!\n"));
2099 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2116 base64_decode_inplace(parameter
);
2119 if (strequal(request
, "LANMAN-Challenge")) {
2120 challenge
= strhex_to_data_blob(NULL
, parameter
);
2121 if (challenge
.length
!= 8) {
2122 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
2124 (int)challenge
.length
);
2125 challenge
= data_blob_null
;
2127 } else if (strequal(request
, "NT-Response")) {
2128 nt_response
= strhex_to_data_blob(NULL
, parameter
);
2129 if (nt_response
.length
< 24) {
2130 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
2132 (int)nt_response
.length
);
2133 nt_response
= data_blob_null
;
2135 } else if (strequal(request
, "LANMAN-Response")) {
2136 lm_response
= strhex_to_data_blob(NULL
, parameter
);
2137 if (lm_response
.length
!= 24) {
2138 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2140 (int)lm_response
.length
);
2141 lm_response
= data_blob_null
;
2143 } else if (strequal(request
, "Password")) {
2144 plaintext_password
= smb_xstrdup(parameter
);
2145 } else if (strequal(request
, "NT-Domain")) {
2146 domain
= smb_xstrdup(parameter
);
2147 } else if (strequal(request
, "Username")) {
2148 username
= smb_xstrdup(parameter
);
2149 } else if (strequal(request
, "Full-Username")) {
2150 full_username
= smb_xstrdup(parameter
);
2151 } else if (strequal(request
, "Request-User-Session-Key")) {
2152 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
2153 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
2154 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
2156 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2160 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
2161 char *buf
, int length
)
2163 char *request
, *parameter
;
2164 static DATA_BLOB new_nt_pswd
;
2165 static DATA_BLOB old_nt_hash_enc
;
2166 static DATA_BLOB new_lm_pswd
;
2167 static DATA_BLOB old_lm_hash_enc
;
2168 static char *full_username
= NULL
;
2169 static char *username
= NULL
;
2170 static char *domain
= NULL
;
2171 static char *newpswd
= NULL
;
2172 static char *oldpswd
= NULL
;
2174 if (strequal(buf
, ".")) {
2175 if(newpswd
&& oldpswd
) {
2176 uchar old_nt_hash
[16];
2177 uchar old_lm_hash
[16];
2178 uchar new_nt_hash
[16];
2179 uchar new_lm_hash
[16];
2181 new_nt_pswd
= data_blob(NULL
, 516);
2182 old_nt_hash_enc
= data_blob(NULL
, 16);
2184 /* Calculate the MD4 hash (NT compatible) of the
2186 E_md4hash(oldpswd
, old_nt_hash
);
2187 E_md4hash(newpswd
, new_nt_hash
);
2189 /* E_deshash returns false for 'long'
2190 passwords (> 14 DOS chars).
2192 Therefore, don't send a buffer
2193 encrypted with the truncated hash
2194 (it could allow an even easier
2195 attack on the password)
2197 Likewise, obey the admin's restriction
2200 if (lp_client_lanman_auth() &&
2201 E_deshash(newpswd
, new_lm_hash
) &&
2202 E_deshash(oldpswd
, old_lm_hash
)) {
2203 new_lm_pswd
= data_blob(NULL
, 516);
2204 old_lm_hash_enc
= data_blob(NULL
, 16);
2205 encode_pw_buffer(new_lm_pswd
.data
, newpswd
,
2208 arcfour_crypt(new_lm_pswd
.data
, old_nt_hash
, 516);
2209 E_old_pw_hash(new_nt_hash
, old_lm_hash
,
2210 old_lm_hash_enc
.data
);
2212 new_lm_pswd
.data
= NULL
;
2213 new_lm_pswd
.length
= 0;
2214 old_lm_hash_enc
.data
= NULL
;
2215 old_lm_hash_enc
.length
= 0;
2218 encode_pw_buffer(new_nt_pswd
.data
, newpswd
,
2221 arcfour_crypt(new_nt_pswd
.data
, old_nt_hash
, 516);
2222 E_old_pw_hash(new_nt_hash
, old_nt_hash
,
2223 old_nt_hash_enc
.data
);
2226 if (!full_username
&& !username
) {
2227 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2228 } else if ((!new_nt_pswd
.data
|| !old_nt_hash_enc
.data
) &&
2229 (!new_lm_pswd
.data
|| old_lm_hash_enc
.data
) ) {
2230 x_fprintf(x_stdout
, "Error: No NT or LM password "
2231 "blobs supplied!\n");
2233 char *error_string
= NULL
;
2235 if (full_username
&& !username
) {
2237 fstring fstr_domain
;
2239 if (!parse_ntlm_auth_domain_user(full_username
,
2242 /* username might be 'tainted', don't
2243 * print into our new-line
2244 * deleimianted stream */
2245 x_fprintf(x_stdout
, "Error: Could not "
2246 "parse into domain and "
2248 SAFE_FREE(username
);
2249 username
= smb_xstrdup(full_username
);
2251 SAFE_FREE(username
);
2253 username
= smb_xstrdup(fstr_user
);
2254 domain
= smb_xstrdup(fstr_domain
);
2259 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2266 x_fprintf(x_stdout
, "Password-Change: No\n");
2267 x_fprintf(x_stdout
, "Password-Change-Error: "
2268 "%s\n.\n", error_string
);
2270 x_fprintf(x_stdout
, "Password-Change: Yes\n");
2273 SAFE_FREE(error_string
);
2275 /* clear out the state */
2276 new_nt_pswd
= data_blob_null
;
2277 old_nt_hash_enc
= data_blob_null
;
2278 new_lm_pswd
= data_blob_null
;
2279 old_nt_hash_enc
= data_blob_null
;
2280 SAFE_FREE(full_username
);
2281 SAFE_FREE(username
);
2285 x_fprintf(x_stdout
, ".\n");
2292 /* Indicates a base64 encoded structure */
2293 parameter
= strstr_m(request
, ":: ");
2295 parameter
= strstr_m(request
, ": ");
2298 DEBUG(0, ("Parameter not found!\n"));
2299 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2315 base64_decode_inplace(parameter
);
2318 if (strequal(request
, "new-nt-password-blob")) {
2319 new_nt_pswd
= strhex_to_data_blob(NULL
, parameter
);
2320 if (new_nt_pswd
.length
!= 516) {
2321 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2322 "(got %d bytes, expected 516)\n.\n",
2324 (int)new_nt_pswd
.length
);
2325 new_nt_pswd
= data_blob_null
;
2327 } else if (strequal(request
, "old-nt-hash-blob")) {
2328 old_nt_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2329 if (old_nt_hash_enc
.length
!= 16) {
2330 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2331 "(got %d bytes, expected 16)\n.\n",
2333 (int)old_nt_hash_enc
.length
);
2334 old_nt_hash_enc
= data_blob_null
;
2336 } else if (strequal(request
, "new-lm-password-blob")) {
2337 new_lm_pswd
= strhex_to_data_blob(NULL
, parameter
);
2338 if (new_lm_pswd
.length
!= 516) {
2339 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2340 "(got %d bytes, expected 516)\n.\n",
2342 (int)new_lm_pswd
.length
);
2343 new_lm_pswd
= data_blob_null
;
2346 else if (strequal(request
, "old-lm-hash-blob")) {
2347 old_lm_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2348 if (old_lm_hash_enc
.length
!= 16)
2350 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2351 "(got %d bytes, expected 16)\n.\n",
2353 (int)old_lm_hash_enc
.length
);
2354 old_lm_hash_enc
= data_blob_null
;
2356 } else if (strequal(request
, "nt-domain")) {
2357 domain
= smb_xstrdup(parameter
);
2358 } else if(strequal(request
, "username")) {
2359 username
= smb_xstrdup(parameter
);
2360 } else if(strequal(request
, "full-username")) {
2361 username
= smb_xstrdup(parameter
);
2362 } else if(strequal(request
, "new-password")) {
2363 newpswd
= smb_xstrdup(parameter
);
2364 } else if (strequal(request
, "old-password")) {
2365 oldpswd
= smb_xstrdup(parameter
);
2367 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2371 static void manage_squid_request(struct ntlm_auth_state
*state
,
2372 stdio_helper_function fn
)
2375 char tmp
[INITIAL_BUFFER_SIZE
+1];
2376 int length
, buf_size
= 0;
2379 buf
= talloc_strdup(state
->mem_ctx
, "");
2381 DEBUG(0, ("Failed to allocate input buffer.\n"));
2382 x_fprintf(x_stderr
, "ERR\n");
2388 /* this is not a typo - x_fgets doesn't work too well under
2390 if (fgets(tmp
, sizeof(tmp
)-1, stdin
) == NULL
) {
2391 if (ferror(stdin
)) {
2392 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2393 "(%s)\n", ferror(stdin
),
2394 strerror(ferror(stdin
))));
2401 buf
= talloc_strdup_append_buffer(buf
, tmp
);
2402 buf_size
+= INITIAL_BUFFER_SIZE
;
2404 if (buf_size
> MAX_BUFFER_SIZE
) {
2405 DEBUG(2, ("Oversized message\n"));
2406 x_fprintf(x_stderr
, "ERR\n");
2411 c
= strchr(buf
, '\n');
2412 } while (c
== NULL
);
2417 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
2419 if (buf
[0] == '\0') {
2420 DEBUG(2, ("Invalid Request\n"));
2421 x_fprintf(x_stderr
, "ERR\n");
2426 fn(state
, buf
, length
);
2431 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
2432 TALLOC_CTX
*mem_ctx
;
2433 struct ntlm_auth_state
*state
;
2435 /* initialize FDescs */
2436 x_setbuf(x_stdout
, NULL
);
2437 x_setbuf(x_stderr
, NULL
);
2439 mem_ctx
= talloc_init("ntlm_auth");
2441 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2442 x_fprintf(x_stderr
, "ERR\n");
2446 state
= talloc_zero(mem_ctx
, struct ntlm_auth_state
);
2448 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2449 x_fprintf(x_stderr
, "ERR\n");
2453 state
->mem_ctx
= mem_ctx
;
2454 state
->helper_mode
= stdio_mode
;
2457 TALLOC_CTX
*frame
= talloc_stackframe();
2458 manage_squid_request(state
, fn
);
2464 /* Authenticate a user with a challenge/response */
2466 static bool check_auth_crap(void)
2471 char user_session_key
[16];
2473 char *hex_user_session_key
;
2475 static uint8 zeros
[16];
2477 x_setbuf(x_stdout
, NULL
);
2480 flags
|= WBFLAG_PAM_LMKEY
;
2482 if (request_user_session_key
)
2483 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2485 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
2487 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
2493 (unsigned char *)lm_key
,
2494 (unsigned char *)user_session_key
,
2495 &error_string
, NULL
);
2497 if (!NT_STATUS_IS_OK(nt_status
)) {
2498 x_fprintf(x_stdout
, "%s (0x%x)\n",
2500 NT_STATUS_V(nt_status
));
2501 SAFE_FREE(error_string
);
2506 && (memcmp(zeros
, lm_key
,
2507 sizeof(lm_key
)) != 0)) {
2508 hex_lm_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key
,
2510 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
2511 TALLOC_FREE(hex_lm_key
);
2513 if (request_user_session_key
2514 && (memcmp(zeros
, user_session_key
,
2515 sizeof(user_session_key
)) != 0)) {
2516 hex_user_session_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key
,
2517 sizeof(user_session_key
));
2518 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
2519 TALLOC_FREE(hex_user_session_key
);
2528 OPT_USERNAME
= 1000,
2537 OPT_USER_SESSION_KEY
,
2539 OPT_REQUIRE_MEMBERSHIP
,
2540 OPT_USE_CACHED_CREDS
,
2541 OPT_PAM_WINBIND_CONF
2544 int main(int argc
, const char **argv
)
2546 TALLOC_CTX
*frame
= talloc_stackframe();
2548 static const char *helper_protocol
;
2549 static int diagnostics
;
2551 static const char *hex_challenge
;
2552 static const char *hex_lm_response
;
2553 static const char *hex_nt_response
;
2557 /* NOTE: DO NOT change this interface without considering the implications!
2558 This is an external interface, which other programs will use to interact
2562 /* We do not use single-letter command abbreviations, because they harm future
2563 interface stability. */
2565 struct poptOption long_options
[] = {
2567 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
2568 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
2569 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
2570 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
2571 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
2572 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
2573 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2574 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
2575 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retrieve LM session key"},
2576 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retrieve User (NT) session key"},
2577 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "Use cached credentials if no password is given"},
2578 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
,
2580 "Perform diagnostics on the authentication chain"},
2581 { "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" },
2582 { "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" },
2583 POPT_COMMON_CONFIGFILE
2588 /* Samba client initialisation */
2591 setup_logging("ntlm_auth", DEBUG_STDERR
);
2595 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
2597 /* Parse command line options */
2600 poptPrintHelp(pc
, stderr
, 0);
2604 while((opt
= poptGetNextOpt(pc
)) != -1) {
2605 /* Get generic config options like --configfile */
2608 poptFreeContext(pc
);
2610 if (!lp_load_global(get_dyn_CONFIGFILE())) {
2611 d_fprintf(stderr
, "ntlm_auth: error opening config file %s. Error was %s\n",
2612 get_dyn_CONFIGFILE(), strerror(errno
));
2616 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2617 POPT_CONTEXT_KEEP_FIRST
);
2619 while((opt
= poptGetNextOpt(pc
)) != -1) {
2622 opt_challenge
= strhex_to_data_blob(NULL
, hex_challenge
);
2623 if (opt_challenge
.length
!= 8) {
2624 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2626 (int)opt_challenge
.length
);
2631 opt_lm_response
= strhex_to_data_blob(NULL
, hex_lm_response
);
2632 if (opt_lm_response
.length
!= 24) {
2633 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2635 (int)opt_lm_response
.length
);
2641 opt_nt_response
= strhex_to_data_blob(NULL
, hex_nt_response
);
2642 if (opt_nt_response
.length
< 24) {
2643 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2645 (int)opt_nt_response
.length
);
2650 case OPT_REQUIRE_MEMBERSHIP
:
2651 if (strncasecmp_m("S-", require_membership_of
, 2) == 0) {
2652 require_membership_of_sid
= require_membership_of
;
2659 char *domain
= SMB_STRDUP(opt_username
);
2660 char *p
= strchr_m(domain
, *lp_winbind_separator());
2664 if (opt_domain
&& !strequal(opt_domain
, domain
)) {
2665 x_fprintf(x_stderr
, "Domain specified in username (%s) "
2666 "doesn't match specified domain (%s)!\n\n",
2667 domain
, opt_domain
);
2668 poptPrintHelp(pc
, stderr
, 0);
2671 opt_domain
= domain
;
2677 /* Note: if opt_domain is "" then send no domain */
2678 if (opt_domain
== NULL
) {
2679 opt_domain
= get_winbind_domain();
2682 if (opt_workstation
== NULL
) {
2683 opt_workstation
= "";
2686 if (helper_protocol
) {
2688 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2689 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
2690 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
2694 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
2696 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2697 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
2703 if (!opt_username
|| !*opt_username
) {
2704 x_fprintf(x_stderr
, "username must be specified!\n\n");
2705 poptPrintHelp(pc
, stderr
, 0);
2709 if (opt_challenge
.length
) {
2710 if (!check_auth_crap()) {
2716 if (!opt_password
) {
2717 opt_password
= getpass("password: ");
2721 if (!diagnose_ntlm_auth()) {
2727 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
2728 if (!check_plaintext_auth(user
, opt_password
, True
)) {
2735 poptFreeContext(pc
);