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"
31 #define DBGC_CLASS DBGC_WINBIND
33 #define INITIAL_BUFFER_SIZE 300
34 #define MAX_BUFFER_SIZE 630000
36 enum stdio_helper_mode
{
44 NTLM_CHANGE_PASSWORD_1
,
48 enum ntlm_auth_cli_state
{
55 enum ntlm_auth_svr_state
{
62 struct ntlm_auth_state
{
64 enum stdio_helper_mode helper_mode
;
65 enum ntlm_auth_cli_state cli_state
;
66 enum ntlm_auth_svr_state svr_state
;
67 struct ntlmssp_state
*ntlmssp_state
;
69 char *want_feature_list
;
70 bool have_session_key
;
71 DATA_BLOB session_key
;
72 DATA_BLOB initial_message
;
75 typedef void (*stdio_helper_function
)(struct ntlm_auth_state
*state
, char *buf
,
78 static void manage_squid_basic_request (struct ntlm_auth_state
*state
,
79 char *buf
, int length
);
81 static void manage_squid_ntlmssp_request (struct ntlm_auth_state
*state
,
82 char *buf
, int length
);
84 static void manage_client_ntlmssp_request (struct ntlm_auth_state
*state
,
85 char *buf
, int length
);
87 static void manage_gss_spnego_request (struct ntlm_auth_state
*state
,
88 char *buf
, int length
);
90 static void manage_gss_spnego_client_request (struct ntlm_auth_state
*state
,
91 char *buf
, int length
);
93 static void manage_ntlm_server_1_request (struct ntlm_auth_state
*state
,
94 char *buf
, int length
);
96 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
97 char *buf
, int length
);
100 enum stdio_helper_mode mode
;
102 stdio_helper_function fn
;
103 } stdio_helper_protocols
[] = {
104 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
105 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
106 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request
},
107 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_client_ntlmssp_request
},
108 { GSS_SPNEGO
, "gss-spnego", manage_gss_spnego_request
},
109 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gss_spnego_client_request
},
110 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
111 { NTLM_CHANGE_PASSWORD_1
, "ntlm-change-password-1", manage_ntlm_change_password_1_request
},
112 { NUM_HELPER_MODES
, NULL
, NULL
}
115 const char *opt_username
;
116 const char *opt_domain
;
117 const char *opt_workstation
;
118 const char *opt_password
;
119 static DATA_BLOB opt_challenge
;
120 static DATA_BLOB opt_lm_response
;
121 static DATA_BLOB opt_nt_response
;
122 static int request_lm_key
;
123 static int request_user_session_key
;
124 static int use_cached_creds
;
126 static const char *require_membership_of
;
127 static const char *require_membership_of_sid
;
129 static char winbind_separator(void)
131 struct winbindd_response response
;
138 ZERO_STRUCT(response
);
140 /* Send off request */
142 if (winbindd_request_response(WINBINDD_INFO
, NULL
, &response
) !=
143 NSS_STATUS_SUCCESS
) {
144 d_printf("could not obtain winbind separator!\n");
145 return *lp_winbind_separator();
148 sep
= response
.data
.info
.winbind_separator
;
152 d_printf("winbind separator was NULL!\n");
153 return *lp_winbind_separator();
159 const char *get_winbind_domain(void)
161 struct winbindd_response response
;
163 static fstring winbind_domain
;
164 if (*winbind_domain
) {
165 return winbind_domain
;
168 ZERO_STRUCT(response
);
170 /* Send off request */
172 if (winbindd_request_response(WINBINDD_DOMAIN_NAME
, NULL
, &response
) !=
173 NSS_STATUS_SUCCESS
) {
174 DEBUG(0, ("could not obtain winbind domain name!\n"));
175 return lp_workgroup();
178 fstrcpy(winbind_domain
, response
.data
.domain_name
);
180 return winbind_domain
;
184 const char *get_winbind_netbios_name(void)
186 struct winbindd_response response
;
188 static fstring winbind_netbios_name
;
190 if (*winbind_netbios_name
) {
191 return winbind_netbios_name
;
194 ZERO_STRUCT(response
);
196 /* Send off request */
198 if (winbindd_request_response(WINBINDD_NETBIOS_NAME
, NULL
, &response
) !=
199 NSS_STATUS_SUCCESS
) {
200 DEBUG(0, ("could not obtain winbind netbios name!\n"));
201 return global_myname();
204 fstrcpy(winbind_netbios_name
, response
.data
.netbios_name
);
206 return winbind_netbios_name
;
210 DATA_BLOB
get_challenge(void)
212 static DATA_BLOB chal
;
213 if (opt_challenge
.length
)
214 return opt_challenge
;
216 chal
= data_blob(NULL
, 8);
218 generate_random_buffer(chal
.data
, chal
.length
);
222 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
223 form DOMAIN/user into a domain and a user */
225 static bool parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
229 char *p
= strchr(domuser
,winbind_separator());
236 fstrcpy(domain
, domuser
);
237 domain
[PTR_DIFF(p
, domuser
)] = 0;
243 static bool get_require_membership_sid(void) {
244 struct winbindd_request request
;
245 struct winbindd_response response
;
247 if (!require_membership_of
) {
251 if (require_membership_of_sid
) {
255 /* Otherwise, ask winbindd for the name->sid request */
257 ZERO_STRUCT(request
);
258 ZERO_STRUCT(response
);
260 if (!parse_ntlm_auth_domain_user(require_membership_of
,
261 request
.data
.name
.dom_name
,
262 request
.data
.name
.name
)) {
263 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
264 require_membership_of
));
268 if (winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
269 NSS_STATUS_SUCCESS
) {
270 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
271 require_membership_of
));
275 require_membership_of_sid
= SMB_STRDUP(response
.data
.sid
.sid
);
277 if (require_membership_of_sid
)
282 /* Authenticate a user with a plaintext password */
284 static bool check_plaintext_auth(const char *user
, const char *pass
,
285 bool stdout_diagnostics
)
287 struct winbindd_request request
;
288 struct winbindd_response response
;
291 if (!get_require_membership_sid()) {
295 /* Send off request */
297 ZERO_STRUCT(request
);
298 ZERO_STRUCT(response
);
300 fstrcpy(request
.data
.auth
.user
, user
);
301 fstrcpy(request
.data
.auth
.pass
, pass
);
302 if (require_membership_of_sid
) {
303 strlcpy(request
.data
.auth
.require_membership_of_sid
,
304 require_membership_of_sid
,
305 sizeof(request
.data
.auth
.require_membership_of_sid
));
308 result
= winbindd_request_response(WINBINDD_PAM_AUTH
, &request
, &response
);
310 /* Display response */
312 if (stdout_diagnostics
) {
313 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
314 d_printf("Reading winbind reply failed! (0x01)\n");
317 d_printf("%s: %s (0x%x)\n",
318 response
.data
.auth
.nt_status_string
,
319 response
.data
.auth
.error_string
,
320 response
.data
.auth
.nt_status
);
322 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
323 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
326 DEBUG(3, ("%s: %s (0x%x)\n",
327 response
.data
.auth
.nt_status_string
,
328 response
.data
.auth
.error_string
,
329 response
.data
.auth
.nt_status
));
332 return (result
== NSS_STATUS_SUCCESS
);
335 /* authenticate a user with an encrypted username/password */
337 NTSTATUS
contact_winbind_auth_crap(const char *username
,
339 const char *workstation
,
340 const DATA_BLOB
*challenge
,
341 const DATA_BLOB
*lm_response
,
342 const DATA_BLOB
*nt_response
,
345 uint8 user_session_key
[16],
351 struct winbindd_request request
;
352 struct winbindd_response response
;
354 if (!get_require_membership_sid()) {
355 return NT_STATUS_INVALID_PARAMETER
;
358 ZERO_STRUCT(request
);
359 ZERO_STRUCT(response
);
361 request
.flags
= flags
;
363 request
.data
.auth_crap
.logon_parameters
= MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
| MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
;
365 if (require_membership_of_sid
)
366 fstrcpy(request
.data
.auth_crap
.require_membership_of_sid
, require_membership_of_sid
);
368 fstrcpy(request
.data
.auth_crap
.user
, username
);
369 fstrcpy(request
.data
.auth_crap
.domain
, domain
);
371 fstrcpy(request
.data
.auth_crap
.workstation
,
374 memcpy(request
.data
.auth_crap
.chal
, challenge
->data
, MIN(challenge
->length
, 8));
376 if (lm_response
&& lm_response
->length
) {
377 memcpy(request
.data
.auth_crap
.lm_resp
,
379 MIN(lm_response
->length
, sizeof(request
.data
.auth_crap
.lm_resp
)));
380 request
.data
.auth_crap
.lm_resp_len
= lm_response
->length
;
383 if (nt_response
&& nt_response
->length
) {
384 if (nt_response
->length
> sizeof(request
.data
.auth_crap
.nt_resp
)) {
385 request
.flags
= request
.flags
| WBFLAG_BIG_NTLMV2_BLOB
;
386 request
.extra_len
= nt_response
->length
;
387 request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, request
.extra_len
);
388 if (request
.extra_data
.data
== NULL
) {
389 return NT_STATUS_NO_MEMORY
;
391 memcpy(request
.extra_data
.data
, nt_response
->data
,
392 nt_response
->length
);
395 memcpy(request
.data
.auth_crap
.nt_resp
,
396 nt_response
->data
, nt_response
->length
);
398 request
.data
.auth_crap
.nt_resp_len
= nt_response
->length
;
401 result
= winbindd_request_response(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
402 SAFE_FREE(request
.extra_data
.data
);
404 /* Display response */
406 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
407 nt_status
= NT_STATUS_UNSUCCESSFUL
;
409 *error_string
= smb_xstrdup("Reading winbind reply failed!");
410 winbindd_free_response(&response
);
414 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
415 if (!NT_STATUS_IS_OK(nt_status
)) {
417 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
418 winbindd_free_response(&response
);
422 if ((flags
& WBFLAG_PAM_LMKEY
) && lm_key
) {
423 memcpy(lm_key
, response
.data
.auth
.first_8_lm_hash
,
424 sizeof(response
.data
.auth
.first_8_lm_hash
));
426 if ((flags
& WBFLAG_PAM_USER_SESSION_KEY
) && user_session_key
) {
427 memcpy(user_session_key
, response
.data
.auth
.user_session_key
,
428 sizeof(response
.data
.auth
.user_session_key
));
431 if (flags
& WBFLAG_PAM_UNIX_NAME
) {
432 *unix_name
= SMB_STRDUP(response
.data
.auth
.unix_username
);
434 winbindd_free_response(&response
);
435 return NT_STATUS_NO_MEMORY
;
439 winbindd_free_response(&response
);
443 /* contact server to change user password using auth crap */
444 static NTSTATUS
contact_winbind_change_pswd_auth_crap(const char *username
,
446 const DATA_BLOB new_nt_pswd
,
447 const DATA_BLOB old_nt_hash_enc
,
448 const DATA_BLOB new_lm_pswd
,
449 const DATA_BLOB old_lm_hash_enc
,
454 struct winbindd_request request
;
455 struct winbindd_response response
;
457 if (!get_require_membership_sid())
460 *error_string
= smb_xstrdup("Can't get membership sid.");
461 return NT_STATUS_INVALID_PARAMETER
;
464 ZERO_STRUCT(request
);
465 ZERO_STRUCT(response
);
468 fstrcpy(request
.data
.chng_pswd_auth_crap
.user
, username
);
470 fstrcpy(request
.data
.chng_pswd_auth_crap
.domain
,domain
);
472 if(new_nt_pswd
.length
)
474 memcpy(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
, new_nt_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
));
475 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
= new_nt_pswd
.length
;
478 if(old_nt_hash_enc
.length
)
480 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
));
481 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
= old_nt_hash_enc
.length
;
484 if(new_lm_pswd
.length
)
486 memcpy(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
, new_lm_pswd
.data
, sizeof(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
));
487 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
= new_lm_pswd
.length
;
490 if(old_lm_hash_enc
.length
)
492 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
));
493 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
= old_lm_hash_enc
.length
;
496 result
= winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, &request
, &response
);
498 /* Display response */
500 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0))
502 nt_status
= NT_STATUS_UNSUCCESSFUL
;
504 *error_string
= smb_xstrdup("Reading winbind reply failed!");
505 winbindd_free_response(&response
);
509 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
510 if (!NT_STATUS_IS_OK(nt_status
))
513 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
514 winbindd_free_response(&response
);
518 winbindd_free_response(&response
);
523 static NTSTATUS
winbind_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
525 static const char zeros
[16] = { 0, };
527 char *error_string
= NULL
;
529 uint8 user_sess_key
[16];
530 char *unix_name
= NULL
;
532 nt_status
= contact_winbind_auth_crap(ntlmssp_state
->user
, ntlmssp_state
->domain
,
533 ntlmssp_state
->workstation
,
534 &ntlmssp_state
->chal
,
535 &ntlmssp_state
->lm_resp
,
536 &ntlmssp_state
->nt_resp
,
537 WBFLAG_PAM_LMKEY
| WBFLAG_PAM_USER_SESSION_KEY
| WBFLAG_PAM_UNIX_NAME
,
538 lm_key
, user_sess_key
,
539 &error_string
, &unix_name
);
541 if (NT_STATUS_IS_OK(nt_status
)) {
542 if (memcmp(lm_key
, zeros
, 8) != 0) {
543 *lm_session_key
= data_blob(NULL
, 16);
544 memcpy(lm_session_key
->data
, lm_key
, 8);
545 memset(lm_session_key
->data
+8, '\0', 8);
548 if (memcmp(user_sess_key
, zeros
, 16) != 0) {
549 *user_session_key
= data_blob(user_sess_key
, 16);
551 ntlmssp_state
->auth_context
= talloc_strdup(ntlmssp_state
->mem_ctx
, unix_name
);
553 DEBUG(NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
) ? 0 : 3,
554 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
555 ntlmssp_state
->domain
, ntlmssp_state
->user
,
556 ntlmssp_state
->workstation
,
557 error_string
? error_string
: "unknown error (NULL)"));
558 ntlmssp_state
->auth_context
= NULL
;
561 SAFE_FREE(error_string
);
562 SAFE_FREE(unix_name
);
566 static NTSTATUS
local_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
569 uint8 lm_pw
[16], nt_pw
[16];
571 nt_lm_owf_gen (opt_password
, nt_pw
, lm_pw
);
573 nt_status
= ntlm_password_check(ntlmssp_state
->mem_ctx
,
574 &ntlmssp_state
->chal
,
575 &ntlmssp_state
->lm_resp
,
576 &ntlmssp_state
->nt_resp
,
580 ntlmssp_state
->domain
,
581 lm_pw
, nt_pw
, user_session_key
, lm_session_key
);
583 if (NT_STATUS_IS_OK(nt_status
)) {
584 ntlmssp_state
->auth_context
= talloc_asprintf(ntlmssp_state
->mem_ctx
,
585 "%s%c%s", ntlmssp_state
->domain
,
586 *lp_winbind_separator(),
587 ntlmssp_state
->user
);
589 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
590 ntlmssp_state
->domain
, ntlmssp_state
->user
, ntlmssp_state
->workstation
,
591 nt_errstr(nt_status
)));
592 ntlmssp_state
->auth_context
= NULL
;
597 static NTSTATUS
ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE
**client_ntlmssp_state
)
600 if ( (opt_username
== NULL
) || (opt_domain
== NULL
) ) {
601 status
= NT_STATUS_UNSUCCESSFUL
;
602 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
603 return NT_STATUS_INVALID_PARAMETER
;
606 status
= ntlmssp_client_start(client_ntlmssp_state
);
608 if (!NT_STATUS_IS_OK(status
)) {
609 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
611 ntlmssp_end(client_ntlmssp_state
);
615 status
= ntlmssp_set_username(*client_ntlmssp_state
, opt_username
);
617 if (!NT_STATUS_IS_OK(status
)) {
618 DEBUG(1, ("Could not set username: %s\n",
620 ntlmssp_end(client_ntlmssp_state
);
624 status
= ntlmssp_set_domain(*client_ntlmssp_state
, opt_domain
);
626 if (!NT_STATUS_IS_OK(status
)) {
627 DEBUG(1, ("Could not set domain: %s\n",
629 ntlmssp_end(client_ntlmssp_state
);
634 status
= ntlmssp_set_password(*client_ntlmssp_state
, opt_password
);
636 if (!NT_STATUS_IS_OK(status
)) {
637 DEBUG(1, ("Could not set password: %s\n",
639 ntlmssp_end(client_ntlmssp_state
);
647 static NTSTATUS
ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE
**ntlmssp_state
)
649 NTSTATUS status
= ntlmssp_server_start(ntlmssp_state
);
651 if (!NT_STATUS_IS_OK(status
)) {
652 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
657 /* Have we been given a local password, or should we ask winbind? */
659 (*ntlmssp_state
)->check_password
= local_pw_check
;
660 (*ntlmssp_state
)->get_domain
= lp_workgroup
;
661 (*ntlmssp_state
)->get_global_myname
= global_myname
;
663 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
664 (*ntlmssp_state
)->get_domain
= get_winbind_domain
;
665 (*ntlmssp_state
)->get_global_myname
= get_winbind_netbios_name
;
670 /*******************************************************************
671 Used by firefox to drive NTLM auth to IIS servers.
672 *******************************************************************/
674 static NTSTATUS
do_ccache_ntlm_auth(DATA_BLOB initial_msg
, DATA_BLOB challenge_msg
,
677 struct winbindd_request wb_request
;
678 struct winbindd_response wb_response
;
681 /* get winbindd to do the ntlmssp step on our behalf */
682 ZERO_STRUCT(wb_request
);
683 ZERO_STRUCT(wb_response
);
685 fstr_sprintf(wb_request
.data
.ccache_ntlm_auth
.user
,
686 "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
687 wb_request
.data
.ccache_ntlm_auth
.uid
= geteuid();
688 wb_request
.data
.ccache_ntlm_auth
.initial_blob_len
= initial_msg
.length
;
689 wb_request
.data
.ccache_ntlm_auth
.challenge_blob_len
= challenge_msg
.length
;
690 wb_request
.extra_len
= initial_msg
.length
+ challenge_msg
.length
;
692 if (wb_request
.extra_len
> 0) {
693 wb_request
.extra_data
.data
= SMB_MALLOC_ARRAY(char, wb_request
.extra_len
);
694 if (wb_request
.extra_data
.data
== NULL
) {
695 return NT_STATUS_NO_MEMORY
;
698 memcpy(wb_request
.extra_data
.data
, initial_msg
.data
, initial_msg
.length
);
699 memcpy(wb_request
.extra_data
.data
+ initial_msg
.length
,
700 challenge_msg
.data
, challenge_msg
.length
);
703 result
= winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH
, &wb_request
, &wb_response
);
704 SAFE_FREE(wb_request
.extra_data
.data
);
706 if (result
!= NSS_STATUS_SUCCESS
) {
707 winbindd_free_response(&wb_response
);
708 return NT_STATUS_UNSUCCESSFUL
;
712 *reply
= data_blob(wb_response
.extra_data
.data
,
713 wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
);
714 if (wb_response
.data
.ccache_ntlm_auth
.auth_blob_len
> 0 &&
715 reply
->data
== NULL
) {
716 winbindd_free_response(&wb_response
);
717 return NT_STATUS_NO_MEMORY
;
721 winbindd_free_response(&wb_response
);
722 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
725 static void manage_squid_ntlmssp_request(struct ntlm_auth_state
*state
,
726 char *buf
, int length
)
728 DATA_BLOB request
, reply
;
731 if (strlen(buf
) < 2) {
732 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
733 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
737 if (strlen(buf
) > 3) {
738 if(strncmp(buf
, "SF ", 3) == 0){
739 DEBUG(10, ("Setting flags to negotioate\n"));
740 TALLOC_FREE(state
->want_feature_list
);
741 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
743 x_fprintf(x_stdout
, "OK\n");
746 request
= base64_decode_data_blob(buf
+ 3);
748 request
= data_blob_null
;
751 if ((strncmp(buf
, "PW ", 3) == 0)) {
752 /* The calling application wants us to use a local password
753 * (rather than winbindd) */
755 opt_password
= SMB_STRNDUP((const char *)request
.data
,
758 if (opt_password
== NULL
) {
759 DEBUG(1, ("Out of memory\n"));
760 x_fprintf(x_stdout
, "BH Out of memory\n");
761 data_blob_free(&request
);
765 x_fprintf(x_stdout
, "OK\n");
766 data_blob_free(&request
);
770 if (strncmp(buf
, "YR", 2) == 0) {
771 if (state
->ntlmssp_state
)
772 ntlmssp_end(&state
->ntlmssp_state
);
773 state
->svr_state
= SERVER_INITIAL
;
774 } else if (strncmp(buf
, "KK", 2) == 0) {
775 /* No special preprocessing required */
776 } else if (strncmp(buf
, "GF", 2) == 0) {
777 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
779 if (state
->svr_state
== SERVER_FINISHED
) {
780 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
783 x_fprintf(x_stdout
, "BH\n");
785 data_blob_free(&request
);
787 } else if (strncmp(buf
, "GK", 2) == 0) {
788 DEBUG(10, ("Requested NTLMSSP session key\n"));
789 if(state
->have_session_key
) {
790 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
792 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
795 x_fprintf(x_stdout
, "BH\n");
798 data_blob_free(&request
);
801 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
802 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
806 if (!state
->ntlmssp_state
) {
807 nt_status
= ntlm_auth_start_ntlmssp_server(
808 &state
->ntlmssp_state
);
809 if (!NT_STATUS_IS_OK(nt_status
)) {
810 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
813 ntlmssp_want_feature_list(state
->ntlmssp_state
,
814 state
->want_feature_list
);
817 DEBUG(10, ("got NTLMSSP packet:\n"));
818 dump_data(10, request
.data
, request
.length
);
820 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
, &reply
);
822 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
823 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
825 x_fprintf(x_stdout
, "TT %s\n", reply_base64
);
826 TALLOC_FREE(reply_base64
);
827 data_blob_free(&reply
);
828 state
->svr_state
= SERVER_CHALLENGE
;
829 DEBUG(10, ("NTLMSSP challenge\n"));
830 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
831 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
832 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
834 ntlmssp_end(&state
->ntlmssp_state
);
835 } else if (!NT_STATUS_IS_OK(nt_status
)) {
836 x_fprintf(x_stdout
, "NA %s\n", nt_errstr(nt_status
));
837 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
839 x_fprintf(x_stdout
, "AF %s\n",
840 (char *)state
->ntlmssp_state
->auth_context
);
841 DEBUG(10, ("NTLMSSP OK!\n"));
843 if(state
->have_session_key
)
844 data_blob_free(&state
->session_key
);
845 state
->session_key
= data_blob(
846 state
->ntlmssp_state
->session_key
.data
,
847 state
->ntlmssp_state
->session_key
.length
);
848 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
849 state
->have_session_key
= true;
850 state
->svr_state
= SERVER_FINISHED
;
853 data_blob_free(&request
);
856 static void manage_client_ntlmssp_request(struct ntlm_auth_state
*state
,
857 char *buf
, int length
)
859 DATA_BLOB request
, reply
;
862 if (!opt_username
|| !*opt_username
) {
863 x_fprintf(x_stderr
, "username must be specified!\n\n");
867 if (strlen(buf
) < 2) {
868 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
869 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
873 if (strlen(buf
) > 3) {
874 if(strncmp(buf
, "SF ", 3) == 0) {
875 DEBUG(10, ("Looking for flags to negotiate\n"));
876 talloc_free(state
->want_feature_list
);
877 state
->want_feature_list
= talloc_strdup(state
->mem_ctx
,
879 x_fprintf(x_stdout
, "OK\n");
882 request
= base64_decode_data_blob(buf
+ 3);
884 request
= data_blob_null
;
887 if (strncmp(buf
, "PW ", 3) == 0) {
888 /* We asked for a password and obviously got it :-) */
890 opt_password
= SMB_STRNDUP((const char *)request
.data
,
893 if (opt_password
== NULL
) {
894 DEBUG(1, ("Out of memory\n"));
895 x_fprintf(x_stdout
, "BH Out of memory\n");
896 data_blob_free(&request
);
900 x_fprintf(x_stdout
, "OK\n");
901 data_blob_free(&request
);
905 if (!state
->ntlmssp_state
&& use_cached_creds
) {
906 /* check whether cached credentials are usable. */
907 DATA_BLOB empty_blob
= data_blob_null
;
909 nt_status
= do_ccache_ntlm_auth(empty_blob
, empty_blob
, NULL
);
910 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
911 /* failed to use cached creds */
912 use_cached_creds
= False
;
916 if (opt_password
== NULL
&& !use_cached_creds
) {
917 /* Request a password from the calling process. After
918 sending it, the calling process should retry asking for the
921 DEBUG(10, ("Requesting password\n"));
922 x_fprintf(x_stdout
, "PW\n");
926 if (strncmp(buf
, "YR", 2) == 0) {
927 if (state
->ntlmssp_state
)
928 ntlmssp_end(&state
->ntlmssp_state
);
929 state
->cli_state
= CLIENT_INITIAL
;
930 } else if (strncmp(buf
, "TT", 2) == 0) {
931 /* No special preprocessing required */
932 } else if (strncmp(buf
, "GF", 2) == 0) {
933 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
935 if(state
->cli_state
== CLIENT_FINISHED
) {
936 x_fprintf(x_stdout
, "GF 0x%08x\n", state
->neg_flags
);
939 x_fprintf(x_stdout
, "BH\n");
942 data_blob_free(&request
);
944 } else if (strncmp(buf
, "GK", 2) == 0 ) {
945 DEBUG(10, ("Requested session key\n"));
947 if(state
->cli_state
== CLIENT_FINISHED
) {
948 char *key64
= base64_encode_data_blob(state
->mem_ctx
,
950 x_fprintf(x_stdout
, "GK %s\n", key64
?key64
:"<NULL>");
954 x_fprintf(x_stdout
, "BH\n");
957 data_blob_free(&request
);
960 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
961 x_fprintf(x_stdout
, "BH NTLMSSP query invalid\n");
965 if (!state
->ntlmssp_state
) {
966 nt_status
= ntlm_auth_start_ntlmssp_client(
967 &state
->ntlmssp_state
);
968 if (!NT_STATUS_IS_OK(nt_status
)) {
969 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
972 ntlmssp_want_feature_list(state
->ntlmssp_state
,
973 state
->want_feature_list
);
974 state
->initial_message
= data_blob_null
;
977 DEBUG(10, ("got NTLMSSP packet:\n"));
978 dump_data(10, request
.data
, request
.length
);
980 if (use_cached_creds
&& !opt_password
&&
981 (state
->cli_state
== CLIENT_RESPONSE
)) {
982 nt_status
= do_ccache_ntlm_auth(state
->initial_message
, request
,
985 nt_status
= ntlmssp_update(state
->ntlmssp_state
, request
,
989 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
990 char *reply_base64
= base64_encode_data_blob(state
->mem_ctx
,
992 if (state
->cli_state
== CLIENT_INITIAL
) {
993 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
994 state
->initial_message
= reply
;
995 state
->cli_state
= CLIENT_RESPONSE
;
997 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
998 data_blob_free(&reply
);
1000 TALLOC_FREE(reply_base64
);
1001 DEBUG(10, ("NTLMSSP challenge\n"));
1002 } else if (NT_STATUS_IS_OK(nt_status
)) {
1003 char *reply_base64
= base64_encode_data_blob(talloc_tos(),
1005 x_fprintf(x_stdout
, "AF %s\n", reply_base64
);
1006 TALLOC_FREE(reply_base64
);
1008 if(state
->have_session_key
)
1009 data_blob_free(&state
->session_key
);
1011 state
->session_key
= data_blob(
1012 state
->ntlmssp_state
->session_key
.data
,
1013 state
->ntlmssp_state
->session_key
.length
);
1014 state
->neg_flags
= state
->ntlmssp_state
->neg_flags
;
1015 state
->have_session_key
= true;
1017 DEBUG(10, ("NTLMSSP OK!\n"));
1018 state
->cli_state
= CLIENT_FINISHED
;
1019 if (state
->ntlmssp_state
)
1020 ntlmssp_end(&state
->ntlmssp_state
);
1022 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
1023 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
1024 state
->cli_state
= CLIENT_ERROR
;
1025 if (state
->ntlmssp_state
)
1026 ntlmssp_end(&state
->ntlmssp_state
);
1029 data_blob_free(&request
);
1032 static void manage_squid_basic_request(struct ntlm_auth_state
*state
,
1033 char *buf
, int length
)
1038 pass
=(char *)memchr(buf
,' ',length
);
1040 DEBUG(2, ("Password not found. Denying access\n"));
1041 x_fprintf(x_stdout
, "ERR\n");
1047 if (state
->helper_mode
== SQUID_2_5_BASIC
) {
1048 rfc1738_unescape(user
);
1049 rfc1738_unescape(pass
);
1052 if (check_plaintext_auth(user
, pass
, False
)) {
1053 x_fprintf(x_stdout
, "OK\n");
1055 x_fprintf(x_stdout
, "ERR\n");
1059 static void offer_gss_spnego_mechs(void) {
1065 TALLOC_CTX
*ctx
= talloc_tos();
1069 ZERO_STRUCT(spnego
);
1071 myname_lower
= talloc_strdup(ctx
, global_myname());
1072 if (!myname_lower
) {
1075 strlower_m(myname_lower
);
1077 principal
= talloc_asprintf(ctx
, "%s$@%s", myname_lower
, lp_realm());
1082 /* Server negTokenInit (mech offerings) */
1083 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1084 spnego
.negTokenInit
.mechTypes
= SMB_XMALLOC_ARRAY(const char *, 2);
1086 spnego
.negTokenInit
.mechTypes
[0] = smb_xstrdup(OID_KERBEROS5_OLD
);
1087 spnego
.negTokenInit
.mechTypes
[1] = smb_xstrdup(OID_NTLMSSP
);
1088 spnego
.negTokenInit
.mechTypes
[2] = NULL
;
1090 spnego
.negTokenInit
.mechTypes
[0] = smb_xstrdup(OID_NTLMSSP
);
1091 spnego
.negTokenInit
.mechTypes
[1] = NULL
;
1095 spnego
.negTokenInit
.mechListMIC
= data_blob(principal
,
1098 len
= write_spnego_data(&token
, &spnego
);
1099 free_spnego_data(&spnego
);
1102 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1103 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1107 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1108 x_fprintf(x_stdout
, "TT %s *\n", reply_base64
);
1110 TALLOC_FREE(reply_base64
);
1111 data_blob_free(&token
);
1112 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1116 static void manage_gss_spnego_request(struct ntlm_auth_state
*state
,
1117 char *buf
, int length
)
1119 static NTLMSSP_STATE
*ntlmssp_state
= NULL
;
1120 SPNEGO_DATA request
, response
;
1124 TALLOC_CTX
*ctx
= talloc_tos();
1127 char *domain
= NULL
;
1129 const char *reply_code
;
1131 char *reply_argument
= NULL
;
1133 if (strlen(buf
) < 2) {
1134 DEBUG(1, ("SPENGO query [%s] invalid", buf
));
1135 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1139 if (strncmp(buf
, "YR", 2) == 0) {
1141 ntlmssp_end(&ntlmssp_state
);
1142 } else if (strncmp(buf
, "KK", 2) == 0) {
1145 DEBUG(1, ("SPENGO query [%s] invalid", buf
));
1146 x_fprintf(x_stdout
, "BH SPENGO query invalid\n");
1150 if ( (strlen(buf
) == 2)) {
1152 /* no client data, get the negTokenInit offering
1155 offer_gss_spnego_mechs();
1159 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1161 if (strlen(buf
) <= 3) {
1162 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
1163 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1167 token
= base64_decode_data_blob(buf
+ 3);
1168 len
= read_spnego_data(token
, &request
);
1169 data_blob_free(&token
);
1172 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf
));
1173 x_fprintf(x_stdout
, "BH GSS-SPNEGO query invalid\n");
1177 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1179 /* Second request from Client. This is where the
1180 client offers its mechanism to use. */
1182 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
1183 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
1184 DEBUG(1, ("Client did not offer any mechanism"));
1185 x_fprintf(x_stdout
, "BH Client did not offer any "
1190 status
= NT_STATUS_UNSUCCESSFUL
;
1191 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_NTLMSSP
) == 0) {
1193 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
1194 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1195 x_fprintf(x_stdout
, "BH Client did not provide "
1200 if ( ntlmssp_state
!= NULL
) {
1201 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1202 "already got one\n"));
1203 x_fprintf(x_stdout
, "BH Client wants a new "
1204 "NTLMSSP challenge, but "
1205 "already got one\n");
1206 ntlmssp_end(&ntlmssp_state
);
1210 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_server(&ntlmssp_state
))) {
1211 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1215 DEBUG(10, ("got NTLMSSP packet:\n"));
1216 dump_data(10, request
.negTokenInit
.mechToken
.data
,
1217 request
.negTokenInit
.mechToken
.length
);
1219 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1220 response
.negTokenTarg
.supportedMech
= SMB_STRDUP(OID_NTLMSSP
);
1221 response
.negTokenTarg
.mechListMIC
= data_blob_null
;
1223 status
= ntlmssp_update(ntlmssp_state
,
1224 request
.negTokenInit
.mechToken
,
1225 &response
.negTokenTarg
.responseToken
);
1229 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_KERBEROS5_OLD
) == 0) {
1231 TALLOC_CTX
*mem_ctx
= talloc_init("manage_gss_spnego_request");
1234 DATA_BLOB session_key
;
1235 struct PAC_DATA
*pac_data
= NULL
;
1237 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
1238 DEBUG(1, ("Client did not provide Kerberos data\n"));
1239 x_fprintf(x_stdout
, "BH Client did not provide "
1244 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1245 response
.negTokenTarg
.supportedMech
= SMB_STRDUP(OID_KERBEROS5_OLD
);
1246 response
.negTokenTarg
.mechListMIC
= data_blob_null
;
1247 response
.negTokenTarg
.responseToken
= data_blob_null
;
1249 status
= ads_verify_ticket(mem_ctx
, lp_realm(), 0,
1250 &request
.negTokenInit
.mechToken
,
1251 &principal
, &pac_data
, &ap_rep
,
1252 &session_key
, True
);
1254 talloc_destroy(mem_ctx
);
1256 /* Now in "principal" we have the name we are
1257 authenticated as. */
1259 if (NT_STATUS_IS_OK(status
)) {
1261 domain
= strchr_m(principal
, '@');
1263 if (domain
== NULL
) {
1264 DEBUG(1, ("Did not get a valid principal "
1265 "from ads_verify_ticket\n"));
1266 x_fprintf(x_stdout
, "BH Did not get a "
1267 "valid principal from "
1268 "ads_verify_ticket\n");
1273 domain
= SMB_STRDUP(domain
);
1274 user
= SMB_STRDUP(principal
);
1276 data_blob_free(&ap_rep
);
1278 SAFE_FREE(principal
);
1285 if ( (request
.negTokenTarg
.supportedMech
== NULL
) ||
1286 ( strcmp(request
.negTokenTarg
.supportedMech
, OID_NTLMSSP
) != 0 ) ) {
1287 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1288 is the only one we support that sends this stuff */
1289 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1290 request
.negTokenTarg
.supportedMech
));
1291 x_fprintf(x_stdout
, "BH Got a negTokenTarg for "
1292 "something non-NTLMSSP\n");
1296 if (request
.negTokenTarg
.responseToken
.data
== NULL
) {
1297 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1298 x_fprintf(x_stdout
, "BH Got a negTokenTarg without a "
1299 "responseToken!\n");
1303 status
= ntlmssp_update(ntlmssp_state
,
1304 request
.negTokenTarg
.responseToken
,
1305 &response
.negTokenTarg
.responseToken
);
1307 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
1308 response
.negTokenTarg
.supportedMech
= SMB_STRDUP(OID_NTLMSSP
);
1309 response
.negTokenTarg
.mechListMIC
= data_blob_null
;
1311 if (NT_STATUS_IS_OK(status
)) {
1312 user
= SMB_STRDUP(ntlmssp_state
->user
);
1313 domain
= SMB_STRDUP(ntlmssp_state
->domain
);
1314 ntlmssp_end(&ntlmssp_state
);
1318 free_spnego_data(&request
);
1320 if (NT_STATUS_IS_OK(status
)) {
1321 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
1323 reply_argument
= talloc_asprintf(ctx
, "%s\\%s", domain
, user
);
1324 } else if (NT_STATUS_EQUAL(status
,
1325 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1326 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1328 reply_argument
= talloc_strdup(ctx
, "*");
1330 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1332 reply_argument
= talloc_strdup(ctx
, nt_errstr(status
));
1335 if (!reply_argument
) {
1336 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1337 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1344 len
= write_spnego_data(&token
, &response
);
1345 free_spnego_data(&response
);
1348 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1349 x_fprintf(x_stdout
, "BH Could not write SPNEGO data blob\n");
1353 reply_base64
= base64_encode_data_blob(talloc_tos(), token
);
1355 x_fprintf(x_stdout
, "%s %s %s\n",
1356 reply_code
, reply_base64
, reply_argument
);
1358 TALLOC_FREE(reply_base64
);
1359 data_blob_free(&token
);
1364 static NTLMSSP_STATE
*client_ntlmssp_state
= NULL
;
1366 static bool manage_client_ntlmssp_init(SPNEGO_DATA spnego
)
1369 DATA_BLOB null_blob
= data_blob_null
;
1370 DATA_BLOB to_server
;
1371 char *to_server_base64
;
1372 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1374 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1376 if (client_ntlmssp_state
!= NULL
) {
1377 DEBUG(1, ("Request for initial SPNEGO request where "
1378 "we already have a state\n"));
1382 if (!client_ntlmssp_state
) {
1383 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1384 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1390 if (opt_password
== NULL
) {
1392 /* Request a password from the calling process. After
1393 sending it, the calling process should retry with
1394 the negTokenInit. */
1396 DEBUG(10, ("Requesting password\n"));
1397 x_fprintf(x_stdout
, "PW\n");
1401 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1402 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1403 spnego
.negTokenInit
.reqFlags
= 0;
1404 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1406 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1407 &spnego
.negTokenInit
.mechToken
);
1409 if ( !(NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) ||
1410 NT_STATUS_IS_OK(status
)) ) {
1411 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1412 nt_errstr(status
)));
1413 ntlmssp_end(&client_ntlmssp_state
);
1417 write_spnego_data(&to_server
, &spnego
);
1418 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1420 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1421 data_blob_free(&to_server
);
1422 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1423 TALLOC_FREE(to_server_base64
);
1427 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego
)
1430 DATA_BLOB null_blob
= data_blob_null
;
1432 DATA_BLOB to_server
;
1433 char *to_server_base64
;
1435 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1437 if (client_ntlmssp_state
== NULL
) {
1438 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1439 x_fprintf(x_stdout
, "BH Got NTLMSSP tArg without a client state\n");
1443 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1444 x_fprintf(x_stdout
, "NA\n");
1445 ntlmssp_end(&client_ntlmssp_state
);
1449 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1450 x_fprintf(x_stdout
, "AF\n");
1451 ntlmssp_end(&client_ntlmssp_state
);
1455 status
= ntlmssp_update(client_ntlmssp_state
,
1456 spnego
.negTokenTarg
.responseToken
,
1459 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1460 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1461 "ntlmssp_client_update, got: %s\n",
1462 nt_errstr(status
)));
1463 x_fprintf(x_stdout
, "BH Expected MORE_PROCESSING_REQUIRED from "
1464 "ntlmssp_client_update\n");
1465 data_blob_free(&request
);
1466 ntlmssp_end(&client_ntlmssp_state
);
1470 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1471 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1472 spnego
.negTokenTarg
.supportedMech
= (char *)OID_NTLMSSP
;
1473 spnego
.negTokenTarg
.responseToken
= request
;
1474 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1476 write_spnego_data(&to_server
, &spnego
);
1477 data_blob_free(&request
);
1479 to_server_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1480 data_blob_free(&to_server
);
1481 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1482 TALLOC_FREE(to_server_base64
);
1488 static bool manage_client_krb5_init(SPNEGO_DATA spnego
)
1491 DATA_BLOB tkt
, to_server
;
1492 DATA_BLOB session_key_krb5
= data_blob_null
;
1497 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1500 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1501 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1502 DEBUG(1, ("Did not get a principal for krb5\n"));
1506 principal
= (char *)SMB_MALLOC(
1507 spnego
.negTokenInit
.mechListMIC
.length
+1);
1509 if (principal
== NULL
) {
1510 DEBUG(1, ("Could not malloc principal\n"));
1514 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1515 spnego
.negTokenInit
.mechListMIC
.length
);
1516 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1518 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
, 0, NULL
, NULL
);
1523 /* Let's try to first get the TGT, for that we need a
1526 if (opt_password
== NULL
) {
1527 DEBUG(10, ("Requesting password\n"));
1528 x_fprintf(x_stdout
, "PW\n");
1532 user
= talloc_asprintf(talloc_tos(), "%s@%s", opt_username
, opt_domain
);
1537 if ((retval
= kerberos_kinit_password(user
, opt_password
, 0, NULL
))) {
1538 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval
)));
1542 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
, 0, NULL
, NULL
);
1545 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1550 data_blob_free(&session_key_krb5
);
1554 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1555 reply
.negTokenInit
.mechTypes
= my_mechs
;
1556 reply
.negTokenInit
.reqFlags
= 0;
1557 reply
.negTokenInit
.mechToken
= tkt
;
1558 reply
.negTokenInit
.mechListMIC
= data_blob_null
;
1560 len
= write_spnego_data(&to_server
, &reply
);
1561 data_blob_free(&tkt
);
1564 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1568 reply_base64
= base64_encode_data_blob(talloc_tos(), to_server
);
1569 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1571 TALLOC_FREE(reply_base64
);
1572 data_blob_free(&to_server
);
1573 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1577 static void manage_client_krb5_targ(SPNEGO_DATA spnego
)
1579 switch (spnego
.negTokenTarg
.negResult
) {
1580 case SPNEGO_ACCEPT_INCOMPLETE
:
1581 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1582 x_fprintf(x_stdout
, "BH Got a Kerberos negTokenTarg with "
1583 "ACCEPT_INCOMPLETE\n");
1585 case SPNEGO_ACCEPT_COMPLETED
:
1586 DEBUG(10, ("Accept completed\n"));
1587 x_fprintf(x_stdout
, "AF\n");
1590 DEBUG(10, ("Rejected\n"));
1591 x_fprintf(x_stdout
, "NA\n");
1594 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1595 x_fprintf(x_stdout
, "AF\n");
1601 static void manage_gss_spnego_client_request(struct ntlm_auth_state
*state
,
1602 char *buf
, int length
)
1608 if (!opt_username
|| !*opt_username
) {
1609 x_fprintf(x_stderr
, "username must be specified!\n\n");
1613 if (strlen(buf
) <= 3) {
1614 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1615 x_fprintf(x_stdout
, "BH SPNEGO query too short\n");
1619 request
= base64_decode_data_blob(buf
+3);
1621 if (strncmp(buf
, "PW ", 3) == 0) {
1623 /* We asked for a password and obviously got it :-) */
1625 opt_password
= SMB_STRNDUP((const char *)request
.data
, request
.length
);
1627 if (opt_password
== NULL
) {
1628 DEBUG(1, ("Out of memory\n"));
1629 x_fprintf(x_stdout
, "BH Out of memory\n");
1630 data_blob_free(&request
);
1634 x_fprintf(x_stdout
, "OK\n");
1635 data_blob_free(&request
);
1639 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1640 (strncmp(buf
, "AF ", 3) != 0) &&
1641 (strncmp(buf
, "NA ", 3) != 0) ) {
1642 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1643 x_fprintf(x_stdout
, "BH SPNEGO request invalid\n");
1644 data_blob_free(&request
);
1648 /* So we got a server challenge to generate a SPNEGO
1649 client-to-server request... */
1651 len
= read_spnego_data(request
, &spnego
);
1652 data_blob_free(&request
);
1655 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1656 x_fprintf(x_stdout
, "BH Could not read SPNEGO data\n");
1660 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1662 /* The server offers a list of mechanisms */
1664 const char **mechType
= (const char **)spnego
.negTokenInit
.mechTypes
;
1666 while (*mechType
!= NULL
) {
1669 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1670 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1671 if (manage_client_krb5_init(spnego
))
1676 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1677 if (manage_client_ntlmssp_init(spnego
))
1684 DEBUG(1, ("Server offered no compatible mechanism\n"));
1685 x_fprintf(x_stdout
, "BH Server offered no compatible mechanism\n");
1689 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1691 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1692 /* On accept/reject Windows does not send the
1693 mechanism anymore. Handle that here and
1694 shut down the mechanisms. */
1696 switch (spnego
.negTokenTarg
.negResult
) {
1697 case SPNEGO_ACCEPT_COMPLETED
:
1698 x_fprintf(x_stdout
, "AF\n");
1701 x_fprintf(x_stdout
, "NA\n");
1704 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1705 "unknown negResult: %d\n",
1706 spnego
.negTokenTarg
.negResult
));
1707 x_fprintf(x_stdout
, "BH Got a negTokenTarg with"
1708 " no mech and an unknown "
1712 ntlmssp_end(&client_ntlmssp_state
);
1716 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1717 OID_NTLMSSP
) == 0) {
1718 manage_client_ntlmssp_targ(spnego
);
1723 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1724 OID_KERBEROS5_OLD
) == 0) {
1725 manage_client_krb5_targ(spnego
);
1732 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1733 x_fprintf(x_stdout
, "BH Got an SPNEGO token I could not handle\n");
1737 free_spnego_data(&spnego
);
1741 static void manage_ntlm_server_1_request(struct ntlm_auth_state
*state
,
1742 char *buf
, int length
)
1744 char *request
, *parameter
;
1745 static DATA_BLOB challenge
;
1746 static DATA_BLOB lm_response
;
1747 static DATA_BLOB nt_response
;
1748 static char *full_username
;
1749 static char *username
;
1750 static char *domain
;
1751 static char *plaintext_password
;
1752 static bool ntlm_server_1_user_session_key
;
1753 static bool ntlm_server_1_lm_session_key
;
1755 if (strequal(buf
, ".")) {
1756 if (!full_username
&& !username
) {
1757 x_fprintf(x_stdout
, "Error: No username supplied!\n");
1758 } else if (plaintext_password
) {
1759 /* handle this request as plaintext */
1760 if (!full_username
) {
1761 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
1762 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
1766 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
1767 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1769 x_fprintf(x_stdout
, "Authenticated: No\n");
1771 } else if (!lm_response
.data
&& !nt_response
.data
) {
1772 x_fprintf(x_stdout
, "Error: No password supplied!\n");
1773 } else if (!challenge
.data
) {
1774 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
1776 char *error_string
= NULL
;
1778 uchar user_session_key
[16];
1781 if (full_username
&& !username
) {
1783 fstring fstr_domain
;
1785 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
1786 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1787 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
1789 SAFE_FREE(username
);
1791 username
= smb_xstrdup(fstr_user
);
1792 domain
= smb_xstrdup(fstr_domain
);
1796 domain
= smb_xstrdup(get_winbind_domain());
1799 if (ntlm_server_1_lm_session_key
)
1800 flags
|= WBFLAG_PAM_LMKEY
;
1802 if (ntlm_server_1_user_session_key
)
1803 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
1805 if (!NT_STATUS_IS_OK(
1806 contact_winbind_auth_crap(username
,
1818 x_fprintf(x_stdout
, "Authenticated: No\n");
1819 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
1821 static char zeros
[16];
1823 char *hex_user_session_key
;
1825 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1827 if (ntlm_server_1_lm_session_key
1828 && (memcmp(zeros
, lm_key
,
1829 sizeof(lm_key
)) != 0)) {
1830 hex_lm_key
= hex_encode(NULL
,
1831 (const unsigned char *)lm_key
,
1833 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
1834 TALLOC_FREE(hex_lm_key
);
1837 if (ntlm_server_1_user_session_key
1838 && (memcmp(zeros
, user_session_key
,
1839 sizeof(user_session_key
)) != 0)) {
1840 hex_user_session_key
= hex_encode(NULL
,
1841 (const unsigned char *)user_session_key
,
1842 sizeof(user_session_key
));
1843 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
1844 TALLOC_FREE(hex_user_session_key
);
1847 SAFE_FREE(error_string
);
1849 /* clear out the state */
1850 challenge
= data_blob_null
;
1851 nt_response
= data_blob_null
;
1852 lm_response
= data_blob_null
;
1853 SAFE_FREE(full_username
);
1854 SAFE_FREE(username
);
1856 SAFE_FREE(plaintext_password
);
1857 ntlm_server_1_user_session_key
= False
;
1858 ntlm_server_1_lm_session_key
= False
;
1859 x_fprintf(x_stdout
, ".\n");
1866 /* Indicates a base64 encoded structure */
1867 parameter
= strstr_m(request
, ":: ");
1869 parameter
= strstr_m(request
, ": ");
1872 DEBUG(0, ("Parameter not found!\n"));
1873 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
1890 base64_decode_inplace(parameter
);
1893 if (strequal(request
, "LANMAN-Challenge")) {
1894 challenge
= strhex_to_data_blob(NULL
, parameter
);
1895 if (challenge
.length
!= 8) {
1896 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1898 (int)challenge
.length
);
1899 challenge
= data_blob_null
;
1901 } else if (strequal(request
, "NT-Response")) {
1902 nt_response
= strhex_to_data_blob(NULL
, parameter
);
1903 if (nt_response
.length
< 24) {
1904 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1906 (int)nt_response
.length
);
1907 nt_response
= data_blob_null
;
1909 } else if (strequal(request
, "LANMAN-Response")) {
1910 lm_response
= strhex_to_data_blob(NULL
, parameter
);
1911 if (lm_response
.length
!= 24) {
1912 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
1914 (int)lm_response
.length
);
1915 lm_response
= data_blob_null
;
1917 } else if (strequal(request
, "Password")) {
1918 plaintext_password
= smb_xstrdup(parameter
);
1919 } else if (strequal(request
, "NT-Domain")) {
1920 domain
= smb_xstrdup(parameter
);
1921 } else if (strequal(request
, "Username")) {
1922 username
= smb_xstrdup(parameter
);
1923 } else if (strequal(request
, "Full-Username")) {
1924 full_username
= smb_xstrdup(parameter
);
1925 } else if (strequal(request
, "Request-User-Session-Key")) {
1926 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
1927 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
1928 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
1930 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
1934 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state
*state
,
1935 char *buf
, int length
)
1937 char *request
, *parameter
;
1938 static DATA_BLOB new_nt_pswd
;
1939 static DATA_BLOB old_nt_hash_enc
;
1940 static DATA_BLOB new_lm_pswd
;
1941 static DATA_BLOB old_lm_hash_enc
;
1942 static char *full_username
= NULL
;
1943 static char *username
= NULL
;
1944 static char *domain
= NULL
;
1945 static char *newpswd
= NULL
;
1946 static char *oldpswd
= NULL
;
1948 if (strequal(buf
, ".")) {
1949 if(newpswd
&& oldpswd
) {
1950 uchar old_nt_hash
[16];
1951 uchar old_lm_hash
[16];
1952 uchar new_nt_hash
[16];
1953 uchar new_lm_hash
[16];
1955 new_nt_pswd
= data_blob(NULL
, 516);
1956 old_nt_hash_enc
= data_blob(NULL
, 16);
1958 /* Calculate the MD4 hash (NT compatible) of the
1960 E_md4hash(oldpswd
, old_nt_hash
);
1961 E_md4hash(newpswd
, new_nt_hash
);
1963 /* E_deshash returns false for 'long'
1964 passwords (> 14 DOS chars).
1966 Therefore, don't send a buffer
1967 encrypted with the truncated hash
1968 (it could allow an even easier
1969 attack on the password)
1971 Likewise, obey the admin's restriction
1974 if (lp_client_lanman_auth() &&
1975 E_deshash(newpswd
, new_lm_hash
) &&
1976 E_deshash(oldpswd
, old_lm_hash
)) {
1977 new_lm_pswd
= data_blob(NULL
, 516);
1978 old_lm_hash_enc
= data_blob(NULL
, 16);
1979 encode_pw_buffer(new_lm_pswd
.data
, newpswd
,
1982 SamOEMhash(new_lm_pswd
.data
, old_nt_hash
, 516);
1983 E_old_pw_hash(new_nt_hash
, old_lm_hash
,
1984 old_lm_hash_enc
.data
);
1986 new_lm_pswd
.data
= NULL
;
1987 new_lm_pswd
.length
= 0;
1988 old_lm_hash_enc
.data
= NULL
;
1989 old_lm_hash_enc
.length
= 0;
1992 encode_pw_buffer(new_nt_pswd
.data
, newpswd
,
1995 SamOEMhash(new_nt_pswd
.data
, old_nt_hash
, 516);
1996 E_old_pw_hash(new_nt_hash
, old_nt_hash
,
1997 old_nt_hash_enc
.data
);
2000 if (!full_username
&& !username
) {
2001 x_fprintf(x_stdout
, "Error: No username supplied!\n");
2002 } else if ((!new_nt_pswd
.data
|| !old_nt_hash_enc
.data
) &&
2003 (!new_lm_pswd
.data
|| old_lm_hash_enc
.data
) ) {
2004 x_fprintf(x_stdout
, "Error: No NT or LM password "
2005 "blobs supplied!\n");
2007 char *error_string
= NULL
;
2009 if (full_username
&& !username
) {
2011 fstring fstr_domain
;
2013 if (!parse_ntlm_auth_domain_user(full_username
,
2016 /* username might be 'tainted', don't
2017 * print into our new-line
2018 * deleimianted stream */
2019 x_fprintf(x_stdout
, "Error: Could not "
2020 "parse into domain and "
2022 SAFE_FREE(username
);
2023 username
= smb_xstrdup(full_username
);
2025 SAFE_FREE(username
);
2027 username
= smb_xstrdup(fstr_user
);
2028 domain
= smb_xstrdup(fstr_domain
);
2033 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2040 x_fprintf(x_stdout
, "Password-Change: No\n");
2041 x_fprintf(x_stdout
, "Password-Change-Error: "
2042 "%s\n.\n", error_string
);
2044 x_fprintf(x_stdout
, "Password-Change: Yes\n");
2047 SAFE_FREE(error_string
);
2049 /* clear out the state */
2050 new_nt_pswd
= data_blob_null
;
2051 old_nt_hash_enc
= data_blob_null
;
2052 new_lm_pswd
= data_blob_null
;
2053 old_nt_hash_enc
= data_blob_null
;
2054 SAFE_FREE(full_username
);
2055 SAFE_FREE(username
);
2059 x_fprintf(x_stdout
, ".\n");
2066 /* Indicates a base64 encoded structure */
2067 parameter
= strstr_m(request
, ":: ");
2069 parameter
= strstr_m(request
, ": ");
2072 DEBUG(0, ("Parameter not found!\n"));
2073 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
2089 base64_decode_inplace(parameter
);
2092 if (strequal(request
, "new-nt-password-blob")) {
2093 new_nt_pswd
= strhex_to_data_blob(NULL
, parameter
);
2094 if (new_nt_pswd
.length
!= 516) {
2095 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2096 "(got %d bytes, expected 516)\n.\n",
2098 (int)new_nt_pswd
.length
);
2099 new_nt_pswd
= data_blob_null
;
2101 } else if (strequal(request
, "old-nt-hash-blob")) {
2102 old_nt_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2103 if (old_nt_hash_enc
.length
!= 16) {
2104 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2105 "(got %d bytes, expected 16)\n.\n",
2107 (int)old_nt_hash_enc
.length
);
2108 old_nt_hash_enc
= data_blob_null
;
2110 } else if (strequal(request
, "new-lm-password-blob")) {
2111 new_lm_pswd
= strhex_to_data_blob(NULL
, parameter
);
2112 if (new_lm_pswd
.length
!= 516) {
2113 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2114 "(got %d bytes, expected 516)\n.\n",
2116 (int)new_lm_pswd
.length
);
2117 new_lm_pswd
= data_blob_null
;
2120 else if (strequal(request
, "old-lm-hash-blob")) {
2121 old_lm_hash_enc
= strhex_to_data_blob(NULL
, parameter
);
2122 if (old_lm_hash_enc
.length
!= 16)
2124 x_fprintf(x_stdout
, "Error: hex decode of %s failed! "
2125 "(got %d bytes, expected 16)\n.\n",
2127 (int)old_lm_hash_enc
.length
);
2128 old_lm_hash_enc
= data_blob_null
;
2130 } else if (strequal(request
, "nt-domain")) {
2131 domain
= smb_xstrdup(parameter
);
2132 } else if(strequal(request
, "username")) {
2133 username
= smb_xstrdup(parameter
);
2134 } else if(strequal(request
, "full-username")) {
2135 username
= smb_xstrdup(parameter
);
2136 } else if(strequal(request
, "new-password")) {
2137 newpswd
= smb_xstrdup(parameter
);
2138 } else if (strequal(request
, "old-password")) {
2139 oldpswd
= smb_xstrdup(parameter
);
2141 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
2145 static void manage_squid_request(struct ntlm_auth_state
*state
,
2146 stdio_helper_function fn
)
2149 char tmp
[INITIAL_BUFFER_SIZE
+1];
2150 int length
, buf_size
= 0;
2153 buf
= talloc_strdup(state
->mem_ctx
, "");
2155 DEBUG(0, ("Failed to allocate input buffer.\n"));
2156 x_fprintf(x_stderr
, "ERR\n");
2162 /* this is not a typo - x_fgets doesn't work too well under
2164 if (fgets(tmp
, sizeof(tmp
)-1, stdin
) == NULL
) {
2165 if (ferror(stdin
)) {
2166 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2167 "(%s)\n", ferror(stdin
),
2168 strerror(ferror(stdin
))));
2175 buf
= talloc_strdup_append_buffer(buf
, tmp
);
2176 buf_size
+= INITIAL_BUFFER_SIZE
;
2178 if (buf_size
> MAX_BUFFER_SIZE
) {
2179 DEBUG(2, ("Oversized message\n"));
2180 x_fprintf(x_stderr
, "ERR\n");
2185 c
= strchr(buf
, '\n');
2186 } while (c
== NULL
);
2191 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
2193 if (buf
[0] == '\0') {
2194 DEBUG(2, ("Invalid Request\n"));
2195 x_fprintf(x_stderr
, "ERR\n");
2200 fn(state
, buf
, length
);
2205 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
2206 TALLOC_CTX
*mem_ctx
;
2207 struct ntlm_auth_state
*state
;
2209 /* initialize FDescs */
2210 x_setbuf(x_stdout
, NULL
);
2211 x_setbuf(x_stderr
, NULL
);
2213 mem_ctx
= talloc_init("ntlm_auth");
2215 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2216 x_fprintf(x_stderr
, "ERR\n");
2220 state
= talloc_zero(mem_ctx
, struct ntlm_auth_state
);
2222 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2223 x_fprintf(x_stderr
, "ERR\n");
2227 state
->mem_ctx
= mem_ctx
;
2228 state
->helper_mode
= stdio_mode
;
2231 manage_squid_request(state
, fn
);
2236 /* Authenticate a user with a challenge/response */
2238 static bool check_auth_crap(void)
2243 char user_session_key
[16];
2245 char *hex_user_session_key
;
2247 static uint8 zeros
[16];
2249 x_setbuf(x_stdout
, NULL
);
2252 flags
|= WBFLAG_PAM_LMKEY
;
2254 if (request_user_session_key
)
2255 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
2257 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
2259 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
2265 (unsigned char *)lm_key
,
2266 (unsigned char *)user_session_key
,
2267 &error_string
, NULL
);
2269 if (!NT_STATUS_IS_OK(nt_status
)) {
2270 x_fprintf(x_stdout
, "%s (0x%x)\n",
2272 NT_STATUS_V(nt_status
));
2273 SAFE_FREE(error_string
);
2278 && (memcmp(zeros
, lm_key
,
2279 sizeof(lm_key
)) != 0)) {
2280 hex_lm_key
= hex_encode(NULL
, (const unsigned char *)lm_key
,
2282 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
2283 TALLOC_FREE(hex_lm_key
);
2285 if (request_user_session_key
2286 && (memcmp(zeros
, user_session_key
,
2287 sizeof(user_session_key
)) != 0)) {
2288 hex_user_session_key
= hex_encode(NULL
, (const unsigned char *)user_session_key
,
2289 sizeof(user_session_key
));
2290 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
2291 TALLOC_FREE(hex_user_session_key
);
2300 OPT_USERNAME
= 1000,
2309 OPT_USER_SESSION_KEY
,
2311 OPT_REQUIRE_MEMBERSHIP
,
2312 OPT_USE_CACHED_CREDS
2315 int main(int argc
, const char **argv
)
2317 TALLOC_CTX
*frame
= talloc_stackframe();
2319 static const char *helper_protocol
;
2320 static int diagnostics
;
2322 static const char *hex_challenge
;
2323 static const char *hex_lm_response
;
2324 static const char *hex_nt_response
;
2328 /* NOTE: DO NOT change this interface without considering the implications!
2329 This is an external interface, which other programs will use to interact
2333 /* We do not use single-letter command abbreviations, because they harm future
2334 interface stability. */
2336 struct poptOption long_options
[] = {
2338 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
2339 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
2340 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
2341 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
2342 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
2343 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
2344 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2345 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
2346 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retrieve LM session key"},
2347 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retrieve User (NT) session key"},
2348 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "Use cached credentials if no password is given"},
2349 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
, OPT_DIAGNOSTICS
, "Perform diagnostics on the authentictaion chain"},
2350 { "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" },
2351 POPT_COMMON_CONFIGFILE
2356 /* Samba client initialisation */
2363 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
2365 /* Parse command line options */
2368 poptPrintHelp(pc
, stderr
, 0);
2372 while((opt
= poptGetNextOpt(pc
)) != -1) {
2373 /* Get generic config options like --configfile */
2376 poptFreeContext(pc
);
2378 if (!lp_load(get_dyn_CONFIGFILE(), True
, False
, False
, True
)) {
2379 d_fprintf(stderr
, "ntlm_auth: error opening config file %s. Error was %s\n",
2380 get_dyn_CONFIGFILE(), strerror(errno
));
2384 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
2385 POPT_CONTEXT_KEEP_FIRST
);
2387 while((opt
= poptGetNextOpt(pc
)) != -1) {
2390 opt_challenge
= strhex_to_data_blob(NULL
, hex_challenge
);
2391 if (opt_challenge
.length
!= 8) {
2392 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2394 (int)opt_challenge
.length
);
2399 opt_lm_response
= strhex_to_data_blob(NULL
, hex_lm_response
);
2400 if (opt_lm_response
.length
!= 24) {
2401 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2403 (int)opt_lm_response
.length
);
2409 opt_nt_response
= strhex_to_data_blob(NULL
, hex_nt_response
);
2410 if (opt_nt_response
.length
< 24) {
2411 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
2413 (int)opt_nt_response
.length
);
2418 case OPT_REQUIRE_MEMBERSHIP
:
2419 if (StrnCaseCmp("S-", require_membership_of
, 2) == 0) {
2420 require_membership_of_sid
= require_membership_of
;
2427 char *domain
= SMB_STRDUP(opt_username
);
2428 char *p
= strchr_m(domain
, *lp_winbind_separator());
2432 if (opt_domain
&& !strequal(opt_domain
, domain
)) {
2433 x_fprintf(x_stderr
, "Domain specified in username (%s) "
2434 "doesn't match specified domain (%s)!\n\n",
2435 domain
, opt_domain
);
2436 poptPrintHelp(pc
, stderr
, 0);
2439 opt_domain
= domain
;
2445 /* Note: if opt_domain is "" then send no domain */
2446 if (opt_domain
== NULL
) {
2447 opt_domain
= get_winbind_domain();
2450 if (opt_workstation
== NULL
) {
2451 opt_workstation
= "";
2454 if (helper_protocol
) {
2456 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2457 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
2458 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
2462 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
2464 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
2465 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
2471 if (!opt_username
|| !*opt_username
) {
2472 x_fprintf(x_stderr
, "username must be specified!\n\n");
2473 poptPrintHelp(pc
, stderr
, 0);
2477 if (opt_challenge
.length
) {
2478 if (!check_auth_crap()) {
2484 if (!opt_password
) {
2485 opt_password
= getpass("password: ");
2489 if (!diagnose_ntlm_auth()) {
2495 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
2496 if (!check_plaintext_auth(user
, opt_password
, True
)) {
2503 poptFreeContext(pc
);