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
40 extern int winbindd_fd;
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;
53 static char winbind_separator(void)
55 struct winbindd_response response;
62 ZERO_STRUCT(response);
64 /* Send off request */
66 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
68 d_printf("could not obtain winbind separator!\n");
72 sep = response.data.info.winbind_separator;
76 d_printf("winbind separator was NULL!\n");
83 static const char *get_winbind_domain(void)
85 struct winbindd_response response;
87 static fstring winbind_domain;
88 if (*winbind_domain) {
89 return winbind_domain;
92 ZERO_STRUCT(response);
94 /* Send off request */
96 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
98 d_printf("could not obtain winbind domain name!\n");
102 fstrcpy(winbind_domain, response.data.domain_name);
104 return winbind_domain;
108 static const char *get_winbind_netbios_name(void)
110 struct winbindd_response response;
112 static fstring winbind_netbios_name;
114 if (*winbind_netbios_name) {
115 return winbind_netbios_name;
118 ZERO_STRUCT(response);
120 /* Send off request */
122 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
123 NSS_STATUS_SUCCESS) {
124 d_printf("could not obtain winbind netbios name!\n");
128 fstrcpy(winbind_netbios_name, response.data.netbios_name);
130 return winbind_netbios_name;
134 /* Authenticate a user with a plaintext password */
136 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
138 struct winbindd_request request;
139 struct winbindd_response response;
142 /* Send off request */
144 ZERO_STRUCT(request);
145 ZERO_STRUCT(response);
147 fstrcpy(request.data.auth.user, user);
148 fstrcpy(request.data.auth.pass, pass);
150 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
152 /* Display response */
154 if (stdout_diagnostics) {
155 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
156 d_printf("Reading winbind reply failed! (0x01)\n");
159 d_printf("%s: %s (0x%x)\n",
160 response.data.auth.nt_status_string,
161 response.data.auth.error_string,
162 response.data.auth.nt_status);
164 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
165 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
168 DEBUG(3, ("%s: %s (0x%x)\n",
169 response.data.auth.nt_status_string,
170 response.data.auth.error_string,
171 response.data.auth.nt_status));
174 return (result == NSS_STATUS_SUCCESS);
177 /* authenticate a user with an encrypted username/password */
179 static NTSTATUS contact_winbind_auth_crap(const char *username,
181 const char *workstation,
182 const DATA_BLOB *challenge,
183 const DATA_BLOB *lm_response,
184 const DATA_BLOB *nt_response,
192 struct winbindd_request request;
193 struct winbindd_response response;
195 static uint8 zeros[16];
197 ZERO_STRUCT(request);
198 ZERO_STRUCT(response);
200 request.flags = flags;
202 fstrcpy(request.data.auth_crap.user, username);
204 fstrcpy(request.data.auth_crap.domain, domain);
205 fstrcpy(request.data.auth_crap.workstation, workstation);
207 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
209 if (lm_response && lm_response->length) {
210 memcpy(request.data.auth_crap.lm_resp, lm_response->data, MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
211 request.data.auth_crap.lm_resp_len = lm_response->length;
214 if (nt_response && nt_response->length) {
215 memcpy(request.data.auth_crap.nt_resp, nt_response->data, MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
216 request.data.auth_crap.nt_resp_len = nt_response->length;
219 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
221 /* Display response */
223 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
224 nt_status = NT_STATUS_UNSUCCESSFUL;
226 *error_string = smb_xstrdup("Reading winbind reply failed!");
230 nt_status = (NT_STATUS(response.data.auth.nt_status));
231 if (!NT_STATUS_IS_OK(nt_status)) {
233 *error_string = smb_xstrdup(response.data.auth.error_string);
237 if ((flags & WBFLAG_PAM_LMKEY) && lm_key
238 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
239 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
240 memcpy(lm_key, response.data.auth.first_8_lm_hash,
241 sizeof(response.data.auth.first_8_lm_hash));
243 if ((flags & WBFLAG_PAM_NTKEY) && nt_key
244 && (memcmp(zeros, response.data.auth.nt_session_key,
245 sizeof(response.data.auth.nt_session_key)) != 0)) {
246 memcpy(nt_key, response.data.auth.nt_session_key,
247 sizeof(response.data.auth.nt_session_key));
252 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state)
254 return contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
255 ntlmssp_state->workstation,
256 &ntlmssp_state->chal,
257 &ntlmssp_state->lm_resp,
258 &ntlmssp_state->nt_resp,
265 static void manage_squid_ntlmssp_request(enum squid_mode squid_mode,
266 char *buf, int length)
268 static NTLMSSP_STATE *ntlmssp_state = NULL;
269 DATA_BLOB request, reply;
272 if (strlen(buf) < 2) {
273 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
274 x_fprintf(x_stdout, "BH\n");
278 if (strlen(buf) > 3) {
279 request = base64_decode_data_blob(buf + 3);
280 } else if (strcmp(buf, "YR") == 0) {
281 request = data_blob(NULL, 0);
283 ntlmssp_server_end(&ntlmssp_state);
285 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
286 x_fprintf(x_stdout, "BH\n");
290 if (!ntlmssp_state) {
291 ntlmssp_server_start(&ntlmssp_state);
292 ntlmssp_state->check_password = winbind_pw_check;
293 ntlmssp_state->get_domain = get_winbind_domain;
294 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
297 DEBUG(10, ("got NTLMSSP packet:\n"));
298 dump_data(10, request.data, request.length);
300 nt_status = ntlmssp_server_update(ntlmssp_state, request, &reply);
302 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
303 char *reply_base64 = base64_encode_data_blob(reply);
304 x_fprintf(x_stdout, "TT %s\n", reply_base64);
305 SAFE_FREE(reply_base64);
306 data_blob_free(&reply);
307 DEBUG(10, ("NTLMSSP challenge\n"));
308 } else if (!NT_STATUS_IS_OK(nt_status)) {
309 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
310 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
312 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
313 DEBUG(10, ("NTLMSSP OK!\n"));
316 data_blob_free(&request);
319 static void manage_squid_basic_request(enum squid_mode squid_mode,
320 char *buf, int length)
325 pass=memchr(buf,' ',length);
327 DEBUG(2, ("Password not found. Denying access\n"));
328 x_fprintf(x_stderr, "ERR\n");
334 if (squid_mode == SQUID_2_5_BASIC) {
335 rfc1738_unescape(user);
336 rfc1738_unescape(pass);
339 if (check_plaintext_auth(user, pass, False)) {
340 x_fprintf(x_stdout, "OK\n");
342 x_fprintf(x_stdout, "ERR\n");
346 static void offer_gss_spnego_mechs(void) {
351 const char *OIDs[] = {OID_NTLMSSP, NULL};
355 /* Server negTokenInit (mech offerings) */
357 spnego.type = SPNEGO_NEG_TOKEN_INIT;
358 spnego.negTokenInit.mechTypes = OIDs;
361 asn1_push_tag(&asn1, ASN1_SEQUENCE(0));
362 asn1_push_tag(&asn1, ASN1_CONTEXT(0));
363 asn1_write_GeneralString(&asn1, "NONE");
366 spnego.negTokenInit.mechListMIC = data_blob(asn1.data, asn1.length);
369 len = write_spnego_data(&token, &spnego);
370 data_blob_free(&spnego.negTokenInit.mechListMIC);
373 DEBUG(1, ("Could not write SPNEGO data blob\n"));
374 x_fprintf(x_stdout, "BH\n");
378 reply_base64 = base64_encode_data_blob(token);
379 x_fprintf(x_stdout, "TT %s\n", reply_base64);
381 SAFE_FREE(reply_base64);
382 data_blob_free(&token);
383 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
387 static void manage_gss_spnego_request(enum squid_mode squid_mode,
388 char *buf, int length)
390 static NTLMSSP_STATE *ntlmssp_state = NULL;
392 DATA_BLOB request, token;
398 if (strlen(buf) < 2) {
400 if (ntlmssp_state != NULL) {
401 DEBUG(1, ("Request for initial SPNEGO request where "
402 "we already have a state\n"));
403 x_fprintf(x_stdout, "BH\n");
407 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
408 x_fprintf(x_stdout, "BH\n");
412 if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
414 /* Initial request, get the negTokenInit offering
417 offer_gss_spnego_mechs();
421 /* All subsequent requests are "KK" (Knock, Knock ;)) and have
422 a blob. This might be negTokenInit or negTokenTarg */
424 if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
425 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
426 x_fprintf(x_stdout, "BH\n");
430 request = base64_decode_data_blob(buf + 3);
432 len = read_spnego_data(request, &spnego);
435 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
436 x_fprintf(x_stdout, "BH\n");
440 if ( (spnego.type != SPNEGO_NEG_TOKEN_INIT) &&
441 (spnego.type != SPNEGO_NEG_TOKEN_TARG) ) {
443 DEBUG(1, ("Got an invalid SPNEGO token!\n"));
444 x_fprintf(x_stdout, "BH\n");
448 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
450 /* Second request from Client. This is where the
451 client offers its mechanism to use. We currently
452 only support NTLMSSP, the decision for Kerberos
453 would be taken here. */
455 if ( (spnego.negTokenInit.mechTypes == NULL) ||
456 (spnego.negTokenInit.mechTypes[0] == NULL) ) {
457 DEBUG(1, ("Client did not offer any mechanism"));
458 x_fprintf(x_stdout, "BH\n");
462 if ( strcmp(spnego.negTokenInit.mechTypes[0], OID_NTLMSSP) != 0 ) {
463 DEBUG(1, ("Client did not choose NTLMSSP but %s\n",
464 spnego.negTokenInit.mechTypes[0]));
465 x_fprintf(x_stdout, "BH\n");
469 if ( spnego.negTokenInit.mechToken.data == NULL ) {
470 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
471 x_fprintf(x_stdout, "BH\n");
475 if ( ntlmssp_state != NULL ) {
476 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
477 "already got one\n"));
478 x_fprintf(x_stdout, "BH\n");
480 ntlmssp_server_end(&ntlmssp_state);
484 ntlmssp_server_start(&ntlmssp_state);
485 ntlmssp_state->check_password = winbind_pw_check;
486 ntlmssp_state->get_domain = get_winbind_domain;
487 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
489 DEBUG(10, ("got NTLMSSP packet:\n"));
490 dump_data(10, spnego.negTokenInit.mechToken.data,
491 spnego.negTokenInit.mechToken.length);
494 spnego.type = SPNEGO_NEG_TOKEN_TARG;
495 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
496 spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
498 status = ntlmssp_server_update(ntlmssp_state,
499 spnego.negTokenInit.mechToken,
500 &spnego.negTokenTarg.responseToken);
504 /* spnego.type == SPNEGO_NEG_TOKEN_TARG */
508 if (spnego.negTokenTarg.responseToken.data == NULL) {
509 DEBUG(1, ("Got a negTokenArg without a responseToken!\n"));
510 x_fprintf(x_stdout, "BH\n");
514 status = ntlmssp_server_update(ntlmssp_state,
515 spnego.negTokenTarg.responseToken,
518 data_blob_free(&spnego.negTokenTarg.responseToken);
520 spnego.negTokenTarg.responseToken = response;
524 if ( !NT_STATUS_IS_OK(status) &&
525 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ) {
527 /* Neither ok nor more work to do, so reject */
529 x_fprintf(x_stdout, "NA %s\n", nt_errstr(status));
530 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(status)));
534 pstr_sprintf(reply, "TT");
536 if (NT_STATUS_IS_OK(status)) {
537 pstr_sprintf(reply, "AF %s\\%s",
538 ntlmssp_state->domain, ntlmssp_state->user);
540 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
542 DEBUG(10, ("NTLMSSP OK!\n"));
545 len = write_spnego_data(&token, &spnego);
548 DEBUG(1, ("Could not write SPNEGO data blob\n"));
549 x_fprintf(x_stdout, "BH\n");
553 reply_base64 = base64_encode_data_blob(token);
554 x_fprintf(x_stdout, "%s %s\n", reply, reply_base64);
555 SAFE_FREE(reply_base64);
556 data_blob_free(&token);
558 if (NT_STATUS_IS_OK(status)) {
559 ntlmssp_server_end(&ntlmssp_state);
565 static void manage_squid_request(enum squid_mode squid_mode)
567 char buf[SQUID_BUFFER_SIZE+1];
572 /* this is not a typo - x_fgets doesn't work too well under squid */
573 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
574 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
575 strerror(ferror(stdin))));
576 exit(1); /* BIIG buffer */
579 c=memchr(buf,'\n',sizeof(buf)-1);
588 DEBUG(2, ("Oversized message\n"));
589 x_fprintf(x_stderr, "ERR\n");
594 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
596 if (buf[0] == '\0') {
597 DEBUG(2, ("Invalid Request\n"));
598 x_fprintf(x_stderr, "ERR\n");
602 if (squid_mode == SQUID_2_5_BASIC || squid_mode == SQUID_2_4_BASIC) {
603 manage_squid_basic_request(squid_mode, buf, length);
604 } else if (squid_mode == SQUID_2_5_NTLMSSP) {
605 manage_squid_ntlmssp_request(squid_mode, buf, length);
606 } else if (squid_mode == GSS_SPNEGO) {
607 manage_gss_spnego_request(squid_mode, buf, length);
612 static void squid_stream(enum squid_mode squid_mode) {
613 /* initialize FDescs */
614 x_setbuf(x_stdout, NULL);
615 x_setbuf(x_stderr, NULL);
617 manage_squid_request(squid_mode);
622 /* Authenticate a user with a challenge/response */
624 static BOOL check_auth_crap(void)
633 static uint8 zeros[16];
635 x_setbuf(x_stdout, NULL);
638 flags |= WBFLAG_PAM_LMKEY;
641 flags |= WBFLAG_PAM_NTKEY;
643 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
653 if (!NT_STATUS_IS_OK(nt_status)) {
654 x_fprintf(x_stdout, "%s (0x%x)\n",
656 NT_STATUS_V(nt_status));
657 SAFE_FREE(error_string);
662 && (memcmp(zeros, lm_key,
663 sizeof(lm_key)) != 0)) {
667 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
668 SAFE_FREE(hex_lm_key);
671 && (memcmp(zeros, nt_key,
672 sizeof(nt_key)) != 0)) {
676 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_nt_key);
677 SAFE_FREE(hex_nt_key);
684 Authenticate a user with a challenge/response, checking session key
685 and valid authentication types
688 static DATA_BLOB get_challenge(void)
690 static DATA_BLOB chal;
691 if (opt_challenge.length)
692 return opt_challenge;
694 chal = data_blob(NULL, 8);
696 generate_random_buffer(chal.data, chal.length, False);
701 * Test LM authentication, no NT response supplied
704 static BOOL test_lm(void)
708 DATA_BLOB lm_response = data_blob(NULL, 24);
713 DATA_BLOB chall = get_challenge();
719 flags |= WBFLAG_PAM_LMKEY;
720 flags |= WBFLAG_PAM_NTKEY;
722 SMBencrypt(opt_password, chall.data, lm_response.data);
723 E_deshash(opt_password, lm_hash);
725 nt_status = contact_winbind_auth_crap(opt_username, opt_domain, opt_workstation,
734 data_blob_free(&lm_response);
736 if (!NT_STATUS_IS_OK(nt_status)) {
737 d_printf("%s (0x%x)\n",
739 NT_STATUS_V(nt_status));
743 if (memcmp(lm_hash, lm_key,
744 sizeof(lm_key)) != 0) {
745 DEBUG(1, ("LM Key does not match expectations!\n"));
746 DEBUG(1, ("lm_key:\n"));
747 dump_data(1, lm_key, 8);
748 DEBUG(1, ("expected:\n"));
749 dump_data(1, lm_hash, 8);
751 if (memcmp(lm_hash, nt_key, 8) != 0) {
752 DEBUG(1, ("Session Key (first 8, lm hash) does not match expectations!\n"));
753 DEBUG(1, ("nt_key:\n"));
754 dump_data(1, nt_key, 8);
755 DEBUG(1, ("expected:\n"));
756 dump_data(1, lm_hash, 8);
762 * Test the normal 'LM and NTLM' combination
765 static BOOL test_lm_ntlm(void)
770 DATA_BLOB lm_response = data_blob(NULL, 24);
771 DATA_BLOB nt_response = data_blob(NULL, 24);
772 DATA_BLOB session_key = data_blob(NULL, 16);
778 DATA_BLOB chall = get_challenge();
784 flags |= WBFLAG_PAM_LMKEY;
785 flags |= WBFLAG_PAM_NTKEY;
787 SMBencrypt(opt_password,chall.data,lm_response.data);
788 E_deshash(opt_password, lm_hash);
790 SMBNTencrypt(opt_password,chall.data,nt_response.data);
792 E_md4hash(opt_password, nt_hash);
793 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
795 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
805 data_blob_free(&lm_response);
807 if (!NT_STATUS_IS_OK(nt_status)) {
808 d_printf("%s (0x%x)\n",
810 NT_STATUS_V(nt_status));
811 SAFE_FREE(error_string);
815 if (memcmp(lm_hash, lm_key,
816 sizeof(lm_key)) != 0) {
817 DEBUG(1, ("LM Key does not match expectations!\n"));
818 DEBUG(1, ("lm_key:\n"));
819 dump_data(1, lm_key, 8);
820 DEBUG(1, ("expected:\n"));
821 dump_data(1, lm_hash, 8);
824 if (memcmp(session_key.data, nt_key,
825 sizeof(nt_key)) != 0) {
826 DEBUG(1, ("NT Session Key does not match expectations!\n"));
827 DEBUG(1, ("nt_key:\n"));
828 dump_data(1, nt_key, 16);
829 DEBUG(1, ("expected:\n"));
830 dump_data(1, session_key.data, session_key.length);
837 * Test the NTLM response only, no LM.
840 static BOOL test_ntlm(void)
845 DATA_BLOB nt_response = data_blob(NULL, 24);
846 DATA_BLOB session_key = data_blob(NULL, 16);
852 DATA_BLOB chall = get_challenge();
858 flags |= WBFLAG_PAM_LMKEY;
859 flags |= WBFLAG_PAM_NTKEY;
861 SMBNTencrypt(opt_password,chall.data,nt_response.data);
862 E_md4hash(opt_password, nt_hash);
863 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
865 E_deshash(opt_password, lm_hash);
867 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
877 data_blob_free(&nt_response);
879 if (!NT_STATUS_IS_OK(nt_status)) {
880 d_printf("%s (0x%x)\n",
882 NT_STATUS_V(nt_status));
883 SAFE_FREE(error_string);
887 if (memcmp(lm_hash, lm_key,
888 sizeof(lm_key)) != 0) {
889 DEBUG(1, ("LM Key does not match expectations!\n"));
890 DEBUG(1, ("lm_key:\n"));
891 dump_data(1, lm_key, 8);
892 DEBUG(1, ("expected:\n"));
893 dump_data(1, lm_hash, 8);
896 if (memcmp(session_key.data, nt_key,
897 sizeof(nt_key)) != 0) {
898 DEBUG(1, ("NT Session Key does not match expectations!\n"));
899 DEBUG(1, ("nt_key:\n"));
900 dump_data(1, nt_key, 16);
901 DEBUG(1, ("expected:\n"));
902 dump_data(1, session_key.data, session_key.length);
909 * Test the NTLM response only, but in the LM field.
912 static BOOL test_ntlm_in_lm(void)
917 DATA_BLOB nt_response = data_blob(NULL, 24);
922 DATA_BLOB chall = get_challenge();
927 flags |= WBFLAG_PAM_LMKEY;
928 flags |= WBFLAG_PAM_NTKEY;
930 SMBNTencrypt(opt_password,chall.data,nt_response.data);
932 E_deshash(opt_password, lm_hash);
934 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
944 data_blob_free(&nt_response);
946 if (!NT_STATUS_IS_OK(nt_status)) {
947 d_printf("%s (0x%x)\n",
949 NT_STATUS_V(nt_status));
950 SAFE_FREE(error_string);
954 if (memcmp(lm_hash, lm_key,
955 sizeof(lm_key)) != 0) {
956 DEBUG(1, ("LM Key does not match expectations!\n"));
957 DEBUG(1, ("lm_key:\n"));
958 dump_data(1, lm_key, 8);
959 DEBUG(1, ("expected:\n"));
960 dump_data(1, lm_hash, 8);
963 if (memcmp(lm_hash, nt_key, 8) != 0) {
964 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
965 DEBUG(1, ("nt_key:\n"));
966 dump_data(1, nt_key, 16);
967 DEBUG(1, ("expected:\n"));
968 dump_data(1, lm_hash, 8);
975 * Test the NTLM response only, but in the both the NT and LM fields.
978 static BOOL test_ntlm_in_both(void)
983 DATA_BLOB nt_response = data_blob(NULL, 24);
984 DATA_BLOB session_key = data_blob(NULL, 16);
990 DATA_BLOB chall = get_challenge();
996 flags |= WBFLAG_PAM_LMKEY;
997 flags |= WBFLAG_PAM_NTKEY;
999 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1000 E_md4hash(opt_password, nt_hash);
1001 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1003 E_deshash(opt_password, lm_hash);
1005 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1015 data_blob_free(&nt_response);
1017 if (!NT_STATUS_IS_OK(nt_status)) {
1018 d_printf("%s (0x%x)\n",
1020 NT_STATUS_V(nt_status));
1021 SAFE_FREE(error_string);
1025 if (memcmp(lm_hash, lm_key,
1026 sizeof(lm_key)) != 0) {
1027 DEBUG(1, ("LM Key does not match expectations!\n"));
1028 DEBUG(1, ("lm_key:\n"));
1029 dump_data(1, lm_key, 8);
1030 DEBUG(1, ("expected:\n"));
1031 dump_data(1, lm_hash, 8);
1034 if (memcmp(session_key.data, nt_key,
1035 sizeof(nt_key)) != 0) {
1036 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1037 DEBUG(1, ("nt_key:\n"));
1038 dump_data(1, nt_key, 16);
1039 DEBUG(1, ("expected:\n"));
1040 dump_data(1, session_key.data, session_key.length);
1049 * Test the NTLMv2 response only
1052 static BOOL test_ntlmv2(void)
1057 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1058 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1059 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1062 DATA_BLOB chall = get_challenge();
1065 ZERO_STRUCT(nt_key);
1067 flags |= WBFLAG_PAM_NTKEY;
1069 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1071 NULL, &ntlmv2_response,
1073 data_blob_free(&names_blob);
1076 data_blob_free(&names_blob);
1078 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1088 data_blob_free(&ntlmv2_response);
1090 if (!NT_STATUS_IS_OK(nt_status)) {
1091 d_printf("%s (0x%x)\n",
1093 NT_STATUS_V(nt_status));
1094 SAFE_FREE(error_string);
1098 if (memcmp(nt_session_key.data, nt_key,
1099 sizeof(nt_key)) != 0) {
1100 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1101 DEBUG(1, ("nt_key:\n"));
1102 dump_data(1, nt_key, 16);
1103 DEBUG(1, ("expected:\n"));
1104 dump_data(1, nt_session_key.data, nt_session_key.length);
1111 * Test the NTLMv2 and LMv2 responses
1114 static BOOL test_lmv2_ntlmv2(void)
1119 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1120 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1121 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1122 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1125 DATA_BLOB chall = get_challenge();
1128 ZERO_STRUCT(nt_key);
1130 flags |= WBFLAG_PAM_NTKEY;
1132 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1134 &lmv2_response, &ntlmv2_response,
1136 data_blob_free(&names_blob);
1139 data_blob_free(&names_blob);
1141 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1151 data_blob_free(&lmv2_response);
1152 data_blob_free(&ntlmv2_response);
1154 if (!NT_STATUS_IS_OK(nt_status)) {
1155 d_printf("%s (0x%x)\n",
1157 NT_STATUS_V(nt_status));
1158 SAFE_FREE(error_string);
1162 if (memcmp(nt_session_key.data, nt_key,
1163 sizeof(nt_key)) != 0) {
1164 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1165 DEBUG(1, ("nt_key:\n"));
1166 dump_data(1, nt_key, 16);
1167 DEBUG(1, ("expected:\n"));
1168 dump_data(1, nt_session_key.data, nt_session_key.length);
1175 * Test the LMv2 response only
1178 static BOOL test_lmv2(void)
1183 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1185 DATA_BLOB chall = get_challenge();
1188 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1190 &lmv2_response, NULL,
1195 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1205 data_blob_free(&lmv2_response);
1207 if (!NT_STATUS_IS_OK(nt_status)) {
1208 d_printf("%s (0x%x)\n",
1210 NT_STATUS_V(nt_status));
1211 SAFE_FREE(error_string);
1219 * Test the normal 'LM and NTLM' combination but deliberately break one
1222 static BOOL test_ntlm_broken(BOOL break_lm)
1227 DATA_BLOB lm_response = data_blob(NULL, 24);
1228 DATA_BLOB nt_response = data_blob(NULL, 24);
1229 DATA_BLOB session_key = data_blob(NULL, 16);
1235 DATA_BLOB chall = get_challenge();
1238 ZERO_STRUCT(lm_key);
1239 ZERO_STRUCT(nt_key);
1241 flags |= WBFLAG_PAM_LMKEY;
1242 flags |= WBFLAG_PAM_NTKEY;
1244 SMBencrypt(opt_password,chall.data,lm_response.data);
1245 E_deshash(opt_password, lm_hash);
1247 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1249 E_md4hash(opt_password, nt_hash);
1250 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1253 lm_response.data[0]++;
1255 nt_response.data[0]++;
1257 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1267 data_blob_free(&lm_response);
1269 if (!NT_STATUS_IS_OK(nt_status)) {
1270 d_printf("%s (0x%x)\n",
1272 NT_STATUS_V(nt_status));
1273 SAFE_FREE(error_string);
1277 if (memcmp(lm_hash, lm_key,
1278 sizeof(lm_key)) != 0) {
1279 DEBUG(1, ("LM Key does not match expectations!\n"));
1280 DEBUG(1, ("lm_key:\n"));
1281 dump_data(1, lm_key, 8);
1282 DEBUG(1, ("expected:\n"));
1283 dump_data(1, lm_hash, 8);
1286 if (memcmp(session_key.data, nt_key,
1287 sizeof(nt_key)) != 0) {
1288 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1289 DEBUG(1, ("nt_key:\n"));
1290 dump_data(1, nt_key, 16);
1291 DEBUG(1, ("expected:\n"));
1292 dump_data(1, session_key.data, session_key.length);
1298 static BOOL test_ntlm_lm_broken(void)
1300 return test_ntlm_broken(True);
1303 static BOOL test_ntlm_ntlm_broken(void)
1305 return test_ntlm_broken(False);
1308 static BOOL test_ntlmv2_broken(BOOL break_lmv2)
1313 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1314 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1315 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1316 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1319 DATA_BLOB chall = get_challenge();
1322 ZERO_STRUCT(nt_key);
1324 flags |= WBFLAG_PAM_NTKEY;
1326 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1328 &lmv2_response, &ntlmv2_response,
1330 data_blob_free(&names_blob);
1333 data_blob_free(&names_blob);
1335 /* Heh - this should break the appropriate password hash nicely! */
1338 lmv2_response.data[0]++;
1340 ntlmv2_response.data[0]++;
1342 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1352 data_blob_free(&lmv2_response);
1353 data_blob_free(&ntlmv2_response);
1355 if (!NT_STATUS_IS_OK(nt_status)) {
1356 d_printf("%s (0x%x)\n",
1358 NT_STATUS_V(nt_status));
1359 SAFE_FREE(error_string);
1366 static BOOL test_ntlmv2_lmv2_broken(void)
1368 return test_ntlmv2_broken(True);
1371 static BOOL test_ntlmv2_ntlmv2_broken(void)
1373 return test_ntlmv2_broken(False);
1388 check we get the correct session key in each case
1389 check what values we get for the LM session key
1398 {test_lm_ntlm, "LM and NTLM"},
1399 {test_ntlm, "NTLM"},
1400 {test_ntlm_in_lm, "NTLM in LM"},
1401 {test_ntlm_in_both, "NTLM in both"},
1402 {test_ntlmv2, "NTLMv2"},
1403 {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
1404 {test_lmv2, "LMv2"},
1405 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
1406 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
1407 {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
1408 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"}
1411 static BOOL diagnose_ntlm_auth(void)
1416 for (i=0; test_table[i].fn; i++) {
1417 if (!test_table[i].fn()) {
1418 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
1429 OPT_USERNAME = 1000,
1442 int main(int argc, const char **argv)
1445 static const char *helper_protocol;
1446 static int diagnostics;
1448 static const char *hex_challenge;
1449 static const char *hex_lm_response;
1450 static const char *hex_nt_response;
1454 size_t challenge_len;
1455 size_t lm_response_len;
1456 size_t nt_response_len;
1460 /* NOTE: DO NOT change this interface without considering the implications!
1461 This is an external interface, which other programs will use to interact
1465 /* We do not use single-letter command abbreviations, because they harm future
1466 interface stability. */
1468 struct poptOption long_options[] = {
1470 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1471 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1472 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1473 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1474 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1475 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1476 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1477 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1478 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1479 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
1480 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
1485 /* Samba client initialisation */
1489 /* Samba client initialisation */
1491 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1492 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1493 dyn_CONFIGFILE, strerror(errno));
1499 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1501 /* Parse command line options */
1504 poptPrintHelp(pc, stderr, 0);
1508 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1509 POPT_CONTEXT_KEEP_FIRST);
1511 while((opt = poptGetNextOpt(pc)) != -1) {
1514 challenge = smb_xmalloc((strlen(hex_challenge))/2+1);
1515 if ((challenge_len = strhex_to_str(challenge,
1516 strlen(hex_challenge),
1517 hex_challenge)) != 8) {
1518 x_fprintf(x_stderr, "hex decode of %s failed (only got %u bytes)!\n",
1519 hex_challenge, challenge_len);
1522 opt_challenge = data_blob(challenge, challenge_len);
1523 SAFE_FREE(challenge);
1526 lm_response = smb_xmalloc((strlen(hex_lm_response))/2+1);
1527 lm_response_len = strhex_to_str(lm_response,
1528 strlen(hex_lm_response),
1530 if (lm_response_len != 24) {
1531 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
1534 opt_lm_response = data_blob(lm_response, lm_response_len);
1535 SAFE_FREE(lm_response);
1538 nt_response = smb_xmalloc((strlen(hex_nt_response)+2)/2+1);
1539 nt_response_len = strhex_to_str(nt_response,
1540 strlen(hex_nt_response),
1542 if (nt_response_len < 24) {
1543 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
1546 opt_nt_response = data_blob(nt_response, nt_response_len);
1547 SAFE_FREE(nt_response);
1552 if (helper_protocol) {
1553 if (strcmp(helper_protocol, "squid-2.5-ntlmssp")== 0) {
1554 squid_stream(SQUID_2_5_NTLMSSP);
1555 } else if (strcmp(helper_protocol, "squid-2.5-basic")== 0) {
1556 squid_stream(SQUID_2_5_BASIC);
1557 } else if (strcmp(helper_protocol, "squid-2.4-basic")== 0) {
1558 squid_stream(SQUID_2_4_BASIC);
1559 } else if (strcmp(helper_protocol, "gss-spnego")== 0) {
1560 squid_stream(GSS_SPNEGO);
1562 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
1567 if (!opt_username) {
1568 x_fprintf(x_stderr, "username must be specified!\n\n");
1569 poptPrintHelp(pc, stderr, 0);
1573 if (opt_domain == NULL) {
1574 opt_domain = get_winbind_domain();
1577 if (opt_workstation == NULL) {
1578 opt_workstation = "";
1581 if (opt_challenge.length) {
1582 if (!check_auth_crap()) {
1588 if (!opt_password) {
1589 opt_password = getpass("password: ");
1593 if (!diagnose_ntlm_auth()) {
1599 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
1600 if (!check_plaintext_auth(user, opt_password, True)) {
1607 poptFreeContext(pc);