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 "lib/ldb/include/ldb.h"
29 #include "auth/credentials/credentials.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/auth.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #include "auth/auth_sam.h"
35 #include "libcli/auth/libcli_auth.h"
36 #include "libcli/security/security.h"
37 #include "lib/events/events.h"
38 #include "lib/messaging/messaging.h"
39 #include "lib/messaging/irpc.h"
40 #include "auth/ntlmssp/ntlmssp.h"
42 #define SQUID_BUFFER_SIZE 2010
44 enum stdio_helper_mode {
55 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
56 #define NTLM_AUTH_FLAG_LMKEY 0x0008
59 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
60 char *buf, int length, void **private,
61 unsigned int mux_id, void **private2);
63 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
64 char *buf, int length, void **private,
65 unsigned int mux_id, void **private2);
67 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
68 char *buf, int length, void **private,
69 unsigned int mux_id, void **private2);
71 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
72 char *buf, int length, void **private,
73 unsigned int mux_id, void **private2);
75 static void manage_squid_request(enum stdio_helper_mode helper_mode,
76 stdio_helper_function fn, void **private2);
79 enum stdio_helper_mode mode;
81 stdio_helper_function fn;
82 } stdio_helper_protocols[] = {
83 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
84 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
85 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
86 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
87 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
88 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
89 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
90 { NUM_HELPER_MODES, NULL, NULL}
93 extern int winbindd_fd;
95 static const char *opt_username;
96 static const char *opt_domain;
97 static const char *opt_workstation;
98 static const char *opt_password;
99 static int opt_multiplex;
100 static int use_cached_creds;
103 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
105 static void mux_printf(unsigned int mux_id, const char *format, ...)
110 x_fprintf(x_stdout, "%d ", mux_id);
113 va_start(ap, format);
114 x_vfprintf(x_stdout, format, ap);
120 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
121 form DOMAIN/user into a domain and a user */
123 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
127 char *p = strchr(domuser,*lp_winbind_separator());
134 fstrcpy(domain, domuser);
135 domain[PTR_DIFF(p, domuser)] = 0;
141 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
143 static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
145 DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
146 ret.length = ldb_base64_decode((char *)ret.data);
151 * Encode a base64 string into a talloc()ed string caller to free.
153 static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
155 return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
159 * Decode a base64 string in-place - wrapper for the above
161 static void base64_decode_inplace(char *s)
163 ldb_base64_decode(s);
168 /* Authenticate a user with a plaintext password */
170 static BOOL check_plaintext_auth(const char *user, const char *pass,
171 BOOL stdout_diagnostics)
173 return (strcmp(pass, opt_password) == 0);
176 /* authenticate a user with an encrypted username/password */
178 static NTSTATUS local_pw_check_specified(const char *username,
180 const char *workstation,
181 const DATA_BLOB *challenge,
182 const DATA_BLOB *lm_response,
183 const DATA_BLOB *nt_response,
185 DATA_BLOB *lm_session_key,
186 DATA_BLOB *user_session_key,
191 struct samr_Password lm_pw, nt_pw;
192 struct samr_Password *lm_pwd, *nt_pwd;
193 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
195 nt_status = NT_STATUS_NO_MEMORY;
198 E_md4hash(opt_password, nt_pw.hash);
199 if (E_deshash(opt_password, lm_pw.hash)) {
207 nt_status = ntlm_password_check(mem_ctx,
208 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
209 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
216 lm_pwd, nt_pwd, user_session_key, lm_session_key);
218 if (NT_STATUS_IS_OK(nt_status)) {
222 *lp_winbind_separator(),
226 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
227 domain, username, workstation,
228 nt_errstr(nt_status)));
230 talloc_free(mem_ctx);
233 *error_string = strdup(nt_errstr(nt_status));
240 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
241 char *buf, int length, void **private,
242 unsigned int mux_id, void **private2)
247 pass=memchr(buf,' ',length);
249 DEBUG(2, ("Password not found. Denying access\n"));
250 mux_printf(mux_id, "ERR\n");
256 if (stdio_helper_mode == SQUID_2_5_BASIC) {
257 rfc1738_unescape(user);
258 rfc1738_unescape(pass);
261 if (check_plaintext_auth(user, pass, False)) {
262 mux_printf(mux_id, "OK\n");
264 mux_printf(mux_id, "ERR\n");
268 /* This is a bit hairy, but the basic idea is to do a password callback
269 to the calling application. The callback comes from within gensec */
271 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
272 char *buf, int length, void **private,
273 unsigned int mux_id, void **password)
276 if (strlen(buf) < 2) {
277 DEBUG(1, ("query [%s] invalid", buf));
278 mux_printf(mux_id, "BH\n");
282 if (strlen(buf) > 3) {
283 in = base64_decode_data_blob(NULL, buf + 3);
285 in = data_blob(NULL, 0);
288 if (strncmp(buf, "PW ", 3) == 0) {
290 *password = talloc_strndup(*private /* hopefully the right gensec context, useful to use for talloc */,
291 (const char *)in.data, in.length);
293 if (*password == NULL) {
294 DEBUG(1, ("Out of memory\n"));
295 mux_printf(mux_id, "BH\n");
300 mux_printf(mux_id, "OK\n");
304 DEBUG(1, ("Asked for (and expected) a password\n"));
305 mux_printf(mux_id, "BH\n");
310 * Callback for password credentails. This is not async, and when
311 * GENSEC and the credentails code is made async, it will look rather
315 static const char *get_password(struct cli_credentials *credentials)
317 char *password = NULL;
319 /* Ask for a password */
320 mux_printf((unsigned int)credentials->priv_data, "PW\n");
321 credentials->priv_data = NULL;
323 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
327 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
329 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
330 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
331 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
333 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
334 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
335 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
337 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
338 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
339 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
343 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
344 char *buf, int length, void **private,
345 unsigned int mux_id, void **private2)
348 DATA_BLOB out = data_blob(NULL, 0);
349 char *out_base64 = NULL;
350 const char *reply_arg = NULL;
351 struct gensec_ntlm_state {
352 struct gensec_security *gensec_state;
353 const char *set_password;
355 struct gensec_ntlm_state *state;
356 struct event_context *ev;
357 struct messaging_context *msg;
361 const char *reply_code;
362 struct cli_credentials *creds;
364 static char *want_feature_list = NULL;
365 static DATA_BLOB session_key;
372 state = talloc_zero(NULL, struct gensec_ntlm_state);
374 mux_printf(mux_id, "BH No Memory\n");
379 state->set_password = opt_password;
383 if (strlen(buf) < 2) {
384 DEBUG(1, ("query [%s] invalid", buf));
385 mux_printf(mux_id, "BH\n");
389 if (strlen(buf) > 3) {
390 if(strncmp(buf, "SF ", 3) == 0) {
391 DEBUG(10, ("Setting flags to negotiate\n"));
392 talloc_free(want_feature_list);
393 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
394 mux_printf(mux_id, "OK\n");
397 in = base64_decode_data_blob(NULL, buf + 3);
399 in = data_blob(NULL, 0);
402 if (strncmp(buf, "YR", 2) == 0) {
403 if (state->gensec_state) {
404 talloc_free(state->gensec_state);
405 state->gensec_state = NULL;
407 } else if ( (strncmp(buf, "OK", 2) == 0)) {
408 /* Just return BH, like ntlm_auth from Samba 3 does. */
409 mux_printf(mux_id, "BH\n");
412 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
413 (strncmp(buf, "KK ", 3) != 0) &&
414 (strncmp(buf, "AF ", 3) != 0) &&
415 (strncmp(buf, "NA ", 3) != 0) &&
416 (strncmp(buf, "UG", 2) != 0) &&
417 (strncmp(buf, "PW ", 3) != 0) &&
418 (strncmp(buf, "GK", 2) != 0) &&
419 (strncmp(buf, "GF", 2) != 0)) {
420 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
421 mux_printf(mux_id, "BH\n");
427 if (!(state->gensec_state)) {
428 switch (stdio_helper_mode) {
429 case GSS_SPNEGO_CLIENT:
430 case NTLMSSP_CLIENT_1:
431 /* setup the client side */
433 nt_status = gensec_client_start(NULL, &state->gensec_state, NULL);
434 if (!NT_STATUS_IS_OK(nt_status)) {
439 case GSS_SPNEGO_SERVER:
440 case SQUID_2_5_NTLMSSP:
441 ev = event_context_init(state);
445 msg = messaging_client_init(state, ev);
449 if (!NT_STATUS_IS_OK(gensec_server_start(state, ev, msg, &state->gensec_state))) {
457 creds = cli_credentials_init(state->gensec_state);
458 cli_credentials_set_conf(creds);
460 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
463 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
465 if (state->set_password) {
466 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
468 cli_credentials_set_password_callback(creds, get_password);
469 creds->priv_data = (void*)mux_id;
471 if (opt_workstation) {
472 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
475 switch (stdio_helper_mode) {
476 case GSS_SPNEGO_SERVER:
477 case SQUID_2_5_NTLMSSP:
478 cli_credentials_set_machine_account(creds);
484 gensec_set_credentials(state->gensec_state, creds);
485 gensec_want_feature_list(state->gensec_state, want_feature_list);
487 switch (stdio_helper_mode) {
488 case GSS_SPNEGO_CLIENT:
489 case GSS_SPNEGO_SERVER:
490 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
495 case NTLMSSP_CLIENT_1:
500 case SQUID_2_5_NTLMSSP:
501 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
507 if (!NT_STATUS_IS_OK(nt_status)) {
508 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
509 mux_printf(mux_id, "BH\n");
516 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
518 if (strncmp(buf, "PW ", 3) == 0) {
519 state->set_password = talloc_strndup(state,
520 (const char *)in.data,
523 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
526 mux_printf(mux_id, "OK\n");
528 talloc_free(mem_ctx);
532 if (strncmp(buf, "UG", 2) == 0) {
534 char *grouplist = NULL;
535 struct auth_session_info *session_info;
537 nt_status = gensec_session_info(state->gensec_state, &session_info);
538 if (!NT_STATUS_IS_OK(nt_status)) {
539 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
540 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
542 talloc_free(mem_ctx);
546 /* get the string onto the context */
547 grouplist = talloc_strdup(mem_ctx, "");
549 for (i=0; i<session_info->security_token->num_sids; i++) {
550 struct security_token *token = session_info->security_token;
551 const char *sidstr = dom_sid_string(session_info,
553 grouplist = talloc_asprintf_append(grouplist, "%s,", sidstr);
556 mux_printf(mux_id, "GL %s\n", grouplist);
557 talloc_free(session_info);
559 talloc_free(mem_ctx);
563 if (strncmp(buf, "GK", 2) == 0) {
565 DEBUG(10, ("Requested session key\n"));
566 nt_status = gensec_session_key(state->gensec_state, &session_key);
567 if(!NT_STATUS_IS_OK(nt_status)) {
568 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
569 mux_printf(mux_id, "BH No session key\n");
570 talloc_free(mem_ctx);
573 base64_key = base64_encode_data_blob(state, session_key);
574 mux_printf(mux_id, "GK %s\n", base64_key);
575 talloc_free(base64_key);
577 talloc_free(mem_ctx);
581 if (strncmp(buf, "GF", 2) == 0) {
582 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
585 gensec_ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
586 struct gensec_ntlmssp_state);
587 neg_flags = gensec_ntlmssp_state->neg_flags;
589 DEBUG(10, ("Requested negotiated feature flags\n"));
590 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
594 nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
596 /* don't leak 'bad password'/'no such user' info to the network client */
597 nt_status = auth_nt_status_squash(nt_status);
600 out_base64 = base64_encode_data_blob(mem_ctx, out);
605 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
609 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
611 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
618 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
620 reply_arg = nt_errstr(nt_status);
621 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
622 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
624 reply_arg = nt_errstr(nt_status);
625 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
626 } else if (!NT_STATUS_IS_OK(nt_status)) {
628 reply_arg = nt_errstr(nt_status);
629 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
630 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
631 struct auth_session_info *session_info;
633 nt_status = gensec_session_info(state->gensec_state, &session_info);
634 if (!NT_STATUS_IS_OK(nt_status)) {
636 reply_arg = nt_errstr(nt_status);
637 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
641 reply_arg = talloc_asprintf(state->gensec_state,
642 "%s%s%s", session_info->server_info->domain_name,
643 lp_winbind_separator(), session_info->server_info->account_name);
644 talloc_free(session_info);
646 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
648 reply_arg = out_base64;
653 switch (stdio_helper_mode) {
654 case GSS_SPNEGO_SERVER:
655 mux_printf(mux_id, "%s %s %s\n", reply_code,
656 out_base64 ? out_base64 : "*",
657 reply_arg ? reply_arg : "*");
661 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
662 } else if (reply_arg) {
663 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
665 mux_printf(mux_id, "%s\n", reply_code);
669 talloc_free(mem_ctx);
673 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
674 char *buf, int length, void **private,
675 unsigned int mux_id, void **private2)
677 char *request, *parameter;
678 static DATA_BLOB challenge;
679 static DATA_BLOB lm_response;
680 static DATA_BLOB nt_response;
681 static char *full_username;
682 static char *username;
684 static char *plaintext_password;
685 static BOOL ntlm_server_1_user_session_key;
686 static BOOL ntlm_server_1_lm_session_key;
688 if (strequal(buf, ".")) {
689 if (!full_username && !username) {
690 mux_printf(mux_id, "Error: No username supplied!\n");
691 } else if (plaintext_password) {
692 /* handle this request as plaintext */
693 if (!full_username) {
694 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
695 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
699 if (check_plaintext_auth(full_username, plaintext_password, False)) {
700 mux_printf(mux_id, "Authenticated: Yes\n");
702 mux_printf(mux_id, "Authenticated: No\n");
704 } else if (!lm_response.data && !nt_response.data) {
705 mux_printf(mux_id, "Error: No password supplied!\n");
706 } else if (!challenge.data) {
707 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
709 char *error_string = NULL;
711 DATA_BLOB user_session_key;
714 if (full_username && !username) {
718 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
719 /* username might be 'tainted', don't print into our new-line deleimianted stream */
720 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
724 username = smb_xstrdup(fstr_user);
725 domain = smb_xstrdup(fstr_domain);
729 domain = smb_xstrdup(lp_workgroup());
732 if (ntlm_server_1_lm_session_key)
733 flags |= NTLM_AUTH_FLAG_LMKEY;
735 if (ntlm_server_1_user_session_key)
736 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
738 if (!NT_STATUS_IS_OK(
739 local_pw_check_specified(username,
751 mux_printf(mux_id, "Authenticated: No\n");
752 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
753 SAFE_FREE(error_string);
755 static char zeros[16];
757 char *hex_user_session_key;
759 mux_printf(mux_id, "Authenticated: Yes\n");
761 if (ntlm_server_1_lm_session_key
763 && (memcmp(zeros, lm_key.data,
764 lm_key.length) != 0)) {
765 hex_encode(lm_key.data,
768 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
769 SAFE_FREE(hex_lm_key);
772 if (ntlm_server_1_user_session_key
773 && user_session_key.length
774 && (memcmp(zeros, user_session_key.data,
775 user_session_key.length) != 0)) {
776 hex_encode(user_session_key.data,
777 user_session_key.length,
778 &hex_user_session_key);
779 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
780 SAFE_FREE(hex_user_session_key);
784 /* clear out the state */
785 challenge = data_blob(NULL, 0);
786 nt_response = data_blob(NULL, 0);
787 lm_response = data_blob(NULL, 0);
788 SAFE_FREE(full_username);
791 SAFE_FREE(plaintext_password);
792 ntlm_server_1_user_session_key = False;
793 ntlm_server_1_lm_session_key = False;
794 mux_printf(mux_id, ".\n");
801 /* Indicates a base64 encoded structure */
802 parameter = strstr(request, ":: ");
804 parameter = strstr(request, ": ");
807 DEBUG(0, ("Parameter not found!\n"));
808 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
825 base64_decode_inplace(parameter);
828 if (strequal(request, "LANMAN-Challenge")) {
829 challenge = strhex_to_data_blob(parameter);
830 if (challenge.length != 8) {
831 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
833 (int)challenge.length);
834 challenge = data_blob(NULL, 0);
836 } else if (strequal(request, "NT-Response")) {
837 nt_response = strhex_to_data_blob(parameter);
838 if (nt_response.length < 24) {
839 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
841 (int)nt_response.length);
842 nt_response = data_blob(NULL, 0);
844 } else if (strequal(request, "LANMAN-Response")) {
845 lm_response = strhex_to_data_blob(parameter);
846 if (lm_response.length != 24) {
847 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
849 (int)lm_response.length);
850 lm_response = data_blob(NULL, 0);
852 } else if (strequal(request, "Password")) {
853 plaintext_password = smb_xstrdup(parameter);
854 } else if (strequal(request, "NT-Domain")) {
855 domain = smb_xstrdup(parameter);
856 } else if (strequal(request, "Username")) {
857 username = smb_xstrdup(parameter);
858 } else if (strequal(request, "Full-Username")) {
859 full_username = smb_xstrdup(parameter);
860 } else if (strequal(request, "Request-User-Session-Key")) {
861 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
862 } else if (strequal(request, "Request-LanMan-Session-Key")) {
863 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
865 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
869 static void manage_squid_request(enum stdio_helper_mode helper_mode,
870 stdio_helper_function fn, void **private2)
872 char buf[SQUID_BUFFER_SIZE+1];
873 unsigned int mux_id = 0;
878 unsigned int max_mux;
879 void **private_pointers;
882 static struct mux_private *mux_private;
883 static void *normal_private;
886 /* this is not a typo - x_fgets doesn't work too well under squid */
887 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
889 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
890 strerror(ferror(stdin))));
892 exit(1); /* BIIG buffer */
897 c=memchr(buf,'\n',sizeof(buf)-1);
906 DEBUG(0, ("Oversized message\n"));
907 x_fprintf(x_stdout, "ERR\n");
912 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
914 if (buf[0] == '\0') {
915 DEBUG(0, ("Invalid Request (empty)\n"));
916 x_fprintf(x_stdout, "ERR\n");
921 if (sscanf(buf, "%u ", &mux_id) != 1) {
922 DEBUG(0, ("Invalid Request - no multiplex id\n"));
923 x_fprintf(x_stdout, "ERR\n");
927 mux_private = talloc(NULL, struct mux_private);
928 mux_private->max_mux = 0;
929 mux_private->private_pointers = NULL;
934 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
935 x_fprintf(x_stdout, "ERR\n");
939 if (mux_id >= mux_private->max_mux) {
940 unsigned int prev_max = mux_private->max_mux;
941 mux_private->max_mux = mux_id + 1;
942 mux_private->private_pointers
943 = talloc_realloc(mux_private,
944 mux_private->private_pointers,
945 void *, mux_private->max_mux);
946 memset(&mux_private->private_pointers[prev_max], '\0',
947 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
950 private = &mux_private->private_pointers[mux_id];
953 private = &normal_private;
956 fn(helper_mode, c, length, private, mux_id, private2);
959 static void squid_stream(enum stdio_helper_mode stdio_mode,
960 stdio_helper_function fn) {
961 /* initialize FDescs */
962 x_setbuf(x_stdout, NULL);
963 x_setbuf(x_stderr, NULL);
965 manage_squid_request(stdio_mode, fn, NULL);
982 OPT_USER_SESSION_KEY,
984 OPT_REQUIRE_MEMBERSHIP,
986 OPT_USE_CACHED_CREDS,
989 int main(int argc, const char **argv)
991 static const char *helper_protocol;
996 /* NOTE: DO NOT change this interface without considering the implications!
997 This is an external interface, which other programs will use to interact
1001 /* We do not use single-letter command abbreviations, because they harm future
1002 interface stability. */
1004 struct poptOption long_options[] = {
1006 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1007 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1008 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1009 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1010 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1011 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1012 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1018 /* Samba client initialisation */
1020 setup_logging(NULL, DEBUG_STDERR);
1024 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1026 /* Parse command line options */
1029 poptPrintHelp(pc, stderr, 0);
1033 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1034 POPT_CONTEXT_KEEP_FIRST);
1036 while((opt = poptGetNextOpt(pc)) != -1) {
1042 fprintf(stderr, "%s: %s\n",
1043 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1050 if (opt_domain == NULL) {
1051 opt_domain = lp_workgroup();
1054 if (helper_protocol) {
1056 for (i=0; i<NUM_HELPER_MODES; i++) {
1057 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1058 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1062 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1064 for (i=0; i<NUM_HELPER_MODES; i++) {
1065 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1071 if (!opt_username) {
1072 x_fprintf(x_stderr, "username must be specified!\n\n");
1073 poptPrintHelp(pc, stderr, 0);
1077 if (opt_workstation == NULL) {
1078 opt_workstation = lp_netbios_name();
1081 if (!opt_password) {
1082 opt_password = getpass("password: ");
1088 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
1089 if (!check_plaintext_auth(user, opt_password, True)) {
1096 poptFreeContext(pc);