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.
28 #define DBGC_CLASS DBGC_WINBIND
30 #define SQUID_BUFFER_SIZE 2010
32 enum stdio_helper_mode {
43 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
44 #define NTLM_AUTH_FLAG_LMKEY 0x0008
47 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
48 char *buf, int length, void **private);
50 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
51 char *buf, int length, void **private);
53 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
54 char *buf, int length, void **private);
56 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
57 char *buf, int length, void **private);
59 static void manage_squid_request(enum stdio_helper_mode helper_mode,
60 stdio_helper_function fn, void *private);
63 enum stdio_helper_mode mode;
65 stdio_helper_function fn;
66 } stdio_helper_protocols[] = {
67 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
68 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
69 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
70 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
71 { GSS_SPNEGO_SERVER, "gss-spnego-server", manage_gensec_request},
72 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
73 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
74 { NUM_HELPER_MODES, NULL, NULL}
77 extern int winbindd_fd;
79 const char *opt_username;
80 const char *opt_domain;
81 const char *opt_workstation;
82 const char *opt_password;
85 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
86 form DOMAIN/user into a domain and a user */
88 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
92 char *p = strchr(domuser,*lp_winbind_separator());
99 fstrcpy(domain, domuser);
100 domain[PTR_DIFF(p, domuser)] = 0;
106 /* Authenticate a user with a plaintext password */
108 static BOOL check_plaintext_auth(const char *user, const char *pass,
109 BOOL stdout_diagnostics)
111 return (strcmp(pass, opt_password) == 0);
114 /* authenticate a user with an encrypted username/password */
116 static NTSTATUS local_pw_check_specified(const char *username,
118 const char *workstation,
119 const DATA_BLOB *challenge,
120 const DATA_BLOB *lm_response,
121 const DATA_BLOB *nt_response,
123 DATA_BLOB *lm_session_key,
124 DATA_BLOB *user_session_key,
129 uint8_t lm_pw[16], nt_pw[16];
130 uint8_t *lm_pwd, *nt_pwd;
131 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
133 nt_status = NT_STATUS_NO_MEMORY;
136 E_md4hash(opt_password, nt_pw);
137 if (E_deshash(opt_password, lm_pw)) {
145 nt_status = ntlm_password_check(mem_ctx,
153 lm_pwd, nt_pwd, user_session_key, lm_session_key);
155 if (NT_STATUS_IS_OK(nt_status)) {
159 *lp_winbind_separator(),
163 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
164 domain, username, workstation,
165 nt_errstr(nt_status)));
167 talloc_destroy(mem_ctx);
170 *error_string = strdup(nt_errstr(nt_status));
177 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
178 char *buf, int length, void **private)
183 pass=memchr(buf,' ',length);
185 DEBUG(2, ("Password not found. Denying access\n"));
186 x_fprintf(x_stdout, "ERR\n");
192 if (stdio_helper_mode == SQUID_2_5_BASIC) {
193 rfc1738_unescape(user);
194 rfc1738_unescape(pass);
197 if (check_plaintext_auth(user, pass, False)) {
198 x_fprintf(x_stdout, "OK\n");
200 x_fprintf(x_stdout, "ERR\n");
204 /* This is a bit hairy, but the basic idea is to do a password callback
205 to the calling application. The callback comes from within gensec */
207 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
208 char *buf, int length, void **private)
211 struct gensec_security **gensec_state = (struct gensec_security **)private;
212 if (strlen(buf) < 2) {
213 DEBUG(1, ("query [%s] invalid", buf));
214 x_fprintf(x_stdout, "BH\n");
218 if (strlen(buf) > 3) {
219 in = base64_decode_data_blob(buf + 3);
221 in = data_blob(NULL, 0);
224 if (strncmp(buf, "PW ", 3) == 0) {
226 (*gensec_state)->password_callback_private = talloc_strndup((*gensec_state)->mem_ctx,
227 (const char *)in.data, in.length);
229 if ((*gensec_state)->password_callback_private == NULL) {
230 DEBUG(1, ("Out of memory\n"));
231 x_fprintf(x_stdout, "BH\n");
236 x_fprintf(x_stdout, "OK\n");
240 DEBUG(1, ("Asked for (and expected) a password\n"));
241 x_fprintf(x_stdout, "BH\n");
246 * Callback for gensec, to ask the calling application for a password. Uses the above function
247 * for the stdio part of this.
250 static NTSTATUS get_password(struct gensec_security *gensec_security, TALLOC_CTX *mem_ctx,
255 /* Ask for a password */
256 x_fprintf(x_stdout, "PW\n");
257 gensec_security->password_callback_private = NULL;
259 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, &gensec_security);
260 *password = (char *)gensec_security->password_callback_private;
264 return NT_STATUS_INVALID_PARAMETER;
268 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
269 char *buf, int length, void **private)
272 DATA_BLOB out = data_blob(NULL, 0);
273 char *out_base64 = NULL;
274 const char *reply_arg = NULL;
275 struct gensec_security **gensec_state = (struct gensec_security **)private;
278 const char *reply_code;
280 if (strlen(buf) < 2) {
281 DEBUG(1, ("query [%s] invalid", buf));
282 x_fprintf(x_stdout, "BH\n");
286 if (strlen(buf) > 3) {
287 in = base64_decode_data_blob(buf + 3);
289 in = data_blob(NULL, 0);
292 if (strncmp(buf, "YR", 2) == 0) {
293 if (gensec_state && *gensec_state) {
294 gensec_end(gensec_state);
295 *gensec_state = NULL;
297 } else if ( (strncmp(buf, "OK", 2) == 0)) {
301 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
302 (strncmp(buf, "KK ", 3) != 0) &&
303 (strncmp(buf, "AF ", 3) != 0) &&
304 (strncmp(buf, "NA ", 3) != 0) &&
305 (strncmp(buf, "PW ", 3) != 0)) {
306 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
307 x_fprintf(x_stdout, "BH\n");
313 if (!(gensec_state && *gensec_state)) {
314 switch (stdio_helper_mode) {
315 case GSS_SPNEGO_CLIENT:
316 case NTLMSSP_CLIENT_1:
317 /* setup the client side */
319 if (!NT_STATUS_IS_OK(gensec_client_start(gensec_state))) {
322 gensec_set_username(*gensec_state, opt_username);
323 gensec_set_domain(*gensec_state, opt_domain);
325 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state, opt_password))) {
326 DEBUG(1, ("Out of memory\n"));
327 x_fprintf(x_stdout, "BH\n");
332 gensec_set_password_callback(*gensec_state, get_password, NULL);
336 case GSS_SPNEGO_SERVER:
337 case SQUID_2_5_NTLMSSP:
338 if (!NT_STATUS_IS_OK(gensec_server_start(gensec_state))) {
346 switch (stdio_helper_mode) {
347 case GSS_SPNEGO_CLIENT:
348 case GSS_SPNEGO_SERVER:
349 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_SPNEGO);
351 case NTLMSSP_CLIENT_1:
352 case SQUID_2_5_NTLMSSP:
353 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_NTLMSSP);
359 if (!NT_STATUS_IS_OK(nt_status)) {
360 DEBUG(1, ("SPENGO login failed to initialise: %s\n", nt_errstr(nt_status)));
361 x_fprintf(x_stdout, "BH\n");
369 if (strncmp(buf, "PW ", 3) == 0) {
371 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state,
372 talloc_strndup((*gensec_state)->mem_ctx,
373 (const char *)in.data,
375 DEBUG(1, ("Out of memory\n"));
376 x_fprintf(x_stdout, "BH\n");
381 x_fprintf(x_stdout, "OK\n");
388 nt_status = gensec_update(*gensec_state, NULL, in, &out);
390 /* don't leak 'bad password'/'no such user' info to the network client */
391 nt_status = nt_status_squash(nt_status);
394 out_base64 = base64_encode_data_blob(out);
398 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
402 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
404 } else if ((*gensec_state)->gensec_role == GENSEC_SERVER) {
411 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
413 reply_arg = nt_errstr(nt_status);
414 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
415 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
417 reply_arg = nt_errstr(nt_status);
418 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
419 } else if (!NT_STATUS_IS_OK(nt_status)) {
421 reply_arg = nt_errstr(nt_status);
422 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
423 } else if /* OK */ ((*gensec_state)->gensec_role == GENSEC_SERVER) {
424 struct auth_session_info *session_info;
426 nt_status = gensec_session_info(*gensec_state, &session_info);
427 if (!NT_STATUS_IS_OK(nt_status)) {
429 reply_arg = nt_errstr(nt_status);
430 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
434 reply_arg = talloc_asprintf((*gensec_state)->mem_ctx,
435 "%s%s%s", session_info->server_info->domain,
436 lp_winbind_separator(), session_info->server_info->account_name);
437 talloc_destroy(session_info->mem_ctx);
439 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
446 switch (stdio_helper_mode) {
447 case GSS_SPNEGO_SERVER:
449 x_fprintf(x_stdout, "%s %s %s\n", reply_code, out_base64, reply_arg);
450 } else if (reply_arg) {
451 x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
453 x_fprintf(x_stdout, "%s\n", reply_code);
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_STDOUT);
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)) {