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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "system/filesys.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "auth/auth.h"
29 #include "auth/auth_sam.h"
31 #include "libcli/auth/libcli_auth.h"
32 #include "libcli/security/security.h"
33 #include "lib/ldb/include/ldb.h"
34 #include "lib/events/events.h"
35 #include "lib/messaging/messaging.h"
36 #include "lib/messaging/irpc.h"
37 #include "auth/ntlmssp/ntlmssp.h"
39 #define SQUID_BUFFER_SIZE 2010
41 enum stdio_helper_mode {
52 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
53 #define NTLM_AUTH_FLAG_LMKEY 0x0008
56 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
57 char *buf, int length, void **private,
58 unsigned int mux_id, void **private2);
60 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
61 char *buf, int length, void **private,
62 unsigned int mux_id, void **private2);
64 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
65 char *buf, int length, void **private,
66 unsigned int mux_id, void **private2);
68 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
69 char *buf, int length, void **private,
70 unsigned int mux_id, void **private2);
72 static void manage_squid_request(enum stdio_helper_mode helper_mode,
73 stdio_helper_function fn, void **private2);
76 enum stdio_helper_mode mode;
78 stdio_helper_function fn;
79 } stdio_helper_protocols[] = {
80 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
81 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
82 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
83 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
84 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
85 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
86 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
87 { NUM_HELPER_MODES, NULL, NULL}
90 extern int winbindd_fd;
92 static const char *opt_username;
93 static const char *opt_domain;
94 static const char *opt_workstation;
95 static const char *opt_password;
96 static int opt_multiplex;
99 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
101 static void mux_printf(unsigned int mux_id, const char *format, ...)
106 x_fprintf(x_stdout, "%d ", mux_id);
109 va_start(ap, format);
110 x_vfprintf(x_stdout, format, ap);
116 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
117 form DOMAIN/user into a domain and a user */
119 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
123 char *p = strchr(domuser,*lp_winbind_separator());
130 fstrcpy(domain, domuser);
131 domain[PTR_DIFF(p, domuser)] = 0;
137 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
139 static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
141 DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
142 ret.length = ldb_base64_decode((char *)ret.data);
147 * Encode a base64 string into a talloc()ed string caller to free.
149 static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
151 return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
155 * Decode a base64 string in-place - wrapper for the above
157 static void base64_decode_inplace(char *s)
159 ldb_base64_decode(s);
164 /* Authenticate a user with a plaintext password */
166 static BOOL check_plaintext_auth(const char *user, const char *pass,
167 BOOL stdout_diagnostics)
169 return (strcmp(pass, opt_password) == 0);
172 /* authenticate a user with an encrypted username/password */
174 static NTSTATUS local_pw_check_specified(const char *username,
176 const char *workstation,
177 const DATA_BLOB *challenge,
178 const DATA_BLOB *lm_response,
179 const DATA_BLOB *nt_response,
181 DATA_BLOB *lm_session_key,
182 DATA_BLOB *user_session_key,
187 struct samr_Password lm_pw, nt_pw;
188 struct samr_Password *lm_pwd, *nt_pwd;
189 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
191 nt_status = NT_STATUS_NO_MEMORY;
194 E_md4hash(opt_password, nt_pw.hash);
195 if (E_deshash(opt_password, lm_pw.hash)) {
203 nt_status = ntlm_password_check(mem_ctx,
204 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
205 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
212 lm_pwd, nt_pwd, user_session_key, lm_session_key);
214 if (NT_STATUS_IS_OK(nt_status)) {
218 *lp_winbind_separator(),
222 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
223 domain, username, workstation,
224 nt_errstr(nt_status)));
226 talloc_free(mem_ctx);
229 *error_string = strdup(nt_errstr(nt_status));
236 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
237 char *buf, int length, void **private,
238 unsigned int mux_id, void **private2)
243 pass=memchr(buf,' ',length);
245 DEBUG(2, ("Password not found. Denying access\n"));
246 mux_printf(mux_id, "ERR\n");
252 if (stdio_helper_mode == SQUID_2_5_BASIC) {
253 rfc1738_unescape(user);
254 rfc1738_unescape(pass);
257 if (check_plaintext_auth(user, pass, False)) {
258 mux_printf(mux_id, "OK\n");
260 mux_printf(mux_id, "ERR\n");
264 /* This is a bit hairy, but the basic idea is to do a password callback
265 to the calling application. The callback comes from within gensec */
267 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
268 char *buf, int length, void **private,
269 unsigned int mux_id, void **password)
272 if (strlen(buf) < 2) {
273 DEBUG(1, ("query [%s] invalid", buf));
274 mux_printf(mux_id, "BH\n");
278 if (strlen(buf) > 3) {
279 in = base64_decode_data_blob(NULL, buf + 3);
281 in = data_blob(NULL, 0);
284 if (strncmp(buf, "PW ", 3) == 0) {
286 *password = talloc_strndup(*private /* hopefully the right gensec context, useful to use for talloc */,
287 (const char *)in.data, in.length);
289 if (*password == NULL) {
290 DEBUG(1, ("Out of memory\n"));
291 mux_printf(mux_id, "BH\n");
296 mux_printf(mux_id, "OK\n");
300 DEBUG(1, ("Asked for (and expected) a password\n"));
301 mux_printf(mux_id, "BH\n");
306 * Callback for password credentails. This is not async, and when
307 * GENSEC and the credentails code is made async, it will look rather
311 static const char *get_password(struct cli_credentials *credentials)
313 char *password = NULL;
315 /* Ask for a password */
316 mux_printf((unsigned int)credentials->priv_data, "PW\n");
317 credentials->priv_data = NULL;
319 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
323 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
325 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
326 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
327 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
329 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
330 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
331 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
333 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
334 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
335 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
339 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
340 char *buf, int length, void **private,
341 unsigned int mux_id, void **private2)
344 DATA_BLOB out = data_blob(NULL, 0);
345 char *out_base64 = NULL;
346 const char *reply_arg = NULL;
347 struct gensec_ntlm_state {
348 struct gensec_security *gensec_state;
349 const char *set_password;
351 struct gensec_ntlm_state *state;
352 struct event_context *ev;
353 struct messaging_context *msg;
357 const char *reply_code;
358 struct cli_credentials *creds;
360 static char *want_feature_list = NULL;
361 static DATA_BLOB session_key;
368 state = talloc_zero(NULL, struct gensec_ntlm_state);
370 mux_printf(mux_id, "BH No Memory\n");
375 state->set_password = opt_password;
379 if (strlen(buf) < 2) {
380 DEBUG(1, ("query [%s] invalid", buf));
381 mux_printf(mux_id, "BH\n");
385 if (strlen(buf) > 3) {
386 if(strncmp(buf, "SF ", 3) == 0) {
387 DEBUG(10, ("Setting flags to negotiate\n"));
388 talloc_free(want_feature_list);
389 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
390 mux_printf(mux_id, "OK\n");
393 in = base64_decode_data_blob(NULL, buf + 3);
395 in = data_blob(NULL, 0);
398 if (strncmp(buf, "YR", 2) == 0) {
399 if (state->gensec_state) {
400 talloc_free(state->gensec_state);
401 state->gensec_state = NULL;
403 } else if ( (strncmp(buf, "OK", 2) == 0)) {
407 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
408 (strncmp(buf, "KK ", 3) != 0) &&
409 (strncmp(buf, "AF ", 3) != 0) &&
410 (strncmp(buf, "NA ", 3) != 0) &&
411 (strncmp(buf, "UG", 2) != 0) &&
412 (strncmp(buf, "PW ", 3) != 0) &&
413 (strncmp(buf, "GK", 2) != 0) &&
414 (strncmp(buf, "GF", 2) != 0)) {
415 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
416 mux_printf(mux_id, "BH\n");
422 if (!(state->gensec_state)) {
423 switch (stdio_helper_mode) {
424 case GSS_SPNEGO_CLIENT:
425 case NTLMSSP_CLIENT_1:
426 /* setup the client side */
428 nt_status = gensec_client_start(NULL, &state->gensec_state, NULL);
429 if (!NT_STATUS_IS_OK(nt_status)) {
434 case GSS_SPNEGO_SERVER:
435 case SQUID_2_5_NTLMSSP:
436 ev = event_context_init(state);
440 msg = messaging_client_init(state, ev);
444 if (!NT_STATUS_IS_OK(gensec_server_start(state, ev, msg, &state->gensec_state))) {
452 creds = cli_credentials_init(state->gensec_state);
453 cli_credentials_set_conf(creds);
455 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
458 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
460 if (state->set_password) {
461 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
463 cli_credentials_set_password_callback(creds, get_password);
464 creds->priv_data = (void*)mux_id;
466 if (opt_workstation) {
467 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
470 switch (stdio_helper_mode) {
471 case GSS_SPNEGO_SERVER:
472 case SQUID_2_5_NTLMSSP:
473 cli_credentials_set_machine_account(creds);
479 gensec_set_credentials(state->gensec_state, creds);
480 gensec_want_feature_list(state->gensec_state, want_feature_list);
482 switch (stdio_helper_mode) {
483 case GSS_SPNEGO_CLIENT:
484 case GSS_SPNEGO_SERVER:
485 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
490 case NTLMSSP_CLIENT_1:
495 case SQUID_2_5_NTLMSSP:
496 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
502 if (!NT_STATUS_IS_OK(nt_status)) {
503 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
504 mux_printf(mux_id, "BH\n");
511 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
513 if (strncmp(buf, "PW ", 3) == 0) {
514 state->set_password = talloc_strndup(state,
515 (const char *)in.data,
518 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
521 mux_printf(mux_id, "OK\n");
523 talloc_free(mem_ctx);
527 if (strncmp(buf, "UG", 2) == 0) {
529 char *grouplist = NULL;
530 struct auth_session_info *session_info;
532 nt_status = gensec_session_info(state->gensec_state, &session_info);
533 if (!NT_STATUS_IS_OK(nt_status)) {
534 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
535 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
537 talloc_free(mem_ctx);
541 /* get the string onto the context */
542 grouplist = talloc_strdup(mem_ctx, "");
544 for (i=0; i<session_info->security_token->num_sids; i++) {
545 struct security_token *token = session_info->security_token;
546 const char *sidstr = dom_sid_string(session_info,
548 grouplist = talloc_asprintf_append(grouplist, "%s,", sidstr);
551 mux_printf(mux_id, "GL %s\n", grouplist);
552 talloc_free(session_info);
554 talloc_free(mem_ctx);
558 if (strncmp(buf, "GK", 2) == 0) {
560 DEBUG(10, ("Requested session key\n"));
561 nt_status = gensec_session_key(state->gensec_state, &session_key);
562 if(!NT_STATUS_IS_OK(nt_status)) {
563 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
564 mux_printf(mux_id, "BH No session key\n");
565 talloc_free(mem_ctx);
568 base64_key = base64_encode_data_blob(state, session_key);
569 mux_printf(mux_id, "GK %s\n", base64_key);
570 talloc_free(base64_key);
572 talloc_free(mem_ctx);
576 if (strncmp(buf, "GF", 2) == 0) {
577 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
580 gensec_ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
581 struct gensec_ntlmssp_state);
582 neg_flags = gensec_ntlmssp_state->neg_flags;
584 DEBUG(10, ("Requested negotiated feature flags\n"));
585 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
589 nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
591 /* don't leak 'bad password'/'no such user' info to the network client */
592 nt_status = auth_nt_status_squash(nt_status);
595 out_base64 = base64_encode_data_blob(mem_ctx, out);
600 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
604 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
606 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
613 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
615 reply_arg = nt_errstr(nt_status);
616 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
617 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
619 reply_arg = nt_errstr(nt_status);
620 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
621 } else if (!NT_STATUS_IS_OK(nt_status)) {
623 reply_arg = nt_errstr(nt_status);
624 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
625 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
626 struct auth_session_info *session_info;
628 nt_status = gensec_session_info(state->gensec_state, &session_info);
629 if (!NT_STATUS_IS_OK(nt_status)) {
631 reply_arg = nt_errstr(nt_status);
632 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
636 reply_arg = talloc_asprintf(state->gensec_state,
637 "%s%s%s", session_info->server_info->domain_name,
638 lp_winbind_separator(), session_info->server_info->account_name);
639 talloc_free(session_info);
641 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
643 reply_arg = out_base64;
648 switch (stdio_helper_mode) {
649 case GSS_SPNEGO_SERVER:
650 mux_printf(mux_id, "%s %s %s\n", reply_code,
651 out_base64 ? out_base64 : "*",
652 reply_arg ? reply_arg : "*");
656 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
657 } else if (reply_arg) {
658 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
660 mux_printf(mux_id, "%s\n", reply_code);
664 talloc_free(mem_ctx);
668 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
669 char *buf, int length, void **private,
670 unsigned int mux_id, void **private2)
672 char *request, *parameter;
673 static DATA_BLOB challenge;
674 static DATA_BLOB lm_response;
675 static DATA_BLOB nt_response;
676 static char *full_username;
677 static char *username;
679 static char *plaintext_password;
680 static BOOL ntlm_server_1_user_session_key;
681 static BOOL ntlm_server_1_lm_session_key;
683 if (strequal(buf, ".")) {
684 if (!full_username && !username) {
685 mux_printf(mux_id, "Error: No username supplied!\n");
686 } else if (plaintext_password) {
687 /* handle this request as plaintext */
688 if (!full_username) {
689 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
690 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
694 if (check_plaintext_auth(full_username, plaintext_password, False)) {
695 mux_printf(mux_id, "Authenticated: Yes\n");
697 mux_printf(mux_id, "Authenticated: No\n");
699 } else if (!lm_response.data && !nt_response.data) {
700 mux_printf(mux_id, "Error: No password supplied!\n");
701 } else if (!challenge.data) {
702 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
704 char *error_string = NULL;
706 DATA_BLOB user_session_key;
709 if (full_username && !username) {
713 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
714 /* username might be 'tainted', don't print into our new-line deleimianted stream */
715 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
719 username = smb_xstrdup(fstr_user);
720 domain = smb_xstrdup(fstr_domain);
724 domain = smb_xstrdup(lp_workgroup());
727 if (ntlm_server_1_lm_session_key)
728 flags |= NTLM_AUTH_FLAG_LMKEY;
730 if (ntlm_server_1_user_session_key)
731 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
733 if (!NT_STATUS_IS_OK(
734 local_pw_check_specified(username,
746 mux_printf(mux_id, "Authenticated: No\n");
747 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
748 SAFE_FREE(error_string);
750 static char zeros[16];
752 char *hex_user_session_key;
754 mux_printf(mux_id, "Authenticated: Yes\n");
756 if (ntlm_server_1_lm_session_key
758 && (memcmp(zeros, lm_key.data,
759 lm_key.length) != 0)) {
760 hex_encode(lm_key.data,
763 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
764 SAFE_FREE(hex_lm_key);
767 if (ntlm_server_1_user_session_key
768 && user_session_key.length
769 && (memcmp(zeros, user_session_key.data,
770 user_session_key.length) != 0)) {
771 hex_encode(user_session_key.data,
772 user_session_key.length,
773 &hex_user_session_key);
774 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
775 SAFE_FREE(hex_user_session_key);
779 /* clear out the state */
780 challenge = data_blob(NULL, 0);
781 nt_response = data_blob(NULL, 0);
782 lm_response = data_blob(NULL, 0);
783 SAFE_FREE(full_username);
786 SAFE_FREE(plaintext_password);
787 ntlm_server_1_user_session_key = False;
788 ntlm_server_1_lm_session_key = False;
789 mux_printf(mux_id, ".\n");
796 /* Indicates a base64 encoded structure */
797 parameter = strstr(request, ":: ");
799 parameter = strstr(request, ": ");
802 DEBUG(0, ("Parameter not found!\n"));
803 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
820 base64_decode_inplace(parameter);
823 if (strequal(request, "LANMAN-Challenge")) {
824 challenge = strhex_to_data_blob(parameter);
825 if (challenge.length != 8) {
826 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
828 (int)challenge.length);
829 challenge = data_blob(NULL, 0);
831 } else if (strequal(request, "NT-Response")) {
832 nt_response = strhex_to_data_blob(parameter);
833 if (nt_response.length < 24) {
834 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
836 (int)nt_response.length);
837 nt_response = data_blob(NULL, 0);
839 } else if (strequal(request, "LANMAN-Response")) {
840 lm_response = strhex_to_data_blob(parameter);
841 if (lm_response.length != 24) {
842 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
844 (int)lm_response.length);
845 lm_response = data_blob(NULL, 0);
847 } else if (strequal(request, "Password")) {
848 plaintext_password = smb_xstrdup(parameter);
849 } else if (strequal(request, "NT-Domain")) {
850 domain = smb_xstrdup(parameter);
851 } else if (strequal(request, "Username")) {
852 username = smb_xstrdup(parameter);
853 } else if (strequal(request, "Full-Username")) {
854 full_username = smb_xstrdup(parameter);
855 } else if (strequal(request, "Request-User-Session-Key")) {
856 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
857 } else if (strequal(request, "Request-LanMan-Session-Key")) {
858 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
860 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
864 static void manage_squid_request(enum stdio_helper_mode helper_mode,
865 stdio_helper_function fn, void **private2)
867 char buf[SQUID_BUFFER_SIZE+1];
868 unsigned int mux_id = 0;
873 unsigned int max_mux;
874 void **private_pointers;
877 static struct mux_private *mux_private;
878 static void *normal_private;
881 /* this is not a typo - x_fgets doesn't work too well under squid */
882 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
884 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
885 strerror(ferror(stdin))));
887 exit(1); /* BIIG buffer */
892 c=memchr(buf,'\n',sizeof(buf)-1);
901 DEBUG(0, ("Oversized message\n"));
902 x_fprintf(x_stdout, "ERR\n");
907 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
909 if (buf[0] == '\0') {
910 DEBUG(0, ("Invalid Request (empty)\n"));
911 x_fprintf(x_stdout, "ERR\n");
916 if (sscanf(buf, "%u ", &mux_id) != 1) {
917 DEBUG(0, ("Invalid Request - no multiplex id\n"));
918 x_fprintf(x_stdout, "ERR\n");
922 mux_private = talloc(NULL, struct mux_private);
923 mux_private->max_mux = 0;
924 mux_private->private_pointers = NULL;
929 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
930 x_fprintf(x_stdout, "ERR\n");
934 if (mux_id >= mux_private->max_mux) {
935 unsigned int prev_max = mux_private->max_mux;
936 mux_private->max_mux = mux_id + 1;
937 mux_private->private_pointers
938 = talloc_realloc(mux_private,
939 mux_private->private_pointers,
940 void *, mux_private->max_mux);
941 memset(&mux_private->private_pointers[prev_max], '\0',
942 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
945 private = &mux_private->private_pointers[mux_id];
948 private = &normal_private;
951 fn(helper_mode, c, length, private, mux_id, private2);
954 static void squid_stream(enum stdio_helper_mode stdio_mode,
955 stdio_helper_function fn) {
956 /* initialize FDescs */
957 x_setbuf(x_stdout, NULL);
958 x_setbuf(x_stderr, NULL);
960 manage_squid_request(stdio_mode, fn, NULL);
977 OPT_USER_SESSION_KEY,
979 OPT_REQUIRE_MEMBERSHIP,
983 int main(int argc, const char **argv)
985 static const char *helper_protocol;
990 /* NOTE: DO NOT change this interface without considering the implications!
991 This is an external interface, which other programs will use to interact
995 /* We do not use single-letter command abbreviations, because they harm future
996 interface stability. */
998 struct poptOption long_options[] = {
1000 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1001 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1002 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1003 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1004 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1005 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1011 /* Samba client initialisation */
1013 setup_logging(NULL, DEBUG_STDERR);
1017 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1019 /* Parse command line options */
1022 poptPrintHelp(pc, stderr, 0);
1026 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1027 POPT_CONTEXT_KEEP_FIRST);
1029 while((opt = poptGetNextOpt(pc)) != -1) {
1035 fprintf(stderr, "%s: %s\n",
1036 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1043 if (opt_domain == NULL) {
1044 opt_domain = lp_workgroup();
1047 if (helper_protocol) {
1049 for (i=0; i<NUM_HELPER_MODES; i++) {
1050 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1051 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1055 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1057 for (i=0; i<NUM_HELPER_MODES; i++) {
1058 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1064 if (!opt_username) {
1065 x_fprintf(x_stderr, "username must be specified!\n\n");
1066 poptPrintHelp(pc, stderr, 0);
1070 if (opt_workstation == NULL) {
1071 opt_workstation = lp_netbios_name();
1074 if (!opt_password) {
1075 opt_password = getpass("password: ");
1081 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
1082 if (!check_plaintext_auth(user, opt_password, True)) {
1089 poptFreeContext(pc);