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 *opt_username;
42 static const char *opt_domain;
43 static const char *opt_workstation;
44 static const char *opt_password;
45 static DATA_BLOB opt_challenge;
46 static DATA_BLOB opt_lm_response;
47 static DATA_BLOB opt_nt_response;
48 static int request_lm_key;
49 static int request_nt_key;
52 static char winbind_separator(void)
54 struct winbindd_response response;
61 ZERO_STRUCT(response);
63 /* Send off request */
65 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
67 d_printf("could not obtain winbind separator!\n");
71 sep = response.data.info.winbind_separator;
75 d_printf("winbind separator was NULL!\n");
82 static const char *get_winbind_domain(void)
84 struct winbindd_response response;
86 static fstring winbind_domain;
87 if (*winbind_domain) {
88 return winbind_domain;
91 ZERO_STRUCT(response);
93 /* Send off request */
95 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
97 d_printf("could not obtain winbind domain name!\n");
101 fstrcpy(winbind_domain, response.data.domain_name);
103 return winbind_domain;
107 static const char *get_winbind_netbios_name(void)
109 struct winbindd_response response;
111 static fstring winbind_netbios_name;
113 if (*winbind_netbios_name) {
114 return winbind_netbios_name;
117 ZERO_STRUCT(response);
119 /* Send off request */
121 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
122 NSS_STATUS_SUCCESS) {
123 d_printf("could not obtain winbind netbios name!\n");
127 fstrcpy(winbind_netbios_name, response.data.netbios_name);
129 return winbind_netbios_name;
133 /* Authenticate a user with a plaintext password */
135 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
137 struct winbindd_request request;
138 struct winbindd_response response;
141 /* Send off request */
143 ZERO_STRUCT(request);
144 ZERO_STRUCT(response);
146 fstrcpy(request.data.auth.user, user);
147 fstrcpy(request.data.auth.pass, pass);
149 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
151 /* Display response */
153 if (stdout_diagnostics) {
154 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
155 d_printf("Reading winbind reply failed! (0x01)\n");
158 d_printf("%s: %s (0x%x)\n",
159 response.data.auth.nt_status_string,
160 response.data.auth.error_string,
161 response.data.auth.nt_status);
163 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
164 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
167 DEBUG(3, ("%s: %s (0x%x)\n",
168 response.data.auth.nt_status_string,
169 response.data.auth.error_string,
170 response.data.auth.nt_status));
173 return (result == NSS_STATUS_SUCCESS);
176 /* authenticate a user with an encrypted username/password */
178 static NTSTATUS contact_winbind_auth_crap(const char *username,
180 const char *workstation,
181 const DATA_BLOB *challenge,
182 const DATA_BLOB *lm_response,
183 const DATA_BLOB *nt_response,
191 struct winbindd_request request;
192 struct winbindd_response response;
194 static uint8 zeros[16];
196 ZERO_STRUCT(request);
197 ZERO_STRUCT(response);
199 request.data.auth_crap.flags = flags;
201 fstrcpy(request.data.auth_crap.user, username);
203 fstrcpy(request.data.auth_crap.domain, domain);
204 fstrcpy(request.data.auth_crap.workstation, workstation);
206 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
208 if (lm_response && lm_response->length) {
209 memcpy(request.data.auth_crap.lm_resp, lm_response->data, MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
210 request.data.auth_crap.lm_resp_len = lm_response->length;
213 if (nt_response && nt_response->length) {
214 memcpy(request.data.auth_crap.nt_resp, nt_response->data, MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
215 request.data.auth_crap.nt_resp_len = nt_response->length;
218 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
220 /* Display response */
222 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
223 nt_status = NT_STATUS_UNSUCCESSFUL;
225 *error_string = smb_xstrdup("Reading winbind reply failed!");
229 nt_status = (NT_STATUS(response.data.auth.nt_status));
230 if (!NT_STATUS_IS_OK(nt_status)) {
232 *error_string = smb_xstrdup(response.data.auth.error_string);
236 if ((flags & WINBIND_PAM_LMKEY) && lm_key
237 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
238 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
239 memcpy(lm_key, response.data.auth.first_8_lm_hash,
240 sizeof(response.data.auth.first_8_lm_hash));
242 if ((flags & WINBIND_PAM_NTKEY) && nt_key
243 && (memcmp(zeros, response.data.auth.nt_session_key,
244 sizeof(response.data.auth.nt_session_key)) != 0)) {
245 memcpy(nt_key, response.data.auth.nt_session_key,
246 sizeof(response.data.auth.nt_session_key));
251 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state)
253 return contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
254 ntlmssp_state->workstation,
255 &ntlmssp_state->chal,
256 &ntlmssp_state->lm_resp,
257 &ntlmssp_state->nt_resp,
264 static void manage_squid_ntlmssp_request(enum squid_mode squid_mode,
265 char *buf, int length)
267 static NTLMSSP_STATE *ntlmssp_state = NULL;
268 DATA_BLOB request, reply;
271 if (strlen(buf) < 2) {
272 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
273 x_fprintf(x_stdout, "BH\n");
277 if (strlen(buf) > 3) {
278 request = base64_decode_data_blob(buf + 3);
279 } else if (strcmp(buf, "YR") == 0) {
280 request = data_blob(NULL, 0);
282 ntlmssp_server_end(&ntlmssp_state);
284 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
285 x_fprintf(x_stdout, "BH\n");
289 if (!ntlmssp_state) {
290 ntlmssp_server_start(&ntlmssp_state);
291 ntlmssp_state->check_password = winbind_pw_check;
292 ntlmssp_state->get_domain = get_winbind_domain;
293 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
296 DEBUG(10, ("got NTLMSSP packet:\n"));
297 dump_data(10, request.data, request.length);
299 nt_status = ntlmssp_server_update(ntlmssp_state, request, &reply);
301 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
302 char *reply_base64 = base64_encode_data_blob(reply);
303 x_fprintf(x_stdout, "TT %s\n", reply_base64);
304 SAFE_FREE(reply_base64);
305 data_blob_free(&reply);
306 DEBUG(10, ("NTLMSSP challenge\n"));
307 } else if (!NT_STATUS_IS_OK(nt_status)) {
308 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
309 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
311 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
312 DEBUG(10, ("NTLMSSP OK!\n"));
315 data_blob_free(&request);
318 static void manage_squid_basic_request(enum squid_mode squid_mode,
319 char *buf, int length)
324 pass=memchr(buf,' ',length);
326 DEBUG(2, ("Password not found. Denying access\n"));
327 x_fprintf(x_stderr, "ERR\n");
333 if (squid_mode == SQUID_2_5_BASIC) {
334 rfc1738_unescape(user);
335 rfc1738_unescape(pass);
338 if (check_plaintext_auth(user, pass, False)) {
339 x_fprintf(x_stdout, "OK\n");
341 x_fprintf(x_stdout, "ERR\n");
345 static void manage_squid_request(enum squid_mode squid_mode)
347 char buf[SQUID_BUFFER_SIZE+1];
352 /* this is not a typo - x_fgets doesn't work too well under squid */
353 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
354 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
355 strerror(ferror(stdin))));
356 exit(1); /* BIIG buffer */
359 c=memchr(buf,'\n',sizeof(buf)-1);
368 DEBUG(2, ("Oversized message\n"));
369 x_fprintf(x_stderr, "ERR\n");
374 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
376 if (buf[0] == '\0') {
377 DEBUG(2, ("Invalid Request\n"));
378 x_fprintf(x_stderr, "ERR\n");
382 if (squid_mode == SQUID_2_5_BASIC || squid_mode == SQUID_2_4_BASIC) {
383 manage_squid_basic_request(squid_mode, buf, length);
384 } else if (squid_mode == SQUID_2_5_NTLMSSP) {
385 manage_squid_ntlmssp_request(squid_mode, buf, length);
390 static void squid_stream(enum squid_mode squid_mode) {
391 /* initialize FDescs */
392 x_setbuf(x_stdout, NULL);
393 x_setbuf(x_stderr, NULL);
395 manage_squid_request(squid_mode);
400 /* Authenticate a user with a challenge/response */
402 static BOOL check_auth_crap(void)
412 static uint8 zeros[16];
415 flags |= WINBIND_PAM_LMKEY;
418 flags |= WINBIND_PAM_NTKEY;
420 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
430 if (!NT_STATUS_IS_OK(nt_status)) {
431 d_printf("%s (0x%x)\n",
433 NT_STATUS_V(nt_status));
434 SAFE_FREE(error_string);
439 && (memcmp(zeros, lm_key,
440 sizeof(lm_key)) != 0)) {
444 d_printf("LM_KEY: %s\n", hex_lm_key);
445 SAFE_FREE(hex_lm_key);
448 && (memcmp(zeros, nt_key,
449 sizeof(nt_key)) != 0)) {
453 d_printf("NT_KEY: %s\n", hex_nt_key);
454 SAFE_FREE(hex_nt_key);
461 Authenticate a user with a challenge/response, checking session key
462 and valid authentication types
465 static DATA_BLOB get_challenge(void)
467 static DATA_BLOB chal;
468 if (opt_challenge.length)
469 return opt_challenge;
471 chal = data_blob(NULL, 8);
473 generate_random_buffer(chal.data, chal.length, False);
478 * Test LM authentication, no NT response supplied
481 static BOOL test_lm(void)
485 DATA_BLOB lm_response = data_blob(NULL, 24);
490 DATA_BLOB chall = get_challenge();
496 flags |= WINBIND_PAM_LMKEY;
497 flags |= WINBIND_PAM_NTKEY;
499 SMBencrypt(opt_password, chall.data, lm_response.data);
500 E_deshash(opt_password, lm_hash);
502 nt_status = contact_winbind_auth_crap(opt_username, opt_domain, opt_workstation,
511 data_blob_free(&lm_response);
513 if (!NT_STATUS_IS_OK(nt_status)) {
514 d_printf("%s (0x%x)\n",
516 NT_STATUS_V(nt_status));
520 if (memcmp(lm_hash, lm_key,
521 sizeof(lm_key)) != 0) {
522 DEBUG(1, ("LM Key does not match expectations!\n"));
523 DEBUG(1, ("lm_key:\n"));
524 dump_data(1, lm_key, 8);
525 DEBUG(1, ("expected:\n"));
526 dump_data(1, lm_hash, 8);
528 if (memcmp(lm_hash, nt_key, 8) != 0) {
529 DEBUG(1, ("Session Key (first 8, lm hash) does not match expectations!\n"));
530 DEBUG(1, ("nt_key:\n"));
531 dump_data(1, nt_key, 8);
532 DEBUG(1, ("expected:\n"));
533 dump_data(1, lm_hash, 8);
539 * Test the normal 'LM and NTLM' combination
542 static BOOL test_lm_ntlm(void)
547 DATA_BLOB lm_response = data_blob(NULL, 24);
548 DATA_BLOB nt_response = data_blob(NULL, 24);
549 DATA_BLOB session_key = data_blob(NULL, 16);
555 DATA_BLOB chall = get_challenge();
561 flags |= WINBIND_PAM_LMKEY;
562 flags |= WINBIND_PAM_NTKEY;
564 SMBencrypt(opt_password,chall.data,lm_response.data);
565 E_deshash(opt_password, lm_hash);
567 SMBNTencrypt(opt_password,chall.data,nt_response.data);
569 E_md4hash(opt_password, nt_hash);
570 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
572 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
582 data_blob_free(&lm_response);
584 if (!NT_STATUS_IS_OK(nt_status)) {
585 d_printf("%s (0x%x)\n",
587 NT_STATUS_V(nt_status));
588 SAFE_FREE(error_string);
592 if (memcmp(lm_hash, lm_key,
593 sizeof(lm_key)) != 0) {
594 DEBUG(1, ("LM Key does not match expectations!\n"));
595 DEBUG(1, ("lm_key:\n"));
596 dump_data(1, lm_key, 8);
597 DEBUG(1, ("expected:\n"));
598 dump_data(1, lm_hash, 8);
601 if (memcmp(session_key.data, nt_key,
602 sizeof(nt_key)) != 0) {
603 DEBUG(1, ("NT Session Key does not match expectations!\n"));
604 DEBUG(1, ("nt_key:\n"));
605 dump_data(1, nt_key, 16);
606 DEBUG(1, ("expected:\n"));
607 dump_data(1, session_key.data, session_key.length);
614 * Test the NTLM response only, no LM.
617 static BOOL test_ntlm(void)
622 DATA_BLOB nt_response = data_blob(NULL, 24);
623 DATA_BLOB session_key = data_blob(NULL, 16);
629 DATA_BLOB chall = get_challenge();
635 flags |= WINBIND_PAM_LMKEY;
636 flags |= WINBIND_PAM_NTKEY;
638 SMBNTencrypt(opt_password,chall.data,nt_response.data);
639 E_md4hash(opt_password, nt_hash);
640 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
642 E_deshash(opt_password, lm_hash);
644 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
654 data_blob_free(&nt_response);
656 if (!NT_STATUS_IS_OK(nt_status)) {
657 d_printf("%s (0x%x)\n",
659 NT_STATUS_V(nt_status));
660 SAFE_FREE(error_string);
664 if (memcmp(lm_hash, lm_key,
665 sizeof(lm_key)) != 0) {
666 DEBUG(1, ("LM Key does not match expectations!\n"));
667 DEBUG(1, ("lm_key:\n"));
668 dump_data(1, lm_key, 8);
669 DEBUG(1, ("expected:\n"));
670 dump_data(1, lm_hash, 8);
673 if (memcmp(session_key.data, nt_key,
674 sizeof(nt_key)) != 0) {
675 DEBUG(1, ("NT Session Key does not match expectations!\n"));
676 DEBUG(1, ("nt_key:\n"));
677 dump_data(1, nt_key, 16);
678 DEBUG(1, ("expected:\n"));
679 dump_data(1, session_key.data, session_key.length);
686 * Test the NTLM response only, but in the LM field.
689 static BOOL test_ntlm_in_lm(void)
694 DATA_BLOB nt_response = data_blob(NULL, 24);
699 DATA_BLOB chall = get_challenge();
704 flags |= WINBIND_PAM_LMKEY;
705 flags |= WINBIND_PAM_NTKEY;
707 SMBNTencrypt(opt_password,chall.data,nt_response.data);
709 E_deshash(opt_password, lm_hash);
711 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
721 data_blob_free(&nt_response);
723 if (!NT_STATUS_IS_OK(nt_status)) {
724 d_printf("%s (0x%x)\n",
726 NT_STATUS_V(nt_status));
727 SAFE_FREE(error_string);
731 if (memcmp(lm_hash, lm_key,
732 sizeof(lm_key)) != 0) {
733 DEBUG(1, ("LM Key does not match expectations!\n"));
734 DEBUG(1, ("lm_key:\n"));
735 dump_data(1, lm_key, 8);
736 DEBUG(1, ("expected:\n"));
737 dump_data(1, lm_hash, 8);
740 if (memcmp(lm_hash, nt_key, 8) != 0) {
741 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
742 DEBUG(1, ("nt_key:\n"));
743 dump_data(1, nt_key, 16);
744 DEBUG(1, ("expected:\n"));
745 dump_data(1, lm_hash, 8);
752 * Test the NTLM response only, but in the both the NT and LM fields.
755 static BOOL test_ntlm_in_both(void)
760 DATA_BLOB nt_response = data_blob(NULL, 24);
761 DATA_BLOB session_key = data_blob(NULL, 16);
767 DATA_BLOB chall = get_challenge();
773 flags |= WINBIND_PAM_LMKEY;
774 flags |= WINBIND_PAM_NTKEY;
776 SMBNTencrypt(opt_password,chall.data,nt_response.data);
777 E_md4hash(opt_password, nt_hash);
778 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
780 E_deshash(opt_password, lm_hash);
782 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
792 data_blob_free(&nt_response);
794 if (!NT_STATUS_IS_OK(nt_status)) {
795 d_printf("%s (0x%x)\n",
797 NT_STATUS_V(nt_status));
798 SAFE_FREE(error_string);
802 if (memcmp(lm_hash, lm_key,
803 sizeof(lm_key)) != 0) {
804 DEBUG(1, ("LM Key does not match expectations!\n"));
805 DEBUG(1, ("lm_key:\n"));
806 dump_data(1, lm_key, 8);
807 DEBUG(1, ("expected:\n"));
808 dump_data(1, lm_hash, 8);
811 if (memcmp(session_key.data, nt_key,
812 sizeof(nt_key)) != 0) {
813 DEBUG(1, ("NT Session Key does not match expectations!\n"));
814 DEBUG(1, ("nt_key:\n"));
815 dump_data(1, nt_key, 16);
816 DEBUG(1, ("expected:\n"));
817 dump_data(1, session_key.data, session_key.length);
826 * Test the NTLMv2 response only
829 static BOOL test_ntlmv2(void)
834 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
835 DATA_BLOB nt_session_key = data_blob(NULL, 0);
836 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
839 DATA_BLOB chall = get_challenge();
844 flags |= WINBIND_PAM_NTKEY;
846 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
848 NULL, &ntlmv2_response,
850 data_blob_free(&names_blob);
853 data_blob_free(&names_blob);
855 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
865 data_blob_free(&ntlmv2_response);
867 if (!NT_STATUS_IS_OK(nt_status)) {
868 d_printf("%s (0x%x)\n",
870 NT_STATUS_V(nt_status));
871 SAFE_FREE(error_string);
875 if (memcmp(nt_session_key.data, nt_key,
876 sizeof(nt_key)) != 0) {
877 DEBUG(1, ("NT Session Key does not match expectations!\n"));
878 DEBUG(1, ("nt_key:\n"));
879 dump_data(1, nt_key, 16);
880 DEBUG(1, ("expected:\n"));
881 dump_data(1, nt_session_key.data, nt_session_key.length);
888 * Test the NTLMv2 and LMv2 responses
891 static BOOL test_lmv2_ntlmv2(void)
896 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
897 DATA_BLOB lmv2_response = data_blob(NULL, 0);
898 DATA_BLOB nt_session_key = data_blob(NULL, 0);
899 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
902 DATA_BLOB chall = get_challenge();
907 flags |= WINBIND_PAM_NTKEY;
909 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
911 &lmv2_response, &ntlmv2_response,
913 data_blob_free(&names_blob);
916 data_blob_free(&names_blob);
918 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
928 data_blob_free(&lmv2_response);
929 data_blob_free(&ntlmv2_response);
931 if (!NT_STATUS_IS_OK(nt_status)) {
932 d_printf("%s (0x%x)\n",
934 NT_STATUS_V(nt_status));
935 SAFE_FREE(error_string);
939 if (memcmp(nt_session_key.data, nt_key,
940 sizeof(nt_key)) != 0) {
941 DEBUG(1, ("NT Session Key does not match expectations!\n"));
942 DEBUG(1, ("nt_key:\n"));
943 dump_data(1, nt_key, 16);
944 DEBUG(1, ("expected:\n"));
945 dump_data(1, nt_session_key.data, nt_session_key.length);
952 * Test the LMv2 response only
955 static BOOL test_lmv2(void)
960 DATA_BLOB lmv2_response = data_blob(NULL, 0);
962 DATA_BLOB chall = get_challenge();
965 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
967 &lmv2_response, NULL,
972 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
982 data_blob_free(&lmv2_response);
984 if (!NT_STATUS_IS_OK(nt_status)) {
985 d_printf("%s (0x%x)\n",
987 NT_STATUS_V(nt_status));
988 SAFE_FREE(error_string);
996 * Test the normal 'LM and NTLM' combination but deliberately break one
999 static BOOL test_ntlm_broken(BOOL break_lm)
1004 DATA_BLOB lm_response = data_blob(NULL, 24);
1005 DATA_BLOB nt_response = data_blob(NULL, 24);
1006 DATA_BLOB session_key = data_blob(NULL, 16);
1012 DATA_BLOB chall = get_challenge();
1015 ZERO_STRUCT(lm_key);
1016 ZERO_STRUCT(nt_key);
1018 flags |= WINBIND_PAM_LMKEY;
1019 flags |= WINBIND_PAM_NTKEY;
1021 SMBencrypt(opt_password,chall.data,lm_response.data);
1022 E_deshash(opt_password, lm_hash);
1024 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1026 E_md4hash(opt_password, nt_hash);
1027 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1030 lm_response.data[0]++;
1032 nt_response.data[0]++;
1034 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1044 data_blob_free(&lm_response);
1046 if (!NT_STATUS_IS_OK(nt_status)) {
1047 d_printf("%s (0x%x)\n",
1049 NT_STATUS_V(nt_status));
1050 SAFE_FREE(error_string);
1054 if (memcmp(lm_hash, lm_key,
1055 sizeof(lm_key)) != 0) {
1056 DEBUG(1, ("LM Key does not match expectations!\n"));
1057 DEBUG(1, ("lm_key:\n"));
1058 dump_data(1, lm_key, 8);
1059 DEBUG(1, ("expected:\n"));
1060 dump_data(1, lm_hash, 8);
1063 if (memcmp(session_key.data, nt_key,
1064 sizeof(nt_key)) != 0) {
1065 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1066 DEBUG(1, ("nt_key:\n"));
1067 dump_data(1, nt_key, 16);
1068 DEBUG(1, ("expected:\n"));
1069 dump_data(1, session_key.data, session_key.length);
1075 static BOOL test_ntlm_lm_broken(void)
1077 return test_ntlm_broken(True);
1080 static BOOL test_ntlm_ntlm_broken(void)
1082 return test_ntlm_broken(False);
1085 static BOOL test_ntlmv2_broken(BOOL break_lmv2)
1090 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1091 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1092 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1093 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1096 DATA_BLOB chall = get_challenge();
1099 ZERO_STRUCT(nt_key);
1101 flags |= WINBIND_PAM_NTKEY;
1103 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1105 &lmv2_response, &ntlmv2_response,
1107 data_blob_free(&names_blob);
1110 data_blob_free(&names_blob);
1112 /* Heh - this should break the appropriate password hash nicely! */
1115 lmv2_response.data[0]++;
1117 ntlmv2_response.data[0]++;
1119 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1129 data_blob_free(&lmv2_response);
1130 data_blob_free(&ntlmv2_response);
1132 if (!NT_STATUS_IS_OK(nt_status)) {
1133 d_printf("%s (0x%x)\n",
1135 NT_STATUS_V(nt_status));
1136 SAFE_FREE(error_string);
1143 static BOOL test_ntlmv2_lmv2_broken(void)
1145 return test_ntlmv2_broken(True);
1148 static BOOL test_ntlmv2_ntlmv2_broken(void)
1150 return test_ntlmv2_broken(False);
1165 check we get the correct session key in each case
1166 check what values we get for the LM session key
1175 {test_lm_ntlm, "LM and NTLM"},
1176 {test_ntlm, "NTLM"},
1177 {test_ntlm_in_lm, "NTLM in LM"},
1178 {test_ntlm_in_both, "NTLM in both"},
1179 {test_ntlmv2, "NTLMv2"},
1180 {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
1181 {test_lmv2, "LMv2"},
1182 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
1183 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
1184 {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
1185 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"}
1188 static BOOL diagnose_ntlm_auth(void)
1193 for (i=0; test_table[i].fn; i++) {
1194 if (!test_table[i].fn()) {
1195 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
1206 OPT_USERNAME = 1000,
1219 int main(int argc, const char **argv)
1222 static const char *helper_protocol;
1223 static int diagnostics;
1225 static const char *hex_challenge;
1226 static const char *hex_lm_response;
1227 static const char *hex_nt_response;
1231 size_t challenge_len;
1232 size_t lm_response_len;
1233 size_t nt_response_len;
1237 /* NOTE: DO NOT change this interface without considering the implications!
1238 This is an external interface, which other programs will use to interact
1242 /* We do not use single-letter command abbreviations, because they harm future
1243 interface stability. */
1245 struct poptOption long_options[] = {
1247 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1248 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1249 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1250 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1251 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1252 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1253 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1254 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1255 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1256 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
1257 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
1262 /* Samba client initialisation */
1268 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1270 /* Parse command line options */
1273 poptPrintHelp(pc, stderr, 0);
1277 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1278 POPT_CONTEXT_KEEP_FIRST);
1280 while((opt = poptGetNextOpt(pc)) != -1) {
1283 challenge = smb_xmalloc((strlen(hex_challenge)+1)/2);
1284 if ((challenge_len = strhex_to_str(challenge,
1285 strlen(hex_challenge),
1286 hex_challenge)) != 8) {
1287 x_fprintf(x_stderr, "hex decode of %s failed (only got %u bytes)!\n",
1288 hex_challenge, challenge_len);
1291 opt_challenge = data_blob(challenge, challenge_len);
1292 SAFE_FREE(challenge);
1295 lm_response = smb_xmalloc((strlen(hex_lm_response)+1)/2);
1296 lm_response_len = strhex_to_str(lm_response,
1297 strlen(hex_lm_response),
1299 if (lm_response_len != 24) {
1300 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
1303 opt_lm_response = data_blob(lm_response, lm_response_len);
1304 SAFE_FREE(lm_response);
1307 nt_response = smb_xmalloc((strlen(hex_nt_response)+1)/2);
1308 nt_response_len = strhex_to_str(nt_response,
1309 strlen(hex_nt_response),
1311 if (nt_response_len < 24) {
1312 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
1315 opt_nt_response = data_blob(nt_response, nt_response_len);
1316 SAFE_FREE(nt_response);
1321 if (helper_protocol) {
1322 if (strcmp(helper_protocol, "squid-2.5-ntlmssp")== 0) {
1323 squid_stream(SQUID_2_5_NTLMSSP);
1324 } else if (strcmp(helper_protocol, "squid-2.5-basic")== 0) {
1325 squid_stream(SQUID_2_5_BASIC);
1326 } else if (strcmp(helper_protocol, "squid-2.4-basic")== 0) {
1327 squid_stream(SQUID_2_4_BASIC);
1329 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
1334 if (!opt_username) {
1335 x_fprintf(x_stderr, "username must be specified!\n\n");
1336 poptPrintHelp(pc, stderr, 0);
1340 if (opt_domain == NULL) {
1341 opt_domain = get_winbind_domain();
1344 if (opt_workstation == NULL) {
1345 opt_workstation = "";
1348 if (opt_challenge.length) {
1349 if (!check_auth_crap()) {
1355 if (!opt_password) {
1356 opt_password = getpass("password: ");
1360 if (!diagnose_ntlm_auth()) {
1366 snprintf(user, sizeof(user)-1, "%s%c%s", opt_domain, winbind_separator(), opt_username);
1367 if (!check_plaintext_auth(user, opt_password, True)) {
1374 poptFreeContext(pc);