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", 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;
105 /* Authenticate a user with a plaintext password */
107 static BOOL check_plaintext_auth(const char *user, const char *pass,
108 BOOL stdout_diagnostics)
110 return (strcmp(pass, opt_password) == 0);
113 /* authenticate a user with an encrypted username/password */
115 static NTSTATUS local_pw_check_specified(const char *username,
117 const char *workstation,
118 const DATA_BLOB *challenge,
119 const DATA_BLOB *lm_response,
120 const DATA_BLOB *nt_response,
122 DATA_BLOB *lm_session_key,
123 DATA_BLOB *user_session_key,
128 uint8_t lm_pw[16], nt_pw[16];
129 uint8_t *lm_pwd, *nt_pwd;
130 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
132 nt_status = NT_STATUS_NO_MEMORY;
135 E_md4hash(opt_password, nt_pw);
136 if (E_deshash(opt_password, lm_pw)) {
144 nt_status = ntlm_password_check(mem_ctx,
152 lm_pwd, nt_pwd, user_session_key, lm_session_key);
154 if (NT_STATUS_IS_OK(nt_status)) {
158 *lp_winbind_separator(),
162 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
163 domain, username, workstation,
164 nt_errstr(nt_status)));
166 talloc_destroy(mem_ctx);
169 *error_string = strdup(nt_errstr(nt_status));
176 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
177 char *buf, int length, void **private)
182 pass=memchr(buf,' ',length);
184 DEBUG(2, ("Password not found. Denying access\n"));
185 x_fprintf(x_stdout, "ERR\n");
191 if (stdio_helper_mode == SQUID_2_5_BASIC) {
192 rfc1738_unescape(user);
193 rfc1738_unescape(pass);
196 if (check_plaintext_auth(user, pass, False)) {
197 x_fprintf(x_stdout, "OK\n");
199 x_fprintf(x_stdout, "ERR\n");
203 /* This is a bit hairy, but the basic idea is to do a password callback
204 to the calling application. The callback comes from within gensec */
206 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
207 char *buf, int length, void **private)
210 struct gensec_security **gensec_state = (struct gensec_security **)private;
211 if (strlen(buf) < 2) {
212 DEBUG(1, ("query [%s] invalid", buf));
213 x_fprintf(x_stdout, "BH\n");
217 if (strlen(buf) > 3) {
218 in = base64_decode_data_blob(buf + 3);
220 in = data_blob(NULL, 0);
223 if (strncmp(buf, "PW ", 3) == 0) {
225 (*gensec_state)->password_callback_private = talloc_strndup((*gensec_state)->mem_ctx,
226 (const char *)in.data, in.length);
228 if ((*gensec_state)->password_callback_private == NULL) {
229 DEBUG(1, ("Out of memory\n"));
230 x_fprintf(x_stdout, "BH\n");
235 x_fprintf(x_stdout, "OK\n");
239 DEBUG(1, ("Asked for (and expected) a password\n"));
240 x_fprintf(x_stdout, "BH\n");
245 * Callback for gensec, to ask the calling application for a password. Uses the above function
246 * for the stdio part of this.
249 static NTSTATUS get_password(struct gensec_security *gensec_security, TALLOC_CTX *mem_ctx,
254 /* Ask for a password */
255 x_fprintf(x_stdout, "PW\n");
256 gensec_security->password_callback_private = NULL;
258 manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, &gensec_security);
259 *password = (char *)gensec_security->password_callback_private;
263 return NT_STATUS_INVALID_PARAMETER;
267 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
268 char *buf, int length, void **private)
271 DATA_BLOB out = data_blob(NULL, 0);
272 char *out_base64 = NULL;
273 const char *reply_arg = NULL;
274 struct gensec_security **gensec_state = (struct gensec_security **)private;
277 const char *reply_code;
279 if (strlen(buf) < 2) {
280 DEBUG(1, ("query [%s] invalid", buf));
281 x_fprintf(x_stdout, "BH\n");
285 if (strlen(buf) > 3) {
286 in = base64_decode_data_blob(buf + 3);
288 in = data_blob(NULL, 0);
291 if (strncmp(buf, "YR", 2) == 0) {
292 if (gensec_state && *gensec_state) {
293 gensec_end(gensec_state);
294 *gensec_state = NULL;
296 } else if ( (strncmp(buf, "OK", 2) == 0)) {
300 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
301 (strncmp(buf, "KK ", 3) != 0) &&
302 (strncmp(buf, "AF ", 3) != 0) &&
303 (strncmp(buf, "NA ", 3) != 0) &&
304 (strncmp(buf, "PW ", 3) != 0)) {
305 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
306 x_fprintf(x_stdout, "BH\n");
312 if (!(gensec_state && *gensec_state)) {
313 switch (stdio_helper_mode) {
314 case GSS_SPNEGO_CLIENT:
315 case NTLMSSP_CLIENT_1:
316 /* setup the client side */
318 if (!NT_STATUS_IS_OK(gensec_client_start(gensec_state))) {
321 gensec_set_username(*gensec_state, opt_username);
322 gensec_set_domain(*gensec_state, opt_domain);
324 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state, opt_password))) {
325 DEBUG(1, ("Out of memory\n"));
326 x_fprintf(x_stdout, "BH\n");
331 gensec_set_password_callback(*gensec_state, get_password, NULL);
335 case GSS_SPNEGO_SERVER:
336 case SQUID_2_5_NTLMSSP:
337 if (!NT_STATUS_IS_OK(gensec_server_start(gensec_state))) {
345 switch (stdio_helper_mode) {
346 case GSS_SPNEGO_CLIENT:
347 case GSS_SPNEGO_SERVER:
348 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_SPNEGO);
350 case NTLMSSP_CLIENT_1:
351 case SQUID_2_5_NTLMSSP:
352 nt_status = gensec_start_mech_by_oid(*gensec_state, OID_NTLMSSP);
358 if (!NT_STATUS_IS_OK(nt_status)) {
359 DEBUG(1, ("SPENGO login failed to initialise: %s\n", nt_errstr(nt_status)));
360 x_fprintf(x_stdout, "BH\n");
368 if (strncmp(buf, "PW ", 3) == 0) {
370 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state,
371 talloc_strndup((*gensec_state)->mem_ctx,
372 (const char *)in.data,
374 DEBUG(1, ("Out of memory\n"));
375 x_fprintf(x_stdout, "BH\n");
380 x_fprintf(x_stdout, "OK\n");
387 nt_status = gensec_update(*gensec_state, NULL, in, &out);
389 /* don't leak 'bad password'/'no such user' info to the network client */
390 nt_status = nt_status_squash(nt_status);
393 out_base64 = base64_encode_data_blob(out);
397 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
401 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
403 } else if ((*gensec_state)->gensec_role == GENSEC_SERVER) {
410 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
412 reply_arg = nt_errstr(nt_status);
413 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
414 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
416 reply_arg = nt_errstr(nt_status);
417 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
418 } else if (!NT_STATUS_IS_OK(nt_status)) {
420 reply_arg = nt_errstr(nt_status);
421 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
422 } else if /* OK */ ((*gensec_state)->gensec_role == GENSEC_SERVER) {
423 struct auth_session_info *session_info;
425 nt_status = gensec_session_info(*gensec_state, &session_info);
426 if (!NT_STATUS_IS_OK(nt_status)) {
428 reply_arg = nt_errstr(nt_status);
429 DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
433 reply_arg = talloc_asprintf((*gensec_state)->mem_ctx,
434 "%s%s%s", session_info->server_info->domain,
435 lp_winbind_separator(), session_info->server_info->account_name);
436 talloc_destroy(session_info->mem_ctx);
438 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
445 switch (stdio_helper_mode) {
446 case GSS_SPNEGO_SERVER:
447 x_fprintf(x_stdout, "%s %s %s\n", reply_code,
448 out_base64 ? out_base64 : "*",
449 reply_arg ? reply_arg : "*");
453 x_fprintf(x_stdout, "%s %s\n", reply_code, out_base64);
454 } else if (reply_arg) {
455 x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
457 x_fprintf(x_stdout, "%s\n", reply_code);
461 SAFE_FREE(out_base64);
465 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
466 char *buf, int length, void **private)
468 char *request, *parameter;
469 static DATA_BLOB challenge;
470 static DATA_BLOB lm_response;
471 static DATA_BLOB nt_response;
472 static char *full_username;
473 static char *username;
475 static char *plaintext_password;
476 static BOOL ntlm_server_1_user_session_key;
477 static BOOL ntlm_server_1_lm_session_key;
479 if (strequal(buf, ".")) {
480 if (!full_username && !username) {
481 x_fprintf(x_stdout, "Error: No username supplied!\n");
482 } else if (plaintext_password) {
483 /* handle this request as plaintext */
484 if (!full_username) {
485 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
486 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
490 if (check_plaintext_auth(full_username, plaintext_password, False)) {
491 x_fprintf(x_stdout, "Authenticated: Yes\n");
493 x_fprintf(x_stdout, "Authenticated: No\n");
495 } else if (!lm_response.data && !nt_response.data) {
496 x_fprintf(x_stdout, "Error: No password supplied!\n");
497 } else if (!challenge.data) {
498 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
500 char *error_string = NULL;
502 DATA_BLOB user_session_key;
505 if (full_username && !username) {
509 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
510 /* username might be 'tainted', don't print into our new-line deleimianted stream */
511 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
515 username = smb_xstrdup(fstr_user);
516 domain = smb_xstrdup(fstr_domain);
520 domain = smb_xstrdup(lp_workgroup());
523 if (ntlm_server_1_lm_session_key)
524 flags |= NTLM_AUTH_FLAG_LMKEY;
526 if (ntlm_server_1_user_session_key)
527 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
529 if (!NT_STATUS_IS_OK(
530 local_pw_check_specified(username,
542 x_fprintf(x_stdout, "Authenticated: No\n");
543 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
544 SAFE_FREE(error_string);
546 static char zeros[16];
548 char *hex_user_session_key;
550 x_fprintf(x_stdout, "Authenticated: Yes\n");
552 if (ntlm_server_1_lm_session_key
554 && (memcmp(zeros, lm_key.data,
555 lm_key.length) != 0)) {
556 hex_encode(lm_key.data,
559 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
560 SAFE_FREE(hex_lm_key);
563 if (ntlm_server_1_user_session_key
564 && user_session_key.length
565 && (memcmp(zeros, user_session_key.data,
566 user_session_key.length) != 0)) {
567 hex_encode(user_session_key.data,
568 user_session_key.length,
569 &hex_user_session_key);
570 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
571 SAFE_FREE(hex_user_session_key);
575 /* clear out the state */
576 challenge = data_blob(NULL, 0);
577 nt_response = data_blob(NULL, 0);
578 lm_response = data_blob(NULL, 0);
579 SAFE_FREE(full_username);
582 SAFE_FREE(plaintext_password);
583 ntlm_server_1_user_session_key = False;
584 ntlm_server_1_lm_session_key = False;
585 x_fprintf(x_stdout, ".\n");
592 /* Indicates a base64 encoded structure */
593 parameter = strstr(request, ":: ");
595 parameter = strstr(request, ": ");
598 DEBUG(0, ("Parameter not found!\n"));
599 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
616 base64_decode_inplace(parameter);
619 if (strequal(request, "LANMAN-Challenge")) {
620 challenge = strhex_to_data_blob(parameter);
621 if (challenge.length != 8) {
622 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
624 (int)challenge.length);
625 challenge = data_blob(NULL, 0);
627 } else if (strequal(request, "NT-Response")) {
628 nt_response = strhex_to_data_blob(parameter);
629 if (nt_response.length < 24) {
630 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
632 (int)nt_response.length);
633 nt_response = data_blob(NULL, 0);
635 } else if (strequal(request, "LANMAN-Response")) {
636 lm_response = strhex_to_data_blob(parameter);
637 if (lm_response.length != 24) {
638 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
640 (int)lm_response.length);
641 lm_response = data_blob(NULL, 0);
643 } else if (strequal(request, "Password")) {
644 plaintext_password = smb_xstrdup(parameter);
645 } else if (strequal(request, "NT-Domain")) {
646 domain = smb_xstrdup(parameter);
647 } else if (strequal(request, "Username")) {
648 username = smb_xstrdup(parameter);
649 } else if (strequal(request, "Full-Username")) {
650 full_username = smb_xstrdup(parameter);
651 } else if (strequal(request, "Request-User-Session-Key")) {
652 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
653 } else if (strequal(request, "Request-LanMan-Session-Key")) {
654 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
656 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
660 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn, void *private)
662 char buf[SQUID_BUFFER_SIZE+1];
667 /* this is not a typo - x_fgets doesn't work too well under squid */
668 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
670 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
671 strerror(ferror(stdin))));
673 exit(1); /* BIIG buffer */
678 c=memchr(buf,'\n',sizeof(buf)-1);
687 DEBUG(2, ("Oversized message\n"));
688 x_fprintf(x_stderr, "ERR\n");
693 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
695 if (buf[0] == '\0') {
696 DEBUG(2, ("Invalid Request\n"));
697 x_fprintf(x_stderr, "ERR\n");
701 fn(helper_mode, buf, length, private);
704 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
705 void *private = NULL;
706 /* initialize FDescs */
707 x_setbuf(x_stdout, NULL);
708 x_setbuf(x_stderr, NULL);
710 manage_squid_request(stdio_mode, fn, &private);
727 OPT_USER_SESSION_KEY,
729 OPT_REQUIRE_MEMBERSHIP
732 int main(int argc, const char **argv)
734 static const char *helper_protocol;
739 /* NOTE: DO NOT change this interface without considering the implications!
740 This is an external interface, which other programs will use to interact
744 /* We do not use single-letter command abbreviations, because they harm future
745 interface stability. */
747 struct poptOption long_options[] = {
749 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
750 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
751 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
752 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
753 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
758 /* Samba client initialisation */
760 setup_logging("ntlm_auth", DEBUG_STDOUT);
762 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
763 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
764 dyn_CONFIGFILE, strerror(errno));
770 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
772 /* Parse command line options */
775 poptPrintHelp(pc, stderr, 0);
779 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
780 POPT_CONTEXT_KEEP_FIRST);
782 while((opt = poptGetNextOpt(pc)) != -1) {
788 fprintf(stderr, "%s: %s\n",
789 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
794 if (opt_domain == NULL) {
795 opt_domain = lp_workgroup();
798 if (helper_protocol) {
800 for (i=0; i<NUM_HELPER_MODES; i++) {
801 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
802 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
806 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
808 for (i=0; i<NUM_HELPER_MODES; i++) {
809 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
816 x_fprintf(x_stderr, "username must be specified!\n\n");
817 poptPrintHelp(pc, stderr, 0);
821 if (opt_workstation == NULL) {
822 opt_workstation = lp_netbios_name();
826 opt_password = getpass("password: ");
832 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
833 if (!check_plaintext_auth(user, opt_password, True)) {