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
41 extern int winbindd_fd;
43 static const char *opt_username;
44 static const char *opt_domain;
45 static const char *opt_workstation;
46 static const char *opt_password;
47 static DATA_BLOB opt_challenge;
48 static DATA_BLOB opt_lm_response;
49 static DATA_BLOB opt_nt_response;
50 static int request_lm_key;
51 static int request_nt_key;
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.flags = flags;
203 if (push_utf8_fstring(request.data.auth_crap.user, username) == -1) {
204 *error_string = smb_xstrdup(
205 "unable to create utf8 string for username");
206 return NT_STATUS_UNSUCCESSFUL;
209 if (push_utf8_fstring(request.data.auth_crap.domain, domain) == -1) {
210 *error_string = smb_xstrdup(
211 "unable to create utf8 string for domain");
212 return NT_STATUS_UNSUCCESSFUL;
215 if (push_utf8_fstring(request.data.auth_crap.workstation,
216 workstation) == -1) {
217 *error_string = smb_xstrdup(
218 "unable to create utf8 string for workstation");
219 return NT_STATUS_UNSUCCESSFUL;
222 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
224 if (lm_response && lm_response->length) {
225 memcpy(request.data.auth_crap.lm_resp, lm_response->data, MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
226 request.data.auth_crap.lm_resp_len = lm_response->length;
229 if (nt_response && nt_response->length) {
230 memcpy(request.data.auth_crap.nt_resp, nt_response->data, MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
231 request.data.auth_crap.nt_resp_len = nt_response->length;
234 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
236 /* Display response */
238 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
239 nt_status = NT_STATUS_UNSUCCESSFUL;
241 *error_string = smb_xstrdup("Reading winbind reply failed!");
245 nt_status = (NT_STATUS(response.data.auth.nt_status));
246 if (!NT_STATUS_IS_OK(nt_status)) {
248 *error_string = smb_xstrdup(response.data.auth.error_string);
252 if ((flags & WBFLAG_PAM_LMKEY) && lm_key
253 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
254 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
255 memcpy(lm_key, response.data.auth.first_8_lm_hash,
256 sizeof(response.data.auth.first_8_lm_hash));
258 if ((flags & WBFLAG_PAM_NTKEY) && nt_key
259 && (memcmp(zeros, response.data.auth.nt_session_key,
260 sizeof(response.data.auth.nt_session_key)) != 0)) {
261 memcpy(nt_key, response.data.auth.nt_session_key,
262 sizeof(response.data.auth.nt_session_key));
267 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state)
269 return contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
270 ntlmssp_state->workstation,
271 &ntlmssp_state->chal,
272 &ntlmssp_state->lm_resp,
273 &ntlmssp_state->nt_resp,
280 static void manage_squid_ntlmssp_request(enum squid_mode squid_mode,
281 char *buf, int length)
283 static NTLMSSP_STATE *ntlmssp_state = NULL;
284 DATA_BLOB request, reply;
287 if (strlen(buf) < 2) {
288 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
289 x_fprintf(x_stdout, "BH\n");
293 if (strlen(buf) > 3) {
294 request = base64_decode_data_blob(buf + 3);
295 } else if (strcmp(buf, "YR") == 0) {
296 request = data_blob(NULL, 0);
298 ntlmssp_server_end(&ntlmssp_state);
300 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
301 x_fprintf(x_stdout, "BH\n");
305 if (!ntlmssp_state) {
306 ntlmssp_server_start(&ntlmssp_state);
307 ntlmssp_state->check_password = winbind_pw_check;
308 ntlmssp_state->get_domain = get_winbind_domain;
309 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
312 DEBUG(10, ("got NTLMSSP packet:\n"));
313 dump_data(10, (const char *)request.data, request.length);
315 nt_status = ntlmssp_server_update(ntlmssp_state, request, &reply);
317 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
318 char *reply_base64 = base64_encode_data_blob(reply);
319 x_fprintf(x_stdout, "TT %s\n", reply_base64);
320 SAFE_FREE(reply_base64);
321 data_blob_free(&reply);
322 DEBUG(10, ("NTLMSSP challenge\n"));
323 } else if (!NT_STATUS_IS_OK(nt_status)) {
324 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
325 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
327 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
328 DEBUG(10, ("NTLMSSP OK!\n"));
331 data_blob_free(&request);
334 static void manage_squid_basic_request(enum squid_mode squid_mode,
335 char *buf, int length)
340 pass=memchr(buf,' ',length);
342 DEBUG(2, ("Password not found. Denying access\n"));
343 x_fprintf(x_stderr, "ERR\n");
349 if (squid_mode == SQUID_2_5_BASIC) {
350 rfc1738_unescape(user);
351 rfc1738_unescape(pass);
354 if (check_plaintext_auth(user, pass, False)) {
355 x_fprintf(x_stdout, "OK\n");
357 x_fprintf(x_stdout, "ERR\n");
361 static void offer_gss_spnego_mechs(void) {
369 pstring myname_lower;
373 pstrcpy(myname_lower, global_myname());
374 strlower_m(myname_lower);
376 pstr_sprintf(principal, "%s$@%s", myname_lower, lp_realm());
378 /* Server negTokenInit (mech offerings) */
379 spnego.type = SPNEGO_NEG_TOKEN_INIT;
380 spnego.negTokenInit.mechTypes = smb_xmalloc(sizeof(char *) * 3);
382 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
383 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
384 spnego.negTokenInit.mechTypes[2] = NULL;
386 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
387 spnego.negTokenInit.mechTypes[1] = NULL;
391 spnego.negTokenInit.mechListMIC = data_blob(principal,
394 len = write_spnego_data(&token, &spnego);
395 free_spnego_data(&spnego);
398 DEBUG(1, ("Could not write SPNEGO data blob\n"));
399 x_fprintf(x_stdout, "BH\n");
403 reply_base64 = base64_encode_data_blob(token);
404 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
406 SAFE_FREE(reply_base64);
407 data_blob_free(&token);
408 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
412 static void manage_gss_spnego_request(enum squid_mode squid_mode,
413 char *buf, int length)
415 static NTLMSSP_STATE *ntlmssp_state = NULL;
416 SPNEGO_DATA request, response;
424 const char *reply_code;
426 pstring reply_argument;
428 if (strlen(buf) < 2) {
430 if (ntlmssp_state != NULL) {
431 DEBUG(1, ("Request for initial SPNEGO request where "
432 "we already have a state\n"));
433 x_fprintf(x_stdout, "BH\n");
437 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
438 x_fprintf(x_stdout, "BH\n");
442 if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
444 /* Initial request, get the negTokenInit offering
447 offer_gss_spnego_mechs();
451 /* All subsequent requests are "KK" (Knock, Knock ;)) and have
452 a blob. This might be negTokenInit or negTokenTarg */
454 if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
455 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
456 x_fprintf(x_stdout, "BH\n");
460 token = base64_decode_data_blob(buf + 3);
461 len = read_spnego_data(token, &request);
462 data_blob_free(&token);
465 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
466 x_fprintf(x_stdout, "BH\n");
470 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
472 /* Second request from Client. This is where the
473 client offers its mechanism to use. We currently
474 only support NTLMSSP, the decision for Kerberos
475 would be taken here. */
477 if ( (request.negTokenInit.mechTypes == NULL) ||
478 (request.negTokenInit.mechTypes[0] == NULL) ) {
479 DEBUG(1, ("Client did not offer any mechanism"));
480 x_fprintf(x_stdout, "BH\n");
484 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
486 if ( request.negTokenInit.mechToken.data == NULL ) {
487 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
488 x_fprintf(x_stdout, "BH\n");
492 if ( ntlmssp_state != NULL ) {
493 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
494 "already got one\n"));
495 x_fprintf(x_stdout, "BH\n");
496 ntlmssp_server_end(&ntlmssp_state);
500 ntlmssp_server_start(&ntlmssp_state);
501 ntlmssp_state->check_password = winbind_pw_check;
502 ntlmssp_state->get_domain = get_winbind_domain;
503 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
505 DEBUG(10, ("got NTLMSSP packet:\n"));
506 dump_data(10, (const char *)request.negTokenInit.mechToken.data,
507 request.negTokenInit.mechToken.length);
509 response.type = SPNEGO_NEG_TOKEN_TARG;
510 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
511 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
513 status = ntlmssp_server_update(ntlmssp_state,
514 request.negTokenInit.mechToken,
515 &response.negTokenTarg.responseToken);
519 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
524 uint8 session_key[16];
526 if ( request.negTokenInit.mechToken.data == NULL ) {
527 DEBUG(1, ("Client did not provide Kerberos data\n"));
528 x_fprintf(x_stdout, "BH\n");
532 response.type = SPNEGO_NEG_TOKEN_TARG;
533 response.negTokenTarg.supportedMech = strdup(OID_KERBEROS5_OLD);
534 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
535 response.negTokenTarg.responseToken = data_blob(NULL, 0);
537 status = ads_verify_ticket(lp_realm(),
538 &request.negTokenInit.mechToken,
539 &principal, &auth_data, &ap_rep,
542 /* Now in "principal" we have the name we are
545 if (NT_STATUS_IS_OK(status)) {
547 domain = strchr(principal, '@');
549 if (domain == NULL) {
550 DEBUG(1, ("Did not get a valid principal "
551 "from ads_verify_ticket\n"));
552 x_fprintf(x_stdout, "BH\n");
557 domain = strdup(domain);
558 user = strdup(principal);
560 data_blob_free(&ap_rep);
561 data_blob_free(&auth_data);
563 SAFE_FREE(principal);
570 if ( (request.negTokenTarg.supportedMech == NULL) ||
571 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
572 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
573 is the only one we support that sends this stuff */
574 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
575 request.negTokenTarg.supportedMech));
576 x_fprintf(x_stdout, "BH\n");
580 if (request.negTokenTarg.responseToken.data == NULL) {
581 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
582 x_fprintf(x_stdout, "BH\n");
586 status = ntlmssp_server_update(ntlmssp_state,
587 request.negTokenTarg.responseToken,
588 &response.negTokenTarg.responseToken);
590 response.type = SPNEGO_NEG_TOKEN_TARG;
591 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
592 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
594 if (NT_STATUS_IS_OK(status)) {
595 user = strdup(ntlmssp_state->user);
596 domain = strdup(ntlmssp_state->domain);
597 ntlmssp_server_end(&ntlmssp_state);
601 free_spnego_data(&request);
603 if (NT_STATUS_IS_OK(status)) {
604 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
606 pstr_sprintf(reply_argument, "%s\\%s", domain, user);
607 } else if (NT_STATUS_EQUAL(status,
608 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
609 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
611 pstr_sprintf(reply_argument, "*");
613 response.negTokenTarg.negResult = SPNEGO_REJECT;
615 pstrcpy(reply_argument, nt_errstr(status));
621 len = write_spnego_data(&token, &response);
622 free_spnego_data(&response);
625 DEBUG(1, ("Could not write SPNEGO data blob\n"));
626 x_fprintf(x_stdout, "BH\n");
630 reply_base64 = base64_encode_data_blob(token);
632 x_fprintf(x_stdout, "%s %s %s\n",
633 reply_code, reply_base64, reply_argument);
635 SAFE_FREE(reply_base64);
636 data_blob_free(&token);
641 static NTLMSSP_CLIENT_STATE *client_ntlmssp_state = NULL;
643 static BOOL manage_client_ntlmssp_init(SPNEGO_DATA spnego)
646 DATA_BLOB null_blob = data_blob(NULL, 0);
648 char *to_server_base64;
649 const char *my_mechs[] = {OID_NTLMSSP, NULL};
651 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
653 if (client_ntlmssp_state != NULL) {
654 DEBUG(1, ("Request for initial SPNEGO request where "
655 "we already have a state\n"));
659 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
660 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
664 if (opt_password == NULL) {
666 /* Request a password from the calling process. After
667 sending it, the calling process should retry with
670 DEBUG(10, ("Requesting password\n"));
671 x_fprintf(x_stdout, "PW\n");
675 status = ntlmssp_client_start(&client_ntlmssp_state);
677 if (!NT_STATUS_IS_OK(status)) {
678 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
680 ntlmssp_client_end(&client_ntlmssp_state);
684 status = ntlmssp_set_username(client_ntlmssp_state, opt_username);
686 if (!NT_STATUS_IS_OK(status)) {
687 DEBUG(1, ("Could not set username: %s\n",
689 ntlmssp_client_end(&client_ntlmssp_state);
693 status = ntlmssp_set_domain(client_ntlmssp_state, opt_domain);
695 if (!NT_STATUS_IS_OK(status)) {
696 DEBUG(1, ("Could not set domain: %s\n",
698 ntlmssp_client_end(&client_ntlmssp_state);
702 status = ntlmssp_set_password(client_ntlmssp_state, opt_password);
704 if (!NT_STATUS_IS_OK(status)) {
705 DEBUG(1, ("Could not set password: %s\n",
707 ntlmssp_client_end(&client_ntlmssp_state);
711 spnego.type = SPNEGO_NEG_TOKEN_INIT;
712 spnego.negTokenInit.mechTypes = my_mechs;
713 spnego.negTokenInit.reqFlags = 0;
714 spnego.negTokenInit.mechListMIC = null_blob;
716 status = ntlmssp_client_update(client_ntlmssp_state, null_blob,
717 &spnego.negTokenInit.mechToken);
719 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
720 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
722 ntlmssp_client_end(&client_ntlmssp_state);
726 write_spnego_data(&to_server, &spnego);
727 data_blob_free(&spnego.negTokenInit.mechToken);
729 to_server_base64 = base64_encode_data_blob(to_server);
730 data_blob_free(&to_server);
731 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
732 SAFE_FREE(to_server_base64);
736 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
739 DATA_BLOB null_blob = data_blob(NULL, 0);
742 char *to_server_base64;
744 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
746 if (client_ntlmssp_state == NULL) {
747 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
748 x_fprintf(x_stdout, "BH\n");
749 ntlmssp_client_end(&client_ntlmssp_state);
753 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
754 x_fprintf(x_stdout, "NA\n");
755 ntlmssp_client_end(&client_ntlmssp_state);
759 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
760 x_fprintf(x_stdout, "AF\n");
761 ntlmssp_client_end(&client_ntlmssp_state);
765 status = ntlmssp_client_update(client_ntlmssp_state,
766 spnego.negTokenTarg.responseToken,
769 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
770 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
771 "ntlmssp_client_update, got: %s\n",
773 x_fprintf(x_stdout, "BH\n");
774 data_blob_free(&request);
775 ntlmssp_client_end(&client_ntlmssp_state);
779 spnego.type = SPNEGO_NEG_TOKEN_TARG;
780 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
781 spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
782 spnego.negTokenTarg.responseToken = request;
783 spnego.negTokenTarg.mechListMIC = null_blob;
785 write_spnego_data(&to_server, &spnego);
786 data_blob_free(&request);
788 to_server_base64 = base64_encode_data_blob(to_server);
789 data_blob_free(&to_server);
790 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
791 SAFE_FREE(to_server_base64);
797 static BOOL manage_client_krb5_init(SPNEGO_DATA spnego)
800 DATA_BLOB tkt, to_server;
801 unsigned char session_key_krb5[16];
805 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
808 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
809 (spnego.negTokenInit.mechListMIC.length == 0) ) {
810 DEBUG(1, ("Did not get a principal for krb5\n"));
814 principal = malloc(spnego.negTokenInit.mechListMIC.length+1);
816 if (principal == NULL) {
817 DEBUG(1, ("Could not malloc principal\n"));
821 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
822 spnego.negTokenInit.mechListMIC.length);
823 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
825 tkt = cli_krb5_get_ticket(principal, 0, session_key_krb5);
827 if (tkt.data == NULL) {
831 /* Let's try to first get the TGT, for that we need a
834 if (opt_password == NULL) {
835 DEBUG(10, ("Requesting password\n"));
836 x_fprintf(x_stdout, "PW\n");
840 pstr_sprintf(user, "%s@%s", opt_username, opt_domain);
842 if (kerberos_kinit_password(user, opt_password, 0) != 0) {
843 DEBUG(10, ("Requesting TGT failed\n"));
844 x_fprintf(x_stdout, "NA\n");
848 tkt = cli_krb5_get_ticket(principal, 0, session_key_krb5);
853 reply.type = SPNEGO_NEG_TOKEN_INIT;
854 reply.negTokenInit.mechTypes = my_mechs;
855 reply.negTokenInit.reqFlags = 0;
856 reply.negTokenInit.mechToken = tkt;
857 reply.negTokenInit.mechListMIC = data_blob(NULL, 0);
859 len = write_spnego_data(&to_server, &reply);
860 data_blob_free(&tkt);
863 DEBUG(1, ("Could not write SPNEGO data blob\n"));
867 reply_base64 = base64_encode_data_blob(to_server);
868 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
870 SAFE_FREE(reply_base64);
871 data_blob_free(&to_server);
872 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
876 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
878 switch (spnego.negTokenTarg.negResult) {
879 case SPNEGO_ACCEPT_INCOMPLETE:
880 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
881 x_fprintf(x_stdout, "BH\n");
883 case SPNEGO_ACCEPT_COMPLETED:
884 DEBUG(10, ("Accept completed\n"));
885 x_fprintf(x_stdout, "AF\n");
888 DEBUG(10, ("Rejected\n"));
889 x_fprintf(x_stdout, "NA\n");
892 DEBUG(1, ("Got an invalid negTokenTarg\n"));
893 x_fprintf(x_stdout, "AF\n");
899 static void manage_gss_spnego_client_request(enum squid_mode squid_mode,
900 char *buf, int length)
906 if (strlen(buf) <= 3) {
907 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
908 x_fprintf(x_stdout, "BH\n");
912 request = base64_decode_data_blob(buf+3);
914 if (strncmp(buf, "PW ", 3) == 0) {
916 /* We asked for a password and obviously got it :-) */
918 opt_password = strndup((const char *)request.data, request.length);
920 if (opt_password == NULL) {
921 DEBUG(1, ("Out of memory\n"));
922 x_fprintf(x_stdout, "BH\n");
923 data_blob_free(&request);
927 x_fprintf(x_stdout, "OK\n");
928 data_blob_free(&request);
932 if ( (strncmp(buf, "TT ", 3) != 0) &&
933 (strncmp(buf, "AF ", 3) != 0) &&
934 (strncmp(buf, "NA ", 3) != 0) ) {
935 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
936 x_fprintf(x_stdout, "BH\n");
937 data_blob_free(&request);
941 /* So we got a server challenge to generate a SPNEGO
942 client-to-server request... */
944 len = read_spnego_data(request, &spnego);
945 data_blob_free(&request);
948 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
949 x_fprintf(x_stdout, "BH\n");
953 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
955 /* The server offers a list of mechanisms */
957 const char **mechType = spnego.negTokenInit.mechTypes;
959 while (*mechType != NULL) {
962 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
963 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
964 if (manage_client_krb5_init(spnego))
969 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
970 if (manage_client_ntlmssp_init(spnego))
977 DEBUG(1, ("Server offered no compatible mechanism\n"));
978 x_fprintf(x_stdout, "BH\n");
982 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
984 if (spnego.negTokenTarg.supportedMech == NULL) {
985 /* On accept/reject Windows does not send the
986 mechanism anymore. Handle that here and
987 shut down the mechanisms. */
989 switch (spnego.negTokenTarg.negResult) {
990 case SPNEGO_ACCEPT_COMPLETED:
991 x_fprintf(x_stdout, "AF\n");
994 x_fprintf(x_stdout, "NA\n");
997 DEBUG(1, ("Got a negTokenTarg with no mech and an "
998 "unknown negResult: %d\n",
999 spnego.negTokenTarg.negResult));
1000 x_fprintf(x_stdout, "BH\n");
1003 ntlmssp_client_end(&client_ntlmssp_state);
1007 if (strcmp(spnego.negTokenTarg.supportedMech,
1008 OID_NTLMSSP) == 0) {
1009 manage_client_ntlmssp_targ(spnego);
1014 if (strcmp(spnego.negTokenTarg.supportedMech,
1015 OID_KERBEROS5_OLD) == 0) {
1016 manage_client_krb5_targ(spnego);
1023 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1024 x_fprintf(x_stdout, "BH\n");
1028 free_spnego_data(&spnego);
1032 static void manage_squid_request(enum squid_mode squid_mode)
1034 char buf[SQUID_BUFFER_SIZE+1];
1039 /* this is not a typo - x_fgets doesn't work too well under squid */
1040 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1041 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
1042 strerror(ferror(stdin))));
1043 exit(1); /* BIIG buffer */
1046 c=memchr(buf,'\n',sizeof(buf)-1);
1055 DEBUG(2, ("Oversized message\n"));
1056 x_fprintf(x_stderr, "ERR\n");
1061 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
1063 if (buf[0] == '\0') {
1064 DEBUG(2, ("Invalid Request\n"));
1065 x_fprintf(x_stderr, "ERR\n");
1069 if (squid_mode == SQUID_2_5_BASIC || squid_mode == SQUID_2_4_BASIC) {
1070 manage_squid_basic_request(squid_mode, buf, length);
1071 } else if (squid_mode == SQUID_2_5_NTLMSSP) {
1072 manage_squid_ntlmssp_request(squid_mode, buf, length);
1073 } else if (squid_mode == GSS_SPNEGO) {
1074 manage_gss_spnego_request(squid_mode, buf, length);
1075 } else if (squid_mode == GSS_SPNEGO_CLIENT) {
1076 manage_gss_spnego_client_request(squid_mode, buf, length);
1081 static void squid_stream(enum squid_mode squid_mode) {
1082 /* initialize FDescs */
1083 x_setbuf(x_stdout, NULL);
1084 x_setbuf(x_stderr, NULL);
1086 manage_squid_request(squid_mode);
1091 /* Authenticate a user with a challenge/response */
1093 static BOOL check_auth_crap(void)
1102 static uint8 zeros[16];
1104 x_setbuf(x_stdout, NULL);
1107 flags |= WBFLAG_PAM_LMKEY;
1110 flags |= WBFLAG_PAM_NTKEY;
1112 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1118 (unsigned char *)lm_key,
1119 (unsigned char *)nt_key,
1122 if (!NT_STATUS_IS_OK(nt_status)) {
1123 x_fprintf(x_stdout, "%s (0x%x)\n",
1125 NT_STATUS_V(nt_status));
1126 SAFE_FREE(error_string);
1131 && (memcmp(zeros, lm_key,
1132 sizeof(lm_key)) != 0)) {
1133 hex_encode((const unsigned char *)lm_key,
1136 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
1137 SAFE_FREE(hex_lm_key);
1140 && (memcmp(zeros, nt_key,
1141 sizeof(nt_key)) != 0)) {
1142 hex_encode((const unsigned char *)nt_key,
1145 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_nt_key);
1146 SAFE_FREE(hex_nt_key);
1153 Authenticate a user with a challenge/response, checking session key
1154 and valid authentication types
1157 static DATA_BLOB get_challenge(void)
1159 static DATA_BLOB chal;
1160 if (opt_challenge.length)
1161 return opt_challenge;
1163 chal = data_blob(NULL, 8);
1165 generate_random_buffer(chal.data, chal.length, False);
1170 * Test LM authentication, no NT response supplied
1173 static BOOL test_lm(void)
1177 DATA_BLOB lm_response = data_blob(NULL, 24);
1182 DATA_BLOB chall = get_challenge();
1185 ZERO_STRUCT(lm_key);
1186 ZERO_STRUCT(nt_key);
1188 flags |= WBFLAG_PAM_LMKEY;
1189 flags |= WBFLAG_PAM_NTKEY;
1191 SMBencrypt(opt_password, chall.data, lm_response.data);
1192 E_deshash(opt_password, lm_hash);
1194 nt_status = contact_winbind_auth_crap(opt_username, opt_domain, opt_workstation,
1203 data_blob_free(&lm_response);
1205 if (!NT_STATUS_IS_OK(nt_status)) {
1206 d_printf("%s (0x%x)\n",
1208 NT_STATUS_V(nt_status));
1212 if (memcmp(lm_hash, lm_key,
1213 sizeof(lm_key)) != 0) {
1214 DEBUG(1, ("LM Key does not match expectations!\n"));
1215 DEBUG(1, ("lm_key:\n"));
1216 dump_data(1, (const char *)lm_key, 8);
1217 DEBUG(1, ("expected:\n"));
1218 dump_data(1, (const char *)lm_hash, 8);
1220 if (memcmp(lm_hash, nt_key, 8) != 0) {
1221 DEBUG(1, ("Session Key (first 8, lm hash) does not match expectations!\n"));
1222 DEBUG(1, ("nt_key:\n"));
1223 dump_data(1, (const char *)nt_key, 8);
1224 DEBUG(1, ("expected:\n"));
1225 dump_data(1, (const char *)lm_hash, 8);
1231 * Test the normal 'LM and NTLM' combination
1234 static BOOL test_lm_ntlm(void)
1239 DATA_BLOB lm_response = data_blob(NULL, 24);
1240 DATA_BLOB nt_response = data_blob(NULL, 24);
1241 DATA_BLOB session_key = data_blob(NULL, 16);
1247 DATA_BLOB chall = get_challenge();
1250 ZERO_STRUCT(lm_key);
1251 ZERO_STRUCT(nt_key);
1253 flags |= WBFLAG_PAM_LMKEY;
1254 flags |= WBFLAG_PAM_NTKEY;
1256 SMBencrypt(opt_password,chall.data,lm_response.data);
1257 E_deshash(opt_password, lm_hash);
1259 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1261 E_md4hash(opt_password, nt_hash);
1262 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1264 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1274 data_blob_free(&lm_response);
1276 if (!NT_STATUS_IS_OK(nt_status)) {
1277 d_printf("%s (0x%x)\n",
1279 NT_STATUS_V(nt_status));
1280 SAFE_FREE(error_string);
1284 if (memcmp(lm_hash, lm_key,
1285 sizeof(lm_key)) != 0) {
1286 DEBUG(1, ("LM Key does not match expectations!\n"));
1287 DEBUG(1, ("lm_key:\n"));
1288 dump_data(1, (const char *)lm_key, 8);
1289 DEBUG(1, ("expected:\n"));
1290 dump_data(1, (const char *)lm_hash, 8);
1293 if (memcmp(session_key.data, nt_key,
1294 sizeof(nt_key)) != 0) {
1295 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1296 DEBUG(1, ("nt_key:\n"));
1297 dump_data(1, (const char *)nt_key, 16);
1298 DEBUG(1, ("expected:\n"));
1299 dump_data(1, (const char *)session_key.data, session_key.length);
1306 * Test the NTLM response only, no LM.
1309 static BOOL test_ntlm(void)
1314 DATA_BLOB nt_response = data_blob(NULL, 24);
1315 DATA_BLOB session_key = data_blob(NULL, 16);
1321 DATA_BLOB chall = get_challenge();
1324 ZERO_STRUCT(lm_key);
1325 ZERO_STRUCT(nt_key);
1327 flags |= WBFLAG_PAM_LMKEY;
1328 flags |= WBFLAG_PAM_NTKEY;
1330 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1331 E_md4hash(opt_password, (unsigned char *)nt_hash);
1332 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1334 E_deshash(opt_password, (unsigned char *)lm_hash);
1336 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1342 (unsigned char *)lm_key,
1343 (unsigned char *)nt_key,
1346 data_blob_free(&nt_response);
1348 if (!NT_STATUS_IS_OK(nt_status)) {
1349 d_printf("%s (0x%x)\n",
1351 NT_STATUS_V(nt_status));
1352 SAFE_FREE(error_string);
1356 if (memcmp(lm_hash, lm_key,
1357 sizeof(lm_key)) != 0) {
1358 DEBUG(1, ("LM Key does not match expectations!\n"));
1359 DEBUG(1, ("lm_key:\n"));
1360 dump_data(1, lm_key, 8);
1361 DEBUG(1, ("expected:\n"));
1362 dump_data(1, lm_hash, 8);
1365 if (memcmp(session_key.data, nt_key,
1366 sizeof(nt_key)) != 0) {
1367 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1368 DEBUG(1, ("nt_key:\n"));
1369 dump_data(1, nt_key, 16);
1370 DEBUG(1, ("expected:\n"));
1371 dump_data(1, (const char *)session_key.data, session_key.length);
1378 * Test the NTLM response only, but in the LM field.
1381 static BOOL test_ntlm_in_lm(void)
1386 DATA_BLOB nt_response = data_blob(NULL, 24);
1391 DATA_BLOB chall = get_challenge();
1394 ZERO_STRUCT(nt_key);
1396 flags |= WBFLAG_PAM_LMKEY;
1397 flags |= WBFLAG_PAM_NTKEY;
1399 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1401 E_deshash(opt_password, lm_hash);
1403 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1413 data_blob_free(&nt_response);
1415 if (!NT_STATUS_IS_OK(nt_status)) {
1416 d_printf("%s (0x%x)\n",
1418 NT_STATUS_V(nt_status));
1419 SAFE_FREE(error_string);
1423 if (memcmp(lm_hash, lm_key,
1424 sizeof(lm_key)) != 0) {
1425 DEBUG(1, ("LM Key does not match expectations!\n"));
1426 DEBUG(1, ("lm_key:\n"));
1427 dump_data(1, (const char *)lm_key, 8);
1428 DEBUG(1, ("expected:\n"));
1429 dump_data(1, (const char *)lm_hash, 8);
1432 if (memcmp(lm_hash, nt_key, 8) != 0) {
1433 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
1434 DEBUG(1, ("nt_key:\n"));
1435 dump_data(1, (const char *)nt_key, 16);
1436 DEBUG(1, ("expected:\n"));
1437 dump_data(1, (const char *)lm_hash, 8);
1444 * Test the NTLM response only, but in the both the NT and LM fields.
1447 static BOOL test_ntlm_in_both(void)
1452 DATA_BLOB nt_response = data_blob(NULL, 24);
1453 DATA_BLOB session_key = data_blob(NULL, 16);
1459 DATA_BLOB chall = get_challenge();
1462 ZERO_STRUCT(lm_key);
1463 ZERO_STRUCT(nt_key);
1465 flags |= WBFLAG_PAM_LMKEY;
1466 flags |= WBFLAG_PAM_NTKEY;
1468 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1469 E_md4hash(opt_password, (unsigned char *)nt_hash);
1470 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1472 E_deshash(opt_password, (unsigned char *)lm_hash);
1474 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1480 (unsigned char *)lm_key,
1481 (unsigned char *)nt_key,
1484 data_blob_free(&nt_response);
1486 if (!NT_STATUS_IS_OK(nt_status)) {
1487 d_printf("%s (0x%x)\n",
1489 NT_STATUS_V(nt_status));
1490 SAFE_FREE(error_string);
1494 if (memcmp(lm_hash, lm_key,
1495 sizeof(lm_key)) != 0) {
1496 DEBUG(1, ("LM Key does not match expectations!\n"));
1497 DEBUG(1, ("lm_key:\n"));
1498 dump_data(1, lm_key, 8);
1499 DEBUG(1, ("expected:\n"));
1500 dump_data(1, lm_hash, 8);
1503 if (memcmp(session_key.data, nt_key,
1504 sizeof(nt_key)) != 0) {
1505 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1506 DEBUG(1, ("nt_key:\n"));
1507 dump_data(1, nt_key, 16);
1508 DEBUG(1, ("expected:\n"));
1509 dump_data(1, (const char *)session_key.data, session_key.length);
1518 * Test the NTLMv2 response only
1521 static BOOL test_ntlmv2(void)
1526 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1527 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1528 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1531 DATA_BLOB chall = get_challenge();
1534 ZERO_STRUCT(nt_key);
1536 flags |= WBFLAG_PAM_NTKEY;
1538 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1540 NULL, &ntlmv2_response,
1542 data_blob_free(&names_blob);
1545 data_blob_free(&names_blob);
1547 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1557 data_blob_free(&ntlmv2_response);
1559 if (!NT_STATUS_IS_OK(nt_status)) {
1560 d_printf("%s (0x%x)\n",
1562 NT_STATUS_V(nt_status));
1563 SAFE_FREE(error_string);
1567 if (memcmp(nt_session_key.data, nt_key,
1568 sizeof(nt_key)) != 0) {
1569 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1570 DEBUG(1, ("nt_key:\n"));
1571 dump_data(1, (const char *)nt_key, 16);
1572 DEBUG(1, ("expected:\n"));
1573 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1580 * Test the NTLMv2 and LMv2 responses
1583 static BOOL test_lmv2_ntlmv2(void)
1588 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1589 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1590 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1591 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1594 DATA_BLOB chall = get_challenge();
1597 ZERO_STRUCT(nt_key);
1599 flags |= WBFLAG_PAM_NTKEY;
1601 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1603 &lmv2_response, &ntlmv2_response,
1605 data_blob_free(&names_blob);
1608 data_blob_free(&names_blob);
1610 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1620 data_blob_free(&lmv2_response);
1621 data_blob_free(&ntlmv2_response);
1623 if (!NT_STATUS_IS_OK(nt_status)) {
1624 d_printf("%s (0x%x)\n",
1626 NT_STATUS_V(nt_status));
1627 SAFE_FREE(error_string);
1631 if (memcmp(nt_session_key.data, nt_key,
1632 sizeof(nt_key)) != 0) {
1633 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1634 DEBUG(1, ("nt_key:\n"));
1635 dump_data(1, (const char *)nt_key, 16);
1636 DEBUG(1, ("expected:\n"));
1637 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1644 * Test the LMv2 response only
1647 static BOOL test_lmv2(void)
1652 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1654 DATA_BLOB chall = get_challenge();
1657 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1659 &lmv2_response, NULL,
1664 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1674 data_blob_free(&lmv2_response);
1676 if (!NT_STATUS_IS_OK(nt_status)) {
1677 d_printf("%s (0x%x)\n",
1679 NT_STATUS_V(nt_status));
1680 SAFE_FREE(error_string);
1688 * Test the normal 'LM and NTLM' combination but deliberately break one
1691 static BOOL test_ntlm_broken(BOOL break_lm)
1696 DATA_BLOB lm_response = data_blob(NULL, 24);
1697 DATA_BLOB nt_response = data_blob(NULL, 24);
1698 DATA_BLOB session_key = data_blob(NULL, 16);
1704 DATA_BLOB chall = get_challenge();
1707 ZERO_STRUCT(lm_key);
1708 ZERO_STRUCT(nt_key);
1710 flags |= WBFLAG_PAM_LMKEY;
1711 flags |= WBFLAG_PAM_NTKEY;
1713 SMBencrypt(opt_password,chall.data,lm_response.data);
1714 E_deshash(opt_password, lm_hash);
1716 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1718 E_md4hash(opt_password, nt_hash);
1719 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1722 lm_response.data[0]++;
1724 nt_response.data[0]++;
1726 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1736 data_blob_free(&lm_response);
1738 if (!NT_STATUS_IS_OK(nt_status)) {
1739 d_printf("%s (0x%x)\n",
1741 NT_STATUS_V(nt_status));
1742 SAFE_FREE(error_string);
1746 if (memcmp(lm_hash, lm_key,
1747 sizeof(lm_key)) != 0) {
1748 DEBUG(1, ("LM Key does not match expectations!\n"));
1749 DEBUG(1, ("lm_key:\n"));
1750 dump_data(1, (const char *)lm_key, 8);
1751 DEBUG(1, ("expected:\n"));
1752 dump_data(1, (const char *)lm_hash, 8);
1755 if (memcmp(session_key.data, nt_key,
1756 sizeof(nt_key)) != 0) {
1757 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1758 DEBUG(1, ("nt_key:\n"));
1759 dump_data(1, (const char *)nt_key, 16);
1760 DEBUG(1, ("expected:\n"));
1761 dump_data(1, (const char *)session_key.data, session_key.length);
1767 static BOOL test_ntlm_lm_broken(void)
1769 return test_ntlm_broken(True);
1772 static BOOL test_ntlm_ntlm_broken(void)
1774 return test_ntlm_broken(False);
1777 static BOOL test_ntlmv2_broken(BOOL break_lmv2)
1782 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1783 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1784 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1785 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1788 DATA_BLOB chall = get_challenge();
1791 ZERO_STRUCT(nt_key);
1793 flags |= WBFLAG_PAM_NTKEY;
1795 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1797 &lmv2_response, &ntlmv2_response,
1799 data_blob_free(&names_blob);
1802 data_blob_free(&names_blob);
1804 /* Heh - this should break the appropriate password hash nicely! */
1807 lmv2_response.data[0]++;
1809 ntlmv2_response.data[0]++;
1811 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1821 data_blob_free(&lmv2_response);
1822 data_blob_free(&ntlmv2_response);
1824 if (!NT_STATUS_IS_OK(nt_status)) {
1825 d_printf("%s (0x%x)\n",
1827 NT_STATUS_V(nt_status));
1828 SAFE_FREE(error_string);
1835 static BOOL test_ntlmv2_lmv2_broken(void)
1837 return test_ntlmv2_broken(True);
1840 static BOOL test_ntlmv2_ntlmv2_broken(void)
1842 return test_ntlmv2_broken(False);
1857 check we get the correct session key in each case
1858 check what values we get for the LM session key
1867 {test_lm_ntlm, "LM and NTLM"},
1868 {test_ntlm, "NTLM"},
1869 {test_ntlm_in_lm, "NTLM in LM"},
1870 {test_ntlm_in_both, "NTLM in both"},
1871 {test_ntlmv2, "NTLMv2"},
1872 {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
1873 {test_lmv2, "LMv2"},
1874 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
1875 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
1876 {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
1877 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"}
1880 static BOOL diagnose_ntlm_auth(void)
1885 for (i=0; test_table[i].fn; i++) {
1886 if (!test_table[i].fn()) {
1887 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
1898 OPT_USERNAME = 1000,
1911 int main(int argc, const char **argv)
1914 static const char *helper_protocol;
1915 static int diagnostics;
1917 static const char *hex_challenge;
1918 static const char *hex_lm_response;
1919 static const char *hex_nt_response;
1923 size_t challenge_len;
1924 size_t lm_response_len;
1925 size_t nt_response_len;
1929 /* NOTE: DO NOT change this interface without considering the implications!
1930 This is an external interface, which other programs will use to interact
1934 /* We do not use single-letter command abbreviations, because they harm future
1935 interface stability. */
1937 struct poptOption long_options[] = {
1939 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1940 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1941 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1942 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1943 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1944 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1945 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1946 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1947 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1948 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
1949 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
1954 /* Samba client initialisation */
1958 /* Samba client initialisation */
1960 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
1961 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
1962 dyn_CONFIGFILE, strerror(errno));
1968 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1970 /* Parse command line options */
1973 poptPrintHelp(pc, stderr, 0);
1977 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1978 POPT_CONTEXT_KEEP_FIRST);
1980 while((opt = poptGetNextOpt(pc)) != -1) {
1983 challenge = smb_xmalloc((strlen(hex_challenge))/2+1);
1984 if ((challenge_len = strhex_to_str(challenge,
1985 strlen(hex_challenge),
1986 hex_challenge)) != 8) {
1987 x_fprintf(x_stderr, "hex decode of %s failed (only got %u bytes)!\n",
1988 hex_challenge, challenge_len);
1991 opt_challenge = data_blob(challenge, challenge_len);
1992 SAFE_FREE(challenge);
1995 lm_response = smb_xmalloc((strlen(hex_lm_response))/2+1);
1996 lm_response_len = strhex_to_str(lm_response,
1997 strlen(hex_lm_response),
1999 if (lm_response_len != 24) {
2000 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
2003 opt_lm_response = data_blob(lm_response, lm_response_len);
2004 SAFE_FREE(lm_response);
2007 nt_response = smb_xmalloc((strlen(hex_nt_response)+2)/2+1);
2008 nt_response_len = strhex_to_str(nt_response,
2009 strlen(hex_nt_response),
2011 if (nt_response_len < 24) {
2012 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
2015 opt_nt_response = data_blob(nt_response, nt_response_len);
2016 SAFE_FREE(nt_response);
2021 if (helper_protocol) {
2022 if (strcmp(helper_protocol, "squid-2.5-ntlmssp")== 0) {
2023 squid_stream(SQUID_2_5_NTLMSSP);
2024 } else if (strcmp(helper_protocol, "squid-2.5-basic")== 0) {
2025 squid_stream(SQUID_2_5_BASIC);
2026 } else if (strcmp(helper_protocol, "squid-2.4-basic")== 0) {
2027 squid_stream(SQUID_2_4_BASIC);
2028 } else if (strcmp(helper_protocol, "gss-spnego")== 0) {
2029 squid_stream(GSS_SPNEGO);
2030 } else if (strcmp(helper_protocol, "gss-spnego-client") == 0) {
2031 squid_stream(GSS_SPNEGO_CLIENT);
2033 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
2038 if (!opt_username) {
2039 x_fprintf(x_stderr, "username must be specified!\n\n");
2040 poptPrintHelp(pc, stderr, 0);
2044 if (opt_domain == NULL) {
2045 opt_domain = get_winbind_domain();
2048 if (opt_workstation == NULL) {
2049 opt_workstation = "";
2052 if (opt_challenge.length) {
2053 if (!check_auth_crap()) {
2059 if (!opt_password) {
2060 opt_password = getpass("password: ");
2064 if (!diagnose_ntlm_auth()) {
2070 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2071 if (!check_plaintext_auth(user, opt_password, True)) {
2078 poptFreeContext(pc);