2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
9 Copyright (C) Robert O'Callahan 2006 (added cached credential code).
10 Copyright (C) Kai Blin <kai@samba.org> 2008
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "utils/ntlm_auth.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../libcli/auth/spnego.h"
32 #include <iniparser.h>
34 #ifndef PAM_WINBIND_CONFIG_FILE
35 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
38 #define WINBIND_KRB5_AUTH 0x00000080
41 #define DBGC_CLASS DBGC_WINBIND
43 #define INITIAL_BUFFER_SIZE 300
44 #define MAX_BUFFER_SIZE 630000
46 enum stdio_helper_mode
{
54 NTLM_CHANGE_PASSWORD_1
,
58 enum ntlm_auth_cli_state
{
65 enum ntlm_auth_svr_state
{
72 struct ntlm_auth_state
{
74 enum stdio_helper_mode helper_mode
;
75 enum ntlm_auth_cli_state cli_state
;
76 enum ntlm_auth_svr_state svr_state
;
77 struct ntlmssp_state
*ntlmssp_state
;
79 char *want_feature_list
;
80 bool have_session_key
;
81 DATA_BLOB session_key
;
82 DATA_BLOB initial_message
;
85 typedef void (*stdio_helper_function
)(struct ntlm_auth_state
*state
, char *buf
,
88 static void manage_squid_basic_request (struct ntlm_auth_state
*state
,
89 char *buf
, int length
);
91 static void manage_squid_ntlmssp_request (struct ntlm_auth_state
*state
,
92 char *buf
, int length
);
94 static void manage_client_ntlmssp_request (struct ntlm_auth_state
*state
,
95 char *buf
, int length
);
97 static void manage_gss_spnego_request (struct ntlm_auth_state
*state
,
98 char *buf
, int length
);
100 static void manage_gss_spnego_client_request (struct ntlm_auth_state
*state
,
101 char *buf
, int length
);
103 static void manage_ntlm_server_1_request (struct ntlm_auth_state
*state
,
104 char *buf
, int length
);
106 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
107 char *buf
, int length
);
109 static const struct {
110 enum stdio_helper_mode mode
;
112 stdio_helper_function fn
;
113 } stdio_helper_protocols
[] = {
114 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
115 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
116 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request
},
117 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_client_ntlmssp_request
},
118 { GSS_SPNEGO
, "gss-spnego", manage_gss_spnego_request
},
119 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gss_spnego_client_request
},
120 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
121 { NTLM_CHANGE_PASSWORD_1
, "ntlm-change-password-1", manage_ntlm_change_password_1_request
},
122 { NUM_HELPER_MODES
, NULL
, NULL
}
125 const char *opt_username
;
126 const char *opt_domain
;
127 const char *opt_workstation
;
128 const char *opt_password
;
129 static DATA_BLOB opt_challenge
;
130 static DATA_BLOB opt_lm_response
;
131 static DATA_BLOB opt_nt_response
;
132 static int request_lm_key
;
133 static int request_user_session_key
;
134 static int use_cached_creds
;
136 static const char *require_membership_of
;
137 static const char *require_membership_of_sid
;
138 static const char *opt_pam_winbind_conf
;
140 static char winbind_separator(void)
142 struct winbindd_response response
;
149 ZERO_STRUCT(response
);
151 /* Send off request */
153 if (winbindd_request_response(WINBINDD_INFO
, NULL
, &response
) !=
154 NSS_STATUS_SUCCESS
) {
155 d_printf("could not obtain winbind separator!\n");
156 return *lp_winbind_separator();
159 sep
= response
.data
.info
.winbind_separator
;
163 d_printf("winbind separator was NULL!\n");
164 return *lp_winbind_separator();
170 const char *get_winbind_domain(void)
172 struct winbindd_response response
;
174 static fstring winbind_domain
;
175 if (*winbind_domain
) {
176 return winbind_domain
;
179 ZERO_STRUCT(response
);
181 /* Send off request */
183 if (winbindd_request_response(WINBINDD_DOMAIN_NAME
, NULL
, &response
) !=
184 NSS_STATUS_SUCCESS
) {
185 DEBUG(0, ("could not obtain winbind domain name!\n"));
186 return lp_workgroup();
189 fstrcpy(winbind_domain
, response
.data
.domain_name
);
191 return winbind_domain
;
195 const char *get_winbind_netbios_name(void)
197 struct winbindd_response response
;
199 static fstring winbind_netbios_name
;
201 if (*winbind_netbios_name
) {
202 return winbind_netbios_name
;
205 ZERO_STRUCT(response
);
207 /* Send off request */
209 if (winbindd_request_response(WINBINDD_NETBIOS_NAME
, NULL
, &response
) !=
210 NSS_STATUS_SUCCESS
) {
211 DEBUG(0, ("could not obtain winbind netbios name!\n"));
212 return global_myname();
215 fstrcpy(winbind_netbios_name
, response
.data
.netbios_name
);
217 return winbind_netbios_name
;
221 DATA_BLOB
get_challenge(void)
223 static DATA_BLOB chal
;
224 if (opt_challenge
.length
)
225 return opt_challenge
;
227 chal
= data_blob(NULL
, 8);
229 generate_random_buffer(chal
.data
, chal
.length
);
233 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
234 form DOMAIN/user into a domain and a user */
236 static bool parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
240 char *p
= strchr(domuser
,winbind_separator());
247 fstrcpy(domain
, domuser
);
248 domain
[PTR_DIFF(p
, domuser
)] = 0;
254 static bool get_require_membership_sid(void) {
255 struct winbindd_request request
;
256 struct winbindd_response response
;
258 if (!require_membership_of
) {
262 if (require_membership_of_sid
) {
266 /* Otherwise, ask winbindd for the name->sid request */
268 ZERO_STRUCT(request
);
269 ZERO_STRUCT(response
);
271 if (!parse_ntlm_auth_domain_user(require_membership_of
,
272 request
.data
.name
.dom_name
,
273 request
.data
.name
.name
)) {
274 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
275 require_membership_of
));
279 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
280 NSS_STATUS_SUCCESS
) {
281 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
282 require_membership_of
));
286 require_membership_of_sid
= SMB_STRDUP(response
.data
.sid
.sid
);
288 if (require_membership_of_sid
)
295 * Get some configuration from pam_winbind.conf to see if we
296 * need to contact trusted domain
299 int get_pam_winbind_config()
302 dictionary
*d
= NULL
;
304 if (!opt_pam_winbind_conf
|| !*opt_pam_winbind_conf
) {
305 opt_pam_winbind_conf
= PAM_WINBIND_CONFIG_FILE
;
308 d
= iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf
));
314 if (iniparser_getboolean(d
, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
315 ctrl
|= WINBIND_KRB5_AUTH
;
318 iniparser_freedict(d
);
323 /* Authenticate a user with a plaintext password */
325 static bool check_plaintext_auth(const char *user
, const char *pass
,
326 bool stdout_diagnostics
)
328 struct winbindd_request request
;
329 struct winbindd_response response
;
332 if (!get_require_membership_sid()) {
336 /* Send off request */
338 ZERO_STRUCT(request
);
339 ZERO_STRUCT(response
);
341 fstrcpy(request
.data
.auth
.user
, user
);
342 fstrcpy(request
.data
.auth
.pass
, pass
);
343 if (require_membership_of_sid
) {
344 strlcpy(request
.data
.auth
.require_membership_of_sid
,
345 require_membership_of_sid
,
346 sizeof(request
.data
.auth
.require_membership_of_sid
));
349 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
351 /* Display response */
353 if (stdout_diagnostics
) {
354 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
355 d_printf("Reading winbind reply failed! (0x01)\n");
358 d_printf("%s: %s (0x%x)\n",
359 response
.data
.auth
.nt_status_string
,
360 response
.data
.auth
.error_string
,
361 response
.data
.auth
.nt_status
);
363 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
364 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
367 DEBUG(3, ("%s: %s (0x%x)\n",
368 response
.data
.auth
.nt_status_string
,
369 response
.data
.auth
.error_string
,
370 response
.data
.auth
.nt_status
));
373 return (result
== NSS_STATUS_SUCCESS
);
376 /* authenticate a user with an encrypted username/password */
378 NTSTATUS
contact_winbind_auth_crap(const char *username
,
380 const char *workstation
,
381 const DATA_BLOB
*challenge
,
382 const DATA_BLOB
*lm_response
,
383 const DATA_BLOB
*nt_response
,
386 uint8 user_session_key
[16],
392 struct winbindd_request request
;
393 struct winbindd_response response
;
395 if (!get_require_membership_sid()) {
396 return NT_STATUS_INVALID_PARAMETER
;
399 ZERO_STRUCT(request
);
400 ZERO_STRUCT(response
);
402 request
.flags
= flags
;
404 request
.data
.auth_crap
.logon_parameters
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
| MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
406 if (require_membership_of_sid
)
407 fstrcpy(request
.data
.auth_crap
.require_membership_of_sid
, require_membership_of_sid
);
409 fstrcpy(request
.data
.auth_crap
.user
, username
);
410 fstrcpy(request
.data
.auth_crap
.domain
, domain
);
412 fstrcpy(request
.data
.auth_crap
.workstation
,
415 memcpy(request
.data
.auth_crap
.chal
, challenge
->data
, MIN(challenge
->length
, 8));
417 if (lm_response
&& lm_response
->length
) {
418 memcpy(request
.data
.auth_crap
.lm_resp
,
420 MIN(lm_response
->length
, sizeof(request
.data
.auth_crap
.lm_resp
)));
421 request
.data
.auth_crap
.lm_resp_len
= lm_response
->length
;
424 if (nt_response
&& nt_response
->length
) {
425 if (nt_response
->length
> sizeof(request
.data
.auth_crap
.nt_resp
)) {
426 request
.flags
= request
.flags
| WBFLAG_BIG_NTLMV2_BLOB
;
427 request
.extra_len
= nt_response
->length
;
428 request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, request
.extra_len
);
429 if (request
.extra_data
.data
== NULL
) {
430 return NT_STATUS_NO_MEMORY
;
432 memcpy(request
.extra_data
.data
, nt_response
->data
,
433 nt_response
->length
);
436 memcpy(request
.data
.auth_crap
.nt_resp
,
437 nt_response
->data
, nt_response
->length
);
439 request
.data
.auth_crap
.nt_resp_len
= nt_response
->length
;
442 result
= winbindd_request_response(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
443 SAFE_FREE(request
.extra_data
.data
);
445 /* Display response */
447 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
448 nt_status
= NT_STATUS_UNSUCCESSFUL
;
450 *error_string
= smb_xstrdup("Reading winbind reply failed!");
451 winbindd_free_response(&response
);
455 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
456 if (!NT_STATUS_IS_OK(nt_status
)) {
458 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
459 winbindd_free_response(&response
);
463 if ((flags
& WBFLAG_PAM_LMKEY
) && lm_key
) {
464 memcpy(lm_key
, response
.data
.auth
.first_8_lm_hash
,
465 sizeof(response
.data
.auth
.first_8_lm_hash
));
467 if ((flags
& WBFLAG_PAM_USER_SESSION_KEY
) && user_session_key
) {
468 memcpy(user_session_key
, response
.data
.auth
.user_session_key
,
469 sizeof(response
.data
.auth
.user_session_key
));
472 if (flags
& WBFLAG_PAM_UNIX_NAME
) {
473 *unix_name
= SMB_STRDUP(response
.data
.auth
.unix_username
);
475 winbindd_free_response(&response
);
476 return NT_STATUS_NO_MEMORY
;
480 winbindd_free_response(&response
);
484 /* contact server to change user password using auth crap */
485 static NTSTATUS
contact_winbind_change_pswd_auth_crap(const char *username
,
487 const DATA_BLOB new_nt_pswd
,
488 const DATA_BLOB old_nt_hash_enc
,
489 const DATA_BLOB new_lm_pswd
,
490 const DATA_BLOB old_lm_hash_enc
,
495 struct winbindd_request request
;
496 struct winbindd_response response
;
498 if (!get_require_membership_sid())
501 *error_string
= smb_xstrdup("Can't get membership sid.");
502 return NT_STATUS_INVALID_PARAMETER
;
505 ZERO_STRUCT(request
);
506 ZERO_STRUCT(response
);
509 fstrcpy(request
.data
.chng_pswd_auth_crap
.user
, username
);
511 fstrcpy(request
.data
.chng_pswd_auth_crap
.domain
,domain
);
513 if(new_nt_pswd
.length
)
515 memcpy(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
, new_nt_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
));
516 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
= new_nt_pswd
.length
;
519 if(old_nt_hash_enc
.length
)
521 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
));
522 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
= old_nt_hash_enc
.length
;
525 if(new_lm_pswd
.length
)
527 memcpy(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
, new_lm_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
));
528 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
= new_lm_pswd
.length
;
531 if(old_lm_hash_enc
.length
)
533 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
));
534 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
= old_lm_hash_enc
.length
;
537 result
= winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, &request
, &response
);
539 /* Display response */
541 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0))
543 nt_status
= NT_STATUS_UNSUCCESSFUL
;
545 *error_string
= smb_xstrdup("Reading winbind reply failed!");
546 winbindd_free_response(&response
);
550 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
551 if (!NT_STATUS_IS_OK(nt_status
))
554 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
555 winbindd_free_response(&response
);
559 winbindd_free_response(&response
);
564 static NTSTATUS
winbind_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
566 static const char zeros
[16] = { 0, };
568 char *error_string
= NULL
;
570 uint8 user_sess_key
[16];
571 char *unix_name
= NULL
;
573 nt_status
= contact_winbind_auth_crap(ntlmssp_state
->user
, ntlmssp_state
->domain
,
574 ntlmssp_state
->client
.netbios_name
,
575 &ntlmssp_state
->chal
,
576 &ntlmssp_state
->lm_resp
,
577 &ntlmssp_state
->nt_resp
,
578 WBFLAG_PAM_LMKEY
| WBFLAG_PAM_USER_SESSION_KEY
| WBFLAG_PAM_UNIX_NAME
,
579 lm_key
, user_sess_key
,
580 &error_string
, &unix_name
);
582 if (NT_STATUS_IS_OK(nt_status
)) {
583 if (memcmp(lm_key
, zeros
, 8) != 0) {
584 *lm_session_key
= data_blob_talloc(ntlmssp_state
, NULL
, 16);
585 memcpy(lm_session_key
->data
, lm_key
, 8);
586 memset(lm_session_key
->data
+8, '\0', 8);
589 if (memcmp(user_sess_key
, zeros
, 16) != 0) {
590 *user_session_key
= data_blob_talloc(ntlmssp_state
, user_sess_key
, 16);
592 ntlmssp_state
->callback_private
= talloc_strdup(ntlmssp_state
,
595 DEBUG(NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
) ? 0 : 3,
596 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
597 ntlmssp_state
->domain
, ntlmssp_state
->user
,
598 ntlmssp_state
->client
.netbios_name
,
599 error_string
? error_string
: "unknown error (NULL)"));
600 ntlmssp_state
->callback_private
= NULL
;
603 SAFE_FREE(error_string
);
604 SAFE_FREE(unix_name
);
608 static NTSTATUS
local_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
611 struct samr_Password lm_pw
, nt_pw
;
613 nt_lm_owf_gen (opt_password
, nt_pw
.hash
, lm_pw
.hash
);
615 nt_status
= ntlm_password_check(ntlmssp_state
,
617 &ntlmssp_state
->chal
,
618 &ntlmssp_state
->lm_resp
,
619 &ntlmssp_state
->nt_resp
,
622 ntlmssp_state
->domain
,
623 &lm_pw
, &nt_pw
, user_session_key
, lm_session_key
);
625 if (NT_STATUS_IS_OK(nt_status
)) {
626 ntlmssp_state
->callback_private
= talloc_asprintf(ntlmssp_state
,
627 "%s%c%s", ntlmssp_state
->domain
,
628 *lp_winbind_separator(),
629 ntlmssp_state
->user
);
631 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
632 ntlmssp_state
->domain
, ntlmssp_state
->user
,
633 ntlmssp_state
->client
.netbios_name
,
634 nt_errstr(nt_status
)));
635 ntlmssp_state
->callback_private
= NULL
;
640 static NTSTATUS
ntlm_auth_start_ntlmssp_client(struct ntlmssp_state
**client_ntlmssp_state
)
643 if ( (opt_username
== NULL
) || (opt_domain
== NULL
) ) {
644 status
= NT_STATUS_UNSUCCESSFUL
;
645 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
646 return NT_STATUS_INVALID_PARAMETER
;
649 status
= ntlmssp_client_start(NULL
,
652 lp_client_ntlmv2_auth(),
653 client_ntlmssp_state
);
655 if (!NT_STATUS_IS_OK(status
)) {
656 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
658 ntlmssp_end(client_ntlmssp_state
);
662 status
= ntlmssp_set_username(*client_ntlmssp_state
, opt_username
);
664 if (!NT_STATUS_IS_OK(status
)) {
665 DEBUG(1, ("Could not set username: %s\n",
667 ntlmssp_end(client_ntlmssp_state
);
671 status
= ntlmssp_set_domain(*client_ntlmssp_state
, opt_domain
);
673 if (!NT_STATUS_IS_OK(status
)) {
674 DEBUG(1, ("Could not set domain: %s\n",
676 ntlmssp_end(client_ntlmssp_state
);
681 status
= ntlmssp_set_password(*client_ntlmssp_state
, opt_password
);
683 if (!NT_STATUS_IS_OK(status
)) {
684 DEBUG(1, ("Could not set password: %s\n",
686 ntlmssp_end(client_ntlmssp_state
);
694 static NTSTATUS
ntlm_auth_start_ntlmssp_server(struct ntlmssp_state
**ntlmssp_state
)
697 const char *netbios_name
;
698 const char *netbios_domain
;
699 const char *dns_name
;
701 bool is_standalone
= false;
704 netbios_name
= global_myname();
705 netbios_domain
= lp_workgroup();
707 netbios_name
= get_winbind_netbios_name();
708 netbios_domain
= get_winbind_domain();
710 /* This should be a 'netbios domain -> DNS domain' mapping */
711 dns_domain
= get_mydnsdomname(talloc_tos());
713 strlower_m(dns_domain
);
715 dns_name
= get_mydnsfullname();
717 status
= ntlmssp_server_start(NULL
,
724 if (!NT_STATUS_IS_OK(status
)) {
725 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
730 /* Have we been given a local password, or should we ask winbind? */
732 (*ntlmssp_state
)->check_password
= local_pw_check
;
734 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
739 /*******************************************************************
740 Used by firefox to drive NTLM auth to IIS servers.
741 *******************************************************************/
743 static NTSTATUS
do_ccache_ntlm_auth(DATA_BLOB initial_msg
, DATA_BLOB challenge_msg
,
746 struct winbindd_request wb_request
;
747 struct winbindd_response wb_response
;
751 /* get winbindd to do the ntlmssp step on our behalf */
752 ZERO_STRUCT(wb_request
);
753 ZERO_STRUCT(wb_response
);
756 * This is tricky here. If we set krb5_auth in pam_winbind.conf
757 * creds for users in trusted domain will be stored the winbindd
758 * child of the trusted domain. If we ask the primary domain for
759 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
760 * domain's child for ccache_ntlm_auth. that is to say, we have to
761 * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
763 ctrl
= get_pam_winbind_config();
765 if (ctrl
| WINBIND_KRB5_AUTH
) {
766 wb_request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
769 fstr_sprintf(wb_request
.data
.ccache_ntlm_auth
.user
,
770 "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
771 wb_request
.data
.ccache_ntlm_auth
.uid
= geteuid();
772 wb_request
.data
.ccache_ntlm_auth
.initial_blob_len
= initial_msg
.length
;
773 wb_request
.data
.ccache_ntlm_auth
.challenge_blob_len
= challenge_msg
.length
;
774 wb_request
.extra_len
= initial_msg
.length
+ challenge_msg
.length
;
776 if (wb_request
.extra_len
> 0) {
777 wb_request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, wb_request
.extra_len
);
778 if (wb_request
.extra_data
.data
== NULL
) {
779 return NT_STATUS_NO_MEMORY
;
782 memcpy(wb_request
.extra_data
.data
, initial_msg
.data
, initial_msg
.length
);
783 memcpy(wb_request
.extra_data
.data
+ initial_msg
.length
,
784 challenge_msg
.data
, challenge_msg
.length
);
787 result
= winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH
, &wb_request
, &wb_response
);
788 SAFE_FREE(wb_request
.extra_data
.data
);
790 if (result
!= NSS_STATUS_SUCCESS
) {
791 winbindd_free_response(&wb_response
);
792 return NT_STATUS_UNSUCCESSFUL
;
796 *reply
= data_blob(wb_response
.extra_data
.data
,
797 wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
);
798 if (wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
> 0 &&
799 reply
->data
== NULL
) {
800 winbindd_free_response(&wb_response
);
801 return NT_STATUS_NO_MEMORY
;
805 winbindd_free_response(&wb_response
);
806 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
809 static void manage_squid_ntlmssp_request(struct ntlm_auth_state
*state
,
810 char *buf
, int length
)
812 DATA_BLOB request
, reply
;
815 if (strlen(buf
) < 2) {
816 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
817 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
821 if (strlen(buf
) > 3) {
822 if(strncmp(buf
, "SF ", 3) == 0){
823 DEBUG(10, ("Setting flags to negotioate\n"));
824 TALLOC_FREE(state
->want_feature_list
);
825 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
827 x_fprintf(x_stdout
, "OK\n");
830 request
= base64_decode_data_blob(buf
+ 3);
832 request
= data_blob_null
;
835 if ((strncmp(buf
, "PW ", 3) == 0)) {
836 /* The calling application wants us to use a local password
837 * (rather than winbindd) */
839 opt_password
= SMB_STRNDUP((const char *)request
.data
,
842 if (opt_password
== NULL
) {
843 DEBUG(1, ("Out of memory\n"));
844 x_fprintf(x_stdout
, "BH Out of memory\n");
845 data_blob_free(&request
);
849 x_fprintf(x_stdout
, "OK\n");
850 data_blob_free(&request
);
854 if (strncmp(buf
, "YR", 2) == 0) {
855 if (state
->ntlmssp_state
)
856 ntlmssp_end(&state
->ntlmssp_state
);
857 state
->svr_state
= SERVER_INITIAL
;
858 } else if (strncmp(buf
, "KK", 2) == 0) {
859 /* No special preprocessing required */
860 } else if (strncmp(buf
, "GF", 2) == 0) {
861 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
863 if (state
->svr_state
== SERVER_FINISHED
) {
864 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
867 x_fprintf(x_stdout
, "BH\n");
869 data_blob_free(&request
);
871 } else if (strncmp(buf
, "GK", 2) == 0) {
872 DEBUG(10, ("Requested NTLMSSP session key\n"));
873 if(state
->have_session_key
) {
874 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
876 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
879 x_fprintf(x_stdout
, "BH\n");
882 data_blob_free(&request
);
885 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
886 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
890 if (!state
->ntlmssp_state
) {
891 nt_status
= ntlm_auth_start_ntlmssp_server(
892 &state
->ntlmssp_state
);
893 if (!NT_STATUS_IS_OK(nt_status
)) {
894 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
897 ntlmssp_want_feature_list(state
->ntlmssp_state
,
898 state
->want_feature_list
);
901 DEBUG(10, ("got NTLMSSP packet:\n"));
902 dump_data(10, request
.data
, request
.length
);
904 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
, &reply
);
906 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
907 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
909 x_fprintf(x_stdout
, "TT %s\n", reply_base64
);
910 TALLOC_FREE(reply_base64
);
911 data_blob_free(&reply
);
912 state
->svr_state
= SERVER_CHALLENGE
;
913 DEBUG(10, ("NTLMSSP challenge\n"));
914 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
915 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
916 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
918 ntlmssp_end(&state
->ntlmssp_state
);
919 } else if (!NT_STATUS_IS_OK(nt_status
)) {
920 x_fprintf(x_stdout
, "NA %s\n", nt_errstr(nt_status
));
921 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
923 x_fprintf(x_stdout
, "AF %s\n",
924 (char *)state
->ntlmssp_state
->callback_private
);
925 DEBUG(10, ("NTLMSSP OK!\n"));
927 if(state
->have_session_key
)
928 data_blob_free(&state
->session_key
);
929 state
->session_key
= data_blob(
930 state
->ntlmssp_state
->session_key
.data
,
931 state
->ntlmssp_state
->session_key
.length
);
932 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
933 state
->have_session_key
= true;
934 state
->svr_state
= SERVER_FINISHED
;
937 data_blob_free(&request
);
940 static void manage_client_ntlmssp_request(struct ntlm_auth_state
*state
,
941 char *buf
, int length
)
943 DATA_BLOB request
, reply
;
946 if (!opt_username
|| !*opt_username
) {
947 x_fprintf(x_stderr
, "username must be specified!\n\n");
951 if (strlen(buf
) < 2) {
952 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
953 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
957 if (strlen(buf
) > 3) {
958 if(strncmp(buf
, "SF ", 3) == 0) {
959 DEBUG(10, ("Looking for flags to negotiate\n"));
960 talloc_free(state
->want_feature_list
);
961 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
963 x_fprintf(x_stdout
, "OK\n");
966 request
= base64_decode_data_blob(buf
+ 3);
968 request
= data_blob_null
;
971 if (strncmp(buf
, "PW ", 3) == 0) {
972 /* We asked for a password and obviously got it :-) */
974 opt_password
= SMB_STRNDUP((const char *)request
.data
,
977 if (opt_password
== NULL
) {
978 DEBUG(1, ("Out of memory\n"));
979 x_fprintf(x_stdout
, "BH Out of memory\n");
980 data_blob_free(&request
);
984 x_fprintf(x_stdout
, "OK\n");
985 data_blob_free(&request
);
989 if (!state
->ntlmssp_state
&& use_cached_creds
) {
990 /* check whether cached credentials are usable. */
991 DATA_BLOB empty_blob
= data_blob_null
;
993 nt_status
= do_ccache_ntlm_auth(empty_blob
, empty_blob
, NULL
);
994 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
995 /* failed to use cached creds */
996 use_cached_creds
= False
;
1000 if (opt_password
== NULL
&& !use_cached_creds
) {
1001 /* Request a password from the calling process. After
1002 sending it, the calling process should retry asking for the
1005 DEBUG(10, ("Requesting password\n"));
1006 x_fprintf(x_stdout
, "PW\n");
1010 if (strncmp(buf
, "YR", 2) == 0) {
1011 if (state
->ntlmssp_state
)
1012 ntlmssp_end(&state
->ntlmssp_state
);
1013 state
->cli_state
= CLIENT_INITIAL
;
1014 } else if (strncmp(buf
, "TT", 2) == 0) {
1015 /* No special preprocessing required */
1016 } else if (strncmp(buf
, "GF", 2) == 0) {
1017 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1019 if(state
->cli_state
== CLIENT_FINISHED
) {
1020 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
1023 x_fprintf(x_stdout
, "BH\n");
1026 data_blob_free(&request
);
1028 } else if (strncmp(buf
, "GK", 2) == 0 ) {
1029 DEBUG(10, ("Requested session key\n"));
1031 if(state
->cli_state
== CLIENT_FINISHED
) {
1032 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
1033 state
->session_key
);
1034 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
1038 x_fprintf(x_stdout
, "BH\n");
1041 data_blob_free(&request
);
1044 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
1045 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
1049 if (!state
->ntlmssp_state
) {
1050 nt_status
= ntlm_auth_start_ntlmssp_client(
1051 &state
->ntlmssp_state
);
1052 if (!NT_STATUS_IS_OK(nt_status
)) {
1053 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1056 ntlmssp_want_feature_list(state
->ntlmssp_state
,
1057 state
->want_feature_list
);
1058 state
->initial_message
= data_blob_null
;
1061 DEBUG(10, ("got NTLMSSP packet:\n"));
1062 dump_data(10, request
.data
, request
.length
);
1064 if (use_cached_creds
&& !opt_password
&&
1065 (state
->cli_state
== CLIENT_RESPONSE
)) {
1066 nt_status
= do_ccache_ntlm_auth(state
->initial_message
, request
,
1069 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
,
1073 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1074 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
1076 if (state
->cli_state
== CLIENT_INITIAL
) {
1077 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
1078 state
->initial_message
= reply
;
1079 state
->cli_state
= CLIENT_RESPONSE
;
1081 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
1082 data_blob_free(&reply
);
1084 TALLOC_FREE(reply_base64
);
1085 DEBUG(10, ("NTLMSSP challenge\n"));
1086 } else if (NT_STATUS_IS_OK(nt_status
)) {
1087 char *reply_base64
= base64_encode_data_blob(talloc_tos(),
1089 x_fprintf(x_stdout
, "AF %s\n", reply_base64
);
1090 TALLOC_FREE(reply_base64
);
1092 if(state
->have_session_key
)
1093 data_blob_free(&state
->session_key
);
1095 state
->session_key
= data_blob(
1096 state
->ntlmssp_state
->session_key
.data
,
1097 state
->ntlmssp_state
->session_key
.length
);
1098 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
1099 state
->have_session_key
= true;
1101 DEBUG(10, ("NTLMSSP OK!\n"));
1102 state
->cli_state
= CLIENT_FINISHED
;
1103 if (state
->ntlmssp_state
)
1104 ntlmssp_end(&state
->ntlmssp_state
);
1106 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1107 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
1108 state
->cli_state
= CLIENT_ERROR
;
1109 if (state
->ntlmssp_state
)
1110 ntlmssp_end(&state
->ntlmssp_state
);
1113 data_blob_free(&request
);
1116 static void manage_squid_basic_request(struct ntlm_auth_state
*state
,
1117 char *buf
, int length
)
1122 pass
=(char *)memchr(buf
,' ',length
);
1124 DEBUG(2, ("Password not found. Denying access\n"));
1125 x_fprintf(x_stdout
, "ERR\n");
1131 if (state
->helper_mode
== SQUID_2_5_BASIC
) {
1132 rfc1738_unescape(user
);
1133 rfc1738_unescape(pass
);
1136 if (check_plaintext_auth(user
, pass
, False
)) {
1137 x_fprintf(x_stdout
, "OK\n");
1139 x_fprintf(x_stdout
, "ERR\n");
1143 static void offer_gss_spnego_mechs(void) {
1146 struct spnego_data spnego
;
1149 TALLOC_CTX
*ctx
= talloc_tos();
1153 ZERO_STRUCT(spnego
);
1155 myname_lower
= talloc_strdup(ctx
, global_myname());
1156 if (!myname_lower
) {
1159 strlower_m(myname_lower
);
1161 principal
= talloc_asprintf(ctx
, "%s$@%s", myname_lower
, lp_realm());
1166 /* Server negTokenInit (mech offerings) */
1167 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1168 spnego
.negTokenInit
.mechTypes
= talloc_array(ctx
, const char *, 2);
1170 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_KERBEROS5_OLD
);
1171 spnego
.negTokenInit
.mechTypes
[1] = talloc_strdup(ctx
, OID_NTLMSSP
);
1172 spnego
.negTokenInit
.mechTypes
[2] = NULL
;
1174 spnego
.negTokenInit
.mechTypes
[0] = talloc_strdup(ctx
, OID_NTLMSSP
);
1175 spnego
.negTokenInit
.mechTypes
[1] = NULL
;
1179 spnego
.negTokenInit
.mechListMIC
= data_blob_talloc(ctx
, principal
,
1182 len
= spnego_write_data(ctx
, &token
, &spnego
);
1183 spnego_free_data(&spnego
);
1186 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1187 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1191 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1192 x_fprintf(x_stdout
, "TT %s *\n", reply_base64
);
1194 TALLOC_FREE(reply_base64
);
1195 data_blob_free(&token
);
1196 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1200 static void manage_gss_spnego_request(struct ntlm_auth_state
*state
,
1201 char *buf
, int length
)
1203 static struct ntlmssp_state
*ntlmssp_state
= NULL
;
1204 struct spnego_data request
, response
;
1208 TALLOC_CTX
*ctx
= talloc_tos();
1211 char *domain
= NULL
;
1213 const char *reply_code
;
1215 char *reply_argument
= NULL
;
1217 if (strlen(buf
) < 2) {
1218 DEBUG(1, ("SPENGO query [%s] invalid", buf
));
1219 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1223 if (strncmp(buf
, "YR", 2) == 0) {
1225 ntlmssp_end(&ntlmssp_state
);
1226 } else if (strncmp(buf
, "KK", 2) == 0) {
1229 DEBUG(1, ("SPENGO query [%s] invalid", buf
));
1230 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1234 if ( (strlen(buf
) == 2)) {
1236 /* no client data, get the negTokenInit offering
1239 offer_gss_spnego_mechs();
1243 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1245 if (strlen(buf
) <= 3) {
1246 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1247 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1251 token
= base64_decode_data_blob(buf
+ 3);
1252 len
= spnego_read_data(ctx
, token
, &request
);
1253 data_blob_free(&token
);
1256 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf
));
1257 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1261 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1263 /* Second request from Client. This is where the
1264 client offers its mechanism to use. */
1266 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
1267 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
1268 DEBUG(1, ("Client did not offer any mechanism"));
1269 x_fprintf(x_stdout
, "BH Client did not offer any "
1274 status
= NT_STATUS_UNSUCCESSFUL
;
1275 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_NTLMSSP
) == 0) {
1277 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
1278 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1279 x_fprintf(x_stdout
, "BH Client did not provide "
1284 if ( ntlmssp_state
!= NULL
) {
1285 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1286 "already got one\n"));
1287 x_fprintf(x_stdout
, "BH Client wants a new "
1288 "NTLMSSP challenge, but "
1289 "already got one\n");
1290 ntlmssp_end(&ntlmssp_state
);
1294 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_server(&ntlmssp_state
))) {
1295 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1299 DEBUG(10, ("got NTLMSSP packet:\n"));
1300 dump_data(10, request
.negTokenInit
.mechToken
.data
,
1301 request
.negTokenInit
.mechToken
.length
);
1303 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1304 response
.negTokenTarg
.supportedMech
= talloc_strdup(ctx
, OID_NTLMSSP
);
1305 response
.negTokenTarg
.mechListMIC
= data_blob_talloc(ctx
, NULL
, 0);
1307 status
= ntlmssp_update(ntlmssp_state
,
1308 request
.negTokenInit
.mechToken
,
1309 &response
.negTokenTarg
.responseToken
);
1313 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_KERBEROS5_OLD
) == 0) {
1315 TALLOC_CTX
*mem_ctx
= talloc_init("manage_gss_spnego_request");
1318 DATA_BLOB session_key
;
1319 struct PAC_DATA
*pac_data
= NULL
;
1321 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
1322 DEBUG(1, ("Client did not provide Kerberos data\n"));
1323 x_fprintf(x_stdout
, "BH Client did not provide "
1328 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1329 response
.negTokenTarg
.supportedMech
= talloc_strdup(ctx
, OID_KERBEROS5_OLD
);
1330 response
.negTokenTarg
.mechListMIC
= data_blob_talloc(ctx
, NULL
, 0);
1331 response
.negTokenTarg
.responseToken
= data_blob_talloc(ctx
, NULL
, 0);
1333 status
= ads_verify_ticket(mem_ctx
, lp_realm(), 0,
1334 &request
.negTokenInit
.mechToken
,
1335 &principal
, &pac_data
, &ap_rep
,
1336 &session_key
, True
);
1338 /* Now in "principal" we have the name we are
1339 authenticated as. */
1341 if (NT_STATUS_IS_OK(status
)) {
1343 domain
= strchr_m(principal
, '@');
1345 if (domain
== NULL
) {
1346 DEBUG(1, ("Did not get a valid principal "
1347 "from ads_verify_ticket\n"));
1348 x_fprintf(x_stdout
, "BH Did not get a "
1349 "valid principal from "
1350 "ads_verify_ticket\n");
1355 domain
= SMB_STRDUP(domain
);
1356 user
= SMB_STRDUP(principal
);
1358 data_blob_free(&ap_rep
);
1361 TALLOC_FREE(mem_ctx
);
1367 if ( (request
.negTokenTarg
.supportedMech
== NULL
) ||
1368 ( strcmp(request
.negTokenTarg
.supportedMech
, OID_NTLMSSP
) != 0 ) ) {
1369 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1370 is the only one we support that sends this stuff */
1371 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1372 request
.negTokenTarg
.supportedMech
));
1373 x_fprintf(x_stdout
, "BH Got a negTokenTarg for "
1374 "something non-NTLMSSP\n");
1378 if (request
.negTokenTarg
.responseToken
.data
== NULL
) {
1379 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1380 x_fprintf(x_stdout
, "BH Got a negTokenTarg without a "
1381 "responseToken!\n");
1385 status
= ntlmssp_update(ntlmssp_state
,
1386 request
.negTokenTarg
.responseToken
,
1387 &response
.negTokenTarg
.responseToken
);
1389 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1390 response
.negTokenTarg
.supportedMech
= talloc_strdup(ctx
, OID_NTLMSSP
);
1391 response
.negTokenTarg
.mechListMIC
= data_blob_talloc(ctx
, NULL
, 0);
1393 if (NT_STATUS_IS_OK(status
)) {
1394 user
= SMB_STRDUP(ntlmssp_state
->user
);
1395 domain
= SMB_STRDUP(ntlmssp_state
->domain
);
1396 ntlmssp_end(&ntlmssp_state
);
1400 spnego_free_data(&request
);
1402 if (NT_STATUS_IS_OK(status
)) {
1403 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
1405 reply_argument
= talloc_asprintf(ctx
, "%s\\%s", domain
, user
);
1406 } else if (NT_STATUS_EQUAL(status
,
1407 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1408 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1410 reply_argument
= talloc_strdup(ctx
, "*");
1412 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1414 reply_argument
= talloc_strdup(ctx
, nt_errstr(status
));
1417 if (!reply_argument
) {
1418 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1419 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1426 len
= spnego_write_data(ctx
, &token
, &response
);
1427 spnego_free_data(&response
);
1430 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1431 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1435 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1437 x_fprintf(x_stdout
, "%s %s %s\n",
1438 reply_code
, reply_base64
, reply_argument
);
1440 TALLOC_FREE(reply_base64
);
1441 data_blob_free(&token
);
1446 static struct ntlmssp_state
*client_ntlmssp_state
= NULL
;
1448 static bool manage_client_ntlmssp_init(struct spnego_data spnego
)
1451 DATA_BLOB null_blob
= data_blob_null
;
1452 DATA_BLOB to_server
;
1453 char *to_server_base64
;
1454 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1455 TALLOC_CTX
*ctx
= talloc_tos();
1457 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1459 if (client_ntlmssp_state
!= NULL
) {
1460 DEBUG(1, ("Request for initial SPNEGO request where "
1461 "we already have a state\n"));
1465 if (!client_ntlmssp_state
) {
1466 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1467 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1473 if (opt_password
== NULL
) {
1475 /* Request a password from the calling process. After
1476 sending it, the calling process should retry with
1477 the negTokenInit. */
1479 DEBUG(10, ("Requesting password\n"));
1480 x_fprintf(x_stdout
, "PW\n");
1484 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1485 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1486 spnego
.negTokenInit
.reqFlags
= data_blob_null
;
1487 spnego
.negTokenInit
.reqFlagsPadding
= 0;
1488 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1490 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1491 &spnego
.negTokenInit
.mechToken
);
1493 if ( !(NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) ||
1494 NT_STATUS_IS_OK(status
)) ) {
1495 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1496 nt_errstr(status
)));
1497 ntlmssp_end(&client_ntlmssp_state
);
1501 spnego_write_data(ctx
, &to_server
, &spnego
);
1502 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1504 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1505 data_blob_free(&to_server
);
1506 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1507 TALLOC_FREE(to_server_base64
);
1511 static void manage_client_ntlmssp_targ(struct spnego_data spnego
)
1514 DATA_BLOB null_blob
= data_blob_null
;
1516 DATA_BLOB to_server
;
1517 char *to_server_base64
;
1518 TALLOC_CTX
*ctx
= talloc_tos();
1520 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1522 if (client_ntlmssp_state
== NULL
) {
1523 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1524 x_fprintf(x_stdout
, "BH Got NTLMSSP tArg without a client state\n");
1528 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1529 x_fprintf(x_stdout
, "NA\n");
1530 ntlmssp_end(&client_ntlmssp_state
);
1534 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1535 x_fprintf(x_stdout
, "AF\n");
1536 ntlmssp_end(&client_ntlmssp_state
);
1540 status
= ntlmssp_update(client_ntlmssp_state
,
1541 spnego
.negTokenTarg
.responseToken
,
1544 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1545 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1546 "ntlmssp_client_update, got: %s\n",
1547 nt_errstr(status
)));
1548 x_fprintf(x_stdout
, "BH Expected MORE_PROCESSING_REQUIRED from "
1549 "ntlmssp_client_update\n");
1550 data_blob_free(&request
);
1551 ntlmssp_end(&client_ntlmssp_state
);
1555 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1556 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1557 spnego
.negTokenTarg
.supportedMech
= (char *)OID_NTLMSSP
;
1558 spnego
.negTokenTarg
.responseToken
= request
;
1559 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1561 spnego_write_data(ctx
, &to_server
, &spnego
);
1562 data_blob_free(&request
);
1564 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1565 data_blob_free(&to_server
);
1566 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1567 TALLOC_FREE(to_server_base64
);
1573 static bool manage_client_krb5_init(struct spnego_data spnego
)
1576 DATA_BLOB tkt
, to_server
;
1577 DATA_BLOB session_key_krb5
= data_blob_null
;
1578 struct spnego_data reply
;
1582 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1584 TALLOC_CTX
*ctx
= talloc_tos();
1586 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1587 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1588 DEBUG(1, ("Did not get a principal for krb5\n"));
1592 principal
= (char *)SMB_MALLOC(
1593 spnego
.negTokenInit
.mechListMIC
.length
+1);
1595 if (principal
== NULL
) {
1596 DEBUG(1, ("Could not malloc principal\n"));
1600 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1601 spnego
.negTokenInit
.mechListMIC
.length
);
1602 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1604 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
, 0, NULL
, NULL
, NULL
);
1609 /* Let's try to first get the TGT, for that we need a
1612 if (opt_password
== NULL
) {
1613 DEBUG(10, ("Requesting password\n"));
1614 x_fprintf(x_stdout
, "PW\n");
1618 user
= talloc_asprintf(talloc_tos(), "%s@%s", opt_username
, opt_domain
);
1623 if ((retval
= kerberos_kinit_password(user
, opt_password
, 0, NULL
))) {
1624 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval
)));
1628 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
, 0, NULL
, NULL
, NULL
);
1631 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1636 data_blob_free(&session_key_krb5
);
1640 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1641 reply
.negTokenInit
.mechTypes
= my_mechs
;
1642 reply
.negTokenInit
.reqFlags
= data_blob_null
;
1643 reply
.negTokenInit
.reqFlagsPadding
= 0;
1644 reply
.negTokenInit
.mechToken
= tkt
;
1645 reply
.negTokenInit
.mechListMIC
= data_blob_null
;
1647 len
= spnego_write_data(ctx
, &to_server
, &reply
);
1648 data_blob_free(&tkt
);
1651 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1655 reply_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1656 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1658 TALLOC_FREE(reply_base64
);
1659 data_blob_free(&to_server
);
1660 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1664 static void manage_client_krb5_targ(struct spnego_data spnego
)
1666 switch (spnego
.negTokenTarg
.negResult
) {
1667 case SPNEGO_ACCEPT_INCOMPLETE
:
1668 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1669 x_fprintf(x_stdout
, "BH Got a Kerberos negTokenTarg with "
1670 "ACCEPT_INCOMPLETE\n");
1672 case SPNEGO_ACCEPT_COMPLETED
:
1673 DEBUG(10, ("Accept completed\n"));
1674 x_fprintf(x_stdout
, "AF\n");
1677 DEBUG(10, ("Rejected\n"));
1678 x_fprintf(x_stdout
, "NA\n");
1681 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1682 x_fprintf(x_stdout
, "AF\n");
1688 static void manage_gss_spnego_client_request(struct ntlm_auth_state
*state
,
1689 char *buf
, int length
)
1692 struct spnego_data spnego
;
1694 TALLOC_CTX
*ctx
= talloc_tos();
1696 if (!opt_username
|| !*opt_username
) {
1697 x_fprintf(x_stderr
, "username must be specified!\n\n");
1701 if (strlen(buf
) <= 3) {
1702 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1703 x_fprintf(x_stdout
, "BH SPNEGO query too short\n");
1707 request
= base64_decode_data_blob(buf
+3);
1709 if (strncmp(buf
, "PW ", 3) == 0) {
1711 /* We asked for a password and obviously got it :-) */
1713 opt_password
= SMB_STRNDUP((const char *)request
.data
, request
.length
);
1715 if (opt_password
== NULL
) {
1716 DEBUG(1, ("Out of memory\n"));
1717 x_fprintf(x_stdout
, "BH Out of memory\n");
1718 data_blob_free(&request
);
1722 x_fprintf(x_stdout
, "OK\n");
1723 data_blob_free(&request
);
1727 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1728 (strncmp(buf
, "AF ", 3) != 0) &&
1729 (strncmp(buf
, "NA ", 3) != 0) ) {
1730 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1731 x_fprintf(x_stdout
, "BH SPNEGO request invalid\n");
1732 data_blob_free(&request
);
1736 /* So we got a server challenge to generate a SPNEGO
1737 client-to-server request... */
1739 len
= spnego_read_data(ctx
, request
, &spnego
);
1740 data_blob_free(&request
);
1743 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1744 x_fprintf(x_stdout
, "BH Could not read SPNEGO data\n");
1748 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1750 /* The server offers a list of mechanisms */
1752 const char **mechType
= (const char **)spnego
.negTokenInit
.mechTypes
;
1754 while (*mechType
!= NULL
) {
1757 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1758 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1759 if (manage_client_krb5_init(spnego
))
1764 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1765 if (manage_client_ntlmssp_init(spnego
))
1772 DEBUG(1, ("Server offered no compatible mechanism\n"));
1773 x_fprintf(x_stdout
, "BH Server offered no compatible mechanism\n");
1777 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1779 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1780 /* On accept/reject Windows does not send the
1781 mechanism anymore. Handle that here and
1782 shut down the mechanisms. */
1784 switch (spnego
.negTokenTarg
.negResult
) {
1785 case SPNEGO_ACCEPT_COMPLETED
:
1786 x_fprintf(x_stdout
, "AF\n");
1789 x_fprintf(x_stdout
, "NA\n");
1792 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1793 "unknown negResult: %d\n",
1794 spnego
.negTokenTarg
.negResult
));
1795 x_fprintf(x_stdout
, "BH Got a negTokenTarg with"
1796 " no mech and an unknown "
1800 ntlmssp_end(&client_ntlmssp_state
);
1804 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1805 OID_NTLMSSP
) == 0) {
1806 manage_client_ntlmssp_targ(spnego
);
1811 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1812 OID_KERBEROS5_OLD
) == 0) {
1813 manage_client_krb5_targ(spnego
);
1820 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1821 x_fprintf(x_stdout
, "BH Got an SPNEGO token I could not handle\n");
1825 spnego_free_data(&spnego
);
1829 static void manage_ntlm_server_1_request(struct ntlm_auth_state
*state
,
1830 char *buf
, int length
)
1832 char *request
, *parameter
;
1833 static DATA_BLOB challenge
;
1834 static DATA_BLOB lm_response
;
1835 static DATA_BLOB nt_response
;
1836 static char *full_username
;
1837 static char *username
;
1838 static char *domain
;
1839 static char *plaintext_password
;
1840 static bool ntlm_server_1_user_session_key
;
1841 static bool ntlm_server_1_lm_session_key
;
1843 if (strequal(buf
, ".")) {
1844 if (!full_username
&& !username
) {
1845 x_fprintf(x_stdout
, "Error: No username supplied!\n");
1846 } else if (plaintext_password
) {
1847 /* handle this request as plaintext */
1848 if (!full_username
) {
1849 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
1850 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
1854 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
1855 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1857 x_fprintf(x_stdout
, "Authenticated: No\n");
1859 } else if (!lm_response
.data
&& !nt_response
.data
) {
1860 x_fprintf(x_stdout
, "Error: No password supplied!\n");
1861 } else if (!challenge
.data
) {
1862 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
1864 char *error_string
= NULL
;
1866 uchar user_session_key
[16];
1869 if (full_username
&& !username
) {
1871 fstring fstr_domain
;
1873 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
1874 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1875 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
1877 SAFE_FREE(username
);
1879 username
= smb_xstrdup(fstr_user
);
1880 domain
= smb_xstrdup(fstr_domain
);
1884 domain
= smb_xstrdup(get_winbind_domain());
1887 if (ntlm_server_1_lm_session_key
)
1888 flags
|= WBFLAG_PAM_LMKEY
;
1890 if (ntlm_server_1_user_session_key
)
1891 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
1893 if (!NT_STATUS_IS_OK(
1894 contact_winbind_auth_crap(username
,
1906 x_fprintf(x_stdout
, "Authenticated: No\n");
1907 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
1909 static char zeros
[16];
1911 char *hex_user_session_key
;
1913 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1915 if (ntlm_server_1_lm_session_key
1916 && (memcmp(zeros
, lm_key
,
1917 sizeof(lm_key
)) != 0)) {
1918 hex_lm_key
= hex_encode_talloc(NULL
,
1919 (const unsigned char *)lm_key
,
1921 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
1922 TALLOC_FREE(hex_lm_key
);
1925 if (ntlm_server_1_user_session_key
1926 && (memcmp(zeros
, user_session_key
,
1927 sizeof(user_session_key
)) != 0)) {
1928 hex_user_session_key
= hex_encode_talloc(NULL
,
1929 (const unsigned char *)user_session_key
,
1930 sizeof(user_session_key
));
1931 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
1932 TALLOC_FREE(hex_user_session_key
);
1935 SAFE_FREE(error_string
);
1937 /* clear out the state */
1938 challenge
= data_blob_null
;
1939 nt_response
= data_blob_null
;
1940 lm_response
= data_blob_null
;
1941 SAFE_FREE(full_username
);
1942 SAFE_FREE(username
);
1944 SAFE_FREE(plaintext_password
);
1945 ntlm_server_1_user_session_key
= False
;
1946 ntlm_server_1_lm_session_key
= False
;
1947 x_fprintf(x_stdout
, ".\n");
1954 /* Indicates a base64 encoded structure */
1955 parameter
= strstr_m(request
, ":: ");
1957 parameter
= strstr_m(request
, ": ");
1960 DEBUG(0, ("Parameter not found!\n"));
1961 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
1978 base64_decode_inplace(parameter
);
1981 if (strequal(request
, "LANMAN-Challenge")) {
1982 challenge
= strhex_to_data_blob(NULL
, parameter
);
1983 if (challenge
.length
!= 8) {
1984 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1986 (int)challenge
.length
);
1987 challenge
= data_blob_null
;
1989 } else if (strequal(request
, "NT-Response")) {
1990 nt_response
= strhex_to_data_blob(NULL
, parameter
);
1991 if (nt_response
.length
< 24) {
1992 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1994 (int)nt_response
.length
);
1995 nt_response
= data_blob_null
;
1997 } else if (strequal(request
, "LANMAN-Response")) {
1998 lm_response
= strhex_to_data_blob(NULL
, parameter
);
1999 if (lm_response
.length
!= 24) {
2000 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2002 (int)lm_response
.length
);
2003 lm_response
= data_blob_null
;
2005 } else if (strequal(request
, "Password")) {
2006 plaintext_password
= smb_xstrdup(parameter
);
2007 } else if (strequal(request
, "NT-Domain")) {
2008 domain
= smb_xstrdup(parameter
);
2009 } else if (strequal(request
, "Username")) {
2010 username
= smb_xstrdup(parameter
);
2011 } else if (strequal(request
, "Full-Username")) {
2012 full_username
= smb_xstrdup(parameter
);
2013 } else if (strequal(request
, "Request-User-Session-Key")) {
2014 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
2015 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
2016 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
2018 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2022 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
2023 char *buf
, int length
)
2025 char *request
, *parameter
;
2026 static DATA_BLOB new_nt_pswd
;
2027 static DATA_BLOB old_nt_hash_enc
;
2028 static DATA_BLOB new_lm_pswd
;
2029 static DATA_BLOB old_lm_hash_enc
;
2030 static char *full_username
= NULL
;
2031 static char *username
= NULL
;
2032 static char *domain
= NULL
;
2033 static char *newpswd
= NULL
;
2034 static char *oldpswd
= NULL
;
2036 if (strequal(buf
, ".")) {
2037 if(newpswd
&& oldpswd
) {
2038 uchar old_nt_hash
[16];
2039 uchar old_lm_hash
[16];
2040 uchar new_nt_hash
[16];
2041 uchar new_lm_hash
[16];
2043 new_nt_pswd
= data_blob(NULL
, 516);
2044 old_nt_hash_enc
= data_blob(NULL
, 16);
2046 /* Calculate the MD4 hash (NT compatible) of the
2048 E_md4hash(oldpswd
, old_nt_hash
);
2049 E_md4hash(newpswd
, new_nt_hash
);
2051 /* E_deshash returns false for 'long'
2052 passwords (> 14 DOS chars).
2054 Therefore, don't send a buffer
2055 encrypted with the truncated hash
2056 (it could allow an even easier
2057 attack on the password)
2059 Likewise, obey the admin's restriction
2062 if (lp_client_lanman_auth() &&
2063 E_deshash(newpswd
, new_lm_hash
) &&
2064 E_deshash(oldpswd
, old_lm_hash
)) {
2065 new_lm_pswd
= data_blob(NULL
, 516);
2066 old_lm_hash_enc
= data_blob(NULL
, 16);
2067 encode_pw_buffer(new_lm_pswd
.data
, newpswd
,
2070 arcfour_crypt(new_lm_pswd
.data
, old_nt_hash
, 516);
2071 E_old_pw_hash(new_nt_hash
, old_lm_hash
,
2072 old_lm_hash_enc
.data
);
2074 new_lm_pswd
.data
= NULL
;
2075 new_lm_pswd
.length
= 0;
2076 old_lm_hash_enc
.data
= NULL
;
2077 old_lm_hash_enc
.length
= 0;
2080 encode_pw_buffer(new_nt_pswd
.data
, newpswd
,
2083 arcfour_crypt(new_nt_pswd
.data
, old_nt_hash
, 516);
2084 E_old_pw_hash(new_nt_hash
, old_nt_hash
,
2085 old_nt_hash_enc
.data
);
2088 if (!full_username
&& !username
) {
2089 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2090 } else if ((!new_nt_pswd
.data
|| !old_nt_hash_enc
.data
) &&
2091 (!new_lm_pswd
.data
|| old_lm_hash_enc
.data
) ) {
2092 x_fprintf(x_stdout
, "Error: No NT or LM password "
2093 "blobs supplied!\n");
2095 char *error_string
= NULL
;
2097 if (full_username
&& !username
) {
2099 fstring fstr_domain
;
2101 if (!parse_ntlm_auth_domain_user(full_username
,
2104 /* username might be 'tainted', don't
2105 * print into our new-line
2106 * deleimianted stream */
2107 x_fprintf(x_stdout
, "Error: Could not "
2108 "parse into domain and "
2110 SAFE_FREE(username
);
2111 username
= smb_xstrdup(full_username
);
2113 SAFE_FREE(username
);
2115 username
= smb_xstrdup(fstr_user
);
2116 domain
= smb_xstrdup(fstr_domain
);
2121 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2128 x_fprintf(x_stdout
, "Password-Change: No\n");
2129 x_fprintf(x_stdout
, "Password-Change-Error: "
2130 "%s\n.\n", error_string
);
2132 x_fprintf(x_stdout
, "Password-Change: Yes\n");
2135 SAFE_FREE(error_string
);
2137 /* clear out the state */
2138 new_nt_pswd
= data_blob_null
;
2139 old_nt_hash_enc
= data_blob_null
;
2140 new_lm_pswd
= data_blob_null
;
2141 old_nt_hash_enc
= data_blob_null
;
2142 SAFE_FREE(full_username
);
2143 SAFE_FREE(username
);
2147 x_fprintf(x_stdout
, ".\n");
2154 /* Indicates a base64 encoded structure */
2155 parameter
= strstr_m(request
, ":: ");
2157 parameter
= strstr_m(request
, ": ");
2160 DEBUG(0, ("Parameter not found!\n"));
2161 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2177 base64_decode_inplace(parameter
);
2180 if (strequal(request
, "new-nt-password-blob")) {
2181 new_nt_pswd
= strhex_to_data_blob(NULL
, parameter
);
2182 if (new_nt_pswd
.length
!= 516) {
2183 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2184 "(got %d bytes, expected 516)\n.\n",
2186 (int)new_nt_pswd
.length
);
2187 new_nt_pswd
= data_blob_null
;
2189 } else if (strequal(request
, "old-nt-hash-blob")) {
2190 old_nt_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2191 if (old_nt_hash_enc
.length
!= 16) {
2192 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2193 "(got %d bytes, expected 16)\n.\n",
2195 (int)old_nt_hash_enc
.length
);
2196 old_nt_hash_enc
= data_blob_null
;
2198 } else if (strequal(request
, "new-lm-password-blob")) {
2199 new_lm_pswd
= strhex_to_data_blob(NULL
, parameter
);
2200 if (new_lm_pswd
.length
!= 516) {
2201 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2202 "(got %d bytes, expected 516)\n.\n",
2204 (int)new_lm_pswd
.length
);
2205 new_lm_pswd
= data_blob_null
;
2208 else if (strequal(request
, "old-lm-hash-blob")) {
2209 old_lm_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2210 if (old_lm_hash_enc
.length
!= 16)
2212 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2213 "(got %d bytes, expected 16)\n.\n",
2215 (int)old_lm_hash_enc
.length
);
2216 old_lm_hash_enc
= data_blob_null
;
2218 } else if (strequal(request
, "nt-domain")) {
2219 domain
= smb_xstrdup(parameter
);
2220 } else if(strequal(request
, "username")) {
2221 username
= smb_xstrdup(parameter
);
2222 } else if(strequal(request
, "full-username")) {
2223 username
= smb_xstrdup(parameter
);
2224 } else if(strequal(request
, "new-password")) {
2225 newpswd
= smb_xstrdup(parameter
);
2226 } else if (strequal(request
, "old-password")) {
2227 oldpswd
= smb_xstrdup(parameter
);
2229 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2233 static void manage_squid_request(struct ntlm_auth_state
*state
,
2234 stdio_helper_function fn
)
2237 char tmp
[INITIAL_BUFFER_SIZE
+1];
2238 int length
, buf_size
= 0;
2241 buf
= talloc_strdup(state
->mem_ctx
, "");
2243 DEBUG(0, ("Failed to allocate input buffer.\n"));
2244 x_fprintf(x_stderr
, "ERR\n");
2250 /* this is not a typo - x_fgets doesn't work too well under
2252 if (fgets(tmp
, sizeof(tmp
)-1, stdin
) == NULL
) {
2253 if (ferror(stdin
)) {
2254 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2255 "(%s)\n", ferror(stdin
),
2256 strerror(ferror(stdin
))));
2263 buf
= talloc_strdup_append_buffer(buf
, tmp
);
2264 buf_size
+= INITIAL_BUFFER_SIZE
;
2266 if (buf_size
> MAX_BUFFER_SIZE
) {
2267 DEBUG(2, ("Oversized message\n"));
2268 x_fprintf(x_stderr
, "ERR\n");
2273 c
= strchr(buf
, '\n');
2274 } while (c
== NULL
);
2279 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
2281 if (buf
[0] == '\0') {
2282 DEBUG(2, ("Invalid Request\n"));
2283 x_fprintf(x_stderr
, "ERR\n");
2288 fn(state
, buf
, length
);
2293 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
2294 TALLOC_CTX
*mem_ctx
;
2295 struct ntlm_auth_state
*state
;
2297 /* initialize FDescs */
2298 x_setbuf(x_stdout
, NULL
);
2299 x_setbuf(x_stderr
, NULL
);
2301 mem_ctx
= talloc_init("ntlm_auth");
2303 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2304 x_fprintf(x_stderr
, "ERR\n");
2308 state
= talloc_zero(mem_ctx
, struct ntlm_auth_state
);
2310 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2311 x_fprintf(x_stderr
, "ERR\n");
2315 state
->mem_ctx
= mem_ctx
;
2316 state
->helper_mode
= stdio_mode
;
2319 manage_squid_request(state
, fn
);
2324 /* Authenticate a user with a challenge/response */
2326 static bool check_auth_crap(void)
2331 char user_session_key
[16];
2333 char *hex_user_session_key
;
2335 static uint8 zeros
[16];
2337 x_setbuf(x_stdout
, NULL
);
2340 flags
|= WBFLAG_PAM_LMKEY
;
2342 if (request_user_session_key
)
2343 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2345 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
2347 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
2353 (unsigned char *)lm_key
,
2354 (unsigned char *)user_session_key
,
2355 &error_string
, NULL
);
2357 if (!NT_STATUS_IS_OK(nt_status
)) {
2358 x_fprintf(x_stdout
, "%s (0x%x)\n",
2360 NT_STATUS_V(nt_status
));
2361 SAFE_FREE(error_string
);
2366 && (memcmp(zeros
, lm_key
,
2367 sizeof(lm_key
)) != 0)) {
2368 hex_lm_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key
,
2370 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
2371 TALLOC_FREE(hex_lm_key
);
2373 if (request_user_session_key
2374 && (memcmp(zeros
, user_session_key
,
2375 sizeof(user_session_key
)) != 0)) {
2376 hex_user_session_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key
,
2377 sizeof(user_session_key
));
2378 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
2379 TALLOC_FREE(hex_user_session_key
);
2388 OPT_USERNAME
= 1000,
2397 OPT_USER_SESSION_KEY
,
2399 OPT_REQUIRE_MEMBERSHIP
,
2400 OPT_USE_CACHED_CREDS
,
2401 OPT_PAM_WINBIND_CONF
2404 int main(int argc
, const char **argv
)
2406 TALLOC_CTX
*frame
= talloc_stackframe();
2408 static const char *helper_protocol
;
2409 static int diagnostics
;
2411 static const char *hex_challenge
;
2412 static const char *hex_lm_response
;
2413 static const char *hex_nt_response
;
2417 /* NOTE: DO NOT change this interface without considering the implications!
2418 This is an external interface, which other programs will use to interact
2422 /* We do not use single-letter command abbreviations, because they harm future
2423 interface stability. */
2425 struct poptOption long_options
[] = {
2427 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
2428 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
2429 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
2430 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
2431 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
2432 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
2433 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2434 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
2435 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retrieve LM session key"},
2436 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retrieve User (NT) session key"},
2437 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "Use cached credentials if no password is given"},
2438 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
, OPT_DIAGNOSTICS
, "Perform diagnostics on the authentictaion chain"},
2439 { "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" },
2440 { "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" },
2441 POPT_COMMON_CONFIGFILE
2446 /* Samba client initialisation */
2453 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
2455 /* Parse command line options */
2458 poptPrintHelp(pc
, stderr
, 0);
2462 while((opt
= poptGetNextOpt(pc
)) != -1) {
2463 /* Get generic config options like --configfile */
2466 poptFreeContext(pc
);
2468 if (!lp_load(get_dyn_CONFIGFILE(), True
, False
, False
, True
)) {
2469 d_fprintf(stderr
, "ntlm_auth: error opening config file %s. Error was %s\n",
2470 get_dyn_CONFIGFILE(), strerror(errno
));
2474 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2475 POPT_CONTEXT_KEEP_FIRST
);
2477 while((opt
= poptGetNextOpt(pc
)) != -1) {
2480 opt_challenge
= strhex_to_data_blob(NULL
, hex_challenge
);
2481 if (opt_challenge
.length
!= 8) {
2482 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2484 (int)opt_challenge
.length
);
2489 opt_lm_response
= strhex_to_data_blob(NULL
, hex_lm_response
);
2490 if (opt_lm_response
.length
!= 24) {
2491 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2493 (int)opt_lm_response
.length
);
2499 opt_nt_response
= strhex_to_data_blob(NULL
, hex_nt_response
);
2500 if (opt_nt_response
.length
< 24) {
2501 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2503 (int)opt_nt_response
.length
);
2508 case OPT_REQUIRE_MEMBERSHIP
:
2509 if (StrnCaseCmp("S-", require_membership_of
, 2) == 0) {
2510 require_membership_of_sid
= require_membership_of
;
2517 char *domain
= SMB_STRDUP(opt_username
);
2518 char *p
= strchr_m(domain
, *lp_winbind_separator());
2522 if (opt_domain
&& !strequal(opt_domain
, domain
)) {
2523 x_fprintf(x_stderr
, "Domain specified in username (%s) "
2524 "doesn't match specified domain (%s)!\n\n",
2525 domain
, opt_domain
);
2526 poptPrintHelp(pc
, stderr
, 0);
2529 opt_domain
= domain
;
2535 /* Note: if opt_domain is "" then send no domain */
2536 if (opt_domain
== NULL
) {
2537 opt_domain
= get_winbind_domain();
2540 if (opt_workstation
== NULL
) {
2541 opt_workstation
= "";
2544 if (helper_protocol
) {
2546 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2547 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
2548 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
2552 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
2554 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2555 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
2561 if (!opt_username
|| !*opt_username
) {
2562 x_fprintf(x_stderr
, "username must be specified!\n\n");
2563 poptPrintHelp(pc
, stderr
, 0);
2567 if (opt_challenge
.length
) {
2568 if (!check_auth_crap()) {
2574 if (!opt_password
) {
2575 opt_password
= getpass("password: ");
2579 if (!diagnose_ntlm_auth()) {
2585 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
2586 if (!check_plaintext_auth(user
, opt_password
, True
)) {
2593 poptFreeContext(pc
);