s3-netapi: allow to use default krb5 credential cache for libnetapi users.
[Samba.git] / source4 / utils / ntlm_auth.c
blob34f79715ff225be10c30ba2469f4fcc3f57c92e6
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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "lib/cmdline/popt_common.h"
27 #include <ldb.h>
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/auth.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
32 #include "auth/auth_sam.h"
33 #include "libcli/auth/libcli_auth.h"
34 #include "libcli/security/security.h"
35 #include "lib/events/events.h"
36 #include "lib/messaging/messaging.h"
37 #include "lib/messaging/irpc.h"
38 #include "auth/ntlmssp/ntlmssp.h"
39 #include "param/param.h"
41 #define INITIAL_BUFFER_SIZE 300
42 #define MAX_BUFFER_SIZE 63000
44 enum stdio_helper_mode {
45 SQUID_2_4_BASIC,
46 SQUID_2_5_BASIC,
47 SQUID_2_5_NTLMSSP,
48 NTLMSSP_CLIENT_1,
49 GSS_SPNEGO_CLIENT,
50 GSS_SPNEGO_SERVER,
51 NTLM_SERVER_1,
52 NUM_HELPER_MODES
55 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
56 #define NTLM_AUTH_FLAG_LMKEY 0x0008
59 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
60 struct loadparm_context *lp_ctx,
61 char *buf, int length, void **private1,
62 unsigned int mux_id, void **private2);
64 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
65 struct loadparm_context *lp_ctx,
66 char *buf, int length, void **private1,
67 unsigned int mux_id, void **private2);
69 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
70 struct loadparm_context *lp_ctx,
71 char *buf, int length, void **private1,
72 unsigned int mux_id, void **private2);
74 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
75 struct loadparm_context *lp_ctx,
76 char *buf, int length, void **private1,
77 unsigned int mux_id, void **private2);
79 static void manage_squid_request(struct loadparm_context *lp_ctx,
80 enum stdio_helper_mode helper_mode,
81 stdio_helper_function fn, void **private2);
83 static const struct {
84 enum stdio_helper_mode mode;
85 const char *name;
86 stdio_helper_function fn;
87 } stdio_helper_protocols[] = {
88 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
89 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
90 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
91 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
92 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
93 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
94 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
95 { NUM_HELPER_MODES, NULL, NULL}
98 extern int winbindd_fd;
100 static const char *opt_username;
101 static const char *opt_domain;
102 static const char *opt_workstation;
103 static const char *opt_password;
104 static int opt_multiplex;
105 static int use_cached_creds;
108 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
110 static void mux_printf(unsigned int mux_id, const char *format, ...)
112 va_list ap;
114 if (opt_multiplex) {
115 x_fprintf(x_stdout, "%d ", mux_id);
118 va_start(ap, format);
119 x_vfprintf(x_stdout, format, ap);
120 va_end(ap);
125 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
126 form DOMAIN/user into a domain and a user */
128 static bool parse_ntlm_auth_domain_user(const char *domuser, char **domain,
129 char **user, char winbind_separator)
132 char *p = strchr(domuser, winbind_separator);
134 if (!p) {
135 return false;
138 *user = smb_xstrdup(p+1);
139 *domain = smb_xstrdup(domuser);
140 (*domain)[PTR_DIFF(p, domuser)] = 0;
142 return true;
146 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
148 static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
150 DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
151 ret.length = ldb_base64_decode((char *)ret.data);
152 return ret;
156 * Encode a base64 string into a talloc()ed string caller to free.
158 static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
160 return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
164 * Decode a base64 string in-place - wrapper for the above
166 static void base64_decode_inplace(char *s)
168 ldb_base64_decode(s);
173 /* Authenticate a user with a plaintext password */
175 static bool check_plaintext_auth(const char *user, const char *pass,
176 bool stdout_diagnostics)
178 return (strcmp(pass, opt_password) == 0);
181 /* authenticate a user with an encrypted username/password */
183 static NTSTATUS local_pw_check_specified(struct loadparm_context *lp_ctx,
184 const char *username,
185 const char *domain,
186 const char *workstation,
187 const DATA_BLOB *challenge,
188 const DATA_BLOB *lm_response,
189 const DATA_BLOB *nt_response,
190 uint32_t flags,
191 DATA_BLOB *lm_session_key,
192 DATA_BLOB *user_session_key,
193 char **error_string,
194 char **unix_name)
196 NTSTATUS nt_status;
197 struct samr_Password lm_pw, nt_pw;
198 struct samr_Password *lm_pwd, *nt_pwd;
199 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
200 if (!mem_ctx) {
201 nt_status = NT_STATUS_NO_MEMORY;
202 } else {
204 E_md4hash(opt_password, nt_pw.hash);
205 if (E_deshash(opt_password, lm_pw.hash)) {
206 lm_pwd = &lm_pw;
207 } else {
208 lm_pwd = NULL;
210 nt_pwd = &nt_pw;
213 nt_status = ntlm_password_check(mem_ctx,
214 lpcfg_lanman_auth(lp_ctx),
215 lpcfg_ntlm_auth(lp_ctx),
216 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
217 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
218 challenge,
219 lm_response,
220 nt_response,
221 username,
222 username,
223 domain,
224 lm_pwd, nt_pwd, user_session_key, lm_session_key);
226 if (NT_STATUS_IS_OK(nt_status)) {
227 if (unix_name) {
228 if (asprintf(unix_name, "%s%c%s", domain,
229 *lpcfg_winbind_separator(lp_ctx),
230 username) < 0) {
231 nt_status = NT_STATUS_NO_MEMORY;
234 } else {
235 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
236 domain, username, workstation,
237 nt_errstr(nt_status)));
239 talloc_free(mem_ctx);
241 if (error_string) {
242 *error_string = strdup(nt_errstr(nt_status));
244 return nt_status;
249 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
250 struct loadparm_context *lp_ctx,
251 char *buf, int length, void **private1,
252 unsigned int mux_id, void **private2)
254 char *user, *pass;
255 user=buf;
257 pass = memchr(buf, ' ', length);
258 if (!pass) {
259 DEBUG(2, ("Password not found. Denying access\n"));
260 mux_printf(mux_id, "ERR\n");
261 return;
263 *pass='\0';
264 pass++;
266 if (stdio_helper_mode == SQUID_2_5_BASIC) {
267 rfc1738_unescape(user);
268 rfc1738_unescape(pass);
271 if (check_plaintext_auth(user, pass, false)) {
272 mux_printf(mux_id, "OK\n");
273 } else {
274 mux_printf(mux_id, "ERR\n");
278 /* This is a bit hairy, but the basic idea is to do a password callback
279 to the calling application. The callback comes from within gensec */
281 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
282 struct loadparm_context *lp_ctx,
283 char *buf, int length, void **private1,
284 unsigned int mux_id, void **password)
286 DATA_BLOB in;
287 if (strlen(buf) < 2) {
288 DEBUG(1, ("query [%s] invalid", buf));
289 mux_printf(mux_id, "BH Query invalid\n");
290 return;
293 if (strlen(buf) > 3) {
294 in = base64_decode_data_blob(NULL, buf + 3);
295 } else {
296 in = data_blob(NULL, 0);
299 if (strncmp(buf, "PW ", 3) == 0) {
301 *password = talloc_strndup(*private1 /* hopefully the right gensec context, useful to use for talloc */,
302 (const char *)in.data, in.length);
304 if (*password == NULL) {
305 DEBUG(1, ("Out of memory\n"));
306 mux_printf(mux_id, "BH Out of memory\n");
307 data_blob_free(&in);
308 return;
311 mux_printf(mux_id, "OK\n");
312 data_blob_free(&in);
313 return;
315 DEBUG(1, ("Asked for (and expected) a password\n"));
316 mux_printf(mux_id, "BH Expected a password\n");
317 data_blob_free(&in);
320 /**
321 * Callback for password credentials. This is not async, and when
322 * GENSEC and the credentials code is made async, it will look rather
323 * different.
326 static const char *get_password(struct cli_credentials *credentials)
328 char *password = NULL;
330 /* Ask for a password */
331 mux_printf((unsigned int)(uintptr_t)credentials->priv_data, "PW\n");
332 credentials->priv_data = NULL;
334 manage_squid_request(cmdline_lp_ctx, NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
335 return password;
339 Check if a string is part of a list.
341 static bool in_list(const char *s, const char *list, bool casesensitive)
343 char *tok;
344 size_t tok_len = 1024;
345 const char *p=list;
347 if (!list)
348 return false;
350 tok = (char *)malloc(tok_len);
351 if (!tok) {
352 return false;
355 while (next_token(&p, tok, LIST_SEP, tok_len)) {
356 if ((casesensitive?strcmp:strcasecmp_m)(tok,s) == 0) {
357 free(tok);
358 return true;
361 free(tok);
362 return false;
365 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
367 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, true)) {
368 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
369 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
371 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, true)) {
372 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
373 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
375 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, true)) {
376 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
377 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
381 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
382 struct loadparm_context *lp_ctx,
383 char *buf, int length, void **private1,
384 unsigned int mux_id, void **private2)
386 DATA_BLOB in;
387 DATA_BLOB out = data_blob(NULL, 0);
388 char *out_base64 = NULL;
389 const char *reply_arg = NULL;
390 struct gensec_ntlm_state {
391 struct gensec_security *gensec_state;
392 const char *set_password;
394 struct gensec_ntlm_state *state;
395 struct tevent_context *ev;
396 struct messaging_context *msg;
398 NTSTATUS nt_status;
399 bool first = false;
400 const char *reply_code;
401 struct cli_credentials *creds;
403 static char *want_feature_list = NULL;
404 static DATA_BLOB session_key;
406 TALLOC_CTX *mem_ctx;
408 if (*private1) {
409 state = (struct gensec_ntlm_state *)*private1;
410 } else {
411 state = talloc_zero(NULL, struct gensec_ntlm_state);
412 if (!state) {
413 mux_printf(mux_id, "BH No Memory\n");
414 exit(1);
416 *private1 = state;
417 if (opt_password) {
418 state->set_password = opt_password;
422 if (strlen(buf) < 2) {
423 DEBUG(1, ("query [%s] invalid", buf));
424 mux_printf(mux_id, "BH Query invalid\n");
425 return;
428 if (strlen(buf) > 3) {
429 if(strncmp(buf, "SF ", 3) == 0) {
430 DEBUG(10, ("Setting flags to negotiate\n"));
431 talloc_free(want_feature_list);
432 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
433 mux_printf(mux_id, "OK\n");
434 return;
436 in = base64_decode_data_blob(NULL, buf + 3);
437 } else {
438 in = data_blob(NULL, 0);
441 if (strncmp(buf, "YR", 2) == 0) {
442 if (state->gensec_state) {
443 talloc_free(state->gensec_state);
444 state->gensec_state = NULL;
446 } else if ( (strncmp(buf, "OK", 2) == 0)) {
447 /* Just return BH, like ntlm_auth from Samba 3 does. */
448 mux_printf(mux_id, "BH Command expected\n");
449 data_blob_free(&in);
450 return;
451 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
452 (strncmp(buf, "KK ", 3) != 0) &&
453 (strncmp(buf, "AF ", 3) != 0) &&
454 (strncmp(buf, "NA ", 3) != 0) &&
455 (strncmp(buf, "UG", 2) != 0) &&
456 (strncmp(buf, "PW ", 3) != 0) &&
457 (strncmp(buf, "GK", 2) != 0) &&
458 (strncmp(buf, "GF", 2) != 0)) {
459 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
460 mux_printf(mux_id, "BH SPNEGO request invalid\n");
461 data_blob_free(&in);
462 return;
465 ev = s4_event_context_init(state);
466 if (!ev) {
467 exit(1);
470 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
472 /* setup gensec */
473 if (!(state->gensec_state)) {
474 switch (stdio_helper_mode) {
475 case GSS_SPNEGO_CLIENT:
476 case NTLMSSP_CLIENT_1:
477 /* setup the client side */
479 nt_status = gensec_client_start(NULL, &state->gensec_state, ev,
480 lpcfg_gensec_settings(NULL, lp_ctx));
481 if (!NT_STATUS_IS_OK(nt_status)) {
482 talloc_free(mem_ctx);
483 exit(1);
486 break;
487 case GSS_SPNEGO_SERVER:
488 case SQUID_2_5_NTLMSSP:
490 const char *winbind_method[] = { "winbind", NULL };
491 struct auth_context *auth_context;
493 msg = messaging_client_init(state, lpcfg_messaging_path(state, lp_ctx), ev);
494 if (!msg) {
495 talloc_free(mem_ctx);
496 exit(1);
498 nt_status = auth_context_create_methods(mem_ctx,
499 winbind_method,
500 ev,
501 msg,
502 lp_ctx,
503 NULL,
504 &auth_context);
506 if (!NT_STATUS_IS_OK(nt_status)) {
507 talloc_free(mem_ctx);
508 exit(1);
511 if (!NT_STATUS_IS_OK(gensec_server_start(state, ev,
512 lpcfg_gensec_settings(state, lp_ctx),
513 auth_context, &state->gensec_state))) {
514 talloc_free(mem_ctx);
515 exit(1);
517 break;
519 default:
520 talloc_free(mem_ctx);
521 abort();
524 creds = cli_credentials_init(state->gensec_state);
525 cli_credentials_set_conf(creds, lp_ctx);
526 if (opt_username) {
527 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
529 if (opt_domain) {
530 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
532 if (state->set_password) {
533 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
534 } else {
535 cli_credentials_set_password_callback(creds, get_password);
536 creds->priv_data = (void*)(uintptr_t)mux_id;
538 if (opt_workstation) {
539 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
542 switch (stdio_helper_mode) {
543 case GSS_SPNEGO_SERVER:
544 case SQUID_2_5_NTLMSSP:
545 cli_credentials_set_machine_account(creds, lp_ctx);
546 break;
547 default:
548 break;
551 gensec_set_credentials(state->gensec_state, creds);
552 gensec_want_feature_list(state->gensec_state, want_feature_list);
554 switch (stdio_helper_mode) {
555 case GSS_SPNEGO_CLIENT:
556 case GSS_SPNEGO_SERVER:
557 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
558 if (!in.length) {
559 first = true;
561 break;
562 case NTLMSSP_CLIENT_1:
563 if (!in.length) {
564 first = true;
566 /* fall through */
567 case SQUID_2_5_NTLMSSP:
568 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
569 break;
570 default:
571 talloc_free(mem_ctx);
572 abort();
575 if (!NT_STATUS_IS_OK(nt_status)) {
576 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
577 mux_printf(mux_id, "BH GENSEC mech failed to start\n");
578 talloc_free(mem_ctx);
579 return;
584 /* update */
586 if (strncmp(buf, "PW ", 3) == 0) {
587 state->set_password = talloc_strndup(state,
588 (const char *)in.data,
589 in.length);
591 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
592 state->set_password,
593 CRED_SPECIFIED);
594 mux_printf(mux_id, "OK\n");
595 data_blob_free(&in);
596 talloc_free(mem_ctx);
597 return;
600 if (strncmp(buf, "UG", 2) == 0) {
601 int i;
602 char *grouplist = NULL;
603 struct auth_session_info *session_info;
605 nt_status = gensec_session_info(state->gensec_state, &session_info);
606 if (!NT_STATUS_IS_OK(nt_status)) {
607 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
608 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
609 data_blob_free(&in);
610 talloc_free(mem_ctx);
611 return;
614 /* get the string onto the context */
615 grouplist = talloc_strdup(mem_ctx, "");
617 for (i=0; i<session_info->security_token->num_sids; i++) {
618 struct security_token *token = session_info->security_token;
619 const char *sidstr = dom_sid_string(session_info,
620 &token->sids[i]);
621 grouplist = talloc_asprintf_append_buffer(grouplist, "%s,", sidstr);
624 mux_printf(mux_id, "GL %s\n", grouplist);
625 talloc_free(session_info);
626 data_blob_free(&in);
627 talloc_free(mem_ctx);
628 return;
631 if (strncmp(buf, "GK", 2) == 0) {
632 char *base64_key;
633 DEBUG(10, ("Requested session key\n"));
634 nt_status = gensec_session_key(state->gensec_state, &session_key);
635 if(!NT_STATUS_IS_OK(nt_status)) {
636 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
637 mux_printf(mux_id, "BH No session key\n");
638 talloc_free(mem_ctx);
639 return;
640 } else {
641 base64_key = base64_encode_data_blob(state, session_key);
642 mux_printf(mux_id, "GK %s\n", base64_key);
643 talloc_free(base64_key);
645 talloc_free(mem_ctx);
646 return;
649 if (strncmp(buf, "GF", 2) == 0) {
650 struct ntlmssp_state *ntlmssp_state;
651 uint32_t neg_flags;
653 ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
654 struct ntlmssp_state);
655 neg_flags = ntlmssp_state->neg_flags;
657 DEBUG(10, ("Requested negotiated feature flags\n"));
658 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
659 return;
662 nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
664 /* don't leak 'bad password'/'no such user' info to the network client */
665 nt_status = nt_status_squash(nt_status);
667 if (out.length) {
668 out_base64 = base64_encode_data_blob(mem_ctx, out);
669 } else {
670 out_base64 = NULL;
673 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
674 reply_arg = "*";
675 if (first) {
676 reply_code = "YR";
677 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
678 reply_code = "KK";
679 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
680 reply_code = "TT";
681 } else {
682 abort();
686 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
687 reply_code = "BH NT_STATUS_ACCESS_DENIED";
688 reply_arg = nt_errstr(nt_status);
689 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
690 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
691 reply_code = "BH NT_STATUS_UNSUCCESSFUL";
692 reply_arg = nt_errstr(nt_status);
693 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
694 } else if (!NT_STATUS_IS_OK(nt_status)) {
695 reply_code = "NA";
696 reply_arg = nt_errstr(nt_status);
697 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
698 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
699 struct auth_session_info *session_info;
701 nt_status = gensec_session_info(state->gensec_state, &session_info);
702 if (!NT_STATUS_IS_OK(nt_status)) {
703 reply_code = "BH Failed to retrive session info";
704 reply_arg = nt_errstr(nt_status);
705 DEBUG(1, ("GENSEC failed to retrieve the session info: %s\n", nt_errstr(nt_status)));
706 } else {
708 reply_code = "AF";
709 reply_arg = talloc_asprintf(state->gensec_state,
710 "%s%s%s", session_info->info->domain_name,
711 lpcfg_winbind_separator(lp_ctx), session_info->info->account_name);
712 talloc_free(session_info);
714 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
715 reply_code = "AF";
716 reply_arg = out_base64;
717 } else {
718 abort();
721 switch (stdio_helper_mode) {
722 case GSS_SPNEGO_SERVER:
723 mux_printf(mux_id, "%s %s %s\n", reply_code,
724 out_base64 ? out_base64 : "*",
725 reply_arg ? reply_arg : "*");
726 break;
727 default:
728 if (out_base64) {
729 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
730 } else if (reply_arg) {
731 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
732 } else {
733 mux_printf(mux_id, "%s\n", reply_code);
737 talloc_free(mem_ctx);
738 return;
741 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
742 struct loadparm_context *lp_ctx,
743 char *buf, int length, void **private1,
744 unsigned int mux_id, void **private2)
746 char *request, *parameter;
747 static DATA_BLOB challenge;
748 static DATA_BLOB lm_response;
749 static DATA_BLOB nt_response;
750 static char *full_username;
751 static char *username;
752 static char *domain;
753 static char *plaintext_password;
754 static bool ntlm_server_1_user_session_key;
755 static bool ntlm_server_1_lm_session_key;
757 if (strequal(buf, ".")) {
758 if (!full_username && !username) {
759 mux_printf(mux_id, "Error: No username supplied!\n");
760 } else if (plaintext_password) {
761 /* handle this request as plaintext */
762 if (!full_username) {
763 if (asprintf(&full_username, "%s%c%s", domain, *lpcfg_winbind_separator(lp_ctx), username) < 0) {
764 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
765 return;
768 if (check_plaintext_auth(full_username, plaintext_password, false)) {
769 mux_printf(mux_id, "Authenticated: Yes\n");
770 } else {
771 mux_printf(mux_id, "Authenticated: No\n");
773 } else if (!lm_response.data && !nt_response.data) {
774 mux_printf(mux_id, "Error: No password supplied!\n");
775 } else if (!challenge.data) {
776 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
777 } else {
778 char *error_string = NULL;
779 DATA_BLOB lm_key;
780 DATA_BLOB user_session_key;
781 uint32_t flags = 0;
783 if (full_username && !username) {
784 SAFE_FREE(username);
785 SAFE_FREE(domain);
786 if (!parse_ntlm_auth_domain_user(full_username, &username,
787 &domain,
788 *lpcfg_winbind_separator(lp_ctx))) {
789 /* username might be 'tainted', don't print into our new-line deleimianted stream */
790 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
794 if (!domain) {
795 domain = smb_xstrdup(lpcfg_workgroup(lp_ctx));
798 if (ntlm_server_1_lm_session_key)
799 flags |= NTLM_AUTH_FLAG_LMKEY;
801 if (ntlm_server_1_user_session_key)
802 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
804 if (!NT_STATUS_IS_OK(
805 local_pw_check_specified(lp_ctx,
806 username,
807 domain,
808 lpcfg_netbios_name(lp_ctx),
809 &challenge,
810 &lm_response,
811 &nt_response,
812 flags,
813 &lm_key,
814 &user_session_key,
815 &error_string,
816 NULL))) {
818 mux_printf(mux_id, "Authenticated: No\n");
819 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
820 SAFE_FREE(error_string);
821 } else {
822 static char zeros[16];
823 char *hex_lm_key;
824 char *hex_user_session_key;
826 mux_printf(mux_id, "Authenticated: Yes\n");
828 if (ntlm_server_1_lm_session_key
829 && lm_key.length
830 && (memcmp(zeros, lm_key.data,
831 lm_key.length) != 0)) {
832 hex_encode(lm_key.data,
833 lm_key.length,
834 &hex_lm_key);
835 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
836 SAFE_FREE(hex_lm_key);
839 if (ntlm_server_1_user_session_key
840 && user_session_key.length
841 && (memcmp(zeros, user_session_key.data,
842 user_session_key.length) != 0)) {
843 hex_encode(user_session_key.data,
844 user_session_key.length,
845 &hex_user_session_key);
846 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
847 SAFE_FREE(hex_user_session_key);
851 /* clear out the state */
852 challenge = data_blob(NULL, 0);
853 nt_response = data_blob(NULL, 0);
854 lm_response = data_blob(NULL, 0);
855 SAFE_FREE(full_username);
856 SAFE_FREE(username);
857 SAFE_FREE(domain);
858 SAFE_FREE(plaintext_password);
859 ntlm_server_1_user_session_key = false;
860 ntlm_server_1_lm_session_key = false;
861 mux_printf(mux_id, ".\n");
863 return;
866 request = buf;
868 /* Indicates a base64 encoded structure */
869 parameter = strstr(request, ":: ");
870 if (!parameter) {
871 parameter = strstr(request, ": ");
873 if (!parameter) {
874 DEBUG(0, ("Parameter not found!\n"));
875 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
876 return;
879 parameter[0] ='\0';
880 parameter++;
881 parameter[0] ='\0';
882 parameter++;
884 } else {
885 parameter[0] ='\0';
886 parameter++;
887 parameter[0] ='\0';
888 parameter++;
889 parameter[0] ='\0';
890 parameter++;
892 base64_decode_inplace(parameter);
895 if (strequal(request, "LANMAN-Challenge")) {
896 challenge = strhex_to_data_blob(NULL, parameter);
897 if (challenge.length != 8) {
898 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
899 parameter,
900 (int)challenge.length);
901 challenge = data_blob(NULL, 0);
903 } else if (strequal(request, "NT-Response")) {
904 nt_response = strhex_to_data_blob(NULL, parameter);
905 if (nt_response.length < 24) {
906 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
907 parameter,
908 (int)nt_response.length);
909 nt_response = data_blob(NULL, 0);
911 } else if (strequal(request, "LANMAN-Response")) {
912 lm_response = strhex_to_data_blob(NULL, parameter);
913 if (lm_response.length != 24) {
914 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
915 parameter,
916 (int)lm_response.length);
917 lm_response = data_blob(NULL, 0);
919 } else if (strequal(request, "Password")) {
920 plaintext_password = smb_xstrdup(parameter);
921 } else if (strequal(request, "NT-Domain")) {
922 domain = smb_xstrdup(parameter);
923 } else if (strequal(request, "Username")) {
924 username = smb_xstrdup(parameter);
925 } else if (strequal(request, "Full-Username")) {
926 full_username = smb_xstrdup(parameter);
927 } else if (strequal(request, "Request-User-Session-Key")) {
928 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
929 } else if (strequal(request, "Request-LanMan-Session-Key")) {
930 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
931 } else {
932 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
936 static void manage_squid_request(struct loadparm_context *lp_ctx, enum stdio_helper_mode helper_mode,
937 stdio_helper_function fn, void **private2)
939 char *buf;
940 char tmp[INITIAL_BUFFER_SIZE+1];
941 unsigned int mux_id = 0;
942 int length, buf_size = 0;
943 char *c;
944 struct mux_private {
945 unsigned int max_mux;
946 void **private_pointers;
949 static struct mux_private *mux_private;
950 static void *normal_private;
951 void **private1;
953 buf = talloc_strdup(NULL, "");
955 if (buf == NULL) {
956 DEBUG(0, ("Failed to allocate memory for reading the input "
957 "buffer.\n"));
958 x_fprintf(x_stdout, "ERR\n");
959 return;
962 do {
963 /* this is not a typo - x_fgets doesn't work too well under
964 * squid */
965 if (fgets(tmp, INITIAL_BUFFER_SIZE, stdin) == NULL) {
966 if (ferror(stdin)) {
967 DEBUG(1, ("fgets() failed! dying..... errno=%d "
968 "(%s)\n", ferror(stdin),
969 strerror(ferror(stdin))));
971 exit(1); /* BIIG buffer */
973 exit(0);
976 buf = talloc_strdup_append_buffer(buf, tmp);
977 buf_size += INITIAL_BUFFER_SIZE;
979 if (buf_size > MAX_BUFFER_SIZE) {
980 DEBUG(0, ("Invalid Request (too large)\n"));
981 x_fprintf(x_stdout, "ERR\n");
982 talloc_free(buf);
983 return;
986 c = strchr(buf, '\n');
987 } while (c == NULL);
989 *c = '\0';
990 length = c-buf;
992 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
994 if (buf[0] == '\0') {
995 DEBUG(0, ("Invalid Request (empty)\n"));
996 x_fprintf(x_stdout, "ERR\n");
997 talloc_free(buf);
998 return;
1001 if (opt_multiplex) {
1002 if (sscanf(buf, "%u ", &mux_id) != 1) {
1003 DEBUG(0, ("Invalid Request - no multiplex id\n"));
1004 x_fprintf(x_stdout, "ERR\n");
1005 talloc_free(buf);
1006 return;
1008 if (!mux_private) {
1009 mux_private = talloc(NULL, struct mux_private);
1010 mux_private->max_mux = 0;
1011 mux_private->private_pointers = NULL;
1014 c=strchr(buf,' ');
1015 if (!c) {
1016 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
1017 x_fprintf(x_stdout, "ERR\n");
1018 talloc_free(buf);
1019 return;
1021 c++;
1022 if (mux_id >= mux_private->max_mux) {
1023 unsigned int prev_max = mux_private->max_mux;
1024 mux_private->max_mux = mux_id + 1;
1025 mux_private->private_pointers
1026 = talloc_realloc(mux_private,
1027 mux_private->private_pointers,
1028 void *, mux_private->max_mux);
1029 memset(&mux_private->private_pointers[prev_max], '\0',
1030 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
1033 private1 = &mux_private->private_pointers[mux_id];
1034 } else {
1035 c = buf;
1036 private1 = &normal_private;
1039 fn(helper_mode, lp_ctx, c, length, private1, mux_id, private2);
1040 talloc_free(buf);
1043 static void squid_stream(struct loadparm_context *lp_ctx,
1044 enum stdio_helper_mode stdio_mode,
1045 stdio_helper_function fn) {
1046 /* initialize FDescs */
1047 x_setbuf(x_stdout, NULL);
1048 x_setbuf(x_stderr, NULL);
1049 while(1) {
1050 manage_squid_request(lp_ctx, stdio_mode, fn, NULL);
1055 /* Main program */
1057 enum {
1058 OPT_USERNAME = 1000,
1059 OPT_DOMAIN,
1060 OPT_WORKSTATION,
1061 OPT_CHALLENGE,
1062 OPT_RESPONSE,
1063 OPT_LM,
1064 OPT_NT,
1065 OPT_PASSWORD,
1066 OPT_LM_KEY,
1067 OPT_USER_SESSION_KEY,
1068 OPT_DIAGNOSTICS,
1069 OPT_REQUIRE_MEMBERSHIP,
1070 OPT_MULTIPLEX,
1071 OPT_USE_CACHED_CREDS,
1074 int main(int argc, const char **argv)
1076 static const char *helper_protocol;
1077 int opt;
1079 poptContext pc;
1081 /* NOTE: DO NOT change this interface without considering the implications!
1082 This is an external interface, which other programs will use to interact
1083 with this helper.
1086 /* We do not use single-letter command abbreviations, because they harm future
1087 interface stability. */
1089 struct poptOption long_options[] = {
1090 POPT_AUTOHELP
1091 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1092 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1093 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1094 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1095 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1096 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1097 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1098 POPT_COMMON_SAMBA
1099 POPT_COMMON_VERSION
1100 { NULL }
1103 /* Samba client initialisation */
1105 setup_logging(NULL, DEBUG_STDERR);
1107 /* Parse options */
1109 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1111 /* Parse command line options */
1113 if (argc == 1) {
1114 poptPrintHelp(pc, stderr, 0);
1115 return 1;
1118 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1119 POPT_CONTEXT_KEEP_FIRST);
1121 while((opt = poptGetNextOpt(pc)) != -1) {
1122 if (opt < -1) {
1123 break;
1126 if (opt < -1) {
1127 fprintf(stderr, "%s: %s\n",
1128 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1129 poptStrerror(opt));
1130 return 1;
1133 gensec_init(cmdline_lp_ctx);
1135 if (opt_domain == NULL) {
1136 opt_domain = lpcfg_workgroup(cmdline_lp_ctx);
1139 if (helper_protocol) {
1140 int i;
1141 for (i=0; i<NUM_HELPER_MODES; i++) {
1142 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1143 squid_stream(cmdline_lp_ctx, stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1144 exit(0);
1147 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1149 for (i=0; i<NUM_HELPER_MODES; i++) {
1150 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1153 exit(1);
1156 if (!opt_username) {
1157 x_fprintf(x_stderr, "username must be specified!\n\n");
1158 poptPrintHelp(pc, stderr, 0);
1159 exit(1);
1162 if (opt_workstation == NULL) {
1163 opt_workstation = lpcfg_netbios_name(cmdline_lp_ctx);
1166 if (!opt_password) {
1167 opt_password = getpass("password: ");
1171 char *user;
1173 if (asprintf(&user, "%s%c%s", opt_domain,
1174 *lpcfg_winbind_separator(cmdline_lp_ctx),
1175 opt_username) < 0) {
1176 return 1;
1178 if (!check_plaintext_auth(user, opt_password, true)) {
1179 return 1;
1183 /* Exit code */
1185 poptFreeContext(pc);
1186 return 0;