Add functions to delete NTACL on posix ACL set.
[Samba.git] / source / utils / ntlm_auth.c
blob8dd433c5386019bc33f374994efcb9f1a47b6ff6
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"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_WINBIND
32 #define INITIAL_BUFFER_SIZE 300
33 #define MAX_BUFFER_SIZE 630000
35 enum stdio_helper_mode {
36 SQUID_2_4_BASIC,
37 SQUID_2_5_BASIC,
38 SQUID_2_5_NTLMSSP,
39 NTLMSSP_CLIENT_1,
40 GSS_SPNEGO,
41 GSS_SPNEGO_CLIENT,
42 NTLM_SERVER_1,
43 NTLM_CHANGE_PASSWORD_1,
44 NUM_HELPER_MODES
47 enum ntlm_auth_cli_state {
48 CLIENT_INITIAL = 0,
49 CLIENT_RESPONSE,
50 CLIENT_FINISHED,
51 CLIENT_ERROR
54 enum ntlm_auth_svr_state {
55 SERVER_INITIAL = 0,
56 SERVER_CHALLENGE,
57 SERVER_FINISHED,
58 SERVER_ERROR
61 struct ntlm_auth_state {
62 TALLOC_CTX *mem_ctx;
63 enum stdio_helper_mode helper_mode;
64 enum ntlm_auth_cli_state cli_state;
65 enum ntlm_auth_svr_state svr_state;
66 struct ntlmssp_state *ntlmssp_state;
67 uint32_t neg_flags;
68 char *want_feature_list;
69 bool have_session_key;
70 DATA_BLOB session_key;
71 DATA_BLOB initial_message;
74 typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
75 int length);
77 static void manage_squid_basic_request (struct ntlm_auth_state *state,
78 char *buf, int length);
80 static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
81 char *buf, int length);
83 static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
84 char *buf, int length);
86 static void manage_gss_spnego_request (struct ntlm_auth_state *state,
87 char *buf, int length);
89 static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
90 char *buf, int length);
92 static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
93 char *buf, int length);
95 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
96 char *buf, int length);
98 static const struct {
99 enum stdio_helper_mode mode;
100 const char *name;
101 stdio_helper_function fn;
102 } stdio_helper_protocols[] = {
103 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
104 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
105 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
106 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
107 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
108 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
109 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
110 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
111 { NUM_HELPER_MODES, NULL, NULL}
114 const char *opt_username;
115 const char *opt_domain;
116 const char *opt_workstation;
117 const char *opt_password;
118 static DATA_BLOB opt_challenge;
119 static DATA_BLOB opt_lm_response;
120 static DATA_BLOB opt_nt_response;
121 static int request_lm_key;
122 static int request_user_session_key;
123 static int use_cached_creds;
125 static const char *require_membership_of;
126 static const char *require_membership_of_sid;
128 static char winbind_separator(void)
130 struct winbindd_response response;
131 static bool got_sep;
132 static char sep;
134 if (got_sep)
135 return sep;
137 ZERO_STRUCT(response);
139 /* Send off request */
141 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
142 NSS_STATUS_SUCCESS) {
143 d_printf("could not obtain winbind separator!\n");
144 return *lp_winbind_separator();
147 sep = response.data.info.winbind_separator;
148 got_sep = True;
150 if (!sep) {
151 d_printf("winbind separator was NULL!\n");
152 return *lp_winbind_separator();
155 return sep;
158 const char *get_winbind_domain(void)
160 struct winbindd_response response;
162 static fstring winbind_domain;
163 if (*winbind_domain) {
164 return winbind_domain;
167 ZERO_STRUCT(response);
169 /* Send off request */
171 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
172 NSS_STATUS_SUCCESS) {
173 DEBUG(0, ("could not obtain winbind domain name!\n"));
174 return lp_workgroup();
177 fstrcpy(winbind_domain, response.data.domain_name);
179 return winbind_domain;
183 const char *get_winbind_netbios_name(void)
185 struct winbindd_response response;
187 static fstring winbind_netbios_name;
189 if (*winbind_netbios_name) {
190 return winbind_netbios_name;
193 ZERO_STRUCT(response);
195 /* Send off request */
197 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
198 NSS_STATUS_SUCCESS) {
199 DEBUG(0, ("could not obtain winbind netbios name!\n"));
200 return global_myname();
203 fstrcpy(winbind_netbios_name, response.data.netbios_name);
205 return winbind_netbios_name;
209 DATA_BLOB get_challenge(void)
211 static DATA_BLOB chal;
212 if (opt_challenge.length)
213 return opt_challenge;
215 chal = data_blob(NULL, 8);
217 generate_random_buffer(chal.data, chal.length);
218 return chal;
221 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
222 form DOMAIN/user into a domain and a user */
224 static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
225 fstring user)
228 char *p = strchr(domuser,winbind_separator());
230 if (!p) {
231 return False;
234 fstrcpy(user, p+1);
235 fstrcpy(domain, domuser);
236 domain[PTR_DIFF(p, domuser)] = 0;
237 strupper_m(domain);
239 return True;
242 static bool get_require_membership_sid(void) {
243 struct winbindd_request request;
244 struct winbindd_response response;
246 if (!require_membership_of) {
247 return True;
250 if (require_membership_of_sid) {
251 return True;
254 /* Otherwise, ask winbindd for the name->sid request */
256 ZERO_STRUCT(request);
257 ZERO_STRUCT(response);
259 if (!parse_ntlm_auth_domain_user(require_membership_of,
260 request.data.name.dom_name,
261 request.data.name.name)) {
262 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
263 require_membership_of));
264 return False;
267 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
268 NSS_STATUS_SUCCESS) {
269 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
270 require_membership_of));
271 return False;
274 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
276 if (require_membership_of_sid)
277 return True;
279 return False;
281 /* Authenticate a user with a plaintext password */
283 static bool check_plaintext_auth(const char *user, const char *pass,
284 bool stdout_diagnostics)
286 struct winbindd_request request;
287 struct winbindd_response response;
288 NSS_STATUS result;
290 if (!get_require_membership_sid()) {
291 return False;
294 /* Send off request */
296 ZERO_STRUCT(request);
297 ZERO_STRUCT(response);
299 fstrcpy(request.data.auth.user, user);
300 fstrcpy(request.data.auth.pass, pass);
301 if (require_membership_of_sid) {
302 strlcpy(request.data.auth.require_membership_of_sid,
303 require_membership_of_sid,
304 sizeof(request.data.auth.require_membership_of_sid));
307 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
309 /* Display response */
311 if (stdout_diagnostics) {
312 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
313 d_printf("Reading winbind reply failed! (0x01)\n");
316 d_printf("%s: %s (0x%x)\n",
317 response.data.auth.nt_status_string,
318 response.data.auth.error_string,
319 response.data.auth.nt_status);
320 } else {
321 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
322 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
325 DEBUG(3, ("%s: %s (0x%x)\n",
326 response.data.auth.nt_status_string,
327 response.data.auth.error_string,
328 response.data.auth.nt_status));
331 return (result == NSS_STATUS_SUCCESS);
334 /* authenticate a user with an encrypted username/password */
336 NTSTATUS contact_winbind_auth_crap(const char *username,
337 const char *domain,
338 const char *workstation,
339 const DATA_BLOB *challenge,
340 const DATA_BLOB *lm_response,
341 const DATA_BLOB *nt_response,
342 uint32 flags,
343 uint8 lm_key[8],
344 uint8 user_session_key[16],
345 char **error_string,
346 char **unix_name)
348 NTSTATUS nt_status;
349 NSS_STATUS result;
350 struct winbindd_request request;
351 struct winbindd_response response;
353 if (!get_require_membership_sid()) {
354 return NT_STATUS_INVALID_PARAMETER;
357 ZERO_STRUCT(request);
358 ZERO_STRUCT(response);
360 request.flags = flags;
362 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
364 if (require_membership_of_sid)
365 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
367 fstrcpy(request.data.auth_crap.user, username);
368 fstrcpy(request.data.auth_crap.domain, domain);
370 fstrcpy(request.data.auth_crap.workstation,
371 workstation);
373 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
375 if (lm_response && lm_response->length) {
376 memcpy(request.data.auth_crap.lm_resp,
377 lm_response->data,
378 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
379 request.data.auth_crap.lm_resp_len = lm_response->length;
382 if (nt_response && nt_response->length) {
383 if (nt_response->length > sizeof(request.data.auth_crap.nt_resp)) {
384 request.flags = request.flags | WBFLAG_BIG_NTLMV2_BLOB;
385 request.extra_len = nt_response->length;
386 request.extra_data.data = SMB_MALLOC_ARRAY(char, request.extra_len);
387 if (request.extra_data.data == NULL) {
388 return NT_STATUS_NO_MEMORY;
390 memcpy(request.extra_data.data, nt_response->data,
391 nt_response->length);
393 } else {
394 memcpy(request.data.auth_crap.nt_resp,
395 nt_response->data, nt_response->length);
397 request.data.auth_crap.nt_resp_len = nt_response->length;
400 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
401 SAFE_FREE(request.extra_data.data);
403 /* Display response */
405 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
406 nt_status = NT_STATUS_UNSUCCESSFUL;
407 if (error_string)
408 *error_string = smb_xstrdup("Reading winbind reply failed!");
409 winbindd_free_response(&response);
410 return nt_status;
413 nt_status = (NT_STATUS(response.data.auth.nt_status));
414 if (!NT_STATUS_IS_OK(nt_status)) {
415 if (error_string)
416 *error_string = smb_xstrdup(response.data.auth.error_string);
417 winbindd_free_response(&response);
418 return nt_status;
421 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
422 memcpy(lm_key, response.data.auth.first_8_lm_hash,
423 sizeof(response.data.auth.first_8_lm_hash));
425 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
426 memcpy(user_session_key, response.data.auth.user_session_key,
427 sizeof(response.data.auth.user_session_key));
430 if (flags & WBFLAG_PAM_UNIX_NAME) {
431 *unix_name = SMB_STRDUP(response.data.auth.unix_username);
432 if (!*unix_name) {
433 winbindd_free_response(&response);
434 return NT_STATUS_NO_MEMORY;
438 winbindd_free_response(&response);
439 return nt_status;
442 /* contact server to change user password using auth crap */
443 static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
444 const char *domain,
445 const DATA_BLOB new_nt_pswd,
446 const DATA_BLOB old_nt_hash_enc,
447 const DATA_BLOB new_lm_pswd,
448 const DATA_BLOB old_lm_hash_enc,
449 char **error_string)
451 NTSTATUS nt_status;
452 NSS_STATUS result;
453 struct winbindd_request request;
454 struct winbindd_response response;
456 if (!get_require_membership_sid())
458 if(error_string)
459 *error_string = smb_xstrdup("Can't get membership sid.");
460 return NT_STATUS_INVALID_PARAMETER;
463 ZERO_STRUCT(request);
464 ZERO_STRUCT(response);
466 if(username != NULL)
467 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
468 if(domain != NULL)
469 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
471 if(new_nt_pswd.length)
473 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
474 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
477 if(old_nt_hash_enc.length)
479 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));
480 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
483 if(new_lm_pswd.length)
485 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
486 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
489 if(old_lm_hash_enc.length)
491 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));
492 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
495 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
497 /* Display response */
499 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
501 nt_status = NT_STATUS_UNSUCCESSFUL;
502 if (error_string)
503 *error_string = smb_xstrdup("Reading winbind reply failed!");
504 winbindd_free_response(&response);
505 return nt_status;
508 nt_status = (NT_STATUS(response.data.auth.nt_status));
509 if (!NT_STATUS_IS_OK(nt_status))
511 if (error_string)
512 *error_string = smb_xstrdup(response.data.auth.error_string);
513 winbindd_free_response(&response);
514 return nt_status;
517 winbindd_free_response(&response);
519 return nt_status;
522 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
524 static const char zeros[16] = { 0, };
525 NTSTATUS nt_status;
526 char *error_string;
527 uint8 lm_key[8];
528 uint8 user_sess_key[16];
529 char *unix_name;
531 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
532 ntlmssp_state->workstation,
533 &ntlmssp_state->chal,
534 &ntlmssp_state->lm_resp,
535 &ntlmssp_state->nt_resp,
536 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
537 lm_key, user_sess_key,
538 &error_string, &unix_name);
540 if (NT_STATUS_IS_OK(nt_status)) {
541 if (memcmp(lm_key, zeros, 8) != 0) {
542 *lm_session_key = data_blob(NULL, 16);
543 memcpy(lm_session_key->data, lm_key, 8);
544 memset(lm_session_key->data+8, '\0', 8);
547 if (memcmp(user_sess_key, zeros, 16) != 0) {
548 *user_session_key = data_blob(user_sess_key, 16);
550 ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state->mem_ctx, unix_name);
551 SAFE_FREE(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;
560 return nt_status;
563 static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
565 NTSTATUS nt_status;
566 uint8 lm_pw[16], nt_pw[16];
568 nt_lm_owf_gen (opt_password, nt_pw, lm_pw);
570 nt_status = ntlm_password_check(ntlmssp_state->mem_ctx,
571 &ntlmssp_state->chal,
572 &ntlmssp_state->lm_resp,
573 &ntlmssp_state->nt_resp,
574 NULL, NULL,
575 ntlmssp_state->user,
576 ntlmssp_state->user,
577 ntlmssp_state->domain,
578 lm_pw, nt_pw, user_session_key, lm_session_key);
580 if (NT_STATUS_IS_OK(nt_status)) {
581 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state->mem_ctx,
582 "%s%c%s", ntlmssp_state->domain,
583 *lp_winbind_separator(),
584 ntlmssp_state->user);
585 } else {
586 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
587 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
588 nt_errstr(nt_status)));
589 ntlmssp_state->auth_context = NULL;
591 return nt_status;
594 static NTSTATUS ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE **client_ntlmssp_state)
596 NTSTATUS status;
597 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
598 status = NT_STATUS_UNSUCCESSFUL;
599 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
600 return NT_STATUS_INVALID_PARAMETER;
603 status = ntlmssp_client_start(client_ntlmssp_state);
605 if (!NT_STATUS_IS_OK(status)) {
606 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
607 nt_errstr(status)));
608 ntlmssp_end(client_ntlmssp_state);
609 return status;
612 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
614 if (!NT_STATUS_IS_OK(status)) {
615 DEBUG(1, ("Could not set username: %s\n",
616 nt_errstr(status)));
617 ntlmssp_end(client_ntlmssp_state);
618 return status;
621 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
623 if (!NT_STATUS_IS_OK(status)) {
624 DEBUG(1, ("Could not set domain: %s\n",
625 nt_errstr(status)));
626 ntlmssp_end(client_ntlmssp_state);
627 return status;
630 if (opt_password) {
631 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
633 if (!NT_STATUS_IS_OK(status)) {
634 DEBUG(1, ("Could not set password: %s\n",
635 nt_errstr(status)));
636 ntlmssp_end(client_ntlmssp_state);
637 return status;
641 return NT_STATUS_OK;
644 static NTSTATUS ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE **ntlmssp_state)
646 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
648 if (!NT_STATUS_IS_OK(status)) {
649 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
650 nt_errstr(status)));
651 return status;
654 /* Have we been given a local password, or should we ask winbind? */
655 if (opt_password) {
656 (*ntlmssp_state)->check_password = local_pw_check;
657 (*ntlmssp_state)->get_domain = lp_workgroup;
658 (*ntlmssp_state)->get_global_myname = global_myname;
659 } else {
660 (*ntlmssp_state)->check_password = winbind_pw_check;
661 (*ntlmssp_state)->get_domain = get_winbind_domain;
662 (*ntlmssp_state)->get_global_myname = get_winbind_netbios_name;
664 return NT_STATUS_OK;
667 /*******************************************************************
668 Used by firefox to drive NTLM auth to IIS servers.
669 *******************************************************************/
671 static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
672 DATA_BLOB *reply)
674 struct winbindd_request wb_request;
675 struct winbindd_response wb_response;
676 NSS_STATUS result;
678 /* get winbindd to do the ntlmssp step on our behalf */
679 ZERO_STRUCT(wb_request);
680 ZERO_STRUCT(wb_response);
682 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
683 "%s%c%s", opt_domain, winbind_separator(), opt_username);
684 wb_request.data.ccache_ntlm_auth.uid = geteuid();
685 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
686 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
687 wb_request.extra_len = initial_msg.length + challenge_msg.length;
689 if (wb_request.extra_len > 0) {
690 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
691 if (wb_request.extra_data.data == NULL) {
692 return NT_STATUS_NO_MEMORY;
695 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
696 memcpy(wb_request.extra_data.data + initial_msg.length,
697 challenge_msg.data, challenge_msg.length);
700 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
701 SAFE_FREE(wb_request.extra_data.data);
703 if (result != NSS_STATUS_SUCCESS) {
704 winbindd_free_response(&wb_response);
705 return NT_STATUS_UNSUCCESSFUL;
708 if (reply) {
709 *reply = data_blob(wb_response.extra_data.data,
710 wb_response.data.ccache_ntlm_auth.auth_blob_len);
711 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
712 reply->data == NULL) {
713 winbindd_free_response(&wb_response);
714 return NT_STATUS_NO_MEMORY;
718 winbindd_free_response(&wb_response);
719 return NT_STATUS_MORE_PROCESSING_REQUIRED;
722 static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
723 char *buf, int length)
725 DATA_BLOB request, reply;
726 NTSTATUS nt_status;
728 if (strlen(buf) < 2) {
729 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
730 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
731 return;
734 if (strlen(buf) > 3) {
735 if(strncmp(buf, "SF ", 3) == 0){
736 DEBUG(10, ("Setting flags to negotioate\n"));
737 TALLOC_FREE(state->want_feature_list);
738 state->want_feature_list = talloc_strdup(state->mem_ctx,
739 buf+3);
740 x_fprintf(x_stdout, "OK\n");
741 return;
743 request = base64_decode_data_blob(buf + 3);
744 } else {
745 request = data_blob_null;
748 if ((strncmp(buf, "PW ", 3) == 0)) {
749 /* The calling application wants us to use a local password
750 * (rather than winbindd) */
752 opt_password = SMB_STRNDUP((const char *)request.data,
753 request.length);
755 if (opt_password == NULL) {
756 DEBUG(1, ("Out of memory\n"));
757 x_fprintf(x_stdout, "BH Out of memory\n");
758 data_blob_free(&request);
759 return;
762 x_fprintf(x_stdout, "OK\n");
763 data_blob_free(&request);
764 return;
767 if (strncmp(buf, "YR", 2) == 0) {
768 if (state->ntlmssp_state)
769 ntlmssp_end(&state->ntlmssp_state);
770 state->svr_state = SERVER_INITIAL;
771 } else if (strncmp(buf, "KK", 2) == 0) {
772 /* No special preprocessing required */
773 } else if (strncmp(buf, "GF", 2) == 0) {
774 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
776 if (state->svr_state == SERVER_FINISHED) {
777 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
779 else {
780 x_fprintf(x_stdout, "BH\n");
782 data_blob_free(&request);
783 return;
784 } else if (strncmp(buf, "GK", 2) == 0) {
785 DEBUG(10, ("Requested NTLMSSP session key\n"));
786 if(state->have_session_key) {
787 char *key64 = base64_encode_data_blob(state->mem_ctx,
788 state->session_key);
789 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
790 TALLOC_FREE(key64);
791 } else {
792 x_fprintf(x_stdout, "BH\n");
795 data_blob_free(&request);
796 return;
797 } else {
798 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
799 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
800 return;
803 if (!state->ntlmssp_state) {
804 nt_status = ntlm_auth_start_ntlmssp_server(
805 &state->ntlmssp_state);
806 if (!NT_STATUS_IS_OK(nt_status)) {
807 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
808 return;
810 ntlmssp_want_feature_list(state->ntlmssp_state,
811 state->want_feature_list);
814 DEBUG(10, ("got NTLMSSP packet:\n"));
815 dump_data(10, request.data, request.length);
817 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
819 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
820 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
821 reply);
822 x_fprintf(x_stdout, "TT %s\n", reply_base64);
823 TALLOC_FREE(reply_base64);
824 data_blob_free(&reply);
825 state->svr_state = SERVER_CHALLENGE;
826 DEBUG(10, ("NTLMSSP challenge\n"));
827 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
828 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
829 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
831 ntlmssp_end(&state->ntlmssp_state);
832 } else if (!NT_STATUS_IS_OK(nt_status)) {
833 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
834 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
835 } else {
836 x_fprintf(x_stdout, "AF %s\n",
837 (char *)state->ntlmssp_state->auth_context);
838 DEBUG(10, ("NTLMSSP OK!\n"));
840 if(state->have_session_key)
841 data_blob_free(&state->session_key);
842 state->session_key = data_blob(
843 state->ntlmssp_state->session_key.data,
844 state->ntlmssp_state->session_key.length);
845 state->neg_flags = state->ntlmssp_state->neg_flags;
846 state->have_session_key = true;
847 state->svr_state = SERVER_FINISHED;
850 data_blob_free(&request);
853 static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
854 char *buf, int length)
856 DATA_BLOB request, reply;
857 NTSTATUS nt_status;
859 if (!opt_username || !*opt_username) {
860 x_fprintf(x_stderr, "username must be specified!\n\n");
861 exit(1);
864 if (strlen(buf) < 2) {
865 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
866 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
867 return;
870 if (strlen(buf) > 3) {
871 if(strncmp(buf, "SF ", 3) == 0) {
872 DEBUG(10, ("Looking for flags to negotiate\n"));
873 talloc_free(state->want_feature_list);
874 state->want_feature_list = talloc_strdup(state->mem_ctx,
875 buf+3);
876 x_fprintf(x_stdout, "OK\n");
877 return;
879 request = base64_decode_data_blob(buf + 3);
880 } else {
881 request = data_blob_null;
884 if (strncmp(buf, "PW ", 3) == 0) {
885 /* We asked for a password and obviously got it :-) */
887 opt_password = SMB_STRNDUP((const char *)request.data,
888 request.length);
890 if (opt_password == NULL) {
891 DEBUG(1, ("Out of memory\n"));
892 x_fprintf(x_stdout, "BH Out of memory\n");
893 data_blob_free(&request);
894 return;
897 x_fprintf(x_stdout, "OK\n");
898 data_blob_free(&request);
899 return;
902 if (!state->ntlmssp_state && use_cached_creds) {
903 /* check whether cached credentials are usable. */
904 DATA_BLOB empty_blob = data_blob_null;
906 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
907 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
908 /* failed to use cached creds */
909 use_cached_creds = False;
913 if (opt_password == NULL && !use_cached_creds) {
914 /* Request a password from the calling process. After
915 sending it, the calling process should retry asking for the
916 negotiate. */
918 DEBUG(10, ("Requesting password\n"));
919 x_fprintf(x_stdout, "PW\n");
920 return;
923 if (strncmp(buf, "YR", 2) == 0) {
924 if (state->ntlmssp_state)
925 ntlmssp_end(&state->ntlmssp_state);
926 state->cli_state = CLIENT_INITIAL;
927 } else if (strncmp(buf, "TT", 2) == 0) {
928 /* No special preprocessing required */
929 } else if (strncmp(buf, "GF", 2) == 0) {
930 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
932 if(state->cli_state == CLIENT_FINISHED) {
933 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
935 else {
936 x_fprintf(x_stdout, "BH\n");
939 data_blob_free(&request);
940 return;
941 } else if (strncmp(buf, "GK", 2) == 0 ) {
942 DEBUG(10, ("Requested session key\n"));
944 if(state->cli_state == CLIENT_FINISHED) {
945 char *key64 = base64_encode_data_blob(state->mem_ctx,
946 state->session_key);
947 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
948 TALLOC_FREE(key64);
950 else {
951 x_fprintf(x_stdout, "BH\n");
954 data_blob_free(&request);
955 return;
956 } else {
957 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
958 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
959 return;
962 if (!state->ntlmssp_state) {
963 nt_status = ntlm_auth_start_ntlmssp_client(
964 &state->ntlmssp_state);
965 if (!NT_STATUS_IS_OK(nt_status)) {
966 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
967 return;
969 ntlmssp_want_feature_list(state->ntlmssp_state,
970 state->want_feature_list);
971 state->initial_message = data_blob_null;
974 DEBUG(10, ("got NTLMSSP packet:\n"));
975 dump_data(10, request.data, request.length);
977 if (use_cached_creds && !opt_password &&
978 (state->cli_state == CLIENT_RESPONSE)) {
979 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
980 &reply);
981 } else {
982 nt_status = ntlmssp_update(state->ntlmssp_state, request,
983 &reply);
986 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
987 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
988 reply);
989 if (state->cli_state == CLIENT_INITIAL) {
990 x_fprintf(x_stdout, "YR %s\n", reply_base64);
991 state->initial_message = reply;
992 state->cli_state = CLIENT_RESPONSE;
993 } else {
994 x_fprintf(x_stdout, "KK %s\n", reply_base64);
995 data_blob_free(&reply);
997 TALLOC_FREE(reply_base64);
998 DEBUG(10, ("NTLMSSP challenge\n"));
999 } else if (NT_STATUS_IS_OK(nt_status)) {
1000 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1001 reply);
1002 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1003 TALLOC_FREE(reply_base64);
1005 if(state->have_session_key)
1006 data_blob_free(&state->session_key);
1008 state->session_key = data_blob(
1009 state->ntlmssp_state->session_key.data,
1010 state->ntlmssp_state->session_key.length);
1011 state->neg_flags = state->ntlmssp_state->neg_flags;
1012 state->have_session_key = true;
1014 DEBUG(10, ("NTLMSSP OK!\n"));
1015 state->cli_state = CLIENT_FINISHED;
1016 if (state->ntlmssp_state)
1017 ntlmssp_end(&state->ntlmssp_state);
1018 } else {
1019 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1020 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1021 state->cli_state = CLIENT_ERROR;
1022 if (state->ntlmssp_state)
1023 ntlmssp_end(&state->ntlmssp_state);
1026 data_blob_free(&request);
1029 static void manage_squid_basic_request(struct ntlm_auth_state *state,
1030 char *buf, int length)
1032 char *user, *pass;
1033 user=buf;
1035 pass=(char *)memchr(buf,' ',length);
1036 if (!pass) {
1037 DEBUG(2, ("Password not found. Denying access\n"));
1038 x_fprintf(x_stdout, "ERR\n");
1039 return;
1041 *pass='\0';
1042 pass++;
1044 if (state->helper_mode == SQUID_2_5_BASIC) {
1045 rfc1738_unescape(user);
1046 rfc1738_unescape(pass);
1049 if (check_plaintext_auth(user, pass, False)) {
1050 x_fprintf(x_stdout, "OK\n");
1051 } else {
1052 x_fprintf(x_stdout, "ERR\n");
1056 static void offer_gss_spnego_mechs(void) {
1058 DATA_BLOB token;
1059 SPNEGO_DATA spnego;
1060 ssize_t len;
1061 char *reply_base64;
1062 TALLOC_CTX *ctx = talloc_tos();
1063 char *principal;
1064 char *myname_lower;
1066 ZERO_STRUCT(spnego);
1068 myname_lower = talloc_strdup(ctx, global_myname());
1069 if (!myname_lower) {
1070 return;
1072 strlower_m(myname_lower);
1074 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1075 if (!principal) {
1076 return;
1079 /* Server negTokenInit (mech offerings) */
1080 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1081 spnego.negTokenInit.mechTypes = SMB_XMALLOC_ARRAY(const char *, 2);
1082 #ifdef HAVE_KRB5
1083 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_KERBEROS5_OLD);
1084 spnego.negTokenInit.mechTypes[1] = smb_xstrdup(OID_NTLMSSP);
1085 spnego.negTokenInit.mechTypes[2] = NULL;
1086 #else
1087 spnego.negTokenInit.mechTypes[0] = smb_xstrdup(OID_NTLMSSP);
1088 spnego.negTokenInit.mechTypes[1] = NULL;
1089 #endif
1092 spnego.negTokenInit.mechListMIC = data_blob(principal,
1093 strlen(principal));
1095 len = write_spnego_data(&token, &spnego);
1096 free_spnego_data(&spnego);
1098 if (len == -1) {
1099 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1100 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1101 return;
1104 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1105 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1107 TALLOC_FREE(reply_base64);
1108 data_blob_free(&token);
1109 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1110 return;
1113 static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1114 char *buf, int length)
1116 static NTLMSSP_STATE *ntlmssp_state = NULL;
1117 SPNEGO_DATA request, response;
1118 DATA_BLOB token;
1119 NTSTATUS status;
1120 ssize_t len;
1121 TALLOC_CTX *ctx = talloc_tos();
1123 char *user = NULL;
1124 char *domain = NULL;
1126 const char *reply_code;
1127 char *reply_base64;
1128 char *reply_argument = NULL;
1130 if (strlen(buf) < 2) {
1131 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1132 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1133 return;
1136 if (strncmp(buf, "YR", 2) == 0) {
1137 if (ntlmssp_state)
1138 ntlmssp_end(&ntlmssp_state);
1139 } else if (strncmp(buf, "KK", 2) == 0) {
1141 } else {
1142 DEBUG(1, ("SPENGO query [%s] invalid", buf));
1143 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1144 return;
1147 if ( (strlen(buf) == 2)) {
1149 /* no client data, get the negTokenInit offering
1150 mechanisms */
1152 offer_gss_spnego_mechs();
1153 return;
1156 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1158 if (strlen(buf) <= 3) {
1159 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1160 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1161 return;
1164 token = base64_decode_data_blob(buf + 3);
1165 len = read_spnego_data(token, &request);
1166 data_blob_free(&token);
1168 if (len == -1) {
1169 DEBUG(1, ("GSS-SPNEGO query [%s] invalid", buf));
1170 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1171 return;
1174 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1176 /* Second request from Client. This is where the
1177 client offers its mechanism to use. */
1179 if ( (request.negTokenInit.mechTypes == NULL) ||
1180 (request.negTokenInit.mechTypes[0] == NULL) ) {
1181 DEBUG(1, ("Client did not offer any mechanism"));
1182 x_fprintf(x_stdout, "BH Client did not offer any "
1183 "mechanism\n");
1184 return;
1187 status = NT_STATUS_UNSUCCESSFUL;
1188 if (strcmp(request.negTokenInit.mechTypes[0], OID_NTLMSSP) == 0) {
1190 if ( request.negTokenInit.mechToken.data == NULL ) {
1191 DEBUG(1, ("Client did not provide NTLMSSP data\n"));
1192 x_fprintf(x_stdout, "BH Client did not provide "
1193 "NTLMSSP data\n");
1194 return;
1197 if ( ntlmssp_state != NULL ) {
1198 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1199 "already got one\n"));
1200 x_fprintf(x_stdout, "BH Client wants a new "
1201 "NTLMSSP challenge, but "
1202 "already got one\n");
1203 ntlmssp_end(&ntlmssp_state);
1204 return;
1207 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_server(&ntlmssp_state))) {
1208 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1209 return;
1212 DEBUG(10, ("got NTLMSSP packet:\n"));
1213 dump_data(10, request.negTokenInit.mechToken.data,
1214 request.negTokenInit.mechToken.length);
1216 response.type = SPNEGO_NEG_TOKEN_TARG;
1217 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1218 response.negTokenTarg.mechListMIC = data_blob_null;
1220 status = ntlmssp_update(ntlmssp_state,
1221 request.negTokenInit.mechToken,
1222 &response.negTokenTarg.responseToken);
1225 #ifdef HAVE_KRB5
1226 if (strcmp(request.negTokenInit.mechTypes[0], OID_KERBEROS5_OLD) == 0) {
1228 TALLOC_CTX *mem_ctx = talloc_init("manage_gss_spnego_request");
1229 char *principal;
1230 DATA_BLOB ap_rep;
1231 DATA_BLOB session_key;
1232 struct PAC_DATA *pac_data = NULL;
1234 if ( request.negTokenInit.mechToken.data == NULL ) {
1235 DEBUG(1, ("Client did not provide Kerberos data\n"));
1236 x_fprintf(x_stdout, "BH Client did not provide "
1237 "Kerberos data\n");
1238 return;
1241 response.type = SPNEGO_NEG_TOKEN_TARG;
1242 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_KERBEROS5_OLD);
1243 response.negTokenTarg.mechListMIC = data_blob_null;
1244 response.negTokenTarg.responseToken = data_blob_null;
1246 status = ads_verify_ticket(mem_ctx, lp_realm(), 0,
1247 &request.negTokenInit.mechToken,
1248 &principal, &pac_data, &ap_rep,
1249 &session_key, True);
1251 talloc_destroy(mem_ctx);
1253 /* Now in "principal" we have the name we are
1254 authenticated as. */
1256 if (NT_STATUS_IS_OK(status)) {
1258 domain = strchr_m(principal, '@');
1260 if (domain == NULL) {
1261 DEBUG(1, ("Did not get a valid principal "
1262 "from ads_verify_ticket\n"));
1263 x_fprintf(x_stdout, "BH Did not get a "
1264 "valid principal from "
1265 "ads_verify_ticket\n");
1266 return;
1269 *domain++ = '\0';
1270 domain = SMB_STRDUP(domain);
1271 user = SMB_STRDUP(principal);
1273 data_blob_free(&ap_rep);
1275 SAFE_FREE(principal);
1278 #endif
1280 } else {
1282 if ( (request.negTokenTarg.supportedMech == NULL) ||
1283 ( strcmp(request.negTokenTarg.supportedMech, OID_NTLMSSP) != 0 ) ) {
1284 /* Kerberos should never send a negTokenTarg, OID_NTLMSSP
1285 is the only one we support that sends this stuff */
1286 DEBUG(1, ("Got a negTokenTarg for something non-NTLMSSP: %s\n",
1287 request.negTokenTarg.supportedMech));
1288 x_fprintf(x_stdout, "BH Got a negTokenTarg for "
1289 "something non-NTLMSSP\n");
1290 return;
1293 if (request.negTokenTarg.responseToken.data == NULL) {
1294 DEBUG(1, ("Got a negTokenTarg without a responseToken!\n"));
1295 x_fprintf(x_stdout, "BH Got a negTokenTarg without a "
1296 "responseToken!\n");
1297 return;
1300 status = ntlmssp_update(ntlmssp_state,
1301 request.negTokenTarg.responseToken,
1302 &response.negTokenTarg.responseToken);
1304 response.type = SPNEGO_NEG_TOKEN_TARG;
1305 response.negTokenTarg.supportedMech = SMB_STRDUP(OID_NTLMSSP);
1306 response.negTokenTarg.mechListMIC = data_blob_null;
1308 if (NT_STATUS_IS_OK(status)) {
1309 user = SMB_STRDUP(ntlmssp_state->user);
1310 domain = SMB_STRDUP(ntlmssp_state->domain);
1311 ntlmssp_end(&ntlmssp_state);
1315 free_spnego_data(&request);
1317 if (NT_STATUS_IS_OK(status)) {
1318 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1319 reply_code = "AF";
1320 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1321 } else if (NT_STATUS_EQUAL(status,
1322 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1323 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1324 reply_code = "TT";
1325 reply_argument = talloc_strdup(ctx, "*");
1326 } else {
1327 response.negTokenTarg.negResult = SPNEGO_REJECT;
1328 reply_code = "NA";
1329 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1332 if (!reply_argument) {
1333 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1334 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1335 return;
1338 SAFE_FREE(user);
1339 SAFE_FREE(domain);
1341 len = write_spnego_data(&token, &response);
1342 free_spnego_data(&response);
1344 if (len == -1) {
1345 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1346 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1347 return;
1350 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1352 x_fprintf(x_stdout, "%s %s %s\n",
1353 reply_code, reply_base64, reply_argument);
1355 TALLOC_FREE(reply_base64);
1356 data_blob_free(&token);
1358 return;
1361 static NTLMSSP_STATE *client_ntlmssp_state = NULL;
1363 static bool manage_client_ntlmssp_init(SPNEGO_DATA spnego)
1365 NTSTATUS status;
1366 DATA_BLOB null_blob = data_blob_null;
1367 DATA_BLOB to_server;
1368 char *to_server_base64;
1369 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1371 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1373 if (client_ntlmssp_state != NULL) {
1374 DEBUG(1, ("Request for initial SPNEGO request where "
1375 "we already have a state\n"));
1376 return False;
1379 if (!client_ntlmssp_state) {
1380 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1381 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1382 return False;
1387 if (opt_password == NULL) {
1389 /* Request a password from the calling process. After
1390 sending it, the calling process should retry with
1391 the negTokenInit. */
1393 DEBUG(10, ("Requesting password\n"));
1394 x_fprintf(x_stdout, "PW\n");
1395 return True;
1398 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1399 spnego.negTokenInit.mechTypes = my_mechs;
1400 spnego.negTokenInit.reqFlags = 0;
1401 spnego.negTokenInit.mechListMIC = null_blob;
1403 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1404 &spnego.negTokenInit.mechToken);
1406 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1407 NT_STATUS_IS_OK(status)) ) {
1408 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1409 nt_errstr(status)));
1410 ntlmssp_end(&client_ntlmssp_state);
1411 return False;
1414 write_spnego_data(&to_server, &spnego);
1415 data_blob_free(&spnego.negTokenInit.mechToken);
1417 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1418 data_blob_free(&to_server);
1419 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1420 TALLOC_FREE(to_server_base64);
1421 return True;
1424 static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
1426 NTSTATUS status;
1427 DATA_BLOB null_blob = data_blob_null;
1428 DATA_BLOB request;
1429 DATA_BLOB to_server;
1430 char *to_server_base64;
1432 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1434 if (client_ntlmssp_state == NULL) {
1435 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1436 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1437 return;
1440 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1441 x_fprintf(x_stdout, "NA\n");
1442 ntlmssp_end(&client_ntlmssp_state);
1443 return;
1446 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1447 x_fprintf(x_stdout, "AF\n");
1448 ntlmssp_end(&client_ntlmssp_state);
1449 return;
1452 status = ntlmssp_update(client_ntlmssp_state,
1453 spnego.negTokenTarg.responseToken,
1454 &request);
1456 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1457 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1458 "ntlmssp_client_update, got: %s\n",
1459 nt_errstr(status)));
1460 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1461 "ntlmssp_client_update\n");
1462 data_blob_free(&request);
1463 ntlmssp_end(&client_ntlmssp_state);
1464 return;
1467 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1468 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1469 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1470 spnego.negTokenTarg.responseToken = request;
1471 spnego.negTokenTarg.mechListMIC = null_blob;
1473 write_spnego_data(&to_server, &spnego);
1474 data_blob_free(&request);
1476 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1477 data_blob_free(&to_server);
1478 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1479 TALLOC_FREE(to_server_base64);
1480 return;
1483 #ifdef HAVE_KRB5
1485 static bool manage_client_krb5_init(SPNEGO_DATA spnego)
1487 char *principal;
1488 DATA_BLOB tkt, to_server;
1489 DATA_BLOB session_key_krb5 = data_blob_null;
1490 SPNEGO_DATA reply;
1491 char *reply_base64;
1492 int retval;
1494 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1495 ssize_t len;
1497 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1498 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1499 DEBUG(1, ("Did not get a principal for krb5\n"));
1500 return False;
1503 principal = (char *)SMB_MALLOC(
1504 spnego.negTokenInit.mechListMIC.length+1);
1506 if (principal == NULL) {
1507 DEBUG(1, ("Could not malloc principal\n"));
1508 return False;
1511 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1512 spnego.negTokenInit.mechListMIC.length);
1513 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1515 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL);
1517 if (retval) {
1518 char *user = NULL;
1520 /* Let's try to first get the TGT, for that we need a
1521 password. */
1523 if (opt_password == NULL) {
1524 DEBUG(10, ("Requesting password\n"));
1525 x_fprintf(x_stdout, "PW\n");
1526 return True;
1529 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1530 if (!user) {
1531 return false;
1534 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1535 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1536 return False;
1539 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL);
1541 if (retval) {
1542 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1543 return False;
1547 data_blob_free(&session_key_krb5);
1549 ZERO_STRUCT(reply);
1551 reply.type = SPNEGO_NEG_TOKEN_INIT;
1552 reply.negTokenInit.mechTypes = my_mechs;
1553 reply.negTokenInit.reqFlags = 0;
1554 reply.negTokenInit.mechToken = tkt;
1555 reply.negTokenInit.mechListMIC = data_blob_null;
1557 len = write_spnego_data(&to_server, &reply);
1558 data_blob_free(&tkt);
1560 if (len == -1) {
1561 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1562 return False;
1565 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1566 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1568 TALLOC_FREE(reply_base64);
1569 data_blob_free(&to_server);
1570 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1571 return True;
1574 static void manage_client_krb5_targ(SPNEGO_DATA spnego)
1576 switch (spnego.negTokenTarg.negResult) {
1577 case SPNEGO_ACCEPT_INCOMPLETE:
1578 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1579 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1580 "ACCEPT_INCOMPLETE\n");
1581 break;
1582 case SPNEGO_ACCEPT_COMPLETED:
1583 DEBUG(10, ("Accept completed\n"));
1584 x_fprintf(x_stdout, "AF\n");
1585 break;
1586 case SPNEGO_REJECT:
1587 DEBUG(10, ("Rejected\n"));
1588 x_fprintf(x_stdout, "NA\n");
1589 break;
1590 default:
1591 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1592 x_fprintf(x_stdout, "AF\n");
1596 #endif
1598 static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1599 char *buf, int length)
1601 DATA_BLOB request;
1602 SPNEGO_DATA spnego;
1603 ssize_t len;
1605 if (!opt_username || !*opt_username) {
1606 x_fprintf(x_stderr, "username must be specified!\n\n");
1607 exit(1);
1610 if (strlen(buf) <= 3) {
1611 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1612 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1613 return;
1616 request = base64_decode_data_blob(buf+3);
1618 if (strncmp(buf, "PW ", 3) == 0) {
1620 /* We asked for a password and obviously got it :-) */
1622 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1624 if (opt_password == NULL) {
1625 DEBUG(1, ("Out of memory\n"));
1626 x_fprintf(x_stdout, "BH Out of memory\n");
1627 data_blob_free(&request);
1628 return;
1631 x_fprintf(x_stdout, "OK\n");
1632 data_blob_free(&request);
1633 return;
1636 if ( (strncmp(buf, "TT ", 3) != 0) &&
1637 (strncmp(buf, "AF ", 3) != 0) &&
1638 (strncmp(buf, "NA ", 3) != 0) ) {
1639 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1640 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1641 data_blob_free(&request);
1642 return;
1645 /* So we got a server challenge to generate a SPNEGO
1646 client-to-server request... */
1648 len = read_spnego_data(request, &spnego);
1649 data_blob_free(&request);
1651 if (len == -1) {
1652 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1653 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1654 return;
1657 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1659 /* The server offers a list of mechanisms */
1661 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1663 while (*mechType != NULL) {
1665 #ifdef HAVE_KRB5
1666 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1667 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1668 if (manage_client_krb5_init(spnego))
1669 goto out;
1671 #endif
1673 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1674 if (manage_client_ntlmssp_init(spnego))
1675 goto out;
1678 mechType++;
1681 DEBUG(1, ("Server offered no compatible mechanism\n"));
1682 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1683 return;
1686 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1688 if (spnego.negTokenTarg.supportedMech == NULL) {
1689 /* On accept/reject Windows does not send the
1690 mechanism anymore. Handle that here and
1691 shut down the mechanisms. */
1693 switch (spnego.negTokenTarg.negResult) {
1694 case SPNEGO_ACCEPT_COMPLETED:
1695 x_fprintf(x_stdout, "AF\n");
1696 break;
1697 case SPNEGO_REJECT:
1698 x_fprintf(x_stdout, "NA\n");
1699 break;
1700 default:
1701 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1702 "unknown negResult: %d\n",
1703 spnego.negTokenTarg.negResult));
1704 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1705 " no mech and an unknown "
1706 "negResult\n");
1709 ntlmssp_end(&client_ntlmssp_state);
1710 goto out;
1713 if (strcmp(spnego.negTokenTarg.supportedMech,
1714 OID_NTLMSSP) == 0) {
1715 manage_client_ntlmssp_targ(spnego);
1716 goto out;
1719 #if HAVE_KRB5
1720 if (strcmp(spnego.negTokenTarg.supportedMech,
1721 OID_KERBEROS5_OLD) == 0) {
1722 manage_client_krb5_targ(spnego);
1723 goto out;
1725 #endif
1729 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1730 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1731 return;
1733 out:
1734 free_spnego_data(&spnego);
1735 return;
1738 static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1739 char *buf, int length)
1741 char *request, *parameter;
1742 static DATA_BLOB challenge;
1743 static DATA_BLOB lm_response;
1744 static DATA_BLOB nt_response;
1745 static char *full_username;
1746 static char *username;
1747 static char *domain;
1748 static char *plaintext_password;
1749 static bool ntlm_server_1_user_session_key;
1750 static bool ntlm_server_1_lm_session_key;
1752 if (strequal(buf, ".")) {
1753 if (!full_username && !username) {
1754 x_fprintf(x_stdout, "Error: No username supplied!\n");
1755 } else if (plaintext_password) {
1756 /* handle this request as plaintext */
1757 if (!full_username) {
1758 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1759 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1760 return;
1763 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1764 x_fprintf(x_stdout, "Authenticated: Yes\n");
1765 } else {
1766 x_fprintf(x_stdout, "Authenticated: No\n");
1768 } else if (!lm_response.data && !nt_response.data) {
1769 x_fprintf(x_stdout, "Error: No password supplied!\n");
1770 } else if (!challenge.data) {
1771 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
1772 } else {
1773 char *error_string = NULL;
1774 uchar lm_key[8];
1775 uchar user_session_key[16];
1776 uint32 flags = 0;
1778 if (full_username && !username) {
1779 fstring fstr_user;
1780 fstring fstr_domain;
1782 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
1783 /* username might be 'tainted', don't print into our new-line deleimianted stream */
1784 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
1786 SAFE_FREE(username);
1787 SAFE_FREE(domain);
1788 username = smb_xstrdup(fstr_user);
1789 domain = smb_xstrdup(fstr_domain);
1792 if (!domain) {
1793 domain = smb_xstrdup(get_winbind_domain());
1796 if (ntlm_server_1_lm_session_key)
1797 flags |= WBFLAG_PAM_LMKEY;
1799 if (ntlm_server_1_user_session_key)
1800 flags |= WBFLAG_PAM_USER_SESSION_KEY;
1802 if (!NT_STATUS_IS_OK(
1803 contact_winbind_auth_crap(username,
1804 domain,
1805 global_myname(),
1806 &challenge,
1807 &lm_response,
1808 &nt_response,
1809 flags,
1810 lm_key,
1811 user_session_key,
1812 &error_string,
1813 NULL))) {
1815 x_fprintf(x_stdout, "Authenticated: No\n");
1816 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
1817 SAFE_FREE(error_string);
1818 } else {
1819 static char zeros[16];
1820 char *hex_lm_key;
1821 char *hex_user_session_key;
1823 x_fprintf(x_stdout, "Authenticated: Yes\n");
1825 if (ntlm_server_1_lm_session_key
1826 && (memcmp(zeros, lm_key,
1827 sizeof(lm_key)) != 0)) {
1828 hex_lm_key = hex_encode(NULL,
1829 (const unsigned char *)lm_key,
1830 sizeof(lm_key));
1831 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
1832 TALLOC_FREE(hex_lm_key);
1835 if (ntlm_server_1_user_session_key
1836 && (memcmp(zeros, user_session_key,
1837 sizeof(user_session_key)) != 0)) {
1838 hex_user_session_key = hex_encode(NULL,
1839 (const unsigned char *)user_session_key,
1840 sizeof(user_session_key));
1841 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
1842 TALLOC_FREE(hex_user_session_key);
1846 /* clear out the state */
1847 challenge = data_blob_null;
1848 nt_response = data_blob_null;
1849 lm_response = data_blob_null;
1850 SAFE_FREE(full_username);
1851 SAFE_FREE(username);
1852 SAFE_FREE(domain);
1853 SAFE_FREE(plaintext_password);
1854 ntlm_server_1_user_session_key = False;
1855 ntlm_server_1_lm_session_key = False;
1856 x_fprintf(x_stdout, ".\n");
1858 return;
1861 request = buf;
1863 /* Indicates a base64 encoded structure */
1864 parameter = strstr_m(request, ":: ");
1865 if (!parameter) {
1866 parameter = strstr_m(request, ": ");
1868 if (!parameter) {
1869 DEBUG(0, ("Parameter not found!\n"));
1870 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
1871 return;
1874 parameter[0] ='\0';
1875 parameter++;
1876 parameter[0] ='\0';
1877 parameter++;
1879 } else {
1880 parameter[0] ='\0';
1881 parameter++;
1882 parameter[0] ='\0';
1883 parameter++;
1884 parameter[0] ='\0';
1885 parameter++;
1887 base64_decode_inplace(parameter);
1890 if (strequal(request, "LANMAN-Challenge")) {
1891 challenge = strhex_to_data_blob(NULL, parameter);
1892 if (challenge.length != 8) {
1893 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
1894 parameter,
1895 (int)challenge.length);
1896 challenge = data_blob_null;
1898 } else if (strequal(request, "NT-Response")) {
1899 nt_response = strhex_to_data_blob(NULL, parameter);
1900 if (nt_response.length < 24) {
1901 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
1902 parameter,
1903 (int)nt_response.length);
1904 nt_response = data_blob_null;
1906 } else if (strequal(request, "LANMAN-Response")) {
1907 lm_response = strhex_to_data_blob(NULL, parameter);
1908 if (lm_response.length != 24) {
1909 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
1910 parameter,
1911 (int)lm_response.length);
1912 lm_response = data_blob_null;
1914 } else if (strequal(request, "Password")) {
1915 plaintext_password = smb_xstrdup(parameter);
1916 } else if (strequal(request, "NT-Domain")) {
1917 domain = smb_xstrdup(parameter);
1918 } else if (strequal(request, "Username")) {
1919 username = smb_xstrdup(parameter);
1920 } else if (strequal(request, "Full-Username")) {
1921 full_username = smb_xstrdup(parameter);
1922 } else if (strequal(request, "Request-User-Session-Key")) {
1923 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
1924 } else if (strequal(request, "Request-LanMan-Session-Key")) {
1925 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
1926 } else {
1927 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
1931 static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
1932 char *buf, int length)
1934 char *request, *parameter;
1935 static DATA_BLOB new_nt_pswd;
1936 static DATA_BLOB old_nt_hash_enc;
1937 static DATA_BLOB new_lm_pswd;
1938 static DATA_BLOB old_lm_hash_enc;
1939 static char *full_username = NULL;
1940 static char *username = NULL;
1941 static char *domain = NULL;
1942 static char *newpswd = NULL;
1943 static char *oldpswd = NULL;
1945 if (strequal(buf, ".")) {
1946 if(newpswd && oldpswd) {
1947 uchar old_nt_hash[16];
1948 uchar old_lm_hash[16];
1949 uchar new_nt_hash[16];
1950 uchar new_lm_hash[16];
1952 new_nt_pswd = data_blob(NULL, 516);
1953 old_nt_hash_enc = data_blob(NULL, 16);
1955 /* Calculate the MD4 hash (NT compatible) of the
1956 * password */
1957 E_md4hash(oldpswd, old_nt_hash);
1958 E_md4hash(newpswd, new_nt_hash);
1960 /* E_deshash returns false for 'long'
1961 passwords (> 14 DOS chars).
1963 Therefore, don't send a buffer
1964 encrypted with the truncated hash
1965 (it could allow an even easier
1966 attack on the password)
1968 Likewise, obey the admin's restriction
1971 if (lp_client_lanman_auth() &&
1972 E_deshash(newpswd, new_lm_hash) &&
1973 E_deshash(oldpswd, old_lm_hash)) {
1974 new_lm_pswd = data_blob(NULL, 516);
1975 old_lm_hash_enc = data_blob(NULL, 16);
1976 encode_pw_buffer(new_lm_pswd.data, newpswd,
1977 STR_UNICODE);
1979 SamOEMhash(new_lm_pswd.data, old_nt_hash, 516);
1980 E_old_pw_hash(new_nt_hash, old_lm_hash,
1981 old_lm_hash_enc.data);
1982 } else {
1983 new_lm_pswd.data = NULL;
1984 new_lm_pswd.length = 0;
1985 old_lm_hash_enc.data = NULL;
1986 old_lm_hash_enc.length = 0;
1989 encode_pw_buffer(new_nt_pswd.data, newpswd,
1990 STR_UNICODE);
1992 SamOEMhash(new_nt_pswd.data, old_nt_hash, 516);
1993 E_old_pw_hash(new_nt_hash, old_nt_hash,
1994 old_nt_hash_enc.data);
1997 if (!full_username && !username) {
1998 x_fprintf(x_stdout, "Error: No username supplied!\n");
1999 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2000 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2001 x_fprintf(x_stdout, "Error: No NT or LM password "
2002 "blobs supplied!\n");
2003 } else {
2004 char *error_string = NULL;
2006 if (full_username && !username) {
2007 fstring fstr_user;
2008 fstring fstr_domain;
2010 if (!parse_ntlm_auth_domain_user(full_username,
2011 fstr_user,
2012 fstr_domain)) {
2013 /* username might be 'tainted', don't
2014 * print into our new-line
2015 * deleimianted stream */
2016 x_fprintf(x_stdout, "Error: Could not "
2017 "parse into domain and "
2018 "username\n");
2019 SAFE_FREE(username);
2020 username = smb_xstrdup(full_username);
2021 } else {
2022 SAFE_FREE(username);
2023 SAFE_FREE(domain);
2024 username = smb_xstrdup(fstr_user);
2025 domain = smb_xstrdup(fstr_domain);
2030 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2031 username, domain,
2032 new_nt_pswd,
2033 old_nt_hash_enc,
2034 new_lm_pswd,
2035 old_lm_hash_enc,
2036 &error_string))) {
2037 x_fprintf(x_stdout, "Password-Change: No\n");
2038 x_fprintf(x_stdout, "Password-Change-Error: "
2039 "%s\n.\n", error_string);
2040 } else {
2041 x_fprintf(x_stdout, "Password-Change: Yes\n");
2044 SAFE_FREE(error_string);
2046 /* clear out the state */
2047 new_nt_pswd = data_blob_null;
2048 old_nt_hash_enc = data_blob_null;
2049 new_lm_pswd = data_blob_null;
2050 old_nt_hash_enc = data_blob_null;
2051 SAFE_FREE(full_username);
2052 SAFE_FREE(username);
2053 SAFE_FREE(domain);
2054 SAFE_FREE(newpswd);
2055 SAFE_FREE(oldpswd);
2056 x_fprintf(x_stdout, ".\n");
2058 return;
2061 request = buf;
2063 /* Indicates a base64 encoded structure */
2064 parameter = strstr_m(request, ":: ");
2065 if (!parameter) {
2066 parameter = strstr_m(request, ": ");
2068 if (!parameter) {
2069 DEBUG(0, ("Parameter not found!\n"));
2070 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2071 return;
2074 parameter[0] ='\0';
2075 parameter++;
2076 parameter[0] ='\0';
2077 parameter++;
2078 } else {
2079 parameter[0] ='\0';
2080 parameter++;
2081 parameter[0] ='\0';
2082 parameter++;
2083 parameter[0] ='\0';
2084 parameter++;
2086 base64_decode_inplace(parameter);
2089 if (strequal(request, "new-nt-password-blob")) {
2090 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2091 if (new_nt_pswd.length != 516) {
2092 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2093 "(got %d bytes, expected 516)\n.\n",
2094 parameter,
2095 (int)new_nt_pswd.length);
2096 new_nt_pswd = data_blob_null;
2098 } else if (strequal(request, "old-nt-hash-blob")) {
2099 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2100 if (old_nt_hash_enc.length != 16) {
2101 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2102 "(got %d bytes, expected 16)\n.\n",
2103 parameter,
2104 (int)old_nt_hash_enc.length);
2105 old_nt_hash_enc = data_blob_null;
2107 } else if (strequal(request, "new-lm-password-blob")) {
2108 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2109 if (new_lm_pswd.length != 516) {
2110 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2111 "(got %d bytes, expected 516)\n.\n",
2112 parameter,
2113 (int)new_lm_pswd.length);
2114 new_lm_pswd = data_blob_null;
2117 else if (strequal(request, "old-lm-hash-blob")) {
2118 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2119 if (old_lm_hash_enc.length != 16)
2121 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2122 "(got %d bytes, expected 16)\n.\n",
2123 parameter,
2124 (int)old_lm_hash_enc.length);
2125 old_lm_hash_enc = data_blob_null;
2127 } else if (strequal(request, "nt-domain")) {
2128 domain = smb_xstrdup(parameter);
2129 } else if(strequal(request, "username")) {
2130 username = smb_xstrdup(parameter);
2131 } else if(strequal(request, "full-username")) {
2132 username = smb_xstrdup(parameter);
2133 } else if(strequal(request, "new-password")) {
2134 newpswd = smb_xstrdup(parameter);
2135 } else if (strequal(request, "old-password")) {
2136 oldpswd = smb_xstrdup(parameter);
2137 } else {
2138 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2142 static void manage_squid_request(struct ntlm_auth_state *state,
2143 stdio_helper_function fn)
2145 char *buf;
2146 char tmp[INITIAL_BUFFER_SIZE+1];
2147 int length, buf_size = 0;
2148 char *c;
2150 buf = talloc_strdup(state->mem_ctx, "");
2151 if (!buf) {
2152 DEBUG(0, ("Failed to allocate input buffer.\n"));
2153 x_fprintf(x_stderr, "ERR\n");
2154 exit(1);
2157 do {
2159 /* this is not a typo - x_fgets doesn't work too well under
2160 * squid */
2161 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2162 if (ferror(stdin)) {
2163 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2164 "(%s)\n", ferror(stdin),
2165 strerror(ferror(stdin))));
2167 exit(1);
2169 exit(0);
2172 buf = talloc_strdup_append_buffer(buf, tmp);
2173 buf_size += INITIAL_BUFFER_SIZE;
2175 if (buf_size > MAX_BUFFER_SIZE) {
2176 DEBUG(2, ("Oversized message\n"));
2177 x_fprintf(x_stderr, "ERR\n");
2178 talloc_free(buf);
2179 return;
2182 c = strchr(buf, '\n');
2183 } while (c == NULL);
2185 *c = '\0';
2186 length = c-buf;
2188 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2190 if (buf[0] == '\0') {
2191 DEBUG(2, ("Invalid Request\n"));
2192 x_fprintf(x_stderr, "ERR\n");
2193 talloc_free(buf);
2194 return;
2197 fn(state, buf, length);
2198 talloc_free(buf);
2202 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2203 TALLOC_CTX *mem_ctx;
2204 struct ntlm_auth_state *state;
2206 /* initialize FDescs */
2207 x_setbuf(x_stdout, NULL);
2208 x_setbuf(x_stderr, NULL);
2210 mem_ctx = talloc_init("ntlm_auth");
2211 if (!mem_ctx) {
2212 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2213 x_fprintf(x_stderr, "ERR\n");
2214 exit(1);
2217 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2218 if (!state) {
2219 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2220 x_fprintf(x_stderr, "ERR\n");
2221 exit(1);
2224 state->mem_ctx = mem_ctx;
2225 state->helper_mode = stdio_mode;
2227 while(1) {
2228 manage_squid_request(state, fn);
2233 /* Authenticate a user with a challenge/response */
2235 static bool check_auth_crap(void)
2237 NTSTATUS nt_status;
2238 uint32 flags = 0;
2239 char lm_key[8];
2240 char user_session_key[16];
2241 char *hex_lm_key;
2242 char *hex_user_session_key;
2243 char *error_string;
2244 static uint8 zeros[16];
2246 x_setbuf(x_stdout, NULL);
2248 if (request_lm_key)
2249 flags |= WBFLAG_PAM_LMKEY;
2251 if (request_user_session_key)
2252 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2254 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2256 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2257 opt_workstation,
2258 &opt_challenge,
2259 &opt_lm_response,
2260 &opt_nt_response,
2261 flags,
2262 (unsigned char *)lm_key,
2263 (unsigned char *)user_session_key,
2264 &error_string, NULL);
2266 if (!NT_STATUS_IS_OK(nt_status)) {
2267 x_fprintf(x_stdout, "%s (0x%x)\n",
2268 error_string,
2269 NT_STATUS_V(nt_status));
2270 SAFE_FREE(error_string);
2271 return False;
2274 if (request_lm_key
2275 && (memcmp(zeros, lm_key,
2276 sizeof(lm_key)) != 0)) {
2277 hex_lm_key = hex_encode(NULL, (const unsigned char *)lm_key,
2278 sizeof(lm_key));
2279 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2280 TALLOC_FREE(hex_lm_key);
2282 if (request_user_session_key
2283 && (memcmp(zeros, user_session_key,
2284 sizeof(user_session_key)) != 0)) {
2285 hex_user_session_key = hex_encode(NULL, (const unsigned char *)user_session_key,
2286 sizeof(user_session_key));
2287 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2288 TALLOC_FREE(hex_user_session_key);
2291 return True;
2294 /* Main program */
2296 enum {
2297 OPT_USERNAME = 1000,
2298 OPT_DOMAIN,
2299 OPT_WORKSTATION,
2300 OPT_CHALLENGE,
2301 OPT_RESPONSE,
2302 OPT_LM,
2303 OPT_NT,
2304 OPT_PASSWORD,
2305 OPT_LM_KEY,
2306 OPT_USER_SESSION_KEY,
2307 OPT_DIAGNOSTICS,
2308 OPT_REQUIRE_MEMBERSHIP,
2309 OPT_USE_CACHED_CREDS
2312 int main(int argc, const char **argv)
2314 TALLOC_CTX *frame = talloc_stackframe();
2315 int opt;
2316 static const char *helper_protocol;
2317 static int diagnostics;
2319 static const char *hex_challenge;
2320 static const char *hex_lm_response;
2321 static const char *hex_nt_response;
2323 poptContext pc;
2325 /* NOTE: DO NOT change this interface without considering the implications!
2326 This is an external interface, which other programs will use to interact
2327 with this helper.
2330 /* We do not use single-letter command abbreviations, because they harm future
2331 interface stability. */
2333 struct poptOption long_options[] = {
2334 POPT_AUTOHELP
2335 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2336 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2337 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2338 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2339 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2340 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2341 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2342 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2343 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2344 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2345 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2346 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2347 { "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" },
2348 POPT_COMMON_CONFIGFILE
2349 POPT_COMMON_VERSION
2350 POPT_TABLEEND
2353 /* Samba client initialisation */
2354 load_case_tables();
2356 dbf = x_stderr;
2358 /* Parse options */
2360 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2362 /* Parse command line options */
2364 if (argc == 1) {
2365 poptPrintHelp(pc, stderr, 0);
2366 return 1;
2369 while((opt = poptGetNextOpt(pc)) != -1) {
2370 /* Get generic config options like --configfile */
2373 poptFreeContext(pc);
2375 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2376 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2377 get_dyn_CONFIGFILE(), strerror(errno));
2378 exit(1);
2381 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2382 POPT_CONTEXT_KEEP_FIRST);
2384 while((opt = poptGetNextOpt(pc)) != -1) {
2385 switch (opt) {
2386 case OPT_CHALLENGE:
2387 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2388 if (opt_challenge.length != 8) {
2389 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2390 hex_challenge,
2391 (int)opt_challenge.length);
2392 exit(1);
2394 break;
2395 case OPT_LM:
2396 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2397 if (opt_lm_response.length != 24) {
2398 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2399 hex_lm_response,
2400 (int)opt_lm_response.length);
2401 exit(1);
2403 break;
2405 case OPT_NT:
2406 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2407 if (opt_nt_response.length < 24) {
2408 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2409 hex_nt_response,
2410 (int)opt_nt_response.length);
2411 exit(1);
2413 break;
2415 case OPT_REQUIRE_MEMBERSHIP:
2416 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2417 require_membership_of_sid = require_membership_of;
2419 break;
2423 if (opt_username) {
2424 char *domain = SMB_STRDUP(opt_username);
2425 char *p = strchr_m(domain, *lp_winbind_separator());
2426 if (p) {
2427 opt_username = p+1;
2428 *p = '\0';
2429 if (opt_domain && !strequal(opt_domain, domain)) {
2430 x_fprintf(x_stderr, "Domain specified in username (%s) "
2431 "doesn't match specified domain (%s)!\n\n",
2432 domain, opt_domain);
2433 poptPrintHelp(pc, stderr, 0);
2434 exit(1);
2436 opt_domain = domain;
2437 } else {
2438 SAFE_FREE(domain);
2442 /* Note: if opt_domain is "" then send no domain */
2443 if (opt_domain == NULL) {
2444 opt_domain = get_winbind_domain();
2447 if (opt_workstation == NULL) {
2448 opt_workstation = "";
2451 if (helper_protocol) {
2452 int i;
2453 for (i=0; i<NUM_HELPER_MODES; i++) {
2454 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2455 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2456 exit(0);
2459 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2461 for (i=0; i<NUM_HELPER_MODES; i++) {
2462 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2465 exit(1);
2468 if (!opt_username || !*opt_username) {
2469 x_fprintf(x_stderr, "username must be specified!\n\n");
2470 poptPrintHelp(pc, stderr, 0);
2471 exit(1);
2474 if (opt_challenge.length) {
2475 if (!check_auth_crap()) {
2476 exit(1);
2478 exit(0);
2481 if (!opt_password) {
2482 opt_password = getpass("password: ");
2485 if (diagnostics) {
2486 if (!diagnose_ntlm_auth()) {
2487 return 1;
2489 } else {
2490 fstring user;
2492 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2493 if (!check_plaintext_auth(user, opt_password, True)) {
2494 return 1;
2498 /* Exit code */
2500 poptFreeContext(pc);
2501 TALLOC_FREE(frame);
2502 return 0;