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/passwd.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "auth/auth.h"
32 #define DBGC_CLASS DBGC_WINBIND
34 #define SQUID_BUFFER_SIZE 2010
36 enum stdio_helper_mode {
47 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
48 #define NTLM_AUTH_FLAG_LMKEY 0x0008
51 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
52 char *buf, int length, void **private);
54 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
55 char *buf, int length, void **private);
57 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
58 char *buf, int length, void **private);
60 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
61 char *buf, int length, void **private);
63 static void manage_squid_request(enum stdio_helper_mode helper_mode,
64 stdio_helper_function fn, void *private);
67 enum stdio_helper_mode mode;
69 stdio_helper_function fn;
70 } stdio_helper_protocols[] = {
71 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
72 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
73 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
74 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
75 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
76 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
77 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
78 { NUM_HELPER_MODES, NULL, NULL}
81 extern int winbindd_fd;
83 const char *opt_username;
84 const char *opt_domain;
85 const char *opt_workstation;
86 const char *opt_password;
89 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
90 form DOMAIN/user into a domain and a user */
92 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
96 char *p = strchr(domuser,*lp_winbind_separator());
103 fstrcpy(domain, domuser);
104 domain[PTR_DIFF(p, domuser)] = 0;
109 /* Authenticate a user with a plaintext password */
111 static BOOL check_plaintext_auth(const char *user, const char *pass,
112 BOOL stdout_diagnostics)
114 return (strcmp(pass, opt_password) == 0);
117 /* authenticate a user with an encrypted username/password */
119 static NTSTATUS local_pw_check_specified(const char *username,
121 const char *workstation,
122 const DATA_BLOB *challenge,
123 const DATA_BLOB *lm_response,
124 const DATA_BLOB *nt_response,
126 DATA_BLOB *lm_session_key,
127 DATA_BLOB *user_session_key,
132 uint8_t lm_pw[16], nt_pw[16];
133 uint8_t *lm_pwd, *nt_pwd;
134 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
136 nt_status = NT_STATUS_NO_MEMORY;
139 E_md4hash(opt_password, nt_pw);
140 if (E_deshash(opt_password, lm_pw)) {
148 nt_status = ntlm_password_check(mem_ctx,
156 lm_pwd, nt_pwd, user_session_key, lm_session_key);
158 if (NT_STATUS_IS_OK(nt_status)) {
162 *lp_winbind_separator(),
166 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
167 domain, username, workstation,
168 nt_errstr(nt_status)));
170 talloc_destroy(mem_ctx);
173 *error_string = strdup(nt_errstr(nt_status));
180 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
181 char *buf, int length, void **private)
186 pass=memchr(buf,' ',length);
188 DEBUG(2, ("Password not found. Denying access\n"));
189 x_fprintf(x_stdout, "ERR\n");
195 if (stdio_helper_mode == SQUID_2_5_BASIC) {
196 rfc1738_unescape(user);
197 rfc1738_unescape(pass);
200 if (check_plaintext_auth(user, pass, False)) {
201 x_fprintf(x_stdout, "OK\n");
203 x_fprintf(x_stdout, "ERR\n");
207 /* This is a bit hairy, but the basic idea is to do a password callback
208 to the calling application. The callback comes from within gensec */
210 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
211 char *buf, int length, void **private)
214 struct gensec_security **gensec_state = (struct gensec_security **)private;
215 if (strlen(buf) < 2) {
216 DEBUG(1, ("query [%s] invalid", buf));
217 x_fprintf(x_stdout, "BH\n");
221 if (strlen(buf) > 3) {
222 in = base64_decode_data_blob(buf + 3);
224 in = data_blob(NULL, 0);
227 if (strncmp(buf, "PW ", 3) == 0) {
229 (*gensec_state)->password_callback_private = talloc_strndup((*gensec_state),
230 (const char *)in.data, in.length);
232 if ((*gensec_state)->password_callback_private == NULL) {
233 DEBUG(1, ("Out of memory\n"));
234 x_fprintf(x_stdout, "BH\n");
239 x_fprintf(x_stdout, "OK\n");
243 DEBUG(1, ("Asked for (and expected) a password\n"));
244 x_fprintf(x_stdout, "BH\n");
249 * Callback for gensec, to ask the calling application for a password. Uses the above function
250 * for the stdio part of this.
253 static NTSTATUS get_password(struct gensec_security *gensec_security, TALLOC_CTX *mem_ctx,
258 /* Ask for a password */
259 x_fprintf(x_stdout, "PW\n");
260 gensec_security->password_callback_private = NULL;
262 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, &gensec_security);
263 *password = (char *)gensec_security->password_callback_private;
267 return NT_STATUS_INVALID_PARAMETER;
271 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
272 char *buf, int length, void **private)
275 DATA_BLOB out = data_blob(NULL, 0);
276 char *out_base64 = NULL;
277 const char *reply_arg = NULL;
278 struct gensec_security **gensec_state = (struct gensec_security **)private;
281 const char *reply_code;
283 if (strlen(buf) < 2) {
284 DEBUG(1, ("query [%s] invalid", buf));
285 x_fprintf(x_stdout, "BH\n");
289 if (strlen(buf) > 3) {
290 in = base64_decode_data_blob(buf + 3);
292 in = data_blob(NULL, 0);
295 if (strncmp(buf, "YR", 2) == 0) {
296 if (gensec_state && *gensec_state) {
297 gensec_end(gensec_state);
298 *gensec_state = NULL;
300 } else if ( (strncmp(buf, "OK", 2) == 0)) {
304 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
305 (strncmp(buf, "KK ", 3) != 0) &&
306 (strncmp(buf, "AF ", 3) != 0) &&
307 (strncmp(buf, "NA ", 3) != 0) &&
308 (strncmp(buf, "PW ", 3) != 0)) {
309 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
310 x_fprintf(x_stdout, "BH\n");
316 if (!(gensec_state && *gensec_state)) {
317 switch (stdio_helper_mode) {
318 case GSS_SPNEGO_CLIENT:
319 case NTLMSSP_CLIENT_1:
320 /* setup the client side */
322 if (!NT_STATUS_IS_OK(gensec_client_start(NULL, gensec_state))) {
325 gensec_set_username(*gensec_state, opt_username);
326 gensec_set_domain(*gensec_state, opt_domain);
328 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state, opt_password))) {
329 DEBUG(1, ("Out of memory\n"));
330 x_fprintf(x_stdout, "BH\n");
335 gensec_set_password_callback(*gensec_state, get_password, NULL);
339 case GSS_SPNEGO_SERVER:
340 case SQUID_2_5_NTLMSSP:
341 if (!NT_STATUS_IS_OK(gensec_server_start(NULL, gensec_state))) {
349 switch (stdio_helper_mode) {
350 case GSS_SPNEGO_CLIENT:
351 case GSS_SPNEGO_SERVER:
352 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_SPNEGO);
354 case NTLMSSP_CLIENT_1:
355 case SQUID_2_5_NTLMSSP:
356 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_NTLMSSP);
362 if (!NT_STATUS_IS_OK(nt_status)) {
363 DEBUG(1, ("SPENGO login failed to initialise: %s\n", nt_errstr(nt_status)));
364 x_fprintf(x_stdout, "BH\n");
372 if (strncmp(buf, "PW ", 3) == 0) {
374 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state,
375 talloc_strndup((*gensec_state),
376 (const char *)in.data,
378 DEBUG(1, ("Out of memory\n"));
379 x_fprintf(x_stdout, "BH\n");
384 x_fprintf(x_stdout, "OK\n");
391 nt_status = gensec_update(*gensec_state, NULL, in, &out);
393 /* don't leak 'bad password'/'no such user' info to the network client */
394 nt_status = nt_status_squash(nt_status);
397 out_base64 = base64_encode_data_blob(out);
401 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
405 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
407 } else if ((*gensec_state)->gensec_role == GENSEC_SERVER) {
414 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
416 reply_arg = nt_errstr(nt_status);
417 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
418 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
420 reply_arg = nt_errstr(nt_status);
421 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
422 } else if (!NT_STATUS_IS_OK(nt_status)) {
424 reply_arg = nt_errstr(nt_status);
425 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
426 } else if /* OK */ ((*gensec_state)->gensec_role == GENSEC_SERVER) {
427 struct auth_session_info *session_info;
429 nt_status = gensec_session_info(*gensec_state, &session_info);
430 if (!NT_STATUS_IS_OK(nt_status)) {
432 reply_arg = nt_errstr(nt_status);
433 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
437 reply_arg = talloc_asprintf(*gensec_state,
438 "%s%s%s", session_info->server_info->domain,
439 lp_winbind_separator(), session_info->server_info->account_name);
440 talloc_free(session_info);
442 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
449 switch (stdio_helper_mode) {
450 case GSS_SPNEGO_SERVER:
451 x_fprintf(x_stdout, "%s %s %s\n", reply_code,
452 out_base64 ? out_base64 : "*",
453 reply_arg ? reply_arg : "*");
457 x_fprintf(x_stdout, "%s %s\n", reply_code, out_base64);
458 } else if (reply_arg) {
459 x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
461 x_fprintf(x_stdout, "%s\n", reply_code);
465 SAFE_FREE(out_base64);
469 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
470 char *buf, int length, void **private)
472 char *request, *parameter;
473 static DATA_BLOB challenge;
474 static DATA_BLOB lm_response;
475 static DATA_BLOB nt_response;
476 static char *full_username;
477 static char *username;
479 static char *plaintext_password;
480 static BOOL ntlm_server_1_user_session_key;
481 static BOOL ntlm_server_1_lm_session_key;
483 if (strequal(buf, ".")) {
484 if (!full_username && !username) {
485 x_fprintf(x_stdout, "Error: No username supplied!\n");
486 } else if (plaintext_password) {
487 /* handle this request as plaintext */
488 if (!full_username) {
489 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
490 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
494 if (check_plaintext_auth(full_username, plaintext_password, False)) {
495 x_fprintf(x_stdout, "Authenticated: Yes\n");
497 x_fprintf(x_stdout, "Authenticated: No\n");
499 } else if (!lm_response.data && !nt_response.data) {
500 x_fprintf(x_stdout, "Error: No password supplied!\n");
501 } else if (!challenge.data) {
502 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
504 char *error_string = NULL;
506 DATA_BLOB user_session_key;
509 if (full_username && !username) {
513 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
514 /* username might be 'tainted', don't print into our new-line deleimianted stream */
515 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
519 username = smb_xstrdup(fstr_user);
520 domain = smb_xstrdup(fstr_domain);
524 domain = smb_xstrdup(lp_workgroup());
527 if (ntlm_server_1_lm_session_key)
528 flags |= NTLM_AUTH_FLAG_LMKEY;
530 if (ntlm_server_1_user_session_key)
531 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
533 if (!NT_STATUS_IS_OK(
534 local_pw_check_specified(username,
546 x_fprintf(x_stdout, "Authenticated: No\n");
547 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
548 SAFE_FREE(error_string);
550 static char zeros[16];
552 char *hex_user_session_key;
554 x_fprintf(x_stdout, "Authenticated: Yes\n");
556 if (ntlm_server_1_lm_session_key
558 && (memcmp(zeros, lm_key.data,
559 lm_key.length) != 0)) {
560 hex_encode(lm_key.data,
563 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
564 SAFE_FREE(hex_lm_key);
567 if (ntlm_server_1_user_session_key
568 && user_session_key.length
569 && (memcmp(zeros, user_session_key.data,
570 user_session_key.length) != 0)) {
571 hex_encode(user_session_key.data,
572 user_session_key.length,
573 &hex_user_session_key);
574 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
575 SAFE_FREE(hex_user_session_key);
579 /* clear out the state */
580 challenge = data_blob(NULL, 0);
581 nt_response = data_blob(NULL, 0);
582 lm_response = data_blob(NULL, 0);
583 SAFE_FREE(full_username);
586 SAFE_FREE(plaintext_password);
587 ntlm_server_1_user_session_key = False;
588 ntlm_server_1_lm_session_key = False;
589 x_fprintf(x_stdout, ".\n");
596 /* Indicates a base64 encoded structure */
597 parameter = strstr(request, ":: ");
599 parameter = strstr(request, ": ");
602 DEBUG(0, ("Parameter not found!\n"));
603 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
620 base64_decode_inplace(parameter);
623 if (strequal(request, "LANMAN-Challenge")) {
624 challenge = strhex_to_data_blob(parameter);
625 if (challenge.length != 8) {
626 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
628 (int)challenge.length);
629 challenge = data_blob(NULL, 0);
631 } else if (strequal(request, "NT-Response")) {
632 nt_response = strhex_to_data_blob(parameter);
633 if (nt_response.length < 24) {
634 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
636 (int)nt_response.length);
637 nt_response = data_blob(NULL, 0);
639 } else if (strequal(request, "LANMAN-Response")) {
640 lm_response = strhex_to_data_blob(parameter);
641 if (lm_response.length != 24) {
642 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
644 (int)lm_response.length);
645 lm_response = data_blob(NULL, 0);
647 } else if (strequal(request, "Password")) {
648 plaintext_password = smb_xstrdup(parameter);
649 } else if (strequal(request, "NT-Domain")) {
650 domain = smb_xstrdup(parameter);
651 } else if (strequal(request, "Username")) {
652 username = smb_xstrdup(parameter);
653 } else if (strequal(request, "Full-Username")) {
654 full_username = smb_xstrdup(parameter);
655 } else if (strequal(request, "Request-User-Session-Key")) {
656 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
657 } else if (strequal(request, "Request-LanMan-Session-Key")) {
658 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
660 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
664 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn, void *private)
666 char buf[SQUID_BUFFER_SIZE+1];
671 /* this is not a typo - x_fgets doesn't work too well under squid */
672 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
674 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
675 strerror(ferror(stdin))));
677 exit(1); /* BIIG buffer */
682 c=memchr(buf,'\n',sizeof(buf)-1);
691 DEBUG(2, ("Oversized message\n"));
692 x_fprintf(x_stderr, "ERR\n");
697 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
699 if (buf[0] == '\0') {
700 DEBUG(2, ("Invalid Request\n"));
701 x_fprintf(x_stderr, "ERR\n");
705 fn(helper_mode, buf, length, private);
708 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
709 void *private = NULL;
710 /* initialize FDescs */
711 x_setbuf(x_stdout, NULL);
712 x_setbuf(x_stderr, NULL);
714 manage_squid_request(stdio_mode, fn, &private);
731 OPT_USER_SESSION_KEY,
733 OPT_REQUIRE_MEMBERSHIP
736 int main(int argc, const char **argv)
738 static const char *helper_protocol;
743 /* NOTE: DO NOT change this interface without considering the implications!
744 This is an external interface, which other programs will use to interact
748 /* We do not use single-letter command abbreviations, because they harm future
749 interface stability. */
751 struct poptOption long_options[] = {
753 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
754 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
755 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
756 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
757 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
762 /* Samba client initialisation */
764 setup_logging("ntlm_auth", DEBUG_STDERR);
766 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
767 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
768 dyn_CONFIGFILE, strerror(errno));
774 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
776 /* Parse command line options */
779 poptPrintHelp(pc, stderr, 0);
783 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
784 POPT_CONTEXT_KEEP_FIRST);
786 while((opt = poptGetNextOpt(pc)) != -1) {
792 fprintf(stderr, "%s: %s\n",
793 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
798 if (opt_domain == NULL) {
799 opt_domain = lp_workgroup();
802 if (helper_protocol) {
804 for (i=0; i<NUM_HELPER_MODES; i++) {
805 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
806 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
810 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
812 for (i=0; i<NUM_HELPER_MODES; i++) {
813 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
820 x_fprintf(x_stderr, "username must be specified!\n\n");
821 poptPrintHelp(pc, stderr, 0);
825 if (opt_workstation == NULL) {
826 opt_workstation = lp_netbios_name();
830 opt_password = getpass("password: ");
836 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
837 if (!check_plaintext_auth(user, opt_password, True)) {