2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2002
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
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
39 extern int winbindd_fd;
41 static const char *helper_protocol;
42 static const char *opt_username;
43 static const char *opt_domain;
44 static const char *opt_workstation;
45 static const char *opt_password;
46 static DATA_BLOB opt_challenge;
47 static DATA_BLOB opt_lm_response;
48 static DATA_BLOB opt_nt_response;
49 static int request_lm_key;
50 static int request_nt_key;
51 static int diagnostics;
54 static char winbind_separator(void)
56 struct winbindd_response response;
63 ZERO_STRUCT(response);
65 /* Send off request */
67 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
69 d_printf("could not obtain winbind separator!\n");
73 sep = response.data.info.winbind_separator;
77 d_printf("winbind separator was NULL!\n");
84 static const char *get_winbind_domain(void)
86 struct winbindd_response response;
88 static fstring winbind_domain;
89 if (*winbind_domain) {
90 return winbind_domain;
93 ZERO_STRUCT(response);
95 /* Send off request */
97 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
99 d_printf("could not obtain winbind domain name!\n");
103 fstrcpy(winbind_domain, response.data.domain_name);
105 return winbind_domain;
109 static const char *get_winbind_netbios_name(void)
111 struct winbindd_response response;
113 static fstring winbind_netbios_name;
115 if (*winbind_netbios_name) {
116 return winbind_netbios_name;
119 ZERO_STRUCT(response);
121 /* Send off request */
123 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
124 NSS_STATUS_SUCCESS) {
125 d_printf("could not obtain winbind netbios name!\n");
129 fstrcpy(winbind_netbios_name, response.data.netbios_name);
131 return winbind_netbios_name;
135 /* Authenticate a user with a plaintext password */
137 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
139 struct winbindd_request request;
140 struct winbindd_response response;
143 /* Send off request */
145 ZERO_STRUCT(request);
146 ZERO_STRUCT(response);
148 fstrcpy(request.data.auth.user, user);
149 fstrcpy(request.data.auth.pass, pass);
151 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
153 /* Display response */
155 if (stdout_diagnostics) {
156 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
157 d_printf("Reading winbind reply failed! (0x01)\n");
160 d_printf("%s: %s (0x%x)\n",
161 response.data.auth.nt_status_string,
162 response.data.auth.error_string,
163 response.data.auth.nt_status);
165 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
166 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
169 DEBUG(3, ("%s: %s (0x%x)\n",
170 response.data.auth.nt_status_string,
171 response.data.auth.error_string,
172 response.data.auth.nt_status));
175 return (result == NSS_STATUS_SUCCESS);
178 /* authenticate a user with an encrypted username/password */
180 static NTSTATUS contact_winbind_auth_crap(const char *username,
182 const char *workstation,
183 const DATA_BLOB *challenge,
184 const DATA_BLOB *lm_response,
185 const DATA_BLOB *nt_response,
193 struct winbindd_request request;
194 struct winbindd_response response;
196 static uint8 zeros[16];
198 ZERO_STRUCT(request);
199 ZERO_STRUCT(response);
201 request.data.auth_crap.flags = flags;
203 fstrcpy(request.data.auth_crap.user, username);
205 fstrcpy(request.data.auth_crap.domain, domain);
206 fstrcpy(request.data.auth_crap.workstation, workstation);
208 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
210 if (lm_response && lm_response->length) {
211 memcpy(request.data.auth_crap.lm_resp, lm_response->data, MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
212 request.data.auth_crap.lm_resp_len = lm_response->length;
215 if (nt_response && nt_response->length) {
216 memcpy(request.data.auth_crap.nt_resp, nt_response->data, MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
217 request.data.auth_crap.nt_resp_len = nt_response->length;
220 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
222 /* Display response */
224 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
225 nt_status = NT_STATUS_UNSUCCESSFUL;
227 *error_string = smb_xstrdup("Reading winbind reply failed!");
231 nt_status = (NT_STATUS(response.data.auth.nt_status));
232 if (!NT_STATUS_IS_OK(nt_status)) {
234 *error_string = smb_xstrdup(response.data.auth.error_string);
238 if ((flags & WINBIND_PAM_LMKEY) && lm_key
239 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
240 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
241 memcpy(lm_key, response.data.auth.first_8_lm_hash,
242 sizeof(response.data.auth.first_8_lm_hash));
244 if ((flags & WINBIND_PAM_NTKEY) && nt_key
245 && (memcmp(zeros, response.data.auth.nt_session_key,
246 sizeof(response.data.auth.nt_session_key)) != 0)) {
247 memcpy(nt_key, response.data.auth.nt_session_key,
248 sizeof(response.data.auth.nt_session_key));
253 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state)
255 return contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
256 ntlmssp_state->workstation,
257 &ntlmssp_state->chal,
258 &ntlmssp_state->lm_resp,
259 &ntlmssp_state->nt_resp,
266 static void manage_squid_ntlmssp_request(enum squid_mode squid_mode,
267 char *buf, int length)
269 static NTLMSSP_STATE *ntlmssp_state = NULL;
270 DATA_BLOB request, reply;
273 if (strlen(buf) < 2) {
274 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
275 x_fprintf(x_stdout, "BH\n");
279 if (strlen(buf) > 3) {
280 request = base64_decode_data_blob(buf + 3);
281 } else if (strcmp(buf, "YR") == 0) {
282 request = data_blob(NULL, 0);
284 ntlmssp_server_end(&ntlmssp_state);
286 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
287 x_fprintf(x_stdout, "BH\n");
291 if (!ntlmssp_state) {
292 ntlmssp_server_start(&ntlmssp_state);
293 ntlmssp_state->check_password = winbind_pw_check;
294 ntlmssp_state->get_domain = get_winbind_domain;
295 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
298 DEBUG(10, ("got NTLMSSP packet:\n"));
299 dump_data(10, request.data, request.length);
301 nt_status = ntlmssp_server_update(ntlmssp_state, request, &reply);
303 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
304 char *reply_base64 = base64_encode_data_blob(reply);
305 x_fprintf(x_stdout, "TT %s\n", reply_base64);
306 SAFE_FREE(reply_base64);
307 data_blob_free(&reply);
308 DEBUG(10, ("NTLMSSP challenge\n"));
309 } else if (!NT_STATUS_IS_OK(nt_status)) {
310 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
311 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
313 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
314 DEBUG(10, ("NTLMSSP OK!\n"));
317 data_blob_free(&request);
320 static void manage_squid_basic_request(enum squid_mode squid_mode,
321 char *buf, int length)
326 pass=memchr(buf,' ',length);
328 DEBUG(2, ("Password not found. Denying access\n"));
329 x_fprintf(x_stderr, "ERR\n");
335 if (squid_mode == SQUID_2_5_BASIC) {
336 rfc1738_unescape(user);
337 rfc1738_unescape(pass);
340 if (check_plaintext_auth(user, pass, False)) {
341 x_fprintf(x_stdout, "OK\n");
343 x_fprintf(x_stdout, "ERR\n");
347 static void manage_squid_request(enum squid_mode squid_mode)
349 char buf[SQUID_BUFFER_SIZE+1];
354 /* this is not a typo - x_fgets doesn't work too well under squid */
355 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
356 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
357 strerror(ferror(stdin))));
358 exit(1); /* BIIG buffer */
361 c=memchr(buf,'\n',sizeof(buf)-1);
370 DEBUG(2, ("Oversized message\n"));
371 x_fprintf(x_stderr, "ERR\n");
376 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
378 if (buf[0] == '\0') {
379 DEBUG(2, ("Invalid Request\n"));
380 x_fprintf(x_stderr, "ERR\n");
384 if (squid_mode == SQUID_2_5_BASIC || squid_mode == SQUID_2_4_BASIC) {
385 manage_squid_basic_request(squid_mode, buf, length);
386 } else if (squid_mode == SQUID_2_5_NTLMSSP) {
387 manage_squid_ntlmssp_request(squid_mode, buf, length);
392 static void squid_stream(enum squid_mode squid_mode) {
393 /* initialize FDescs */
394 x_setbuf(x_stdout, NULL);
395 x_setbuf(x_stderr, NULL);
397 manage_squid_request(squid_mode);
402 /* Authenticate a user with a challenge/response */
404 static BOOL check_auth_crap(void)
414 static uint8 zeros[16];
417 flags |= WINBIND_PAM_LMKEY;
420 flags |= WINBIND_PAM_NTKEY;
422 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
432 if (!NT_STATUS_IS_OK(nt_status)) {
433 d_printf("%s (0x%x)\n",
435 NT_STATUS_V(nt_status));
436 SAFE_FREE(error_string);
441 && (memcmp(zeros, lm_key,
442 sizeof(lm_key)) != 0)) {
446 d_printf("LM_KEY: %s\n", hex_lm_key);
447 SAFE_FREE(hex_lm_key);
450 && (memcmp(zeros, nt_key,
451 sizeof(nt_key)) != 0)) {
455 d_printf("NT_KEY: %s\n", hex_nt_key);
456 SAFE_FREE(hex_nt_key);
463 Authenticate a user with a challenge/response, checking session key
464 and valid authentication types
467 static const DATA_BLOB get_challenge(void)
469 static DATA_BLOB chal;
470 if (opt_challenge.length)
471 return opt_challenge;
473 chal = data_blob(NULL, 8);
475 generate_random_buffer(chal.data, chal.length, False);
479 static BOOL test_lm(void)
483 DATA_BLOB lm_response = data_blob(NULL, 24);
487 DATA_BLOB chall = get_challenge();
490 flags |= WINBIND_PAM_LMKEY;
492 SMBencrypt(opt_password,chall.data,lm_response.data);
493 E_deshash(opt_password, lm_hash);
495 nt_status = contact_winbind_auth_crap(opt_username, opt_domain, opt_workstation,
504 data_blob_free(&lm_response);
506 if (!NT_STATUS_IS_OK(nt_status)) {
507 d_printf("%s (0x%x)\n",
509 NT_STATUS_V(nt_status));
513 if (memcmp(lm_hash, lm_key,
514 sizeof(lm_key)) != 0) {
515 DEBUG(1, ("LM Key does not match expectations!\n"));
516 DEBUG(1, ("lm_key:\n"));
517 dump_data(1, lm_key, 8);
518 DEBUG(1, ("expected:\n"));
519 dump_data(1, lm_hash, 8);
524 static BOOL test_lm_ntlm(void)
529 DATA_BLOB lm_response = data_blob(NULL, 24);
530 DATA_BLOB nt_response = data_blob(NULL, 24);
531 DATA_BLOB session_key = data_blob(NULL, 16);
537 DATA_BLOB chall = get_challenge();
540 flags |= WINBIND_PAM_LMKEY;
541 flags |= WINBIND_PAM_NTKEY;
543 SMBencrypt(opt_password,chall.data,lm_response.data);
544 E_deshash(opt_password, lm_hash);
546 SMBNTencrypt(opt_password,chall.data,nt_response.data);
548 E_md4hash(opt_password, nt_hash);
549 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
551 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
561 data_blob_free(&lm_response);
563 if (!NT_STATUS_IS_OK(nt_status)) {
564 d_printf("%s (0x%x)\n",
566 NT_STATUS_V(nt_status));
567 SAFE_FREE(error_string);
571 if (memcmp(lm_hash, lm_key,
572 sizeof(lm_key)) != 0) {
573 DEBUG(1, ("LM Key does not match expectations!\n"));
574 DEBUG(1, ("lm_key:\n"));
575 dump_data(1, lm_key, 8);
576 DEBUG(1, ("expected:\n"));
577 dump_data(1, lm_hash, 8);
580 if (memcmp(session_key.data, nt_key,
581 sizeof(nt_key)) != 0) {
582 DEBUG(1, ("NT Session Key does not match expectations!\n"));
583 DEBUG(1, ("nt_key:\n"));
584 dump_data(1, nt_key, 16);
585 DEBUG(1, ("expected:\n"));
586 dump_data(1, session_key.data, session_key.length);
592 static BOOL test_ntlm(void)
597 DATA_BLOB nt_response = data_blob(NULL, 24);
598 DATA_BLOB session_key = data_blob(NULL, 16);
602 DATA_BLOB chall = get_challenge();
605 flags |= WINBIND_PAM_NTKEY;
607 SMBNTencrypt(opt_password,chall.data,nt_response.data);
608 E_md4hash(opt_password, nt_hash);
609 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
611 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
621 data_blob_free(&nt_response);
623 if (!NT_STATUS_IS_OK(nt_status)) {
624 d_printf("%s (0x%x)\n",
626 NT_STATUS_V(nt_status));
627 SAFE_FREE(error_string);
631 if (memcmp(session_key.data, nt_key,
632 sizeof(nt_key)) != 0) {
633 DEBUG(1, ("NT Session Key does not match expectations!\n"));
634 DEBUG(1, ("nt_key:\n"));
635 dump_data(1, nt_key, 16);
636 DEBUG(1, ("expected:\n"));
637 dump_data(1, session_key.data, session_key.length);
653 check we get the correct session key in each case
654 check what values we get for the LM session key
662 {test_lm, "test LM"},
663 {test_lm_ntlm, "test LM and NTLM"},
664 {test_ntlm, "test NTLM"}
665 /* {test_lm_ntlmv2, "test NTLMv2"}, */
666 /* {test_lm_ntlmv2, "test NTLMv2 and LMv2"}, */
667 /* {test_lm_ntlmv2, "test LMv2"} */
670 static BOOL diagnose_ntlm_auth(void)
675 for (i=0; test_table[i].fn; i++) {
676 if (!test_table[i].fn()) {
677 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
701 int main(int argc, const char **argv)
705 static const char *hex_challenge;
706 static const char *hex_lm_response;
707 static const char *hex_nt_response;
711 size_t challenge_len;
712 size_t lm_response_len;
713 size_t nt_response_len;
717 /* NOTE: DO NOT change this interface without considering the implications!
718 This is an external interface, which other programs will use to interact
722 /* We do not use single-letter command abbreviations, because they harm future
723 interface stability. */
725 struct poptOption long_options[] = {
727 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
728 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
729 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
730 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
731 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
732 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
733 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
734 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
735 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
736 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
737 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
738 { NULL, 0, 0, NULL, 0, NULL}
741 /* Samba client initialisation */
747 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
749 /* Parse command line options */
752 poptPrintHelp(pc, stderr, 0);
756 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
757 POPT_CONTEXT_KEEP_FIRST);
759 while((opt = poptGetNextOpt(pc)) != -1) {
762 challenge = smb_xmalloc((strlen(hex_challenge)+1)/2);
763 if ((challenge_len = strhex_to_str(challenge,
764 strlen(hex_challenge),
765 hex_challenge)) != 8) {
766 x_fprintf(x_stderr, "hex decode of %s failed (only got %u bytes)!\n",
767 hex_challenge, challenge_len);
770 opt_challenge = data_blob(challenge, challenge_len);
771 SAFE_FREE(challenge);
774 lm_response = smb_xmalloc((strlen(hex_lm_response)+1)/2);
775 lm_response_len = strhex_to_str(lm_response,
776 strlen(hex_lm_response),
778 if (lm_response_len != 24) {
779 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
782 opt_lm_response = data_blob(lm_response, lm_response_len);
783 SAFE_FREE(lm_response);
786 nt_response = smb_xmalloc((strlen(hex_nt_response)+1)/2);
787 nt_response_len = strhex_to_str(nt_response,
788 strlen(hex_nt_response),
790 if (nt_response_len < 24) {
791 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
794 opt_nt_response = data_blob(nt_response, nt_response_len);
795 SAFE_FREE(nt_response);
800 if (helper_protocol) {
801 if (strcmp(helper_protocol, "squid-2.5-ntlmssp")== 0) {
802 squid_stream(SQUID_2_5_NTLMSSP);
803 } else if (strcmp(helper_protocol, "squid-2.5-basic")== 0) {
804 squid_stream(SQUID_2_5_BASIC);
805 } else if (strcmp(helper_protocol, "squid-2.4-basic")== 0) {
806 squid_stream(SQUID_2_4_BASIC);
808 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
814 x_fprintf(x_stderr, "username must be specified!\n\n");
815 poptPrintHelp(pc, stderr, 0);
819 if (opt_domain == NULL) {
820 opt_domain = get_winbind_domain();
823 if (opt_workstation == NULL) {
824 opt_workstation = "";
827 if (opt_challenge.length) {
828 if (!check_auth_crap()) {
835 opt_password = getpass("password: ");
839 if (!diagnose_ntlm_auth()) {
845 snprintf(user, sizeof(user)-1, "%s%c%s", opt_domain, winbind_separator(), opt_username);
846 if (!check_plaintext_auth(user, opt_password, True)) {