17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * auth.c - PPP authentication and phase control.
37c478bd9Sstevel@tonic-gate  *
42a9459bdSsangeeta  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1993 The Australian National University.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the Australian National University.  The name of the University
167c478bd9Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
177c478bd9Sstevel@tonic-gate  * software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
197c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
207c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * Copyright (c) 1989 Carnegie Mellon University.
237c478bd9Sstevel@tonic-gate  * All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
267c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
277c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
287c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
297c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
307c478bd9Sstevel@tonic-gate  * by Carnegie Mellon University.  The name of the
317c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
327c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
337c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
347c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
357c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* Pull in crypt() definition. */
397c478bd9Sstevel@tonic-gate #define __EXTENSIONS__
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stddef.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <pwd.h>
467c478bd9Sstevel@tonic-gate #include <grp.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <sys/stat.h>
507c478bd9Sstevel@tonic-gate #include <sys/socket.h>
517c478bd9Sstevel@tonic-gate #include <utmp.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
537c478bd9Sstevel@tonic-gate #if defined(_PATH_LASTLOG) && (defined(_linux_) || defined(__linux__))
547c478bd9Sstevel@tonic-gate #include <lastlog.h>
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #if defined(_linux_) || defined(__linux__)
587c478bd9Sstevel@tonic-gate #include <crypt.h>
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <netdb.h>
627c478bd9Sstevel@tonic-gate #include <netinet/in.h>
637c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* Backward compatibility with old Makefiles */
667c478bd9Sstevel@tonic-gate #if defined(USE_PAM) && !defined(ALLOW_PAM)
677c478bd9Sstevel@tonic-gate #define ALLOW_PAM
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #ifdef ALLOW_PAM
717c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef HAS_SHADOW
757c478bd9Sstevel@tonic-gate #include <shadow.h>
767c478bd9Sstevel@tonic-gate #ifndef PW_PPP
777c478bd9Sstevel@tonic-gate #define PW_PPP PW_LOGIN
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #include "pppd.h"
827c478bd9Sstevel@tonic-gate #include "fsm.h"
837c478bd9Sstevel@tonic-gate #include "lcp.h"
847c478bd9Sstevel@tonic-gate #include "ipcp.h"
857c478bd9Sstevel@tonic-gate #include "upap.h"
867c478bd9Sstevel@tonic-gate #include "chap.h"
877c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT
887c478bd9Sstevel@tonic-gate #include "cbcp.h"
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate #include "pathnames.h"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* Bits in scan_authfile return value */
937c478bd9Sstevel@tonic-gate #define NONWILD_SERVER	1
947c478bd9Sstevel@tonic-gate #define NONWILD_CLIENT	2
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define ISWILD(word)	(word[0] == '*' && word[1] == '\0')
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* The name by which the peer authenticated itself to us. */
997c478bd9Sstevel@tonic-gate char peer_authname[MAXNAMELEN];
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /* Records which authentication operations haven't completed yet. */
1027c478bd9Sstevel@tonic-gate static int auth_pending[NUM_PPP];
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* Set if we have successfully called plogin() */
1057c478bd9Sstevel@tonic-gate static int logged_in;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /* List of addresses which the peer may use. */
1087c478bd9Sstevel@tonic-gate static struct permitted_ip *addresses[NUM_PPP];
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* Wordlist giving addresses which the peer may use
1117c478bd9Sstevel@tonic-gate    without authenticating itself. */
1127c478bd9Sstevel@tonic-gate static struct wordlist *noauth_addrs;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* Extra options to apply, from the secrets file entry for the peer. */
1157c478bd9Sstevel@tonic-gate static struct wordlist *extra_options;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* Source of those extra options. */
1187c478bd9Sstevel@tonic-gate static const char *extra_opt_filename;
1197c478bd9Sstevel@tonic-gate static int extra_opt_line;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* Number of network protocols which we have opened. */
1227c478bd9Sstevel@tonic-gate static int num_np_open;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* Number of network protocols which have come up. */
1257c478bd9Sstevel@tonic-gate static int num_np_up;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* Set if we got the contents of passwd[] from the pap-secrets file. */
1287c478bd9Sstevel@tonic-gate static int passwd_from_file;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* Set if we require authentication only because we have a default route. */
1317c478bd9Sstevel@tonic-gate static bool default_auth;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* Hook to enable a plugin to control the idle time limit */
1347c478bd9Sstevel@tonic-gate int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* Hook for a plugin to say whether we can possibly authenticate any peer */
1377c478bd9Sstevel@tonic-gate int (*pap_check_hook) __P((void)) = NULL;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* Hook for a plugin to check the PAP user and password */
1407c478bd9Sstevel@tonic-gate int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
1417c478bd9Sstevel@tonic-gate 			  struct wordlist **paddrs,
1427c478bd9Sstevel@tonic-gate 			  struct wordlist **popts)) = NULL;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* Hook for a plugin to know about the PAP user logout */
1457c478bd9Sstevel@tonic-gate void (*pap_logout_hook) __P((void)) = NULL;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /* Hook for a plugin to get the PAP password for authenticating us */
1487c478bd9Sstevel@tonic-gate int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * This is used to ensure that we don't start an auth-up/down
1527c478bd9Sstevel@tonic-gate  * script while one is already running.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate enum script_state {
1557c478bd9Sstevel@tonic-gate     s_down,
1567c478bd9Sstevel@tonic-gate     s_up
1577c478bd9Sstevel@tonic-gate };
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static enum script_state auth_state = s_down;
1607c478bd9Sstevel@tonic-gate static enum script_state auth_script_state = s_down;
1617c478bd9Sstevel@tonic-gate static pid_t auth_script_pid = 0;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * This is set by scan_authfile if a client matches, but server doesn't
1657c478bd9Sstevel@tonic-gate  * (possible configuration error).
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static char scan_server_match_failed[MAXWORDLEN];
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Option variables.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate bool uselogin = 0;		/* Use /etc/passwd for checking PAP */
1737c478bd9Sstevel@tonic-gate bool cryptpap = 0;		/* Passwords in pap-secrets are encrypted */
1747c478bd9Sstevel@tonic-gate bool refuse_pap = 0;		/* Don't wanna auth. ourselves with PAP */
1757c478bd9Sstevel@tonic-gate bool refuse_chap = 0;		/* Don't wanna auth. ourselves with CHAP */
1767c478bd9Sstevel@tonic-gate bool usehostname = 0;		/* Use hostname for our_name */
1777c478bd9Sstevel@tonic-gate bool auth_required = 0;		/* Always require authentication from peer */
1787c478bd9Sstevel@tonic-gate bool allow_any_ip = 0;		/* Allow peer to use any IP address */
1797c478bd9Sstevel@tonic-gate bool explicit_remote = 0;	/* User specified explicit remote name */
1807c478bd9Sstevel@tonic-gate char remote_name[MAXNAMELEN];	/* Peer's name for authentication */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #ifdef CHAPMS
1837c478bd9Sstevel@tonic-gate bool refuse_mschap = 0;		/* Don't wanna auth. ourself with MS-CHAPv1 */
1847c478bd9Sstevel@tonic-gate #else
1857c478bd9Sstevel@tonic-gate bool refuse_mschap = 1;		/* Never auth. ourself with MS-CHAPv1 */
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate #ifdef CHAPMSV2
1887c478bd9Sstevel@tonic-gate bool refuse_mschapv2 = 0;	/* Don't wanna auth. ourself with MS-CHAPv2 */
1897c478bd9Sstevel@tonic-gate #else
1907c478bd9Sstevel@tonic-gate bool refuse_mschapv2 = 1;	/* Never auth. ourself with MS-CHAPv2 */
1917c478bd9Sstevel@tonic-gate #endif
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate #ifdef USE_PAM
1947c478bd9Sstevel@tonic-gate bool use_pam = 1;		/* Enable use of PAM by default */
1957c478bd9Sstevel@tonic-gate #else
1967c478bd9Sstevel@tonic-gate bool use_pam = 0;		/* Disable use of PAM by default */
1977c478bd9Sstevel@tonic-gate #endif
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* Bits in auth_pending[] */
2007c478bd9Sstevel@tonic-gate #define PAP_WITHPEER	1
2017c478bd9Sstevel@tonic-gate #define PAP_PEER	2
2027c478bd9Sstevel@tonic-gate #define CHAP_WITHPEER	4
2037c478bd9Sstevel@tonic-gate #define CHAP_PEER	8
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /* Prototypes for procedures local to this file. */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate static void network_phase __P((int));
2087c478bd9Sstevel@tonic-gate static void check_idle __P((void *));
2097c478bd9Sstevel@tonic-gate static void connect_time_expired __P((void *));
2107c478bd9Sstevel@tonic-gate static int  plogin __P((char *, char *, char **));
2117c478bd9Sstevel@tonic-gate static void plogout __P((void));
2127c478bd9Sstevel@tonic-gate static int  null_login __P((int));
2137c478bd9Sstevel@tonic-gate static int  get_pap_passwd __P((char *));
2147c478bd9Sstevel@tonic-gate static int  have_pap_secret __P((int *));
2157c478bd9Sstevel@tonic-gate static int  have_chap_secret __P((char *, char *, int, int *));
2167c478bd9Sstevel@tonic-gate static int  ip_addr_check __P((u_int32_t, struct permitted_ip *));
2177c478bd9Sstevel@tonic-gate static int  scan_authfile __P((FILE *, char *, char *, char *,
2187c478bd9Sstevel@tonic-gate 			       struct wordlist **, struct wordlist **,
2197c478bd9Sstevel@tonic-gate 			       char *));
2207c478bd9Sstevel@tonic-gate static void free_wordlist __P((struct wordlist *));
2217c478bd9Sstevel@tonic-gate static void auth_script __P((char *));
2227c478bd9Sstevel@tonic-gate static void auth_script_done __P((void *, int));
2237c478bd9Sstevel@tonic-gate static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
2247c478bd9Sstevel@tonic-gate static int  some_ip_ok __P((struct wordlist *));
2257c478bd9Sstevel@tonic-gate static int  setupapfile __P((char **, option_t *));
2267c478bd9Sstevel@tonic-gate static int  privgroup __P((char **, option_t *));
2277c478bd9Sstevel@tonic-gate static int  set_noauth_addr __P((char **, option_t *));
2287c478bd9Sstevel@tonic-gate static void check_access __P((FILE *, char *));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * Authentication-related options.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate option_t auth_options[] = {
2347c478bd9Sstevel@tonic-gate     { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
2357c478bd9Sstevel@tonic-gate       "Require PAP authentication from peer", 1, &auth_required },
2367c478bd9Sstevel@tonic-gate     { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
2377c478bd9Sstevel@tonic-gate       "Require PAP authentication from peer", 1, &auth_required },
2387c478bd9Sstevel@tonic-gate     { "refuse-pap", o_bool, &refuse_pap,
2397c478bd9Sstevel@tonic-gate       "Don't agree to auth to peer with PAP", 1 },
2407c478bd9Sstevel@tonic-gate     { "-pap", o_bool, &refuse_pap,
2417c478bd9Sstevel@tonic-gate       "Don't allow PAP authentication with peer", 1 },
2427c478bd9Sstevel@tonic-gate     { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
2437c478bd9Sstevel@tonic-gate       "Require CHAP authentication from peer", 1, &auth_required },
2447c478bd9Sstevel@tonic-gate     { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
2457c478bd9Sstevel@tonic-gate       "Require CHAP authentication from peer", 1, &auth_required },
2467c478bd9Sstevel@tonic-gate     { "refuse-chap", o_bool, &refuse_chap,
2477c478bd9Sstevel@tonic-gate       "Don't agree to auth to peer with CHAP", 1 },
2487c478bd9Sstevel@tonic-gate     { "-chap", o_bool, &refuse_chap,
2497c478bd9Sstevel@tonic-gate       "Don't allow CHAP authentication with peer", 1 },
2507c478bd9Sstevel@tonic-gate     { "name", o_string, our_name,
2517c478bd9Sstevel@tonic-gate       "Set local name for authentication",
2527c478bd9Sstevel@tonic-gate       OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
2537c478bd9Sstevel@tonic-gate     { "user", o_string, user,
2547c478bd9Sstevel@tonic-gate       "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
2557c478bd9Sstevel@tonic-gate     { "usehostname", o_bool, &usehostname,
2567c478bd9Sstevel@tonic-gate       "Must use hostname for authentication", 1 },
2577c478bd9Sstevel@tonic-gate     { "remotename", o_string, remote_name,
2587c478bd9Sstevel@tonic-gate       "Set remote name for authentication", OPT_STATIC,
2597c478bd9Sstevel@tonic-gate       &explicit_remote, MAXNAMELEN },
2607c478bd9Sstevel@tonic-gate     { "auth", o_bool, &auth_required,
2617c478bd9Sstevel@tonic-gate       "Require authentication from peer", 1 },
2627c478bd9Sstevel@tonic-gate     { "noauth", o_bool, &auth_required,
2637c478bd9Sstevel@tonic-gate       "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
2647c478bd9Sstevel@tonic-gate     {  "login", o_bool, &uselogin,
2657c478bd9Sstevel@tonic-gate       "Use system password database for PAP", 1 },
2667c478bd9Sstevel@tonic-gate     { "papcrypt", o_bool, &cryptpap,
2677c478bd9Sstevel@tonic-gate       "PAP passwords are encrypted", 1 },
2687c478bd9Sstevel@tonic-gate     { "+ua", o_special, (void *)setupapfile,
2697c478bd9Sstevel@tonic-gate       "Get PAP user and password from file" },
2707c478bd9Sstevel@tonic-gate     { "password", o_string, passwd,
2717c478bd9Sstevel@tonic-gate       "Password for authenticating us to the peer", OPT_STATIC,
2727c478bd9Sstevel@tonic-gate       NULL, MAXSECRETLEN },
2737c478bd9Sstevel@tonic-gate     { "privgroup", o_special, (void *)privgroup,
2747c478bd9Sstevel@tonic-gate       "Allow group members to use privileged options", OPT_PRIV },
2757c478bd9Sstevel@tonic-gate     { "allow-ip", o_special, (void *)set_noauth_addr,
2767c478bd9Sstevel@tonic-gate       "Set peer IP address(es) usable without authentication",
2777c478bd9Sstevel@tonic-gate       OPT_PRIV },
2787c478bd9Sstevel@tonic-gate #ifdef CHAPMS
2797c478bd9Sstevel@tonic-gate     { "require-mschap", o_bool, &lcp_wantoptions[0].neg_mschap,
2807c478bd9Sstevel@tonic-gate       "Require MS-CHAPv1 authentication from peer", 1, &auth_required },
2817c478bd9Sstevel@tonic-gate     { "refuse-mschap", o_bool, &refuse_mschap,
2827c478bd9Sstevel@tonic-gate       "Don't agree to authenticate to peer with MS-CHAPv1", 1 },
2837c478bd9Sstevel@tonic-gate #endif
2847c478bd9Sstevel@tonic-gate #ifdef CHAPMSV2
2857c478bd9Sstevel@tonic-gate     { "require-mschapv2", o_bool, &lcp_wantoptions[0].neg_mschapv2,
2867c478bd9Sstevel@tonic-gate       "Require MS-CHAPv2 authentication from peer", 1, &auth_required },
2877c478bd9Sstevel@tonic-gate     { "refuse-mschapv2", o_bool, &refuse_mschapv2,
2887c478bd9Sstevel@tonic-gate       "Don't agree to authenticate to peer with MS-CHAPv2", 1 },
2897c478bd9Sstevel@tonic-gate #endif
2907c478bd9Sstevel@tonic-gate #ifdef ALLOW_PAM
2917c478bd9Sstevel@tonic-gate     { "pam", o_bool, &use_pam,
2927c478bd9Sstevel@tonic-gate       "Enable use of Pluggable Authentication Modules", OPT_PRIV|1 },
2937c478bd9Sstevel@tonic-gate     { "nopam", o_bool, &use_pam,
2947c478bd9Sstevel@tonic-gate       "Disable use of Pluggable Authentication Modules", OPT_PRIV|0 },
2957c478bd9Sstevel@tonic-gate #endif
2967c478bd9Sstevel@tonic-gate     { NULL }
2977c478bd9Sstevel@tonic-gate };
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * setupapfile - specifies UPAP info for authenticating with peer.
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3037c478bd9Sstevel@tonic-gate static int
setupapfile(argv,opt)3047c478bd9Sstevel@tonic-gate setupapfile(argv, opt)
3057c478bd9Sstevel@tonic-gate     char **argv;
3067c478bd9Sstevel@tonic-gate     option_t *opt;
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate     FILE * ufile;
3097c478bd9Sstevel@tonic-gate     int l;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate     lcp_allowoptions[0].neg_upap = 1;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate     /* open user info file */
3147c478bd9Sstevel@tonic-gate     (void) seteuid(getuid());
3157c478bd9Sstevel@tonic-gate     ufile = fopen(*argv, "r");
3167c478bd9Sstevel@tonic-gate     (void) seteuid(0);
3177c478bd9Sstevel@tonic-gate     if (ufile == NULL) {
3187c478bd9Sstevel@tonic-gate 	option_error("unable to open user login data file %s", *argv);
3197c478bd9Sstevel@tonic-gate 	return 0;
3207c478bd9Sstevel@tonic-gate     }
3217c478bd9Sstevel@tonic-gate     check_access(ufile, *argv);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate     /* get username */
3247c478bd9Sstevel@tonic-gate     if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
3257c478bd9Sstevel@tonic-gate 	|| fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
3267c478bd9Sstevel@tonic-gate 	option_error("unable to read user login data file %s", *argv);
3277c478bd9Sstevel@tonic-gate 	return 0;
3287c478bd9Sstevel@tonic-gate     }
3297c478bd9Sstevel@tonic-gate     (void) fclose(ufile);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate     /* get rid of newlines */
3327c478bd9Sstevel@tonic-gate     l = strlen(user);
3337c478bd9Sstevel@tonic-gate     if (l > 0 && user[l-1] == '\n')
3347c478bd9Sstevel@tonic-gate 	user[l-1] = '\0';
3357c478bd9Sstevel@tonic-gate     l = strlen(passwd);
3367c478bd9Sstevel@tonic-gate     if (l > 0 && passwd[l-1] == '\n')
3377c478bd9Sstevel@tonic-gate 	passwd[l-1] = '\0';
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate     return (1);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * privgroup - allow members of the group to have privileged access.
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3477c478bd9Sstevel@tonic-gate static int
privgroup(argv,opt)3487c478bd9Sstevel@tonic-gate privgroup(argv, opt)
3497c478bd9Sstevel@tonic-gate     char **argv;
3507c478bd9Sstevel@tonic-gate     option_t *opt;
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate     struct group *g;
3537c478bd9Sstevel@tonic-gate     int i;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate     g = getgrnam(*argv);
3567c478bd9Sstevel@tonic-gate     if (g == NULL) {
3577c478bd9Sstevel@tonic-gate 	option_error("group %s is unknown", *argv);
3587c478bd9Sstevel@tonic-gate 	return 0;
3597c478bd9Sstevel@tonic-gate     }
3607c478bd9Sstevel@tonic-gate     for (i = 0; i < ngroups; ++i) {
3617c478bd9Sstevel@tonic-gate 	if (groups[i] == g->gr_gid) {
3627c478bd9Sstevel@tonic-gate 	    privileged = 1;
3637c478bd9Sstevel@tonic-gate 	    break;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate     }
3667c478bd9Sstevel@tonic-gate     return 1;
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate  * set_noauth_addr - set address(es) that can be used without authentication.
3727c478bd9Sstevel@tonic-gate  * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3757c478bd9Sstevel@tonic-gate static int
set_noauth_addr(argv,opt)3767c478bd9Sstevel@tonic-gate set_noauth_addr(argv, opt)
3777c478bd9Sstevel@tonic-gate     char **argv;
3787c478bd9Sstevel@tonic-gate     option_t *opt;
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate     char *addr = *argv;
3817c478bd9Sstevel@tonic-gate     int l = strlen(addr);
3827c478bd9Sstevel@tonic-gate     struct wordlist *wp;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate     wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l + 1);
3857c478bd9Sstevel@tonic-gate     if (wp == NULL)
3867c478bd9Sstevel@tonic-gate 	novm("allow-ip argument");
3877c478bd9Sstevel@tonic-gate     wp->word = (char *) (wp + 1);
3887c478bd9Sstevel@tonic-gate     wp->next = noauth_addrs;
3897c478bd9Sstevel@tonic-gate     (void) strcpy(wp->word, addr);
3907c478bd9Sstevel@tonic-gate     noauth_addrs = wp;
3917c478bd9Sstevel@tonic-gate     return 1;
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate  * An Open on LCP has requested a change from Dead to Establish phase.
3967c478bd9Sstevel@tonic-gate  * Do what's necessary to bring the physical layer up.
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3997c478bd9Sstevel@tonic-gate void
link_required(unit)4007c478bd9Sstevel@tonic-gate link_required(unit)
4017c478bd9Sstevel@tonic-gate     int unit;
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * LCP has terminated the link; go to the Dead phase and take the
4077c478bd9Sstevel@tonic-gate  * physical layer down.
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4107c478bd9Sstevel@tonic-gate void
link_terminated(unit)4117c478bd9Sstevel@tonic-gate link_terminated(unit)
4127c478bd9Sstevel@tonic-gate     int unit;
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate     const char *pn1, *pn2;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate     if (phase == PHASE_DEAD)
4177c478bd9Sstevel@tonic-gate 	return;
4187c478bd9Sstevel@tonic-gate     if (pap_logout_hook != NULL) {
4197c478bd9Sstevel@tonic-gate 	(*pap_logout_hook)();
4207c478bd9Sstevel@tonic-gate     } else {
4217c478bd9Sstevel@tonic-gate 	if (logged_in)
4227c478bd9Sstevel@tonic-gate 	    plogout();
4237c478bd9Sstevel@tonic-gate     }
4247c478bd9Sstevel@tonic-gate     new_phase(PHASE_DEAD);
4257c478bd9Sstevel@tonic-gate     if (peer_nak_auth) {
4267c478bd9Sstevel@tonic-gate 	if ((pn1 = protocol_name(nak_auth_orig)) == NULL)
4277c478bd9Sstevel@tonic-gate 	    pn1 = "?";
4287c478bd9Sstevel@tonic-gate 	if ((pn2 = protocol_name(nak_auth_proto)) == NULL)
4297c478bd9Sstevel@tonic-gate 	    pn2 = "?";
4307c478bd9Sstevel@tonic-gate 	warn("Peer sent Configure-Nak for 0x%x (%s) to suggest 0x%x (%s)",
4317c478bd9Sstevel@tonic-gate 	    nak_auth_orig, pn1, nak_auth_proto, pn2);
4327c478bd9Sstevel@tonic-gate     }
4337c478bd9Sstevel@tonic-gate     if (unsolicited_nak_auth) {
4347c478bd9Sstevel@tonic-gate 	if ((pn1 = protocol_name(unsolicit_auth_proto)) == NULL)
4357c478bd9Sstevel@tonic-gate 	    pn1 = "?";
4367c478bd9Sstevel@tonic-gate 	warn("Peer unexpectedly asked us to authenticate with 0x%x (%s)",
4377c478bd9Sstevel@tonic-gate 	    unsolicit_auth_proto, pn1);
4387c478bd9Sstevel@tonic-gate     }
4397c478bd9Sstevel@tonic-gate     if (peer_reject_auth) {
4407c478bd9Sstevel@tonic-gate 	if ((pn1 = protocol_name(reject_auth_proto)) == NULL)
4417c478bd9Sstevel@tonic-gate 	    pn1 = "?";
4427c478bd9Sstevel@tonic-gate 	warn("Peer rejected our demand for 0x%x (%s)",
4437c478bd9Sstevel@tonic-gate 	    reject_auth_proto, pn1);
4447c478bd9Sstevel@tonic-gate     }
4457c478bd9Sstevel@tonic-gate     if (naked_peers_auth) {
4467c478bd9Sstevel@tonic-gate 	if ((pn1 = protocol_name(naked_auth_orig)) == NULL)
4477c478bd9Sstevel@tonic-gate 	    pn1 = "?";
4487c478bd9Sstevel@tonic-gate 	if ((pn2 = protocol_name(naked_auth_proto)) == NULL)
4497c478bd9Sstevel@tonic-gate 	    pn2 = "?";
4507c478bd9Sstevel@tonic-gate 	warn("We set Configure-Nak for 0x%x (%s) to suggest 0x%x (%s)",
4517c478bd9Sstevel@tonic-gate 	    naked_auth_orig, pn1, naked_auth_proto, pn2);
4527c478bd9Sstevel@tonic-gate     }
4537c478bd9Sstevel@tonic-gate     if (rejected_peers_auth) {
4547c478bd9Sstevel@tonic-gate 	if ((pn1 = protocol_name(rejected_auth_proto)) == NULL)
4557c478bd9Sstevel@tonic-gate 	    pn1 = "?";
4567c478bd9Sstevel@tonic-gate 	warn("We rejected the peer's demand for 0x%x (%s)",
4577c478bd9Sstevel@tonic-gate 	    rejected_auth_proto, pn1);
4587c478bd9Sstevel@tonic-gate     }
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate     peer_nak_auth = unsolicited_nak_auth = peer_reject_auth =
4617c478bd9Sstevel@tonic-gate 	rejected_peers_auth = naked_peers_auth = 0;
4627c478bd9Sstevel@tonic-gate     nak_auth_proto = nak_auth_orig = unsolicit_auth_proto = reject_auth_proto =
4637c478bd9Sstevel@tonic-gate 	rejected_auth_proto = naked_auth_orig = naked_auth_proto = 0;
4647c478bd9Sstevel@tonic-gate     notice("Connection terminated.");
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * LCP has gone down; it will either die or try to re-establish.
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate void
link_down(unit)4717c478bd9Sstevel@tonic-gate link_down(unit)
4727c478bd9Sstevel@tonic-gate     int unit;
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate     int i;
4757c478bd9Sstevel@tonic-gate     struct protent *protp;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate     auth_state = s_down;
4787c478bd9Sstevel@tonic-gate     if (auth_script_state == s_up && auth_script_pid == 0) {
4797c478bd9Sstevel@tonic-gate 	update_link_stats(unit);
4807c478bd9Sstevel@tonic-gate 	auth_script_state = s_down;
4817c478bd9Sstevel@tonic-gate 	auth_script(_PATH_AUTHDOWN);
4827c478bd9Sstevel@tonic-gate     }
4837c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i) {
4847c478bd9Sstevel@tonic-gate 	if (!protp->enabled_flag)
4857c478bd9Sstevel@tonic-gate 	    continue;
4867c478bd9Sstevel@tonic-gate         if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
4877c478bd9Sstevel@tonic-gate 	    (*protp->lowerdown)(unit);
4887c478bd9Sstevel@tonic-gate         if (protp->protocol < 0xC000 && protp->close != NULL)
4897c478bd9Sstevel@tonic-gate 	    (*protp->close)(unit, "LCP down");
4907c478bd9Sstevel@tonic-gate     }
4917c478bd9Sstevel@tonic-gate     num_np_open = 0;
4927c478bd9Sstevel@tonic-gate     num_np_up = 0;
4937c478bd9Sstevel@tonic-gate     if (phase != PHASE_DEAD)
4947c478bd9Sstevel@tonic-gate 	new_phase(PHASE_TERMINATE);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * The link is established.
4997c478bd9Sstevel@tonic-gate  * Proceed to the Dead, Authenticate or Network phase as appropriate.
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate void
link_established(unit)5027c478bd9Sstevel@tonic-gate link_established(unit)
5037c478bd9Sstevel@tonic-gate     int unit;
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate     int auth;
5067c478bd9Sstevel@tonic-gate     lcp_options *wo = &lcp_wantoptions[unit];
5077c478bd9Sstevel@tonic-gate     lcp_options *go = &lcp_gotoptions[unit];
5087c478bd9Sstevel@tonic-gate     lcp_options *ho = &lcp_hisoptions[unit];
5097c478bd9Sstevel@tonic-gate     int i;
5107c478bd9Sstevel@tonic-gate     struct protent *protp;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate     /*
5137c478bd9Sstevel@tonic-gate      * Tell higher-level protocols that LCP is up.
5147c478bd9Sstevel@tonic-gate      */
5157c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
5167c478bd9Sstevel@tonic-gate         if (protp->protocol != PPP_LCP && protp->enabled_flag
5177c478bd9Sstevel@tonic-gate 	    && protp->lowerup != NULL)
5187c478bd9Sstevel@tonic-gate 	    (*protp->lowerup)(unit);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate     if (auth_required && !(go->neg_chap || go->neg_mschap ||
5217c478bd9Sstevel@tonic-gate 	go->neg_mschapv2 || go->neg_upap)) {
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * We wanted the peer to authenticate itself, and it refused:
5247c478bd9Sstevel@tonic-gate 	 * if we have some address(es) it can use without auth, fine,
5257c478bd9Sstevel@tonic-gate 	 * otherwise treat it as though it authenticated with PAP using
5267c478bd9Sstevel@tonic-gate 	 * a username * of "" and a password of "".  If that's not OK,
5277c478bd9Sstevel@tonic-gate 	 * boot it out.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	if (noauth_addrs != NULL) {
5307c478bd9Sstevel@tonic-gate 	    set_allowed_addrs(unit, noauth_addrs, NULL);
5317c478bd9Sstevel@tonic-gate 	} else if (!wo->neg_upap || !null_login(unit)) {
5327c478bd9Sstevel@tonic-gate 	    warn("peer refused to authenticate: terminating link");
5337c478bd9Sstevel@tonic-gate 	    lcp_close(unit, "peer refused to authenticate");
5347c478bd9Sstevel@tonic-gate 	    status = EXIT_PEER_AUTH_FAILED;
5357c478bd9Sstevel@tonic-gate 	    return;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate     }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate     new_phase(PHASE_AUTHENTICATE);
5407c478bd9Sstevel@tonic-gate     auth = 0;
5417c478bd9Sstevel@tonic-gate     if (go->neg_chap || go->neg_mschap || go->neg_mschapv2) {
5427c478bd9Sstevel@tonic-gate 	if (go->neg_chap) {
5437c478bd9Sstevel@tonic-gate 	    if (debug)
5447c478bd9Sstevel@tonic-gate 		dbglog("Authenticating peer with standard CHAP");
5457c478bd9Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_DIGEST_MD5;
5467c478bd9Sstevel@tonic-gate 	} else if (go->neg_mschap) {
5477c478bd9Sstevel@tonic-gate 	    if (debug)
5487c478bd9Sstevel@tonic-gate 		dbglog("Authenticating peer with MS-CHAPv1");
5497c478bd9Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT;
5507c478bd9Sstevel@tonic-gate 	} else {
5517c478bd9Sstevel@tonic-gate 	    if (debug)
5527c478bd9Sstevel@tonic-gate 		dbglog("Authenticating peer with MS-CHAPv2");
5537c478bd9Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT_V2;
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 	ChapAuthPeer(unit, our_name, go->chap_mdtype);
5567c478bd9Sstevel@tonic-gate 	auth |= CHAP_PEER;
5577c478bd9Sstevel@tonic-gate     } else if (go->neg_upap) {
5587c478bd9Sstevel@tonic-gate 	if (debug)
5597c478bd9Sstevel@tonic-gate 	    dbglog("Authenticating peer with PAP");
5607c478bd9Sstevel@tonic-gate 	upap_authpeer(unit);
5617c478bd9Sstevel@tonic-gate 	auth |= PAP_PEER;
5627c478bd9Sstevel@tonic-gate     }
5637c478bd9Sstevel@tonic-gate     if (ho->neg_chap || ho->neg_mschap || ho->neg_mschapv2) {
5647c478bd9Sstevel@tonic-gate 	switch (ho->chap_mdtype) {
5657c478bd9Sstevel@tonic-gate 	case CHAP_DIGEST_MD5:
5667c478bd9Sstevel@tonic-gate 	    if (debug)
5677c478bd9Sstevel@tonic-gate 		dbglog("Authenticating to peer with standard CHAP");
5687c478bd9Sstevel@tonic-gate 	    break;
5697c478bd9Sstevel@tonic-gate 	case CHAP_MICROSOFT:
5707c478bd9Sstevel@tonic-gate 	    if (debug)
5717c478bd9Sstevel@tonic-gate 		dbglog("Authenticating to peer with MS-CHAPv1");
5727c478bd9Sstevel@tonic-gate 	    break;
5737c478bd9Sstevel@tonic-gate 	case CHAP_MICROSOFT_V2:
5747c478bd9Sstevel@tonic-gate 	    if (debug)
5757c478bd9Sstevel@tonic-gate 		dbglog("Authenticating to peer with MS-CHAPv2");
5767c478bd9Sstevel@tonic-gate 	    break;
5777c478bd9Sstevel@tonic-gate 	default:
5787c478bd9Sstevel@tonic-gate 	    if (debug)
5797c478bd9Sstevel@tonic-gate 		dbglog("Authenticating to peer with CHAP 0x%x", ho->chap_mdtype);
5807c478bd9Sstevel@tonic-gate 	    break;
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	ChapAuthWithPeer(unit, user, ho->chap_mdtype);
5837c478bd9Sstevel@tonic-gate 	auth |= CHAP_WITHPEER;
5847c478bd9Sstevel@tonic-gate     } else if (ho->neg_upap) {
5857c478bd9Sstevel@tonic-gate 	if (passwd[0] == '\0') {
5867c478bd9Sstevel@tonic-gate 	    passwd_from_file = 1;
5877c478bd9Sstevel@tonic-gate 	    if (!get_pap_passwd(passwd))
5887c478bd9Sstevel@tonic-gate 		error("No secret found for PAP login");
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	if (debug)
5917c478bd9Sstevel@tonic-gate 	    dbglog("Authenticating to peer with PAP");
5927c478bd9Sstevel@tonic-gate 	upap_authwithpeer(unit, user, passwd);
5937c478bd9Sstevel@tonic-gate 	auth |= PAP_WITHPEER;
5947c478bd9Sstevel@tonic-gate     }
5957c478bd9Sstevel@tonic-gate     auth_pending[unit] = auth;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate     if (!auth)
5987c478bd9Sstevel@tonic-gate 	network_phase(unit);
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate  * Proceed to the network phase.
6037c478bd9Sstevel@tonic-gate  */
6047c478bd9Sstevel@tonic-gate static void
network_phase(unit)6057c478bd9Sstevel@tonic-gate network_phase(unit)
6067c478bd9Sstevel@tonic-gate     int unit;
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate     lcp_options *go = &lcp_gotoptions[unit];
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate     /*
6117c478bd9Sstevel@tonic-gate      * If the peer had to authenticate, run the auth-up script now.
6127c478bd9Sstevel@tonic-gate      */
6137c478bd9Sstevel@tonic-gate     if (go->neg_chap || go->neg_mschap || go->neg_mschapv2 || go->neg_upap) {
6147c478bd9Sstevel@tonic-gate 	auth_state = s_up;
6157c478bd9Sstevel@tonic-gate 	if (auth_script_state == s_down && auth_script_pid == 0) {
6167c478bd9Sstevel@tonic-gate 	    auth_script_state = s_up;
6177c478bd9Sstevel@tonic-gate 	    auth_script(_PATH_AUTHUP);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate     }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate     /*
6227c478bd9Sstevel@tonic-gate      * Process extra options from the secrets file
6237c478bd9Sstevel@tonic-gate      */
6247c478bd9Sstevel@tonic-gate     if (extra_options != NULL) {
6257c478bd9Sstevel@tonic-gate 	option_source = (char *)extra_opt_filename;
6267c478bd9Sstevel@tonic-gate 	option_line = extra_opt_line;
6277c478bd9Sstevel@tonic-gate 	(void) options_from_list(extra_options, 1);
6287c478bd9Sstevel@tonic-gate 	free_wordlist(extra_options);
6297c478bd9Sstevel@tonic-gate 	extra_options = NULL;
6307c478bd9Sstevel@tonic-gate     }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate #ifdef CBCP_SUPPORT
6337c478bd9Sstevel@tonic-gate     /*
6347c478bd9Sstevel@tonic-gate      * If we negotiated callback, do it now.
6357c478bd9Sstevel@tonic-gate      */
6367c478bd9Sstevel@tonic-gate     if (go->neg_cbcp) {
6377c478bd9Sstevel@tonic-gate 	new_phase(PHASE_CALLBACK);
6387c478bd9Sstevel@tonic-gate 	(*cbcp_protent.open)(unit);
6397c478bd9Sstevel@tonic-gate 	return;
6407c478bd9Sstevel@tonic-gate     }
6417c478bd9Sstevel@tonic-gate #endif
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate     start_networks();
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate void
start_networks()6477c478bd9Sstevel@tonic-gate start_networks()
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate     int i;
6507c478bd9Sstevel@tonic-gate     struct protent *protp;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate     new_phase(PHASE_NETWORK);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
6557c478bd9Sstevel@tonic-gate     if (multilink) {
6567c478bd9Sstevel@tonic-gate 	if (mp_join_bundle()) {
6577c478bd9Sstevel@tonic-gate 	    if (updetach && !nodetach)
6587c478bd9Sstevel@tonic-gate 		detach();
6597c478bd9Sstevel@tonic-gate 	    return;
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate     }
6627c478bd9Sstevel@tonic-gate #endif /* HAVE_MULTILINK */
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate #if 0
6657c478bd9Sstevel@tonic-gate     if (!demand)
6667c478bd9Sstevel@tonic-gate 	set_filters(&pass_filter, &active_filter);
6677c478bd9Sstevel@tonic-gate #endif
6687c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
6697c478bd9Sstevel@tonic-gate         if (protp->protocol < 0xC000 && protp->enabled_flag
6707c478bd9Sstevel@tonic-gate 	    && protp->open != NULL) {
6717c478bd9Sstevel@tonic-gate 	    (*protp->open)(0);
6727c478bd9Sstevel@tonic-gate 	    if (protp->protocol != PPP_CCP)
6737c478bd9Sstevel@tonic-gate 		++num_np_open;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate     if (num_np_open == 0)
6777c478bd9Sstevel@tonic-gate 	/* nothing to do */
6787c478bd9Sstevel@tonic-gate 	lcp_close(0, "No network protocols running");
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * The peer has failed to authenticate himself using `protocol'.
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6857c478bd9Sstevel@tonic-gate void
auth_peer_fail(unit,protocol)6867c478bd9Sstevel@tonic-gate auth_peer_fail(unit, protocol)
6877c478bd9Sstevel@tonic-gate     int unit, protocol;
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate     /*
6907c478bd9Sstevel@tonic-gate      * Authentication failure: take the link down
6917c478bd9Sstevel@tonic-gate      */
6927c478bd9Sstevel@tonic-gate     lcp_close(unit, "Authentication failed");
6937c478bd9Sstevel@tonic-gate     status = EXIT_PEER_AUTH_FAILED;
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /*
6977c478bd9Sstevel@tonic-gate  * The peer has been successfully authenticated using `protocol'.
6987c478bd9Sstevel@tonic-gate  */
6997c478bd9Sstevel@tonic-gate void
auth_peer_success(unit,protocol,name,namelen)7007c478bd9Sstevel@tonic-gate auth_peer_success(unit, protocol, name, namelen)
7017c478bd9Sstevel@tonic-gate     int unit, protocol;
7027c478bd9Sstevel@tonic-gate     char *name;
7037c478bd9Sstevel@tonic-gate     int namelen;
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate     int bit;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate     switch (protocol) {
7087c478bd9Sstevel@tonic-gate     case PPP_CHAP:
7097c478bd9Sstevel@tonic-gate 	bit = CHAP_PEER;
7107c478bd9Sstevel@tonic-gate 	break;
7117c478bd9Sstevel@tonic-gate     case PPP_PAP:
7127c478bd9Sstevel@tonic-gate 	bit = PAP_PEER;
7137c478bd9Sstevel@tonic-gate 	break;
7147c478bd9Sstevel@tonic-gate     default:
7157c478bd9Sstevel@tonic-gate 	warn("auth_peer_success: unknown protocol %x", protocol);
7167c478bd9Sstevel@tonic-gate 	return;
7177c478bd9Sstevel@tonic-gate     }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate     /*
7207c478bd9Sstevel@tonic-gate      * Save the authenticated name of the peer for later.
7217c478bd9Sstevel@tonic-gate      */
7227c478bd9Sstevel@tonic-gate     if (namelen > sizeof(peer_authname) - 1)
7237c478bd9Sstevel@tonic-gate 	namelen = sizeof(peer_authname) - 1;
7247c478bd9Sstevel@tonic-gate     BCOPY(name, peer_authname, namelen);
7257c478bd9Sstevel@tonic-gate     peer_authname[namelen] = '\0';
7267c478bd9Sstevel@tonic-gate     script_setenv("PEERNAME", peer_authname, 0);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate     /*
7297c478bd9Sstevel@tonic-gate      * If there is no more authentication still to be done,
7307c478bd9Sstevel@tonic-gate      * proceed to the network (or callback) phase.
7317c478bd9Sstevel@tonic-gate      */
7327c478bd9Sstevel@tonic-gate     if ((auth_pending[unit] &= ~bit) == 0)
7337c478bd9Sstevel@tonic-gate         network_phase(unit);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate /*
7377c478bd9Sstevel@tonic-gate  * We have failed to authenticate ourselves to the peer using `protocol'.
7387c478bd9Sstevel@tonic-gate  */
7397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7407c478bd9Sstevel@tonic-gate void
auth_withpeer_fail(unit,protocol)7417c478bd9Sstevel@tonic-gate auth_withpeer_fail(unit, protocol)
7427c478bd9Sstevel@tonic-gate     int unit, protocol;
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate     if (passwd_from_file)
7457c478bd9Sstevel@tonic-gate 	BZERO(passwd, MAXSECRETLEN);
7467c478bd9Sstevel@tonic-gate     /*
7477c478bd9Sstevel@tonic-gate      * We've failed to authenticate ourselves to our peer.
7487c478bd9Sstevel@tonic-gate      * Some servers keep sending CHAP challenges, but there
7497c478bd9Sstevel@tonic-gate      * is no point in persisting without any way to get updated
7507c478bd9Sstevel@tonic-gate      * authentication secrets.
7517c478bd9Sstevel@tonic-gate      */
7527c478bd9Sstevel@tonic-gate     lcp_close(unit, "Failed to authenticate ourselves to peer");
7537c478bd9Sstevel@tonic-gate     status = EXIT_AUTH_TOPEER_FAILED;
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate  * We have successfully authenticated ourselves with the peer using `protocol'.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate void
auth_withpeer_success(unit,protocol)7607c478bd9Sstevel@tonic-gate auth_withpeer_success(unit, protocol)
7617c478bd9Sstevel@tonic-gate     int unit, protocol;
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate     int bit;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate     switch (protocol) {
7667c478bd9Sstevel@tonic-gate     case PPP_CHAP:
7677c478bd9Sstevel@tonic-gate 	bit = CHAP_WITHPEER;
7687c478bd9Sstevel@tonic-gate 	break;
7697c478bd9Sstevel@tonic-gate     case PPP_PAP:
7707c478bd9Sstevel@tonic-gate 	if (passwd_from_file)
7717c478bd9Sstevel@tonic-gate 	    BZERO(passwd, MAXSECRETLEN);
7727c478bd9Sstevel@tonic-gate 	bit = PAP_WITHPEER;
7737c478bd9Sstevel@tonic-gate 	break;
7747c478bd9Sstevel@tonic-gate     default:
7757c478bd9Sstevel@tonic-gate 	warn("auth_withpeer_success: unknown protocol %x", protocol);
7767c478bd9Sstevel@tonic-gate 	bit = 0;
7777c478bd9Sstevel@tonic-gate     }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate     /*
7807c478bd9Sstevel@tonic-gate      * If there is no more authentication still being done,
7817c478bd9Sstevel@tonic-gate      * proceed to the network (or callback) phase.
7827c478bd9Sstevel@tonic-gate      */
7837c478bd9Sstevel@tonic-gate     if ((auth_pending[unit] &= ~bit) == 0)
7847c478bd9Sstevel@tonic-gate 	network_phase(unit);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate  * np_up - a network protocol has come up.
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7927c478bd9Sstevel@tonic-gate void
np_up(unit,proto)7937c478bd9Sstevel@tonic-gate np_up(unit, proto)
7947c478bd9Sstevel@tonic-gate     int unit, proto;
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate     int tlim;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate     if (num_np_up == 0) {
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * At this point we consider that the link has come up successfully.
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	status = EXIT_OK;
8037c478bd9Sstevel@tonic-gate 	unsuccess = 0;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	peer_nak_auth = unsolicited_nak_auth = peer_reject_auth =
8067c478bd9Sstevel@tonic-gate 	    rejected_peers_auth = naked_peers_auth = 0;
8077c478bd9Sstevel@tonic-gate 	nak_auth_proto = nak_auth_orig = unsolicit_auth_proto =
8087c478bd9Sstevel@tonic-gate 	    reject_auth_proto = rejected_auth_proto = naked_auth_orig =
8097c478bd9Sstevel@tonic-gate 	    naked_auth_proto = 0;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	new_phase(PHASE_RUNNING);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (idle_time_hook != NULL)
8147c478bd9Sstevel@tonic-gate 	    tlim = (*idle_time_hook)(NULL);
8157c478bd9Sstevel@tonic-gate 	else
8167c478bd9Sstevel@tonic-gate 	    tlim = idle_time_limit;
8177c478bd9Sstevel@tonic-gate 	if (tlim > 0)
8187c478bd9Sstevel@tonic-gate 	    TIMEOUT(check_idle, NULL, tlim);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * Set a timeout to close the connection once the maximum
8227c478bd9Sstevel@tonic-gate 	 * connect time has expired.
8237c478bd9Sstevel@tonic-gate 	 */
8247c478bd9Sstevel@tonic-gate 	if (maxconnect > 0) {
8257c478bd9Sstevel@tonic-gate 	    TIMEOUT(connect_time_expired, &lcp_fsm[unit], maxconnect);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	    /*
8287c478bd9Sstevel@tonic-gate 	     * Tell LCP to send Time-Remaining packets.  One should be
8297c478bd9Sstevel@tonic-gate 	     * sent out now, at maxconnect-300, at maxconnect-120, and
8307c478bd9Sstevel@tonic-gate 	     * again at maxconnect-30.
8317c478bd9Sstevel@tonic-gate 	     */
8327c478bd9Sstevel@tonic-gate 	    lcp_settimeremaining(unit, maxconnect, maxconnect);
8337c478bd9Sstevel@tonic-gate 	    if (maxconnect > 300)
8347c478bd9Sstevel@tonic-gate 		lcp_settimeremaining(unit, maxconnect, 300);
8357c478bd9Sstevel@tonic-gate 	    if (maxconnect > 120)
8367c478bd9Sstevel@tonic-gate 		lcp_settimeremaining(unit, maxconnect, 120);
8377c478bd9Sstevel@tonic-gate 	    if (maxconnect > 30)
8387c478bd9Sstevel@tonic-gate 		lcp_settimeremaining(unit, maxconnect, 30);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	/*
8427c478bd9Sstevel@tonic-gate 	 * Detach now, if the updetach option was given.
8437c478bd9Sstevel@tonic-gate 	 */
8447c478bd9Sstevel@tonic-gate 	if (updetach && !nodetach)
8457c478bd9Sstevel@tonic-gate 	    detach();
8467c478bd9Sstevel@tonic-gate     }
8477c478bd9Sstevel@tonic-gate     ++num_np_up;
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * np_down - a network protocol has gone down.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8547c478bd9Sstevel@tonic-gate void
np_down(unit,proto)8557c478bd9Sstevel@tonic-gate np_down(unit, proto)
8567c478bd9Sstevel@tonic-gate     int unit, proto;
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate     if (--num_np_up == 0) {
8597c478bd9Sstevel@tonic-gate 	UNTIMEOUT(check_idle, NULL);
8607c478bd9Sstevel@tonic-gate 	new_phase(PHASE_NETWORK);
8617c478bd9Sstevel@tonic-gate     }
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate  * np_finished - a network protocol has finished using the link.
8667c478bd9Sstevel@tonic-gate  */
8677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8687c478bd9Sstevel@tonic-gate void
np_finished(unit,proto)8697c478bd9Sstevel@tonic-gate np_finished(unit, proto)
8707c478bd9Sstevel@tonic-gate     int unit, proto;
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate     if (--num_np_open <= 0) {
8737c478bd9Sstevel@tonic-gate 	/* no further use for the link: shut up shop. */
8747c478bd9Sstevel@tonic-gate 	lcp_close(0, "No network protocols running");
8757c478bd9Sstevel@tonic-gate     }
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate  * check_idle - check whether the link has been idle for long
8807c478bd9Sstevel@tonic-gate  * enough that we can shut it down.
8817c478bd9Sstevel@tonic-gate  */
8827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8837c478bd9Sstevel@tonic-gate static void
check_idle(arg)8847c478bd9Sstevel@tonic-gate check_idle(arg)
8857c478bd9Sstevel@tonic-gate     void *arg;
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate     struct ppp_idle idle;
8887c478bd9Sstevel@tonic-gate     time_t itime;
8897c478bd9Sstevel@tonic-gate     int tlim;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate     if (!get_idle_time(0, &idle))
8927c478bd9Sstevel@tonic-gate 	return;
8937c478bd9Sstevel@tonic-gate     if (idle_time_hook != NULL) {
8947c478bd9Sstevel@tonic-gate 	tlim = (*idle_time_hook)(&idle);
8957c478bd9Sstevel@tonic-gate     } else {
8967c478bd9Sstevel@tonic-gate 	itime = MIN(idle.xmit_idle, idle.recv_idle);
8977c478bd9Sstevel@tonic-gate 	tlim = idle_time_limit - itime;
8987c478bd9Sstevel@tonic-gate     }
8997c478bd9Sstevel@tonic-gate     if (tlim <= 0) {
9007c478bd9Sstevel@tonic-gate 	/* link is idle: shut it down. */
9017c478bd9Sstevel@tonic-gate 	notice("Terminating connection due to lack of activity.");
9027c478bd9Sstevel@tonic-gate 	lcp_close(0, "Link inactive");
9037c478bd9Sstevel@tonic-gate 	need_holdoff = 0;
9047c478bd9Sstevel@tonic-gate 	status = EXIT_IDLE_TIMEOUT;
9057c478bd9Sstevel@tonic-gate     } else {
9067c478bd9Sstevel@tonic-gate 	TIMEOUT(check_idle, NULL, tlim);
9077c478bd9Sstevel@tonic-gate     }
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * connect_time_expired - log a message and close the connection.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9147c478bd9Sstevel@tonic-gate static void
connect_time_expired(arg)9157c478bd9Sstevel@tonic-gate connect_time_expired(arg)
9167c478bd9Sstevel@tonic-gate     void *arg;
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate     fsm *f = (fsm *)arg;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate     info("Connect time expired");
9217c478bd9Sstevel@tonic-gate     lcp_close(f->unit, "Connect time expired");	/* Close connection */
9227c478bd9Sstevel@tonic-gate     status = EXIT_CONNECT_TIME;
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate  * auth_check_options - called to check authentication options.
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate void
auth_check_options()9297c478bd9Sstevel@tonic-gate auth_check_options()
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate     lcp_options *wo = &lcp_wantoptions[0];
9327c478bd9Sstevel@tonic-gate     int can_auth;
9337c478bd9Sstevel@tonic-gate     int lacks_ip;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate     /* Default our_name to hostname, and user to our_name */
9367c478bd9Sstevel@tonic-gate     if (our_name[0] == '\0' || usehostname)
9377c478bd9Sstevel@tonic-gate 	(void) strlcpy(our_name, hostname, sizeof(our_name));
9387c478bd9Sstevel@tonic-gate     if (user[0] == '\0')
9397c478bd9Sstevel@tonic-gate 	(void) strlcpy(user, our_name, sizeof(user));
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate     /*
9427c478bd9Sstevel@tonic-gate      * If we have a default route, require the peer to authenticate
9437c478bd9Sstevel@tonic-gate      * unless the noauth option was given or the real user is root.
9447c478bd9Sstevel@tonic-gate      */
9457c478bd9Sstevel@tonic-gate     if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
9467c478bd9Sstevel@tonic-gate 	auth_required = 1;
9477c478bd9Sstevel@tonic-gate 	default_auth = 1;
9487c478bd9Sstevel@tonic-gate     }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate     /* If authentication is required, ask peer for CHAP or PAP. */
9517c478bd9Sstevel@tonic-gate     if (auth_required) {
9527c478bd9Sstevel@tonic-gate 	if (!wo->neg_chap && !wo->neg_mschap && !wo->neg_mschapv2 &&
9537c478bd9Sstevel@tonic-gate 	    !wo->neg_upap) {
9547c478bd9Sstevel@tonic-gate 	    wo->neg_chap = 1;
9557c478bd9Sstevel@tonic-gate #ifdef CHAPMS
9567c478bd9Sstevel@tonic-gate 	    wo->neg_mschap = 1;
9577c478bd9Sstevel@tonic-gate #endif
9587c478bd9Sstevel@tonic-gate #ifdef CHAPMSV2
9597c478bd9Sstevel@tonic-gate 	    wo->neg_mschapv2 = 1;
9607c478bd9Sstevel@tonic-gate #endif
9617c478bd9Sstevel@tonic-gate 	    wo->chap_mdtype = CHAP_DIGEST_MD5;
9627c478bd9Sstevel@tonic-gate 	    wo->neg_upap = 1;
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate     } else {
9657c478bd9Sstevel@tonic-gate 	wo->neg_chap = 0;
9667c478bd9Sstevel@tonic-gate 	wo->neg_mschap = 0;
9677c478bd9Sstevel@tonic-gate 	wo->neg_mschapv2 = 0;
9687c478bd9Sstevel@tonic-gate 	wo->neg_upap = 0;
9697c478bd9Sstevel@tonic-gate     }
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate     /*
9727c478bd9Sstevel@tonic-gate      * Check whether we have appropriate secrets to use
9737c478bd9Sstevel@tonic-gate      * to authenticate the peer.
9747c478bd9Sstevel@tonic-gate      */
9757c478bd9Sstevel@tonic-gate     lacks_ip = 0;
9767c478bd9Sstevel@tonic-gate     can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
9777c478bd9Sstevel@tonic-gate     if (!can_auth && (wo->neg_chap || wo->neg_mschap || wo->neg_mschapv2)) {
9787c478bd9Sstevel@tonic-gate 	can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
9797c478bd9Sstevel@tonic-gate 				    our_name, 1, &lacks_ip);
9807c478bd9Sstevel@tonic-gate     }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate     if (auth_required && !can_auth && noauth_addrs == NULL) {
9837c478bd9Sstevel@tonic-gate 	if (default_auth) {
9847c478bd9Sstevel@tonic-gate 	    option_error(
9857c478bd9Sstevel@tonic-gate "By default the remote system is required to authenticate itself");
9867c478bd9Sstevel@tonic-gate 	    option_error(
9877c478bd9Sstevel@tonic-gate "(because this system has a default route to the Internet)");
9887c478bd9Sstevel@tonic-gate 	} else if (explicit_remote)
9897c478bd9Sstevel@tonic-gate 	    option_error(
9907c478bd9Sstevel@tonic-gate "The remote system (%s) is required to authenticate itself",
9917c478bd9Sstevel@tonic-gate 			 remote_name);
9927c478bd9Sstevel@tonic-gate 	else
9937c478bd9Sstevel@tonic-gate 	    option_error(
9947c478bd9Sstevel@tonic-gate "The remote system is required to authenticate itself");
9957c478bd9Sstevel@tonic-gate 	option_error(
9967c478bd9Sstevel@tonic-gate "but I couldn't find any suitable secret (password) for it to use to do so.");
9977c478bd9Sstevel@tonic-gate 	if (lacks_ip)
9987c478bd9Sstevel@tonic-gate 	    option_error(
9997c478bd9Sstevel@tonic-gate "(None of the available passwords would let it use an IP address.)");
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	exit(1);
10027c478bd9Sstevel@tonic-gate     }
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate  * auth_reset - called when LCP is starting negotiations to recheck
10077c478bd9Sstevel@tonic-gate  * authentication options, i.e. whether we have appropriate secrets
10087c478bd9Sstevel@tonic-gate  * to use for authenticating ourselves and/or the peer.
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate void
auth_reset(unit)10117c478bd9Sstevel@tonic-gate auth_reset(unit)
10127c478bd9Sstevel@tonic-gate     int unit;
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate     lcp_options *go = &lcp_gotoptions[unit];
10157c478bd9Sstevel@tonic-gate     lcp_options *ao = &lcp_allowoptions[unit];
10167c478bd9Sstevel@tonic-gate     int havesecret;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate     ao->neg_upap = !refuse_pap && (passwd[0] != '\0' || get_pap_passwd(NULL));
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate     havesecret = passwd[0] != '\0' ||
10217c478bd9Sstevel@tonic-gate 	have_chap_secret(user, (explicit_remote? remote_name: NULL), 0, NULL);
10227c478bd9Sstevel@tonic-gate     ao->neg_chap = !refuse_chap && havesecret;
10237c478bd9Sstevel@tonic-gate     ao->neg_mschap = !refuse_mschap && havesecret;
10247c478bd9Sstevel@tonic-gate     ao->neg_mschapv2 = !refuse_mschapv2 && havesecret;
10257c478bd9Sstevel@tonic-gate     if (ao->neg_chap)
10267c478bd9Sstevel@tonic-gate 	ao->chap_mdtype = CHAP_DIGEST_MD5;
10277c478bd9Sstevel@tonic-gate     else if (ao->neg_mschap)
10287c478bd9Sstevel@tonic-gate 	ao->chap_mdtype = CHAP_MICROSOFT;
10297c478bd9Sstevel@tonic-gate     else
10307c478bd9Sstevel@tonic-gate 	ao->chap_mdtype = CHAP_MICROSOFT_V2;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate     if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
10337c478bd9Sstevel@tonic-gate 	go->neg_upap = 0;
10347c478bd9Sstevel@tonic-gate     if (go->neg_chap || go->neg_mschap || go->neg_mschapv2) {
10357c478bd9Sstevel@tonic-gate 	havesecret = have_chap_secret((explicit_remote? remote_name: NULL),
10367c478bd9Sstevel@tonic-gate 		our_name, 1, NULL);
10377c478bd9Sstevel@tonic-gate 	if (!havesecret)
10387c478bd9Sstevel@tonic-gate 	    go->neg_chap = go->neg_mschap = go->neg_mschapv2 = 0;
10397c478bd9Sstevel@tonic-gate 	else if (go->neg_chap)
10407c478bd9Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_DIGEST_MD5;
10417c478bd9Sstevel@tonic-gate 	else if (go->neg_mschap)
10427c478bd9Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT;
10437c478bd9Sstevel@tonic-gate 	else
10447c478bd9Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT_V2;
10457c478bd9Sstevel@tonic-gate     }
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate  * check_passwd - Check the user name and passwd against the PAP secrets
10517c478bd9Sstevel@tonic-gate  * file.  If requested, also check against the system password database,
10527c478bd9Sstevel@tonic-gate  * and login the user if OK.
10537c478bd9Sstevel@tonic-gate  *
10547c478bd9Sstevel@tonic-gate  * returns:
10557c478bd9Sstevel@tonic-gate  *	UPAP_AUTHNAK: Authentication failed.
10567c478bd9Sstevel@tonic-gate  *	UPAP_AUTHACK: Authentication succeeded.
10577c478bd9Sstevel@tonic-gate  * In either case, msg points to an appropriate message.
10587c478bd9Sstevel@tonic-gate  */
10597c478bd9Sstevel@tonic-gate int
check_passwd(unit,auser,userlen,apasswd,passwdlen,msg)10607c478bd9Sstevel@tonic-gate check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
10617c478bd9Sstevel@tonic-gate     int unit;
10627c478bd9Sstevel@tonic-gate     char *auser;
10637c478bd9Sstevel@tonic-gate     int userlen;
10647c478bd9Sstevel@tonic-gate     char *apasswd;
10657c478bd9Sstevel@tonic-gate     int passwdlen;
10667c478bd9Sstevel@tonic-gate     char **msg;
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate     int ret;
10697c478bd9Sstevel@tonic-gate     char *filename;
10707c478bd9Sstevel@tonic-gate     FILE *f;
10717c478bd9Sstevel@tonic-gate     struct wordlist *addrs = NULL, *opts = NULL;
10727c478bd9Sstevel@tonic-gate     char passwd[256], user[256];
10737c478bd9Sstevel@tonic-gate     char secret[MAXWORDLEN];
10747c478bd9Sstevel@tonic-gate     static int attempts = 0;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate     /*
10777c478bd9Sstevel@tonic-gate      * Make copies of apasswd and auser, then null-terminate them.
10787c478bd9Sstevel@tonic-gate      * If there are unprintable characters in the password, make
10797c478bd9Sstevel@tonic-gate      * them visible.
10807c478bd9Sstevel@tonic-gate      */
10817c478bd9Sstevel@tonic-gate     (void) slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
10827c478bd9Sstevel@tonic-gate     (void) slprintf(user, sizeof(user), "%.*v", userlen, auser);
10837c478bd9Sstevel@tonic-gate     *msg = "";
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate     /*
10867c478bd9Sstevel@tonic-gate      * Check if a plugin wants to handle this.
10877c478bd9Sstevel@tonic-gate      */
10887c478bd9Sstevel@tonic-gate     if (pap_auth_hook != NULL) {
10897c478bd9Sstevel@tonic-gate 	/* Set a default and allow the plug-in to change it. */
10907c478bd9Sstevel@tonic-gate 	extra_opt_filename = "plugin";
10917c478bd9Sstevel@tonic-gate 	extra_opt_line = 0;
10927c478bd9Sstevel@tonic-gate 	ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
10937c478bd9Sstevel@tonic-gate 	if (ret >= 0) {
10947c478bd9Sstevel@tonic-gate 	    if (ret > 0)
10957c478bd9Sstevel@tonic-gate 		set_allowed_addrs(unit, addrs, opts);
10967c478bd9Sstevel@tonic-gate 	    BZERO(passwd, sizeof(passwd));
10977c478bd9Sstevel@tonic-gate 	    if (addrs != NULL)
10987c478bd9Sstevel@tonic-gate 		free_wordlist(addrs);
10997c478bd9Sstevel@tonic-gate 	    return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate     }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate     /*
11047c478bd9Sstevel@tonic-gate      * Open the file of pap secrets and scan for a suitable secret
11057c478bd9Sstevel@tonic-gate      * for authenticating this user.
11067c478bd9Sstevel@tonic-gate      */
11077c478bd9Sstevel@tonic-gate     filename = _PATH_UPAPFILE;
11087c478bd9Sstevel@tonic-gate     addrs = opts = NULL;
11097c478bd9Sstevel@tonic-gate     ret = UPAP_AUTHNAK;
11107c478bd9Sstevel@tonic-gate     f = fopen(filename, "r");
11117c478bd9Sstevel@tonic-gate     if (f == NULL) {
11127c478bd9Sstevel@tonic-gate 	error("Can't open PAP password file %s: %m", filename);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate     } else {
11157c478bd9Sstevel@tonic-gate 	check_access(f, filename);
11167c478bd9Sstevel@tonic-gate 	if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
11177c478bd9Sstevel@tonic-gate 	    warn("no PAP secret found for %s", user);
11187c478bd9Sstevel@tonic-gate 	    if (scan_server_match_failed[0] != '\0')
11197c478bd9Sstevel@tonic-gate 		warn("possible configuration error: local name is %q, but "
11207c478bd9Sstevel@tonic-gate 		    "found %q instead", our_name, scan_server_match_failed);
11217c478bd9Sstevel@tonic-gate 	} else if (secret[0] != '\0') {
11227c478bd9Sstevel@tonic-gate 	    /* password given in pap-secrets - must match */
11237c478bd9Sstevel@tonic-gate 	    if ((!cryptpap && strcmp(passwd, secret) == 0)
11247c478bd9Sstevel@tonic-gate 		|| strcmp(crypt(passwd, secret), secret) == 0)
11257c478bd9Sstevel@tonic-gate 		ret = UPAP_AUTHACK;
11267c478bd9Sstevel@tonic-gate 	    else
11277c478bd9Sstevel@tonic-gate 		warn("PAP authentication failure for %s", user);
11287c478bd9Sstevel@tonic-gate 	} else if (uselogin) {
11297c478bd9Sstevel@tonic-gate 	    /* empty password in pap-secrets and login option */
11307c478bd9Sstevel@tonic-gate 	    ret = plogin(user, passwd, msg);
11317c478bd9Sstevel@tonic-gate 	    if (ret == UPAP_AUTHNAK)
11327c478bd9Sstevel@tonic-gate 		warn("PAP login failure for %s", user);
11337c478bd9Sstevel@tonic-gate 	} else {
11347c478bd9Sstevel@tonic-gate 	    /* empty password in pap-secrets and login option not used */
11357c478bd9Sstevel@tonic-gate 	    ret = UPAP_AUTHACK;
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 	(void) fclose(f);
11387c478bd9Sstevel@tonic-gate     }
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate     if (ret == UPAP_AUTHNAK) {
11417c478bd9Sstevel@tonic-gate         if (**msg == '\0')
11427c478bd9Sstevel@tonic-gate 	    *msg = "Login incorrect";
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * Frustrate passwd stealer programs.
11457c478bd9Sstevel@tonic-gate 	 * Allow 10 tries, but start backing off after 3 (stolen from login).
11467c478bd9Sstevel@tonic-gate 	 * On 10'th, drop the connection.
11477c478bd9Sstevel@tonic-gate 	 */
11487c478bd9Sstevel@tonic-gate 	if (attempts++ >= 10) {
11497c478bd9Sstevel@tonic-gate 	    warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
11507c478bd9Sstevel@tonic-gate 	    lcp_close(unit, "login failed");
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 	if (attempts > 3)
11537c478bd9Sstevel@tonic-gate 	    (void) sleep((u_int) (attempts - 3) * 5);
11547c478bd9Sstevel@tonic-gate 	if (opts != NULL)
11557c478bd9Sstevel@tonic-gate 	    free_wordlist(opts);
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate     } else {
11587c478bd9Sstevel@tonic-gate 	attempts = 0;			/* Reset count */
11597c478bd9Sstevel@tonic-gate 	if (**msg == '\0')
11607c478bd9Sstevel@tonic-gate 	    *msg = "Login ok";
11617c478bd9Sstevel@tonic-gate 	set_allowed_addrs(unit, addrs, opts);
11627c478bd9Sstevel@tonic-gate     }
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate     if (addrs != NULL)
11657c478bd9Sstevel@tonic-gate 	free_wordlist(addrs);
11667c478bd9Sstevel@tonic-gate     BZERO(passwd, sizeof(passwd));
11677c478bd9Sstevel@tonic-gate     BZERO(secret, sizeof(secret));
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate     return ret;
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate /*
11737c478bd9Sstevel@tonic-gate  * This function is needed for PAM.
11747c478bd9Sstevel@tonic-gate  */
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate #ifdef ALLOW_PAM
11777c478bd9Sstevel@tonic-gate /* Static variables used to communicate between the conversation function
11787c478bd9Sstevel@tonic-gate  * and the server_login function
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate static char *PAM_username;
11817c478bd9Sstevel@tonic-gate static char *PAM_password;
11827c478bd9Sstevel@tonic-gate static int PAM_error = 0;
11837c478bd9Sstevel@tonic-gate static pam_handle_t *pamh = NULL;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate /* PAM conversation function
11867c478bd9Sstevel@tonic-gate  * Here we assume (for now, at least) that echo on means login name, and
11877c478bd9Sstevel@tonic-gate  * echo off means password.
11887c478bd9Sstevel@tonic-gate  */
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
PAM_conv(int num_msg,const struct pam_message ** msg,struct pam_response ** resp,void * appdata_ptr)11917c478bd9Sstevel@tonic-gate static int PAM_conv (int num_msg,
11927c478bd9Sstevel@tonic-gate #ifndef SOL2
11937c478bd9Sstevel@tonic-gate     const
11947c478bd9Sstevel@tonic-gate #endif
11957c478bd9Sstevel@tonic-gate     struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate     int replies = 0;
11987c478bd9Sstevel@tonic-gate     struct pam_response *reply = NULL;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate #define COPY_STRING(s) (s) ? strdup(s) : NULL
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate     reply = malloc(sizeof(struct pam_response) * num_msg);
12037c478bd9Sstevel@tonic-gate     if (reply == NULL)
12047c478bd9Sstevel@tonic-gate 	return PAM_CONV_ERR;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate     for (replies = 0; replies < num_msg; replies++) {
12077c478bd9Sstevel@tonic-gate         switch (msg[replies]->msg_style) {
12087c478bd9Sstevel@tonic-gate             case PAM_PROMPT_ECHO_ON:
12097c478bd9Sstevel@tonic-gate                 reply[replies].resp_retcode = PAM_SUCCESS;
12107c478bd9Sstevel@tonic-gate                 reply[replies].resp = COPY_STRING(PAM_username);
12117c478bd9Sstevel@tonic-gate                 /* PAM frees resp */
12127c478bd9Sstevel@tonic-gate                 break;
12137c478bd9Sstevel@tonic-gate             case PAM_PROMPT_ECHO_OFF:
12147c478bd9Sstevel@tonic-gate                 reply[replies].resp_retcode = PAM_SUCCESS;
12157c478bd9