smbd: brl_lock does not need "blr" anymore
[Samba.git] / source4 / utils / ntlm_auth.c
blobf44e782f66473dc9166b3e2989686a61bebee2f5
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/gensec/gensec_internal.h" /* TODO: remove this */
31 #include "auth/auth.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #include "auth/auth_sam.h"
34 #include "libcli/auth/libcli_auth.h"
35 #include "libcli/security/security.h"
36 #include "lib/events/events.h"
37 #include "lib/messaging/messaging.h"
38 #include "lib/messaging/irpc.h"
39 #include "auth/ntlmssp/ntlmssp.h"
40 #include "param/param.h"
42 #define INITIAL_BUFFER_SIZE 300
43 #define MAX_BUFFER_SIZE 63000
45 enum stdio_helper_mode {
46 SQUID_2_4_BASIC,
47 SQUID_2_5_BASIC,
48 SQUID_2_5_NTLMSSP,
49 NTLMSSP_CLIENT_1,
50 GSS_SPNEGO_CLIENT,
51 GSS_SPNEGO_SERVER,
52 NTLM_SERVER_1,
53 NUM_HELPER_MODES
56 #define NTLM_AUTH_FLAG_USER_SESSION_KEY 0x0004
57 #define NTLM_AUTH_FLAG_LMKEY 0x0008
60 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode,
61 struct loadparm_context *lp_ctx,
62 char *buf, int length, void **private1,
63 unsigned int mux_id, void **private2);
65 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode,
66 struct loadparm_context *lp_ctx,
67 char *buf, int length, void **private1,
68 unsigned int mux_id, void **private2);
70 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode,
71 struct loadparm_context *lp_ctx,
72 char *buf, int length, void **private1,
73 unsigned int mux_id, void **private2);
75 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode,
76 struct loadparm_context *lp_ctx,
77 char *buf, int length, void **private1,
78 unsigned int mux_id, void **private2);
80 static void manage_squid_request(struct loadparm_context *lp_ctx,
81 enum stdio_helper_mode helper_mode,
82 stdio_helper_function fn, void **private2);
84 static const struct {
85 enum stdio_helper_mode mode;
86 const char *name;
87 stdio_helper_function fn;
88 } stdio_helper_protocols[] = {
89 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
90 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
91 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
92 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
93 { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
94 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
95 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
96 { NUM_HELPER_MODES, NULL, NULL}
99 extern int winbindd_fd;
101 static const char *opt_username;
102 static const char *opt_domain;
103 static const char *opt_workstation;
104 static const char *opt_password;
105 static int opt_multiplex;
106 static int use_cached_creds;
109 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
111 static void mux_printf(unsigned int mux_id, const char *format, ...)
113 va_list ap;
115 if (opt_multiplex) {
116 x_fprintf(x_stdout, "%d ", mux_id);
119 va_start(ap, format);
120 x_vfprintf(x_stdout, format, ap);
121 va_end(ap);
126 /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
127 form DOMAIN/user into a domain and a user */
129 static bool parse_ntlm_auth_domain_user(const char *domuser, char **domain,
130 char **user, char winbind_separator)
133 char *p = strchr(domuser, winbind_separator);
135 if (!p) {
136 return false;
139 *user = smb_xstrdup(p+1);
140 *domain = smb_xstrdup(domuser);
141 (*domain)[PTR_DIFF(p, domuser)] = 0;
143 return true;
147 /* Authenticate a user with a plaintext password */
149 static bool check_plaintext_auth(const char *user, const char *pass,
150 bool stdout_diagnostics)
152 return (strcmp(pass, opt_password) == 0);
155 /* authenticate a user with an encrypted username/password */
157 static NTSTATUS local_pw_check_specified(struct loadparm_context *lp_ctx,
158 const char *username,
159 const char *domain,
160 const char *workstation,
161 const DATA_BLOB *challenge,
162 const DATA_BLOB *lm_response,
163 const DATA_BLOB *nt_response,
164 uint32_t flags,
165 DATA_BLOB *lm_session_key,
166 DATA_BLOB *user_session_key,
167 char **error_string,
168 char **unix_name)
170 NTSTATUS nt_status;
171 struct samr_Password lm_pw, nt_pw;
172 struct samr_Password *lm_pwd, *nt_pwd;
173 TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
174 if (!mem_ctx) {
175 nt_status = NT_STATUS_NO_MEMORY;
176 } else {
178 E_md4hash(opt_password, nt_pw.hash);
179 if (E_deshash(opt_password, lm_pw.hash)) {
180 lm_pwd = &lm_pw;
181 } else {
182 lm_pwd = NULL;
184 nt_pwd = &nt_pw;
187 nt_status = ntlm_password_check(mem_ctx,
188 lpcfg_lanman_auth(lp_ctx),
189 lpcfg_ntlm_auth(lp_ctx),
190 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
191 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
192 challenge,
193 lm_response,
194 nt_response,
195 username,
196 username,
197 domain,
198 lm_pwd, nt_pwd, user_session_key, lm_session_key);
200 if (NT_STATUS_IS_OK(nt_status)) {
201 if (unix_name) {
202 if (asprintf(unix_name, "%s%c%s", domain,
203 *lpcfg_winbind_separator(lp_ctx),
204 username) < 0) {
205 nt_status = NT_STATUS_NO_MEMORY;
208 } else {
209 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
210 domain, username, workstation,
211 nt_errstr(nt_status)));
213 talloc_free(mem_ctx);
215 if (error_string) {
216 *error_string = strdup(nt_errstr(nt_status));
218 return nt_status;
223 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode,
224 struct loadparm_context *lp_ctx,
225 char *buf, int length, void **private1,
226 unsigned int mux_id, void **private2)
228 char *user, *pass;
229 user=buf;
231 pass = memchr(buf, ' ', length);
232 if (!pass) {
233 DEBUG(2, ("Password not found. Denying access\n"));
234 mux_printf(mux_id, "ERR\n");
235 return;
237 *pass='\0';
238 pass++;
240 if (stdio_helper_mode == SQUID_2_5_BASIC) {
241 rfc1738_unescape(user);
242 rfc1738_unescape(pass);
245 if (check_plaintext_auth(user, pass, false)) {
246 mux_printf(mux_id, "OK\n");
247 } else {
248 mux_printf(mux_id, "ERR\n");
252 /* This is a bit hairy, but the basic idea is to do a password callback
253 to the calling application. The callback comes from within gensec */
255 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode,
256 struct loadparm_context *lp_ctx,
257 char *buf, int length, void **private1,
258 unsigned int mux_id, void **password)
260 DATA_BLOB in;
261 if (strlen(buf) < 2) {
262 DEBUG(1, ("query [%s] invalid", buf));
263 mux_printf(mux_id, "BH Query invalid\n");
264 return;
267 if (strlen(buf) > 3) {
268 in = base64_decode_data_blob(buf + 3);
269 } else {
270 in = data_blob(NULL, 0);
273 if (strncmp(buf, "PW ", 3) == 0) {
275 *password = talloc_strndup(*private1 /* hopefully the right gensec context, useful to use for talloc */,
276 (const char *)in.data, in.length);
278 if (*password == NULL) {
279 DEBUG(1, ("Out of memory\n"));
280 mux_printf(mux_id, "BH Out of memory\n");
281 data_blob_free(&in);
282 return;
285 mux_printf(mux_id, "OK\n");
286 data_blob_free(&in);
287 return;
289 DEBUG(1, ("Asked for (and expected) a password\n"));
290 mux_printf(mux_id, "BH Expected a password\n");
291 data_blob_free(&in);
294 /**
295 * Callback for password credentials. This is not async, and when
296 * GENSEC and the credentials code is made async, it will look rather
297 * different.
300 static const char *get_password(struct cli_credentials *credentials)
302 char *password = NULL;
303 void *cb = cli_credentials_callback_data_void(credentials);
305 /* Ask for a password */
306 mux_printf((unsigned int)(uintptr_t)cb, "PW\n");
307 cli_credentials_set_callback_data(credentials, NULL);
309 manage_squid_request(cmdline_lp_ctx, NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
310 return password;
314 Check if a string is part of a list.
316 static bool in_list(const char *s, const char *list, bool casesensitive)
318 char *tok;
319 size_t tok_len = 1024;
320 const char *p=list;
322 if (!list)
323 return false;
325 tok = (char *)malloc(tok_len);
326 if (!tok) {
327 return false;
330 while (next_token(&p, tok, LIST_SEP, tok_len)) {
331 if ((casesensitive?strcmp:strcasecmp_m)(tok,s) == 0) {
332 free(tok);
333 return true;
336 free(tok);
337 return false;
340 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
342 if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, true)) {
343 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
344 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
346 if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, true)) {
347 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
348 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
350 if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, true)) {
351 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
352 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
356 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode,
357 struct loadparm_context *lp_ctx,
358 char *buf, int length, void **private1,
359 unsigned int mux_id, void **private2)
361 DATA_BLOB in;
362 DATA_BLOB out = data_blob(NULL, 0);
363 char *out_base64 = NULL;
364 const char *reply_arg = NULL;
365 struct gensec_ntlm_state {
366 struct gensec_security *gensec_state;
367 const char *set_password;
369 struct gensec_ntlm_state *state;
370 struct tevent_context *ev;
371 struct imessaging_context *msg;
373 NTSTATUS nt_status;
374 bool first = false;
375 const char *reply_code;
376 struct cli_credentials *creds;
378 static char *want_feature_list = NULL;
379 static DATA_BLOB session_key;
381 TALLOC_CTX *mem_ctx;
383 if (*private1) {
384 state = (struct gensec_ntlm_state *)*private1;
385 } else {
386 state = talloc_zero(NULL, struct gensec_ntlm_state);
387 if (!state) {
388 mux_printf(mux_id, "BH No Memory\n");
389 exit(1);
391 *private1 = state;
392 if (opt_password) {
393 state->set_password = opt_password;
397 if (strlen(buf) < 2) {
398 DEBUG(1, ("query [%s] invalid", buf));
399 mux_printf(mux_id, "BH Query invalid\n");
400 return;
403 if (strlen(buf) > 3) {
404 if(strncmp(buf, "SF ", 3) == 0) {
405 DEBUG(10, ("Setting flags to negotiate\n"));
406 talloc_free(want_feature_list);
407 want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
408 mux_printf(mux_id, "OK\n");
409 return;
411 in = base64_decode_data_blob(buf + 3);
412 } else {
413 in = data_blob(NULL, 0);
416 if (strncmp(buf, "YR", 2) == 0) {
417 if (state->gensec_state) {
418 talloc_free(state->gensec_state);
419 state->gensec_state = NULL;
421 } else if ( (strncmp(buf, "OK", 2) == 0)) {
422 /* Just return BH, like ntlm_auth from Samba 3 does. */
423 mux_printf(mux_id, "BH Command expected\n");
424 data_blob_free(&in);
425 return;
426 } else if ( (strncmp(buf, "TT ", 3) != 0) &&
427 (strncmp(buf, "KK ", 3) != 0) &&
428 (strncmp(buf, "AF ", 3) != 0) &&
429 (strncmp(buf, "NA ", 3) != 0) &&
430 (strncmp(buf, "UG", 2) != 0) &&
431 (strncmp(buf, "PW ", 3) != 0) &&
432 (strncmp(buf, "GK", 2) != 0) &&
433 (strncmp(buf, "GF", 2) != 0)) {
434 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
435 mux_printf(mux_id, "BH SPNEGO request invalid\n");
436 data_blob_free(&in);
437 return;
440 ev = s4_event_context_init(state);
441 if (!ev) {
442 exit(1);
445 mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
447 /* setup gensec */
448 if (!(state->gensec_state)) {
449 switch (stdio_helper_mode) {
450 case GSS_SPNEGO_CLIENT:
451 case NTLMSSP_CLIENT_1:
452 /* setup the client side */
454 nt_status = gensec_client_start(NULL, &state->gensec_state,
455 lpcfg_gensec_settings(NULL, lp_ctx));
456 if (!NT_STATUS_IS_OK(nt_status)) {
457 talloc_free(mem_ctx);
458 exit(1);
461 break;
462 case GSS_SPNEGO_SERVER:
463 case SQUID_2_5_NTLMSSP:
465 const char *winbind_method[] = { "winbind", NULL };
466 struct auth4_context *auth_context;
468 msg = imessaging_client_init(state, lp_ctx, ev);
469 if (!msg) {
470 talloc_free(mem_ctx);
471 exit(1);
473 nt_status = auth_context_create_methods(mem_ctx,
474 winbind_method,
475 ev,
476 msg,
477 lp_ctx,
478 NULL,
479 &auth_context);
481 if (!NT_STATUS_IS_OK(nt_status)) {
482 talloc_free(mem_ctx);
483 exit(1);
486 if (!NT_STATUS_IS_OK(gensec_server_start(state,
487 lpcfg_gensec_settings(state, lp_ctx),
488 auth_context, &state->gensec_state))) {
489 talloc_free(mem_ctx);
490 exit(1);
492 break;
494 default:
495 talloc_free(mem_ctx);
496 abort();
499 creds = cli_credentials_init(state->gensec_state);
500 cli_credentials_set_conf(creds, lp_ctx);
501 if (opt_username) {
502 cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
504 if (opt_domain) {
505 cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
507 if (state->set_password) {
508 cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
509 } else {
510 void *cb = (void*)(uintptr_t)mux_id;
511 cli_credentials_set_callback_data(creds, cb);
512 cli_credentials_set_password_callback(creds, get_password);
514 if (opt_workstation) {
515 cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
518 switch (stdio_helper_mode) {
519 case GSS_SPNEGO_SERVER:
520 case SQUID_2_5_NTLMSSP:
521 cli_credentials_set_machine_account(creds, lp_ctx);
522 break;
523 default:
524 break;
527 gensec_set_credentials(state->gensec_state, creds);
528 gensec_want_feature_list(state->gensec_state, want_feature_list);
530 switch (stdio_helper_mode) {
531 case GSS_SPNEGO_CLIENT:
532 case GSS_SPNEGO_SERVER:
533 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
534 if (!in.length) {
535 first = true;
537 break;
538 case NTLMSSP_CLIENT_1:
539 if (!in.length) {
540 first = true;
542 /* fall through */
543 case SQUID_2_5_NTLMSSP:
544 nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
545 break;
546 default:
547 talloc_free(mem_ctx);
548 abort();
551 if (!NT_STATUS_IS_OK(nt_status)) {
552 DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
553 mux_printf(mux_id, "BH GENSEC mech failed to start\n");
554 talloc_free(mem_ctx);
555 return;
560 /* update */
562 if (strncmp(buf, "PW ", 3) == 0) {
563 state->set_password = talloc_strndup(state,
564 (const char *)in.data,
565 in.length);
567 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
568 state->set_password,
569 CRED_SPECIFIED);
570 mux_printf(mux_id, "OK\n");
571 data_blob_free(&in);
572 talloc_free(mem_ctx);
573 return;
576 if (strncmp(buf, "UG", 2) == 0) {
577 int i;
578 char *grouplist = NULL;
579 struct auth_session_info *session_info;
581 nt_status = gensec_session_info(state->gensec_state, mem_ctx, &session_info);
582 if (!NT_STATUS_IS_OK(nt_status)) {
583 DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
584 mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
585 data_blob_free(&in);
586 talloc_free(mem_ctx);
587 return;
590 /* get the string onto the context */
591 grouplist = talloc_strdup(mem_ctx, "");
593 for (i=0; i<session_info->security_token->num_sids; i++) {
594 struct security_token *token = session_info->security_token;
595 const char *sidstr = dom_sid_string(session_info,
596 &token->sids[i]);
597 grouplist = talloc_asprintf_append_buffer(grouplist, "%s,", sidstr);
600 mux_printf(mux_id, "GL %s\n", grouplist);
601 talloc_free(session_info);
602 data_blob_free(&in);
603 talloc_free(mem_ctx);
604 return;
607 if (strncmp(buf, "GK", 2) == 0) {
608 char *base64_key;
609 DEBUG(10, ("Requested session key\n"));
610 nt_status = gensec_session_key(state->gensec_state, mem_ctx, &session_key);
611 if(!NT_STATUS_IS_OK(nt_status)) {
612 DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
613 mux_printf(mux_id, "BH No session key\n");
614 talloc_free(mem_ctx);
615 return;
616 } else {
617 base64_key = base64_encode_data_blob(state, session_key);
618 mux_printf(mux_id, "GK %s\n", base64_key);
619 talloc_free(base64_key);
621 talloc_free(mem_ctx);
622 return;
625 if (strncmp(buf, "GF", 2) == 0) {
626 struct ntlmssp_state *ntlmssp_state;
627 uint32_t neg_flags;
629 ntlmssp_state = talloc_get_type(state->gensec_state->private_data,
630 struct ntlmssp_state);
631 neg_flags = ntlmssp_state->neg_flags;
633 DEBUG(10, ("Requested negotiated feature flags\n"));
634 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
635 return;
638 nt_status = gensec_update_ev(state->gensec_state, mem_ctx, ev, in, &out);
640 /* don't leak 'bad password'/'no such user' info to the network client */
641 nt_status = nt_status_squash(nt_status);
643 if (out.length) {
644 out_base64 = base64_encode_data_blob(mem_ctx, out);
645 } else {
646 out_base64 = NULL;
649 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
650 reply_arg = "*";
651 if (first) {
652 reply_code = "YR";
653 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
654 reply_code = "KK";
655 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) {
656 reply_code = "TT";
657 } else {
658 abort();
662 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
663 reply_code = "BH NT_STATUS_ACCESS_DENIED";
664 reply_arg = nt_errstr(nt_status);
665 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
666 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
667 reply_code = "BH NT_STATUS_UNSUCCESSFUL";
668 reply_arg = nt_errstr(nt_status);
669 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
670 } else if (!NT_STATUS_IS_OK(nt_status)) {
671 reply_code = "NA";
672 reply_arg = nt_errstr(nt_status);
673 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
674 } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
675 struct auth_session_info *session_info;
677 nt_status = gensec_session_info(state->gensec_state, mem_ctx, &session_info);
678 if (!NT_STATUS_IS_OK(nt_status)) {
679 reply_code = "BH Failed to retrive session info";
680 reply_arg = nt_errstr(nt_status);
681 DEBUG(1, ("GENSEC failed to retrieve the session info: %s\n", nt_errstr(nt_status)));
682 } else {
684 reply_code = "AF";
685 reply_arg = talloc_asprintf(state->gensec_state,
686 "%s%s%s", session_info->info->domain_name,
687 lpcfg_winbind_separator(lp_ctx), session_info->info->account_name);
688 talloc_free(session_info);
690 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
691 reply_code = "AF";
692 reply_arg = out_base64;
693 } else {
694 abort();
697 switch (stdio_helper_mode) {
698 case GSS_SPNEGO_SERVER:
699 mux_printf(mux_id, "%s %s %s\n", reply_code,
700 out_base64 ? out_base64 : "*",
701 reply_arg ? reply_arg : "*");
702 break;
703 default:
704 if (out_base64) {
705 mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
706 } else if (reply_arg) {
707 mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
708 } else {
709 mux_printf(mux_id, "%s\n", reply_code);
713 talloc_free(mem_ctx);
714 return;
717 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode,
718 struct loadparm_context *lp_ctx,
719 char *buf, int length, void **private1,
720 unsigned int mux_id, void **private2)
722 char *request, *parameter;
723 static DATA_BLOB challenge;
724 static DATA_BLOB lm_response;
725 static DATA_BLOB nt_response;
726 static char *full_username;
727 static char *username;
728 static char *domain;
729 static char *plaintext_password;
730 static bool ntlm_server_1_user_session_key;
731 static bool ntlm_server_1_lm_session_key;
733 if (strequal(buf, ".")) {
734 if (!full_username && !username) {
735 mux_printf(mux_id, "Error: No username supplied!\n");
736 } else if (plaintext_password) {
737 /* handle this request as plaintext */
738 if (!full_username) {
739 if (asprintf(&full_username, "%s%c%s", domain, *lpcfg_winbind_separator(lp_ctx), username) < 0) {
740 mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
741 return;
744 if (check_plaintext_auth(full_username, plaintext_password, false)) {
745 mux_printf(mux_id, "Authenticated: Yes\n");
746 } else {
747 mux_printf(mux_id, "Authenticated: No\n");
749 } else if (!lm_response.data && !nt_response.data) {
750 mux_printf(mux_id, "Error: No password supplied!\n");
751 } else if (!challenge.data) {
752 mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
753 } else {
754 char *error_string = NULL;
755 DATA_BLOB lm_key;
756 DATA_BLOB user_session_key;
757 uint32_t flags = 0;
759 if (full_username && !username) {
760 SAFE_FREE(username);
761 SAFE_FREE(domain);
762 if (!parse_ntlm_auth_domain_user(full_username, &username,
763 &domain,
764 *lpcfg_winbind_separator(lp_ctx))) {
765 /* username might be 'tainted', don't print into our new-line deleimianted stream */
766 mux_printf(mux_id, "Error: Could not parse into domain and username\n");
770 if (!domain) {
771 domain = smb_xstrdup(lpcfg_workgroup(lp_ctx));
774 if (ntlm_server_1_lm_session_key)
775 flags |= NTLM_AUTH_FLAG_LMKEY;
777 if (ntlm_server_1_user_session_key)
778 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
780 if (!NT_STATUS_IS_OK(
781 local_pw_check_specified(lp_ctx,
782 username,
783 domain,
784 lpcfg_netbios_name(lp_ctx),
785 &challenge,
786 &lm_response,
787 &nt_response,
788 flags,
789 &lm_key,
790 &user_session_key,
791 &error_string,
792 NULL))) {
794 mux_printf(mux_id, "Authenticated: No\n");
795 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
796 SAFE_FREE(error_string);
797 } else {
798 static char zeros[16];
799 char *hex_lm_key;
800 char *hex_user_session_key;
802 mux_printf(mux_id, "Authenticated: Yes\n");
804 if (ntlm_server_1_lm_session_key
805 && lm_key.length
806 && (memcmp(zeros, lm_key.data,
807 lm_key.length) != 0)) {
808 hex_encode(lm_key.data,
809 lm_key.length,
810 &hex_lm_key);
811 mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
812 SAFE_FREE(hex_lm_key);
815 if (ntlm_server_1_user_session_key
816 && user_session_key.length
817 && (memcmp(zeros, user_session_key.data,
818 user_session_key.length) != 0)) {
819 hex_encode(user_session_key.data,
820 user_session_key.length,
821 &hex_user_session_key);
822 mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
823 SAFE_FREE(hex_user_session_key);
827 /* clear out the state */
828 challenge = data_blob(NULL, 0);
829 nt_response = data_blob(NULL, 0);
830 lm_response = data_blob(NULL, 0);
831 SAFE_FREE(full_username);
832 SAFE_FREE(username);
833 SAFE_FREE(domain);
834 SAFE_FREE(plaintext_password);
835 ntlm_server_1_user_session_key = false;
836 ntlm_server_1_lm_session_key = false;
837 mux_printf(mux_id, ".\n");
839 return;
842 request = buf;
844 /* Indicates a base64 encoded structure */
845 parameter = strstr(request, ":: ");
846 if (!parameter) {
847 parameter = strstr(request, ": ");
849 if (!parameter) {
850 DEBUG(0, ("Parameter not found!\n"));
851 mux_printf(mux_id, "Error: Parameter not found!\n.\n");
852 return;
855 parameter[0] ='\0';
856 parameter++;
857 parameter[0] ='\0';
858 parameter++;
860 } else {
861 parameter[0] ='\0';
862 parameter++;
863 parameter[0] ='\0';
864 parameter++;
865 parameter[0] ='\0';
866 parameter++;
868 base64_decode_inplace(parameter);
871 if (strequal(request, "LANMAN-Challenge")) {
872 challenge = strhex_to_data_blob(NULL, parameter);
873 if (challenge.length != 8) {
874 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
875 parameter,
876 (int)challenge.length);
877 challenge = data_blob(NULL, 0);
879 } else if (strequal(request, "NT-Response")) {
880 nt_response = strhex_to_data_blob(NULL, parameter);
881 if (nt_response.length < 24) {
882 mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
883 parameter,
884 (int)nt_response.length);
885 nt_response = data_blob(NULL, 0);
887 } else if (strequal(request, "LANMAN-Response")) {
888 lm_response = strhex_to_data_blob(NULL, parameter);
889 if (lm_response.length != 24) {
890 mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
891 parameter,
892 (int)lm_response.length);
893 lm_response = data_blob(NULL, 0);
895 } else if (strequal(request, "Password")) {
896 plaintext_password = smb_xstrdup(parameter);
897 } else if (strequal(request, "NT-Domain")) {
898 domain = smb_xstrdup(parameter);
899 } else if (strequal(request, "Username")) {
900 username = smb_xstrdup(parameter);
901 } else if (strequal(request, "Full-Username")) {
902 full_username = smb_xstrdup(parameter);
903 } else if (strequal(request, "Request-User-Session-Key")) {
904 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
905 } else if (strequal(request, "Request-LanMan-Session-Key")) {
906 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
907 } else {
908 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
912 static void manage_squid_request(struct loadparm_context *lp_ctx, enum stdio_helper_mode helper_mode,
913 stdio_helper_function fn, void **private2)
915 char *buf;
916 char tmp[INITIAL_BUFFER_SIZE+1];
917 unsigned int mux_id = 0;
918 int length, buf_size = 0;
919 char *c;
920 struct mux_private {
921 unsigned int max_mux;
922 void **private_pointers;
925 static struct mux_private *mux_private;
926 static void *normal_private;
927 void **private1;
929 buf = talloc_strdup(NULL, "");
931 if (buf == NULL) {
932 DEBUG(0, ("Failed to allocate memory for reading the input "
933 "buffer.\n"));
934 x_fprintf(x_stdout, "ERR\n");
935 return;
938 do {
939 /* this is not a typo - x_fgets doesn't work too well under
940 * squid */
941 if (fgets(tmp, INITIAL_BUFFER_SIZE, stdin) == NULL) {
942 if (ferror(stdin)) {
943 DEBUG(1, ("fgets() failed! dying..... errno=%d "
944 "(%s)\n", ferror(stdin),
945 strerror(ferror(stdin))));
947 exit(1); /* BIIG buffer */
949 exit(0);
952 buf = talloc_strdup_append_buffer(buf, tmp);
953 buf_size += INITIAL_BUFFER_SIZE;
955 if (buf_size > MAX_BUFFER_SIZE) {
956 DEBUG(0, ("Invalid Request (too large)\n"));
957 x_fprintf(x_stdout, "ERR\n");
958 talloc_free(buf);
959 return;
962 c = strchr(buf, '\n');
963 } while (c == NULL);
965 *c = '\0';
966 length = c-buf;
968 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
970 if (buf[0] == '\0') {
971 DEBUG(0, ("Invalid Request (empty)\n"));
972 x_fprintf(x_stdout, "ERR\n");
973 talloc_free(buf);
974 return;
977 if (opt_multiplex) {
978 if (sscanf(buf, "%u ", &mux_id) != 1) {
979 DEBUG(0, ("Invalid Request - no multiplex id\n"));
980 x_fprintf(x_stdout, "ERR\n");
981 talloc_free(buf);
982 return;
984 if (!mux_private) {
985 mux_private = talloc(NULL, struct mux_private);
986 mux_private->max_mux = 0;
987 mux_private->private_pointers = NULL;
990 c=strchr(buf,' ');
991 if (!c) {
992 DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
993 x_fprintf(x_stdout, "ERR\n");
994 talloc_free(buf);
995 return;
997 c++;
998 if (mux_id >= mux_private->max_mux) {
999 unsigned int prev_max = mux_private->max_mux;
1000 mux_private->max_mux = mux_id + 1;
1001 mux_private->private_pointers
1002 = talloc_realloc(mux_private,
1003 mux_private->private_pointers,
1004 void *, mux_private->max_mux);
1005 memset(&mux_private->private_pointers[prev_max], '\0',
1006 (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max)));
1009 private1 = &mux_private->private_pointers[mux_id];
1010 } else {
1011 c = buf;
1012 private1 = &normal_private;
1015 fn(helper_mode, lp_ctx, c, length, private1, mux_id, private2);
1016 talloc_free(buf);
1019 static void squid_stream(struct loadparm_context *lp_ctx,
1020 enum stdio_helper_mode stdio_mode,
1021 stdio_helper_function fn) {
1022 /* initialize FDescs */
1023 x_setbuf(x_stdout, NULL);
1024 x_setbuf(x_stderr, NULL);
1025 while(1) {
1026 manage_squid_request(lp_ctx, stdio_mode, fn, NULL);
1031 /* Main program */
1033 enum {
1034 OPT_USERNAME = 1000,
1035 OPT_DOMAIN,
1036 OPT_WORKSTATION,
1037 OPT_CHALLENGE,
1038 OPT_RESPONSE,
1039 OPT_LM,
1040 OPT_NT,
1041 OPT_PASSWORD,
1042 OPT_LM_KEY,
1043 OPT_USER_SESSION_KEY,
1044 OPT_DIAGNOSTICS,
1045 OPT_REQUIRE_MEMBERSHIP,
1046 OPT_MULTIPLEX,
1047 OPT_USE_CACHED_CREDS,
1050 int main(int argc, const char **argv)
1052 static const char *helper_protocol;
1053 int opt;
1055 poptContext pc;
1057 /* NOTE: DO NOT change this interface without considering the implications!
1058 This is an external interface, which other programs will use to interact
1059 with this helper.
1062 /* We do not use single-letter command abbreviations, because they harm future
1063 interface stability. */
1065 struct poptOption long_options[] = {
1066 POPT_AUTOHELP
1067 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1068 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1069 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1070 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},
1071 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
1072 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1073 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1074 POPT_COMMON_SAMBA
1075 POPT_COMMON_VERSION
1076 { NULL }
1079 /* Samba client initialisation */
1081 setup_logging(NULL, DEBUG_STDERR);
1083 /* Parse options */
1085 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1087 /* Parse command line options */
1089 if (argc == 1) {
1090 poptPrintHelp(pc, stderr, 0);
1091 return 1;
1094 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
1095 POPT_CONTEXT_KEEP_FIRST);
1097 while((opt = poptGetNextOpt(pc)) != -1) {
1098 if (opt < -1) {
1099 break;
1102 if (opt < -1) {
1103 fprintf(stderr, "%s: %s\n",
1104 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1105 poptStrerror(opt));
1106 return 1;
1109 gensec_init();
1111 if (opt_domain == NULL) {
1112 opt_domain = lpcfg_workgroup(cmdline_lp_ctx);
1115 if (helper_protocol) {
1116 int i;
1117 for (i=0; i<NUM_HELPER_MODES; i++) {
1118 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1119 squid_stream(cmdline_lp_ctx, stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1120 exit(0);
1123 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1125 for (i=0; i<NUM_HELPER_MODES; i++) {
1126 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1129 exit(1);
1132 if (!opt_username) {
1133 x_fprintf(x_stderr, "username must be specified!\n\n");
1134 poptPrintHelp(pc, stderr, 0);
1135 exit(1);
1138 if (opt_workstation == NULL) {
1139 opt_workstation = lpcfg_netbios_name(cmdline_lp_ctx);
1142 if (!opt_password) {
1143 char pwd[256] = {0};
1144 int rc;
1146 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
1147 if (rc == 0) {
1148 opt_password = smb_xstrdup(pwd);
1153 char *user;
1155 if (asprintf(&user, "%s%c%s", opt_domain,
1156 *lpcfg_winbind_separator(cmdline_lp_ctx),
1157 opt_username) < 0) {
1158 return 1;
1160 if (!check_plaintext_auth(user, opt_password, true)) {
1161 return 1;
1165 /* Exit code */
1167 poptFreeContext(pc);
1168 return 0;