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 SMB_ASSERT(base64_key != NULL);
625 mux_printf(mux_id, "GK %s\n", base64_key);
626 talloc_free(base64_key);
628 talloc_free(mem_ctx);
632 if (strncmp(buf, "GF", 2) == 0) {
633 struct ntlmssp_state *ntlmssp_state;
636 ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
637 struct ntlmssp_state);
638 neg_flags = ntlmssp_state->neg_flags;
640 DEBUG(10, ("Requested negotiated feature flags\n"));
641 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
645 nt_status = gensec_update_ev(state->gensec_state, mem_ctx, ev, in, &out);
647 /* don't leak 'bad password'/'no such user' info to the network client */
648 nt_status = nt_status_squash(nt_status);
651 out_base64 = base64_encode_data_blob(mem_ctx, out);
652 SMB_ASSERT(out_base64 != NULL);
657 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
661 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
663 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
670 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
671 reply_code = "BH NT_STATUS_ACCESS_DENIED";
672 reply_arg = nt_errstr(nt_status);
673 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
674 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
675 reply_code = "BH NT_STATUS_UNSUCCESSFUL";
676 reply_arg = nt_errstr(nt_status);
677 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
678 } else if (!NT_STATUS_IS_OK(nt_status)) {
680 reply_arg = nt_errstr(nt_status);
681 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
682 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
683 struct auth_session_info *session_info;
685 nt_status = gensec_session_info(state->gensec_state, mem_ctx, &session_info);
686 if (!NT_STATUS_IS_OK(nt_status)) {
687 reply_code = "BH Failed to retrive session info";
688 reply_arg = nt_errstr(nt_status);
689 DEBUG(1, ("GENSEC failed to retrieve the session info: %s\n", nt_errstr(nt_status)));
693 reply_arg = talloc_asprintf(state->gensec_state,
694 "%s%s%s", session_info->info->domain_name,
695 lpcfg_winbind_separator(lp_ctx), session_info->info->account_name);
696 talloc_free(session_info);
698 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
700 reply_arg = out_base64;
705 switch (stdio_helper_mode) {
706 case GSS_SPNEGO_SERVER:
707 mux_printf(mux_id, "%s %s %s\n", reply_code,
708 out_base64 ? out_base64 : "*",
709 reply_arg ? reply_arg : "*");
713 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
714 } else if (reply_arg) {
715 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
717 mux_printf(mux_id, "%s\n", reply_code);
721 talloc_free(mem_ctx);
725 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
726 struct loadparm_context *lp_ctx,
727 char *buf, int length, void **private1,
728 unsigned int mux_id, void **private2)
730 char *request, *parameter;
731 static DATA_BLOB challenge;
732 static DATA_BLOB lm_response;
733 static DATA_BLOB nt_response;
734 static char *full_username;
735 static char *username;
737 static char *plaintext_password;
738 static bool ntlm_server_1_user_session_key;
739 static bool ntlm_server_1_lm_session_key;
741 if (strequal(buf, ".")) {
742 if (!full_username && !username) {
743 mux_printf(mux_id, "Error: No username supplied!\n");
744 } else if (plaintext_password) {
745 /* handle this request as plaintext */
746 if (!full_username) {
747 if (asprintf(&full_username, "%s%c%s", domain, *lpcfg_winbind_separator(lp_ctx), username) < 0) {
748 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
752 if (check_plaintext_auth(full_username, plaintext_password, false)) {
753 mux_printf(mux_id, "Authenticated: Yes\n");
755 mux_printf(mux_id, "Authenticated: No\n");
757 } else if (!lm_response.data && !nt_response.data) {
758 mux_printf(mux_id, "Error: No password supplied!\n");
759 } else if (!challenge.data) {
760 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
762 char *error_string = NULL;
764 DATA_BLOB user_session_key;
767 if (full_username && !username) {
770 if (!parse_ntlm_auth_domain_user(full_username, &username,
772 *lpcfg_winbind_separator(lp_ctx))) {
773 /* username might be 'tainted', don't print into our new-line deleimianted stream */
774 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
779 domain = smb_xstrdup(lpcfg_workgroup(lp_ctx));
782 if (ntlm_server_1_lm_session_key)
783 flags |= NTLM_AUTH_FLAG_LMKEY;
785 if (ntlm_server_1_user_session_key)
786 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
788 if (!NT_STATUS_IS_OK(
789 local_pw_check_specified(lp_ctx,
792 lpcfg_netbios_name(lp_ctx),
802 mux_printf(mux_id, "Authenticated: No\n");
803 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
804 SAFE_FREE(error_string);
806 static char zeros[16];
808 mux_printf(mux_id, "Authenticated: Yes\n");
810 if (ntlm_server_1_lm_session_key
812 && (memcmp(zeros, lm_key.data,
813 lm_key.length) != 0)) {
814 char hex_lm_key[lm_key.length*2+1];
815 hex_encode_buf(hex_lm_key, lm_key.data,
817 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
820 if (ntlm_server_1_user_session_key
821 && user_session_key.length
822 && (memcmp(zeros, user_session_key.data,
823 user_session_key.length) != 0)) {
824 char hex_user_session_key[
825 user_session_key.length*2+1];
826 hex_encode_buf(hex_user_session_key,
827 user_session_key.data,
828 user_session_key.length);
829 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
833 /* clear out the state */
834 challenge = data_blob(NULL, 0);
835 nt_response = data_blob(NULL, 0);
836 lm_response = data_blob(NULL, 0);
837 SAFE_FREE(full_username);
840 SAFE_FREE(plaintext_password);
841 ntlm_server_1_user_session_key = false;
842 ntlm_server_1_lm_session_key = false;
843 mux_printf(mux_id, ".\n");
850 /* Indicates a base64 encoded structure */
851 parameter = strstr(request, ":: ");
853 parameter = strstr(request, ": ");
856 DEBUG(0, ("Parameter not found!\n"));
857 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
874 base64_decode_inplace(parameter);
877 if (strequal(request, "LANMAN-Challenge")) {
878 challenge = strhex_to_data_blob(NULL, parameter);
879 if (challenge.length != 8) {
880 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
882 (int)challenge.length);
883 challenge = data_blob(NULL, 0);
885 } else if (strequal(request, "NT-Response")) {
886 nt_response = strhex_to_data_blob(NULL, parameter);
887 if (nt_response.length < 24) {
888 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
890 (int)nt_response.length);
891 nt_response = data_blob(NULL, 0);
893 } else if (strequal(request, "LANMAN-Response")) {
894 lm_response = strhex_to_data_blob(NULL, parameter);
895 if (lm_response.length != 24) {
896 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
898 (int)lm_response.length);
899 lm_response = data_blob(NULL, 0);
901 } else if (strequal(request, "Password")) {
902 plaintext_password = smb_xstrdup(parameter);
903 } else if (strequal(request, "NT-Domain")) {
904 domain = smb_xstrdup(parameter);
905 } else if (strequal(request, "Username")) {
906 username = smb_xstrdup(parameter);
907 } else if (strequal(request, "Full-Username")) {
908 full_username = smb_xstrdup(parameter);
909 } else if (strequal(request, "Request-User-Session-Key")) {
910 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
911 } else if (strequal(request, "Request-LanMan-Session-Key")) {
912 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
914 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
918 static void manage_squid_request(struct loadparm_context *lp_ctx, enum stdio_helper_mode helper_mode,
919 stdio_helper_function fn, void **private2)
922 char tmp[INITIAL_BUFFER_SIZE+1];
923 unsigned int mux_id = 0;
924 int length, buf_size = 0;
927 unsigned int max_mux;
928 void **private_pointers;
931 static struct mux_private *mux_private;
932 static void *normal_private;
935 buf = talloc_strdup(NULL, "");
938 DEBUG(0, ("Failed to allocate memory for reading the input "
940 x_fprintf(x_stdout, "ERR\n");
945 /* this is not a typo - x_fgets doesn't work too well under
947 if (fgets(tmp, INITIAL_BUFFER_SIZE, stdin) == NULL) {
949 DEBUG(1, ("fgets() failed! dying..... errno=%d "
950 "(%s)\n", ferror(stdin),
951 strerror(ferror(stdin))));
953 exit(1); /* BIIG buffer */
958 buf = talloc_strdup_append_buffer(buf, tmp);
959 buf_size += INITIAL_BUFFER_SIZE;
961 if (buf_size > MAX_BUFFER_SIZE) {
962 DEBUG(0, ("Invalid Request (too large)\n"));
963 x_fprintf(x_stdout, "ERR\n");
968 c = strchr(buf, '\n');
974 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
976 if (buf[0] == '\0') {
977 DEBUG(0, ("Invalid Request (empty)\n"));
978 x_fprintf(x_stdout, "ERR\n");
984 if (sscanf(buf, "%u ", &mux_id) != 1) {
985 DEBUG(0, ("Invalid Request - no multiplex id\n"));
986 x_fprintf(x_stdout, "ERR\n");
991 mux_private = talloc(NULL, struct mux_private);
992 mux_private->max_mux = 0;
993 mux_private->private_pointers = NULL;
998 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
999 x_fprintf(x_stdout, "ERR\n");
1004 if (mux_id >= mux_private->max_mux) {
1005 unsigned int prev_max = mux_private->max_mux;
1006 mux_private->max_mux = mux_id + 1;
1007 mux_private->private_pointers
1008 = talloc_realloc(mux_private,
1009 mux_private->private_pointers,
1010 void *, mux_private->max_mux);
1011 memset(&mux_private->private_pointers[prev_max], '\0',
1012 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
1015 private1 = &mux_private->private_pointers[mux_id];
1018 private1 = &normal_private;
1021 fn(helper_mode, lp_ctx, c, length, private1, mux_id, private2);
1025 static void squid_stream(struct loadparm_context *lp_ctx,
1026 enum stdio_helper_mode stdio_mode,
1027 stdio_helper_function fn) {
1028 /* initialize FDescs */
1029 x_setbuf(x_stdout, NULL);
1030 x_setbuf(x_stderr, NULL);
1032 manage_squid_request(lp_ctx, stdio_mode, fn, NULL);
1040 OPT_USERNAME = 1000,
1049 OPT_USER_SESSION_KEY,
1051 OPT_REQUIRE_MEMBERSHIP,
1053 OPT_USE_CACHED_CREDS,
1057 int main(int argc, const char **argv)
1059 static const char *helper_protocol;
1064 /* NOTE: DO NOT change this interface without considering the implications!
1065 This is an external interface, which other programs will use to interact
1069 /* We do not use single-letter command abbreviations, because they harm future
1070 interface stability. */
1072 struct poptOption long_options[] = {
1074 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1075 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1076 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1077 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1078 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1079 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1080 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1081 { "allow-mschapv2", 0, POPT_ARG_NONE, &opt_allow_mschapv2, OPT_ALLOW_MSCHAPV2, "Explicitly allow MSCHAPv2" },
1087 /* Samba client initialisation */
1089 setup_logging(NULL, DEBUG_STDERR);
1093 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1095 /* Parse command line options */
1098 poptPrintHelp(pc, stderr, 0);
1102 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1103 POPT_CONTEXT_KEEP_FIRST);
1105 while((opt = poptGetNextOpt(pc)) != -1) {
1111 fprintf(stderr, "%s: %s\n",
1112 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1119 if (opt_domain == NULL) {
1120 opt_domain = lpcfg_workgroup(cmdline_lp_ctx);
1123 if (helper_protocol) {
1125 for (i=0; i<NUM_HELPER_MODES; i++) {
1126 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1127 squid_stream(cmdline_lp_ctx, stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1131 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1133 for (i=0; i<NUM_HELPER_MODES; i++) {
1134 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1140 if (!opt_username) {
1141 x_fprintf(x_stderr, "username must be specified!\n\n");
1142 poptPrintHelp(pc, stderr, 0);
1146 if (opt_workstation == NULL) {
1147 opt_workstation = lpcfg_netbios_name(cmdline_lp_ctx);
1150 if (!opt_password) {
1151 char pwd[256] = {0};
1154 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
1156 opt_password = smb_xstrdup(pwd);
1163 if (asprintf(&user, "%s%c%s", opt_domain,
1164 *lpcfg_winbind_separator(cmdline_lp_ctx),
1165 opt_username) < 0) {
1168 if (!check_plaintext_auth(user, opt_password, true)) {
1175 poptFreeContext(pc);