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;
97 static int use_cached_creds;
100 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
102 static void mux_printf(unsigned int mux_id, const char *format, ...)
107 x_fprintf(x_stdout, "%d ", mux_id);
110 va_start(ap, format);
111 x_vfprintf(x_stdout, format, ap);
117 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
118 form DOMAIN/user into a domain and a user */
120 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
124 char *p = strchr(domuser,*lp_winbind_separator());
131 fstrcpy(domain, domuser);
132 domain[PTR_DIFF(p, domuser)] = 0;
138 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
140 static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
142 DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
143 ret.length = ldb_base64_decode((char *)ret.data);
148 * Encode a base64 string into a talloc()ed string caller to free.
150 static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
152 return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
156 * Decode a base64 string in-place - wrapper for the above
158 static void base64_decode_inplace(char *s)
160 ldb_base64_decode(s);
165 /* Authenticate a user with a plaintext password */
167 static BOOL check_plaintext_auth(const char *user, const char *pass,
168 BOOL stdout_diagnostics)
170 return (strcmp(pass, opt_password) == 0);
173 /* authenticate a user with an encrypted username/password */
175 static NTSTATUS local_pw_check_specified(const char *username,
177 const char *workstation,
178 const DATA_BLOB *challenge,
179 const DATA_BLOB *lm_response,
180 const DATA_BLOB *nt_response,
182 DATA_BLOB *lm_session_key,
183 DATA_BLOB *user_session_key,
188 struct samr_Password lm_pw, nt_pw;
189 struct samr_Password *lm_pwd, *nt_pwd;
190 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
192 nt_status = NT_STATUS_NO_MEMORY;
195 E_md4hash(opt_password, nt_pw.hash);
196 if (E_deshash(opt_password, lm_pw.hash)) {
204 nt_status = ntlm_password_check(mem_ctx,
205 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
206 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
213 lm_pwd, nt_pwd, user_session_key, lm_session_key);
215 if (NT_STATUS_IS_OK(nt_status)) {
219 *lp_winbind_separator(),
223 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
224 domain, username, workstation,
225 nt_errstr(nt_status)));
227 talloc_free(mem_ctx);
230 *error_string = strdup(nt_errstr(nt_status));
237 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
238 char *buf, int length, void **private,
239 unsigned int mux_id, void **private2)
244 pass=memchr(buf,' ',length);
246 DEBUG(2, ("Password not found. Denying access\n"));
247 mux_printf(mux_id, "ERR\n");
253 if (stdio_helper_mode == SQUID_2_5_BASIC) {
254 rfc1738_unescape(user);
255 rfc1738_unescape(pass);
258 if (check_plaintext_auth(user, pass, False)) {
259 mux_printf(mux_id, "OK\n");
261 mux_printf(mux_id, "ERR\n");
265 /* This is a bit hairy, but the basic idea is to do a password callback
266 to the calling application. The callback comes from within gensec */
268 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
269 char *buf, int length, void **private,
270 unsigned int mux_id, void **password)
273 if (strlen(buf) < 2) {
274 DEBUG(1, ("query [%s] invalid", buf));
275 mux_printf(mux_id, "BH\n");
279 if (strlen(buf) > 3) {
280 in = base64_decode_data_blob(NULL, buf + 3);
282 in = data_blob(NULL, 0);
285 if (strncmp(buf, "PW ", 3) == 0) {
287 *password = talloc_strndup(*private /* hopefully the right gensec context, useful to use for talloc */,
288 (const char *)in.data, in.length);
290 if (*password == NULL) {
291 DEBUG(1, ("Out of memory\n"));
292 mux_printf(mux_id, "BH\n");
297 mux_printf(mux_id, "OK\n");
301 DEBUG(1, ("Asked for (and expected) a password\n"));
302 mux_printf(mux_id, "BH\n");
307 * Callback for password credentails. This is not async, and when
308 * GENSEC and the credentails code is made async, it will look rather
312 static const char *get_password(struct cli_credentials *credentials)
314 char *password = NULL;
316 /* Ask for a password */
317 mux_printf((unsigned int)credentials->priv_data, "PW\n");
318 credentials->priv_data = NULL;
320 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
324 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
326 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
327 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
328 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
330 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
331 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
332 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
334 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
335 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
336 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
340 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
341 char *buf, int length, void **private,
342 unsigned int mux_id, void **private2)
345 DATA_BLOB out = data_blob(NULL, 0);
346 char *out_base64 = NULL;
347 const char *reply_arg = NULL;
348 struct gensec_ntlm_state {
349 struct gensec_security *gensec_state;
350 const char *set_password;
352 struct gensec_ntlm_state *state;
353 struct event_context *ev;
354 struct messaging_context *msg;
358 const char *reply_code;
359 struct cli_credentials *creds;
361 static char *want_feature_list = NULL;
362 static DATA_BLOB session_key;
369 state = talloc_zero(NULL, struct gensec_ntlm_state);
371 mux_printf(mux_id, "BH No Memory\n");
376 state->set_password = opt_password;
380 if (strlen(buf) < 2) {
381 DEBUG(1, ("query [%s] invalid", buf));
382 mux_printf(mux_id, "BH\n");
386 if (strlen(buf) > 3) {
387 if(strncmp(buf, "SF ", 3) == 0) {
388 DEBUG(10, ("Setting flags to negotiate\n"));
389 talloc_free(want_feature_list);
390 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
391 mux_printf(mux_id, "OK\n");
394 in = base64_decode_data_blob(NULL, buf + 3);
396 in = data_blob(NULL, 0);
399 if (strncmp(buf, "YR", 2) == 0) {
400 if (state->gensec_state) {
401 talloc_free(state->gensec_state);
402 state->gensec_state = NULL;
404 } else if ( (strncmp(buf, "OK", 2) == 0)) {
408 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
409 (strncmp(buf, "KK ", 3) != 0) &&
410 (strncmp(buf, "AF ", 3) != 0) &&
411 (strncmp(buf, "NA ", 3) != 0) &&
412 (strncmp(buf, "UG", 2) != 0) &&
413 (strncmp(buf, "PW ", 3) != 0) &&
414 (strncmp(buf, "GK", 2) != 0) &&
415 (strncmp(buf, "GF", 2) != 0)) {
416 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
417 mux_printf(mux_id, "BH\n");
423 if (!(state->gensec_state)) {
424 switch (stdio_helper_mode) {
425 case GSS_SPNEGO_CLIENT:
426 case NTLMSSP_CLIENT_1:
427 /* setup the client side */
429 nt_status = gensec_client_start(NULL, &state->gensec_state, NULL);
430 if (!NT_STATUS_IS_OK(nt_status)) {
435 case GSS_SPNEGO_SERVER:
436 case SQUID_2_5_NTLMSSP:
437 ev = event_context_init(state);
441 msg = messaging_client_init(state, ev);
445 if (!NT_STATUS_IS_OK(gensec_server_start(state, ev, msg, &state->gensec_state))) {
453 creds = cli_credentials_init(state->gensec_state);
454 cli_credentials_set_conf(creds);
456 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
459 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
461 if (state->set_password) {
462 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
464 cli_credentials_set_password_callback(creds, get_password);
465 creds->priv_data = (void*)mux_id;
467 if (opt_workstation) {
468 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
471 switch (stdio_helper_mode) {
472 case GSS_SPNEGO_SERVER:
473 case SQUID_2_5_NTLMSSP:
474 cli_credentials_set_machine_account(creds);
480 gensec_set_credentials(state->gensec_state, creds);
481 gensec_want_feature_list(state->gensec_state, want_feature_list);
483 switch (stdio_helper_mode) {
484 case GSS_SPNEGO_CLIENT:
485 case GSS_SPNEGO_SERVER:
486 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
491 case NTLMSSP_CLIENT_1:
496 case SQUID_2_5_NTLMSSP:
497 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
503 if (!NT_STATUS_IS_OK(nt_status)) {
504 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
505 mux_printf(mux_id, "BH\n");
512 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
514 if (strncmp(buf, "PW ", 3) == 0) {
515 state->set_password = talloc_strndup(state,
516 (const char *)in.data,
519 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
522 mux_printf(mux_id, "OK\n");
524 talloc_free(mem_ctx);
528 if (strncmp(buf, "UG", 2) == 0) {
530 char *grouplist = NULL;
531 struct auth_session_info *session_info;
533 nt_status = gensec_session_info(state->gensec_state, &session_info);
534 if (!NT_STATUS_IS_OK(nt_status)) {
535 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
536 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
538 talloc_free(mem_ctx);
542 /* get the string onto the context */
543 grouplist = talloc_strdup(mem_ctx, "");
545 for (i=0; i<session_info->security_token->num_sids; i++) {
546 struct security_token *token = session_info->security_token;
547 const char *sidstr = dom_sid_string(session_info,
549 grouplist = talloc_asprintf_append(grouplist, "%s,", sidstr);
552 mux_printf(mux_id, "GL %s\n", grouplist);
553 talloc_free(session_info);
555 talloc_free(mem_ctx);
559 if (strncmp(buf, "GK", 2) == 0) {
561 DEBUG(10, ("Requested session key\n"));
562 nt_status = gensec_session_key(state->gensec_state, &session_key);
563 if(!NT_STATUS_IS_OK(nt_status)) {
564 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
565 mux_printf(mux_id, "BH No session key\n");
566 talloc_free(mem_ctx);
569 base64_key = base64_encode_data_blob(state, session_key);
570 mux_printf(mux_id, "GK %s\n", base64_key);
571 talloc_free(base64_key);
573 talloc_free(mem_ctx);
577 if (strncmp(buf, "GF", 2) == 0) {
578 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
581 gensec_ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
582 struct gensec_ntlmssp_state);
583 neg_flags = gensec_ntlmssp_state->neg_flags;
585 DEBUG(10, ("Requested negotiated feature flags\n"));
586 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
590 nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
592 /* don't leak 'bad password'/'no such user' info to the network client */
593 nt_status = auth_nt_status_squash(nt_status);
596 out_base64 = base64_encode_data_blob(mem_ctx, out);
601 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
605 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
607 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
614 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
616 reply_arg = nt_errstr(nt_status);
617 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
618 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
620 reply_arg = nt_errstr(nt_status);
621 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
622 } else if (!NT_STATUS_IS_OK(nt_status)) {
624 reply_arg = nt_errstr(nt_status);
625 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
626 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
627 struct auth_session_info *session_info;
629 nt_status = gensec_session_info(state->gensec_state, &session_info);
630 if (!NT_STATUS_IS_OK(nt_status)) {
632 reply_arg = nt_errstr(nt_status);
633 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
637 reply_arg = talloc_asprintf(state->gensec_state,
638 "%s%s%s", session_info->server_info->domain_name,
639 lp_winbind_separator(), session_info->server_info->account_name);
640 talloc_free(session_info);
642 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
644 reply_arg = out_base64;
649 switch (stdio_helper_mode) {
650 case GSS_SPNEGO_SERVER:
651 mux_printf(mux_id, "%s %s %s\n", reply_code,
652 out_base64 ? out_base64 : "*",
653 reply_arg ? reply_arg : "*");
657 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
658 } else if (reply_arg) {
659 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
661 mux_printf(mux_id, "%s\n", reply_code);
665 talloc_free(mem_ctx);
669 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
670 char *buf, int length, void **private,
671 unsigned int mux_id, void **private2)
673 char *request, *parameter;
674 static DATA_BLOB challenge;
675 static DATA_BLOB lm_response;
676 static DATA_BLOB nt_response;
677 static char *full_username;
678 static char *username;
680 static char *plaintext_password;
681 static BOOL ntlm_server_1_user_session_key;
682 static BOOL ntlm_server_1_lm_session_key;
684 if (strequal(buf, ".")) {
685 if (!full_username && !username) {
686 mux_printf(mux_id, "Error: No username supplied!\n");
687 } else if (plaintext_password) {
688 /* handle this request as plaintext */
689 if (!full_username) {
690 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
691 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
695 if (check_plaintext_auth(full_username, plaintext_password, False)) {
696 mux_printf(mux_id, "Authenticated: Yes\n");
698 mux_printf(mux_id, "Authenticated: No\n");
700 } else if (!lm_response.data && !nt_response.data) {
701 mux_printf(mux_id, "Error: No password supplied!\n");
702 } else if (!challenge.data) {
703 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
705 char *error_string = NULL;
707 DATA_BLOB user_session_key;
710 if (full_username && !username) {
714 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
715 /* username might be 'tainted', don't print into our new-line deleimianted stream */
716 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
720 username = smb_xstrdup(fstr_user);
721 domain = smb_xstrdup(fstr_domain);
725 domain = smb_xstrdup(lp_workgroup());
728 if (ntlm_server_1_lm_session_key)
729 flags |= NTLM_AUTH_FLAG_LMKEY;
731 if (ntlm_server_1_user_session_key)
732 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
734 if (!NT_STATUS_IS_OK(
735 local_pw_check_specified(username,
747 mux_printf(mux_id, "Authenticated: No\n");
748 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
749 SAFE_FREE(error_string);
751 static char zeros[16];
753 char *hex_user_session_key;
755 mux_printf(mux_id, "Authenticated: Yes\n");
757 if (ntlm_server_1_lm_session_key
759 && (memcmp(zeros, lm_key.data,
760 lm_key.length) != 0)) {
761 hex_encode(lm_key.data,
764 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
765 SAFE_FREE(hex_lm_key);
768 if (ntlm_server_1_user_session_key
769 && user_session_key.length
770 && (memcmp(zeros, user_session_key.data,
771 user_session_key.length) != 0)) {
772 hex_encode(user_session_key.data,
773 user_session_key.length,
774 &hex_user_session_key);
775 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
776 SAFE_FREE(hex_user_session_key);
780 /* clear out the state */
781 challenge = data_blob(NULL, 0);
782 nt_response = data_blob(NULL, 0);
783 lm_response = data_blob(NULL, 0);
784 SAFE_FREE(full_username);
787 SAFE_FREE(plaintext_password);
788 ntlm_server_1_user_session_key = False;
789 ntlm_server_1_lm_session_key = False;
790 mux_printf(mux_id, ".\n");
797 /* Indicates a base64 encoded structure */
798 parameter = strstr(request, ":: ");
800 parameter = strstr(request, ": ");
803 DEBUG(0, ("Parameter not found!\n"));
804 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
821 base64_decode_inplace(parameter);
824 if (strequal(request, "LANMAN-Challenge")) {
825 challenge = strhex_to_data_blob(parameter);
826 if (challenge.length != 8) {
827 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
829 (int)challenge.length);
830 challenge = data_blob(NULL, 0);
832 } else if (strequal(request, "NT-Response")) {
833 nt_response = strhex_to_data_blob(parameter);
834 if (nt_response.length < 24) {
835 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
837 (int)nt_response.length);
838 nt_response = data_blob(NULL, 0);
840 } else if (strequal(request, "LANMAN-Response")) {
841 lm_response = strhex_to_data_blob(parameter);
842 if (lm_response.length != 24) {
843 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
845 (int)lm_response.length);
846 lm_response = data_blob(NULL, 0);
848 } else if (strequal(request, "Password")) {
849 plaintext_password = smb_xstrdup(parameter);
850 } else if (strequal(request, "NT-Domain")) {
851 domain = smb_xstrdup(parameter);
852 } else if (strequal(request, "Username")) {
853 username = smb_xstrdup(parameter);
854 } else if (strequal(request, "Full-Username")) {
855 full_username = smb_xstrdup(parameter);
856 } else if (strequal(request, "Request-User-Session-Key")) {
857 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
858 } else if (strequal(request, "Request-LanMan-Session-Key")) {
859 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
861 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
865 static void manage_squid_request(enum stdio_helper_mode helper_mode,
866 stdio_helper_function fn, void **private2)
868 char buf[SQUID_BUFFER_SIZE+1];
869 unsigned int mux_id = 0;
874 unsigned int max_mux;
875 void **private_pointers;
878 static struct mux_private *mux_private;
879 static void *normal_private;
882 /* this is not a typo - x_fgets doesn't work too well under squid */
883 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
885 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
886 strerror(ferror(stdin))));
888 exit(1); /* BIIG buffer */
893 c=memchr(buf,'\n',sizeof(buf)-1);
902 DEBUG(0, ("Oversized message\n"));
903 x_fprintf(x_stdout, "ERR\n");
908 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
910 if (buf[0] == '\0') {
911 DEBUG(0, ("Invalid Request (empty)\n"));
912 x_fprintf(x_stdout, "ERR\n");
917 if (sscanf(buf, "%u ", &mux_id) != 1) {
918 DEBUG(0, ("Invalid Request - no multiplex id\n"));
919 x_fprintf(x_stdout, "ERR\n");
923 mux_private = talloc(NULL, struct mux_private);
924 mux_private->max_mux = 0;
925 mux_private->private_pointers = NULL;
930 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
931 x_fprintf(x_stdout, "ERR\n");
935 if (mux_id >= mux_private->max_mux) {
936 unsigned int prev_max = mux_private->max_mux;
937 mux_private->max_mux = mux_id + 1;
938 mux_private->private_pointers
939 = talloc_realloc(mux_private,
940 mux_private->private_pointers,
941 void *, mux_private->max_mux);
942 memset(&mux_private->private_pointers[prev_max], '\0',
943 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
946 private = &mux_private->private_pointers[mux_id];
949 private = &normal_private;
952 fn(helper_mode, c, length, private, mux_id, private2);
955 static void squid_stream(enum stdio_helper_mode stdio_mode,
956 stdio_helper_function fn) {
957 /* initialize FDescs */
958 x_setbuf(x_stdout, NULL);
959 x_setbuf(x_stderr, NULL);
961 manage_squid_request(stdio_mode, fn, NULL);
978 OPT_USER_SESSION_KEY,
980 OPT_REQUIRE_MEMBERSHIP,
982 OPT_USE_CACHED_CREDS,
985 int main(int argc, const char **argv)
987 static const char *helper_protocol;
992 /* NOTE: DO NOT change this interface without considering the implications!
993 This is an external interface, which other programs will use to interact
997 /* We do not use single-letter command abbreviations, because they harm future
998 interface stability. */
1000 struct poptOption long_options[] = {
1002 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1003 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1004 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1005 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1006 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1007 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1008 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1014 /* Samba client initialisation */
1016 setup_logging(NULL, DEBUG_STDERR);
1020 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1022 /* Parse command line options */
1025 poptPrintHelp(pc, stderr, 0);
1029 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1030 POPT_CONTEXT_KEEP_FIRST);
1032 while((opt = poptGetNextOpt(pc)) != -1) {
1038 fprintf(stderr, "%s: %s\n",
1039 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1046 if (opt_domain == NULL) {
1047 opt_domain = lp_workgroup();
1050 if (helper_protocol) {
1052 for (i=0; i<NUM_HELPER_MODES; i++) {
1053 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1054 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1058 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1060 for (i=0; i<NUM_HELPER_MODES; i++) {
1061 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1067 if (!opt_username) {
1068 x_fprintf(x_stderr, "username must be specified!\n\n");
1069 poptPrintHelp(pc, stderr, 0);
1073 if (opt_workstation == NULL) {
1074 opt_workstation = lp_netbios_name();
1077 if (!opt_password) {
1078 opt_password = getpass("password: ");
1084 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
1085 if (!check_plaintext_auth(user, opt_password, True)) {
1092 poptFreeContext(pc);