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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/auth.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
32 #include "auth/auth_sam.h"
34 #include "libcli/auth/libcli_auth.h"
35 #include "libcli/security/security.h"
36 #include "lib/events/events.h"
37 #include "lib/messaging/messaging.h"
38 #include "lib/messaging/irpc.h"
39 #include "auth/ntlmssp/ntlmssp.h"
40 #include "param/param.h"
42 #define INITIAL_BUFFER_SIZE 300
43 #define MAX_BUFFER_SIZE 63000
45 enum stdio_helper_mode
{
56 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
57 #define NTLM_AUTH_FLAG_LMKEY 0x0008
60 typedef void (*stdio_helper_function
)(enum stdio_helper_mode stdio_helper_mode
,
61 struct loadparm_context
*lp_ctx
,
62 char *buf
, int length
, void **private,
63 unsigned int mux_id
, void **private2
);
65 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode
,
66 struct loadparm_context
*lp_ctx
,
67 char *buf
, int length
, void **private,
68 unsigned int mux_id
, void **private2
);
70 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode
,
71 struct loadparm_context
*lp_ctx
,
72 char *buf
, int length
, void **private,
73 unsigned int mux_id
, void **private2
);
75 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode
,
76 struct loadparm_context
*lp_ctx
,
77 char *buf
, int length
, void **private,
78 unsigned int mux_id
, void **private2
);
80 static void manage_squid_request(struct loadparm_context
*lp_ctx
,
81 enum stdio_helper_mode helper_mode
,
82 stdio_helper_function fn
, void **private2
);
85 enum stdio_helper_mode mode
;
87 stdio_helper_function fn
;
88 } stdio_helper_protocols
[] = {
89 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
90 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
91 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_gensec_request
},
92 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gensec_request
},
93 { GSS_SPNEGO_SERVER
, "gss-spnego", manage_gensec_request
},
94 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_gensec_request
},
95 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
96 { NUM_HELPER_MODES
, NULL
, NULL
}
99 extern int winbindd_fd
;
101 static const char *opt_username
;
102 static const char *opt_domain
;
103 static const char *opt_workstation
;
104 static const char *opt_password
;
105 static int opt_multiplex
;
106 static int use_cached_creds
;
109 static void mux_printf(unsigned int mux_id
, const char *format
, ...) PRINTF_ATTRIBUTE(2, 3);
111 static void mux_printf(unsigned int mux_id
, const char *format
, ...)
116 x_fprintf(x_stdout
, "%d ", mux_id
);
119 va_start(ap
, format
);
120 x_vfprintf(x_stdout
, format
, ap
);
126 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
127 form DOMAIN/user into a domain and a user */
129 static bool parse_ntlm_auth_domain_user(const char *domuser
, fstring domain
,
130 fstring user
, char winbind_separator
)
133 char *p
= strchr(domuser
, winbind_separator
);
140 fstrcpy(domain
, domuser
);
141 domain
[PTR_DIFF(p
, domuser
)] = 0;
147 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
149 static DATA_BLOB
base64_decode_data_blob(TALLOC_CTX
*mem_ctx
, const char *s
)
151 DATA_BLOB ret
= data_blob_talloc(mem_ctx
, s
, strlen(s
)+1);
152 ret
.length
= ldb_base64_decode((char *)ret
.data
);
157 * Encode a base64 string into a talloc()ed string caller to free.
159 static char *base64_encode_data_blob(TALLOC_CTX
*mem_ctx
, DATA_BLOB data
)
161 return ldb_base64_encode(mem_ctx
, (const char *)data
.data
, data
.length
);
165 * Decode a base64 string in-place - wrapper for the above
167 static void base64_decode_inplace(char *s
)
169 ldb_base64_decode(s
);
174 /* Authenticate a user with a plaintext password */
176 static bool check_plaintext_auth(const char *user
, const char *pass
,
177 bool stdout_diagnostics
)
179 return (strcmp(pass
, opt_password
) == 0);
182 /* authenticate a user with an encrypted username/password */
184 static NTSTATUS
local_pw_check_specified(struct loadparm_context
*lp_ctx
,
185 const char *username
,
187 const char *workstation
,
188 const DATA_BLOB
*challenge
,
189 const DATA_BLOB
*lm_response
,
190 const DATA_BLOB
*nt_response
,
192 DATA_BLOB
*lm_session_key
,
193 DATA_BLOB
*user_session_key
,
198 struct samr_Password lm_pw
, nt_pw
;
199 struct samr_Password
*lm_pwd
, *nt_pwd
;
200 TALLOC_CTX
*mem_ctx
= talloc_init("local_pw_check_specified");
202 nt_status
= NT_STATUS_NO_MEMORY
;
205 E_md4hash(opt_password
, nt_pw
.hash
);
206 if (E_deshash(opt_password
, lm_pw
.hash
)) {
214 nt_status
= ntlm_password_check(mem_ctx
,
216 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
|
217 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
,
224 lm_pwd
, nt_pwd
, user_session_key
, lm_session_key
);
226 if (NT_STATUS_IS_OK(nt_status
)) {
230 *lp_winbind_separator(lp_ctx
),
234 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
235 domain
, username
, workstation
,
236 nt_errstr(nt_status
)));
238 talloc_free(mem_ctx
);
241 *error_string
= strdup(nt_errstr(nt_status
));
248 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode
,
249 struct loadparm_context
*lp_ctx
,
250 char *buf
, int length
, void **private,
251 unsigned int mux_id
, void **private2
)
256 pass
= memchr(buf
, ' ', length
);
258 DEBUG(2, ("Password not found. Denying access\n"));
259 mux_printf(mux_id
, "ERR\n");
265 if (stdio_helper_mode
== SQUID_2_5_BASIC
) {
266 rfc1738_unescape(user
);
267 rfc1738_unescape(pass
);
270 if (check_plaintext_auth(user
, pass
, false)) {
271 mux_printf(mux_id
, "OK\n");
273 mux_printf(mux_id
, "ERR\n");
277 /* This is a bit hairy, but the basic idea is to do a password callback
278 to the calling application. The callback comes from within gensec */
280 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode
,
281 struct loadparm_context
*lp_ctx
,
282 char *buf
, int length
, void **private,
283 unsigned int mux_id
, void **password
)
286 if (strlen(buf
) < 2) {
287 DEBUG(1, ("query [%s] invalid", buf
));
288 mux_printf(mux_id
, "BH\n");
292 if (strlen(buf
) > 3) {
293 in
= base64_decode_data_blob(NULL
, buf
+ 3);
295 in
= data_blob(NULL
, 0);
298 if (strncmp(buf
, "PW ", 3) == 0) {
300 *password
= talloc_strndup(*private /* hopefully the right gensec context, useful to use for talloc */,
301 (const char *)in
.data
, in
.length
);
303 if (*password
== NULL
) {
304 DEBUG(1, ("Out of memory\n"));
305 mux_printf(mux_id
, "BH\n");
310 mux_printf(mux_id
, "OK\n");
314 DEBUG(1, ("Asked for (and expected) a password\n"));
315 mux_printf(mux_id
, "BH\n");
320 * Callback for password credentials. This is not async, and when
321 * GENSEC and the credentials code is made async, it will look rather
325 static const char *get_password(struct cli_credentials
*credentials
)
327 char *password
= NULL
;
329 /* Ask for a password */
330 mux_printf((unsigned int)credentials
->priv_data
, "PW\n");
331 credentials
->priv_data
= NULL
;
333 manage_squid_request(cmdline_lp_ctx
, NUM_HELPER_MODES
/* bogus */, manage_gensec_get_pw_request
, (void **)&password
);
338 Check if a string is part of a list.
340 static bool in_list(const char *s
, const char *list
, bool casesensitive
)
348 while (next_token(&p
, tok
, LIST_SEP
, sizeof(tok
))) {
349 if ((casesensitive
?strcmp
:strcasecmp_m
)(tok
,s
) == 0)
355 static void gensec_want_feature_list(struct gensec_security
*state
, char* feature_list
)
357 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list
, true)) {
358 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
359 gensec_want_feature(state
, GENSEC_FEATURE_SESSION_KEY
);
361 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list
, true)) {
362 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
363 gensec_want_feature(state
, GENSEC_FEATURE_SIGN
);
365 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list
, true)) {
366 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
367 gensec_want_feature(state
, GENSEC_FEATURE_SEAL
);
371 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode
,
372 struct loadparm_context
*lp_ctx
,
373 char *buf
, int length
, void **private,
374 unsigned int mux_id
, void **private2
)
377 DATA_BLOB out
= data_blob(NULL
, 0);
378 char *out_base64
= NULL
;
379 const char *reply_arg
= NULL
;
380 struct gensec_ntlm_state
{
381 struct gensec_security
*gensec_state
;
382 const char *set_password
;
384 struct gensec_ntlm_state
*state
;
385 struct event_context
*ev
;
386 struct messaging_context
*msg
;
390 const char *reply_code
;
391 struct cli_credentials
*creds
;
393 static char *want_feature_list
= NULL
;
394 static DATA_BLOB session_key
;
399 state
= (struct gensec_ntlm_state
*)*private;
401 state
= talloc_zero(NULL
, struct gensec_ntlm_state
);
403 mux_printf(mux_id
, "BH No Memory\n");
408 state
->set_password
= opt_password
;
412 if (strlen(buf
) < 2) {
413 DEBUG(1, ("query [%s] invalid", buf
));
414 mux_printf(mux_id
, "BH\n");
418 if (strlen(buf
) > 3) {
419 if(strncmp(buf
, "SF ", 3) == 0) {
420 DEBUG(10, ("Setting flags to negotiate\n"));
421 talloc_free(want_feature_list
);
422 want_feature_list
= talloc_strndup(state
, buf
+3, strlen(buf
)-3);
423 mux_printf(mux_id
, "OK\n");
426 in
= base64_decode_data_blob(NULL
, buf
+ 3);
428 in
= data_blob(NULL
, 0);
431 if (strncmp(buf
, "YR", 2) == 0) {
432 if (state
->gensec_state
) {
433 talloc_free(state
->gensec_state
);
434 state
->gensec_state
= NULL
;
436 } else if ( (strncmp(buf
, "OK", 2) == 0)) {
437 /* Just return BH, like ntlm_auth from Samba 3 does. */
438 mux_printf(mux_id
, "BH\n");
441 } else if ( (strncmp(buf
, "TT ", 3) != 0) &&
442 (strncmp(buf
, "KK ", 3) != 0) &&
443 (strncmp(buf
, "AF ", 3) != 0) &&
444 (strncmp(buf
, "NA ", 3) != 0) &&
445 (strncmp(buf
, "UG", 2) != 0) &&
446 (strncmp(buf
, "PW ", 3) != 0) &&
447 (strncmp(buf
, "GK", 2) != 0) &&
448 (strncmp(buf
, "GF", 2) != 0)) {
449 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
450 mux_printf(mux_id
, "BH\n");
456 if (!(state
->gensec_state
)) {
457 switch (stdio_helper_mode
) {
458 case GSS_SPNEGO_CLIENT
:
459 case NTLMSSP_CLIENT_1
:
460 /* setup the client side */
462 nt_status
= gensec_client_start(NULL
, &state
->gensec_state
, NULL
, lp_ctx
);
463 if (!NT_STATUS_IS_OK(nt_status
)) {
468 case GSS_SPNEGO_SERVER
:
469 case SQUID_2_5_NTLMSSP
:
470 ev
= event_context_init(state
);
474 msg
= messaging_client_init(state
, lp_messaging_path(state
, lp_ctx
),
475 lp_iconv_convenience(lp_ctx
), ev
);
479 if (!NT_STATUS_IS_OK(gensec_server_start(state
, ev
, lp_ctx
, msg
, &state
->gensec_state
))) {
487 creds
= cli_credentials_init(state
->gensec_state
);
488 cli_credentials_set_conf(creds
, lp_ctx
);
490 cli_credentials_set_username(creds
, opt_username
, CRED_SPECIFIED
);
493 cli_credentials_set_domain(creds
, opt_domain
, CRED_SPECIFIED
);
495 if (state
->set_password
) {
496 cli_credentials_set_password(creds
, state
->set_password
, CRED_SPECIFIED
);
498 cli_credentials_set_password_callback(creds
, get_password
);
499 creds
->priv_data
= (void*)mux_id
;
501 if (opt_workstation
) {
502 cli_credentials_set_workstation(creds
, opt_workstation
, CRED_SPECIFIED
);
505 switch (stdio_helper_mode
) {
506 case GSS_SPNEGO_SERVER
:
507 case SQUID_2_5_NTLMSSP
:
508 cli_credentials_set_machine_account(creds
, lp_ctx
);
514 gensec_set_credentials(state
->gensec_state
, creds
);
515 gensec_want_feature_list(state
->gensec_state
, want_feature_list
);
517 switch (stdio_helper_mode
) {
518 case GSS_SPNEGO_CLIENT
:
519 case GSS_SPNEGO_SERVER
:
520 nt_status
= gensec_start_mech_by_oid(state
->gensec_state
, GENSEC_OID_SPNEGO
);
525 case NTLMSSP_CLIENT_1
:
530 case SQUID_2_5_NTLMSSP
:
531 nt_status
= gensec_start_mech_by_oid(state
->gensec_state
, GENSEC_OID_NTLMSSP
);
537 if (!NT_STATUS_IS_OK(nt_status
)) {
538 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status
)));
539 mux_printf(mux_id
, "BH\n");
546 mem_ctx
= talloc_named(NULL
, 0, "manage_gensec_request internal mem_ctx");
548 if (strncmp(buf
, "PW ", 3) == 0) {
549 state
->set_password
= talloc_strndup(state
,
550 (const char *)in
.data
,
553 cli_credentials_set_password(gensec_get_credentials(state
->gensec_state
),
556 mux_printf(mux_id
, "OK\n");
558 talloc_free(mem_ctx
);
562 if (strncmp(buf
, "UG", 2) == 0) {
564 char *grouplist
= NULL
;
565 struct auth_session_info
*session_info
;
567 nt_status
= gensec_session_info(state
->gensec_state
, &session_info
);
568 if (!NT_STATUS_IS_OK(nt_status
)) {
569 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status
)));
570 mux_printf(mux_id
, "BH %s\n", nt_errstr(nt_status
));
572 talloc_free(mem_ctx
);
576 /* get the string onto the context */
577 grouplist
= talloc_strdup(mem_ctx
, "");
579 for (i
=0; i
<session_info
->security_token
->num_sids
; i
++) {
580 struct security_token
*token
= session_info
->security_token
;
581 const char *sidstr
= dom_sid_string(session_info
,
583 grouplist
= talloc_asprintf_append_buffer(grouplist
, "%s,", sidstr
);
586 mux_printf(mux_id
, "GL %s\n", grouplist
);
587 talloc_free(session_info
);
589 talloc_free(mem_ctx
);
593 if (strncmp(buf
, "GK", 2) == 0) {
595 DEBUG(10, ("Requested session key\n"));
596 nt_status
= gensec_session_key(state
->gensec_state
, &session_key
);
597 if(!NT_STATUS_IS_OK(nt_status
)) {
598 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status
)));
599 mux_printf(mux_id
, "BH No session key\n");
600 talloc_free(mem_ctx
);
603 base64_key
= base64_encode_data_blob(state
, session_key
);
604 mux_printf(mux_id
, "GK %s\n", base64_key
);
605 talloc_free(base64_key
);
607 talloc_free(mem_ctx
);
611 if (strncmp(buf
, "GF", 2) == 0) {
612 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
;
615 gensec_ntlmssp_state
= talloc_get_type(state
->gensec_state
->private_data
,
616 struct gensec_ntlmssp_state
);
617 neg_flags
= gensec_ntlmssp_state
->neg_flags
;
619 DEBUG(10, ("Requested negotiated feature flags\n"));
620 mux_printf(mux_id
, "GF 0x%08x\n", neg_flags
);
624 nt_status
= gensec_update(state
->gensec_state
, mem_ctx
, in
, &out
);
626 /* don't leak 'bad password'/'no such user' info to the network client */
627 nt_status
= auth_nt_status_squash(nt_status
);
630 out_base64
= base64_encode_data_blob(mem_ctx
, out
);
635 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
639 } else if (state
->gensec_state
->gensec_role
== GENSEC_CLIENT
) {
641 } else if (state
->gensec_state
->gensec_role
== GENSEC_SERVER
) {
648 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
650 reply_arg
= nt_errstr(nt_status
);
651 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status
)));
652 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_UNSUCCESSFUL
)) {
654 reply_arg
= nt_errstr(nt_status
);
655 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status
)));
656 } else if (!NT_STATUS_IS_OK(nt_status
)) {
658 reply_arg
= nt_errstr(nt_status
);
659 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status
)));
660 } else if /* OK */ (state
->gensec_state
->gensec_role
== GENSEC_SERVER
) {
661 struct auth_session_info
*session_info
;
663 nt_status
= gensec_session_info(state
->gensec_state
, &session_info
);
664 if (!NT_STATUS_IS_OK(nt_status
)) {
666 reply_arg
= nt_errstr(nt_status
);
667 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status
)));
671 reply_arg
= talloc_asprintf(state
->gensec_state
,
672 "%s%s%s", session_info
->server_info
->domain_name
,
673 lp_winbind_separator(lp_ctx
), session_info
->server_info
->account_name
);
674 talloc_free(session_info
);
676 } else if (state
->gensec_state
->gensec_role
== GENSEC_CLIENT
) {
678 reply_arg
= out_base64
;
683 switch (stdio_helper_mode
) {
684 case GSS_SPNEGO_SERVER
:
685 mux_printf(mux_id
, "%s %s %s\n", reply_code
,
686 out_base64
? out_base64
: "*",
687 reply_arg
? reply_arg
: "*");
691 mux_printf(mux_id
, "%s %s\n", reply_code
, out_base64
);
692 } else if (reply_arg
) {
693 mux_printf(mux_id
, "%s %s\n", reply_code
, reply_arg
);
695 mux_printf(mux_id
, "%s\n", reply_code
);
699 talloc_free(mem_ctx
);
703 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode
,
704 struct loadparm_context
*lp_ctx
,
705 char *buf
, int length
, void **private,
706 unsigned int mux_id
, void **private2
)
708 char *request
, *parameter
;
709 static DATA_BLOB challenge
;
710 static DATA_BLOB lm_response
;
711 static DATA_BLOB nt_response
;
712 static char *full_username
;
713 static char *username
;
715 static char *plaintext_password
;
716 static bool ntlm_server_1_user_session_key
;
717 static bool ntlm_server_1_lm_session_key
;
719 if (strequal(buf
, ".")) {
720 if (!full_username
&& !username
) {
721 mux_printf(mux_id
, "Error: No username supplied!\n");
722 } else if (plaintext_password
) {
723 /* handle this request as plaintext */
724 if (!full_username
) {
725 if (asprintf(&full_username
, "%s%c%s", domain
, *lp_winbind_separator(lp_ctx
), username
) == -1) {
726 mux_printf(mux_id
, "Error: Out of memory in asprintf!\n.\n");
730 if (check_plaintext_auth(full_username
, plaintext_password
, false)) {
731 mux_printf(mux_id
, "Authenticated: Yes\n");
733 mux_printf(mux_id
, "Authenticated: No\n");
735 } else if (!lm_response
.data
&& !nt_response
.data
) {
736 mux_printf(mux_id
, "Error: No password supplied!\n");
737 } else if (!challenge
.data
) {
738 mux_printf(mux_id
, "Error: No lanman-challenge supplied!\n");
740 char *error_string
= NULL
;
742 DATA_BLOB user_session_key
;
745 if (full_username
&& !username
) {
749 if (!parse_ntlm_auth_domain_user(full_username
, fstr_user
, fstr_domain
,
750 *lp_winbind_separator(lp_ctx
))) {
751 /* username might be 'tainted', don't print into our new-line deleimianted stream */
752 mux_printf(mux_id
, "Error: Could not parse into domain and username\n");
756 username
= smb_xstrdup(fstr_user
);
757 domain
= smb_xstrdup(fstr_domain
);
761 domain
= smb_xstrdup(lp_workgroup(lp_ctx
));
764 if (ntlm_server_1_lm_session_key
)
765 flags
|= NTLM_AUTH_FLAG_LMKEY
;
767 if (ntlm_server_1_user_session_key
)
768 flags
|= NTLM_AUTH_FLAG_USER_SESSION_KEY
;
770 if (!NT_STATUS_IS_OK(
771 local_pw_check_specified(lp_ctx
,
774 lp_netbios_name(lp_ctx
),
784 mux_printf(mux_id
, "Authenticated: No\n");
785 mux_printf(mux_id
, "Authentication-Error: %s\n.\n", error_string
);
786 SAFE_FREE(error_string
);
788 static char zeros
[16];
790 char *hex_user_session_key
;
792 mux_printf(mux_id
, "Authenticated: Yes\n");
794 if (ntlm_server_1_lm_session_key
796 && (memcmp(zeros
, lm_key
.data
,
797 lm_key
.length
) != 0)) {
798 hex_encode(lm_key
.data
,
801 mux_printf(mux_id
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
802 SAFE_FREE(hex_lm_key
);
805 if (ntlm_server_1_user_session_key
806 && user_session_key
.length
807 && (memcmp(zeros
, user_session_key
.data
,
808 user_session_key
.length
) != 0)) {
809 hex_encode(user_session_key
.data
,
810 user_session_key
.length
,
811 &hex_user_session_key
);
812 mux_printf(mux_id
, "User-Session-Key: %s\n", hex_user_session_key
);
813 SAFE_FREE(hex_user_session_key
);
817 /* clear out the state */
818 challenge
= data_blob(NULL
, 0);
819 nt_response
= data_blob(NULL
, 0);
820 lm_response
= data_blob(NULL
, 0);
821 SAFE_FREE(full_username
);
824 SAFE_FREE(plaintext_password
);
825 ntlm_server_1_user_session_key
= false;
826 ntlm_server_1_lm_session_key
= false;
827 mux_printf(mux_id
, ".\n");
834 /* Indicates a base64 encoded structure */
835 parameter
= strstr(request
, ":: ");
837 parameter
= strstr(request
, ": ");
840 DEBUG(0, ("Parameter not found!\n"));
841 mux_printf(mux_id
, "Error: Parameter not found!\n.\n");
858 base64_decode_inplace(parameter
);
861 if (strequal(request
, "LANMAN-Challenge")) {
862 challenge
= strhex_to_data_blob(parameter
);
863 if (challenge
.length
!= 8) {
864 mux_printf(mux_id
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
866 (int)challenge
.length
);
867 challenge
= data_blob(NULL
, 0);
869 } else if (strequal(request
, "NT-Response")) {
870 nt_response
= strhex_to_data_blob(parameter
);
871 if (nt_response
.length
< 24) {
872 mux_printf(mux_id
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
874 (int)nt_response
.length
);
875 nt_response
= data_blob(NULL
, 0);
877 } else if (strequal(request
, "LANMAN-Response")) {
878 lm_response
= strhex_to_data_blob(parameter
);
879 if (lm_response
.length
!= 24) {
880 mux_printf(mux_id
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
882 (int)lm_response
.length
);
883 lm_response
= data_blob(NULL
, 0);
885 } else if (strequal(request
, "Password")) {
886 plaintext_password
= smb_xstrdup(parameter
);
887 } else if (strequal(request
, "NT-Domain")) {
888 domain
= smb_xstrdup(parameter
);
889 } else if (strequal(request
, "Username")) {
890 username
= smb_xstrdup(parameter
);
891 } else if (strequal(request
, "Full-Username")) {
892 full_username
= smb_xstrdup(parameter
);
893 } else if (strequal(request
, "Request-User-Session-Key")) {
894 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
895 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
896 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
898 mux_printf(mux_id
, "Error: Unknown request %s\n.\n", request
);
902 static void manage_squid_request(struct loadparm_context
*lp_ctx
, enum stdio_helper_mode helper_mode
,
903 stdio_helper_function fn
, void **private2
)
906 char tmp
[INITIAL_BUFFER_SIZE
+1];
907 unsigned int mux_id
= 0;
908 int length
, buf_size
= 0;
911 unsigned int max_mux
;
912 void **private_pointers
;
915 static struct mux_private
*mux_private
;
916 static void *normal_private
;
919 buf
= talloc_strdup(NULL
, "");
922 DEBUG(0, ("Failed to allocate memory for reading the input "
924 x_fprintf(x_stdout
, "ERR\n");
929 /* this is not a typo - x_fgets doesn't work too well under
931 if (fgets(tmp
, INITIAL_BUFFER_SIZE
, stdin
) == NULL
) {
933 DEBUG(1, ("fgets() failed! dying..... errno=%d "
934 "(%s)\n", ferror(stdin
),
935 strerror(ferror(stdin
))));
937 exit(1); /* BIIG buffer */
942 buf
= talloc_strdup_append_buffer(buf
, tmp
);
943 buf_size
+= INITIAL_BUFFER_SIZE
;
945 if (buf_size
> MAX_BUFFER_SIZE
) {
946 DEBUG(0, ("Invalid Request (too large)\n"));
947 x_fprintf(x_stdout
, "ERR\n");
952 c
= strchr(buf
, '\n');
958 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
960 if (buf
[0] == '\0') {
961 DEBUG(0, ("Invalid Request (empty)\n"));
962 x_fprintf(x_stdout
, "ERR\n");
968 if (sscanf(buf
, "%u ", &mux_id
) != 1) {
969 DEBUG(0, ("Invalid Request - no multiplex id\n"));
970 x_fprintf(x_stdout
, "ERR\n");
975 mux_private
= talloc(NULL
, struct mux_private
);
976 mux_private
->max_mux
= 0;
977 mux_private
->private_pointers
= NULL
;
982 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
983 x_fprintf(x_stdout
, "ERR\n");
988 if (mux_id
>= mux_private
->max_mux
) {
989 unsigned int prev_max
= mux_private
->max_mux
;
990 mux_private
->max_mux
= mux_id
+ 1;
991 mux_private
->private_pointers
992 = talloc_realloc(mux_private
,
993 mux_private
->private_pointers
,
994 void *, mux_private
->max_mux
);
995 memset(&mux_private
->private_pointers
[prev_max
], '\0',
996 (sizeof(*mux_private
->private_pointers
) * (mux_private
->max_mux
- prev_max
)));
999 private = &mux_private
->private_pointers
[mux_id
];
1002 private = &normal_private
;
1005 fn(helper_mode
, lp_ctx
, c
, length
, private, mux_id
, private2
);
1009 static void squid_stream(struct loadparm_context
*lp_ctx
,
1010 enum stdio_helper_mode stdio_mode
,
1011 stdio_helper_function fn
) {
1012 /* initialize FDescs */
1013 x_setbuf(x_stdout
, NULL
);
1014 x_setbuf(x_stderr
, NULL
);
1016 manage_squid_request(lp_ctx
, stdio_mode
, fn
, NULL
);
1024 OPT_USERNAME
= 1000,
1033 OPT_USER_SESSION_KEY
,
1035 OPT_REQUIRE_MEMBERSHIP
,
1037 OPT_USE_CACHED_CREDS
,
1040 int main(int argc
, const char **argv
)
1042 static const char *helper_protocol
;
1047 /* NOTE: DO NOT change this interface without considering the implications!
1048 This is an external interface, which other programs will use to interact
1052 /* We do not use single-letter command abbreviations, because they harm future
1053 interface stability. */
1055 struct poptOption long_options
[] = {
1057 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
1058 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
1059 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
1060 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_PASSWORD
, "Username"},
1061 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
1062 { "multiplex", 0, POPT_ARG_NONE
, &opt_multiplex
, OPT_MULTIPLEX
, "Multiplex Mode"},
1063 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "silently ignored for compatibility reasons"},
1069 /* Samba client initialisation */
1071 setup_logging(NULL
, DEBUG_STDERR
);
1075 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
1077 /* Parse command line options */
1080 poptPrintHelp(pc
, stderr
, 0);
1084 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
1085 POPT_CONTEXT_KEEP_FIRST
);
1087 while((opt
= poptGetNextOpt(pc
)) != -1) {
1093 fprintf(stderr
, "%s: %s\n",
1094 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
1099 gensec_init(cmdline_lp_ctx
);
1101 if (opt_domain
== NULL
) {
1102 opt_domain
= lp_workgroup(cmdline_lp_ctx
);
1105 if (helper_protocol
) {
1107 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
1108 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
1109 squid_stream(cmdline_lp_ctx
, stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
1113 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
1115 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
1116 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
1122 if (!opt_username
) {
1123 x_fprintf(x_stderr
, "username must be specified!\n\n");
1124 poptPrintHelp(pc
, stderr
, 0);
1128 if (opt_workstation
== NULL
) {
1129 opt_workstation
= lp_netbios_name(cmdline_lp_ctx
);
1132 if (!opt_password
) {
1133 opt_password
= getpass("password: ");
1139 asprintf(&user
, "%s%c%s", opt_domain
, *lp_winbind_separator(cmdline_lp_ctx
), opt_username
);
1140 if (!check_plaintext_auth(user
, opt_password
, true)) {
1147 poptFreeContext(pc
);