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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "../utils/ntlm_auth.h"
29 #define DBGC_CLASS DBGC_WINBIND
31 #define SQUID_BUFFER_SIZE 2010
33 enum stdio_helper_mode
{
44 typedef void (*stdio_helper_function
)(enum stdio_helper_mode stdio_helper_mode
,
45 char *buf
, int length
);
47 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode
,
48 char *buf
, int length
);
50 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode
,
51 char *buf
, int length
);
53 static void manage_client_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode
,
54 char *buf
, int length
);
56 static void manage_gss_spnego_request (enum stdio_helper_mode stdio_helper_mode
,
57 char *buf
, int length
);
59 static void manage_gss_spnego_client_request (enum stdio_helper_mode stdio_helper_mode
,
60 char *buf
, int length
);
62 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode
,
63 char *buf
, int length
);
66 enum stdio_helper_mode mode
;
68 stdio_helper_function fn
;
69 } stdio_helper_protocols
[] = {
70 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
71 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
72 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request
},
73 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_client_ntlmssp_request
},
74 { GSS_SPNEGO
, "gss-spnego", manage_gss_spnego_request
},
75 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gss_spnego_client_request
},
76 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
77 { NUM_HELPER_MODES
, NULL
, NULL
}
80 extern int winbindd_fd
;
82 const char *opt_username
;
83 const char *opt_domain
;
84 const char *opt_workstation
;
85 const char *opt_password
;
86 static DATA_BLOB opt_challenge
;
87 static DATA_BLOB opt_lm_response
;
88 static DATA_BLOB opt_nt_response
;
89 static int request_lm_key
;
90 static int request_user_session_key
;
92 static const char *require_membership_of
;
93 static const char *require_membership_sid
;
95 static char winbind_separator(void)
97 struct winbindd_response response
;
104 ZERO_STRUCT(response
);
106 /* Send off request */
108 if (winbindd_request(WINBINDD_INFO
, NULL
, &response
) !=
109 NSS_STATUS_SUCCESS
) {
110 d_printf("could not obtain winbind separator!\n");
111 return *lp_winbind_separator();
114 sep
= response
.data
.info
.winbind_separator
;
118 d_printf("winbind separator was NULL!\n");
119 return *lp_winbind_separator();
125 const char *get_winbind_domain(void)
127 struct winbindd_response response
;
129 static fstring winbind_domain
;
130 if (*winbind_domain
) {
131 return winbind_domain
;
134 ZERO_STRUCT(response
);
136 /* Send off request */
138 if (winbindd_request(WINBINDD_DOMAIN_NAME
, NULL
, &response
) !=
139 NSS_STATUS_SUCCESS
) {
140 DEBUG(0, ("could not obtain winbind domain name!\n"));
141 return lp_workgroup();
144 fstrcpy(winbind_domain
, response
.data
.domain_name
);
146 return winbind_domain
;
150 const char *get_winbind_netbios_name(void)
152 struct winbindd_response response
;
154 static fstring winbind_netbios_name
;
156 if (*winbind_netbios_name
) {
157 return winbind_netbios_name
;
160 ZERO_STRUCT(response
);
162 /* Send off request */
164 if (winbindd_request(WINBINDD_NETBIOS_NAME
, NULL
, &response
) !=
165 NSS_STATUS_SUCCESS
) {
166 DEBUG(0, ("could not obtain winbind netbios name!\n"));
167 return global_myname();
170 fstrcpy(winbind_netbios_name
, response
.data
.netbios_name
);
172 return winbind_netbios_name
;
176 DATA_BLOB
get_challenge(void)
178 static DATA_BLOB chal
;
179 if (opt_challenge
.length
)
180 return opt_challenge
;
182 chal
= data_blob(NULL
, 8);
184 generate_random_buffer(chal
.data
, chal
.length
);
188 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
189 form DOMAIN/user into a domain and a user */
191 static BOOL
parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
195 char *p
= strchr(domuser
,winbind_separator());
202 fstrcpy(domain
, domuser
);
203 domain
[PTR_DIFF(p
, domuser
)] = 0;
209 static BOOL
get_require_membership_sid(void) {
210 struct winbindd_request request
;
211 struct winbindd_response response
;
213 if (!require_membership_of
) {
217 if (require_membership_sid
) {
221 /* Otherwise, ask winbindd for the name->sid request */
223 ZERO_STRUCT(request
);
224 ZERO_STRUCT(response
);
226 if (!parse_ntlm_auth_domain_user(require_membership_of
,
227 request
.data
.name
.dom_name
,
228 request
.data
.name
.name
)) {
229 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
230 require_membership_of
));
234 if (winbindd_request(WINBINDD_LOOKUPNAME
, &request
, &response
) !=
235 NSS_STATUS_SUCCESS
) {
236 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
237 require_membership_of
));
241 require_membership_sid
= strdup(response
.data
.sid
.sid
);
243 if (require_membership_sid
)
248 /* Authenticate a user with a plaintext password */
250 static BOOL
check_plaintext_auth(const char *user
, const char *pass
,
251 BOOL stdout_diagnostics
)
253 struct winbindd_request request
;
254 struct winbindd_response response
;
257 if (!get_require_membership_sid()) {
261 /* Send off request */
263 ZERO_STRUCT(request
);
264 ZERO_STRUCT(response
);
266 fstrcpy(request
.data
.auth
.user
, user
);
267 fstrcpy(request
.data
.auth
.pass
, pass
);
268 if (require_membership_sid
)
269 fstrcpy(request
.data
.auth
.required_membership_sid
, require_membership_sid
);
271 result
= winbindd_request(WINBINDD_PAM_AUTH
, &request
, &response
);
273 /* Display response */
275 if (stdout_diagnostics
) {
276 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
277 d_printf("Reading winbind reply failed! (0x01)\n");
280 d_printf("%s: %s (0x%x)\n",
281 response
.data
.auth
.nt_status_string
,
282 response
.data
.auth
.error_string
,
283 response
.data
.auth
.nt_status
);
285 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
286 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
289 DEBUG(3, ("%s: %s (0x%x)\n",
290 response
.data
.auth
.nt_status_string
,
291 response
.data
.auth
.error_string
,
292 response
.data
.auth
.nt_status
));
295 return (result
== NSS_STATUS_SUCCESS
);
298 /* authenticate a user with an encrypted username/password */
300 NTSTATUS
contact_winbind_auth_crap(const char *username
,
302 const char *workstation
,
303 const DATA_BLOB
*challenge
,
304 const DATA_BLOB
*lm_response
,
305 const DATA_BLOB
*nt_response
,
308 uint8 user_session_key
[16],
314 struct winbindd_request request
;
315 struct winbindd_response response
;
317 if (!get_require_membership_sid()) {
318 return NT_STATUS_INVALID_PARAMETER
;
321 ZERO_STRUCT(request
);
322 ZERO_STRUCT(response
);
324 request
.flags
= flags
;
326 if (require_membership_sid
)
327 fstrcpy(request
.data
.auth_crap
.required_membership_sid
, require_membership_sid
);
329 if (push_utf8_fstring(request
.data
.auth_crap
.user
, username
) == -1) {
330 *error_string
= smb_xstrdup(
331 "unable to create utf8 string for username");
332 return NT_STATUS_UNSUCCESSFUL
;
335 if (push_utf8_fstring(request
.data
.auth_crap
.domain
, domain
) == -1) {
336 *error_string
= smb_xstrdup(
337 "unable to create utf8 string for domain");
338 return NT_STATUS_UNSUCCESSFUL
;
341 if (push_utf8_fstring(request
.data
.auth_crap
.workstation
,
342 workstation
) == -1) {
343 *error_string
= smb_xstrdup(
344 "unable to create utf8 string for workstation");
345 return NT_STATUS_UNSUCCESSFUL
;
348 memcpy(request
.data
.auth_crap
.chal
, challenge
->data
, MIN(challenge
->length
, 8));
350 if (lm_response
&& lm_response
->length
) {
351 memcpy(request
.data
.auth_crap
.lm_resp
,
353 MIN(lm_response
->length
, sizeof(request
.data
.auth_crap
.lm_resp
)));
354 request
.data
.auth_crap
.lm_resp_len
= lm_response
->length
;
357 if (nt_response
&& nt_response
->length
) {
358 memcpy(request
.data
.auth_crap
.nt_resp
,
360 MIN(nt_response
->length
, sizeof(request
.data
.auth_crap
.nt_resp
)));
361 request
.data
.auth_crap
.nt_resp_len
= nt_response
->length
;
364 result
= winbindd_request(WINBINDD_PAM_AUTH_CRAP
, &request
, &response
);
366 /* Display response */
368 if ((result
!= NSS_STATUS_SUCCESS
) && (response
.data
.auth
.nt_status
== 0)) {
369 nt_status
= NT_STATUS_UNSUCCESSFUL
;
371 *error_string
= smb_xstrdup("Reading winbind reply failed!");
372 free_response(&response
);
376 nt_status
= (NT_STATUS(response
.data
.auth
.nt_status
));
377 if (!NT_STATUS_IS_OK(nt_status
)) {
379 *error_string
= smb_xstrdup(response
.data
.auth
.error_string
);
380 free_response(&response
);
384 if ((flags
& WBFLAG_PAM_LMKEY
) && lm_key
) {
385 memcpy(lm_key
, response
.data
.auth
.first_8_lm_hash
,
386 sizeof(response
.data
.auth
.first_8_lm_hash
));
388 if ((flags
& WBFLAG_PAM_USER_SESSION_KEY
) && user_session_key
) {
389 memcpy(user_session_key
, response
.data
.auth
.user_session_key
,
390 sizeof(response
.data
.auth
.user_session_key
));
393 if (flags
& WBFLAG_PAM_UNIX_NAME
) {
394 if (pull_utf8_allocate(unix_name
, (char *)response
.extra_data
) == -1) {
395 free_response(&response
);
396 return NT_STATUS_NO_MEMORY
;
400 free_response(&response
);
404 static NTSTATUS
winbind_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
406 static const char zeros
[16];
410 uint8 user_sess_key
[16];
413 nt_status
= contact_winbind_auth_crap(ntlmssp_state
->user
, ntlmssp_state
->domain
,
414 ntlmssp_state
->workstation
,
415 &ntlmssp_state
->chal
,
416 &ntlmssp_state
->lm_resp
,
417 &ntlmssp_state
->nt_resp
,
418 WBFLAG_PAM_LMKEY
| WBFLAG_PAM_USER_SESSION_KEY
| WBFLAG_PAM_UNIX_NAME
,
419 lm_key
, user_sess_key
,
420 &error_string
, &unix_name
);
422 if (NT_STATUS_IS_OK(nt_status
)) {
423 if (memcmp(lm_key
, zeros
, 8) != 0) {
424 *lm_session_key
= data_blob(NULL
, 16);
425 memcpy(lm_session_key
->data
, lm_key
, 8);
426 memset(lm_session_key
->data
+8, '\0', 8);
429 if (memcmp(user_sess_key
, zeros
, 16) != 0) {
430 *user_session_key
= data_blob(user_sess_key
, 16);
432 ntlmssp_state
->auth_context
= talloc_strdup(ntlmssp_state
->mem_ctx
, unix_name
);
433 SAFE_FREE(unix_name
);
435 DEBUG(NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
) ? 0 : 3,
436 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
437 ntlmssp_state
->domain
, ntlmssp_state
->user
,
438 ntlmssp_state
->workstation
,
439 error_string
? error_string
: "unknown error (NULL)"));
440 ntlmssp_state
->auth_context
= NULL
;
445 static NTSTATUS
local_pw_check(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*user_session_key
, DATA_BLOB
*lm_session_key
)
448 uint8 lm_pw
[16], nt_pw
[16];
450 nt_lm_owf_gen (opt_password
, nt_pw
, lm_pw
);
452 nt_status
= ntlm_password_check(ntlmssp_state
->mem_ctx
,
453 &ntlmssp_state
->chal
,
454 &ntlmssp_state
->lm_resp
,
455 &ntlmssp_state
->nt_resp
,
459 ntlmssp_state
->domain
,
460 lm_pw
, nt_pw
, user_session_key
, lm_session_key
);
462 if (NT_STATUS_IS_OK(nt_status
)) {
463 ntlmssp_state
->auth_context
= talloc_asprintf(ntlmssp_state
->mem_ctx
,
464 "%s%c%s", ntlmssp_state
->domain
,
465 *lp_winbind_separator(),
466 ntlmssp_state
->user
);
468 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
469 ntlmssp_state
->domain
, ntlmssp_state
->user
, ntlmssp_state
->workstation
,
470 nt_errstr(nt_status
)));
471 ntlmssp_state
->auth_context
= NULL
;
476 static NTSTATUS
ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE
**client_ntlmssp_state
)
479 if ( (opt_username
== NULL
) || (opt_domain
== NULL
) ) {
480 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
484 status
= ntlmssp_client_start(client_ntlmssp_state
);
486 if (!NT_STATUS_IS_OK(status
)) {
487 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
489 ntlmssp_end(client_ntlmssp_state
);
493 status
= ntlmssp_set_username(*client_ntlmssp_state
, opt_username
);
495 if (!NT_STATUS_IS_OK(status
)) {
496 DEBUG(1, ("Could not set username: %s\n",
498 ntlmssp_end(client_ntlmssp_state
);
502 status
= ntlmssp_set_domain(*client_ntlmssp_state
, opt_domain
);
504 if (!NT_STATUS_IS_OK(status
)) {
505 DEBUG(1, ("Could not set domain: %s\n",
507 ntlmssp_end(client_ntlmssp_state
);
511 status
= ntlmssp_set_password(*client_ntlmssp_state
, opt_password
);
513 if (!NT_STATUS_IS_OK(status
)) {
514 DEBUG(1, ("Could not set password: %s\n",
516 ntlmssp_end(client_ntlmssp_state
);
522 static NTSTATUS
ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE
**ntlmssp_state
)
524 NTSTATUS status
= ntlmssp_server_start(ntlmssp_state
);
526 if (!NT_STATUS_IS_OK(status
)) {
527 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
532 /* Have we been given a local password, or should we ask winbind? */
534 (*ntlmssp_state
)->check_password
= local_pw_check
;
535 (*ntlmssp_state
)->get_domain
= lp_workgroup
;
536 (*ntlmssp_state
)->get_global_myname
= global_myname
;
538 (*ntlmssp_state
)->check_password
= winbind_pw_check
;
539 (*ntlmssp_state
)->get_domain
= get_winbind_domain
;
540 (*ntlmssp_state
)->get_global_myname
= get_winbind_netbios_name
;
545 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode
,
546 char *buf
, int length
)
548 static NTLMSSP_STATE
*ntlmssp_state
= NULL
;
549 DATA_BLOB request
, reply
;
552 if (strlen(buf
) < 2) {
553 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
554 x_fprintf(x_stdout
, "BH\n");
558 if (strlen(buf
) > 3) {
559 request
= base64_decode_data_blob(buf
+ 3);
561 request
= data_blob(NULL
, 0);
564 if ((strncmp(buf
, "PW ", 3) == 0)) {
565 /* The calling application wants us to use a local password (rather than winbindd) */
567 opt_password
= strndup((const char *)request
.data
, request
.length
);
569 if (opt_password
== NULL
) {
570 DEBUG(1, ("Out of memory\n"));
571 x_fprintf(x_stdout
, "BH\n");
572 data_blob_free(&request
);
576 x_fprintf(x_stdout
, "OK\n");
577 data_blob_free(&request
);
581 if (strncmp(buf
, "YR", 2) == 0) {
583 ntlmssp_end(&ntlmssp_state
);
584 } else if (strncmp(buf
, "KK", 2) == 0) {
587 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
588 x_fprintf(x_stdout
, "BH\n");
592 if (!ntlmssp_state
) {
593 if (!NT_STATUS_IS_OK(nt_status
= ntlm_auth_start_ntlmssp_server(&ntlmssp_state
))) {
594 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
599 DEBUG(10, ("got NTLMSSP packet:\n"));
600 dump_data(10, (const char *)request
.data
, request
.length
);
602 nt_status
= ntlmssp_update(ntlmssp_state
, request
, &reply
);
604 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
605 char *reply_base64
= base64_encode_data_blob(reply
);
606 x_fprintf(x_stdout
, "TT %s\n", reply_base64
);
607 SAFE_FREE(reply_base64
);
608 data_blob_free(&reply
);
609 DEBUG(10, ("NTLMSSP challenge\n"));
610 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
611 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
612 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
614 ntlmssp_end(&ntlmssp_state
);
615 } else if (!NT_STATUS_IS_OK(nt_status
)) {
616 x_fprintf(x_stdout
, "NA %s\n", nt_errstr(nt_status
));
617 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status
)));
619 x_fprintf(x_stdout
, "AF %s\n", (char *)ntlmssp_state
->auth_context
);
620 DEBUG(10, ("NTLMSSP OK!\n"));
623 data_blob_free(&request
);
626 static void manage_client_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode
,
627 char *buf
, int length
)
629 static NTLMSSP_STATE
*ntlmssp_state
= NULL
;
630 DATA_BLOB request
, reply
;
634 if (strlen(buf
) < 2) {
635 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
636 x_fprintf(x_stdout
, "BH\n");
640 if (strlen(buf
) > 3) {
641 request
= base64_decode_data_blob(buf
+ 3);
643 request
= data_blob(NULL
, 0);
646 if (strncmp(buf
, "PW ", 3) == 0) {
647 /* We asked for a password and obviously got it :-) */
649 opt_password
= strndup((const char *)request
.data
, request
.length
);
651 if (opt_password
== NULL
) {
652 DEBUG(1, ("Out of memory\n"));
653 x_fprintf(x_stdout
, "BH\n");
654 data_blob_free(&request
);
658 x_fprintf(x_stdout
, "OK\n");
659 data_blob_free(&request
);
663 if (opt_password
== NULL
) {
665 /* Request a password from the calling process. After
666 sending it, the calling process should retry asking for the negotiate. */
668 DEBUG(10, ("Requesting password\n"));
669 x_fprintf(x_stdout
, "PW\n");
673 if (strncmp(buf
, "YR", 2) == 0) {
675 ntlmssp_end(&ntlmssp_state
);
676 } else if (strncmp(buf
, "TT", 2) == 0) {
679 DEBUG(1, ("NTLMSSP query [%s] invalid", buf
));
680 x_fprintf(x_stdout
, "BH\n");
684 if (!ntlmssp_state
) {
685 if (!NT_STATUS_IS_OK(nt_status
= ntlm_auth_start_ntlmssp_client(&ntlmssp_state
))) {
686 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
692 DEBUG(10, ("got NTLMSSP packet:\n"));
693 dump_data(10, (const char *)request
.data
, request
.length
);
695 nt_status
= ntlmssp_update(ntlmssp_state
, request
, &reply
);
697 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
698 char *reply_base64
= base64_encode_data_blob(reply
);
700 x_fprintf(x_stdout
, "YR %s\n", reply_base64
);
702 x_fprintf(x_stdout
, "KK %s\n", reply_base64
);
704 SAFE_FREE(reply_base64
);
705 data_blob_free(&reply
);
706 DEBUG(10, ("NTLMSSP challenge\n"));
707 } else if (NT_STATUS_IS_OK(nt_status
)) {
708 x_fprintf(x_stdout
, "AF\n");
709 DEBUG(10, ("NTLMSSP OK!\n"));
711 ntlmssp_end(&ntlmssp_state
);
713 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(nt_status
));
714 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status
)));
716 ntlmssp_end(&ntlmssp_state
);
719 data_blob_free(&request
);
722 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode
,
723 char *buf
, int length
)
728 pass
=memchr(buf
,' ',length
);
730 DEBUG(2, ("Password not found. Denying access\n"));
731 x_fprintf(x_stdout
, "ERR\n");
737 if (stdio_helper_mode
== SQUID_2_5_BASIC
) {
738 rfc1738_unescape(user
);
739 rfc1738_unescape(pass
);
742 if (check_plaintext_auth(user
, pass
, False
)) {
743 x_fprintf(x_stdout
, "OK\n");
745 x_fprintf(x_stdout
, "ERR\n");
749 static void offer_gss_spnego_mechs(void) {
757 pstring myname_lower
;
761 pstrcpy(myname_lower
, global_myname());
762 strlower_m(myname_lower
);
764 pstr_sprintf(principal
, "%s$@%s", myname_lower
, lp_realm());
766 /* Server negTokenInit (mech offerings) */
767 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
768 spnego
.negTokenInit
.mechTypes
= smb_xmalloc(sizeof(char *) * 3);
770 spnego
.negTokenInit
.mechTypes
[0] = smb_xstrdup(OID_KERBEROS5_OLD
);
771 spnego
.negTokenInit
.mechTypes
[1] = smb_xstrdup(OID_NTLMSSP
);
772 spnego
.negTokenInit
.mechTypes
[2] = NULL
;
774 spnego
.negTokenInit
.mechTypes
[0] = smb_xstrdup(OID_NTLMSSP
);
775 spnego
.negTokenInit
.mechTypes
[1] = NULL
;
779 spnego
.negTokenInit
.mechListMIC
= data_blob(principal
,
782 len
= write_spnego_data(&token
, &spnego
);
783 free_spnego_data(&spnego
);
786 DEBUG(1, ("Could not write SPNEGO data blob\n"));
787 x_fprintf(x_stdout
, "BH\n");
791 reply_base64
= base64_encode_data_blob(token
);
792 x_fprintf(x_stdout
, "TT %s *\n", reply_base64
);
794 SAFE_FREE(reply_base64
);
795 data_blob_free(&token
);
796 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
800 static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode
,
801 char *buf
, int length
)
803 static NTLMSSP_STATE
*ntlmssp_state
= NULL
;
804 SPNEGO_DATA request
, response
;
812 const char *reply_code
;
814 pstring reply_argument
;
816 if (strlen(buf
) < 2) {
817 DEBUG(1, ("SPENGO query [%s] invalid", buf
));
818 x_fprintf(x_stdout
, "BH\n");
822 if (strncmp(buf
, "YR", 2) == 0) {
824 ntlmssp_end(&ntlmssp_state
);
825 } else if (strncmp(buf
, "KK", 2) == 0) {
828 DEBUG(1, ("SPENGO query [%s] invalid", buf
));
829 x_fprintf(x_stdout
, "BH\n");
833 if ( (strlen(buf
) == 2)) {
835 /* no client data, get the negTokenInit offering
838 offer_gss_spnego_mechs();
842 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
844 if (strlen(buf
) <= 3) {
845 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf
));
846 x_fprintf(x_stdout
, "BH\n");
850 token
= base64_decode_data_blob(buf
+ 3);
851 len
= read_spnego_data(token
, &request
);
852 data_blob_free(&token
);
855 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf
));
856 x_fprintf(x_stdout
, "BH\n");
860 if (request
.type
== SPNEGO_NEG_TOKEN_INIT
) {
862 /* Second request from Client. This is where the
863 client offers its mechanism to use. */
865 if ( (request
.negTokenInit
.mechTypes
== NULL
) ||
866 (request
.negTokenInit
.mechTypes
[0] == NULL
) ) {
867 DEBUG(1, ("Client did not offer any mechanism"));
868 x_fprintf(x_stdout
, "BH\n");
872 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_NTLMSSP
) == 0) {
874 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
875 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
876 x_fprintf(x_stdout
, "BH\n");
880 if ( ntlmssp_state
!= NULL
) {
881 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
882 "already got one\n"));
883 x_fprintf(x_stdout
, "BH\n");
884 ntlmssp_end(&ntlmssp_state
);
888 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_server(&ntlmssp_state
))) {
889 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
893 DEBUG(10, ("got NTLMSSP packet:\n"));
894 dump_data(10, (const char *)request
.negTokenInit
.mechToken
.data
,
895 request
.negTokenInit
.mechToken
.length
);
897 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
898 response
.negTokenTarg
.supportedMech
= strdup(OID_NTLMSSP
);
899 response
.negTokenTarg
.mechListMIC
= data_blob(NULL
, 0);
901 status
= ntlmssp_update(ntlmssp_state
,
902 request
.negTokenInit
.mechToken
,
903 &response
.negTokenTarg
.responseToken
);
907 if (strcmp(request
.negTokenInit
.mechTypes
[0], OID_KERBEROS5_OLD
) == 0) {
912 DATA_BLOB session_key
;
914 if ( request
.negTokenInit
.mechToken
.data
== NULL
) {
915 DEBUG(1, ("Client did not provide Kerberos data\n"));
916 x_fprintf(x_stdout
, "BH\n");
920 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
921 response
.negTokenTarg
.supportedMech
= strdup(OID_KERBEROS5_OLD
);
922 response
.negTokenTarg
.mechListMIC
= data_blob(NULL
, 0);
923 response
.negTokenTarg
.responseToken
= data_blob(NULL
, 0);
925 status
= ads_verify_ticket(lp_realm(),
926 &request
.negTokenInit
.mechToken
,
927 &principal
, &auth_data
, &ap_rep
,
930 /* Now in "principal" we have the name we are
933 if (NT_STATUS_IS_OK(status
)) {
935 domain
= strchr(principal
, '@');
937 if (domain
== NULL
) {
938 DEBUG(1, ("Did not get a valid principal "
939 "from ads_verify_ticket\n"));
940 x_fprintf(x_stdout
, "BH\n");
945 domain
= strdup(domain
);
946 user
= strdup(principal
);
948 data_blob_free(&ap_rep
);
949 data_blob_free(&auth_data
);
951 SAFE_FREE(principal
);
958 if ( (request
.negTokenTarg
.supportedMech
== NULL
) ||
959 ( strcmp(request
.negTokenTarg
.supportedMech
, OID_NTLMSSP
) != 0 ) ) {
960 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
961 is the only one we support that sends this stuff */
962 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
963 request
.negTokenTarg
.supportedMech
));
964 x_fprintf(x_stdout
, "BH\n");
968 if (request
.negTokenTarg
.responseToken
.data
== NULL
) {
969 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
970 x_fprintf(x_stdout
, "BH\n");
974 status
= ntlmssp_update(ntlmssp_state
,
975 request
.negTokenTarg
.responseToken
,
976 &response
.negTokenTarg
.responseToken
);
978 response
.type
= SPNEGO_NEG_TOKEN_TARG
;
979 response
.negTokenTarg
.supportedMech
= strdup(OID_NTLMSSP
);
980 response
.negTokenTarg
.mechListMIC
= data_blob(NULL
, 0);
982 if (NT_STATUS_IS_OK(status
)) {
983 user
= strdup(ntlmssp_state
->user
);
984 domain
= strdup(ntlmssp_state
->domain
);
985 ntlmssp_end(&ntlmssp_state
);
989 free_spnego_data(&request
);
991 if (NT_STATUS_IS_OK(status
)) {
992 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_COMPLETED
;
994 pstr_sprintf(reply_argument
, "%s\\%s", domain
, user
);
995 } else if (NT_STATUS_EQUAL(status
,
996 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
997 response
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
999 pstr_sprintf(reply_argument
, "*");
1001 response
.negTokenTarg
.negResult
= SPNEGO_REJECT
;
1003 pstrcpy(reply_argument
, nt_errstr(status
));
1009 len
= write_spnego_data(&token
, &response
);
1010 free_spnego_data(&response
);
1013 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1014 x_fprintf(x_stdout
, "BH\n");
1018 reply_base64
= base64_encode_data_blob(token
);
1020 x_fprintf(x_stdout
, "%s %s %s\n",
1021 reply_code
, reply_base64
, reply_argument
);
1023 SAFE_FREE(reply_base64
);
1024 data_blob_free(&token
);
1029 static NTLMSSP_STATE
*client_ntlmssp_state
= NULL
;
1031 static BOOL
manage_client_ntlmssp_init(SPNEGO_DATA spnego
)
1034 DATA_BLOB null_blob
= data_blob(NULL
, 0);
1035 DATA_BLOB to_server
;
1036 char *to_server_base64
;
1037 const char *my_mechs
[] = {OID_NTLMSSP
, NULL
};
1039 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1041 if (client_ntlmssp_state
!= NULL
) {
1042 DEBUG(1, ("Request for initial SPNEGO request where "
1043 "we already have a state\n"));
1047 if (!client_ntlmssp_state
) {
1048 if (!NT_STATUS_IS_OK(status
= ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state
))) {
1049 x_fprintf(x_stdout
, "BH %s\n", nt_errstr(status
));
1055 if (opt_password
== NULL
) {
1057 /* Request a password from the calling process. After
1058 sending it, the calling process should retry with
1059 the negTokenInit. */
1061 DEBUG(10, ("Requesting password\n"));
1062 x_fprintf(x_stdout
, "PW\n");
1066 spnego
.type
= SPNEGO_NEG_TOKEN_INIT
;
1067 spnego
.negTokenInit
.mechTypes
= my_mechs
;
1068 spnego
.negTokenInit
.reqFlags
= 0;
1069 spnego
.negTokenInit
.mechListMIC
= null_blob
;
1071 status
= ntlmssp_update(client_ntlmssp_state
, null_blob
,
1072 &spnego
.negTokenInit
.mechToken
);
1074 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1075 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
1076 nt_errstr(status
)));
1077 ntlmssp_end(&client_ntlmssp_state
);
1081 write_spnego_data(&to_server
, &spnego
);
1082 data_blob_free(&spnego
.negTokenInit
.mechToken
);
1084 to_server_base64
= base64_encode_data_blob(to_server
);
1085 data_blob_free(&to_server
);
1086 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1087 SAFE_FREE(to_server_base64
);
1091 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego
)
1094 DATA_BLOB null_blob
= data_blob(NULL
, 0);
1096 DATA_BLOB to_server
;
1097 char *to_server_base64
;
1099 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1101 if (client_ntlmssp_state
== NULL
) {
1102 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1103 x_fprintf(x_stdout
, "BH\n");
1104 ntlmssp_end(&client_ntlmssp_state
);
1108 if (spnego
.negTokenTarg
.negResult
== SPNEGO_REJECT
) {
1109 x_fprintf(x_stdout
, "NA\n");
1110 ntlmssp_end(&client_ntlmssp_state
);
1114 if (spnego
.negTokenTarg
.negResult
== SPNEGO_ACCEPT_COMPLETED
) {
1115 x_fprintf(x_stdout
, "AF\n");
1116 ntlmssp_end(&client_ntlmssp_state
);
1120 status
= ntlmssp_update(client_ntlmssp_state
,
1121 spnego
.negTokenTarg
.responseToken
,
1124 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1125 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1126 "ntlmssp_client_update, got: %s\n",
1127 nt_errstr(status
)));
1128 x_fprintf(x_stdout
, "BH\n");
1129 data_blob_free(&request
);
1130 ntlmssp_end(&client_ntlmssp_state
);
1134 spnego
.type
= SPNEGO_NEG_TOKEN_TARG
;
1135 spnego
.negTokenTarg
.negResult
= SPNEGO_ACCEPT_INCOMPLETE
;
1136 spnego
.negTokenTarg
.supportedMech
= OID_NTLMSSP
;
1137 spnego
.negTokenTarg
.responseToken
= request
;
1138 spnego
.negTokenTarg
.mechListMIC
= null_blob
;
1140 write_spnego_data(&to_server
, &spnego
);
1141 data_blob_free(&request
);
1143 to_server_base64
= base64_encode_data_blob(to_server
);
1144 data_blob_free(&to_server
);
1145 x_fprintf(x_stdout
, "KK %s\n", to_server_base64
);
1146 SAFE_FREE(to_server_base64
);
1152 static BOOL
manage_client_krb5_init(SPNEGO_DATA spnego
)
1155 DATA_BLOB tkt
, to_server
;
1156 DATA_BLOB session_key_krb5
= data_blob(NULL
, 0);
1161 const char *my_mechs
[] = {OID_KERBEROS5_OLD
, NULL
};
1164 if ( (spnego
.negTokenInit
.mechListMIC
.data
== NULL
) ||
1165 (spnego
.negTokenInit
.mechListMIC
.length
== 0) ) {
1166 DEBUG(1, ("Did not get a principal for krb5\n"));
1170 principal
= malloc(spnego
.negTokenInit
.mechListMIC
.length
+1);
1172 if (principal
== NULL
) {
1173 DEBUG(1, ("Could not malloc principal\n"));
1177 memcpy(principal
, spnego
.negTokenInit
.mechListMIC
.data
,
1178 spnego
.negTokenInit
.mechListMIC
.length
);
1179 principal
[spnego
.negTokenInit
.mechListMIC
.length
] = '\0';
1181 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
);
1187 /* Let's try to first get the TGT, for that we need a
1190 if (opt_password
== NULL
) {
1191 DEBUG(10, ("Requesting password\n"));
1192 x_fprintf(x_stdout
, "PW\n");
1196 pstr_sprintf(user
, "%s@%s", opt_username
, opt_domain
);
1198 if ((retval
= kerberos_kinit_password(user
, opt_password
,
1200 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval
)));
1204 retval
= cli_krb5_get_ticket(principal
, 0, &tkt
, &session_key_krb5
);
1207 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval
)));
1212 data_blob_free(&session_key_krb5
);
1216 reply
.type
= SPNEGO_NEG_TOKEN_INIT
;
1217 reply
.negTokenInit
.mechTypes
= my_mechs
;
1218 reply
.negTokenInit
.reqFlags
= 0;
1219 reply
.negTokenInit
.mechToken
= tkt
;
1220 reply
.negTokenInit
.mechListMIC
= data_blob(NULL
, 0);
1222 len
= write_spnego_data(&to_server
, &reply
);
1223 data_blob_free(&tkt
);
1226 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1230 reply_base64
= base64_encode_data_blob(to_server
);
1231 x_fprintf(x_stdout
, "KK %s *\n", reply_base64
);
1233 SAFE_FREE(reply_base64
);
1234 data_blob_free(&to_server
);
1235 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1239 static void manage_client_krb5_targ(SPNEGO_DATA spnego
)
1241 switch (spnego
.negTokenTarg
.negResult
) {
1242 case SPNEGO_ACCEPT_INCOMPLETE
:
1243 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1244 x_fprintf(x_stdout
, "BH\n");
1246 case SPNEGO_ACCEPT_COMPLETED
:
1247 DEBUG(10, ("Accept completed\n"));
1248 x_fprintf(x_stdout
, "AF\n");
1251 DEBUG(10, ("Rejected\n"));
1252 x_fprintf(x_stdout
, "NA\n");
1255 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1256 x_fprintf(x_stdout
, "AF\n");
1262 static void manage_gss_spnego_client_request(enum stdio_helper_mode stdio_helper_mode
,
1263 char *buf
, int length
)
1269 if (strlen(buf
) <= 3) {
1270 DEBUG(1, ("SPNEGO query [%s] too short\n", buf
));
1271 x_fprintf(x_stdout
, "BH\n");
1275 request
= base64_decode_data_blob(buf
+3);
1277 if (strncmp(buf
, "PW ", 3) == 0) {
1279 /* We asked for a password and obviously got it :-) */
1281 opt_password
= strndup((const char *)request
.data
, request
.length
);
1283 if (opt_password
== NULL
) {
1284 DEBUG(1, ("Out of memory\n"));
1285 x_fprintf(x_stdout
, "BH\n");
1286 data_blob_free(&request
);
1290 x_fprintf(x_stdout
, "OK\n");
1291 data_blob_free(&request
);
1295 if ( (strncmp(buf
, "TT ", 3) != 0) &&
1296 (strncmp(buf
, "AF ", 3) != 0) &&
1297 (strncmp(buf
, "NA ", 3) != 0) ) {
1298 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
1299 x_fprintf(x_stdout
, "BH\n");
1300 data_blob_free(&request
);
1304 /* So we got a server challenge to generate a SPNEGO
1305 client-to-server request... */
1307 len
= read_spnego_data(request
, &spnego
);
1308 data_blob_free(&request
);
1311 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf
));
1312 x_fprintf(x_stdout
, "BH\n");
1316 if (spnego
.type
== SPNEGO_NEG_TOKEN_INIT
) {
1318 /* The server offers a list of mechanisms */
1320 const char **mechType
= spnego
.negTokenInit
.mechTypes
;
1322 while (*mechType
!= NULL
) {
1325 if ( (strcmp(*mechType
, OID_KERBEROS5_OLD
) == 0) ||
1326 (strcmp(*mechType
, OID_KERBEROS5
) == 0) ) {
1327 if (manage_client_krb5_init(spnego
))
1332 if (strcmp(*mechType
, OID_NTLMSSP
) == 0) {
1333 if (manage_client_ntlmssp_init(spnego
))
1340 DEBUG(1, ("Server offered no compatible mechanism\n"));
1341 x_fprintf(x_stdout
, "BH\n");
1345 if (spnego
.type
== SPNEGO_NEG_TOKEN_TARG
) {
1347 if (spnego
.negTokenTarg
.supportedMech
== NULL
) {
1348 /* On accept/reject Windows does not send the
1349 mechanism anymore. Handle that here and
1350 shut down the mechanisms. */
1352 switch (spnego
.negTokenTarg
.negResult
) {
1353 case SPNEGO_ACCEPT_COMPLETED
:
1354 x_fprintf(x_stdout
, "AF\n");
1357 x_fprintf(x_stdout
, "NA\n");
1360 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1361 "unknown negResult: %d\n",
1362 spnego
.negTokenTarg
.negResult
));
1363 x_fprintf(x_stdout
, "BH\n");
1366 ntlmssp_end(&client_ntlmssp_state
);
1370 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1371 OID_NTLMSSP
) == 0) {
1372 manage_client_ntlmssp_targ(spnego
);
1377 if (strcmp(spnego
.negTokenTarg
.supportedMech
,
1378 OID_KERBEROS5_OLD
) == 0) {
1379 manage_client_krb5_targ(spnego
);
1386 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf
));
1387 x_fprintf(x_stdout
, "BH\n");
1391 free_spnego_data(&spnego
);
1395 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode
,
1396 char *buf
, int length
)
1398 char *request
, *parameter
;
1399 static DATA_BLOB challenge
;
1400 static DATA_BLOB lm_response
;
1401 static DATA_BLOB nt_response
;
1402 static char *full_username
;
1403 static char *username
;
1404 static char *domain
;
1405 static char *plaintext_password
;
1406 static BOOL ntlm_server_1_user_session_key
;
1407 static BOOL ntlm_server_1_lm_session_key
;
1409 if (strequal(buf
, ".")) {
1410 if (!full_username
&& !username
) {
1411 x_fprintf(x_stdout
, "Error: No username supplied!\n");
1412 } else if (plaintext_password
) {
1413 /* handle this request as plaintext */
1414 if (!full_username
) {
1415 if (asprintf(&full_username
, "%s%c%s", domain
, winbind_separator(), username
) == -1) {
1416 x_fprintf(x_stdout
, "Error: Out of memory in asprintf!\n.\n");
1420 if (check_plaintext_auth(full_username
, plaintext_password
, False
)) {
1421 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1423 x_fprintf(x_stdout
, "Authenticated: No\n");
1425 } else if (!lm_response
.data
&& !nt_response
.data
) {
1426 x_fprintf(x_stdout
, "Error: No password supplied!\n");
1427 } else if (!challenge
.data
) {
1428 x_fprintf(x_stdout
, "Error: No lanman-challenge supplied!\n");
1430 char *error_string
= NULL
;
1432 uchar user_session_key
[16];
1435 if (full_username
&& !username
) {
1437 fstring fstr_domain
;
1439 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
)) {
1440 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1441 x_fprintf(x_stdout
, "Error: Could not parse into domain and username\n");
1443 SAFE_FREE(username
);
1445 username
= smb_xstrdup(fstr_user
);
1446 domain
= smb_xstrdup(fstr_domain
);
1450 domain
= smb_xstrdup(get_winbind_domain());
1453 if (ntlm_server_1_lm_session_key
)
1454 flags
|= WBFLAG_PAM_LMKEY
;
1456 if (ntlm_server_1_user_session_key
)
1457 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
1459 if (!NT_STATUS_IS_OK(
1460 contact_winbind_auth_crap(username
,
1472 x_fprintf(x_stdout
, "Authenticated: No\n");
1473 x_fprintf(x_stdout
, "Authentication-Error: %s\n.\n", error_string
);
1474 SAFE_FREE(error_string
);
1476 static char zeros
[16];
1478 char *hex_user_session_key
;
1480 x_fprintf(x_stdout
, "Authenticated: Yes\n");
1482 if (ntlm_server_1_lm_session_key
1483 && (memcmp(zeros
, lm_key
,
1484 sizeof(lm_key
)) != 0)) {
1485 hex_encode((const unsigned char *)lm_key
,
1488 x_fprintf(x_stdout
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
1489 SAFE_FREE(hex_lm_key
);
1492 if (ntlm_server_1_user_session_key
1493 && (memcmp(zeros
, user_session_key
,
1494 sizeof(user_session_key
)) != 0)) {
1495 hex_encode((const unsigned char *)user_session_key
,
1496 sizeof(user_session_key
),
1497 &hex_user_session_key
);
1498 x_fprintf(x_stdout
, "User-Session-Key: %s\n", hex_user_session_key
);
1499 SAFE_FREE(hex_user_session_key
);
1503 /* clear out the state */
1504 challenge
= data_blob(NULL
, 0);
1505 nt_response
= data_blob(NULL
, 0);
1506 lm_response
= data_blob(NULL
, 0);
1507 SAFE_FREE(full_username
);
1508 SAFE_FREE(username
);
1510 SAFE_FREE(plaintext_password
);
1511 ntlm_server_1_user_session_key
= False
;
1512 ntlm_server_1_lm_session_key
= False
;
1513 x_fprintf(x_stdout
, ".\n");
1520 /* Indicates a base64 encoded structure */
1521 parameter
= strstr_m(request
, ":: ");
1523 parameter
= strstr_m(request
, ": ");
1526 DEBUG(0, ("Parameter not found!\n"));
1527 x_fprintf(x_stdout
, "Error: Parameter not found!\n.\n");
1544 base64_decode_inplace(parameter
);
1547 if (strequal(request
, "LANMAN-Challenge")) {
1548 challenge
= strhex_to_data_blob(parameter
);
1549 if (challenge
.length
!= 8) {
1550 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1552 (int)challenge
.length
);
1553 challenge
= data_blob(NULL
, 0);
1555 } else if (strequal(request
, "NT-Response")) {
1556 nt_response
= strhex_to_data_blob(parameter
);
1557 if (nt_response
.length
< 24) {
1558 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1560 (int)nt_response
.length
);
1561 nt_response
= data_blob(NULL
, 0);
1563 } else if (strequal(request
, "LANMAN-Response")) {
1564 lm_response
= strhex_to_data_blob(parameter
);
1565 if (lm_response
.length
!= 24) {
1566 x_fprintf(x_stdout
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
1568 (int)lm_response
.length
);
1569 lm_response
= data_blob(NULL
, 0);
1571 } else if (strequal(request
, "Password")) {
1572 plaintext_password
= smb_xstrdup(parameter
);
1573 } else if (strequal(request
, "NT-Domain")) {
1574 domain
= smb_xstrdup(parameter
);
1575 } else if (strequal(request
, "Username")) {
1576 username
= smb_xstrdup(parameter
);
1577 } else if (strequal(request
, "Full-Username")) {
1578 full_username
= smb_xstrdup(parameter
);
1579 } else if (strequal(request
, "Request-User-Session-Key")) {
1580 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
1581 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
1582 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
1584 x_fprintf(x_stdout
, "Error: Unknown request %s\n.\n", request
);
1588 static void manage_squid_request(enum stdio_helper_mode helper_mode
, stdio_helper_function fn
)
1590 char buf
[SQUID_BUFFER_SIZE
+1];
1595 /* this is not a typo - x_fgets doesn't work too well under squid */
1596 if (fgets(buf
, sizeof(buf
)-1, stdin
) == NULL
) {
1597 if (ferror(stdin
)) {
1598 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin
),
1599 strerror(ferror(stdin
))));
1601 exit(1); /* BIIG buffer */
1606 c
=memchr(buf
,'\n',sizeof(buf
)-1);
1615 DEBUG(2, ("Oversized message\n"));
1616 x_fprintf(x_stderr
, "ERR\n");
1621 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
1623 if (buf
[0] == '\0') {
1624 DEBUG(2, ("Invalid Request\n"));
1625 x_fprintf(x_stderr
, "ERR\n");
1629 fn(helper_mode
, buf
, length
);
1633 static void squid_stream(enum stdio_helper_mode stdio_mode
, stdio_helper_function fn
) {
1634 /* initialize FDescs */
1635 x_setbuf(x_stdout
, NULL
);
1636 x_setbuf(x_stderr
, NULL
);
1638 manage_squid_request(stdio_mode
, fn
);
1643 /* Authenticate a user with a challenge/response */
1645 static BOOL
check_auth_crap(void)
1650 char user_session_key
[16];
1652 char *hex_user_session_key
;
1654 static uint8 zeros
[16];
1656 x_setbuf(x_stdout
, NULL
);
1659 flags
|= WBFLAG_PAM_LMKEY
;
1661 if (request_user_session_key
)
1662 flags
|= WBFLAG_PAM_USER_SESSION_KEY
;
1664 flags
|= WBFLAG_PAM_NT_STATUS_SQUASH
;
1666 nt_status
= contact_winbind_auth_crap(opt_username
, opt_domain
,
1672 (unsigned char *)lm_key
,
1673 (unsigned char *)user_session_key
,
1674 &error_string
, NULL
);
1676 if (!NT_STATUS_IS_OK(nt_status
)) {
1677 x_fprintf(x_stdout
, "%s (0x%x)\n",
1679 NT_STATUS_V(nt_status
));
1680 SAFE_FREE(error_string
);
1685 && (memcmp(zeros
, lm_key
,
1686 sizeof(lm_key
)) != 0)) {
1687 hex_encode((const unsigned char *)lm_key
,
1690 x_fprintf(x_stdout
, "LM_KEY: %s\n", hex_lm_key
);
1691 SAFE_FREE(hex_lm_key
);
1693 if (request_user_session_key
1694 && (memcmp(zeros
, user_session_key
,
1695 sizeof(user_session_key
)) != 0)) {
1696 hex_encode((const unsigned char *)user_session_key
,
1697 sizeof(user_session_key
),
1698 &hex_user_session_key
);
1699 x_fprintf(x_stdout
, "NT_KEY: %s\n", hex_user_session_key
);
1700 SAFE_FREE(hex_user_session_key
);
1709 OPT_USERNAME
= 1000,
1718 OPT_USER_SESSION_KEY
,
1720 OPT_REQUIRE_MEMBERSHIP
1723 int main(int argc
, const char **argv
)
1726 static const char *helper_protocol
;
1727 static int diagnostics
;
1729 static const char *hex_challenge
;
1730 static const char *hex_lm_response
;
1731 static const char *hex_nt_response
;
1735 /* NOTE: DO NOT change this interface without considering the implications!
1736 This is an external interface, which other programs will use to interact
1740 /* We do not use single-letter command abbreviations, because they harm future
1741 interface stability. */
1743 struct poptOption long_options
[] = {
1745 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
1746 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_USERNAME
, "username"},
1747 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
1748 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
1749 { "challenge", 0, POPT_ARG_STRING
, &hex_challenge
, OPT_CHALLENGE
, "challenge (HEX encoded)"},
1750 { "lm-response", 0, POPT_ARG_STRING
, &hex_lm_response
, OPT_LM
, "LM Response to the challenge (HEX encoded)"},
1751 { "nt-response", 0, POPT_ARG_STRING
, &hex_nt_response
, OPT_NT
, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1752 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
1753 { "request-lm-key", 0, POPT_ARG_NONE
, &request_lm_key
, OPT_LM_KEY
, "Retreive LM session key"},
1754 { "request-nt-key", 0, POPT_ARG_NONE
, &request_user_session_key
, OPT_USER_SESSION_KEY
, "Retreive User (NT) session key"},
1755 { "diagnostics", 0, POPT_ARG_NONE
, &diagnostics
, OPT_DIAGNOSTICS
, "Perform diagnostics on the authentictaion chain"},
1756 { "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" },
1761 /* Samba client initialisation */
1765 /* Samba client initialisation */
1767 if (!lp_load(dyn_CONFIGFILE
, True
, False
, False
)) {
1768 d_fprintf(stderr
, "wbinfo: error opening config file %s. Error was %s\n",
1769 dyn_CONFIGFILE
, strerror(errno
));
1775 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
1777 /* Parse command line options */
1780 poptPrintHelp(pc
, stderr
, 0);
1784 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
1785 POPT_CONTEXT_KEEP_FIRST
);
1787 while((opt
= poptGetNextOpt(pc
)) != -1) {
1790 opt_challenge
= strhex_to_data_blob(hex_challenge
);
1791 if (opt_challenge
.length
!= 8) {
1792 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
1794 (int)opt_challenge
.length
);
1799 opt_lm_response
= strhex_to_data_blob(hex_lm_response
);
1800 if (opt_lm_response
.length
!= 24) {
1801 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
1803 (int)opt_lm_response
.length
);
1809 opt_nt_response
= strhex_to_data_blob(hex_nt_response
);
1810 if (opt_nt_response
.length
< 24) {
1811 x_fprintf(x_stderr
, "hex decode of %s failed! (only got %d bytes)\n",
1813 (int)opt_nt_response
.length
);
1818 case OPT_REQUIRE_MEMBERSHIP
:
1819 if (StrnCaseCmp("S-", require_membership_of
, 2) == 0) {
1820 require_membership_sid
= require_membership_of
;
1826 if (helper_protocol
) {
1828 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
1829 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
1830 squid_stream(stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
1834 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
1836 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
1837 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
1843 if (!opt_username
) {
1844 x_fprintf(x_stderr
, "username must be specified!\n\n");
1845 poptPrintHelp(pc
, stderr
, 0);
1849 if (opt_domain
== NULL
) {
1850 opt_domain
= get_winbind_domain();
1853 if (opt_workstation
== NULL
) {
1854 opt_workstation
= "";
1857 if (opt_challenge
.length
) {
1858 if (!check_auth_crap()) {
1864 if (!opt_password
) {
1865 opt_password
= getpass("password: ");
1869 if (!diagnose_ntlm_auth()) {
1875 fstr_sprintf(user
, "%s%c%s", opt_domain
, winbind_separator(), opt_username
);
1876 if (!check_plaintext_auth(user
, opt_password
, True
)) {
1883 poptFreeContext(pc
);