Debian Codegraph @

pppd/plugins/winbind.c

raw
1 /***********************************************************************
2 *
3 * winbind.c
4 *
5 * WINBIND plugin for pppd. Performs PAP, CHAP, MS-CHAP, MS-CHAPv2
6 * authentication using WINBIND to contact a NT-style PDC.
7 *
8 * Based on the structure of the radius module.
9 *
10 * Copyright 1999-2024 Paul Mackerras <[email protected]>
11 * Copyright (C) 2003 Andrew Bartlet <[email protected]>
12 *
13 * Copyright 1999 Alan Curry.
14 * (pipe read code from passpromt.c)
15 *
16 * Copyright (C) 2002 Roaring Penguin Software Inc.
17 *
18 * Based on a patch for ipppd, which is:
19 * Copyright (C) 1996, Matjaz Godec <[email protected]>
20 * Copyright (C) 1996, Lars Fenneberg <[email protected]>
21 * Copyright (C) 1997, Miguel A.L. Paraz <[email protected]>
22 *
23 * Uses radiusclient library, which is:
24 * Copyright (C) 1995,1996,1997,1998 Lars Fenneberg <[email protected]>
25 * Copyright (C) 2002 Roaring Penguin Software Inc.
26 *
27 * MPPE support is by Ralf Hofmann, <[email protected]>, with
28 * modification from Frank Cusack, <[email protected]>.
29 *
30 * Updated on 2003-12-12 to support updated PPP plugin API from latest CVS
31 * Copyright (C) 2003, Sean E. Millichamp <sean at bruenor dot org>
32 *
33 * This plugin may be distributed according to the terms of the GNU
34 * General Public License, version 2 or (at your option) any later version.
35 *
36 ***********************************************************************/
37
38 #include <syslog.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <sys/time.h>
43 #include <sys/wait.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include <ctype.h>
49 #include <stdbool.h>
50 #include <stdarg.h>
51 #include <stdint.h>
52 #include <stdio.h>
53
54 #include <pppd/pppd.h>
55 #include <pppd/options.h>
56 #include <pppd/chap.h>
57 #include <pppd/chap_ms.h>
58 #include <pppd/upap.h>
59 #include <pppd/fsm.h>
60 #include <pppd/ipcp.h>
61 #include <pppd/mppe.h>
62 #include <pppd/crypto.h>
63
64 #define BUF_LEN 1024
65
66 #define NOT_AUTHENTICATED 0
67 #define AUTHENTICATED 1
68
69 static char *ntlm_auth = NULL;
70
71 static int set_ntlm_auth(char **argv)
72 {
73 char *p;
74
75 p = argv[0];
76 if (p[0] != '/') {
77 ppp_option_error("ntlm_auth-helper argument must be full path");
78 return 0;
79 }
80 p = strdup(p);
81 if (p == NULL) {
82 novm("ntlm_auth-helper argument");
83 return 0;
84 }
85 if (ntlm_auth != NULL)
86 free(ntlm_auth);
87 ntlm_auth = p;
88 return 1;
89 }
90
91 static struct option Options[] = {
92 { "ntlm_auth-helper", o_special, (void *) &set_ntlm_auth,
93 "Path to ntlm_auth executable", OPT_PRIV },
94 { NULL }
95 };
96
97 static pap_check_hook_fn winbind_secret_check;
98 static pap_auth_hook_fn winbind_pap_auth;
99 static chap_verify_hook_fn winbind_chap_verify;
100 static int winbind_allowed_address(uint32_t addr);
101
102 char pppd_version[] = PPPD_VERSION;
103
104 /**********************************************************************
105 * %FUNCTION: plugin_init
106 * %ARGUMENTS:
107 * None
108 * %RETURNS:
109 * Nothing
110 * %DESCRIPTION:
111 * Initializes WINBIND plugin.
112 ***********************************************************************/
113 void
114 plugin_init(void)
115 {
116 pap_check_hook = winbind_secret_check;
117 pap_auth_hook = winbind_pap_auth;
118
119 chap_check_hook = winbind_secret_check;
120 chap_verify_hook = winbind_chap_verify;
121
122 allowed_address_hook = winbind_allowed_address;
123
124 /* Don't ask the peer for anything other than MS-CHAP or MS-CHAP V2 */
125 chap_mdtype_all &= (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT);
126
127 ppp_add_options(Options);
128
129 info("WINBIND plugin initialized.");
130 }
131
132 /**
133 Routine to get hex characters and turn them into a 16 byte array.
134 the array can be variable length, and any non-hex-numeric
135 characters are skipped. "0xnn" or "0Xnn" is specially catered
136 for.
137
138 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
139
140 **/
141
142 /*
143 Unix SMB/CIFS implementation.
144 Samba utility functions
145
146 Copyright (C) Andrew Tridgell 1992-2001
147 Copyright (C) Simo Sorce 2001-2002
148 Copyright (C) Martin Pool 2003
149
150 This program is free software; you can redistribute it and/or modify
151 it under the terms of the GNU General Public License as published by
152 the Free Software Foundation; either version 2 of the License, or
153 (at your option) any later version.
154
155 This program is distributed in the hope that it will be useful,
156 but WITHOUT ANY WARRANTY; without even the implied warranty of
157 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
158 GNU General Public License for more details.
159
160 You should have received a copy of the GNU General Public License
161 along with this program; if not, write to the Free Software
162 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
163 */
164
165 size_t strhex_to_str(unsigned char *p, size_t len, const char *strhex)
166 {
167 size_t i;
168 size_t num_chars = 0;
169 unsigned char lonybble, hinybble;
170 const char *hexchars = "0123456789ABCDEF";
171 char *p1 = NULL, *p2 = NULL;
172
173 for (i = 0; i < len && strhex[i] != 0; i++) {
174 if (strncmp(hexchars, "0x", 2) == 0) {
175 i++; /* skip two chars */
176 continue;
177 }
178
179 if (!(p1 = strchr(hexchars, toupper(strhex[i]))))
180 break;
181
182 i++; /* next hex digit */
183
184 if (!(p2 = strchr(hexchars, toupper(strhex[i]))))
185 break;
186
187 /* get the two nybbles */
188 hinybble = (p1 - hexchars);
189 lonybble = (p2 - hexchars);
190
191 p[num_chars] = (hinybble << 4) | lonybble;
192 num_chars++;
193
194 p1 = NULL;
195 p2 = NULL;
196 }
197 return num_chars;
198 }
199
200 static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
201
202 /**
203 * Encode a base64 string into a malloc()ed string caller to free.
204 *
205 *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
206 **/
207 char * base64_encode(const char *data)
208 {
209 size_t out_cnt = 0;
210 size_t len = strlen(data);
211 size_t output_len = 4 * ((len + 2) / 3) + 2;
212 const unsigned char *ptr = (const unsigned char *) data;
213 char *result = malloc(output_len); /* get us plenty of space */
214 unsigned int bits;
215
216 for (; len >= 3; len -= 3) {
217 bits = (ptr[0] << 16) + (ptr[1] << 8) + ptr[2];
218 ptr += 3;
219 result[out_cnt++] = b64[bits >> 18];
220 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
221 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
222 result[out_cnt++] = b64[bits & 0x3f];
223 }
224 if (len != 0) {
225 bits = ptr[0] << 16;
226 if (len > 1)
227 bits |= ptr[1] << 8;
228 result[out_cnt++] = b64[bits >> 18];
229 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
230 result[out_cnt++] = (len > 1)? b64[(bits >> 6) & 0x3f]: '=';
231 result[out_cnt++] = '=';
232 }
233
234 result[out_cnt] = '\0'; /* terminate */
235 return result;
236 }
237
238 unsigned int run_ntlm_auth(const char *username,
239 const char *domain,
240 const char *full_username,
241 const char *plaintext_password,
242 const u_char *challenge,
243 size_t challenge_length,
244 const u_char *lm_response,
245 size_t lm_response_length,
246 const u_char *nt_response,
247 size_t nt_response_length,
248 u_char nt_key[16],
249 char **error_string)
250 {
251
252 pid_t forkret;
253 int child_in[2];
254 int child_out[2];
255 int status;
256
257 int authenticated = NOT_AUTHENTICATED; /* not auth */
258 int got_user_session_key = 0; /* not got key */
259
260 char buffer[1024];
261
262 FILE *pipe_in;
263 FILE *pipe_out;
264
265 int i;
266 char *challenge_hex;
267 char *lm_hex_hash;
268 char *nt_hex_hash;
269
270 /* First see if we have a program to run... */
271 if (ntlm_auth == NULL)
272 return NOT_AUTHENTICATED;
273
274 /* Make first child */
275 if (pipe(child_out) == -1) {
276 error("pipe creation failed for child OUT!");
277 return NOT_AUTHENTICATED;
278 }
279
280 if (pipe(child_in) == -1) {
281 error("pipe creation failed for child IN!");
282 return NOT_AUTHENTICATED;
283 }
284
285 forkret = ppp_safe_fork(child_in[0], child_out[1], 2);
286 if (forkret == -1) {
287 if (error_string) {
288 *error_string = strdup("fork failed!");
289 }
290
291 return NOT_AUTHENTICATED;
292 }
293
294 if (forkret == 0) {
295 /* child process */
296 uid_t uid;
297 gid_t gid;
298
299 close(child_out[0]);
300 close(child_in[1]);
301
302 /* run winbind as the user that invoked pppd */
303 gid = getgid();
304 if (setgid(gid) == -1 || getgid() != gid) {
305 fatal("pppd/winbind: could not setgid to %d: %m", gid);
306 }
307 uid = getuid();
308 if (setuid(uid) == -1 || getuid() != uid) {
309 fatal("pppd/winbind: could not setuid to %d: %m", uid);
310 }
311 execl("/bin/sh", "sh", "-c", ntlm_auth, NULL);
312 fatal("pppd/winbind: could not exec /bin/sh: %m");
313 }
314
315 /* parent */
316 close(child_out[1]);
317 close(child_in[0]);
318
319 /* Need to write the User's info onto the pipe */
320
321 pipe_in = fdopen(child_in[1], "w");
322
323 pipe_out = fdopen(child_out[0], "r");
324
325 /* look for session key coming back */
326
327 if (username) {
328 char *b64_username = base64_encode(username);
329 fprintf(pipe_in, "Username:: %s\n", b64_username);
330 free(b64_username);
331 }
332
333 if (domain) {
334 char *b64_domain = base64_encode(domain);
335 fprintf(pipe_in, "NT-Domain:: %s\n", b64_domain);
336 free(b64_domain);
337 }
338
339 if (full_username) {
340 char *b64_full_username = base64_encode(full_username);
341 fprintf(pipe_in, "Full-Username:: %s\n", b64_full_username);
342 free(b64_full_username);
343 }
344
345 if (plaintext_password) {
346 char *b64_plaintext_password = base64_encode(plaintext_password);
347 fprintf(pipe_in, "Password:: %s\n", b64_plaintext_password);
348 free(b64_plaintext_password);
349 }
350
351 if (challenge_length) {
352 fprintf(pipe_in, "Request-User-Session-Key: yes\n");
353
354 challenge_hex = malloc(challenge_length*2+1);
355
356 for (i = 0; i < challenge_length; i++)
357 sprintf(challenge_hex + i * 2, "%02X", challenge[i]);
358
359 fprintf(pipe_in, "LANMAN-Challenge: %s\n", challenge_hex);
360 free(challenge_hex);
361 }
362
363 if (lm_response_length) {
364 lm_hex_hash = malloc(lm_response_length*2+1);
365
366 for (i = 0; i < lm_response_length; i++)
367 sprintf(lm_hex_hash + i * 2, "%02X", lm_response[i]);
368
369 fprintf(pipe_in, "LANMAN-response: %s\n", lm_hex_hash);
370 free(lm_hex_hash);
371 }
372
373 if (nt_response_length) {
374 nt_hex_hash = malloc(nt_response_length*2+1);
375
376 for (i = 0; i < nt_response_length; i++)
377 sprintf(nt_hex_hash + i * 2, "%02X", nt_response[i]);
378
379 fprintf(pipe_in, "NT-response: %s\n", nt_hex_hash);
380 free(nt_hex_hash);
381 }
382
383 fprintf(pipe_in, ".\n");
384 fflush(pipe_in);
385
386 while (fgets(buffer, sizeof(buffer)-1, pipe_out) != NULL) {
387 char *message, *parameter;
388 if (buffer[strlen(buffer)-1] != '\n') {
389 break;
390 }
391 buffer[strlen(buffer)-1] = '\0';
392 message = buffer;
393
394 if (!(parameter = strstr(buffer, ": "))) {
395 break;
396 }
397
398 parameter[0] = '\0';
399 parameter++;
400 parameter[0] = '\0';
401 parameter++;
402
403 if (strcmp(message, ".") == 0) {
404 /* end of sequence */
405 break;
406 } else if (strcasecmp(message, "Authenticated") == 0) {
407 if (strcasecmp(parameter, "Yes") == 0) {
408 authenticated = AUTHENTICATED;
409 } else {
410 notice("Winbind has declined authentication for user!");
411 authenticated = NOT_AUTHENTICATED;
412 }
413 } else if (strcasecmp(message, "User-session-key") == 0) {
414 /* length is the number of characters to parse */
415 if (nt_key) {
416 if (strhex_to_str(nt_key, 32, parameter) == 16) {
417 got_user_session_key = 1;
418 } else {
419 notice("NT session key for user was not 16 bytes!");
420 }
421 }
422 } else if (strcasecmp(message, "Error") == 0) {
423 authenticated = NOT_AUTHENTICATED;
424 if (error_string)
425 *error_string = strdup(parameter);
426 } else if (strcasecmp(message, "Authentication-Error") == 0) {
427 authenticated = NOT_AUTHENTICATED;
428 if (error_string)
429 *error_string = strdup(parameter);
430 } else {
431 notice("unrecognised input from ntlm_auth helper - %s: %s", message, parameter);
432 }
433 }
434
435 /* parent */
436 if (fclose(pipe_out) == -1) {
437 fclose(pipe_in);
438 notice("error closing pipe?!? for child OUT[0]");
439 return NOT_AUTHENTICATED;
440 }
441
442 /* parent */
443 if (fclose(pipe_in) == -1) {
444 notice("error closing pipe?!? for child IN[1]");
445 return NOT_AUTHENTICATED;
446 }
447
448 while ((wait(&status) == -1) && errno == EINTR && !ppp_signaled(SIGTERM))
449 ;
450
451 if ((authenticated == AUTHENTICATED) && nt_key && !got_user_session_key) {
452 notice("Did not get user session key, despite being authenticated!");
453 return NOT_AUTHENTICATED;
454 }
455 return authenticated;
456 }
457
458 /**********************************************************************
459 * %FUNCTION: winbind_secret_check
460 * %ARGUMENTS:
461 * None
462 * %RETURNS:
463 * 0 if we don't have an ntlm_auth program to run, otherwise 1.
464 * %DESCRIPTION:
465 * Tells pppd that we will try to authenticate the peer, and not to
466 * worry about looking in /etc/ppp/ *-secrets
467 ***********************************************************************/
468 static int
469 winbind_secret_check(void)
470 {
471 return ntlm_auth != NULL;
472 }
473
474 /**********************************************************************
475 * %FUNCTION: winbind_pap_auth
476 * %ARGUMENTS:
477 * user -- user-name of peer
478 * passwd -- password supplied by peer
479 * msgp -- Message which will be sent in PAP response
480 * paddrs -- set to a list of possible peer IP addresses
481 * popts -- set to a list of additional pppd options
482 * %RETURNS:
483 * 1 if we can authenticate, -1 if we cannot.
484 * %DESCRIPTION:
485 * Performs PAP authentication using WINBIND
486 ***********************************************************************/
487 static int
488 winbind_pap_auth(char *user,
489 char *password,
490 char **msgp,
491 struct wordlist **paddrs,
492 struct wordlist **popts)
493 {
494 if (run_ntlm_auth(NULL, NULL, user, password, NULL, 0, NULL, 0, NULL, 0, NULL, msgp) == AUTHENTICATED) {
495 return 1;
496 }
497 return -1;
498 }
499
500 /**********************************************************************
501 * %FUNCTION: winbind_chap_auth
502 * %ARGUMENTS:
503 * user -- user-name of peer
504 * remmd -- hash received from peer
505 * remmd_len -- length of remmd
506 * cstate -- pppd's chap_state structure
507 * %RETURNS:
508 * AUTHENTICATED (1) if we can authenticate, NOT_AUTHENTICATED (0) if we cannot.
509 * %DESCRIPTION:
510 * Performs MS-CHAP and MS-CHAPv2 authentication using WINBIND.
511 ***********************************************************************/
512
513 static int
514 winbind_chap_verify(char *user, char *ourname, int id,
515 struct chap_digest_type *digest,
516 unsigned char *challenge,
517 unsigned char *response,
518 char *message, int message_space)
519 {
520 int challenge_len, response_len;
521 char domainname[256];
522 char *domain;
523 const char *username;
524 char *p;
525 unsigned char saresponse[MS_AUTH_RESPONSE_LENGTH+1];
526
527 /* The first byte of each of these strings contains their length */
528 challenge_len = *challenge++;
529 response_len = *response++;
530
531 /* remove domain from "domain\username" */
532 if ((username = strrchr(user, '\\')) != NULL)
533 ++username;
534 else
535 username = user;
536
537 strlcpy(domainname, user, sizeof(domainname));
538
539 /* remove domain from "domain\username" */
540 if ((p = strrchr(domainname, '\\')) != NULL) {
541 *p = '\0';
542 domain = domainname;
543 } else {
544 domain = NULL;
545 }
546
547 /* generate MD based on negotiated type */
548 switch (digest->code) {
549
550 case CHAP_MICROSOFT:
551 {
552 char *error_string = NULL;
553 u_char *nt_response = NULL;
554 u_char *lm_response = NULL;
555 int nt_response_size = 0;
556 int lm_response_size = 0;
557 u_char session_key[MD4_DIGEST_LENGTH];
558
559 if (response_len != MS_CHAP_RESPONSE_LEN)
560 break; /* not even the right length */
561
562 /* Determine which part of response to verify against */
563 if (response[MS_CHAP_USENT]) {
564 nt_response = &response[MS_CHAP_NTRESP];
565 nt_response_size = MS_CHAP_NTRESP_LEN;
566 } else {
567 #ifdef PPP_WITH_MSLANMAN
568 lm_response = &response[MS_CHAP_LANMANRESP];
569 lm_response_size = MS_CHAP_LANMANRESP_LEN;
570 #else
571 /* Should really propagate this into the error packet. */
572 notice("Peer request for LANMAN auth not supported");
573 return NOT_AUTHENTICATED;
574 #endif /* PPP_WITH_MSLANMAN */
575 }
576
577 /* ship off to winbind, and check */
578
579 if (run_ntlm_auth(username,
580 domain,
581 NULL,
582 NULL,
583 challenge, challenge_len,
584 lm_response, lm_response_size,
585 nt_response, nt_response_size,
586 session_key,
587 &error_string) == AUTHENTICATED) {
588 #ifdef PPP_WITH_MPPE
589 mppe_set_chapv1(challenge, session_key);
590 #endif
591 slprintf(message, message_space, "Access granted");
592 return AUTHENTICATED;
593
594 } else {
595 if (error_string) {
596 notice(error_string);
597 free(error_string);
598 }
599 slprintf(message, message_space, "E=691 R=1 C=%0.*B V=0",
600 challenge_len, challenge);
601 return NOT_AUTHENTICATED;
602 }
603 break;
604 }
605
606 case CHAP_MICROSOFT_V2:
607 {
608 u_char Challenge[8];
609 u_char session_key[MD4_DIGEST_LENGTH];
610 char *error_string = NULL;
611
612 if (response_len != MS_CHAP2_RESPONSE_LEN)
613 break; /* not even the right length */
614
615 ChallengeHash(&response[MS_CHAP2_PEER_CHALLENGE], challenge,
616 user, Challenge);
617
618 /* ship off to winbind, and check */
619
620 if (run_ntlm_auth(username,
621 domain,
622 NULL,
623 NULL,
624 Challenge, 8,
625 NULL, 0,
626 &response[MS_CHAP2_NTRESP],
627 MS_CHAP2_NTRESP_LEN,
628 session_key,
629 &error_string) == AUTHENTICATED) {
630
631 GenerateAuthenticatorResponse(session_key,
632 &response[MS_CHAP2_NTRESP],
633 &response[MS_CHAP2_PEER_CHALLENGE],
634 challenge, user, saresponse);
635 #ifdef PPP_WITH_MPPE
636 mppe_set_chapv2(session_key, &response[MS_CHAP2_NTRESP],
637 MS_CHAP2_AUTHENTICATOR);
638 #endif
639 if (response[MS_CHAP2_FLAGS]) {
640 slprintf(message, message_space, "S=%s", saresponse);
641 } else {
642 slprintf(message, message_space, "S=%s M=%s",
643 saresponse, "Access granted");
644 }
645 return AUTHENTICATED;
646
647 } else {
648 if (error_string) {
649 notice(error_string);
650 slprintf(message, message_space, "E=691 R=1 C=%0.*B V=0 M=%s",
651 challenge_len, challenge, error_string);
652 free(error_string);
653 } else {
654 slprintf(message, message_space, "E=691 R=1 C=%0.*B V=0 M=%s",
655 challenge_len, challenge, "Access denied");
656 }
657 return NOT_AUTHENTICATED;
658 }
659 break;
660 }
661
662 default:
663 error("WINBIND: Challenge type %u unsupported", digest->code);
664 }
665 return NOT_AUTHENTICATED;
666 }
667
668 static int
669 winbind_allowed_address(uint32_t addr)
670 {
671 ipcp_options *wo = &ipcp_wantoptions[0];
672 if (wo->hisaddr !=0 && wo->hisaddr == addr) {
673 return 1;
674 }
675 return -1;
676 }