1 /* $NetBSD: auth.c,v 1.6 2025/01/08 19:59:38 christos Exp $ */ 2 3 /* 4 * auth.c - PPP authentication and phase control. 5 * 6 * Copyright (c) 1993-2024 Paul Mackerras. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO 21 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 22 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 23 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 24 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 25 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 26 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 27 * 28 * Derived from main.c, which is: 29 * 30 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in 41 * the documentation and/or other materials provided with the 42 * distribution. 43 * 44 * 3. The name "Carnegie Mellon University" must not be used to 45 * endorse or promote products derived from this software without 46 * prior written permission. For permission or any legal 47 * details, please contact 48 * Office of Technology Transfer 49 * Carnegie Mellon University 50 * 5000 Forbes Avenue 51 * Pittsburgh, PA 15213-3890 52 * (412) 268-4387, fax: (412) 268-7395 53 * tech-transfer@andrew.cmu.edu 54 * 55 * 4. Redistributions of any form whatsoever must retain the following 56 * acknowledgment: 57 * "This product includes software developed by Computing Services 58 * at Carnegie Mellon University (http://www.cmu.edu/computing/)." 59 * 60 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO 61 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 62 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE 63 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 64 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 65 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 66 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 67 */ 68 69 #include <sys/cdefs.h> 70 __RCSID("$NetBSD: auth.c,v 1.6 2025/01/08 19:59:38 christos Exp $"); 71 72 #ifdef HAVE_CONFIG_H 73 #include "config.h" 74 #endif 75 76 #include <stdio.h> 77 #include <stddef.h> 78 #include <stdlib.h> 79 #include <unistd.h> 80 #include <errno.h> 81 #include <pwd.h> 82 #include <grp.h> 83 #include <string.h> 84 #include <strings.h> 85 #include <sys/param.h> 86 #include <sys/types.h> 87 #include <sys/stat.h> 88 #include <sys/socket.h> 89 #include <fcntl.h> 90 #if defined(_PATH_LASTLOG) && defined(__linux__) 91 #include <lastlog.h> 92 #endif 93 94 #include <netdb.h> 95 #include <netinet/in.h> 96 #include <arpa/inet.h> 97 98 99 #ifdef HAVE_SHADOW_H 100 #include <shadow.h> 101 #ifndef PW_PPP 102 #define PW_PPP PW_LOGIN 103 #endif 104 #endif 105 #include <time.h> 106 107 #ifdef HAVE_CRYPT_H 108 #include <crypt.h> 109 #endif 110 111 #ifdef SYSTEMD 112 #include <systemd/sd-daemon.h> 113 #endif 114 115 #include "pppd-private.h" 116 #include "options.h" 117 #include "fsm.h" 118 #include "lcp.h" 119 #include "ccp.h" 120 #include "ecp.h" 121 #include "ipcp.h" 122 #include "upap.h" 123 #include "chap.h" 124 #include "eap.h" 125 #ifdef PPP_WITH_EAPTLS 126 #include "eap-tls.h" 127 #endif 128 #ifdef PPP_WITH_CBCP 129 #include "cbcp.h" 130 #endif 131 #include "multilink.h" 132 #include "pathnames.h" 133 #include "session.h" 134 135 136 /* Bits in scan_authfile return value */ 137 #define NONWILD_SERVER 1 138 #define NONWILD_CLIENT 2 139 140 #define ISWILD(word) (word[0] == '*' && word[1] == 0) 141 142 /* The name by which the peer authenticated itself to us. */ 143 char peer_authname[MAXNAMELEN]; 144 145 /* Records which authentication operations haven't completed yet. */ 146 static int auth_pending[NUM_PPP]; 147 148 /* Records which authentication operations have been completed. */ 149 int auth_done[NUM_PPP]; 150 151 /* List of addresses which the peer may use. */ 152 static struct permitted_ip *addresses[NUM_PPP]; 153 154 /* Wordlist giving addresses which the peer may use 155 without authenticating itself. */ 156 static struct wordlist *noauth_addrs; 157 158 /* Remote telephone number, if available */ 159 char remote_number[MAXNAMELEN]; 160 161 /* Wordlist giving remote telephone numbers which may connect. */ 162 static struct wordlist *permitted_numbers; 163 164 /* Extra options to apply, from the secrets file entry for the peer. */ 165 static struct wordlist *extra_options; 166 167 /* Number of network protocols which we have opened. */ 168 static int num_np_open; 169 170 /* Number of network protocols which have come up. */ 171 static int num_np_up; 172 173 /* Set if we got the contents of passwd[] from the pap-secrets file. */ 174 static int passwd_from_file; 175 176 /* Set if we require authentication only because we have a default route. */ 177 static bool default_auth; 178 179 /* Hook to enable a plugin to control the idle time limit */ 180 int (*idle_time_hook)(struct ppp_idle *) = NULL; 181 182 /* Hook for a plugin to say whether we can possibly authenticate any peer */ 183 pap_check_hook_fn *pap_check_hook = NULL; 184 185 /* Hook for a plugin to check the PAP user and password */ 186 pap_auth_hook_fn *pap_auth_hook = NULL; 187 188 /* Hook for a plugin to know about the PAP user logout */ 189 pap_logout_hook_fn *pap_logout_hook = NULL; 190 191 /* Hook for a plugin to get the PAP password for authenticating us */ 192 pap_passwd_hook_fn *pap_passwd_hook = NULL; 193 194 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */ 195 chap_check_hook_fn *chap_check_hook = NULL; 196 197 /* Hook for a plugin to get the CHAP password for authenticating us */ 198 chap_passwd_hook_fn *chap_passwd_hook = NULL; 199 200 #ifdef PPP_WITH_EAPTLS 201 /* Hook for a plugin to get the EAP-TLS password for authenticating us */ 202 eaptls_passwd_hook_fn *eaptls_passwd_hook = NULL; 203 #endif 204 205 /* Hook for a plugin to say whether it is OK if the peer 206 refuses to authenticate. */ 207 int (*null_auth_hook)(struct wordlist **paddrs, 208 struct wordlist **popts) = NULL; 209 210 int (*allowed_address_hook)(u_int32_t addr) = NULL; 211 212 /* A notifier for when the peer has authenticated itself, 213 and we are proceeding to the network phase. */ 214 struct notifier *auth_up_notifier = NULL; 215 216 /* A notifier for when the link goes down. */ 217 struct notifier *link_down_notifier = NULL; 218 219 /* 220 * This is used to ensure that we don't start an auth-up/down 221 * script while one is already running. 222 */ 223 enum script_state { 224 s_down, 225 s_up 226 }; 227 228 static enum script_state auth_state = s_down; 229 static enum script_state auth_script_state = s_down; 230 static pid_t auth_script_pid = 0; 231 232 /* 233 * Option variables. 234 */ 235 bool uselogin = 0; /* Use /etc/passwd for checking PAP */ 236 bool session_mgmt = 0; /* Do session management (login records) */ 237 bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */ 238 bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */ 239 bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */ 240 bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */ 241 #ifdef PPP_WITH_CHAPMS 242 bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */ 243 bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */ 244 #else 245 bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */ 246 bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */ 247 #endif 248 bool usehostname = 0; /* Use hostname for our_name */ 249 bool auth_required = 0; /* Always require authentication from peer */ 250 bool allow_any_ip = 0; /* Allow peer to use any IP address */ 251 bool explicit_remote = 0; /* User specified explicit remote name */ 252 bool explicit_user = 0; /* Set if "user" option supplied */ 253 bool explicit_passwd = 0; /* Set if "password" option supplied */ 254 char remote_name[MAXNAMELEN]; /* Peer's name for authentication */ 255 char path_upapfile[MAXPATHLEN]; /* Pathname of pap-secrets file */ 256 char path_chapfile[MAXPATHLEN]; /* Pathname of chap-secrets file */ 257 258 #if defined(PPP_WITH_EAPTLS) || defined(PPP_WITH_PEAP) 259 char *cacert_file = NULL; /* CA certificate file (pem format) */ 260 char *ca_path = NULL; /* Directory with CA certificates */ 261 char *crl_dir = NULL; /* Directory containing CRL files */ 262 char *crl_file = NULL; /* Certificate Revocation List (CRL) file (pem format) */ 263 char *max_tls_version = NULL; /* Maximum TLS protocol version (default=1.2) */ 264 char *tls_verify_method = NULL; /* Verify certificate method */ 265 bool tls_verify_key_usage = 0; /* Verify peer certificate key usage */ 266 #endif 267 268 #if defined(PPP_WITH_EAPTLS) 269 char *cert_file = NULL; /* Client certificate file (pem format) */ 270 char *privkey_file = NULL; /* Client private key file (pem format) */ 271 char *pkcs12_file = NULL; /* Client private key envelope file (pkcs12 format) */ 272 bool need_peer_eap = 0; /* Require peer to authenticate us */ 273 #endif 274 275 static char *fname; /* name of most recent +ua file */ 276 277 /* Prototypes for procedures local to this file. */ 278 279 static void network_phase (int); 280 static void check_idle (void *); 281 static void connect_time_expired (void *); 282 static int null_login (int); 283 static int get_pap_passwd (char *); 284 static int have_pap_secret (int *); 285 static int have_chap_secret (char *, char *, int, int *); 286 static int have_srp_secret(char *client, char *server, int need_ip, 287 int *lacks_ipp); 288 289 #ifdef PPP_WITH_EAPTLS 290 static int have_eaptls_secret_server 291 (char *client, char *server, int need_ip, int *lacks_ipp); 292 static int have_eaptls_secret_client (char *client, char *server); 293 static int scan_authfile_eaptls(FILE * f, char *client, char *server, 294 char *cli_cert, char *serv_cert, 295 char *ca_cert, char *pk, 296 struct wordlist ** addrs, 297 struct wordlist ** opts, 298 char *filename, int flags); 299 #endif 300 301 static int ip_addr_check (u_int32_t, struct permitted_ip *); 302 static int scan_authfile(FILE *, char *, char *, char *, 303 struct wordlist **, struct wordlist **, 304 char *, int); 305 static void free_wordlist (struct wordlist *); 306 static void auth_script (char *); 307 static void auth_script_done (void *); 308 static void set_allowed_addrs (int, struct wordlist *, struct wordlist *); 309 static int some_ip_ok (struct wordlist *); 310 static int setupapfile (char **); 311 static int privgroup (char **); 312 static int set_noauth_addr (char **); 313 static int set_permitted_number (char **); 314 static void check_access (FILE *, char *); 315 static int wordlist_count (struct wordlist *); 316 static void check_maxoctets (void *); 317 318 /* 319 * Authentication-related options. 320 */ 321 struct option auth_options[] = { 322 { "auth", o_bool, &auth_required, 323 "Require authentication from peer", OPT_PRIO | 1 }, 324 { "noauth", o_bool, &auth_required, 325 "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV, 326 &allow_any_ip }, 327 { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap, 328 "Require PAP authentication from peer", 329 OPT_PRIOSUB | 1, &auth_required }, 330 { "+pap", o_bool, &lcp_wantoptions[0].neg_upap, 331 "Require PAP authentication from peer", 332 OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required }, 333 { "require-chap", o_bool, &auth_required, 334 "Require CHAP authentication from peer", 335 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5, 336 &lcp_wantoptions[0].chap_mdtype }, 337 { "+chap", o_bool, &auth_required, 338 "Require CHAP authentication from peer", 339 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5, 340 &lcp_wantoptions[0].chap_mdtype }, 341 #ifdef PPP_WITH_CHAPMS 342 { "require-mschap", o_bool, &auth_required, 343 "Require MS-CHAP authentication from peer", 344 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT, 345 &lcp_wantoptions[0].chap_mdtype }, 346 { "+mschap", o_bool, &auth_required, 347 "Require MS-CHAP authentication from peer", 348 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT, 349 &lcp_wantoptions[0].chap_mdtype }, 350 { "require-mschap-v2", o_bool, &auth_required, 351 "Require MS-CHAPv2 authentication from peer", 352 OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2, 353 &lcp_wantoptions[0].chap_mdtype }, 354 { "+mschap-v2", o_bool, &auth_required, 355 "Require MS-CHAPv2 authentication from peer", 356 OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2, 357 &lcp_wantoptions[0].chap_mdtype }, 358 #endif 359 360 { "refuse-pap", o_bool, &refuse_pap, 361 "Don't agree to auth to peer with PAP", 1 }, 362 { "-pap", o_bool, &refuse_pap, 363 "Don't allow PAP authentication with peer", OPT_ALIAS | 1 }, 364 { "refuse-chap", o_bool, &refuse_chap, 365 "Don't agree to auth to peer with CHAP", 366 OPT_A2CLRB | MDTYPE_MD5, 367 &lcp_allowoptions[0].chap_mdtype }, 368 { "-chap", o_bool, &refuse_chap, 369 "Don't allow CHAP authentication with peer", 370 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5, 371 &lcp_allowoptions[0].chap_mdtype }, 372 #ifdef PPP_WITH_CHAPMS 373 { "refuse-mschap", o_bool, &refuse_mschap, 374 "Don't agree to auth to peer with MS-CHAP", 375 OPT_A2CLRB | MDTYPE_MICROSOFT, 376 &lcp_allowoptions[0].chap_mdtype }, 377 { "-mschap", o_bool, &refuse_mschap, 378 "Don't allow MS-CHAP authentication with peer", 379 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT, 380 &lcp_allowoptions[0].chap_mdtype }, 381 { "refuse-mschap-v2", o_bool, &refuse_mschap_v2, 382 "Don't agree to auth to peer with MS-CHAPv2", 383 OPT_A2CLRB | MDTYPE_MICROSOFT_V2, 384 &lcp_allowoptions[0].chap_mdtype }, 385 { "-mschap-v2", o_bool, &refuse_mschap_v2, 386 "Don't allow MS-CHAPv2 authentication with peer", 387 OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2, 388 &lcp_allowoptions[0].chap_mdtype }, 389 #endif 390 391 { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap, 392 "Require EAP authentication from peer", OPT_PRIOSUB | 1, 393 &auth_required }, 394 { "refuse-eap", o_bool, &refuse_eap, 395 "Don't agree to authenticate to peer with EAP", 1 }, 396 397 { "name", o_string, our_name, 398 "Set local name for authentication", 399 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN }, 400 401 { "+ua", o_special, (void *)setupapfile, 402 "Get PAP user and password from file", 403 OPT_PRIO | OPT_A2STRVAL, &fname }, 404 405 { "user", o_string, user, 406 "Set name for auth with peer", OPT_PRIO | OPT_STATIC, 407 &explicit_user, MAXNAMELEN }, 408 409 { "password", o_string, passwd, 410 "Password for authenticating us to the peer", 411 OPT_PRIO | OPT_STATIC | OPT_HIDE, 412 &explicit_passwd, MAXSECRETLEN }, 413 414 { "usehostname", o_bool, &usehostname, 415 "Must use hostname for authentication", 1 }, 416 417 { "remotename", o_string, remote_name, 418 "Set remote name for authentication", OPT_PRIO | OPT_STATIC, 419 &explicit_remote, MAXNAMELEN }, 420 421 { "pap-secrets", o_string, path_upapfile, 422 "Set pathname of pap-secrets", OPT_PRIO | OPT_PRIV | OPT_STATIC, 423 NULL, MAXPATHLEN }, 424 425 { "chap-secrets", o_string, path_chapfile, 426 "Set pathname of chap-secrets", OPT_PRIO | OPT_PRIV | OPT_STATIC, 427 NULL, MAXPATHLEN }, 428 429 { "login", o_bool, &uselogin, 430 "Use system password database for PAP", OPT_A2COPY | 1 , 431 &session_mgmt }, 432 { "enable-session", o_bool, &session_mgmt, 433 "Enable session accounting for remote peers", OPT_PRIV | 1 }, 434 435 { "papcrypt", o_bool, &cryptpap, 436 "PAP passwords are encrypted", 1 }, 437 438 { "privgroup", o_special, (void *)privgroup, 439 "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST }, 440 441 { "allow-ip", o_special, (void *)set_noauth_addr, 442 "Set IP address(es) which can be used without authentication", 443 OPT_PRIV | OPT_A2LIST }, 444 445 { "remotenumber", o_string, remote_number, 446 "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC, 447 NULL, MAXNAMELEN }, 448 449 { "allow-number", o_special, (void *)set_permitted_number, 450 "Set telephone number(s) which are allowed to connect", 451 OPT_PRIV | OPT_A2LIST }, 452 453 #if defined(PPP_WITH_EAPTLS) || defined(PPP_WITH_PEAP) 454 { "ca", o_string, &cacert_file, "CA certificate in PEM format" }, 455 { "capath", o_string, &ca_path, "TLS CA certificate directory" }, 456 { "crl-dir", o_string, &crl_dir, "Use CRLs in directory" }, 457 { "crl", o_string, &crl_file, "Use specific CRL file" }, 458 { "max-tls-version", o_string, &max_tls_version, 459 "Maximum TLS version (1.0/1.1/1.2 (default)/1.3)" }, 460 { "tls-verify-key-usage", o_bool, &tls_verify_key_usage, 461 "Verify certificate type and extended key usage" }, 462 { "tls-verify-method", o_string, &tls_verify_method, 463 "Verify peer by method (none|subject|name|suffix)" }, 464 #endif 465 466 #if defined(PPP_WITH_EAPTLS) 467 { "cert", o_string, &cert_file, "client certificate in PEM format" }, 468 { "key", o_string, &privkey_file, "client private key in PEM format" }, 469 { "pkcs12", o_string, &pkcs12_file, "EAP-TLS client credentials in PKCS12 format" }, 470 { "need-peer-eap", o_bool, &need_peer_eap, 471 "Require the peer to authenticate us", 1 }, 472 #endif /* PPP_WITH_EAPTLS */ 473 { NULL } 474 }; 475 476 const char * 477 ppp_remote_name() 478 { 479 return remote_name; 480 } 481 482 const char * 483 ppp_get_remote_number(void) 484 { 485 return remote_number; 486 } 487 488 void 489 ppp_set_remote_number(const char *buf) 490 { 491 if (buf) { 492 strlcpy(remote_number, buf, sizeof(remote_number)); 493 ppp_script_setenv("REMOTENUMBER", remote_number, 0); 494 } 495 } 496 497 const char * 498 ppp_peer_authname(char *buf, size_t bufsz) 499 { 500 if (buf && bufsz > 0) { 501 strlcpy(buf, peer_authname, bufsz); 502 return buf; 503 } 504 return peer_authname; 505 } 506 507 /* 508 * setupapfile - specifies UPAP info for authenticating with peer. 509 */ 510 static int 511 setupapfile(char **argv) 512 { 513 FILE *ufile; 514 int l; 515 uid_t euid; 516 char u[MAXNAMELEN], p[MAXSECRETLEN]; 517 518 lcp_allowoptions[0].neg_upap = 1; 519 520 if (*argv == NULL) 521 novm("+ua file name"); 522 523 if (fname != NULL) 524 free(fname); 525 /* open user info file */ 526 fname = strdup(*argv); 527 if (fname == NULL) 528 novm("+ua file name"); 529 euid = geteuid(); 530 if (seteuid(getuid()) == -1) { 531 ppp_option_error("unable to reset uid before opening %s: %m", fname); 532 free(fname); 533 return 0; 534 } 535 ufile = fopen(fname, "r"); 536 if (seteuid(euid) == -1) 537 fatal("unable to regain privileges: %m"); 538 if (ufile == NULL) { 539 ppp_option_error("unable to open user login data file %s", fname); 540 free(fname); 541 return 0; 542 } 543 check_access(ufile, fname); 544 545 /* get username */ 546 if (fgets(u, MAXNAMELEN - 1, ufile) == NULL 547 || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) { 548 fclose(ufile); 549 ppp_option_error("unable to read user login data file %s", fname); 550 free(fname); 551 return 0; 552 } 553 fclose(ufile); 554 555 /* get rid of newlines */ 556 l = strlen(u); 557 if (l > 0 && u[l-1] == '\n') 558 u[l-1] = 0; 559 l = strlen(p); 560 if (l > 0 && p[l-1] == '\n') 561 p[l-1] = 0; 562 563 if (override_value("user", option_priority, fname)) { 564 strlcpy(user, u, sizeof(user)); 565 explicit_user = 1; 566 } 567 if (override_value("passwd", option_priority, fname)) { 568 strlcpy(passwd, p, sizeof(passwd)); 569 explicit_passwd = 1; 570 } 571 572 free(fname); 573 return (1); 574 } 575 576 577 /* 578 * privgroup - allow members of the group to have privileged access. 579 */ 580 static int 581 privgroup(char **argv) 582 { 583 struct group *g; 584 int i; 585 586 g = getgrnam(*argv); 587 if (g == 0) { 588 ppp_option_error("group %s is unknown", *argv); 589 return 0; 590 } 591 for (i = 0; i < ngroups; ++i) { 592 if (groups[i] == g->gr_gid) { 593 privileged = 1; 594 break; 595 } 596 } 597 return 1; 598 } 599 600 601 /* 602 * set_noauth_addr - set address(es) that can be used without authentication. 603 * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets. 604 */ 605 static int 606 set_noauth_addr(char **argv) 607 { 608 char *addr = *argv; 609 int l = strlen(addr) + 1; 610 struct wordlist *wp; 611 612 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l); 613 if (wp == NULL) 614 novm("allow-ip argument"); 615 wp->word = (char *) (wp + 1); 616 wp->next = noauth_addrs; 617 BCOPY(addr, wp->word, l); 618 noauth_addrs = wp; 619 return 1; 620 } 621 622 623 /* 624 * set_permitted_number - set remote telephone number(s) that may connect. 625 */ 626 static int 627 set_permitted_number(char **argv) 628 { 629 char *number = *argv; 630 int l = strlen(number) + 1; 631 struct wordlist *wp; 632 633 wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l); 634 if (wp == NULL) 635 novm("allow-number argument"); 636 wp->word = (char *) (wp + 1); 637 wp->next = permitted_numbers; 638 BCOPY(number, wp->word, l); 639 permitted_numbers = wp; 640 return 1; 641 } 642 643 644 /* 645 * An Open on LCP has requested a change from Dead to Establish phase. 646 */ 647 void 648 link_required(int unit) 649 { 650 } 651 652 /* 653 * Bring the link up to the point of being able to do ppp. 654 */ 655 void start_link(int unit) 656 { 657 ppp_set_status(EXIT_CONNECT_FAILED); 658 new_phase(PHASE_SERIALCONN); 659 660 hungup = 0; 661 devfd = the_channel->connect(); 662 if (devfd < 0) 663 goto fail; 664 665 /* set up the serial device as a ppp interface */ 666 /* 667 * N.B. we used to do tdb_writelock/tdb_writeunlock around this 668 * (from establish_ppp to set_ifunit). However, we won't be 669 * doing the set_ifunit in multilink mode, which is the only time 670 * we need the atomicity that the tdb_writelock/tdb_writeunlock 671 * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock. 672 */ 673 fd_ppp = the_channel->establish_ppp(devfd); 674 if (fd_ppp < 0) { 675 ppp_set_status(EXIT_FATAL_ERROR); 676 goto disconnect; 677 } 678 679 if (!demand && ifunit >= 0) 680 set_ifunit(1); 681 682 /* 683 * Start opening the connection and wait for 684 * incoming events (reply, timeout, etc.). 685 */ 686 if (ifunit >= 0) 687 notice("Connect: %s <--> %s", ifname, ppp_devname); 688 else 689 notice("Starting negotiation on %s", ppp_devname); 690 add_fd(fd_ppp); 691 692 ppp_set_status(EXIT_NEGOTIATION_FAILED); 693 new_phase(PHASE_ESTABLISH); 694 695 lcp_lowerup(0); 696 return; 697 698 disconnect: 699 new_phase(PHASE_DISCONNECT); 700 if (the_channel->disconnect) 701 the_channel->disconnect(); 702 703 fail: 704 new_phase(PHASE_DEAD); 705 if (the_channel->cleanup) 706 (*the_channel->cleanup)(); 707 } 708 709 /* 710 * LCP has terminated the link; go to the Dead phase and take the 711 * physical layer down. 712 */ 713 void 714 link_terminated(int unit) 715 { 716 if (in_phase(PHASE_DEAD) || in_phase(PHASE_MASTER)) 717 return; 718 new_phase(PHASE_DISCONNECT); 719 720 if (pap_logout_hook) { 721 pap_logout_hook(); 722 } 723 session_end(devnam); 724 725 if (!mp_on()) { 726 notice("Connection terminated."); 727 print_link_stats(); 728 } else 729 notice("Link terminated."); 730 731 /* 732 * Delete pid files before disestablishing ppp. Otherwise it 733 * can happen that another pppd gets the same unit and then 734 * we delete its pid file. 735 */ 736 if (!demand && !mp_on()) 737 remove_pidfiles(); 738 /* 739 * If we may want to bring the link up again, transfer 740 * the ppp unit back to the loopback. Set the 741 * real serial device back to its normal mode of operation. 742 */ 743 if (fd_ppp >= 0) { 744 remove_fd(fd_ppp); 745 clean_check(); 746 the_channel->disestablish_ppp(devfd); 747 if (mp_on()) 748 mp_exit_bundle(); 749 fd_ppp = -1; 750 } 751 if (!hungup) 752 lcp_lowerdown(0); 753 if (!mp_on() && !demand) 754 ppp_script_unsetenv("IFNAME"); 755 756 /* 757 * Run disconnector script, if requested. 758 * XXX we may not be able to do this if the line has hung up! 759 */ 760 if (devfd >= 0 && the_channel->disconnect) { 761 the_channel->disconnect(); 762 devfd = -1; 763 } 764 if (the_channel->cleanup) 765 (*the_channel->cleanup)(); 766 767 if (mp_on() && mp_master()) { 768 if (!bundle_terminating) { 769 new_phase(PHASE_MASTER); 770 if (master_detach && !detached) 771 detach(); 772 } else 773 mp_bundle_terminated(); 774 } else 775 new_phase(PHASE_DEAD); 776 } 777 778 /* 779 * LCP has gone down; it will either die or try to re-establish. 780 */ 781 void 782 link_down(int unit) 783 { 784 if (auth_state != s_down) { 785 notify(link_down_notifier, 0); 786 auth_state = s_down; 787 if (auth_script_state == s_up && auth_script_pid == 0) { 788 ppp_get_link_stats(NULL); 789 auth_script_state = s_down; 790 auth_script(PPP_PATH_AUTHDOWN); 791 } 792 } 793 if (!mp_on()) 794 { 795 upper_layers_down(unit); 796 if (!in_phase(PHASE_DEAD) && !in_phase(PHASE_MASTER)) 797 new_phase(PHASE_ESTABLISH); 798 } 799 /* XXX if doing_multilink, should do something to stop 800 network-layer traffic on the link */ 801 } 802 803 void upper_layers_down(int unit) 804 { 805 int i; 806 struct protent *protp; 807 808 for (i = 0; (protp = protocols[i]) != NULL; ++i) { 809 if (!protp->enabled_flag) 810 continue; 811 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL) 812 (*protp->lowerdown)(unit); 813 if (protp->protocol < 0xC000 && protp->close != NULL) 814 (*protp->close)(unit, "LCP down"); 815 } 816 num_np_open = 0; 817 num_np_up = 0; 818 } 819 820 /* 821 * The link is established. 822 * Proceed to the Dead, Authenticate or Network phase as appropriate. 823 */ 824 void 825 link_established(int unit) 826 { 827 int auth; 828 lcp_options *wo = &lcp_wantoptions[unit]; 829 lcp_options *go = &lcp_gotoptions[unit]; 830 lcp_options *ho = &lcp_hisoptions[unit]; 831 #ifdef PPP_WITH_EAPTLS 832 lcp_options *ao = &lcp_allowoptions[unit]; 833 #endif 834 int i; 835 struct protent *protp; 836 837 /* 838 * Tell higher-level protocols that LCP is up. 839 */ 840 if (!mp_on()) 841 for (i = 0; (protp = protocols[i]) != NULL; ++i) 842 if (protp->protocol != PPP_LCP && protp->enabled_flag 843 && protp->lowerup != NULL) 844 (*protp->lowerup)(unit); 845 if (!auth_required && noauth_addrs != NULL) 846 set_allowed_addrs(unit, NULL, NULL); 847 848 if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) { 849 /* 850 * We wanted the peer to authenticate itself, and it refused: 851 * if we have some address(es) it can use without auth, fine, 852 * otherwise treat it as though it authenticated with PAP using 853 * a username of "" and a password of "". If that's not OK, 854 * boot it out. 855 */ 856 if (noauth_addrs != NULL) { 857 set_allowed_addrs(unit, NULL, NULL); 858 } else if (!wo->neg_upap || uselogin || !null_login(unit)) { 859 warn("peer refused to authenticate: terminating link"); 860 ppp_set_status(EXIT_PEER_AUTH_FAILED); 861 lcp_close(unit, "peer refused to authenticate"); 862 return; 863 } 864 } 865 866 #ifdef PPP_WITH_EAPTLS 867 if (need_peer_eap && !ao->neg_eap) { 868 warn("eap required to authenticate us but no suitable secrets"); 869 lcp_close(unit, "couldn't negotiate eap"); 870 ppp_set_status(EXIT_AUTH_TOPEER_FAILED); 871 return; 872 } 873 874 if (need_peer_eap && !ho->neg_eap) { 875 warn("peer doesn't want to authenticate us with eap"); 876 lcp_close(unit, "couldn't negotiate eap"); 877 ppp_set_status(EXIT_PEER_AUTH_FAILED); 878 return; 879 } 880 #endif 881 882 new_phase(PHASE_AUTHENTICATE); 883 auth = 0; 884 if (go->neg_eap) { 885 eap_authpeer(unit, our_name); 886 auth |= EAP_PEER; 887 } else if (go->neg_chap) { 888 chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype)); 889 auth |= CHAP_PEER; 890 } else if (go->neg_upap) { 891 upap_authpeer(unit); 892 auth |= PAP_PEER; 893 } 894 if (ho->neg_eap) { 895 eap_authwithpeer(unit, user); 896 auth |= EAP_WITHPEER; 897 } else if (ho->neg_chap) { 898 chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype)); 899 auth |= CHAP_WITHPEER; 900 } else if (ho->neg_upap) { 901 /* If a blank password was explicitly given as an option, trust 902 the user and don't try to look up one. */ 903 if (passwd[0] == 0 && !explicit_passwd) { 904 passwd_from_file = 1; 905 if (!get_pap_passwd(passwd)) 906 error("No secret found for PAP login"); 907 } 908 upap_authwithpeer(unit, user, passwd); 909 auth |= PAP_WITHPEER; 910 } 911 auth_pending[unit] = auth; 912 auth_done[unit] = 0; 913 914 if (!auth) 915 network_phase(unit); 916 } 917 918 /* 919 * Proceed to the network phase. 920 */ 921 static void 922 network_phase(int unit) 923 { 924 lcp_options *go = &lcp_gotoptions[unit]; 925 926 /* Log calling number. */ 927 if (*remote_number) 928 notice("peer from calling number %q authorized", remote_number); 929 930 /* 931 * If the peer had to authenticate, run the auth-up script now. 932 */ 933 notify(auth_up_notifier, 0); 934 if (go->neg_chap || go->neg_upap || go->neg_eap) { 935 auth_state = s_up; 936 if (auth_script_state == s_down && auth_script_pid == 0) { 937 auth_script_state = s_up; 938 auth_script(PPP_PATH_AUTHUP); 939 } 940 } 941 942 #ifdef PPP_WITH_CBCP 943 /* 944 * If we negotiated callback, do it now. 945 */ 946 if (go->neg_cbcp) { 947 new_phase(PHASE_CALLBACK); 948 (*cbcp_protent.open)(unit); 949 return; 950 } 951 #endif 952 953 /* 954 * Process extra options from the secrets file 955 */ 956 if (extra_options) { 957 options_from_list(extra_options, 1); 958 free_wordlist(extra_options); 959 extra_options = 0; 960 } 961 start_networks(unit); 962 } 963 964 void 965 start_networks(int unit) 966 { 967 int i; 968 struct protent *protp; 969 int ecp_required, mppe_required; 970 971 new_phase(PHASE_NETWORK); 972 973 #ifdef PPP_WITH_MULTILINK 974 if (multilink) { 975 if (mp_join_bundle()) { 976 if (multilink_join_hook) 977 (*multilink_join_hook)(); 978 if (updetach && !nodetach) 979 detach(); 980 return; 981 } 982 } 983 #endif /* PPP_WITH_MULTILINK */ 984 985 #ifdef PPP_WITH_FILTER 986 if (!demand) 987 set_filters(&pass_filter_in, &pass_filter_out, 988 &active_filter_in, &active_filter_out); 989 #endif 990 /* Start CCP and ECP */ 991 for (i = 0; (protp = protocols[i]) != NULL; ++i) 992 if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP) 993 && protp->enabled_flag && protp->open != NULL) 994 (*protp->open)(0); 995 996 /* 997 * Bring up other network protocols iff encryption is not required. 998 */ 999 ecp_required = ecp_gotoptions[unit].required; 1000 mppe_required = ccp_gotoptions[unit].mppe; 1001 if (!ecp_required && !mppe_required) 1002 continue_networks(unit); 1003 } 1004 1005 void 1006 continue_networks(int unit) 1007 { 1008 int i; 1009 struct protent *protp; 1010 1011 /* 1012 * Start the "real" network protocols. 1013 */ 1014 for (i = 0; (protp = protocols[i]) != NULL; ++i) 1015 if (protp->protocol < 0xC000 1016 && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP 1017 && protp->enabled_flag && protp->open != NULL) { 1018 (*protp->open)(0); 1019 ++num_np_open; 1020 } 1021 1022 if (num_np_open == 0) 1023 /* nothing to do */ 1024 lcp_close(0, "No network protocols running"); 1025 } 1026 1027 /* 1028 * The peer has failed to authenticate himself using `protocol'. 1029 */ 1030 void 1031 auth_peer_fail(int unit, int protocol) 1032 { 1033 /* 1034 * Authentication failure: take the link down 1035 */ 1036 ppp_set_status(EXIT_PEER_AUTH_FAILED); 1037 lcp_close(unit, "Authentication failed"); 1038 } 1039 1040 /* 1041 * The peer has been successfully authenticated using `protocol'. 1042 */ 1043 void 1044 auth_peer_success(int unit, int protocol, int prot_flavor, 1045 char *name, int namelen) 1046 { 1047 int bit; 1048 const char *prot; 1049 1050 switch (protocol) { 1051 case PPP_CHAP: 1052 bit = CHAP_PEER; 1053 prot = "CHAP"; 1054 switch (prot_flavor) { 1055 case CHAP_MD5: 1056 bit |= CHAP_MD5_PEER; 1057 break; 1058 #ifdef PPP_WITH_CHAPMS 1059 case CHAP_MICROSOFT: 1060 bit |= CHAP_MS_PEER; 1061 break; 1062 case CHAP_MICROSOFT_V2: 1063 bit |= CHAP_MS2_PEER; 1064 break; 1065 #endif 1066 } 1067 break; 1068 case PPP_PAP: 1069 bit = PAP_PEER; 1070 prot = "PAP"; 1071 break; 1072 case PPP_EAP: 1073 bit = EAP_PEER; 1074 prot = "EAP"; 1075 break; 1076 default: 1077 warn("auth_peer_success: unknown protocol %x", protocol); 1078 prot = "unknown protocol"; 1079 return; 1080 } 1081 1082 /* 1083 * Save the authenticated name of the peer for later. 1084 */ 1085 if (namelen > sizeof(peer_authname) - 1) 1086 namelen = sizeof(peer_authname) - 1; 1087 BCOPY(name, peer_authname, namelen); 1088 peer_authname[namelen] = 0; 1089 ppp_script_setenv("PEERNAME", peer_authname, 0); 1090 notice("Peer %q authenticated with %s", peer_authname, prot); 1091 1092 /* Save the authentication method for later. */ 1093 auth_done[unit] |= bit; 1094 1095 /* 1096 * If there is no more authentication still to be done, 1097 * proceed to the network (or callback) phase. 1098 */ 1099 if ((auth_pending[unit] &= ~bit) == 0) 1100 network_phase(unit); 1101 } 1102 1103 /* 1104 * We have failed to authenticate ourselves to the peer using `protocol'. 1105 */ 1106 void 1107 auth_withpeer_fail(int unit, int protocol) 1108 { 1109 if (passwd_from_file) 1110 BZERO(passwd, MAXSECRETLEN); 1111 /* 1112 * We've failed to authenticate ourselves to our peer. 1113 * Some servers keep sending CHAP challenges, but there 1114 * is no point in persisting without any way to get updated 1115 * authentication secrets. 1116 */ 1117 ppp_set_status(EXIT_AUTH_TOPEER_FAILED); 1118 lcp_close(unit, "Failed to authenticate ourselves to peer"); 1119 } 1120 1121 /* 1122 * We have successfully authenticated ourselves with the peer using `protocol'. 1123 */ 1124 void 1125 auth_withpeer_success(int unit, int protocol, int prot_flavor) 1126 { 1127 int bit; 1128 const char *prot = ""; 1129 1130 switch (protocol) { 1131 case PPP_CHAP: 1132 bit = CHAP_WITHPEER; 1133 prot = "CHAP"; 1134 switch (prot_flavor) { 1135 case CHAP_MD5: 1136 bit |= CHAP_MD5_WITHPEER; 1137 break; 1138 #ifdef PPP_WITH_CHAPMS 1139 case CHAP_MICROSOFT: 1140 bit |= CHAP_MS_WITHPEER; 1141 break; 1142 case CHAP_MICROSOFT_V2: 1143 bit |= CHAP_MS2_WITHPEER; 1144 break; 1145 #endif 1146 } 1147 break; 1148 case PPP_PAP: 1149 if (passwd_from_file) 1150 BZERO(passwd, MAXSECRETLEN); 1151 bit = PAP_WITHPEER; 1152 prot = "PAP"; 1153 break; 1154 case PPP_EAP: 1155 bit = EAP_WITHPEER; 1156 prot = "EAP"; 1157 break; 1158 default: 1159 warn("auth_withpeer_success: unknown protocol %x", protocol); 1160 bit = 0; 1161 } 1162 1163 notice("%s authentication succeeded", prot); 1164 1165 /* Save the authentication method for later. */ 1166 auth_done[unit] |= bit; 1167 1168 /* 1169 * If there is no more authentication still being done, 1170 * proceed to the network (or callback) phase. 1171 */ 1172 if ((auth_pending[unit] &= ~bit) == 0) 1173 network_phase(unit); 1174 } 1175 1176 1177 /* 1178 * np_up - a network protocol has come up. 1179 */ 1180 void 1181 np_up(int unit, int proto) 1182 { 1183 int tlim; 1184 1185 if (num_np_up == 0) { 1186 /* 1187 * At this point we consider that the link has come up successfully. 1188 */ 1189 ppp_set_status(EXIT_OK); 1190 unsuccess = 0; 1191 new_phase(PHASE_RUNNING); 1192 1193 if (idle_time_hook != 0) 1194 tlim = (*idle_time_hook)(NULL); 1195 else 1196 tlim = ppp_get_max_idle_time(); 1197 if (tlim > 0) 1198 TIMEOUT(check_idle, NULL, tlim); 1199 1200 /* 1201 * Set a timeout to close the connection once the maximum 1202 * connect time has expired. 1203 */ 1204 if (ppp_get_max_connect_time() > 0) 1205 TIMEOUT(connect_time_expired, 0, ppp_get_max_connect_time()); 1206 1207 /* 1208 * Configure a check to see if session has outlived it's limit 1209 * in terms of octets 1210 */ 1211 if (maxoctets > 0) 1212 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout); 1213 1214 /* 1215 * Detach now, if the updetach option was given. 1216 */ 1217 if (updetach && !nodetach) { 1218 dbglog("updetach is set. Now detaching."); 1219 detach(); 1220 #ifdef SYSTEMD 1221 } else if (nodetach && up_sdnotify) { 1222 dbglog("up_sdnotify is set. Now notifying systemd: READY=1"); 1223 sd_notify(0, "READY=1"); 1224 #endif 1225 } 1226 } 1227 ++num_np_up; 1228 } 1229 1230 /* 1231 * np_down - a network protocol has gone down. 1232 */ 1233 void 1234 np_down(int unit, int proto) 1235 { 1236 if (--num_np_up == 0) { 1237 UNTIMEOUT(check_idle, NULL); 1238 UNTIMEOUT(connect_time_expired, NULL); 1239 UNTIMEOUT(check_maxoctets, NULL); 1240 new_phase(PHASE_NETWORK); 1241 } 1242 } 1243 1244 /* 1245 * np_finished - a network protocol has finished using the link. 1246 */ 1247 void 1248 np_finished(int unit, int proto) 1249 { 1250 if (--num_np_open <= 0) { 1251 /* no further use for the link: shut up shop. */ 1252 lcp_close(0, "No network protocols running"); 1253 } 1254 } 1255 1256 /* 1257 * Periodic callback to check if session has reached its limit. The period defaults 1258 * to 1 second and is configurable by setting "mo-timeout" in configuration 1259 */ 1260 static void 1261 check_maxoctets(void *arg) 1262 { 1263 unsigned int used = 0; 1264 ppp_link_stats_st stats; 1265 1266 if (ppp_get_link_stats(&stats)) { 1267 switch(maxoctets_dir) { 1268 case PPP_OCTETS_DIRECTION_IN: 1269 used = stats.bytes_in; 1270 break; 1271 case PPP_OCTETS_DIRECTION_OUT: 1272 used = stats.bytes_out; 1273 break; 1274 case PPP_OCTETS_DIRECTION_MAXOVERAL: 1275 case PPP_OCTETS_DIRECTION_MAXSESSION: 1276 used = (stats.bytes_in > stats.bytes_out) 1277 ? stats.bytes_in 1278 : stats.bytes_out; 1279 break; 1280 default: 1281 used = stats.bytes_in+stats.bytes_out; 1282 break; 1283 } 1284 } 1285 1286 if (used > maxoctets) { 1287 notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used); 1288 ppp_set_status(EXIT_TRAFFIC_LIMIT); 1289 lcp_close(0, "Traffic limit"); 1290 link_stats_print = 0; 1291 need_holdoff = 0; 1292 } else { 1293 TIMEOUT(check_maxoctets, NULL, maxoctets_timeout); 1294 } 1295 } 1296 1297 /* 1298 * check_idle - check whether the link has been idle for long 1299 * enough that we can shut it down. 1300 */ 1301 static void 1302 check_idle(void *arg) 1303 { 1304 struct ppp_idle idle; 1305 time_t itime; 1306 int tlim; 1307 1308 if (!get_idle_time(0, &idle)) 1309 return; 1310 if (idle_time_hook != 0) { 1311 tlim = idle_time_hook(&idle); 1312 } else { 1313 itime = MIN(idle.xmit_idle, idle.recv_idle); 1314 tlim = ppp_get_max_idle_time() - itime; 1315 } 1316 if (tlim <= 0) { 1317 /* link is idle: shut it down. */ 1318 notice("Terminating connection due to lack of activity."); 1319 ppp_set_status(EXIT_IDLE_TIMEOUT); 1320 lcp_close(0, "Link inactive"); 1321 need_holdoff = 0; 1322 } else { 1323 TIMEOUT(check_idle, NULL, tlim); 1324 } 1325 } 1326 1327 /* 1328 * connect_time_expired - log a message and close the connection. 1329 */ 1330 static void 1331 connect_time_expired(void *arg) 1332 { 1333 info("Connect time expired"); 1334 ppp_set_status(EXIT_CONNECT_TIME); 1335 lcp_close(0, "Connect time expired"); /* Close connection */ 1336 need_holdoff = 0; 1337 } 1338 1339 /* 1340 * auth_check_options - called to check authentication options. 1341 */ 1342 void 1343 auth_check_options(void) 1344 { 1345 lcp_options *wo = &lcp_wantoptions[0]; 1346 int can_auth; 1347 int lacks_ip; 1348 1349 /* Default our_name to hostname, and user to our_name */ 1350 if (our_name[0] == 0 || usehostname) 1351 strlcpy(our_name, hostname, sizeof(our_name)); 1352 1353 /* If a blank username was explicitly given as an option, trust 1354 the user and don't use our_name */ 1355 if (user[0] == 0 && !explicit_user) 1356 strlcpy(user, our_name, sizeof(user)); 1357 1358 #if defined(SYSTEM_CA_PATH) && (defined(PPP_WITH_EAPTLS) || defined(PPP_WITH_PEAP)) 1359 /* Use system default for CA Path if not specified */ 1360 if (!ca_path) { 1361 ca_path = SYSTEM_CA_PATH; 1362 } 1363 #endif 1364 1365 /* 1366 * If we have a default route, require the peer to authenticate 1367 * unless the noauth option was given or the real user is root. 1368 */ 1369 if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) { 1370 auth_required = 1; 1371 default_auth = 1; 1372 } 1373 1374 /* If we selected any CHAP flavors, we should probably negotiate it. :-) */ 1375 if (wo->chap_mdtype) 1376 wo->neg_chap = 1; 1377 1378 /* If authentication is required, ask peer for CHAP, PAP, or EAP. */ 1379 if (auth_required) { 1380 allow_any_ip = 0; 1381 if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) { 1382 wo->neg_chap = chap_mdtype_all != MDTYPE_NONE; 1383 wo->chap_mdtype = chap_mdtype_all; 1384 wo->neg_upap = 1; 1385 wo->neg_eap = 1; 1386 } 1387 } else { 1388 wo->neg_chap = 0; 1389 wo->chap_mdtype = MDTYPE_NONE; 1390 wo->neg_upap = 0; 1391 wo->neg_eap = 0; 1392 } 1393 1394 /* 1395 * Check whether we have appropriate secrets to use 1396 * to authenticate the peer. Note that EAP can authenticate by way 1397 * of a CHAP-like exchanges as well as SRP. 1398 */ 1399 lacks_ip = 0; 1400 can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip)); 1401 if (!can_auth && (wo->neg_chap || wo->neg_eap)) { 1402 can_auth = have_chap_secret((explicit_remote? remote_name: NULL), 1403 our_name, 1, &lacks_ip); 1404 } 1405 if (!can_auth && wo->neg_eap) { 1406 can_auth = have_srp_secret((explicit_remote? remote_name: NULL), 1407 our_name, 1, &lacks_ip); 1408 } 1409 1410 #ifdef PPP_WITH_EAPTLS 1411 if (!can_auth && wo->neg_eap) { 1412 can_auth = 1413 have_eaptls_secret_server((explicit_remote ? remote_name : 1414 NULL), our_name, 1, &lacks_ip); 1415 1416 } 1417 #endif 1418 1419 if (auth_required && !can_auth && noauth_addrs == NULL) { 1420 if (default_auth) { 1421 ppp_option_error( 1422 "By default the remote system is required to authenticate itself"); 1423 ppp_option_error( 1424 "(because this system has a default route to the internet)"); 1425 } else if (explicit_remote) 1426 ppp_option_error( 1427 "The remote system (%s) is required to authenticate itself", 1428 remote_name); 1429 else 1430 ppp_option_error( 1431 "The remote system is required to authenticate itself"); 1432 ppp_option_error( 1433 "but I couldn't find any suitable secret (password) for it to use to do so."); 1434 if (lacks_ip) 1435 ppp_option_error( 1436 "(None of the available passwords would let it use an IP address.)"); 1437 1438 exit(1); 1439 } 1440 1441 /* 1442 * Early check for remote number authorization. 1443 */ 1444 if (!auth_number()) { 1445 warn("calling number %q is not authorized", remote_number); 1446 exit(EXIT_CNID_AUTH_FAILED); 1447 } 1448 } 1449 1450 /* 1451 * auth_reset - called when LCP is starting negotiations to recheck 1452 * authentication options, i.e. whether we have appropriate secrets 1453 * to use for authenticating ourselves and/or the peer. 1454 */ 1455 void 1456 auth_reset(int unit) 1457 { 1458 lcp_options *go = &lcp_gotoptions[unit]; 1459 lcp_options *ao = &lcp_allowoptions[unit]; 1460 int hadchap; 1461 1462 hadchap = -1; 1463 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL)); 1464 ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2) 1465 && ((passwd[0] != 0 || explicit_passwd) || 1466 (hadchap = have_chap_secret(user, (explicit_remote? remote_name: 1467 NULL), 0, NULL))); 1468 ao->neg_eap = !refuse_eap && ( 1469 passwd[0] != 0 || 1470 (hadchap == 1 || (hadchap == -1 && have_chap_secret(user, 1471 (explicit_remote? remote_name: NULL), 0, NULL))) || 1472 have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL) 1473 #ifdef PPP_WITH_EAPTLS 1474 || have_eaptls_secret_client(user, (explicit_remote? remote_name: NULL)) 1475 #endif 1476 ); 1477 1478 hadchap = -1; 1479 if (go->neg_upap && !uselogin && !have_pap_secret(NULL)) 1480 go->neg_upap = 0; 1481 if (go->neg_chap) { 1482 if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL), 1483 our_name, 1, NULL))) 1484 go->neg_chap = 0; 1485 } 1486 if (go->neg_eap && 1487 (hadchap == 0 || (hadchap == -1 && 1488 !have_chap_secret((explicit_remote? remote_name: NULL), our_name, 1489 1, NULL))) && 1490 !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1, 1491 NULL) 1492 #ifdef PPP_WITH_EAPTLS 1493 && !have_eaptls_secret_server((explicit_remote? remote_name: NULL), 1494 our_name, 1, NULL) 1495 #endif 1496 ) 1497 go->neg_eap = 0; 1498 } 1499 1500 1501 /* 1502 * check_passwd - Check the user name and passwd against the PAP secrets 1503 * file. If requested, also check against the system password database, 1504 * and login the user if OK. 1505 * 1506 * returns: 1507 * UPAP_AUTHNAK: Authentication failed. 1508 * UPAP_AUTHACK: Authentication succeeded. 1509 * In either case, msg points to an appropriate message. 1510 */ 1511 int 1512 check_passwd(int unit, 1513 char *auser, int userlen, 1514 char *apasswd, int passwdlen, char **msg) 1515 { 1516 int ret; 1517 char *filename; 1518 FILE *f; 1519 struct wordlist *addrs = NULL, *opts = NULL; 1520 char passwd[256], user[256]; 1521 char secret[MAXWORDLEN]; 1522 static int attempts = 0; 1523 1524 /* 1525 * Make copies of apasswd and auser, then null-terminate them. 1526 * If there are unprintable characters in the password, make 1527 * them visible. 1528 */ 1529 slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd); 1530 slprintf(user, sizeof(user), "%.*v", userlen, auser); 1531 *msg = ""; 1532 1533 /* 1534 * Check if a plugin wants to handle this. 1535 */ 1536 if (pap_auth_hook) { 1537 ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts); 1538 if (ret >= 0) { 1539 /* note: set_allowed_addrs() saves opts (but not addrs): 1540 don't free it! */ 1541 if (ret) 1542 set_allowed_addrs(unit, addrs, opts); 1543 else if (opts != 0) 1544 free_wordlist(opts); 1545 if (addrs != 0) 1546 free_wordlist(addrs); 1547 BZERO(passwd, sizeof(passwd)); 1548 return ret? UPAP_AUTHACK: UPAP_AUTHNAK; 1549 } 1550 } 1551 1552 /* 1553 * Open the file of pap secrets and scan for a suitable secret 1554 * for authenticating this user. 1555 */ 1556 filename = path_upapfile; 1557 addrs = opts = NULL; 1558 ret = UPAP_AUTHNAK; 1559 f = fopen(filename, "r"); 1560 if (f == NULL) { 1561 error("Can't open PAP password file %s: %m", filename); 1562 1563 } else { 1564 check_access(f, filename); 1565 if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) { 1566 warn("no PAP secret found for %s", user); 1567 } else { 1568 /* 1569 * If the secret is "@login", it means to check 1570 * the password against the login database. 1571 */ 1572 int login_secret = strcmp(secret, "@login") == 0; 1573 ret = UPAP_AUTHACK; 1574 if (uselogin || login_secret) { 1575 /* login option or secret is @login */ 1576 if (session_full(user, passwd, devnam, msg) == 0) { 1577 ret = UPAP_AUTHNAK; 1578 } 1579 } else if (session_mgmt) { 1580 if (session_check(user, NULL, devnam, NULL) == 0) { 1581 warn("Peer %q failed PAP Session verification", user); 1582 ret = UPAP_AUTHNAK; 1583 } 1584 } 1585 if (secret[0] != 0 && !login_secret) { 1586 /* password given in pap-secrets - must match */ 1587 if (cryptpap || strcmp(passwd, secret) != 0) { 1588 #ifdef HAVE_CRYPT_H 1589 char *cbuf = crypt(passwd, secret); 1590 if (!cbuf || strcmp(cbuf, secret) != 0) 1591 #endif 1592 ret = UPAP_AUTHNAK; 1593 } 1594 } 1595 } 1596 fclose(f); 1597 } 1598 1599 if (ret == UPAP_AUTHNAK) { 1600 if (**msg == 0) 1601 *msg = "Login incorrect"; 1602 /* 1603 * XXX can we ever get here more than once?? 1604 * Frustrate passwd stealer programs. 1605 * Allow 10 tries, but start backing off after 3 (stolen from login). 1606 * On 10'th, drop the connection. 1607 */ 1608 if (attempts++ >= 10) { 1609 warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user); 1610 lcp_close(unit, "login failed"); 1611 } 1612 if (attempts > 3) 1613 sleep((u_int) (attempts - 3) * 5); 1614 if (opts != NULL) 1615 free_wordlist(opts); 1616 1617 } else { 1618 attempts = 0; /* Reset count */ 1619 if (**msg == 0) 1620 *msg = "Login ok"; 1621 set_allowed_addrs(unit, addrs, opts); 1622 } 1623 1624 if (addrs != NULL) 1625 free_wordlist(addrs); 1626 BZERO(passwd, sizeof(passwd)); 1627 BZERO(secret, sizeof(secret)); 1628 1629 return ret; 1630 } 1631 1632 /* 1633 * null_login - Check if a username of "" and a password of "" are 1634 * acceptable, and iff so, set the list of acceptable IP addresses 1635 * and return 1. 1636 */ 1637 static int 1638 null_login(int unit) 1639 { 1640 char *filename; 1641 FILE *f; 1642 int i, ret; 1643 struct wordlist *addrs, *opts; 1644 char secret[MAXWORDLEN]; 1645 1646 /* 1647 * Check if a plugin wants to handle this. 1648 */ 1649 ret = -1; 1650 if (null_auth_hook) 1651 ret = (*null_auth_hook)(&addrs, &opts); 1652 1653 /* 1654 * Open the file of pap secrets and scan for a suitable secret. 1655 */ 1656 if (ret <= 0) { 1657 filename = path_upapfile; 1658 addrs = NULL; 1659 f = fopen(filename, "r"); 1660 if (f == NULL) 1661 return 0; 1662 check_access(f, filename); 1663 1664 i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0); 1665 ret = i >= 0 && secret[0] == 0; 1666 BZERO(secret, sizeof(secret)); 1667 fclose(f); 1668 } 1669 1670 if (ret) 1671 set_allowed_addrs(unit, addrs, opts); 1672 else if (opts != 0) 1673 free_wordlist(opts); 1674 if (addrs != 0) 1675 free_wordlist(addrs); 1676 1677 return ret; 1678 } 1679 1680 1681 /* 1682 * get_pap_passwd - get a password for authenticating ourselves with 1683 * our peer using PAP. Returns 1 on success, 0 if no suitable password 1684 * could be found. 1685 * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null). 1686 */ 1687 static int 1688 get_pap_passwd(char *passwd) 1689 { 1690 char *filename; 1691 FILE *f; 1692 int ret; 1693 char secret[MAXWORDLEN]; 1694 1695 /* 1696 * Check whether a plugin wants to supply this. 1697 */ 1698 if (pap_passwd_hook) { 1699 ret = (*pap_passwd_hook)(user, passwd); 1700 if (ret >= 0) 1701 return ret; 1702 } 1703 1704 filename = path_upapfile; 1705 f = fopen(filename, "r"); 1706 if (f == NULL) 1707 return 0; 1708 check_access(f, filename); 1709 ret = scan_authfile(f, user, 1710 (remote_name[0]? remote_name: NULL), 1711 secret, NULL, NULL, filename, 0); 1712 fclose(f); 1713 if (ret < 0) 1714 return 0; 1715 if (passwd != NULL) 1716 strlcpy(passwd, secret, MAXSECRETLEN); 1717 BZERO(secret, sizeof(secret)); 1718 return 1; 1719 } 1720 1721 1722 /* 1723 * have_pap_secret - check whether we have a PAP file with any 1724 * secrets that we could possibly use for authenticating the peer. 1725 */ 1726 static int 1727 have_pap_secret(int *lacks_ipp) 1728 { 1729 FILE *f; 1730 int ret; 1731 char *filename; 1732 struct wordlist *addrs; 1733 1734 /* let the plugin decide, if there is one */ 1735 if (pap_check_hook) { 1736 ret = (*pap_check_hook)(); 1737 if (ret >= 0) 1738 return ret; 1739 } 1740 1741 filename = path_upapfile; 1742 f = fopen(filename, "r"); 1743 if (f == NULL) 1744 return 0; 1745 1746 ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name, 1747 NULL, &addrs, NULL, filename, 0); 1748 fclose(f); 1749 if (ret >= 0 && !some_ip_ok(addrs)) { 1750 if (lacks_ipp != 0) 1751 *lacks_ipp = 1; 1752 ret = -1; 1753 } 1754 if (addrs != 0) 1755 free_wordlist(addrs); 1756 1757 return ret >= 0; 1758 } 1759 1760 1761 /* 1762 * have_chap_secret - check whether we have a CHAP file with a 1763 * secret that we could possibly use for authenticating `client' 1764 * on `server'. Either can be the null string, meaning we don't 1765 * know the identity yet. 1766 */ 1767 static int 1768 have_chap_secret(char *client, char *server, 1769 int need_ip, int *lacks_ipp) 1770 { 1771 FILE *f; 1772 int ret; 1773 char *filename; 1774 struct wordlist *addrs; 1775 1776 if (chap_check_hook) { 1777 ret = (*chap_check_hook)(); 1778 if (ret >= 0) { 1779 return ret; 1780 } 1781 } 1782 1783 filename = path_chapfile; 1784 f = fopen(filename, "r"); 1785 if (f == NULL) 1786 return 0; 1787 1788 if (client != NULL && client[0] == 0) 1789 client = NULL; 1790 else if (server != NULL && server[0] == 0) 1791 server = NULL; 1792 1793 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0); 1794 fclose(f); 1795 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) { 1796 if (lacks_ipp != 0) 1797 *lacks_ipp = 1; 1798 ret = -1; 1799 } 1800 if (addrs != 0) 1801 free_wordlist(addrs); 1802 1803 return ret >= 0; 1804 } 1805 1806 1807 /* 1808 * have_srp_secret - check whether we have a SRP file with a 1809 * secret that we could possibly use for authenticating `client' 1810 * on `server'. Either can be the null string, meaning we don't 1811 * know the identity yet. 1812 */ 1813 static int 1814 have_srp_secret(char *client, char *server, int need_ip, int *lacks_ipp) 1815 { 1816 FILE *f; 1817 int ret; 1818 char *filename; 1819 struct wordlist *addrs; 1820 1821 filename = PPP_PATH_SRPFILE; 1822 f = fopen(filename, "r"); 1823 if (f == NULL) 1824 return 0; 1825 1826 if (client != NULL && client[0] == 0) 1827 client = NULL; 1828 else if (server != NULL && server[0] == 0) 1829 server = NULL; 1830 1831 ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0); 1832 fclose(f); 1833 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) { 1834 if (lacks_ipp != 0) 1835 *lacks_ipp = 1; 1836 ret = -1; 1837 } 1838 if (addrs != 0) 1839 free_wordlist(addrs); 1840 1841 return ret >= 0; 1842 } 1843 1844 1845 /* 1846 * get_secret - open the CHAP secret file and return the secret 1847 * for authenticating the given client on the given server. 1848 * (We could be either client or server). 1849 */ 1850 int 1851 get_secret(int unit, char *client, char *server, 1852 char *secret, int *secret_len, int am_server) 1853 { 1854 FILE *f; 1855 int ret, len; 1856 char *filename; 1857 struct wordlist *addrs, *opts; 1858 char secbuf[MAXWORDLEN]; 1859 1860 if (!am_server && passwd[0] != 0) { 1861 strlcpy(secbuf, passwd, sizeof(secbuf)); 1862 } else if (!am_server && chap_passwd_hook) { 1863 if ( (*chap_passwd_hook)(client, secbuf) < 0) { 1864 error("Unable to obtain CHAP password for %s on %s from plugin", 1865 client, server); 1866 return 0; 1867 } 1868 } else { 1869 filename = path_chapfile; 1870 addrs = NULL; 1871 secbuf[0] = 0; 1872 1873 f = fopen(filename, "r"); 1874 if (f == NULL) { 1875 error("Can't open chap secret file %s: %m", filename); 1876 return 0; 1877 } 1878 check_access(f, filename); 1879 1880 ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0); 1881 fclose(f); 1882 if (ret < 0) 1883 return 0; 1884 1885 if (am_server) 1886 set_allowed_addrs(unit, addrs, opts); 1887 else if (opts != 0) 1888 free_wordlist(opts); 1889 if (addrs != 0) 1890 free_wordlist(addrs); 1891 } 1892 1893 len = strlen(secbuf); 1894 if (len > MAXSECRETLEN) { 1895 error("Secret for %s on %s is too long", client, server); 1896 len = MAXSECRETLEN; 1897 } 1898 BCOPY(secbuf, secret, len); 1899 BZERO(secbuf, sizeof(secbuf)); 1900 *secret_len = len; 1901 1902 return 1; 1903 } 1904 1905 1906 /* 1907 * get_srp_secret - open the SRP secret file and return the secret 1908 * for authenticating the given client on the given server. 1909 * (We could be either client or server). 1910 */ 1911 int 1912 get_srp_secret(int unit, char *client, char *server, 1913 char *secret, int am_server) 1914 { 1915 FILE *fp; 1916 int ret; 1917 char *filename; 1918 struct wordlist *addrs, *opts; 1919 1920 if (!am_server && passwd[0] != '\0') { 1921 strlcpy(secret, passwd, MAXWORDLEN); 1922 } else { 1923 filename = PPP_PATH_SRPFILE; 1924 addrs = NULL; 1925 1926 fp = fopen(filename, "r"); 1927 if (fp == NULL) { 1928 error("Can't open srp secret file %s: %m", filename); 1929 return 0; 1930 } 1931 check_access(fp, filename); 1932 1933 secret[0] = '\0'; 1934 ret = scan_authfile(fp, client, server, secret, &addrs, &opts, 1935 filename, am_server); 1936 fclose(fp); 1937 if (ret < 0) 1938 return 0; 1939 1940 if (am_server) 1941 set_allowed_addrs(unit, addrs, opts); 1942 else if (opts != NULL) 1943 free_wordlist(opts); 1944 if (addrs != NULL) 1945 free_wordlist(addrs); 1946 } 1947 1948 return 1; 1949 } 1950 1951 /* 1952 * set_allowed_addrs() - set the list of allowed addresses. 1953 * Also looks for `--' indicating options to apply for this peer 1954 * and leaves the following words in extra_options. 1955 */ 1956 static void 1957 set_allowed_addrs(int unit, struct wordlist *addrs, 1958 struct wordlist *opts) 1959 { 1960 int n; 1961 struct wordlist *ap, **plink; 1962 struct permitted_ip *ip; 1963 char *ptr_word, *ptr_mask; 1964 struct hostent *hp; 1965 struct netent *np; 1966 u_int32_t a, mask, ah, offset; 1967 struct ipcp_options *wo = &ipcp_wantoptions[unit]; 1968 u_int32_t suggested_ip = 0; 1969 1970 if (addresses[unit] != NULL) 1971 free(addresses[unit]); 1972 addresses[unit] = NULL; 1973 if (extra_options != NULL) 1974 free_wordlist(extra_options); 1975 extra_options = opts; 1976 1977 /* 1978 * Count the number of IP addresses given. 1979 */ 1980 n = wordlist_count(addrs) + wordlist_count(noauth_addrs); 1981 if (n == 0) 1982 return; 1983 ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip)); 1984 if (ip == 0) 1985 return; 1986 1987 /* temporarily append the noauth_addrs list to addrs */ 1988 for (plink = &addrs; *plink != NULL; plink = &(*plink)->next) 1989 ; 1990 *plink = noauth_addrs; 1991 1992 n = 0; 1993 for (ap = addrs; ap != NULL; ap = ap->next) { 1994 /* "-" means no addresses authorized, "*" means any address allowed */ 1995 ptr_word = ap->word; 1996 if (strcmp(ptr_word, "-") == 0) 1997 break; 1998 if (strcmp(ptr_word, "*") == 0) { 1999 ip[n].permit = 1; 2000 ip[n].base = ip[n].mask = 0; 2001 ++n; 2002 break; 2003 } 2004 2005 ip[n].permit = 1; 2006 if (*ptr_word == '!') { 2007 ip[n].permit = 0; 2008 ++ptr_word; 2009 } 2010 2011 mask = ~ (u_int32_t) 0; 2012 offset = 0; 2013 ptr_mask = strchr (ptr_word, '/'); 2014 if (ptr_mask != NULL) { 2015 int bit_count; 2016 char *endp; 2017 2018 bit_count = (int) strtol (ptr_mask+1, &endp, 10); 2019 if (bit_count <= 0 || bit_count > 32) { 2020 warn("invalid address length %v in auth. address list", 2021 ptr_mask+1); 2022 continue; 2023 } 2024 bit_count = 32 - bit_count; /* # bits in host part */ 2025 if (*endp == '+') { 2026 offset = ifunit + 1; 2027 ++endp; 2028 } 2029 if (*endp != 0) { 2030 warn("invalid address length syntax: %v", ptr_mask+1); 2031 continue; 2032 } 2033 *ptr_mask = '\0'; 2034 mask <<= bit_count; 2035 } 2036 2037 hp = gethostbyname(ptr_word); 2038 if (hp != NULL && hp->h_addrtype == AF_INET) { 2039 a = *(u_int32_t *)hp->h_addr; 2040 } else { 2041 np = getnetbyname (ptr_word); 2042 if (np != NULL && np->n_addrtype == AF_INET) { 2043 a = htonl ((u_int32_t)np->n_net); 2044 if (ptr_mask == NULL) { 2045 /* calculate appropriate mask for net */ 2046 ah = ntohl(a); 2047 if (IN_CLASSA(ah)) 2048 mask = IN_CLASSA_NET; 2049 else if (IN_CLASSB(ah)) 2050 mask = IN_CLASSB_NET; 2051 else if (IN_CLASSC(ah)) 2052 mask = IN_CLASSC_NET; 2053 } 2054 } else { 2055 a = inet_addr (ptr_word); 2056 } 2057 } 2058 2059 if (ptr_mask != NULL) 2060 *ptr_mask = '/'; 2061 2062 if (a == (u_int32_t)-1L) { 2063 warn("unknown host %s in auth. address list", ap->word); 2064 continue; 2065 } 2066 if (offset != 0) { 2067 if (offset >= ~mask) { 2068 warn("interface unit %d too large for subnet %v", 2069 ifunit, ptr_word); 2070 continue; 2071 } 2072 a = htonl((ntohl(a) & mask) + offset); 2073 mask = ~(u_int32_t)0; 2074 } 2075 ip[n].mask = htonl(mask); 2076 ip[n].base = a & ip[n].mask; 2077 ++n; 2078 if (~mask == 0 && suggested_ip == 0) 2079 suggested_ip = a; 2080 } 2081 *plink = NULL; 2082 2083 ip[n].permit = 0; /* make the last entry forbid all addresses */ 2084 ip[n].base = 0; /* to terminate the list */ 2085 ip[n].mask = 0; 2086 2087 addresses[unit] = ip; 2088 2089 /* 2090 * If the address given for the peer isn't authorized, or if 2091 * the user hasn't given one, AND there is an authorized address 2092 * which is a single host, then use that if we find one. 2093 */ 2094 if (suggested_ip != 0 2095 && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) { 2096 wo->hisaddr = suggested_ip; 2097 /* 2098 * Do we insist on this address? No, if there are other 2099 * addresses authorized than the suggested one. 2100 */ 2101 if (n > 1) 2102 wo->accept_remote = 1; 2103 } 2104 } 2105 2106 /* 2107 * auth_ip_addr - check whether the peer is authorized to use 2108 * a given IP address. Returns 1 if authorized, 0 otherwise. 2109 */ 2110 int 2111 auth_ip_addr(int unit, u_int32_t addr) 2112 { 2113 int ok; 2114 2115 /* don't allow loopback or multicast address */ 2116 if (ppp_bad_ip_addr(addr)) 2117 return 0; 2118 2119 if (allowed_address_hook) { 2120 ok = allowed_address_hook(addr); 2121 if (ok >= 0) return ok; 2122 } 2123 2124 if (addresses[unit] != NULL) { 2125 ok = ip_addr_check(addr, addresses[unit]); 2126 if (ok >= 0) 2127 return ok; 2128 } 2129 2130 if (auth_required) 2131 return 0; /* no addresses authorized */ 2132 return allow_any_ip || privileged || !have_route_to(addr); 2133 } 2134 2135 static int 2136 ip_addr_check(u_int32_t addr, struct permitted_ip *addrs) 2137 { 2138 for (; ; ++addrs) 2139 if ((addr & addrs->mask) == addrs->base) 2140 return addrs->permit; 2141 } 2142 2143 /* 2144 * Check if given addr in network byte order is in the looback network, or a multicast address. 2145 */ 2146 bool 2147 ppp_bad_ip_addr(u_int32_t addr) 2148 { 2149 addr = ntohl(addr); 2150 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET 2151 || IN_MULTICAST(addr) || IN_BADCLASS(addr); 2152 } 2153 2154 /* 2155 * some_ip_ok - check a wordlist to see if it authorizes any 2156 * IP address(es). 2157 */ 2158 static int 2159 some_ip_ok(struct wordlist *addrs) 2160 { 2161 for (; addrs != 0; addrs = addrs->next) { 2162 if (addrs->word[0] == '-') 2163 break; 2164 if (addrs->word[0] != '!') 2165 return 1; /* some IP address is allowed */ 2166 } 2167 return 0; 2168 } 2169 2170 /* 2171 * auth_number - check whether the remote number is allowed to connect. 2172 * Returns 1 if authorized, 0 otherwise. 2173 */ 2174 int 2175 auth_number(void) 2176 { 2177 struct wordlist *wp = permitted_numbers; 2178 size_t l; 2179 2180 /* Allow all if no authorization list. */ 2181 if (!wp) 2182 return 1; 2183 2184 /* Allow if we have a match in the authorization list. */ 2185 while (wp) { 2186 /* trailing '*' wildcard */ 2187 l = strlen(wp->word); 2188 if (l > 0 && (wp->word)[l - 1] == '*') { 2189 if (!strncasecmp(wp->word, remote_number, l - 1)) 2190 return 1; 2191 } else if (strcasecmp(wp->word, remote_number) == 0) 2192 return 1; 2193 wp = wp->next; 2194 } 2195 2196 return 0; 2197 } 2198 2199 /* 2200 * check_access - complain if a secret file has too-liberal permissions. 2201 */ 2202 static void 2203 check_access(FILE *f, char *filename) 2204 { 2205 struct stat sbuf; 2206 2207 if (fstat(fileno(f), &sbuf) < 0) { 2208 warn("cannot stat secret file %s: %m", filename); 2209 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) { 2210 warn("Warning - secret file %s has world and/or group access", 2211 filename); 2212 } 2213 } 2214 2215 2216 /* 2217 * scan_authfile - Scan an authorization file for a secret suitable 2218 * for authenticating `client' on `server'. The return value is -1 2219 * if no secret is found, otherwise >= 0. The return value has 2220 * NONWILD_CLIENT set if the secret didn't have "*" for the client, and 2221 * NONWILD_SERVER set if the secret didn't have "*" for the server. 2222 * Any following words on the line up to a "--" (i.e. address authorization 2223 * info) are placed in a wordlist and returned in *addrs. Any 2224 * following words (extra options) are placed in a wordlist and 2225 * returned in *opts. 2226 * We assume secret is NULL or points to MAXWORDLEN bytes of space. 2227 * Flags are non-zero if we need two colons in the secret in order to 2228 * match. 2229 */ 2230 static int 2231 scan_authfile(FILE *f, char *client, char *server, 2232 char *secret, struct wordlist **addrs, 2233 struct wordlist **opts, char *filename, 2234 int flags) 2235 { 2236 int newline, xxx; 2237 int got_flag, best_flag; 2238 FILE *sf; 2239 struct wordlist *ap, *addr_list, *alist, **app; 2240 char word[MAXWORDLEN]; 2241 char atfile[MAXWORDLEN]; 2242 char lsecret[MAXWORDLEN]; 2243 char *cp; 2244 2245 if (addrs != NULL) 2246 *addrs = NULL; 2247 if (opts != NULL) 2248 *opts = NULL; 2249 addr_list = NULL; 2250 if (!getword(f, word, &newline, filename)) 2251 return -1; /* file is empty??? */ 2252 newline = 1; 2253 best_flag = -1; 2254 for (;;) { 2255 /* 2256 * Skip until we find a word at the start of a line. 2257 */ 2258 while (!newline && getword(f, word, &newline, filename)) 2259 ; 2260 if (!newline) 2261 break; /* got to end of file */ 2262 2263 /* 2264 * Got a client - check if it's a match or a wildcard. 2265 */ 2266 got_flag = 0; 2267 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) { 2268 newline = 0; 2269 continue; 2270 } 2271 if (!ISWILD(word)) 2272 got_flag = NONWILD_CLIENT; 2273 2274 /* 2275 * Now get a server and check if it matches. 2276 */ 2277 if (!getword(f, word, &newline, filename)) 2278 break; 2279 if (newline) 2280 continue; 2281 if (!ISWILD(word)) { 2282 if (server != NULL && strcmp(word, server) != 0) 2283 continue; 2284 got_flag |= NONWILD_SERVER; 2285 } 2286 2287 /* 2288 * Got some sort of a match - see if it's better than what 2289 * we have already. 2290 */ 2291 if (got_flag <= best_flag) 2292 continue; 2293 2294 /* 2295 * Get the secret. 2296 */ 2297 if (!getword(f, word, &newline, filename)) 2298 break; 2299 if (newline) 2300 continue; 2301 2302 /* 2303 * SRP-SHA1 authenticator should never be reading secrets from 2304 * a file. (Authenticatee may, though.) 2305 */ 2306 if (flags && ((cp = strchr(word, ':')) == NULL || 2307 strchr(cp + 1, ':') == NULL)) 2308 continue; 2309 2310 if (secret != NULL) { 2311 /* 2312 * Special syntax: @/pathname means read secret from file. 2313 */ 2314 if (word[0] == '@' && word[1] == '/') { 2315 strlcpy(atfile, word+1, sizeof(atfile)); 2316 if ((sf = fopen(atfile, "r")) == NULL) { 2317 warn("can't open indirect secret file %s", atfile); 2318 continue; 2319 } 2320 check_access(sf, atfile); 2321 if (!getword(sf, word, &xxx, atfile)) { 2322 warn("no secret in indirect secret file %s", atfile); 2323 fclose(sf); 2324 continue; 2325 } 2326 fclose(sf); 2327 } 2328 strlcpy(lsecret, word, sizeof(lsecret)); 2329 } 2330 2331 /* 2332 * Now read address authorization info and make a wordlist. 2333 */ 2334 app = &alist; 2335 for (;;) { 2336 size_t len; 2337 if (!getword(f, word, &newline, filename) || newline) 2338 break; 2339 len = strlen(word) + 1; 2340 ap = (struct wordlist *)malloc(sizeof(struct wordlist) + len); 2341 if (ap == NULL) 2342 novm("authorized addresses"); 2343 ap->word = (char *) (ap + 1); 2344 memcpy(ap->word, word, len); 2345 *app = ap; 2346 app = &ap->next; 2347 } 2348 *app = NULL; 2349 2350 /* 2351 * This is the best so far; remember it. 2352 */ 2353 best_flag = got_flag; 2354 if (addr_list) 2355 free_wordlist(addr_list); 2356 addr_list = alist; 2357 if (secret != NULL) 2358 strlcpy(secret, lsecret, MAXWORDLEN); 2359 2360 if (!newline) 2361 break; 2362 } 2363 2364 /* scan for a -- word indicating the start of options */ 2365 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next) 2366 if (strcmp(ap->word, "--") == 0) 2367 break; 2368 /* ap = start of options */ 2369 if (ap != NULL) { 2370 ap = ap->next; /* first option */ 2371 free(*app); /* free the "--" word */ 2372 *app = NULL; /* terminate addr list */ 2373 } 2374 if (opts != NULL) 2375 *opts = ap; 2376 else if (ap != NULL) 2377 free_wordlist(ap); 2378 if (addrs != NULL) 2379 *addrs = addr_list; 2380 else if (addr_list != NULL) 2381 free_wordlist(addr_list); 2382 2383 return best_flag; 2384 } 2385 2386 /* 2387 * wordlist_count - return the number of items in a wordlist 2388 */ 2389 static int 2390 wordlist_count(struct wordlist *wp) 2391 { 2392 int n; 2393 2394 for (n = 0; wp != NULL; wp = wp->next) 2395 ++n; 2396 return n; 2397 } 2398 2399 /* 2400 * free_wordlist - release memory allocated for a wordlist. 2401 */ 2402 static void 2403 free_wordlist(struct wordlist *wp) 2404 { 2405 struct wordlist *next; 2406 2407 while (wp != NULL) { 2408 next = wp->next; 2409 free(wp); 2410 wp = next; 2411 } 2412 } 2413 2414 /* 2415 * auth_script_done - called when the auth-up or auth-down script 2416 * has finished. 2417 */ 2418 static void 2419 auth_script_done(void *arg) 2420 { 2421 auth_script_pid = 0; 2422 switch (auth_script_state) { 2423 case s_up: 2424 if (auth_state == s_down) { 2425 auth_script_state = s_down; 2426 auth_script(PPP_PATH_AUTHDOWN); 2427 } 2428 break; 2429 case s_down: 2430 if (auth_state == s_up) { 2431 auth_script_state = s_up; 2432 auth_script(PPP_PATH_AUTHUP); 2433 } 2434 break; 2435 } 2436 } 2437 2438 /* 2439 * auth_script - execute a script with arguments 2440 * interface-name peer-name real-user tty speed 2441 */ 2442 static void 2443 auth_script(char *script) 2444 { 2445 char strspeed[32]; 2446 struct passwd *pw; 2447 char struid[32]; 2448 char *user_name; 2449 char *argv[8]; 2450 2451 if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL) 2452 user_name = pw->pw_name; 2453 else { 2454 slprintf(struid, sizeof(struid), "%d", getuid()); 2455 user_name = struid; 2456 } 2457 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate); 2458 2459 argv[0] = script; 2460 argv[1] = ifname; 2461 argv[2] = peer_authname; 2462 argv[3] = user_name; 2463 argv[4] = devnam; 2464 argv[5] = strspeed; 2465 argv[6] = ipparam; 2466 argv[7] = NULL; 2467 2468 auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0); 2469 } 2470 2471 2472 #ifdef PPP_WITH_EAPTLS 2473 static int 2474 have_eaptls_secret_server(char *client, char *server, 2475 int need_ip, int *lacks_ipp) 2476 { 2477 FILE *f; 2478 int ret; 2479 char *filename; 2480 struct wordlist *addrs; 2481 char servcertfile[MAXWORDLEN]; 2482 char clicertfile[MAXWORDLEN]; 2483 char cacertfile[MAXWORDLEN]; 2484 char pkfile[MAXWORDLEN]; 2485 2486 filename = PPP_PATH_EAPTLSSERVFILE; 2487 f = fopen(filename, "r"); 2488 if (f == NULL) 2489 return 0; 2490 2491 if (client != NULL && client[0] == 0) 2492 client = NULL; 2493 else if (server != NULL && server[0] == 0) 2494 server = NULL; 2495 2496 ret = 2497 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile, 2498 cacertfile, pkfile, &addrs, NULL, filename, 2499 0); 2500 2501 fclose(f); 2502 2503 /* 2504 if (ret >= 0 && !eaptls_init_ssl(1, cacertfile, servcertfile, 2505 clicertfile, pkfile)) 2506 ret = -1; 2507 */ 2508 2509 if (ret >= 0 && need_ip && !some_ip_ok(addrs)) { 2510 if (lacks_ipp != 0) 2511 *lacks_ipp = 1; 2512 ret = -1; 2513 } 2514 if (addrs != 0) 2515 free_wordlist(addrs); 2516 2517 return ret >= 0; 2518 } 2519 2520 2521 static int 2522 have_eaptls_secret_client(char *client, char *server) 2523 { 2524 FILE *f; 2525 int ret; 2526 char *filename; 2527 struct wordlist *addrs = NULL; 2528 char servcertfile[MAXWORDLEN]; 2529 char clicertfile[MAXWORDLEN]; 2530 char cacertfile[MAXWORDLEN]; 2531 char pkfile[MAXWORDLEN]; 2532 2533 if (client != NULL && client[0] == 0) 2534 client = NULL; 2535 else if (server != NULL && server[0] == 0) 2536 server = NULL; 2537 2538 if ((cacert_file || ca_path) && cert_file && privkey_file) 2539 return 1; 2540 if (pkcs12_file) 2541 return 1; 2542 2543 filename = PPP_PATH_EAPTLSCLIFILE; 2544 f = fopen(filename, "r"); 2545 if (f == NULL) 2546 return 0; 2547 2548 ret = 2549 scan_authfile_eaptls(f, client, server, clicertfile, servcertfile, 2550 cacertfile, pkfile, &addrs, NULL, filename, 2551 0); 2552 fclose(f); 2553 2554 /* 2555 if (ret >= 0 && !eaptls_init_ssl(0, cacertfile, clicertfile, 2556 servcertfile, pkfile)) 2557 ret = -1; 2558 */ 2559 2560 if (addrs != 0) 2561 free_wordlist(addrs); 2562 2563 return ret >= 0; 2564 } 2565 2566 2567 static int 2568 scan_authfile_eaptls(FILE *f, char *client, char *server, 2569 char *cli_cert, char *serv_cert, char *ca_cert, 2570 char *pk, struct wordlist **addrs, 2571 struct wordlist **opts, 2572 char *filename, int flags) 2573 { 2574 int newline; 2575 int got_flag, best_flag; 2576 struct wordlist *ap, *addr_list, *alist, **app; 2577 char word[MAXWORDLEN]; 2578 2579 if (addrs != NULL) 2580 *addrs = NULL; 2581 if (opts != NULL) 2582 *opts = NULL; 2583 addr_list = NULL; 2584 if (!getword(f, word, &newline, filename)) 2585 return -1; /* file is empty??? */ 2586 newline = 1; 2587 best_flag = -1; 2588 for (;;) { 2589 /* 2590 * Skip until we find a word at the start of a line. 2591 */ 2592 while (!newline && getword(f, word, &newline, filename)); 2593 if (!newline) 2594 break; /* got to end of file */ 2595 2596 /* 2597 * Got a client - check if it's a match or a wildcard. 2598 */ 2599 got_flag = 0; 2600 if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) { 2601 newline = 0; 2602 continue; 2603 } 2604 if (!ISWILD(word)) 2605 got_flag = NONWILD_CLIENT; 2606 2607 /* 2608 * Now get a server and check if it matches. 2609 */ 2610 if (!getword(f, word, &newline, filename)) 2611 break; 2612 if (newline) 2613 continue; 2614 if (!ISWILD(word)) { 2615 if (server != NULL && strcmp(word, server) != 0) 2616 continue; 2617 got_flag |= NONWILD_SERVER; 2618 } 2619 2620 /* 2621 * Got some sort of a match - see if it's better than what 2622 * we have already. 2623 */ 2624 if (got_flag <= best_flag) 2625 continue; 2626 2627 /* 2628 * Get the cli_cert 2629 */ 2630 if (!getword(f, word, &newline, filename)) 2631 break; 2632 if (newline) 2633 continue; 2634 if (strcmp(word, "-") != 0) { 2635 strlcpy(cli_cert, word, MAXWORDLEN); 2636 } else 2637 cli_cert[0] = 0; 2638 2639 /* 2640 * Get serv_cert 2641 */ 2642 if (!getword(f, word, &newline, filename)) 2643 break; 2644 if (newline) 2645 continue; 2646 if (strcmp(word, "-") != 0) { 2647 strlcpy(serv_cert, word, MAXWORDLEN); 2648 } else 2649 serv_cert[0] = 0; 2650 2651 /* 2652 * Get ca_cert 2653 */ 2654 if (!getword(f, word, &newline, filename)) 2655 break; 2656 if (newline) 2657 continue; 2658 strlcpy(ca_cert, word, MAXWORDLEN); 2659 2660 /* 2661 * Get pk 2662 */ 2663 if (!getword(f, word, &newline, filename)) 2664 break; 2665 if (newline) 2666 continue; 2667 strlcpy(pk, word, MAXWORDLEN); 2668 2669 2670 /* 2671 * Now read address authorization info and make a wordlist. 2672 */ 2673 app = &alist; 2674 for (;;) { 2675 if (!getword(f, word, &newline, filename) || newline) 2676 break; 2677 ap = (struct wordlist *) 2678 malloc(sizeof(struct wordlist) + strlen(word) + 1); 2679 if (ap == NULL) 2680 novm("authorized addresses"); 2681 ap->word = (char *) (ap + 1); 2682 strcpy(ap->word, word); 2683 *app = ap; 2684 app = &ap->next; 2685 } 2686 *app = NULL; 2687 /* 2688 * This is the best so far; remember it. 2689 */ 2690 best_flag = got_flag; 2691 if (addr_list) 2692 free_wordlist(addr_list); 2693 addr_list = alist; 2694 2695 if (!newline) 2696 break; 2697 } 2698 2699 /* scan for a -- word indicating the start of options */ 2700 for (app = &addr_list; (ap = *app) != NULL; app = &ap->next) 2701 if (strcmp(ap->word, "--") == 0) 2702 break; 2703 /* ap = start of options */ 2704 if (ap != NULL) { 2705 ap = ap->next; /* first option */ 2706 free(*app); /* free the "--" word */ 2707 *app = NULL; /* terminate addr list */ 2708 } 2709 if (opts != NULL) 2710 *opts = ap; 2711 else if (ap != NULL) 2712 free_wordlist(ap); 2713 if (addrs != NULL) 2714 *addrs = addr_list; 2715 else if (addr_list != NULL) 2716 free_wordlist(addr_list); 2717 2718 return best_flag; 2719 } 2720 2721 2722 int 2723 get_eaptls_secret(int unit, char *client, char *server, 2724 char *clicertfile, char *servcertfile, char *cacertfile, 2725 char *capath, char *pkfile, char *pkcs12, int am_server) 2726 { 2727 FILE *fp; 2728 int ret; 2729 char *filename = NULL; 2730 struct wordlist *addrs = NULL; 2731 struct wordlist *opts = NULL; 2732 2733 /* maybe overkill, but it eases debugging */ 2734 bzero(clicertfile, MAXWORDLEN); 2735 bzero(servcertfile, MAXWORDLEN); 2736 bzero(cacertfile, MAXWORDLEN); 2737 bzero(capath, MAXWORDLEN); 2738 bzero(pkfile, MAXWORDLEN); 2739 bzero(pkcs12, MAXWORDLEN); 2740 2741 /* the ca+cert+privkey can also be specified as options */ 2742 if (!am_server && (cacert_file || ca_path) && cert_file && privkey_file ) 2743 { 2744 strlcpy( clicertfile, cert_file, MAXWORDLEN ); 2745 if (cacert_file) 2746 strlcpy( cacertfile, cacert_file, MAXWORDLEN ); 2747 if (ca_path) 2748 strlcpy( capath, ca_path, MAXWORDLEN ); 2749 strlcpy( pkfile, privkey_file, MAXWORDLEN ); 2750 } 2751 else if (!am_server && pkcs12_file) 2752 { 2753 strlcpy( pkcs12, pkcs12_file, MAXWORDLEN ); 2754 if (cacert_file) 2755 strlcpy( cacertfile, cacert_file, MAXWORDLEN ); 2756 if (ca_path) 2757 strlcpy( capath, ca_path, MAXWORDLEN ); 2758 } 2759 else 2760 { 2761 filename = (am_server ? PPP_PATH_EAPTLSSERVFILE : PPP_PATH_EAPTLSCLIFILE); 2762 addrs = NULL; 2763 2764 fp = fopen(filename, "r"); 2765 if (fp == NULL) 2766 { 2767 error("Can't open eap-tls secret file %s: %m", filename); 2768 return 0; 2769 } 2770 2771 check_access(fp, filename); 2772 2773 ret = scan_authfile_eaptls(fp, client, server, clicertfile, servcertfile, 2774 cacertfile, pkfile, &addrs, &opts, filename, 0); 2775 2776 fclose(fp); 2777 2778 if (ret < 0) return 0; 2779 } 2780 2781 if (eaptls_passwd_hook) 2782 { 2783 dbglog( "Calling eaptls password hook" ); 2784 if ( (*eaptls_passwd_hook)(pkfile, passwd) < 0) 2785 { 2786 error("Unable to obtain EAP-TLS password for %s (%s) from plugin", 2787 client, pkfile); 2788 return 0; 2789 } 2790 } 2791 if (am_server) 2792 set_allowed_addrs(unit, addrs, opts); 2793 else if (opts != NULL) 2794 free_wordlist(opts); 2795 if (addrs != NULL) 2796 free_wordlist(addrs); 2797 2798 return 1; 2799 } 2800 #endif 2801