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 "dynconfig.h"
27 #include "system/passwd.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "auth/auth.h"
33 #define DBGC_CLASS DBGC_WINBIND
35 #define SQUID_BUFFER_SIZE 2010
37 enum stdio_helper_mode {
48 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
49 #define NTLM_AUTH_FLAG_LMKEY 0x0008
52 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
53 char *buf, int length, void **private,
56 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
57 char *buf, int length, void **private,
60 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
61 char *buf, int length, void **private,
64 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
65 char *buf, int length, void **private,
68 static void manage_squid_request(enum stdio_helper_mode helper_mode,
69 stdio_helper_function fn);
72 enum stdio_helper_mode mode;
74 stdio_helper_function fn;
75 } stdio_helper_protocols[] = {
76 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
77 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
78 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
79 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
80 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
81 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
82 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
83 { NUM_HELPER_MODES, NULL, NULL}
86 extern int winbindd_fd;
88 const char *opt_username;
89 const char *opt_domain;
90 const char *opt_workstation;
91 const char *opt_password;
95 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
97 static void mux_printf(unsigned int mux_id, const char *format, ...)
102 x_fprintf(x_stdout, "%d ", mux_id);
105 va_start(ap, format);
106 x_vfprintf(x_stdout, format, ap);
112 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
113 form DOMAIN/user into a domain and a user */
115 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
119 char *p = strchr(domuser,*lp_winbind_separator());
126 fstrcpy(domain, domuser);
127 domain[PTR_DIFF(p, domuser)] = 0;
132 /* Authenticate a user with a plaintext password */
134 static BOOL check_plaintext_auth(const char *user, const char *pass,
135 BOOL stdout_diagnostics)
137 return (strcmp(pass, opt_password) == 0);
140 /* authenticate a user with an encrypted username/password */
142 static NTSTATUS local_pw_check_specified(const char *username,
144 const char *workstation,
145 const DATA_BLOB *challenge,
146 const DATA_BLOB *lm_response,
147 const DATA_BLOB *nt_response,
149 DATA_BLOB *lm_session_key,
150 DATA_BLOB *user_session_key,
155 uint8_t lm_pw[16], nt_pw[16];
156 uint8_t *lm_pwd, *nt_pwd;
157 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
159 nt_status = NT_STATUS_NO_MEMORY;
162 E_md4hash(opt_password, nt_pw);
163 if (E_deshash(opt_password, lm_pw)) {
171 nt_status = ntlm_password_check(mem_ctx,
179 lm_pwd, nt_pwd, user_session_key, lm_session_key);
181 if (NT_STATUS_IS_OK(nt_status)) {
185 *lp_winbind_separator(),
189 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
190 domain, username, workstation,
191 nt_errstr(nt_status)));
193 talloc_destroy(mem_ctx);
196 *error_string = strdup(nt_errstr(nt_status));
203 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
204 char *buf, int length, void **private,
210 pass=memchr(buf,' ',length);
212 DEBUG(2, ("Password not found. Denying access\n"));
213 mux_printf(mux_id, "ERR\n");
219 if (stdio_helper_mode == SQUID_2_5_BASIC) {
220 rfc1738_unescape(user);
221 rfc1738_unescape(pass);
224 if (check_plaintext_auth(user, pass, False)) {
225 mux_printf(mux_id, "OK\n");
227 mux_printf(mux_id, "ERR\n");
231 /* This is a bit hairy, but the basic idea is to do a password callback
232 to the calling application. The callback comes from within gensec */
234 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
235 char *buf, int length, void **private,
239 struct gensec_security **gensec_state = (struct gensec_security **)private;
240 if (strlen(buf) < 2) {
241 DEBUG(1, ("query [%s] invalid", buf));
242 mux_printf(mux_id, "BH\n");
246 if (strlen(buf) > 3) {
247 in = base64_decode_data_blob(buf + 3);
249 in = data_blob(NULL, 0);
252 if (strncmp(buf, "PW ", 3) == 0) {
254 (*gensec_state)->password_callback_private = talloc_strndup((*gensec_state),
255 (const char *)in.data, in.length);
257 if ((*gensec_state)->password_callback_private == NULL) {
258 DEBUG(1, ("Out of memory\n"));
259 mux_printf(mux_id, "BH\n");
264 mux_printf(mux_id, "OK\n");
268 DEBUG(1, ("Asked for (and expected) a password\n"));
269 mux_printf(mux_id, "BH\n");
274 * Callback for gensec, to ask the calling application for a password. Uses the above function
275 * for the stdio part of this.
278 static NTSTATUS get_password(struct gensec_security *gensec_security,
284 /* Ask for a password */
285 mux_printf((unsigned int)gensec_security->password_callback_private, "PW\n");
286 gensec_security->password_callback_private = NULL;
288 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request);
289 *password = (char *)gensec_security->password_callback_private;
293 return NT_STATUS_INVALID_PARAMETER;
297 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
298 char *buf, int length, void **private,
302 DATA_BLOB out = data_blob(NULL, 0);
303 char *out_base64 = NULL;
304 const char *reply_arg = NULL;
305 struct gensec_security **gensec_state = (struct gensec_security **)private;
308 const char *reply_code;
310 if (strlen(buf) < 2) {
311 DEBUG(1, ("query [%s] invalid", buf));
312 mux_printf(mux_id, "BH\n");
316 if (strlen(buf) > 3) {
317 in = base64_decode_data_blob(buf + 3);
319 in = data_blob(NULL, 0);
322 if (strncmp(buf, "YR", 2) == 0) {
323 if (gensec_state && *gensec_state) {
324 gensec_end(gensec_state);
325 *gensec_state = NULL;
327 } else if ( (strncmp(buf, "OK", 2) == 0)) {
331 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
332 (strncmp(buf, "KK ", 3) != 0) &&
333 (strncmp(buf, "AF ", 3) != 0) &&
334 (strncmp(buf, "NA ", 3) != 0) &&
335 (strncmp(buf, "PW ", 3) != 0)) {
336 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
337 mux_printf(mux_id, "BH\n");
343 if (!(gensec_state && *gensec_state)) {
344 switch (stdio_helper_mode) {
345 case GSS_SPNEGO_CLIENT:
346 case NTLMSSP_CLIENT_1:
347 /* setup the client side */
349 if (!NT_STATUS_IS_OK(gensec_client_start(NULL, gensec_state))) {
352 gensec_set_username(*gensec_state, opt_username);
353 gensec_set_domain(*gensec_state, opt_domain);
355 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state, opt_password))) {
356 DEBUG(1, ("Out of memory\n"));
357 mux_printf(mux_id, "BH\n");
362 gensec_set_password_callback(*gensec_state, get_password, (void*)mux_id);
366 case GSS_SPNEGO_SERVER:
367 case SQUID_2_5_NTLMSSP:
368 if (!NT_STATUS_IS_OK(gensec_server_start(NULL, gensec_state))) {
376 switch (stdio_helper_mode) {
377 case GSS_SPNEGO_CLIENT:
378 case GSS_SPNEGO_SERVER:
379 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_SPNEGO);
384 case NTLMSSP_CLIENT_1:
388 case SQUID_2_5_NTLMSSP:
389 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_NTLMSSP);
395 if (!NT_STATUS_IS_OK(nt_status)) {
396 DEBUG(1, ("SPENGO login failed to initialise: %s\n", nt_errstr(nt_status)));
397 mux_printf(mux_id, "BH\n");
402 if (strncmp(buf, "PW ", 3) == 0) {
404 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state,
405 talloc_strndup((*gensec_state),
406 (const char *)in.data,
408 DEBUG(1, ("Out of memory\n"));
409 mux_printf(mux_id, "BH\n");
414 mux_printf(mux_id, "OK\n");
421 nt_status = gensec_update(*gensec_state, NULL, in, &out);
423 /* don't leak 'bad password'/'no such user' info to the network client */
424 nt_status = nt_status_squash(nt_status);
427 out_base64 = base64_encode_data_blob(out);
431 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
435 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
437 } else if ((*gensec_state)->gensec_role == GENSEC_SERVER) {
444 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
446 reply_arg = nt_errstr(nt_status);
447 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
448 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
450 reply_arg = nt_errstr(nt_status);
451 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
452 } else if (!NT_STATUS_IS_OK(nt_status)) {
454 reply_arg = nt_errstr(nt_status);
455 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
456 } else if /* OK */ ((*gensec_state)->gensec_role == GENSEC_SERVER) {
457 struct auth_session_info *session_info;
459 nt_status = gensec_session_info(*gensec_state, &session_info);
460 if (!NT_STATUS_IS_OK(nt_status)) {
462 reply_arg = nt_errstr(nt_status);
463 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
467 reply_arg = talloc_asprintf(*gensec_state,
468 "%s%s%s", session_info->server_info->domain,
469 lp_winbind_separator(), session_info->server_info->account_name);
470 talloc_free(session_info);
472 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
479 switch (stdio_helper_mode) {
480 case GSS_SPNEGO_SERVER:
481 mux_printf(mux_id, "%s %s %s\n", reply_code,
482 out_base64 ? out_base64 : "*",
483 reply_arg ? reply_arg : "*");
487 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
488 } else if (reply_arg) {
489 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
491 mux_printf(mux_id, "%s\n", reply_code);
495 SAFE_FREE(out_base64);
499 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
500 char *buf, int length, void **private,
503 char *request, *parameter;
504 static DATA_BLOB challenge;
505 static DATA_BLOB lm_response;
506 static DATA_BLOB nt_response;
507 static char *full_username;
508 static char *username;
510 static char *plaintext_password;
511 static BOOL ntlm_server_1_user_session_key;
512 static BOOL ntlm_server_1_lm_session_key;
514 if (strequal(buf, ".")) {
515 if (!full_username && !username) {
516 mux_printf(mux_id, "Error: No username supplied!\n");
517 } else if (plaintext_password) {
518 /* handle this request as plaintext */
519 if (!full_username) {
520 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
521 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
525 if (check_plaintext_auth(full_username, plaintext_password, False)) {
526 mux_printf(mux_id, "Authenticated: Yes\n");
528 mux_printf(mux_id, "Authenticated: No\n");
530 } else if (!lm_response.data && !nt_response.data) {
531 mux_printf(mux_id, "Error: No password supplied!\n");
532 } else if (!challenge.data) {
533 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
535 char *error_string = NULL;
537 DATA_BLOB user_session_key;
540 if (full_username && !username) {
544 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
545 /* username might be 'tainted', don't print into our new-line deleimianted stream */
546 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
550 username = smb_xstrdup(fstr_user);
551 domain = smb_xstrdup(fstr_domain);
555 domain = smb_xstrdup(lp_workgroup());
558 if (ntlm_server_1_lm_session_key)
559 flags |= NTLM_AUTH_FLAG_LMKEY;
561 if (ntlm_server_1_user_session_key)
562 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
564 if (!NT_STATUS_IS_OK(
565 local_pw_check_specified(username,
577 mux_printf(mux_id, "Authenticated: No\n");
578 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
579 SAFE_FREE(error_string);
581 static char zeros[16];
583 char *hex_user_session_key;
585 mux_printf(mux_id, "Authenticated: Yes\n");
587 if (ntlm_server_1_lm_session_key
589 && (memcmp(zeros, lm_key.data,
590 lm_key.length) != 0)) {
591 hex_encode(lm_key.data,
594 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
595 SAFE_FREE(hex_lm_key);
598 if (ntlm_server_1_user_session_key
599 && user_session_key.length
600 && (memcmp(zeros, user_session_key.data,
601 user_session_key.length) != 0)) {
602 hex_encode(user_session_key.data,
603 user_session_key.length,
604 &hex_user_session_key);
605 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
606 SAFE_FREE(hex_user_session_key);
610 /* clear out the state */
611 challenge = data_blob(NULL, 0);
612 nt_response = data_blob(NULL, 0);
613 lm_response = data_blob(NULL, 0);
614 SAFE_FREE(full_username);
617 SAFE_FREE(plaintext_password);
618 ntlm_server_1_user_session_key = False;
619 ntlm_server_1_lm_session_key = False;
620 mux_printf(mux_id, ".\n");
627 /* Indicates a base64 encoded structure */
628 parameter = strstr(request, ":: ");
630 parameter = strstr(request, ": ");
633 DEBUG(0, ("Parameter not found!\n"));
634 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
651 base64_decode_inplace(parameter);
654 if (strequal(request, "LANMAN-Challenge")) {
655 challenge = strhex_to_data_blob(parameter);
656 if (challenge.length != 8) {
657 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
659 (int)challenge.length);
660 challenge = data_blob(NULL, 0);
662 } else if (strequal(request, "NT-Response")) {
663 nt_response = strhex_to_data_blob(parameter);
664 if (nt_response.length < 24) {
665 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
667 (int)nt_response.length);
668 nt_response = data_blob(NULL, 0);
670 } else if (strequal(request, "LANMAN-Response")) {
671 lm_response = strhex_to_data_blob(parameter);
672 if (lm_response.length != 24) {
673 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
675 (int)lm_response.length);
676 lm_response = data_blob(NULL, 0);
678 } else if (strequal(request, "Password")) {
679 plaintext_password = smb_xstrdup(parameter);
680 } else if (strequal(request, "NT-Domain")) {
681 domain = smb_xstrdup(parameter);
682 } else if (strequal(request, "Username")) {
683 username = smb_xstrdup(parameter);
684 } else if (strequal(request, "Full-Username")) {
685 full_username = smb_xstrdup(parameter);
686 } else if (strequal(request, "Request-User-Session-Key")) {
687 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
688 } else if (strequal(request, "Request-LanMan-Session-Key")) {
689 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
691 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
695 static void manage_squid_request(enum stdio_helper_mode helper_mode,
696 stdio_helper_function fn)
698 char buf[SQUID_BUFFER_SIZE+1];
704 unsigned int max_mux;
705 void **private_pointers;
708 static struct mux_private *mux_private;
709 static void *normal_private;
712 /* this is not a typo - x_fgets doesn't work too well under squid */
713 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
715 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
716 strerror(ferror(stdin))));
718 exit(1); /* BIIG buffer */
723 c=memchr(buf,'\n',sizeof(buf)-1);
732 DEBUG(0, ("Oversized message\n"));
733 x_fprintf(x_stdout, "ERR\n");
738 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
740 if (buf[0] == '\0') {
741 DEBUG(0, ("Invalid Request (empty)\n"));
742 x_fprintf(x_stdout, "ERR\n");
747 if (sscanf(buf, "%u ", &mux_id) != 1) {
748 DEBUG(0, ("Invalid Request - no multiplex id\n"));
749 x_fprintf(x_stdout, "ERR\n");
753 mux_private = talloc_p(NULL, struct mux_private);
754 mux_private->max_mux = 0;
755 mux_private->private_pointers = NULL;
758 c=memchr(buf,' ',sizeof(buf)-1);
760 if (mux_id >= mux_private->max_mux) {
761 unsigned int prev_max = mux_private->max_mux;
762 mux_private->max_mux = mux_id + 1;
763 mux_private->private_pointers
764 = talloc_realloc_p(mux_private,
765 mux_private->private_pointers,
766 void *, mux_private->max_mux);
767 memset(&mux_private->private_pointers[prev_max], '\0',
768 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
771 private = &mux_private->private_pointers[mux_id];
774 private = &normal_private;
777 fn(helper_mode, c, length, private, mux_id);
780 static void squid_stream(enum stdio_helper_mode stdio_mode,
781 stdio_helper_function fn) {
782 /* initialize FDescs */
783 x_setbuf(x_stdout, NULL);
784 x_setbuf(x_stderr, NULL);
786 manage_squid_request(stdio_mode, fn);
803 OPT_USER_SESSION_KEY,
805 OPT_REQUIRE_MEMBERSHIP,
809 int main(int argc, const char **argv)
811 static const char *helper_protocol;
816 /* NOTE: DO NOT change this interface without considering the implications!
817 This is an external interface, which other programs will use to interact
821 /* We do not use single-letter command abbreviations, because they harm future
822 interface stability. */
824 struct poptOption long_options[] = {
826 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
827 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
828 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
829 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
830 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
831 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
836 /* Samba client initialisation */
838 setup_logging("ntlm_auth", DEBUG_STDERR);
840 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
841 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
842 dyn_CONFIGFILE, strerror(errno));
848 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
850 /* Parse command line options */
853 poptPrintHelp(pc, stderr, 0);
857 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
858 POPT_CONTEXT_KEEP_FIRST);
860 while((opt = poptGetNextOpt(pc)) != -1) {
866 fprintf(stderr, "%s: %s\n",
867 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
872 if (opt_domain == NULL) {
873 opt_domain = lp_workgroup();
876 if (helper_protocol) {
878 for (i=0; i<NUM_HELPER_MODES; i++) {
879 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
880 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
884 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
886 for (i=0; i<NUM_HELPER_MODES; i++) {
887 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
894 x_fprintf(x_stderr, "username must be specified!\n\n");
895 poptPrintHelp(pc, stderr, 0);
899 if (opt_workstation == NULL) {
900 opt_workstation = lp_netbios_name();
904 opt_password = getpass("password: ");
910 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
911 if (!check_plaintext_auth(user, opt_password, True)) {