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"
27 #include "lib/ldb/include/ldb.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/auth.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
32 #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"
41 #define SQUID_BUFFER_SIZE 2010
43 enum stdio_helper_mode {
54 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
55 #define NTLM_AUTH_FLAG_LMKEY 0x0008
58 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
59 char *buf, int length, void **private,
60 unsigned int mux_id, void **private2);
62 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
63 char *buf, int length, void **private,
64 unsigned int mux_id, void **private2);
66 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
67 char *buf, int length, void **private,
68 unsigned int mux_id, void **private2);
70 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
71 char *buf, int length, void **private,
72 unsigned int mux_id, void **private2);
74 static void manage_squid_request(enum stdio_helper_mode helper_mode,
75 stdio_helper_function fn, void **private2);
78 enum stdio_helper_mode mode;
80 stdio_helper_function fn;
81 } stdio_helper_protocols[] = {
82 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
83 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
84 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
85 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
86 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
87 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
88 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
89 { NUM_HELPER_MODES, NULL, NULL}
92 extern int winbindd_fd;
94 static const char *opt_username;
95 static const char *opt_domain;
96 static const char *opt_workstation;
97 static const char *opt_password;
98 static int opt_multiplex;
99 static int use_cached_creds;
102 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
104 static void mux_printf(unsigned int mux_id, const char *format, ...)
109 x_fprintf(x_stdout, "%d ", mux_id);
112 va_start(ap, format);
113 x_vfprintf(x_stdout, format, ap);
119 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
120 form DOMAIN/user into a domain and a user */
122 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
126 char *p = strchr(domuser,*lp_winbind_separator());
133 fstrcpy(domain, domuser);
134 domain[PTR_DIFF(p, domuser)] = 0;
140 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
142 static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
144 DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
145 ret.length = ldb_base64_decode((char *)ret.data);
150 * Encode a base64 string into a talloc()ed string caller to free.
152 static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
154 return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
158 * Decode a base64 string in-place - wrapper for the above
160 static void base64_decode_inplace(char *s)
162 ldb_base64_decode(s);
167 /* Authenticate a user with a plaintext password */
169 static BOOL check_plaintext_auth(const char *user, const char *pass,
170 BOOL stdout_diagnostics)
172 return (strcmp(pass, opt_password) == 0);
175 /* authenticate a user with an encrypted username/password */
177 static NTSTATUS local_pw_check_specified(const char *username,
179 const char *workstation,
180 const DATA_BLOB *challenge,
181 const DATA_BLOB *lm_response,
182 const DATA_BLOB *nt_response,
184 DATA_BLOB *lm_session_key,
185 DATA_BLOB *user_session_key,
190 struct samr_Password lm_pw, nt_pw;
191 struct samr_Password *lm_pwd, *nt_pwd;
192 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
194 nt_status = NT_STATUS_NO_MEMORY;
197 E_md4hash(opt_password, nt_pw.hash);
198 if (E_deshash(opt_password, lm_pw.hash)) {
206 nt_status = ntlm_password_check(mem_ctx,
207 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
208 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
215 lm_pwd, nt_pwd, user_session_key, lm_session_key);
217 if (NT_STATUS_IS_OK(nt_status)) {
221 *lp_winbind_separator(),
225 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
226 domain, username, workstation,
227 nt_errstr(nt_status)));
229 talloc_free(mem_ctx);
232 *error_string = strdup(nt_errstr(nt_status));
239 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
240 char *buf, int length, void **private,
241 unsigned int mux_id, void **private2)
246 pass=memchr(buf,' ',length);
248 DEBUG(2, ("Password not found. Denying access\n"));
249 mux_printf(mux_id, "ERR\n");
255 if (stdio_helper_mode == SQUID_2_5_BASIC) {
256 rfc1738_unescape(user);
257 rfc1738_unescape(pass);
260 if (check_plaintext_auth(user, pass, False)) {
261 mux_printf(mux_id, "OK\n");
263 mux_printf(mux_id, "ERR\n");
267 /* This is a bit hairy, but the basic idea is to do a password callback
268 to the calling application. The callback comes from within gensec */
270 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
271 char *buf, int length, void **private,
272 unsigned int mux_id, void **password)
275 if (strlen(buf) < 2) {
276 DEBUG(1, ("query [%s] invalid", buf));
277 mux_printf(mux_id, "BH\n");
281 if (strlen(buf) > 3) {
282 in = base64_decode_data_blob(NULL, buf + 3);
284 in = data_blob(NULL, 0);
287 if (strncmp(buf, "PW ", 3) == 0) {
289 *password = talloc_strndup(*private /* hopefully the right gensec context, useful to use for talloc */,
290 (const char *)in.data, in.length);
292 if (*password == NULL) {
293 DEBUG(1, ("Out of memory\n"));
294 mux_printf(mux_id, "BH\n");
299 mux_printf(mux_id, "OK\n");
303 DEBUG(1, ("Asked for (and expected) a password\n"));
304 mux_printf(mux_id, "BH\n");
309 * Callback for password credentails. This is not async, and when
310 * GENSEC and the credentails code is made async, it will look rather
314 static const char *get_password(struct cli_credentials *credentials)
316 char *password = NULL;
318 /* Ask for a password */
319 mux_printf((unsigned int)credentials->priv_data, "PW\n");
320 credentials->priv_data = NULL;
322 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
326 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
328 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
329 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
330 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
332 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
333 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
334 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
336 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
337 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
338 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
342 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
343 char *buf, int length, void **private,
344 unsigned int mux_id, void **private2)
347 DATA_BLOB out = data_blob(NULL, 0);
348 char *out_base64 = NULL;
349 const char *reply_arg = NULL;
350 struct gensec_ntlm_state {
351 struct gensec_security *gensec_state;
352 const char *set_password;
354 struct gensec_ntlm_state *state;
355 struct event_context *ev;
356 struct messaging_context *msg;
360 const char *reply_code;
361 struct cli_credentials *creds;
363 static char *want_feature_list = NULL;
364 static DATA_BLOB session_key;
371 state = talloc_zero(NULL, struct gensec_ntlm_state);
373 mux_printf(mux_id, "BH No Memory\n");
378 state->set_password = opt_password;
382 if (strlen(buf) < 2) {
383 DEBUG(1, ("query [%s] invalid", buf));
384 mux_printf(mux_id, "BH\n");
388 if (strlen(buf) > 3) {
389 if(strncmp(buf, "SF ", 3) == 0) {
390 DEBUG(10, ("Setting flags to negotiate\n"));
391 talloc_free(want_feature_list);
392 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
393 mux_printf(mux_id, "OK\n");
396 in = base64_decode_data_blob(NULL, buf + 3);
398 in = data_blob(NULL, 0);
401 if (strncmp(buf, "YR", 2) == 0) {
402 if (state->gensec_state) {
403 talloc_free(state->gensec_state);
404 state->gensec_state = NULL;
406 } else if ( (strncmp(buf, "OK", 2) == 0)) {
407 /* Just return BH, like ntlm_auth from Samba 3 does. */
408 mux_printf(mux_id, "BH\n");
411 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
412 (strncmp(buf, "KK ", 3) != 0) &&
413 (strncmp(buf, "AF ", 3) != 0) &&
414 (strncmp(buf, "NA ", 3) != 0) &&
415 (strncmp(buf, "UG", 2) != 0) &&
416 (strncmp(buf, "PW ", 3) != 0) &&
417 (strncmp(buf, "GK", 2) != 0) &&
418 (strncmp(buf, "GF", 2) != 0)) {
419 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
420 mux_printf(mux_id, "BH\n");
426 if (!(state->gensec_state)) {
427 switch (stdio_helper_mode) {
428 case GSS_SPNEGO_CLIENT:
429 case NTLMSSP_CLIENT_1:
430 /* setup the client side */
432 nt_status = gensec_client_start(NULL, &state->gensec_state, NULL);
433 if (!NT_STATUS_IS_OK(nt_status)) {
438 case GSS_SPNEGO_SERVER:
439 case SQUID_2_5_NTLMSSP:
440 ev = event_context_init(state);
444 msg = messaging_client_init(state, ev);
448 if (!NT_STATUS_IS_OK(gensec_server_start(state, ev, msg, &state->gensec_state))) {
456 creds = cli_credentials_init(state->gensec_state);
457 cli_credentials_set_conf(creds);
459 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
462 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
464 if (state->set_password) {
465 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
467 cli_credentials_set_password_callback(creds, get_password);
468 creds->priv_data = (void*)mux_id;
470 if (opt_workstation) {
471 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
474 switch (stdio_helper_mode) {
475 case GSS_SPNEGO_SERVER:
476 case SQUID_2_5_NTLMSSP:
477 cli_credentials_set_machine_account(creds);
483 gensec_set_credentials(state->gensec_state, creds);
484 gensec_want_feature_list(state->gensec_state, want_feature_list);
486 switch (stdio_helper_mode) {
487 case GSS_SPNEGO_CLIENT:
488 case GSS_SPNEGO_SERVER:
489 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
494 case NTLMSSP_CLIENT_1:
499 case SQUID_2_5_NTLMSSP:
500 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
506 if (!NT_STATUS_IS_OK(nt_status)) {
507 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
508 mux_printf(mux_id, "BH\n");
515 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
517 if (strncmp(buf, "PW ", 3) == 0) {
518 state->set_password = talloc_strndup(state,
519 (const char *)in.data,
522 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
525 mux_printf(mux_id, "OK\n");
527 talloc_free(mem_ctx);
531 if (strncmp(buf, "UG", 2) == 0) {
533 char *grouplist = NULL;
534 struct auth_session_info *session_info;
536 nt_status = gensec_session_info(state->gensec_state, &session_info);
537 if (!NT_STATUS_IS_OK(nt_status)) {
538 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
539 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
541 talloc_free(mem_ctx);
545 /* get the string onto the context */
546 grouplist = talloc_strdup(mem_ctx, "");
548 for (i=0; i<session_info->security_token->num_sids; i++) {
549 struct security_token *token = session_info->security_token;
550 const char *sidstr = dom_sid_string(session_info,
552 grouplist = talloc_asprintf_append(grouplist, "%s,", sidstr);
555 mux_printf(mux_id, "GL %s\n", grouplist);
556 talloc_free(session_info);
558 talloc_free(mem_ctx);
562 if (strncmp(buf, "GK", 2) == 0) {
564 DEBUG(10, ("Requested session key\n"));
565 nt_status = gensec_session_key(state->gensec_state, &session_key);
566 if(!NT_STATUS_IS_OK(nt_status)) {
567 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
568 mux_printf(mux_id, "BH No session key\n");
569 talloc_free(mem_ctx);
572 base64_key = base64_encode_data_blob(state, session_key);
573 mux_printf(mux_id, "GK %s\n", base64_key);
574 talloc_free(base64_key);
576 talloc_free(mem_ctx);
580 if (strncmp(buf, "GF", 2) == 0) {
581 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
584 gensec_ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
585 struct gensec_ntlmssp_state);
586 neg_flags = gensec_ntlmssp_state->neg_flags;
588 DEBUG(10, ("Requested negotiated feature flags\n"));
589 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
593 nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
595 /* don't leak 'bad password'/'no such user' info to the network client */
596 nt_status = auth_nt_status_squash(nt_status);
599 out_base64 = base64_encode_data_blob(mem_ctx, out);
604 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
608 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
610 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
617 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
619 reply_arg = nt_errstr(nt_status);
620 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
621 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
623 reply_arg = nt_errstr(nt_status);
624 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
625 } else if (!NT_STATUS_IS_OK(nt_status)) {
627 reply_arg = nt_errstr(nt_status);
628 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
629 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
630 struct auth_session_info *session_info;
632 nt_status = gensec_session_info(state->gensec_state, &session_info);
633 if (!NT_STATUS_IS_OK(nt_status)) {
635 reply_arg = nt_errstr(nt_status);
636 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
640 reply_arg = talloc_asprintf(state->gensec_state,
641 "%s%s%s", session_info->server_info->domain_name,
642 lp_winbind_separator(), session_info->server_info->account_name);
643 talloc_free(session_info);
645 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
647 reply_arg = out_base64;
652 switch (stdio_helper_mode) {
653 case GSS_SPNEGO_SERVER:
654 mux_printf(mux_id, "%s %s %s\n", reply_code,
655 out_base64 ? out_base64 : "*",
656 reply_arg ? reply_arg : "*");
660 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
661 } else if (reply_arg) {
662 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
664 mux_printf(mux_id, "%s\n", reply_code);
668 talloc_free(mem_ctx);
672 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
673 char *buf, int length, void **private,
674 unsigned int mux_id, void **private2)
676 char *request, *parameter;
677 static DATA_BLOB challenge;
678 static DATA_BLOB lm_response;
679 static DATA_BLOB nt_response;
680 static char *full_username;
681 static char *username;
683 static char *plaintext_password;
684 static BOOL ntlm_server_1_user_session_key;
685 static BOOL ntlm_server_1_lm_session_key;
687 if (strequal(buf, ".")) {
688 if (!full_username && !username) {
689 mux_printf(mux_id, "Error: No username supplied!\n");
690 } else if (plaintext_password) {
691 /* handle this request as plaintext */
692 if (!full_username) {
693 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
694 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
698 if (check_plaintext_auth(full_username, plaintext_password, False)) {
699 mux_printf(mux_id, "Authenticated: Yes\n");
701 mux_printf(mux_id, "Authenticated: No\n");
703 } else if (!lm_response.data && !nt_response.data) {
704 mux_printf(mux_id, "Error: No password supplied!\n");
705 } else if (!challenge.data) {
706 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
708 char *error_string = NULL;
710 DATA_BLOB user_session_key;
713 if (full_username && !username) {
717 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
718 /* username might be 'tainted', don't print into our new-line deleimianted stream */
719 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
723 username = smb_xstrdup(fstr_user);
724 domain = smb_xstrdup(fstr_domain);
728 domain = smb_xstrdup(lp_workgroup());
731 if (ntlm_server_1_lm_session_key)
732 flags |= NTLM_AUTH_FLAG_LMKEY;
734 if (ntlm_server_1_user_session_key)
735 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
737 if (!NT_STATUS_IS_OK(
738 local_pw_check_specified(username,
750 mux_printf(mux_id, "Authenticated: No\n");
751 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
752 SAFE_FREE(error_string);
754 static char zeros[16];
756 char *hex_user_session_key;
758 mux_printf(mux_id, "Authenticated: Yes\n");
760 if (ntlm_server_1_lm_session_key
762 && (memcmp(zeros, lm_key.data,
763 lm_key.length) != 0)) {
764 hex_encode(lm_key.data,
767 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
768 SAFE_FREE(hex_lm_key);
771 if (ntlm_server_1_user_session_key
772 && user_session_key.length
773 && (memcmp(zeros, user_session_key.data,
774 user_session_key.length) != 0)) {
775 hex_encode(user_session_key.data,
776 user_session_key.length,
777 &hex_user_session_key);
778 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
779 SAFE_FREE(hex_user_session_key);
783 /* clear out the state */
784 challenge = data_blob(NULL, 0);
785 nt_response = data_blob(NULL, 0);
786 lm_response = data_blob(NULL, 0);
787 SAFE_FREE(full_username);
790 SAFE_FREE(plaintext_password);
791 ntlm_server_1_user_session_key = False;
792 ntlm_server_1_lm_session_key = False;
793 mux_printf(mux_id, ".\n");
800 /* Indicates a base64 encoded structure */
801 parameter = strstr(request, ":: ");
803 parameter = strstr(request, ": ");
806 DEBUG(0, ("Parameter not found!\n"));
807 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
824 base64_decode_inplace(parameter);
827 if (strequal(request, "LANMAN-Challenge")) {
828 challenge = strhex_to_data_blob(parameter);
829 if (challenge.length != 8) {
830 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
832 (int)challenge.length);
833 challenge = data_blob(NULL, 0);
835 } else if (strequal(request, "NT-Response")) {
836 nt_response = strhex_to_data_blob(parameter);
837 if (nt_response.length < 24) {
838 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
840 (int)nt_response.length);
841 nt_response = data_blob(NULL, 0);
843 } else if (strequal(request, "LANMAN-Response")) {
844 lm_response = strhex_to_data_blob(parameter);
845 if (lm_response.length != 24) {
846 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
848 (int)lm_response.length);
849 lm_response = data_blob(NULL, 0);
851 } else if (strequal(request, "Password")) {
852 plaintext_password = smb_xstrdup(parameter);
853 } else if (strequal(request, "NT-Domain")) {
854 domain = smb_xstrdup(parameter);
855 } else if (strequal(request, "Username")) {
856 username = smb_xstrdup(parameter);
857 } else if (strequal(request, "Full-Username")) {
858 full_username = smb_xstrdup(parameter);
859 } else if (strequal(request, "Request-User-Session-Key")) {
860 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
861 } else if (strequal(request, "Request-LanMan-Session-Key")) {
862 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
864 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
868 static void manage_squid_request(enum stdio_helper_mode helper_mode,
869 stdio_helper_function fn, void **private2)
871 char buf[SQUID_BUFFER_SIZE+1];
872 unsigned int mux_id = 0;
877 unsigned int max_mux;
878 void **private_pointers;
881 static struct mux_private *mux_private;
882 static void *normal_private;
885 /* this is not a typo - x_fgets doesn't work too well under squid */
886 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
888 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
889 strerror(ferror(stdin))));
891 exit(1); /* BIIG buffer */
896 c=memchr(buf,'\n',sizeof(buf)-1);
905 DEBUG(0, ("Oversized message\n"));
906 x_fprintf(x_stdout, "ERR\n");
911 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
913 if (buf[0] == '\0') {
914 DEBUG(0, ("Invalid Request (empty)\n"));
915 x_fprintf(x_stdout, "ERR\n");
920 if (sscanf(buf, "%u ", &mux_id) != 1) {
921 DEBUG(0, ("Invalid Request - no multiplex id\n"));
922 x_fprintf(x_stdout, "ERR\n");
926 mux_private = talloc(NULL, struct mux_private);
927 mux_private->max_mux = 0;
928 mux_private->private_pointers = NULL;
933 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
934 x_fprintf(x_stdout, "ERR\n");
938 if (mux_id >= mux_private->max_mux) {
939 unsigned int prev_max = mux_private->max_mux;
940 mux_private->max_mux = mux_id + 1;
941 mux_private->private_pointers
942 = talloc_realloc(mux_private,
943 mux_private->private_pointers,
944 void *, mux_private->max_mux);
945 memset(&mux_private->private_pointers[prev_max], '\0',
946 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
949 private = &mux_private->private_pointers[mux_id];
952 private = &normal_private;
955 fn(helper_mode, c, length, private, mux_id, private2);
958 static void squid_stream(enum stdio_helper_mode stdio_mode,
959 stdio_helper_function fn) {
960 /* initialize FDescs */
961 x_setbuf(x_stdout, NULL);
962 x_setbuf(x_stderr, NULL);
964 manage_squid_request(stdio_mode, fn, NULL);
981 OPT_USER_SESSION_KEY,
983 OPT_REQUIRE_MEMBERSHIP,
985 OPT_USE_CACHED_CREDS,
988 int main(int argc, const char **argv)
990 static const char *helper_protocol;
995 /* NOTE: DO NOT change this interface without considering the implications!
996 This is an external interface, which other programs will use to interact
1000 /* We do not use single-letter command abbreviations, because they harm future
1001 interface stability. */
1003 struct poptOption long_options[] = {
1005 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1006 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1007 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1008 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1009 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1010 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1011 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1017 /* Samba client initialisation */
1019 setup_logging(NULL, DEBUG_STDERR);
1023 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1025 /* Parse command line options */
1028 poptPrintHelp(pc, stderr, 0);
1032 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1033 POPT_CONTEXT_KEEP_FIRST);
1035 while((opt = poptGetNextOpt(pc)) != -1) {
1041 fprintf(stderr, "%s: %s\n",
1042 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1049 if (opt_domain == NULL) {
1050 opt_domain = lp_workgroup();
1053 if (helper_protocol) {
1055 for (i=0; i<NUM_HELPER_MODES; i++) {
1056 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1057 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1061 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1063 for (i=0; i<NUM_HELPER_MODES; i++) {
1064 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1070 if (!opt_username) {
1071 x_fprintf(x_stderr, "username must be specified!\n\n");
1072 poptPrintHelp(pc, stderr, 0);
1076 if (opt_workstation == NULL) {
1077 opt_workstation = lp_netbios_name();
1080 if (!opt_password) {
1081 opt_password = getpass("password: ");
1087 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
1088 if (!check_plaintext_auth(user, opt_password, True)) {
1095 poptFreeContext(pc);