2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
9 Copyright (C) Robert O'Callahan 2006 (added cached credential code).
10 Copyright (C) Kai Blin <kai@samba.org> 2008
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "popt_common.h"
28 #include "utils/ntlm_auth.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "../libcli/auth/spnego.h"
31 #include "../libcli/auth/ntlmssp.h"
33 #include <iniparser.h>
34 #include "../lib/crypto/arcfour.h"
35 #include "libads/kerberos_proto.h"
36 #include "nsswitch/winbind_client.h"
38 #ifndef PAM_WINBIND_CONFIG_FILE
39 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
42 #define WINBIND_KRB5_AUTH 0x00000080
45 #define DBGC_CLASS DBGC_WINBIND
47 #define INITIAL_BUFFER_SIZE 300
48 #define MAX_BUFFER_SIZE 630000
50 enum stdio_helper_mode
{
58 NTLM_CHANGE_PASSWORD_1
,
62 enum ntlm_auth_cli_state
{
69 enum ntlm_auth_svr_state
{
76 struct ntlm_auth_state
{
78 enum stdio_helper_mode helper_mode
;
79 enum ntlm_auth_cli_state cli_state
;
80 enum ntlm_auth_svr_state svr_state
;
81 struct ntlmssp_state
*ntlmssp_state
;
83 char *want_feature_list
;
84 bool have_session_key
;
85 DATA_BLOB session_key
;
86 DATA_BLOB initial_message
;
89 typedef void (*stdio_helper_function
)(struct ntlm_auth_state
*state
, char *buf
,
92 static void manage_squid_basic_request (struct ntlm_auth_state
*state
,
93 char *buf
, int length
);
95 static void manage_squid_ntlmssp_request (struct ntlm_auth_state
*state
,
96 char *buf
, int length
);
98 static void manage_client_ntlmssp_request (struct ntlm_auth_state
*state
,
99 char *buf
, int length
);
101 static void manage_gss_spnego_request (struct ntlm_auth_state
*state
,
102 char *buf
, int length
);
104 static void manage_gss_spnego_client_request (struct ntlm_auth_state
*state
,
105 char *buf
, int length
);
107 static void manage_ntlm_server_1_request (struct ntlm_auth_state
*state
,
108 char *buf
, int length
);
110 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
111 char *buf
, int length
);
113 static const struct {
114 enum stdio_helper_mode mode
;
116 stdio_helper_function fn
;
117 } stdio_helper_protocols
[] = {
118 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
119 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
120 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request
},
121 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_client_ntlmssp_request
},
122 { GSS_SPNEGO
, "gss-spnego", manage_gss_spnego_request
},
123 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gss_spnego_client_request
},
124 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
125 { NTLM_CHANGE_PASSWORD_1
, "ntlm-change-password-1", manage_ntlm_change_password_1_request
},
126 { NUM_HELPER_MODES
, NULL
, NULL
}
129 const char *opt_username
;
130 const char *opt_domain
;
131 const char *opt_workstation
;
132 const char *opt_password
;
133 static DATA_BLOB opt_challenge
;
134 static DATA_BLOB opt_lm_response
;
135 static DATA_BLOB opt_nt_response
;
136 static int request_lm_key
;
137 static int request_user_session_key
;
138 static int use_cached_creds
;
140 static const char *require_membership_of
;
141 static const char *require_membership_of_sid
;
142 static const char *opt_pam_winbind_conf
;
144 static char winbind_separator(void)
146 struct winbindd_response response
;
153 ZERO_STRUCT(response
);
155 /* Send off request */
157 if (winbindd_request_response(WINBINDD_INFO
, NULL
, &response
) !=
158 NSS_STATUS_SUCCESS
) {
159 d_printf("could not obtain winbind separator!\n");
160 return *lp_winbind_separator();
163 sep
= response
.data
.info
.winbind_separator
;
167 d_printf("winbind separator was NULL!\n");
168 return *lp_winbind_separator();
174 const char *get_winbind_domain(void)
176 struct winbindd_response response
;
178 static fstring winbind_domain
;
179 if (*winbind_domain
) {
180 return winbind_domain
;
183 ZERO_STRUCT(response
);
185 /* Send off request */
187 if (winbindd_request_response(WINBINDD_DOMAIN_NAME
, NULL
, &response
) !=
188 NSS_STATUS_SUCCESS
) {
189 DEBUG(0, ("could not obtain winbind domain name!\n"));
190 return lp_workgroup();
193 fstrcpy(winbind_domain
, response
.data
.domain_name
);
195 return winbind_domain
;
199 const char *get_winbind_netbios_name(void)
201 struct winbindd_response response
;
203 static fstring winbind_netbios_name
;
205 if (*winbind_netbios_name
) {
206 return winbind_netbios_name
;
209 ZERO_STRUCT(response
);
211 /* Send off request */
213 if (winbindd_request_response(WINBINDD_NETBIOS_NAME
, NULL
, &response
) !=
214 NSS_STATUS_SUCCESS
) {
215 DEBUG(0, ("could not obtain winbind netbios name!\n"));
216 return global_myname();
219 fstrcpy(winbind_netbios_name
, response
.data
.netbios_name
);
221 return winbind_netbios_name
;
225 DATA_BLOB
get_challenge(void)
227 static DATA_BLOB chal
;
228 if (opt_challenge
.length
)
229 return opt_challenge
;
231 chal
= data_blob(NULL
, 8);
233 generate_random_buffer(chal
.data
, chal
.length
);
237 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
238 form DOMAIN/user into a domain and a user */
240 static bool parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
244 char *p
= strchr(domuser
,winbind_separator());
251 fstrcpy(domain
, domuser
);
252 domain
[PTR_DIFF(p
, domuser
)] = 0;
258 static bool get_require_membership_sid(void) {
259 struct winbindd_request request
;
260 struct winbindd_response response
;
262 if (!require_membership_of
) {
266 if (require_membership_of_sid
) {
270 /* Otherwise, ask winbindd for the name->sid request */
272 ZERO_STRUCT(request
);
273 ZERO_STRUCT(response
);
275 if (!parse_ntlm_auth_domain_user(require_membership_of
,
276 request
.data
.name
.dom_name
,
277 request
.data
.name
.name
)) {
278 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
279 require_membership_of
));
283 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
284 NSS_STATUS_SUCCESS
) {
285 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
286 require_membership_of
));
290 require_membership_of_sid
= SMB_STRDUP(response
.data
.sid
.sid
);
292 if (require_membership_of_sid
)
299 * Get some configuration from pam_winbind.conf to see if we
300 * need to contact trusted domain
303 int get_pam_winbind_config()
306 dictionary
*d
= NULL
;
308 if (!opt_pam_winbind_conf
|| !*opt_pam_winbind_conf
) {
309 opt_pam_winbind_conf
= PAM_WINBIND_CONFIG_FILE
;
312 d
= iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf
));
318 if (iniparser_getboolean(d
, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
319 ctrl
|= WINBIND_KRB5_AUTH
;
322 iniparser_freedict(d
);
327 /* Authenticate a user with a plaintext password */
329 static bool check_plaintext_auth(const char *user
, const char *pass
,
330 bool stdout_diagnostics
)
332 struct winbindd_request request
;
333 struct winbindd_response response
;
336 if (!get_require_membership_sid()) {
340 /* Send off request */
342 ZERO_STRUCT(request
);
343 ZERO_STRUCT(response
);
345 fstrcpy(request
.data
.auth
.user
, user
);
346 fstrcpy(request
.data
.auth
.pass
, pass
);
347 if (require_membership_of_sid
) {
348 strlcpy(request
.data
.auth
.require_membership_of_sid
,
349 require_membership_of_sid
,
350 sizeof(request
.data
.auth
.require_membership_of_sid
));
353 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
355 /* Display response */
357 if (stdout_diagnostics
) {
358 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
359 d_printf("Reading winbind reply failed! (0x01)\n");
362 d_printf("%s: %s (0x%x)\n",
363 response
.data
.auth
.nt_status_string
,
364 response
.data
.auth
.error_string
,
365 response
.data
.auth
.nt_status
);
367 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
368 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
371 DEBUG(3, ("%s: %s (0x%x)\n",
372 response
.data
.auth
.nt_status_string
,
373 response
.data
.auth
.error_string
,
374 response
.data
.auth
.nt_status
));
377 return (result
== NSS_STATUS_SUCCESS
);
380 /* authenticate a user with an encrypted username/password */
382 NTSTATUS
contact_winbind_auth_crap(const char *username
,
384 const char *workstation
,
385 const DATA_BLOB
*challenge
,
386 const DATA_BLOB
*lm_response
,
387 const DATA_BLOB
*nt_response
,
390 uint8 user_session_key
[16],
396 struct winbindd_request request
;
397 struct winbindd_response response
;
399 if (!get_require_membership_sid()) {
400 return NT_STATUS_INVALID_PARAMETER
;
403 ZERO_STRUCT(request
);
404 ZERO_STRUCT(response
);
406 request
.flags
= flags
;
408 request
.data
.auth_crap
.logon_parameters
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
| MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
410 if (require_membership_of_sid
)
411 fstrcpy(request
.data
.auth_crap
.require_membership_of_sid
, require_membership_of_sid
);
413 fstrcpy(request
.data
.auth_crap
.user
, username
);
414 fstrcpy(request
.data
.auth_crap
.domain
, domain
);
416 fstrcpy(request
.data
.auth_crap
.workstation
,
419 memcpy(request
.data
.auth_crap
.chal
, challenge
->data
, MIN(challenge
->length
, 8));
421 if (lm_response
&& lm_response
->length
) {
422 memcpy(request
.data
.auth_crap
.lm_resp
,
424 MIN(lm_response
->length
, sizeof(request
.data
.auth_crap
.lm_resp
)));
425 request
.data
.auth_crap
.lm_resp_len
= lm_response
->length
;
428 if (nt_response
&& nt_response
->length
) {
429 if (nt_response
->length
> sizeof(request
.data
.auth_crap
.nt_resp
)) {
430 request
.flags
= request
.flags
| WBFLAG_BIG_NTLMV2_BLOB
;
431 request
.extra_len
= nt_response
->length
;
432 request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, request
.extra_len
);
433 if (request
.extra_data
.data
== NULL
) {
434 return NT_STATUS_NO_MEMORY
;
436 memcpy(request
.extra_data
.data
, nt_response
->data
,
437 nt_response
->length
);
440 memcpy(request
.data
.auth_crap
.nt_resp
,
441 nt_response
->data
, nt_response
->length
);
443 request
.data
.auth_crap
.nt_resp_len
= nt_response
->length
;
446 result
= winbindd_request_response(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
447 SAFE_FREE(request
.extra_data
.data
);
449 /* Display response */
451 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
452 nt_status
= NT_STATUS_UNSUCCESSFUL
;
454 *error_string
= smb_xstrdup("Reading winbind reply failed!");
455 winbindd_free_response(&response
);
459 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
460 if (!NT_STATUS_IS_OK(nt_status
)) {
462 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
463 winbindd_free_response(&response
);
467 if ((flags
& WBFLAG_PAM_LMKEY
) && lm_key
) {
468 memcpy(lm_key
, response
.data
.auth
.first_8_lm_hash
,
469 sizeof(response
.data
.auth
.first_8_lm_hash
));
471 if ((flags
& WBFLAG_PAM_USER_SESSION_KEY
) && user_session_key
) {
472 memcpy(user_session_key
, response
.data
.auth
.user_session_key
,
473 sizeof(response
.data
.auth
.user_session_key
));
476 if (flags
& WBFLAG_PAM_UNIX_NAME
) {
477 *unix_name
= SMB_STRDUP(response
.data
.auth
.unix_username
);
479 winbindd_free_response(&response
);
480 return NT_STATUS_NO_MEMORY
;
484 winbindd_free_response(&response
);
488 /* contact server to change user password using auth crap */
489 static NTSTATUS
contact_winbind_change_pswd_auth_crap(const char *username
,
491 const DATA_BLOB new_nt_pswd
,
492 const DATA_BLOB old_nt_hash_enc
,
493 const DATA_BLOB new_lm_pswd
,
494 const DATA_BLOB old_lm_hash_enc
,
499 struct winbindd_request request
;
500 struct winbindd_response response
;
502 if (!get_require_membership_sid())
505 *error_string
= smb_xstrdup("Can't get membership sid.");
506 return NT_STATUS_INVALID_PARAMETER
;
509 ZERO_STRUCT(request
);
510 ZERO_STRUCT(response
);
513 fstrcpy(request
.data
.chng_pswd_auth_crap
.user
, username
);
515 fstrcpy(request
.data
.chng_pswd_auth_crap
.domain
,domain
);
517 if(new_nt_pswd
.length
)
519 memcpy(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
, new_nt_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
));
520 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
= new_nt_pswd
.length
;
523 if(old_nt_hash_enc
.length
)
525 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
));
526 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
= old_nt_hash_enc
.length
;
529 if(new_lm_pswd
.length
)
531 memcpy(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
, new_lm_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
));
532 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
= new_lm_pswd
.length
;
535 if(old_lm_hash_enc
.length
)
537 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
));
538 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
= old_lm_hash_enc
.length
;
541 result
= winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, &request
, &response
);
543 /* Display response */
545 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0))
547 nt_status
= NT_STATUS_UNSUCCESSFUL
;
549 *error_string
= smb_xstrdup("Reading winbind reply failed!");
550 winbindd_free_response(&response
);
554 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
555 if (!NT_STATUS_IS_OK(nt_status
))
558 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
559 winbindd_free_response(&response
);
563 winbindd_free_response(&response
);
568 static NTSTATUS
winbind_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
570 static const char zeros
[16] = { 0, };
572 char *error_string
= NULL
;
574 uint8 user_sess_key
[16];
575 char *unix_name
= NULL
;
577 nt_status
= contact_winbind_auth_crap(ntlmssp_state
->user
, ntlmssp_state
->domain
,
578 ntlmssp_state
->client
.netbios_name
,
579 &ntlmssp_state
->chal
,
580 &ntlmssp_state
->lm_resp
,
581 &ntlmssp_state
->nt_resp
,
582 WBFLAG_PAM_LMKEY
| WBFLAG_PAM_USER_SESSION_KEY
| WBFLAG_PAM_UNIX_NAME
,
583 lm_key
, user_sess_key
,
584 &error_string
, &unix_name
);
586 if (NT_STATUS_IS_OK(nt_status
)) {
587 if (memcmp(lm_key
, zeros
, 8) != 0) {
588 *lm_session_key
= data_blob_talloc(ntlmssp_state
, NULL
, 16);
589 memcpy(lm_session_key
->data
, lm_key
, 8);
590 memset(lm_session_key
->data
+8, '\0', 8);
593 if (memcmp(user_sess_key
, zeros
, 16) != 0) {
594 *user_session_key
= data_blob_talloc(ntlmssp_state
, user_sess_key
, 16);
596 ntlmssp_state
->callback_private
= talloc_strdup(ntlmssp_state
,
599 DEBUG(NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
) ? 0 : 3,
600 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
601 ntlmssp_state
->domain
, ntlmssp_state
->user
,
602 ntlmssp_state
->client
.netbios_name
,
603 error_string
? error_string
: "unknown error (NULL)"));
604 ntlmssp_state
->callback_private
= NULL
;
607 SAFE_FREE(error_string
);
608 SAFE_FREE(unix_name
);
612 static NTSTATUS
local_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
615 struct samr_Password lm_pw
, nt_pw
;
617 nt_lm_owf_gen (opt_password
, nt_pw
.hash
, lm_pw
.hash
);
619 nt_status
= ntlm_password_check(ntlmssp_state
,
621 &ntlmssp_state
->chal
,
622 &ntlmssp_state
->lm_resp
,
623 &ntlmssp_state
->nt_resp
,
626 ntlmssp_state
->domain
,
627 &lm_pw
, &nt_pw
, user_session_key
, lm_session_key
);
629 if (NT_STATUS_IS_OK(nt_status
)) {
630 ntlmssp_state
->callback_private
= talloc_asprintf(ntlmssp_state
,
631 "%s%c%s", ntlmssp_state
->domain
,
632 *lp_winbind_separator(),
633 ntlmssp_state
->user
);
635 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
636 ntlmssp_state
->domain
, ntlmssp_state
->user
,
637 ntlmssp_state
->client
.netbios_name
,
638 nt_errstr(nt_status
)));
639 ntlmssp_state
->callback_private
= NULL
;
644 static NTSTATUS
ntlm_auth_start_ntlmssp_client(struct ntlmssp_state
**client_ntlmssp_state
)
647 if ( (opt_username
== NULL
) || (opt_domain
== NULL
) ) {
648 status
= NT_STATUS_UNSUCCESSFUL
;
649 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
650 return NT_STATUS_INVALID_PARAMETER
;
653 status
= ntlmssp_client_start(NULL
,
656 lp_client_ntlmv2_auth(),
657 client_ntlmssp_state
);
659 if (!NT_STATUS_IS_OK(status
)) {
660 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
662 TALLOC_FREE(*client_ntlmssp_state
);
666 status
= ntlmssp_set_username(*client_ntlmssp_state
, opt_username
);
668 if (!NT_STATUS_IS_OK(status
)) {
669 DEBUG(1, ("Could not set username: %s\n",
671 TALLOC_FREE(*client_ntlmssp_state
);
675 status
= ntlmssp_set_domain(*client_ntlmssp_state
, opt_domain
);
677 if (!NT_STATUS_IS_OK(status
)) {
678 DEBUG(1, ("Could not set domain: %s\n",
680 TALLOC_FREE(*client_ntlmssp_state
);
685 status
= ntlmssp_set_password(*client_ntlmssp_state
, opt_password
);
687 if (!NT_STATUS_IS_OK(status
)) {
688 DEBUG(1, ("Could not set password: %s\n",
690 TALLOC_FREE(*client_ntlmssp_state
);
698 static NTSTATUS
ntlm_auth_start_ntlmssp_server(struct ntlmssp_state
**ntlmssp_state
)
701 const char *netbios_name
;
702 const char *netbios_domain
;
703 const char *dns_name
;
705 bool is_standalone
= false;
708 netbios_name
= global_myname();
709 netbios_domain
= lp_workgroup();
711 netbios_name
= get_winbind_netbios_name();
712 netbios_domain
= get_winbind_domain();
714 /* This should be a 'netbios domain -> DNS domain' mapping */
715 dns_domain
= get_mydnsdomname(talloc_tos());
717 strlower_m(dns_domain
);
719 dns_name
= get_mydnsfullname();
721 status
= ntlmssp_server_start(NULL
,
728 if (!NT_STATUS_IS_OK(status
)) {
729 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
734 /* Have we been given a local password, or should we ask winbind? */
736 (*ntlmssp_state
)->check_password
= local_pw_check
;
738 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
743 /*******************************************************************
744 Used by firefox to drive NTLM auth to IIS servers.
745 *******************************************************************/
747 static NTSTATUS
do_ccache_ntlm_auth(DATA_BLOB initial_msg
, DATA_BLOB challenge_msg
,
750 struct winbindd_request wb_request
;
751 struct winbindd_response wb_response
;
755 /* get winbindd to do the ntlmssp step on our behalf */
756 ZERO_STRUCT(wb_request
);
757 ZERO_STRUCT(wb_response
);
760 * This is tricky here. If we set krb5_auth in pam_winbind.conf
761 * creds for users in trusted domain will be stored the winbindd
762 * child of the trusted domain. If we ask the primary domain for
763 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
764 * domain's child for ccache_ntlm_auth. that is to say, we have to
765 * set WBFLAG_PAM_CONTACT_TRUSTDOM in request.flags.
767 ctrl
= get_pam_winbind_config();
769 if (ctrl
| WINBIND_KRB5_AUTH
) {
770 wb_request
.flags
|= WBFLAG_PAM_CONTACT_TRUSTDOM
;
773 fstr_sprintf(wb_request
.data
.ccache_ntlm_auth
.user
,
774 "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
775 wb_request
.data
.ccache_ntlm_auth
.uid
= geteuid();
776 wb_request
.data
.ccache_ntlm_auth
.initial_blob_len
= initial_msg
.length
;
777 wb_request
.data
.ccache_ntlm_auth
.challenge_blob_len
= challenge_msg
.length
;
778 wb_request
.extra_len
= initial_msg
.length
+ challenge_msg
.length
;
780 if (wb_request
.extra_len
> 0) {
781 wb_request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, wb_request
.extra_len
);
782 if (wb_request
.extra_data
.data
== NULL
) {
783 return NT_STATUS_NO_MEMORY
;
786 memcpy(wb_request
.extra_data
.data
, initial_msg
.data
, initial_msg
.length
);
787 memcpy(wb_request
.extra_data
.data
+ initial_msg
.length
,
788 challenge_msg
.data
, challenge_msg
.length
);
791 result
= winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH
, &wb_request
, &wb_response
);
792 SAFE_FREE(wb_request
.extra_data
.data
);
794 if (result
!= NSS_STATUS_SUCCESS
) {
795 winbindd_free_response(&wb_response
);
796 return NT_STATUS_UNSUCCESSFUL
;
800 *reply
= data_blob(wb_response
.extra_data
.data
,
801 wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
);
802 if (wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
> 0 &&
803 reply
->data
== NULL
) {
804 winbindd_free_response(&wb_response
);
805 return NT_STATUS_NO_MEMORY
;
809 winbindd_free_response(&wb_response
);
810 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
813 static void manage_squid_ntlmssp_request(struct ntlm_auth_state
*state
,
814 char *buf
, int length
)
816 DATA_BLOB request
, reply
;
819 if (strlen(buf
) < 2) {
820 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
821 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
825 if (strlen(buf
) > 3) {
826 if(strncmp(buf
, "SF ", 3) == 0){
827 DEBUG(10, ("Setting flags to negotioate\n"));
828 TALLOC_FREE(state
->want_feature_list
);
829 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
831 x_fprintf(x_stdout
, "OK\n");
834 request
= base64_decode_data_blob(buf
+ 3);
836 request
= data_blob_null
;
839 if ((strncmp(buf
, "PW ", 3) == 0)) {
840 /* The calling application wants us to use a local password
841 * (rather than winbindd) */
843 opt_password
= SMB_STRNDUP((const char *)request
.data
,
846 if (opt_password
== NULL
) {
847 DEBUG(1, ("Out of memory\n"));
848 x_fprintf(x_stdout
, "BH Out of memory\n");
849 data_blob_free(&request
);
853 x_fprintf(x_stdout
, "OK\n");
854 data_blob_free(&request
);
858 if (strncmp(buf
, "YR", 2) == 0) {
859 TALLOC_FREE(state
->ntlmssp_state
);
860 state
->svr_state
= SERVER_INITIAL
;
861 } else if (strncmp(buf
, "KK", 2) == 0) {
862 /* No special preprocessing required */
863 } else if (strncmp(buf
, "GF", 2) == 0) {
864 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
866 if (state
->svr_state
== SERVER_FINISHED
) {
867 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
870 x_fprintf(x_stdout
, "BH\n");
872 data_blob_free(&request
);
874 } else if (strncmp(buf
, "GK", 2) == 0) {
875 DEBUG(10, ("Requested NTLMSSP session key\n"));
876 if(state
->have_session_key
) {
877 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
879 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
882 x_fprintf(x_stdout
, "BH\n");
885 data_blob_free(&request
);
888 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
889 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
893 if (!state
->ntlmssp_state
) {
894 nt_status
= ntlm_auth_start_ntlmssp_server(
895 &state
->ntlmssp_state
);
896 if (!NT_STATUS_IS_OK(nt_status
)) {
897 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
900 ntlmssp_want_feature_list(state
->ntlmssp_state
,
901 state
->want_feature_list
);
904 DEBUG(10, ("got NTLMSSP packet:\n"));
905 dump_data(10, request
.data
, request
.length
);
907 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
, &reply
);
909 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
910 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
912 x_fprintf(x_stdout
, "TT %s\n", reply_base64
);
913 TALLOC_FREE(reply_base64
);
914 data_blob_free(&reply
);
915 state
->svr_state
= SERVER_CHALLENGE
;
916 DEBUG(10, ("NTLMSSP challenge\n"));
917 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
918 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
919 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
921 TALLOC_FREE(state
->ntlmssp_state
);
922 } else if (!NT_STATUS_IS_OK(nt_status
)) {
923 x_fprintf(x_stdout
, "NA %s\n", nt_errstr(nt_status
));
924 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
926 x_fprintf(x_stdout
, "AF %s\n",
927 (char *)state
->ntlmssp_state
->callback_private
);
928 DEBUG(10, ("NTLMSSP OK!\n"));
930 if(state
->have_session_key
)
931 data_blob_free(&state
->session_key
);
932 state
->session_key
= data_blob(
933 state
->ntlmssp_state
->session_key
.data
,
934 state
->ntlmssp_state
->session_key
.length
);
935 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
936 state
->have_session_key
= true;
937 state
->svr_state
= SERVER_FINISHED
;
940 data_blob_free(&request
);
943 static void manage_client_ntlmssp_request(struct ntlm_auth_state
*state
,
944 char *buf
, int length
)
946 DATA_BLOB request
, reply
;
949 if (!opt_username
|| !*opt_username
) {
950 x_fprintf(x_stderr
, "username must be specified!\n\n");
954 if (strlen(buf
) < 2) {
955 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
956 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
960 if (strlen(buf
) > 3) {
961 if(strncmp(buf
, "SF ", 3) == 0) {
962 DEBUG(10, ("Looking for flags to negotiate\n"));
963 talloc_free(state
->want_feature_list
);
964 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
966 x_fprintf(x_stdout
, "OK\n");
969 request
= base64_decode_data_blob(buf
+ 3);
971 request
= data_blob_null
;
974 if (strncmp(buf
, "PW ", 3) == 0) {
975 /* We asked for a password and obviously got it :-) */
977 opt_password
= SMB_STRNDUP((const char *)request
.data
,
980 if (opt_password
== NULL
) {
981 DEBUG(1, ("Out of memory\n"));
982 x_fprintf(x_stdout
, "BH Out of memory\n");
983 data_blob_free(&request
);
987 x_fprintf(x_stdout
, "OK\n");
988 data_blob_free(&request
);
992 if (!state
->ntlmssp_state
&& use_cached_creds
) {
993 /* check whether cached credentials are usable. */
994 DATA_BLOB empty_blob
= data_blob_null
;
996 nt_status
= do_ccache_ntlm_auth(empty_blob
, empty_blob
, NULL
);
997 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
998 /* failed to use cached creds */
999 use_cached_creds
= False
;
1003 if (opt_password
== NULL
&& !use_cached_creds
) {
1004 /* Request a password from the calling process. After
1005 sending it, the calling process should retry asking for the
1008 DEBUG(10, ("Requesting password\n"));
1009 x_fprintf(x_stdout
, "PW\n");
1013 if (strncmp(buf
, "YR", 2) == 0) {
1014 TALLOC_FREE(state
->ntlmssp_state
);
1015 state
->cli_state
= CLIENT_INITIAL
;
1016 } else if (strncmp(buf
, "TT", 2) == 0) {
1017 /* No special preprocessing required */
1018 } else if (strncmp(buf
, "GF", 2) == 0) {
1019 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1021 if(state
->cli_state
== CLIENT_FINISHED
) {
1022 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
1025 x_fprintf(x_stdout
, "BH\n");
1028 data_blob_free(&request
);
1030 } else if (strncmp(buf
, "GK", 2) == 0 ) {
1031 DEBUG(10, ("Requested session key\n"));
1033 if(state
->cli_state
== CLIENT_FINISHED
) {
1034 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
1035 state
->session_key
);
1036 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
1040 x_fprintf(x_stdout
, "BH\n");
1043 data_blob_free(&request
);
1046 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
1047 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
1051 if (!state
->ntlmssp_state
) {
1052 nt_status
= ntlm_auth_start_ntlmssp_client(
1053 &state
->ntlmssp_state
);
1054 if (!NT_STATUS_IS_OK(nt_status
)) {
1055 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1058 ntlmssp_want_feature_list(state
->ntlmssp_state
,
1059 state
->want_feature_list
);
1060 state
->initial_message
= data_blob_null
;
1063 DEBUG(10, ("got NTLMSSP packet:\n"));
1064 dump_data(10, request
.data
, request
.length
);
1066 if (use_cached_creds
&& !opt_password
&&
1067 (state
->cli_state
== CLIENT_RESPONSE
)) {
1068 nt_status
= do_ccache_ntlm_auth(state
->initial_message
, request
,
1071 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
,
1075 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1076 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
1078 if (state
->cli_state
== CLIENT_INITIAL
) {
1079 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
1080 state
->initial_message
= reply
;
1081 state
->cli_state
= CLIENT_RESPONSE
;
1083 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
1084 data_blob_free(&reply
);
1086 TALLOC_FREE(reply_base64
);
1087 DEBUG(10, ("NTLMSSP challenge\n"));
1088 } else if (NT_STATUS_IS_OK(nt_status
)) {
1089 char *reply_base64
= base64_encode_data_blob(talloc_tos(),
1091 x_fprintf(x_stdout
, "AF %s\n", reply_base64
);
1092 TALLOC_FREE(reply_base64
);
1094 if(state
->have_session_key
)
1095 data_blob_free(&state
->session_key
);
1097 state
->session_key
= data_blob(
1098 state
->ntlmssp_state
->session_key
.data
,
1099 state
->ntlmssp_state
->session_key
.length
);
1100 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
1101 state
->have_session_key
= true;
1103 DEBUG(10, ("NTLMSSP OK!\n"));
1104 state
->cli_state
= CLIENT_FINISHED
;
1105 TALLOC_FREE(state
->ntlmssp_state
);
1107 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1108 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
1109 state
->cli_state
= CLIENT_ERROR
;
1110 TALLOC_FREE(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 *, 3);
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) {
1224 TALLOC_FREE(ntlmssp_state
);
1225 } else if (strncmp(buf
, "KK", 2) == 0) {
1228 DEBUG(1, ("SPENGO query [%s] invalid", buf
));
1229 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1233 if ( (strlen(buf
) == 2)) {
1235 /* no client data, get the negTokenInit offering
1238 offer_gss_spnego_mechs();
1242 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1244 if (strlen(buf
) <= 3) {
1245 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1246 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1250 token
= base64_decode_data_blob(buf
+ 3);
1251 len
= spnego_read_data(ctx
, token
, &request
);
1252 data_blob_free(&token
);
1255 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf
));
1256 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1260 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1262 /* Second request from Client. This is where the
1263 client offers its mechanism to use. */
1265 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
1266 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
1267 DEBUG(1, ("Client did not offer any mechanism"));
1268 x_fprintf(x_stdout
, "BH Client did not offer any "
1273 status
= NT_STATUS_UNSUCCESSFUL
;
1274 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_NTLMSSP
) == 0) {
1276 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
1277 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1278 x_fprintf(x_stdout
, "BH Client did not provide "
1283 if ( ntlmssp_state
!= NULL
) {
1284 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1285 "already got one\n"));
1286 x_fprintf(x_stdout
, "BH Client wants a new "
1287 "NTLMSSP challenge, but "
1288 "already got one\n");
1289 TALLOC_FREE(ntlmssp_state
);
1293 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_server(&ntlmssp_state
))) {
1294 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1298 DEBUG(10, ("got NTLMSSP packet:\n"));
1299 dump_data(10, request
.negTokenInit
.mechToken
.data
,
1300 request
.negTokenInit
.mechToken
.length
);
1302 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1303 response
.negTokenTarg
.supportedMech
= talloc_strdup(ctx
, OID_NTLMSSP
);
1304 response
.negTokenTarg
.mechListMIC
= data_blob_talloc(ctx
, NULL
, 0);
1306 status
= ntlmssp_update(ntlmssp_state
,
1307 request
.negTokenInit
.mechToken
,
1308 &response
.negTokenTarg
.responseToken
);
1312 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_KERBEROS5_OLD
) == 0) {
1314 TALLOC_CTX
*mem_ctx
= talloc_init("manage_gss_spnego_request");
1317 DATA_BLOB session_key
;
1318 struct PAC_LOGON_INFO
*logon_info
= NULL
;
1320 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
1321 DEBUG(1, ("Client did not provide Kerberos data\n"));
1322 x_fprintf(x_stdout
, "BH Client did not provide "
1327 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1328 response
.negTokenTarg
.supportedMech
= talloc_strdup(ctx
, OID_KERBEROS5_OLD
);
1329 response
.negTokenTarg
.mechListMIC
= data_blob_talloc(ctx
, NULL
, 0);
1330 response
.negTokenTarg
.responseToken
= data_blob_talloc(ctx
, NULL
, 0);
1332 status
= ads_verify_ticket(mem_ctx
, lp_realm(), 0,
1333 &request
.negTokenInit
.mechToken
,
1334 &principal
, &logon_info
, &ap_rep
,
1335 &session_key
, True
);
1337 /* Now in "principal" we have the name we are
1338 authenticated as. */
1340 if (NT_STATUS_IS_OK(status
)) {
1342 domain
= strchr_m(principal
, '@');
1344 if (domain
== NULL
) {
1345 DEBUG(1, ("Did not get a valid principal "
1346 "from ads_verify_ticket\n"));
1347 x_fprintf(x_stdout
, "BH Did not get a "
1348 "valid principal from "
1349 "ads_verify_ticket\n");
1354 domain
= SMB_STRDUP(domain
);
1355 user
= SMB_STRDUP(principal
);
1357 data_blob_free(&ap_rep
);
1360 TALLOC_FREE(mem_ctx
);
1366 if ( (request
.negTokenTarg
.supportedMech
== NULL
) ||
1367 ( strcmp(request
.negTokenTarg
.supportedMech
, OID_NTLMSSP
) != 0 ) ) {
1368 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1369 is the only one we support that sends this stuff */
1370 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1371 request
.negTokenTarg
.supportedMech
));
1372 x_fprintf(x_stdout
, "BH Got a negTokenTarg for "
1373 "something non-NTLMSSP\n");
1377 if (request
.negTokenTarg
.responseToken
.data
== NULL
) {
1378 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1379 x_fprintf(x_stdout
, "BH Got a negTokenTarg without a "
1380 "responseToken!\n");
1384 status
= ntlmssp_update(ntlmssp_state
,
1385 request
.negTokenTarg
.responseToken
,
1386 &response
.negTokenTarg
.responseToken
);
1388 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1389 response
.negTokenTarg
.supportedMech
= talloc_strdup(ctx
, OID_NTLMSSP
);
1390 response
.negTokenTarg
.mechListMIC
= data_blob_talloc(ctx
, NULL
, 0);
1392 if (NT_STATUS_IS_OK(status
)) {
1393 user
= SMB_STRDUP(ntlmssp_state
->user
);
1394 domain
= SMB_STRDUP(ntlmssp_state
->domain
);
1395 TALLOC_FREE(ntlmssp_state
);
1399 spnego_free_data(&request
);
1401 if (NT_STATUS_IS_OK(status
)) {
1402 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
1404 reply_argument
= talloc_asprintf(ctx
, "%s\\%s", domain
, user
);
1405 } else if (NT_STATUS_EQUAL(status
,
1406 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1407 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1409 reply_argument
= talloc_strdup(ctx
, "*");
1411 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1413 reply_argument
= talloc_strdup(ctx
, nt_errstr(status
));
1416 if (!reply_argument
) {
1417 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1418 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1425 len
= spnego_write_data(ctx
, &token
, &response
);
1426 spnego_free_data(&response
);
1429 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1430 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1434 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1436 x_fprintf(x_stdout
, "%s %s %s\n",
1437 reply_code
, reply_base64
, reply_argument
);
1439 TALLOC_FREE(reply_base64
);
1440 data_blob_free(&token
);
1445 static struct ntlmssp_state
*client_ntlmssp_state
= NULL
;
1447 static bool manage_client_ntlmssp_init(struct spnego_data spnego
)
1450 DATA_BLOB null_blob
= data_blob_null
;
1451 DATA_BLOB to_server
;
1452 char *to_server_base64
;
1453 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1454 TALLOC_CTX
*ctx
= talloc_tos();
1456 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1458 if (client_ntlmssp_state
!= NULL
) {
1459 DEBUG(1, ("Request for initial SPNEGO request where "
1460 "we already have a state\n"));
1464 if (!client_ntlmssp_state
) {
1465 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1466 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1472 if (opt_password
== NULL
) {
1474 /* Request a password from the calling process. After
1475 sending it, the calling process should retry with
1476 the negTokenInit. */
1478 DEBUG(10, ("Requesting password\n"));
1479 x_fprintf(x_stdout
, "PW\n");
1483 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1484 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1485 spnego
.negTokenInit
.reqFlags
= data_blob_null
;
1486 spnego
.negTokenInit
.reqFlagsPadding
= 0;
1487 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1489 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1490 &spnego
.negTokenInit
.mechToken
);
1492 if ( !(NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) ||
1493 NT_STATUS_IS_OK(status
)) ) {
1494 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1495 nt_errstr(status
)));
1496 TALLOC_FREE(client_ntlmssp_state
);
1500 spnego_write_data(ctx
, &to_server
, &spnego
);
1501 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1503 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1504 data_blob_free(&to_server
);
1505 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1506 TALLOC_FREE(to_server_base64
);
1510 static void manage_client_ntlmssp_targ(struct spnego_data spnego
)
1513 DATA_BLOB null_blob
= data_blob_null
;
1515 DATA_BLOB to_server
;
1516 char *to_server_base64
;
1517 TALLOC_CTX
*ctx
= talloc_tos();
1519 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1521 if (client_ntlmssp_state
== NULL
) {
1522 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1523 x_fprintf(x_stdout
, "BH Got NTLMSSP tArg without a client state\n");
1527 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1528 x_fprintf(x_stdout
, "NA\n");
1529 TALLOC_FREE(client_ntlmssp_state
);
1533 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1534 x_fprintf(x_stdout
, "AF\n");
1535 TALLOC_FREE(client_ntlmssp_state
);
1539 status
= ntlmssp_update(client_ntlmssp_state
,
1540 spnego
.negTokenTarg
.responseToken
,
1543 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1544 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1545 "ntlmssp_client_update, got: %s\n",
1546 nt_errstr(status
)));
1547 x_fprintf(x_stdout
, "BH Expected MORE_PROCESSING_REQUIRED from "
1548 "ntlmssp_client_update\n");
1549 data_blob_free(&request
);
1550 TALLOC_FREE(client_ntlmssp_state
);
1554 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1555 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1556 spnego
.negTokenTarg
.supportedMech
= (char *)OID_NTLMSSP
;
1557 spnego
.negTokenTarg
.responseToken
= request
;
1558 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1560 spnego_write_data(ctx
, &to_server
, &spnego
);
1561 data_blob_free(&request
);
1563 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1564 data_blob_free(&to_server
);
1565 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1566 TALLOC_FREE(to_server_base64
);
1572 static bool manage_client_krb5_init(struct spnego_data spnego
)
1575 DATA_BLOB tkt
, to_server
;
1576 DATA_BLOB session_key_krb5
= data_blob_null
;
1577 struct spnego_data reply
;
1581 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1583 TALLOC_CTX
*ctx
= talloc_tos();
1585 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1586 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1587 DEBUG(1, ("Did not get a principal for krb5\n"));
1591 principal
= (char *)SMB_MALLOC(
1592 spnego
.negTokenInit
.mechListMIC
.length
+1);
1594 if (principal
== NULL
) {
1595 DEBUG(1, ("Could not malloc principal\n"));
1599 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1600 spnego
.negTokenInit
.mechListMIC
.length
);
1601 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1603 retval
= cli_krb5_get_ticket(ctx
, principal
, 0,
1604 &tkt
, &session_key_krb5
,
1605 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(ctx
, principal
, 0,
1629 &tkt
, &session_key_krb5
,
1630 0, NULL
, NULL
, NULL
);
1632 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1637 data_blob_free(&session_key_krb5
);
1641 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1642 reply
.negTokenInit
.mechTypes
= my_mechs
;
1643 reply
.negTokenInit
.reqFlags
= data_blob_null
;
1644 reply
.negTokenInit
.reqFlagsPadding
= 0;
1645 reply
.negTokenInit
.mechToken
= tkt
;
1646 reply
.negTokenInit
.mechListMIC
= data_blob_null
;
1648 len
= spnego_write_data(ctx
, &to_server
, &reply
);
1649 data_blob_free(&tkt
);
1652 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1656 reply_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1657 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1659 TALLOC_FREE(reply_base64
);
1660 data_blob_free(&to_server
);
1661 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1665 static void manage_client_krb5_targ(struct spnego_data spnego
)
1667 switch (spnego
.negTokenTarg
.negResult
) {
1668 case SPNEGO_ACCEPT_INCOMPLETE
:
1669 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1670 x_fprintf(x_stdout
, "BH Got a Kerberos negTokenTarg with "
1671 "ACCEPT_INCOMPLETE\n");
1673 case SPNEGO_ACCEPT_COMPLETED
:
1674 DEBUG(10, ("Accept completed\n"));
1675 x_fprintf(x_stdout
, "AF\n");
1678 DEBUG(10, ("Rejected\n"));
1679 x_fprintf(x_stdout
, "NA\n");
1682 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1683 x_fprintf(x_stdout
, "AF\n");
1689 static void manage_gss_spnego_client_request(struct ntlm_auth_state
*state
,
1690 char *buf
, int length
)
1693 struct spnego_data spnego
;
1695 TALLOC_CTX
*ctx
= talloc_tos();
1697 if (!opt_username
|| !*opt_username
) {
1698 x_fprintf(x_stderr
, "username must be specified!\n\n");
1702 if (strlen(buf
) <= 3) {
1703 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1704 x_fprintf(x_stdout
, "BH SPNEGO query too short\n");
1708 request
= base64_decode_data_blob(buf
+3);
1710 if (strncmp(buf
, "PW ", 3) == 0) {
1712 /* We asked for a password and obviously got it :-) */
1714 opt_password
= SMB_STRNDUP((const char *)request
.data
, request
.length
);
1716 if (opt_password
== NULL
) {
1717 DEBUG(1, ("Out of memory\n"));
1718 x_fprintf(x_stdout
, "BH Out of memory\n");
1719 data_blob_free(&request
);
1723 x_fprintf(x_stdout
, "OK\n");
1724 data_blob_free(&request
);
1728 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1729 (strncmp(buf
, "AF ", 3) != 0) &&
1730 (strncmp(buf
, "NA ", 3) != 0) ) {
1731 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1732 x_fprintf(x_stdout
, "BH SPNEGO request invalid\n");
1733 data_blob_free(&request
);
1737 /* So we got a server challenge to generate a SPNEGO
1738 client-to-server request... */
1740 len
= spnego_read_data(ctx
, request
, &spnego
);
1741 data_blob_free(&request
);
1744 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1745 x_fprintf(x_stdout
, "BH Could not read SPNEGO data\n");
1749 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1751 /* The server offers a list of mechanisms */
1753 const char **mechType
= (const char **)spnego
.negTokenInit
.mechTypes
;
1755 while (*mechType
!= NULL
) {
1758 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1759 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1760 if (manage_client_krb5_init(spnego
))
1765 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1766 if (manage_client_ntlmssp_init(spnego
))
1773 DEBUG(1, ("Server offered no compatible mechanism\n"));
1774 x_fprintf(x_stdout
, "BH Server offered no compatible mechanism\n");
1778 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1780 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1781 /* On accept/reject Windows does not send the
1782 mechanism anymore. Handle that here and
1783 shut down the mechanisms. */
1785 switch (spnego
.negTokenTarg
.negResult
) {
1786 case SPNEGO_ACCEPT_COMPLETED
:
1787 x_fprintf(x_stdout
, "AF\n");
1790 x_fprintf(x_stdout
, "NA\n");
1793 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1794 "unknown negResult: %d\n",
1795 spnego
.negTokenTarg
.negResult
));
1796 x_fprintf(x_stdout
, "BH Got a negTokenTarg with"
1797 " no mech and an unknown "
1801 TALLOC_FREE(client_ntlmssp_state
);
1805 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1806 OID_NTLMSSP
) == 0) {
1807 manage_client_ntlmssp_targ(spnego
);
1812 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1813 OID_KERBEROS5_OLD
) == 0) {
1814 manage_client_krb5_targ(spnego
);
1821 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1822 x_fprintf(x_stdout
, "BH Got an SPNEGO token I could not handle\n");
1826 spnego_free_data(&spnego
);
1830 static void manage_ntlm_server_1_request(struct ntlm_auth_state
*state
,
1831 char *buf
, int length
)
1833 char *request
, *parameter
;
1834 static DATA_BLOB challenge
;
1835 static DATA_BLOB lm_response
;
1836 static DATA_BLOB nt_response
;
1837 static char *full_username
;
1838 static char *username
;
1839 static char *domain
;
1840 static char *plaintext_password
;
1841 static bool ntlm_server_1_user_session_key
;
1842 static bool ntlm_server_1_lm_session_key
;
1844 if (strequal(buf
, ".")) {
1845 if (!full_username
&& !username
) {
1846 x_fprintf(x_stdout
, "Error: No username supplied!\n");
1847 } else if (plaintext_password
) {
1848 /* handle this request as plaintext */
1849 if (!full_username
) {
1850 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
1851 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
1855 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
1856 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1858 x_fprintf(x_stdout
, "Authenticated: No\n");
1860 } else if (!lm_response
.data
&& !nt_response
.data
) {
1861 x_fprintf(x_stdout
, "Error: No password supplied!\n");
1862 } else if (!challenge
.data
) {
1863 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
1865 char *error_string
= NULL
;
1867 uchar user_session_key
[16];
1870 if (full_username
&& !username
) {
1872 fstring fstr_domain
;
1874 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
1875 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1876 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
1878 SAFE_FREE(username
);
1880 username
= smb_xstrdup(fstr_user
);
1881 domain
= smb_xstrdup(fstr_domain
);
1885 domain
= smb_xstrdup(get_winbind_domain());
1888 if (ntlm_server_1_lm_session_key
)
1889 flags
|= WBFLAG_PAM_LMKEY
;
1891 if (ntlm_server_1_user_session_key
)
1892 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
1894 if (!NT_STATUS_IS_OK(
1895 contact_winbind_auth_crap(username
,
1907 x_fprintf(x_stdout
, "Authenticated: No\n");
1908 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
1910 static char zeros
[16];
1912 char *hex_user_session_key
;
1914 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1916 if (ntlm_server_1_lm_session_key
1917 && (memcmp(zeros
, lm_key
,
1918 sizeof(lm_key
)) != 0)) {
1919 hex_lm_key
= hex_encode_talloc(NULL
,
1920 (const unsigned char *)lm_key
,
1922 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
1923 TALLOC_FREE(hex_lm_key
);
1926 if (ntlm_server_1_user_session_key
1927 && (memcmp(zeros
, user_session_key
,
1928 sizeof(user_session_key
)) != 0)) {
1929 hex_user_session_key
= hex_encode_talloc(NULL
,
1930 (const unsigned char *)user_session_key
,
1931 sizeof(user_session_key
));
1932 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
1933 TALLOC_FREE(hex_user_session_key
);
1936 SAFE_FREE(error_string
);
1938 /* clear out the state */
1939 challenge
= data_blob_null
;
1940 nt_response
= data_blob_null
;
1941 lm_response
= data_blob_null
;
1942 SAFE_FREE(full_username
);
1943 SAFE_FREE(username
);
1945 SAFE_FREE(plaintext_password
);
1946 ntlm_server_1_user_session_key
= False
;
1947 ntlm_server_1_lm_session_key
= False
;
1948 x_fprintf(x_stdout
, ".\n");
1955 /* Indicates a base64 encoded structure */
1956 parameter
= strstr_m(request
, ":: ");
1958 parameter
= strstr_m(request
, ": ");
1961 DEBUG(0, ("Parameter not found!\n"));
1962 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
1979 base64_decode_inplace(parameter
);
1982 if (strequal(request
, "LANMAN-Challenge")) {
1983 challenge
= strhex_to_data_blob(NULL
, parameter
);
1984 if (challenge
.length
!= 8) {
1985 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1987 (int)challenge
.length
);
1988 challenge
= data_blob_null
;
1990 } else if (strequal(request
, "NT-Response")) {
1991 nt_response
= strhex_to_data_blob(NULL
, parameter
);
1992 if (nt_response
.length
< 24) {
1993 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1995 (int)nt_response
.length
);
1996 nt_response
= data_blob_null
;
1998 } else if (strequal(request
, "LANMAN-Response")) {
1999 lm_response
= strhex_to_data_blob(NULL
, parameter
);
2000 if (lm_response
.length
!= 24) {
2001 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2003 (int)lm_response
.length
);
2004 lm_response
= data_blob_null
;
2006 } else if (strequal(request
, "Password")) {
2007 plaintext_password
= smb_xstrdup(parameter
);
2008 } else if (strequal(request
, "NT-Domain")) {
2009 domain
= smb_xstrdup(parameter
);
2010 } else if (strequal(request
, "Username")) {
2011 username
= smb_xstrdup(parameter
);
2012 } else if (strequal(request
, "Full-Username")) {
2013 full_username
= smb_xstrdup(parameter
);
2014 } else if (strequal(request
, "Request-User-Session-Key")) {
2015 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
2016 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
2017 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
2019 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2023 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
2024 char *buf
, int length
)
2026 char *request
, *parameter
;
2027 static DATA_BLOB new_nt_pswd
;
2028 static DATA_BLOB old_nt_hash_enc
;
2029 static DATA_BLOB new_lm_pswd
;
2030 static DATA_BLOB old_lm_hash_enc
;
2031 static char *full_username
= NULL
;
2032 static char *username
= NULL
;
2033 static char *domain
= NULL
;
2034 static char *newpswd
= NULL
;
2035 static char *oldpswd
= NULL
;
2037 if (strequal(buf
, ".")) {
2038 if(newpswd
&& oldpswd
) {
2039 uchar old_nt_hash
[16];
2040 uchar old_lm_hash
[16];
2041 uchar new_nt_hash
[16];
2042 uchar new_lm_hash
[16];
2044 new_nt_pswd
= data_blob(NULL
, 516);
2045 old_nt_hash_enc
= data_blob(NULL
, 16);
2047 /* Calculate the MD4 hash (NT compatible) of the
2049 E_md4hash(oldpswd
, old_nt_hash
);
2050 E_md4hash(newpswd
, new_nt_hash
);
2052 /* E_deshash returns false for 'long'
2053 passwords (> 14 DOS chars).
2055 Therefore, don't send a buffer
2056 encrypted with the truncated hash
2057 (it could allow an even easier
2058 attack on the password)
2060 Likewise, obey the admin's restriction
2063 if (lp_client_lanman_auth() &&
2064 E_deshash(newpswd
, new_lm_hash
) &&
2065 E_deshash(oldpswd
, old_lm_hash
)) {
2066 new_lm_pswd
= data_blob(NULL
, 516);
2067 old_lm_hash_enc
= data_blob(NULL
, 16);
2068 encode_pw_buffer(new_lm_pswd
.data
, newpswd
,
2071 arcfour_crypt(new_lm_pswd
.data
, old_nt_hash
, 516);
2072 E_old_pw_hash(new_nt_hash
, old_lm_hash
,
2073 old_lm_hash_enc
.data
);
2075 new_lm_pswd
.data
= NULL
;
2076 new_lm_pswd
.length
= 0;
2077 old_lm_hash_enc
.data
= NULL
;
2078 old_lm_hash_enc
.length
= 0;
2081 encode_pw_buffer(new_nt_pswd
.data
, newpswd
,
2084 arcfour_crypt(new_nt_pswd
.data
, old_nt_hash
, 516);
2085 E_old_pw_hash(new_nt_hash
, old_nt_hash
,
2086 old_nt_hash_enc
.data
);
2089 if (!full_username
&& !username
) {
2090 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2091 } else if ((!new_nt_pswd
.data
|| !old_nt_hash_enc
.data
) &&
2092 (!new_lm_pswd
.data
|| old_lm_hash_enc
.data
) ) {
2093 x_fprintf(x_stdout
, "Error: No NT or LM password "
2094 "blobs supplied!\n");
2096 char *error_string
= NULL
;
2098 if (full_username
&& !username
) {
2100 fstring fstr_domain
;
2102 if (!parse_ntlm_auth_domain_user(full_username
,
2105 /* username might be 'tainted', don't
2106 * print into our new-line
2107 * deleimianted stream */
2108 x_fprintf(x_stdout
, "Error: Could not "
2109 "parse into domain and "
2111 SAFE_FREE(username
);
2112 username
= smb_xstrdup(full_username
);
2114 SAFE_FREE(username
);
2116 username
= smb_xstrdup(fstr_user
);
2117 domain
= smb_xstrdup(fstr_domain
);
2122 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2129 x_fprintf(x_stdout
, "Password-Change: No\n");
2130 x_fprintf(x_stdout
, "Password-Change-Error: "
2131 "%s\n.\n", error_string
);
2133 x_fprintf(x_stdout
, "Password-Change: Yes\n");
2136 SAFE_FREE(error_string
);
2138 /* clear out the state */
2139 new_nt_pswd
= data_blob_null
;
2140 old_nt_hash_enc
= data_blob_null
;
2141 new_lm_pswd
= data_blob_null
;
2142 old_nt_hash_enc
= data_blob_null
;
2143 SAFE_FREE(full_username
);
2144 SAFE_FREE(username
);
2148 x_fprintf(x_stdout
, ".\n");
2155 /* Indicates a base64 encoded structure */
2156 parameter
= strstr_m(request
, ":: ");
2158 parameter
= strstr_m(request
, ": ");
2161 DEBUG(0, ("Parameter not found!\n"));
2162 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2178 base64_decode_inplace(parameter
);
2181 if (strequal(request
, "new-nt-password-blob")) {
2182 new_nt_pswd
= strhex_to_data_blob(NULL
, parameter
);
2183 if (new_nt_pswd
.length
!= 516) {
2184 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2185 "(got %d bytes, expected 516)\n.\n",
2187 (int)new_nt_pswd
.length
);
2188 new_nt_pswd
= data_blob_null
;
2190 } else if (strequal(request
, "old-nt-hash-blob")) {
2191 old_nt_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2192 if (old_nt_hash_enc
.length
!= 16) {
2193 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2194 "(got %d bytes, expected 16)\n.\n",
2196 (int)old_nt_hash_enc
.length
);
2197 old_nt_hash_enc
= data_blob_null
;
2199 } else if (strequal(request
, "new-lm-password-blob")) {
2200 new_lm_pswd
= strhex_to_data_blob(NULL
, parameter
);
2201 if (new_lm_pswd
.length
!= 516) {
2202 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2203 "(got %d bytes, expected 516)\n.\n",
2205 (int)new_lm_pswd
.length
);
2206 new_lm_pswd
= data_blob_null
;
2209 else if (strequal(request
, "old-lm-hash-blob")) {
2210 old_lm_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2211 if (old_lm_hash_enc
.length
!= 16)
2213 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2214 "(got %d bytes, expected 16)\n.\n",
2216 (int)old_lm_hash_enc
.length
);
2217 old_lm_hash_enc
= data_blob_null
;
2219 } else if (strequal(request
, "nt-domain")) {
2220 domain
= smb_xstrdup(parameter
);
2221 } else if(strequal(request
, "username")) {
2222 username
= smb_xstrdup(parameter
);
2223 } else if(strequal(request
, "full-username")) {
2224 username
= smb_xstrdup(parameter
);
2225 } else if(strequal(request
, "new-password")) {
2226 newpswd
= smb_xstrdup(parameter
);
2227 } else if (strequal(request
, "old-password")) {
2228 oldpswd
= smb_xstrdup(parameter
);
2230 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2234 static void manage_squid_request(struct ntlm_auth_state
*state
,
2235 stdio_helper_function fn
)
2238 char tmp
[INITIAL_BUFFER_SIZE
+1];
2239 int length
, buf_size
= 0;
2242 buf
= talloc_strdup(state
->mem_ctx
, "");
2244 DEBUG(0, ("Failed to allocate input buffer.\n"));
2245 x_fprintf(x_stderr
, "ERR\n");
2251 /* this is not a typo - x_fgets doesn't work too well under
2253 if (fgets(tmp
, sizeof(tmp
)-1, stdin
) == NULL
) {
2254 if (ferror(stdin
)) {
2255 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2256 "(%s)\n", ferror(stdin
),
2257 strerror(ferror(stdin
))));
2264 buf
= talloc_strdup_append_buffer(buf
, tmp
);
2265 buf_size
+= INITIAL_BUFFER_SIZE
;
2267 if (buf_size
> MAX_BUFFER_SIZE
) {
2268 DEBUG(2, ("Oversized message\n"));
2269 x_fprintf(x_stderr
, "ERR\n");
2274 c
= strchr(buf
, '\n');
2275 } while (c
== NULL
);
2280 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
2282 if (buf
[0] == '\0') {
2283 DEBUG(2, ("Invalid Request\n"));
2284 x_fprintf(x_stderr
, "ERR\n");
2289 fn(state
, buf
, length
);
2294 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
2295 TALLOC_CTX
*mem_ctx
;
2296 struct ntlm_auth_state
*state
;
2298 /* initialize FDescs */
2299 x_setbuf(x_stdout
, NULL
);
2300 x_setbuf(x_stderr
, NULL
);
2302 mem_ctx
= talloc_init("ntlm_auth");
2304 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2305 x_fprintf(x_stderr
, "ERR\n");
2309 state
= talloc_zero(mem_ctx
, struct ntlm_auth_state
);
2311 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2312 x_fprintf(x_stderr
, "ERR\n");
2316 state
->mem_ctx
= mem_ctx
;
2317 state
->helper_mode
= stdio_mode
;
2320 manage_squid_request(state
, fn
);
2325 /* Authenticate a user with a challenge/response */
2327 static bool check_auth_crap(void)
2332 char user_session_key
[16];
2334 char *hex_user_session_key
;
2336 static uint8 zeros
[16];
2338 x_setbuf(x_stdout
, NULL
);
2341 flags
|= WBFLAG_PAM_LMKEY
;
2343 if (request_user_session_key
)
2344 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2346 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
2348 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
2354 (unsigned char *)lm_key
,
2355 (unsigned char *)user_session_key
,
2356 &error_string
, NULL
);
2358 if (!NT_STATUS_IS_OK(nt_status
)) {
2359 x_fprintf(x_stdout
, "%s (0x%x)\n",
2361 NT_STATUS_V(nt_status
));
2362 SAFE_FREE(error_string
);
2367 && (memcmp(zeros
, lm_key
,
2368 sizeof(lm_key
)) != 0)) {
2369 hex_lm_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key
,
2371 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
2372 TALLOC_FREE(hex_lm_key
);
2374 if (request_user_session_key
2375 && (memcmp(zeros
, user_session_key
,
2376 sizeof(user_session_key
)) != 0)) {
2377 hex_user_session_key
= hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key
,
2378 sizeof(user_session_key
));
2379 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
2380 TALLOC_FREE(hex_user_session_key
);
2389 OPT_USERNAME
= 1000,
2398 OPT_USER_SESSION_KEY
,
2400 OPT_REQUIRE_MEMBERSHIP
,
2401 OPT_USE_CACHED_CREDS
,
2402 OPT_PAM_WINBIND_CONF
2405 int main(int argc
, const char **argv
)
2407 TALLOC_CTX
*frame
= talloc_stackframe();
2409 static const char *helper_protocol
;
2410 static int diagnostics
;
2412 static const char *hex_challenge
;
2413 static const char *hex_lm_response
;
2414 static const char *hex_nt_response
;
2418 /* NOTE: DO NOT change this interface without considering the implications!
2419 This is an external interface, which other programs will use to interact
2423 /* We do not use single-letter command abbreviations, because they harm future
2424 interface stability. */
2426 struct poptOption long_options
[] = {
2428 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
2429 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
2430 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
2431 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
2432 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
2433 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
2434 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2435 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
2436 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retrieve LM session key"},
2437 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retrieve User (NT) session key"},
2438 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "Use cached credentials if no password is given"},
2439 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
,
2441 "Perform diagnostics on the authentication chain"},
2442 { "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" },
2443 { "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" },
2444 POPT_COMMON_CONFIGFILE
2449 /* Samba client initialisation */
2456 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
2458 /* Parse command line options */
2461 poptPrintHelp(pc
, stderr
, 0);
2465 while((opt
= poptGetNextOpt(pc
)) != -1) {
2466 /* Get generic config options like --configfile */
2469 poptFreeContext(pc
);
2471 if (!lp_load(get_dyn_CONFIGFILE(), True
, False
, False
, True
)) {
2472 d_fprintf(stderr
, "ntlm_auth: error opening config file %s. Error was %s\n",
2473 get_dyn_CONFIGFILE(), strerror(errno
));
2477 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2478 POPT_CONTEXT_KEEP_FIRST
);
2480 while((opt
= poptGetNextOpt(pc
)) != -1) {
2483 opt_challenge
= strhex_to_data_blob(NULL
, hex_challenge
);
2484 if (opt_challenge
.length
!= 8) {
2485 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2487 (int)opt_challenge
.length
);
2492 opt_lm_response
= strhex_to_data_blob(NULL
, hex_lm_response
);
2493 if (opt_lm_response
.length
!= 24) {
2494 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2496 (int)opt_lm_response
.length
);
2502 opt_nt_response
= strhex_to_data_blob(NULL
, hex_nt_response
);
2503 if (opt_nt_response
.length
< 24) {
2504 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2506 (int)opt_nt_response
.length
);
2511 case OPT_REQUIRE_MEMBERSHIP
:
2512 if (StrnCaseCmp("S-", require_membership_of
, 2) == 0) {
2513 require_membership_of_sid
= require_membership_of
;
2520 char *domain
= SMB_STRDUP(opt_username
);
2521 char *p
= strchr_m(domain
, *lp_winbind_separator());
2525 if (opt_domain
&& !strequal(opt_domain
, domain
)) {
2526 x_fprintf(x_stderr
, "Domain specified in username (%s) "
2527 "doesn't match specified domain (%s)!\n\n",
2528 domain
, opt_domain
);
2529 poptPrintHelp(pc
, stderr
, 0);
2532 opt_domain
= domain
;
2538 /* Note: if opt_domain is "" then send no domain */
2539 if (opt_domain
== NULL
) {
2540 opt_domain
= get_winbind_domain();
2543 if (opt_workstation
== NULL
) {
2544 opt_workstation
= "";
2547 if (helper_protocol
) {
2549 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2550 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
2551 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
2555 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
2557 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2558 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
2564 if (!opt_username
|| !*opt_username
) {
2565 x_fprintf(x_stderr
, "username must be specified!\n\n");
2566 poptPrintHelp(pc
, stderr
, 0);
2570 if (opt_challenge
.length
) {
2571 if (!check_auth_crap()) {
2577 if (!opt_password
) {
2578 opt_password
= getpass("password: ");
2582 if (!diagnose_ntlm_auth()) {
2588 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
2589 if (!check_plaintext_auth(user
, opt_password
, True
)) {
2596 poptFreeContext(pc
);