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"
33 #include "libcli/auth/libcli_auth.h"
34 #include "libcli/security/security.h"
35 #include "lib/events/events.h"
36 #include "lib/messaging/messaging.h"
37 #include "lib/messaging/irpc.h"
38 #include "auth/ntlmssp/ntlmssp.h"
39 #include "param/param.h"
41 #define INITIAL_BUFFER_SIZE 300
42 #define MAX_BUFFER_SIZE 63000
44 enum stdio_helper_mode
{
55 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
56 #define NTLM_AUTH_FLAG_LMKEY 0x0008
59 typedef void (*stdio_helper_function
)(enum stdio_helper_mode stdio_helper_mode
,
60 struct loadparm_context
*lp_ctx
,
61 char *buf
, int length
, void **private1
,
62 unsigned int mux_id
, void **private2
);
64 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode
,
65 struct loadparm_context
*lp_ctx
,
66 char *buf
, int length
, void **private1
,
67 unsigned int mux_id
, void **private2
);
69 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode
,
70 struct loadparm_context
*lp_ctx
,
71 char *buf
, int length
, void **private1
,
72 unsigned int mux_id
, void **private2
);
74 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode
,
75 struct loadparm_context
*lp_ctx
,
76 char *buf
, int length
, void **private1
,
77 unsigned int mux_id
, void **private2
);
79 static void manage_squid_request(struct loadparm_context
*lp_ctx
,
80 enum stdio_helper_mode helper_mode
,
81 stdio_helper_function fn
, void **private2
);
84 enum stdio_helper_mode mode
;
86 stdio_helper_function fn
;
87 } stdio_helper_protocols
[] = {
88 { SQUID_2_4_BASIC
, "squid-2.4-basic", manage_squid_basic_request
},
89 { SQUID_2_5_BASIC
, "squid-2.5-basic", manage_squid_basic_request
},
90 { SQUID_2_5_NTLMSSP
, "squid-2.5-ntlmssp", manage_gensec_request
},
91 { GSS_SPNEGO_CLIENT
, "gss-spnego-client", manage_gensec_request
},
92 { GSS_SPNEGO_SERVER
, "gss-spnego", manage_gensec_request
},
93 { NTLMSSP_CLIENT_1
, "ntlmssp-client-1", manage_gensec_request
},
94 { NTLM_SERVER_1
, "ntlm-server-1", manage_ntlm_server_1_request
},
95 { NUM_HELPER_MODES
, NULL
, NULL
}
98 extern int winbindd_fd
;
100 static const char *opt_username
;
101 static const char *opt_domain
;
102 static const char *opt_workstation
;
103 static const char *opt_password
;
104 static int opt_multiplex
;
105 static int use_cached_creds
;
108 static void mux_printf(unsigned int mux_id
, const char *format
, ...) PRINTF_ATTRIBUTE(2, 3);
110 static void mux_printf(unsigned int mux_id
, const char *format
, ...)
115 x_fprintf(x_stdout
, "%d ", mux_id
);
118 va_start(ap
, format
);
119 x_vfprintf(x_stdout
, format
, ap
);
125 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
126 form DOMAIN/user into a domain and a user */
128 static bool parse_ntlm_auth_domain_user(const char *domuser
, char **domain
,
129 char **user
, char winbind_separator
)
132 char *p
= strchr(domuser
, winbind_separator
);
138 *user
= smb_xstrdup(p
+1);
139 *domain
= smb_xstrdup(domuser
);
140 (*domain
)[PTR_DIFF(p
, domuser
)] = 0;
146 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
148 static DATA_BLOB
base64_decode_data_blob(TALLOC_CTX
*mem_ctx
, const char *s
)
150 DATA_BLOB ret
= data_blob_talloc(mem_ctx
, s
, strlen(s
)+1);
151 ret
.length
= ldb_base64_decode((char *)ret
.data
);
156 * Encode a base64 string into a talloc()ed string caller to free.
158 static char *base64_encode_data_blob(TALLOC_CTX
*mem_ctx
, DATA_BLOB data
)
160 return ldb_base64_encode(mem_ctx
, (const char *)data
.data
, data
.length
);
164 * Decode a base64 string in-place - wrapper for the above
166 static void base64_decode_inplace(char *s
)
168 ldb_base64_decode(s
);
173 /* Authenticate a user with a plaintext password */
175 static bool check_plaintext_auth(const char *user
, const char *pass
,
176 bool stdout_diagnostics
)
178 return (strcmp(pass
, opt_password
) == 0);
181 /* authenticate a user with an encrypted username/password */
183 static NTSTATUS
local_pw_check_specified(struct loadparm_context
*lp_ctx
,
184 const char *username
,
186 const char *workstation
,
187 const DATA_BLOB
*challenge
,
188 const DATA_BLOB
*lm_response
,
189 const DATA_BLOB
*nt_response
,
191 DATA_BLOB
*lm_session_key
,
192 DATA_BLOB
*user_session_key
,
197 struct samr_Password lm_pw
, nt_pw
;
198 struct samr_Password
*lm_pwd
, *nt_pwd
;
199 TALLOC_CTX
*mem_ctx
= talloc_init("local_pw_check_specified");
201 nt_status
= NT_STATUS_NO_MEMORY
;
204 E_md4hash(opt_password
, nt_pw
.hash
);
205 if (E_deshash(opt_password
, lm_pw
.hash
)) {
213 nt_status
= ntlm_password_check(mem_ctx
,
214 lp_lanman_auth(lp_ctx
),
215 lp_ntlm_auth(lp_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 **private1
,
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 **private1
,
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 Query invalid\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(*private1
/* 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 Out of memory\n");
310 mux_printf(mux_id
, "OK\n");
314 DEBUG(1, ("Asked for (and expected) a password\n"));
315 mux_printf(mux_id
, "BH Expected a password\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)(uintptr_t)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
)
343 size_t tok_len
= 1024;
349 tok
= (char *)malloc(tok_len
);
354 while (next_token(&p
, tok
, LIST_SEP
, tok_len
)) {
355 if ((casesensitive
?strcmp
:strcasecmp_m
)(tok
,s
) == 0) {
364 static void gensec_want_feature_list(struct gensec_security
*state
, char* feature_list
)
366 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list
, true)) {
367 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
368 gensec_want_feature(state
, GENSEC_FEATURE_SESSION_KEY
);
370 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list
, true)) {
371 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
372 gensec_want_feature(state
, GENSEC_FEATURE_SIGN
);
374 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list
, true)) {
375 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
376 gensec_want_feature(state
, GENSEC_FEATURE_SEAL
);
380 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode
,
381 struct loadparm_context
*lp_ctx
,
382 char *buf
, int length
, void **private1
,
383 unsigned int mux_id
, void **private2
)
386 DATA_BLOB out
= data_blob(NULL
, 0);
387 char *out_base64
= NULL
;
388 const char *reply_arg
= NULL
;
389 struct gensec_ntlm_state
{
390 struct gensec_security
*gensec_state
;
391 const char *set_password
;
393 struct gensec_ntlm_state
*state
;
394 struct tevent_context
*ev
;
395 struct messaging_context
*msg
;
399 const char *reply_code
;
400 struct cli_credentials
*creds
;
402 static char *want_feature_list
= NULL
;
403 static DATA_BLOB session_key
;
408 state
= (struct gensec_ntlm_state
*)*private1
;
410 state
= talloc_zero(NULL
, struct gensec_ntlm_state
);
412 mux_printf(mux_id
, "BH No Memory\n");
417 state
->set_password
= opt_password
;
421 if (strlen(buf
) < 2) {
422 DEBUG(1, ("query [%s] invalid", buf
));
423 mux_printf(mux_id
, "BH Query invalid\n");
427 if (strlen(buf
) > 3) {
428 if(strncmp(buf
, "SF ", 3) == 0) {
429 DEBUG(10, ("Setting flags to negotiate\n"));
430 talloc_free(want_feature_list
);
431 want_feature_list
= talloc_strndup(state
, buf
+3, strlen(buf
)-3);
432 mux_printf(mux_id
, "OK\n");
435 in
= base64_decode_data_blob(NULL
, buf
+ 3);
437 in
= data_blob(NULL
, 0);
440 if (strncmp(buf
, "YR", 2) == 0) {
441 if (state
->gensec_state
) {
442 talloc_free(state
->gensec_state
);
443 state
->gensec_state
= NULL
;
445 } else if ( (strncmp(buf
, "OK", 2) == 0)) {
446 /* Just return BH, like ntlm_auth from Samba 3 does. */
447 mux_printf(mux_id
, "BH Command expected\n");
450 } else if ( (strncmp(buf
, "TT ", 3) != 0) &&
451 (strncmp(buf
, "KK ", 3) != 0) &&
452 (strncmp(buf
, "AF ", 3) != 0) &&
453 (strncmp(buf
, "NA ", 3) != 0) &&
454 (strncmp(buf
, "UG", 2) != 0) &&
455 (strncmp(buf
, "PW ", 3) != 0) &&
456 (strncmp(buf
, "GK", 2) != 0) &&
457 (strncmp(buf
, "GF", 2) != 0)) {
458 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf
));
459 mux_printf(mux_id
, "BH SPNEGO request invalid\n");
464 ev
= s4_event_context_init(state
);
469 mem_ctx
= talloc_named(NULL
, 0, "manage_gensec_request internal mem_ctx");
472 if (!(state
->gensec_state
)) {
473 switch (stdio_helper_mode
) {
474 case GSS_SPNEGO_CLIENT
:
475 case NTLMSSP_CLIENT_1
:
476 /* setup the client side */
478 nt_status
= gensec_client_start(NULL
, &state
->gensec_state
, ev
,
479 lp_gensec_settings(NULL
, lp_ctx
));
480 if (!NT_STATUS_IS_OK(nt_status
)) {
481 talloc_free(mem_ctx
);
486 case GSS_SPNEGO_SERVER
:
487 case SQUID_2_5_NTLMSSP
:
489 const char *winbind_method
[] = { "winbind", NULL
};
490 struct auth_context
*auth_context
;
492 msg
= messaging_client_init(state
, lp_messaging_path(state
, lp_ctx
),
493 lp_iconv_convenience(lp_ctx
), ev
);
495 talloc_free(mem_ctx
);
498 nt_status
= auth_context_create_methods(mem_ctx
,
505 if (!NT_STATUS_IS_OK(nt_status
)) {
506 talloc_free(mem_ctx
);
510 if (!NT_STATUS_IS_OK(gensec_server_start(state
, ev
,
511 lp_gensec_settings(state
, lp_ctx
),
512 auth_context
, &state
->gensec_state
))) {
513 talloc_free(mem_ctx
);
519 talloc_free(mem_ctx
);
523 creds
= cli_credentials_init(state
->gensec_state
);
524 cli_credentials_set_conf(creds
, lp_ctx
);
526 cli_credentials_set_username(creds
, opt_username
, CRED_SPECIFIED
);
529 cli_credentials_set_domain(creds
, opt_domain
, CRED_SPECIFIED
);
531 if (state
->set_password
) {
532 cli_credentials_set_password(creds
, state
->set_password
, CRED_SPECIFIED
);
534 cli_credentials_set_password_callback(creds
, get_password
);
535 creds
->priv_data
= (void*)(uintptr_t)mux_id
;
537 if (opt_workstation
) {
538 cli_credentials_set_workstation(creds
, opt_workstation
, CRED_SPECIFIED
);
541 switch (stdio_helper_mode
) {
542 case GSS_SPNEGO_SERVER
:
543 case SQUID_2_5_NTLMSSP
:
544 cli_credentials_set_machine_account(creds
, lp_ctx
);
550 gensec_set_credentials(state
->gensec_state
, creds
);
551 gensec_want_feature_list(state
->gensec_state
, want_feature_list
);
553 switch (stdio_helper_mode
) {
554 case GSS_SPNEGO_CLIENT
:
555 case GSS_SPNEGO_SERVER
:
556 nt_status
= gensec_start_mech_by_oid(state
->gensec_state
, GENSEC_OID_SPNEGO
);
561 case NTLMSSP_CLIENT_1
:
566 case SQUID_2_5_NTLMSSP
:
567 nt_status
= gensec_start_mech_by_oid(state
->gensec_state
, GENSEC_OID_NTLMSSP
);
570 talloc_free(mem_ctx
);
574 if (!NT_STATUS_IS_OK(nt_status
)) {
575 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status
)));
576 mux_printf(mux_id
, "BH GENSEC mech failed to start\n");
577 talloc_free(mem_ctx
);
585 if (strncmp(buf
, "PW ", 3) == 0) {
586 state
->set_password
= talloc_strndup(state
,
587 (const char *)in
.data
,
590 cli_credentials_set_password(gensec_get_credentials(state
->gensec_state
),
593 mux_printf(mux_id
, "OK\n");
595 talloc_free(mem_ctx
);
599 if (strncmp(buf
, "UG", 2) == 0) {
601 char *grouplist
= NULL
;
602 struct auth_session_info
*session_info
;
604 nt_status
= gensec_session_info(state
->gensec_state
, &session_info
);
605 if (!NT_STATUS_IS_OK(nt_status
)) {
606 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status
)));
607 mux_printf(mux_id
, "BH %s\n", nt_errstr(nt_status
));
609 talloc_free(mem_ctx
);
613 /* get the string onto the context */
614 grouplist
= talloc_strdup(mem_ctx
, "");
616 for (i
=0; i
<session_info
->security_token
->num_sids
; i
++) {
617 struct security_token
*token
= session_info
->security_token
;
618 const char *sidstr
= dom_sid_string(session_info
,
620 grouplist
= talloc_asprintf_append_buffer(grouplist
, "%s,", sidstr
);
623 mux_printf(mux_id
, "GL %s\n", grouplist
);
624 talloc_free(session_info
);
626 talloc_free(mem_ctx
);
630 if (strncmp(buf
, "GK", 2) == 0) {
632 DEBUG(10, ("Requested session key\n"));
633 nt_status
= gensec_session_key(state
->gensec_state
, &session_key
);
634 if(!NT_STATUS_IS_OK(nt_status
)) {
635 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status
)));
636 mux_printf(mux_id
, "BH No session key\n");
637 talloc_free(mem_ctx
);
640 base64_key
= base64_encode_data_blob(state
, session_key
);
641 mux_printf(mux_id
, "GK %s\n", base64_key
);
642 talloc_free(base64_key
);
644 talloc_free(mem_ctx
);
648 if (strncmp(buf
, "GF", 2) == 0) {
649 struct gensec_ntlmssp_state
*gensec_ntlmssp_state
;
652 gensec_ntlmssp_state
= talloc_get_type(state
->gensec_state
->private_data
,
653 struct gensec_ntlmssp_state
);
654 neg_flags
= gensec_ntlmssp_state
->neg_flags
;
656 DEBUG(10, ("Requested negotiated feature flags\n"));
657 mux_printf(mux_id
, "GF 0x%08x\n", neg_flags
);
661 nt_status
= gensec_update(state
->gensec_state
, mem_ctx
, in
, &out
);
663 /* don't leak 'bad password'/'no such user' info to the network client */
664 nt_status
= auth_nt_status_squash(nt_status
);
667 out_base64
= base64_encode_data_blob(mem_ctx
, out
);
672 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
676 } else if (state
->gensec_state
->gensec_role
== GENSEC_CLIENT
) {
678 } else if (state
->gensec_state
->gensec_role
== GENSEC_SERVER
) {
685 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
686 reply_code
= "BH NT_STATUS_ACCESS_DENIED";
687 reply_arg
= nt_errstr(nt_status
);
688 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status
)));
689 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_UNSUCCESSFUL
)) {
690 reply_code
= "BH NT_STATUS_UNSUCCESSFUL";
691 reply_arg
= nt_errstr(nt_status
);
692 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status
)));
693 } else if (!NT_STATUS_IS_OK(nt_status
)) {
695 reply_arg
= nt_errstr(nt_status
);
696 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status
)));
697 } else if /* OK */ (state
->gensec_state
->gensec_role
== GENSEC_SERVER
) {
698 struct auth_session_info
*session_info
;
700 nt_status
= gensec_session_info(state
->gensec_state
, &session_info
);
701 if (!NT_STATUS_IS_OK(nt_status
)) {
702 reply_code
= "BH Failed to retrive session info";
703 reply_arg
= nt_errstr(nt_status
);
704 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status
)));
708 reply_arg
= talloc_asprintf(state
->gensec_state
,
709 "%s%s%s", session_info
->server_info
->domain_name
,
710 lp_winbind_separator(lp_ctx
), session_info
->server_info
->account_name
);
711 talloc_free(session_info
);
713 } else if (state
->gensec_state
->gensec_role
== GENSEC_CLIENT
) {
715 reply_arg
= out_base64
;
720 switch (stdio_helper_mode
) {
721 case GSS_SPNEGO_SERVER
:
722 mux_printf(mux_id
, "%s %s %s\n", reply_code
,
723 out_base64
? out_base64
: "*",
724 reply_arg
? reply_arg
: "*");
728 mux_printf(mux_id
, "%s %s\n", reply_code
, out_base64
);
729 } else if (reply_arg
) {
730 mux_printf(mux_id
, "%s %s\n", reply_code
, reply_arg
);
732 mux_printf(mux_id
, "%s\n", reply_code
);
736 talloc_free(mem_ctx
);
740 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode
,
741 struct loadparm_context
*lp_ctx
,
742 char *buf
, int length
, void **private1
,
743 unsigned int mux_id
, void **private2
)
745 char *request
, *parameter
;
746 static DATA_BLOB challenge
;
747 static DATA_BLOB lm_response
;
748 static DATA_BLOB nt_response
;
749 static char *full_username
;
750 static char *username
;
752 static char *plaintext_password
;
753 static bool ntlm_server_1_user_session_key
;
754 static bool ntlm_server_1_lm_session_key
;
756 if (strequal(buf
, ".")) {
757 if (!full_username
&& !username
) {
758 mux_printf(mux_id
, "Error: No username supplied!\n");
759 } else if (plaintext_password
) {
760 /* handle this request as plaintext */
761 if (!full_username
) {
762 if (asprintf(&full_username
, "%s%c%s", domain
, *lp_winbind_separator(lp_ctx
), username
) == -1) {
763 mux_printf(mux_id
, "Error: Out of memory in asprintf!\n.\n");
767 if (check_plaintext_auth(full_username
, plaintext_password
, false)) {
768 mux_printf(mux_id
, "Authenticated: Yes\n");
770 mux_printf(mux_id
, "Authenticated: No\n");
772 } else if (!lm_response
.data
&& !nt_response
.data
) {
773 mux_printf(mux_id
, "Error: No password supplied!\n");
774 } else if (!challenge
.data
) {
775 mux_printf(mux_id
, "Error: No lanman-challenge supplied!\n");
777 char *error_string
= NULL
;
779 DATA_BLOB user_session_key
;
782 if (full_username
&& !username
) {
785 if (!parse_ntlm_auth_domain_user(full_username
, &username
,
787 *lp_winbind_separator(lp_ctx
))) {
788 /* username might be 'tainted', don't print into our new-line deleimianted stream */
789 mux_printf(mux_id
, "Error: Could not parse into domain and username\n");
794 domain
= smb_xstrdup(lp_workgroup(lp_ctx
));
797 if (ntlm_server_1_lm_session_key
)
798 flags
|= NTLM_AUTH_FLAG_LMKEY
;
800 if (ntlm_server_1_user_session_key
)
801 flags
|= NTLM_AUTH_FLAG_USER_SESSION_KEY
;
803 if (!NT_STATUS_IS_OK(
804 local_pw_check_specified(lp_ctx
,
807 lp_netbios_name(lp_ctx
),
817 mux_printf(mux_id
, "Authenticated: No\n");
818 mux_printf(mux_id
, "Authentication-Error: %s\n.\n", error_string
);
819 SAFE_FREE(error_string
);
821 static char zeros
[16];
823 char *hex_user_session_key
;
825 mux_printf(mux_id
, "Authenticated: Yes\n");
827 if (ntlm_server_1_lm_session_key
829 && (memcmp(zeros
, lm_key
.data
,
830 lm_key
.length
) != 0)) {
831 hex_encode(lm_key
.data
,
834 mux_printf(mux_id
, "LANMAN-Session-Key: %s\n", hex_lm_key
);
835 SAFE_FREE(hex_lm_key
);
838 if (ntlm_server_1_user_session_key
839 && user_session_key
.length
840 && (memcmp(zeros
, user_session_key
.data
,
841 user_session_key
.length
) != 0)) {
842 hex_encode(user_session_key
.data
,
843 user_session_key
.length
,
844 &hex_user_session_key
);
845 mux_printf(mux_id
, "User-Session-Key: %s\n", hex_user_session_key
);
846 SAFE_FREE(hex_user_session_key
);
850 /* clear out the state */
851 challenge
= data_blob(NULL
, 0);
852 nt_response
= data_blob(NULL
, 0);
853 lm_response
= data_blob(NULL
, 0);
854 SAFE_FREE(full_username
);
857 SAFE_FREE(plaintext_password
);
858 ntlm_server_1_user_session_key
= false;
859 ntlm_server_1_lm_session_key
= false;
860 mux_printf(mux_id
, ".\n");
867 /* Indicates a base64 encoded structure */
868 parameter
= strstr(request
, ":: ");
870 parameter
= strstr(request
, ": ");
873 DEBUG(0, ("Parameter not found!\n"));
874 mux_printf(mux_id
, "Error: Parameter not found!\n.\n");
891 base64_decode_inplace(parameter
);
894 if (strequal(request
, "LANMAN-Challenge")) {
895 challenge
= strhex_to_data_blob(NULL
, parameter
);
896 if (challenge
.length
!= 8) {
897 mux_printf(mux_id
, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
899 (int)challenge
.length
);
900 challenge
= data_blob(NULL
, 0);
902 } else if (strequal(request
, "NT-Response")) {
903 nt_response
= strhex_to_data_blob(NULL
, parameter
);
904 if (nt_response
.length
< 24) {
905 mux_printf(mux_id
, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
907 (int)nt_response
.length
);
908 nt_response
= data_blob(NULL
, 0);
910 } else if (strequal(request
, "LANMAN-Response")) {
911 lm_response
= strhex_to_data_blob(NULL
, parameter
);
912 if (lm_response
.length
!= 24) {
913 mux_printf(mux_id
, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
915 (int)lm_response
.length
);
916 lm_response
= data_blob(NULL
, 0);
918 } else if (strequal(request
, "Password")) {
919 plaintext_password
= smb_xstrdup(parameter
);
920 } else if (strequal(request
, "NT-Domain")) {
921 domain
= smb_xstrdup(parameter
);
922 } else if (strequal(request
, "Username")) {
923 username
= smb_xstrdup(parameter
);
924 } else if (strequal(request
, "Full-Username")) {
925 full_username
= smb_xstrdup(parameter
);
926 } else if (strequal(request
, "Request-User-Session-Key")) {
927 ntlm_server_1_user_session_key
= strequal(parameter
, "Yes");
928 } else if (strequal(request
, "Request-LanMan-Session-Key")) {
929 ntlm_server_1_lm_session_key
= strequal(parameter
, "Yes");
931 mux_printf(mux_id
, "Error: Unknown request %s\n.\n", request
);
935 static void manage_squid_request(struct loadparm_context
*lp_ctx
, enum stdio_helper_mode helper_mode
,
936 stdio_helper_function fn
, void **private2
)
939 char tmp
[INITIAL_BUFFER_SIZE
+1];
940 unsigned int mux_id
= 0;
941 int length
, buf_size
= 0;
944 unsigned int max_mux
;
945 void **private_pointers
;
948 static struct mux_private
*mux_private
;
949 static void *normal_private
;
952 buf
= talloc_strdup(NULL
, "");
955 DEBUG(0, ("Failed to allocate memory for reading the input "
957 x_fprintf(x_stdout
, "ERR\n");
962 /* this is not a typo - x_fgets doesn't work too well under
964 if (fgets(tmp
, INITIAL_BUFFER_SIZE
, stdin
) == NULL
) {
966 DEBUG(1, ("fgets() failed! dying..... errno=%d "
967 "(%s)\n", ferror(stdin
),
968 strerror(ferror(stdin
))));
970 exit(1); /* BIIG buffer */
975 buf
= talloc_strdup_append_buffer(buf
, tmp
);
976 buf_size
+= INITIAL_BUFFER_SIZE
;
978 if (buf_size
> MAX_BUFFER_SIZE
) {
979 DEBUG(0, ("Invalid Request (too large)\n"));
980 x_fprintf(x_stdout
, "ERR\n");
985 c
= strchr(buf
, '\n');
991 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf
,length
));
993 if (buf
[0] == '\0') {
994 DEBUG(0, ("Invalid Request (empty)\n"));
995 x_fprintf(x_stdout
, "ERR\n");
1000 if (opt_multiplex
) {
1001 if (sscanf(buf
, "%u ", &mux_id
) != 1) {
1002 DEBUG(0, ("Invalid Request - no multiplex id\n"));
1003 x_fprintf(x_stdout
, "ERR\n");
1008 mux_private
= talloc(NULL
, struct mux_private
);
1009 mux_private
->max_mux
= 0;
1010 mux_private
->private_pointers
= NULL
;
1015 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
1016 x_fprintf(x_stdout
, "ERR\n");
1021 if (mux_id
>= mux_private
->max_mux
) {
1022 unsigned int prev_max
= mux_private
->max_mux
;
1023 mux_private
->max_mux
= mux_id
+ 1;
1024 mux_private
->private_pointers
1025 = talloc_realloc(mux_private
,
1026 mux_private
->private_pointers
,
1027 void *, mux_private
->max_mux
);
1028 memset(&mux_private
->private_pointers
[prev_max
], '\0',
1029 (sizeof(*mux_private
->private_pointers
) * (mux_private
->max_mux
- prev_max
)));
1032 private1
= &mux_private
->private_pointers
[mux_id
];
1035 private1
= &normal_private
;
1038 fn(helper_mode
, lp_ctx
, c
, length
, private1
, mux_id
, private2
);
1042 static void squid_stream(struct loadparm_context
*lp_ctx
,
1043 enum stdio_helper_mode stdio_mode
,
1044 stdio_helper_function fn
) {
1045 /* initialize FDescs */
1046 x_setbuf(x_stdout
, NULL
);
1047 x_setbuf(x_stderr
, NULL
);
1049 manage_squid_request(lp_ctx
, stdio_mode
, fn
, NULL
);
1057 OPT_USERNAME
= 1000,
1066 OPT_USER_SESSION_KEY
,
1068 OPT_REQUIRE_MEMBERSHIP
,
1070 OPT_USE_CACHED_CREDS
,
1073 int main(int argc
, const char **argv
)
1075 static const char *helper_protocol
;
1080 /* NOTE: DO NOT change this interface without considering the implications!
1081 This is an external interface, which other programs will use to interact
1085 /* We do not use single-letter command abbreviations, because they harm future
1086 interface stability. */
1088 struct poptOption long_options
[] = {
1090 { "helper-protocol", 0, POPT_ARG_STRING
, &helper_protocol
, OPT_DOMAIN
, "operate as a stdio-based helper", "helper protocol to use"},
1091 { "domain", 0, POPT_ARG_STRING
, &opt_domain
, OPT_DOMAIN
, "domain name"},
1092 { "workstation", 0, POPT_ARG_STRING
, &opt_workstation
, OPT_WORKSTATION
, "workstation"},
1093 { "username", 0, POPT_ARG_STRING
, &opt_username
, OPT_PASSWORD
, "Username"},
1094 { "password", 0, POPT_ARG_STRING
, &opt_password
, OPT_PASSWORD
, "User's plaintext password"},
1095 { "multiplex", 0, POPT_ARG_NONE
, &opt_multiplex
, OPT_MULTIPLEX
, "Multiplex Mode"},
1096 { "use-cached-creds", 0, POPT_ARG_NONE
, &use_cached_creds
, OPT_USE_CACHED_CREDS
, "silently ignored for compatibility reasons"},
1102 /* Samba client initialisation */
1104 setup_logging(NULL
, DEBUG_STDERR
);
1108 pc
= poptGetContext("ntlm_auth", argc
, argv
, long_options
, 0);
1110 /* Parse command line options */
1113 poptPrintHelp(pc
, stderr
, 0);
1117 pc
= poptGetContext(NULL
, argc
, (const char **)argv
, long_options
,
1118 POPT_CONTEXT_KEEP_FIRST
);
1120 while((opt
= poptGetNextOpt(pc
)) != -1) {
1126 fprintf(stderr
, "%s: %s\n",
1127 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
1132 gensec_init(cmdline_lp_ctx
);
1134 if (opt_domain
== NULL
) {
1135 opt_domain
= lp_workgroup(cmdline_lp_ctx
);
1138 if (helper_protocol
) {
1140 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
1141 if (strcmp(helper_protocol
, stdio_helper_protocols
[i
].name
) == 0) {
1142 squid_stream(cmdline_lp_ctx
, stdio_helper_protocols
[i
].mode
, stdio_helper_protocols
[i
].fn
);
1146 x_fprintf(x_stderr
, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol
);
1148 for (i
=0; i
<NUM_HELPER_MODES
; i
++) {
1149 x_fprintf(x_stderr
, "%s\n", stdio_helper_protocols
[i
].name
);
1155 if (!opt_username
) {
1156 x_fprintf(x_stderr
, "username must be specified!\n\n");
1157 poptPrintHelp(pc
, stderr
, 0);
1161 if (opt_workstation
== NULL
) {
1162 opt_workstation
= lp_netbios_name(cmdline_lp_ctx
);
1165 if (!opt_password
) {
1166 opt_password
= getpass("password: ");
1172 asprintf(&user
, "%s%c%s", opt_domain
, *lp_winbind_separator(cmdline_lp_ctx
), opt_username
);
1173 if (!check_plaintext_auth(user
, opt_password
, true)) {
1180 poptFreeContext(pc
);