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"
40 #include "param/param.h"
42 #define INITIAL_BUFFER_SIZE 300
43 #define MAX_BUFFER_SIZE 63000
45 enum stdio_helper_mode {
56 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
57 #define NTLM_AUTH_FLAG_LMKEY 0x0008
60 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
61 char *buf, int length, void **private,
62 unsigned int mux_id, void **private2);
64 static void manage_squid_basic_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_gensec_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_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
73 char *buf, int length, void **private,
74 unsigned int mux_id, void **private2);
76 static void manage_squid_request(enum stdio_helper_mode helper_mode,
77 stdio_helper_function fn, void **private2);
80 enum stdio_helper_mode mode;
82 stdio_helper_function fn;
83 } stdio_helper_protocols[] = {
84 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
85 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
86 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
87 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
88 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
89 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
90 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
91 { NUM_HELPER_MODES, NULL, NULL}
94 extern int winbindd_fd;
96 static const char *opt_username;
97 static const char *opt_domain;
98 static const char *opt_workstation;
99 static const char *opt_password;
100 static int opt_multiplex;
101 static int use_cached_creds;
104 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
106 static void mux_printf(unsigned int mux_id, const char *format, ...)
111 x_fprintf(x_stdout, "%d ", mux_id);
114 va_start(ap, format);
115 x_vfprintf(x_stdout, format, ap);
121 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
122 form DOMAIN/user into a domain and a user */
124 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
128 char *p = strchr(domuser,*lp_winbind_separator(global_loadparm));
135 fstrcpy(domain, domuser);
136 domain[PTR_DIFF(p, domuser)] = 0;
142 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
144 static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
146 DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
147 ret.length = ldb_base64_decode((char *)ret.data);
152 * Encode a base64 string into a talloc()ed string caller to free.
154 static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
156 return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
160 * Decode a base64 string in-place - wrapper for the above
162 static void base64_decode_inplace(char *s)
164 ldb_base64_decode(s);
169 /* Authenticate a user with a plaintext password */
171 static bool check_plaintext_auth(const char *user, const char *pass,
172 bool stdout_diagnostics)
174 return (strcmp(pass, opt_password) == 0);
177 /* authenticate a user with an encrypted username/password */
179 static NTSTATUS local_pw_check_specified(struct loadparm_context *lp_ctx,
180 const char *username,
182 const char *workstation,
183 const DATA_BLOB *challenge,
184 const DATA_BLOB *lm_response,
185 const DATA_BLOB *nt_response,
187 DATA_BLOB *lm_session_key,
188 DATA_BLOB *user_session_key,
193 struct samr_Password lm_pw, nt_pw;
194 struct samr_Password *lm_pwd, *nt_pwd;
195 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
197 nt_status = NT_STATUS_NO_MEMORY;
200 E_md4hash(opt_password, nt_pw.hash);
201 if (E_deshash(opt_password, lm_pw.hash)) {
209 nt_status = ntlm_password_check(mem_ctx,
211 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
212 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
219 lm_pwd, nt_pwd, user_session_key, lm_session_key);
221 if (NT_STATUS_IS_OK(nt_status)) {
225 *lp_winbind_separator(global_loadparm),
229 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
230 domain, username, workstation,
231 nt_errstr(nt_status)));
233 talloc_free(mem_ctx);
236 *error_string = strdup(nt_errstr(nt_status));
243 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
244 char *buf, int length, void **private,
245 unsigned int mux_id, void **private2)
250 pass = memchr(buf, ' ', length);
252 DEBUG(2, ("Password not found. Denying access\n"));
253 mux_printf(mux_id, "ERR\n");
259 if (stdio_helper_mode == SQUID_2_5_BASIC) {
260 rfc1738_unescape(user);
261 rfc1738_unescape(pass);
264 if (check_plaintext_auth(user, pass, false)) {
265 mux_printf(mux_id, "OK\n");
267 mux_printf(mux_id, "ERR\n");
271 /* This is a bit hairy, but the basic idea is to do a password callback
272 to the calling application. The callback comes from within gensec */
274 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
275 char *buf, int length, void **private,
276 unsigned int mux_id, void **password)
279 if (strlen(buf) < 2) {
280 DEBUG(1, ("query [%s] invalid", buf));
281 mux_printf(mux_id, "BH\n");
285 if (strlen(buf) > 3) {
286 in = base64_decode_data_blob(NULL, buf + 3);
288 in = data_blob(NULL, 0);
291 if (strncmp(buf, "PW ", 3) == 0) {
293 *password = talloc_strndup(*private /* hopefully the right gensec context, useful to use for talloc */,
294 (const char *)in.data, in.length);
296 if (*password == NULL) {
297 DEBUG(1, ("Out of memory\n"));
298 mux_printf(mux_id, "BH\n");
303 mux_printf(mux_id, "OK\n");
307 DEBUG(1, ("Asked for (and expected) a password\n"));
308 mux_printf(mux_id, "BH\n");
313 * Callback for password credentails. This is not async, and when
314 * GENSEC and the credentails code is made async, it will look rather
318 static const char *get_password(struct cli_credentials *credentials)
320 char *password = NULL;
322 /* Ask for a password */
323 mux_printf((unsigned int)credentials->priv_data, "PW\n");
324 credentials->priv_data = NULL;
326 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
331 Check if a string is part of a list.
333 static bool in_list(const char *s, const char *list, bool casesensitive)
341 while (next_token(&p, tok, LIST_SEP, sizeof(tok))) {
342 if ((casesensitive?strcmp:strcasecmp_m)(tok,s) == 0)
348 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
350 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, true)) {
351 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
352 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
354 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, true)) {
355 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
356 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
358 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, true)) {
359 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
360 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
364 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
365 char *buf, int length, void **private,
366 unsigned int mux_id, void **private2)
369 DATA_BLOB out = data_blob(NULL, 0);
370 char *out_base64 = NULL;
371 const char *reply_arg = NULL;
372 struct gensec_ntlm_state {
373 struct gensec_security *gensec_state;
374 const char *set_password;
376 struct gensec_ntlm_state *state;
377 struct event_context *ev;
378 struct messaging_context *msg;
382 const char *reply_code;
383 struct cli_credentials *creds;
385 static char *want_feature_list = NULL;
386 static DATA_BLOB session_key;
391 state = (struct gensec_ntlm_state *)*private;
393 state = talloc_zero(NULL, struct gensec_ntlm_state);
395 mux_printf(mux_id, "BH No Memory\n");
400 state->set_password = opt_password;
404 if (strlen(buf) < 2) {
405 DEBUG(1, ("query [%s] invalid", buf));
406 mux_printf(mux_id, "BH\n");
410 if (strlen(buf) > 3) {
411 if(strncmp(buf, "SF ", 3) == 0) {
412 DEBUG(10, ("Setting flags to negotiate\n"));
413 talloc_free(want_feature_list);
414 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
415 mux_printf(mux_id, "OK\n");
418 in = base64_decode_data_blob(NULL, buf + 3);
420 in = data_blob(NULL, 0);
423 if (strncmp(buf, "YR", 2) == 0) {
424 if (state->gensec_state) {
425 talloc_free(state->gensec_state);
426 state->gensec_state = NULL;
428 } else if ( (strncmp(buf, "OK", 2) == 0)) {
429 /* Just return BH, like ntlm_auth from Samba 3 does. */
430 mux_printf(mux_id, "BH\n");
433 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
434 (strncmp(buf, "KK ", 3) != 0) &&
435 (strncmp(buf, "AF ", 3) != 0) &&
436 (strncmp(buf, "NA ", 3) != 0) &&
437 (strncmp(buf, "UG", 2) != 0) &&
438 (strncmp(buf, "PW ", 3) != 0) &&
439 (strncmp(buf, "GK", 2) != 0) &&
440 (strncmp(buf, "GF", 2) != 0)) {
441 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
442 mux_printf(mux_id, "BH\n");
448 if (!(state->gensec_state)) {
449 switch (stdio_helper_mode) {
450 case GSS_SPNEGO_CLIENT:
451 case NTLMSSP_CLIENT_1:
452 /* setup the client side */
454 nt_status = gensec_client_start(NULL, &state->gensec_state, NULL);
455 if (!NT_STATUS_IS_OK(nt_status)) {
460 case GSS_SPNEGO_SERVER:
461 case SQUID_2_5_NTLMSSP:
462 ev = event_context_init(state);
466 msg = messaging_client_init(state, lp_messaging_path(state, global_loadparm), ev);
470 if (!NT_STATUS_IS_OK(gensec_server_start(state, ev, msg, &state->gensec_state))) {
478 creds = cli_credentials_init(state->gensec_state);
479 cli_credentials_set_conf(creds, global_loadparm);
481 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
484 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
486 if (state->set_password) {
487 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
489 cli_credentials_set_password_callback(creds, get_password);
490 creds->priv_data = (void*)mux_id;
492 if (opt_workstation) {
493 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
496 switch (stdio_helper_mode) {
497 case GSS_SPNEGO_SERVER:
498 case SQUID_2_5_NTLMSSP:
499 cli_credentials_set_machine_account(creds);
505 gensec_set_credentials(state->gensec_state, creds);
506 gensec_want_feature_list(state->gensec_state, want_feature_list);
508 switch (stdio_helper_mode) {
509 case GSS_SPNEGO_CLIENT:
510 case GSS_SPNEGO_SERVER:
511 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
516 case NTLMSSP_CLIENT_1:
521 case SQUID_2_5_NTLMSSP:
522 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
528 if (!NT_STATUS_IS_OK(nt_status)) {
529 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
530 mux_printf(mux_id, "BH\n");
537 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
539 if (strncmp(buf, "PW ", 3) == 0) {
540 state->set_password = talloc_strndup(state,
541 (const char *)in.data,
544 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
547 mux_printf(mux_id, "OK\n");
549 talloc_free(mem_ctx);
553 if (strncmp(buf, "UG", 2) == 0) {
555 char *grouplist = NULL;
556 struct auth_session_info *session_info;
558 nt_status = gensec_session_info(state->gensec_state, &session_info);
559 if (!NT_STATUS_IS_OK(nt_status)) {
560 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
561 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
563 talloc_free(mem_ctx);
567 /* get the string onto the context */
568 grouplist = talloc_strdup(mem_ctx, "");
570 for (i=0; i<session_info->security_token->num_sids; i++) {
571 struct security_token *token = session_info->security_token;
572 const char *sidstr = dom_sid_string(session_info,
574 grouplist = talloc_asprintf_append_buffer(grouplist, "%s,", sidstr);
577 mux_printf(mux_id, "GL %s\n", grouplist);
578 talloc_free(session_info);
580 talloc_free(mem_ctx);
584 if (strncmp(buf, "GK", 2) == 0) {
586 DEBUG(10, ("Requested session key\n"));
587 nt_status = gensec_session_key(state->gensec_state, &session_key);
588 if(!NT_STATUS_IS_OK(nt_status)) {
589 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
590 mux_printf(mux_id, "BH No session key\n");
591 talloc_free(mem_ctx);
594 base64_key = base64_encode_data_blob(state, session_key);
595 mux_printf(mux_id, "GK %s\n", base64_key);
596 talloc_free(base64_key);
598 talloc_free(mem_ctx);
602 if (strncmp(buf, "GF", 2) == 0) {
603 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
606 gensec_ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
607 struct gensec_ntlmssp_state);
608 neg_flags = gensec_ntlmssp_state->neg_flags;
610 DEBUG(10, ("Requested negotiated feature flags\n"));
611 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
615 nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
617 /* don't leak 'bad password'/'no such user' info to the network client */
618 nt_status = auth_nt_status_squash(nt_status);
621 out_base64 = base64_encode_data_blob(mem_ctx, out);
626 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
630 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
632 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
639 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
641 reply_arg = nt_errstr(nt_status);
642 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
643 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
645 reply_arg = nt_errstr(nt_status);
646 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
647 } else if (!NT_STATUS_IS_OK(nt_status)) {
649 reply_arg = nt_errstr(nt_status);
650 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
651 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
652 struct auth_session_info *session_info;
654 nt_status = gensec_session_info(state->gensec_state, &session_info);
655 if (!NT_STATUS_IS_OK(nt_status)) {
657 reply_arg = nt_errstr(nt_status);
658 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
662 reply_arg = talloc_asprintf(state->gensec_state,
663 "%s%s%s", session_info->server_info->domain_name,
664 lp_winbind_separator(global_loadparm), session_info->server_info->account_name);
665 talloc_free(session_info);
667 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
669 reply_arg = out_base64;
674 switch (stdio_helper_mode) {
675 case GSS_SPNEGO_SERVER:
676 mux_printf(mux_id, "%s %s %s\n", reply_code,
677 out_base64 ? out_base64 : "*",
678 reply_arg ? reply_arg : "*");
682 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
683 } else if (reply_arg) {
684 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
686 mux_printf(mux_id, "%s\n", reply_code);
690 talloc_free(mem_ctx);
694 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
695 char *buf, int length, void **private,
696 unsigned int mux_id, void **private2)
698 char *request, *parameter;
699 static DATA_BLOB challenge;
700 static DATA_BLOB lm_response;
701 static DATA_BLOB nt_response;
702 static char *full_username;
703 static char *username;
705 static char *plaintext_password;
706 static bool ntlm_server_1_user_session_key;
707 static bool ntlm_server_1_lm_session_key;
709 if (strequal(buf, ".")) {
710 if (!full_username && !username) {
711 mux_printf(mux_id, "Error: No username supplied!\n");
712 } else if (plaintext_password) {
713 /* handle this request as plaintext */
714 if (!full_username) {
715 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(global_loadparm), username) == -1) {
716 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
720 if (check_plaintext_auth(full_username, plaintext_password, false)) {
721 mux_printf(mux_id, "Authenticated: Yes\n");
723 mux_printf(mux_id, "Authenticated: No\n");
725 } else if (!lm_response.data && !nt_response.data) {
726 mux_printf(mux_id, "Error: No password supplied!\n");
727 } else if (!challenge.data) {
728 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
730 char *error_string = NULL;
732 DATA_BLOB user_session_key;
735 if (full_username && !username) {
739 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
740 /* username might be 'tainted', don't print into our new-line deleimianted stream */
741 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
745 username = smb_xstrdup(fstr_user);
746 domain = smb_xstrdup(fstr_domain);
750 domain = smb_xstrdup(lp_workgroup(global_loadparm));
753 if (ntlm_server_1_lm_session_key)
754 flags |= NTLM_AUTH_FLAG_LMKEY;
756 if (ntlm_server_1_user_session_key)
757 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
759 if (!NT_STATUS_IS_OK(
760 local_pw_check_specified(global_loadparm,
763 lp_netbios_name(global_loadparm),
773 mux_printf(mux_id, "Authenticated: No\n");
774 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
775 SAFE_FREE(error_string);
777 static char zeros[16];
779 char *hex_user_session_key;
781 mux_printf(mux_id, "Authenticated: Yes\n");
783 if (ntlm_server_1_lm_session_key
785 && (memcmp(zeros, lm_key.data,
786 lm_key.length) != 0)) {
787 hex_encode(lm_key.data,
790 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
791 SAFE_FREE(hex_lm_key);
794 if (ntlm_server_1_user_session_key
795 && user_session_key.length
796 && (memcmp(zeros, user_session_key.data,
797 user_session_key.length) != 0)) {
798 hex_encode(user_session_key.data,
799 user_session_key.length,
800 &hex_user_session_key);
801 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
802 SAFE_FREE(hex_user_session_key);
806 /* clear out the state */
807 challenge = data_blob(NULL, 0);
808 nt_response = data_blob(NULL, 0);
809 lm_response = data_blob(NULL, 0);
810 SAFE_FREE(full_username);
813 SAFE_FREE(plaintext_password);
814 ntlm_server_1_user_session_key = false;
815 ntlm_server_1_lm_session_key = false;
816 mux_printf(mux_id, ".\n");
823 /* Indicates a base64 encoded structure */
824 parameter = strstr(request, ":: ");
826 parameter = strstr(request, ": ");
829 DEBUG(0, ("Parameter not found!\n"));
830 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
847 base64_decode_inplace(parameter);
850 if (strequal(request, "LANMAN-Challenge")) {
851 challenge = strhex_to_data_blob(parameter);
852 if (challenge.length != 8) {
853 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
855 (int)challenge.length);
856 challenge = data_blob(NULL, 0);
858 } else if (strequal(request, "NT-Response")) {
859 nt_response = strhex_to_data_blob(parameter);
860 if (nt_response.length < 24) {
861 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
863 (int)nt_response.length);
864 nt_response = data_blob(NULL, 0);
866 } else if (strequal(request, "LANMAN-Response")) {
867 lm_response = strhex_to_data_blob(parameter);
868 if (lm_response.length != 24) {
869 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
871 (int)lm_response.length);
872 lm_response = data_blob(NULL, 0);
874 } else if (strequal(request, "Password")) {
875 plaintext_password = smb_xstrdup(parameter);
876 } else if (strequal(request, "NT-Domain")) {
877 domain = smb_xstrdup(parameter);
878 } else if (strequal(request, "Username")) {
879 username = smb_xstrdup(parameter);
880 } else if (strequal(request, "Full-Username")) {
881 full_username = smb_xstrdup(parameter);
882 } else if (strequal(request, "Request-User-Session-Key")) {
883 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
884 } else if (strequal(request, "Request-LanMan-Session-Key")) {
885 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
887 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
891 static void manage_squid_request(enum stdio_helper_mode helper_mode,
892 stdio_helper_function fn, void **private2)
895 char tmp[INITIAL_BUFFER_SIZE+1];
896 unsigned int mux_id = 0;
897 int length, buf_size = 0;
900 unsigned int max_mux;
901 void **private_pointers;
904 static struct mux_private *mux_private;
905 static void *normal_private;
908 buf = talloc_strdup(NULL, "");
911 DEBUG(0, ("Failed to allocate memory for reading the input "
913 x_fprintf(x_stdout, "ERR\n");
918 /* this is not a typo - x_fgets doesn't work too well under
920 if (fgets(tmp, INITIAL_BUFFER_SIZE, stdin) == NULL) {
922 DEBUG(1, ("fgets() failed! dying..... errno=%d "
923 "(%s)\n", ferror(stdin),
924 strerror(ferror(stdin))));
926 exit(1); /* BIIG buffer */
931 buf = talloc_strdup_append_buffer(buf, tmp);
932 buf_size += INITIAL_BUFFER_SIZE;
934 if (buf_size > MAX_BUFFER_SIZE) {
935 DEBUG(0, ("Invalid Request (too large)\n"));
936 x_fprintf(x_stdout, "ERR\n");
941 c = strchr(buf, '\n');
947 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
949 if (buf[0] == '\0') {
950 DEBUG(0, ("Invalid Request (empty)\n"));
951 x_fprintf(x_stdout, "ERR\n");
957 if (sscanf(buf, "%u ", &mux_id) != 1) {
958 DEBUG(0, ("Invalid Request - no multiplex id\n"));
959 x_fprintf(x_stdout, "ERR\n");
964 mux_private = talloc(NULL, struct mux_private);
965 mux_private->max_mux = 0;
966 mux_private->private_pointers = NULL;
971 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
972 x_fprintf(x_stdout, "ERR\n");
977 if (mux_id >= mux_private->max_mux) {
978 unsigned int prev_max = mux_private->max_mux;
979 mux_private->max_mux = mux_id + 1;
980 mux_private->private_pointers
981 = talloc_realloc(mux_private,
982 mux_private->private_pointers,
983 void *, mux_private->max_mux);
984 memset(&mux_private->private_pointers[prev_max], '\0',
985 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
988 private = &mux_private->private_pointers[mux_id];
991 private = &normal_private;
994 fn(helper_mode, c, length, private, mux_id, private2);
998 static void squid_stream(enum stdio_helper_mode stdio_mode,
999 stdio_helper_function fn) {
1000 /* initialize FDescs */
1001 x_setbuf(x_stdout, NULL);
1002 x_setbuf(x_stderr, NULL);
1004 manage_squid_request(stdio_mode, fn, NULL);
1012 OPT_USERNAME = 1000,
1021 OPT_USER_SESSION_KEY,
1023 OPT_REQUIRE_MEMBERSHIP,
1025 OPT_USE_CACHED_CREDS,
1028 int main(int argc, const char **argv)
1030 static const char *helper_protocol;
1035 /* NOTE: DO NOT change this interface without considering the implications!
1036 This is an external interface, which other programs will use to interact
1040 /* We do not use single-letter command abbreviations, because they harm future
1041 interface stability. */
1043 struct poptOption long_options[] = {
1045 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1046 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1047 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1048 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1049 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1050 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1051 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1057 /* Samba client initialisation */
1059 setup_logging(NULL, DEBUG_STDERR);
1063 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1065 /* Parse command line options */
1068 poptPrintHelp(pc, stderr, 0);
1072 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1073 POPT_CONTEXT_KEEP_FIRST);
1075 while((opt = poptGetNextOpt(pc)) != -1) {
1081 fprintf(stderr, "%s: %s\n",
1082 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1087 gensec_init(global_loadparm);
1089 if (opt_domain == NULL) {
1090 opt_domain = lp_workgroup(global_loadparm);
1093 if (helper_protocol) {
1095 for (i=0; i<NUM_HELPER_MODES; i++) {
1096 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1097 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1101 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1103 for (i=0; i<NUM_HELPER_MODES; i++) {
1104 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1110 if (!opt_username) {
1111 x_fprintf(x_stderr, "username must be specified!\n\n");
1112 poptPrintHelp(pc, stderr, 0);
1116 if (opt_workstation == NULL) {
1117 opt_workstation = lp_netbios_name(global_loadparm);
1120 if (!opt_password) {
1121 opt_password = getpass("password: ");
1127 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(global_loadparm), opt_username);
1128 if (!check_plaintext_auth(user, opt_password, true)) {
1135 poptFreeContext(pc);