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 "auth/ntlm/ntlm_check.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 **private1,
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 **private1,
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 **private1,
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 **private1,
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, char **domain,
130 char **user, char winbind_separator)
133 char *p = strchr(domuser, winbind_separator);
139 *user = smb_xstrdup(p+1);
140 *domain = smb_xstrdup(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,
215 lp_lanman_auth(lp_ctx),
216 lp_ntlm_auth(lp_ctx),
217 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
218 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
225 lm_pwd, nt_pwd, user_session_key, lm_session_key);
227 if (NT_STATUS_IS_OK(nt_status)) {
231 *lp_winbind_separator(lp_ctx),
235 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
236 domain, username, workstation,
237 nt_errstr(nt_status)));
239 talloc_free(mem_ctx);
242 *error_string = strdup(nt_errstr(nt_status));
249 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
250 struct loadparm_context *lp_ctx,
251 char *buf, int length, void **private1,
252 unsigned int mux_id, void **private2)
257 pass = memchr(buf, ' ', length);
259 DEBUG(2, ("Password not found. Denying access\n"));
260 mux_printf(mux_id, "ERR\n");
266 if (stdio_helper_mode == SQUID_2_5_BASIC) {
267 rfc1738_unescape(user);
268 rfc1738_unescape(pass);
271 if (check_plaintext_auth(user, pass, false)) {
272 mux_printf(mux_id, "OK\n");
274 mux_printf(mux_id, "ERR\n");
278 /* This is a bit hairy, but the basic idea is to do a password callback
279 to the calling application. The callback comes from within gensec */
281 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
282 struct loadparm_context *lp_ctx,
283 char *buf, int length, void **private1,
284 unsigned int mux_id, void **password)
287 if (strlen(buf) < 2) {
288 DEBUG(1, ("query [%s] invalid", buf));
289 mux_printf(mux_id, "BH Query invalid\n");
293 if (strlen(buf) > 3) {
294 in = base64_decode_data_blob(NULL, buf + 3);
296 in = data_blob(NULL, 0);
299 if (strncmp(buf, "PW ", 3) == 0) {
301 *password = talloc_strndup(*private1 /* hopefully the right gensec context, useful to use for talloc */,
302 (const char *)in.data, in.length);
304 if (*password == NULL) {
305 DEBUG(1, ("Out of memory\n"));
306 mux_printf(mux_id, "BH Out of memory\n");
311 mux_printf(mux_id, "OK\n");
315 DEBUG(1, ("Asked for (and expected) a password\n"));
316 mux_printf(mux_id, "BH Expected a password\n");
321 * Callback for password credentials. This is not async, and when
322 * GENSEC and the credentials code is made async, it will look rather
326 static const char *get_password(struct cli_credentials *credentials)
328 char *password = NULL;
330 /* Ask for a password */
331 mux_printf((unsigned int)(uintptr_t)credentials->priv_data, "PW\n");
332 credentials->priv_data = NULL;
334 manage_squid_request(cmdline_lp_ctx, NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
339 Check if a string is part of a list.
341 static bool in_list(const char *s, const char *list, bool casesensitive)
344 size_t tok_len = 1024;
350 tok = (char *)malloc(tok_len);
355 while (next_token(&p, tok, LIST_SEP, tok_len)) {
356 if ((casesensitive?strcmp:strcasecmp_m)(tok,s) == 0) {
365 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
367 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, true)) {
368 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
369 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
371 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, true)) {
372 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
373 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
375 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, true)) {
376 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
377 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
381 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
382 struct loadparm_context *lp_ctx,
383 char *buf, int length, void **private1,
384 unsigned int mux_id, void **private2)
387 DATA_BLOB out = data_blob(NULL, 0);
388 char *out_base64 = NULL;
389 const char *reply_arg = NULL;
390 struct gensec_ntlm_state {
391 struct gensec_security *gensec_state;
392 const char *set_password;
394 struct gensec_ntlm_state *state;
395 struct tevent_context *ev;
396 struct messaging_context *msg;
400 const char *reply_code;
401 struct cli_credentials *creds;
403 static char *want_feature_list = NULL;
404 static DATA_BLOB session_key;
409 state = (struct gensec_ntlm_state *)*private1;
411 state = talloc_zero(NULL, struct gensec_ntlm_state);
413 mux_printf(mux_id, "BH No Memory\n");
418 state->set_password = opt_password;
422 if (strlen(buf) < 2) {
423 DEBUG(1, ("query [%s] invalid", buf));
424 mux_printf(mux_id, "BH Query invalid\n");
428 if (strlen(buf) > 3) {
429 if(strncmp(buf, "SF ", 3) == 0) {
430 DEBUG(10, ("Setting flags to negotiate\n"));
431 talloc_free(want_feature_list);
432 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
433 mux_printf(mux_id, "OK\n");
436 in = base64_decode_data_blob(NULL, buf + 3);
438 in = data_blob(NULL, 0);
441 if (strncmp(buf, "YR", 2) == 0) {
442 if (state->gensec_state) {
443 talloc_free(state->gensec_state);
444 state->gensec_state = NULL;
446 } else if ( (strncmp(buf, "OK", 2) == 0)) {
447 /* Just return BH, like ntlm_auth from Samba 3 does. */
448 mux_printf(mux_id, "BH Command expected\n");
451 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
452 (strncmp(buf, "KK ", 3) != 0) &&
453 (strncmp(buf, "AF ", 3) != 0) &&
454 (strncmp(buf, "NA ", 3) != 0) &&
455 (strncmp(buf, "UG", 2) != 0) &&
456 (strncmp(buf, "PW ", 3) != 0) &&
457 (strncmp(buf, "GK", 2) != 0) &&
458 (strncmp(buf, "GF", 2) != 0)) {
459 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
460 mux_printf(mux_id, "BH SPNEGO request invalid\n");
465 ev = s4_event_context_init(state);
470 if (!(state->gensec_state)) {
471 switch (stdio_helper_mode) {
472 case GSS_SPNEGO_CLIENT:
473 case NTLMSSP_CLIENT_1:
474 /* setup the client side */
476 nt_status = gensec_client_start(NULL, &state->gensec_state, ev,
477 lp_gensec_settings(NULL, lp_ctx));
478 if (!NT_STATUS_IS_OK(nt_status)) {
483 case GSS_SPNEGO_SERVER:
484 case SQUID_2_5_NTLMSSP:
485 msg = messaging_client_init(state, lp_messaging_path(state, lp_ctx),
486 lp_iconv_convenience(lp_ctx), ev);
490 if (!NT_STATUS_IS_OK(gensec_server_start(state, ev, lp_gensec_settings(state, lp_ctx),
491 msg, &state->gensec_state))) {
499 creds = cli_credentials_init(state->gensec_state);
500 cli_credentials_set_conf(creds, lp_ctx);
502 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
505 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
507 if (state->set_password) {
508 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
510 cli_credentials_set_password_callback(creds, get_password);
511 creds->priv_data = (void*)(uintptr_t)mux_id;
513 if (opt_workstation) {
514 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
517 switch (stdio_helper_mode) {
518 case GSS_SPNEGO_SERVER:
519 case SQUID_2_5_NTLMSSP:
520 cli_credentials_set_machine_account(creds, lp_ctx);
526 gensec_set_credentials(state->gensec_state, creds);
527 gensec_want_feature_list(state->gensec_state, want_feature_list);
529 switch (stdio_helper_mode) {
530 case GSS_SPNEGO_CLIENT:
531 case GSS_SPNEGO_SERVER:
532 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
537 case NTLMSSP_CLIENT_1:
542 case SQUID_2_5_NTLMSSP:
543 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
549 if (!NT_STATUS_IS_OK(nt_status)) {
550 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
551 mux_printf(mux_id, "BH GENSEC mech failed to start\n");
558 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
560 if (strncmp(buf, "PW ", 3) == 0) {
561 state->set_password = talloc_strndup(state,
562 (const char *)in.data,
565 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
568 mux_printf(mux_id, "OK\n");
570 talloc_free(mem_ctx);
574 if (strncmp(buf, "UG", 2) == 0) {
576 char *grouplist = NULL;
577 struct auth_session_info *session_info;
579 nt_status = gensec_session_info(state->gensec_state, &session_info);
580 if (!NT_STATUS_IS_OK(nt_status)) {
581 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
582 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
584 talloc_free(mem_ctx);
588 /* get the string onto the context */
589 grouplist = talloc_strdup(mem_ctx, "");
591 for (i=0; i<session_info->security_token->num_sids; i++) {
592 struct security_token *token = session_info->security_token;
593 const char *sidstr = dom_sid_string(session_info,
595 grouplist = talloc_asprintf_append_buffer(grouplist, "%s,", sidstr);
598 mux_printf(mux_id, "GL %s\n", grouplist);
599 talloc_free(session_info);
601 talloc_free(mem_ctx);
605 if (strncmp(buf, "GK", 2) == 0) {
607 DEBUG(10, ("Requested session key\n"));
608 nt_status = gensec_session_key(state->gensec_state, &session_key);
609 if(!NT_STATUS_IS_OK(nt_status)) {
610 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
611 mux_printf(mux_id, "BH No session key\n");
612 talloc_free(mem_ctx);
615 base64_key = base64_encode_data_blob(state, session_key);
616 mux_printf(mux_id, "GK %s\n", base64_key);
617 talloc_free(base64_key);
619 talloc_free(mem_ctx);
623 if (strncmp(buf, "GF", 2) == 0) {
624 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
627 gensec_ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
628 struct gensec_ntlmssp_state);
629 neg_flags = gensec_ntlmssp_state->neg_flags;
631 DEBUG(10, ("Requested negotiated feature flags\n"));
632 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
636 nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
638 /* don't leak 'bad password'/'no such user' info to the network client */
639 nt_status = auth_nt_status_squash(nt_status);
642 out_base64 = base64_encode_data_blob(mem_ctx, out);
647 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
651 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
653 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
660 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
661 reply_code = "BH NT_STATUS_ACCESS_DENIED";
662 reply_arg = nt_errstr(nt_status);
663 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
664 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
665 reply_code = "BH NT_STATUS_UNSUCCESSFUL";
666 reply_arg = nt_errstr(nt_status);
667 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
668 } else if (!NT_STATUS_IS_OK(nt_status)) {
670 reply_arg = nt_errstr(nt_status);
671 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
672 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
673 struct auth_session_info *session_info;
675 nt_status = gensec_session_info(state->gensec_state, &session_info);
676 if (!NT_STATUS_IS_OK(nt_status)) {
677 reply_code = "BH Failed to retrive session info";
678 reply_arg = nt_errstr(nt_status);
679 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
683 reply_arg = talloc_asprintf(state->gensec_state,
684 "%s%s%s", session_info->server_info->domain_name,
685 lp_winbind_separator(lp_ctx), session_info->server_info->account_name);
686 talloc_free(session_info);
688 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
690 reply_arg = out_base64;
695 switch (stdio_helper_mode) {
696 case GSS_SPNEGO_SERVER:
697 mux_printf(mux_id, "%s %s %s\n", reply_code,
698 out_base64 ? out_base64 : "*",
699 reply_arg ? reply_arg : "*");
703 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
704 } else if (reply_arg) {
705 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
707 mux_printf(mux_id, "%s\n", reply_code);
711 talloc_free(mem_ctx);
715 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
716 struct loadparm_context *lp_ctx,
717 char *buf, int length, void **private1,
718 unsigned int mux_id, void **private2)
720 char *request, *parameter;
721 static DATA_BLOB challenge;
722 static DATA_BLOB lm_response;
723 static DATA_BLOB nt_response;
724 static char *full_username;
725 static char *username;
727 static char *plaintext_password;
728 static bool ntlm_server_1_user_session_key;
729 static bool ntlm_server_1_lm_session_key;
731 if (strequal(buf, ".")) {
732 if (!full_username && !username) {
733 mux_printf(mux_id, "Error: No username supplied!\n");
734 } else if (plaintext_password) {
735 /* handle this request as plaintext */
736 if (!full_username) {
737 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(lp_ctx), username) == -1) {
738 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
742 if (check_plaintext_auth(full_username, plaintext_password, false)) {
743 mux_printf(mux_id, "Authenticated: Yes\n");
745 mux_printf(mux_id, "Authenticated: No\n");
747 } else if (!lm_response.data && !nt_response.data) {
748 mux_printf(mux_id, "Error: No password supplied!\n");
749 } else if (!challenge.data) {
750 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
752 char *error_string = NULL;
754 DATA_BLOB user_session_key;
757 if (full_username && !username) {
760 if (!parse_ntlm_auth_domain_user(full_username, &username,
762 *lp_winbind_separator(lp_ctx))) {
763 /* username might be 'tainted', don't print into our new-line deleimianted stream */
764 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
769 domain = smb_xstrdup(lp_workgroup(lp_ctx));
772 if (ntlm_server_1_lm_session_key)
773 flags |= NTLM_AUTH_FLAG_LMKEY;
775 if (ntlm_server_1_user_session_key)
776 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
778 if (!NT_STATUS_IS_OK(
779 local_pw_check_specified(lp_ctx,
782 lp_netbios_name(lp_ctx),
792 mux_printf(mux_id, "Authenticated: No\n");
793 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
794 SAFE_FREE(error_string);
796 static char zeros[16];
798 char *hex_user_session_key;
800 mux_printf(mux_id, "Authenticated: Yes\n");
802 if (ntlm_server_1_lm_session_key
804 && (memcmp(zeros, lm_key.data,
805 lm_key.length) != 0)) {
806 hex_encode(lm_key.data,
809 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
810 SAFE_FREE(hex_lm_key);
813 if (ntlm_server_1_user_session_key
814 && user_session_key.length
815 && (memcmp(zeros, user_session_key.data,
816 user_session_key.length) != 0)) {
817 hex_encode(user_session_key.data,
818 user_session_key.length,
819 &hex_user_session_key);
820 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
821 SAFE_FREE(hex_user_session_key);
825 /* clear out the state */
826 challenge = data_blob(NULL, 0);
827 nt_response = data_blob(NULL, 0);
828 lm_response = data_blob(NULL, 0);
829 SAFE_FREE(full_username);
832 SAFE_FREE(plaintext_password);
833 ntlm_server_1_user_session_key = false;
834 ntlm_server_1_lm_session_key = false;
835 mux_printf(mux_id, ".\n");
842 /* Indicates a base64 encoded structure */
843 parameter = strstr(request, ":: ");
845 parameter = strstr(request, ": ");
848 DEBUG(0, ("Parameter not found!\n"));
849 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
866 base64_decode_inplace(parameter);
869 if (strequal(request, "LANMAN-Challenge")) {
870 challenge = strhex_to_data_blob(NULL, parameter);
871 if (challenge.length != 8) {
872 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
874 (int)challenge.length);
875 challenge = data_blob(NULL, 0);
877 } else if (strequal(request, "NT-Response")) {
878 nt_response = strhex_to_data_blob(NULL, parameter);
879 if (nt_response.length < 24) {
880 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
882 (int)nt_response.length);
883 nt_response = data_blob(NULL, 0);
885 } else if (strequal(request, "LANMAN-Response")) {
886 lm_response = strhex_to_data_blob(NULL, parameter);
887 if (lm_response.length != 24) {
888 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
890 (int)lm_response.length);
891 lm_response = data_blob(NULL, 0);
893 } else if (strequal(request, "Password")) {
894 plaintext_password = smb_xstrdup(parameter);
895 } else if (strequal(request, "NT-Domain")) {
896 domain = smb_xstrdup(parameter);
897 } else if (strequal(request, "Username")) {
898 username = smb_xstrdup(parameter);
899 } else if (strequal(request, "Full-Username")) {
900 full_username = smb_xstrdup(parameter);
901 } else if (strequal(request, "Request-User-Session-Key")) {
902 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
903 } else if (strequal(request, "Request-LanMan-Session-Key")) {
904 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
906 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
910 static void manage_squid_request(struct loadparm_context *lp_ctx, enum stdio_helper_mode helper_mode,
911 stdio_helper_function fn, void **private2)
914 char tmp[INITIAL_BUFFER_SIZE+1];
915 unsigned int mux_id = 0;
916 int length, buf_size = 0;
919 unsigned int max_mux;
920 void **private_pointers;
923 static struct mux_private *mux_private;
924 static void *normal_private;
927 buf = talloc_strdup(NULL, "");
930 DEBUG(0, ("Failed to allocate memory for reading the input "
932 x_fprintf(x_stdout, "ERR\n");
937 /* this is not a typo - x_fgets doesn't work too well under
939 if (fgets(tmp, INITIAL_BUFFER_SIZE, stdin) == NULL) {
941 DEBUG(1, ("fgets() failed! dying..... errno=%d "
942 "(%s)\n", ferror(stdin),
943 strerror(ferror(stdin))));
945 exit(1); /* BIIG buffer */
950 buf = talloc_strdup_append_buffer(buf, tmp);
951 buf_size += INITIAL_BUFFER_SIZE;
953 if (buf_size > MAX_BUFFER_SIZE) {
954 DEBUG(0, ("Invalid Request (too large)\n"));
955 x_fprintf(x_stdout, "ERR\n");
960 c = strchr(buf, '\n');
966 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
968 if (buf[0] == '\0') {
969 DEBUG(0, ("Invalid Request (empty)\n"));
970 x_fprintf(x_stdout, "ERR\n");
976 if (sscanf(buf, "%u ", &mux_id) != 1) {
977 DEBUG(0, ("Invalid Request - no multiplex id\n"));
978 x_fprintf(x_stdout, "ERR\n");
983 mux_private = talloc(NULL, struct mux_private);
984 mux_private->max_mux = 0;
985 mux_private->private_pointers = NULL;
990 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
991 x_fprintf(x_stdout, "ERR\n");
996 if (mux_id >= mux_private->max_mux) {
997 unsigned int prev_max = mux_private->max_mux;
998 mux_private->max_mux = mux_id + 1;
999 mux_private->private_pointers
1000 = talloc_realloc(mux_private,
1001 mux_private->private_pointers,
1002 void *, mux_private->max_mux);
1003 memset(&mux_private->private_pointers[prev_max], '\0',
1004 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
1007 private1 = &mux_private->private_pointers[mux_id];
1010 private1 = &normal_private;
1013 fn(helper_mode, lp_ctx, c, length, private1, mux_id, private2);
1017 static void squid_stream(struct loadparm_context *lp_ctx,
1018 enum stdio_helper_mode stdio_mode,
1019 stdio_helper_function fn) {
1020 /* initialize FDescs */
1021 x_setbuf(x_stdout, NULL);
1022 x_setbuf(x_stderr, NULL);
1024 manage_squid_request(lp_ctx, stdio_mode, fn, NULL);
1032 OPT_USERNAME = 1000,
1041 OPT_USER_SESSION_KEY,
1043 OPT_REQUIRE_MEMBERSHIP,
1045 OPT_USE_CACHED_CREDS,
1048 int main(int argc, const char **argv)
1050 static const char *helper_protocol;
1055 /* NOTE: DO NOT change this interface without considering the implications!
1056 This is an external interface, which other programs will use to interact
1060 /* We do not use single-letter command abbreviations, because they harm future
1061 interface stability. */
1063 struct poptOption long_options[] = {
1065 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1066 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1067 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1068 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1069 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1070 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1071 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1077 /* Samba client initialisation */
1079 setup_logging(NULL, DEBUG_STDERR);
1083 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1085 /* Parse command line options */
1088 poptPrintHelp(pc, stderr, 0);
1092 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1093 POPT_CONTEXT_KEEP_FIRST);
1095 while((opt = poptGetNextOpt(pc)) != -1) {
1101 fprintf(stderr, "%s: %s\n",
1102 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1107 gensec_init(cmdline_lp_ctx);
1109 if (opt_domain == NULL) {
1110 opt_domain = lp_workgroup(cmdline_lp_ctx);
1113 if (helper_protocol) {
1115 for (i=0; i<NUM_HELPER_MODES; i++) {
1116 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1117 squid_stream(cmdline_lp_ctx, stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1121 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1123 for (i=0; i<NUM_HELPER_MODES; i++) {
1124 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1130 if (!opt_username) {
1131 x_fprintf(x_stderr, "username must be specified!\n\n");
1132 poptPrintHelp(pc, stderr, 0);
1136 if (opt_workstation == NULL) {
1137 opt_workstation = lp_netbios_name(cmdline_lp_ctx);
1140 if (!opt_password) {
1141 opt_password = getpass("password: ");
1147 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(cmdline_lp_ctx), opt_username);
1148 if (!check_plaintext_auth(user, opt_password, true)) {
1155 poptFreeContext(pc);