xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/pppd/auth.c (revision 5577:add1e851895c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * auth.c - PPP authentication and phase control.
30Sstevel@tonic-gate  *
4*5577Ssangeeta  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
50Sstevel@tonic-gate  * Use is subject to license terms.
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * Copyright (c) 1993 The Australian National University.
80Sstevel@tonic-gate  * All rights reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
110Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
120Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
130Sstevel@tonic-gate  * advertising materials, and other materials related to such
140Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
150Sstevel@tonic-gate  * by the Australian National University.  The name of the University
160Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
170Sstevel@tonic-gate  * software without specific prior written permission.
180Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
190Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
200Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
210Sstevel@tonic-gate  *
220Sstevel@tonic-gate  * Copyright (c) 1989 Carnegie Mellon University.
230Sstevel@tonic-gate  * All rights reserved.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
260Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
270Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
280Sstevel@tonic-gate  * advertising materials, and other materials related to such
290Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
300Sstevel@tonic-gate  * by Carnegie Mellon University.  The name of the
310Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
320Sstevel@tonic-gate  * from this software without specific prior written permission.
330Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
340Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
350Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
390Sstevel@tonic-gate #define RCSID	"$Id: auth.c,v 1.65 2000/04/15 01:27:10 masputra Exp $"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /* Pull in crypt() definition. */
420Sstevel@tonic-gate #define __EXTENSIONS__
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <stddef.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <unistd.h>
480Sstevel@tonic-gate #include <pwd.h>
490Sstevel@tonic-gate #include <grp.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <sys/types.h>
520Sstevel@tonic-gate #include <sys/stat.h>
530Sstevel@tonic-gate #include <sys/socket.h>
540Sstevel@tonic-gate #include <utmp.h>
550Sstevel@tonic-gate #include <fcntl.h>
560Sstevel@tonic-gate #if defined(_PATH_LASTLOG) && (defined(_linux_) || defined(__linux__))
570Sstevel@tonic-gate #include <lastlog.h>
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #if defined(_linux_) || defined(__linux__)
610Sstevel@tonic-gate #include <crypt.h>
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <netdb.h>
650Sstevel@tonic-gate #include <netinet/in.h>
660Sstevel@tonic-gate #include <arpa/inet.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* Backward compatibility with old Makefiles */
690Sstevel@tonic-gate #if defined(USE_PAM) && !defined(ALLOW_PAM)
700Sstevel@tonic-gate #define ALLOW_PAM
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #ifdef ALLOW_PAM
740Sstevel@tonic-gate #include <security/pam_appl.h>
750Sstevel@tonic-gate #endif
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #ifdef HAS_SHADOW
780Sstevel@tonic-gate #include <shadow.h>
790Sstevel@tonic-gate #ifndef PW_PPP
800Sstevel@tonic-gate #define PW_PPP PW_LOGIN
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #include "pppd.h"
850Sstevel@tonic-gate #include "fsm.h"
860Sstevel@tonic-gate #include "lcp.h"
870Sstevel@tonic-gate #include "ipcp.h"
880Sstevel@tonic-gate #include "upap.h"
890Sstevel@tonic-gate #include "chap.h"
900Sstevel@tonic-gate #ifdef CBCP_SUPPORT
910Sstevel@tonic-gate #include "cbcp.h"
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate #include "pathnames.h"
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #if !defined(lint) && !defined(_lint)
960Sstevel@tonic-gate static const char rcsid[] = RCSID;
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /* Bits in scan_authfile return value */
1000Sstevel@tonic-gate #define NONWILD_SERVER	1
1010Sstevel@tonic-gate #define NONWILD_CLIENT	2
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define ISWILD(word)	(word[0] == '*' && word[1] == '\0')
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /* The name by which the peer authenticated itself to us. */
1060Sstevel@tonic-gate char peer_authname[MAXNAMELEN];
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /* Records which authentication operations haven't completed yet. */
1090Sstevel@tonic-gate static int auth_pending[NUM_PPP];
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* Set if we have successfully called plogin() */
1120Sstevel@tonic-gate static int logged_in;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /* List of addresses which the peer may use. */
1150Sstevel@tonic-gate static struct permitted_ip *addresses[NUM_PPP];
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /* Wordlist giving addresses which the peer may use
1180Sstevel@tonic-gate    without authenticating itself. */
1190Sstevel@tonic-gate static struct wordlist *noauth_addrs;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /* Extra options to apply, from the secrets file entry for the peer. */
1220Sstevel@tonic-gate static struct wordlist *extra_options;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /* Source of those extra options. */
1250Sstevel@tonic-gate static const char *extra_opt_filename;
1260Sstevel@tonic-gate static int extra_opt_line;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /* Number of network protocols which we have opened. */
1290Sstevel@tonic-gate static int num_np_open;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /* Number of network protocols which have come up. */
1320Sstevel@tonic-gate static int num_np_up;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /* Set if we got the contents of passwd[] from the pap-secrets file. */
1350Sstevel@tonic-gate static int passwd_from_file;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /* Set if we require authentication only because we have a default route. */
1380Sstevel@tonic-gate static bool default_auth;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /* Hook to enable a plugin to control the idle time limit */
1410Sstevel@tonic-gate int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /* Hook for a plugin to say whether we can possibly authenticate any peer */
1440Sstevel@tonic-gate int (*pap_check_hook) __P((void)) = NULL;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* Hook for a plugin to check the PAP user and password */
1470Sstevel@tonic-gate int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
1480Sstevel@tonic-gate 			  struct wordlist **paddrs,
1490Sstevel@tonic-gate 			  struct wordlist **popts)) = NULL;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /* Hook for a plugin to know about the PAP user logout */
1520Sstevel@tonic-gate void (*pap_logout_hook) __P((void)) = NULL;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /* Hook for a plugin to get the PAP password for authenticating us */
1550Sstevel@tonic-gate int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * This is used to ensure that we don't start an auth-up/down
1590Sstevel@tonic-gate  * script while one is already running.
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate enum script_state {
1620Sstevel@tonic-gate     s_down,
1630Sstevel@tonic-gate     s_up
1640Sstevel@tonic-gate };
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static enum script_state auth_state = s_down;
1670Sstevel@tonic-gate static enum script_state auth_script_state = s_down;
1680Sstevel@tonic-gate static pid_t auth_script_pid = 0;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * This is set by scan_authfile if a client matches, but server doesn't
1720Sstevel@tonic-gate  * (possible configuration error).
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static char scan_server_match_failed[MAXWORDLEN];
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate  * Option variables.
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate bool uselogin = 0;		/* Use /etc/passwd for checking PAP */
1800Sstevel@tonic-gate bool cryptpap = 0;		/* Passwords in pap-secrets are encrypted */
1810Sstevel@tonic-gate bool refuse_pap = 0;		/* Don't wanna auth. ourselves with PAP */
1820Sstevel@tonic-gate bool refuse_chap = 0;		/* Don't wanna auth. ourselves with CHAP */
1830Sstevel@tonic-gate bool usehostname = 0;		/* Use hostname for our_name */
1840Sstevel@tonic-gate bool auth_required = 0;		/* Always require authentication from peer */
1850Sstevel@tonic-gate bool allow_any_ip = 0;		/* Allow peer to use any IP address */
1860Sstevel@tonic-gate bool explicit_remote = 0;	/* User specified explicit remote name */
1870Sstevel@tonic-gate char remote_name[MAXNAMELEN];	/* Peer's name for authentication */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #ifdef CHAPMS
1900Sstevel@tonic-gate bool refuse_mschap = 0;		/* Don't wanna auth. ourself with MS-CHAPv1 */
1910Sstevel@tonic-gate #else
1920Sstevel@tonic-gate bool refuse_mschap = 1;		/* Never auth. ourself with MS-CHAPv1 */
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate #ifdef CHAPMSV2
1950Sstevel@tonic-gate bool refuse_mschapv2 = 0;	/* Don't wanna auth. ourself with MS-CHAPv2 */
1960Sstevel@tonic-gate #else
1970Sstevel@tonic-gate bool refuse_mschapv2 = 1;	/* Never auth. ourself with MS-CHAPv2 */
1980Sstevel@tonic-gate #endif
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #ifdef USE_PAM
2010Sstevel@tonic-gate bool use_pam = 1;		/* Enable use of PAM by default */
2020Sstevel@tonic-gate #else
2030Sstevel@tonic-gate bool use_pam = 0;		/* Disable use of PAM by default */
2040Sstevel@tonic-gate #endif
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /* Bits in auth_pending[] */
2070Sstevel@tonic-gate #define PAP_WITHPEER	1
2080Sstevel@tonic-gate #define PAP_PEER	2
2090Sstevel@tonic-gate #define CHAP_WITHPEER	4
2100Sstevel@tonic-gate #define CHAP_PEER	8
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /* Prototypes for procedures local to this file. */
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static void network_phase __P((int));
2150Sstevel@tonic-gate static void check_idle __P((void *));
2160Sstevel@tonic-gate static void connect_time_expired __P((void *));
2170Sstevel@tonic-gate static int  plogin __P((char *, char *, char **));
2180Sstevel@tonic-gate static void plogout __P((void));
2190Sstevel@tonic-gate static int  null_login __P((int));
2200Sstevel@tonic-gate static int  get_pap_passwd __P((char *));
2210Sstevel@tonic-gate static int  have_pap_secret __P((int *));
2220Sstevel@tonic-gate static int  have_chap_secret __P((char *, char *, int, int *));
2230Sstevel@tonic-gate static int  ip_addr_check __P((u_int32_t, struct permitted_ip *));
2240Sstevel@tonic-gate static int  scan_authfile __P((FILE *, char *, char *, char *,
2250Sstevel@tonic-gate 			       struct wordlist **, struct wordlist **,
2260Sstevel@tonic-gate 			       char *));
2270Sstevel@tonic-gate static void free_wordlist __P((struct wordlist *));
2280Sstevel@tonic-gate static void auth_script __P((char *));
2290Sstevel@tonic-gate static void auth_script_done __P((void *, int));
2300Sstevel@tonic-gate static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
2310Sstevel@tonic-gate static int  some_ip_ok __P((struct wordlist *));
2320Sstevel@tonic-gate static int  setupapfile __P((char **, option_t *));
2330Sstevel@tonic-gate static int  privgroup __P((char **, option_t *));
2340Sstevel@tonic-gate static int  set_noauth_addr __P((char **, option_t *));
2350Sstevel@tonic-gate static void check_access __P((FILE *, char *));
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * Authentication-related options.
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate option_t auth_options[] = {
2410Sstevel@tonic-gate     { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
2420Sstevel@tonic-gate       "Require PAP authentication from peer", 1, &auth_required },
2430Sstevel@tonic-gate     { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
2440Sstevel@tonic-gate       "Require PAP authentication from peer", 1, &auth_required },
2450Sstevel@tonic-gate     { "refuse-pap", o_bool, &refuse_pap,
2460Sstevel@tonic-gate       "Don't agree to auth to peer with PAP", 1 },
2470Sstevel@tonic-gate     { "-pap", o_bool, &refuse_pap,
2480Sstevel@tonic-gate       "Don't allow PAP authentication with peer", 1 },
2490Sstevel@tonic-gate     { "require-chap", o_bool, &lcp_wantoptions[0].neg_chap,
2500Sstevel@tonic-gate       "Require CHAP authentication from peer", 1, &auth_required },
2510Sstevel@tonic-gate     { "+chap", o_bool, &lcp_wantoptions[0].neg_chap,
2520Sstevel@tonic-gate       "Require CHAP authentication from peer", 1, &auth_required },
2530Sstevel@tonic-gate     { "refuse-chap", o_bool, &refuse_chap,
2540Sstevel@tonic-gate       "Don't agree to auth to peer with CHAP", 1 },
2550Sstevel@tonic-gate     { "-chap", o_bool, &refuse_chap,
2560Sstevel@tonic-gate       "Don't allow CHAP authentication with peer", 1 },
2570Sstevel@tonic-gate     { "name", o_string, our_name,
2580Sstevel@tonic-gate       "Set local name for authentication",
2590Sstevel@tonic-gate       OPT_PRIV|OPT_STATIC, NULL, MAXNAMELEN },
2600Sstevel@tonic-gate     { "user", o_string, user,
2610Sstevel@tonic-gate       "Set name for auth with peer", OPT_STATIC, NULL, MAXNAMELEN },
2620Sstevel@tonic-gate     { "usehostname", o_bool, &usehostname,
2630Sstevel@tonic-gate       "Must use hostname for authentication", 1 },
2640Sstevel@tonic-gate     { "remotename", o_string, remote_name,
2650Sstevel@tonic-gate       "Set remote name for authentication", OPT_STATIC,
2660Sstevel@tonic-gate       &explicit_remote, MAXNAMELEN },
2670Sstevel@tonic-gate     { "auth", o_bool, &auth_required,
2680Sstevel@tonic-gate       "Require authentication from peer", 1 },
2690Sstevel@tonic-gate     { "noauth", o_bool, &auth_required,
2700Sstevel@tonic-gate       "Don't require peer to authenticate", OPT_PRIV, &allow_any_ip },
2710Sstevel@tonic-gate     {  "login", o_bool, &uselogin,
2720Sstevel@tonic-gate       "Use system password database for PAP", 1 },
2730Sstevel@tonic-gate     { "papcrypt", o_bool, &cryptpap,
2740Sstevel@tonic-gate       "PAP passwords are encrypted", 1 },
2750Sstevel@tonic-gate     { "+ua", o_special, (void *)setupapfile,
2760Sstevel@tonic-gate       "Get PAP user and password from file" },
2770Sstevel@tonic-gate     { "password", o_string, passwd,
2780Sstevel@tonic-gate       "Password for authenticating us to the peer", OPT_STATIC,
2790Sstevel@tonic-gate       NULL, MAXSECRETLEN },
2800Sstevel@tonic-gate     { "privgroup", o_special, (void *)privgroup,
2810Sstevel@tonic-gate       "Allow group members to use privileged options", OPT_PRIV },
2820Sstevel@tonic-gate     { "allow-ip", o_special, (void *)set_noauth_addr,
2830Sstevel@tonic-gate       "Set peer IP address(es) usable without authentication",
2840Sstevel@tonic-gate       OPT_PRIV },
2850Sstevel@tonic-gate #ifdef CHAPMS
2860Sstevel@tonic-gate     { "require-mschap", o_bool, &lcp_wantoptions[0].neg_mschap,
2870Sstevel@tonic-gate       "Require MS-CHAPv1 authentication from peer", 1, &auth_required },
2880Sstevel@tonic-gate     { "refuse-mschap", o_bool, &refuse_mschap,
2890Sstevel@tonic-gate       "Don't agree to authenticate to peer with MS-CHAPv1", 1 },
2900Sstevel@tonic-gate #endif
2910Sstevel@tonic-gate #ifdef CHAPMSV2
2920Sstevel@tonic-gate     { "require-mschapv2", o_bool, &lcp_wantoptions[0].neg_mschapv2,
2930Sstevel@tonic-gate       "Require MS-CHAPv2 authentication from peer", 1, &auth_required },
2940Sstevel@tonic-gate     { "refuse-mschapv2", o_bool, &refuse_mschapv2,
2950Sstevel@tonic-gate       "Don't agree to authenticate to peer with MS-CHAPv2", 1 },
2960Sstevel@tonic-gate #endif
2970Sstevel@tonic-gate #ifdef ALLOW_PAM
2980Sstevel@tonic-gate     { "pam", o_bool, &use_pam,
2990Sstevel@tonic-gate       "Enable use of Pluggable Authentication Modules", OPT_PRIV|1 },
3000Sstevel@tonic-gate     { "nopam", o_bool, &use_pam,
3010Sstevel@tonic-gate       "Disable use of Pluggable Authentication Modules", OPT_PRIV|0 },
3020Sstevel@tonic-gate #endif
3030Sstevel@tonic-gate     { NULL }
3040Sstevel@tonic-gate };
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * setupapfile - specifies UPAP info for authenticating with peer.
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate /*ARGSUSED*/
3100Sstevel@tonic-gate static int
setupapfile(argv,opt)3110Sstevel@tonic-gate setupapfile(argv, opt)
3120Sstevel@tonic-gate     char **argv;
3130Sstevel@tonic-gate     option_t *opt;
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate     FILE * ufile;
3160Sstevel@tonic-gate     int l;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate     lcp_allowoptions[0].neg_upap = 1;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate     /* open user info file */
3210Sstevel@tonic-gate     (void) seteuid(getuid());
3220Sstevel@tonic-gate     ufile = fopen(*argv, "r");
3230Sstevel@tonic-gate     (void) seteuid(0);
3240Sstevel@tonic-gate     if (ufile == NULL) {
3250Sstevel@tonic-gate 	option_error("unable to open user login data file %s", *argv);
3260Sstevel@tonic-gate 	return 0;
3270Sstevel@tonic-gate     }
3280Sstevel@tonic-gate     check_access(ufile, *argv);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate     /* get username */
3310Sstevel@tonic-gate     if (fgets(user, MAXNAMELEN - 1, ufile) == NULL
3320Sstevel@tonic-gate 	|| fgets(passwd, MAXSECRETLEN - 1, ufile) == NULL){
3330Sstevel@tonic-gate 	option_error("unable to read user login data file %s", *argv);
3340Sstevel@tonic-gate 	return 0;
3350Sstevel@tonic-gate     }
3360Sstevel@tonic-gate     (void) fclose(ufile);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate     /* get rid of newlines */
3390Sstevel@tonic-gate     l = strlen(user);
3400Sstevel@tonic-gate     if (l > 0 && user[l-1] == '\n')
3410Sstevel@tonic-gate 	user[l-1] = '\0';
3420Sstevel@tonic-gate     l = strlen(passwd);
3430Sstevel@tonic-gate     if (l > 0 && passwd[l-1] == '\n')
3440Sstevel@tonic-gate 	passwd[l-1] = '\0';
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate     return (1);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate  * privgroup - allow members of the group to have privileged access.
3520Sstevel@tonic-gate  */
3530Sstevel@tonic-gate /*ARGSUSED*/
3540Sstevel@tonic-gate static int
privgroup(argv,opt)3550Sstevel@tonic-gate privgroup(argv, opt)
3560Sstevel@tonic-gate     char **argv;
3570Sstevel@tonic-gate     option_t *opt;
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate     struct group *g;
3600Sstevel@tonic-gate     int i;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate     g = getgrnam(*argv);
3630Sstevel@tonic-gate     if (g == NULL) {
3640Sstevel@tonic-gate 	option_error("group %s is unknown", *argv);
3650Sstevel@tonic-gate 	return 0;
3660Sstevel@tonic-gate     }
3670Sstevel@tonic-gate     for (i = 0; i < ngroups; ++i) {
3680Sstevel@tonic-gate 	if (groups[i] == g->gr_gid) {
3690Sstevel@tonic-gate 	    privileged = 1;
3700Sstevel@tonic-gate 	    break;
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate     }
3730Sstevel@tonic-gate     return 1;
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * set_noauth_addr - set address(es) that can be used without authentication.
3790Sstevel@tonic-gate  * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate /*ARGSUSED*/
3820Sstevel@tonic-gate static int
set_noauth_addr(argv,opt)3830Sstevel@tonic-gate set_noauth_addr(argv, opt)
3840Sstevel@tonic-gate     char **argv;
3850Sstevel@tonic-gate     option_t *opt;
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate     char *addr = *argv;
3880Sstevel@tonic-gate     int l = strlen(addr);
3890Sstevel@tonic-gate     struct wordlist *wp;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate     wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l + 1);
3920Sstevel@tonic-gate     if (wp == NULL)
3930Sstevel@tonic-gate 	novm("allow-ip argument");
3940Sstevel@tonic-gate     wp->word = (char *) (wp + 1);
3950Sstevel@tonic-gate     wp->next = noauth_addrs;
3960Sstevel@tonic-gate     (void) strcpy(wp->word, addr);
3970Sstevel@tonic-gate     noauth_addrs = wp;
3980Sstevel@tonic-gate     return 1;
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate  * An Open on LCP has requested a change from Dead to Establish phase.
4030Sstevel@tonic-gate  * Do what's necessary to bring the physical layer up.
4040Sstevel@tonic-gate  */
4050Sstevel@tonic-gate /*ARGSUSED*/
4060Sstevel@tonic-gate void
link_required(unit)4070Sstevel@tonic-gate link_required(unit)
4080Sstevel@tonic-gate     int unit;
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate  * LCP has terminated the link; go to the Dead phase and take the
4140Sstevel@tonic-gate  * physical layer down.
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate /*ARGSUSED*/
4170Sstevel@tonic-gate void
link_terminated(unit)4180Sstevel@tonic-gate link_terminated(unit)
4190Sstevel@tonic-gate     int unit;
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate     const char *pn1, *pn2;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate     if (phase == PHASE_DEAD)
4240Sstevel@tonic-gate 	return;
4250Sstevel@tonic-gate     if (pap_logout_hook != NULL) {
4260Sstevel@tonic-gate 	(*pap_logout_hook)();
4270Sstevel@tonic-gate     } else {
4280Sstevel@tonic-gate 	if (logged_in)
4290Sstevel@tonic-gate 	    plogout();
4300Sstevel@tonic-gate     }
4310Sstevel@tonic-gate     new_phase(PHASE_DEAD);
4320Sstevel@tonic-gate     if (peer_nak_auth) {
4330Sstevel@tonic-gate 	if ((pn1 = protocol_name(nak_auth_orig)) == NULL)
4340Sstevel@tonic-gate 	    pn1 = "?";
4350Sstevel@tonic-gate 	if ((pn2 = protocol_name(nak_auth_proto)) == NULL)
4360Sstevel@tonic-gate 	    pn2 = "?";
4370Sstevel@tonic-gate 	warn("Peer sent Configure-Nak for 0x%x (%s) to suggest 0x%x (%s)",
4380Sstevel@tonic-gate 	    nak_auth_orig, pn1, nak_auth_proto, pn2);
4390Sstevel@tonic-gate     }
4400Sstevel@tonic-gate     if (unsolicited_nak_auth) {
4410Sstevel@tonic-gate 	if ((pn1 = protocol_name(unsolicit_auth_proto)) == NULL)
4420Sstevel@tonic-gate 	    pn1 = "?";
4430Sstevel@tonic-gate 	warn("Peer unexpectedly asked us to authenticate with 0x%x (%s)",
4440Sstevel@tonic-gate 	    unsolicit_auth_proto, pn1);
4450Sstevel@tonic-gate     }
4460Sstevel@tonic-gate     if (peer_reject_auth) {
4470Sstevel@tonic-gate 	if ((pn1 = protocol_name(reject_auth_proto)) == NULL)
4480Sstevel@tonic-gate 	    pn1 = "?";
4490Sstevel@tonic-gate 	warn("Peer rejected our demand for 0x%x (%s)",
4500Sstevel@tonic-gate 	    reject_auth_proto, pn1);
4510Sstevel@tonic-gate     }
4520Sstevel@tonic-gate     if (naked_peers_auth) {
4530Sstevel@tonic-gate 	if ((pn1 = protocol_name(naked_auth_orig)) == NULL)
4540Sstevel@tonic-gate 	    pn1 = "?";
4550Sstevel@tonic-gate 	if ((pn2 = protocol_name(naked_auth_proto)) == NULL)
4560Sstevel@tonic-gate 	    pn2 = "?";
4570Sstevel@tonic-gate 	warn("We set Configure-Nak for 0x%x (%s) to suggest 0x%x (%s)",
4580Sstevel@tonic-gate 	    naked_auth_orig, pn1, naked_auth_proto, pn2);
4590Sstevel@tonic-gate     }
4600Sstevel@tonic-gate     if (rejected_peers_auth) {
4610Sstevel@tonic-gate 	if ((pn1 = protocol_name(rejected_auth_proto)) == NULL)
4620Sstevel@tonic-gate 	    pn1 = "?";
4630Sstevel@tonic-gate 	warn("We rejected the peer's demand for 0x%x (%s)",
4640Sstevel@tonic-gate 	    rejected_auth_proto, pn1);
4650Sstevel@tonic-gate     }
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate     peer_nak_auth = unsolicited_nak_auth = peer_reject_auth =
4680Sstevel@tonic-gate 	rejected_peers_auth = naked_peers_auth = 0;
4690Sstevel@tonic-gate     nak_auth_proto = nak_auth_orig = unsolicit_auth_proto = reject_auth_proto =
4700Sstevel@tonic-gate 	rejected_auth_proto = naked_auth_orig = naked_auth_proto = 0;
4710Sstevel@tonic-gate     notice("Connection terminated.");
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * LCP has gone down; it will either die or try to re-establish.
4760Sstevel@tonic-gate  */
4770Sstevel@tonic-gate void
link_down(unit)4780Sstevel@tonic-gate link_down(unit)
4790Sstevel@tonic-gate     int unit;
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate     int i;
4820Sstevel@tonic-gate     struct protent *protp;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate     auth_state = s_down;
4850Sstevel@tonic-gate     if (auth_script_state == s_up && auth_script_pid == 0) {
4860Sstevel@tonic-gate 	update_link_stats(unit);
4870Sstevel@tonic-gate 	auth_script_state = s_down;
4880Sstevel@tonic-gate 	auth_script(_PATH_AUTHDOWN);
4890Sstevel@tonic-gate     }
4900Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i) {
4910Sstevel@tonic-gate 	if (!protp->enabled_flag)
4920Sstevel@tonic-gate 	    continue;
4930Sstevel@tonic-gate         if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
4940Sstevel@tonic-gate 	    (*protp->lowerdown)(unit);
4950Sstevel@tonic-gate         if (protp->protocol < 0xC000 && protp->close != NULL)
4960Sstevel@tonic-gate 	    (*protp->close)(unit, "LCP down");
4970Sstevel@tonic-gate     }
4980Sstevel@tonic-gate     num_np_open = 0;
4990Sstevel@tonic-gate     num_np_up = 0;
5000Sstevel@tonic-gate     if (phase != PHASE_DEAD)
5010Sstevel@tonic-gate 	new_phase(PHASE_TERMINATE);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate  * The link is established.
5060Sstevel@tonic-gate  * Proceed to the Dead, Authenticate or Network phase as appropriate.
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate void
link_established(unit)5090Sstevel@tonic-gate link_established(unit)
5100Sstevel@tonic-gate     int unit;
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate     int auth;
5130Sstevel@tonic-gate     lcp_options *wo = &lcp_wantoptions[unit];
5140Sstevel@tonic-gate     lcp_options *go = &lcp_gotoptions[unit];
5150Sstevel@tonic-gate     lcp_options *ho = &lcp_hisoptions[unit];
5160Sstevel@tonic-gate     int i;
5170Sstevel@tonic-gate     struct protent *protp;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate     /*
5200Sstevel@tonic-gate      * Tell higher-level protocols that LCP is up.
5210Sstevel@tonic-gate      */
5220Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
5230Sstevel@tonic-gate         if (protp->protocol != PPP_LCP && protp->enabled_flag
5240Sstevel@tonic-gate 	    && protp->lowerup != NULL)
5250Sstevel@tonic-gate 	    (*protp->lowerup)(unit);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate     if (auth_required && !(go->neg_chap || go->neg_mschap ||
5280Sstevel@tonic-gate 	go->neg_mschapv2 || go->neg_upap)) {
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	 * We wanted the peer to authenticate itself, and it refused:
5310Sstevel@tonic-gate 	 * if we have some address(es) it can use without auth, fine,
5320Sstevel@tonic-gate 	 * otherwise treat it as though it authenticated with PAP using
5330Sstevel@tonic-gate 	 * a username * of "" and a password of "".  If that's not OK,
5340Sstevel@tonic-gate 	 * boot it out.
5350Sstevel@tonic-gate 	 */
5360Sstevel@tonic-gate 	if (noauth_addrs != NULL) {
5370Sstevel@tonic-gate 	    set_allowed_addrs(unit, noauth_addrs, NULL);
5380Sstevel@tonic-gate 	} else if (!wo->neg_upap || !null_login(unit)) {
5390Sstevel@tonic-gate 	    warn("peer refused to authenticate: terminating link");
5400Sstevel@tonic-gate 	    lcp_close(unit, "peer refused to authenticate");
5410Sstevel@tonic-gate 	    status = EXIT_PEER_AUTH_FAILED;
5420Sstevel@tonic-gate 	    return;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate     }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate     new_phase(PHASE_AUTHENTICATE);
5470Sstevel@tonic-gate     auth = 0;
5480Sstevel@tonic-gate     if (go->neg_chap || go->neg_mschap || go->neg_mschapv2) {
5490Sstevel@tonic-gate 	if (go->neg_chap) {
5500Sstevel@tonic-gate 	    if (debug)
5510Sstevel@tonic-gate 		dbglog("Authenticating peer with standard CHAP");
5520Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_DIGEST_MD5;
5530Sstevel@tonic-gate 	} else if (go->neg_mschap) {
5540Sstevel@tonic-gate 	    if (debug)
5550Sstevel@tonic-gate 		dbglog("Authenticating peer with MS-CHAPv1");
5560Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT;
5570Sstevel@tonic-gate 	} else {
5580Sstevel@tonic-gate 	    if (debug)
5590Sstevel@tonic-gate 		dbglog("Authenticating peer with MS-CHAPv2");
5600Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT_V2;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 	ChapAuthPeer(unit, our_name, go->chap_mdtype);
5630Sstevel@tonic-gate 	auth |= CHAP_PEER;
5640Sstevel@tonic-gate     } else if (go->neg_upap) {
5650Sstevel@tonic-gate 	if (debug)
5660Sstevel@tonic-gate 	    dbglog("Authenticating peer with PAP");
5670Sstevel@tonic-gate 	upap_authpeer(unit);
5680Sstevel@tonic-gate 	auth |= PAP_PEER;
5690Sstevel@tonic-gate     }
5700Sstevel@tonic-gate     if (ho->neg_chap || ho->neg_mschap || ho->neg_mschapv2) {
5710Sstevel@tonic-gate 	switch (ho->chap_mdtype) {
5720Sstevel@tonic-gate 	case CHAP_DIGEST_MD5:
5730Sstevel@tonic-gate 	    if (debug)
5740Sstevel@tonic-gate 		dbglog("Authenticating to peer with standard CHAP");
5750Sstevel@tonic-gate 	    break;
5760Sstevel@tonic-gate 	case CHAP_MICROSOFT:
5770Sstevel@tonic-gate 	    if (debug)
5780Sstevel@tonic-gate 		dbglog("Authenticating to peer with MS-CHAPv1");
5790Sstevel@tonic-gate 	    break;
5800Sstevel@tonic-gate 	case CHAP_MICROSOFT_V2:
5810Sstevel@tonic-gate 	    if (debug)
5820Sstevel@tonic-gate 		dbglog("Authenticating to peer with MS-CHAPv2");
5830Sstevel@tonic-gate 	    break;
5840Sstevel@tonic-gate 	default:
5850Sstevel@tonic-gate 	    if (debug)
5860Sstevel@tonic-gate 		dbglog("Authenticating to peer with CHAP 0x%x", ho->chap_mdtype);
5870Sstevel@tonic-gate 	    break;
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 	ChapAuthWithPeer(unit, user, ho->chap_mdtype);
5900Sstevel@tonic-gate 	auth |= CHAP_WITHPEER;
5910Sstevel@tonic-gate     } else if (ho->neg_upap) {
5920Sstevel@tonic-gate 	if (passwd[0] == '\0') {
5930Sstevel@tonic-gate 	    passwd_from_file = 1;
5940Sstevel@tonic-gate 	    if (!get_pap_passwd(passwd))
5950Sstevel@tonic-gate 		error("No secret found for PAP login");
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 	if (debug)
5980Sstevel@tonic-gate 	    dbglog("Authenticating to peer with PAP");
5990Sstevel@tonic-gate 	upap_authwithpeer(unit, user, passwd);
6000Sstevel@tonic-gate 	auth |= PAP_WITHPEER;
6010Sstevel@tonic-gate     }
6020Sstevel@tonic-gate     auth_pending[unit] = auth;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate     if (!auth)
6050Sstevel@tonic-gate 	network_phase(unit);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate  * Proceed to the network phase.
6100Sstevel@tonic-gate  */
6110Sstevel@tonic-gate static void
network_phase(unit)6120Sstevel@tonic-gate network_phase(unit)
6130Sstevel@tonic-gate     int unit;
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate     lcp_options *go = &lcp_gotoptions[unit];
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate     /*
6180Sstevel@tonic-gate      * If the peer had to authenticate, run the auth-up script now.
6190Sstevel@tonic-gate      */
6200Sstevel@tonic-gate     if (go->neg_chap || go->neg_mschap || go->neg_mschapv2 || go->neg_upap) {
6210Sstevel@tonic-gate 	auth_state = s_up;
6220Sstevel@tonic-gate 	if (auth_script_state == s_down && auth_script_pid == 0) {
6230Sstevel@tonic-gate 	    auth_script_state = s_up;
6240Sstevel@tonic-gate 	    auth_script(_PATH_AUTHUP);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate     }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate     /*
6290Sstevel@tonic-gate      * Process extra options from the secrets file
6300Sstevel@tonic-gate      */
6310Sstevel@tonic-gate     if (extra_options != NULL) {
6320Sstevel@tonic-gate 	option_source = (char *)extra_opt_filename;
6330Sstevel@tonic-gate 	option_line = extra_opt_line;
6340Sstevel@tonic-gate 	(void) options_from_list(extra_options, 1);
6350Sstevel@tonic-gate 	free_wordlist(extra_options);
6360Sstevel@tonic-gate 	extra_options = NULL;
6370Sstevel@tonic-gate     }
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate #ifdef CBCP_SUPPORT
6400Sstevel@tonic-gate     /*
6410Sstevel@tonic-gate      * If we negotiated callback, do it now.
6420Sstevel@tonic-gate      */
6430Sstevel@tonic-gate     if (go->neg_cbcp) {
6440Sstevel@tonic-gate 	new_phase(PHASE_CALLBACK);
6450Sstevel@tonic-gate 	(*cbcp_protent.open)(unit);
6460Sstevel@tonic-gate 	return;
6470Sstevel@tonic-gate     }
6480Sstevel@tonic-gate #endif
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate     start_networks();
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate void
start_networks()6540Sstevel@tonic-gate start_networks()
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate     int i;
6570Sstevel@tonic-gate     struct protent *protp;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate     new_phase(PHASE_NETWORK);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate #ifdef HAVE_MULTILINK
6620Sstevel@tonic-gate     if (multilink) {
6630Sstevel@tonic-gate 	if (mp_join_bundle()) {
6640Sstevel@tonic-gate 	    if (updetach && !nodetach)
6650Sstevel@tonic-gate 		detach();
6660Sstevel@tonic-gate 	    return;
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate     }
6690Sstevel@tonic-gate #endif /* HAVE_MULTILINK */
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate #if 0
6720Sstevel@tonic-gate     if (!demand)
6730Sstevel@tonic-gate 	set_filters(&pass_filter, &active_filter);
6740Sstevel@tonic-gate #endif
6750Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
6760Sstevel@tonic-gate         if (protp->protocol < 0xC000 && protp->enabled_flag
6770Sstevel@tonic-gate 	    && protp->open != NULL) {
6780Sstevel@tonic-gate 	    (*protp->open)(0);
6790Sstevel@tonic-gate 	    if (protp->protocol != PPP_CCP)
6800Sstevel@tonic-gate 		++num_np_open;
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate     if (num_np_open == 0)
6840Sstevel@tonic-gate 	/* nothing to do */
6850Sstevel@tonic-gate 	lcp_close(0, "No network protocols running");
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /*
6890Sstevel@tonic-gate  * The peer has failed to authenticate himself using `protocol'.
6900Sstevel@tonic-gate  */
6910Sstevel@tonic-gate /*ARGSUSED*/
6920Sstevel@tonic-gate void
auth_peer_fail(unit,protocol)6930Sstevel@tonic-gate auth_peer_fail(unit, protocol)
6940Sstevel@tonic-gate     int unit, protocol;
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate     /*
6970Sstevel@tonic-gate      * Authentication failure: take the link down
6980Sstevel@tonic-gate      */
6990Sstevel@tonic-gate     lcp_close(unit, "Authentication failed");
7000Sstevel@tonic-gate     status = EXIT_PEER_AUTH_FAILED;
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * The peer has been successfully authenticated using `protocol'.
7050Sstevel@tonic-gate  */
7060Sstevel@tonic-gate void
auth_peer_success(unit,protocol,name,namelen)7070Sstevel@tonic-gate auth_peer_success(unit, protocol, name, namelen)
7080Sstevel@tonic-gate     int unit, protocol;
7090Sstevel@tonic-gate     char *name;
7100Sstevel@tonic-gate     int namelen;
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate     int bit;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate     switch (protocol) {
7150Sstevel@tonic-gate     case PPP_CHAP:
7160Sstevel@tonic-gate 	bit = CHAP_PEER;
7170Sstevel@tonic-gate 	break;
7180Sstevel@tonic-gate     case PPP_PAP:
7190Sstevel@tonic-gate 	bit = PAP_PEER;
7200Sstevel@tonic-gate 	break;
7210Sstevel@tonic-gate     default:
7220Sstevel@tonic-gate 	warn("auth_peer_success: unknown protocol %x", protocol);
7230Sstevel@tonic-gate 	return;
7240Sstevel@tonic-gate     }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate     /*
7270Sstevel@tonic-gate      * Save the authenticated name of the peer for later.
7280Sstevel@tonic-gate      */
7290Sstevel@tonic-gate     if (namelen > sizeof(peer_authname) - 1)
7300Sstevel@tonic-gate 	namelen = sizeof(peer_authname) - 1;
7310Sstevel@tonic-gate     BCOPY(name, peer_authname, namelen);
7320Sstevel@tonic-gate     peer_authname[namelen] = '\0';
7330Sstevel@tonic-gate     script_setenv("PEERNAME", peer_authname, 0);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate     /*
7360Sstevel@tonic-gate      * If there is no more authentication still to be done,
7370Sstevel@tonic-gate      * proceed to the network (or callback) phase.
7380Sstevel@tonic-gate      */
7390Sstevel@tonic-gate     if ((auth_pending[unit] &= ~bit) == 0)
7400Sstevel@tonic-gate         network_phase(unit);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate  * We have failed to authenticate ourselves to the peer using `protocol'.
7450Sstevel@tonic-gate  */
7460Sstevel@tonic-gate /*ARGSUSED*/
7470Sstevel@tonic-gate void
auth_withpeer_fail(unit,protocol)7480Sstevel@tonic-gate auth_withpeer_fail(unit, protocol)
7490Sstevel@tonic-gate     int unit, protocol;
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate     if (passwd_from_file)
7520Sstevel@tonic-gate 	BZERO(passwd, MAXSECRETLEN);
7530Sstevel@tonic-gate     /*
7540Sstevel@tonic-gate      * We've failed to authenticate ourselves to our peer.
7550Sstevel@tonic-gate      * Some servers keep sending CHAP challenges, but there
7560Sstevel@tonic-gate      * is no point in persisting without any way to get updated
7570Sstevel@tonic-gate      * authentication secrets.
7580Sstevel@tonic-gate      */
7590Sstevel@tonic-gate     lcp_close(unit, "Failed to authenticate ourselves to peer");
7600Sstevel@tonic-gate     status = EXIT_AUTH_TOPEER_FAILED;
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate /*
7640Sstevel@tonic-gate  * We have successfully authenticated ourselves with the peer using `protocol'.
7650Sstevel@tonic-gate  */
7660Sstevel@tonic-gate void
auth_withpeer_success(unit,protocol)7670Sstevel@tonic-gate auth_withpeer_success(unit, protocol)
7680Sstevel@tonic-gate     int unit, protocol;
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate     int bit;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate     switch (protocol) {
7730Sstevel@tonic-gate     case PPP_CHAP:
7740Sstevel@tonic-gate 	bit = CHAP_WITHPEER;
7750Sstevel@tonic-gate 	break;
7760Sstevel@tonic-gate     case PPP_PAP:
7770Sstevel@tonic-gate 	if (passwd_from_file)
7780Sstevel@tonic-gate 	    BZERO(passwd, MAXSECRETLEN);
7790Sstevel@tonic-gate 	bit = PAP_WITHPEER;
7800Sstevel@tonic-gate 	break;
7810Sstevel@tonic-gate     default:
7820Sstevel@tonic-gate 	warn("auth_withpeer_success: unknown protocol %x", protocol);
7830Sstevel@tonic-gate 	bit = 0;
7840Sstevel@tonic-gate     }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate     /*
7870Sstevel@tonic-gate      * If there is no more authentication still being done,
7880Sstevel@tonic-gate      * proceed to the network (or callback) phase.
7890Sstevel@tonic-gate      */
7900Sstevel@tonic-gate     if ((auth_pending[unit] &= ~bit) == 0)
7910Sstevel@tonic-gate 	network_phase(unit);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate /*
7960Sstevel@tonic-gate  * np_up - a network protocol has come up.
7970Sstevel@tonic-gate  */
7980Sstevel@tonic-gate /*ARGSUSED*/
7990Sstevel@tonic-gate void
np_up(unit,proto)8000Sstevel@tonic-gate np_up(unit, proto)
8010Sstevel@tonic-gate     int unit, proto;
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate     int tlim;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate     if (num_np_up == 0) {
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * At this point we consider that the link has come up successfully.
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	status = EXIT_OK;
8100Sstevel@tonic-gate 	unsuccess = 0;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	peer_nak_auth = unsolicited_nak_auth = peer_reject_auth =
8130Sstevel@tonic-gate 	    rejected_peers_auth = naked_peers_auth = 0;
8140Sstevel@tonic-gate 	nak_auth_proto = nak_auth_orig = unsolicit_auth_proto =
8150Sstevel@tonic-gate 	    reject_auth_proto = rejected_auth_proto = naked_auth_orig =
8160Sstevel@tonic-gate 	    naked_auth_proto = 0;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	new_phase(PHASE_RUNNING);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if (idle_time_hook != NULL)
8210Sstevel@tonic-gate 	    tlim = (*idle_time_hook)(NULL);
8220Sstevel@tonic-gate 	else
8230Sstevel@tonic-gate 	    tlim = idle_time_limit;
8240Sstevel@tonic-gate 	if (tlim > 0)
8250Sstevel@tonic-gate 	    TIMEOUT(check_idle, NULL, tlim);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * Set a timeout to close the connection once the maximum
8290Sstevel@tonic-gate 	 * connect time has expired.
8300Sstevel@tonic-gate 	 */
8310Sstevel@tonic-gate 	if (maxconnect > 0) {
8320Sstevel@tonic-gate 	    TIMEOUT(connect_time_expired, &lcp_fsm[unit], maxconnect);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	    /*
8350Sstevel@tonic-gate 	     * Tell LCP to send Time-Remaining packets.  One should be
8360Sstevel@tonic-gate 	     * sent out now, at maxconnect-300, at maxconnect-120, and
8370Sstevel@tonic-gate 	     * again at maxconnect-30.
8380Sstevel@tonic-gate 	     */
8390Sstevel@tonic-gate 	    lcp_settimeremaining(unit, maxconnect, maxconnect);
8400Sstevel@tonic-gate 	    if (maxconnect > 300)
8410Sstevel@tonic-gate 		lcp_settimeremaining(unit, maxconnect, 300);
8420Sstevel@tonic-gate 	    if (maxconnect > 120)
8430Sstevel@tonic-gate 		lcp_settimeremaining(unit, maxconnect, 120);
8440Sstevel@tonic-gate 	    if (maxconnect > 30)
8450Sstevel@tonic-gate 		lcp_settimeremaining(unit, maxconnect, 30);
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/*
8490Sstevel@tonic-gate 	 * Detach now, if the updetach option was given.
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	if (updetach && !nodetach)
8520Sstevel@tonic-gate 	    detach();
8530Sstevel@tonic-gate     }
8540Sstevel@tonic-gate     ++num_np_up;
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate /*
8580Sstevel@tonic-gate  * np_down - a network protocol has gone down.
8590Sstevel@tonic-gate  */
8600Sstevel@tonic-gate /*ARGSUSED*/
8610Sstevel@tonic-gate void
np_down(unit,proto)8620Sstevel@tonic-gate np_down(unit, proto)
8630Sstevel@tonic-gate     int unit, proto;
8640Sstevel@tonic-gate {
8650Sstevel@tonic-gate     if (--num_np_up == 0) {
8660Sstevel@tonic-gate 	UNTIMEOUT(check_idle, NULL);
8670Sstevel@tonic-gate 	new_phase(PHASE_NETWORK);
8680Sstevel@tonic-gate     }
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate /*
8720Sstevel@tonic-gate  * np_finished - a network protocol has finished using the link.
8730Sstevel@tonic-gate  */
8740Sstevel@tonic-gate /*ARGSUSED*/
8750Sstevel@tonic-gate void
np_finished(unit,proto)8760Sstevel@tonic-gate np_finished(unit, proto)
8770Sstevel@tonic-gate     int unit, proto;
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate     if (--num_np_open <= 0) {
8800Sstevel@tonic-gate 	/* no further use for the link: shut up shop. */
8810Sstevel@tonic-gate 	lcp_close(0, "No network protocols running");
8820Sstevel@tonic-gate     }
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*
8860Sstevel@tonic-gate  * check_idle - check whether the link has been idle for long
8870Sstevel@tonic-gate  * enough that we can shut it down.
8880Sstevel@tonic-gate  */
8890Sstevel@tonic-gate /*ARGSUSED*/
8900Sstevel@tonic-gate static void
check_idle(arg)8910Sstevel@tonic-gate check_idle(arg)
8920Sstevel@tonic-gate     void *arg;
8930Sstevel@tonic-gate {
8940Sstevel@tonic-gate     struct ppp_idle idle;
8950Sstevel@tonic-gate     time_t itime;
8960Sstevel@tonic-gate     int tlim;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate     if (!get_idle_time(0, &idle))
8990Sstevel@tonic-gate 	return;
9000Sstevel@tonic-gate     if (idle_time_hook != NULL) {
9010Sstevel@tonic-gate 	tlim = (*idle_time_hook)(&idle);
9020Sstevel@tonic-gate     } else {
9030Sstevel@tonic-gate 	itime = MIN(idle.xmit_idle, idle.recv_idle);
9040Sstevel@tonic-gate 	tlim = idle_time_limit - itime;
9050Sstevel@tonic-gate     }
9060Sstevel@tonic-gate     if (tlim <= 0) {
9070Sstevel@tonic-gate 	/* link is idle: shut it down. */
9080Sstevel@tonic-gate 	notice("Terminating connection due to lack of activity.");
9090Sstevel@tonic-gate 	lcp_close(0, "Link inactive");
9100Sstevel@tonic-gate 	need_holdoff = 0;
9110Sstevel@tonic-gate 	status = EXIT_IDLE_TIMEOUT;
9120Sstevel@tonic-gate     } else {
9130Sstevel@tonic-gate 	TIMEOUT(check_idle, NULL, tlim);
9140Sstevel@tonic-gate     }
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate /*
9180Sstevel@tonic-gate  * connect_time_expired - log a message and close the connection.
9190Sstevel@tonic-gate  */
9200Sstevel@tonic-gate /*ARGSUSED*/
9210Sstevel@tonic-gate static void
connect_time_expired(arg)9220Sstevel@tonic-gate connect_time_expired(arg)
9230Sstevel@tonic-gate     void *arg;
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate     fsm *f = (fsm *)arg;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate     info("Connect time expired");
9280Sstevel@tonic-gate     lcp_close(f->unit, "Connect time expired");	/* Close connection */
9290Sstevel@tonic-gate     status = EXIT_CONNECT_TIME;
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate  * auth_check_options - called to check authentication options.
9340Sstevel@tonic-gate  */
9350Sstevel@tonic-gate void
auth_check_options()9360Sstevel@tonic-gate auth_check_options()
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate     lcp_options *wo = &lcp_wantoptions[0];
9390Sstevel@tonic-gate     int can_auth;
9400Sstevel@tonic-gate     int lacks_ip;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate     /* Default our_name to hostname, and user to our_name */
9430Sstevel@tonic-gate     if (our_name[0] == '\0' || usehostname)
9440Sstevel@tonic-gate 	(void) strlcpy(our_name, hostname, sizeof(our_name));
9450Sstevel@tonic-gate     if (user[0] == '\0')
9460Sstevel@tonic-gate 	(void) strlcpy(user, our_name, sizeof(user));
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate     /*
9490Sstevel@tonic-gate      * If we have a default route, require the peer to authenticate
9500Sstevel@tonic-gate      * unless the noauth option was given or the real user is root.
9510Sstevel@tonic-gate      */
9520Sstevel@tonic-gate     if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
9530Sstevel@tonic-gate 	auth_required = 1;
9540Sstevel@tonic-gate 	default_auth = 1;
9550Sstevel@tonic-gate     }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate     /* If authentication is required, ask peer for CHAP or PAP. */
9580Sstevel@tonic-gate     if (auth_required) {
9590Sstevel@tonic-gate 	if (!wo->neg_chap && !wo->neg_mschap && !wo->neg_mschapv2 &&
9600Sstevel@tonic-gate 	    !wo->neg_upap) {
9610Sstevel@tonic-gate 	    wo->neg_chap = 1;
9620Sstevel@tonic-gate #ifdef CHAPMS
9630Sstevel@tonic-gate 	    wo->neg_mschap = 1;
9640Sstevel@tonic-gate #endif
9650Sstevel@tonic-gate #ifdef CHAPMSV2
9660Sstevel@tonic-gate 	    wo->neg_mschapv2 = 1;
9670Sstevel@tonic-gate #endif
9680Sstevel@tonic-gate 	    wo->chap_mdtype = CHAP_DIGEST_MD5;
9690Sstevel@tonic-gate 	    wo->neg_upap = 1;
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate     } else {
9720Sstevel@tonic-gate 	wo->neg_chap = 0;
9730Sstevel@tonic-gate 	wo->neg_mschap = 0;
9740Sstevel@tonic-gate 	wo->neg_mschapv2 = 0;
9750Sstevel@tonic-gate 	wo->neg_upap = 0;
9760Sstevel@tonic-gate     }
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate     /*
9790Sstevel@tonic-gate      * Check whether we have appropriate secrets to use
9800Sstevel@tonic-gate      * to authenticate the peer.
9810Sstevel@tonic-gate      */
9820Sstevel@tonic-gate     lacks_ip = 0;
9830Sstevel@tonic-gate     can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
9840Sstevel@tonic-gate     if (!can_auth && (wo->neg_chap || wo->neg_mschap || wo->neg_mschapv2)) {
9850Sstevel@tonic-gate 	can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
9860Sstevel@tonic-gate 				    our_name, 1, &lacks_ip);
9870Sstevel@tonic-gate     }
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate     if (auth_required && !can_auth && noauth_addrs == NULL) {
9900Sstevel@tonic-gate 	if (default_auth) {
9910Sstevel@tonic-gate 	    option_error(
9920Sstevel@tonic-gate "By default the remote system is required to authenticate itself");
9930Sstevel@tonic-gate 	    option_error(
9940Sstevel@tonic-gate "(because this system has a default route to the Internet)");
9950Sstevel@tonic-gate 	} else if (explicit_remote)
9960Sstevel@tonic-gate 	    option_error(
9970Sstevel@tonic-gate "The remote system (%s) is required to authenticate itself",
9980Sstevel@tonic-gate 			 remote_name);
9990Sstevel@tonic-gate 	else
10000Sstevel@tonic-gate 	    option_error(
10010Sstevel@tonic-gate "The remote system is required to authenticate itself");
10020Sstevel@tonic-gate 	option_error(
10030Sstevel@tonic-gate "but I couldn't find any suitable secret (password) for it to use to do so.");
10040Sstevel@tonic-gate 	if (lacks_ip)
10050Sstevel@tonic-gate 	    option_error(
10060Sstevel@tonic-gate "(None of the available passwords would let it use an IP address.)");
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	exit(1);
10090Sstevel@tonic-gate     }
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate /*
10130Sstevel@tonic-gate  * auth_reset - called when LCP is starting negotiations to recheck
10140Sstevel@tonic-gate  * authentication options, i.e. whether we have appropriate secrets
10150Sstevel@tonic-gate  * to use for authenticating ourselves and/or the peer.
10160Sstevel@tonic-gate  */
10170Sstevel@tonic-gate void
auth_reset(unit)10180Sstevel@tonic-gate auth_reset(unit)
10190Sstevel@tonic-gate     int unit;
10200Sstevel@tonic-gate {
10210Sstevel@tonic-gate     lcp_options *go = &lcp_gotoptions[unit];
10220Sstevel@tonic-gate     lcp_options *ao = &lcp_allowoptions[unit];
10230Sstevel@tonic-gate     int havesecret;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate     ao->neg_upap = !refuse_pap && (passwd[0] != '\0' || get_pap_passwd(NULL));
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate     havesecret = passwd[0] != '\0' ||
10280Sstevel@tonic-gate 	have_chap_secret(user, (explicit_remote? remote_name: NULL), 0, NULL);
10290Sstevel@tonic-gate     ao->neg_chap = !refuse_chap && havesecret;
10300Sstevel@tonic-gate     ao->neg_mschap = !refuse_mschap && havesecret;
10310Sstevel@tonic-gate     ao->neg_mschapv2 = !refuse_mschapv2 && havesecret;
10320Sstevel@tonic-gate     if (ao->neg_chap)
10330Sstevel@tonic-gate 	ao->chap_mdtype = CHAP_DIGEST_MD5;
10340Sstevel@tonic-gate     else if (ao->neg_mschap)
10350Sstevel@tonic-gate 	ao->chap_mdtype = CHAP_MICROSOFT;
10360Sstevel@tonic-gate     else
10370Sstevel@tonic-gate 	ao->chap_mdtype = CHAP_MICROSOFT_V2;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate     if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
10400Sstevel@tonic-gate 	go->neg_upap = 0;
10410Sstevel@tonic-gate     if (go->neg_chap || go->neg_mschap || go->neg_mschapv2) {
10420Sstevel@tonic-gate 	havesecret = have_chap_secret((explicit_remote? remote_name: NULL),
10430Sstevel@tonic-gate 		our_name, 1, NULL);
10440Sstevel@tonic-gate 	if (!havesecret)
10450Sstevel@tonic-gate 	    go->neg_chap = go->neg_mschap = go->neg_mschapv2 = 0;
10460Sstevel@tonic-gate 	else if (go->neg_chap)
10470Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_DIGEST_MD5;
10480Sstevel@tonic-gate 	else if (go->neg_mschap)
10490Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT;
10500Sstevel@tonic-gate 	else
10510Sstevel@tonic-gate 	    go->chap_mdtype = CHAP_MICROSOFT_V2;
10520Sstevel@tonic-gate     }
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate /*
10570Sstevel@tonic-gate  * check_passwd - Check the user name and passwd against the PAP secrets
10580Sstevel@tonic-gate  * file.  If requested, also check against the system password database,
10590Sstevel@tonic-gate  * and login the user if OK.
10600Sstevel@tonic-gate  *
10610Sstevel@tonic-gate  * returns:
10620Sstevel@tonic-gate  *	UPAP_AUTHNAK: Authentication failed.
10630Sstevel@tonic-gate  *	UPAP_AUTHACK: Authentication succeeded.
10640Sstevel@tonic-gate  * In either case, msg points to an appropriate message.
10650Sstevel@tonic-gate  */
10660Sstevel@tonic-gate int
check_passwd(unit,auser,userlen,apasswd,passwdlen,msg)10670Sstevel@tonic-gate check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
10680Sstevel@tonic-gate     int unit;
10690Sstevel@tonic-gate     char *auser;
10700Sstevel@tonic-gate     int userlen;
10710Sstevel@tonic-gate     char *apasswd;
10720Sstevel@tonic-gate     int passwdlen;
10730Sstevel@tonic-gate     char **msg;
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate     int ret;
10760Sstevel@tonic-gate     char *filename;
10770Sstevel@tonic-gate     FILE *f;
10780Sstevel@tonic-gate     struct wordlist *addrs = NULL, *opts = NULL;
10790Sstevel@tonic-gate     char passwd[256], user[256];
10800Sstevel@tonic-gate     char secret[MAXWORDLEN];
10810Sstevel@tonic-gate     static int attempts = 0;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate     /*
10840Sstevel@tonic-gate      * Make copies of apasswd and auser, then null-terminate them.
10850Sstevel@tonic-gate      * If there are unprintable characters in the password, make
10860Sstevel@tonic-gate      * them visible.
10870Sstevel@tonic-gate      */
10880Sstevel@tonic-gate     (void) slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
10890Sstevel@tonic-gate     (void) slprintf(user, sizeof(user), "%.*v", userlen, auser);
10900Sstevel@tonic-gate     *msg = "";
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate     /*
10930Sstevel@tonic-gate      * Check if a plugin wants to handle this.
10940Sstevel@tonic-gate      */
10950Sstevel@tonic-gate     if (pap_auth_hook != NULL) {
10960Sstevel@tonic-gate 	/* Set a default and allow the plug-in to change it. */
10970Sstevel@tonic-gate 	extra_opt_filename = "plugin";
10980Sstevel@tonic-gate 	extra_opt_line = 0;
10990Sstevel@tonic-gate 	ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
11000Sstevel@tonic-gate 	if (ret >= 0) {
11010Sstevel@tonic-gate 	    if (ret > 0)
11020Sstevel@tonic-gate 		set_allowed_addrs(unit, addrs, opts);
11030Sstevel@tonic-gate 	    BZERO(passwd, sizeof(passwd));
11040Sstevel@tonic-gate 	    if (addrs != NULL)
11050Sstevel@tonic-gate 		free_wordlist(addrs);
11060Sstevel@tonic-gate 	    return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate     }
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate     /*
11110Sstevel@tonic-gate      * Open the file of pap secrets and scan for a suitable secret
11120Sstevel@tonic-gate      * for authenticating this user.
11130Sstevel@tonic-gate      */
11140Sstevel@tonic-gate     filename = _PATH_UPAPFILE;
11150Sstevel@tonic-gate     addrs = opts = NULL;
11160Sstevel@tonic-gate     ret = UPAP_AUTHNAK;
11170Sstevel@tonic-gate     f = fopen(filename, "r");
11180Sstevel@tonic-gate     if (f == NULL) {
11190Sstevel@tonic-gate 	error("Can't open PAP password file %s: %m", filename);
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate     } else {
11220Sstevel@tonic-gate 	check_access(f, filename);
11230Sstevel@tonic-gate 	if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename) < 0) {
11240Sstevel@tonic-gate 	    warn("no PAP secret found for %s", user);
11250Sstevel@tonic-gate 	    if (scan_server_match_failed[0] != '\0')
11260Sstevel@tonic-gate 		warn("possible configuration error: local name is %q, but "
11270Sstevel@tonic-gate 		    "found %q instead", our_name, scan_server_match_failed);
11280Sstevel@tonic-gate 	} else if (secret[0] != '\0') {
11290Sstevel@tonic-gate 	    /* password given in pap-secrets - must match */
11300Sstevel@tonic-gate 	    if ((!cryptpap && strcmp(passwd, secret) == 0)
11310Sstevel@tonic-gate 		|| strcmp(crypt(passwd, secret), secret) == 0)
11320Sstevel@tonic-gate 		ret = UPAP_AUTHACK;
11330Sstevel@tonic-gate 	    else
11340Sstevel@tonic-gate 		warn("PAP authentication failure for %s", user);
11350Sstevel@tonic-gate 	} else if (uselogin) {
11360Sstevel@tonic-gate 	    /* empty password in pap-secrets and login option */
11370Sstevel@tonic-gate 	    ret = plogin(user, passwd, msg);
11380Sstevel@tonic-gate 	    if (ret == UPAP_AUTHNAK)
11390Sstevel@tonic-gate 		warn("PAP login failure for %s", user);
11400Sstevel@tonic-gate 	} else {
11410Sstevel@tonic-gate 	    /* empty password in pap-secrets and login option not used */
11420Sstevel@tonic-gate 	    ret = UPAP_AUTHACK;
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 	(void) fclose(f);
11450Sstevel@tonic-gate     }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate     if (ret == UPAP_AUTHNAK) {
11480Sstevel@tonic-gate         if (**msg == '\0')
11490Sstevel@tonic-gate 	    *msg = "Login incorrect";
11500Sstevel@tonic-gate 	/*
11510Sstevel@tonic-gate 	 * Frustrate passwd stealer programs.
11520Sstevel@tonic-gate 	 * Allow 10 tries, but start backing off after 3 (stolen from login).
11530Sstevel@tonic-gate 	 * On 10'th, drop the connection.
11540Sstevel@tonic-gate 	 */
11550Sstevel@tonic-gate 	if (attempts++ >= 10) {
11560Sstevel@tonic-gate 	    warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
11570Sstevel@tonic-gate 	    lcp_close(unit, "login failed");
11580Sstevel@tonic-gate 	}
11590Sstevel@tonic-gate 	if (attempts > 3)
11600Sstevel@tonic-gate 	    (void) sleep((u_int) (attempts - 3) * 5);
11610Sstevel@tonic-gate 	if (opts != NULL)
11620Sstevel@tonic-gate 	    free_wordlist(opts);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate     } else {
11650Sstevel@tonic-gate 	attempts = 0;			/* Reset count */
11660Sstevel@tonic-gate 	if (**msg == '\0')
11670Sstevel@tonic-gate 	    *msg = "Login ok";
11680Sstevel@tonic-gate 	set_allowed_addrs(unit, addrs, opts);
11690Sstevel@tonic-gate     }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate     if (addrs != NULL)
11720Sstevel@tonic-gate 	free_wordlist(addrs);
11730Sstevel@tonic-gate     BZERO(passwd, sizeof(passwd));
11740Sstevel@tonic-gate     BZERO(secret, sizeof(secret));
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate     return ret;
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate /*
11800Sstevel@tonic-gate  * This function is needed for PAM.
11810Sstevel@tonic-gate  */
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate #ifdef ALLOW_PAM
11840Sstevel@tonic-gate /* Static variables used to communicate between the conversation function
11850Sstevel@tonic-gate  * and the server_login function
11860Sstevel@tonic-gate  */
11870Sstevel@tonic-gate static char *PAM_username;
11880Sstevel@tonic-gate static char *PAM_password;
11890Sstevel@tonic-gate static int PAM_error = 0;
11900Sstevel@tonic-gate static pam_handle_t *pamh = NULL;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /* PAM conversation function
11930Sstevel@tonic-gate  * Here we assume (for now, at least) that echo on means login name, and
11940Sstevel@tonic-gate  * echo off means password.
11950Sstevel@tonic-gate  */
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate /*ARGSUSED*/
PAM_conv(int num_msg,const struct pam_message ** msg,struct pam_response ** resp,void * appdata_ptr)11980Sstevel@tonic-gate static int PAM_conv (int num_msg,
11990Sstevel@tonic-gate #ifndef SOL2
12000Sstevel@tonic-gate     const
12010Sstevel@tonic-gate #endif
12020Sstevel@tonic-gate     struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate     int replies = 0;
12050Sstevel@tonic-gate     struct pam_response *reply = NULL;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate #define COPY_STRING(s) (s) ? strdup(s) : NULL
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate     reply = malloc(sizeof(struct pam_response) * num_msg);
12100Sstevel@tonic-gate     if (reply == NULL)
12110Sstevel@tonic-gate 	return PAM_CONV_ERR;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate     for (replies = 0; replies < num_msg; replies++) {
12140Sstevel@tonic-gate         switch (msg[replies]->msg_style) {
12150Sstevel@tonic-gate             case PAM_PROMPT_ECHO_ON:
12160Sstevel@tonic-gate                 reply[replies].resp_retcode = PAM_SUCCESS;
12170Sstevel@tonic-gate                 reply[replies].resp = COPY_STRING(PAM_username);
12180Sstevel@tonic-gate                 /* PAM frees resp */
12190Sstevel@tonic-gate                 break;
12200Sstevel@tonic-gate             case PAM_PROMPT_ECHO_OFF:
12210Sstevel@tonic-gate                 reply[replies].resp_retcode = PAM_SUCCESS;
12220Sstevel@tonic-gate                 reply[replies].resp = COPY_STRING(PAM_password);
12230Sstevel@tonic-gate                 /* PAM frees resp */
12240Sstevel@tonic-gate                 break;
12250Sstevel@tonic-gate             case PAM_TEXT_INFO:
12260Sstevel@tonic-gate                 /* fall through */
12270Sstevel@tonic-gate             case PAM_ERROR_MSG:
12280Sstevel@tonic-gate                 /* ignore it, but pam still wants a NULL response... */
12290Sstevel@tonic-gate                 reply[replies].resp_retcode = PAM_SUCCESS;
12300Sstevel@tonic-gate                 reply[replies].resp = NULL;
12310Sstevel@tonic-gate                 break;
12320Sstevel@tonic-gate             default:
12330Sstevel@tonic-gate                 /* Must be an error of some sort... */
12340Sstevel@tonic-gate                 free (reply);
12350Sstevel@tonic-gate                 PAM_error = 1;
12360Sstevel@tonic-gate                 return PAM_CONV_ERR;
12370Sstevel@tonic-gate         }
12380Sstevel@tonic-gate     }
12390Sstevel@tonic-gate     *resp = reply;
12400Sstevel@tonic-gate     return PAM_SUCCESS;
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate static struct pam_conv PAM_conversation = {
12440Sstevel@tonic-gate 	PAM_conv, NULL
12450Sstevel@tonic-gate };
12460Sstevel@tonic-gate #endif  /* ALLOW_PAM */
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate  * plogin - Check the user name and password against the system
12500Sstevel@tonic-gate  * password database, and login the user if OK.
12510Sstevel@tonic-gate  *
12520Sstevel@tonic-gate  * returns:
12530Sstevel@tonic-gate  *	UPAP_AUTHNAK: Login failed.
12540Sstevel@tonic-gate  *	UPAP_AUTHACK: Login succeeded.
12550Sstevel@tonic-gate  * In either case, msg points to an appropriate message.
12560Sstevel@tonic-gate  */
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate static int
plogin(user,passwd,msg)12590Sstevel@tonic-gate plogin(user, passwd, msg)
12600Sstevel@tonic-gate     char *user;
12610Sstevel@tonic-gate     char *passwd;
12620Sstevel@tonic-gate     char **msg;
12630Sstevel@tonic-gate {
12640Sstevel@tonic-gate     char *tty;
12650Sstevel@tonic-gate #ifdef HAS_SHADOW
12660Sstevel@tonic-gate     struct spwd *spwd;
12670Sstevel@tonic-gate #endif
12680Sstevel@tonic-gate     struct passwd *pw = NULL;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate #ifdef ALLOW_PAM
12710Sstevel@tonic-gate     int pam_error;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate     if (use_pam) {
12740Sstevel@tonic-gate 	if (debug)
12750Sstevel@tonic-gate 	    dbglog("using PAM for user authentication");
12760Sstevel@tonic-gate 	pam_error = pam_start ("ppp", user, &PAM_conversation, &pamh);
12770Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
12780Sstevel@tonic-gate 	    *msg = (char *) pam_strerror (pamh, pam_error);
12790Sstevel@tonic-gate 	    reopen_log();
12800Sstevel@tonic-gate 	    return UPAP_AUTHNAK;
12810Sstevel@tonic-gate 	}
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * Define the fields for the credential validation
12840Sstevel@tonic-gate 	 */
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	PAM_username = user;
12870Sstevel@tonic-gate 	PAM_password = passwd;
12880Sstevel@tonic-gate 	PAM_error = 0;
12890Sstevel@tonic-gate 	/* this might be useful to some modules; required for Solaris */
12900Sstevel@tonic-gate 	tty = devnam;
12910Sstevel@tonic-gate 	if (*tty == '\0')
12920Sstevel@tonic-gate 	    tty = ppp_devnam;
12930Sstevel@tonic-gate 	(void) pam_set_item(pamh, PAM_TTY, tty);
12940Sstevel@tonic-gate #ifdef PAM_RHOST
12950Sstevel@tonic-gate 	(void) pam_set_item(pamh, PAM_RHOST, "");
12960Sstevel@tonic-gate #endif
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	/*
12990Sstevel@tonic-gate 	 * Validate the user
13000Sstevel@tonic-gate 	 */
13010Sstevel@tonic-gate 	pam_error = pam_authenticate (pamh, PAM_SILENT);
13020Sstevel@tonic-gate 	if (pam_error == PAM_SUCCESS && !PAM_error) {
13030Sstevel@tonic-gate 	    pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
13040Sstevel@tonic-gate 	    if (pam_error == PAM_SUCCESS)
13050Sstevel@tonic-gate 		(void) pam_open_session (pamh, PAM_SILENT);
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	*msg = (char *) pam_strerror (pamh, pam_error);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	/*
13110Sstevel@tonic-gate 	 * Clean up the mess
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 	reopen_log();	/* apparently the PAM stuff does closelog() */
13140Sstevel@tonic-gate 	PAM_username = NULL;
13150Sstevel@tonic-gate 	PAM_password = NULL;
13160Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS)
13170Sstevel@tonic-gate 	    return UPAP_AUTHNAK;
13180Sstevel@tonic-gate     } else
13190Sstevel@tonic-gate #endif /* ALLOW_PAM */
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate     {
13220Sstevel@tonic-gate 	if (debug) {
13230Sstevel@tonic-gate #ifdef HAS_SHADOW
13240Sstevel@tonic-gate 	    dbglog("using passwd/shadow for user authentication");
13250Sstevel@tonic-gate #else
13260Sstevel@tonic-gate 	    dbglog("using passwd for user authentication");
13270Sstevel@tonic-gate #endif
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate /*
13300Sstevel@tonic-gate  * Use the non-PAM methods directly
13310Sstevel@tonic-gate  */
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	pw = getpwnam(user);
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	endpwent();
13360Sstevel@tonic-gate 	if (pw == NULL)
13370Sstevel@tonic-gate 	    return (UPAP_AUTHNAK);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate #ifdef HAS_SHADOW
13400Sstevel@tonic-gate 	spwd = getspnam(user);
13410Sstevel@tonic-gate 	endspent();
13420Sstevel@tonic-gate 	if (spwd != NULL) {
13430Sstevel@tonic-gate 	    /* check the age of the password entry */
13440Sstevel@tonic-gate 	    long now = time(NULL) / 86400L;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	    if ((spwd->sp_expire > 0 && now >= spwd->sp_expire)
13470Sstevel@tonic-gate 		|| ((spwd->sp_max >= 0 && spwd->sp_max < 10000)
13480Sstevel@tonic-gate 		&& spwd->sp_lstchg >= 0
13490Sstevel@tonic-gate 		&& now >= spwd->sp_lstchg + spwd->sp_max)) {
13500Sstevel@tonic-gate 		warn("Password for %s has expired", user);
13510Sstevel@tonic-gate 		return (UPAP_AUTHNAK);
13520Sstevel@tonic-gate 	    }
13530Sstevel@tonic-gate 	    pw->pw_passwd = spwd->sp_pwdp;
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate #endif
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate     /*
13580Sstevel@tonic-gate      * If no passwd, don't let them login.
13590Sstevel@tonic-gate      */
13600Sstevel@tonic-gate 	if (pw->pw_passwd == NULL || strlen(pw->pw_passwd) < 2 ||
13610Sstevel@tonic-gate 	    strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd) != 0)
13620Sstevel@tonic-gate 	    return (UPAP_AUTHNAK);
13630Sstevel@tonic-gate     }
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate     /*
13660Sstevel@tonic-gate      * Write a wtmp entry for this user.
13670Sstevel@tonic-gate      */
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate     tty = devnam;
13700Sstevel@tonic-gate     if (strncmp(tty, "/dev/", 5) == 0)
13710Sstevel@tonic-gate 	tty += 5;
13720Sstevel@tonic-gate     logwtmp(tty, user, remote_name);		/* Add wtmp login entry */
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate #ifdef _PATH_LASTLOG
13750Sstevel@tonic-gate     if (!use_pam && pw != (struct passwd *)NULL) {
13760Sstevel@tonic-gate 	struct lastlog ll;
13770Sstevel@tonic-gate 	int fd;
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
13800Sstevel@tonic-gate 	   (void)lseek(fd, (off_t)(pw->pw_uid * sizeof(ll)), SEEK_SET);
13810Sstevel@tonic-gate 	    BZERO((void *)&ll, sizeof(ll));
13820Sstevel@tonic-gate 	    (void)time(&ll.ll_time);
13830Sstevel@tonic-gate 	    (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
13840Sstevel@tonic-gate 	    (void)write(fd, (char *)&ll, sizeof(ll));
13850Sstevel@tonic-gate 	    (void)close(fd);
13860Sstevel@tonic-gate 	}
13870Sstevel@tonic-gate     }
13880Sstevel@tonic-gate #endif /* _PATH_LASTLOG */
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate     info("user %s logged in", user);
13910Sstevel@tonic-gate     logged_in = 1;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate     return (UPAP_AUTHACK);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate /*
13970Sstevel@tonic-gate  * plogout - Logout the user.
13980Sstevel@tonic-gate  */
13990Sstevel@tonic-gate static void
plogout()14000Sstevel@tonic-gate plogout()
14010Sstevel@tonic-gate {
14020Sstevel@tonic-gate     char *tty;
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate #ifdef ALLOW_PAM
14050Sstevel@tonic-gate     int pam_error;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate     if (use_pam) {
14080Sstevel@tonic-gate 	if (pamh != NULL) {
14090Sstevel@tonic-gate 	    pam_error = pam_close_session (pamh, PAM_SILENT);
14100Sstevel@tonic-gate 	    (void) pam_end (pamh, pam_error);
14110Sstevel@tonic-gate 	    pamh = NULL;
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 	/* Apparently the pam stuff does closelog(). */
14140Sstevel@tonic-gate 	reopen_log();
14150Sstevel@tonic-gate     } else
14160Sstevel@tonic-gate #endif /* ALLOW_PAM */
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate     {
14190Sstevel@tonic-gate 	tty = devnam;
14200Sstevel@tonic-gate 	if (strncmp(tty, "/dev/", 5) == 0)
14210Sstevel@tonic-gate 	    tty += 5;
14220Sstevel@tonic-gate 	/* Wipe out utmp logout entry */
14230Sstevel@tonic-gate 	logwtmp(tty, "", "");
14240Sstevel@tonic-gate     }
14250Sstevel@tonic-gate     logged_in = 0;
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate /*
14300Sstevel@tonic-gate  * null_login - Check if a username of "" and a password of "" are
14310Sstevel@tonic-gate  * acceptable, and iff so, set the list of acceptable IP addresses
14320Sstevel@tonic-gate  * and return 1.
14330Sstevel@tonic-gate  */
14340Sstevel@tonic-gate static int
null_login(unit)14350Sstevel@tonic-gate null_login(unit)
14360Sstevel@tonic-gate     int unit;
14370Sstevel@tonic-gate {
14380Sstevel@tonic-gate     char *filename;
14390Sstevel@tonic-gate     FILE *f;
14400Sstevel@tonic-gate     int i, ret;
14410Sstevel@tonic-gate     struct wordlist *addrs, *opts;
14420Sstevel@tonic-gate     char secret[MAXWORDLEN];
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate     /*
14450Sstevel@tonic-gate      * Open the file of pap secrets and scan for a suitable secret.
14460Sstevel@tonic-gate      */
14470Sstevel@tonic-gate     filename = _PATH_UPAPFILE;
14480Sstevel@tonic-gate     addrs = NULL;
14490Sstevel@tonic-gate     f = fopen(filename, "r");
14500Sstevel@tonic-gate     if (f == NULL)
14510Sstevel@tonic-gate 	return 0;
14520Sstevel@tonic-gate     check_access(f, filename);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate     i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename);
14550Sstevel@tonic-gate     ret = i >= 0 && secret[0] == '\0';
14560Sstevel@tonic-gate     BZERO(secret, sizeof(secret));
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate     if (ret)
14590Sstevel@tonic-gate 	set_allowed_addrs(unit, addrs, opts);
14600Sstevel@tonic-gate     else if (opts != NULL)
14610Sstevel@tonic-gate 	free_wordlist(opts);
14620Sstevel@tonic-gate     if (addrs != NULL)
14630Sstevel@tonic-gate 	free_wordlist(addrs);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate     (void) fclose(f);
14660Sstevel@tonic-gate     return ret;
14670Sstevel@tonic-gate }
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate /*
14710Sstevel@tonic-gate  * get_pap_passwd - get a password for authenticating ourselves with
14720Sstevel@tonic-gate  * our peer using PAP.  Returns 1 on success, 0 if no suitable password
14730Sstevel@tonic-gate  * could be found.
14740Sstevel@tonic-gate  * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
14750Sstevel@tonic-gate  */
14760Sstevel@tonic-gate static int
get_pap_passwd(passwd)14770Sstevel@tonic-gate get_pap_passwd(passwd)
14780Sstevel@tonic-gate     char *passwd;
14790Sstevel@tonic-gate {
14800Sstevel@tonic-gate     char *filename;
14810Sstevel@tonic-gate     FILE *f;
14820Sstevel@tonic-gate     int ret;
14830Sstevel@tonic-gate     char secret[MAXWORDLEN];
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate     /*
14860Sstevel@tonic-gate      * Check whether a plugin wants to supply this.
14870Sstevel@tonic-gate      */
14880Sstevel@tonic-gate     if (pap_passwd_hook != NULL) {
14890Sstevel@tonic-gate 	ret = (*pap_passwd_hook)(user, passwd);
14900Sstevel@tonic-gate 	if (ret >= 0)
14910Sstevel@tonic-gate 	    return ret;
14920Sstevel@tonic-gate     }
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate     filename = _PATH_UPAPFILE;
14950Sstevel@tonic-gate     f = fopen(filename, "r");
14960Sstevel@tonic-gate     if (f == NULL)
14970Sstevel@tonic-gate 	return 0;
14980Sstevel@tonic-gate     check_access(f, filename);
14990Sstevel@tonic-gate     ret = scan_authfile(f, user,
15000Sstevel@tonic-gate 			(remote_name[0] != '\0' ? remote_name: NULL),
15010Sstevel@tonic-gate 			secret, NULL, NULL, filename);
15020Sstevel@tonic-gate     (void) fclose(f);
15030Sstevel@tonic-gate     if (ret < 0)
15040Sstevel@tonic-gate 	return 0;
15050Sstevel@tonic-gate     if (passwd != NULL)
15060Sstevel@tonic-gate 	(void) strlcpy(passwd, secret, MAXSECRETLEN);
15070Sstevel@tonic-gate     BZERO(secret, sizeof(secret));
15080Sstevel@tonic-gate     return 1;
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate /*
15130Sstevel@tonic-gate  * have_pap_secret - check whether we have a PAP file with any
15140Sstevel@tonic-gate  * secrets that we could possibly use for authenticating the peer.
15150Sstevel@tonic-gate  */
15160Sstevel@tonic-gate static int
have_pap_secret(lacks_ipp)15170Sstevel@tonic-gate have_pap_secret(lacks_ipp)
15180Sstevel@tonic-gate     int *lacks_ipp;
15190Sstevel@tonic-gate {
15200Sstevel@tonic-gate     FILE *f;
15210Sstevel@tonic-gate     int ret;
15220Sstevel@tonic-gate     char *filename;
15230Sstevel@tonic-gate     struct wordlist *addrs;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate     /* let the plugin decide, if there is one */
15260Sstevel@tonic-gate     if (pap_check_hook != NULL) {
15270Sstevel@tonic-gate 	ret = (*pap_check_hook)();
15280Sstevel@tonic-gate 	if (ret >= 0)
15290Sstevel@tonic-gate 	    return ret;
15300Sstevel@tonic-gate     }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate     filename = _PATH_UPAPFILE;
15330Sstevel@tonic-gate     f = fopen(filename, "r");
15340Sstevel@tonic-gate     if (f == NULL)
15350Sstevel@tonic-gate 	return 0;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate     ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
15380Sstevel@tonic-gate 			NULL, &addrs, NULL, filename);
15390Sstevel@tonic-gate     (void) fclose(f);
15400Sstevel@tonic-gate     if (ret >= 0 && !some_ip_ok(addrs)) {
15410Sstevel@tonic-gate 	if (lacks_ipp != NULL)
15420Sstevel@tonic-gate 	    *lacks_ipp = 1;
15430Sstevel@tonic-gate 	ret = -1;
15440Sstevel@tonic-gate     }
15450Sstevel@tonic-gate     if (addrs != NULL)
15460Sstevel@tonic-gate 	free_wordlist(addrs);
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate     return ret >= 0;
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate /*
15530Sstevel@tonic-gate  * have_chap_secret - check whether we have a CHAP file with a secret
15540Sstevel@tonic-gate  * that we could possibly use for authenticating `client' on `server'.
15550Sstevel@tonic-gate  * Either or both can be the null string, meaning we don't know the
15560Sstevel@tonic-gate  * identity yet.
15570Sstevel@tonic-gate  */
15580Sstevel@tonic-gate static int
have_chap_secret(client,server,need_ip,lacks_ipp)15590Sstevel@tonic-gate have_chap_secret(client, server, need_ip, lacks_ipp)
15600Sstevel@tonic-gate     char *client;
15610Sstevel@tonic-gate     char *server;
15620Sstevel@tonic-gate     int need_ip;
15630Sstevel@tonic-gate     int *lacks_ipp;
15640Sstevel@tonic-gate {
15650Sstevel@tonic-gate     FILE *f;
15660Sstevel@tonic-gate     int ret;
15670Sstevel@tonic-gate     char *filename;
15680Sstevel@tonic-gate     struct wordlist *addrs;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate     filename = _PATH_CHAPFILE;
15710Sstevel@tonic-gate     f = fopen(filename, "r");
15720Sstevel@tonic-gate     if (f == NULL)
15730Sstevel@tonic-gate 	return 0;
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate     if (client != NULL && client[0] == '\0')
15760Sstevel@tonic-gate 	client = NULL;
15770Sstevel@tonic-gate     if (server != NULL && server[0] == '\0')
15780Sstevel@tonic-gate 	server = NULL;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate     ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename);
15810Sstevel@tonic-gate     (void) fclose(f);
15820Sstevel@tonic-gate     if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
15830Sstevel@tonic-gate 	if (lacks_ipp != NULL)
15840Sstevel@tonic-gate 	    *lacks_ipp = 1;
15850Sstevel@tonic-gate 	ret = -1;
15860Sstevel@tonic-gate     }
15870Sstevel@tonic-gate     if (addrs != NULL)
15880Sstevel@tonic-gate 	free_wordlist(addrs);
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate     return ret >= 0;
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate /*
15950Sstevel@tonic-gate  * get_secret - open the CHAP secret file and return the secret
15960Sstevel@tonic-gate  * for authenticating the given client on the given server.
15970Sstevel@tonic-gate  *
15980Sstevel@tonic-gate  *	"am_server" means that we're the authenticator (demanding
15990Sstevel@tonic-gate  *	identity from the peer).
16000Sstevel@tonic-gate  *
16010Sstevel@tonic-gate  *	"!am_server" means that we're the authenticatee (supplying
16020Sstevel@tonic-gate  *	identity to the peer).
16030Sstevel@tonic-gate  */
16040Sstevel@tonic-gate int
get_secret(unit,client,server,secret,secret_len,am_server)16050Sstevel@tonic-gate get_secret(unit, client, server, secret, secret_len, am_server)
16060Sstevel@tonic-gate     int unit;
16070Sstevel@tonic-gate     char *client;
16080Sstevel@tonic-gate     char *server;
16090Sstevel@tonic-gate     char *secret;
16100Sstevel@tonic-gate     int *secret_len;
16110Sstevel@tonic-gate     int am_server;
16120Sstevel@tonic-gate {
16130Sstevel@tonic-gate     FILE *f;
16140Sstevel@tonic-gate     int ret, len;
16150Sstevel@tonic-gate     char *filename;
16160Sstevel@tonic-gate     struct wordlist *addrs, *opts;
16170Sstevel@tonic-gate     char secbuf[MAXWORDLEN];
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate     /*
16200Sstevel@tonic-gate      * Support the 'password' option on authenticatee only in order to
16210Sstevel@tonic-gate      * avoid obvious security problem (authenticator and authenticatee
16220Sstevel@tonic-gate      * within a given implementation must never share secrets).
16230Sstevel@tonic-gate      */
16240Sstevel@tonic-gate     if (!am_server && passwd[0] != '\0') {
16250Sstevel@tonic-gate 	(void) strlcpy(secbuf, passwd, sizeof(secbuf));
16260Sstevel@tonic-gate     } else {
16270Sstevel@tonic-gate 	filename = _PATH_CHAPFILE;
16280Sstevel@tonic-gate 	addrs = NULL;
16290Sstevel@tonic-gate 	secbuf[0] = '\0';
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	f = fopen(filename, "r");
16320Sstevel@tonic-gate 	if (f == NULL) {
16330Sstevel@tonic-gate 	    error("Can't open chap secret file %s: %m", filename);
16340Sstevel@tonic-gate 	    return 0;
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate 	check_access(f, filename);
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	ret = scan_authfile(f, client, server, secbuf, &addrs, &opts,
16390Sstevel@tonic-gate 	    filename);
16400Sstevel@tonic-gate 	(void) fclose(f);
16410Sstevel@tonic-gate 	if (ret < 0) {
16420Sstevel@tonic-gate 	    if (scan_server_match_failed[0] != '\0')
16430Sstevel@tonic-gate 		warn("possible configuration error: local name is %q, but "
16440Sstevel@tonic-gate 		    "found %q instead", our_name, scan_server_match_failed);
16450Sstevel@tonic-gate 	    return 0;
16460Sstevel@tonic-gate 	}
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	/* Only the authenticator cares about limiting peer addresses. */
16490Sstevel@tonic-gate 	if (am_server)
16500Sstevel@tonic-gate 	    set_allowed_addrs(unit, addrs, opts);
16510Sstevel@tonic-gate 	else if (opts != NULL)
16520Sstevel@tonic-gate 	    free_wordlist(opts);
16530Sstevel@tonic-gate 	if (addrs != NULL)
16540Sstevel@tonic-gate 	    free_wordlist(addrs);
16550Sstevel@tonic-gate     }
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate     len = strlen(secbuf);
16580Sstevel@tonic-gate     if (len > MAXSECRETLEN) {
16590Sstevel@tonic-gate 	error("Secret for %s on %s is too long", client, server);
16600Sstevel@tonic-gate 	len = MAXSECRETLEN;
16610Sstevel@tonic-gate     }
16620Sstevel@tonic-gate     /* Do not leave a temporary copy of the secret on the stack. */
16630Sstevel@tonic-gate     BCOPY(secbuf, secret, len);
16640Sstevel@tonic-gate     BZERO(secbuf, sizeof(secbuf));
16650Sstevel@tonic-gate     *secret_len = len;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate     return 1;
16680Sstevel@tonic-gate }
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate /*
16710Sstevel@tonic-gate  * set_allowed_addrs() - set the list of allowed addresses.
16720Sstevel@tonic-gate  * The caller must also look for `--' indicating options to apply for
16730Sstevel@tonic-gate  * this peer and leaves the following words in extra_options.
16740Sstevel@tonic-gate  */
16750Sstevel@tonic-gate static void
set_allowed_addrs(unit,addrs,opts)16760Sstevel@tonic-gate set_allowed_addrs(unit, addrs, opts)
16770Sstevel@tonic-gate     int unit;
16780Sstevel@tonic-gate     struct wordlist *addrs;
16790Sstevel@tonic-gate     struct wordlist *opts;
16800Sstevel@tonic-gate {
16810Sstevel@tonic-gate     int n;
16820Sstevel@tonic-gate     struct wordlist *ap, **pap;
16830Sstevel@tonic-gate     struct permitted_ip *ip;
16840Sstevel@tonic-gate     char *ptr_word, *ptr_mask;
16850Sstevel@tonic-gate     struct hostent *hp;
16860Sstevel@tonic-gate     struct netent *np;
16870Sstevel@tonic-gate     u_int32_t a, mask, newmask, ah, offset;
16880Sstevel@tonic-gate     struct ipcp_options *wo = &ipcp_wantoptions[unit];
16890Sstevel@tonic-gate     u_int32_t suggested_ip = 0;
16900Sstevel@tonic-gate     int err_num;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate     if (addresses[unit] != NULL)
16930Sstevel@tonic-gate 	free(addresses[unit]);
16940Sstevel@tonic-gate     addresses[unit] = NULL;
16950Sstevel@tonic-gate     if (extra_options != NULL)
16960Sstevel@tonic-gate 	free_wordlist(extra_options);
16970Sstevel@tonic-gate     extra_options = opts;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate     /*
17000Sstevel@tonic-gate      * Count the number of IP addresses given.
17010Sstevel@tonic-gate      */
17020Sstevel@tonic-gate     for (n = 0, pap = &addrs; (ap = *pap) != NULL; pap = &ap->next)
17030Sstevel@tonic-gate 	++n;
17040Sstevel@tonic-gate     if (n == 0)
17050Sstevel@tonic-gate 	return;
17060Sstevel@tonic-gate     ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
17070Sstevel@tonic-gate     if (ip == NULL)
17080Sstevel@tonic-gate 	return;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate     n = 0;
17110Sstevel@tonic-gate     for (ap = addrs; ap != NULL; ap = ap->next) {
17120Sstevel@tonic-gate 	/* "-" means no addresses authorized, "*" means any address allowed */
17130Sstevel@tonic-gate 	ptr_word = ap->word;
17140Sstevel@tonic-gate 	if (strcmp(ptr_word, "-") == 0)
17150Sstevel@tonic-gate 	    break;
17160Sstevel@tonic-gate 	if (strcmp(ptr_word, "*") == 0) {
17170Sstevel@tonic-gate 	    ip[n].permit = 1;
17180Sstevel@tonic-gate 	    ip[n].base = ip[n].mask = 0;
17190Sstevel@tonic-gate 	    ++n;
17200Sstevel@tonic-gate 	    break;
17210Sstevel@tonic-gate 	}
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	ip[n].permit = 1;
17240Sstevel@tonic-gate 	if (*ptr_word == '!') {
17250Sstevel@tonic-gate 	    ip[n].permit = 0;
17260Sstevel@tonic-gate 	    ++ptr_word;
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	mask = ~ (u_int32_t) 0;
17300Sstevel@tonic-gate 	offset = 0;
17310Sstevel@tonic-gate 	ptr_mask = strchr (ptr_word, '/');
17320Sstevel@tonic-gate 	if (ptr_mask != NULL) {
17330Sstevel@tonic-gate 	    int bit_count;
17340Sstevel@tonic-gate 	    char *endp;
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	    bit_count = (int) strtol (ptr_mask+1, &endp, 10);
17370Sstevel@tonic-gate 	    if (bit_count <= 0 || bit_count > 32) {
17380Sstevel@tonic-gate 		warn("invalid address length %v in authorized address list",
17390Sstevel@tonic-gate 		     ptr_mask+1);
17400Sstevel@tonic-gate 		continue;
17410Sstevel@tonic-gate 	    }
17420Sstevel@tonic-gate 	    bit_count = 32 - bit_count;	/* # bits in host part */
17430Sstevel@tonic-gate 	    if (*endp == '+') {
17440Sstevel@tonic-gate 		offset = ifunit + 1;
17450Sstevel@tonic-gate 		++endp;
17460Sstevel@tonic-gate 	    }
17470Sstevel@tonic-gate 	    if (*endp != '\0') {
17480Sstevel@tonic-gate 		warn("invalid address length syntax: %v", ptr_mask+1);
17490Sstevel@tonic-gate 		continue;
17500Sstevel@tonic-gate 	    }
17510Sstevel@tonic-gate 	    *ptr_mask = '\0';
17520Sstevel@tonic-gate 	    mask <<= bit_count;
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	/* Try to interpret value as host name or numeric address first */
17560Sstevel@tonic-gate 	hp = getipnodebyname(ptr_word, AF_INET, 0, &err_num);
17570Sstevel@tonic-gate 	if (hp != NULL) {
17580Sstevel@tonic-gate 	    (void) memcpy(&a, hp->h_addr, sizeof(a));
17590Sstevel@tonic-gate 	    freehostent(hp);
17600Sstevel@tonic-gate 	} else {
17610Sstevel@tonic-gate 	    char *cp = ptr_word + strlen(ptr_word);
17620Sstevel@tonic-gate 	    if (cp > ptr_word)
17630Sstevel@tonic-gate 		cp--;
17640Sstevel@tonic-gate 	    if (*cp == '+') {
17650Sstevel@tonic-gate 		offset = ifunit + 1;
17660Sstevel@tonic-gate 		*cp = '\0';
17670Sstevel@tonic-gate 	    }
17680Sstevel@tonic-gate 	    np = getnetbyname (ptr_word);
17690Sstevel@tonic-gate 	    if (np != NULL && np->n_addrtype == AF_INET) {
17700Sstevel@tonic-gate 		ah = np->n_net;
17710Sstevel@tonic-gate 		newmask = (u_int32_t)~0;
17720Sstevel@tonic-gate 		if ((ah & 0xff000000ul) == 0)
17730Sstevel@tonic-gate 		    ah <<= 8, newmask <<= 8;
17740Sstevel@tonic-gate 		if ((ah & 0xff000000ul) == 0)
17750Sstevel@tonic-gate 		    ah <<= 8, newmask <<= 8;
17760Sstevel@tonic-gate 		if ((ah & 0xff000000ul) == 0)
17770Sstevel@tonic-gate 		    ah <<= 8, newmask <<= 8;
17780Sstevel@tonic-gate 		if (ptr_mask == NULL)
17790Sstevel@tonic-gate 		    mask = newmask;
17800Sstevel@tonic-gate 		a = htonl(ah);
17810Sstevel@tonic-gate 	    }
17820Sstevel@tonic-gate 	}
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	if (ptr_mask != NULL)
17850Sstevel@tonic-gate 	    *ptr_mask = '/';
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	if (a == (u_int32_t)-1L) {
17880Sstevel@tonic-gate 	    warn("unknown host %s in auth. address list", ap->word);
17890Sstevel@tonic-gate 	    continue;
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 	if (offset != 0) {
17920Sstevel@tonic-gate 	    if (offset >= ~mask) {
17930Sstevel@tonic-gate 		warn("interface unit %d too large for subnet %v",
17940Sstevel@tonic-gate 		     ifunit, ptr_word);
17950Sstevel@tonic-gate 		continue;
17960Sstevel@tonic-gate 	    }
17970Sstevel@tonic-gate 	    a = htonl((ntohl(a) & mask) + offset);
17980Sstevel@tonic-gate 	    mask = ~(u_int32_t)0;
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate 	ip[n].mask = htonl(mask);
18010Sstevel@tonic-gate 	ip[n].base = a & ip[n].mask;
18020Sstevel@tonic-gate 	++n;
18030Sstevel@tonic-gate 	if (~mask == 0 && suggested_ip == 0)
18040Sstevel@tonic-gate 	    suggested_ip = a;
18050Sstevel@tonic-gate     }
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate     /* Sentinel value at end of list */
18080Sstevel@tonic-gate     ip[n].permit = 0;		/* make the last entry forbid all addresses */
18090Sstevel@tonic-gate     ip[n].base = 0;		/* to terminate the list */
18100Sstevel@tonic-gate     ip[n].mask = 0;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate     addresses[unit] = ip;
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate     /*
18150Sstevel@tonic-gate      * If the address given for the peer isn't authorized, or if
18160Sstevel@tonic-gate      * the user hasn't given one, AND there is an authorized address
18170Sstevel@tonic-gate      * which is a single host, then use that if we find one.
18180Sstevel@tonic-gate      */
18190Sstevel@tonic-gate     if (suggested_ip != 0
18200Sstevel@tonic-gate 	&& (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr)))
18210Sstevel@tonic-gate 	wo->hisaddr = suggested_ip;
18220Sstevel@tonic-gate }
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate /*
18250Sstevel@tonic-gate  * auth_ip_addr - check whether the peer is authorized to use
18260Sstevel@tonic-gate  * a given IP address.  Returns 1 if authorized, 0 otherwise.
18270Sstevel@tonic-gate  */
18280Sstevel@tonic-gate int
auth_ip_addr(unit,addr)18290Sstevel@tonic-gate auth_ip_addr(unit, addr)
18300Sstevel@tonic-gate     int unit;
18310Sstevel@tonic-gate     u_int32_t addr;
18320Sstevel@tonic-gate {
18330Sstevel@tonic-gate     int ok;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate     /* don't allow loopback or multicast address */
18360Sstevel@tonic-gate     if (bad_ip_adrs(addr))
18370Sstevel@tonic-gate 	return 0;
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate     if (addresses[unit] != NULL) {
18400Sstevel@tonic-gate 	ok = ip_addr_check(addr, addresses[unit]);
18410Sstevel@tonic-gate 	if (ok >= 0)
18420Sstevel@tonic-gate 	    return ok;
18430Sstevel@tonic-gate     }
18440Sstevel@tonic-gate     if (auth_required)
18450Sstevel@tonic-gate 	return 0;		/* no addresses authorized */
18460Sstevel@tonic-gate     return allow_any_ip || privileged || !have_route_to(addr);
18470Sstevel@tonic-gate }
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate static int
ip_addr_check(addr,addrs)18500Sstevel@tonic-gate ip_addr_check(addr, addrs)
18510Sstevel@tonic-gate     u_int32_t addr;
18520Sstevel@tonic-gate     struct permitted_ip *addrs;
18530Sstevel@tonic-gate {
18540Sstevel@tonic-gate     /* This loop is safe because of the sentinel value in set_allowed_addrs */
18550Sstevel@tonic-gate     for (; ; ++addrs)
18560Sstevel@tonic-gate 	if ((addr & addrs->mask) == addrs->base)
18570Sstevel@tonic-gate 	    return addrs->permit;
18580Sstevel@tonic-gate }
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate /*
18610Sstevel@tonic-gate  * bad_ip_adrs - return 1 if the IP address is one we don't want
18620Sstevel@tonic-gate  * to use, such as an address in the loopback net or a multicast address.
18630Sstevel@tonic-gate  * addr is in network byte order.
18640Sstevel@tonic-gate  */
18650Sstevel@tonic-gate int
bad_ip_adrs(addr)18660Sstevel@tonic-gate bad_ip_adrs(addr)
18670Sstevel@tonic-gate     u_int32_t addr;
18680Sstevel@tonic-gate {
18690Sstevel@tonic-gate     addr = ntohl(addr);
18700Sstevel@tonic-gate     return
18710Sstevel@tonic-gate #ifndef ALLOW_127_NET
18720Sstevel@tonic-gate 	(addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
18730Sstevel@tonic-gate #endif
18740Sstevel@tonic-gate #ifndef ALLOW_0_NET
18750Sstevel@tonic-gate 	((addr >> IN_CLASSA_NSHIFT) == 0 && addr != 0) ||
18760Sstevel@tonic-gate #endif
1877*5577Ssangeeta 	IN_MULTICAST(addr);
18780Sstevel@tonic-gate }
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate /*
18810Sstevel@tonic-gate  * some_ip_ok - check a wordlist to see if it authorizes any
18820Sstevel@tonic-gate  * IP address(es).
18830Sstevel@tonic-gate  */
18840Sstevel@tonic-gate static int
some_ip_ok(addrs)18850Sstevel@tonic-gate some_ip_ok(addrs)
18860Sstevel@tonic-gate     struct wordlist *addrs;
18870Sstevel@tonic-gate {
18880Sstevel@tonic-gate     for (; addrs != NULL; addrs = addrs->next) {
18890Sstevel@tonic-gate 	if (addrs->word[0] == '-')
18900Sstevel@tonic-gate 	    break;
18910Sstevel@tonic-gate 	if (addrs->word[0] != '!')
18920Sstevel@tonic-gate 	    return 1;		/* some IP address is allowed */
18930Sstevel@tonic-gate     }
18940Sstevel@tonic-gate     return 0;
18950Sstevel@tonic-gate }
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate /*
18980Sstevel@tonic-gate  * check_access - complain if a secret file has too-liberal permissions.
18990Sstevel@tonic-gate  */
19000Sstevel@tonic-gate static void
check_access(f,filename)19010Sstevel@tonic-gate check_access(f, filename)
19020Sstevel@tonic-gate     FILE *f;
19030Sstevel@tonic-gate     char *filename;
19040Sstevel@tonic-gate {
19050Sstevel@tonic-gate     struct stat sbuf;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate     if (fstat(fileno(f), &sbuf) < 0) {
19080Sstevel@tonic-gate 	warn("cannot stat secret file %s: %m", filename);
19090Sstevel@tonic-gate     } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
19100Sstevel@tonic-gate 	warn("Warning - secret file %s has world and/or group access",
19110Sstevel@tonic-gate 	     filename);
19120Sstevel@tonic-gate     }
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate /*
19170Sstevel@tonic-gate  * scan_authfile - Scan an authorization file for a secret suitable
19180Sstevel@tonic-gate  * for authenticating `client' on `server'.  The return value is -1 if
19190Sstevel@tonic-gate  * no secret is found, otherwise >= 0.  The return value has
19200Sstevel@tonic-gate  * NONWILD_CLIENT set if the secret didn't have "*" for the client,
19210Sstevel@tonic-gate  * and NONWILD_SERVER set if the secret didn't have "*" for the
19220Sstevel@tonic-gate  * server.
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate  * Any following words on the line up to a "--" (i.e. address
19250Sstevel@tonic-gate  * authorization info) are placed in a wordlist and returned in
19260Sstevel@tonic-gate  * *addrs.  Any following words (extra options) are placed in a
19270Sstevel@tonic-gate  * wordlist and returned in *opts.  If opts is NULL, these are just
19280Sstevel@tonic-gate  * discarded.  Otherwise, the extra_opt_* variables are set to
19290Sstevel@tonic-gate  * indicate the source of the options.
19300Sstevel@tonic-gate  *
19310Sstevel@tonic-gate  * We assume secret is NULL or points to MAXWORDLEN bytes of space.
19320Sstevel@tonic-gate  */
19330Sstevel@tonic-gate static int
scan_authfile(f,client,server,secret,addrs,opts,filename)19340Sstevel@tonic-gate scan_authfile(f, client, server, secret, addrs, opts, filename)
19350Sstevel@tonic-gate     FILE *f;
19360Sstevel@tonic-gate     char *client;
19370Sstevel@tonic-gate     char *server;
19380Sstevel@tonic-gate     char *secret;
19390Sstevel@tonic-gate     struct wordlist **addrs;
19400Sstevel@tonic-gate     struct wordlist **opts;
19410Sstevel@tonic-gate     char *filename;
19420Sstevel@tonic-gate {
19430Sstevel@tonic-gate     int newline, xxx, sline;
19440Sstevel@tonic-gate     int got_flag, best_flag;
19450Sstevel@tonic-gate     FILE *sf;
19460Sstevel@tonic-gate     struct wordlist *ap, *addr_list, *alist, **app;
19470Sstevel@tonic-gate     char word[MAXWORDLEN];
19480Sstevel@tonic-gate     char atfile[MAXWORDLEN];
19490Sstevel@tonic-gate     char lsecret[MAXWORDLEN];
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate     scan_server_match_failed[0] = '\0';
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate     if (addrs != NULL)
19540Sstevel@tonic-gate 	*addrs = NULL;
19550Sstevel@tonic-gate     if (opts != NULL)
19560Sstevel@tonic-gate 	*opts = NULL;
19570Sstevel@tonic-gate     addr_list = NULL;
19580Sstevel@tonic-gate     option_line = 0;
19590Sstevel@tonic-gate     if (!getword(f, word, &newline, filename)) {
19600Sstevel@tonic-gate 	if (debug)
19610Sstevel@tonic-gate 	    dbglog("%s is apparently empty", filename);
19620Sstevel@tonic-gate 	return -1;		/* file is empty??? */
19630Sstevel@tonic-gate     }
19640Sstevel@tonic-gate     newline = 1;
19650Sstevel@tonic-gate     best_flag = -1;
19660Sstevel@tonic-gate     for (;;) {
19670Sstevel@tonic-gate 	/*
19680Sstevel@tonic-gate 	 * Skip until we find a word at the start of a line.
19690Sstevel@tonic-gate 	 */
19700Sstevel@tonic-gate 	while (!newline && getword(f, word, &newline, filename))
19710Sstevel@tonic-gate 	    ;
19720Sstevel@tonic-gate 	if (!newline)
19730Sstevel@tonic-gate 	    break;		/* got to end of file */
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	sline = option_line;
19760Sstevel@tonic-gate 	/*
19770Sstevel@tonic-gate 	 * Got a client - check if it's a match or a wildcard.
19780Sstevel@tonic-gate 	 */
19790Sstevel@tonic-gate 	got_flag = 0;
19800Sstevel@tonic-gate 	if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
19810Sstevel@tonic-gate 	    newline = 0;
19820Sstevel@tonic-gate 	    continue;
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 	if (!ISWILD(word))
19850Sstevel@tonic-gate 	    got_flag = NONWILD_CLIENT;
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	/*
19880Sstevel@tonic-gate 	 * Now get a server and check if it matches.
19890Sstevel@tonic-gate 	 */
19900Sstevel@tonic-gate 	if (!getword(f, word, &newline, filename))
19910Sstevel@tonic-gate 	    break;
19920Sstevel@tonic-gate 	if (newline)
19930Sstevel@tonic-gate 	    continue;
19940Sstevel@tonic-gate 	if (!ISWILD(word)) {
19950Sstevel@tonic-gate 	    if (server != NULL && strcmp(word, server) != 0) {
19960Sstevel@tonic-gate 		(void) strcpy(scan_server_match_failed, word);
19970Sstevel@tonic-gate 		continue;
19980Sstevel@tonic-gate 	    }
19990Sstevel@tonic-gate 	    got_flag |= NONWILD_SERVER;
20000Sstevel@tonic-gate 	}
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	/*
20030Sstevel@tonic-gate 	 * Got some sort of a match - see if it's better than what
20040Sstevel@tonic-gate 	 * we have already.
20050Sstevel@tonic-gate 	 */
20060Sstevel@tonic-gate 	if (got_flag <= best_flag)
20070Sstevel@tonic-gate 	    continue;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	/*
20100Sstevel@tonic-gate 	 * Get the secret.
20110Sstevel@tonic-gate 	 */
20120Sstevel@tonic-gate 	if (!getword(f, word, &newline, filename))
20130Sstevel@tonic-gate 	    break;
20140Sstevel@tonic-gate 	if (newline)
20150Sstevel@tonic-gate 	    continue;
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	/*
20180Sstevel@tonic-gate 	 * Special syntax: @filename means read secret from file.
20190Sstevel@tonic-gate 	 * Because the secrets files are modifiable only by root,
20200Sstevel@tonic-gate 	 * it's safe to open this file as root.  One small addition --
20210Sstevel@tonic-gate 	 * if open fails, we try as the regular user; just in case
20220Sstevel@tonic-gate 	 * it's over NFS and not root-equivalent.
20230Sstevel@tonic-gate 	 */
20240Sstevel@tonic-gate 	if (word[0] == '@') {
20250Sstevel@tonic-gate 	    (void) strlcpy(atfile, word+1, sizeof(atfile));
20260Sstevel@tonic-gate 	    if ((sf = fopen(atfile, "r")) == NULL) {
20270Sstevel@tonic-gate 		(void) seteuid(getuid());
20280Sstevel@tonic-gate 		sf = fopen(atfile, "r");
20290Sstevel@tonic-gate 		(void) seteuid(0);
20300Sstevel@tonic-gate 	    }
20310Sstevel@tonic-gate 	    if (sf == NULL) {
20320Sstevel@tonic-gate 		warn("can't open indirect secret file %s: %m", atfile);
20330Sstevel@tonic-gate 		continue;
20340Sstevel@tonic-gate 	    }
20350Sstevel@tonic-gate 	    check_access(sf, atfile);
20360Sstevel@tonic-gate 	    if (!getword(sf, word, &xxx, atfile)) {
20370Sstevel@tonic-gate 		warn("no secret in indirect secret file %s", atfile);
20380Sstevel@tonic-gate 		(void) fclose(sf);
20390Sstevel@tonic-gate 		continue;
20400Sstevel@tonic-gate 	    }
20410Sstevel@tonic-gate 	    (void) fclose(sf);
20420Sstevel@tonic-gate 	}
20430Sstevel@tonic-gate 	if (secret != NULL)
20440Sstevel@tonic-gate 	    (void) strlcpy(lsecret, word, sizeof(lsecret));
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	/*
20470Sstevel@tonic-gate 	 * Now read address authorization info and make a wordlist.
20480Sstevel@tonic-gate 	 */
20490Sstevel@tonic-gate 	app = &alist;
20500Sstevel@tonic-gate 	for (;;) {
20510Sstevel@tonic-gate 	    if (!getword(f, word, &newline, filename) || newline)
20520Sstevel@tonic-gate 		break;
20530Sstevel@tonic-gate 	    ap = (struct wordlist *) malloc(sizeof(struct wordlist));
20540Sstevel@tonic-gate 	    if (ap == NULL)
20550Sstevel@tonic-gate 		novm("authorized addresses");
20560Sstevel@tonic-gate 	    ap->word = strdup(word);
20570Sstevel@tonic-gate 	    if (ap->word == NULL)
20580Sstevel@tonic-gate 		novm("authorized addresses");
20590Sstevel@tonic-gate 	    *app = ap;
20600Sstevel@tonic-gate 	    app = &ap->next;
20610Sstevel@tonic-gate 	}
20620Sstevel@tonic-gate 	*app = NULL;
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	/*
20650Sstevel@tonic-gate 	 * This is the best so far; remember it.
20660Sstevel@tonic-gate 	 */
20670Sstevel@tonic-gate 	best_flag = got_flag;
20680Sstevel@tonic-gate 	if (addr_list != NULL)
20690Sstevel@tonic-gate 	    free_wordlist(addr_list);
20700Sstevel@tonic-gate 	addr_list = alist;
20710Sstevel@tonic-gate 	if (secret != NULL)
20720Sstevel@tonic-gate 	    (void) strlcpy(secret, lsecret, MAXWORDLEN);
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	if (opts != NULL) {
20750Sstevel@tonic-gate 	    extra_opt_filename = filename;
20760Sstevel@tonic-gate 	    extra_opt_line = sline;
20770Sstevel@tonic-gate 	}
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	if (!newline)
20800Sstevel@tonic-gate 	    break;
20810Sstevel@tonic-gate     }
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate     /* scan for a -- word indicating the start of options */
20840Sstevel@tonic-gate     for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
20850Sstevel@tonic-gate 	if (strcmp(ap->word, "--") == 0)
20860Sstevel@tonic-gate 	    break;
20870Sstevel@tonic-gate     /* ap = start of options */
20880Sstevel@tonic-gate     if (ap != NULL) {
20890Sstevel@tonic-gate 	ap = ap->next;		/* first option */
20900Sstevel@tonic-gate 	free(*app);			/* free the "--" word */
20910Sstevel@tonic-gate 	*app = NULL;		/* terminate addr list */
20920Sstevel@tonic-gate     }
20930Sstevel@tonic-gate     if (opts != NULL)
20940Sstevel@tonic-gate 	*opts = ap;
20950Sstevel@tonic-gate     else if (ap != NULL)
20960Sstevel@tonic-gate 	free_wordlist(ap);
20970Sstevel@tonic-gate     if (addrs != NULL)
20980Sstevel@tonic-gate 	*addrs = addr_list;
20990Sstevel@tonic-gate     else if (addr_list != NULL)
21000Sstevel@tonic-gate 	free_wordlist(addr_list);
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate     return best_flag;
21030Sstevel@tonic-gate }
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate /*
21060Sstevel@tonic-gate  * free_wordlist - release memory allocated for a wordlist.
21070Sstevel@tonic-gate  */
21080Sstevel@tonic-gate static void
free_wordlist(wp)21090Sstevel@tonic-gate free_wordlist(wp)
21100Sstevel@tonic-gate     struct wordlist *wp;
21110Sstevel@tonic-gate {
21120Sstevel@tonic-gate     struct wordlist *next;
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate     while (wp != NULL) {
21150Sstevel@tonic-gate 	next = wp->next;
21160Sstevel@tonic-gate 	free(wp);
21170Sstevel@tonic-gate 	wp = next;
21180Sstevel@tonic-gate     }
21190Sstevel@tonic-gate }
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate /*
21220Sstevel@tonic-gate  * auth_script_done - called when the auth-up or auth-down script
21230Sstevel@tonic-gate  * has finished.
21240Sstevel@tonic-gate  */
21250Sstevel@tonic-gate /*ARGSUSED*/
21260Sstevel@tonic-gate static void
auth_script_done(arg,status)21270Sstevel@tonic-gate auth_script_done(arg, status)
21280Sstevel@tonic-gate     void *arg;
21290Sstevel@tonic-gate     int status;
21300Sstevel@tonic-gate {
21310Sstevel@tonic-gate     auth_script_pid = 0;
21320Sstevel@tonic-gate     switch (auth_script_state) {
21330Sstevel@tonic-gate     case s_up:
21340Sstevel@tonic-gate 	if (auth_state == s_down) {
21350Sstevel@tonic-gate 	    auth_script_state = s_down;
21360Sstevel@tonic-gate 	    auth_script(_PATH_AUTHDOWN);
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 	break;
21390Sstevel@tonic-gate     case s_down:
21400Sstevel@tonic-gate 	if (auth_state == s_up) {
21410Sstevel@tonic-gate 	    auth_script_state = s_up;
21420Sstevel@tonic-gate 	    auth_script(_PATH_AUTHUP);
21430Sstevel@tonic-gate 	}
21440Sstevel@tonic-gate 	break;
21450Sstevel@tonic-gate     }
21460Sstevel@tonic-gate }
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate /*
21490Sstevel@tonic-gate  * auth_script - execute a script with arguments
21500Sstevel@tonic-gate  * interface-name peer-name real-user tty speed
21510Sstevel@tonic-gate  */
21520Sstevel@tonic-gate static void
auth_script(script)21530Sstevel@tonic-gate auth_script(script)
21540Sstevel@tonic-gate     char *script;
21550Sstevel@tonic-gate {
21560Sstevel@tonic-gate     char strspeed[32];
21570Sstevel@tonic-gate     struct passwd *pw;
21580Sstevel@tonic-gate     char struid[32];
21590Sstevel@tonic-gate     char *user_name;
21600Sstevel@tonic-gate     char *argv[8];
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate     if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
21630Sstevel@tonic-gate 	user_name = pw->pw_name;
21640Sstevel@tonic-gate     else {
21650Sstevel@tonic-gate 	(void) slprintf(struid, sizeof(struid), "%d", getuid());
21660Sstevel@tonic-gate 	user_name = struid;
21670Sstevel@tonic-gate     }
21680Sstevel@tonic-gate     (void) slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate     argv[0] = script;
21710Sstevel@tonic-gate     argv[1] = ifname;
21720Sstevel@tonic-gate     argv[2] = peer_authname;
21730Sstevel@tonic-gate     argv[3] = user_name;
21740Sstevel@tonic-gate     argv[4] = devnam;
21750Sstevel@tonic-gate     argv[5] = strspeed;
21760Sstevel@tonic-gate     argv[6] = NULL;
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate     auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL);
21790Sstevel@tonic-gate }
2180