s3 ntlm_auth: Don't malloc data that will be talloc_free()d
[Samba/ekacnet.git] / source3 / utils / ntlm_auth.c
blob9bd7b5af66a3d33ddd8722a61a3cdeaf9759dbec
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 "../libcli/auth/libcli_auth.h"
29 #include "../libcli/auth/spnego.h"
30 #include "ntlmssp.h"
31 #include "smb_krb5.h"
32 #include <iniparser.h>
34 #ifndef PAM_WINBIND_CONFIG_FILE
35 #define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
36 #endif
38 #define WINBIND_KRB5_AUTH 0x00000080
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_WINBIND
43 #define INITIAL_BUFFER_SIZE 300
44 #define MAX_BUFFER_SIZE 630000
46 enum stdio_helper_mode {
47 SQUID_2_4_BASIC,
48 SQUID_2_5_BASIC,
49 SQUID_2_5_NTLMSSP,
50 NTLMSSP_CLIENT_1,
51 GSS_SPNEGO,
52 GSS_SPNEGO_CLIENT,
53 NTLM_SERVER_1,
54 NTLM_CHANGE_PASSWORD_1,
55 NUM_HELPER_MODES
58 enum ntlm_auth_cli_state {
59 CLIENT_INITIAL = 0,
60 CLIENT_RESPONSE,
61 CLIENT_FINISHED,
62 CLIENT_ERROR
65 enum ntlm_auth_svr_state {
66 SERVER_INITIAL = 0,
67 SERVER_CHALLENGE,
68 SERVER_FINISHED,
69 SERVER_ERROR
72 struct ntlm_auth_state {
73 TALLOC_CTX *mem_ctx;
74 enum stdio_helper_mode helper_mode;
75 enum ntlm_auth_cli_state cli_state;
76 enum ntlm_auth_svr_state svr_state;
77 struct ntlmssp_state *ntlmssp_state;
78 uint32_t neg_flags;
79 char *want_feature_list;
80 bool have_session_key;
81 DATA_BLOB session_key;
82 DATA_BLOB initial_message;
85 typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
86 int length);
88 static void manage_squid_basic_request (struct ntlm_auth_state *state,
89 char *buf, int length);
91 static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
92 char *buf, int length);
94 static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
95 char *buf, int length);
97 static void manage_gss_spnego_request (struct ntlm_auth_state *state,
98 char *buf, int length);
100 static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
101 char *buf, int length);
103 static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
104 char *buf, int length);
106 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
107 char *buf, int length);
109 static const struct {
110 enum stdio_helper_mode mode;
111 const char *name;
112 stdio_helper_function fn;
113 } stdio_helper_protocols[] = {
114 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
115 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
116 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
117 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
118 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
119 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
120 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
121 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
122 { NUM_HELPER_MODES, NULL, NULL}
125 const char *opt_username;
126 const char *opt_domain;
127 const char *opt_workstation;
128 const char *opt_password;
129 static DATA_BLOB opt_challenge;
130 static DATA_BLOB opt_lm_response;
131 static DATA_BLOB opt_nt_response;
132 static int request_lm_key;
133 static int request_user_session_key;
134 static int use_cached_creds;
136 static const char *require_membership_of;
137 static const char *require_membership_of_sid;
138 static const char *opt_pam_winbind_conf;
140 static char winbind_separator(void)
142 struct winbindd_response response;
143 static bool got_sep;
144 static char sep;
146 if (got_sep)
147 return sep;
149 ZERO_STRUCT(response);
151 /* Send off request */
153 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
154 NSS_STATUS_SUCCESS) {
155 d_printf("could not obtain winbind separator!\n");
156 return *lp_winbind_separator();
159 sep = response.data.info.winbind_separator;
160 got_sep = True;
162 if (!sep) {
163 d_printf("winbind separator was NULL!\n");
164 return *lp_winbind_separator();
167 return sep;
170 const char *get_winbind_domain(void)
172 struct winbindd_response response;
174 static fstring winbind_domain;
175 if (*winbind_domain) {
176 return winbind_domain;
179 ZERO_STRUCT(response);
181 /* Send off request */
183 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
184 NSS_STATUS_SUCCESS) {
185 DEBUG(0, ("could not obtain winbind domain name!\n"));
186 return lp_workgroup();
189 fstrcpy(winbind_domain, response.data.domain_name);
191 return winbind_domain;
195 const char *get_winbind_netbios_name(void)
197 struct winbindd_response response;
199 static fstring winbind_netbios_name;
201 if (*winbind_netbios_name) {
202 return winbind_netbios_name;
205 ZERO_STRUCT(response);
207 /* Send off request */
209 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
210 NSS_STATUS_SUCCESS) {
211 DEBUG(0, ("could not obtain winbind netbios name!\n"));
212 return global_myname();
215 fstrcpy(winbind_netbios_name, response.data.netbios_name);
217 return winbind_netbios_name;
221 DATA_BLOB get_challenge(void)
223 static DATA_BLOB chal;
224 if (opt_challenge.length)
225 return opt_challenge;
227 chal = data_blob(NULL, 8);
229 generate_random_buffer(chal.data, chal.length);
230 return chal;
233 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
234 form DOMAIN/user into a domain and a user */
236 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
237 fstring user)
240 char *p = strchr(domuser,winbind_separator());
242 if (!p) {
243 return False;
246 fstrcpy(user, p+1);
247 fstrcpy(domain, domuser);
248 domain[PTR_DIFF(p, domuser)] = 0;
249 strupper_m(domain);
251 return True;
254 static bool get_require_membership_sid(void) {
255 struct winbindd_request request;
256 struct winbindd_response response;
258 if (!require_membership_of) {
259 return True;
262 if (require_membership_of_sid) {
263 return True;
266 /* Otherwise, ask winbindd for the name->sid request */
268 ZERO_STRUCT(request);
269 ZERO_STRUCT(response);
271 if (!parse_ntlm_auth_domain_user(require_membership_of,
272 request.data.name.dom_name,
273 request.data.name.name)) {
274 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
275 require_membership_of));
276 return False;
279 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
280 NSS_STATUS_SUCCESS) {
281 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
282 require_membership_of));
283 return False;
286 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
288 if (require_membership_of_sid)
289 return True;
291 return False;
295 * Get some configuration from pam_winbind.conf to see if we
296 * need to contact trusted domain
299 int get_pam_winbind_config()
301 int ctrl = 0;
302 dictionary *d = NULL;
304 if (!opt_pam_winbind_conf || !*opt_pam_winbind_conf) {
305 opt_pam_winbind_conf = PAM_WINBIND_CONFIG_FILE;
308 d = iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf));
310 if (!d) {
311 return 0;
314 if (iniparser_getboolean(d, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
315 ctrl |= WINBIND_KRB5_AUTH;
318 iniparser_freedict(d);
320 return ctrl;
323 /* Authenticate a user with a plaintext password */
325 static bool check_plaintext_auth(const char *user, const char *pass,
326 bool stdout_diagnostics)
328 struct winbindd_request request;
329 struct winbindd_response response;
330 NSS_STATUS result;
332 if (!get_require_membership_sid()) {
333 return False;
336 /* Send off request */
338 ZERO_STRUCT(request);
339 ZERO_STRUCT(response);
341 fstrcpy(request.data.auth.user, user);
342 fstrcpy(request.data.auth.pass, pass);
343 if (require_membership_of_sid) {
344 strlcpy(request.data.auth.require_membership_of_sid,
345 require_membership_of_sid,
346 sizeof(request.data.auth.require_membership_of_sid));
349 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
351 /* Display response */
353 if (stdout_diagnostics) {
354 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
355 d_printf("Reading winbind reply failed! (0x01)\n");
358 d_printf("%s: %s (0x%x)\n",
359 response.data.auth.nt_status_string,
360 response.data.auth.error_string,
361 response.data.auth.nt_status);
362 } else {
363 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
364 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
367 DEBUG(3, ("%s: %s (0x%x)\n",
368 response.data.auth.nt_status_string,
369 response.data.auth.error_string,
370 response.data.auth.nt_status));
373 return (result == NSS_STATUS_SUCCESS);
376 /* authenticate a user with an encrypted username/password */
378 NTSTATUS contact_winbind_auth_crap(const char *username,
379 const char *domain,
380 const char *workstation,
381 const DATA_BLOB *challenge,
382 const DATA_BLOB *lm_response,
383 const DATA_BLOB *nt_response,
384 uint32 flags,
385 uint8 lm_key[8],
386 uint8 user_session_key[16],
387 char **error_string,
388 char **unix_name)
390 NTSTATUS nt_status;
391 NSS_STATUS result;
392 struct winbindd_request request;
393 struct winbindd_response response;
395 if (!get_require_membership_sid()) {
396 return NT_STATUS_INVALID_PARAMETER;
399 ZERO_STRUCT(request);
400 ZERO_STRUCT(response);
402 request.flags = flags;
404 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
406 if (require_membership_of_sid)
407 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
409 fstrcpy(request.data.auth_crap.user, username);
410 fstrcpy(request.data.auth_crap.domain, domain);
412 fstrcpy(request.data.auth_crap.workstation,
413 workstation);
415 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
417 if (lm_response && lm_response->length) {
418 memcpy(request.data.auth_crap.lm_resp,
419 lm_response->data,
420 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
421 request.data.auth_crap.lm_resp_len = lm_response->length;
424 if (nt_response && nt_response->length) {
425 if (nt_response->length > sizeof(request.data.auth_crap.nt_resp)) {
426 request.flags = request.flags | WBFLAG_BIG_NTLMV2_BLOB;
427 request.extra_len = nt_response->length;
428 request.extra_data.data = SMB_MALLOC_ARRAY(char, request.extra_len);
429 if (request.extra_data.data == NULL) {
430 return NT_STATUS_NO_MEMORY;
432 memcpy(request.extra_data.data, nt_response->data,
433 nt_response->length);
435 } else {
436 memcpy(request.data.auth_crap.nt_resp,
437 nt_response->data, nt_response->length);
439 request.data.auth_crap.nt_resp_len = nt_response->length;
442 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
443 SAFE_FREE(request.extra_data.data);
445 /* Display response */
447 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
448 nt_status = NT_STATUS_UNSUCCESSFUL;
449 if (error_string)
450 *error_string = smb_xstrdup("Reading winbind reply failed!");
451 winbindd_free_response(&response);
452 return nt_status;
455 nt_status = (NT_STATUS(response.data.auth.nt_status));
456 if (!NT_STATUS_IS_OK(nt_status)) {
457 if (error_string)
458 *error_string = smb_xstrdup(response.data.auth.error_string);
459 winbindd_free_response(&response);
460 return nt_status;
463 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
464 memcpy(lm_key, response.data.auth.first_8_lm_hash,
465 sizeof(response.data.auth.first_8_lm_hash));
467 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
468 memcpy(user_session_key, response.data.auth.user_session_key,
469 sizeof(response.data.auth.user_session_key));
472 if (flags & WBFLAG_PAM_UNIX_NAME) {
473 *unix_name = SMB_STRDUP(response.data.auth.unix_username);
474 if (!*unix_name) {
475 winbindd_free_response(&response);
476 return NT_STATUS_NO_MEMORY;
480 winbindd_free_response(&response);
481 return nt_status;
484 /* contact server to change user password using auth crap */
485 static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
486 const char *domain,
487 const DATA_BLOB new_nt_pswd,
488 const DATA_BLOB old_nt_hash_enc,
489 const DATA_BLOB new_lm_pswd,
490 const DATA_BLOB old_lm_hash_enc,
491 char **error_string)
493 NTSTATUS nt_status;
494 NSS_STATUS result;
495 struct winbindd_request request;
496 struct winbindd_response response;
498 if (!get_require_membership_sid())
500 if(error_string)
501 *error_string = smb_xstrdup("Can't get membership sid.");
502 return NT_STATUS_INVALID_PARAMETER;
505 ZERO_STRUCT(request);
506 ZERO_STRUCT(response);
508 if(username != NULL)
509 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
510 if(domain != NULL)
511 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
513 if(new_nt_pswd.length)
515 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
516 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
519 if(old_nt_hash_enc.length)
521 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));
522 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
525 if(new_lm_pswd.length)
527 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
528 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
531 if(old_lm_hash_enc.length)
533 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));
534 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
537 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
539 /* Display response */
541 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
543 nt_status = NT_STATUS_UNSUCCESSFUL;
544 if (error_string)
545 *error_string = smb_xstrdup("Reading winbind reply failed!");
546 winbindd_free_response(&response);
547 return nt_status;
550 nt_status = (NT_STATUS(response.data.auth.nt_status));
551 if (!NT_STATUS_IS_OK(nt_status))
553 if (error_string)
554 *error_string = smb_xstrdup(response.data.auth.error_string);
555 winbindd_free_response(&response);
556 return nt_status;
559 winbindd_free_response(&response);
561 return nt_status;
564 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
566 static const char zeros[16] = { 0, };
567 NTSTATUS nt_status;
568 char *error_string = NULL;
569 uint8 lm_key[8];
570 uint8 user_sess_key[16];
571 char *unix_name = NULL;
573 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
574 ntlmssp_state->client.netbios_name,
575 &ntlmssp_state->chal,
576 &ntlmssp_state->lm_resp,
577 &ntlmssp_state->nt_resp,
578 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
579 lm_key, user_sess_key,
580 &error_string, &unix_name);
582 if (NT_STATUS_IS_OK(nt_status)) {
583 if (memcmp(lm_key, zeros, 8) != 0) {
584 *lm_session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
585 memcpy(lm_session_key->data, lm_key, 8);
586 memset(lm_session_key->data+8, '\0', 8);
589 if (memcmp(user_sess_key, zeros, 16) != 0) {
590 *user_session_key = data_blob_talloc(ntlmssp_state, user_sess_key, 16);
592 ntlmssp_state->callback_private = talloc_strdup(ntlmssp_state,
593 unix_name);
594 } else {
595 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
596 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
597 ntlmssp_state->domain, ntlmssp_state->user,
598 ntlmssp_state->client.netbios_name,
599 error_string ? error_string : "unknown error (NULL)"));
600 ntlmssp_state->callback_private = NULL;
603 SAFE_FREE(error_string);
604 SAFE_FREE(unix_name);
605 return nt_status;
608 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
610 NTSTATUS nt_status;
611 struct samr_Password lm_pw, nt_pw;
613 nt_lm_owf_gen (opt_password, nt_pw.hash, lm_pw.hash);
615 nt_status = ntlm_password_check(ntlmssp_state,
616 true, true, 0,
617 &ntlmssp_state->chal,
618 &ntlmssp_state->lm_resp,
619 &ntlmssp_state->nt_resp,
620 ntlmssp_state->user,
621 ntlmssp_state->user,
622 ntlmssp_state->domain,
623 &lm_pw, &nt_pw, user_session_key, lm_session_key);
625 if (NT_STATUS_IS_OK(nt_status)) {
626 ntlmssp_state->callback_private = talloc_asprintf(ntlmssp_state,
627 "%s%c%s", ntlmssp_state->domain,
628 *lp_winbind_separator(),
629 ntlmssp_state->user);
630 } else {
631 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
632 ntlmssp_state->domain, ntlmssp_state->user,
633 ntlmssp_state->client.netbios_name,
634 nt_errstr(nt_status)));
635 ntlmssp_state->callback_private = NULL;
637 return nt_status;
640 static NTSTATUS ntlm_auth_start_ntlmssp_client(struct ntlmssp_state **client_ntlmssp_state)
642 NTSTATUS status;
643 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
644 status = NT_STATUS_UNSUCCESSFUL;
645 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
646 return NT_STATUS_INVALID_PARAMETER;
649 status = ntlmssp_client_start(NULL,
650 global_myname(),
651 lp_workgroup(),
652 lp_client_ntlmv2_auth(),
653 client_ntlmssp_state);
655 if (!NT_STATUS_IS_OK(status)) {
656 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
657 nt_errstr(status)));
658 ntlmssp_end(client_ntlmssp_state);
659 return status;
662 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
664 if (!NT_STATUS_IS_OK(status)) {
665 DEBUG(1, ("Could not set username: %s\n",
666 nt_errstr(status)));
667 ntlmssp_end(client_ntlmssp_state);
668 return status;
671 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
673 if (!NT_STATUS_IS_OK(status)) {
674 DEBUG(1, ("Could not set domain: %s\n",
675 nt_errstr(status)));
676 ntlmssp_end(client_ntlmssp_state);
677 return status;
680 if (opt_password) {
681 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
683 if (!NT_STATUS_IS_OK(status)) {
684 DEBUG(1, ("Could not set password: %s\n",
685 nt_errstr(status)));
686 ntlmssp_end(client_ntlmssp_state);
687 return status;
691 return NT_STATUS_OK;
694 static NTSTATUS ntlm_auth_start_ntlmssp_server(struct ntlmssp_state **ntlmssp_state)
696 NTSTATUS status;
697 const char *netbios_name;
698 const char *netbios_domain;
699 const char *dns_name;
700 char *dns_domain;
701 bool is_standalone = false;
703 if (opt_password) {
704 netbios_name = global_myname();
705 netbios_domain = lp_workgroup();
706 } else {
707 netbios_name = get_winbind_netbios_name();
708 netbios_domain = get_winbind_domain();
710 /* This should be a 'netbios domain -> DNS domain' mapping */
711 dns_domain = get_mydnsdomname(talloc_tos());
712 if (dns_domain) {
713 strlower_m(dns_domain);
715 dns_name = get_mydnsfullname();
717 status = ntlmssp_server_start(NULL,
718 is_standalone,
719 netbios_name,
720 netbios_domain,
721 dns_name,
722 dns_domain,
723 ntlmssp_state);
724 if (!NT_STATUS_IS_OK(status)) {
725 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
726 nt_errstr(status)));
727 return status;
730 /* Have we been given a local password, or should we ask winbind? */
731 if (opt_password) {
732 (*ntlmssp_state)->check_password = local_pw_check;
733 } else {
734 (*ntlmssp_state)->check_password = winbind_pw_check;
736 return NT_STATUS_OK;
739 /*******************************************************************
740 Used by firefox to drive NTLM auth to IIS servers.
741 *******************************************************************/
743 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
744 DATA_BLOB *reply)
746 struct winbindd_request wb_request;
747 struct winbindd_response wb_response;
748 int ctrl = 0;
749 NSS_STATUS result;
751 /* get winbindd to do the ntlmssp step on our behalf */
752 ZERO_STRUCT(wb_request);
753 ZERO_STRUCT(wb_response);
756 * This is tricky here. If we set krb5_auth in pam_winbind.conf
757 * creds for users in trusted domain will be stored the winbindd
758 * child of the trusted domain. If we ask the primary domain for
759 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
760 * domain's child for ccache_ntlm_auth. that is to say, we have to
761 * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
763 ctrl = get_pam_winbind_config();
765 if (ctrl | WINBIND_KRB5_AUTH) {
766 wb_request.flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
769 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
770 "%s%c%s", opt_domain, winbind_separator(), opt_username);
771 wb_request.data.ccache_ntlm_auth.uid = geteuid();
772 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
773 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
774 wb_request.extra_len = initial_msg.length + challenge_msg.length;
776 if (wb_request.extra_len > 0) {
777 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
778 if (wb_request.extra_data.data == NULL) {
779 return NT_STATUS_NO_MEMORY;
782 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
783 memcpy(wb_request.extra_data.data + initial_msg.length,
784 challenge_msg.data, challenge_msg.length);
787 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
788 SAFE_FREE(wb_request.extra_data.data);
790 if (result != NSS_STATUS_SUCCESS) {
791 winbindd_free_response(&wb_response);
792 return NT_STATUS_UNSUCCESSFUL;
795 if (reply) {
796 *reply = data_blob(wb_response.extra_data.data,
797 wb_response.data.ccache_ntlm_auth.auth_blob_len);
798 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
799 reply->data == NULL) {
800 winbindd_free_response(&wb_response);
801 return NT_STATUS_NO_MEMORY;
805 winbindd_free_response(&wb_response);
806 return NT_STATUS_MORE_PROCESSING_REQUIRED;
809 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
810 char *buf, int length)
812 DATA_BLOB request, reply;
813 NTSTATUS nt_status;
815 if (strlen(buf) < 2) {
816 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
817 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
818 return;
821 if (strlen(buf) > 3) {
822 if(strncmp(buf, "SF ", 3) == 0){
823 DEBUG(10, ("Setting flags to negotioate\n"));
824 TALLOC_FREE(state->want_feature_list);
825 state->want_feature_list = talloc_strdup(state->mem_ctx,
826 buf+3);
827 x_fprintf(x_stdout, "OK\n");
828 return;
830 request = base64_decode_data_blob(buf + 3);
831 } else {
832 request = data_blob_null;
835 if ((strncmp(buf, "PW ", 3) == 0)) {
836 /* The calling application wants us to use a local password
837 * (rather than winbindd) */
839 opt_password = SMB_STRNDUP((const char *)request.data,
840 request.length);
842 if (opt_password == NULL) {
843 DEBUG(1, ("Out of memory\n"));
844 x_fprintf(x_stdout, "BH Out of memory\n");
845 data_blob_free(&request);
846 return;
849 x_fprintf(x_stdout, "OK\n");
850 data_blob_free(&request);
851 return;
854 if (strncmp(buf, "YR", 2) == 0) {
855 if (state->ntlmssp_state)
856 ntlmssp_end(&state->ntlmssp_state);
857 state->svr_state = SERVER_INITIAL;
858 } else if (strncmp(buf, "KK", 2) == 0) {
859 /* No special preprocessing required */
860 } else if (strncmp(buf, "GF", 2) == 0) {
861 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
863 if (state->svr_state == SERVER_FINISHED) {
864 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
866 else {
867 x_fprintf(x_stdout, "BH\n");
869 data_blob_free(&request);
870 return;
871 } else if (strncmp(buf, "GK", 2) == 0) {
872 DEBUG(10, ("Requested NTLMSSP session key\n"));
873 if(state->have_session_key) {
874 char *key64 = base64_encode_data_blob(state->mem_ctx,
875 state->session_key);
876 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
877 TALLOC_FREE(key64);
878 } else {
879 x_fprintf(x_stdout, "BH\n");
882 data_blob_free(&request);
883 return;
884 } else {
885 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
886 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
887 return;
890 if (!state->ntlmssp_state) {
891 nt_status = ntlm_auth_start_ntlmssp_server(
892 &state->ntlmssp_state);
893 if (!NT_STATUS_IS_OK(nt_status)) {
894 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
895 return;
897 ntlmssp_want_feature_list(state->ntlmssp_state,
898 state->want_feature_list);
901 DEBUG(10, ("got NTLMSSP packet:\n"));
902 dump_data(10, request.data, request.length);
904 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
906 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
907 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
908 reply);
909 x_fprintf(x_stdout, "TT %s\n", reply_base64);
910 TALLOC_FREE(reply_base64);
911 data_blob_free(&reply);
912 state->svr_state = SERVER_CHALLENGE;
913 DEBUG(10, ("NTLMSSP challenge\n"));
914 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
915 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
916 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
918 ntlmssp_end(&state->ntlmssp_state);
919 } else if (!NT_STATUS_IS_OK(nt_status)) {
920 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
921 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
922 } else {
923 x_fprintf(x_stdout, "AF %s\n",
924 (char *)state->ntlmssp_state->callback_private);
925 DEBUG(10, ("NTLMSSP OK!\n"));
927 if(state->have_session_key)
928 data_blob_free(&state->session_key);
929 state->session_key = data_blob(
930 state->ntlmssp_state->session_key.data,
931 state->ntlmssp_state->session_key.length);
932 state->neg_flags = state->ntlmssp_state->neg_flags;
933 state->have_session_key = true;
934 state->svr_state = SERVER_FINISHED;
937 data_blob_free(&request);
940 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
941 char *buf, int length)
943 DATA_BLOB request, reply;
944 NTSTATUS nt_status;
946 if (!opt_username || !*opt_username) {
947 x_fprintf(x_stderr, "username must be specified!\n\n");
948 exit(1);
951 if (strlen(buf) < 2) {
952 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
953 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
954 return;
957 if (strlen(buf) > 3) {
958 if(strncmp(buf, "SF ", 3) == 0) {
959 DEBUG(10, ("Looking for flags to negotiate\n"));
960 talloc_free(state->want_feature_list);
961 state->want_feature_list = talloc_strdup(state->mem_ctx,
962 buf+3);
963 x_fprintf(x_stdout, "OK\n");
964 return;
966 request = base64_decode_data_blob(buf + 3);
967 } else {
968 request = data_blob_null;
971 if (strncmp(buf, "PW ", 3) == 0) {
972 /* We asked for a password and obviously got it :-) */
974 opt_password = SMB_STRNDUP((const char *)request.data,
975 request.length);
977 if (opt_password == NULL) {
978 DEBUG(1, ("Out of memory\n"));
979 x_fprintf(x_stdout, "BH Out of memory\n");
980 data_blob_free(&request);
981 return;
984 x_fprintf(x_stdout, "OK\n");
985 data_blob_free(&request);
986 return;
989 if (!state->ntlmssp_state && use_cached_creds) {
990 /* check whether cached credentials are usable. */
991 DATA_BLOB empty_blob = data_blob_null;
993 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
994 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
995 /* failed to use cached creds */
996 use_cached_creds = False;
1000 if (opt_password == NULL && !use_cached_creds) {
1001 /* Request a password from the calling process. After
1002 sending it, the calling process should retry asking for the
1003 negotiate. */
1005 DEBUG(10, ("Requesting password\n"));
1006 x_fprintf(x_stdout, "PW\n");
1007 return;
1010 if (strncmp(buf, "YR", 2) == 0) {
1011 if (state->ntlmssp_state)
1012 ntlmssp_end(&state->ntlmssp_state);
1013 state->cli_state = CLIENT_INITIAL;
1014 } else if (strncmp(buf, "TT", 2) == 0) {
1015 /* No special preprocessing required */
1016 } else if (strncmp(buf, "GF", 2) == 0) {
1017 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1019 if(state->cli_state == CLIENT_FINISHED) {
1020 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
1022 else {
1023 x_fprintf(x_stdout, "BH\n");
1026 data_blob_free(&request);
1027 return;
1028 } else if (strncmp(buf, "GK", 2) == 0 ) {
1029 DEBUG(10, ("Requested session key\n"));
1031 if(state->cli_state == CLIENT_FINISHED) {
1032 char *key64 = base64_encode_data_blob(state->mem_ctx,
1033 state->session_key);
1034 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
1035 TALLOC_FREE(key64);
1037 else {
1038 x_fprintf(x_stdout, "BH\n");
1041 data_blob_free(&request);
1042 return;
1043 } else {
1044 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
1045 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
1046 return;
1049 if (!state->ntlmssp_state) {
1050 nt_status = ntlm_auth_start_ntlmssp_client(
1051 &state->ntlmssp_state);
1052 if (!NT_STATUS_IS_OK(nt_status)) {
1053 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1054 return;
1056 ntlmssp_want_feature_list(state->ntlmssp_state,
1057 state->want_feature_list);
1058 state->initial_message = data_blob_null;
1061 DEBUG(10, ("got NTLMSSP packet:\n"));
1062 dump_data(10, request.data, request.length);
1064 if (use_cached_creds && !opt_password &&
1065 (state->cli_state == CLIENT_RESPONSE)) {
1066 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
1067 &reply);
1068 } else {
1069 nt_status = ntlmssp_update(state->ntlmssp_state, request,
1070 &reply);
1073 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1074 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
1075 reply);
1076 if (state->cli_state == CLIENT_INITIAL) {
1077 x_fprintf(x_stdout, "YR %s\n", reply_base64);
1078 state->initial_message = reply;
1079 state->cli_state = CLIENT_RESPONSE;
1080 } else {
1081 x_fprintf(x_stdout, "KK %s\n", reply_base64);
1082 data_blob_free(&reply);
1084 TALLOC_FREE(reply_base64);
1085 DEBUG(10, ("NTLMSSP challenge\n"));
1086 } else if (NT_STATUS_IS_OK(nt_status)) {
1087 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1088 reply);
1089 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1090 TALLOC_FREE(reply_base64);
1092 if(state->have_session_key)
1093 data_blob_free(&state->session_key);
1095 state->session_key = data_blob(
1096 state->ntlmssp_state->session_key.data,
1097 state->ntlmssp_state->session_key.length);
1098 state->neg_flags = state->ntlmssp_state->neg_flags;
1099 state->have_session_key = true;
1101 DEBUG(10, ("NTLMSSP OK!\n"));
1102 state->cli_state = CLIENT_FINISHED;
1103 if (state->ntlmssp_state)
1104 ntlmssp_end(&state->ntlmssp_state);
1105 } else {
1106 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1107 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1108 state->cli_state = CLIENT_ERROR;
1109 if (state->ntlmssp_state)
1110 ntlmssp_end(&state->ntlmssp_state);
1113 data_blob_free(&request);
1116 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1117 char *buf, int length)
1119 char *user, *pass;
1120 user=buf;
1122 pass=(char *)memchr(buf,' ',length);
1123 if (!pass) {
1124 DEBUG(2, ("Password not found. Denying access\n"));
1125 x_fprintf(x_stdout, "ERR\n");
1126 return;
1128 *pass='\0';
1129 pass++;
1131 if (state->helper_mode == SQUID_2_5_BASIC) {
1132 rfc1738_unescape(user);
1133 rfc1738_unescape(pass);
1136 if (check_plaintext_auth(user, pass, False)) {
1137 x_fprintf(x_stdout, "OK\n");
1138 } else {
1139 x_fprintf(x_stdout, "ERR\n");
1143 static void offer_gss_spnego_mechs(void) {
1145 DATA_BLOB token;
1146 struct spnego_data spnego;
1147 ssize_t len;
1148 char *reply_base64;
1149 TALLOC_CTX *ctx = talloc_tos();
1150 char *principal;
1151 char *myname_lower;
1153 ZERO_STRUCT(spnego);
1155 myname_lower = talloc_strdup(ctx, global_myname());
1156 if (!myname_lower) {
1157 return;
1159 strlower_m(myname_lower);
1161 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1162 if (!principal) {
1163 return;
1166 /* Server negTokenInit (mech offerings) */
1167 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1168 spnego.negTokenInit.mechTypes = talloc_array(ctx, const char *, 2);
1169 #ifdef HAVE_KRB5
1170 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_KERBEROS5_OLD);
1171 spnego.negTokenInit.mechTypes[1] = talloc_strdup(ctx, OID_NTLMSSP);
1172 spnego.negTokenInit.mechTypes[2] = NULL;
1173 #else
1174 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_NTLMSSP);
1175 spnego.negTokenInit.mechTypes[1] = NULL;
1176 #endif
1179 spnego.negTokenInit.mechListMIC = data_blob_talloc(ctx, principal,
1180 strlen(principal));
1182 len = spnego_write_data(ctx, &token, &spnego);
1183 spnego_free_data(&spnego);
1185 if (len == -1) {
1186 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1187 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1188 return;
1191 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1192 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1194 TALLOC_FREE(reply_base64);
1195 data_blob_free(&token);
1196 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1197 return;
1200 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1201 char *buf, int length)
1203 static struct ntlmssp_state *ntlmssp_state = NULL;
1204 struct spnego_data request, response;
1205 DATA_BLOB token;
1206 NTSTATUS status;
1207 ssize_t len;
1208 TALLOC_CTX *ctx = talloc_tos();
1210 char *user = NULL;
1211 char *domain = NULL;
1213 const char *reply_code;
1214 char *reply_base64;
1215 char *reply_argument = NULL;
1217 if (strlen(buf) < 2) {
1218 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1219 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1220 return;
1223 if (strncmp(buf, "YR", 2) == 0) {
1224 if (ntlmssp_state)
1225 ntlmssp_end(&ntlmssp_state);
1226 } else if (strncmp(buf, "KK", 2) == 0) {
1228 } else {
1229 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1230 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1231 return;
1234 if ( (strlen(buf) == 2)) {
1236 /* no client data, get the negTokenInit offering
1237 mechanisms */
1239 offer_gss_spnego_mechs();
1240 return;
1243 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1245 if (strlen(buf) <= 3) {
1246 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1247 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1248 return;
1251 token = base64_decode_data_blob(buf + 3);
1252 len = spnego_read_data(ctx, token, &request);
1253 data_blob_free(&token);
1255 if (len == -1) {
1256 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
1257 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1258 return;
1261 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1263 /* Second request from Client. This is where the
1264 client offers its mechanism to use. */
1266 if ( (request.negTokenInit.mechTypes == NULL) ||
1267 (request.negTokenInit.mechTypes[0] == NULL) ) {
1268 DEBUG(1, ("Client did not offer any mechanism"));
1269 x_fprintf(x_stdout, "BH Client did not offer any "
1270 "mechanism\n");
1271 return;
1274 status = NT_STATUS_UNSUCCESSFUL;
1275 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1277 if ( request.negTokenInit.mechToken.data == NULL ) {
1278 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1279 x_fprintf(x_stdout, "BH Client did not provide "
1280 "NTLMSSP data\n");
1281 return;
1284 if ( ntlmssp_state != NULL ) {
1285 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1286 "already got one\n"));
1287 x_fprintf(x_stdout, "BH Client wants a new "
1288 "NTLMSSP challenge, but "
1289 "already got one\n");
1290 ntlmssp_end(&ntlmssp_state);
1291 return;
1294 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1295 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1296 return;
1299 DEBUG(10, ("got NTLMSSP packet:\n"));
1300 dump_data(10, request.negTokenInit.mechToken.data,
1301 request.negTokenInit.mechToken.length);
1303 response.type = SPNEGO_NEG_TOKEN_TARG;
1304 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
1305 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1307 status = ntlmssp_update(ntlmssp_state,
1308 request.negTokenInit.mechToken,
1309 &response.negTokenTarg.responseToken);
1312 #ifdef HAVE_KRB5
1313 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1315 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1316 char *principal;
1317 DATA_BLOB ap_rep;
1318 DATA_BLOB session_key;
1319 struct PAC_DATA *pac_data = NULL;
1321 if ( request.negTokenInit.mechToken.data == NULL ) {
1322 DEBUG(1, ("Client did not provide Kerberos data\n"));
1323 x_fprintf(x_stdout, "BH Client did not provide "
1324 "Kerberos data\n");
1325 return;
1328 response.type = SPNEGO_NEG_TOKEN_TARG;
1329 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_KERBEROS5_OLD);
1330 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1331 response.negTokenTarg.responseToken = data_blob_talloc(ctx, NULL, 0);
1333 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1334 &request.negTokenInit.mechToken,
1335 &principal, &pac_data, &ap_rep,
1336 &session_key, True);
1338 /* Now in "principal" we have the name we are
1339 authenticated as. */
1341 if (NT_STATUS_IS_OK(status)) {
1343 domain = strchr_m(principal, '@');
1345 if (domain == NULL) {
1346 DEBUG(1, ("Did not get a valid principal "
1347 "from ads_verify_ticket\n"));
1348 x_fprintf(x_stdout, "BH Did not get a "
1349 "valid principal from "
1350 "ads_verify_ticket\n");
1351 return;
1354 *domain++ = '\0';
1355 domain = SMB_STRDUP(domain);
1356 user = SMB_STRDUP(principal);
1358 data_blob_free(&ap_rep);
1361 TALLOC_FREE(mem_ctx);
1363 #endif
1365 } else {
1367 if ( (request.negTokenTarg.supportedMech == NULL) ||
1368 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1369 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1370 is the only one we support that sends this stuff */
1371 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1372 request.negTokenTarg.supportedMech));
1373 x_fprintf(x_stdout, "BH Got a negTokenTarg for "
1374 "something non-NTLMSSP\n");
1375 return;
1378 if (request.negTokenTarg.responseToken.data == NULL) {
1379 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1380 x_fprintf(x_stdout, "BH Got a negTokenTarg without a "
1381 "responseToken!\n");
1382 return;
1385 status = ntlmssp_update(ntlmssp_state,
1386 request.negTokenTarg.responseToken,
1387 &response.negTokenTarg.responseToken);
1389 response.type = SPNEGO_NEG_TOKEN_TARG;
1390 response.negTokenTarg.supportedMech = talloc_strdup(ctx, OID_NTLMSSP);
1391 response.negTokenTarg.mechListMIC = data_blob_talloc(ctx, NULL, 0);
1393 if (NT_STATUS_IS_OK(status)) {
1394 user = SMB_STRDUP(ntlmssp_state->user);
1395 domain = SMB_STRDUP(ntlmssp_state->domain);
1396 ntlmssp_end(&ntlmssp_state);
1400 spnego_free_data(&request);
1402 if (NT_STATUS_IS_OK(status)) {
1403 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1404 reply_code = "AF";
1405 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1406 } else if (NT_STATUS_EQUAL(status,
1407 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1408 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1409 reply_code = "TT";
1410 reply_argument = talloc_strdup(ctx, "*");
1411 } else {
1412 response.negTokenTarg.negResult = SPNEGO_REJECT;
1413 reply_code = "NA";
1414 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1417 if (!reply_argument) {
1418 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1419 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1420 return;
1423 SAFE_FREE(user);
1424 SAFE_FREE(domain);
1426 len = spnego_write_data(ctx, &token, &response);
1427 spnego_free_data(&response);
1429 if (len == -1) {
1430 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1431 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1432 return;
1435 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1437 x_fprintf(x_stdout, "%s %s %s\n",
1438 reply_code, reply_base64, reply_argument);
1440 TALLOC_FREE(reply_base64);
1441 data_blob_free(&token);
1443 return;
1446 static struct ntlmssp_state *client_ntlmssp_state = NULL;
1448 static bool manage_client_ntlmssp_init(struct spnego_data spnego)
1450 NTSTATUS status;
1451 DATA_BLOB null_blob = data_blob_null;
1452 DATA_BLOB to_server;
1453 char *to_server_base64;
1454 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1455 TALLOC_CTX *ctx = talloc_tos();
1457 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1459 if (client_ntlmssp_state != NULL) {
1460 DEBUG(1, ("Request for initial SPNEGO request where "
1461 "we already have a state\n"));
1462 return False;
1465 if (!client_ntlmssp_state) {
1466 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1467 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1468 return False;
1473 if (opt_password == NULL) {
1475 /* Request a password from the calling process. After
1476 sending it, the calling process should retry with
1477 the negTokenInit. */
1479 DEBUG(10, ("Requesting password\n"));
1480 x_fprintf(x_stdout, "PW\n");
1481 return True;
1484 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1485 spnego.negTokenInit.mechTypes = my_mechs;
1486 spnego.negTokenInit.reqFlags = data_blob_null;
1487 spnego.negTokenInit.reqFlagsPadding = 0;
1488 spnego.negTokenInit.mechListMIC = null_blob;
1490 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1491 &spnego.negTokenInit.mechToken);
1493 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1494 NT_STATUS_IS_OK(status)) ) {
1495 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1496 nt_errstr(status)));
1497 ntlmssp_end(&client_ntlmssp_state);
1498 return False;
1501 spnego_write_data(ctx, &to_server, &spnego);
1502 data_blob_free(&spnego.negTokenInit.mechToken);
1504 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1505 data_blob_free(&to_server);
1506 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1507 TALLOC_FREE(to_server_base64);
1508 return True;
1511 static void manage_client_ntlmssp_targ(struct spnego_data spnego)
1513 NTSTATUS status;
1514 DATA_BLOB null_blob = data_blob_null;
1515 DATA_BLOB request;
1516 DATA_BLOB to_server;
1517 char *to_server_base64;
1518 TALLOC_CTX *ctx = talloc_tos();
1520 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1522 if (client_ntlmssp_state == NULL) {
1523 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1524 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1525 return;
1528 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1529 x_fprintf(x_stdout, "NA\n");
1530 ntlmssp_end(&client_ntlmssp_state);
1531 return;
1534 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1535 x_fprintf(x_stdout, "AF\n");
1536 ntlmssp_end(&client_ntlmssp_state);
1537 return;
1540 status = ntlmssp_update(client_ntlmssp_state,
1541 spnego.negTokenTarg.responseToken,
1542 &request);
1544 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1545 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1546 "ntlmssp_client_update, got: %s\n",
1547 nt_errstr(status)));
1548 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1549 "ntlmssp_client_update\n");
1550 data_blob_free(&request);
1551 ntlmssp_end(&client_ntlmssp_state);
1552 return;
1555 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1556 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1557 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1558 spnego.negTokenTarg.responseToken = request;
1559 spnego.negTokenTarg.mechListMIC = null_blob;
1561 spnego_write_data(ctx, &to_server, &spnego);
1562 data_blob_free(&request);
1564 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1565 data_blob_free(&to_server);
1566 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1567 TALLOC_FREE(to_server_base64);
1568 return;
1571 #ifdef HAVE_KRB5
1573 static bool manage_client_krb5_init(struct spnego_data spnego)
1575 char *principal;
1576 DATA_BLOB tkt, to_server;
1577 DATA_BLOB session_key_krb5 = data_blob_null;
1578 struct spnego_data reply;
1579 char *reply_base64;
1580 int retval;
1582 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1583 ssize_t len;
1584 TALLOC_CTX *ctx = talloc_tos();
1586 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1587 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1588 DEBUG(1, ("Did not get a principal for krb5\n"));
1589 return False;
1592 principal = (char *)SMB_MALLOC(
1593 spnego.negTokenInit.mechListMIC.length+1);
1595 if (principal == NULL) {
1596 DEBUG(1, ("Could not malloc principal\n"));
1597 return False;
1600 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1601 spnego.negTokenInit.mechListMIC.length);
1602 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1604 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1606 if (retval) {
1607 char *user = NULL;
1609 /* Let's try to first get the TGT, for that we need a
1610 password. */
1612 if (opt_password == NULL) {
1613 DEBUG(10, ("Requesting password\n"));
1614 x_fprintf(x_stdout, "PW\n");
1615 return True;
1618 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1619 if (!user) {
1620 return false;
1623 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1624 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1625 return False;
1628 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1630 if (retval) {
1631 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1632 return False;
1636 data_blob_free(&session_key_krb5);
1638 ZERO_STRUCT(reply);
1640 reply.type = SPNEGO_NEG_TOKEN_INIT;
1641 reply.negTokenInit.mechTypes = my_mechs;
1642 reply.negTokenInit.reqFlags = data_blob_null;
1643 reply.negTokenInit.reqFlagsPadding = 0;
1644 reply.negTokenInit.mechToken = tkt;
1645 reply.negTokenInit.mechListMIC = data_blob_null;
1647 len = spnego_write_data(ctx, &to_server, &reply);
1648 data_blob_free(&tkt);
1650 if (len == -1) {
1651 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1652 return False;
1655 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1656 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1658 TALLOC_FREE(reply_base64);
1659 data_blob_free(&to_server);
1660 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1661 return True;
1664 static void manage_client_krb5_targ(struct spnego_data spnego)
1666 switch (spnego.negTokenTarg.negResult) {
1667 case SPNEGO_ACCEPT_INCOMPLETE:
1668 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1669 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1670 "ACCEPT_INCOMPLETE\n");
1671 break;
1672 case SPNEGO_ACCEPT_COMPLETED:
1673 DEBUG(10, ("Accept completed\n"));
1674 x_fprintf(x_stdout, "AF\n");
1675 break;
1676 case SPNEGO_REJECT:
1677 DEBUG(10, ("Rejected\n"));
1678 x_fprintf(x_stdout, "NA\n");
1679 break;
1680 default:
1681 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1682 x_fprintf(x_stdout, "AF\n");
1686 #endif
1688 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1689 char *buf, int length)
1691 DATA_BLOB request;
1692 struct spnego_data spnego;
1693 ssize_t len;
1694 TALLOC_CTX *ctx = talloc_tos();
1696 if (!opt_username || !*opt_username) {
1697 x_fprintf(x_stderr, "username must be specified!\n\n");
1698 exit(1);
1701 if (strlen(buf) <= 3) {
1702 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1703 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1704 return;
1707 request = base64_decode_data_blob(buf+3);
1709 if (strncmp(buf, "PW ", 3) == 0) {
1711 /* We asked for a password and obviously got it :-) */
1713 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1715 if (opt_password == NULL) {
1716 DEBUG(1, ("Out of memory\n"));
1717 x_fprintf(x_stdout, "BH Out of memory\n");
1718 data_blob_free(&request);
1719 return;
1722 x_fprintf(x_stdout, "OK\n");
1723 data_blob_free(&request);
1724 return;
1727 if ( (strncmp(buf, "TT ", 3) != 0) &&
1728 (strncmp(buf, "AF ", 3) != 0) &&
1729 (strncmp(buf, "NA ", 3) != 0) ) {
1730 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1731 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1732 data_blob_free(&request);
1733 return;
1736 /* So we got a server challenge to generate a SPNEGO
1737 client-to-server request... */
1739 len = spnego_read_data(ctx, request, &spnego);
1740 data_blob_free(&request);
1742 if (len == -1) {
1743 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1744 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1745 return;
1748 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1750 /* The server offers a list of mechanisms */
1752 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1754 while (*mechType != NULL) {
1756 #ifdef HAVE_KRB5
1757 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1758 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1759 if (manage_client_krb5_init(spnego))
1760 goto out;
1762 #endif
1764 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1765 if (manage_client_ntlmssp_init(spnego))
1766 goto out;
1769 mechType++;
1772 DEBUG(1, ("Server offered no compatible mechanism\n"));
1773 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1774 return;
1777 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1779 if (spnego.negTokenTarg.supportedMech == NULL) {
1780 /* On accept/reject Windows does not send the
1781 mechanism anymore. Handle that here and
1782 shut down the mechanisms. */
1784 switch (spnego.negTokenTarg.negResult) {
1785 case SPNEGO_ACCEPT_COMPLETED:
1786 x_fprintf(x_stdout, "AF\n");
1787 break;
1788 case SPNEGO_REJECT:
1789 x_fprintf(x_stdout, "NA\n");
1790 break;
1791 default:
1792 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1793 "unknown negResult: %d\n",
1794 spnego.negTokenTarg.negResult));
1795 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1796 " no mech and an unknown "
1797 "negResult\n");
1800 ntlmssp_end(&client_ntlmssp_state);
1801 goto out;
1804 if (strcmp(spnego.negTokenTarg.supportedMech,
1805 OID_NTLMSSP) == 0) {
1806 manage_client_ntlmssp_targ(spnego);
1807 goto out;
1810 #if HAVE_KRB5
1811 if (strcmp(spnego.negTokenTarg.supportedMech,
1812 OID_KERBEROS5_OLD) == 0) {
1813 manage_client_krb5_targ(spnego);
1814 goto out;
1816 #endif
1820 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1821 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1822 return;
1824 out:
1825 spnego_free_data(&spnego);
1826 return;
1829 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1830 char *buf, int length)
1832 char *request, *parameter;
1833 static DATA_BLOB challenge;
1834 static DATA_BLOB lm_response;
1835 static DATA_BLOB nt_response;
1836 static char *full_username;
1837 static char *username;
1838 static char *domain;
1839 static char *plaintext_password;
1840 static bool ntlm_server_1_user_session_key;
1841 static bool ntlm_server_1_lm_session_key;
1843 if (strequal(buf, ".")) {
1844 if (!full_username && !username) {
1845 x_fprintf(x_stdout, "Error: No username supplied!\n");
1846 } else if (plaintext_password) {
1847 /* handle this request as plaintext */
1848 if (!full_username) {
1849 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1850 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1851 return;
1854 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1855 x_fprintf(x_stdout, "Authenticated: Yes\n");
1856 } else {
1857 x_fprintf(x_stdout, "Authenticated: No\n");
1859 } else if (!lm_response.data && !nt_response.data) {
1860 x_fprintf(x_stdout, "Error: No password supplied!\n");
1861 } else if (!challenge.data) {
1862 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1863 } else {
1864 char *error_string = NULL;
1865 uchar lm_key[8];
1866 uchar user_session_key[16];
1867 uint32 flags = 0;
1869 if (full_username && !username) {
1870 fstring fstr_user;
1871 fstring fstr_domain;
1873 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1874 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1875 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1877 SAFE_FREE(username);
1878 SAFE_FREE(domain);
1879 username = smb_xstrdup(fstr_user);
1880 domain = smb_xstrdup(fstr_domain);
1883 if (!domain) {
1884 domain = smb_xstrdup(get_winbind_domain());
1887 if (ntlm_server_1_lm_session_key)
1888 flags |= WBFLAG_PAM_LMKEY;
1890 if (ntlm_server_1_user_session_key)
1891 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1893 if (!NT_STATUS_IS_OK(
1894 contact_winbind_auth_crap(username,
1895 domain,
1896 global_myname(),
1897 &challenge,
1898 &lm_response,
1899 &nt_response,
1900 flags,
1901 lm_key,
1902 user_session_key,
1903 &error_string,
1904 NULL))) {
1906 x_fprintf(x_stdout, "Authenticated: No\n");
1907 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1908 } else {
1909 static char zeros[16];
1910 char *hex_lm_key;
1911 char *hex_user_session_key;
1913 x_fprintf(x_stdout, "Authenticated: Yes\n");
1915 if (ntlm_server_1_lm_session_key
1916 && (memcmp(zeros, lm_key,
1917 sizeof(lm_key)) != 0)) {
1918 hex_lm_key = hex_encode_talloc(NULL,
1919 (const unsigned char *)lm_key,
1920 sizeof(lm_key));
1921 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1922 TALLOC_FREE(hex_lm_key);
1925 if (ntlm_server_1_user_session_key
1926 && (memcmp(zeros, user_session_key,
1927 sizeof(user_session_key)) != 0)) {
1928 hex_user_session_key = hex_encode_talloc(NULL,
1929 (const unsigned char *)user_session_key,
1930 sizeof(user_session_key));
1931 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1932 TALLOC_FREE(hex_user_session_key);
1935 SAFE_FREE(error_string);
1937 /* clear out the state */
1938 challenge = data_blob_null;
1939 nt_response = data_blob_null;
1940 lm_response = data_blob_null;
1941 SAFE_FREE(full_username);
1942 SAFE_FREE(username);
1943 SAFE_FREE(domain);
1944 SAFE_FREE(plaintext_password);
1945 ntlm_server_1_user_session_key = False;
1946 ntlm_server_1_lm_session_key = False;
1947 x_fprintf(x_stdout, ".\n");
1949 return;
1952 request = buf;
1954 /* Indicates a base64 encoded structure */
1955 parameter = strstr_m(request, ":: ");
1956 if (!parameter) {
1957 parameter = strstr_m(request, ": ");
1959 if (!parameter) {
1960 DEBUG(0, ("Parameter not found!\n"));
1961 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1962 return;
1965 parameter[0] ='\0';
1966 parameter++;
1967 parameter[0] ='\0';
1968 parameter++;
1970 } else {
1971 parameter[0] ='\0';
1972 parameter++;
1973 parameter[0] ='\0';
1974 parameter++;
1975 parameter[0] ='\0';
1976 parameter++;
1978 base64_decode_inplace(parameter);
1981 if (strequal(request, "LANMAN-Challenge")) {
1982 challenge = strhex_to_data_blob(NULL, parameter);
1983 if (challenge.length != 8) {
1984 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1985 parameter,
1986 (int)challenge.length);
1987 challenge = data_blob_null;
1989 } else if (strequal(request, "NT-Response")) {
1990 nt_response = strhex_to_data_blob(NULL, parameter);
1991 if (nt_response.length < 24) {
1992 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1993 parameter,
1994 (int)nt_response.length);
1995 nt_response = data_blob_null;
1997 } else if (strequal(request, "LANMAN-Response")) {
1998 lm_response = strhex_to_data_blob(NULL, parameter);
1999 if (lm_response.length != 24) {
2000 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2001 parameter,
2002 (int)lm_response.length);
2003 lm_response = data_blob_null;
2005 } else if (strequal(request, "Password")) {
2006 plaintext_password = smb_xstrdup(parameter);
2007 } else if (strequal(request, "NT-Domain")) {
2008 domain = smb_xstrdup(parameter);
2009 } else if (strequal(request, "Username")) {
2010 username = smb_xstrdup(parameter);
2011 } else if (strequal(request, "Full-Username")) {
2012 full_username = smb_xstrdup(parameter);
2013 } else if (strequal(request, "Request-User-Session-Key")) {
2014 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
2015 } else if (strequal(request, "Request-LanMan-Session-Key")) {
2016 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
2017 } else {
2018 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2022 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
2023 char *buf, int length)
2025 char *request, *parameter;
2026 static DATA_BLOB new_nt_pswd;
2027 static DATA_BLOB old_nt_hash_enc;
2028 static DATA_BLOB new_lm_pswd;
2029 static DATA_BLOB old_lm_hash_enc;
2030 static char *full_username = NULL;
2031 static char *username = NULL;
2032 static char *domain = NULL;
2033 static char *newpswd = NULL;
2034 static char *oldpswd = NULL;
2036 if (strequal(buf, ".")) {
2037 if(newpswd && oldpswd) {
2038 uchar old_nt_hash[16];
2039 uchar old_lm_hash[16];
2040 uchar new_nt_hash[16];
2041 uchar new_lm_hash[16];
2043 new_nt_pswd = data_blob(NULL, 516);
2044 old_nt_hash_enc = data_blob(NULL, 16);
2046 /* Calculate the MD4 hash (NT compatible) of the
2047 * password */
2048 E_md4hash(oldpswd, old_nt_hash);
2049 E_md4hash(newpswd, new_nt_hash);
2051 /* E_deshash returns false for 'long'
2052 passwords (> 14 DOS chars).
2054 Therefore, don't send a buffer
2055 encrypted with the truncated hash
2056 (it could allow an even easier
2057 attack on the password)
2059 Likewise, obey the admin's restriction
2062 if (lp_client_lanman_auth() &&
2063 E_deshash(newpswd, new_lm_hash) &&
2064 E_deshash(oldpswd, old_lm_hash)) {
2065 new_lm_pswd = data_blob(NULL, 516);
2066 old_lm_hash_enc = data_blob(NULL, 16);
2067 encode_pw_buffer(new_lm_pswd.data, newpswd,
2068 STR_UNICODE);
2070 arcfour_crypt(new_lm_pswd.data, old_nt_hash, 516);
2071 E_old_pw_hash(new_nt_hash, old_lm_hash,
2072 old_lm_hash_enc.data);
2073 } else {
2074 new_lm_pswd.data = NULL;
2075 new_lm_pswd.length = 0;
2076 old_lm_hash_enc.data = NULL;
2077 old_lm_hash_enc.length = 0;
2080 encode_pw_buffer(new_nt_pswd.data, newpswd,
2081 STR_UNICODE);
2083 arcfour_crypt(new_nt_pswd.data, old_nt_hash, 516);
2084 E_old_pw_hash(new_nt_hash, old_nt_hash,
2085 old_nt_hash_enc.data);
2088 if (!full_username && !username) {
2089 x_fprintf(x_stdout, "Error: No username supplied!\n");
2090 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2091 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2092 x_fprintf(x_stdout, "Error: No NT or LM password "
2093 "blobs supplied!\n");
2094 } else {
2095 char *error_string = NULL;
2097 if (full_username && !username) {
2098 fstring fstr_user;
2099 fstring fstr_domain;
2101 if (!parse_ntlm_auth_domain_user(full_username,
2102 fstr_user,
2103 fstr_domain)) {
2104 /* username might be 'tainted', don't
2105 * print into our new-line
2106 * deleimianted stream */
2107 x_fprintf(x_stdout, "Error: Could not "
2108 "parse into domain and "
2109 "username\n");
2110 SAFE_FREE(username);
2111 username = smb_xstrdup(full_username);
2112 } else {
2113 SAFE_FREE(username);
2114 SAFE_FREE(domain);
2115 username = smb_xstrdup(fstr_user);
2116 domain = smb_xstrdup(fstr_domain);
2121 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2122 username, domain,
2123 new_nt_pswd,
2124 old_nt_hash_enc,
2125 new_lm_pswd,
2126 old_lm_hash_enc,
2127 &error_string))) {
2128 x_fprintf(x_stdout, "Password-Change: No\n");
2129 x_fprintf(x_stdout, "Password-Change-Error: "
2130 "%s\n.\n", error_string);
2131 } else {
2132 x_fprintf(x_stdout, "Password-Change: Yes\n");
2135 SAFE_FREE(error_string);
2137 /* clear out the state */
2138 new_nt_pswd = data_blob_null;
2139 old_nt_hash_enc = data_blob_null;
2140 new_lm_pswd = data_blob_null;
2141 old_nt_hash_enc = data_blob_null;
2142 SAFE_FREE(full_username);
2143 SAFE_FREE(username);
2144 SAFE_FREE(domain);
2145 SAFE_FREE(newpswd);
2146 SAFE_FREE(oldpswd);
2147 x_fprintf(x_stdout, ".\n");
2149 return;
2152 request = buf;
2154 /* Indicates a base64 encoded structure */
2155 parameter = strstr_m(request, ":: ");
2156 if (!parameter) {
2157 parameter = strstr_m(request, ": ");
2159 if (!parameter) {
2160 DEBUG(0, ("Parameter not found!\n"));
2161 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2162 return;
2165 parameter[0] ='\0';
2166 parameter++;
2167 parameter[0] ='\0';
2168 parameter++;
2169 } else {
2170 parameter[0] ='\0';
2171 parameter++;
2172 parameter[0] ='\0';
2173 parameter++;
2174 parameter[0] ='\0';
2175 parameter++;
2177 base64_decode_inplace(parameter);
2180 if (strequal(request, "new-nt-password-blob")) {
2181 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2182 if (new_nt_pswd.length != 516) {
2183 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2184 "(got %d bytes, expected 516)\n.\n",
2185 parameter,
2186 (int)new_nt_pswd.length);
2187 new_nt_pswd = data_blob_null;
2189 } else if (strequal(request, "old-nt-hash-blob")) {
2190 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2191 if (old_nt_hash_enc.length != 16) {
2192 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2193 "(got %d bytes, expected 16)\n.\n",
2194 parameter,
2195 (int)old_nt_hash_enc.length);
2196 old_nt_hash_enc = data_blob_null;
2198 } else if (strequal(request, "new-lm-password-blob")) {
2199 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2200 if (new_lm_pswd.length != 516) {
2201 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2202 "(got %d bytes, expected 516)\n.\n",
2203 parameter,
2204 (int)new_lm_pswd.length);
2205 new_lm_pswd = data_blob_null;
2208 else if (strequal(request, "old-lm-hash-blob")) {
2209 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2210 if (old_lm_hash_enc.length != 16)
2212 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2213 "(got %d bytes, expected 16)\n.\n",
2214 parameter,
2215 (int)old_lm_hash_enc.length);
2216 old_lm_hash_enc = data_blob_null;
2218 } else if (strequal(request, "nt-domain")) {
2219 domain = smb_xstrdup(parameter);
2220 } else if(strequal(request, "username")) {
2221 username = smb_xstrdup(parameter);
2222 } else if(strequal(request, "full-username")) {
2223 username = smb_xstrdup(parameter);
2224 } else if(strequal(request, "new-password")) {
2225 newpswd = smb_xstrdup(parameter);
2226 } else if (strequal(request, "old-password")) {
2227 oldpswd = smb_xstrdup(parameter);
2228 } else {
2229 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2233 static void manage_squid_request(struct ntlm_auth_state *state,
2234 stdio_helper_function fn)
2236 char *buf;
2237 char tmp[INITIAL_BUFFER_SIZE+1];
2238 int length, buf_size = 0;
2239 char *c;
2241 buf = talloc_strdup(state->mem_ctx, "");
2242 if (!buf) {
2243 DEBUG(0, ("Failed to allocate input buffer.\n"));
2244 x_fprintf(x_stderr, "ERR\n");
2245 exit(1);
2248 do {
2250 /* this is not a typo - x_fgets doesn't work too well under
2251 * squid */
2252 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2253 if (ferror(stdin)) {
2254 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2255 "(%s)\n", ferror(stdin),
2256 strerror(ferror(stdin))));
2258 exit(1);
2260 exit(0);
2263 buf = talloc_strdup_append_buffer(buf, tmp);
2264 buf_size += INITIAL_BUFFER_SIZE;
2266 if (buf_size > MAX_BUFFER_SIZE) {
2267 DEBUG(2, ("Oversized message\n"));
2268 x_fprintf(x_stderr, "ERR\n");
2269 talloc_free(buf);
2270 return;
2273 c = strchr(buf, '\n');
2274 } while (c == NULL);
2276 *c = '\0';
2277 length = c-buf;
2279 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2281 if (buf[0] == '\0') {
2282 DEBUG(2, ("Invalid Request\n"));
2283 x_fprintf(x_stderr, "ERR\n");
2284 talloc_free(buf);
2285 return;
2288 fn(state, buf, length);
2289 talloc_free(buf);
2293 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2294 TALLOC_CTX *mem_ctx;
2295 struct ntlm_auth_state *state;
2297 /* initialize FDescs */
2298 x_setbuf(x_stdout, NULL);
2299 x_setbuf(x_stderr, NULL);
2301 mem_ctx = talloc_init("ntlm_auth");
2302 if (!mem_ctx) {
2303 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2304 x_fprintf(x_stderr, "ERR\n");
2305 exit(1);
2308 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2309 if (!state) {
2310 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2311 x_fprintf(x_stderr, "ERR\n");
2312 exit(1);
2315 state->mem_ctx = mem_ctx;
2316 state->helper_mode = stdio_mode;
2318 while(1) {
2319 manage_squid_request(state, fn);
2324 /* Authenticate a user with a challenge/response */
2326 static bool check_auth_crap(void)
2328 NTSTATUS nt_status;
2329 uint32 flags = 0;
2330 char lm_key[8];
2331 char user_session_key[16];
2332 char *hex_lm_key;
2333 char *hex_user_session_key;
2334 char *error_string;
2335 static uint8 zeros[16];
2337 x_setbuf(x_stdout, NULL);
2339 if (request_lm_key)
2340 flags |= WBFLAG_PAM_LMKEY;
2342 if (request_user_session_key)
2343 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2345 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2347 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2348 opt_workstation,
2349 &opt_challenge,
2350 &opt_lm_response,
2351 &opt_nt_response,
2352 flags,
2353 (unsigned char *)lm_key,
2354 (unsigned char *)user_session_key,
2355 &error_string, NULL);
2357 if (!NT_STATUS_IS_OK(nt_status)) {
2358 x_fprintf(x_stdout, "%s (0x%x)\n",
2359 error_string,
2360 NT_STATUS_V(nt_status));
2361 SAFE_FREE(error_string);
2362 return False;
2365 if (request_lm_key
2366 && (memcmp(zeros, lm_key,
2367 sizeof(lm_key)) != 0)) {
2368 hex_lm_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key,
2369 sizeof(lm_key));
2370 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2371 TALLOC_FREE(hex_lm_key);
2373 if (request_user_session_key
2374 && (memcmp(zeros, user_session_key,
2375 sizeof(user_session_key)) != 0)) {
2376 hex_user_session_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key,
2377 sizeof(user_session_key));
2378 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2379 TALLOC_FREE(hex_user_session_key);
2382 return True;
2385 /* Main program */
2387 enum {
2388 OPT_USERNAME = 1000,
2389 OPT_DOMAIN,
2390 OPT_WORKSTATION,
2391 OPT_CHALLENGE,
2392 OPT_RESPONSE,
2393 OPT_LM,
2394 OPT_NT,
2395 OPT_PASSWORD,
2396 OPT_LM_KEY,
2397 OPT_USER_SESSION_KEY,
2398 OPT_DIAGNOSTICS,
2399 OPT_REQUIRE_MEMBERSHIP,
2400 OPT_USE_CACHED_CREDS,
2401 OPT_PAM_WINBIND_CONF
2404 int main(int argc, const char **argv)
2406 TALLOC_CTX *frame = talloc_stackframe();
2407 int opt;
2408 static const char *helper_protocol;
2409 static int diagnostics;
2411 static const char *hex_challenge;
2412 static const char *hex_lm_response;
2413 static const char *hex_nt_response;
2415 poptContext pc;
2417 /* NOTE: DO NOT change this interface without considering the implications!
2418 This is an external interface, which other programs will use to interact
2419 with this helper.
2422 /* We do not use single-letter command abbreviations, because they harm future
2423 interface stability. */
2425 struct poptOption long_options[] = {
2426 POPT_AUTOHELP
2427 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2428 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2429 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2430 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2431 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2432 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2433 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2434 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2435 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2436 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2437 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2438 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2439 { "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" },
2440 { "pam-winbind-conf", 0, POPT_ARG_STRING, &opt_pam_winbind_conf, OPT_PAM_WINBIND_CONF, "Require that request must set WBFLAG_PAM_CONTACT_TRUSTDOM when krb5 auth is required" },
2441 POPT_COMMON_CONFIGFILE
2442 POPT_COMMON_VERSION
2443 POPT_TABLEEND
2446 /* Samba client initialisation */
2447 load_case_tables();
2449 dbf = x_stderr;
2451 /* Parse options */
2453 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2455 /* Parse command line options */
2457 if (argc == 1) {
2458 poptPrintHelp(pc, stderr, 0);
2459 return 1;
2462 while((opt = poptGetNextOpt(pc)) != -1) {
2463 /* Get generic config options like --configfile */
2466 poptFreeContext(pc);
2468 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2469 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2470 get_dyn_CONFIGFILE(), strerror(errno));
2471 exit(1);
2474 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2475 POPT_CONTEXT_KEEP_FIRST);
2477 while((opt = poptGetNextOpt(pc)) != -1) {
2478 switch (opt) {
2479 case OPT_CHALLENGE:
2480 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2481 if (opt_challenge.length != 8) {
2482 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2483 hex_challenge,
2484 (int)opt_challenge.length);
2485 exit(1);
2487 break;
2488 case OPT_LM:
2489 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2490 if (opt_lm_response.length != 24) {
2491 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2492 hex_lm_response,
2493 (int)opt_lm_response.length);
2494 exit(1);
2496 break;
2498 case OPT_NT:
2499 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2500 if (opt_nt_response.length < 24) {
2501 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2502 hex_nt_response,
2503 (int)opt_nt_response.length);
2504 exit(1);
2506 break;
2508 case OPT_REQUIRE_MEMBERSHIP:
2509 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2510 require_membership_of_sid = require_membership_of;
2512 break;
2516 if (opt_username) {
2517 char *domain = SMB_STRDUP(opt_username);
2518 char *p = strchr_m(domain, *lp_winbind_separator());
2519 if (p) {
2520 opt_username = p+1;
2521 *p = '\0';
2522 if (opt_domain && !strequal(opt_domain, domain)) {
2523 x_fprintf(x_stderr, "Domain specified in username (%s) "
2524 "doesn't match specified domain (%s)!\n\n",
2525 domain, opt_domain);
2526 poptPrintHelp(pc, stderr, 0);
2527 exit(1);
2529 opt_domain = domain;
2530 } else {
2531 SAFE_FREE(domain);
2535 /* Note: if opt_domain is "" then send no domain */
2536 if (opt_domain == NULL) {
2537 opt_domain = get_winbind_domain();
2540 if (opt_workstation == NULL) {
2541 opt_workstation = "";
2544 if (helper_protocol) {
2545 int i;
2546 for (i=0; i<NUM_HELPER_MODES; i++) {
2547 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2548 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2549 exit(0);
2552 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2554 for (i=0; i<NUM_HELPER_MODES; i++) {
2555 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2558 exit(1);
2561 if (!opt_username || !*opt_username) {
2562 x_fprintf(x_stderr, "username must be specified!\n\n");
2563 poptPrintHelp(pc, stderr, 0);
2564 exit(1);
2567 if (opt_challenge.length) {
2568 if (!check_auth_crap()) {
2569 exit(1);
2571 exit(0);
2574 if (!opt_password) {
2575 opt_password = getpass("password: ");
2578 if (diagnostics) {
2579 if (!diagnose_ntlm_auth()) {
2580 return 1;
2582 } else {
2583 fstring user;
2585 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2586 if (!check_plaintext_auth(user, opt_password, True)) {
2587 return 1;
2591 /* Exit code */
2593 poptFreeContext(pc);
2594 TALLOC_FREE(frame);
2595 return 0;