source: branches/samba-3.5.x/source3/utils/ntlm_auth.c

Last change on this file was 599, checked in by Herwig Bauernfeind, 14 years ago

Samba 3.5: Update trunk to 3.5.9

File size: 75.9 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Winbind status program.
5
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
11
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.
16
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.
21
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/>.
24*/
25
26#include "includes.h"
27#include "utils/ntlm_auth.h"
28#include "../libcli/auth/libcli_auth.h"
29#include "../libcli/auth/spnego.h"
30#include "smb_krb5.h"
31#include <iniparser.h>
32
33#ifndef PAM_WINBIND_CONFIG_FILE
34#define PAM_WINBIND_CONFIG_FILE "/etc/security/pam_winbind.conf"
35#endif
36
37#define WINBIND_KRB5_AUTH 0x00000080
38
39#undef DBGC_CLASS
40#define DBGC_CLASS DBGC_WINBIND
41
42#define INITIAL_BUFFER_SIZE 300
43#define MAX_BUFFER_SIZE 630000
44
45enum 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,
51 GSS_SPNEGO_CLIENT,
52 NTLM_SERVER_1,
53 NTLM_CHANGE_PASSWORD_1,
54 NUM_HELPER_MODES
55};
56
57enum ntlm_auth_cli_state {
58 CLIENT_INITIAL = 0,
59 CLIENT_RESPONSE,
60 CLIENT_FINISHED,
61 CLIENT_ERROR
62};
63
64enum ntlm_auth_svr_state {
65 SERVER_INITIAL = 0,
66 SERVER_CHALLENGE,
67 SERVER_FINISHED,
68 SERVER_ERROR
69};
70
71struct ntlm_auth_state {
72 TALLOC_CTX *mem_ctx;
73 enum stdio_helper_mode helper_mode;
74 enum ntlm_auth_cli_state cli_state;
75 enum ntlm_auth_svr_state svr_state;
76 struct ntlmssp_state *ntlmssp_state;
77 uint32_t neg_flags;
78 char *want_feature_list;
79 char *spnego_mech;
80 char *spnego_mech_oid;
81 bool have_session_key;
82 DATA_BLOB session_key;
83 DATA_BLOB initial_message;
84};
85
86typedef void (*stdio_helper_function)(struct ntlm_auth_state *state, char *buf,
87 int length);
88
89static void manage_squid_basic_request (struct ntlm_auth_state *state,
90 char *buf, int length);
91
92static void manage_squid_ntlmssp_request (struct ntlm_auth_state *state,
93 char *buf, int length);
94
95static void manage_client_ntlmssp_request (struct ntlm_auth_state *state,
96 char *buf, int length);
97
98static void manage_gss_spnego_request (struct ntlm_auth_state *state,
99 char *buf, int length);
100
101static void manage_gss_spnego_client_request (struct ntlm_auth_state *state,
102 char *buf, int length);
103
104static void manage_ntlm_server_1_request (struct ntlm_auth_state *state,
105 char *buf, int length);
106
107static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
108 char *buf, int length);
109
110static const struct {
111 enum stdio_helper_mode mode;
112 const char *name;
113 stdio_helper_function fn;
114} stdio_helper_protocols[] = {
115 { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
116 { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
117 { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_squid_ntlmssp_request},
118 { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_client_ntlmssp_request},
119 { GSS_SPNEGO, "gss-spnego", manage_gss_spnego_request},
120 { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gss_spnego_client_request},
121 { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
122 { NTLM_CHANGE_PASSWORD_1, "ntlm-change-password-1", manage_ntlm_change_password_1_request},
123 { NUM_HELPER_MODES, NULL, NULL}
124};
125
126const char *opt_username;
127const char *opt_domain;
128const char *opt_workstation;
129const char *opt_password;
130static DATA_BLOB opt_challenge;
131static DATA_BLOB opt_lm_response;
132static DATA_BLOB opt_nt_response;
133static int request_lm_key;
134static int request_user_session_key;
135static int use_cached_creds;
136
137static const char *require_membership_of;
138static const char *require_membership_of_sid;
139static const char *opt_pam_winbind_conf;
140
141static char winbind_separator(void)
142{
143 struct winbindd_response response;
144 static bool got_sep;
145 static char sep;
146
147 if (got_sep)
148 return sep;
149
150 ZERO_STRUCT(response);
151
152 /* Send off request */
153
154 if (winbindd_request_response(WINBINDD_INFO, NULL, &response) !=
155 NSS_STATUS_SUCCESS) {
156 d_printf("could not obtain winbind separator!\n");
157 return *lp_winbind_separator();
158 }
159
160 sep = response.data.info.winbind_separator;
161 got_sep = True;
162
163 if (!sep) {
164 d_printf("winbind separator was NULL!\n");
165 return *lp_winbind_separator();
166 }
167
168 return sep;
169}
170
171const char *get_winbind_domain(void)
172{
173 struct winbindd_response response;
174
175 static fstring winbind_domain;
176 if (*winbind_domain) {
177 return winbind_domain;
178 }
179
180 ZERO_STRUCT(response);
181
182 /* Send off request */
183
184 if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) !=
185 NSS_STATUS_SUCCESS) {
186 DEBUG(0, ("could not obtain winbind domain name!\n"));
187 return lp_workgroup();
188 }
189
190 fstrcpy(winbind_domain, response.data.domain_name);
191
192 return winbind_domain;
193
194}
195
196const char *get_winbind_netbios_name(void)
197{
198 struct winbindd_response response;
199
200 static fstring winbind_netbios_name;
201
202 if (*winbind_netbios_name) {
203 return winbind_netbios_name;
204 }
205
206 ZERO_STRUCT(response);
207
208 /* Send off request */
209
210 if (winbindd_request_response(WINBINDD_NETBIOS_NAME, NULL, &response) !=
211 NSS_STATUS_SUCCESS) {
212 DEBUG(0, ("could not obtain winbind netbios name!\n"));
213 return global_myname();
214 }
215
216 fstrcpy(winbind_netbios_name, response.data.netbios_name);
217
218 return winbind_netbios_name;
219
220}
221
222DATA_BLOB get_challenge(void)
223{
224 static DATA_BLOB chal;
225 if (opt_challenge.length)
226 return opt_challenge;
227
228 chal = data_blob(NULL, 8);
229
230 generate_random_buffer(chal.data, chal.length);
231 return chal;
232}
233
234/* Copy of parse_domain_user from winbindd_util.c. Parse a string of the
235 form DOMAIN/user into a domain and a user */
236
237static bool parse_ntlm_auth_domain_user(const char *domuser, fstring domain,
238 fstring user)
239{
240
241 char *p = strchr(domuser,winbind_separator());
242
243 if (!p) {
244 return False;
245 }
246
247 fstrcpy(user, p+1);
248 fstrcpy(domain, domuser);
249 domain[PTR_DIFF(p, domuser)] = 0;
250 strupper_m(domain);
251
252 return True;
253}
254
255static bool get_require_membership_sid(void) {
256 struct winbindd_request request;
257 struct winbindd_response response;
258
259 if (!require_membership_of) {
260 return True;
261 }
262
263 if (require_membership_of_sid) {
264 return True;
265 }
266
267 /* Otherwise, ask winbindd for the name->sid request */
268
269 ZERO_STRUCT(request);
270 ZERO_STRUCT(response);
271
272 if (!parse_ntlm_auth_domain_user(require_membership_of,
273 request.data.name.dom_name,
274 request.data.name.name)) {
275 DEBUG(0, ("Could not parse %s into seperate domain/name parts!\n",
276 require_membership_of));
277 return False;
278 }
279
280 if (winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response) !=
281 NSS_STATUS_SUCCESS) {
282 DEBUG(0, ("Winbindd lookupname failed to resolve %s into a SID!\n",
283 require_membership_of));
284 return False;
285 }
286
287 require_membership_of_sid = SMB_STRDUP(response.data.sid.sid);
288
289 if (require_membership_of_sid)
290 return True;
291
292 return False;
293}
294
295/*
296 * Get some configuration from pam_winbind.conf to see if we
297 * need to contact trusted domain
298 */
299
300int get_pam_winbind_config()
301{
302 int ctrl = 0;
303 dictionary *d = NULL;
304
305 if (!opt_pam_winbind_conf || !*opt_pam_winbind_conf) {
306 opt_pam_winbind_conf = PAM_WINBIND_CONFIG_FILE;
307 }
308
309 d = iniparser_load(CONST_DISCARD(char *, opt_pam_winbind_conf));
310
311 if (!d) {
312 return 0;
313 }
314
315 if (iniparser_getboolean(d, CONST_DISCARD(char *, "global:krb5_auth"), false)) {
316 ctrl |= WINBIND_KRB5_AUTH;
317 }
318
319 iniparser_freedict(d);
320
321 return ctrl;
322}
323
324/* Authenticate a user with a plaintext password */
325
326static bool check_plaintext_auth(const char *user, const char *pass,
327 bool stdout_diagnostics)
328{
329 struct winbindd_request request;
330 struct winbindd_response response;
331 NSS_STATUS result;
332
333 if (!get_require_membership_sid()) {
334 return False;
335 }
336
337 /* Send off request */
338
339 ZERO_STRUCT(request);
340 ZERO_STRUCT(response);
341
342 fstrcpy(request.data.auth.user, user);
343 fstrcpy(request.data.auth.pass, pass);
344 if (require_membership_of_sid) {
345 strlcpy(request.data.auth.require_membership_of_sid,
346 require_membership_of_sid,
347 sizeof(request.data.auth.require_membership_of_sid));
348 }
349
350 result = winbindd_request_response(WINBINDD_PAM_AUTH, &request, &response);
351
352 /* Display response */
353
354 if (stdout_diagnostics) {
355 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
356 d_printf("Reading winbind reply failed! (0x01)\n");
357 }
358
359 d_printf("%s: %s (0x%x)\n",
360 response.data.auth.nt_status_string,
361 response.data.auth.error_string,
362 response.data.auth.nt_status);
363 } else {
364 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
365 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
366 }
367
368 DEBUG(3, ("%s: %s (0x%x)\n",
369 response.data.auth.nt_status_string,
370 response.data.auth.error_string,
371 response.data.auth.nt_status));
372 }
373
374 return (result == NSS_STATUS_SUCCESS);
375}
376
377/* authenticate a user with an encrypted username/password */
378
379NTSTATUS contact_winbind_auth_crap(const char *username,
380 const char *domain,
381 const char *workstation,
382 const DATA_BLOB *challenge,
383 const DATA_BLOB *lm_response,
384 const DATA_BLOB *nt_response,
385 uint32 flags,
386 uint8 lm_key[8],
387 uint8 user_session_key[16],
388 char **error_string,
389 char **unix_name)
390{
391 NTSTATUS nt_status;
392 NSS_STATUS result;
393 struct winbindd_request request;
394 struct winbindd_response response;
395
396 if (!get_require_membership_sid()) {
397 return NT_STATUS_INVALID_PARAMETER;
398 }
399
400 ZERO_STRUCT(request);
401 ZERO_STRUCT(response);
402
403 request.flags = flags;
404
405 request.data.auth_crap.logon_parameters = MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT | MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT;
406
407 if (require_membership_of_sid)
408 fstrcpy(request.data.auth_crap.require_membership_of_sid, require_membership_of_sid);
409
410 fstrcpy(request.data.auth_crap.user, username);
411 fstrcpy(request.data.auth_crap.domain, domain);
412
413 fstrcpy(request.data.auth_crap.workstation,
414 workstation);
415
416 memcpy(request.data.auth_crap.chal, challenge->data, MIN(challenge->length, 8));
417
418 if (lm_response && lm_response->length) {
419 memcpy(request.data.auth_crap.lm_resp,
420 lm_response->data,
421 MIN(lm_response->length, sizeof(request.data.auth_crap.lm_resp)));
422 request.data.auth_crap.lm_resp_len = lm_response->length;
423 }
424
425 if (nt_response && nt_response->length) {
426 if (nt_response->length > sizeof(request.data.auth_crap.nt_resp)) {
427 request.flags = request.flags | WBFLAG_BIG_NTLMV2_BLOB;
428 request.extra_len = nt_response->length;
429 request.extra_data.data = SMB_MALLOC_ARRAY(char, request.extra_len);
430 if (request.extra_data.data == NULL) {
431 return NT_STATUS_NO_MEMORY;
432 }
433 memcpy(request.extra_data.data, nt_response->data,
434 nt_response->length);
435
436 } else {
437 memcpy(request.data.auth_crap.nt_resp,
438 nt_response->data, nt_response->length);
439 }
440 request.data.auth_crap.nt_resp_len = nt_response->length;
441 }
442
443 result = winbindd_request_response(WINBINDD_PAM_AUTH_CRAP, &request, &response);
444 SAFE_FREE(request.extra_data.data);
445
446 /* Display response */
447
448 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
449 nt_status = NT_STATUS_UNSUCCESSFUL;
450 if (error_string)
451 *error_string = smb_xstrdup("Reading winbind reply failed!");
452 winbindd_free_response(&response);
453 return nt_status;
454 }
455
456 nt_status = (NT_STATUS(response.data.auth.nt_status));
457 if (!NT_STATUS_IS_OK(nt_status)) {
458 if (error_string)
459 *error_string = smb_xstrdup(response.data.auth.error_string);
460 winbindd_free_response(&response);
461 return nt_status;
462 }
463
464 if ((flags & WBFLAG_PAM_LMKEY) && lm_key) {
465 memcpy(lm_key, response.data.auth.first_8_lm_hash,
466 sizeof(response.data.auth.first_8_lm_hash));
467 }
468 if ((flags & WBFLAG_PAM_USER_SESSION_KEY) && user_session_key) {
469 memcpy(user_session_key, response.data.auth.user_session_key,
470 sizeof(response.data.auth.user_session_key));
471 }
472
473 if (flags & WBFLAG_PAM_UNIX_NAME) {
474 *unix_name = SMB_STRDUP(response.data.auth.unix_username);
475 if (!*unix_name) {
476 winbindd_free_response(&response);
477 return NT_STATUS_NO_MEMORY;
478 }
479 }
480
481 winbindd_free_response(&response);
482 return nt_status;
483}
484
485/* contact server to change user password using auth crap */
486static NTSTATUS contact_winbind_change_pswd_auth_crap(const char *username,
487 const char *domain,
488 const DATA_BLOB new_nt_pswd,
489 const DATA_BLOB old_nt_hash_enc,
490 const DATA_BLOB new_lm_pswd,
491 const DATA_BLOB old_lm_hash_enc,
492 char **error_string)
493{
494 NTSTATUS nt_status;
495 NSS_STATUS result;
496 struct winbindd_request request;
497 struct winbindd_response response;
498
499 if (!get_require_membership_sid())
500 {
501 if(error_string)
502 *error_string = smb_xstrdup("Can't get membership sid.");
503 return NT_STATUS_INVALID_PARAMETER;
504 }
505
506 ZERO_STRUCT(request);
507 ZERO_STRUCT(response);
508
509 if(username != NULL)
510 fstrcpy(request.data.chng_pswd_auth_crap.user, username);
511 if(domain != NULL)
512 fstrcpy(request.data.chng_pswd_auth_crap.domain,domain);
513
514 if(new_nt_pswd.length)
515 {
516 memcpy(request.data.chng_pswd_auth_crap.new_nt_pswd, new_nt_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_nt_pswd));
517 request.data.chng_pswd_auth_crap.new_nt_pswd_len = new_nt_pswd.length;
518 }
519
520 if(old_nt_hash_enc.length)
521 {
522 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));
523 request.data.chng_pswd_auth_crap.old_nt_hash_enc_len = old_nt_hash_enc.length;
524 }
525
526 if(new_lm_pswd.length)
527 {
528 memcpy(request.data.chng_pswd_auth_crap.new_lm_pswd, new_lm_pswd.data, sizeof(request.data.chng_pswd_auth_crap.new_lm_pswd));
529 request.data.chng_pswd_auth_crap.new_lm_pswd_len = new_lm_pswd.length;
530 }
531
532 if(old_lm_hash_enc.length)
533 {
534 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));
535 request.data.chng_pswd_auth_crap.old_lm_hash_enc_len = old_lm_hash_enc.length;
536 }
537
538 result = winbindd_request_response(WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, &request, &response);
539
540 /* Display response */
541
542 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0))
543 {
544 nt_status = NT_STATUS_UNSUCCESSFUL;
545 if (error_string)
546 *error_string = smb_xstrdup("Reading winbind reply failed!");
547 winbindd_free_response(&response);
548 return nt_status;
549 }
550
551 nt_status = (NT_STATUS(response.data.auth.nt_status));
552 if (!NT_STATUS_IS_OK(nt_status))
553 {
554 if (error_string)
555 *error_string = smb_xstrdup(response.data.auth.error_string);
556 winbindd_free_response(&response);
557 return nt_status;
558 }
559
560 winbindd_free_response(&response);
561
562 return nt_status;
563}
564
565static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
566{
567 static const char zeros[16] = { 0, };
568 NTSTATUS nt_status;
569 char *error_string = NULL;
570 uint8 lm_key[8];
571 uint8 user_sess_key[16];
572 char *unix_name = NULL;
573
574 nt_status = contact_winbind_auth_crap(ntlmssp_state->user, ntlmssp_state->domain,
575 ntlmssp_state->workstation,
576 &ntlmssp_state->chal,
577 &ntlmssp_state->lm_resp,
578 &ntlmssp_state->nt_resp,
579 WBFLAG_PAM_LMKEY | WBFLAG_PAM_USER_SESSION_KEY | WBFLAG_PAM_UNIX_NAME,
580 lm_key, user_sess_key,
581 &error_string, &unix_name);
582
583 if (NT_STATUS_IS_OK(nt_status)) {
584 if (memcmp(lm_key, zeros, 8) != 0) {
585 *lm_session_key = data_blob_talloc(ntlmssp_state, NULL, 16);
586 memcpy(lm_session_key->data, lm_key, 8);
587 memset(lm_session_key->data+8, '\0', 8);
588 }
589
590 if (memcmp(user_sess_key, zeros, 16) != 0) {
591 *user_session_key = data_blob_talloc(ntlmssp_state, user_sess_key, 16);
592 }
593 ntlmssp_state->auth_context = talloc_strdup(ntlmssp_state,
594 unix_name);
595 } else {
596 DEBUG(NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED) ? 0 : 3,
597 ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
598 ntlmssp_state->domain, ntlmssp_state->user,
599 ntlmssp_state->workstation,
600 error_string ? error_string : "unknown error (NULL)"));
601 ntlmssp_state->auth_context = NULL;
602 }
603
604 SAFE_FREE(error_string);
605 SAFE_FREE(unix_name);
606 return nt_status;
607}
608
609static NTSTATUS local_pw_check(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
610{
611 NTSTATUS nt_status;
612 struct samr_Password lm_pw, nt_pw;
613
614 nt_lm_owf_gen (opt_password, nt_pw.hash, lm_pw.hash);
615
616 nt_status = ntlm_password_check(ntlmssp_state,
617 true, true, 0,
618 &ntlmssp_state->chal,
619 &ntlmssp_state->lm_resp,
620 &ntlmssp_state->nt_resp,
621 ntlmssp_state->user,
622 ntlmssp_state->user,
623 ntlmssp_state->domain,
624 &lm_pw, &nt_pw, user_session_key, lm_session_key);
625
626 if (NT_STATUS_IS_OK(nt_status)) {
627 ntlmssp_state->auth_context = talloc_asprintf(ntlmssp_state,
628 "%s%c%s", ntlmssp_state->domain,
629 *lp_winbind_separator(),
630 ntlmssp_state->user);
631 } else {
632 DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n",
633 ntlmssp_state->domain, ntlmssp_state->user, ntlmssp_state->workstation,
634 nt_errstr(nt_status)));
635 ntlmssp_state->auth_context = NULL;
636 }
637 return nt_status;
638}
639
640static NTSTATUS ntlm_auth_start_ntlmssp_client(NTLMSSP_STATE **client_ntlmssp_state)
641{
642 NTSTATUS status;
643 if ( (opt_username == NULL) || (opt_domain == NULL) ) {
644 status = NT_STATUS_UNSUCCESSFUL;
645 DEBUG(1, ("Need username and domain for NTLMSSP\n"));
646 return NT_STATUS_INVALID_PARAMETER;
647 }
648
649 status = ntlmssp_client_start(client_ntlmssp_state);
650
651 if (!NT_STATUS_IS_OK(status)) {
652 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
653 nt_errstr(status)));
654 ntlmssp_end(client_ntlmssp_state);
655 return status;
656 }
657
658 status = ntlmssp_set_username(*client_ntlmssp_state, opt_username);
659
660 if (!NT_STATUS_IS_OK(status)) {
661 DEBUG(1, ("Could not set username: %s\n",
662 nt_errstr(status)));
663 ntlmssp_end(client_ntlmssp_state);
664 return status;
665 }
666
667 status = ntlmssp_set_domain(*client_ntlmssp_state, opt_domain);
668
669 if (!NT_STATUS_IS_OK(status)) {
670 DEBUG(1, ("Could not set domain: %s\n",
671 nt_errstr(status)));
672 ntlmssp_end(client_ntlmssp_state);
673 return status;
674 }
675
676 if (opt_password) {
677 status = ntlmssp_set_password(*client_ntlmssp_state, opt_password);
678
679 if (!NT_STATUS_IS_OK(status)) {
680 DEBUG(1, ("Could not set password: %s\n",
681 nt_errstr(status)));
682 ntlmssp_end(client_ntlmssp_state);
683 return status;
684 }
685 }
686
687 return NT_STATUS_OK;
688}
689
690static NTSTATUS ntlm_auth_start_ntlmssp_server(NTLMSSP_STATE **ntlmssp_state)
691{
692 NTSTATUS status = ntlmssp_server_start(ntlmssp_state);
693
694 if (!NT_STATUS_IS_OK(status)) {
695 DEBUG(1, ("Could not start NTLMSSP server: %s\n",
696 nt_errstr(status)));
697 return status;
698 }
699
700 /* Have we been given a local password, or should we ask winbind? */
701 if (opt_password) {
702 (*ntlmssp_state)->check_password = local_pw_check;
703 (*ntlmssp_state)->get_domain = lp_workgroup;
704 (*ntlmssp_state)->get_global_myname = global_myname;
705 } else {
706 (*ntlmssp_state)->check_password = winbind_pw_check;
707 (*ntlmssp_state)->get_domain = get_winbind_domain;
708 (*ntlmssp_state)->get_global_myname = get_winbind_netbios_name;
709 }
710 return NT_STATUS_OK;
711}
712
713/*******************************************************************
714 Used by firefox to drive NTLM auth to IIS servers.
715*******************************************************************/
716
717static NTSTATUS do_ccache_ntlm_auth(DATA_BLOB initial_msg, DATA_BLOB challenge_msg,
718 DATA_BLOB *reply)
719{
720 struct winbindd_request wb_request;
721 struct winbindd_response wb_response;
722 int ctrl = 0;
723 NSS_STATUS result;
724
725 /* get winbindd to do the ntlmssp step on our behalf */
726 ZERO_STRUCT(wb_request);
727 ZERO_STRUCT(wb_response);
728
729 /*
730 * This is tricky here. If we set krb5_auth in pam_winbind.conf
731 * creds for users in trusted domain will be stored the winbindd
732 * child of the trusted domain. If we ask the primary domain for
733 * ntlm_ccache_auth, it will fail. So, we have to ask the trusted
734 * domain's child for ccache_ntlm_auth. that is to say, we have to
735 * set WBFALG_PAM_CONTACT_TRUSTDOM in request.flags.
736 */
737 ctrl = get_pam_winbind_config();
738
739 if (ctrl & WINBIND_KRB5_AUTH) {
740 wb_request.flags |= WBFLAG_PAM_CONTACT_TRUSTDOM;
741 }
742
743 fstr_sprintf(wb_request.data.ccache_ntlm_auth.user,
744 "%s%c%s", opt_domain, winbind_separator(), opt_username);
745 wb_request.data.ccache_ntlm_auth.uid = geteuid();
746 wb_request.data.ccache_ntlm_auth.initial_blob_len = initial_msg.length;
747 wb_request.data.ccache_ntlm_auth.challenge_blob_len = challenge_msg.length;
748 wb_request.extra_len = initial_msg.length + challenge_msg.length;
749
750 if (wb_request.extra_len > 0) {
751 wb_request.extra_data.data = SMB_MALLOC_ARRAY(char, wb_request.extra_len);
752 if (wb_request.extra_data.data == NULL) {
753 return NT_STATUS_NO_MEMORY;
754 }
755
756 memcpy(wb_request.extra_data.data, initial_msg.data, initial_msg.length);
757 memcpy(wb_request.extra_data.data + initial_msg.length,
758 challenge_msg.data, challenge_msg.length);
759 }
760
761 result = winbindd_request_response(WINBINDD_CCACHE_NTLMAUTH, &wb_request, &wb_response);
762 SAFE_FREE(wb_request.extra_data.data);
763
764 if (result != NSS_STATUS_SUCCESS) {
765 winbindd_free_response(&wb_response);
766 return NT_STATUS_UNSUCCESSFUL;
767 }
768
769 if (reply) {
770 *reply = data_blob(wb_response.extra_data.data,
771 wb_response.data.ccache_ntlm_auth.auth_blob_len);
772 if (wb_response.data.ccache_ntlm_auth.auth_blob_len > 0 &&
773 reply->data == NULL) {
774 winbindd_free_response(&wb_response);
775 return NT_STATUS_NO_MEMORY;
776 }
777 }
778
779 winbindd_free_response(&wb_response);
780 return NT_STATUS_MORE_PROCESSING_REQUIRED;
781}
782
783static void manage_squid_ntlmssp_request_int(struct ntlm_auth_state *state,
784 char *buf, int length,
785 TALLOC_CTX *mem_ctx,
786 char **response)
787{
788 DATA_BLOB request, reply;
789 NTSTATUS nt_status;
790
791 if (strlen(buf) < 2) {
792 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
793 *response = talloc_strdup(mem_ctx, "BH NTLMSSP query invalid");
794 return;
795 }
796
797 if (strlen(buf) > 3) {
798 if(strncmp(buf, "SF ", 3) == 0){
799 DEBUG(10, ("Setting flags to negotioate\n"));
800 TALLOC_FREE(state->want_feature_list);
801 state->want_feature_list = talloc_strdup(state->mem_ctx,
802 buf+3);
803 *response = talloc_strdup(mem_ctx, "OK");
804 return;
805 }
806 request = base64_decode_data_blob(buf + 3);
807 } else {
808 request = data_blob_null;
809 }
810
811 if ((strncmp(buf, "PW ", 3) == 0)) {
812 /* The calling application wants us to use a local password
813 * (rather than winbindd) */
814
815 opt_password = SMB_STRNDUP((const char *)request.data,
816 request.length);
817
818 if (opt_password == NULL) {
819 DEBUG(1, ("Out of memory\n"));
820 *response = talloc_strdup(mem_ctx, "BH Out of memory");
821 data_blob_free(&request);
822 return;
823 }
824
825 *response = talloc_strdup(mem_ctx, "OK");
826 data_blob_free(&request);
827 return;
828 }
829
830 if (strncmp(buf, "YR", 2) == 0) {
831 if (state->ntlmssp_state)
832 ntlmssp_end(&state->ntlmssp_state);
833 state->svr_state = SERVER_INITIAL;
834 } else if (strncmp(buf, "KK", 2) == 0) {
835 /* No special preprocessing required */
836 } else if (strncmp(buf, "GF", 2) == 0) {
837 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
838
839 if (state->svr_state == SERVER_FINISHED) {
840 *response = talloc_asprintf(mem_ctx, "GF 0x%08x",
841 state->neg_flags);
842 }
843 else {
844 *response = talloc_strdup(mem_ctx, "BH\n");
845 }
846 data_blob_free(&request);
847 return;
848 } else if (strncmp(buf, "GK", 2) == 0) {
849 DEBUG(10, ("Requested NTLMSSP session key\n"));
850 if(state->have_session_key) {
851 char *key64 = base64_encode_data_blob(state->mem_ctx,
852 state->session_key);
853 *response = talloc_asprintf(mem_ctx, "GK %s",
854 key64 ? key64 : "<NULL>");
855 TALLOC_FREE(key64);
856 } else {
857 *response = talloc_strdup(mem_ctx, "BH");
858 }
859
860 data_blob_free(&request);
861 return;
862 } else {
863 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
864 *response = talloc_strdup(mem_ctx, "BH NTLMSSP query invalid");
865 return;
866 }
867
868 if (!state->ntlmssp_state) {
869 nt_status = ntlm_auth_start_ntlmssp_server(
870 &state->ntlmssp_state);
871 if (!NT_STATUS_IS_OK(nt_status)) {
872 *response = talloc_asprintf(
873 mem_ctx, "BH %s", nt_errstr(nt_status));
874 return;
875 }
876 ntlmssp_want_feature_list(state->ntlmssp_state,
877 state->want_feature_list);
878 }
879
880 DEBUG(10, ("got NTLMSSP packet:\n"));
881 dump_data(10, request.data, request.length);
882
883 nt_status = ntlmssp_update(state->ntlmssp_state, request, &reply);
884
885 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
886 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
887 reply);
888 *response = talloc_asprintf(mem_ctx, "TT %s", reply_base64);
889 TALLOC_FREE(reply_base64);
890 data_blob_free(&reply);
891 state->svr_state = SERVER_CHALLENGE;
892 DEBUG(10, ("NTLMSSP challenge\n"));
893 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
894 *response = talloc_asprintf(mem_ctx, "BH %s",
895 nt_errstr(nt_status));
896 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
897
898 ntlmssp_end(&state->ntlmssp_state);
899 } else if (!NT_STATUS_IS_OK(nt_status)) {
900 *response = talloc_asprintf(mem_ctx, "NA %s",
901 nt_errstr(nt_status));
902 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
903 } else {
904 *response = talloc_asprintf(
905 mem_ctx, "AF %s",
906 (char *)state->ntlmssp_state->auth_context);
907 DEBUG(10, ("NTLMSSP OK!\n"));
908
909 if(state->have_session_key)
910 data_blob_free(&state->session_key);
911 state->session_key = data_blob(
912 state->ntlmssp_state->session_key.data,
913 state->ntlmssp_state->session_key.length);
914 state->neg_flags = state->ntlmssp_state->neg_flags;
915 state->have_session_key = true;
916 state->svr_state = SERVER_FINISHED;
917 }
918
919 data_blob_free(&request);
920}
921
922static void manage_squid_ntlmssp_request(struct ntlm_auth_state *state,
923 char *buf, int length)
924{
925 char *response;
926
927 manage_squid_ntlmssp_request_int(state, buf, length,
928 talloc_tos(), &response);
929
930 if (response == NULL) {
931 x_fprintf(x_stdout, "BH Out of memory\n");
932 return;
933 }
934 x_fprintf(x_stdout, "%s\n", response);
935 TALLOC_FREE(response);
936}
937
938static void manage_client_ntlmssp_request(struct ntlm_auth_state *state,
939 char *buf, int length)
940{
941 DATA_BLOB request, reply;
942 NTSTATUS nt_status;
943
944 if (!opt_username || !*opt_username) {
945 x_fprintf(x_stderr, "username must be specified!\n\n");
946 exit(1);
947 }
948
949 if (strlen(buf) < 2) {
950 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
951 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
952 return;
953 }
954
955 if (strlen(buf) > 3) {
956 if(strncmp(buf, "SF ", 3) == 0) {
957 DEBUG(10, ("Looking for flags to negotiate\n"));
958 talloc_free(state->want_feature_list);
959 state->want_feature_list = talloc_strdup(state->mem_ctx,
960 buf+3);
961 x_fprintf(x_stdout, "OK\n");
962 return;
963 }
964 request = base64_decode_data_blob(buf + 3);
965 } else {
966 request = data_blob_null;
967 }
968
969 if (strncmp(buf, "PW ", 3) == 0) {
970 /* We asked for a password and obviously got it :-) */
971
972 opt_password = SMB_STRNDUP((const char *)request.data,
973 request.length);
974
975 if (opt_password == NULL) {
976 DEBUG(1, ("Out of memory\n"));
977 x_fprintf(x_stdout, "BH Out of memory\n");
978 data_blob_free(&request);
979 return;
980 }
981
982 x_fprintf(x_stdout, "OK\n");
983 data_blob_free(&request);
984 return;
985 }
986
987 if (!state->ntlmssp_state && use_cached_creds) {
988 /* check whether cached credentials are usable. */
989 DATA_BLOB empty_blob = data_blob_null;
990
991 nt_status = do_ccache_ntlm_auth(empty_blob, empty_blob, NULL);
992 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
993 /* failed to use cached creds */
994 use_cached_creds = False;
995 }
996 }
997
998 if (opt_password == NULL && !use_cached_creds) {
999 /* Request a password from the calling process. After
1000 sending it, the calling process should retry asking for the
1001 negotiate. */
1002
1003 DEBUG(10, ("Requesting password\n"));
1004 x_fprintf(x_stdout, "PW\n");
1005 return;
1006 }
1007
1008 if (strncmp(buf, "YR", 2) == 0) {
1009 if (state->ntlmssp_state)
1010 ntlmssp_end(&state->ntlmssp_state);
1011 state->cli_state = CLIENT_INITIAL;
1012 } else if (strncmp(buf, "TT", 2) == 0) {
1013 /* No special preprocessing required */
1014 } else if (strncmp(buf, "GF", 2) == 0) {
1015 DEBUG(10, ("Requested negotiated NTLMSSP flags\n"));
1016
1017 if(state->cli_state == CLIENT_FINISHED) {
1018 x_fprintf(x_stdout, "GF 0x%08x\n", state->neg_flags);
1019 }
1020 else {
1021 x_fprintf(x_stdout, "BH\n");
1022 }
1023
1024 data_blob_free(&request);
1025 return;
1026 } else if (strncmp(buf, "GK", 2) == 0 ) {
1027 DEBUG(10, ("Requested session key\n"));
1028
1029 if(state->cli_state == CLIENT_FINISHED) {
1030 char *key64 = base64_encode_data_blob(state->mem_ctx,
1031 state->session_key);
1032 x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
1033 TALLOC_FREE(key64);
1034 }
1035 else {
1036 x_fprintf(x_stdout, "BH\n");
1037 }
1038
1039 data_blob_free(&request);
1040 return;
1041 } else {
1042 DEBUG(1, ("NTLMSSP query [%s] invalid\n", buf));
1043 x_fprintf(x_stdout, "BH NTLMSSP query invalid\n");
1044 return;
1045 }
1046
1047 if (!state->ntlmssp_state) {
1048 nt_status = ntlm_auth_start_ntlmssp_client(
1049 &state->ntlmssp_state);
1050 if (!NT_STATUS_IS_OK(nt_status)) {
1051 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1052 return;
1053 }
1054 ntlmssp_want_feature_list(state->ntlmssp_state,
1055 state->want_feature_list);
1056 state->initial_message = data_blob_null;
1057 }
1058
1059 DEBUG(10, ("got NTLMSSP packet:\n"));
1060 dump_data(10, request.data, request.length);
1061
1062 if (use_cached_creds && !opt_password &&
1063 (state->cli_state == CLIENT_RESPONSE)) {
1064 nt_status = do_ccache_ntlm_auth(state->initial_message, request,
1065 &reply);
1066 } else {
1067 nt_status = ntlmssp_update(state->ntlmssp_state, request,
1068 &reply);
1069 }
1070
1071 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1072 char *reply_base64 = base64_encode_data_blob(state->mem_ctx,
1073 reply);
1074 if (state->cli_state == CLIENT_INITIAL) {
1075 x_fprintf(x_stdout, "YR %s\n", reply_base64);
1076 state->initial_message = reply;
1077 state->cli_state = CLIENT_RESPONSE;
1078 } else {
1079 x_fprintf(x_stdout, "KK %s\n", reply_base64);
1080 data_blob_free(&reply);
1081 }
1082 TALLOC_FREE(reply_base64);
1083 DEBUG(10, ("NTLMSSP challenge\n"));
1084 } else if (NT_STATUS_IS_OK(nt_status)) {
1085 char *reply_base64 = base64_encode_data_blob(talloc_tos(),
1086 reply);
1087 x_fprintf(x_stdout, "AF %s\n", reply_base64);
1088 TALLOC_FREE(reply_base64);
1089
1090 if(state->have_session_key)
1091 data_blob_free(&state->session_key);
1092
1093 state->session_key = data_blob(
1094 state->ntlmssp_state->session_key.data,
1095 state->ntlmssp_state->session_key.length);
1096 state->neg_flags = state->ntlmssp_state->neg_flags;
1097 state->have_session_key = true;
1098
1099 DEBUG(10, ("NTLMSSP OK!\n"));
1100 state->cli_state = CLIENT_FINISHED;
1101 if (state->ntlmssp_state)
1102 ntlmssp_end(&state->ntlmssp_state);
1103 } else {
1104 x_fprintf(x_stdout, "BH %s\n", nt_errstr(nt_status));
1105 DEBUG(0, ("NTLMSSP BH: %s\n", nt_errstr(nt_status)));
1106 state->cli_state = CLIENT_ERROR;
1107 if (state->ntlmssp_state)
1108 ntlmssp_end(&state->ntlmssp_state);
1109 }
1110
1111 data_blob_free(&request);
1112}
1113
1114static void manage_squid_basic_request(struct ntlm_auth_state *state,
1115 char *buf, int length)
1116{
1117 char *user, *pass;
1118 user=buf;
1119
1120 pass=(char *)memchr(buf,' ',length);
1121 if (!pass) {
1122 DEBUG(2, ("Password not found. Denying access\n"));
1123 x_fprintf(x_stdout, "ERR\n");
1124 return;
1125 }
1126 *pass='\0';
1127 pass++;
1128
1129 if (state->helper_mode == SQUID_2_5_BASIC) {
1130 rfc1738_unescape(user);
1131 rfc1738_unescape(pass);
1132 }
1133
1134 if (check_plaintext_auth(user, pass, False)) {
1135 x_fprintf(x_stdout, "OK\n");
1136 } else {
1137 x_fprintf(x_stdout, "ERR\n");
1138 }
1139}
1140
1141static void offer_gss_spnego_mechs(void) {
1142
1143 DATA_BLOB token;
1144 struct spnego_data spnego;
1145 ssize_t len;
1146 char *reply_base64;
1147 TALLOC_CTX *ctx = talloc_tos();
1148 char *principal;
1149 char *myname_lower;
1150
1151 ZERO_STRUCT(spnego);
1152
1153 myname_lower = talloc_strdup(ctx, global_myname());
1154 if (!myname_lower) {
1155 return;
1156 }
1157 strlower_m(myname_lower);
1158
1159 principal = talloc_asprintf(ctx, "%s$@%s", myname_lower, lp_realm());
1160 if (!principal) {
1161 return;
1162 }
1163
1164 /* Server negTokenInit (mech offerings) */
1165 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1166 spnego.negTokenInit.mechTypes = talloc_array(ctx, const char *, 4);
1167#ifdef HAVE_KRB5
1168 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_KERBEROS5_OLD);
1169 spnego.negTokenInit.mechTypes[1] = talloc_strdup(ctx, OID_KERBEROS5);
1170 spnego.negTokenInit.mechTypes[2] = talloc_strdup(ctx, OID_NTLMSSP);
1171 spnego.negTokenInit.mechTypes[3] = NULL;
1172#else
1173 spnego.negTokenInit.mechTypes[0] = talloc_strdup(ctx, OID_NTLMSSP);
1174 spnego.negTokenInit.mechTypes[1] = NULL;
1175#endif
1176
1177
1178 spnego.negTokenInit.mechListMIC = data_blob_talloc(ctx, principal,
1179 strlen(principal));
1180
1181 len = spnego_write_data(ctx, &token, &spnego);
1182 spnego_free_data(&spnego);
1183
1184 if (len == -1) {
1185 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1186 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1187 return;
1188 }
1189
1190 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1191 x_fprintf(x_stdout, "TT %s *\n", reply_base64);
1192
1193 TALLOC_FREE(reply_base64);
1194 data_blob_free(&token);
1195 DEBUG(10, ("sent SPNEGO negTokenInit\n"));
1196 return;
1197}
1198
1199static bool _spnego_parse_krb5_wrap(TALLOC_CTX *ctx, DATA_BLOB blob, DATA_BLOB *ticket, uint8 tok_id[2])
1200{
1201 bool ret;
1202 ASN1_DATA *data;
1203 int data_remaining;
1204
1205 data = asn1_init(talloc_tos());
1206 if (data == NULL) {
1207 return false;
1208 }
1209
1210 asn1_load(data, blob);
1211 asn1_start_tag(data, ASN1_APPLICATION(0));
1212 asn1_check_OID(data, OID_KERBEROS5);
1213
1214 data_remaining = asn1_tag_remaining(data);
1215
1216 if (data_remaining < 3) {
1217 data->has_error = True;
1218 } else {
1219 asn1_read(data, tok_id, 2);
1220 data_remaining -= 2;
1221 *ticket = data_blob_talloc(ctx, NULL, data_remaining);
1222 asn1_read(data, ticket->data, ticket->length);
1223 }
1224
1225 asn1_end_tag(data);
1226
1227 ret = !data->has_error;
1228
1229 if (data->has_error) {
1230 data_blob_free(ticket);
1231 }
1232
1233 asn1_free(data);
1234
1235 return ret;
1236}
1237
1238static void manage_gss_spnego_request(struct ntlm_auth_state *state,
1239 char *buf, int length)
1240{
1241 struct spnego_data request, response;
1242 DATA_BLOB token;
1243 DATA_BLOB raw_in_token = data_blob_null;
1244 DATA_BLOB raw_out_token = data_blob_null;
1245 NTSTATUS status;
1246 ssize_t len;
1247 TALLOC_CTX *ctx = talloc_tos();
1248
1249 char *user = NULL;
1250 char *domain = NULL;
1251
1252 const char *reply_code;
1253 char *reply_base64;
1254 char *reply_argument = NULL;
1255 char *supportedMech = NULL;
1256
1257 if (strlen(buf) < 2) {
1258 DEBUG(1, ("SPENGO query [%s] invalid\n", buf));
1259 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1260 return;
1261 }
1262
1263 if (strncmp(buf, "YR", 2) == 0) {
1264 if (state->ntlmssp_state)
1265 ntlmssp_end(&state->ntlmssp_state);
1266 TALLOC_FREE(state->spnego_mech);
1267 TALLOC_FREE(state->spnego_mech_oid);
1268 } else if (strncmp(buf, "KK", 2) == 0) {
1269 ;
1270 } else {
1271 DEBUG(1, ("SPENGO query [%s] invalid\n", buf));
1272 x_fprintf(x_stdout, "BH SPENGO query invalid\n");
1273 return;
1274 }
1275
1276 if ( (strlen(buf) == 2)) {
1277
1278 /* no client data, get the negTokenInit offering
1279 mechanisms */
1280
1281 offer_gss_spnego_mechs();
1282 return;
1283 }
1284
1285 /* All subsequent requests have a blob. This might be negTokenInit or negTokenTarg */
1286
1287 if (strlen(buf) <= 3) {
1288 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1289 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1290 return;
1291 }
1292
1293 token = base64_decode_data_blob(buf + 3);
1294
1295 if ((token.length >= 7)
1296 && (strncmp((char *)token.data, "NTLMSSP", 7) == 0)) {
1297 char *reply;
1298
1299 data_blob_free(&token);
1300
1301 DEBUG(10, ("Could not parse GSS-SPNEGO, trying raw "
1302 "ntlmssp\n"));
1303
1304 manage_squid_ntlmssp_request_int(state, buf, length,
1305 talloc_tos(), &reply);
1306 if (reply == NULL) {
1307 x_fprintf(x_stdout, "BH Out of memory\n");
1308 return;
1309 }
1310
1311 if (strncmp(reply, "AF ", 3) == 0) {
1312 x_fprintf(x_stdout, "AF * %s\n", reply+3);
1313 } else {
1314 x_fprintf(x_stdout, "%s *\n", reply);
1315 }
1316
1317 TALLOC_FREE(reply);
1318 return;
1319 }
1320
1321 ZERO_STRUCT(request);
1322 len = spnego_read_data(ctx, token, &request);
1323 data_blob_free(&token);
1324
1325 if (len == -1) {
1326 DEBUG(1, ("GSS-SPNEGO query [%s] invalid\n", buf));
1327 x_fprintf(x_stdout, "BH GSS-SPNEGO query invalid\n");
1328 return;
1329 }
1330
1331 if (request.type == SPNEGO_NEG_TOKEN_INIT) {
1332#ifdef HAVE_KRB5
1333 int krb5_idx = -1;
1334#endif
1335 int ntlm_idx = -1;
1336 int used_idx = -1;
1337 int i;
1338
1339 if (state->spnego_mech) {
1340 DEBUG(1, ("Client restarted SPNEGO with NegTokenInit "
1341 "while mech[%s] was already negotiated\n",
1342 state->spnego_mech));
1343 x_fprintf(x_stdout, "BH Client send NegTokenInit twice\n");
1344 return;
1345 }
1346
1347 /* Second request from Client. This is where the
1348 client offers its mechanism to use. */
1349
1350 if ( (request.negTokenInit.mechTypes == NULL) ||
1351 (request.negTokenInit.mechTypes[0] == NULL) ) {
1352 DEBUG(1, ("Client did not offer any mechanism\n"));
1353 x_fprintf(x_stdout, "BH Client did not offer any "
1354 "mechanism\n");
1355 return;
1356 }
1357
1358 status = NT_STATUS_UNSUCCESSFUL;
1359 for (i = 0; request.negTokenInit.mechTypes[i] != NULL; i++) {
1360 DEBUG(10,("got mech[%d][%s]\n",
1361 i, request.negTokenInit.mechTypes[i]));
1362#ifdef HAVE_KRB5
1363 if (strcmp(request.negTokenInit.mechTypes[i], OID_KERBEROS5_OLD) == 0) {
1364 krb5_idx = i;
1365 break;
1366 }
1367 if (strcmp(request.negTokenInit.mechTypes[i], OID_KERBEROS5) == 0) {
1368 krb5_idx = i;
1369 break;
1370 }
1371#endif
1372 if (strcmp(request.negTokenInit.mechTypes[i], OID_NTLMSSP) == 0) {
1373 ntlm_idx = i;
1374 break;
1375 }
1376 }
1377
1378 used_idx = ntlm_idx;
1379#ifdef HAVE_KRB5
1380 if (krb5_idx != -1) {
1381 ntlm_idx = -1;
1382 used_idx = krb5_idx;
1383 }
1384#endif
1385 if (ntlm_idx > -1) {
1386 state->spnego_mech = talloc_strdup(state, "ntlmssp");
1387 if (state->spnego_mech == NULL) {
1388 x_fprintf(x_stdout, "BH Out of memory\n");
1389 return;
1390 }
1391
1392 if (state->ntlmssp_state) {
1393 DEBUG(1, ("Client wants a new NTLMSSP challenge, but "
1394 "already got one\n"));
1395 x_fprintf(x_stdout, "BH Client wants a new "
1396 "NTLMSSP challenge, but "
1397 "already got one\n");
1398 ntlmssp_end(&state->ntlmssp_state);
1399 return;
1400 }
1401
1402 status = ntlm_auth_start_ntlmssp_server(&state->ntlmssp_state);
1403 if (!NT_STATUS_IS_OK(status)) {
1404 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1405 return;
1406 }
1407 }
1408
1409#ifdef HAVE_KRB5
1410 if (krb5_idx > -1) {
1411 state->spnego_mech = talloc_strdup(state, "krb5");
1412 if (state->spnego_mech == NULL) {
1413 x_fprintf(x_stdout, "BH Out of memory\n");
1414 return;
1415 }
1416 }
1417#endif
1418 if (used_idx > -1) {
1419 state->spnego_mech_oid = talloc_strdup(state,
1420 request.negTokenInit.mechTypes[used_idx]);
1421 if (state->spnego_mech_oid == NULL) {
1422 x_fprintf(x_stdout, "BH Out of memory\n");
1423 return;
1424 }
1425 supportedMech = talloc_strdup(ctx, state->spnego_mech_oid);
1426 if (supportedMech == NULL) {
1427 x_fprintf(x_stdout, "BH Out of memory\n");
1428 return;
1429 }
1430
1431 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
1432 } else {
1433 status = NT_STATUS_NOT_SUPPORTED;
1434 }
1435 if (used_idx == 0) {
1436 status = NT_STATUS_OK;
1437 raw_in_token = request.negTokenInit.mechToken;
1438 }
1439 } else {
1440 if (state->spnego_mech == NULL) {
1441 DEBUG(1,("Got netTokenTarg without negTokenInit\n"));
1442 x_fprintf(x_stdout, "BH Got a negTokenTarg without "
1443 "negTokenInit\n");
1444 return;
1445 }
1446
1447 if ((request.negTokenTarg.supportedMech != NULL) &&
1448 (strcmp(request.negTokenTarg.supportedMech, state->spnego_mech_oid) != 0 ) ) {
1449 DEBUG(1, ("Got a negTokenTarg with mech[%s] while [%s] was already negotiated\n",
1450 request.negTokenTarg.supportedMech,
1451 state->spnego_mech_oid));
1452 x_fprintf(x_stdout, "BH Got a negTokenTarg with speficied mech\n");
1453 return;
1454 }
1455
1456 status = NT_STATUS_OK;
1457 raw_in_token = request.negTokenTarg.responseToken;
1458 }
1459
1460 if (!NT_STATUS_IS_OK(status)) {
1461 /* error or more processing */
1462 } else if (strcmp(state->spnego_mech, "ntlmssp") == 0) {
1463
1464 DEBUG(10, ("got NTLMSSP packet:\n"));
1465 dump_data(10, raw_in_token.data, raw_in_token.length);
1466
1467 status = ntlmssp_update(state->ntlmssp_state,
1468 raw_in_token,
1469 &raw_out_token);
1470 if (NT_STATUS_IS_OK(status)) {
1471 user = talloc_strdup(ctx, state->ntlmssp_state->user);
1472 domain = talloc_strdup(ctx, state->ntlmssp_state->domain);
1473 }
1474 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1475 ntlmssp_end(&state->ntlmssp_state);
1476 }
1477#ifdef HAVE_KRB5
1478 } else if (strcmp(state->spnego_mech, "krb5") == 0) {
1479 char *principal;
1480 DATA_BLOB ap_rep;
1481 DATA_BLOB session_key;
1482 struct PAC_DATA *pac_data = NULL;
1483 DATA_BLOB ticket;
1484 uint8_t tok_id[2];
1485
1486 if (!_spnego_parse_krb5_wrap(ctx, raw_in_token,
1487 &ticket, tok_id)) {
1488 DEBUG(1, ("spnego_parse_krb5_wrap failed\n"));
1489 x_fprintf(x_stdout, "BH spnego_parse_krb5_wrap failed\n");
1490 return;
1491 }
1492
1493 status = ads_verify_ticket(ctx, lp_realm(), 0,
1494 &ticket,
1495 &principal, &pac_data, &ap_rep,
1496 &session_key, True);
1497
1498 /* Now in "principal" we have the name we are authenticated as. */
1499
1500 if (NT_STATUS_IS_OK(status)) {
1501
1502 domain = strchr_m(principal, '@');
1503
1504 if (domain == NULL) {
1505 DEBUG(1, ("Did not get a valid principal "
1506 "from ads_verify_ticket\n"));
1507 x_fprintf(x_stdout, "BH Did not get a "
1508 "valid principal from "
1509 "ads_verify_ticket\n");
1510 return;
1511 }
1512
1513 *domain++ = '\0';
1514 domain = talloc_strdup(ctx, domain);
1515 user = talloc_strdup(ctx, principal);
1516
1517 if (pac_data) {
1518 struct PAC_LOGON_INFO *logon_info;
1519 logon_info = get_logon_info_from_pac(
1520 pac_data);
1521 if (logon_info) {
1522 netsamlogon_cache_store(
1523 user,
1524 &logon_info->info3);
1525 }
1526 }
1527
1528 data_blob_free(&ap_rep);
1529 data_blob_free(&session_key);
1530 }
1531 data_blob_free(&ticket);
1532#endif
1533 }
1534
1535 spnego_free_data(&request);
1536 ZERO_STRUCT(response);
1537 response.type = SPNEGO_NEG_TOKEN_TARG;
1538
1539 if (NT_STATUS_IS_OK(status)) {
1540 TALLOC_FREE(state->spnego_mech);
1541 TALLOC_FREE(state->spnego_mech_oid);
1542 response.negTokenTarg.negResult = SPNEGO_ACCEPT_COMPLETED;
1543 response.negTokenTarg.responseToken = raw_out_token;
1544 reply_code = "AF";
1545 reply_argument = talloc_asprintf(ctx, "%s\\%s", domain, user);
1546 } else if (NT_STATUS_EQUAL(status,
1547 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1548 response.negTokenTarg.supportedMech = supportedMech;
1549 response.negTokenTarg.responseToken = raw_out_token;
1550 response.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1551 reply_code = "TT";
1552 reply_argument = talloc_strdup(ctx, "*");
1553 } else {
1554 TALLOC_FREE(state->spnego_mech);
1555 TALLOC_FREE(state->spnego_mech_oid);
1556 data_blob_free(&raw_out_token);
1557 response.negTokenTarg.negResult = SPNEGO_REJECT;
1558 reply_code = "NA";
1559 reply_argument = talloc_strdup(ctx, nt_errstr(status));
1560 }
1561
1562 if (!reply_argument) {
1563 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1564 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1565 spnego_free_data(&response);
1566 return;
1567 }
1568
1569 len = spnego_write_data(ctx, &token, &response);
1570 spnego_free_data(&response);
1571
1572 if (len == -1) {
1573 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1574 x_fprintf(x_stdout, "BH Could not write SPNEGO data blob\n");
1575 return;
1576 }
1577
1578 reply_base64 = base64_encode_data_blob(talloc_tos(), token);
1579
1580 x_fprintf(x_stdout, "%s %s %s\n",
1581 reply_code, reply_base64, reply_argument);
1582
1583 TALLOC_FREE(reply_base64);
1584 data_blob_free(&token);
1585
1586 return;
1587}
1588
1589static NTLMSSP_STATE *client_ntlmssp_state = NULL;
1590
1591static bool manage_client_ntlmssp_init(struct spnego_data spnego)
1592{
1593 NTSTATUS status;
1594 DATA_BLOB null_blob = data_blob_null;
1595 DATA_BLOB to_server;
1596 char *to_server_base64;
1597 const char *my_mechs[] = {OID_NTLMSSP, NULL};
1598 TALLOC_CTX *ctx = talloc_tos();
1599
1600 DEBUG(10, ("Got spnego negTokenInit with NTLMSSP\n"));
1601
1602 if (client_ntlmssp_state != NULL) {
1603 DEBUG(1, ("Request for initial SPNEGO request where "
1604 "we already have a state\n"));
1605 return False;
1606 }
1607
1608 if (!client_ntlmssp_state) {
1609 if (!NT_STATUS_IS_OK(status = ntlm_auth_start_ntlmssp_client(&client_ntlmssp_state))) {
1610 x_fprintf(x_stdout, "BH %s\n", nt_errstr(status));
1611 return False;
1612 }
1613 }
1614
1615
1616 if (opt_password == NULL) {
1617
1618 /* Request a password from the calling process. After
1619 sending it, the calling process should retry with
1620 the negTokenInit. */
1621
1622 DEBUG(10, ("Requesting password\n"));
1623 x_fprintf(x_stdout, "PW\n");
1624 return True;
1625 }
1626
1627 spnego.type = SPNEGO_NEG_TOKEN_INIT;
1628 spnego.negTokenInit.mechTypes = my_mechs;
1629 spnego.negTokenInit.reqFlags = data_blob_null;
1630 spnego.negTokenInit.reqFlagsPadding = 0;
1631 spnego.negTokenInit.mechListMIC = null_blob;
1632
1633 status = ntlmssp_update(client_ntlmssp_state, null_blob,
1634 &spnego.negTokenInit.mechToken);
1635
1636 if ( !(NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) ||
1637 NT_STATUS_IS_OK(status)) ) {
1638 DEBUG(1, ("Expected OK or MORE_PROCESSING_REQUIRED, got: %s\n",
1639 nt_errstr(status)));
1640 ntlmssp_end(&client_ntlmssp_state);
1641 return False;
1642 }
1643
1644 spnego_write_data(ctx, &to_server, &spnego);
1645 data_blob_free(&spnego.negTokenInit.mechToken);
1646
1647 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1648 data_blob_free(&to_server);
1649 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1650 TALLOC_FREE(to_server_base64);
1651 return True;
1652}
1653
1654static void manage_client_ntlmssp_targ(struct spnego_data spnego)
1655{
1656 NTSTATUS status;
1657 DATA_BLOB null_blob = data_blob_null;
1658 DATA_BLOB request;
1659 DATA_BLOB to_server;
1660 char *to_server_base64;
1661 TALLOC_CTX *ctx = talloc_tos();
1662
1663 DEBUG(10, ("Got spnego negTokenTarg with NTLMSSP\n"));
1664
1665 if (client_ntlmssp_state == NULL) {
1666 DEBUG(1, ("Got NTLMSSP tArg without a client state\n"));
1667 x_fprintf(x_stdout, "BH Got NTLMSSP tArg without a client state\n");
1668 return;
1669 }
1670
1671 if (spnego.negTokenTarg.negResult == SPNEGO_REJECT) {
1672 x_fprintf(x_stdout, "NA\n");
1673 ntlmssp_end(&client_ntlmssp_state);
1674 return;
1675 }
1676
1677 if (spnego.negTokenTarg.negResult == SPNEGO_ACCEPT_COMPLETED) {
1678 x_fprintf(x_stdout, "AF\n");
1679 ntlmssp_end(&client_ntlmssp_state);
1680 return;
1681 }
1682
1683 status = ntlmssp_update(client_ntlmssp_state,
1684 spnego.negTokenTarg.responseToken,
1685 &request);
1686
1687 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1688 DEBUG(1, ("Expected MORE_PROCESSING_REQUIRED from "
1689 "ntlmssp_client_update, got: %s\n",
1690 nt_errstr(status)));
1691 x_fprintf(x_stdout, "BH Expected MORE_PROCESSING_REQUIRED from "
1692 "ntlmssp_client_update\n");
1693 data_blob_free(&request);
1694 ntlmssp_end(&client_ntlmssp_state);
1695 return;
1696 }
1697
1698 spnego.type = SPNEGO_NEG_TOKEN_TARG;
1699 spnego.negTokenTarg.negResult = SPNEGO_ACCEPT_INCOMPLETE;
1700 spnego.negTokenTarg.supportedMech = (char *)OID_NTLMSSP;
1701 spnego.negTokenTarg.responseToken = request;
1702 spnego.negTokenTarg.mechListMIC = null_blob;
1703
1704 spnego_write_data(ctx, &to_server, &spnego);
1705 data_blob_free(&request);
1706
1707 to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1708 data_blob_free(&to_server);
1709 x_fprintf(x_stdout, "KK %s\n", to_server_base64);
1710 TALLOC_FREE(to_server_base64);
1711 return;
1712}
1713
1714#ifdef HAVE_KRB5
1715
1716static bool manage_client_krb5_init(struct spnego_data spnego)
1717{
1718 char *principal;
1719 DATA_BLOB tkt, to_server;
1720 DATA_BLOB session_key_krb5 = data_blob_null;
1721 struct spnego_data reply;
1722 char *reply_base64;
1723 int retval;
1724
1725 const char *my_mechs[] = {OID_KERBEROS5_OLD, NULL};
1726 ssize_t len;
1727 TALLOC_CTX *ctx = talloc_tos();
1728
1729 if ( (spnego.negTokenInit.mechListMIC.data == NULL) ||
1730 (spnego.negTokenInit.mechListMIC.length == 0) ) {
1731 DEBUG(1, ("Did not get a principal for krb5\n"));
1732 return False;
1733 }
1734
1735 principal = (char *)SMB_MALLOC(
1736 spnego.negTokenInit.mechListMIC.length+1);
1737
1738 if (principal == NULL) {
1739 DEBUG(1, ("Could not malloc principal\n"));
1740 return False;
1741 }
1742
1743 memcpy(principal, spnego.negTokenInit.mechListMIC.data,
1744 spnego.negTokenInit.mechListMIC.length);
1745 principal[spnego.negTokenInit.mechListMIC.length] = '\0';
1746
1747 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1748
1749 if (retval) {
1750 char *user = NULL;
1751
1752 /* Let's try to first get the TGT, for that we need a
1753 password. */
1754
1755 if (opt_password == NULL) {
1756 DEBUG(10, ("Requesting password\n"));
1757 x_fprintf(x_stdout, "PW\n");
1758 return True;
1759 }
1760
1761 user = talloc_asprintf(talloc_tos(), "%s@%s", opt_username, opt_domain);
1762 if (!user) {
1763 return false;
1764 }
1765
1766 if ((retval = kerberos_kinit_password(user, opt_password, 0, NULL))) {
1767 DEBUG(10, ("Requesting TGT failed: %s\n", error_message(retval)));
1768 return False;
1769 }
1770
1771 retval = cli_krb5_get_ticket(principal, 0, &tkt, &session_key_krb5, 0, NULL, NULL, NULL);
1772
1773 if (retval) {
1774 DEBUG(10, ("Kinit suceeded, but getting a ticket failed: %s\n", error_message(retval)));
1775 return False;
1776 }
1777 }
1778
1779 data_blob_free(&session_key_krb5);
1780
1781 ZERO_STRUCT(reply);
1782
1783 reply.type = SPNEGO_NEG_TOKEN_INIT;
1784 reply.negTokenInit.mechTypes = my_mechs;
1785 reply.negTokenInit.reqFlags = data_blob_null;
1786 reply.negTokenInit.reqFlagsPadding = 0;
1787 reply.negTokenInit.mechToken = tkt;
1788 reply.negTokenInit.mechListMIC = data_blob_null;
1789
1790 len = spnego_write_data(ctx, &to_server, &reply);
1791 data_blob_free(&tkt);
1792
1793 if (len == -1) {
1794 DEBUG(1, ("Could not write SPNEGO data blob\n"));
1795 return False;
1796 }
1797
1798 reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
1799 x_fprintf(x_stdout, "KK %s *\n", reply_base64);
1800
1801 TALLOC_FREE(reply_base64);
1802 data_blob_free(&to_server);
1803 DEBUG(10, ("sent GSS-SPNEGO KERBEROS5 negTokenInit\n"));
1804 return True;
1805}
1806
1807static void manage_client_krb5_targ(struct spnego_data spnego)
1808{
1809 switch (spnego.negTokenTarg.negResult) {
1810 case SPNEGO_ACCEPT_INCOMPLETE:
1811 DEBUG(1, ("Got a Kerberos negTokenTarg with ACCEPT_INCOMPLETE\n"));
1812 x_fprintf(x_stdout, "BH Got a Kerberos negTokenTarg with "
1813 "ACCEPT_INCOMPLETE\n");
1814 break;
1815 case SPNEGO_ACCEPT_COMPLETED:
1816 DEBUG(10, ("Accept completed\n"));
1817 x_fprintf(x_stdout, "AF\n");
1818 break;
1819 case SPNEGO_REJECT:
1820 DEBUG(10, ("Rejected\n"));
1821 x_fprintf(x_stdout, "NA\n");
1822 break;
1823 default:
1824 DEBUG(1, ("Got an invalid negTokenTarg\n"));
1825 x_fprintf(x_stdout, "AF\n");
1826 }
1827}
1828
1829#endif
1830
1831static void manage_gss_spnego_client_request(struct ntlm_auth_state *state,
1832 char *buf, int length)
1833{
1834 DATA_BLOB request;
1835 struct spnego_data spnego;
1836 ssize_t len;
1837 TALLOC_CTX *ctx = talloc_tos();
1838
1839 if (!opt_username || !*opt_username) {
1840 x_fprintf(x_stderr, "username must be specified!\n\n");
1841 exit(1);
1842 }
1843
1844 if (strlen(buf) <= 3) {
1845 DEBUG(1, ("SPNEGO query [%s] too short\n", buf));
1846 x_fprintf(x_stdout, "BH SPNEGO query too short\n");
1847 return;
1848 }
1849
1850 request = base64_decode_data_blob(buf+3);
1851
1852 if (strncmp(buf, "PW ", 3) == 0) {
1853
1854 /* We asked for a password and obviously got it :-) */
1855
1856 opt_password = SMB_STRNDUP((const char *)request.data, request.length);
1857
1858 if (opt_password == NULL) {
1859 DEBUG(1, ("Out of memory\n"));
1860 x_fprintf(x_stdout, "BH Out of memory\n");
1861 data_blob_free(&request);
1862 return;
1863 }
1864
1865 x_fprintf(x_stdout, "OK\n");
1866 data_blob_free(&request);
1867 return;
1868 }
1869
1870 if ( (strncmp(buf, "TT ", 3) != 0) &&
1871 (strncmp(buf, "AF ", 3) != 0) &&
1872 (strncmp(buf, "NA ", 3) != 0) ) {
1873 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
1874 x_fprintf(x_stdout, "BH SPNEGO request invalid\n");
1875 data_blob_free(&request);
1876 return;
1877 }
1878
1879 /* So we got a server challenge to generate a SPNEGO
1880 client-to-server request... */
1881
1882 len = spnego_read_data(ctx, request, &spnego);
1883 data_blob_free(&request);
1884
1885 if (len == -1) {
1886 DEBUG(1, ("Could not read SPNEGO data for [%s]\n", buf));
1887 x_fprintf(x_stdout, "BH Could not read SPNEGO data\n");
1888 return;
1889 }
1890
1891 if (spnego.type == SPNEGO_NEG_TOKEN_INIT) {
1892
1893 /* The server offers a list of mechanisms */
1894
1895 const char **mechType = (const char **)spnego.negTokenInit.mechTypes;
1896
1897 while (*mechType != NULL) {
1898
1899#ifdef HAVE_KRB5
1900 if ( (strcmp(*mechType, OID_KERBEROS5_OLD) == 0) ||
1901 (strcmp(*mechType, OID_KERBEROS5) == 0) ) {
1902 if (manage_client_krb5_init(spnego))
1903 goto out;
1904 }
1905#endif
1906
1907 if (strcmp(*mechType, OID_NTLMSSP) == 0) {
1908 if (manage_client_ntlmssp_init(spnego))
1909 goto out;
1910 }
1911
1912 mechType++;
1913 }
1914
1915 DEBUG(1, ("Server offered no compatible mechanism\n"));
1916 x_fprintf(x_stdout, "BH Server offered no compatible mechanism\n");
1917 return;
1918 }
1919
1920 if (spnego.type == SPNEGO_NEG_TOKEN_TARG) {
1921
1922 if (spnego.negTokenTarg.supportedMech == NULL) {
1923 /* On accept/reject Windows does not send the
1924 mechanism anymore. Handle that here and
1925 shut down the mechanisms. */
1926
1927 switch (spnego.negTokenTarg.negResult) {
1928 case SPNEGO_ACCEPT_COMPLETED:
1929 x_fprintf(x_stdout, "AF\n");
1930 break;
1931 case SPNEGO_REJECT:
1932 x_fprintf(x_stdout, "NA\n");
1933 break;
1934 default:
1935 DEBUG(1, ("Got a negTokenTarg with no mech and an "
1936 "unknown negResult: %d\n",
1937 spnego.negTokenTarg.negResult));
1938 x_fprintf(x_stdout, "BH Got a negTokenTarg with"
1939 " no mech and an unknown "
1940 "negResult\n");
1941 }
1942
1943 ntlmssp_end(&client_ntlmssp_state);
1944 goto out;
1945 }
1946
1947 if (strcmp(spnego.negTokenTarg.supportedMech,
1948 OID_NTLMSSP) == 0) {
1949 manage_client_ntlmssp_targ(spnego);
1950 goto out;
1951 }
1952
1953#if HAVE_KRB5
1954 if (strcmp(spnego.negTokenTarg.supportedMech,
1955 OID_KERBEROS5_OLD) == 0) {
1956 manage_client_krb5_targ(spnego);
1957 goto out;
1958 }
1959#endif
1960
1961 }
1962
1963 DEBUG(1, ("Got an SPNEGO token I could not handle [%s]!\n", buf));
1964 x_fprintf(x_stdout, "BH Got an SPNEGO token I could not handle\n");
1965 return;
1966
1967 out:
1968 spnego_free_data(&spnego);
1969 return;
1970}
1971
1972static void manage_ntlm_server_1_request(struct ntlm_auth_state *state,
1973 char *buf, int length)
1974{
1975 char *request, *parameter;
1976 static DATA_BLOB challenge;
1977 static DATA_BLOB lm_response;
1978 static DATA_BLOB nt_response;
1979 static char *full_username;
1980 static char *username;
1981 static char *domain;
1982 static char *plaintext_password;
1983 static bool ntlm_server_1_user_session_key;
1984 static bool ntlm_server_1_lm_session_key;
1985
1986 if (strequal(buf, ".")) {
1987 if (!full_username && !username) {
1988 x_fprintf(x_stdout, "Error: No username supplied!\n");
1989 } else if (plaintext_password) {
1990 /* handle this request as plaintext */
1991 if (!full_username) {
1992 if (asprintf(&full_username, "%s%c%s", domain, winbind_separator(), username) == -1) {
1993 x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
1994 return;
1995 }
1996 }
1997 if (check_plaintext_auth(full_username, plaintext_password, False)) {
1998 x_fprintf(x_stdout, "Authenticated: Yes\n");
1999 } else {
2000 x_fprintf(x_stdout, "Authenticated: No\n");
2001 }
2002 } else if (!lm_response.data && !nt_response.data) {
2003 x_fprintf(x_stdout, "Error: No password supplied!\n");
2004 } else if (!challenge.data) {
2005 x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
2006 } else {
2007 char *error_string = NULL;
2008 uchar lm_key[8];
2009 uchar user_session_key[16];
2010 uint32 flags = 0;
2011
2012 if (full_username && !username) {
2013 fstring fstr_user;
2014 fstring fstr_domain;
2015
2016 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
2017 /* username might be 'tainted', don't print into our new-line deleimianted stream */
2018 x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
2019 }
2020 SAFE_FREE(username);
2021 SAFE_FREE(domain);
2022 username = smb_xstrdup(fstr_user);
2023 domain = smb_xstrdup(fstr_domain);
2024 }
2025
2026 if (!domain) {
2027 domain = smb_xstrdup(get_winbind_domain());
2028 }
2029
2030 if (ntlm_server_1_lm_session_key)
2031 flags |= WBFLAG_PAM_LMKEY;
2032
2033 if (ntlm_server_1_user_session_key)
2034 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2035
2036 if (!NT_STATUS_IS_OK(
2037 contact_winbind_auth_crap(username,
2038 domain,
2039 global_myname(),
2040 &challenge,
2041 &lm_response,
2042 &nt_response,
2043 flags,
2044 lm_key,
2045 user_session_key,
2046 &error_string,
2047 NULL))) {
2048
2049 x_fprintf(x_stdout, "Authenticated: No\n");
2050 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
2051 } else {
2052 static char zeros[16];
2053 char *hex_lm_key;
2054 char *hex_user_session_key;
2055
2056 x_fprintf(x_stdout, "Authenticated: Yes\n");
2057
2058 if (ntlm_server_1_lm_session_key
2059 && (memcmp(zeros, lm_key,
2060 sizeof(lm_key)) != 0)) {
2061 hex_lm_key = hex_encode_talloc(NULL,
2062 (const unsigned char *)lm_key,
2063 sizeof(lm_key));
2064 x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
2065 TALLOC_FREE(hex_lm_key);
2066 }
2067
2068 if (ntlm_server_1_user_session_key
2069 && (memcmp(zeros, user_session_key,
2070 sizeof(user_session_key)) != 0)) {
2071 hex_user_session_key = hex_encode_talloc(NULL,
2072 (const unsigned char *)user_session_key,
2073 sizeof(user_session_key));
2074 x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
2075 TALLOC_FREE(hex_user_session_key);
2076 }
2077 }
2078 SAFE_FREE(error_string);
2079 }
2080 /* clear out the state */
2081 challenge = data_blob_null;
2082 nt_response = data_blob_null;
2083 lm_response = data_blob_null;
2084 SAFE_FREE(full_username);
2085 SAFE_FREE(username);
2086 SAFE_FREE(domain);
2087 SAFE_FREE(plaintext_password);
2088 ntlm_server_1_user_session_key = False;
2089 ntlm_server_1_lm_session_key = False;
2090 x_fprintf(x_stdout, ".\n");
2091
2092 return;
2093 }
2094
2095 request = buf;
2096
2097 /* Indicates a base64 encoded structure */
2098 parameter = strstr_m(request, ":: ");
2099 if (!parameter) {
2100 parameter = strstr_m(request, ": ");
2101
2102 if (!parameter) {
2103 DEBUG(0, ("Parameter not found!\n"));
2104 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2105 return;
2106 }
2107
2108 parameter[0] ='\0';
2109 parameter++;
2110 parameter[0] ='\0';
2111 parameter++;
2112
2113 } else {
2114 parameter[0] ='\0';
2115 parameter++;
2116 parameter[0] ='\0';
2117 parameter++;
2118 parameter[0] ='\0';
2119 parameter++;
2120
2121 base64_decode_inplace(parameter);
2122 }
2123
2124 if (strequal(request, "LANMAN-Challenge")) {
2125 challenge = strhex_to_data_blob(NULL, parameter);
2126 if (challenge.length != 8) {
2127 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n",
2128 parameter,
2129 (int)challenge.length);
2130 challenge = data_blob_null;
2131 }
2132 } else if (strequal(request, "NT-Response")) {
2133 nt_response = strhex_to_data_blob(NULL, parameter);
2134 if (nt_response.length < 24) {
2135 x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n",
2136 parameter,
2137 (int)nt_response.length);
2138 nt_response = data_blob_null;
2139 }
2140 } else if (strequal(request, "LANMAN-Response")) {
2141 lm_response = strhex_to_data_blob(NULL, parameter);
2142 if (lm_response.length != 24) {
2143 x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n",
2144 parameter,
2145 (int)lm_response.length);
2146 lm_response = data_blob_null;
2147 }
2148 } else if (strequal(request, "Password")) {
2149 plaintext_password = smb_xstrdup(parameter);
2150 } else if (strequal(request, "NT-Domain")) {
2151 domain = smb_xstrdup(parameter);
2152 } else if (strequal(request, "Username")) {
2153 username = smb_xstrdup(parameter);
2154 } else if (strequal(request, "Full-Username")) {
2155 full_username = smb_xstrdup(parameter);
2156 } else if (strequal(request, "Request-User-Session-Key")) {
2157 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
2158 } else if (strequal(request, "Request-LanMan-Session-Key")) {
2159 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
2160 } else {
2161 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2162 }
2163}
2164
2165static void manage_ntlm_change_password_1_request(struct ntlm_auth_state *state,
2166 char *buf, int length)
2167{
2168 char *request, *parameter;
2169 static DATA_BLOB new_nt_pswd;
2170 static DATA_BLOB old_nt_hash_enc;
2171 static DATA_BLOB new_lm_pswd;
2172 static DATA_BLOB old_lm_hash_enc;
2173 static char *full_username = NULL;
2174 static char *username = NULL;
2175 static char *domain = NULL;
2176 static char *newpswd = NULL;
2177 static char *oldpswd = NULL;
2178
2179 if (strequal(buf, ".")) {
2180 if(newpswd && oldpswd) {
2181 uchar old_nt_hash[16];
2182 uchar old_lm_hash[16];
2183 uchar new_nt_hash[16];
2184 uchar new_lm_hash[16];
2185
2186 new_nt_pswd = data_blob(NULL, 516);
2187 old_nt_hash_enc = data_blob(NULL, 16);
2188
2189 /* Calculate the MD4 hash (NT compatible) of the
2190 * password */
2191 E_md4hash(oldpswd, old_nt_hash);
2192 E_md4hash(newpswd, new_nt_hash);
2193
2194 /* E_deshash returns false for 'long'
2195 passwords (> 14 DOS chars).
2196
2197 Therefore, don't send a buffer
2198 encrypted with the truncated hash
2199 (it could allow an even easier
2200 attack on the password)
2201
2202 Likewise, obey the admin's restriction
2203 */
2204
2205 if (lp_client_lanman_auth() &&
2206 E_deshash(newpswd, new_lm_hash) &&
2207 E_deshash(oldpswd, old_lm_hash)) {
2208 new_lm_pswd = data_blob(NULL, 516);
2209 old_lm_hash_enc = data_blob(NULL, 16);
2210 encode_pw_buffer(new_lm_pswd.data, newpswd,
2211 STR_UNICODE);
2212
2213 arcfour_crypt(new_lm_pswd.data, old_nt_hash, 516);
2214 E_old_pw_hash(new_nt_hash, old_lm_hash,
2215 old_lm_hash_enc.data);
2216 } else {
2217 new_lm_pswd.data = NULL;
2218 new_lm_pswd.length = 0;
2219 old_lm_hash_enc.data = NULL;
2220 old_lm_hash_enc.length = 0;
2221 }
2222
2223 encode_pw_buffer(new_nt_pswd.data, newpswd,
2224 STR_UNICODE);
2225
2226 arcfour_crypt(new_nt_pswd.data, old_nt_hash, 516);
2227 E_old_pw_hash(new_nt_hash, old_nt_hash,
2228 old_nt_hash_enc.data);
2229 }
2230
2231 if (!full_username && !username) {
2232 x_fprintf(x_stdout, "Error: No username supplied!\n");
2233 } else if ((!new_nt_pswd.data || !old_nt_hash_enc.data) &&
2234 (!new_lm_pswd.data || old_lm_hash_enc.data) ) {
2235 x_fprintf(x_stdout, "Error: No NT or LM password "
2236 "blobs supplied!\n");
2237 } else {
2238 char *error_string = NULL;
2239
2240 if (full_username && !username) {
2241 fstring fstr_user;
2242 fstring fstr_domain;
2243
2244 if (!parse_ntlm_auth_domain_user(full_username,
2245 fstr_user,
2246 fstr_domain)) {
2247 /* username might be 'tainted', don't
2248 * print into our new-line
2249 * deleimianted stream */
2250 x_fprintf(x_stdout, "Error: Could not "
2251 "parse into domain and "
2252 "username\n");
2253 SAFE_FREE(username);
2254 username = smb_xstrdup(full_username);
2255 } else {
2256 SAFE_FREE(username);
2257 SAFE_FREE(domain);
2258 username = smb_xstrdup(fstr_user);
2259 domain = smb_xstrdup(fstr_domain);
2260 }
2261
2262 }
2263
2264 if(!NT_STATUS_IS_OK(contact_winbind_change_pswd_auth_crap(
2265 username, domain,
2266 new_nt_pswd,
2267 old_nt_hash_enc,
2268 new_lm_pswd,
2269 old_lm_hash_enc,
2270 &error_string))) {
2271 x_fprintf(x_stdout, "Password-Change: No\n");
2272 x_fprintf(x_stdout, "Password-Change-Error: "
2273 "%s\n.\n", error_string);
2274 } else {
2275 x_fprintf(x_stdout, "Password-Change: Yes\n");
2276 }
2277
2278 SAFE_FREE(error_string);
2279 }
2280 /* clear out the state */
2281 new_nt_pswd = data_blob_null;
2282 old_nt_hash_enc = data_blob_null;
2283 new_lm_pswd = data_blob_null;
2284 old_nt_hash_enc = data_blob_null;
2285 SAFE_FREE(full_username);
2286 SAFE_FREE(username);
2287 SAFE_FREE(domain);
2288 SAFE_FREE(newpswd);
2289 SAFE_FREE(oldpswd);
2290 x_fprintf(x_stdout, ".\n");
2291
2292 return;
2293 }
2294
2295 request = buf;
2296
2297 /* Indicates a base64 encoded structure */
2298 parameter = strstr_m(request, ":: ");
2299 if (!parameter) {
2300 parameter = strstr_m(request, ": ");
2301
2302 if (!parameter) {
2303 DEBUG(0, ("Parameter not found!\n"));
2304 x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
2305 return;
2306 }
2307
2308 parameter[0] ='\0';
2309 parameter++;
2310 parameter[0] ='\0';
2311 parameter++;
2312 } else {
2313 parameter[0] ='\0';
2314 parameter++;
2315 parameter[0] ='\0';
2316 parameter++;
2317 parameter[0] ='\0';
2318 parameter++;
2319
2320 base64_decode_inplace(parameter);
2321 }
2322
2323 if (strequal(request, "new-nt-password-blob")) {
2324 new_nt_pswd = strhex_to_data_blob(NULL, parameter);
2325 if (new_nt_pswd.length != 516) {
2326 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2327 "(got %d bytes, expected 516)\n.\n",
2328 parameter,
2329 (int)new_nt_pswd.length);
2330 new_nt_pswd = data_blob_null;
2331 }
2332 } else if (strequal(request, "old-nt-hash-blob")) {
2333 old_nt_hash_enc = strhex_to_data_blob(NULL, parameter);
2334 if (old_nt_hash_enc.length != 16) {
2335 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2336 "(got %d bytes, expected 16)\n.\n",
2337 parameter,
2338 (int)old_nt_hash_enc.length);
2339 old_nt_hash_enc = data_blob_null;
2340 }
2341 } else if (strequal(request, "new-lm-password-blob")) {
2342 new_lm_pswd = strhex_to_data_blob(NULL, parameter);
2343 if (new_lm_pswd.length != 516) {
2344 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2345 "(got %d bytes, expected 516)\n.\n",
2346 parameter,
2347 (int)new_lm_pswd.length);
2348 new_lm_pswd = data_blob_null;
2349 }
2350 }
2351 else if (strequal(request, "old-lm-hash-blob")) {
2352 old_lm_hash_enc = strhex_to_data_blob(NULL, parameter);
2353 if (old_lm_hash_enc.length != 16)
2354 {
2355 x_fprintf(x_stdout, "Error: hex decode of %s failed! "
2356 "(got %d bytes, expected 16)\n.\n",
2357 parameter,
2358 (int)old_lm_hash_enc.length);
2359 old_lm_hash_enc = data_blob_null;
2360 }
2361 } else if (strequal(request, "nt-domain")) {
2362 domain = smb_xstrdup(parameter);
2363 } else if(strequal(request, "username")) {
2364 username = smb_xstrdup(parameter);
2365 } else if(strequal(request, "full-username")) {
2366 username = smb_xstrdup(parameter);
2367 } else if(strequal(request, "new-password")) {
2368 newpswd = smb_xstrdup(parameter);
2369 } else if (strequal(request, "old-password")) {
2370 oldpswd = smb_xstrdup(parameter);
2371 } else {
2372 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
2373 }
2374}
2375
2376static void manage_squid_request(struct ntlm_auth_state *state,
2377 stdio_helper_function fn)
2378{
2379 char *buf;
2380 char tmp[INITIAL_BUFFER_SIZE+1];
2381 int length, buf_size = 0;
2382 char *c;
2383
2384 buf = talloc_strdup(state->mem_ctx, "");
2385 if (!buf) {
2386 DEBUG(0, ("Failed to allocate input buffer.\n"));
2387 x_fprintf(x_stderr, "ERR\n");
2388 exit(1);
2389 }
2390
2391 do {
2392
2393 /* this is not a typo - x_fgets doesn't work too well under
2394 * squid */
2395 if (fgets(tmp, sizeof(tmp)-1, stdin) == NULL) {
2396 if (ferror(stdin)) {
2397 DEBUG(1, ("fgets() failed! dying..... errno=%d "
2398 "(%s)\n", ferror(stdin),
2399 strerror(ferror(stdin))));
2400
2401 exit(1);
2402 }
2403 exit(0);
2404 }
2405
2406 buf = talloc_strdup_append_buffer(buf, tmp);
2407 buf_size += INITIAL_BUFFER_SIZE;
2408
2409 if (buf_size > MAX_BUFFER_SIZE) {
2410 DEBUG(2, ("Oversized message\n"));
2411 x_fprintf(x_stderr, "ERR\n");
2412 talloc_free(buf);
2413 return;
2414 }
2415
2416 c = strchr(buf, '\n');
2417 } while (c == NULL);
2418
2419 *c = '\0';
2420 length = c-buf;
2421
2422 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
2423
2424 if (buf[0] == '\0') {
2425 DEBUG(2, ("Invalid Request\n"));
2426 x_fprintf(x_stderr, "ERR\n");
2427 talloc_free(buf);
2428 return;
2429 }
2430
2431 fn(state, buf, length);
2432 talloc_free(buf);
2433}
2434
2435
2436static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
2437 TALLOC_CTX *mem_ctx;
2438 struct ntlm_auth_state *state;
2439
2440 /* initialize FDescs */
2441 x_setbuf(x_stdout, NULL);
2442 x_setbuf(x_stderr, NULL);
2443
2444 mem_ctx = talloc_init("ntlm_auth");
2445 if (!mem_ctx) {
2446 DEBUG(0, ("squid_stream: Failed to create talloc context\n"));
2447 x_fprintf(x_stderr, "ERR\n");
2448 exit(1);
2449 }
2450
2451 state = talloc_zero(mem_ctx, struct ntlm_auth_state);
2452 if (!state) {
2453 DEBUG(0, ("squid_stream: Failed to talloc ntlm_auth_state\n"));
2454 x_fprintf(x_stderr, "ERR\n");
2455 exit(1);
2456 }
2457
2458 state->mem_ctx = mem_ctx;
2459 state->helper_mode = stdio_mode;
2460
2461 while(1) {
2462 TALLOC_CTX *frame = talloc_stackframe();
2463 manage_squid_request(state, fn);
2464 TALLOC_FREE(frame);
2465 }
2466}
2467
2468
2469/* Authenticate a user with a challenge/response */
2470
2471static bool check_auth_crap(void)
2472{
2473 NTSTATUS nt_status;
2474 uint32 flags = 0;
2475 char lm_key[8];
2476 char user_session_key[16];
2477 char *hex_lm_key;
2478 char *hex_user_session_key;
2479 char *error_string;
2480 static uint8 zeros[16];
2481
2482 x_setbuf(x_stdout, NULL);
2483
2484 if (request_lm_key)
2485 flags |= WBFLAG_PAM_LMKEY;
2486
2487 if (request_user_session_key)
2488 flags |= WBFLAG_PAM_USER_SESSION_KEY;
2489
2490 flags |= WBFLAG_PAM_NT_STATUS_SQUASH;
2491
2492 nt_status = contact_winbind_auth_crap(opt_username, opt_domain,
2493 opt_workstation,
2494 &opt_challenge,
2495 &opt_lm_response,
2496 &opt_nt_response,
2497 flags,
2498 (unsigned char *)lm_key,
2499 (unsigned char *)user_session_key,
2500 &error_string, NULL);
2501
2502 if (!NT_STATUS_IS_OK(nt_status)) {
2503 x_fprintf(x_stdout, "%s (0x%x)\n",
2504 error_string,
2505 NT_STATUS_V(nt_status));
2506 SAFE_FREE(error_string);
2507 return False;
2508 }
2509
2510 if (request_lm_key
2511 && (memcmp(zeros, lm_key,
2512 sizeof(lm_key)) != 0)) {
2513 hex_lm_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)lm_key,
2514 sizeof(lm_key));
2515 x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
2516 TALLOC_FREE(hex_lm_key);
2517 }
2518 if (request_user_session_key
2519 && (memcmp(zeros, user_session_key,
2520 sizeof(user_session_key)) != 0)) {
2521 hex_user_session_key = hex_encode_talloc(talloc_tos(), (const unsigned char *)user_session_key,
2522 sizeof(user_session_key));
2523 x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
2524 TALLOC_FREE(hex_user_session_key);
2525 }
2526
2527 return True;
2528}
2529
2530/* Main program */
2531
2532enum {
2533 OPT_USERNAME = 1000,
2534 OPT_DOMAIN,
2535 OPT_WORKSTATION,
2536 OPT_CHALLENGE,
2537 OPT_RESPONSE,
2538 OPT_LM,
2539 OPT_NT,
2540 OPT_PASSWORD,
2541 OPT_LM_KEY,
2542 OPT_USER_SESSION_KEY,
2543 OPT_DIAGNOSTICS,
2544 OPT_REQUIRE_MEMBERSHIP,
2545 OPT_USE_CACHED_CREDS,
2546 OPT_PAM_WINBIND_CONF
2547};
2548
2549 int main(int argc, const char **argv)
2550{
2551 TALLOC_CTX *frame = talloc_stackframe();
2552 int opt;
2553 static const char *helper_protocol;
2554 static int diagnostics;
2555
2556 static const char *hex_challenge;
2557 static const char *hex_lm_response;
2558 static const char *hex_nt_response;
2559
2560 poptContext pc;
2561
2562 /* NOTE: DO NOT change this interface without considering the implications!
2563 This is an external interface, which other programs will use to interact
2564 with this helper.
2565 */
2566
2567 /* We do not use single-letter command abbreviations, because they harm future
2568 interface stability. */
2569
2570 struct poptOption long_options[] = {
2571 POPT_AUTOHELP
2572 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
2573 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_USERNAME, "username"},
2574 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
2575 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
2576 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
2577 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
2578 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
2579 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},
2580 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retrieve LM session key"},
2581 { "request-nt-key", 0, POPT_ARG_NONE, &request_user_session_key, OPT_USER_SESSION_KEY, "Retrieve User (NT) session key"},
2582 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "Use cached credentials if no password is given"},
2583 { "diagnostics", 0, POPT_ARG_NONE, &diagnostics, OPT_DIAGNOSTICS, "Perform diagnostics on the authentictaion chain"},
2584 { "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" },
2585 { "pam-winbind-conf", 0, POPT_ARG_STRING, &opt_pam_winbind_conf, OPT_PAM_WINBIND_CONF, "Require that request must set WBFLAG_PAM_CONTACT_TRUSTDOM when krb5 auth is required" },
2586 POPT_COMMON_CONFIGFILE
2587 POPT_COMMON_VERSION
2588 POPT_TABLEEND
2589 };
2590
2591 /* Samba client initialisation */
2592 load_case_tables();
2593
2594 dbf = x_stderr;
2595
2596 /* Parse options */
2597
2598 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
2599
2600 /* Parse command line options */
2601
2602 if (argc == 1) {
2603 poptPrintHelp(pc, stderr, 0);
2604 return 1;
2605 }
2606
2607 while((opt = poptGetNextOpt(pc)) != -1) {
2608 /* Get generic config options like --configfile */
2609 }
2610
2611 poptFreeContext(pc);
2612
2613 if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
2614 d_fprintf(stderr, "ntlm_auth: error opening config file %s. Error was %s\n",
2615 get_dyn_CONFIGFILE(), strerror(errno));
2616 exit(1);
2617 }
2618
2619 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
2620 POPT_CONTEXT_KEEP_FIRST);
2621
2622 while((opt = poptGetNextOpt(pc)) != -1) {
2623 switch (opt) {
2624 case OPT_CHALLENGE:
2625 opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
2626 if (opt_challenge.length != 8) {
2627 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2628 hex_challenge,
2629 (int)opt_challenge.length);
2630 exit(1);
2631 }
2632 break;
2633 case OPT_LM:
2634 opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
2635 if (opt_lm_response.length != 24) {
2636 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2637 hex_lm_response,
2638 (int)opt_lm_response.length);
2639 exit(1);
2640 }
2641 break;
2642
2643 case OPT_NT:
2644 opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
2645 if (opt_nt_response.length < 24) {
2646 x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n",
2647 hex_nt_response,
2648 (int)opt_nt_response.length);
2649 exit(1);
2650 }
2651 break;
2652
2653 case OPT_REQUIRE_MEMBERSHIP:
2654 if (StrnCaseCmp("S-", require_membership_of, 2) == 0) {
2655 require_membership_of_sid = require_membership_of;
2656 }
2657 break;
2658 }
2659 }
2660
2661 if (opt_username) {
2662 char *domain = SMB_STRDUP(opt_username);
2663 char *p = strchr_m(domain, *lp_winbind_separator());
2664 if (p) {
2665 opt_username = p+1;
2666 *p = '\0';
2667 if (opt_domain && !strequal(opt_domain, domain)) {
2668 x_fprintf(x_stderr, "Domain specified in username (%s) "
2669 "doesn't match specified domain (%s)!\n\n",
2670 domain, opt_domain);
2671 poptPrintHelp(pc, stderr, 0);
2672 exit(1);
2673 }
2674 opt_domain = domain;
2675 } else {
2676 SAFE_FREE(domain);
2677 }
2678 }
2679
2680 /* Note: if opt_domain is "" then send no domain */
2681 if (opt_domain == NULL) {
2682 opt_domain = get_winbind_domain();
2683 }
2684
2685 if (opt_workstation == NULL) {
2686 opt_workstation = "";
2687 }
2688
2689 if (helper_protocol) {
2690 int i;
2691 for (i=0; i<NUM_HELPER_MODES; i++) {
2692 if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
2693 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
2694 exit(0);
2695 }
2696 }
2697 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
2698
2699 for (i=0; i<NUM_HELPER_MODES; i++) {
2700 x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
2701 }
2702
2703 exit(1);
2704 }
2705
2706 if (!opt_username || !*opt_username) {
2707 x_fprintf(x_stderr, "username must be specified!\n\n");
2708 poptPrintHelp(pc, stderr, 0);
2709 exit(1);
2710 }
2711
2712 if (opt_challenge.length) {
2713 if (!check_auth_crap()) {
2714 exit(1);
2715 }
2716 exit(0);
2717 }
2718
2719 if (!opt_password) {
2720 opt_password = getpass("password: ");
2721 }
2722
2723 if (diagnostics) {
2724 if (!diagnose_ntlm_auth()) {
2725 return 1;
2726 }
2727 } else {
2728 fstring user;
2729
2730 fstr_sprintf(user, "%s%c%s", opt_domain, winbind_separator(), opt_username);
2731 if (!check_plaintext_auth(user, opt_password, True)) {
2732 return 1;
2733 }
2734 }
2735
2736 /* Exit code */
2737
2738 poptFreeContext(pc);
2739 TALLOC_FREE(frame);
2740 return 0;
2741}
Note: See TracBrowser for help on using the repository browser.