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 {
42 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
43 #define NTLM_AUTH_FLAG_LMKEY 0x0008
46 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
47 char *buf, int length);
49 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
50 char *buf, int length);
52 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
53 char *buf, int length);
55 static void manage_gensec_client_request (enum stdio_helper_mode stdio_helper_mode,
56 char *buf, int length);
58 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
59 char *buf, int length);
62 enum stdio_helper_mode mode;
64 stdio_helper_function fn;
65 } stdio_helper_protocols[] = {
66 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
67 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
68 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
69 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_client_request},
70 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_client_request},
71 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
72 { NUM_HELPER_MODES, NULL, NULL}
75 extern int winbindd_fd;
77 const char *opt_username;
78 const char *opt_domain;
79 const char *opt_workstation;
80 const char *opt_password;
83 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
84 form DOMAIN/user into a domain and a user */
86 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
90 char *p = strchr(domuser,*lp_winbind_separator());
97 fstrcpy(domain, domuser);
98 domain[PTR_DIFF(p, domuser)] = 0;
104 /* Authenticate a user with a plaintext password */
106 static BOOL check_plaintext_auth(const char *user, const char *pass,
107 BOOL stdout_diagnostics)
109 return (strcmp(pass, opt_password) == 0);
112 /* authenticate a user with an encrypted username/password */
114 static NTSTATUS local_pw_check_specified(const char *username,
116 const char *workstation,
117 const DATA_BLOB *challenge,
118 const DATA_BLOB *lm_response,
119 const DATA_BLOB *nt_response,
121 DATA_BLOB *lm_session_key,
122 DATA_BLOB *user_session_key,
127 uint8_t lm_pw[16], nt_pw[16];
128 uint8_t *lm_pwd, *nt_pwd;
129 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
131 nt_status = NT_STATUS_NO_MEMORY;
134 E_md4hash(opt_password, nt_pw);
135 if (E_deshash(opt_password, lm_pw)) {
143 nt_status = ntlm_password_check(mem_ctx,
151 lm_pwd, nt_pwd, user_session_key, lm_session_key);
153 if (NT_STATUS_IS_OK(nt_status)) {
157 *lp_winbind_separator(),
161 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
162 domain, username, workstation,
163 nt_errstr(nt_status)));
165 talloc_destroy(mem_ctx);
168 *error_string = strdup(nt_errstr(nt_status));
175 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
178 uint8 lm_pw[16], nt_pw[16];
179 uint8_t *lm_pwd, *nt_pwd;
181 E_md4hash(opt_password, nt_pw);
182 if (E_deshash(opt_password, lm_pw)) {
189 nt_status = ntlm_password_check(ntlmssp_state->mem_ctx,
190 &ntlmssp_state->chal,
191 &ntlmssp_state->lm_resp,
192 &ntlmssp_state->nt_resp,
196 ntlmssp_state->domain,
197 lm_pwd, nt_pwd, user_session_key, lm_session_key);
199 if (NT_STATUS_IS_OK(nt_status)) {
200 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state->mem_ctx,
201 "%s%c%s", ntlmssp_state->domain,
202 *lp_winbind_separator(),
203 ntlmssp_state->user);
205 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
206 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
207 nt_errstr(nt_status)));
208 ntlmssp_state->auth_context = NULL;
213 static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_state)
215 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
217 if (!NT_STATUS_IS_OK(status)) {
218 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
223 /* Have we been given a local password, or should we ask winbind? */
225 (*ntlmssp_state)->check_password = local_pw_check;
226 (*ntlmssp_state)->get_domain = lp_workgroup;
227 (*ntlmssp_state)->get_global_myname = global_myname;
229 DEBUG(0, ("Winbind not supported in Samba4 ntlm_auth yet, specify --password\n"));
235 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
236 char *buf, int length)
238 static struct ntlmssp_state *ntlmssp_state = NULL;
239 DATA_BLOB request, reply;
242 if (strlen(buf) < 2) {
243 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
244 x_fprintf(x_stdout, "BH\n");
248 if (strlen(buf) > 3) {
249 request = base64_decode_data_blob(buf + 3);
251 request = data_blob(NULL, 0);
254 if ((strncmp(buf, "PW ", 3) == 0)) {
255 /* The calling application wants us to use a local password (rather than winbindd) */
257 opt_password = strndup((const char *)request.data, request.length);
259 if (opt_password == NULL) {
260 DEBUG(1, ("Out of memory\n"));
261 x_fprintf(x_stdout, "BH\n");
262 data_blob_free(&request);
266 x_fprintf(x_stdout, "OK\n");
267 data_blob_free(&request);
271 if (strncmp(buf, "YR", 2) == 0) {
273 ntlmssp_end(&ntlmssp_state);
274 } else if (strncmp(buf, "KK", 2) == 0) {
277 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
278 x_fprintf(x_stdout, "BH\n");
282 if (!ntlmssp_state) {
283 if (!NT_STATUS_IS_OK(nt_status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
284 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
289 DEBUG(10, ("got NTLMSSP packet:\n"));
290 dump_data(10, (const char *)request.data, request.length);
292 nt_status = ntlmssp_update(ntlmssp_state, NULL, request, &reply);
294 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
295 char *reply_base64 = base64_encode_data_blob(reply);
296 x_fprintf(x_stdout, "TT %s\n", reply_base64);
297 SAFE_FREE(reply_base64);
298 data_blob_free(&reply);
299 DEBUG(10, ("NTLMSSP challenge\n"));
300 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
301 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
302 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
304 ntlmssp_end(&ntlmssp_state);
305 } else if (!NT_STATUS_IS_OK(nt_status)) {
306 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
307 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
309 x_fprintf(x_stdout, "AF %s\n", (char *)ntlmssp_state->auth_context);
310 DEBUG(10, ("NTLMSSP OK!\n"));
313 data_blob_free(&request);
316 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
317 char *buf, int length)
322 pass=memchr(buf,' ',length);
324 DEBUG(2, ("Password not found. Denying access\n"));
325 x_fprintf(x_stdout, "ERR\n");
331 if (stdio_helper_mode == SQUID_2_5_BASIC) {
332 rfc1738_unescape(user);
333 rfc1738_unescape(pass);
336 if (check_plaintext_auth(user, pass, False)) {
337 x_fprintf(x_stdout, "OK\n");
339 x_fprintf(x_stdout, "ERR\n");
343 static void manage_gensec_client_request(enum stdio_helper_mode stdio_helper_mode,
344 char *buf, int length)
349 static struct gensec_security gensec_state;
353 if (strlen(buf) < 2) {
354 DEBUG(1, ("query [%s] invalid", buf));
355 x_fprintf(x_stdout, "BH\n");
359 if (strlen(buf) > 3) {
360 in = base64_decode_data_blob(buf + 3);
362 in = data_blob(NULL, 0);
365 if (strncmp(buf, "PW ", 3) == 0) {
367 /* We asked for a password and obviously got it :-) */
369 opt_password = strndup((const char *)in.data, in.length);
371 if (opt_password == NULL) {
372 DEBUG(1, ("Out of memory\n"));
373 x_fprintf(x_stdout, "BH\n");
378 x_fprintf(x_stdout, "OK\n");
382 if (strncmp(buf, "YR", 2) == 0) {
383 if (gensec_state.ops) {
384 gensec_state.ops->end(&gensec_state);
385 gensec_state.ops = NULL;
387 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
388 (strncmp(buf, "AF ", 3) != 0) &&
389 (strncmp(buf, "NA ", 3) != 0) ) {
390 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
391 x_fprintf(x_stdout, "BH\n");
397 x_fprintf(x_stdout, "PW\n");
403 if (!gensec_state.ops) {
404 if (stdio_helper_mode == GSS_SPNEGO_CLIENT) {
405 gensec_state.ops = gensec_security_by_oid(OID_SPNEGO);
406 } else if (stdio_helper_mode == NTLMSSP_CLIENT_1) {
407 gensec_state.ops = gensec_security_by_oid(OID_NTLMSSP);
411 gensec_state.user.name = opt_username;
412 gensec_state.user.domain = opt_domain;
413 gensec_state.user.password = opt_password;
414 nt_status = gensec_state.ops->client_start(&gensec_state);
416 if (!NT_STATUS_IS_OK(nt_status)) {
417 DEBUG(1, ("SPENGO login failed to initialise: %s\n", nt_errstr(nt_status)));
418 x_fprintf(x_stdout, "BH\n");
428 nt_status = gensec_state.ops->update(&gensec_state, NULL, in, &out);
430 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
432 out_base64 = base64_encode_data_blob(out);
434 x_fprintf(x_stdout, "YR %s\n", out_base64);
436 x_fprintf(x_stdout, "KK %s\n", out_base64);
438 SAFE_FREE(out_base64);
441 } else if (!NT_STATUS_IS_OK(nt_status)) {
442 DEBUG(1, ("SPENGO login failed: %s\n", nt_errstr(nt_status)));
443 x_fprintf(x_stdout, "BH\n");
445 x_fprintf(x_stdout, "AF\n");
451 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
452 char *buf, int length)
454 char *request, *parameter;
455 static DATA_BLOB challenge;
456 static DATA_BLOB lm_response;
457 static DATA_BLOB nt_response;
458 static char *full_username;
459 static char *username;
461 static char *plaintext_password;
462 static BOOL ntlm_server_1_user_session_key;
463 static BOOL ntlm_server_1_lm_session_key;
465 if (strequal(buf, ".")) {
466 if (!full_username && !username) {
467 x_fprintf(x_stdout, "Error: No username supplied!\n");
468 } else if (plaintext_password) {
469 /* handle this request as plaintext */
470 if (!full_username) {
471 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
472 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
476 if (check_plaintext_auth(full_username, plaintext_password, False)) {
477 x_fprintf(x_stdout, "Authenticated: Yes\n");
479 x_fprintf(x_stdout, "Authenticated: No\n");
481 } else if (!lm_response.data && !nt_response.data) {
482 x_fprintf(x_stdout, "Error: No password supplied!\n");
483 } else if (!challenge.data) {
484 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
486 char *error_string = NULL;
488 DATA_BLOB user_session_key;
491 if (full_username && !username) {
495 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
496 /* username might be 'tainted', don't print into our new-line deleimianted stream */
497 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
501 username = smb_xstrdup(fstr_user);
502 domain = smb_xstrdup(fstr_domain);
506 domain = smb_xstrdup(lp_workgroup());
509 if (ntlm_server_1_lm_session_key)
510 flags |= NTLM_AUTH_FLAG_LMKEY;
512 if (ntlm_server_1_user_session_key)
513 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
515 if (!NT_STATUS_IS_OK(
516 local_pw_check_specified(username,
528 x_fprintf(x_stdout, "Authenticated: No\n");
529 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
530 SAFE_FREE(error_string);
532 static char zeros[16];
534 char *hex_user_session_key;
536 x_fprintf(x_stdout, "Authenticated: Yes\n");
538 if (ntlm_server_1_lm_session_key
540 && (memcmp(zeros, lm_key.data,
541 lm_key.length) != 0)) {
542 hex_encode(lm_key.data,
545 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
546 SAFE_FREE(hex_lm_key);
549 if (ntlm_server_1_user_session_key
550 && user_session_key.length
551 && (memcmp(zeros, user_session_key.data,
552 user_session_key.length) != 0)) {
553 hex_encode(user_session_key.data,
554 user_session_key.length,
555 &hex_user_session_key);
556 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
557 SAFE_FREE(hex_user_session_key);
561 /* clear out the state */
562 challenge = data_blob(NULL, 0);
563 nt_response = data_blob(NULL, 0);
564 lm_response = data_blob(NULL, 0);
565 SAFE_FREE(full_username);
568 SAFE_FREE(plaintext_password);
569 ntlm_server_1_user_session_key = False;
570 ntlm_server_1_lm_session_key = False;
571 x_fprintf(x_stdout, ".\n");
578 /* Indicates a base64 encoded structure */
579 parameter = strstr(request, ":: ");
581 parameter = strstr(request, ": ");
584 DEBUG(0, ("Parameter not found!\n"));
585 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
602 base64_decode_inplace(parameter);
605 if (strequal(request, "LANMAN-Challenge")) {
606 challenge = strhex_to_data_blob(parameter);
607 if (challenge.length != 8) {
608 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
610 (int)challenge.length);
611 challenge = data_blob(NULL, 0);
613 } else if (strequal(request, "NT-Response")) {
614 nt_response = strhex_to_data_blob(parameter);
615 if (nt_response.length < 24) {
616 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
618 (int)nt_response.length);
619 nt_response = data_blob(NULL, 0);
621 } else if (strequal(request, "LANMAN-Response")) {
622 lm_response = strhex_to_data_blob(parameter);
623 if (lm_response.length != 24) {
624 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
626 (int)lm_response.length);
627 lm_response = data_blob(NULL, 0);
629 } else if (strequal(request, "Password")) {
630 plaintext_password = smb_xstrdup(parameter);
631 } else if (strequal(request, "NT-Domain")) {
632 domain = smb_xstrdup(parameter);
633 } else if (strequal(request, "Username")) {
634 username = smb_xstrdup(parameter);
635 } else if (strequal(request, "Full-Username")) {
636 full_username = smb_xstrdup(parameter);
637 } else if (strequal(request, "Request-User-Session-Key")) {
638 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
639 } else if (strequal(request, "Request-LanMan-Session-Key")) {
640 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
642 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
646 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn)
648 char buf[SQUID_BUFFER_SIZE+1];
653 /* this is not a typo - x_fgets doesn't work too well under squid */
654 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
656 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
657 strerror(ferror(stdin))));
659 exit(1); /* BIIG buffer */
664 c=memchr(buf,'\n',sizeof(buf)-1);
673 DEBUG(2, ("Oversized message\n"));
674 x_fprintf(x_stderr, "ERR\n");
679 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
681 if (buf[0] == '\0') {
682 DEBUG(2, ("Invalid Request\n"));
683 x_fprintf(x_stderr, "ERR\n");
687 fn(helper_mode, buf, length);
691 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
692 /* initialize FDescs */
693 x_setbuf(x_stdout, NULL);
694 x_setbuf(x_stderr, NULL);
696 manage_squid_request(stdio_mode, fn);
713 OPT_USER_SESSION_KEY,
715 OPT_REQUIRE_MEMBERSHIP
718 int main(int argc, const char **argv)
720 static const char *helper_protocol;
725 /* NOTE: DO NOT change this interface without considering the implications!
726 This is an external interface, which other programs will use to interact
730 /* We do not use single-letter command abbreviations, because they harm future
731 interface stability. */
733 struct poptOption long_options[] = {
735 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
736 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
737 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
738 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
739 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
744 /* Samba client initialisation */
746 setup_logging("ntlm_auth", DEBUG_STDOUT);
748 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
749 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
750 dyn_CONFIGFILE, strerror(errno));
756 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
758 /* Parse command line options */
761 poptPrintHelp(pc, stderr, 0);
765 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
766 POPT_CONTEXT_KEEP_FIRST);
768 while((opt = poptGetNextOpt(pc)) != -1) {
774 fprintf(stderr, "%s: %s\n",
775 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
780 if (opt_domain == NULL) {
781 opt_domain = lp_workgroup();
784 if (helper_protocol) {
786 for (i=0; i<NUM_HELPER_MODES; i++) {
787 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
788 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
792 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
794 for (i=0; i<NUM_HELPER_MODES; i++) {
795 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
802 x_fprintf(x_stderr, "username must be specified!\n\n");
803 poptPrintHelp(pc, stderr, 0);
807 if (opt_workstation == NULL) {
808 opt_workstation = lp_netbios_name();
812 opt_password = getpass("password: ");
818 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
819 if (!check_plaintext_auth(user, opt_password, True)) {