Fix bug 7005 - mangle method = hash truncates files with dot '. ' character
[Samba.git] / source / utils / ntlm_auth.c
blob2d29ea9b4024628f7afd235350230ff6066cd7b4
1 /*
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/>.
26 #include "includes.h"
27 #include "utils/ntlm_auth.h"
28 #include "smb_krb5.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_WINBIND
33 #define INITIAL_BUFFER_SIZE 300
34 #define MAX_BUFFER_SIZE 630000
36 enum stdio_helper_mode {
37 SQUID_2_4_BASIC,
38 SQUID_2_5_BASIC,
39 SQUID_2_5_NTLMSSP,
40 NTLMSSP_CLIENT_1,
41 GSS_SPNEGO,
42 GSS_SPNEGO_CLIENT,
43 NTLM_SERVER_1,
44 NTLM_CHANGE_PASSWORD_1,
45 NUM_HELPER_MODES
48 enum ntlm_auth_cli_state {
49 CLIENT_INITIAL = 0,
50 CLIENT_RESPONSE,
51 CLIENT_FINISHED,
52 CLIENT_ERROR
55 enum ntlm_auth_svr_state {
56 SERVER_INITIAL = 0,
57 SERVER_CHALLENGE,
58 SERVER_FINISHED,
59 SERVER_ERROR
62 struct ntlm_auth_state {
63 TALLOC_CTX *mem_ctx;
64 enum stdio_helper_mode helper_mode;
65 enum ntlm_auth_cli_state cli_state;
66 enum ntlm_auth_svr_state svr_state;
67 struct ntlmssp_state *ntlmssp_state;
68 uint32_t neg_flags;
69 char *want_feature_list;
70 bool have_session_key;
71 DATA_BLOB session_key;
72 DATA_BLOB initial_message;
75 typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
76 int length);
78 static void manage_squid_basic_request (struct ntlm_auth_state *state,
79 char *buf, int length);
81 static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
82 char *buf, int length);
84 static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
85 char *buf, int length);
87 static void manage_gss_spnego_request (struct ntlm_auth_state *state,
88 char *buf, int length);
90 static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
91 char *buf, int length);
93 static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
94 char *buf, int length);
96 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
97 char *buf, int length);
99 static const struct {
100 enum stdio_helper_mode mode;
101 const char *name;
102 stdio_helper_function fn;
103 } stdio_helper_protocols[] = {
104 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
105 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
106 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
107 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
108 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
109 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
110 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
111 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
112 { NUM_HELPER_MODES, NULL, NULL}
115 const char *opt_username;
116 const char *opt_domain;
117 const char *opt_workstation;
118 const char *opt_password;
119 static DATA_BLOB opt_challenge;
120 static DATA_BLOB opt_lm_response;
121 static DATA_BLOB opt_nt_response;
122 static int request_lm_key;
123 static int request_user_session_key;
124 static int use_cached_creds;
126 static const char *require_membership_of;
127 static const char *require_membership_of_sid;
129 static char winbind_separator(void)
131 struct winbindd_response response;
132 static bool got_sep;
133 static char sep;
135 if (got_sep)
136 return sep;
138 ZERO_STRUCT(response);
140 /* Send off request */
142 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
143 NSS_STATUS_SUCCESS) {
144 d_printf("could not obtain winbind separator!\n");
145 return *lp_winbind_separator();
148 sep = response.data.info.winbind_separator;
149 got_sep = True;
151 if (!sep) {
152 d_printf("winbind separator was NULL!\n");
153 return *lp_winbind_separator();
156 return sep;
159 const char *get_winbind_domain(void)
161 struct winbindd_response response;
163 static fstring winbind_domain;
164 if (*winbind_domain) {
165 return winbind_domain;
168 ZERO_STRUCT(response);
170 /* Send off request */
172 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
173 NSS_STATUS_SUCCESS) {
174 DEBUG(0, ("could not obtain winbind domain name!\n"));
175 return lp_workgroup();
178 fstrcpy(winbind_domain, response.data.domain_name);
180 return winbind_domain;
184 const char *get_winbind_netbios_name(void)
186 struct winbindd_response response;
188 static fstring winbind_netbios_name;
190 if (*winbind_netbios_name) {
191 return winbind_netbios_name;
194 ZERO_STRUCT(response);
196 /* Send off request */
198 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
199 NSS_STATUS_SUCCESS) {
200 DEBUG(0, ("could not obtain winbind netbios name!\n"));
201 return global_myname();
204 fstrcpy(winbind_netbios_name, response.data.netbios_name);
206 return winbind_netbios_name;
210 DATA_BLOB get_challenge(void)
212 static DATA_BLOB chal;
213 if (opt_challenge.length)
214 return opt_challenge;
216 chal = data_blob(NULL, 8);
218 generate_random_buffer(chal.data, chal.length);
219 return chal;
222 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
223 form DOMAIN/user into a domain and a user */
225 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
226 fstring user)
229 char *p = strchr(domuser,winbind_separator());
231 if (!p) {
232 return False;
235 fstrcpy(user, p+1);
236 fstrcpy(domain, domuser);
237 domain[PTR_DIFF(p, domuser)] = 0;
238 strupper_m(domain);
240 return True;
243 static bool get_require_membership_sid(void) {
244 struct winbindd_request request;
245 struct winbindd_response response;
247 if (!require_membership_of) {
248 return True;
251 if (require_membership_of_sid) {
252 return True;
255 /* Otherwise, ask winbindd for the name->sid request */
257 ZERO_STRUCT(request);
258 ZERO_STRUCT(response);
260 if (!parse_ntlm_auth_domain_user(require_membership_of,
261 request.data.name.dom_name,
262 request.data.name.name)) {
263 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
264 require_membership_of));
265 return False;
268 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
269 NSS_STATUS_SUCCESS) {
270 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
271 require_membership_of));
272 return False;
275 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
277 if (require_membership_of_sid)
278 return True;
280 return False;
282 /* Authenticate a user with a plaintext password */
284 static bool check_plaintext_auth(const char *user, const char *pass,
285 bool stdout_diagnostics)
287 struct winbindd_request request;
288 struct winbindd_response response;
289 NSS_STATUS result;
291 if (!get_require_membership_sid()) {
292 return False;
295 /* Send off request */
297 ZERO_STRUCT(request);
298 ZERO_STRUCT(response);
300 fstrcpy(request.data.auth.user, user);
301 fstrcpy(request.data.auth.pass, pass);
302 if (require_membership_of_sid) {
303 strlcpy(request.data.auth.require_membership_of_sid,
304 require_membership_of_sid,
305 sizeof(request.data.auth.require_membership_of_sid));
308 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
310 /* Display response */
312 if (stdout_diagnostics) {
313 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
314 d_printf("Reading winbind reply failed! (0x01)\n");
317 d_printf("%s: %s (0x%x)\n",
318 response.data.auth.nt_status_string,
319 response.data.auth.error_string,
320 response.data.auth.nt_status);
321 } else {
322 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
323 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
326 DEBUG(3, ("%s: %s (0x%x)\n",
327 response.data.auth.nt_status_string,
328 response.data.auth.error_string,
329 response.data.auth.nt_status));
332 return (result == NSS_STATUS_SUCCESS);
335 /* authenticate a user with an encrypted username/password */
337 NTSTATUS contact_winbind_auth_crap(const char *username,
338 const char *domain,
339 const char *workstation,
340 const DATA_BLOB *challenge,
341 const DATA_BLOB *lm_response,
342 const DATA_BLOB *nt_response,
343 uint32 flags,
344 uint8 lm_key[8],
345 uint8 user_session_key[16],
346 char **error_string,
347 char **unix_name)
349 NTSTATUS nt_status;
350 NSS_STATUS result;
351 struct winbindd_request request;
352 struct winbindd_response response;
354 if (!get_require_membership_sid()) {
355 return NT_STATUS_INVALID_PARAMETER;
358 ZERO_STRUCT(request);
359 ZERO_STRUCT(response);
361 request.flags = flags;
363 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
365 if (require_membership_of_sid)
366 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
368 fstrcpy(request.data.auth_crap.user, username);
369 fstrcpy(request.data.auth_crap.domain, domain);
371 fstrcpy(request.data.auth_crap.workstation,
372 workstation);
374 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
376 if (lm_response && lm_response->length) {
377 memcpy(request.data.auth_crap.lm_resp,
378 lm_response->data,
379 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
380 request.data.auth_crap.lm_resp_len = lm_response->length;
383 if (nt_response && nt_response->length) {
384 if (nt_response->length > sizeof(request.data.auth_crap.nt_resp)) {
385 request.flags = request.flags | WBFLAG_BIG_NTLMV2_BLOB;
386 request.extra_len = nt_response->length;
387 request.extra_data.data = SMB_MALLOC_ARRAY(char, request.extra_len);
388 if (request.extra_data.data == NULL) {
389 return NT_STATUS_NO_MEMORY;
391 memcpy(request.extra_data.data, nt_response->data,
392 nt_response->length);
394 } else {
395 memcpy(request.data.auth_crap.nt_resp,
396 nt_response->data, nt_response->length);
398 request.data.auth_crap.nt_resp_len = nt_response->length;
401 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
402 SAFE_FREE(request.extra_data.data);
404 /* Display response */
406 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
407 nt_status = NT_STATUS_UNSUCCESSFUL;
408 if (error_string)
409 *error_string = smb_xstrdup("Reading winbind reply failed!");
410 winbindd_free_response(&response);
411 return nt_status;
414 nt_status = (NT_STATUS(response.data.auth.nt_status));
415 if (!NT_STATUS_IS_OK(nt_status)) {
416 if (error_string)
417 *error_string = smb_xstrdup(response.data.auth.error_string);
418 winbindd_free_response(&response);
419 return nt_status;
422 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
423 memcpy(lm_key, response.data.auth.first_8_lm_hash,
424 sizeof(response.data.auth.first_8_lm_hash));
426 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
427 memcpy(user_session_key, response.data.auth.user_session_key,
428 sizeof(response.data.auth.user_session_key));
431 if (flags & WBFLAG_PAM_UNIX_NAME) {
432 *unix_name = SMB_STRDUP(response.data.auth.unix_username);
433 if (!*unix_name) {
434 winbindd_free_response(&response);
435 return NT_STATUS_NO_MEMORY;
439 winbindd_free_response(&response);
440 return nt_status;
443 /* contact server to change user password using auth crap */
444 static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
445 const char *domain,
446 const DATA_BLOB new_nt_pswd,
447 const DATA_BLOB old_nt_hash_enc,
448 const DATA_BLOB new_lm_pswd,
449 const DATA_BLOB old_lm_hash_enc,
450 char **error_string)
452 NTSTATUS nt_status;
453 NSS_STATUS result;
454 struct winbindd_request request;
455 struct winbindd_response response;
457 if (!get_require_membership_sid())
459 if(error_string)
460 *error_string = smb_xstrdup("Can't get membership sid.");
461 return NT_STATUS_INVALID_PARAMETER;
464 ZERO_STRUCT(request);
465 ZERO_STRUCT(response);
467 if(username != NULL)
468 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
469 if(domain != NULL)
470 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
472 if(new_nt_pswd.length)
474 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
475 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
478 if(old_nt_hash_enc.length)
480 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));
481 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
484 if(new_lm_pswd.length)
486 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
487 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
490 if(old_lm_hash_enc.length)
492 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));
493 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
496 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
498 /* Display response */
500 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
502 nt_status = NT_STATUS_UNSUCCESSFUL;
503 if (error_string)
504 *error_string = smb_xstrdup("Reading winbind reply failed!");
505 winbindd_free_response(&response);
506 return nt_status;
509 nt_status = (NT_STATUS(response.data.auth.nt_status));
510 if (!NT_STATUS_IS_OK(nt_status))
512 if (error_string)
513 *error_string = smb_xstrdup(response.data.auth.error_string);
514 winbindd_free_response(&response);
515 return nt_status;
518 winbindd_free_response(&response);
520 return nt_status;
523 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
525 static const char zeros[16] = { 0, };
526 NTSTATUS nt_status;
527 char *error_string = NULL;
528 uint8 lm_key[8];
529 uint8 user_sess_key[16];
530 char *unix_name = NULL;
532 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
533 ntlmssp_state->workstation,
534 &ntlmssp_state->chal,
535 &ntlmssp_state->lm_resp,
536 &ntlmssp_state->nt_resp,
537 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
538 lm_key, user_sess_key,
539 &error_string, &unix_name);
541 if (NT_STATUS_IS_OK(nt_status)) {
542 if (memcmp(lm_key, zeros, 8) != 0) {
543 *lm_session_key = data_blob(NULL, 16);
544 memcpy(lm_session_key->data, lm_key, 8);
545 memset(lm_session_key->data+8, '\0', 8);
548 if (memcmp(user_sess_key, zeros, 16) != 0) {
549 *user_session_key = data_blob(user_sess_key, 16);
551 ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state->mem_ctx, unix_name);
552 } else {
553 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
554 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
555 ntlmssp_state->domain, ntlmssp_state->user,
556 ntlmssp_state->workstation,
557 error_string ? error_string : "unknown error (NULL)"));
558 ntlmssp_state->auth_context = NULL;
561 SAFE_FREE(error_string);
562 SAFE_FREE(unix_name);
563 return nt_status;
566 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
568 NTSTATUS nt_status;
569 uint8 lm_pw[16], nt_pw[16];
571 nt_lm_owf_gen (opt_password, nt_pw, lm_pw);
573 nt_status = ntlm_password_check(ntlmssp_state->mem_ctx,
574 &ntlmssp_state->chal,
575 &ntlmssp_state->lm_resp,
576 &ntlmssp_state->nt_resp,
577 NULL, NULL,
578 ntlmssp_state->user,
579 ntlmssp_state->user,
580 ntlmssp_state->domain,
581 lm_pw, nt_pw, user_session_key, lm_session_key);
583 if (NT_STATUS_IS_OK(nt_status)) {
584 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state->mem_ctx,
585 "%s%c%s", ntlmssp_state->domain,
586 *lp_winbind_separator(),
587 ntlmssp_state->user);
588 } else {
589 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
590 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
591 nt_errstr(nt_status)));
592 ntlmssp_state->auth_context = NULL;
594 return nt_status;
597 static NTSTATUS ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE **client_ntlmssp_state)
599 NTSTATUS status;
600 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
601 status = NT_STATUS_UNSUCCESSFUL;
602 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
603 return NT_STATUS_INVALID_PARAMETER;
606 status = ntlmssp_client_start(client_ntlmssp_state);
608 if (!NT_STATUS_IS_OK(status)) {
609 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
610 nt_errstr(status)));
611 ntlmssp_end(client_ntlmssp_state);
612 return status;
615 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
617 if (!NT_STATUS_IS_OK(status)) {
618 DEBUG(1, ("Could not set username: %s\n",
619 nt_errstr(status)));
620 ntlmssp_end(client_ntlmssp_state);
621 return status;
624 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
626 if (!NT_STATUS_IS_OK(status)) {
627 DEBUG(1, ("Could not set domain: %s\n",
628 nt_errstr(status)));
629 ntlmssp_end(client_ntlmssp_state);
630 return status;
633 if (opt_password) {
634 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
636 if (!NT_STATUS_IS_OK(status)) {
637 DEBUG(1, ("Could not set password: %s\n",
638 nt_errstr(status)));
639 ntlmssp_end(client_ntlmssp_state);
640 return status;
644 return NT_STATUS_OK;
647 static NTSTATUS ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE **ntlmssp_state)
649 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
651 if (!NT_STATUS_IS_OK(status)) {
652 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
653 nt_errstr(status)));
654 return status;
657 /* Have we been given a local password, or should we ask winbind? */
658 if (opt_password) {
659 (*ntlmssp_state)->check_password = local_pw_check;
660 (*ntlmssp_state)->get_domain = lp_workgroup;
661 (*ntlmssp_state)->get_global_myname = global_myname;
662 } else {
663 (*ntlmssp_state)->check_password = winbind_pw_check;
664 (*ntlmssp_state)->get_domain = get_winbind_domain;
665 (*ntlmssp_state)->get_global_myname = get_winbind_netbios_name;
667 return NT_STATUS_OK;
670 /*******************************************************************
671 Used by firefox to drive NTLM auth to IIS servers.
672 *******************************************************************/
674 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
675 DATA_BLOB *reply)
677 struct winbindd_request wb_request;
678 struct winbindd_response wb_response;
679 NSS_STATUS result;
681 /* get winbindd to do the ntlmssp step on our behalf */
682 ZERO_STRUCT(wb_request);
683 ZERO_STRUCT(wb_response);
685 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
686 "%s%c%s", opt_domain, winbind_separator(), opt_username);
687 wb_request.data.ccache_ntlm_auth.uid = geteuid();
688 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
689 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
690 wb_request.extra_len = initial_msg.length + challenge_msg.length;
692 if (wb_request.extra_len > 0) {
693 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
694 if (wb_request.extra_data.data == NULL) {
695 return NT_STATUS_NO_MEMORY;
698 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
699 memcpy(wb_request.extra_data.data + initial_msg.length,
700 challenge_msg.data, challenge_msg.length);
703 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
704 SAFE_FREE(wb_request.extra_data.data);
706 if (result != NSS_STATUS_SUCCESS) {
707 winbindd_free_response(&wb_response);
708 return NT_STATUS_UNSUCCESSFUL;
711 if (reply) {
712 *reply = data_blob(wb_response.extra_data.data,
713 wb_response.data.ccache_ntlm_auth.auth_blob_len);
714 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
715 reply->data == NULL) {
716 winbindd_free_response(&wb_response);
717 return NT_STATUS_NO_MEMORY;
721 winbindd_free_response(&wb_response);
722 return NT_STATUS_MORE_PROCESSING_REQUIRED;
725 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
726 char *buf, int length)
728 DATA_BLOB request, reply;
729 NTSTATUS nt_status;
731 if (strlen(buf) < 2) {
732 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
733 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
734 return;
737 if (strlen(buf) > 3) {
738 if(strncmp(buf, "SF ", 3) == 0){
739 DEBUG(10, ("Setting flags to negotioate\n"));
740 TALLOC_FREE(state->want_feature_list);
741 state->want_feature_list = talloc_strdup(state->mem_ctx,
742 buf+3);
743 x_fprintf(x_stdout, "OK\n");
744 return;
746 request = base64_decode_data_blob(buf + 3);
747 } else {
748 request = data_blob_null;
751 if ((strncmp(buf, "PW ", 3) == 0)) {
752 /* The calling application wants us to use a local password
753 * (rather than winbindd) */
755 opt_password = SMB_STRNDUP((const char *)request.data,
756 request.length);
758 if (opt_password == NULL) {
759 DEBUG(1, ("Out of memory\n"));
760 x_fprintf(x_stdout, "BH Out of memory\n");
761 data_blob_free(&request);
762 return;
765 x_fprintf(x_stdout, "OK\n");
766 data_blob_free(&request);
767 return;
770 if (strncmp(buf, "YR", 2) == 0) {
771 if (state->ntlmssp_state)
772 ntlmssp_end(&state->ntlmssp_state);
773 state->svr_state = SERVER_INITIAL;
774 } else if (strncmp(buf, "KK", 2) == 0) {
775 /* No special preprocessing required */
776 } else if (strncmp(buf, "GF", 2) == 0) {
777 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
779 if (state->svr_state == SERVER_FINISHED) {
780 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
782 else {
783 x_fprintf(x_stdout, "BH\n");
785 data_blob_free(&request);
786 return;
787 } else if (strncmp(buf, "GK", 2) == 0) {
788 DEBUG(10, ("Requested NTLMSSP session key\n"));
789 if(state->have_session_key) {
790 char *key64 = base64_encode_data_blob(state->mem_ctx,
791 state->session_key);
792 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
793 TALLOC_FREE(key64);
794 } else {
795 x_fprintf(x_stdout, "BH\n");
798 data_blob_free(&request);
799 return;
800 } else {
801 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
802 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
803 return;
806 if (!state->ntlmssp_state) {
807 nt_status = ntlm_auth_start_ntlmssp_server(
808 &state->ntlmssp_state);
809 if (!NT_STATUS_IS_OK(nt_status)) {
810 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
811 return;
813 ntlmssp_want_feature_list(state->ntlmssp_state,
814 state->want_feature_list);
817 DEBUG(10, ("got NTLMSSP packet:\n"));
818 dump_data(10, request.data, request.length);
820 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
822 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
823 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
824 reply);
825 x_fprintf(x_stdout, "TT %s\n", reply_base64);
826 TALLOC_FREE(reply_base64);
827 data_blob_free(&reply);
828 state->svr_state = SERVER_CHALLENGE;
829 DEBUG(10, ("NTLMSSP challenge\n"));
830 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
831 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
832 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
834 ntlmssp_end(&state->ntlmssp_state);
835 } else if (!NT_STATUS_IS_OK(nt_status)) {
836 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
837 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
838 } else {
839 x_fprintf(x_stdout, "AF %s\n",
840 (char *)state->ntlmssp_state->auth_context);
841 DEBUG(10, ("NTLMSSP OK!\n"));
843 if(state->have_session_key)
844 data_blob_free(&state->session_key);
845 state->session_key = data_blob(
846 state->ntlmssp_state->session_key.data,
847 state->ntlmssp_state->session_key.length);
848 state->neg_flags = state->ntlmssp_state->neg_flags;
849 state->have_session_key = true;
850 state->svr_state = SERVER_FINISHED;
853 data_blob_free(&request);
856 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
857 char *buf, int length)
859 DATA_BLOB request, reply;
860 NTSTATUS nt_status;
862 if (!opt_username || !*opt_username) {
863 x_fprintf(x_stderr, "username must be specified!\n\n");
864 exit(1);
867 if (strlen(buf) < 2) {
868 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
869 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
870 return;
873 if (strlen(buf) > 3) {
874 if(strncmp(buf, "SF ", 3) == 0) {
875 DEBUG(10, ("Looking for flags to negotiate\n"));
876 talloc_free(state->want_feature_list);
877 state->want_feature_list = talloc_strdup(state->mem_ctx,
878 buf+3);
879 x_fprintf(x_stdout, "OK\n");
880 return;
882 request = base64_decode_data_blob(buf + 3);
883 } else {
884 request = data_blob_null;
887 if (strncmp(buf, "PW ", 3) == 0) {
888 /* We asked for a password and obviously got it :-) */
890 opt_password = SMB_STRNDUP((const char *)request.data,
891 request.length);
893 if (opt_password == NULL) {
894 DEBUG(1, ("Out of memory\n"));
895 x_fprintf(x_stdout, "BH Out of memory\n");
896 data_blob_free(&request);
897 return;
900 x_fprintf(x_stdout, "OK\n");
901 data_blob_free(&request);
902 return;
905 if (!state->ntlmssp_state && use_cached_creds) {
906 /* check whether cached credentials are usable. */
907 DATA_BLOB empty_blob = data_blob_null;
909 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
910 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
911 /* failed to use cached creds */
912 use_cached_creds = False;
916 if (opt_password == NULL && !use_cached_creds) {
917 /* Request a password from the calling process. After
918 sending it, the calling process should retry asking for the
919 negotiate. */
921 DEBUG(10, ("Requesting password\n"));
922 x_fprintf(x_stdout, "PW\n");
923 return;
926 if (strncmp(buf, "YR", 2) == 0) {
927 if (state->ntlmssp_state)
928 ntlmssp_end(&state->ntlmssp_state);
929 state->cli_state = CLIENT_INITIAL;
930 } else if (strncmp(buf, "TT", 2) == 0) {
931 /* No special preprocessing required */
932 } else if (strncmp(buf, "GF", 2) == 0) {
933 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
935 if(state->cli_state == CLIENT_FINISHED) {
936 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
938 else {
939 x_fprintf(x_stdout, "BH\n");
942 data_blob_free(&request);
943 return;
944 } else if (strncmp(buf, "GK", 2) == 0 ) {
945 DEBUG(10, ("Requested session key\n"));
947 if(state->cli_state == CLIENT_FINISHED) {
948 char *key64 = base64_encode_data_blob(state->mem_ctx,
949 state->session_key);
950 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
951 TALLOC_FREE(key64);
953 else {
954 x_fprintf(x_stdout, "BH\n");
957 data_blob_free(&request);
958 return;
959 } else {
960 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
961 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
962 return;
965 if (!state->ntlmssp_state) {
966 nt_status = ntlm_auth_start_ntlmssp_client(
967 &state->ntlmssp_state);
968 if (!NT_STATUS_IS_OK(nt_status)) {
969 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
970 return;
972 ntlmssp_want_feature_list(state->ntlmssp_state,
973 state->want_feature_list);
974 state->initial_message = data_blob_null;
977 DEBUG(10, ("got NTLMSSP packet:\n"));
978 dump_data(10, request.data, request.length);
980 if (use_cached_creds && !opt_password &&
981 (state->cli_state == CLIENT_RESPONSE)) {
982 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
983 &reply);
984 } else {
985 nt_status = ntlmssp_update(state->ntlmssp_state, request,
986 &reply);
989 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
990 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
991 reply);
992 if (state->cli_state == CLIENT_INITIAL) {
993 x_fprintf(x_stdout, "YR %s\n", reply_base64);
994 state->initial_message = reply;
995 state->cli_state = CLIENT_RESPONSE;
996 } else {
997 x_fprintf(x_stdout, "KK %s\n", reply_base64);
998 data_blob_free(&reply);
1000 TALLOC_FREE(reply_base64);
1001 DEBUG(10, ("NTLMSSP challenge\n"));
1002 } else if (NT_STATUS_IS_OK(nt_status)) {
1003 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1004 reply);
1005 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1006 TALLOC_FREE(reply_base64);
1008 if(state->have_session_key)
1009 data_blob_free(&state->session_key);
1011 state->session_key = data_blob(
1012 state->ntlmssp_state->session_key.data,
1013 state->ntlmssp_state->session_key.length);
1014 state->neg_flags = state->ntlmssp_state->neg_flags;
1015 state->have_session_key = true;
1017 DEBUG(10, ("NTLMSSP OK!\n"));
1018 state->cli_state = CLIENT_FINISHED;
1019 if (state->ntlmssp_state)
1020 ntlmssp_end(&state->ntlmssp_state);
1021 } else {
1022 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1023 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1024 state->cli_state = CLIENT_ERROR;
1025 if (state->ntlmssp_state)
1026 ntlmssp_end(&state->ntlmssp_state);
1029 data_blob_free(&request);
1032 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1033 char *buf, int length)
1035 char *user, *pass;
1036 user=buf;
1038 pass=(char *)memchr(buf,' ',length);
1039 if (!pass) {
1040 DEBUG(2, ("Password not found. Denying access\n"));
1041 x_fprintf(x_stdout, "ERR\n");
1042 return;
1044 *pass='\0';
1045 pass++;
1047 if (state->helper_mode == SQUID_2_5_BASIC) {
1048 rfc1738_unescape(user);
1049 rfc1738_unescape(pass);
1052 if (check_plaintext_auth(user, pass, False)) {
1053 x_fprintf(x_stdout, "OK\n");
1054 } else {
1055 x_fprintf(x_stdout, "ERR\n");
1059 static void offer_gss_spnego_mechs(void) {
1061 DATA_BLOB token;
1062 SPNEGO_DATA spnego;
1063 ssize_t len;
1064 char *reply_base64;
1065 TALLOC_CTX *ctx = talloc_tos();
1066 char *principal;
1067 char *myname_lower;
1069 ZERO_STRUCT(spnego);
1071 myname_lower = talloc_strdup(ctx, global_myname());
1072 if (!myname_lower) {
1073 return;
1075 strlower_m(myname_lower);
1077 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1078 if (!principal) {
1079 return;
1082 /* Server negTokenInit (mech offerings) */
1083 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1084 spnego.negTokenInit.mechTypes = SMB_XMALLOC_ARRAY(const char *, 2);
1085 #ifdef HAVE_KRB5
1086 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
1087 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
1088 spnego.negTokenInit.mechTypes[2] = NULL;
1089 #else
1090 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
1091 spnego.negTokenInit.mechTypes[1] = NULL;
1092 #endif
1095 spnego.negTokenInit.mechListMIC = data_blob(principal,
1096 strlen(principal));
1098 len = write_spnego_data(&token, &spnego);
1099 free_spnego_data(&spnego);
1101 if (len == -1) {
1102 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1103 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1104 return;
1107 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1108 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1110 TALLOC_FREE(reply_base64);
1111 data_blob_free(&token);
1112 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1113 return;
1116 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1117 char *buf, int length)
1119 static NTLMSSP_STATE *ntlmssp_state = NULL;
1120 SPNEGO_DATA request, response;
1121 DATA_BLOB token;
1122 NTSTATUS status;
1123 ssize_t len;
1124 TALLOC_CTX *ctx = talloc_tos();
1126 char *user = NULL;
1127 char *domain = NULL;
1129 const char *reply_code;
1130 char *reply_base64;
1131 char *reply_argument = NULL;
1133 if (strlen(buf) < 2) {
1134 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1135 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1136 return;
1139 if (strncmp(buf, "YR", 2) == 0) {
1140 if (ntlmssp_state)
1141 ntlmssp_end(&ntlmssp_state);
1142 } else if (strncmp(buf, "KK", 2) == 0) {
1144 } else {
1145 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1146 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1147 return;
1150 if ( (strlen(buf) == 2)) {
1152 /* no client data, get the negTokenInit offering
1153 mechanisms */
1155 offer_gss_spnego_mechs();
1156 return;
1159 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1161 if (strlen(buf) <= 3) {
1162 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1163 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1164 return;
1167 token = base64_decode_data_blob(buf + 3);
1168 len = read_spnego_data(token, &request);
1169 data_blob_free(&token);
1171 if (len == -1) {
1172 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
1173 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1174 return;
1177 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1179 /* Second request from Client. This is where the
1180 client offers its mechanism to use. */
1182 if ( (request.negTokenInit.mechTypes == NULL) ||
1183 (request.negTokenInit.mechTypes[0] == NULL) ) {
1184 DEBUG(1, ("Client did not offer any mechanism"));
1185 x_fprintf(x_stdout, "BH Client did not offer any "
1186 "mechanism\n");
1187 return;
1190 status = NT_STATUS_UNSUCCESSFUL;
1191 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1193 if ( request.negTokenInit.mechToken.data == NULL ) {
1194 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1195 x_fprintf(x_stdout, "BH Client did not provide "
1196 "NTLMSSP data\n");
1197 return;
1200 if ( ntlmssp_state != NULL ) {
1201 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1202 "already got one\n"));
1203 x_fprintf(x_stdout, "BH Client wants a new "
1204 "NTLMSSP challenge, but "
1205 "already got one\n");
1206 ntlmssp_end(&ntlmssp_state);
1207 return;
1210 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1211 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1212 return;
1215 DEBUG(10, ("got NTLMSSP packet:\n"));
1216 dump_data(10, request.negTokenInit.mechToken.data,
1217 request.negTokenInit.mechToken.length);
1219 response.type = SPNEGO_NEG_TOKEN_TARG;
1220 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1221 response.negTokenTarg.mechListMIC = data_blob_null;
1223 status = ntlmssp_update(ntlmssp_state,
1224 request.negTokenInit.mechToken,
1225 &response.negTokenTarg.responseToken);
1228 #ifdef HAVE_KRB5
1229 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1231 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1232 char *principal;
1233 DATA_BLOB ap_rep;
1234 DATA_BLOB session_key;
1235 struct PAC_DATA *pac_data = NULL;
1237 if ( request.negTokenInit.mechToken.data == NULL ) {
1238 DEBUG(1, ("Client did not provide Kerberos data\n"));
1239 x_fprintf(x_stdout, "BH Client did not provide "
1240 "Kerberos data\n");
1241 return;
1244 response.type = SPNEGO_NEG_TOKEN_TARG;
1245 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_KERBEROS5_OLD);
1246 response.negTokenTarg.mechListMIC = data_blob_null;
1247 response.negTokenTarg.responseToken = data_blob_null;
1249 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1250 &request.negTokenInit.mechToken,
1251 &principal, &pac_data, &ap_rep,
1252 &session_key, True);
1254 talloc_destroy(mem_ctx);
1256 /* Now in "principal" we have the name we are
1257 authenticated as. */
1259 if (NT_STATUS_IS_OK(status)) {
1261 domain = strchr_m(principal, '@');
1263 if (domain == NULL) {
1264 DEBUG(1, ("Did not get a valid principal "
1265 "from ads_verify_ticket\n"));
1266 x_fprintf(x_stdout, "BH Did not get a "
1267 "valid principal from "
1268 "ads_verify_ticket\n");
1269 return;
1272 *domain++ = '\0';
1273 domain = SMB_STRDUP(domain);
1274 user = SMB_STRDUP(principal);
1276 data_blob_free(&ap_rep);
1278 SAFE_FREE(principal);
1281 #endif
1283 } else {
1285 if ( (request.negTokenTarg.supportedMech == NULL) ||
1286 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1287 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1288 is the only one we support that sends this stuff */
1289 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1290 request.negTokenTarg.supportedMech));
1291 x_fprintf(x_stdout, "BH Got a negTokenTarg for "
1292 "something non-NTLMSSP\n");
1293 return;
1296 if (request.negTokenTarg.responseToken.data == NULL) {
1297 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1298 x_fprintf(x_stdout, "BH Got a negTokenTarg without a "
1299 "responseToken!\n");
1300 return;
1303 status = ntlmssp_update(ntlmssp_state,
1304 request.negTokenTarg.responseToken,
1305 &response.negTokenTarg.responseToken);
1307 response.type = SPNEGO_NEG_TOKEN_TARG;
1308 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1309 response.negTokenTarg.mechListMIC = data_blob_null;
1311 if (NT_STATUS_IS_OK(status)) {
1312 user = SMB_STRDUP(ntlmssp_state->user);
1313 domain = SMB_STRDUP(ntlmssp_state->domain);
1314 ntlmssp_end(&ntlmssp_state);
1318 free_spnego_data(&request);
1320 if (NT_STATUS_IS_OK(status)) {
1321 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1322 reply_code = "AF";
1323 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1324 } else if (NT_STATUS_EQUAL(status,
1325 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1326 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1327 reply_code = "TT";
1328 reply_argument = talloc_strdup(ctx, "*");
1329 } else {
1330 response.negTokenTarg.negResult = SPNEGO_REJECT;
1331 reply_code = "NA";
1332 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1335 if (!reply_argument) {
1336 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1337 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1338 return;
1341 SAFE_FREE(user);
1342 SAFE_FREE(domain);
1344 len = write_spnego_data(&token, &response);
1345 free_spnego_data(&response);
1347 if (len == -1) {
1348 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1349 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1350 return;
1353 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1355 x_fprintf(x_stdout, "%s %s %s\n",
1356 reply_code, reply_base64, reply_argument);
1358 TALLOC_FREE(reply_base64);
1359 data_blob_free(&token);
1361 return;
1364 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
1366 static bool manage_client_ntlmssp_init(SPNEGO_DATA spnego)
1368 NTSTATUS status;
1369 DATA_BLOB null_blob = data_blob_null;
1370 DATA_BLOB to_server;
1371 char *to_server_base64;
1372 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1374 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1376 if (client_ntlmssp_state != NULL) {
1377 DEBUG(1, ("Request for initial SPNEGO request where "
1378 "we already have a state\n"));
1379 return False;
1382 if (!client_ntlmssp_state) {
1383 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1384 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1385 return False;
1390 if (opt_password == NULL) {
1392 /* Request a password from the calling process. After
1393 sending it, the calling process should retry with
1394 the negTokenInit. */
1396 DEBUG(10, ("Requesting password\n"));
1397 x_fprintf(x_stdout, "PW\n");
1398 return True;
1401 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1402 spnego.negTokenInit.mechTypes = my_mechs;
1403 spnego.negTokenInit.reqFlags = 0;
1404 spnego.negTokenInit.mechListMIC = null_blob;
1406 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1407 &spnego.negTokenInit.mechToken);
1409 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1410 NT_STATUS_IS_OK(status)) ) {
1411 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1412 nt_errstr(status)));
1413 ntlmssp_end(&client_ntlmssp_state);
1414 return False;
1417 write_spnego_data(&to_server, &spnego);
1418 data_blob_free(&spnego.negTokenInit.mechToken);
1420 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1421 data_blob_free(&to_server);
1422 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1423 TALLOC_FREE(to_server_base64);
1424 return True;
1427 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
1429 NTSTATUS status;
1430 DATA_BLOB null_blob = data_blob_null;
1431 DATA_BLOB request;
1432 DATA_BLOB to_server;
1433 char *to_server_base64;
1435 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1437 if (client_ntlmssp_state == NULL) {
1438 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1439 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1440 return;
1443 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1444 x_fprintf(x_stdout, "NA\n");
1445 ntlmssp_end(&client_ntlmssp_state);
1446 return;
1449 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1450 x_fprintf(x_stdout, "AF\n");
1451 ntlmssp_end(&client_ntlmssp_state);
1452 return;
1455 status = ntlmssp_update(client_ntlmssp_state,
1456 spnego.negTokenTarg.responseToken,
1457 &request);
1459 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1460 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1461 "ntlmssp_client_update, got: %s\n",
1462 nt_errstr(status)));
1463 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1464 "ntlmssp_client_update\n");
1465 data_blob_free(&request);
1466 ntlmssp_end(&client_ntlmssp_state);
1467 return;
1470 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1471 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1472 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1473 spnego.negTokenTarg.responseToken = request;
1474 spnego.negTokenTarg.mechListMIC = null_blob;
1476 write_spnego_data(&to_server, &spnego);
1477 data_blob_free(&request);
1479 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1480 data_blob_free(&to_server);
1481 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1482 TALLOC_FREE(to_server_base64);
1483 return;
1486 #ifdef HAVE_KRB5
1488 static bool manage_client_krb5_init(SPNEGO_DATA spnego)
1490 char *principal;
1491 DATA_BLOB tkt, to_server;
1492 DATA_BLOB session_key_krb5 = data_blob_null;
1493 SPNEGO_DATA reply;
1494 char *reply_base64;
1495 int retval;
1497 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1498 ssize_t len;
1500 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1501 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1502 DEBUG(1, ("Did not get a principal for krb5\n"));
1503 return False;
1506 principal = (char *)SMB_MALLOC(
1507 spnego.negTokenInit.mechListMIC.length+1);
1509 if (principal == NULL) {
1510 DEBUG(1, ("Could not malloc principal\n"));
1511 return False;
1514 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1515 spnego.negTokenInit.mechListMIC.length);
1516 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1518 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL);
1520 if (retval) {
1521 char *user = NULL;
1523 /* Let's try to first get the TGT, for that we need a
1524 password. */
1526 if (opt_password == NULL) {
1527 DEBUG(10, ("Requesting password\n"));
1528 x_fprintf(x_stdout, "PW\n");
1529 return True;
1532 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1533 if (!user) {
1534 return false;
1537 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1538 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1539 return False;
1542 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL);
1544 if (retval) {
1545 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1546 return False;
1550 data_blob_free(&session_key_krb5);
1552 ZERO_STRUCT(reply);
1554 reply.type = SPNEGO_NEG_TOKEN_INIT;
1555 reply.negTokenInit.mechTypes = my_mechs;
1556 reply.negTokenInit.reqFlags = 0;
1557 reply.negTokenInit.mechToken = tkt;
1558 reply.negTokenInit.mechListMIC = data_blob_null;
1560 len = write_spnego_data(&to_server, &reply);
1561 data_blob_free(&tkt);
1563 if (len == -1) {
1564 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1565 return False;
1568 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1569 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1571 TALLOC_FREE(reply_base64);
1572 data_blob_free(&to_server);
1573 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1574 return True;
1577 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
1579 switch (spnego.negTokenTarg.negResult) {
1580 case SPNEGO_ACCEPT_INCOMPLETE:
1581 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1582 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1583 "ACCEPT_INCOMPLETE\n");
1584 break;
1585 case SPNEGO_ACCEPT_COMPLETED:
1586 DEBUG(10, ("Accept completed\n"));
1587 x_fprintf(x_stdout, "AF\n");
1588 break;
1589 case SPNEGO_REJECT:
1590 DEBUG(10, ("Rejected\n"));
1591 x_fprintf(x_stdout, "NA\n");
1592 break;
1593 default:
1594 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1595 x_fprintf(x_stdout, "AF\n");
1599 #endif
1601 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1602 char *buf, int length)
1604 DATA_BLOB request;
1605 SPNEGO_DATA spnego;
1606 ssize_t len;
1608 if (!opt_username || !*opt_username) {
1609 x_fprintf(x_stderr, "username must be specified!\n\n");
1610 exit(1);
1613 if (strlen(buf) <= 3) {
1614 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1615 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1616 return;
1619 request = base64_decode_data_blob(buf+3);
1621 if (strncmp(buf, "PW ", 3) == 0) {
1623 /* We asked for a password and obviously got it :-) */
1625 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1627 if (opt_password == NULL) {
1628 DEBUG(1, ("Out of memory\n"));
1629 x_fprintf(x_stdout, "BH Out of memory\n");
1630 data_blob_free(&request);
1631 return;
1634 x_fprintf(x_stdout, "OK\n");
1635 data_blob_free(&request);
1636 return;
1639 if ( (strncmp(buf, "TT ", 3) != 0) &&
1640 (strncmp(buf, "AF ", 3) != 0) &&
1641 (strncmp(buf, "NA ", 3) != 0) ) {
1642 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1643 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1644 data_blob_free(&request);
1645 return;
1648 /* So we got a server challenge to generate a SPNEGO
1649 client-to-server request... */
1651 len = read_spnego_data(request, &spnego);
1652 data_blob_free(&request);
1654 if (len == -1) {
1655 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1656 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1657 return;
1660 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1662 /* The server offers a list of mechanisms */
1664 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1666 while (*mechType != NULL) {
1668 #ifdef HAVE_KRB5
1669 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1670 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1671 if (manage_client_krb5_init(spnego))
1672 goto out;
1674 #endif
1676 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1677 if (manage_client_ntlmssp_init(spnego))
1678 goto out;
1681 mechType++;
1684 DEBUG(1, ("Server offered no compatible mechanism\n"));
1685 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1686 return;
1689 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1691 if (spnego.negTokenTarg.supportedMech == NULL) {
1692 /* On accept/reject Windows does not send the
1693 mechanism anymore. Handle that here and
1694 shut down the mechanisms. */
1696 switch (spnego.negTokenTarg.negResult) {
1697 case SPNEGO_ACCEPT_COMPLETED:
1698 x_fprintf(x_stdout, "AF\n");
1699 break;
1700 case SPNEGO_REJECT:
1701 x_fprintf(x_stdout, "NA\n");
1702 break;
1703 default:
1704 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1705 "unknown negResult: %d\n",
1706 spnego.negTokenTarg.negResult));
1707 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1708 " no mech and an unknown "
1709 "negResult\n");
1712 ntlmssp_end(&client_ntlmssp_state);
1713 goto out;
1716 if (strcmp(spnego.negTokenTarg.supportedMech,
1717 OID_NTLMSSP) == 0) {
1718 manage_client_ntlmssp_targ(spnego);
1719 goto out;
1722 #if HAVE_KRB5
1723 if (strcmp(spnego.negTokenTarg.supportedMech,
1724 OID_KERBEROS5_OLD) == 0) {
1725 manage_client_krb5_targ(spnego);
1726 goto out;
1728 #endif
1732 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1733 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1734 return;
1736 out:
1737 free_spnego_data(&spnego);
1738 return;
1741 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1742 char *buf, int length)
1744 char *request, *parameter;
1745 static DATA_BLOB challenge;
1746 static DATA_BLOB lm_response;
1747 static DATA_BLOB nt_response;
1748 static char *full_username;
1749 static char *username;
1750 static char *domain;
1751 static char *plaintext_password;
1752 static bool ntlm_server_1_user_session_key;
1753 static bool ntlm_server_1_lm_session_key;
1755 if (strequal(buf, ".")) {
1756 if (!full_username && !username) {
1757 x_fprintf(x_stdout, "Error: No username supplied!\n");
1758 } else if (plaintext_password) {
1759 /* handle this request as plaintext */
1760 if (!full_username) {
1761 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1762 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1763 return;
1766 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1767 x_fprintf(x_stdout, "Authenticated: Yes\n");
1768 } else {
1769 x_fprintf(x_stdout, "Authenticated: No\n");
1771 } else if (!lm_response.data && !nt_response.data) {
1772 x_fprintf(x_stdout, "Error: No password supplied!\n");
1773 } else if (!challenge.data) {
1774 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1775 } else {
1776 char *error_string = NULL;
1777 uchar lm_key[8];
1778 uchar user_session_key[16];
1779 uint32 flags = 0;
1781 if (full_username && !username) {
1782 fstring fstr_user;
1783 fstring fstr_domain;
1785 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1786 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1787 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1789 SAFE_FREE(username);
1790 SAFE_FREE(domain);
1791 username = smb_xstrdup(fstr_user);
1792 domain = smb_xstrdup(fstr_domain);
1795 if (!domain) {
1796 domain = smb_xstrdup(get_winbind_domain());
1799 if (ntlm_server_1_lm_session_key)
1800 flags |= WBFLAG_PAM_LMKEY;
1802 if (ntlm_server_1_user_session_key)
1803 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1805 if (!NT_STATUS_IS_OK(
1806 contact_winbind_auth_crap(username,
1807 domain,
1808 global_myname(),
1809 &challenge,
1810 &lm_response,
1811 &nt_response,
1812 flags,
1813 lm_key,
1814 user_session_key,
1815 &error_string,
1816 NULL))) {
1818 x_fprintf(x_stdout, "Authenticated: No\n");
1819 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1820 } else {
1821 static char zeros[16];
1822 char *hex_lm_key;
1823 char *hex_user_session_key;
1825 x_fprintf(x_stdout, "Authenticated: Yes\n");
1827 if (ntlm_server_1_lm_session_key
1828 && (memcmp(zeros, lm_key,
1829 sizeof(lm_key)) != 0)) {
1830 hex_lm_key = hex_encode(NULL,
1831 (const unsigned char *)lm_key,
1832 sizeof(lm_key));
1833 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1834 TALLOC_FREE(hex_lm_key);
1837 if (ntlm_server_1_user_session_key
1838 && (memcmp(zeros, user_session_key,
1839 sizeof(user_session_key)) != 0)) {
1840 hex_user_session_key = hex_encode(NULL,
1841 (const unsigned char *)user_session_key,
1842 sizeof(user_session_key));
1843 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1844 TALLOC_FREE(hex_user_session_key);
1847 SAFE_FREE(error_string);
1849 /* clear out the state */
1850 challenge = data_blob_null;
1851 nt_response = data_blob_null;
1852 lm_response = data_blob_null;
1853 SAFE_FREE(full_username);
1854 SAFE_FREE(username);
1855 SAFE_FREE(domain);
1856 SAFE_FREE(plaintext_password);
1857 ntlm_server_1_user_session_key = False;
1858 ntlm_server_1_lm_session_key = False;
1859 x_fprintf(x_stdout, ".\n");
1861 return;
1864 request = buf;
1866 /* Indicates a base64 encoded structure */
1867 parameter = strstr_m(request, ":: ");
1868 if (!parameter) {
1869 parameter = strstr_m(request, ": ");
1871 if (!parameter) {
1872 DEBUG(0, ("Parameter not found!\n"));
1873 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1874 return;
1877 parameter[0] ='\0';
1878 parameter++;
1879 parameter[0] ='\0';
1880 parameter++;
1882 } else {
1883 parameter[0] ='\0';
1884 parameter++;
1885 parameter[0] ='\0';
1886 parameter++;
1887 parameter[0] ='\0';
1888 parameter++;
1890 base64_decode_inplace(parameter);
1893 if (strequal(request, "LANMAN-Challenge")) {
1894 challenge = strhex_to_data_blob(NULL, parameter);
1895 if (challenge.length != 8) {
1896 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1897 parameter,
1898 (int)challenge.length);
1899 challenge = data_blob_null;
1901 } else if (strequal(request, "NT-Response")) {
1902 nt_response = strhex_to_data_blob(NULL, parameter);
1903 if (nt_response.length < 24) {
1904 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1905 parameter,
1906 (int)nt_response.length);
1907 nt_response = data_blob_null;
1909 } else if (strequal(request, "LANMAN-Response")) {
1910 lm_response = strhex_to_data_blob(NULL, parameter);
1911 if (lm_response.length != 24) {
1912 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
1913 parameter,
1914 (int)lm_response.length);
1915 lm_response = data_blob_null;
1917 } else if (strequal(request, "Password")) {
1918 plaintext_password = smb_xstrdup(parameter);
1919 } else if (strequal(request, "NT-Domain")) {
1920 domain = smb_xstrdup(parameter);
1921 } else if (strequal(request, "Username")) {
1922 username = smb_xstrdup(parameter);
1923 } else if (strequal(request, "Full-Username")) {
1924 full_username = smb_xstrdup(parameter);
1925 } else if (strequal(request, "Request-User-Session-Key")) {
1926 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
1927 } else if (strequal(request, "Request-LanMan-Session-Key")) {
1928 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
1929 } else {
1930 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
1934 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
1935 char *buf, int length)
1937 char *request, *parameter;
1938 static DATA_BLOB new_nt_pswd;
1939 static DATA_BLOB old_nt_hash_enc;
1940 static DATA_BLOB new_lm_pswd;
1941 static DATA_BLOB old_lm_hash_enc;
1942 static char *full_username = NULL;
1943 static char *username = NULL;
1944 static char *domain = NULL;
1945 static char *newpswd = NULL;
1946 static char *oldpswd = NULL;
1948 if (strequal(buf, ".")) {
1949 if(newpswd && oldpswd) {
1950 uchar old_nt_hash[16];
1951 uchar old_lm_hash[16];
1952 uchar new_nt_hash[16];
1953 uchar new_lm_hash[16];
1955 new_nt_pswd = data_blob(NULL, 516);
1956 old_nt_hash_enc = data_blob(NULL, 16);
1958 /* Calculate the MD4 hash (NT compatible) of the
1959 * password */
1960 E_md4hash(oldpswd, old_nt_hash);
1961 E_md4hash(newpswd, new_nt_hash);
1963 /* E_deshash returns false for 'long'
1964 passwords (> 14 DOS chars).
1966 Therefore, don't send a buffer
1967 encrypted with the truncated hash
1968 (it could allow an even easier
1969 attack on the password)
1971 Likewise, obey the admin's restriction
1974 if (lp_client_lanman_auth() &&
1975 E_deshash(newpswd, new_lm_hash) &&
1976 E_deshash(oldpswd, old_lm_hash)) {
1977 new_lm_pswd = data_blob(NULL, 516);
1978 old_lm_hash_enc = data_blob(NULL, 16);
1979 encode_pw_buffer(new_lm_pswd.data, newpswd,
1980 STR_UNICODE);
1982 SamOEMhash(new_lm_pswd.data, old_nt_hash, 516);
1983 E_old_pw_hash(new_nt_hash, old_lm_hash,
1984 old_lm_hash_enc.data);
1985 } else {
1986 new_lm_pswd.data = NULL;
1987 new_lm_pswd.length = 0;
1988 old_lm_hash_enc.data = NULL;
1989 old_lm_hash_enc.length = 0;
1992 encode_pw_buffer(new_nt_pswd.data, newpswd,
1993 STR_UNICODE);
1995 SamOEMhash(new_nt_pswd.data, old_nt_hash, 516);
1996 E_old_pw_hash(new_nt_hash, old_nt_hash,
1997 old_nt_hash_enc.data);
2000 if (!full_username && !username) {
2001 x_fprintf(x_stdout, "Error: No username supplied!\n");
2002 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2003 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2004 x_fprintf(x_stdout, "Error: No NT or LM password "
2005 "blobs supplied!\n");
2006 } else {
2007 char *error_string = NULL;
2009 if (full_username && !username) {
2010 fstring fstr_user;
2011 fstring fstr_domain;
2013 if (!parse_ntlm_auth_domain_user(full_username,
2014 fstr_user,
2015 fstr_domain)) {
2016 /* username might be 'tainted', don't
2017 * print into our new-line
2018 * deleimianted stream */
2019 x_fprintf(x_stdout, "Error: Could not "
2020 "parse into domain and "
2021 "username\n");
2022 SAFE_FREE(username);
2023 username = smb_xstrdup(full_username);
2024 } else {
2025 SAFE_FREE(username);
2026 SAFE_FREE(domain);
2027 username = smb_xstrdup(fstr_user);
2028 domain = smb_xstrdup(fstr_domain);
2033 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2034 username, domain,
2035 new_nt_pswd,
2036 old_nt_hash_enc,
2037 new_lm_pswd,
2038 old_lm_hash_enc,
2039 &error_string))) {
2040 x_fprintf(x_stdout, "Password-Change: No\n");
2041 x_fprintf(x_stdout, "Password-Change-Error: "
2042 "%s\n.\n", error_string);
2043 } else {
2044 x_fprintf(x_stdout, "Password-Change: Yes\n");
2047 SAFE_FREE(error_string);
2049 /* clear out the state */
2050 new_nt_pswd = data_blob_null;
2051 old_nt_hash_enc = data_blob_null;
2052 new_lm_pswd = data_blob_null;
2053 old_nt_hash_enc = data_blob_null;
2054 SAFE_FREE(full_username);
2055 SAFE_FREE(username);
2056 SAFE_FREE(domain);
2057 SAFE_FREE(newpswd);
2058 SAFE_FREE(oldpswd);
2059 x_fprintf(x_stdout, ".\n");
2061 return;
2064 request = buf;
2066 /* Indicates a base64 encoded structure */
2067 parameter = strstr_m(request, ":: ");
2068 if (!parameter) {
2069 parameter = strstr_m(request, ": ");
2071 if (!parameter) {
2072 DEBUG(0, ("Parameter not found!\n"));
2073 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2074 return;
2077 parameter[0] ='\0';
2078 parameter++;
2079 parameter[0] ='\0';
2080 parameter++;
2081 } else {
2082 parameter[0] ='\0';
2083 parameter++;
2084 parameter[0] ='\0';
2085 parameter++;
2086 parameter[0] ='\0';
2087 parameter++;
2089 base64_decode_inplace(parameter);
2092 if (strequal(request, "new-nt-password-blob")) {
2093 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2094 if (new_nt_pswd.length != 516) {
2095 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2096 "(got %d bytes, expected 516)\n.\n",
2097 parameter,
2098 (int)new_nt_pswd.length);
2099 new_nt_pswd = data_blob_null;
2101 } else if (strequal(request, "old-nt-hash-blob")) {
2102 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2103 if (old_nt_hash_enc.length != 16) {
2104 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2105 "(got %d bytes, expected 16)\n.\n",
2106 parameter,
2107 (int)old_nt_hash_enc.length);
2108 old_nt_hash_enc = data_blob_null;
2110 } else if (strequal(request, "new-lm-password-blob")) {
2111 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2112 if (new_lm_pswd.length != 516) {
2113 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2114 "(got %d bytes, expected 516)\n.\n",
2115 parameter,
2116 (int)new_lm_pswd.length);
2117 new_lm_pswd = data_blob_null;
2120 else if (strequal(request, "old-lm-hash-blob")) {
2121 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2122 if (old_lm_hash_enc.length != 16)
2124 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2125 "(got %d bytes, expected 16)\n.\n",
2126 parameter,
2127 (int)old_lm_hash_enc.length);
2128 old_lm_hash_enc = data_blob_null;
2130 } else if (strequal(request, "nt-domain")) {
2131 domain = smb_xstrdup(parameter);
2132 } else if(strequal(request, "username")) {
2133 username = smb_xstrdup(parameter);
2134 } else if(strequal(request, "full-username")) {
2135 username = smb_xstrdup(parameter);
2136 } else if(strequal(request, "new-password")) {
2137 newpswd = smb_xstrdup(parameter);
2138 } else if (strequal(request, "old-password")) {
2139 oldpswd = smb_xstrdup(parameter);
2140 } else {
2141 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2145 static void manage_squid_request(struct ntlm_auth_state *state,
2146 stdio_helper_function fn)
2148 char *buf;
2149 char tmp[INITIAL_BUFFER_SIZE+1];
2150 int length, buf_size = 0;
2151 char *c;
2153 buf = talloc_strdup(state->mem_ctx, "");
2154 if (!buf) {
2155 DEBUG(0, ("Failed to allocate input buffer.\n"));
2156 x_fprintf(x_stderr, "ERR\n");
2157 exit(1);
2160 do {
2162 /* this is not a typo - x_fgets doesn't work too well under
2163 * squid */
2164 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2165 if (ferror(stdin)) {
2166 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2167 "(%s)\n", ferror(stdin),
2168 strerror(ferror(stdin))));
2170 exit(1);
2172 exit(0);
2175 buf = talloc_strdup_append_buffer(buf, tmp);
2176 buf_size += INITIAL_BUFFER_SIZE;
2178 if (buf_size > MAX_BUFFER_SIZE) {
2179 DEBUG(2, ("Oversized message\n"));
2180 x_fprintf(x_stderr, "ERR\n");
2181 talloc_free(buf);
2182 return;
2185 c = strchr(buf, '\n');
2186 } while (c == NULL);
2188 *c = '\0';
2189 length = c-buf;
2191 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2193 if (buf[0] == '\0') {
2194 DEBUG(2, ("Invalid Request\n"));
2195 x_fprintf(x_stderr, "ERR\n");
2196 talloc_free(buf);
2197 return;
2200 fn(state, buf, length);
2201 talloc_free(buf);
2205 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2206 TALLOC_CTX *mem_ctx;
2207 struct ntlm_auth_state *state;
2209 /* initialize FDescs */
2210 x_setbuf(x_stdout, NULL);
2211 x_setbuf(x_stderr, NULL);
2213 mem_ctx = talloc_init("ntlm_auth");
2214 if (!mem_ctx) {
2215 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2216 x_fprintf(x_stderr, "ERR\n");
2217 exit(1);
2220 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2221 if (!state) {
2222 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2223 x_fprintf(x_stderr, "ERR\n");
2224 exit(1);
2227 state->mem_ctx = mem_ctx;
2228 state->helper_mode = stdio_mode;
2230 while(1) {
2231 manage_squid_request(state, fn);
2236 /* Authenticate a user with a challenge/response */
2238 static bool check_auth_crap(void)
2240 NTSTATUS nt_status;
2241 uint32 flags = 0;
2242 char lm_key[8];
2243 char user_session_key[16];
2244 char *hex_lm_key;
2245 char *hex_user_session_key;
2246 char *error_string;
2247 static uint8 zeros[16];
2249 x_setbuf(x_stdout, NULL);
2251 if (request_lm_key)
2252 flags |= WBFLAG_PAM_LMKEY;
2254 if (request_user_session_key)
2255 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2257 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2259 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2260 opt_workstation,
2261 &opt_challenge,
2262 &opt_lm_response,
2263 &opt_nt_response,
2264 flags,
2265 (unsigned char *)lm_key,
2266 (unsigned char *)user_session_key,
2267 &error_string, NULL);
2269 if (!NT_STATUS_IS_OK(nt_status)) {
2270 x_fprintf(x_stdout, "%s (0x%x)\n",
2271 error_string,
2272 NT_STATUS_V(nt_status));
2273 SAFE_FREE(error_string);
2274 return False;
2277 if (request_lm_key
2278 && (memcmp(zeros, lm_key,
2279 sizeof(lm_key)) != 0)) {
2280 hex_lm_key = hex_encode(NULL, (const unsigned char *)lm_key,
2281 sizeof(lm_key));
2282 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2283 TALLOC_FREE(hex_lm_key);
2285 if (request_user_session_key
2286 && (memcmp(zeros, user_session_key,
2287 sizeof(user_session_key)) != 0)) {
2288 hex_user_session_key = hex_encode(NULL, (const unsigned char *)user_session_key,
2289 sizeof(user_session_key));
2290 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2291 TALLOC_FREE(hex_user_session_key);
2294 return True;
2297 /* Main program */
2299 enum {
2300 OPT_USERNAME = 1000,
2301 OPT_DOMAIN,
2302 OPT_WORKSTATION,
2303 OPT_CHALLENGE,
2304 OPT_RESPONSE,
2305 OPT_LM,
2306 OPT_NT,
2307 OPT_PASSWORD,
2308 OPT_LM_KEY,
2309 OPT_USER_SESSION_KEY,
2310 OPT_DIAGNOSTICS,
2311 OPT_REQUIRE_MEMBERSHIP,
2312 OPT_USE_CACHED_CREDS
2315 int main(int argc, const char **argv)
2317 TALLOC_CTX *frame = talloc_stackframe();
2318 int opt;
2319 static const char *helper_protocol;
2320 static int diagnostics;
2322 static const char *hex_challenge;
2323 static const char *hex_lm_response;
2324 static const char *hex_nt_response;
2326 poptContext pc;
2328 /* NOTE: DO NOT change this interface without considering the implications!
2329 This is an external interface, which other programs will use to interact
2330 with this helper.
2333 /* We do not use single-letter command abbreviations, because they harm future
2334 interface stability. */
2336 struct poptOption long_options[] = {
2337 POPT_AUTOHELP
2338 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2339 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2340 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2341 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2342 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2343 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2344 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2345 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2346 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2347 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2348 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2349 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2350 { "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" },
2351 POPT_COMMON_CONFIGFILE
2352 POPT_COMMON_VERSION
2353 POPT_TABLEEND
2356 /* Samba client initialisation */
2357 load_case_tables();
2359 dbf = x_stderr;
2361 /* Parse options */
2363 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2365 /* Parse command line options */
2367 if (argc == 1) {
2368 poptPrintHelp(pc, stderr, 0);
2369 return 1;
2372 while((opt = poptGetNextOpt(pc)) != -1) {
2373 /* Get generic config options like --configfile */
2376 poptFreeContext(pc);
2378 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2379 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2380 get_dyn_CONFIGFILE(), strerror(errno));
2381 exit(1);
2384 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2385 POPT_CONTEXT_KEEP_FIRST);
2387 while((opt = poptGetNextOpt(pc)) != -1) {
2388 switch (opt) {
2389 case OPT_CHALLENGE:
2390 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2391 if (opt_challenge.length != 8) {
2392 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2393 hex_challenge,
2394 (int)opt_challenge.length);
2395 exit(1);
2397 break;
2398 case OPT_LM:
2399 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2400 if (opt_lm_response.length != 24) {
2401 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2402 hex_lm_response,
2403 (int)opt_lm_response.length);
2404 exit(1);
2406 break;
2408 case OPT_NT:
2409 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2410 if (opt_nt_response.length < 24) {
2411 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2412 hex_nt_response,
2413 (int)opt_nt_response.length);
2414 exit(1);
2416 break;
2418 case OPT_REQUIRE_MEMBERSHIP:
2419 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2420 require_membership_of_sid = require_membership_of;
2422 break;
2426 if (opt_username) {
2427 char *domain = SMB_STRDUP(opt_username);
2428 char *p = strchr_m(domain, *lp_winbind_separator());
2429 if (p) {
2430 opt_username = p+1;
2431 *p = '\0';
2432 if (opt_domain && !strequal(opt_domain, domain)) {
2433 x_fprintf(x_stderr, "Domain specified in username (%s) "
2434 "doesn't match specified domain (%s)!\n\n",
2435 domain, opt_domain);
2436 poptPrintHelp(pc, stderr, 0);
2437 exit(1);
2439 opt_domain = domain;
2440 } else {
2441 SAFE_FREE(domain);
2445 /* Note: if opt_domain is "" then send no domain */
2446 if (opt_domain == NULL) {
2447 opt_domain = get_winbind_domain();
2450 if (opt_workstation == NULL) {
2451 opt_workstation = "";
2454 if (helper_protocol) {
2455 int i;
2456 for (i=0; i<NUM_HELPER_MODES; i++) {
2457 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2458 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2459 exit(0);
2462 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2464 for (i=0; i<NUM_HELPER_MODES; i++) {
2465 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2468 exit(1);
2471 if (!opt_username || !*opt_username) {
2472 x_fprintf(x_stderr, "username must be specified!\n\n");
2473 poptPrintHelp(pc, stderr, 0);
2474 exit(1);
2477 if (opt_challenge.length) {
2478 if (!check_auth_crap()) {
2479 exit(1);
2481 exit(0);
2484 if (!opt_password) {
2485 opt_password = getpass("password: ");
2488 if (diagnostics) {
2489 if (!diagnose_ntlm_auth()) {
2490 return 1;
2492 } else {
2493 fstring user;
2495 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2496 if (!check_plaintext_auth(user, opt_password, True)) {
2497 return 1;
2501 /* Exit code */
2503 poptFreeContext(pc);
2504 TALLOC_FREE(frame);
2505 return 0;