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
32 enum stdio_helper_mode {
41 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
42 char *buf, int length);
44 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
45 char *buf, int length);
47 static void manage_squid_ntlmssp_request (enum stdio_helper_mode stdio_helper_mode,
48 char *buf, int length);
50 static void manage_gss_spnego_request (enum stdio_helper_mode stdio_helper_mode,
51 char *buf, int length);
53 static void manage_gss_spnego_client_request (enum stdio_helper_mode stdio_helper_mode,
54 char *buf, int length);
57 enum stdio_helper_mode mode;
59 stdio_helper_function fn;
60 } stdio_helper_protocols[] = {
61 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
62 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
63 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
64 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
65 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
66 { NUM_HELPER_MODES, NULL, NULL}
69 extern int winbindd_fd;
71 static const char *opt_username;
72 static const char *opt_domain;
73 static const char *opt_workstation;
74 static const char *opt_password;
75 static DATA_BLOB opt_challenge;
76 static DATA_BLOB opt_lm_response;
77 static DATA_BLOB opt_nt_response;
78 static int request_lm_key;
79 static int request_nt_key;
82 static char winbind_separator(void)
84 struct winbindd_response response;
91 ZERO_STRUCT(response);
93 /* Send off request */
95 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
97 d_printf("could not obtain winbind separator!\n");
101 sep = response.data.info.winbind_separator;
105 d_printf("winbind separator was NULL!\n");
112 static const char *get_winbind_domain(void)
114 struct winbindd_response response;
116 static fstring winbind_domain;
117 if (*winbind_domain) {
118 return winbind_domain;
121 ZERO_STRUCT(response);
123 /* Send off request */
125 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
126 NSS_STATUS_SUCCESS) {
127 DEBUG(0, ("could not obtain winbind domain name!\n"));
131 fstrcpy(winbind_domain, response.data.domain_name);
133 return winbind_domain;
137 static const char *get_winbind_netbios_name(void)
139 struct winbindd_response response;
141 static fstring winbind_netbios_name;
143 if (*winbind_netbios_name) {
144 return winbind_netbios_name;
147 ZERO_STRUCT(response);
149 /* Send off request */
151 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
152 NSS_STATUS_SUCCESS) {
153 DEBUG(0, ("could not obtain winbind netbios name!\n"));
157 fstrcpy(winbind_netbios_name, response.data.netbios_name);
159 return winbind_netbios_name;
163 /* Authenticate a user with a plaintext password */
165 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
167 struct winbindd_request request;
168 struct winbindd_response response;
171 /* Send off request */
173 ZERO_STRUCT(request);
174 ZERO_STRUCT(response);
176 fstrcpy(request.data.auth.user, user);
177 fstrcpy(request.data.auth.pass, pass);
179 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
181 /* Display response */
183 if (stdout_diagnostics) {
184 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
185 d_printf("Reading winbind reply failed! (0x01)\n");
188 d_printf("%s: %s (0x%x)\n",
189 response.data.auth.nt_status_string,
190 response.data.auth.error_string,
191 response.data.auth.nt_status);
193 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
194 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
197 DEBUG(3, ("%s: %s (0x%x)\n",
198 response.data.auth.nt_status_string,
199 response.data.auth.error_string,
200 response.data.auth.nt_status));
203 return (result == NSS_STATUS_SUCCESS);
206 /* authenticate a user with an encrypted username/password */
208 static NTSTATUS contact_winbind_auth_crap(const char *username,
210 const char *workstation,
211 const DATA_BLOB *challenge,
212 const DATA_BLOB *lm_response,
213 const DATA_BLOB *nt_response,
221 struct winbindd_request request;
222 struct winbindd_response response;
224 static uint8 zeros[16];
226 ZERO_STRUCT(request);
227 ZERO_STRUCT(response);
229 request.flags = flags;
231 if (push_utf8_fstring(request.data.auth_crap.user, username) == -1) {
232 *error_string = smb_xstrdup(
233 "unable to create utf8 string for username");
234 return NT_STATUS_UNSUCCESSFUL;
237 if (push_utf8_fstring(request.data.auth_crap.domain, domain) == -1) {
238 *error_string = smb_xstrdup(
239 "unable to create utf8 string for domain");
240 return NT_STATUS_UNSUCCESSFUL;
243 if (push_utf8_fstring(request.data.auth_crap.workstation,
244 workstation) == -1) {
245 *error_string = smb_xstrdup(
246 "unable to create utf8 string for workstation");
247 return NT_STATUS_UNSUCCESSFUL;
250 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
252 if (lm_response && lm_response->length) {
253 memcpy(request.data.auth_crap.lm_resp, lm_response->data, MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
254 request.data.auth_crap.lm_resp_len = lm_response->length;
257 if (nt_response && nt_response->length) {
258 memcpy(request.data.auth_crap.nt_resp, nt_response->data, MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
259 request.data.auth_crap.nt_resp_len = nt_response->length;
262 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
264 /* Display response */
266 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
267 nt_status = NT_STATUS_UNSUCCESSFUL;
269 *error_string = smb_xstrdup("Reading winbind reply failed!");
273 nt_status = (NT_STATUS(response.data.auth.nt_status));
274 if (!NT_STATUS_IS_OK(nt_status)) {
276 *error_string = smb_xstrdup(response.data.auth.error_string);
280 if ((flags & WBFLAG_PAM_LMKEY) && lm_key
281 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
282 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
283 memcpy(lm_key, response.data.auth.first_8_lm_hash,
284 sizeof(response.data.auth.first_8_lm_hash));
286 if ((flags & WBFLAG_PAM_NTKEY) && nt_key
287 && (memcmp(zeros, response.data.auth.nt_session_key,
288 sizeof(response.data.auth.nt_session_key)) != 0)) {
289 memcpy(nt_key, response.data.auth.nt_session_key,
290 sizeof(response.data.auth.nt_session_key));
295 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *nt_session_key, DATA_BLOB *lm_session_key)
297 static const char zeros[16];
303 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
304 ntlmssp_state->workstation,
305 &ntlmssp_state->chal,
306 &ntlmssp_state->lm_resp,
307 &ntlmssp_state->nt_resp,
308 WBFLAG_PAM_LMKEY | WBFLAG_PAM_NTKEY,
312 if (NT_STATUS_IS_OK(nt_status)) {
313 if (memcmp(lm_key, zeros, 8) != 0) {
314 *lm_session_key = data_blob(NULL, 16);
315 memcpy(lm_session_key->data, lm_key, 8);
316 memset(lm_session_key->data+8, '\0', 8);
319 if (memcmp(nt_key, zeros, 16) != 0) {
320 *nt_session_key = data_blob(nt_key, 16);
323 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
324 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
325 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation, error_string ? error_string : "unknown error (NULL)"));
330 static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mode,
331 char *buf, int length)
333 static NTLMSSP_STATE *ntlmssp_state = NULL;
334 DATA_BLOB request, reply;
337 if (strlen(buf) < 2) {
338 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
339 x_fprintf(x_stdout, "BH\n");
343 if (strncmp(buf, "YR", 2) == 0) {
345 ntlmssp_end(&ntlmssp_state);
346 } else if (strncmp(buf, "KK", 2) == 0) {
349 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
350 x_fprintf(x_stdout, "BH\n");
354 if (strlen(buf) > 3) {
355 request = base64_decode_data_blob(buf + 3);
357 request = data_blob(NULL, 0);
360 if (!ntlmssp_state) {
361 ntlmssp_server_start(&ntlmssp_state);
362 ntlmssp_state->check_password = winbind_pw_check;
363 ntlmssp_state->get_domain = get_winbind_domain;
364 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
367 DEBUG(10, ("got NTLMSSP packet:\n"));
368 dump_data(10, (const char *)request.data, request.length);
370 nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
372 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
373 char *reply_base64 = base64_encode_data_blob(reply);
374 x_fprintf(x_stdout, "TT %s\n", reply_base64);
375 SAFE_FREE(reply_base64);
376 data_blob_free(&reply);
377 DEBUG(10, ("NTLMSSP challenge\n"));
378 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
379 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
380 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
381 } else if (!NT_STATUS_IS_OK(nt_status)) {
382 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
383 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
385 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
386 DEBUG(10, ("NTLMSSP OK!\n"));
389 data_blob_free(&request);
392 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
393 char *buf, int length)
398 pass=memchr(buf,' ',length);
400 DEBUG(2, ("Password not found. Denying access\n"));
401 x_fprintf(x_stderr, "ERR\n");
407 if (stdio_helper_mode == SQUID_2_5_BASIC) {
408 rfc1738_unescape(user);
409 rfc1738_unescape(pass);
412 if (check_plaintext_auth(user, pass, False)) {
413 x_fprintf(x_stdout, "OK\n");
415 x_fprintf(x_stdout, "ERR\n");
419 static void offer_gss_spnego_mechs(void) {
427 pstring myname_lower;
431 pstrcpy(myname_lower, global_myname());
432 strlower_m(myname_lower);
434 pstr_sprintf(principal, "%s$@%s", myname_lower, lp_realm());
436 /* Server negTokenInit (mech offerings) */
437 spnego.type = SPNEGO_NEG_TOKEN_INIT;
438 spnego.negTokenInit.mechTypes = smb_xmalloc(sizeof(char *) * 3);
440 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
441 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
442 spnego.negTokenInit.mechTypes[2] = NULL;
444 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
445 spnego.negTokenInit.mechTypes[1] = NULL;
449 spnego.negTokenInit.mechListMIC = data_blob(principal,
452 len = write_spnego_data(&token, &spnego);
453 free_spnego_data(&spnego);
456 DEBUG(1, ("Could not write SPNEGO data blob\n"));
457 x_fprintf(x_stdout, "BH\n");
461 reply_base64 = base64_encode_data_blob(token);
462 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
464 SAFE_FREE(reply_base64);
465 data_blob_free(&token);
466 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
470 static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode,
471 char *buf, int length)
473 static NTLMSSP_STATE *ntlmssp_state = NULL;
474 SPNEGO_DATA request, response;
482 const char *reply_code;
484 pstring reply_argument;
486 if (strlen(buf) < 2) {
488 if (ntlmssp_state != NULL) {
489 DEBUG(1, ("Request for initial SPNEGO request where "
490 "we already have a state\n"));
491 x_fprintf(x_stdout, "BH\n");
495 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
496 x_fprintf(x_stdout, "BH\n");
500 if ( (strlen(buf) == 2) && (strcmp(buf, "YR") == 0) ) {
502 /* Initial request, get the negTokenInit offering
505 offer_gss_spnego_mechs();
509 /* All subsequent requests are "KK" (Knock, Knock ;)) and have
510 a blob. This might be negTokenInit or negTokenTarg */
512 if ( (strlen(buf) <= 3) || (strncmp(buf, "KK", 2) != 0) ) {
513 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
514 x_fprintf(x_stdout, "BH\n");
518 token = base64_decode_data_blob(buf + 3);
519 len = read_spnego_data(token, &request);
520 data_blob_free(&token);
523 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
524 x_fprintf(x_stdout, "BH\n");
528 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
530 /* Second request from Client. This is where the
531 client offers its mechanism to use. */
533 if ( (request.negTokenInit.mechTypes == NULL) ||
534 (request.negTokenInit.mechTypes[0] == NULL) ) {
535 DEBUG(1, ("Client did not offer any mechanism"));
536 x_fprintf(x_stdout, "BH\n");
540 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
542 if ( request.negTokenInit.mechToken.data == NULL ) {
543 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
544 x_fprintf(x_stdout, "BH\n");
548 if ( ntlmssp_state != NULL ) {
549 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
550 "already got one\n"));
551 x_fprintf(x_stdout, "BH\n");
552 ntlmssp_end(&ntlmssp_state);
556 ntlmssp_server_start(&ntlmssp_state);
557 ntlmssp_state->check_password = winbind_pw_check;
558 ntlmssp_state->get_domain = get_winbind_domain;
559 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
561 DEBUG(10, ("got NTLMSSP packet:\n"));
562 dump_data(10, (const char *)request.negTokenInit.mechToken.data,
563 request.negTokenInit.mechToken.length);
565 response.type = SPNEGO_NEG_TOKEN_TARG;
566 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
567 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
569 status = ntlmssp_update(ntlmssp_state,
570 request.negTokenInit.mechToken,
571 &response.negTokenTarg.responseToken);
575 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
580 DATA_BLOB session_key;
582 if ( request.negTokenInit.mechToken.data == NULL ) {
583 DEBUG(1, ("Client did not provide Kerberos data\n"));
584 x_fprintf(x_stdout, "BH\n");
588 response.type = SPNEGO_NEG_TOKEN_TARG;
589 response.negTokenTarg.supportedMech = strdup(OID_KERBEROS5_OLD);
590 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
591 response.negTokenTarg.responseToken = data_blob(NULL, 0);
593 status = ads_verify_ticket(lp_realm(),
594 &request.negTokenInit.mechToken,
595 &principal, &auth_data, &ap_rep,
598 /* Now in "principal" we have the name we are
601 if (NT_STATUS_IS_OK(status)) {
603 domain = strchr(principal, '@');
605 if (domain == NULL) {
606 DEBUG(1, ("Did not get a valid principal "
607 "from ads_verify_ticket\n"));
608 x_fprintf(x_stdout, "BH\n");
613 domain = strdup(domain);
614 user = strdup(principal);
616 data_blob_free(&ap_rep);
617 data_blob_free(&auth_data);
619 SAFE_FREE(principal);
626 if ( (request.negTokenTarg.supportedMech == NULL) ||
627 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
628 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
629 is the only one we support that sends this stuff */
630 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
631 request.negTokenTarg.supportedMech));
632 x_fprintf(x_stdout, "BH\n");
636 if (request.negTokenTarg.responseToken.data == NULL) {
637 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
638 x_fprintf(x_stdout, "BH\n");
642 status = ntlmssp_update(ntlmssp_state,
643 request.negTokenTarg.responseToken,
644 &response.negTokenTarg.responseToken);
646 response.type = SPNEGO_NEG_TOKEN_TARG;
647 response.negTokenTarg.supportedMech = strdup(OID_NTLMSSP);
648 response.negTokenTarg.mechListMIC = data_blob(NULL, 0);
650 if (NT_STATUS_IS_OK(status)) {
651 user = strdup(ntlmssp_state->user);
652 domain = strdup(ntlmssp_state->domain);
653 ntlmssp_end(&ntlmssp_state);
657 free_spnego_data(&request);
659 if (NT_STATUS_IS_OK(status)) {
660 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
662 pstr_sprintf(reply_argument, "%s\\%s", domain, user);
663 } else if (NT_STATUS_EQUAL(status,
664 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
665 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
667 pstr_sprintf(reply_argument, "*");
669 response.negTokenTarg.negResult = SPNEGO_REJECT;
671 pstrcpy(reply_argument, nt_errstr(status));
677 len = write_spnego_data(&token, &response);
678 free_spnego_data(&response);
681 DEBUG(1, ("Could not write SPNEGO data blob\n"));
682 x_fprintf(x_stdout, "BH\n");
686 reply_base64 = base64_encode_data_blob(token);
688 x_fprintf(x_stdout, "%s %s %s\n",
689 reply_code, reply_base64, reply_argument);
691 SAFE_FREE(reply_base64);
692 data_blob_free(&token);
697 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
699 static BOOL manage_client_ntlmssp_init(SPNEGO_DATA spnego)
702 DATA_BLOB null_blob = data_blob(NULL, 0);
704 char *to_server_base64;
705 const char *my_mechs[] = {OID_NTLMSSP, NULL};
707 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
709 if (client_ntlmssp_state != NULL) {
710 DEBUG(1, ("Request for initial SPNEGO request where "
711 "we already have a state\n"));
715 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
716 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
720 if (opt_password == NULL) {
722 /* Request a password from the calling process. After
723 sending it, the calling process should retry with
726 DEBUG(10, ("Requesting password\n"));
727 x_fprintf(x_stdout, "PW\n");
731 status = ntlmssp_client_start(&client_ntlmssp_state);
733 if (!NT_STATUS_IS_OK(status)) {
734 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
736 ntlmssp_end(&client_ntlmssp_state);
740 status = ntlmssp_set_username(client_ntlmssp_state, opt_username);
742 if (!NT_STATUS_IS_OK(status)) {
743 DEBUG(1, ("Could not set username: %s\n",
745 ntlmssp_end(&client_ntlmssp_state);
749 status = ntlmssp_set_domain(client_ntlmssp_state, opt_domain);
751 if (!NT_STATUS_IS_OK(status)) {
752 DEBUG(1, ("Could not set domain: %s\n",
754 ntlmssp_end(&client_ntlmssp_state);
758 status = ntlmssp_set_password(client_ntlmssp_state, opt_password);
760 if (!NT_STATUS_IS_OK(status)) {
761 DEBUG(1, ("Could not set password: %s\n",
763 ntlmssp_end(&client_ntlmssp_state);
767 spnego.type = SPNEGO_NEG_TOKEN_INIT;
768 spnego.negTokenInit.mechTypes = my_mechs;
769 spnego.negTokenInit.reqFlags = 0;
770 spnego.negTokenInit.mechListMIC = null_blob;
772 status = ntlmssp_update(client_ntlmssp_state, null_blob,
773 &spnego.negTokenInit.mechToken);
775 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
776 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED, got: %s\n",
778 ntlmssp_end(&client_ntlmssp_state);
782 write_spnego_data(&to_server, &spnego);
783 data_blob_free(&spnego.negTokenInit.mechToken);
785 to_server_base64 = base64_encode_data_blob(to_server);
786 data_blob_free(&to_server);
787 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
788 SAFE_FREE(to_server_base64);
792 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
795 DATA_BLOB null_blob = data_blob(NULL, 0);
798 char *to_server_base64;
800 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
802 if (client_ntlmssp_state == NULL) {
803 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
804 x_fprintf(x_stdout, "BH\n");
805 ntlmssp_end(&client_ntlmssp_state);
809 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
810 x_fprintf(x_stdout, "NA\n");
811 ntlmssp_end(&client_ntlmssp_state);
815 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
816 x_fprintf(x_stdout, "AF\n");
817 ntlmssp_end(&client_ntlmssp_state);
821 status = ntlmssp_update(client_ntlmssp_state,
822 spnego.negTokenTarg.responseToken,
825 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
826 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
827 "ntlmssp_client_update, got: %s\n",
829 x_fprintf(x_stdout, "BH\n");
830 data_blob_free(&request);
831 ntlmssp_end(&client_ntlmssp_state);
835 spnego.type = SPNEGO_NEG_TOKEN_TARG;
836 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
837 spnego.negTokenTarg.supportedMech = OID_NTLMSSP;
838 spnego.negTokenTarg.responseToken = request;
839 spnego.negTokenTarg.mechListMIC = null_blob;
841 write_spnego_data(&to_server, &spnego);
842 data_blob_free(&request);
844 to_server_base64 = base64_encode_data_blob(to_server);
845 data_blob_free(&to_server);
846 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
847 SAFE_FREE(to_server_base64);
853 static BOOL manage_client_krb5_init(SPNEGO_DATA spnego)
856 DATA_BLOB tkt, to_server;
857 DATA_BLOB session_key_krb5;
861 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
864 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
865 (spnego.negTokenInit.mechListMIC.length == 0) ) {
866 DEBUG(1, ("Did not get a principal for krb5\n"));
870 principal = malloc(spnego.negTokenInit.mechListMIC.length+1);
872 if (principal == NULL) {
873 DEBUG(1, ("Could not malloc principal\n"));
877 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
878 spnego.negTokenInit.mechListMIC.length);
879 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
881 tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
883 if (tkt.data == NULL) {
887 /* Let's try to first get the TGT, for that we need a
890 if (opt_password == NULL) {
891 DEBUG(10, ("Requesting password\n"));
892 x_fprintf(x_stdout, "PW\n");
896 pstr_sprintf(user, "%s@%s", opt_username, opt_domain);
898 if (kerberos_kinit_password(user, opt_password, 0) != 0) {
899 DEBUG(10, ("Requesting TGT failed\n"));
900 x_fprintf(x_stdout, "NA\n");
904 tkt = cli_krb5_get_ticket(principal, 0, &session_key_krb5);
907 data_blob_free(&session_key_krb5);
911 reply.type = SPNEGO_NEG_TOKEN_INIT;
912 reply.negTokenInit.mechTypes = my_mechs;
913 reply.negTokenInit.reqFlags = 0;
914 reply.negTokenInit.mechToken = tkt;
915 reply.negTokenInit.mechListMIC = data_blob(NULL, 0);
917 len = write_spnego_data(&to_server, &reply);
918 data_blob_free(&tkt);
921 DEBUG(1, ("Could not write SPNEGO data blob\n"));
925 reply_base64 = base64_encode_data_blob(to_server);
926 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
928 SAFE_FREE(reply_base64);
929 data_blob_free(&to_server);
930 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
934 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
936 switch (spnego.negTokenTarg.negResult) {
937 case SPNEGO_ACCEPT_INCOMPLETE:
938 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
939 x_fprintf(x_stdout, "BH\n");
941 case SPNEGO_ACCEPT_COMPLETED:
942 DEBUG(10, ("Accept completed\n"));
943 x_fprintf(x_stdout, "AF\n");
946 DEBUG(10, ("Rejected\n"));
947 x_fprintf(x_stdout, "NA\n");
950 DEBUG(1, ("Got an invalid negTokenTarg\n"));
951 x_fprintf(x_stdout, "AF\n");
957 static void manage_gss_spnego_client_request(enum stdio_helper_mode stdio_helper_mode,
958 char *buf, int length)
964 if (strlen(buf) <= 3) {
965 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
966 x_fprintf(x_stdout, "BH\n");
970 request = base64_decode_data_blob(buf+3);
972 if (strncmp(buf, "PW ", 3) == 0) {
974 /* We asked for a password and obviously got it :-) */
976 opt_password = strndup((const char *)request.data, request.length);
978 if (opt_password == NULL) {
979 DEBUG(1, ("Out of memory\n"));
980 x_fprintf(x_stdout, "BH\n");
981 data_blob_free(&request);
985 x_fprintf(x_stdout, "OK\n");
986 data_blob_free(&request);
990 if ( (strncmp(buf, "TT ", 3) != 0) &&
991 (strncmp(buf, "AF ", 3) != 0) &&
992 (strncmp(buf, "NA ", 3) != 0) ) {
993 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
994 x_fprintf(x_stdout, "BH\n");
995 data_blob_free(&request);
999 /* So we got a server challenge to generate a SPNEGO
1000 client-to-server request... */
1002 len = read_spnego_data(request, &spnego);
1003 data_blob_free(&request);
1006 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1007 x_fprintf(x_stdout, "BH\n");
1011 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1013 /* The server offers a list of mechanisms */
1015 const char **mechType = spnego.negTokenInit.mechTypes;
1017 while (*mechType != NULL) {
1020 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1021 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1022 if (manage_client_krb5_init(spnego))
1027 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1028 if (manage_client_ntlmssp_init(spnego))
1035 DEBUG(1, ("Server offered no compatible mechanism\n"));
1036 x_fprintf(x_stdout, "BH\n");
1040 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1042 if (spnego.negTokenTarg.supportedMech == NULL) {
1043 /* On accept/reject Windows does not send the
1044 mechanism anymore. Handle that here and
1045 shut down the mechanisms. */
1047 switch (spnego.negTokenTarg.negResult) {
1048 case SPNEGO_ACCEPT_COMPLETED:
1049 x_fprintf(x_stdout, "AF\n");
1052 x_fprintf(x_stdout, "NA\n");
1055 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1056 "unknown negResult: %d\n",
1057 spnego.negTokenTarg.negResult));
1058 x_fprintf(x_stdout, "BH\n");
1061 ntlmssp_end(&client_ntlmssp_state);
1065 if (strcmp(spnego.negTokenTarg.supportedMech,
1066 OID_NTLMSSP) == 0) {
1067 manage_client_ntlmssp_targ(spnego);
1072 if (strcmp(spnego.negTokenTarg.supportedMech,
1073 OID_KERBEROS5_OLD) == 0) {
1074 manage_client_krb5_targ(spnego);
1081 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1082 x_fprintf(x_stdout, "BH\n");
1086 free_spnego_data(&spnego);
1090 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn)
1092 char buf[SQUID_BUFFER_SIZE+1];
1097 /* this is not a typo - x_fgets doesn't work too well under squid */
1098 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
1099 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
1100 strerror(ferror(stdin))));
1101 exit(1); /* BIIG buffer */
1104 c=memchr(buf,'\n',sizeof(buf)-1);
1113 DEBUG(2, ("Oversized message\n"));
1114 x_fprintf(x_stderr, "ERR\n");
1119 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
1121 if (buf[0] == '\0') {
1122 DEBUG(2, ("Invalid Request\n"));
1123 x_fprintf(x_stderr, "ERR\n");
1127 fn(helper_mode, buf, length);
1131 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
1132 /* initialize FDescs */
1133 x_setbuf(x_stdout, NULL);
1134 x_setbuf(x_stderr, NULL);
1136 manage_squid_request(stdio_mode, fn);
1141 /* Authenticate a user with a challenge/response */
1143 static BOOL check_auth_crap(void)
1152 static uint8 zeros[16];
1154 x_setbuf(x_stdout, NULL);
1157 flags |= WBFLAG_PAM_LMKEY;
1160 flags |= WBFLAG_PAM_NTKEY;
1162 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1168 (unsigned char *)lm_key,
1169 (unsigned char *)nt_key,
1172 if (!NT_STATUS_IS_OK(nt_status)) {
1173 x_fprintf(x_stdout, "%s (0x%x)\n",
1175 NT_STATUS_V(nt_status));
1176 SAFE_FREE(error_string);
1181 && (memcmp(zeros, lm_key,
1182 sizeof(lm_key)) != 0)) {
1183 hex_encode((const unsigned char *)lm_key,
1186 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
1187 SAFE_FREE(hex_lm_key);
1190 && (memcmp(zeros, nt_key,
1191 sizeof(nt_key)) != 0)) {
1192 hex_encode((const unsigned char *)nt_key,
1195 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_nt_key);
1196 SAFE_FREE(hex_nt_key);
1203 Authenticate a user with a challenge/response, checking session key
1204 and valid authentication types
1207 static DATA_BLOB get_challenge(void)
1209 static DATA_BLOB chal;
1210 if (opt_challenge.length)
1211 return opt_challenge;
1213 chal = data_blob(NULL, 8);
1215 generate_random_buffer(chal.data, chal.length, False);
1220 * Test LM authentication, no NT response supplied
1223 static BOOL test_lm(void)
1227 DATA_BLOB lm_response = data_blob(NULL, 24);
1232 DATA_BLOB chall = get_challenge();
1235 ZERO_STRUCT(lm_key);
1236 ZERO_STRUCT(nt_key);
1238 flags |= WBFLAG_PAM_LMKEY;
1239 flags |= WBFLAG_PAM_NTKEY;
1241 SMBencrypt(opt_password, chall.data, lm_response.data);
1242 E_deshash(opt_password, lm_hash);
1244 nt_status = contact_winbind_auth_crap(opt_username, opt_domain, opt_workstation,
1253 data_blob_free(&lm_response);
1255 if (!NT_STATUS_IS_OK(nt_status)) {
1256 d_printf("%s (0x%x)\n",
1258 NT_STATUS_V(nt_status));
1262 if (memcmp(lm_hash, lm_key,
1263 sizeof(lm_key)) != 0) {
1264 DEBUG(1, ("LM Key does not match expectations!\n"));
1265 DEBUG(1, ("lm_key:\n"));
1266 dump_data(1, (const char *)lm_key, 8);
1267 DEBUG(1, ("expected:\n"));
1268 dump_data(1, (const char *)lm_hash, 8);
1270 if (memcmp(lm_hash, nt_key, 8) != 0) {
1271 DEBUG(1, ("Session Key (first 8, lm hash) does not match expectations!\n"));
1272 DEBUG(1, ("nt_key:\n"));
1273 dump_data(1, (const char *)nt_key, 8);
1274 DEBUG(1, ("expected:\n"));
1275 dump_data(1, (const char *)lm_hash, 8);
1281 * Test the normal 'LM and NTLM' combination
1284 static BOOL test_lm_ntlm(void)
1289 DATA_BLOB lm_response = data_blob(NULL, 24);
1290 DATA_BLOB nt_response = data_blob(NULL, 24);
1291 DATA_BLOB session_key = data_blob(NULL, 16);
1297 DATA_BLOB chall = get_challenge();
1300 ZERO_STRUCT(lm_key);
1301 ZERO_STRUCT(nt_key);
1303 flags |= WBFLAG_PAM_LMKEY;
1304 flags |= WBFLAG_PAM_NTKEY;
1306 SMBencrypt(opt_password,chall.data,lm_response.data);
1307 E_deshash(opt_password, lm_hash);
1309 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1311 E_md4hash(opt_password, nt_hash);
1312 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1314 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1324 data_blob_free(&lm_response);
1326 if (!NT_STATUS_IS_OK(nt_status)) {
1327 d_printf("%s (0x%x)\n",
1329 NT_STATUS_V(nt_status));
1330 SAFE_FREE(error_string);
1334 if (memcmp(lm_hash, lm_key,
1335 sizeof(lm_key)) != 0) {
1336 DEBUG(1, ("LM Key does not match expectations!\n"));
1337 DEBUG(1, ("lm_key:\n"));
1338 dump_data(1, (const char *)lm_key, 8);
1339 DEBUG(1, ("expected:\n"));
1340 dump_data(1, (const char *)lm_hash, 8);
1343 if (memcmp(session_key.data, nt_key,
1344 sizeof(nt_key)) != 0) {
1345 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1346 DEBUG(1, ("nt_key:\n"));
1347 dump_data(1, (const char *)nt_key, 16);
1348 DEBUG(1, ("expected:\n"));
1349 dump_data(1, (const char *)session_key.data, session_key.length);
1356 * Test the NTLM response only, no LM.
1359 static BOOL test_ntlm(void)
1364 DATA_BLOB nt_response = data_blob(NULL, 24);
1365 DATA_BLOB session_key = data_blob(NULL, 16);
1371 DATA_BLOB chall = get_challenge();
1374 ZERO_STRUCT(lm_key);
1375 ZERO_STRUCT(nt_key);
1377 flags |= WBFLAG_PAM_LMKEY;
1378 flags |= WBFLAG_PAM_NTKEY;
1380 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1381 E_md4hash(opt_password, (unsigned char *)nt_hash);
1382 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1384 E_deshash(opt_password, (unsigned char *)lm_hash);
1386 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1392 (unsigned char *)lm_key,
1393 (unsigned char *)nt_key,
1396 data_blob_free(&nt_response);
1398 if (!NT_STATUS_IS_OK(nt_status)) {
1399 d_printf("%s (0x%x)\n",
1401 NT_STATUS_V(nt_status));
1402 SAFE_FREE(error_string);
1406 if (memcmp(lm_hash, lm_key,
1407 sizeof(lm_key)) != 0) {
1408 DEBUG(1, ("LM Key does not match expectations!\n"));
1409 DEBUG(1, ("lm_key:\n"));
1410 dump_data(1, lm_key, 8);
1411 DEBUG(1, ("expected:\n"));
1412 dump_data(1, lm_hash, 8);
1415 if (memcmp(session_key.data, nt_key,
1416 sizeof(nt_key)) != 0) {
1417 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1418 DEBUG(1, ("nt_key:\n"));
1419 dump_data(1, nt_key, 16);
1420 DEBUG(1, ("expected:\n"));
1421 dump_data(1, (const char *)session_key.data, session_key.length);
1428 * Test the NTLM response only, but in the LM field.
1431 static BOOL test_ntlm_in_lm(void)
1436 DATA_BLOB nt_response = data_blob(NULL, 24);
1441 DATA_BLOB chall = get_challenge();
1444 ZERO_STRUCT(nt_key);
1446 flags |= WBFLAG_PAM_LMKEY;
1447 flags |= WBFLAG_PAM_NTKEY;
1449 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1451 E_deshash(opt_password, lm_hash);
1453 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1463 data_blob_free(&nt_response);
1465 if (!NT_STATUS_IS_OK(nt_status)) {
1466 d_printf("%s (0x%x)\n",
1468 NT_STATUS_V(nt_status));
1469 SAFE_FREE(error_string);
1473 if (memcmp(lm_hash, lm_key,
1474 sizeof(lm_key)) != 0) {
1475 DEBUG(1, ("LM Key does not match expectations!\n"));
1476 DEBUG(1, ("lm_key:\n"));
1477 dump_data(1, (const char *)lm_key, 8);
1478 DEBUG(1, ("expected:\n"));
1479 dump_data(1, (const char *)lm_hash, 8);
1482 if (memcmp(lm_hash, nt_key, 8) != 0) {
1483 DEBUG(1, ("Session Key (first 8 lm hash) does not match expectations!\n"));
1484 DEBUG(1, ("nt_key:\n"));
1485 dump_data(1, (const char *)nt_key, 16);
1486 DEBUG(1, ("expected:\n"));
1487 dump_data(1, (const char *)lm_hash, 8);
1494 * Test the NTLM response only, but in the both the NT and LM fields.
1497 static BOOL test_ntlm_in_both(void)
1502 DATA_BLOB nt_response = data_blob(NULL, 24);
1503 DATA_BLOB session_key = data_blob(NULL, 16);
1509 DATA_BLOB chall = get_challenge();
1512 ZERO_STRUCT(lm_key);
1513 ZERO_STRUCT(nt_key);
1515 flags |= WBFLAG_PAM_LMKEY;
1516 flags |= WBFLAG_PAM_NTKEY;
1518 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1519 E_md4hash(opt_password, (unsigned char *)nt_hash);
1520 SMBsesskeygen_ntv1((const unsigned char *)nt_hash, NULL, session_key.data);
1522 E_deshash(opt_password, (unsigned char *)lm_hash);
1524 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1530 (unsigned char *)lm_key,
1531 (unsigned char *)nt_key,
1534 data_blob_free(&nt_response);
1536 if (!NT_STATUS_IS_OK(nt_status)) {
1537 d_printf("%s (0x%x)\n",
1539 NT_STATUS_V(nt_status));
1540 SAFE_FREE(error_string);
1544 if (memcmp(lm_hash, lm_key,
1545 sizeof(lm_key)) != 0) {
1546 DEBUG(1, ("LM Key does not match expectations!\n"));
1547 DEBUG(1, ("lm_key:\n"));
1548 dump_data(1, lm_key, 8);
1549 DEBUG(1, ("expected:\n"));
1550 dump_data(1, lm_hash, 8);
1553 if (memcmp(session_key.data, nt_key,
1554 sizeof(nt_key)) != 0) {
1555 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1556 DEBUG(1, ("nt_key:\n"));
1557 dump_data(1, nt_key, 16);
1558 DEBUG(1, ("expected:\n"));
1559 dump_data(1, (const char *)session_key.data, session_key.length);
1568 * Test the NTLMv2 response only
1571 static BOOL test_ntlmv2(void)
1576 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1577 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1578 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1581 DATA_BLOB chall = get_challenge();
1584 ZERO_STRUCT(nt_key);
1586 flags |= WBFLAG_PAM_NTKEY;
1588 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1590 NULL, &ntlmv2_response,
1592 data_blob_free(&names_blob);
1595 data_blob_free(&names_blob);
1597 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1607 data_blob_free(&ntlmv2_response);
1609 if (!NT_STATUS_IS_OK(nt_status)) {
1610 d_printf("%s (0x%x)\n",
1612 NT_STATUS_V(nt_status));
1613 SAFE_FREE(error_string);
1617 if (memcmp(nt_session_key.data, nt_key,
1618 sizeof(nt_key)) != 0) {
1619 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1620 DEBUG(1, ("nt_key:\n"));
1621 dump_data(1, (const char *)nt_key, 16);
1622 DEBUG(1, ("expected:\n"));
1623 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1630 * Test the NTLMv2 and LMv2 responses
1633 static BOOL test_lmv2_ntlmv2(void)
1638 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1639 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1640 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1641 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1644 DATA_BLOB chall = get_challenge();
1647 ZERO_STRUCT(nt_key);
1649 flags |= WBFLAG_PAM_NTKEY;
1651 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1653 &lmv2_response, &ntlmv2_response,
1655 data_blob_free(&names_blob);
1658 data_blob_free(&names_blob);
1660 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1670 data_blob_free(&lmv2_response);
1671 data_blob_free(&ntlmv2_response);
1673 if (!NT_STATUS_IS_OK(nt_status)) {
1674 d_printf("%s (0x%x)\n",
1676 NT_STATUS_V(nt_status));
1677 SAFE_FREE(error_string);
1681 if (memcmp(nt_session_key.data, nt_key,
1682 sizeof(nt_key)) != 0) {
1683 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1684 DEBUG(1, ("nt_key:\n"));
1685 dump_data(1, (const char *)nt_key, 16);
1686 DEBUG(1, ("expected:\n"));
1687 dump_data(1, (const char *)nt_session_key.data, nt_session_key.length);
1694 * Test the LMv2 response only
1697 static BOOL test_lmv2(void)
1702 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1704 DATA_BLOB chall = get_challenge();
1707 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1709 &lmv2_response, NULL,
1714 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1724 data_blob_free(&lmv2_response);
1726 if (!NT_STATUS_IS_OK(nt_status)) {
1727 d_printf("%s (0x%x)\n",
1729 NT_STATUS_V(nt_status));
1730 SAFE_FREE(error_string);
1738 * Test the normal 'LM and NTLM' combination but deliberately break one
1741 static BOOL test_ntlm_broken(BOOL break_lm)
1746 DATA_BLOB lm_response = data_blob(NULL, 24);
1747 DATA_BLOB nt_response = data_blob(NULL, 24);
1748 DATA_BLOB session_key = data_blob(NULL, 16);
1754 DATA_BLOB chall = get_challenge();
1757 ZERO_STRUCT(lm_key);
1758 ZERO_STRUCT(nt_key);
1760 flags |= WBFLAG_PAM_LMKEY;
1761 flags |= WBFLAG_PAM_NTKEY;
1763 SMBencrypt(opt_password,chall.data,lm_response.data);
1764 E_deshash(opt_password, lm_hash);
1766 SMBNTencrypt(opt_password,chall.data,nt_response.data);
1768 E_md4hash(opt_password, nt_hash);
1769 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
1772 lm_response.data[0]++;
1774 nt_response.data[0]++;
1776 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1786 data_blob_free(&lm_response);
1788 if (!NT_STATUS_IS_OK(nt_status)) {
1789 d_printf("%s (0x%x)\n",
1791 NT_STATUS_V(nt_status));
1792 SAFE_FREE(error_string);
1796 if (memcmp(lm_hash, lm_key,
1797 sizeof(lm_key)) != 0) {
1798 DEBUG(1, ("LM Key does not match expectations!\n"));
1799 DEBUG(1, ("lm_key:\n"));
1800 dump_data(1, (const char *)lm_key, 8);
1801 DEBUG(1, ("expected:\n"));
1802 dump_data(1, (const char *)lm_hash, 8);
1805 if (memcmp(session_key.data, nt_key,
1806 sizeof(nt_key)) != 0) {
1807 DEBUG(1, ("NT Session Key does not match expectations!\n"));
1808 DEBUG(1, ("nt_key:\n"));
1809 dump_data(1, (const char *)nt_key, 16);
1810 DEBUG(1, ("expected:\n"));
1811 dump_data(1, (const char *)session_key.data, session_key.length);
1817 static BOOL test_ntlm_lm_broken(void)
1819 return test_ntlm_broken(True);
1822 static BOOL test_ntlm_ntlm_broken(void)
1824 return test_ntlm_broken(False);
1827 static BOOL test_ntlmv2_broken(BOOL break_lmv2)
1832 DATA_BLOB ntlmv2_response = data_blob(NULL, 0);
1833 DATA_BLOB lmv2_response = data_blob(NULL, 0);
1834 DATA_BLOB nt_session_key = data_blob(NULL, 0);
1835 DATA_BLOB names_blob = NTLMv2_generate_names_blob(get_winbind_netbios_name(), get_winbind_domain());
1838 DATA_BLOB chall = get_challenge();
1841 ZERO_STRUCT(nt_key);
1843 flags |= WBFLAG_PAM_NTKEY;
1845 if (!SMBNTLMv2encrypt(opt_username, opt_domain, opt_password, &chall,
1847 &lmv2_response, &ntlmv2_response,
1849 data_blob_free(&names_blob);
1852 data_blob_free(&names_blob);
1854 /* Heh - this should break the appropriate password hash nicely! */
1857 lmv2_response.data[0]++;
1859 ntlmv2_response.data[0]++;
1861 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
1871 data_blob_free(&lmv2_response);
1872 data_blob_free(&ntlmv2_response);
1874 if (!NT_STATUS_IS_OK(nt_status)) {
1875 d_printf("%s (0x%x)\n",
1877 NT_STATUS_V(nt_status));
1878 SAFE_FREE(error_string);
1885 static BOOL test_ntlmv2_lmv2_broken(void)
1887 return test_ntlmv2_broken(True);
1890 static BOOL test_ntlmv2_ntlmv2_broken(void)
1892 return test_ntlmv2_broken(False);
1907 check we get the correct session key in each case
1908 check what values we get for the LM session key
1917 {test_lm_ntlm, "LM and NTLM"},
1918 {test_ntlm, "NTLM"},
1919 {test_ntlm_in_lm, "NTLM in LM"},
1920 {test_ntlm_in_both, "NTLM in both"},
1921 {test_ntlmv2, "NTLMv2"},
1922 {test_lmv2_ntlmv2, "NTLMv2 and LMv2"},
1923 {test_lmv2, "LMv2"},
1924 {test_ntlmv2_lmv2_broken, "NTLMv2 and LMv2, LMv2 broken"},
1925 {test_ntlmv2_ntlmv2_broken, "NTLMv2 and LMv2, NTLMv2 broken"},
1926 {test_ntlm_lm_broken, "NTLM and LM, LM broken"},
1927 {test_ntlm_ntlm_broken, "NTLM and LM, NTLM broken"}
1930 static BOOL diagnose_ntlm_auth(void)
1935 for (i=0; test_table[i].fn; i++) {
1936 if (!test_table[i].fn()) {
1937 DEBUG(1, ("Test %s failed!\n", test_table[i].name));
1948 OPT_USERNAME = 1000,
1961 int main(int argc, const char **argv)
1964 static const char *helper_protocol;
1965 static int diagnostics;
1967 static const char *hex_challenge;
1968 static const char *hex_lm_response;
1969 static const char *hex_nt_response;
1973 size_t challenge_len;
1974 size_t lm_response_len;
1975 size_t nt_response_len;
1979 /* NOTE: DO NOT change this interface without considering the implications!
1980 This is an external interface, which other programs will use to interact
1984 /* We do not use single-letter command abbreviations, because they harm future
1985 interface stability. */
1987 struct poptOption long_options[] = {
1989 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1990 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
1991 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1992 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1993 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
1994 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
1995 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
1996 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1997 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
1998 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
1999 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2004 /* Samba client initialisation */
2008 /* Samba client initialisation */
2010 if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
2011 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
2012 dyn_CONFIGFILE, strerror(errno));
2018 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2020 /* Parse command line options */
2023 poptPrintHelp(pc, stderr, 0);
2027 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2028 POPT_CONTEXT_KEEP_FIRST);
2030 while((opt = poptGetNextOpt(pc)) != -1) {
2033 challenge = smb_xmalloc((strlen(hex_challenge))/2+1);
2034 if ((challenge_len = strhex_to_str(challenge,
2035 strlen(hex_challenge),
2036 hex_challenge)) != 8) {
2037 x_fprintf(x_stderr, "hex decode of %s failed (only got %lu bytes)!\n",
2038 hex_challenge, (unsigned long)challenge_len);
2041 opt_challenge = data_blob(challenge, challenge_len);
2042 SAFE_FREE(challenge);
2045 lm_response = smb_xmalloc((strlen(hex_lm_response))/2+1);
2046 lm_response_len = strhex_to_str(lm_response,
2047 strlen(hex_lm_response),
2049 if (lm_response_len != 24) {
2050 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_lm_response);
2053 opt_lm_response = data_blob(lm_response, lm_response_len);
2054 SAFE_FREE(lm_response);
2057 nt_response = smb_xmalloc((strlen(hex_nt_response)+2)/2+1);
2058 nt_response_len = strhex_to_str(nt_response,
2059 strlen(hex_nt_response),
2061 if (nt_response_len < 24) {
2062 x_fprintf(x_stderr, "hex decode of %s failed!\n", hex_nt_response);
2065 opt_nt_response = data_blob(nt_response, nt_response_len);
2066 SAFE_FREE(nt_response);
2071 if (helper_protocol) {
2073 for (i=0; i<NUM_HELPER_MODES; i++) {
2074 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2075 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2079 x_fprintf(x_stderr, "unknown helper protocol [%s]\n", helper_protocol);
2083 if (!opt_username) {
2084 x_fprintf(x_stderr, "username must be specified!\n\n");
2085 poptPrintHelp(pc, stderr, 0);
2089 if (opt_domain == NULL) {
2090 opt_domain = get_winbind_domain();
2093 if (opt_workstation == NULL) {
2094 opt_workstation = "";
2097 if (opt_challenge.length) {
2098 if (!check_auth_crap()) {
2104 if (!opt_password) {
2105 opt_password = getpass("password: ");
2109 if (!diagnose_ntlm_auth()) {
2115 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2116 if (!check_plaintext_auth(user, opt_password, True)) {
2123 poptFreeContext(pc);