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;
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:
448 x_fprintf(x_stdout, "%s %s %s\n", reply_code, out_base64, reply_arg);
449 } else if (reply_arg) {
450 x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
452 x_fprintf(x_stdout, "%s\n", reply_code);
456 x_fprintf(x_stdout, "%s %s\n", reply_code, out_base64);
457 } else if (reply_arg) {
458 x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
460 x_fprintf(x_stdout, "%s\n", reply_code);
464 SAFE_FREE(out_base64);
468 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
469 char *buf, int length, void **private)
471 char *request, *parameter;
472 static DATA_BLOB challenge;
473 static DATA_BLOB lm_response;
474 static DATA_BLOB nt_response;
475 static char *full_username;
476 static char *username;
478 static char *plaintext_password;
479 static BOOL ntlm_server_1_user_session_key;
480 static BOOL ntlm_server_1_lm_session_key;
482 if (strequal(buf, ".")) {
483 if (!full_username && !username) {
484 x_fprintf(x_stdout, "Error: No username supplied!\n");
485 } else if (plaintext_password) {
486 /* handle this request as plaintext */
487 if (!full_username) {
488 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
489 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
493 if (check_plaintext_auth(full_username, plaintext_password, False)) {
494 x_fprintf(x_stdout, "Authenticated: Yes\n");
496 x_fprintf(x_stdout, "Authenticated: No\n");
498 } else if (!lm_response.data && !nt_response.data) {
499 x_fprintf(x_stdout, "Error: No password supplied!\n");
500 } else if (!challenge.data) {
501 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
503 char *error_string = NULL;
505 DATA_BLOB user_session_key;
508 if (full_username && !username) {
512 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
513 /* username might be 'tainted', don't print into our new-line deleimianted stream */
514 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
518 username = smb_xstrdup(fstr_user);
519 domain = smb_xstrdup(fstr_domain);
523 domain = smb_xstrdup(lp_workgroup());
526 if (ntlm_server_1_lm_session_key)
527 flags |= NTLM_AUTH_FLAG_LMKEY;
529 if (ntlm_server_1_user_session_key)
530 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
532 if (!NT_STATUS_IS_OK(
533 local_pw_check_specified(username,
545 x_fprintf(x_stdout, "Authenticated: No\n");
546 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
547 SAFE_FREE(error_string);
549 static char zeros[16];
551 char *hex_user_session_key;
553 x_fprintf(x_stdout, "Authenticated: Yes\n");
555 if (ntlm_server_1_lm_session_key
557 && (memcmp(zeros, lm_key.data,
558 lm_key.length) != 0)) {
559 hex_encode(lm_key.data,
562 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
563 SAFE_FREE(hex_lm_key);
566 if (ntlm_server_1_user_session_key
567 && user_session_key.length
568 && (memcmp(zeros, user_session_key.data,
569 user_session_key.length) != 0)) {
570 hex_encode(user_session_key.data,
571 user_session_key.length,
572 &hex_user_session_key);
573 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
574 SAFE_FREE(hex_user_session_key);
578 /* clear out the state */
579 challenge = data_blob(NULL, 0);
580 nt_response = data_blob(NULL, 0);
581 lm_response = data_blob(NULL, 0);
582 SAFE_FREE(full_username);
585 SAFE_FREE(plaintext_password);
586 ntlm_server_1_user_session_key = False;
587 ntlm_server_1_lm_session_key = False;
588 x_fprintf(x_stdout, ".\n");
595 /* Indicates a base64 encoded structure */
596 parameter = strstr(request, ":: ");
598 parameter = strstr(request, ": ");
601 DEBUG(0, ("Parameter not found!\n"));
602 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
619 base64_decode_inplace(parameter);
622 if (strequal(request, "LANMAN-Challenge")) {
623 challenge = strhex_to_data_blob(parameter);
624 if (challenge.length != 8) {
625 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
627 (int)challenge.length);
628 challenge = data_blob(NULL, 0);
630 } else if (strequal(request, "NT-Response")) {
631 nt_response = strhex_to_data_blob(parameter);
632 if (nt_response.length < 24) {
633 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
635 (int)nt_response.length);
636 nt_response = data_blob(NULL, 0);
638 } else if (strequal(request, "LANMAN-Response")) {
639 lm_response = strhex_to_data_blob(parameter);
640 if (lm_response.length != 24) {
641 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
643 (int)lm_response.length);
644 lm_response = data_blob(NULL, 0);
646 } else if (strequal(request, "Password")) {
647 plaintext_password = smb_xstrdup(parameter);
648 } else if (strequal(request, "NT-Domain")) {
649 domain = smb_xstrdup(parameter);
650 } else if (strequal(request, "Username")) {
651 username = smb_xstrdup(parameter);
652 } else if (strequal(request, "Full-Username")) {
653 full_username = smb_xstrdup(parameter);
654 } else if (strequal(request, "Request-User-Session-Key")) {
655 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
656 } else if (strequal(request, "Request-LanMan-Session-Key")) {
657 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
659 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
663 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn, void *private)
665 char buf[SQUID_BUFFER_SIZE+1];
670 /* this is not a typo - x_fgets doesn't work too well under squid */
671 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
673 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
674 strerror(ferror(stdin))));
676 exit(1); /* BIIG buffer */
681 c=memchr(buf,'\n',sizeof(buf)-1);
690 DEBUG(2, ("Oversized message\n"));
691 x_fprintf(x_stderr, "ERR\n");
696 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
698 if (buf[0] == '\0') {
699 DEBUG(2, ("Invalid Request\n"));
700 x_fprintf(x_stderr, "ERR\n");
704 fn(helper_mode, buf, length, private);
707 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
708 void *private = NULL;
709 /* initialize FDescs */
710 x_setbuf(x_stdout, NULL);
711 x_setbuf(x_stderr, NULL);
713 manage_squid_request(stdio_mode, fn, &private);
730 OPT_USER_SESSION_KEY,
732 OPT_REQUIRE_MEMBERSHIP
735 int main(int argc, const char **argv)
737 static const char *helper_protocol;
742 /* NOTE: DO NOT change this interface without considering the implications!
743 This is an external interface, which other programs will use to interact
747 /* We do not use single-letter command abbreviations, because they harm future
748 interface stability. */
750 struct poptOption long_options[] = {
752 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
753 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
754 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
755 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
756 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
761 /* Samba client initialisation */
763 setup_logging("ntlm_auth", DEBUG_STDOUT);
765 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
766 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
767 dyn_CONFIGFILE, strerror(errno));
773 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
775 /* Parse command line options */
778 poptPrintHelp(pc, stderr, 0);
782 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
783 POPT_CONTEXT_KEEP_FIRST);
785 while((opt = poptGetNextOpt(pc)) != -1) {
791 fprintf(stderr, "%s: %s\n",
792 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
797 if (opt_domain == NULL) {
798 opt_domain = lp_workgroup();
801 if (helper_protocol) {
803 for (i=0; i<NUM_HELPER_MODES; i++) {
804 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
805 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
809 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
811 for (i=0; i<NUM_HELPER_MODES; i++) {
812 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
819 x_fprintf(x_stderr, "username must be specified!\n\n");
820 poptPrintHelp(pc, stderr, 0);
824 if (opt_workstation == NULL) {
825 opt_workstation = lp_netbios_name();
829 opt_password = getpass("password: ");
835 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
836 if (!check_plaintext_auth(user, opt_password, True)) {