2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2003
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
9 Copyright (C) Robert O'Callahan 2006 (added cached credential code).
10 Copyright (C) Kai Blin <kai@samba.org> 2008
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "utils/ntlm_auth.h"
30 #define DBGC_CLASS DBGC_WINBIND
32 #define INITIAL_BUFFER_SIZE 300
33 #define MAX_BUFFER_SIZE 630000
35 enum stdio_helper_mode {
43 NTLM_CHANGE_PASSWORD_1,
47 enum ntlm_auth_cli_state {
54 enum ntlm_auth_svr_state {
61 struct ntlm_auth_state {
63 enum stdio_helper_mode helper_mode;
64 enum ntlm_auth_cli_state cli_state;
65 enum ntlm_auth_svr_state svr_state;
66 struct ntlmssp_state *ntlmssp_state;
68 char *want_feature_list;
69 bool have_session_key;
70 DATA_BLOB session_key;
71 DATA_BLOB initial_message;
74 typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
77 static void manage_squid_basic_request (struct ntlm_auth_state *state,
78 char *buf, int length);
80 static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
81 char *buf, int length);
83 static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
84 char *buf, int length);
86 static void manage_gss_spnego_request (struct ntlm_auth_state *state,
87 char *buf, int length);
89 static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
90 char *buf, int length);
92 static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
93 char *buf, int length);
95 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
96 char *buf, int length);
99 enum stdio_helper_mode mode;
101 stdio_helper_function fn;
102 } stdio_helper_protocols[] = {
103 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
104 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
105 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
106 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
107 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
108 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
109 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
110 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
111 { NUM_HELPER_MODES, NULL, NULL}
114 extern int winbindd_fd;
116 const char *opt_username;
117 const char *opt_domain;
118 const char *opt_workstation;
119 const char *opt_password;
120 static DATA_BLOB opt_challenge;
121 static DATA_BLOB opt_lm_response;
122 static DATA_BLOB opt_nt_response;
123 static int request_lm_key;
124 static int request_user_session_key;
125 static int use_cached_creds;
127 static const char *require_membership_of;
128 static const char *require_membership_of_sid;
130 static char winbind_separator(void)
132 struct winbindd_response response;
139 ZERO_STRUCT(response);
141 /* Send off request */
143 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
144 NSS_STATUS_SUCCESS) {
145 d_printf("could not obtain winbind separator!\n");
146 return *lp_winbind_separator();
149 sep = response.data.info.winbind_separator;
153 d_printf("winbind separator was NULL!\n");
154 return *lp_winbind_separator();
160 const char *get_winbind_domain(void)
162 struct winbindd_response response;
164 static fstring winbind_domain;
165 if (*winbind_domain) {
166 return winbind_domain;
169 ZERO_STRUCT(response);
171 /* Send off request */
173 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
174 NSS_STATUS_SUCCESS) {
175 DEBUG(0, ("could not obtain winbind domain name!\n"));
176 return lp_workgroup();
179 fstrcpy(winbind_domain, response.data.domain_name);
181 return winbind_domain;
185 const char *get_winbind_netbios_name(void)
187 struct winbindd_response response;
189 static fstring winbind_netbios_name;
191 if (*winbind_netbios_name) {
192 return winbind_netbios_name;
195 ZERO_STRUCT(response);
197 /* Send off request */
199 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
200 NSS_STATUS_SUCCESS) {
201 DEBUG(0, ("could not obtain winbind netbios name!\n"));
202 return global_myname();
205 fstrcpy(winbind_netbios_name, response.data.netbios_name);
207 return winbind_netbios_name;
211 DATA_BLOB get_challenge(void)
213 static DATA_BLOB chal;
214 if (opt_challenge.length)
215 return opt_challenge;
217 chal = data_blob(NULL, 8);
219 generate_random_buffer(chal.data, chal.length);
223 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
224 form DOMAIN/user into a domain and a user */
226 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
230 char *p = strchr(domuser,winbind_separator());
237 fstrcpy(domain, domuser);
238 domain[PTR_DIFF(p, domuser)] = 0;
244 static bool get_require_membership_sid(void) {
245 struct winbindd_request request;
246 struct winbindd_response response;
248 if (!require_membership_of) {
252 if (require_membership_of_sid) {
256 /* Otherwise, ask winbindd for the name->sid request */
258 ZERO_STRUCT(request);
259 ZERO_STRUCT(response);
261 if (!parse_ntlm_auth_domain_user(require_membership_of,
262 request.data.name.dom_name,
263 request.data.name.name)) {
264 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
265 require_membership_of));
269 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
270 NSS_STATUS_SUCCESS) {
271 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
272 require_membership_of));
276 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
278 if (require_membership_of_sid)
283 /* Authenticate a user with a plaintext password */
285 static bool check_plaintext_auth(const char *user, const char *pass,
286 bool stdout_diagnostics)
288 struct winbindd_request request;
289 struct winbindd_response response;
292 if (!get_require_membership_sid()) {
296 /* Send off request */
298 ZERO_STRUCT(request);
299 ZERO_STRUCT(response);
301 fstrcpy(request.data.auth.user, user);
302 fstrcpy(request.data.auth.pass, pass);
303 if (require_membership_of_sid) {
304 strlcpy(request.data.auth.require_membership_of_sid,
305 require_membership_of_sid,
306 sizeof(request.data.auth.require_membership_of_sid));
309 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
311 /* Display response */
313 if (stdout_diagnostics) {
314 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
315 d_printf("Reading winbind reply failed! (0x01)\n");
318 d_printf("%s: %s (0x%x)\n",
319 response.data.auth.nt_status_string,
320 response.data.auth.error_string,
321 response.data.auth.nt_status);
323 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
324 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
327 DEBUG(3, ("%s: %s (0x%x)\n",
328 response.data.auth.nt_status_string,
329 response.data.auth.error_string,
330 response.data.auth.nt_status));
333 return (result == NSS_STATUS_SUCCESS);
336 /* authenticate a user with an encrypted username/password */
338 NTSTATUS contact_winbind_auth_crap(const char *username,
340 const char *workstation,
341 const DATA_BLOB *challenge,
342 const DATA_BLOB *lm_response,
343 const DATA_BLOB *nt_response,
346 uint8 user_session_key[16],
352 struct winbindd_request request;
353 struct winbindd_response response;
355 if (!get_require_membership_sid()) {
356 return NT_STATUS_INVALID_PARAMETER;
359 ZERO_STRUCT(request);
360 ZERO_STRUCT(response);
362 request.flags = flags;
364 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
366 if (require_membership_of_sid)
367 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
369 fstrcpy(request.data.auth_crap.user, username);
370 fstrcpy(request.data.auth_crap.domain, domain);
372 fstrcpy(request.data.auth_crap.workstation,
375 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
377 if (lm_response && lm_response->length) {
378 memcpy(request.data.auth_crap.lm_resp,
380 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
381 request.data.auth_crap.lm_resp_len = lm_response->length;
384 if (nt_response && nt_response->length) {
385 memcpy(request.data.auth_crap.nt_resp,
387 MIN(nt_response->length, sizeof(request.data.auth_crap.nt_resp)));
388 request.data.auth_crap.nt_resp_len = nt_response->length;
391 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
393 /* Display response */
395 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
396 nt_status = NT_STATUS_UNSUCCESSFUL;
398 *error_string = smb_xstrdup("Reading winbind reply failed!");
399 winbindd_free_response(&response);
403 nt_status = (NT_STATUS(response.data.auth.nt_status));
404 if (!NT_STATUS_IS_OK(nt_status)) {
406 *error_string = smb_xstrdup(response.data.auth.error_string);
407 winbindd_free_response(&response);
411 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
412 memcpy(lm_key, response.data.auth.first_8_lm_hash,
413 sizeof(response.data.auth.first_8_lm_hash));
415 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
416 memcpy(user_session_key, response.data.auth.user_session_key,
417 sizeof(response.data.auth.user_session_key));
420 if (flags & WBFLAG_PAM_UNIX_NAME) {
421 *unix_name = SMB_STRDUP((char *)response.extra_data.data);
423 winbindd_free_response(&response);
424 return NT_STATUS_NO_MEMORY;
428 winbindd_free_response(&response);
432 /* contact server to change user password using auth crap */
433 static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
435 const DATA_BLOB new_nt_pswd,
436 const DATA_BLOB old_nt_hash_enc,
437 const DATA_BLOB new_lm_pswd,
438 const DATA_BLOB old_lm_hash_enc,
443 struct winbindd_request request;
444 struct winbindd_response response;
446 if (!get_require_membership_sid())
449 *error_string = smb_xstrdup("Can't get membership sid.");
450 return NT_STATUS_INVALID_PARAMETER;
453 ZERO_STRUCT(request);
454 ZERO_STRUCT(response);
457 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
459 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
461 if(new_nt_pswd.length)
463 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
464 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
467 if(old_nt_hash_enc.length)
469 memcpy(request.data.chng_pswd_auth_crap.old_nt_hash_enc, old_nt_hash_enc.data, sizeof(request.data.chng_pswd_auth_crap.old_nt_hash_enc));
470 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
473 if(new_lm_pswd.length)
475 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
476 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
479 if(old_lm_hash_enc.length)
481 memcpy(request.data.chng_pswd_auth_crap.old_lm_hash_enc, old_lm_hash_enc.data, sizeof(request.data.chng_pswd_auth_crap.old_lm_hash_enc));
482 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
485 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
487 /* Display response */
489 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
491 nt_status = NT_STATUS_UNSUCCESSFUL;
493 *error_string = smb_xstrdup("Reading winbind reply failed!");
494 winbindd_free_response(&response);
498 nt_status = (NT_STATUS(response.data.auth.nt_status));
499 if (!NT_STATUS_IS_OK(nt_status))
502 *error_string = smb_xstrdup(response.data.auth.error_string);
503 winbindd_free_response(&response);
507 winbindd_free_response(&response);
512 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
514 static const char zeros[16] = { 0, };
518 uint8 user_sess_key[16];
521 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
522 ntlmssp_state->workstation,
523 &ntlmssp_state->chal,
524 &ntlmssp_state->lm_resp,
525 &ntlmssp_state->nt_resp,
526 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
527 lm_key, user_sess_key,
528 &error_string, &unix_name);
530 if (NT_STATUS_IS_OK(nt_status)) {
531 if (memcmp(lm_key, zeros, 8) != 0) {
532 *lm_session_key = data_blob(NULL, 16);
533 memcpy(lm_session_key->data, lm_key, 8);
534 memset(lm_session_key->data+8, '\0', 8);
537 if (memcmp(user_sess_key, zeros, 16) != 0) {
538 *user_session_key = data_blob(user_sess_key, 16);
540 ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state->mem_ctx, unix_name);
541 SAFE_FREE(unix_name);
543 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
544 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
545 ntlmssp_state->domain, ntlmssp_state->user,
546 ntlmssp_state->workstation,
547 error_string ? error_string : "unknown error (NULL)"));
548 ntlmssp_state->auth_context = NULL;
553 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
556 uint8 lm_pw[16], nt_pw[16];
558 nt_lm_owf_gen (opt_password, nt_pw, lm_pw);
560 nt_status = ntlm_password_check(ntlmssp_state->mem_ctx,
561 &ntlmssp_state->chal,
562 &ntlmssp_state->lm_resp,
563 &ntlmssp_state->nt_resp,
567 ntlmssp_state->domain,
568 lm_pw, nt_pw, user_session_key, lm_session_key);
570 if (NT_STATUS_IS_OK(nt_status)) {
571 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state->mem_ctx,
572 "%s%c%s", ntlmssp_state->domain,
573 *lp_winbind_separator(),
574 ntlmssp_state->user);
576 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
577 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
578 nt_errstr(nt_status)));
579 ntlmssp_state->auth_context = NULL;
584 static NTSTATUS ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE **client_ntlmssp_state)
587 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
588 status = NT_STATUS_UNSUCCESSFUL;
589 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
590 return NT_STATUS_INVALID_PARAMETER;
593 status = ntlmssp_client_start(client_ntlmssp_state);
595 if (!NT_STATUS_IS_OK(status)) {
596 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
598 ntlmssp_end(client_ntlmssp_state);
602 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
604 if (!NT_STATUS_IS_OK(status)) {
605 DEBUG(1, ("Could not set username: %s\n",
607 ntlmssp_end(client_ntlmssp_state);
611 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
613 if (!NT_STATUS_IS_OK(status)) {
614 DEBUG(1, ("Could not set domain: %s\n",
616 ntlmssp_end(client_ntlmssp_state);
621 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
623 if (!NT_STATUS_IS_OK(status)) {
624 DEBUG(1, ("Could not set password: %s\n",
626 ntlmssp_end(client_ntlmssp_state);
634 static NTSTATUS ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE **ntlmssp_state)
636 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
638 if (!NT_STATUS_IS_OK(status)) {
639 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
644 /* Have we been given a local password, or should we ask winbind? */
646 (*ntlmssp_state)->check_password = local_pw_check;
647 (*ntlmssp_state)->get_domain = lp_workgroup;
648 (*ntlmssp_state)->get_global_myname = global_myname;
650 (*ntlmssp_state)->check_password = winbind_pw_check;
651 (*ntlmssp_state)->get_domain = get_winbind_domain;
652 (*ntlmssp_state)->get_global_myname = get_winbind_netbios_name;
657 /*******************************************************************
658 Used by firefox to drive NTLM auth to IIS servers.
659 *******************************************************************/
661 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
664 struct winbindd_request wb_request;
665 struct winbindd_response wb_response;
668 /* get winbindd to do the ntlmssp step on our behalf */
669 ZERO_STRUCT(wb_request);
670 ZERO_STRUCT(wb_response);
672 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
673 "%s%c%s", opt_domain, winbind_separator(), opt_username);
674 wb_request.data.ccache_ntlm_auth.uid = geteuid();
675 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
676 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
677 wb_request.extra_len = initial_msg.length + challenge_msg.length;
679 if (wb_request.extra_len > 0) {
680 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
681 if (wb_request.extra_data.data == NULL) {
682 return NT_STATUS_NO_MEMORY;
685 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
686 memcpy(wb_request.extra_data.data + initial_msg.length,
687 challenge_msg.data, challenge_msg.length);
690 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
691 SAFE_FREE(wb_request.extra_data.data);
693 if (result != NSS_STATUS_SUCCESS) {
694 winbindd_free_response(&wb_response);
695 return NT_STATUS_UNSUCCESSFUL;
699 *reply = data_blob(wb_response.extra_data.data,
700 wb_response.data.ccache_ntlm_auth.auth_blob_len);
701 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
702 reply->data == NULL) {
703 winbindd_free_response(&wb_response);
704 return NT_STATUS_NO_MEMORY;
708 winbindd_free_response(&wb_response);
709 return NT_STATUS_MORE_PROCESSING_REQUIRED;
712 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
713 char *buf, int length)
715 DATA_BLOB request, reply;
718 if (strlen(buf) < 2) {
719 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
720 x_fprintf(x_stdout, "BH\n");
724 if (strlen(buf) > 3) {
725 if(strncmp(buf, "SF ", 3) == 0){
726 DEBUG(10, ("Setting flags to negotioate\n"));
727 TALLOC_FREE(state->want_feature_list);
728 state->want_feature_list = talloc_strdup(state->mem_ctx,
730 x_fprintf(x_stdout, "OK\n");
733 request = base64_decode_data_blob(buf + 3);
735 request = data_blob_null;
738 if ((strncmp(buf, "PW ", 3) == 0)) {
739 /* The calling application wants us to use a local password
740 * (rather than winbindd) */
742 opt_password = SMB_STRNDUP((const char *)request.data,
745 if (opt_password == NULL) {
746 DEBUG(1, ("Out of memory\n"));
747 x_fprintf(x_stdout, "BH\n");
748 data_blob_free(&request);
752 x_fprintf(x_stdout, "OK\n");
753 data_blob_free(&request);
757 if (strncmp(buf, "YR", 2) == 0) {
758 if (state->ntlmssp_state)
759 ntlmssp_end(&state->ntlmssp_state);
760 state->svr_state = SERVER_INITIAL;
761 } else if (strncmp(buf, "KK", 2) == 0) {
762 /* No special preprocessing required */
763 } else if (strncmp(buf, "GF", 2) == 0) {
764 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
766 if (state->svr_state == SERVER_FINISHED) {
767 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
770 x_fprintf(x_stdout, "BH\n");
772 data_blob_free(&request);
774 } else if (strncmp(buf, "GK", 2) == 0) {
775 DEBUG(10, ("Requested NTLMSSP session key\n"));
776 if(state->have_session_key) {
777 char *key64 = base64_encode_data_blob(state->mem_ctx,
779 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
782 x_fprintf(x_stdout, "BH\n");
785 data_blob_free(&request);
788 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
789 x_fprintf(x_stdout, "BH\n");
793 if (!state->ntlmssp_state) {
794 nt_status = ntlm_auth_start_ntlmssp_server(
795 &state->ntlmssp_state);
796 if (!NT_STATUS_IS_OK(nt_status)) {
797 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
800 ntlmssp_want_feature_list(state->ntlmssp_state,
801 state->want_feature_list);
804 DEBUG(10, ("got NTLMSSP packet:\n"));
805 dump_data(10, request.data, request.length);
807 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
809 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
810 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
812 x_fprintf(x_stdout, "TT %s\n", reply_base64);
813 TALLOC_FREE(reply_base64);
814 data_blob_free(&reply);
815 state->svr_state = SERVER_CHALLENGE;
816 DEBUG(10, ("NTLMSSP challenge\n"));
817 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
818 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
819 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
821 ntlmssp_end(&state->ntlmssp_state);
822 } else if (!NT_STATUS_IS_OK(nt_status)) {
823 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
824 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
826 x_fprintf(x_stdout, "AF %s\n",
827 (char *)state->ntlmssp_state->auth_context);
828 DEBUG(10, ("NTLMSSP OK!\n"));
830 if(state->have_session_key)
831 data_blob_free(&state->session_key);
832 state->session_key = data_blob(
833 state->ntlmssp_state->session_key.data,
834 state->ntlmssp_state->session_key.length);
835 state->neg_flags = state->ntlmssp_state->neg_flags;
836 state->have_session_key = true;
837 state->svr_state = SERVER_FINISHED;
840 data_blob_free(&request);
843 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
844 char *buf, int length)
846 DATA_BLOB request, reply;
849 if (!opt_username || !*opt_username) {
850 x_fprintf(x_stderr, "username must be specified!\n\n");
854 if (strlen(buf) < 2) {
855 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
856 x_fprintf(x_stdout, "BH\n");
860 if (strlen(buf) > 3) {
861 if(strncmp(buf, "SF ", 3) == 0) {
862 DEBUG(10, ("Looking for flags to negotiate\n"));
863 talloc_free(state->want_feature_list);
864 state->want_feature_list = talloc_strdup(state->mem_ctx,
866 x_fprintf(x_stdout, "OK\n");
869 request = base64_decode_data_blob(buf + 3);
871 request = data_blob_null;
874 if (strncmp(buf, "PW ", 3) == 0) {
875 /* We asked for a password and obviously got it :-) */
877 opt_password = SMB_STRNDUP((const char *)request.data,
880 if (opt_password == NULL) {
881 DEBUG(1, ("Out of memory\n"));
882 x_fprintf(x_stdout, "BH\n");
883 data_blob_free(&request);
887 x_fprintf(x_stdout, "OK\n");
888 data_blob_free(&request);
892 if (!state->ntlmssp_state && use_cached_creds) {
893 /* check whether cached credentials are usable. */
894 DATA_BLOB empty_blob = data_blob_null;
896 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
897 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
898 /* failed to use cached creds */
899 use_cached_creds = False;
903 if (opt_password == NULL && !use_cached_creds) {
904 /* Request a password from the calling process. After
905 sending it, the calling process should retry asking for the
908 DEBUG(10, ("Requesting password\n"));
909 x_fprintf(x_stdout, "PW\n");
913 if (strncmp(buf, "YR", 2) == 0) {
914 if (state->ntlmssp_state)
915 ntlmssp_end(&state->ntlmssp_state);
916 state->cli_state = CLIENT_INITIAL;
917 } else if (strncmp(buf, "TT", 2) == 0) {
918 /* No special preprocessing required */
919 } else if (strncmp(buf, "GF", 2) == 0) {
920 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
922 if(state->cli_state == CLIENT_FINISHED) {
923 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
926 x_fprintf(x_stdout, "BH\n");
929 data_blob_free(&request);
931 } else if (strncmp(buf, "GK", 2) == 0 ) {
932 DEBUG(10, ("Requested session key\n"));
934 if(state->cli_state == CLIENT_FINISHED) {
935 char *key64 = base64_encode_data_blob(state->mem_ctx,
937 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
941 x_fprintf(x_stdout, "BH\n");
944 data_blob_free(&request);
947 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
948 x_fprintf(x_stdout, "BH\n");
952 if (!state->ntlmssp_state) {
953 nt_status = ntlm_auth_start_ntlmssp_client(
954 &state->ntlmssp_state);
955 if (!NT_STATUS_IS_OK(nt_status)) {
956 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
959 ntlmssp_want_feature_list(state->ntlmssp_state,
960 state->want_feature_list);
961 state->initial_message = data_blob_null;
964 DEBUG(10, ("got NTLMSSP packet:\n"));
965 dump_data(10, request.data, request.length);
967 if (use_cached_creds && !opt_password &&
968 (state->cli_state == CLIENT_RESPONSE)) {
969 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
972 nt_status = ntlmssp_update(state->ntlmssp_state, request,
976 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
977 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
979 if (state->cli_state == CLIENT_INITIAL) {
980 x_fprintf(x_stdout, "YR %s\n", reply_base64);
981 state->initial_message = reply;
982 state->cli_state = CLIENT_RESPONSE;
984 x_fprintf(x_stdout, "KK %s\n", reply_base64);
985 data_blob_free(&reply);
987 TALLOC_FREE(reply_base64);
988 DEBUG(10, ("NTLMSSP challenge\n"));
989 } else if (NT_STATUS_IS_OK(nt_status)) {
990 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
992 x_fprintf(x_stdout, "AF %s\n", reply_base64);
993 TALLOC_FREE(reply_base64);
995 if(state->have_session_key)
996 data_blob_free(&state->session_key);
998 state->session_key = data_blob(
999 state->ntlmssp_state->session_key.data,
1000 state->ntlmssp_state->session_key.length);
1001 state->neg_flags = state->ntlmssp_state->neg_flags;
1002 state->have_session_key = true;
1004 DEBUG(10, ("NTLMSSP OK!\n"));
1005 state->cli_state = CLIENT_FINISHED;
1006 if (state->ntlmssp_state)
1007 ntlmssp_end(&state->ntlmssp_state);
1009 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1010 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1011 state->cli_state = CLIENT_ERROR;
1012 if (state->ntlmssp_state)
1013 ntlmssp_end(&state->ntlmssp_state);
1016 data_blob_free(&request);
1019 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1020 char *buf, int length)
1025 pass=(char *)memchr(buf,' ',length);
1027 DEBUG(2, ("Password not found. Denying access\n"));
1028 x_fprintf(x_stdout, "ERR\n");
1034 if (state->helper_mode == SQUID_2_5_BASIC) {
1035 rfc1738_unescape(user);
1036 rfc1738_unescape(pass);
1039 if (check_plaintext_auth(user, pass, False)) {
1040 x_fprintf(x_stdout, "OK\n");
1042 x_fprintf(x_stdout, "ERR\n");
1046 static void offer_gss_spnego_mechs(void) {
1052 TALLOC_CTX *ctx = talloc_tos();
1056 ZERO_STRUCT(spnego);
1058 myname_lower = talloc_strdup(ctx, global_myname());
1059 if (!myname_lower) {
1062 strlower_m(myname_lower);
1064 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1069 /* Server negTokenInit (mech offerings) */
1070 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1071 spnego.negTokenInit.mechTypes = SMB_XMALLOC_ARRAY(const char *, 2);
1073 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
1074 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
1075 spnego.negTokenInit.mechTypes[2] = NULL;
1077 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
1078 spnego.negTokenInit.mechTypes[1] = NULL;
1082 spnego.negTokenInit.mechListMIC = data_blob(principal,
1085 len = write_spnego_data(&token, &spnego);
1086 free_spnego_data(&spnego);
1089 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1090 x_fprintf(x_stdout, "BH\n");
1094 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1095 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1097 TALLOC_FREE(reply_base64);
1098 data_blob_free(&token);
1099 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1103 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1104 char *buf, int length)
1106 static NTLMSSP_STATE *ntlmssp_state = NULL;
1107 SPNEGO_DATA request, response;
1111 TALLOC_CTX *ctx = talloc_tos();
1114 char *domain = NULL;
1116 const char *reply_code;
1118 char *reply_argument = NULL;
1120 if (strlen(buf) < 2) {
1121 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1122 x_fprintf(x_stdout, "BH\n");
1126 if (strncmp(buf, "YR", 2) == 0) {
1128 ntlmssp_end(&ntlmssp_state);
1129 } else if (strncmp(buf, "KK", 2) == 0) {
1132 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1133 x_fprintf(x_stdout, "BH\n");
1137 if ( (strlen(buf) == 2)) {
1139 /* no client data, get the negTokenInit offering
1142 offer_gss_spnego_mechs();
1146 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1148 if (strlen(buf) <= 3) {
1149 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1150 x_fprintf(x_stdout, "BH\n");
1154 token = base64_decode_data_blob(buf + 3);
1155 len = read_spnego_data(token, &request);
1156 data_blob_free(&token);
1159 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
1160 x_fprintf(x_stdout, "BH\n");
1164 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1166 /* Second request from Client. This is where the
1167 client offers its mechanism to use. */
1169 if ( (request.negTokenInit.mechTypes == NULL) ||
1170 (request.negTokenInit.mechTypes[0] == NULL) ) {
1171 DEBUG(1, ("Client did not offer any mechanism"));
1172 x_fprintf(x_stdout, "BH\n");
1176 status = NT_STATUS_UNSUCCESSFUL;
1177 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1179 if ( request.negTokenInit.mechToken.data == NULL ) {
1180 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1181 x_fprintf(x_stdout, "BH\n");
1185 if ( ntlmssp_state != NULL ) {
1186 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1187 "already got one\n"));
1188 x_fprintf(x_stdout, "BH\n");
1189 ntlmssp_end(&ntlmssp_state);
1193 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1194 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1198 DEBUG(10, ("got NTLMSSP packet:\n"));
1199 dump_data(10, request.negTokenInit.mechToken.data,
1200 request.negTokenInit.mechToken.length);
1202 response.type = SPNEGO_NEG_TOKEN_TARG;
1203 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1204 response.negTokenTarg.mechListMIC = data_blob_null;
1206 status = ntlmssp_update(ntlmssp_state,
1207 request.negTokenInit.mechToken,
1208 &response.negTokenTarg.responseToken);
1212 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1214 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1217 DATA_BLOB session_key;
1218 PAC_DATA *pac_data = NULL;
1220 if ( request.negTokenInit.mechToken.data == NULL ) {
1221 DEBUG(1, ("Client did not provide Kerberos data\n"));
1222 x_fprintf(x_stdout, "BH\n");
1226 response.type = SPNEGO_NEG_TOKEN_TARG;
1227 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_KERBEROS5_OLD);
1228 response.negTokenTarg.mechListMIC = data_blob_null;
1229 response.negTokenTarg.responseToken = data_blob_null;
1231 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1232 &request.negTokenInit.mechToken,
1233 &principal, &pac_data, &ap_rep,
1234 &session_key, True);
1236 talloc_destroy(mem_ctx);
1238 /* Now in "principal" we have the name we are
1239 authenticated as. */
1241 if (NT_STATUS_IS_OK(status)) {
1243 domain = strchr_m(principal, '@');
1245 if (domain == NULL) {
1246 DEBUG(1, ("Did not get a valid principal "
1247 "from ads_verify_ticket\n"));
1248 x_fprintf(x_stdout, "BH\n");
1253 domain = SMB_STRDUP(domain);
1254 user = SMB_STRDUP(principal);
1256 data_blob_free(&ap_rep);
1258 SAFE_FREE(principal);
1265 if ( (request.negTokenTarg.supportedMech == NULL) ||
1266 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1267 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1268 is the only one we support that sends this stuff */
1269 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1270 request.negTokenTarg.supportedMech));
1271 x_fprintf(x_stdout, "BH\n");
1275 if (request.negTokenTarg.responseToken.data == NULL) {
1276 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1277 x_fprintf(x_stdout, "BH\n");
1281 status = ntlmssp_update(ntlmssp_state,
1282 request.negTokenTarg.responseToken,
1283 &response.negTokenTarg.responseToken);
1285 response.type = SPNEGO_NEG_TOKEN_TARG;
1286 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1287 response.negTokenTarg.mechListMIC = data_blob_null;
1289 if (NT_STATUS_IS_OK(status)) {
1290 user = SMB_STRDUP(ntlmssp_state->user);
1291 domain = SMB_STRDUP(ntlmssp_state->domain);
1292 ntlmssp_end(&ntlmssp_state);
1296 free_spnego_data(&request);
1298 if (NT_STATUS_IS_OK(status)) {
1299 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1301 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1302 } else if (NT_STATUS_EQUAL(status,
1303 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1304 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1306 reply_argument = talloc_strdup(ctx, "*");
1308 response.negTokenTarg.negResult = SPNEGO_REJECT;
1310 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1313 if (!reply_argument) {
1314 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1315 x_fprintf(x_stdout, "BH\n");
1322 len = write_spnego_data(&token, &response);
1323 free_spnego_data(&response);
1326 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1327 x_fprintf(x_stdout, "BH\n");
1331 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1333 x_fprintf(x_stdout, "%s %s %s\n",
1334 reply_code, reply_base64, reply_argument);
1336 TALLOC_FREE(reply_base64);
1337 data_blob_free(&token);
1342 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
1344 static bool manage_client_ntlmssp_init(SPNEGO_DATA spnego)
1347 DATA_BLOB null_blob = data_blob_null;
1348 DATA_BLOB to_server;
1349 char *to_server_base64;
1350 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1352 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1354 if (client_ntlmssp_state != NULL) {
1355 DEBUG(1, ("Request for initial SPNEGO request where "
1356 "we already have a state\n"));
1360 if (!client_ntlmssp_state) {
1361 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1362 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1368 if (opt_password == NULL) {
1370 /* Request a password from the calling process. After
1371 sending it, the calling process should retry with
1372 the negTokenInit. */
1374 DEBUG(10, ("Requesting password\n"));
1375 x_fprintf(x_stdout, "PW\n");
1379 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1380 spnego.negTokenInit.mechTypes = my_mechs;
1381 spnego.negTokenInit.reqFlags = 0;
1382 spnego.negTokenInit.mechListMIC = null_blob;
1384 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1385 &spnego.negTokenInit.mechToken);
1387 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1388 NT_STATUS_IS_OK(status)) ) {
1389 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1390 nt_errstr(status)));
1391 ntlmssp_end(&client_ntlmssp_state);
1395 write_spnego_data(&to_server, &spnego);
1396 data_blob_free(&spnego.negTokenInit.mechToken);
1398 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1399 data_blob_free(&to_server);
1400 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1401 TALLOC_FREE(to_server_base64);
1405 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
1408 DATA_BLOB null_blob = data_blob_null;
1410 DATA_BLOB to_server;
1411 char *to_server_base64;
1413 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1415 if (client_ntlmssp_state == NULL) {
1416 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1417 x_fprintf(x_stdout, "BH\n");
1421 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1422 x_fprintf(x_stdout, "NA\n");
1423 ntlmssp_end(&client_ntlmssp_state);
1427 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1428 x_fprintf(x_stdout, "AF\n");
1429 ntlmssp_end(&client_ntlmssp_state);
1433 status = ntlmssp_update(client_ntlmssp_state,
1434 spnego.negTokenTarg.responseToken,
1437 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1438 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1439 "ntlmssp_client_update, got: %s\n",
1440 nt_errstr(status)));
1441 x_fprintf(x_stdout, "BH\n");
1442 data_blob_free(&request);
1443 ntlmssp_end(&client_ntlmssp_state);
1447 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1448 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1449 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1450 spnego.negTokenTarg.responseToken = request;
1451 spnego.negTokenTarg.mechListMIC = null_blob;
1453 write_spnego_data(&to_server, &spnego);
1454 data_blob_free(&request);
1456 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1457 data_blob_free(&to_server);
1458 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1459 TALLOC_FREE(to_server_base64);
1465 static bool manage_client_krb5_init(SPNEGO_DATA spnego)
1468 DATA_BLOB tkt, to_server;
1469 DATA_BLOB session_key_krb5 = data_blob_null;
1474 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1477 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1478 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1479 DEBUG(1, ("Did not get a principal for krb5\n"));
1483 principal = (char *)SMB_MALLOC(
1484 spnego.negTokenInit.mechListMIC.length+1);
1486 if (principal == NULL) {
1487 DEBUG(1, ("Could not malloc principal\n"));
1491 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1492 spnego.negTokenInit.mechListMIC.length);
1493 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1495 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL);
1500 /* Let's try to first get the TGT, for that we need a
1503 if (opt_password == NULL) {
1504 DEBUG(10, ("Requesting password\n"));
1505 x_fprintf(x_stdout, "PW\n");
1509 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1514 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1515 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1519 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL);
1522 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1527 data_blob_free(&session_key_krb5);
1531 reply.type = SPNEGO_NEG_TOKEN_INIT;
1532 reply.negTokenInit.mechTypes = my_mechs;
1533 reply.negTokenInit.reqFlags = 0;
1534 reply.negTokenInit.mechToken = tkt;
1535 reply.negTokenInit.mechListMIC = data_blob_null;
1537 len = write_spnego_data(&to_server, &reply);
1538 data_blob_free(&tkt);
1541 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1545 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1546 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1548 TALLOC_FREE(reply_base64);
1549 data_blob_free(&to_server);
1550 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1554 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
1556 switch (spnego.negTokenTarg.negResult) {
1557 case SPNEGO_ACCEPT_INCOMPLETE:
1558 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1559 x_fprintf(x_stdout, "BH\n");
1561 case SPNEGO_ACCEPT_COMPLETED:
1562 DEBUG(10, ("Accept completed\n"));
1563 x_fprintf(x_stdout, "AF\n");
1566 DEBUG(10, ("Rejected\n"));
1567 x_fprintf(x_stdout, "NA\n");
1570 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1571 x_fprintf(x_stdout, "AF\n");
1577 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1578 char *buf, int length)
1584 if (!opt_username || !*opt_username) {
1585 x_fprintf(x_stderr, "username must be specified!\n\n");
1589 if (strlen(buf) <= 3) {
1590 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1591 x_fprintf(x_stdout, "BH\n");
1595 request = base64_decode_data_blob(buf+3);
1597 if (strncmp(buf, "PW ", 3) == 0) {
1599 /* We asked for a password and obviously got it :-) */
1601 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1603 if (opt_password == NULL) {
1604 DEBUG(1, ("Out of memory\n"));
1605 x_fprintf(x_stdout, "BH\n");
1606 data_blob_free(&request);
1610 x_fprintf(x_stdout, "OK\n");
1611 data_blob_free(&request);
1615 if ( (strncmp(buf, "TT ", 3) != 0) &&
1616 (strncmp(buf, "AF ", 3) != 0) &&
1617 (strncmp(buf, "NA ", 3) != 0) ) {
1618 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1619 x_fprintf(x_stdout, "BH\n");
1620 data_blob_free(&request);
1624 /* So we got a server challenge to generate a SPNEGO
1625 client-to-server request... */
1627 len = read_spnego_data(request, &spnego);
1628 data_blob_free(&request);
1631 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1632 x_fprintf(x_stdout, "BH\n");
1636 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1638 /* The server offers a list of mechanisms */
1640 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1642 while (*mechType != NULL) {
1645 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1646 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1647 if (manage_client_krb5_init(spnego))
1652 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1653 if (manage_client_ntlmssp_init(spnego))
1660 DEBUG(1, ("Server offered no compatible mechanism\n"));
1661 x_fprintf(x_stdout, "BH\n");
1665 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1667 if (spnego.negTokenTarg.supportedMech == NULL) {
1668 /* On accept/reject Windows does not send the
1669 mechanism anymore. Handle that here and
1670 shut down the mechanisms. */
1672 switch (spnego.negTokenTarg.negResult) {
1673 case SPNEGO_ACCEPT_COMPLETED:
1674 x_fprintf(x_stdout, "AF\n");
1677 x_fprintf(x_stdout, "NA\n");
1680 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1681 "unknown negResult: %d\n",
1682 spnego.negTokenTarg.negResult));
1683 x_fprintf(x_stdout, "BH\n");
1686 ntlmssp_end(&client_ntlmssp_state);
1690 if (strcmp(spnego.negTokenTarg.supportedMech,
1691 OID_NTLMSSP) == 0) {
1692 manage_client_ntlmssp_targ(spnego);
1697 if (strcmp(spnego.negTokenTarg.supportedMech,
1698 OID_KERBEROS5_OLD) == 0) {
1699 manage_client_krb5_targ(spnego);
1706 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1707 x_fprintf(x_stdout, "BH\n");
1711 free_spnego_data(&spnego);
1715 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1716 char *buf, int length)
1718 char *request, *parameter;
1719 static DATA_BLOB challenge;
1720 static DATA_BLOB lm_response;
1721 static DATA_BLOB nt_response;
1722 static char *full_username;
1723 static char *username;
1724 static char *domain;
1725 static char *plaintext_password;
1726 static bool ntlm_server_1_user_session_key;
1727 static bool ntlm_server_1_lm_session_key;
1729 if (strequal(buf, ".")) {
1730 if (!full_username && !username) {
1731 x_fprintf(x_stdout, "Error: No username supplied!\n");
1732 } else if (plaintext_password) {
1733 /* handle this request as plaintext */
1734 if (!full_username) {
1735 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1736 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1740 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1741 x_fprintf(x_stdout, "Authenticated: Yes\n");
1743 x_fprintf(x_stdout, "Authenticated: No\n");
1745 } else if (!lm_response.data && !nt_response.data) {
1746 x_fprintf(x_stdout, "Error: No password supplied!\n");
1747 } else if (!challenge.data) {
1748 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1750 char *error_string = NULL;
1752 uchar user_session_key[16];
1755 if (full_username && !username) {
1757 fstring fstr_domain;
1759 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1760 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1761 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1763 SAFE_FREE(username);
1765 username = smb_xstrdup(fstr_user);
1766 domain = smb_xstrdup(fstr_domain);
1770 domain = smb_xstrdup(get_winbind_domain());
1773 if (ntlm_server_1_lm_session_key)
1774 flags |= WBFLAG_PAM_LMKEY;
1776 if (ntlm_server_1_user_session_key)
1777 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1779 if (!NT_STATUS_IS_OK(
1780 contact_winbind_auth_crap(username,
1792 x_fprintf(x_stdout, "Authenticated: No\n");
1793 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1794 SAFE_FREE(error_string);
1796 static char zeros[16];
1798 char *hex_user_session_key;
1800 x_fprintf(x_stdout, "Authenticated: Yes\n");
1802 if (ntlm_server_1_lm_session_key
1803 && (memcmp(zeros, lm_key,
1804 sizeof(lm_key)) != 0)) {
1805 hex_lm_key = hex_encode(NULL,
1806 (const unsigned char *)lm_key,
1808 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1809 TALLOC_FREE(hex_lm_key);
1812 if (ntlm_server_1_user_session_key
1813 && (memcmp(zeros, user_session_key,
1814 sizeof(user_session_key)) != 0)) {
1815 hex_user_session_key = hex_encode(NULL,
1816 (const unsigned char *)user_session_key,
1817 sizeof(user_session_key));
1818 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1819 TALLOC_FREE(hex_user_session_key);
1823 /* clear out the state */
1824 challenge = data_blob_null;
1825 nt_response = data_blob_null;
1826 lm_response = data_blob_null;
1827 SAFE_FREE(full_username);
1828 SAFE_FREE(username);
1830 SAFE_FREE(plaintext_password);
1831 ntlm_server_1_user_session_key = False;
1832 ntlm_server_1_lm_session_key = False;
1833 x_fprintf(x_stdout, ".\n");
1840 /* Indicates a base64 encoded structure */
1841 parameter = strstr_m(request, ":: ");
1843 parameter = strstr_m(request, ": ");
1846 DEBUG(0, ("Parameter not found!\n"));
1847 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1864 base64_decode_inplace(parameter);
1867 if (strequal(request, "LANMAN-Challenge")) {
1868 challenge = strhex_to_data_blob(NULL, parameter);
1869 if (challenge.length != 8) {
1870 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1872 (int)challenge.length);
1873 challenge = data_blob_null;
1875 } else if (strequal(request, "NT-Response")) {
1876 nt_response = strhex_to_data_blob(NULL, parameter);
1877 if (nt_response.length < 24) {
1878 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1880 (int)nt_response.length);
1881 nt_response = data_blob_null;
1883 } else if (strequal(request, "LANMAN-Response")) {
1884 lm_response = strhex_to_data_blob(NULL, parameter);
1885 if (lm_response.length != 24) {
1886 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
1888 (int)lm_response.length);
1889 lm_response = data_blob_null;
1891 } else if (strequal(request, "Password")) {
1892 plaintext_password = smb_xstrdup(parameter);
1893 } else if (strequal(request, "NT-Domain")) {
1894 domain = smb_xstrdup(parameter);
1895 } else if (strequal(request, "Username")) {
1896 username = smb_xstrdup(parameter);
1897 } else if (strequal(request, "Full-Username")) {
1898 full_username = smb_xstrdup(parameter);
1899 } else if (strequal(request, "Request-User-Session-Key")) {
1900 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
1901 } else if (strequal(request, "Request-LanMan-Session-Key")) {
1902 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
1904 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
1908 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
1909 char *buf, int length)
1911 char *request, *parameter;
1912 static DATA_BLOB new_nt_pswd;
1913 static DATA_BLOB old_nt_hash_enc;
1914 static DATA_BLOB new_lm_pswd;
1915 static DATA_BLOB old_lm_hash_enc;
1916 static char *full_username = NULL;
1917 static char *username = NULL;
1918 static char *domain = NULL;
1919 static char *newpswd = NULL;
1920 static char *oldpswd = NULL;
1922 if (strequal(buf, ".")) {
1923 if(newpswd && oldpswd) {
1924 uchar old_nt_hash[16];
1925 uchar old_lm_hash[16];
1926 uchar new_nt_hash[16];
1927 uchar new_lm_hash[16];
1929 new_nt_pswd = data_blob(NULL, 516);
1930 old_nt_hash_enc = data_blob(NULL, 16);
1932 /* Calculate the MD4 hash (NT compatible) of the
1934 E_md4hash(oldpswd, old_nt_hash);
1935 E_md4hash(newpswd, new_nt_hash);
1937 /* E_deshash returns false for 'long'
1938 passwords (> 14 DOS chars).
1940 Therefore, don't send a buffer
1941 encrypted with the truncated hash
1942 (it could allow an even easier
1943 attack on the password)
1945 Likewise, obey the admin's restriction
1948 if (lp_client_lanman_auth() &&
1949 E_deshash(newpswd, new_lm_hash) &&
1950 E_deshash(oldpswd, old_lm_hash)) {
1951 new_lm_pswd = data_blob(NULL, 516);
1952 old_lm_hash_enc = data_blob(NULL, 16);
1953 encode_pw_buffer(new_lm_pswd.data, newpswd,
1956 SamOEMhash(new_lm_pswd.data, old_nt_hash, 516);
1957 E_old_pw_hash(new_nt_hash, old_lm_hash,
1958 old_lm_hash_enc.data);
1960 new_lm_pswd.data = NULL;
1961 new_lm_pswd.length = 0;
1962 old_lm_hash_enc.data = NULL;
1963 old_lm_hash_enc.length = 0;
1966 encode_pw_buffer(new_nt_pswd.data, newpswd,
1969 SamOEMhash(new_nt_pswd.data, old_nt_hash, 516);
1970 E_old_pw_hash(new_nt_hash, old_nt_hash,
1971 old_nt_hash_enc.data);
1974 if (!full_username && !username) {
1975 x_fprintf(x_stdout, "Error: No username supplied!\n");
1976 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
1977 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
1978 x_fprintf(x_stdout, "Error: No NT or LM password "
1979 "blobs supplied!\n");
1981 char *error_string = NULL;
1983 if (full_username && !username) {
1985 fstring fstr_domain;
1987 if (!parse_ntlm_auth_domain_user(full_username,
1990 /* username might be 'tainted', don't
1991 * print into our new-line
1992 * deleimianted stream */
1993 x_fprintf(x_stdout, "Error: Could not "
1994 "parse into domain and "
1996 SAFE_FREE(username);
1997 username = smb_xstrdup(full_username);
1999 SAFE_FREE(username);
2001 username = smb_xstrdup(fstr_user);
2002 domain = smb_xstrdup(fstr_domain);
2007 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2014 x_fprintf(x_stdout, "Password-Change: No\n");
2015 x_fprintf(x_stdout, "Password-Change-Error: "
2016 "%s\n.\n", error_string);
2018 x_fprintf(x_stdout, "Password-Change: Yes\n");
2021 SAFE_FREE(error_string);
2023 /* clear out the state */
2024 new_nt_pswd = data_blob_null;
2025 old_nt_hash_enc = data_blob_null;
2026 new_lm_pswd = data_blob_null;
2027 old_nt_hash_enc = data_blob_null;
2028 SAFE_FREE(full_username);
2029 SAFE_FREE(username);
2033 x_fprintf(x_stdout, ".\n");
2040 /* Indicates a base64 encoded structure */
2041 parameter = strstr_m(request, ":: ");
2043 parameter = strstr_m(request, ": ");
2046 DEBUG(0, ("Parameter not found!\n"));
2047 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2063 base64_decode_inplace(parameter);
2066 if (strequal(request, "new-nt-password-blob")) {
2067 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2068 if (new_nt_pswd.length != 516) {
2069 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2070 "(got %d bytes, expected 516)\n.\n",
2072 (int)new_nt_pswd.length);
2073 new_nt_pswd = data_blob_null;
2075 } else if (strequal(request, "old-nt-hash-blob")) {
2076 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2077 if (old_nt_hash_enc.length != 16) {
2078 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2079 "(got %d bytes, expected 16)\n.\n",
2081 (int)old_nt_hash_enc.length);
2082 old_nt_hash_enc = data_blob_null;
2084 } else if (strequal(request, "new-lm-password-blob")) {
2085 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2086 if (new_lm_pswd.length != 516) {
2087 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2088 "(got %d bytes, expected 516)\n.\n",
2090 (int)new_lm_pswd.length);
2091 new_lm_pswd = data_blob_null;
2094 else if (strequal(request, "old-lm-hash-blob")) {
2095 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2096 if (old_lm_hash_enc.length != 16)
2098 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2099 "(got %d bytes, expected 16)\n.\n",
2101 (int)old_lm_hash_enc.length);
2102 old_lm_hash_enc = data_blob_null;
2104 } else if (strequal(request, "nt-domain")) {
2105 domain = smb_xstrdup(parameter);
2106 } else if(strequal(request, "username")) {
2107 username = smb_xstrdup(parameter);
2108 } else if(strequal(request, "full-username")) {
2109 username = smb_xstrdup(parameter);
2110 } else if(strequal(request, "new-password")) {
2111 newpswd = smb_xstrdup(parameter);
2112 } else if (strequal(request, "old-password")) {
2113 oldpswd = smb_xstrdup(parameter);
2115 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2119 static void manage_squid_request(struct ntlm_auth_state *state,
2120 stdio_helper_function fn)
2123 char tmp[INITIAL_BUFFER_SIZE+1];
2124 int length, buf_size = 0;
2127 buf = talloc_strdup(state->mem_ctx, "");
2129 DEBUG(0, ("Failed to allocate input buffer.\n"));
2130 x_fprintf(x_stderr, "ERR\n");
2136 /* this is not a typo - x_fgets doesn't work too well under
2138 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2139 if (ferror(stdin)) {
2140 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2141 "(%s)\n", ferror(stdin),
2142 strerror(ferror(stdin))));
2149 buf = talloc_strdup_append_buffer(buf, tmp);
2150 buf_size += INITIAL_BUFFER_SIZE;
2152 if (buf_size > MAX_BUFFER_SIZE) {
2153 DEBUG(2, ("Oversized message\n"));
2154 x_fprintf(x_stderr, "ERR\n");
2159 c = strchr(buf, '\n');
2160 } while (c == NULL);
2165 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2167 if (buf[0] == '\0') {
2168 DEBUG(2, ("Invalid Request\n"));
2169 x_fprintf(x_stderr, "ERR\n");
2174 fn(state, buf, length);
2179 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2180 TALLOC_CTX *mem_ctx;
2181 struct ntlm_auth_state *state;
2183 /* initialize FDescs */
2184 x_setbuf(x_stdout, NULL);
2185 x_setbuf(x_stderr, NULL);
2187 mem_ctx = talloc_init("ntlm_auth");
2189 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2190 x_fprintf(x_stderr, "ERR\n");
2194 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2196 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2197 x_fprintf(x_stderr, "ERR\n");
2201 state->mem_ctx = mem_ctx;
2202 state->helper_mode = stdio_mode;
2205 manage_squid_request(state, fn);
2210 /* Authenticate a user with a challenge/response */
2212 static bool check_auth_crap(void)
2217 char user_session_key[16];
2219 char *hex_user_session_key;
2221 static uint8 zeros[16];
2223 x_setbuf(x_stdout, NULL);
2226 flags |= WBFLAG_PAM_LMKEY;
2228 if (request_user_session_key)
2229 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2231 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2233 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2239 (unsigned char *)lm_key,
2240 (unsigned char *)user_session_key,
2241 &error_string, NULL);
2243 if (!NT_STATUS_IS_OK(nt_status)) {
2244 x_fprintf(x_stdout, "%s (0x%x)\n",
2246 NT_STATUS_V(nt_status));
2247 SAFE_FREE(error_string);
2252 && (memcmp(zeros, lm_key,
2253 sizeof(lm_key)) != 0)) {
2254 hex_lm_key = hex_encode(NULL, (const unsigned char *)lm_key,
2256 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2257 TALLOC_FREE(hex_lm_key);
2259 if (request_user_session_key
2260 && (memcmp(zeros, user_session_key,
2261 sizeof(user_session_key)) != 0)) {
2262 hex_user_session_key = hex_encode(NULL, (const unsigned char *)user_session_key,
2263 sizeof(user_session_key));
2264 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2265 TALLOC_FREE(hex_user_session_key);
2274 OPT_USERNAME = 1000,
2283 OPT_USER_SESSION_KEY,
2285 OPT_REQUIRE_MEMBERSHIP,
2286 OPT_USE_CACHED_CREDS
2289 int main(int argc, const char **argv)
2291 TALLOC_CTX *frame = talloc_stackframe();
2293 static const char *helper_protocol;
2294 static int diagnostics;
2296 static const char *hex_challenge;
2297 static const char *hex_lm_response;
2298 static const char *hex_nt_response;
2302 /* NOTE: DO NOT change this interface without considering the implications!
2303 This is an external interface, which other programs will use to interact
2307 /* We do not use single-letter command abbreviations, because they harm future
2308 interface stability. */
2310 struct poptOption long_options[] = {
2312 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2313 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2314 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2315 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2316 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2317 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2318 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2319 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2320 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2321 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2322 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2323 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2324 { "require-membership-of", 0, POPT_ARG_STRING, &require_membership_of, OPT_REQUIRE_MEMBERSHIP, "Require that a user be a member of this group (either name or SID) for authentication to succeed" },
2329 /* Samba client initialisation */
2334 /* Samba client initialisation */
2336 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2337 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2338 get_dyn_CONFIGFILE(), strerror(errno));
2344 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2346 /* Parse command line options */
2349 poptPrintHelp(pc, stderr, 0);
2353 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2354 POPT_CONTEXT_KEEP_FIRST);
2356 while((opt = poptGetNextOpt(pc)) != -1) {
2359 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2360 if (opt_challenge.length != 8) {
2361 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2363 (int)opt_challenge.length);
2368 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2369 if (opt_lm_response.length != 24) {
2370 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2372 (int)opt_lm_response.length);
2378 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2379 if (opt_nt_response.length < 24) {
2380 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2382 (int)opt_nt_response.length);
2387 case OPT_REQUIRE_MEMBERSHIP:
2388 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2389 require_membership_of_sid = require_membership_of;
2396 char *domain = SMB_STRDUP(opt_username);
2397 char *p = strchr_m(domain, *lp_winbind_separator());
2401 if (opt_domain && !strequal(opt_domain, domain)) {
2402 x_fprintf(x_stderr, "Domain specified in username (%s) "
2403 "doesn't match specified domain (%s)!\n\n",
2404 domain, opt_domain);
2405 poptPrintHelp(pc, stderr, 0);
2408 opt_domain = domain;
2414 /* Note: if opt_domain is "" then send no domain */
2415 if (opt_domain == NULL) {
2416 opt_domain = get_winbind_domain();
2419 if (opt_workstation == NULL) {
2420 opt_workstation = "";
2423 if (helper_protocol) {
2425 for (i=0; i<NUM_HELPER_MODES; i++) {
2426 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2427 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2431 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2433 for (i=0; i<NUM_HELPER_MODES; i++) {
2434 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2440 if (!opt_username || !*opt_username) {
2441 x_fprintf(x_stderr, "username must be specified!\n\n");
2442 poptPrintHelp(pc, stderr, 0);
2446 if (opt_challenge.length) {
2447 if (!check_auth_crap()) {
2453 if (!opt_password) {
2454 opt_password = getpass("password: ");
2458 if (!diagnose_ntlm_auth()) {
2464 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2465 if (!check_plaintext_auth(user, opt_password, True)) {
2472 poptFreeContext(pc);