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