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"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
31 #include "auth/auth.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #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 **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;
107 static int opt_allow_mschapv2;
110 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
112 static void mux_printf(unsigned int mux_id, const char *format, ...)
117 x_fprintf(x_stdout, "%d ", mux_id);
120 va_start(ap, format);
121 x_vfprintf(x_stdout, format, ap);
127 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
128 form DOMAIN/user into a domain and a user */
130 static bool parse_ntlm_auth_domain_user(const char *domuser, char **domain,
131 char **user, char winbind_separator)
134 char *p = strchr(domuser, winbind_separator);
140 *user = smb_xstrdup(p+1);
141 *domain = smb_xstrdup(domuser);
142 (*domain)[PTR_DIFF(p, domuser)] = 0;
148 /* Authenticate a user with a plaintext password */
150 static bool check_plaintext_auth(const char *user, const char *pass,
151 bool stdout_diagnostics)
153 return (strcmp(pass, opt_password) == 0);
156 /* authenticate a user with an encrypted username/password */
158 static NTSTATUS local_pw_check_specified(struct loadparm_context *lp_ctx,
159 const char *username,
161 const char *workstation,
162 const DATA_BLOB *challenge,
163 const DATA_BLOB *lm_response,
164 const DATA_BLOB *nt_response,
166 DATA_BLOB *lm_session_key,
167 DATA_BLOB *user_session_key,
172 struct samr_Password lm_pw, nt_pw;
173 struct samr_Password *lm_pwd, *nt_pwd;
174 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
176 nt_status = NT_STATUS_NO_MEMORY;
178 uint32_t logon_parameters = 0;
180 E_md4hash(opt_password, nt_pw.hash);
181 if (E_deshash(opt_password, lm_pw.hash)) {
188 if (opt_allow_mschapv2) {
189 logon_parameters |= MSV1_0_ALLOW_MSVCHAPV2;
192 nt_status = ntlm_password_check(mem_ctx,
193 lpcfg_lanman_auth(lp_ctx),
194 lpcfg_ntlm_auth(lp_ctx),
196 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
197 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
204 lm_pwd, nt_pwd, user_session_key, lm_session_key);
206 if (NT_STATUS_IS_OK(nt_status)) {
208 if (asprintf(unix_name, "%s%c%s", domain,
209 *lpcfg_winbind_separator(lp_ctx),
211 nt_status = NT_STATUS_NO_MEMORY;
215 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
216 domain, username, workstation,
217 nt_errstr(nt_status)));
219 talloc_free(mem_ctx);
222 *error_string = strdup(nt_errstr(nt_status));
229 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
230 struct loadparm_context *lp_ctx,
231 char *buf, int length, void **private1,
232 unsigned int mux_id, void **private2)
237 pass = memchr(buf, ' ', length);
239 DEBUG(2, ("Password not found. Denying access\n"));
240 mux_printf(mux_id, "ERR\n");
246 if (stdio_helper_mode == SQUID_2_5_BASIC) {
247 rfc1738_unescape(user);
248 rfc1738_unescape(pass);
251 if (check_plaintext_auth(user, pass, false)) {
252 mux_printf(mux_id, "OK\n");
254 mux_printf(mux_id, "ERR\n");
258 /* This is a bit hairy, but the basic idea is to do a password callback
259 to the calling application. The callback comes from within gensec */
261 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
262 struct loadparm_context *lp_ctx,
263 char *buf, int length, void **private1,
264 unsigned int mux_id, void **password)
267 if (strlen(buf) < 2) {
268 DEBUG(1, ("query [%s] invalid", buf));
269 mux_printf(mux_id, "BH Query invalid\n");
273 if (strlen(buf) > 3) {
274 in = base64_decode_data_blob(buf + 3);
276 in = data_blob(NULL, 0);
279 if (strncmp(buf, "PW ", 3) == 0) {
281 *password = talloc_strndup(*private1 /* hopefully the right gensec context, useful to use for talloc */,
282 (const char *)in.data, in.length);
284 if (*password == NULL) {
285 DEBUG(1, ("Out of memory\n"));
286 mux_printf(mux_id, "BH Out of memory\n");
291 mux_printf(mux_id, "OK\n");
295 DEBUG(1, ("Asked for (and expected) a password\n"));
296 mux_printf(mux_id, "BH Expected a password\n");
301 * Callback for password credentials. This is not async, and when
302 * GENSEC and the credentials code is made async, it will look rather
306 static const char *get_password(struct cli_credentials *credentials)
308 char *password = NULL;
309 void *cb = cli_credentials_callback_data_void(credentials);
311 /* Ask for a password */
312 mux_printf((unsigned int)(uintptr_t)cb, "PW\n");
313 cli_credentials_set_callback_data(credentials, NULL);
315 manage_squid_request(cmdline_lp_ctx, NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
320 Check if a string is part of a list.
322 static bool in_list(const char *s, const char *list, bool casesensitive)
325 size_t tok_len = 1024;
331 tok = (char *)malloc(tok_len);
336 while (next_token(&p, tok, LIST_SEP, tok_len)) {
337 if ((casesensitive?strcmp:strcasecmp_m)(tok,s) == 0) {
346 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
348 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, true)) {
349 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
350 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
352 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, true)) {
353 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
354 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
356 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, true)) {
357 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
358 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
362 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
363 struct loadparm_context *lp_ctx,
364 char *buf, int length, void **private1,
365 unsigned int mux_id, void **private2)
368 DATA_BLOB out = data_blob(NULL, 0);
369 char *out_base64 = NULL;
370 const char *reply_arg = NULL;
371 struct gensec_ntlm_state {
372 struct gensec_security *gensec_state;
373 const char *set_password;
375 struct gensec_ntlm_state *state;
376 struct tevent_context *ev;
377 struct imessaging_context *msg;
381 const char *reply_code;
382 struct cli_credentials *creds;
384 static char *want_feature_list = NULL;
385 static DATA_BLOB session_key;
390 state = (struct gensec_ntlm_state *)*private1;
392 state = talloc_zero(NULL, struct gensec_ntlm_state);
394 mux_printf(mux_id, "BH No Memory\n");
399 state->set_password = opt_password;
403 if (strlen(buf) < 2) {
404 DEBUG(1, ("query [%s] invalid", buf));
405 mux_printf(mux_id, "BH Query invalid\n");
409 if (strlen(buf) > 3) {
410 if(strncmp(buf, "SF ", 3) == 0) {
411 DEBUG(10, ("Setting flags to negotiate\n"));
412 talloc_free(want_feature_list);
413 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
414 mux_printf(mux_id, "OK\n");
417 in = base64_decode_data_blob(buf + 3);
419 in = data_blob(NULL, 0);
422 if (strncmp(buf, "YR", 2) == 0) {
423 if (state->gensec_state) {
424 talloc_free(state->gensec_state);
425 state->gensec_state = NULL;
427 } else if ( (strncmp(buf, "OK", 2) == 0)) {
428 /* Just return BH, like ntlm_auth from Samba 3 does. */
429 mux_printf(mux_id, "BH Command expected\n");
432 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
433 (strncmp(buf, "KK ", 3) != 0) &&
434 (strncmp(buf, "AF ", 3) != 0) &&
435 (strncmp(buf, "NA ", 3) != 0) &&
436 (strncmp(buf, "UG", 2) != 0) &&
437 (strncmp(buf, "PW ", 3) != 0) &&
438 (strncmp(buf, "GK", 2) != 0) &&
439 (strncmp(buf, "GF", 2) != 0)) {
440 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
441 mux_printf(mux_id, "BH SPNEGO request invalid\n");
446 ev = s4_event_context_init(state);
451 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
454 if (!(state->gensec_state)) {
455 switch (stdio_helper_mode) {
456 case GSS_SPNEGO_CLIENT:
457 case NTLMSSP_CLIENT_1:
458 /* setup the client side */
460 nt_status = gensec_client_start(NULL, &state->gensec_state,
461 lpcfg_gensec_settings(NULL, lp_ctx));
462 if (!NT_STATUS_IS_OK(nt_status)) {
463 talloc_free(mem_ctx);
468 case GSS_SPNEGO_SERVER:
469 case SQUID_2_5_NTLMSSP:
471 const char *winbind_method[] = { "winbind", NULL };
472 struct auth4_context *auth_context;
474 msg = imessaging_client_init(state, lp_ctx, ev);
476 talloc_free(mem_ctx);
479 nt_status = auth_context_create_methods(mem_ctx,
487 if (!NT_STATUS_IS_OK(nt_status)) {
488 talloc_free(mem_ctx);
492 if (!NT_STATUS_IS_OK(gensec_server_start(state,
493 lpcfg_gensec_settings(state, lp_ctx),
494 auth_context, &state->gensec_state))) {
495 talloc_free(mem_ctx);
501 talloc_free(mem_ctx);
505 creds = cli_credentials_init(state->gensec_state);
506 cli_credentials_set_conf(creds, lp_ctx);
508 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
511 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
513 if (state->set_password) {
514 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
516 void *cb = (void*)(uintptr_t)mux_id;
517 cli_credentials_set_callback_data(creds, cb);
518 cli_credentials_set_password_callback(creds, get_password);
520 if (opt_workstation) {
521 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
524 switch (stdio_helper_mode) {
525 case GSS_SPNEGO_SERVER:
526 case SQUID_2_5_NTLMSSP:
527 cli_credentials_set_machine_account(creds, lp_ctx);
533 gensec_set_credentials(state->gensec_state, creds);
534 gensec_want_feature_list(state->gensec_state, want_feature_list);
536 switch (stdio_helper_mode) {
537 case GSS_SPNEGO_CLIENT:
538 case GSS_SPNEGO_SERVER:
539 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
544 case NTLMSSP_CLIENT_1:
549 case SQUID_2_5_NTLMSSP:
550 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
553 talloc_free(mem_ctx);
557 if (!NT_STATUS_IS_OK(nt_status)) {
558 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
559 mux_printf(mux_id, "BH GENSEC mech failed to start\n");
560 talloc_free(mem_ctx);
568 if (strncmp(buf, "PW ", 3) == 0) {
569 state->set_password = talloc_strndup(state,
570 (const char *)in.data,
573 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
576 mux_printf(mux_id, "OK\n");
578 talloc_free(mem_ctx);
582 if (strncmp(buf, "UG", 2) == 0) {
584 char *grouplist = NULL;
585 struct auth_session_info *session_info;
587 nt_status = gensec_session_info(state->gensec_state, mem_ctx, &session_info);
588 if (!NT_STATUS_IS_OK(nt_status)) {
589 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
590 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
592 talloc_free(mem_ctx);
596 /* get the string onto the context */
597 grouplist = talloc_strdup(mem_ctx, "");
599 for (i=0; i<session_info->security_token->num_sids; i++) {
600 struct security_token *token = session_info->security_token;
601 const char *sidstr = dom_sid_string(session_info,
603 grouplist = talloc_asprintf_append_buffer(grouplist, "%s,", sidstr);
606 mux_printf(mux_id, "GL %s\n", grouplist);
607 talloc_free(session_info);
609 talloc_free(mem_ctx);
613 if (strncmp(buf, "GK", 2) == 0) {
615 DEBUG(10, ("Requested session key\n"));
616 nt_status = gensec_session_key(state->gensec_state, mem_ctx, &session_key);
617 if(!NT_STATUS_IS_OK(nt_status)) {
618 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
619 mux_printf(mux_id, "BH No session key\n");
620 talloc_free(mem_ctx);
623 base64_key = base64_encode_data_blob(state, session_key);
624 mux_printf(mux_id, "GK %s\n", base64_key);
625 talloc_free(base64_key);
627 talloc_free(mem_ctx);
631 if (strncmp(buf, "GF", 2) == 0) {
632 struct ntlmssp_state *ntlmssp_state;
635 ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
636 struct ntlmssp_state);
637 neg_flags = ntlmssp_state->neg_flags;
639 DEBUG(10, ("Requested negotiated feature flags\n"));
640 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
644 nt_status = gensec_update_ev(state->gensec_state, mem_ctx, ev, in, &out);
646 /* don't leak 'bad password'/'no such user' info to the network client */
647 nt_status = nt_status_squash(nt_status);
650 out_base64 = base64_encode_data_blob(mem_ctx, out);
655 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
659 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
661 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
668 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
669 reply_code = "BH NT_STATUS_ACCESS_DENIED";
670 reply_arg = nt_errstr(nt_status);
671 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
672 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
673 reply_code = "BH NT_STATUS_UNSUCCESSFUL";
674 reply_arg = nt_errstr(nt_status);
675 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
676 } else if (!NT_STATUS_IS_OK(nt_status)) {
678 reply_arg = nt_errstr(nt_status);
679 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
680 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
681 struct auth_session_info *session_info;
683 nt_status = gensec_session_info(state->gensec_state, mem_ctx, &session_info);
684 if (!NT_STATUS_IS_OK(nt_status)) {
685 reply_code = "BH Failed to retrive session info";
686 reply_arg = nt_errstr(nt_status);
687 DEBUG(1, ("GENSEC failed to retrieve the session info: %s\n", nt_errstr(nt_status)));
691 reply_arg = talloc_asprintf(state->gensec_state,
692 "%s%s%s", session_info->info->domain_name,
693 lpcfg_winbind_separator(lp_ctx), session_info->info->account_name);
694 talloc_free(session_info);
696 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
698 reply_arg = out_base64;
703 switch (stdio_helper_mode) {
704 case GSS_SPNEGO_SERVER:
705 mux_printf(mux_id, "%s %s %s\n", reply_code,
706 out_base64 ? out_base64 : "*",
707 reply_arg ? reply_arg : "*");
711 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
712 } else if (reply_arg) {
713 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
715 mux_printf(mux_id, "%s\n", reply_code);
719 talloc_free(mem_ctx);
723 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
724 struct loadparm_context *lp_ctx,
725 char *buf, int length, void **private1,
726 unsigned int mux_id, void **private2)
728 char *request, *parameter;
729 static DATA_BLOB challenge;
730 static DATA_BLOB lm_response;
731 static DATA_BLOB nt_response;
732 static char *full_username;
733 static char *username;
735 static char *plaintext_password;
736 static bool ntlm_server_1_user_session_key;
737 static bool ntlm_server_1_lm_session_key;
739 if (strequal(buf, ".")) {
740 if (!full_username && !username) {
741 mux_printf(mux_id, "Error: No username supplied!\n");
742 } else if (plaintext_password) {
743 /* handle this request as plaintext */
744 if (!full_username) {
745 if (asprintf(&full_username, "%s%c%s", domain, *lpcfg_winbind_separator(lp_ctx), username) < 0) {
746 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
750 if (check_plaintext_auth(full_username, plaintext_password, false)) {
751 mux_printf(mux_id, "Authenticated: Yes\n");
753 mux_printf(mux_id, "Authenticated: No\n");
755 } else if (!lm_response.data && !nt_response.data) {
756 mux_printf(mux_id, "Error: No password supplied!\n");
757 } else if (!challenge.data) {
758 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
760 char *error_string = NULL;
762 DATA_BLOB user_session_key;
765 if (full_username && !username) {
768 if (!parse_ntlm_auth_domain_user(full_username, &username,
770 *lpcfg_winbind_separator(lp_ctx))) {
771 /* username might be 'tainted', don't print into our new-line deleimianted stream */
772 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
777 domain = smb_xstrdup(lpcfg_workgroup(lp_ctx));
780 if (ntlm_server_1_lm_session_key)
781 flags |= NTLM_AUTH_FLAG_LMKEY;
783 if (ntlm_server_1_user_session_key)
784 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
786 if (!NT_STATUS_IS_OK(
787 local_pw_check_specified(lp_ctx,
790 lpcfg_netbios_name(lp_ctx),
800 mux_printf(mux_id, "Authenticated: No\n");
801 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
802 SAFE_FREE(error_string);
804 static char zeros[16];
806 mux_printf(mux_id, "Authenticated: Yes\n");
808 if (ntlm_server_1_lm_session_key
810 && (memcmp(zeros, lm_key.data,
811 lm_key.length) != 0)) {
812 char hex_lm_key[lm_key.length*2+1];
813 hex_encode_buf(hex_lm_key, lm_key.data,
815 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
818 if (ntlm_server_1_user_session_key
819 && user_session_key.length
820 && (memcmp(zeros, user_session_key.data,
821 user_session_key.length) != 0)) {
822 char hex_user_session_key[
823 user_session_key.length*2+1];
824 hex_encode_buf(hex_user_session_key,
825 user_session_key.data,
826 user_session_key.length);
827 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
831 /* clear out the state */
832 challenge = data_blob(NULL, 0);
833 nt_response = data_blob(NULL, 0);
834 lm_response = data_blob(NULL, 0);
835 SAFE_FREE(full_username);
838 SAFE_FREE(plaintext_password);
839 ntlm_server_1_user_session_key = false;
840 ntlm_server_1_lm_session_key = false;
841 mux_printf(mux_id, ".\n");
848 /* Indicates a base64 encoded structure */
849 parameter = strstr(request, ":: ");
851 parameter = strstr(request, ": ");
854 DEBUG(0, ("Parameter not found!\n"));
855 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
872 base64_decode_inplace(parameter);
875 if (strequal(request, "LANMAN-Challenge")) {
876 challenge = strhex_to_data_blob(NULL, parameter);
877 if (challenge.length != 8) {
878 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
880 (int)challenge.length);
881 challenge = data_blob(NULL, 0);
883 } else if (strequal(request, "NT-Response")) {
884 nt_response = strhex_to_data_blob(NULL, parameter);
885 if (nt_response.length < 24) {
886 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
888 (int)nt_response.length);
889 nt_response = data_blob(NULL, 0);
891 } else if (strequal(request, "LANMAN-Response")) {
892 lm_response = strhex_to_data_blob(NULL, parameter);
893 if (lm_response.length != 24) {
894 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
896 (int)lm_response.length);
897 lm_response = data_blob(NULL, 0);
899 } else if (strequal(request, "Password")) {
900 plaintext_password = smb_xstrdup(parameter);
901 } else if (strequal(request, "NT-Domain")) {
902 domain = smb_xstrdup(parameter);
903 } else if (strequal(request, "Username")) {
904 username = smb_xstrdup(parameter);
905 } else if (strequal(request, "Full-Username")) {
906 full_username = smb_xstrdup(parameter);
907 } else if (strequal(request, "Request-User-Session-Key")) {
908 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
909 } else if (strequal(request, "Request-LanMan-Session-Key")) {
910 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
912 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
916 static void manage_squid_request(struct loadparm_context *lp_ctx, enum stdio_helper_mode helper_mode,
917 stdio_helper_function fn, void **private2)
920 char tmp[INITIAL_BUFFER_SIZE+1];
921 unsigned int mux_id = 0;
922 int length, buf_size = 0;
925 unsigned int max_mux;
926 void **private_pointers;
929 static struct mux_private *mux_private;
930 static void *normal_private;
933 buf = talloc_strdup(NULL, "");
936 DEBUG(0, ("Failed to allocate memory for reading the input "
938 x_fprintf(x_stdout, "ERR\n");
943 /* this is not a typo - x_fgets doesn't work too well under
945 if (fgets(tmp, INITIAL_BUFFER_SIZE, stdin) == NULL) {
947 DEBUG(1, ("fgets() failed! dying..... errno=%d "
948 "(%s)\n", ferror(stdin),
949 strerror(ferror(stdin))));
951 exit(1); /* BIIG buffer */
956 buf = talloc_strdup_append_buffer(buf, tmp);
957 buf_size += INITIAL_BUFFER_SIZE;
959 if (buf_size > MAX_BUFFER_SIZE) {
960 DEBUG(0, ("Invalid Request (too large)\n"));
961 x_fprintf(x_stdout, "ERR\n");
966 c = strchr(buf, '\n');
972 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
974 if (buf[0] == '\0') {
975 DEBUG(0, ("Invalid Request (empty)\n"));
976 x_fprintf(x_stdout, "ERR\n");
982 if (sscanf(buf, "%u ", &mux_id) != 1) {
983 DEBUG(0, ("Invalid Request - no multiplex id\n"));
984 x_fprintf(x_stdout, "ERR\n");
989 mux_private = talloc(NULL, struct mux_private);
990 mux_private->max_mux = 0;
991 mux_private->private_pointers = NULL;
996 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
997 x_fprintf(x_stdout, "ERR\n");
1002 if (mux_id >= mux_private->max_mux) {
1003 unsigned int prev_max = mux_private->max_mux;
1004 mux_private->max_mux = mux_id + 1;
1005 mux_private->private_pointers
1006 = talloc_realloc(mux_private,
1007 mux_private->private_pointers,
1008 void *, mux_private->max_mux);
1009 memset(&mux_private->private_pointers[prev_max], '\0',
1010 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
1013 private1 = &mux_private->private_pointers[mux_id];
1016 private1 = &normal_private;
1019 fn(helper_mode, lp_ctx, c, length, private1, mux_id, private2);
1023 static void squid_stream(struct loadparm_context *lp_ctx,
1024 enum stdio_helper_mode stdio_mode,
1025 stdio_helper_function fn) {
1026 /* initialize FDescs */
1027 x_setbuf(x_stdout, NULL);
1028 x_setbuf(x_stderr, NULL);
1030 manage_squid_request(lp_ctx, stdio_mode, fn, NULL);
1038 OPT_USERNAME = 1000,
1047 OPT_USER_SESSION_KEY,
1049 OPT_REQUIRE_MEMBERSHIP,
1051 OPT_USE_CACHED_CREDS,
1055 int main(int argc, const char **argv)
1057 static const char *helper_protocol;
1062 /* NOTE: DO NOT change this interface without considering the implications!
1063 This is an external interface, which other programs will use to interact
1067 /* We do not use single-letter command abbreviations, because they harm future
1068 interface stability. */
1070 struct poptOption long_options[] = {
1072 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1073 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1074 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1075 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1076 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1077 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1078 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1079 { "allow-mschapv2", 0, POPT_ARG_NONE, &opt_allow_mschapv2, OPT_ALLOW_MSCHAPV2, "Explicitly allow MSCHAPv2" },
1085 /* Samba client initialisation */
1087 setup_logging(NULL, DEBUG_STDERR);
1091 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1093 /* Parse command line options */
1096 poptPrintHelp(pc, stderr, 0);
1100 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1101 POPT_CONTEXT_KEEP_FIRST);
1103 while((opt = poptGetNextOpt(pc)) != -1) {
1109 fprintf(stderr, "%s: %s\n",
1110 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1117 if (opt_domain == NULL) {
1118 opt_domain = lpcfg_workgroup(cmdline_lp_ctx);
1121 if (helper_protocol) {
1123 for (i=0; i<NUM_HELPER_MODES; i++) {
1124 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1125 squid_stream(cmdline_lp_ctx, stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1129 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1131 for (i=0; i<NUM_HELPER_MODES; i++) {
1132 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1138 if (!opt_username) {
1139 x_fprintf(x_stderr, "username must be specified!\n\n");
1140 poptPrintHelp(pc, stderr, 0);
1144 if (opt_workstation == NULL) {
1145 opt_workstation = lpcfg_netbios_name(cmdline_lp_ctx);
1148 if (!opt_password) {
1149 char pwd[256] = {0};
1152 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
1154 opt_password = smb_xstrdup(pwd);
1161 if (asprintf(&user, "%s%c%s", opt_domain,
1162 *lpcfg_winbind_separator(cmdline_lp_ctx),
1163 opt_username) < 0) {
1166 if (!check_plaintext_auth(user, opt_password, true)) {
1173 poptFreeContext(pc);